@osolmaz/pi-workflows 0.1.0 → 0.2.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 +36 -21
- package/dist/extension/executor.d.ts +14 -1
- package/dist/extension/executor.js +11 -1
- package/dist/extension/executor.js.map +1 -1
- package/dist/extension/index.js +79 -7
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/recorder.d.ts +85 -0
- package/dist/extension/recorder.js +525 -0
- package/dist/extension/recorder.js.map +1 -0
- package/dist/extension/session-events.d.ts +134 -0
- package/dist/extension/session-events.js +60 -0
- package/dist/extension/session-events.js.map +1 -0
- package/dist/extension/widget.js +25 -24
- package/dist/extension/widget.js.map +1 -1
- package/dist/render/canvas.d.ts +1 -1
- package/dist/render/canvas.js +5 -0
- package/dist/render/canvas.js.map +1 -1
- package/dist/render/graph-render.d.ts +5 -0
- package/dist/render/graph-render.js +211 -48
- package/dist/render/graph-render.js.map +1 -1
- package/dist/viewer/render.js +19 -3
- package/dist/viewer/render.js.map +1 -1
- package/dist/viewer/session-reducer.d.ts +45 -0
- package/dist/viewer/session-reducer.js +266 -0
- package/dist/viewer/session-reducer.js.map +1 -0
- package/dist/workflows/artifacts.d.ts +40 -0
- package/dist/workflows/artifacts.js +155 -0
- package/dist/workflows/artifacts.js.map +1 -0
- package/dist/workflows/engine.d.ts +2 -0
- package/dist/workflows/engine.js +38 -7
- package/dist/workflows/engine.js.map +1 -1
- package/dist/workflows/index.d.ts +3 -2
- package/dist/workflows/index.js +2 -1
- package/dist/workflows/index.js.map +1 -1
- package/dist/workflows/store.d.ts +53 -9
- package/dist/workflows/store.js +523 -43
- package/dist/workflows/store.js.map +1 -1
- package/dist/workflows/types.d.ts +126 -3
- package/docs/development.md +43 -19
- package/docs/live-replay-protocol.md +155 -0
- package/docs/plans/piw-viewer-experience-implementation-plan.md +674 -0
- package/docs/plans/replayable-run-bundles-implementation-plan.md +65 -0
- package/docs/plans/session-event-replay-implementation-plan.md +494 -0
- package/docs/plans/tui-viewer-implementation-plan.md +64 -0
- package/docs/run-bundles.md +320 -55
- package/docs/session-event-journal.md +470 -0
- package/docs/tui-viewer.md +218 -0
- package/package.json +2 -1
- package/src/extension/executor.ts +28 -1
- package/src/extension/index.ts +87 -7
- package/src/extension/recorder.ts +633 -0
- package/src/extension/session-events.ts +119 -0
- package/src/extension/widget.ts +26 -24
- package/src/render/canvas.ts +19 -1
- package/src/render/graph-render.ts +277 -44
- package/src/viewer/render.ts +21 -3
- package/src/viewer/session-reducer.ts +347 -0
- package/src/workflows/artifacts.ts +188 -0
- package/src/workflows/engine.ts +39 -7
- package/src/workflows/index.ts +15 -0
- package/src/workflows/store.ts +649 -49
- package/src/workflows/types.ts +141 -3
package/dist/workflows/store.js
CHANGED
|
@@ -2,13 +2,24 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { ArtifactWriter, encodeValue } from "./artifacts.js";
|
|
5
6
|
export const RUN_BUNDLE_SCHEMA = "pi-workflows.run-bundle.v1";
|
|
7
|
+
export const RUN_STATE_SCHEMA = "pi-workflows.run-state.v1";
|
|
6
8
|
export const TRACE_EVENT_SCHEMA = "pi-workflows.trace-event.v1";
|
|
7
9
|
export const DEFINITION_SNAPSHOT_SCHEMA = "pi-workflows.definition-snapshot.v1";
|
|
10
|
+
export const SESSION_BINDING_SCHEMA = "pi-workflows.session-binding.v1";
|
|
11
|
+
export const SESSION_EVENT_SCHEMA = "pi-workflows.session-event.v1";
|
|
12
|
+
export const SESSION_CAPTURE_SCHEMA = "pi-workflows.session-capture.v1";
|
|
13
|
+
export const SESSION_EVENT_MAX_BYTES = 1024 * 1024;
|
|
8
14
|
const MANIFEST_PATH = "manifest.json";
|
|
9
15
|
const WORKFLOW_SNAPSHOT_PATH = "workflow.json";
|
|
10
16
|
const STATE_PATH = "state.json";
|
|
11
17
|
const TRACE_PATH = "trace.ndjson";
|
|
18
|
+
const SESSION_DIR = "session";
|
|
19
|
+
const SESSION_BINDING_PATH = `${SESSION_DIR}/binding.json`;
|
|
20
|
+
const SESSION_ENTRIES_PATH = `${SESSION_DIR}/entries.ndjson`;
|
|
21
|
+
const SESSION_EVENTS_PATH = `${SESSION_DIR}/events.ndjson`;
|
|
22
|
+
const SESSION_CAPTURE_PATH = `${SESSION_DIR}/capture.json`;
|
|
12
23
|
/** Runs directory: `$PI_WORKFLOWS_RUNS_DIR` or `~/.pi/agent/workflows/runs`. */
|
|
13
24
|
export function workflowRunsBaseDir(homeDir = os.homedir()) {
|
|
14
25
|
const override = process.env.PI_WORKFLOWS_RUNS_DIR;
|
|
@@ -30,75 +41,441 @@ export function createRunId(workflowName, now = new Date()) {
|
|
|
30
41
|
return `${stamp}-${slug || "workflow"}-${randomUUID().slice(0, 8)}`;
|
|
31
42
|
}
|
|
32
43
|
/**
|
|
33
|
-
* Persists run bundles
|
|
34
|
-
*
|
|
35
|
-
* atomically
|
|
44
|
+
* Persists run bundles (see docs/run-bundles.md). `trace.ndjson` is the
|
|
45
|
+
* append-only source of truth; every transition appends the trace event
|
|
46
|
+
* first, then atomically replaces `state.json` (carrying `traceSeq`) and
|
|
47
|
+
* `manifest.json`. Large string leaves in persisted values are externalized
|
|
48
|
+
* into content-addressed `artifacts/`. Bundles are private: directories are
|
|
49
|
+
* 0700 and files 0600.
|
|
36
50
|
*/
|
|
37
51
|
export class WorkflowRunStore {
|
|
38
52
|
outputRoot;
|
|
39
|
-
|
|
40
|
-
appendChainByPath = new Map();
|
|
53
|
+
contexts = new Map();
|
|
41
54
|
constructor(outputRoot = workflowRunsBaseDir()) {
|
|
42
55
|
this.outputRoot = outputRoot;
|
|
43
56
|
}
|
|
44
57
|
runDirFor(runId) {
|
|
45
58
|
return path.join(this.outputRoot, runId);
|
|
46
59
|
}
|
|
60
|
+
contextFor(runDir) {
|
|
61
|
+
let context = this.contexts.get(runDir);
|
|
62
|
+
if (!context) {
|
|
63
|
+
context = {
|
|
64
|
+
traceSeq: 0,
|
|
65
|
+
sessionSeq: 0,
|
|
66
|
+
sessionEventSeq: 0,
|
|
67
|
+
sessionBound: false,
|
|
68
|
+
sessionEventsStopped: false,
|
|
69
|
+
artifacts: new ArtifactWriter(runDir),
|
|
70
|
+
lock: Promise.resolve(),
|
|
71
|
+
sessionEventLock: Promise.resolve(),
|
|
72
|
+
};
|
|
73
|
+
this.contexts.set(runDir, context);
|
|
74
|
+
}
|
|
75
|
+
return context;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Run `task` exclusively for this bundle. Sequence numbers are assigned
|
|
79
|
+
* inside the lock, so physical file order always matches logical order.
|
|
80
|
+
*/
|
|
81
|
+
withRunLock(runDir, task) {
|
|
82
|
+
const context = this.contextFor(runDir);
|
|
83
|
+
const result = context.lock.then(task);
|
|
84
|
+
context.lock = result.then(() => undefined, () => undefined);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
withSessionEventLock(runDir, task) {
|
|
88
|
+
const context = this.contextFor(runDir);
|
|
89
|
+
const result = context.sessionEventLock.then(task);
|
|
90
|
+
context.sessionEventLock = result.then(() => undefined, () => undefined);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
47
93
|
async initializeRunBundle(workflow, state) {
|
|
48
94
|
const runDir = this.runDirFor(state.runId);
|
|
49
|
-
|
|
50
|
-
this.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
95
|
+
this.contexts.delete(runDir);
|
|
96
|
+
return await this.withRunLock(runDir, async () => {
|
|
97
|
+
await fs.mkdir(runDir, { recursive: true, mode: 0o700 });
|
|
98
|
+
await writeJsonAtomic(path.join(runDir, WORKFLOW_SNAPSHOT_PATH), createDefinitionSnapshot(workflow));
|
|
99
|
+
await appendLine(path.join(runDir, TRACE_PATH), null);
|
|
100
|
+
await this.writeProjections(runDir, state);
|
|
101
|
+
return runDir;
|
|
102
|
+
});
|
|
56
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Persist one transition: append the trace event, then rewrite the
|
|
106
|
+
* projections reflecting it.
|
|
107
|
+
*/
|
|
57
108
|
async writeSnapshot(runDir, state, event) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
109
|
+
return await this.withRunLock(runDir, async () => {
|
|
110
|
+
const traceEvent = await this.appendTraceEvent(runDir, state.runId, event);
|
|
111
|
+
state.traceSeq = traceEvent.seq;
|
|
112
|
+
state.updatedAt = new Date().toISOString();
|
|
113
|
+
await this.writeProjections(runDir, state);
|
|
114
|
+
return traceEvent;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Bind the run to a Pi conversation: write `session/binding.json` once and
|
|
119
|
+
* append a `session_bound` trace event. Projections catch up on the next
|
|
120
|
+
* snapshot.
|
|
121
|
+
*/
|
|
122
|
+
async writeSessionBinding(runDir, binding) {
|
|
123
|
+
await this.withRunLock(runDir, async () => {
|
|
124
|
+
const context = this.contextFor(runDir);
|
|
125
|
+
if (context.sessionBound) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
context.sessionBound = true;
|
|
129
|
+
await fs.mkdir(path.join(runDir, SESSION_DIR), { recursive: true, mode: 0o700 });
|
|
130
|
+
await writeJsonAtomic(path.join(runDir, SESSION_BINDING_PATH), binding);
|
|
131
|
+
await this.appendTraceEvent(runDir, binding.runId, {
|
|
132
|
+
scope: "session",
|
|
133
|
+
type: "session_bound",
|
|
134
|
+
payload: { piSessionId: binding.piSessionId },
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** Append one verbatim Pi session entry to `session/entries.ndjson`. */
|
|
139
|
+
async appendSessionEntry(runDir, entry) {
|
|
140
|
+
return await this.withRunLock(runDir, async () => {
|
|
141
|
+
const context = this.contextFor(runDir);
|
|
142
|
+
context.sessionSeq += 1;
|
|
143
|
+
const record = {
|
|
144
|
+
seq: context.sessionSeq,
|
|
145
|
+
at: new Date().toISOString(),
|
|
146
|
+
entry,
|
|
147
|
+
};
|
|
148
|
+
await appendLine(path.join(runDir, SESSION_ENTRIES_PATH), record);
|
|
149
|
+
return record.seq;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/** Append a fully stamped ordered batch to `session/events.ndjson`. */
|
|
153
|
+
async appendSessionEventBatch(runDir, records) {
|
|
154
|
+
if (records.length === 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
await this.withSessionEventLock(runDir, async () => {
|
|
158
|
+
const context = this.contextFor(runDir);
|
|
159
|
+
if (context.sessionEventsStopped) {
|
|
160
|
+
throw new Error("Session event capture has stopped");
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
let expected = context.sessionEventSeq + 1;
|
|
164
|
+
for (const record of records) {
|
|
165
|
+
validateSessionEventRecord(record);
|
|
166
|
+
if (record.seq !== expected) {
|
|
167
|
+
throw new Error(`Expected session event seq ${expected}, got ${record.seq}`);
|
|
168
|
+
}
|
|
169
|
+
expected += 1;
|
|
170
|
+
}
|
|
171
|
+
const encoded = await Promise.all(records.map(async (record) => ({
|
|
172
|
+
...record,
|
|
173
|
+
payload: record.type === "tool_execution_started" ||
|
|
174
|
+
record.type === "tool_execution_finished" ||
|
|
175
|
+
(record.type === "assistant_event" && record.payload.type === "toolcall_end")
|
|
176
|
+
? (await encodeValue(record.payload, context.artifacts))
|
|
177
|
+
: record.payload,
|
|
178
|
+
})));
|
|
179
|
+
for (const record of encoded) {
|
|
180
|
+
if (Buffer.byteLength(JSON.stringify(record), "utf8") + 1 > SESSION_EVENT_MAX_BYTES) {
|
|
181
|
+
throw new Error(`session event exceeded ${SESSION_EVENT_MAX_BYTES} bytes`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
await appendLines(path.join(runDir, SESSION_EVENTS_PATH), encoded);
|
|
185
|
+
context.sessionEventSeq = records.at(-1)?.seq ?? context.sessionEventSeq;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
context.sessionEventsStopped = true;
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/** Atomically replace the temporal capture integrity projection. */
|
|
194
|
+
async writeSessionCapture(runDir, capture) {
|
|
195
|
+
validateSessionCapture(capture);
|
|
196
|
+
await this.withSessionEventLock(runDir, async () => {
|
|
197
|
+
const context = this.contextFor(runDir);
|
|
198
|
+
if (capture.status !== "recording") {
|
|
199
|
+
context.sessionEventsStopped = true;
|
|
200
|
+
}
|
|
201
|
+
await writeJsonAtomic(path.join(runDir, SESSION_CAPTURE_PATH), capture);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/** Count complete durable session records after both writers have drained. */
|
|
205
|
+
async sessionCounts(runDir) {
|
|
206
|
+
const events = await readCompleteNdjson(path.join(runDir, SESSION_EVENTS_PATH));
|
|
207
|
+
const entries = await readCompleteNdjson(path.join(runDir, SESSION_ENTRIES_PATH));
|
|
208
|
+
return {
|
|
209
|
+
eventCount: events.length,
|
|
210
|
+
entryCount: entries.length,
|
|
211
|
+
lastEventSeq: events.at(-1)?.seq ?? 0,
|
|
212
|
+
};
|
|
62
213
|
}
|
|
63
|
-
async
|
|
214
|
+
async appendTraceEvent(runDir, runId, event) {
|
|
215
|
+
const context = this.contextFor(runDir);
|
|
64
216
|
const traceEvent = {
|
|
65
|
-
seq:
|
|
217
|
+
seq: context.traceSeq + 1,
|
|
66
218
|
at: new Date().toISOString(),
|
|
67
|
-
runId
|
|
219
|
+
runId,
|
|
68
220
|
...event,
|
|
221
|
+
payload: (await encodeValue(event.payload, context.artifacts)),
|
|
69
222
|
};
|
|
70
|
-
await
|
|
223
|
+
await appendLine(path.join(runDir, TRACE_PATH), traceEvent);
|
|
224
|
+
context.traceSeq = traceEvent.seq;
|
|
71
225
|
return traceEvent;
|
|
72
226
|
}
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
227
|
+
async writeProjections(runDir, state) {
|
|
228
|
+
const context = this.contextFor(runDir);
|
|
229
|
+
const encoded = await encodeRunState(state, context.artifacts);
|
|
230
|
+
await writeJsonAtomic(path.join(runDir, STATE_PATH), encoded);
|
|
231
|
+
await writeJsonAtomic(path.join(runDir, MANIFEST_PATH), createManifest(state, {
|
|
232
|
+
session: context.sessionBound,
|
|
233
|
+
}));
|
|
77
234
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
235
|
+
}
|
|
236
|
+
async function appendLine(filePath, value) {
|
|
237
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
238
|
+
await fs.appendFile(filePath, value === null ? "" : `${JSON.stringify(value)}\n`, {
|
|
239
|
+
encoding: "utf8",
|
|
240
|
+
mode: 0o600,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async function appendLines(filePath, values) {
|
|
244
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
245
|
+
const text = values.map((value) => `${JSON.stringify(value)}\n`).join("");
|
|
246
|
+
await fs.appendFile(filePath, text, { encoding: "utf8", mode: 0o600 });
|
|
247
|
+
}
|
|
248
|
+
function isNonEmptyString(value) {
|
|
249
|
+
return typeof value === "string" && value.length > 0;
|
|
250
|
+
}
|
|
251
|
+
function validateSessionEventRecord(record) {
|
|
252
|
+
if (!Number.isSafeInteger(record.seq) || record.seq < 1) {
|
|
253
|
+
throw new Error("Session event seq must be a positive safe integer");
|
|
254
|
+
}
|
|
255
|
+
if (!isNonEmptyString(record.at) ||
|
|
256
|
+
!isNonEmptyString(record.nodeId) ||
|
|
257
|
+
!isNonEmptyString(record.attemptId) ||
|
|
258
|
+
!isNonEmptyString(record.type) ||
|
|
259
|
+
typeof record.payload !== "object" ||
|
|
260
|
+
record.payload === null ||
|
|
261
|
+
Array.isArray(record.payload)) {
|
|
262
|
+
throw new Error("Session event is missing required envelope fields");
|
|
263
|
+
}
|
|
264
|
+
const knownType = [
|
|
265
|
+
"turn_started",
|
|
266
|
+
"turn_finished",
|
|
267
|
+
"message_started",
|
|
268
|
+
"assistant_event",
|
|
269
|
+
"message_finished",
|
|
270
|
+
"tool_execution_started",
|
|
271
|
+
"tool_execution_updated",
|
|
272
|
+
"tool_execution_finished",
|
|
273
|
+
].includes(record.type);
|
|
274
|
+
if (!knownType) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (!isNonEmptyString(record.turnId)) {
|
|
278
|
+
throw new Error(`${record.type} requires turnId`);
|
|
279
|
+
}
|
|
280
|
+
if (!["turn_started", "turn_finished"].includes(record.type) &&
|
|
281
|
+
!isNonEmptyString(record.messageId)) {
|
|
282
|
+
throw new Error(`${record.type} requires messageId`);
|
|
283
|
+
}
|
|
284
|
+
if (record.type.startsWith("tool_execution_") && !isNonEmptyString(record.toolCallId)) {
|
|
285
|
+
throw new Error(`${record.type} requires toolCallId`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function sessionRelationshipDiagnostics(entries, events) {
|
|
289
|
+
const entryIds = new Set(entries.flatMap((record) => (isNonEmptyString(record.entry.id) ? [record.entry.id] : [])));
|
|
290
|
+
const turns = new Set();
|
|
291
|
+
const messages = new Set();
|
|
292
|
+
const tools = new Set();
|
|
293
|
+
const diagnostics = [];
|
|
294
|
+
for (const event of events) {
|
|
295
|
+
switch (event.type) {
|
|
296
|
+
case "turn_started":
|
|
297
|
+
if (event.turnId)
|
|
298
|
+
turns.add(event.turnId);
|
|
299
|
+
break;
|
|
300
|
+
case "turn_finished":
|
|
301
|
+
if (!event.turnId || !turns.has(event.turnId)) {
|
|
302
|
+
diagnostics.push(`turn_finished ${event.seq} precedes turn_started`);
|
|
303
|
+
}
|
|
304
|
+
break;
|
|
305
|
+
case "message_started":
|
|
306
|
+
if (!event.turnId || !turns.has(event.turnId)) {
|
|
307
|
+
diagnostics.push(`message_started ${event.seq} precedes turn_started`);
|
|
308
|
+
}
|
|
309
|
+
if (event.messageId)
|
|
310
|
+
messages.add(event.messageId);
|
|
311
|
+
break;
|
|
312
|
+
case "assistant_event":
|
|
313
|
+
if (!event.messageId || !messages.has(event.messageId)) {
|
|
314
|
+
diagnostics.push(`assistant_event ${event.seq} precedes message_started`);
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
case "message_finished": {
|
|
318
|
+
if (!event.messageId || !messages.has(event.messageId)) {
|
|
319
|
+
diagnostics.push(`message_finished ${event.seq} precedes message_started`);
|
|
320
|
+
}
|
|
321
|
+
const settled = event.payload.settled;
|
|
322
|
+
const entryId = event.payload.entryId;
|
|
323
|
+
if (settled === true && (!isNonEmptyString(entryId) || !entryIds.has(entryId))) {
|
|
324
|
+
diagnostics.push(`message_finished ${event.seq} references a missing entry`);
|
|
325
|
+
}
|
|
326
|
+
else if (settled !== true && entryId !== undefined) {
|
|
327
|
+
diagnostics.push(`message_finished ${event.seq} has entryId while unsettled`);
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
87
330
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
331
|
+
case "tool_execution_started":
|
|
332
|
+
if (!event.messageId || !messages.has(event.messageId)) {
|
|
333
|
+
diagnostics.push(`tool_execution_started ${event.seq} precedes message_started`);
|
|
334
|
+
}
|
|
335
|
+
if (event.toolCallId)
|
|
336
|
+
tools.add(event.toolCallId);
|
|
337
|
+
break;
|
|
338
|
+
case "tool_execution_updated":
|
|
339
|
+
case "tool_execution_finished":
|
|
340
|
+
if (!event.toolCallId || !tools.has(event.toolCallId)) {
|
|
341
|
+
diagnostics.push(`${event.type} ${event.seq} precedes tool_execution_started`);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
default:
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
91
347
|
}
|
|
348
|
+
return diagnostics;
|
|
349
|
+
}
|
|
350
|
+
function validateSessionCapture(capture) {
|
|
351
|
+
if (capture.schema !== SESSION_CAPTURE_SCHEMA ||
|
|
352
|
+
capture.eventSchema !== SESSION_EVENT_SCHEMA ||
|
|
353
|
+
!["recording", "complete", "failed"].includes(capture.status) ||
|
|
354
|
+
!Number.isSafeInteger(capture.eventCount) ||
|
|
355
|
+
capture.eventCount < 0 ||
|
|
356
|
+
!Number.isSafeInteger(capture.entryCount) ||
|
|
357
|
+
capture.entryCount < 0 ||
|
|
358
|
+
!Number.isSafeInteger(capture.lastEventSeq) ||
|
|
359
|
+
capture.lastEventSeq < 0) {
|
|
360
|
+
throw new Error("Invalid session capture projection");
|
|
361
|
+
}
|
|
362
|
+
if (capture.status === "failed" && capture.failure === undefined) {
|
|
363
|
+
throw new Error("Failed session capture requires failure details");
|
|
364
|
+
}
|
|
365
|
+
if (capture.status !== "failed" && capture.failure !== undefined) {
|
|
366
|
+
throw new Error("Only failed session capture may contain failure details");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async function readCompleteNdjson(filePath) {
|
|
370
|
+
let raw;
|
|
371
|
+
try {
|
|
372
|
+
raw = await fs.readFile(filePath, "utf8");
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return [];
|
|
376
|
+
}
|
|
377
|
+
const lines = raw.split("\n");
|
|
378
|
+
if (!raw.endsWith("\n")) {
|
|
379
|
+
lines.pop();
|
|
380
|
+
}
|
|
381
|
+
const records = [];
|
|
382
|
+
for (const line of lines) {
|
|
383
|
+
if (line.trim().length === 0) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
const value = JSON.parse(line);
|
|
388
|
+
if (!Number.isSafeInteger(value.seq) || value.seq < 1) {
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
records.push({ seq: value.seq });
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return records;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Encode the externalizable value positions of the state document. The
|
|
401
|
+
* in-memory state always holds raw values; the persisted copy may carry
|
|
402
|
+
* `$artifact` references instead of large strings.
|
|
403
|
+
*/
|
|
404
|
+
async function encodeRunState(state, artifacts) {
|
|
405
|
+
const results = Object.fromEntries(await Promise.all(Object.entries(state.results).map(async ([nodeId, result]) => [
|
|
406
|
+
nodeId,
|
|
407
|
+
"output" in result
|
|
408
|
+
? { ...result, output: await encodeValue(result.output, artifacts) }
|
|
409
|
+
: result,
|
|
410
|
+
])));
|
|
411
|
+
return {
|
|
412
|
+
...state,
|
|
413
|
+
input: await encodeValue(state.input, artifacts),
|
|
414
|
+
outputs: Object.fromEntries(await Promise.all(Object.entries(state.outputs).map(async ([nodeId, output]) => [
|
|
415
|
+
nodeId,
|
|
416
|
+
await encodeValue(output, artifacts),
|
|
417
|
+
]))),
|
|
418
|
+
results,
|
|
419
|
+
steps: await Promise.all(state.steps.map(async (step) => ({
|
|
420
|
+
...step,
|
|
421
|
+
prompt: (await encodeValue(step.prompt, artifacts)),
|
|
422
|
+
output: await encodeValue(step.output, artifacts),
|
|
423
|
+
}))),
|
|
424
|
+
...(state.finalOutput !== undefined
|
|
425
|
+
? { finalOutput: await encodeValue(state.finalOutput, artifacts) }
|
|
426
|
+
: {}),
|
|
427
|
+
};
|
|
92
428
|
}
|
|
93
429
|
/** Read a run bundle from disk. Returns null when the bundle is unreadable. */
|
|
94
430
|
export async function readRunBundle(runDir) {
|
|
95
431
|
const manifest = await readJsonFile(path.join(runDir, MANIFEST_PATH));
|
|
96
|
-
|
|
97
|
-
if (!manifest || !state || manifest.schema !== RUN_BUNDLE_SCHEMA) {
|
|
432
|
+
if (!manifest || manifest.schema !== RUN_BUNDLE_SCHEMA) {
|
|
98
433
|
return null;
|
|
99
434
|
}
|
|
100
|
-
|
|
101
|
-
|
|
435
|
+
// A schema-tagged manifest may still be malformed (e.g. hand-edited);
|
|
436
|
+
// treat anything unexpected as an unreadable bundle rather than throwing.
|
|
437
|
+
const paths = typeof manifest.paths === "object" && manifest.paths !== null ? manifest.paths : {};
|
|
438
|
+
const state = await readJsonFile(resolveBundlePath(runDir, paths.state, STATE_PATH));
|
|
439
|
+
if (!state || state.schema !== RUN_STATE_SCHEMA) {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
const snapshot = await readJsonFile(resolveBundlePath(runDir, paths.workflow, WORKFLOW_SNAPSHOT_PATH));
|
|
443
|
+
const sessionDir = resolveBundlePath(runDir, paths.session, SESSION_DIR);
|
|
444
|
+
const sessionBinding = await readJsonFile(path.join(sessionDir, "binding.json"));
|
|
445
|
+
const entries = await readNdjsonFile(path.join(sessionDir, "entries.ndjson"));
|
|
446
|
+
const events = await readNdjsonFile(path.join(sessionDir, "events.ndjson"));
|
|
447
|
+
const sessionCapture = await readJsonFile(path.join(sessionDir, "capture.json"));
|
|
448
|
+
const sessionIntegrity = assessSessionIntegrity({
|
|
449
|
+
binding: sessionBinding,
|
|
450
|
+
entries,
|
|
451
|
+
events,
|
|
452
|
+
capture: sessionCapture,
|
|
453
|
+
runTerminal: state.status !== "running",
|
|
454
|
+
});
|
|
455
|
+
return {
|
|
456
|
+
runDir,
|
|
457
|
+
manifest,
|
|
458
|
+
state,
|
|
459
|
+
snapshot,
|
|
460
|
+
sessionBinding,
|
|
461
|
+
sessionEntries: entries.records,
|
|
462
|
+
sessionEvents: events.records,
|
|
463
|
+
sessionCapture,
|
|
464
|
+
sessionIntegrity,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Resolve a manifest-relative path, rejecting anything that is not a string
|
|
469
|
+
* or escapes the bundle directory. Malformed manifests must degrade to an
|
|
470
|
+
* unreadable bundle, never to a thrown error that aborts a listing.
|
|
471
|
+
*/
|
|
472
|
+
function resolveBundlePath(runDir, relative, fallback) {
|
|
473
|
+
const candidate = path.resolve(runDir, typeof relative === "string" && relative ? relative : fallback);
|
|
474
|
+
if (candidate !== path.resolve(runDir) &&
|
|
475
|
+
!candidate.startsWith(path.resolve(runDir) + path.sep)) {
|
|
476
|
+
return path.join(runDir, fallback);
|
|
477
|
+
}
|
|
478
|
+
return candidate;
|
|
102
479
|
}
|
|
103
480
|
/** List run bundles under `outputRoot`, most recently started first. */
|
|
104
481
|
export async function listRunBundles(outputRoot) {
|
|
@@ -128,7 +505,102 @@ async function readJsonFile(filePath) {
|
|
|
128
505
|
return null;
|
|
129
506
|
}
|
|
130
507
|
}
|
|
131
|
-
function
|
|
508
|
+
async function readNdjsonFile(filePath) {
|
|
509
|
+
let raw;
|
|
510
|
+
try {
|
|
511
|
+
raw = await fs.readFile(filePath, "utf8");
|
|
512
|
+
}
|
|
513
|
+
catch {
|
|
514
|
+
return { records: [], exists: false, tornTail: false, malformed: false };
|
|
515
|
+
}
|
|
516
|
+
const tornTail = raw.length > 0 && !raw.endsWith("\n");
|
|
517
|
+
const lines = raw.split("\n");
|
|
518
|
+
if (tornTail) {
|
|
519
|
+
lines.pop();
|
|
520
|
+
}
|
|
521
|
+
const records = [];
|
|
522
|
+
let malformed = false;
|
|
523
|
+
for (const line of lines) {
|
|
524
|
+
if (line.trim().length === 0) {
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
records.push(JSON.parse(line));
|
|
529
|
+
}
|
|
530
|
+
catch {
|
|
531
|
+
malformed = true;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return { records, exists: true, tornTail, malformed };
|
|
535
|
+
}
|
|
536
|
+
function assessSessionIntegrity(input) {
|
|
537
|
+
const anySessionFile = input.binding !== null || input.entries.exists || input.events.exists || input.capture !== null;
|
|
538
|
+
if (!anySessionFile) {
|
|
539
|
+
return { status: "unavailable", diagnostics: [] };
|
|
540
|
+
}
|
|
541
|
+
const diagnostics = [];
|
|
542
|
+
if (!input.binding || input.binding.schema !== SESSION_BINDING_SCHEMA) {
|
|
543
|
+
diagnostics.push("missing or invalid session binding");
|
|
544
|
+
}
|
|
545
|
+
if (!input.capture) {
|
|
546
|
+
diagnostics.push("missing session capture status");
|
|
547
|
+
return { status: "invalid", diagnostics };
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
validateSessionCapture(input.capture);
|
|
551
|
+
}
|
|
552
|
+
catch (error) {
|
|
553
|
+
diagnostics.push(failureMessageForDiagnostic(error));
|
|
554
|
+
return { status: "invalid", diagnostics };
|
|
555
|
+
}
|
|
556
|
+
if (input.entries.malformed || input.events.malformed) {
|
|
557
|
+
diagnostics.push("malformed NDJSON line before the journal tail");
|
|
558
|
+
}
|
|
559
|
+
if (input.events.tornTail && input.capture.status !== "recording") {
|
|
560
|
+
diagnostics.push("terminal session event journal has a torn tail");
|
|
561
|
+
}
|
|
562
|
+
if (input.runTerminal && input.capture.status === "recording") {
|
|
563
|
+
diagnostics.push("terminal run still reports recording capture");
|
|
564
|
+
}
|
|
565
|
+
let expected = 1;
|
|
566
|
+
for (const event of input.events.records) {
|
|
567
|
+
try {
|
|
568
|
+
validateSessionEventRecord(event);
|
|
569
|
+
}
|
|
570
|
+
catch (error) {
|
|
571
|
+
diagnostics.push(failureMessageForDiagnostic(error));
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
if (event.seq !== expected) {
|
|
575
|
+
diagnostics.push(`session event sequence gap at ${expected}`);
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
expected += 1;
|
|
579
|
+
}
|
|
580
|
+
diagnostics.push(...sessionRelationshipDiagnostics(input.entries.records, input.events.records));
|
|
581
|
+
if (input.capture.status !== "recording") {
|
|
582
|
+
const lastEventSeq = input.events.records.at(-1)?.seq ?? 0;
|
|
583
|
+
if (input.capture.eventCount !== input.events.records.length ||
|
|
584
|
+
input.capture.entryCount !== input.entries.records.length ||
|
|
585
|
+
input.capture.lastEventSeq !== lastEventSeq) {
|
|
586
|
+
diagnostics.push("session capture counts do not match durable files");
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (diagnostics.length > 0) {
|
|
590
|
+
return { status: "invalid", diagnostics };
|
|
591
|
+
}
|
|
592
|
+
if (input.capture.status === "failed") {
|
|
593
|
+
return {
|
|
594
|
+
status: "failed",
|
|
595
|
+
diagnostics: [input.capture.failure?.message ?? "session capture failed"],
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
return { status: input.capture.status, diagnostics: [] };
|
|
599
|
+
}
|
|
600
|
+
function failureMessageForDiagnostic(error) {
|
|
601
|
+
return error instanceof Error ? error.message : String(error);
|
|
602
|
+
}
|
|
603
|
+
function createManifest(state, present) {
|
|
132
604
|
return {
|
|
133
605
|
schema: RUN_BUNDLE_SCHEMA,
|
|
134
606
|
runId: state.runId,
|
|
@@ -143,6 +615,11 @@ function createManifest(state) {
|
|
|
143
615
|
workflow: WORKFLOW_SNAPSHOT_PATH,
|
|
144
616
|
state: STATE_PATH,
|
|
145
617
|
trace: TRACE_PATH,
|
|
618
|
+
...(present.session ? { session: SESSION_DIR } : {}),
|
|
619
|
+
// Declare this before any payload can externalize a string. Live
|
|
620
|
+
// session-event patches may reference a newly written artifact before
|
|
621
|
+
// the next workflow state projection refreshes the manifest.
|
|
622
|
+
artifacts: "artifacts",
|
|
146
623
|
},
|
|
147
624
|
};
|
|
148
625
|
}
|
|
@@ -174,8 +651,11 @@ function snapshotNode(node) {
|
|
|
174
651
|
}
|
|
175
652
|
async function writeJsonAtomic(filePath, value) {
|
|
176
653
|
const tempPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
|
|
177
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
178
|
-
await fs.writeFile(tempPath, `${JSON.stringify(value, null, 2)}\n`,
|
|
654
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
655
|
+
await fs.writeFile(tempPath, `${JSON.stringify(value, null, 2)}\n`, {
|
|
656
|
+
encoding: "utf8",
|
|
657
|
+
mode: 0o600,
|
|
658
|
+
});
|
|
179
659
|
await fs.rename(tempPath, filePath);
|
|
180
660
|
}
|
|
181
661
|
//# sourceMappingURL=store.js.map
|