@opengeni/config 1.0.0-canary.6 → 1.0.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
- "Installed and selected Skills are indexed under .agents/ and may include 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.",
224
+ "Installed and selected Skills appear in the session Skill index; follow its reading instructions and any role-specific guidance.",
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(),
@@ -392,8 +407,14 @@ const SettingsSchema = z.object({
392
407
  // variable out to disable it).
393
408
  organizationTenancyCanonicalActivationEnabled: EnvBoolean.default(false),
394
409
  billingMode: BillingMode.default("disabled"),
410
+ // Explicit launch gate for the one-time $10 verified self-service signup grant.
411
+ // A migration or deployment alone must not start issuing live credits.
412
+ verifiedSignupTrialCreditsEnabled: EnvBoolean.default(false),
395
413
  entitlementsMode: EntitlementsMode.default("none"),
396
414
  usageLimitsMode: UsageLimitsMode.default("none"),
415
+ // Gate new allowance policies until every API/worker in the fleet enforces
416
+ // them. Persisted-policy enforcement and recovery reads/clears stay active.
417
+ usageAllowancesEnabled: EnvBoolean.default(false),
397
418
  staticEntitlementsJson: z.string().default("{}"),
398
419
  staticUsageLimitsJson: z.string().default("{}"),
399
420
  delegationSecret: z.string().optional(),
@@ -434,11 +455,18 @@ const SettingsSchema = z.object({
434
455
  environmentsEncryptionKey: z.string().optional(),
435
456
  integrationsEnabled: EnvBoolean.default(false),
436
457
  integrationsStateSecret: z.string().optional(),
458
+ integrationsOauthShortStateEnabled: EnvBoolean.default(false),
437
459
  integrationsAllowPrivateNetworkTargets: EnvBoolean.default(false),
438
460
  integrationsOauthClientsJson: z.string().default("{}"),
461
+ gmailWatchTopicName: z
462
+ .string()
463
+ .regex(/^projects\/[^/]+\/topics\/[^/]+$/u)
464
+ .optional(),
439
465
  slackClientId: z.string().optional(),
440
466
  slackClientSecret: z.string().optional(),
441
467
  slackSigningSecret: z.string().optional(),
468
+ // Unlisted apps share Slack’s restricted history/replies quota across all tokens.
469
+ slackAccessMode: z.enum(["limited", "full"]).default("limited"),
442
470
  slackBotDisplayName: OpenGeniSlackBotDisplayName.default("OpenGeni"),
443
471
  slackCommand: z
444
472
  .string()
@@ -525,13 +553,6 @@ const SettingsSchema = z.object({
525
553
  // into @opengeni/db once at boot.
526
554
  // Env: OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED.
527
555
  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
556
  // Per-channel and per-DM Slack workspace routing. Default ON. A channel does
536
557
  // not count a personal workspace as a candidate, so an organization with one
537
558
  // shared workspace resolves it as the sole candidate and never asks; the
@@ -589,6 +610,9 @@ const SettingsSchema = z.object({
589
610
  authAllowMetrics: EnvBoolean.default(false),
590
611
  apiHost: z.string().default("0.0.0.0"),
591
612
  apiPort: z.coerce.number().int().positive().default(8000),
613
+ // When set, GET /metrics is served only on this dedicated internal listener
614
+ // and never on the public API port that an ingress forwards to.
615
+ apiMetricsPort: z.coerce.number().int().positive().optional(),
592
616
  workerHttpPort: z.coerce.number().int().positive().default(8001),
593
617
  // Worker-side first-party MCP traffic stays on the deployment's internal
594
618
  // network. OPENGENI_MCP_URL remains the sandbox/external route used by
@@ -599,11 +623,16 @@ const SettingsSchema = z.object({
599
623
  // call the public API with bearer credentials, but never receive credentialed
600
624
  // CORS responses.
601
625
  corsAllowOriginRegex: z.string().default(String.raw`^https?://(localhost|127\.0\.0\.1)(:\d+)?$`),
626
+ // Local development only (`local` access mode in the `local` environment):
627
+ // extra exact browser origins, comma-separated, that may call the
628
+ // unauthenticated API besides this stack's own web origin. The local browser
629
+ // boundary ignores `corsAllowOriginRegex`.
630
+ localAllowedOrigins: z.string().optional(),
602
631
  openaiProvider: z.enum(["openai", "azure"]).default("openai"),
603
632
  openaiApiKey: z.string().optional(),
604
633
  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"),
634
+ openaiModel: z.string().default("gpt-6-astra"),
635
+ openaiAllowedModels: z.string().default("gpt-6-astra,gpt-6-sol,gpt-6-luna"),
607
636
  // OpenGeni-managed Vercel AI Gateway. When configured, the two reviewed
608
637
  // Gateway models below are added to the managed-credit catalog. Workspace
609
638
  // Gateway keys use the encrypted connection broker and never this secret.
@@ -732,6 +761,7 @@ const SettingsSchema = z.object({
732
761
  // binding so database mode cannot be bypassed with a second source.
733
762
  resolvedGatewayModelsJson: z.string().optional(),
734
763
  resolvedOpenRouterModelsJson: z.string().optional(),
764
+ resolvedCodexModelsJson: z.string().optional(),
735
765
  // Extra (non-built-in) model providers, declared by the host as a JSON
736
766
  // provider registry. Each entry carries its own base URL, API key, wire API
737
767
  // ("responses" | "chat") and the models it exposes. The models a client may
@@ -746,6 +776,7 @@ const SettingsSchema = z.object({
746
776
  // SuperGrok/xAI connected subscription. This is a workspace-scoped OAuth
747
777
  // account pool and a distinct rail from the existing xai/* API-key provider.
748
778
  supergrokSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED
779
+ claudeSubscriptionEnabled: EnvBoolean.default(false), // OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED
749
780
  // Maximum silence between complete, valid SuperGrok SSE data events. This is
750
781
  // not a request/run duration cap; every valid event resets the timer.
751
782
  supergrokResponseStreamIdleTimeoutMs: z.coerce
@@ -782,8 +813,27 @@ const SettingsSchema = z.object({
782
813
  // rotated TO — when EITHER usage window (5h/weekly) is at/over this percent. Default 90 to
783
814
  // match the UI danger flip (UsageBar danger at pct >= 90). OPENGENI_CODEX_ROTATION_NEAR_EXHAUSTION_PCT.
784
815
  codexRotationNearExhaustionPct: z.coerce.number().int().min(1).max(100).default(90),
816
+ reasoningConfigurationUpdatesEnabled: EnvBoolean.default(false),
785
817
  openaiReasoningEffort: ReasoningEffort.default("low"),
786
818
  openaiAllowedReasoningEfforts: z.string().default("low,medium,high,xhigh,max"),
819
+ // Default for new chats and scheduled tasks when the workspace has an
820
+ // OpenGeni credit balance, no saved workspace default, and no usable
821
+ // connected subscription, and the deployment default is not already a
822
+ // credits-billed model. Selected only when this model is selectable in the
823
+ // workspace catalog; otherwise the first selectable credits model is used.
824
+ // OPENGENI_CREDITS_DEFAULT_MODEL / OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT.
825
+ creditsDefaultModel: z
826
+ .string()
827
+ .trim()
828
+ .min(1)
829
+ .max(256)
830
+ .refine((value) => !/[\u000A\u000D|]/u.test(value), {
831
+ message: "credits default model must not contain newlines or the | field separator",
832
+ })
833
+ .default("gpt-6-luna"),
834
+ // Preferred effort for the credits default. Clamped to the highest effort
835
+ // the selected model supports at or below this value.
836
+ creditsDefaultReasoningEffort: ReasoningEffort.default("xhigh"),
787
837
  openaiResponsesTransport: z.enum(["http", "websocket"]).default("http"),
788
838
  // Provider-assigned item ids (rs_/msg_/fc_…) in Responses API input are
789
839
  // resolved against the provider's server-side response store. That store is
@@ -809,6 +859,19 @@ const SettingsSchema = z.object({
809
859
  // merged with the MCP-server tools (getAllTools = [...mcpTools, ...tools])
810
860
  // and the sandbox capability tools, never replacing them.
811
861
  webSearchEnabled: EnvBoolean.default(true),
862
+ // Jev (TypeSafe's fast judge model) for worker-side agent tools. Without a
863
+ // usable key every Jev-backed feature is off. The key stays on the server
864
+ // (API and worker) and never reaches a sandbox or Connected Machine.
865
+ jevApiKey: z.string().optional(),
866
+ jevBaseUrl: z.string().url().default("https://api.typesafe.ai"),
867
+ jevModel: z.string().trim().min(1).max(128).default("jev-latest"),
868
+ jevRequestTimeoutMs: z.coerce.number().int().positive().max(120_000).default(10_000),
869
+ // Jev-backed `code_search` agent tool. `off` never offers it, `opt_in` offers
870
+ // it only where workspace settings enable it, `default_on` offers it
871
+ // everywhere except workspaces that disable it, and `experiment` gives it to
872
+ // a fixed half of sessions in workspaces without their own setting, for
873
+ // comparison. It also needs jevApiKey.
874
+ codeSearchMode: z.enum(["off", "opt_in", "default_on", "experiment"]).default("off"),
812
875
  // Deployment-default agent persona template (the white-label surface). The
813
876
  // runtime resolves the effective template per turn as
814
877
  // per-session-override > per-workspace override > this default, substitutes
@@ -830,7 +893,7 @@ const SettingsSchema = z.object({
830
893
  // the verified, self-contained native artifact runtime at its fixed image
831
894
  // paths. Disabled by default so arbitrary/custom provider images never make
832
895
  // document/spreadsheet/presentation skills appear when their runtime is
833
- // absent. Per-pack/per-rig image overrides fail closed in the worker even
896
+
834
897
  // when this base-image contract is enabled.
835
898
  sandboxArtifactRuntimeEnabled: EnvBoolean.default(false),
836
899
  dockerExposedPorts: z.string().default(""),
@@ -842,6 +905,9 @@ const SettingsSchema = z.object({
842
905
  dockerWorkspaceBaseDir: z.string().min(1).optional(),
843
906
  modalAppName: z.string().default("opengeni-sandbox"),
844
907
  modalImageRef: z.string().optional(),
908
+ // Activate only after descriptor-aware readers, DB fences and the exact
909
+ // native image have passed the supervised-command rollout canary.
910
+ modalCommandSupervisionEnabled: EnvBoolean.default(false),
845
911
  // Provider-native immutable Modal image ID for the exact logical
846
912
  // `modalImageRef`. When set, the runtime uses ModalImageSelector.fromId and
847
913
  // never asks Modal to parse or import the registry ref. The logical ref is
@@ -919,12 +985,10 @@ const SettingsSchema = z.object({
919
985
  // Shared desktop toggle: this module reads it for the 6080 port-merge; the
920
986
  // owner module (P4.x) acts on it to launch the display stack.
921
987
  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.
988
+ // Human sandbox input policy. Canonical ComputerSession attachments reflect
989
+ // it and /actions enforces it independently of the client. The legacy
990
+ // DesktopStream adapter also reports read-only when disabled. Agent tools
991
+ // retain their separate session-control authority.
928
992
  sandboxDesktopInteractive: EnvBoolean.default(true),
929
993
  // REAL PTY terminal toggle (P5.t): gates the ttyd pty-ws plane (7681) the API
930
994
  // mints over the SAME tunnel as the desktop. Defaults ON — the interactive
@@ -988,6 +1052,7 @@ const SettingsSchema = z.object({
988
1052
  // --- cloudflare (headless) ---
989
1053
  cloudflareWorkerUrl: z.string().url().optional(),
990
1054
  cloudflareApiKey: z.string().optional(),
1055
+ experimentalBrowserContextPoolEnabled: EnvBoolean.default(false),
991
1056
  // --- remote browser placements ---
992
1057
  // Provider credentials are injected only into the placement-resident
993
1058
  // browserd launch. They never enter session contracts, journals, or sandboxes.
@@ -1173,6 +1238,33 @@ const SettingsSchema = z.object({
1173
1238
  // getSettings caps the default at half a shorter configured Modal lifetime so
1174
1239
  // the entire reaper window always fits. Knob: OPENGENI_SANDBOX_IDLE_GRACE_MS.
1175
1240
  sandboxIdleGraceMs: z.coerce.number().int().positive().default(900_000),
1241
+ // Idle command containment. A legacy retained background command (a dev
1242
+ // server, a command whose output is still draining, a stopped command the
1243
+ // provider no longer answers for) keeps its box warm through a non-expiring
1244
+ // process holder, so the zero-holder drain never runs and the box would stay
1245
+ // up until the provider deadline kills it uncaptured. Once every session of
1246
+ // the sandbox group has been unused this long - no open turn or pending
1247
+ // request, no held wait_for_input, no other holder or writer, measured from
1248
+ // durable turn/holder/admission facts - the reaper checkpoints the workspace,
1249
+ // stops the box and settles those commands `lost` with reason
1250
+ // `idle_containment`. Default 30min: twice the default
1251
+ // idle grace, so a lease that is only waiting for a "glanced away" user is
1252
+ // never contained earlier than an idle lease would drain, and well inside
1253
+ // the 1h provider-deadline rotation lead, so an idle box is saved long before
1254
+ // the deadline path has to act. Set 0 to disable new idle enrollments without
1255
+ // disabling provider-deadline containment or cancelling an enrolled drain.
1256
+ // A positive window must exceed OPENGENI_SANDBOX_IDLE_GRACE_MS;
1257
+ // an explicit positive value must also stay below OPENGENI_SANDBOX_ROTATION_LEAD_MS,
1258
+ // and with an explicit OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS the reaper period
1259
+ // plus this window plus the drain capture budget must fit before it.
1260
+ // getSettings derives the unset default between those two for short-lived
1261
+ // provider lifetimes. Knob: OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS.
1262
+ sandboxIdleCommandContainmentMs: z.coerce
1263
+ .number()
1264
+ .int()
1265
+ .nonnegative()
1266
+ .optional()
1267
+ .transform((value) => (value === 0 ? undefined : value)),
1176
1268
  // MID-SESSION /workspace snapshot cadence (sandbox-file-persistence). The
1177
1269
  // reaper's drain-persist only protects boxes the reaper itself kills; a box
1178
1270
  // that dies any other way (Modal's hard creation-time timeout on a session
@@ -1256,6 +1348,7 @@ const SettingsSchema = z.object({
1256
1348
  // Shape: { "modal": 5, "runloop": 4, ... }. Backends absent here meter
1257
1349
  // warm-seconds but accrue NO warm_cost / debit (rate 0).
1258
1350
  sandboxWarmRateMicrosPerSecondJson: z.string().default("{}"),
1351
+ sandboxWarmBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1259
1352
  // Per-workspace warm cap (cumulative warm-seconds since the start of the UTC
1260
1353
  // month, summed over sandbox.warm_seconds). 0 = unbounded. A workspace over the
1261
1354
  // cap force-drains its VIEWER-ONLY boxes (guarded AND turn_holders=0 — a paying
@@ -1263,6 +1356,9 @@ const SettingsSchema = z.object({
1263
1356
  sandboxMaxWarmSecondsPerWorkspace: z.coerce.number().int().nonnegative().default(0),
1264
1357
  sandboxPreparationProfiles: z.string().default("none"),
1265
1358
  sandboxEnvAllowlist: z.string().default(""),
1359
+ // Comma-separated image references a workspace may select as its default
1360
+ // sandbox image. Empty keeps every workspace on the deployment image.
1361
+ sandboxImageAllowlist: z.string().default(""),
1266
1362
  objectStorageEndpoint: z.string().url().optional(),
1267
1363
  objectStorageInternalEndpoint: z.string().url().optional(),
1268
1364
  objectStorageSandboxEndpoint: z.string().url().optional(),
@@ -1291,6 +1387,13 @@ const SettingsSchema = z.object({
1291
1387
  documentEmbeddingDimensions: z.coerce.number().int().positive().default(3072),
1292
1388
  documentEmbeddingApiKey: z.string().optional(),
1293
1389
  documentEmbeddingBaseUrl: z.string().url().optional(),
1390
+ documentEmbeddingBillingMode: z.enum(["usage_only", "shadow", "credits"]).default("usage_only"),
1391
+ // Explicit commercial cutover. Existing queued revisions remain unpriced;
1392
+ // only jobs created at or after this instant may enter the paid rail.
1393
+ documentEmbeddingCreditsActivatedAt: z.iso.datetime().optional(),
1394
+ // Customer tariff for the exact UTF-8 bytes sent to the embedder. Zero leaves
1395
+ // the paid mode unavailable; no commercial rate is selected by this PR.
1396
+ documentEmbeddingRateMicrosPerMillionBytes: z.coerce.number().int().nonnegative().default(0),
1294
1397
  documentCurationProvider: z.enum(["openai", "heuristic", "none"]).default("openai"),
1295
1398
  documentCurationModel: z.string().min(1).default("gpt-4o-mini"),
1296
1399
  documentCurationApiKey: z.string().optional(),
@@ -1328,7 +1431,17 @@ const SettingsSchema = z.object({
1328
1431
  managedAuthGithubClientSecret: z.string().optional(),
1329
1432
  // Rolling browser login-slot compatibility. Repository/deployment default is
1330
1433
  // deliberately legacy; changing to broker is an operator-authorized rollout.
1434
+ allowedUserEmails: z
1435
+ .array(z.email().transform((email) => email.toLowerCase()))
1436
+ .min(1)
1437
+ .optional(),
1331
1438
  managedAuthSessionSetMode: z.enum(["legacy", "dual", "broker"]).default("legacy"),
1439
+ // Deployment ceiling for new managed accounts. When false, managed auth
1440
+ // refuses every new Better Auth account (email/password sign-up and implicit
1441
+ // Google/GitHub sign-up) while existing sign-in, sessions, password reset,
1442
+ // email verification, and invitation-bound account setup keep working. Read
1443
+ // at startup; the 0585 runtime switch pauses sign-ups without a restart.
1444
+ managedAuthNewSignupsEnabled: EnvBoolean.default(true),
1332
1445
  // Query transport is an explicit second-stage rollout. A pre-compatibility
1333
1446
  // web image understands only fragment bearers, so API replicas must keep
1334
1447
  // generating fragment links until the compatible web fleet has converged.
@@ -1363,12 +1476,131 @@ const SettingsSchema = z.object({
1363
1476
  */
1364
1477
  headers: z.record(z.string(), z.string()).optional(),
1365
1478
  connectionRef: McpServerConnectionRefSchema.optional(),
1479
+ operationRecovery: McpOperationRecoverySchema.optional(),
1366
1480
  }),
1367
1481
  )
1368
1482
  .default([]),
1369
1483
  });
1370
1484
 
1371
1485
  export type Settings = z.infer<typeof SettingsSchema>;
1486
+
1487
+ // Independently supervised artifact services are not API/agent runtimes. Parse
1488
+ // only their actual capabilities, using the same field schemas as getSettings.
1489
+ const ArtifactServiceSettingsSchema = SettingsSchema.pick({
1490
+ serviceName: true,
1491
+ environment: true,
1492
+ deploymentRevision: true,
1493
+ dbSchema: true,
1494
+ rlsStrategy: true,
1495
+ observabilityStructuredLogs: true,
1496
+ observabilityMetricsEnabled: true,
1497
+ observabilityOtlpEndpoint: true,
1498
+ observabilityOtlpHeaders: true,
1499
+ });
1500
+ const ArtifactOutboxSettingsSchema = ArtifactServiceSettingsSchema.extend({
1501
+ natsUrl: SettingsSchema.shape.natsUrl,
1502
+ selfhostedNatsControlUser: SettingsSchema.shape.selfhostedNatsControlUser,
1503
+ selfhostedNatsControlPassword: SettingsSchema.shape.selfhostedNatsControlPassword,
1504
+ });
1505
+ const ObjectStorageSettingsSchema = SettingsSchema.pick({
1506
+ objectStorageBackend: true,
1507
+ objectStorageEndpoint: true,
1508
+ objectStorageInternalEndpoint: true,
1509
+ objectStorageSandboxEndpoint: true,
1510
+ objectStorageBucket: true,
1511
+ objectStorageRegion: true,
1512
+ objectStorageAccessKeyId: true,
1513
+ objectStorageSecretAccessKey: true,
1514
+ objectStorageForcePathStyle: true,
1515
+ objectStorageAzureConnectionString: true,
1516
+ objectStorageAzureAccountName: true,
1517
+ objectStorageAzureAccountKey: true,
1518
+ objectStorageAzureEndpoint: true,
1519
+ objectStorageGcsProjectId: true,
1520
+ objectStorageGcsCredentialsJson: true,
1521
+ objectStorageGcsKeyFilename: true,
1522
+ objectStorageGcsApiEndpoint: true,
1523
+ });
1524
+ const ArtifactMaterializerSettingsSchema = ArtifactServiceSettingsSchema.extend(
1525
+ ObjectStorageSettingsSchema.shape,
1526
+ );
1527
+ export type ArtifactOutboxSettings = z.infer<typeof ArtifactOutboxSettingsSchema>;
1528
+ export type ObjectStorageSettings = z.infer<typeof ObjectStorageSettingsSchema>;
1529
+ export type ArtifactMaterializerSettings = z.infer<typeof ArtifactMaterializerSettingsSchema>;
1530
+
1531
+ function artifactServiceEnvironment(source: NodeJS.ProcessEnv) {
1532
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1533
+ return {
1534
+ serviceName: optional("OPENGENI_SERVICE_NAME"),
1535
+ environment: optional("OPENGENI_ENVIRONMENT"),
1536
+ deploymentRevision:
1537
+ optional("OPENGENI_DEPLOYMENT_REVISION") ??
1538
+ optional("SOURCE_VERSION") ??
1539
+ optional("GITHUB_SHA"),
1540
+ dbSchema: optional("OPENGENI_DB_SCHEMA"),
1541
+ rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
1542
+ observabilityStructuredLogs: optional("OPENGENI_OBSERVABILITY_STRUCTURED_LOGS"),
1543
+ observabilityMetricsEnabled: optional("OPENGENI_OBSERVABILITY_METRICS_ENABLED"),
1544
+ observabilityOtlpEndpoint:
1545
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
1546
+ observabilityOtlpHeaders:
1547
+ optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
1548
+ };
1549
+ }
1550
+
1551
+ export function getArtifactOutboxSettings(
1552
+ source: NodeJS.ProcessEnv = process.env,
1553
+ ): ArtifactOutboxSettings {
1554
+ const settings = ArtifactOutboxSettingsSchema.parse({
1555
+ ...artifactServiceEnvironment(source),
1556
+ natsUrl: optionalEnvironmentValue("OPENGENI_NATS_URL", source),
1557
+ selfhostedNatsControlUser: optionalEnvironmentValue(
1558
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_USER",
1559
+ source,
1560
+ ),
1561
+ selfhostedNatsControlPassword: optionalEnvironmentValue(
1562
+ "OPENGENI_SELFHOSTED_NATS_CONTROL_PASSWORD",
1563
+ source,
1564
+ ),
1565
+ });
1566
+ dbSearchPath(settings);
1567
+ if (
1568
+ Boolean(settings.selfhostedNatsControlUser) !== Boolean(settings.selfhostedNatsControlPassword)
1569
+ ) {
1570
+ throw new Error("Artifact outbox NATS control user and password must be configured together");
1571
+ }
1572
+ return settings;
1573
+ }
1574
+
1575
+ export function getArtifactMaterializerSettings(
1576
+ source: NodeJS.ProcessEnv = process.env,
1577
+ ): ArtifactMaterializerSettings {
1578
+ const optional = (name: string) => optionalEnvironmentValue(name, source);
1579
+ const settings = ArtifactMaterializerSettingsSchema.parse({
1580
+ ...artifactServiceEnvironment(source),
1581
+ objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
1582
+ objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
1583
+ objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
1584
+ objectStorageBackend: optional("OPENGENI_OBJECT_STORAGE_BACKEND"),
1585
+ objectStorageBucket: optional("OPENGENI_OBJECT_STORAGE_BUCKET"),
1586
+ objectStorageRegion: optional("OPENGENI_OBJECT_STORAGE_REGION"),
1587
+ objectStorageAccessKeyId: optional("OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID"),
1588
+ objectStorageSecretAccessKey: optional("OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY"),
1589
+ objectStorageForcePathStyle: optional("OPENGENI_OBJECT_STORAGE_FORCE_PATH_STYLE"),
1590
+ objectStorageAzureConnectionString: optional("OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING"),
1591
+ objectStorageAzureAccountName: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME"),
1592
+ objectStorageAzureAccountKey: optional("OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY"),
1593
+ objectStorageAzureEndpoint: optional("OPENGENI_OBJECT_STORAGE_AZURE_ENDPOINT"),
1594
+ objectStorageGcsProjectId: optional("OPENGENI_OBJECT_STORAGE_GCS_PROJECT_ID"),
1595
+ objectStorageGcsCredentialsJson: optional("OPENGENI_OBJECT_STORAGE_GCS_CREDENTIALS_JSON"),
1596
+ objectStorageGcsKeyFilename: optional("OPENGENI_OBJECT_STORAGE_GCS_KEY_FILENAME"),
1597
+ objectStorageGcsApiEndpoint: optional("OPENGENI_OBJECT_STORAGE_GCS_API_ENDPOINT"),
1598
+ });
1599
+ dbSearchPath(settings);
1600
+ validateObjectStorageSettings(settings);
1601
+ return settings;
1602
+ }
1603
+
1372
1604
  export type McpServerConfig = Settings["mcpServers"][number];
1373
1605
 
1374
1606
  export type GoogleDriveProviderRetryOptions = {
@@ -1476,6 +1708,48 @@ export type VoiceInputProviderConfig =
1476
1708
  experimental: true;
1477
1709
  };
1478
1710
 
1711
+ function usableDeploymentSecret(value: string | null | undefined): string | undefined {
1712
+ return isUsableVoiceInputSecret(value) ? value : undefined;
1713
+ }
1714
+
1715
+ /** The deployment's Jev key, or undefined when it is missing or a placeholder. */
1716
+ export function usableJevApiKey(settings: Pick<Settings, "jevApiKey">): string | undefined {
1717
+ return usableDeploymentSecret(settings.jevApiKey);
1718
+ }
1719
+
1720
+ /** Deployment half of agent configuration: hard capability limits. */
1721
+ export function agentConfigDeploymentPolicy(
1722
+ settings: Pick<
1723
+ Settings,
1724
+ "webSearchEnabled" | "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools"
1725
+ >,
1726
+ ): AgentConfigDeploymentLimits {
1727
+ return agentConfigDeploymentLimitsFromAllowlist(
1728
+ resolveFirstPartyMcpToolPolicy(settings).allowed,
1729
+ settings.webSearchEnabled ? {} : { webSearch: "web search is turned off on this server" },
1730
+ );
1731
+ }
1732
+
1733
+ /**
1734
+ * Deployment half of the `code_search` decision. `available` is false when
1735
+ * the mode is off or no usable Jev key is configured; workspaces then cannot
1736
+ * turn it on. `workspaceDefault` applies to workspaces without their own
1737
+ * setting.
1738
+ */
1739
+ export function codeSearchDeploymentPolicy(
1740
+ settings: Pick<Settings, "codeSearchMode" | "jevApiKey">,
1741
+ ): CodeSearchDeploymentPolicy {
1742
+ const available = settings.codeSearchMode !== "off" && usableJevApiKey(settings) !== undefined;
1743
+ if (!available) return { available: false, workspaceDefault: "off" };
1744
+ const workspaceDefault =
1745
+ settings.codeSearchMode === "default_on"
1746
+ ? "on"
1747
+ : settings.codeSearchMode === "experiment"
1748
+ ? "split"
1749
+ : "off";
1750
+ return { available, workspaceDefault };
1751
+ }
1752
+
1479
1753
  /**
1480
1754
  * Reject empty / template secrets so `.env.example` placeholders like
1481
1755
  * `your-key` cannot advertise voice input as available and then 401 upstream.
@@ -1667,6 +1941,27 @@ export type ModelUsageCostBreakdown = {
1667
1941
  creditCostMicros: number;
1668
1942
  };
1669
1943
 
1944
+ export type ModelListCostClassesMicros = {
1945
+ uncachedInput: number;
1946
+ cacheRead: number;
1947
+ cacheWrite: number;
1948
+ output: number;
1949
+ };
1950
+
1951
+ export type ModelListUsageCostSnapshot = ModelUsageCostBreakdown & {
1952
+ /** Captured upstream list costs, summing to providerCostMicros, never debits. */
1953
+ listByClassMicros: ModelListCostClassesMicros | null;
1954
+ /** True only when a returned class split includes latency rounding allocation. */
1955
+ listByClassApprox: boolean;
1956
+ };
1957
+
1958
+ export type ModelRecordedListCostAllocation = {
1959
+ /** Approximate attribution of a stored upstream total, never a new price. */
1960
+ listByClassMicros: ModelListCostClassesMicros | null;
1961
+ /** True for every eligible historical allocation, including known zero. */
1962
+ listByClassApprox: boolean;
1963
+ };
1964
+
1670
1965
  export type StaticUsageLimitsConfig = StaticUsageLimits;
1671
1966
  export type EntitlementsConfig = Entitlements;
1672
1967
 
@@ -1837,7 +2132,7 @@ export type ModelExecutionLimitsV1 = {
1837
2132
 
1838
2133
  export type CredentialSourceV1 =
1839
2134
  | { kind: "deployment"; mechanism: "api_key" | "azure_ad_bearer" | "none" }
1840
- | { kind: "connected_subscription"; provider: "codex" | "xai" }
2135
+ | { kind: "connected_subscription"; provider: "codex" | "xai" | "claude" }
1841
2136
  | { kind: "workspace_connection"; mechanism: "api_key" }
1842
2137
  | { kind: "organization_connection"; mechanism: "api_key" };
1843
2138
 
@@ -1853,7 +2148,7 @@ export type BillingAttributionV1 = {
1853
2148
  * wired as "chat" because its beta Responses endpoint echoes input back and
1854
2149
  * silently no-ops hosted tools (see docs/model-providers.md).
1855
2150
  */
1856
- export const ModelProviderApi = z.enum(["responses", "chat"]);
2151
+ export const ModelProviderApi = z.enum(["responses", "chat", "anthropic-messages"]);
1857
2152
  export type ModelProviderApi = z.infer<typeof ModelProviderApi>;
1858
2153
 
1859
2154
  /**
@@ -1878,8 +2173,14 @@ export const RegistryProviderKind = z.enum([
1878
2173
  "vercel-gateway-managed",
1879
2174
  "vercel-gateway-workspace",
1880
2175
  "vercel-gateway-organization",
2176
+ "direct-openai-workspace",
2177
+ "direct-azure-workspace",
1881
2178
  "openrouter-workspace",
1882
2179
  "openrouter-organization",
2180
+ "anthropic-organization",
2181
+ "claude-subscription-organization",
2182
+ "anthropic-workspace",
2183
+ "claude-subscription-workspace",
1883
2184
  ]);
1884
2185
  export type RegistryProviderKind = z.infer<typeof RegistryProviderKind>;
1885
2186
 
@@ -1933,12 +2234,51 @@ const RegistryModelSchema = z
1933
2234
  });
1934
2235
 
1935
2236
  /** A non-built-in provider declared by the host via OPENGENI_MODEL_PROVIDERS_JSON. */
2237
+ export const ClaudeSubscriptionIdentity = z
2238
+ .object({
2239
+ accountUuid: z.union([z.string().uuid(), z.literal("")]),
2240
+ deviceId: z.string().regex(/^[a-f0-9]{64}$/),
2241
+ })
2242
+ .strict();
2243
+ export type ClaudeSubscriptionIdentity = z.infer<typeof ClaudeSubscriptionIdentity>;
2244
+ export const ClaudeSubscriptionCredential = z
2245
+ .object({
2246
+ version: z.literal(1),
2247
+ token: z.string().regex(/^sk-ant-oat[0-9]+-\S+$/),
2248
+ identity: ClaudeSubscriptionIdentity,
2249
+ oauth: z
2250
+ .object({
2251
+ refreshToken: z.string().min(1).max(16384),
2252
+ expiresAt: z.string().datetime(),
2253
+ scopes: z.array(z.string().min(1).max(256)).min(1).max(32),
2254
+ })
2255
+ .strict()
2256
+ .optional(),
2257
+ })
2258
+ .strict();
2259
+ const AnthropicProviderOptions = z.object({
2260
+ identity: ClaudeSubscriptionIdentity.optional(),
2261
+ // Native connection provenance only; never included in Anthropic request bodies.
2262
+ credentialBinding: z
2263
+ .object({
2264
+ connectionId: z.string().uuid(),
2265
+ credentialVersion: z.number().int().positive(),
2266
+ })
2267
+ .strict()
2268
+ .optional(),
2269
+ auth: z.enum(["api-key", "oauth"]).default("api-key"),
2270
+ cacheTtl: z.enum(["5m", "1h", "off"]).default("5m"),
2271
+ maxOutputTokens: z.number().int().positive().default(32000),
2272
+ streamIdleTimeoutMs: z.number().int().positive().default(600000),
2273
+ });
2274
+
1936
2275
  const RegistryProviderSchema = z
1937
2276
  .object({
1938
2277
  kind: RegistryProviderKind.default("api-key"),
1939
2278
  id: z.string().min(1).regex(registryId), // stable provider id, e.g. "fireworks"
1940
2279
  label: z.string().min(1).optional(),
1941
2280
  api: ModelProviderApi.default("chat"),
2281
+ anthropic: AnthropicProviderOptions.optional(),
1942
2282
  wireProfile: ModelProviderWireProfile.default("openai"),
1943
2283
  baseUrl: z.string().url(),
1944
2284
  apiKey: z.string().optional(), // inline key (pragmatic) ...
@@ -2026,6 +2366,10 @@ const RESERVED_MODEL_PROVIDER_IDS = new Set<string>([
2026
2366
  OPENROUTER_PROVIDER_ID,
2027
2367
  WORKSPACE_OPENROUTER_PROVIDER_ID,
2028
2368
  ORGANIZATION_OPENROUTER_PROVIDER_ID,
2369
+ "organization-anthropic",
2370
+ "organization-claude-subscription",
2371
+ "workspace-anthropic",
2372
+ "workspace-claude-subscription",
2029
2373
  ]);
2030
2374
 
2031
2375
  export const ModelCostClass = z.enum(["free", "credits"]);
@@ -2167,6 +2511,28 @@ const DeploymentGatewayCatalogModelSchema = GatewayCatalogModel.safeExtend({
2167
2511
  pricing: z.never().optional(),
2168
2512
  }).strict();
2169
2513
 
2514
+ const CodexCatalogModelSchema = RegistryModelSchema.safeExtend({
2515
+ retired: z.boolean().optional(),
2516
+ id: z.string().regex(/^codex\/[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2517
+ upstreamModelId: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/),
2518
+ capabilities: ModelCapabilitiesV1Schema,
2519
+ pricing: z.never().optional(),
2520
+ })
2521
+ .strict()
2522
+ .superRefine((model, context) => {
2523
+ if (model.id !== `${CODEX_MODEL_ID_PREFIX}${model.upstreamModelId}`) {
2524
+ context.addIssue({
2525
+ code: "custom",
2526
+ path: ["id"],
2527
+ message: "Codex product id must match its upstream model slug",
2528
+ });
2529
+ }
2530
+ })
2531
+ .transform((model) => ({
2532
+ ...model,
2533
+ capabilities: reviewedCodexCatalogCapabilities(model.upstreamModelId, model.capabilities),
2534
+ }));
2535
+
2170
2536
  export const ModelCatalogDocument = z
2171
2537
  .object({
2172
2538
  schemaVersion: z.literal(1),
@@ -2174,8 +2540,9 @@ export const ModelCatalogDocument = z
2174
2540
  * fallback for existing documents; operators should set this explicitly
2175
2541
  * when cutting over a registry or connected-subscription default. */
2176
2542
  defaultModel: z.string().min(1).optional(),
2177
- builtInModels: z.array(z.string().min(1)).min(1),
2543
+ builtInModels: z.array(z.string().min(1)),
2178
2544
  registryProviders: z.array(DeploymentRegistryProviderSchema).default([]),
2545
+ codexModels: z.array(CodexCatalogModelSchema).optional(),
2179
2546
  gatewayModels: z.array(DeploymentGatewayCatalogModelSchema).default([]),
2180
2547
  openrouterModels: z.array(OpenRouterCatalogModel).default([]),
2181
2548
  modelNotes: z.record(z.string().min(1), ModelNote).default({}),
@@ -2186,6 +2553,13 @@ export const ModelCatalogDocument = z
2186
2553
  })
2187
2554
  .strict()
2188
2555
  .superRefine((document, context) => {
2556
+ if (document.builtInModels.length === 0 && !document.defaultModel) {
2557
+ context.addIssue({
2558
+ code: "custom",
2559
+ path: ["defaultModel"],
2560
+ message: "catalogs without built-in models require an explicit default model",
2561
+ });
2562
+ }
2189
2563
  const productIds = new Set<string>();
2190
2564
  const providerIds = new Set<string>();
2191
2565
  const gatewayUpstreamIds = new Set<string>();
@@ -2207,8 +2581,22 @@ export const ModelCatalogDocument = z
2207
2581
  productIds.add(id);
2208
2582
  };
2209
2583
  document.builtInModels.forEach((id, index) => add(id, ["builtInModels", index]));
2584
+ document.codexModels?.forEach((model, index) => add(model.id, ["codexModels", index, "id"]));
2585
+ if (
2586
+ document.codexModels?.some((model) => model.retired && model.id === document.defaultModel)
2587
+ ) {
2588
+ context.addIssue({
2589
+ code: "custom",
2590
+ path: ["defaultModel"],
2591
+ message: "a retired model cannot be the deployment default",
2592
+ });
2593
+ }
2210
2594
  document.registryProviders.forEach((provider, providerIndex) => {
2211
- if (RESERVED_MODEL_PROVIDER_IDS.has(provider.id)) {
2595
+ if (
2596
+ RESERVED_MODEL_PROVIDER_IDS.has(provider.id) ||
2597
+ provider.id.startsWith("workspace-openai-") ||
2598
+ provider.id.startsWith("workspace-azure-openai-")
2599
+ ) {
2212
2600
  context.addIssue({
2213
2601
  code: "custom",
2214
2602
  path: ["registryProviders", providerIndex, "id"],
@@ -2256,7 +2644,10 @@ export const ModelCatalogDocument = z
2256
2644
  if (
2257
2645
  document.defaultModel &&
2258
2646
  !productIds.has(document.defaultModel) &&
2259
- !document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX) &&
2647
+ !(
2648
+ document.codexModels === undefined &&
2649
+ document.defaultModel.startsWith(CODEX_MODEL_ID_PREFIX)
2650
+ ) &&
2260
2651
  !document.defaultModel.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX)
2261
2652
  ) {
2262
2653
  context.addIssue({
@@ -2341,6 +2732,8 @@ export function applyModelCatalogDocument(settings: Settings, rawDocument: unkno
2341
2732
  ),
2342
2733
  resolvedGatewayModelsJson: JSON.stringify(document.gatewayModels),
2343
2734
  resolvedOpenRouterModelsJson: JSON.stringify(document.openrouterModels),
2735
+ resolvedCodexModelsJson:
2736
+ document.codexModels === undefined ? undefined : JSON.stringify(document.codexModels),
2344
2737
  modelNotesJson: JSON.stringify(document.modelNotes),
2345
2738
  };
2346
2739
  return resolved;
@@ -2363,6 +2756,7 @@ export type IntegrationOAuthClientConfig = z.infer<typeof IntegrationOAuthClient
2363
2756
  * replacement.
2364
2757
  */
2365
2758
  export interface ResolvedModelProvider {
2759
+ anthropic?: z.infer<typeof AnthropicProviderOptions> | undefined;
2366
2760
  id: string; // "openai" | "azure" | registry id
2367
2761
  label: string;
2368
2762
  kind: RegistryProviderKind | "openrouter-managed";
@@ -2418,6 +2812,26 @@ export interface ConfiguredModel {
2418
2812
  hostedWebSearch: boolean;
2419
2813
  }
2420
2814
 
2815
+ /**
2816
+ * Whether a resolved route is the deployment-funded OpenRouter free tier: the
2817
+ * managed OpenRouter provider, which sends every workspace's requests with the
2818
+ * deployment's one OpenRouter key, serving an upstream `:free` variant (the
2819
+ * curated catalog admits nothing else). OpenRouter limits free-variant
2820
+ * requests per account per minute and per day, so each request on this route
2821
+ * spends quota that every turn on the deployment shares. A workspace or
2822
+ * organization OpenRouter connection uses its own account and is not this
2823
+ * route.
2824
+ */
2825
+ export function isManagedOpenRouterFreeRoute(route: {
2826
+ provider: Pick<ResolvedModelProvider, "kind">;
2827
+ configured: Pick<ConfiguredModel, "upstreamModelId">;
2828
+ }): boolean {
2829
+ return (
2830
+ route.provider.kind === "openrouter-managed" &&
2831
+ route.configured.upstreamModelId.endsWith(":free")
2832
+ );
2833
+ }
2834
+
2421
2835
  export const VERCEL_AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh/v1" as const;
2422
2836
  export const VERCEL_AI_GATEWAY_AI_SDK_BASE_URL = "https://ai-gateway.vercel.sh/v4/ai" as const;
2423
2837
  export const VERCEL_AI_GATEWAY_CONNECTION_DOMAIN = "ai-gateway.vercel.sh" as const;
@@ -2502,12 +2916,10 @@ export const OPENGENI_OPENROUTER_MODELS: readonly OpenRouterCatalogModel[] = [
2502
2916
  capabilities: {
2503
2917
  reasoning: {
2504
2918
  upstream: "supported",
2505
- // OpenRouter advertises the reasoning controls, but the catalogue does
2506
- // not publish this model's accepted effort vocabulary. Preserve that
2507
- // upstream fact without exposing an unverified runnable selector.
2508
- runnable: false,
2509
- efforts: [],
2510
- defaultEffort: null,
2919
+ // OpenRouter /api/v1/models advertises low and medium for this route.
2920
+ runnable: true,
2921
+ efforts: ["low", "medium"],
2922
+ defaultEffort: "medium",
2511
2923
  required: false,
2512
2924
  },
2513
2925
  functionCalling: { upstream: "supported", runnable: true },
@@ -2626,6 +3038,73 @@ export function configuredOpenRouterOrganizationProductModelIds(settings: Settin
2626
3038
  * llm-prices.com as a ground-truth canary; it does not generate this table.
2627
3039
  */
2628
3040
  export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
3041
+ "gpt-6-astra": {
3042
+ default: {
3043
+ // OpenAI list price: $10 / $1 cached / $12.50 cache write / $50 output.
3044
+ inputMicrosPerMillionTokens: 10_000_000,
3045
+ cachedInputMicrosPerMillionTokens: 1_000_000,
3046
+ cacheWriteMicrosPerMillionTokens: 12_500_000,
3047
+ outputMicrosPerMillionTokens: 50_000_000,
3048
+ marginBps: 500,
3049
+ },
3050
+ inputTokenTiers: [
3051
+ {
3052
+ // Prompts with more than 272K input tokens use 2x input and 1.5x output.
3053
+ minimumInputTokens: 272_001,
3054
+ pricing: {
3055
+ inputMicrosPerMillionTokens: 20_000_000,
3056
+ cachedInputMicrosPerMillionTokens: 2_000_000,
3057
+ cacheWriteMicrosPerMillionTokens: 25_000_000,
3058
+ outputMicrosPerMillionTokens: 75_000_000,
3059
+ marginBps: 500,
3060
+ },
3061
+ },
3062
+ ],
3063
+ },
3064
+ "gpt-6-sol": {
3065
+ default: {
3066
+ // OpenAI list price: $2 / $0.20 cached / $2.50 cache write / $10 output.
3067
+ inputMicrosPerMillionTokens: 2_000_000,
3068
+ cachedInputMicrosPerMillionTokens: 200_000,
3069
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3070
+ outputMicrosPerMillionTokens: 10_000_000,
3071
+ marginBps: 500,
3072
+ },
3073
+ inputTokenTiers: [
3074
+ {
3075
+ minimumInputTokens: 272_001,
3076
+ pricing: {
3077
+ inputMicrosPerMillionTokens: 4_000_000,
3078
+ cachedInputMicrosPerMillionTokens: 400_000,
3079
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3080
+ outputMicrosPerMillionTokens: 15_000_000,
3081
+ marginBps: 500,
3082
+ },
3083
+ },
3084
+ ],
3085
+ },
3086
+ "gpt-6-luna": {
3087
+ default: {
3088
+ // OpenAI list price: $0.10 / $0.01 cached / $0.125 cache write / $0.50 output.
3089
+ inputMicrosPerMillionTokens: 100_000,
3090
+ cachedInputMicrosPerMillionTokens: 10_000,
3091
+ cacheWriteMicrosPerMillionTokens: 125_000,
3092
+ outputMicrosPerMillionTokens: 500_000,
3093
+ marginBps: 500,
3094
+ },
3095
+ inputTokenTiers: [
3096
+ {
3097
+ minimumInputTokens: 272_001,
3098
+ pricing: {
3099
+ inputMicrosPerMillionTokens: 200_000,
3100
+ cachedInputMicrosPerMillionTokens: 20_000,
3101
+ cacheWriteMicrosPerMillionTokens: 250_000,
3102
+ outputMicrosPerMillionTokens: 750_000,
3103
+ marginBps: 500,
3104
+ },
3105
+ },
3106
+ ],
3107
+ },
2629
3108
  "gpt-5.6-sol": {
2630
3109
  default: {
2631
3110
  // Promotional OpenAI pricing, guaranteed through at least 2026-11-21.
@@ -2726,6 +3205,113 @@ export const defaultModelPricing: Record<string, ModelPricingScheduleV1> = {
2726
3205
  },
2727
3206
  };
2728
3207
 
3208
+ /** Reviewed comparison rates only; never debit or execution-definition defaults. */
3209
+ export const reviewedModelListPricing: Record<string, ModelPricingScheduleV1> = {
3210
+ // Reviewed 2026-10-03 against https://developers.openai.com/api/docs/pricing
3211
+ // and https://ai-gateway.vercel.sh/v1/models. GPT-6.1 Sol's cache-read rate
3212
+ // is 5% of input, not GPT-6 Sol's 10%. The long-context boundary is >272K.
3213
+ "gpt-6.1-sol": {
3214
+ default: {
3215
+ inputMicrosPerMillionTokens: 2_000_000,
3216
+ cachedInputMicrosPerMillionTokens: 100_000,
3217
+ cacheWriteMicrosPerMillionTokens: 2_500_000,
3218
+ outputMicrosPerMillionTokens: 10_000_000,
3219
+ marginBps: 500,
3220
+ },
3221
+ inputTokenTiers: [
3222
+ {
3223
+ minimumInputTokens: 272_001,
3224
+ pricing: {
3225
+ inputMicrosPerMillionTokens: 4_000_000,
3226
+ cachedInputMicrosPerMillionTokens: 200_000,
3227
+ cacheWriteMicrosPerMillionTokens: 5_000_000,
3228
+ outputMicrosPerMillionTokens: 15_000_000,
3229
+ marginBps: 500,
3230
+ },
3231
+ },
3232
+ ],
3233
+ },
3234
+ // xAI Standard API list rates, reviewed 2026-10-03:
3235
+ // https://docs.x.ai/developers/models/grok-4.5 (and grok-4.6 / grok-4.7).
3236
+ // Preserve the native API's established >=200K boundary. Gateway's separately
3237
+ // configured >200K schedule is not interchangeable with this native schedule.
3238
+ // xAI's automatic caching has no separate cache-write surcharge.
3239
+ ...Object.fromEntries(
3240
+ (
3241
+ [
3242
+ ["grok-4.5", 300_000],
3243
+ ["grok-4.6", 500_000],
3244
+ ["grok-4.7", 500_000],
3245
+ ] as const
3246
+ ).map(([model, cachedInput]) => [
3247
+ model,
3248
+ {
3249
+ default: {
3250
+ inputMicrosPerMillionTokens: 2_000_000,
3251
+ cachedInputMicrosPerMillionTokens: cachedInput,
3252
+ outputMicrosPerMillionTokens: 6_000_000,
3253
+ marginBps: 500,
3254
+ },
3255
+ inputTokenTiers: [
3256
+ {
3257
+ minimumInputTokens: 200_000,
3258
+ pricing: {
3259
+ inputMicrosPerMillionTokens: 4_000_000,
3260
+ cachedInputMicrosPerMillionTokens: cachedInput * 2,
3261
+ outputMicrosPerMillionTokens: 12_000_000,
3262
+ marginBps: 500,
3263
+ },
3264
+ },
3265
+ ],
3266
+ },
3267
+ ]),
3268
+ ),
3269
+ // Every reviewed native Claude profile. Standard/global prices include
3270
+ // 5-minute cache writes; 1-hour native routes are projected separately.
3271
+ // https://platform.claude.com/docs/en/about-claude/pricing (2026-10-03).
3272
+ // Opus 5.5 cache reads are 5%, unlike the older models' 10%; none of these
3273
+ // models has a long-context premium.
3274
+ ...Object.fromEntries(
3275
+ (
3276
+ [
3277
+ ["claude-opus-5-5", 4_000_000, 200_000, 5_000_000, 20_000_000],
3278
+ ["claude-sonnet-5-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3279
+ ["claude-opus-5", 5_000_000, 500_000, 6_250_000, 25_000_000],
3280
+ ["claude-sonnet-5", 2_000_000, 200_000, 2_500_000, 10_000_000],
3281
+ ["claude-opus-4-8", 5_000_000, 500_000, 6_250_000, 25_000_000],
3282
+ ["claude-opus-4-7", 5_000_000, 500_000, 6_250_000, 25_000_000],
3283
+ ["claude-opus-4-6", 5_000_000, 500_000, 6_250_000, 25_000_000],
3284
+ ["claude-sonnet-4-6", 3_000_000, 300_000, 3_750_000, 15_000_000],
3285
+ ["claude-haiku-4-5-20251001", 1_000_000, 100_000, 1_250_000, 5_000_000],
3286
+ ] as const
3287
+ ).map(([model, input, cachedInput, cacheWrite, output]) => [
3288
+ model,
3289
+ {
3290
+ default: {
3291
+ inputMicrosPerMillionTokens: input,
3292
+ cachedInputMicrosPerMillionTokens: cachedInput,
3293
+ cacheWriteMicrosPerMillionTokens: cacheWrite,
3294
+ outputMicrosPerMillionTokens: output,
3295
+ marginBps: 500,
3296
+ },
3297
+ },
3298
+ ]),
3299
+ ),
3300
+ };
3301
+
3302
+ // Explicitly priced free variant, not an unknown rate. List-only metadata
3303
+ // preserves previously accepted free-route execution definitions.
3304
+ // https://openrouter.ai/api/v1/models, verified 2026-10-03.
3305
+ const reviewedFreeOpenRouterListPricing: ModelPricingScheduleV1 = {
3306
+ default: {
3307
+ inputMicrosPerMillionTokens: 0,
3308
+ cachedInputMicrosPerMillionTokens: 0,
3309
+ cacheWriteMicrosPerMillionTokens: 0,
3310
+ outputMicrosPerMillionTokens: 0,
3311
+ marginBps: 500,
3312
+ },
3313
+ };
3314
+
2729
3315
  // --- backend-gated required-credential table (the single source of truth) ---
2730
3316
  // Each sandbox backend declares ONLY its own required credentials: a deployment
2731
3317
  // configured for `sandboxBackend=modal` must carry the Modal token, but a
@@ -2817,6 +3403,9 @@ function optionalEnvironmentValue(name: string, source: NodeJS.ProcessEnv): stri
2817
3403
  return value && value.trim().length > 0 ? value : undefined;
2818
3404
  }
2819
3405
 
3406
+ // getSettings runs per request in some processes; say this once per process.
3407
+ let idleCommandContainmentDisabledWarned = false;
3408
+
2820
3409
  export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2821
3410
  const optional = (name: string): string | undefined => optionalEnvironmentValue(name, source);
2822
3411
  const modelCatalogSource = optional("OPENGENI_MODEL_CATALOG_SOURCE");
@@ -2832,6 +3421,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2832
3421
  optional("GITHUB_SHA"),
2833
3422
  serverVersion: optional("OPENGENI_SERVER_VERSION"),
2834
3423
  databaseUrl: optional("OPENGENI_DATABASE_URL"),
3424
+ apiDatabasePoolMax: optional("OPENGENI_API_DATABASE_POOL_MAX"),
2835
3425
  dbSchema: optional("OPENGENI_DB_SCHEMA"),
2836
3426
  rlsStrategy: optional("OPENGENI_RLS_STRATEGY"),
2837
3427
  runtimeDatabaseRole: optional("OPENGENI_RUNTIME_DATABASE_ROLE"),
@@ -2865,15 +3455,22 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2865
3455
  optional("OPENGENI_OTEL_EXPORTER_OTLP_ENDPOINT") ?? optional("OTEL_EXPORTER_OTLP_ENDPOINT"),
2866
3456
  observabilityOtlpHeaders:
2867
3457
  optional("OPENGENI_OTEL_EXPORTER_OTLP_HEADERS") ?? optional("OTEL_EXPORTER_OTLP_HEADERS"),
3458
+ observabilityDiagnosticsEndpoint: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_ENDPOINT"),
3459
+ observabilityDiagnosticsHeaders: optional("OPENGENI_OBSERVABILITY_DIAGNOSTICS_HEADERS"),
2868
3460
  analyticsEnabled: optional("OPENGENI_ANALYTICS_ENABLED"),
2869
3461
  analyticsConsentRequired: optional("OPENGENI_ANALYTICS_CONSENT_REQUIRED"),
2870
3462
  analyticsReoClientId: optional("OPENGENI_ANALYTICS_REO_CLIENT_ID"),
2871
3463
  analyticsPosthogProjectKey: optional("OPENGENI_ANALYTICS_POSTHOG_PROJECT_KEY"),
2872
3464
  analyticsPosthogHost: optional("OPENGENI_ANALYTICS_POSTHOG_HOST"),
2873
3465
  analyticsGa4MeasurementId: optional("OPENGENI_ANALYTICS_GA4_MEASUREMENT_ID"),
3466
+ legalPrivacyPolicyUrl: optional("OPENGENI_LEGAL_PRIVACY_POLICY_URL"),
3467
+ legalTermsOfServiceUrl: optional("OPENGENI_LEGAL_TERMS_OF_SERVICE_URL"),
3468
+ supportEmail: optional("OPENGENI_SUPPORT_EMAIL"),
2874
3469
  publicBaseUrl: optional("OPENGENI_PUBLIC_BASE_URL"),
3470
+ documentationUrl: optional("OPENGENI_DOCUMENTATION_URL"),
2875
3471
  mcpOauthEnabled: optional("OPENGENI_MCP_OAUTH_ENABLED"),
2876
- mcpOauthTrustedProxyHops: optional("OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS"),
3472
+ apiTrustedProxyHops: optional("OPENGENI_API_TRUSTED_PROXY_HOPS"),
3473
+ apiTrustedProxyCidrs: optional("OPENGENI_API_TRUSTED_PROXY_CIDRS"),
2877
3474
  webBaseUrl: optional("OPENGENI_WEB_BASE_URL"),
2878
3475
  agentReleasesBaseUrl: optional("OPENGENI_AGENT_RELEASES_BASE_URL"),
2879
3476
  agentStableVersion: optional("OPENGENI_AGENT_STABLE_VERSION"),
@@ -2883,8 +3480,10 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2883
3480
  "OPENGENI_ORGANIZATION_TENANCY_CANONICAL_ACTIVATION_ENABLED",
2884
3481
  ),
2885
3482
  billingMode: optional("OPENGENI_BILLING_MODE"),
3483
+ verifiedSignupTrialCreditsEnabled: optional("OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED"),
2886
3484
  entitlementsMode: optional("OPENGENI_ENTITLEMENTS_MODE"),
2887
3485
  usageLimitsMode: optional("OPENGENI_USAGE_LIMITS_MODE"),
3486
+ usageAllowancesEnabled: optional("OPENGENI_USAGE_ALLOWANCES_ENABLED"),
2888
3487
  staticEntitlementsJson: optional("OPENGENI_STATIC_ENTITLEMENTS_JSON"),
2889
3488
  staticUsageLimitsJson: optional("OPENGENI_STATIC_USAGE_LIMITS_JSON"),
2890
3489
  delegationSecret: optional("OPENGENI_DELEGATION_SECRET"),
@@ -2904,13 +3503,16 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2904
3503
  environmentsEncryptionKey: optional("OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY"),
2905
3504
  integrationsEnabled: optional("OPENGENI_INTEGRATIONS_ENABLED"),
2906
3505
  integrationsStateSecret: optional("OPENGENI_INTEGRATIONS_STATE_SECRET"),
3506
+ integrationsOauthShortStateEnabled: optional("OPENGENI_INTEGRATIONS_OAUTH_SHORT_STATE_ENABLED"),
2907
3507
  integrationsAllowPrivateNetworkTargets: optional(
2908
3508
  "OPENGENI_INTEGRATIONS_ALLOW_PRIVATE_NETWORK_TARGETS",
2909
3509
  ),
2910
3510
  integrationsOauthClientsJson: optional("OPENGENI_INTEGRATIONS_OAUTH_CLIENTS_JSON"),
3511
+ gmailWatchTopicName: optional("OPENGENI_GMAIL_WATCH_TOPIC_NAME"),
2911
3512
  slackClientId: optional("OPENGENI_SLACK_CLIENT_ID"),
2912
3513
  slackClientSecret: optional("OPENGENI_SLACK_CLIENT_SECRET"),
2913
3514
  slackSigningSecret: optional("OPENGENI_SLACK_SIGNING_SECRET"),
3515
+ slackAccessMode: optional("OPENGENI_SLACK_ACCESS_MODE"),
2914
3516
  slackBotDisplayName: optional("OPENGENI_SLACK_BOT_DISPLAY_NAME"),
2915
3517
  slackCommand: optional("OPENGENI_SLACK_COMMAND"),
2916
3518
  googleDriveClientId: optional("OPENGENI_GOOGLE_DRIVE_CLIENT_ID"),
@@ -2945,9 +3547,6 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2945
3547
  goalIdleBackoffMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MS"),
2946
3548
  goalIdleBackoffMaxMs: optional("OPENGENI_GOAL_IDLE_BACKOFF_MAX_MS"),
2947
3549
  childLifecycleNoticesEnabled: optional("OPENGENI_CHILD_LIFECYCLE_NOTICES_ENABLED"),
2948
- hostMcpAuthoritySourceAdmissionEnabled: optional(
2949
- "OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED",
2950
- ),
2951
3550
  slackWorkspaceRoutingEnabled: optional("OPENGENI_SLACK_WORKSPACE_ROUTING_ENABLED"),
2952
3551
  agentMaxModelCallsPerTurn: optional("OPENGENI_AGENT_MAX_MODEL_CALLS_PER_TURN"),
2953
3552
  contextWindowTokens: optional("OPENGENI_CONTEXT_WINDOW_TOKENS"),
@@ -2962,10 +3561,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
2962
3561
  authAllowMetrics: optional("OPENGENI_AUTH_ALLOW_METRICS"),
2963
3562
  apiHost: optional("OPENGENI_API_HOST"),
2964
3563
  apiPort: optional("OPENGENI_API_PORT"),
3564
+ apiMetricsPort: optional("OPENGENI_API_METRICS_PORT"),
2965
3565
  workerHttpPort: optional("OPENGENI_WORKER_HTTP_PORT"),
2966
3566
  opengeniMcpInternalUrl: optional("OPENGENI_MCP_INTERNAL_URL"),
2967
3567
  opengeniMcpUrl: optional("OPENGENI_MCP_URL"),
2968
3568
  corsAllowOriginRegex: optional("OPENGENI_CORS_ALLOW_ORIGIN_REGEX"),
3569
+ localAllowedOrigins: optional("OPENGENI_LOCAL_ALLOWED_ORIGINS"),
2969
3570
  openaiProvider: optional("OPENGENI_OPENAI_PROVIDER"),
2970
3571
  openaiApiKey: optional("OPENGENI_OPENAI_API_KEY") ?? optional("OPENAI_API_KEY"),
2971
3572
  openaiBaseUrl: optional("OPENGENI_OPENAI_BASE_URL") ?? optional("OPENAI_BASE_URL"),
@@ -3024,6 +3625,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3024
3625
  modelProvidersJson: optional("OPENGENI_MODEL_PROVIDERS_JSON"),
3025
3626
  codexSubscriptionEnabled: optional("OPENGENI_CODEX_SUBSCRIPTION_ENABLED"),
3026
3627
  supergrokSubscriptionEnabled: optional("OPENGENI_SUPERGROK_SUBSCRIPTION_ENABLED"),
3628
+ claudeSubscriptionEnabled: optional("OPENGENI_CLAUDE_SUBSCRIPTION_ENABLED"),
3027
3629
  supergrokResponseStreamIdleTimeoutMs: optional(
3028
3630
  "OPENGENI_SUPERGROK_RESPONSE_STREAM_IDLE_TIMEOUT_MS",
3029
3631
  ),
@@ -3032,13 +3634,23 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3032
3634
  lazyToolSearchEnabled: optional("OPENGENI_LAZY_TOOL_SEARCH_ENABLED"),
3033
3635
  codexFleetPolicyShadowEnabled: optional("OPENGENI_CODEX_FLEET_POLICY_SHADOW_ENABLED"),
3034
3636
  codexProductSku: optional("OPENGENI_CODEX_PRODUCT_SKU"),
3637
+ reasoningConfigurationUpdatesEnabled: optional(
3638
+ "OPENGENI_REASONING_CONFIGURATION_UPDATES_ENABLED",
3639
+ ),
3035
3640
  openaiReasoningEffort: optional("OPENGENI_OPENAI_REASONING_EFFORT"),
3036
3641
  openaiAllowedReasoningEfforts: optional("OPENGENI_OPENAI_ALLOWED_REASONING_EFFORTS"),
3642
+ creditsDefaultModel: optional("OPENGENI_CREDITS_DEFAULT_MODEL"),
3643
+ creditsDefaultReasoningEffort: optional("OPENGENI_CREDITS_DEFAULT_REASONING_EFFORT"),
3037
3644
  openaiResponsesTransport: optional("OPENGENI_OPENAI_RESPONSES_TRANSPORT"),
3038
3645
  openaiProviderItemIds: optional("OPENGENI_OPENAI_PROVIDER_ITEM_IDS"),
3039
3646
  openaiReasoningEncryptedContent: optional("OPENGENI_OPENAI_REASONING_ENCRYPTED_CONTENT"),
3040
3647
  openaiMaxRetries: optional("OPENGENI_OPENAI_MAX_RETRIES"),
3041
3648
  webSearchEnabled: optional("OPENGENI_WEB_SEARCH_ENABLED"),
3649
+ jevApiKey: optional("OPENGENI_JEV_API_KEY"),
3650
+ jevBaseUrl: optional("OPENGENI_JEV_BASE_URL"),
3651
+ jevModel: optional("OPENGENI_JEV_MODEL"),
3652
+ jevRequestTimeoutMs: optional("OPENGENI_JEV_REQUEST_TIMEOUT_MS"),
3653
+ codeSearchMode: optional("OPENGENI_CODE_SEARCH_MODE"),
3042
3654
  agentInstructionsTemplate: optional("OPENGENI_AGENT_INSTRUCTIONS_TEMPLATE"),
3043
3655
  azureOpenaiBaseUrl: optional("OPENGENI_AZURE_OPENAI_BASE_URL"),
3044
3656
  azureOpenaiEndpoint: optional("OPENGENI_AZURE_OPENAI_ENDPOINT"),
@@ -3055,6 +3667,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3055
3667
  dockerWorkspaceBaseDir: optional("OPENGENI_DOCKER_WORKSPACE_BASE_DIR"),
3056
3668
  modalAppName: optional("OPENGENI_MODAL_APP_NAME"),
3057
3669
  modalImageRef: optional("OPENGENI_MODAL_IMAGE_REF") ?? DEFAULT_MODAL_IMAGE_REF,
3670
+ modalCommandSupervisionEnabled: optional("OPENGENI_MODAL_COMMAND_SUPERVISION_ENABLED"),
3058
3671
  modalImageId: optional("OPENGENI_MODAL_IMAGE_ID"),
3059
3672
  modalImageRegistrySecret: optional("OPENGENI_MODAL_IMAGE_REGISTRY_SECRET"),
3060
3673
  modalTimeoutSeconds: optional("OPENGENI_MODAL_TIMEOUT_SECONDS"),
@@ -3106,6 +3719,9 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3106
3719
  blaxelTtl: optional("OPENGENI_BLAXEL_TTL"),
3107
3720
  cloudflareWorkerUrl: optional("OPENGENI_CLOUDFLARE_WORKER_URL"),
3108
3721
  cloudflareApiKey: optional("OPENGENI_CLOUDFLARE_API_KEY"),
3722
+ experimentalBrowserContextPoolEnabled: optional(
3723
+ "OPENGENI_EXPERIMENTAL_BROWSER_CONTEXT_POOL_ENABLED",
3724
+ ),
3109
3725
  browserbaseApiKey: optional("OPENGENI_BROWSERBASE_API_KEY"),
3110
3726
  kernelApiKey: optional("OPENGENI_KERNEL_API_KEY"),
3111
3727
  kernelEndpoint: optional("OPENGENI_KERNEL_ENDPOINT"),
@@ -3154,6 +3770,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3154
3770
  sandboxViewerHolderTtlMs: optional("OPENGENI_SANDBOX_VIEWER_HOLDER_TTL_MS"),
3155
3771
  sandboxInteractionHolderTtlMs: optional("OPENGENI_SANDBOX_INTERACTION_HOLDER_TTL_MS"),
3156
3772
  sandboxIdleGraceMs: optional("OPENGENI_SANDBOX_IDLE_GRACE_MS"),
3773
+ sandboxIdleCommandContainmentMs: optional("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS"),
3157
3774
  sandboxSnapshotIntervalMs: optional("OPENGENI_SANDBOX_SNAPSHOT_INTERVAL_MS"),
3158
3775
  sandboxSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_SNAPSHOT_TIMEOUT_MS"),
3159
3776
  sandboxDrainSnapshotTimeoutMs: optional("OPENGENI_SANDBOX_DRAIN_SNAPSHOT_TIMEOUT_MS"),
@@ -3167,9 +3784,11 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3167
3784
  sandboxWarmRateMicrosPerSecondJson: optional(
3168
3785
  "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON",
3169
3786
  ),
3787
+ sandboxWarmBillingMode: optional("OPENGENI_SANDBOX_WARM_BILLING_MODE"),
3170
3788
  sandboxMaxWarmSecondsPerWorkspace: optional("OPENGENI_SANDBOX_MAX_WARM_SECONDS_PER_WORKSPACE"),
3171
3789
  sandboxPreparationProfiles: optional("OPENGENI_SANDBOX_PREPARATION_PROFILES"),
3172
3790
  sandboxEnvAllowlist: optional("OPENGENI_SANDBOX_ENV_ALLOWLIST"),
3791
+ sandboxImageAllowlist: optional("OPENGENI_SANDBOX_IMAGE_ALLOWLIST"),
3173
3792
  objectStorageEndpoint: optional("OPENGENI_OBJECT_STORAGE_ENDPOINT"),
3174
3793
  objectStorageInternalEndpoint: optional("OPENGENI_OBJECT_STORAGE_INTERNAL_ENDPOINT"),
3175
3794
  objectStorageSandboxEndpoint: optional("OPENGENI_OBJECT_STORAGE_SANDBOX_ENDPOINT"),
@@ -3196,6 +3815,13 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3196
3815
  documentEmbeddingDimensions: optional("OPENGENI_DOCUMENT_EMBEDDING_DIMENSIONS"),
3197
3816
  documentEmbeddingApiKey: optional("OPENGENI_DOCUMENT_EMBEDDING_API_KEY"),
3198
3817
  documentEmbeddingBaseUrl: optional("OPENGENI_DOCUMENT_EMBEDDING_BASE_URL"),
3818
+ documentEmbeddingBillingMode: optional("OPENGENI_DOCUMENT_EMBEDDING_BILLING_MODE"),
3819
+ documentEmbeddingCreditsActivatedAt: optional(
3820
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT",
3821
+ ),
3822
+ documentEmbeddingRateMicrosPerMillionBytes: optional(
3823
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES",
3824
+ ),
3199
3825
  documentCurationProvider: optional("OPENGENI_DOCUMENT_CURATION_PROVIDER"),
3200
3826
  documentCurationModel: optional("OPENGENI_DOCUMENT_CURATION_MODEL"),
3201
3827
  documentCurationApiKey: optional("OPENGENI_DOCUMENT_CURATION_API_KEY"),
@@ -3230,7 +3856,12 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3230
3856
  managedAuthGoogleClientSecret: optional("OPENGENI_MANAGED_AUTH_GOOGLE_CLIENT_SECRET"),
3231
3857
  managedAuthGithubClientId: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_ID"),
3232
3858
  managedAuthGithubClientSecret: optional("OPENGENI_MANAGED_AUTH_GITHUB_CLIENT_SECRET"),
3859
+ allowedUserEmails:
3860
+ source.OPENGENI_ALLOWED_USER_EMAILS === undefined
3861
+ ? undefined
3862
+ : source.OPENGENI_ALLOWED_USER_EMAILS.split(",").map((email) => email.trim()),
3233
3863
  managedAuthSessionSetMode: optional("OPENGENI_MANAGED_AUTH_SESSION_SET_MODE"),
3864
+ managedAuthNewSignupsEnabled: optional("OPENGENI_MANAGED_AUTH_NEW_SIGNUPS_ENABLED"),
3234
3865
  organizationUserSetupEmailTokenTransport: optional(
3235
3866
  "OPENGENI_ORGANIZATION_USER_SETUP_EMAIL_TOKEN_TRANSPORT",
3236
3867
  ),
@@ -3254,10 +3885,58 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3254
3885
  : parsed.sandboxIdleGraceMs,
3255
3886
  sandboxRotationLeadMs:
3256
3887
  raw.sandboxRotationLeadMs === undefined && parsed.sandboxBackend === "modal"
3257
- ? Math.min(3_600_000, Math.floor((parsed.modalTimeoutSeconds * 1000) / 2))
3888
+ ? Math.min(
3889
+ 3_600_000,
3890
+ Math.max(
3891
+ Math.floor((parsed.modalTimeoutSeconds * 1000) / 2),
3892
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS +
3893
+ sandboxArchiveCaptureTimeoutMs({
3894
+ sandboxSnapshotTimeoutMs: Math.max(
3895
+ parsed.sandboxSnapshotTimeoutMs,
3896
+ parsed.sandboxDrainSnapshotTimeoutMs ?? parsed.sandboxSnapshotTimeoutMs,
3897
+ ),
3898
+ }) +
3899
+ 2 * parsed.sandboxLeaseReaperPeriodMs +
3900
+ 1,
3901
+ ),
3902
+ )
3258
3903
  : parsed.sandboxRotationLeadMs,
3259
3904
  mcpServers: ensureBuiltInMcpServers(parsed),
3260
3905
  };
3906
+ if (raw.sandboxIdleCommandContainmentMs === undefined) {
3907
+ // Strictly between the idle grace and the rotation lead whenever that
3908
+ // interval exists (short test/canary lifetimes), never above 30 minutes,
3909
+ // and early enough that an explicit Modal idle timeout cannot fire first.
3910
+ const modalIdleCeilingMs =
3911
+ settings.sandboxBackend === "modal" && settings.modalIdleTimeoutSeconds !== undefined
3912
+ ? settings.modalIdleTimeoutSeconds * 1000 -
3913
+ settings.sandboxLeaseReaperPeriodMs -
3914
+ sandboxArchiveCaptureTimeoutMs({
3915
+ sandboxSnapshotTimeoutMs: effectiveSandboxDrainSnapshotTimeoutMs(settings),
3916
+ }) -
3917
+ 1
3918
+ : Number.POSITIVE_INFINITY;
3919
+ const derived = Math.min(
3920
+ 1_800_000,
3921
+ Math.floor((settings.sandboxIdleGraceMs + settings.sandboxRotationLeadMs) / 2),
3922
+ modalIdleCeilingMs,
3923
+ );
3924
+ // A derived value never fails validation: with no room above the idle
3925
+ // grace, idle containment stays off and only the deadline rule applies.
3926
+ if (derived > settings.sandboxIdleGraceMs) {
3927
+ settings.sandboxIdleCommandContainmentMs = derived;
3928
+ } else {
3929
+ settings.sandboxIdleCommandContainmentMs = undefined;
3930
+ if (!idleCommandContainmentDisabledWarned) {
3931
+ idleCommandContainmentDisabledWarned = true;
3932
+ console.warn(
3933
+ "[config] idle command containment disabled: no window fits between " +
3934
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}) and the Modal idle ` +
3935
+ "timeout / rotation lead ceiling; set OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS to override.",
3936
+ );
3937
+ }
3938
+ }
3939
+ }
3261
3940
  validateSettings(settings, source);
3262
3941
  return settings;
3263
3942
  }
@@ -3292,9 +3971,13 @@ export type FirstPartyMcpToolPolicy = {
3292
3971
  export function resolveFirstPartyMcpToolPolicy(
3293
3972
  settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
3294
3973
  ): FirstPartyMcpToolPolicy {
3295
- const allowed = settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES];
3974
+ const allowed = currentAgentLearningToolSelection(
3975
+ settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES],
3976
+ ).filter((tool) => !isRetiredNativeAtlassianTool(tool));
3296
3977
  const allowedSet = new Set(allowed);
3297
- const defaults = settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS];
3978
+ const defaults = currentAgentLearningToolSelection(
3979
+ settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
3980
+ );
3298
3981
  return {
3299
3982
  default: defaults.filter((tool) => allowedSet.has(tool)),
3300
3983
  allowed: [...allowed],
@@ -3308,7 +3991,7 @@ export function allowedFirstPartyMcpToolsForSession(
3308
3991
  ): FirstPartyMcpToolNameType[] {
3309
3992
  const policy = resolveFirstPartyMcpToolPolicy(settings);
3310
3993
  const allowed = new Set(policy.allowed);
3311
- const tools = new Set(selected ?? policy.default);
3994
+ const tools = new Set(currentAgentLearningToolSelection(selected ?? policy.default));
3312
3995
  // Existing sessions with pause authority also receive its resume counterpart.
3313
3996
  if (tools.has("goal_pause")) tools.add("goal_resume");
3314
3997
  return [...tools].filter((tool) => allowed.has(tool));
@@ -3983,7 +4666,11 @@ function openRouterRegistryProvider(
3983
4666
 
3984
4667
  function configuredRegistryProviders(settings: Settings): InternalRegistryProvider[] {
3985
4668
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
3986
- const injected: InternalRegistryProvider[] = [...providers];
4669
+ const injected: InternalRegistryProvider[] = providers.filter(
4670
+ (provider) =>
4671
+ settings.claudeSubscriptionEnabled ||
4672
+ !(provider.api === "anthropic-messages" && provider.anthropic?.auth === "oauth"),
4673
+ );
3987
4674
  if (settings.vercelAiGatewayApiKey && configuredGatewayCatalogModels(settings).length > 0) {
3988
4675
  injected.push(
3989
4676
  gatewayRegistryProvider(settings, {
@@ -4002,13 +4689,60 @@ function configuredRegistryProviders(settings: Settings): InternalRegistryProvid
4002
4689
  return injected;
4003
4690
  }
4004
4691
 
4005
- /** Static catalog overlay; it contains no concrete workspace credential. */
4006
- export function withWorkspaceGatewayCatalogProvider(
4692
+ /** Customer-owned OpenAI/Azure routes, bound to immutable connection identity. */
4693
+ export function withDirectModelProviders(
4007
4694
  settings: Settings,
4008
- customModels: readonly {
4009
- upstreamModelId: string;
4010
- label?: string | null;
4011
- }[] = [],
4695
+ connections: readonly {
4696
+ id: string;
4697
+ version: number;
4698
+ subjectId: string | null;
4699
+ kind: string;
4700
+ status: string;
4701
+ providerDomain: string;
4702
+ metadata: Record<string, unknown>;
4703
+ apiKey?: string;
4704
+ }[],
4705
+ ): Settings {
4706
+ const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
4707
+ (provider) =>
4708
+ !provider.id.startsWith("workspace-openai-") &&
4709
+ !provider.id.startsWith("workspace-azure-openai-"),
4710
+ );
4711
+ for (const connection of connections) {
4712
+ const spec = directModelConnectionSpec(connection);
4713
+ if (!spec) continue;
4714
+ providers.push({
4715
+ kind: spec.provider === "openai" ? "direct-openai-workspace" : "direct-azure-workspace",
4716
+ id: spec.providerId,
4717
+ label: spec.provider === "openai" ? "Your OpenAI" : "Your Azure OpenAI",
4718
+ api: "responses",
4719
+ wireProfile: spec.provider === "openai" ? "openai" : "azure-openai",
4720
+ baseUrl: spec.baseUrl,
4721
+ ...(connection.apiKey ? { apiKey: connection.apiKey } : {}),
4722
+ models: [
4723
+ {
4724
+ id: spec.modelId,
4725
+ upstreamModelId: spec.model,
4726
+ label: spec.model,
4727
+ capabilities: legacyModelCapabilities(settings, {
4728
+ reasoningEffort: false,
4729
+ hostedWebSearch: false,
4730
+ }),
4731
+ toolOutputTruncationTokens: settings.modelToolOutputTruncationTokens,
4732
+ },
4733
+ ],
4734
+ });
4735
+ }
4736
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
4737
+ }
4738
+
4739
+ /** Static catalog overlay; it contains no concrete workspace credential. */
4740
+ export function withWorkspaceGatewayCatalogProvider(
4741
+ settings: Settings,
4742
+ customModels: readonly {
4743
+ upstreamModelId: string;
4744
+ label?: string | null;
4745
+ }[] = [],
4012
4746
  ): Settings {
4013
4747
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
4014
4748
  const withoutWorkspace = providers.filter(
@@ -4092,7 +4826,10 @@ export function withWorkspaceOpenRouterCredential(
4092
4826
  /** Secret-free organization Vercel AI Gateway catalog overlay. */
4093
4827
  export function withOrganizationGatewayCatalogProvider(
4094
4828
  settings: Settings,
4095
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4829
+ customModels: readonly {
4830
+ upstreamModelId: string;
4831
+ label?: string | null;
4832
+ }[] = [],
4096
4833
  ): Settings {
4097
4834
  if (customModels.length === 0) return settings;
4098
4835
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
@@ -4108,7 +4845,10 @@ export function withOrganizationGatewayCatalogProvider(
4108
4845
  export function withOrganizationGatewayCredential(
4109
4846
  settings: Settings,
4110
4847
  apiKey: string,
4111
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4848
+ customModels: readonly {
4849
+ upstreamModelId: string;
4850
+ label?: string | null;
4851
+ }[] = [],
4112
4852
  ): Settings {
4113
4853
  if (!apiKey.trim()) throw new Error("organization AI Gateway credential is empty");
4114
4854
  const catalog = withOrganizationGatewayCatalogProvider(settings, customModels);
@@ -4121,7 +4861,10 @@ export function withOrganizationGatewayCredential(
4121
4861
  /** Secret-free organization OpenRouter catalog overlay. */
4122
4862
  export function withOrganizationOpenRouterCatalogProvider(
4123
4863
  settings: Settings,
4124
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4864
+ customModels: readonly {
4865
+ upstreamModelId: string;
4866
+ label?: string | null;
4867
+ }[] = [],
4125
4868
  ): Settings {
4126
4869
  const providers = parseModelProvidersJson(settings.modelProvidersJson).filter(
4127
4870
  (provider) => provider.id !== ORGANIZATION_OPENROUTER_PROVIDER_ID,
@@ -4138,7 +4881,10 @@ export function withOrganizationOpenRouterCatalogProvider(
4138
4881
  export function withOrganizationOpenRouterCredential(
4139
4882
  settings: Settings,
4140
4883
  apiKey: string,
4141
- customModels: readonly { upstreamModelId: string; label?: string | null }[] = [],
4884
+ customModels: readonly {
4885
+ upstreamModelId: string;
4886
+ label?: string | null;
4887
+ }[] = [],
4142
4888
  ): Settings {
4143
4889
  if (!apiKey.trim()) throw new Error("organization OpenRouter credential is empty");
4144
4890
  const catalog = withOrganizationOpenRouterCatalogProvider(settings, customModels);
@@ -4191,8 +4937,8 @@ export function productShortLabelForModelId(modelId: string): string | null {
4191
4937
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4192
4938
  : modelId;
4193
4939
  switch (slug) {
4194
- case "grok-4.6":
4195
- return "4.6";
4940
+ case "grok-4.7":
4941
+ return "4.7";
4196
4942
  case "gpt-5.6-sol":
4197
4943
  return "5.6 Sol";
4198
4944
  case "gpt-5.6-terra":
@@ -4201,6 +4947,10 @@ export function productShortLabelForModelId(modelId: string): string | null {
4201
4947
  return "5.6 Luna";
4202
4948
  case "gpt-6-astra":
4203
4949
  return "6 Astra";
4950
+ case "gpt-6-sol":
4951
+ return "6 Sol";
4952
+ case "gpt-6-luna":
4953
+ return "6 Luna";
4204
4954
  default:
4205
4955
  return null;
4206
4956
  }
@@ -4238,8 +4988,9 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4238
4988
  }> {
4239
4989
  if (
4240
4990
  isBuiltinGpt56ModelId(modelId) ||
4991
+ /^gpt-6(?:\.\d+)?-/u.test(modelId) ||
4241
4992
  modelId.startsWith("codex/gpt-5.6-") ||
4242
- modelId === "codex/gpt-6-astra"
4993
+ /^codex\/gpt-6(?:\.\d+)?-/u.test(modelId)
4243
4994
  ) {
4244
4995
  return [
4245
4996
  { id: "standard", upstream: "supported", runnable: true },
@@ -4254,13 +5005,38 @@ function builtinLatencyModesForModel(modelId: string): Array<{
4254
5005
  return [{ id: "standard", upstream: "unknown", runnable: true }];
4255
5006
  }
4256
5007
 
5008
+ function isReviewedGptVisionModel(slug: string): boolean {
5009
+ return slug.startsWith("gpt-5.6-") || /^gpt-6(?:\.\d+)?-/u.test(slug);
5010
+ }
5011
+
5012
+ /** Repair missing reviewed capabilities in live/stored Codex definitions.
5013
+ * Explicit latency restrictions and every unrelated catalog field remain authoritative.
5014
+ */
5015
+ function reviewedCodexCatalogCapabilities(
5016
+ slug: string,
5017
+ capabilities: ModelCapabilitiesV1,
5018
+ ): ModelCapabilitiesV1 {
5019
+ if (!/^gpt-6(?:\.\d+)?-/u.test(slug)) return capabilities;
5020
+ const fast = builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`).find(
5021
+ (mode) => mode.id === "fast",
5022
+ );
5023
+ return normalizeCapabilities({
5024
+ ...capabilities,
5025
+ inputModalities: [...new Set([...capabilities.inputModalities, "image" as const])],
5026
+ latencyModes:
5027
+ fast && !capabilities.latencyModes.some((mode) => mode.id === "fast")
5028
+ ? [...capabilities.latencyModes, fast]
5029
+ : capabilities.latencyModes,
5030
+ });
5031
+ }
5032
+
4257
5033
  function builtinPromptCachingForModel(
4258
5034
  modelId: string,
4259
5035
  ): NonNullable<ModelCapabilitiesV1["promptCaching"]> | undefined {
4260
5036
  const slug = modelId.startsWith(CODEX_MODEL_ID_PREFIX)
4261
5037
  ? modelId.slice(CODEX_MODEL_ID_PREFIX.length)
4262
5038
  : modelId;
4263
- return slug.startsWith("gpt-5.6-")
5039
+ return slug.startsWith("gpt-5.6-") || slug.startsWith("gpt-6-")
4264
5040
  ? { upstream: "supported", runnable: true, mode: "implicit" }
4265
5041
  : undefined;
4266
5042
  }
@@ -4270,7 +5046,7 @@ function builtinHostedImageGenerationForModel(settings: Settings, modelId: strin
4270
5046
  return (
4271
5047
  settings.openaiProvider === "openai" &&
4272
5048
  isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl) &&
4273
- isBuiltinGpt56ModelId(modelId)
5049
+ (isBuiltinGpt56ModelId(modelId) || modelId.startsWith("gpt-6-"))
4274
5050
  );
4275
5051
  }
4276
5052
 
@@ -4347,9 +5123,22 @@ function assertLatencyModeRunnable(
4347
5123
  ): void {
4348
5124
  const runnable = runnableLatencyModesForModel(settings, modelId);
4349
5125
  if (!runnable.includes(latencyMode)) {
4350
- throw new Error(
4351
- `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${runnable.join(", ")})`,
5126
+ throw new UnsupportedLatencyModeError(modelId, latencyMode, runnable);
5127
+ }
5128
+ }
5129
+
5130
+ export class UnsupportedLatencyModeError extends Error {
5131
+ readonly code = "UNSUPPORTED_LATENCY_MODE";
5132
+
5133
+ constructor(
5134
+ readonly modelId: string,
5135
+ readonly latencyMode: LatencyMode,
5136
+ readonly allowedLatencyModes: readonly LatencyMode[],
5137
+ ) {
5138
+ super(
5139
+ `latency mode ${latencyMode} is not runnable for model ${modelId} (allowed: ${allowedLatencyModes.join(", ")})`,
4352
5140
  );
5141
+ this.name = "UnsupportedLatencyModeError";
4353
5142
  }
4354
5143
  }
4355
5144
 
@@ -4361,10 +5150,17 @@ function registryCredentialSource(provider: InternalRegistryProvider): Credentia
4361
5150
  return { kind: "connected_subscription", provider: "codex" };
4362
5151
  case "xai-subscription":
4363
5152
  return { kind: "connected_subscription", provider: "xai" };
5153
+ case "claude-subscription-workspace":
5154
+ case "claude-subscription-organization":
5155
+ return { kind: "connected_subscription", provider: "claude" };
4364
5156
  case "vercel-gateway-workspace":
5157
+ case "direct-openai-workspace":
5158
+ case "direct-azure-workspace":
4365
5159
  case "openrouter-workspace":
5160
+ case "anthropic-workspace":
4366
5161
  return { kind: "workspace_connection", mechanism: "api_key" };
4367
5162
  case "vercel-gateway-organization":
5163
+ case "anthropic-organization":
4368
5164
  case "openrouter-organization":
4369
5165
  return { kind: "organization_connection", mechanism: "api_key" };
4370
5166
  case "api-key":
@@ -4385,11 +5181,17 @@ function registryBilling(provider: InternalRegistryProvider): BillingAttribution
4385
5181
  return { upstreamPayer: "deployment", metering: "external" };
4386
5182
  case "codex-subscription":
4387
5183
  case "xai-subscription":
5184
+ case "claude-subscription-workspace":
5185
+ case "claude-subscription-organization":
4388
5186
  return { upstreamPayer: "connected_subscription", metering: "external" };
4389
5187
  case "vercel-gateway-workspace":
5188
+ case "direct-openai-workspace":
5189
+ case "direct-azure-workspace":
4390
5190
  case "openrouter-workspace":
5191
+ case "anthropic-workspace":
4391
5192
  return { upstreamPayer: "workspace", metering: "external" };
4392
5193
  case "vercel-gateway-organization":
5194
+ case "anthropic-organization":
4393
5195
  case "openrouter-organization":
4394
5196
  return { upstreamPayer: "organization", metering: "external" };
4395
5197
  case "api-key":
@@ -4428,6 +5230,7 @@ function builtinCredentialSource(settings: Settings): CredentialSourceV1 {
4428
5230
  }
4429
5231
 
4430
5232
  function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
5233
+ anthropic?: ResolvedModelProvider["anthropic"];
4431
5234
  headers: Array<{
4432
5235
  name: string;
4433
5236
  classification: "public" | "secret";
@@ -4442,6 +5245,12 @@ function staticRequestMetadataForDigest(provider: ResolvedModelProvider): {
4442
5245
  const publicHeaders = new Set(provider.publicDefaultHeaderNames ?? []);
4443
5246
  const publicQuery = new Set(provider.publicDefaultQueryNames ?? []);
4444
5247
  return {
5248
+ ...(provider.anthropic
5249
+ ? {
5250
+ anthropic: (({ identity: _identity, credentialBinding: _binding, ...options }) =>
5251
+ options)(provider.anthropic),
5252
+ }
5253
+ : {}),
4445
5254
  headers: Object.entries(provider.defaultHeaders ?? {})
4446
5255
  .sort(([left], [right]) => left.localeCompare(right))
4447
5256
  .map(([name, value]) =>
@@ -4498,6 +5307,7 @@ function definitionVersionFor(
4498
5307
  baseUrl: provider.baseUrl ?? null,
4499
5308
  defaultHeaders: requestMetadata.headers,
4500
5309
  defaultQuery: requestMetadata.query,
5310
+ ...(requestMetadata.anthropic ? { anthropic: requestMetadata.anthropic } : {}),
4501
5311
  },
4502
5312
  credentialSource: model.credentialSource,
4503
5313
  billing: model.billing,
@@ -4527,6 +5337,75 @@ function legacyImplicitOpenAiDefinitionVersionFor(
4527
5337
  });
4528
5338
  }
4529
5339
 
5340
+ function legacyCodexAstraImplicitCachingDefinitionVersionFor(
5341
+ model: ConfiguredModel,
5342
+ provider: ResolvedModelProvider,
5343
+ ): string | null {
5344
+ // The GPT-6 rollout only made the already-implicit Codex cache discoverable.
5345
+ // Bound this compatibility to that product/transport and exact declaration;
5346
+ // never normalize explicit caching, another capability, or another model.
5347
+ if (
5348
+ model.id !== "codex/gpt-6-astra" ||
5349
+ model.upstreamModelId !== "gpt-6-astra" ||
5350
+ provider.id !== CODEX_PROVIDER_ID ||
5351
+ provider.kind !== "codex-subscription" ||
5352
+ provider.api !== "responses" ||
5353
+ provider.wireProfile !== "openai" ||
5354
+ canonicalJson(model.capabilities.promptCaching) !==
5355
+ canonicalJson({ upstream: "supported", runnable: true, mode: "implicit" })
5356
+ ) {
5357
+ return null;
5358
+ }
5359
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5360
+ const { promptCaching: _promptCaching, ...capabilities } = model.capabilities;
5361
+ // Recompute, rather than allowlisting an incident hash: all other current
5362
+ // fields must still reproduce the accepted digest. Do not compose this with
5363
+ // the older wire-profile compatibility or rewrite the accepted policy.
5364
+ return definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider);
5365
+ }
5366
+
5367
+ function matchesAdditiveCapabilityDefinitionVersion(
5368
+ model: ConfiguredModel,
5369
+ provider: ResolvedModelProvider,
5370
+ policy: TurnExecutionPolicyV1,
5371
+ ): boolean {
5372
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5373
+ const { latencyModes, inputModalities } = model.capabilities;
5374
+ // V1 has only three unique latency ids and three unique input modalities:
5375
+ // at most 4 * 7 subset digests retaining the frozen mode. Reconstruct an
5376
+ // exact historical declaration; never ignore the digest or alter existing
5377
+ // mode support, runnable state, billing multiplier, or request-tier routing.
5378
+ // Every other executable field remains in the digest. Do not compose this
5379
+ // with the pre-wire-profile or implicit-caching migration exceptions.
5380
+ for (let latencyMask = 1; latencyMask < 1 << latencyModes.length; latencyMask += 1) {
5381
+ const retainedModes = latencyModes.filter((_mode, index) => latencyMask & (1 << index));
5382
+ if (!retainedModes.some((mode) => mode.id === policy.latencyMode && mode.runnable)) {
5383
+ continue;
5384
+ }
5385
+ for (let inputMask = 1; inputMask < 1 << inputModalities.length; inputMask += 1) {
5386
+ const retainedInputs = inputModalities.filter((_modality, index) => inputMask & (1 << index));
5387
+ if (
5388
+ retainedModes.length === latencyModes.length &&
5389
+ retainedInputs.length === inputModalities.length
5390
+ ) {
5391
+ continue;
5392
+ }
5393
+ const capabilities = {
5394
+ ...model.capabilities,
5395
+ latencyModes: retainedModes,
5396
+ inputModalities: retainedInputs,
5397
+ };
5398
+ if (
5399
+ policy.definitionVersion ===
5400
+ definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider)
5401
+ ) {
5402
+ return true;
5403
+ }
5404
+ }
5405
+ }
5406
+ return false;
5407
+ }
5408
+
4530
5409
  /**
4531
5410
  * The built-in provider's stable id: "openai" on the OpenAI platform, "azure"
4532
5411
  * on Azure. Exported because the workspace model-policy gate must attribute
@@ -4568,12 +5447,13 @@ export function configuredProviders(
4568
5447
  if (settings.openaiProvider === "azure") {
4569
5448
  const baseUrl = settings.azureOpenaiBaseUrl ?? settings.azureOpenaiEndpoint;
4570
5449
  builtin.baseUrl = baseUrl ? normalizeRegistryBaseUrl(baseUrl, builtin.id) : undefined;
4571
- builtin.apiKey = settings.azureOpenaiApiKey ?? settings.azureOpenaiAdToken;
5450
+ builtin.apiKey =
5451
+ usableDeploymentSecret(settings.azureOpenaiApiKey) ?? settings.azureOpenaiAdToken;
4572
5452
  } else {
4573
5453
  builtin.baseUrl = settings.openaiBaseUrl
4574
5454
  ? normalizeRegistryBaseUrl(settings.openaiBaseUrl, builtin.id)
4575
5455
  : undefined;
4576
- builtin.apiKey = settings.openaiApiKey;
5456
+ builtin.apiKey = usableDeploymentSecret(settings.openaiApiKey);
4577
5457
  }
4578
5458
  const registry = configuredRegistryProviders(settings).map(
4579
5459
  (provider): ResolvedModelProvider => ({
@@ -4581,6 +5461,7 @@ export function configuredProviders(
4581
5461
  label: provider.label ?? provider.id,
4582
5462
  kind: provider.kind,
4583
5463
  api: provider.api,
5464
+ anthropic: provider.anthropic,
4584
5465
  wireProfile: provider.wireProfile,
4585
5466
  builtin: false,
4586
5467
  baseUrl: provider.baseUrl,
@@ -4602,11 +5483,30 @@ export function configuredProviders(
4602
5483
  * select, lease, refresh, or expose a concrete credential; those runtime
4603
5484
  * operations remain owned by the credential allocator.
4604
5485
  */
5486
+ /** Exact upstream slugs of the active Codex catalog (or the built-in fallback list). */
5487
+ export function codexUpstreamModelSlugs(settings: Settings): string[] {
5488
+ if (settings.resolvedCodexModelsJson === undefined) return [...CODEX_FALLBACK_MODEL_SLUGS];
5489
+ return z
5490
+ .array(CodexCatalogModelSchema)
5491
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
5492
+ .filter((model) => !model.retired)
5493
+ .map((model) => model.upstreamModelId);
5494
+ }
5495
+
4605
5496
  export function withCodexCatalogProvider(settings: Settings): Settings {
4606
5497
  const providers = parseModelProvidersJson(settings.modelProvidersJson);
4607
5498
  if (providers.some((provider) => provider.id === CODEX_PROVIDER_ID)) {
4608
5499
  return settings;
4609
5500
  }
5501
+ const catalogModels =
5502
+ settings.resolvedCodexModelsJson === undefined
5503
+ ? undefined
5504
+ : z
5505
+ .array(CodexCatalogModelSchema)
5506
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))
5507
+ .filter((model) => !model.retired)
5508
+ .map(({ retired: _retired, ...model }) => model);
5509
+ if (catalogModels?.length === 0) return settings;
4610
5510
  const provider: RegistryProvider = {
4611
5511
  kind: "codex-subscription",
4612
5512
  id: CODEX_PROVIDER_ID,
@@ -4614,40 +5514,42 @@ export function withCodexCatalogProvider(settings: Settings): Settings {
4614
5514
  api: "responses",
4615
5515
  wireProfile: "openai",
4616
5516
  baseUrl: CODEX_PROVIDER_BASE_URL,
4617
- models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
4618
- const capabilities = {
4619
- ...legacyModelCapabilities(settings, {
5517
+ models:
5518
+ catalogModels ??
5519
+ CODEX_FALLBACK_MODEL_SLUGS.map((slug) => {
5520
+ const capabilities = {
5521
+ ...legacyModelCapabilities(settings, {
5522
+ reasoningEffort: true,
5523
+ hostedWebSearch: true,
5524
+ vision: isReviewedGptVisionModel(slug),
5525
+ }),
5526
+ ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
5527
+ ? {
5528
+ promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
5529
+ }
5530
+ : {}),
5531
+ latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
5532
+ };
5533
+ return {
5534
+ id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
5535
+ upstreamModelId: slug,
5536
+ label: productLabelForModelId(slug),
5537
+ ...(productShortLabelForModelId(slug)
5538
+ ? { shortLabel: productShortLabelForModelId(slug)! }
5539
+ : {}),
4620
5540
  reasoningEffort: true,
5541
+ // The ChatGPT/Codex Responses backend accepts the native web_search
5542
+ // hosted tool (unlike hosted apply_patch/computer transports). Declaring
5543
+ // this here makes provider resolution truthful; the worker still applies
5544
+ // the durable session/turn policy gate before attaching it.
4621
5545
  hostedWebSearch: true,
4622
- vision: slug.startsWith("gpt-5.6-") || slug === "gpt-6-astra",
4623
- }),
4624
- ...(builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)
4625
- ? {
4626
- promptCaching: builtinPromptCachingForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`)!,
4627
- }
4628
- : {}),
4629
- latencyModes: builtinLatencyModesForModel(`${CODEX_MODEL_ID_PREFIX}${slug}`),
4630
- };
4631
- return {
4632
- id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
4633
- upstreamModelId: slug,
4634
- label: productLabelForModelId(slug),
4635
- ...(productShortLabelForModelId(slug)
4636
- ? { shortLabel: productShortLabelForModelId(slug)! }
4637
- : {}),
4638
- reasoningEffort: true,
4639
- // The ChatGPT/Codex Responses backend accepts the native web_search
4640
- // hosted tool (unlike hosted apply_patch/computer transports). Declaring
4641
- // this here makes provider resolution truthful; the worker still applies
4642
- // the durable session/turn policy gate before attaching it.
4643
- hostedWebSearch: true,
4644
- capabilities,
4645
- contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
4646
- effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
4647
- autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
4648
- toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
4649
- };
4650
- }),
5546
+ capabilities,
5547
+ contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
5548
+ effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
5549
+ autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
5550
+ toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
5551
+ };
5552
+ }),
4651
5553
  };
4652
5554
  return {
4653
5555
  ...settings,
@@ -4886,7 +5788,7 @@ export function configuredModels(
4886
5788
  reasoningEffort: true,
4887
5789
  hostedWebSearch: settings.webSearchEnabled,
4888
5790
  hostedImageGeneration: builtinHostedImageGenerationForModel(settings, id),
4889
- vision: id.startsWith("gpt-5.6-"),
5791
+ vision: isReviewedGptVisionModel(id),
4890
5792
  }),
4891
5793
  ...(builtinPromptCachingForModel(id)
4892
5794
  ? { promptCaching: builtinPromptCachingForModel(id)! }
@@ -4997,7 +5899,78 @@ export function canonicalizeConfiguredModelId(settings: Settings, modelId: strin
4997
5899
  * openai allow-list, then registry ids.
4998
5900
  */
4999
5901
  export function configuredAllowedModels(settings: Settings): string[] {
5000
- return configuredModels(settings).map((model) => model.id);
5902
+ return configuredModels(settings)
5903
+ .filter((model) => isModelAvailableForNewSelection(settings, model.id))
5904
+ .map((model) => model.id);
5905
+ }
5906
+
5907
+ /** Retirement is selection metadata, never provider authority or executable identity. */
5908
+ export function isModelAvailableForNewSelection(settings: Settings, modelId: string): boolean {
5909
+ if (!modelId.startsWith(CODEX_MODEL_ID_PREFIX) || settings.resolvedCodexModelsJson === undefined)
5910
+ return true;
5911
+ const models = z
5912
+ .array(CodexCatalogModelSchema)
5913
+ .parse(JSON.parse(settings.resolvedCodexModelsJson));
5914
+ return models.some((model) => model.id === modelId && !model.retired);
5915
+ }
5916
+
5917
+ /** Called only with the durable policy installed for a claimed accepted attempt. */
5918
+ export function settingsForAcceptedSubscriptionTurn(
5919
+ settings: Settings,
5920
+ policy: TurnExecutionPolicyV1,
5921
+ expected: {
5922
+ modelId: string;
5923
+ reasoningEffort: Settings["openaiReasoningEffort"];
5924
+ latencyMode?: LatencyMode;
5925
+ },
5926
+ ): Settings {
5927
+ const parsed = TurnExecutionPolicyV1.parse(policy);
5928
+ if (!parsed.productModelId.startsWith(CODEX_MODEL_ID_PREFIX)) return settings;
5929
+ if (!settings.codexSubscriptionEnabled) throw new Error("Codex subscription is disabled");
5930
+ const models =
5931
+ settings.resolvedCodexModelsJson === undefined
5932
+ ? []
5933
+ : z.array(CodexCatalogModelSchema).parse(JSON.parse(settings.resolvedCodexModelsJson));
5934
+ const retained = models.find((model) => model.id === parsed.productModelId && model.retired);
5935
+ if (!retained) {
5936
+ assertTurnExecutionPolicyMatchesConfigV1(settings, parsed, expected);
5937
+ return settings;
5938
+ }
5939
+ const { retired: _retired, ...model } = retained;
5940
+ // Use the existing fixed broker transport. Only this exact definition is added;
5941
+ // selection metadata remains retired in the original catalog JSON.
5942
+ const withoutCodex = {
5943
+ ...settings,
5944
+ modelProvidersJson: JSON.stringify(
5945
+ parseModelProvidersJson(settings.modelProvidersJson).filter(
5946
+ (provider) => provider.id !== CODEX_PROVIDER_ID,
5947
+ ),
5948
+ ),
5949
+ };
5950
+ const providerSettings = withCodexCatalogProvider({
5951
+ ...withoutCodex,
5952
+ resolvedCodexModelsJson: JSON.stringify([model]),
5953
+ });
5954
+ const retainedProvider = parseModelProvidersJson(providerSettings.modelProvidersJson).find(
5955
+ (provider) => provider.id === CODEX_PROVIDER_ID,
5956
+ )!;
5957
+ const providers = parseModelProvidersJson(settings.modelProvidersJson);
5958
+ const current = providers.find((provider) => provider.id === CODEX_PROVIDER_ID);
5959
+ const restored = {
5960
+ ...settings,
5961
+ modelProvidersJson: JSON.stringify([
5962
+ ...providers.filter((provider) => provider.id !== CODEX_PROVIDER_ID),
5963
+ {
5964
+ ...retainedProvider,
5965
+ models: [
5966
+ ...(current?.models ?? []).filter((candidate) => candidate.id !== model.id),
5967
+ model,
5968
+ ],
5969
+ },
5970
+ ]),
5971
+ };
5972
+ assertTurnExecutionPolicyMatchesConfigV1(restored, parsed, expected);
5973
+ return restored;
5001
5974
  }
5002
5975
 
5003
5976
  /**
@@ -5102,6 +6075,9 @@ export function resolveTurnExecutionPolicyV1(
5102
6075
  ): TurnExecutionPolicyV1 {
5103
6076
  const catalogSettings = settingsForTurnExecutionPolicy(settings, input.modelId);
5104
6077
  const productModelId = canonicalizeConfiguredModelId(catalogSettings, input.modelId);
6078
+ if (!isModelAvailableForNewSelection(catalogSettings, productModelId)) {
6079
+ throw new Error("Turn execution policy model is retired from new selection");
6080
+ }
5105
6081
  const resolved = resolveModelProvider(catalogSettings, productModelId);
5106
6082
  if (!resolved) {
5107
6083
  throw new Error("Turn execution policy model is not present in the configured catalog");
@@ -5133,10 +6109,20 @@ export function resolveTurnExecutionPolicyV1(
5133
6109
  });
5134
6110
  }
5135
6111
 
6112
+ /** Explicit accepted execution identity matches, but its definition digest differs. */
6113
+ export class TurnExecutionPolicyDefinitionMismatchError extends Error {
6114
+ readonly code = "turn_execution_policy_definition_mismatch";
6115
+
6116
+ constructor() {
6117
+ super("Turn execution policy does not match the current provider definition");
6118
+ this.name = "TurnExecutionPolicyDefinitionMismatchError";
6119
+ }
6120
+ }
6121
+
5136
6122
  /**
5137
6123
  * Parse-time validation lives in @opengeni/contracts; this verifier binds a
5138
6124
  * present snapshot to the current executable definition and exact turn row.
5139
- * Any deployment/provider drift fails before a provider or compaction call.
6125
+ * Non-additive executable drift fails before a provider or compaction call.
5140
6126
  */
5141
6127
  export function assertTurnExecutionPolicyMatchesConfigV1(
5142
6128
  settings: Settings,
@@ -5176,6 +6162,39 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5176
6162
  if (!resolved) {
5177
6163
  throw new Error("Turn execution policy model is no longer configured");
5178
6164
  }
6165
+ // The individual-account cutover corrects subscription accounting labels.
6166
+ // Preserve an already accepted legacy policy only if its complete original
6167
+ // executable digest matches; this exception cannot compose with other drift.
6168
+ const legacyClaudeScope =
6169
+ resolved.provider.kind === "claude-subscription-workspace"
6170
+ ? "workspace"
6171
+ : resolved.provider.kind === "claude-subscription-organization"
6172
+ ? "organization"
6173
+ : null;
6174
+ const legacyClaudeAccountingMatches =
6175
+ legacyClaudeScope !== null &&
6176
+ parsed.providerId === resolved.provider.id &&
6177
+ parsed.upstreamModelId === resolved.model.upstreamModelId &&
6178
+ parsed.wireApi === resolved.model.api &&
6179
+ canonicalJson(parsed.credentialSource) ===
6180
+ canonicalJson({
6181
+ kind: `${legacyClaudeScope}_connection`,
6182
+ mechanism: "api_key",
6183
+ }) &&
6184
+ canonicalJson(parsed.billing) ===
6185
+ canonicalJson({
6186
+ upstreamPayer: legacyClaudeScope,
6187
+ metering: "external",
6188
+ }) &&
6189
+ parsed.definitionVersion ===
6190
+ definitionVersionFor(
6191
+ {
6192
+ ...resolved.model,
6193
+ credentialSource: parsed.credentialSource,
6194
+ billing: parsed.billing,
6195
+ },
6196
+ resolved.provider,
6197
+ );
5179
6198
  // wireProfile was added to the definition digest after policies already
5180
6199
  // existed in durable in-flight turns. An omitted profile meant exactly
5181
6200
  // "openai", so accept that one legacy digest only; Azure and every other
@@ -5186,17 +6205,24 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
5186
6205
  );
5187
6206
  const definitionVersionMatches =
5188
6207
  parsed.definitionVersion === resolved.model.definitionVersion ||
5189
- parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion;
5190
- const mismatched =
6208
+ parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion ||
6209
+ parsed.definitionVersion ===
6210
+ legacyCodexAstraImplicitCachingDefinitionVersionFor(resolved.model, resolved.provider) ||
6211
+ matchesAdditiveCapabilityDefinitionVersion(resolved.model, resolved.provider, parsed);
6212
+ const identityMismatched =
5191
6213
  parsed.providerId !== resolved.provider.id ||
5192
6214
  parsed.upstreamModelId !== resolved.model.upstreamModelId ||
5193
6215
  parsed.wireApi !== resolved.model.api ||
5194
- !definitionVersionMatches ||
5195
6216
  canonicalJson(parsed.credentialSource) !== canonicalJson(resolved.model.credentialSource) ||
5196
6217
  canonicalJson(parsed.billing) !== canonicalJson(resolved.model.billing);
5197
- if (mismatched) {
6218
+ // Identity/source changes must never enter a rollout-retry classification,
6219
+ // even when their definition digest also differs.
6220
+ if (identityMismatched && !legacyClaudeAccountingMatches) {
5198
6221
  throw new Error("Turn execution policy does not match the current provider definition");
5199
6222
  }
6223
+ if (!definitionVersionMatches && !legacyClaudeAccountingMatches) {
6224
+ throw new TurnExecutionPolicyDefinitionMismatchError();
6225
+ }
5200
6226
  return { policy: parsed, provider: resolved.provider, model: resolved.model };
5201
6227
  }
5202
6228
 
@@ -5236,6 +6262,154 @@ export function configuredModelPricingSchedules(
5236
6262
  };
5237
6263
  }
5238
6264
 
6265
+ /**
6266
+ * Insights list-price metadata, including reviewed namespaced provider routes.
6267
+ * Kept separate from debit/catalog pricing: adding a comparison must not change
6268
+ * an accepted turn's frozen execution-definition hash or payer classification.
6269
+ * Inline registry and explicit product-ID prices always win over projections.
6270
+ */
6271
+ export function configuredModelListPricingSchedules(
6272
+ settings: Settings,
6273
+ ): Record<string, ModelPricingScheduleV1> {
6274
+ const prices = configuredModelPricingSchedules(settings);
6275
+ const configured = Object.fromEntries(
6276
+ Object.entries(parseModelPricingJson(settings.modelPricingJson)).map(([model, pricing]) => [
6277
+ model,
6278
+ normalizeModelPricingSchedule(pricing),
6279
+ ]),
6280
+ );
6281
+ // Bare new IDs need the actual configured public OpenAI route, not merely a
6282
+ // familiar model name. Azure/custom endpoints do not inherit these API rates.
6283
+ if (
6284
+ settings.openaiProvider === "openai" &&
6285
+ isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl ?? "https://api.openai.com/v1")
6286
+ ) {
6287
+ for (const model of configuredModels(settings)) {
6288
+ if (
6289
+ model.providerId !== builtinProviderId(settings) ||
6290
+ !model.upstreamModelId.startsWith("gpt-") ||
6291
+ prices[model.id] !== undefined
6292
+ )
6293
+ continue;
6294
+ const reviewed = reviewedModelListPricing[model.upstreamModelId];
6295
+ if (reviewed) prices[model.id] = reviewed;
6296
+ }
6297
+ }
6298
+ for (const provider of configuredRegistryProviders(settings)) {
6299
+ for (const model of provider.models) {
6300
+ if (prices[model.id] !== undefined) continue;
6301
+ const reviewed = reviewedProviderModelPricing(settings, provider, model, configured);
6302
+ if (reviewed) prices[model.id] = reviewed;
6303
+ }
6304
+ }
6305
+ return prices;
6306
+ }
6307
+
6308
+ /**
6309
+ * A reviewed upstream list rate may also describe a namespaced product route.
6310
+ * Never infer a rate by stripping arbitrary prefixes, matching labels, or
6311
+ * treating every OpenAI-compatible endpoint as the original provider.
6312
+ * This is pricing metadata only: credential selection and payer/metering are
6313
+ * still derived exclusively from the accepted execution policy.
6314
+ */
6315
+ function reviewedProviderModelPricing(
6316
+ settings: Settings,
6317
+ provider: InternalRegistryProvider,
6318
+ model: RegistryProvider["models"][number],
6319
+ configured: Record<string, ModelPricingScheduleV1>,
6320
+ ): ModelPricingScheduleV1 | undefined {
6321
+ const upstream = model.upstreamModelId ?? model.id;
6322
+ let priceId: string | undefined;
6323
+ let nativeClaude = false;
6324
+ switch (provider.kind) {
6325
+ case "anonymous":
6326
+ if (
6327
+ provider.baseUrl.replace(/\/$/u, "") === OPENROUTER_BASE_URL &&
6328
+ upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)
6329
+ ) {
6330
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6331
+ }
6332
+ break;
6333
+ case "codex-subscription":
6334
+ if (upstream.startsWith("gpt-")) priceId = upstream;
6335
+ break;
6336
+ case "xai-subscription":
6337
+ if (upstream.startsWith("grok-")) priceId = upstream;
6338
+ break;
6339
+ case "anthropic-workspace":
6340
+ case "anthropic-organization":
6341
+ case "claude-subscription-workspace":
6342
+ case "claude-subscription-organization":
6343
+ if (claudeNativeModelProfile(upstream)) {
6344
+ priceId = upstream;
6345
+ nativeClaude = true;
6346
+ }
6347
+ break;
6348
+ case "direct-openai-workspace":
6349
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6350
+ priceId = upstream;
6351
+ }
6352
+ break;
6353
+ case "vercel-gateway-managed":
6354
+ case "vercel-gateway-workspace":
6355
+ case "vercel-gateway-organization":
6356
+ // Only curated, provider-pinned routes use the conservative fallback.
6357
+ // Arbitrary Gateway custom models still require exact reported cost or
6358
+ // an explicit operator rate, even if their slugs resemble a known model.
6359
+ priceId = configuredGatewayCatalogModels(settings).find(
6360
+ (candidate) =>
6361
+ candidate.upstreamModelId === upstream &&
6362
+ Object.values(OPENGENI_GATEWAY_MODELS).some(
6363
+ (reviewed) => reviewed.upstreamModelId === candidate.upstreamModelId,
6364
+ ),
6365
+ )?.productId;
6366
+ break;
6367
+ case "openrouter-workspace":
6368
+ case "openrouter-organization":
6369
+ case "openrouter-managed":
6370
+ if (upstream === DEFAULT_OPENROUTER_MODEL_ID.slice(OPENROUTER_MODEL_ID_PREFIX.length)) {
6371
+ priceId = DEFAULT_OPENROUTER_MODEL_ID;
6372
+ }
6373
+ break;
6374
+ case "api-key":
6375
+ // Explicit deployments of the official APIs may use their own product
6376
+ // IDs. Custom gateways/proxies and Azure SKUs do not inherit API rates.
6377
+ if (isDirectOpenAiApiBaseUrl(provider.baseUrl) && upstream.startsWith("gpt-")) {
6378
+ priceId = upstream;
6379
+ } else if (provider.baseUrl.replace(/\/$/u, "") === "https://api.x.ai/v1") {
6380
+ if (upstream.startsWith("grok-")) priceId = upstream;
6381
+ } else if (
6382
+ provider.api === "anthropic-messages" &&
6383
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1" &&
6384
+ claudeNativeModelProfile(upstream)
6385
+ ) {
6386
+ priceId = upstream;
6387
+ nativeClaude = true;
6388
+ }
6389
+ break;
6390
+ default:
6391
+ return undefined;
6392
+ }
6393
+ if (!priceId) return undefined;
6394
+ // Explicit upstream overrides retain the existing Codex comparison behavior;
6395
+ // a product-ID override or inline registry rate still has higher precedence.
6396
+ if (configured[priceId]) return configured[priceId];
6397
+ const schedule =
6398
+ reviewedModelListPricing[priceId] ??
6399
+ defaultModelPricing[priceId] ??
6400
+ (priceId === DEFAULT_OPENROUTER_MODEL_ID ? reviewedFreeOpenRouterListPricing : undefined);
6401
+ if (!schedule) return undefined;
6402
+ if (!nativeClaude || provider.anthropic?.cacheTtl !== "1h") return schedule;
6403
+ // Anthropic's 1-hour cache writes are 2x base input, rather than 5m's 1.25x.
6404
+ return {
6405
+ ...schedule,
6406
+ default: {
6407
+ ...schedule.default,
6408
+ cacheWriteMicrosPerMillionTokens: schedule.default.inputMicrosPerMillionTokens * 2,
6409
+ },
6410
+ };
6411
+ }
6412
+
5239
6413
  /** Legacy flat projection: returns the default/below-threshold price. */
5240
6414
  export function configuredModelPricing(settings: Settings): Record<string, ModelPricing> {
5241
6415
  return Object.fromEntries(
@@ -5352,61 +6526,369 @@ export function calculateModelUsageCostBreakdown(
5352
6526
  options?: { latencyMode?: LatencyMode },
5353
6527
  ): ModelUsageCostBreakdown {
5354
6528
  const schedule = configuredModelPricingSchedules(settings)[model];
5355
- if (!schedule) {
5356
- throw new Error(`Missing model pricing for ${model}`);
5357
- }
5358
- const entries =
5359
- usage.requestUsageEntries && usage.requestUsageEntries.length > 0
5360
- ? usage.requestUsageEntries
5361
- : [usage];
6529
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6530
+ }
6531
+
6532
+ /** Provider-list/equivalent-credit comparison only; never debit authority. */
6533
+ export function calculateModelListUsageCostBreakdown(
6534
+ settings: Settings,
6535
+ model: string,
6536
+ usage: ModelUsageInput,
6537
+ options?: { latencyMode?: LatencyMode },
6538
+ ): ModelUsageCostBreakdown {
6539
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6540
+ return calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6541
+ }
6542
+
6543
+ /**
6544
+ * Forward-only list comparison snapshot for the model-call fact writer.
6545
+ * Callers must establish request price provenance (including geography/service
6546
+ * tier) and retain per-request counters. Unknown counters/TTL/modifiers yield
6547
+ * no class split, not invented zeros. Historical facts must not use this helper.
6548
+ * Native Claude details preserve cache_write_tokens_5m and cache_write_tokens_1h.
6549
+ */
6550
+ export function calculateModelListUsageCostSnapshot(
6551
+ settings: Settings,
6552
+ model: string,
6553
+ usage: ModelUsageInput,
6554
+ options?: { latencyMode?: LatencyMode; priceContextKnown?: boolean },
6555
+ ): ModelListUsageCostSnapshot {
6556
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6557
+ const fallback = calculateUsageCostBreakdown(settings, model, usage, schedule, options);
6558
+ const unknown = { ...fallback, listByClassMicros: null, listByClassApprox: false };
6559
+ if (!schedule || !options?.priceContextKnown) return unknown;
6560
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
6561
+ const classes: ModelListCostClassesMicros = {
6562
+ uncachedInput: 0,
6563
+ cacheRead: 0,
6564
+ cacheWrite: 0,
6565
+ output: 0,
6566
+ };
5362
6567
  const rawCostByPricing = new Map<ModelPricing, number>();
6568
+ const native = nativeClaudeListWriteContext(settings, model);
5363
6569
  for (const entry of entries) {
5364
- const pricing = selectModelPricing(schedule, positiveInt(entry.inputTokens));
5365
- rawCostByPricing.set(
5366
- pricing,
5367
- (rawCostByPricing.get(pricing) ?? 0) + calculateEntryCostMicros(pricing, entry),
5368
- );
6570
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
6571
+ return unknown;
6572
+ const cached = knownDetailTokenCounter(entry, [
6573
+ "cached_tokens",
6574
+ "cachedInputTokens",
6575
+ "cached_input_tokens",
6576
+ ]);
6577
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
6578
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
6579
+ return unknown;
6580
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
6581
+ if (cached > 0 && pricing.cachedInputMicrosPerMillionTokens === undefined) return unknown;
6582
+ if (writes > 0 && pricing.cacheWriteMicrosPerMillionTokens === undefined) return unknown;
6583
+ const costs: ModelListCostClassesMicros = {
6584
+ uncachedInput: Math.ceil(
6585
+ ((entry.inputTokens - cached - writes) * pricing.inputMicrosPerMillionTokens) / 1_000_000,
6586
+ ),
6587
+ cacheRead: Math.ceil((cached * (pricing.cachedInputMicrosPerMillionTokens ?? 0)) / 1_000_000),
6588
+ cacheWrite: Math.ceil((writes * (pricing.cacheWriteMicrosPerMillionTokens ?? 0)) / 1_000_000),
6589
+ output: Math.ceil((entry.outputTokens * pricing.outputMicrosPerMillionTokens) / 1_000_000),
6590
+ };
6591
+ if (native && writes > 0) {
6592
+ const fiveMinute = knownDetailTokenCounter(entry, ["cache_write_tokens_5m"]);
6593
+ const oneHour = knownDetailTokenCounter(entry, ["cache_write_tokens_1h"]);
6594
+ if (fiveMinute === undefined || oneHour === undefined || fiveMinute + oneHour !== writes)
6595
+ return unknown;
6596
+ if (native.publishedRates) {
6597
+ costs.cacheWrite =
6598
+ Math.ceil((fiveMinute * native.publishedRates.fiveMinute) / 1_000_000) +
6599
+ Math.ceil((oneHour * native.publishedRates.oneHour) / 1_000_000);
6600
+ } else if (
6601
+ !(
6602
+ (native.declaredTtl === "5m" && oneHour === 0) ||
6603
+ (native.declaredTtl === "1h" && fiveMinute === 0)
6604
+ )
6605
+ ) {
6606
+ // A single explicit override cannot establish two different TTL prices.
6607
+ return unknown;
6608
+ }
6609
+ }
6610
+ const raw = sumListCostClasses(costs);
6611
+ rawCostByPricing.set(pricing, (rawCostByPricing.get(pricing) ?? 0) + raw);
6612
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) classes[key] += costs[key];
5369
6613
  }
5370
- let providerCostMicros = 0;
6614
+ let providerCostMicros = sumListCostClasses(classes);
5371
6615
  let creditCostMicros = 0;
5372
- for (const [pricing, rawCost] of rawCostByPricing) {
5373
- const marginBps = pricing.marginBps ?? 0;
5374
- providerCostMicros += rawCost;
5375
- creditCostMicros += Math.ceil((rawCost * (10_000 + marginBps)) / 10_000);
6616
+ for (const [pricing, raw] of rawCostByPricing) {
6617
+ creditCostMicros += Math.ceil((raw * (10_000 + (pricing.marginBps ?? 0))) / 10_000);
5376
6618
  }
5377
- const latencyMode = options?.latencyMode ?? "standard";
6619
+ let listByClassApprox = false;
6620
+ const latencyMode = options.latencyMode ?? "standard";
5378
6621
  if (latencyMode !== "standard") {
5379
6622
  const catalogSettings = settingsForTurnExecutionPolicy(settings, model);
5380
6623
  const resolved = resolveModelProvider(
5381
6624
  catalogSettings,
5382
6625
  canonicalizeConfiguredModelId(catalogSettings, model),
5383
6626
  );
5384
- const multiplierBps = resolved?.model.capabilities.latencyModes.find(
6627
+ const multiplier = resolved?.model.capabilities.latencyModes.find(
5385
6628
  (mode) => mode.id === latencyMode && mode.runnable,
5386
6629
  )?.billingMultiplierBps;
5387
- if (multiplierBps && multiplierBps > 0) {
5388
- providerCostMicros = Math.ceil((providerCostMicros * multiplierBps) / 10_000);
5389
- creditCostMicros = Math.ceil((creditCostMicros * multiplierBps) / 10_000);
5390
- }
6630
+ if (multiplier === undefined || multiplier <= 0) return unknown;
6631
+ providerCostMicros = Math.ceil((providerCostMicros * multiplier) / 10_000);
6632
+ creditCostMicros = Math.ceil((creditCostMicros * multiplier) / 10_000);
6633
+ const scaled = allocateLatencyListCostClasses(classes, multiplier, providerCostMicros);
6634
+ if (!scaled) return unknown;
6635
+ Object.assign(classes, scaled.classes);
6636
+ listByClassApprox = scaled.approximate;
5391
6637
  }
5392
- return { providerCostMicros, creditCostMicros };
6638
+ if (![providerCostMicros, creditCostMicros, ...Object.values(classes)].every(knownTokenCounter))
6639
+ return unknown;
6640
+ return { providerCostMicros, creditCostMicros, listByClassMicros: classes, listByClassApprox };
5393
6641
  }
5394
6642
 
5395
6643
  /**
5396
- * Convert AI Gateway's exact USD inference cost to OpenGeni credit micros and
5397
- * apply the configured model margin. Decimal arithmetic is integer-only so a
5398
- * sub-micro provider charge cannot be lost to floating-point rounding.
6644
+ * Approximate class attribution of a previously recorded upstream list total.
6645
+ * Current reviewed rates are weights only: this never recomputes that total,
6646
+ * credits, or charges. Missing class counters/rates remain unknown. Historical
6647
+ * cache writes use the schedule's single TTL rate, not an exact TTL assertion.
6648
+ * Reasoning is already included in outputTokens and is never added again.
5399
6649
  */
5400
- export function calculateGatewayReportedCostMicros(
6650
+ export function allocateRecordedModelListCostByClass(
5401
6651
  settings: Settings,
5402
6652
  model: string,
5403
- inferenceCostUsd: string,
5404
- options?: { inputTokens?: number },
5405
- ): number {
5406
- return calculateGatewayReportedCostBreakdown(settings, model, inferenceCostUsd, options)
5407
- .creditCostMicros;
5408
- }
5409
-
6653
+ usage: ModelUsageInput,
6654
+ recordedProviderCostMicros: number | null | undefined,
6655
+ ): ModelRecordedListCostAllocation {
6656
+ const unknown: ModelRecordedListCostAllocation = {
6657
+ listByClassMicros: null,
6658
+ listByClassApprox: false,
6659
+ };
6660
+ if (!knownTokenCounter(recordedProviderCostMicros)) return unknown;
6661
+ const schedule = configuredModelListPricingSchedules(settings)[model];
6662
+ if (!schedule) return unknown;
6663
+ const weights = { uncachedInput: 0n, cacheRead: 0n, cacheWrite: 0n, output: 0n };
6664
+ const entries = usage.requestUsageEntries?.length ? usage.requestUsageEntries : [usage];
6665
+ for (const entry of entries) {
6666
+ if (!knownTokenCounter(entry.inputTokens) || !knownTokenCounter(entry.outputTokens))
6667
+ return unknown;
6668
+ const cached = knownDetailTokenCounter(entry, [
6669
+ "cached_tokens",
6670
+ "cachedInputTokens",
6671
+ "cached_input_tokens",
6672
+ ]);
6673
+ const writes = knownDetailTokenCounter(entry, ["cache_write_tokens", "cacheWriteTokens"], true);
6674
+ if (cached === undefined || writes === undefined || cached + writes > entry.inputTokens)
6675
+ return unknown;
6676
+ const pricing = selectModelPricing(schedule, entry.inputTokens);
6677
+ const counters = {
6678
+ uncachedInput: entry.inputTokens - cached - writes,
6679
+ cacheRead: cached,
6680
+ cacheWrite: writes,
6681
+ output: entry.outputTokens,
6682
+ };
6683
+ const rates = {
6684
+ uncachedInput: pricing.inputMicrosPerMillionTokens,
6685
+ cacheRead: pricing.cachedInputMicrosPerMillionTokens,
6686
+ cacheWrite: pricing.cacheWriteMicrosPerMillionTokens,
6687
+ output: pricing.outputMicrosPerMillionTokens,
6688
+ };
6689
+ for (const key of MODEL_LIST_COST_CLASS_KEYS) {
6690
+ if (counters[key] === 0) continue;
6691
+ const rate = rates[key];
6692
+ if (!knownTokenCounter(rate)) return unknown;
6693
+ // Do not round or convert weights to Number: both can distort ratios.
6694
+ weights[key] += BigInt(counters[key]) * BigInt(rate);
6695
+ }
6696
+ }
6697
+ const classes = allocateRecordedListCostWeights(weights, recordedProviderCostMicros);
6698
+ return classes ? { listByClassMicros: classes, listByClassApprox: true } : unknown;
6699
+ }
6700
+
6701
+ const MODEL_LIST_COST_CLASS_KEYS = ["uncachedInput", "cacheRead", "cacheWrite", "output"] as const;
6702
+
6703
+ function sumListCostClasses(costs: ModelListCostClassesMicros): number {
6704
+ return MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + costs[key], 0);
6705
+ }
6706
+
6707
+ /** Integer-only largest remainder, preserving the supplied historical total. */
6708
+ function allocateRecordedListCostWeights(
6709
+ weights: Record<(typeof MODEL_LIST_COST_CLASS_KEYS)[number], bigint>,
6710
+ target: number,
6711
+ ): ModelListCostClassesMicros | undefined {
6712
+ const denominator = MODEL_LIST_COST_CLASS_KEYS.reduce((total, key) => total + weights[key], 0n);
6713
+ if (denominator === 0n) {
6714
+ return target === 0 ? { uncachedInput: 0, cacheRead: 0, cacheWrite: 0, output: 0 } : undefined;
6715
+ }
6716
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
6717
+ const numerator = BigInt(target) * weights[key];
6718
+ return { key, index, value: numerator / denominator, remainder: numerator % denominator };
6719
+ });
6720
+ const remaining = BigInt(target) - rows.reduce((total, row) => total + row.value, 0n);
6721
+ const ranked = [...rows].sort((a, b) =>
6722
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
6723
+ );
6724
+ for (let index = 0; index < Number(remaining); index++) ranked[index]!.value += 1n;
6725
+ return Object.fromEntries(
6726
+ rows.map((row) => [row.key, Number(row.value)]),
6727
+ ) as ModelListCostClassesMicros;
6728
+ }
6729
+
6730
+ function knownTokenCounter(value: unknown): value is number {
6731
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
6732
+ }
6733
+
6734
+ function knownDetailTokenCounter(
6735
+ entry: ModelUsageInput,
6736
+ keys: readonly string[],
6737
+ firstOnly = false,
6738
+ ): number | undefined {
6739
+ const details = Array.isArray(entry.inputTokensDetails)
6740
+ ? entry.inputTokensDetails
6741
+ : entry.inputTokensDetails
6742
+ ? [entry.inputTokensDetails]
6743
+ : [];
6744
+ if (details.length === 0) return undefined;
6745
+ let total = 0;
6746
+ for (const detail of details) {
6747
+ const present = keys.filter((key) => detail[key] !== undefined);
6748
+ if (present.length === 0) return undefined;
6749
+ for (const key of firstOnly ? present.slice(0, 1) : present) {
6750
+ if (!knownTokenCounter(detail[key])) return undefined;
6751
+ total += detail[key]!;
6752
+ }
6753
+ }
6754
+ return knownTokenCounter(total) ? total : undefined;
6755
+ }
6756
+
6757
+ function nativeClaudeListWriteContext(
6758
+ settings: Settings,
6759
+ modelId: string,
6760
+ ):
6761
+ | {
6762
+ declaredTtl: "off" | "5m" | "1h" | undefined;
6763
+ publishedRates?: { fiveMinute: number; oneHour: number };
6764
+ }
6765
+ | undefined {
6766
+ for (const provider of configuredRegistryProviders(settings)) {
6767
+ const model = provider.models.find((candidate) => candidate.id === modelId);
6768
+ if (!model || provider.api !== "anthropic-messages") continue;
6769
+ const result = { declaredTtl: provider.anthropic?.cacheTtl };
6770
+ const upstream = model.upstreamModelId ?? model.id;
6771
+ const overrides = parseModelPricingJson(settings.modelPricingJson);
6772
+ const official =
6773
+ provider.kind === "anthropic-workspace" ||
6774
+ provider.kind === "anthropic-organization" ||
6775
+ provider.kind === "claude-subscription-workspace" ||
6776
+ provider.kind === "claude-subscription-organization" ||
6777
+ (provider.kind === "api-key" &&
6778
+ provider.baseUrl.replace(/\/$/u, "") === "https://api.anthropic.com/v1");
6779
+ const published = reviewedModelListPricing[upstream]?.default;
6780
+ if (
6781
+ !official ||
6782
+ !claudeNativeModelProfile(upstream) ||
6783
+ !published?.cacheWriteMicrosPerMillionTokens ||
6784
+ model.pricing ||
6785
+ overrides[modelId] !== undefined ||
6786
+ overrides[upstream] !== undefined
6787
+ )
6788
+ return result;
6789
+ return {
6790
+ ...result,
6791
+ publishedRates: {
6792
+ fiveMinute: published.cacheWriteMicrosPerMillionTokens,
6793
+ oneHour: published.inputMicrosPerMillionTokens * 2,
6794
+ },
6795
+ };
6796
+ }
6797
+ return undefined;
6798
+ }
6799
+
6800
+ /** Deterministic largest-remainder allocation; disclose any fractional scaling. */
6801
+ function allocateLatencyListCostClasses(
6802
+ classes: ModelListCostClassesMicros,
6803
+ multiplier: number,
6804
+ target: number,
6805
+ ): { classes: ModelListCostClassesMicros; approximate: boolean } | undefined {
6806
+ if (
6807
+ !knownTokenCounter(multiplier) ||
6808
+ !knownTokenCounter(target) ||
6809
+ !Object.values(classes).every(knownTokenCounter)
6810
+ )
6811
+ return undefined;
6812
+ const rows = MODEL_LIST_COST_CLASS_KEYS.map((key, index) => {
6813
+ const numerator = BigInt(classes[key]) * BigInt(multiplier);
6814
+ return { key, index, value: Number(numerator / 10_000n), remainder: numerator % 10_000n };
6815
+ });
6816
+ const remaining = target - rows.reduce((total, row) => total + row.value, 0);
6817
+ if (remaining < 0 || remaining > rows.length) return undefined;
6818
+ const ranked = [...rows].sort((a, b) =>
6819
+ a.remainder === b.remainder ? a.index - b.index : a.remainder > b.remainder ? -1 : 1,
6820
+ );
6821
+ for (let index = 0; index < remaining; index++) ranked[index]!.value += 1;
6822
+ return {
6823
+ classes: Object.fromEntries(
6824
+ rows.map((row) => [row.key, row.value]),
6825
+ ) as ModelListCostClassesMicros,
6826
+ approximate: rows.some((row) => row.remainder !== 0n),
6827
+ };
6828
+ }
6829
+
6830
+ function calculateUsageCostBreakdown(
6831
+ settings: Settings,
6832
+ model: string,
6833
+ usage: ModelUsageInput,
6834
+ schedule: ModelPricingScheduleV1 | undefined,
6835
+ options?: { latencyMode?: LatencyMode },
6836
+ ): ModelUsageCostBreakdown {
6837
+ if (!schedule) {
6838
+ throw new Error(`Missing model pricing for ${model}`);
6839
+ }
6840
+ const entries =
6841
+ usage.requestUsageEntries && usage.requestUsageEntries.length > 0
6842
+ ? usage.requestUsageEntries
6843
+ : [usage];
6844
+ const rawCostByPricing = new Map<ModelPricing, number>();
6845
+ for (const entry of entries) {
6846
+ const pricing = selectModelPricing(schedule, positiveInt(entry.inputTokens));
6847
+ rawCostByPricing.set(
6848
+ pricing,
6849
+ (rawCostByPricing.get(pricing) ?? 0) + calculateEntryCostMicros(pricing, entry),
6850
+ );
6851
+ }
6852
+ let providerCostMicros = 0;
6853
+ let creditCostMicros = 0;
6854
+ for (const [pricing, rawCost] of rawCostByPricing) {
6855
+ const marginBps = pricing.marginBps ?? 0;
6856
+ providerCostMicros += rawCost;
6857
+ creditCostMicros += Math.ceil((rawCost * (10_000 + marginBps)) / 10_000);
6858
+ }
6859
+ const latencyMode = options?.latencyMode ?? "standard";
6860
+ if (latencyMode !== "standard") {
6861
+ const catalogSettings = settingsForTurnExecutionPolicy(settings, model);
6862
+ const resolved = resolveModelProvider(
6863
+ catalogSettings,
6864
+ canonicalizeConfiguredModelId(catalogSettings, model),
6865
+ );
6866
+ const multiplierBps = resolved?.model.capabilities.latencyModes.find(
6867
+ (mode) => mode.id === latencyMode && mode.runnable,
6868
+ )?.billingMultiplierBps;
6869
+ if (multiplierBps && multiplierBps > 0) {
6870
+ providerCostMicros = Math.ceil((providerCostMicros * multiplierBps) / 10_000);
6871
+ creditCostMicros = Math.ceil((creditCostMicros * multiplierBps) / 10_000);
6872
+ }
6873
+ }
6874
+ return { providerCostMicros, creditCostMicros };
6875
+ }
6876
+
6877
+ /**
6878
+ * Convert AI Gateway's exact USD inference cost to OpenGeni credit micros and
6879
+ * apply the configured model margin. Decimal arithmetic is integer-only so a
6880
+ * sub-micro provider charge cannot be lost to floating-point rounding.
6881
+ */
6882
+ export function calculateGatewayReportedCostMicros(
6883
+ settings: Settings,
6884
+ model: string,
6885
+ inferenceCostUsd: string,
6886
+ options?: { inputTokens?: number },
6887
+ ): number {
6888
+ return calculateGatewayReportedCostBreakdown(settings, model, inferenceCostUsd, options)
6889
+ .creditCostMicros;
6890
+ }
6891
+
5410
6892
  type GatewayReportedCostDecimal = {
5411
6893
  providerNumerator: bigint;
5412
6894
  decimalScale: bigint;
@@ -5798,6 +7280,7 @@ export function applyGitAuthPointerEnvironment(
5798
7280
  }
5799
7281
 
5800
7282
  export type StartupRetryOptions = {
7283
+ shouldRetry?: (error: unknown) => boolean;
5801
7284
  attempts?: number;
5802
7285
  initialDelayMs?: number;
5803
7286
  maxDelayMs?: number;
@@ -5812,7 +7295,7 @@ export type StartupRetryOptions = {
5812
7295
 
5813
7296
  export function startupRetryOptions(
5814
7297
  settings: Settings,
5815
- ): Required<Omit<StartupRetryOptions, "onRetry">> {
7298
+ ): Required<Omit<StartupRetryOptions, "onRetry" | "shouldRetry">> {
5816
7299
  return {
5817
7300
  attempts: settings.startupDependencyRetryAttempts,
5818
7301
  initialDelayMs: settings.startupDependencyRetryInitialDelayMs,
@@ -5832,7 +7315,7 @@ export async function retryStartupDependency<T>(
5832
7315
  try {
5833
7316
  return await operation();
5834
7317
  } catch (error) {
5835
- if (attempt >= attempts) {
7318
+ if (attempt >= attempts || options.shouldRetry?.(error) === false) {
5836
7319
  throw error;
5837
7320
  }
5838
7321
  const delayMs = Math.min(maxDelayMs, initialDelayMs * 2 ** (attempt - 1));
@@ -6232,20 +7715,9 @@ function ensureBuiltInMcpServers(settings: Settings): Settings["mcpServers"] {
6232
7715
  : [
6233
7716
  {
6234
7717
  id: "docs",
6235
- name: "Document Search",
7718
+ name: "Knowledge",
6236
7719
  url: firstPartyDocsMcpUrl,
6237
- allowedTools: [
6238
- "search_documents",
6239
- "fetch_document_chunk",
6240
- "list_document_bases",
6241
- "list_indexed_documents",
6242
- "knowledge_search",
6243
- "knowledge_get",
6244
- "knowledge_browse",
6245
- "knowledge_fetch",
6246
- "memory_search",
6247
- "memory_propose",
6248
- ],
7720
+ allowedTools: ["knowledge_search", "knowledge_get", "knowledge_browse"],
6249
7721
  cacheToolsList: false,
6250
7722
  },
6251
7723
  ]),
@@ -6355,8 +7827,99 @@ function isDigestPinnedModalDesktopImage(settings: Settings): boolean {
6355
7827
  );
6356
7828
  }
6357
7829
 
7830
+ export type TrustedProxyCidr = {
7831
+ address: string;
7832
+ prefix: number;
7833
+ family: "ipv4" | "ipv6";
7834
+ };
7835
+
7836
+ /**
7837
+ * Parse `OPENGENI_API_TRUSTED_PROXY_CIDRS`: comma-separated IPv4/IPv6 CIDRs or
7838
+ * single addresses. Throws on any malformed entry so a typo can never widen or
7839
+ * silently disable the trusted proxy set.
7840
+ */
7841
+ export function trustedProxyCidrEntries(raw: string): TrustedProxyCidr[] {
7842
+ const entries: TrustedProxyCidr[] = [];
7843
+ for (const entry of raw.split(",")) {
7844
+ const value = entry.trim();
7845
+ if (!value) continue;
7846
+ const slash = value.indexOf("/");
7847
+ const address = slash < 0 ? value : value.slice(0, slash);
7848
+ const version = isIP(address);
7849
+ const family = version === 4 ? "ipv4" : version === 6 ? "ipv6" : null;
7850
+ const maxPrefix = family === "ipv4" ? 32 : 128;
7851
+ const prefixText = slash < 0 ? String(maxPrefix) : value.slice(slash + 1);
7852
+ const prefix = /^\d{1,3}$/u.test(prefixText) ? Number(prefixText) : Number.NaN;
7853
+ if (!family || !Number.isInteger(prefix) || prefix < 0 || prefix > maxPrefix) {
7854
+ throw new Error(
7855
+ `OPENGENI_API_TRUSTED_PROXY_CIDRS entry "${value.slice(0, 64)}" is not an IP address or CIDR range`,
7856
+ );
7857
+ }
7858
+ entries.push({ address, prefix, family });
7859
+ }
7860
+ return entries;
7861
+ }
7862
+
7863
+ /**
7864
+ * Parse `OPENGENI_LOCAL_ALLOWED_ORIGINS`: comma-separated exact browser origins
7865
+ * (`http(s)://host[:port]`, no path, query, credentials, or wildcard). Throws on
7866
+ * any malformed entry so a typo can never widen or silently disable the local
7867
+ * browser boundary.
7868
+ */
7869
+ export function localAllowedOriginEntries(raw: string | undefined): string[] {
7870
+ const origins: string[] = [];
7871
+ for (const entry of (raw ?? "").split(",")) {
7872
+ const value = entry.trim();
7873
+ if (!value) continue;
7874
+ let url: URL | null = null;
7875
+ try {
7876
+ url = new URL(value);
7877
+ } catch {
7878
+ url = null;
7879
+ }
7880
+ if (
7881
+ !url ||
7882
+ (url.protocol !== "http:" && url.protocol !== "https:") ||
7883
+ url.username ||
7884
+ url.password ||
7885
+ url.hostname.includes("*") ||
7886
+ url.pathname !== "/" ||
7887
+ url.search ||
7888
+ url.hash ||
7889
+ value.endsWith("?") ||
7890
+ value.endsWith("#")
7891
+ ) {
7892
+ throw new Error(
7893
+ `OPENGENI_LOCAL_ALLOWED_ORIGINS entry "${value.slice(0, 64)}" is not an exact http(s) origin such as http://127.0.0.1:5173`,
7894
+ );
7895
+ }
7896
+ origins.push(url.origin);
7897
+ }
7898
+ return origins;
7899
+ }
7900
+
7901
+ const LOCAL_SANDBOX_API_ROUTES: readonly RegExp[] = [
7902
+ // Codemode calls, journal reads, catalog, and the Site/SDK proxy.
7903
+ /^\/v1\/workspaces\/[^/]+\/codemode(?:\/|$)/u,
7904
+ // First-party MCP, including its /docs and /files servers.
7905
+ /^\/v1\/workspaces\/[^/]+\/mcp(?:\/|$)/u,
7906
+ // The personal GitHub HTTPS smart-Git broker.
7907
+ /^\/v1\/git\/personal\/[A-Za-z0-9_-]{43}\/(?:info\/refs|git-upload-pack|git-receive-pack)$/u,
7908
+ ];
7909
+
7910
+ /**
7911
+ * Local development only: whether a normalized API path is one a sandbox calls
7912
+ * (Codemode, first-party MCP, and the personal Git broker). The Linux Docker
7913
+ * sandbox route relays only these, and the local API serves only these on the
7914
+ * addresses that only sandboxes use (see `apps/api/src/http/local-browser-boundary.ts`).
7915
+ */
7916
+ export function localSandboxApiRouteAllowed(pathname: string): boolean {
7917
+ return LOCAL_SANDBOX_API_ROUTES.some((route) => route.test(pathname));
7918
+ }
7919
+
6358
7920
  function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = process.env): void {
6359
7921
  temporalConnectionOptions(settings);
7922
+ localAllowedOriginEntries(settings.localAllowedOrigins);
6360
7923
  if (
6361
7924
  settings.organizationUserSetupEmailTokenTransport === "query" &&
6362
7925
  !settings.organizationUserSetupQueryEdgeSanitizationConfirmed
@@ -6450,6 +8013,21 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6450
8013
  );
6451
8014
  }
6452
8015
  }
8016
+ // The retired MCP-only name must never be ignored silently: a deployment
8017
+ // that trusted forwarded addresses would fall back to the transport peer.
8018
+ // "0" (the old .env.example value) means the same as the new default.
8019
+ const retiredTrustedProxyHops = source.OPENGENI_MCP_OAUTH_TRUSTED_PROXY_HOPS?.trim();
8020
+ if (retiredTrustedProxyHops && retiredTrustedProxyHops !== "0") {
8021
+ throw new Error(
8022
+ "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",
8023
+ );
8024
+ }
8025
+ const trustedProxyCidrs = trustedProxyCidrEntries(settings.apiTrustedProxyCidrs);
8026
+ if (trustedProxyCidrs.length > 0 && settings.apiTrustedProxyHops === 0) {
8027
+ throw new Error(
8028
+ "OPENGENI_API_TRUSTED_PROXY_CIDRS requires OPENGENI_API_TRUSTED_PROXY_HOPS greater than 0",
8029
+ );
8030
+ }
6453
8031
  if (settings.mcpOauthEnabled) {
6454
8032
  if (settings.productAccessMode === "configured") {
6455
8033
  throw new Error(
@@ -6750,105 +8328,7 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6750
8328
  );
6751
8329
  }
6752
8330
  }
6753
- if (
6754
- settings.objectStorageBackend === "s3-compatible" ||
6755
- settings.objectStorageBackend === "aws-s3"
6756
- ) {
6757
- if (
6758
- Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
6759
- ) {
6760
- throw new Error(
6761
- "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
6762
- );
6763
- }
6764
- if (
6765
- settings.objectStorageBackend === "s3-compatible" &&
6766
- (settings.objectStorageEndpoint ||
6767
- settings.objectStorageInternalEndpoint ||
6768
- settings.objectStorageSandboxEndpoint) &&
6769
- (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
6770
- ) {
6771
- throw new Error(
6772
- "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
6773
- );
6774
- }
6775
- if (
6776
- settings.objectStorageAzureConnectionString ||
6777
- settings.objectStorageAzureAccountName ||
6778
- settings.objectStorageAzureAccountKey ||
6779
- settings.objectStorageAzureEndpoint
6780
- ) {
6781
- throw new Error(
6782
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6783
- );
6784
- }
6785
- if (
6786
- settings.objectStorageGcsProjectId ||
6787
- settings.objectStorageGcsCredentialsJson ||
6788
- settings.objectStorageGcsKeyFilename ||
6789
- settings.objectStorageGcsApiEndpoint
6790
- ) {
6791
- throw new Error(
6792
- "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6793
- );
6794
- }
6795
- } else if (settings.objectStorageBackend === "azure-blob") {
6796
- if (
6797
- settings.objectStorageEndpoint ||
6798
- settings.objectStorageInternalEndpoint ||
6799
- settings.objectStorageSandboxEndpoint ||
6800
- settings.objectStorageAccessKeyId ||
6801
- settings.objectStorageSecretAccessKey
6802
- ) {
6803
- throw new Error(
6804
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
6805
- );
6806
- }
6807
- if (
6808
- settings.objectStorageGcsProjectId ||
6809
- settings.objectStorageGcsCredentialsJson ||
6810
- settings.objectStorageGcsKeyFilename ||
6811
- settings.objectStorageGcsApiEndpoint
6812
- ) {
6813
- throw new Error(
6814
- "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
6815
- );
6816
- }
6817
- const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
6818
- const hasSharedKey =
6819
- Boolean(settings.objectStorageAzureAccountName) &&
6820
- Boolean(settings.objectStorageAzureAccountKey);
6821
- if (!hasConnectionString && !hasSharedKey) {
6822
- throw new Error(
6823
- "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
6824
- );
6825
- }
6826
- } else {
6827
- if (
6828
- settings.objectStorageEndpoint ||
6829
- settings.objectStorageInternalEndpoint ||
6830
- settings.objectStorageSandboxEndpoint ||
6831
- settings.objectStorageAccessKeyId ||
6832
- settings.objectStorageSecretAccessKey
6833
- ) {
6834
- throw new Error(
6835
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
6836
- );
6837
- }
6838
- if (
6839
- settings.objectStorageAzureConnectionString ||
6840
- settings.objectStorageAzureAccountName ||
6841
- settings.objectStorageAzureAccountKey ||
6842
- settings.objectStorageAzureEndpoint
6843
- ) {
6844
- throw new Error(
6845
- "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
6846
- );
6847
- }
6848
- if (settings.objectStorageGcsCredentialsJson) {
6849
- parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
6850
- }
6851
- }
8331
+ validateObjectStorageSettings(settings);
6852
8332
  if (settings.documentChunkOverlap >= settings.documentChunkSize) {
6853
8333
  throw new Error(
6854
8334
  "OPENGENI_DOCUMENT_CHUNK_OVERLAP must be smaller than OPENGENI_DOCUMENT_CHUNK_SIZE",
@@ -6858,7 +8338,36 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6858
8338
  sandboxEnvironmentVariableNames(settings);
6859
8339
  sandboxLifecycleHookIds(settings);
6860
8340
  // Fail fast on a malformed warm-rate table (P2.1).
6861
- parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8341
+ const warmRates = parseSandboxWarmRateJson(settings.sandboxWarmRateMicrosPerSecondJson);
8342
+ // The meter rounds each settled interval to whole USD micros. Fractional
8343
+ // micros/second would make the total depend on heartbeat frequency; only
8344
+ // integer rates have interval-independent settlement at the same elapsed time.
8345
+ if (
8346
+ settings.sandboxWarmBillingMode === "credits" &&
8347
+ Object.values(warmRates).some((rate) => !Number.isSafeInteger(rate))
8348
+ ) {
8349
+ throw new Error(
8350
+ "OPENGENI_SANDBOX_WARM_RATE_MICROS_PER_SECOND_JSON paid rates must be safe integers",
8351
+ );
8352
+ }
8353
+ if (
8354
+ settings.documentEmbeddingBillingMode === "credits" &&
8355
+ settings.documentEmbeddingProvider === "openai" &&
8356
+ settings.documentEmbeddingRateMicrosPerMillionBytes <= 0
8357
+ ) {
8358
+ throw new Error(
8359
+ "OPENGENI_DOCUMENT_EMBEDDING_RATE_MICROS_PER_MILLION_BYTES must be positive when paid OpenAI embedding billing is enabled",
8360
+ );
8361
+ }
8362
+ if (
8363
+ settings.documentEmbeddingBillingMode === "credits" &&
8364
+ settings.documentEmbeddingProvider === "openai" &&
8365
+ !settings.documentEmbeddingCreditsActivatedAt
8366
+ ) {
8367
+ throw new Error(
8368
+ "OPENGENI_DOCUMENT_EMBEDDING_CREDITS_ACTIVATED_AT is required for paid OpenAI embeddings",
8369
+ );
8370
+ }
6862
8371
  if (settings.sandboxBackend === "opensandbox") {
6863
8372
  if (!/@sha256:[0-9a-f]{64}$/i.test(settings.openSandboxImage ?? "")) {
6864
8373
  throw new Error(
@@ -6877,12 +8386,9 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6877
8386
  throw new Error(`OPENGENI_MCP_SERVERS contains duplicate id ${server.id}`);
6878
8387
  }
6879
8388
  serverIds.add(server.id);
6880
- if (
6881
- server.connectionRef?.authoritySource === "host" &&
6882
- !settings.hostMcpAuthoritySourceAdmissionEnabled
6883
- ) {
8389
+ if (server.connectionRef?.authoritySource === "host") {
6884
8390
  throw new Error(
6885
- "OPENGENI_MCP_SERVERS host-owned connection refs require OPENGENI_HOST_MCP_AUTHORITY_SOURCE_ADMISSION_ENABLED=true after the whole API/worker fleet is upgraded",
8391
+ "OPENGENI_MCP_SERVERS host-owned connection refs are no longer supported; select ordinary native connections",
6886
8392
  );
6887
8393
  }
6888
8394
  }
@@ -6936,14 +8442,36 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6936
8442
  ordinaryCaptureTimeoutMs,
6937
8443
  drainCaptureTimeoutMs,
6938
8444
  );
6939
- if (!(rotationLeadMs > providerDeadlineCaptureTimeoutMs + reaperPeriod)) {
8445
+ const requiredRotationLeadMs =
8446
+ SANDBOX_DEADLINE_COMMAND_STOP_GRACE_MS + providerDeadlineCaptureTimeoutMs + 2 * reaperPeriod;
8447
+ if (!(rotationLeadMs > requiredRotationLeadMs)) {
6940
8448
  throw new Error(
6941
8449
  `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}) must exceed the ` +
6942
- `largest durable snapshot or drain capture timeout plus one reaper period ` +
6943
- `(${providerDeadlineCaptureTimeoutMs + reaperPeriod}), including for persisted Modal ` +
8450
+ `legacy command stop grace, largest durable snapshot or drain capture timeout, and two reaper periods ` +
8451
+ `(${requiredRotationLeadMs}), including for persisted Modal ` +
6944
8452
  `leases after a default-backend rollout.`,
6945
8453
  );
6946
8454
  }
8455
+ const containmentMs = settings.sandboxIdleCommandContainmentMs;
8456
+ if (containmentMs !== undefined && !(containmentMs > settings.sandboxIdleGraceMs)) {
8457
+ throw new Error(
8458
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must exceed ` +
8459
+ `OPENGENI_SANDBOX_IDLE_GRACE_MS (${settings.sandboxIdleGraceMs}): a box kept warm only by ` +
8460
+ `retained commands must stay available at least as long as an idle box awaiting drain.`,
8461
+ );
8462
+ }
8463
+ if (
8464
+ containmentMs !== undefined &&
8465
+ optionalEnvironmentValue("OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS", source) !==
8466
+ undefined &&
8467
+ !(containmentMs < rotationLeadMs)
8468
+ ) {
8469
+ throw new Error(
8470
+ `OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS (${containmentMs}) must be strictly less than ` +
8471
+ `OPENGENI_SANDBOX_ROTATION_LEAD_MS (${rotationLeadMs}): an idle box must be checkpointed ` +
8472
+ `and stopped well before its provider deadline.`,
8473
+ );
8474
+ }
6947
8475
  if (settings.sandboxBackend === "modal") {
6948
8476
  const idleGraceMs = settings.sandboxIdleGraceMs;
6949
8477
  const lifecycle = effectiveSandboxLifecycle(settings, "modal");
@@ -6987,6 +8515,19 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
6987
8515
  `OPENGENI_SANDBOX_IDLE_GRACE_MS.`,
6988
8516
  );
6989
8517
  }
8518
+ if (
8519
+ settings.modalIdleTimeoutSeconds !== undefined &&
8520
+ containmentMs !== undefined &&
8521
+ !(reaperPeriod + containmentMs + drainCaptureTimeoutMs < idleTimeoutMs)
8522
+ ) {
8523
+ throw new Error(
8524
+ `OPENGENI_SANDBOX_LEASE_REAPER_PERIOD_MS + OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS + ` +
8525
+ `drain capture timeout (${reaperPeriod} + ${containmentMs} + ${drainCaptureTimeoutMs}) ` +
8526
+ `must be strictly less than OPENGENI_MODAL_IDLE_TIMEOUT_SECONDS*1000 (${idleTimeoutMs}): ` +
8527
+ `idle command containment must checkpoint a box before Modal's own idle reaper stops it. ` +
8528
+ `Lower OPENGENI_SANDBOX_IDLE_COMMAND_CONTAINMENT_MS or raise the Modal idle timeout.`,
8529
+ );
8530
+ }
6990
8531
  }
6991
8532
  }
6992
8533
  // --- stream-token secret: required-when-desktop, but GRACEFULLY DEGRADE (stream-token availability contract) ---
@@ -7044,8 +8585,14 @@ export function validateModelCatalogSettings(
7044
8585
  provider.kind === "vercel-gateway-managed" ||
7045
8586
  provider.kind === "vercel-gateway-workspace" ||
7046
8587
  provider.kind === "vercel-gateway-organization" ||
8588
+ provider.kind === "direct-openai-workspace" ||
8589
+ provider.kind === "direct-azure-workspace" ||
7047
8590
  provider.kind === "openrouter-workspace" ||
7048
8591
  provider.kind === "openrouter-organization" ||
8592
+ provider.kind === "anthropic-organization" ||
8593
+ provider.kind === "anthropic-workspace" ||
8594
+ provider.kind === "claude-subscription-workspace" ||
8595
+ provider.kind === "claude-subscription-organization" ||
7049
8596
  provider.kind === "xai-subscription"
7050
8597
  ) {
7051
8598
  throw new Error(
@@ -7097,11 +8644,39 @@ export function validateModelCatalogSettings(
7097
8644
  `The default model ${settings.openaiModel} is not executable in the resolved model catalog`,
7098
8645
  );
7099
8646
  }
8647
+ // An operator-set credits default must name a credits-billed model in the
8648
+ // env catalog; a typo would otherwise fall back silently to the first
8649
+ // selectable credits model. Only an explicit value in code catalog mode is
8650
+ // checked: the built-in gpt-6-luna may be absent from a custom catalog, and
8651
+ // a database catalog is edited independently of this env value, so both keep
8652
+ // the documented fallback (docs/model-providers.md) instead of failing
8653
+ // every catalog read.
8654
+ const explicitCreditsDefault = source.OPENGENI_CREDITS_DEFAULT_MODEL?.trim();
8655
+ if (
8656
+ explicitCreditsDefault &&
8657
+ settings.modelCatalogSource === "code" &&
8658
+ settings.billingMode === "stripe"
8659
+ ) {
8660
+ const creditsDefaultId = canonicalizeConfiguredModelId(settings, settings.creditsDefaultModel);
8661
+ const creditsDefault = models.find((model) => model.id === creditsDefaultId);
8662
+ if (creditsDefault?.cost !== "credits") {
8663
+ throw new Error(
8664
+ `OPENGENI_CREDITS_DEFAULT_MODEL ${settings.creditsDefaultModel} is not a credits-billed model in the resolved model catalog`,
8665
+ );
8666
+ }
8667
+ }
7100
8668
 
7101
8669
  const deploymentProductIds = new Set(
7102
8670
  models.filter((model) => model.credentialSource.kind === "deployment").map((model) => model.id),
7103
8671
  );
7104
8672
  const noteProductIds = new Set(models.map((model) => model.id));
8673
+ if (settings.resolvedCodexModelsJson !== undefined) {
8674
+ for (const model of z
8675
+ .array(CodexCatalogModelSchema)
8676
+ .parse(JSON.parse(settings.resolvedCodexModelsJson))) {
8677
+ noteProductIds.add(model.id);
8678
+ }
8679
+ }
7105
8680
  for (const model of configuredGatewayCatalogModels(settings)) {
7106
8681
  deploymentProductIds.add(model.productId);
7107
8682
  noteProductIds.add(model.productId);
@@ -7236,6 +8811,108 @@ export function resolveNatsCalloutConfig(settings: Settings): NatsCalloutConfig
7236
8811
  return { accountSeed, accountName, user, password };
7237
8812
  }
7238
8813
 
8814
+ function validateObjectStorageSettings(settings: ObjectStorageSettings): void {
8815
+ if (
8816
+ settings.objectStorageBackend === "s3-compatible" ||
8817
+ settings.objectStorageBackend === "aws-s3"
8818
+ ) {
8819
+ if (
8820
+ Boolean(settings.objectStorageAccessKeyId) !== Boolean(settings.objectStorageSecretAccessKey)
8821
+ ) {
8822
+ throw new Error(
8823
+ "OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY must both be set or both omitted",
8824
+ );
8825
+ }
8826
+ if (
8827
+ settings.objectStorageBackend === "s3-compatible" &&
8828
+ (settings.objectStorageEndpoint ||
8829
+ settings.objectStorageInternalEndpoint ||
8830
+ settings.objectStorageSandboxEndpoint) &&
8831
+ (!settings.objectStorageAccessKeyId || !settings.objectStorageSecretAccessKey)
8832
+ ) {
8833
+ throw new Error(
8834
+ "S3-compatible object storage endpoints require OPENGENI_OBJECT_STORAGE_ACCESS_KEY_ID and OPENGENI_OBJECT_STORAGE_SECRET_ACCESS_KEY",
8835
+ );
8836
+ }
8837
+ if (
8838
+ settings.objectStorageAzureConnectionString ||
8839
+ settings.objectStorageAzureAccountName ||
8840
+ settings.objectStorageAzureAccountKey ||
8841
+ settings.objectStorageAzureEndpoint
8842
+ ) {
8843
+ throw new Error(
8844
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
8845
+ );
8846
+ }
8847
+ if (
8848
+ settings.objectStorageGcsProjectId ||
8849
+ settings.objectStorageGcsCredentialsJson ||
8850
+ settings.objectStorageGcsKeyFilename ||
8851
+ settings.objectStorageGcsApiEndpoint
8852
+ ) {
8853
+ throw new Error(
8854
+ "S3 object storage uses OPENGENI_OBJECT_STORAGE_* S3 settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
8855
+ );
8856
+ }
8857
+ } else if (settings.objectStorageBackend === "azure-blob") {
8858
+ if (
8859
+ settings.objectStorageEndpoint ||
8860
+ settings.objectStorageInternalEndpoint ||
8861
+ settings.objectStorageSandboxEndpoint ||
8862
+ settings.objectStorageAccessKeyId ||
8863
+ settings.objectStorageSecretAccessKey
8864
+ ) {
8865
+ throw new Error(
8866
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not S3-compatible object storage settings",
8867
+ );
8868
+ }
8869
+ if (
8870
+ settings.objectStorageGcsProjectId ||
8871
+ settings.objectStorageGcsCredentialsJson ||
8872
+ settings.objectStorageGcsKeyFilename ||
8873
+ settings.objectStorageGcsApiEndpoint
8874
+ ) {
8875
+ throw new Error(
8876
+ "Azure Blob storage uses OPENGENI_OBJECT_STORAGE_AZURE_* settings, not OPENGENI_OBJECT_STORAGE_GCS_* settings",
8877
+ );
8878
+ }
8879
+ const hasConnectionString = Boolean(settings.objectStorageAzureConnectionString);
8880
+ const hasSharedKey =
8881
+ Boolean(settings.objectStorageAzureAccountName) &&
8882
+ Boolean(settings.objectStorageAzureAccountKey);
8883
+ if (!hasConnectionString && !hasSharedKey) {
8884
+ throw new Error(
8885
+ "Azure Blob storage requires OPENGENI_OBJECT_STORAGE_AZURE_CONNECTION_STRING or OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_NAME plus OPENGENI_OBJECT_STORAGE_AZURE_ACCOUNT_KEY",
8886
+ );
8887
+ }
8888
+ } else {
8889
+ if (
8890
+ settings.objectStorageEndpoint ||
8891
+ settings.objectStorageInternalEndpoint ||
8892
+ settings.objectStorageSandboxEndpoint ||
8893
+ settings.objectStorageAccessKeyId ||
8894
+ settings.objectStorageSecretAccessKey
8895
+ ) {
8896
+ throw new Error(
8897
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not S3-compatible object storage settings",
8898
+ );
8899
+ }
8900
+ if (
8901
+ settings.objectStorageAzureConnectionString ||
8902
+ settings.objectStorageAzureAccountName ||
8903
+ settings.objectStorageAzureAccountKey ||
8904
+ settings.objectStorageAzureEndpoint
8905
+ ) {
8906
+ throw new Error(
8907
+ "GCS object storage uses OPENGENI_OBJECT_STORAGE_GCS_* settings, not OPENGENI_OBJECT_STORAGE_AZURE_* settings",
8908
+ );
8909
+ }
8910
+ if (settings.objectStorageGcsCredentialsJson) {
8911
+ parseGcsCredentialsJson(settings.objectStorageGcsCredentialsJson);
8912
+ }
8913
+ }
8914
+ }
8915
+
7239
8916
  /**
7240
8917
  * The PRIVILEGED control-plane NATS login (api/worker). Present only when BOTH a
7241
8918
  * user and password are set; otherwise null and the bus connects anonymously (local
@@ -7247,7 +8924,9 @@ export interface NatsControlPlaneAuth {
7247
8924
  password: string;
7248
8925
  }
7249
8926
 
7250
- export function resolveNatsControlPlaneAuth(settings: Settings): NatsControlPlaneAuth | null {
8927
+ export function resolveNatsControlPlaneAuth(
8928
+ settings: Pick<Settings, "selfhostedNatsControlUser" | "selfhostedNatsControlPassword">,
8929
+ ): NatsControlPlaneAuth | null {
7251
8930
  const user = settings.selfhostedNatsControlUser?.trim();
7252
8931
  const password = settings.selfhostedNatsControlPassword?.trim();
7253
8932
  if (!user || !password) {
@@ -7263,6 +8942,11 @@ function splitCsv(raw: string): string[] {
7263
8942
  .filter(Boolean);
7264
8943
  }
7265
8944
 
8945
+ /** Image references a workspace may select as its default sandbox image. */
8946
+ export function sandboxImageAllowlist(settings: Pick<Settings, "sandboxImageAllowlist">): string[] {
8947
+ return [...new Set(splitCsv(settings.sandboxImageAllowlist ?? ""))];
8948
+ }
8949
+
7266
8950
  function uniqueEnvNames(raw: string[], fieldName: string): string[] {
7267
8951
  const seen = new Set<string>();
7268
8952
  const out: string[] = [];
@@ -7296,3 +8980,196 @@ function parseGcsCredentialsJson(raw: string): unknown {
7296
8980
  function delay(ms: number): Promise<void> {
7297
8981
  return new Promise((resolve) => setTimeout(resolve, ms));
7298
8982
  }
8983
+
8984
+ /** Native Claude connections reuse the encrypted workspace and organization boundaries. */
8985
+ export const CLAUDE_CONNECTION_KINDS = ["anthropic", "claude_subscription"] as const;
8986
+ export type ClaudeConnectionKind = (typeof CLAUDE_CONNECTION_KINDS)[number];
8987
+ // Per-model native Messages controls. Unknown IDs never inherit adaptive
8988
+ // thinking merely because they share a provider with a supported model.
8989
+ const CLAUDE_NATIVE_MODEL_PROFILES: Readonly<
8990
+ Record<
8991
+ string,
8992
+ Readonly<{
8993
+ efforts: readonly ReasoningEffort[];
8994
+ defaultEffort: ReasoningEffort | null;
8995
+ contextWindowTokens: number;
8996
+ maxOutputTokens: number;
8997
+ }>
8998
+ >
8999
+ > = Object.fromEntries([
9000
+ ...[
9001
+ ["claude-opus-5-5", "medium"],
9002
+ ["claude-sonnet-5-5", "medium"],
9003
+ ["claude-opus-5", "high"],
9004
+ ["claude-sonnet-5", "high"],
9005
+ ["claude-opus-4-8", "high"],
9006
+ ["claude-opus-4-7", "xhigh"],
9007
+ ].map(([id, defaultEffort]) => [
9008
+ id,
9009
+ {
9010
+ efforts: ["low", "medium", "high", "xhigh", "max"],
9011
+ defaultEffort,
9012
+ contextWindowTokens: 1_000_000,
9013
+ maxOutputTokens: 128_000,
9014
+ },
9015
+ ]),
9016
+ ...["claude-opus-4-6", "claude-sonnet-4-6"].map((id) => [
9017
+ id,
9018
+ {
9019
+ efforts: ["low", "medium", "high", "max"],
9020
+ defaultEffort: "high",
9021
+ contextWindowTokens: 1_000_000,
9022
+ maxOutputTokens: 128_000,
9023
+ },
9024
+ ]),
9025
+ [
9026
+ "claude-haiku-4-5-20251001",
9027
+ {
9028
+ efforts: [],
9029
+ defaultEffort: null,
9030
+ contextWindowTokens: 200_000,
9031
+ maxOutputTokens: 64_000,
9032
+ },
9033
+ ],
9034
+ ]);
9035
+ export function claudeNativeModelProfile(upstreamModelId: string) {
9036
+ return Object.hasOwn(CLAUDE_NATIVE_MODEL_PROFILES, upstreamModelId)
9037
+ ? CLAUDE_NATIVE_MODEL_PROFILES[upstreamModelId]
9038
+ : undefined;
9039
+ }
9040
+ export function claudeProviderId(
9041
+ kind: ClaudeConnectionKind,
9042
+ scope: "workspace" | "organization" = "organization",
9043
+ ): string {
9044
+ return `${scope}-${kind === "anthropic" ? "anthropic" : "claude-subscription"}`;
9045
+ }
9046
+ export type ClaudeConnectionCatalog = Partial<
9047
+ Record<
9048
+ ClaudeConnectionKind,
9049
+ {
9050
+ active?: boolean;
9051
+ models: readonly { upstreamModelId: string; label?: string | null }[];
9052
+ }
9053
+ >
9054
+ >;
9055
+ export function withClaudeConnectionCatalog(
9056
+ settings: Settings,
9057
+ connections: ClaudeConnectionCatalog,
9058
+ scope: "workspace" | "organization" = "organization",
9059
+ ): Settings {
9060
+ let providers = parseModelProvidersJson(settings.modelProvidersJson);
9061
+ for (const kind of CLAUDE_CONNECTION_KINDS) {
9062
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled) {
9063
+ providers = providers.filter((provider) => provider.id !== claudeProviderId(kind, scope));
9064
+ continue;
9065
+ }
9066
+ const connection = connections[kind];
9067
+ if (!connection) continue;
9068
+ const id = claudeProviderId(kind, scope);
9069
+ providers = providers.filter((provider) => provider.id !== id);
9070
+ if (!connection.models.length) continue;
9071
+ providers.push({
9072
+ id,
9073
+ label: kind === "anthropic" ? "Anthropic API" : "Claude subscription",
9074
+ kind: kind === "anthropic" ? `anthropic-${scope}` : `claude-subscription-${scope}`,
9075
+ api: "anthropic-messages",
9076
+ wireProfile: "openai",
9077
+ baseUrl: "https://api.anthropic.com/v1",
9078
+ anthropic: {
9079
+ auth: kind === "anthropic" ? "api-key" : "oauth",
9080
+ cacheTtl: "5m",
9081
+ maxOutputTokens: 128000,
9082
+ streamIdleTimeoutMs: 600000,
9083
+ },
9084
+ models: connection.models.map((model) => {
9085
+ const profile = claudeNativeModelProfile(model.upstreamModelId);
9086
+ const adaptiveThinking = Boolean(profile?.efforts.length);
9087
+ const contextWindowTokens = profile?.contextWindowTokens ?? 200_000;
9088
+ const outputReserve = profile?.maxOutputTokens ?? 32_000;
9089
+ return {
9090
+ contextWindowTokens,
9091
+ effectiveContextWindowTokens: contextWindowTokens - outputReserve,
9092
+ autoCompactTokenLimit:
9093
+ contextWindowTokens === 1_000_000
9094
+ ? 800_000
9095
+ : Math.min(150_000, contextWindowTokens - outputReserve - 18_000),
9096
+ id: id + "/" + model.upstreamModelId,
9097
+ upstreamModelId: model.upstreamModelId,
9098
+ label:
9099
+ model.label ??
9100
+ (model.upstreamModelId === "claude-opus-5-5"
9101
+ ? "Claude Opus 5.5"
9102
+ : model.upstreamModelId === "claude-sonnet-5-5"
9103
+ ? "Claude Sonnet 5.5"
9104
+ : model.upstreamModelId),
9105
+ reasoningEffort: adaptiveThinking,
9106
+ hostedWebSearch: false,
9107
+ capabilities: {
9108
+ ...legacyModelCapabilities(settings, {
9109
+ reasoningEffort: adaptiveThinking,
9110
+ hostedWebSearch: false,
9111
+ vision: true,
9112
+ }),
9113
+ inputFileMediaTypes: [],
9114
+ reasoning: {
9115
+ upstream: adaptiveThinking ? "supported" : "unknown",
9116
+ runnable: adaptiveThinking,
9117
+ efforts: [...(profile?.efforts ?? [])],
9118
+ defaultEffort: profile?.defaultEffort ?? null,
9119
+ required: false,
9120
+ },
9121
+ functionCalling: { upstream: "supported", runnable: true },
9122
+ promptCaching: { upstream: "supported", runnable: true, mode: "automatic" },
9123
+ },
9124
+ };
9125
+ }),
9126
+ });
9127
+ }
9128
+ return { ...settings, modelProvidersJson: JSON.stringify(providers) };
9129
+ }
9130
+ export function withClaudeConnectionCredential(
9131
+ settings: Settings,
9132
+ kind: ClaudeConnectionKind,
9133
+ credential: string,
9134
+ scope: "workspace" | "organization" = "organization",
9135
+ credentialBinding?: { connectionId: string; credentialVersion: number },
9136
+ ): Settings {
9137
+ if (kind === "claude_subscription" && !settings.claudeSubscriptionEnabled)
9138
+ throw new Error("Claude subscriptions are not enabled on this deployment");
9139
+ if (!credential.trim()) throw new Error("Claude credential is empty");
9140
+ const bundle =
9141
+ kind === "claude_subscription" && credential.trim().startsWith("{")
9142
+ ? ClaudeSubscriptionCredential.parse(JSON.parse(credential))
9143
+ : null;
9144
+ return {
9145
+ ...settings,
9146
+ modelProvidersJson: JSON.stringify(
9147
+ parseModelProvidersJson(settings.modelProvidersJson).map((provider) =>
9148
+ provider.id === claudeProviderId(kind, scope)
9149
+ ? {
9150
+ ...provider,
9151
+ apiKey: bundle?.token ?? credential,
9152
+ ...(kind === "claude_subscription" && provider.anthropic
9153
+ ? {
9154
+ anthropic: {
9155
+ ...provider.anthropic,
9156
+ identity: bundle?.identity,
9157
+ credentialBinding: credentialBinding
9158
+ ? {
9159
+ connectionId: credentialBinding.connectionId,
9160
+ credentialVersion: credentialBinding.credentialVersion,
9161
+ }
9162
+ : undefined,
9163
+ },
9164
+ }
9165
+ : {}),
9166
+ }
9167
+ : provider,
9168
+ ),
9169
+ ),
9170
+ };
9171
+ }
9172
+ export * from "./claude-subscription-usage";
9173
+ export * from "./claude-subscription-oauth";
9174
+ export * from "./subscription-account-selection";
9175
+ export * from "./claude-subscription-capacity";