@opengeni/worker-bundle 0.15.1 → 0.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,5 @@
1
- export type DocumentIndexWorkflowInput = {
2
- accountId: string;
3
- workspaceId: string;
4
- documentId: string;
5
- };
1
+ import type { IndexDocumentInput } from "../activities/types";
2
+ export type DocumentIndexWorkflowInput = IndexDocumentInput;
6
3
  export declare function documentIndexWorkflow(input: DocumentIndexWorkflowInput): Promise<{
7
4
  id: string;
8
5
  workspaceId: string;
@@ -22,6 +19,9 @@ export declare function documentIndexWorkflow(input: DocumentIndexWorkflowInput)
22
19
  sourceUpdatedAt: string | null;
23
20
  sourceVersion: string | null;
24
21
  aclTags: string[];
22
+ authorityKind: "organization" | "personal" | "workspace";
23
+ authorityWorkspaceId: string | null;
24
+ authoritySubjectId: string | null;
25
25
  visibility: "private" | "workspace";
26
26
  createdBy: string | null;
27
27
  agentAccess: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.15.1",
3
+ "version": "0.16.3",
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": {
@@ -40,17 +40,17 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@opengeni/agent-proto": "^0.3.0",
43
- "@opengeni/codex": "^0.2.9",
44
- "@opengeni/config": "^0.10.1",
45
- "@opengeni/contracts": "^0.31.1",
46
- "@opengeni/core": "^0.18.1",
47
- "@opengeni/db": "^0.22.2",
48
- "@opengeni/documents": "^0.2.70",
49
- "@opengeni/events": "^0.3.60",
50
- "@opengeni/github": "^0.4.17",
51
- "@opengeni/observability": "^0.4.4",
52
- "@opengeni/runtime": "^0.17.1",
53
- "@opengeni/storage": "^0.2.54",
43
+ "@opengeni/codex": "^0.2.10",
44
+ "@opengeni/config": "^0.10.6",
45
+ "@opengeni/contracts": "^0.35.0",
46
+ "@opengeni/core": "^0.20.2",
47
+ "@opengeni/db": "^0.26.0",
48
+ "@opengeni/documents": "^0.3.2",
49
+ "@opengeni/events": "^0.3.65",
50
+ "@opengeni/github": "^0.4.22",
51
+ "@opengeni/observability": "^0.4.9",
52
+ "@opengeni/runtime": "^0.18.3",
53
+ "@opengeni/storage": "^0.2.59",
54
54
  "@temporalio/activity": "^1.17.0",
55
55
  "@temporalio/client": "^1.17.0",
56
56
  "@temporalio/worker": "^1.17.0",
@@ -70,6 +70,9 @@ import {
70
70
  getEnrollment,
71
71
  abandonRecordingForTurnAttempt,
72
72
  markSessionAttemptQuiesced,
73
+ getOrCreatePreferenceRegistrySnapshot,
74
+ getOrCreateWorkspaceInstructionPolicySnapshot,
75
+ PreferenceRegistryInitiatorError,
73
76
  beginConnectorActionExecution,
74
77
  completeConnectorActionExecution,
75
78
  prepareConnectorActionApproval,
@@ -91,10 +94,14 @@ import {
91
94
  normalizeModelCallUsage,
92
95
  normalizeSdkEvent,
93
96
  sanitizeHistoryItemsForModel,
97
+ projectModelInputForCapabilities,
94
98
  appendPersistentSessionSettings,
95
99
  appendSessionInstructions,
100
+ appendWorkspaceGovernance,
96
101
  appendWorkspaceMemory,
97
102
  composeAgentInstructions,
103
+ hasActiveWorkspaceInstructionPolicy,
104
+ renderWorkspaceGovernanceContext,
98
105
  summarizeForCompaction,
99
106
  requestRemoteCompactionV2,
100
107
  serializedToolsForRemoteCompaction,
@@ -142,6 +149,7 @@ import {
142
149
  import { connectionTokenResolverForTurn } from "./mcp-credentials";
143
150
  import {
144
151
  assertTurnExecutionPolicyMatchesConfigV1,
152
+ calculateGatewayReportedCostMicros,
145
153
  calculateModelUsageCostMicros,
146
154
  configuredModelPricing,
147
155
  configuredStaticUsageLimits,
@@ -151,6 +159,8 @@ import {
151
159
  serviceTierForLatencyMode,
152
160
  settingsWithResolvedModelContext,
153
161
  resolveTurnExecutionPolicyV1,
162
+ OPENGENI_GATEWAY_MODELS,
163
+ OPENGENI_GATEWAY_PROVIDER_ID,
154
164
  type ModelUsageInput,
155
165
  type ModelProviderApi,
156
166
  type RegistryProviderKind,
@@ -201,6 +211,7 @@ import {
201
211
  codexRequestStorage,
202
212
  isCodexTransportError,
203
213
  withCodexRequestOverrides,
214
+ selectCodexCredentialId,
204
215
  type CodexRequestContext,
205
216
  type CodexUsageHeaderSnapshot,
206
217
  } from "@opengeni/codex";
@@ -261,7 +272,13 @@ import {
261
272
  redactSensitiveText,
262
273
  type SecretForRedaction,
263
274
  } from "./redaction";
264
- import { applyCodexHistoryStrip, turnInput, type TurnCodexAccount } from "./run-input";
275
+ import {
276
+ applyCodexHistoryStrip,
277
+ createModelHistoryAttachmentProjector,
278
+ turnInput,
279
+ type ModelAttachmentInputPolicy,
280
+ type TurnCodexAccount,
281
+ } from "./run-input";
265
282
  import {
266
283
  createRuntimeBatcher,
267
284
  currentActivityContext,
@@ -328,6 +345,7 @@ import {
328
345
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
329
346
  evaluateWorkspaceModelPolicy,
330
347
  readTurnExecutionPolicyV1,
348
+ resourceMountPath,
331
349
  type LatencyMode,
332
350
  type ResourceRef,
333
351
  type SessionEvent,
@@ -499,14 +517,7 @@ export function selectCodexCredentialForTurn(args: {
499
517
  activeCredentialId: string | null;
500
518
  connectedIds: Set<string>;
501
519
  }): string | null {
502
- const { sessionPinnedCredentialId: pin, activeCredentialId: active, connectedIds } = args;
503
- if (pin && connectedIds.has(pin)) {
504
- return pin;
505
- }
506
- if (active && connectedIds.has(active)) {
507
- return active;
508
- }
509
- return null;
520
+ return selectCodexCredentialId(args);
510
521
  }
511
522
 
512
523
  export function filterUnmaterializedSandboxFileDownloads(
@@ -1186,6 +1197,8 @@ export async function processModelResponseUsageEvent(input: {
1186
1197
  externallyBilled: input.externallyBilled,
1187
1198
  usage: responseUsage.usage,
1188
1199
  normalizedUsage,
1200
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
1201
+ gatewayBilling: responseUsage.gatewayBilling,
1189
1202
  sourceKey,
1190
1203
  ...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
1191
1204
  observability: input.observability,
@@ -1312,6 +1325,8 @@ export async function processCompactionModelUsageEvent(input: {
1312
1325
  externallyBilled: input.externallyBilled,
1313
1326
  usage: input.usage.usage,
1314
1327
  normalizedUsage,
1328
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
1329
+ gatewayBilling: input.usage.gatewayBilling,
1315
1330
  sourceKey,
1316
1331
  observability: input.observability,
1317
1332
  });
@@ -1863,11 +1878,15 @@ export function shouldStartOnTurnRecording(params: {
1863
1878
  export function computerToolModeForTurn(
1864
1879
  resolvedModel: {
1865
1880
  provider: { kind: RegistryProviderKind; api: ModelProviderApi };
1881
+ configured: { capabilities: { inputModalities: string[] } };
1866
1882
  } | null,
1867
1883
  ): ComputerToolMode {
1868
1884
  if (!resolvedModel) {
1869
1885
  return "hosted"; // legacy built-in Responses client — real hosted support
1870
1886
  }
1887
+ if (!modelSupportsImageInputForTurn(resolvedModel)) {
1888
+ return "disabled";
1889
+ }
1871
1890
  if (resolvedModel.provider.kind === "codex-subscription") {
1872
1891
  return "function-image";
1873
1892
  }
@@ -1907,17 +1926,43 @@ export function hostedWebSearchForTurn(
1907
1926
  return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
1908
1927
  }
1909
1928
 
1910
- /**
1911
- * Decide whether this turn's provider accepts typed file/image content in a
1912
- * user message. The legacy built-in client and registry Responses clients use
1913
- * the Agents SDK Responses converter, which supports `input_image` and
1914
- * `input_file`. Chat-completions providers retain the sandbox-path projection
1915
- * until that transport declares and proves an equivalent capability.
1916
- */
1917
- export function modelAcceptsTypedAttachmentContentForTurn(
1918
- resolvedModel: { provider: { api: ModelProviderApi } } | null,
1929
+ /** Exact model-catalog modality gate; null preserves the legacy built-in path. */
1930
+ export function modelSupportsImageInputForTurn(
1931
+ resolvedModel: {
1932
+ configured: { capabilities?: { inputModalities: string[] } };
1933
+ } | null,
1919
1934
  ): boolean {
1920
- return resolvedModel === null || resolvedModel.provider.api === "responses";
1935
+ return (
1936
+ resolvedModel === null ||
1937
+ resolvedModel.configured.capabilities?.inputModalities.includes("image") !== false
1938
+ );
1939
+ }
1940
+
1941
+ const LEGACY_INPUT_FILE_MEDIA_TYPES = [
1942
+ "application/json",
1943
+ "application/pdf",
1944
+ "application/x-yaml",
1945
+ "application/yaml",
1946
+ "text/*",
1947
+ ] as const;
1948
+
1949
+ /** Image and file input are independent catalogue capabilities. */
1950
+ export function modelAttachmentInputPolicyForTurn(
1951
+ resolvedModel: {
1952
+ provider: { api: ModelProviderApi };
1953
+ configured: {
1954
+ capabilities?: { inputModalities: string[]; inputFileMediaTypes?: string[] };
1955
+ };
1956
+ } | null,
1957
+ ): ModelAttachmentInputPolicy {
1958
+ const typedTransport = resolvedModel === null || resolvedModel.provider.api === "responses";
1959
+ return {
1960
+ supportsImageInput: typedTransport && modelSupportsImageInputForTurn(resolvedModel),
1961
+ inputFileMediaTypes: typedTransport
1962
+ ? (resolvedModel?.configured.capabilities?.inputFileMediaTypes ??
1963
+ LEGACY_INPUT_FILE_MEDIA_TYPES)
1964
+ : [],
1965
+ };
1921
1966
  }
1922
1967
 
1923
1968
  export type TurnSandboxProvisioner<T> = {
@@ -4053,10 +4098,29 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4053
4098
  // deployment default). null means the workspace has no override, so the
4054
4099
  // runtime falls back to runSettings.agentInstructionsTemplate (the
4055
4100
  // deployment default, byte-identical to the historical preamble).
4056
- const workspaceAgentInstructions = await resolveWorkspaceAgentInstructions(
4057
- db,
4058
- input.workspaceId,
4059
- );
4101
+ const governanceClaims = {
4102
+ accountId: input.accountId,
4103
+ workspaceId: input.workspaceId,
4104
+ sessionId: input.sessionId,
4105
+ turnId: turn.id,
4106
+ attemptId: input.attemptId,
4107
+ executionGeneration: turn.executionGeneration,
4108
+ };
4109
+ const [workspaceAgentInstructions, instructionPolicySnapshot, preferenceSnapshot] =
4110
+ await Promise.all([
4111
+ resolveWorkspaceAgentInstructions(db, input.workspaceId),
4112
+ getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
4113
+ getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
4114
+ if (error instanceof PreferenceRegistryInitiatorError) return null;
4115
+ throw error;
4116
+ }),
4117
+ ]);
4118
+ const workspaceGovernance = renderWorkspaceGovernanceContext({
4119
+ instructionPolicy: instructionPolicySnapshot,
4120
+ preferences: preferenceSnapshot,
4121
+ });
4122
+ const structuredWorkspacePolicyActive =
4123
+ hasActiveWorkspaceInstructionPolicy(instructionPolicySnapshot);
4060
4124
  const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
4061
4125
  const baseRunSettings = {
4062
4126
  // IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
@@ -4119,6 +4183,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4119
4183
  capabilitySettings,
4120
4184
  turnExecutionPolicy.productModelId,
4121
4185
  );
4186
+ const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
4187
+ const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
4188
+ const attachmentProjector = createModelHistoryAttachmentProjector(
4189
+ db,
4190
+ input.workspaceId,
4191
+ modelInputPolicy,
4192
+ objectStorage ? (file) => objectStorage.getFileBytes(file) : undefined,
4193
+ );
4194
+ const modelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4195
+ projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
4122
4196
  const selectedProvider = resolvedModel?.provider;
4123
4197
  const publicProviderHeaders = new Set(
4124
4198
  selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? [],
@@ -4431,19 +4505,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4431
4505
  const persistentSessionSettings = {
4432
4506
  titleIsSet: Boolean(session.title?.trim()),
4433
4507
  };
4434
- const compactionInstructions = appendPersistentSessionSettings(
4435
- appendSessionInstructions(
4436
- appendWorkspaceMemory(
4437
- composeAgentInstructions(
4438
- workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate,
4439
- undefined,
4440
- rigVersion && rigName ? { name: rigName, version: rigVersion.version } : undefined,
4508
+ const compactionInstructions = appendWorkspaceMemory(
4509
+ appendPersistentSessionSettings(
4510
+ appendSessionInstructions(
4511
+ appendWorkspaceGovernance(
4512
+ composeAgentInstructions(
4513
+ structuredWorkspacePolicyActive
4514
+ ? modelRunSettings.agentInstructionsTemplate
4515
+ : (workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate),
4516
+ undefined,
4517
+ rigVersion && rigName
4518
+ ? { name: rigName, version: rigVersion.version }
4519
+ : undefined,
4520
+ ),
4521
+ workspaceGovernance ?? undefined,
4441
4522
  ),
4442
- workspaceMemory ?? undefined,
4523
+ session.instructions ?? undefined,
4443
4524
  ),
4444
- session.instructions ?? undefined,
4525
+ persistentSessionSettings,
4445
4526
  ),
4446
- persistentSessionSettings,
4527
+ workspaceMemory ?? undefined,
4447
4528
  );
4448
4529
  const requested = await isSessionCompactionRequested(
4449
4530
  db,
@@ -4474,6 +4555,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4474
4555
  codexAccount: {
4475
4556
  currentCodexCredentialId: effectiveCodexCredentialId,
4476
4557
  },
4558
+ projectModelInput: modelHistoryProjector,
4477
4559
  ...compactionModeOptions,
4478
4560
  },
4479
4561
  ),
@@ -5437,6 +5519,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5437
5519
  objectStorage,
5438
5520
  input.workspaceId,
5439
5521
  turnResources,
5522
+ activeSandboxBackend ?? groupBoxBackend,
5440
5523
  ),
5441
5524
  cancellationSignal,
5442
5525
  undefined,
@@ -5655,6 +5738,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5655
5738
  // session's MCP allow-list; the effective context window drives the
5656
5739
  // compaction threshold.
5657
5740
  hostedWebSearch,
5741
+ supportsImageInput,
5742
+ inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
5658
5743
  ...(resolvedModel
5659
5744
  ? {
5660
5745
  encryptedReasoning:
@@ -5708,7 +5793,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5708
5793
  skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections,
5709
5794
  }
5710
5795
  : {}),
5711
- ...(workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {}),
5796
+ ...(!structuredWorkspacePolicyActive && workspaceAgentInstructions
5797
+ ? { instructionsTemplate: workspaceAgentInstructions }
5798
+ : {}),
5799
+ ...(workspaceGovernance ? { workspaceGovernance } : {}),
5712
5800
  ...(workspaceMemory ? { workspaceMemory } : {}),
5713
5801
  // Per-session persona tier (session > workspace > deployment default).
5714
5802
  // Composed system-level AFTER the workspace persona so it refines it for
@@ -5998,6 +6086,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5998
6086
  codexAccount: {
5999
6087
  currentCodexCredentialId: effectiveCodexCredentialId,
6000
6088
  },
6089
+ projectModelInput: modelHistoryProjector,
6001
6090
  ...compactionModeOptions,
6002
6091
  },
6003
6092
  ),
@@ -6132,6 +6221,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6132
6221
  codexAccount: {
6133
6222
  currentCodexCredentialId: effectiveCodexCredentialId,
6134
6223
  },
6224
+ projectModelInput: modelHistoryProjector,
6135
6225
  ...compactionModeOptions,
6136
6226
  }
6137
6227
  : {
@@ -6139,6 +6229,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6139
6229
  codexAccount: {
6140
6230
  currentCodexCredentialId: effectiveCodexCredentialId,
6141
6231
  },
6232
+ projectModelInput: modelHistoryProjector,
6142
6233
  ...compactionModeOptions,
6143
6234
  },
6144
6235
  ),
@@ -6210,20 +6301,22 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6210
6301
  }
6211
6302
  let fileMaterializationFailures: SandboxFileDownloadFailure[] = [];
6212
6303
  let fileDownloadsMaterializedForRun = false;
6213
- if (
6214
- resolvedSandbox &&
6215
- setupBoxSession &&
6216
- activeSandboxBackend !== "selfhosted" &&
6217
- fileResourceDownloads.length > 0
6218
- ) {
6304
+ if (resolvedSandbox && setupBoxSession && fileResourceDownloads.length > 0) {
6219
6305
  const boxInstanceId = resolvedSandbox.established.instanceId;
6220
- const alreadyMaterialized = await getMaterializedSandboxFileResources(db, {
6221
- accountId: input.accountId,
6222
- workspaceId: input.workspaceId,
6223
- sandboxGroupId: session.sandboxGroupId,
6224
- expectedEpoch: resolvedSandbox.leaseEpoch,
6225
- instanceId: boxInstanceId,
6226
- });
6306
+ // Managed boxes are immutable platform state, so their durable lease can
6307
+ // memoize successful downloads. A connected machine is user-owned: files
6308
+ // can be changed or removed between turns, so verify/materialize every
6309
+ // attached file each turn instead of trusting the managed-box cache.
6310
+ const cacheMaterialization = resolvedSandbox.established.backendId !== "selfhosted";
6311
+ const alreadyMaterialized = cacheMaterialization
6312
+ ? await getMaterializedSandboxFileResources(db, {
6313
+ accountId: input.accountId,
6314
+ workspaceId: input.workspaceId,
6315
+ sandboxGroupId: session.sandboxGroupId,
6316
+ expectedEpoch: resolvedSandbox.leaseEpoch,
6317
+ instanceId: boxInstanceId,
6318
+ })
6319
+ : new Set<string>();
6227
6320
  const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
6228
6321
  fileResourceDownloads,
6229
6322
  alreadyMaterialized,
@@ -6257,7 +6350,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6257
6350
  const succeededFileIds = downloadsToMaterialize
6258
6351
  .map((download) => download.fileId)
6259
6352
  .filter((fileId) => !failedFileIds.has(fileId));
6260
- if (succeededFileIds.length > 0) {
6353
+ if (cacheMaterialization && succeededFileIds.length > 0) {
6261
6354
  await markSandboxFileResourcesMaterialized(db, {
6262
6355
  accountId: input.accountId,
6263
6356
  workspaceId: input.workspaceId,
@@ -6298,11 +6391,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6298
6391
  recovering: turn.executionGeneration > 1,
6299
6392
  ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
6300
6393
  ...(runCredentialsNote ? { runCredentialsNote } : {}),
6301
- ...(modelAcceptsTypedAttachmentContentForTurn(resolvedModel) && objectStorage
6302
- ? {
6303
- readFileBytesForModel: (file) => objectStorage.getFileBytes(file),
6304
- }
6305
- : {}),
6394
+ projectModelHistory: modelHistoryProjector,
6306
6395
  },
6307
6396
  );
6308
6397
  runInput = prepared.input;
@@ -6376,6 +6465,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6376
6465
  codexAccount: {
6377
6466
  currentCodexCredentialId: effectiveCodexCredentialId,
6378
6467
  },
6468
+ projectModelInput: modelHistoryProjector,
6379
6469
  ...compactionModeOptions,
6380
6470
  },
6381
6471
  ),
@@ -9211,6 +9301,7 @@ export type ModelUsageBillingRecord = {
9211
9301
  /** Same quantity written to usage_events.model.cost when present; else 0. */
9212
9302
  pricedCostMicros: number;
9213
9303
  normalizedUsage: ModelCallUsageNormalization;
9304
+ upstreamProvider?: string;
9214
9305
  };
9215
9306
 
9216
9307
  // Exported for unit testing the external-billing bypass; not part of the activity surface.
@@ -9225,6 +9316,8 @@ export async function recordModelUsageAndDebitCredits(
9225
9316
  turnAttemptId: string;
9226
9317
  model: string;
9227
9318
  externallyBilled: boolean;
9319
+ gatewayManaged?: boolean;
9320
+ gatewayBilling?: ModelResponseUsage["gatewayBilling"];
9228
9321
  usage?: ModelUsageInput | ModelCallUsageInput | null;
9229
9322
  normalizedUsage?: ModelCallUsageNormalization;
9230
9323
  sourceKey: string;
@@ -9240,6 +9333,20 @@ export async function recordModelUsageAndDebitCredits(
9240
9333
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
9241
9334
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
9242
9335
  const totalTokens = sanitizedUsage.totalTokens ?? 0;
9336
+ const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : undefined;
9337
+ if (gatewayBilling) {
9338
+ const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
9339
+ (candidate) => candidate.productId === input.model,
9340
+ );
9341
+ if (
9342
+ !gatewayModel ||
9343
+ !(gatewayModel.providers as readonly string[]).includes(gatewayBilling.finalProvider)
9344
+ ) {
9345
+ throw new Error(
9346
+ `AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`,
9347
+ );
9348
+ }
9349
+ }
9243
9350
  // An externally billed turn is paid outside OpenGeni, so it
9244
9351
  // consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
9245
9352
  // codex/<slug> model has no entry in configuredModelPricing, so the normal path
@@ -9285,19 +9392,24 @@ export async function recordModelUsageAndDebitCredits(
9285
9392
  });
9286
9393
  }
9287
9394
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
9288
- if (!shouldDebit || totalTokens === 0) {
9395
+ if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
9289
9396
  return {
9290
9397
  billingPath: "opengeni_credits",
9291
9398
  pricedCostMicros: 0,
9292
9399
  normalizedUsage,
9400
+ ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
9293
9401
  };
9294
9402
  }
9295
9403
  if (!configuredModelPricing(settings)[input.model]) {
9296
9404
  throw new Error(`Missing model pricing for ${input.model}`);
9297
9405
  }
9298
- const costMicros = calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
9299
- latencyMode: input.latencyMode ?? "standard",
9300
- });
9406
+ const costMicros = gatewayBilling
9407
+ ? calculateGatewayReportedCostMicros(settings, input.model, gatewayBilling.inferenceCostUsd, {
9408
+ inputTokens,
9409
+ })
9410
+ : calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
9411
+ latencyMode: input.latencyMode ?? "standard",
9412
+ });
9301
9413
  await recordUsageEvent(db, {
9302
9414
  accountId: input.accountId,
9303
9415
  workspaceId: input.workspaceId,
@@ -9333,6 +9445,7 @@ export async function recordModelUsageAndDebitCredits(
9333
9445
  // per-call debit record carries cache efficiency alongside the token
9334
9446
  // counts. 0 when the provider did not report cached tokens.
9335
9447
  cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
9448
+ ...(gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}),
9336
9449
  },
9337
9450
  });
9338
9451
  recordCreditMicros(input.observability, "usage", result.debitedMicros);
@@ -9341,6 +9454,7 @@ export async function recordModelUsageAndDebitCredits(
9341
9454
  billingPath: "opengeni_credits",
9342
9455
  pricedCostMicros: costMicros,
9343
9456
  normalizedUsage,
9457
+ ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
9344
9458
  };
9345
9459
  }
9346
9460
 
@@ -9368,7 +9482,7 @@ export async function recordAuthoritativeModelCallFact(input: {
9368
9482
  turnId: input.turnId,
9369
9483
  turnAttemptId: input.turnAttemptId,
9370
9484
  sourceKey: input.sourceKey,
9371
- provider: input.provider,
9485
+ provider: input.billing.upstreamProvider ?? input.provider,
9372
9486
  providerApi: input.providerApi,
9373
9487
  model: input.model,
9374
9488
  billingPath: input.billing.billingPath,
@@ -9422,8 +9536,9 @@ async function sandboxFileDownloadsForRun(
9422
9536
  objectStorage: ObjectStorage | null,
9423
9537
  workspaceId: string,
9424
9538
  resources: ResourceRef[],
9539
+ activeSandboxBackend: Settings["sandboxBackend"] = settings.sandboxBackend,
9425
9540
  ): Promise<SandboxFileDownload[]> {
9426
- if (settings.sandboxBackend === "none" || !requiresSignedFileResourceDownloads(settings)) {
9541
+ if (!requiresSignedFileResourceDownloads(settings, activeSandboxBackend)) {
9427
9542
  return [];
9428
9543
  }
9429
9544
  const fileResources = resources.filter(
@@ -9444,17 +9559,24 @@ async function sandboxFileDownloadsForRun(
9444
9559
  const url = await downloadStorage.createGetUrl({ key: file.objectKey });
9445
9560
  downloads.push({
9446
9561
  fileId: file.id,
9447
- mountPath: resource.mountPath ?? `files/${file.id}`,
9562
+ mountPath: resourceMountPath(resource),
9448
9563
  filename: file.safeFilename,
9449
9564
  url: url.url,
9450
9565
  expiresAt: url.expiresAt,
9451
9566
  sizeBytes: file.sizeBytes,
9567
+ ...(file.sha256 ? { sha256: file.sha256 } : {}),
9452
9568
  });
9453
9569
  }
9454
9570
  return downloads;
9455
9571
  }
9456
9572
 
9457
- function requiresSignedFileResourceDownloads(settings: Settings): boolean {
9573
+ export function requiresSignedFileResourceDownloads(
9574
+ settings: Settings,
9575
+ activeSandboxBackend: Settings["sandboxBackend"] = settings.sandboxBackend,
9576
+ ): boolean {
9577
+ if (activeSandboxBackend === "none") {
9578
+ return false;
9579
+ }
9458
9580
  // A selfhosted machine (bring-your-own-compute) can NEVER mount ANY object store
9459
9581
  // — it is a remote user machine reached only over NATS, so file resources are
9460
9582
  // ALWAYS delivered by exec-curling a pre-signed URL onto it. Without this a
@@ -9462,15 +9584,15 @@ function requiresSignedFileResourceDownloads(settings: Settings): boolean {
9462
9584
  // resources on an azure-blob / s3-compatible store (nativeBucketMount=false),
9463
9585
  // a regression from the pre-honest-label path where the same turn ran home=modal
9464
9586
  // and modal's descriptor forced signed downloads.
9465
- if (settings.sandboxBackend === "selfhosted") {
9587
+ if (activeSandboxBackend === "selfhosted") {
9466
9588
  return true;
9467
9589
  }
9468
9590
  // A nativeBucketMount backend (modal) cannot mount Azure Blob entries, so it
9469
9591
  // needs pre-signed downloads for that store. Keying on the descriptor (not the
9470
9592
  // "modal" literal) keeps this correct as bucket-mount backends are added.
9471
- const nativeBucketMount = CAPABILITY_DESCRIPTORS[settings.sandboxBackend].nativeBucketMount;
9593
+ const nativeBucketMount = CAPABILITY_DESCRIPTORS[activeSandboxBackend].nativeBucketMount;
9472
9594
  return (
9473
- (settings.sandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible") ||
9595
+ (activeSandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible") ||
9474
9596
  settings.objectStorageBackend === "aws-s3" ||
9475
9597
  settings.objectStorageBackend === "gcs" ||
9476
9598
  (nativeBucketMount && settings.objectStorageBackend === "azure-blob")