@linnlabs/linnkit 0.42.0 → 0.44.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 (75) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/dist/cli.cjs +113 -5
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +113 -5
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager/profiles/agent/context/config.d.cts +1 -1
  7. package/dist/context-manager/profiles/agent/context/config.d.ts +1 -1
  8. package/dist/context-manager.cjs +37 -4
  9. package/dist/context-manager.cjs.map +1 -1
  10. package/dist/context-manager.js +37 -4
  11. package/dist/context-manager.js.map +1 -1
  12. package/dist/contracts/events.d.cts +4 -0
  13. package/dist/contracts/events.d.ts +4 -0
  14. package/dist/contracts/messages.d.cts +226 -2
  15. package/dist/contracts/messages.d.ts +226 -2
  16. package/dist/contracts/sse.d.cts +65 -1
  17. package/dist/contracts/sse.d.ts +65 -1
  18. package/dist/contracts.cjs +19 -2
  19. package/dist/contracts.cjs.map +1 -1
  20. package/dist/contracts.d.cts +1 -1
  21. package/dist/contracts.d.ts +1 -1
  22. package/dist/contracts.js +19 -3
  23. package/dist/contracts.js.map +1 -1
  24. package/dist/index.cjs +115 -5
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +115 -5
  27. package/dist/index.js.map +1 -1
  28. package/dist/quickstart.cjs +113 -5
  29. package/dist/quickstart.cjs.map +1 -1
  30. package/dist/quickstart.js +113 -5
  31. package/dist/quickstart.js.map +1 -1
  32. package/dist/runtime-kernel/events.cjs +29 -2
  33. package/dist/runtime-kernel/events.cjs.map +1 -1
  34. package/dist/runtime-kernel/events.js +29 -2
  35. package/dist/runtime-kernel/events.js.map +1 -1
  36. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.cts +27 -0
  37. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.ts +27 -0
  38. package/dist/runtime-kernel/graph-engine/engine.d.cts +2 -1
  39. package/dist/runtime-kernel/graph-engine/engine.d.ts +2 -1
  40. package/dist/runtime-kernel/graph-engine/index.d.cts +1 -0
  41. package/dist/runtime-kernel/graph-engine/index.d.ts +1 -0
  42. package/dist/runtime-kernel/graph-engine/types.d.cts +3 -0
  43. package/dist/runtime-kernel/graph-engine/types.d.ts +3 -0
  44. package/dist/runtime-kernel.cjs +113 -5
  45. package/dist/runtime-kernel.cjs.map +1 -1
  46. package/dist/runtime-kernel.d.cts +1 -0
  47. package/dist/runtime-kernel.d.ts +1 -0
  48. package/dist/runtime-kernel.js +113 -5
  49. package/dist/runtime-kernel.js.map +1 -1
  50. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.cts +24 -6
  51. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.ts +24 -6
  52. package/dist/testkit/scenario/graphScenario.d.cts +89 -0
  53. package/dist/testkit/scenario/graphScenario.d.ts +89 -0
  54. package/dist/testkit/scenario/pendingUserInputQueue.d.cts +26 -0
  55. package/dist/testkit/scenario/pendingUserInputQueue.d.ts +26 -0
  56. package/dist/testkit/scenario/runtimeEventRecorder.d.cts +36 -0
  57. package/dist/testkit/scenario/runtimeEventRecorder.d.ts +36 -0
  58. package/dist/testkit/scenario/scriptedToolRuntime.d.cts +51 -0
  59. package/dist/testkit/scenario/scriptedToolRuntime.d.ts +51 -0
  60. package/dist/testkit/scenario/steeringInvariants.d.cts +33 -0
  61. package/dist/testkit/scenario/steeringInvariants.d.ts +33 -0
  62. package/dist/testkit/scenario/timeline.d.cts +27 -0
  63. package/dist/testkit/scenario/timeline.d.ts +27 -0
  64. package/dist/testkit.cjs +683 -18
  65. package/dist/testkit.cjs.map +1 -1
  66. package/dist/testkit.d.cts +7 -1
  67. package/dist/testkit.d.ts +7 -1
  68. package/dist/testkit.js +675 -19
  69. package/dist/testkit.js.map +1 -1
  70. package/docs/integration/README.md +1 -0
  71. package/docs/integration/mid-run-user-input.md +73 -0
  72. package/docs/integration/realtime.md +4 -0
  73. package/docs/integration/testing.md +40 -4
  74. package/docs/integration/tool-development-guide.md +1 -0
  75. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -12,6 +12,70 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
 
13
13
  ---
14
14
 
