@linnlabs/linnkit 0.37.1 → 0.37.3

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 (57) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/cli.cjs +238 -242
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +238 -242
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager.cjs +198 -200
  7. package/dist/context-manager.cjs.map +1 -1
  8. package/dist/context-manager.js +198 -200
  9. package/dist/context-manager.js.map +1 -1
  10. package/dist/contracts/agentSpec.d.cts +30 -30
  11. package/dist/contracts/agentSpec.d.ts +30 -30
  12. package/dist/contracts/audit.d.cts +32 -32
  13. package/dist/contracts/audit.d.ts +32 -32
  14. package/dist/contracts/contextPolicy.d.cts +36 -36
  15. package/dist/contracts/contextPolicy.d.ts +36 -36
  16. package/dist/contracts/events.d.cts +1113 -1113
  17. package/dist/contracts/events.d.ts +1113 -1113
  18. package/dist/contracts/execution.d.cts +8 -8
  19. package/dist/contracts/execution.d.ts +8 -8
  20. package/dist/contracts/messages.d.cts +1178 -1178
  21. package/dist/contracts/messages.d.ts +1178 -1178
  22. package/dist/contracts/provider-continuation.d.cts +28 -28
  23. package/dist/contracts/provider-continuation.d.ts +28 -28
  24. package/dist/contracts/sse.d.cts +897 -897
  25. package/dist/contracts/sse.d.ts +897 -897
  26. package/dist/contracts/sub-run-trace-payload.d.cts +26 -26
  27. package/dist/contracts/sub-run-trace-payload.d.ts +26 -26
  28. package/dist/contracts/token-accounting.d.cts +38 -38
  29. package/dist/contracts/token-accounting.d.ts +38 -38
  30. package/dist/index.cjs +238 -242
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.js +238 -242
  33. package/dist/index.js.map +1 -1
  34. package/dist/quickstart.cjs +238 -242
  35. package/dist/quickstart.cjs.map +1 -1
  36. package/dist/quickstart.js +238 -242
  37. package/dist/quickstart.js.map +1 -1
  38. package/dist/runtime-kernel/events/agentEvents.d.cts +172 -172
  39. package/dist/runtime-kernel/events/agentEvents.d.ts +172 -172
  40. package/dist/runtime-kernel/events.cjs +1 -3
  41. package/dist/runtime-kernel/events.cjs.map +1 -1
  42. package/dist/runtime-kernel/events.js +1 -3
  43. package/dist/runtime-kernel/events.js.map +1 -1
  44. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.cts +1 -0
  45. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.ts +1 -0
  46. package/dist/runtime-kernel.cjs +238 -242
  47. package/dist/runtime-kernel.cjs.map +1 -1
  48. package/dist/runtime-kernel.js +238 -242
  49. package/dist/runtime-kernel.js.map +1 -1
  50. package/dist/testkit.cjs +238 -242
  51. package/dist/testkit.cjs.map +1 -1
  52. package/dist/testkit.js +238 -242
  53. package/dist/testkit.js.map +1 -1
  54. package/docs/integration/llm-provider.md +1 -0
  55. package/docs/integration/persistence.md +10 -0
  56. package/docs/integration/realtime.md +5 -0
  57. package/package.json +2 -2
@@ -5021,10 +5021,7 @@ function createExecuteLlmStage(dependencies) {
5021
5021
  const promptBudget = ctx.promptBudget;
5022
5022
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
5023
5023
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
5024
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
5025
- if (processedEvent) {
5026
- ctx.eventHandler?.(processedEvent);
5027
- }
5024
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
5028
5025
  } : void 0;
5029
5026
  const llmCallStartedAt = Date.now();
5030
5027
  let llmResp;
@@ -5180,9 +5177,6 @@ function processStreamEvent(event, outputProcessor) {
5180
5177
  return event;
5181
5178
  }
5182
5179
  const processedContent = outputProcessor.processStreamChunk(event.content);
5183
- if (processedContent.length === 0) {
5184
- return null;
5185
- }
5186
5180
  return {
5187
5181
  ...event,
5188
5182
  content: processedContent
@@ -6363,6 +6357,204 @@ var GraphAgentExecutor = class {
6363
6357
  }
6364
6358
  };
6365
6359
 
