lib/Skilletor/AgentsMD.pm

filter: Statementstmt 99.4% 167 / 168 Branchbran 96.3% 79 / 82 Conditioncond 82.3% 28 / 34 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 28 / 28 Podpod 100% 9 / 9 totaltotal 94.7% 325 / 343 CC 59 SCAR 20.4
CC
59
Cov
96%
codex_hook_trusted
15.1
inspect_agents_md
11.5
parse_block
11.0
CRAP 7.7
« lib/Skilletor/Catalog.pm ↑ Summary /usr/local/lib/perl5/5.44.0/CPAN/Meta/Requirements.pm »
1 package Skilletor::AgentsMD;
2 # ABSTRACT: The Codex rules file, the pointer block in a Codex AGENTS.md, and the checks of Codex's config.toml
3  
4 401 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 401 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 401 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 401 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 401 ▶use Skilletor::Error::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 401 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text node_fs_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 401 ▶use Skilletor::JSString qw( js_trim js_ws_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 401 our $VERSION = '0.008000';
14  
15 401 our @EXPORT_OK = qw(
16 BEGIN_MARKER END_MARKER NOTE RULES_FILE RULES_MARKER MARKER_LINE normalize_section
17 rules_file_text parse_rules_file pointer_lines parse_block with_block inspect_agents_md
18 project_doc_limit codex_hook_trusted
19 );
20  
21 401 ▶use constant BEGIN_MARKER => '<!-- skilletor:begin -->';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 401 ▶use constant END_MARKER => '<!-- skilletor:end -->';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 401 ▶use constant NOTE => '<!-- managed by skilletor — edits inside are overwritten -->';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 401 ▶use constant RULES_FILE => 'skilletor-rules.md';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 401 ▶use constant RULES_MARKER => '<!-- skilletor:rules';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 401 ▶use constant RULES_NOTE => '<!-- managed by skilletor — edits are overwritten; change the rule in its source -->';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Any skilletor marker line; a rule body must not contain one. `\b` with JavaScript's ASCII
30 # word characters, so `<!-- skilletor:ruleé` is one too.
31 401 ▶use constant MARKER_LINE => qr/\A<!-- skilletor:(?:begin|end|rules?)\b/a;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 401 my $WS = js_ws_re();
34  
35 # JavaScript's `\S` and `.` (no line terminator), for the reference's patterns.
36 401 my $NOT_WS = qr/[^\t\n\x{0B}\f\r \x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]/;
37 401 my $DOT = qr/[^\n\r\x{2028}\x{2029}]/;
38  
39 401 my $RULE_RE = qr/\A<!-- skilletor:rule ($NOT_WS+) source=($DOT*) -->\z/;
40  
41 2010 ▶sub normalize_section {
Subroutine normalize_section: called CC=1 SCAR=0.0
Pod documented
42 2010 my ( $text ) = @_;
43 2010 $text =~ s/\A(?:[ \t]*\r?\n)+//;
44 2010 $text =~ s/$WS+\z//;
45 2010 return $text."\n";
46 }
47  
48 # text.split("\n"): an empty text is one empty line.
49 5435 ▶sub _lines {
Subroutine _lines: called CC=2 SCAR=6.9
50 5435 my ( $text ) = @_;
51 5435 ▶ return length $text ? split( /\n/, $text, -1 ) : ('');
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 4683 752 5435
52 }
53  
54 # The rule sections among the lines, each running to the next rule marker: { name => { source,
55 # text } }, a later section of one name replacing the earlier one.
56 2262 ▶sub _parse_sections {
Subroutine _parse_sections: called CC=4 SCAR=13.9
57 2262 my ( @lines ) = @_;
58 2262 my ( %sections, $current );
59 3012 ▶ my $flush = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
60 3012 ▶ $sections{ $current->{name} } = { source => $current->{source}, text => normalize_section( join "\n", @{ $current->{lines} } ) }
Branch: 1 branch
BranchTrueFalseTotal
if $current 750 2262 3012
61 if $current;
62 2262 };
63 2262 for my $line (@lines) {
64 11709 ▶ if ( js_trim($line) =~ $RULE_RE ) {
Branch: 2 branches
BranchTrueFalseTotal
if (js_trim($line) =~ /$RULE_RE/) { } 750 10959 11709
elsif ($current) { } 1497 9462 10959
65 750 $flush->();
66 750 $current = { name => $1, source => $2, lines => [] };
67 }
68 elsif ($current) {
69 1497 push @{ $current->{lines} }, $line;
70 }
71 }
72 2262 $flush->();
73 2262 return \%sections;
74 }
75  
76 #### the rules file ##########################################################
77  
78 1212 ▶sub rules_file_text {
Subroutine rules_file_text: called CC=3 SCAR=11.0
Pod documented
79 1212 my ( $scope, $sections ) = @_;
80 1212 ▶ return undef unless @$sections;
Branch: 1 branch
BranchTrueFalseTotal
unless @$sections 49 1163 1212
81 1163 my @out = ( RULES_MARKER.' scope='.$scope.' -->', RULES_NOTE, '' );
82 1163 for my $s (@$sections) {
83 1165 ( my $text = $s->{text} ) =~ s/\n\z//;
84 1165 push @out, '<!-- skilletor:rule '.$s->{name}.' source='.$s->{source}.' -->', _lines($text), '';
85 }
86 1163 ( my $joined = join "\n", @out ) =~ s/\n+\z//;
87 1163 return $joined."\n";
88 }
89  
90 764 ▶sub parse_rules_file {
Subroutine parse_rules_file: called CC=1 SCAR=0.0
Pod documented
91 764 my ( $text ) = @_;
92 764 return _parse_sections( _lines($text) );
93 }
94  
95 #### the pointer block in AGENTS.md ##########################################
96  
97 1156 ▶sub pointer_lines {
Subroutine pointer_lines: called CC=2 SCAR=6.9
Pod documented
98 1156 my ( $scope, $file ) = @_;
99 return (
100 1156 ▶ NOTE,
Branch: 1 branch
BranchTrueFalseTotal
$scope eq 'user' ? : 535 621 1156
101 'Additional rules for '.( $scope eq 'user' ? 'all projects' : 'this project' ).' are managed by skilletor. They are normally provided at',
102 'session start as a developer message beginning with `'.RULES_MARKER.'`. If that message',
103 'is not in your context (for example after context compaction), read',
104 '`'.$file.'` before you start a task, and follow it.',
105 );
106 }
107  
108 1577 ▶sub parse_block {
Subroutine parse_block: called CC=11 SCAR=24.0
Pod documented
109 1577 my ( $text ) = @_;
110 1577 my @lines = _lines($text);
111 1577 my ( @begins, @ends );
112 1577 for my $i ( 0 .. $#lines ) {
113 11963 my $t = js_trim( $lines[$i] );
114 11963 ▶ if ( $t eq BEGIN_MARKER ) { push @begins, $i }
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq '<!-- skilletor:begin -->') { } 1529 10434 11963
elsif ($t eq '<!-- skilletor:end -->') { } 1519 8915 10434
115 1519 elsif ( $t eq END_MARKER ) { push @ends, $i }
116 }
117 1577 ▶ return undef unless @begins || @ends;
Branch: 1 branch
BranchTrueFalseTotal
unless @begins or @ends 52 1525 1577
Condition100%3/3
@begins or @ends
l!l&&r!l&&!r
1521452
MC/DC100%2/2
@begins or @ends
@begins @ends
Truth table100%3/3
A or B
A= @beginsB= @ends
ABresult
1X1
011
000
118 1525 my ( $b, $e ) = ( '"'.BEGIN_MARKER.'"', '"'.END_MARKER.'"' );
119 1525 ▶ Skilletor::Error::Block->throw( $b.' appears '.@begins.' times' ) if @begins > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @begins > 1 8 1517 1525
120 1517 ▶ Skilletor::Error::Block->throw( $e.' appears '.@ends.' times' ) if @ends > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ends > 1 1 1516 1517
121 1516 ▶ Skilletor::Error::Block->throw( $e.' without '.$b ) unless @begins;
Branch: 1 branch
BranchTrueFalseTotal
unless @begins 4 1512 1516
122 1512 ▶ Skilletor::Error::Block->throw( $b.' without '.$e ) unless @ends;
Branch: 1 branch
BranchTrueFalseTotal
unless @ends 7 1505 1512
123 1505 my ( $begin, $end ) = ( $begins[0], $ends[0] );
124 1505 ▶ Skilletor::Error::Block->throw( $e.' before '.$b ) if $end < $begin;
Branch: 1 branch
BranchTrueFalseTotal
if $end < $begin 7 1498 1505
125 1498 return { begin => $begin, end => $end, sections => _parse_sections( @lines[ $begin + 1 .. $end - 1 ] ) };
126 }
127  
128 1317 ▶sub with_block {
Subroutine with_block: called CC=10 SCAR=23.0
Pod documented
129 1317 my ( $text, $body ) = @_;
130 1317 ▶ my @block = $body ? ( BEGIN_MARKER, @$body, END_MARKER ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$body ? : 1246 71 1317
131 1317 ▶ return $body ? join( "\n", @block )."\n" : undef unless defined $text;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text 489 828 1317
$body ? : 471 18 489
132 828 my $parsed = parse_block($text);
133 813 my @lines = _lines($text);
134 813 my $out;
135 813 ▶ if ($parsed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed) { } 768 45 813
136 768 my @before = @lines[ 0 .. $parsed->{begin} - 1 ];
137 768 my @after = @lines[ $parsed->{end} + 1 .. $#lines ];
138 768 $out = join "\n", @before, @block, @after;
139 # A block that was the last content leaves no blank lines of ours behind.
140 768 ▶ $out =~ s/$WS*\z/\n/ if !$body && !grep { js_trim($_) ne '' } @after;
Branch: 1 branch
BranchTrueFalseTotal
unless $body or grep {js_trim($_) ne "";} @after 39 729 768
Condition100%3/3
$body or grep {js_trim($_) ne "";} @after
l!l&&r!l&&!r
727239
MC/DC100%2/2
$body or grep {js_trim($_) ne "";} @after
$body grep {js_trim($_) ne "";} @after
Truth table100%3/3
A or B
A= $bodyB= grep {js_trim($_) ne "";} @after
ABresult
1X1
011
000
141 }
142 else {
143 45 ▶ return $text unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 9 36 45
144 36 ( my $base = $text ) =~ s/$WS*\z//;
145 36 ▶ $out = ( length $base ? $base."\n\n" : '' ).join( "\n", @block )."\n";
Branch: 1 branch
BranchTrueFalseTotal
length $base ? : 20 16 36
146 }
147 804 ▶ return js_trim($out) eq '' ? undef : $out;
Branch: 1 branch
BranchTrueFalseTotal
js_trim($out) eq '' ? : 35 769 804
148 }
149  
150 1218 ▶sub inspect_agents_md {
Subroutine inspect_agents_md: called CC=11 SCAR=24.4
Pod documented
151 1218 my ( $path ) = @_;
152 1218 my $bytes = encode_text($path);
153 1218 ▶ unless ( lstat $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (lstat $bytes) 485 733 1218
154 485 ▶ return { ok => 1, text => undef, parsed => undef } if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 485 0 485
155 0 return { ok => 0, reason => 'unreadable ('.node_fs_error( 0 + $!, 'lstat', $path ).')' };
156 }
157 733 ▶ return { ok => 0, reason => 'is a symlink (skilletor does not write through it)' } if -l _;
Branch: 1 branch
BranchTrueFalseTotal
if -l _ 2 731 733
158 731 ▶ return { ok => 0, reason => 'is a directory' } if -d _;
Branch: 1 branch
BranchTrueFalseTotal
if -d _ 1 730 731
159 730 ▶ return { ok => 0, reason => 'is not a regular file' } unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 730 730
160 730 my $raw;
161 my $fh;
162 730 ▶ unless ( open( $fh, '<:raw', $bytes ) && defined( $raw = do { local $/; <$fh> } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<:raw", $bytes and defined($raw = do { local $/; readline $fh })) 1 729 730
Condition66%2/3
open $fh, "<:raw", $bytes and defined($raw = do { local $/; readline $fh })
!ll&&!rl&&r
10729
MC/DC50%1/2
open $fh, "<:raw", $bytes and defined($raw = do { local $/; readline $fh })
open $fh, "<:raw", $bytes defined($raw = do { local $/; readline $fh })
Truth table66%2/3
A and B
A= open $fh, "<:raw", $bytesB= defined($raw = do { local $/; readline $fh })
ABresult
0X0
100
111
163 1 return { ok => 0, reason => 'unreadable ('.node_fs_error( 0 + $!, 'open', $path ).')' };
164 }
165 729 close $fh;
166 729 my $text = decode_utf8_lossy($raw);
167 729 my $parsed;
168 729 ▶ return { ok => 1, text => $text, parsed => $parsed } if eval { $parsed = parse_block($text); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $parsed = parse_block($text); 1 } } 726 3 729
169 3 my $err = $@;
170 3 ▶ die $err unless ref $err && $err->isa('Skilletor::Error::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err and $err->isa("Skilletor::Error::Block") 0 3 3
Condition33%1/3
ref $err and $err->isa("Skilletor::Error::Block")
!ll&&!rl&&r
003
MC/DC0%0/2
ref $err and $err->isa("Skilletor::Error::Block")
ref $err $err->isa("Skilletor::Error::Block")
Truth table33%1/3
A and B
A= ref $errB= $err->isa("Skilletor::Error::Block")
ABresult
0X0
100
111
171 3 return { ok => 0, reason => 'malformed skilletor markers: '.$err->message };
172 }
173  
174 #### Codex config.toml (read-only, line based) ###############################
175  
176 2244 ▶sub _read_config_toml {
Subroutine _read_config_toml: called CC=3 SCAR=11.0
177 2244 my ( $codex_home ) = @_;
178 2244 ▶ open( my $fh, '<:raw', encode_text("$codex_home/config.toml") ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", encode_text("$codex_home/config.toml") or return undef 1118 1126 2244
179 1118 my $raw = do { local $/; <$fh> };
180 1118 close $fh;
181 1118 ▶ return defined $raw ? decode_utf8_lossy($raw) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 1116 2 1118
182 }
183  
184 678 ▶sub project_doc_limit {
Subroutine project_doc_limit: called CC=5 SCAR=16.1
Pod documented
185 678 my ( $codex_home ) = @_;
186 678 ▶ my $text = _read_config_toml($codex_home) // return 32768;
Condition100%2/2
_read_config_toml($codex_home) // (return 32768)
l!l
180498
MC/DC100%1/1
_read_config_toml($codex_home) // (return 32768)
_read_config_toml($codex_home)
Truth table100%2/2
A
A= _read_config_toml($codex_home)
Aresult
11
00
187 180 for my $line ( _lines($text) ) {
188 194 ▶ last if $line =~ /\A$WS*\[/; # a table header: no longer top level
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A$WS*\[/ 162 32 194
189 32 ▶ return 0 + $1 if $line =~ /\A$WS*project_doc_max_bytes$WS*=$WS*([0-9]+)$WS*(?:#$DOT*)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A$WS*project_doc_max_bytes$WS*=$WS*([0-9]+)$WS*(?:#$DOT*)?\z/ 11 21 32
190 }
191 169 return 32768;
192 }
193  
194 # A hooks.state key of skilletor's SessionStart handler, in any marketplace.
195 176 ▶sub _is_session_start_key {
Subroutine _is_session_start_key: called CC=2 SCAR=6.9
196 176 my ( $key ) = @_;
197 176 ▶ return index( $key, 'skilletor@' ) == 0 && index( $key, ':session_start:' ) >= 0;
Condition100%3/3
index($key, 'skilletor@') == 0 && index($key, ':session_start:') != -1
!ll&&!rl&&r
11174
MC/DC100%2/2
index($key, 'skilletor@') == 0 && index($key, ':session_start:') != -1
index($key, 'skilletor@') == 0 index($key, ':session_start:') != -1
Truth table100%3/3
A && B
A= index($key, 'skilletor@') == 0B= index($key, ':session_start:') != -1
ABresult
0X0
100
111
198 }
199  
200 401 my $QUOTED = qr/"((?:[^"\\]|\\$DOT)*)"|'([^']*)'/;
201 401 my $TRUSTED_HASH = qr/\A$WS*trusted_hash$WS*=$WS*["']/;
202  
203 1566 ▶sub codex_hook_trusted {
Subroutine codex_hook_trusted: called CC=15 SCAR=27.1
Pod documented
204 1566 my ( $codex_home ) = @_;
205 1566 ▶ my $text = _read_config_toml($codex_home) // return 0;
Condition100%2/2
_read_config_toml($codex_home) // (return 0)
l!l
936630
MC/DC100%1/1
_read_config_toml($codex_home) // (return 0)
_read_config_toml($codex_home)
Truth table100%2/2
A
A= _read_config_toml($codex_home)
Aresult
11
00
206 936 my $table = 'other';
207 936 for my $line ( _lines($text) ) {
208 1170 ▶ if ( $line =~ /\A$WS*\[$WS*([^\[\]]*?)$WS*\]$WS*(?:#$DOT*)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A$WS*\[$WS*([^\[\]]*?)$WS*\]$WS*(?:#$DOT*)?\z/) 185 985 1170
209 185 my $name = $1;
210 185 ▶ if ( $name =~ /\Ahooks$WS*\.$WS*state$WS*\.$WS*(?:$QUOTED)\z/ ) { $table = _is_session_start_key( $1 // $2 ) ? 'hook' : 'other' }
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ /\Ahooks$WS*\.$WS*state$WS*\.$WS*(?:$QUOTED)\z/) { } 163 22 185
elsif ($name =~ /\Ahooks$WS*\.$WS*state\z/) { } 15 7 22
_is_session_start_key($1 // $2) ? : 161 2 163
Condition66%2/3
$1 // $2
l!l&&r!l&&!r
16210
MC/DC0%0/2
$1 // $2
$1 $2
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
211 15 elsif ( $name =~ /\Ahooks$WS*\.$WS*state\z/ ) { $table = 'state' }
212 7 else { $table = 'other' }
213 185 next;
214 }
215 985 ▶ return 1 if $table eq 'hook' && $line =~ $TRUSTED_HASH;
Branch: 1 branch
BranchTrueFalseTotal
if $table eq "hook" and $line =~ /$TRUSTED_HASH/ 156 829 985
Condition100%3/3
$table eq "hook" and $line =~ /$TRUSTED_HASH/
!ll&&!rl&&r
8218156
MC/DC100%2/2
$table eq "hook" and $line =~ /$TRUSTED_HASH/
$table eq "hook" $line =~ /$TRUSTED_HASH/
Truth table100%3/3
A and B
A= $table eq "hook"B= $line =~ /$TRUSTED_HASH/
ABresult
0X0
100
111
216 829 ▶ if ( $table eq 'state' && $line =~ /\A$WS*(?:$QUOTED)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table eq "state" and $line =~ /\A$WS*(?:$QUOTED)/) 13 816 829
Condition100%3/3
$table eq "state" and $line =~ /\A$WS*(?:$QUOTED)/
!ll&&!rl&&r
808813
MC/DC100%2/2
$table eq "state" and $line =~ /\A$WS*(?:$QUOTED)/
$table eq "state" $line =~ /\A$WS*(?:$QUOTED)/
Truth table100%3/3
A and B
A= $table eq "state"B= $line =~ /\A$WS*(?:$QUOTED)/
ABresult
0X0
100
111
217 13 ▶ my ( $key, $rest ) = ( $1 // $2, substr( $line, $+[0] ) );
Condition66%2/3
$1 // $2
l!l&&r!l&&!r
1210
MC/DC0%0/2
$1 // $2
$1 $2
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
218 13 ▶ return 1 if _is_session_start_key($key) && $rest =~ /\btrusted_hash$WS*=$WS*["']/a;
Branch: 1 branch
BranchTrueFalseTotal
if _is_session_start_key($key) and $rest =~ /\btrusted_hash$WS*=$WS*["']/a 9 4 13
Condition66%2/3
_is_session_start_key($key) and $rest =~ /\btrusted_hash$WS*=$WS*["']/a
!ll&&!rl&&r
049
MC/DC50%1/2
_is_session_start_key($key) and $rest =~ /\btrusted_hash$WS*=$WS*["']/a
_is_session_start_key($key) $rest =~ /\btrusted_hash$WS*=$WS*["']/a
Truth table66%2/3
A and B
A= _is_session_start_key($key)B= $rest =~ /\btrusted_hash$WS*=$WS*["']/a
ABresult
0X0
100
111
219 }
220 }
221 771 return 0;
222 }
223  
224 401 1;
225  
226 __END__
« lib/Skilletor/Catalog.pm ↑ Summary /usr/local/lib/perl5/5.44.0/CPAN/Meta/Requirements.pm »