Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

44.3 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import {
  harnessCarriedModelAuth,
  baseModelProviders,
  boolEnv,
  enabledSandboxBackends,
  loadConfig,
  configuredModelForHarness,
  numEnv,
  providerKeysPresent,
  CONFIG_DEFAULTS,
} from "../src/config.ts";

const productionEnv = {
  NODE_ENV: "production",
  CORE_SIGNING_SECRET: "core-signing-secret-0123456789abcdef",
  SKILL_SIGNING_SECRET: "skill-signing-secret-0123456789abcdef",
  CAPABILITY_SECRET: "capabilities",
  PORTAL_IDENTITY_SECRET: "portal",
  CONNECTOR_SECRET_KEY: "connector-secret-0123456789abcdef",
  SANDBOX_BACKEND: "local",
  DATABASE_URL: "postgres://qm@localhost/qm",
  SESSION_STORE: "postgres",
} as const;

test("Sprites proxy transition URLs are optional and parsed independently of the primary", () => {
  assert.equal(loadConfig({}).spritesSandbox.egressProxyAdditionalUrls, undefined);
  const config = loadConfig({
    SPRITES_EGRESS_PROXY_URL: "https://primary.example.com",
    SPRITES_EGRESS_PROXY_ADDITIONAL_URLS: " https://old.example.com, ,https://next.example.com ",
  });
  assert.equal(config.spritesSandbox.egressProxyUrl, "https://primary.example.com");
  assert.deepEqual(config.spritesSandbox.egressProxyAdditionalUrls, [
    "https://old.example.com",
    "https://next.example.com",
  ]);
});

test("capability compression is explicitly enabled after verifier rollout", () => {
  assert.equal(loadConfig({}).capabilityTokenCompression, false);
  assert.equal(loadConfig({ CAPABILITY_TOKEN_COMPRESSION: "0" }).capabilityTokenCompression, false);
  assert.equal(loadConfig({ CAPABILITY_TOKEN_COMPRESSION: "1" }).capabilityTokenCompression, true);
  assert.throws(() => loadConfig({ CAPABILITY_TOKEN_COMPRESSION: "invalid" }), /CAPABILITY_TOKEN_COMPRESSION/);
});

test("ORG_BRAND_* parses into a validated branding default", () => {
  assert.equal(loadConfig({}).brandingDefault, undefined);
  assert.deepEqual(
    loadConfig({ ORG_BRAND_ACCENT: "#6366f1", ORG_BRAND_MARK: "Q", ORG_BRAND_SELF_LABEL: "qm" }).brandingDefault,
    { accent: "#6366f1", mark: "Q", selfLabel: "qm" },
  );
  assert.equal(loadConfig({ ORG_BRAND_ACCENT: "#abcde" }).brandingDefault, undefined);
  assert.deepEqual(loadConfig({ ORG_BRAND_MARK: 'a"bc' }).brandingDefault, { mark: "ab" });
  assert.equal(loadConfig({ ORG_BRAND_SELF_LABEL: "x".repeat(80) }).brandingDefault?.selfLabel?.length, 40);
  assert.deepEqual(loadConfig({ ORG_BRAND_ORG_NAME: "Acme Corp" }).brandingDefault, { orgName: "Acme Corp" });
  assert.equal(loadConfig({ ORG_BRAND_ORG_NAME: "x".repeat(80) }).brandingDefault?.orgName?.length, 40);
  assert.deepEqual(loadConfig({ ORG_BRAND_SELF_LABEL: "{{straylight}}" }).brandingDefault, { selfLabel: "straylight" });
});

test("AUTH_ALLOWED_EMAILS becomes a normalized email-auth principal set", () => {
  assert.equal(loadConfig({}).emailAuthPrincipals, undefined);
  assert.deepEqual(
    loadConfig({ AUTH_ALLOWED_EMAILS: " [email protected],[email protected],[email protected] " }).emailAuthPrincipals,
    ["[email protected]", "[email protected]"],
  );
});

test("store kinds default to memory and accept postgres", () => {
  const def = loadConfig({});
  assert.equal(def.sessionStore, "memory");
  assert.equal(def.runStore, "memory");

  const pg = loadConfig({ SESSION_STORE: "postgres", DATABASE_URL: "postgres://test" });
  assert.equal(pg.sessionStore, "postgres");
  assert.equal(pg.runStore, "postgres", "runStore mirrors sessionStore when unset");

  assert.equal(
    loadConfig({ SESSION_STORE: "postgres", RUN_STORE: "memory", DATABASE_URL: "postgres://test" }).runStore,
    "memory",
  );
  assert.throws(
    () => loadConfig({ SESSION_STORE: "postgres" }),
    /missing or insecure required core secrets: DATABASE_URL/,
  );
});

test("deploy provider defaults to docker and rejects unknown values", () => {
  assert.equal(loadConfig({}).deployProvider, "docker");
  assert.equal(loadConfig({ DEPLOY_PROVIDER: "fly", FLY_DEPLOY_API_TOKEN: "test-token" }).deployProvider, "fly");
  assert.throws(() => loadConfig({ DEPLOY_PROVIDER: "flly" }), /DEPLOY_PROVIDER="flly" is not recognized/);
});

test("production and unauthenticated-core escape hatch are parsed once", () => {
  assert.throws(() => loadConfig({ NODE_ENV: "production" }), /missing or insecure required core secrets/);
  assert.equal(loadConfig(productionEnv).production, true);
  assert.equal(loadConfig({}).production, false);
  assert.equal(loadConfig({ ALLOW_UNAUTHENTICATED_CORE: "yes" }).allowUnauthenticatedCore, true);
  assert.throws(() => loadConfig({ ALLOW_UNAUTHENTICATED_CORE: "sometimes" }), /not a recognized boolean/);
});

