@opengeni/worker-bundle 0.23.3-canary.0 → 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 (62) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +10 -2
  2. package/dist/activities/agent-turn/claim.d.ts +1 -0
  3. package/dist/activities/agent-turn/codex.d.ts +2 -2
  4. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  5. package/dist/activities/agent-turn/model-usage.d.ts +10 -1
  6. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  7. package/dist/activities/agent-turn/tool-policy.d.ts +36 -5
  8. package/dist/activities/agent-turn/turn-context.d.ts +2 -0
  9. package/dist/activities/automations.d.ts +1 -0
  10. package/dist/activities/capabilities.d.ts +2 -1
  11. package/dist/activities/goals.d.ts +11 -0
  12. package/dist/activities/image-generation-references.d.ts +2 -1
  13. package/dist/activities/scheduled-tasks.d.ts +1 -0
  14. package/dist/activities/types.d.ts +8 -0
  15. package/dist/{activities-control-52W5IDWM.js → activities-control-SGU2WEVB.js} +206 -97
  16. package/dist/activities-control-SGU2WEVB.js.map +1 -0
  17. package/dist/activities-control.d.ts +2 -0
  18. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-7EAQXW6Q.js} +301 -65
  19. package/dist/activities-turn-7EAQXW6Q.js.map +1 -0
  20. package/dist/activities.d.ts +2 -0
  21. package/dist/{chunk-IPVMCAW5.js → chunk-S2QVRVJW.js} +85 -23
  22. package/dist/chunk-S2QVRVJW.js.map +1 -0
  23. package/dist/{chunk-GFNXVZVQ.js → chunk-TJIPCHMP.js} +3 -2
  24. package/dist/chunk-TJIPCHMP.js.map +1 -0
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +29 -6
  27. package/dist/index.js.map +1 -1
  28. package/dist/observability-metrics.d.ts +1 -0
  29. package/dist/workflow-bundle.js +26 -3
  30. package/dist/workflows/activities.d.ts +5 -0
  31. package/dist/workflows/automations.d.ts +5 -0
  32. package/package.json +17 -17
  33. package/src/activities/agent-run-admission.ts +7 -1
  34. package/src/activities/agent-turn/admission.ts +28 -6
  35. package/src/activities/agent-turn/claim.ts +25 -5
  36. package/src/activities/agent-turn/codex.ts +2 -2
  37. package/src/activities/agent-turn/compaction-prep.ts +2 -0
  38. package/src/activities/agent-turn/list-models.ts +101 -0
  39. package/src/activities/agent-turn/model-usage.ts +88 -42
  40. package/src/activities/agent-turn/run.ts +6 -3
  41. package/src/activities/agent-turn/stream-attempt.ts +8 -0
  42. package/src/activities/agent-turn/tool-environment.ts +62 -1
  43. package/src/activities/agent-turn/tool-policy.ts +58 -5
  44. package/src/activities/agent-turn/turn-context.ts +4 -0
  45. package/src/activities/automations.ts +122 -67
  46. package/src/activities/capabilities.ts +72 -2
  47. package/src/activities/goals.ts +107 -31
  48. package/src/activities/image-generation-references.ts +9 -9
  49. package/src/activities/scheduled-tasks.ts +120 -26
  50. package/src/activities/site-auth-maintenance.ts +12 -12
  51. package/src/activities/types.ts +6 -0
  52. package/src/activities.ts +1 -0
  53. package/src/activity-services.ts +4 -1
  54. package/src/github-rest-mcp.ts +2 -1
  55. package/src/index.ts +19 -4
  56. package/src/observability-metrics.ts +2 -0
  57. package/src/workflows/activities.ts +15 -0
  58. package/src/workflows/automations.ts +21 -2
  59. package/dist/activities-control-52W5IDWM.js.map +0 -1
  60. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  61. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  62. package/dist/chunk-IPVMCAW5.js.map +0 -1
