lib/Skilletor/Convert.pm

filter: Statementstmt 99.3% 150 / 151 Branchbran 98.9% 91 / 92 Conditioncond 93.3% 56 / 60 MC/DCmcdc 87.8% 29 / 33 Subroutinesub 100% 23 / 23 Podpod 100% 3 / 3 totaltotal 97.2% 352 / 362 CC 73 SCAR 24.4
CC
73
Cov
98%
codex_agent_toml
26.0
convert_for_target
11.0
_briefing_comment
9.0
CRAP 11.4
« lib/Skilletor/Frontmatter.pm ↑ Summary lib/Skilletor/Briefing.pm »
1 package Skilletor::Convert;
2 # ABSTRACT: A Claude agent as a Codex agent role, a rule as its section of the Codex rules file
3  
4 400 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 400 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 400 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 400 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 400 ▶use Skilletor::AgentsMD qw( RULES_FILE MARKER_LINE normalize_section );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 400 ▶use Skilletor::Error::Convert;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 400 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 400 ▶use Skilletor::Frontmatter qw( split_frontmatter is_yaml_float );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 400 ▶use Skilletor::JSON qw( js_number js_ordered js_stringify js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 400 ▶use Skilletor::JSString qw( js_trim );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 400 ▶use Skilletor::TOML qw( stringify_toml toml_float );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 400 our $VERSION = '0.008000';
18  
19 400 our @EXPORT_OK = qw( codex_agent_toml codex_rule_section convert_for_target );
20  
21 93 ▶sub _fail { Skilletor::Error::Convert->throw( $_[0] ) }
Subroutine _fail: called CC=1 SCAR=0.0
22  
23 # split_frontmatter; unreadable frontmatter is a conversion error with the reader's message.
24 1437 ▶sub _split {
Subroutine _split: called CC=4 SCAR=14.6
25 1437 my ( $markdown ) = @_;
26 1437 my $fm;
27 1437 ▶ return $fm if eval { $fm = split_frontmatter($markdown); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $fm = split_frontmatter($markdown); 1 } } 1425 12 1437
28 12 my $err = $@;
29 12 ▶ _fail( $err->message ) if blessed $err && $err->isa('Skilletor::Error::Frontmatter');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa("Skilletor::Error::Frontmatter") 12 0 12
Condition33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Frontmatter")
!ll&&!rl&&r
0012
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Frontmatter")
builtin::blessed($err) $err->isa("Skilletor::Error::Frontmatter")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Frontmatter")
ABresult
0X0
100
111
30 0 die $err;
31 }
32  
33 672 ▶sub _is_mapping {
Subroutine _is_mapping: called CC=5 SCAR=16.1
34 672 my ( $v ) = @_;
35 672 ▶ return ref $v && ( reftype $v // '' ) eq 'HASH' && !is_yaml_float($v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $v && (builtin::reftype($v) // '') eq 'HASH' && !is_yaml_float($v) ? : 159 513 672
Condition87%7/8
ref $v and (builtin::reftype($v) // "") eq "HASH" and not is_yaml_float($v)
!ll&&!rl&&r
46647159
ref $v and (builtin::reftype($v) // "") eq "HASH"
!ll&&!rl&&r
43135206
builtin::reftype($v) // ""
l!l
2410
MC/DC66%2/3
ref $v and (builtin::reftype($v) // "") eq "HASH" and not is_yaml_float($v)
ref $v builtin::reftype($v) not is_yaml_float($v)
Truth table75%3/4
A and B and not(C)
A= ref $vB= builtin::reftype($v)C= not is_yaml_float($v)
ABCresult
0XX0
10X0
1100
1111
36 }
37  
38 # A YAML value as a TOML scalar or string array: ( $value ), or ( undef, $reason ) when it
39 # cannot be one.
40 122 ▶sub _toml_leaf {
Subroutine _toml_leaf: called CC=8 SCAR=20.8
41 122 my ( $v ) = @_;
42 122 ▶ return ( toml_float( $v->value ) ) if is_yaml_float($v);
Branch: 1 branch
BranchTrueFalseTotal
if is_yaml_float($v) 26 96 122
43 96 my $type = js_type($v);
44 96 ▶ return ($v) if $type eq 'string' || $type eq 'boolean' || $type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" or $type eq "boolean" or $type eq "number" 68 28 96
Condition100%6/6
$type eq "string" or $type eq "boolean" or $type eq "number"
l!l&&r!l&&!r
511728
$type eq "string" or $type eq "boolean"
l!l&&r!l&&!r
46545
MC/DC100%3/3
$type eq "string" or $type eq "boolean" or $type eq "number"
$type eq "string" $type eq "boolean" $type eq "number"
Truth table100%4/4
A or B or C
A= $type eq "string"B= $type eq "boolean"C= $type eq "number"
ABCresult
1XX1
01X1
0011
0000
45 28 ▶ return ( undef, 'null has no TOML form' ) if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 7 21 28
46 21 ▶ if ( $type eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array") 19 2 21
47 19 ▶ return ( undef, 'only arrays of strings are supported' ) if grep { js_type($_) ne 'string' } @$v;
Branch: 1 branch
BranchTrueFalseTotal
if grep {js_type($_) ne "string";} @$v 6 13 19
48 13 return ($v);
49 }
50 2 return ( undef, 'nested tables are not supported' );
51 }
52  
53 # String(v) of a scalar, undef for null, a list, a mapping or a float (all objects there).
54 431 ▶sub _as_text {
Subroutine _as_text: called CC=5 SCAR=16.1
55 431 my ( $v ) = @_;
56 431 my $type = js_type($v);
57 431 ▶ return $v ? 'true' : 'false' if $type eq 'boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "boolean" 5 426 431
$v ? : 4 1 5
58 426 ▶ return js_number($v) if $type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "number" 7 419 426
59 419 ▶ return $type eq 'string' ? $v : undef;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'string' ? : 240 179 419
60 }
61  
62 # String(v) of a YAML value that is no list.
63 27 ▶sub _js_string {
Subroutine _js_string: called CC=4 SCAR=13.9
64 27 my ( $v ) = @_;
65 27 ▶ return '[object Object]' if is_yaml_float($v) || _is_mapping($v);
Branch: 1 branch
BranchTrueFalseTotal
if is_yaml_float($v) or _is_mapping($v) 3 24 27
Condition100%3/3
is_yaml_float($v) or _is_mapping($v)
l!l&&r!l&&!r
2124
MC/DC100%2/2
is_yaml_float($v) or _is_mapping($v)
is_yaml_float($v) _is_mapping($v)
Truth table100%3/3
A or B
A= is_yaml_float($v)B= _is_mapping($v)
ABresult
1X1
011
000
66 24 ▶ return defined $v ? _as_text($v) : 'null';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 23 1 24
67 }
68  
69 # JSON.stringify(v) of a YAML scalar; a float is the object it is in the reference.
70 21 ▶sub _json {
Subroutine _json: called CC=2 SCAR=6.9
71 21 my ( $v ) = @_;
72 21 ▶ return js_stringify( is_yaml_float($v) ? js_ordered( value => $v->value ) : $v );
Branch: 1 branch
BranchTrueFalseTotal
is_yaml_float($v) ? : 2 19 21
73 }
74  
75 # A briefing skill name the comment line cannot carry: `"`, `\`, `]`, or any control character
76 # (line breaks included; TOML allows none but tab in a comment).
77 400 my $BAD_SKILL_CHAR = qr/["\\\]\x00-\x1F\x7F]/;
78  
79 # briefing.skills as the comment line the briefing plugin reads for Codex (spec ยง14.7): Codex
80 # drops a role with any unknown key, so a [briefing] table is not an option. Undef when there
81 # is nothing to declare.
82 204 ▶sub _briefing_comment {
Subroutine _briefing_comment: called CC=9 SCAR=22.0
83 204 my ( $briefing ) = @_;
84 204 ▶ return undef unless _is_mapping($briefing) && exists $briefing->{skills};
Branch: 1 branch
BranchTrueFalseTotal
unless _is_mapping($briefing) and exists $briefing->{"skills"} 169 35 204
Condition100%3/3
_is_mapping($briefing) and exists $briefing->{"skills"}
!ll&&!rl&&r
167235
MC/DC100%2/2
_is_mapping($briefing) and exists $briefing->{"skills"}
_is_mapping($briefing) exists $briefing->{"skills"}
Truth table100%3/3
A and B
A= _is_mapping($briefing)B= exists $briefing->{"skills"}
ABresult
0X0
100
111
85 35 my $skills = $briefing->{skills};
86 35 ▶ _fail('briefing.skills must be a list of skill names') unless js_type($skills) eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($skills) eq "array" 7 28 35
87 28 ▶ return undef unless @$skills;
Branch: 1 branch
BranchTrueFalseTotal
unless @$skills 1 27 28
88 27 my $i = 0;
89 27 for my $s (@$skills) {
90 46 $i++;
91 46 ▶ next if js_type($s) eq 'string' && $s ne '' && $s !~ $BAD_SKILL_CHAR;
Branch: 1 branch
BranchTrueFalseTotal
if js_type($s) eq "string" and $s ne "" and not $s =~ /$BAD_SKILL_CHAR/ 25 21 46
Condition100%6/6
js_type($s) eq "string" and $s ne "" and not $s =~ /$BAD_SKILL_CHAR/
!ll&&!rl&&r
101125
js_type($s) eq "string" and $s ne ""
!ll&&!rl&&r
8236
MC/DC100%3/3
js_type($s) eq "string" and $s ne "" and not $s =~ /$BAD_SKILL_CHAR/
js_type($s) eq "string" $s ne "" not $s =~ /$BAD_SKILL_CHAR/
Truth table100%4/4
A and B and not(C)
A= js_type($s) eq "string"B= $s ne ""C= not $s =~ /$BAD_SKILL_CHAR/
ABCresult
0XX0
10X0
1100
1111
92 21 _fail( 'briefing.skills item '.$i.' ('._json($s).') must be a non-empty string '
93 .'without a double quote, backslash, ] or line break' );
94 }
95 6 return '# briefing: skills = ['.join( ', ', map { '"'.$_.'"' } @$skills )."]\n";
96 }
97  
98 213 ▶sub codex_agent_toml {
Subroutine codex_agent_toml: called CC=26 SCAR=32.6
Pod documented
99 213 my ( $markdown, $item_name ) = @_;
100 213 my $fm = _split($markdown);
101 204 my ( $data, $body ) = @{$fm}{qw( data body )};
102 204 my @warnings;
103  
104 204 my $name = _as_text( $data->{name} );
105 my $top = js_ordered(
106 name => defined $name && $name ne '' ? $name : $item_name,
107 204 ▶ description => _as_text( $data->{description} ) // '',
Branch: 1 branch
BranchTrueFalseTotal
defined $name && $name ne '' ? : 36 168 204
Condition100%5/5
defined $name and $name ne ""
!ll&&!rl&&r
166236
_as_text($data->{"description"}) // ""
l!l
19113
MC/DC100%2/2
defined $name and $name ne ""
defined $name $name ne ""
MC/DC100%1/1
_as_text($data->{"description"}) // ""
_as_text($data->{"description"})
Truth table100%3/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
Truth table100%2/2
A
A= _as_text($data->{"description"})
Aresult
11
00
108 );
109 204 my $tables = js_ordered();
110  
111 204 my $briefing = _briefing_comment( $data->{briefing} );
112  
113 176 my $instructions = $body;
114 176 my $codex = $data->{codex};
115 176 ▶ if ( exists $data->{codex} && !_is_mapping($codex) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{'codex'} and not _is_mapping($codex)) { } 8 168 176
elsif (_is_mapping($codex)) { } 42 126 168
Condition100%3/3
exists $data->{"codex"} and not _is_mapping($codex)
!ll&&!rl&&r
126428
MC/DC100%2/2
exists $data->{"codex"} and not _is_mapping($codex)
exists $data->{"codex"} not _is_mapping($codex)
Truth table100%3/3
A and not(B)
A= exists $data->{"codex"}B= not _is_mapping($codex)
ABresult
0X0
100
111
116 8 push @warnings, 'codex: must be a mapping; ignored';
117 }
118 elsif ( _is_mapping($codex) ) {
119 42 for my $key ( keys %$codex ) {
120 117 my $v = $codex->{$key};
121 117 ▶ if ( $key eq 'briefing' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "briefing") 3 114 117
122 3 _fail( 'codex.briefing: would become a [briefing] table, and Codex ignores a role with one; '
123 .'declare skills in the top-level briefing.skills' );
124 }
125 114 ▶ if ( _is_mapping($v) && ( exists $top->{$key} || $key eq 'developer_instructions' || exists $tables->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_mapping($v) and exists $top->{$key} || $key eq "developer_instructions" || exists $tables->{$key}) 3 111 114
Condition88%8/9
_is_mapping($v) and exists $top->{$key} || $key eq "developer_instructions" || exists $tables->{$key}
!ll&&!rl&&r
94173
exists $top->{$key} or $key eq "developer_instructions" or exists $tables->{$key}
l!l&&r!l&&!r
3017
exists $top->{$key} or $key eq "developer_instructions"
l!l&&r!l&&!r
2117
MC/DC75%3/4
_is_mapping($v) and exists $top->{$key} || $key eq "developer_instructions" || exists $tables->{$key}
_is_mapping($v) exists $top->{$key} $key eq "developer_instructions" exists $tables->{$key}
Truth table80%4/5
A and B or C or D
A= _is_mapping($v)B= exists $top->{$key}C= $key eq "developer_instructions"D= exists $tables->{$key}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
126 3 push @warnings, "codex.$key: a table cannot replace the $key key; dropped";
127 3 next;
128 }
129 111 ▶ if ( _is_mapping($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_mapping($v)) 17 94 111
130 17 my $t = js_ordered();
131 17 for my $sk ( keys %$v ) {
132 28 my ( $leaf, $reason ) = _toml_leaf( $v->{$sk} );
133 28 ▶ if ( defined $reason ) { push @warnings, "codex.$key.$sk: $reason; dropped" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) { } 6 22 28
134 22 else { $t->{$sk} = $leaf }
135 }
136 17 $tables->{$key} = $t;
137 17 next;
138 }
139 94 my ( $leaf, $reason ) = _toml_leaf($v);
140 94 ▶ if ( defined $reason ) { push @warnings, "codex.$key: $reason; dropped" }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $reason) { } 9 85 94
elsif ($key eq 'developer_instructions') { } 8 77 85
141 8 elsif ( $key eq 'developer_instructions' ) { $instructions = $leaf }
142 77 else { $top->{$key} = $leaf }
143 }
144 }
145  
146 173 ▶ _fail('name must be a non-empty string') unless js_type( $top->{name} ) eq 'string' && js_trim( $top->{name} ) ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($top->{"name"}) eq "string" and js_trim($top->{"name"}) ne "" 13 160 173
Condition100%3/3
js_type($top->{"name"}) eq "string" and js_trim($top->{"name"}) ne ""
!ll&&!rl&&r
49160
MC/DC100%2/2
js_type($top->{"name"}) eq "string" and js_trim($top->{"name"}) ne ""
js_type($top->{"name"}) eq "string" js_trim($top->{"name"}) ne ""
Truth table100%3/3
A and B
A= js_type($top->{"name"}) eq "string"B= js_trim($top->{"name"}) ne ""
ABresult
0X0
100
111
147 160 ▶ unless ( js_type( $top->{description} ) eq 'string' && js_trim( $top->{description} ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (js_type($top->{"description"}) eq "string" and js_trim($top->{"description"}) ne "") 17 143 160
Condition100%3/3
js_type($top->{"description"}) eq "string" and js_trim($top->{"description"}) ne ""
!ll&&!rl&&r
215143
MC/DC100%2/2
js_type($top->{"description"}) eq "string" and js_trim($top->{"description"}) ne ""
js_type($top->{"description"}) eq "string" js_trim($top->{"description"}) ne ""
Truth table100%3/3
A and B
A= js_type($top->{"description"}) eq "string"B= js_trim($top->{"description"}) ne ""
ABresult
0X0
100
111
148 17 _fail('has no description (Codex rejects an agent role without one)');
149 }
150 143 ▶ _fail('developer_instructions must be a string') unless js_type($instructions) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($instructions) eq "string" 3 140 143
151 140 ▶ return { warnings => \@warnings } if js_trim($instructions) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if js_trim($instructions) eq "" 13 127 140
152  
153 # $top holds no tables, so its keys come out first; the briefing line sits between them and
154 # developer_instructions, never inside the multi-line string.
155 127 my $rest = js_ordered( developer_instructions => $instructions );
156 127 $rest->{$_} = $tables->{$_} for keys %$tables;
157 127 ▶ my $text = stringify_toml($top).( $briefing // '' ).stringify_toml( $rest, { multiline => ['developer_instructions'] } );
Condition100%2/2
$briefing // ""
l!l
5120
MC/DC100%1/1
$briefing // ""
$briefing
Truth table100%2/2
A
A= $briefing
Aresult
11
00
158 124 return { toml => $text, warnings => \@warnings };
159 }
160  
161 1224 ▶sub codex_rule_section {
Subroutine codex_rule_section: called CC=6 SCAR=17.9
Pod documented
162 1224 my ( $markdown, $item_name ) = @_;
163 1224 my $fm = _split($markdown);
164 1221 ▶ return undef if js_trim( $fm->{body} ) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if js_trim($fm->{"body"}) eq "" 3 1218 1221
165 1218 ▶ if ( grep { js_trim($_) =~ MARKER_LINE } split /\n/, $fm->{body}, -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {js_trim($_) =~ /\A<!-- skilletor:(?:begin|end|rules?)\b/a;} split(/\n/, $fm->{"body"}, -1)) 14 1204 1218
166 14 _fail( 'rule '.$item_name.' contains a skilletor marker line; it would break the rules file' );
167 }
168 1204 my $raw = $fm->{data}{paths};
169 1204 ▶ my @paths = map { _js_string($_) } js_type($raw) eq 'array' ? @$raw : defined $raw ? ($raw) : ();
Branch: 2 branches
BranchTrueFalseTotal
js_type($raw) eq 'array' ? : 11 1193 1204
defined $raw ? : 8 1185 1193
170 1204 ▶ my $lead = @paths ? 'Applies when working with files matching: '.join( ', ', map { "`$_`" } @paths ).".\n\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
@paths ? : 18 1186 1204
171 1204 return normalize_section( $lead.$fm->{body} );
172 }
173  
174 1186 ▶sub _file {
Subroutine _file: called CC=3 SCAR=11.0
175 1186 my ( $output, $path ) = @_;
176 1186 for my $f (@$output) {
177 1185 ▶ return $f->[1] if $f->[0] eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $f->[0] eq $path 1183 2 1185
178 }
179 3 return undef;
180 }
181  
182 5202 ▶sub convert_for_target {
Subroutine convert_for_target: called CC=11 SCAR=24.0
Pod documented
183 5202 my ( $harness, $type, $name, $output ) = @_;
184 5202 ▶ if ( $harness eq 'codex' && $type eq 'rule' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($harness eq "codex" and $type eq "rule") 1160 4042 5202
Condition100%3/3
$harness eq "codex" and $type eq "rule"
!ll&&!rl&&r
33496931160
MC/DC100%2/2
$harness eq "codex" and $type eq "rule"
$harness eq "codex" $type eq "rule"
Truth table100%3/3
A and B
A= $harness eq "codex"B= $type eq "rule"
ABresult
0X0
100
111
185 1160 my $md = _file( $output, "rules/$name.md" );
186 1160 ▶ _fail("rules/$name.md missing from the build") unless defined $md;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $md 1 1159 1160
187 1159 my $section = codex_rule_section( decode_utf8_lossy($md), $name );
188 return {
189 1157 ▶ output => [ defined $section ? [ RULES_FILE, encode_text($section) ] : () ],
Branch: 2 branches
BranchTrueFalseTotal
defined $section ? : 1156 1 1157
defined $section ? : 1156 1 1157
190 skipped => defined $section ? 0 : 1,
191 warnings => [],
192 };
193 }
194 4042 ▶ return { output => $output, skipped => 0, warnings => [] } unless $harness eq 'codex' && $type eq 'agent';
Branch: 1 branch
BranchTrueFalseTotal
unless $harness eq "codex" and $type eq "agent" 4016 26 4042
Condition100%3/3
$harness eq "codex" and $type eq "agent"
!ll&&!rl&&r
334966726
MC/DC100%2/2
$harness eq "codex" and $type eq "agent"
$harness eq "codex" $type eq "agent"
Truth table100%3/3
A and B
A= $harness eq "codex"B= $type eq "agent"
ABresult
0X0
100
111
195 26 my $md = _file( $output, "agents/$name.md" );
196 26 ▶ _fail("agents/$name.md missing from the build") unless defined $md;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $md 2 24 26
197 24 my $conv = codex_agent_toml( decode_utf8_lossy($md), $name );
198 22 my $toml = $conv->{toml};
199 return {
200 output => [ defined $toml ? [ "agents/$name.toml", encode_text($toml) ] : () ],
201 skipped => defined $toml ? 0 : 1,
202 warnings => $conv->{warnings},
203 22 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
defined $toml ? : 21 1 22
defined $toml ? : 21 1 22
204 }
205  
206 400 1;
207  
208 __END__
« lib/Skilletor/Frontmatter.pm ↑ Summary lib/Skilletor/Briefing.pm »