6360
+ // src/runtime-kernel/events/eventGovernance.ts
6361
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6362
+ "audit_envelope",
6363
+ "final_answer_chunk",
6364
+ "final_answer_reset",
6365
+ "subrun_trace",
6366
+ "context_usage_snapshot"
6367
+ ];
6368
+ function isRecord6(value) {
6369
+ return !!value && typeof value === "object" && !Array.isArray(value);
6370
+ }
6371
+ function isToolCallDecisionEvent(event) {
6372
+ return event.type === "tool_call_decision";
6373
+ }
6374
+ function isToolProcessEvent(event) {
6375
+ return event.type === "tool_process";
6376
+ }
6377
+ function isSubRunTraceRuntimeEvent(event) {
6378
+ return event.type === "subrun_trace";
6379
+ }
6380
+ function isRunExecutionMetricsRuntimeEvent(event) {
6381
+ return event.type === "run_execution_metrics";
6382
+ }
6383
+ function isContextUsageSnapshotRuntimeEvent(event) {
6384
+ return event.type === "context_usage_snapshot";
6385
+ }
6386
+ function isRequiresUserInteractionRuntimeEvent(event) {
6387
+ return event.type === "requires_user_interaction";
6388
+ }
6389
+ function isAuditEnvelopeRuntimeEvent(event) {
6390
+ return event.type === "audit_envelope";
6391
+ }
6392
+ function isControlRuntimeEvent(event) {
6393
+ return event.type === "control";
6394
+ }
6395
+ function isHiddenUserInputEvent(event) {
6396
+ if (event.type !== "user_input") {
6397
+ return false;
6398
+ }
6399
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6400
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6401
+ return ui?.presentation === "hidden";
6402
+ }
6403
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6404
+ if (event.type !== "thought" && event.type !== "final_answer") {
6405
+ return false;
6406
+ }
6407
+ return event.content.trim().length === 0;
6408
+ }
6409
+ function isConversationUiRuntimeEvent(event) {
6410
+ return event.lane === "foreground" && event.visibility === "conversation";
6411
+ }
6412
+ function shouldPersistRuntimeEvent(event) {
6413
+ return describeRuntimeEventLifecycle(event).persist;
6414
+ }
6415
+ function requirePersistableRuntimeEvent(event) {
6416
+ if (!shouldPersistRuntimeEvent(event)) {
6417
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6418
+ }
6419
+ return event;
6420
+ }
6421
+ function requirePersistableRoutedRuntimeEvent(event) {
6422
+ requirePersistableRuntimeEvent(event);
6423
+ return parseRoutedRuntimeEvent(event);
6424
+ }
6425
+ function shouldReplayRuntimeEventToUi(event) {
6426
+ return describeRuntimeEventLifecycle(event).replayToUi;
6427
+ }
6428
+ function shouldEnterAgentContext(event) {
6429
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6430
+ }
6431
+ function getRuntimeEventUiProjectionKind(event) {
6432
+ if (isHiddenUserInputEvent(event)) {
6433
+ return "hidden";
6434
+ }
6435
+ switch (event.type) {
6436
+ case "user_input":
6437
+ return "user_input";
6438
+ case "thought":
6439
+ return "thought";
6440
+ case "final_answer":
6441
+ return "final_answer";
6442
+ case "final_answer_chunk":
6443
+ return "final_answer_chunk";
6444
+ case "final_answer_reset":
6445
+ return "final_answer_reset";
6446
+ case "tool_call_decision":
6447
+ return "tool_call_decision";
6448
+ case "tool_process":
6449
+ return "tool_process";
6450
+ case "tool_output":
6451
+ return "tool_output";
6452
+ case "requires_user_interaction":
6453
+ return "requires_user_interaction";
6454
+ case "audit_envelope":
6455
+ return "audit_envelope";
6456
+ case "subrun_trace":
6457
+ return "subrun_trace";
6458
+ case "error":
6459
+ return "error";
6460
+ case "context_usage_snapshot":
6461
+ return "context_usage_snapshot";
6462
+ case "run_execution_metrics":
6463
+ return "run_execution_metrics";
6464
+ case "history_summary":
6465
+ return "history_summary";
6466
+ default:
6467
+ return "unsupported";
6468
+ }
6469
+ }
6470
+ function describeRuntimeEventLifecycle(event) {
6471
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6472
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6473
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6474
+ const enterAgentContext = (() => {
6475
+ if (uiProjectionKind === "unsupported") {
6476
+ return false;
6477
+ }
6478
+ if (isSubRunTraceRuntimeEvent(event)) {
6479
+ return false;
6480
+ }
6481
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6482
+ return false;
6483
+ }
6484
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6485
+ return false;
6486
+ }
6487
+ if (event.type === "error") {
6488
+ return false;
6489
+ }
6490
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6491
+ return false;
6492
+ }
6493
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6494
+ return false;
6495
+ }
6496
+ if (isControlRuntimeEvent(event)) {
6497
+ return false;
6498
+ }
6499
+ if (isHiddenUserInputEvent(event)) {
6500
+ return false;
6501
+ }
6502
+ if (isToolProcessEvent(event)) {
6503
+ return false;
6504
+ }
6505
+ if (event.type === "thought") {
6506
+ return false;
6507
+ }
6508
+ if (event.type === "final_answer_chunk") {
6509
+ return false;
6510
+ }
6511
+ if (event.type === "final_answer_reset") {
6512
+ return false;
6513
+ }
6514
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6515
+ return false;
6516
+ }
6517
+ return true;
6518
+ })();
6519
+ const realtimeChannel = (() => {
6520
+ switch (uiProjectionKind) {
6521
+ case "thought":
6522
+ case "final_answer":
6523
+ case "final_answer_chunk":
6524
+ case "final_answer_reset":
6525
+ case "tool_call_decision":
6526
+ case "tool_process":
6527
+ case "tool_output":
6528
+ case "requires_user_interaction":
6529
+ case "subrun_trace":
6530
+ case "error":
6531
+ case "context_usage_snapshot":
6532
+ case "run_execution_metrics":
6533
+ case "history_summary":
6534
+ return "event_bus_sse";
6535
+ case "audit_envelope":
6536
+ return "none";
6537
+ default:
6538
+ return "none";
6539
+ }
6540
+ })();
6541
+ return {
6542
+ uiProjectionKind,
6543
+ persist,
6544
+ replayToUi,
6545
+ enterAgentContext,
6546
+ realtimeChannel
6547
+ };
6548
+ }
6549
+ function shouldEmitRuntimeEventToSse(event) {
6550
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6551
+ }
6552
+
6553
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6554
+ function appendWorkingHistory(history, events) {
6555
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6556
+ }
6557
+
6366
6558
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6367
6559
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6368
6560
  const answerChanged = state.answerId !== answerId;
