gsd-pi 2.26.0 → 2.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -6
- package/dist/cli.js +4 -2
- package/dist/headless.d.ts +3 -0
- package/dist/headless.js +136 -8
- package/dist/help-text.js +3 -0
- package/dist/loader.js +33 -4
- package/dist/resources/extensions/bg-shell/index.ts +19 -2
- package/dist/resources/extensions/bg-shell/process-manager.ts +45 -0
- package/dist/resources/extensions/bg-shell/types.ts +21 -1
- package/dist/resources/extensions/gsd/auto/session.ts +224 -0
- package/dist/resources/extensions/gsd/auto-budget.ts +32 -0
- package/dist/resources/extensions/gsd/auto-dashboard.ts +63 -10
- package/dist/resources/extensions/gsd/auto-direct-dispatch.ts +229 -0
- package/dist/resources/extensions/gsd/auto-dispatch.ts +23 -10
- package/dist/resources/extensions/gsd/auto-model-selection.ts +179 -0
- package/dist/resources/extensions/gsd/auto-observability.ts +74 -0
- package/dist/resources/extensions/gsd/auto-prompts.ts +0 -1
- package/dist/resources/extensions/gsd/auto-timeout-recovery.ts +262 -0
- package/dist/resources/extensions/gsd/auto-tool-tracking.ts +54 -0
- package/dist/resources/extensions/gsd/auto-unit-closeout.ts +46 -0
- package/dist/resources/extensions/gsd/auto-worktree-sync.ts +207 -0
- package/dist/resources/extensions/gsd/auto.ts +977 -1551
- package/dist/resources/extensions/gsd/commands.ts +3 -3
- package/dist/resources/extensions/gsd/dashboard-overlay.ts +47 -72
- package/dist/resources/extensions/gsd/doctor-proactive.ts +9 -4
- package/dist/resources/extensions/gsd/export-html.ts +1001 -0
- package/dist/resources/extensions/gsd/export.ts +49 -1
- package/dist/resources/extensions/gsd/git-service.ts +6 -0
- package/dist/resources/extensions/gsd/gitignore.ts +4 -1
- package/dist/resources/extensions/gsd/guided-flow.ts +24 -5
- package/dist/resources/extensions/gsd/index.ts +54 -1
- package/dist/resources/extensions/gsd/native-git-bridge.ts +30 -2
- package/dist/resources/extensions/gsd/observability-validator.ts +21 -0
- package/dist/resources/extensions/gsd/parallel-orchestrator.ts +231 -20
- package/dist/resources/extensions/gsd/preferences.ts +62 -1
- package/dist/resources/extensions/gsd/prompts/execute-task.md +4 -3
- package/dist/resources/extensions/gsd/prompts/system.md +1 -1
- package/dist/resources/extensions/gsd/reports.ts +510 -0
- package/dist/resources/extensions/gsd/roadmap-slices.ts +1 -1
- package/dist/resources/extensions/gsd/skills/gsd-headless/SKILL.md +178 -0
- package/dist/resources/extensions/gsd/skills/gsd-headless/references/answer-injection.md +54 -0
- package/dist/resources/extensions/gsd/skills/gsd-headless/references/commands.md +59 -0
- package/dist/resources/extensions/gsd/skills/gsd-headless/references/multi-session.md +185 -0
- package/dist/resources/extensions/gsd/state.ts +30 -0
- package/dist/resources/extensions/gsd/templates/task-summary.md +9 -0
- package/dist/resources/extensions/gsd/tests/auto-dashboard.test.ts +13 -0
- package/dist/resources/extensions/gsd/tests/continue-here.test.ts +81 -0
- package/dist/resources/extensions/gsd/tests/derive-state-db.test.ts +5 -0
- package/dist/resources/extensions/gsd/tests/derive-state-deps.test.ts +1 -0
- package/dist/resources/extensions/gsd/tests/derive-state-draft.test.ts +1 -0
- package/dist/resources/extensions/gsd/tests/derive-state.test.ts +10 -1
- package/dist/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +132 -0
- package/dist/resources/extensions/gsd/tests/doctor-proactive.test.ts +14 -0
- package/dist/resources/extensions/gsd/tests/integration-mixed-milestones.test.ts +1 -0
- package/dist/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +1 -1
- package/dist/resources/extensions/gsd/tests/native-has-changes-cache.test.ts +61 -0
- package/dist/resources/extensions/gsd/tests/network-error-fallback.test.ts +51 -1
- package/dist/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +331 -0
- package/dist/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +298 -0
- package/dist/resources/extensions/gsd/tests/parallel-merge.test.ts +465 -0
- package/dist/resources/extensions/gsd/tests/parallel-orchestration.test.ts +39 -10
- package/dist/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +71 -0
- package/dist/resources/extensions/gsd/tests/replan-slice.test.ts +42 -0
- package/dist/resources/extensions/gsd/tests/triage-dispatch.test.ts +9 -9
- package/dist/resources/extensions/gsd/tests/verification-evidence.test.ts +743 -0
- package/dist/resources/extensions/gsd/tests/verification-gate.test.ts +965 -0
- package/dist/resources/extensions/gsd/tests/visualizer-data.test.ts +1 -1
- package/dist/resources/extensions/gsd/tests/visualizer-overlay.test.ts +44 -10
- package/dist/resources/extensions/gsd/tests/worktree.test.ts +3 -1
- package/dist/resources/extensions/gsd/types.ts +38 -0
- package/dist/resources/extensions/gsd/verification-evidence.ts +183 -0
- package/dist/resources/extensions/gsd/verification-gate.ts +567 -0
- package/dist/resources/extensions/gsd/visualizer-data.ts +25 -3
- package/dist/resources/extensions/gsd/visualizer-overlay.ts +31 -21
- package/dist/resources/extensions/gsd/visualizer-views.ts +15 -66
- package/dist/resources/extensions/search-the-web/tool-search.ts +26 -0
- package/dist/resources/extensions/shared/format-utils.ts +85 -0
- package/dist/resources/extensions/shared/tests/format-utils.test.ts +153 -0
- package/dist/resources/extensions/subagent/index.ts +46 -1
- package/dist/resources/extensions/subagent/isolation.ts +9 -6
- package/package.json +1 -1
- package/packages/pi-ai/dist/providers/openai-completions.js +7 -4
- package/packages/pi-ai/dist/providers/openai-completions.js.map +1 -1
- package/packages/pi-ai/src/providers/openai-completions.ts +7 -4
- package/packages/pi-coding-agent/dist/core/lsp/client.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/lsp/client.js +7 -0
- package/packages/pi-coding-agent/dist/core/lsp/client.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/lsp/config.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/lsp/config.js +9 -2
- package/packages/pi-coding-agent/dist/core/lsp/config.js.map +1 -1
- package/packages/pi-coding-agent/src/core/lsp/client.ts +8 -0
- package/packages/pi-coding-agent/src/core/lsp/config.ts +9 -2
- package/packages/pi-tui/dist/components/editor.d.ts.map +1 -1
- package/packages/pi-tui/dist/components/editor.js +1 -1
- package/packages/pi-tui/dist/components/editor.js.map +1 -1
- package/packages/pi-tui/src/components/editor.ts +3 -1
- package/scripts/link-workspace-packages.cjs +22 -6
- package/src/resources/extensions/bg-shell/index.ts +19 -2
- package/src/resources/extensions/bg-shell/process-manager.ts +45 -0
- package/src/resources/extensions/bg-shell/types.ts +21 -1
- package/src/resources/extensions/gsd/auto/session.ts +224 -0
- package/src/resources/extensions/gsd/auto-budget.ts +32 -0
- package/src/resources/extensions/gsd/auto-dashboard.ts +63 -10
- package/src/resources/extensions/gsd/auto-direct-dispatch.ts +229 -0
- package/src/resources/extensions/gsd/auto-dispatch.ts +23 -10
- package/src/resources/extensions/gsd/auto-model-selection.ts +179 -0
- package/src/resources/extensions/gsd/auto-observability.ts +74 -0
- package/src/resources/extensions/gsd/auto-prompts.ts +0 -1
- package/src/resources/extensions/gsd/auto-timeout-recovery.ts +262 -0
- package/src/resources/extensions/gsd/auto-tool-tracking.ts +54 -0
- package/src/resources/extensions/gsd/auto-unit-closeout.ts +46 -0
- package/src/resources/extensions/gsd/auto-worktree-sync.ts +207 -0
- package/src/resources/extensions/gsd/auto.ts +977 -1551
- package/src/resources/extensions/gsd/commands.ts +3 -3
- package/src/resources/extensions/gsd/dashboard-overlay.ts +47 -72
- package/src/resources/extensions/gsd/doctor-proactive.ts +9 -4
- package/src/resources/extensions/gsd/export-html.ts +1001 -0
- package/src/resources/extensions/gsd/export.ts +49 -1
- package/src/resources/extensions/gsd/git-service.ts +6 -0
- package/src/resources/extensions/gsd/gitignore.ts +4 -1
- package/src/resources/extensions/gsd/guided-flow.ts +24 -5
- package/src/resources/extensions/gsd/index.ts +54 -1
- package/src/resources/extensions/gsd/native-git-bridge.ts +30 -2
- package/src/resources/extensions/gsd/observability-validator.ts +21 -0
- package/src/resources/extensions/gsd/parallel-orchestrator.ts +231 -20
- package/src/resources/extensions/gsd/preferences.ts +62 -1
- package/src/resources/extensions/gsd/prompts/execute-task.md +4 -3
- package/src/resources/extensions/gsd/prompts/system.md +1 -1
- package/src/resources/extensions/gsd/reports.ts +510 -0
- package/src/resources/extensions/gsd/roadmap-slices.ts +1 -1
- package/src/resources/extensions/gsd/skills/gsd-headless/SKILL.md +178 -0
- package/src/resources/extensions/gsd/skills/gsd-headless/references/answer-injection.md +54 -0
- package/src/resources/extensions/gsd/skills/gsd-headless/references/commands.md +59 -0
- package/src/resources/extensions/gsd/skills/gsd-headless/references/multi-session.md +185 -0
- package/src/resources/extensions/gsd/state.ts +30 -0
- package/src/resources/extensions/gsd/templates/task-summary.md +9 -0
- package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +13 -0
- package/src/resources/extensions/gsd/tests/continue-here.test.ts +81 -0
- package/src/resources/extensions/gsd/tests/derive-state-db.test.ts +5 -0
- package/src/resources/extensions/gsd/tests/derive-state-deps.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/derive-state-draft.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/derive-state.test.ts +10 -1
- package/src/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +132 -0
- package/src/resources/extensions/gsd/tests/doctor-proactive.test.ts +14 -0
- package/src/resources/extensions/gsd/tests/integration-mixed-milestones.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/native-has-changes-cache.test.ts +61 -0
- package/src/resources/extensions/gsd/tests/network-error-fallback.test.ts +51 -1
- package/src/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +331 -0
- package/src/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +298 -0
- package/src/resources/extensions/gsd/tests/parallel-merge.test.ts +465 -0
- package/src/resources/extensions/gsd/tests/parallel-orchestration.test.ts +39 -10
- package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +71 -0
- package/src/resources/extensions/gsd/tests/replan-slice.test.ts +42 -0
- package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +9 -9
- package/src/resources/extensions/gsd/tests/verification-evidence.test.ts +743 -0
- package/src/resources/extensions/gsd/tests/verification-gate.test.ts +965 -0
- package/src/resources/extensions/gsd/tests/visualizer-data.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/visualizer-overlay.test.ts +44 -10
- package/src/resources/extensions/gsd/tests/worktree.test.ts +3 -1
- package/src/resources/extensions/gsd/types.ts +38 -0
- package/src/resources/extensions/gsd/verification-evidence.ts +183 -0
- package/src/resources/extensions/gsd/verification-gate.ts +567 -0
- package/src/resources/extensions/gsd/visualizer-data.ts +25 -3
- package/src/resources/extensions/gsd/visualizer-overlay.ts +31 -21
- package/src/resources/extensions/gsd/visualizer-views.ts +15 -66
- package/src/resources/extensions/search-the-web/tool-search.ts +26 -0
- package/src/resources/extensions/shared/format-utils.ts +85 -0
- package/src/resources/extensions/shared/tests/format-utils.test.ts +153 -0
- package/src/resources/extensions/subagent/index.ts +46 -1
- package/src/resources/extensions/subagent/isolation.ts +9 -6
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worktree ↔ project root state synchronization for auto-mode.
|
|
3
|
+
*
|
|
4
|
+
* When auto-mode runs inside a worktree, dispatch-critical state files
|
|
5
|
+
* (.gsd/ metadata) diverge between the worktree (where work happens)
|
|
6
|
+
* and the project root (where startAutoMode reads initial state on restart).
|
|
7
|
+
* Without syncing, restarting auto-mode reads stale state from the project
|
|
8
|
+
* root and re-dispatches already-completed units.
|
|
9
|
+
*
|
|
10
|
+
* Also contains resource staleness detection and stale worktree escape.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, cpSync, unlinkSync, readdirSync } from "node:fs";
|
|
14
|
+
import { join, sep as pathSep } from "node:path";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
|
|
17
|
+
// ─── Project Root → Worktree Sync ─────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sync milestone artifacts from project root INTO worktree before deriveState.
|
|
21
|
+
* Covers the case where the LLM wrote artifacts to the main repo filesystem
|
|
22
|
+
* (e.g. via absolute paths) but the worktree has stale data. Also deletes
|
|
23
|
+
* gsd.db in the worktree so it rebuilds from fresh disk state (#853).
|
|
24
|
+
* Non-fatal — sync failure should never block dispatch.
|
|
25
|
+
*/
|
|
26
|
+
export function syncProjectRootToWorktree(projectRoot: string, worktreePath: string, milestoneId: string | null): void {
|
|
27
|
+
if (!worktreePath || !projectRoot || worktreePath === projectRoot) return;
|
|
28
|
+
if (!milestoneId) return;
|
|
29
|
+
|
|
30
|
+
const prGsd = join(projectRoot, ".gsd");
|
|
31
|
+
const wtGsd = join(worktreePath, ".gsd");
|
|
32
|
+
|
|
33
|
+
// Copy milestone directory from project root to worktree if the project root
|
|
34
|
+
// has newer artifacts (e.g. slices that don't exist in the worktree yet)
|
|
35
|
+
try {
|
|
36
|
+
const srcMilestone = join(prGsd, "milestones", milestoneId);
|
|
37
|
+
const dstMilestone = join(wtGsd, "milestones", milestoneId);
|
|
38
|
+
if (existsSync(srcMilestone)) {
|
|
39
|
+
mkdirSync(dstMilestone, { recursive: true });
|
|
40
|
+
cpSync(srcMilestone, dstMilestone, { recursive: true, force: false });
|
|
41
|
+
}
|
|
42
|
+
} catch { /* non-fatal */ }
|
|
43
|
+
|
|
44
|
+
// Delete worktree gsd.db so it rebuilds from the freshly synced files.
|
|
45
|
+
// Stale DB rows are the root cause of the infinite skip loop (#853).
|
|
46
|
+
try {
|
|
47
|
+
const wtDb = join(wtGsd, "gsd.db");
|
|
48
|
+
if (existsSync(wtDb)) {
|
|
49
|
+
unlinkSync(wtDb);
|
|
50
|
+
}
|
|
51
|
+
} catch { /* non-fatal */ }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ─── Worktree → Project Root Sync ─────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Sync dispatch-critical .gsd/ state files from worktree to project root.
|
|
58
|
+
* Only runs when inside an auto-worktree (worktreePath differs from projectRoot).
|
|
59
|
+
* Copies: STATE.md + active milestone directory (roadmap, slice plans, task summaries).
|
|
60
|
+
* Non-fatal — sync failure should never block dispatch.
|
|
61
|
+
*/
|
|
62
|
+
export function syncStateToProjectRoot(worktreePath: string, projectRoot: string, milestoneId: string | null): void {
|
|
63
|
+
if (!worktreePath || !projectRoot || worktreePath === projectRoot) return;
|
|
64
|
+
if (!milestoneId) return;
|
|
65
|
+
|
|
66
|
+
const wtGsd = join(worktreePath, ".gsd");
|
|
67
|
+
const prGsd = join(projectRoot, ".gsd");
|
|
68
|
+
|
|
69
|
+
// 1. STATE.md — the quick-glance status used by initial deriveState()
|
|
70
|
+
try {
|
|
71
|
+
const src = join(wtGsd, "STATE.md");
|
|
72
|
+
const dst = join(prGsd, "STATE.md");
|
|
73
|
+
if (existsSync(src)) cpSync(src, dst, { force: true });
|
|
74
|
+
} catch { /* non-fatal */ }
|
|
75
|
+
|
|
76
|
+
// 2. Milestone directory — ROADMAP, slice PLANs, task summaries
|
|
77
|
+
// Copy the entire milestone .gsd subtree so deriveState reads current checkboxes
|
|
78
|
+
try {
|
|
79
|
+
const srcMilestone = join(wtGsd, "milestones", milestoneId);
|
|
80
|
+
const dstMilestone = join(prGsd, "milestones", milestoneId);
|
|
81
|
+
if (existsSync(srcMilestone)) {
|
|
82
|
+
mkdirSync(dstMilestone, { recursive: true });
|
|
83
|
+
cpSync(srcMilestone, dstMilestone, { recursive: true, force: true });
|
|
84
|
+
}
|
|
85
|
+
} catch { /* non-fatal */ }
|
|
86
|
+
|
|
87
|
+
// 3. Merge completed-units.json (set-union of both locations)
|
|
88
|
+
// Prevents already-completed units from being re-dispatched after crash/restart.
|
|
89
|
+
const srcKeysFile = join(wtGsd, "completed-units.json");
|
|
90
|
+
const dstKeysFile = join(prGsd, "completed-units.json");
|
|
91
|
+
if (existsSync(srcKeysFile)) {
|
|
92
|
+
try {
|
|
93
|
+
const srcKeys: string[] = JSON.parse(readFileSync(srcKeysFile, "utf8"));
|
|
94
|
+
let dstKeys: string[] = [];
|
|
95
|
+
if (existsSync(dstKeysFile)) {
|
|
96
|
+
try { dstKeys = JSON.parse(readFileSync(dstKeysFile, "utf8")); } catch { /* ignore corrupt dst */ }
|
|
97
|
+
}
|
|
98
|
+
const merged = [...new Set([...dstKeys, ...srcKeys])];
|
|
99
|
+
writeFileSync(dstKeysFile, JSON.stringify(merged, null, 2));
|
|
100
|
+
} catch { /* non-fatal */ }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 4. Runtime records — unit dispatch state used by selfHealRuntimeRecords().
|
|
104
|
+
// Without this, a crash during a unit leaves the runtime record only in the
|
|
105
|
+
// worktree. If the next session resolves basePath before worktree re-entry,
|
|
106
|
+
// selfHeal can't find or clear the stale record (#769).
|
|
107
|
+
try {
|
|
108
|
+
const srcRuntime = join(wtGsd, "runtime", "units");
|
|
109
|
+
const dstRuntime = join(prGsd, "runtime", "units");
|
|
110
|
+
if (existsSync(srcRuntime)) {
|
|
111
|
+
mkdirSync(dstRuntime, { recursive: true });
|
|
112
|
+
cpSync(srcRuntime, dstRuntime, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
} catch { /* non-fatal */ }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ─── Resource Staleness ───────────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Read the resource version (semver) from the managed-resources manifest.
|
|
121
|
+
* Uses gsdVersion instead of syncedAt so that launching a second session
|
|
122
|
+
* doesn't falsely trigger staleness (#804).
|
|
123
|
+
*/
|
|
124
|
+
export function readResourceVersion(): string | null {
|
|
125
|
+
const agentDir = process.env.GSD_CODING_AGENT_DIR || join(homedir(), ".gsd", "agent");
|
|
126
|
+
const manifestPath = join(agentDir, "managed-resources.json");
|
|
127
|
+
try {
|
|
128
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
129
|
+
return typeof manifest?.gsdVersion === "string" ? manifest.gsdVersion : null;
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Check if managed resources have been updated since session start.
|
|
137
|
+
* Returns a warning message if stale, null otherwise.
|
|
138
|
+
*/
|
|
139
|
+
export function checkResourcesStale(versionOnStart: string | null): string | null {
|
|
140
|
+
if (versionOnStart === null) return null;
|
|
141
|
+
const current = readResourceVersion();
|
|
142
|
+
if (current === null) return null;
|
|
143
|
+
if (current !== versionOnStart) {
|
|
144
|
+
return "GSD resources were updated since this session started. Restart gsd to load the new code.";
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ─── Stale Worktree Escape ────────────────────────────────────────────────
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Detect and escape a stale worktree cwd (#608).
|
|
153
|
+
*
|
|
154
|
+
* After milestone completion + merge, the worktree directory is removed but
|
|
155
|
+
* the process cwd may still point inside `.gsd/worktrees/<MID>/`.
|
|
156
|
+
* When a new session starts, `process.cwd()` is passed as `base` to startAuto
|
|
157
|
+
* and all subsequent writes land in the wrong directory. This function detects
|
|
158
|
+
* that scenario and chdir back to the project root.
|
|
159
|
+
*
|
|
160
|
+
* Returns the corrected base path.
|
|
161
|
+
*/
|
|
162
|
+
export function escapeStaleWorktree(base: string): string {
|
|
163
|
+
const marker = `${pathSep}.gsd${pathSep}worktrees${pathSep}`;
|
|
164
|
+
const idx = base.indexOf(marker);
|
|
165
|
+
if (idx === -1) return base;
|
|
166
|
+
|
|
167
|
+
// base is inside .gsd/worktrees/<something> — extract the project root
|
|
168
|
+
const projectRoot = base.slice(0, idx);
|
|
169
|
+
try {
|
|
170
|
+
process.chdir(projectRoot);
|
|
171
|
+
} catch {
|
|
172
|
+
// If chdir fails, return the original — caller will handle errors downstream
|
|
173
|
+
return base;
|
|
174
|
+
}
|
|
175
|
+
return projectRoot;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Clean stale runtime unit files for completed milestones.
|
|
180
|
+
*
|
|
181
|
+
* After restart, stale runtime/units/*.json from prior milestones can
|
|
182
|
+
* cause deriveState to resume the wrong milestone (#887). Removes files
|
|
183
|
+
* for milestones that have a SUMMARY (fully complete).
|
|
184
|
+
*/
|
|
185
|
+
export function cleanStaleRuntimeUnits(
|
|
186
|
+
gsdRootPath: string,
|
|
187
|
+
hasMilestoneSummary: (mid: string) => boolean,
|
|
188
|
+
): number {
|
|
189
|
+
const runtimeUnitsDir = join(gsdRootPath, "runtime", "units");
|
|
190
|
+
if (!existsSync(runtimeUnitsDir)) return 0;
|
|
191
|
+
|
|
192
|
+
let cleaned = 0;
|
|
193
|
+
try {
|
|
194
|
+
for (const file of readdirSync(runtimeUnitsDir)) {
|
|
195
|
+
if (!file.endsWith(".json")) continue;
|
|
196
|
+
const midMatch = file.match(/(M\d+(?:-[a-z0-9]{6})?)/);
|
|
197
|
+
if (!midMatch) continue;
|
|
198
|
+
if (hasMilestoneSummary(midMatch[1])) {
|
|
199
|
+
try {
|
|
200
|
+
unlinkSync(join(runtimeUnitsDir, file));
|
|
201
|
+
cleaned++;
|
|
202
|
+
} catch { /* non-fatal */ }
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
} catch { /* non-fatal */ }
|
|
206
|
+
return cleaned;
|
|
207
|
+
}
|