@opengeni/config 1.2.2 → 1.4.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.
package/src/index.ts CHANGED
@@ -1,7 +1,26 @@
1
+ export { managedUserEmailAllowed } from "./managed-user-admission";
1
2
  import {
3
+ parseRealtimeVoicePricingJson,
4
+ parseRealtimeVoicePricingTableJson,
5
+ } from "./realtime-voice-pricing";
6
+ export * from "./realtime-voice-pricing";
7
+ export * from "./web-search";
8
+ import { EnvCreditPromotionPolicy } from "./credit-promotions";
9
+ export {
10
+ CreditPromotionPolicy,
11
+ signupCreditModelIds,
12
+ promotionalCreditScope,
13
+ } from "./credit-promotions";
14
+ import { isRetiredNativeAtlassianTool } from "@opengeni/contracts/atlassian-native-retirement";
15
+ import { modelLogoUrl } from "@opengeni/contracts/model-display";
16
+ import {
17
+ directModelConnectionSpec,
2
18
  BillingMode,
3
19
  CAPABILITY_DESCRIPTORS,
20
+ agentConfigDeploymentLimitsFromAllowlist,
21
+ type AgentConfigDeploymentLimits,
4
22
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
23
+ DEFAULT_OPENGENI_DOCUMENTATION_URL,
5
24
  currentAgentLearningToolSelection,
6
25
  Entitlements,
7
26
  EntitlementsMode,
@@ -26,6 +45,7 @@ import {
26
45
  type VideoGenerationResolution,
27
46
  type FirstPartyMcpToolName as FirstPartyMcpToolNameType,
28
47
  } from "@opengeni/contracts";
48
+ import type { CodeSearchDeploymentPolicy } from "@opengeni/contracts/code-search";
29
49
  import { CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS } from "@opengeni/codex";
30
50
  import {
31
51
  CODEX_FALLBACK_MODEL_SLUGS,
@@ -48,6 +68,7 @@ import {
48
68
  } from "@opengeni/xai-subscription";
49
69
  export { XAI_SUBSCRIPTION_MODEL_ID_PREFIX } from "@opengeni/xai-subscription";
50
70
  import { createHash } from "node:crypto";
71
+ import { isIP } from "node:net";
51
72
  import { z } from "zod";
52
73
 
53
74
  const envName = /^[A-Za-z_][A-Za-z0-9_]*$/;
@@ -63,6 +84,9 @@ export const DEFAULT_MODEL_COST_POLICY_JSON = JSON.stringify({
63
84
  // a configured request timeout may never consume the entire durable claim.
64
85
  export const SANDBOX_ARCHIVE_CAPTURE_MAX_TIMEOUT_MS = 60 * 60_000;
65
86
  export const SANDBOX_ARCHIVE_CAPTURE_SETTLEMENT_GRACE_MS = 10_000;
87
+ // Deadline rotation gives retained legacy commands a bounded stop window
88
+ // before capturing the still-running sandbox's current files.
89
+ export const SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS = 120_000;
66
90
  export const SANDBOX_SNAPSHOT_MAX_TIMEOUT_MS =
67
91
  SANDBOX_ARCHIVE_CAPTURE_MAX_TIMEOUT_MS - SANDBOX_ARCHIVE_CAPTURE_SETTLEMENT_GRACE_MS;
68
92
  export const GOOGLE_DRIVE_PROVIDER_REQUEST_TIMEOUT_MAX_MS = 60_000;
@@ -91,6 +115,13 @@ const EnvBoolean = z.preprocess((value) => {
91
115
  return value;
92
116
  }, z.boolean());
93
117
 
118
+ /** An absolute http(s) URL that is safe to render as a browser link. */
119
+ const PublicHttpUrl = z
120
+ .string()
121
+ .url()
122
+ .max(2_048)
123
+ .refine((value) => /^https?:\/\//iu.test(value), "must be an http(s) URL");
124
+
94
125
  /** Default pacing between consecutive no-input goal continuations. */
95
126
  export const DEFAULT_GOAL_IDLE_BACKOFF_MS: readonly number[] = [3_000, 30_000, 120_000, 300_000];
96
127
  export const DEFAULT_GOAL_IDLE_BACKOFF_MAX_MS = 600_000;
@@ -181,8 +212,9 @@ export const AGENT_INSTRUCTIONS_CORE_PLACEHOLDER = "{{core}}";
181
212
 
182
213
  /**
183
214
  * Default per-workspace agent persona template. This is the BRAND + tool-usage
184
- * opinion (the white-labellable surface): the "You are an OpenGeni workspace
185
- * agent." identity line, the framing/opinion lines, and the mount-path facts.
215
+ * opinion (the white-labellable surface): the general-assistant identity line,
216
+ * the framing/opinion lines, and the mount-path facts. Domain-specific tooling
217
+ * guidance (for example Terraform or Checkov) belongs in opt-in Skills.
186
218
  *
187
219
  * The CORE that MUST survive any override — the goal-loop ownership line (which
188
220
  * names the opengeni__goal_* tools) and the dynamic workspace-environment block
@@ -196,17 +228,17 @@ export const AGENT_INSTRUCTIONS_CORE_PLACEHOLDER = "{{core}}";
196
228
  * intentionally.
197
229
  */
198
230
  export const DEFAULT_AGENT_INSTRUCTIONS = [
199
- "You are an OpenGeni workspace agent.",
200
- "Follow the user's task and any enabled pack or skill instructions for the current role.",
201
- "Work inside the sandbox workspace and use filesystem and shell tools when useful.",
231
+ "You are an OpenGeni workspace agent: a general assistant for questions, writing, research, analysis, and technical work.",
232
+ "Follow the user's task and the applicable Skill instructions for the current role.",
233
+ "When a task needs files or commands, work inside the sandbox workspace with the filesystem and shell tools.",
202
234
  "Repository resources are mounted under repos/<host>/<owner>/<repo> unless the session specifies another collision-free mount path.",
203
235
  "File resources are mounted under .opengeni/files/<file-id>/ unless the session specifies another mount path.",
204
236
  "Attached files are mounted read-only; copy them before modifying.",
205
237
  "Installed and selected Skills appear in the session Skill index; follow its reading instructions and any role-specific guidance.",
206
- "Use Checkov, Terraform, Azure CLI, git provider CLIs, and repository tools when relevant; gh, glab, and az repos are pre-authenticated when the host brokers matching git credentials.",
207
- "When the Azure sandbox preparation profile is enabled and service-principal variables are present, the sandbox is pre-authenticated with normal Azure CLI before work starts.",
208
- "Treat code-changing work as GitOps work: create a focused branch/commit/PR when git provider credentials are available; otherwise report exact commands and blockers.",
209
- "Return concise, factual summaries with files changed, commands run, and remaining blockers.",
238
+ "Provider CLIs such as gh, glab, and az may be pre-authenticated by the host through brokered git credentials or a sandbox preparation profile; try them before asking for credentials.",
239
+ "When the Git repository you change has a remote and git provider credentials are available, work on a focused branch and open a pull request.",
240
+ "Otherwise leave changes in the working tree and do not create or mention branches, commits, or pull requests unless the user asks; if the repository has a remote, say the changes are not pushed, and if the user asks for something you cannot make, say what blocks it.",
241
+ "Answer questions directly and briefly; after making changes, say what changed, how you checked it, and anything still blocked.",
210
242
  AGENT_INSTRUCTIONS_CORE_PLACEHOLDER,
211
243
  ].join(" ");
212
244
 
@@ -234,7 +266,7 @@ export const McpOperationRecoverySchema = z
234
266
 
235
267
  /** Public, digest-pinned desktop image used by Modal unless the operator overrides it. */
236
268
  export const DEFAULT_MODAL_IMAGE_REF =
237
- "opengenipublicneuacr.azurecr.io/opengeni-desktop@sha256:c3bd17b8841de1bff9bb2777aad422cf8c75de78e2c30f9ac81d0cd6810a1b78";
269
+ "opengenipublicneuacr.azurecr.io/opengeni-desktop@sha256:554d5b324a580071dd669e7c1190931e498eb1b397412a27c7de84a430ef393a";
238
270
 
239
271
  const SettingsSchema = z.object({
240
272
  serviceName: z.string().default("opengeni"),
@@ -244,6 +276,10 @@ const SettingsSchema = z.object({
244
276
  // Absent on dev/source builds — consumers must treat it as optional.
245
277
  serverVersion: z.string().optional(),
246
278
  databaseUrl: z.string().default("postgres://opengeni:opengeni@127.0.0.1:5432/opengeni"),
279
+ // API application pool only. Managed auth owns its separate pool; readiness
280
+ // reuses the application handle. Keep the historical default for existing
281
+ // deployments and let operators budget this per process with replica counts.
282
+ apiDatabasePoolMax: z.coerce.number().int().positive().default(32),
247
283
  // Step I (§7.8 runtime half). Dedicated Postgres schema for the EMBEDDED
248
284
  // topology. Default "" → standalone: no search_path scoping, server default
249
285
  // (`public`). When set (e.g. "opengeni"), the db handle + the managed-auth
@@ -285,6 +321,8 @@ const SettingsSchema = z.object({
285
321
  observabilityMetricsEnabled: EnvBoolean.default(true),
286
322
  observabilityOtlpEndpoint: z.string().url().optional(),
287
323
  observabilityOtlpHeaders: z.string().default(""),
324
+ observabilityDiagnosticsEndpoint: z.string().url().optional(),
325
+ observabilityDiagnosticsHeaders: z.string().default(""),
288
326
  analyticsEnabled: EnvBoolean.default(false),
289
327
  analyticsConsentRequired: EnvBoolean.default(true),
290
328
  analyticsReoClientId: z
@@ -299,13 +337,44 @@ const SettingsSchema = z.object({
299
337
  .max(32)
300
338
  .regex(/^G-[A-Z0-9]+$/u)
301
339
  .optional(),
340
+ // Optional operator-owned legal documents linked from the signed-out console.
341
+ // Unset by default, so a self-hosted deployment never shows another operator's
342
+ // policies; the managed service points these at its own published pages.
343
+ legalPrivacyPolicyUrl: PublicHttpUrl.optional(),
344
+ legalTermsOfServiceUrl: PublicHttpUrl.optional(),
345
+ // Optional operator support address, shown as a "Contact support" mailto link
346
+ // on the signed-out page and in the Help menu. Unset by default for the same
347
+ // reason as the legal links.
348
+ supportEmail: z.string().trim().email().max(254).optional(),
302
349
  publicBaseUrl: z.string().url().optional(),
350
+ // Product documentation the web console links from its Help menu. Absent
351
+ // means the public OpenGeni docs; `none` hides the link for deployments that
352
+ // publish no documentation of their own.
353
+ documentationUrl: z.preprocess(
354
+ (value) =>
355
+ typeof value === "string"
356
+ ? value.trim().toLowerCase() === "none"
357
+ ? null
358
+ : value.trim()
359
+ : value,
360
+ z
361
+ .url({ protocol: /^https?$/u, error: "must be an absolute http(s) URL or none" })
362
+ .nullable()
363
+ .default(DEFAULT_OPENGENI_DOCUMENTATION_URL),
364
+ ),
303
365
  // Standards-based OAuth authorization server for external workspace MCP
304
366
  // clients. Opt-in because it creates a new public authentication surface.
305
367
  mcpOauthEnabled: EnvBoolean.default(false),
306
- // Forwarded client addresses are ignored by default. Operators may trust an
368
+ // Client source address for every API rate limit, abuse quota, and auth
369
+ // session record (Better Auth, MCP OAuth registration, enrollment, account
370
+ // setup, login transactions). Forwarded client addresses are ignored by
371
+ // default so a caller cannot choose its own bucket. Operators may trust an
307
372
  // exact number of proxy hops only when direct access to the API is blocked.
308
- mcpOauthTrustedProxyHops: z.coerce.number().int().min(0).max(16).default(0),
373
+ apiTrustedProxyHops: z.coerce.number().int().min(0).max(16).default(0),
374
+ // Optional comma-separated CIDRs (or single addresses) of the proxy that
375
+ // connects to the API. When set, forwarded client addresses are honored
376
+ // only for requests whose transport peer is inside one of them.
377
+ apiTrustedProxyCidrs: z.string().default(""),
309
378
  // Browser origin when the web app and API use separate origins in local
310
379
  // development. Production normally leaves this unset and uses publicBaseUrl.
311
380
  webBaseUrl: z.string().url().optional(),
@@ -330,34 +399,26 @@ const SettingsSchema = z.object({
330
399
  .regex(/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/u)
331
400
  .optional(),
332
401
  productAccessMode: ProductAccessMode.default("local"),
333
- // --- canonical organization-tenancy authority activation, default OFF ---
334
- // The named PRE-ACTIVATION opt-out for the organization-tenancy program. FALSE (the
335
- // default, and the value an operator leaves in place to decline or defer) means
336
- // this deployment stays on the reversible legacy workspace-owned lane: no phase-F
337
- // subsystem may switch its access decision to organization/membership authority
338
- // ids. TRUE is an operator's explicit statement that the activation preconditions
339
- // in docs/organization-tenancy.md have been proven for this deployment and that
340
- // the one-way boundary is accepted.
341
- //
342
- // This is NOT a kill switch and NOT a rollback: once an activation migration has
343
- // committed, setting it back to false does not restore the legacy authority - only
344
- // forward recovery is available. It also grants and revokes nothing by itself;
345
- // every individual authorization decision keeps its own fences.
346
- //
347
- // No runtime path reads it yet: canonical activation (phase F) is unshipped, so
348
- // the flag exists to reserve the name, pin the safe default, and give every future
349
- // activation slice one gate to consult. EnvBoolean (NOT z.coerce.boolean(), which
350
- // coerces "false" -> true and would activate the moment an operator wrote the
351
- // variable out to disable it).
352
- organizationTenancyCanonicalActivationEnabled: EnvBoolean.default(false),
353
402
  billingMode: BillingMode.default("disabled"),
403
+ // Explicit launch gate for the one-time $10 verified self-service signup grant.
404
+ // A migration or deployment alone must not start issuing live credits.
405
+ verifiedSignupTrialCreditsEnabled: EnvBoolean.default(false),
406
+ creditPromotionPolicy: EnvCreditPromotionPolicy,
354
407
  entitlementsMode: EntitlementsMode.default("none"),
355
408
  usageLimitsMode: UsageLimitsMode.default("none"),
409
+ // Gate new allowance policies until every API/worker in the fleet enforces
410
+ // them. Persisted-policy enforcement and recovery reads/clears stay active.
411
+ usageAllowancesEnabled: EnvBoolean.default(false),
356
412
  staticEntitlementsJson: z.string().default("{}"),
357
413
  staticUsageLimitsJson: z.string().default("{}"),
358
414
  delegationSecret: z.string().optional(),
359
415
  defaultFirstPartyMcpTools: EnvFirstPartyMcpTools,
360
416
  allowedFirstPartyMcpTools: EnvFirstPartyMcpTools,
417
+ // Deployment fact: the isolated artifact materializer workload runs and
418
+ // drains export jobs. Without it an export is queued forever, so the export
419
+ // tools leave the first-party ceiling. Helm sets this from
420
+ // artifactMaterializer.enabled.
421
+ artifactMaterializerDeployed: EnvBoolean.default(false),
361
422
  // sandbox workspace scoped stream-token HMAC secret (sandbox contract §C.3 / stream-token availability contract).
362
423
  // When unset, the API falls back to `delegationSecret` (the same HMAC envelope
363
424
  // family, `ogs_` vs `ogd_` prefix). REQUIRED-WHEN-DESKTOP, but the absence of
@@ -393,11 +454,18 @@ const SettingsSchema = z.object({
393
454
  environmentsEncryptionKey: z.string().optional(),
394
455
  integrationsEnabled: EnvBoolean.default(false),
395
456
  integrationsStateSecret: z.string().optional(),
457
+ integrationsOauthShortStateEnabled: EnvBoolean.default(false),
396
458
  integrationsAllowPrivateNetworkTargets: EnvBoolean.default(false),
397
459
  integrationsOauthClientsJson: z.string().default("{}"),
460
+ gmailWatchTopicName: z
461
+ .string()
462
+ .regex(/^projects\/[^/]+\/topics\/[^/]+$/u)
463
+ .optional(),
398
464
  slackClientId: z.string().optional(),
399
465
  slackClientSecret: z.string().optional(),
400
466
  slackSigningSecret: z.string().optional(),
467
+ // Unlisted apps share Slack’s restricted history/replies quota across all tokens.
468
+ slackAccessMode: z.enum(["limited", "full"]).default("limited"),
401
469
  slackBotDisplayName: OpenGeniSlackBotDisplayName.default("OpenGeni"),
402
470
  slackCommand: z
403
471
  .string()
@@ -484,16 +552,6 @@ const SettingsSchema = z.object({
484
552
  // into @opengeni/db once at boot.
485
553
  // Env: OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED.
486
554
  childLifecycleNoticesEnabled: EnvBoolean.default(false),
487
- // Explicit host-owned MCP connection authority is a rolling protocol
488
- // activation. Keep it off while any API, worker, or browser bundle predates
489
- // the authority discriminator; enable it only after the whole fleet runs an
490
- // image that understands host refs. Legacy markerless non-UUID refs remain a
491
- // separate compatibility lane for already-persisted embedding integrations.
492
- // Env: OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED.
493
- hostMcpAuthoritySourceAdmissionEnabled: EnvBoolean.default(false),
494
- // Server-owned per-organization remote MCP resolver configuration. Optional;
495
- // inline credentials and native provider OAuth do not require this service.
496
- hostMcpCredentialResolversJson: z.string().optional(),
497
555
  // Per-channel and per-DM Slack workspace routing. Default ON. A channel does
498
556
  // not count a personal workspace as a candidate, so an organization with one
499
557
  // shared workspace resolves it as the sole candidate and never asks; the
@@ -551,6 +609,9 @@ const SettingsSchema = z.object({
551
609
  authAllowMetrics: EnvBoolean.default(false),
552
610
  apiHost: z.string().default("0.0.0.0"),
553
611
  apiPort: z.coerce.number().int().positive().default(8000),
612
+ // When set, GET /metrics is served only on this dedicated internal listener
613
+ // and never on the public API port that an ingress forwards to.
614
+ apiMetricsPort: z.coerce.number().int().positive().optional(),
554
615
  workerHttpPort: z.coerce.number().int().positive().default(8001),
555
616
  // Worker-side first-party MCP traffic stays on the deployment's internal
556
617
  // network. OPENGENI_MCP_URL remains the sandbox/external route used by
@@ -561,11 +622,16 @@ const SettingsSchema = z.object({
561
622
  // call the public API with bearer credentials, but never receive credentialed
562
623
  // CORS responses.
563
624
  corsAllowOriginRegex: z.string().default(String.raw`^https?://(localhost|127\.0\.0\.1)(:\d+)?$`),
625
+ // Local development only (`local` access mode in the `local` environment):
626
+ // extra exact browser origins, comma-separated, that may call the
627
+ // unauthenticated API besides this stack's own web origin. The local browser
628
+ // boundary ignores `corsAllowOriginRegex`.
629
+ localAllowedOrigins: z.string().optional(),
564
630
  openaiProvider: z.enum(["openai", "azure"]).default("openai"),
565
631
  openaiApiKey: z.string().optional(),
566
632
  openaiBaseUrl: z.string().optional(),
567
- openaiModel: z.string().default("gpt-5.6-sol"),
568
- openaiAllowedModels: z.string().default("gpt-5.6-sol,gpt-5.6-terra,gpt-5.6-luna"),
633
+ openaiModel: z.string().default("gpt-6-astra"),
634
+ openaiAllowedModels: z.string().default("gpt-6-astra,gpt-6-sol,gpt-6-luna"),
569
635
  // OpenGeni-managed Vercel AI Gateway. When configured, the two reviewed
570
636
  // Gateway models below are added to the managed-credit catalog. Workspace
571
637
  // Gateway keys use the encrypted connection broker and never this secret.
@@ -670,6 +736,35 @@ const SettingsSchema = z.object({
670
736
  voiceInputAzureApiVersion: z.string().optional(),
671
737
  voiceInputAzureApiKey: z.string().optional(),
672
738
  voiceInputAzureAdToken: z.string().optional(),
739
+ // Hosted realtime voice is configured independently of dictation.
740
+ azureLiveEndpoint: z.string().url().optional(),
741
+ azureLiveApiKey: z.string().optional(),
742
+ azureLiveDeployment: z.string().default("gpt-live-1"),
743
+ azureLiveVoice: z.string().default("marin"),
744
+ // Credit price of deployment-funded live voice (RealtimeVoicePricing JSON).
745
+ // Required while credit billing is active, otherwise the model is withheld.
746
+ azureLivePricingJson: z.string().optional(),
747
+ // Managed AI Gateway live voice prices keyed by upstream model id.
748
+ aiGatewayRealtimePricingJson: z.string().optional(),
749
+ // Azure Speech credentials are explicit: a different resource can host MAI.
750
+ voiceInputMaiEndpoint: z.string().url().optional(),
751
+ voiceInputMaiApiKey: z.string().optional(),
752
+ voiceInputMaiApiVersion: z.string().default("2025-10-15"),
753
+ voiceInputMaiPricingJson: z.string().optional(),
754
+ voiceInputMaiModel: z.string().default("MAI-Transcribe-2"),
755
+ // Underlying model of the Azure deployment (for example gpt-4o-transcribe or
756
+ // whisper). Azure routes by deployment name, so this only selects built-in
757
+ // pricing and the provider-reported usage shape (Whisper requests
758
+ // verbose_json so Azure reports the billed audio duration). Defaults to the
759
+ // deployment name.
760
+ voiceInputAzureModel: z.string().trim().min(1).max(256).optional(),
761
+ // Opengeni credit pricing for deployment-funded voice input (`openai`,
762
+ // `azure-openai`). JSON VoiceInputPricing; overrides the built-in list price
763
+ // for the configured model. Validated at boot. When credit billing is active
764
+ // (OPENGENI_BILLING_MODE=stripe or OPENGENI_USAGE_LIMITS_MODE=managed) a
765
+ // deployment-funded provider without any pricing is not offered at all.
766
+ voiceInputOpenaiPricingJson: z.string().optional(),
767
+ voiceInputAzurePricingJson: z.string().optional(),
673
768
  // Legacy opt-in for undocumented ChatGPT /backend-api/transcribe. When
674
769
  // OPENGENI_CODEX_SUBSCRIPTION_ENABLED is true, Codex STT is included without
675
770
  // this flag. Set false and omit codex-subscription from PROVIDER_ORDER to
@@ -694,6 +789,7 @@ const SettingsSchema = z.object({
694
789
  // binding so database mode cannot be bypassed with a second source.
695
790
  resolvedGatewayModelsJson: z.string().optional(),
696
791
  resolvedOpenRouterModelsJson: z.string().optional(),
792
+ resolvedCodexModelsJson: z.string().optional(),
697
793
  // Extra (non-built-in) model providers, declared by the host as a JSON
698
794
  // provider registry. Each entry carries its own base URL, API key, wire API
699
795
  // ("responses" | "chat") and the models it exposes. The models a client may
@@ -708,6 +804,7 @@ const SettingsSchema = z.object({
708
804
  // SuperGrok/xAI connected subscription. This is a workspace-scoped OAuth
709
805
  // account pool and a distinct rail from the existing xai/* API-key provider.
710
806
  supergrokSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED
807
+ claudeSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED
711
808
  // Maximum silence between complete, valid SuperGrok SSE data events. This is
712
809
  // not a request/run duration cap; every valid event resets the timer.
713
810
  supergrokResponseStreamIdleTimeoutMs: z.coerce
@@ -744,8 +841,27 @@ const SettingsSchema = z.object({
744
841
  // rotated TO — when EITHER usage window (5h/weekly) is at/over this percent. Default 90 to
745
842
  // match the UI danger flip (UsageBar danger at pct >= 90). OPENGENI_CODEX_ROTATION_NEAR_EXHAUSTION_PCT.
746
843
  codexRotationNearExhaustionPct: z.coerce.number().int().min(1).max(100).default(90),
844
+ reasoningConfigurationUpdatesEnabled: EnvBoolean.default(false),
747
845
  openaiReasoningEffort: ReasoningEffort.default("low"),
748
846
  openaiAllowedReasoningEfforts: z.string().default("low,medium,high,xhigh,max"),
847
+ // Default for new chats and scheduled tasks when the workspace has an
848
+ // OpenGeni credit balance, no saved workspace default, and no usable
849
+ // connected subscription, and the deployment default is not already a
850
+ // credits-billed model. Selected only when this model is selectable in the
851
+ // workspace catalog; otherwise the first selectable credits model is used.
852
+ // OPENGENI_CREDITS_DEFAULT_MODEL / OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT.
853
+ creditsDefaultModel: z
854
+ .string()
855
+ .trim()
856
+ .min(1)
857
+ .max(256)
858
+ .refine((value) => !/[\u000A\u000D|]/u.test(value), {
859
+ message: "credits default model must not contain newlines or the | field separator",
860
+ })
861
+ .default("gpt-6-luna"),
862
+ // Preferred effort for the credits default. Clamped to the highest effort
863
+ // the selected model supports at or below this value.
864
+ creditsDefaultReasoningEffort: ReasoningEffort.default("xhigh"),
749
865
  openaiResponsesTransport: z.enum(["http", "websocket"]).default("http"),
750
866
  // Provider-assigned item ids (rs_/msg_/fc_…) in Responses API input are
751
867
  // resolved against the provider's server-side response store. That store is
@@ -771,6 +887,31 @@ const SettingsSchema = z.object({
771
887
  // merged with the MCP-server tools (getAllTools = [...mcpTools, ...tools])
772
888
  // and the sandbox capability tools, never replacing them.
773
889
  webSearchEnabled: EnvBoolean.default(true),
890
+ // Provider-agnostic web search (`web_search` / `web_fetch` agent tools,
891
+ // see ./web-search.ts). Inert until a provider is named; validated lazily so
892
+ // a misconfiguration withholds the tools instead of failing boot.
893
+ webSearchProvider: z.string().optional(),
894
+ webSearchApiKey: z.string().optional(),
895
+ webSearchBaseUrl: z.string().optional(),
896
+ webFetchProvider: z.string().optional(),
897
+ webFetchApiKey: z.string().optional(),
898
+ webFetchBaseUrl: z.string().optional(),
899
+ webSearchProviderMode: z.string().optional(),
900
+ webSearchPricingJson: z.string().optional(),
901
+ webSearchRequestTimeoutMs: z.coerce.number().int().positive().max(120_000).default(20_000),
902
+ // Jev (TypeSafe's fast judge model) for worker-side agent tools. Without a
903
+ // usable key every Jev-backed feature is off. The key stays on the server
904
+ // (API and worker) and never reaches a sandbox or Connected Machine.
905
+ jevApiKey: z.string().optional(),
906
+ jevBaseUrl: z.string().url().default("https://api.typesafe.ai"),
907
+ jevModel: z.string().trim().min(1).max(128).default("jev-latest"),
908
+ jevRequestTimeoutMs: z.coerce.number().int().positive().max(120_000).default(10_000),
909
+ // Jev-backed `code_search` agent tool. `off` never offers it, `opt_in` offers
910
+ // it only where workspace settings enable it, `default_on` offers it
911
+ // everywhere except workspaces that disable it, and `experiment` gives it to
912
+ // a fixed half of sessions in workspaces without their own setting, for
913
+ // comparison. It also needs jevApiKey.
914
+ codeSearchMode: z.enum(["off", "opt_in", "default_on", "experiment"]).default("off"),
774
915
  // Deployment-default agent persona template (the white-label surface). The
775
916
  // runtime resolves the effective template per turn as
776
917
  // per-session-override > per-workspace override > this default, substitutes
@@ -792,7 +933,7 @@ const SettingsSchema = z.object({
792
933
  // the verified, self-contained native artifact runtime at its fixed image
793
934
  // paths. Disabled by default so arbitrary/custom provider images never make
794
935
  // document/spreadsheet/presentation skills appear when their runtime is
795
- // absent. Per-pack/per-rig image overrides fail closed in the worker even
936
+
796
937
  // when this base-image contract is enabled.
797
938
  sandboxArtifactRuntimeEnabled: EnvBoolean.default(false),
798
939
  dockerExposedPorts: z.string().default(""),
@@ -804,6 +945,9 @@ const SettingsSchema = z.object({
804
945
  dockerWorkspaceBaseDir: z.string().min(1).optional(),
805
946
  modalAppName: z.string().default("opengeni-sandbox"),
806
947
  modalImageRef: z.string().optional(),
948
+ // Activate only after descriptor-aware readers, DB fences and the exact
949
+ // native image have passed the supervised-command rollout canary.
950
+ modalCommandSupervisionEnabled: EnvBoolean.default(false),
807
951
  // Provider-native immutable Modal image ID for the exact logical
808
952
  // `modalImageRef`. When set, the runtime uses ModalImageSelector.fromId and
809
953
  // never asks Modal to parse or import the registry ref. The logical ref is
@@ -881,12 +1025,10 @@ const SettingsSchema = z.object({
881
1025
  // Shared desktop toggle: this module reads it for the 6080 port-merge; the
882
1026
  // owner module (P4.x) acts on it to launch the display stack.
883
1027
  sandboxDesktopEnabled: EnvBoolean.default(false),
884
- // Human take-control toggle: when ON (default) the negotiated DesktopStream
885
- // cell advertises mode "interactive" — the noVNC viewer can drive mouse+keyboard
886
- // into :0 (x11vnc runs without -viewonly). Turn it OFF for a genuinely read-only
887
- // deployment: the cell reports mode "read-only" and the client disables the
888
- // "Take control" affordance. This gates the HUMAN viewer plane; agent
889
- // interaction is authorized through managed ComputerSession tools.
1028
+ // Human sandbox input policy. Canonical ComputerSession attachments reflect
1029
+ // it and /actions enforces it independently of the client. The legacy
1030
+ // DesktopStream adapter also reports read-only when disabled. Agent tools
1031
+ // retain their separate session-control authority.
890
1032
  sandboxDesktopInteractive: EnvBoolean.default(true),
891
1033
  // REAL PTY terminal toggle (P5.t): gates the ttyd pty-ws plane (7681) the API
892
1034
  // mints over the SAME tunnel as the desktop. Defaults ON — the interactive
@@ -950,6 +1092,7 @@ const SettingsSchema = z.object({
950
1092
  // --- cloudflare (headless) ---
951
1093
  cloudflareWorkerUrl: z.string().url().optional(),
952
1094
  cloudflareApiKey: z.string().optional(),
1095
+ experimentalBrowserContextPoolEnabled: EnvBoolean.default(false),
953
1096
  // --- remote browser placements ---
954
1097
  // Provider credentials are injected only into the placement-resident
955
1098
  // browserd launch. They never enter session contracts, journals, or sandboxes.
@@ -1135,6 +1278,33 @@ const SettingsSchema = z.object({
1135
1278
  // getSettings caps the default at half a shorter configured Modal lifetime so
1136
1279
  // the entire reaper window always fits. Knob: OPENGENI_SANDBOX_IDLE_GRACE_MS.
1137
1280
  sandboxIdleGraceMs: z.coerce.number().int().positive().default(900_000),
1281
+ // Idle command containment. A legacy retained background command (a dev
1282
+ // server, a command whose output is still draining, a stopped command the
1283
+ // provider no longer answers for) keeps its box warm through a non-expiring
1284
+ // process holder, so the zero-holder drain never runs and the box would stay
1285
+ // up until the provider deadline kills it uncaptured. Once every session of
1286
+ // the sandbox group has been unused this long - no open turn or pending
1287
+ // request, no held wait_for_input, no other holder or writer, measured from
1288
+ // durable turn/holder/admission facts - the reaper checkpoints the workspace,
1289
+ // stops the box and settles those commands `lost` with reason
1290
+ // `idle_containment`. Default 30min: twice the default
1291
+ // idle grace, so a lease that is only waiting for a "glanced away" user is
1292
+ // never contained earlier than an idle lease would drain, and well inside
1293
+ // the 1h provider-deadline rotation lead, so an idle box is saved long before
1294
+ // the deadline path has to act. Set 0 to disable new idle enrollments without
1295
+ // disabling provider-deadline containment or cancelling an enrolled drain.
1296
+ // A positive window must exceed OPENGENI_SANDBOX_IDLE_GRACE_MS;
1297
+ // an explicit positive value must also stay below OPENGENI_SANDBOX_ROTATION_LEAD_MS,
1298
+ // and with an explicit OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS the reaper period
1299
+ // plus this window plus the drain capture budget must fit before it.
1300
+ // getSettings derives the unset default between those two for short-lived
1301
+ // provider lifetimes. Knob: OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS.
1302
+ sandboxIdleCommandContainmentMs: z.coerce
1303
+ .number()
1304
+ .int()
1305
+ .nonnegative()
1306
+ .optional()
1307
+ .transform((value) => (value === 0 ? undefined : value)),
1138
1308
  // MID-SESSION /workspace snapshot cadence (sandbox-file-persistence). The
1139
1309
  // reaper's drain-persist only protects boxes the reaper itself kills; a box
1140
1310
  // that dies any other way (Modal's hard creation-time timeout on a session
@@ -1218,6 +1388,7 @@ const SettingsSchema = z.object({
1218
1388
  // Shape: { "modal": 5, "runloop": 4, ... }. Backends absent here meter
1219
1389
  // warm-seconds but accrue NO warm_cost / debit (rate 0).
1220
1390
  sandboxWarmRateMicrosPerSecondJson: z.string().default("{}"),
1391
+ sandboxWarmBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1221
1392
  // Per-workspace warm cap (cumulative warm-seconds since the start of the UTC
1222
1393
  // month, summed over sandbox.warm_seconds). 0 = unbounded. A workspace over the
1223
1394
  // cap force-drains its VIEWER-ONLY boxes (guarded AND turn_holders=0 — a paying
@@ -1225,6 +1396,9 @@ const SettingsSchema = z.object({
1225
1396
  sandboxMaxWarmSecondsPerWorkspace: z.coerce.number().int().nonnegative().default(0),
1226
1397
  sandboxPreparationProfiles: z.string().default("none"),
1227
1398
  sandboxEnvAllowlist: z.string().default(""),
1399
+ // Comma-separated image references a workspace may select as its default
1400
+ // sandbox image. Empty keeps every workspace on the deployment image.
1401
+ sandboxImageAllowlist: z.string().default(""),
1228
1402
  objectStorageEndpoint: z.string().url().optional(),
1229
1403
  objectStorageInternalEndpoint: z.string().url().optional(),
1230
1404
  objectStorageSandboxEndpoint: z.string().url().optional(),
@@ -1253,6 +1427,13 @@ const SettingsSchema = z.object({
1253
1427
  documentEmbeddingDimensions: z.coerce.number().int().positive().default(3072),
1254
1428
  documentEmbeddingApiKey: z.string().optional(),
1255
1429
  documentEmbeddingBaseUrl: z.string().url().optional(),
1430
+ documentEmbeddingBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1431
+ // Explicit commercial cutover. Existing queued revisions remain unpriced;
1432
+ // only jobs created at or after this instant may enter the paid rail.
1433
+ documentEmbeddingCreditsActivatedAt: z.iso.datetime().optional(),
1434
+ // Customer tariff for the exact UTF-8 bytes sent to the embedder. Zero leaves
1435
+ // the paid mode unavailable; no commercial rate is selected by this PR.
1436
+ documentEmbeddingRateMicrosPerMillionBytes: z.coerce.number().int().nonnegative().default(0),
1256
1437
  documentCurationProvider: z.enum(["openai", "heuristic", "none"]).default("openai"),
1257
1438
  documentCurationModel: z.string().min(1).default("gpt-4o-mini"),
1258
1439
  documentCurationApiKey: z.string().optional(),
@@ -1290,7 +1471,17 @@ const SettingsSchema = z.object({
1290
1471
  managedAuthGithubClientSecret: z.string().optional(),
1291
1472
  // Rolling browser login-slot compatibility. Repository/deployment default is
1292
1473
  // deliberately legacy; changing to broker is an operator-authorized rollout.
1474
+ allowedUserEmails: z
1475
+ .array(z.email().transform((email) => email.toLowerCase()))
1476
+ .min(1)
1477
+ .optional(),
1293
1478
  managedAuthSessionSetMode: z.enum(["legacy", "dual", "broker"]).default("legacy"),
1479
+ // Deployment ceiling for new managed accounts. When false, managed auth
1480
+ // refuses every new Better Auth account (email/password sign-up and implicit
1481
+ // Google/GitHub sign-up) while existing sign-in, sessions, password reset,
1482
+ // email verification, and invitation-bound account setup keep working. Read
1483
+ // at startup; the 0585 runtime switch pauses sign-ups without a restart.
1484
+ managedAuthNewSignupsEnabled: EnvBoolean.default(true),
1294
1485
  // Query transport is an explicit second-stage rollout. A pre-compatibility
1295
1486
  // web image understands only fragment bearers, so API replicas must keep
1296
1487
  // generating fragment links until the compatible web fleet has converged.
@@ -1332,6 +1523,124 @@ const SettingsSchema = z.object({
1332
1523
  });
1333
1524
 
1334
1525
  export type Settings = z.infer<typeof SettingsSchema>;
1526
+
1527
+ // Independently supervised artifact services are not API/agent runtimes. Parse
1528
+ // only their actual capabilities, using the same field schemas as getSettings.
1529
+ const ArtifactServiceSettingsSchema = SettingsSchema.pick({
1530
+ serviceName: true,
1531
+ environment: true,
1532
+ deploymentRevision: true,
1533
+ dbSchema: true,
1534
+ rlsStrategy: true,
1535
+ observabilityStructuredLogs: true,
1536
+ observabilityMetricsEnabled: true,
1537
+ observabilityOtlpEndpoint: true,
1538
+ observabilityOtlpHeaders: true,
1539
+ });
1540
+ const ArtifactOutboxSettingsSchema = ArtifactServiceSettingsSchema.extend({
1541
+ natsUrl: SettingsSchema.shape.natsUrl,
1542
+ selfhostedNatsControlUser: SettingsSchema.shape.selfhostedNatsControlUser,
1543
+ selfhostedNatsControlPassword: SettingsSchema.shape.selfhostedNatsControlPassword,
1544
+ });
1545
+ const ObjectStorageSettingsSchema = SettingsSchema.pick({
1546
+ objectStorageBackend: true,
1547
+ objectStorageEndpoint: true,
1548
+ objectStorageInternalEndpoint: true,
1549
+ objectStorageSandboxEndpoint: true,
1550
+ objectStorageBucket: true,
1551
+ objectStorageRegion: true,
1552
+ objectStorageAccessKeyId: true,
1553
+ objectStorageSecretAccessKey: true,
1554
+ objectStorageForcePathStyle: true,
1555
+ objectStorageAzureConnectionString: true,
1556
+ objectStorageAzureAccountName: true,
1557
+ objectStorageAzureAccountKey: true,
1558
+ objectStorageAzureEndpoint: true,
1559
+ objectStorageGcsProjectId: true,
1560
+ objectStorageGcsCredentialsJson: true,
1561
+ objectStorageGcsKeyFilename: true,
1562
+ objectStorageGcsApiEndpoint: true,
1563
+ });
1564
+ const ArtifactMaterializerSettingsSchema = ArtifactServiceSettingsSchema.extend(
1565
+ ObjectStorageSettingsSchema.shape,
1566
+ );
1567
+ export type ArtifactOutboxSettings = z.infer<typeof ArtifactOutboxSettingsSchema>;
1568
+ export type ObjectStorageSettings = z.infer<typeof ObjectStorageSettingsSchema>;
1569
+ export type ArtifactMaterializerSettings = z.infer<typeof ArtifactMaterializerSettingsSchema>;
1570
+
1571
+ function artifactServiceEnvironment(source: NodeJS.ProcessEnv) {
1572
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1573
+ return {
1574
+ serviceName: optional("OPENGENI_SERVICE_NAME"),
1575
+ environment: optional("OPENGENI_ENVIRONMENT"),
1576
+ deploymentRevision:
1577
+ optional("OPENGENI_DEPLOYMENT_REVISION") ??
1578
+ optional("SOURCE_VERSION") ??
1579
+ optional("GITHUB_SHA"),
1580
+ dbSchema: optional("OPENGENI_DB_SCHEMA"),
1581
+ rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
1582
+ observabilityStructuredLogs: optional("OPENGENI_OBSERVABILITY_STRUCTURED_LOGS"),
1583
+ observabilityMetricsEnabled: optional("OPENGENI_OBSERVABILITY_METRICS_ENABLED"),
1584
+ observabilityOtlpEndpoint:
1585
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
1586
+ observabilityOtlpHeaders:
1587
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
1588
+ };
1589
+ }
1590
+
1591
+ export function getArtifactOutboxSettings(
1592
+ source: NodeJS.ProcessEnv = process.env,
1593
+ ): ArtifactOutboxSettings {
1594
+ const settings = ArtifactOutboxSettingsSchema.parse({
1595
+ ...artifactServiceEnvironment(source),
1596
+ natsUrl: optionalEnvironmentValue("OPENGENI_NATS_URL", source),
1597
+ selfhostedNatsControlUser: optionalEnvironmentValue(
1598
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_USER",
1599
+ source,
1600
+ ),
1601
+ selfhostedNatsControlPassword: optionalEnvironmentValue(
1602
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_PASSWORD",
1603
+ source,
1604
+ ),
1605
+ });
1606
+ dbSearchPath(settings);
1607
+ if (
1608
+ Boolean(settings.selfhostedNatsControlUser) !== Boolean(settings.selfhostedNatsControlPassword)
1609
+ ) {
1610
+ throw new Error("Artifact outbox NATS control user and password must be configured together");
1611
+ }
1612
+ return settings;
1613
+ }
1614
+
1615
+ export function getArtifactMaterializerSettings(
1616
+ source: NodeJS.ProcessEnv = process.env,
1617
+ ): ArtifactMaterializerSettings {
1618
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1619
+ const settings = ArtifactMaterializerSettingsSchema.parse({
1620
+ ...artifactServiceEnvironment(source),
1621
+ objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
1622
+ objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
1623
+ objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
1624
+ objectStorageBackend: optional("OPENGENI_OBJECT_STORAGE_BACKEND"),
1625
+ objectStorageBucket: optional("OPENGENI_OBJECT_STORAGE_BUCKET"),
1626
+ objectStorageRegion: optional("OPENGENI_OBJECT_STORAGE_REGION"),
1627
+ objectStorageAccessKeyId: optional("OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID"),
1628
+ objectStorageSecretAccessKey: optional("OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY"),
1629
+ objectStorageForcePathStyle: optional("OPENGENI_OBJECT_STORAGE_FORCE_PATH_STYLE"),
1630
+ objectStorageAzureConnectionString: optional("OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING"),
1631
+ objectStorageAzureAccountName: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME"),
1632
+ objectStorageAzureAccountKey: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY"),
1633
+ objectStorageAzureEndpoint: optional("OPENGENI_OBJECT_STORAGE_AZURE_ENDPOINT"),
1634
+ objectStorageGcsProjectId: optional("OPENGENI_OBJECT_STORAGE_GCS_PROJECT_ID"),
1635
+ objectStorageGcsCredentialsJson: optional("OPENGENI_OBJECT_STORAGE_GCS_CREDENTIALS_JSON"),
1636
+ objectStorageGcsKeyFilename: optional("OPENGENI_OBJECT_STORAGE_GCS_KEY_FILENAME"),
1637
+ objectStorageGcsApiEndpoint: optional("OPENGENI_OBJECT_STORAGE_GCS_API_ENDPOINT"),
1638
+ });
1639
+ dbSearchPath(settings);
1640
+ validateObjectStorageSettings(settings);
1641
+ return settings;
1642
+ }
1643
+
1335
1644
  export type McpServerConfig = Settings["mcpServers"][number];
1336
1645
 
1337
1646
  export type GoogleDriveProviderRetryOptions = {
@@ -1408,16 +1717,28 @@ export function personalGitHubOAuthCallbackUrl(publicBaseUrl: string | undefined
1408
1717
  export type VoiceInputProviderId =
1409
1718
  | "openai"
1410
1719
  | "azure-openai"
1720
+ | "azure-mai"
1411
1721
  | "codex-subscription"
1412
1722
  | "supergrok-subscription";
1413
1723
 
1414
1724
  export type VoiceInputProviderConfig =
1725
+ | {
1726
+ id: "azure-mai";
1727
+ kind: "azure-mai";
1728
+ pricing: VoiceInputPricing | null;
1729
+ endpoint: string;
1730
+ apiKey: string;
1731
+ apiVersion: string;
1732
+ model: string;
1733
+ }
1415
1734
  | {
1416
1735
  id: "openai";
1417
1736
  kind: "openai";
1418
1737
  apiKey: string;
1419
1738
  baseUrl: string;
1420
1739
  model: string;
1740
+ /** Deployment-funded: Opengeni pays upstream and charges credits. */
1741
+ pricing: VoiceInputPricing | null;
1421
1742
  }
1422
1743
  | {
1423
1744
  id: "azure-openai";
@@ -1427,6 +1748,10 @@ export type VoiceInputProviderConfig =
1427
1748
  apiVersion: string;
1428
1749
  apiKey: string | null;
1429
1750
  adToken: string | null;
1751
+ /** Underlying model (pricing + response shape); defaults to the deployment name. */
1752
+ model: string;
1753
+ /** Deployment-funded: Opengeni pays upstream and charges credits. */
1754
+ pricing: VoiceInputPricing | null;
1430
1755
  }
1431
1756
  | {
1432
1757
  id: "codex-subscription";
@@ -1439,6 +1764,45 @@ export type VoiceInputProviderConfig =
1439
1764
  experimental: true;
1440
1765
  };
1441
1766
 
1767
+ function usableDeploymentSecret(value: string | null | undefined): string | undefined {
1768
+ return isUsableVoiceInputSecret(value) ? value : undefined;
1769
+ }
1770
+
1771
+ /** The deployment's Jev key, or undefined when it is missing or a placeholder. */
1772
+ export function usableJevApiKey(settings: Pick<Settings, "jevApiKey">): string | undefined {
1773
+ return usableDeploymentSecret(settings.jevApiKey);
1774
+ }
1775
+
1776
+ /** Deployment half of agent configuration: hard capability limits. */
1777
+ export function agentConfigDeploymentPolicy(
1778
+ settings: FirstPartyMcpToolPolicySettings & Pick<Settings, "webSearchEnabled">,
1779
+ ): AgentConfigDeploymentLimits {
1780
+ return agentConfigDeploymentLimitsFromAllowlist(
1781
+ resolveFirstPartyMcpToolPolicy(settings).allowed,
1782
+ settings.webSearchEnabled ? {} : { webSearch: "web search is turned off on this server" },
1783
+ );
1784
+ }
1785
+
1786
+ /**
1787
+ * Deployment half of the `code_search` decision. `available` is false when
1788
+ * the mode is off or no usable Jev key is configured; workspaces then cannot
1789
+ * turn it on. `workspaceDefault` applies to workspaces without their own
1790
+ * setting.
1791
+ */
1792
+ export function codeSearchDeploymentPolicy(
1793
+ settings: Pick<Settings, "codeSearchMode" | "jevApiKey">,
1794
+ ): CodeSearchDeploymentPolicy {
1795
+ const available = settings.codeSearchMode !== "off" && usableJevApiKey(settings) !== undefined;
1796
+ if (!available) return { available: false, workspaceDefault: "off" };
1797
+ const workspaceDefault =
1798
+ settings.codeSearchMode === "default_on"
1799
+ ? "on"
1800
+ : settings.codeSearchMode === "experiment"
1801
+ ? "split"
1802
+ : "off";
1803
+ return { available, workspaceDefault };
1804
+ }
1805
+
1442
1806
  /**
1443
1807
  * Reject empty / template secrets so `.env.example` placeholders like
1444
1808
  * `your-key` cannot advertise voice input as available and then 401 upstream.
@@ -1468,6 +1832,58 @@ export function isUsableVoiceInputSecret(value: string | null | undefined): valu
1468
1832
  * whether at least one non-experimental (or probed experimental) provider exists.
1469
1833
  */
1470
1834
  export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInputProviderConfig[] {
1835
+ return voiceInputProviderRegistryWithIssues(settings).providers;
1836
+ }
1837
+
1838
+ /** Why a credentialed deployment-funded voice provider is withheld. */
1839
+ export type VoiceInputPricingIssue = {
1840
+ providerId: "openai" | "azure-openai" | "azure-mai";
1841
+ env: string;
1842
+ reason: "malformed" | "unpriced";
1843
+ message: string;
1844
+ };
1845
+
1846
+ /**
1847
+ * Pricing problems that withhold a credentialed deployment-funded provider.
1848
+ * A malformed PRICING_JSON never fails boot (workers share this config and
1849
+ * must not crash-loop over a voice-only setting); the API logs each issue
1850
+ * once at startup and the provider stays unavailable.
1851
+ */
1852
+ export function voiceInputPricingIssues(settings: Settings): VoiceInputPricingIssue[] {
1853
+ return voiceInputProviderRegistryWithIssues(settings).issues;
1854
+ }
1855
+
1856
+ function voiceInputProviderPricing(
1857
+ settings: Settings,
1858
+ issues: VoiceInputPricingIssue[],
1859
+ providerId: VoiceInputPricingIssue["providerId"],
1860
+ env: string,
1861
+ raw: string | undefined,
1862
+ model: string,
1863
+ ): { usable: true; pricing: VoiceInputPricing | null } | { usable: false } {
1864
+ const resolved = safeResolveVoiceInputPricing(raw, model, env);
1865
+ if (!resolved.ok) {
1866
+ issues.push({ providerId, env, reason: "malformed", message: resolved.error });
1867
+ return { usable: false };
1868
+ }
1869
+ if (!resolved.pricing && voiceInputCreditBillingActive(settings)) {
1870
+ // Never serve unpriced deployment-paid audio on a credit-billed deployment.
1871
+ issues.push({
1872
+ providerId,
1873
+ env,
1874
+ reason: "unpriced",
1875
+ message: `${env} is required: credit billing is active and model "${model}" has no built-in voice price`,
1876
+ });
1877
+ return { usable: false };
1878
+ }
1879
+ return { usable: true, pricing: resolved.pricing };
1880
+ }
1881
+
1882
+ function voiceInputProviderRegistryWithIssues(settings: Settings): {
1883
+ providers: VoiceInputProviderConfig[];
1884
+ issues: VoiceInputPricingIssue[];
1885
+ } {
1886
+ const issues: VoiceInputPricingIssue[] = [];
1471
1887
  const order = settings.voiceInputProviderOrder
1472
1888
  .split(",")
1473
1889
  .map((part) => part.trim())
@@ -1475,6 +1891,7 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1475
1891
  (part): part is VoiceInputProviderId =>
1476
1892
  part === "openai" ||
1477
1893
  part === "azure-openai" ||
1894
+ part === "azure-mai" ||
1478
1895
  part === "codex-subscription" ||
1479
1896
  part === "supergrok-subscription",
1480
1897
  );
@@ -1483,6 +1900,32 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1483
1900
  for (const id of order) {
1484
1901
  if (seen.has(id)) continue;
1485
1902
  seen.add(id);
1903
+ if (id === "azure-mai") {
1904
+ if (
1905
+ !settings.voiceInputMaiEndpoint ||
1906
+ !isUsableVoiceInputSecret(settings.voiceInputMaiApiKey)
1907
+ )
1908
+ continue;
1909
+ const priced = voiceInputProviderPricing(
1910
+ settings,
1911
+ issues,
1912
+ "azure-mai",
1913
+ "OPENGENI_VOICE_INPUT_MAI_PRICING_JSON",
1914
+ settings.voiceInputMaiPricingJson,
1915
+ settings.voiceInputMaiModel,
1916
+ );
1917
+ if (!priced.usable) continue;
1918
+ providers.push({
1919
+ id,
1920
+ kind: "azure-mai",
1921
+ pricing: priced.pricing,
1922
+ endpoint: settings.voiceInputMaiEndpoint.replace(/\/+$/, ""),
1923
+ apiKey: settings.voiceInputMaiApiKey,
1924
+ apiVersion: settings.voiceInputMaiApiVersion,
1925
+ model: settings.voiceInputMaiModel,
1926
+ });
1927
+ continue;
1928
+ }
1486
1929
  if (id === "openai") {
1487
1930
  if (!settings.voiceInputOpenaiEnabled) continue;
1488
1931
  const apiKey = settings.voiceInputOpenaiApiKey ?? settings.openaiApiKey;
@@ -1496,9 +1939,19 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1496
1939
  ) {
1497
1940
  continue;
1498
1941
  }
1942
+ const priced = voiceInputProviderPricing(
1943
+ settings,
1944
+ issues,
1945
+ "openai",
1946
+ "OPENGENI_VOICE_INPUT_OPENAI_PRICING_JSON",
1947
+ settings.voiceInputOpenaiPricingJson,
1948
+ settings.voiceInputOpenaiModel,
1949
+ );
1950
+ if (!priced.usable) continue;
1499
1951
  providers.push({
1500
1952
  id: "openai",
1501
1953
  kind: "openai",
1954
+ pricing: priced.pricing,
1502
1955
  apiKey,
1503
1956
  baseUrl: (
1504
1957
  settings.voiceInputOpenaiBaseUrl ??
@@ -1541,6 +1994,17 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1541
1994
  ) {
1542
1995
  continue;
1543
1996
  }
1997
+ const model = settings.voiceInputAzureModel ?? deployment;
1998
+ const priced = voiceInputProviderPricing(
1999
+ settings,
2000
+ issues,
2001
+ "azure-openai",
2002
+ "OPENGENI_VOICE_INPUT_AZURE_PRICING_JSON",
2003
+ settings.voiceInputAzurePricingJson,
2004
+ model,
2005
+ );
2006
+ if (!priced.usable) continue;
2007
+ const pricing = priced.pricing;
1544
2008
  providers.push({
1545
2009
  id: "azure-openai",
1546
2010
  kind: "azure-openai",
@@ -1549,6 +2013,8 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1549
2013
  apiVersion,
1550
2014
  apiKey: isUsableVoiceInputSecret(apiKey) ? apiKey : null,
1551
2015
  adToken: isUsableVoiceInputSecret(adToken) ? adToken : null,
2016
+ model,
2017
+ pricing,
1552
2018
  });
1553
2019
  continue;
1554
2020
  }
@@ -1575,7 +2041,251 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1575
2041
  });
1576
2042
  }
1577
2043
  }
1578
- return providers;
2044
+ return { providers, issues };
2045
+ }
2046
+
2047
+ /**
2048
+ * Opengeni credit price for one deployment-funded transcription provider.
2049
+ * Rates are upstream list prices in integer USD micros; `marginBps` (500 =
2050
+ * +5%) is added on top, exactly like model pricing. `microsPerMinute` prices
2051
+ * provider-reported audio duration (Whisper, `usage.type: "duration"`) and is
2052
+ * a server-measured WAV duration when provider usage is absent. Token
2053
+ * rates price `usage.type: "tokens"` (gpt-4o-transcribe family); `input`
2054
+ * covers text input tokens and, unless `audioInput` is set, audio tokens.
2055
+ */
2056
+ export type VoiceInputPricing = {
2057
+ microsPerMinute: number;
2058
+ inputMicrosPerMillionTokens?: number | undefined;
2059
+ audioInputMicrosPerMillionTokens?: number | undefined;
2060
+ outputMicrosPerMillionTokens?: number | undefined;
2061
+ marginBps?: number | undefined;
2062
+ };
2063
+
2064
+ const VoiceInputPricingSchema = z
2065
+ .object({
2066
+ microsPerMinute: z.number().int().positive().max(100_000_000),
2067
+ inputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2068
+ audioInputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2069
+ outputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2070
+ marginBps: z.number().int().min(0).max(100_000).optional(),
2071
+ })
2072
+ .strict()
2073
+ .superRefine((pricing, ctx) => {
2074
+ const input = pricing.inputMicrosPerMillionTokens !== undefined;
2075
+ const output = pricing.outputMicrosPerMillionTokens !== undefined;
2076
+ if (input !== output) {
2077
+ ctx.addIssue({
2078
+ code: "custom",
2079
+ message: "inputMicrosPerMillionTokens and outputMicrosPerMillionTokens are set together",
2080
+ });
2081
+ }
2082
+ if (pricing.audioInputMicrosPerMillionTokens !== undefined && !input) {
2083
+ ctx.addIssue({
2084
+ code: "custom",
2085
+ message: "audioInputMicrosPerMillionTokens requires the token input/output rates",
2086
+ });
2087
+ }
2088
+ });
2089
+
2090
+ /**
2091
+ * Built-in list prices (OpenAI API pricing, "Transcription models" table)
2092
+ * plus the default 5% Opengeni margin used for model pricing. Azure Global
2093
+ * Standard deployments use the same list prices; set the provider
2094
+ * PRICING_JSON when a deployment's contracted or regional price differs.
2095
+ */
2096
+ export const defaultVoiceInputPricing: Readonly<Record<string, VoiceInputPricing>> = {
2097
+ // Audio tokens are listed separately from text tokens on the model pages;
2098
+ // set explicitly so the audio rate never silently follows a text-rate edit.
2099
+ "gpt-4o-transcribe": {
2100
+ microsPerMinute: 6_000,
2101
+ inputMicrosPerMillionTokens: 2_500_000,
2102
+ audioInputMicrosPerMillionTokens: 2_500_000,
2103
+ outputMicrosPerMillionTokens: 10_000_000,
2104
+ marginBps: 500,
2105
+ },
2106
+ "gpt-4o-transcribe-diarize": {
2107
+ microsPerMinute: 6_000,
2108
+ inputMicrosPerMillionTokens: 2_500_000,
2109
+ audioInputMicrosPerMillionTokens: 2_500_000,
2110
+ outputMicrosPerMillionTokens: 10_000_000,
2111
+ marginBps: 500,
2112
+ },
2113
+ "gpt-4o-mini-transcribe": {
2114
+ microsPerMinute: 3_000,
2115
+ inputMicrosPerMillionTokens: 1_250_000,
2116
+ audioInputMicrosPerMillionTokens: 1_250_000,
2117
+ outputMicrosPerMillionTokens: 5_000_000,
2118
+ marginBps: 500,
2119
+ },
2120
+ "gpt-transcribe": { microsPerMinute: 4_500, marginBps: 500 },
2121
+ whisper: { microsPerMinute: 6_000, marginBps: 500 },
2122
+ "whisper-1": { microsPerMinute: 6_000, marginBps: 500 },
2123
+ };
2124
+
2125
+ /** Lowercased model with a dated snapshot suffix (`-2025-03-20`) removed. */
2126
+ export function canonicalVoiceInputModel(model: string): string {
2127
+ return model
2128
+ .trim()
2129
+ .toLowerCase()
2130
+ .replace(/-\d{4}-\d{2}-\d{2}$/, "");
2131
+ }
2132
+
2133
+ /** Whisper reports its billed audio duration only in `verbose_json`. */
2134
+ export function voiceInputModelReportsDurationOnly(model: string): boolean {
2135
+ return canonicalVoiceInputModel(model).startsWith("whisper");
2136
+ }
2137
+
2138
+ /** Parse one provider pricing JSON. Throws a configuration error when malformed. */
2139
+ export function parseVoiceInputPricingJson(
2140
+ raw: string | undefined,
2141
+ env = "voice-input pricing JSON",
2142
+ ): VoiceInputPricing | null {
2143
+ if (raw === undefined || raw.trim() === "") return null;
2144
+ let parsed: unknown;
2145
+ try {
2146
+ parsed = JSON.parse(raw);
2147
+ } catch {
2148
+ throw new Error(`${env} must be valid JSON`);
2149
+ }
2150
+ const result = VoiceInputPricingSchema.safeParse(parsed);
2151
+ if (!result.success) {
2152
+ throw new Error(
2153
+ `${env} is invalid: ${result.error.issues.map((issue) => issue.message).join("; ")}`,
2154
+ );
2155
+ }
2156
+ return result.data;
2157
+ }
2158
+
2159
+ /** Explicit provider pricing JSON wins; otherwise the built-in list price for the model. */
2160
+ export function resolveVoiceInputPricing(
2161
+ raw: string | undefined,
2162
+ model: string,
2163
+ ): VoiceInputPricing | null {
2164
+ return (
2165
+ parseVoiceInputPricingJson(raw) ??
2166
+ defaultVoiceInputPricing[canonicalVoiceInputModel(model)] ??
2167
+ null
2168
+ );
2169
+ }
2170
+
2171
+ /** Non-throwing {@link resolveVoiceInputPricing} for registry resolution. */
2172
+ export function safeResolveVoiceInputPricing(
2173
+ raw: string | undefined,
2174
+ model: string,
2175
+ env?: string,
2176
+ ): { ok: true; pricing: VoiceInputPricing | null } | { ok: false; error: string } {
2177
+ let explicit: VoiceInputPricing | null;
2178
+ try {
2179
+ explicit = parseVoiceInputPricingJson(raw, env);
2180
+ } catch (error) {
2181
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
2182
+ }
2183
+ return {
2184
+ ok: true,
2185
+ pricing: explicit ?? defaultVoiceInputPricing[canonicalVoiceInputModel(model)] ?? null,
2186
+ };
2187
+ }
2188
+
2189
+ /** Same predicate as model/credit limits: credit balance and debits are enforced. */
2190
+ export function voiceInputCreditBillingActive(
2191
+ settings: Pick<Settings, "billingMode" | "usageLimitsMode">,
2192
+ ): boolean {
2193
+ return settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
2194
+ }
2195
+
2196
+ /** Provider-reported transcription usage, normalized. Never client-reported. */
2197
+ export type VoiceInputUsage =
2198
+ | {
2199
+ kind: "tokens";
2200
+ inputTokens: number;
2201
+ audioInputTokens: number;
2202
+ textInputTokens: number;
2203
+ outputTokens: number;
2204
+ }
2205
+ | { kind: "duration"; seconds: number };
2206
+
2207
+ export type VoiceInputCost = {
2208
+ /** Upstream list cost before margin, rounded up to whole micros. */
2209
+ providerCostMicros: number;
2210
+ /** Opengeni credit price after margin, rounded up to whole micros. */
2211
+ creditCostMicros: number;
2212
+ };
2213
+
2214
+ /** True when token usage can be priced exactly. */
2215
+ export function voiceInputPricingHasTokenRates(pricing: VoiceInputPricing): boolean {
2216
+ return (
2217
+ pricing.inputMicrosPerMillionTokens !== undefined &&
2218
+ pricing.outputMicrosPerMillionTokens !== undefined
2219
+ );
2220
+ }
2221
+
2222
+ /**
2223
+ * Exact integer credit math. Token usage needs token rates; duration usage
2224
+ * uses `microsPerMinute` at millisecond resolution. Both round up once, after
2225
+ * summing, so a positive use never becomes a free debit.
2226
+ */
2227
+ export function calculateVoiceInputCost(
2228
+ pricing: VoiceInputPricing,
2229
+ usage: VoiceInputUsage,
2230
+ ): VoiceInputCost {
2231
+ let numerator: bigint;
2232
+ let denominator: bigint;
2233
+ if (usage.kind === "tokens") {
2234
+ if (
2235
+ pricing.inputMicrosPerMillionTokens === undefined ||
2236
+ pricing.outputMicrosPerMillionTokens === undefined
2237
+ ) {
2238
+ throw new Error("voice-input token usage requires token pricing");
2239
+ }
2240
+ for (const value of [
2241
+ usage.inputTokens,
2242
+ usage.audioInputTokens,
2243
+ usage.textInputTokens,
2244
+ usage.outputTokens,
2245
+ ]) {
2246
+ if (!Number.isSafeInteger(value) || value < 0) {
2247
+ throw new Error("voice-input token usage must be non-negative integers");
2248
+ }
2249
+ }
2250
+ const audioRate =
2251
+ pricing.audioInputMicrosPerMillionTokens ?? pricing.inputMicrosPerMillionTokens;
2252
+ // Input tokens the provider did not attribute are priced as audio (never cheaper).
2253
+ const unattributed = Math.max(
2254
+ 0,
2255
+ usage.inputTokens - usage.audioInputTokens - usage.textInputTokens,
2256
+ );
2257
+ numerator =
2258
+ BigInt(usage.audioInputTokens + unattributed) * BigInt(audioRate) +
2259
+ BigInt(usage.textInputTokens) * BigInt(pricing.inputMicrosPerMillionTokens) +
2260
+ BigInt(usage.outputTokens) * BigInt(pricing.outputMicrosPerMillionTokens);
2261
+ denominator = 1_000_000n;
2262
+ } else {
2263
+ if (!Number.isFinite(usage.seconds) || usage.seconds < 0) {
2264
+ throw new Error("voice-input duration must be a non-negative number of seconds");
2265
+ }
2266
+ numerator = BigInt(Math.ceil(usage.seconds * 1_000)) * BigInt(pricing.microsPerMinute);
2267
+ denominator = 60_000n;
2268
+ }
2269
+ const margin = BigInt(10_000 + (pricing.marginBps ?? 0));
2270
+ const provider = (numerator + denominator - 1n) / denominator;
2271
+ const scaled = denominator * 10_000n;
2272
+ const credit = (numerator * margin + scaled - 1n) / scaled;
2273
+ if (credit > BigInt(Number.MAX_SAFE_INTEGER)) {
2274
+ throw new Error("voice-input cost exceeds the supported billing range");
2275
+ }
2276
+ return { providerCostMicros: Number(provider), creditCostMicros: Number(credit) };
2277
+ }
2278
+
2279
+ /**
2280
+ * Deployment-funded providers that have credentials but are withheld because
2281
+ * credit billing is active and no price is known. The API logs these (and
2282
+ * malformed pricing, see {@link voiceInputPricingIssues}) once at startup so
2283
+ * an operator sees why voice input stayed unavailable.
2284
+ */
2285
+ export function unpricedVoiceInputProviders(settings: Settings): VoiceInputProviderId[] {
2286
+ return voiceInputPricingIssues(settings).flatMap((issue) =>
2287
+ issue.reason === "unpriced" ? [issue.providerId] : [],
2288
+ );
1579
2289
  }
1580
2290
 
1581
2291
  /** True when the deployment has at least one supported (non-experimental) provider. */
@@ -1630,6 +2340,27 @@ export type ModelUsageCostBreakdown = {
1630
2340
  creditCostMicros: number;
1631
2341
  };
1632
2342
 
2343
+ export type ModelListCostClassesMicros = {
2344
+ uncachedInput: number;
2345
+ cacheRead: number;
2346
+ cacheWrite: number;
2347
+ output: number;
2348
+ };
2349
+
2350
+ export type ModelListUsageCostSnapshot = ModelUsageCostBreakdown & {
2351
+ /** Captured upstream list costs, summing to providerCostMicros, never debits. */
2352
+ listByClassMicros: ModelListCostClassesMicros | null;
2353
+ /** True only when a returned class split includes latency rounding allocation. */
2354
+ listByClassApprox: boolean;
2355
+ };
2356
+
2357
+ export type ModelRecordedListCostAllocation = {
2358
+ /** Approximate attribution of a stored upstream total, never a new price. */
2359
+ listByClassMicros: ModelListCostClassesMicros | null;
2360
+ /** True for every eligible historical allocation, including known zero. */
2361
+ listByClassApprox: boolean;
2362
+ };
2363
+
1633
2364
  export type StaticUsageLimitsConfig = StaticUsageLimits;
1634
2365
  export type EntitlementsConfig = Entitlements;
1635
2366
 
@@ -1800,7 +2531,7 @@ export type ModelExecutionLimitsV1 = {
1800
2531
 
1801
2532
  export type CredentialSourceV1 =
1802
2533
  | { kind: "deployment"; mechanism: "api_key" | "azure_ad_bearer" | "none" }
1803
- | { kind: "connected_subscription"; provider: "codex" | "xai" }
2534
+ | { kind: "connected_subscription"; provider: "codex" | "xai" | "claude" }
1804
2535
  | { kind: "workspace_connection"; mechanism: "api_key" }
1805
2536
  | { kind: "organization_connection"; mechanism: "api_key" };
1806
2537
 
@@ -1816,7 +2547,7 @@ export type BillingAttributionV1 = {
1816
2547
  * wired as "chat" because its beta Responses endpoint echoes input back and
1817
2548
  * silently no-ops hosted tools (see docs/model-providers.md).
1818
2549
  */
1819
- export const ModelProviderApi = z.enum(["responses", "chat"]);
2550
+ export const ModelProviderApi = z.enum(["responses", "chat", "anthropic-messages"]);
1820
2551
  export type ModelProviderApi = z.infer<typeof ModelProviderApi>;
1821
2552
 
1822
2553
  /**
@@ -1841,12 +2572,25 @@ export const RegistryProviderKind = z.enum([
1841
2572
  "vercel-gateway-managed",
1842
2573
  "vercel-gateway-workspace",
1843
2574
  "vercel-gateway-organization",
2575
+ "direct-openai-workspace",
2576
+ "direct-azure-workspace",
1844
2577
  "openrouter-workspace",
1845
2578
  "openrouter-organization",
2579
+ "anthropic-organization",
2580
+ "claude-subscription-organization",
2581
+ "anthropic-workspace",
2582
+ "claude-subscription-workspace",
1846
2583
  ]);
1847
2584
  export type RegistryProviderKind = z.infer<typeof RegistryProviderKind>;
1848
2585
 
1849
2586
  /** A single model exposed by a registry provider. */
2587
+ const ModelLogoUrlSchema = z
2588
+ .string()
2589
+ .refine(
2590
+ (value) => modelLogoUrl({ id: "", logoUrl: value }) !== null,
2591
+ "model logoUrl must be an HTTPS URL without embedded credentials, at most 2048 characters",
2592
+ );
2593
+
1850
2594
  const RegistryModelSchema = z
1851
2595
  .object({
1852
2596
  id: z.string().min(1), // canonical OpenGeni product id
@@ -1854,6 +2598,7 @@ const RegistryModelSchema = z
1854
2598
  aliases: z.array(z.string().min(1)).optional(), // accepted input only; never sent upstream
1855
2599
  label: z.string().min(1).optional(), // display name; defaults to id
1856
2600
  shortLabel: z.string().min(1).max(64).optional(), // compact UI label; optional
2601
+ logoUrl: ModelLogoUrlSchema.optional(),
1857
2602
  contextWindowTokens: z.number().int().positive().optional(),
1858
2603
  effectiveContextWindowTokens: z.number().int().positive().optional(),
1859
2604
  autoCompactTokenLimit: z.number().int().positive().optional(),
@@ -1896,12 +2641,51 @@ const RegistryModelSchema = z
1896
2641
  });
1897
2642
 
1898
2643
  /** A non-built-in provider declared by the host via OPENGENI_MODEL_PROVIDERS_JSON. */
2644
+ export const ClaudeSubscriptionIdentity = z
2645
+ .object({
2646
+ accountUuid: z.union([z.string().uuid(), z.literal("")]),
2647
+ deviceId: z.string().regex(/^[a-f0-9]{64}$/),
2648
+ })
2649
+ .strict();
2650
+ export type ClaudeSubscriptionIdentity = z.infer<typeof ClaudeSubscriptionIdentity>;
2651
+ export const ClaudeSubscriptionCredential = z
2652
+ .object({
2653
+ version: z.literal(1),
2654
+ token: z.string().regex(/^sk-ant-oat[0-9]+-\S+$/),
2655
+ identity: ClaudeSubscriptionIdentity,
2656
+ oauth: z
2657
+ .object({
2658
+ refreshToken: z.string().min(1).max(16384),
2659
+ expiresAt: z.string().datetime(),
2660
+ scopes: z.array(z.string().min(1).max(256)).min(1).max(32),
2661
+ })
2662
+ .strict()
2663
+ .optional(),
2664
+ })
2665
+ .strict();
2666
+ const AnthropicProviderOptions = z.object({
2667
+ identity: ClaudeSubscriptionIdentity.optional(),
2668
+ // Native connection provenance only; never included in Anthropic request bodies.
2669
+ credentialBinding: z
2670
+ .object({
2671
+ connectionId: z.string().uuid(),
2672
+ credentialVersion: z.number().int().positive(),
2673
+ })
2674
+ .strict()
2675
+ .optional(),
2676
+ auth: z.enum(["api-key", "oauth"]).default("api-key"),
2677
+ cacheTtl: z.enum(["5m", "1h", "off"]).default("5m"),
2678
+ maxOutputTokens: z.number().int().positive().default(32000),
2679
+ streamIdleTimeoutMs: z.number().int().positive().default(600000),
2680
+ });
2681
+
1899
2682
  const RegistryProviderSchema = z
1900
2683
  .object({
1901
2684
  kind: RegistryProviderKind.default("api-key"),
1902
2685
  id: z.string().min(1).regex(registryId), // stable provider id, e.g. "fireworks"
1903
2686
  label: z.string().min(1).optional(),
1904
2687
  api: ModelProviderApi.default("chat"),
2688
+ anthropic: AnthropicProviderOptions.optional(),
1905
2689
  wireProfile: ModelProviderWireProfile.default("openai"),
1906
2690
  baseUrl: z.string().url(),
1907
2691
  apiKey: z.string().optional(), // inline key (pragmatic) ...
@@ -1989,6 +2773,10 @@ const RESERVED_MODEL_PROVIDER_IDS = new Set<string>([
1989
2773
  OPENROUTER_PROVIDER_ID,
1990
2774
  WORKSPACE_OPENROUTER_PROVIDER_ID,
1991
2775
  ORGANIZATION_OPENROUTER_PROVIDER_ID,
2776
+ "organization-anthropic",
2777
+ "organization-claude-subscription",
2778
+ "workspace-anthropic",
2779
+ "workspace-claude-subscription",
1992
2780
  ]);
1993
2781
 
1994
2782
  export const ModelCostClass = z.enum(["free", "credits"]);
@@ -2049,6 +2837,7 @@ export const GatewayCatalogModel = z
2049
2837
  upstreamModelId: z.string().min(1),
2050
2838
  label: z.string().min(1),
2051
2839
  shortLabel: z.string().min(1).max(64).optional(),
2840
+ logoUrl: ModelLogoUrlSchema.optional(),
2052
2841
  providers: z.array(z.string().min(1)).min(1),
2053
2842
  implicitCaching: z.boolean().default(false),
2054
2843
  vision: z.boolean().default(false),
@@ -2069,6 +2858,7 @@ export const OpenRouterCatalogModel = z
2069
2858
  upstreamModelId: z.string().min(1).endsWith(":free"),
2070
2859
  label: z.string().min(1),
2071
2860
  shortLabel: z.string().min(1).max(64).optional(),
2861
+ logoUrl: ModelLogoUrlSchema.optional(),
2072
2862
  aliases: z.array(z.string().min(1)).default([]),
2073
2863
  capabilities: ModelCapabilitiesV1Schema,
2074
2864
  contextWindowTokens: z.number().int().positive().optional(),
@@ -2130,6 +2920,28 @@ const DeploymentGatewayCatalogModelSchema = GatewayCatalogModel.safeExtend({
2130
2920
  pricing: z.never().optional(),
2131
2921
  }).strict();
2132
2922
 
2923
+ const CodexCatalogModelSchema = RegistryModelSchema.safeExtend({
2924
+ retired: z.boolean().optional(),
2925
+ id: z.string().regex(/^codex\/[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2926
+ upstreamModelId: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2927
+ capabilities: ModelCapabilitiesV1Schema,
2928
+ pricing: z.never().optional(),
2929
+ })
2930
+ .strict()
2931
+ .superRefine((model, context) => {
2932
+ if (model.id !== `${CODEX_MODEL_ID_PREFIX}${model.upstreamModelId}`) {
2933
+ context.addIssue({
2934
+ code: "custom",
2935
+ path: ["id"],
2936
+ message: "Codex product id must match its upstream model slug",
2937
+ });
2938
+ }
2939
+ })
2940
+ .transform((model) => ({
2941
+ ...model,
2942
+ capabilities: reviewedCodexCatalogCapabilities(model.upstreamModelId, model.capabilities),
2943
+ }));
2944
+
2133
2945
  export const ModelCatalogDocument = z
2134
2946
  .object({
2135
2947
  schemaVersion: z.literal(1),
@@ -2137,8 +2949,9 @@ export const ModelCatalogDocument = z
2137
2949
  * fallback for existing documents; operators should set this explicitly
2138
2950
  * when cutting over a registry or connected-subscription default. */
2139
2951
  defaultModel: z.string().min(1).optional(),
2140
- builtInModels: z.array(z.string().min(1)).min(1),
2952
+ builtInModels: z.array(z.string().min(1)),
2141
2953
  registryProviders: z.array(DeploymentRegistryProviderSchema).default([]),
2954
+ codexModels: z.array(CodexCatalogModelSchema).optional(),
2142
2955
  gatewayModels: z.array(DeploymentGatewayCatalogModelSchema).default([]),
2143
2956
  openrouterModels: z.array(OpenRouterCatalogModel).default([]),
2144
2957
  modelNotes: z.record(z.string().min(1), ModelNote).default({}),
@@ -2149,6 +2962,13 @@ export const ModelCatalogDocument = z
2149
2962
  })
2150
2963
  .strict()
2151
2964
  .superRefine((document, context) => {
2965
+ if (document.builtInModels.length === 0 && !document.defaultModel) {
2966
+ context.addIssue({
2967
+ code: "custom",
2968
+ path: ["defaultModel"],
2969
+ message: "catalogs without built-in models require an explicit default model",
2970
+ });
2971
+ }
2152
2972
  const productIds = new Set<string>();
2153
2973
  const providerIds = new Set<string>();
2154
2974
  const gatewayUpstreamIds = new Set<string>();
@@ -2170,8 +2990,22 @@ export const ModelCatalogDocument = z
2170
2990
  productIds.add(id);
2171
2991
  };
2172
2992
  document.builtInModels.forEach((id, index) => add(id, ["builtInModels", index]));
2993
+ document.codexModels?.forEach((model, index) => add(model.id, ["codexModels", index, "id"]));
2994
+ if (
2995
+ document.codexModels?.some((model) => model.retired && model.id === document.defaultModel)
2996
+ ) {
2997
+ context.addIssue({
2998
+ code: "custom",
2999
+ path: ["defaultModel"],
3000
+ message: "a retired model cannot be the deployment default",
3001
+ });
3002
+ }
2173
3003
  document.registryProviders.forEach((provider, providerIndex) => {
2174
- if (RESERVED_MODEL_PROVIDER_IDS.has(provider.id)) {
3004
+ if (
3005
+ RESERVED_MODEL_PROVIDER_IDS.has(provider.id) ||
3006
+ provider.id.startsWith("workspace-openai-") ||
3007
+ provider.id.startsWith("workspace-azure-openai-")
3008
+ ) {
2175
3009
  context.addIssue({
2176
3010
  code: "custom",
2177
3011
  path: ["registryProviders", providerIndex, "id"],
@@ -2219,7 +3053,10 @@ export const ModelCatalogDocument = z
2219
3053
  if (
2220
3054
  document.defaultModel &&
2221
3055
  !productIds.has(document.defaultModel) &&
2222
- !document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX) &&
3056
+ !(
3057
+ document.codexModels === undefined &&
3058
+ document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX)
3059
+ ) &&
2223
3060
  !document.defaultModel.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX)
2224
3061
  ) {
2225
3062
  context.addIssue({
@@ -2304,6 +3141,8 @@ export function applyModelCatalogDocument(settings: Settings, rawDocument: unkno
2304
3141
  ),
2305
3142
  resolvedGatewayModelsJson: JSON.stringify(document.gatewayModels),
2306
3143
  resolvedOpenRouterModelsJson: JSON.stringify(document.openrouterModels),
3144
+ resolvedCodexModelsJson:
3145
+ document.codexModels === undefined ? undefined : JSON.stringify(document.codexModels),
2307
3146
  modelNotesJson: JSON.stringify(document.modelNotes),
2308
3147
  };
2309
3148
  return resolved;
@@ -2326,6 +3165,7 @@ export type IntegrationOAuthClientConfig = z.infer<typeof IntegrationOAuthClient
2326
3165
  * replacement.
2327
3166
  */
2328
3167
  export interface ResolvedModelProvider {
3168
+ anthropic?: z.infer<typeof AnthropicProviderOptions> | undefined;
2329
3169
  id: string; // "openai" | "azure" | registry id
2330
3170
  label: string;
2331
3171
  kind: RegistryProviderKind | "openrouter-managed";
@@ -2354,6 +3194,8 @@ export interface ConfiguredModel {
2354
3194
  label: string;
2355
3195
  /** Optional curated compact label for dense UI (e.g. mobile composer). */
2356
3196
  shortLabel?: string | undefined;
3197
+ /** Optional HTTPS maker logo; display metadata does not change execution identity. */
3198
+ logoUrl?: string | undefined;
2357
3199
  providerId: string;
2358
3200
  providerLabel: string;
2359
3201
  api: ModelProviderApi;
@@ -2381,11 +3223,31 @@ export interface ConfiguredModel {
2381
3223
  hostedWebSearch: boolean;
2382
3224
  }
2383
3225
 
2384
- export const VERCEL_AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh/v1" as const;
2385
- export const VERCEL_AI_GATEWAY_AI_SDK_BASE_URL = "https://ai-gateway.vercel.sh/v4/ai" as const;
2386
- export const VERCEL_AI_GATEWAY_CONNECTION_DOMAIN = "ai-gateway.vercel.sh" as const;
2387
- export const VERCEL_AI_GATEWAY_CONNECTION_ROLE = "vercel_ai_gateway" as const;
2388
- export const WORKSPACE_OPENROUTER_CONNECTION_DOMAIN = "openrouter.ai" as const;
3226
+ /**
3227
+ * Whether a resolved route is the deployment-funded OpenRouter free tier: the
3228
+ * managed OpenRouter provider, which sends every workspace's requests with the
3229
+ * deployment's one OpenRouter key, serving an upstream `:free` variant (the
3230
+ * curated catalog admits nothing else). OpenRouter limits free-variant
3231
+ * requests per account per minute and per day, so each request on this route
3232
+ * spends quota that every turn on the deployment shares. A workspace or
3233
+ * organization OpenRouter connection uses its own account and is not this
3234
+ * route.
3235
+ */
3236
+ export function isManagedOpenRouterFreeRoute(route: {
3237
+ provider: Pick<ResolvedModelProvider, "kind">;
3238
+ configured: Pick<ConfiguredModel, "upstreamModelId">;
3239
+ }): boolean {
3240
+ return (
3241
+ route.provider.kind === "openrouter-managed" &&
3242
+ route.configured.upstreamModelId.endsWith(":free")
3243
+ );
3244
+ }
3245
+
3246
+ export const VERCEL_AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh/v1" as const;
3247
+ export const VERCEL_AI_GATEWAY_AI_SDK_BASE_URL = "https://ai-gateway.vercel.sh/v4/ai" as const;
3248
+ export const VERCEL_AI_GATEWAY_CONNECTION_DOMAIN = "ai-gateway.vercel.sh" as const;
3249
+ export const VERCEL_AI_GATEWAY_CONNECTION_ROLE = "vercel_ai_gateway" as const;
3250
+ export const WORKSPACE_OPENROUTER_CONNECTION_DOMAIN = "openrouter.ai" as const;
2389
3251
  export const WORKSPACE_OPENROUTER_CONNECTION_ROLE = "openrouter" as const;
2390
3252
 
2391
3253
  export const CODEX_REALTIME_MODEL_ID = "gpt-live-1-boulder-alpha" as const;
@@ -2587,6 +3449,73 @@ export function configuredOpenRouterOrganizationProductModelIds(settings: Settin
2587
3449
  * llm-prices.com as a ground-truth canary; it does not generate this table.
2588
3450
  */
2589
3451
  export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
3452
+ "gpt-6-astra": {
3453
+ default: {
3454
+ // OpenAI list price: $10 / $1 cached / $12.50 cache write / $50 output.
3455
+ inputMicrosPerMillionTokens: 10_000_000,
3456
+ cachedInputMicrosPerMillionTokens: 1_000_000,
3457
+ cacheWriteMicrosPerMillionTokens: 12_500_000,
3458
+ outputMicrosPerMillionTokens: 50_000_000,
3459
+ marginBps: 500,
3460
+ },
3461
+ inputTokenTiers: [
3462
+ {
3463
+ // Prompts with more than 272K input tokens use 2x input and 1.5x output.
3464
+ minimumInputTokens: 272_001,
3465
+ pricing: {
3466
+ inputMicrosPerMillionTokens: 20_000_000,
3467
+ cachedInputMicrosPerMillionTokens: 2_000_000,
3468
+ cacheWriteMicrosPerMillionTokens: 25_000_000,
3469
+ outputMicrosPerMillionTokens: 75_000_000,
3470
+ marginBps: 500,
3471
+ },
3472
+ },
3473
+ ],
3474
+ },
3475
+ "gpt-6-sol": {
3476
+ default: {
3477
+ // OpenAI list price: $2 / $0.20 cached / $2.50 cache write / $10 output.
3478
+ inputMicrosPerMillionTokens: 2_000_000,
3479
+ cachedInputMicrosPerMillionTokens: 200_000,
3480
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3481
+ outputMicrosPerMillionTokens: 10_000_000,
3482
+ marginBps: 500,
3483
+ },
3484
+ inputTokenTiers: [
3485
+ {
3486
+ minimumInputTokens: 272_001,
3487
+ pricing: {
3488
+ inputMicrosPerMillionTokens: 4_000_000,
3489
+ cachedInputMicrosPerMillionTokens: 400_000,
3490
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3491
+ outputMicrosPerMillionTokens: 15_000_000,
3492
+ marginBps: 500,
3493
+ },
3494
+ },
3495
+ ],
3496
+ },
3497
+ "gpt-6-luna": {
3498
+ default: {
3499
+ // OpenAI list price: $0.10 / $0.01 cached / $0.125 cache write / $0.50 output.
3500
+ inputMicrosPerMillionTokens: 100_000,
3501
+ cachedInputMicrosPerMillionTokens: 10_000,
3502
+ cacheWriteMicrosPerMillionTokens: 125_000,
3503
+ outputMicrosPerMillionTokens: 500_000,
3504
+ marginBps: 500,
3505
+ },
3506
+ inputTokenTiers: [
3507
+ {
3508
+ minimumInputTokens: 272_001,
3509
+ pricing: {
3510
+ inputMicrosPerMillionTokens: 200_000,
3511
+ cachedInputMicrosPerMillionTokens: 20_000,
3512
+ cacheWriteMicrosPerMillionTokens: 250_000,
3513
+ outputMicrosPerMillionTokens: 750_000,
3514
+ marginBps: 500,
3515
+ },
3516
+ },
3517
+ ],
3518
+ },
2590
3519
  "gpt-5.6-sol": {
2591
3520
  default: {
2592
3521
  // Promotional OpenAI pricing, guaranteed through at least 2026-11-21.
@@ -2687,6 +3616,113 @@ export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
2687
3616
  },
2688
3617
  };
2689
3618
 
3619
+ /** Reviewed comparison rates only; never debit or execution-definition defaults. */
3620
+ export const reviewedModelListPricing: Record<string, ModelPricingScheduleV1> = {
3621
+ // Reviewed 2026-10-03 against https://developers.openai.com/api/docs/pricing
3622
+ // and https://ai-gateway.vercel.sh/v1/models. GPT-6.1 Sol's cache-read rate
3623
+ // is 5% of input, not GPT-6 Sol's 10%. The long-context boundary is >272K.
3624
+ "gpt-6.1-sol": {
3625
+ default: {
3626
+ inputMicrosPerMillionTokens: 2_000_000,
3627
+ cachedInputMicrosPerMillionTokens: 100_000,
3628
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3629
+ outputMicrosPerMillionTokens: 10_000_000,
3630
+ marginBps: 500,
3631
+ },
3632
+ inputTokenTiers: [
3633
+ {
3634
+ minimumInputTokens: 272_001,
3635
+ pricing: {
3636
+ inputMicrosPerMillionTokens: 4_000_000,
3637
+ cachedInputMicrosPerMillionTokens: 200_000,
3638
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3639
+ outputMicrosPerMillionTokens: 15_000_000,
3640
+ marginBps: 500,
3641
+ },
3642
+ },
3643
+ ],
3644
+ },
3645
+ // xAI Standard API list rates, reviewed 2026-10-03:
3646
+ // https://docs.x.ai/developers/models/grok-4.5 (and grok-4.6 / grok-4.7).
3647
+ // Preserve the native API's established >=200K boundary. Gateway's separately
3648
+ // configured >200K schedule is not interchangeable with this native schedule.
3649
+ // xAI's automatic caching has no separate cache-write surcharge.
3650
+ ...Object.fromEntries(
3651
+ (
3652
+ [
3653
+ ["grok-4.5", 300_000],
3654
+ ["grok-4.6", 500_000],
3655
+ ["grok-4.7", 500_000],
3656
+ ] as const
3657
+ ).map(([model, cachedInput]) => [
3658
+ model,
3659
+ {
3660
+ default: {
3661
+ inputMicrosPerMillionTokens: 2_000_000,
3662
+ cachedInputMicrosPerMillionTokens: cachedInput,
3663
+ outputMicrosPerMillionTokens: 6_000_000,
3664
+ marginBps: 500,
3665
+ },
3666
+ inputTokenTiers: [
3667
+ {
3668
+ minimumInputTokens: 200_000,
3669
+ pricing: {
3670
+ inputMicrosPerMillionTokens: 4_000_000,
3671
+ cachedInputMicrosPerMillionTokens: cachedInput * 2,
3672
+ outputMicrosPerMillionTokens: 12_000_000,
3673
+ marginBps: 500,
3674
+ },
3675
+ },
3676
+ ],
3677
+ },
3678
+ ]),
3679
+ ),
3680
+ // Every reviewed native Claude profile. Standard/global prices include
3681
+ // 5-minute cache writes; 1-hour native routes are projected separately.
3682
+ // https://platform.claude.com/docs/en/about-claude/pricing (2026-10-03).
3683
+ // Opus 5.5 cache reads are 5%, unlike the older models' 10%; none of these
3684
+ // models has a long-context premium.
3685
+ ...Object.fromEntries(
3686
+ (
3687
+ [
3688
+ ["claude-opus-5-5", 4_000_000, 200_000, 5_000_000, 20_000_000],
3689
+ ["claude-sonnet-5-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3690
+ ["claude-opus-5", 5_000_000, 500_000, 6_250_000, 25_000_000],
3691
+ ["claude-sonnet-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3692
+ ["claude-opus-4-8", 5_000_000, 500_000, 6_250_000, 25_000_000],
3693
+ ["claude-opus-4-7", 5_000_000, 500_000, 6_250_000, 25_000_000],
3694
+ ["claude-opus-4-6", 5_000_000, 500_000, 6_250_000, 25_000_000],
3695
+ ["claude-sonnet-4-6", 3_000_000, 300_000, 3_750_000, 15_000_000],
3696
+ ["claude-haiku-4-5-20251001", 1_000_000, 100_000, 1_250_000, 5_000_000],
3697
+ ] as const
3698
+ ).map(([model, input, cachedInput, cacheWrite, output]) => [
3699
+ model,
3700
+ {
3701
+ default: {
3702
+ inputMicrosPerMillionTokens: input,
3703
+ cachedInputMicrosPerMillionTokens: cachedInput,
3704
+ cacheWriteMicrosPerMillionTokens: cacheWrite,
3705
+ outputMicrosPerMillionTokens: output,
3706
+ marginBps: 500,
3707
+ },
3708
+ },
3709
+ ]),
3710
+ ),
3711
+ };
3712
+
3713
+ // Explicitly priced free variant, not an unknown rate. List-only metadata
3714
+ // preserves previously accepted free-route execution definitions.
3715
+ // https://openrouter.ai/api/v1/models, verified 2026-10-03.
3716
+ const reviewedFreeOpenRouterListPricing: ModelPricingScheduleV1 = {
3717
+ default: {
3718
+ inputMicrosPerMillionTokens: 0,
3719
+ cachedInputMicrosPerMillionTokens: 0,
3720
+ cacheWriteMicrosPerMillionTokens: 0,
3721
+ outputMicrosPerMillionTokens: 0,
3722
+ marginBps: 500,
3723
+ },
3724
+ };
3725
+
2690
3726
  // --- backend-gated required-credential table (the single source of truth) ---
2691
3727
  // Each sandbox backend declares ONLY its own required credentials: a deployment
2692
3728
  // configured for `sandboxBackend=modal` must carry the Modal token, but a
@@ -2778,6 +3814,9 @@ function optionalEnvironmentValue(name: string, source: NodeJS.ProcessEnv): stri
2778
3814
  return value && value.trim().length > 0 ? value : undefined;
2779
3815
  }
2780
3816
 
3817
+ // getSettings runs per request in some processes; say this once per process.
3818
+ let idleCommandContainmentDisabledWarned = false;
3819
+
2781
3820
  export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2782
3821
  const optional = (name: string): string | undefined => optionalEnvironmentValue(name, source);
2783
3822
  const modelCatalogSource = optional("OPENGENI_MODEL_CATALOG_SOURCE");
@@ -2793,6 +3832,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2793
3832
  optional("GITHUB_SHA"),
2794
3833
  serverVersion: optional("OPENGENI_SERVER_VERSION"),
2795
3834
  databaseUrl: optional("OPENGENI_DATABASE_URL"),
3835
+ apiDatabasePoolMax: optional("OPENGENI_API_DATABASE_POOL_MAX"),
2796
3836
  dbSchema: optional("OPENGENI_DB_SCHEMA"),
2797
3837
  rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
2798
3838
  runtimeDatabaseRole: optional("OPENGENI_RUNTIME_DATABASE_ROLE"),
@@ -2826,31 +3866,39 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2826
3866
  optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
2827
3867
  observabilityOtlpHeaders:
2828
3868
  optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
3869
+ observabilityDiagnosticsEndpoint: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_ENDPOINT"),
3870
+ observabilityDiagnosticsHeaders: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_HEADERS"),
2829
3871
  analyticsEnabled: optional("OPENGENI_ANALYTICS_ENABLED"),
2830
3872
  analyticsConsentRequired: optional("OPENGENI_ANALYTICS_CONSENT_REQUIRED"),
2831
3873
  analyticsReoClientId: optional("OPENGENI_ANALYTICS_REO_CLIENT_ID"),
2832
3874
  analyticsPosthogProjectKey: optional("OPENGENI_ANALYTICS_POSTHOG_PROJECT_KEY"),
2833
3875
  analyticsPosthogHost: optional("OPENGENI_ANALYTICS_POSTHOG_HOST"),
2834
3876
  analyticsGa4MeasurementId: optional("OPENGENI_ANALYTICS_GA4_MEASUREMENT_ID"),
3877
+ legalPrivacyPolicyUrl: optional("OPENGENI_LEGAL_PRIVACY_POLICY_URL"),
3878
+ legalTermsOfServiceUrl: optional("OPENGENI_LEGAL_TERMS_OF_SERVICE_URL"),
3879
+ supportEmail: optional("OPENGENI_SUPPORT_EMAIL"),
2835
3880
  publicBaseUrl: optional("OPENGENI_PUBLIC_BASE_URL"),
3881
+ documentationUrl: optional("OPENGENI_DOCUMENTATION_URL"),
2836
3882
  mcpOauthEnabled: optional("OPENGENI_MCP_OAUTH_ENABLED"),
2837
- mcpOauthTrustedProxyHops: optional("OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS"),
3883
+ apiTrustedProxyHops: optional("OPENGENI_API_TRUSTED_PROXY_HOPS"),
3884
+ apiTrustedProxyCidrs: optional("OPENGENI_API_TRUSTED_PROXY_CIDRS"),
2838
3885
  webBaseUrl: optional("OPENGENI_WEB_BASE_URL"),
2839
3886
  agentReleasesBaseUrl: optional("OPENGENI_AGENT_RELEASES_BASE_URL"),
2840
3887
  agentStableVersion: optional("OPENGENI_AGENT_STABLE_VERSION"),
2841
3888
  agentBetaVersion: optional("OPENGENI_AGENT_BETA_VERSION"),
2842
3889
  productAccessMode: optional("OPENGENI_PRODUCT_ACCESS_MODE"),
2843
- organizationTenancyCanonicalActivationEnabled: optional(
2844
- "OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED",
2845
- ),
2846
3890
  billingMode: optional("OPENGENI_BILLING_MODE"),
3891
+ verifiedSignupTrialCreditsEnabled: optional("OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED"),
3892
+ creditPromotionPolicy: optional("OPENGENI_CREDIT_PROMOTION_POLICY_JSON"),
2847
3893
  entitlementsMode: optional("OPENGENI_ENTITLEMENTS_MODE"),
2848
3894
  usageLimitsMode: optional("OPENGENI_USAGE_LIMITS_MODE"),
3895
+ usageAllowancesEnabled: optional("OPENGENI_USAGE_ALLOWANCES_ENABLED"),
2849
3896
  staticEntitlementsJson: optional("OPENGENI_STATIC_ENTITLEMENTS_JSON"),
2850
3897
  staticUsageLimitsJson: optional("OPENGENI_STATIC_USAGE_LIMITS_JSON"),
2851
3898
  delegationSecret: optional("OPENGENI_DELEGATION_SECRET"),
2852
3899
  defaultFirstPartyMcpTools: optional("OPENGENI_DEFAULT_FIRST_PARTY_MCP_TOOLS"),
2853
3900
  allowedFirstPartyMcpTools: optional("OPENGENI_ALLOWED_FIRST_PARTY_MCP_TOOLS"),
3901
+ artifactMaterializerDeployed: optional("OPENGENI_ARTIFACT_MATERIALIZER_DEPLOYED"),
2854
3902
  streamTokenSecret: optional("OPENGENI_STREAM_TOKEN_SECRET"),
2855
3903
  streamControlEnabled: optional("OPENGENI_STREAM_CONTROL_ENABLED"),
2856
3904
  workDiscoveryEnabled: optional("OPENGENI_WORK_DISCOVERY_ENABLED"),
@@ -2865,13 +3913,16 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2865
3913
  environmentsEncryptionKey: optional("OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY"),
2866
3914
  integrationsEnabled: optional("OPENGENI_INTEGRATIONS_ENABLED"),
2867
3915
  integrationsStateSecret: optional("OPENGENI_INTEGRATIONS_STATE_SECRET"),
3916
+ integrationsOauthShortStateEnabled: optional("OPENGENI_INTEGRATIONS_OAUTH_SHORT_STATE_ENABLED"),
2868
3917
  integrationsAllowPrivateNetworkTargets: optional(
2869
3918
  "OPENGENI_INTEGRATIONS_ALLOW_PRIVATE_NETWORK_TARGETS",
2870
3919
  ),
2871
3920
  integrationsOauthClientsJson: optional("OPENGENI_INTEGRATIONS_OAUTH_CLIENTS_JSON"),
3921
+ gmailWatchTopicName: optional("OPENGENI_GMAIL_WATCH_TOPIC_NAME"),
2872
3922
  slackClientId: optional("OPENGENI_SLACK_CLIENT_ID"),
2873
3923
  slackClientSecret: optional("OPENGENI_SLACK_CLIENT_SECRET"),
2874
3924
  slackSigningSecret: optional("OPENGENI_SLACK_SIGNING_SECRET"),
3925
+ slackAccessMode: optional("OPENGENI_SLACK_ACCESS_MODE"),
2875
3926
  slackBotDisplayName: optional("OPENGENI_SLACK_BOT_DISPLAY_NAME"),
2876
3927
  slackCommand: optional("OPENGENI_SLACK_COMMAND"),
2877
3928
  googleDriveClientId: optional("OPENGENI_GOOGLE_DRIVE_CLIENT_ID"),
@@ -2906,10 +3957,6 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2906
3957
  goalIdleBackoffMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MS"),
2907
3958
  goalIdleBackoffMaxMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MAX_MS"),
2908
3959
  childLifecycleNoticesEnabled: optional("OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED"),
2909
- hostMcpAuthoritySourceAdmissionEnabled: optional(
2910
- "OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED",
2911
- ),
2912
- hostMcpCredentialResolversJson: optional("OPENGENI_HOST_MCP_CREDENTIAL_RESOLVERS_JSON"),
2913
3960
  slackWorkspaceRoutingEnabled: optional("OPENGENI_SLACK_WORKSPACE_ROUTING_ENABLED"),
2914
3961
  agentMaxModelCallsPerTurn: optional("OPENGENI_AGENT_MAX_MODEL_CALLS_PER_TURN"),
2915
3962
  contextWindowTokens: optional("OPENGENI_CONTEXT_WINDOW_TOKENS"),
@@ -2924,10 +3971,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2924
3971
  authAllowMetrics: optional("OPENGENI_AUTH_ALLOW_METRICS"),
2925
3972
  apiHost: optional("OPENGENI_API_HOST"),
2926
3973
  apiPort: optional("OPENGENI_API_PORT"),
3974
+ apiMetricsPort: optional("OPENGENI_API_METRICS_PORT"),
2927
3975
  workerHttpPort: optional("OPENGENI_WORKER_HTTP_PORT"),
2928
3976
  opengeniMcpInternalUrl: optional("OPENGENI_MCP_INTERNAL_URL"),
2929
3977
  opengeniMcpUrl: optional("OPENGENI_MCP_URL"),
2930
3978
  corsAllowOriginRegex: optional("OPENGENI_CORS_ALLOW_ORIGIN_REGEX"),
3979
+ localAllowedOrigins: optional("OPENGENI_LOCAL_ALLOWED_ORIGINS"),
2931
3980
  openaiProvider: optional("OPENGENI_OPENAI_PROVIDER"),
2932
3981
  openaiApiKey: optional("OPENGENI_OPENAI_API_KEY") ?? optional("OPENAI_API_KEY"),
2933
3982
  openaiBaseUrl: optional("OPENGENI_OPENAI_BASE_URL") ?? optional("OPENAI_BASE_URL"),
@@ -2977,6 +4026,20 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2977
4026
  voiceInputAzureApiVersion: optional("OPENGENI_VOICE_INPUT_AZURE_API_VERSION"),
2978
4027
  voiceInputAzureApiKey: optional("OPENGENI_VOICE_INPUT_AZURE_API_KEY"),
2979
4028
  voiceInputAzureAdToken: optional("OPENGENI_VOICE_INPUT_AZURE_AD_TOKEN"),
4029
+ azureLiveEndpoint: optional("OPENGENI_AZURE_LIVE_ENDPOINT"),
4030
+ azureLiveApiKey: optional("OPENGENI_AZURE_LIVE_API_KEY"),
4031
+ azureLiveDeployment: optional("OPENGENI_AZURE_LIVE_DEPLOYMENT"),
4032
+ azureLiveVoice: optional("OPENGENI_AZURE_LIVE_VOICE"),
4033
+ azureLivePricingJson: optional("OPENGENI_AZURE_LIVE_PRICING_JSON"),
4034
+ aiGatewayRealtimePricingJson: optional("OPENGENI_AI_GATEWAY_REALTIME_PRICING_JSON"),
4035
+ voiceInputMaiEndpoint: optional("OPENGENI_VOICE_INPUT_MAI_ENDPOINT"),
4036
+ voiceInputMaiApiKey: optional("OPENGENI_VOICE_INPUT_MAI_API_KEY"),
4037
+ voiceInputMaiApiVersion: optional("OPENGENI_VOICE_INPUT_MAI_API_VERSION"),
4038
+ voiceInputMaiPricingJson: optional("OPENGENI_VOICE_INPUT_MAI_PRICING_JSON"),
4039
+ voiceInputMaiModel: optional("OPENGENI_VOICE_INPUT_MAI_MODEL"),
4040
+ voiceInputAzureModel: optional("OPENGENI_VOICE_INPUT_AZURE_MODEL"),
4041
+ voiceInputOpenaiPricingJson: optional("OPENGENI_VOICE_INPUT_OPENAI_PRICING_JSON"),
4042
+ voiceInputAzurePricingJson: optional("OPENGENI_VOICE_INPUT_AZURE_PRICING_JSON"),
2980
4043
  voiceInputCodexExperimentalEnabled: optional("OPENGENI_VOICE_INPUT_CODEX_EXPERIMENTAL"),
2981
4044
  modelPricingJson: optional("OPENGENI_MODEL_PRICING_JSON"),
2982
4045
  modelCatalogSource,
@@ -2986,6 +4049,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2986
4049
  modelProvidersJson: optional("OPENGENI_MODEL_PROVIDERS_JSON"),
2987
4050
  codexSubscriptionEnabled: optional("OPENGENI_CODEX_SUBSCRIPTION_ENABLED"),
2988
4051
  supergrokSubscriptionEnabled: optional("OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED"),
4052
+ claudeSubscriptionEnabled: optional("OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED"),
2989
4053
  supergrokResponseStreamIdleTimeoutMs: optional(
2990
4054
  "OPENGENI_SUPERGROK_RESPONSE_STREAM_IDLE_TIMEOUT_MS",
2991
4055
  ),
@@ -2994,13 +4058,32 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2994
4058
  lazyToolSearchEnabled: optional("OPENGENI_LAZY_TOOL_SEARCH_ENABLED"),
2995
4059
  codexFleetPolicyShadowEnabled: optional("OPENGENI_CODEX_FLEET_POLICY_SHADOW_ENABLED"),
2996
4060
  codexProductSku: optional("OPENGENI_CODEX_PRODUCT_SKU"),
4061
+ reasoningConfigurationUpdatesEnabled: optional(
4062
+ "OPENGENI_REASONING_CONFIGURATION_UPDATES_ENABLED",
4063
+ ),
2997
4064
  openaiReasoningEffort: optional("OPENGENI_OPENAI_REASONING_EFFORT"),
2998
4065
  openaiAllowedReasoningEfforts: optional("OPENGENI_OPENAI_ALLOWED_REASONING_EFFORTS"),
4066
+ creditsDefaultModel: optional("OPENGENI_CREDITS_DEFAULT_MODEL"),
4067
+ creditsDefaultReasoningEffort: optional("OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT"),
2999
4068
  openaiResponsesTransport: optional("OPENGENI_OPENAI_RESPONSES_TRANSPORT"),
3000
4069
  openaiProviderItemIds: optional("OPENGENI_OPENAI_PROVIDER_ITEM_IDS"),
3001
4070
  openaiReasoningEncryptedContent: optional("OPENGENI_OPENAI_REASONING_ENCRYPTED_CONTENT"),
3002
4071
  openaiMaxRetries: optional("OPENGENI_OPENAI_MAX_RETRIES"),
3003
4072
  webSearchEnabled: optional("OPENGENI_WEB_SEARCH_ENABLED"),
4073
+ webSearchProvider: optional("OPENGENI_WEB_SEARCH_PROVIDER"),
4074
+ webSearchApiKey: optional("OPENGENI_WEB_SEARCH_API_KEY"),
4075
+ webSearchBaseUrl: optional("OPENGENI_WEB_SEARCH_BASE_URL"),
4076
+ webFetchProvider: optional("OPENGENI_WEB_FETCH_PROVIDER"),
4077
+ webFetchApiKey: optional("OPENGENI_WEB_FETCH_API_KEY"),
4078
+ webFetchBaseUrl: optional("OPENGENI_WEB_FETCH_BASE_URL"),
4079
+ webSearchProviderMode: optional("OPENGENI_WEB_SEARCH_PROVIDER_MODE"),
4080
+ webSearchPricingJson: optional("OPENGENI_WEB_SEARCH_PRICING_JSON"),
4081
+ webSearchRequestTimeoutMs: optional("OPENGENI_WEB_SEARCH_REQUEST_TIMEOUT_MS"),
4082
+ jevApiKey: optional("OPENGENI_JEV_API_KEY"),
4083
+ jevBaseUrl: optional("OPENGENI_JEV_BASE_URL"),
4084
+ jevModel: optional("OPENGENI_JEV_MODEL"),
4085
+ jevRequestTimeoutMs: optional("OPENGENI_JEV_REQUEST_TIMEOUT_MS"),
4086
+ codeSearchMode: optional("OPENGENI_CODE_SEARCH_MODE"),
3004
4087
  agentInstructionsTemplate: optional("OPENGENI_AGENT_INSTRUCTIONS_TEMPLATE"),
3005
4088
  azureOpenaiBaseUrl: optional("OPENGENI_AZURE_OPENAI_BASE_URL"),
3006
4089
  azureOpenaiEndpoint: optional("OPENGENI_AZURE_OPENAI_ENDPOINT"),
@@ -3017,6 +4100,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3017
4100
  dockerWorkspaceBaseDir: optional("OPENGENI_DOCKER_WORKSPACE_BASE_DIR"),
3018
4101
  modalAppName: optional("OPENGENI_MODAL_APP_NAME"),
3019
4102
  modalImageRef: optional("OPENGENI_MODAL_IMAGE_REF") ?? DEFAULT_MODAL_IMAGE_REF,
4103
+ modalCommandSupervisionEnabled: optional("OPENGENI_MODAL_COMMAND_SUPERVISION_ENABLED"),
3020
4104
  modalImageId: optional("OPENGENI_MODAL_IMAGE_ID"),
3021
4105
  modalImageRegistrySecret: optional("OPENGENI_MODAL_IMAGE_REGISTRY_SECRET"),
3022
4106
  modalTimeoutSeconds: optional("OPENGENI_MODAL_TIMEOUT_SECONDS"),
@@ -3068,6 +4152,9 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3068
4152
  blaxelTtl: optional("OPENGENI_BLAXEL_TTL"),
3069
4153
  cloudflareWorkerUrl: optional("OPENGENI_CLOUDFLARE_WORKER_URL"),
3070
4154
  cloudflareApiKey: optional("OPENGENI_CLOUDFLARE_API_KEY"),
4155
+ experimentalBrowserContextPoolEnabled: optional(
4156
+ "OPENGENI_EXPERIMENTAL_BROWSER_CONTEXT_POOL_ENABLED",
4157
+ ),
3071
4158
  browserbaseApiKey: optional("OPENGENI_BROWSERBASE_API_KEY"),
3072
4159
  kernelApiKey: optional("OPENGENI_KERNEL_API_KEY"),
3073
4160
  kernelEndpoint: optional("OPENGENI_KERNEL_ENDPOINT"),
@@ -3116,6 +4203,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3116
4203
  sandboxViewerHolderTtlMs: optional("OPENGENI_SANDBOX_VIEWER_HOLDER_TTL_MS"),
3117
4204
  sandboxInteractionHolderTtlMs: optional("OPENGENI_SANDBOX_INTERACTION_HOLDER_TTL_MS"),
3118
4205
  sandboxIdleGraceMs: optional("OPENGENI_SANDBOX_IDLE_GRACE_MS"),
4206
+ sandboxIdleCommandContainmentMs: optional("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS"),
3119
4207
  sandboxSnapshotIntervalMs: optional("OPENGENI_SANDBOX_SNAPSHOT_INTERVAL_MS"),
3120
4208
  sandboxSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_SNAPSHOT_TIMEOUT_MS"),
3121
4209
  sandboxDrainSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_DRAIN_SNAPSHOT_TIMEOUT_MS"),
@@ -3129,9 +4217,11 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3129
4217
  sandboxWarmRateMicrosPerSecondJson: optional(
3130
4218
  "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON",
3131
4219
  ),
4220
+ sandboxWarmBillingMode: optional("OPENGENI_SANDBOX_WARM_BILLING_MODE"),
3132
4221
  sandboxMaxWarmSecondsPerWorkspace: optional("OPENGENI_SANDBOX_MAX_WARM_SECONDS_PER_WORKSPACE"),
3133
4222
  sandboxPreparationProfiles: optional("OPENGENI_SANDBOX_PREPARATION_PROFILES"),
3134
4223
  sandboxEnvAllowlist: optional("OPENGENI_SANDBOX_ENV_ALLOWLIST"),
4224
+ sandboxImageAllowlist: optional("OPENGENI_SANDBOX_IMAGE_ALLOWLIST"),
3135
4225
  objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
3136
4226
  objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
3137
4227
  objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
@@ -3158,6 +4248,13 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3158
4248
  documentEmbeddingDimensions: optional("OPENGENI_DOCUMENT_EMBEDDING_DIMENSIONS"),
3159
4249
  documentEmbeddingApiKey: optional("OPENGENI_DOCUMENT_EMBEDDING_API_KEY"),
3160
4250
  documentEmbeddingBaseUrl: optional("OPENGENI_DOCUMENT_EMBEDDING_BASE_URL"),
4251
+ documentEmbeddingBillingMode: optional("OPENGENI_DOCUMENT_EMBEDDING_BILLING_MODE"),
4252
+ documentEmbeddingCreditsActivatedAt: optional(
4253
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT",
4254
+ ),
4255
+ documentEmbeddingRateMicrosPerMillionBytes: optional(
4256
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES",
4257
+ ),
3161
4258
  documentCurationProvider: optional("OPENGENI_DOCUMENT_CURATION_PROVIDER"),
3162
4259
  documentCurationModel: optional("OPENGENI_DOCUMENT_CURATION_MODEL"),
3163
4260
  documentCurationApiKey: optional("OPENGENI_DOCUMENT_CURATION_API_KEY"),
@@ -3192,7 +4289,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3192
4289
  managedAuthGoogleClientSecret: optional("OPENGENI_MANAGED_AUTH_GOOGLE_CLIENT_SECRET"),
3193
4290
  managedAuthGithubClientId: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_ID"),
3194
4291
  managedAuthGithubClientSecret: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_SECRET"),
4292
+ allowedUserEmails:
4293
+ source.OPENGENI_ALLOWED_USER_EMAILS === undefined
4294
+ ? undefined
4295
+ : source.OPENGENI_ALLOWED_USER_EMAILS.split(",").map((email) => email.trim()),
3195
4296
  managedAuthSessionSetMode: optional("OPENGENI_MANAGED_AUTH_SESSION_SET_MODE"),
4297
+ managedAuthNewSignupsEnabled: optional("OPENGENI_MANAGED_AUTH_NEW_SIGNUPS_ENABLED"),
3196
4298
  organizationUserSetupEmailTokenTransport: optional(
3197
4299
  "OPENGENI_ORGANIZATION_USER_SETUP_EMAIL_TOKEN_TRANSPORT",
3198
4300
  ),
@@ -3216,14 +4318,80 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3216
4318
  : parsed.sandboxIdleGraceMs,
3217
4319
  sandboxRotationLeadMs:
3218
4320
  raw.sandboxRotationLeadMs === undefined && parsed.sandboxBackend === "modal"
3219
- ? Math.min(3_600_000, Math.floor((parsed.modalTimeoutSeconds * 1000) / 2))
4321
+ ? Math.min(
4322
+ 3_600_000,
4323
+ Math.max(
4324
+ Math.floor((parsed.modalTimeoutSeconds * 1000) / 2),
4325
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS +
4326
+ sandboxArchiveCaptureTimeoutMs({
4327
+ sandboxSnapshotTimeoutMs: Math.max(
4328
+ parsed.sandboxSnapshotTimeoutMs,
4329
+ parsed.sandboxDrainSnapshotTimeoutMs ?? parsed.sandboxSnapshotTimeoutMs,
4330
+ ),
4331
+ }) +
4332
+ 2 * parsed.sandboxLeaseReaperPeriodMs +
4333
+ 1,
4334
+ ),
4335
+ )
3220
4336
  : parsed.sandboxRotationLeadMs,
3221
4337
  mcpServers: ensureBuiltInMcpServers(parsed),
3222
4338
  };
4339
+ if (raw.sandboxIdleCommandContainmentMs === undefined) {
4340
+ // Strictly between the idle grace and the rotation lead whenever that
4341
+ // interval exists (short test/canary lifetimes), never above 30 minutes,
4342
+ // and early enough that an explicit Modal idle timeout cannot fire first.
4343
+ const modalIdleCeilingMs =
4344
+ settings.sandboxBackend === "modal" && settings.modalIdleTimeoutSeconds !== undefined
4345
+ ? settings.modalIdleTimeoutSeconds * 1000 -
4346
+ settings.sandboxLeaseReaperPeriodMs -
4347
+ sandboxArchiveCaptureTimeoutMs({
4348
+ sandboxSnapshotTimeoutMs: effectiveSandboxDrainSnapshotTimeoutMs(settings),
4349
+ }) -
4350
+ 1
4351
+ : Number.POSITIVE_INFINITY;
4352
+ const derived = Math.min(
4353
+ 1_800_000,
4354
+ Math.floor((settings.sandboxIdleGraceMs + settings.sandboxRotationLeadMs) / 2),
4355
+ modalIdleCeilingMs,
4356
+ );
4357
+ // A derived value never fails validation: with no room above the idle
4358
+ // grace, idle containment stays off and only the deadline rule applies.
4359
+ if (derived > settings.sandboxIdleGraceMs) {
4360
+ settings.sandboxIdleCommandContainmentMs = derived;
4361
+ } else {
4362
+ settings.sandboxIdleCommandContainmentMs = undefined;
4363
+ if (!idleCommandContainmentDisabledWarned) {
4364
+ idleCommandContainmentDisabledWarned = true;
4365
+ console.warn(
4366
+ "[config] idle command containment disabled: no window fits between " +
4367
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}) and the Modal idle ` +
4368
+ "timeout / rotation lead ceiling; set OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS to override.",
4369
+ );
4370
+ }
4371
+ }
4372
+ }
4373
+ warnRetiredOrganizationTenancyActivationSwitch(source);
3223
4374
  validateSettings(settings, source);
3224
4375
  return settings;
3225
4376
  }
3226
4377
 
4378
+ let retiredOrganizationTenancyActivationSwitchWarned = false;
4379
+
4380
+ /**
4381
+ * Session-tenancy activation is universal since migration 0611, so the former
4382
+ * deployment switch is accepted and ignored. Deployments that still set it keep
4383
+ * booting; the one-time warning tells the operator to delete it.
4384
+ */
4385
+ function warnRetiredOrganizationTenancyActivationSwitch(source: NodeJS.ProcessEnv): void {
4386
+ if (retiredOrganizationTenancyActivationSwitchWarned) return;
4387
+ if (source.OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED === undefined) return;
4388
+ retiredOrganizationTenancyActivationSwitchWarned = true;
4389
+ console.warn(
4390
+ "[config] OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED is retired and ignored: " +
4391
+ "every organization is session-tenancy activated (migration 0611). Remove it from the deployment.",
4392
+ );
4393
+ }
4394
+
3227
4395
  const LOCAL_FIRST_PARTY_DELEGATION_SECRET = "opengeni-local-first-party-delegation-secret-v1";
3228
4396
 
3229
4397
  /**
@@ -3250,13 +4418,35 @@ export type FirstPartyMcpToolPolicy = {
3250
4418
  allowed: FirstPartyMcpToolNameType[];
3251
4419
  };
3252
4420
 
4421
+ export type FirstPartyMcpToolPolicySettings = Pick<
4422
+ Settings,
4423
+ "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools" | "artifactMaterializerDeployed"
4424
+ >;
4425
+
4426
+ const EDITABLE_ARTIFACT_EXPORT_TOOLS: readonly FirstPartyMcpToolNameType[] = [
4427
+ "editable_artifact_export",
4428
+ "editable_artifact_export_status",
4429
+ ];
4430
+
4431
+ /**
4432
+ * First-party tools whose backing workload this deployment does not run. They
4433
+ * are absent from the ceiling (never offered) and an explicit request for one
4434
+ * is dropped rather than rejected: it is a deployment fact, not a caller error.
4435
+ */
4436
+ export function deploymentUnavailableFirstPartyMcpTools(
4437
+ settings: Pick<Settings, "artifactMaterializerDeployed">,
4438
+ ): ReadonlySet<FirstPartyMcpToolNameType> {
4439
+ return new Set(settings.artifactMaterializerDeployed ? [] : EDITABLE_ARTIFACT_EXPORT_TOOLS);
4440
+ }
4441
+
3253
4442
  /** Resolve the deployment's session-tool defaults and hard execution ceiling. */
3254
4443
  export function resolveFirstPartyMcpToolPolicy(
3255
- settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
4444
+ settings: FirstPartyMcpToolPolicySettings,
3256
4445
  ): FirstPartyMcpToolPolicy {
4446
+ const unavailable = deploymentUnavailableFirstPartyMcpTools(settings);
3257
4447
  const allowed = currentAgentLearningToolSelection(
3258
4448
  settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES],
3259
- );
4449
+ ).filter((tool) => !isRetiredNativeAtlassianTool(tool) && !unavailable.has(tool));
3260
4450
  const allowedSet = new Set(allowed);
3261
4451
  const defaults = currentAgentLearningToolSelection(
3262
4452
  settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
@@ -3269,7 +4459,7 @@ export function resolveFirstPartyMcpToolPolicy(
3269
4459
 
3270
4460
  /** Apply the deployment ceiling to an existing durable session selection. */
3271
4461
  export function allowedFirstPartyMcpToolsForSession(
3272
- settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
4462
+ settings: FirstPartyMcpToolPolicySettings,
3273
4463
  selected: readonly FirstPartyMcpToolNameType[] | null | undefined,
3274
4464
  ): FirstPartyMcpToolNameType[] {
3275
4465
  const policy = resolveFirstPartyMcpToolPolicy(settings);
@@ -3789,6 +4979,7 @@ function gatewayRegistryProvider(
3789
4979
  upstreamModelId: model.upstreamModelId,
3790
4980
  label: model.label,
3791
4981
  ...(model.shortLabel ? { shortLabel: model.shortLabel } : {}),
4982
+ ...(model.logoUrl ? { logoUrl: model.logoUrl } : {}),
3792
4983
  capabilities: gatewayModelCapabilities(settings, {
3793
4984
  implicitCaching: model.implicitCaching,
3794
4985
  vision: model.vision,
@@ -3894,6 +5085,7 @@ function openRouterRegistryProvider(
3894
5085
  aliases,
3895
5086
  label: model.label,
3896
5087
  ...(model.shortLabel ? { shortLabel: model.shortLabel } : {}),
5088
+ ...(model.logoUrl ? { logoUrl: model.logoUrl } : {}),
3897
5089
  capabilities: model.capabilities,
3898
5090
  ...(model.contextWindowTokens === undefined
3899
5091
  ? {}
@@ -3949,7 +5141,11 @@ function openRouterRegistryProvider(
3949
5141
 
3950
5142
  function configuredRegistryProviders(settings: Settings): InternalRegistryProvider[] {
3951
5143
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
3952
- const injected: InternalRegistryProvider[] = [...providers];
5144
+ const injected: InternalRegistryProvider[] = providers.filter(
5145
+ (provider) =>
5146
+ settings.claudeSubscriptionEnabled ||
5147
+ !(provider.api === "anthropic-messages" && provider.anthropic?.auth === "oauth"),
5148
+ );
3953
5149
  if (settings.vercelAiGatewayApiKey && configuredGatewayCatalogModels(settings).length > 0) {
3954
5150
  injected.push(
3955
5151
  gatewayRegistryProvider(settings, {
@@ -3968,6 +5164,53 @@ function configuredRegistryProviders(settings: Settings): InternalRegistryProvid
3968
5164
  return injected;
3969
5165
  }
3970
5166
 
5167
+ /** Customer-owned OpenAI/Azure routes, bound to immutable connection identity. */
5168
+ export function withDirectModelProviders(
5169
+ settings: Settings,
5170
+ connections: readonly {
5171
+ id: string;
5172
+ version: number;
5173
+ subjectId: string | null;
5174
+ kind: string;
5175
+ status: string;
5176
+ providerDomain: string;
5177
+ metadata: Record<string, unknown>;
5178
+ apiKey?: string;
5179
+ }[],
5180
+ ): Settings {
5181
+ const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
5182
+ (provider) =>
5183
+ !provider.id.startsWith("workspace-openai-") &&
5184
+ !provider.id.startsWith("workspace-azure-openai-"),
5185
+ );
5186
+ for (const connection of connections) {
5187
+ const spec = directModelConnectionSpec(connection);
5188
+ if (!spec) continue;
5189
+ providers.push({
5190
+ kind: spec.provider === "openai" ? "direct-openai-workspace" : "direct-azure-workspace",
5191
+ id: spec.providerId,
5192
+ label: spec.provider === "openai" ? "Your OpenAI" : "Your Azure OpenAI",
5193
+ api: "responses",
5194
+ wireProfile: spec.provider === "openai" ? "openai" : "azure-openai",
5195
+ baseUrl: spec.baseUrl,
5196
+ ...(connection.apiKey ? { apiKey: connection.apiKey } : {}),
5197
+ models: [
5198
+ {
5199
+ id: spec.modelId,
5200
+ upstreamModelId: spec.model,
5201
+ label: spec.model,
5202
+ capabilities: legacyModelCapabilities(settings, {
5203
+ reasoningEffort: false,
5204
+ hostedWebSearch: false,
5205
+ }),
5206
+ toolOutputTruncationTokens: settings.modelToolOutputTruncationTokens,
5207
+ },
5208
+ ],
5209
+ });
5210
+ }
5211
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
5212
+ }
5213
+
3971
5214
  /** Static catalog overlay; it contains no concrete workspace credential. */
3972
5215
  export function withWorkspaceGatewayCatalogProvider(
3973
5216
  settings: Settings,
@@ -4058,7 +5301,10 @@ export function withWorkspaceOpenRouterCredential(
4058
5301
  /** Secret-free organization Vercel AI Gateway catalog overlay. */
4059
5302
  export function withOrganizationGatewayCatalogProvider(
4060
5303
  settings: Settings,
4061
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
5304
+ customModels: readonly {
5305
+ upstreamModelId: string;
5306
+ label?: string | null;
5307
+ }[] = [],
4062
5308
  ): Settings {
4063
5309
  if (customModels.length === 0) return settings;
4064
5310
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
@@ -4074,7 +5320,10 @@ export function withOrganizationGatewayCatalogProvider(
4074
5320
  export function withOrganizationGatewayCredential(
4075
5321
  settings: Settings,
4076
5322
  apiKey: string,
4077
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
5323
+ customModels: readonly {
5324
+ upstreamModelId: string;
5325
+ label?: string | null;
5326
+ }[] = [],
4078
5327
  ): Settings {
4079
5328
  if (!apiKey.trim()) throw new Error("organization AI Gateway credential is empty");
4080
5329
  const catalog = withOrganizationGatewayCatalogProvider(settings, customModels);
@@ -4087,7 +5336,10 @@ export function withOrganizationGatewayCredential(
4087
5336
  /** Secret-free organization OpenRouter catalog overlay. */
4088
5337
  export function withOrganizationOpenRouterCatalogProvider(
4089
5338
  settings: Settings,
4090
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
5339
+ customModels: readonly {
5340
+ upstreamModelId: string;
5341
+ label?: string | null;
5342
+ }[] = [],
4091
5343
  ): Settings {
4092
5344
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
4093
5345
  (provider) => provider.id !== ORGANIZATION_OPENROUTER_PROVIDER_ID,
@@ -4104,7 +5356,10 @@ export function withOrganizationOpenRouterCatalogProvider(
4104
5356
  export function withOrganizationOpenRouterCredential(
4105
5357
  settings: Settings,
4106
5358
  apiKey: string,
4107
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
5359
+ customModels: readonly {
5360
+ upstreamModelId: string;
5361
+ label?: string | null;
5362
+ }[] = [],
4108
5363
  ): Settings {
4109
5364
  if (!apiKey.trim()) throw new Error("organization OpenRouter credential is empty");
4110
5365
  const catalog = withOrganizationOpenRouterCatalogProvider(settings, customModels);
@@ -4157,8 +5412,8 @@ export function productShortLabelForModelId(modelId: string): string | null {
4157
5412
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4158
5413
  : modelId;
4159
5414
  switch (slug) {
4160
- case "grok-4.6":
4161
- return "4.6";
5415
+ case "grok-4.7":
5416
+ return "4.7";
4162
5417
  case "gpt-5.6-sol":
4163
5418
  return "5.6 Sol";
4164
5419
  case "gpt-5.6-terra":
@@ -4167,6 +5422,10 @@ export function productShortLabelForModelId(modelId: string): string | null {
4167
5422
  return "5.6 Luna";
4168
5423
  case "gpt-6-astra":
4169
5424
  return "6 Astra";
5425
+ case "gpt-6-sol":
5426
+ return "6 Sol";
5427
+ case "gpt-6-luna":
5428
+ return "6 Luna";
4170
5429
  default:
4171
5430
  return null;
4172
5431
  }
@@ -4204,8 +5463,9 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4204
5463
  }> {
4205
5464
  if (
4206
5465
  isBuiltinGpt56ModelId(modelId) ||
5466
+ /^gpt-6(?:\.\d+)?-/u.test(modelId) ||
4207
5467
  modelId.startsWith("codex/gpt-5.6-") ||
4208
- modelId === "codex/gpt-6-astra"
5468
+ /^codex\/gpt-6(?:\.\d+)?-/u.test(modelId)
4209
5469
  ) {
4210
5470
  return [
4211
5471
  { id: "standard", upstream: "supported", runnable: true },
@@ -4220,13 +5480,38 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4220
5480
  return [{ id: "standard", upstream: "unknown", runnable: true }];
4221
5481
  }
4222
5482
 
5483
+ function isReviewedGptVisionModel(slug: string): boolean {
5484
+ return slug.startsWith("gpt-5.6-") || /^gpt-6(?:\.\d+)?-/u.test(slug);
5485
+ }
5486
+
5487
+ /** Repair missing reviewed capabilities in live/stored Codex definitions.
5488
+ * Explicit latency restrictions and every unrelated catalog field remain authoritative.
5489
+ */
5490
+ function reviewedCodexCatalogCapabilities(
5491
+ slug: string,
5492
+ capabilities: ModelCapabilitiesV1,
5493
+ ): ModelCapabilitiesV1 {
5494
+ if (!/^gpt-6(?:\.\d+)?-/u.test(slug)) return capabilities;
5495
+ const fast = builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`).find(
5496
+ (mode) => mode.id === "fast",
5497
+ );
5498
+ return normalizeCapabilities({
5499
+ ...capabilities,
5500
+ inputModalities: [...new Set([...capabilities.inputModalities, "image" as const])],
5501
+ latencyModes:
5502
+ fast && !capabilities.latencyModes.some((mode) => mode.id === "fast")
5503
+ ? [...capabilities.latencyModes, fast]
5504
+ : capabilities.latencyModes,
5505
+ });
5506
+ }
5507
+
4223
5508
  function builtinPromptCachingForModel(
4224
5509
  modelId: string,
4225
5510
  ): NonNullable<ModelCapabilitiesV1["promptCaching"]> | undefined {
4226
5511
  const slug = modelId.startsWith(CODEX_MODEL_ID_PREFIX)
4227
5512
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4228
5513
  : modelId;
4229
- return slug.startsWith("gpt-5.6-")
5514
+ return slug.startsWith("gpt-5.6-") || slug.startsWith("gpt-6-")
4230
5515
  ? { upstream: "supported", runnable: true, mode: "implicit" }
4231
5516
  : undefined;
4232
5517
  }
@@ -4236,7 +5521,7 @@ function builtinHostedImageGenerationForModel(settings: Settings, modelId: strin
4236
5521
  return (
4237
5522
  settings.openaiProvider === "openai" &&
4238
5523
  isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl) &&
4239
- isBuiltinGpt56ModelId(modelId)
5524
+ (isBuiltinGpt56ModelId(modelId) || modelId.startsWith("gpt-6-"))
4240
5525
  );
4241
5526
  }
4242
5527
 
@@ -4313,9 +5598,22 @@ function assertLatencyModeRunnable(
4313
5598
  ): void {
4314
5599
  const runnable = runnableLatencyModesForModel(settings, modelId);
4315
5600
  if (!runnable.includes(latencyMode)) {
4316
- throw new Error(
4317
- `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${runnable.join(", ")})`,
5601
+ throw new UnsupportedLatencyModeError(modelId, latencyMode, runnable);
5602
+ }
5603
+ }
5604
+
5605
+ export class UnsupportedLatencyModeError extends Error {
5606
+ readonly code = "UNSUPPORTED_LATENCY_MODE";
5607
+
5608
+ constructor(
5609
+ readonly modelId: string,
5610
+ readonly latencyMode: LatencyMode,
5611
+ readonly allowedLatencyModes: readonly LatencyMode[],
5612
+ ) {
5613
+ super(
5614
+ `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${allowedLatencyModes.join(", ")})`,
4318
5615
  );
5616
+ this.name = "UnsupportedLatencyModeError";
4319
5617
  }
4320
5618
  }
4321
5619
 
@@ -4327,10 +5625,17 @@ function registryCredentialSource(provider: InternalRegistryProvider): Credentia
4327
5625
  return { kind: "connected_subscription", provider: "codex" };
4328
5626
  case "xai-subscription":
4329
5627
  return { kind: "connected_subscription", provider: "xai" };
5628
+ case "claude-subscription-workspace":
5629
+ case "claude-subscription-organization":
5630
+ return { kind: "connected_subscription", provider: "claude" };
4330
5631
  case "vercel-gateway-workspace":
5632
+ case "direct-openai-workspace":
5633
+ case "direct-azure-workspace":
4331
5634
  case "openrouter-workspace":
5635
+ case "anthropic-workspace":
4332
5636
  return { kind: "workspace_connection", mechanism: "api_key" };
4333
5637
  case "vercel-gateway-organization":
5638
+ case "anthropic-organization":
4334
5639
  case "openrouter-organization":
4335
5640
  return { kind: "organization_connection", mechanism: "api_key" };
4336
5641
  case "api-key":
@@ -4351,11 +5656,17 @@ function registryBilling(provider: InternalRegistryProvider): BillingAttribution
4351
5656
  return { upstreamPayer: "deployment", metering: "external" };
4352
5657
  case "codex-subscription":
4353
5658
  case "xai-subscription":
5659
+ case "claude-subscription-workspace":
5660
+ case "claude-subscription-organization":
4354
5661
  return { upstreamPayer: "connected_subscription", metering: "external" };
4355
5662
  case "vercel-gateway-workspace":
5663
+ case "direct-openai-workspace":
5664
+ case "direct-azure-workspace":
4356
5665
  case "openrouter-workspace":
5666
+ case "anthropic-workspace":
4357
5667
  return { upstreamPayer: "workspace", metering: "external" };
4358
5668
  case "vercel-gateway-organization":
5669
+ case "anthropic-organization":
4359
5670
  case "openrouter-organization":
4360
5671
  return { upstreamPayer: "organization", metering: "external" };
4361
5672
  case "api-key":
@@ -4394,6 +5705,7 @@ function builtinCredentialSource(settings: Settings): CredentialSourceV1 {
4394
5705
  }
4395
5706
 
4396
5707
  function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
5708
+ anthropic?: ResolvedModelProvider["anthropic"];
4397
5709
  headers: Array<{
4398
5710
  name: string;
4399
5711
  classification: "public" | "secret";
@@ -4408,6 +5720,12 @@ function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
4408
5720
  const publicHeaders = new Set(provider.publicDefaultHeaderNames ?? []);
4409
5721
  const publicQuery = new Set(provider.publicDefaultQueryNames ?? []);
4410
5722
  return {
5723
+ ...(provider.anthropic
5724
+ ? {
5725
+ anthropic: (({ identity: _identity, credentialBinding: _binding, ...options }) =>
5726
+ options)(provider.anthropic),
5727
+ }
5728
+ : {}),
4411
5729
  headers: Object.entries(provider.defaultHeaders ?? {})
4412
5730
  .sort(([left], [right]) => left.localeCompare(right))
4413
5731
  .map(([name, value]) =>
@@ -4464,6 +5782,7 @@ function definitionVersionFor(
4464
5782
  baseUrl: provider.baseUrl ?? null,
4465
5783
  defaultHeaders: requestMetadata.headers,
4466
5784
  defaultQuery: requestMetadata.query,
5785
+ ...(requestMetadata.anthropic ? { anthropic: requestMetadata.anthropic } : {}),
4467
5786
  },
4468
5787
  credentialSource: model.credentialSource,
4469
5788
  billing: model.billing,
@@ -4493,6 +5812,140 @@ function legacyImplicitOpenAiDefinitionVersionFor(
4493
5812
  });
4494
5813
  }
4495
5814
 
5815
+ function legacyCodexAstraImplicitCachingDefinitionVersionFor(
5816
+ model: ConfiguredModel,
5817
+ provider: ResolvedModelProvider,
5818
+ ): string | null {
5819
+ // The GPT-6 rollout only made the already-implicit Codex cache discoverable.
5820
+ // Bound this compatibility to that product/transport and exact declaration;
5821
+ // never normalize explicit caching, another capability, or another model.
5822
+ if (
5823
+ model.id !== "codex/gpt-6-astra" ||
5824
+ model.upstreamModelId !== "gpt-6-astra" ||
5825
+ provider.id !== CODEX_PROVIDER_ID ||
5826
+ provider.kind !== "codex-subscription" ||
5827
+ provider.api !== "responses" ||
5828
+ provider.wireProfile !== "openai" ||
5829
+ canonicalJson(model.capabilities.promptCaching) !==
5830
+ canonicalJson({ upstream: "supported", runnable: true, mode: "implicit" })
5831
+ ) {
5832
+ return null;
5833
+ }
5834
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5835
+ const { promptCaching: _promptCaching, ...capabilities } = model.capabilities;
5836
+ // Recompute, rather than allowlisting an incident hash: all other current
5837
+ // fields must still reproduce the accepted digest. Do not compose this with
5838
+ // the older wire-profile compatibility or rewrite the accepted policy.
5839
+ return definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider);
5840
+ }
5841
+
5842
+ function matchesAdditiveCapabilityDefinitionVersion(
5843
+ model: ConfiguredModel,
5844
+ provider: ResolvedModelProvider,
5845
+ policy: TurnExecutionPolicyV1,
5846
+ ): boolean {
5847
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5848
+ const { latencyModes, inputModalities } = model.capabilities;
5849
+ // V1 has only three unique latency ids and three unique input modalities:
5850
+ // at most 4 * 7 subset digests retaining the frozen mode. Reconstruct an
5851
+ // exact historical declaration; never ignore the digest or alter existing
5852
+ // mode support, runnable state, billing multiplier, or request-tier routing.
5853
+ // Every other executable field remains in the digest. Do not compose this
5854
+ // with the pre-wire-profile or implicit-caching migration exceptions.
5855
+ for (let latencyMask = 1; latencyMask < 1 << latencyModes.length; latencyMask += 1) {
5856
+ const retainedModes = latencyModes.filter((_mode, index) => latencyMask & (1 << index));
5857
+ if (!retainedModes.some((mode) => mode.id === policy.latencyMode && mode.runnable)) {
5858
+ continue;
5859
+ }
5860
+ for (let inputMask = 1; inputMask < 1 << inputModalities.length; inputMask += 1) {
5861
+ const retainedInputs = inputModalities.filter((_modality, index) => inputMask & (1 << index));
5862
+ if (
5863
+ retainedModes.length === latencyModes.length &&
5864
+ retainedInputs.length === inputModalities.length
5865
+ ) {
5866
+ continue;
5867
+ }
5868
+ const capabilities = {
5869
+ ...model.capabilities,
5870
+ latencyModes: retainedModes,
5871
+ inputModalities: retainedInputs,
5872
+ };
5873
+ if (
5874
+ policy.definitionVersion ===
5875
+ definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider)
5876
+ ) {
5877
+ return true;
5878
+ }
5879
+ }
5880
+ }
5881
+ return false;
5882
+ }
5883
+
5884
+ /** The one pre-enablement hosted web-search declaration an operator may upgrade from. */
5885
+ function webSearchPreEnablementState(): CapabilityStateV1 {
5886
+ return { upstream: "unknown", runnable: false };
5887
+ }
5888
+
5889
+ function matchesWebSearchEnablementDefinitionVersion(
5890
+ model: ConfiguredModel,
5891
+ provider: ResolvedModelProvider,
5892
+ policy: TurnExecutionPolicyV1,
5893
+ ): boolean {
5894
+ // An operator turning on hosted web search for an existing model changes
5895
+ // only capabilities.hostedTools.webSearch from the exact unknown/off
5896
+ // declaration to a runnable one. Reconstruct that single historical
5897
+ // declaration and require every other current field to reproduce the frozen
5898
+ // digest; never ignore the digest. Turning web search off, or changing any
5899
+ // other capability, still fails closed. Do not compose this with the
5900
+ // latency/input-modality subsets or the wire-profile, implicit-caching, or
5901
+ // Claude-accounting migration exceptions.
5902
+ if (!model.capabilities.hostedTools.webSearch.runnable) return false;
5903
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5904
+ const capabilities = {
5905
+ ...model.capabilities,
5906
+ hostedTools: { ...model.capabilities.hostedTools, webSearch: webSearchPreEnablementState() },
5907
+ };
5908
+ return (
5909
+ policy.definitionVersion ===
5910
+ definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider)
5911
+ );
5912
+ }
5913
+
5914
+ /**
5915
+ * The executable model an accepted turn runs: the current definition, except
5916
+ * that a turn frozen before hosted web search was enabled keeps its frozen
5917
+ * tool set (no web_search) on every recovery attempt. Only the next accepted
5918
+ * logical turn resolves the newly enabled tool. Call only with a policy that
5919
+ * already passed assertTurnExecutionPolicyMatchesConfigV1.
5920
+ */
5921
+ export function configuredModelForAcceptedTurnExecutionPolicy(
5922
+ model: ConfiguredModel,
5923
+ provider: ResolvedModelProvider,
5924
+ policy: TurnExecutionPolicyV1,
5925
+ ): ConfiguredModel {
5926
+ if (
5927
+ policy.definitionVersion === model.definitionVersion ||
5928
+ // Runtime test doubles may resolve a partial shape; a model without a
5929
+ // runnable web-search declaration has nothing to withhold.
5930
+ model.capabilities?.hostedTools?.webSearch?.runnable !== true ||
5931
+ !matchesWebSearchEnablementDefinitionVersion(model, provider, policy)
5932
+ ) {
5933
+ return model;
5934
+ }
5935
+ return {
5936
+ ...model,
5937
+ capabilities: {
5938
+ ...model.capabilities,
5939
+ hostedTools: {
5940
+ ...model.capabilities.hostedTools,
5941
+ webSearch: webSearchPreEnablementState(),
5942
+ },
5943
+ },
5944
+ hostedWebSearch: false,
5945
+ definitionVersion: policy.definitionVersion,
5946
+ };
5947
+ }
5948
+
4496
5949
  /**
4497
5950
  * The built-in provider's stable id: "openai" on the OpenAI platform, "azure"
4498
5951
  * on Azure. Exported because the workspace model-policy gate must attribute
@@ -4534,12 +5987,13 @@ export function configuredProviders(
4534
5987
  if (settings.openaiProvider === "azure") {
4535
5988
  const baseUrl = settings.azureOpenaiBaseUrl ?? settings.azureOpenaiEndpoint;
4536
5989
  builtin.baseUrl = baseUrl ? normalizeRegistryBaseUrl(baseUrl, builtin.id) : undefined;
4537
- builtin.apiKey = settings.azureOpenaiApiKey ?? settings.azureOpenaiAdToken;
5990
+ builtin.apiKey =
5991
+ usableDeploymentSecret(settings.azureOpenaiApiKey) ?? settings.azureOpenaiAdToken;
4538
5992
  } else {
4539
5993
  builtin.baseUrl = settings.openaiBaseUrl
4540
5994
  ? normalizeRegistryBaseUrl(settings.openaiBaseUrl, builtin.id)
4541
5995
  : undefined;
4542
- builtin.apiKey = settings.openaiApiKey;
5996
+ builtin.apiKey = usableDeploymentSecret(settings.openaiApiKey);
4543
5997
  }
4544
5998
  const registry = configuredRegistryProviders(settings).map(
4545
5999
  (provider): ResolvedModelProvider => ({
@@ -4547,6 +6001,7 @@ export function configuredProviders(
4547
6001
  label: provider.label ?? provider.id,
4548
6002
  kind: provider.kind,
4549
6003
  api: provider.api,
6004
+ anthropic: provider.anthropic,
4550
6005
  wireProfile: provider.wireProfile,
4551
6006
  builtin: false,
4552
6007
  baseUrl: provider.baseUrl,
@@ -4568,11 +6023,30 @@ export function configuredProviders(
4568
6023
  * select, lease, refresh, or expose a concrete credential; those runtime
4569
6024
  * operations remain owned by the credential allocator.
4570
6025
  */
6026
+ /** Exact upstream slugs of the active Codex catalog (or the built-in fallback list). */
6027
+ export function codexUpstreamModelSlugs(settings: Settings): string[] {
6028
+ if (settings.resolvedCodexModelsJson === undefined) return [...CODEX_FALLBACK_MODEL_SLUGS];
6029
+ return z
6030
+ .array(CodexCatalogModelSchema)
6031
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
6032
+ .filter((model) => !model.retired)
6033
+ .map((model) => model.upstreamModelId);
6034
+ }
6035
+
4571
6036
  export function withCodexCatalogProvider(settings: Settings): Settings {
4572
6037
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
4573
6038
  if (providers.some((provider) => provider.id === CODEX_PROVIDER_ID)) {
4574
6039
  return settings;
4575
6040
  }
6041
+ const catalogModels =
6042
+ settings.resolvedCodexModelsJson === undefined
6043
+ ? undefined
6044
+ : z
6045
+ .array(CodexCatalogModelSchema)
6046
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
6047
+ .filter((model) => !model.retired)
6048
+ .map(({ retired: _retired, ...model }) => model);
6049
+ if (catalogModels?.length === 0) return settings;
4576
6050
  const provider: RegistryProvider = {
4577
6051
  kind: "codex-subscription",
4578
6052
  id: CODEX_PROVIDER_ID,
@@ -4580,40 +6054,42 @@ export function withCodexCatalogProvider(settings: Settings): Settings {
4580
6054
  api: "responses",
4581
6055
  wireProfile: "openai",
4582
6056
  baseUrl: CODEX_PROVIDER_BASE_URL,
4583
- models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
4584
- const capabilities = {
4585
- ...legacyModelCapabilities(settings, {
6057
+ models:
6058
+ catalogModels ??
6059
+ CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
6060
+ const capabilities = {
6061
+ ...legacyModelCapabilities(settings, {
6062
+ reasoningEffort: true,
6063
+ hostedWebSearch: true,
6064
+ vision: isReviewedGptVisionModel(slug),
6065
+ }),
6066
+ ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
6067
+ ? {
6068
+ promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
6069
+ }
6070
+ : {}),
6071
+ latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
6072
+ };
6073
+ return {
6074
+ id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
6075
+ upstreamModelId: slug,
6076
+ label: productLabelForModelId(slug),
6077
+ ...(productShortLabelForModelId(slug)
6078
+ ? { shortLabel: productShortLabelForModelId(slug)! }
6079
+ : {}),
4586
6080
  reasoningEffort: true,
6081
+ // The ChatGPT/Codex Responses backend accepts the native web_search
6082
+ // hosted tool (unlike hosted apply_patch/computer transports). Declaring
6083
+ // this here makes provider resolution truthful; the worker still applies
6084
+ // the durable session/turn policy gate before attaching it.
4587
6085
  hostedWebSearch: true,
4588
- vision: slug.startsWith("gpt-5.6-") || slug === "gpt-6-astra",
4589
- }),
4590
- ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
4591
- ? {
4592
- promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
4593
- }
4594
- : {}),
4595
- latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
4596
- };
4597
- return {
4598
- id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
4599
- upstreamModelId: slug,
4600
- label: productLabelForModelId(slug),
4601
- ...(productShortLabelForModelId(slug)
4602
- ? { shortLabel: productShortLabelForModelId(slug)! }
4603
- : {}),
4604
- reasoningEffort: true,
4605
- // The ChatGPT/Codex Responses backend accepts the native web_search
4606
- // hosted tool (unlike hosted apply_patch/computer transports). Declaring
4607
- // this here makes provider resolution truthful; the worker still applies
4608
- // the durable session/turn policy gate before attaching it.
4609
- hostedWebSearch: true,
4610
- capabilities,
4611
- contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
4612
- effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
4613
- autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
4614
- toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
4615
- };
4616
- }),
6086
+ capabilities,
6087
+ contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
6088
+ effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
6089
+ autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
6090
+ toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
6091
+ };
6092
+ }),
4617
6093
  };
4618
6094
  return {
4619
6095
  ...settings,
@@ -4852,7 +6328,7 @@ export function configuredModels(
4852
6328
  reasoningEffort: true,
4853
6329
  hostedWebSearch: settings.webSearchEnabled,
4854
6330
  hostedImageGeneration: builtinHostedImageGenerationForModel(settings, id),
4855
- vision: id.startsWith("gpt-5.6-"),
6331
+ vision: isReviewedGptVisionModel(id),
4856
6332
  }),
4857
6333
  ...(builtinPromptCachingForModel(id)
4858
6334
  ? { promptCaching: builtinPromptCachingForModel(id)! }
@@ -4900,6 +6376,7 @@ export function configuredModels(
4900
6376
  id: model.id,
4901
6377
  aliases: [...(model.aliases ?? [])],
4902
6378
  label: model.label ?? productLabelForModelId(model.id),
6379
+ ...(model.logoUrl ? { logoUrl: model.logoUrl } : {}),
4903
6380
  ...(model.shortLabel
4904
6381
  ? { shortLabel: model.shortLabel }
4905
6382
  : productShortLabelForModelId(model.id)
@@ -4963,22 +6440,93 @@ export function canonicalizeConfiguredModelId(settings: Settings, modelId: strin
4963
6440
  * openai allow-list, then registry ids.
4964
6441
  */
4965
6442
  export function configuredAllowedModels(settings: Settings): string[] {
4966
- return configuredModels(settings).map((model) => model.id);
6443
+ return configuredModels(settings)
6444
+ .filter((model) => isModelAvailableForNewSelection(settings, model.id))
6445
+ .map((model) => model.id);
4967
6446
  }
4968
6447
 
4969
- /**
4970
- * Resolve a model string to the provider that serves it and its configured
4971
- * shape. Returns undefined when the id is not exposed (built-in allow-list nor
4972
- * any registry provider), so the runtime can fall back to the legacy global
4973
- * client path.
4974
- */
4975
- export function resolveModelProvider(
6448
+ /** Retirement is selection metadata, never provider authority or executable identity. */
6449
+ export function isModelAvailableForNewSelection(settings: Settings, modelId: string): boolean {
6450
+ if (!modelId.startsWith(CODEX_MODEL_ID_PREFIX) || settings.resolvedCodexModelsJson === undefined)
6451
+ return true;
6452
+ const models = z
6453
+ .array(CodexCatalogModelSchema)
6454
+ .parse(JSON.parse(settings.resolvedCodexModelsJson));
6455
+ return models.some((model) => model.id === modelId && !model.retired);
6456
+ }
6457
+
6458
+ /** Called only with the durable policy installed for a claimed accepted attempt. */
6459
+ export function settingsForAcceptedSubscriptionTurn(
4976
6460
  settings: Settings,
4977
- modelId: string,
4978
- ): { provider: ResolvedModelProvider; model: ConfiguredModel } | undefined {
4979
- const canonicalModelId = canonicalizeConfiguredModelId(settings, modelId);
4980
- const model = configuredModels(settings).find((candidate) => candidate.id === canonicalModelId);
4981
- if (!model) {
6461
+ policy: TurnExecutionPolicyV1,
6462
+ expected: {
6463
+ modelId: string;
6464
+ reasoningEffort: Settings["openaiReasoningEffort"];
6465
+ latencyMode?: LatencyMode;
6466
+ },
6467
+ ): Settings {
6468
+ const parsed = TurnExecutionPolicyV1.parse(policy);
6469
+ if (!parsed.productModelId.startsWith(CODEX_MODEL_ID_PREFIX)) return settings;
6470
+ if (!settings.codexSubscriptionEnabled) throw new Error("Codex subscription is disabled");
6471
+ const models =
6472
+ settings.resolvedCodexModelsJson === undefined
6473
+ ? []
6474
+ : z.array(CodexCatalogModelSchema).parse(JSON.parse(settings.resolvedCodexModelsJson));
6475
+ const retained = models.find((model) => model.id === parsed.productModelId && model.retired);
6476
+ if (!retained) {
6477
+ assertTurnExecutionPolicyMatchesConfigV1(settings, parsed, expected);
6478
+ return settings;
6479
+ }
6480
+ const { retired: _retired, ...model } = retained;
6481
+ // Use the existing fixed broker transport. Only this exact definition is added;
6482
+ // selection metadata remains retired in the original catalog JSON.
6483
+ const withoutCodex = {
6484
+ ...settings,
6485
+ modelProvidersJson: JSON.stringify(
6486
+ parseModelProvidersJson(settings.modelProvidersJson).filter(
6487
+ (provider) => provider.id !== CODEX_PROVIDER_ID,
6488
+ ),
6489
+ ),
6490
+ };
6491
+ const providerSettings = withCodexCatalogProvider({
6492
+ ...withoutCodex,
6493
+ resolvedCodexModelsJson: JSON.stringify([model]),
6494
+ });
6495
+ const retainedProvider = parseModelProvidersJson(providerSettings.modelProvidersJson).find(
6496
+ (provider) => provider.id === CODEX_PROVIDER_ID,
6497
+ )!;
6498
+ const providers = parseModelProvidersJson(settings.modelProvidersJson);
6499
+ const current = providers.find((provider) => provider.id === CODEX_PROVIDER_ID);
6500
+ const restored = {
6501
+ ...settings,
6502
+ modelProvidersJson: JSON.stringify([
6503
+ ...providers.filter((provider) => provider.id !== CODEX_PROVIDER_ID),
6504
+ {
6505
+ ...retainedProvider,
6506
+ models: [
6507
+ ...(current?.models ?? []).filter((candidate) => candidate.id !== model.id),
6508
+ model,
6509
+ ],
6510
+ },
6511
+ ]),
6512
+ };
6513
+ assertTurnExecutionPolicyMatchesConfigV1(restored, parsed, expected);
6514
+ return restored;
6515
+ }
6516
+
6517
+ /**
6518
+ * Resolve a model string to the provider that serves it and its configured
6519
+ * shape. Returns undefined when the id is not exposed (built-in allow-list nor
6520
+ * any registry provider), so the runtime can fall back to the legacy global
6521
+ * client path.
6522
+ */
6523
+ export function resolveModelProvider(
6524
+ settings: Settings,
6525
+ modelId: string,
6526
+ ): { provider: ResolvedModelProvider; model: ConfiguredModel } | undefined {
6527
+ const canonicalModelId = canonicalizeConfiguredModelId(settings, modelId);
6528
+ const model = configuredModels(settings).find((candidate) => candidate.id === canonicalModelId);
6529
+ if (!model) {
4982
6530
  return undefined;
4983
6531
  }
4984
6532
  const provider = configuredProviders(settings).find(
@@ -5057,6 +6605,18 @@ export function resolveModelProviderForTurn(
5057
6605
  );
5058
6606
  }
5059
6607
 
6608
+ /**
6609
+ * The requested model cannot start new work: it is retired from new selection
6610
+ * or absent from the configured catalog. Callers that own a durable refusal
6611
+ * (for example a scheduled occurrence) record it instead of failing blindly.
6612
+ */
6613
+ export class TurnExecutionPolicyModelUnavailableError extends Error {
6614
+ constructor(message: string) {
6615
+ super(message);
6616
+ this.name = "TurnExecutionPolicyModelUnavailableError";
6617
+ }
6618
+ }
6619
+
5060
6620
  /**
5061
6621
  * Build a trusted, secret-safe execution policy from the normalized catalog.
5062
6622
  * The Codex overlay here contains static product/provider identity only; it
@@ -5068,9 +6628,16 @@ export function resolveTurnExecutionPolicyV1(
5068
6628
  ): TurnExecutionPolicyV1 {
5069
6629
  const catalogSettings = settingsForTurnExecutionPolicy(settings, input.modelId);
5070
6630
  const productModelId = canonicalizeConfiguredModelId(catalogSettings, input.modelId);
6631
+ if (!isModelAvailableForNewSelection(catalogSettings, productModelId)) {
6632
+ throw new TurnExecutionPolicyModelUnavailableError(
6633
+ "Turn execution policy model is retired from new selection",
6634
+ );
6635
+ }
5071
6636
  const resolved = resolveModelProvider(catalogSettings, productModelId);
5072
6637
  if (!resolved) {
5073
- throw new Error("Turn execution policy model is not present in the configured catalog");
6638
+ throw new TurnExecutionPolicyModelUnavailableError(
6639
+ "Turn execution policy model is not present in the configured catalog",
6640
+ );
5074
6641
  }
5075
6642
  if (
5076
6643
  input.requestedModelId !== null &&
@@ -5099,10 +6666,20 @@ export function resolveTurnExecutionPolicyV1(
5099
6666
  });
5100
6667
  }
5101
6668
 
6669
+ /** Explicit accepted execution identity matches, but its definition digest differs. */
6670
+ export class TurnExecutionPolicyDefinitionMismatchError extends Error {
6671
+ readonly code = "turn_execution_policy_definition_mismatch";
6672
+
6673
+ constructor() {
6674
+ super("Turn execution policy does not match the current provider definition");
6675
+ this.name = "TurnExecutionPolicyDefinitionMismatchError";
6676
+ }
6677
+ }
6678
+
5102
6679
  /**
5103
6680
  * Parse-time validation lives in @opengeni/contracts; this verifier binds a
5104
6681
  * present snapshot to the current executable definition and exact turn row.
5105
- * Any deployment/provider drift fails before a provider or compaction call.
6682
+ * Non-additive executable drift fails before a provider or compaction call.
5106
6683
  */
5107
6684
  export function assertTurnExecutionPolicyMatchesConfigV1(
5108
6685
  settings: Settings,
@@ -5142,6 +6719,39 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5142
6719
  if (!resolved) {
5143
6720
  throw new Error("Turn execution policy model is no longer configured");
5144
6721
  }
6722
+ // The individual-account cutover corrects subscription accounting labels.
6723
+ // Preserve an already accepted legacy policy only if its complete original
6724
+ // executable digest matches; this exception cannot compose with other drift.
6725
+ const legacyClaudeScope =
6726
+ resolved.provider.kind === "claude-subscription-workspace"
6727
+ ? "workspace"
6728
+ : resolved.provider.kind === "claude-subscription-organization"
6729
+ ? "organization"
6730
+ : null;
6731
+ const legacyClaudeAccountingMatches =
6732
+ legacyClaudeScope !== null &&
6733
+ parsed.providerId === resolved.provider.id &&
6734
+ parsed.upstreamModelId === resolved.model.upstreamModelId &&
6735
+ parsed.wireApi === resolved.model.api &&
6736
+ canonicalJson(parsed.credentialSource) ===
6737
+ canonicalJson({
6738
+ kind: `${legacyClaudeScope}_connection`,
6739
+ mechanism: "api_key",
6740
+ }) &&
6741
+ canonicalJson(parsed.billing) ===
6742
+ canonicalJson({
6743
+ upstreamPayer: legacyClaudeScope,
6744
+ metering: "external",
6745
+ }) &&
6746
+ parsed.definitionVersion ===
6747
+ definitionVersionFor(
6748
+ {
6749
+ ...resolved.model,
6750
+ credentialSource: parsed.credentialSource,
6751
+ billing: parsed.billing,
6752
+ },
6753
+ resolved.provider,
6754
+ );
5145
6755
  // wireProfile was added to the definition digest after policies already
5146
6756
  // existed in durable in-flight turns. An omitted profile meant exactly
5147
6757
  // "openai", so accept that one legacy digest only; Azure and every other
@@ -5152,18 +6762,30 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5152
6762
  );
5153
6763
  const definitionVersionMatches =
5154
6764
  parsed.definitionVersion === resolved.model.definitionVersion ||
5155
- parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion;
5156
- const mismatched =
6765
+ parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion ||
6766
+ parsed.definitionVersion ===
6767
+ legacyCodexAstraImplicitCachingDefinitionVersionFor(resolved.model, resolved.provider) ||
6768
+ matchesAdditiveCapabilityDefinitionVersion(resolved.model, resolved.provider, parsed) ||
6769
+ matchesWebSearchEnablementDefinitionVersion(resolved.model, resolved.provider, parsed);
6770
+ const identityMismatched =
5157
6771
  parsed.providerId !== resolved.provider.id ||
5158
6772
  parsed.upstreamModelId !== resolved.model.upstreamModelId ||
5159
6773
  parsed.wireApi !== resolved.model.api ||
5160
- !definitionVersionMatches ||
5161
6774
  canonicalJson(parsed.credentialSource) !== canonicalJson(resolved.model.credentialSource) ||
5162
6775
  canonicalJson(parsed.billing) !== canonicalJson(resolved.model.billing);
5163
- if (mismatched) {
6776
+ // Identity/source changes must never enter a rollout-retry classification,
6777
+ // even when their definition digest also differs.
6778
+ if (identityMismatched && !legacyClaudeAccountingMatches) {
5164
6779
  throw new Error("Turn execution policy does not match the current provider definition");
5165
6780
  }
5166
- return { policy: parsed, provider: resolved.provider, model: resolved.model };
6781
+ if (!definitionVersionMatches && !legacyClaudeAccountingMatches) {
6782
+ throw new TurnExecutionPolicyDefinitionMismatchError();
6783
+ }
6784
+ return {
6785
+ policy: parsed,
6786
+ provider: resolved.provider,
6787
+ model: configuredModelForAcceptedTurnExecutionPolicy(resolved.model, resolved.provider, parsed),
6788
+ };
5167
6789
  }
5168
6790
 
5169
6791
  /**
@@ -5202,6 +6824,154 @@ export function configuredModelPricingSchedules(
5202
6824
  };
5203
6825
  }
5204
6826
 
6827
+ /**
6828
+ * Insights list-price metadata, including reviewed namespaced provider routes.
6829
+ * Kept separate from debit/catalog pricing: adding a comparison must not change
6830
+ * an accepted turn's frozen execution-definition hash or payer classification.
6831
+ * Inline registry and explicit product-ID prices always win over projections.
6832
+ */
6833
+ export function configuredModelListPricingSchedules(
6834
+ settings: Settings,
6835
+ ): Record<string, ModelPricingScheduleV1> {
6836
+ const prices = configuredModelPricingSchedules(settings);
6837
+ const configured = Object.fromEntries(
6838
+ Object.entries(parseModelPricingJson(settings.modelPricingJson)).map(([model, pricing]) => [
6839
+ model,
6840
+ normalizeModelPricingSchedule(pricing),
6841
+ ]),
6842
+ );
6843
+ // Bare new IDs need the actual configured public OpenAI route, not merely a
6844
+ // familiar model name. Azure/custom endpoints do not inherit these API rates.
6845
+ if (
6846
+ settings.openaiProvider === "openai" &&
6847
+ isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl ?? "https://api.openai.com/v1")
6848
+ ) {
6849
+ for (const model of configuredModels(settings)) {
6850
+ if (
6851
+ model.providerId !== builtinProviderId(settings) ||
6852
+ !model.upstreamModelId.startsWith("gpt-") ||
6853
+ prices[model.id] !== undefined
6854
+ )
6855
+ continue;
6856
+ const reviewed = reviewedModelListPricing[model.upstreamModelId];
6857
+ if (reviewed) prices[model.id] = reviewed;
6858
+ }
6859
+ }
6860
+ for (const provider of configuredRegistryProviders(settings)) {
6861
+ for (const model of provider.models) {
6862
+ if (prices[model.id] !== undefined) continue;
6863
+ const reviewed = reviewedProviderModelPricing(settings, provider, model, configured);
6864
+ if (reviewed) prices[model.id] = reviewed;
6865
+ }
6866
+ }
6867
+ return prices;
6868
+ }
6869
+
6870
+ /**
6871
+ * A reviewed upstream list rate may also describe a namespaced product route.
6872
+ * Never infer a rate by stripping arbitrary prefixes, matching labels, or
6873
+ * treating every OpenAI-compatible endpoint as the original provider.
6874
+ * This is pricing metadata only: credential selection and payer/metering are
6875
+ * still derived exclusively from the accepted execution policy.
6876
+ */
6877
+ function reviewedProviderModelPricing(
6878
+ settings: Settings,
6879
+ provider: InternalRegistryProvider,
6880
+ model: RegistryProvider["models"][number],
6881
+ configured: Record<string, ModelPricingScheduleV1>,
6882
+ ): ModelPricingScheduleV1 | undefined {
6883
+ const upstream = model.upstreamModelId ?? model.id;
6884
+ let priceId: string | undefined;
6885
+ let nativeClaude = false;
6886
+ switch (provider.kind) {
6887
+ case "anonymous":
6888
+ if (
6889
+ provider.baseUrl.replace(/\/$/u, "") === OPENROUTER_BASE_URL &&
6890
+ upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)
6891
+ ) {
6892
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6893
+ }
6894
+ break;
6895
+ case "codex-subscription":
6896
+ if (upstream.startsWith("gpt-")) priceId = upstream;
6897
+ break;
6898
+ case "xai-subscription":
6899
+ if (upstream.startsWith("grok-")) priceId = upstream;
6900
+ break;
6901
+ case "anthropic-workspace":
6902
+ case "anthropic-organization":
6903
+ case "claude-subscription-workspace":
6904
+ case "claude-subscription-organization":
6905
+ if (claudeNativeModelProfile(upstream)) {
6906
+ priceId = upstream;
6907
+ nativeClaude = true;
6908
+ }
6909
+ break;
6910
+ case "direct-openai-workspace":
6911
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6912
+ priceId = upstream;
6913
+ }
6914
+ break;
6915
+ case "vercel-gateway-managed":
6916
+ case "vercel-gateway-workspace":
6917
+ case "vercel-gateway-organization":
6918
+ // Only curated, provider-pinned routes use the conservative fallback.
6919
+ // Arbitrary Gateway custom models still require exact reported cost or
6920
+ // an explicit operator rate, even if their slugs resemble a known model.
6921
+ priceId = configuredGatewayCatalogModels(settings).find(
6922
+ (candidate) =>
6923
+ candidate.upstreamModelId === upstream &&
6924
+ Object.values(OPENGENI_GATEWAY_MODELS).some(
6925
+ (reviewed) => reviewed.upstreamModelId === candidate.upstreamModelId,
6926
+ ),
6927
+ )?.productId;
6928
+ break;
6929
+ case "openrouter-workspace":
6930
+ case "openrouter-organization":
6931
+ case "openrouter-managed":
6932
+ if (upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)) {
6933
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6934
+ }
6935
+ break;
6936
+ case "api-key":
6937
+ // Explicit deployments of the official APIs may use their own product
6938
+ // IDs. Custom gateways/proxies and Azure SKUs do not inherit API rates.
6939
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6940
+ priceId = upstream;
6941
+ } else if (provider.baseUrl.replace(/\/$/u, "") === "https://api.x.ai/v1") {
6942
+ if (upstream.startsWith("grok-")) priceId = upstream;
6943
+ } else if (
6944
+ provider.api === "anthropic-messages" &&
6945
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1" &&
6946
+ claudeNativeModelProfile(upstream)
6947
+ ) {
6948
+ priceId = upstream;
6949
+ nativeClaude = true;
6950
+ }
6951
+ break;
6952
+ default:
6953
+ return undefined;
6954
+ }
6955
+ if (!priceId) return undefined;
6956
+ // Explicit upstream overrides retain the existing Codex comparison behavior;
6957
+ // a product-ID override or inline registry rate still has higher precedence.
6958
+ if (configured[priceId]) return configured[priceId];
6959
+ const schedule =
6960
+ reviewedModelListPricing[priceId] ??
6961
+ defaultModelPricing[priceId] ??
6962
+ (priceId === DEFAULT_OPENROUTER_MODEL_ID ? reviewedFreeOpenRouterListPricing : undefined);
6963
+ if (!schedule) return undefined;
6964
+ if (!nativeClaude || provider.anthropic?.cacheTtl !== "1h") return schedule;
6965
+ // Anthropic's 1-hour cache writes are 2x base input, rather than 5m's 1.25x.
6966
+ return {
6967
+ ...schedule,
6968
+ default: {
6969
+ ...schedule.default,
6970
+ cacheWriteMicrosPerMillionTokens: schedule.default.inputMicrosPerMillionTokens * 2,
6971
+ },
6972
+ };
6973
+ }
6974
+
5205
6975
  /** Legacy flat projection: returns the default/below-threshold price. */
5206
6976
  export function configuredModelPricing(settings: Settings): Record<string, ModelPricing> {
5207
6977
  return Object.fromEntries(
@@ -5318,6 +7088,314 @@ export function calculateModelUsageCostBreakdown(
5318
7088
  options?: { latencyMode?: LatencyMode },
5319
7089
  ): ModelUsageCostBreakdown {
5320
7090
  const schedule = configuredModelPricingSchedules(settings)[model];
7091
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
7092
+ }
7093
+
7094
+ /** Provider-list/equivalent-credit comparison only; never debit authority. */
7095
+ export function calculateModelListUsageCostBreakdown(
7096
+ settings: Settings,
7097
+ model: string,
7098
+ usage: ModelUsageInput,
7099
+ options?: { latencyMode?: LatencyMode },
7100
+ ): ModelUsageCostBreakdown {
7101
+ const schedule = configuredModelListPricingSchedules(settings)[model];
7102
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
7103
+ }
7104
+
7105
+ /**
7106
+ * Forward-only list comparison snapshot for the model-call fact writer.
7107
+ * Callers must establish request price provenance (including geography/service
7108
+ * tier) and retain per-request counters. Unknown counters/TTL/modifiers yield
7109
+ * no class split, not invented zeros. Historical facts must not use this helper.
7110
+ * Native Claude details preserve cache_write_tokens_5m and cache_write_tokens_1h.
7111
+ */
7112
+ export function calculateModelListUsageCostSnapshot(
7113
+ settings: Settings,
7114
+ model: string,
7115
+ usage: ModelUsageInput,
7116
+ options?: { latencyMode?: LatencyMode; priceContextKnown?: boolean },
7117
+ ): ModelListUsageCostSnapshot {
7118
+ const schedule = configuredModelListPricingSchedules(settings)[model];
7119
+ const fallback = calculateUsageCostBreakdown(settings, model, usage, schedule, options);
7120
+ const unknown = { ...fallback, listByClassMicros: null, listByClassApprox: false };
7121
+ if (!schedule || !options?.priceContextKnown) return unknown;
7122
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
7123
+ const classes: ModelListCostClassesMicros = {
7124
+ uncachedInput: 0,
7125
+ cacheRead: 0,
7126
+ cacheWrite: 0,
7127
+ output: 0,
7128
+ };
7129
+ const rawCostByPricing = new Map<ModelPricing, number>();
7130
+ const native = nativeClaudeListWriteContext(settings, model);
7131
+ for (const entry of entries) {
7132
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
7133
+ return unknown;
7134
+ const cached = knownDetailTokenCounter(entry, [
7135
+ "cached_tokens",
7136
+ "cachedInputTokens",
7137
+ "cached_input_tokens",
7138
+ ]);
7139
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
7140
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
7141
+ return unknown;
7142
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
7143
+ if (cached > 0 && pricing.cachedInputMicrosPerMillionTokens === undefined) return unknown;
7144
+ if (writes > 0 && pricing.cacheWriteMicrosPerMillionTokens === undefined) return unknown;
7145
+ const costs: ModelListCostClassesMicros = {
7146
+ uncachedInput: Math.ceil(
7147
+ ((entry.inputTokens - cached - writes) * pricing.inputMicrosPerMillionTokens) / 1_000_000,
7148
+ ),
7149
+ cacheRead: Math.ceil((cached * (pricing.cachedInputMicrosPerMillionTokens ?? 0)) / 1_000_000),
7150
+ cacheWrite: Math.ceil((writes * (pricing.cacheWriteMicrosPerMillionTokens ?? 0)) / 1_000_000),
7151
+ output: Math.ceil((entry.outputTokens * pricing.outputMicrosPerMillionTokens) / 1_000_000),
7152
+ };
7153
+ if (native && writes > 0) {
7154
+ const fiveMinute = knownDetailTokenCounter(entry, ["cache_write_tokens_5m"]);
7155
+ const oneHour = knownDetailTokenCounter(entry, ["cache_write_tokens_1h"]);
7156
+ if (fiveMinute === undefined || oneHour === undefined || fiveMinute + oneHour !== writes)
7157
+ return unknown;
7158
+ if (native.publishedRates) {
7159
+ costs.cacheWrite =
7160
+ Math.ceil((fiveMinute * native.publishedRates.fiveMinute) / 1_000_000) +
7161
+ Math.ceil((oneHour * native.publishedRates.oneHour) / 1_000_000);
7162
+ } else if (
7163
+ !(
7164
+ (native.declaredTtl === "5m" && oneHour === 0) ||
7165
+ (native.declaredTtl === "1h" && fiveMinute === 0)
7166
+ )
7167
+ ) {
7168
+ // A single explicit override cannot establish two different TTL prices.
7169
+ return unknown;
7170
+ }
7171
+ }
7172
+ const raw = sumListCostClasses(costs);
7173
+ rawCostByPricing.set(pricing, (rawCostByPricing.get(pricing) ?? 0) + raw);
7174
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) classes[key] += costs[key];
7175
+ }
7176
+ let providerCostMicros = sumListCostClasses(classes);
7177
+ let creditCostMicros = 0;
7178
+ for (const [pricing, raw] of rawCostByPricing) {
7179
+ creditCostMicros += Math.ceil((raw * (10_000 + (pricing.marginBps ?? 0))) / 10_000);
7180
+ }
7181
+ let listByClassApprox = false;
7182
+ const latencyMode = options.latencyMode ?? "standard";
7183
+ if (latencyMode !== "standard") {
7184
+ const catalogSettings = settingsForTurnExecutionPolicy(settings, model);
7185
+ const resolved = resolveModelProvider(
7186
+ catalogSettings,
7187
+ canonicalizeConfiguredModelId(catalogSettings, model),
7188
+ );
7189
+ const multiplier = resolved?.model.capabilities.latencyModes.find(
7190
+ (mode) => mode.id === latencyMode && mode.runnable,
7191
+ )?.billingMultiplierBps;
7192
+ if (multiplier === undefined || multiplier <= 0) return unknown;
7193
+ providerCostMicros = Math.ceil((providerCostMicros * multiplier) / 10_000);
7194
+ creditCostMicros = Math.ceil((creditCostMicros * multiplier) / 10_000);
7195
+ const scaled = allocateLatencyListCostClasses(classes, multiplier, providerCostMicros);
7196
+ if (!scaled) return unknown;
7197
+ Object.assign(classes, scaled.classes);
7198
+ listByClassApprox = scaled.approximate;
7199
+ }
7200
+ if (![providerCostMicros, creditCostMicros, ...Object.values(classes)].every(knownTokenCounter))
7201
+ return unknown;
7202
+ return { providerCostMicros, creditCostMicros, listByClassMicros: classes, listByClassApprox };
7203
+ }
7204
+
7205
+ /**
7206
+ * Approximate class attribution of a previously recorded upstream list total.
7207
+ * Current reviewed rates are weights only: this never recomputes that total,
7208
+ * credits, or charges. Missing class counters/rates remain unknown. Historical
7209
+ * cache writes use the schedule's single TTL rate, not an exact TTL assertion.
7210
+ * Reasoning is already included in outputTokens and is never added again.
7211
+ */
7212
+ export function allocateRecordedModelListCostByClass(
7213
+ settings: Settings,
7214
+ model: string,
7215
+ usage: ModelUsageInput,
7216
+ recordedProviderCostMicros: number | null | undefined,
7217
+ ): ModelRecordedListCostAllocation {
7218
+ const unknown: ModelRecordedListCostAllocation = {
7219
+ listByClassMicros: null,
7220
+ listByClassApprox: false,
7221
+ };
7222
+ if (!knownTokenCounter(recordedProviderCostMicros)) return unknown;
7223
+ const schedule = configuredModelListPricingSchedules(settings)[model];
7224
+ if (!schedule) return unknown;
7225
+ const weights = { uncachedInput: 0n, cacheRead: 0n, cacheWrite: 0n, output: 0n };
7226
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
7227
+ for (const entry of entries) {
7228
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
7229
+ return unknown;
7230
+ const cached = knownDetailTokenCounter(entry, [
7231
+ "cached_tokens",
7232
+ "cachedInputTokens",
7233
+ "cached_input_tokens",
7234
+ ]);
7235
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
7236
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
7237
+ return unknown;
7238
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
7239
+ const counters = {
7240
+ uncachedInput: entry.inputTokens - cached - writes,
7241
+ cacheRead: cached,
7242
+ cacheWrite: writes,
7243
+ output: entry.outputTokens,
7244
+ };
7245
+ const rates = {
7246
+ uncachedInput: pricing.inputMicrosPerMillionTokens,
7247
+ cacheRead: pricing.cachedInputMicrosPerMillionTokens,
7248
+ cacheWrite: pricing.cacheWriteMicrosPerMillionTokens,
7249
+ output: pricing.outputMicrosPerMillionTokens,
7250
+ };
7251
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) {
7252
+ if (counters[key] === 0) continue;
7253
+ const rate = rates[key];
7254
+ if (!knownTokenCounter(rate)) return unknown;
7255
+ // Do not round or convert weights to Number: both can distort ratios.
7256
+ weights[key] += BigInt(counters[key]) * BigInt(rate);
7257
+ }
7258
+ }
7259
+ const classes = allocateRecordedListCostWeights(weights, recordedProviderCostMicros);
7260
+ return classes ? { listByClassMicros: classes, listByClassApprox: true } : unknown;
7261
+ }
7262
+
7263
+ const MODEL_LIST_COST_CLASS_KEYS = ["uncachedInput", "cacheRead", "cacheWrite", "output"] as const;
7264
+
7265
+ function sumListCostClasses(costs: ModelListCostClassesMicros): number {
7266
+ return MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + costs[key], 0);
7267
+ }
7268
+
7269
+ /** Integer-only largest remainder, preserving the supplied historical total. */
7270
+ function allocateRecordedListCostWeights(
7271
+ weights: Record<(typeof MODEL_LIST_COST_CLASS_KEYS)[number], bigint>,
7272
+ target: number,
7273
+ ): ModelListCostClassesMicros | undefined {
7274
+ const denominator = MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + weights[key], 0n);
7275
+ if (denominator === 0n) {
7276
+ return target === 0 ? { uncachedInput: 0, cacheRead: 0, cacheWrite: 0, output: 0 } : undefined;
7277
+ }
7278
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
7279
+ const numerator = BigInt(target) * weights[key];
7280
+ return { key, index, value: numerator / denominator, remainder: numerator % denominator };
7281
+ });
7282
+ const remaining = BigInt(target) - rows.reduce((total, row) => total + row.value, 0n);
7283
+ const ranked = [...rows].sort((a, b) =>
7284
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
7285
+ );
7286
+ for (let index = 0; index < Number(remaining); index++) ranked[index]!.value += 1n;
7287
+ return Object.fromEntries(
7288
+ rows.map((row) => [row.key, Number(row.value)]),
7289
+ ) as ModelListCostClassesMicros;
7290
+ }
7291
+
7292
+ function knownTokenCounter(value: unknown): value is number {
7293
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
7294
+ }
7295
+
7296
+ function knownDetailTokenCounter(
7297
+ entry: ModelUsageInput,
7298
+ keys: readonly string[],
7299
+ firstOnly = false,
7300
+ ): number | undefined {
7301
+ const details = Array.isArray(entry.inputTokensDetails)
7302
+ ? entry.inputTokensDetails
7303
+ : entry.inputTokensDetails
7304
+ ? [entry.inputTokensDetails]
7305
+ : [];
7306
+ if (details.length === 0) return undefined;
7307
+ let total = 0;
7308
+ for (const detail of details) {
7309
+ const present = keys.filter((key) => detail[key] !== undefined);
7310
+ if (present.length === 0) return undefined;
7311
+ for (const key of firstOnly ? present.slice(0, 1) : present) {
7312
+ if (!knownTokenCounter(detail[key])) return undefined;
7313
+ total += detail[key]!;
7314
+ }
7315
+ }
7316
+ return knownTokenCounter(total) ? total : undefined;
7317
+ }
7318
+
7319
+ function nativeClaudeListWriteContext(
7320
+ settings: Settings,
7321
+ modelId: string,
7322
+ ):
7323
+ | {
7324
+ declaredTtl: "off" | "5m" | "1h" | undefined;
7325
+ publishedRates?: { fiveMinute: number; oneHour: number };
7326
+ }
7327
+ | undefined {
7328
+ for (const provider of configuredRegistryProviders(settings)) {
7329
+ const model = provider.models.find((candidate) => candidate.id === modelId);
7330
+ if (!model || provider.api !== "anthropic-messages") continue;
7331
+ const result = { declaredTtl: provider.anthropic?.cacheTtl };
7332
+ const upstream = model.upstreamModelId ?? model.id;
7333
+ const overrides = parseModelPricingJson(settings.modelPricingJson);
7334
+ const official =
7335
+ provider.kind === "anthropic-workspace" ||
7336
+ provider.kind === "anthropic-organization" ||
7337
+ provider.kind === "claude-subscription-workspace" ||
7338
+ provider.kind === "claude-subscription-organization" ||
7339
+ (provider.kind === "api-key" &&
7340
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1");
7341
+ const published = reviewedModelListPricing[upstream]?.default;
7342
+ if (
7343
+ !official ||
7344
+ !claudeNativeModelProfile(upstream) ||
7345
+ !published?.cacheWriteMicrosPerMillionTokens ||
7346
+ model.pricing ||
7347
+ overrides[modelId] !== undefined ||
7348
+ overrides[upstream] !== undefined
7349
+ )
7350
+ return result;
7351
+ return {
7352
+ ...result,
7353
+ publishedRates: {
7354
+ fiveMinute: published.cacheWriteMicrosPerMillionTokens,
7355
+ oneHour: published.inputMicrosPerMillionTokens * 2,
7356
+ },
7357
+ };
7358
+ }
7359
+ return undefined;
7360
+ }
7361
+
7362
+ /** Deterministic largest-remainder allocation; disclose any fractional scaling. */
7363
+ function allocateLatencyListCostClasses(
7364
+ classes: ModelListCostClassesMicros,
7365
+ multiplier: number,
7366
+ target: number,
7367
+ ): { classes: ModelListCostClassesMicros; approximate: boolean } | undefined {
7368
+ if (
7369
+ !knownTokenCounter(multiplier) ||
7370
+ !knownTokenCounter(target) ||
7371
+ !Object.values(classes).every(knownTokenCounter)
7372
+ )
7373
+ return undefined;
7374
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
7375
+ const numerator = BigInt(classes[key]) * BigInt(multiplier);
7376
+ return { key, index, value: Number(numerator / 10_000n), remainder: numerator % 10_000n };
7377
+ });
7378
+ const remaining = target - rows.reduce((total, row) => total + row.value, 0);
7379
+ if (remaining < 0 || remaining > rows.length) return undefined;
7380
+ const ranked = [...rows].sort((a, b) =>
7381
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
7382
+ );
7383
+ for (let index = 0; index < remaining; index++) ranked[index]!.value += 1;
7384
+ return {
7385
+ classes: Object.fromEntries(
7386
+ rows.map((row) => [row.key, row.value]),
7387
+ ) as ModelListCostClassesMicros,
7388
+ approximate: rows.some((row) => row.remainder !== 0n),
7389
+ };
7390
+ }
7391
+
7392
+ function calculateUsageCostBreakdown(
7393
+ settings: Settings,
7394
+ model: string,
7395
+ usage: ModelUsageInput,
7396
+ schedule: ModelPricingScheduleV1 | undefined,
7397
+ options?: { latencyMode?: LatencyMode },
7398
+ ): ModelUsageCostBreakdown {
5321
7399
  if (!schedule) {
5322
7400
  throw new Error(`Missing model pricing for ${model}`);
5323
7401
  }
@@ -5764,6 +7842,7 @@ export function applyGitAuthPointerEnvironment(
5764
7842
  }
5765
7843
 
5766
7844
  export type StartupRetryOptions = {
7845
+ shouldRetry?: (error: unknown) => boolean;
5767
7846
  attempts?: number;
5768
7847
  initialDelayMs?: number;
5769
7848
  maxDelayMs?: number;
@@ -5778,7 +7857,7 @@ export type StartupRetryOptions = {
5778
7857
 
5779
7858
  export function startupRetryOptions(
5780
7859
  settings: Settings,
5781
- ): Required<Omit<StartupRetryOptions, "onRetry">> {
7860
+ ): Required<Omit<StartupRetryOptions, "onRetry" | "shouldRetry">> {
5782
7861
  return {
5783
7862
  attempts: settings.startupDependencyRetryAttempts,
5784
7863
  initialDelayMs: settings.startupDependencyRetryInitialDelayMs,
@@ -5798,7 +7877,7 @@ export async function retryStartupDependency<T>(
5798
7877
  try {
5799
7878
  return await operation();
5800
7879
  } catch (error) {
5801
- if (attempt >= attempts) {
7880
+ if (attempt >= attempts || options.shouldRetry?.(error) === false) {
5802
7881
  throw error;
5803
7882
  }
5804
7883
  const delayMs = Math.min(maxDelayMs, initialDelayMs * 2 ** (attempt - 1));
@@ -6310,8 +8389,99 @@ function isDigestPinnedModalDesktopImage(settings: Settings): boolean {
6310
8389
  );
6311
8390
  }
6312
8391
 
8392
+ export type TrustedProxyCidr = {
8393
+ address: string;
8394
+ prefix: number;
8395
+ family: "ipv4" | "ipv6";
8396
+ };
8397
+
8398
+ /**
8399
+ * Parse `OPENGENI_API_TRUSTED_PROXY_CIDRS`: comma-separated IPv4/IPv6 CIDRs or
8400
+ * single addresses. Throws on any malformed entry so a typo can never widen or
8401
+ * silently disable the trusted proxy set.
8402
+ */
8403
+ export function trustedProxyCidrEntries(raw: string): TrustedProxyCidr[] {
8404
+ const entries: TrustedProxyCidr[] = [];
8405
+ for (const entry of raw.split(",")) {
8406
+ const value = entry.trim();
8407
+ if (!value) continue;
8408
+ const slash = value.indexOf("/");
8409
+ const address = slash < 0 ? value : value.slice(0, slash);
8410
+ const version = isIP(address);
8411
+ const family = version === 4 ? "ipv4" : version === 6 ? "ipv6" : null;
8412
+ const maxPrefix = family === "ipv4" ? 32 : 128;
8413
+ const prefixText = slash < 0 ? String(maxPrefix) : value.slice(slash + 1);
8414
+ const prefix = /^\d{1,3}$/u.test(prefixText) ? Number(prefixText) : Number.NaN;
8415
+ if (!family || !Number.isInteger(prefix) || prefix < 0 || prefix > maxPrefix) {
8416
+ throw new Error(
8417
+ `OPENGENI_API_TRUSTED_PROXY_CIDRS entry "${value.slice(0, 64)}" is not an IP address or CIDR range`,
8418
+ );
8419
+ }
8420
+ entries.push({ address, prefix, family });
8421
+ }
8422
+ return entries;
8423
+ }
8424
+
8425
+ /**
8426
+ * Parse `OPENGENI_LOCAL_ALLOWED_ORIGINS`: comma-separated exact browser origins
8427
+ * (`http(s)://host[:port]`, no path, query, credentials, or wildcard). Throws on
8428
+ * any malformed entry so a typo can never widen or silently disable the local
8429
+ * browser boundary.
8430
+ */
8431
+ export function localAllowedOriginEntries(raw: string | undefined): string[] {
8432
+ const origins: string[] = [];
8433
+ for (const entry of (raw ?? "").split(",")) {
8434
+ const value = entry.trim();
8435
+ if (!value) continue;
8436
+ let url: URL | null = null;
8437
+ try {
8438
+ url = new URL(value);
8439
+ } catch {
8440
+ url = null;
8441
+ }
8442
+ if (
8443
+ !url ||
8444
+ (url.protocol !== "http:" && url.protocol !== "https:") ||
8445
+ url.username ||
8446
+ url.password ||
8447
+ url.hostname.includes("*") ||
8448
+ url.pathname !== "/" ||
8449
+ url.search ||
8450
+ url.hash ||
8451
+ value.endsWith("?") ||
8452
+ value.endsWith("#")
8453
+ ) {
8454
+ throw new Error(
8455
+ `OPENGENI_LOCAL_ALLOWED_ORIGINS entry "${value.slice(0, 64)}" is not an exact http(s) origin such as http://127.0.0.1:5173`,
8456
+ );
8457
+ }
8458
+ origins.push(url.origin);
8459
+ }
8460
+ return origins;
8461
+ }
8462
+
8463
+ const LOCAL_SANDBOX_API_ROUTES: readonly RegExp[] = [
8464
+ // Codemode calls, journal reads, catalog, and the Site/SDK proxy.
8465
+ /^\/v1\/workspaces\/[^/]+\/codemode(?:\/|$)/u,
8466
+ // First-party MCP, including its /docs and /files servers.
8467
+ /^\/v1\/workspaces\/[^/]+\/mcp(?:\/|$)/u,
8468
+ // The personal GitHub HTTPS smart-Git broker.
8469
+ /^\/v1\/git\/personal\/[A-Za-z0-9_-]{43}\/(?:info\/refs|git-upload-pack|git-receive-pack)$/u,
8470
+ ];
8471
+
8472
+ /**
8473
+ * Local development only: whether a normalized API path is one a sandbox calls
8474
+ * (Codemode, first-party MCP, and the personal Git broker). The Linux Docker
8475
+ * sandbox route relays only these, and the local API serves only these on the
8476
+ * addresses that only sandboxes use (see `apps/api/src/http/local-browser-boundary.ts`).
8477
+ */
8478
+ export function localSandboxApiRouteAllowed(pathname: string): boolean {
8479
+ return LOCAL_SANDBOX_API_ROUTES.some((route) => route.test(pathname));
8480
+ }
8481
+
6313
8482
  function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = process.env): void {
6314
8483
  temporalConnectionOptions(settings);
8484
+ localAllowedOriginEntries(settings.localAllowedOrigins);
6315
8485
  if (
6316
8486
  settings.organizationUserSetupEmailTokenTransport === "query" &&
6317
8487
  !settings.organizationUserSetupQueryEdgeSanitizationConfirmed
@@ -6405,6 +8575,21 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6405
8575
  );
6406
8576
  }
6407
8577
  }
8578
+ // The retired MCP-only name must never be ignored silently: a deployment
8579
+ // that trusted forwarded addresses would fall back to the transport peer.
8580
+ // "0" (the old .env.example value) means the same as the new default.
8581
+ const retiredTrustedProxyHops = source.OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS?.trim();
8582
+ if (retiredTrustedProxyHops && retiredTrustedProxyHops !== "0") {
8583
+ throw new Error(
8584
+ "OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS was renamed to OPENGENI_API_TRUSTED_PROXY_HOPS, which now sets the client address for every API rate limit and auth session; rename the variable",
8585
+ );
8586
+ }
8587
+ const trustedProxyCidrs = trustedProxyCidrEntries(settings.apiTrustedProxyCidrs);
8588
+ if (trustedProxyCidrs.length > 0 && settings.apiTrustedProxyHops === 0) {
8589
+ throw new Error(
8590
+ "OPENGENI_API_TRUSTED_PROXY_CIDRS requires OPENGENI_API_TRUSTED_PROXY_HOPS greater than 0",
8591
+ );
8592
+ }
6408
8593
  if (settings.mcpOauthEnabled) {
6409
8594
  if (settings.productAccessMode === "configured") {
6410
8595
  throw new Error(
@@ -6705,115 +8890,54 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6705
8890
  );
6706
8891
  }
6707
8892
  }
6708
- if (
6709
- settings.objectStorageBackend === "s3-compatible" ||
6710
- settings.objectStorageBackend === "aws-s3"
6711
- ) {
6712
- if (
6713
- Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
6714
- ) {
6715
- throw new Error(
6716
- "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
6717
- );
6718
- }
6719
- if (
6720
- settings.objectStorageBackend === "s3-compatible" &&
6721
- (settings.objectStorageEndpoint ||
6722
- settings.objectStorageInternalEndpoint ||
6723
- settings.objectStorageSandboxEndpoint) &&
6724
- (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
6725
- ) {
6726
- throw new Error(
6727
- "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
6728
- );
6729
- }
6730
- if (
6731
- settings.objectStorageAzureConnectionString ||
6732
- settings.objectStorageAzureAccountName ||
6733
- settings.objectStorageAzureAccountKey ||
6734
- settings.objectStorageAzureEndpoint
6735
- ) {
6736
- throw new Error(
6737
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6738
- );
6739
- }
6740
- if (
6741
- settings.objectStorageGcsProjectId ||
6742
- settings.objectStorageGcsCredentialsJson ||
6743
- settings.objectStorageGcsKeyFilename ||
6744
- settings.objectStorageGcsApiEndpoint
6745
- ) {
6746
- throw new Error(
6747
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6748
- );
6749
- }
6750
- } else if (settings.objectStorageBackend === "azure-blob") {
6751
- if (
6752
- settings.objectStorageEndpoint ||
6753
- settings.objectStorageInternalEndpoint ||
6754
- settings.objectStorageSandboxEndpoint ||
6755
- settings.objectStorageAccessKeyId ||
6756
- settings.objectStorageSecretAccessKey
6757
- ) {
6758
- throw new Error(
6759
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
6760
- );
6761
- }
6762
- if (
6763
- settings.objectStorageGcsProjectId ||
6764
- settings.objectStorageGcsCredentialsJson ||
6765
- settings.objectStorageGcsKeyFilename ||
6766
- settings.objectStorageGcsApiEndpoint
6767
- ) {
6768
- throw new Error(
6769
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6770
- );
6771
- }
6772
- const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
6773
- const hasSharedKey =
6774
- Boolean(settings.objectStorageAzureAccountName) &&
6775
- Boolean(settings.objectStorageAzureAccountKey);
6776
- if (!hasConnectionString && !hasSharedKey) {
6777
- throw new Error(
6778
- "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
6779
- );
6780
- }
6781
- } else {
6782
- if (
6783
- settings.objectStorageEndpoint ||
6784
- settings.objectStorageInternalEndpoint ||
6785
- settings.objectStorageSandboxEndpoint ||
6786
- settings.objectStorageAccessKeyId ||
6787
- settings.objectStorageSecretAccessKey
6788
- ) {
6789
- throw new Error(
6790
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
6791
- );
6792
- }
6793
- if (
6794
- settings.objectStorageAzureConnectionString ||
6795
- settings.objectStorageAzureAccountName ||
6796
- settings.objectStorageAzureAccountKey ||
6797
- settings.objectStorageAzureEndpoint
6798
- ) {
6799
- throw new Error(
6800
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6801
- );
6802
- }
6803
- if (settings.objectStorageGcsCredentialsJson) {
6804
- parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
6805
- }
6806
- }
8893
+ validateObjectStorageSettings(settings);
6807
8894
  if (settings.documentChunkOverlap >= settings.documentChunkSize) {
6808
8895
  throw new Error(
6809
8896
  "OPENGENI_DOCUMENT_CHUNK_OVERLAP must be smaller than OPENGENI_DOCUMENT_CHUNK_SIZE",
6810
8897
  );
6811
8898
  }
6812
8899
  parseExposedPorts(settings.dockerExposedPorts);
8900
+ // Malformed voice-input pricing deliberately does not fail boot: every
8901
+ // process shares this config, and a voice-only typo must not crash-loop
8902
+ // workers. The provider is withheld and the API logs voiceInputPricingIssues.
8903
+ parseRealtimeVoicePricingJson(settings.azureLivePricingJson, "OPENGENI_AZURE_LIVE_PRICING_JSON");
8904
+ parseRealtimeVoicePricingTableJson(
8905
+ settings.aiGatewayRealtimePricingJson,
8906
+ "OPENGENI_AI_GATEWAY_REALTIME_PRICING_JSON",
8907
+ );
6813
8908
  sandboxEnvironmentVariableNames(settings);
6814
8909
  sandboxLifecycleHookIds(settings);
6815
8910
  // Fail fast on a malformed warm-rate table (P2.1).
6816
- parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8911
+ const warmRates = parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8912
+ // The meter rounds each settled interval to whole USD micros. Fractional
8913
+ // micros/second would make the total depend on heartbeat frequency; only
8914
+ // integer rates have interval-independent settlement at the same elapsed time.
8915
+ if (
8916
+ settings.sandboxWarmBillingMode === "credits" &&
8917
+ Object.values(warmRates).some((rate) => !Number.isSafeInteger(rate))
8918
+ ) {
8919
+ throw new Error(
8920
+ "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON paid rates must be safe integers",
8921
+ );
8922
+ }
8923
+ if (
8924
+ settings.documentEmbeddingBillingMode === "credits" &&
8925
+ settings.documentEmbeddingProvider === "openai" &&
8926
+ settings.documentEmbeddingRateMicrosPerMillionBytes <= 0
8927
+ ) {
8928
+ throw new Error(
8929
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES must be positive when paid OpenAI embedding billing is enabled",
8930
+ );
8931
+ }
8932
+ if (
8933
+ settings.documentEmbeddingBillingMode === "credits" &&
8934
+ settings.documentEmbeddingProvider === "openai" &&
8935
+ !settings.documentEmbeddingCreditsActivatedAt
8936
+ ) {
8937
+ throw new Error(
8938
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT is required for paid OpenAI embeddings",
8939
+ );
8940
+ }
6817
8941
  if (settings.sandboxBackend === "opensandbox") {
6818
8942
  if (!/@sha256:[0-9a-f]{64}$/i.test(settings.openSandboxImage ?? "")) {
6819
8943
  throw new Error(
@@ -6832,12 +8956,9 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6832
8956
  throw new Error(`OPENGENI_MCP_SERVERS contains duplicate id ${server.id}`);
6833
8957
  }
6834
8958
  serverIds.add(server.id);
6835
- if (
6836
- server.connectionRef?.authoritySource === "host" &&
6837
- !settings.hostMcpAuthoritySourceAdmissionEnabled
6838
- ) {
8959
+ if (server.connectionRef?.authoritySource === "host") {
6839
8960
  throw new Error(
6840
- "OPENGENI_MCP_SERVERS host-owned connection refs require OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED=true after the whole API/worker fleet is upgraded",
8961
+ "OPENGENI_MCP_SERVERS host-owned connection refs are no longer supported; select ordinary native connections",
6841
8962
  );
6842
8963
  }
6843
8964
  }
@@ -6891,14 +9012,36 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6891
9012
  ordinaryCaptureTimeoutMs,
6892
9013
  drainCaptureTimeoutMs,
6893
9014
  );
6894
- if (!(rotationLeadMs > providerDeadlineCaptureTimeoutMs + reaperPeriod)) {
9015
+ const requiredRotationLeadMs =
9016
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS + providerDeadlineCaptureTimeoutMs + 2 * reaperPeriod;
9017
+ if (!(rotationLeadMs > requiredRotationLeadMs)) {
6895
9018
  throw new Error(
6896
9019
  `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}) must exceed the ` +
6897
- `largest durable snapshot or drain capture timeout plus one reaper period ` +
6898
- `(${providerDeadlineCaptureTimeoutMs + reaperPeriod}), including for persisted Modal ` +
9020
+ `legacy command stop grace, largest durable snapshot or drain capture timeout, and two reaper periods ` +
9021
+ `(${requiredRotationLeadMs}), including for persisted Modal ` +
6899
9022
  `leases after a default-backend rollout.`,
6900
9023
  );
6901
9024
  }
9025
+ const containmentMs = settings.sandboxIdleCommandContainmentMs;
9026
+ if (containmentMs !== undefined && !(containmentMs > settings.sandboxIdleGraceMs)) {
9027
+ throw new Error(
9028
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must exceed ` +
9029
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}): a box kept warm only by ` +
9030
+ `retained commands must stay available at least as long as an idle box awaiting drain.`,
9031
+ );
9032
+ }
9033
+ if (
9034
+ containmentMs !== undefined &&
9035
+ optionalEnvironmentValue("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS", source) !==
9036
+ undefined &&
9037
+ !(containmentMs < rotationLeadMs)
9038
+ ) {
9039
+ throw new Error(
9040
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must be strictly less than ` +
9041
+ `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}): an idle box must be checkpointed ` +
9042
+ `and stopped well before its provider deadline.`,
9043
+ );
9044
+ }
6902
9045
  if (settings.sandboxBackend === "modal") {
6903
9046
  const idleGraceMs = settings.sandboxIdleGraceMs;
6904
9047
  const lifecycle = effectiveSandboxLifecycle(settings, "modal");
@@ -6942,6 +9085,19 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6942
9085
  `OPENGENI_SANDBOX_IDLE_GRACE_MS.`,
6943
9086
  );
6944
9087
  }
9088
+ if (
9089
+ settings.modalIdleTimeoutSeconds !== undefined &&
9090
+ containmentMs !== undefined &&
9091
+ !(reaperPeriod + containmentMs + drainCaptureTimeoutMs < idleTimeoutMs)
9092
+ ) {
9093
+ throw new Error(
9094
+ `OPENGENI_SANDBOX_LEASE_REAPER_PERIOD_MS + OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS + ` +
9095
+ `drain capture timeout (${reaperPeriod} + ${containmentMs} + ${drainCaptureTimeoutMs}) ` +
9096
+ `must be strictly less than OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS*1000 (${idleTimeoutMs}): ` +
9097
+ `idle command containment must checkpoint a box before Modal's own idle reaper stops it. ` +
9098
+ `Lower OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS or raise the Modal idle timeout.`,
9099
+ );
9100
+ }
6945
9101
  }
6946
9102
  }
6947
9103
  // --- stream-token secret: required-when-desktop, but GRACEFULLY DEGRADE (stream-token availability contract) ---
@@ -6999,8 +9155,14 @@ export function validateModelCatalogSettings(
6999
9155
  provider.kind === "vercel-gateway-managed" ||
7000
9156
  provider.kind === "vercel-gateway-workspace" ||
7001
9157
  provider.kind === "vercel-gateway-organization" ||
9158
+ provider.kind === "direct-openai-workspace" ||
9159
+ provider.kind === "direct-azure-workspace" ||
7002
9160
  provider.kind === "openrouter-workspace" ||
7003
9161
  provider.kind === "openrouter-organization" ||
9162
+ provider.kind === "anthropic-organization" ||
9163
+ provider.kind === "anthropic-workspace" ||
9164
+ provider.kind === "claude-subscription-workspace" ||
9165
+ provider.kind === "claude-subscription-organization" ||
7004
9166
  provider.kind === "xai-subscription"
7005
9167
  ) {
7006
9168
  throw new Error(
@@ -7038,6 +9200,19 @@ export function validateModelCatalogSettings(
7038
9200
  // validated even when managed billing is disabled.
7039
9201
  const models = configuredModels(settings, source);
7040
9202
  const defaultCatalogSettings = settingsForTurnExecutionPolicy(settings, settings.openaiModel);
9203
+ const policy = settings.creditPromotionPolicy;
9204
+ const promotionalModelIds = new Set([
9205
+ ...(policy.defaultModelIds ?? []),
9206
+ ...(policy.signupModelIds ?? []),
9207
+ ...Object.values(policy.offers).flatMap((offer) => offer.eligibleModelIds ?? []),
9208
+ ]);
9209
+ for (const modelId of promotionalModelIds) {
9210
+ if (!models.some((model) => model.id === modelId && model.cost === "credits")) {
9211
+ throw new Error(
9212
+ `Promotional credit model ${modelId} must be a canonical credits-billed model in the catalog`,
9213
+ );
9214
+ }
9215
+ }
7041
9216
  const defaultCatalogModels =
7042
9217
  defaultCatalogSettings === settings ? models : configuredModels(defaultCatalogSettings, source);
7043
9218
  if (models.length === 0 && defaultCatalogModels.length === 0) {
@@ -7052,11 +9227,39 @@ export function validateModelCatalogSettings(
7052
9227
  `The default model ${settings.openaiModel} is not executable in the resolved model catalog`,
7053
9228
  );
7054
9229
  }
9230
+ // An operator-set credits default must name a credits-billed model in the
9231
+ // env catalog; a typo would otherwise fall back silently to the first
9232
+ // selectable credits model. Only an explicit value in code catalog mode is
9233
+ // checked: the built-in gpt-6-luna may be absent from a custom catalog, and
9234
+ // a database catalog is edited independently of this env value, so both keep
9235
+ // the documented fallback (docs/model-providers.md) instead of failing
9236
+ // every catalog read.
9237
+ const explicitCreditsDefault = source.OPENGENI_CREDITS_DEFAULT_MODEL?.trim();
9238
+ if (
9239
+ explicitCreditsDefault &&
9240
+ settings.modelCatalogSource === "code" &&
9241
+ settings.billingMode === "stripe"
9242
+ ) {
9243
+ const creditsDefaultId = canonicalizeConfiguredModelId(settings, settings.creditsDefaultModel);
9244
+ const creditsDefault = models.find((model) => model.id === creditsDefaultId);
9245
+ if (creditsDefault?.cost !== "credits") {
9246
+ throw new Error(
9247
+ `OPENGENI_CREDITS_DEFAULT_MODEL ${settings.creditsDefaultModel} is not a credits-billed model in the resolved model catalog`,
9248
+ );
9249
+ }
9250
+ }
7055
9251
 
7056
9252
  const deploymentProductIds = new Set(
7057
9253
  models.filter((model) => model.credentialSource.kind === "deployment").map((model) => model.id),
7058
9254
  );
7059
9255
  const noteProductIds = new Set(models.map((model) => model.id));
9256
+ if (settings.resolvedCodexModelsJson !== undefined) {
9257
+ for (const model of z
9258
+ .array(CodexCatalogModelSchema)
9259
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))) {
9260
+ noteProductIds.add(model.id);
9261
+ }
9262
+ }
7060
9263
  for (const model of configuredGatewayCatalogModels(settings)) {
7061
9264
  deploymentProductIds.add(model.productId);
7062
9265
  noteProductIds.add(model.productId);
@@ -7191,6 +9394,108 @@ export function resolveNatsCalloutConfig(settings: Settings): NatsCalloutConfig
7191
9394
  return { accountSeed, accountName, user, password };
7192
9395
  }
7193
9396
 
9397
+ function validateObjectStorageSettings(settings: ObjectStorageSettings): void {
9398
+ if (
9399
+ settings.objectStorageBackend === "s3-compatible" ||
9400
+ settings.objectStorageBackend === "aws-s3"
9401
+ ) {
9402
+ if (
9403
+ Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
9404
+ ) {
9405
+ throw new Error(
9406
+ "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
9407
+ );
9408
+ }
9409
+ if (
9410
+ settings.objectStorageBackend === "s3-compatible" &&
9411
+ (settings.objectStorageEndpoint ||
9412
+ settings.objectStorageInternalEndpoint ||
9413
+ settings.objectStorageSandboxEndpoint) &&
9414
+ (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
9415
+ ) {
9416
+ throw new Error(
9417
+ "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
9418
+ );
9419
+ }
9420
+ if (
9421
+ settings.objectStorageAzureConnectionString ||
9422
+ settings.objectStorageAzureAccountName ||
9423
+ settings.objectStorageAzureAccountKey ||
9424
+ settings.objectStorageAzureEndpoint
9425
+ ) {
9426
+ throw new Error(
9427
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
9428
+ );
9429
+ }
9430
+ if (
9431
+ settings.objectStorageGcsProjectId ||
9432
+ settings.objectStorageGcsCredentialsJson ||
9433
+ settings.objectStorageGcsKeyFilename ||
9434
+ settings.objectStorageGcsApiEndpoint
9435
+ ) {
9436
+ throw new Error(
9437
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
9438
+ );
9439
+ }
9440
+ } else if (settings.objectStorageBackend === "azure-blob") {
9441
+ if (
9442
+ settings.objectStorageEndpoint ||
9443
+ settings.objectStorageInternalEndpoint ||
9444
+ settings.objectStorageSandboxEndpoint ||
9445
+ settings.objectStorageAccessKeyId ||
9446
+ settings.objectStorageSecretAccessKey
9447
+ ) {
9448
+ throw new Error(
9449
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
9450
+ );
9451
+ }
9452
+ if (
9453
+ settings.objectStorageGcsProjectId ||
9454
+ settings.objectStorageGcsCredentialsJson ||
9455
+ settings.objectStorageGcsKeyFilename ||
9456
+ settings.objectStorageGcsApiEndpoint
9457
+ ) {
9458
+ throw new Error(
9459
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
9460
+ );
9461
+ }
9462
+ const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
9463
+ const hasSharedKey =
9464
+ Boolean(settings.objectStorageAzureAccountName) &&
9465
+ Boolean(settings.objectStorageAzureAccountKey);
9466
+ if (!hasConnectionString && !hasSharedKey) {
9467
+ throw new Error(
9468
+ "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
9469
+ );
9470
+ }
9471
+ } else {
9472
+ if (
9473
+ settings.objectStorageEndpoint ||
9474
+ settings.objectStorageInternalEndpoint ||
9475
+ settings.objectStorageSandboxEndpoint ||
9476
+ settings.objectStorageAccessKeyId ||
9477
+ settings.objectStorageSecretAccessKey
9478
+ ) {
9479
+ throw new Error(
9480
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
9481
+ );
9482
+ }
9483
+ if (
9484
+ settings.objectStorageAzureConnectionString ||
9485
+ settings.objectStorageAzureAccountName ||
9486
+ settings.objectStorageAzureAccountKey ||
9487
+ settings.objectStorageAzureEndpoint
9488
+ ) {
9489
+ throw new Error(
9490
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
9491
+ );
9492
+ }
9493
+ if (settings.objectStorageGcsCredentialsJson) {
9494
+ parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
9495
+ }
9496
+ }
9497
+ }
9498
+
7194
9499
  /**
7195
9500
  * The PRIVILEGED control-plane NATS login (api/worker). Present only when BOTH a
7196
9501
  * user and password are set; otherwise null and the bus connects anonymously (local
@@ -7202,7 +9507,9 @@ export interface NatsControlPlaneAuth {
7202
9507
  password: string;
7203
9508
  }
7204
9509
 
7205
- export function resolveNatsControlPlaneAuth(settings: Settings): NatsControlPlaneAuth | null {
9510
+ export function resolveNatsControlPlaneAuth(
9511
+ settings: Pick<Settings, "selfhostedNatsControlUser" | "selfhostedNatsControlPassword">,
9512
+ ): NatsControlPlaneAuth | null {
7206
9513
  const user = settings.selfhostedNatsControlUser?.trim();
7207
9514
  const password = settings.selfhostedNatsControlPassword?.trim();
7208
9515
  if (!user || !password) {
@@ -7218,6 +9525,11 @@ function splitCsv(raw: string): string[] {
7218
9525
  .filter(Boolean);
7219
9526
  }
7220
9527
 
9528
+ /** Image references a workspace may select as its default sandbox image. */
9529
+ export function sandboxImageAllowlist(settings: Pick<Settings, "sandboxImageAllowlist">): string[] {
9530
+ return [...new Set(splitCsv(settings.sandboxImageAllowlist ?? ""))];
9531
+ }
9532
+
7221
9533
  function uniqueEnvNames(raw: string[], fieldName: string): string[] {
7222
9534
  const seen = new Set<string>();
7223
9535
  const out: string[] = [];
@@ -7251,3 +9563,196 @@ function parseGcsCredentialsJson(raw: string): unknown {
7251
9563
  function delay(ms: number): Promise<void> {
7252
9564
  return new Promise((resolve) => setTimeout(resolve, ms));
7253
9565
  }
9566
+
9567
+ /** Native Claude connections reuse the encrypted workspace and organization boundaries. */
9568
+ export const CLAUDE_CONNECTION_KINDS = ["anthropic", "claude_subscription"] as const;
9569
+ export type ClaudeConnectionKind = (typeof CLAUDE_CONNECTION_KINDS)[number];
9570
+ // Per-model native Messages controls. Unknown IDs never inherit adaptive
9571
+ // thinking merely because they share a provider with a supported model.
9572
+ const CLAUDE_NATIVE_MODEL_PROFILES: Readonly<
9573
+ Record<
9574
+ string,
9575
+ Readonly<{
9576
+ efforts: readonly ReasoningEffort[];
9577
+ defaultEffort: ReasoningEffort | null;
9578
+ contextWindowTokens: number;
9579
+ maxOutputTokens: number;
9580
+ }>
9581
+ >
9582
+ > = Object.fromEntries([
9583
+ ...[
9584
+ ["claude-opus-5-5", "medium"],
9585
+ ["claude-sonnet-5-5", "medium"],
9586
+ ["claude-opus-5", "high"],
9587
+ ["claude-sonnet-5", "high"],
9588
+ ["claude-opus-4-8", "high"],
9589
+ ["claude-opus-4-7", "xhigh"],
9590
+ ].map(([id, defaultEffort]) => [
9591
+ id,
9592
+ {
9593
+ efforts: ["low", "medium", "high", "xhigh", "max"],
9594
+ defaultEffort,
9595
+ contextWindowTokens: 1_000_000,
9596
+ maxOutputTokens: 128_000,
9597
+ },
9598
+ ]),
9599
+ ...["claude-opus-4-6", "claude-sonnet-4-6"].map((id) => [
9600
+ id,
9601
+ {
9602
+ efforts: ["low", "medium", "high", "max"],
9603
+ defaultEffort: "high",
9604
+ contextWindowTokens: 1_000_000,
9605
+ maxOutputTokens: 128_000,
9606
+ },
9607
+ ]),
9608
+ [
9609
+ "claude-haiku-4-5-20251001",
9610
+ {
9611
+ efforts: [],
9612
+ defaultEffort: null,
9613
+ contextWindowTokens: 200_000,
9614
+ maxOutputTokens: 64_000,
9615
+ },
9616
+ ],
9617
+ ]);
9618
+ export function claudeNativeModelProfile(upstreamModelId: string) {
9619
+ return Object.hasOwn(CLAUDE_NATIVE_MODEL_PROFILES, upstreamModelId)
9620
+ ? CLAUDE_NATIVE_MODEL_PROFILES[upstreamModelId]
9621
+ : undefined;
9622
+ }
9623
+ export function claudeProviderId(
9624
+ kind: ClaudeConnectionKind,
9625
+ scope: "workspace" | "organization" = "organization",
9626
+ ): string {
9627
+ return `${scope}-${kind === "anthropic" ? "anthropic" : "claude-subscription"}`;
9628
+ }
9629
+ export type ClaudeConnectionCatalog = Partial<
9630
+ Record<
9631
+ ClaudeConnectionKind,
9632
+ {
9633
+ active?: boolean;
9634
+ models: readonly { upstreamModelId: string; label?: string | null }[];
9635
+ }
9636
+ >
9637
+ >;
9638
+ export function withClaudeConnectionCatalog(
9639
+ settings: Settings,
9640
+ connections: ClaudeConnectionCatalog,
9641
+ scope: "workspace" | "organization" = "organization",
9642
+ ): Settings {
9643
+ let providers = parseModelProvidersJson(settings.modelProvidersJson);
9644
+ for (const kind of CLAUDE_CONNECTION_KINDS) {
9645
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled) {
9646
+ providers = providers.filter((provider) => provider.id !== claudeProviderId(kind, scope));
9647
+ continue;
9648
+ }
9649
+ const connection = connections[kind];
9650
+ if (!connection) continue;
9651
+ const id = claudeProviderId(kind, scope);
9652
+ providers = providers.filter((provider) => provider.id !== id);
9653
+ if (!connection.models.length) continue;
9654
+ providers.push({
9655
+ id,
9656
+ label: kind === "anthropic" ? "Anthropic API" : "Claude subscription",
9657
+ kind: kind === "anthropic" ? `anthropic-${scope}` : `claude-subscription-${scope}`,
9658
+ api: "anthropic-messages",
9659
+ wireProfile: "openai",
9660
+ baseUrl: "https://api.anthropic.com/v1",
9661
+ anthropic: {
9662
+ auth: kind === "anthropic" ? "api-key" : "oauth",
9663
+ cacheTtl: "5m",
9664
+ maxOutputTokens: 128000,
9665
+ streamIdleTimeoutMs: 600000,
9666
+ },
9667
+ models: connection.models.map((model) => {
9668
+ const profile = claudeNativeModelProfile(model.upstreamModelId);
9669
+ const adaptiveThinking = Boolean(profile?.efforts.length);
9670
+ const contextWindowTokens = profile?.contextWindowTokens ?? 200_000;
9671
+ const outputReserve = profile?.maxOutputTokens ?? 32_000;
9672
+ return {
9673
+ contextWindowTokens,
9674
+ effectiveContextWindowTokens: contextWindowTokens - outputReserve,
9675
+ autoCompactTokenLimit:
9676
+ contextWindowTokens === 1_000_000
9677
+ ? 800_000
9678
+ : Math.min(150_000, contextWindowTokens - outputReserve - 18_000),
9679
+ id: id + "/" + model.upstreamModelId,
9680
+ upstreamModelId: model.upstreamModelId,
9681
+ label:
9682
+ model.label ??
9683
+ (model.upstreamModelId === "claude-opus-5-5"
9684
+ ? "Claude Opus 5.5"
9685
+ : model.upstreamModelId === "claude-sonnet-5-5"
9686
+ ? "Claude Sonnet 5.5"
9687
+ : model.upstreamModelId),
9688
+ reasoningEffort: adaptiveThinking,
9689
+ hostedWebSearch: false,
9690
+ capabilities: {
9691
+ ...legacyModelCapabilities(settings, {
9692
+ reasoningEffort: adaptiveThinking,
9693
+ hostedWebSearch: false,
9694
+ vision: true,
9695
+ }),
9696
+ inputFileMediaTypes: [],
9697
+ reasoning: {
9698
+ upstream: adaptiveThinking ? "supported" : "unknown",
9699
+ runnable: adaptiveThinking,
9700
+ efforts: [...(profile?.efforts ?? [])],
9701
+ defaultEffort: profile?.defaultEffort ?? null,
9702
+ required: false,
9703
+ },
9704
+ functionCalling: { upstream: "supported", runnable: true },
9705
+ promptCaching: { upstream: "supported", runnable: true, mode: "automatic" },
9706
+ },
9707
+ };
9708
+ }),
9709
+ });
9710
+ }
9711
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
9712
+ }
9713
+ export function withClaudeConnectionCredential(
9714
+ settings: Settings,
9715
+ kind: ClaudeConnectionKind,
9716
+ credential: string,
9717
+ scope: "workspace" | "organization" = "organization",
9718
+ credentialBinding?: { connectionId: string; credentialVersion: number },
9719
+ ): Settings {
9720
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled)
9721
+ throw new Error("Claude subscriptions are not enabled on this deployment");
9722
+ if (!credential.trim()) throw new Error("Claude credential is empty");
9723
+ const bundle =
9724
+ kind === "claude_subscription" && credential.trim().startsWith("{")
9725
+ ? ClaudeSubscriptionCredential.parse(JSON.parse(credential))
9726
+ : null;
9727
+ return {
9728
+ ...settings,
9729
+ modelProvidersJson: JSON.stringify(
9730
+ parseModelProvidersJson(settings.modelProvidersJson).map((provider) =>
9731
+ provider.id === claudeProviderId(kind, scope)
9732
+ ? {
9733
+ ...provider,
9734
+ apiKey: bundle?.token ?? credential,
9735
+ ...(kind === "claude_subscription" && provider.anthropic
9736
+ ? {
9737
+ anthropic: {
9738
+ ...provider.anthropic,
9739
+ identity: bundle?.identity,
9740
+ credentialBinding: credentialBinding
9741
+ ? {
9742
+ connectionId: credentialBinding.connectionId,
9743
+ credentialVersion: credentialBinding.credentialVersion,
9744
+ }
9745
+ : undefined,
9746
+ },
9747
+ }
9748
+ : {}),
9749
+ }
9750
+ : provider,
9751
+ ),
9752
+ ),
9753
+ };
9754
+ }
9755
+ export * from "./claude-subscription-usage";
9756
+ export * from "./claude-subscription-oauth";
9757
+ export * from "./subscription-account-selection";
9758
+ export * from "./claude-subscription-capacity";