@leitwerk-dev/worker 0.1.8 → 0.2.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 (235) hide show
  1. package/dist/automatic-turn-executor.d.ts +1 -0
  2. package/dist/automatic-turn-executor.d.ts.map +1 -1
  3. package/dist/{chunk-ZTJ7UAOC.js → chunk-Y7S2MZD3.js} +1423 -917
  4. package/dist/chunk-Y7S2MZD3.js.map +1 -0
  5. package/dist/connection-diagnostics.d.ts +5 -0
  6. package/dist/connection-diagnostics.d.ts.map +1 -0
  7. package/dist/container-entry-main.d.ts +2 -0
  8. package/dist/container-entry-main.d.ts.map +1 -0
  9. package/dist/container-entry-main.js +232 -0
  10. package/dist/container-entry-main.js.map +1 -0
  11. package/dist/container-entry.d.ts +14 -0
  12. package/dist/container-entry.d.ts.map +1 -0
  13. package/dist/development-tool-environment.d.ts +48 -0
  14. package/dist/development-tool-environment.d.ts.map +1 -0
  15. package/dist/diagnostics.d.ts +9 -1
  16. package/dist/diagnostics.d.ts.map +1 -1
  17. package/dist/docker-registry-credentials.d.ts +8 -0
  18. package/dist/docker-registry-credentials.d.ts.map +1 -0
  19. package/dist/entry-runtime.d.ts +1 -0
  20. package/dist/entry-runtime.d.ts.map +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +44 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/input-consumer.d.ts +6 -11
  26. package/dist/input-consumer.d.ts.map +1 -1
  27. package/dist/ipc.d.ts +1 -0
  28. package/dist/ipc.d.ts.map +1 -1
  29. package/dist/logical-prompt-runner.d.ts.map +1 -1
  30. package/dist/managed-pi-bootstrap.d.ts.map +1 -1
  31. package/dist/missing-tool-recovery-runner.d.ts +4 -3
  32. package/dist/missing-tool-recovery-runner.d.ts.map +1 -1
  33. package/dist/pi-adapter.d.ts +1 -0
  34. package/dist/pi-adapter.d.ts.map +1 -1
  35. package/dist/pi-event-reporter.d.ts.map +1 -1
  36. package/dist/pi-resource-bundle.d.ts +11 -0
  37. package/dist/pi-resource-bundle.d.ts.map +1 -1
  38. package/dist/pre-turn-targeted-inputs.d.ts +8 -7
  39. package/dist/pre-turn-targeted-inputs.d.ts.map +1 -1
  40. package/dist/prompt-guards.d.ts.map +1 -1
  41. package/dist/repository-bash-tool.d.ts +4 -0
  42. package/dist/repository-bash-tool.d.ts.map +1 -0
  43. package/dist/resource-loader-factory.d.ts +1 -2
  44. package/dist/resource-loader-factory.d.ts.map +1 -1
  45. package/dist/result-image-upload.d.ts.map +1 -1
  46. package/dist/runtime/adapters.d.ts +2 -0
  47. package/dist/runtime/adapters.d.ts.map +1 -1
  48. package/dist/runtime/bootstrap-session.d.ts +8 -5
  49. package/dist/runtime/bootstrap-session.d.ts.map +1 -1
  50. package/dist/runtime/bootstrap.d.ts +14 -18
  51. package/dist/runtime/bootstrap.d.ts.map +1 -1
  52. package/dist/runtime/failure-policy.d.ts +1 -0
  53. package/dist/runtime/failure-policy.d.ts.map +1 -1
  54. package/dist/runtime/index.d.ts.map +1 -1
  55. package/dist/runtime/lifecycle-reducer.d.ts +4 -1
  56. package/dist/runtime/lifecycle-reducer.d.ts.map +1 -1
  57. package/dist/runtime/llm-turn-execution.d.ts +3 -8
  58. package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
  59. package/dist/runtime/turn-execution.d.ts +2 -9
  60. package/dist/runtime/turn-execution.d.ts.map +1 -1
  61. package/dist/session-snapshot-exchange.d.ts +0 -4
  62. package/dist/session-snapshot-exchange.d.ts.map +1 -1
  63. package/dist/turn-continuation.d.ts.map +1 -1
  64. package/dist/turn-execution-error.d.ts +1 -1
  65. package/dist/turn-execution-error.d.ts.map +1 -1
  66. package/dist/turn-execution-result.d.ts +1 -0
  67. package/dist/turn-execution-result.d.ts.map +1 -1
  68. package/dist/turn-outcome-tool-session.d.ts +0 -2
  69. package/dist/turn-outcome-tool-session.d.ts.map +1 -1
  70. package/dist/turn-result-markdown.d.ts +2 -1
  71. package/dist/turn-result-markdown.d.ts.map +1 -1
  72. package/dist/turn-tree-strategy.d.ts +0 -1
  73. package/dist/turn-tree-strategy.d.ts.map +1 -1
  74. package/dist/worker-entry.js +16 -4
  75. package/dist/worker-entry.js.map +1 -1
  76. package/dist/worker-ipc-reporter.d.ts.map +1 -1
  77. package/dist/workspace/manifest.d.ts +4 -7
  78. package/dist/workspace/manifest.d.ts.map +1 -1
  79. package/dist/workspace/node-run-root-git-ops.d.ts +2 -2
  80. package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -1
  81. package/dist/workspace/resource-aggregator.d.ts.map +1 -1
  82. package/dist/workspace/run-root.d.ts +9 -20
  83. package/dist/workspace/run-root.d.ts.map +1 -1
  84. package/package.json +8 -8
  85. package/src/automatic-turn-executor.ts +2 -0
  86. package/src/connection-diagnostics.ts +27 -0
  87. package/src/container-entry-main.ts +24 -0
  88. package/src/container-entry.ts +246 -0
  89. package/src/development-tool-environment.ts +452 -0
  90. package/src/diagnostics.ts +3 -1
  91. package/src/docker-registry-credentials.ts +65 -0
  92. package/src/entry-runtime.ts +5 -4
  93. package/src/index.ts +11 -0
  94. package/src/input-consumer.ts +7 -29
  95. package/src/ipc.ts +37 -2
  96. package/src/logical-prompt-runner.ts +23 -29
  97. package/src/managed-pi-bootstrap.ts +3 -3
  98. package/src/missing-tool-recovery-runner.ts +4 -18
  99. package/src/pi-adapter.ts +19 -50
  100. package/src/pi-event-reporter.ts +16 -38
  101. package/src/pi-resource-bundle.ts +67 -1
  102. package/src/pre-turn-targeted-inputs.ts +9 -11
  103. package/src/prompt-guards.ts +44 -75
  104. package/src/repository-bash-tool.ts +15 -0
  105. package/src/resource-loader-factory.ts +1 -18
  106. package/src/result-image-upload.ts +16 -27
  107. package/src/runtime/adapters.ts +2 -0
  108. package/src/runtime/bootstrap-session.ts +69 -39
  109. package/src/runtime/bootstrap.ts +160 -103
  110. package/src/runtime/failure-policy.ts +36 -42
  111. package/src/runtime/index.ts +49 -14
  112. package/src/runtime/lifecycle-reducer.ts +140 -173
  113. package/src/runtime/llm-turn-execution.ts +13 -30
  114. package/src/runtime/turn-execution.ts +91 -9
  115. package/src/session-snapshot-exchange.ts +23 -64
  116. package/src/turn-continuation.ts +9 -17
  117. package/src/turn-execution-error.ts +3 -11
  118. package/src/turn-execution-result.ts +1 -0
  119. package/src/turn-outcome-tool-session.ts +25 -21
  120. package/src/turn-result-markdown.ts +31 -34
  121. package/src/turn-tree-strategy.ts +6 -19
  122. package/src/worker-entry.ts +8 -0
  123. package/src/worker-ipc-reporter.ts +8 -16
  124. package/src/workspace/manifest.ts +5 -7
  125. package/src/workspace/node-run-root-git-ops.ts +65 -25
  126. package/src/workspace/resource-aggregator.ts +5 -16
  127. package/src/workspace/run-root.ts +38 -89
  128. package/dist/automatic-turn-executor.js +0 -58
  129. package/dist/automatic-turn-executor.js.map +0 -1
  130. package/dist/chunk-ZTJ7UAOC.js.map +0 -1
  131. package/dist/diagnostics.js +0 -51
  132. package/dist/diagnostics.js.map +0 -1
  133. package/dist/entry-runtime.js +0 -160
  134. package/dist/entry-runtime.js.map +0 -1
  135. package/dist/event-translator.js +0 -12
  136. package/dist/event-translator.js.map +0 -1
  137. package/dist/extension-entry-roots.js +0 -41
  138. package/dist/extension-entry-roots.js.map +0 -1
  139. package/dist/identified-pi-entry.js +0 -125
  140. package/dist/identified-pi-entry.js.map +0 -1
  141. package/dist/input-consumer.js +0 -34
  142. package/dist/input-consumer.js.map +0 -1
  143. package/dist/integration-tool-bridge.js +0 -53
  144. package/dist/integration-tool-bridge.js.map +0 -1
  145. package/dist/ipc.js +0 -192
  146. package/dist/ipc.js.map +0 -1
  147. package/dist/logical-prompt-runner.js +0 -225
  148. package/dist/logical-prompt-runner.js.map +0 -1
  149. package/dist/managed-pi-agent-dir.js +0 -66
  150. package/dist/managed-pi-agent-dir.js.map +0 -1
  151. package/dist/managed-pi-bootstrap.js +0 -135
  152. package/dist/managed-pi-bootstrap.js.map +0 -1
  153. package/dist/missing-tool-recovery-runner.js +0 -73
  154. package/dist/missing-tool-recovery-runner.js.map +0 -1
  155. package/dist/pi-adapter.js +0 -1648
  156. package/dist/pi-adapter.js.map +0 -1
  157. package/dist/pi-branch-guard.js +0 -75
  158. package/dist/pi-branch-guard.js.map +0 -1
  159. package/dist/pi-event-reporter.js +0 -136
  160. package/dist/pi-event-reporter.js.map +0 -1
  161. package/dist/pi-resource-bundle.js +0 -58
  162. package/dist/pi-resource-bundle.js.map +0 -1
  163. package/dist/pre-turn-targeted-inputs.js +0 -101
  164. package/dist/pre-turn-targeted-inputs.js.map +0 -1
  165. package/dist/prompt-guards.js +0 -318
  166. package/dist/prompt-guards.js.map +0 -1
  167. package/dist/question-bridge.js +0 -24
  168. package/dist/question-bridge.js.map +0 -1
  169. package/dist/question-tool.js +0 -54
  170. package/dist/question-tool.js.map +0 -1
  171. package/dist/replaying-request-bridge.js +0 -71
  172. package/dist/replaying-request-bridge.js.map +0 -1
  173. package/dist/resource-loader-factory.js +0 -87
  174. package/dist/resource-loader-factory.js.map +0 -1
  175. package/dist/result-image-upload.js +0 -183
  176. package/dist/result-image-upload.js.map +0 -1
  177. package/dist/runtime/adapters.js +0 -15
  178. package/dist/runtime/adapters.js.map +0 -1
  179. package/dist/runtime/bootstrap-session.js +0 -199
  180. package/dist/runtime/bootstrap-session.js.map +0 -1
  181. package/dist/runtime/bootstrap.js +0 -440
  182. package/dist/runtime/bootstrap.js.map +0 -1
  183. package/dist/runtime/failure-policy.js +0 -182
  184. package/dist/runtime/failure-policy.js.map +0 -1
  185. package/dist/runtime/index.js +0 -336
  186. package/dist/runtime/index.js.map +0 -1
  187. package/dist/runtime/lifecycle-reducer.js +0 -1100
  188. package/dist/runtime/lifecycle-reducer.js.map +0 -1
  189. package/dist/runtime/llm-turn-execution.js +0 -494
  190. package/dist/runtime/llm-turn-execution.js.map +0 -1
  191. package/dist/runtime/settings.js +0 -24
  192. package/dist/runtime/settings.js.map +0 -1
  193. package/dist/runtime/snapshot-policy.js +0 -16
  194. package/dist/runtime/snapshot-policy.js.map +0 -1
  195. package/dist/runtime/turn-execution.js +0 -121
  196. package/dist/runtime/turn-execution.js.map +0 -1
  197. package/dist/session-snapshot-exchange.js +0 -102
  198. package/dist/session-snapshot-exchange.js.map +0 -1
  199. package/dist/test-helpers/fake-websocket.js +0 -40
  200. package/dist/test-helpers/fake-websocket.js.map +0 -1
  201. package/dist/test-helpers/ipc-harness.d.ts +0 -11
  202. package/dist/test-helpers/ipc-harness.d.ts.map +0 -1
  203. package/dist/test-helpers/ipc-harness.js +0 -17
  204. package/dist/test-helpers/ipc-harness.js.map +0 -1
  205. package/dist/turn-continuation.js +0 -80
  206. package/dist/turn-continuation.js.map +0 -1
  207. package/dist/turn-execution-error.js +0 -64
  208. package/dist/turn-execution-error.js.map +0 -1
  209. package/dist/turn-execution-result.js +0 -21
  210. package/dist/turn-execution-result.js.map +0 -1
  211. package/dist/turn-outcome-tool-session.js +0 -210
  212. package/dist/turn-outcome-tool-session.js.map +0 -1
  213. package/dist/turn-record-id.js +0 -10
  214. package/dist/turn-record-id.js.map +0 -1
  215. package/dist/turn-result-markdown.js +0 -263
  216. package/dist/turn-result-markdown.js.map +0 -1
  217. package/dist/turn-tree-strategy.js +0 -80
  218. package/dist/turn-tree-strategy.js.map +0 -1
  219. package/dist/worker-http.js +0 -9
  220. package/dist/worker-http.js.map +0 -1
  221. package/dist/worker-ipc-reporter.js +0 -58
  222. package/dist/worker-ipc-reporter.js.map +0 -1
  223. package/dist/worker-payloads.js +0 -66
  224. package/dist/worker-payloads.js.map +0 -1
  225. package/dist/worker-start-payload.js +0 -5
  226. package/dist/worker-start-payload.js.map +0 -1
  227. package/dist/workspace/manifest.js +0 -79
  228. package/dist/workspace/manifest.js.map +0 -1
  229. package/dist/workspace/node-run-root-git-ops.js +0 -96
  230. package/dist/workspace/node-run-root-git-ops.js.map +0 -1
  231. package/dist/workspace/resource-aggregator.js +0 -39
  232. package/dist/workspace/resource-aggregator.js.map +0 -1
  233. package/dist/workspace/run-root.js +0 -209
  234. package/dist/workspace/run-root.js.map +0 -1
  235. package/src/test-helpers/ipc-harness.ts +0 -19