test("harness security posture defaults to auto and validates named modes", () => {
  assert.equal(loadConfig({}).securityPosture, "auto");
  assert.equal(loadConfig({}).securityScreen, "off");
  assert.equal(loadConfig({}).securityScreenClassifier, "model");
  assert.equal(loadConfig({ SECURITY_SCREEN: "Observe" }).securityScreen, "observe");
  assert.equal(loadConfig({ SECURITY_SCREEN: "enforce" }).securityScreen, "enforce");
  assert.throws(() => loadConfig({ SECURITY_SCREEN: "shadow" }), /SECURITY_SCREEN="shadow" is not recognized/);
  assert.equal(loadConfig({}).securityScreenProxy, undefined);
  assert.equal(loadConfig({}).securityScreenTimeoutMs, 15_000);
  assert.equal(loadConfig({ SECURITY_SCREEN_TIMEOUT_MS: "25" }).securityScreenTimeoutMs, 25);
  assert.equal(loadConfig({ HARNESS_SECURITY_POSTURE: "Dangerous" }).securityPosture, "dangerous");
  assert.equal(loadConfig({ HARNESS_SECURITY_POSTURE: "strict" }).securityPosture, "strict");
  assert.throws(
    () => loadConfig({ HARNESS_SECURITY_POSTURE: "permissive" }),
    /HARNESS_SECURITY_POSTURE="permissive" is not recognized/,
  );
  const proxy = {
    SECURITY_SCREEN: "observe",
    SECURITY_SCREEN_CLASSIFIER: "proxy",
    SECURITY_SCREEN_PROXY_PROVIDER: "example-screen",
    SECURITY_SCREEN_PROXY_ENDPOINT: "https://screen.example.test/classify",
    SECURITY_SCREEN_PROXY_TOKEN: "test-token",
  };
  assert.throws(
    () => loadConfig({ SECURITY_SCREEN_CLASSIFIER: "proxy" }),
    /requires SECURITY_SCREEN_PROXY_PROVIDER, SECURITY_SCREEN_PROXY_ENDPOINT, and SECURITY_SCREEN_PROXY_TOKEN/,
  );
  assert.deepEqual(loadConfig(proxy).securityScreenProxy, {
    provider: "example-screen",
    endpoint: "https://screen.example.test/classify",
    token: "test-token",
  });
  for (const timeout of ["0", "-1", "1.5", "2147483648"]) {
    assert.throws(
      () => loadConfig({ SECURITY_SCREEN_TIMEOUT_MS: timeout }),
      /SECURITY_SCREEN_TIMEOUT_MS must be a positive integer/,
    );
  }
  assert.throws(
    () => loadConfig({ SECURITY_SCREEN_PROXY_PROVIDER: "example-screen" }),
    /requires SECURITY_SCREEN_CLASSIFIER=proxy/,
  );
  for (const provider of ["Bad Provider", "surface", "origin", "-leading", `${"x".repeat(64)}`]) {
    assert.throws(
      () => loadConfig({ ...proxy, SECURITY_SCREEN_PROXY_PROVIDER: provider }),
      /SECURITY_SCREEN_PROXY_PROVIDER/,
    );
  }
  for (const endpoint of [
    "http://screen.example.test/classify",
    "https://user:[email protected]/classify",
    "https://screen.example.test/classify#fragment",
    "https://screen.example.test./classify",
  ]) {
    assert.throws(
      () => loadConfig({ ...proxy, SECURITY_SCREEN_PROXY_ENDPOINT: endpoint }),
      /SECURITY_SCREEN_PROXY_ENDPOINT/,
    );
  }
});

test("retired security screen variables fail loudly unless they only said off", () => {
  assert.equal(loadConfig({ SECURITY_SCREEN_BACKEND: "off" }).securityScreen, "off");
  for (const env of [
    { SECURITY_SCREEN_BACKEND: "proxy" },
    { SECURITY_SCREEN_BACKEND: "off", SECURITY_SCREEN: "observe" },
    { SECURITY_SCREEN_BACKEND: "off", SECURITY_SCREEN_ALL_POSTURES: "false" },
    { SECURITY_SCREEN_ALL_POSTURES: "true" },
    { SECURITY_SCREEN_PROXY_ROLLOUT: "shadow" },
  ]) {
    assert.throws(() => loadConfig(env), /retired.*SECURITY_SCREEN=off\|observe\|enforce/);
  }
});

test("sharing posture defaults to isolated and accepts only isolated or open", () => {
  assert.equal(loadConfig({}).sharingPosture, "isolated");
  assert.equal(loadConfig({ HARNESS_SHARING_POSTURE: "Open" }).sharingPosture, "open");
  assert.equal(loadConfig({ HARNESS_SHARING_POSTURE: "isolated" }).sharingPosture, "isolated");
  assert.throws(
    () => loadConfig({ HARNESS_SHARING_POSTURE: "dangerous" }),
    /HARNESS_SHARING_POSTURE="dangerous" is not recognized/,
  );
});

test("Modal without an egress proxy warns that sandboxes run fail-open", () => {
  const warnings: string[] = [];
  const original = console.warn;
  console.warn = (msg: unknown) => void warnings.push(String(msg));
  try {
    loadConfig({ MODAL_TOKEN_ID: "id", MODAL_TOKEN_SECRET: "secret" });
    loadConfig({ MODAL_TOKEN_ID: "id", MODAL_TOKEN_SECRET: "secret", MODAL_EGRESS_PROXY_URL: "https://egress.test" });
  } finally {
    console.warn = original;
  }
  assert.equal(warnings.filter((w) => w.includes("MODAL_EGRESS_PROXY_URL")).length, 1);
});

test("a leftover *=sqlite env throws (no silent downgrade to ephemeral memory)", () => {
  assert.throws(() => loadConfig({ SESSION_STORE: "sqlite" }), /SESSION_STORE=sqlite is no longer supported/);
  assert.throws(() => loadConfig({ RUN_STORE: "sqlite" }), /RUN_STORE=sqlite is no longer supported/);
  assert.throws(() => loadConfig({ ARTIFACT_STORE: "sqlite" }), /ARTIFACT_STORE=sqlite is no longer supported/);
});

test("a harmless ARTIFACT_STORE=memory (now a dead knob) is ignored, not fatal", () => {
  assert.doesNotThrow(() => loadConfig({ ARTIFACT_STORE: "memory" }));
});

test("boolEnv: one vocabulary for every boolean env knob", () => {
  for (const v of ["1", "true", "yes", "on", "TRUE", " On "]) assert.equal(boolEnv(v), true, v);
  for (const v of ["0", "false", "no", "off", "none", "OFF"]) assert.equal(boolEnv(v), false, v);
  for (const v of [undefined, "", "2", "enabled"]) assert.equal(boolEnv(v), undefined, String(v));
});

test("every boolean knob accepts the shared vocabulary (off means off)", () => {
  const off = loadConfig({
    SEED_SKILLS: "off",
    EXECUTE_SCRATCH: "off",
    REACH_EXEC: "off",
    PI_CAPTURE_REQUESTS: "off",
    EAGER_PROVISION: "off",
  });
  assert.equal(off.seedSkills, false);
  assert.equal(off.eagerProvisionEnabled, false);
  assert.equal(off.scratchExecEnabled, false);
  assert.equal(off.reachExecEnabled, false);
  assert.equal(off.piCaptureRequests, false);

  const on = loadConfig({
    SEED_SKILLS: "yes",
    EXECUTE_SCRATCH: "on",
    REACH_EXEC: "1",
    PI_SYSTEM_CACHE_SPLIT: "on",
  });
  assert.equal(on.seedSkills, true);
  assert.equal(on.scratchExecEnabled, true);
  assert.equal(on.reachExecEnabled, true);
  assert.equal(on.piSystemCacheSplit, true);

  const unset = loadConfig({});
  assert.equal(unset.piCaptureRequests, true, "capture defaults on");
  assert.equal(unset.piSystemCacheSplit, false, "cache split defaults off");
  assert.equal(unset.eagerProvisionEnabled, true, "eager provision defaults on");
});

test("numEnv: empty and non-numeric values fall back instead of poisoning config with NaN", () => {
  assert.equal(numEnv(""), undefined);
  assert.equal(numEnv("abc"), undefined);
  assert.equal(numEnv("42"), 42);
  assert.equal(loadConfig({ PORT: "" }).port, CONFIG_DEFAULTS.port);
});

