@opengeni/worker-bundle 0.22.8 → 0.23.4-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 (103) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +10 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +1 -4
  3. package/dist/activities/agent-turn/claim.d.ts +3 -4
  4. package/dist/activities/agent-turn/codex.d.ts +8 -7
  5. package/dist/activities/agent-turn/compaction-prep.d.ts +2 -0
  6. package/dist/activities/agent-turn/errors.d.ts +13 -0
  7. package/dist/activities/agent-turn/finalization.d.ts +1 -3
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +10 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +39 -0
  11. package/dist/activities/agent-turn/sandbox-route.d.ts +14 -24
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -4
  13. package/dist/activities/agent-turn/sandbox-shared-preparation.d.ts +30 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +7 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  16. package/dist/activities/agent-turn/tool-policy.d.ts +7 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +3 -8
  18. package/dist/activities/agent-turn/turn-sandbox-access.d.ts +12 -0
  19. package/dist/activities/agent-turn/video-reference-sandbox.d.ts +7 -0
  20. package/dist/activities/automations.d.ts +1 -0
  21. package/dist/activities/capabilities.d.ts +2 -1
  22. package/dist/activities/codex-rotation.d.ts +2 -0
  23. package/dist/activities/goals.d.ts +11 -0
  24. package/dist/activities/image-generation-references.d.ts +2 -1
  25. package/dist/activities/scheduled-tasks.d.ts +1 -0
  26. package/dist/activities/session-state.d.ts +2 -1
  27. package/dist/activities/types.d.ts +8 -0
  28. package/dist/{activities-control-VZIIV7RX.js → activities-control-SGU2WEVB.js} +328 -107
  29. package/dist/activities-control-SGU2WEVB.js.map +1 -0
  30. package/dist/activities-control.d.ts +2 -0
  31. package/dist/{activities-turn-5RGQDBFV.js → activities-turn-7EAQXW6Q.js} +828 -540
  32. package/dist/activities-turn-7EAQXW6Q.js.map +1 -0
  33. package/dist/activities.d.ts +2 -0
  34. package/dist/{chunk-O4HDX5SC.js → chunk-S2QVRVJW.js} +227 -36
  35. package/dist/chunk-S2QVRVJW.js.map +1 -0
  36. package/dist/{chunk-NAVSAZ56.js → chunk-TJIPCHMP.js} +162 -2
  37. package/dist/chunk-TJIPCHMP.js.map +1 -0
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.js +69 -6
  40. package/dist/index.js.map +1 -1
  41. package/dist/observability-metrics.d.ts +29 -0
  42. package/dist/personal-github-git-credentials.d.ts +1 -0
  43. package/dist/unhandled-rejection-boundary.d.ts +29 -0
  44. package/dist/workflow-bundle.js +26 -3
  45. package/dist/workflows/activities.d.ts +5 -0
  46. package/dist/workflows/automations.d.ts +5 -0
  47. package/package.json +18 -18
  48. package/src/activities/agent-run-admission.ts +7 -1
  49. package/src/activities/agent-turn/admission.ts +28 -6
  50. package/src/activities/agent-turn/agent-build.ts +15 -31
  51. package/src/activities/agent-turn/claim.ts +53 -72
  52. package/src/activities/agent-turn/codex.ts +21 -12
  53. package/src/activities/agent-turn/compaction-prep.ts +52 -1
  54. package/src/activities/agent-turn/errors.ts +78 -0
  55. package/src/activities/agent-turn/failure-settlement.ts +30 -19
  56. package/src/activities/agent-turn/finalization.ts +24 -21
  57. package/src/activities/agent-turn/history.ts +5 -1
  58. package/src/activities/agent-turn/list-models.ts +101 -0
  59. package/src/activities/agent-turn/model-usage.ts +88 -42
  60. package/src/activities/agent-turn/quiescence.ts +86 -0
  61. package/src/activities/agent-turn/run-credentials.ts +12 -1
  62. package/src/activities/agent-turn/run.ts +20 -14
  63. package/src/activities/agent-turn/sandbox-establish.ts +46 -11
  64. package/src/activities/agent-turn/sandbox-route.ts +21 -25
  65. package/src/activities/agent-turn/sandbox-runtime.ts +1 -116
  66. package/src/activities/agent-turn/sandbox-shared-preparation.ts +206 -0
  67. package/src/activities/agent-turn/stream-attempt.ts +106 -14
  68. package/src/activities/agent-turn/tool-environment.ts +72 -14
  69. package/src/activities/agent-turn/tool-policy.ts +7 -6
  70. package/src/activities/agent-turn/turn-context.ts +12 -14
  71. package/src/activities/agent-turn/{image-reference-sandbox.ts → turn-sandbox-access.ts} +15 -13
  72. package/src/activities/agent-turn/video-reference-sandbox.ts +23 -0
  73. package/src/activities/automations.ts +122 -67
  74. package/src/activities/capabilities.ts +72 -2
  75. package/src/activities/codex-capacity.ts +13 -5
  76. package/src/activities/codex-rotation.ts +17 -0
  77. package/src/activities/goals.ts +117 -35
  78. package/src/activities/image-generation-references.ts +9 -9
  79. package/src/activities/retained-screenshots.ts +104 -21
  80. package/src/activities/sandbox-lease.ts +7 -4
  81. package/src/activities/scheduled-tasks.ts +248 -30
  82. package/src/activities/session-state.ts +8 -1
  83. package/src/activities/site-auth-maintenance.ts +12 -12
  84. package/src/activities/types.ts +6 -0
  85. package/src/activities.ts +1 -0
  86. package/src/activity-services.ts +4 -1
  87. package/src/github-rest-mcp.ts +8 -4
  88. package/src/http.ts +3 -0
  89. package/src/index.ts +38 -4
  90. package/src/observability-metrics.ts +202 -0
  91. package/src/personal-github-git-credentials.ts +45 -4
  92. package/src/sandbox-resume.ts +1 -1
  93. package/src/sandbox-routing.ts +7 -4
  94. package/src/unhandled-rejection-boundary.ts +73 -0
  95. package/src/workflows/activities.ts +15 -0
  96. package/src/workflows/automations.ts +21 -2
  97. package/dist/activities/agent-turn/image-reference-sandbox.d.ts +0 -14
  98. package/dist/activities/recording.d.ts +0 -90
  99. package/dist/activities-control-VZIIV7RX.js.map +0 -1
  100. package/dist/activities-turn-5RGQDBFV.js.map +0 -1
  101. package/dist/chunk-NAVSAZ56.js.map +0 -1
  102. package/dist/chunk-O4HDX5SC.js.map +0 -1
  103. package/src/activities/recording.ts +0 -279