@@ -33,7 +33,7 @@ export type WorkerRuntimeStateName =
33
33
  /** Compatibility type used by pure failure normalization. */
34
34
  export type WorkerRuntimeState = WorkerRuntimeStateName;
35
35
  export type ReportedWorkerState = "idle" | "busy" | "draining";
36
- export type WorkerTimerName = "heartbeat" | "acceptance_retry" | "credential_poll";
36
+ export type WorkerTimerName = "heartbeat" | "acceptance_retry" | "credential_poll" | "terminal_ack";
37
37
 
38
38
  type ActiveTurnStatus = "pending" | "executing" | "completed";
39
39
  type DrainingOperation =
@@ -108,6 +108,10 @@ type PublicationState =
108
108
  };
109
109
  terminal: PendingTerminal;
110
110
  }
111
+ | {
112
+ kind: "terminal_pending_ack";
113
+ terminal: PendingTerminal;
114
+ }
111
115
  | {
112
116
  kind: "cleanup_snapshot";
113
117
  snapshot: {
@@ -282,7 +286,12 @@ export function createInitialWorkerRuntimeState(): WorkerRuntimeStateMachine {
282
286
  publication: { kind: "none" },
283
287
  },
284
288
  credential: null,
285
- timers: { heartbeat: null, acceptance_retry: null, credential_poll: null },
289
+ timers: {
290
+ heartbeat: null,
291
+ acceptance_retry: null,
292
+ credential_poll: null,
293
+ terminal_ack: null,
294
+ },
286
295
  };
287
296
  }
