@opengeni/worker-bundle 0.7.0 → 0.7.2

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 (45) hide show
  1. package/dist/index.d.ts +182 -67
  2. package/dist/index.js +7127 -1654
  3. package/dist/index.js.map +1 -1
  4. package/package.json +19 -19
  5. package/src/activities/agent-turn.ts +4405 -1089
  6. package/src/activities/capabilities.ts +43 -8
  7. package/src/activities/codex-capacity.ts +223 -0
  8. package/src/activities/codex-rotation.ts +550 -54
  9. package/src/activities/common.ts +7 -1
  10. package/src/activities/context-compaction.ts +152 -118
  11. package/src/activities/documents.ts +32 -27
  12. package/src/activities/environment.ts +318 -75
  13. package/src/activities/file-upload-reaper.ts +79 -0
  14. package/src/activities/git-credential-renewal.ts +183 -0
  15. package/src/activities/goals.ts +142 -123
  16. package/src/activities/packs.ts +55 -5
  17. package/src/activities/parent-wake.ts +228 -41
  18. package/src/activities/recording.ts +154 -122
  19. package/src/activities/redaction.ts +3 -1
  20. package/src/activities/rig-verification.ts +441 -0
  21. package/src/activities/run-input.ts +110 -122
  22. package/src/activities/sandbox-lease.ts +186 -51
  23. package/src/activities/scheduled-tasks.ts +287 -171
  24. package/src/activities/session-state.ts +168 -157
  25. package/src/activities/streaming.ts +143 -19
  26. package/src/activities/turn-attempt-fenced.ts +14 -0
  27. package/src/activities/types.ts +102 -32
  28. package/src/activities/workflow-wake.ts +44 -0
  29. package/src/activities/workspace-capture.ts +964 -0
  30. package/src/activities.ts +75 -35
  31. package/src/concurrency.ts +11 -0
  32. package/src/http.ts +16 -6
  33. package/src/index.ts +322 -49
  34. package/src/observability-metrics.ts +441 -8
  35. package/src/op-journal.ts +46 -0
  36. package/src/sandbox-resume.ts +431 -201
  37. package/src/sandbox-routing.ts +209 -22
  38. package/src/workflow-wake-contract.ts +5 -0
  39. package/src/workflows/activities.ts +58 -7
  40. package/src/workflows/file-upload-reaper.ts +12 -0
  41. package/src/workflows/rig-verification.ts +28 -0
  42. package/src/workflows/scheduled-tasks.ts +12 -26
  43. package/src/workflows/session.ts +330 -173
  44. package/src/workflows/workflow-wake.ts +12 -0
  45. package/src/workflows.ts +9 -7
@@ -1,5 +1,9 @@
1
1
  import type { Settings } from "@opengeni/config";
2
- import type { ConnectionCredentialsPort, EntitlementsPort, ScheduledTaskTriggerType } from "@opengeni/contracts";
2
+ import type {
3
+ ConnectionCredentialsPort,
4
+ EntitlementsPort,
5
+ ScheduledTaskTriggerType,
6
+ } from "@opengeni/contracts";
3
7
  import type { Database } from "@opengeni/db";
4
8
  import type { DocumentServices } from "@opengeni/documents";
5
9
  import type { EventBus } from "@opengeni/events";
@@ -13,9 +17,24 @@ import type { ObjectStorage } from "@opengeni/storage";
13
17
  // its workflow run complete, so a plain signal would not start one — this must
14
18
  // signalWithStart. Injected (not built from the worker's NativeConnection)
15
19
  // because the worker package owns only the worker runtime, not a client; an
16
- // undefined signaler degrades to "DB wake recorded, no workflow nudge" (the
17
- // turn is still claimed on the parent's next natural wake).
18
- export type WakeSessionWorkflowSignal = (input: { accountId: string; workspaceId: string; sessionId: string; workflowId: string }) => Promise<void>;
20
+ // missing signaler leaves the committed outbox revision for the global repair
21
+ // sweep; production workers always inject this dependency.
22
+ export type WakeSessionWorkflowSignal = (input: {
23
+ accountId: string;
24
+ workspaceId: string;
25
+ sessionId: string;
26
+ workflowId: string;
27
+ wakeRevision: number;
28
+ controlEventId?: string;
29
+ }) => Promise<void>;
30
+
31
+ export type SignalCodexCapacityWorkflow = (input: {
32
+ accountId: string;
33
+ workspaceId: string;
34
+ sessionId: string;
35
+ workflowId: string;
36
+ wakeRevision: number;
37
+ }) => Promise<void>;
19
38
 
