@opengeni/worker-bundle 0.7.1 → 0.7.3
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/dist/index.d.ts +184 -68
- package/dist/index.js +7099 -1643
- package/dist/index.js.map +1 -1
- package/package.json +18 -18
- package/src/activities/agent-turn.ts +4332 -1068
- package/src/activities/capabilities.ts +43 -8
- package/src/activities/codex-capacity.ts +223 -0
- package/src/activities/codex-rotation.ts +550 -54
- package/src/activities/common.ts +14 -2
- package/src/activities/context-compaction.ts +152 -118
- package/src/activities/documents.ts +32 -27
- package/src/activities/environment.ts +318 -75
- package/src/activities/file-upload-reaper.ts +79 -0
- package/src/activities/git-credential-renewal.ts +183 -0
- package/src/activities/goals.ts +142 -123
- package/src/activities/packs.ts +55 -5
- package/src/activities/parent-wake.ts +216 -42
- package/src/activities/recording.ts +154 -122
- package/src/activities/redaction.ts +3 -1
- package/src/activities/rig-verification.ts +441 -0
- package/src/activities/run-input.ts +110 -122
- package/src/activities/sandbox-lease.ts +186 -51
- package/src/activities/scheduled-tasks.ts +287 -171
- package/src/activities/session-state.ts +165 -157
- package/src/activities/streaming.ts +143 -19
- package/src/activities/turn-attempt-fenced.ts +14 -0
- package/src/activities/types.ts +106 -36
- package/src/activities/workflow-wake.ts +44 -0
- package/src/activities/workspace-capture.ts +964 -0
- package/src/activities.ts +75 -35
- package/src/concurrency.ts +11 -0
- package/src/http.ts +16 -6
- package/src/index.ts +322 -49
- package/src/observability-metrics.ts +441 -8
- package/src/op-journal.ts +46 -0
- package/src/sandbox-resume.ts +431 -201
- package/src/sandbox-routing.ts +209 -22
- package/src/workflows/activities.ts +65 -8
- package/src/workflows/file-upload-reaper.ts +12 -0
- package/src/workflows/rig-verification.ts +28 -0
- package/src/workflows/scheduled-tasks.ts +12 -26
- package/src/workflows/session.ts +330 -192
- package/src/workflows/workflow-wake.ts +13 -0
- package/src/workflows.ts +9 -7
package/src/activities/types.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { Settings } from "@opengeni/config";
|
|
2
|
-
import type {
|
|
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
|
-
//
|
|
17
|
-
//
|
|
18
|
-
export type WakeSessionWorkflowSignal = (input: {
|
|
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
|
+
interruptionRequested?: boolean;
|
|
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,73 @@ 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
|
-
|
|
115
|
+
workflowRunId: string;
|
|
116
|
+
attemptId: string;
|
|
117
|
+
trigger: { kind: "next" } | { kind: "approval"; triggerEventId: string };
|
|
70
118
|
};
|
|
71
119
|
|
|
72
|
-
export type
|
|
120
|
+
export type SettleSessionInterruptionsInput = {
|
|
73
121
|
accountId: string;
|
|
74
122
|
workspaceId: string;
|
|
75
123
|
sessionId: string;
|
|
76
|
-
|
|
77
|
-
// this is the approval-decision event, not the turn row's original trigger).
|
|
78
|
-
triggerEventId: string;
|
|
124
|
+
attemptId: string;
|
|
79
125
|
workflowId: string;
|
|
80
|
-
turnId: string;
|
|
81
126
|
};
|
|
82
127
|
|
|
83
|
-
export type
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
128
|
+
export type FailSessionAttemptInput = {
|
|
129
|
+
accountId: string;
|
|
130
|
+
workspaceId: string;
|
|
131
|
+
sessionId: string;
|
|
132
|
+
attemptId: string;
|
|
133
|
+
error?: string;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export type RecoverDispatchInput = {
|
|
137
|
+
accountId: string;
|
|
138
|
+
workspaceId: string;
|
|
139
|
+
sessionId: string;
|
|
140
|
+
attemptId: string;
|
|
141
|
+
timeoutType: "HEARTBEAT" | "SCHEDULE_TO_START";
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export type RecoverDispatchResult =
|
|
145
|
+
// The same current inference is now recoverable. It never enters the prompt
|
|
146
|
+
// queue; the next claim creates a new attempt for this exact turn.
|
|
147
|
+
| { action: "unclaimed" }
|
|
148
|
+
| { action: "recovering"; turnId: string; redispatches: number }
|
|
88
149
|
// The turn is no longer running/requires_action: the timed-out attempt was
|
|
89
150
|
// a zombie that actually settled the turn after the server gave up on its
|
|
90
151
|
// heartbeats. Nothing to redo; the workflow just continues its loop.
|
|
@@ -92,12 +153,11 @@ export type RequeueTurnAfterWorkerDeathResult =
|
|
|
92
153
|
// The per-turn crash-loop guard tripped; the workflow must fail the
|
|
93
154
|
// session for real. `redispatches` is the count already consumed (== the
|
|
94
155
|
// ceiling), so the failed attempt was worker death number redispatches + 1.
|
|
95
|
-
| { action: "exceeded"; redispatches: number };
|
|
156
|
+
| { action: "exceeded"; turnId: string; redispatches: number };
|
|
96
157
|
|
|
97
|
-
export type
|
|
158
|
+
export type PeekSessionWorkInput = {
|
|
98
159
|
workspaceId: string;
|
|
99
160
|
sessionId: string;
|
|
100
|
-
workflowId: string;
|
|
101
161
|
};
|
|
102
162
|
|
|
103
163
|
export type MarkSessionIdleInput = {
|
|
@@ -116,19 +176,12 @@ export type MaybeContinueGoalResult = {
|
|
|
116
176
|
action: "none" | "queue" | "continue" | "paused";
|
|
117
177
|
};
|
|
118
178
|
|
|
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
179
|
export type DispatchScheduledTaskRunInput = {
|
|
129
180
|
workspaceId: string;
|
|
130
181
|
taskId: string;
|
|
131
182
|
triggerType: ScheduledTaskTriggerType;
|
|
183
|
+
/** Stable Temporal workflow identity; retries must reuse the same source row. */
|
|
184
|
+
producerKey?: string;
|
|
132
185
|
agentRunUsageIdempotencyKey?: string;
|
|
133
186
|
};
|
|
134
187
|
|
|
@@ -139,6 +192,7 @@ export type DispatchScheduledTaskRunResult = {
|
|
|
139
192
|
sessionId: string;
|
|
140
193
|
triggerEventId: string;
|
|
141
194
|
workflowId: string;
|
|
195
|
+
workflowWakeRevision: number | null;
|
|
142
196
|
};
|
|
143
197
|
|
|
144
198
|
export type IndexDocumentInput = {
|
|
@@ -147,14 +201,14 @@ export type IndexDocumentInput = {
|
|
|
147
201
|
documentId: string;
|
|
148
202
|
};
|
|
149
203
|
|
|
150
|
-
|
|
151
|
-
// "
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
//
|
|
157
|
-
//
|
|
204
|
+
type ClaimedRunAgentTurnResult = {
|
|
205
|
+
// "recovering": this attempt ended after durably preserving the same current
|
|
206
|
+
// inference for a new attempt. Recovery is not prompt queue work.
|
|
207
|
+
status: "idle" | "requires_action" | "failed" | "cancelled" | "recovering";
|
|
208
|
+
turnId: string;
|
|
209
|
+
attemptId: string;
|
|
210
|
+
// Provider backpressure pacing: when set on an idle or recovering result, the
|
|
211
|
+
// session workflow holds the loop this long before admitting the next attempt.
|
|
158
212
|
continueDelayMs?: number;
|
|
159
213
|
// Multi-account rotation all-capped idle: every connected Codex subscription is
|
|
160
214
|
// rate-limited/cooling. This is a MANDATORY hold — session.ts must wait
|
|
@@ -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
|
+
}
|