@@ -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.23.3-canary.0",
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": {
@@ -54,22 +54,22 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@llamaindex/liteparse": "2.14.2",
57
- "@opengeni/agent-proto": "^0.5.1-canary.6",
58
- "@opengeni/capabilities": "^0.3.1-canary.5",
59
- "@opengeni/codemode": "^0.4.22-canary.0",
60
- "@opengeni/codex": "^0.2.20-canary.0",
61
- "@opengeni/config": "^0.22.5-canary.0",
62
- "@opengeni/contracts": "^2.9.2-canary.0",
63
- "@opengeni/core": "^2.6.3-canary.0",
64
- "@opengeni/db": "^3.7.3-canary.0",
65
- "@opengeni/documents": "^0.8.13-canary.0",
66
- "@opengeni/events": "^0.4.11-canary.0",
67
- "@opengeni/github": "^0.6.7-canary.0",
68
- "@opengeni/network": "^0.2.3-canary.5",
69
- "@opengeni/observability": "^0.8.14-canary.0",
70
- "@opengeni/runtime": "^2.0.1-canary.0",
71
- "@opengeni/storage": "^0.2.115-canary.0",
72
- "@opengeni/xai-subscription": "^0.1.2-canary.5",
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",
@@ -24,11 +24,13 @@ import {
24
24
  settingsWithCodexCredential,
25
25
  settingsWithEnabledCapabilityMcpServers,
26
26
  settingsWithWorkspaceGatewayCredential,
27
+ settingsWithWorkspaceOpenRouterCredential,
27
28
  withXaiSubscriptionProvider,
28
29
  } from "../capabilities";
29
30
  import { validateIncidentTelemetrySystemUpdateAuthority } from "../incident-telemetry-authority";
30
31
  import {
31
32
  assertSessionAllowsProductModel,
33
+ resolveCatalogSettings,
32
34
  resolveCodexAppsCredentialIdForRun,
33
35
  } from "@opengeni/core";
