@leitwerk-dev/protocol 0.3.0 → 0.4.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.
Files changed (49) hide show
  1. package/dist/{chunk-LTIDCJFK.js → chunk-GOYGXRLL.js} +5 -1
  2. package/dist/chunk-GOYGXRLL.js.map +1 -0
  3. package/dist/{chunk-4AKWRRFS.js → chunk-JHJ6ACSD.js} +19 -2
  4. package/dist/chunk-JHJ6ACSD.js.map +1 -0
  5. package/dist/config-snapshot.d.ts +4 -0
  6. package/dist/config-snapshot.d.ts.map +1 -1
  7. package/dist/execution-inspection.d.ts +178 -0
  8. package/dist/execution-inspection.d.ts.map +1 -0
  9. package/dist/form-contract.d.ts +10 -0
  10. package/dist/form-contract.d.ts.map +1 -1
  11. package/dist/{http-contracts-public-rL4HFcxz.d.ts → http-contracts-public-BDrsnENx.d.ts} +435 -7
  12. package/dist/http-contracts-public.d.ts +3 -1
  13. package/dist/http-contracts-public.d.ts.map +1 -1
  14. package/dist/http-contracts-public.js +6 -4
  15. package/dist/http-contracts.d.ts +98 -5
  16. package/dist/http-contracts.d.ts.map +1 -1
  17. package/dist/http-contracts.js +4 -2
  18. package/dist/index.d.ts +5 -4
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +255 -249
  21. package/dist/index.js.map +1 -1
  22. package/dist/live-turn-projection.d.ts +30 -37
  23. package/dist/live-turn-projection.d.ts.map +1 -1
  24. package/dist/protocol.d.ts +13 -0
  25. package/dist/protocol.d.ts.map +1 -1
  26. package/dist/protocol.js +1 -1
  27. package/dist/scoped-settings.d.ts +116 -0
  28. package/dist/scoped-settings.d.ts.map +1 -0
  29. package/dist/skill-pack.d.ts +49 -0
  30. package/dist/skill-pack.d.ts.map +1 -0
  31. package/dist/ticket-creation-contracts.d.ts +6 -0
  32. package/dist/ticket-creation-contracts.d.ts.map +1 -1
  33. package/package.json +2 -2
  34. package/src/config-snapshot.ts +4 -0
  35. package/src/execution-inspection.ts +204 -0
  36. package/src/form-contract.ts +10 -0
  37. package/src/http-contracts-public.ts +16 -0
  38. package/src/http-contracts.ts +117 -3
  39. package/src/index.ts +10 -6
  40. package/src/live-turn-projection.ts +177 -173
  41. package/src/protocol.ts +13 -0
  42. package/src/scoped-settings.ts +148 -0
  43. package/src/skill-pack.ts +107 -0
  44. package/src/ticket-creation-contracts.ts +6 -0
  45. package/dist/chunk-4AKWRRFS.js.map +0 -1
  46. package/dist/chunk-LTIDCJFK.js.map +0 -1
  47. package/dist/turn-trace-projection.d.ts +0 -5
  48. package/dist/turn-trace-projection.d.ts.map +0 -1
  49. package/src/turn-trace-projection.ts +0 -42
@@ -3,13 +3,17 @@ import {
3
3
  readFiniteNumber as readNumber,
4
4
  readNonBlankString as readString,
5
5
  } from "@leitwerk-dev/domain";
6
+ import type { TurnTraceSnapshot, TurnTraceToolCallSnapshot } from "./http-contracts.js";
7
+ import { extractPiSessionMessageText } from "./pi-session-message.js";
6
8
  import type {
9
+ PrimaryPathActiveTurnSnapshot,
7
10
  PrimaryPathOperationalTraceItemSnapshot,
8
11
  PrimaryPathStreamingAssistantSnapshot,
9
12
  PrimaryPathToolCallSnapshot,
10
13
  PrimaryPathTraceItemSnapshot,
11
14
  } from "./primary-path-snapshot.js";