15
+ ## [0.44.0] - 2026-11-18
16
+
17
+ ### Added
18
+
19
+ - Mid-run user input. A Host can give the Graph an optional
20
+ `pendingUserInputPort` (`PendingUserInput` / `PendingUserInputPort` in the
21
+ graph exports). After a tool batch settles and before the run returns to the
22
+ model, `ToolNode` claims the pending inputs, publishes each as a `user_input`
23
+ fact with `delivery: 'mid_run'` after all the batch's tool outputs, and
24
+ commits them with the same tool boundary. It confirms after the commit and
25
+ releases them back to the Host if the commit fails. Nothing is claimed when
26
+ the batch does not return to the model, when a pause or cancel signal has
27
+ arrived, or when no step is left for another model call. Child runs should
28
+ not receive the port. Hosts without the port behave as before.
29
+ - `user_input.delivery` (optional, `'mid_run'`) and the `AiMessage` type
30
+ `user_steer`. A mid-run input enters the context as `user_steer` and is sent
31
+ to the model as an ordinary user message. The start of the current turn is
32
+ still the run's starting `user_input`, so current-turn fence assembly and
33
+ cleanup, tool history grouping and working-memory protection are unchanged.
34
+ Steers in the current run are core context and are protected from
35
+ compaction; steers from earlier runs are treated as historical user text.
36
+ - `SSEUserInputEvent` in the `SSEEvent` union. A mid-run `user_input` goes to
37
+ the realtime channel (`shouldEmitRuntimeEventToSse` is true) and
38
+ `runtimeEventToSSEEvent` projects it with the user's text and
39
+ `delivery: 'mid_run'`, so clients learn where a steer was delivered while
40
+ the run is still going. Starting inputs are still not sent back. Clients
41
+ that switch exhaustively over `SSEEvent['type']` need a `user_input` case.
42
+ - testkit: `createGraphScenario` runs the real Graph in memory (LlmNode,
43
+ ToolNode, WaitUserNode, the event bus, the persistence queue and a JSON
44
+ round-trip of every checkpoint) with a scripted model, scripted tools and an
45
+ in-memory steering queue. A scenario can start several runs in one
46
+ conversation, steer, pause, continue or cancel at any point, and returns the
47
+ run status instead of throwing. Supporting exports:
48
+ `createScriptedToolRuntime`, `createPendingUserInputQueue`,
49
+ `createRuntimeEventRecorder`, `formatRuntimeTimeline`,
50
+ `formatModelMessages`, `userInputText`, and the steering invariants
51
+ `validateSteeringInvariants` / `assertSteeringInvariants` (S1–S5).
52
+ - testkit: `createScriptedInferenceHarness` also accepts a function that
53
+ chooses each turn from the request, awaits an async `assertCall`, exposes
54
+ `getInferencePort()`, and lets a scripted tool call omit `id` (generated per
55
+ call) and pass `arguments` as an object instead of `argumentsJson`.
56
+
57
+ ---
58
+
59
+ ## [0.43.0] - 2026-11-12
60
+
61
+ ### Fixed
62
+
63
+ - `GraphExecutor.resumeSession` now finishes the tool batch that an
64
+ interactive (`requireUser`) tool interrupted before returning to the model.
65
+ Previously a resume always went to `llm`, so calls queued after the
66
+ interactive tool in the same batch never ran. The batch was left with calls
67
+ that had no results, context building dropped the whole group — including
68
+ the user's answer — and the model asked the same question again.
69
+
70
+ ### Changed
71
+
72
+ - Without `nodeId`, `resumeSession` starts at `tool` when the waiting
73
+ checkpoint still has pending tool calls, and at `llm` otherwise. Passing a
74
+ `nodeId` other than `tool` while calls are pending now throws. Hosts that
75
+ passed `nodeId: 'llm'` should drop it.
76
+
77
+ ---
78
+
15
79
  ## [0.42.0] - 2026-11-11
16
80
 
17
81
  ### Breaking
package/dist/cli.cjs CHANGED
@@ -346,6 +346,10 @@ var UserInputMessage = BaseMessage.extend({
346
346
  type: zod.z.literal("user_input"),
347
347
  attachments: RuntimeResourceRefs.optional()
348
348
  });
349
+ var UserSteerMessage = BaseMessage.extend({
350
+ role: zod.z.literal("user"),
351
+ type: zod.z.literal("user_steer")
352
+ });
349
353
  var UserContextMessage = BaseMessage.extend({
350
354
  role: zod.z.literal("user"),
351
355
  // 产品上下文种类由 Host fence metadata 表达,不进入框架消息枚举。
@@ -354,7 +358,7 @@ var UserContextMessage = BaseMessage.extend({
354
358
  "task_request"
355
359
  ])
356
360
  });
