Skip to content

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

test(adapters): pin signing-value redaction in log sinks and client bodies

Jack Rudenkocabd8397c72f
diff --git a/packages/cli/src/handlers/composed-handler-client-redaction.test.ts b/packages/cli/src/handlers/composed-handler-client-redaction.test.ts
new file mode 100644
index 00000000..12bae21a
--- /dev/null
+++ b/packages/cli/src/handlers/composed-handler-client-redaction.test.ts
@@ -0,0 +1,251 @@
+import { afterEach, describe, expect, it, jest } from "bun:test";
+import { mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const childKey = "T1_COMPOSED_REDACTION_CHILD";
+
+if (process.env[childKey] !== "1") {
+ it("runs client-body redaction checks with an isolated HOME", () => {
+ const home = mkdtempSync(join(tmpdir(), "claudish-t1-client-"));
+ try {
+ const env: Record<string, string | undefined> = {
+ ...process.env,
+ HOME: home,
+ XDG_CONFIG_HOME: home,
+ [childKey]: "1",
+ };
+ for (const key of Object.keys(env)) {
+ if (/KEY|TOKEN|SECRET|PASSWORD|COOKIE/.test(key) && key !== childKey) delete env[key];
+ }
+ const result = Bun.spawnSync([process.execPath, "test", import.meta.path], {
+ env,
+ timeout: 18_000,
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ const output = result.stdout.toString() + result.stderr.toString();
+ console.log(output);
+ expect(result.exitCode, output).toBe(0);
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ }, 20_000);
+} else {
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (() => {
+ throw new Error("T1 forbids network");
+ }) as unknown as typeof fetch;
+ const { Hono } = await import("hono");
+ const { ComposedHandler } = await import("./composed-handler.js");
+ const { OpenAIAPIFormat } = await import("../adapters/openai-api-format.js");
+ const { AnthropicAPIFormat } = await import("../adapters/anthropic-api-format.js");
+ const { buildAdvisorPanelPayload } = await import("./advisor-route-plan.js");
+ const { acquireSigningLease, redactForClient } = await import("../redact.js");
+ const { respondAuthFailure } = await import("./shared/auth-failure.js");
+ const { createSigningSecrets, releaseWhenBodyEnds, SIGNING_SECRETS_KEY } = await import(
+ "./shared/call-context.js"
+ );
+ const { setDiagOutput } = await import("../logger.js");
+ type AuthFailure = import("./shared/auth-failure.js").AuthFailure;
+ type ProviderTransport = import("../providers/transport/types.js").ProviderTransport;
+ type StreamFormat = import("../providers/transport/types.js").StreamFormat;
+ const source = readFileSync(
+ new URL("../test-fixtures/redaction/zai-instream-error.sse", import.meta.url),
+ "utf8"
+ );
+ const requestSource = JSON.parse(
+ readFileSync(
+ new URL("../test-fixtures/redaction/roundtrip-request.json", import.meta.url),
+ "utf8"
+ )
+ );
+ const errorLine = source.split("\n").find((line) => line.includes('"request_id"'))!;
+ const errorBody = JSON.parse(errorLine.slice(6));
+ const secret = "temporarily";
+ const leases: ReturnType<typeof acquireSigningLease>[] = [];
+ const handlers: InstanceType<typeof ComposedHandler>[] = [];
+ // Derived from roundtrip-string.request.json's actual user turn; the production builder supplies request encoding.
+ const payload = buildAdvisorPanelPayload(
+ requestSource.body.model,
+ requestSource.body.instructions,
+ [
+ {
+ role: requestSource.body.input[0].role,
+ content: requestSource.body.input[0].content[0].text,
+ },
+ ],
+ 16
+ );
+ // Derived from zai-instream-error.sse: its "error" key supplies the type/event discriminator.
+ // The complete provider error object, message and SSE field prefixes are retained from the capture.
+ const errorType = Object.keys(errorBody)[0]!;
+ const typedData = errorLine.slice(0, 6) + JSON.stringify({ type: errorType, ...errorBody });
+ const eventLine = source.split("\n")[0]!.split(":")[0] + ": " + errorType;
+ const responsesHead = typedData + "\n\n";
+ const anthropicError =
+ source.slice(0, source.indexOf(errorLine)) + eventLine + "\n" + typedData + "\n\n";
+
+ function hold() {
+ const lease = acquireSigningLease();
+ leases.push(lease);
+ lease.add([secret]);
+ return lease;
+ }
+ function appFor(transport: ProviderTransport, stream: boolean) {
+ const app = new Hono();
+ const format =
+ transport.streamFormat === "anthropic-sse"
+ ? new AnthropicAPIFormat(payload.model)
+ : new OpenAIAPIFormat(payload.model);
+ const handler = new ComposedHandler(transport, payload.model, payload.model, 0, {
+ adapter: format,
+ requestRole: "advisor",
+ codeMode: "off",
+ isInteractive: false,
+ });
+ handlers.push(handler);
+ app.post("/v1/messages", async (c) => {
+ const secrets = createSigningSecrets();
+ leases.push(secrets.lease);
+ c.set(SIGNING_SECRETS_KEY, secrets);
+ // The only change to the captured-turn-derived payload is the requested stream mode.
+ return releaseWhenBodyEnds(await handler.handle(c, { ...payload, stream }), () =>
+ secrets.lease.release()
+ );
+ });
+ return () => app.request("/v1/messages", { method: "POST" });
+ }
+ function transport(
+ format: StreamFormat,
+ extras: Partial<ProviderTransport> = {}
+ ): ProviderTransport {
+ return {
+ name: "t1-redaction",
+ displayName: "T1 redaction",
+ streamFormat: format,
+ getEndpoint: () => "https://t1.invalid/v1/messages",
+ getHeaders: async () => ({ authorization: `Bearer ${secret}` }),
+ overrideStreamFormat: () => format,
+ ...extras,
+ };
+ }
+ function mockFetch(response: () => Response) {
+ let calls = 0;
+ globalThis.fetch = (async (input: string | URL | Request) => {
+ const url = input instanceof Request ? input.url : String(input);
+ if (url !== "https://t1.invalid/v1/messages") throw new Error(`T1 forbids network: ${url}`);
+ calls++;
+ return response();
+ }) as unknown as typeof fetch;
+ return () => calls;
+ }
+
+ afterEach(async () => {
+ for (const handler of handlers.splice(0)) await handler.shutdown();
+ for (const lease of leases.splice(0)) lease.release();
+ setDiagOutput(null);
+ jest.useRealTimers();
+ globalThis.fetch = (() => {
+ throw new Error("T1 forbids network");
+ }) as unknown as typeof fetch;
+ });
+ process.on("exit", () => {
+ globalThis.fetch = originalFetch;
+ });
+
+ describe("G3 Claude Code-facing error bodies", () => {
+ const failures: AuthFailure[] = [
+ { kind: "terminal", upstreamStatus: 401, message: errorBody.error.message },
+ { kind: "transient", status: 503, message: errorBody.error.message },
+ { kind: "unknown", message: errorBody.error.message },
+ ];
+ for (const failure of failures) {
+ it(`redacts respondAuthFailure's ${failure.kind} message without changing its verdict`, async () => {
+ hold();
+ const app = new Hono();
+ app.post(
+ "/auth",
+ (c) =>
+ respondAuthFailure(c, failure, {
+ isSubscription: true,
+ providerDisplayName: "T1 subscription",
+ })!
+ );
+ const result = await app.request("/auth", { method: "POST" });
+ expect(result.status).toBe(failure.kind === "terminal" ? 400 : 503);
+ expect(result.headers.get("x-claudish-auth-verdict")).toBe(
+ failure.kind === "terminal" ? "terminal" : "transient"
+ );
+ const text = await result.text();
+ expect(text).toContain("[redacted]");
+ expect(text).not.toContain(secret);
+ expect(JSON.parse(text).error.message).toContain("[redacted]");
+ expect(redactForClient(secret)).toBe("[redacted]");
+ }, 10_000);
+ }
+
+ it("redacts a ComposedHandler HTTP error signed with an opaque header value", async () => {
+ const calls = mockFetch(
+ () =>
+ new Response(JSON.stringify(errorBody), {
+ status: 401,
+ headers: { "content-type": "application/json" },
+ })
+ );
+ const response = await appFor(transport("openai-sse"), false)();
+ const text = await response.text();
+ expect(calls()).toBe(1);
+ expect(response.ok).toBe(false);
+ expect(text).toContain("[redacted]");
+ expect(text).not.toContain(secret);
+ expect(JSON.parse(text).error).toBeDefined();
+ }, 10_000);
+
+ it("redacts exhausted Responses stream-head failure text after all retries", async () => {
+ jest.useFakeTimers({ now: new Date("2026-01-01T00:00:00Z") });
+ const calls = mockFetch(
+ () =>
+ new Response(responsesHead, {
+ headers: { "content-type": "text/event-stream" },
+ })
+ );
+ setDiagOutput({ write() {}, cleanup() {} });
+ let settled = false;
+ const pending = Promise.resolve(appFor(transport("openai-responses-sse"), false)());
+ void pending.then(() => {
+ settled = true;
+ });
+ for (let seconds = 0; seconds < 120 && !settled; seconds++) {
+ for (let turn = 0; turn < 80; turn++) await Promise.resolve();
+ jest.advanceTimersByTime(1000);
+ }
+ expect(settled).toBe(true);
+ const response = await pending;
+ const text = await response.text();
+ expect(calls()).toBe(4);
+ expect(response.status).toBe(503);
+ expect(text).toContain("[redacted]");
+ expect(text).not.toContain(secret);
+ expect(JSON.parse(text).error).toBeDefined();
+ }, 10_000);
+
+ it("redacts the parser's SSE error frame using the transport's body-only signing secret", async () => {
+ mockFetch(
+ () => new Response(anthropicError, { headers: { "content-type": "text/event-stream" } })
+ );
+ const response = await appFor(
+ transport("anthropic-sse", {
+ getHeaders: async () => ({}),
+ redactionSecrets: () => [secret],
+ }),
+ true
+ )();
+ const text = await response.text();
+ expect(response.status).toBe(200);
+ expect(text).toContain("event: error");
+ expect(text).toContain("[redacted]");
+ expect(text).not.toContain(secret);
+ }, 10_000);
+ });
+}
diff --git a/packages/cli/src/handlers/native-handler-redaction.test.ts b/packages/cli/src/handlers/native-handler-redaction.test.ts
new file mode 100644
index 00000000..ec9c3af6
--- /dev/null
+++ b/packages/cli/src/handlers/native-handler-redaction.test.ts
@@ -0,0 +1,351 @@
+import { afterEach, describe, expect, it, jest } from "bun:test";
+import { mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const childKey = "T1_NATIVE_REDACTION_CHILD";
+
+if (process.env[childKey] !== "1") {
+ it("runs native redaction checks with an isolated HOME", () => {
+ const home = mkdtempSync(join(tmpdir(), "claudish-t1-native-"));
+ try {
+ const env: Record<string, string | undefined> = {
+ ...process.env,
+ HOME: home,
+ XDG_CONFIG_HOME: home,
+ [childKey]: "1",
+ };
+ for (const key of Object.keys(env)) {
+ if (/KEY|TOKEN|SECRET|PASSWORD|COOKIE/.test(key) && key !== childKey) delete env[key];
+ }
+ const result = Bun.spawnSync([process.execPath, "test", import.meta.path], {
+ env,
+ timeout: 18_000,
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ const output = result.stdout.toString() + result.stderr.toString();
+ console.log(output);
+ expect(result.exitCode, output).toBe(0);
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ }, 20_000);
+} else {
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (() => {
+ throw new Error("T1 forbids network");
+ }) as unknown as typeof fetch;
+ const { Hono } = await import("hono");
+ const { NativeHandler, redactSseErrorEvent } = await import("./native-handler.js");
+ const { acquireSigningLease, redactSigningValues } = await import("../redact.js");
+ const { buildAdvisorPanelPayload } = await import("./advisor-route-plan.js");
+ const { createSigningSecrets, releaseWhenBodyEnds, SIGNING_SECRETS_KEY } = await import(
+ "./shared/call-context.js"
+ );
+ const { setDiagOutput } = await import("../logger.js");
+ const leases: ReturnType<typeof acquireSigningLease>[] = [];
+ const encoder = new TextEncoder();
+ const errorCapture = readFileSync(
+ new URL("../test-fixtures/redaction/zai-instream-error.sse", import.meta.url),
+ "utf8"
+ );
+ const contentCapture = readFileSync(
+ new URL("../test-fixtures/redaction/native-content.sse", import.meta.url),
+ "utf8"
+ );
+ const requestCapture = JSON.parse(
+ readFileSync(
+ new URL("../test-fixtures/redaction/roundtrip-request.json", import.meta.url),
+ "utf8"
+ )
+ );
+ const capturedErrorLine = errorCapture.split("\n").find((line) => line.includes('"request_id"'))!;
+ const capturedError = JSON.parse(capturedErrorLine.slice("data: ".length));
+ const secret = "temporarily"; // A real substring of the captured error, not an invented upstream echo.
+ const nativeModel = JSON.parse(
+ contentCapture
+ .split("\n")
+ .find((line) => line.includes('"type":"message_start"'))!
+ .slice(5)
+ ).message.model;
+ // Derived request: the user turn is copied from roundtrip-string.request.json; the existing builder supplies Anthropic encoding.
+ const payload = buildAdvisorPanelPayload(
+ nativeModel,
+ requestCapture.body.instructions,
+ [
+ {
+ role: requestCapture.body.input[0].role,
+ content: requestCapture.body.input[0].content[0].text,
+ },
+ ],
+ 16
+ );
+ const dataPrefix = capturedErrorLine.slice(0, capturedErrorLine.indexOf("{"));
+ const eventPrefix = errorCapture.split("\n")[0]!.split(":")[0] + ": ";
+ const errorType = Object.keys(capturedError)[0]!;
+ // Derived test data: zai-instream-error.sse supplies the entire error object and SSE field prefixes.
+ // Its existing "error" key supplies the event/type discriminator; no provider message is fabricated.
+ const typedErrorLine = dataPrefix + JSON.stringify({ type: errorType, ...capturedError });
+ const errorEventLine = eventPrefix + errorType;
+
+ function hold(value: string) {
+ const lease = acquireSigningLease();
+ leases.push(lease);
+ lease.add([value]);
+ return lease;
+ }
+ function streamOf(bytes: Uint8Array) {
+ // One-byte chunks also split field names, separators and UTF-8 code points.
+ return new ReadableStream<Uint8Array>({
+ start(controller) {
+ for (const byte of bytes) controller.enqueue(Uint8Array.of(byte));
+ controller.close();
+ },
+ });
+ }
+ function appFor(upstream: () => Response, headers: Record<string, string>) {
+ const app = new Hono();
+ const handler = new NativeHandler(); // No advisor is configured: this is the ordinary native request path.
+ const secrets = createSigningSecrets();
+ leases.push(secrets.lease);
+ const outbound: Request[] = [];
+ globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
+ outbound.push(new Request(input, init));
+ return upstream();
+ }) as unknown as typeof fetch;
+ app.post("/v1/messages", async (c) => {
+ c.set(SIGNING_SECRETS_KEY, secrets);
+ return releaseWhenBodyEnds(await handler.handle(c, payload), () => secrets.lease.release());
+ });
+ return {
+ response: () => app.request("/v1/messages", { method: "POST", headers }),
+ secrets,
+ outbound,
+ };
+ }
+
+ afterEach(() => {
+ globalThis.fetch = (() => {
+ throw new Error("T1 forbids network");
+ }) as unknown as typeof fetch;
+ for (const lease of leases.splice(0)) lease.release();
+ setDiagOutput(null);
+ jest.restoreAllMocks();
+ jest.useRealTimers();
+ });
+ process.on("exit", () => {
+ globalThis.fetch = originalFetch;
+ });
+
+ describe("G4 native passthrough SSE error dispatch", () => {
+ const cases = [
+ {
+ name: "data precedes event: error",
+ lines: [capturedErrorLine, errorEventLine],
+ newline: "\n",
+ tail: "\n\n",
+ },
+ {
+ name: "event: error precedes data",
+ lines: [errorEventLine, capturedErrorLine],
+ newline: "\n",
+ tail: "\n\n",
+ },
+ {
+ name: "JSON type:error without an event line",
+ lines: [typedErrorLine],
+ newline: "\n",
+ tail: "\n\n",
+ },
+ {
+ name: "CRLF event split across byte chunks",
+ lines: [capturedErrorLine, errorEventLine],
+ newline: "\r\n",
+ tail: "\r\n\r\n",
+ },
+ {
+ name: "trailing event without a newline",
+ lines: [capturedErrorLine, errorEventLine],
+ newline: "\n",
+ tail: "",
+ },
+ ];
+ for (const entry of cases) {
+ it(`redacts ${entry.name} and preserves its framing`, async () => {
+ const wire = entry.lines.join(entry.newline) + entry.tail;
+ const expected = wire.replaceAll(secret, "[redacted]");
+ hold(secret);
+ const lines = wire.match(/[^\n]*\n|[^\n]+$/g)!;
+ expect(redactSseErrorEvent(lines).join("")).toBe(expected);
+ const { response } = appFor(
+ () =>
+ new Response(streamOf(encoder.encode(wire)), {
+ headers: { "content-type": "text/event-stream" },
+ }),
+ { authorization: `Bearer ${secret}` }
+ );
+ const result = await response();
+ expect(result.status).toBe(200);
+ expect(await result.text()).toBe(expected);
+ }, 10_000);
+ }
+
+ it("passes ordinary captured content byte-for-byte even when it quotes a registered value", async () => {
+ const firstData = contentCapture
+ .split("\n")
+ .find((line) => line.includes('"type":"message_start"'))!;
+ const value = JSON.parse(firstData.slice(5)).message.id as string;
+ expect(value.length).toBeGreaterThanOrEqual(8);
+ hold(value);
+ expect(redactSseErrorEvent(contentCapture.match(/[^\n]*\n|[^\n]+$/g)!).join("")).toBe(
+ contentCapture
+ );
+ const bytes = encoder.encode(contentCapture);
+ const { response } = appFor(
+ () =>
+ new Response(streamOf(bytes), {
+ headers: { "content-type": "text/event-stream" },
+ }),
+ { authorization: `Bearer ${value}` }
+ );
+ expect(new Uint8Array(await (await response()).arrayBuffer())).toEqual(bytes);
+ }, 10_000);
+
+ it("delivers a complete event before the next event is available", async () => {
+ const separator = contentCapture.indexOf("\n\n") + 2;
+ expect(separator).toBeGreaterThan(1);
+ const first = encoder.encode(contentCapture.slice(0, separator));
+ const rest = encoder.encode(contentCapture.slice(separator));
+ let source!: ReadableStreamDefaultController<Uint8Array>;
+ const { response } = appFor(
+ () =>
+ new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ start(controller) {
+ source = controller;
+ },
+ },
+ { highWaterMark: 0 }
+ ),
+ { headers: { "content-type": "text/event-stream" } }
+ ),
+ { authorization: "Bearer OPAQUE_TEST_TOKEN_T1" }
+ );
+ const result = await response();
+ const reader = result.body!.getReader();
+ source.enqueue(first);
+ expect((await reader.read()).value).toEqual(first);
+ source.enqueue(rest);
+ source.close();
+ const remaining: number[] = [];
+ for (;;) {
+ const chunk = await reader.read();
+ if (chunk.done) break;
+ remaining.push(...chunk.value);
+ }
+ expect(Uint8Array.from(remaining)).toEqual(rest);
+ }, 10_000);
+ });
+
+ describe("G2 native stream lease lifetime (A2)", () => {
+ it("still redacts a late error frame after a year while the native stream is producing", async () => {
+ jest.useFakeTimers({ now: new Date("2026-01-01T00:00:00Z") });
+ const separator = contentCapture.indexOf("\n\n") + 2;
+ const first = encoder.encode(contentCapture.slice(0, separator));
+ let source!: ReadableStreamDefaultController<Uint8Array>;
+ const { response } = appFor(
+ () =>
+ new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ start(controller) {
+ source = controller;
+ },
+ },
+ { highWaterMark: 0 }
+ ),
+ { headers: { "content-type": "text/event-stream" } }
+ ),
+ {
+ authorization: `Bearer ${secret}`,
+ }
+ );
+ const result = await response();
+ const reader = result.body!.getReader();
+ source.enqueue(first);
+ expect((await reader.read()).value).toEqual(first);
+ for (let day = 0; day < 366; day++) jest.advanceTimersByTime(24 * 60 * 60 * 1000);
+ expect(redactSigningValues(secret)).toBe("[redacted]");
+ // Derived late event: the same captured error line, with the captured field prefix and error discriminator.
+ const late = capturedErrorLine + "\n" + errorEventLine + "\n\n";
+ source.enqueue(encoder.encode(late));
+ const later = new TextDecoder().decode((await reader.read()).value);
+ expect(later).toBe(late.replaceAll(secret, "[redacted]"));
+ source.close();
+ expect((await reader.read()).done).toBe(true);
+ expect(redactSigningValues(secret)).toBe(secret);
+ }, 10_000);
+ });
+
+ describe("G3 native client error bodies", () => {
+ it("redacts a non-success JSON body before it is sent to Claude Code", async () => {
+ const { response } = appFor(
+ () =>
+ new Response(JSON.stringify(capturedError), {
+ status: 401,
+ headers: { "content-type": "application/json" },
+ }),
+ { authorization: `Bearer ${secret}` }
+ );
+ const result = await response();
+ expect(result.ok).toBe(false);
+ const text = await result.text();
+ expect(text).toContain("[redacted]");
+ expect(text).not.toContain(secret);
+ expect(JSON.parse(text).error.message).toContain("[redacted]");
+ expect(redactSigningValues(secret)).toBe(secret);
+ }, 10_000);
+ });
+
+ describe("G5 every native request registers outbound credentials", () => {
+ for (const header of ["authorization", "x-api-key"]) {
+ it(`registers the ordinary native request's ${header} before fetch and releases after its body`, async () => {
+ const value = "OPAQUE_TEST_TOKEN_NATIVE";
+ const observations: string[] = [];
+ let source!: ReadableStreamDefaultController<Uint8Array>;
+ const { response, secrets, outbound } = appFor(
+ () => {
+ observations.push(redactSigningValues(value));
+ return new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ start(controller) {
+ source = controller;
+ },
+ },
+ { highWaterMark: 0 }
+ ),
+ { headers: { "content-type": "text/event-stream" } }
+ );
+ },
+ { [header]: header === "authorization" ? `Bearer ${value}` : value }
+ );
+ const result = await response();
+ expect(observations).toEqual(["[redacted]"]);
+ expect(outbound).toHaveLength(1);
+ expect(new URL(outbound[0]!.url).origin).toBe("https://api.anthropic.com");
+ expect(outbound[0]!.headers.get(header)).toBe(
+ header === "authorization" ? `Bearer ${value}` : value
+ );
+ expect(secrets.values.has(value)).toBe(true);
+ expect(redactSigningValues(value)).toBe("[redacted]");
+ source.enqueue(encoder.encode(contentCapture));
+ source.close();
+ await result.text();
+ expect(redactSigningValues(value)).toBe(value);
+ }, 10_000);
+ }
+ });
+}
diff --git a/packages/cli/src/redact-registry.test.ts b/packages/cli/src/redact-registry.test.ts
new file mode 100644
index 00000000..0fa1d1e2
--- /dev/null
+++ b/packages/cli/src/redact-registry.test.ts
@@ -0,0 +1,224 @@
+import { afterEach, describe, expect, it, jest, spyOn } from "bun:test";
+import { mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const childKey = "T1_REDACT_REGISTRY_CHILD";
+
+if (process.env[childKey] !== "1") {
+ it("runs registry and lease checks with an isolated HOME", () => {
+ const home = mkdtempSync(join(tmpdir(), "claudish-t1-registry-"));
+ try {
+ const env: Record<string, string | undefined> = {
+ ...process.env,
+ HOME: home,
+ XDG_CONFIG_HOME: home,
+ [childKey]: "1",
+ };
+ for (const key of Object.keys(env)) {
+ if (/KEY|TOKEN|SECRET|PASSWORD|COOKIE/.test(key) && key !== childKey) delete env[key];
+ }
+ const result = Bun.spawnSync([process.execPath, "test", import.meta.path], {
+ env,
+ timeout: 18_000,
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ const output = result.stdout.toString() + result.stderr.toString();
+ console.log(output);
+ expect(result.exitCode, output).toBe(0);
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ }, 20_000);
+} else {
+ const { acquireSigningLease, redactForClient, redactSecrets, redactSigningValues } = await import(
+ "./redact.js"
+ );
+ const { log, logStderr, setDiagOutput } = await import("./logger.js");
+ const { releaseWhenBodyEnds } = await import("./handlers/shared/call-context.js");
+ const leases: ReturnType<typeof acquireSigningLease>[] = [];
+ const bytes = readFileSync(
+ new URL("./test-fixtures/redaction/native-content.sse", import.meta.url)
+ );
+ const token = "OPAQUE_TEST_TOKEN_T1";
+ const lookalike = "OPAQUE_TEST_TOKEN_T2";
+ function leaseFor(...values: string[]) {
+ const lease = acquireSigningLease();
+ leases.push(lease);
+ lease.add(values);
+ return lease;
+ }
+ function deferred() {
+ let resolve!: () => void;
+ const promise = new Promise<void>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+ }
+
+ afterEach(() => {
+ for (const lease of leases.splice(0)) lease.release();
+ setDiagOutput(null);
+ jest.restoreAllMocks();
+ jest.useRealTimers();
+ });
+
+ describe("G1 signing-value registry sinks", () => {
+ it("redacts opaque registered values in log and logStderr, not look-alikes", () => {
+ leaseFor(token);
+ const diagnostic: string[] = [];
+ const printed: string[] = [];
+ setDiagOutput({ write: (text) => diagnostic.push(text), cleanup() {} });
+ spyOn(console, "log").mockImplementation((...args) => {
+ printed.push(args.join(" "));
+ });
+ const input = `[T1] ${token} ${lookalike}`;
+ log(input, true);
+ const logOutput = [...printed, ...diagnostic].join("\n");
+ expect(logOutput).toContain("[redacted]");
+ expect(logOutput).toContain(lookalike);
+ expect(logOutput).not.toContain(token);
+ diagnostic.length = 0;
+ logStderr(input);
+ expect(diagnostic.join("\n")).toContain("[redacted]");
+ expect(diagnostic.join("\n")).toContain(lookalike);
+ expect(diagnostic.join("\n")).not.toContain(token);
+ }, 10_000);
+
+ it("redactSecrets and redactForClient replace only registered opaque values", () => {
+ const lease = leaseFor(token);
+ const input = `${token} / ${lookalike} / ${token}`;
+ for (const redact of [redactSecrets, redactForClient, redactSigningValues]) {
+ expect(redact(input)).toBe(`[redacted] / ${lookalike} / [redacted]`);
+ }
+ lease.release();
+ expect(redactSigningValues(input)).toBe(input);
+ }, 10_000);
+
+ it("redacts JSON-escaped values repeatedly without regex state (A8)", () => {
+ const escapedToken = 'OPAQUE_TEST_"quoted"\\line\nTOKEN';
+ leaseFor(escapedToken);
+ const input = JSON.stringify({ message: escapedToken, unrelated: lookalike });
+ const expected = JSON.stringify({ message: "[redacted]", unrelated: lookalike });
+ for (let repetition = 0; repetition < 6; repetition++) {
+ expect(redactSigningValues(input)).toBe(expected);
+ expect(redactSecrets(input)).toBe(expected);
+ expect(redactForClient(input)).toBe(expected);
+ expect(JSON.parse(redactForClient(input))).toEqual({
+ message: "[redacted]",
+ unrelated: lookalike,
+ });
+ }
+ }, 10_000);
+
+ it("replaces overlapping values longest first and ignores values shorter than eight", () => {
+ leaseFor(token, `${token}_longer`, "tiny");
+ expect(redactSigningValues(`${token}_longer ${token} tiny`)).toBe(
+ "[redacted] [redacted] tiny"
+ );
+ }, 10_000);
+ });
+
+ describe("G2 signing lease lifetime (A2)", () => {
+ it("holds a producing body's value across a year of injected clock time, then releases at EOF", async () => {
+ jest.useFakeTimers({ now: new Date("2026-01-01T00:00:00Z") });
+ const lease = leaseFor(token);
+ let source!: ReadableStreamDefaultController<Uint8Array>;
+ const response = releaseWhenBodyEnds(
+ new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ start(controller) {
+ source = controller;
+ },
+ },
+ { highWaterMark: 0 }
+ )
+ ),
+ () => lease.release()
+ );
+ const reader = response.body!.getReader();
+ const split = Math.floor(bytes.length / 2);
+ source.enqueue(bytes.subarray(0, split));
+ expect((await reader.read()).value).toEqual(bytes.subarray(0, split));
+ expect(redactForClient(token)).toBe("[redacted]");
+ for (let day = 0; day < 366; day++) jest.advanceTimersByTime(24 * 60 * 60 * 1000);
+ source.enqueue(bytes.subarray(split));
+ expect((await reader.read()).value).toEqual(bytes.subarray(split));
+ expect(redactForClient(token)).toBe("[redacted]");
+ source.close();
+ expect((await reader.read()).done).toBe(true);
+ expect(redactSigningValues(token)).toBe(token);
+ }, 10_000);
+
+ it("cancel with an outstanding read retains the value until upstream teardown settles", async () => {
+ const lease = leaseFor(token);
+ const teardown = deferred();
+ const enteredCancel = deferred();
+ const observations: string[] = [];
+ let releases = 0;
+ const response = releaseWhenBodyEnds(
+ new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ async cancel() {
+ enteredCancel.resolve();
+ await teardown.promise;
+ observations.push(redactForClient(token));
+ },
+ },
+ { highWaterMark: 0 }
+ )
+ ),
+ () => {
+ releases++;
+ lease.release();
+ }
+ );
+ const reader = response.body!.getReader();
+ const pendingRead = reader.read();
+ const cancel = reader.cancel();
+ await enteredCancel.promise;
+ expect((await pendingRead).done).toBe(true);
+ expect(releases).toBe(0);
+ expect(redactForClient(token)).toBe("[redacted]");
+ teardown.resolve();
+ await cancel;
+ expect(observations).toEqual(["[redacted]"]);
+ expect(releases).toBe(1);
+ expect(redactSigningValues(token)).toBe(token);
+ }, 10_000);
+
+ it("overlapping leases retain a shared value until the last idempotent release", () => {
+ const first = leaseFor(token);
+ const second = leaseFor(token);
+ first.add([token]);
+ first.release();
+ first.release();
+ expect(redactForClient(token)).toBe("[redacted]");
+ second.release();
+ second.release();
+ expect(redactSigningValues(token)).toBe(token);
+ }, 10_000);
+
+ it("releases on body error and immediately for a null body", async () => {
+ const lease = leaseFor(token);
+ const failed = releaseWhenBodyEnds(
+ new Response(
+ new ReadableStream<Uint8Array>({
+ start(controller) {
+ controller.error(new Error("T1 source failed"));
+ },
+ })
+ ),
+ () => lease.release()
+ );
+ await expect(failed.text()).rejects.toThrow("T1 source failed");
+ expect(redactSigningValues(token)).toBe(token);
+ const emptyLease = leaseFor(token);
+ releaseWhenBodyEnds(new Response(null, { status: 204 }), () => emptyLease.release());
+ expect(redactSigningValues(token)).toBe(token);
+ }, 10_000);
+ });
+}
diff --git a/packages/cli/src/test-fixtures/redaction/native-content.sse b/packages/cli/src/test-fixtures/redaction/native-content.sse
new file mode 100644
index 00000000..f57e6847
--- /dev/null
+++ b/packages/cli/src/test-fixtures/redaction/native-content.sse
@@ -0,0 +1,134 @@
+# claudish response capture (reconstructed upstream stream)
+# source: production capture reqN=10416, MiniMax-M3 anthropic passthrough (2026-07)
+# The on-disk capture taped the OUTGOING stream (post-clamp): the old clamp
+# remapped content_block_start to sequential indices but let deltas/stops leak
+# at their upstream index. This fixture undoes the start remap (index+1 per start)
+# to restore the upstream stream the parser must absorb: implicit signature block
+# at 0 (signature_delta+stop, no start), text at 1, tool_use at 2.
+# verification: replaying the old clamp on this fixture reproduces the capture
+# event-for-event. Capture sha256=87E1926B52BB7374...
+# ==========================================================
+
+event: ping
+data: {"type":"ping"}
+
+event: message_start
+data: {"type":"message_start","message":{"id":"06c3b9b250191b480da3570519225a87","type":"message","role":"assistant","content":[],"model":"MiniMax-M3","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0,"service_tier":"standard"},"service_tier":"standard"}}
+
+event: ping
+data: {"type":"ping"}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":0}
+
+event: content_block_start
+data: {"type": "content_block_start", "index": 1, "content_block": {"type": "text", "text": ""}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" 8 open PRs touching QuantConnect. #6891 is the"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" **scope-defining issue** for 7 quantbooks"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" honesty. **No active PR on those 7 quantbook"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" paths** (the open PRs are timing drains"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" with .ipynb extension, not the project quant"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"books). Let me also check QC-Py-"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"40/41 PaperTrading status and verify which projects have"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":" config.json:"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":1}
+
+event: content_block_start
+data: {"type": "content_block_start", "index": 2, "content_block": {"type": "tool_use", "id": "call_991f0dd36bb64cab89b91c94", "name": "Bash", "input": {}}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"{\"command\": \"cd \\\"d:/Dev/Cours"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"IA-2\\\" && ls -la MyIA.A"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"I.Notebooks/QuantConnect/projects/ 2>&1 |"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" head -3 && echo \\\"--- projects list ---"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"\\\" && ls -1 MyIA.AI.Not"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"ebooks/QuantConnect/projects/ 2>&1 | grep -"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"E \\\"AllWeather|DualMomentum|EMA"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"-Cross-Alpha|FuturesTrend|MomentumStrategy|R"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"iskParity|SectorMomentum|TurnOfMonth\\\" |"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" head -10 && echo \\\"--- config.json check"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" ---\\\" && for p in AllWeather DualMomentum EMA"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"-Cross-Alpha FuturesTrend MomentumStrategy RiskParity Sector"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"Momentum TurnOfMonth; do\\n if"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" [ -f \\\"MyIA.AI.Notebooks/"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"QuantConnect/projects/${p}/config.json\\\" ]; then"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"\\n echo \\\"${p}/config.json EXISTS\\\"\\n "}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" cat \\\"MyIA.AI.Notebooks/QuantConnect"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"/projects/${p}/config.json\\\" 2>&1"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" | head -5\\n echo \\\"\\\"\\n fi"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"\\ndone\", \"description\": \"Check"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" which 7 quantbooks have config.json (QC"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":" Cloud presence)\"}"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":2}
+
+event: message_delta
+data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"input_tokens":150556,"output_tokens":339,"cache_read_input_tokens":2176,"service_tier":"standard"}}
+
+event: message_stop
+data: {"type":"message_stop"}
+
diff --git a/packages/cli/src/test-fixtures/redaction/roundtrip-request.json b/packages/cli/src/test-fixtures/redaction/roundtrip-request.json
new file mode 100644
index 00000000..5cd16812
--- /dev/null
+++ b/packages/cli/src/test-fixtures/redaction/roundtrip-request.json
@@ -0,0 +1,61 @@
+{
+ "headers": {
+ "authorization": "<redacted>",
+ "chatgpt-account-id": "<redacted>",
+ "content-type": "application/json",
+ "accept": "text/event-stream",
+ "originator": "codex_exec",
+ "user-agent": "codex_exec/0.161.0 (variant-probe)",
+ "version": "0.161.0"
+ },
+ "body": {
+ "model": "gpt-6.1-sol",
+ "stream": true,
+ "store": false,
+ "tool_choice": "auto",
+ "parallel_tool_calls": false,
+ "include": ["reasoning.encrypted_content"],
+ "reasoning": {
+ "effort": "low"
+ },
+ "text": {
+ "verbosity": "low"
+ },
+ "instructions": "You are a coding agent. Use the exec tool for any file access.",
+ "tools": [
+ {
+ "type": "custom",
+ "name": "exec",
+ "description": "Run JavaScript code to orchestrate/compose tool calls\n- Evaluates the provided JavaScript code in a fresh V8 isolate as an async module.\n- All nested tools are available on the global `tools` object, for example `await tools.exec_command(...)`. Tool names are exposed as normalized JavaScript identifiers, for example `await tools.mcp__ologs__get_profile(...)`.\n- Nested tool methods take either a string or an object as their input argument.\n- Nested tools return either an object or a string, based on the description.\n- Runs raw JavaScript -- no Node, no file system, no network access, no console.\n- Accepts raw JavaScript source text, not JSON, quoted strings, or markdown code fences.\n- You may optionally start the tool input with a first-line pragma like `// @exec: {\"yield_time_ms\": 10000, \"max_output_tokens\": 1000}`.\n- `yield_time_ms` asks `exec` to yield early if the script is still running. Defaults to 30000 ms.\n- `max_output_tokens` sets the token budget for direct `exec` results. Defaults to 10000 tokens.\n- When the JS code is fully evaluated, the isolate's lifetime ends and unawaited promises are silently discarded.\n\n- Global helpers:\n- `exit()`: Immediately ends the current script successfully (like an early return from the top level).\n- `text(value: string | number | boolean | undefined | null)`: Appends a text item. Non-string values are stringified with `JSON.stringify(...)` when possible.\n- `image(imageUrlOrItem: string | { image_url: string; detail?: \"auto\" | \"low\" | \"high\" | \"original\" | null } | ImageContent, detail?: \"auto\" | \"low\" | \"high\" | \"original\" | null)`: Appends an image item. `image_url` should be a base64-encoded `data:` URL. To forward an MCP tool image, pass an individual `ImageContent` block from `result.content`, for example `image(result.content[0])`. MCP image blocks may request detail with `_meta: { \"codex/imageDetail\": \"original\" }`. When provided, the second `detail` argument overrides any detail embedded in the first argument.\n- `audio(audioUrlOrItem: string | { audio_url: string } | AudioContent)`: Appends an audio item. `audio_url` should be a base64-encoded `data:` URL. To forward an MCP tool audio block, pass an individual `AudioContent` block from `result.content`, for example `audio(result.content[0])`.\n- `generatedImage(result: { image_url: string; output_hint?: string })`: Appends an image-generation result and its optional output hint. HTTP(S) URLs are not supported.\n- `store(key: string, value: any)`: stores a serializable value under a string key for later `exec` calls in the same session.\n- `load(key: string)`: returns the stored value for a string key, or `undefined` if it is missing.\n- `notify(value: string | number | boolean | undefined | null)`: immediately injects an extra `custom_tool_call_output` for the current `exec` call. Values are stringified like `text(...)`.\n- `setTimeout(callback: () => void, delayMs?: number)`: schedules a callback to run later and returns a timeout id. Pending timeouts do not keep `exec` alive by themselves; await an explicit promise if you need to wait for one.\n- `clearTimeout(timeoutId?: number)`: cancels a timeout created by `setTimeout`.\n- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description }` entries.\n- `yield_control()`: yields the accumulated output to the model immediately while the script keeps running.\n\nSome deferred nested tools may be omitted from this description. They are still available on the global `tools` object and listed in `ALL_TOOLS`.\nTo find one, filter `ALL_TOOLS` by `name` and `description`.\n\n\n### `exec_command`\nRuns a command.\n\nexec tool declaration:\n```ts\ndeclare const tools: { exec_command(args: {\n // Shell command to execute.\n cmd: string;\n}): Promise<{ output: string; exit_code?: number; wall_time_seconds: number }>; };\n```",
+ "format": {
+ "type": "grammar",
+ "syntax": "lark",
+ "definition": "\nstart: pragma_source | plain_source\npragma_source: PRAGMA_LINE NEWLINE SOURCE\nplain_source: SOURCE\n\nPRAGMA_LINE: /[ \\t]*\\/\\/ @exec:[^\\r\\n]*/\nNEWLINE: /\\r?\\n/\nSOURCE: /[\\s\\S]+/\n"
+ }
+ }
+ ],
+ "input": [
+ {
+ "type": "message",
+ "role": "user",
+ "content": [
+ {
+ "type": "input_text",
+ "text": "Read README.md and notes.txt in parallel and report line counts."
+ }
+ ]
+ },
+ {
+ "type": "custom_tool_call",
+ "call_id": "call_probe_1",
+ "name": "exec",
+ "input": "const r = await Promise.all([tools.exec_command({cmd:\"wc -l README.md\"}), tools.exec_command({cmd:\"wc -l notes.txt\"})]);\nr.forEach((x)=>text(x.output));\n"
+ },
+ {
+ "type": "custom_tool_call_output",
+ "call_id": "call_probe_1",
+ "output": "Script completed\nWall time 0.2 seconds\nOutput:\n 6 README.md\n 3 notes.txt\n"
+ }
+ ]
+ }
+}
diff --git a/packages/cli/src/test-fixtures/redaction/zai-instream-error.sse b/packages/cli/src/test-fixtures/redaction/zai-instream-error.sse
new file mode 100644
index 00000000..28c75724
--- /dev/null
+++ b/packages/cli/src/test-fixtures/redaction/zai-instream-error.sse
@@ -0,0 +1,11 @@
+event: message_start
+data: {"type":"message_start","message":{"id":"msg_err_test","type":"message","role":"assistant","content":[],"model":"glm-5.1","stop_reason":null,"usage":{"input_tokens":100,"output_tokens":1}}}
+
+event: content_block_start
+data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}
+
+data: {"error":{"code":"1305","message":"The service may be temporarily overloaded, please try again later"},"request_id":"202604091053007734acf4dc554292"}
+