test("a set-but-unparseable env value refuses to boot instead of silently taking the default", () => {
  assert.throws(() => loadConfig({ WORKERS: "not-a-number" }), /WORKERS="not-a-number" is not a number/);
  assert.throws(() => loadConfig({ BUDGET_USD_PER_WINDOW: "10$" }), /BUDGET_USD_PER_WINDOW="10\$" is not a number/);
  assert.throws(() => loadConfig({ EXECUTE_SCRATCH: "2" }), /EXECUTE_SCRATCH="2" is not a recognized boolean/);
  assert.throws(() => loadConfig({ SANDBOX_BACKEND: "docker" }), /SANDBOX_BACKEND="docker" is not recognized/);
  assert.equal(loadConfig({ WORKERS: "  " }).workers, CONFIG_DEFAULTS.workers);
  assert.equal(loadConfig({ EXECUTE_SCRATCH: "" }).scratchExecEnabled, false);
});

test("Slack HTTP ingress exposes only a valid configured receiver port", () => {
  assert.equal(loadConfig({ SLACK_EVENTS_MODE: "http", SLACK_EVENTS_PORT: "8182" }).slackEventsPort, 8182);
  assert.equal(loadConfig({ SLACK_EVENTS_MODE: "socket", SLACK_EVENTS_PORT: "8182" }).slackEventsPort, undefined);
  assert.throws(
    () => loadConfig({ SLACK_EVENTS_MODE: "http", SLACK_EVENTS_PORT: "70000" }),
    /SLACK_EVENTS_PORT must be an integer from 1 through 65535/,
  );
});

test("sandbox backend is parsed once before production backend guards", () => {
  assert.equal(loadConfig({ SANDBOX_BACKEND: " aws " }).sandboxBackend, "aws");
  assert.throws(
    () => loadConfig({ ...productionEnv, SANDBOX_BACKEND: "bogus" }),
    /SANDBOX_BACKEND="bogus" is not recognized/,
  );
});

test("production refuses to boot on in-memory sessions, runs, approvals, audit, locks and leases", () => {
  const { DATABASE_URL: _databaseUrl, ...noDatabase } = productionEnv;
  assert.throws(
    () => loadConfig(noDatabase),
    /missing or insecure required core secrets: DATABASE_URL \(.*resident disk\)/,
  );
  const { SESSION_STORE: _sessionStore, ...memorySessions } = productionEnv;
  assert.throws(() => loadConfig(memorySessions), /production requires SESSION_STORE=postgres/);
  assert.throws(
    () => loadConfig({ ...productionEnv, RUN_STORE: "memory" }),
    /production requires SESSION_STORE=postgres/,
  );
  assert.doesNotThrow(() => loadConfig({ ...productionEnv, RUN_STORE: "postgres" }));
  assert.doesNotThrow(
    () => loadConfig({ SANDBOX_BACKEND: "local" }),
    "a single-process dev instance needs no durable store",
  );
});

test("production refuses missing, placeholder, or weak signing keys", () => {
  assert.throws(
    () => loadConfig({ NODE_ENV: "production" }),
    /CAPABILITY_SECRET, CONNECTOR_SECRET_KEY, CORE_SIGNING_SECRET, PORTAL_IDENTITY_SECRET, SKILL_SIGNING_SECRET/,
  );
  assert.throws(
    () => loadConfig({ ...productionEnv, CORE_SIGNING_SECRET: "short" }),
    /core secrets: CORE_SIGNING_SECRET$/,
  );
  assert.throws(
    () => loadConfig({ ...productionEnv, CONNECTOR_SECRET_KEY: "short" }),
    /core secrets: CONNECTOR_SECRET_KEY$/,
  );
  assert.throws(
    () => loadConfig({ ...productionEnv, CAPABILITY_SECRET: "replace-me" }),
    /core secrets: CAPABILITY_SECRET$/,
  );
});

test("defaults come from CONFIG_DEFAULTS, set exactly once", () => {
  const def = loadConfig({});
  assert.equal(CONFIG_DEFAULTS.workers, 16);
  assert.equal(def.workers, CONFIG_DEFAULTS.workers);
  assert.equal(def.rateLimitPerWindow, CONFIG_DEFAULTS.rateLimitPerWindow);
  assert.equal(def.rateLimitWindowMs, CONFIG_DEFAULTS.rateLimitWindowMs);
  assert.equal(def.monitorPollMs, CONFIG_DEFAULTS.monitorPollMs);
  assert.equal(def.approvalSummaryTimeoutMs, CONFIG_DEFAULTS.approvalSummaryTimeoutMs);
  assert.equal(def.deployDialTimeoutMs, CONFIG_DEFAULTS.deployDialTimeoutMs);
  assert.equal(def.execTimeoutDefaultMs, CONFIG_DEFAULTS.execTimeoutDefaultSec * 1000);
  assert.equal(def.execTimeoutMaxMs, CONFIG_DEFAULTS.execTimeoutMaxSec * 1000);
  assert.equal(def.backgroundJobTtlMs, CONFIG_DEFAULTS.backgroundJobTtlSec * 1000);
  assert.equal(def.backgroundJobTtlMaxMs, CONFIG_DEFAULTS.backgroundJobTtlMaxSec * 1000);
  assert.equal(def.turnWallClockMs, CONFIG_DEFAULTS.turnWallClockSec * 1000);
  assert.equal(def.turnWallClockMs, 0);
  assert.equal(def.runMaxAgeMs, 24 * 60 * 60_000);
  assert.equal(def.runWaitMs, def.runMaxAgeMs + 60_000);
});

test("turn wall clock config drives run bounds only when capped", () => {
  const capped = loadConfig({ TURN_WALL_CLOCK_SEC: "120" });
  assert.equal(capped.turnWallClockMs, 120_000);
  assert.equal(capped.runMaxAgeMs, 240_000);
  assert.equal(capped.runWaitMs, 180_000);
  const explicit = loadConfig({ TURN_WALL_CLOCK_SEC: "120", RUN_MAX_AGE_MS: "999999" });
  assert.equal(explicit.runMaxAgeMs, 999_999);
});

test("APPROVAL_SUMMARY_TIMEOUT_MS overrides the approval-summary deadline; unset uses the 6s default", () => {
  assert.equal(loadConfig({ APPROVAL_SUMMARY_TIMEOUT_MS: "9000" }).approvalSummaryTimeoutMs, 9000);
  assert.equal(loadConfig({}).approvalSummaryTimeoutMs, 6_000);
});

test("deploy proxy dial timeout is parsed once from config", () => {
  assert.equal(loadConfig({ DEPLOY_DIAL_TIMEOUT_MS: "1234" }).deployDialTimeoutMs, 1234);
  assert.throws(() => loadConfig({ DEPLOY_DIAL_TIMEOUT_MS: "soon" }), /DEPLOY_DIAL_TIMEOUT_MS="soon" is not a number/);
});

