@nanobpm/nano-workforce 0.78.0 → 0.80.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/CHANGELOG.md +14 -0
- package/app/agentic/README.md +20 -0
- package/app/agentic/cockpit/index.ts +5 -0
- package/app/agentic/cockpit/transcript-derive.test.ts +78 -0
- package/app/agentic/cockpit/transcript-derive.ts +90 -0
- package/app/agentic/transcript-events.drift.test.ts +55 -0
- package/app/agentic/transcript-events.test.ts +186 -0
- package/app/agentic/transcript-events.ts +470 -0
- package/app/agentic/transcript-fork.test.ts +156 -0
- package/app/agentic/transcript-fork.ts +151 -0
- package/app/agentic/transcript-read.ts +2 -1
- package/app/delivery.test.ts +2 -1
- package/app/delivery.ts +76 -0
- package/app/instance-tracking.test.ts +2 -1
- package/app/lineage.test.ts +300 -0
- package/app/lineage.ts +537 -0
- package/app/migration037.test.ts +62 -0
- package/app/retro.ts +1 -1
- package/app/service.test.ts +104 -1
- package/app/service.ts +33 -71
- package/db/migrations/037_lineage.sql +69 -0
- package/openapi.yaml +120 -0
- package/operations/getLineage.test.ts +105 -0
- package/operations/getLineage.ts +32 -0
- package/package.json +1 -1
- package/pages/cockpit.page.json +1 -0
- package/pages/epic-detail.page.json +1 -0
- package/pages/epic.page.json +1 -0
- package/pages/feature.page.json +1 -0
- package/pages/home.page.json +4 -0
- package/pages/lineage.page.json +146 -0
- package/pages/overview.page.json +1 -0
- package/pages/tasks.page.json +4 -0
- package/workers/converge-feature/worker.ts +1 -1
- package/workers/record-wave/worker.ts +2 -2
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
// nano-workforce — the transcript EVENT vocabulary + the single derive() fold (ADR 0056, #251).
|
|
2
|
+
//
|
|
3
|
+
// This is the "event-sourced session" layer over the H3 transcript store (#146/#222). The store is
|
|
4
|
+
// already append-only and offset-keyed — chunks are appended, never mutated — which is half of the
|
|
5
|
+
// dsh (DeepSeek Harness) event-sourced-session pattern. The gap it left is that chunks are opaque
|
|
6
|
+
// `TEXT`: every richer view (structured message history, tool cards, per-turn boundaries, token
|
|
7
|
+
// accounting) had to re-parse the raw frame bytes ad hoc, a DRIFT SURFACE (two parsers of the same
|
|
8
|
+
// bytes), which our "Derivation Over Duplication" doctrine forbids.
|
|
9
|
+
//
|
|
10
|
+
// This module closes that gap the way dsh does: the append-only log of TYPED events is the single
|
|
11
|
+
// source of truth, and every higher-level view is a DERIVATION of that one log via a single
|
|
12
|
+
// {@link deriveView} fold — "the log IS the state, so divergence is structurally impossible". A raw
|
|
13
|
+
// terminal chunk is retained verbatim as a `stream-chunk` event (byte-level replay fidelity is
|
|
14
|
+
// preserved); a producer that emits a structured, marker-tagged JSON envelope is decoded into the
|
|
15
|
+
// authoritative typed events (message / tool-call / tool-result / turn / step / lifecycle) the derived
|
|
16
|
+
// views fold over — mirroring dsh (raw chunks for token-replay, `assistant/message` authoritative).
|
|
17
|
+
//
|
|
18
|
+
// THE ONE PARSER. {@link parseTranscriptEvent} is the SINGLE place a stored chunk is classified into a
|
|
19
|
+
// typed event; every consumer (cockpit, search, token accounting, export) reads the derived view, not
|
|
20
|
+
// the raw bytes. A drift-guard test (`transcript-events.drift.test.ts`) asserts the event marker — and
|
|
21
|
+
// therefore the raw→event parse — appears in exactly this module, so a second parser cannot creep in.
|
|
22
|
+
//
|
|
23
|
+
// MERGE-EXTENSIBLE. The vocabulary is a small core ({@link CORE_TRANSCRIPT_VOCAB}) authors extend in the
|
|
24
|
+
// same schema with {@link mergeTranscriptVocab} (cribbed from dsh's merge-extensible event taxonomy and
|
|
25
|
+
// the S3 `mergeVocab`), so a new event kind is an additive merge, never a fork of the parser.
|
|
26
|
+
//
|
|
27
|
+
// Pure and side-effect-free: no I/O, unit-testable on Node, and it never touches the engine or a BPMN
|
|
28
|
+
// flow (ADR 0056: app-tier only, advisory).
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Runtime-safe UTF-8 byte length. This module is imported by cockpit code that runs in the BROWSER
|
|
32
|
+
* (via `cockpit/transcript-derive.ts`), where Node's `Buffer` global is not available — a bare
|
|
33
|
+
* `Buffer.byteLength` would throw at runtime when deriving the view for a replayed transcript. Prefer
|
|
34
|
+
* `Buffer` when present (Node) and fall back to `TextEncoder` (a Web/Node standard) otherwise, so the
|
|
35
|
+
* single derive fold is portable across both hosts. This is the one canonical UTF-8 byte-length
|
|
36
|
+
* implementation the transcript plane derives from (reused by `transcript-read.ts`).
|
|
37
|
+
*/
|
|
38
|
+
let cachedTextEncoder: TextEncoder | undefined;
|
|
39
|
+
export function utf8ByteLength(text: string): number {
|
|
40
|
+
if (typeof Buffer !== "undefined") return Buffer.byteLength(text, "utf8");
|
|
41
|
+
// Cache one TextEncoder in the browser hot path (folding many stream-chunk events) to avoid
|
|
42
|
+
// allocating a new encoder — and the GC pressure it creates — on every call.
|
|
43
|
+
cachedTextEncoder ??= new TextEncoder();
|
|
44
|
+
return cachedTextEncoder.encode(text).length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The reserved marker field that distinguishes a structured transcript-event envelope from raw
|
|
49
|
+
* terminal bytes. A stored chunk is decoded as a typed event ONLY when it is a JSON object carrying
|
|
50
|
+
* this field set to the schema version — otherwise it is retained verbatim as a raw `stream-chunk`, so
|
|
51
|
+
* a raw ANSI frame that happens to be valid JSON is never mis-classified. Namespaced to nano-workforce
|
|
52
|
+
* so it cannot collide with a producer's own payload keys.
|
|
53
|
+
*/
|
|
54
|
+
export const TRANSCRIPT_EVENT_MARKER = "nwfTranscriptEvent" as const;
|
|
55
|
+
|
|
56
|
+
/** The current transcript-event envelope schema version (the value {@link TRANSCRIPT_EVENT_MARKER} carries). */
|
|
57
|
+
export const TRANSCRIPT_EVENT_VERSION = 1 as const;
|
|
58
|
+
|
|
59
|
+
/** The core, merge-extensible transcript-event kinds (authors add more via {@link mergeTranscriptVocab}). */
|
|
60
|
+
export type TranscriptEventKind =
|
|
61
|
+
| "stream-chunk"
|
|
62
|
+
| "message"
|
|
63
|
+
| "tool-call"
|
|
64
|
+
| "tool-result"
|
|
65
|
+
| "turn"
|
|
66
|
+
| "step"
|
|
67
|
+
| "lifecycle";
|
|
68
|
+
|
|
69
|
+
/** The message roles the derived history distinguishes (assistant is authoritative for derivation). */
|
|
70
|
+
export type TranscriptRole = "assistant" | "user" | "system" | "tool";
|
|
71
|
+
|
|
72
|
+
/** Fields every typed event carries: its kind and the store offset it was decoded from. */
|
|
73
|
+
interface TranscriptEventBase {
|
|
74
|
+
readonly offset: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A raw terminal chunk retained verbatim for byte-level replay fidelity (the default classification). */
|
|
78
|
+
export interface StreamChunkEvent extends TranscriptEventBase {
|
|
79
|
+
readonly kind: "stream-chunk";
|
|
80
|
+
/** The exact stored bytes — unmodified, so raw-byte replay stays faithful. */
|
|
81
|
+
readonly chunk: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** An assistant/user/system message — authoritative for the derived message history. */
|
|
85
|
+
export interface MessageEvent extends TranscriptEventBase {
|
|
86
|
+
readonly kind: "message";
|
|
87
|
+
readonly role: TranscriptRole;
|
|
88
|
+
readonly text: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A tool invocation the agent issued. */
|
|
92
|
+
export interface ToolCallEvent extends TranscriptEventBase {
|
|
93
|
+
readonly kind: "tool-call";
|
|
94
|
+
readonly name: string;
|
|
95
|
+
/** A stable id linking this call to its {@link ToolResultEvent}, when the producer supplies one. */
|
|
96
|
+
readonly callId?: string;
|
|
97
|
+
readonly args?: unknown;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A tool result, paired back to its {@link ToolCallEvent} by `callId` (else the most recent open call). */
|
|
101
|
+
export interface ToolResultEvent extends TranscriptEventBase {
|
|
102
|
+
readonly kind: "tool-result";
|
|
103
|
+
readonly callId?: string;
|
|
104
|
+
readonly ok: boolean;
|
|
105
|
+
readonly content?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A turn boundary — the start of a new request/response cycle. */
|
|
109
|
+
export interface TurnEvent extends TranscriptEventBase {
|
|
110
|
+
readonly kind: "turn";
|
|
111
|
+
/** The producer's turn index, when supplied (else derived positionally). */
|
|
112
|
+
readonly index?: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A step boundary within a turn (a tool loop iteration, a sub-agent hop, …). */
|
|
116
|
+
export interface StepEvent extends TranscriptEventBase {
|
|
117
|
+
readonly kind: "step";
|
|
118
|
+
readonly label?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A session lifecycle transition (open → completed, or an explicit exit). */
|
|
122
|
+
export interface LifecycleEvent extends TranscriptEventBase {
|
|
123
|
+
readonly kind: "lifecycle";
|
|
124
|
+
readonly phase: "open" | "completed" | "exited";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The core typed transcript-event union (merge-extensible: authors add kinds via the vocab). */
|
|
128
|
+
export type TranscriptEvent =
|
|
129
|
+
| StreamChunkEvent
|
|
130
|
+
| MessageEvent
|
|
131
|
+
| ToolCallEvent
|
|
132
|
+
| ToolResultEvent
|
|
133
|
+
| TurnEvent
|
|
134
|
+
| StepEvent
|
|
135
|
+
| LifecycleEvent;
|
|
136
|
+
|
|
137
|
+
/** A stored chunk as the store/read path exposes it (mirrors `TranscriptChunk`). */
|
|
138
|
+
export interface StoredChunk {
|
|
139
|
+
readonly offset: number;
|
|
140
|
+
readonly chunk: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A decoder for one event kind: given the parsed envelope body and the chunk offset, it returns the
|
|
145
|
+
* typed event (or `undefined` to reject a malformed envelope, which then falls back to `stream-chunk`).
|
|
146
|
+
* A vocabulary is the map kind → decoder; {@link mergeTranscriptVocab} extends it additively.
|
|
147
|
+
*/
|
|
148
|
+
export type TranscriptEventDecoder = (body: Record<string, unknown>, offset: number) => TranscriptEvent | undefined;
|
|
149
|
+
|
|
150
|
+
/** A transcript-event vocabulary: the ONE registry of kind → decoder the single parser consults. */
|
|
151
|
+
export type TranscriptVocab = Readonly<Record<string, TranscriptEventDecoder>>;
|
|
152
|
+
|
|
153
|
+
function str(body: Record<string, unknown>, key: string): string | undefined {
|
|
154
|
+
const v = body[key];
|
|
155
|
+
return typeof v === "string" ? v : undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function num(body: Record<string, unknown>, key: string): number | undefined {
|
|
159
|
+
const v = body[key];
|
|
160
|
+
return typeof v === "number" && Number.isFinite(v) ? v : undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const ROLES: readonly TranscriptRole[] = ["assistant", "user", "system", "tool"];
|
|
164
|
+
|
|
165
|
+
/** Narrow an arbitrary string to a known {@link TranscriptRole}, defaulting to `assistant`. */
|
|
166
|
+
function toRole(value: string | undefined): TranscriptRole {
|
|
167
|
+
return ROLES.find((role) => role === value) ?? "assistant";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A structural guard: a non-null, non-array object is a plain record of unknown values. */
|
|
171
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
172
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The opinionated core vocabulary — the built-in event kinds every consumer understands out of the
|
|
177
|
+
* box. Authors extend it in the SAME schema via {@link mergeTranscriptVocab}; they never fork the
|
|
178
|
+
* parser. (`stream-chunk` is not decoded here — it is the fallback the parser applies to any chunk
|
|
179
|
+
* that is not a well-formed typed envelope, so raw fidelity needs no decoder.)
|
|
180
|
+
*/
|
|
181
|
+
export const CORE_TRANSCRIPT_VOCAB: TranscriptVocab = Object.freeze({
|
|
182
|
+
message: (body, offset) => {
|
|
183
|
+
const text = str(body, "text");
|
|
184
|
+
if (text === undefined) return undefined;
|
|
185
|
+
const roleRaw = str(body, "role");
|
|
186
|
+
return { kind: "message", offset, role: toRole(roleRaw), text };
|
|
187
|
+
},
|
|
188
|
+
"tool-call": (body, offset) => {
|
|
189
|
+
const name = str(body, "name");
|
|
190
|
+
if (name === undefined) return undefined;
|
|
191
|
+
const event: ToolCallEvent = { kind: "tool-call", offset, name };
|
|
192
|
+
const callId = str(body, "callId");
|
|
193
|
+
return {
|
|
194
|
+
...event,
|
|
195
|
+
...(callId !== undefined ? { callId } : {}),
|
|
196
|
+
...("args" in body ? { args: body.args } : {}),
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
"tool-result": (body, offset) => {
|
|
200
|
+
const ok = typeof body.ok === "boolean" ? body.ok : true;
|
|
201
|
+
const event: ToolResultEvent = { kind: "tool-result", offset, ok };
|
|
202
|
+
const callId = str(body, "callId");
|
|
203
|
+
const content = str(body, "content");
|
|
204
|
+
return {
|
|
205
|
+
...event,
|
|
206
|
+
...(callId !== undefined ? { callId } : {}),
|
|
207
|
+
...(content !== undefined ? { content } : {}),
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
turn: (body, offset) => {
|
|
211
|
+
const index = num(body, "index");
|
|
212
|
+
return index !== undefined ? { kind: "turn", offset, index } : { kind: "turn", offset };
|
|
213
|
+
},
|
|
214
|
+
step: (body, offset) => {
|
|
215
|
+
const label = str(body, "label");
|
|
216
|
+
return label !== undefined ? { kind: "step", offset, label } : { kind: "step", offset };
|
|
217
|
+
},
|
|
218
|
+
lifecycle: (body, offset) => {
|
|
219
|
+
const phase = str(body, "phase");
|
|
220
|
+
if (phase !== "open" && phase !== "completed" && phase !== "exited") return undefined;
|
|
221
|
+
return { kind: "lifecycle", offset, phase };
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Extend a vocabulary additively: later entries win on a key clash, so an author can either register a
|
|
227
|
+
* brand-new kind or deliberately override a core decoder. Returns a NEW frozen vocab — neither input is
|
|
228
|
+
* mutated — so the core stays canonical. (Cribbed from dsh's merge-extensible taxonomy / the S3
|
|
229
|
+
* `mergeVocab`: one schema, extended by merge, never a second parser.)
|
|
230
|
+
*/
|
|
231
|
+
export function mergeTranscriptVocab(base: TranscriptVocab, ...extensions: TranscriptVocab[]): TranscriptVocab {
|
|
232
|
+
return Object.freeze(Object.assign({}, base, ...extensions));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* THE ONE PARSER. Classify a single stored chunk into a typed {@link TranscriptEvent}.
|
|
237
|
+
*
|
|
238
|
+
* A chunk is decoded as a structured event ONLY when it is a JSON object carrying the
|
|
239
|
+
* {@link TRANSCRIPT_EVENT_MARKER} at the current version AND a `kind` the vocab knows AND its decoder
|
|
240
|
+
* accepts the body. Anything else — raw terminal bytes, non-JSON, a JSON value without the marker, an
|
|
241
|
+
* unknown kind, a decoder rejection — is retained verbatim as a `stream-chunk`, so byte-level replay
|
|
242
|
+
* fidelity is never lost. This is the SINGLE point at which raw bytes become typed events; every view
|
|
243
|
+
* folds over the result of this function, so there is exactly one parser of the log.
|
|
244
|
+
*/
|
|
245
|
+
export function parseTranscriptEvent(
|
|
246
|
+
entry: StoredChunk,
|
|
247
|
+
vocab: TranscriptVocab = CORE_TRANSCRIPT_VOCAB,
|
|
248
|
+
): TranscriptEvent {
|
|
249
|
+
const raw: StreamChunkEvent = { kind: "stream-chunk", offset: entry.offset, chunk: entry.chunk };
|
|
250
|
+
const body = decodeEnvelope(entry.chunk);
|
|
251
|
+
if (body === undefined) return raw;
|
|
252
|
+
const kind = typeof body.kind === "string" ? body.kind : undefined;
|
|
253
|
+
if (kind === undefined) return raw;
|
|
254
|
+
const decoder = vocab[kind];
|
|
255
|
+
if (decoder === undefined) return raw;
|
|
256
|
+
return decoder(body, entry.offset) ?? raw;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Decode a chunk into a marker-tagged envelope body, or `undefined` when it is not one. Kept private
|
|
261
|
+
* so `JSON.parse` of a chunk lives in exactly one place (the drift-guard depends on this).
|
|
262
|
+
*/
|
|
263
|
+
function decodeEnvelope(chunk: string): Record<string, unknown> | undefined {
|
|
264
|
+
// Cheap reject before the parse: a valid envelope is a JSON object mentioning the marker key.
|
|
265
|
+
const trimmed = chunk.trimStart();
|
|
266
|
+
if (!trimmed.startsWith("{") || !chunk.includes(TRANSCRIPT_EVENT_MARKER)) return undefined;
|
|
267
|
+
let parsed: unknown;
|
|
268
|
+
try {
|
|
269
|
+
parsed = JSON.parse(chunk);
|
|
270
|
+
} catch {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
if (!isRecord(parsed)) return undefined;
|
|
274
|
+
return parsed[TRANSCRIPT_EVENT_MARKER] === TRANSCRIPT_EVENT_VERSION ? parsed : undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Encode a typed event into the stored-chunk wire form a structured producer appends. The inverse of
|
|
279
|
+
* {@link parseTranscriptEvent} for every non-raw kind (a `stream-chunk` is stored as its own raw bytes,
|
|
280
|
+
* so it is returned verbatim). Provided so producers and tests speak the one envelope grammar rather
|
|
281
|
+
* than hand-rolling the marker — the derivation-over-duplication rule applied to the write side too.
|
|
282
|
+
*/
|
|
283
|
+
export function encodeTranscriptEvent(event: TranscriptEvent): string {
|
|
284
|
+
if (event.kind === "stream-chunk") return event.chunk;
|
|
285
|
+
const { offset: _offset, ...rest } = event;
|
|
286
|
+
return JSON.stringify({ [TRANSCRIPT_EVENT_MARKER]: TRANSCRIPT_EVENT_VERSION, ...rest });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** A derived tool card: a tool-call paired with its result (result absent while the call is pending). */
|
|
290
|
+
export interface DerivedTool {
|
|
291
|
+
readonly name: string;
|
|
292
|
+
readonly callId?: string;
|
|
293
|
+
readonly args?: unknown;
|
|
294
|
+
readonly offset: number;
|
|
295
|
+
readonly result?: { readonly ok: boolean; readonly content?: string; readonly offset: number };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** A derived message in the folded history. */
|
|
299
|
+
export interface DerivedMessage {
|
|
300
|
+
readonly role: TranscriptRole;
|
|
301
|
+
readonly text: string;
|
|
302
|
+
readonly offset: number;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** A derived turn: the messages, tool cards and step count folded within one turn boundary. */
|
|
306
|
+
export interface DerivedTurn {
|
|
307
|
+
readonly index: number;
|
|
308
|
+
readonly startOffset: number;
|
|
309
|
+
readonly messages: readonly DerivedMessage[];
|
|
310
|
+
readonly tools: readonly DerivedTool[];
|
|
311
|
+
readonly steps: number;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The single derived view every higher-level consumer reads instead of re-parsing raw bytes. */
|
|
315
|
+
export interface DerivedView {
|
|
316
|
+
/** The per-turn structure (a turn is opened implicitly before the first turn event, if any content precedes it). */
|
|
317
|
+
readonly turns: readonly DerivedTurn[];
|
|
318
|
+
/** Every message across all turns, in offset order (the flat derived history). */
|
|
319
|
+
readonly messages: readonly DerivedMessage[];
|
|
320
|
+
/** Every tool card across all turns, in offset order. */
|
|
321
|
+
readonly tools: readonly DerivedTool[];
|
|
322
|
+
/** Total retained raw bytes (UTF-8) across `stream-chunk` events — the byte-replay fidelity accounting. */
|
|
323
|
+
readonly rawByteLength: number;
|
|
324
|
+
/** Number of retained raw chunks. */
|
|
325
|
+
readonly rawChunkCount: number;
|
|
326
|
+
/** The session lifecycle as the last lifecycle event reports it (defaults to `open`). */
|
|
327
|
+
readonly lifecycle: "open" | "completed" | "exited";
|
|
328
|
+
/** Number of typed events folded. */
|
|
329
|
+
readonly eventCount: number;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
interface MutableTurn {
|
|
333
|
+
index: number;
|
|
334
|
+
startOffset: number;
|
|
335
|
+
messages: DerivedMessage[];
|
|
336
|
+
tools: DerivedTool[];
|
|
337
|
+
steps: number;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* THE SINGLE FOLD. Derive every higher-level view from the typed event log — "the log IS the state".
|
|
342
|
+
*
|
|
343
|
+
* Folds the events (assumed in offset order — the store's append order) into per-turn structure, a flat
|
|
344
|
+
* message history, tool cards (each call paired to its result by `callId`, else the most recent open
|
|
345
|
+
* call), raw-byte accounting for replay fidelity, and the session lifecycle. It is a pure reduction of
|
|
346
|
+
* one log: the cockpit, search, token accounting and export all read THIS, so there is never a second
|
|
347
|
+
* parser of the same bytes. Content that precedes the first explicit `turn` event opens an implicit
|
|
348
|
+
* turn 0, so a producer that never emits turn boundaries still derives a coherent single-turn view.
|
|
349
|
+
*/
|
|
350
|
+
export function deriveView(events: Iterable<TranscriptEvent>): DerivedView {
|
|
351
|
+
const turns: MutableTurn[] = [];
|
|
352
|
+
const messages: DerivedMessage[] = [];
|
|
353
|
+
const tools: DerivedTool[] = [];
|
|
354
|
+
const openTools = new Map<string, DerivedTool>();
|
|
355
|
+
let anonymousTool: DerivedTool | undefined;
|
|
356
|
+
let rawByteLength = 0;
|
|
357
|
+
let rawChunkCount = 0;
|
|
358
|
+
let lifecycle: "open" | "completed" | "exited" = "open";
|
|
359
|
+
let eventCount = 0;
|
|
360
|
+
let current: MutableTurn | undefined;
|
|
361
|
+
|
|
362
|
+
const ensureTurn = (offset: number): MutableTurn => {
|
|
363
|
+
if (current === undefined) {
|
|
364
|
+
current = { index: turns.length, startOffset: offset, messages: [], tools: [], steps: 0 };
|
|
365
|
+
turns.push(current);
|
|
366
|
+
}
|
|
367
|
+
return current;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
for (const event of events) {
|
|
371
|
+
eventCount++;
|
|
372
|
+
switch (event.kind) {
|
|
373
|
+
case "turn": {
|
|
374
|
+
current = { index: event.index ?? turns.length, startOffset: event.offset, messages: [], tools: [], steps: 0 };
|
|
375
|
+
turns.push(current);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "step": {
|
|
379
|
+
ensureTurn(event.offset).steps++;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
case "message": {
|
|
383
|
+
const msg: DerivedMessage = { role: event.role, text: event.text, offset: event.offset };
|
|
384
|
+
messages.push(msg);
|
|
385
|
+
ensureTurn(event.offset).messages.push(msg);
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
case "tool-call": {
|
|
389
|
+
const tool: DerivedTool = {
|
|
390
|
+
name: event.name,
|
|
391
|
+
offset: event.offset,
|
|
392
|
+
...(event.callId !== undefined ? { callId: event.callId } : {}),
|
|
393
|
+
...(event.args !== undefined ? { args: event.args } : {}),
|
|
394
|
+
};
|
|
395
|
+
tools.push(tool);
|
|
396
|
+
ensureTurn(event.offset).tools.push(tool);
|
|
397
|
+
if (event.callId !== undefined) openTools.set(event.callId, tool);
|
|
398
|
+
else anonymousTool = tool;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case "tool-result": {
|
|
402
|
+
const target = event.callId !== undefined ? openTools.get(event.callId) : anonymousTool;
|
|
403
|
+
if (target !== undefined) {
|
|
404
|
+
pairResult(tools, target, event);
|
|
405
|
+
pairResultInTurns(turns, target, event);
|
|
406
|
+
if (event.callId !== undefined) openTools.delete(event.callId);
|
|
407
|
+
else anonymousTool = undefined;
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
case "lifecycle": {
|
|
412
|
+
lifecycle = event.phase;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
case "stream-chunk": {
|
|
416
|
+
rawByteLength += utf8ByteLength(event.chunk);
|
|
417
|
+
rawChunkCount++;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return {
|
|
424
|
+
turns: turns.map((t) => ({ index: t.index, startOffset: t.startOffset, messages: t.messages, tools: t.tools, steps: t.steps })),
|
|
425
|
+
messages,
|
|
426
|
+
tools,
|
|
427
|
+
rawByteLength,
|
|
428
|
+
rawChunkCount,
|
|
429
|
+
lifecycle,
|
|
430
|
+
eventCount,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Replace a pending tool with its result in the flat list. A pending tool starts as the same object in
|
|
435
|
+
* both the flat list and its turn (pushed by reference), so {@link pairResultInTurns} locates it there by
|
|
436
|
+
* identity; each list is then replaced independently with its own resolved copy via {@link withResult}. */
|
|
437
|
+
function pairResult(list: DerivedTool[], target: DerivedTool, result: ToolResultEvent): void {
|
|
438
|
+
const idx = list.indexOf(target);
|
|
439
|
+
if (idx >= 0) list[idx] = withResult(target, result);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Replace a pending tool with its result inside whichever turn holds it. */
|
|
443
|
+
function pairResultInTurns(turns: MutableTurn[], target: DerivedTool, result: ToolResultEvent): void {
|
|
444
|
+
for (const turn of turns) {
|
|
445
|
+
const idx = turn.tools.indexOf(target);
|
|
446
|
+
if (idx >= 0) {
|
|
447
|
+
turn.tools[idx] = withResult(target, result);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function withResult(tool: DerivedTool, result: ToolResultEvent): DerivedTool {
|
|
454
|
+
return {
|
|
455
|
+
...tool,
|
|
456
|
+
result: { ok: result.ok, offset: result.offset, ...(result.content !== undefined ? { content: result.content } : {}) },
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Convenience: parse a run of stored chunks into typed events through {@link parseTranscriptEvent} (the
|
|
462
|
+
* one parser) and fold them with {@link deriveView} in a single call — the entry point a consumer uses
|
|
463
|
+
* to go from stored bytes to a derived view without ever touching a second parser.
|
|
464
|
+
*/
|
|
465
|
+
export function deriveViewFromChunks(chunks: Iterable<StoredChunk>, vocab: TranscriptVocab = CORE_TRANSCRIPT_VOCAB): DerivedView {
|
|
466
|
+
function* parsed(): Generator<TranscriptEvent> {
|
|
467
|
+
for (const entry of chunks) yield parseTranscriptEvent(entry, vocab);
|
|
468
|
+
}
|
|
469
|
+
return deriveView(parsed());
|
|
470
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Unit tests for replay-by-reseed / fork of a transcript log (#251).
|
|
2
|
+
//
|
|
3
|
+
// Uses a real TranscriptStore over an in-memory node:sqlite db (the same double the relay-family suite
|
|
4
|
+
// uses), so the fork is exercised against the store's real idempotent, offset-keyed record/read path.
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { DatabaseSync } from "node:sqlite";
|
|
7
|
+
import { type SqliteDb, TranscriptStore } from "@nanobpm/agentic/transcript";
|
|
8
|
+
import { assert, assertEquals, assertThrows } from "#test-assert";
|
|
9
|
+
import { forkTranscript, TranscriptForkError } from "./transcript-fork.ts";
|
|
10
|
+
|
|
11
|
+
/** An in-memory {@link SqliteDb} over `node:sqlite`, matching the store's exec/run/all surface. */
|
|
12
|
+
function memoryDb(): SqliteDb {
|
|
13
|
+
const raw = new DatabaseSync(":memory:");
|
|
14
|
+
return {
|
|
15
|
+
exec: (sql) => raw.exec(sql),
|
|
16
|
+
run: (sql, params = []) => raw.prepare(sql).run(...params),
|
|
17
|
+
all: <T = Record<string, unknown>>(sql: string, params: unknown[] = []): T[] => raw.prepare(sql).all(...params) as T[],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function seededStore(): TranscriptStore {
|
|
22
|
+
const store = new TranscriptStore(memoryDb());
|
|
23
|
+
store.ensureSchema();
|
|
24
|
+
return store;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Record chunks c0..c(n-1) into `stream` and complete it (an "exited" ephemeral session). */
|
|
28
|
+
function recordExited(store: TranscriptStore, stream: string, n: number): void {
|
|
29
|
+
const entries = Array.from({ length: n }, (_, i) => ({ offset: i, chunk: `c${i}` }));
|
|
30
|
+
store.record(stream, entries, "ephemeral");
|
|
31
|
+
// Complete via a flush of a ring that reports the whole window, marking the stream completed.
|
|
32
|
+
store.flush(stream, { since: () => ({ entries }), nextOffset: n }, "ephemeral");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test("forkTranscript: seeds a new stream from the whole source log, offset-parity preserved", () => {
|
|
36
|
+
const store = seededStore();
|
|
37
|
+
recordExited(store, "job:src", 4);
|
|
38
|
+
|
|
39
|
+
const result = forkTranscript(store, "job:src", "fork:a");
|
|
40
|
+
assertEquals(result.seeded, 4);
|
|
41
|
+
assertEquals(result.throughOffset, 3);
|
|
42
|
+
assertEquals(result.stream, "fork:a");
|
|
43
|
+
// The fork replays the identical chunks at the identical offsets.
|
|
44
|
+
assertEquals(store.read("fork:a"), [
|
|
45
|
+
{ offset: 0, chunk: "c0" },
|
|
46
|
+
{ offset: 1, chunk: "c1" },
|
|
47
|
+
{ offset: 2, chunk: "c2" },
|
|
48
|
+
{ offset: 3, chunk: "c3" },
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("forkTranscript: throughOffset seeds only the prefix up to (and including) N", () => {
|
|
53
|
+
const store = seededStore();
|
|
54
|
+
recordExited(store, "job:src", 5);
|
|
55
|
+
|
|
56
|
+
const result = forkTranscript(store, "job:src", "fork:b", { throughOffset: 2 });
|
|
57
|
+
assertEquals(result.seeded, 3);
|
|
58
|
+
assertEquals(result.throughOffset, 2);
|
|
59
|
+
assertEquals(
|
|
60
|
+
store.read("fork:b").map((c) => c.offset),
|
|
61
|
+
[0, 1, 2],
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("forkTranscript: the branch is independent — appending to the source never touches the fork", () => {
|
|
66
|
+
const store = seededStore();
|
|
67
|
+
recordExited(store, "job:src", 3);
|
|
68
|
+
forkTranscript(store, "job:src", "fork:c", { throughOffset: 1, lifecycle: "long-lived" });
|
|
69
|
+
|
|
70
|
+
// Continue the fork with a divergent chunk, and separately grow a long-lived source.
|
|
71
|
+
store.record("fork:c", [{ offset: 2, chunk: "branch-continuation" }], "long-lived");
|
|
72
|
+
assertEquals(
|
|
73
|
+
store.read("fork:c").map((c) => c.chunk),
|
|
74
|
+
["c0", "c1", "branch-continuation"],
|
|
75
|
+
);
|
|
76
|
+
// The source is untouched by the fork's divergence.
|
|
77
|
+
assertEquals(
|
|
78
|
+
store.read("job:src").map((c) => c.chunk),
|
|
79
|
+
["c0", "c1", "c2"],
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("forkTranscript: a fork replays through the SAME resume-from-offset (since) path as a native stream", () => {
|
|
84
|
+
const store = seededStore();
|
|
85
|
+
recordExited(store, "job:src", 4);
|
|
86
|
+
forkTranscript(store, "job:src", "fork:d");
|
|
87
|
+
|
|
88
|
+
const slice = store.since("fork:d", 2);
|
|
89
|
+
assertEquals(slice.gap, false);
|
|
90
|
+
assertEquals(
|
|
91
|
+
slice.entries.map((c) => c.offset),
|
|
92
|
+
[2, 3],
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("forkTranscript: throughOffset below the log yields an empty — but real, listed — fork", () => {
|
|
97
|
+
const store = seededStore();
|
|
98
|
+
recordExited(store, "job:src", 3);
|
|
99
|
+
|
|
100
|
+
const result = forkTranscript(store, "job:src", "fork:empty", { throughOffset: -1 });
|
|
101
|
+
assertEquals(result.seeded, 0);
|
|
102
|
+
assertEquals(result.throughOffset, undefined);
|
|
103
|
+
assert(store.get("fork:empty") !== undefined);
|
|
104
|
+
assertEquals(store.read("fork:empty"), []);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("forkTranscript: refuses to fork a missing source", () => {
|
|
108
|
+
const store = seededStore();
|
|
109
|
+
assertThrows(() => forkTranscript(store, "job:nope", "fork:x"), TranscriptForkError, "no transcript");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("forkTranscript: refuses an existing target unless allowExisting is set", () => {
|
|
113
|
+
const store = seededStore();
|
|
114
|
+
recordExited(store, "job:src", 2);
|
|
115
|
+
forkTranscript(store, "job:src", "fork:e");
|
|
116
|
+
|
|
117
|
+
assertThrows(() => forkTranscript(store, "job:src", "fork:e"), TranscriptForkError, "already exists");
|
|
118
|
+
// With allowExisting the reseed is an idempotent no-op (offset-keyed record).
|
|
119
|
+
const again = forkTranscript(store, "job:src", "fork:e", { allowExisting: true });
|
|
120
|
+
assertEquals(again.seeded, 0);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("forkTranscript: allowExisting refuses a target whose contents diverge from the reseed prefix", () => {
|
|
124
|
+
const store = seededStore();
|
|
125
|
+
recordExited(store, "job:src", 3);
|
|
126
|
+
// A pre-existing target that carries DIFFERENT bytes at an overlapping offset — reseeding here would
|
|
127
|
+
// leave an interleaved mixture (offset-keyed record silently no-ops the divergent offset).
|
|
128
|
+
store.record("fork:diverge", [{ offset: 0, chunk: "not-c0" }], "ephemeral");
|
|
129
|
+
assertThrows(
|
|
130
|
+
() => forkTranscript(store, "job:src", "fork:diverge", { allowExisting: true }),
|
|
131
|
+
TranscriptForkError,
|
|
132
|
+
"does not match the reseed prefix",
|
|
133
|
+
);
|
|
134
|
+
// The target is left untouched — no partial interleave.
|
|
135
|
+
assertEquals(
|
|
136
|
+
store.read("fork:diverge").map((c) => c.chunk),
|
|
137
|
+
["not-c0"],
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("forkTranscript: allowExisting refuses a target opened under a different lifecycle", () => {
|
|
142
|
+
const store = seededStore();
|
|
143
|
+
recordExited(store, "job:src", 2);
|
|
144
|
+
store.open("fork:lc", "long-lived");
|
|
145
|
+
assertThrows(
|
|
146
|
+
() => forkTranscript(store, "job:src", "fork:lc", { allowExisting: true }),
|
|
147
|
+
TranscriptForkError,
|
|
148
|
+
"cannot reseed",
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("forkTranscript: refuses to fork a stream onto itself", () => {
|
|
153
|
+
const store = seededStore();
|
|
154
|
+
recordExited(store, "job:src", 1);
|
|
155
|
+
assertThrows(() => forkTranscript(store, "job:src", "job:src"), TranscriptForkError, "onto itself");
|
|
156
|
+
});
|