@@ -16,6 +16,11 @@ export declare const activity: import("@temporalio/workflow").ActivityInterfaceF
16
16
  * is an independent workflow and will re-evaluate current state. */
17
17
  export declare const scheduledTaskActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "dispatchScheduledTaskRun">>;
18
18
  export declare const automationActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "dispatchAutomationRun">>;
19
+ /** Dispatch has a bounded retry window, but an accepted automation event must
20
+ * not remain durably `dispatching` after that window closes. This compensating
21
+ * database write is idempotent and retries without a cap until the run reaches
22
+ * a terminal state (or is observed as already dispatched). */
23
+ export declare const automationFailureActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "settleAutomationRunFailure">>;
19
24
  /** Goal continuation evaluates a durable Postgres obligation at an idle
20
25
  * boundary. A transient failure gets a short retry window, then records an
21
26
  * explicit delayed outbox wake instead of relying on an unrelated mutation or
@@ -3,4 +3,9 @@ export type AutomationRunWorkflowInput = {
3
3
  workspaceId: string;
4
4
  runId: string;
5
5
  };
6
+ export type AutomationRunWorkflowActivities = {
7
+ dispatchAutomationRun: (input: AutomationRunWorkflowInput) => Promise<unknown>;
8
+ settleAutomationRunFailure: (input: AutomationRunWorkflowInput) => Promise<void>;
9
+ };
10
+ export declare function runAutomationRunWorkflow(input: AutomationRunWorkflowInput, activities: AutomationRunWorkflowActivities): Promise<void>;
6
11
  export declare function automationRunWorkflow(input: AutomationRunWorkflowInput): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.22.8",
3
+ "version": "0.23.4-canary.0",
4
4
  "description": "OpenGeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -53,23 +53,23 @@
53
53
  "prepublishOnly": "bash ../../scripts/prepublish-guard"
54
54
  },
55
55
  "dependencies": {
56
- "@llamaindex/liteparse": "^1.5.3",
57
- "@opengeni/agent-proto": "^0.5.1",
58
- "@opengeni/capabilities": "^0.3.1",
59
- "@opengeni/codemode": "^0.4.17",
60
- "@opengeni/codex": "^0.2.19",
61
- "@opengeni/config": "^0.22.0",
62
- "@opengeni/contracts": "^2.7.0",
63
- "@opengeni/core": "^2.5.3",
64
- "@opengeni/db": "^3.6.0",
65
- "@opengeni/documents": "^0.8.6",
66
- "@opengeni/events": "^0.4.4",
67
- "@opengeni/github": "^0.6.1",
68
- "@opengeni/network": "^0.2.3",
69
- "@opengeni/observability": "^0.8.9",
70
- "@opengeni/runtime": "^1.4.2",
71
- "@opengeni/storage": "^0.2.110",
72
- "@opengeni/xai-subscription": "^0.1.2",
56
+ "@llamaindex/liteparse": "2.14.2",
57
+ "@opengeni/agent-proto": "^0.5.1-canary.7",
58
+ "@opengeni/capabilities": "^0.3.1-canary.6",
59
+ "@opengeni/codemode": "^0.4.22-canary.1",
60
+ "@opengeni/codex": "^0.2.20-canary.1",
61
+ "@opengeni/config": "^0.22.5-canary.1",
62
+ "@opengeni/contracts": "^2.9.2-canary.1",
63
+ "@opengeni/core": "^2.6.4-canary.0",
64
+ "@opengeni/db": "^3.7.4-canary.0",
65
+ "@opengeni/documents": "^0.8.14-canary.0",
66
+ "@opengeni/events": "^0.4.12-canary.0",
67
+ "@opengeni/github": "^0.6.7-canary.1",
68
+ "@opengeni/network": "^0.2.3-canary.6",
69
+ "@opengeni/observability": "^0.8.14-canary.1",
70
+ "@opengeni/runtime": "^2.0.1-canary.1",
71
+ "@opengeni/storage": "^0.2.115-canary.1",
72
+ "@opengeni/xai-subscription": "^0.1.2-canary.6",
73
73
  "@temporalio/activity": "^1.17.0",
74
74
  "@temporalio/client": "^1.17.0",
75
75
  "@temporalio/worker": "^1.17.0",
@@ -1,4 +1,5 @@
1
1
  import { configuredStaticUsageLimits, type Settings } from "@opengeni/config";
2
+ import { modelFundingForAdmission } from "@opengeni/core";
2
3
  import { getBillingBalance, isCodexBilledTurn, sumUsageQuantity } from "@opengeni/db";
3
4
  import type { ControlActivityServices } from "./types";
4
5
 
@@ -17,12 +18,17 @@ export async function agentRunAdmissionDenial(
17
18
  requestedAgentRuns: number;
18
19
  },
19
20
  ): Promise<AgentRunAdmissionDenial | null> {
20
- const externallyBilled = await isCodexBilledTurn({
21
+ const codexBilled = await isCodexBilledTurn({
21
22
  db: services.db,
22
23
  settings: services.settings,
23
24
  workspaceId: input.workspaceId,
24
25
  model: input.model,
25
26
  });
27
+ const externallyBilled = modelFundingForAdmission(
28
+ services.settings,
29
+ input.model,
30
+ codexBilled,
31
+ ).fundedWithoutCredits;
26
32
  if (
27
33
  !externallyBilled &&
28
34
  (services.settings.billingMode === "stripe" || services.settings.usageLimitsMode === "managed")
@@ -12,6 +12,7 @@ import {
12
12
  type SessionTurn,
13
13
  type ToolAuthNeededPayload,
14
14
  type TurnExecutionPolicyV1,
15
+ type XaiProviderAccountAuthoritySnapshotV1,
15
16
  } from "@opengeni/contracts";
16
17
  import { createHash } from "node:crypto";
17
18
  import { startOfUtcMonth } from "./model-usage";
@@ -38,6 +39,22 @@ export function credentialSubjectIdForTurnInitiator(
38
39
  return directPersonalConnectionSubjectId(turn);
39
40
  }
40
41
 
42
+ export function xaiCatalogReadinessAuthority(
43
+ turn: {
44
+ initiatingHumanSubjectId: string | null;
45
+ xaiProviderAccountAuthoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
46
+ },
47
+ directCredentialSubjectId: string | undefined,
48
+ ): {
49
+ subjectId: string;
50
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
51
+ } | null {
52
+ const subjectId = turn.initiatingHumanSubjectId ?? directCredentialSubjectId;
53
+ return subjectId
54
+ ? { subjectId, authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot }
55
+ : null;
56
+ }
57
+
41
58
  /**
42
59
  * Direct authenticated-human work is the persisted human source with subject
43
60
  * authority and no inherited agent or legacy provenance. Use this complete
@@ -84,11 +101,13 @@ export function shouldPublishToolAuthNeededForTurn(
84
101
 
85
102
  export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1): {
86
103
  externallyBilled: boolean;
104
+ countsTowardTokenCap: boolean;
87
105
  codexSubscription: boolean;
88
106
  xaiSubscription: boolean;
89
107
  } {
90
108
  return {
91
109
  externallyBilled: policy.billing.metering === "external",
110
+ countsTowardTokenCap: policy.billing.upstreamPayer === "deployment",
92
111
  codexSubscription:
93
112
  policy.providerId === "codex-subscription" &&
94
113
  policy.credentialSource.kind === "connected_subscription" &&
@@ -222,10 +241,13 @@ export async function ensureRunAllowed(
222
241
  workspaceId: string,
223
242
  isExternallyBilledTurn: boolean,
224
243
  entitlements?: ActivityServices["entitlements"],
244
+ chargesOpenGeniCredits = !isExternallyBilledTurn,
245
+ countsTowardTokenCap = !isExternallyBilledTurn,
225
246
  ): Promise<void> {
226
- // Externally billed turns are paid outside OpenGeni: skip the credit-balance
227
- // gate and monthly token cap. The agent-run COUNT cap below is a
228
- // volume/fairness quota (not a credit/cost gate) and is intentionally kept.
247
+ // Upstream settlement and workspace-facing cost are independent. External
248
+ // metering skips the token cap; free/subscription/workspace cost skips the
249
+ // OpenGeni credit gate. The agent-run COUNT cap below is a volume/fairness
250
+ // quota and is intentionally kept for every funding path.
229
251
  //
230
252
  // §7.5 P3 — host-entitlements DELEGATION (the worker half of the same seam the
231
253
  // API edge exposes). For a non-codex turn, when the host binds `entitlements`, its
@@ -239,7 +261,7 @@ export async function ensureRunAllowed(
239
261
  // idempotency-keyed writer at recordModelUsageAndDebitCredits), so a PULL host meter
240
262
  // is consulted without ever double-charging.
241
263
  if (
242
- !isExternallyBilledTurn &&
264
+ chargesOpenGeniCredits &&
243
265
  entitlements &&
244
266
  (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
245
267
  ) {
@@ -253,7 +275,7 @@ export async function ensureRunAllowed(
253
275
  throw new Error(decision.reason || "insufficient OpenGeni credits");
254
276
  }
255
277
  } else if (
256
- !isExternallyBilledTurn &&
278
+ chargesOpenGeniCredits &&
257
279
  (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
258
280
  ) {
259
281
  const balance = await getBillingBalance(db, accountId);
@@ -278,7 +300,7 @@ export async function ensureRunAllowed(
278
300
  );
279
301
  }
280
302
  }
281
- if (!isExternallyBilledTurn && limits.maxMonthlyTokensPerWorkspace) {
303
+ if (countsTowardTokenCap && limits.maxMonthlyTokensPerWorkspace) {
282
304
  const used = await sumUsageQuantity(db, {
283
305
  workspaceId,
284
306
  eventType: "model.tokens",
@@ -54,7 +54,6 @@ import { sandboxRunAs } from "@opengeni/runtime";
54
54
  import { VideoGenerationRejectedResult } from "@opengeni/contracts";
55
55
 
56
56
  import {
57
- computerToolModeForTurn,
58
57
  structuredToolTransportForTurn,
59
58
  hostedWebSearchForTurn,
60
59
  connectedSubscriptionImageGenerationAuthority,
@@ -62,7 +61,6 @@ import {
62
61
  import type { ClaimTurnOk } from "./claim";
63
62
  import type { GovernanceModelOk } from "./governance-model";
64
63
  import type { CompactionPrepOk } from "./compaction-prep";
65
- import type { SandboxTurnRuntime } from "./sandbox-runtime";
66
64
  import type { runtimeResourcesForTurn } from "./file-resources";
67
65
  import type { sandboxArtifactRuntimeAdmission } from "./sandbox-route";
68
66
  import type {
@@ -73,11 +71,11 @@ import type {
73
71
  AttemptIdentityState,
74
72
  EventingState,
75
73
  ProviderTurnState,
76
- RecordingState,
77
74
  SandboxRuntimeState,
78
75
  } from "./turn-context";
79
76
  import { SESSION_TITLE_MODEL_TOOL_NAME } from "./session-title";
80
- import { resolveImageReferenceSandboxSession } from "./image-reference-sandbox";
77
+ import { resolveTurnSandboxAccess } from "./turn-sandbox-access";
78
+ import { resolveVideoReferenceSandboxAccess } from "./video-reference-sandbox";
81
79
 
82
80
  export type BuildTurnAgentDeps = {
83
81
  input: RunAgentTurnInput;
@@ -90,8 +88,6 @@ export type BuildTurnAgentDeps = {
90
88
  eventing: EventingState;
91
89
  attempt: AttemptIdentityState;
92
90
  sandboxState: SandboxRuntimeState;
93
- recordingState: RecordingState;
94
- maybeStartOnTurnRecording: SandboxTurnRuntime["maybeStartOnTurnRecording"];
95
91
  providerTurn: ProviderTurnState;
96
92
  media: ReturnType<typeof createTurnMediaArtifacts>;
97
93
  leases: ReturnType<typeof createTurnCredentialLeases>;
@@ -160,8 +156,6 @@ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
160
156
  runtimeCancellationSignal,
161
157
  eventing,
162
158
  sandboxState,
163
- recordingState,
164
- maybeStartOnTurnRecording,
165
159
  providerTurn,
166
160
  media,
167
161
  leases,
@@ -254,9 +248,10 @@ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
254
248
  subjectId: fileAuthoritySubjectId,
255
249
  references,
256
250
  readSandboxFile: async (path, maxBytes) => {
257
- const imageReferenceSandbox = await resolveImageReferenceSandboxSession(
251
+ const imageReferenceSandbox = await resolveTurnSandboxAccess(
258
252
  sandboxState,
259
253
  media.sdkOwnedSandboxSession,
254
+ "Sandbox image reference is unavailable",
260
255
  );
261
256
  const relativePath = path.slice("/workspace/".length);
262
257
  const referenceRunAs = sandboxRunAs(eventing.modelRunSettings);
@@ -508,9 +503,15 @@ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
508
503
  videoGeneration: {
509
504
  capabilities: async () => capabilities,
510
505
  execute: async (toolInput, { toolCallId }) => {
511
- const sessionForReference =
512
- sandboxState.resolvedSandbox?.established.session ?? media.sdkOwnedSandboxSession;
506
+ const referenceSandbox = await resolveVideoReferenceSandboxAccess(
507
+ toolInput,
508
+ sandboxState,
509
+ media.sdkOwnedSandboxSession,
510
+ );
513
511
  const fence = eventing.toolCancellationFenceRef.current;
512
+ if (referenceSandbox && !fence) {
513
+ throw new Error("Video reference command fence is unavailable");
514
+ }
514
515
  const runAs = sandboxRunAs(eventing.modelRunSettings);
515
516
  let accepted: Awaited<ReturnType<typeof admitVideoGenerationRequest>>;
516
517
  try {
@@ -527,11 +528,11 @@ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
527
528
  toolInput,
528
529
  policy: videoGenerationPolicy,
529
530
  credential: videoGenerationCredential,
530
- ...(sessionForReference && fence
531
+ ...(referenceSandbox && fence
531
532
  ? {
532
533
  runCommand: async (command) =>
533
534
  await fence.runSandboxCommandStructured(
534
- sessionForReference as TurnSandboxCommandSession,
535
+ referenceSandbox.session as TurnSandboxCommandSession,
535
536
  {
536
537
  ...command,
537
538
  ...(runAs ? { runAs } : {}),
@@ -710,31 +711,14 @@ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
710
711
  // typed input_image content. Chat wires have no proven typed image
711
712
  // result transport and therefore receive no view_image tool.
712
713
  structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
713
- // EXPLICIT computer-use tool transport. See {@link computerToolModeForTurn}.
714
- computerToolMode: computerToolModeForTurn(resolvedModel),
715
714
  ...(promptCacheKey ? { promptCacheKey } : {}),
716
715
  }
717
716
  : // LEGACY global-client fallback (resolveTurnModel returned null → the model
718
717
  // is not in the registry, served by the built-in OpenAI/Azure Responses
719
- // client). Pin computerToolMode to function-image EXPLICITLY rather than
720
- // leaving the runtime to sniff the instance.
718
+ // client).
721
719
  {
722
- computerToolMode: computerToolModeForTurn(null),
723
720
  promptCacheKey: input.sessionId,
724
721
  }),
725
- // Lazy computer-use seam: runtime first brings up :0 only after the model
726
- // selects a computer tool, then this hook begins the optional proof
727
- // recording. Shell/filesystem turns never invoke either operation.
728
- onComputerUseReady: async () => {
729
- if (!sandboxState.resolvedSandbox) {
730
- throw new Error("Computer-use display became ready without a resolved sandbox");
731
- }
732
- // This callback is the authoritative execution boundary. Record the
733
- // action before async ffmpeg startup so transport-event ordering cannot
734
- // make settlement misclassify a real computer turn as unused.
735
- recordingState.didComputerUse = true;
736
- await maybeStartOnTurnRecording(sandboxState.resolvedSandbox, activeSandboxBackend);
737
- },
738
722
  onRetainableSessionImageOutput: media.retainSessionImageAtToolBoundary,
739
723
  ...(runtimeSkillActivations.length > 0
740
724
  ? { skillActivations: runtimeSkillActivations }
@@ -10,12 +10,11 @@ import {
10
10
  type AppendEventInput,
11
11
  type ApiIntegrationRuntime,
12
12
  type CanonicalTurnStartupMilestoneReceipt,
13
+ type ClaimSessionWorkForAttemptInput,
13
14
  type SessionTurnForExecution,
14
- type SessionTurnRecordingSettlement,
15
15
  } from "@opengeni/db";
16
16
  import { appendAndPublishTurnEventsFenced, publishDurableSessionEvents } from "@opengeni/events";
17
17
  import { deliverChildRequiresActionToParent } from "../parent-wake";
18
- import { deleteRecordingArtifacts } from "@opengeni/runtime";
19
18
  import {
20
19
  assertTurnExecutionPolicyMatchesConfigV1,
21
20
  resolveTurnExecutionPolicyV1,
@@ -25,11 +24,13 @@ import {
25
24
  settingsWithCodexCredential,
26
25
  settingsWithEnabledCapabilityMcpServers,
27
26
  settingsWithWorkspaceGatewayCredential,
27
+ settingsWithWorkspaceOpenRouterCredential,
28
28
  withXaiSubscriptionProvider,
29
29
  } from "../capabilities";
30
30
  import { validateIncidentTelemetrySystemUpdateAuthority } from "../incident-telemetry-authority";
31
31
  import {
32
32
  assertSessionAllowsProductModel,
33
+ resolveCatalogSettings,
33
34
  resolveCodexAppsCredentialIdForRun,
34
35
  } from "@opengeni/core";
35
36
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
@@ -42,12 +43,12 @@ import type {
42
43
  import { makeTurnOpJournal, type TurnHeartbeatDetails } from "../../op-journal";
43
44
  import {
44
45
  recordSessionEventAppendLatency,
46
+ recordSessionEventAppendPhase,
45
47
  recordSessionEventPublishLatency,
46
48
  recordTurnStartupPhase,
47
49
  recordTurnStartupMilestone,
48
50
  turnLifecycleMetricsFor,
49
51
  } from "../../observability-metrics";
50
- import { prepareRecordingForSettlement, type ActiveRecording } from "../recording";
51
52
  import { createTurnCredentialLeases } from "./credential-leases";
52
53
  import { createTurnMediaArtifacts } from "./media-artifacts";
53
54
  import { readTurnExecutionPolicyV1 } from "@opengeni/contracts";
@@ -66,7 +67,6 @@ import type {
66
67
  BillingState,
67
68
  ClaimedResult,
68
69
  EventingState,
69
- RecordingState,
70
70
  SandboxRuntimeState,
71
71
  TurnControlState,
72
72
  } from "./turn-context";
@@ -74,10 +74,10 @@ import type {
74
74
  export type ClaimTurnDeps = {
75
75
  input: RunAgentTurnInput;
76
76
  settings: Settings;
77
+ catalogSourceSettings: Settings;
77
78
  db: ActivityServices["db"];
78
79
  bus: ActivityServices["bus"];
79
80
  runtime: ActivityServices["runtime"];
80
- objectStorage: ActivityServices["objectStorage"];
81
81
  observability: ActivityServices["observability"];
82
82
  entitlements: ActivityServices["entitlements"];
83
83
  wakeSessionWorkflow: ActivityServices["wakeSessionWorkflow"];
@@ -89,13 +89,11 @@ export type ClaimTurnDeps = {
89
89
  attempt: AttemptIdentityState;
90
90
  billingState: BillingState;
91
91
  sandboxState: SandboxRuntimeState;
92
- recordingState: RecordingState;
93
92
  eventing: EventingState;
94
93
  leases: ReturnType<typeof createTurnCredentialLeases>;
95
94
  media: ReturnType<typeof createTurnMediaArtifacts>;
96
95
  claimedResult: ClaimedResult;
97
96
  acknowledgeLostAttemptOwnership: () => void;
98
- abandonActiveRecording: (reason: string, disposition?: "failed" | "discard") => Promise<void>;
99
97
  };
100
98
 
101
99
  export type ClaimTurnOk = {
@@ -112,6 +110,7 @@ export type ClaimTurnOk = {
112
110
  interactionInterventionResume: Awaited<
113
111
  ReturnType<typeof getInteractionInterventionResumeForEvent>
114
112
  >;
113
+ attachPendingUpdatesAfterOpenSuffix: () => Promise<boolean>;
115
114
  throwIfWorkerShuttingDown: () => void;
116
115
  throwIfTurnCancelled: () => void;
117
116
  opJournal: ReturnType<typeof makeTurnOpJournal>;
@@ -139,10 +138,10 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
139
138
  const {
140
139
  input,
141
140
  settings,
141
+ catalogSourceSettings,
142
142
  db,
143
143
  bus,
144
144
  runtime,
145
- objectStorage,
146
145
  observability,
147
146
  entitlements,
148
147
  wakeSessionWorkflow,
@@ -154,15 +153,26 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
154
153
  attempt,
155
154
  billingState,
156
155
  sandboxState,
157
- recordingState,
158
156
  eventing,
159
157
  leases,
160
158
  media,
161
159
  claimedResult,
162
160
  acknowledgeLostAttemptOwnership,
163
- abandonActiveRecording,
164
161
  } = deps;
165
162
 
163
+ const deploymentCatalogSettings = (await resolveCatalogSettings(db, catalogSourceSettings))
164
+ .settings;
165
+
166
+ const validatePendingSystemUpdateAuthority: NonNullable<
167
+ ClaimSessionWorkForAttemptInput["validatePendingSystemUpdateAuthority"]
168
+ > = async (tx, update) =>
169
+ await validateIncidentTelemetrySystemUpdateAuthority({
170
+ db: tx,
171
+ settings: deploymentCatalogSettings,
172
+ workspaceId: input.workspaceId,
173
+ sessionId: input.sessionId,
174
+ update,
175
+ });
166
176
  const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
167
177
  sessionId: input.sessionId,
168
178
  workflowId: input.workflowId,
@@ -170,14 +180,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
170
180
  attemptId: input.attemptId,
171
181
  dispatchId,
172
182
  trigger: input.trigger,
173
- validatePendingSystemUpdateAuthority: async (tx, update) =>
174
- await validateIncidentTelemetrySystemUpdateAuthority({
175
- db: tx,
176
- settings,
177
- workspaceId: input.workspaceId,
178
- sessionId: input.sessionId,
179
- update,
180
- }),
183
+ validatePendingSystemUpdateAuthority,
181
184
  });
182
185
  if (claim.action === "unclaimed") {
183
186
  control.activityStatus = "unclaimed";
@@ -191,6 +194,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
191
194
  attempt.redispatchesAtDispatch = Number(
192
195
  (turn.metadata as { workerDeathRedispatches?: number } | null)?.workerDeathRedispatches ?? 0,
193
196
  );
197
+ const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
194
198
  // Establish durable attempt ownership before any later read can fail.
195
199
  // Therefore every failure with no turnId came from the one atomic claim
196
200
  // transaction and can be classified without conflating ordinary runtime
@@ -202,7 +206,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
202
206
  const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
203
207
  db,
204
208
  input.workspaceId,
205
- settings,
209
+ deploymentCatalogSettings,
206
210
  {
207
211
  ...(credentialSubjectId
208
212
  ? { subjectId: credentialSubjectId }
@@ -231,16 +235,24 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
231
235
  const xaiSettings = codexSettings.supergrokSubscriptionEnabled
232
236
  ? withXaiSubscriptionProvider(codexSettings)
233
237
  : codexSettings;
234
- const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
238
+ const gatewaySettings = await settingsWithWorkspaceGatewayCredential(
235
239
  db,
240
+ input.accountId,
236
241
  input.workspaceId,
237
242
  xaiSettings,
243
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
244
+ );
245
+ const capabilitySettings = await settingsWithWorkspaceOpenRouterCredential(
246
+ db,
247
+ input.accountId,
248
+ input.workspaceId,
249
+ gatewaySettings,
250
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
238
251
  );
239
252
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
240
253
  ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
241
254
  : null;
242
255
  runtime.configure(capabilitySettings);
243
- const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
244
256
  const policyForAbsent =
245
257
  claimedPolicy.kind === "valid"
246
258
  ? claimedPolicy.policy
@@ -270,6 +282,8 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
270
282
  assertSessionAllowsProductModel(session, turnExecutionPolicy.productModelId);
271
283
  const billingIdentity = turnExecutionPolicyBillingIdentity(turnExecutionPolicy);
272
284
  billingState.isExternallyBilledTurn = billingIdentity.externallyBilled;
285
+ billingState.chargesOpenGeniCredits = verifiedExecutionPolicy.model.cost === "credits";
286
+ billingState.countsTowardTokenCap = billingIdentity.countsTowardTokenCap;
273
287
  billingState.isCodexTurn = billingIdentity.codexSubscription;
274
288
  billingState.isXaiTurn = billingIdentity.xaiSubscription;
275
289
  const trigger = await getSessionEvent(db, input.workspaceId, attempt.triggerEventId);
@@ -289,6 +303,19 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
289
303
  trigger,
290
304
  );
291
305
  attempt.triggerType = trigger.type;
306
+ const attachPendingUpdatesAfterOpenSuffix = async (): Promise<boolean> => {
307
+ const attached = await claimSessionWorkForAttempt(db, input.workspaceId, {
308
+ sessionId: input.sessionId,
309
+ workflowId: input.workflowId,
310
+ workflowRunId: input.workflowRunId,
311
+ attemptId: input.attemptId,
312
+ dispatchId,
313
+ trigger: input.trigger,
314
+ validatePendingSystemUpdateAuthority,
315
+ attachPendingUpdatesToRunningAttempt: true,
316
+ });
317
+ return attached.action === "claimed" && attached.turn.id === turn.id;
318
+ };
292
319
  turnLifecycleMetricsFor(observability).start(attempt.turnId);
293
320
  // §7.5 P3 — pass the accepted billing attribution (externally funded turns
294
321
  // bypass OpenGeni credit/token gates)
@@ -296,12 +323,14 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
296
323
  // the local credit read). Unset port → today's local-ledger path.
297
324
  await waitForTurnOperation(
298
325
  ensureRunAllowed(
299
- settings,
326
+ capabilitySettings,
300
327
  db,
301
328
  input.accountId,
302
329
  input.workspaceId,
303
330
  billingState.isExternallyBilledTurn,
304
331
  entitlements,
332
+ billingState.chargesOpenGeniCredits,
333
+ billingState.countsTowardTokenCap,
305
334
  ),
306
335
  cancellationSignal,
307
336
  undefined,
@@ -380,6 +409,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
380
409
  recordSessionEventAppendLatency(observability, {
381
410
  durationSeconds,
382
411
  }),
412
+ onAppendPhase: (observation) => recordSessionEventAppendPhase(observability, observation),
383
413
  onPublish: ({ durationSeconds }) =>
384
414
  recordSessionEventPublishLatency(observability, {
385
415
  durationSeconds,
@@ -404,36 +434,6 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
404
434
  return appended;
405
435
  };
406
436
  eventing.settle = async (inputSettlement) => {
407
- const attemptClosing = ["completed", "failed", "cancelled", "requires_action"].includes(
408
- inputSettlement.turnStatus,
409
- );
410
- const recordingForSettlement =
411
- attemptClosing && recordingState.activeRecording && sandboxState.resolvedSandbox
412
- ? (recordingState.activeRecording as ActiveRecording)
413
- : null;
414
- const preparedRecording = recordingForSettlement
415
- ? await prepareRecordingForSettlement({
416
- settings,
417
- objectStorage,
418
- workspaceId: input.workspaceId,
419
- sessionId: input.sessionId,
420
- active: recordingForSettlement,
421
- session: sandboxState.resolvedSandbox!.established.session,
422
- didComputerUse: recordingState.didComputerUse,
423
- })
424
- : null;
425
- let recordingMutation: SessionTurnRecordingSettlement | undefined;
426
- if (preparedRecording) {
427
- const mutation = preparedRecording.mutation;
428
- recordingMutation =
429
- mutation.action === "discard"
430
- ? mutation
431
- : {
432
- ...mutation,
433
- producerId,
434
- producerSeq: ++producerSeq,
435
- };
436
- }
437
437
  const compactionRequestFailure = inputSettlement.consumeRequestedCompactionFailure
438
438
  ? {
439
439
  reason: "summarization_failed" as const,
@@ -466,7 +466,6 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
466
466
  activeTurnId: inputSettlement.activeTurnId,
467
467
  events: inputs,
468
468
  ...(runState ? { runState } : {}),
469
- ...(recordingMutation ? { recording: recordingMutation } : {}),
470
469
  ...(compactionRequestFailure ? { compactionRequestFailure } : {}),
471
470
  });
472
471
  if (result.action === "stale") {
@@ -478,31 +477,12 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
478
477
  // TurnAttemptFencedError. If ownership was lost for an unrelated
479
478
  // reason, allowUninterrupted makes the receipt transaction a no-op.
480
479
  acknowledgeLostAttemptOwnership();
481
- if (recordingForSettlement) {
482
- await abandonActiveRecording(
483
- "recording settlement lost attempt ownership",
484
- preparedRecording?.mutation.action === "discard" ? "discard" : "failed",
485
- );
486
- }
487
480
  control.activityStatus = "cancelled";
488
481
  control.turnMetricOutcome = "cancelled";
489
482
  return false;
490
483
  }
491
484
  recordCanonicalStartupMilestones(result.canonicalStartupMilestones);
492
485
  turnLifecycleMetricsFor(observability).progress(attempt.turnId!);
493
- if (recordingForSettlement && preparedRecording) {
494
- if (result.recordingMutationApplied) {
495
- recordingState.activeRecording = null;
496
- if (preparedRecording.deleteArtifactsAfterCommit) {
497
- await deleteRecordingArtifacts(
498
- sandboxState.resolvedSandbox!.established.session,
499
- recordingForSettlement.proc,
500
- );
501
- }
502
- } else {
503
- await abandonActiveRecording("recording row was unavailable during turn settlement");
504
- }
505
- }
506
486
  await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, result.events);
507
487
  if (inputSettlement.turnStatus === "requires_action") {
508
488
  // The settlement transaction committed the parent's child_requires_action
@@ -579,6 +559,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
579
559
  trigger,
580
560
  humanInputResume,
581
561
  interactionInterventionResume,
562
+ attachPendingUpdatesAfterOpenSuffix,
582
563
  throwIfWorkerShuttingDown,
583
564
  throwIfTurnCancelled,
584
565
  opJournal,