test("PUBLIC_API_URL is not treated as the human-facing web URL", () => {
  const apiOnly = loadConfig({ PUBLIC_API_URL: "https://agent-api.example" });
  assert.equal(apiOnly.apiBaseUrl, "https://agent-api.example");
  assert.equal(apiOnly.publicUrl, "https://agent-api.example");
  assert.equal(apiOnly.publicWebUrl, undefined);

  const disabledWeb = loadConfig({ PUBLIC_API_URL: "https://agent-api.example", PUBLIC_WEB_URL: "" });
  assert.equal(disabledWeb.apiBaseUrl, "https://agent-api.example");
  assert.equal(disabledWeb.publicUrl, "https://agent-api.example");
  assert.equal(disabledWeb.publicWebUrl, undefined);

  const web = loadConfig({ PUBLIC_API_URL: "https://agent-api.example", PUBLIC_WEB_URL: "https://portal.example" });
  assert.equal(web.apiBaseUrl, "https://agent-api.example");
  assert.equal(web.publicUrl, "https://portal.example");
  assert.equal(web.publicWebUrl, "https://portal.example");
});

test("plugin skill directories can be overridden or disabled", () => {
  assert.deepEqual(loadConfig({ PLUGIN_SKILLS_DIRS: "plugins/onboarding/skills, custom/skills" }).pluginSkillDirs, [
    resolve("plugins/onboarding/skills"),
    resolve("custom/skills"),
  ]);
  assert.deepEqual(loadConfig({ PLUGIN_SKILLS_DIRS: "0" }).pluginSkillDirs, []);
  assert.deepEqual(loadConfig({}).pluginSkillDirs, [resolve("plugins/onboarding/skills")]);
});

test("HARNESS=pi can boot before an admin configures a model provider", () => {
  assert.doesNotThrow(() => loadConfig({ HARNESS: "pi" }));
  assert.doesNotThrow(() => loadConfig({ HARNESS: "pi", ANTHROPIC_API_KEY: "sk-ant" }));
  assert.doesNotThrow(() => loadConfig({ HARNESS: " pi " }));
  assert.doesNotThrow(() => loadConfig({ HARNESS: " pi ", ANTHROPIC_API_KEY: "sk-ant" }));
  assert.doesNotThrow(() => loadConfig({ ...productionEnv, HARNESS: "pi" }));
  assert.doesNotThrow(() => loadConfig({ ...productionEnv, HARNESS: "pi", ANTHROPIC_API_KEY: "sk-ant" }));
});

test("HARNESS accepts only the remaining harnesses", () => {
  for (const harness of ["codex", " opencode ", "mock"])
    assert.throws(() => loadConfig({ HARNESS: harness }), /is not recognized — use pi or claude/);
  assert.equal(loadConfig({ HARNESS: "pi" }).harness, "pi");
  assert.equal(loadConfig({}).harness, "pi");
});

test("HARNESS=claude uses native Claude authentication and does not require an Anthropic key", () => {
  assert.doesNotThrow(() => loadConfig({ HARNESS: "claude" }));
  assert.equal(loadConfig({ HARNESS: "claude", CLAUDE_MODEL: "claude-opus-4-8" }).claudeModel, "claude-opus-4-8");
});

test("SUPERSERVE_CONFIG_GENERATION accepts only nonnegative safe integers", () => {
  for (const value of ["9007199254740993", "-1", "0.5", "NaN", "Infinity"]) {
    assert.throws(() => loadConfig({ SUPERSERVE_CONFIG_GENERATION: value }), /SUPERSERVE_CONFIG_GENERATION/, value);
  }
  for (const value of [undefined, "", "  "]) {
    assert.equal(loadConfig({ SUPERSERVE_CONFIG_GENERATION: value }).superserveSandbox.configGeneration, undefined);
  }
  for (const value of ["0", "7", " 42 ", String(Number.MAX_SAFE_INTEGER)]) {
    assert.equal(loadConfig({ SUPERSERVE_CONFIG_GENERATION: value }).superserveSandbox.configGeneration, Number(value));
  }
});

test("SANDBOX_BACKEND: unset defaults to local (dev only); the retired secondary variable is tolerated", () => {
  assert.equal(loadConfig({}).sandboxBackend, "local");
  assert.throws(
    () => loadConfig({ ...productionEnv, SANDBOX_BACKEND: undefined }),
    /SANDBOX_BACKEND must be set explicitly in production/,
  );
  assert.throws(() => loadConfig({ SANDBOX_BACKEND: "sprites" }), /SPRITES_TOKEN/);
  assert.throws(() => loadConfig({ SANDBOX_BACKEND: "agent37" }), /AGENT37_API_KEY/);
  assert.equal(loadConfig({ SANDBOX_BACKEND: "agent37", AGENT37_API_KEY: "sk_live_k" }).sandboxBackend, "agent37");
  assert.throws(() => loadConfig({ SANDBOX_BACKEND: "superserve" }), /SUPERSERVE_API_KEY/);
  assert.throws(
    () => loadConfig({ SANDBOX_BACKEND: "superserve", SUPERSERVE_API_KEY: "ss_live_k" }),
    /SUPERSERVE_TEMPLATE/,
  );
  assert.throws(
    () => loadConfig({ SANDBOX_BACKEND: " superserve ", SUPERSERVE_API_KEY: "ss_live_k" }),
    /SUPERSERVE_TEMPLATE/,
  );
  assert.throws(
    () => loadConfig({ SANDBOX_BACKEND: " superserve ", SUPERSERVE_TEMPLATE: "qm-agent-1.0.0" }),
    /SUPERSERVE_API_KEY/,
  );
  assert.equal(
    loadConfig({
      SANDBOX_BACKEND: "superserve",
      SUPERSERVE_TEMPLATE: "qm-agent-1.0.0",
      SUPERSERVE_API_KEY: "ss_live_k",
      SUPERSERVE_CONFIG_GENERATION: "7",
    }).superserveSandbox.configGeneration,
    7,
    "a deployment that tracks its own rollouts stamps the generation its sandboxes carry",
  );
  assert.throws(
    () =>
      loadConfig({
        SANDBOX_BACKEND: "superserve",
        SUPERSERVE_TEMPLATE: "qm-agent-1.0.0",
        SUPERSERVE_API_KEY: "ss_live_k",
        SUPERSERVE_CONFIG_GENERATION: "later",
      }),
    /SUPERSERVE_CONFIG_GENERATION/,
  );
  assert.equal(
    loadConfig({
      SANDBOX_BACKEND: "superserve",
      SUPERSERVE_TEMPLATE: "qm-agent-1.0.0",
      SUPERSERVE_API_KEY: "ss_live_k",
    }).sandboxBackend,
    "superserve",
  );
  assert.doesNotThrow(
    () =>
      loadConfig({
        SANDBOX_BACKEND: "superserve",
        SUPERSERVE_TEMPLATE: "qm-agent-1.0.0",
        SUPERSERVE_API_KEY: "ss_live_k",
      }),
    "a single-process dev instance needs no durable store",
  );
  assert.ok(
    !enabledSandboxBackends(loadConfig({ SANDBOX_BACKEND: "local", SUPERSERVE_API_KEY: "ss_live_k" })).includes(
      "superserve",
    ),
    "a stray key without a template must not enable the secondary backend",
  );
  assert.doesNotThrow(
    () =>
      loadConfig({
        SANDBOX_BACKEND: "local",
        SANDBOX_SCOPE_BACKENDS: '{"channel":"superserve"}',
        SUPERSERVE_API_KEY: "ss_live_k",
      }),
    "upgrade-only provider settings do not require live credentials or templates",
  );
  assert.doesNotThrow(() =>
    loadConfig({
      SANDBOX_BACKEND: "local",
      SANDBOX_SCOPE_BACKENDS: '{"channel":"superserve"}',
      SUPERSERVE_TEMPLATE: "qm-agent-1.0.0",
      SUPERSERVE_API_KEY: "ss_live_k",
    }),
  );
  assert.ok(
    enabledSandboxBackends(
      loadConfig({ SANDBOX_BACKEND: "local", SUPERSERVE_API_KEY: "ss_live_k", SUPERSERVE_TEMPLATE: "qm-agent-1.0.0" }),
    ).includes("superserve"),
  );
  assert.ok(
    !enabledSandboxBackends(
      loadConfig({ SANDBOX_BACKEND: "local", SUPERSERVE_API_KEY: "ss_live_k", SUPERSERVE_TEMPLATE: "   " }),
    ).includes("superserve"),
    "a blank template must not enable the secondary backend",
  );
  assert.equal(
    loadConfig({
      SANDBOX_BACKEND: "superserve",
      SUPERSERVE_TEMPLATE: " qm-agent-1.0.0 ",
      SUPERSERVE_API_KEY: "ss_live_k",
    }).superserveSandbox?.template,
    "qm-agent-1.0.0",
  );
  const config = loadConfig({ SANDBOX_SECONDARY_BACKEND: "smolmachines" });
  assert.equal(config.sandboxBackend, "local");
  assert.ok(!("sandboxSecondaryBackend" in config));
});