12
15
  import { compareTimestampStrings } from "./timestamp-ordering.js";
16
+ import { isToolResultTruncated } from "./tool-result-truncation.js";
13
17
  import {
14
18
  cloneUsageSnapshot,
15
19
  mergeUsageSnapshots,
@@ -28,22 +32,46 @@ import {
28
32
  type WsEventPayloadRecord,
29
33
  } from "./ws-event-payloads.js";
30
34
 
31
- /** @internal */
32
- export interface MutableLiveTurnProjection {
33
- /** @internal */
35
+ interface ProjectionToolCall extends PrimaryPathToolCallSnapshot {
36
+ restoredResult?: Pick<TurnTraceToolCallSnapshot, "resultText" | "truncated">;
37
+ }
38
+
39
+ interface LiveTurnProjectionState {
34
40
  assistant: PrimaryPathStreamingAssistantSnapshot;
35
- /** @internal */
36
- toolCalls: PrimaryPathToolCallSnapshot[];
37
- /** @internal */
38
41
  traceItems: PrimaryPathTraceItemSnapshot[];
39
- /** @internal */
40
42
  usage: UsageSnapshot | null;
43
+ toolCallsById: Map<string, ProjectionToolCall>;
44
+ nextFallbackOrdinal: number;
45
+ }
46
+
47
+ /** @internal */
48
+ export interface LiveTurnProjectionEvent {
41
49
  /** @internal */
42
- toolCallsById: Map<string, PrimaryPathToolCallSnapshot>;
50
+ eventType: string;
43
51
  /** @internal */
44
- openToolCallIdsByName: Map<string, string[]>;
52
+ data: WsEventPayloadRecord;
45
53
  /** @internal */
46
- nextFallbackOrdinal: number;
54
+ fallbackTimestamp: string;
55
+ }
56
+
57
+ /** @internal */
58
+ export type LiveTurnSnapshot = Pick<
59
+ PrimaryPathActiveTurnSnapshot,
60
+ "assistant" | "toolCalls" | "traceItems" | "usage"
61
+ >;
62
+
63
+ /** Projection of events already scoped and ordered by the caller. @internal */
64
+ export interface TurnTraceProjection {
65
+ /** Canonicalize and incorporate one event. Transport deduplication belongs to the caller. @internal */
66
+ apply(event: LiveTurnProjectionEvent): AppliedLiveTurnProjectionEvent;
67
+ /** Present retained content, preserving the restored prompt unless one is supplied. @internal */
68
+ snapshot(piInput?: TurnTraceSnapshot["piInput"]): TurnTraceSnapshot;
69
+ }
70
+
71
+ /** Event-backed projection retaining original tool results. @internal */
72
+ export interface LiveTurnProjection extends TurnTraceProjection {
73
+ /** Read the recorded result values as well as the projected activity. @internal */
74
+ rawSnapshot(): LiveTurnSnapshot;
47
75
  }
48
76
 
49
77
  /** @internal */
@@ -56,6 +84,8 @@ export interface AppliedLiveTurnProjectionEvent {
56
84
  toolCallId: string | null;
57
85
  /** @internal */
58
86
  toolName: string | null;
87
+ /** Aggregate usage without copying the full activity history. @internal */
88
+ usage: UsageSnapshot | null;
59
89
  }
60
90
 
61
91
  /** @internal */
@@ -67,11 +97,7 @@ export const PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES = [
67
97
  "pi.compaction.end",
68
98
  ] as const;
69
99
 
70
- const PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET = new Set<string>(
71
- PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES,
72
- );
73
-
74
- function cloneToolCall(toolCall: PrimaryPathToolCallSnapshot): PrimaryPathToolCallSnapshot {
100
+ function cloneToolCall(toolCall: ProjectionToolCall): ProjectionToolCall {
75
101
  return {
76
102
  ...toolCall,
77
103
  ...(toolCall.arguments ? { arguments: { ...toolCall.arguments } } : {}),
@@ -123,156 +149,98 @@ export function buildPrimaryPathOperationalTraceItem(input: {
123
149
  /** @internal */
124
150
  fallbackTimestamp: string;
125
151
  }): PrimaryPathOperationalTraceItemSnapshot | null {
126
- if (!PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET.has(input.eventType)) {
127
- return null;
128
- }
129
- const timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
152
+ let presentation: Pick<PrimaryPathOperationalTraceItemSnapshot, "severity" | "title" | "message">;
130
153
  if (input.eventType === "pi.error") {
131
- return {
132
- kind: "operational_event",
133
- eventType: input.eventType,
154
+ presentation = {
134
155
  severity: "error",
135
156
  title: "Pi request failed",
136
157
  message:
137
158
  firstMessage(input.data, ["message", "errorMessage", "finalError"]) ??
138
159
  "Pi reported an error.",
139
- timestamp,
140
160
  };
141
- }
142
- if (input.eventType === "pi.retry.start") {
143
- return {
144
- kind: "operational_event",
145
- eventType: input.eventType,
161
+ } else if (input.eventType === "pi.retry.start") {
162
+ presentation = {
146
163
  severity: "warning",
147
164
  title: "Retry scheduled",
148
165
  message:
149
166
  firstMessage(input.data, ["message", "errorMessage"]) ??
150
167
  retryStartFallbackMessage(input.data),
151
- timestamp,
152
168
  };
153
- }
154
- if (input.eventType === "pi.retry.end") {
169
+ } else if (input.eventType === "pi.retry.end") {
155
170
  const success = input.data.success === true;
156
- return {
157
- kind: "operational_event",
158
- eventType: input.eventType,
171
+ presentation = {
159
172
  severity: success ? "success" : "error",
160
173
  title: success ? "Retry succeeded" : "Retry exhausted",
161
174
  message:
162
175
  firstMessage(input.data, ["message", "finalError", "errorMessage"]) ??
163
176
  retryEndFallbackMessage(input.data),
164
- timestamp,
165
177
  };
166
- }
167
- if (input.eventType === "pi.compaction.start") {
178
+ } else if (input.eventType === "pi.compaction.start") {
168
179
  const reason = formatReason(input.data.reason);
169
- return {
170
- kind: "operational_event",
171
- eventType: input.eventType,
180
+ presentation = {
172
181
  severity: "info",
173
182
  title: "Context compaction started",
174
183
  message: reason
175
184
  ? `Pi started compacting context (${reason}).`
176
185
  : "Pi started compacting context.",
177
- timestamp,
178
186
  };
179
- }
180
- if (input.eventType === "pi.compaction.end") {
187
+ } else if (input.eventType === "pi.compaction.end") {
181
188
  const reason = formatReason(input.data.reason);
182
189
  const errorMessage = firstMessage(input.data, ["errorMessage", "message"]);
183
190
  const aborted = input.data.aborted === true;
184
- const willRetry = input.data.willRetry === true;
185
- const statusMessage = errorMessage
186
- ? errorMessage
187
- : aborted
188
- ? "Context compaction was cancelled."
189
- : `Pi compacted context${reason ? ` (${reason})` : ""}.${willRetry ? " Pi will continue automatically." : ""}`;
190
- return {
191
- kind: "operational_event",
192
- eventType: input.eventType,
191
+ presentation = {
193
192
  severity: errorMessage ? "error" : aborted ? "warning" : "success",
194
193
  title: errorMessage
195
194
  ? "Context compaction failed"
196
195
  : aborted
197
196
  ? "Context compaction cancelled"
198
197
  : "Context compacted",
199
- message: statusMessage,
200
- timestamp,
198
+ message:
199
+ errorMessage ??
200
+ (aborted
201
+ ? "Context compaction was cancelled."
202
+ : `Pi compacted context${reason ? ` (${reason})` : ""}.${input.data.willRetry === true ? " Pi will continue automatically." : ""}`),
201
203
  };
202
- }
203
- return null;
204
- }
205
-
206
- function addOpenToolCallId(
207
- projection: MutableLiveTurnProjection,
208
- toolName: string,
209
- toolCallId: string,
210
- ) {
211
- const ids = projection.openToolCallIdsByName.get(toolName) ?? [];
212
- ids.push(toolCallId);
213
- projection.openToolCallIdsByName.set(toolName, ids);
214
- }
215
-
216
- function consumeLatestOpenToolCallId(
217
- projection: MutableLiveTurnProjection,
218
- toolName: string,
219
- ): string | null {
220
- const ids = projection.openToolCallIdsByName.get(toolName);
221
- if (!ids || ids.length === 0) {
222
- return null;
223
- }
224
- const toolCallId = ids.pop() ?? null;
225
- if (ids.length === 0) {
226
- projection.openToolCallIdsByName.delete(toolName);
227
204
  } else {
228
- projection.openToolCallIdsByName.set(toolName, ids);
229
- }
230
- return toolCallId;
231
- }
232
-
233
- function removeOpenToolCallId(projection: MutableLiveTurnProjection, toolCallId: string) {
234
- for (const [toolName, ids] of projection.openToolCallIdsByName.entries()) {
235
- const nextIds = ids.filter((candidate) => candidate !== toolCallId);
236
- if (nextIds.length === ids.length) {
237
- continue;
238
- }
239
- if (nextIds.length === 0) {
240
- projection.openToolCallIdsByName.delete(toolName);
241
- } else {
242
- projection.openToolCallIdsByName.set(toolName, nextIds);
243
- }
244
- return;
205
+ return null;
245
206
  }
207
+ return {
208
+ kind: "operational_event",
209
+ eventType: input.eventType,
210
+ timestamp: readWsEventTimestamp(input.data, input.fallbackTimestamp),
211
+ ...presentation,
212
+ };
246
213
  }
247
214
 
248
215
  function canonicalizeToolCallId(input: {
249
- projection: MutableLiveTurnProjection;
216
+ projection: LiveTurnProjectionState;
250
217
  data: WsEventPayloadRecord;
251
218
  timestamp: string;
252
219
  toolName: string;
253
220
  preferOpenCall: boolean;
254
221
  }): string {
255
222
  const explicitToolCallId = readWsEventToolCallId(input.data);
256
- if (explicitToolCallId) {
257
- if (input.preferOpenCall) {
258
- removeOpenToolCallId(input.projection, explicitToolCallId);
259
- }
260
- return explicitToolCallId;
261
- }
223
+ if (explicitToolCallId) return explicitToolCallId;
262
224
  if (input.preferOpenCall) {
263
- const openToolCallId = consumeLatestOpenToolCallId(input.projection, input.toolName);
264
- if (openToolCallId) {
265
- return openToolCallId;
266
- }
225
+ const openToolCall = [...input.projection.toolCallsById.values()]
226
+ .reverse()
227
+ .find((tool) => tool.status === "running" && tool.toolName === input.toolName);
228
+ if (openToolCall) return openToolCall.toolCallId;
267
229
  }
268
- return resolveWsEventToolCallId(input.data, {
269
- timestamp: input.timestamp,
270
- toolName: input.toolName,
271
- ordinal: input.projection.nextFallbackOrdinal++,
272
- });
230
+ // Replayed canonical IDs and restored snapshots do not retain the old ordinal.
231
+ // Reserve their identities by checking the index, without interpreting ID strings.
232
+ let toolCallId: string;
233
+ do {
234
+ toolCallId = resolveWsEventToolCallId(input.data, {
235
+ timestamp: input.timestamp,
236
+ toolName: input.toolName,
237
+ ordinal: input.projection.nextFallbackOrdinal++,
238
+ });
239
+ } while (input.projection.toolCallsById.has(toolCallId));
240
+ return toolCallId;
273
241
  }
274
242
 
275
- function appendThinkingTraceText(projection: MutableLiveTurnProjection, text: string): void {
243
+ function appendThinkingTraceText(projection: LiveTurnProjectionState, text: string): void {
276
244
  if (text.length === 0) {
277
245
  return;
278
246
  }
@@ -287,7 +255,7 @@ function appendThinkingTraceText(projection: MutableLiveTurnProjection, text: st
287
255
  });
288
256
  }
289
257
 
290
- function ensureToolTraceItem(projection: MutableLiveTurnProjection, toolCallId: string): void {
258
+ function ensureToolTraceItem(projection: LiveTurnProjectionState, toolCallId: string): void {
291
259
  if (
292
260
  projection.traceItems.some(
293
261
  (traceItem) => traceItem.kind === "tool_call" && traceItem.toolCallId === toolCallId,
@@ -301,55 +269,42 @@ function ensureToolTraceItem(projection: MutableLiveTurnProjection, toolCallId:
301
269
  });
302
270
  }
303
271
 
304
- /** @internal */
305
- export function createMutableLiveTurnProjection(): MutableLiveTurnProjection {
272
+ function createProjectionState(): LiveTurnProjectionState {
306
273
  return {
307
274
  assistant: {
308
275
  text: "",
309
276
  thinking: "",
310
277
  lastUpdatedAt: null,
311
278
  },
312
- toolCalls: [],
313
279
  traceItems: [],
314
280
  usage: null,
315
- toolCallsById: new Map<string, PrimaryPathToolCallSnapshot>(),
316
- openToolCallIdsByName: new Map<string, string[]>(),
281
+ toolCallsById: new Map<string, ProjectionToolCall>(),
317
282
  nextFallbackOrdinal: 1,
318
283
  };
319
284
  }
320
285
 
321
- /** @internal */
322
- export function snapshotLiveTurnProjection(projection: MutableLiveTurnProjection): {
323
- /** @internal */
324
- assistant: PrimaryPathStreamingAssistantSnapshot;
325
- /** @internal */
326
- toolCalls: PrimaryPathToolCallSnapshot[];
327
- /** @internal */
328
- traceItems: PrimaryPathTraceItemSnapshot[];
329
- /** @internal */
330
- usage: UsageSnapshot | null;
331
- } {
286
+ function snapshotProjection<T>(
287
+ projection: LiveTurnProjectionState,
288
+ presentTool: (tool: ProjectionToolCall) => T,
289
+ ) {
332
290
  return {
333
291
  assistant: { ...projection.assistant },
334
- toolCalls: projection.toolCalls.map(cloneToolCall),
335
292
  traceItems: projection.traceItems.map((item) => ({ ...item })),
336
293
  usage: cloneUsageSnapshot(projection.usage),
294
+ toolCalls: Array.from(projection.toolCallsById.values(), (tool) =>
295
+ presentTool(cloneToolCall(tool)),
296
+ ),
337
297
  };
338
298
  }
339
299
 
340
- /** @internal */
341
- export function applyPiEventToLiveTurnProjection(
342
- projection: MutableLiveTurnProjection,
343
- input: {
344
- /** @internal */
345
- eventType: string;
346
- /** @internal */
347
- data: WsEventPayloadRecord;
348
- /** @internal */
349
- fallbackTimestamp: string;
350
- },
300
+ function applyPiEventToLiveTurnProjection(
301
+ projection: LiveTurnProjectionState,
302
+ input: LiveTurnProjectionEvent,
351
303
  ): AppliedLiveTurnProjectionEvent {
352
304
  let timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
305
+ let canonicalData = input.data;
306
+ let toolCallId: string | null = null;
307
+ let toolName: string | null = null;
353
308
  if (input.eventType === "pi.stream.delta") {
354
309
  const text = readWsEventStreamText(input.data);
355
310
  if (text) {
@@ -363,18 +318,18 @@ export function applyPiEventToLiveTurnProjection(
363
318
  }
364
319
  } else if (input.eventType === "pi.tool.call" || input.eventType === "pi.tool.result") {
365
320
  const isResult = input.eventType === "pi.tool.result";
366
- const toolName = readWsEventToolName(input.data);
367
- const toolCallId = canonicalizeToolCallId({
321
+ toolName = readWsEventToolName(input.data);
322
+ toolCallId = canonicalizeToolCallId({
368
323
  projection,
369
324
  data: input.data,
370
325
  timestamp,
371
326
  toolName,
372
327
  preferOpenCall: isResult,
373
328
  });
374
- const canonicalData =
329
+ canonicalData =
375
330
  readWsEventToolCallId(input.data) === toolCallId ? input.data : { ...input.data, toolCallId };
376
331
  const existingToolCall = projection.toolCallsById.get(toolCallId);
377
- const toolCall: PrimaryPathToolCallSnapshot = existingToolCall ?? {
332
+ const toolCall: ProjectionToolCall = existingToolCall ?? {
378
333
  toolCallId,
379
334
  toolName,
380
335
  status: "running",
@@ -384,33 +339,19 @@ export function applyPiEventToLiveTurnProjection(
384
339
  result: null,
385
340
  isError: false,
386
341
  };
387
- if (!existingToolCall) {
388
- projection.toolCalls.push(toolCall);
389
- projection.toolCallsById.set(toolCallId, toolCall);
390
- }
342
+ if (!existingToolCall) projection.toolCallsById.set(toolCallId, toolCall);
391
343
  if (isResult) {
392
344
  toolCall.status = "completed";
393
345
  toolCall.completedAt = timestamp;
394
346
  toolCall.result = canonicalData.result ?? null;
347
+ delete toolCall.restoredResult;
395
348
  toolCall.isError = canonicalData.isError === true;
396
- if (existingToolCall) removeOpenToolCallId(projection, toolCallId);
397
349
  }
398
350
  ensureToolTraceItem(projection, toolCallId);
399
- if (!isResult) addOpenToolCallId(projection, toolName, toolCallId);
400
- return {
401
- canonicalData,
402
- timestamp,
403
- toolCallId,
404
- toolName,
405
- };
406
351
  } else if (input.eventType === "pi.usage") {
407
352
  projection.usage = mergeUsageSnapshots(projection.usage, normalizeUsageSnapshot(input.data));
408
353
  } else {
409
- const operationalTraceItem = buildPrimaryPathOperationalTraceItem({
410
- eventType: input.eventType,
411
- data: input.data,
412
- fallbackTimestamp: input.fallbackTimestamp,
413
- });
354
+ const operationalTraceItem = buildPrimaryPathOperationalTraceItem(input);
414
355
  if (operationalTraceItem) {
415
356
  projection.traceItems.push(operationalTraceItem);
416
357
  timestamp = operationalTraceItem.timestamp;
@@ -418,10 +359,11 @@ export function applyPiEventToLiveTurnProjection(
418
359
  }
419
360
 
420
361
  return {
421
- canonicalData: input.data,
362
+ canonicalData,
422
363
  timestamp,
423
- toolCallId: null,
424
- toolName: null,
364
+ toolCallId,
365
+ toolName,
366
+ usage: cloneUsageSnapshot(projection.usage),
425
367
  };
426
368
  }
427
369
 
@@ -442,17 +384,79 @@ function compareProcessEventsChronologically(left: ProcessEvent, right: ProcessE
442
384
  return left.id.localeCompare(right.id);
443
385
  }
444
386
 
445
- /** @internal */
446
- export function buildLiveTurnProjectionFromEvents(
447
- events: readonly ProcessEvent[],
448
- ): MutableLiveTurnProjection {
449
- const projection = createMutableLiveTurnProjection();
387
+ function snapshotToolTrace({
388
+ result,
389
+ restoredResult,
390
+ ...tool
391
+ }: ProjectionToolCall): TurnTraceToolCallSnapshot {
392
+ if (restoredResult) return { ...tool, ...restoredResult };
393
+ const record = result && typeof result === "object" ? (result as Record<string, unknown>) : null;
394
+ const resultText =
395
+ result == null
396
+ ? null
397
+ : typeof result === "string"
398
+ ? result
399
+ : record && "content" in record
400
+ ? extractPiSessionMessageText(record.content)
401
+ : JSON.stringify(result, null, 2);
402
+ return {
403
+ ...tool,
404
+ resultText,
405
+ truncated: isToolResultTruncated({
406
+ resultText,
407
+ resultDetails: record?.details,
408
+ resultValue: result,
409
+ }),
410
+ };
411
+ }
412
+
413
+ function traceProjection(
414
+ state: LiveTurnProjectionState,
415
+ retainedPiInput: TurnTraceSnapshot["piInput"] = null,
416
+ ): TurnTraceProjection {
417
+ return {
418
+ apply: (event) => applyPiEventToLiveTurnProjection(state, event),
419
+ snapshot: (piInput = retainedPiInput) => ({
420
+ ...snapshotProjection(state, snapshotToolTrace),
421
+ piInput,
422
+ }),
423
+ };
424
+ }
425
+
426
+ /** Start empty, or replay recorded events in their persisted order. @internal */
427
+ export function createLiveTurnProjection(events: readonly ProcessEvent[] = []): LiveTurnProjection {
428
+ const state = createProjectionState();
450
429
  for (const event of [...events].sort(compareProcessEventsChronologically)) {
451
- applyPiEventToLiveTurnProjection(projection, {
430
+ applyPiEventToLiveTurnProjection(state, {
452
431
  eventType: event.eventType,
453
432
  data: asWsEventPayloadRecord(event.data),
454
433
  fallbackTimestamp: event.createdAt,
455
434
  });
456
435
  }
457
- return projection;
436
+ return {
437
+ ...traceProjection(state),
438
+ rawSnapshot: () =>
439
+ snapshotProjection(state, ({ restoredResult: _restoredResult, ...tool }) => tool),
440
+ };
441
+ }
442
+
443
+ /**
444
+ * Resume presentation from a Trace, including correlation for its running tools.
445
+ * A Trace retains displayed results, not the original structured tool results.
446
+ * @internal
447
+ */
448
+ export function restoreTurnTraceProjection(trace: TurnTraceSnapshot): TurnTraceProjection {
449
+ const state = createProjectionState();
450
+ state.assistant = { ...trace.assistant };
451
+ state.traceItems = trace.traceItems.map((item) => ({ ...item }));
452
+ state.usage = cloneUsageSnapshot(trace.usage);
453
+ for (const { resultText, truncated, ...tool } of trace.toolCalls) {
454
+ const toolCall = cloneToolCall({
455
+ ...tool,
456
+ result: null,
457
+ restoredResult: { resultText, truncated },
458
+ });
459
+ state.toolCallsById.set(toolCall.toolCallId, toolCall);
460
+ }
461
+ return traceProjection(state, trace.piInput);
458
462
  }
package/src/protocol.ts CHANGED
@@ -420,6 +420,15 @@ export type ProcessAttentionTarget =
420
420
 
421
421
  /** @internal */
422
422
  export type WsPayloadByType = {
423
+ /** @internal */
424
+ "wiki.updated": {
425
+ /** @internal */ topicId: string;
426
+ };
427
+ /** @internal */
428
+ "settings.updated": {
429
+ /** @internal */
430
+ subjectId: string;
431
+ };
423
432
  /** @internal */
424
433
  hello: {
425
434
  /** @internal */
@@ -700,6 +709,10 @@ export const WS_FRAME_DURABILITY = {
700
709
  /** @internal */
701
710
  "launch.updated": "durable",
702
711
  /** @internal */
712
+ "settings.updated": "durable",
713
+ /** @internal */
714
+ "wiki.updated": "durable",
715
+ /** @internal */
703
716
  "process.updated": "durable",
704
717
  /** @internal */
705
718
  "process.deleted": "durable",