@nanobpm/nano-workforce 0.171.0 → 0.171.2

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.
@@ -1,721 +1,19 @@
1
- // nano-workforce — the transcript EVENT vocabulary + the single derive() fold (ADR 0056, #251).
1
+ // nano-workforce — the transcript EVENT grammar, DERIVED from its one canonical owner (issue #676).
2
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.
3
+ // The transcript event-sourced session layer (ADR 0056, #251) the marker/version, the ONE parser,
4
+ // the merge-extensible vocabulary, the typed event union, and the single {@link deriveView} fold is
5
+ // now DEFINED once, in `@nanobpm/agentic/transcript` (published in agentic 0.10.0 via nano-ide#534).
6
+ // nano-workforce used to carry a byte-for-byte hand-rolled duplicate of that grammar here; two copies
7
+ // of one envelope grammar is exactly the DRIFT SURFACE our "Derivation Over Duplication" doctrine
8
+ // forbids — the day agentic's grammar evolved (a new event kind, a marker/version bump, a decoder fix)
9
+ // this fork would silently keep decoding by the old rules and the cockpit transcript view would
10
+ // diverge.
9
11
  //
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 overmirroring 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
- | "permission";
69
-
70
- /** The message roles the derived history distinguishes (assistant is authoritative for derivation). */
71
- export type TranscriptRole = "assistant" | "user" | "system" | "tool";
72
-
73
- /** Fields every typed event carries: its kind and the store offset it was decoded from. */
74
- interface TranscriptEventBase {
75
- readonly offset: number;
76
- }
77
-
78
- /** A raw terminal chunk retained verbatim for byte-level replay fidelity (the default classification). */
79
- export interface StreamChunkEvent extends TranscriptEventBase {
80
- readonly kind: "stream-chunk";
81
- /** The exact stored bytes — unmodified, so raw-byte replay stays faithful. */
82
- readonly chunk: string;
83
- }
84
-
85
- /** An assistant/user/system message — authoritative for the derived message history. */
86
- export interface MessageEvent extends TranscriptEventBase {
87
- readonly kind: "message";
88
- readonly role: TranscriptRole;
89
- readonly text: string;
90
- }
91
-
92
- /** A tool invocation the agent issued. */
93
- export interface ToolCallEvent extends TranscriptEventBase {
94
- readonly kind: "tool-call";
95
- readonly name: string;
96
- /** A stable id linking this call to its {@link ToolResultEvent}, when the producer supplies one. */
97
- readonly callId?: string;
98
- readonly args?: unknown;
99
- }
100
-
101
- /** A tool result, paired back to its {@link ToolCallEvent} by `callId` (else the most recent open call). */
102
- export interface ToolResultEvent extends TranscriptEventBase {
103
- readonly kind: "tool-result";
104
- readonly callId?: string;
105
- readonly ok: boolean;
106
- readonly content?: string;
107
- }
108
-
109
- /** A turn boundary — the start of a new request/response cycle. */
110
- export interface TurnEvent extends TranscriptEventBase {
111
- readonly kind: "turn";
112
- /** The producer's turn index, when supplied (else derived positionally). */
113
- readonly index?: number;
114
- }
115
-
116
- /** A step boundary within a turn (a tool loop iteration, a sub-agent hop, …). */
117
- export interface StepEvent extends TranscriptEventBase {
118
- readonly kind: "step";
119
- readonly label?: string;
120
- }
121
-
122
- /** A session lifecycle transition (open → completed, or an explicit exit). */
123
- export interface LifecycleEvent extends TranscriptEventBase {
124
- readonly kind: "lifecycle";
125
- readonly phase: "open" | "completed" | "exited";
126
- }
127
-
128
- // --- Permission (ACP `session/request_permission`) — SHARED CONTRACT (issue #559) ------------------
129
- // A `permission` event models ACP's `session/request_permission`: the agent asks the operator to
130
- // allow/deny a proposed action (usually a tool call), and the operator (or an auto policy) resolves it.
131
- // It is decoded here (the ONE parser) and folded here (the ONE fold) into a paired {@link DerivedPermission}.
132
- // These exported types are the SINGLE SOURCE OF TRUTH the sibling slices (cockpit render, escalation
133
- // bridge) consume — they must import these, never reinvent a divergent permission shape. See the durable
134
- // declaration in `app/contracts.ts` (`type:PermissionPolicy`, `wire:transcript.permission`).
135
-
136
- /**
137
- * The role's permission policy the PRODUCER tags a request with. `"escalate"` means a human must be
138
- * asked (the cockpit renders an Allow/Deny prompt, the escalation bridge raises a user task);
139
- * `"yolo"` means the action is auto-allowed and never prompts a human. Cockpit + bridge branch on this.
140
- */
141
- export type PermissionPolicy = "escalate" | "yolo";
142
-
143
- /** The kind of a permission option — mirrors ACP's option kinds (allow/reject × once/always). */
144
- export type PermissionOptionKind = "allow-once" | "allow-always" | "reject-once" | "reject-always";
145
-
146
- /** Pure, canonical: does a permission option kind ALLOW (true) or REJECT (false) the proposed action?
147
- * The `allow-*` vs `reject-*` prefix is the single source of truth. This lives beside
148
- * {@link PermissionOptionKind} so every consumer (the cockpit render seam and the permission-escalation
149
- * bridge) derives allow/deny from ONE implementation — the two paths can never disagree on what a
150
- * chosen option means (no drift surface). */
151
- export function optionKindAllows(kind: PermissionOptionKind): boolean {
152
- return kind === "allow-once" || kind === "allow-always";
153
- }
154
-
155
- /** One offered permission option (ACP `options[]` member): a stable id, a label, and its kind. */
156
- export interface PermissionOption {
157
- readonly optionId: string;
158
- readonly name: string;
159
- readonly kind: PermissionOptionKind;
160
- }
161
-
162
- /**
163
- * A permission REQUEST: the agent asks the operator to allow/deny a proposed action. The `callId`
164
- * pairs the eventual {@link PermissionResolutionEvent} back to this request (mirroring how
165
- * `tool-call`/`tool-result` pair by `callId`).
166
- */
167
- export interface PermissionRequestEvent extends TranscriptEventBase {
168
- readonly kind: "permission";
169
- readonly phase: "request";
170
- /** Stable id pairing this request to its resolution. */
171
- readonly callId: string;
172
- /** The producer-tagged policy the cockpit + bridge branch on. */
173
- readonly policy: PermissionPolicy;
174
- /** The offered options — always at least one (the decoder rejects an empty list). */
175
- readonly options: readonly PermissionOption[];
176
- /** The tool the proposed action would invoke, when known. */
177
- readonly toolName?: string;
178
- /** A short human-readable title for the prompt. */
179
- readonly title?: string;
180
- /** A longer human-readable reason for the prompt. */
181
- readonly reason?: string;
182
- }
183
-
184
- /**
185
- * A permission RESOLUTION: the operator's (or an auto policy's) decision, carrying the same `callId`,
186
- * the chosen `optionId`, and whether the action was `allowed`.
187
- */
188
- export interface PermissionResolutionEvent extends TranscriptEventBase {
189
- readonly kind: "permission";
190
- readonly phase: "resolution";
191
- /** The `callId` of the {@link PermissionRequestEvent} this resolves. */
192
- readonly callId: string;
193
- /** The chosen option's id. */
194
- readonly optionId: string;
195
- /** True = allowed, false = denied. */
196
- readonly allowed: boolean;
197
- /** Provenance of the decision, when supplied. */
198
- readonly by?: "operator" | "auto";
199
- }
200
-
201
- /** The core typed transcript-event union (merge-extensible: authors add kinds via the vocab). */
202
- export type TranscriptEvent =
203
- | StreamChunkEvent
204
- | MessageEvent
205
- | ToolCallEvent
206
- | ToolResultEvent
207
- | TurnEvent
208
- | StepEvent
209
- | LifecycleEvent
210
- | PermissionRequestEvent
211
- | PermissionResolutionEvent;
212
-
213
- /** A stored chunk as the store/read path exposes it (mirrors `TranscriptChunk`). */
214
- export interface StoredChunk {
215
- readonly offset: number;
216
- readonly chunk: string;
217
- }
218
-
219
- /**
220
- * A decoder for one event kind: given the parsed envelope body and the chunk offset, it returns the
221
- * typed event (or `undefined` to reject a malformed envelope, which then falls back to `stream-chunk`).
222
- * A vocabulary is the map kind → decoder; {@link mergeTranscriptVocab} extends it additively.
223
- */
224
- export type TranscriptEventDecoder = (body: Record<string, unknown>, offset: number) => TranscriptEvent | undefined;
225
-
226
- /** A transcript-event vocabulary: the ONE registry of kind → decoder the single parser consults. */
227
- export type TranscriptVocab = Readonly<Record<string, TranscriptEventDecoder>>;
228
-
229
- function str(body: Record<string, unknown>, key: string): string | undefined {
230
- const v = body[key];
231
- return typeof v === "string" ? v : undefined;
232
- }
233
-
234
- function num(body: Record<string, unknown>, key: string): number | undefined {
235
- const v = body[key];
236
- return typeof v === "number" && Number.isFinite(v) ? v : undefined;
237
- }
238
-
239
- const ROLES: readonly TranscriptRole[] = ["assistant", "user", "system", "tool"];
240
-
241
- /** Narrow an arbitrary string to a known {@link TranscriptRole}, defaulting to `assistant`. */
242
- function toRole(value: string | undefined): TranscriptRole {
243
- return ROLES.find((role) => role === value) ?? "assistant";
244
- }
245
-
246
- /** A structural guard: a non-null, non-array object is a plain record of unknown values. */
247
- function isRecord(value: unknown): value is Record<string, unknown> {
248
- return value !== null && typeof value === "object" && !Array.isArray(value);
249
- }
250
-
251
- const PERMISSION_OPTION_KINDS: readonly PermissionOptionKind[] = [
252
- "allow-once",
253
- "allow-always",
254
- "reject-once",
255
- "reject-always",
256
- ];
257
-
258
- /**
259
- * Decode ACP's `options[]` into typed {@link PermissionOption}s, or `undefined` if the array is
260
- * missing/empty or any member is malformed (so the whole request envelope is rejected → `stream-chunk`).
261
- */
262
- function decodePermissionOptions(value: unknown): PermissionOption[] | undefined {
263
- if (!Array.isArray(value) || value.length === 0) return undefined;
264
- const options: PermissionOption[] = [];
265
- for (const raw of value) {
266
- if (!isRecord(raw)) return undefined;
267
- const optionId = str(raw, "optionId");
268
- const name = str(raw, "name");
269
- const kindRaw = str(raw, "kind");
270
- const kind = PERMISSION_OPTION_KINDS.find((k) => k === kindRaw);
271
- if (optionId === undefined || name === undefined || kind === undefined) return undefined;
272
- options.push({ optionId, name, kind });
273
- }
274
- return options;
275
- }
276
-
277
- /**
278
- * The opinionated core vocabulary — the built-in event kinds every consumer understands out of the
279
- * box. Authors extend it in the SAME schema via {@link mergeTranscriptVocab}; they never fork the
280
- * parser. (`stream-chunk` is not decoded here — it is the fallback the parser applies to any chunk
281
- * that is not a well-formed typed envelope, so raw fidelity needs no decoder.)
282
- */
283
- export const CORE_TRANSCRIPT_VOCAB: TranscriptVocab = Object.freeze({
284
- message: (body, offset) => {
285
- const text = str(body, "text");
286
- if (text === undefined) return undefined;
287
- const roleRaw = str(body, "role");
288
- return { kind: "message", offset, role: toRole(roleRaw), text };
289
- },
290
- "tool-call": (body, offset) => {
291
- const name = str(body, "name");
292
- if (name === undefined) return undefined;
293
- const event: ToolCallEvent = { kind: "tool-call", offset, name };
294
- const callId = str(body, "callId");
295
- return {
296
- ...event,
297
- ...(callId !== undefined ? { callId } : {}),
298
- ...("args" in body ? { args: body.args } : {}),
299
- };
300
- },
301
- "tool-result": (body, offset) => {
302
- const ok = typeof body.ok === "boolean" ? body.ok : true;
303
- const event: ToolResultEvent = { kind: "tool-result", offset, ok };
304
- const callId = str(body, "callId");
305
- const content = str(body, "content");
306
- return {
307
- ...event,
308
- ...(callId !== undefined ? { callId } : {}),
309
- ...(content !== undefined ? { content } : {}),
310
- };
311
- },
312
- // ACP `plan` mapping: ACP `session/update` plan updates map onto the EXISTING `step`/`turn`
313
- // vocabulary rather than a new kind — an ACP plan ENTRY becomes a `step` (its `label` is the plan
314
- // entry's title; the entry ordinal is not preserved, as `StepEvent` carries only a `label`), and a
315
- // plan/turn BOUNDARY becomes a `turn` (its `index` the ACP turn/plan ordinal). The decoders below
316
- // already cope with an ACP-shaped `label` (`step`) / `index` (`turn`), so no new kind is needed.
317
- turn: (body, offset) => {
318
- const index = num(body, "index");
319
- return index !== undefined ? { kind: "turn", offset, index } : { kind: "turn", offset };
320
- },
321
- step: (body, offset) => {
322
- const label = str(body, "label");
323
- return label !== undefined ? { kind: "step", offset, label } : { kind: "step", offset };
324
- },
325
- lifecycle: (body, offset) => {
326
- const phase = str(body, "phase");
327
- if (phase !== "open" && phase !== "completed" && phase !== "exited") return undefined;
328
- return { kind: "lifecycle", offset, phase };
329
- },
330
- // A single `permission` decoder handles BOTH shapes (never a parser fork), branching on `phase`.
331
- // Malformed envelopes return `undefined` and fall back to `stream-chunk`, like the other decoders.
332
- permission: (body, offset) => {
333
- const callId = str(body, "callId");
334
- if (callId === undefined) return undefined;
335
- const phase = str(body, "phase");
336
- if (phase === "request") {
337
- const policy = str(body, "policy");
338
- if (policy !== "escalate" && policy !== "yolo") return undefined;
339
- const options = decodePermissionOptions(body.options);
340
- if (options === undefined) return undefined;
341
- const toolName = str(body, "toolName");
342
- const title = str(body, "title");
343
- const reason = str(body, "reason");
344
- const event: PermissionRequestEvent = { kind: "permission", phase: "request", offset, callId, policy, options };
345
- return {
346
- ...event,
347
- ...(toolName !== undefined ? { toolName } : {}),
348
- ...(title !== undefined ? { title } : {}),
349
- ...(reason !== undefined ? { reason } : {}),
350
- };
351
- }
352
- if (phase === "resolution") {
353
- const optionId = str(body, "optionId");
354
- if (optionId === undefined) return undefined;
355
- if (typeof body.allowed !== "boolean") return undefined;
356
- const by = str(body, "by");
357
- // Reject a malformed `by` rather than silently dropping it: a present-but-unknown provenance is a
358
- // producer bug, and swallowing it would make the typed event diverge from the on-wire JSON. This
359
- // covers BOTH a present-but-non-string `by` (e.g. `by: 123`, where str() coerces to undefined) and
360
- // a string that isn't a known provenance — either way the on-wire `by` is present but invalid.
361
- if (body.by !== undefined && by === undefined) return undefined;
362
- if (by !== undefined && by !== "operator" && by !== "auto") return undefined;
363
- const event: PermissionResolutionEvent = {
364
- kind: "permission",
365
- phase: "resolution",
366
- offset,
367
- callId,
368
- optionId,
369
- allowed: body.allowed,
370
- };
371
- return { ...event, ...(by !== undefined ? { by } : {}) };
372
- }
373
- return undefined;
374
- },
375
- });
376
-
377
- /**
378
- * Extend a vocabulary additively: later entries win on a key clash, so an author can either register a
379
- * brand-new kind or deliberately override a core decoder. Returns a NEW frozen vocab — neither input is
380
- * mutated — so the core stays canonical. (Cribbed from dsh's merge-extensible taxonomy / the S3
381
- * `mergeVocab`: one schema, extended by merge, never a second parser.)
382
- */
383
- export function mergeTranscriptVocab(base: TranscriptVocab, ...extensions: TranscriptVocab[]): TranscriptVocab {
384
- return Object.freeze(Object.assign({}, base, ...extensions));
385
- }
386
-
387
- /**
388
- * THE ONE PARSER. Classify a single stored chunk into a typed {@link TranscriptEvent}.
389
- *
390
- * A chunk is decoded as a structured event ONLY when it is a JSON object carrying the
391
- * {@link TRANSCRIPT_EVENT_MARKER} at the current version AND a `kind` the vocab knows AND its decoder
392
- * accepts the body. Anything else — raw terminal bytes, non-JSON, a JSON value without the marker, an
393
- * unknown kind, a decoder rejection — is retained verbatim as a `stream-chunk`, so byte-level replay
394
- * fidelity is never lost. This is the SINGLE point at which raw bytes become typed events; every view
395
- * folds over the result of this function, so there is exactly one parser of the log.
396
- */
397
- export function parseTranscriptEvent(
398
- entry: StoredChunk,
399
- vocab: TranscriptVocab = CORE_TRANSCRIPT_VOCAB,
400
- ): TranscriptEvent {
401
- const raw: StreamChunkEvent = { kind: "stream-chunk", offset: entry.offset, chunk: entry.chunk };
402
- const body = decodeEnvelope(entry.chunk);
403
- if (body === undefined) return raw;
404
- const kind = typeof body.kind === "string" ? body.kind : undefined;
405
- if (kind === undefined) return raw;
406
- const decoder = vocab[kind];
407
- if (decoder === undefined) return raw;
408
- return decoder(body, entry.offset) ?? raw;
409
- }
410
-
411
- /**
412
- * Decode a chunk into a marker-tagged envelope body, or `undefined` when it is not one. Kept private
413
- * so `JSON.parse` of a chunk lives in exactly one place (the drift-guard depends on this).
414
- */
415
- function decodeEnvelope(chunk: string): Record<string, unknown> | undefined {
416
- // Cheap reject before the parse: a valid envelope is a JSON object mentioning the marker key.
417
- const trimmed = chunk.trimStart();
418
- if (!trimmed.startsWith("{") || !chunk.includes(TRANSCRIPT_EVENT_MARKER)) return undefined;
419
- let parsed: unknown;
420
- try {
421
- parsed = JSON.parse(chunk);
422
- } catch {
423
- return undefined;
424
- }
425
- if (!isRecord(parsed)) return undefined;
426
- return parsed[TRANSCRIPT_EVENT_MARKER] === TRANSCRIPT_EVENT_VERSION ? parsed : undefined;
427
- }
428
-
429
- /**
430
- * Encode a typed event into the stored-chunk wire form a structured producer appends. The inverse of
431
- * {@link parseTranscriptEvent} for every non-raw kind (a `stream-chunk` is stored as its own raw bytes,
432
- * so it is returned verbatim). Provided so producers and tests speak the one envelope grammar rather
433
- * than hand-rolling the marker — the derivation-over-duplication rule applied to the write side too.
434
- */
435
- export function encodeTranscriptEvent(event: TranscriptEvent): string {
436
- if (event.kind === "stream-chunk") return event.chunk;
437
- const { offset: _offset, ...rest } = event;
438
- return JSON.stringify({ [TRANSCRIPT_EVENT_MARKER]: TRANSCRIPT_EVENT_VERSION, ...rest });
439
- }
440
-
441
- /** A derived tool card: a tool-call paired with its result (result absent while the call is pending). */
442
- export interface DerivedTool {
443
- readonly name: string;
444
- readonly callId?: string;
445
- readonly args?: unknown;
446
- readonly offset: number;
447
- readonly result?: { readonly ok: boolean; readonly content?: string; readonly offset: number };
448
- }
449
-
450
- /** A derived message in the folded history. */
451
- export interface DerivedMessage {
452
- readonly role: TranscriptRole;
453
- readonly text: string;
454
- readonly offset: number;
455
- }
456
-
457
- /**
458
- * A derived permission: a permission REQUEST paired with its RESOLUTION by `callId` (resolution absent
459
- * while the request is still pending), mirroring how {@link DerivedTool} pairs a call with its result.
460
- * The cockpit and the escalation bridge read THIS — they never re-parse the log.
461
- */
462
- export interface DerivedPermission {
463
- readonly callId: string;
464
- readonly policy: PermissionPolicy;
465
- readonly options: readonly PermissionOption[];
466
- readonly toolName?: string;
467
- readonly title?: string;
468
- readonly reason?: string;
469
- readonly offset: number;
470
- /** The resolution, once present (pending request → `undefined`). */
471
- readonly resolved?: {
472
- readonly allowed: boolean;
473
- readonly optionId: string;
474
- readonly by?: "operator" | "auto";
475
- readonly offset: number;
476
- };
477
- }
478
-
479
- /** A derived turn: the messages, tool cards and step count folded within one turn boundary. */
480
- export interface DerivedTurn {
481
- readonly index: number;
482
- readonly startOffset: number;
483
- readonly messages: readonly DerivedMessage[];
484
- readonly tools: readonly DerivedTool[];
485
- readonly permissions: readonly DerivedPermission[];
486
- readonly steps: number;
487
- }
488
-
489
- /** The single derived view every higher-level consumer reads instead of re-parsing raw bytes. */
490
- export interface DerivedView {
491
- /** The per-turn structure (a turn is opened implicitly before the first turn event, if any content precedes it). */
492
- readonly turns: readonly DerivedTurn[];
493
- /** Every message across all turns, in offset order (the flat derived history). */
494
- readonly messages: readonly DerivedMessage[];
495
- /** Every tool card across all turns, in offset order. */
496
- readonly tools: readonly DerivedTool[];
497
- /** Every permission across all turns, in offset order (each request paired to its resolution by `callId`). */
498
- readonly permissions: readonly DerivedPermission[];
499
- /** Total retained raw bytes (UTF-8) across `stream-chunk` events — the byte-replay fidelity accounting. */
500
- readonly rawByteLength: number;
501
- /** Number of retained raw chunks. */
502
- readonly rawChunkCount: number;
503
- /** The session lifecycle as the last lifecycle event reports it (defaults to `open`). */
504
- readonly lifecycle: "open" | "completed" | "exited";
505
- /** Number of typed events folded. */
506
- readonly eventCount: number;
507
- }
508
-
509
- interface MutableTurn {
510
- index: number;
511
- startOffset: number;
512
- messages: DerivedMessage[];
513
- tools: DerivedTool[];
514
- permissions: DerivedPermission[];
515
- steps: number;
516
- }
517
-
518
- /**
519
- * THE SINGLE FOLD. Derive every higher-level view from the typed event log — "the log IS the state".
520
- *
521
- * Folds the events (assumed in offset order — the store's append order) into per-turn structure, a flat
522
- * message history, tool cards (each call paired to its result by `callId`, else the most recent open
523
- * call), raw-byte accounting for replay fidelity, and the session lifecycle. It is a pure reduction of
524
- * one log: the cockpit, search, token accounting and export all read THIS, so there is never a second
525
- * parser of the same bytes. Content that precedes the first explicit `turn` event opens an implicit
526
- * turn 0, so a producer that never emits turn boundaries still derives a coherent single-turn view.
527
- */
528
- export function deriveView(events: Iterable<TranscriptEvent>): DerivedView {
529
- const turns: MutableTurn[] = [];
530
- const messages: DerivedMessage[] = [];
531
- const tools: DerivedTool[] = [];
532
- const permissions: DerivedPermission[] = [];
533
- const openTools = new Map<string, DerivedTool>();
534
- let anonymousTool: DerivedTool | undefined;
535
- const openPermissions = new Map<string, DerivedPermission>();
536
- let rawByteLength = 0;
537
- let rawChunkCount = 0;
538
- let lifecycle: "open" | "completed" | "exited" = "open";
539
- let eventCount = 0;
540
- let current: MutableTurn | undefined;
541
-
542
- const ensureTurn = (offset: number): MutableTurn => {
543
- if (current === undefined) {
544
- current = { index: turns.length, startOffset: offset, messages: [], tools: [], permissions: [], steps: 0 };
545
- turns.push(current);
546
- }
547
- return current;
548
- };
549
-
550
- for (const event of events) {
551
- eventCount++;
552
- switch (event.kind) {
553
- case "turn": {
554
- current = {
555
- index: event.index ?? turns.length,
556
- startOffset: event.offset,
557
- messages: [],
558
- tools: [],
559
- permissions: [],
560
- steps: 0,
561
- };
562
- turns.push(current);
563
- break;
564
- }
565
- case "step": {
566
- ensureTurn(event.offset).steps++;
567
- break;
568
- }
569
- case "message": {
570
- const msg: DerivedMessage = { role: event.role, text: event.text, offset: event.offset };
571
- messages.push(msg);
572
- ensureTurn(event.offset).messages.push(msg);
573
- break;
574
- }
575
- case "tool-call": {
576
- const tool: DerivedTool = {
577
- name: event.name,
578
- offset: event.offset,
579
- ...(event.callId !== undefined ? { callId: event.callId } : {}),
580
- ...(event.args !== undefined ? { args: event.args } : {}),
581
- };
582
- tools.push(tool);
583
- ensureTurn(event.offset).tools.push(tool);
584
- if (event.callId !== undefined) openTools.set(event.callId, tool);
585
- else anonymousTool = tool;
586
- break;
587
- }
588
- case "tool-result": {
589
- const target = event.callId !== undefined ? openTools.get(event.callId) : anonymousTool;
590
- if (target !== undefined) {
591
- pairResult(tools, target, event);
592
- pairResultInTurns(turns, target, event);
593
- if (event.callId !== undefined) openTools.delete(event.callId);
594
- else anonymousTool = undefined;
595
- }
596
- break;
597
- }
598
- case "permission": {
599
- // A `permission` event is one of two phases (same discriminant `kind`); branch on `phase`. A
600
- // REQUEST opens a pending DerivedPermission (paired to its turn); a RESOLUTION folds back into
601
- // the open request by `callId` — mirroring the tool-call/tool-result open-map pairing above.
602
- if (event.phase === "request") {
603
- const permission: DerivedPermission = {
604
- policy: event.policy,
605
- options: event.options,
606
- offset: event.offset,
607
- callId: event.callId,
608
- ...(event.toolName !== undefined ? { toolName: event.toolName } : {}),
609
- ...(event.title !== undefined ? { title: event.title } : {}),
610
- ...(event.reason !== undefined ? { reason: event.reason } : {}),
611
- };
612
- permissions.push(permission);
613
- ensureTurn(event.offset).permissions.push(permission);
614
- openPermissions.set(event.callId, permission);
615
- } else {
616
- const target = openPermissions.get(event.callId);
617
- if (target !== undefined) {
618
- pairResolution(permissions, target, event);
619
- pairResolutionInTurns(turns, target, event);
620
- openPermissions.delete(event.callId);
621
- }
622
- }
623
- break;
624
- }
625
- case "lifecycle": {
626
- lifecycle = event.phase;
627
- break;
628
- }
629
- case "stream-chunk": {
630
- rawByteLength += utf8ByteLength(event.chunk);
631
- rawChunkCount++;
632
- break;
633
- }
634
- }
635
- }
636
-
637
- return {
638
- turns: turns.map((t) => ({
639
- index: t.index,
640
- startOffset: t.startOffset,
641
- messages: t.messages,
642
- tools: t.tools,
643
- permissions: t.permissions,
644
- steps: t.steps,
645
- })),
646
- messages,
647
- tools,
648
- permissions,
649
- rawByteLength,
650
- rawChunkCount,
651
- lifecycle,
652
- eventCount,
653
- };
654
- }
655
-
656
- /** Replace a pending tool with its result in the flat list. A pending tool starts as the same object in
657
- * both the flat list and its turn (pushed by reference), so {@link pairResultInTurns} locates it there by
658
- * identity; each list is then replaced independently with its own resolved copy via {@link withResult}. */
659
- function pairResult(list: DerivedTool[], target: DerivedTool, result: ToolResultEvent): void {
660
- const idx = list.indexOf(target);
661
- if (idx >= 0) list[idx] = withResult(target, result);
662
- }
663
-
664
- /** Replace a pending tool with its result inside whichever turn holds it. */
665
- function pairResultInTurns(turns: MutableTurn[], target: DerivedTool, result: ToolResultEvent): void {
666
- for (const turn of turns) {
667
- const idx = turn.tools.indexOf(target);
668
- if (idx >= 0) {
669
- turn.tools[idx] = withResult(target, result);
670
- return;
671
- }
672
- }
673
- }
674
-
675
- function withResult(tool: DerivedTool, result: ToolResultEvent): DerivedTool {
676
- return {
677
- ...tool,
678
- result: { ok: result.ok, offset: result.offset, ...(result.content !== undefined ? { content: result.content } : {}) },
679
- };
680
- }
681
-
682
- /** Replace a pending permission with its resolution in the flat list (by identity — see {@link pairResult}). */
683
- function pairResolution(list: DerivedPermission[], target: DerivedPermission, resolution: PermissionResolutionEvent): void {
684
- const idx = list.indexOf(target);
685
- if (idx >= 0) list[idx] = withResolution(target, resolution);
686
- }
687
-
688
- /** Replace a pending permission with its resolution inside whichever turn holds it. */
689
- function pairResolutionInTurns(turns: MutableTurn[], target: DerivedPermission, resolution: PermissionResolutionEvent): void {
690
- for (const turn of turns) {
691
- const idx = turn.permissions.indexOf(target);
692
- if (idx >= 0) {
693
- turn.permissions[idx] = withResolution(target, resolution);
694
- return;
695
- }
696
- }
697
- }
698
-
699
- function withResolution(permission: DerivedPermission, resolution: PermissionResolutionEvent): DerivedPermission {
700
- return {
701
- ...permission,
702
- resolved: {
703
- allowed: resolution.allowed,
704
- optionId: resolution.optionId,
705
- offset: resolution.offset,
706
- ...(resolution.by !== undefined ? { by: resolution.by } : {}),
707
- },
708
- };
709
- }
710
-
711
- /**
712
- * Convenience: parse a run of stored chunks into typed events through {@link parseTranscriptEvent} (the
713
- * one parser) and fold them with {@link deriveView} in a single call — the entry point a consumer uses
714
- * to go from stored bytes to a derived view without ever touching a second parser.
715
- */
716
- export function deriveViewFromChunks(chunks: Iterable<StoredChunk>, vocab: TranscriptVocab = CORE_TRANSCRIPT_VOCAB): DerivedView {
717
- function* parsed(): Generator<TranscriptEvent> {
718
- for (const entry of chunks) yield parseTranscriptEvent(entry, vocab);
719
- }
720
- return deriveView(parsed());
721
- }
12
+ // So this module is now a THIN RE-EXPORT BARREL over the single source of truth: every consumer
13
+ // (cockpit render, transcript-read, permission-bridge, token accounting, export) keeps importing the
14
+ // grammar from here, but the grammar itself lives in exactly one place agentic. There is NO local
15
+ // definition of the marker, NO local re-parse of a stored chunk, and NO forked vocabulary; the
16
+ // canonical `mergeTranscriptVocab` / `CORE_TRANSCRIPT_VOCAB` are re-exported so a future nwf-specific
17
+ // event kind stays an additive merge, never a fork of the parser. `transcript-events.drift.test.ts`
18
+ // pins this structurallyit fails if a local envelope grammar or a chunk re-parser ever reappears.
19
+ export * from "@nanobpm/agentic/transcript";