test("Fly identity and Slack runtime settings are parsed once into Config", () => {
  const config = loadConfig({
    FLY_APP_NAME: "qm-core",
    SLACK_BOT_TOKEN: "xoxb-test",
    SLACK_APP_TOKEN: "xapp-test",
    SLACK_API_URL: "https://slack.example/api",
  });
  assert.equal(config.flyAppName, "qm-core");
  assert.deepEqual(config.slack, {
    botToken: "xoxb-test",
    appToken: "xapp-test",
    apiUrl: "https://slack.example/api",
  });
});

test("maxClaims defaults from CONFIG_DEFAULTS and MAX_CLAIMS overrides", () => {
  assert.equal(loadConfig({}).maxClaims, CONFIG_DEFAULTS.maxClaims);
  assert.equal(loadConfig({ MAX_CLAIMS: "5" }).maxClaims, 5);
  assert.throws(() => loadConfig({ MAX_CLAIMS: "lots" }), /MAX_CLAIMS="lots" is not a number/);
});

test("MODEL_PROVIDER declares the vendor that bills the base model", () => {
  assert.equal(loadConfig({}).modelProvider, undefined);
  assert.equal(loadConfig({ MODEL_PROVIDER: " openrouter ", OPENROUTER_API_KEY: "k" }).modelProvider, "openrouter");
  assert.throws(() => loadConfig({ MODEL_PROVIDER: "bedrock" }), /MODEL_PROVIDER.*not recognized/);
});

test("MODEL_PROVIDER is refused when the harness can never run that vendor's models", () => {
  assert.throws(
    () => loadConfig({ MODEL_PROVIDER: "openrouter", HARNESS: "claude", OPENROUTER_API_KEY: "k" }),
    /cannot serve a base model on HARNESS=claude/,
  );
  assert.throws(
    () => loadConfig({ MODEL_PROVIDER: "openai", HARNESS: "claude", OPENAI_API_KEY: "k" }),
    /cannot serve a base model on HARNESS=claude/,
  );
  assert.equal(loadConfig({ MODEL_PROVIDER: "openai", HARNESS: "pi", OPENAI_API_KEY: "k" }).modelProvider, "openai");
});

test("baseModelProviders constrains the base model only when a provider is declared", () => {
  assert.deepEqual(
    baseModelProviders(loadConfig({ MODEL_PROVIDER: "openrouter", OPENROUTER_API_KEY: "k", ANTHROPIC_API_KEY: "k" })),
    { anthropic: false, openai: false, openrouter: true },
    "the declaration outranks a stray key from another vendor",
  );
  assert.equal(
    baseModelProviders(loadConfig({ OPENROUTER_API_KEY: "k" })),
    undefined,
    "with no declaration the shipped default stands, so upgrading never moves a deployment's model or its billing",
  );
});

test("DEPLOY_PROVIDER=porter selects the Porter deploy provider and reads its env", () => {
  const config = loadConfig({
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com",
    PORTER_DEPLOY_RUNNER_IMAGE: "ghcr.io/x/runner:1",
    PORTER_DEPLOY_VISIBILITY: "private",
    PORTER_DEPLOY_TTL_SEC: "3600",
  });
  assert.equal(config.deployProvider, "porter");
  assert.deepEqual(config.porterDeploy, {
    token: "tok",
    baseUrl: "https://dashboard.porter.run/api/v2/alpha/projects/7/clusters/9",
    runnerImage: "ghcr.io/x/runner:1",
    appsDomain: "apps.example.com",
    visibility: "private",
    ttlSec: 3600,
  });
});

test("DEPLOY_PROVIDER=e2b selects E2B publishing and needs the E2B key", () => {
  assert.equal(loadConfig({ DEPLOY_PROVIDER: "e2b", E2B_API_KEY: "e2b-key" }).deployProvider, "e2b");
  assert.throws(() => loadConfig({ DEPLOY_PROVIDER: "e2b" }), /missing or insecure required core secrets: E2B_API_KEY/);
});

test("the deploy runner image falls back to the sandbox image", () => {
  const config = loadConfig({
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com",
    PORTER_SANDBOX_IMAGE: "localhost:5000/qm-sandbox:latest",
  });
  assert.equal(config.porterDeploy.runnerImage, "localhost:5000/qm-sandbox:latest");
});

test("DEPLOY_PROVIDER=porter refuses to boot without a cluster and tolerates a missing apps domain", () => {
  assert.throws(
    () =>
      loadConfig({
        DEPLOY_PROVIDER: "porter",
        PORTER_DEPLOY_API_TOKEN: "tok",
        PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com",
      }),
    /PORTER_DEPLOY_PROJECT_ID/,
  );
  assert.equal(
    loadConfig({
      DEPLOY_PROVIDER: "porter",
      PORTER_DEPLOY_API_TOKEN: "tok",
      PORTER_DEPLOY_PROJECT_ID: "7",
      PORTER_DEPLOY_CLUSTER_ID: "9",
    }).porterDeploy.appsDomain,
    undefined,
  );
  assert.throws(
    () =>
      loadConfig({
        DEPLOY_PROVIDER: "porter",
        PORTER_DEPLOY_API_TOKEN: "tok",
        PORTER_DEPLOY_PROJECT_ID: "7",
        PORTER_DEPLOY_CLUSTER_ID: "9",
        PORTER_DEPLOY_APPS_DOMAIN: "a.b",
        PORTER_DEPLOY_VISIBILITY: "hidden",
      }),
    /PORTER_DEPLOY_VISIBILITY/,
  );
});

