feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A) (#2341)

* feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A)

Golden/read-only parity tests and registry alignment, query handler fixes
(check-completion, state-mutation, commit, validate, summary, etc.), and
WAITING.json dual-write for .gsd/.planning readers.

Refs gsd-build/get-shit-done#2341

* fix(sdk): getMilestoneInfo matches GSD ROADMAP (🟡, last bold, STATE fallback)

- Recognize in-flight 🟡 milestone bullets like 🚧.
- Derive from last **vX.Y Title** before ## Phases when emoji absent.
- Fall back to STATE.md milestone when ROADMAP is missing; use last bare vX.Y
  in cleaned text instead of first (avoids v1.0 from shipped list).
- Fixes init.execute-phase milestone_version and buildStateFrontmatter after
  state.begin-phase (syncStateFrontmatter).

* feat(sdk): phase list, plan task structure, requirements extract handlers

- Register phase.list-plans, phase.list-artifacts, plan.task-structure,
  requirements.extract-from-plans (SDK-only; golden-policy exceptions).
- Add unit tests; document in QUERY-HANDLERS.md.
- writeProfile: honor --output, render dimensions, return profile_path and dimensions_scored.

* feat(sdk): centralize getGsdAgentsDir in query helpers

Extract agent directory resolution to helpers (GSD_AGENTS_DIR, primary
~/.claude/agents, legacy path). Use from init and docs-init init bundles.

docs(15): add 15-CONTEXT for autonomous phase-15 run.

* feat(sdk): query CLI CJS fallback and session correlation

- createRegistry(eventStream, sessionId) threads correlation into mutation events
- gsd-sdk query falls back to gsd-tools.cjs when no native handler matches
  (disable with GSD_QUERY_FALLBACK=off); stderr bridge warnings
- Export createRegistry from @gsd-build/sdk; add sdk/README.md
- Update QUERY-HANDLERS.md and registry module docs for fallback + sessionId
- Agents: prefer node dist/cli.js query over cat/grep for STATE and plans

* fix(sdk): init phase_found parity, docs-init agents path, state field extract

- Normalize findPhase not-found to null before roadmap fallback (matches findPhaseInternal)

- docs-init: use detectRuntime + resolveAgentsDir for checkAgentsInstalled

- state.cjs stateExtractField: horizontal whitespace only after colon (YAML progress guard)

- Tests: commit_docs default true; config-get golden uses temp config; golden integration green

Refs: #2302

* refactor(sdk): share SessionJsonlRecord in profile-extract-messages

CodeRabbit nit: dedupe JSONL record shape for isGenuineUserMessage and streamExtractMessages.

* fix(sdk): address CodeRabbit major threads (paths, gates, audit, verify)

- Resolve @file: and CLI JSON indirection relative to projectDir; guard empty normalized query command

- plan.task-structure + intel extract/patch-meta: resolvePathUnderProject containment

- check.config-gates: safe string booleans; plan_checker alias precedence over plan_check default

- state.validate/sync: phaseTokenMatches + comparePhaseNum ordering

- verify.schema-drift: token match phase dirs; files_modified from parsed frontmatter

- audit-open: has_scan_errors, unreadable rows, human report when scans fail

- requirements PLANNED key PLAN for root PLAN.md; gsd-tools timeout note

- ingest-docs: repo-root path containment; classifier output slug-hash

Golden parity test strips has_scan_errors until CJS adds field.

* fix: Resolve CodeRabbit security and quality findings
- Secure intel.ts and cli.ts against path traversal
- Catch and validate git add status in commit.ts
- Expand roadmap milestone marker extraction
- Fix parsing array-of-objects in frontmatter YAML
- Fix unhandled config evaluations
- Improve coverage test parity mapping

* test: raise planner character extraction limit to 48K

* fix(sdk): resolve TS build error in docs-init passing config
This commit is contained in:
Rezolv
2026-04-20 18:09:02 -04:00
committed by GitHub
parent c8807e38d7
commit c5b1445529
109 changed files with 11588 additions and 1030 deletions

View File