34
36
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
@@ -72,6 +74,7 @@ import type {
72
74
  export type ClaimTurnDeps = {
73
75
  input: RunAgentTurnInput;
74
76
  settings: Settings;
77
+ catalogSourceSettings: Settings;
75
78
  db: ActivityServices["db"];
76
79
  bus: ActivityServices["bus"];
77
80
  runtime: ActivityServices["runtime"];
@@ -135,6 +138,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
135
138
  const {
136
139
  input,
137
140
  settings,
141
+ catalogSourceSettings,
138
142
  db,
139
143
  bus,
140
144
  runtime,
@@ -156,12 +160,15 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
156
160
  acknowledgeLostAttemptOwnership,
157
161
  } = deps;
158
162
 
163
+ const deploymentCatalogSettings = (await resolveCatalogSettings(db, catalogSourceSettings))
164
+ .settings;
165
+
159
166
  const validatePendingSystemUpdateAuthority: NonNullable<
160
167
  ClaimSessionWorkForAttemptInput["validatePendingSystemUpdateAuthority"]
161
168
  > = async (tx, update) =>
162
169
  await validateIncidentTelemetrySystemUpdateAuthority({
163
170
  db: tx,
164
- settings,
171
+ settings: deploymentCatalogSettings,
165
172
  workspaceId: input.workspaceId,
166
173
  sessionId: input.sessionId,
167
174
  update,
@@ -187,6 +194,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
187
194
  attempt.redispatchesAtDispatch = Number(
188
195
  (turn.metadata as { workerDeathRedispatches?: number } | null)?.workerDeathRedispatches ?? 0,
189
196
  );
197
+ const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
190
198
  // Establish durable attempt ownership before any later read can fail.
191
199
  // Therefore every failure with no turnId came from the one atomic claim
192
200
  // transaction and can be classified without conflating ordinary runtime
@@ -198,7 +206,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
198
206
  const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
199
207
  db,
200
208
  input.workspaceId,
201
- settings,
209
+ deploymentCatalogSettings,
202
210
  {
203
211
  ...(credentialSubjectId
204
212
  ? { subjectId: credentialSubjectId }
@@ -227,16 +235,24 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
227
235
  const xaiSettings = codexSettings.supergrokSubscriptionEnabled
228
236
  ? withXaiSubscriptionProvider(codexSettings)
229
237
  : codexSettings;
230
- const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
238
+ const gatewaySettings = await settingsWithWorkspaceGatewayCredential(
231
239
  db,
240
+ input.accountId,
232
241
  input.workspaceId,
233
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,
234
251
  );
235
252
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
236
253
  ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
237
254
  : null;
238
255
  runtime.configure(capabilitySettings);
239
- const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
240
256
  const policyForAbsent =
241
257
  claimedPolicy.kind === "valid"
242
258
  ? claimedPolicy.policy
@@ -266,6 +282,8 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
266
282
  assertSessionAllowsProductModel(session, turnExecutionPolicy.productModelId);
267
283
  const billingIdentity = turnExecutionPolicyBillingIdentity(turnExecutionPolicy);
268
284
  billingState.isExternallyBilledTurn = billingIdentity.externallyBilled;
285
+ billingState.chargesOpenGeniCredits = verifiedExecutionPolicy.model.cost === "credits";
286
+ billingState.countsTowardTokenCap = billingIdentity.countsTowardTokenCap;
269
287
  billingState.isCodexTurn = billingIdentity.codexSubscription;
270
288
  billingState.isXaiTurn = billingIdentity.xaiSubscription;
271
289
  const trigger = await getSessionEvent(db, input.workspaceId, attempt.triggerEventId);
@@ -305,12 +323,14 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
305
323
  // the local credit read). Unset port → today's local-ledger path.
306
324
  await waitForTurnOperation(
307
325
  ensureRunAllowed(
308
- settings,
326
+ capabilitySettings,
309
327
  db,
310
328
  input.accountId,
311
329
  input.workspaceId,
312
330
  billingState.isExternallyBilledTurn,
313
331
  entitlements,
332
+ billingState.chargesOpenGeniCredits,
333
+ billingState.countsTowardTokenCap,
314
334
  ),
315
335
  cancellationSignal,
316
336
  undefined,
@@ -3,7 +3,7 @@ import {
3
3
  fetchCodexUsageForAccount,
4
4
  type CodexAccountStatus,
5
5
  } from "@opengeni/db";
6
- import { type RegistryProviderKind, type Settings } from "@opengeni/config";
6
+ import { type ResolvedModelProvider, type Settings } from "@opengeni/config";
7
7
  import { codexAccountNeedsLiveCapacityRefresh } from "../codex-rotation";
8
8
  import {
9
9
  refreshCodexUsageAndRepairCapacityWaiters,
@@ -19,7 +19,7 @@ export function codexWorkspaceMetricKey(workspaceId: string): string {
19
19
  /** Stable public request identity across partial resumes and activity retries. */
20
20
  export function acceptsPromptCacheKeyForTurn(
21
21
  resolvedModel: {
22
- provider: { kind: RegistryProviderKind; builtin?: boolean };
22
+ provider: { kind: ResolvedModelProvider["kind"]; builtin?: boolean };
23
23
  } | null,
24
24
  ): boolean {
25
25
  if (!resolvedModel) {
@@ -188,6 +188,8 @@ export async function prepareCompaction(deps: CompactionPrepDeps): Promise<Compa
188
188
  providerApi: resolvedModel?.provider.api ?? "responses",
189
189
  model: resolvedModel?.configured.id ?? turn.model,
190
190
  externallyBilled: billingState.isExternallyBilledTurn,
191
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
192
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
191
193
  turnAttemptId: input.attemptId,
192
194
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
193
195
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
@@ -0,0 +1,101 @@
1
+ import type { AttemptToolDefinition } from "@opengeni/codemode";
2
+ import {
3
+ compareModelPickerOrder,
4
+ modelPickerBillingClassFor,
5
+ } from "@opengeni/contracts/model-picker-order";
6
+ import type { WorkspaceModelSelection } from "@opengeni/core";
7
+
8
+ export const LIST_MODELS_TOOL_NAME = "list_models";
9
+
10
+ export const LIST_MODELS_TOOL_DESCRIPTION =
11
+ "List the models this workspace can select right now, in picker order, with deployment-defined cost and optional guidance. This does not switch the current session model; use an ID with session_create or the human model picker.";
12
+
13
+ function safeLineField(value: string): string {
14
+ return value
15
+ .replace(/[\r\n|]/gu, " ")
16
+ .replace(/\s+/gu, " ")
17
+ .trim();
18
+ }
19
+
20
+ export function renderListModels(input: {
21
+ currentModelId: string | null;
22
+ selections: readonly WorkspaceModelSelection[];
23
+ modelNotes: Readonly<Record<string, string>>;
24
+ }): string {
25
+ const lines: string[] = [];
26
+ if (input.currentModelId) {
27
+ lines.push(`Current: ${safeLineField(input.currentModelId)}`);
28
+ }
29
+
30
+ const selectable = input.selections
31
+ .filter((entry) => entry.availability.selectable)
32
+ .map((selection) => ({
33
+ selection,
34
+ billingClass: modelPickerBillingClassFor(selection.model),
35
+ selectable: selection.availability.selectable,
36
+ label: selection.model.label,
37
+ }))
38
+ .sort(compareModelPickerOrder);
39
+ if (selectable.length === 0) {
40
+ lines.push("No models are available in this workspace.");
41
+ return lines.join("\n");
42
+ }
43
+
44
+ for (const { selection } of selectable) {
45
+ const { model } = selection;
46
+ const fields = [safeLineField(model.id), safeLineField(model.label), model.cost];
47
+ const note = input.modelNotes[model.id];
48
+ if (note) fields.push(safeLineField(note));
49
+ lines.push(`- ${fields.join(" | ")}`);
50
+ }
51
+ return lines.join("\n");
52
+ }
53
+
54
+ export function createListModelsAttemptToolDefinition(input: {
55
+ currentModelId: string | null;
56
+ load: () => Promise<{
57
+ selections: readonly WorkspaceModelSelection[];
58
+ modelNotes: Readonly<Record<string, string>>;
59
+ }>;
60
+ }): AttemptToolDefinition {
61
+ return {
62
+ identity: { serverId: "opengeni", toolName: LIST_MODELS_TOOL_NAME },
63
+ modelName: LIST_MODELS_TOOL_NAME,
64
+ codemodePath: ["opengeni", LIST_MODELS_TOOL_NAME],
65
+ title: "List selectable models",
66
+ description: LIST_MODELS_TOOL_DESCRIPTION,
67
+ inputSchema: {
68
+ type: "object",
69
+ properties: {},
70
+ additionalProperties: false,
71
+ },
72
+ annotations: {
73
+ title: "List selectable models",
74
+ readOnlyHint: true,
75
+ destructiveHint: false,
76
+ idempotentHint: true,
77
+ openWorldHint: false,
78
+ },
79
+ source: "opengeni",
80
+ approval: "none",
81
+ execute: async (args) => {
82
+ if (Object.keys(args).length !== 0) {
83
+ throw new Error("list_models accepts no arguments");
84
+ }
85
+ const catalog = await input.load();
86
+ return {
87
+ isError: false,
88
+ content: [
89
+ {
90
+ type: "text",
91
+ text: renderListModels({
92
+ currentModelId: input.currentModelId,
93
+ selections: catalog.selections,
94
+ modelNotes: catalog.modelNotes,
95
+ }),
96
+ },
97
+ ],
98
+ };
99
+ },
100
+ };
101
+ }
@@ -15,11 +15,13 @@ import {
15
15
  } from "@opengeni/runtime";
16
16
  import {
17
17
  calculateGatewayReportedCostBreakdown,
18
+ calculateGatewayReportedProviderCostMicros,
18
19
  calculateModelUsageCostBreakdown,
19
20
  configuredModelPricingSchedules,
21
+ resolveModelProvider,
20
22
  responseSatisfiesLatencyMode,
21
- OPENGENI_GATEWAY_MODELS,
22
23
  OPENGENI_GATEWAY_PROVIDER_ID,
24
+ WORKSPACE_GATEWAY_PROVIDER_ID,
23
25
  type ModelUsageInput,
24
26
  type ModelProviderApi,
25
27
  type Settings,
@@ -191,6 +193,8 @@ export async function processModelResponseTerminalEvent(input: {
191
193
  latencyMode?: LatencyMode;
192
194
  metricProvider: string;
193
195
  externallyBilled: boolean;
196
+ chargesOpenGeniCredits?: boolean;
197
+ countsTowardTokenCap?: boolean;
194
198
  servingCredentialId: string | null;
195
199
  priorSessionCredentialId: string | null;
196
200
  emittedSourceKeys: Set<string>;
@@ -251,9 +255,14 @@ export async function processModelResponseTerminalEvent(input: {
251
255
  turnAttemptId: input.turnAttemptId,
252
256
  model: input.model,
253
257
  externallyBilled: input.externallyBilled,
258
+ ...(input.chargesOpenGeniCredits !== undefined
259
+ ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
260
+ : {}),
261
+ ...(input.countsTowardTokenCap !== undefined
262
+ ? { countsTowardTokenCap: input.countsTowardTokenCap }
263
+ : {}),
254
264
  usage: responseUsage.usage,
255
265
  normalizedUsage,
256
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
257
266
  gatewayBilling: responseUsage.gatewayBilling,
258
267
  sourceKey,
259
268
  ...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
@@ -272,6 +281,8 @@ export async function processModelResponseTerminalEvent(input: {
272
281
  sourceKey,
273
282
  usage: responseUsage,
274
283
  normalizedUsage,
284
+ ...(billing ? { billingPath: billing.billingPath } : {}),
285
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
275
286
  servingAccountHash: accountContext.servingAccountHash,
276
287
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
277
288
  emittedSourceKeys: input.emittedSourceKeys,
@@ -345,6 +356,8 @@ export async function processCompactionModelUsageEvent(input: {
345
356
  providerApi: ModelProviderApi;
346
357
  model: string;
347
358
  externallyBilled: boolean;
359
+ chargesOpenGeniCredits?: boolean;
360
+ countsTowardTokenCap?: boolean;
348
361
  servingCredentialId: string | null;
349
362
  priorSessionCredentialId: string | null;
350
363
  emittedSourceKeys: Set<string>;
@@ -388,9 +401,14 @@ export async function processCompactionModelUsageEvent(input: {
388
401
  turnAttemptId: input.turnAttemptId,
389
402
  model: input.model,
390
403
  externallyBilled: input.externallyBilled,
404
+ ...(input.chargesOpenGeniCredits !== undefined
405
+ ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
406
+ : {}),
407
+ ...(input.countsTowardTokenCap !== undefined
408
+ ? { countsTowardTokenCap: input.countsTowardTokenCap }
409
+ : {}),
391
410
  usage: input.usage.usage,
392
411
  normalizedUsage,
393
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
394
412
  gatewayBilling: input.usage.gatewayBilling,
395
413
  sourceKey,
396
414
  observability: input.observability,
@@ -408,6 +426,8 @@ export async function processCompactionModelUsageEvent(input: {
408
426
  sourceKey,
409
427
  usage: input.usage,
410
428
  normalizedUsage,
429
+ ...(billing ? { billingPath: billing.billingPath } : {}),
430
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
411
431
  servingAccountHash: accountContext.servingAccountHash,
412
432
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
413
433
  emittedSourceKeys: input.emittedSourceKeys,
@@ -449,6 +469,10 @@ export async function emitModelCallUsage(input: {
449
469
  sourceKey: string;
450
470
  usage: ModelResponseUsage | { usage?: unknown | null } | null;
451
471
  normalizedUsage?: ModelCallUsageNormalization;
472
+ /** Accepted billing authority persisted for Insights repair after a soft fact-write failure. */
473
+ billingPath?: ModelUsageBillingRecord["billingPath"];
474
+ /** Validated Gateway endpoint provider persisted for exact Insights repair. */
475
+ upstreamProvider?: string;
452
476
  // Prompt-cache research dimensions (log-only; NEVER on a metric label or a
453
477
  // durable event). The opaque serving-account tag and whether it changed since
454
478
  // the session's previous call — the account-switch hypothesis for cache misses.
@@ -480,6 +504,8 @@ export async function emitModelCallUsage(input: {
480
504
  providerApi: input.providerApi,
481
505
  model: input.model,
482
506
  sourceKey: input.sourceKey,
507
+ ...(input.billingPath ? { billingPath: input.billingPath } : {}),
508
+ ...(input.upstreamProvider ? { upstreamProvider: input.upstreamProvider } : {}),
483
509
  ...telemetry,
484
510
  },
485
511
  },
@@ -572,7 +598,8 @@ export async function recordModelUsageAndDebitCredits(
572
598
  turnAttemptId: string;
573
599
  model: string;
574
600
  externallyBilled: boolean;
575
- gatewayManaged?: boolean;
601
+ chargesOpenGeniCredits?: boolean;
602
+ countsTowardTokenCap?: boolean;
576
603
  gatewayBilling?: ModelResponseUsage["gatewayBilling"];
577
604
  usage?: ModelUsageInput | ModelCallUsageInput | null;
578
605
  normalizedUsage?: ModelCallUsageNormalization;
@@ -589,19 +616,35 @@ export async function recordModelUsageAndDebitCredits(
589
616
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
590
617
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
591
618
  const totalTokens = sanitizedUsage.totalTokens ?? 0;
592
- const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : undefined;
619
+ const chargesOpenGeniCredits = input.chargesOpenGeniCredits ?? !input.externallyBilled;
620
+ const countsTowardTokenCap = input.countsTowardTokenCap ?? !input.externallyBilled;
621
+ const resolvedGatewayModel = input.gatewayBilling
622
+ ? resolveModelProvider(settings, input.model)
623
+ : undefined;
624
+ const gatewayProviderId = resolvedGatewayModel?.provider.id;
625
+ const gatewayBilling =
626
+ gatewayProviderId === OPENGENI_GATEWAY_PROVIDER_ID ||
627
+ gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID
628
+ ? input.gatewayBilling
629
+ : undefined;
630
+ const allowedProviders = resolvedGatewayModel?.model.requestPolicy?.gateway.only;
631
+ const unpinnedWorkspaceGatewayModel =
632
+ gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID && allowedProviders === undefined;
593
633
  if (gatewayBilling) {
594
- const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
595
- (candidate) => candidate.productId === input.model,
596
- );
597
634
  if (
598
- !gatewayModel ||
599
- !(gatewayModel.providers as readonly string[]).includes(gatewayBilling.finalProvider)
635
+ !unpinnedWorkspaceGatewayModel &&
636
+ (!allowedProviders ||
637
+ !(allowedProviders as readonly string[]).includes(gatewayBilling.finalProvider))
600
638
  ) {
601
639
  throw new Error(
602
640
  `AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`,
603
641
  );
604
642
  }
643
+ if (unpinnedWorkspaceGatewayModel && chargesOpenGeniCredits) {
644
+ throw new Error(
645
+ `Workspace Gateway custom model ${input.model} cannot charge OpenGeni credits without pinned pricing`,
646
+ );
647
+ }
605
648
  }
606
649
  const pricingSchedules = configuredModelPricingSchedules(settings);
607
650
  const configuredPricingModel = pricingSchedules[input.model]
@@ -610,12 +653,19 @@ export async function recordModelUsageAndDebitCredits(
610
653
  ? input.model.slice("codex/".length)
611
654
  : null;
612
655
  const pricingBreakdown = gatewayBilling
613
- ? calculateGatewayReportedCostBreakdown(
614
- settings,
615
- input.model,
616
- gatewayBilling.inferenceCostUsd,
617
- { inputTokens },
618
- )
656
+ ? unpinnedWorkspaceGatewayModel
657
+ ? {
658
+ providerCostMicros: calculateGatewayReportedProviderCostMicros(
659
+ gatewayBilling.inferenceCostUsd,
660
+ ),
661
+ creditCostMicros: 0,
662
+ }
663
+ : calculateGatewayReportedCostBreakdown(
664
+ settings,
665
+ input.model,
666
+ gatewayBilling.inferenceCostUsd,
667
+ { inputTokens },
668
+ )
619
669
  : configuredPricingModel
620
670
  ? calculateModelUsageCostBreakdown(settings, configuredPricingModel, sanitizedUsage, {
621
671
  latencyMode: input.latencyMode ?? "standard",
@@ -634,17 +684,27 @@ export async function recordModelUsageAndDebitCredits(
634
684
  : estimatedProviderCostMicros !== null
635
685
  ? ("configured_list_price" as const)
636
686
  : null;
637
- // An externally billed turn is paid outside OpenGeni, so it
638
- // consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
639
- // codex/<slug> model has no entry in configuredModelPricing, so the normal path
640
- // below would throw "Missing model pricing". We:
641
- // - do NOT emit the cap-feeding `model.tokens` event (ensureRunAllowed and
642
- // the API tokens:consume cap sum `model.tokens` with NO cost dimension, so
643
- // any row would count against maxMonthlyTokensPerWorkspace);
644
- // - record a `model.cost = 0` audit marker (harmless to the monthly cost cap);
645
- // - optionally calculate a non-charging provider estimate for Insights when
646
- // core token telemetry is complete, but never debit credits.
647
- if (input.externallyBilled) {
687
+ // Provider settlement and workspace-facing cost are separate. Externally
688
+ // metered subscription/workspace turns remain exempt from the OpenGeni token
689
+ // cap, while a deployment-funded free model still records model.tokens. Every
690
+ // non-credit path records a zero-cost marker and never consults pricing for a
691
+ // debit.
692
+ if (countsTowardTokenCap && totalTokens > 0) {
693
+ await recordUsageEvent(db, {
694
+ accountId: input.accountId,
695
+ workspaceId: input.workspaceId,
696
+ eventType: "model.tokens",
697
+ quantity: totalTokens,
698
+ unit: "tokens",
699
+ sourceResourceType: "model_response",
700
+ sourceResourceId: `${input.turnId}:${input.sourceKey}`,
701
+ sessionId: input.sessionId,
702
+ turnId: input.turnId,
703
+ turnAttemptId: input.turnAttemptId,
704
+ idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
705
+ });
706
+ }
707
+ if (!chargesOpenGeniCredits) {
648
708
  await recordUsageEvent(db, {
649
709
  accountId: input.accountId,
650
710
  workspaceId: input.workspaceId,
@@ -664,23 +724,9 @@ export async function recordModelUsageAndDebitCredits(
664
724
  estimatedProviderCostMicros,
665
725
  pricingSource,
666
726
  normalizedUsage,
727
+ ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
667
728
  };
668
729
  }
669
- if (totalTokens > 0) {
670
- await recordUsageEvent(db, {
671
- accountId: input.accountId,
672
- workspaceId: input.workspaceId,
673
- eventType: "model.tokens",
674
- quantity: totalTokens,
675
- unit: "tokens",
676
- sourceResourceType: "model_response",
677
- sourceResourceId: `${input.turnId}:${input.sourceKey}`,
678
- sessionId: input.sessionId,
679
- turnId: input.turnId,
680
- turnAttemptId: input.turnAttemptId,
681
- idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
682
- });
683
- }
684
730
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
685
731
  if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
686
732
  return {