test("PORTER_DEPLOY_VISIBILITY=internal is accepted and refuses an apps domain", () => {
  const cluster = {
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    PORTER_DEPLOY_VISIBILITY: "internal",
  };
  assert.equal(loadConfig(cluster).porterDeploy.visibility, "internal");
  assert.throws(
    () => loadConfig({ ...cluster, PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com" }),
    /PORTER_DEPLOY_APPS_DOMAIN/,
  );
});

test("SANDBOX_BACKEND=porter locates the API and shares the deploy provider's token", () => {
  assert.throws(
    () => loadConfig({ SANDBOX_BACKEND: "porter", PORTER_DEPLOY_API_TOKEN: "tok" }),
    /PORTER_DEPLOY_PROJECT_ID/,
  );
  const inCluster = loadConfig({
    SANDBOX_BACKEND: "porter",
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_CLUSTER_ID: "3",
    PORTER_SANDBOX_TTL_SEC: "120",
  });
  assert.equal(inCluster.porterSandbox.token, "tok");
  assert.equal(inCluster.porterSandbox.ttlSec, 120);
  assert.equal(inCluster.porterDeploy.token, "tok");
});

test("DEPLOY_APPS_DOMAIN is the one-var apps setup: it feeds the gate and defaults every provider's domain", () => {
  const config = loadConfig({
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    DEPLOY_APPS_DOMAIN: "apps.example.com",
    AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
  });
  assert.equal(config.deployAppsDomain, "apps.example.com");
  assert.equal(
    config.porterDeploy.appsDomain,
    undefined,
    "the gate domain must not be registered on Porter ingress — that would bypass the gate or loop the proxy",
  );
  assert.equal(config.awsDeploy.appsDomain, "apps.example.com");
  const overridden = loadConfig({
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    DEPLOY_APPS_DOMAIN: "apps.example.com",
    PORTER_DEPLOY_APPS_DOMAIN: "apps.other.example.com",
    AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
  });
  assert.equal(overridden.porterDeploy.appsDomain, "apps.other.example.com");
  assert.equal(overridden.deployAppsDomain, "apps.example.com");
});

test("the active provider's own apps domain reaches the gate when DEPLOY_APPS_DOMAIN is unset", () => {
  const porter = loadConfig({
    DEPLOY_PROVIDER: "porter",
    PORTER_DEPLOY_API_TOKEN: "tok",
    PORTER_DEPLOY_PROJECT_ID: "7",
    PORTER_DEPLOY_CLUSTER_ID: "9",
    PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com",
  });
  assert.equal(porter.deployAppsDomain, "apps.example.com");
  assert.equal(porter.awsDeploy.appsDomain, undefined);
  assert.equal(porter.porterDeploy.appsDomain, "apps.example.com");
  const aws = loadConfig({
    AWS_DEPLOY_APPS_DOMAIN: "apps.example.com",
    AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
  });
  assert.equal(aws.deployAppsDomain, "apps.example.com");
  assert.equal(loadConfig({}).deployAppsDomain, undefined);
});

test("DEPLOY_APPS_DOMAIN refuses shared platform domains that cannot carry per-app subdomains", () => {
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.onporter.run" }), /shared platform domain/);
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.fly.dev" }), /shared platform domain/);
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "8081-abc.e2b.app" }), /shared platform domain/);
  assert.equal(
    loadConfig({ DEPLOY_APPS_DOMAIN: "apps.example.com", AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef" })
      .deployAppsDomain,
    "apps.example.com",
  );
});

test("DEPLOY_APPS_DOMAIN must be a bare DNS name, normalized to lowercase without a trailing dot", () => {
  const gate = { AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef" };
  assert.equal(loadConfig({ DEPLOY_APPS_DOMAIN: "Apps.Example.COM.", ...gate }).deployAppsDomain, "apps.example.com");
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "https://apps.example.com", ...gate }), /bare domain/);
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "apps.example.com:[email protected]", ...gate }), /bare domain/);
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "*.apps.example.com", ...gate }), /bare domain/);
  assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.fly.dev.", ...gate }), /shared platform domain/);
});

test("the portal session secret doubles as the deploy-apps viewer secret when a login URL exists", () => {
  const derived = loadConfig({ PORTAL_SESSION_SECRET: "shared", PUBLIC_WEB_URL: "https://qm.example.com" });
  assert.equal(derived.deployAppsSessionSecret, "shared");
  assert.equal(derived.deployAppsLoginUrl, "https://qm.example.com");
  const noUrl = loadConfig({ PORTAL_SESSION_SECRET: "shared" });
  assert.equal(
    noUrl.deployAppsSessionSecret,
    undefined,
    "no sign-in address means the fallback stays off, not a throw",
  );
  const explicit = loadConfig({
    PORTAL_SESSION_SECRET: "shared",
    DEPLOY_APPS_SESSION_SECRET: "own",
    PUBLIC_WEB_URL: "https://qm.example.com",
  });
  assert.equal(explicit.deployAppsSessionSecret, "own");
});

test("the deploy-apps sign-in address defaults to the public web URL", () => {
  const derived = loadConfig({
    DEPLOY_APPS_SESSION_SECRET: "s",
    PUBLIC_WEB_URL: "https://qm.example.com/",
  });
  assert.equal(derived.deployAppsLoginUrl, "https://qm.example.com");
  assert.equal(derived.deployAppsSessionSecret, "s");
  assert.equal(derived.deployAppsLoginPath, "/auth/login");
  assert.equal(
    loadConfig({
      DEPLOY_APPS_SESSION_SECRET: "s",
      PUBLIC_WEB_URL: "https://qm.example.com",
      DEPLOY_APPS_LOGIN_PATH: "/auth/trusted/login",
    }).deployAppsLoginPath,
    "/auth/trusted/login",
  );
  assert.throws(() => loadConfig({ DEPLOY_APPS_LOGIN_PATH: "//evil.example" }), /DEPLOY_APPS_LOGIN_PATH/);
  const explicit = loadConfig({
    DEPLOY_APPS_SESSION_SECRET: "s",
    DEPLOY_APPS_LOGIN_URL: "https://portal.example.com/",
    PUBLIC_WEB_URL: "https://qm.example.com",
  });
  assert.equal(explicit.deployAppsLoginUrl, "https://portal.example.com");
  assert.throws(() => loadConfig({ DEPLOY_APPS_SESSION_SECRET: "s" }), /DEPLOY_APPS_LOGIN_URL or PUBLIC_WEB_URL/);
  assert.throws(
    () => loadConfig({ DEPLOY_APPS_LOGIN_URL: "https://portal.example.com" }),
    /requires DEPLOY_APPS_SESSION_SECRET/,
  );
});

