@opengeni/worker-bundle 0.7.4 → 0.7.5

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": "@opengeni/worker-bundle",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "description": "OpenGeni worker entry: runOpenGeniWorker/createOpenGeniWorker(deps) + the un-bundled Temporal workflow source tree. An engine-distribution surface — its runtime closure includes engine-internal packages and the workflow bundle ships as on-disk TS source.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -38,10 +38,10 @@
38
38
  "@opengeni/codex": "^0.2.3",
39
39
  "@opengeni/config": "^0.5.1",
40
40
  "@opengeni/contracts": "^0.10.0",
41
- "@opengeni/core": "^0.4.9",
42
- "@opengeni/db": "^0.7.2",
43
- "@opengeni/documents": "^0.2.11",
44
- "@opengeni/events": "^0.3.2",
41
+ "@opengeni/core": "^0.4.10",
42
+ "@opengeni/db": "^0.7.3",
43
+ "@opengeni/documents": "^0.2.12",
44
+ "@opengeni/events": "^0.3.3",
45
45
  "@opengeni/observability": "^0.3.0",
46
46
  "@opengeni/runtime": "^0.7.1",
47
47
  "@opengeni/storage": "^0.2.10",
@@ -171,7 +171,7 @@ import {
171
171
  settingsWithPackSandboxImage,
172
172
  settingsWithRigImage,
173
173
  } from "./packs";
174
- import { notifyParentOfChildTerminal } from "./parent-wake";
174
+ import { deliverFailedChildTurnToParent } from "./parent-wake";
175
175
  import { createSecretRedactor, identityRedactor } from "./redaction";
176
176
  import { applyCodexHistoryStrip, turnInput, type TurnCodexAccount } from "./run-input";