@@ -6447,7 +6639,7 @@ function initLlmNodeState(init) {
6447
6639
  };
6448
6640
  }
6449
6641
  function buildLocalPatch(nodeState, ctx) {
6450
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6642
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6451
6643
  const patch = {
6452
6644
  answerId: nodeState.answerId,
6453
6645
  chunkSeq: nodeState.chunkSeq,
@@ -6600,11 +6792,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
6600
6792
  });
6601
6793
 
6602
6794
  // src/runtime-kernel/events/provider-sidecar.ts
6603
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6795
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6604
6796
  function readMetaFromEvent(event) {
6605
- if (!isRecord6(event)) return void 0;
6797
+ if (!isRecord7(event)) return void 0;
6606
6798
  const value = event["meta"];
6607
- return isRecord6(value) ? value : void 0;
6799
+ return isRecord7(value) ? value : void 0;
6608
6800
  }
6609
6801
 
6610
6802
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6612,7 +6804,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6612
6804
  return mapAgentEventToRuntime(agentEvent, context, options);
6613
6805
  }
6614
6806
  function mapAgentEventToRuntime(agentEvent, context, options) {
6615
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
6807
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6616
6808
  const event = RuntimeEvent.parse(agentEvent);
6617
6809
  return context.metadata ? {
6618
6810
  ...event,
@@ -6753,9 +6945,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6753
6945
  return event;
6754
6946
  }
6755
6947
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6756
- const text = streamEvent.content ?? "";
6757
- if (!text) return null;
6758
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
6948
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6759
6949
  timestamp,
6760
6950
  is_last: streamEvent.is_last === true
6761
6951
  });
@@ -7275,7 +7465,7 @@ var LlmNode = class {
7275
7465
  ];
7276
7466
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7277
7467
  previousCount: history.length,