test("company ChatGPT auth satisfies model onboarding on any harness without an API key", () => {
  const config = { ...loadConfig({}), harness: "pi" as const };
  assert.equal(harnessCarriedModelAuth({ ...config, codexAuthCredential: "cred-1" }), "openai");
  assert.equal(harnessCarriedModelAuth({ ...config, codexAuthFile: "/local/auth.json" }), "openai");
  assert.equal(harnessCarriedModelAuth({ ...config, harness: "claude", codexAuthCredential: "cred-1" }), "openai");
  assert.equal(harnessCarriedModelAuth(config), undefined);
  assert.equal(providerKeysPresent({ ...config, codexAuthCredential: "cred-1" }).codexOAuth, true);
  assert.equal(
    loadConfig({ MODEL_PROVIDER: "openai", HARNESS: "pi", CODEX_AUTH_CREDENTIAL: "cred-1" }).codexAuthCredential,
    "cred-1",
  );
});

test("CODEX_AUTH_FILE is a validated local-only company ChatGPT login", (t) => {
  const dir = mkdtempSync(join(tmpdir(), "qm-config-chatgpt-"));
  t.after(() => rmSync(dir, { recursive: true, force: true }));
  const path = join(dir, "auth.json");
  const claims = Buffer.from(JSON.stringify({ "https://api.openai.com/auth": { chatgpt_account_id: "acct" } }));
  const token = `e30.${claims.toString("base64url")}.sig`;
  writeFileSync(path, JSON.stringify({ auth_mode: "chatgpt", tokens: { access_token: token, refresh_token: "r" } }));
  chmodSync(path, 0o600);
  assert.equal(loadConfig({ HARNESS: "pi", CODEX_AUTH_FILE: path }).codexAuthFile, path);
  assert.throws(
    () => loadConfig({ HARNESS: "pi", CODEX_AUTH_FILE: join(dir, "missing.json") }),
    /CODEX_AUTH_FILE is unusable: .*ENOENT/,
  );
  assert.equal(
    loadConfig({ HARNESS: "pi", CODEX_AUTH_FILE: path, CODEX_AUTH_CREDENTIAL: "cred-1" }).codexAuthFile,
    undefined,
  );
  assert.throws(
    () => loadConfig({ ...productionEnv, CODEX_AUTH_FILE: path }),
    /CODEX_AUTH_FILE is supported for local development only/,
  );
  chmodSync(path, 0o644);
  assert.throws(() => loadConfig({ HARNESS: "pi", CODEX_AUTH_FILE: path }), /must not be readable by group or others/);
});

test("retired brain environment does not configure a runtime integration and warns once", () => {
  const warnings: string[] = [];
  const original = console.warn;
  console.warn = (msg: unknown) => void warnings.push(String(msg));
  let config;
  try {
    config = loadConfig({
      BRAIN: "mcp",
      BRAIN_MCP_URL: "https://unused.invalid",
      BRAIN_RO_CLIENT_ID: "retired",
      BRAIN_RO_CLIENT_SECRET: "retired",
    });
  } finally {
    console.warn = original;
  }
  assert.deepEqual({ ...config, layerEnv: {} }, loadConfig({}));
  const retired = warnings.filter((w) => w.includes("retired and ignored"));
  assert.equal(retired.length, 1);
  assert.match(retired[0]!, /BRAIN, BRAIN_MCP_URL, BRAIN_RO_CLIENT_ID are retired/);
  assert.match(retired[0]!, /MEMORY_PROVIDER_CONFIG/);
});

test("Modal native retention and interval configuration are independent of legacy portable checkpoint throttling", () => {
  const config = loadConfig({
    MODAL_NATIVE_SNAPSHOT_INTERVAL_SEC: "60",
    MODAL_SNAPSHOT_RETENTION_SEC: "86400",
    MODAL_SNAPSHOT_INTERVAL_SEC: "315360000",
  });
  assert.equal(config.modalSandbox.nativeSnapshotIntervalSec, 60);
  assert.equal(config.modalSandbox.snapshotRetentionSec, 86400);
  assert.equal(config.modalSandbox.snapshotIntervalSec, 315360000);
});

test("Smolmachines lifecycle, egress, and snapshot knobs are parsed into Config", () => {
  const config = loadConfig({
    SMOLMACHINES_TOKEN: "smk_test",
    SMOLMACHINES_AUTOSTOP_SEC: "900",
    SMOLMACHINES_EGRESS_PROXY_URL: "https://proxy.example.com",
    SMOLMACHINES_SNAPSHOT_S3_BUCKET: "qm-home-snapshots",
    SMOLMACHINES_SNAPSHOT_INTERVAL_SEC: "300",
  });
  assert.equal(config.smolmachinesSandbox.autoStopSec, 900);
  assert.equal(config.smolmachinesSandbox.egressProxyUrl, "https://proxy.example.com");
  assert.equal(config.smolmachinesSandbox.snapshotS3Bucket, "qm-home-snapshots");
  assert.equal(config.smolmachinesSandbox.snapshotIntervalSec, 300);
  const bare = loadConfig({ SMOLMACHINES_TOKEN: "smk_test" });
  assert.equal(bare.smolmachinesSandbox.autoStopSec, undefined);
  assert.equal(bare.smolmachinesSandbox.snapshotS3Bucket, undefined);
  assert.throws(() => loadConfig({ SMOLMACHINES_AUTOSTOP_SEC: "soon" }), /SMOLMACHINES_AUTOSTOP_SEC/);
});

test("Modal native activation is default-off and uses strict boolean configuration", () => {
  assert.equal(loadConfig({}).modalSandbox.nativeSnapshotsEnabled, false);
  for (const value of ["true", "on", "1"])
    assert.equal(loadConfig({ MODAL_NATIVE_SNAPSHOTS_ENABLED: value }).modalSandbox.nativeSnapshotsEnabled, true);
  assert.throws(() => loadConfig({ MODAL_NATIVE_SNAPSHOTS_ENABLED: "enable" }), /not a recognized boolean/);
});

test("direct Files initiation defaults off and requires explicit activation", () => {
  assert.equal(loadConfig({}).filesDirectUploadsEnabled, false);
  assert.equal(loadConfig({ FILES_DIRECT_UPLOADS_ENABLED: "true" }).filesDirectUploadsEnabled, true);
  assert.equal(loadConfig({ FILES_DIRECT_UPLOADS_ENABLED: "false" }).filesDirectUploadsEnabled, false);
  assert.throws(() => loadConfig({ FILES_DIRECT_UPLOADS_ENABLED: "maybe" }));
});

