@opengeni/config 1.1.0-canary.0 → 1.1.0

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