@opengeni/config 1.0.4 → 1.1.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,12 +1,20 @@
1
+ export { managedUserEmailAllowed } from "./managed-user-admission";
2
+ import { isRetiredNativeAtlassianTool } from "@opengeni/contracts/atlassian-native-retirement";
1
3
  import {
4
+ directModelConnectionSpec,
2
5
  BillingMode,
3
6
  CAPABILITY_DESCRIPTORS,
7
+ agentConfigDeploymentLimitsFromAllowlist,
8
+ type AgentConfigDeploymentLimits,
4
9
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
10
+ DEFAULT_OPENGENI_DOCUMENTATION_URL,
11
+ currentAgentLearningToolSelection,
5
12
  Entitlements,
6
13
  EntitlementsMode,
7
14
  KnowledgeSourceSyncLimits,
8
15
  LatencyMode,
9
16
  MAX_NESTED_AGENT_DEPTH,
17
+ McpServerConnectionRef as PublicMcpServerConnectionRef,
10
18
  ProductAccessMode,
11
19
  ReasoningEffort,
12
20
  FIRST_PARTY_MCP_TOOL_NAMES,
@@ -24,6 +32,7 @@ import {
24
32
  type VideoGenerationResolution,
25
33
  type FirstPartyMcpToolName as FirstPartyMcpToolNameType,
26
34
  } from "@opengeni/contracts";
35
+ import type { CodeSearchDeploymentPolicy } from "@opengeni/contracts/code-search";
27
36
  import { CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS } from "@opengeni/codex";
28
37
  import {
29
38
  CODEX_FALLBACK_MODEL_SLUGS,
@@ -46,6 +55,7 @@ import {
46
55
  } from "@opengeni/xai-subscription";
47
56
  export { XAI_SUBSCRIPTION_MODEL_ID_PREFIX } from "@opengeni/xai-subscription";
48
57
  import { createHash } from "node:crypto";
58
+ import { isIP } from "node:net";
49
59
  import { z } from "zod";
50
60
 
51
61
  const envName = /^[A-Za-z_][A-Za-z0-9_]*$/;
@@ -61,6 +71,9 @@ export const DEFAULT_MODEL_COST_POLICY_JSON = JSON.stringify({
61
71
  // a configured request timeout may never consume the entire durable claim.
62
72
  export const SANDBOX_ARCHIVE_CAPTURE_MAX_TIMEOUT_MS = 60 * 60_000;
63
73
  export const SANDBOX_ARCHIVE_CAPTURE_SETTLEMENT_GRACE_MS = 10_000;
74
+ // Deadline rotation gives retained legacy commands a bounded stop window
75
+ // before capturing the still-running sandbox's current files.
76
+ export const SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS = 120_000;
64
77
  export const SANDBOX_SNAPSHOT_MAX_TIMEOUT_MS =
65
78
  SANDBOX_ARCHIVE_CAPTURE_MAX_TIMEOUT_MS - SANDBOX_ARCHIVE_CAPTURE_SETTLEMENT_GRACE_MS;
66
79
  export const GOOGLE_DRIVE_PROVIDER_REQUEST_TIMEOUT_MAX_MS = 60_000;
@@ -89,6 +102,13 @@ const EnvBoolean = z.preprocess((value) => {
89
102
  return value;
90
103
  }, z.boolean());
91
104
 
105
+ /** An absolute http(s) URL that is safe to render as a browser link. */
106
+ const PublicHttpUrl = z
107
+ .string()
108
+ .url()
109
+ .max(2_048)
110
+ .refine((value) => /^https?:\/\//iu.test(value), "must be an http(s) URL");
111
+
92
112
  /** Default pacing between consecutive no-input goal continuations. */
93
113
  export const DEFAULT_GOAL_IDLE_BACKOFF_MS: readonly number[] = [3_000, 30_000, 120_000, 300_000];
94
114
  export const DEFAULT_GOAL_IDLE_BACKOFF_MAX_MS = 600_000;
@@ -179,8 +199,9 @@ export const AGENT_INSTRUCTIONS_CORE_PLACEHOLDER = "{{core}}";
179
199
 
180
200
  /**
181
201
  * Default per-workspace agent persona template. This is the BRAND + tool-usage
182
- * opinion (the white-labellable surface): the "You are an OpenGeni workspace
183
- * agent." identity line, the framing/opinion lines, and the mount-path facts.
202
+ * opinion (the white-labellable surface): the general-assistant identity line,
203
+ * the framing/opinion lines, and the mount-path facts. Domain-specific tooling
204
+ * guidance (for example Terraform or Checkov) belongs in opt-in Skills.
184
205
  *
185
206
  * The CORE that MUST survive any override — the goal-loop ownership line (which
186
207
  * names the opengeni__goal_* tools) and the dynamic workspace-environment block
@@ -194,88 +215,45 @@ export const AGENT_INSTRUCTIONS_CORE_PLACEHOLDER = "{{core}}";
194
215
  * intentionally.
195
216
  */
196
217
  export const DEFAULT_AGENT_INSTRUCTIONS = [
197
- "You are an OpenGeni workspace agent.",
198
- "Follow the user's task and any enabled pack or skill instructions for the current role.",
199
- "Work inside the sandbox workspace and use filesystem and shell tools when useful.",
218
+ "You are an OpenGeni workspace agent: a general assistant for questions, writing, research, analysis, and technical work.",
219
+ "Follow the user's task and the applicable Skill instructions for the current role.",
220
+ "When a task needs files or commands, work inside the sandbox workspace with the filesystem and shell tools.",
200
221
  "Repository resources are mounted under repos/<host>/<owner>/<repo> unless the session specifies another collision-free mount path.",
201
222
  "File resources are mounted under .opengeni/files/<file-id>/ unless the session specifies another mount path.",
202
223
  "Attached files are mounted read-only; copy them before modifying.",
203
224
  "Installed and selected Skills appear in the session Skill index; follow its reading instructions and any role-specific guidance.",
204
- "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.",
205
- "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.",
206
- "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.",
207
- "Return concise, factual summaries with files changed, commands run, and remaining blockers.",
225
+ "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.",
226
+ "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.",
227
+ "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.",
228
+ "Answer questions directly and briefly; after making changes, say what changed, how you checked it, and anything still blocked.",
208
229
  AGENT_INSTRUCTIONS_CORE_PLACEHOLDER,
209
230
  ].join(" ");
210
231
 
211
- export const McpServerConnectionRefSchema = z
212
- .object({
213
- // Standalone ids are UUIDs; embedded hosts may use any stable opaque id.
214
- connectionId: z.string().min(1).optional(),
215
- provider: z.string().min(1).max(128).optional(),
216
- providerDomain: z.string().min(1),
217
- kind: z.enum(["oauth2", "api_key", "app_install", "delegated"]).optional(),
218
- scopes: z.array(z.string().min(1)).optional(),
219
- resource: z.string().min(1).optional(),
220
- selectedResources: z
221
- .array(
222
- z
223
- .object({
224
- id: z.string().min(1).max(512),
225
- kind: z.literal("repository"),
226
- })
227
- .strict(),
228
- )
229
- .min(1)
230
- .max(256)
231
- .superRefine((resources, context) => {
232
- const seen = new Set<string>();
233
- for (const [index, resource] of resources.entries()) {
234
- const key = `${resource.kind}\0${resource.id}`;
235
- if (seen.has(key)) {
236
- context.addIssue({
237
- code: "custom",
238
- message: "selectedResources must not contain duplicates",
239
- path: [index],
240
- });
241
- }
242
- seen.add(key);
243
- }
244
- })
245
- .optional(),
246
- authoritySource: z.literal("host").optional(),
247
- subjectScope: z.enum(["workspace", "subject"]).optional(),
248
- })
249
- .strict()
250
- .superRefine((reference, context) => {
251
- if (reference.authoritySource === "host" && !reference.connectionId) {
252
- context.addIssue({
253
- code: "custom",
254
- message: "host authority requires connectionId",
255
- path: ["connectionId"],
256
- });
257
- }
258
- if (!reference.selectedResources) return;
259
- if (!reference.connectionId) {
260
- context.addIssue({
261
- code: "custom",
262
- message: "selectedResources requires connectionId",
263
- path: ["connectionId"],
264
- });
265
- }
266
- if (!reference.provider) {
267
- context.addIssue({
268
- code: "custom",
269
- message: "selectedResources requires provider",
270
- path: ["provider"],
271
- });
232
+ // Configuration and public session attachments share one authority contract.
233
+ export const McpServerConnectionRefSchema = PublicMcpServerConnectionRef;
234
+ export type McpServerConnectionRef = z.infer<typeof McpServerConnectionRefSchema>;
235
+
236
+ /** Operator-owned protocol binding. Tool annotations cannot establish recovery. */
237
+ export const McpOperationRecoverySchema = z
238
+ .record(
239
+ z.string().min(1).max(512).regex(/^\S+$/u),
240
+ z.object({ observerTool: z.string().min(1).max(512).regex(/^\S+$/u) }).strict(),
241
+ )
242
+ .superRefine((bindings, context) => {
243
+ for (const [originalTool, binding] of Object.entries(bindings)) {
244
+ if (originalTool === binding.observerTool) {
245
+ context.addIssue({
246
+ code: "custom",
247
+ path: [originalTool, "observerTool"],
248
+ message: "MCP recovery observer must differ from the original tool",
249
+ });
250
+ }
272
251
  }
273
252
  });
274
- export type McpServerConnectionRef = z.infer<typeof McpServerConnectionRefSchema>;
275
253
 
276
254
  /** Public, digest-pinned desktop image used by Modal unless the operator overrides it. */
277
255
  export const DEFAULT_MODAL_IMAGE_REF =
278
- "opengenipublicneuacr.azurecr.io/opengeni-desktop@sha256:c3bd17b8841de1bff9bb2777aad422cf8c75de78e2c30f9ac81d0cd6810a1b78";
256
+ "opengenipublicneuacr.azurecr.io/opengeni-desktop@sha256:554d5b324a580071dd669e7c1190931e498eb1b397412a27c7de84a430ef393a";
279
257
 
280
258
  const SettingsSchema = z.object({
281
259
  serviceName: z.string().default("opengeni"),
@@ -285,6 +263,10 @@ const SettingsSchema = z.object({
285
263
  // Absent on dev/source builds — consumers must treat it as optional.
286
264
  serverVersion: z.string().optional(),
287
265
  databaseUrl: z.string().default("postgres://opengeni:opengeni@127.0.0.1:5432/opengeni"),
266
+ // API application pool only. Managed auth owns its separate pool; readiness
267
+ // reuses the application handle. Keep the historical default for existing
268
+ // deployments and let operators budget this per process with replica counts.
269
+ apiDatabasePoolMax: z.coerce.number().int().positive().default(32),
288
270
  // Step I (§7.8 runtime half). Dedicated Postgres schema for the EMBEDDED
289
271
  // topology. Default "" → standalone: no search_path scoping, server default
290
272
  // (`public`). When set (e.g. "opengeni"), the db handle + the managed-auth
@@ -326,6 +308,8 @@ const SettingsSchema = z.object({
326
308
  observabilityMetricsEnabled: EnvBoolean.default(true),
327
309
  observabilityOtlpEndpoint: z.string().url().optional(),
328
310
  observabilityOtlpHeaders: z.string().default(""),
311
+ observabilityDiagnosticsEndpoint: z.string().url().optional(),
312
+ observabilityDiagnosticsHeaders: z.string().default(""),
329
313
  analyticsEnabled: EnvBoolean.default(false),
330
314
  analyticsConsentRequired: EnvBoolean.default(true),
331
315
  analyticsReoClientId: z
@@ -340,13 +324,44 @@ const SettingsSchema = z.object({
340
324
  .max(32)
341
325
  .regex(/^G-[A-Z0-9]+$/u)
342
326
  .optional(),
327
+ // Optional operator-owned legal documents linked from the signed-out console.
328
+ // Unset by default, so a self-hosted deployment never shows another operator's
329
+ // policies; the managed service points these at its own published pages.
330
+ legalPrivacyPolicyUrl: PublicHttpUrl.optional(),
331
+ legalTermsOfServiceUrl: PublicHttpUrl.optional(),
332
+ // Optional operator support address, shown as a "Contact support" mailto link
333
+ // on the signed-out page and in the Help menu. Unset by default for the same
334
+ // reason as the legal links.
335
+ supportEmail: z.string().trim().email().max(254).optional(),
343
336
  publicBaseUrl: z.string().url().optional(),
337
+ // Product documentation the web console links from its Help menu. Absent
338
+ // means the public OpenGeni docs; `none` hides the link for deployments that
339
+ // publish no documentation of their own.
340
+ documentationUrl: z.preprocess(
341
+ (value) =>
342
+ typeof value === "string"
343
+ ? value.trim().toLowerCase() === "none"
344
+ ? null
345
+ : value.trim()
346
+ : value,
347
+ z
348
+ .url({ protocol: /^https?$/u, error: "must be an absolute http(s) URL or none" })
349
+ .nullable()
350
+ .default(DEFAULT_OPENGENI_DOCUMENTATION_URL),
351
+ ),
344
352
  // Standards-based OAuth authorization server for external workspace MCP
345
353
  // clients. Opt-in because it creates a new public authentication surface.
346
354
  mcpOauthEnabled: EnvBoolean.default(false),
347
- // Forwarded client addresses are ignored by default. Operators may trust an
355
+ // Client source address for every API rate limit, abuse quota, and auth
356
+ // session record (Better Auth, MCP OAuth registration, enrollment, account
357
+ // setup, login transactions). Forwarded client addresses are ignored by
358
+ // default so a caller cannot choose its own bucket. Operators may trust an
348
359
  // exact number of proxy hops only when direct access to the API is blocked.
349
- mcpOauthTrustedProxyHops: z.coerce.number().int().min(0).max(16).default(0),
360
+ apiTrustedProxyHops: z.coerce.number().int().min(0).max(16).default(0),
361
+ // Optional comma-separated CIDRs (or single addresses) of the proxy that
362
+ // connects to the API. When set, forwarded client addresses are honored
363
+ // only for requests whose transport peer is inside one of them.
364
+ apiTrustedProxyCidrs: z.string().default(""),
350
365
  // Browser origin when the web app and API use separate origins in local
351
366
  // development. Production normally leaves this unset and uses publicBaseUrl.
352
367
  webBaseUrl: z.string().url().optional(),
@@ -371,29 +386,15 @@ const SettingsSchema = z.object({
371
386
  .regex(/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/u)
372
387
  .optional(),
373
388
  productAccessMode: ProductAccessMode.default("local"),
374
- // --- canonical organization-tenancy authority activation, default OFF ---
375
- // The named PRE-ACTIVATION opt-out for the organization-tenancy program. FALSE (the
376
- // default, and the value an operator leaves in place to decline or defer) means
377
- // this deployment stays on the reversible legacy workspace-owned lane: no phase-F
378
- // subsystem may switch its access decision to organization/membership authority
379
- // ids. TRUE is an operator's explicit statement that the activation preconditions
380
- // in docs/organization-tenancy.md have been proven for this deployment and that
381
- // the one-way boundary is accepted.
382
- //
383
- // This is NOT a kill switch and NOT a rollback: once an activation migration has
384
- // committed, setting it back to false does not restore the legacy authority - only
385
- // forward recovery is available. It also grants and revokes nothing by itself;
386
- // every individual authorization decision keeps its own fences.
387
- //
388
- // No runtime path reads it yet: canonical activation (phase F) is unshipped, so
389
- // the flag exists to reserve the name, pin the safe default, and give every future
390
- // activation slice one gate to consult. EnvBoolean (NOT z.coerce.boolean(), which
391
- // coerces "false" -> true and would activate the moment an operator wrote the
392
- // variable out to disable it).
393
- organizationTenancyCanonicalActivationEnabled: EnvBoolean.default(false),
394
389
  billingMode: BillingMode.default("disabled"),
390
+ // Explicit launch gate for the one-time $10 verified self-service signup grant.
391
+ // A migration or deployment alone must not start issuing live credits.
392
+ verifiedSignupTrialCreditsEnabled: EnvBoolean.default(false),
395
393
  entitlementsMode: EntitlementsMode.default("none"),
396
394
  usageLimitsMode: UsageLimitsMode.default("none"),
395
+ // Gate new allowance policies until every API/worker in the fleet enforces
396
+ // them. Persisted-policy enforcement and recovery reads/clears stay active.
397
+ usageAllowancesEnabled: EnvBoolean.default(false),
397
398
  staticEntitlementsJson: z.string().default("{}"),
398
399
  staticUsageLimitsJson: z.string().default("{}"),
399
400
  delegationSecret: z.string().optional(),
@@ -434,11 +435,18 @@ const SettingsSchema = z.object({
434
435
  environmentsEncryptionKey: z.string().optional(),
435
436
  integrationsEnabled: EnvBoolean.default(false),
436
437
  integrationsStateSecret: z.string().optional(),
438
+ integrationsOauthShortStateEnabled: EnvBoolean.default(false),
437
439
  integrationsAllowPrivateNetworkTargets: EnvBoolean.default(false),
438
440
  integrationsOauthClientsJson: z.string().default("{}"),
441
+ gmailWatchTopicName: z
442
+ .string()
443
+ .regex(/^projects\/[^/]+\/topics\/[^/]+$/u)
444
+ .optional(),
439
445
  slackClientId: z.string().optional(),
440
446
  slackClientSecret: z.string().optional(),
441
447
  slackSigningSecret: z.string().optional(),
448
+ // Unlisted apps share Slack’s restricted history/replies quota across all tokens.
449
+ slackAccessMode: z.enum(["limited", "full"]).default("limited"),
442
450
  slackBotDisplayName: OpenGeniSlackBotDisplayName.default("OpenGeni"),
443
451
  slackCommand: z
444
452
  .string()
@@ -525,13 +533,6 @@ const SettingsSchema = z.object({
525
533
  // into @opengeni/db once at boot.
526
534
  // Env: OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED.
527
535
  childLifecycleNoticesEnabled: EnvBoolean.default(false),
528
- // Explicit host-owned MCP connection authority is a rolling protocol
529
- // activation. Keep it off while any API, worker, or browser bundle predates
530
- // the authority discriminator; enable it only after the whole fleet runs an
531
- // image that understands host refs. Legacy markerless non-UUID refs remain a
532
- // separate compatibility lane for already-persisted embedding integrations.
533
- // Env: OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED.
534
- hostMcpAuthoritySourceAdmissionEnabled: EnvBoolean.default(false),
535
536
  // Per-channel and per-DM Slack workspace routing. Default ON. A channel does
536
537
  // not count a personal workspace as a candidate, so an organization with one
537
538
  // shared workspace resolves it as the sole candidate and never asks; the
@@ -589,6 +590,9 @@ const SettingsSchema = z.object({
589
590
  authAllowMetrics: EnvBoolean.default(false),
590
591
  apiHost: z.string().default("0.0.0.0"),
591
592
  apiPort: z.coerce.number().int().positive().default(8000),
593
+ // When set, GET /metrics is served only on this dedicated internal listener
594
+ // and never on the public API port that an ingress forwards to.
595
+ apiMetricsPort: z.coerce.number().int().positive().optional(),
592
596
  workerHttpPort: z.coerce.number().int().positive().default(8001),
593
597
  // Worker-side first-party MCP traffic stays on the deployment's internal
594
598
  // network. OPENGENI_MCP_URL remains the sandbox/external route used by
@@ -599,11 +603,16 @@ const SettingsSchema = z.object({
599
603
  // call the public API with bearer credentials, but never receive credentialed
600
604
  // CORS responses.
601
605
  corsAllowOriginRegex: z.string().default(String.raw`^https?://(localhost|127\.0\.0\.1)(:\d+)?$`),
606
+ // Local development only (`local` access mode in the `local` environment):
607
+ // extra exact browser origins, comma-separated, that may call the
608
+ // unauthenticated API besides this stack's own web origin. The local browser
609
+ // boundary ignores `corsAllowOriginRegex`.
610
+ localAllowedOrigins: z.string().optional(),
602
611
  openaiProvider: z.enum(["openai", "azure"]).default("openai"),
603
612
  openaiApiKey: z.string().optional(),
604
613
  openaiBaseUrl: z.string().optional(),
605
- openaiModel: z.string().default("gpt-5.6-sol"),
606
- openaiAllowedModels: z.string().default("gpt-5.6-sol,gpt-5.6-terra,gpt-5.6-luna"),
614
+ openaiModel: z.string().default("gpt-6-astra"),
615
+ openaiAllowedModels: z.string().default("gpt-6-astra,gpt-6-sol,gpt-6-luna"),
607
616
  // OpenGeni-managed Vercel AI Gateway. When configured, the two reviewed
608
617
  // Gateway models below are added to the managed-credit catalog. Workspace
609
618
  // Gateway keys use the encrypted connection broker and never this secret.
@@ -732,6 +741,7 @@ const SettingsSchema = z.object({
732
741
  // binding so database mode cannot be bypassed with a second source.
733
742
  resolvedGatewayModelsJson: z.string().optional(),
734
743
  resolvedOpenRouterModelsJson: z.string().optional(),
744
+ resolvedCodexModelsJson: z.string().optional(),
735
745
  // Extra (non-built-in) model providers, declared by the host as a JSON
736
746
  // provider registry. Each entry carries its own base URL, API key, wire API
737
747
  // ("responses" | "chat") and the models it exposes. The models a client may
@@ -746,6 +756,7 @@ const SettingsSchema = z.object({
746
756
  // SuperGrok/xAI connected subscription. This is a workspace-scoped OAuth
747
757
  // account pool and a distinct rail from the existing xai/* API-key provider.
748
758
  supergrokSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED
759
+ claudeSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED
749
760
  // Maximum silence between complete, valid SuperGrok SSE data events. This is
750
761
  // not a request/run duration cap; every valid event resets the timer.
751
762
  supergrokResponseStreamIdleTimeoutMs: z.coerce
@@ -782,8 +793,27 @@ const SettingsSchema = z.object({
782
793
  // rotated TO — when EITHER usage window (5h/weekly) is at/over this percent. Default 90 to
783
794
  // match the UI danger flip (UsageBar danger at pct >= 90). OPENGENI_CODEX_ROTATION_NEAR_EXHAUSTION_PCT.
784
795
  codexRotationNearExhaustionPct: z.coerce.number().int().min(1).max(100).default(90),
796
+ reasoningConfigurationUpdatesEnabled: EnvBoolean.default(false),
785
797
  openaiReasoningEffort: ReasoningEffort.default("low"),
786
798
  openaiAllowedReasoningEfforts: z.string().default("low,medium,high,xhigh,max"),
799
+ // Default for new chats and scheduled tasks when the workspace has an
800
+ // OpenGeni credit balance, no saved workspace default, and no usable
801
+ // connected subscription, and the deployment default is not already a
802
+ // credits-billed model. Selected only when this model is selectable in the
803
+ // workspace catalog; otherwise the first selectable credits model is used.
804
+ // OPENGENI_CREDITS_DEFAULT_MODEL / OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT.
805
+ creditsDefaultModel: z
806
+ .string()
807
+ .trim()
808
+ .min(1)
809
+ .max(256)
810
+ .refine((value) => !/[\u000A\u000D|]/u.test(value), {
811
+ message: "credits default model must not contain newlines or the | field separator",
812
+ })
813
+ .default("gpt-6-luna"),
814
+ // Preferred effort for the credits default. Clamped to the highest effort
815
+ // the selected model supports at or below this value.
816
+ creditsDefaultReasoningEffort: ReasoningEffort.default("xhigh"),
787
817
  openaiResponsesTransport: z.enum(["http", "websocket"]).default("http"),
788
818
  // Provider-assigned item ids (rs_/msg_/fc_…) in Responses API input are
789
819
  // resolved against the provider's server-side response store. That store is
@@ -809,6 +839,19 @@ const SettingsSchema = z.object({
809
839
  // merged with the MCP-server tools (getAllTools = [...mcpTools, ...tools])
810
840
  // and the sandbox capability tools, never replacing them.
811
841
  webSearchEnabled: EnvBoolean.default(true),
842
+ // Jev (TypeSafe's fast judge model) for worker-side agent tools. Without a
843
+ // usable key every Jev-backed feature is off. The key stays on the server
844
+ // (API and worker) and never reaches a sandbox or Connected Machine.
845
+ jevApiKey: z.string().optional(),
846
+ jevBaseUrl: z.string().url().default("https://api.typesafe.ai"),
847
+ jevModel: z.string().trim().min(1).max(128).default("jev-latest"),
848
+ jevRequestTimeoutMs: z.coerce.number().int().positive().max(120_000).default(10_000),
849
+ // Jev-backed `code_search` agent tool. `off` never offers it, `opt_in` offers
850
+ // it only where workspace settings enable it, `default_on` offers it
851
+ // everywhere except workspaces that disable it, and `experiment` gives it to
852
+ // a fixed half of sessions in workspaces without their own setting, for
853
+ // comparison. It also needs jevApiKey.
854
+ codeSearchMode: z.enum(["off", "opt_in", "default_on", "experiment"]).default("off"),
812
855
  // Deployment-default agent persona template (the white-label surface). The
813
856
  // runtime resolves the effective template per turn as
814
857
  // per-session-override > per-workspace override > this default, substitutes
@@ -830,7 +873,7 @@ const SettingsSchema = z.object({
830
873
  // the verified, self-contained native artifact runtime at its fixed image
831
874
  // paths. Disabled by default so arbitrary/custom provider images never make
832
875
  // document/spreadsheet/presentation skills appear when their runtime is
833
- // absent. Per-pack/per-rig image overrides fail closed in the worker even
876
+
834
877
  // when this base-image contract is enabled.
835
878
  sandboxArtifactRuntimeEnabled: EnvBoolean.default(false),
836
879
  dockerExposedPorts: z.string().default(""),
@@ -842,6 +885,9 @@ const SettingsSchema = z.object({
842
885
  dockerWorkspaceBaseDir: z.string().min(1).optional(),
843
886
  modalAppName: z.string().default("opengeni-sandbox"),
844
887
  modalImageRef: z.string().optional(),
888
+ // Activate only after descriptor-aware readers, DB fences and the exact
889
+ // native image have passed the supervised-command rollout canary.
890
+ modalCommandSupervisionEnabled: EnvBoolean.default(false),
845
891
  // Provider-native immutable Modal image ID for the exact logical
846
892
  // `modalImageRef`. When set, the runtime uses ModalImageSelector.fromId and
847
893
  // never asks Modal to parse or import the registry ref. The logical ref is
@@ -919,12 +965,10 @@ const SettingsSchema = z.object({
919
965
  // Shared desktop toggle: this module reads it for the 6080 port-merge; the
920
966
  // owner module (P4.x) acts on it to launch the display stack.
921
967
  sandboxDesktopEnabled: EnvBoolean.default(false),
922
- // Human take-control toggle: when ON (default) the negotiated DesktopStream
923
- // cell advertises mode "interactive" — the noVNC viewer can drive mouse+keyboard
924
- // into :0 (x11vnc runs without -viewonly). Turn it OFF for a genuinely read-only
925
- // deployment: the cell reports mode "read-only" and the client disables the
926
- // "Take control" affordance. This gates the HUMAN viewer plane; agent
927
- // interaction is authorized through managed ComputerSession tools.
968
+ // Human sandbox input policy. Canonical ComputerSession attachments reflect
969
+ // it and /actions enforces it independently of the client. The legacy
970
+ // DesktopStream adapter also reports read-only when disabled. Agent tools
971
+ // retain their separate session-control authority.
928
972
  sandboxDesktopInteractive: EnvBoolean.default(true),
929
973
  // REAL PTY terminal toggle (P5.t): gates the ttyd pty-ws plane (7681) the API
930
974
  // mints over the SAME tunnel as the desktop. Defaults ON — the interactive
@@ -988,6 +1032,7 @@ const SettingsSchema = z.object({
988
1032
  // --- cloudflare (headless) ---
989
1033
  cloudflareWorkerUrl: z.string().url().optional(),
990
1034
  cloudflareApiKey: z.string().optional(),
1035
+ experimentalBrowserContextPoolEnabled: EnvBoolean.default(false),
991
1036
  // --- remote browser placements ---
992
1037
  // Provider credentials are injected only into the placement-resident
993
1038
  // browserd launch. They never enter session contracts, journals, or sandboxes.
@@ -1173,6 +1218,33 @@ const SettingsSchema = z.object({
1173
1218
  // getSettings caps the default at half a shorter configured Modal lifetime so
1174
1219
  // the entire reaper window always fits. Knob: OPENGENI_SANDBOX_IDLE_GRACE_MS.
1175
1220
  sandboxIdleGraceMs: z.coerce.number().int().positive().default(900_000),
1221
+ // Idle command containment. A legacy retained background command (a dev
1222
+ // server, a command whose output is still draining, a stopped command the
1223
+ // provider no longer answers for) keeps its box warm through a non-expiring
1224
+ // process holder, so the zero-holder drain never runs and the box would stay
1225
+ // up until the provider deadline kills it uncaptured. Once every session of
1226
+ // the sandbox group has been unused this long - no open turn or pending
1227
+ // request, no held wait_for_input, no other holder or writer, measured from
1228
+ // durable turn/holder/admission facts - the reaper checkpoints the workspace,
1229
+ // stops the box and settles those commands `lost` with reason
1230
+ // `idle_containment`. Default 30min: twice the default
1231
+ // idle grace, so a lease that is only waiting for a "glanced away" user is
1232
+ // never contained earlier than an idle lease would drain, and well inside
1233
+ // the 1h provider-deadline rotation lead, so an idle box is saved long before
1234
+ // the deadline path has to act. Set 0 to disable new idle enrollments without
1235
+ // disabling provider-deadline containment or cancelling an enrolled drain.
1236
+ // A positive window must exceed OPENGENI_SANDBOX_IDLE_GRACE_MS;
1237
+ // an explicit positive value must also stay below OPENGENI_SANDBOX_ROTATION_LEAD_MS,
1238
+ // and with an explicit OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS the reaper period
1239
+ // plus this window plus the drain capture budget must fit before it.
1240
+ // getSettings derives the unset default between those two for short-lived
1241
+ // provider lifetimes. Knob: OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS.
1242
+ sandboxIdleCommandContainmentMs: z.coerce
1243
+ .number()
1244
+ .int()
1245
+ .nonnegative()
1246
+ .optional()
1247
+ .transform((value) => (value === 0 ? undefined : value)),
1176
1248
  // MID-SESSION /workspace snapshot cadence (sandbox-file-persistence). The
1177
1249
  // reaper's drain-persist only protects boxes the reaper itself kills; a box
1178
1250
  // that dies any other way (Modal's hard creation-time timeout on a session
@@ -1256,6 +1328,7 @@ const SettingsSchema = z.object({
1256
1328
  // Shape: { "modal": 5, "runloop": 4, ... }. Backends absent here meter
1257
1329
  // warm-seconds but accrue NO warm_cost / debit (rate 0).
1258
1330
  sandboxWarmRateMicrosPerSecondJson: z.string().default("{}"),
1331
+ sandboxWarmBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1259
1332
  // Per-workspace warm cap (cumulative warm-seconds since the start of the UTC
1260
1333
  // month, summed over sandbox.warm_seconds). 0 = unbounded. A workspace over the
1261
1334
  // cap force-drains its VIEWER-ONLY boxes (guarded AND turn_holders=0 — a paying
@@ -1263,6 +1336,9 @@ const SettingsSchema = z.object({
1263
1336
  sandboxMaxWarmSecondsPerWorkspace: z.coerce.number().int().nonnegative().default(0),
1264
1337
  sandboxPreparationProfiles: z.string().default("none"),
1265
1338
  sandboxEnvAllowlist: z.string().default(""),
1339
+ // Comma-separated image references a workspace may select as its default
1340
+ // sandbox image. Empty keeps every workspace on the deployment image.
1341
+ sandboxImageAllowlist: z.string().default(""),
1266
1342
  objectStorageEndpoint: z.string().url().optional(),
1267
1343
  objectStorageInternalEndpoint: z.string().url().optional(),
1268
1344
  objectStorageSandboxEndpoint: z.string().url().optional(),
@@ -1291,6 +1367,13 @@ const SettingsSchema = z.object({
1291
1367
  documentEmbeddingDimensions: z.coerce.number().int().positive().default(3072),
1292
1368
  documentEmbeddingApiKey: z.string().optional(),
1293
1369
  documentEmbeddingBaseUrl: z.string().url().optional(),
1370
+ documentEmbeddingBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1371
+ // Explicit commercial cutover. Existing queued revisions remain unpriced;
1372
+ // only jobs created at or after this instant may enter the paid rail.
1373
+ documentEmbeddingCreditsActivatedAt: z.iso.datetime().optional(),
1374
+ // Customer tariff for the exact UTF-8 bytes sent to the embedder. Zero leaves
1375
+ // the paid mode unavailable; no commercial rate is selected by this PR.
1376
+ documentEmbeddingRateMicrosPerMillionBytes: z.coerce.number().int().nonnegative().default(0),
1294
1377
  documentCurationProvider: z.enum(["openai", "heuristic", "none"]).default("openai"),
1295
1378
  documentCurationModel: z.string().min(1).default("gpt-4o-mini"),
1296
1379
  documentCurationApiKey: z.string().optional(),
@@ -1328,7 +1411,17 @@ const SettingsSchema = z.object({
1328
1411
  managedAuthGithubClientSecret: z.string().optional(),
1329
1412
  // Rolling browser login-slot compatibility. Repository/deployment default is
1330
1413
  // deliberately legacy; changing to broker is an operator-authorized rollout.
1414
+ allowedUserEmails: z
1415
+ .array(z.email().transform((email) => email.toLowerCase()))
1416
+ .min(1)
1417
+ .optional(),
1331
1418
  managedAuthSessionSetMode: z.enum(["legacy", "dual", "broker"]).default("legacy"),
1419
+ // Deployment ceiling for new managed accounts. When false, managed auth
1420
+ // refuses every new Better Auth account (email/password sign-up and implicit
1421
+ // Google/GitHub sign-up) while existing sign-in, sessions, password reset,
1422
+ // email verification, and invitation-bound account setup keep working. Read
1423
+ // at startup; the 0585 runtime switch pauses sign-ups without a restart.
1424
+ managedAuthNewSignupsEnabled: EnvBoolean.default(true),
1332
1425
  // Query transport is an explicit second-stage rollout. A pre-compatibility
1333
1426
  // web image understands only fragment bearers, so API replicas must keep
1334
1427
  // generating fragment links until the compatible web fleet has converged.
@@ -1363,12 +1456,131 @@ const SettingsSchema = z.object({
1363
1456
  */
1364
1457
  headers: z.record(z.string(), z.string()).optional(),
1365
1458
  connectionRef: McpServerConnectionRefSchema.optional(),
1459
+ operationRecovery: McpOperationRecoverySchema.optional(),
1366
1460
  }),
1367
1461
  )
1368
1462
  .default([]),
1369
1463
  });
1370
1464
 
1371
1465
  export type Settings = z.infer<typeof SettingsSchema>;
1466
+
1467
+ // Independently supervised artifact services are not API/agent runtimes. Parse
1468
+ // only their actual capabilities, using the same field schemas as getSettings.
1469
+ const ArtifactServiceSettingsSchema = SettingsSchema.pick({
1470
+ serviceName: true,
1471
+ environment: true,
1472
+ deploymentRevision: true,
1473
+ dbSchema: true,
1474
+ rlsStrategy: true,
1475
+ observabilityStructuredLogs: true,
1476
+ observabilityMetricsEnabled: true,
1477
+ observabilityOtlpEndpoint: true,
1478
+ observabilityOtlpHeaders: true,
1479
+ });
1480
+ const ArtifactOutboxSettingsSchema = ArtifactServiceSettingsSchema.extend({
1481
+ natsUrl: SettingsSchema.shape.natsUrl,
1482
+ selfhostedNatsControlUser: SettingsSchema.shape.selfhostedNatsControlUser,
1483
+ selfhostedNatsControlPassword: SettingsSchema.shape.selfhostedNatsControlPassword,
1484
+ });
1485
+ const ObjectStorageSettingsSchema = SettingsSchema.pick({
1486
+ objectStorageBackend: true,
1487
+ objectStorageEndpoint: true,
1488
+ objectStorageInternalEndpoint: true,
1489
+ objectStorageSandboxEndpoint: true,
1490
+ objectStorageBucket: true,
1491
+ objectStorageRegion: true,
1492
+ objectStorageAccessKeyId: true,
1493
+ objectStorageSecretAccessKey: true,
1494
+ objectStorageForcePathStyle: true,
1495
+ objectStorageAzureConnectionString: true,
1496
+ objectStorageAzureAccountName: true,
1497
+ objectStorageAzureAccountKey: true,
1498
+ objectStorageAzureEndpoint: true,
1499
+ objectStorageGcsProjectId: true,
1500
+ objectStorageGcsCredentialsJson: true,
1501
+ objectStorageGcsKeyFilename: true,
1502
+ objectStorageGcsApiEndpoint: true,
1503
+ });
1504
+ const ArtifactMaterializerSettingsSchema = ArtifactServiceSettingsSchema.extend(
1505
+ ObjectStorageSettingsSchema.shape,
1506
+ );
1507
+ export type ArtifactOutboxSettings = z.infer<typeof ArtifactOutboxSettingsSchema>;
1508
+ export type ObjectStorageSettings = z.infer<typeof ObjectStorageSettingsSchema>;
1509
+ export type ArtifactMaterializerSettings = z.infer<typeof ArtifactMaterializerSettingsSchema>;
1510
+
1511
+ function artifactServiceEnvironment(source: NodeJS.ProcessEnv) {
1512
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1513
+ return {
1514
+ serviceName: optional("OPENGENI_SERVICE_NAME"),
1515
+ environment: optional("OPENGENI_ENVIRONMENT"),
1516
+ deploymentRevision:
1517
+ optional("OPENGENI_DEPLOYMENT_REVISION") ??
1518
+ optional("SOURCE_VERSION") ??
1519
+ optional("GITHUB_SHA"),
1520
+ dbSchema: optional("OPENGENI_DB_SCHEMA"),
1521
+ rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
1522
+ observabilityStructuredLogs: optional("OPENGENI_OBSERVABILITY_STRUCTURED_LOGS"),
1523
+ observabilityMetricsEnabled: optional("OPENGENI_OBSERVABILITY_METRICS_ENABLED"),
1524
+ observabilityOtlpEndpoint:
1525
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
1526
+ observabilityOtlpHeaders:
1527
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
1528
+ };
1529
+ }
1530
+
1531
+ export function getArtifactOutboxSettings(
1532
+ source: NodeJS.ProcessEnv = process.env,
1533
+ ): ArtifactOutboxSettings {
1534
+ const settings = ArtifactOutboxSettingsSchema.parse({
1535
+ ...artifactServiceEnvironment(source),
1536
+ natsUrl: optionalEnvironmentValue("OPENGENI_NATS_URL", source),
1537
+ selfhostedNatsControlUser: optionalEnvironmentValue(
1538
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_USER",
1539
+ source,
1540
+ ),
1541
+ selfhostedNatsControlPassword: optionalEnvironmentValue(
1542
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_PASSWORD",
1543
+ source,
1544
+ ),
1545
+ });
1546
+ dbSearchPath(settings);
1547
+ if (
1548
+ Boolean(settings.selfhostedNatsControlUser) !== Boolean(settings.selfhostedNatsControlPassword)
1549
+ ) {
1550
+ throw new Error("Artifact outbox NATS control user and password must be configured together");
1551
+ }
1552
+ return settings;
1553
+ }
1554
+
1555
+ export function getArtifactMaterializerSettings(
1556
+ source: NodeJS.ProcessEnv = process.env,
1557
+ ): ArtifactMaterializerSettings {
1558
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1559
+ const settings = ArtifactMaterializerSettingsSchema.parse({
1560
+ ...artifactServiceEnvironment(source),
1561
+ objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
1562
+ objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
1563
+ objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
1564
+ objectStorageBackend: optional("OPENGENI_OBJECT_STORAGE_BACKEND"),
1565
+ objectStorageBucket: optional("OPENGENI_OBJECT_STORAGE_BUCKET"),
1566
+ objectStorageRegion: optional("OPENGENI_OBJECT_STORAGE_REGION"),
1567
+ objectStorageAccessKeyId: optional("OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID"),
1568
+ objectStorageSecretAccessKey: optional("OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY"),
1569
+ objectStorageForcePathStyle: optional("OPENGENI_OBJECT_STORAGE_FORCE_PATH_STYLE"),
1570
+ objectStorageAzureConnectionString: optional("OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING"),
1571
+ objectStorageAzureAccountName: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME"),
1572
+ objectStorageAzureAccountKey: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY"),
1573
+ objectStorageAzureEndpoint: optional("OPENGENI_OBJECT_STORAGE_AZURE_ENDPOINT"),
1574
+ objectStorageGcsProjectId: optional("OPENGENI_OBJECT_STORAGE_GCS_PROJECT_ID"),
1575
+ objectStorageGcsCredentialsJson: optional("OPENGENI_OBJECT_STORAGE_GCS_CREDENTIALS_JSON"),
1576
+ objectStorageGcsKeyFilename: optional("OPENGENI_OBJECT_STORAGE_GCS_KEY_FILENAME"),
1577
+ objectStorageGcsApiEndpoint: optional("OPENGENI_OBJECT_STORAGE_GCS_API_ENDPOINT"),
1578
+ });
1579
+ dbSearchPath(settings);
1580
+ validateObjectStorageSettings(settings);
1581
+ return settings;
1582
+ }
1583
+
1372
1584
  export type McpServerConfig = Settings["mcpServers"][number];
1373
1585
 
1374
1586
  export type GoogleDriveProviderRetryOptions = {
@@ -1476,6 +1688,48 @@ export type VoiceInputProviderConfig =
1476
1688
  experimental: true;
1477
1689
  };
1478
1690
 
1691
+ function usableDeploymentSecret(value: string | null | undefined): string | undefined {
1692
+ return isUsableVoiceInputSecret(value) ? value : undefined;
1693
+ }
1694
+
1695
+ /** The deployment's Jev key, or undefined when it is missing or a placeholder. */
1696
+ export function usableJevApiKey(settings: Pick<Settings, "jevApiKey">): string | undefined {
1697
+ return usableDeploymentSecret(settings.jevApiKey);
1698
+ }
1699
+
1700
+ /** Deployment half of agent configuration: hard capability limits. */
1701
+ export function agentConfigDeploymentPolicy(
1702
+ settings: Pick<
1703
+ Settings,
1704
+ "webSearchEnabled" | "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools"
1705
+ >,
1706
+ ): AgentConfigDeploymentLimits {
1707
+ return agentConfigDeploymentLimitsFromAllowlist(
1708
+ resolveFirstPartyMcpToolPolicy(settings).allowed,
1709
+ settings.webSearchEnabled ? {} : { webSearch: "web search is turned off on this server" },
1710
+ );
1711
+ }
1712
+
1713
+ /**
1714
+ * Deployment half of the `code_search` decision. `available` is false when
1715
+ * the mode is off or no usable Jev key is configured; workspaces then cannot
1716
+ * turn it on. `workspaceDefault` applies to workspaces without their own
1717
+ * setting.
1718
+ */
1719
+ export function codeSearchDeploymentPolicy(
1720
+ settings: Pick<Settings, "codeSearchMode" | "jevApiKey">,
1721
+ ): CodeSearchDeploymentPolicy {
1722
+ const available = settings.codeSearchMode !== "off" && usableJevApiKey(settings) !== undefined;
1723
+ if (!available) return { available: false, workspaceDefault: "off" };
1724
+ const workspaceDefault =
1725
+ settings.codeSearchMode === "default_on"
1726
+ ? "on"
1727
+ : settings.codeSearchMode === "experiment"
1728
+ ? "split"
1729
+ : "off";
1730
+ return { available, workspaceDefault };
1731
+ }
1732
+
1479
1733
  /**
1480
1734
  * Reject empty / template secrets so `.env.example` placeholders like
1481
1735
  * `your-key` cannot advertise voice input as available and then 401 upstream.
@@ -1667,6 +1921,27 @@ export type ModelUsageCostBreakdown = {
1667
1921
  creditCostMicros: number;
1668
1922
  };
1669
1923
 
1924
+ export type ModelListCostClassesMicros = {
1925
+ uncachedInput: number;
1926
+ cacheRead: number;
1927
+ cacheWrite: number;
1928
+ output: number;
1929
+ };
1930
+
1931
+ export type ModelListUsageCostSnapshot = ModelUsageCostBreakdown & {
1932
+ /** Captured upstream list costs, summing to providerCostMicros, never debits. */
1933
+ listByClassMicros: ModelListCostClassesMicros | null;
1934
+ /** True only when a returned class split includes latency rounding allocation. */
1935
+ listByClassApprox: boolean;
1936
+ };
1937
+
1938
+ export type ModelRecordedListCostAllocation = {
1939
+ /** Approximate attribution of a stored upstream total, never a new price. */
1940
+ listByClassMicros: ModelListCostClassesMicros | null;
1941
+ /** True for every eligible historical allocation, including known zero. */
1942
+ listByClassApprox: boolean;
1943
+ };
1944
+
1670
1945
  export type StaticUsageLimitsConfig = StaticUsageLimits;
1671
1946
  export type EntitlementsConfig = Entitlements;
1672
1947
 
@@ -1837,7 +2112,7 @@ export type ModelExecutionLimitsV1 = {
1837
2112
 
1838
2113
  export type CredentialSourceV1 =
1839
2114
  | { kind: "deployment"; mechanism: "api_key" | "azure_ad_bearer" | "none" }
1840
- | { kind: "connected_subscription"; provider: "codex" | "xai" }
2115
+ | { kind: "connected_subscription"; provider: "codex" | "xai" | "claude" }
1841
2116
  | { kind: "workspace_connection"; mechanism: "api_key" }
1842
2117
  | { kind: "organization_connection"; mechanism: "api_key" };
1843
2118
 
@@ -1853,7 +2128,7 @@ export type BillingAttributionV1 = {
1853
2128
  * wired as "chat" because its beta Responses endpoint echoes input back and
1854
2129
  * silently no-ops hosted tools (see docs/model-providers.md).
1855
2130
  */
1856
- export const ModelProviderApi = z.enum(["responses", "chat"]);
2131
+ export const ModelProviderApi = z.enum(["responses", "chat", "anthropic-messages"]);
1857
2132
  export type ModelProviderApi = z.infer<typeof ModelProviderApi>;
1858
2133
 
1859
2134
  /**
@@ -1878,8 +2153,14 @@ export const RegistryProviderKind = z.enum([
1878
2153
  "vercel-gateway-managed",
1879
2154
  "vercel-gateway-workspace",
1880
2155
  "vercel-gateway-organization",
2156
+ "direct-openai-workspace",
2157
+ "direct-azure-workspace",
1881
2158
  "openrouter-workspace",
1882
2159
  "openrouter-organization",
2160
+ "anthropic-organization",
2161
+ "claude-subscription-organization",
2162
+ "anthropic-workspace",
2163
+ "claude-subscription-workspace",
1883
2164
  ]);
1884
2165
  export type RegistryProviderKind = z.infer<typeof RegistryProviderKind>;
1885
2166
 
@@ -1933,12 +2214,51 @@ const RegistryModelSchema = z
1933
2214
  });
1934
2215
 
1935
2216
  /** A non-built-in provider declared by the host via OPENGENI_MODEL_PROVIDERS_JSON. */
2217
+ export const ClaudeSubscriptionIdentity = z
2218
+ .object({
2219
+ accountUuid: z.union([z.string().uuid(), z.literal("")]),
2220
+ deviceId: z.string().regex(/^[a-f0-9]{64}$/),
2221
+ })
2222
+ .strict();
2223
+ export type ClaudeSubscriptionIdentity = z.infer<typeof ClaudeSubscriptionIdentity>;
2224
+ export const ClaudeSubscriptionCredential = z
2225
+ .object({
2226
+ version: z.literal(1),
2227
+ token: z.string().regex(/^sk-ant-oat[0-9]+-\S+$/),
2228
+ identity: ClaudeSubscriptionIdentity,
2229
+ oauth: z
2230
+ .object({
2231
+ refreshToken: z.string().min(1).max(16384),
2232
+ expiresAt: z.string().datetime(),
2233
+ scopes: z.array(z.string().min(1).max(256)).min(1).max(32),
2234
+ })
2235
+ .strict()
2236
+ .optional(),
2237
+ })
2238
+ .strict();
2239
+ const AnthropicProviderOptions = z.object({
2240
+ identity: ClaudeSubscriptionIdentity.optional(),
2241
+ // Native connection provenance only; never included in Anthropic request bodies.
2242
+ credentialBinding: z
2243
+ .object({
2244
+ connectionId: z.string().uuid(),
2245
+ credentialVersion: z.number().int().positive(),
2246
+ })
2247
+ .strict()
2248
+ .optional(),
2249
+ auth: z.enum(["api-key", "oauth"]).default("api-key"),
2250
+ cacheTtl: z.enum(["5m", "1h", "off"]).default("5m"),
2251
+ maxOutputTokens: z.number().int().positive().default(32000),
2252
+ streamIdleTimeoutMs: z.number().int().positive().default(600000),
2253
+ });
2254
+
1936
2255
  const RegistryProviderSchema = z
1937
2256
  .object({
1938
2257
  kind: RegistryProviderKind.default("api-key"),
1939
2258
  id: z.string().min(1).regex(registryId), // stable provider id, e.g. "fireworks"
1940
2259
  label: z.string().min(1).optional(),
1941
2260
  api: ModelProviderApi.default("chat"),
2261
+ anthropic: AnthropicProviderOptions.optional(),
1942
2262
  wireProfile: ModelProviderWireProfile.default("openai"),
1943
2263
  baseUrl: z.string().url(),
1944
2264
  apiKey: z.string().optional(), // inline key (pragmatic) ...
@@ -2026,6 +2346,10 @@ const RESERVED_MODEL_PROVIDER_IDS = new Set<string>([
2026
2346
  OPENROUTER_PROVIDER_ID,
2027
2347
  WORKSPACE_OPENROUTER_PROVIDER_ID,
2028
2348
  ORGANIZATION_OPENROUTER_PROVIDER_ID,
2349
+ "organization-anthropic",
2350
+ "organization-claude-subscription",
2351
+ "workspace-anthropic",
2352
+ "workspace-claude-subscription",
2029
2353
  ]);
2030
2354
 
2031
2355
  export const ModelCostClass = z.enum(["free", "credits"]);
@@ -2167,6 +2491,28 @@ const DeploymentGatewayCatalogModelSchema = GatewayCatalogModel.safeExtend({
2167
2491
  pricing: z.never().optional(),
2168
2492
  }).strict();
2169
2493
 
2494
+ const CodexCatalogModelSchema = RegistryModelSchema.safeExtend({
2495
+ retired: z.boolean().optional(),
2496
+ id: z.string().regex(/^codex\/[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2497
+ upstreamModelId: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2498
+ capabilities: ModelCapabilitiesV1Schema,
2499
+ pricing: z.never().optional(),
2500
+ })
2501
+ .strict()
2502
+ .superRefine((model, context) => {
2503
+ if (model.id !== `${CODEX_MODEL_ID_PREFIX}${model.upstreamModelId}`) {
2504
+ context.addIssue({
2505
+ code: "custom",
2506
+ path: ["id"],
2507
+ message: "Codex product id must match its upstream model slug",
2508
+ });
2509
+ }
2510
+ })
2511
+ .transform((model) => ({
2512
+ ...model,
2513
+ capabilities: reviewedCodexCatalogCapabilities(model.upstreamModelId, model.capabilities),
2514
+ }));
2515
+
2170
2516
  export const ModelCatalogDocument = z
2171
2517
  .object({
2172
2518
  schemaVersion: z.literal(1),
@@ -2174,8 +2520,9 @@ export const ModelCatalogDocument = z
2174
2520
  * fallback for existing documents; operators should set this explicitly
2175
2521
  * when cutting over a registry or connected-subscription default. */
2176
2522
  defaultModel: z.string().min(1).optional(),
2177
- builtInModels: z.array(z.string().min(1)).min(1),
2523
+ builtInModels: z.array(z.string().min(1)),
2178
2524
  registryProviders: z.array(DeploymentRegistryProviderSchema).default([]),
2525
+ codexModels: z.array(CodexCatalogModelSchema).optional(),
2179
2526
  gatewayModels: z.array(DeploymentGatewayCatalogModelSchema).default([]),
2180
2527
  openrouterModels: z.array(OpenRouterCatalogModel).default([]),
2181
2528
  modelNotes: z.record(z.string().min(1), ModelNote).default({}),
@@ -2186,6 +2533,13 @@ export const ModelCatalogDocument = z
2186
2533
  })
2187
2534
  .strict()
2188
2535
  .superRefine((document, context) => {
2536
+ if (document.builtInModels.length === 0 && !document.defaultModel) {
2537
+ context.addIssue({
2538
+ code: "custom",
2539
+ path: ["defaultModel"],
2540
+ message: "catalogs without built-in models require an explicit default model",
2541
+ });
2542
+ }
2189
2543
  const productIds = new Set<string>();
2190
2544
  const providerIds = new Set<string>();
2191
2545
  const gatewayUpstreamIds = new Set<string>();
@@ -2207,8 +2561,22 @@ export const ModelCatalogDocument = z
2207
2561
  productIds.add(id);
2208
2562
  };
2209
2563
  document.builtInModels.forEach((id, index) => add(id, ["builtInModels", index]));
2564
+ document.codexModels?.forEach((model, index) => add(model.id, ["codexModels", index, "id"]));
2565
+ if (
2566
+ document.codexModels?.some((model) => model.retired && model.id === document.defaultModel)
2567
+ ) {
2568
+ context.addIssue({
2569
+ code: "custom",
2570
+ path: ["defaultModel"],
2571
+ message: "a retired model cannot be the deployment default",
2572
+ });
2573
+ }
2210
2574
  document.registryProviders.forEach((provider, providerIndex) => {
2211
- if (RESERVED_MODEL_PROVIDER_IDS.has(provider.id)) {
2575
+ if (
2576
+ RESERVED_MODEL_PROVIDER_IDS.has(provider.id) ||
2577
+ provider.id.startsWith("workspace-openai-") ||
2578
+ provider.id.startsWith("workspace-azure-openai-")
2579
+ ) {
2212
2580
  context.addIssue({
2213
2581
  code: "custom",
2214
2582
  path: ["registryProviders", providerIndex, "id"],
@@ -2256,7 +2624,10 @@ export const ModelCatalogDocument = z
2256
2624
  if (
2257
2625
  document.defaultModel &&
2258
2626
  !productIds.has(document.defaultModel) &&
2259
- !document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX) &&
2627
+ !(
2628
+ document.codexModels === undefined &&
2629
+ document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX)
2630
+ ) &&
2260
2631
  !document.defaultModel.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX)
2261
2632
  ) {
2262
2633
  context.addIssue({
@@ -2341,6 +2712,8 @@ export function applyModelCatalogDocument(settings: Settings, rawDocument: unkno
2341
2712
  ),
2342
2713
  resolvedGatewayModelsJson: JSON.stringify(document.gatewayModels),
2343
2714
  resolvedOpenRouterModelsJson: JSON.stringify(document.openrouterModels),
2715
+ resolvedCodexModelsJson:
2716
+ document.codexModels === undefined ? undefined : JSON.stringify(document.codexModels),
2344
2717
  modelNotesJson: JSON.stringify(document.modelNotes),
2345
2718
  };
2346
2719
  return resolved;
@@ -2363,6 +2736,7 @@ export type IntegrationOAuthClientConfig = z.infer<typeof IntegrationOAuthClient
2363
2736
  * replacement.
2364
2737
  */
2365
2738
  export interface ResolvedModelProvider {
2739
+ anthropic?: z.infer<typeof AnthropicProviderOptions> | undefined;
2366
2740
  id: string; // "openai" | "azure" | registry id
2367
2741
  label: string;
2368
2742
  kind: RegistryProviderKind | "openrouter-managed";
@@ -2418,6 +2792,26 @@ export interface ConfiguredModel {
2418
2792
  hostedWebSearch: boolean;
2419
2793
  }
2420
2794
 
2795
+ /**
2796
+ * Whether a resolved route is the deployment-funded OpenRouter free tier: the
2797
+ * managed OpenRouter provider, which sends every workspace's requests with the
2798
+ * deployment's one OpenRouter key, serving an upstream `:free` variant (the
2799
+ * curated catalog admits nothing else). OpenRouter limits free-variant
2800
+ * requests per account per minute and per day, so each request on this route
2801
+ * spends quota that every turn on the deployment shares. A workspace or
2802
+ * organization OpenRouter connection uses its own account and is not this
2803
+ * route.
2804
+ */
2805
+ export function isManagedOpenRouterFreeRoute(route: {
2806
+ provider: Pick<ResolvedModelProvider, "kind">;
2807
+ configured: Pick<ConfiguredModel, "upstreamModelId">;
2808
+ }): boolean {
2809
+ return (
2810
+ route.provider.kind === "openrouter-managed" &&
2811
+ route.configured.upstreamModelId.endsWith(":free")
2812
+ );
2813
+ }
2814
+
2421
2815
  export const VERCEL_AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh/v1" as const;
2422
2816
  export const VERCEL_AI_GATEWAY_AI_SDK_BASE_URL = "https://ai-gateway.vercel.sh/v4/ai" as const;
2423
2817
  export const VERCEL_AI_GATEWAY_CONNECTION_DOMAIN = "ai-gateway.vercel.sh" as const;
@@ -2624,6 +3018,73 @@ export function configuredOpenRouterOrganizationProductModelIds(settings: Settin
2624
3018
  * llm-prices.com as a ground-truth canary; it does not generate this table.
2625
3019
  */
2626
3020
  export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
3021
+ "gpt-6-astra": {
3022
+ default: {
3023
+ // OpenAI list price: $10 / $1 cached / $12.50 cache write / $50 output.
3024
+ inputMicrosPerMillionTokens: 10_000_000,
3025
+ cachedInputMicrosPerMillionTokens: 1_000_000,
3026
+ cacheWriteMicrosPerMillionTokens: 12_500_000,
3027
+ outputMicrosPerMillionTokens: 50_000_000,
3028
+ marginBps: 500,
3029
+ },
3030
+ inputTokenTiers: [
3031
+ {
3032
+ // Prompts with more than 272K input tokens use 2x input and 1.5x output.
3033
+ minimumInputTokens: 272_001,
3034
+ pricing: {
3035
+ inputMicrosPerMillionTokens: 20_000_000,
3036
+ cachedInputMicrosPerMillionTokens: 2_000_000,
3037
+ cacheWriteMicrosPerMillionTokens: 25_000_000,
3038
+ outputMicrosPerMillionTokens: 75_000_000,
3039
+ marginBps: 500,
3040
+ },
3041
+ },
3042
+ ],
3043
+ },
3044
+ "gpt-6-sol": {
3045
+ default: {
3046
+ // OpenAI list price: $2 / $0.20 cached / $2.50 cache write / $10 output.
3047
+ inputMicrosPerMillionTokens: 2_000_000,
3048
+ cachedInputMicrosPerMillionTokens: 200_000,
3049
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3050
+ outputMicrosPerMillionTokens: 10_000_000,
3051
+ marginBps: 500,
3052
+ },
3053
+ inputTokenTiers: [
3054
+ {
3055
+ minimumInputTokens: 272_001,
3056
+ pricing: {
3057
+ inputMicrosPerMillionTokens: 4_000_000,
3058
+ cachedInputMicrosPerMillionTokens: 400_000,
3059
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3060
+ outputMicrosPerMillionTokens: 15_000_000,
3061
+ marginBps: 500,
3062
+ },
3063
+ },
3064
+ ],
3065
+ },
3066
+ "gpt-6-luna": {
3067
+ default: {
3068
+ // OpenAI list price: $0.10 / $0.01 cached / $0.125 cache write / $0.50 output.
3069
+ inputMicrosPerMillionTokens: 100_000,
3070
+ cachedInputMicrosPerMillionTokens: 10_000,
3071
+ cacheWriteMicrosPerMillionTokens: 125_000,
3072
+ outputMicrosPerMillionTokens: 500_000,
3073
+ marginBps: 500,
3074
+ },
3075
+ inputTokenTiers: [
3076
+ {
3077
+ minimumInputTokens: 272_001,
3078
+ pricing: {
3079
+ inputMicrosPerMillionTokens: 200_000,
3080
+ cachedInputMicrosPerMillionTokens: 20_000,
3081
+ cacheWriteMicrosPerMillionTokens: 250_000,
3082
+ outputMicrosPerMillionTokens: 750_000,
3083
+ marginBps: 500,
3084
+ },
3085
+ },
3086
+ ],
3087
+ },
2627
3088
  "gpt-5.6-sol": {
2628
3089
  default: {
2629
3090
  // Promotional OpenAI pricing, guaranteed through at least 2026-11-21.
@@ -2724,6 +3185,113 @@ export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
2724
3185
  },
2725
3186
  };
2726
3187
 
3188
+ /** Reviewed comparison rates only; never debit or execution-definition defaults. */
3189
+ export const reviewedModelListPricing: Record<string, ModelPricingScheduleV1> = {
3190
+ // Reviewed 2026-10-03 against https://developers.openai.com/api/docs/pricing
3191
+ // and https://ai-gateway.vercel.sh/v1/models. GPT-6.1 Sol's cache-read rate
3192
+ // is 5% of input, not GPT-6 Sol's 10%. The long-context boundary is >272K.
3193
+ "gpt-6.1-sol": {
3194
+ default: {
3195
+ inputMicrosPerMillionTokens: 2_000_000,
3196
+ cachedInputMicrosPerMillionTokens: 100_000,
3197
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3198
+ outputMicrosPerMillionTokens: 10_000_000,
3199
+ marginBps: 500,
3200
+ },
3201
+ inputTokenTiers: [
3202
+ {
3203
+ minimumInputTokens: 272_001,
3204
+ pricing: {
3205
+ inputMicrosPerMillionTokens: 4_000_000,
3206
+ cachedInputMicrosPerMillionTokens: 200_000,
3207
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3208
+ outputMicrosPerMillionTokens: 15_000_000,
3209
+ marginBps: 500,
3210
+ },
3211
+ },
3212
+ ],
3213
+ },
3214
+ // xAI Standard API list rates, reviewed 2026-10-03:
3215
+ // https://docs.x.ai/developers/models/grok-4.5 (and grok-4.6 / grok-4.7).
3216
+ // Preserve the native API's established >=200K boundary. Gateway's separately
3217
+ // configured >200K schedule is not interchangeable with this native schedule.
3218
+ // xAI's automatic caching has no separate cache-write surcharge.
3219
+ ...Object.fromEntries(
3220
+ (
3221
+ [
3222
+ ["grok-4.5", 300_000],
3223
+ ["grok-4.6", 500_000],
3224
+ ["grok-4.7", 500_000],
3225
+ ] as const
3226
+ ).map(([model, cachedInput]) => [
3227
+ model,
3228
+ {
3229
+ default: {
3230
+ inputMicrosPerMillionTokens: 2_000_000,
3231
+ cachedInputMicrosPerMillionTokens: cachedInput,
3232
+ outputMicrosPerMillionTokens: 6_000_000,
3233
+ marginBps: 500,
3234
+ },
3235
+ inputTokenTiers: [
3236
+ {
3237
+ minimumInputTokens: 200_000,
3238
+ pricing: {
3239
+ inputMicrosPerMillionTokens: 4_000_000,
3240
+ cachedInputMicrosPerMillionTokens: cachedInput * 2,
3241
+ outputMicrosPerMillionTokens: 12_000_000,
3242
+ marginBps: 500,
3243
+ },
3244
+ },
3245
+ ],
3246
+ },
3247
+ ]),
3248
+ ),
3249
+ // Every reviewed native Claude profile. Standard/global prices include
3250
+ // 5-minute cache writes; 1-hour native routes are projected separately.
3251
+ // https://platform.claude.com/docs/en/about-claude/pricing (2026-10-03).
3252
+ // Opus 5.5 cache reads are 5%, unlike the older models' 10%; none of these
3253
+ // models has a long-context premium.
3254
+ ...Object.fromEntries(
3255
+ (
3256
+ [
3257
+ ["claude-opus-5-5", 4_000_000, 200_000, 5_000_000, 20_000_000],
3258
+ ["claude-sonnet-5-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3259
+ ["claude-opus-5", 5_000_000, 500_000, 6_250_000, 25_000_000],
3260
+ ["claude-sonnet-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3261
+ ["claude-opus-4-8", 5_000_000, 500_000, 6_250_000, 25_000_000],
3262
+ ["claude-opus-4-7", 5_000_000, 500_000, 6_250_000, 25_000_000],
3263
+ ["claude-opus-4-6", 5_000_000, 500_000, 6_250_000, 25_000_000],
3264
+ ["claude-sonnet-4-6", 3_000_000, 300_000, 3_750_000, 15_000_000],
3265
+ ["claude-haiku-4-5-20251001", 1_000_000, 100_000, 1_250_000, 5_000_000],
3266
+ ] as const
3267
+ ).map(([model, input, cachedInput, cacheWrite, output]) => [
3268
+ model,
3269
+ {
3270
+ default: {
3271
+ inputMicrosPerMillionTokens: input,
3272
+ cachedInputMicrosPerMillionTokens: cachedInput,
3273
+ cacheWriteMicrosPerMillionTokens: cacheWrite,
3274
+ outputMicrosPerMillionTokens: output,
3275
+ marginBps: 500,
3276
+ },
3277
+ },
3278
+ ]),
3279
+ ),
3280
+ };
3281
+
3282
+ // Explicitly priced free variant, not an unknown rate. List-only metadata
3283
+ // preserves previously accepted free-route execution definitions.
3284
+ // https://openrouter.ai/api/v1/models, verified 2026-10-03.
3285
+ const reviewedFreeOpenRouterListPricing: ModelPricingScheduleV1 = {
3286
+ default: {
3287
+ inputMicrosPerMillionTokens: 0,
3288
+ cachedInputMicrosPerMillionTokens: 0,
3289
+ cacheWriteMicrosPerMillionTokens: 0,
3290
+ outputMicrosPerMillionTokens: 0,
3291
+ marginBps: 500,
3292
+ },
3293
+ };
3294
+
2727
3295
  // --- backend-gated required-credential table (the single source of truth) ---
2728
3296
  // Each sandbox backend declares ONLY its own required credentials: a deployment
2729
3297
  // configured for `sandboxBackend=modal` must carry the Modal token, but a
@@ -2815,6 +3383,9 @@ function optionalEnvironmentValue(name: string, source: NodeJS.ProcessEnv): stri
2815
3383
  return value && value.trim().length > 0 ? value : undefined;
2816
3384
  }
2817
3385
 
3386
+ // getSettings runs per request in some processes; say this once per process.
3387
+ let idleCommandContainmentDisabledWarned = false;
3388
+
2818
3389
  export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2819
3390
  const optional = (name: string): string | undefined => optionalEnvironmentValue(name, source);
2820
3391
  const modelCatalogSource = optional("OPENGENI_MODEL_CATALOG_SOURCE");
@@ -2830,6 +3401,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2830
3401
  optional("GITHUB_SHA"),
2831
3402
  serverVersion: optional("OPENGENI_SERVER_VERSION"),
2832
3403
  databaseUrl: optional("OPENGENI_DATABASE_URL"),
3404
+ apiDatabasePoolMax: optional("OPENGENI_API_DATABASE_POOL_MAX"),
2833
3405
  dbSchema: optional("OPENGENI_DB_SCHEMA"),
2834
3406
  rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
2835
3407
  runtimeDatabaseRole: optional("OPENGENI_RUNTIME_DATABASE_ROLE"),
@@ -2863,26 +3435,32 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2863
3435
  optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
2864
3436
  observabilityOtlpHeaders:
2865
3437
  optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
3438
+ observabilityDiagnosticsEndpoint: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_ENDPOINT"),
3439
+ observabilityDiagnosticsHeaders: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_HEADERS"),
2866
3440
  analyticsEnabled: optional("OPENGENI_ANALYTICS_ENABLED"),
2867
3441
  analyticsConsentRequired: optional("OPENGENI_ANALYTICS_CONSENT_REQUIRED"),
2868
3442
  analyticsReoClientId: optional("OPENGENI_ANALYTICS_REO_CLIENT_ID"),
2869
3443
  analyticsPosthogProjectKey: optional("OPENGENI_ANALYTICS_POSTHOG_PROJECT_KEY"),
2870
3444
  analyticsPosthogHost: optional("OPENGENI_ANALYTICS_POSTHOG_HOST"),
2871
3445
  analyticsGa4MeasurementId: optional("OPENGENI_ANALYTICS_GA4_MEASUREMENT_ID"),
3446
+ legalPrivacyPolicyUrl: optional("OPENGENI_LEGAL_PRIVACY_POLICY_URL"),
3447
+ legalTermsOfServiceUrl: optional("OPENGENI_LEGAL_TERMS_OF_SERVICE_URL"),
3448
+ supportEmail: optional("OPENGENI_SUPPORT_EMAIL"),
2872
3449
  publicBaseUrl: optional("OPENGENI_PUBLIC_BASE_URL"),
3450
+ documentationUrl: optional("OPENGENI_DOCUMENTATION_URL"),
2873
3451
  mcpOauthEnabled: optional("OPENGENI_MCP_OAUTH_ENABLED"),
2874
- mcpOauthTrustedProxyHops: optional("OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS"),
3452
+ apiTrustedProxyHops: optional("OPENGENI_API_TRUSTED_PROXY_HOPS"),
3453
+ apiTrustedProxyCidrs: optional("OPENGENI_API_TRUSTED_PROXY_CIDRS"),
2875
3454
  webBaseUrl: optional("OPENGENI_WEB_BASE_URL"),
2876
3455
  agentReleasesBaseUrl: optional("OPENGENI_AGENT_RELEASES_BASE_URL"),
2877
3456
  agentStableVersion: optional("OPENGENI_AGENT_STABLE_VERSION"),
2878
3457
  agentBetaVersion: optional("OPENGENI_AGENT_BETA_VERSION"),
2879
3458
  productAccessMode: optional("OPENGENI_PRODUCT_ACCESS_MODE"),
2880
- organizationTenancyCanonicalActivationEnabled: optional(
2881
- "OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED",
2882
- ),
2883
3459
  billingMode: optional("OPENGENI_BILLING_MODE"),
3460
+ verifiedSignupTrialCreditsEnabled: optional("OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED"),
2884
3461
  entitlementsMode: optional("OPENGENI_ENTITLEMENTS_MODE"),
2885
3462
  usageLimitsMode: optional("OPENGENI_USAGE_LIMITS_MODE"),
3463
+ usageAllowancesEnabled: optional("OPENGENI_USAGE_ALLOWANCES_ENABLED"),
2886
3464
  staticEntitlementsJson: optional("OPENGENI_STATIC_ENTITLEMENTS_JSON"),
2887
3465
  staticUsageLimitsJson: optional("OPENGENI_STATIC_USAGE_LIMITS_JSON"),
2888
3466
  delegationSecret: optional("OPENGENI_DELEGATION_SECRET"),
@@ -2902,13 +3480,16 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2902
3480
  environmentsEncryptionKey: optional("OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY"),
2903
3481
  integrationsEnabled: optional("OPENGENI_INTEGRATIONS_ENABLED"),
2904
3482
  integrationsStateSecret: optional("OPENGENI_INTEGRATIONS_STATE_SECRET"),
3483
+ integrationsOauthShortStateEnabled: optional("OPENGENI_INTEGRATIONS_OAUTH_SHORT_STATE_ENABLED"),
2905
3484
  integrationsAllowPrivateNetworkTargets: optional(
2906
3485
  "OPENGENI_INTEGRATIONS_ALLOW_PRIVATE_NETWORK_TARGETS",
2907
3486
  ),
2908
3487
  integrationsOauthClientsJson: optional("OPENGENI_INTEGRATIONS_OAUTH_CLIENTS_JSON"),
3488
+ gmailWatchTopicName: optional("OPENGENI_GMAIL_WATCH_TOPIC_NAME"),
2909
3489
  slackClientId: optional("OPENGENI_SLACK_CLIENT_ID"),
2910
3490
  slackClientSecret: optional("OPENGENI_SLACK_CLIENT_SECRET"),
2911
3491
  slackSigningSecret: optional("OPENGENI_SLACK_SIGNING_SECRET"),
3492
+ slackAccessMode: optional("OPENGENI_SLACK_ACCESS_MODE"),
2912
3493
  slackBotDisplayName: optional("OPENGENI_SLACK_BOT_DISPLAY_NAME"),
2913
3494
  slackCommand: optional("OPENGENI_SLACK_COMMAND"),
2914
3495
  googleDriveClientId: optional("OPENGENI_GOOGLE_DRIVE_CLIENT_ID"),
@@ -2943,9 +3524,6 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2943
3524
  goalIdleBackoffMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MS"),
2944
3525
  goalIdleBackoffMaxMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MAX_MS"),
2945
3526
  childLifecycleNoticesEnabled: optional("OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED"),
2946
- hostMcpAuthoritySourceAdmissionEnabled: optional(
2947
- "OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED",
2948
- ),
2949
3527
  slackWorkspaceRoutingEnabled: optional("OPENGENI_SLACK_WORKSPACE_ROUTING_ENABLED"),
2950
3528
  agentMaxModelCallsPerTurn: optional("OPENGENI_AGENT_MAX_MODEL_CALLS_PER_TURN"),
2951
3529
  contextWindowTokens: optional("OPENGENI_CONTEXT_WINDOW_TOKENS"),
@@ -2960,10 +3538,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2960
3538
  authAllowMetrics: optional("OPENGENI_AUTH_ALLOW_METRICS"),
2961
3539
  apiHost: optional("OPENGENI_API_HOST"),
2962
3540
  apiPort: optional("OPENGENI_API_PORT"),
3541
+ apiMetricsPort: optional("OPENGENI_API_METRICS_PORT"),
2963
3542
  workerHttpPort: optional("OPENGENI_WORKER_HTTP_PORT"),
2964
3543
  opengeniMcpInternalUrl: optional("OPENGENI_MCP_INTERNAL_URL"),
2965
3544
  opengeniMcpUrl: optional("OPENGENI_MCP_URL"),
2966
3545
  corsAllowOriginRegex: optional("OPENGENI_CORS_ALLOW_ORIGIN_REGEX"),
3546
+ localAllowedOrigins: optional("OPENGENI_LOCAL_ALLOWED_ORIGINS"),
2967
3547
  openaiProvider: optional("OPENGENI_OPENAI_PROVIDER"),
2968
3548
  openaiApiKey: optional("OPENGENI_OPENAI_API_KEY") ?? optional("OPENAI_API_KEY"),
2969
3549
  openaiBaseUrl: optional("OPENGENI_OPENAI_BASE_URL") ?? optional("OPENAI_BASE_URL"),
@@ -3022,6 +3602,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3022
3602
  modelProvidersJson: optional("OPENGENI_MODEL_PROVIDERS_JSON"),
3023
3603
  codexSubscriptionEnabled: optional("OPENGENI_CODEX_SUBSCRIPTION_ENABLED"),
3024
3604
  supergrokSubscriptionEnabled: optional("OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED"),
3605
+ claudeSubscriptionEnabled: optional("OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED"),
3025
3606
  supergrokResponseStreamIdleTimeoutMs: optional(
3026
3607
  "OPENGENI_SUPERGROK_RESPONSE_STREAM_IDLE_TIMEOUT_MS",
3027
3608
  ),
@@ -3030,13 +3611,23 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3030
3611
  lazyToolSearchEnabled: optional("OPENGENI_LAZY_TOOL_SEARCH_ENABLED"),
3031
3612
  codexFleetPolicyShadowEnabled: optional("OPENGENI_CODEX_FLEET_POLICY_SHADOW_ENABLED"),
3032
3613
  codexProductSku: optional("OPENGENI_CODEX_PRODUCT_SKU"),
3614
+ reasoningConfigurationUpdatesEnabled: optional(
3615
+ "OPENGENI_REASONING_CONFIGURATION_UPDATES_ENABLED",
3616
+ ),
3033
3617
  openaiReasoningEffort: optional("OPENGENI_OPENAI_REASONING_EFFORT"),
3034
3618
  openaiAllowedReasoningEfforts: optional("OPENGENI_OPENAI_ALLOWED_REASONING_EFFORTS"),
3619
+ creditsDefaultModel: optional("OPENGENI_CREDITS_DEFAULT_MODEL"),
3620
+ creditsDefaultReasoningEffort: optional("OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT"),
3035
3621
  openaiResponsesTransport: optional("OPENGENI_OPENAI_RESPONSES_TRANSPORT"),
3036
3622
  openaiProviderItemIds: optional("OPENGENI_OPENAI_PROVIDER_ITEM_IDS"),
3037
3623
  openaiReasoningEncryptedContent: optional("OPENGENI_OPENAI_REASONING_ENCRYPTED_CONTENT"),
3038
3624
  openaiMaxRetries: optional("OPENGENI_OPENAI_MAX_RETRIES"),
3039
3625
  webSearchEnabled: optional("OPENGENI_WEB_SEARCH_ENABLED"),
3626
+ jevApiKey: optional("OPENGENI_JEV_API_KEY"),
3627
+ jevBaseUrl: optional("OPENGENI_JEV_BASE_URL"),
3628
+ jevModel: optional("OPENGENI_JEV_MODEL"),
3629
+ jevRequestTimeoutMs: optional("OPENGENI_JEV_REQUEST_TIMEOUT_MS"),
3630
+ codeSearchMode: optional("OPENGENI_CODE_SEARCH_MODE"),
3040
3631
  agentInstructionsTemplate: optional("OPENGENI_AGENT_INSTRUCTIONS_TEMPLATE"),
3041
3632
  azureOpenaiBaseUrl: optional("OPENGENI_AZURE_OPENAI_BASE_URL"),
3042
3633
  azureOpenaiEndpoint: optional("OPENGENI_AZURE_OPENAI_ENDPOINT"),
@@ -3053,6 +3644,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3053
3644
  dockerWorkspaceBaseDir: optional("OPENGENI_DOCKER_WORKSPACE_BASE_DIR"),
3054
3645
  modalAppName: optional("OPENGENI_MODAL_APP_NAME"),
3055
3646
  modalImageRef: optional("OPENGENI_MODAL_IMAGE_REF") ?? DEFAULT_MODAL_IMAGE_REF,
3647
+ modalCommandSupervisionEnabled: optional("OPENGENI_MODAL_COMMAND_SUPERVISION_ENABLED"),
3056
3648
  modalImageId: optional("OPENGENI_MODAL_IMAGE_ID"),
3057
3649
  modalImageRegistrySecret: optional("OPENGENI_MODAL_IMAGE_REGISTRY_SECRET"),
3058
3650
  modalTimeoutSeconds: optional("OPENGENI_MODAL_TIMEOUT_SECONDS"),
@@ -3104,6 +3696,9 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3104
3696
  blaxelTtl: optional("OPENGENI_BLAXEL_TTL"),
3105
3697
  cloudflareWorkerUrl: optional("OPENGENI_CLOUDFLARE_WORKER_URL"),
3106
3698
  cloudflareApiKey: optional("OPENGENI_CLOUDFLARE_API_KEY"),
3699
+ experimentalBrowserContextPoolEnabled: optional(
3700
+ "OPENGENI_EXPERIMENTAL_BROWSER_CONTEXT_POOL_ENABLED",
3701
+ ),
3107
3702
  browserbaseApiKey: optional("OPENGENI_BROWSERBASE_API_KEY"),
3108
3703
  kernelApiKey: optional("OPENGENI_KERNEL_API_KEY"),
3109
3704
  kernelEndpoint: optional("OPENGENI_KERNEL_ENDPOINT"),
@@ -3152,6 +3747,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3152
3747
  sandboxViewerHolderTtlMs: optional("OPENGENI_SANDBOX_VIEWER_HOLDER_TTL_MS"),
3153
3748
  sandboxInteractionHolderTtlMs: optional("OPENGENI_SANDBOX_INTERACTION_HOLDER_TTL_MS"),
3154
3749
  sandboxIdleGraceMs: optional("OPENGENI_SANDBOX_IDLE_GRACE_MS"),
3750
+ sandboxIdleCommandContainmentMs: optional("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS"),
3155
3751
  sandboxSnapshotIntervalMs: optional("OPENGENI_SANDBOX_SNAPSHOT_INTERVAL_MS"),
3156
3752
  sandboxSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_SNAPSHOT_TIMEOUT_MS"),
3157
3753
  sandboxDrainSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_DRAIN_SNAPSHOT_TIMEOUT_MS"),
@@ -3165,9 +3761,11 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3165
3761
  sandboxWarmRateMicrosPerSecondJson: optional(
3166
3762
  "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON",
3167
3763
  ),
3764
+ sandboxWarmBillingMode: optional("OPENGENI_SANDBOX_WARM_BILLING_MODE"),
3168
3765
  sandboxMaxWarmSecondsPerWorkspace: optional("OPENGENI_SANDBOX_MAX_WARM_SECONDS_PER_WORKSPACE"),
3169
3766
  sandboxPreparationProfiles: optional("OPENGENI_SANDBOX_PREPARATION_PROFILES"),
3170
3767
  sandboxEnvAllowlist: optional("OPENGENI_SANDBOX_ENV_ALLOWLIST"),
3768
+ sandboxImageAllowlist: optional("OPENGENI_SANDBOX_IMAGE_ALLOWLIST"),
3171
3769
  objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
3172
3770
  objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
3173
3771
  objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
@@ -3194,6 +3792,13 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3194
3792
  documentEmbeddingDimensions: optional("OPENGENI_DOCUMENT_EMBEDDING_DIMENSIONS"),
3195
3793
  documentEmbeddingApiKey: optional("OPENGENI_DOCUMENT_EMBEDDING_API_KEY"),
3196
3794
  documentEmbeddingBaseUrl: optional("OPENGENI_DOCUMENT_EMBEDDING_BASE_URL"),
3795
+ documentEmbeddingBillingMode: optional("OPENGENI_DOCUMENT_EMBEDDING_BILLING_MODE"),
3796
+ documentEmbeddingCreditsActivatedAt: optional(
3797
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT",
3798
+ ),
3799
+ documentEmbeddingRateMicrosPerMillionBytes: optional(
3800
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES",
3801
+ ),
3197
3802
  documentCurationProvider: optional("OPENGENI_DOCUMENT_CURATION_PROVIDER"),
3198
3803
  documentCurationModel: optional("OPENGENI_DOCUMENT_CURATION_MODEL"),
3199
3804
  documentCurationApiKey: optional("OPENGENI_DOCUMENT_CURATION_API_KEY"),
@@ -3228,7 +3833,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3228
3833
  managedAuthGoogleClientSecret: optional("OPENGENI_MANAGED_AUTH_GOOGLE_CLIENT_SECRET"),
3229
3834
  managedAuthGithubClientId: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_ID"),
3230
3835
  managedAuthGithubClientSecret: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_SECRET"),
3836
+ allowedUserEmails:
3837
+ source.OPENGENI_ALLOWED_USER_EMAILS === undefined
3838
+ ? undefined
3839
+ : source.OPENGENI_ALLOWED_USER_EMAILS.split(",").map((email) => email.trim()),
3231
3840
  managedAuthSessionSetMode: optional("OPENGENI_MANAGED_AUTH_SESSION_SET_MODE"),
3841
+ managedAuthNewSignupsEnabled: optional("OPENGENI_MANAGED_AUTH_NEW_SIGNUPS_ENABLED"),
3232
3842
  organizationUserSetupEmailTokenTransport: optional(
3233
3843
  "OPENGENI_ORGANIZATION_USER_SETUP_EMAIL_TOKEN_TRANSPORT",
3234
3844
  ),
@@ -3252,14 +3862,80 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3252
3862
  : parsed.sandboxIdleGraceMs,
3253
3863
  sandboxRotationLeadMs:
3254
3864
  raw.sandboxRotationLeadMs === undefined && parsed.sandboxBackend === "modal"
3255
- ? Math.min(3_600_000, Math.floor((parsed.modalTimeoutSeconds * 1000) / 2))
3865
+ ? Math.min(
3866
+ 3_600_000,
3867
+ Math.max(
3868
+ Math.floor((parsed.modalTimeoutSeconds * 1000) / 2),
3869
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS +
3870
+ sandboxArchiveCaptureTimeoutMs({
3871
+ sandboxSnapshotTimeoutMs: Math.max(
3872
+ parsed.sandboxSnapshotTimeoutMs,
3873
+ parsed.sandboxDrainSnapshotTimeoutMs ?? parsed.sandboxSnapshotTimeoutMs,
3874
+ ),
3875
+ }) +
3876
+ 2 * parsed.sandboxLeaseReaperPeriodMs +
3877
+ 1,
3878
+ ),
3879
+ )
3256
3880
  : parsed.sandboxRotationLeadMs,
3257
3881
  mcpServers: ensureBuiltInMcpServers(parsed),
3258
3882
  };
3883
+ if (raw.sandboxIdleCommandContainmentMs === undefined) {
3884
+ // Strictly between the idle grace and the rotation lead whenever that
3885
+ // interval exists (short test/canary lifetimes), never above 30 minutes,
3886
+ // and early enough that an explicit Modal idle timeout cannot fire first.
3887
+ const modalIdleCeilingMs =
3888
+ settings.sandboxBackend === "modal" && settings.modalIdleTimeoutSeconds !== undefined
3889
+ ? settings.modalIdleTimeoutSeconds * 1000 -
3890
+ settings.sandboxLeaseReaperPeriodMs -
3891
+ sandboxArchiveCaptureTimeoutMs({
3892
+ sandboxSnapshotTimeoutMs: effectiveSandboxDrainSnapshotTimeoutMs(settings),
3893
+ }) -
3894
+ 1
3895
+ : Number.POSITIVE_INFINITY;
3896
+ const derived = Math.min(
3897
+ 1_800_000,
3898
+ Math.floor((settings.sandboxIdleGraceMs + settings.sandboxRotationLeadMs) / 2),
3899
+ modalIdleCeilingMs,
3900
+ );
3901
+ // A derived value never fails validation: with no room above the idle
3902
+ // grace, idle containment stays off and only the deadline rule applies.
3903
+ if (derived > settings.sandboxIdleGraceMs) {
3904
+ settings.sandboxIdleCommandContainmentMs = derived;
3905
+ } else {
3906
+ settings.sandboxIdleCommandContainmentMs = undefined;
3907
+ if (!idleCommandContainmentDisabledWarned) {
3908
+ idleCommandContainmentDisabledWarned = true;
3909
+ console.warn(
3910
+ "[config] idle command containment disabled: no window fits between " +
3911
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}) and the Modal idle ` +
3912
+ "timeout / rotation lead ceiling; set OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS to override.",
3913
+ );
3914
+ }
3915
+ }
3916
+ }
3917
+ warnRetiredOrganizationTenancyActivationSwitch(source);
3259
3918
  validateSettings(settings, source);
3260
3919
  return settings;
3261
3920
  }
3262
3921
 
3922
+ let retiredOrganizationTenancyActivationSwitchWarned = false;
3923
+
3924
+ /**
3925
+ * Session-tenancy activation is universal since migration 0611, so the former
3926
+ * deployment switch is accepted and ignored. Deployments that still set it keep
3927
+ * booting; the one-time warning tells the operator to delete it.
3928
+ */
3929
+ function warnRetiredOrganizationTenancyActivationSwitch(source: NodeJS.ProcessEnv): void {
3930
+ if (retiredOrganizationTenancyActivationSwitchWarned) return;
3931
+ if (source.OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED === undefined) return;
3932
+ retiredOrganizationTenancyActivationSwitchWarned = true;
3933
+ console.warn(
3934
+ "[config] OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED is retired and ignored: " +
3935
+ "every organization is session-tenancy activated (migration 0611). Remove it from the deployment.",
3936
+ );
3937
+ }
3938
+
3263
3939
  const LOCAL_FIRST_PARTY_DELEGATION_SECRET = "opengeni-local-first-party-delegation-secret-v1";
3264
3940
 
3265
3941
  /**
@@ -3290,9 +3966,13 @@ export type FirstPartyMcpToolPolicy = {
3290
3966
  export function resolveFirstPartyMcpToolPolicy(
3291
3967
  settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
3292
3968
  ): FirstPartyMcpToolPolicy {
3293
- const allowed = settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES];
3969
+ const allowed = currentAgentLearningToolSelection(
3970
+ settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES],
3971
+ ).filter((tool) => !isRetiredNativeAtlassianTool(tool));
3294
3972
  const allowedSet = new Set(allowed);
3295
- const defaults = settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS];
3973
+ const defaults = currentAgentLearningToolSelection(
3974
+ settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
3975
+ );
3296
3976
  return {
3297
3977
  default: defaults.filter((tool) => allowedSet.has(tool)),
3298
3978
  allowed: [...allowed],
@@ -3306,7 +3986,7 @@ export function allowedFirstPartyMcpToolsForSession(
3306
3986
  ): FirstPartyMcpToolNameType[] {
3307
3987
  const policy = resolveFirstPartyMcpToolPolicy(settings);
3308
3988
  const allowed = new Set(policy.allowed);
3309
- const tools = new Set(selected ?? policy.default);
3989
+ const tools = new Set(currentAgentLearningToolSelection(selected ?? policy.default));
3310
3990
  // Existing sessions with pause authority also receive its resume counterpart.
3311
3991
  if (tools.has("goal_pause")) tools.add("goal_resume");
3312
3992
  return [...tools].filter((tool) => allowed.has(tool));
@@ -3981,7 +4661,11 @@ function openRouterRegistryProvider(
3981
4661
 
3982
4662
  function configuredRegistryProviders(settings: Settings): InternalRegistryProvider[] {
3983
4663
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
3984
- const injected: InternalRegistryProvider[] = [...providers];
4664
+ const injected: InternalRegistryProvider[] = providers.filter(
4665
+ (provider) =>
4666
+ settings.claudeSubscriptionEnabled ||
4667
+ !(provider.api === "anthropic-messages" && provider.anthropic?.auth === "oauth"),
4668
+ );
3985
4669
  if (settings.vercelAiGatewayApiKey && configuredGatewayCatalogModels(settings).length > 0) {
3986
4670
  injected.push(
3987
4671
  gatewayRegistryProvider(settings, {
@@ -4000,6 +4684,53 @@ function configuredRegistryProviders(settings: Settings): InternalRegistryProvid
4000
4684
  return injected;
4001
4685
  }
4002
4686
 
4687
+ /** Customer-owned OpenAI/Azure routes, bound to immutable connection identity. */
4688
+ export function withDirectModelProviders(
4689
+ settings: Settings,
4690
+ connections: readonly {
4691
+ id: string;
4692
+ version: number;
4693
+ subjectId: string | null;
4694
+ kind: string;
4695
+ status: string;
4696
+ providerDomain: string;
4697
+ metadata: Record<string, unknown>;
4698
+ apiKey?: string;
4699
+ }[],
4700
+ ): Settings {
4701
+ const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
4702
+ (provider) =>
4703
+ !provider.id.startsWith("workspace-openai-") &&
4704
+ !provider.id.startsWith("workspace-azure-openai-"),
4705
+ );
4706
+ for (const connection of connections) {
4707
+ const spec = directModelConnectionSpec(connection);
4708
+ if (!spec) continue;
4709
+ providers.push({
4710
+ kind: spec.provider === "openai" ? "direct-openai-workspace" : "direct-azure-workspace",
4711
+ id: spec.providerId,
4712
+ label: spec.provider === "openai" ? "Your OpenAI" : "Your Azure OpenAI",
4713
+ api: "responses",
4714
+ wireProfile: spec.provider === "openai" ? "openai" : "azure-openai",
4715
+ baseUrl: spec.baseUrl,
4716
+ ...(connection.apiKey ? { apiKey: connection.apiKey } : {}),
4717
+ models: [
4718
+ {
4719
+ id: spec.modelId,
4720
+ upstreamModelId: spec.model,
4721
+ label: spec.model,
4722
+ capabilities: legacyModelCapabilities(settings, {
4723
+ reasoningEffort: false,
4724
+ hostedWebSearch: false,
4725
+ }),
4726
+ toolOutputTruncationTokens: settings.modelToolOutputTruncationTokens,
4727
+ },
4728
+ ],
4729
+ });
4730
+ }
4731
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
4732
+ }
4733
+
4003
4734
  /** Static catalog overlay; it contains no concrete workspace credential. */
4004
4735
  export function withWorkspaceGatewayCatalogProvider(
4005
4736
  settings: Settings,
@@ -4090,7 +4821,10 @@ export function withWorkspaceOpenRouterCredential(
4090
4821
  /** Secret-free organization Vercel AI Gateway catalog overlay. */
4091
4822
  export function withOrganizationGatewayCatalogProvider(
4092
4823
  settings: Settings,
4093
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4824
+ customModels: readonly {
4825
+ upstreamModelId: string;
4826
+ label?: string | null;
4827
+ }[] = [],
4094
4828
  ): Settings {
4095
4829
  if (customModels.length === 0) return settings;
4096
4830
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
@@ -4106,7 +4840,10 @@ export function withOrganizationGatewayCatalogProvider(
4106
4840
  export function withOrganizationGatewayCredential(
4107
4841
  settings: Settings,
4108
4842
  apiKey: string,
4109
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4843
+ customModels: readonly {
4844
+ upstreamModelId: string;
4845
+ label?: string | null;
4846
+ }[] = [],
4110
4847
  ): Settings {
4111
4848
  if (!apiKey.trim()) throw new Error("organization AI Gateway credential is empty");
4112
4849
  const catalog = withOrganizationGatewayCatalogProvider(settings, customModels);
@@ -4119,7 +4856,10 @@ export function withOrganizationGatewayCredential(
4119
4856
  /** Secret-free organization OpenRouter catalog overlay. */
4120
4857
  export function withOrganizationOpenRouterCatalogProvider(
4121
4858
  settings: Settings,
4122
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4859
+ customModels: readonly {
4860
+ upstreamModelId: string;
4861
+ label?: string | null;
4862
+ }[] = [],
4123
4863
  ): Settings {
4124
4864
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
4125
4865
  (provider) => provider.id !== ORGANIZATION_OPENROUTER_PROVIDER_ID,
@@ -4136,7 +4876,10 @@ export function withOrganizationOpenRouterCatalogProvider(
4136
4876
  export function withOrganizationOpenRouterCredential(
4137
4877
  settings: Settings,
4138
4878
  apiKey: string,
4139
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4879
+ customModels: readonly {
4880
+ upstreamModelId: string;
4881
+ label?: string | null;
4882
+ }[] = [],
4140
4883
  ): Settings {
4141
4884
  if (!apiKey.trim()) throw new Error("organization OpenRouter credential is empty");
4142
4885
  const catalog = withOrganizationOpenRouterCatalogProvider(settings, customModels);
@@ -4189,8 +4932,8 @@ export function productShortLabelForModelId(modelId: string): string | null {
4189
4932
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4190
4933
  : modelId;
4191
4934
  switch (slug) {
4192
- case "grok-4.6":
4193
- return "4.6";
4935
+ case "grok-4.7":
4936
+ return "4.7";
4194
4937
  case "gpt-5.6-sol":
4195
4938
  return "5.6 Sol";
4196
4939
  case "gpt-5.6-terra":
@@ -4199,6 +4942,10 @@ export function productShortLabelForModelId(modelId: string): string | null {
4199
4942
  return "5.6 Luna";
4200
4943
  case "gpt-6-astra":
4201
4944
  return "6 Astra";
4945
+ case "gpt-6-sol":
4946
+ return "6 Sol";
4947
+ case "gpt-6-luna":
4948
+ return "6 Luna";
4202
4949
  default:
4203
4950
  return null;
4204
4951
  }
@@ -4236,8 +4983,9 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4236
4983
  }> {
4237
4984
  if (
4238
4985
  isBuiltinGpt56ModelId(modelId) ||
4986
+ /^gpt-6(?:\.\d+)?-/u.test(modelId) ||
4239
4987
  modelId.startsWith("codex/gpt-5.6-") ||
4240
- modelId === "codex/gpt-6-astra"
4988
+ /^codex\/gpt-6(?:\.\d+)?-/u.test(modelId)
4241
4989
  ) {
4242
4990
  return [
4243
4991
  { id: "standard", upstream: "supported", runnable: true },
@@ -4252,13 +5000,38 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4252
5000
  return [{ id: "standard", upstream: "unknown", runnable: true }];
4253
5001
  }
4254
5002
 
5003
+ function isReviewedGptVisionModel(slug: string): boolean {
5004
+ return slug.startsWith("gpt-5.6-") || /^gpt-6(?:\.\d+)?-/u.test(slug);
5005
+ }
5006
+
5007
+ /** Repair missing reviewed capabilities in live/stored Codex definitions.
5008
+ * Explicit latency restrictions and every unrelated catalog field remain authoritative.
5009
+ */
5010
+ function reviewedCodexCatalogCapabilities(
5011
+ slug: string,
5012
+ capabilities: ModelCapabilitiesV1,
5013
+ ): ModelCapabilitiesV1 {
5014
+ if (!/^gpt-6(?:\.\d+)?-/u.test(slug)) return capabilities;
5015
+ const fast = builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`).find(
5016
+ (mode) => mode.id === "fast",
5017
+ );
5018
+ return normalizeCapabilities({
5019
+ ...capabilities,
5020
+ inputModalities: [...new Set([...capabilities.inputModalities, "image" as const])],
5021
+ latencyModes:
5022
+ fast && !capabilities.latencyModes.some((mode) => mode.id === "fast")
5023
+ ? [...capabilities.latencyModes, fast]
5024
+ : capabilities.latencyModes,
5025
+ });
5026
+ }
5027
+
4255
5028
  function builtinPromptCachingForModel(
4256
5029
  modelId: string,
4257
5030
  ): NonNullable<ModelCapabilitiesV1["promptCaching"]> | undefined {
4258
5031
  const slug = modelId.startsWith(CODEX_MODEL_ID_PREFIX)
4259
5032
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4260
5033
  : modelId;
4261
- return slug.startsWith("gpt-5.6-")
5034
+ return slug.startsWith("gpt-5.6-") || slug.startsWith("gpt-6-")
4262
5035
  ? { upstream: "supported", runnable: true, mode: "implicit" }
4263
5036
  : undefined;
4264
5037
  }
@@ -4268,7 +5041,7 @@ function builtinHostedImageGenerationForModel(settings: Settings, modelId: strin
4268
5041
  return (
4269
5042
  settings.openaiProvider === "openai" &&
4270
5043
  isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl) &&
4271
- isBuiltinGpt56ModelId(modelId)
5044
+ (isBuiltinGpt56ModelId(modelId) || modelId.startsWith("gpt-6-"))
4272
5045
  );
4273
5046
  }
4274
5047
 
@@ -4345,9 +5118,22 @@ function assertLatencyModeRunnable(
4345
5118
  ): void {
4346
5119
  const runnable = runnableLatencyModesForModel(settings, modelId);
4347
5120
  if (!runnable.includes(latencyMode)) {
4348
- throw new Error(
4349
- `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${runnable.join(", ")})`,
5121
+ throw new UnsupportedLatencyModeError(modelId, latencyMode, runnable);
5122
+ }
5123
+ }
5124
+
5125
+ export class UnsupportedLatencyModeError extends Error {
5126
+ readonly code = "UNSUPPORTED_LATENCY_MODE";
5127
+
5128
+ constructor(
5129
+ readonly modelId: string,
5130
+ readonly latencyMode: LatencyMode,
5131
+ readonly allowedLatencyModes: readonly LatencyMode[],
5132
+ ) {
5133
+ super(
5134
+ `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${allowedLatencyModes.join(", ")})`,
4350
5135
  );
5136
+ this.name = "UnsupportedLatencyModeError";
4351
5137
  }
4352
5138
  }
4353
5139
 
@@ -4359,10 +5145,17 @@ function registryCredentialSource(provider: InternalRegistryProvider): Credentia
4359
5145
  return { kind: "connected_subscription", provider: "codex" };
4360
5146
  case "xai-subscription":
4361
5147
  return { kind: "connected_subscription", provider: "xai" };
5148
+ case "claude-subscription-workspace":
5149
+ case "claude-subscription-organization":
5150
+ return { kind: "connected_subscription", provider: "claude" };
4362
5151
  case "vercel-gateway-workspace":
5152
+ case "direct-openai-workspace":
5153
+ case "direct-azure-workspace":
4363
5154
  case "openrouter-workspace":
5155
+ case "anthropic-workspace":
4364
5156
  return { kind: "workspace_connection", mechanism: "api_key" };
4365
5157
  case "vercel-gateway-organization":
5158
+ case "anthropic-organization":
4366
5159
  case "openrouter-organization":
4367
5160
  return { kind: "organization_connection", mechanism: "api_key" };
4368
5161
  case "api-key":
@@ -4383,11 +5176,17 @@ function registryBilling(provider: InternalRegistryProvider): BillingAttribution
4383
5176
  return { upstreamPayer: "deployment", metering: "external" };
4384
5177
  case "codex-subscription":
4385
5178
  case "xai-subscription":
5179
+ case "claude-subscription-workspace":
5180
+ case "claude-subscription-organization":
4386
5181
  return { upstreamPayer: "connected_subscription", metering: "external" };
4387
5182
  case "vercel-gateway-workspace":
5183
+ case "direct-openai-workspace":
5184
+ case "direct-azure-workspace":
4388
5185
  case "openrouter-workspace":
5186
+ case "anthropic-workspace":
4389
5187
  return { upstreamPayer: "workspace", metering: "external" };
4390
5188
  case "vercel-gateway-organization":
5189
+ case "anthropic-organization":
4391
5190
  case "openrouter-organization":
4392
5191
  return { upstreamPayer: "organization", metering: "external" };
4393
5192
  case "api-key":
@@ -4426,6 +5225,7 @@ function builtinCredentialSource(settings: Settings): CredentialSourceV1 {
4426
5225
  }
4427
5226
 
4428
5227
  function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
5228
+ anthropic?: ResolvedModelProvider["anthropic"];
4429
5229
  headers: Array<{
4430
5230
  name: string;
4431
5231
  classification: "public" | "secret";
@@ -4440,6 +5240,12 @@ function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
4440
5240
  const publicHeaders = new Set(provider.publicDefaultHeaderNames ?? []);
4441
5241
  const publicQuery = new Set(provider.publicDefaultQueryNames ?? []);
4442
5242
  return {
5243
+ ...(provider.anthropic
5244
+ ? {
5245
+ anthropic: (({ identity: _identity, credentialBinding: _binding, ...options }) =>
5246
+ options)(provider.anthropic),
5247
+ }
5248
+ : {}),
4443
5249
  headers: Object.entries(provider.defaultHeaders ?? {})
4444
5250
  .sort(([left], [right]) => left.localeCompare(right))
4445
5251
  .map(([name, value]) =>
@@ -4496,6 +5302,7 @@ function definitionVersionFor(
4496
5302
  baseUrl: provider.baseUrl ?? null,
4497
5303
  defaultHeaders: requestMetadata.headers,
4498
5304
  defaultQuery: requestMetadata.query,
5305
+ ...(requestMetadata.anthropic ? { anthropic: requestMetadata.anthropic } : {}),
4499
5306
  },
4500
5307
  credentialSource: model.credentialSource,
4501
5308
  billing: model.billing,
@@ -4525,6 +5332,75 @@ function legacyImplicitOpenAiDefinitionVersionFor(
4525
5332
  });
4526
5333
  }
4527
5334
 
5335
+ function legacyCodexAstraImplicitCachingDefinitionVersionFor(
5336
+ model: ConfiguredModel,
5337
+ provider: ResolvedModelProvider,
5338
+ ): string | null {
5339
+ // The GPT-6 rollout only made the already-implicit Codex cache discoverable.
5340
+ // Bound this compatibility to that product/transport and exact declaration;
5341
+ // never normalize explicit caching, another capability, or another model.
5342
+ if (
5343
+ model.id !== "codex/gpt-6-astra" ||
5344
+ model.upstreamModelId !== "gpt-6-astra" ||
5345
+ provider.id !== CODEX_PROVIDER_ID ||
5346
+ provider.kind !== "codex-subscription" ||
5347
+ provider.api !== "responses" ||
5348
+ provider.wireProfile !== "openai" ||
5349
+ canonicalJson(model.capabilities.promptCaching) !==
5350
+ canonicalJson({ upstream: "supported", runnable: true, mode: "implicit" })
5351
+ ) {
5352
+ return null;
5353
+ }
5354
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5355
+ const { promptCaching: _promptCaching, ...capabilities } = model.capabilities;
5356
+ // Recompute, rather than allowlisting an incident hash: all other current
5357
+ // fields must still reproduce the accepted digest. Do not compose this with
5358
+ // the older wire-profile compatibility or rewrite the accepted policy.
5359
+ return definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider);
5360
+ }
5361
+
5362
+ function matchesAdditiveCapabilityDefinitionVersion(
5363
+ model: ConfiguredModel,
5364
+ provider: ResolvedModelProvider,
5365
+ policy: TurnExecutionPolicyV1,
5366
+ ): boolean {
5367
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5368
+ const { latencyModes, inputModalities } = model.capabilities;
5369
+ // V1 has only three unique latency ids and three unique input modalities:
5370
+ // at most 4 * 7 subset digests retaining the frozen mode. Reconstruct an
5371
+ // exact historical declaration; never ignore the digest or alter existing
5372
+ // mode support, runnable state, billing multiplier, or request-tier routing.
5373
+ // Every other executable field remains in the digest. Do not compose this
5374
+ // with the pre-wire-profile or implicit-caching migration exceptions.
5375
+ for (let latencyMask = 1; latencyMask < 1 << latencyModes.length; latencyMask += 1) {
5376
+ const retainedModes = latencyModes.filter((_mode, index) => latencyMask & (1 << index));
5377
+ if (!retainedModes.some((mode) => mode.id === policy.latencyMode && mode.runnable)) {
5378
+ continue;
5379
+ }
5380
+ for (let inputMask = 1; inputMask < 1 << inputModalities.length; inputMask += 1) {
5381
+ const retainedInputs = inputModalities.filter((_modality, index) => inputMask & (1 << index));
5382
+ if (
5383
+ retainedModes.length === latencyModes.length &&
5384
+ retainedInputs.length === inputModalities.length
5385
+ ) {
5386
+ continue;
5387
+ }
5388
+ const capabilities = {
5389
+ ...model.capabilities,
5390
+ latencyModes: retainedModes,
5391
+ inputModalities: retainedInputs,
5392
+ };
5393
+ if (
5394
+ policy.definitionVersion ===
5395
+ definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider)
5396
+ ) {
5397
+ return true;
5398
+ }
5399
+ }
5400
+ }
5401
+ return false;
5402
+ }
5403
+
4528
5404
  /**
4529
5405
  * The built-in provider's stable id: "openai" on the OpenAI platform, "azure"
4530
5406
  * on Azure. Exported because the workspace model-policy gate must attribute
@@ -4566,12 +5442,13 @@ export function configuredProviders(
4566
5442
  if (settings.openaiProvider === "azure") {
4567
5443
  const baseUrl = settings.azureOpenaiBaseUrl ?? settings.azureOpenaiEndpoint;
4568
5444
  builtin.baseUrl = baseUrl ? normalizeRegistryBaseUrl(baseUrl, builtin.id) : undefined;
4569
- builtin.apiKey = settings.azureOpenaiApiKey ?? settings.azureOpenaiAdToken;
5445
+ builtin.apiKey =
5446
+ usableDeploymentSecret(settings.azureOpenaiApiKey) ?? settings.azureOpenaiAdToken;
4570
5447
  } else {
4571
5448
  builtin.baseUrl = settings.openaiBaseUrl
4572
5449
  ? normalizeRegistryBaseUrl(settings.openaiBaseUrl, builtin.id)
4573
5450
  : undefined;
4574
- builtin.apiKey = settings.openaiApiKey;
5451
+ builtin.apiKey = usableDeploymentSecret(settings.openaiApiKey);
4575
5452
  }
4576
5453
  const registry = configuredRegistryProviders(settings).map(
4577
5454
  (provider): ResolvedModelProvider => ({
@@ -4579,6 +5456,7 @@ export function configuredProviders(
4579
5456
  label: provider.label ?? provider.id,
4580
5457
  kind: provider.kind,
4581
5458
  api: provider.api,
5459
+ anthropic: provider.anthropic,
4582
5460
  wireProfile: provider.wireProfile,
4583
5461
  builtin: false,
4584
5462
  baseUrl: provider.baseUrl,
@@ -4600,11 +5478,30 @@ export function configuredProviders(
4600
5478
  * select, lease, refresh, or expose a concrete credential; those runtime
4601
5479
  * operations remain owned by the credential allocator.
4602
5480
  */
5481
+ /** Exact upstream slugs of the active Codex catalog (or the built-in fallback list). */
5482
+ export function codexUpstreamModelSlugs(settings: Settings): string[] {
5483
+ if (settings.resolvedCodexModelsJson === undefined) return [...CODEX_FALLBACK_MODEL_SLUGS];
5484
+ return z
5485
+ .array(CodexCatalogModelSchema)
5486
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
5487
+ .filter((model) => !model.retired)
5488
+ .map((model) => model.upstreamModelId);
5489
+ }
5490
+
4603
5491
  export function withCodexCatalogProvider(settings: Settings): Settings {
4604
5492
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
4605
5493
  if (providers.some((provider) => provider.id === CODEX_PROVIDER_ID)) {
4606
5494
  return settings;
4607
5495
  }
5496
+ const catalogModels =
5497
+ settings.resolvedCodexModelsJson === undefined
5498
+ ? undefined
5499
+ : z
5500
+ .array(CodexCatalogModelSchema)
5501
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
5502
+ .filter((model) => !model.retired)
5503
+ .map(({ retired: _retired, ...model }) => model);
5504
+ if (catalogModels?.length === 0) return settings;
4608
5505
  const provider: RegistryProvider = {
4609
5506
  kind: "codex-subscription",
4610
5507
  id: CODEX_PROVIDER_ID,
@@ -4612,40 +5509,42 @@ export function withCodexCatalogProvider(settings: Settings): Settings {
4612
5509
  api: "responses",
4613
5510
  wireProfile: "openai",
4614
5511
  baseUrl: CODEX_PROVIDER_BASE_URL,
4615
- models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
4616
- const capabilities = {
4617
- ...legacyModelCapabilities(settings, {
5512
+ models:
5513
+ catalogModels ??
5514
+ CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
5515
+ const capabilities = {
5516
+ ...legacyModelCapabilities(settings, {
5517
+ reasoningEffort: true,
5518
+ hostedWebSearch: true,
5519
+ vision: isReviewedGptVisionModel(slug),
5520
+ }),
5521
+ ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
5522
+ ? {
5523
+ promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
5524
+ }
5525
+ : {}),
5526
+ latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
5527
+ };
5528
+ return {
5529
+ id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
5530
+ upstreamModelId: slug,
5531
+ label: productLabelForModelId(slug),
5532
+ ...(productShortLabelForModelId(slug)
5533
+ ? { shortLabel: productShortLabelForModelId(slug)! }
5534
+ : {}),
4618
5535
  reasoningEffort: true,
5536
+ // The ChatGPT/Codex Responses backend accepts the native web_search
5537
+ // hosted tool (unlike hosted apply_patch/computer transports). Declaring
5538
+ // this here makes provider resolution truthful; the worker still applies
5539
+ // the durable session/turn policy gate before attaching it.
4619
5540
  hostedWebSearch: true,
4620
- vision: slug.startsWith("gpt-5.6-") || slug === "gpt-6-astra",
4621
- }),
4622
- ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
4623
- ? {
4624
- promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
4625
- }
4626
- : {}),
4627
- latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
4628
- };
4629
- return {
4630
- id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
4631
- upstreamModelId: slug,
4632
- label: productLabelForModelId(slug),
4633
- ...(productShortLabelForModelId(slug)
4634
- ? { shortLabel: productShortLabelForModelId(slug)! }
4635
- : {}),
4636
- reasoningEffort: true,
4637
- // The ChatGPT/Codex Responses backend accepts the native web_search
4638
- // hosted tool (unlike hosted apply_patch/computer transports). Declaring
4639
- // this here makes provider resolution truthful; the worker still applies
4640
- // the durable session/turn policy gate before attaching it.
4641
- hostedWebSearch: true,
4642
- capabilities,
4643
- contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
4644
- effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
4645
- autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
4646
- toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
4647
- };
4648
- }),
5541
+ capabilities,
5542
+ contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
5543
+ effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
5544
+ autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
5545
+ toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
5546
+ };
5547
+ }),
4649
5548
  };
4650
5549
  return {
4651
5550
  ...settings,
@@ -4884,7 +5783,7 @@ export function configuredModels(
4884
5783
  reasoningEffort: true,
4885
5784
  hostedWebSearch: settings.webSearchEnabled,
4886
5785
  hostedImageGeneration: builtinHostedImageGenerationForModel(settings, id),
4887
- vision: id.startsWith("gpt-5.6-"),
5786
+ vision: isReviewedGptVisionModel(id),
4888
5787
  }),
4889
5788
  ...(builtinPromptCachingForModel(id)
4890
5789
  ? { promptCaching: builtinPromptCachingForModel(id)! }
@@ -4995,7 +5894,78 @@ export function canonicalizeConfiguredModelId(settings: Settings, modelId: strin
4995
5894
  * openai allow-list, then registry ids.
4996
5895
  */
4997
5896
  export function configuredAllowedModels(settings: Settings): string[] {
4998
- return configuredModels(settings).map((model) => model.id);
5897
+ return configuredModels(settings)
5898
+ .filter((model) => isModelAvailableForNewSelection(settings, model.id))
5899
+ .map((model) => model.id);
5900
+ }
5901
+
5902
+ /** Retirement is selection metadata, never provider authority or executable identity. */
5903
+ export function isModelAvailableForNewSelection(settings: Settings, modelId: string): boolean {
5904
+ if (!modelId.startsWith(CODEX_MODEL_ID_PREFIX) || settings.resolvedCodexModelsJson === undefined)
5905
+ return true;
5906
+ const models = z
5907
+ .array(CodexCatalogModelSchema)
5908
+ .parse(JSON.parse(settings.resolvedCodexModelsJson));
5909
+ return models.some((model) => model.id === modelId && !model.retired);
5910
+ }
5911
+
5912
+ /** Called only with the durable policy installed for a claimed accepted attempt. */
5913
+ export function settingsForAcceptedSubscriptionTurn(
5914
+ settings: Settings,
5915
+ policy: TurnExecutionPolicyV1,
5916
+ expected: {
5917
+ modelId: string;
5918
+ reasoningEffort: Settings["openaiReasoningEffort"];
5919
+ latencyMode?: LatencyMode;
5920
+ },
5921
+ ): Settings {
5922
+ const parsed = TurnExecutionPolicyV1.parse(policy);
5923
+ if (!parsed.productModelId.startsWith(CODEX_MODEL_ID_PREFIX)) return settings;
5924
+ if (!settings.codexSubscriptionEnabled) throw new Error("Codex subscription is disabled");
5925
+ const models =
5926
+ settings.resolvedCodexModelsJson === undefined
5927
+ ? []
5928
+ : z.array(CodexCatalogModelSchema).parse(JSON.parse(settings.resolvedCodexModelsJson));
5929
+ const retained = models.find((model) => model.id === parsed.productModelId && model.retired);
5930
+ if (!retained) {
5931
+ assertTurnExecutionPolicyMatchesConfigV1(settings, parsed, expected);
5932
+ return settings;
5933
+ }
5934
+ const { retired: _retired, ...model } = retained;
5935
+ // Use the existing fixed broker transport. Only this exact definition is added;
5936
+ // selection metadata remains retired in the original catalog JSON.
5937
+ const withoutCodex = {
5938
+ ...settings,
5939
+ modelProvidersJson: JSON.stringify(
5940
+ parseModelProvidersJson(settings.modelProvidersJson).filter(
5941
+ (provider) => provider.id !== CODEX_PROVIDER_ID,
5942
+ ),
5943
+ ),
5944
+ };
5945
+ const providerSettings = withCodexCatalogProvider({
5946
+ ...withoutCodex,
5947
+ resolvedCodexModelsJson: JSON.stringify([model]),
5948
+ });
5949
+ const retainedProvider = parseModelProvidersJson(providerSettings.modelProvidersJson).find(
5950
+ (provider) => provider.id === CODEX_PROVIDER_ID,
5951
+ )!;
5952
+ const providers = parseModelProvidersJson(settings.modelProvidersJson);
5953
+ const current = providers.find((provider) => provider.id === CODEX_PROVIDER_ID);
5954
+ const restored = {
5955
+ ...settings,
5956
+ modelProvidersJson: JSON.stringify([
5957
+ ...providers.filter((provider) => provider.id !== CODEX_PROVIDER_ID),
5958
+ {
5959
+ ...retainedProvider,
5960
+ models: [
5961
+ ...(current?.models ?? []).filter((candidate) => candidate.id !== model.id),
5962
+ model,
5963
+ ],
5964
+ },
5965
+ ]),
5966
+ };
5967
+ assertTurnExecutionPolicyMatchesConfigV1(restored, parsed, expected);
5968
+ return restored;
4999
5969
  }
5000
5970
 
5001
5971
  /**
@@ -5089,6 +6059,18 @@ export function resolveModelProviderForTurn(
5089
6059
  );
5090
6060
  }
5091
6061
 
6062
+ /**
6063
+ * The requested model cannot start new work: it is retired from new selection
6064
+ * or absent from the configured catalog. Callers that own a durable refusal
6065
+ * (for example a scheduled occurrence) record it instead of failing blindly.
6066
+ */
6067
+ export class TurnExecutionPolicyModelUnavailableError extends Error {
6068
+ constructor(message: string) {
6069
+ super(message);
6070
+ this.name = "TurnExecutionPolicyModelUnavailableError";
6071
+ }
6072
+ }
6073
+
5092
6074
  /**
5093
6075
  * Build a trusted, secret-safe execution policy from the normalized catalog.
5094
6076
  * The Codex overlay here contains static product/provider identity only; it
@@ -5100,9 +6082,16 @@ export function resolveTurnExecutionPolicyV1(
5100
6082
  ): TurnExecutionPolicyV1 {
5101
6083
  const catalogSettings = settingsForTurnExecutionPolicy(settings, input.modelId);
5102
6084
  const productModelId = canonicalizeConfiguredModelId(catalogSettings, input.modelId);
6085
+ if (!isModelAvailableForNewSelection(catalogSettings, productModelId)) {
6086
+ throw new TurnExecutionPolicyModelUnavailableError(
6087
+ "Turn execution policy model is retired from new selection",
6088
+ );
6089
+ }
5103
6090
  const resolved = resolveModelProvider(catalogSettings, productModelId);
5104
6091
  if (!resolved) {
5105
- throw new Error("Turn execution policy model is not present in the configured catalog");
6092
+ throw new TurnExecutionPolicyModelUnavailableError(
6093
+ "Turn execution policy model is not present in the configured catalog",
6094
+ );
5106
6095
  }
5107
6096
  if (
5108
6097
  input.requestedModelId !== null &&
@@ -5131,10 +6120,20 @@ export function resolveTurnExecutionPolicyV1(
5131
6120
  });
5132
6121
  }
5133
6122
 
6123
+ /** Explicit accepted execution identity matches, but its definition digest differs. */
6124
+ export class TurnExecutionPolicyDefinitionMismatchError extends Error {
6125
+ readonly code = "turn_execution_policy_definition_mismatch";
6126
+
6127
+ constructor() {
6128
+ super("Turn execution policy does not match the current provider definition");
6129
+ this.name = "TurnExecutionPolicyDefinitionMismatchError";
6130
+ }
6131
+ }
6132
+
5134
6133
  /**
5135
6134
  * Parse-time validation lives in @opengeni/contracts; this verifier binds a
5136
6135
  * present snapshot to the current executable definition and exact turn row.
5137
- * Any deployment/provider drift fails before a provider or compaction call.
6136
+ * Non-additive executable drift fails before a provider or compaction call.
5138
6137
  */
5139
6138
  export function assertTurnExecutionPolicyMatchesConfigV1(
5140
6139
  settings: Settings,
@@ -5174,6 +6173,39 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5174
6173
  if (!resolved) {
5175
6174
  throw new Error("Turn execution policy model is no longer configured");
5176
6175
  }
6176
+ // The individual-account cutover corrects subscription accounting labels.
6177
+ // Preserve an already accepted legacy policy only if its complete original
6178
+ // executable digest matches; this exception cannot compose with other drift.
6179
+ const legacyClaudeScope =
6180
+ resolved.provider.kind === "claude-subscription-workspace"
6181
+ ? "workspace"
6182
+ : resolved.provider.kind === "claude-subscription-organization"
6183
+ ? "organization"
6184
+ : null;
6185
+ const legacyClaudeAccountingMatches =
6186
+ legacyClaudeScope !== null &&
6187
+ parsed.providerId === resolved.provider.id &&
6188
+ parsed.upstreamModelId === resolved.model.upstreamModelId &&
6189
+ parsed.wireApi === resolved.model.api &&
6190
+ canonicalJson(parsed.credentialSource) ===
6191
+ canonicalJson({
6192
+ kind: `${legacyClaudeScope}_connection`,
6193
+ mechanism: "api_key",
6194
+ }) &&
6195
+ canonicalJson(parsed.billing) ===
6196
+ canonicalJson({
6197
+ upstreamPayer: legacyClaudeScope,
6198
+ metering: "external",
6199
+ }) &&
6200
+ parsed.definitionVersion ===
6201
+ definitionVersionFor(
6202
+ {
6203
+ ...resolved.model,
6204
+ credentialSource: parsed.credentialSource,
6205
+ billing: parsed.billing,
6206
+ },
6207
+ resolved.provider,
6208
+ );
5177
6209
  // wireProfile was added to the definition digest after policies already
5178
6210
  // existed in durable in-flight turns. An omitted profile meant exactly
5179
6211
  // "openai", so accept that one legacy digest only; Azure and every other
@@ -5184,17 +6216,24 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5184
6216
  );
5185
6217
  const definitionVersionMatches =
5186
6218
  parsed.definitionVersion === resolved.model.definitionVersion ||
5187
- parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion;
5188
- const mismatched =
6219
+ parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion ||
6220
+ parsed.definitionVersion ===
6221
+ legacyCodexAstraImplicitCachingDefinitionVersionFor(resolved.model, resolved.provider) ||
6222
+ matchesAdditiveCapabilityDefinitionVersion(resolved.model, resolved.provider, parsed);
6223
+ const identityMismatched =
5189
6224
  parsed.providerId !== resolved.provider.id ||
5190
6225
  parsed.upstreamModelId !== resolved.model.upstreamModelId ||
5191
6226
  parsed.wireApi !== resolved.model.api ||
5192
- !definitionVersionMatches ||
5193
6227
  canonicalJson(parsed.credentialSource) !== canonicalJson(resolved.model.credentialSource) ||
5194
6228
  canonicalJson(parsed.billing) !== canonicalJson(resolved.model.billing);
5195
- if (mismatched) {
6229
+ // Identity/source changes must never enter a rollout-retry classification,
6230
+ // even when their definition digest also differs.
6231
+ if (identityMismatched && !legacyClaudeAccountingMatches) {
5196
6232
  throw new Error("Turn execution policy does not match the current provider definition");
5197
6233
  }
6234
+ if (!definitionVersionMatches && !legacyClaudeAccountingMatches) {
6235
+ throw new TurnExecutionPolicyDefinitionMismatchError();
6236
+ }
5198
6237
  return { policy: parsed, provider: resolved.provider, model: resolved.model };
5199
6238
  }
5200
6239
 
@@ -5234,6 +6273,154 @@ export function configuredModelPricingSchedules(
5234
6273
  };
5235
6274
  }
5236
6275
 
6276
+ /**
6277
+ * Insights list-price metadata, including reviewed namespaced provider routes.
6278
+ * Kept separate from debit/catalog pricing: adding a comparison must not change
6279
+ * an accepted turn's frozen execution-definition hash or payer classification.
6280
+ * Inline registry and explicit product-ID prices always win over projections.
6281
+ */
6282
+ export function configuredModelListPricingSchedules(
6283
+ settings: Settings,
6284
+ ): Record<string, ModelPricingScheduleV1> {
6285
+ const prices = configuredModelPricingSchedules(settings);
6286
+ const configured = Object.fromEntries(
6287
+ Object.entries(parseModelPricingJson(settings.modelPricingJson)).map(([model, pricing]) => [
6288
+ model,
6289
+ normalizeModelPricingSchedule(pricing),
6290
+ ]),
6291
+ );
6292
+ // Bare new IDs need the actual configured public OpenAI route, not merely a
6293
+ // familiar model name. Azure/custom endpoints do not inherit these API rates.
6294
+ if (
6295
+ settings.openaiProvider === "openai" &&
6296
+ isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl ?? "https://api.openai.com/v1")
6297
+ ) {
6298
+ for (const model of configuredModels(settings)) {
6299
+ if (
6300
+ model.providerId !== builtinProviderId(settings) ||
6301
+ !model.upstreamModelId.startsWith("gpt-") ||
6302
+ prices[model.id] !== undefined
6303
+ )
6304
+ continue;
6305
+ const reviewed = reviewedModelListPricing[model.upstreamModelId];
6306
+ if (reviewed) prices[model.id] = reviewed;
6307
+ }
6308
+ }
6309
+ for (const provider of configuredRegistryProviders(settings)) {
6310
+ for (const model of provider.models) {
6311
+ if (prices[model.id] !== undefined) continue;
6312
+ const reviewed = reviewedProviderModelPricing(settings, provider, model, configured);
6313
+ if (reviewed) prices[model.id] = reviewed;
6314
+ }
6315
+ }
6316
+ return prices;
6317
+ }
6318
+
6319
+ /**
6320
+ * A reviewed upstream list rate may also describe a namespaced product route.
6321
+ * Never infer a rate by stripping arbitrary prefixes, matching labels, or
6322
+ * treating every OpenAI-compatible endpoint as the original provider.
6323
+ * This is pricing metadata only: credential selection and payer/metering are
6324
+ * still derived exclusively from the accepted execution policy.
6325
+ */
6326
+ function reviewedProviderModelPricing(
6327
+ settings: Settings,
6328
+ provider: InternalRegistryProvider,
6329
+ model: RegistryProvider["models"][number],
6330
+ configured: Record<string, ModelPricingScheduleV1>,
6331
+ ): ModelPricingScheduleV1 | undefined {
6332
+ const upstream = model.upstreamModelId ?? model.id;
6333
+ let priceId: string | undefined;
6334
+ let nativeClaude = false;
6335
+ switch (provider.kind) {
6336
+ case "anonymous":
6337
+ if (
6338
+ provider.baseUrl.replace(/\/$/u, "") === OPENROUTER_BASE_URL &&
6339
+ upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)
6340
+ ) {
6341
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6342
+ }
6343
+ break;
6344
+ case "codex-subscription":
6345
+ if (upstream.startsWith("gpt-")) priceId = upstream;
6346
+ break;
6347
+ case "xai-subscription":
6348
+ if (upstream.startsWith("grok-")) priceId = upstream;
6349
+ break;
6350
+ case "anthropic-workspace":
6351
+ case "anthropic-organization":
6352
+ case "claude-subscription-workspace":
6353
+ case "claude-subscription-organization":
6354
+ if (claudeNativeModelProfile(upstream)) {
6355
+ priceId = upstream;
6356
+ nativeClaude = true;
6357
+ }
6358
+ break;
6359
+ case "direct-openai-workspace":
6360
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6361
+ priceId = upstream;
6362
+ }
6363
+ break;
6364
+ case "vercel-gateway-managed":
6365
+ case "vercel-gateway-workspace":
6366
+ case "vercel-gateway-organization":
6367
+ // Only curated, provider-pinned routes use the conservative fallback.
6368
+ // Arbitrary Gateway custom models still require exact reported cost or
6369
+ // an explicit operator rate, even if their slugs resemble a known model.
6370
+ priceId = configuredGatewayCatalogModels(settings).find(
6371
+ (candidate) =>
6372
+ candidate.upstreamModelId === upstream &&
6373
+ Object.values(OPENGENI_GATEWAY_MODELS).some(
6374
+ (reviewed) => reviewed.upstreamModelId === candidate.upstreamModelId,
6375
+ ),
6376
+ )?.productId;
6377
+ break;
6378
+ case "openrouter-workspace":
6379
+ case "openrouter-organization":
6380
+ case "openrouter-managed":
6381
+ if (upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)) {
6382
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6383
+ }
6384
+ break;
6385
+ case "api-key":
6386
+ // Explicit deployments of the official APIs may use their own product
6387
+ // IDs. Custom gateways/proxies and Azure SKUs do not inherit API rates.
6388
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6389
+ priceId = upstream;
6390
+ } else if (provider.baseUrl.replace(/\/$/u, "") === "https://api.x.ai/v1") {
6391
+ if (upstream.startsWith("grok-")) priceId = upstream;
6392
+ } else if (
6393
+ provider.api === "anthropic-messages" &&
6394
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1" &&
6395
+ claudeNativeModelProfile(upstream)
6396
+ ) {
6397
+ priceId = upstream;
6398
+ nativeClaude = true;
6399
+ }
6400
+ break;
6401
+ default:
6402
+ return undefined;
6403
+ }
6404
+ if (!priceId) return undefined;
6405
+ // Explicit upstream overrides retain the existing Codex comparison behavior;
6406
+ // a product-ID override or inline registry rate still has higher precedence.
6407
+ if (configured[priceId]) return configured[priceId];
6408
+ const schedule =
6409
+ reviewedModelListPricing[priceId] ??
6410
+ defaultModelPricing[priceId] ??
6411
+ (priceId === DEFAULT_OPENROUTER_MODEL_ID ? reviewedFreeOpenRouterListPricing : undefined);
6412
+ if (!schedule) return undefined;
6413
+ if (!nativeClaude || provider.anthropic?.cacheTtl !== "1h") return schedule;
6414
+ // Anthropic's 1-hour cache writes are 2x base input, rather than 5m's 1.25x.
6415
+ return {
6416
+ ...schedule,
6417
+ default: {
6418
+ ...schedule.default,
6419
+ cacheWriteMicrosPerMillionTokens: schedule.default.inputMicrosPerMillionTokens * 2,
6420
+ },
6421
+ };
6422
+ }
6423
+
5237
6424
  /** Legacy flat projection: returns the default/below-threshold price. */
5238
6425
  export function configuredModelPricing(settings: Settings): Record<string, ModelPricing> {
5239
6426
  return Object.fromEntries(
@@ -5350,59 +6537,367 @@ export function calculateModelUsageCostBreakdown(
5350
6537
  options?: { latencyMode?: LatencyMode },
5351
6538
  ): ModelUsageCostBreakdown {
5352
6539
  const schedule = configuredModelPricingSchedules(settings)[model];
5353
- if (!schedule) {
5354
- throw new Error(`Missing model pricing for ${model}`);
5355
- }
5356
- const entries =
5357
- usage.requestUsageEntries && usage.requestUsageEntries.length > 0
5358
- ? usage.requestUsageEntries
5359
- : [usage];
6540
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6541
+ }
6542
+
6543
+ /** Provider-list/equivalent-credit comparison only; never debit authority. */
6544
+ export function calculateModelListUsageCostBreakdown(
6545
+ settings: Settings,
6546
+ model: string,
6547
+ usage: ModelUsageInput,
6548
+ options?: { latencyMode?: LatencyMode },
6549
+ ): ModelUsageCostBreakdown {
6550
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6551
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6552
+ }
6553
+
6554
+ /**
6555
+ * Forward-only list comparison snapshot for the model-call fact writer.
6556
+ * Callers must establish request price provenance (including geography/service
6557
+ * tier) and retain per-request counters. Unknown counters/TTL/modifiers yield
6558
+ * no class split, not invented zeros. Historical facts must not use this helper.
6559
+ * Native Claude details preserve cache_write_tokens_5m and cache_write_tokens_1h.
6560
+ */
6561
+ export function calculateModelListUsageCostSnapshot(
6562
+ settings: Settings,
6563
+ model: string,
6564
+ usage: ModelUsageInput,
6565
+ options?: { latencyMode?: LatencyMode; priceContextKnown?: boolean },
6566
+ ): ModelListUsageCostSnapshot {
6567
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6568
+ const fallback = calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6569
+ const unknown = { ...fallback, listByClassMicros: null, listByClassApprox: false };
6570
+ if (!schedule || !options?.priceContextKnown) return unknown;
6571
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
6572
+ const classes: ModelListCostClassesMicros = {
6573
+ uncachedInput: 0,
6574
+ cacheRead: 0,
6575
+ cacheWrite: 0,
6576
+ output: 0,
6577
+ };
5360
6578
  const rawCostByPricing = new Map<ModelPricing, number>();
6579
+ const native = nativeClaudeListWriteContext(settings, model);
5361
6580
  for (const entry of entries) {
5362
- const pricing = selectModelPricing(schedule, positiveInt(entry.inputTokens));
5363
- rawCostByPricing.set(
5364
- pricing,
5365
- (rawCostByPricing.get(pricing) ?? 0) + calculateEntryCostMicros(pricing, entry),
5366
- );
6581
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
6582
+ return unknown;
6583
+ const cached = knownDetailTokenCounter(entry, [
6584
+ "cached_tokens",
6585
+ "cachedInputTokens",
6586
+ "cached_input_tokens",
6587
+ ]);
6588
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
6589
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
6590
+ return unknown;
6591
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
6592
+ if (cached > 0 && pricing.cachedInputMicrosPerMillionTokens === undefined) return unknown;
6593
+ if (writes > 0 && pricing.cacheWriteMicrosPerMillionTokens === undefined) return unknown;
6594
+ const costs: ModelListCostClassesMicros = {
6595
+ uncachedInput: Math.ceil(
6596
+ ((entry.inputTokens - cached - writes) * pricing.inputMicrosPerMillionTokens) / 1_000_000,
6597
+ ),
6598
+ cacheRead: Math.ceil((cached * (pricing.cachedInputMicrosPerMillionTokens ?? 0)) / 1_000_000),
6599
+ cacheWrite: Math.ceil((writes * (pricing.cacheWriteMicrosPerMillionTokens ?? 0)) / 1_000_000),
6600
+ output: Math.ceil((entry.outputTokens * pricing.outputMicrosPerMillionTokens) / 1_000_000),
6601
+ };
6602
+ if (native && writes > 0) {
6603
+ const fiveMinute = knownDetailTokenCounter(entry, ["cache_write_tokens_5m"]);
6604
+ const oneHour = knownDetailTokenCounter(entry, ["cache_write_tokens_1h"]);
6605
+ if (fiveMinute === undefined || oneHour === undefined || fiveMinute + oneHour !== writes)
6606
+ return unknown;
6607
+ if (native.publishedRates) {
6608
+ costs.cacheWrite =
6609
+ Math.ceil((fiveMinute * native.publishedRates.fiveMinute) / 1_000_000) +
6610
+ Math.ceil((oneHour * native.publishedRates.oneHour) / 1_000_000);
6611
+ } else if (
6612
+ !(
6613
+ (native.declaredTtl === "5m" && oneHour === 0) ||
6614
+ (native.declaredTtl === "1h" && fiveMinute === 0)
6615
+ )
6616
+ ) {
6617
+ // A single explicit override cannot establish two different TTL prices.
6618
+ return unknown;
6619
+ }
6620
+ }
6621
+ const raw = sumListCostClasses(costs);
6622
+ rawCostByPricing.set(pricing, (rawCostByPricing.get(pricing) ?? 0) + raw);
6623
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) classes[key] += costs[key];
5367
6624
  }
5368
- let providerCostMicros = 0;
6625
+ let providerCostMicros = sumListCostClasses(classes);
5369
6626
  let creditCostMicros = 0;
5370
- for (const [pricing, rawCost] of rawCostByPricing) {
5371
- const marginBps = pricing.marginBps ?? 0;
5372
- providerCostMicros += rawCost;
5373
- creditCostMicros += Math.ceil((rawCost * (10_000 + marginBps)) / 10_000);
6627
+ for (const [pricing, raw] of rawCostByPricing) {
6628
+ creditCostMicros += Math.ceil((raw * (10_000 + (pricing.marginBps ?? 0))) / 10_000);
5374
6629
  }
5375
- const latencyMode = options?.latencyMode ?? "standard";
6630
+ let listByClassApprox = false;
6631
+ const latencyMode = options.latencyMode ?? "standard";
5376
6632
  if (latencyMode !== "standard") {
5377
6633
  const catalogSettings = settingsForTurnExecutionPolicy(settings, model);
5378
6634
  const resolved = resolveModelProvider(
5379
6635
  catalogSettings,
5380
6636
  canonicalizeConfiguredModelId(catalogSettings, model),
5381
6637
  );
5382
- const multiplierBps = resolved?.model.capabilities.latencyModes.find(
6638
+ const multiplier = resolved?.model.capabilities.latencyModes.find(
5383
6639
  (mode) => mode.id === latencyMode && mode.runnable,
5384
6640
  )?.billingMultiplierBps;
5385
- if (multiplierBps && multiplierBps > 0) {
5386
- providerCostMicros = Math.ceil((providerCostMicros * multiplierBps) / 10_000);
5387
- creditCostMicros = Math.ceil((creditCostMicros * multiplierBps) / 10_000);
5388
- }
6641
+ if (multiplier === undefined || multiplier <= 0) return unknown;
6642
+ providerCostMicros = Math.ceil((providerCostMicros * multiplier) / 10_000);
6643
+ creditCostMicros = Math.ceil((creditCostMicros * multiplier) / 10_000);
6644
+ const scaled = allocateLatencyListCostClasses(classes, multiplier, providerCostMicros);
6645
+ if (!scaled) return unknown;
6646
+ Object.assign(classes, scaled.classes);
6647
+ listByClassApprox = scaled.approximate;
5389
6648
  }
5390
- return { providerCostMicros, creditCostMicros };
6649
+ if (![providerCostMicros, creditCostMicros, ...Object.values(classes)].every(knownTokenCounter))
6650
+ return unknown;
6651
+ return { providerCostMicros, creditCostMicros, listByClassMicros: classes, listByClassApprox };
5391
6652
  }
5392
6653
 
5393
6654
  /**
5394
- * Convert AI Gateway's exact USD inference cost to OpenGeni credit micros and
5395
- * apply the configured model margin. Decimal arithmetic is integer-only so a
5396
- * sub-micro provider charge cannot be lost to floating-point rounding.
6655
+ * Approximate class attribution of a previously recorded upstream list total.
6656
+ * Current reviewed rates are weights only: this never recomputes that total,
6657
+ * credits, or charges. Missing class counters/rates remain unknown. Historical
6658
+ * cache writes use the schedule's single TTL rate, not an exact TTL assertion.
6659
+ * Reasoning is already included in outputTokens and is never added again.
5397
6660
  */
5398
- export function calculateGatewayReportedCostMicros(
6661
+ export function allocateRecordedModelListCostByClass(
5399
6662
  settings: Settings,
5400
6663
  model: string,
5401
- inferenceCostUsd: string,
5402
- options?: { inputTokens?: number },
5403
- ): number {
5404
- return calculateGatewayReportedCostBreakdown(settings, model, inferenceCostUsd, options)
5405
- .creditCostMicros;
6664
+ usage: ModelUsageInput,
6665
+ recordedProviderCostMicros: number | null | undefined,
6666
+ ): ModelRecordedListCostAllocation {
6667
+ const unknown: ModelRecordedListCostAllocation = {
6668
+ listByClassMicros: null,
6669
+ listByClassApprox: false,
6670
+ };
6671
+ if (!knownTokenCounter(recordedProviderCostMicros)) return unknown;
6672
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6673
+ if (!schedule) return unknown;
6674
+ const weights = { uncachedInput: 0n, cacheRead: 0n, cacheWrite: 0n, output: 0n };
6675
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
6676
+ for (const entry of entries) {
6677
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
6678
+ return unknown;
6679
+ const cached = knownDetailTokenCounter(entry, [
6680
+ "cached_tokens",
6681
+ "cachedInputTokens",
6682
+ "cached_input_tokens",
6683
+ ]);
6684
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
6685
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
6686
+ return unknown;
6687
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
6688
+ const counters = {
6689
+ uncachedInput: entry.inputTokens - cached - writes,
6690
+ cacheRead: cached,
6691
+ cacheWrite: writes,
6692
+ output: entry.outputTokens,
6693
+ };
6694
+ const rates = {
6695
+ uncachedInput: pricing.inputMicrosPerMillionTokens,
6696
+ cacheRead: pricing.cachedInputMicrosPerMillionTokens,
6697
+ cacheWrite: pricing.cacheWriteMicrosPerMillionTokens,
6698
+ output: pricing.outputMicrosPerMillionTokens,
6699
+ };
6700
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) {
6701
+ if (counters[key] === 0) continue;
6702
+ const rate = rates[key];
6703
+ if (!knownTokenCounter(rate)) return unknown;
6704
+ // Do not round or convert weights to Number: both can distort ratios.
6705
+ weights[key] += BigInt(counters[key]) * BigInt(rate);
6706
+ }
6707
+ }
6708
+ const classes = allocateRecordedListCostWeights(weights, recordedProviderCostMicros);
6709
+ return classes ? { listByClassMicros: classes, listByClassApprox: true } : unknown;
6710
+ }
6711
+
6712
+ const MODEL_LIST_COST_CLASS_KEYS = ["uncachedInput", "cacheRead", "cacheWrite", "output"] as const;
6713
+
6714
+ function sumListCostClasses(costs: ModelListCostClassesMicros): number {
6715
+ return MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + costs[key], 0);
6716
+ }
6717
+
6718
+ /** Integer-only largest remainder, preserving the supplied historical total. */
6719
+ function allocateRecordedListCostWeights(
6720
+ weights: Record<(typeof MODEL_LIST_COST_CLASS_KEYS)[number], bigint>,
6721
+ target: number,
6722
+ ): ModelListCostClassesMicros | undefined {
6723
+ const denominator = MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + weights[key], 0n);
6724
+ if (denominator === 0n) {
6725
+ return target === 0 ? { uncachedInput: 0, cacheRead: 0, cacheWrite: 0, output: 0 } : undefined;
6726
+ }
6727
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
6728
+ const numerator = BigInt(target) * weights[key];
6729
+ return { key, index, value: numerator / denominator, remainder: numerator % denominator };
6730
+ });
6731
+ const remaining = BigInt(target) - rows.reduce((total, row) => total + row.value, 0n);
6732
+ const ranked = [...rows].sort((a, b) =>
6733
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
6734
+ );
6735
+ for (let index = 0; index < Number(remaining); index++) ranked[index]!.value += 1n;
6736
+ return Object.fromEntries(
6737
+ rows.map((row) => [row.key, Number(row.value)]),
6738
+ ) as ModelListCostClassesMicros;
6739
+ }
6740
+
6741
+ function knownTokenCounter(value: unknown): value is number {
6742
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
6743
+ }
6744
+
6745
+ function knownDetailTokenCounter(
6746
+ entry: ModelUsageInput,
6747
+ keys: readonly string[],
6748
+ firstOnly = false,
6749
+ ): number | undefined {
6750
+ const details = Array.isArray(entry.inputTokensDetails)
6751
+ ? entry.inputTokensDetails
6752
+ : entry.inputTokensDetails
6753
+ ? [entry.inputTokensDetails]
6754
+ : [];
6755
+ if (details.length === 0) return undefined;
6756
+ let total = 0;
6757
+ for (const detail of details) {
6758
+ const present = keys.filter((key) => detail[key] !== undefined);
6759
+ if (present.length === 0) return undefined;
6760
+ for (const key of firstOnly ? present.slice(0, 1) : present) {
6761
+ if (!knownTokenCounter(detail[key])) return undefined;
6762
+ total += detail[key]!;
6763
+ }
6764
+ }
6765
+ return knownTokenCounter(total) ? total : undefined;
6766
+ }
6767
+
6768
+ function nativeClaudeListWriteContext(
6769
+ settings: Settings,
6770
+ modelId: string,
6771
+ ):
6772
+ | {
6773
+ declaredTtl: "off" | "5m" | "1h" | undefined;
6774
+ publishedRates?: { fiveMinute: number; oneHour: number };
6775
+ }
6776
+ | undefined {
6777
+ for (const provider of configuredRegistryProviders(settings)) {
6778
+ const model = provider.models.find((candidate) => candidate.id === modelId);
6779
+ if (!model || provider.api !== "anthropic-messages") continue;
6780
+ const result = { declaredTtl: provider.anthropic?.cacheTtl };
6781
+ const upstream = model.upstreamModelId ?? model.id;
6782
+ const overrides = parseModelPricingJson(settings.modelPricingJson);
6783
+ const official =
6784
+ provider.kind === "anthropic-workspace" ||
6785
+ provider.kind === "anthropic-organization" ||
6786
+ provider.kind === "claude-subscription-workspace" ||
6787
+ provider.kind === "claude-subscription-organization" ||
6788
+ (provider.kind === "api-key" &&
6789
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1");
6790
+ const published = reviewedModelListPricing[upstream]?.default;
6791
+ if (
6792
+ !official ||
6793
+ !claudeNativeModelProfile(upstream) ||
6794
+ !published?.cacheWriteMicrosPerMillionTokens ||
6795
+ model.pricing ||
6796
+ overrides[modelId] !== undefined ||
6797
+ overrides[upstream] !== undefined
6798
+ )
6799
+ return result;
6800
+ return {
6801
+ ...result,
6802
+ publishedRates: {
6803
+ fiveMinute: published.cacheWriteMicrosPerMillionTokens,
6804
+ oneHour: published.inputMicrosPerMillionTokens * 2,
6805
+ },
6806
+ };
6807
+ }
6808
+ return undefined;
6809
+ }
6810
+
6811
+ /** Deterministic largest-remainder allocation; disclose any fractional scaling. */
6812
+ function allocateLatencyListCostClasses(
6813
+ classes: ModelListCostClassesMicros,
6814
+ multiplier: number,
6815
+ target: number,
6816
+ ): { classes: ModelListCostClassesMicros; approximate: boolean } | undefined {
6817
+ if (
6818
+ !knownTokenCounter(multiplier) ||
6819
+ !knownTokenCounter(target) ||
6820
+ !Object.values(classes).every(knownTokenCounter)
6821
+ )
6822
+ return undefined;
6823
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
6824
+ const numerator = BigInt(classes[key]) * BigInt(multiplier);
6825
+ return { key, index, value: Number(numerator / 10_000n), remainder: numerator % 10_000n };
6826
+ });
6827
+ const remaining = target - rows.reduce((total, row) => total + row.value, 0);
6828
+ if (remaining < 0 || remaining > rows.length) return undefined;
6829
+ const ranked = [...rows].sort((a, b) =>
6830
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
6831
+ );
6832
+ for (let index = 0; index < remaining; index++) ranked[index]!.value += 1;
6833
+ return {
6834
+ classes: Object.fromEntries(
6835
+ rows.map((row) => [row.key, row.value]),
6836
+ ) as ModelListCostClassesMicros,
6837
+ approximate: rows.some((row) => row.remainder !== 0n),
6838
+ };
6839
+ }
6840
+
6841
+ function calculateUsageCostBreakdown(
6842
+ settings: Settings,
6843
+ model: string,
6844
+ usage: ModelUsageInput,
6845
+ schedule: ModelPricingScheduleV1 | undefined,
6846
+ options?: { latencyMode?: LatencyMode },
6847
+ ): ModelUsageCostBreakdown {
6848
+ if (!schedule) {
6849
+ throw new Error(`Missing model pricing for ${model}`);
6850
+ }
6851
+ const entries =
6852
+ usage.requestUsageEntries && usage.requestUsageEntries.length > 0
6853
+ ? usage.requestUsageEntries
6854
+ : [usage];
6855
+ const rawCostByPricing = new Map<ModelPricing, number>();
6856
+ for (const entry of entries) {
6857
+ const pricing = selectModelPricing(schedule, positiveInt(entry.inputTokens));
6858
+ rawCostByPricing.set(
6859
+ pricing,
6860
+ (rawCostByPricing.get(pricing) ?? 0) + calculateEntryCostMicros(pricing, entry),
6861
+ );
6862
+ }
6863
+ let providerCostMicros = 0;
6864
+ let creditCostMicros = 0;
6865
+ for (const [pricing, rawCost] of rawCostByPricing) {
6866
+ const marginBps = pricing.marginBps ?? 0;
6867
+ providerCostMicros += rawCost;
6868
+ creditCostMicros += Math.ceil((rawCost * (10_000 + marginBps)) / 10_000);
6869
+ }
6870
+ const latencyMode = options?.latencyMode ?? "standard";
6871
+ if (latencyMode !== "standard") {
6872
+ const catalogSettings = settingsForTurnExecutionPolicy(settings, model);
6873
+ const resolved = resolveModelProvider(
6874
+ catalogSettings,
6875
+ canonicalizeConfiguredModelId(catalogSettings, model),
6876
+ );
6877
+ const multiplierBps = resolved?.model.capabilities.latencyModes.find(
6878
+ (mode) => mode.id === latencyMode && mode.runnable,
6879
+ )?.billingMultiplierBps;
6880
+ if (multiplierBps && multiplierBps > 0) {
6881
+ providerCostMicros = Math.ceil((providerCostMicros * multiplierBps) / 10_000);
6882
+ creditCostMicros = Math.ceil((creditCostMicros * multiplierBps) / 10_000);
6883
+ }
6884
+ }
6885
+ return { providerCostMicros, creditCostMicros };
6886
+ }
6887
+
6888
+ /**
6889
+ * Convert AI Gateway's exact USD inference cost to OpenGeni credit micros and
6890
+ * apply the configured model margin. Decimal arithmetic is integer-only so a
6891
+ * sub-micro provider charge cannot be lost to floating-point rounding.
6892
+ */
6893
+ export function calculateGatewayReportedCostMicros(
6894
+ settings: Settings,
6895
+ model: string,
6896
+ inferenceCostUsd: string,
6897
+ options?: { inputTokens?: number },
6898
+ ): number {
6899
+ return calculateGatewayReportedCostBreakdown(settings, model, inferenceCostUsd, options)
6900
+ .creditCostMicros;
5406
6901
  }
5407
6902
 
5408
6903
  type GatewayReportedCostDecimal = {
@@ -5796,6 +7291,7 @@ export function applyGitAuthPointerEnvironment(
5796
7291
  }
5797
7292
 
5798
7293
  export type StartupRetryOptions = {
7294
+ shouldRetry?: (error: unknown) => boolean;
5799
7295
  attempts?: number;
5800
7296
  initialDelayMs?: number;
5801
7297
  maxDelayMs?: number;
@@ -5810,7 +7306,7 @@ export type StartupRetryOptions = {
5810
7306
 
5811
7307
  export function startupRetryOptions(
5812
7308
  settings: Settings,
5813
- ): Required<Omit<StartupRetryOptions, "onRetry">> {
7309
+ ): Required<Omit<StartupRetryOptions, "onRetry" | "shouldRetry">> {
5814
7310
  return {
5815
7311
  attempts: settings.startupDependencyRetryAttempts,
5816
7312
  initialDelayMs: settings.startupDependencyRetryInitialDelayMs,
@@ -5830,7 +7326,7 @@ export async function retryStartupDependency<T>(
5830
7326
  try {
5831
7327
  return await operation();
5832
7328
  } catch (error) {
5833
- if (attempt >= attempts) {
7329
+ if (attempt >= attempts || options.shouldRetry?.(error) === false) {
5834
7330
  throw error;
5835
7331
  }
5836
7332
  const delayMs = Math.min(maxDelayMs, initialDelayMs * 2 ** (attempt - 1));
@@ -6230,20 +7726,9 @@ function ensureBuiltInMcpServers(settings: Settings): Settings["mcpServers"] {
6230
7726
  : [
6231
7727
  {
6232
7728
  id: "docs",
6233
- name: "Document Search",
7729
+ name: "Knowledge",
6234
7730
  url: firstPartyDocsMcpUrl,
6235
- allowedTools: [
6236
- "search_documents",
6237
- "fetch_document_chunk",
6238
- "list_document_bases",
6239
- "list_indexed_documents",
6240
- "knowledge_search",
6241
- "knowledge_get",
6242
- "knowledge_browse",
6243
- "knowledge_fetch",
6244
- "memory_search",
6245
- "memory_propose",
6246
- ],
7731
+ allowedTools: ["knowledge_search", "knowledge_get", "knowledge_browse"],
6247
7732
  cacheToolsList: false,
6248
7733
  },
6249
7734
  ]),
@@ -6353,8 +7838,99 @@ function isDigestPinnedModalDesktopImage(settings: Settings): boolean {
6353
7838
  );
6354
7839
  }
6355
7840
 
7841
+ export type TrustedProxyCidr = {
7842
+ address: string;
7843
+ prefix: number;
7844
+ family: "ipv4" | "ipv6";
7845
+ };
7846
+
7847
+ /**
7848
+ * Parse `OPENGENI_API_TRUSTED_PROXY_CIDRS`: comma-separated IPv4/IPv6 CIDRs or
7849
+ * single addresses. Throws on any malformed entry so a typo can never widen or
7850
+ * silently disable the trusted proxy set.
7851
+ */
7852
+ export function trustedProxyCidrEntries(raw: string): TrustedProxyCidr[] {
7853
+ const entries: TrustedProxyCidr[] = [];
7854
+ for (const entry of raw.split(",")) {
7855
+ const value = entry.trim();
7856
+ if (!value) continue;
7857
+ const slash = value.indexOf("/");
7858
+ const address = slash < 0 ? value : value.slice(0, slash);
7859
+ const version = isIP(address);
7860
+ const family = version === 4 ? "ipv4" : version === 6 ? "ipv6" : null;
7861
+ const maxPrefix = family === "ipv4" ? 32 : 128;
7862
+ const prefixText = slash < 0 ? String(maxPrefix) : value.slice(slash + 1);
7863
+ const prefix = /^\d{1,3}$/u.test(prefixText) ? Number(prefixText) : Number.NaN;
7864
+ if (!family || !Number.isInteger(prefix) || prefix < 0 || prefix > maxPrefix) {
7865
+ throw new Error(
7866
+ `OPENGENI_API_TRUSTED_PROXY_CIDRS entry "${value.slice(0, 64)}" is not an IP address or CIDR range`,
7867
+ );
7868
+ }
7869
+ entries.push({ address, prefix, family });
7870
+ }
7871
+ return entries;
7872
+ }
7873
+
7874
+ /**
7875
+ * Parse `OPENGENI_LOCAL_ALLOWED_ORIGINS`: comma-separated exact browser origins
7876
+ * (`http(s)://host[:port]`, no path, query, credentials, or wildcard). Throws on
7877
+ * any malformed entry so a typo can never widen or silently disable the local
7878
+ * browser boundary.
7879
+ */
7880
+ export function localAllowedOriginEntries(raw: string | undefined): string[] {
7881
+ const origins: string[] = [];
7882
+ for (const entry of (raw ?? "").split(",")) {
7883
+ const value = entry.trim();
7884
+ if (!value) continue;
7885
+ let url: URL | null = null;
7886
+ try {
7887
+ url = new URL(value);
7888
+ } catch {
7889
+ url = null;
7890
+ }
7891
+ if (
7892
+ !url ||
7893
+ (url.protocol !== "http:" && url.protocol !== "https:") ||
7894
+ url.username ||
7895
+ url.password ||
7896
+ url.hostname.includes("*") ||
7897
+ url.pathname !== "/" ||
7898
+ url.search ||
7899
+ url.hash ||
7900
+ value.endsWith("?") ||
7901
+ value.endsWith("#")
7902
+ ) {
7903
+ throw new Error(
7904
+ `OPENGENI_LOCAL_ALLOWED_ORIGINS entry "${value.slice(0, 64)}" is not an exact http(s) origin such as http://127.0.0.1:5173`,
7905
+ );
7906
+ }
7907
+ origins.push(url.origin);
7908
+ }
7909
+ return origins;
7910
+ }
7911
+
7912
+ const LOCAL_SANDBOX_API_ROUTES: readonly RegExp[] = [
7913
+ // Codemode calls, journal reads, catalog, and the Site/SDK proxy.
7914
+ /^\/v1\/workspaces\/[^/]+\/codemode(?:\/|$)/u,
7915
+ // First-party MCP, including its /docs and /files servers.
7916
+ /^\/v1\/workspaces\/[^/]+\/mcp(?:\/|$)/u,
7917
+ // The personal GitHub HTTPS smart-Git broker.
7918
+ /^\/v1\/git\/personal\/[A-Za-z0-9_-]{43}\/(?:info\/refs|git-upload-pack|git-receive-pack)$/u,
7919
+ ];
7920
+
7921
+ /**
7922
+ * Local development only: whether a normalized API path is one a sandbox calls
7923
+ * (Codemode, first-party MCP, and the personal Git broker). The Linux Docker
7924
+ * sandbox route relays only these, and the local API serves only these on the
7925
+ * addresses that only sandboxes use (see `apps/api/src/http/local-browser-boundary.ts`).
7926
+ */
7927
+ export function localSandboxApiRouteAllowed(pathname: string): boolean {
7928
+ return LOCAL_SANDBOX_API_ROUTES.some((route) => route.test(pathname));
7929
+ }
7930
+
6356
7931
  function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = process.env): void {
6357
7932
  temporalConnectionOptions(settings);
7933
+ localAllowedOriginEntries(settings.localAllowedOrigins);
6358
7934
  if (
6359
7935
  settings.organizationUserSetupEmailTokenTransport === "query" &&
6360
7936
  !settings.organizationUserSetupQueryEdgeSanitizationConfirmed
@@ -6448,6 +8024,21 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6448
8024
  );
6449
8025
  }
6450
8026
  }
8027
+ // The retired MCP-only name must never be ignored silently: a deployment
8028
+ // that trusted forwarded addresses would fall back to the transport peer.
8029
+ // "0" (the old .env.example value) means the same as the new default.
8030
+ const retiredTrustedProxyHops = source.OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS?.trim();
8031
+ if (retiredTrustedProxyHops && retiredTrustedProxyHops !== "0") {
8032
+ throw new Error(
8033
+ "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",
8034
+ );
8035
+ }
8036
+ const trustedProxyCidrs = trustedProxyCidrEntries(settings.apiTrustedProxyCidrs);
8037
+ if (trustedProxyCidrs.length > 0 && settings.apiTrustedProxyHops === 0) {
8038
+ throw new Error(
8039
+ "OPENGENI_API_TRUSTED_PROXY_CIDRS requires OPENGENI_API_TRUSTED_PROXY_HOPS greater than 0",
8040
+ );
8041
+ }
6451
8042
  if (settings.mcpOauthEnabled) {
6452
8043
  if (settings.productAccessMode === "configured") {
6453
8044
  throw new Error(
@@ -6748,105 +8339,7 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6748
8339
  );
6749
8340
  }
6750
8341
  }
6751
- if (
6752
- settings.objectStorageBackend === "s3-compatible" ||
6753
- settings.objectStorageBackend === "aws-s3"
6754
- ) {
6755
- if (
6756
- Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
6757
- ) {
6758
- throw new Error(
6759
- "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
6760
- );
6761
- }
6762
- if (
6763
- settings.objectStorageBackend === "s3-compatible" &&
6764
- (settings.objectStorageEndpoint ||
6765
- settings.objectStorageInternalEndpoint ||
6766
- settings.objectStorageSandboxEndpoint) &&
6767
- (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
6768
- ) {
6769
- throw new Error(
6770
- "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
6771
- );
6772
- }
6773
- if (
6774
- settings.objectStorageAzureConnectionString ||
6775
- settings.objectStorageAzureAccountName ||
6776
- settings.objectStorageAzureAccountKey ||
6777
- settings.objectStorageAzureEndpoint
6778
- ) {
6779
- throw new Error(
6780
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6781
- );
6782
- }
6783
- if (
6784
- settings.objectStorageGcsProjectId ||
6785
- settings.objectStorageGcsCredentialsJson ||
6786
- settings.objectStorageGcsKeyFilename ||
6787
- settings.objectStorageGcsApiEndpoint
6788
- ) {
6789
- throw new Error(
6790
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6791
- );
6792
- }
6793
- } else if (settings.objectStorageBackend === "azure-blob") {
6794
- if (
6795
- settings.objectStorageEndpoint ||
6796
- settings.objectStorageInternalEndpoint ||
6797
- settings.objectStorageSandboxEndpoint ||
6798
- settings.objectStorageAccessKeyId ||
6799
- settings.objectStorageSecretAccessKey
6800
- ) {
6801
- throw new Error(
6802
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
6803
- );
6804
- }
6805
- if (
6806
- settings.objectStorageGcsProjectId ||
6807
- settings.objectStorageGcsCredentialsJson ||
6808
- settings.objectStorageGcsKeyFilename ||
6809
- settings.objectStorageGcsApiEndpoint
6810
- ) {
6811
- throw new Error(
6812
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6813
- );
6814
- }
6815
- const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
6816
- const hasSharedKey =
6817
- Boolean(settings.objectStorageAzureAccountName) &&
6818
- Boolean(settings.objectStorageAzureAccountKey);
6819
- if (!hasConnectionString && !hasSharedKey) {
6820
- throw new Error(
6821
- "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
6822
- );
6823
- }
6824
- } else {
6825
- if (
6826
- settings.objectStorageEndpoint ||
6827
- settings.objectStorageInternalEndpoint ||
6828
- settings.objectStorageSandboxEndpoint ||
6829
- settings.objectStorageAccessKeyId ||
6830
- settings.objectStorageSecretAccessKey
6831
- ) {
6832
- throw new Error(
6833
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
6834
- );
6835
- }
6836
- if (
6837
- settings.objectStorageAzureConnectionString ||
6838
- settings.objectStorageAzureAccountName ||
6839
- settings.objectStorageAzureAccountKey ||
6840
- settings.objectStorageAzureEndpoint
6841
- ) {
6842
- throw new Error(
6843
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6844
- );
6845
- }
6846
- if (settings.objectStorageGcsCredentialsJson) {
6847
- parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
6848
- }
6849
- }
8342
+ validateObjectStorageSettings(settings);
6850
8343
  if (settings.documentChunkOverlap >= settings.documentChunkSize) {
6851
8344
  throw new Error(
6852
8345
  "OPENGENI_DOCUMENT_CHUNK_OVERLAP must be smaller than OPENGENI_DOCUMENT_CHUNK_SIZE",
@@ -6856,7 +8349,36 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6856
8349
  sandboxEnvironmentVariableNames(settings);
6857
8350
  sandboxLifecycleHookIds(settings);
6858
8351
  // Fail fast on a malformed warm-rate table (P2.1).
6859
- parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8352
+ const warmRates = parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8353
+ // The meter rounds each settled interval to whole USD micros. Fractional
8354
+ // micros/second would make the total depend on heartbeat frequency; only
8355
+ // integer rates have interval-independent settlement at the same elapsed time.
8356
+ if (
8357
+ settings.sandboxWarmBillingMode === "credits" &&
8358
+ Object.values(warmRates).some((rate) => !Number.isSafeInteger(rate))
8359
+ ) {
8360
+ throw new Error(
8361
+ "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON paid rates must be safe integers",
8362
+ );
8363
+ }
8364
+ if (
8365
+ settings.documentEmbeddingBillingMode === "credits" &&
8366
+ settings.documentEmbeddingProvider === "openai" &&
8367
+ settings.documentEmbeddingRateMicrosPerMillionBytes <= 0
8368
+ ) {
8369
+ throw new Error(
8370
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES must be positive when paid OpenAI embedding billing is enabled",
8371
+ );
8372
+ }
8373
+ if (
8374
+ settings.documentEmbeddingBillingMode === "credits" &&
8375
+ settings.documentEmbeddingProvider === "openai" &&
8376
+ !settings.documentEmbeddingCreditsActivatedAt
8377
+ ) {
8378
+ throw new Error(
8379
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT is required for paid OpenAI embeddings",
8380
+ );
8381
+ }
6860
8382
  if (settings.sandboxBackend === "opensandbox") {
6861
8383
  if (!/@sha256:[0-9a-f]{64}$/i.test(settings.openSandboxImage ?? "")) {
6862
8384
  throw new Error(
@@ -6875,12 +8397,9 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6875
8397
  throw new Error(`OPENGENI_MCP_SERVERS contains duplicate id ${server.id}`);
6876
8398
  }
6877
8399
  serverIds.add(server.id);
6878
- if (
6879
- server.connectionRef?.authoritySource === "host" &&
6880
- !settings.hostMcpAuthoritySourceAdmissionEnabled
6881
- ) {
8400
+ if (server.connectionRef?.authoritySource === "host") {
6882
8401
  throw new Error(
6883
- "OPENGENI_MCP_SERVERS host-owned connection refs require OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED=true after the whole API/worker fleet is upgraded",
8402
+ "OPENGENI_MCP_SERVERS host-owned connection refs are no longer supported; select ordinary native connections",
6884
8403
  );
6885
8404
  }
6886
8405
  }
@@ -6934,14 +8453,36 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6934
8453
  ordinaryCaptureTimeoutMs,
6935
8454
  drainCaptureTimeoutMs,
6936
8455
  );
6937
- if (!(rotationLeadMs > providerDeadlineCaptureTimeoutMs + reaperPeriod)) {
8456
+ const requiredRotationLeadMs =
8457
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS + providerDeadlineCaptureTimeoutMs + 2 * reaperPeriod;
8458
+ if (!(rotationLeadMs > requiredRotationLeadMs)) {
6938
8459
  throw new Error(
6939
8460
  `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}) must exceed the ` +
6940
- `largest durable snapshot or drain capture timeout plus one reaper period ` +
6941
- `(${providerDeadlineCaptureTimeoutMs + reaperPeriod}), including for persisted Modal ` +
8461
+ `legacy command stop grace, largest durable snapshot or drain capture timeout, and two reaper periods ` +
8462
+ `(${requiredRotationLeadMs}), including for persisted Modal ` +
6942
8463
  `leases after a default-backend rollout.`,
6943
8464
  );
6944
8465
  }
8466
+ const containmentMs = settings.sandboxIdleCommandContainmentMs;
8467
+ if (containmentMs !== undefined && !(containmentMs > settings.sandboxIdleGraceMs)) {
8468
+ throw new Error(
8469
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must exceed ` +
8470
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}): a box kept warm only by ` +
8471
+ `retained commands must stay available at least as long as an idle box awaiting drain.`,
8472
+ );
8473
+ }
8474
+ if (
8475
+ containmentMs !== undefined &&
8476
+ optionalEnvironmentValue("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS", source) !==
8477
+ undefined &&
8478
+ !(containmentMs < rotationLeadMs)
8479
+ ) {
8480
+ throw new Error(
8481
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must be strictly less than ` +
8482
+ `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}): an idle box must be checkpointed ` +
8483
+ `and stopped well before its provider deadline.`,
8484
+ );
8485
+ }
6945
8486
  if (settings.sandboxBackend === "modal") {
6946
8487
  const idleGraceMs = settings.sandboxIdleGraceMs;
6947
8488
  const lifecycle = effectiveSandboxLifecycle(settings, "modal");
@@ -6985,6 +8526,19 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6985
8526
  `OPENGENI_SANDBOX_IDLE_GRACE_MS.`,
6986
8527
  );
6987
8528
  }
8529
+ if (
8530
+ settings.modalIdleTimeoutSeconds !== undefined &&
8531
+ containmentMs !== undefined &&
8532
+ !(reaperPeriod + containmentMs + drainCaptureTimeoutMs < idleTimeoutMs)
8533
+ ) {
8534
+ throw new Error(
8535
+ `OPENGENI_SANDBOX_LEASE_REAPER_PERIOD_MS + OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS + ` +
8536
+ `drain capture timeout (${reaperPeriod} + ${containmentMs} + ${drainCaptureTimeoutMs}) ` +
8537
+ `must be strictly less than OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS*1000 (${idleTimeoutMs}): ` +
8538
+ `idle command containment must checkpoint a box before Modal's own idle reaper stops it. ` +
8539
+ `Lower OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS or raise the Modal idle timeout.`,
8540
+ );
8541
+ }
6988
8542
  }
6989
8543
  }
6990
8544
  // --- stream-token secret: required-when-desktop, but GRACEFULLY DEGRADE (stream-token availability contract) ---
@@ -7042,8 +8596,14 @@ export function validateModelCatalogSettings(
7042
8596
  provider.kind === "vercel-gateway-managed" ||
7043
8597
  provider.kind === "vercel-gateway-workspace" ||
7044
8598
  provider.kind === "vercel-gateway-organization" ||
8599
+ provider.kind === "direct-openai-workspace" ||
8600
+ provider.kind === "direct-azure-workspace" ||
7045
8601
  provider.kind === "openrouter-workspace" ||
7046
8602
  provider.kind === "openrouter-organization" ||
8603
+ provider.kind === "anthropic-organization" ||
8604
+ provider.kind === "anthropic-workspace" ||
8605
+ provider.kind === "claude-subscription-workspace" ||
8606
+ provider.kind === "claude-subscription-organization" ||
7047
8607
  provider.kind === "xai-subscription"
7048
8608
  ) {
7049
8609
  throw new Error(
@@ -7095,11 +8655,39 @@ export function validateModelCatalogSettings(
7095
8655
  `The default model ${settings.openaiModel} is not executable in the resolved model catalog`,
7096
8656
  );
7097
8657
  }
8658
+ // An operator-set credits default must name a credits-billed model in the
8659
+ // env catalog; a typo would otherwise fall back silently to the first
8660
+ // selectable credits model. Only an explicit value in code catalog mode is
8661
+ // checked: the built-in gpt-6-luna may be absent from a custom catalog, and
8662
+ // a database catalog is edited independently of this env value, so both keep
8663
+ // the documented fallback (docs/model-providers.md) instead of failing
8664
+ // every catalog read.
8665
+ const explicitCreditsDefault = source.OPENGENI_CREDITS_DEFAULT_MODEL?.trim();
8666
+ if (
8667
+ explicitCreditsDefault &&
8668
+ settings.modelCatalogSource === "code" &&
8669
+ settings.billingMode === "stripe"
8670
+ ) {
8671
+ const creditsDefaultId = canonicalizeConfiguredModelId(settings, settings.creditsDefaultModel);
8672
+ const creditsDefault = models.find((model) => model.id === creditsDefaultId);
8673
+ if (creditsDefault?.cost !== "credits") {
8674
+ throw new Error(
8675
+ `OPENGENI_CREDITS_DEFAULT_MODEL ${settings.creditsDefaultModel} is not a credits-billed model in the resolved model catalog`,
8676
+ );
8677
+ }
8678
+ }
7098
8679
 
7099
8680
  const deploymentProductIds = new Set(
7100
8681
  models.filter((model) => model.credentialSource.kind === "deployment").map((model) => model.id),
7101
8682
  );
7102
8683
  const noteProductIds = new Set(models.map((model) => model.id));
8684
+ if (settings.resolvedCodexModelsJson !== undefined) {
8685
+ for (const model of z
8686
+ .array(CodexCatalogModelSchema)
8687
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))) {
8688
+ noteProductIds.add(model.id);
8689
+ }
8690
+ }
7103
8691
  for (const model of configuredGatewayCatalogModels(settings)) {
7104
8692
  deploymentProductIds.add(model.productId);
7105
8693
  noteProductIds.add(model.productId);
@@ -7234,6 +8822,108 @@ export function resolveNatsCalloutConfig(settings: Settings): NatsCalloutConfig
7234
8822
  return { accountSeed, accountName, user, password };
7235
8823
  }
7236
8824
 
8825
+ function validateObjectStorageSettings(settings: ObjectStorageSettings): void {
8826
+ if (
8827
+ settings.objectStorageBackend === "s3-compatible" ||
8828
+ settings.objectStorageBackend === "aws-s3"
8829
+ ) {
8830
+ if (
8831
+ Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
8832
+ ) {
8833
+ throw new Error(
8834
+ "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
8835
+ );
8836
+ }
8837
+ if (
8838
+ settings.objectStorageBackend === "s3-compatible" &&
8839
+ (settings.objectStorageEndpoint ||
8840
+ settings.objectStorageInternalEndpoint ||
8841
+ settings.objectStorageSandboxEndpoint) &&
8842
+ (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
8843
+ ) {
8844
+ throw new Error(
8845
+ "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
8846
+ );
8847
+ }
8848
+ if (
8849
+ settings.objectStorageAzureConnectionString ||
8850
+ settings.objectStorageAzureAccountName ||
8851
+ settings.objectStorageAzureAccountKey ||
8852
+ settings.objectStorageAzureEndpoint
8853
+ ) {
8854
+ throw new Error(
8855
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
8856
+ );
8857
+ }
8858
+ if (
8859
+ settings.objectStorageGcsProjectId ||
8860
+ settings.objectStorageGcsCredentialsJson ||
8861
+ settings.objectStorageGcsKeyFilename ||
8862
+ settings.objectStorageGcsApiEndpoint
8863
+ ) {
8864
+ throw new Error(
8865
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
8866
+ );
8867
+ }
8868
+ } else if (settings.objectStorageBackend === "azure-blob") {
8869
+ if (
8870
+ settings.objectStorageEndpoint ||
8871
+ settings.objectStorageInternalEndpoint ||
8872
+ settings.objectStorageSandboxEndpoint ||
8873
+ settings.objectStorageAccessKeyId ||
8874
+ settings.objectStorageSecretAccessKey
8875
+ ) {
8876
+ throw new Error(
8877
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
8878
+ );
8879
+ }
8880
+ if (
8881
+ settings.objectStorageGcsProjectId ||
8882
+ settings.objectStorageGcsCredentialsJson ||
8883
+ settings.objectStorageGcsKeyFilename ||
8884
+ settings.objectStorageGcsApiEndpoint
8885
+ ) {
8886
+ throw new Error(
8887
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
8888
+ );
8889
+ }
8890
+ const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
8891
+ const hasSharedKey =
8892
+ Boolean(settings.objectStorageAzureAccountName) &&
8893
+ Boolean(settings.objectStorageAzureAccountKey);
8894
+ if (!hasConnectionString && !hasSharedKey) {
8895
+ throw new Error(
8896
+ "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
8897
+ );
8898
+ }
8899
+ } else {
8900
+ if (
8901
+ settings.objectStorageEndpoint ||
8902
+ settings.objectStorageInternalEndpoint ||
8903
+ settings.objectStorageSandboxEndpoint ||
8904
+ settings.objectStorageAccessKeyId ||
8905
+ settings.objectStorageSecretAccessKey
8906
+ ) {
8907
+ throw new Error(
8908
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
8909
+ );
8910
+ }
8911
+ if (
8912
+ settings.objectStorageAzureConnectionString ||
8913
+ settings.objectStorageAzureAccountName ||
8914
+ settings.objectStorageAzureAccountKey ||
8915
+ settings.objectStorageAzureEndpoint
8916
+ ) {
8917
+ throw new Error(
8918
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
8919
+ );
8920
+ }
8921
+ if (settings.objectStorageGcsCredentialsJson) {
8922
+ parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
8923
+ }
8924
+ }
8925
+ }
8926
+
7237
8927
  /**
7238
8928
  * The PRIVILEGED control-plane NATS login (api/worker). Present only when BOTH a
7239
8929
  * user and password are set; otherwise null and the bus connects anonymously (local
@@ -7245,7 +8935,9 @@ export interface NatsControlPlaneAuth {
7245
8935
  password: string;
7246
8936
  }
7247
8937
 
7248
- export function resolveNatsControlPlaneAuth(settings: Settings): NatsControlPlaneAuth | null {
8938
+ export function resolveNatsControlPlaneAuth(
8939
+ settings: Pick<Settings, "selfhostedNatsControlUser" | "selfhostedNatsControlPassword">,
8940
+ ): NatsControlPlaneAuth | null {
7249
8941
  const user = settings.selfhostedNatsControlUser?.trim();
7250
8942
  const password = settings.selfhostedNatsControlPassword?.trim();
7251
8943
  if (!user || !password) {
@@ -7261,6 +8953,11 @@ function splitCsv(raw: string): string[] {
7261
8953
  .filter(Boolean);
7262
8954
  }
7263
8955
 
8956
+ /** Image references a workspace may select as its default sandbox image. */
8957
+ export function sandboxImageAllowlist(settings: Pick<Settings, "sandboxImageAllowlist">): string[] {
8958
+ return [...new Set(splitCsv(settings.sandboxImageAllowlist ?? ""))];
8959
+ }
8960
+
7264
8961
  function uniqueEnvNames(raw: string[], fieldName: string): string[] {
7265
8962
  const seen = new Set<string>();
7266
8963
  const out: string[] = [];
@@ -7294,3 +8991,196 @@ function parseGcsCredentialsJson(raw: string): unknown {
7294
8991
  function delay(ms: number): Promise<void> {
7295
8992
  return new Promise((resolve) => setTimeout(resolve, ms));
7296
8993
  }
8994
+
8995
+ /** Native Claude connections reuse the encrypted workspace and organization boundaries. */
8996
+ export const CLAUDE_CONNECTION_KINDS = ["anthropic", "claude_subscription"] as const;
8997
+ export type ClaudeConnectionKind = (typeof CLAUDE_CONNECTION_KINDS)[number];
8998
+ // Per-model native Messages controls. Unknown IDs never inherit adaptive
8999
+ // thinking merely because they share a provider with a supported model.
9000
+ const CLAUDE_NATIVE_MODEL_PROFILES: Readonly<
9001
+ Record<
9002
+ string,
9003
+ Readonly<{
9004
+ efforts: readonly ReasoningEffort[];
9005
+ defaultEffort: ReasoningEffort | null;
9006
+ contextWindowTokens: number;
9007
+ maxOutputTokens: number;
9008
+ }>
9009
+ >
9010
+ > = Object.fromEntries([
9011
+ ...[
9012
+ ["claude-opus-5-5", "medium"],
9013
+ ["claude-sonnet-5-5", "medium"],
9014
+ ["claude-opus-5", "high"],
9015
+ ["claude-sonnet-5", "high"],
9016
+ ["claude-opus-4-8", "high"],
9017
+ ["claude-opus-4-7", "xhigh"],
9018
+ ].map(([id, defaultEffort]) => [
9019
+ id,
9020
+ {
9021
+ efforts: ["low", "medium", "high", "xhigh", "max"],
9022
+ defaultEffort,
9023
+ contextWindowTokens: 1_000_000,
9024
+ maxOutputTokens: 128_000,
9025
+ },
9026
+ ]),
9027
+ ...["claude-opus-4-6", "claude-sonnet-4-6"].map((id) => [
9028
+ id,
9029
+ {
9030
+ efforts: ["low", "medium", "high", "max"],
9031
+ defaultEffort: "high",
9032
+ contextWindowTokens: 1_000_000,
9033
+ maxOutputTokens: 128_000,
9034
+ },
9035
+ ]),
9036
+ [
9037
+ "claude-haiku-4-5-20251001",
9038
+ {
9039
+ efforts: [],
9040
+ defaultEffort: null,
9041
+ contextWindowTokens: 200_000,
9042
+ maxOutputTokens: 64_000,
9043
+ },
9044
+ ],
9045
+ ]);
9046
+ export function claudeNativeModelProfile(upstreamModelId: string) {
9047
+ return Object.hasOwn(CLAUDE_NATIVE_MODEL_PROFILES, upstreamModelId)
9048
+ ? CLAUDE_NATIVE_MODEL_PROFILES[upstreamModelId]
9049
+ : undefined;
9050
+ }
9051
+ export function claudeProviderId(
9052
+ kind: ClaudeConnectionKind,
9053
+ scope: "workspace" | "organization" = "organization",
9054
+ ): string {
9055
+ return `${scope}-${kind === "anthropic" ? "anthropic" : "claude-subscription"}`;
9056
+ }
9057
+ export type ClaudeConnectionCatalog = Partial<
9058
+ Record<
9059
+ ClaudeConnectionKind,
9060
+ {
9061
+ active?: boolean;
9062
+ models: readonly { upstreamModelId: string; label?: string | null }[];
9063
+ }
9064
+ >
9065
+ >;
9066
+ export function withClaudeConnectionCatalog(
9067
+ settings: Settings,
9068
+ connections: ClaudeConnectionCatalog,
9069
+ scope: "workspace" | "organization" = "organization",
9070
+ ): Settings {
9071
+ let providers = parseModelProvidersJson(settings.modelProvidersJson);
9072
+ for (const kind of CLAUDE_CONNECTION_KINDS) {
9073
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled) {
9074
+ providers = providers.filter((provider) => provider.id !== claudeProviderId(kind, scope));
9075
+ continue;
9076
+ }
9077
+ const connection = connections[kind];
9078
+ if (!connection) continue;
9079
+ const id = claudeProviderId(kind, scope);
9080
+ providers = providers.filter((provider) => provider.id !== id);
9081
+ if (!connection.models.length) continue;
9082
+ providers.push({
9083
+ id,
9084
+ label: kind === "anthropic" ? "Anthropic API" : "Claude subscription",
9085
+ kind: kind === "anthropic" ? `anthropic-${scope}` : `claude-subscription-${scope}`,
9086
+ api: "anthropic-messages",
9087
+ wireProfile: "openai",
9088
+ baseUrl: "https://api.anthropic.com/v1",
9089
+ anthropic: {
9090
+ auth: kind === "anthropic" ? "api-key" : "oauth",
9091
+ cacheTtl: "5m",
9092
+ maxOutputTokens: 128000,
9093
+ streamIdleTimeoutMs: 600000,
9094
+ },
9095
+ models: connection.models.map((model) => {
9096
+ const profile = claudeNativeModelProfile(model.upstreamModelId);
9097
+ const adaptiveThinking = Boolean(profile?.efforts.length);
9098
+ const contextWindowTokens = profile?.contextWindowTokens ?? 200_000;
9099
+ const outputReserve = profile?.maxOutputTokens ?? 32_000;
9100
+ return {
9101
+ contextWindowTokens,
9102
+ effectiveContextWindowTokens: contextWindowTokens - outputReserve,
9103
+ autoCompactTokenLimit:
9104
+ contextWindowTokens === 1_000_000
9105
+ ? 800_000
9106
+ : Math.min(150_000, contextWindowTokens - outputReserve - 18_000),
9107
+ id: id + "/" + model.upstreamModelId,
9108
+ upstreamModelId: model.upstreamModelId,
9109
+ label:
9110
+ model.label ??
9111
+ (model.upstreamModelId === "claude-opus-5-5"
9112
+ ? "Claude Opus 5.5"
9113
+ : model.upstreamModelId === "claude-sonnet-5-5"
9114
+ ? "Claude Sonnet 5.5"
9115
+ : model.upstreamModelId),
9116
+ reasoningEffort: adaptiveThinking,
9117
+ hostedWebSearch: false,
9118
+ capabilities: {
9119
+ ...legacyModelCapabilities(settings, {
9120
+ reasoningEffort: adaptiveThinking,
9121
+ hostedWebSearch: false,
9122
+ vision: true,
9123
+ }),
9124
+ inputFileMediaTypes: [],
9125
+ reasoning: {
9126
+ upstream: adaptiveThinking ? "supported" : "unknown",
9127
+ runnable: adaptiveThinking,
9128
+ efforts: [...(profile?.efforts ?? [])],
9129
+ defaultEffort: profile?.defaultEffort ?? null,
9130
+ required: false,
9131
+ },
9132
+ functionCalling: { upstream: "supported", runnable: true },
9133
+ promptCaching: { upstream: "supported", runnable: true, mode: "automatic" },
9134
+ },
9135
+ };
9136
+ }),
9137
+ });
9138
+ }
9139
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
9140
+ }
9141
+ export function withClaudeConnectionCredential(
9142
+ settings: Settings,
9143
+ kind: ClaudeConnectionKind,
9144
+ credential: string,
9145
+ scope: "workspace" | "organization" = "organization",
9146
+ credentialBinding?: { connectionId: string; credentialVersion: number },
9147
+ ): Settings {
9148
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled)
9149
+ throw new Error("Claude subscriptions are not enabled on this deployment");
9150
+ if (!credential.trim()) throw new Error("Claude credential is empty");
9151
+ const bundle =
9152
+ kind === "claude_subscription" && credential.trim().startsWith("{")
9153
+ ? ClaudeSubscriptionCredential.parse(JSON.parse(credential))
9154
+ : null;
9155
+ return {
9156
+ ...settings,
9157
+ modelProvidersJson: JSON.stringify(
9158
+ parseModelProvidersJson(settings.modelProvidersJson).map((provider) =>
9159
+ provider.id === claudeProviderId(kind, scope)
9160
+ ? {
9161
+ ...provider,
9162
+ apiKey: bundle?.token ?? credential,
9163
+ ...(kind === "claude_subscription" && provider.anthropic
9164
+ ? {
9165
+ anthropic: {
9166
+ ...provider.anthropic,
9167
+ identity: bundle?.identity,
9168
+ credentialBinding: credentialBinding
9169
+ ? {
9170
+ connectionId: credentialBinding.connectionId,
9171
+ credentialVersion: credentialBinding.credentialVersion,
9172
+ }
9173
+ : undefined,
9174
+ },
9175
+ }
9176
+ : {}),
9177
+ }
9178
+ : provider,
9179
+ ),
9180
+ ),
9181
+ };
9182
+ }
9183
+ export * from "./claude-subscription-usage";
9184
+ export * from "./claude-subscription-oauth";
9185
+ export * from "./subscription-account-selection";
9186
+ export * from "./claude-subscription-capacity";