Multiplayer agent harness for work.
Show gated call arguments in strict-posture approvals (#776) (#1702)
Sina Matian50beec778d39
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.tsindex 26c04ed03..5abd3f6f9 100644--- a/src/harness/agent-tools.ts+++ b/src/harness/agent-tools.ts@@ -16,6 +16,8 @@ import type { McpToolDescriptor } from "../mcp/mcp-tool-service.ts";import { splitToScope } from "../api/artifact-share.ts";import { errMessage } from "../util/errors.ts";import { computerVerdict } from "../sandbox/sandbox.ts";+import { redactCommand } from "../sandbox/exec-process-session.ts";+import { redactSecrets } from "./redact-secrets.ts";import { isObj } from "../util/objects.ts";import { BOT_MODES } from "../surface-cache/channel-policy-store.ts";import { headSlice, tailSlice } from "../util/text.ts";@@ -62,6 +64,7 @@ export interface ToolContextRef {matched?: string;purpose?: string;approvalKey?: string;+ summary?: string;grantModes?: { session: boolean; always: boolean };}>;pausedOnApproval?: boolean;@@ -4388,6 +4391,7 @@ function withToolApprovalGate(reason: STRICT_TOOL_APPROVAL_REASON,kind: "approval",approvalKey: `tool:${approvalIdentity}`,+ ...(params === undefined ? {} : { summary: redactCommand(redactSecrets(JSON.stringify(params))) }),...(tool.name === "sandbox" && isObj(params) && typeof params.purpose === "string"? { purpose: params.purpose }: {}),diff --git a/src/harness/codex-app-server.ts b/src/harness/codex-app-server.tsindex 9ae310425..559f285fe 100644--- a/src/harness/codex-app-server.ts+++ b/src/harness/codex-app-server.ts@@ -1,5 +1,6 @@import { spawn, type ChildProcess } from "node:child_process";import { errMessage } from "../util/errors.ts";+import { redactSecrets } from "./redact-secrets.ts";export class CodexRpcError extends Error {constructor(message: string) {@@ -46,75 +47,6 @@ function isJsonRpcMessage(value: unknown): value is JsonRpcMessage {return true;}-const CODEX_DIAGNOSTIC_SENSITIVE_KEYS = new Set([- "accesstoken",- "refreshtoken",- "idtoken",- "apikey",- "clientsecret",- "credential",- "credentials",- "password",- "passphrase",- "secret",- "token",- "authorization",- "proxyauthorization",- "cookie",- "setcookie",-]);--function diagnosticKeyIsSensitive(key: string): boolean {- return CODEX_DIAGNOSTIC_SENSITIVE_KEYS.has(key.toLowerCase().replace(/[^a-z]/g, ""));-}--function redactStructuredDiagnosticsValue(value: unknown, sensitive = false): unknown {- if (sensitive) return "[redacted]";- if (Array.isArray(value)) return value.map((item) => redactStructuredDiagnosticsValue(item));- if (!value || typeof value !== "object") return value;- return Object.fromEntries(- Object.entries(value).map(([key, item]) => [- key,- redactStructuredDiagnosticsValue(item, diagnosticKeyIsSensitive(key)),- ]),- );-}--function redactStructuredDiagnostics(value: string): string {- try {- const parsed: unknown = JSON.parse(value);- if (!parsed || typeof parsed !== "object") return value;- return JSON.stringify(redactStructuredDiagnosticsValue(parsed));- } catch {- return value;- }-}--export function redactCodexDiagnostics(value: string): string {- return redactStructuredDiagnostics(value)- .replace(- /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)\[[\s\S]*?(?:\]|$)/gi,- "$1[redacted]",- )- .replace(- /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)\{[\s\S]*$/gi,- "$1{redacted}",- )- .replace(- /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)(["'])(?:(?:\\[\s\S])|(?!\2)[\s\S])*(?:\2|$)/gi,- "$1$2[redacted]$2",- )- .replace(- /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)(?!(?:["']|\[))[^,\r\n}\]]+/gi,- "$1[redacted]",- )- .replace(/\b(?:Basic|Digest)\s+\S+/gi, "[redacted]")- .replace(/\bBearer\s+\S+/gi, "Bearer [redacted]")- .replace(/\bsk-[A-Za-z0-9._-]{8,}/g, "[redacted]")- .replace(/\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g, "[redacted]")- .replace(/\b(?=[A-Za-z0-9_-]*\d)[A-Za-z0-9_-]{32,}\b/g, "[redacted]");-}-export interface CodexAppServerOptions {binaryPath: string;cwd: string;@@ -176,7 +108,7 @@ export class CodexAppServer {});this.process.once("close", (code, signal) => {this.closed = true;- const stderr = redactCodexDiagnostics(this.stderr.trim());+ const stderr = redactSecrets(this.stderr.trim());this.closeError = new Error(`Codex app-server exited (${code ?? signal ?? "unknown"})${stderr ? `: ${stderr}` : ""}`,);@@ -267,7 +199,7 @@ export class CodexAppServer {if (!isJsonRpcMessage(parsed)) throw new Error("Codex app-server emitted an invalid JSON-RPC message");message = parsed;} catch {- throw new Error(redactCodexDiagnostics(`Codex app-server emitted invalid JSON: ${line.slice(0, 500)}`));+ throw new Error(redactSecrets(`Codex app-server emitted invalid JSON: ${line.slice(0, 500)}`));}if (message.id !== undefined && !message.method) {const waiter = this.pending.get(message.id);@@ -283,7 +215,7 @@ export class CodexAppServer {}waiter.reject(new CodexRpcError(- redactCodexDiagnostics(+ redactSecrets(`Codex ${message.error.code ?? "error"}: ${message.error.message ?? JSON.stringify(message.error.data)}`,),),diff --git a/src/harness/codex-harness.ts b/src/harness/codex-harness.tsindex 2c4e8143b..5a1504328 100644--- a/src/harness/codex-harness.ts+++ b/src/harness/codex-harness.ts@@ -12,7 +12,8 @@ import type { LlmCallUsage } from "../sessions/session-store.ts";import type { ScopeId, SessionEntry } from "../types.ts";import { asError, swallow } from "../util/errors.ts";import { countTokens } from "../util/tokens.ts";-import { CodexAppServer, CodexRpcError, redactCodexDiagnostics } from "./codex-app-server.ts";+import { CodexAppServer, CodexRpcError } from "./codex-app-server.ts";+import { redactSecrets } from "./redact-secrets.ts";import { codexAuthFileForEnv, readCodexOAuthAuthFile } from "./codex-auth.ts";import {childCodexAuthFromDerived,@@ -172,7 +173,7 @@ export function codexNonRetryable(message: string): boolean {}export function codexProviderFailure(message: string): Error {- const safe = redactCodexDiagnostics(message);+ const safe = redactSecrets(message);return codexNonRetryable(safe) ? new NonRetryableTurnError(safe) : new Error(safe);}export function codexChildToolAllowed(name: string, args?: unknown): boolean {diff --git a/src/harness/redact-secrets.ts b/src/harness/redact-secrets.tsnew file mode 100644index 000000000..59332a9d0--- /dev/null+++ b/src/harness/redact-secrets.ts@@ -0,0 +1,68 @@+const SENSITIVE_KEYS = new Set([+ "accesstoken",+ "refreshtoken",+ "idtoken",+ "apikey",+ "clientsecret",+ "credential",+ "credentials",+ "password",+ "passphrase",+ "secret",+ "token",+ "authorization",+ "proxyauthorization",+ "cookie",+ "setcookie",+]);++function diagnosticKeyIsSensitive(key: string): boolean {+ return SENSITIVE_KEYS.has(key.toLowerCase().replace(/[^a-z]/g, ""));+}++function redactStructuredDiagnosticsValue(value: unknown, sensitive = false): unknown {+ if (sensitive) return "[redacted]";+ if (Array.isArray(value)) return value.map((item) => redactStructuredDiagnosticsValue(item));+ if (!value || typeof value !== "object") return value;+ return Object.fromEntries(+ Object.entries(value).map(([key, item]) => [+ key,+ redactStructuredDiagnosticsValue(item, diagnosticKeyIsSensitive(key)),+ ]),+ );+}++function redactStructuredDiagnostics(value: string): string {+ try {+ const parsed: unknown = JSON.parse(value);+ if (!parsed || typeof parsed !== "object") return value;+ return JSON.stringify(redactStructuredDiagnosticsValue(parsed));+ } catch {+ return value;+ }+}++export function redactSecrets(value: string): string {+ return redactStructuredDiagnostics(value)+ .replace(+ /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)\[[\s\S]*?(?:\]|$)/gi,+ "$1[redacted]",+ )+ .replace(+ /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)\{[\s\S]*$/gi,+ "$1{redacted}",+ )+ .replace(+ /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)(["'])(?:(?:\\[\s\S])|(?!\2)[\s\S])*(?:\2|$)/gi,+ "$1$2[redacted]$2",+ )+ .replace(+ /(["']?(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|apikey|client[_-]?secret|credential|credentials|password|passphrase|secret|token|authorization|proxy-authorization|cookie|set-cookie)["']?\s*[:=]\s*)(?!(?:["']|\[))[^,\r\n}\]]+/gi,+ "$1[redacted]",+ )+ .replace(/\b(?:Basic|Digest)\s+\S+/gi, "[redacted]")+ .replace(/\bBearer\s+\S+/gi, "Bearer [redacted]")+ .replace(/\bsk-[A-Za-z0-9._-]{8,}/g, "[redacted]")+ .replace(/\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g, "[redacted]")+ .replace(/\b(?=[A-Za-z0-9_-]*\d)[A-Za-z0-9_-]{32,}\b/g, "[redacted]");+}diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.tsindex 7602bcd5a..54702218e 100644--- a/test/agent-tools.test.ts+++ b/test/agent-tools.test.ts@@ -2948,6 +2948,25 @@ test("unified sandbox keeps strict approval and quarantined output associated wiassert.equal(entries[1]?.quarantined, true);});+test("strict approval records carry the gated call's redacted arguments", async () => {+ const ref: ToolContextRef = {+ current: fakeToolContext(),+ pendingApprovals: [],+ scopeLabel: "personal:U1",+ toolApprovalGate: () => false,+ };+ const tool = createAgentTools(ref, { sandboxResources: true }).find((t) => t.name === "sandbox")!;+ await call(tool, { action: "exec", command: "ls /tmp" });+ await call(tool, { action: "exec", command: "curl --token s3cr3t-value https://example.com" });+ await call(tool, { action: "exec", command: "env", api_key: "k3y-value" });+ const [a, b, c] = ref.pendingApprovals!;+ assert.equal(a?.command, b?.command);+ assert.match(a?.summary ?? "", /ls \/tmp/);+ assert.match(b?.summary ?? "", /curl --token <redacted>/);+ assert.doesNotMatch(b?.summary ?? "", /s3cr3t-value/);+ assert.doesNotMatch(c?.summary ?? "", /k3y-value/);+});+test("unscreened unified output retains the called action in the durable transcript", async () => {const entries: Array<Record<string, unknown>> = [];const tool = createAgentTools(diff --git a/test/codex-harness.test.ts b/test/codex-harness.test.tsindex 3b3878ca9..214100696 100644--- a/test/codex-harness.test.ts+++ b/test/codex-harness.test.ts@@ -33,7 +33,8 @@ import { createMemoryRunSignalStore } from "../src/runs/run-signal-store.ts";import { NonRetryableTurnError } from "../src/core/turn-error.ts";import type { ScopeId, Session, SessionEntry } from "../src/types.ts";import { createMemoryTaskStore } from "../src/tasks/memory-task-store.ts";-import { CodexAppServer, redactCodexDiagnostics } from "../src/harness/codex-app-server.ts";+import { CodexAppServer } from "../src/harness/codex-app-server.ts";+import { redactSecrets } from "../src/harness/redact-secrets.ts";import { DEFAULT_CODEX_MODEL_ID } from "../src/model/pi-models.ts";import { readCodexOAuthAuthFile } from "../src/harness/codex-auth.ts";import { acquireCodexOAuthAuthLock } from "../src/harness/codex-auth.ts";@@ -719,12 +720,12 @@ test("Codex materializes ChatGPT OAuth auth as ephemeral child material withouttest("Codex diagnostics redact credential-shaped stderr", () => {assert.equal(- redactCodexDiagnostics(+ redactSecrets('{"access_token":"access-secret","refresh_token":"refresh-secret"} Bearer bearer-secret-123456789 sk-secret-value',),'{"access_token":"[redacted]","refresh_token":"[redacted]"} Bearer [redacted] [redacted]',);- const diagnostics = redactCodexDiagnostics(+ const diagnostics = redactSecrets("Authorization: Basic basic-secret-123456 Cookie: session-cookie-secret; Set-Cookie: refresh-cookie-secret; X-Api-Key: api-secret-123456 accessToken=camel-secret-123456 token=generic-secret-123456",);for (const secret of [@@ -736,19 +737,19 @@ test("Codex diagnostics redact credential-shaped stderr", () => {"generic-secret-123456",])assert.equal(diagnostics.includes(secret), false, secret);- const structured = redactCodexDiagnostics('authorization=["Bearer array-secret"] access_token="unterminated-secret');+ const structured = redactSecrets('authorization=["Bearer array-secret"] access_token="unterminated-secret');assert.equal(structured.includes("array-secret"), false);assert.equal(structured.includes("unterminated-secret"), false);- const arrayDiagnostics = redactCodexDiagnostics('access_token=["first-array-secret","second-array-secret"]');+ const arrayDiagnostics = redactSecrets('access_token=["first-array-secret","second-array-secret"]');assert.equal(arrayDiagnostics.includes("first-array-secret"), false);assert.equal(arrayDiagnostics.includes("second-array-secret"), false);- const malformedArray = redactCodexDiagnostics('access_token=["first-array-secret",\n"second-array-secret"');+ const malformedArray = redactSecrets('access_token=["first-array-secret",\n"second-array-secret"');assert.equal(malformedArray.includes("first-array-secret"), false);assert.equal(malformedArray.includes("second-array-secret"), false);- const malformedObject = redactCodexDiagnostics('access_token={"a":"first-object-secret","b":"second-object-secret"}');+ const malformedObject = redactSecrets('access_token={"a":"first-object-secret","b":"second-object-secret"}');assert.equal(malformedObject.includes("first-object-secret"), false);assert.equal(malformedObject.includes("second-object-secret"), false);- const nested = redactCodexDiagnostics(+ const nested = redactSecrets(JSON.stringify({nested: { authorization: { header: "Bearer nested-secret" } },tokens: { access_token: ["one-secret"] },@@ -756,8 +757,8 @@ test("Codex diagnostics redact credential-shaped stderr", () => {);assert.equal(nested.includes("nested-secret"), false);assert.equal(nested.includes("one-secret"), false);- assert.equal(redactCodexDiagnostics("id_token=header.payload.signature").includes("header.payload.signature"), false);- const generic = redactCodexDiagnostics(+ assert.equal(redactSecrets("id_token=header.payload.signature").includes("header.payload.signature"), false);+ const generic = redactSecrets(JSON.stringify({secret: "generic-secret",password: "generic-password",