@leitwerk-dev/worker 0.1.8 → 0.1.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leitwerk-dev/worker",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/leitwerk-dev/leitwerk.git",
@@ -25,12 +25,12 @@
25
25
  "dependencies": {
26
26
  "@earendil-works/pi-ai": "^0.81.1",
27
27
  "@earendil-works/pi-coding-agent": "^0.81.1",
28
- "@leitwerk-dev/domain": "0.1.8",
29
- "@leitwerk-dev/extension-runtime": "0.1.8",
30
- "@leitwerk-dev/process-sdk": "0.1.8",
31
- "@leitwerk-dev/protocol": "0.1.8",
32
- "@leitwerk-dev/watcher-utils": "0.1.8",
33
- "@leitwerk-dev/worker-protocol": "0.1.8",
28
+ "@leitwerk-dev/domain": "0.1.9",
29
+ "@leitwerk-dev/extension-runtime": "0.1.9",
30
+ "@leitwerk-dev/process-sdk": "0.1.9",
31
+ "@leitwerk-dev/protocol": "0.1.9",
32
+ "@leitwerk-dev/watcher-utils": "0.1.9",
33
+ "@leitwerk-dev/worker-protocol": "0.1.9",
34
34
  "execa": "^9.6.1",
35
35
  "fast-glob": "^3.3.3",
36
36
  "valibot": "^1.3.1"
@@ -1,4 +1,4 @@
1
- import type { WorkerErrorClass } from "@leitwerk-dev/domain";
1
+ import type { TurnProgressReport, WorkerErrorClass } from "@leitwerk-dev/domain";
2
2
 
3
3
  export type WorkerDiagnosticLevel = "debug" | "info" | "warn" | "error";
4
4
 
@@ -16,6 +16,7 @@ export interface WorkerDiagnosticPayload {
16
16
 
17
17
  export type WorkerOperationEmission =
18
18
  | { kind: "trace" | "error"; payload: WorkerDiagnosticPayload }
19
+ | { kind: "progress"; turnRecordId: string; report: TurnProgressReport }
19
20
  | { kind: "session_tainted"; reason: string };
20
21
 
21
22
  export type WorkerOperationEmitter = (emission: WorkerOperationEmission) => void;
@@ -210,6 +210,12 @@ export function createWorkerRuntime(options: WorkerRuntimeOptions): WorkerRuntim
210
210
  complete({ kind: "session_tainted", reason: emission.reason });
211
211
  } else if (emission.kind === "trace") {
212
212
  reporter.workerTrace(emission.payload, command.session.selectedTurnId);
213
+ } else if (emission.kind === "progress") {
214
+ reporter.workerEvent(
215
+ "turn.progress",
216
+ { turnRecordId: emission.turnRecordId, report: emission.report },
217
+ command.session.selectedTurnId,
218
+ );
213
219
  } else reporter.workerError(emission.payload, command.session.selectedTurnId);
214
220
  },
215
221
  }),
@@ -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";
@@ -447,6 +457,30 @@ function beginTerminalSnapshot(
447
457
  };
448
458
  }
449
459
 
460
+ const TERMINAL_ACK_RETRY_MS = 5_000;
461
+
462
+ function publishTerminalAndAwaitAcknowledgement(
463
+ state: WorkerRuntimeStateMachine,
464
+ terminal: PendingTerminal,
465
+ outputs: WorkerRuntimeOutput[],
466
+ ): WorkerRuntimeStateMachine {
467
+ outputs.push(terminal.fact);
468
+ if (terminal.park) outputs.push(protocol("worker.lifecycle_parked", terminal.park));
469
+ return armTimer(
470
+ {
471
+ ...state,
472
+ work: {
473
+ ...state.work,
474
+ publication: { kind: "terminal_pending_ack", terminal },
475
+ },
476
+ },
477
+ outputs,
478
+ "terminal_ack",
479
+ TERMINAL_ACK_RETRY_MS,
480
+ terminal.correlation.turnRecordId,
481
+ );
482
+ }
483
+
450
484
  function beginCleanupSnapshot(
451
485
  state: WorkerRuntimeStateMachine,
452
486
  source: WorkerSnapshotSource,
@@ -826,6 +860,13 @@ export function reduceWorkerRuntime(
826
860
  }),