7278
- nextCount: parseRuntimeEvents(patch.history).length,
7468
+ nextCount: patch.history.length,
7279
7469
  streamedEventCount: nodeState.streamRuntimeEvents.length
7280
7470
  });
7281
7471
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7322,7 +7512,7 @@ var ToolModelInputResolutionError = class extends Error {
7322
7512
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7323
7513
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7324
7514
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7325
- function isRecord7(value) {
7515
+ function isRecord8(value) {
7326
7516
  return value !== null && typeof value === "object" && !Array.isArray(value);
7327
7517
  }
7328
7518
  function hasOnlyKeys(value, allowed) {
@@ -7335,7 +7525,7 @@ function readTrimmedString(value, maxLength) {
7335
7525
  return value === value.trim() ? value : void 0;
7336
7526
  }
7337
7527
  function parseSelection(value, index) {
7338
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7528
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7339
7529
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7340
7530
  }
7341
7531
  const id = readTrimmedString(value.id, 200);
@@ -7356,7 +7546,7 @@ function parseSelection(value, index) {
7356
7546
  return Object.freeze({ id, uri });
7357
7547
  }
7358
7548
  function parseToolModelInputDeclaration(value) {
7359
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7549
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7360
7550
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7361
7551
  }
7362
7552
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7432,7 +7622,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7432
7622
  }
7433
7623
 
7434
7624
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7435
- function isRecord8(value) {
7625
+ function isRecord9(value) {
7436
7626
  return !!value && typeof value === "object" && !Array.isArray(value);
7437
7627
  }
7438
7628
  function readString3(value) {
@@ -7475,7 +7665,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7475
7665
  }
7476
7666
  function readObservationPreviewMeta(parsed) {
7477
7667
  const meta = parsed["observationPreviewMeta"];
7478
- if (!isRecord8(meta)) {
7668
+ if (!isRecord9(meta)) {
7479
7669
  return void 0;
7480
7670
  }
7481
7671
  const filename = readString3(meta["filename"]);
@@ -7493,7 +7683,7 @@ function readObservationPreviewMeta(parsed) {
7493
7683
  };
7494
7684
  }
7495
7685
  async function applyObservationGovernance(params) {
7496
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7686
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7497
7687
  throw new Error("Observation governance requires a validated structured tool result.");
7498
7688
  }
7499
7689
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7585,7 +7775,7 @@ function createToolProtocolFuseError(nextCount, error) {
7585
7775
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7586
7776
  function mergeHistory(local, runtimeEvents) {
7587
7777
  const history = parseRuntimeEvents(local.history ?? []);
7588
- return [...history, ...runtimeEvents];
7778
+ return appendWorkingHistory(history, runtimeEvents);
7589
7779
  }
7590
7780
  function stripAnswerState(local) {
7591
7781
  const nextLocal = { ...local };
@@ -7594,7 +7784,7 @@ function stripAnswerState(local) {
7594
7784
  return nextLocal;
7595
7785
  }
7596
7786
  function validateStructuredToolResultContract(parsed) {
7597
- if (!isRecord8(parsed)) {
7787
+ if (!isRecord9(parsed)) {
7598
7788
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7599
7789
  }
7600
7790
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7622,7 +7812,7 @@ function validateStructuredToolResultContract(parsed) {
7622
7812
  };
7623
7813
  }
7624
7814
  function extractToolControlInfo(parsed) {
7625
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
7815
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7626
7816
  return void 0;
7627
7817
  }
7628
7818
  const control = parsed.control;
@@ -7646,7 +7836,7 @@ function buildRequireUserLocalState(params) {
7646
7836
  ...params.local,
7647
7837
  pendingToolCalls: params.remainingCalls,
7648
7838
  pendingInteractionSpec: {
7649
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
7839
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7650
7840
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7651
7841
  form: params.parsed,
7652
7842
  toolCallId: params.toolCallId,
@@ -7685,7 +7875,7 @@ function buildErrorLocalState(params) {
7685
7875
 
7686
7876
  // src/runtime-kernel/tools/argNormalizer.ts
7687
7877
  var logger8 = new Logger("ToolArgNormalizer");
7688
- function isRecord9(v) {
7878
+ function isRecord10(v) {
7689
7879
  return !!v && typeof v === "object" && !Array.isArray(v);
7690
7880
  }
7691
7881
  function tryParseJsonEncodedValue(v) {
@@ -7778,12 +7968,12 @@ function normalizeByPropertySchema(prop, v, options) {
7778
7968
  }
7779
7969
  case "object": {
7780
7970
  const parsedValue = tryParseJsonEncodedValue(v);
7781
- if (parsedValue !== v && isRecord9(parsedValue)) {
7971
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7782
7972
  logJsonEncodedNormalization("object", options);
7783
7973
  }
7784
- if (!isRecord9(parsedValue)) return parsedValue;
7974
+ if (!isRecord10(parsedValue)) return parsedValue;
7785
7975
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7786
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
7976
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7787
7977
  return normalizeToolArgs(
7788
7978
  { properties: selectedSchema.properties },
7789
7979
  parsedValue,
@@ -7805,7 +7995,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7805
7995
  }
7806
7996
  return next;
7807
7997
  }
7808
- function isRecord10(v) {
7998
+ function isRecord11(v) {
7809
7999
  return !!v && typeof v === "object" && !Array.isArray(v);
7810
8000
  }
7811
8001
  function stableStringify(v) {
@@ -7815,7 +8005,7 @@ function stableStringify(v) {
7815
8005
  if (t === "string") return JSON.stringify(v);
7816
8006
  if (t === "number" || t === "boolean") return String(v);
7817
8007
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
7818
- if (isRecord10(v)) {
8008
+ if (isRecord11(v)) {
7819
8009
  const keys = Object.keys(v).sort();
7820
8010
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
7821
8011
  return `{${parts.join(",")}}`;
@@ -7852,13 +8042,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
7852
8042
  if (e.tool_name !== params.toolName) continue;
7853
8043
  if (e.status !== "success") continue;
7854
8044
  const meta = e.metadata;
7855
- if (!isRecord10(meta)) continue;
8045
+ if (!isRecord11(meta)) continue;
7856
8046
  const idem = meta["idempotency"];
7857
- if (!isRecord10(idem)) continue;
8047
+ if (!isRecord11(idem)) continue;
7858
8048
  if (idem["key"] !== params.idempotencyKey) continue;
7859
8049
  const structuredResult = { data: e.data, observation: e.observation };
7860
8050
  const presentation = meta?.presentation;
7861
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
8051
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
7862
8052
  structuredResult["media"] = presentation["media"];
7863
8053
  }
7864
8054
  return {
@@ -8132,7 +8322,7 @@ function parseToolArgs(call) {
8132
8322
  }
8133
8323
  try {
8134
8324
  const parsed = JSON.parse(rawArguments);
8135
- if (!isRecord8(parsed)) {
8325
+ if (!isRecord9(parsed)) {
8136
8326
  return {
8137
8327
  ok: false,
8138
8328
  error: "Tool arguments must decode to a JSON object."
@@ -8150,13 +8340,12 @@ function parseToolArgs(call) {
8150
8340
  }
8151
8341
  function prepareToolNodeContext(state) {
8152
8342
  const local = state.local || {};
8153
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8154
- if (!isRecord8(local.toolContext)) {
8343
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8344
+ if (!isRecord9(local.toolContext)) {
8155
8345
  local.toolContext = toolContext;
8156
8346
  }
8157
8347
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8158
8348
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8159
- parseRuntimeEvents(local.history ?? []);
8160
8349
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8161
8350
  ensureToolContextRuntimeCapability({
8162
8351
  context: toolContext,
@@ -8424,11 +8613,11 @@ function createPendingToolCancellationEvents(params) {
8424
8613
  return events;
8425
8614
  }
8426
8615
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8427
- const history = Array.isArray(local.history) ? local.history : [];
8616
+ const history = parseRuntimeEvents(local.history ?? []);
8428
8617
  return {
8429
8618
  ...stripAnswerState(local),
8430
8619
  pendingToolCalls: [],
8431
- history: [...history, ...runtimeEvents]
8620
+ history: appendWorkingHistory(history, runtimeEvents)
8432
8621
  };
8433
8622
  }
8434
8623
  function recordToolProtocolError(params) {
@@ -8497,14 +8686,14 @@ function readHistoryEvents(local) {
8497
8686
  }
8498
8687
  function readLastSuccessfulLlmModelId(local) {
8499
8688
  const executorLocal = local.executorLocal;
8500
- if (!isRecord8(executorLocal)) {
8689
+ if (!isRecord9(executorLocal)) {
8501
8690
  return void 0;
8502
8691
  }
8503
8692
  const value = executorLocal.lastSuccessfulLlmModelId;
8504
8693
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8505
8694
  }
8506
8695
  function formatCapabilityError(error) {
8507
- if (isRecord8(error)) {
8696
+ if (isRecord9(error)) {
8508
8697
  const codeValue = error.errorCode ?? error.code;
8509
8698
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8510
8699
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9119,16 +9308,16 @@ var ToolNode = class {
9119
9308
  };
9120
9309
 
9121
9310
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9122
- function isRecord11(value) {
9311
+ function isRecord12(value) {
9123
9312
  return typeof value === "object" && value !== null;
9124
9313
  }
9125
9314
  function isRuntimeEvent(value) {
9126
- return isRecord11(value) && typeof value.type === "string";
9315
+ return isRecord12(value) && typeof value.type === "string";
9127
9316
  }
9128
9317
  var UserNode = class {
9129
9318
  id = "user";
9130
9319
  async run(state) {
9131
- const local = isRecord11(state.local) ? state.local : {};
9320
+ const local = isRecord12(state.local) ? state.local : {};
9132
9321
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9133
9322
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9134
9323
  if (!userInputEvent) {
@@ -9139,7 +9328,7 @@ var UserNode = class {
9139
9328
  };
9140
9329
 
9141
9330
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9142
- function isRecord12(value) {
9331
+ function isRecord13(value) {
9143
9332
  return !!value && typeof value === "object" && !Array.isArray(value);
9144
9333
  }
9145
9334
  function requireNonEmptyString(value, field) {
@@ -9156,10 +9345,10 @@ var WaitUserNode = class {
9156
9345
  }
9157
9346
  async run(state) {
9158
9347
  const local = state.local ?? {};
9159
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9348
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9160
9349
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9161
9350
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9162
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9351
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9163
9352
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9164
9353
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9165
9354
  const toolCallId = ToolCallIdSchema.parse(
@@ -9314,199 +9503,6 @@ __export(events_exports, {
9314
9503
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9315
9504
  });
9316
9505
 
9317
- // src/runtime-kernel/events/eventGovernance.ts
9318
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9319
- "audit_envelope",
9320
- "final_answer_chunk",
9321
- "final_answer_reset",
9322
- "subrun_trace",
9323
- "context_usage_snapshot"
9324
- ];
9325
- function isRecord13(value) {
9326
- return !!value && typeof value === "object" && !Array.isArray(value);
9327
- }
9328
- function isToolCallDecisionEvent(event) {
9329
- return event.type === "tool_call_decision";
9330
- }
9331
- function isToolProcessEvent(event) {
9332
- return event.type === "tool_process";
9333
- }
9334
- function isSubRunTraceRuntimeEvent(event) {
9335
- return event.type === "subrun_trace";
9336
- }
9337
- function isRunExecutionMetricsRuntimeEvent(event) {
9338
- return event.type === "run_execution_metrics";
9339
- }
9340
- function isContextUsageSnapshotRuntimeEvent(event) {
9341
- return event.type === "context_usage_snapshot";
9342
- }
9343
- function isRequiresUserInteractionRuntimeEvent(event) {
9344
- return event.type === "requires_user_interaction";
9345
- }
9346
- function isAuditEnvelopeRuntimeEvent(event) {
9347
- return event.type === "audit_envelope";
9348
- }
9349
- function isControlRuntimeEvent(event) {
9350
- return event.type === "control";
9351
- }
9352
- function isHiddenUserInputEvent(event) {
9353
- if (event.type !== "user_input") {
9354
- return false;
9355
- }
9356
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9357
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9358
- return ui?.presentation === "hidden";
9359
- }
9360
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9361
- if (event.type !== "thought" && event.type !== "final_answer") {
9362
- return false;
9363
- }
9364
- return event.content.trim().length === 0;
9365
- }
9366
- function isConversationUiRuntimeEvent(event) {
9367
- return event.lane === "foreground" && event.visibility === "conversation";
9368
- }
9369
- function shouldPersistRuntimeEvent(event) {
9370
- return describeRuntimeEventLifecycle(event).persist;
9371
- }
9372
- function requirePersistableRuntimeEvent(event) {
9373
- if (!shouldPersistRuntimeEvent(event)) {
9374
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9375
- }
9376
- return event;
9377
- }
9378
- function requirePersistableRoutedRuntimeEvent(event) {
9379
- requirePersistableRuntimeEvent(event);
9380
- return parseRoutedRuntimeEvent(event);
9381
- }
9382
- function shouldReplayRuntimeEventToUi(event) {
9383
- return describeRuntimeEventLifecycle(event).replayToUi;
9384
- }
9385
- function shouldEnterAgentContext(event) {
9386
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9387
- }
9388
- function getRuntimeEventUiProjectionKind(event) {
9389
- if (isHiddenUserInputEvent(event)) {
9390
- return "hidden";
9391
- }
9392
- switch (event.type) {
9393
- case "user_input":
9394
- return "user_input";
9395
- case "thought":
9396
- return "thought";
9397
- case "final_answer":
9398
- return "final_answer";
9399
- case "final_answer_chunk":
9400
- return "final_answer_chunk";
9401
- case "final_answer_reset":
9402
- return "final_answer_reset";
9403
- case "tool_call_decision":
9404
- return "tool_call_decision";
9405
- case "tool_process":
9406
- return "tool_process";
9407
- case "tool_output":
9408
- return "tool_output";
9409
- case "requires_user_interaction":
9410
- return "requires_user_interaction";
9411
- case "audit_envelope":
9412
- return "audit_envelope";
9413
- case "subrun_trace":
9414
- return "subrun_trace";
9415
- case "error":
9416
- return "error";
9417
- case "context_usage_snapshot":
9418
- return "context_usage_snapshot";
9419
- case "run_execution_metrics":
9420
- return "run_execution_metrics";
9421
- case "history_summary":
9422
- return "history_summary";
9423
- default:
9424
- return "unsupported";
9425
- }
9426
- }
9427
- function describeRuntimeEventLifecycle(event) {
9428
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9429
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9430
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9431
- const enterAgentContext = (() => {
9432
- if (uiProjectionKind === "unsupported") {
9433
- return false;
9434
- }
9435
- if (isSubRunTraceRuntimeEvent(event)) {
9436
- return false;
9437
- }
9438
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9439
- return false;
9440
- }
9441
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9442
- return false;
9443
- }
9444
- if (event.type === "error") {
9445
- return false;
9446
- }
9447
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9448
- return false;
9449
- }
9450
- if (isAuditEnvelopeRuntimeEvent(event)) {
9451
- return false;
9452
- }
9453
- if (isControlRuntimeEvent(event)) {
9454
- return false;
9455
- }
9456
- if (isHiddenUserInputEvent(event)) {
9457
- return false;
9458
- }
9459
- if (isToolProcessEvent(event)) {
9460
- return false;
9461
- }
9462
- if (event.type === "thought") {
9463
- return false;
9464
- }
9465
- if (event.type === "final_answer_chunk") {
9466
- return false;
9467
- }
9468
- if (event.type === "final_answer_reset") {
9469
- return false;
9470
- }
9471
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9472
- return false;
9473
- }
9474
- return true;
9475
- })();
9476
- const realtimeChannel = (() => {
9477
- switch (uiProjectionKind) {
9478
- case "thought":
9479
- case "final_answer":
9480
- case "final_answer_chunk":
9481
- case "final_answer_reset":
9482
- case "tool_call_decision":
9483
- case "tool_process":
9484
- case "tool_output":
9485
- case "requires_user_interaction":
9486
- case "subrun_trace":
9487
- case "error":
9488
- case "context_usage_snapshot":
9489
- case "run_execution_metrics":
9490
- case "history_summary":
9491
- return "event_bus_sse";
9492
- case "audit_envelope":
9493
- return "none";
9494
- default:
9495
- return "none";
9496
- }
9497
- })();
9498
- return {
9499
- uiProjectionKind,
9500
- persist,
9501
- replayToUi,
9502
- enterAgentContext,
9503
- realtimeChannel
9504
- };
9505
- }
9506
- function shouldEmitRuntimeEventToSse(event) {
9507
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9508
- }
9509
-
9510
9506
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9511
9507
  function findTerminalFinalAnswer(events) {
9512
9508
  for (let index = events.length - 1; index >= 0; index -= 1) {