288
297
 
@@ -291,7 +300,8 @@ export function deriveReportedWorkerState(state: WorkerRuntimeStateMachine): Rep
291
300
  if (
292
301
  (state.phase.kind === "active" &&
293
302
  (state.phase.turnStatus === "executing" ||
294
- state.work.publication.kind === "terminal_snapshot")) ||
303
+ state.work.publication.kind === "terminal_snapshot" ||
304
+ state.work.publication.kind === "terminal_pending_ack")) ||
295
305
  state.work.deliveryHeadSequence !== null
296
306
  )
297
307
  return "busy";
@@ -405,87 +415,39 @@ function hasActivePublication(state: WorkerRuntimeStateMachine): boolean {
405
415
  return state.work.publication.kind !== "none";
406
416
  }
407
417
 
408
- function requireNoPublication(state: WorkerRuntimeStateMachine): void {
409
- if (hasActivePublication(state)) throw new Error("Snapshot publication is already active");
410
- }
411
-
412
- function beginReadySnapshot(
413
- state: WorkerRuntimeStateMachine,
414
- source: WorkerSnapshotSource,
415
- outputs: WorkerRuntimeOutput[],
416
- ): WorkerRuntimeStateMachine {
417
- requireNoPublication(state);
418
- const snapshot = {
419
- point: "after_worker_ready" as const,
420
- turnRecordId: null,
421
- required: false as const,
422
- };
423
- outputs.push({ kind: "upload_snapshot", ...snapshot, source });
424
- return {
425
- ...state,
426
- work: { ...state.work, publication: { kind: "ready_snapshot", snapshot } },
427
- };
428
- }
429
-
430
- function beginTerminalSnapshot(
418
+ function beginSnapshot(
431
419
  state: WorkerRuntimeStateMachine,
432
- point: "before_turn_outcome" | "before_turn_failed",
433
- turnRecordId: string,
434
- terminal: PendingTerminal,
420
+ publication: Extract<PublicationState, { snapshot: SnapshotCompletionEvent }>,
435
421
  source: WorkerSnapshotSource,
436
422
  outputs: WorkerRuntimeOutput[],
437
423
  ): WorkerRuntimeStateMachine {
438
- requireNoPublication(state);
439
- const snapshot = { point, turnRecordId, required: true as const };
440
- outputs.push({ kind: "upload_snapshot", ...snapshot, source });
441
- return {
442
- ...state,
443
- work: {
444
- ...state.work,
445
- publication: { kind: "terminal_snapshot", snapshot, terminal },
446
- },
447
- };
424
+ if (hasActivePublication(state)) throw new Error("Snapshot publication is already active");
425
+ outputs.push({ kind: "upload_snapshot", ...publication.snapshot, source });
426
+ return { ...state, work: { ...state.work, publication } };
448
427
  }
449
428
 
450
- function beginCleanupSnapshot(
451
- state: WorkerRuntimeStateMachine,
452
- source: WorkerSnapshotSource,
453
- outputs: WorkerRuntimeOutput[],
454
- ): WorkerRuntimeStateMachine {
455
- requireNoPublication(state);
456
- const snapshot = {
457
- point: "before_cleanup_completed" as const,
458
- turnRecordId: null,
459
- required: true as const,
460
- };
461
- outputs.push({ kind: "upload_snapshot", ...snapshot, source });
462
- return {
463
- ...state,
464
- work: { ...state.work, publication: { kind: "cleanup_snapshot", snapshot } },
465
- };
466
- }
429
+ const TERMINAL_ACK_RETRY_MS = 5_000;
467
430
 
468
- function beginFatalSnapshot(
431
+ function publishTerminalAndAwaitAcknowledgement(
469
432
  state: WorkerRuntimeStateMachine,
470
- fatal: PendingFatal,
471
- source: WorkerSnapshotSource,
472
- afterSnapshot: "exit" | "cleanup",
433
+ terminal: PendingTerminal,
473
434
  outputs: WorkerRuntimeOutput[],
474
435
  ): WorkerRuntimeStateMachine {
475
- requireNoPublication(state);
476
- const snapshot = {
477
- point: "before_worker_failed" as const,
478
- turnRecordId: null,
479
- required: false as const,
480
- };
481
- outputs.push({ kind: "upload_snapshot", ...snapshot, source });
482
- return {
483
- ...state,
484
- work: {
485
- ...state.work,
486
- publication: { kind: "fatal_snapshot", snapshot, fatal, afterSnapshot },
436
+ outputs.push(terminal.fact);
437
+ if (terminal.park) outputs.push(protocol("worker.lifecycle_parked", terminal.park));
438
+ return armTimer(
439
+ {
440
+ ...state,
441
+ work: {
442
+ ...state.work,
443
+ publication: { kind: "terminal_pending_ack", terminal },
444
+ },
487
445
  },
488
- };
446
+ outputs,
447
+ "terminal_ack",
448
+ TERMINAL_ACK_RETRY_MS,
449
+ terminal.correlation.turnRecordId,
450
+ );
489
451
  }
490
452
 
491
453
  function finishFatal(
@@ -539,7 +501,17 @@ function failureOutputs(
539
501
  state.phase.kind === "cleaning" && state.phase.stage === "publication"
540
502
  ? "cleanup"
541
503
  : "exit";
542
- next = beginFatalSnapshot(next, fatal, disposition.snapshot.source, afterSnapshot, outputs);
504
+ next = beginSnapshot(
505
+ next,
506
+ {
507
+ kind: "fatal_snapshot",
508
+ snapshot: { point: "before_worker_failed", turnRecordId: null, required: false },
509
+ fatal,
510
+ afterSnapshot,
511
+ },
512
+ disposition.snapshot.source,
513
+ outputs,
514
+ );
543
515
  break;
544
516
  }
545
517
  const finished = finishFatal(next, fatal);
@@ -704,11 +676,15 @@ function advance(
704
676
  )
705
677
  : { upload: false as const };
706
678
  if (cleanupSnapshot.upload && next.session) {
707
- next = beginCleanupSnapshot(
679
+ next = beginSnapshot(
708
680
  {
709
681
  ...next,
710
682
  phase: { kind: "cleaning", reason, exitAfterCleanup, stage: "publication" },
711
683
  },
684
+ {
685
+ kind: "cleanup_snapshot",
686
+ snapshot: { point: "before_cleanup_completed", turnRecordId: null, required: true },
687
+ },
712
688
  sessionSnapshotSource(next.session),
713
689
  outputs,
714
690
  );
@@ -814,9 +790,11 @@ export function reduceWorkerRuntime(
814
790
  if (state.phase.kind !== "init") break;
815
791
  next = { ...state, phase: { kind: "waiting_for_start" } };
816
792
  outputs.push(HELLO);
793
+ outputs.push(protocol("worker.bootstrap_progress", { phase: "worker_connected" }));
817
794
  break;
818
795
  case "transport_connected":
819
796
  outputs.push(HELLO);
797
+ outputs.push(protocol("worker.bootstrap_progress", { phase: "worker_connected" }));
820
798
  if (state.session) {
821
799
  outputs.push(
822
800
  protocol("worker.heartbeat", {
@@ -826,6 +804,13 @@ export function reduceWorkerRuntime(
826
804
  }),
827
805
  );
828
806
  }
807
+ if (state.work.publication.kind === "terminal_pending_ack") {
808
+ next = publishTerminalAndAwaitAcknowledgement(
809
+ state,
810
+ state.work.publication.terminal,
811
+ outputs,
812
+ );
813
+ }
829
814
  break;
830
815
  case "transport_failed": {
831
816
  const failed = failureOutputs(state, { kind: "transport", error: event.error });
@@ -866,7 +851,12 @@ export function reduceWorkerRuntime(
866
851
  outputs.push({ kind: "cancel_timer", name });
867
852
  next = {
868
853
  ...next,
869
- timers: { heartbeat: null, acceptance_retry: null, credential_poll: null },
854
+ timers: {
855
+ heartbeat: null,
856
+ acceptance_retry: null,
857
+ credential_poll: null,
858
+ terminal_ack: null,
859
+ },
870
860
  credential: null,
871
861
  };
872
862
  break;
@@ -910,6 +900,7 @@ export function reduceWorkerRuntime(
910
900
  },
911
901
  sessionTainted: false,
912
902
  };
903
+ outputs.push(protocol("worker.bootstrap_progress", { phase: "preparing_workspace" }));
913
904
  outputs.push({ kind: "bootstrap", startRecordId, payload: message.payload });
914
905
  break;
915
906
  }
@@ -942,6 +933,21 @@ export function reduceWorkerRuntime(
942
933
  );
943
934
  break;
944
935
  }
936
+ if (message.type === "worker.turn_terminal_recorded") {
937
+ const publication = state.work.publication;
938
+ if (
939
+ publication.kind !== "terminal_pending_ack" ||
940
+ publication.terminal.correlation.turnRecordId !== message.payload.turnRecordId
941
+ )
942
+ break;
943
+ outputs.push({ kind: "cancel_timer", name: "terminal_ack" });
944
+ next = {
945
+ ...state,
946
+ work: { ...state.work, publication: { kind: "none" } },
947
+ timers: { ...state.timers, terminal_ack: null },
948
+ };
949
+ break;
950
+ }
945
951
  if (message.type === "worker.credential_update_accepted") {
946
952
  const credential = state.credential;
947
953
  if (
@@ -1029,6 +1035,8 @@ export function reduceWorkerRuntime(
1029
1035
  kind: "diagnostic",
1030
1036
  payload: { level: "warn", code: "bootstrap.diagnostic", message },
1031
1037
  });
1038
+ outputs.push(protocol("worker.bootstrap_progress", { phase: "loading_resources" }));
1039
+ outputs.push(protocol("worker.bootstrap_progress", { phase: "preparing_turn" }));
1032
1040
  outputs.push(protocol("worker.ready", completion.readyPayload));
1033
1041
  if (wasDraining) break;
1034
1042
  const readySnapshot = resolveWorkerSnapshotPolicy(
@@ -1037,7 +1045,15 @@ export function reduceWorkerRuntime(
1037
1045
  completion.session.piAvailable,
1038
1046
  );
1039
1047
  if (readySnapshot.upload)
1040
- next = beginReadySnapshot(next, sessionSnapshotSource(completion.session), outputs);
1048
+ next = beginSnapshot(
1049
+ next,
1050
+ {
1051
+ kind: "ready_snapshot",
1052
+ snapshot: { point: "after_worker_ready", turnRecordId: null, required: false },
1053
+ },
1054
+ sessionSnapshotSource(completion.session),
1055
+ outputs,
1056
+ );
1041
1057
  next = armTimer(
1042
1058
  next,
1043
1059
  outputs,
@@ -1108,7 +1124,13 @@ export function reduceWorkerRuntime(
1108
1124
  });
1109
1125
  break;
1110
1126
  case "activation_failed":
1111
- if (!matchesActivation(state, event.startRecordId, event.turnRecordId)) break;
1127
+ case "turn_defect":
1128
+ if (
1129
+ event.kind === "activation_failed"
1130
+ ? !matchesActivation(state, event.startRecordId, event.turnRecordId)
1131
+ : !matchesTurn(state, event.turnRecordId)
1132
+ )
1133
+ break;
1112
1134
  if (state.phase.kind === "draining") {
1113
1135
  next = finishDrainingOperation(state);
1114
1136
  } else {
@@ -1192,6 +1214,7 @@ export function reduceWorkerRuntime(
1192
1214
  forkPiEntryId: event.result.meta.forkPiEntryId ?? null,
1193
1215
  resultPiEntryId: event.result.meta.resultPiEntryId ?? null,
1194
1216
  turnResultMarkdown: event.result.meta.turnResultMarkdown ?? null,
1217
+ resultSummary: event.result.meta.resultSummary,
1195
1218
  rootEntryId: event.result.meta.rootEntryId ?? null,
1196
1219
  } as never),
1197
1220
  park: null,
@@ -1225,8 +1248,7 @@ export function reduceWorkerRuntime(
1225
1248
  state.phase.kind === "active" ? { ...state.phase, turnStatus: "completed" } : next.phase,
1226
1249
  };
1227
1250
  if (state.session?.kind === "automatic") {
1228
- outputs.push(pending.fact);
1229
- if (pending.park) outputs.push(protocol("worker.lifecycle_parked", pending.park));
1251
+ next = publishTerminalAndAwaitAcknowledgement(next, pending, outputs);
1230
1252
  } else if (state.session) {
1231
1253
  const point =
1232
1254
  event.result.kind === "outcome" ? "before_turn_outcome" : "before_turn_failed";
@@ -1237,98 +1259,33 @@ export function reduceWorkerRuntime(
1237
1259
  );
1238
1260
  if (!snapshotPolicy.upload)
1239
1261
  throw new Error("LLM terminal snapshot policy unexpectedly skipped upload");
1240
- next = beginTerminalSnapshot(
1262
+ next = beginSnapshot(
1241
1263
  next,
1242
- point,
1243
- event.turnRecordId,
1244
- pending,
1264
+ {
1265
+ kind: "terminal_snapshot",
1266
+ snapshot: { point, turnRecordId: event.turnRecordId, required: true },
1267
+ terminal: pending,
1268
+ },
1245
1269
  sessionSnapshotSource(state.session),
1246
1270
  outputs,
1247
1271
  );
1248
1272
  }
1249
1273
  break;
1250
1274
  }
1251
- case "turn_defect":
1252
- if (!matchesTurn(state, event.turnRecordId)) break;
1253
- if (state.phase.kind === "draining") {
1254
- next = finishDrainingOperation(state);
1255
- } else {
1256
- const failed = failureOutputs(next, {
1257
- kind: "dispatch",
1258
- error: event.error,
1259
- state: "busy",
1260
- session: failureContext(state),
1261
- });
1262
- next = failed.state;
1263
- outputs.push(...failed.outputs);
1264
- }
1265
- break;
1266
- case "snapshot_succeeded": {
1267
- const publication = state.work.publication;
1268
- switch (publication.kind) {
1269
- case "none":
1270
- case "fatal_pending_cleanup":
1271
- break;
1272
- case "ready_snapshot":
1273
- if (!snapshotMatches(publication.snapshot, event)) break;
1274
- next = { ...state, work: { ...state.work, publication: { kind: "none" } } };
1275
- break;
1276
- case "terminal_snapshot":
1277
- if (!snapshotMatches(publication.snapshot, event)) break;
1278
- outputs.push(publication.terminal.fact);
1279
- if (publication.terminal.park)
1280
- outputs.push(protocol("worker.lifecycle_parked", publication.terminal.park));
1281
- next = { ...state, work: { ...state.work, publication: { kind: "none" } } };
1282
- break;
1283
- case "cleanup_snapshot":
1284
- if (!snapshotMatches(publication.snapshot, event)) break;
1285
- if (state.phase.kind !== "cleaning" || state.phase.stage !== "publication")
1286
- throw new Error("Cleanup snapshot completed outside cleanup publication");
1287
- outputs.push({ kind: "cleanup" });
1288
- next = {
1289
- ...state,
1290
- phase: { ...state.phase, stage: "resources" },
1291
- work: { ...state.work, publication: { kind: "none" } },
1292
- };
1293
- break;
1294
- case "fatal_snapshot":
1295
- if (!snapshotMatches(publication.snapshot, event)) break;
1296
- if (publication.afterSnapshot === "exit") {
1297
- const finished = finishFatal(state, publication.fatal);
1298
- next = finished.state;
1299
- outputs.push(...finished.outputs);
1300
- } else {
1301
- if (state.phase.kind !== "cleaning" || state.phase.stage !== "publication")
1302
- throw new Error("Fatal snapshot completed outside cleanup publication");
1303
- outputs.push({ kind: "cleanup" });
1304
- next = {
1305
- ...state,
1306
- phase: { ...state.phase, stage: "resources" },
1307
- work: {
1308
- ...state.work,
1309
- publication: {
1310
- kind: "fatal_pending_cleanup",
1311
- fatal: publication.fatal,
1312
- },
1313
- },
1314
- };
1315
- }
1316
- break;
1317
- }
1318
- break;
1319
- }
1275
+ case "snapshot_succeeded":
1320
1276
  case "snapshot_failed": {
1321
1277
  const publication = state.work.publication;
1278
+ if (!("snapshot" in publication) || !snapshotMatches(publication.snapshot, event)) break;
1279
+ const completion = event.kind === "snapshot_succeeded" ? "completed" : "failed";
1322
1280
  switch (publication.kind) {
1323
- case "none":
1324
- case "fatal_pending_cleanup":
1325
- break;
1326
1281
  case "ready_snapshot":
1327
- if (!snapshotMatches(publication.snapshot, event)) break;
1328
1282
  next = { ...state, work: { ...state.work, publication: { kind: "none" } } };
1329
1283
  break;
1330
1284
  case "terminal_snapshot": {
1331
- if (!snapshotMatches(publication.snapshot, event)) break;
1285
+ if (event.kind === "snapshot_succeeded") {
1286
+ next = publishTerminalAndAwaitAcknowledgement(state, publication.terminal, outputs);
1287
+ break;
1288
+ }
1332
1289
  const cleared = {
1333
1290
  ...state,
1334
1291
  work: { ...state.work, publication: { kind: "none" } as const },
@@ -1345,41 +1302,41 @@ export function reduceWorkerRuntime(
1345
1302
  break;
1346
1303
  }
1347
1304
  case "cleanup_snapshot": {
1348
- if (!snapshotMatches(publication.snapshot, event)) break;
1349
1305
  if (state.phase.kind !== "cleaning" || state.phase.stage !== "publication")
1350
- throw new Error("Cleanup snapshot failed outside cleanup publication");
1306
+ throw new Error(`Cleanup snapshot ${completion} outside cleanup publication`);
1351
1307
  const cleared = {
1352
1308
  ...state,
1353
1309
  work: { ...state.work, publication: { kind: "none" } as const },
1354
1310
  };
1355
- const failed = failureOutputs(cleared, {
1356
- kind: "cleanup",
1357
- error: event.error,
1358
- session: failureContext(state),
1359
- });
1360
- next = failed.state;
1361
- outputs.push(...failed.outputs);
1311
+ if (event.kind === "snapshot_succeeded") {
1312
+ outputs.push({ kind: "cleanup" });
1313
+ next = { ...cleared, phase: { ...state.phase, stage: "resources" } };
1314
+ } else {
1315
+ const failed = failureOutputs(cleared, {
1316
+ kind: "cleanup",
1317
+ error: event.error,
1318
+ session: failureContext(state),
1319
+ });
1320
+ next = failed.state;
1321
+ outputs.push(...failed.outputs);
1322
+ }
1362
1323
  break;
1363
1324
  }
1364
1325
  case "fatal_snapshot":
1365
- if (!snapshotMatches(publication.snapshot, event)) break;
1366
1326
  if (publication.afterSnapshot === "exit") {
1367
1327
  const finished = finishFatal(state, publication.fatal);
1368
1328
  next = finished.state;
1369
1329
  outputs.push(...finished.outputs);
1370
1330
  } else {
1371
1331
  if (state.phase.kind !== "cleaning" || state.phase.stage !== "publication")
1372
- throw new Error("Fatal snapshot failed outside cleanup publication");
1332
+ throw new Error(`Fatal snapshot ${completion} outside cleanup publication`);
1373
1333
  outputs.push({ kind: "cleanup" });
1374
1334
  next = {
1375
1335
  ...state,
1376
1336
  phase: { ...state.phase, stage: "resources" },
1377
1337
  work: {
1378
1338
  ...state.work,
1379
- publication: {
1380
- kind: "fatal_pending_cleanup",
1381
- fatal: publication.fatal,
1382
- },
1339
+ publication: { kind: "fatal_pending_cleanup", fatal: publication.fatal },
1383
1340
  },
1384
1341
  };
1385
1342
  }
@@ -1416,6 +1373,16 @@ export function reduceWorkerRuntime(
1416
1373
  }),
1417
1374
  );
1418
1375
  next = armTimer(next, outputs, "acceptance_retry", 1_000, event.correlation);
1376
+ } else if (
1377
+ event.name === "terminal_ack" &&
1378
+ state.work.publication.kind === "terminal_pending_ack" &&
1379
+ state.work.publication.terminal.correlation.turnRecordId === event.correlation
1380
+ ) {
1381
+ next = publishTerminalAndAwaitAcknowledgement(
1382
+ next,
1383
+ state.work.publication.terminal,
1384
+ outputs,
1385
+ );
1419
1386
  } else if (
1420
1387
  event.name === "credential_poll" &&
1421
1388
  state.credential?.enabled &&
@@ -30,6 +30,7 @@ import {
30
30
  } from "../missing-tool-recovery-runner.js";
31
31
  import type { PiTreeHandle } from "../pi-adapter.js";
32
32
  import {
33
+ type AppliedTargetedInputMetadata,
33
34
  applyPreTurnTargetedInputs,
34
35
  type PreTurnTargetedInput,
35
36
  resolvePreTurnTargetStartSelection,
@@ -108,13 +109,7 @@ export interface LlmTurnExecutorCallbacks {
108
109
  onPreTurnTargetedInputApplied?(
109
110
  inputId: string,
110
111
  sequence: number,
111
- meta?: {
112
- currentPrimaryPathLeafId?: string | null;
113
- rootEntryId?: string | null;
114
- targetSemanticRef?: import("@leitwerk-dev/domain").ProcessSemanticEntryRefKey | null;
115
- targetProductName?: string | null;
116
- targetEntryId?: string | null;
117
- },
112
+ meta?: AppliedTargetedInputMetadata,
118
113
  ): void;
119
114
  emit?: WorkerOperationEmitter;
120
115
  integrationTools?: readonly import("@leitwerk-dev/process-sdk").PiCustomTool[];
@@ -123,16 +118,6 @@ export interface LlmTurnExecutorCallbacks {
123
118
  /** Pi writes these on new sessions before any conversational content exists. */
124
119
  const PI_SESSION_BOOTSTRAP_ENTRY_TYPES = new Set(["model_change", "thinking_level_change"]);
125
120
 
126
- function visitPiTreeEntries(
127
- nodes: readonly PiTreeNode[],
128
- visit: (entryType: string) => void,
129
- ): void {
130
- for (const node of nodes) {
131
- visit(node.entry.type);
132
- visitPiTreeEntries(node.children, visit);
133
- }
134
- }
135
-
136
121
  /**
137
122
  * Empty-tree prepared starts mean "no conversational fork yet". Pi still appends
138
123
  * model/thinking bootstrap entries when activating a new session, so those alone
@@ -141,13 +126,9 @@ function visitPiTreeEntries(
141
126
  export function isPiTreeEmptyForPreparedEmptyPlan(
142
127
  piHandle: Pick<PiTreeHandle, "getTree">,
143
128
  ): boolean {
144
- let hasNonBootstrap = false;
145
- visitPiTreeEntries(piHandle.getTree(), (entryType) => {
146
- if (!PI_SESSION_BOOTSTRAP_ENTRY_TYPES.has(entryType)) {
147
- hasNonBootstrap = true;
148
- }
149
- });
150
- return !hasNonBootstrap;
129
+ const isBootstrapOnly = (node: PiTreeNode): boolean =>
130
+ PI_SESSION_BOOTSTRAP_ENTRY_TYPES.has(node.entry.type) && node.children.every(isBootstrapOnly);
131
+ return piHandle.getTree().every(isBootstrapOnly);
151
132
  }
152
133
 
153
134
  /**
@@ -210,6 +191,7 @@ function treePlanFromPreparedStart(input: {
210
191
  export async function executeLlmTurn<TOutcome extends string>(input: {
211
192
  resolvedWorkerProcess: ResolvedWorkerProcess;
212
193
  ctx: WorkerProcessContext;
194
+ prepared?: unknown;
213
195
  piHandle: PiTreeHandle;
214
196
  turnDef: LlmTurnDefinition<TOutcome, unknown, unknown>;
215
197
  turnId: string;
@@ -467,7 +449,7 @@ export async function executeLlmTurn<TOutcome extends string>(input: {
467
449
  initialPromptlessTurnResumeReason === "active_turn_resume"
468
450
  ) {
469
451
  try {
470
- promptText = await input.turnDef.prompt(input.ctx);
452
+ promptText = await input.turnDef.prompt({ ...input.ctx, prepared: input.prepared });
471
453
  } catch (error: unknown) {
472
454
  return failWithWorkerError(
473
455
  "turn.prompt_preparation_failed",
@@ -665,15 +647,15 @@ export async function executeLlmTurn<TOutcome extends string>(input: {
665
647
  const finalCompletionState = recoveryResult.finalAttempt.completionState;
666
648
  const finalPromptResult = recoveryResult.finalAttempt.promptResult;
667
649
  const finalResolvedOutcome = recoveryResult.finalAttempt.resolvedOutcome;
668
- if (!recoveryResult.recovered) {
650
+ if (recoveryResult.failure) {
651
+ const { recoveryContext, description } = recoveryResult.failure;
669
652
  return reportFailedTurn(
670
653
  "protocol_error",
671
- `Turn '${turnId}' completed without the required tool calls after ${MAX_MISSING_TOOL_CALL_RECOVERY_ATTEMPTS} automatic recovery attempts: ${recoveryResult.baseMissingToolRecoveryDescription}`,
654
+ `Turn '${turnId}' completed without the required tool calls after ${MAX_MISSING_TOOL_CALL_RECOVERY_ATTEMPTS} automatic recovery attempts: ${description}`,
672
655
  finalPromptResult.resultEntryId,
673
656
  {
674
- recoveryContext: recoveryResult.recoveryContext,
675
- restorePrimaryLeaf:
676
- recoveryResult.recoveryContext?.failureCode !== "missing_markdown_result",
657
+ recoveryContext,
658
+ restorePrimaryLeaf: recoveryContext.failureCode !== "missing_markdown_result",
677
659
  },
678
660
  );
679
661
  }
@@ -723,6 +705,7 @@ export async function executeLlmTurn<TOutcome extends string>(input: {
723
705
  forkPiEntryId: treePlan.forkPiEntryId,
724
706
  resultPiEntryId: finalPromptResult.resultEntryId,
725
707
  turnResultMarkdown: finalizedTurnResultMarkdown.markdown,
708
+ resultSummary: finalCompletionState.markdownState.resultSummary,
726
709
  rootEntryId: rootEntryIdAfterTurn,
727
710
  },
728
711
  };