@@ -0,0 +1,345 @@
/**
* Next slash-command suggestion for `/gsd-next`-style routing (`route.next-action`).
*
* Deterministic routing from STATE.md, ROADMAP, and phase directories.
* See `.planning/research/decision-routing-audit.md` §3.1 and `get-shit-done/workflows/next.md`.
*/
import { readFile, readdir } from 'node:fs/promises';
import { readFileSync, existsSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { planningPaths, normalizePhaseName, comparePhaseNum } from './helpers.js';
import { stateJson } from './state.js';
import { roadmapAnalyze } from './roadmap.js';
import { findPhase } from './phase.js';
import type { QueryHandler } from './utils.js';
function readConsecutiveCallCount(planningDir: string): number {
try {
const raw = readFileSync(join(planningDir, '.next-call-count'), 'utf-8');
return parseInt(raw.trim(), 10) || 0;
} catch {
return 0;
}
}
/** Unresolved FAIL rows in phase VERIFICATION.md (lightweight gate). */
async function hasUnresolvedVerificationFails(phaseDirAbs: string): Promise<boolean> {
try {
const files = await readdir(phaseDirAbs);
const vf = files.find(f => f === 'VERIFICATION.md' || f.endsWith('-VERIFICATION.md'));
if (!vf) return false;
const content = await readFile(join(phaseDirAbs, vf), 'utf-8');
const lines = content.split('\n');
for (const line of lines) {
if (/\|\s*FAIL\s*\|/i.test(line) && !/override/i.test(line)) return true;
}
return false;
} catch {
return false;
}
}
async function verificationPassed(phaseDirAbs: string): Promise<boolean> {
try {
const files = await readdir(phaseDirAbs);
const vf = files.find(f => f === 'VERIFICATION.md' || f.endsWith('-VERIFICATION.md'));
if (!vf) return false;
const content = await readFile(join(phaseDirAbs, vf), 'utf-8');
return /status:\s*passed/i.test(content);
} catch {
return false;
}
}
export const routeNextAction: QueryHandler = async (_args, projectDir) => {
const planning = planningPaths(projectDir).planning;
const continueHere = existsSync(join(planning, '.continue-here.md'));
const sj = await stateJson([], projectDir);
const sjd = sj.data as Record<string, unknown>;
if (sjd.error) {
return {
data: {
command: '/gsd-new-project',
args: '',
reason: 'No STATE.md — initialize a GSD project first',
current_phase: null,
phase_name: null,
gates: {
continue_here: continueHere,
error_state: false,
unresolved_verification: false,
consecutive_calls: 0,
},
context: {},
},
};
}
const status = String(sjd.status ?? '');
const errorState = /\b(error|failed)\b/i.test(status);
const pausedAt = sjd.paused_at ? String(sjd.paused_at) : null;
let currentPhase = sjd.current_phase ? String(sjd.current_phase) : null;
const phaseName = sjd.current_phase_name ? String(sjd.current_phase_name) : null;
const consecutiveCalls = readConsecutiveCallCount(planning);
const ra = await roadmapAnalyze([], projectDir);
const raData = ra.data as { phases?: Array<Record<string, unknown>> };
const phases = raData.phases ?? [];
const phasesDir = planningPaths(projectDir).phases;
let dirCount = 0;
try {
dirCount = readdirSync(phasesDir, { withFileTypes: true }).filter(e => e.isDirectory()).length;
} catch { /* no phases dir */ }
let unresolvedVerification = false;
if (currentPhase) {
const fp = await findPhase([currentPhase], projectDir);
const fd = fp.data as Record<string, unknown>;
if (fd.found && fd.directory) {
unresolvedVerification = await hasUnresolvedVerificationFails(
join(projectDir, fd.directory as string),
);
}
}
const gates = {
continue_here: continueHere,
error_state: errorState,
unresolved_verification: unresolvedVerification,
consecutive_calls: consecutiveCalls,
};
const buildContext = async (cp: string | null) => {
if (!cp) {
return {
has_context: false,
has_research: false,
has_plans: false,
plan_count: 0,
summary_count: 0,
has_verification: false,
paused_at: pausedAt,
uat_gaps: 0,
};
}
const fp = await findPhase([cp], projectDir);
const d = fp.data as Record<string, unknown>;
const plans = (d.plans as string[]) ?? [];
const summaries = (d.summaries as string[]) ?? [];
return {
has_context: Boolean(d.has_context),
has_research: Boolean(d.has_research),
has_plans: plans.length > 0,
plan_count: plans.length,
summary_count: summaries.length,
has_verification: Boolean(d.has_verification),
paused_at: pausedAt,
uat_gaps: 0,
};
};
if (pausedAt) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-resume-work',
args: '',
reason: 'Paused — resume work before other routing',
current_phase: currentPhase,
phase_name: phaseName,
gates,
context: { ...ctx, paused_at: pausedAt },
},
};
}
if (continueHere || errorState || unresolvedVerification) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '',
args: '',
reason: continueHere
? 'Blocked: .planning/.continue-here.md exists'
: errorState
? 'Blocked: STATE.md status is error or failed'
: 'Blocked: unresolved VERIFICATION FAIL items',
current_phase: currentPhase,
phase_name: phaseName,
gates,
context: ctx,
},
};
}
// Route 1 — ROADMAP lists phases but no phase directories
if (phases.length > 0 && dirCount === 0) {
const first = String(phases[0].number);
const ctx = await buildContext(first);
return {
data: {
command: '/gsd-discuss-phase',
args: first,
reason: 'ROADMAP has phases but no phase directories on disk yet',
current_phase: first,
phase_name: String(phases[0].name ?? ''),
gates,
context: ctx,
},
};
}
if (!currentPhase && phases.length > 0) {
currentPhase = String(phases[0].number);
}
if (!currentPhase) {
const ctx = await buildContext(null);
return {
data: {
command: '',
args: '',
reason: 'No current phase in STATE.md and no roadmap phases',
current_phase: null,
phase_name: null,
gates,
context: ctx,
},
};
}
const fp = await findPhase([currentPhase], projectDir);
const pd = fp.data as Record<string, unknown>;
const found = Boolean(pd.found);
const cp = normalizePhaseName(currentPhase);
const displayName = (pd.phase_name as string) || phaseName || '';
const sorted = [...phases].sort((a, b) =>
comparePhaseNum(String(a.number), String(b.number)),
);
if (!found) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-discuss-phase',
args: cp,
reason: 'Phase directory not found — start with discuss',
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
}
const plans = (pd.plans as string[]) ?? [];
const incomplete = (pd.incomplete_plans as string[]) ?? [];
const hasContext = Boolean(pd.has_context);
const hasResearch = Boolean(pd.has_research);
const phaseDirAbs = pd.directory ? join(projectDir, pd.directory as string) : '';
// Route 2
if (!hasContext && !hasResearch) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-discuss-phase',
args: cp,
reason: 'No CONTEXT.md or RESEARCH.md for this phase',
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
}
// Route 3
if (plans.length === 0) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-plan-phase',
args: cp,
reason: 'Context exists but no PLAN.md files',
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
}
// Route 4
if (incomplete.length > 0) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-execute-phase',
args: cp,
reason: `${incomplete.length} plan(s) still need SUMMARY.md`,
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
}
// Summaries match plans — verification / advance
const verPassed = phaseDirAbs ? await verificationPassed(phaseDirAbs) : false;
const hasVerFile = Boolean(pd.has_verification);
if (!hasVerFile || !verPassed) {
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-verify-work',
args: '',
reason: 'All plans have summaries — run verification',
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
}
// Phase verified — Route 6 vs 7 handled by allComplete above; find next incomplete phase
const idx = sorted.findIndex(p => normalizePhaseName(String(p.number)) === cp);
const next = idx >= 0 ? sorted.slice(idx + 1).find(p => p.disk_status !== 'complete' && !p.roadmap_complete) : null;
if (next) {
const nextNum = String(next.number);
const ctx = await buildContext(nextNum);
return {
data: {
command: '/gsd-discuss-phase',
args: nextNum,
reason: 'Current phase verified — advance to next phase',
current_phase: nextNum,
phase_name: String(next.name ?? ''),
gates,
context: ctx,
},
};
}
const ctx = await buildContext(currentPhase);
return {
data: {
command: '/gsd-complete-milestone',
args: '',
reason: 'Verified phase with no further phases — complete milestone',
current_phase: currentPhase,
phase_name: displayName,
gates,
context: ctx,
},
};
};