Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(advisor): pin the Claude login arm and billing by signing arm

Jack Rudenkoe6bbdda521d8
diff --git a/packages/cli/src/claude-runner-auth-control.test.ts b/packages/cli/src/claude-runner-auth-control.test.ts
new file mode 100644
index 00000000..170e1e5c
--- /dev/null
+++ b/packages/cli/src/claude-runner-auth-control.test.ts
@@ -0,0 +1,218 @@
+import { beforeAll, describe, expect, test } from "bun:test";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import type { ChildAuthControlDeps } from "./claude-runner.js";
+import type { ClaudishConfig } from "./types.js";
+
+const FILE = import.meta.path;
+const TIMEOUT = 20_000;
+
+if (process.env.T4_ISOLATED_FILE !== FILE) {
+ test(
+ "T4 G1 runs with an isolated HOME and no inherited credentials",
+ async () => {
+ const home = mkdtempSync(join(tmpdir(), "t4-auth-control-"));
+ const env = { ...process.env };
+ for (const key of Object.keys(env)) {
+ if (/^(ANTHROPIC|CLAUDE|CLAUDISH|OPENAI|OP_)/.test(key)) delete env[key];
+ }
+ const child = Bun.spawn([process.execPath, "test", FILE], {
+ cwd: home,
+ env: {
+ ...env,
+ HOME: home,
+ CLAUDE_CONFIG_DIR: join(home, "claude"),
+ T4_ISOLATED_FILE: FILE,
+ },
+ stdin: "ignore",
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ try {
+ const [code, stdout, stderr] = await Promise.all([
+ child.exited,
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ ]);
+ console.info(stdout + stderr);
+ expect(code).toBe(0);
+ } finally {
+ child.kill();
+ rmSync(home, { recursive: true, force: true });
+ }
+ },
+ TIMEOUT
+ );
+} else {
+ let childAuthControl: typeof import("./claude-runner.js").childAuthControl;
+ let placeholder: string;
+ const cwd = join(process.env.HOME!, "project");
+ const settingsPath = join(cwd, "auth-settings.json");
+ const firstPath = join(cwd, "first-settings.json");
+ const token = "OPAQUE_TEST_TOKEN_abc";
+ const config = (overrides: Partial<ClaudishConfig> = {}): ClaudishConfig => ({
+ autoApprove: false,
+ dangerous: false,
+ interactive: false,
+ debug: false,
+ logLevel: "minimal",
+ quiet: true,
+ jsonOutput: false,
+ monitor: false,
+ stdin: false,
+ noLogs: true,
+ diagMode: "off",
+ claudeArgs: [],
+ advisor: true,
+ // A native role mapping forwards real auth rather than a foreign-model placeholder.
+ model: "cx@t4-structural-model",
+ modelHaiku: "haiku",
+ ...overrides,
+ });
+ const reads = (files: Record<string, unknown> = {}): ChildAuthControlDeps => ({
+ cwd,
+ home: process.env.HOME!,
+ readFile(path) {
+ if (!(path in files)) throw new Error("ENOENT");
+ return JSON.stringify(files[path]);
+ },
+ });
+
+ beforeAll(async () => {
+ globalThis.fetch = (() => {
+ throw new Error("T4 forbids network");
+ }) as unknown as typeof fetch;
+ ({ childAuthControl, CLAUDISH_PLACEHOLDER_AUTH_TOKEN: placeholder } = await import(
+ "./claude-runner.js"
+ ));
+ });
+
+ describe("G1 childAuthControl — A9 and every forwarded --settings", () => {
+ test(
+ "native role mapping without another auth source is controlled",
+ () => {
+ expect(childAuthControl(config(), {}, reads())).toEqual({ controlled: true });
+ },
+ TIMEOUT
+ );
+
+ test(
+ "foreign main model without a native mapping is not controlled",
+ () => {
+ expect(childAuthControl(config({ modelHaiku: undefined }), {}, reads())).toMatchObject({
+ controlled: false,
+ reason: "proxy-auth",
+ });
+ },
+ TIMEOUT
+ );
+
+ test(
+ "a forwarded user token is not controlled, even on a native mapping",
+ () => {
+ const result = childAuthControl(config(), { ANTHROPIC_AUTH_TOKEN: token }, reads());
+ expect(result).toMatchObject({ controlled: false, reason: "user-auth-token" });
+ expect(JSON.stringify(result)).not.toContain(token);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "a claudish placeholder cannot prove the child uses a login",
+ () => {
+ expect(
+ childAuthControl(
+ config({ modelHaiku: undefined }),
+ {
+ ANTHROPIC_AUTH_TOKEN: placeholder,
+ },
+ reads()
+ )
+ ).toMatchObject({ controlled: false, reason: "proxy-auth" });
+ },
+ TIMEOUT
+ );
+
+ for (const [label, value, reason] of [
+ ["token", { env: { ANTHROPIC_AUTH_TOKEN: token } }, "settings-auth-token"],
+ ["apiKeyHelper", { apiKeyHelper: "T4-never-executed-helper" }, "api-key-helper"],
+ ] as const) {
+ for (const [form, args] of [
+ ["two-token path", ["--settings", settingsPath]],
+ ["equals path", [`--settings=${settingsPath}`]],
+ ["second two-token occurrence", ["--settings", firstPath, "--settings", settingsPath]],
+ ["second equals occurrence", ["--settings", firstPath, `--settings=${settingsPath}`]],
+ ["inline JSON", ["--settings", JSON.stringify(value)]],
+ ] as const) {
+ test(
+ `${label} through ${form} is not controlled`,
+ () => {
+ const result = childAuthControl(
+ config({ claudeArgs: [...args] }),
+ {},
+ reads({
+ [firstPath]: { permissions: {} },
+ [settingsPath]: value,
+ })
+ );
+ expect(result).toMatchObject({ controlled: false, reason });
+ expect(JSON.stringify(result)).not.toContain(token);
+ expect(JSON.stringify(result)).not.toContain("T4-never-executed-helper");
+ },
+ TIMEOUT
+ );
+ }
+ }
+
+ test(
+ "an unreadable forwarded equals settings file is not controlled",
+ () => {
+ expect(
+ childAuthControl(config({ claudeArgs: [`--settings=${settingsPath}`] }), {}, reads())
+ ).toMatchObject({ controlled: false, reason: "settings-unreadable" });
+ },
+ TIMEOUT
+ );
+
+ test(
+ "a second unreadable settings file cannot be ignored",
+ () => {
+ expect(
+ childAuthControl(
+ config({
+ claudeArgs: ["--settings", firstPath, "--settings", settingsPath],
+ }),
+ {},
+ reads({ [firstPath]: {} })
+ )
+ ).toMatchObject({ controlled: false, reason: "settings-unreadable" });
+ },
+ TIMEOUT
+ );
+
+ for (const path of [
+ join(process.env.HOME!, ".claude", "settings.json"),
+ join(cwd, ".claude", "settings.json"),
+ join(cwd, ".claude", "settings.local.json"),
+ ]) {
+ test(
+ `a settings-supplied token at ${path} prevents the login promise`,
+ () => {
+ expect(
+ childAuthControl(
+ config(),
+ {},
+ reads({
+ [path]: {
+ env: { ANTHROPIC_AUTH_TOKEN: token },
+ },
+ })
+ )
+ ).toMatchObject({ controlled: false, reason: "settings-auth-token" });
+ },
+ TIMEOUT
+ );
+ }
+ });
+}
diff --git a/packages/cli/src/handlers/advisor-claude-arm.test.ts b/packages/cli/src/handlers/advisor-claude-arm.test.ts
new file mode 100644
index 00000000..2e6bb33f
--- /dev/null
+++ b/packages/cli/src/handlers/advisor-claude-arm.test.ts
@@ -0,0 +1,447 @@
+import { afterEach, beforeAll, describe, expect, test } from "bun:test";
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import type { ChildAuthControl } from "../claude-runner.js";
+import type { ClaudeAdvisorAuth, ClaudeRequestFacts } from "./native-handler-advisor.js";
+import type { SigningSecrets } from "./shared/call-context.js";
+
+const FILE = import.meta.path;
+const TIMEOUT = 20_000;
+
+if (process.env.T4_ISOLATED_FILE !== FILE) {
+ test(
+ "T4 G2/G4 runs with an isolated HOME and no inherited credentials",
+ async () => {
+ const home = mkdtempSync(join(tmpdir(), "t4-claude-arm-"));
+ const env = { ...process.env };
+ for (const key of Object.keys(env)) {
+ if (/^(ANTHROPIC|CLAUDE|CLAUDISH|OPENAI|OP_)/.test(key)) delete env[key];
+ }
+ const child = Bun.spawn([process.execPath, "test", FILE], {
+ cwd: home,
+ env: {
+ ...env,
+ HOME: home,
+ CLAUDE_CONFIG_DIR: join(home, "claude"),
+ T4_ISOLATED_FILE: FILE,
+ },
+ stdin: "ignore",
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ try {
+ const [code, stdout, stderr] = await Promise.all([
+ child.exited,
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ ]);
+ console.info(stdout + stderr);
+ expect(code).toBe(0);
+ } finally {
+ child.kill();
+ rmSync(home, { recursive: true, force: true });
+ }
+ },
+ TIMEOUT
+ );
+} else {
+ let api: typeof import("./native-handler-advisor.js");
+ let callContext: typeof import("./shared/call-context.js");
+ let wrapAnthropicError: typeof import("./shared/anthropic-error.js").wrapAnthropicError;
+ let calculateCost: typeof import("./shared/remote-provider-types.js").calculateCost;
+ let Hono: typeof import("hono").Hono;
+ const dataRoot = join(import.meta.dir, "../test-fixtures");
+ const inbound = readFileSync(join(dataRoot, "advisor/inbound-login.jsonl"), "utf8")
+ .trim()
+ .split("\n")
+ .map((line) => JSON.parse(line))
+ .find((entry) => entry.body?.toolsCount > 0);
+ const requestBody = JSON.parse(
+ readFileSync(join(dataRoot, "code-mode/cc-2.1.304-inbound-system.jsonl"), "utf8")
+ .trim()
+ .split("\n")[0]!
+ ).body;
+ const matrix = JSON.parse(
+ readFileSync(join(dataRoot, "advisor/replay-results-1792535094326.json"), "utf8")
+ );
+ const accepted = matrix.results.find((entry: any) => entry.shape === "A");
+ const refused = matrix.results.find((entry: any) => entry.shape === "A0");
+ const token = "OPAQUE_TEST_TOKEN_abc";
+ const inboundKey = "OPAQUE_TEST_KEY_inbound";
+ const resolvedKey = "OPAQUE_TEST_KEY_resolved";
+ const leases: SigningSecrets[] = [];
+ const noNetwork = (() => {
+ throw new Error("T4 forbids network");
+ }) as unknown as typeof fetch;
+ let successBody: string;
+
+ beforeAll(async () => {
+ globalThis.fetch = noNetwork;
+ api = await import("./native-handler-advisor.js");
+ callContext = await import("./shared/call-context.js");
+ ({ wrapAnthropicError } = await import("./shared/anthropic-error.js"));
+ ({ calculateCost } = await import("./shared/remote-provider-types.js"));
+ ({ Hono } = await import("hono"));
+ const logger = await import("../logger.js");
+ logger.initLogger(false, "minimal", true);
+ logger.setStderrQuiet(true);
+ const { sseResponseToJson } = await import("./shared/collect-sse-message.js");
+ // Derive the JSON reply with the production collector from this real Anthropic SSE capture.
+ successBody = await (
+ await sseResponseToJson(
+ new Response(
+ readFileSync(
+ join(dataRoot, "sse-responses/minimax-m3-anthropic-implicit-signature-r10416.sse")
+ ),
+ { headers: { "content-type": "text/event-stream" } }
+ ),
+ accepted.tier
+ )
+ ).text();
+ });
+
+ afterEach(() => {
+ for (const secrets of leases.splice(0)) secrets.lease.release();
+ api._debug_resetClaudeAuthWarnings();
+ api._debug_resetClaudeAdvisorSpend();
+ globalThis.fetch = noNetwork;
+ });
+
+ async function facts(
+ options: {
+ childAuth?: ChildAuthControl;
+ bearer?: string | null;
+ key?: string;
+ userToken?: string;
+ omitBeta?: boolean;
+ } = {}
+ ): Promise<ClaudeRequestFacts> {
+ // Header map from the S0 main-turn capture; only the redacted credential is replaced.
+ const headers = new Headers(inbound.headers);
+ if (options.bearer === null) headers.delete("authorization");
+ else headers.set("authorization", `Bearer ${options.bearer ?? token}`);
+ if (options.key) headers.set("x-api-key", options.key);
+ if (options.omitBeta) headers.delete("anthropic-beta");
+ const app = new Hono();
+ let result!: ClaudeRequestFacts;
+ app.post("/", (c) => {
+ const secrets = callContext.createSigningSecrets();
+ leases.push(secrets);
+ c.set(callContext.SIGNING_SECRETS_KEY, secrets);
+ result = api.claudeRequestFacts(c, requestBody, {
+ childAuth: options.childAuth ?? { controlled: true },
+ env: options.userToken ? { ANTHROPIC_AUTH_TOKEN: options.userToken } : {},
+ });
+ return c.text("ok");
+ });
+ await app.request("/", { method: "POST", headers });
+ return result;
+ }
+
+ function callParams(auth: ClaudeAdvisorAuth, role: "panel" | "collector" = "panel") {
+ return {
+ role,
+ spec: accepted.tier,
+ model: accepted.tier,
+ auth,
+ system: accepted.sent.systemShape.blocks.at(-1).text,
+ // Turns derived from the real inbound Claude Code capture, not a synthetic request body.
+ turns: [
+ {
+ role: "user" as const,
+ content: api.extractBlocksAsText(requestBody.messages[0].content),
+ },
+ ],
+ timeoutMs: 1000,
+ };
+ }
+
+ function fetchReply(body = successBody, status = 200, headers: HeadersInit = {}) {
+ const requests: Request[] = [];
+ const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => {
+ requests.push(input instanceof Request ? input.clone() : new Request(input, init));
+ return new Response(body, { status, headers });
+ }) as unknown as typeof fetch;
+ return { requests, fetchImpl };
+ }
+
+ describe("G2 Claude login arm — A1/A9 and S0", () => {
+ for (const role of ["panel", "collector"] as const) {
+ test(
+ `${role}: controlled login signs only the fixed Anthropic endpoint with shape A`,
+ async () => {
+ const f = await facts({ omitBeta: true });
+ let resolutions = 0;
+ const auth = await api.resolveClaudeAdvisorAuth(f, async () => {
+ resolutions++;
+ return resolvedKey;
+ });
+ expect(auth.arm).toBe("oauth");
+ expect(resolutions).toBe(0);
+ const reply = fetchReply();
+ const warnings: string[] = [];
+ const result = await api.runClaudeAdvisorCall({
+ ...callParams(auth, role),
+ ...reply,
+ warn: (message) => warnings.push(message),
+ });
+ expect(reply.requests).toHaveLength(1);
+ const request = reply.requests[0]!;
+ expect(request.url).toBe("https://api.anthropic.com/v1/messages");
+ expect(request.headers.get("authorization")).toBe(`Bearer ${token}`);
+ expect(request.headers.has("x-api-key")).toBe(false);
+ const body = await request.json();
+ expect(body.system[0]).toEqual({ type: "text", text: f.identityBlock });
+ expect(body.system[0]).not.toHaveProperty("cache_control");
+ expect(body.system).toHaveLength(2);
+ expect(result).toMatchObject({
+ origin: "upstream",
+ servedArm: "oauth",
+ billing: "subscription",
+ systemHead: "identity",
+ });
+ expect(warnings).toEqual([]);
+ expect(JSON.stringify(result)).not.toContain(token);
+ },
+ TIMEOUT
+ );
+ }
+
+ test(
+ "the captured advisor beta is stripped without removing the login beta",
+ async () => {
+ const auth = await api.resolveClaudeAdvisorAuth(await facts(), async () => undefined);
+ const reply = fetchReply();
+ await api.runClaudeAdvisorCall({ ...callParams(auth), ...reply, warn() {} });
+ const beta = reply.requests[0]!.headers.get("anthropic-beta")!;
+ expect(beta).not.toContain("advisor-tool-2026-03-01");
+ expect(beta).toContain("oauth-2025-04-20");
+ },
+ TIMEOUT
+ );
+
+ for (const source of ["settings-auth-token", "api-key-helper", "proxy-auth"] as const) {
+ test(
+ `uncontrolled ${source} never signs with a bearer and warns before metered use`,
+ async () => {
+ const f = await facts({
+ childAuth: { controlled: false, reason: source, where: "T4 settings" },
+ key: inboundKey,
+ });
+ let resolutions = 0;
+ const auth = await api.resolveClaudeAdvisorAuth(f, async () => {
+ resolutions++;
+ return resolvedKey;
+ });
+ expect(f.login).toBeUndefined();
+ expect(f.loginSkipped).toBe("launch-auth-not-claudish-controlled");
+ expect(auth).toMatchObject({ arm: "api-key", source: "inbound-x-api-key" });
+ expect(resolutions).toBe(0);
+ const reply = fetchReply();
+ const warnings: string[] = [];
+ const result = await api.runClaudeAdvisorCall({
+ ...callParams(auth),
+ ...reply,
+ warn: (message) => warnings.push(message),
+ });
+ expect(reply.requests[0]!.url).toBe("https://api.anthropic.com/v1/messages");
+ expect(reply.requests[0]!.headers.has("authorization")).toBe(false);
+ expect(reply.requests[0]!.headers.get("x-api-key")).toBe(inboundKey);
+ expect(result).toMatchObject({
+ origin: "upstream",
+ servedArm: "api-key",
+ billing: "metered",
+ });
+ expect(warnings).toHaveLength(1);
+ expect(warnings[0]).toContain(source);
+ expect(warnings[0]).toMatch(/metered|billed per token/);
+ expect(warnings.join(" ")).not.toContain(token);
+ },
+ TIMEOUT
+ );
+ }
+
+ test(
+ "a user's ANTHROPIC_AUTH_TOKEN is excluded even when the beta and controlled flag are present",
+ async () => {
+ const f = await facts({ userToken: token });
+ const auth = await api.resolveClaudeAdvisorAuth(f, async () => resolvedKey);
+ expect(f.login).toBeUndefined();
+ expect(f.loginSkipped).toBe("user-auth-token");
+ expect(auth).toMatchObject({ arm: "api-key", source: "ANTHROPIC_API_KEY" });
+ const reply = fetchReply();
+ const warnings: string[] = [];
+ await api.runClaudeAdvisorCall({
+ ...callParams(auth),
+ ...reply,
+ warn: (message) => warnings.push(message),
+ });
+ expect(reply.requests[0]!.headers.has("authorization")).toBe(false);
+ expect(reply.requests[0]!.headers.get("x-api-key")).toBe(resolvedKey);
+ expect(warnings.join(" ")).toContain("ANTHROPIC_AUTH_TOKEN");
+ expect(warnings.join(" ")).not.toContain(token);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "no bearer uses the resolved key, warns once per reason, and warns for a later different reason",
+ async () => {
+ const noBearer = await api.resolveClaudeAdvisorAuth(
+ await facts({ bearer: null }),
+ async () => resolvedKey
+ );
+ const skipped = await api.resolveClaudeAdvisorAuth(
+ await facts({ userToken: token }),
+ async () => resolvedKey
+ );
+ const reply = fetchReply();
+ const warnings: string[] = [];
+ for (const auth of [noBearer, noBearer, skipped, skipped]) {
+ const result = await api.runClaudeAdvisorCall({
+ ...callParams(auth),
+ ...reply,
+ warn: (message) => warnings.push(message),
+ });
+ expect(result.billing).toBe("metered");
+ }
+ expect(warnings).toHaveLength(2);
+ expect(new Set(warnings).size).toBe(2);
+ expect(reply.requests.every((request) => !request.headers.has("authorization"))).toBe(true);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "no accepted login and no key produces a credential stub without fetching",
+ async () => {
+ const auth = await api.resolveClaudeAdvisorAuth(
+ await facts({ bearer: null }),
+ async () => undefined
+ );
+ const reply = fetchReply();
+ const warnings: string[] = [];
+ const result = await api.runClaudeAdvisorCall({
+ ...callParams(auth),
+ ...reply,
+ warn: (message) => warnings.push(message),
+ });
+ expect(reply.requests).toHaveLength(0);
+ expect(result.origin).toBe("stub");
+ expect(result.reason).toContain("no Claude credential");
+ expect(warnings).toHaveLength(1);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "S0 429 Error without unified headers is a shape stub, never retried or cooled down",
+ async () => {
+ // Derived from S0 replay-results-1792535094326.json: captured error type/message/status
+ // and headers are passed through the production Anthropic error builder, not retyped.
+ const body = JSON.stringify(
+ wrapAnthropicError(refused.status, refused.errorMessage, refused.errorType)
+ );
+ expect(
+ api.isClaudeShapeRefusal(refused.status, body, new Headers(refused.responseHeaders))
+ ).toBe(true);
+ expect(
+ api.isClaudeShapeRefusal(refused.status, body, new Headers(accepted.responseHeaders))
+ ).toBe(false);
+ expect(api.isClaudeShapeRefusal(401, body, new Headers(refused.responseHeaders))).toBe(
+ false
+ );
+ const auth = await api.resolveClaudeAdvisorAuth(await facts(), async () => resolvedKey);
+ const reply = fetchReply(body, refused.status, refused.responseHeaders);
+ for (let i = 0; i < 2; i++) {
+ const result = await api.runClaudeAdvisorCall({
+ ...callParams(auth),
+ ...reply,
+ warn() {},
+ });
+ expect(result).toMatchObject({
+ origin: "stub",
+ upstreamStatus: refused.status,
+ shapeRefusal: true,
+ });
+ expect(result.reason).toContain("shape");
+ expect(result.reason).toContain("not a quota");
+ expect(reply.requests).toHaveLength(i + 1);
+ }
+ expect(reply.requests.every((request) => request.headers.has("authorization"))).toBe(true);
+ },
+ TIMEOUT
+ );
+ });
+
+ describe("G4 advisor stats and the main status line", () => {
+ test(
+ "Claude stats have request_role advisor and accumulate metered spend, never subscription spend",
+ () => {
+ const outcome = { upstreamStatus: 200, latencyMs: 1, usage: accepted.ok.usage };
+ const login = api.claudeAdvisorStatsEvent(accepted.tier, "oauth", outcome);
+ expect(login).toMatchObject({
+ request_role: "advisor",
+ estimated_cost: 0,
+ is_free_model: true,
+ provider_name: "native-anthropic",
+ });
+ const cost = calculateCost(
+ "native-anthropic",
+ accepted.tier,
+ outcome.usage.input_tokens,
+ outcome.usage.output_tokens
+ );
+ expect(cost).toBeGreaterThan(0);
+ const first = api.claudeAdvisorStatsEvent(accepted.tier, "api-key", outcome);
+ const second = api.claudeAdvisorStatsEvent(accepted.tier, "api-key", outcome);
+ expect(first.estimated_cost).toBeCloseTo(cost, 12);
+ expect(second).toMatchObject({ request_role: "advisor", is_free_model: false });
+ expect(second.estimated_cost).toBeCloseTo(cost * 2, 12);
+ const afterLogin = api.claudeAdvisorStatsEvent(accepted.tier, "oauth", outcome);
+ expect(afterLogin.estimated_cost).toBeCloseTo(cost * 2, 12);
+ const failed = api.claudeAdvisorStatsEvent(accepted.tier, "api-key", {
+ upstreamStatus: 429,
+ latencyMs: 1,
+ });
+ expect(failed).toMatchObject({ request_role: "advisor", success: false, http_status: 429 });
+ expect(failed.estimated_cost).toBeCloseTo(cost * 2, 12);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "Claude panel and collector calls leave the main token file byte-identical",
+ async () => {
+ const path = join(process.env.HOME!, "main-token.json");
+ const sentinel = JSON.stringify({
+ model: "main-session",
+ input_tokens: 71,
+ output_tokens: 9,
+ });
+ writeFileSync(path, sentinel);
+ process.env.CLAUDISH_TOKEN_FILE = path;
+ try {
+ for (const role of ["panel", "collector"] as const) {
+ for (const bearer of [token, null]) {
+ const auth = await api.resolveClaudeAdvisorAuth(
+ await facts({ bearer }),
+ async () => resolvedKey
+ );
+ await api.runClaudeAdvisorCall({
+ ...callParams(auth, role),
+ ...fetchReply(),
+ warn() {},
+ });
+ expect(readFileSync(path, "utf8")).toBe(sentinel);
+ }
+ }
+ } finally {
+ delete process.env.CLAUDISH_TOKEN_FILE;
+ }
+ },
+ TIMEOUT
+ );
+ });
+}
diff --git a/packages/cli/src/handlers/shared/signed-billing.test.ts b/packages/cli/src/handlers/shared/signed-billing.test.ts
new file mode 100644
index 00000000..897db1e3
--- /dev/null
+++ b/packages/cli/src/handlers/shared/signed-billing.test.ts
@@ -0,0 +1,362 @@
+import { afterEach, beforeAll, describe, expect, spyOn, test } from "bun:test";
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import type { CredentialArm } from "../../auth/credentials/types.js";
+import type { AuthBinding, ProviderTransport } from "../../providers/transport/types.js";
+import type { ComposedHandler } from "../composed-handler.js";
+import type { HopAttempt, SigningSecrets } from "./call-context.js";
+
+const FILE = import.meta.path;
+const TIMEOUT = 20_000;
+
+if (process.env.T4_ISOLATED_FILE !== FILE) {
+ test(
+ "T4 G3/G4 runs with an isolated HOME and no inherited credentials",
+ async () => {
+ const home = mkdtempSync(join(tmpdir(), "t4-signed-billing-"));
+ const env = { ...process.env };
+ for (const key of Object.keys(env)) {
+ if (/^(ANTHROPIC|CLAUDE|CLAUDISH|OPENAI|OP_)/.test(key)) delete env[key];
+ }
+ const child = Bun.spawn([process.execPath, "test", FILE], {
+ cwd: home,
+ env: {
+ ...env,
+ HOME: home,
+ CLAUDE_CONFIG_DIR: join(home, "claude"),
+ T4_ISOLATED_FILE: FILE,
+ },
+ stdin: "ignore",
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ try {
+ const [code, stdout, stderr] = await Promise.all([
+ child.exited,
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ ]);
+ console.info(stdout + stderr);
+ expect(code).toBe(0);
+ } finally {
+ child.kill();
+ rmSync(home, { recursive: true, force: true });
+ }
+ },
+ TIMEOUT
+ );
+} else {
+ let pricing: typeof import("./remote-provider-types.js");
+ let TokenTracker: typeof import("./token-tracker.js").TokenTracker;
+ let Handler: typeof import("../composed-handler.js").ComposedHandler;
+ let OpenAIAPIFormat: typeof import("../../adapters/openai-api-format.js").OpenAIAPIFormat;
+ let callContext: typeof import("./call-context.js");
+ let Hono: typeof import("hono").Hono;
+ let previousProbe: ReturnType<typeof pricing.registerSubscriptionCredentialProbe>;
+ const noNetwork = (() => {
+ throw new Error("T4 forbids network");
+ }) as unknown as typeof fetch;
+ const handlers: ComposedHandler[] = [];
+ const leases: SigningSecrets[] = [];
+ const dataRoot = join(import.meta.dir, "../../test-fixtures");
+ const requestBody = JSON.parse(
+ readFileSync(join(dataRoot, "code-mode/cc-2.1.304-inbound-system.jsonl"), "utf8")
+ .trim()
+ .split("\n")[0]!
+ ).body;
+ const sse = readFileSync(join(dataRoot, "sse-responses/gpt-5.6-sol-responses-turn1.sse"), "utf8");
+ const completed = sse
+ .split("\n")
+ .filter((line) => line.startsWith("data: {"))
+ .map((line) => JSON.parse(line.slice(6)))
+ .find((event) => event.type === "response.completed").response;
+ const model: string = completed.model;
+ const usage: { input_tokens: number; output_tokens: number } = completed.usage;
+
+ beforeAll(async () => {
+ globalThis.fetch = noNetwork;
+ pricing = await import("./remote-provider-types.js");
+ ({ TokenTracker } = await import("./token-tracker.js"));
+ ({ ComposedHandler: Handler } = await import("../composed-handler.js"));
+ ({ OpenAIAPIFormat } = await import("../../adapters/openai-api-format.js"));
+ callContext = await import("./call-context.js");
+ ({ Hono } = await import("hono"));
+ const logger = await import("../../logger.js");
+ logger.initLogger(false, "minimal", true);
+ logger.setStderrQuiet(true);
+ previousProbe = pricing.registerSubscriptionCredentialProbe(null);
+ });
+
+ afterEach(async () => {
+ for (const handler of handlers.splice(0)) await handler.shutdown();
+ for (const secrets of leases.splice(0)) secrets.lease.release();
+ pricing.registerSubscriptionCredentialProbe(previousProbe);
+ globalThis.fetch = noNetwork;
+ delete process.env.CLAUDISH_TOKEN_FILE;
+ });
+
+ function cost(input: number, output: number): number {
+ const rates = pricing.getModelPricing("openai-codex", model, { subscription: false });
+ return (input * rates.inputCostPer1M + output * rates.outputCostPer1M) / 1_000_000;
+ }
+
+ function tracker() {
+ return new TokenTracker(0, {
+ providerName: "openai-codex",
+ modelName: model,
+ contextWindow: 0,
+ writesTokenFile: false,
+ });
+ }
+
+ function deferred<T>() {
+ let resolve!: (value: T) => void;
+ const promise = new Promise<T>((r) => {
+ resolve = r;
+ });
+ return { promise, resolve };
+ }
+
+ function handlerWithArms(
+ arms: Array<CredentialArm | undefined>,
+ onSign: (arm: CredentialArm | undefined) => void
+ ) {
+ let index = 0;
+ const provider: ProviderTransport = {
+ name: "openai-codex",
+ displayName: "T4 Codex",
+ streamFormat: "openai-responses-sse",
+ overrideStreamFormat: () => "openai-responses-sse",
+ getEndpoint: () => "https://t4.invalid/v1/responses",
+ async getHeaders(_request, binding) {
+ return { "content-type": "application/json", "x-t4-arm": binding?.auth?.arm ?? "absent" };
+ },
+ async refreshAuth(): Promise<AuthBinding> {
+ const arm = arms[index++];
+ onSign(arm);
+ return { auth: { headers: {}, ...(arm ? { arm } : {}) } };
+ },
+ };
+ const handler = new Handler(provider, model, model, 0, {
+ adapter: new OpenAIAPIFormat(model),
+ tokenStrategy: "standard",
+ requestRole: "advisor",
+ codeMode: "off",
+ catalogCachePath: join(process.env.HOME!, "no-catalog.json"),
+ });
+ handlers.push(handler);
+ return handler;
+ }
+
+ function startCall(handler: ComposedHandler) {
+ const hops: HopAttempt[] = [];
+ const secrets = callContext.createSigningSecrets();
+ leases.push(secrets);
+ const app = new Hono();
+ app.post("/", (c) => {
+ c.set(callContext.HOP_LOG_KEY, hops);
+ c.set(callContext.SIGNING_SECRETS_KEY, secrets);
+ // Replay the real inbound body through the public handler. No request protocol is invented.
+ return handler.handle(c, structuredClone(requestBody));
+ });
+ return { hops, response: app.request("/", { method: "POST" }) };
+ }
+
+ describe("G3 billing follows each request's signing arm", () => {
+ test(
+ "request pricing overrides the opposite process-wide credential probe",
+ () => {
+ pricing.registerSubscriptionCredentialProbe(() => true);
+ expect(pricing.getModelPricing("openai-codex", model).isSubscription).toBe(true);
+ const metered = pricing.getModelPricing("openai-codex", model, { subscription: false });
+ expect(metered.inputCostPer1M + metered.outputCostPer1M).toBeGreaterThan(0);
+ expect(metered.isSubscription).not.toBe(true);
+ pricing.registerSubscriptionCredentialProbe(() => false);
+ expect(
+ pricing.getModelPricing("openai-codex", model, { subscription: true })
+ ).toMatchObject({ inputCostPer1M: 0, outputCostPer1M: 0, isSubscription: true });
+ },
+ TIMEOUT
+ );
+
+ for (const strategy of ["update", "accumulateBoth", "updateWithDelta"] as const) {
+ test(
+ `${strategy}: metered token costs and retry-leg extra input survive an oauth probe`,
+ () => {
+ pricing.registerSubscriptionCredentialProbe(() => true);
+ const t = tracker();
+ t[strategy](usage.input_tokens, usage.output_tokens, undefined, { subscription: false });
+ expect(t.getTotalCost()).toBeCloseTo(cost(usage.input_tokens, usage.output_tokens), 12);
+ t.addBilledInputTokens(usage.input_tokens, undefined, { subscription: false });
+ expect(t.getTotalCost()).toBeCloseTo(
+ cost(usage.input_tokens * 2, usage.output_tokens),
+ 12
+ );
+ },
+ TIMEOUT
+ );
+
+ test(
+ `${strategy}: oauth token costs and retry-leg extra input stay zero under a metered probe`,
+ () => {
+ pricing.registerSubscriptionCredentialProbe(() => false);
+ const t = tracker();
+ t[strategy](usage.input_tokens, usage.output_tokens, undefined, { subscription: true });
+ t.addBilledInputTokens(usage.input_tokens, undefined, { subscription: true });
+ expect(t.getTotalCost()).toBe(0);
+ },
+ TIMEOUT
+ );
+ }
+
+ for (const arm of ["oauth", "api-key", undefined] as const) {
+ test(
+ `ComposedHandler arm ${arm ?? "absent"} labels and prices the captured stream independently of the probe`,
+ async () => {
+ const oauth = arm === "oauth";
+ pricing.registerSubscriptionCredentialProbe(() => !oauth);
+ const handler = handlerWithArms([arm], () => {});
+ const signed: string[] = [];
+ globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
+ signed.push(new Headers(init?.headers).get("x-t4-arm")!);
+ return new Response(sse, { headers: { "content-type": "text/event-stream" } });
+ }) as unknown as typeof fetch;
+ const call = startCall(handler);
+ const response = await call.response;
+ expect(response.status).toBe(200);
+ await response.text();
+ expect(signed).toEqual([arm ?? "absent"]);
+ expect(call.hops).toHaveLength(1);
+ expect(call.hops[0]).toMatchObject({
+ arm: arm ?? null,
+ billing: oauth ? "subscription" : "metered",
+ });
+ expect(handler.getTokenTracker().getTotalCost()).toBeCloseTo(
+ oauth ? 0 : cost(usage.input_tokens, usage.output_tokens),
+ 12
+ );
+ },
+ TIMEOUT
+ );
+ }
+
+ for (const arms of [
+ [undefined, "oauth"],
+ ["oauth", undefined],
+ ] as const) {
+ test(
+ `overlapping ${arms[0] ?? "absent"} then ${arms[1] ?? "absent"} requests cannot exchange rates`,
+ async () => {
+ let currentArm: CredentialArm | undefined;
+ pricing.registerSubscriptionCredentialProbe(() => currentArm === "oauth");
+ const handler = handlerWithArms([...arms], (arm) => {
+ currentArm = arm;
+ });
+ const issued = [deferred<void>(), deferred<void>()];
+ const replies = [deferred<Response>(), deferred<Response>()];
+ let fetchIndex = 0;
+ globalThis.fetch = (async () => {
+ const index = fetchIndex++;
+ issued[index]!.resolve();
+ return replies[index]!.promise;
+ }) as unknown as typeof fetch;
+ const first = startCall(handler);
+ await issued[0]!.promise;
+ const second = startCall(handler);
+ await issued[1]!.promise;
+ expect(currentArm).toBe(arms[1]);
+ // The second request signs before the first receives usage, then completes first.
+ replies[1]!.resolve(
+ new Response(sse, { headers: { "content-type": "text/event-stream" } })
+ );
+ await (await second.response).text();
+ expect(handler.getTokenTracker().getTotalCost()).toBeCloseTo(
+ arms[1] === "oauth" ? 0 : cost(usage.input_tokens, usage.output_tokens),
+ 12
+ );
+ replies[0]!.resolve(
+ new Response(sse, { headers: { "content-type": "text/event-stream" } })
+ );
+ await (await first.response).text();
+ expect(first.hops[0]).toMatchObject({
+ arm: arms[0] ?? null,
+ billing: arms[0] === "oauth" ? "subscription" : "metered",
+ });
+ expect(second.hops[0]).toMatchObject({
+ arm: arms[1] ?? null,
+ billing: arms[1] === "oauth" ? "subscription" : "metered",
+ });
+ expect(handler.getTokenTracker().getTotalCost()).toBeCloseTo(
+ cost(usage.input_tokens, usage.output_tokens),
+ 12
+ );
+ expect(fetchIndex).toBe(2);
+ },
+ TIMEOUT
+ );
+ }
+ });
+
+ describe("G4 advisor token-file isolation", () => {
+ test(
+ "all tracker update and rewrite entry points preserve the main token file",
+ () => {
+ const path = join(process.env.HOME!, "main-token.json");
+ const sentinel = JSON.stringify({
+ model: "main-session",
+ input_tokens: 71,
+ output_tokens: 9,
+ });
+ writeFileSync(path, sentinel);
+ process.env.CLAUDISH_TOKEN_FILE = path;
+ const t = tracker();
+ t.update(usage.input_tokens, usage.output_tokens, undefined, { subscription: false });
+ t.accumulateBoth(usage.input_tokens, usage.output_tokens, undefined, {
+ subscription: false,
+ });
+ t.updateWithDelta(usage.input_tokens, usage.output_tokens, undefined, {
+ subscription: false,
+ });
+ t.addBilledInputTokens(usage.input_tokens, undefined, { subscription: false });
+ t.setPlanUsage(undefined);
+ t.rewrite();
+ expect(t.getTotalCost()).toBeGreaterThan(0);
+ expect(readFileSync(path, "utf8")).toBe(sentinel);
+ },
+ TIMEOUT
+ );
+
+ test(
+ "an advisor ComposedHandler tags emitted stats without overwriting main status-line counts",
+ async () => {
+ const path = join(process.env.HOME!, "main-token.json");
+ const sentinel = JSON.stringify({
+ model: "main-session",
+ input_tokens: 71,
+ output_tokens: 9,
+ });
+ writeFileSync(path, sentinel);
+ process.env.CLAUDISH_TOKEN_FILE = path;
+ const handler = handlerWithArms(["api-key"], () => {});
+ globalThis.fetch = (async () =>
+ new Response(sse, {
+ headers: { "content-type": "text/event-stream" },
+ })) as unknown as typeof fetch;
+ // Instrument only this HOME-isolated process; never mock.module shared infrastructure.
+ const stats = await import("../../stats.js");
+ const emitted = spyOn(stats, "recordStats").mockImplementation(() => {});
+ try {
+ await (await startCall(handler).response).text();
+ expect(emitted).toHaveBeenCalled();
+ for (const [event] of emitted.mock.calls) expect(event.request_role).toBe("advisor");
+ expect(handler.getTokenTracker().getTotalCost()).toBeGreaterThan(0);
+ expect(readFileSync(path, "utf8")).toBe(sentinel);
+ } finally {
+ emitted.mockRestore();
+ }
+ },
+ TIMEOUT
+ );
+ });
+}