20
39
  export type ActivityServices = {
21
40
  settings: Settings;
@@ -26,6 +45,8 @@ export type ActivityServices = {
26
45
  documentServices: DocumentServices;
27
46
  observability: Observability;
28
47
  wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
48
+ /** Revision-carrying capacity nudge; generic outbox repair is also sufficient. */
49
+ signalCodexCapacityWorkflow?: SignalCodexCapacityWorkflow | null;
29
50
  // §7.5 P3 — host-entitlements port, the WORKER half of the same seam the API
30
51
  // edge exposes on `AppDependencies`. When set, `ensureRunAllowed` (turn-entry
31
52
  // AND the mid-stream budget valve) delegates the funding decision to
@@ -58,33 +79,72 @@ export type ActivityServices = {
58
79
  connectionCredentials?: ConnectionCredentialsPort | null;
59
80
  };
60
81
 
82
+ export type CodexCapacityWaitRef = {
83
+ waiterId: string;
84
+ generation: number;
85
+ nextCheckAt: string;
86
+ wakeRevision: number;
87
+ };
88
+
89
+ export type GetCodexCapacityWaitInput = {
90
+ workspaceId: string;
91
+ sessionId: string;
92
+ };
93
+
94
+ export type ReconcileCodexCapacityWaitInput = {
95
+ accountId: string;
96
+ workspaceId: string;
97
+ sessionId: string;
98
+ waiterId: string;
99
+ generation: number;
100
+ cause: "timer" | "signal" | "queue" | "recovery";
101
+ };
102
+
103
+ export type ReconcileCodexCapacityWaitResult =
104
+ | ({ action: "waiting" } & CodexCapacityWaitRef)
105
+ | { action: "resumed"; updateId: string }
106
+ | { action: "superseded" | "stale" };
107
+
61
108
  export type ActivityDependencies = Partial<ActivityServices>;
62
109
 
63
110
  export type RunAgentTurnInput = {
64
111
  accountId: string;
65
112
  workspaceId: string;
66
113
  sessionId: string;
67
- triggerEventId: string;
68
114
  workflowId: string;
69
- turnId?: string;
115
+ attemptId: string;
116
+ trigger: { kind: "next" } | { kind: "approval"; triggerEventId: string };
70
117
  };
71
118
 
72
- export type RequeueTurnAfterWorkerDeathInput = {
119
+ export type SettleSessionControlInput = {
73
120
  accountId: string;
74
121
  workspaceId: string;
75
122
  sessionId: string;
76
- // The trigger the dead attempt was actually running (for an approval rerun
77
- // this is the approval-decision event, not the turn row's original trigger).
78
123
  triggerEventId: string;
79
124
  workflowId: string;
80
- turnId: string;
81
125
  };
82
126
 
83
- export type RequeueTurnAfterWorkerDeathResult =
84
- // The turn is back on the queue; the session workflow's next claim
85
- // re-dispatches it on a healthy worker. `redispatches` is the total number
86
- // of worker-death re-dispatches this turn has now consumed.
87
- | { action: "requeued"; redispatches: number }
127
+ export type FailSessionAttemptInput = {
128
+ accountId: string;
129
+ workspaceId: string;
130
+ sessionId: string;
131
+ attemptId: string;
132
+ error?: string;
133
+ };
134
+
135
+ export type RecoverDispatchInput = {
136
+ accountId: string;
137
+ workspaceId: string;
138
+ sessionId: string;
139
+ attemptId: string;
140
+ timeoutType: "HEARTBEAT" | "SCHEDULE_TO_START";
141
+ };
142
+
143
+ export type RecoverDispatchResult =
144
+ // The same current inference is now recoverable. It never enters the prompt
145
+ // queue; the next claim creates a new attempt for this exact turn.
146
+ | { action: "unclaimed" }
147
+ | { action: "recovering"; turnId: string; redispatches: number }
88
148
  // The turn is no longer running/requires_action: the timed-out attempt was
89
149
  // a zombie that actually settled the turn after the server gave up on its
90
150
  // heartbeats. Nothing to redo; the workflow just continues its loop.
@@ -92,12 +152,11 @@ export type RequeueTurnAfterWorkerDeathResult =
92
152
  // The per-turn crash-loop guard tripped; the workflow must fail the
93
153
  // session for real. `redispatches` is the count already consumed (== the
94
154
  // ceiling), so the failed attempt was worker death number redispatches + 1.
95
- | { action: "exceeded"; redispatches: number };
155
+ | { action: "exceeded"; turnId: string; redispatches: number };
96
156
 
97
- export type ClaimNextQueuedTurnInput = {
157
+ export type PeekSessionWorkInput = {
98
158
  workspaceId: string;
99
159
  sessionId: string;
100
- workflowId: string;
101
160
  };
102
161
 
103
162
  export type MarkSessionIdleInput = {
@@ -116,19 +175,12 @@ export type MaybeContinueGoalResult = {
116
175
  action: "none" | "queue" | "continue" | "paused";
117
176
  };
118
177
 
119
- export type PauseGoalForInterruptInput = {
120
- workspaceId: string;
121
- sessionId: string;
122
- // The `user.interrupt` event that triggered the pause, when there is one.
123
- // A steer-tagged interrupt (reason "steer") must NOT pause the goal:
124
- // steering redirects the work, it does not stop it.
125
- triggerEventId?: string;
126
- };
127
-
128
178
  export type DispatchScheduledTaskRunInput = {
129
179
  workspaceId: string;
130
180
  taskId: string;
131
181
  triggerType: ScheduledTaskTriggerType;
182
+ /** Stable Temporal workflow identity; retries must reuse the same source row. */
183
+ producerKey?: string;
132
184
  agentRunUsageIdempotencyKey?: string;
133
185
  };
134
186
 
@@ -139,6 +191,7 @@ export type DispatchScheduledTaskRunResult = {
139
191
  sessionId: string;
140
192
  triggerEventId: string;
141
193
  workflowId: string;
194
+ workflowWakeRevision: number | null;
142
195
  };
143
196
 
144
197
  export type IndexDocumentInput = {
@@ -147,11 +200,12 @@ export type IndexDocumentInput = {
147
200
  documentId: string;
148
201
  };
149
202
 
150
- export type RunAgentTurnResult = {
151
- // "preempted": the worker hosting this turn shut down gracefully mid-turn;
152
- // the activity checkpointed conversation truth, re-queued the turn, and the
153
- // session workflow re-dispatches it on a healthy worker.
154
- status: "idle" | "requires_action" | "failed" | "cancelled" | "preempted";
203
+ type ClaimedRunAgentTurnResult = {
204
+ // "recovering": this attempt ended after durably preserving the same current
205
+ // inference for a new attempt. Recovery is not prompt queue work.
206
+ status: "idle" | "requires_action" | "failed" | "cancelled" | "recovering";
207
+ turnId: string;
208
+ attemptId: string;
155
209
  // Provider backpressure pacing: when set on an idle result, the session
156
210
  // workflow holds the loop this long before admitting the next turn (an
157
211
  // active goal's continuation would otherwise immediately re-hit the limit).
@@ -162,4 +216,20 @@ export type RunAgentTurnResult = {
162
216
  // "continue now" (invariant 4: NO THRASH). Distinct from a normal continueDelayMs:0
163
217
  // which legitimately means "a rotation candidate is ready, re-dispatch immediately".
164
218
  idleUntilReset?: boolean;
219
+ // Durable native zero-pool wait. Unlike continueDelayMs, this reference is
220
+ // persisted in Postgres and reconstructed after workflow/worker restart.
221
+ // The workflow must not call maybeContinueGoal while this waiter is active.
222
+ capacityWait?: CodexCapacityWaitRef;
223
+ // A maintenance execution could not run (for example, every Codex account
224
+ // is unavailable). End this workflow run without consuming the durable
225
+ // request; a later prompt/control wake starts the workflow again and normal
226
+ // prompt claim ordering consumes the request in-turn.
227
+ deferredUntilWake?: boolean;
165
228
  };
229
+
230
+ export type RunAgentTurnResult =
231
+ | ClaimedRunAgentTurnResult
232
+ | {
233
+ status: "unclaimed";
234
+ reason: "gate-closed" | "no-work" | "stale-approval" | "control-pending";
235
+ };
@@ -0,0 +1,44 @@
1
+ import type { ActivityServices } from "./types";
2
+ import { reconcilePendingSessionWorkflowWakes } from "./parent-wake";
3
+
4
+ const BATCH_SIZE = 1_000;
5
+ const MAX_DELIVERIES_PER_ACTIVITY = 10_000;
6
+
7
+ export type DispatchSessionWorkflowWakesResult = {
8
+ claimed: number;
9
+ delivered: number;
10
+ failed: number;
11
+ exhaustedBatchLimit: boolean;
12
+ };
13
+
14
+ /**
15
+ * Drain committed session-workflow wake revisions through the same signal and
16
+ * acknowledgement path used by immediate delivery. This is a repair path, not
17
+ * an eligibility scan: producers have already decided what must be delivered.
18
+ */
19
+ export function createWorkflowWakeActivities(services: () => Promise<ActivityServices>) {
20
+ return {
21
+ async dispatchSessionWorkflowWakes(): Promise<DispatchSessionWorkflowWakesResult> {
22
+ const service = await services();
23
+ let claimed = 0;
24
+ let delivered = 0;
25
+ let failed = 0;
26
+ for (;;) {
27
+ const remaining = MAX_DELIVERIES_PER_ACTIVITY - claimed;
28
+ if (remaining <= 0) {
29
+ return { claimed, delivered, failed, exhaustedBatchLimit: true };
30
+ }
31
+ const batch = await reconcilePendingSessionWorkflowWakes(
32
+ service,
33
+ Math.min(BATCH_SIZE, remaining),
34
+ );
35
+ claimed += batch.claimed;
36
+ delivered += batch.delivered;
37
+ failed += batch.failed;
38
+ if (batch.claimed < Math.min(BATCH_SIZE, remaining)) {
39
+ return { claimed, delivered, failed, exhaustedBatchLimit: false };
40
+ }
41
+ }
42
+ },
43
+ };
44
+ }