@opengeni/worker-bundle 0.16.21 → 0.17.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 (180) hide show
  1. package/dist/activities/agent-turn.d.ts +145 -32
  2. package/dist/activities/api-integrations.d.ts +21 -0
  3. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  4. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  5. package/dist/activities/codex-capacity.d.ts +5 -5
  6. package/dist/activities/codex-image-generation.d.ts +21 -0
  7. package/dist/activities/context-compaction.d.ts +3 -0
  8. package/dist/activities/documents.d.ts +3 -2
  9. package/dist/activities/environment.d.ts +11 -7
  10. package/dist/activities/expiring-material-renewal.d.ts +5 -3
  11. package/dist/activities/file-upload-reaper.d.ts +7 -2
  12. package/dist/activities/gateway-image-generation.d.ts +39 -0
  13. package/dist/activities/gateway-video-generation.d.ts +53 -0
  14. package/dist/activities/generated-images.d.ts +112 -0
  15. package/dist/activities/git-credential-renewal.d.ts +2 -2
  16. package/dist/activities/goals.d.ts +2 -2
  17. package/dist/activities/google-drive-changes.d.ts +99 -0
  18. package/dist/activities/image-generation-operation.d.ts +74 -0
  19. package/dist/activities/image-generation-references.d.ts +25 -0
  20. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  21. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  22. package/dist/activities/packs.d.ts +13 -26
  23. package/dist/activities/parent-wake.d.ts +1 -1
  24. package/dist/activities/retained-screenshot-reaper.d.ts +32 -0
  25. package/dist/activities/retained-screenshots.d.ts +129 -0
  26. package/dist/activities/rig-verification.d.ts +82 -5
  27. package/dist/activities/run-credential-renewal.d.ts +2 -2
  28. package/dist/activities/run-input.d.ts +5 -1
  29. package/dist/activities/sandbox-images.d.ts +14 -0
  30. package/dist/activities/sandbox-lease.d.ts +47 -9
  31. package/dist/activities/scheduled-tasks.d.ts +2 -2
  32. package/dist/activities/session-state.d.ts +4 -4
  33. package/dist/activities/streaming.d.ts +1 -1
  34. package/dist/activities/types.d.ts +71 -5
  35. package/dist/activities/video-generation-admission.d.ts +38 -0
  36. package/dist/activities/video-generation-credential.d.ts +7 -0
  37. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  38. package/dist/activities/video-media-validation.d.ts +34 -0
  39. package/dist/activities/video-output-retention.d.ts +36 -0
  40. package/dist/activities/video-reference-staging.d.ts +49 -0
  41. package/dist/activities/workflow-wake.d.ts +2 -2
  42. package/dist/activities/workspace-capture.d.ts +11 -1
  43. package/dist/activities-control-44KBWLSM.js +6885 -0
  44. package/dist/activities-control-44KBWLSM.js.map +1 -0
  45. package/dist/activities-control.d.ts +289 -0
  46. package/dist/activities-turn-K4CEDEGJ.js +14675 -0
  47. package/dist/activities-turn-K4CEDEGJ.js.map +1 -0
  48. package/dist/activities-turn.d.ts +17 -0
  49. package/dist/activities.d.ts +130 -147
  50. package/dist/activity-services.d.ts +10 -0
  51. package/dist/artifact-materializer-entry.d.ts +6 -0
  52. package/dist/artifact-materializer-entry.js +2662 -0
  53. package/dist/artifact-materializer-entry.js.map +1 -0
  54. package/dist/artifact-outbox-entry.d.ts +2 -0
  55. package/dist/artifact-outbox-entry.js +681 -0
  56. package/dist/artifact-outbox-entry.js.map +1 -0
  57. package/dist/chunk-E3RI3I33.js +828 -0
  58. package/dist/chunk-E3RI3I33.js.map +1 -0
  59. package/dist/chunk-GRFQTTSQ.js +448 -0
  60. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  61. package/dist/chunk-KKCRGAGZ.js +3091 -0
  62. package/dist/chunk-KKCRGAGZ.js.map +1 -0
  63. package/dist/{chunk-BJUW5LCM.js → chunk-RAMNYSAW.js} +15 -12
  64. package/dist/chunk-RAMNYSAW.js.map +1 -0
  65. package/dist/concurrency.d.ts +108 -5
  66. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  67. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  68. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  69. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  70. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  71. package/dist/editable-artifact-materializer.d.ts +262 -0
  72. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  73. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  74. package/dist/host-export.d.ts +1 -1
  75. package/dist/host-export.js +1 -1
  76. package/dist/index.d.ts +176 -10
  77. package/dist/index.js +675 -16460
  78. package/dist/index.js.map +1 -1
  79. package/dist/memory-pressure-guard.d.ts +57 -0
  80. package/dist/model-checkpoint-memory-collector.d.ts +22 -0
  81. package/dist/observability-metrics.d.ts +33 -0
  82. package/dist/sandbox-reaper-contract.d.ts +40 -0
  83. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  84. package/dist/sandbox-resume.d.ts +40 -7
  85. package/dist/sandbox-routing.d.ts +13 -6
  86. package/dist/worker-service-lifecycle.d.ts +6 -2
  87. package/dist/workflow-bundle.js +1044 -363
  88. package/dist/workflows/activities.d.ts +7 -1
  89. package/dist/workflows/document-index.d.ts +1 -1
  90. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  91. package/dist/workflows/rig-verification.d.ts +1 -1
  92. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  93. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  94. package/dist/workflows/session.d.ts +2 -2
  95. package/dist/workflows/video-generation.d.ts +10 -0
  96. package/dist/workflows/workflow-wake.d.ts +1 -1
  97. package/dist/workflows.d.ts +9 -7
  98. package/package.json +34 -15
  99. package/src/activities/agent-turn.ts +1905 -559
  100. package/src/activities/api-integrations.ts +138 -0
  101. package/src/activities/codemode-dispatcher.ts +335 -0
  102. package/src/activities/codemode-token-renewal.ts +64 -0
  103. package/src/activities/codex-capacity.ts +16 -7
  104. package/src/activities/codex-image-generation.ts +56 -0
  105. package/src/activities/codex-rotation.ts +19 -5
  106. package/src/activities/context-compaction.ts +47 -6
  107. package/src/activities/documents.ts +14 -4
  108. package/src/activities/environment.ts +52 -60
  109. package/src/activities/expiring-material-renewal.ts +16 -7
  110. package/src/activities/file-upload-reaper.ts +34 -3
  111. package/src/activities/gateway-image-generation.ts +146 -0
  112. package/src/activities/gateway-video-generation.ts +298 -0
  113. package/src/activities/generated-images.ts +860 -0
  114. package/src/activities/git-credential-renewal.ts +3 -3
  115. package/src/activities/goals.ts +6 -2
  116. package/src/activities/google-drive-changes.ts +454 -0
  117. package/src/activities/image-generation-operation.ts +333 -0
  118. package/src/activities/image-generation-references.ts +118 -0
  119. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  120. package/src/activities/knowledge-source-sync.ts +2840 -0
  121. package/src/activities/mcp-credentials.ts +1 -0
  122. package/src/activities/packs.ts +55 -55
  123. package/src/activities/recording.ts +4 -5
  124. package/src/activities/retained-screenshot-reaper.ts +202 -0
  125. package/src/activities/retained-screenshots.ts +1317 -0
  126. package/src/activities/rig-verification.ts +567 -77
  127. package/src/activities/run-credential-renewal.ts +9 -2
  128. package/src/activities/run-input.ts +40 -10
  129. package/src/activities/sandbox-images.ts +167 -0
  130. package/src/activities/sandbox-lease.ts +1065 -483
  131. package/src/activities/scheduled-tasks.ts +96 -7
  132. package/src/activities/session-state.ts +4 -4
  133. package/src/activities/streaming.ts +42 -3
  134. package/src/activities/types.ts +87 -5
  135. package/src/activities/video-generation-admission.ts +270 -0
  136. package/src/activities/video-generation-credential.ts +34 -0
  137. package/src/activities/video-generation-reconciliation.ts +578 -0
  138. package/src/activities/video-media-validation.ts +326 -0
  139. package/src/activities/video-output-retention.ts +314 -0
  140. package/src/activities/video-reference-staging.ts +263 -0
  141. package/src/activities/workflow-wake.ts +2 -2
  142. package/src/activities/workspace-capture.ts +77 -13
  143. package/src/activities-control.ts +54 -0
  144. package/src/activities-turn.ts +40 -0
  145. package/src/activities.ts +50 -118
  146. package/src/activity-services.ts +62 -0
  147. package/src/artifact-materializer-entry.ts +35 -0
  148. package/src/artifact-outbox-entry.ts +29 -0
  149. package/src/concurrency.ts +484 -6
  150. package/src/editable-artifact-hint-broker.ts +235 -0
  151. package/src/editable-artifact-materializer-launcher.ts +515 -0
  152. package/src/editable-artifact-materializer-service.ts +787 -0
  153. package/src/editable-artifact-materializer-storage.ts +413 -0
  154. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  155. package/src/editable-artifact-materializer.ts +1289 -0
  156. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  157. package/src/editable-artifact-outbox-service.ts +438 -0
  158. package/src/host-export-pump.ts +28 -11
  159. package/src/http.ts +3 -6
  160. package/src/index.ts +277 -72
  161. package/src/memory-pressure-guard.ts +489 -0
  162. package/src/model-checkpoint-memory-collector.ts +116 -0
  163. package/src/observability-metrics.ts +202 -7
  164. package/src/sandbox-reaper-contract.ts +73 -0
  165. package/src/sandbox-reaper-timeout.ts +79 -0
  166. package/src/sandbox-resume.ts +331 -157
  167. package/src/sandbox-routing.ts +49 -39
  168. package/src/worker-service-lifecycle.ts +82 -12
  169. package/src/workflows/activities.ts +30 -0
  170. package/src/workflows/file-upload-reaper.ts +6 -1
  171. package/src/workflows/knowledge-source-sync.ts +83 -0
  172. package/src/workflows/sandbox-reaper.ts +183 -21
  173. package/src/workflows/scheduled-tasks.ts +42 -6
  174. package/src/workflows/video-generation.ts +36 -0
  175. package/src/workflows.ts +17 -1
  176. package/dist/activities/redaction.d.ts +0 -1
  177. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  178. package/dist/chunk-BJUW5LCM.js.map +0 -1
  179. package/src/activities/redaction.ts +0 -9
  180. package/src/activities/toolspace-token-renewal.ts +0 -57
