gsd-pi 2.45.0-dev.fdcf73c → 2.46.0-dev.5c2b8ed
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/resources/extensions/remote-questions/config.js +42 -0
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +19 -19
- 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 +19 -19
- 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/remote-questions.test.ts +84 -0
- 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/src/resources/extensions/remote-questions/config.ts +45 -0
- /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → NtM_pwvGTaXTeqiByOv3A}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → NtM_pwvGTaXTeqiByOv3A}/_ssgManifest.js +0 -0
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
} from "../gsd-db.js";
|
|
12
12
|
import { invalidateStateCache } from "../state.js";
|
|
13
13
|
import { renderPlanFromDb, renderReplanFromDb } from "../markdown-renderer.js";
|
|
14
|
+
import { renderAllProjections } from "../workflow-projections.js";
|
|
15
|
+
import { writeManifest } from "../workflow-manifest.js";
|
|
16
|
+
import { appendEvent } from "../workflow-events.js";
|
|
14
17
|
|
|
15
18
|
export interface ReplanSliceTaskInput {
|
|
16
19
|
taskId: string;
|
|
@@ -32,6 +35,10 @@ export interface ReplanSliceParams {
|
|
|
32
35
|
whatChanged: string;
|
|
33
36
|
updatedTasks: ReplanSliceTaskInput[];
|
|
34
37
|
removedTaskIds: string[];
|
|
38
|
+
/** Optional caller-provided identity for audit trail */
|
|
39
|
+
actorName?: string;
|
|
40
|
+
/** Optional caller-provided reason this action was triggered */
|
|
41
|
+
triggerReason?: string;
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
export interface ReplanSliceResult {
|
|
@@ -83,11 +90,23 @@ export async function handleReplanSlice(
|
|
|
83
90
|
return { error: `validation failed: ${(err as Error).message}` };
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
// ── Verify parent slice exists
|
|
93
|
+
// ── Verify parent slice exists and is not closed ─────────────────
|
|
87
94
|
const parentSlice = getSlice(params.milestoneId, params.sliceId);
|
|
88
95
|
if (!parentSlice) {
|
|
89
96
|
return { error: `missing parent slice: ${params.milestoneId}/${params.sliceId}` };
|
|
90
97
|
}
|
|
98
|
+
if (parentSlice.status === "complete" || parentSlice.status === "done") {
|
|
99
|
+
return { error: `cannot replan a closed slice: ${params.sliceId} (status: ${parentSlice.status})` };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── Verify blocker task exists and is complete ────────────────────
|
|
103
|
+
const blockerTask = getTask(params.milestoneId, params.sliceId, params.blockerTaskId);
|
|
104
|
+
if (!blockerTask) {
|
|
105
|
+
return { error: `blockerTaskId not found: ${params.milestoneId}/${params.sliceId}/${params.blockerTaskId}` };
|
|
106
|
+
}
|
|
107
|
+
if (blockerTask.status !== "complete" && blockerTask.status !== "done") {
|
|
108
|
+
return { error: `blockerTaskId ${params.blockerTaskId} is not complete (status: ${blockerTask.status}) — the blocker task must be finished before a replan is triggered` };
|
|
109
|
+
}
|
|
91
110
|
|
|
92
111
|
// ── Structural enforcement ────────────────────────────────────────
|
|
93
112
|
const existingTasks = getSliceTasks(params.milestoneId, params.sliceId);
|
|
@@ -183,6 +202,24 @@ export async function handleReplanSlice(
|
|
|
183
202
|
invalidateStateCache();
|
|
184
203
|
clearParseCache();
|
|
185
204
|
|
|
205
|
+
// ── Post-mutation hook: projections, manifest, event log ─────
|
|
206
|
+
try {
|
|
207
|
+
await renderAllProjections(basePath, params.milestoneId);
|
|
208
|
+
writeManifest(basePath);
|
|
209
|
+
appendEvent(basePath, {
|
|
210
|
+
cmd: "replan-slice",
|
|
211
|
+
params: { milestoneId: params.milestoneId, sliceId: params.sliceId, blockerTaskId: params.blockerTaskId },
|
|
212
|
+
ts: new Date().toISOString(),
|
|
213
|
+
actor: "agent",
|
|
214
|
+
actor_name: params.actorName,
|
|
215
|
+
trigger_reason: params.triggerReason,
|
|
216
|
+
});
|
|
217
|
+
} catch (hookErr) {
|
|
218
|
+
process.stderr.write(
|
|
219
|
+
`gsd: replan-slice post-mutation hook warning: ${(hookErr as Error).message}\n`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
186
223
|
return {
|
|
187
224
|
milestoneId: params.milestoneId,
|
|
188
225
|
sliceId: params.sliceId,
|
|
@@ -520,6 +520,10 @@ export interface CompleteTaskParams {
|
|
|
520
520
|
verdict: string;
|
|
521
521
|
durationMs: number;
|
|
522
522
|
}>;
|
|
523
|
+
/** Optional caller-provided identity for audit trail */
|
|
524
|
+
actorName?: string;
|
|
525
|
+
/** Optional caller-provided reason this action was triggered */
|
|
526
|
+
triggerReason?: string;
|
|
523
527
|
}
|
|
524
528
|
|
|
525
529
|
// ─── Complete Slice Params (gsd_complete_slice tool input) ───────────────
|
|
@@ -548,4 +552,8 @@ export interface CompleteSliceParams {
|
|
|
548
552
|
requires: Array<{ slice: string; provides: string }>;
|
|
549
553
|
affects: string[];
|
|
550
554
|
drillDownPaths: string[];
|
|
555
|
+
/** Optional caller-provided identity for audit trail */
|
|
556
|
+
actorName?: string;
|
|
557
|
+
/** Optional caller-provided reason this action was triggered */
|
|
558
|
+
triggerReason?: string;
|
|
551
559
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
|
|
15
|
+
import { existsSync, readFileSync, mkdirSync } from "node:fs";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { atomicWriteSync } from "./atomic-write.js";
|
|
18
|
+
|
|
19
|
+
// ─── Types ───────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export interface UnitClaim {
|
|
22
|
+
agent: string;
|
|
23
|
+
claimed_at: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ClaimsMap = Record<string, UnitClaim>;
|
|
27
|
+
|
|
28
|
+
// ─── Key Builders ────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
export function taskUnitKey(milestoneId: string, sliceId: string, taskId: string): string {
|
|
31
|
+
return `${milestoneId}/${sliceId}/${taskId}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function sliceUnitKey(milestoneId: string, sliceId: string): string {
|
|
35
|
+
return `${milestoneId}/${sliceId}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ─── File Path ───────────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
function claimsPath(basePath: string): string {
|
|
41
|
+
return join(basePath, ".gsd", "unit-claims.json");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ─── Read Claims ─────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
function readClaims(basePath: string): ClaimsMap | null {
|
|
47
|
+
const path = claimsPath(basePath);
|
|
48
|
+
if (!existsSync(path)) return null;
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(readFileSync(path, "utf-8")) as ClaimsMap;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ─── Public API ──────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Claim a unit for an agent.
|
|
60
|
+
* Overwrites any existing claim for this unit (last writer wins).
|
|
61
|
+
*/
|
|
62
|
+
export function claimUnit(basePath: string, unitKey: string, agentName: string): void {
|
|
63
|
+
const claims = readClaims(basePath) ?? {};
|
|
64
|
+
claims[unitKey] = { agent: agentName, claimed_at: new Date().toISOString() };
|
|
65
|
+
const dir = join(basePath, ".gsd");
|
|
66
|
+
mkdirSync(dir, { recursive: true });
|
|
67
|
+
atomicWriteSync(claimsPath(basePath), JSON.stringify(claims, null, 2) + "\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Release a unit claim (remove it from the claims map).
|
|
72
|
+
*/
|
|
73
|
+
export function releaseUnit(basePath: string, unitKey: string): void {
|
|
74
|
+
const claims = readClaims(basePath);
|
|
75
|
+
if (!claims || !(unitKey in claims)) return;
|
|
76
|
+
delete claims[unitKey];
|
|
77
|
+
atomicWriteSync(claimsPath(basePath), JSON.stringify(claims, null, 2) + "\n");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the current owner of a unit, or null if unclaimed / no claims file.
|
|
82
|
+
*/
|
|
83
|
+
export function getOwner(basePath: string, unitKey: string): string | null {
|
|
84
|
+
const claims = readClaims(basePath);
|
|
85
|
+
if (!claims) return null;
|
|
86
|
+
return claims[unitKey]?.agent ?? null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Check if an actor is authorized to operate on a unit.
|
|
91
|
+
* Returns null if ownership passes (or is unclaimed / no file).
|
|
92
|
+
* Returns an error string if a different agent owns the unit.
|
|
93
|
+
*/
|
|
94
|
+
export function checkOwnership(
|
|
95
|
+
basePath: string,
|
|
96
|
+
unitKey: string,
|
|
97
|
+
actorName: string | undefined,
|
|
98
|
+
): string | null {
|
|
99
|
+
if (!actorName) return null; // no actor identity provided — opt-in, so allow
|
|
100
|
+
const owner = getOwner(basePath, unitKey);
|
|
101
|
+
if (owner === null) return null; // unit unclaimed or no claims file
|
|
102
|
+
if (owner === actorName) return null; // actor is the owner
|
|
103
|
+
return `Unit ${unitKey} is owned by ${owner}, not ${actorName}`;
|
|
104
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
|
|
6
|
+
// ─── Session ID ───────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Engine-generated session ID — stable for the lifetime of this process.
|
|
10
|
+
* Agents can reference this to correlate all events from one run.
|
|
11
|
+
*/
|
|
12
|
+
const ENGINE_SESSION_ID: string = randomUUID();
|
|
13
|
+
|
|
14
|
+
export function getSessionId(): string {
|
|
15
|
+
return ENGINE_SESSION_ID;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ─── Event Types ─────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
export interface WorkflowEvent {
|
|
21
|
+
cmd: string; // e.g. "complete_task"
|
|
22
|
+
params: Record<string, unknown>;
|
|
23
|
+
ts: string; // ISO 8601
|
|
24
|
+
hash: string; // content hash (hex, 16 chars)
|
|
25
|
+
actor: "agent" | "system";
|
|
26
|
+
actor_name?: string; // e.g. "executor-agent-01" — caller-provided identity
|
|
27
|
+
trigger_reason?: string; // e.g. "plan-phase complete" — caller-provided causation
|
|
28
|
+
session_id: string; // engine-generated UUID, stable per process lifetime
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ─── appendEvent ─────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Append one event to .gsd/event-log.jsonl.
|
|
35
|
+
* Computes a content hash from cmd+params (deterministic, independent of ts/actor/session).
|
|
36
|
+
* Creates .gsd directory if needed.
|
|
37
|
+
*/
|
|
38
|
+
export function appendEvent(
|
|
39
|
+
basePath: string,
|
|
40
|
+
event: Omit<WorkflowEvent, "hash" | "session_id"> & { actor_name?: string; trigger_reason?: string },
|
|
41
|
+
): void {
|
|
42
|
+
const hash = createHash("sha256")
|
|
43
|
+
.update(JSON.stringify({ cmd: event.cmd, params: event.params, ts: event.ts }))
|
|
44
|
+
.digest("hex")
|
|
45
|
+
.slice(0, 16);
|
|
46
|
+
|
|
47
|
+
const fullEvent: WorkflowEvent = {
|
|
48
|
+
...event,
|
|
49
|
+
hash,
|
|
50
|
+
session_id: ENGINE_SESSION_ID,
|
|
51
|
+
};
|
|
52
|
+
const dir = join(basePath, ".gsd");
|
|
53
|
+
mkdirSync(dir, { recursive: true });
|
|
54
|
+
appendFileSync(join(dir, "event-log.jsonl"), JSON.stringify(fullEvent) + "\n", "utf-8");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ─── readEvents ──────────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Read all events from a JSONL file.
|
|
61
|
+
* Returns empty array if file doesn't exist.
|
|
62
|
+
* Corrupted lines are skipped with stderr warning.
|
|
63
|
+
*/
|
|
64
|
+
export function readEvents(logPath: string): WorkflowEvent[] {
|
|
65
|
+
if (!existsSync(logPath)) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const content = readFileSync(logPath, "utf-8");
|
|
70
|
+
const lines = content.split("\n").filter((l) => l.length > 0);
|
|
71
|
+
const events: WorkflowEvent[] = [];
|
|
72
|
+
|
|
73
|
+
for (const line of lines) {
|
|
74
|
+
try {
|
|
75
|
+
events.push(JSON.parse(line) as WorkflowEvent);
|
|
76
|
+
} catch {
|
|
77
|
+
process.stderr.write(`workflow-events: skipping corrupted event line: ${line.slice(0, 80)}\n`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return events;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ─── findForkPoint ───────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Find the index of the last common event between two logs by comparing hashes.
|
|
88
|
+
* Returns -1 if the first events differ (completely diverged).
|
|
89
|
+
* If one log is a prefix of the other, returns length of shorter - 1.
|
|
90
|
+
*/
|
|
91
|
+
export function findForkPoint(
|
|
92
|
+
logA: WorkflowEvent[],
|
|
93
|
+
logB: WorkflowEvent[],
|
|
94
|
+
): number {
|
|
95
|
+
const minLen = Math.min(logA.length, logB.length);
|
|
96
|
+
let lastCommon = -1;
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < minLen; i++) {
|
|
99
|
+
if (logA[i]!.hash === logB[i]!.hash) {
|
|
100
|
+
lastCommon = i;
|
|
101
|
+
} else {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return lastCommon;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ─── compactMilestoneEvents ─────────────────────────────────────────────────
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Archive a milestone's events from the active log to a separate file.
|
|
113
|
+
* Active log retains only events from other milestones.
|
|
114
|
+
* Archived file is kept on disk for forensics.
|
|
115
|
+
*
|
|
116
|
+
* @param basePath - Project root (parent of .gsd/)
|
|
117
|
+
* @param milestoneId - The milestone whose events should be archived
|
|
118
|
+
* @returns { archived: number } — count of events moved to archive
|
|
119
|
+
*/
|
|
120
|
+
export function compactMilestoneEvents(
|
|
121
|
+
basePath: string,
|
|
122
|
+
milestoneId: string,
|
|
123
|
+
): { archived: number } {
|
|
124
|
+
const logPath = join(basePath, ".gsd", "event-log.jsonl");
|
|
125
|
+
const archivePath = join(basePath, ".gsd", `event-log-${milestoneId}.jsonl.archived`);
|
|
126
|
+
|
|
127
|
+
const allEvents = readEvents(logPath);
|
|
128
|
+
const toArchive = allEvents.filter(
|
|
129
|
+
(e) => (e.params as { milestoneId?: string }).milestoneId === milestoneId,
|
|
130
|
+
);
|
|
131
|
+
const remaining = allEvents.filter(
|
|
132
|
+
(e) => (e.params as { milestoneId?: string }).milestoneId !== milestoneId,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (toArchive.length === 0) {
|
|
136
|
+
return { archived: 0 };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Write archived events to .jsonl.archived file (crash-safe)
|
|
140
|
+
atomicWriteSync(
|
|
141
|
+
archivePath,
|
|
142
|
+
toArchive.map((e) => JSON.stringify(e)).join("\n") + "\n",
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// Truncate active log to remaining events only
|
|
146
|
+
atomicWriteSync(
|
|
147
|
+
logPath,
|
|
148
|
+
remaining.length > 0
|
|
149
|
+
? remaining.map((e) => JSON.stringify(e)).join("\n") + "\n"
|
|
150
|
+
: "",
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return { archived: toArchive.length };
|
|
154
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Centralized warning/error accumulator for the workflow engine pipeline.
|
|
3
3
|
// Captures structured entries that the auto-loop can drain after each unit
|
|
4
4
|
// to surface root causes for stuck loops, silent degradation, and blocked writes.
|
|
5
|
+
// All entries are also persisted to .gsd/audit-log.jsonl for post-mortem analysis.
|
|
5
6
|
//
|
|
6
7
|
// Stderr policy: every logWarning/logError call writes immediately to stderr
|
|
7
8
|
// for terminal visibility. This is intentional — unlike debug-logger (which is
|
|
@@ -13,6 +14,9 @@
|
|
|
13
14
|
// the start of each unit to prevent log bleed between units running in the same
|
|
14
15
|
// Node process.
|
|
15
16
|
|
|
17
|
+
import { appendFileSync, readFileSync, existsSync, mkdirSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
|
|
16
20
|
// ─── Types ──────────────────────────────────────────────────────────────
|
|
17
21
|
|
|
18
22
|
export type LogSeverity = "warn" | "error";
|
|
@@ -38,10 +42,20 @@ export interface LogEntry {
|
|
|
38
42
|
context?: Record<string, string>;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
|
-
// ─── Buffer
|
|
45
|
+
// ─── Buffer & Persistent Audit ──────────────────────────────────────────
|
|
42
46
|
|
|
43
47
|
const MAX_BUFFER = 100;
|
|
44
48
|
let _buffer: LogEntry[] = [];
|
|
49
|
+
let _auditBasePath: string | null = null;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Set the base path for persistent audit log writes.
|
|
53
|
+
* Should be called once at engine init with the project root.
|
|
54
|
+
* Until set, log entries are buffered in-memory only.
|
|
55
|
+
*/
|
|
56
|
+
export function setLogBasePath(basePath: string): void {
|
|
57
|
+
_auditBasePath = basePath;
|
|
58
|
+
}
|
|
45
59
|
|
|
46
60
|
// ─── Public API ─────────────────────────────────────────────────────────
|
|
47
61
|
|
|
@@ -156,12 +170,36 @@ export function formatForNotification(entries: readonly LogEntry[]): string {
|
|
|
156
170
|
.join("\n");
|
|
157
171
|
}
|
|
158
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Read all entries from the persistent audit log.
|
|
175
|
+
* Returns empty array if no basePath is set or the file doesn't exist.
|
|
176
|
+
*/
|
|
177
|
+
export function readAuditLog(basePath?: string): LogEntry[] {
|
|
178
|
+
const bp = basePath ?? _auditBasePath;
|
|
179
|
+
if (!bp) return [];
|
|
180
|
+
const auditPath = join(bp, ".gsd", "audit-log.jsonl");
|
|
181
|
+
if (!existsSync(auditPath)) return [];
|
|
182
|
+
try {
|
|
183
|
+
const content = readFileSync(auditPath, "utf-8");
|
|
184
|
+
return content
|
|
185
|
+
.split("\n")
|
|
186
|
+
.filter((l) => l.length > 0)
|
|
187
|
+
.map((l) => {
|
|
188
|
+
try { return JSON.parse(l) as LogEntry; } catch { return null; }
|
|
189
|
+
})
|
|
190
|
+
.filter((e): e is LogEntry => e !== null);
|
|
191
|
+
} catch {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
159
196
|
/**
|
|
160
197
|
* Reset buffer. Call at the start of each auto-loop unit to prevent log bleed
|
|
161
198
|
* between units running in the same process. Also used in tests via _resetLogs().
|
|
162
199
|
*/
|
|
163
200
|
export function _resetLogs(): void {
|
|
164
201
|
_buffer = [];
|
|
202
|
+
_auditBasePath = null;
|
|
165
203
|
}
|
|
166
204
|
|
|
167
205
|
// ─── Internal ───────────────────────────────────────────────────────────
|
|
@@ -190,4 +228,16 @@ function _push(
|
|
|
190
228
|
if (_buffer.length > MAX_BUFFER) {
|
|
191
229
|
_buffer.shift();
|
|
192
230
|
}
|
|
231
|
+
|
|
232
|
+
// Persist to .gsd/audit-log.jsonl so entries survive context resets
|
|
233
|
+
if (_auditBasePath) {
|
|
234
|
+
try {
|
|
235
|
+
const auditDir = join(_auditBasePath, ".gsd");
|
|
236
|
+
mkdirSync(auditDir, { recursive: true });
|
|
237
|
+
appendFileSync(join(auditDir, "audit-log.jsonl"), JSON.stringify(entry) + "\n", "utf-8");
|
|
238
|
+
} catch (auditErr) {
|
|
239
|
+
// Best-effort — never let audit write failures bubble up
|
|
240
|
+
process.stderr.write(`[gsd:audit] failed to persist log entry: ${(auditErr as Error).message}\n`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
193
243
|
}
|