| 1 | package Sim::Agent::Journal; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | sub new | ||||||||||
| 7 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 8 | 0 | my ($class, %opts) = @_; | |||||||||
| 9 | |||||||||||
| 10 | 0 | my $self = {}; | |||||||||
| 11 | 0 | bless $self, $class; | |||||||||
| 12 | |||||||||||
| 13 | 0 | my $run_dir = $self->_create_run_dir; | |||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | open my $fh, '>', "$run_dir/journal.log" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | or die "Cannot open journal.log"; | ||||||||||
| 17 | |||||||||||
| 18 | 0 | $self->{run_dir} = $run_dir; | |||||||||
| 19 | 0 | $self->{fh} = $fh; | |||||||||
| 20 | |||||||||||
| 21 | 0 | return $self; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | # ------------------------------------------------------------ | ||||||||||
| 25 | # Public Methods | ||||||||||
| 26 | # ------------------------------------------------------------ | ||||||||||
| 27 | |||||||||||
| 28 | sub log | ||||||||||
| 29 | 0 | ▶ | { | ||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 0 | my ($self, $message) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 0 | my $ts = $self->_timestamp; | |||||||||
| 33 | |||||||||||
| 34 | 0 | print { $self->{fh} } "[$ts] $message\n"; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | sub archive_plan | ||||||||||
| 38 | 0 | ▶ | { | ||||||||
|
Subroutine
archive_plan: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 39 | 0 | my ($self, $plan_file) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | open my $in, '<', $plan_file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | or die "Cannot read plan file: $plan_file"; | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | open my $out, '>', "$self->{run_dir}/plan.sexpr" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | or die "Cannot write archived plan"; | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | while (<$in>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | { | ||||||||||
| 49 | 0 | print {$out} $_; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | close $in; | |||||||||
| 53 | 0 | close $out; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub agent_dir | ||||||||||
| 57 | 0 | ▶ | { | ||||||||
|
Subroutine
agent_dir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 58 | 0 | my ($self, $agent) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 0 | my $dir = "$self->{run_dir}/agents/$agent"; | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | unless (-d $dir) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | { | ||||||||||
| 64 | 0 | ▶ | mkdir $dir | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | or die "Cannot create agent directory: $dir"; | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | return $dir; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | # ------------------------------------------------------------ | ||||||||||
| 72 | # Private Helpers | ||||||||||
| 73 | # ------------------------------------------------------------ | ||||||||||
| 74 | |||||||||||
| 75 | sub _create_run_dir | ||||||||||
| 76 | 0 | ▶ | { | ||||||||
|
Subroutine
_create_run_dir: not called CC=3 SCAR=24.8
| |||||||||||
| 77 | 0 | my ($self) = @_; | |||||||||
| 78 | |||||||||||
| 79 | 0 | my $base = "run_" . $self->_timestamp; | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | mkdir $base | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | or die "Cannot create run dir $base"; | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | mkdir "$base/agents" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | or die "Cannot create agents dir"; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | return $base; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | sub _timestamp | ||||||||||
| 91 | 0 | ▶ | { | ||||||||
|
Subroutine
_timestamp: not called CC=1 SCAR=6.9
| |||||||||||
| 92 | 0 | my ($self) = @_; | |||||||||
| 93 | |||||||||||
| 94 | 0 | my @t = localtime(); | |||||||||
| 95 | |||||||||||
| 96 | 0 | return sprintf( | |||||||||
| 97 | "%04d%02d%02d_%02d%02d%02d", | ||||||||||
| 98 | $t[5] + 1900, | ||||||||||
| 99 | $t[4] + 1, | ||||||||||
| 100 | $t[3], | ||||||||||
| 101 | $t[2], | ||||||||||
| 102 | $t[1], | ||||||||||
| 103 | $t[0], | ||||||||||
| 104 | ); | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | 1; | |||||||||
| 108 | |||||||||||
| 109 | =pod | ||||||||||
| 110 | |||||||||||
| 111 | =head1 NAME | ||||||||||
| 112 | |||||||||||
| 113 | Sim::Agent::Journal - File-backed journaling and run directory management | ||||||||||
| 114 | |||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||
| 116 | |||||||||||
| 117 | Creates the per-run directory, writes journal.log entries, archives the plan, and provides per-agent directories used by LLM adapters. | ||||||||||
| 118 | |||||||||||
| 119 | See L<Sim::Agent>. | ||||||||||
| 120 | |||||||||||
| 121 | =head1 AUTHOR | ||||||||||
| 122 | |||||||||||
| 123 | Gian Luca Brunetti (2026), gianluca.brunetti@gmail.com | ||||||||||
| 124 | |||||||||||
| 125 | =head1 LICENSE | ||||||||||
| 126 | |||||||||||
| 127 | The GNU General Public License v3.0 | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||