|
|
|
|
@@ -4,7 +4,8 @@
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const { loadConfig, output, error } = require('./core.cjs');
|
|
|
|
|
const { loadConfig, getMilestoneInfo, output, error } = require('./core.cjs');
|
|
|
|
|
const { extractFrontmatter, reconstructFrontmatter } = require('./frontmatter.cjs');
|
|
|
|
|
|
|
|
|
|
function cmdStateLoad(cwd, raw) {
|
|
|
|
|
const config = loadConfig(cwd);
|
|
|
|
|
@@ -116,7 +117,7 @@ function cmdStatePatch(cwd, patches, raw) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (results.updated.length > 0) {
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output(results, raw, results.updated.length > 0 ? 'true' : 'false');
|
|
|
|
|
@@ -137,7 +138,7 @@ function cmdStateUpdate(cwd, field, value) {
|
|
|
|
|
const pattern = new RegExp(`(\\*\\*${fieldEscaped}:\\*\\*\\s*)(.*)`, 'i');
|
|
|
|
|
if (pattern.test(content)) {
|
|
|
|
|
content = content.replace(pattern, (_match, prefix) => `${prefix}${value}`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ updated: true });
|
|
|
|
|
} else {
|
|
|
|
|
output({ updated: false, reason: `Field "${field}" not found in STATE.md` });
|
|
|
|
|
@@ -181,14 +182,14 @@ function cmdStateAdvancePlan(cwd, raw) {
|
|
|
|
|
if (currentPlan >= totalPlans) {
|
|
|
|
|
content = stateReplaceField(content, 'Status', 'Phase complete — ready for verification') || content;
|
|
|
|
|
content = stateReplaceField(content, 'Last Activity', today) || content;
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ advanced: false, reason: 'last_plan', current_plan: currentPlan, total_plans: totalPlans, status: 'ready_for_verification' }, raw, 'false');
|
|
|
|
|
} else {
|
|
|
|
|
const newPlan = currentPlan + 1;
|
|
|
|
|
content = stateReplaceField(content, 'Current Plan', String(newPlan)) || content;
|
|
|
|
|
content = stateReplaceField(content, 'Status', 'Ready to execute') || content;
|
|
|
|
|
content = stateReplaceField(content, 'Last Activity', today) || content;
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ advanced: true, previous_plan: currentPlan, current_plan: newPlan, total_plans: totalPlans }, raw, 'true');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -220,7 +221,7 @@ function cmdStateRecordMetric(cwd, options, raw) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content = content.replace(metricsPattern, (_match, header) => `${header}${tableBody}\n`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ recorded: true, phase, plan, duration }, raw, 'true');
|
|
|
|
|
} else {
|
|
|
|
|
output({ recorded: false, reason: 'Performance Metrics section not found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -257,7 +258,7 @@ function cmdStateUpdateProgress(cwd, raw) {
|
|
|
|
|
const progressPattern = /(\*\*Progress:\*\*\s*).*/i;
|
|
|
|
|
if (progressPattern.test(content)) {
|
|
|
|
|
content = content.replace(progressPattern, (_match, prefix) => `${prefix}${progressStr}`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ updated: true, percent, completed: totalSummaries, total: totalPlans, bar: progressStr }, raw, progressStr);
|
|
|
|
|
} else {
|
|
|
|
|
output({ updated: false, reason: 'Progress field not found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -295,7 +296,7 @@ function cmdStateAddDecision(cwd, options, raw) {
|
|
|
|
|
sectionBody = sectionBody.replace(/None yet\.?\s*\n?/gi, '').replace(/No decisions yet\.?\s*\n?/gi, '');
|
|
|
|
|
sectionBody = sectionBody.trimEnd() + '\n' + entry + '\n';
|
|
|
|
|
content = content.replace(sectionPattern, (_match, header) => `${header}${sectionBody}`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ added: true, decision: entry }, raw, 'true');
|
|
|
|
|
} else {
|
|
|
|
|
output({ added: false, reason: 'Decisions section not found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -328,7 +329,7 @@ function cmdStateAddBlocker(cwd, text, raw) {
|
|
|
|
|
sectionBody = sectionBody.replace(/None\.?\s*\n?/gi, '').replace(/None yet\.?\s*\n?/gi, '');
|
|
|
|
|
sectionBody = sectionBody.trimEnd() + '\n' + entry + '\n';
|
|
|
|
|
content = content.replace(sectionPattern, (_match, header) => `${header}${sectionBody}`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ added: true, blocker: blockerText }, raw, 'true');
|
|
|
|
|
} else {
|
|
|
|
|
output({ added: false, reason: 'Blockers section not found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -360,7 +361,7 @@ function cmdStateResolveBlocker(cwd, text, raw) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content = content.replace(sectionPattern, (_match, header) => `${header}${newBody}`);
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ resolved: true, blocker: text }, raw, 'true');
|
|
|
|
|
} else {
|
|
|
|
|
output({ resolved: false, reason: 'Blockers section not found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -395,7 +396,7 @@ function cmdStateRecordSession(cwd, options, raw) {
|
|
|
|
|
if (result) { content = result; updated.push('Resume File'); }
|
|
|
|
|
|
|
|
|
|
if (updated.length > 0) {
|
|
|
|
|
fs.writeFileSync(statePath, content, 'utf-8');
|
|
|
|
|
writeStateMd(statePath, content, cwd);
|
|
|
|
|
output({ recorded: true, updated }, raw, 'true');
|
|
|
|
|
} else {
|
|
|
|
|
output({ recorded: false, reason: 'No session fields found in STATE.md' }, raw, 'false');
|
|
|
|
|
@@ -503,9 +504,165 @@ function cmdStateSnapshot(cwd, raw) {
|
|
|
|
|
output(result, raw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── State Frontmatter Sync ──────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract machine-readable fields from STATE.md markdown body and build
|
|
|
|
|
* a YAML frontmatter object. Allows hooks and scripts to read state
|
|
|
|
|
* reliably via `state json` instead of fragile regex parsing.
|
|
|
|
|
*/
|
|
|
|
|
function buildStateFrontmatter(bodyContent, cwd) {
|
|
|
|
|
const extractField = (fieldName) => {
|
|
|
|
|
const pattern = new RegExp(`\\*\\*${fieldName}:\\*\\*\\s*(.+)`, 'i');
|
|
|
|
|
const match = bodyContent.match(pattern);
|
|
|
|
|
return match ? match[1].trim() : null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const currentPhase = extractField('Current Phase');
|
|
|
|
|
const currentPhaseName = extractField('Current Phase Name');
|
|
|
|
|
const currentPlan = extractField('Current Plan');
|
|
|
|
|
const totalPhasesRaw = extractField('Total Phases');
|
|
|
|
|
const totalPlansRaw = extractField('Total Plans in Phase');
|
|
|
|
|
const status = extractField('Status');
|
|
|
|
|
const progressRaw = extractField('Progress');
|
|
|
|
|
const lastActivity = extractField('Last Activity');
|
|
|
|
|
const stoppedAt = extractField('Stopped At') || extractField('Stopped at');
|
|
|
|
|
const pausedAt = extractField('Paused At');
|
|
|
|
|
|
|
|
|
|
let milestone = null;
|
|
|
|
|
let milestoneName = null;
|
|
|
|
|
if (cwd) {
|
|
|
|
|
try {
|
|
|
|
|
const info = getMilestoneInfo(cwd);
|
|
|
|
|
milestone = info.version;
|
|
|
|
|
milestoneName = info.name;
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let totalPhases = totalPhasesRaw ? parseInt(totalPhasesRaw, 10) : null;
|
|
|
|
|
let completedPhases = null;
|
|
|
|
|
let totalPlans = totalPlansRaw ? parseInt(totalPlansRaw, 10) : null;
|
|
|
|
|
let completedPlans = null;
|
|
|
|
|
|
|
|
|
|
if (cwd) {
|
|
|
|
|
try {
|
|
|
|
|
const phasesDir = path.join(cwd, '.planning', 'phases');
|
|
|
|
|
if (fs.existsSync(phasesDir)) {
|
|
|
|
|
const phaseDirs = fs.readdirSync(phasesDir, { withFileTypes: true })
|
|
|
|
|
.filter(e => e.isDirectory()).map(e => e.name);
|
|
|
|
|
let diskTotalPlans = 0;
|
|
|
|
|
let diskTotalSummaries = 0;
|
|
|
|
|
let diskCompletedPhases = 0;
|
|
|
|
|
|
|
|
|
|
for (const dir of phaseDirs) {
|
|
|
|
|
const files = fs.readdirSync(path.join(phasesDir, dir));
|
|
|
|
|
const plans = files.filter(f => f.match(/-PLAN\.md$/i)).length;
|
|
|
|
|
const summaries = files.filter(f => f.match(/-SUMMARY\.md$/i)).length;
|
|
|
|
|
diskTotalPlans += plans;
|
|
|
|
|
diskTotalSummaries += summaries;
|
|
|
|
|
if (plans > 0 && summaries >= plans) diskCompletedPhases++;
|
|
|
|
|
}
|
|
|
|
|
if (totalPhases === null) totalPhases = phaseDirs.length;
|
|
|
|
|
completedPhases = diskCompletedPhases;
|
|
|
|
|
totalPlans = diskTotalPlans;
|
|
|
|
|
completedPlans = diskTotalSummaries;
|
|
|
|
|
}
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let progressPercent = null;
|
|
|
|
|
if (progressRaw) {
|
|
|
|
|
const pctMatch = progressRaw.match(/(\d+)%/);
|
|
|
|
|
if (pctMatch) progressPercent = parseInt(pctMatch[1], 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normalize status to one of: planning, discussing, executing, verifying, paused, completed, unknown
|
|
|
|
|
let normalizedStatus = status || 'unknown';
|
|
|
|
|
const statusLower = (status || '').toLowerCase();
|
|
|
|
|
if (statusLower.includes('paused') || statusLower.includes('stopped') || pausedAt) {
|
|
|
|
|
normalizedStatus = 'paused';
|
|
|
|
|
} else if (statusLower.includes('executing') || statusLower.includes('in progress')) {
|
|
|
|
|
normalizedStatus = 'executing';
|
|
|
|
|
} else if (statusLower.includes('planning') || statusLower.includes('ready to plan')) {
|
|
|
|
|
normalizedStatus = 'planning';
|
|
|
|
|
} else if (statusLower.includes('discussing')) {
|
|
|
|
|
normalizedStatus = 'discussing';
|
|
|
|
|
} else if (statusLower.includes('verif')) {
|
|
|
|
|
normalizedStatus = 'verifying';
|
|
|
|
|
} else if (statusLower.includes('complete') || statusLower.includes('done')) {
|
|
|
|
|
normalizedStatus = 'completed';
|
|
|
|
|
} else if (statusLower.includes('ready to execute')) {
|
|
|
|
|
normalizedStatus = 'executing';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fm = { gsd_state_version: '1.0' };
|
|
|
|
|
|
|
|
|
|
if (milestone) fm.milestone = milestone;
|
|
|
|
|
if (milestoneName) fm.milestone_name = milestoneName;
|
|
|
|
|
if (currentPhase) fm.current_phase = currentPhase;
|
|
|
|
|
if (currentPhaseName) fm.current_phase_name = currentPhaseName;
|
|
|
|
|
if (currentPlan) fm.current_plan = currentPlan;
|
|
|
|
|
fm.status = normalizedStatus;
|
|
|
|
|
if (stoppedAt) fm.stopped_at = stoppedAt;
|
|
|
|
|
if (pausedAt) fm.paused_at = pausedAt;
|
|
|
|
|
fm.last_updated = new Date().toISOString();
|
|
|
|
|
if (lastActivity) fm.last_activity = lastActivity;
|
|
|
|
|
|
|
|
|
|
const progress = {};
|
|
|
|
|
if (totalPhases !== null) progress.total_phases = totalPhases;
|
|
|
|
|
if (completedPhases !== null) progress.completed_phases = completedPhases;
|
|
|
|
|
if (totalPlans !== null) progress.total_plans = totalPlans;
|
|
|
|
|
if (completedPlans !== null) progress.completed_plans = completedPlans;
|
|
|
|
|
if (progressPercent !== null) progress.percent = progressPercent;
|
|
|
|
|
if (Object.keys(progress).length > 0) fm.progress = progress;
|
|
|
|
|
|
|
|
|
|
return fm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stripFrontmatter(content) {
|
|
|
|
|
return content.replace(/^---\n[\s\S]*?\n---\n*/, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncStateFrontmatter(content, cwd) {
|
|
|
|
|
const body = stripFrontmatter(content);
|
|
|
|
|
const fm = buildStateFrontmatter(body, cwd);
|
|
|
|
|
const yamlStr = reconstructFrontmatter(fm);
|
|
|
|
|
return `---\n${yamlStr}\n---\n\n${body}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write STATE.md with synchronized YAML frontmatter.
|
|
|
|
|
* All STATE.md writes should use this instead of raw writeFileSync.
|
|
|
|
|
*/
|
|
|
|
|
function writeStateMd(statePath, content, cwd) {
|
|
|
|
|
const synced = syncStateFrontmatter(content, cwd);
|
|
|
|
|
fs.writeFileSync(statePath, synced, 'utf-8');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cmdStateJson(cwd, raw) {
|
|
|
|
|
const statePath = path.join(cwd, '.planning', 'STATE.md');
|
|
|
|
|
if (!fs.existsSync(statePath)) {
|
|
|
|
|
output({ error: 'STATE.md not found' }, raw, 'STATE.md not found');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const content = fs.readFileSync(statePath, 'utf-8');
|
|
|
|
|
const fm = extractFrontmatter(content);
|
|
|
|
|
|
|
|
|
|
if (!fm || Object.keys(fm).length === 0) {
|
|
|
|
|
const body = stripFrontmatter(content);
|
|
|
|
|
const built = buildStateFrontmatter(body, cwd);
|
|
|
|
|
output(built, raw, JSON.stringify(built, null, 2));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output(fm, raw, JSON.stringify(fm, null, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
stateExtractField,
|
|
|
|
|
stateReplaceField,
|
|
|
|
|
writeStateMd,
|
|
|
|
|
cmdStateLoad,
|
|
|
|
|
cmdStateGet,
|
|
|
|
|
cmdStatePatch,
|
|
|
|
|
@@ -518,4 +675,5 @@ module.exports = {
|
|
|
|
|
cmdStateResolveBlocker,
|
|
|
|
|
cmdStateRecordSession,
|
|
|
|
|
cmdStateSnapshot,
|
|
|
|
|
cmdStateJson,
|
|
|
|
|
};
|
|
|
|
|
|