feat(3567): state.* router delegates via executeForCjs + Phase 5.0 worker fix (Phase 5.1 of #3524)
Phase 5.1 of the CJS↔SDK hard-seam migration (parent #3524). Migrates the bin/lib/state-command-router.cjs handlers map to delegate every canonical state subcommand through the executeForCjs synchronous primitive (shipped in Phase 5.0, PR #3558). ## Bundled fix for Phase 5.0 worker defect Discovered during Phase 5.1 implementation that the Phase 5.0 worker drops projectDir and workstream from RuntimeBridgeExecuteInput. The dispatch closure at sdk/src/runtime-bridge-sync/worker.ts:41-42 hardcoded projectDir to '', so registry handlers that read .planning/ from projectDir (every state.* handler) saw an empty path and failed. Phase 5.0's pinning tests passed because they exercised commands that don't depend on projectDir (generate-slug takes its arg directly; unknown_command doesn't dispatch). Maintainer authorized bundling the fix into this PR. Fix: moved QueryNativeDirectAdapter construction inside the dispatchNative lambda so request.projectDir and request.workstream close over the per-request values. Per-request adapter construction adds <1ms overhead; correctness wins. Regression test at sdk/src/runtime-bridge-sync/projectdir-regression.test.ts demonstrates RED before fix → GREEN after. Phase 5.0's index.test.ts native_failure fixture was passing because of the bug — it relied on projectDir = '' producing a specific error path. Updated to use a /nonexistent-... path that triggers ENOENT under realpath, producing native_failure as intended. ## What landed for Phase 5.1 - bin/lib/state-command-router.cjs migrated. Every subcommand entry in the handlers map dispatches via executeForCjs when SDK is available, with transparent fallback to the existing CJS handlers in state.cjs if (a) SDK is not built / not present, or (b) GSD_WORKSTREAM is set (the sync-bridge worker cannot serve workstream-scoped commands per the SDK transport architecture). - Special cases preserved: - load --raw: SDK data formatted into key=value lines matching cmdStateLoad's exact format. - complete-phase: CJS-only (no SDK counterpart yet). - add-roadmap-evolution: stays on the unsupported list (SDK-only). - Golden parity tests added for 12 previously-uncovered state subcommands: advance-plan, record-metric, update-progress, add-decision, add-blocker, resolve-blocker, record-session, signal-waiting, signal-resume, planned-phase, milestone-switch, prune. ## Design decisions worth reviewer visibility 1. Lazy SDK loading with CJS fallback. The migration routes via executeForCjs only when the SDK is loadable; otherwise falls back to the existing CJS handlers. Conservative for rollback — if the SDK build is broken on a deploy, state commands keep working via the CJS path. Trade-off: drift surface is not structurally eliminated yet — the CJS handlers remain reachable. 2. Workstream → CJS fallback. The SDK transport forces subprocess for workstream commands, but subprocess is disabled in the sync bridge. When GSD_WORKSTREAM is set, the entire state command falls back to CJS rather than failing. Workstream users continue running the CJS handlers; the SDK path is exercised only in the default (no workstream) case. 3. Two documented parity divergences. state.record-metric: CJS auto-creates ## Performance Metrics section when absent; SDK returns {recorded: false, reason}. Test requires fixture with the section present. state.prune: CJS counts phases from disk; SDK reads from frontmatter fields. Test asserts structural shape rather than exact equality. ## Numbers - Full CJS suite: 9323/9323 pass (baseline 9323; +0 net because the 12 new parity tests are SDK-side vitest, not CJS-side). - SDK vitest sync-bridge: 10/10 pass. - Regression test: 3/3 pass (proved RED before fix, GREEN after). - tests/state.test.cjs (the safety net): 104/104 pass unchanged. ## Performance gsd-tools state load via the SDK path: 49ms first call (Worker startup), 43-44ms steady-state median. Slower than the Phase 5.0-measured 0.1ms because state.load does fs reads on top of the bridge overhead. Still well within the budget for CJS dispatcher overhead. Closes #3567.
This commit is contained in:
@@ -312,6 +312,235 @@ describe('Golden file tests', () => {
|
||||
const sdkResult = await registry.dispatch('state.sync', ['--verify'], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
// ─── Phase 5.1: 12 additional state subcommand parity tests ────────────
|
||||
|
||||
it('state.advance-plan matches gsd-tools.cjs', async () => {
|
||||
// Setup: add compound Plan field so advance-plan can parse it
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
const content = await readFile(statePath, 'utf-8');
|
||||
await writeFile(statePath, content + '\nPlan: 2 of 3\n', 'utf-8');
|
||||
const gsdOutput = await captureGsdToolsOutput('state', ['advance-plan'], tmpDir);
|
||||
// Restore and re-apply for SDK call
|
||||
await writeFile(statePath, content + '\nPlan: 2 of 3\n', 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.advance-plan', [], tmpDir);
|
||||
// Both advance plan: compare shape (times may differ slightly but structure matches)
|
||||
expect(typeof sdkResult.data).toBe('object');
|
||||
const sdkData = sdkResult.data as Record<string, unknown>;
|
||||
const gsdData = gsdOutput as Record<string, unknown>;
|
||||
expect(sdkData.advanced).toBe(gsdData.advanced);
|
||||
if (sdkData.advanced) {
|
||||
expect(typeof sdkData.current_plan).toBe('number');
|
||||
expect(typeof sdkData.previous_plan).toBe('number');
|
||||
}
|
||||
});
|
||||
|
||||
it('state.update-progress matches gsd-tools.cjs', async () => {
|
||||
// Both update the progress bar. Phase dir is empty so percent=0.
|
||||
const gsdOutput = await captureGsdToolsOutput('state', ['update-progress'], tmpDir);
|
||||
// Restore state for SDK call (CJS mutates the file)
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
await writeFile(statePath, MINIMAL_STATE, 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.update-progress', [], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
it('state.add-decision matches gsd-tools.cjs', async () => {
|
||||
// Setup: add a Decisions section to STATE.md body
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
const withDecisions = MINIMAL_STATE + '\n## Decisions\n\nNone yet.\n';
|
||||
await writeFile(statePath, withDecisions, 'utf-8');
|
||||
const argv = ['add-decision', '--phase', '10', '--summary', 'SDK parity decision'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
await writeFile(statePath, withDecisions, 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.add-decision', ['--phase', '10', '--summary', 'SDK parity decision'], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
it('state.add-blocker matches gsd-tools.cjs', async () => {
|
||||
// Setup: add a Blockers section to STATE.md body
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
const withBlockers = MINIMAL_STATE + '\n## Blockers\n\nNone\n';
|
||||
await writeFile(statePath, withBlockers, 'utf-8');
|
||||
const argv = ['add-blocker', '--text', 'SDK parity blocker'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
await writeFile(statePath, withBlockers, 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.add-blocker', ['--text', 'SDK parity blocker'], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
it('state.resolve-blocker matches gsd-tools.cjs', async () => {
|
||||
// Setup: add a Blockers section that has a blocker entry to remove
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
const withBlocker = MINIMAL_STATE + '\n## Blockers\n\n- SDK parity blocker to resolve\n';
|
||||
await writeFile(statePath, withBlocker, 'utf-8');
|
||||
const argv = ['resolve-blocker', '--text', 'SDK parity blocker to resolve'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
await writeFile(statePath, withBlocker, 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.resolve-blocker', ['--text', 'SDK parity blocker to resolve'], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
it('state.record-session matches gsd-tools.cjs', async () => {
|
||||
// Setup: add session fields to STATE.md body
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
const withSession = MINIMAL_STATE + '\nLast session: 2026-05-01T00:00:00.000Z\n';
|
||||
await writeFile(statePath, withSession, 'utf-8');
|
||||
const argv = ['record-session', '--stopped-at', 'plan 2 done'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
// SDK writes timestamp — compare shape not exact value
|
||||
const registry = createRegistry();
|
||||
await writeFile(statePath, withSession, 'utf-8');
|
||||
const sdkResult = await registry.dispatch('state.record-session', ['--stopped-at', 'plan 2 done'], tmpDir);
|
||||
const sdkData = sdkResult.data as Record<string, unknown>;
|
||||
const gsdData = gsdOutput as Record<string, unknown>;
|
||||
// Both should agree on recorded:true/false shape
|
||||
expect(sdkData.recorded).toBe(gsdData.recorded);
|
||||
if (sdkData.recorded && gsdData.recorded) {
|
||||
expect(Array.isArray(sdkData.updated)).toBe(true);
|
||||
expect(Array.isArray(gsdData.updated)).toBe(true);
|
||||
expect((sdkData.updated as string[]).sort()).toEqual((gsdData.updated as string[]).sort());
|
||||
}
|
||||
});
|
||||
|
||||
it('state.signal-waiting matches gsd-tools.cjs', async () => {
|
||||
const argv = ['signal-waiting', '--type', 'decision_point', '--question', 'Which SDK approach?', '--phase', '10'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.signal-waiting', ['--type', 'decision_point', '--question', 'Which SDK approach?', '--phase', '10'], tmpDir);
|
||||
const sdkData = sdkResult.data as Record<string, unknown>;
|
||||
const gsdData = gsdOutput as Record<string, unknown>;
|
||||
// Both write WAITING.json — compare structural fields, not timestamp or exact paths
|
||||
expect(sdkData.signaled).toBe(gsdData.signaled);
|
||||
expect(typeof sdkData.path).toBe('string');
|
||||
expect(typeof gsdData.path).toBe('string');
|
||||
});
|
||||
|
||||
it('state.signal-resume matches gsd-tools.cjs', async () => {
|
||||
// First signal so resume has something to remove
|
||||
const gsdDir2 = join(tmpdir(), `gsd-golden-state-resume-gsd-${Date.now()}`);
|
||||
const sdkDir2 = join(tmpdir(), `gsd-golden-state-resume-sdk-${Date.now()}`);
|
||||
try {
|
||||
await setupMinimalStateProject(gsdDir2);
|
||||
await setupMinimalStateProject(sdkDir2);
|
||||
// Signal in both dirs first
|
||||
await captureGsdToolsOutput('state', ['signal-waiting', '--type', 'review'], gsdDir2);
|
||||
const registry1 = createRegistry();
|
||||
await registry1.dispatch('state.signal-waiting', ['--type', 'review'], sdkDir2);
|
||||
// Now resume
|
||||
const gsdOutput = await captureGsdToolsOutput('state', ['signal-resume'], gsdDir2);
|
||||
const registry2 = createRegistry();
|
||||
const sdkResult = await registry2.dispatch('state.signal-resume', [], sdkDir2);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
} finally {
|
||||
await rm(gsdDir2, { recursive: true, force: true });
|
||||
await rm(sdkDir2, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('state.planned-phase matches gsd-tools.cjs', async () => {
|
||||
const argv = ['planned-phase', '--phase', '11', '--plans', '4'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, tmpDir);
|
||||
const statePath = join(tmpDir, '.planning', 'STATE.md');
|
||||
await writeFile(statePath, MINIMAL_STATE, 'utf-8');
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.planned-phase', ['--phase', '11', '--plans', '4'], tmpDir);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
});
|
||||
|
||||
it('state.milestone-switch matches gsd-tools.cjs', async () => {
|
||||
const gsdDir2 = join(tmpdir(), `gsd-golden-state-ms-gsd-${Date.now()}`);
|
||||
const sdkDir2 = join(tmpdir(), `gsd-golden-state-ms-sdk-${Date.now()}`);
|
||||
try {
|
||||
await setupMinimalStateProject(gsdDir2);
|
||||
await setupMinimalStateProject(sdkDir2);
|
||||
const argv = ['milestone-switch', '--milestone', 'v4.0', '--name', 'Next Milestone'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, gsdDir2);
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.milestone-switch', ['--milestone', 'v4.0', '--name', 'Next Milestone'], sdkDir2);
|
||||
// Both return {switched:true, milestone, name} — compare structural shape
|
||||
const sdkData = sdkResult.data as Record<string, unknown>;
|
||||
const gsdData = gsdOutput as Record<string, unknown>;
|
||||
expect(sdkData.switched).toBe(gsdData.switched);
|
||||
expect(sdkData.version).toBe(gsdData.version);
|
||||
expect(sdkData.name).toBe(gsdData.name);
|
||||
} finally {
|
||||
await rm(gsdDir2, { recursive: true, force: true });
|
||||
await rm(sdkDir2, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('state.prune dry-run matches gsd-tools.cjs', async () => {
|
||||
// Prune needs a parseable current_phase. Use fresh dirs with a STATE.md
|
||||
// whose frontmatter includes current_phase so both CJS and SDK agree.
|
||||
// CJS extracts current phase from disk-counted phases (result: 0 phases → "Only 0 phases..."),
|
||||
// SDK extracts from frontmatter current_phase field.
|
||||
// Use only 2 keepRecent phases, leaving phases dir empty so CJS reports "Only 0 phases"
|
||||
// and SDK also bails early (current_phase=10, cutoff=7, but no phases to scan → same reason).
|
||||
// Align via a fixture that has current_phase in frontmatter AND no phases on disk.
|
||||
const gsdDir2 = join(tmpdir(), `gsd-golden-prune-gsd-${Date.now()}`);
|
||||
const sdkDir2 = join(tmpdir(), `gsd-golden-prune-sdk-${Date.now()}`);
|
||||
// Minimal state — no phases on disk, prune returns "Only N phases — nothing to prune"
|
||||
try {
|
||||
await setupMinimalStateProject(gsdDir2);
|
||||
await setupMinimalStateProject(sdkDir2);
|
||||
const argv = ['prune', '--keep-recent', '3', '--dry-run'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, gsdDir2);
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.prune', ['--keep-recent', '3', '--dry-run'], sdkDir2);
|
||||
// Both should return pruned:false. Exact reason may differ (CJS: phase count from disk;
|
||||
// SDK: phase count from frontmatter). Compare just the structural result.
|
||||
const sdkData = sdkResult.data as Record<string, unknown>;
|
||||
const gsdData = gsdOutput as Record<string, unknown>;
|
||||
expect(sdkData.pruned).toBe(false);
|
||||
expect(gsdData.pruned).toBe(false);
|
||||
expect(typeof sdkData.reason).toBe('string');
|
||||
expect(typeof gsdData.reason).toBe('string');
|
||||
} finally {
|
||||
await rm(gsdDir2, { recursive: true, force: true });
|
||||
await rm(sdkDir2, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('state.record-metric matches gsd-tools.cjs (no-metrics-section → divergence documented)', async () => {
|
||||
// Divergence: CJS auto-creates the Performance Metrics section when absent;
|
||||
// SDK returns { recorded: false, reason: '...' }. We test both via fresh dirs
|
||||
// and add a metrics section to align behavior for parity.
|
||||
const gsdDir2 = join(tmpdir(), `gsd-golden-state-metric-gsd-${Date.now()}`);
|
||||
const sdkDir2 = join(tmpdir(), `gsd-golden-state-metric-sdk-${Date.now()}`);
|
||||
try {
|
||||
const metricsState = MINIMAL_STATE + [
|
||||
'',
|
||||
'## Performance Metrics',
|
||||
'',
|
||||
'| Phase | Plan | Duration | Notes |',
|
||||
'|-------|------|----------|-------|',
|
||||
'',
|
||||
].join('\n');
|
||||
await mkdir(join(gsdDir2, '.planning', 'phases'), { recursive: true });
|
||||
await writeFile(join(gsdDir2, '.planning', 'STATE.md'), metricsState, 'utf-8');
|
||||
await writeFile(join(gsdDir2, '.planning', 'ROADMAP.md'), '# Roadmap\n', 'utf-8');
|
||||
await writeFile(join(gsdDir2, '.planning', 'config.json'), '{"model_profile":"balanced"}', 'utf-8');
|
||||
await mkdir(join(sdkDir2, '.planning', 'phases'), { recursive: true });
|
||||
await writeFile(join(sdkDir2, '.planning', 'STATE.md'), metricsState, 'utf-8');
|
||||
await writeFile(join(sdkDir2, '.planning', 'ROADMAP.md'), '# Roadmap\n', 'utf-8');
|
||||
await writeFile(join(sdkDir2, '.planning', 'config.json'), '{"model_profile":"balanced"}', 'utf-8');
|
||||
|
||||
const argv = ['record-metric', '--phase', '10', '--plan', '1', '--duration', '45m', '--tasks', '12', '--files', '8'];
|
||||
const gsdOutput = await captureGsdToolsOutput('state', argv, gsdDir2);
|
||||
const registry = createRegistry();
|
||||
const sdkResult = await registry.dispatch('state.record-metric', ['--phase', '10', '--plan', '1', '--duration', '45m', '--tasks', '12', '--files', '8'], sdkDir2);
|
||||
expect(sdkResult.data).toEqual(gsdOutput);
|
||||
} finally {
|
||||
await rm(gsdDir2, { recursive: true, force: true });
|
||||
await rm(sdkDir2, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('phase mutations (subprocess parity)', () => {
|
||||
|
||||
Reference in New Issue
Block a user