gsd-pi 2.45.0-dev.fdcf73c → 2.46.0-dev.cc9d310
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/dist/resources/extensions/gsd/auto/phases.js +14 -35
- package/dist/resources/extensions/gsd/auto/session.js +0 -11
- package/dist/resources/extensions/gsd/auto-artifact-paths.js +112 -0
- package/dist/resources/extensions/gsd/auto-post-unit.js +25 -96
- package/dist/resources/extensions/gsd/auto-start.js +2 -3
- package/dist/resources/extensions/gsd/auto.js +8 -52
- package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +18 -0
- package/dist/resources/extensions/gsd/commands/context.js +0 -4
- package/dist/resources/extensions/gsd/commands/handlers/parallel.js +1 -1
- package/dist/resources/extensions/gsd/crash-recovery.js +2 -4
- package/dist/resources/extensions/gsd/dashboard-overlay.js +0 -44
- package/dist/resources/extensions/gsd/doctor-checks.js +166 -1
- package/dist/resources/extensions/gsd/doctor.js +3 -1
- package/dist/resources/extensions/gsd/gsd-db.js +11 -2
- package/dist/resources/extensions/gsd/guided-flow.js +1 -2
- package/dist/resources/extensions/gsd/parallel-merge.js +1 -1
- package/dist/resources/extensions/gsd/parallel-orchestrator.js +5 -18
- package/dist/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
- package/dist/resources/extensions/gsd/prompts/complete-slice.md +10 -23
- package/dist/resources/extensions/gsd/prompts/discuss.md +2 -2
- package/dist/resources/extensions/gsd/prompts/execute-task.md +5 -15
- package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
- package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
- package/dist/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
- package/dist/resources/extensions/gsd/prompts/guided-research-slice.md +1 -1
- package/dist/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
- package/dist/resources/extensions/gsd/prompts/plan-slice.md +4 -2
- package/dist/resources/extensions/gsd/prompts/queue.md +2 -2
- package/dist/resources/extensions/gsd/prompts/quick-task.md +2 -0
- package/dist/resources/extensions/gsd/prompts/reactive-execute.md +1 -1
- package/dist/resources/extensions/gsd/prompts/research-slice.md +3 -3
- package/dist/resources/extensions/gsd/prompts/rethink.md +7 -2
- package/dist/resources/extensions/gsd/prompts/system.md +1 -1
- package/dist/resources/extensions/gsd/session-lock.js +1 -3
- package/dist/resources/extensions/gsd/state.js +7 -0
- package/dist/resources/extensions/gsd/sync-lock.js +89 -0
- package/dist/resources/extensions/gsd/tools/complete-milestone.js +58 -12
- package/dist/resources/extensions/gsd/tools/complete-slice.js +56 -11
- package/dist/resources/extensions/gsd/tools/complete-task.js +50 -2
- package/dist/resources/extensions/gsd/tools/plan-milestone.js +37 -1
- package/dist/resources/extensions/gsd/tools/plan-slice.js +30 -1
- package/dist/resources/extensions/gsd/tools/plan-task.js +27 -1
- package/dist/resources/extensions/gsd/tools/reassess-roadmap.js +32 -2
- package/dist/resources/extensions/gsd/tools/reopen-slice.js +86 -0
- package/dist/resources/extensions/gsd/tools/reopen-task.js +90 -0
- package/dist/resources/extensions/gsd/tools/replan-slice.js +32 -2
- package/dist/resources/extensions/gsd/unit-ownership.js +85 -0
- package/dist/resources/extensions/gsd/workflow-events.js +102 -0
- package/dist/resources/extensions/gsd/workflow-logger.js +56 -1
- package/dist/resources/extensions/gsd/workflow-manifest.js +244 -0
- package/dist/resources/extensions/gsd/workflow-migration.js +280 -0
- package/dist/resources/extensions/gsd/workflow-projections.js +373 -0
- package/dist/resources/extensions/gsd/workflow-reconcile.js +411 -0
- package/dist/resources/extensions/gsd/write-intercept.js +84 -0
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +17 -17
- package/dist/web/standalone/.next/build-manifest.json +2 -2
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +17 -17
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +2 -2
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/package.json +1 -1
- package/packages/pi-coding-agent/package.json +1 -1
- package/pkg/package.json +1 -1
- package/src/resources/extensions/gsd/auto/loop-deps.ts +0 -19
- package/src/resources/extensions/gsd/auto/phases.ts +11 -35
- package/src/resources/extensions/gsd/auto/session.ts +0 -18
- package/src/resources/extensions/gsd/auto-artifact-paths.ts +131 -0
- package/src/resources/extensions/gsd/auto-dashboard.ts +0 -1
- package/src/resources/extensions/gsd/auto-post-unit.ts +25 -106
- package/src/resources/extensions/gsd/auto-start.ts +1 -3
- package/src/resources/extensions/gsd/auto.ts +4 -80
- package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +22 -0
- package/src/resources/extensions/gsd/commands/context.ts +0 -5
- package/src/resources/extensions/gsd/commands/handlers/parallel.ts +1 -1
- package/src/resources/extensions/gsd/crash-recovery.ts +1 -5
- package/src/resources/extensions/gsd/dashboard-overlay.ts +0 -50
- package/src/resources/extensions/gsd/doctor-checks.ts +179 -1
- package/src/resources/extensions/gsd/doctor-types.ts +7 -1
- package/src/resources/extensions/gsd/doctor.ts +4 -1
- package/src/resources/extensions/gsd/gsd-db.ts +11 -2
- package/src/resources/extensions/gsd/guided-flow.ts +1 -2
- package/src/resources/extensions/gsd/parallel-merge.ts +1 -1
- package/src/resources/extensions/gsd/parallel-orchestrator.ts +5 -21
- package/src/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
- package/src/resources/extensions/gsd/prompts/complete-slice.md +10 -23
- package/src/resources/extensions/gsd/prompts/discuss.md +2 -2
- package/src/resources/extensions/gsd/prompts/execute-task.md +5 -15
- package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
- package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
- package/src/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
- package/src/resources/extensions/gsd/prompts/guided-research-slice.md +1 -1
- package/src/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
- package/src/resources/extensions/gsd/prompts/plan-slice.md +4 -2
- package/src/resources/extensions/gsd/prompts/queue.md +2 -2
- package/src/resources/extensions/gsd/prompts/quick-task.md +2 -0
- package/src/resources/extensions/gsd/prompts/reactive-execute.md +1 -1
- package/src/resources/extensions/gsd/prompts/research-slice.md +3 -3
- package/src/resources/extensions/gsd/prompts/rethink.md +7 -2
- package/src/resources/extensions/gsd/prompts/system.md +1 -1
- package/src/resources/extensions/gsd/session-lock.ts +0 -4
- package/src/resources/extensions/gsd/state.ts +8 -0
- package/src/resources/extensions/gsd/sync-lock.ts +94 -0
- package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +5 -13
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +6 -10
- package/src/resources/extensions/gsd/tests/complete-slice.test.ts +264 -228
- package/src/resources/extensions/gsd/tests/complete-task.test.ts +317 -250
- package/src/resources/extensions/gsd/tests/crash-recovery.test.ts +2 -8
- package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +0 -3
- package/src/resources/extensions/gsd/tests/gsd-db.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/idle-recovery.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/integration-proof.test.ts +15 -24
- package/src/resources/extensions/gsd/tests/journal-integration.test.ts +0 -3
- package/src/resources/extensions/gsd/tests/md-importer.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/memory-store.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/milestone-transition-state-rebuild.test.ts +8 -9
- package/src/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +0 -1
- package/src/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +0 -7
- package/src/resources/extensions/gsd/tests/parallel-merge.test.ts +7 -8
- package/src/resources/extensions/gsd/tests/parallel-orchestration.test.ts +20 -24
- package/src/resources/extensions/gsd/tests/parallel-worker-monitoring.test.ts +0 -2
- package/src/resources/extensions/gsd/tests/plan-milestone.test.ts +9 -6
- package/src/resources/extensions/gsd/tests/post-mutation-hook.test.ts +171 -0
- package/src/resources/extensions/gsd/tests/projection-regression.test.ts +174 -0
- package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +15 -14
- package/src/resources/extensions/gsd/tests/reopen-slice.test.ts +155 -0
- package/src/resources/extensions/gsd/tests/reopen-task.test.ts +165 -0
- package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +1 -4
- package/src/resources/extensions/gsd/tests/stop-auto-remote.test.ts +2 -3
- package/src/resources/extensions/gsd/tests/sync-lock.test.ts +122 -0
- package/src/resources/extensions/gsd/tests/unit-ownership.test.ts +175 -0
- package/src/resources/extensions/gsd/tests/workflow-events.test.ts +205 -0
- package/src/resources/extensions/gsd/tests/workflow-manifest.test.ts +186 -0
- package/src/resources/extensions/gsd/tests/workflow-projections.test.ts +171 -0
- package/src/resources/extensions/gsd/tests/write-intercept.test.ts +76 -0
- package/src/resources/extensions/gsd/tools/complete-milestone.ts +70 -13
- package/src/resources/extensions/gsd/tools/complete-slice.ts +68 -11
- package/src/resources/extensions/gsd/tools/complete-task.ts +63 -1
- package/src/resources/extensions/gsd/tools/plan-milestone.ts +45 -0
- package/src/resources/extensions/gsd/tools/plan-slice.ts +38 -0
- package/src/resources/extensions/gsd/tools/plan-task.ts +35 -1
- package/src/resources/extensions/gsd/tools/reassess-roadmap.ts +39 -1
- package/src/resources/extensions/gsd/tools/reopen-slice.ts +125 -0
- package/src/resources/extensions/gsd/tools/reopen-task.ts +129 -0
- package/src/resources/extensions/gsd/tools/replan-slice.ts +38 -1
- package/src/resources/extensions/gsd/types.ts +8 -0
- package/src/resources/extensions/gsd/unit-ownership.ts +104 -0
- package/src/resources/extensions/gsd/workflow-events.ts +154 -0
- package/src/resources/extensions/gsd/workflow-logger.ts +51 -1
- package/src/resources/extensions/gsd/workflow-manifest.ts +334 -0
- package/src/resources/extensions/gsd/workflow-migration.ts +345 -0
- package/src/resources/extensions/gsd/workflow-projections.ts +425 -0
- package/src/resources/extensions/gsd/workflow-reconcile.ts +503 -0
- package/src/resources/extensions/gsd/write-intercept.ts +90 -0
- /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → ZIDqryyYDroh_8AnaAOSG}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → ZIDqryyYDroh_8AnaAOSG}/_ssgManifest.js +0 -0
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { clearParseCache } from "../files.js";
|
|
2
|
-
import { transaction, getSlice, insertTask, upsertSlicePlanning, upsertTaskPlanning, } from "../gsd-db.js";
|
|
2
|
+
import { transaction, getMilestone, getSlice, insertTask, upsertSlicePlanning, upsertTaskPlanning, } from "../gsd-db.js";
|
|
3
3
|
import { invalidateStateCache } from "../state.js";
|
|
4
4
|
import { renderPlanFromDb } from "../markdown-renderer.js";
|
|
5
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
6
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
7
|
+
import { appendEvent } from "../workflow-events.js";
|
|
5
8
|
function isNonEmptyString(value) {
|
|
6
9
|
return typeof value === "string" && value.trim().length > 0;
|
|
7
10
|
}
|
|
@@ -99,10 +102,20 @@ export async function handlePlanSlice(rawParams, basePath) {
|
|
|
99
102
|
catch (err) {
|
|
100
103
|
return { error: `validation failed: ${err.message}` };
|
|
101
104
|
}
|
|
105
|
+
const parentMilestone = getMilestone(params.milestoneId);
|
|
106
|
+
if (!parentMilestone) {
|
|
107
|
+
return { error: `milestone not found: ${params.milestoneId}` };
|
|
108
|
+
}
|
|
109
|
+
if (parentMilestone.status === "complete" || parentMilestone.status === "done") {
|
|
110
|
+
return { error: `cannot plan slice in a closed milestone: ${params.milestoneId} (status: ${parentMilestone.status})` };
|
|
111
|
+
}
|
|
102
112
|
const parentSlice = getSlice(params.milestoneId, params.sliceId);
|
|
103
113
|
if (!parentSlice) {
|
|
104
114
|
return { error: `missing parent slice: ${params.milestoneId}/${params.sliceId}` };
|
|
105
115
|
}
|
|
116
|
+
if (parentSlice.status === "complete" || parentSlice.status === "done") {
|
|
117
|
+
return { error: `cannot re-plan slice ${params.sliceId}: it is already complete — use gsd_slice_reopen first` };
|
|
118
|
+
}
|
|
106
119
|
try {
|
|
107
120
|
transaction(() => {
|
|
108
121
|
upsertSlicePlanning(params.milestoneId, params.sliceId, {
|
|
@@ -141,6 +154,22 @@ export async function handlePlanSlice(rawParams, basePath) {
|
|
|
141
154
|
const renderResult = await renderPlanFromDb(basePath, params.milestoneId, params.sliceId);
|
|
142
155
|
invalidateStateCache();
|
|
143
156
|
clearParseCache();
|
|
157
|
+
// ── Post-mutation hook: projections, manifest, event log ─────────────
|
|
158
|
+
try {
|
|
159
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
160
|
+
writeManifest(basePath);
|
|
161
|
+
appendEvent(basePath, {
|
|
162
|
+
cmd: "plan-slice",
|
|
163
|
+
params: { milestoneId: params.milestoneId, sliceId: params.sliceId },
|
|
164
|
+
ts: new Date().toISOString(),
|
|
165
|
+
actor: "agent",
|
|
166
|
+
actor_name: params.actorName,
|
|
167
|
+
trigger_reason: params.triggerReason,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (hookErr) {
|
|
171
|
+
process.stderr.write(`gsd: plan-slice post-mutation hook warning: ${hookErr.message}\n`);
|
|
172
|
+
}
|
|
144
173
|
return {
|
|
145
174
|
milestoneId: params.milestoneId,
|
|
146
175
|
sliceId: params.sliceId,
|
|
@@ -2,6 +2,9 @@ import { clearParseCache } from "../files.js";
|
|
|
2
2
|
import { transaction, getSlice, getTask, insertTask, upsertTaskPlanning } from "../gsd-db.js";
|
|
3
3
|
import { invalidateStateCache } from "../state.js";
|
|
4
4
|
import { renderTaskPlanFromDb } from "../markdown-renderer.js";
|
|
5
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
6
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
7
|
+
import { appendEvent } from "../workflow-events.js";
|
|
5
8
|
function isNonEmptyString(value) {
|
|
6
9
|
return typeof value === "string" && value.trim().length > 0;
|
|
7
10
|
}
|
|
@@ -51,9 +54,16 @@ export async function handlePlanTask(rawParams, basePath) {
|
|
|
51
54
|
if (!parentSlice) {
|
|
52
55
|
return { error: `missing parent slice: ${params.milestoneId}/${params.sliceId}` };
|
|
53
56
|
}
|
|
57
|
+
if (parentSlice.status === "complete" || parentSlice.status === "done") {
|
|
58
|
+
return { error: `cannot plan task in a closed slice: ${params.sliceId} (status: ${parentSlice.status})` };
|
|
59
|
+
}
|
|
60
|
+
const existingTask = getTask(params.milestoneId, params.sliceId, params.taskId);
|
|
61
|
+
if (existingTask && (existingTask.status === "complete" || existingTask.status === "done")) {
|
|
62
|
+
return { error: `cannot re-plan task ${params.taskId}: it is already complete — use gsd_task_reopen first` };
|
|
63
|
+
}
|
|
54
64
|
try {
|
|
55
65
|
transaction(() => {
|
|
56
|
-
if (!
|
|
66
|
+
if (!existingTask) {
|
|
57
67
|
insertTask({
|
|
58
68
|
id: params.taskId,
|
|
59
69
|
sliceId: params.sliceId,
|
|
@@ -82,6 +92,22 @@ export async function handlePlanTask(rawParams, basePath) {
|
|
|
82
92
|
const renderResult = await renderTaskPlanFromDb(basePath, params.milestoneId, params.sliceId, params.taskId);
|
|
83
93
|
invalidateStateCache();
|
|
84
94
|
clearParseCache();
|
|
95
|
+
// ── Post-mutation hook: projections, manifest, event log ─────────────
|
|
96
|
+
try {
|
|
97
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
98
|
+
writeManifest(basePath);
|
|
99
|
+
appendEvent(basePath, {
|
|
100
|
+
cmd: "plan-task",
|
|
101
|
+
params: { milestoneId: params.milestoneId, sliceId: params.sliceId, taskId: params.taskId },
|
|
102
|
+
ts: new Date().toISOString(),
|
|
103
|
+
actor: "agent",
|
|
104
|
+
actor_name: params.actorName,
|
|
105
|
+
trigger_reason: params.triggerReason,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (hookErr) {
|
|
109
|
+
process.stderr.write(`gsd: plan-task post-mutation hook warning: ${hookErr.message}\n`);
|
|
110
|
+
}
|
|
85
111
|
return {
|
|
86
112
|
milestoneId: params.milestoneId,
|
|
87
113
|
sliceId: params.sliceId,
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { clearParseCache } from "../files.js";
|
|
2
|
-
import { transaction, getMilestone, getMilestoneSlices, insertSlice, updateSliceFields, insertAssessment, deleteSlice, } from "../gsd-db.js";
|
|
2
|
+
import { transaction, getMilestone, getMilestoneSlices, getSlice, insertSlice, updateSliceFields, insertAssessment, deleteSlice, } from "../gsd-db.js";
|
|
3
3
|
import { invalidateStateCache } from "../state.js";
|
|
4
4
|
import { renderRoadmapFromDb, renderAssessmentFromDb } from "../markdown-renderer.js";
|
|
5
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
6
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
7
|
+
import { appendEvent } from "../workflow-events.js";
|
|
5
8
|
import { join } from "node:path";
|
|
6
9
|
function isNonEmptyString(value) {
|
|
7
10
|
return typeof value === "string" && value.trim().length > 0;
|
|
@@ -58,11 +61,22 @@ export async function handleReassessRoadmap(rawParams, basePath) {
|
|
|
58
61
|
catch (err) {
|
|
59
62
|
return { error: `validation failed: ${err.message}` };
|
|
60
63
|
}
|
|
61
|
-
// ── Verify milestone exists
|
|
64
|
+
// ── Verify milestone exists and is active ────────────────────────
|
|
62
65
|
const milestone = getMilestone(params.milestoneId);
|
|
63
66
|
if (!milestone) {
|
|
64
67
|
return { error: `milestone not found: ${params.milestoneId}` };
|
|
65
68
|
}
|
|
69
|
+
if (milestone.status === "complete" || milestone.status === "done") {
|
|
70
|
+
return { error: `cannot reassess a closed milestone: ${params.milestoneId} (status: ${milestone.status})` };
|
|
71
|
+
}
|
|
72
|
+
// ── Verify completedSliceId is actually complete ──────────────────
|
|
73
|
+
const completedSlice = getSlice(params.milestoneId, params.completedSliceId);
|
|
74
|
+
if (!completedSlice) {
|
|
75
|
+
return { error: `completedSliceId not found: ${params.milestoneId}/${params.completedSliceId}` };
|
|
76
|
+
}
|
|
77
|
+
if (completedSlice.status !== "complete" && completedSlice.status !== "done") {
|
|
78
|
+
return { error: `completedSliceId ${params.completedSliceId} is not complete (status: ${completedSlice.status}) — reassess can only be called after a slice finishes` };
|
|
79
|
+
}
|
|
66
80
|
// ── Structural enforcement ────────────────────────────────────────
|
|
67
81
|
const existingSlices = getMilestoneSlices(params.milestoneId);
|
|
68
82
|
const completedSliceIds = new Set();
|
|
@@ -139,6 +153,22 @@ export async function handleReassessRoadmap(rawParams, basePath) {
|
|
|
139
153
|
// ── Invalidate caches ─────────────────────────────────────────
|
|
140
154
|
invalidateStateCache();
|
|
141
155
|
clearParseCache();
|
|
156
|
+
// ── Post-mutation hook: projections, manifest, event log ─────
|
|
157
|
+
try {
|
|
158
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
159
|
+
writeManifest(basePath);
|
|
160
|
+
appendEvent(basePath, {
|
|
161
|
+
cmd: "reassess-roadmap",
|
|
162
|
+
params: { milestoneId: params.milestoneId, completedSliceId: params.completedSliceId },
|
|
163
|
+
ts: new Date().toISOString(),
|
|
164
|
+
actor: "agent",
|
|
165
|
+
actor_name: params.actorName,
|
|
166
|
+
trigger_reason: params.triggerReason,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (hookErr) {
|
|
170
|
+
process.stderr.write(`gsd: reassess-roadmap post-mutation hook warning: ${hookErr.message}\n`);
|
|
171
|
+
}
|
|
142
172
|
return {
|
|
143
173
|
milestoneId: params.milestoneId,
|
|
144
174
|
completedSliceId: params.completedSliceId,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* reopen-slice handler — the core operation behind gsd_slice_reopen.
|
|
3
|
+
*
|
|
4
|
+
* Resets a completed slice back to "in_progress" and resets ALL of its
|
|
5
|
+
* tasks back to "pending". This is intentional — if you're reopening a
|
|
6
|
+
* slice, you're re-doing the work. Partial resets create ambiguous state.
|
|
7
|
+
*
|
|
8
|
+
* The parent milestone must still be open (not complete).
|
|
9
|
+
*/
|
|
10
|
+
// GSD — reopen-slice tool handler
|
|
11
|
+
// Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
|
|
12
|
+
import { getMilestone, getSlice, getSliceTasks, updateSliceStatus, updateTaskStatus, transaction, } from "../gsd-db.js";
|
|
13
|
+
import { invalidateStateCache } from "../state.js";
|
|
14
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
15
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
16
|
+
import { appendEvent } from "../workflow-events.js";
|
|
17
|
+
export async function handleReopenSlice(params, basePath) {
|
|
18
|
+
// ── Validate required fields ────────────────────────────────────────────
|
|
19
|
+
if (!params.sliceId || typeof params.sliceId !== "string" || params.sliceId.trim() === "") {
|
|
20
|
+
return { error: "sliceId is required and must be a non-empty string" };
|
|
21
|
+
}
|
|
22
|
+
if (!params.milestoneId || typeof params.milestoneId !== "string" || params.milestoneId.trim() === "") {
|
|
23
|
+
return { error: "milestoneId is required and must be a non-empty string" };
|
|
24
|
+
}
|
|
25
|
+
// ── Guards + DB writes inside a single transaction (prevents TOCTOU) ───
|
|
26
|
+
let guardError = null;
|
|
27
|
+
let tasksResetCount = 0;
|
|
28
|
+
transaction(() => {
|
|
29
|
+
const milestone = getMilestone(params.milestoneId);
|
|
30
|
+
if (!milestone) {
|
|
31
|
+
guardError = `milestone not found: ${params.milestoneId}`;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (milestone.status === "complete" || milestone.status === "done") {
|
|
35
|
+
guardError = `cannot reopen slice inside a closed milestone: ${params.milestoneId} (status: ${milestone.status})`;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const slice = getSlice(params.milestoneId, params.sliceId);
|
|
39
|
+
if (!slice) {
|
|
40
|
+
guardError = `slice not found: ${params.milestoneId}/${params.sliceId}`;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (slice.status !== "complete" && slice.status !== "done") {
|
|
44
|
+
guardError = `slice ${params.sliceId} is not complete (status: ${slice.status}) — nothing to reopen`;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// Fetch tasks inside txn so the list is consistent with the slice status check
|
|
48
|
+
const tasks = getSliceTasks(params.milestoneId, params.sliceId);
|
|
49
|
+
tasksResetCount = tasks.length;
|
|
50
|
+
updateSliceStatus(params.milestoneId, params.sliceId, "in_progress");
|
|
51
|
+
for (const task of tasks) {
|
|
52
|
+
updateTaskStatus(params.milestoneId, params.sliceId, task.id, "pending");
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if (guardError) {
|
|
56
|
+
return { error: guardError };
|
|
57
|
+
}
|
|
58
|
+
// ── Invalidate caches ────────────────────────────────────────────────────
|
|
59
|
+
invalidateStateCache();
|
|
60
|
+
// ── Post-mutation hook ───────────────────────────────────────────────────
|
|
61
|
+
try {
|
|
62
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
63
|
+
writeManifest(basePath);
|
|
64
|
+
appendEvent(basePath, {
|
|
65
|
+
cmd: "reopen-slice",
|
|
66
|
+
params: {
|
|
67
|
+
milestoneId: params.milestoneId,
|
|
68
|
+
sliceId: params.sliceId,
|
|
69
|
+
reason: params.reason ?? null,
|
|
70
|
+
tasksReset: tasksResetCount,
|
|
71
|
+
},
|
|
72
|
+
ts: new Date().toISOString(),
|
|
73
|
+
actor: "agent",
|
|
74
|
+
actor_name: params.actorName,
|
|
75
|
+
trigger_reason: params.triggerReason,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (hookErr) {
|
|
79
|
+
process.stderr.write(`gsd: reopen-slice post-mutation hook warning: ${hookErr.message}\n`);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
milestoneId: params.milestoneId,
|
|
83
|
+
sliceId: params.sliceId,
|
|
84
|
+
tasksReset: tasksResetCount,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* reopen-task handler — the core operation behind gsd_task_reopen.
|
|
3
|
+
*
|
|
4
|
+
* Resets a completed task back to "pending" so it can be re-done
|
|
5
|
+
* without manual SQL surgery. The parent slice and milestone must
|
|
6
|
+
* still be open (not complete) — you cannot reopen tasks inside a
|
|
7
|
+
* closed slice.
|
|
8
|
+
*/
|
|
9
|
+
// GSD — reopen-task tool handler
|
|
10
|
+
// Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
|
|
11
|
+
import { getMilestone, getSlice, getTask, updateTaskStatus, transaction, } from "../gsd-db.js";
|
|
12
|
+
import { invalidateStateCache } from "../state.js";
|
|
13
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
14
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
15
|
+
import { appendEvent } from "../workflow-events.js";
|
|
16
|
+
export async function handleReopenTask(params, basePath) {
|
|
17
|
+
// ── Validate required fields ────────────────────────────────────────────
|
|
18
|
+
if (!params.taskId || typeof params.taskId !== "string" || params.taskId.trim() === "") {
|
|
19
|
+
return { error: "taskId is required and must be a non-empty string" };
|
|
20
|
+
}
|
|
21
|
+
if (!params.sliceId || typeof params.sliceId !== "string" || params.sliceId.trim() === "") {
|
|
22
|
+
return { error: "sliceId is required and must be a non-empty string" };
|
|
23
|
+
}
|
|
24
|
+
if (!params.milestoneId || typeof params.milestoneId !== "string" || params.milestoneId.trim() === "") {
|
|
25
|
+
return { error: "milestoneId is required and must be a non-empty string" };
|
|
26
|
+
}
|
|
27
|
+
// ── Guards + DB write inside a single transaction (prevents TOCTOU) ────
|
|
28
|
+
let guardError = null;
|
|
29
|
+
transaction(() => {
|
|
30
|
+
const milestone = getMilestone(params.milestoneId);
|
|
31
|
+
if (!milestone) {
|
|
32
|
+
guardError = `milestone not found: ${params.milestoneId}`;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (milestone.status === "complete" || milestone.status === "done") {
|
|
36
|
+
guardError = `cannot reopen task in a closed milestone: ${params.milestoneId} (status: ${milestone.status})`;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const slice = getSlice(params.milestoneId, params.sliceId);
|
|
40
|
+
if (!slice) {
|
|
41
|
+
guardError = `slice not found: ${params.milestoneId}/${params.sliceId}`;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (slice.status === "complete" || slice.status === "done") {
|
|
45
|
+
guardError = `cannot reopen task inside a closed slice: ${params.sliceId} (status: ${slice.status}) — use gsd_slice_reopen first`;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const task = getTask(params.milestoneId, params.sliceId, params.taskId);
|
|
49
|
+
if (!task) {
|
|
50
|
+
guardError = `task not found: ${params.milestoneId}/${params.sliceId}/${params.taskId}`;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (task.status !== "complete" && task.status !== "done") {
|
|
54
|
+
guardError = `task ${params.taskId} is not complete (status: ${task.status}) — nothing to reopen`;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
updateTaskStatus(params.milestoneId, params.sliceId, params.taskId, "pending");
|
|
58
|
+
});
|
|
59
|
+
if (guardError) {
|
|
60
|
+
return { error: guardError };
|
|
61
|
+
}
|
|
62
|
+
// ── Invalidate caches ────────────────────────────────────────────────────
|
|
63
|
+
invalidateStateCache();
|
|
64
|
+
// ── Post-mutation hook ───────────────────────────────────────────────────
|
|
65
|
+
try {
|
|
66
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
67
|
+
writeManifest(basePath);
|
|
68
|
+
appendEvent(basePath, {
|
|
69
|
+
cmd: "reopen-task",
|
|
70
|
+
params: {
|
|
71
|
+
milestoneId: params.milestoneId,
|
|
72
|
+
sliceId: params.sliceId,
|
|
73
|
+
taskId: params.taskId,
|
|
74
|
+
reason: params.reason ?? null,
|
|
75
|
+
},
|
|
76
|
+
ts: new Date().toISOString(),
|
|
77
|
+
actor: "agent",
|
|
78
|
+
actor_name: params.actorName,
|
|
79
|
+
trigger_reason: params.triggerReason,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (hookErr) {
|
|
83
|
+
process.stderr.write(`gsd: reopen-task post-mutation hook warning: ${hookErr.message}\n`);
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
milestoneId: params.milestoneId,
|
|
87
|
+
sliceId: params.sliceId,
|
|
88
|
+
taskId: params.taskId,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { clearParseCache } from "../files.js";
|
|
2
|
-
import { transaction, getSlice, getSliceTasks, insertTask, upsertTaskPlanning, insertReplanHistory, deleteTask, } from "../gsd-db.js";
|
|
2
|
+
import { transaction, getSlice, getSliceTasks, getTask, insertTask, upsertTaskPlanning, insertReplanHistory, deleteTask, } from "../gsd-db.js";
|
|
3
3
|
import { invalidateStateCache } from "../state.js";
|
|
4
4
|
import { renderPlanFromDb, renderReplanFromDb } from "../markdown-renderer.js";
|
|
5
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
6
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
7
|
+
import { appendEvent } from "../workflow-events.js";
|
|
5
8
|
function isNonEmptyString(value) {
|
|
6
9
|
return typeof value === "string" && value.trim().length > 0;
|
|
7
10
|
}
|
|
@@ -43,11 +46,22 @@ export async function handleReplanSlice(rawParams, basePath) {
|
|
|
43
46
|
catch (err) {
|
|
44
47
|
return { error: `validation failed: ${err.message}` };
|
|
45
48
|
}
|
|
46
|
-
// ── Verify parent slice exists
|
|
49
|
+
// ── Verify parent slice exists and is not closed ─────────────────
|
|
47
50
|
const parentSlice = getSlice(params.milestoneId, params.sliceId);
|
|
48
51
|
if (!parentSlice) {
|
|
49
52
|
return { error: `missing parent slice: ${params.milestoneId}/${params.sliceId}` };
|
|
50
53
|
}
|
|
54
|
+
if (parentSlice.status === "complete" || parentSlice.status === "done") {
|
|
55
|
+
return { error: `cannot replan a closed slice: ${params.sliceId} (status: ${parentSlice.status})` };
|
|
56
|
+
}
|
|
57
|
+
// ── Verify blocker task exists and is complete ────────────────────
|
|
58
|
+
const blockerTask = getTask(params.milestoneId, params.sliceId, params.blockerTaskId);
|
|
59
|
+
if (!blockerTask) {
|
|
60
|
+
return { error: `blockerTaskId not found: ${params.milestoneId}/${params.sliceId}/${params.blockerTaskId}` };
|
|
61
|
+
}
|
|
62
|
+
if (blockerTask.status !== "complete" && blockerTask.status !== "done") {
|
|
63
|
+
return { error: `blockerTaskId ${params.blockerTaskId} is not complete (status: ${blockerTask.status}) — the blocker task must be finished before a replan is triggered` };
|
|
64
|
+
}
|
|
51
65
|
// ── Structural enforcement ────────────────────────────────────────
|
|
52
66
|
const existingTasks = getSliceTasks(params.milestoneId, params.sliceId);
|
|
53
67
|
const completedTaskIds = new Set();
|
|
@@ -135,6 +149,22 @@ export async function handleReplanSlice(rawParams, basePath) {
|
|
|
135
149
|
// ── Invalidate caches ─────────────────────────────────────────
|
|
136
150
|
invalidateStateCache();
|
|
137
151
|
clearParseCache();
|
|
152
|
+
// ── Post-mutation hook: projections, manifest, event log ─────
|
|
153
|
+
try {
|
|
154
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
155
|
+
writeManifest(basePath);
|
|
156
|
+
appendEvent(basePath, {
|
|
157
|
+
cmd: "replan-slice",
|
|
158
|
+
params: { milestoneId: params.milestoneId, sliceId: params.sliceId, blockerTaskId: params.blockerTaskId },
|
|
159
|
+
ts: new Date().toISOString(),
|
|
160
|
+
actor: "agent",
|
|
161
|
+
actor_name: params.actorName,
|
|
162
|
+
trigger_reason: params.triggerReason,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
catch (hookErr) {
|
|
166
|
+
process.stderr.write(`gsd: replan-slice post-mutation hook warning: ${hookErr.message}\n`);
|
|
167
|
+
}
|
|
138
168
|
return {
|
|
139
169
|
milestoneId: params.milestoneId,
|
|
140
170
|
sliceId: params.sliceId,
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// GSD Extension — Unit Ownership
|
|
2
|
+
// Opt-in per-unit ownership claims for multi-agent safety.
|
|
3
|
+
//
|
|
4
|
+
// An agent can claim a unit (task, slice) before working on it.
|
|
5
|
+
// complete-task and complete-slice enforce ownership when claims exist.
|
|
6
|
+
// If no claim file is present, ownership is not enforced (backward compatible).
|
|
7
|
+
//
|
|
8
|
+
// Claim file location: .gsd/unit-claims.json
|
|
9
|
+
// Unit key format:
|
|
10
|
+
// task: "<milestoneId>/<sliceId>/<taskId>"
|
|
11
|
+
// slice: "<milestoneId>/<sliceId>"
|
|
12
|
+
//
|
|
13
|
+
// Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
|
|
14
|
+
import { existsSync, readFileSync, mkdirSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { atomicWriteSync } from "./atomic-write.js";
|
|
17
|
+
// ─── Key Builders ────────────────────────────────────────────────────────
|
|
18
|
+
export function taskUnitKey(milestoneId, sliceId, taskId) {
|
|
19
|
+
return `${milestoneId}/${sliceId}/${taskId}`;
|
|
20
|
+
}
|
|
21
|
+
export function sliceUnitKey(milestoneId, sliceId) {
|
|
22
|
+
return `${milestoneId}/${sliceId}`;
|
|
23
|
+
}
|
|
24
|
+
// ─── File Path ───────────────────────────────────────────────────────────
|
|
25
|
+
function claimsPath(basePath) {
|
|
26
|
+
return join(basePath, ".gsd", "unit-claims.json");
|
|
27
|
+
}
|
|
28
|
+
// ─── Read Claims ─────────────────────────────────────────────────────────
|
|
29
|
+
function readClaims(basePath) {
|
|
30
|
+
const path = claimsPath(basePath);
|
|
31
|
+
if (!existsSync(path))
|
|
32
|
+
return null;
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// ─── Public API ──────────────────────────────────────────────────────────
|
|
41
|
+
/**
|
|
42
|
+
* Claim a unit for an agent.
|
|
43
|
+
* Overwrites any existing claim for this unit (last writer wins).
|
|
44
|
+
*/
|
|
45
|
+
export function claimUnit(basePath, unitKey, agentName) {
|
|
46
|
+
const claims = readClaims(basePath) ?? {};
|
|
47
|
+
claims[unitKey] = { agent: agentName, claimed_at: new Date().toISOString() };
|
|
48
|
+
const dir = join(basePath, ".gsd");
|
|
49
|
+
mkdirSync(dir, { recursive: true });
|
|
50
|
+
atomicWriteSync(claimsPath(basePath), JSON.stringify(claims, null, 2) + "\n");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Release a unit claim (remove it from the claims map).
|
|
54
|
+
*/
|
|
55
|
+
export function releaseUnit(basePath, unitKey) {
|
|
56
|
+
const claims = readClaims(basePath);
|
|
57
|
+
if (!claims || !(unitKey in claims))
|
|
58
|
+
return;
|
|
59
|
+
delete claims[unitKey];
|
|
60
|
+
atomicWriteSync(claimsPath(basePath), JSON.stringify(claims, null, 2) + "\n");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the current owner of a unit, or null if unclaimed / no claims file.
|
|
64
|
+
*/
|
|
65
|
+
export function getOwner(basePath, unitKey) {
|
|
66
|
+
const claims = readClaims(basePath);
|
|
67
|
+
if (!claims)
|
|
68
|
+
return null;
|
|
69
|
+
return claims[unitKey]?.agent ?? null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if an actor is authorized to operate on a unit.
|
|
73
|
+
* Returns null if ownership passes (or is unclaimed / no file).
|
|
74
|
+
* Returns an error string if a different agent owns the unit.
|
|
75
|
+
*/
|
|
76
|
+
export function checkOwnership(basePath, unitKey, actorName) {
|
|
77
|
+
if (!actorName)
|
|
78
|
+
return null; // no actor identity provided — opt-in, so allow
|
|
79
|
+
const owner = getOwner(basePath, unitKey);
|
|
80
|
+
if (owner === null)
|
|
81
|
+
return null; // unit unclaimed or no claims file
|
|
82
|
+
if (owner === actorName)
|
|
83
|
+
return null; // actor is the owner
|
|
84
|
+
return `Unit ${unitKey} is owned by ${owner}, not ${actorName}`;
|
|
85
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { appendFileSync, readFileSync, existsSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { atomicWriteSync } from "./atomic-write.js";
|
|
5
|
+
// ─── Session ID ───────────────────────────────────────────────────────────
|
|
6
|
+
/**
|
|
7
|
+
* Engine-generated session ID — stable for the lifetime of this process.
|
|
8
|
+
* Agents can reference this to correlate all events from one run.
|
|
9
|
+
*/
|
|
10
|
+
const ENGINE_SESSION_ID = randomUUID();
|
|
11
|
+
export function getSessionId() {
|
|
12
|
+
return ENGINE_SESSION_ID;
|
|
13
|
+
}
|
|
14
|
+
// ─── appendEvent ─────────────────────────────────────────────────────────
|
|
15
|
+
/**
|
|
16
|
+
* Append one event to .gsd/event-log.jsonl.
|
|
17
|
+
* Computes a content hash from cmd+params (deterministic, independent of ts/actor/session).
|
|
18
|
+
* Creates .gsd directory if needed.
|
|
19
|
+
*/
|
|
20
|
+
export function appendEvent(basePath, event) {
|
|
21
|
+
const hash = createHash("sha256")
|
|
22
|
+
.update(JSON.stringify({ cmd: event.cmd, params: event.params, ts: event.ts }))
|
|
23
|
+
.digest("hex")
|
|
24
|
+
.slice(0, 16);
|
|
25
|
+
const fullEvent = {
|
|
26
|
+
...event,
|
|
27
|
+
hash,
|
|
28
|
+
session_id: ENGINE_SESSION_ID,
|
|
29
|
+
};
|
|
30
|
+
const dir = join(basePath, ".gsd");
|
|
31
|
+
mkdirSync(dir, { recursive: true });
|
|
32
|
+
appendFileSync(join(dir, "event-log.jsonl"), JSON.stringify(fullEvent) + "\n", "utf-8");
|
|
33
|
+
}
|
|
34
|
+
// ─── readEvents ──────────────────────────────────────────────────────────
|
|
35
|
+
/**
|
|
36
|
+
* Read all events from a JSONL file.
|
|
37
|
+
* Returns empty array if file doesn't exist.
|
|
38
|
+
* Corrupted lines are skipped with stderr warning.
|
|
39
|
+
*/
|
|
40
|
+
export function readEvents(logPath) {
|
|
41
|
+
if (!existsSync(logPath)) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const content = readFileSync(logPath, "utf-8");
|
|
45
|
+
const lines = content.split("\n").filter((l) => l.length > 0);
|
|
46
|
+
const events = [];
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
try {
|
|
49
|
+
events.push(JSON.parse(line));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
process.stderr.write(`workflow-events: skipping corrupted event line: ${line.slice(0, 80)}\n`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return events;
|
|
56
|
+
}
|
|
57
|
+
// ─── findForkPoint ───────────────────────────────────────────────────────
|
|
58
|
+
/**
|
|
59
|
+
* Find the index of the last common event between two logs by comparing hashes.
|
|
60
|
+
* Returns -1 if the first events differ (completely diverged).
|
|
61
|
+
* If one log is a prefix of the other, returns length of shorter - 1.
|
|
62
|
+
*/
|
|
63
|
+
export function findForkPoint(logA, logB) {
|
|
64
|
+
const minLen = Math.min(logA.length, logB.length);
|
|
65
|
+
let lastCommon = -1;
|
|
66
|
+
for (let i = 0; i < minLen; i++) {
|
|
67
|
+
if (logA[i].hash === logB[i].hash) {
|
|
68
|
+
lastCommon = i;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return lastCommon;
|
|
75
|
+
}
|
|
76
|
+
// ─── compactMilestoneEvents ─────────────────────────────────────────────────
|
|
77
|
+
/**
|
|
78
|
+
* Archive a milestone's events from the active log to a separate file.
|
|
79
|
+
* Active log retains only events from other milestones.
|
|
80
|
+
* Archived file is kept on disk for forensics.
|
|
81
|
+
*
|
|
82
|
+
* @param basePath - Project root (parent of .gsd/)
|
|
83
|
+
* @param milestoneId - The milestone whose events should be archived
|
|
84
|
+
* @returns { archived: number } — count of events moved to archive
|
|
85
|
+
*/
|
|
86
|
+
export function compactMilestoneEvents(basePath, milestoneId) {
|
|
87
|
+
const logPath = join(basePath, ".gsd", "event-log.jsonl");
|
|
88
|
+
const archivePath = join(basePath, ".gsd", `event-log-${milestoneId}.jsonl.archived`);
|
|
89
|
+
const allEvents = readEvents(logPath);
|
|
90
|
+
const toArchive = allEvents.filter((e) => e.params.milestoneId === milestoneId);
|
|
91
|
+
const remaining = allEvents.filter((e) => e.params.milestoneId !== milestoneId);
|
|
92
|
+
if (toArchive.length === 0) {
|
|
93
|
+
return { archived: 0 };
|
|
94
|
+
}
|
|
95
|
+
// Write archived events to .jsonl.archived file (crash-safe)
|
|
96
|
+
atomicWriteSync(archivePath, toArchive.map((e) => JSON.stringify(e)).join("\n") + "\n");
|
|
97
|
+
// Truncate active log to remaining events only
|
|
98
|
+
atomicWriteSync(logPath, remaining.length > 0
|
|
99
|
+
? remaining.map((e) => JSON.stringify(e)).join("\n") + "\n"
|
|
100
|
+
: "");
|
|
101
|
+
return { archived: toArchive.length };
|
|
102
|
+
}
|