@opengeni/worker-bundle 0.24.2-canary.0 → 0.26.0-canary.0

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 (73) hide show
  1. package/dist/activities/agent-turn/agent-build.d.ts +3 -10
  2. package/dist/activities/agent-turn/claim.d.ts +7 -0
  3. package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
  4. package/dist/activities/agent-turn/errors.d.ts +1 -1
  5. package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
  6. package/dist/activities/agent-turn/quiescence.d.ts +9 -6
  7. package/dist/activities/agent-turn/tool-environment.d.ts +2 -13
  8. package/dist/activities/agent-turn/tool-policy.d.ts +4 -0
  9. package/dist/activities/agent-turn/turn-context.d.ts +9 -1
  10. package/dist/activities/codemode-dispatcher.d.ts +10 -1
  11. package/dist/activities/codex-capacity.d.ts +32 -3
  12. package/dist/activities/codex-fleet-shadow.d.ts +1 -1
  13. package/dist/activities/codex-image-generation.d.ts +1 -1
  14. package/dist/activities/codex-rotation.d.ts +8 -67
  15. package/dist/activities/goals.d.ts +1 -1
  16. package/dist/activities/google-drive-publication.d.ts +2 -13
  17. package/dist/activities/image-generation-operation.d.ts +13 -4
  18. package/dist/activities/session-state.d.ts +4 -2
  19. package/dist/activities/types.d.ts +11 -1
  20. package/dist/{activities-control-ZWH7J4KZ.js → activities-control-CTEMLTHG.js} +45 -18
  21. package/dist/activities-control-CTEMLTHG.js.map +1 -0
  22. package/dist/activities-control.d.ts +2 -0
  23. package/dist/{activities-turn-I4OPLQLS.js → activities-turn-S5IU347A.js} +960 -815
  24. package/dist/activities-turn-S5IU347A.js.map +1 -0
  25. package/dist/activities.d.ts +2 -0
  26. package/dist/{chunk-65JEWP2Q.js → chunk-D4ZEFOYH.js} +1 -1
  27. package/dist/chunk-D4ZEFOYH.js.map +1 -0
  28. package/dist/{chunk-RQNH6CGR.js → chunk-TAUE3GYT.js} +117 -95
  29. package/dist/chunk-TAUE3GYT.js.map +1 -0
  30. package/dist/github-rest-mcp.d.ts +0 -2
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.js +2 -2
  33. package/dist/index.js.map +1 -1
  34. package/dist/observability-metrics.d.ts +1 -1
  35. package/dist/workflow-bundle.js +71 -8
  36. package/dist/workflows/activities.d.ts +1 -1
  37. package/dist/workflows/session.d.ts +3 -0
  38. package/package.json +17 -17
  39. package/src/activities/agent-turn/agent-build.ts +20 -26
  40. package/src/activities/agent-turn/claim.ts +26 -1
  41. package/src/activities/agent-turn/codex-capacity.ts +311 -234
  42. package/src/activities/agent-turn/credential-leases.ts +36 -0
  43. package/src/activities/agent-turn/errors.ts +29 -1
  44. package/src/activities/agent-turn/failure-settlement.ts +451 -323
  45. package/src/activities/agent-turn/quiescence.ts +21 -15
  46. package/src/activities/agent-turn/run.ts +18 -5
  47. package/src/activities/agent-turn/stream-attempt.ts +15 -1
  48. package/src/activities/agent-turn/tool-environment.ts +30 -32
  49. package/src/activities/agent-turn/tool-policy.ts +14 -8
  50. package/src/activities/agent-turn/turn-context.ts +13 -0
  51. package/src/activities/codemode-dispatcher.ts +235 -59
  52. package/src/activities/codex-capacity.ts +89 -10
  53. package/src/activities/codex-fleet-shadow.ts +8 -1
  54. package/src/activities/codex-image-generation.ts +19 -2
  55. package/src/activities/codex-rotation.ts +45 -142
  56. package/src/activities/goals.ts +9 -9
  57. package/src/activities/google-drive-publication.ts +4 -81
  58. package/src/activities/image-generation-operation.ts +28 -3
  59. package/src/activities/sandbox-lease.ts +29 -12
  60. package/src/activities/session-state.ts +17 -0
  61. package/src/activities/types.ts +16 -8
  62. package/src/activities.ts +1 -0
  63. package/src/github-rest-mcp.ts +0 -82
  64. package/src/observability-metrics.ts +4 -0
  65. package/src/sandbox-routing.ts +71 -16
  66. package/src/workflows/activities.ts +1 -0
  67. package/src/workflows/session.ts +85 -7
  68. package/dist/activities/api-integrations.d.ts +0 -21
  69. package/dist/activities-control-ZWH7J4KZ.js.map +0 -1
  70. package/dist/activities-turn-I4OPLQLS.js.map +0 -1
  71. package/dist/chunk-65JEWP2Q.js.map +0 -1
  72. package/dist/chunk-RQNH6CGR.js.map +0 -1
  73. package/src/activities/api-integrations.ts +0 -151