@@ -0,0 +1,138 @@
1
+ import {
2
+ createGraphqlMcpServer,
3
+ createOpenApiMcpServer,
4
+ createPinnedIntegrationTransport,
5
+ type IntegrationCredentialResolver,
6
+ type IntegrationInvocationAuthority,
7
+ } from "@opengeni/capabilities";
8
+ import type { Settings } from "@opengeni/config";
9
+ import type { ToolAuthNeededPayload } from "@opengeni/contracts";
10
+ import type {
11
+ ApiIntegrationRuntime,
12
+ ResolveConnectionCredentialInput,
13
+ ResolveConnectionCredentialResult,
14
+ } from "@opengeni/db";
15
+ import type { LocalMcpServerRegistration } from "@opengeni/runtime";
16
+ import type { FetchLike } from "@opengeni/network";
17
+
18
+ export type BuildApiIntegrationServersInput = {
19
+ settings: Settings;
20
+ integrations: readonly ApiIntegrationRuntime[];
21
+ authority: Omit<IntegrationInvocationAuthority, "connectionRef">;
22
+ resolveCredential: (
23
+ input: ResolveConnectionCredentialInput,
24
+ ) => Promise<ResolveConnectionCredentialResult>;
25
+ onAuthNeeded?: (payload: ToolAuthNeededPayload) => Promise<void> | void;
26
+ fetchImpl?: FetchLike;
27
+ };
28
+
29
+ /**
30
+ * Compile active persisted API facets into ordinary in-process MCP servers for
31
+ * one exact attempt. Connection resolution remains request-time, destination-
32
+ * bound, personal-delegation-fenced, and refreshable through the same worker
33
+ * resolver used by remote MCP servers.
34
+ */
35
+ export function buildApiIntegrationServersForTurn(
36
+ input: BuildApiIntegrationServersInput,
37
+ ): LocalMcpServerRegistration[] {
38
+ const transport = createPinnedIntegrationTransport({
39
+ network: input.settings,
40
+ ...(input.fetchImpl ? { fetchImpl: input.fetchImpl } : {}),
41
+ });
42
+ return input.integrations.map((integration) => {
43
+ const authority: IntegrationInvocationAuthority = {
44
+ ...input.authority,
45
+ ...(integration.connectionRef?.connectionId
46
+ ? { connectionRef: integration.connectionRef.connectionId }
47
+ : {}),
48
+ };
49
+ const credentialResolver = integration.connectionRef
50
+ ? integrationCredentialResolver(input, integration)
51
+ : undefined;
52
+ const server =
53
+ integration.revision.protocol === "openapi"
54
+ ? createOpenApiMcpServer({
55
+ revision: integration.revision,
56
+ transport,
57
+ authority,
58
+ ...(credentialResolver ? { credentialResolver } : {}),
59
+ })
60
+ : createGraphqlMcpServer({
61
+ revision: integration.revision,
62
+ endpoint: integration.baseUrl,
63
+ transport,
64
+ authority,
65
+ ...(credentialResolver ? { credentialResolver } : {}),
66
+ });
67
+ return {
68
+ id: integration.serverId,
69
+ server,
70
+ ...(integration.connectionRef?.connectionId
71
+ ? { resolvedConnectionId: integration.connectionRef.connectionId }
72
+ : {}),
73
+ };
74
+ });
75
+ }
76
+
77
+ function integrationCredentialResolver(
78
+ input: BuildApiIntegrationServersInput,
79
+ integration: ApiIntegrationRuntime,
80
+ ): IntegrationCredentialResolver {
81
+ const connectionRef = integration.connectionRef;
82
+ if (!connectionRef) throw new Error("Integration credential resolver requires a connection");
83
+ return {
84
+ resolve: async (request) => {
85
+ const result = await input.resolveCredential({
86
+ workspaceId: input.authority.workspaceId,
87
+ serverId: integration.serverId,
88
+ toolName: request.operationKey,
89
+ connectionRef,
90
+ destinationUrl: request.destinationUrl,
91
+ credentialTarget: "http_api",
92
+ forceRefresh: request.forceRefresh === true,
93
+ });
94
+ if (result.status === "auth_needed") {
95
+ await publishAuthNeeded(input, integration.serverId, request.operationKey, result);
96
+ return null;
97
+ }
98
+ const destination = new URL(request.destinationUrl);
99
+ return {
100
+ audience: { origin: destination.origin, pathPrefix: "/" },
101
+ placements:
102
+ result.placements ??
103
+ Object.entries(result.headers).map(([name, value]) => ({
104
+ carrier: "header" as const,
105
+ name,
106
+ value,
107
+ })),
108
+ ...(result.expiresAt ? { expiresAt: result.expiresAt.toISOString() } : {}),
109
+ ...(connectionRef.scopes ? { scope: [...connectionRef.scopes] } : {}),
110
+ };
111
+ },
112
+ };
113
+ }
114
+
115
+ async function publishAuthNeeded(
116
+ input: BuildApiIntegrationServersInput,
117
+ serverId: string,
118
+ toolName: string,
119
+ result: Extract<ResolveConnectionCredentialResult, { status: "auth_needed" }>,
120
+ ): Promise<void> {
121
+ try {
122
+ await input.onAuthNeeded?.({
123
+ serverId,
124
+ toolName,
125
+ providerDomain: result.providerDomain,
126
+ ...(result.provider ? { provider: result.provider } : {}),
127
+ reason: result.reason,
128
+ ...(result.connectionId ? { connectionId: result.connectionId } : {}),
129
+ ...(result.scopes ? { scopes: result.scopes } : {}),
130
+ ...(result.resource ? { resource: result.resource } : {}),
131
+ ...(result.selectedResources ? { selectedResources: result.selectedResources } : {}),
132
+ ...(result.authorizationUrl ? { authorizationUrl: result.authorizationUrl } : {}),
133
+ });
134
+ } catch {
135
+ // Authentication notices are advisory UI/audit signals. The local tool
136
+ // still returns the fixed connection-required result when publication fails.
137
+ }
138
+ }
@@ -0,0 +1,335 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ AttemptToolApprovalRequiredError,
4
+ AttemptToolCatalogStaleError,
5
+ AttemptToolNotFoundError,
6
+ codemodeDispatchSubject,
7
+ decodeCodemodeDispatchRequest,
8
+ encodeCodemodeDispatchAck,
9
+ type AttemptToolEnvironment,
10
+ } from "@opengeni/codemode";
11
+ import {
12
+ CODEMODE_CLAIM_HEARTBEAT_MS,
13
+ CODEMODE_MAX_CONCURRENT_CALLS_PER_ATTEMPT,
14
+ type CodemodeOperation,
15
+ } from "@opengeni/contracts";
16
+ import {
17
+ cancelQueuedCodemodeOperationsForAttempt,
18
+ claimCodemodeOperation,
19
+ failCodemodeOperation,
20
+ markCodemodeOperationExecutionStarted,
21
+ renewCodemodeOperationClaim,
22
+ settleCodemodeOperationWithOutput,
23
+ type Database,
24
+ } from "@opengeni/db";
25
+ import {
26
+ appendAndPublishTurnEventsFenced,
27
+ publishDurableSessionEvents,
28
+ type EventBus,
29
+ } from "@opengeni/events";
30
+
31
+ export type CodemodeDispatcherScope = {
32
+ accountId: string;
33
+ workspaceId: string;
34
+ sessionId: string;
35
+ turnId: string;
36
+ attemptId: string;
37
+ executionGeneration: number;
38
+ };
39
+
40
+ /**
41
+ * Owns the only Codemode execution edge for one exact attempt. NATS carries a
42
+ * wake-up only; the durable row is claimed before any side effect and the call
43
+ * is executed by the same AttemptToolEnvironment already bound to model MCP.
44
+ */
45
+ export class CodemodeAttemptDispatcher {
46
+ private readonly stopController = new AbortController();
47
+ private readonly inFlight = new Map<string, Promise<void>>();
48
+ private pendingClaims = 0;
49
+ private unsubscribe: (() => void) | null = null;
50
+ private closing = false;
51
+
52
+ constructor(
53
+ private readonly db: Database,
54
+ private readonly bus: EventBus,
55
+ private readonly environment: AttemptToolEnvironment,
56
+ private readonly scope: CodemodeDispatcherScope,
57
+ private readonly turnSignal?: AbortSignal,
58
+ private readonly maxConcurrentCalls = CODEMODE_MAX_CONCURRENT_CALLS_PER_ATTEMPT,
59
+ ) {
60
+ if (
61
+ environment.catalog.accountId !== scope.accountId ||
62
+ environment.catalog.workspaceId !== scope.workspaceId ||
63
+ environment.catalog.sessionId !== scope.sessionId ||
64
+ environment.catalog.turnId !== scope.turnId ||
65
+ environment.catalog.attemptId !== scope.attemptId ||
66
+ environment.catalog.executionGeneration !== scope.executionGeneration
67
+ ) {
68
+ throw new Error("Codemode dispatcher scope does not match its tool environment");
69
+ }
70
+ if (!Number.isSafeInteger(maxConcurrentCalls) || maxConcurrentCalls < 1) {
71
+ throw new Error("Codemode dispatcher concurrency must be a positive safe integer");
72
+ }
73
+ }
74
+
75
+ start(): void {
76
+ if (this.unsubscribe || this.closing)
77
+ throw new Error("Codemode dispatcher cannot be restarted");
78
+ this.unsubscribe = this.bus.subscribeRequests(
79
+ codemodeDispatchSubject(this.scope.workspaceId, this.scope.attemptId),
80
+ async (payload) => {
81
+ const request = decodeCodemodeDispatchRequest(payload);
82
+ if (request.catalogDigest !== this.environment.catalog.digest || this.closing) {
83
+ return encodeCodemodeDispatchAck({
84
+ version: 1,
85
+ operationId: request.operationId,
86
+ status: "rejected",
87
+ });
88
+ }
89
+ if (
90
+ !this.inFlight.has(request.operationId) &&
91
+ this.inFlight.size + this.pendingClaims >= this.maxConcurrentCalls
92
+ ) {
93
+ return encodeCodemodeDispatchAck({
94
+ version: 1,
95
+ operationId: request.operationId,
96
+ status: "unavailable",
97
+ });
98
+ }
99
+ this.pendingClaims += 1;
100
+ try {
101
+ const claim = await claimCodemodeOperation(this.db, {
102
+ ...this.scope,
103
+ catalogDigest: request.catalogDigest,
104
+ operationId: request.operationId,
105
+ claimId: randomUUID(),
106
+ });
107
+ if (claim.status === "claimed") {
108
+ const execution = this.execute(claim.operation, claim.claimId).finally(() => {
109
+ this.inFlight.delete(request.operationId);
110
+ });
111
+ this.inFlight.set(request.operationId, execution);
112
+ return encodeCodemodeDispatchAck({
113
+ version: 1,
114
+ operationId: request.operationId,
115
+ status: "accepted",
116
+ });
117
+ }
118
+ return encodeCodemodeDispatchAck({
119
+ version: 1,
120
+ operationId: request.operationId,
121
+ status: claim.status,
122
+ });
123
+ } finally {
124
+ this.pendingClaims -= 1;
125
+ }
126
+ },
127
+ );
128
+ }
129
+
130
+ async close(reason = "Execution attempt ended"): Promise<void> {
131
+ if (this.closing) {
132
+ await Promise.allSettled([...this.inFlight.values()]);
133
+ return;
134
+ }
135
+ this.closing = true;
136
+ this.unsubscribe?.();
137
+ this.unsubscribe = null;
138
+ this.stopController.abort(reason);
139
+ await cancelQueuedCodemodeOperationsForAttempt(this.db, {
140
+ accountId: this.scope.accountId,
141
+ workspaceId: this.scope.workspaceId,
142
+ attemptId: this.scope.attemptId,
143
+ reason,
144
+ });
145
+ await Promise.allSettled([...this.inFlight.values()]);
146
+ }
147
+
148
+ private async execute(operation: CodemodeOperation, claimId: string): Promise<void> {
149
+ const entry = this.environment.catalog.entries.find(
150
+ (candidate) =>
151
+ candidate.identity.serverId === operation.identity.serverId &&
152
+ candidate.identity.toolName === operation.identity.toolName,
153
+ );
154
+ if (!entry) {
155
+ await this.failBeforeExecution(
156
+ operation.operationId,
157
+ claimId,
158
+ "tool_not_found",
159
+ "Tool left the active catalog",
160
+ );
161
+ return;
162
+ }
163
+ const signal = combinedSignal(this.stopController.signal, this.turnSignal);
164
+ if (signal.aborted) {
165
+ await this.failBeforeExecution(
166
+ operation.operationId,
167
+ claimId,
168
+ "attempt_cancelled",
169
+ "Execution attempt ended before the Codemode call started",
170
+ );
171
+ return;
172
+ }
173
+ const created = await appendAndPublishTurnEventsFenced(
174
+ this.db,
175
+ this.bus,
176
+ this.scope.workspaceId,
177
+ this.scope.sessionId,
178
+ this.scope.turnId,
179
+ this.scope.executionGeneration,
180
+ this.scope.attemptId,
181
+ [
182
+ {
183
+ type: "agent.toolCall.created",
184
+ turnId: this.scope.turnId,
185
+ turnGeneration: this.scope.executionGeneration,
186
+ turnAttemptId: this.scope.attemptId,
187
+ producerId: operation.caller.subjectId,
188
+ payload: {
189
+ id: operation.operationId,
190
+ name: entry.modelName,
191
+ arguments: operation.arguments,
192
+ origin: "codemode",
193
+ subjectId: operation.caller.subjectId,
194
+ raw: {
195
+ type: "codemode_call",
196
+ serverId: operation.identity.serverId,
197
+ toolName: operation.identity.toolName,
198
+ catalogDigest: operation.catalogDigest,
199
+ },
200
+ },
201
+ },
202
+ ],
203
+ ).catch(() => ({ accepted: false }));
204
+ if (!created.accepted) {
205
+ await this.failBeforeExecution(
206
+ operation.operationId,
207
+ claimId,
208
+ "attempt_fence_closed",
209
+ "Execution attempt ended before the Codemode call started",
210
+ );
211
+ return;
212
+ }
213
+ if (signal.aborted) {
214
+ await this.settleWithOutput(operation, claimId, {
215
+ state: "failed",
216
+ errorCode: "attempt_cancelled",
217
+ errorMessage: "Execution attempt ended before the Codemode call started",
218
+ });
219
+ return;
220
+ }
221
+
222
+ const claimAuthority = {
223
+ accountId: this.scope.accountId,
224
+ workspaceId: this.scope.workspaceId,
225
+ attemptId: this.scope.attemptId,
226
+ operationId: operation.operationId,
227
+ claimId,
228
+ };
229
+ const crossedExecutionBoundary = await markCodemodeOperationExecutionStarted(
230
+ this.db,
231
+ claimAuthority,
232
+ );
233
+ if (!crossedExecutionBoundary) return;
234
+ const heartbeat = setInterval(() => {
235
+ void renewCodemodeOperationClaim(this.db, claimAuthority).catch(() => undefined);
236
+ }, CODEMODE_CLAIM_HEARTBEAT_MS);
237
+ heartbeat.unref?.();
238
+ try {
239
+ const result = await this.environment.call(
240
+ {
241
+ operationId: operation.operationId,
242
+ catalogDigest: operation.catalogDigest,
243
+ identity: operation.identity,
244
+ arguments: operation.arguments,
245
+ caller: operation.caller,
246
+ },
247
+ { signal },
248
+ );
249
+ await this.settleWithOutput(operation, claimId, { state: "completed", result });
250
+ } catch (error) {
251
+ const knownPreExecution =
252
+ error instanceof AttemptToolApprovalRequiredError ||
253
+ error instanceof AttemptToolCatalogStaleError ||
254
+ error instanceof AttemptToolNotFoundError;
255
+ await this.settleWithOutput(operation, claimId, {
256
+ state: knownPreExecution ? "failed" : "outcome_unknown",
257
+ errorCode: errorCode(error, signal),
258
+ errorMessage: !knownPreExecution
259
+ ? "Tool execution ended without a durable result. Its side-effect outcome is unknown; inspect actual state before retrying."
260
+ : safeErrorMessage(error),
261
+ });
262
+ } finally {
263
+ clearInterval(heartbeat);
264
+ }
265
+ }
266
+
267
+ private async settleWithOutput(
268
+ operation: CodemodeOperation,
269
+ claimId: string,
270
+ settlement:
271
+ | { state: "completed"; result: NonNullable<CodemodeOperation["result"]> }
272
+ | {
273
+ state: "failed" | "outcome_unknown";
274
+ errorCode: string;
275
+ errorMessage: string;
276
+ },
277
+ ): Promise<void> {
278
+ const result = await settleCodemodeOperationWithOutput(this.db, {
279
+ ...this.scope,
280
+ operationId: operation.operationId,
281
+ claimId,
282
+ producerId: operation.caller.subjectId,
283
+ settlement,
284
+ });
285
+ if (!result.committed || result.events.length === 0) return;
286
+ await publishDurableSessionEvents(
287
+ this.bus,
288
+ this.scope.workspaceId,
289
+ this.scope.sessionId,
290
+ result.events,
291
+ );
292
+ }
293
+
294
+ private async failBeforeExecution(
295
+ operationId: string,
296
+ claimId: string,
297
+ failureCode: string,
298
+ failureMessage: string,
299
+ ): Promise<void> {
300
+ await failCodemodeOperation(this.db, {
301
+ accountId: this.scope.accountId,
302
+ workspaceId: this.scope.workspaceId,
303
+ attemptId: this.scope.attemptId,
304
+ operationId,
305
+ claimId,
306
+ state: "failed",
307
+ errorCode: failureCode,
308
+ errorMessage: failureMessage,
309
+ });
310
+ }
311
+ }
312
+
313
+ function combinedSignal(primary: AbortSignal, secondary?: AbortSignal): AbortSignal {
314
+ return secondary ? AbortSignal.any([primary, secondary]) : primary;
315
+ }
316
+
317
+ function errorCode(error: unknown, signal: AbortSignal): string {
318
+ if (signal.aborted) return "attempt_cancelled_during_execution";
319
+ if (typeof error === "object" && error !== null && "code" in error) {
320
+ const code = (error as { code?: unknown }).code;
321
+ if (typeof code === "string" && /^[a-z0-9_]{1,128}$/u.test(code)) return code;
322
+ }
323
+ return "tool_outcome_unknown";
324
+ }
325
+
326
+ function safeErrorMessage(error: unknown): string {
327
+ if (
328
+ error instanceof AttemptToolApprovalRequiredError ||
329
+ error instanceof AttemptToolCatalogStaleError ||
330
+ error instanceof AttemptToolNotFoundError
331
+ ) {
332
+ return error.message;
333
+ }
334
+ return "Codemode call failed before tool execution";
335
+ }
@@ -0,0 +1,64 @@
1
+ import {
2
+ startExpiringMaterialRenewalLoop,
3
+ type ExpiringMaterialRenewalController,
4
+ } from "./expiring-material-renewal";
5
+ import type { MintedSandboxCodemodeToken } from "./environment";
6
+
7
+ export const CODEMODE_TOKEN_DEFAULT_REFRESH_MS = 30 * 60_000;
8
+ export const CODEMODE_TOKEN_EXPIRY_LEAD_MS = 5 * 60_000;
9
+ export const CODEMODE_TOKEN_MIN_REFRESH_MS = 5_000;
10
+ export const CODEMODE_TOKEN_MAX_RETRY_MS = 5 * 60_000;
11
+
12
+ export type CodemodeTokenRenewalController = ExpiringMaterialRenewalController;
13
+
14
+ export type CodemodeTokenRenewalOptions = {
15
+ initialExpiresAt: Date;
16
+ mint: () => Promise<MintedSandboxCodemodeToken | undefined>;
17
+ write: (material: MintedSandboxCodemodeToken) => Promise<void>;
18
+ now?: () => number;
19
+ schedule?: (callback: () => void, delayMs: number) => unknown;
20
+ clearSchedule?: (timer: unknown) => void;
21
+ onSuccess?: (result: { nextDelayMs: number }) => void;
22
+ onFailure?: (failure: {
23
+ retryDelayMs: number;
24
+ errorClass: "CodemodeTokenRenewalOperationError";
25
+ }) => void;
26
+ };
27
+
28
+ class CodemodeTokenRenewalError extends Error {
29
+ override readonly name = "CodemodeTokenRenewalError";
30
+ }
31
+
32
+ export function startCodemodeTokenRenewalLoop(
33
+ options: CodemodeTokenRenewalOptions,
34
+ ): CodemodeTokenRenewalController {
35
+ return startExpiringMaterialRenewalLoop<
36
+ MintedSandboxCodemodeToken,
37
+ "CodemodeTokenRenewalOperationError"
38
+ >({
39
+ initialExpiresAt: options.initialExpiresAt,
40
+ resolve: async () => {
41
+ const minted = await options.mint();
42
+ if (!minted) {
43
+ throw new CodemodeTokenRenewalError("Codemode token mint is no longer available");
44
+ }
45
+ return minted;
46
+ },
47
+ write: options.write,
48
+ expiresAt: (material) => material.expiresAt,
49
+ publicErrorClass: "CodemodeTokenRenewalOperationError",
50
+ policy: {
51
+ defaultRefreshMs: CODEMODE_TOKEN_DEFAULT_REFRESH_MS,
52
+ expiryLeadMs: CODEMODE_TOKEN_EXPIRY_LEAD_MS,
53
+ minRefreshMs: CODEMODE_TOKEN_MIN_REFRESH_MS,
54
+ maxRetryMs: CODEMODE_TOKEN_MAX_RETRY_MS,
55
+ },
56
+ ...(options.now ? { now: options.now } : {}),
57
+ ...(options.schedule ? { schedule: options.schedule } : {}),
58
+ ...(options.clearSchedule ? { clearSchedule: options.clearSchedule } : {}),
59
+ ...(options.onSuccess
60
+ ? { onSuccess: ({ nextDelayMs }) => options.onSuccess?.({ nextDelayMs }) }
61
+ : {}),
62
+ ...(options.onFailure ? { onFailure: options.onFailure } : {}),
63
+ });
64
+ }
@@ -12,10 +12,11 @@ import {
12
12
  CODEX_USAGE_EXHAUSTED_PCT,
13
13
  authoritativeCodexCapacityResetAt,
14
14
  isCodexCredentialEligible,
15
+ isCodexCredentialHealthy,
15
16
  selectCodexCredentialLeaseForTurn,
16
17
  } from "./codex-rotation";