test("suggestion generation defaults on and can be explicitly disabled", () => {
  assert.equal(loadConfig({}).suggestedActivitiesEnabled, true);
  assert.equal(loadConfig({ SUGGESTED_ACTIVITIES_ENABLED: "true" }).suggestedActivitiesEnabled, true);
  assert.equal(loadConfig({ SUGGESTED_ACTIVITIES_ENABLED: "false" }).suggestedActivitiesEnabled, false);
  assert.throws(() => loadConfig({ SUGGESTED_ACTIVITIES_ENABLED: "maybe" }));
});

test("legacy sandbox scope defaults are parsed for upgrade", () => {
  const credentials = {
    SPRITES_TOKEN: "unit-test-sprites",
    MODAL_TOKEN_ID: "unit-test-modal-id",
    MODAL_TOKEN_SECRET: "unit-test-modal-secret",
  };
  assert.deepEqual(
    loadConfig({ ...credentials, SANDBOX_SCOPE_BACKENDS: '{"personal":"modal","channel":"sprites"}' })
      .legacySandboxScopeDefaults,
    { personal: "modal", channel: "sprites" },
  );
  for (const value of [
    "[]",
    "null",
    '{"personal:someone":"modal"}',
    '{"personal":"missing"}',
    '{"unknown":"sprites"}',
    '{"personal":""}',
  ])
    assert.throws(() => loadConfig({ ...credentials, SANDBOX_SCOPE_BACKENDS: value }));
});

test("Fly shared application name is passed to the deployment provider", () => {
  const config = loadConfig({
    DEPLOY_PROVIDER: "fly",
    FLY_DEPLOY_SHARED_APP_NAME: "qm-example-apps",
    FLY_DEPLOY_WIREGUARD_PEERS: "[]",
    FLY_DEPLOY_API_TOKEN: "test-token",
    FLY_DEPLOY_DATA_VOLUME_SIZE_GB: "1",
  });
  assert.equal(config.flyDeploy.sharedAppName, "qm-example-apps");
  assert.equal(config.flyDeploy.dataVolumeSizeGb, 1);
});

test("background ownership requires durable storage and an independent deployment authority", () => {
  const env = {
    BACKGROUND_DEPLOYMENT_ID: "core:release-a",
    DATABASE_URL: "postgres://localhost/test",
    CORE_SIGNING_SECRET: "source-signing-secret-0123456789abcdef",
    DEPLOYMENT_CONTROL_SECRET: "deployment-control-secret-0123456789abcdef",
  };
  const config = loadConfig(env);
  assert.equal(config.backgroundDeploymentId, env.BACKGROUND_DEPLOYMENT_ID);
  assert.equal(config.deploymentControlSecret, env.DEPLOYMENT_CONTROL_SECRET);
  assert.equal(config.backgroundWorkEnabled, true);
  assert.equal(loadConfig({ ...env, BACKGROUND_WORK_ENABLED: "false" }).backgroundWorkEnabled, false);
  assert.throws(() => loadConfig({ ...env, DATABASE_URL: "" }), /DATABASE_URL/);
  assert.throws(() => loadConfig({ ...env, DEPLOYMENT_CONTROL_SECRET: "short" }), /distinct DEPLOYMENT_CONTROL_SECRET/);
  assert.throws(
    () => loadConfig({ ...env, DEPLOYMENT_CONTROL_SECRET: env.CORE_SIGNING_SECRET }),
    /distinct DEPLOYMENT_CONTROL_SECRET/,
  );
  assert.throws(() => loadConfig({ ...env, BACKGROUND_DEPLOYMENT_ID: " " }), /BACKGROUND_DEPLOYMENT_ID/);
  assert.throws(() => loadConfig({ ...env, CORE_SIGNING_SECRET: "short" }), /CORE_SIGNING_SECRET/);
  assert.throws(() => loadConfig({ ...env, DEPLOYMENT_CONTROL_SECRET: " ".repeat(32) }), /DEPLOYMENT_CONTROL_SECRET/);
});

test("the spend export token is optional, strong, trimmed and never shared with another environment value", () => {
  const env = {
    CORE_SIGNING_SECRET: "source-signing-secret-0123456789abcdef",
    CAPABILITY_SECRET: "capability-secret-0123456789abcdef0123",
    PORTAL_IDENTITY_SECRET: "portal-identity-secret-0123456789abcdef",
    SPEND_EXPORT_TOKEN: "spend-export-token-0123456789abcdef01234",
  };
  assert.equal(loadConfig(env).spendExportToken, env.SPEND_EXPORT_TOKEN);
  assert.equal(loadConfig({ ...env, SPEND_EXPORT_TOKEN: undefined }).spendExportToken, undefined);
  assert.equal(loadConfig({ ...env, SPEND_EXPORT_TOKEN: "" }).spendExportToken, undefined);
  assert.throws(() => loadConfig({ ...env, SPEND_EXPORT_TOKEN: "short" }), /SPEND_EXPORT_TOKEN/);
  assert.throws(() => loadConfig({ ...env, SPEND_EXPORT_TOKEN: " ".repeat(40) }), /SPEND_EXPORT_TOKEN/);
  assert.throws(() => loadConfig({ ...env, SPEND_EXPORT_TOKEN: `${env.SPEND_EXPORT_TOKEN}\n` }), /SPEND_EXPORT_TOKEN/);
  for (const name of [
    "CORE_SIGNING_SECRET",
    "QM_SLACK_SERVICE_TOKEN",
    "DEPLOY_APPS_SESSION_SECRET",
    "SLACK_SIGNING_SECRET",
  ]) {
    assert.throws(
      () => loadConfig({ ...env, [name]: env.SPEND_EXPORT_TOKEN }),
      /SPEND_EXPORT_TOKEN/,
      `${name} must not share the export token`,
    );
  }
});

test("sandbox capability TTL is deployment-configurable with a 48-hour default", () => {
  assert.equal(loadConfig({}).sandboxCapabilityTtlMs, 48 * 3_600_000);
  assert.equal(loadConfig({ SANDBOX_CAPABILITY_TTL_HOURS: "168" }).sandboxCapabilityTtlMs, 168 * 3_600_000);
  assert.equal(loadConfig({ SANDBOX_CAPABILITY_TTL_HOURS: "none" }).sandboxCapabilityTtlMs, 0);
  assert.throws(() => loadConfig({ SANDBOX_CAPABILITY_TTL_HOURS: "-5" }), /SANDBOX_CAPABILITY_TTL_HOURS/);
});

test("a subscription-only company ChatGPT deployment defaults pi to the plain Codex model id", () => {
  const base = { ...loadConfig({}), harness: "pi" as const };
  assert.equal(configuredModelForHarness({ ...base, codexAuthCredential: "cred-1" }, "pi"), "gpt-5.6-sol");
  assert.equal(
    configuredModelForHarness({ ...base, codexAuthCredential: "cred-1", anthropicApiKey: "k" }, "pi"),
    undefined,
  );
  assert.equal(
    configuredModelForHarness({ ...base, codexAuthCredential: "cred-1", modelId: "claude-opus-5" }, "pi"),
    "claude-opus-5",
  );
  assert.equal(configuredModelForHarness(base, "pi"), undefined);
});