feat: add --include flag to init commands to eliminate redundant file reads
Workflows were calling init commands to get parsed JSON metadata, then immediately reading the same files again with cat to pass raw content to agents. This wastes context tokens. Changes: - Add --include flag to init execute-phase, plan-phase, and progress - Support includes: state, config, roadmap, requirements, context, research, verification, uat, project - Update plan-phase.md to use --include (removes 6 cat calls) - Update execute-phase.md to use --include (removes 2 cat calls) - Update execute-plan.md to use --include (removes 2 cat calls) - Update progress.md to use --include (removes 4 cat calls) - Add 7 tests for --include functionality Token savings: ~5,000-10,000 tokens per plan-phase execution, ~1,500-3,000 per other workflow executions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -53,6 +53,22 @@ 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');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function loadConfig(cwd) {
|
||||
const configPath = path.join(cwd, '.planning', 'config.json');
|
||||
const defaults = {
|
||||
@@ -1392,7 +1408,7 @@ function getMilestoneInfo(cwd) {
|
||||
}
|
||||
}
|
||||
|
||||
function cmdInitExecutePhase(cwd, phase, raw) {
|
||||
function cmdInitExecutePhase(cwd, phase, includes, raw) {
|
||||
if (!phase) {
|
||||
error('phase required for init execute-phase');
|
||||
}
|
||||
@@ -1450,10 +1466,21 @@ function cmdInitExecutePhase(cwd, phase, raw) {
|
||||
config_exists: pathExistsInternal(cwd, '.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, raw) {
|
||||
function cmdInitPlanPhase(cwd, phase, includes, raw) {
|
||||
if (!phase) {
|
||||
error('phase required for init plan-phase');
|
||||
}
|
||||
@@ -1491,6 +1518,61 @@ function cmdInitPlanPhase(cwd, phase, raw) {
|
||||
roadmap_exists: pathExistsInternal(cwd, '.planning/ROADMAP.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) {
|
||||
// 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));
|
||||
}
|
||||
} 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));
|
||||
}
|
||||
} 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));
|
||||
}
|
||||
} 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));
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
output(result, raw);
|
||||
}
|
||||
|
||||
@@ -1857,7 +1939,7 @@ function cmdInitMapCodebase(cwd, raw) {
|
||||
output(result, raw);
|
||||
}
|
||||
|
||||
function cmdInitProgress(cwd, raw) {
|
||||
function cmdInitProgress(cwd, includes, raw) {
|
||||
const config = loadConfig(cwd);
|
||||
const milestone = getMilestoneInfo(cwd);
|
||||
|
||||
@@ -1947,6 +2029,20 @@ function cmdInitProgress(cwd, raw) {
|
||||
state_exists: pathExistsInternal(cwd, '.planning/STATE.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('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);
|
||||
}
|
||||
|
||||
@@ -2091,12 +2187,13 @@ function main() {
|
||||
|
||||
case 'init': {
|
||||
const workflow = args[1];
|
||||
const includes = parseIncludeFlag(args);
|
||||
switch (workflow) {
|
||||
case 'execute-phase':
|
||||
cmdInitExecutePhase(cwd, args[2], raw);
|
||||
cmdInitExecutePhase(cwd, args[2], includes, raw);
|
||||
break;
|
||||
case 'plan-phase':
|
||||
cmdInitPlanPhase(cwd, args[2], raw);
|
||||
cmdInitPlanPhase(cwd, args[2], includes, raw);
|
||||
break;
|
||||
case 'new-project':
|
||||
cmdInitNewProject(cwd, raw);
|
||||
@@ -2126,7 +2223,7 @@ function main() {
|
||||
cmdInitMapCodebase(cwd, raw);
|
||||
break;
|
||||
case 'progress':
|
||||
cmdInitProgress(cwd, raw);
|
||||
cmdInitProgress(cwd, includes, 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`);
|
||||
|
||||
@@ -1052,3 +1052,142 @@ key-decisions:
|
||||
assert.strictEqual(output.decisions[1].rationale, 'Stateless auth for scalability', 'second decision rationale');
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// init --include flag tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('init commands with --include flag', () => {
|
||||
let tmpDir;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = createTempProject();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup(tmpDir);
|
||||
});
|
||||
|
||||
test('init execute-phase includes state and config 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\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');
|
||||
});
|
||||
|
||||
test('init plan-phase includes multiple file contents', () => {
|
||||
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');
|
||||
|
||||
const result = runGsdTools('init plan-phase 03 --include state,roadmap,requirements,context,research', 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');
|
||||
});
|
||||
|
||||
test('init plan-phase includes verification and uat content', () => {
|
||||
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');
|
||||
|
||||
const result = runGsdTools('init plan-phase 03 --include verification,uat', 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');
|
||||
});
|
||||
|
||||
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', () => {
|
||||
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);
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user