@@ -346,6 +346,159 @@ export function deriveFrontmatter(text, pkgs) {
346346 return { text : lines . join ( '\n' ) , changes } ;
347347}
348348
349+ // ---------------------------------------------------------------------------
350+ // Body versions — the exact versions a skill's BODY names
351+ // ---------------------------------------------------------------------------
352+ //
353+ // The frontmatter is not the only place a skill names a release. A body names
354+ // one in two shapes, and both drifted while the frontmatter gate was green:
355+ // `references/bootstrap.md`'s starter package.json pinned `18.1.0` after the
356+ // workspace moved on, and a rule page named `18.3.0` while the package was
357+ // `18.2.0`. The contract here is the card's: a body version is EQUAL to that
358+ // package's workspace version, or it sits in a sentence that reads as history
359+ // (`since`, `removed in`, `until`, …) — a past release named as a past release.
360+ //
361+ // install shape `"@objectstack/<pkg>": "<x.y.z>"` (a `^`/`~` range spelled
362+ // the same way). Always judged, history wording or not: it is
363+ // what an author copies into a package.json.
364+ // prose shape `@objectstack/<pkg> <x.y.z>` or `@objectstack/<pkg>@<x.y.z>`.
365+ //
366+ // Frontmatter is skipped (the two derived lines are judged above), and so is
367+ // nothing else: a starter package.json lives in a fenced block on purpose.
368+ //
369+ // `deriveBodyVersions` is the pure derivation of every non-history body
370+ // version, kept apart from `deriveFrontmatter` because the token ratchet asks
371+ // that function which ONE line of a SKILL.md is the release stamp.
372+
373+ const BODY_INSTALL_PIN_RE = / " @ o b j e c t s t a c k \/ ( [ a - z 0 - 9 ] [ a - z 0 - 9 - ] * ) " ( \s * : \s * " ) ( [ ~ ^ ] ? ) ( \d + \. \d + \. \d + (?: - [ 0 - 9 A - Z a - z - ] + (?: \. [ 0 - 9 A - Z a - z - ] + ) * ) ? ) " / g;
374+ const BODY_PROSE_VERSION_RE = / @ o b j e c t s t a c k \/ ( [ a - z 0 - 9 ] [ a - z 0 - 9 - ] * ) ( [ @ ] v ? ) ( \d + \. \d + \. \d + (?: - [ 0 - 9 A - Z a - z - ] + (?: \. [ 0 - 9 A - Z a - z - ] + ) * ) ? ) / g;
375+
376+ /** Words that make a sentence name a past release rather than the current one. */
377+ const HISTORY_RE = / \b (?: s i n c e | u n t i l | b e f o r e | a s o f | (?: r e m o v e d | r e t i r e d | a d d e d | i n t r o d u c e d | d e p r e c a t e d | r e n a m e d | c h a n g e d | s h i p p e d | l a n d e d ) i n ) \b / i;
378+
379+ /** The frontmatter's line count (0 when there is none), so body scans start below it. */
380+ function frontmatterEnd ( lines ) {
381+ if ( lines [ 0 ] ?. trim ( ) !== '---' ) return 0 ;
382+ const end = lines . findIndex ( ( l , i ) => i > 0 && l . trim ( ) === '---' ) ;
383+ return end === - 1 ? 0 : end + 1 ;
384+ }
385+
386+ /**
387+ * Every body version in one file, each with its line, shape, the version
388+ * named, and whether its sentence reads as history.
389+ *
390+ * @returns {Array<{line: number, shape: 'install' | 'prose', pkg: string, version: string, history: boolean}> }
391+ */
392+ export function extractBodyVersions ( text ) {
393+ const lines = text . split ( '\n' ) ;
394+ const out = [ ] ;
395+ const start = frontmatterEnd ( lines ) ;
396+ // The sentence a match sits in, across line breaks (prose is wrapped): the
397+ // paragraph (its run of non-blank lines) joined with spaces, cut back to the
398+ // previous sentence end and forward to the next.
399+ const sentenceAt = ( i , col ) => {
400+ let top = i ;
401+ while ( top - 1 >= start && lines [ top - 1 ] . trim ( ) !== '' ) top -= 1 ;
402+ let bottom = i ;
403+ while ( bottom + 1 < lines . length && lines [ bottom + 1 ] . trim ( ) !== '' ) bottom += 1 ;
404+ const para = lines . slice ( top , bottom + 1 ) . join ( ' ' ) ;
405+ const at = lines . slice ( top , i ) . reduce ( ( n , l ) => n + l . length + 1 , 0 ) + col ;
406+ const head = para . slice ( 0 , at ) . split ( / [ . ! ? ] \s + / ) . pop ( ) ;
407+ const tail = para . slice ( at ) . split ( / [ . ! ? ] (?: \s | $ ) / ) [ 0 ] ;
408+ return `${ head } ${ tail } ` ;
409+ } ;
410+ for ( let i = start ; i < lines . length ; i += 1 ) {
411+ for ( const m of lines [ i ] . matchAll ( BODY_INSTALL_PIN_RE ) ) {
412+ out . push ( { line : i + 1 , shape : 'install' , pkg : `@objectstack/${ m [ 1 ] } ` , version : m [ 4 ] , history : false } ) ;
413+ }
414+ for ( const m of lines [ i ] . matchAll ( BODY_PROSE_VERSION_RE ) ) {
415+ out . push ( { line : i + 1 , shape : 'prose' , pkg : `@objectstack/${ m [ 1 ] } ` , version : m [ 3 ] , history : HISTORY_RE . test ( sentenceAt ( i , m . index ) ) } ) ;
416+ }
417+ }
418+ return out ;
419+ }
420+
421+ /**
422+ * The body half of the check, over every skill markdown file.
423+ *
424+ * @param files [{ file, text }] every `*.md` under skills/
425+ * @param pkgs Map<name, {version, file}>
426+ * @returns {{ problems: string[], count: number } }
427+ */
428+ export function judgeBodyVersions ( files , pkgs ) {
429+ const problems = [ ] ;
430+ let count = 0 ;
431+ for ( const { file, text } of files ) {
432+ for ( const v of extractBodyVersions ( text ) ) {
433+ count += 1 ;
434+ const actual = pkgs . get ( v . pkg ) ;
435+ if ( ! actual ) {
436+ problems . push ( `${ file } :${ v . line } \n names ${ v . pkg } ${ v . version } , but no workspace package is named ${ v . pkg } \n` +
437+ ' fix: correct the package name, or drop the version.' ) ;
438+ continue ;
439+ }
440+ if ( v . version === actual . version || ( v . shape === 'prose' && v . history ) ) continue ;
441+ problems . push (
442+ `${ file } :${ v . line } \n` +
443+ ` names: ${ v . pkg } ${ v . version } (${ v . shape === 'install' ? 'an install pin an author copies' : 'prose that does not read as history' } )\n` +
444+ ` actual: ${ v . pkg } ${ actual . version } (${ actual . file } )\n` +
445+ ( v . shape === 'install'
446+ ? ` fix: run \`${ FIX_COMMAND } \` — it rewrites the pin to "${ actual . version } "; the version is derived, never typed.`
447+ : ` fix: run \`${ FIX_COMMAND } \` to name the current version, or say which past release it was ` +
448+ '("since", "removed in", "until", …) if that is what the sentence means.' ) ,
449+ ) ;
450+ }
451+ }
452+ return { problems, count } ;
453+ }
454+
455+ /**
456+ * The `--fix` derivation for a body, pure: every body version that is not a
457+ * history sentence becomes its package's workspace version (an install pin
458+ * keeps its `^`/`~`). A version naming a package the workspace lacks is left
459+ * for the check to refuse.
460+ *
461+ * @returns {{ text: string, changes: string[] } }
462+ */
463+ export function deriveBodyVersions ( text , pkgs ) {
464+ const lines = text . split ( '\n' ) ;
465+ const changes = [ ] ;
466+ const history = new Set ( extractBodyVersions ( text ) . filter ( ( v ) => v . history ) . map ( ( v ) => `${ v . line } :${ v . pkg } :${ v . version } ` ) ) ;
467+ for ( let i = frontmatterEnd ( lines ) ; i < lines . length ; i += 1 ) {
468+ const to = ( pkg , version ) => {
469+ const actual = pkgs . get ( `@objectstack/${ pkg } ` ) ;
470+ if ( ! actual || actual . version === version || history . has ( `${ i + 1 } :@objectstack/${ pkg } :${ version } ` ) ) return null ;
471+ changes . push ( `line ${ i + 1 } : @objectstack/${ pkg } ${ version } → ${ actual . version } ` ) ;
472+ return actual . version ;
473+ } ;
474+ lines [ i ] = lines [ i ]
475+ . replace ( BODY_INSTALL_PIN_RE , ( whole , pkg , sep , range , version ) => {
476+ const next = to ( pkg , version ) ;
477+ return next === null ? whole : `"@objectstack/${ pkg } "${ sep } ${ range } ${ next } "` ;
478+ } )
479+ . replace ( BODY_PROSE_VERSION_RE , ( whole , pkg , sep , version ) => {
480+ const next = to ( pkg , version ) ;
481+ return next === null ? whole : `@objectstack/${ pkg } ${ sep } ${ next } ` ;
482+ } ) ;
483+ }
484+ return { text : lines . join ( '\n' ) , changes } ;
485+ }
486+
487+ /** Every `*.md` under skills/, as { file, text }, sorted by path. */
488+ export function readSkillMarkdown ( root = REPO_ROOT ) {
489+ const out = [ ] ;
490+ const walk = ( rel ) => {
491+ for ( const e of readdirSync ( join ( root , rel ) , { withFileTypes : true } ) ) {
492+ if ( e . name === 'node_modules' || e . name . startsWith ( '.' ) ) continue ;
493+ const child = `${ rel } /${ e . name } ` ;
494+ if ( e . isDirectory ( ) ) walk ( child ) ;
495+ else if ( e . isFile ( ) && e . name . endsWith ( '.md' ) ) out . push ( child ) ;
496+ }
497+ } ;
498+ if ( existsSync ( join ( root , SKILLS_DIR ) ) ) walk ( SKILLS_DIR ) ;
499+ return out . sort ( ) . map ( ( file ) => ( { file, text : readFileSync ( join ( root , file ) , 'utf8' ) } ) ) ;
500+ }
501+
349502// ---------------------------------------------------------------------------
350503// Checks (pure — the self-test drives these with in-memory inputs)
351504// ---------------------------------------------------------------------------
@@ -754,6 +907,16 @@ const SELF_TEST_BATTERIES = Object.freeze({
754907 'stampedSkillPaths() equals the set --fix writes on an all-stale fixture, in walk order' : 1 ,
755908 'a SKILL.md write outside stampedSkillPaths() stays unexpected under the guard\'s grep -vxF -f filter' : 1 ,
756909 'stampedSkillPaths() THROWS on a tree with no skills, never an empty list' : 1 ,
910+ 'body: an install pin at the workspace version → GREEN; one behind → RED naming file:line, both versions and the fix' : 1 ,
911+ 'body: a ^/~ range pin is judged by its version and keeps its range under --fix' : 1 ,
912+ 'body: prose at the current version → GREEN; a past version outside a history sentence → RED' : 1 ,
913+ 'body: a past version in a history sentence (removed in / since / until), wrapped across lines → GREEN' : 1 ,
914+ 'body: history wording one sentence away does not excuse a version' : 1 ,
915+ 'body: an install pin is judged even when its sentence reads as history' : 1 ,
916+ 'body: a body version naming a package the workspace lacks → RED' : 1 ,
917+ 'body: frontmatter pins and the stamp are not body versions' : 1 ,
918+ 'body: deriveBodyVersions rewrites every non-history version and nothing else; a second pass is a no-op' : 1 ,
919+ 'body: the live catalog carries install pins to judge (lit control), every one naming a workspace package' : 1 ,
757920} ) ;
758921
759922// DELETING an entry silences that battery's floor exactly as effectively as
@@ -762,7 +925,106 @@ const SELF_TEST_BATTERIES = Object.freeze({
762925// the literal above, so the roster falls below this number; the table
763926// cross-check in the floor block is the other half, and names WHICH label
764927// collided.
765- const SELF_TEST_BATTERY_FLOOR = 30 ;
928+ const SELF_TEST_BATTERY_FLOOR = 40 ;
929+
930+ // The body-version rows of the self-test's derivation table, each a battery of
931+ // its own (see SELF_TEST_BATTERIES). A row throws to fail.
932+ function bodyVersionCases ( PKGS ) {
933+ const V = PKGS . get ( '@objectstack/spec' ) . version ;
934+ const md = ( body ) => `---\nname: x\ncompatibility: Requires @objectstack/spec 17.x\nmetadata:\n version: "${ V } "\n---\n\n${ body } \n` ;
935+ const pkgJson = ( v , range = '' ) => md ( `\`\`\`json\n{ "dependencies": {\n "@objectstack/spec": "${ range } ${ v } ",\n "@objectstack/core": "${ range } ${ v } "\n} }\n\`\`\`` ) ;
936+ const judgeOne = ( text ) => judgeBodyVersions ( [ { file : 'skills/a/references/bootstrap.md' , text } ] , PKGS ) ;
937+ const need = ( cond , msg ) => { if ( ! cond ) throw new Error ( msg ) ; } ;
938+ return [
939+ {
940+ label : 'body: an install pin at the workspace version → GREEN; one behind → RED naming file:line, both versions and the fix' ,
941+ run : ( ) => {
942+ need ( judgeOne ( pkgJson ( V ) ) . problems . length === 0 , 'a current pin was refused' ) ;
943+ const r = judgeOne ( pkgJson ( '17.0.0-rc.4' ) ) ;
944+ need ( r . problems . length === 2 , `want 2 problems, got ${ r . problems . length } ` ) ;
945+ need ( r . problems [ 0 ] . startsWith ( 'skills/a/references/bootstrap.md:10' ) && r . problems [ 0 ] . includes ( '17.0.0-rc.4' )
946+ && r . problems [ 0 ] . includes ( V ) && r . problems [ 0 ] . includes ( FIX_COMMAND ) , r . problems [ 0 ] ) ;
947+ } ,
948+ } ,
949+ {
950+ label : 'body: a ^/~ range pin is judged by its version and keeps its range under --fix' ,
951+ run : ( ) => {
952+ need ( judgeOne ( pkgJson ( V , '^' ) ) . problems . length === 0 , 'a current ^ pin was refused' ) ;
953+ need ( judgeOne ( pkgJson ( '16.0.0' , '~' ) ) . problems . length === 2 , 'a stale ~ pin was not refused' ) ;
954+ const d = deriveBodyVersions ( pkgJson ( '16.0.0' , '~' ) , PKGS ) ;
955+ need ( d . text === pkgJson ( V , '~' ) && d . changes . length === 2 , d . text ) ;
956+ } ,
957+ } ,
958+ {
959+ label : 'body: prose at the current version → GREEN; a past version outside a history sentence → RED' ,
960+ run : ( ) => {
961+ need ( judgeOne ( md ( `Install @objectstack/spec ${ V } and go.` ) ) . problems . length === 0 , 'current prose refused' ) ;
962+ const r = judgeOne ( md ( 'Install @objectstack/spec@16.2.0 and go.' ) ) ;
963+ need ( r . problems . length === 1 && r . problems [ 0 ] . includes ( 'does not read as history' ) , JSON . stringify ( r . problems ) ) ;
964+ } ,
965+ } ,
966+ {
967+ label : 'body: a past version in a history sentence (removed in / since / until), wrapped across lines → GREEN' ,
968+ run : ( ) => {
969+ const text = md ( '`split` and `popover` were\nremoved in @objectstack/spec 16.2.0 (ADR-0087)\nand are refused. Present since @objectstack/spec 15.0.0. Accepted until @objectstack/core 16.0.0.' ) ;
970+ const r = judgeOne ( text ) ;
971+ need ( r . problems . length === 0 && r . count === 3 , JSON . stringify ( r ) ) ;
972+ need ( deriveBodyVersions ( text , PKGS ) . changes . length === 0 , '--fix rewrote a history sentence' ) ;
973+ } ,
974+ } ,
975+ {
976+ label : 'body: history wording one sentence away does not excuse a version' ,
977+ run : ( ) => {
978+ const r = judgeOne ( md ( 'Removed in 16.0.0, sadly. Install @objectstack/spec 16.2.0 now.' ) ) ;
979+ need ( r . problems . length === 1 , JSON . stringify ( r . problems ) ) ;
980+ } ,
981+ } ,
982+ {
983+ label : 'body: an install pin is judged even when its sentence reads as history' ,
984+ run : ( ) => {
985+ const r = judgeOne ( md ( 'Since the move: "@objectstack/spec": "16.0.0"' ) ) ;
986+ need ( r . problems . length === 1 && r . problems [ 0 ] . includes ( 'an install pin' ) , JSON . stringify ( r . problems ) ) ;
987+ } ,
988+ } ,
989+ {
990+ label : 'body: a body version naming a package the workspace lacks → RED' ,
991+ run : ( ) => {
992+ const r = judgeOne ( md ( '"@objectstack/nope": "1.0.0"' ) ) ;
993+ need ( r . problems . length === 1 && r . problems [ 0 ] . includes ( 'no workspace package is named @objectstack/nope' ) , JSON . stringify ( r . problems ) ) ;
994+ } ,
995+ } ,
996+ {
997+ label : 'body: frontmatter pins and the stamp are not body versions' ,
998+ run : ( ) => {
999+ const text = `---\nname: x\ncompatibility: Requires @objectstack/spec 9.x and @objectstack/spec 9.0.0\nmetadata:\n version: "9.0.0"\n---\n\nBody.\n` ;
1000+ need ( extractBodyVersions ( text ) . length === 0 , 'the frontmatter was read as body' ) ;
1001+ need ( extractBodyVersions ( md ( 'Requires @objectstack/spec 17.x here.' ) ) . length === 0 , 'an N.x pin was read as an exact version' ) ;
1002+ } ,
1003+ } ,
1004+ {
1005+ label : 'body: deriveBodyVersions rewrites every non-history version and nothing else; a second pass is a no-op' ,
1006+ run : ( ) => {
1007+ const stale = md ( 'Install @objectstack/spec 16.2.0. Removed in @objectstack/spec 16.0.0.\n\n```json\n"@objectstack/core": "16.2.0"\n```' ) ;
1008+ const want = md ( `Install @objectstack/spec ${ V } . Removed in @objectstack/spec 16.0.0.\n\n\`\`\`json\n"@objectstack/core": "${ V } "\n\`\`\`` ) ;
1009+ const once = deriveBodyVersions ( stale , PKGS ) ;
1010+ need ( once . text === want && once . changes . length === 2 , once . text ) ;
1011+ const twice = deriveBodyVersions ( once . text , PKGS ) ;
1012+ need ( twice . text === once . text && twice . changes . length === 0 , 'a second pass moved bytes' ) ;
1013+ need ( deriveFrontmatter ( once . text , PKGS ) . changes . length === 0 , 'the body derivation touched the frontmatter' ) ;
1014+ } ,
1015+ } ,
1016+ {
1017+ label : 'body: the live catalog carries install pins to judge (lit control), every one naming a workspace package' ,
1018+ run : ( ) => {
1019+ const pkgs = readWorkspacePackages ( ) ;
1020+ const found = readSkillMarkdown ( ) . flatMap ( ( f ) => extractBodyVersions ( f . text ) ) ;
1021+ const install = found . filter ( ( v ) => v . shape === 'install' ) ;
1022+ need ( install . length >= 1 , `the live catalog yields ${ install . length } install pin(s); the extractor stopped matching` ) ;
1023+ need ( found . every ( ( v ) => pkgs . has ( v . pkg ) ) , 'a live body version names a package the workspace lacks' ) ;
1024+ } ,
1025+ } ,
1026+ ] ;
1027+ }
7661028
7671029function selfTest ( ) {
7681030 console . log ( 'check-skill-compatibility-version self-test\n' ) ;
@@ -1133,6 +1395,7 @@ function selfTest() {
11331395 }
11341396 } ,
11351397 } ,
1398+ ...bodyVersionCases ( PKGS ) ,
11361399 ] ;
11371400 for ( const c of derivationCases ) {
11381401 registerCase ( c . label ) ;
0 commit comments