refactor: complete context-proxy orchestration flow

This commit is contained in:
Lex Christopherson
2026-02-19 12:40:45 -06:00
parent bf2f57105f
commit 3dcd3f0609
39 changed files with 351 additions and 427 deletions

View File

@@ -145,14 +145,6 @@ const MODEL_PROFILES = {
// ─── Helpers ──────────────────────────────────────────────────────────────────
function parseIncludeFlag(args) {
const includeIndex = args.indexOf('--include');
if (includeIndex === -1) return new Set();
const includeValue = args[includeIndex + 1];
if (!includeValue) return new Set();
return new Set(includeValue.split(',').map(s => s.trim()));
}
function safeReadFile(filePath) {
try {
return fs.readFileSync(filePath, 'utf-8');
@@ -4244,7 +4236,7 @@ function getMilestoneInfo(cwd) {
}
}
function cmdInitExecutePhase(cwd, phase, includes, raw) {
function cmdInitExecutePhase(cwd, phase, raw) {
if (!phase) {
error('phase required for init execute-phase');
}
@@ -4300,23 +4292,16 @@ function cmdInitExecutePhase(cwd, phase, includes, raw) {
state_exists: pathExistsInternal(cwd, '.planning/STATE.md'),
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
config_exists: pathExistsInternal(cwd, '.planning/config.json'),
// File paths
state_path: '.planning/STATE.md',
roadmap_path: '.planning/ROADMAP.md',
config_path: '.planning/config.json',
};
// Include file contents if requested via --include
if (includes.has('state')) {
result.state_content = safeReadFile(path.join(cwd, '.planning', 'STATE.md'));
}
if (includes.has('config')) {
result.config_content = safeReadFile(path.join(cwd, '.planning', 'config.json'));
}
if (includes.has('roadmap')) {
result.roadmap_content = safeReadFile(path.join(cwd, '.planning', 'ROADMAP.md'));
}
output(result, raw);
}
function cmdInitPlanPhase(cwd, phase, includes, raw) {
function cmdInitPlanPhase(cwd, phase, raw) {
if (!phase) {
error('phase required for init plan-phase');
}
@@ -4352,59 +4337,33 @@ function cmdInitPlanPhase(cwd, phase, includes, raw) {
// Environment
planning_exists: pathExistsInternal(cwd, '.planning'),
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
// File paths
state_path: '.planning/STATE.md',
roadmap_path: '.planning/ROADMAP.md',
requirements_path: '.planning/REQUIREMENTS.md',
};
// Include file contents if requested via --include
if (includes.has('state')) {
result.state_content = safeReadFile(path.join(cwd, '.planning', 'STATE.md'));
}
if (includes.has('roadmap')) {
result.roadmap_content = safeReadFile(path.join(cwd, '.planning', 'ROADMAP.md'));
}
if (includes.has('requirements')) {
result.requirements_content = safeReadFile(path.join(cwd, '.planning', 'REQUIREMENTS.md'));
}
if (includes.has('context') && phaseInfo?.directory) {
if (phaseInfo?.directory) {
// Find *-CONTEXT.md in phase directory
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const contextFile = files.find(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md');
if (contextFile) {
result.context_content = safeReadFile(path.join(phaseDirFull, contextFile));
result.context_path = path.join(phaseInfo.directory, contextFile);
}
} catch {}
}
if (includes.has('research') && phaseInfo?.directory) {
// Find *-RESEARCH.md in phase directory
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const researchFile = files.find(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md');
if (researchFile) {
result.research_content = safeReadFile(path.join(phaseDirFull, researchFile));
result.research_path = path.join(phaseInfo.directory, researchFile);
}
} catch {}
}
if (includes.has('verification') && phaseInfo?.directory) {
// Find *-VERIFICATION.md in phase directory
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const verificationFile = files.find(f => f.endsWith('-VERIFICATION.md') || f === 'VERIFICATION.md');
if (verificationFile) {
result.verification_content = safeReadFile(path.join(phaseDirFull, verificationFile));
result.verification_path = path.join(phaseInfo.directory, verificationFile);
}
} catch {}
}
if (includes.has('uat') && phaseInfo?.directory) {
// Find *-UAT.md in phase directory
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const uatFile = files.find(f => f.endsWith('-UAT.md') || f === 'UAT.md');
if (uatFile) {
result.uat_content = safeReadFile(path.join(phaseDirFull, uatFile));
result.uat_path = path.join(phaseInfo.directory, uatFile);
}
} catch {}
}
@@ -4463,6 +4422,9 @@ function cmdInitNewProject(cwd, raw) {
// Enhanced search
brave_search_available: hasBraveSearch,
// File paths
project_path: '.planning/PROJECT.md',
};
output(result, raw);
@@ -4490,6 +4452,11 @@ function cmdInitNewMilestone(cwd, raw) {
project_exists: pathExistsInternal(cwd, '.planning/PROJECT.md'),
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
state_exists: pathExistsInternal(cwd, '.planning/STATE.md'),
// File paths
project_path: '.planning/PROJECT.md',
roadmap_path: '.planning/ROADMAP.md',
state_path: '.planning/STATE.md',
};
output(result, raw);
@@ -4539,6 +4506,7 @@ function cmdInitQuick(cwd, description, raw) {
// File existence
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
planning_exists: pathExistsInternal(cwd, '.planning'),
};
output(result, raw);
@@ -4560,6 +4528,11 @@ function cmdInitResume(cwd, raw) {
project_exists: pathExistsInternal(cwd, '.planning/PROJECT.md'),
planning_exists: pathExistsInternal(cwd, '.planning'),
// File paths
state_path: '.planning/STATE.md',
roadmap_path: '.planning/ROADMAP.md',
project_path: '.planning/PROJECT.md',
// Agent state
has_interrupted_agent: !!interruptedAgentId,
interrupted_agent_id: interruptedAgentId,
@@ -4648,8 +4621,36 @@ function cmdInitPhaseOp(cwd, phase, raw) {
// File existence
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
planning_exists: pathExistsInternal(cwd, '.planning'),
// File paths
state_path: '.planning/STATE.md',
roadmap_path: '.planning/ROADMAP.md',
requirements_path: '.planning/REQUIREMENTS.md',
};
if (phaseInfo?.directory) {
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const contextFile = files.find(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md');
if (contextFile) {
result.context_path = path.join(phaseInfo.directory, contextFile);
}
const researchFile = files.find(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md');
if (researchFile) {
result.research_path = path.join(phaseInfo.directory, researchFile);
}
const verificationFile = files.find(f => f.endsWith('-VERIFICATION.md') || f === 'VERIFICATION.md');
if (verificationFile) {
result.verification_path = path.join(phaseInfo.directory, verificationFile);
}
const uatFile = files.find(f => f.endsWith('-UAT.md') || f === 'UAT.md');
if (uatFile) {
result.uat_path = path.join(phaseInfo.directory, uatFile);
}
} catch {}
}
output(result, raw);
}
@@ -4807,7 +4808,7 @@ function cmdInitMapCodebase(cwd, raw) {
output(result, raw);
}
function cmdInitProgress(cwd, includes, raw) {
function cmdInitProgress(cwd, raw) {
const config = loadConfig(cwd);
const milestone = getMilestoneInfo(cwd);
@@ -4895,22 +4896,13 @@ function cmdInitProgress(cwd, includes, raw) {
project_exists: pathExistsInternal(cwd, '.planning/PROJECT.md'),
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.md'),
state_exists: pathExistsInternal(cwd, '.planning/STATE.md'),
// File paths
state_path: '.planning/STATE.md',
roadmap_path: '.planning/ROADMAP.md',
project_path: '.planning/PROJECT.md',
config_path: '.planning/config.json',
};
// Include file contents if requested via --include
if (includes.has('state')) {
result.state_content = safeReadFile(path.join(cwd, '.planning', 'STATE.md'));
}
if (includes.has('roadmap')) {
result.roadmap_content = safeReadFile(path.join(cwd, '.planning', 'ROADMAP.md'));
}
if (includes.has('project')) {
result.project_content = safeReadFile(path.join(cwd, '.planning', 'PROJECT.md'));
}
if (includes.has('config')) {
result.config_content = safeReadFile(path.join(cwd, '.planning', 'config.json'));
}
output(result, raw);
}
@@ -5252,13 +5244,12 @@ async function main() {
case 'init': {
const workflow = args[1];
const includes = parseIncludeFlag(args);
switch (workflow) {
case 'execute-phase':
cmdInitExecutePhase(cwd, args[2], includes, raw);
cmdInitExecutePhase(cwd, args[2], raw);
break;
case 'plan-phase':
cmdInitPlanPhase(cwd, args[2], includes, raw);
cmdInitPlanPhase(cwd, args[2], raw);
break;
case 'new-project':
cmdInitNewProject(cwd, raw);
@@ -5288,7 +5279,7 @@ async function main() {
cmdInitMapCodebase(cwd, raw);
break;
case 'progress':
cmdInitProgress(cwd, includes, raw);
cmdInitProgress(cwd, raw);
break;
default:
error(`Unknown init workflow: ${workflow}\nAvailable: execute-phase, plan-phase, new-project, new-milestone, quick, resume, verify-work, phase-op, todos, milestone-op, map-codebase, progress`);

View File

@@ -1098,10 +1098,10 @@ key-decisions:
});
// ─────────────────────────────────────────────────────────────────────────────
// init --include flag tests
// init commands tests
// ─────────────────────────────────────────────────────────────────────────────
describe('init commands with --include flag', () => {
describe('init commands', () => {
let tmpDir;
beforeEach(() => {
@@ -1112,127 +1112,83 @@ describe('init commands with --include flag', () => {
cleanup(tmpDir);
});
test('init execute-phase includes state and config content', () => {
test('init execute-phase returns file paths', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(phaseDir, '03-01-PLAN.md'), '# Plan');
fs.writeFileSync(
path.join(tmpDir, '.planning', 'STATE.md'),
'# State\n\n**Current Phase:** 03\n**Status:** In progress'
);
fs.writeFileSync(
path.join(tmpDir, '.planning', 'config.json'),
JSON.stringify({ model_profile: 'balanced' })
);
const result = runGsdTools('init execute-phase 03 --include state,config', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.ok(output.state_content, 'state_content should be included');
assert.ok(output.state_content.includes('Current Phase'), 'state content correct');
assert.ok(output.config_content, 'config_content should be included');
assert.ok(output.config_content.includes('model_profile'), 'config content correct');
});
test('init execute-phase without --include omits content', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(phaseDir, '03-01-PLAN.md'), '# Plan');
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), '# State');
const result = runGsdTools('init execute-phase 03', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.state_content, undefined, 'state_content should be omitted');
assert.strictEqual(output.config_content, undefined, 'config_content should be omitted');
assert.strictEqual(output.state_path, '.planning/STATE.md');
assert.strictEqual(output.roadmap_path, '.planning/ROADMAP.md');
assert.strictEqual(output.config_path, '.planning/config.json');
});
test('init plan-phase includes multiple file contents', () => {
test('init plan-phase returns file paths', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), '# Project State');
fs.writeFileSync(path.join(tmpDir, '.planning', 'ROADMAP.md'), '# Roadmap v1.0');
fs.writeFileSync(path.join(tmpDir, '.planning', 'REQUIREMENTS.md'), '# Requirements');
fs.writeFileSync(path.join(phaseDir, '03-CONTEXT.md'), '# Phase Context');
fs.writeFileSync(path.join(phaseDir, '03-RESEARCH.md'), '# Research Findings');
fs.writeFileSync(path.join(phaseDir, '03-VERIFICATION.md'), '# Verification');
fs.writeFileSync(path.join(phaseDir, '03-UAT.md'), '# UAT');
const result = runGsdTools('init plan-phase 03 --include state,roadmap,requirements,context,research', tmpDir);
const result = runGsdTools('init plan-phase 03', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.ok(output.state_content, 'state_content included');
assert.ok(output.state_content.includes('Project State'), 'state content correct');
assert.ok(output.roadmap_content, 'roadmap_content included');
assert.ok(output.roadmap_content.includes('Roadmap v1.0'), 'roadmap content correct');
assert.ok(output.requirements_content, 'requirements_content included');
assert.ok(output.context_content, 'context_content included');
assert.ok(output.research_content, 'research_content included');
assert.strictEqual(output.state_path, '.planning/STATE.md');
assert.strictEqual(output.roadmap_path, '.planning/ROADMAP.md');
assert.strictEqual(output.requirements_path, '.planning/REQUIREMENTS.md');
assert.strictEqual(output.context_path, '.planning/phases/03-api/03-CONTEXT.md');
assert.strictEqual(output.research_path, '.planning/phases/03-api/03-RESEARCH.md');
assert.strictEqual(output.verification_path, '.planning/phases/03-api/03-VERIFICATION.md');
assert.strictEqual(output.uat_path, '.planning/phases/03-api/03-UAT.md');
});
test('init plan-phase includes verification and uat content', () => {
test('init progress returns file paths', () => {
const result = runGsdTools('init progress', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.state_path, '.planning/STATE.md');
assert.strictEqual(output.roadmap_path, '.planning/ROADMAP.md');
assert.strictEqual(output.project_path, '.planning/PROJECT.md');
assert.strictEqual(output.config_path, '.planning/config.json');
});
test('init phase-op returns core and optional phase file paths', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(phaseDir, '03-VERIFICATION.md'), '# Verification Results');
fs.writeFileSync(path.join(phaseDir, '03-UAT.md'), '# UAT Findings');
fs.writeFileSync(path.join(phaseDir, '03-CONTEXT.md'), '# Phase Context');
fs.writeFileSync(path.join(phaseDir, '03-RESEARCH.md'), '# Research');
fs.writeFileSync(path.join(phaseDir, '03-VERIFICATION.md'), '# Verification');
fs.writeFileSync(path.join(phaseDir, '03-UAT.md'), '# UAT');
const result = runGsdTools('init plan-phase 03 --include verification,uat', tmpDir);
const result = runGsdTools('init phase-op 03', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.ok(output.verification_content, 'verification_content included');
assert.ok(output.verification_content.includes('Verification Results'), 'verification content correct');
assert.ok(output.uat_content, 'uat_content included');
assert.ok(output.uat_content.includes('UAT Findings'), 'uat content correct');
assert.strictEqual(output.state_path, '.planning/STATE.md');
assert.strictEqual(output.roadmap_path, '.planning/ROADMAP.md');
assert.strictEqual(output.requirements_path, '.planning/REQUIREMENTS.md');
assert.strictEqual(output.context_path, '.planning/phases/03-api/03-CONTEXT.md');
assert.strictEqual(output.research_path, '.planning/phases/03-api/03-RESEARCH.md');
assert.strictEqual(output.verification_path, '.planning/phases/03-api/03-VERIFICATION.md');
assert.strictEqual(output.uat_path, '.planning/phases/03-api/03-UAT.md');
});
test('init progress includes state, roadmap, project, config', () => {
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), '# State');
fs.writeFileSync(path.join(tmpDir, '.planning', 'ROADMAP.md'), '# Roadmap');
fs.writeFileSync(path.join(tmpDir, '.planning', 'PROJECT.md'), '# Project');
fs.writeFileSync(
path.join(tmpDir, '.planning', 'config.json'),
JSON.stringify({ model_profile: 'quality' })
);
const result = runGsdTools('init progress --include state,roadmap,project,config', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.ok(output.state_content, 'state_content included');
assert.ok(output.roadmap_content, 'roadmap_content included');
assert.ok(output.project_content, 'project_content included');
assert.ok(output.config_content, 'config_content included');
});
test('missing files return null in content fields', () => {
test('init plan-phase omits optional paths if files missing', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(phaseDir, '03-01-PLAN.md'), '# Plan');
const result = runGsdTools('init execute-phase 03 --include state,config', tmpDir);
const result = runGsdTools('init plan-phase 03', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.state_content, null, 'missing state returns null');
assert.strictEqual(output.config_content, null, 'missing config returns null');
});
test('partial includes work correctly', () => {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '03-api');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(phaseDir, '03-01-PLAN.md'), '# Plan');
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), '# State');
fs.writeFileSync(path.join(tmpDir, '.planning', 'ROADMAP.md'), '# Roadmap');
// Only request state, not roadmap
const result = runGsdTools('init execute-phase 03 --include state', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.ok(output.state_content, 'state_content included');
assert.strictEqual(output.roadmap_content, undefined, 'roadmap_content not requested, should be undefined');
assert.strictEqual(output.context_path, undefined);
assert.strictEqual(output.research_path, undefined);
});
});