827
861
  );
828
862
  }
863
+ if (state.work.publication.kind === "terminal_pending_ack") {
864
+ next = publishTerminalAndAwaitAcknowledgement(
865
+ state,
866
+ state.work.publication.terminal,
867
+ outputs,
868
+ );
869
+ }
829
870
  break;
830
871
  case "transport_failed": {
831
872
  const failed = failureOutputs(state, { kind: "transport", error: event.error });
@@ -866,7 +907,12 @@ export function reduceWorkerRuntime(
866
907
  outputs.push({ kind: "cancel_timer", name });
867
908
  next = {
868
909
  ...next,
869
- timers: { heartbeat: null, acceptance_retry: null, credential_poll: null },
910
+ timers: {
911
+ heartbeat: null,
912
+ acceptance_retry: null,
913
+ credential_poll: null,
914
+ terminal_ack: null,
915
+ },
870
916
  credential: null,
871
917
  };
872
918
  break;
@@ -942,6 +988,21 @@ export function reduceWorkerRuntime(
942
988
  );
943
989
  break;
944
990
  }
991
+ if (message.type === "worker.turn_terminal_recorded") {
992
+ const publication = state.work.publication;
993
+ if (
994
+ publication.kind !== "terminal_pending_ack" ||
995
+ publication.terminal.correlation.turnRecordId !== message.payload.turnRecordId
996
+ )
997
+ break;
998
+ outputs.push({ kind: "cancel_timer", name: "terminal_ack" });
999
+ next = {
1000
+ ...state,
1001
+ work: { ...state.work, publication: { kind: "none" } },
1002
+ timers: { ...state.timers, terminal_ack: null },
1003
+ };
1004
+ break;
1005
+ }
945
1006
  if (message.type === "worker.credential_update_accepted") {
946
1007
  const credential = state.credential;
947
1008
  if (
@@ -1225,8 +1286,7 @@ export function reduceWorkerRuntime(
1225
1286
  state.phase.kind === "active" ? { ...state.phase, turnStatus: "completed" } : next.phase,
1226
1287
  };
1227
1288
  if (state.session?.kind === "automatic") {
1228
- outputs.push(pending.fact);
1229
- if (pending.park) outputs.push(protocol("worker.lifecycle_parked", pending.park));
1289
+ next = publishTerminalAndAwaitAcknowledgement(next, pending, outputs);
1230
1290
  } else if (state.session) {
1231
1291
  const point =
1232
1292
  event.result.kind === "outcome" ? "before_turn_outcome" : "before_turn_failed";
@@ -1267,6 +1327,7 @@ export function reduceWorkerRuntime(
1267
1327
  const publication = state.work.publication;
1268
1328
  switch (publication.kind) {
1269
1329
  case "none":
1330
+ case "terminal_pending_ack":
1270
1331
  case "fatal_pending_cleanup":
1271
1332
  break;
1272
1333
  case "ready_snapshot":
@@ -1275,10 +1336,7 @@ export function reduceWorkerRuntime(
1275
1336
  break;
1276
1337
  case "terminal_snapshot":
1277
1338
  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" } } };
1339
+ next = publishTerminalAndAwaitAcknowledgement(state, publication.terminal, outputs);
1282
1340
  break;
1283
1341
  case "cleanup_snapshot":
1284
1342
  if (!snapshotMatches(publication.snapshot, event)) break;
@@ -1321,6 +1379,7 @@ export function reduceWorkerRuntime(
1321
1379
  const publication = state.work.publication;
1322
1380
  switch (publication.kind) {
1323
1381
  case "none":
1382
+ case "terminal_pending_ack":
1324
1383
  case "fatal_pending_cleanup":
1325
1384
  break;
1326
1385
  case "ready_snapshot":
@@ -1416,6 +1475,16 @@ export function reduceWorkerRuntime(
1416
1475
  }),
1417
1476
  );
1418
1477
  next = armTimer(next, outputs, "acceptance_retry", 1_000, event.correlation);
1478
+ } else if (
1479
+ event.name === "terminal_ack" &&
1480
+ state.work.publication.kind === "terminal_pending_ack" &&
1481
+ state.work.publication.terminal.correlation.turnRecordId === event.correlation
1482
+ ) {
1483
+ next = publishTerminalAndAwaitAcknowledgement(
1484
+ next,
1485
+ state.work.publication.terminal,
1486
+ outputs,
1487
+ );
1419
1488
  } else if (
1420
1489
  event.name === "credential_poll" &&
1421
1490
  state.credential?.enabled &&
@@ -1,4 +1,8 @@
1
- import type { ProcessSemanticEntryRefKey } from "@leitwerk-dev/domain";
1
+ import {
2
+ failActiveTurnProgress,
3
+ type ProcessSemanticEntryRefKey,
4
+ type TurnProgressReport,
5
+ } from "@leitwerk-dev/domain";
2
6
  import type {
3
7
  LlmTurnDefinition,
4
8
  TurnOptions,
@@ -83,6 +87,8 @@ export async function executeSelectedTurn(
83
87
  }
84
88
  const handler = resolvedWorkerProcess.definition.turns.get(currentTurnId);
85
89
  if (!handler) throw new Error(`Validated turn handler '${currentTurnId}' is unavailable`);
90
+ let automaticIntegrationCallIndex = 0;
91
+ let latestProgressReport: TurnProgressReport | null = null;
86
92
 
87
93
  const ctx = {
88
94
  process: processSnapshot,
@@ -92,6 +98,27 @@ export async function executeSelectedTurn(
92
98
  turnResultMarkdownBySemanticRef: input.session.turnResultMarkdownBySemanticRef,
93
99
  turnResultMarkdownByProduct: input.session.turnResultMarkdownByProduct,
94
100
  workspaceRoot: input.session.workspaceRoot,
101
+ ...(selectedTurnType === "automatic"
102
+ ? {
103
+ reportProgress(report: TurnProgressReport) {
104
+ latestProgressReport = report;
105
+ input.emit({ kind: "progress", turnRecordId: input.turnRecordId, report });
106
+ },
107
+ async callIntegrationTool(name: string, args: Record<string, unknown>) {
108
+ const tool = input.integrationTools?.find((candidate) => candidate.name === name);
109
+ if (!tool) {
110
+ throw new Error(
111
+ `Integration tool '${name}' is not authorized for turn '${currentTurnId}'`,
112
+ );
113
+ }
114
+ automaticIntegrationCallIndex += 1;
115
+ return tool.execute(args, {
116
+ toolCallId: `${input.turnRecordId}:${automaticIntegrationCallIndex}:${name}`,
117
+ signal: input.signal,
118
+ });
119
+ },
120
+ }
121
+ : {}),
95
122
  } as WorkerProcessContext;
96
123
  let terminal: SelectedTurnExecutionResult | null = null;
97
124
  const appliedTargetedInputs: AppliedTargetedInput[] = [];
@@ -197,6 +224,14 @@ export async function executeSelectedTurn(
197
224
  }
198
225
  return terminal;
199
226
  } catch (error) {
227
+ const failedProgressReport = latestProgressReport as TurnProgressReport | null;
228
+ if (failedProgressReport) {
229
+ input.emit({
230
+ kind: "progress",
231
+ turnRecordId: input.turnRecordId,
232
+ report: failActiveTurnProgress(failedProgressReport),
233
+ });
234
+ }
200
235
  if (error instanceof TurnExecutionFailure) return failedResult(error, appliedTargetedInputs);
201
236
  if (automaticExecutor)
202
237
  return failedResult(automaticExecutor.failure(error), appliedTargetedInputs);