17
18
  import type {
18
- ActivityServices,
19
+ ControlActivityServices,
19
20
  GetCodexCapacityWaitInput,
20
21
  ReconcileCodexCapacityWaitInput,
21
22
  ReconcileCodexCapacityWaitResult,
@@ -23,10 +24,10 @@ import type {
23
24
 
24
25
  type CodexCapacitySignalServices = {
25
26
  signalCodexCapacityWorkflow?:
26
- | NonNullable<ActivityServices["signalCodexCapacityWorkflow"]>
27
+ | NonNullable<ControlActivityServices["signalCodexCapacityWorkflow"]>
27
28
  | null
28
29
  | undefined;
29
- wakeSessionWorkflow: ActivityServices["wakeSessionWorkflow"];
30
+ wakeSessionWorkflow: ControlActivityServices["wakeSessionWorkflow"];
30
31
  };
31
32
 
32
33
  /**
@@ -73,7 +74,7 @@ export async function signalCodexCapacityWakeTargets(
73
74
 
74
75
  /** Repair a commit/signal crash edge by redelivering every pending revision. */
75
76
  export async function signalPendingCodexCapacityWakeTargets(
76
- services: CodexCapacitySignalServices & { db: ActivityServices["db"] },
77
+ services: CodexCapacitySignalServices & { db: ControlActivityServices["db"] },
77
78
  workspaceId: string,
78
79
  ): Promise<void> {
79
80
  const targets = await listPendingCodexCapacityWakeTargets(services.db, workspaceId).catch(
@@ -96,7 +97,15 @@ export function codexCapacityDecision(
96
97
  sessionLastCredentialId: context.sessionLastCredentialId,
97
98
  now,
98
99
  });
99
- if (selected.credentialId) {
100
+ const selectedAccount = selected.credentialId
101
+ ? context.accounts.find((account) => account.id === selected.credentialId)
102
+ : undefined;
103
+ const selectedIsAvailable =
104
+ selectedAccount !== undefined &&
105
+ (selectedAccount.id === context.existingCredentialId
106
+ ? isCodexCredentialHealthy(selectedAccount, now)
107
+ : isCodexCredentialEligible(selectedAccount, now));
108
+ if (selected.credentialId && selectedIsAvailable) {
100
109
  return {
101
110
  kind: "available",
102
111
  credentialId: selected.credentialId,
@@ -121,7 +130,7 @@ export function codexCapacityDecision(
121
130
  }
122
131
 
123
132
  async function refreshCapacityMetadata(
124
- services: ActivityServices,
133
+ services: ControlActivityServices,
125
134
  workspaceId: string,
126
135
  ): Promise<void> {
127
136
  const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
@@ -142,7 +151,7 @@ async function refreshCapacityMetadata(
142
151
  );
143
152
  }
144
153
 
145
- export function createCodexCapacityActivities(services: () => Promise<ActivityServices>) {
154
+ export function createCodexCapacityActivities(services: () => Promise<ControlActivityServices>) {
146
155
  async function getCodexCapacityWait(input: GetCodexCapacityWaitInput) {
147
156
  const { db } = await services();
148
157
  const waiter = await getCodexCapacityWaitForSession(db, input.workspaceId, input.sessionId);
@@ -0,0 +1,56 @@
1
+ import {
2
+ CODEX_PROVIDER_ID,
3
+ generateCodexSubscriptionImage,
4
+ type CodexRequestContext,
5
+ } from "@opengeni/codex";
6
+ import type { Database } from "@opengeni/db";
7
+ import type { ObjectStorage } from "@opengeni/storage";
8
+ import type { GeneratedImageReceipt } from "./generated-images";
9
+ import {
10
+ executeImageGenerationOperation,
11
+ imageProviderBindingHash,
12
+ } from "./image-generation-operation";
13
+ import type { ResolvedImageGenerationReference } from "./image-generation-references";
14
+
15
+ const CODEX_IMAGE_MODEL = "gpt-image-2";
16
+
17
+ /** Execute the same standalone subscription image path used by Codex clients. */
18
+ export async function executeCodexImageGeneration(input: {
19
+ db: Database;
20
+ objectStorage: ObjectStorage | null;
21
+ accountId: string;
22
+ workspaceId: string;
23
+ sessionId: string;
24
+ turnId: string;
25
+ attemptId: string;
26
+ toolCallId: string;
27
+ prompt: string;
28
+ references?: readonly ResolvedImageGenerationReference[];
29
+ credentialId: string;
30
+ codexContext: Pick<CodexRequestContext, "clientVersion" | "getToken" | "refresh">;
31
+ abortSignal?: AbortSignal;
32
+ }): Promise<GeneratedImageReceipt> {
33
+ const providerBindingHash = imageProviderBindingHash(CODEX_PROVIDER_ID, input.credentialId);
34
+ return await executeImageGenerationOperation({
35
+ ...input,
36
+ providerId: CODEX_PROVIDER_ID,
37
+ providerBindingHash,
38
+ modelId: CODEX_IMAGE_MODEL,
39
+ ...(input.references ? { referenceDigests: input.references } : {}),
40
+ generate: async () => {
41
+ const generated = await generateCodexSubscriptionImage({
42
+ prompt: input.prompt,
43
+ ...(input.references ? { references: input.references } : {}),
44
+ turnId: input.turnId,
45
+ context: input.codexContext,
46
+ ...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
47
+ });
48
+ return {
49
+ toolCallId: input.toolCallId,
50
+ providerItemId: null,
51
+ bytes: generated.bytes,
52
+ declaredMediaType: generated.declaredMediaType,
53
+ };
54
+ },
55
+ });
56
+ }
@@ -694,13 +694,27 @@ export function selectCodexCredentialLeaseForTurn(args: {
694
694
 
695
695
  // A stale policy pin is intentionally ignored here and cleared by the caller
696
696
  // after the atomic selection. Rotation-off/unpinned behavior otherwise keeps
697
- // the pre-lease pin > active-pointer contract.
697
+ // the pre-lease pin > active-pointer contract. The pointer is preference, not
698
+ // proof of capacity: rotation-off must wait on a capped pointer rather than
699
+ // falsely admitting it or silently failing over to another subscription.
698
700
  if (!rotationEnabled) {
699
- const credentialId =
700
- activeCredentialId && connectedIds.has(activeCredentialId) ? activeCredentialId : null;
701
+ const active = activeCredentialId
702
+ ? accounts.find((account) => account.id === activeCredentialId)
703
+ : undefined;
704
+ if (!active?.allocatorEnabled) {
705
+ return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
706
+ }
707
+ if (!isCodexCredentialHealthy(active, args.now)) {
708
+ return {
709
+ credentialId: null,
710
+ decision: { kind: "allCapped", earliestResetAt: availableAt(active, args.now) },
711
+ advanceActivePointer: false,
712
+ };
713
+ }
701
714
  return {
702
- credentialId,
703
- decision: credentialId ? { kind: "active", credentialId, moved: false } : { kind: "none" },
715
+ credentialId: active.id,
716
+ decision: { kind: "active", credentialId: active.id, moved: false },
717
+ advanceActivePointer: false,
704
718
  };
705
719
  }
706
720