Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
// Shared helpers for the skill-script tests (scripts/test-*script*.mjs).
//
// The tests run the scripts the way an agent does: as `node <skill>/scripts/x.mjs`
// from the built skills/ tree, against throwaway fixtures in the OS temp dir,
// with the clock pinned by PLAN2CODE_NOW. They live in scripts/ so nothing
// here ships.
import fs from "node:fs" ;
import os from "node:os" ;
import path from "node:path" ;
import { spawnSync , execFileSync } from "node:child_process" ;
import { fileURLToPath } from "node:url" ;
export const ROOT = path . dirname ( path . dirname ( fileURLToPath ( import . meta . url )));
export const SKILLS = path . join ( ROOT , "skills" );
export const NOW = "2026-08-03 14:05" ;
2026-10-11 16:39:25 -07:00
export { copyFixture , slashed , fixtureCleanup } from "./fixture-helpers.mjs" ;
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
// Rebuild skills/ on every import, not only when it is missing: a test file run
// directly after an edit to src/ must not test a stale tree. The build is
2026-10-11 16:39:25 -07:00
// idempotent and takes over a second, once per test file, so `npm test`, whose
// script has just built the tree before `node --test`, skips it: npm sets
// npm_lifecycle_event for every script it runs.
if ( process . env . npm_lifecycle_event !== "test" ) {
execFileSync ( process . execPath , [ path . join ( ROOT , "install.js" ), "--build-skills" ], { stdio : "ignore" });
}
// Env a spawned script or printed git command must not inherit: relocation
// vars (GIT_DIR and friends) would redirect every fixture command, a
// developer's global git config can perturb printed commands, and stray
// SSH_*/PLAN2CODE_* vars change what the scripts report.
const AMBIENT_GIT_ENV = [
"GIT_DIR" ,
"GIT_WORK_TREE" ,
"GIT_INDEX_FILE" ,
"GIT_COMMON_DIR" ,
"GIT_OBJECT_DIRECTORY" ,
"GIT_CONFIG" ,
"GIT_CONFIG_GLOBAL" ,
"GIT_CONFIG_SYSTEM" ,
"GIT_CONFIG_NOSYSTEM" ,
"SSH_CONNECTION" ,
"PLAN2CODE_PICKER_ECHO" ,
];
// GIT_CONFIG_GLOBAL wants a real path: os.devNull is `\\.\nul` here, which git
// cannot open. An empty file is the portable "no global config".
const EMPTY_GITCONFIG = path . join ( os . tmpdir (), "p2c-empty-gitconfig" );
try {
fs . writeFileSync ( EMPTY_GITCONFIG , "" );
} catch {}
export function scrubbedEnv ( extra = {}) {
const e = { ... process . env };
for ( const k of AMBIENT_GIT_ENV ) delete e [ k ];
return { ... e , GIT_CONFIG_NOSYSTEM : "1" , GIT_CONFIG_GLOBAL : EMPTY_GITCONFIG , ... extra };
}
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
2026-10-14 20:30:35 -07:00
// Every git in a test process that imports this file runs under that same
// scrubbed config: fixture commands, the modules a test imports and the
// scripts it spawns. Mixed configs disagree: a global core.autocrlf=true once
// checked fixtures out with CRLF that the scrubbed scripts read as changed.
for ( const k of AMBIENT_GIT_ENV ) delete process . env [ k ];
Object . assign ( process . env , { GIT_CONFIG_NOSYSTEM : "1" , GIT_CONFIG_GLOBAL : EMPTY_GITCONFIG });
2026-11-13 23:29:12 -08:00
// Learning's store on this machine (~/.plan2code/learning): a script run from a
// test never reaches the real one. A test that checks the store passes its own
// PLAN2CODE_LEARNING_DIR in `env`; every other run shares this throwaway one.
let sandboxLearningDir ;
const learningDirFor = ( env ) => {
if ( env && env . PLAN2CODE_LEARNING_DIR ) return env . PLAN2CODE_LEARNING_DIR ;
sandboxLearningDir ||= fs . mkdtempSync ( path . join ( fs . realpathSync . native ( os . tmpdir ()), "p2c-learning-" ));
return sandboxLearningDir ;
};
2026-11-03 21:26:10 -08:00
export function runScript ( skill , script , args , { cwd , env , input } = {}) {
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
const r = spawnSync ( process . execPath , [ path . join ( SKILLS , skill , "scripts" , script ), ... args ], {
cwd : cwd || ROOT ,
encoding : "utf8" ,
2026-11-03 21:26:10 -08:00
input ,
2026-11-13 23:29:12 -08:00
env : scrubbedEnv ({ PLAN2CODE_NOW : NOW , ... env , PLAN2CODE_LEARNING_DIR : learningDirFor ( env ) }),
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
});
let json = null ;
try {
json = JSON . parse ( r . stdout );
} catch {}
return { code : r . status , json , stdout : r . stdout , stderr : r . stderr };
}
export function tmp ( name ) {
return fs . mkdtempSync ( path . join ( fs . realpathSync . native ( os . tmpdir ()), `p2c- ${ name } -` ));
}
export function write ( root , rel , content ) {
const p = path . join ( root , rel );
fs . mkdirSync ( path . dirname ( p ), { recursive : true });
fs . writeFileSync ( p , content );
return p ;
}
export const read = ( root , rel ) => fs . readFileSync ( path . join ( root , rel ), "utf8" );
2026-10-14 20:30:35 -07:00
/** A git runner for `dir`, under the scrubbed config set beside `scrubbedEnv`. */
export const gitIn = ( dir ) => (... a ) => execFileSync ( "git" , a , { cwd : dir , stdio : "pipe" , encoding : "utf8" });
2026-10-11 16:39:25 -07:00
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
export function gitInit ( dir , branch = "main" ) {
2026-10-11 16:39:25 -07:00
const g = gitIn ( dir );
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
g ( "init" , "-q" , "-b" , branch );
g ( "config" , "user.email" , "t@example.com" );
g ( "config" , "user.name" , "Test" );
g ( "config" , "commit.gpgsign" , "false" );
2026-10-26 20:46:03 -07:00
// No background maintenance after a commit: on macOS it held
// .git/objects/maintenance.lock while copyFixture copied the template, and
// the copy failed with ENOENT once the lock was gone.
g ( "config" , "maintenance.auto" , "false" );
g ( "config" , "gc.auto" , "0" );
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
return g ;
}
/** A phase-N.md in the Documentation template's shape. tasks: [[marker, id], ...] */
export function phase ( n , status , tasks , extra = "" ) {
return `# Phase ${ n } : Name ${ n } \n\n**Status:** ${ status } \n**Estimated Tasks:** ${ tasks . length } \n\n## Overview\n\nPhase ${ n } builds the thing. More words here.\n\n## Prerequisites\n\n- Something exists\n\n## Tasks\n\n ${ tasks . map (([ m , id ]) => `- [ ${ m } ] **Task ${ id } :** Do ${ id } ` ). join ( "\n" ) } \n ${ extra } \n## Acceptance Criteria\n\n- It works\n` ;
}