357
- var UserMessage = zod.z.union([UserInputMessage, UserContextMessage]);
361
+ var UserMessage = zod.z.union([UserInputMessage, UserSteerMessage, UserContextMessage]);
358
362
  var AssistantMessage = BaseMessage.extend({
359
363
  role: zod.z.literal("assistant"),
360
364
  type: zod.z.enum(["thought", "final_answer", "tool_code", "tool_calls", "task_completion"])
@@ -971,7 +975,12 @@ var RuntimeEventShape = zod.z.discriminatedUnion("type", [
971
975
  content: zod.z.string(),
972
976
  raw_content: zod.z.string().optional(),
973
977
  source: zod.z.enum(["user", "editor", "system"]).default("user"),
974
- attachments: RuntimeResourceRefs.optional()
978
+ attachments: RuntimeResourceRefs.optional(),
979
+ /**
980
+ * 运行中途送达的用户输入(插话)。缺省表示运行起点的输入。
981
+ * 上下文里投影为 `user_steer`,不改变“当前轮从哪条 user_input 开始”的判定。
982
+ */
983
+ delivery: zod.z.literal("mid_run").optional()
975
984
  }),
976
985
  BaseEvent.extend({
977
986
  type: zod.z.literal("thought"),
@@ -1418,6 +1427,11 @@ zod.z.object({
1418
1427
  run_id: RunIdSchema,
1419
1428
  execution_id: ExecutionIdSchema
1420
1429
  });
1430
+ var SSEUserInputEvent = BaseSSEEvent.extend({
1431
+ type: zod.z.literal("user_input"),
1432
+ content: zod.z.string(),
1433
+ delivery: zod.z.literal("mid_run")
1434
+ });
1421
1435
  var SSEThoughtEvent = BaseSSEEvent.extend({
1422
1436
  type: zod.z.literal("thought"),
1423
1437
  thought_message_id: ThoughtMessageIdSchema.optional(),
@@ -1572,6 +1586,7 @@ var SSESummarizationErrorEvent = BaseSSEEvent.extend({
1572
1586
  error: zod.z.string()
1573
1587
  });
1574
1588
  zod.z.discriminatedUnion("type", [
1589
+ SSEUserInputEvent,
1575
1590
  SSEThoughtEvent,
1576
1591
  SSEFinalAnswerChunkEvent,
1577
1592
  SSEFinalAnswerResetEvent,
@@ -2542,6 +2557,7 @@ var NON_CHECKPOINT_LOCAL_KEYS = [
2542
2557
  "runtimeEventCommitPort",
2543
2558
  "commitExecutionBoundary",
2544
2559
  "toolRecoveryPort",
2560
+ "pendingUserInputPort",
2545
2561
  "runtimeFailureFactSink",
2546
2562
  "signal",
2547
2563
  "summarizationCallbacks",
@@ -3105,6 +3121,16 @@ function mergeResumeSessionLocal(persistedLocal, localPatch) {
3105
3121
  }
3106
3122
  };
3107
3123
  }
3124
+ function resolveResumeNodeId(local, requested) {
3125
+ const pending = local?.pendingToolCalls;
3126
+ if (!Array.isArray(pending) || pending.length === 0) return requested ?? "llm";
3127
+ if (requested !== void 0 && requested !== "tool") {
3128
+ throw new Error(
3129
+ `Graph resume must first run ${pending.length} pending tool call(s) from the interrupted batch: requested node=${requested}`
3130
+ );
3131
+ }
3132
+ return "tool";
3133
+ }
3108
3134
  var GraphExecutor = class {
3109
3135
  constructor(checkpointer, config = {}) {
3110
3136
  this.checkpointer = checkpointer;
@@ -3217,13 +3243,14 @@ var GraphExecutor = class {
3217
3243
  );
3218
3244
  }
3219
3245
  const resumedLocal = mergeResumeSessionLocal(current.local, input.localPatch);
3246
+ const nodeId = resolveResumeNodeId(resumedLocal, input.nodeId);
3220
3247
  if (this.executionCheckpointPort && current.local?.executorLocal) {
3221
3248
  resumedLocal.executorLocal = { ...current.local.executorLocal };
3222
3249
  }
3223
3250
  this.ephemeralLocals.set(checkpointKey, resumedLocal);
3224
3251
  await this.commitCheckpoint(checkpointKey, {
3225
3252
  ...current,
3226
- nodeId: input.nodeId ?? "llm",
3253
+ nodeId,
3227
3254
  ...this.executionCheckpointPort ? { executionStatus: "ready" } : {},
3228
3255
  revision: currentRevision + 1,
3229
3256
  local: sanitizeCheckpointLocal(resumedLocal)
@@ -6523,6 +6550,8 @@ function describeRuntimeEventLifecycle(event) {
6523
6550
  case "run_execution_metrics":
6524
6551
  case "history_summary":
6525
6552
  return "event_bus_sse";
6553
+ case "user_input":
6554
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
6526
6555
  case "audit_envelope":
6527
6556
  return "none";
6528
6557
  default:
@@ -6966,6 +6995,16 @@ function requireEventTimestamp(value) {
6966
6995
  function applyRuntimeEventToMemory(event, memory) {
6967
6996
  switch (event.type) {
6968
6997
  case "user_input": {
6998
+ if (event.delivery === "mid_run") {
6999
+ memory.appendMessage({
7000
+ id: event.id,
7001
+ role: "user",
7002
+ type: "user_steer",
7003
+ content: event.content,
7004
+ timestamp: event.timestamp
7005
+ });
7006
+ break;
7007
+ }
6969
7008
  memory.addUserMessage(event.content, event.id, event.attachments);
6970
7009
  break;
6971
7010
  }
@@ -8613,6 +8652,62 @@ async function commitToolBoundary(state, result) {
8613
8652
  await state.local.commitExecutionBoundary(state);
8614
8653
  }
8615
8654
 
8655
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
8656
+ function isPendingUserInputPort(value) {
8657
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
8658
+ }
8659
+ function readRemainingSteps(state) {
8660
+ const value = state.local?.executorLocal?.remainingSteps;
8661
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
8662
+ }
8663
+ function readIdentity(value, field) {
8664
+ if (typeof value !== "string" || value.trim().length === 0) {
8665
+ throw new Error(`ToolNode pending user input requires ${field}`);
8666
+ }
8667
+ return value;
8668
+ }
8669
+ async function claimPendingUserInputs(state, result) {
8670
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
8671
+ const local = state.local;
8672
+ const port = local?.pendingUserInputPort;
8673
+ if (!isPendingUserInputPort(port)) return void 0;
8674
+ const signal = local?.signal;
8675
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
8676
+ const remainingSteps = readRemainingSteps(state);
8677
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
8678
+ const inputs = await port.claim();
8679
+ if (inputs.length === 0) return void 0;
8680
+ const ids = inputs.map((input) => input.id);
8681
+ try {
8682
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
8683
+ const turnId = readIdentity(local?.turnId, "turnId");
8684
+ const sink = requireRuntimeEventSink(local);
8685
+ const events = inputs.map((input) => sink(
8686
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
8687
+ timestamp: input.timestamp ?? Date.now(),
8688
+ delivery: "mid_run"
8689
+ }),
8690
+ "ToolNode.pending_user_input"
8691
+ ));
8692
+ state.local = {
8693
+ ...state.local,
8694
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
8695
+ };
8696
+ return {
8697
+ events,
8698
+ confirm: async () => {
8699
+ await port.confirm(ids);
8700
+ },
8701
+ release: async () => {
8702
+ await port.release(ids);
8703
+ }
8704
+ };
8705
+ } catch (error) {
8706
+ await port.release(ids);
8707
+ throw error;
8708
+ }
8709
+ }
8710
+
8616
8711
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
8617
8712
  var logger11 = new Logger("ToolNode");
8618
8713
  function isAbortSignal(value) {
@@ -8780,7 +8875,19 @@ var ToolNode = class {
8780
8875
  });
8781
8876
  throw result.error;
8782
8877
  }
8783
- await commitToolBoundary(state, result);
8878
+ const steering = await claimPendingUserInputs(state, result);
8879
+ if (steering) {
8880
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
8881
+ try {
8882
+ await commitToolBoundary(state, result);
8883
+ } catch (error) {
8884
+ await steering.release();
8885
+ throw error;
8886
+ }
8887
+ await steering.confirm();
8888
+ } else {
8889
+ await commitToolBoundary(state, result);
8890
+ }
8784
8891
  if (Array.isArray(result.events) && result.events.length > 0) {
8785
8892
  events.push(...result.events);
8786
8893
  }
@@ -9538,7 +9645,8 @@ function parseEngineCheckpoint(value) {
9538
9645
  "runtimeFailureFactSink",
9539
9646
  "summarizationCallbacks",
9540
9647
  "commitExecutionBoundary",
9541
- "toolRecoveryPort"
9648
+ "toolRecoveryPort",
9649
+ "pendingUserInputPort"
9542
9650
  ]) {
9543
9651
  if (result.local && Object.hasOwn(result.local, key)) {
9544
9652
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);