@@ -89,6 +89,7 @@ export function assertSessionAttemptQuiescenceRecoveryDurable(input: {
89
89
  export const QUIESCENCE_PROOF_SIGNAL_INITIAL_RETRY_MS = 250;
90
90
  export const QUIESCENCE_PROOF_SIGNAL_MAX_RETRY_MS = 5_000;
91
91
  export const TURN_QUIESCENCE_WATCHDOG_MS = 5 * 60_000;
92
+ export const TURN_FINALIZER_STEP_TIMEOUT_MS = 30_000;
92
93
 
93
94
  /** A fenced activity must never heartbeat forever while its physical writer
94
95
  * drain is stuck. The last-resort worker exit stops every in-process writer;
@@ -241,35 +242,40 @@ export async function releaseTurnSandboxAfterWriterDrain(
241
242
  }
242
243
 
243
244
  /**
244
- * Await a finalizer operation only while this Temporal activity still owns its
245
- * execution window. Once Pause/Steer cancellation arrives, the operation keeps
246
- * its own rejection handler and may finish its idempotent, attempt-scoped
247
- * cleanup in the background, but it cannot pin activity terminalization or
248
- * delay the separately receipt-gated replacement dispatch.
245
+ * Await bounded, idempotent finalizer housekeeping only while this Temporal
246
+ * activity still owns its execution window. A completed turn must never keep
247
+ * heartbeating forever because a provider/tool close promise did not settle:
248
+ * that strands every later durable prompt behind an already-terminal attempt.
249
+ * Cancellation or the hard timeout detaches the operation with a rejection
250
+ * handler; the operation may still finish its attempt-scoped cleanup, but it
251
+ * cannot pin activity terminalization.
249
252
  */
250
253
  export async function waitForTurnFinalizerStep<T>(
251
254
  operation: Promise<T>,
252
255
  signal: AbortSignal | undefined,
256
+ timeoutMs = TURN_FINALIZER_STEP_TIMEOUT_MS,
253
257
  ): Promise<T | undefined> {
254
- if (!signal) return await operation;
255
- if (signal.aborted) {
258
+ if (signal?.aborted) {
256
259
  void operation.catch(() => undefined);
257
260
  return undefined;
258
261
  }
259
262
 
260
- let resolveCancellation: (() => void) | undefined;
261
- const cancelled = new Promise<undefined>((resolve) => {
262
- resolveCancellation = () => resolve(undefined);
263
+ let resolveDetached: (() => void) | undefined;
264
+ const detached = new Promise<undefined>((resolve) => {
265
+ resolveDetached = () => resolve(undefined);
263
266
  });
264
- const cancel = (): void => {
267
+ const detach = (): void => {
265
268
  void operation.catch(() => undefined);
266
- resolveCancellation?.();
269
+ resolveDetached?.();
267
270
  };
268
- signal.addEventListener("abort", cancel, { once: true });
271
+ signal?.addEventListener("abort", detach, { once: true });
272
+ const timeout = setTimeout(detach, Math.max(1, timeoutMs));
273
+ timeout.unref?.();
269
274
  try {
270
- return await Promise.race([operation, cancelled]);
275
+ return await Promise.race([operation, detached]);
271
276
  } finally {
272
- signal.removeEventListener("abort", cancel);
277
+ clearTimeout(timeout);
278
+ signal?.removeEventListener("abort", detach);
273
279
  }
274
280
  }
275
281
 
@@ -98,6 +98,9 @@ export function sessionTitleCodexRequestContext(
98
98
  refresh: context.refresh,
99
99
  resolveModel: context.resolveModel,
100
100
  ...(context.onUsageHeaders ? { onUsageHeaders: context.onUsageHeaders } : {}),
101
+ ...(context.beforeProviderDispatch
102
+ ? { beforeProviderDispatch: context.beforeProviderDispatch }
103
+ : {}),
101
104
  ...(context.responseTimeoutPolicy
102
105
  ? { responseTimeoutPolicy: context.responseTimeoutPolicy }
103
106
  : {}),
@@ -514,6 +517,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
514
517
  input.workspaceId,
515
518
  providerTurn.effectiveCodexCredentialId ?? "",
516
519
  );
520
+ const resolveTrackedToken = async (
521
+ resolve: () => ReturnType<typeof resolver.getToken>,
522
+ ) => {
523
+ const token = await resolve();
524
+ providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
525
+ return token;
526
+ };
517
527
  return {
518
528
  clientVersion: CODEX_CLIENT_VERSION,
519
529
  // Backend sticky cache-routing key — the SAME id as the body's
@@ -523,8 +533,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
523
533
  // (per-request shard lottery = prod's measured 48.6% on sol);
524
534
  // with it, resends pin to the warm shard (Codex CLI parity).
525
535
  sessionId: input.sessionId,
526
- getToken: () => resolver.getToken(),
527
- refresh: () => resolver.refresh(),
536
+ getToken: () => resolveTrackedToken(resolver.getToken),
537
+ refresh: () => resolveTrackedToken(resolver.refresh),
528
538
  resolveModel: buildModelResolver(
529
539
  CODEX_FALLBACK_MODEL_SLUGS,
530
540
  CODEX_FALLBACK_MODEL_SLUGS[0],
@@ -532,6 +542,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
532
542
  onUsageHeaders: (snapshot) => {
533
543
  providerTurn.latestCodexUsage = snapshot;
534
544
  }, // latest wins; flushed once in finally
545
+ beforeProviderDispatch: () => {
546
+ leases.codex.assertUsable();
547
+ },
535
548
  onRequestPreparationDiagnostic: (phase) => {
536
549
  if (
537
550
  eventing.firstModelRequestCheckpointAt === null ||
@@ -1171,7 +1184,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1171
1184
  });
1172
1185
  const {
1173
1186
  attemptConnectorActionBindings,
1174
- connectorActionIdentity,
1187
+ connectorActionPolicy,
1175
1188
  generateSessionTitleInParallel,
1176
1189
  postToolPreparationStartedAt,
1177
1190
  preparationIndependentToolNames,
@@ -1226,7 +1239,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1226
1239
  sandboxCodemodeToken,
1227
1240
  fileResourceDownloads,
1228
1241
  attemptConnectorActionBindings,
1229
- connectorActionIdentity,
1242
+ connectorActionPolicy,
1243
+ trigger,
1230
1244
  preparationIndependentToolNames,
1231
1245
  videoGenerationAcceptancesByCallId,
1232
1246
  activeSandboxBackend,
@@ -1565,7 +1579,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1565
1579
  bus,
1566
1580
  observability,
1567
1581
  wakeSessionWorkflow,
1568
- signalCodexCapacityWorkflow,
1569
1582
  cancellationSignal,
1570
1583
  sandboxRotationController,
1571
1584
  noteCancellationRequested,
@@ -10,6 +10,7 @@ import {
10
10
  clearDurablePendingSessionToolCalls,
11
11
  isSessionCompactionRequested,
12
12
  nextSessionHistoryPosition,
13
+ persistModelContextSnapshot,
13
14
  updateSessionTitleWithEvent,
14
15
  } from "@opengeni/db";
15
16
  import { publishDurableSessionEvents } from "@opengeni/events";
@@ -777,6 +778,17 @@ export async function runTurnStreamAttempt(
777
778
  return await runtime.runStream(agent, runInput!, eventing.modelRunSettings, {
778
779
  signal: runtimeCancellationSignal,
779
780
  sandboxEnvironment,
781
+ onModelVisibleContext: async (snapshot) => {
782
+ await persistModelContextSnapshot(db, {
783
+ accountId: input.accountId,
784
+ workspaceId: input.workspaceId,
785
+ sessionId: input.sessionId,
786
+ turnId: activeTurnId,
787
+ attemptId: input.attemptId,
788
+ executionGeneration: attempt.executionGeneration,
789
+ snapshot,
790
+ });
791
+ },
780
792
  onRuntimeEvent: async (event) => {
781
793
  await leases.renewServing("runtime_event");
782
794
  if (leases.servingLost()) {
@@ -890,7 +902,9 @@ export async function runTurnStreamAttempt(
890
902
  });
891
903
  }
892
904
  };
893
- if (leases.codex.lost) {
905
+ if (billingState.isCodexTurn) {
906
+ leases.codex.assertUsable();
907
+ } else if (leases.codex.lost) {
894
908
  throw new Error("Codex credential lease expired before the model run");
895
909
  }
896
910
  if (leases.xai.lost) {
@@ -1,4 +1,6 @@
1
1
  import {
2
+ beginConnectorActionExecution,
3
+ completeConnectorActionExecution,
2
4
  getScheduledVariableSetExpectedGenerationForAttempt,
3
5
  getWorkspaceModelPolicy,
4
6
  listWorkspaceGatewayCustomModels,
@@ -6,6 +8,8 @@ import {
6
8
  listOrganizationModelProviderCustomModelsForWorkspace,
7
9
  organizationModelProviderConnectionActiveForWorkspace,
8
10
  persistAttemptToolCatalog,
11
+ prepareConnectorActionApproval,
12
+ previewConnectorActionApproval,
9
13
  namedSubjectHasLiveWorkspaceAuthority,
10
14
  updateSessionTitleWithEvent,
11
15
  withCodexAppsRequestAuthorization,
@@ -19,16 +23,15 @@ import {
19
23
  type OpenGeniRuntime,
20
24
  type AttemptConnectorActionBinding,
21
25
  type ConnectorAttachmentMaterializationRequest,
26
+ type ConnectorActionPolicyHooks,
22
27
  createFirstPartyInteractionAttemptToolDefinitions,
23
28
  } from "@opengeni/runtime";
24
29
  import {
25
- authorizeGoogleDrivePublicationAttempt,
26
30
  createGoogleDrivePublicationAttemptTool,
27
31
  googleDrivePublicationConnectorCall,
28
32
  resolveGoogleDrivePublicationTarget,
29
33
  } from "../google-drive-publication";
30
34
  import { connectionTokenResolverForTurn } from "../mcp-credentials";
31
- import { buildApiIntegrationServersForTurn } from "../api-integrations";
32
35
  import { buildGitHubRestMcpForTurn } from "../../github-rest-mcp";
33
36
  import { materializeConnectorAttachmentsInChannel } from "../connector-attachments";
34
37
  import { allowedFirstPartyMcpToolsForSession, type Settings } from "@opengeni/config";
@@ -40,6 +43,7 @@ import {
40
43
  defaultSessionMcpServerIds,
41
44
  loadRigDefaultVariableSetEnvironment,
42
45
  mergeRigDefaultVariableSetEnvironment,
46
+ buildApiIntegrationMcpServers,
43
47
  resolveCatalogSettings,
44
48
  resolveWorkspaceModelSelection,
45
49
  withFrozenPersonalConnectionDelegations,
@@ -439,7 +443,7 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
439
443
  );
440
444
  };
441
445
  const selectedApiIntegrationServerIds = new Set(turnTools.map((tool) => tool.id));
442
- const apiIntegrationMcpServers = buildApiIntegrationServersForTurn({
446
+ const apiIntegrationMcpServers = buildApiIntegrationMcpServers({
443
447
  settings: runSettings,
444
448
  integrations: installedApiIntegrations.filter((integration) =>
445
449
  selectedApiIntegrationServerIds.has(integration.serverId),
@@ -531,6 +535,25 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
531
535
  },
532
536
  ]
533
537
  : [];
538
+ const attemptConnectorActionBindings = [
539
+ ...googleDriveConnectorBindings,
540
+ ...githubRestMcp.connectorBindings,
541
+ ];
542
+ const connectorActionPolicy: ConnectorActionPolicyHooks = {
543
+ preview: async (call) =>
544
+ await previewConnectorActionApproval(db, connectorActionIdentity, call),
545
+ prepare: async (call) =>
546
+ await prepareConnectorActionApproval(db, connectorActionIdentity, call),
547
+ begin: async (call) => await beginConnectorActionExecution(db, connectorActionIdentity, call),
548
+ complete: async ({ requestId, outcome }) =>
549
+ await completeConnectorActionExecution(db, {
550
+ accountId: input.accountId,
551
+ workspaceId: input.workspaceId,
552
+ requestId,
553
+ attemptId: input.attemptId,
554
+ outcome,
555
+ }),
556
+ };
534
557
  const attemptToolDefinitions = [
535
558
  createListModelsAttemptToolDefinition({
536
559
  currentModelId: turnExecutionPolicy.productModelId,
@@ -760,30 +783,8 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
760
783
  nestedAgentDepth: session.nestedAgentDepth,
761
784
  effectiveMaxNestedAgentDepth: session.effectiveMaxNestedAgentDepth,
762
785
  attemptToolDefinitions,
763
- ...((googleDrivePublicationTarget && googleDrivePublicationAllowed) ||
764
- githubRestMcp.authorizeCodemodeCall
765
- ? {
766
- attemptToolAuthorize: async (authorization) => {
767
- const { call } = authorization;
768
- if (
769
- googleDrivePublicationTarget &&
770
- googleDrivePublicationAllowed &&
771
- call.caller.kind === "codemode" &&
772
- call.identity.serverId === "google-drive-publishing" &&
773
- call.identity.toolName === "google_drive_publish_file"
774
- ) {
775
- await authorizeGoogleDrivePublicationAttempt({
776
- db,
777
- identity: connectorActionIdentity,
778
- target: googleDrivePublicationTarget,
779
- approvalId: call.operationId,
780
- arguments: call.arguments,
781
- });
782
- }
783
- await githubRestMcp.authorizeCodemodeCall?.(authorization);
784
- },
785
- }
786
- : {}),
786
+ connectorActionPolicy,
787
+ attemptConnectorActionBindings,
787
788
  }),
788
789
  cancellationSignal,
789
790
  async (latePreparedTools) => await latePreparedTools.close().catch(() => undefined),
@@ -855,11 +856,8 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
855
856
  activatePreparedToolEnvironment(eventing.preparedTools);
856
857
  }
857
858
  return {
858
- attemptConnectorActionBindings: [
859
- ...googleDriveConnectorBindings,
860
- ...githubRestMcp.connectorBindings,
861
- ],
862
- connectorActionIdentity,
859
+ attemptConnectorActionBindings,
860
+ connectorActionPolicy,
863
861
  generateSessionTitleInParallel: titleToolPlan.generateTitleInParallel,
864
862
  postToolPreparationStartedAt,
865
863
  preparationIndependentToolNames: titleToolPlan.preparationIndependentToolNames,
@@ -64,17 +64,23 @@ export function computerToolModeForTurn(
64
64
  /** Chat wires and Gateway models do not advertise OpenAI's hosted sandbox tool types. */
65
65
  export function structuredToolTransportForTurn(
66
66
  resolvedModel: {
67
- provider: { kind: ResolvedModelProvider["kind"]; api: ModelProviderApi };
67
+ provider: {
68
+ id: string;
69
+ kind: ResolvedModelProvider["kind"];
70
+ api: ModelProviderApi;
71
+ wireProfile: "openai" | "azure-openai";
72
+ builtin: boolean;
73
+ baseUrl?: string | undefined;
74
+ };
68
75
  } | null,
69
76
  ): boolean {
70
77
  if (!resolvedModel) return true;
71
- if (resolvedModel.provider.api === "chat") return false;
72
- return ![
73
- "codex-subscription",
74
- "xai-subscription",
75
- "vercel-gateway-managed",
76
- "vercel-gateway-workspace",
77
- ].includes(resolvedModel.provider.kind);
78
+ const provider = resolvedModel.provider;
79
+ if (provider.api === "chat" || provider.kind === "codex-subscription") return false;
80
+ return (
81
+ provider.wireProfile === "azure-openai" ||
82
+ (provider.builtin && provider.id === "openai" && provider.baseUrl === undefined)
83
+ );
78
84
  }
79
85
 
80
86
  /**
@@ -2,6 +2,7 @@ import type { Settings } from "@opengeni/config";
2
2
  import type { CodexUsageHeaderSnapshot } from "@opengeni/codex";
3
3
  import type { AppendEventInput, ApplySessionTurnSettlementInput } from "@opengeni/db";
4
4
  import type {
5
+ CodexCredentialPolicySnapshotV1,
5
6
  ModelContextContributionSummary,
6
7
  SessionStatus,
7
8
  XaiProviderAccountAuthoritySnapshotV1,
@@ -45,6 +46,7 @@ export type TurnSettleFn = (input: {
45
46
  | "requires_action";
46
47
  sessionStatus: SessionStatus;
47
48
  activeTurnId: string | null;
49
+ suppressGoalContinuation?: boolean;
48
50
  consumeRequestedCompactionFailure?: boolean;
49
51
  runState?: ApplySessionTurnSettlementInput["runState"];
50
52
  }) => Promise<boolean>;
@@ -59,6 +61,7 @@ export type TurnControlState = {
59
61
 
60
62
  export type AttemptIdentityState = {
61
63
  turnId: string | undefined;
64
+ dispatchId: string;
62
65
  triggerEventId: string | undefined;
63
66
  executionGeneration: number;
64
67
  providerRecoveryCount: number;
@@ -150,6 +153,12 @@ export type ProviderTurnState = {
150
153
  // The Codex account this turn runs on (pin > workspace active), resolved once
151
154
  // a codex-billed turn is confirmed and threaded into the token resolver.
152
155
  effectiveCodexCredentialId: string | null;
156
+ /** Exact row version returned by the resolver for the latest provider request. */
157
+ effectiveCodexCredentialVersion: number | null;
158
+ /** Frozen alternate-account ceiling observed by the fenced allocator. */
159
+ codexCredentialFailoverLimit: number;
160
+ /** Accepted Codex allocator policy captured with the first durable lease. */
161
+ codexPolicySnapshot: CodexCredentialPolicySnapshotV1 | null;
153
162
  effectiveXaiCredentialId: string | null;
154
163
  xaiRotationEnabled: boolean;
155
164
  xaiAuthoritySnapshot: XaiProviderAccountAuthoritySnapshotV1 | null;
@@ -196,6 +205,7 @@ export function createTurnContext(input: {
196
205
  },
197
206
  attempt: {
198
207
  turnId: undefined,
208
+ dispatchId: "",
199
209
  triggerEventId: undefined,
200
210
  executionGeneration: 0,
201
211
  providerRecoveryCount: 0,
@@ -268,6 +278,9 @@ export function createTurnContext(input: {
268
278
  },
269
279
  providerTurn: {
270
280
  effectiveCodexCredentialId: null,
281
+ effectiveCodexCredentialVersion: null,
282
+ codexCredentialFailoverLimit: 1,
283
+ codexPolicySnapshot: null,
271
284
  effectiveXaiCredentialId: null,
272
285
  xaiRotationEnabled: false,
273
286
  xaiAuthoritySnapshot: null,