177
177
  import {
@@ -4515,12 +4515,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4515
4515
  if (settlement.action === "failed") {
4516
4516
  activityStatus = "failed";
4517
4517
  turnMetricOutcome = "failed";
4518
- await notifyParentOfChildTerminal(
4518
+ await deliverFailedChildTurnToParent(
4519
4519
  { db, bus, settings, observability, wakeSessionWorkflow },
4520
4520
  input.workspaceId,
4521
4521
  input.sessionId,
4522
- "failed",
4523
- `turn:${lostTurnId}`,
4522
+ lostTurnId,
4524
4523
  );
4525
4524
  return claimedResult({ status: "failed" });
4526
4525
  }
@@ -5163,15 +5162,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5163
5162
  // failed and returns "failed", and the session workflow then exits
5164
5163
  // WITHOUT calling failSession/markSessionIdle. Wake a spawned worker's
5165
5164
  // parent here too, so a manager learns of a worker that died inside its
5166
- // turn (not just one failed by the workflow's failSession path). Deduped
5167
- // per terminal episode by the child's lastSequence, so it never
5168
- // double-fires with the workflow-level wake.
5169
- await notifyParentOfChildTerminal(
5165
+ // turn (not just one failed by the workflow's failSession path). Turn
5166
+ // settlement already owns the durable outbox payload; this call only
5167
+ // delivers that exact turn-scoped row.
5168
+ await deliverFailedChildTurnToParent(
5170
5169
  { db, bus, settings, observability, wakeSessionWorkflow },
5171
5170
  input.workspaceId,
5172
5171
  input.sessionId,
5173
- "failed",
5174
- `turn:${turnId}`,
5172
+ turnId,
5175
5173
  );
5176
5174
  return claimedResult({ status: "failed" });
5177
5175
  } finally {
@@ -6,6 +6,7 @@ import {
6
6
  addSessionSystemUpdateWithSourceMutation,
7
7
  claimPendingSessionSystemUpdateOutbox,
8
8
  claimPendingSessionWorkflowWakes,
9
+ getSessionSystemUpdateOutboxByDedupeKey,
9
10
  getOrCreateSessionSystemUpdateOutbox,
10
11
  markSessionWorkflowWakeFailed,
11
12
  markSessionSystemUpdateOutboxDeliveredInTransaction,
@@ -33,36 +34,15 @@ export type ReconcileSessionWorkflowWakeOverrides = Partial<{
33
34
  }>;
34
35
 
35
36
  /**
36
- * Deliver exactly one completion wake to a spawned worker's parent (manager)
37
- * session when the worker reaches a terminal-for-now state. No-op for a
38
- * parentless session (direct API create / scheduled run). Idempotent per
39
- * terminal episode: the idempotency key is the child's current lastSequence,
40
- * which advances every time the child does work, so a retry of the same
41
- * terminal transition (activity retry, the workflow's idle re-check, the
42
- * runAgentTurn-failed path overlapping a workflow-level wake) is deduped while
43
- * a genuinely new idle-after-work episode notifies again. The parent receives
44
- * one typed internal update; signalling its workflow ensures an idle parent can
45
- * coalesce and process all pending updates in one inference. Failures here never
46
- * fail the child because the durable outbox remains retryable.
47
- *
48
- * Lives in its own module so both the session-state terminal activities
49
- * (markSessionIdle / failSession) and runAgentTurn's in-turn failure path can
50
- * call it without a circular import between those two activity modules.
37
+ * Enrich and deliver the durable idle-boundary row committed by
38
+ * settleSessionIdleWithParentOutbox. Idle has no single owning turn, so its
39
+ * stable episode identity is the newest non-status event sequence.
51
40
  */
52
- export async function notifyParentOfChildTerminal(
41
+ export async function notifyParentOfChildIdle(
53
42
  svc: NotifyServices,
54
43
  workspaceId: string,
55
44
  childSessionId: string,
56
- terminalStatus: "idle" | "failed",
57
- // Stable identifier for the terminal episode, used as the idempotency key so
58
- // the same completion never wakes the parent twice. A FAILURE passes the
59
- // failed turn's id: both the in-turn wake (runAgentTurn) and the
60
- // workflow-level wake (failSession, after it appends more events that would
61
- // shift lastSequence) key on the same turn, so a finally-throw that turns one
62
- // failure into both paths still dedupes. An idle episode has no single
63
- // owning turn, so it falls back to the child's lastSequence — which advances
64
- // per work batch and is stable across retries of that same idle transition.
65
- episodeKey?: string | null,
45
+ episodeKey: string,
66
46
  ): Promise<void> {
67
47
  try {
68
48
  const child = await getSession(svc.db, workspaceId, childSessionId);
@@ -75,23 +55,18 @@ export async function notifyParentOfChildTerminal(
75
55
  // what re-arms the loop the user just stopped. Suppress the resume nudge and
76
56
  // tell the agent to stay paused. Paired with the goal_set reactivation
77
57
  // guard so a nudge that slips through still cannot revive the goal.
78
- const clientEventId = `child-completion:${childSessionId}:${episodeKey ?? child.lastSequence}`;
79
- const payload = childCompletionPayload(child, goal, terminalStatus);
58
+ const clientEventId = `child-completion:${childSessionId}:${episodeKey}`;
59
+ const payload = childCompletionPayload(child, goal);
80
60
  const outbox = await getOrCreateSessionSystemUpdateOutbox(svc.db, {
81
61
  accountId: child.accountId,
82
62
  workspaceId,
83
63
  sourceSessionId: child.id,
84
64
  targetSessionId: child.parentSessionId,
85
65
  kind: "child_terminal_result",
86
- classification:
87
- terminalStatus === "failed"
88
- ? "failure"
89
- : goal?.status === "paused"
90
- ? "action_required"
91
- : "success",
66
+ classification: goal?.status === "paused" ? "action_required" : "success",
92
67
  sourceId: child.id,
93
68
  dedupeKey: clientEventId,
94
- summary: childCompletionSummary(child, goal, terminalStatus),
69
+ summary: childCompletionSummary(child, goal, "idle"),
95
70
  payload,
96
71
  lineage: { childSessionId: child.id, parentSessionId: child.parentSessionId },
97
72
  });
@@ -102,8 +77,44 @@ export async function notifyParentOfChildTerminal(
102
77
  } catch (error) {
103
78
  // A durable pending outbox row survives this boundary. The global worker
104
79
  // reaper retries it; child terminal settlement never depends on this turn.
105
- svc.observability.error("Failed to wake parent session on worker completion", {
80
+ svc.observability.error("Failed to wake parent session on worker idle boundary", {
81
+ childSessionId,
82
+ error: error instanceof Error ? error.message : String(error),
83
+ });
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Deliver the exact failure row already committed by turn settlement. This
89
+ * layer deliberately cannot create or rewrite the row: the settlement
90
+ * transaction is the sole owner of the failed turn id, payload, and lineage.
91
+ */
92
+ export async function deliverFailedChildTurnToParent(
93
+ svc: NotifyServices,
94
+ workspaceId: string,
95
+ childSessionId: string,
96
+ turnId: string,
97
+ ): Promise<void> {
98
+ try {
99
+ const child = await getSession(svc.db, workspaceId, childSessionId);
100
+ if (!child || !child.parentSessionId) return;
101
+ const dedupeKey = `child-completion:${childSessionId}:turn:${turnId}`;
102
+ const outbox = await getSessionSystemUpdateOutboxByDedupeKey(svc.db, {
103
+ accountId: child.accountId,
104
+ workspaceId,
105
+ dedupeKey,
106
+ });
107
+ if (!outbox) {
108
+ throw new Error(`Committed failed-child outbox disappeared: ${dedupeKey}`);
109
+ }
110
+ if (outbox.status === "delivered") return;
111
+ await deliverParentSystemUpdateOutbox(svc, outbox);
112
+ } catch (error) {
113
+ // Settlement already committed the retryable outbox row. The global
114
+ // reconciler remains the recovery path if immediate delivery fails.
115
+ svc.observability.error("Failed to deliver committed child-turn failure", {
106
116
  childSessionId,
117
+ turnId,
107
118
  error: error instanceof Error ? error.message : String(error),
108
119
  });
109
120
  }
@@ -231,12 +242,11 @@ export async function reconcilePendingSessionWorkflowWakes(
231
242
  function childCompletionPayload(
232
243
  child: Session,
233
244
  goal: SessionGoal | null,
234
- terminalStatus: "idle" | "failed",
235
245
  ): Extract<SessionSystemUpdatePayload, { type: "child_terminal_result" }> {
236
246
  return {
237
247
  type: "child_terminal_result",
238
248
  childSessionId: child.id,
239
- status: terminalStatus,
249
+ status: "idle",
240
250
  ...(goal
241
251
  ? {
242
252
  goal: {
@@ -10,7 +10,7 @@ import {
10
10
  settleSessionIdleWithParentOutbox,
11
11
  } from "@opengeni/db";
12
12
  import { publishDurableSessionEvents } from "@opengeni/events";
13
- import { notifyParentOfChildTerminal } from "./parent-wake";
13
+ import { deliverFailedChildTurnToParent, notifyParentOfChildIdle } from "./parent-wake";
14
14
  import { recordTurnsQueuedGauge } from "../observability-metrics";
15
15
  import type {
16
16
  ActivityServices,
@@ -33,7 +33,8 @@ export type SessionStateActivityOverrides = Partial<{
33
33
  requireSession: typeof requireSession;
34
34
  settleSessionIdleWithParentOutbox: typeof settleSessionIdleWithParentOutbox;
35
35
  publishDurableSessionEvents: typeof publishDurableSessionEvents;
36
- notifyParentOfChildTerminal: typeof notifyParentOfChildTerminal;
36
+ deliverFailedChildTurnToParent: typeof deliverFailedChildTurnToParent;
37
+ notifyParentOfChildIdle: typeof notifyParentOfChildIdle;
37
38
  recordTurnsQueuedGauge: typeof recordTurnsQueuedGauge;
38
39
  }>;
39
40
 
@@ -61,8 +62,9 @@ export function createSessionStateActivities(
61
62
  overrides.settleSessionIdleWithParentOutbox ?? settleSessionIdleWithParentOutbox;
62
63
  const publishDurableSessionEventsFn =
63
64
  overrides.publishDurableSessionEvents ?? publishDurableSessionEvents;
64
- const notifyParentOfChildTerminalFn =
65
- overrides.notifyParentOfChildTerminal ?? notifyParentOfChildTerminal;
65
+ const deliverFailedChildTurnToParentFn =
66
+ overrides.deliverFailedChildTurnToParent ?? deliverFailedChildTurnToParent;
67
+ const notifyParentOfChildIdleFn = overrides.notifyParentOfChildIdle ?? notifyParentOfChildIdle;
66
68
  const recordTurnsQueuedGaugeFn = overrides.recordTurnsQueuedGauge ?? recordTurnsQueuedGauge;
67
69
 
68
70
  async function failSessionAttempt(input: FailSessionAttemptInput): Promise<void> {
@@ -103,12 +105,11 @@ export function createSessionStateActivities(
103
105
  return;
104
106
  }
105
107
  await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, result.events);
106
- await notifyParentOfChildTerminalFn(
108
+ await deliverFailedChildTurnToParentFn(
107
109
  { db, bus, settings, observability, wakeSessionWorkflow },
108
110
  input.workspaceId,
109
111
  input.sessionId,
110
- "failed",
111
- `turn:${turn.id}`,
112
+ turn.id,
112
113
  );
113
114
  }
114
115
 
@@ -150,12 +151,11 @@ export function createSessionStateActivities(
150
151
  await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, result.events);
151
152
  await refreshQueuedTurnsGauge(db, observability, countQueuedTurnsFn, recordTurnsQueuedGaugeFn);
152
153
  if (result.action === "exceeded") {
153
- await notifyParentOfChildTerminalFn(
154
+ await deliverFailedChildTurnToParentFn(
154
155
  { db, bus, settings, observability, wakeSessionWorkflow },
155
156
  input.workspaceId,
156
157
  input.sessionId,
157
- "failed",
158
- `turn:${result.turnId}`,
158
+ result.turnId,
159
159
  );
160
160
  return {
161
161
  action: "exceeded",
@@ -196,11 +196,10 @@ export function createSessionStateActivities(
196
196
  // point for a spawned worker, whatever the cause (goal completed, agent or
197
197
  // system paused goal, goalless work finished, idle control settlement). Wake
198
198
  // the parent here, deduped per idle episode so the manager is nudged once.
199
- await notifyParentOfChildTerminalFn(
199
+ await notifyParentOfChildIdleFn(
200
200
  { db, bus, settings, observability, wakeSessionWorkflow },
201
201
  input.workspaceId,
202
202
  input.sessionId,
203
- "idle",
204
203
  settled.episodeKey,
205
204
  );
206
205
  }