Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(uploader): scrubber no longer corrupts code in file tool calls and diffs

ogsamrat35eaaa6f5024
diff --git a/apps/server/src/__fixtures__/scrub-file-mode.json b/apps/server/src/__fixtures__/scrub-file-mode.json
new file mode 100644
index 00000000..33e32786
--- /dev/null
+++ b/apps/server/src/__fixtures__/scrub-file-mode.json
@@ -0,0 +1,1368 @@
+[
+ {
+ "name": "opencode edit of a .ts file keeps code in oldString/newString",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/a.ts",
+ "oldString": "export function f(){}",
+ "newString": "export async function f(token:string){await fetch(u)}"
+ },
+ "output": "Edit applied successfully.",
+ "metadata": {
+ "diff": "Index: /work/proj/src/a.ts\n===================================================================\n--- /work/proj/src/a.ts\n+++ /work/proj/src/a.ts\n@@ -1,1 +1,1 @@\n-export function f(){}\n+export async function f(token:string){await fetch(u)}\n",
+ "filediff": {
+ "file": "/work/proj/src/a.ts",
+ "before": "export function f(){}\n",
+ "after": "export async function f(token:string){await fetch(u)}\n",
+ "additions": 1,
+ "deletions": 1
+ }
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/a.ts",
+ "oldString": "export function f(){}",
+ "newString": "export async function f(token:string){await fetch(u)}"
+ },
+ "output": "Edit applied successfully.",
+ "metadata": {
+ "diff": "Index: /work/proj/src/a.ts\n===================================================================\n--- /work/proj/src/a.ts\n+++ /work/proj/src/a.ts\n@@ -1,1 +1,1 @@\n-export function f(){}\n+export async function f(token:string){await fetch(u)}\n",
+ "filediff": {
+ "file": "/work/proj/src/a.ts",
+ "before": "export function f(){}\n",
+ "after": "export async function f(token:string){await fetch(u)}\n",
+ "additions": 1,
+ "deletions": 1
+ }
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "opencode write of a .ts file keeps code with a real newline",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/b.ts",
+ "content": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/b.ts",
+ "content": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "pi edit with edits sent as a JSON string: escapes are never eaten",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "path": "src/c.ts",
+ "edits": "[{\"oldText\": \"a\", \"newText\": \"async(token:string)=>{}\\nfunction a(token:string){await b}\"}]"
+ },
+ "output": "ok"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "path": "src/c.ts",
+ "edits": "[{\"oldText\": \"a\", \"newText\": \"async(token:string)=>{}\\nfunction a(token:string){await b}\"}]"
+ },
+ "output": "ok"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "pi edit with an edits array and a details diff",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "path": "src/c.ts",
+ "edits": [
+ {
+ "oldText": "x",
+ "newText": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ }
+ ]
+ },
+ "output": "ok",
+ "metadata": {
+ "diff": "-1 x\n+1 const g = async(token:string)=>{}\n+2 function a(token:string){await b}",
+ "firstChangedLine": 1
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "edit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "path": "src/c.ts",
+ "edits": [
+ {
+ "oldText": "x",
+ "newText": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ }
+ ]
+ },
+ "output": "ok",
+ "metadata": {
+ "diff": "-1 x\n+1 const g = async(token:string)=>{}\n+2 function a(token:string){await b}",
+ "firstChangedLine": 1
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "the same code in a bash command keeps CONFIG treatment",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "bash",
+ "state": {
+ "status": "completed",
+ "input": {
+ "command": "cat > a.ts <<'EOF'\nexport async function f(token:string){await fetch(u)}\nEOF",
+ "description": "write"
+ },
+ "output": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "bash",
+ "state": {
+ "status": "completed",
+ "input": {
+ "command": "cat > a.ts <<'EOF'\nexport async function f(token:[REDACTED] fetch(u)}\nEOF",
+ "description": "write"
+ },
+ "output": "const g = async(token:[REDACTED]\nfunction a(token:[REDACTED] b}"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "a file tool call with no path keeps CONFIG treatment",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "content": "export async function f(token:string){await fetch(u)}"
+ },
+ "output": "?"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "content": "export async function f(token:[REDACTED] fetch(u)}"
+ },
+ "output": "?"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": ".env write still redacts a secret assignment",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/.env",
+ "content": "API_TOKEN=abcdefgh123\nDEBUG=1\n"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/.env",
+ "content": "API_TOKEN=[REDACTED]\nDEBUG=1\n"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": ".ts write still redacts a provider key",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/k.ts",
+ "content": "const k = \"sk-proj-AAAAAAAAAAAAAAAAAAAA\"\nexport async function f(token:string){await fetch(u)}"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/k.ts",
+ "content": "const k = \"[REDACTED]\"\nexport async function f(token:string){await fetch(u)}"
+ },
+ "output": "Wrote file successfully."
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": ".ts write still redacts a quoted secret literal and a private key",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "src/cfg.ts",
+ "content": "const apiKey = \"abc123def456ghi\";\nconst pem = `-----BEGIN PRIVATE KEY-----\nMIIEv\n-----END PRIVATE KEY-----`;\n"
+ },
+ "output": "ok"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "src/cfg.ts",
+ "content": "const apiKey = \"[REDACTED]\";\nconst pem = `[REDACTED]`;\n"
+ },
+ "output": "ok"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "apply_patch scrubs each file of the patch with its own mode",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "apply_patch",
+ "state": {
+ "status": "completed",
+ "input": {
+ "patchText": "*** Begin Patch\n*** Update File: src/a.ts\n@@\n-x\n+export async function f(token:string){await fetch(u)}\n*** Add File: .env\n+API_TOKEN=abcdefgh123\n*** End Patch"
+ },
+ "output": "Success. Updated the following files:\nM src/a.ts\nA .env",
+ "metadata": {
+ "diff": "Index: /work/proj/src/a.ts\n--- /work/proj/src/a.ts\n+++ /work/proj/src/a.ts\n@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n\nIndex: /work/proj/.env\n--- /work/proj/.env\n+++ /work/proj/.env\n@@ -0,0 +1 @@\n+API_TOKEN=abcdefgh123\n",
+ "files": [
+ {
+ "filePath": "/work/proj/src/a.ts",
+ "relativePath": "src/a.ts",
+ "type": "update",
+ "patch": "@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n",
+ "before": "x\n",
+ "after": "export async function f(token:string){await fetch(u)}\n"
+ },
+ {
+ "filePath": "/work/proj/.env",
+ "relativePath": ".env",
+ "type": "add",
+ "patch": "@@ -0,0 +1 @@\n+API_TOKEN=abcdefgh123\n",
+ "after": "API_TOKEN=abcdefgh123\n"
+ }
+ ],
+ "v2": {
+ "files": [
+ {
+ "file": "src/a.ts",
+ "patch": "+export async function f(token:string){await fetch(u)}\n",
+ "status": "modified"
+ },
+ {
+ "file": ".env",
+ "patch": "+API_TOKEN=abcdefgh123\n",
+ "status": "added"
+ }
+ ]
+ }
+ }
+ },
+ "name": "patch"
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "apply_patch",
+ "state": {
+ "status": "completed",
+ "input": {
+ "patchText": "*** Begin Patch\n*** Update File: src/a.ts\n@@\n-x\n+export async function f(token:string){await fetch(u)}\n*** Add File: .env\n+API_TOKEN=[REDACTED]\n*** End Patch"
+ },
+ "output": "Success. Updated the following files:\nM src/a.ts\nA .env",
+ "metadata": {
+ "diff": "Index: /work/proj/src/a.ts\n--- /work/proj/src/a.ts\n+++ /work/proj/src/a.ts\n@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n\nIndex: /work/proj/.env\n--- /work/proj/.env\n+++ /work/proj/.env\n@@ -0,0 +1 @@\n+API_TOKEN=[REDACTED]\n",
+ "files": [
+ {
+ "filePath": "/work/proj/src/a.ts",
+ "relativePath": "src/a.ts",
+ "type": "update",
+ "patch": "@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n",
+ "before": "x\n",
+ "after": "export async function f(token:string){await fetch(u)}\n"
+ },
+ {
+ "filePath": "/work/proj/.env",
+ "relativePath": ".env",
+ "type": "add",
+ "patch": "@@ -0,0 +1 @@\n+API_TOKEN=[REDACTED]\n",
+ "after": "API_TOKEN=[REDACTED]\n"
+ }
+ ],
+ "v2": {
+ "files": [
+ {
+ "file": "src/a.ts",
+ "patch": "+export async function f(token:string){await fetch(u)}\n",
+ "status": "modified"
+ },
+ {
+ "file": ".env",
+ "patch": "+API_TOKEN=[REDACTED]\n",
+ "status": "added"
+ }
+ ]
+ }
+ }
+ },
+ "name": "patch"
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "multiedit scrubs each edit with its own file's mode",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "multiedit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "src/m.ts",
+ "edits": [
+ {
+ "filePath": "src/m.ts",
+ "oldString": "a",
+ "newString": "export async function f(token:string){await fetch(u)}"
+ },
+ {
+ "filePath": "conf/app.ini",
+ "oldString": "a",
+ "newString": "auth_token=abcdefgh123"
+ }
+ ]
+ },
+ "output": "ok",
+ "metadata": {
+ "results": [
+ {
+ "diff": "@@\n+export async function f(token:string){await fetch(u)}\n"
+ }
+ ]
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "multiedit",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "src/m.ts",
+ "edits": [
+ {
+ "filePath": "src/m.ts",
+ "oldString": "a",
+ "newString": "export async function f(token:string){await fetch(u)}"
+ },
+ {
+ "filePath": "conf/app.ini",
+ "oldString": "a",
+ "newString": "auth_token=[REDACTED]"
+ }
+ ]
+ },
+ "output": "ok",
+ "metadata": {
+ "results": [
+ {
+ "diff": "@@\n+export async function f(token:string){await fetch(u)}\n"
+ }
+ ]
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "read output of a .ts file keeps code; of a .env file is redacted",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "read",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/a.ts"
+ },
+ "output": "<file>\n00001| export async function f(token:string){await fetch(u)}\n</file>",
+ "content": [
+ {
+ "type": "text",
+ "text": "1: export async function f(token:string){await fetch(u)}"
+ }
+ ],
+ "metadata": {
+ "preview": "export async function f(token:string){await fetch(u)}",
+ "truncated": false
+ }
+ }
+ },
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "read",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/.env.local"
+ },
+ "output": "1: API_TOKEN=abcdefgh123",
+ "metadata": {
+ "preview": "API_TOKEN=abcdefgh123"
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "read",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/src/a.ts"
+ },
+ "output": "<file>\n00001| export async function f(token:string){await fetch(u)}\n</file>",
+ "content": [
+ {
+ "type": "text",
+ "text": "1: export async function f(token:string){await fetch(u)}"
+ }
+ ],
+ "metadata": {
+ "preview": "export async function f(token:string){await fetch(u)}",
+ "truncated": false
+ }
+ }
+ },
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "read",
+ "state": {
+ "status": "completed",
+ "input": {
+ "filePath": "/work/proj/.env.local"
+ },
+ "output": "1: API_TOKEN=[REDACTED]",
+ "metadata": {
+ "preview": "API_TOKEN=[REDACTED]"
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "2.x tool part: native input path under v2 selects the mode",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_9",
+ "tool": "write",
+ "name": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "content": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ },
+ "v2": {
+ "input": {
+ "path": "src/n.ts",
+ "content": "export async function f(token:string){await fetch(u)}"
+ }
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_9",
+ "tool": "write",
+ "name": "write",
+ "state": {
+ "status": "completed",
+ "input": {
+ "content": "const g = async(token:string)=>{}\nfunction a(token:string){await b}"
+ },
+ "v2": {
+ "input": {
+ "path": "src/n.ts",
+ "content": "export async function f(token:string){await fetch(u)}"
+ }
+ }
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "other tool output keeps CONFIG treatment",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "grep",
+ "state": {
+ "status": "completed",
+ "input": {
+ "pattern": "token",
+ "path": "src/a.ts"
+ },
+ "output": "src/a.ts:\n Line 1: export async function f(token:string){await fetch(u)}"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:00.000Z",
+ "type": "turn.messages",
+ "data": {
+ "messages": [
+ {
+ "info": {
+ "id": "msg_1",
+ "role": "assistant"
+ },
+ "parts": [
+ {
+ "type": "tool",
+ "callID": "call_1",
+ "tool": "grep",
+ "state": {
+ "status": "completed",
+ "input": {
+ "pattern": "token",
+ "path": "src/a.ts"
+ },
+ "output": "src/a.ts:\n Line 1: export async function f(token:[REDACTED] fetch(u)}"
+ }
+ }
+ ]
+ }
+ ],
+ "part": 1,
+ "parts": 1
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "turn.diff scrubs each file's hunk with its own mode",
+ "kind": "trace",
+ "input": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:01.000Z",
+ "type": "turn.diff",
+ "data": {
+ "schema_version": 1,
+ "files": [
+ {
+ "path": "src/a.ts",
+ "status": "modified",
+ "diff": "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n+const k = \"sk-proj-AAAAAAAAAAAAAAAAAAAA\"\n",
+ "truncated": false,
+ "reason": null
+ },
+ {
+ "path": "conf/app.ini",
+ "status": "modified",
+ "diff": "--- a/conf/app.ini\n+++ b/conf/app.ini\n@@ -1 +1 @@\n-x\n+auth_token=abcdefgh123\n",
+ "truncated": false,
+ "reason": null
+ }
+ ],
+ "file_count": 2,
+ "omitted_file_count": 0,
+ "truncated": false
+ }
+ }
+ ]
+ },
+ "expected": {
+ "schema_version": 1,
+ "session_id": "ses_1",
+ "events": [
+ {
+ "at": "2026-10-04T00:00:01.000Z",
+ "type": "turn.diff",
+ "data": {
+ "schema_version": 1,
+ "files": [
+ {
+ "path": "src/a.ts",
+ "status": "modified",
+ "diff": "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1 @@\n-x\n+export async function f(token:string){await fetch(u)}\n+const k = \"[REDACTED]\"\n",
+ "truncated": false,
+ "reason": null
+ },
+ {
+ "path": "conf/app.ini",
+ "status": "modified",
+ "diff": "--- a/conf/app.ini\n+++ b/conf/app.ini\n@@ -1 +1 @@\n-x\n+auth_token=[REDACTED]\n",
+ "truncated": false,
+ "reason": null
+ }
+ ],
+ "file_count": 2,
+ "omitted_file_count": 0,
+ "truncated": false
+ }
+ }
+ ]
+ }
+ },
+ {
+ "name": "git diff: a .ts section keeps code, a config section is redacted",
+ "kind": "git_diff",
+ "input": "diff --git a/src/a.ts b/src/a.ts\nindex 1..2 100644\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-x\n+export async function f(token:string){await fetch(u)}\n+async(token:string)=>{}\ndiff --git a/conf/app.ini b/conf/app.ini\nindex 1..2 100644\n--- a/conf/app.ini\n+++ b/conf/app.ini\n@@ -1 +1 @@\n-x\n+auth_token=abcdefgh123\n+f(token:string){await fetch(u)}\n",
+ "expected": "diff --git a/src/a.ts b/src/a.ts\nindex 1..2 100644\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-x\n+export async function f(token:string){await fetch(u)}\n+async(token:string)=>{}\ndiff --git a/conf/app.ini b/conf/app.ini\nindex 1..2 100644\n--- a/conf/app.ini\n+++ b/conf/app.ini\n@@ -1 +1 @@\n-x\n+auth_token=[REDACTED]\n+f(token:[REDACTED] fetch(u)}\n"
+ }
+]
diff --git a/apps/server/src/session-uploader-file-mode.test.ts b/apps/server/src/session-uploader-file-mode.test.ts
new file mode 100644
index 00000000..7e8fdde2
--- /dev/null
+++ b/apps/server/src/session-uploader-file-mode.test.ts
@@ -0,0 +1,84 @@
+import { describe, expect, test } from "bun:test";
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+import { filterUploadDiff, redactDiffByFile, redactUploadJson } from "./session-uploader.js";
+
+// The scrubber's file-aware trace pass: code that file tools write, replace,
+// patch or read, and each file's hunk of a diff, is scrubbed with the mode its
+// file's path selects, as the file's snapshot is; everything else in the trace
+// stays CONFIG. The cases in __fixtures__/scrub-file-mode.json are shared byte
+// for byte with the CLI (test/fixtures/scrub-file-mode.json) and the backend
+// (tests/omnirush/fixtures/scrub_file_mode.json), so the three scrubbers give
+// the same output.
+type Case = { name: string; kind: "trace" | "git_diff"; input: unknown; expected: unknown };
+const cases = JSON.parse(readFileSync(join(import.meta.dir, "__fixtures__", "scrub-file-mode.json"), "utf8")) as Case[];
+
+const plain = (value: unknown): any => JSON.parse(JSON.stringify(value));
+const scrubTrace = (document: unknown): any => plain(redactUploadJson(document, { fileAware: true }).value);
+const TS1 = "export async function f(token:string){await fetch(u)}";
+const TS2 = "const g = async(token:string)=>{}\nfunction a(token:string){await b}";
+
+function toolEvent(tool: string, state: Record<string, unknown>) {
+ return { schema_version: 1, events: [{ type: "turn.messages", data: { messages: [{ info: { role: "assistant" }, parts: [{ type: "tool", tool, state }] }] } }] };
+}
+const stateOf = (document: any): any => document.events[0].data.messages[0].parts[0].state;
+
+describe("scrub parity fixture", () => {
+ for (const entry of cases) {
+ test(entry.name, () => {
+ const actual = entry.kind === "trace" ? scrubTrace(entry.input) : filterUploadDiff(entry.input as string).diff;
+ expect(actual).toEqual(entry.expected);
+ });
+ }
+});
+
+describe("file-aware trace scrub", () => {
+ test("a .ts edit/write keeps both code examples intact, real or escaped newlines", () => {
+ const edit = stateOf(scrubTrace(toolEvent("edit", { input: { filePath: "/w/src/a.ts", oldString: TS1, newString: TS2 } })));
+ expect(edit.input.oldString).toBe(TS1);
+ expect(edit.input.newString).toBe(TS2);
+ const escaped = JSON.stringify([{ oldText: "x", newText: TS2 }]);
+ expect(escaped).toContain("\\nfunction");
+ const patch = stateOf(scrubTrace(toolEvent("edit", { input: { path: "src/a.ts", edits: escaped } })));
+ expect(patch.input.edits).toBe(escaped);
+ const write = stateOf(scrubTrace(toolEvent("write", { input: { filePath: "src/b.tsx", content: `${TS1}\n${TS2}` } })));
+ expect(write.input.content).toBe(`${TS1}\n${TS2}`);
+ });
+
+ test("the same code in a bash command, a pathless write or other JSON stays CONFIG", () => {
+ const bash = stateOf(scrubTrace(toolEvent("bash", { input: { command: TS1 }, output: TS2 })));
+ expect(bash.input.command).toBe("export async function f(token:[REDACTED] fetch(u)}");
+ expect(bash.output).toBe("const g = async(token:[REDACTED]\nfunction a(token:[REDACTED] b}");
+ expect(stateOf(scrubTrace(toolEvent("write", { input: { content: TS1 } }))).input.content).toContain("[REDACTED]");
+ const legacy = plain(redactUploadJson(toolEvent("write", { input: { filePath: "a.ts", content: TS1 } })).value);
+ expect(stateOf(legacy).input.content).toContain("[REDACTED]");
+ });
+
+ test("real secrets are still removed from file tool content in every mode", () => {
+ expect(stateOf(scrubTrace(toolEvent("write", { input: { filePath: ".env", content: "API_TOKEN=abcdefgh123" } }))).input.content).toBe("API_TOKEN=[REDACTED]");
+ expect(stateOf(scrubTrace(toolEvent("write", { input: { filePath: "src/k.ts", content: 'const k = "sk-proj-AAAAAAAAAAAAAAAAAAAA"' } }))).input.content)
+ .toBe('const k = "[REDACTED]"');
+ for (const shape of [
+ "ghp_AAAAAAAAAAAAAAAAAAAAAAAA",
+ "AKIAABCDEFGHIJKLMNOP",
+ "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.abcdefghijklmnop",
+ "Authorization: Bearer abcDEF1234567890xyzQ",
+ "postgres://user:[email protected]/app",
+ ]) {
+ const state = stateOf(scrubTrace(toolEvent("edit", { input: { filePath: "src/x.py", oldString: "a", newString: shape } })));
+ expect(state.input.newString).not.toBe(shape);
+ expect(state.input.newString).toMatch(/\[REDACTED(?:_PII)?\]/);
+ }
+ });
+
+ test("a diff with a .ts hunk and a .env hunk scrubs each with its own mode", () => {
+ const diff = `Index: src/a.ts\n--- src/a.ts\n+++ src/a.ts\n@@ -1 +1 @@\n+${TS1}\nIndex: .env\n--- .env\n+++ .env\n@@ -0,0 +1 @@\n+API_TOKEN=abcdefgh123\n+${TS1}\n`;
+ expect(redactDiffByFile(diff, null)).toBe(
+ `Index: src/a.ts\n--- src/a.ts\n+++ src/a.ts\n@@ -1 +1 @@\n+${TS1}\nIndex: .env\n--- .env\n+++ .env\n@@ -0,0 +1 @@\n+API_TOKEN=[REDACTED]\n+export async function f(token:[REDACTED] fetch(u)}\n`,
+ );
+ expect(redactDiffByFile(`+${TS1}\n`, "a.ts")).toBe(`+${TS1}\n`);
+ expect(redactDiffByFile(`+${TS1}\n`, null)).toContain("[REDACTED]");
+ expect(filterUploadDiff("diff --git a/.env b/.env\n+API_TOKEN=abcdefgh123\n").diff).toBeNull();
+ });
+});
diff --git a/apps/server/src/session-uploader.test.ts b/apps/server/src/session-uploader.test.ts
index 35e6b156..38e9014f 100644
--- a/apps/server/src/session-uploader.test.ts
+++ b/apps/server/src/session-uploader.test.ts
@@ -2261,10 +2261,13 @@ describe("session uploader scrubber parity: second-sample refinements", () => {
expect(redactUploadText("LIVE_DSN = 'postgres://live:secret@host/db'\n", { context: "tests/fixtures.py", mode: "source" }).count).toBe(1);
expect(redactUploadContent("fix.patch", PATCH)).toBe(PATCH.replace('"hunter2abc"', '"[REDACTED]"'));
expect(redactUploadText(PATCH, { context: "fix.patch", mode: redactModeForPath("fix.patch") }).count).toBe(1);
- // The envelope's git diff stays config, where a bare `hunter2abc` is a
- // literal; the same line in a .patch file is a name.
+ // The envelope's git diff scrubs each file's section with that file's
+ // mode: in a .py section a bare `hunter2abc` is a name, as in the file
+ // itself; in a config file's section it is a literal.
expect(filterUploadDiff("diff --git a/app/settings.py b/app/settings.py\n+password = hunter2abc\n").diff)
- .toBe("diff --git a/app/settings.py b/app/settings.py\n+password = [REDACTED]\n");
+ .toBe("diff --git a/app/settings.py b/app/settings.py\n+password = hunter2abc\n");
+ expect(filterUploadDiff("diff --git a/app/settings.ini b/app/settings.ini\n+password = hunter2abc\n").diff)
+ .toBe("diff --git a/app/settings.ini b/app/settings.ini\n+password = [REDACTED]\n");
expect(redactUploadText("+password = hunter2abc", { mode: redactModeForPath("fix.patch") })).toEqual({ text: "+password = hunter2abc", count: 0 });
});
@@ -2419,7 +2422,7 @@ describe("session uploader scrubber parity: second-sample refinements", () => {
expect(redactUploadContent("a.json", '{"__proto__": {"password": "hunter2abc"}}')).toBe('{"__proto__":{"password":"[REDACTED]"}}');
});
- test("a .patch file is scrubbed as source while the envelope's git diff stays config", async () => {
+ test("a .patch file is scrubbed as source, and the envelope's git diff with each file's own mode", async () => {
const root = await mkdtemp(join(tmpdir(), "omnirush-upload-patch-"));
roots.push(root);
await git(root, "init", "-q");
@@ -2441,10 +2444,10 @@ describe("session uploader scrubber parity: second-sample refinements", () => {
expect(content("app/settings.py")).toBe("import config\npassword = hunter2abc\n");
expect(content("fix.patch")).toBe(PATCH.replace('"hunter2abc"', '"[REDACTED]"'));
expect(content("notes.diff")).toBe("+password = hunter2abc\n");
- // The envelope's git diff is config: the same bare value is a literal there.
+ // The envelope's git diff scrubs the .py section as source, as the file itself.
const gitBlock = start.workspace.git as { diff: string };
- expect(gitBlock.diff).toContain("+password = [REDACTED]");
- expect(gitBlock.diff).not.toContain("hunter2abc");
+ expect(gitBlock.diff).toContain("+password = hunter2abc");
+ expect(gitBlock.diff).not.toContain("[REDACTED]");
});
});
diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.ts
index 355c8f53..21594c92 100644
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@ -662,9 +662,11 @@ const EXCLUDED_LAST_SEGMENTS = new Set([
* extension names a programming language is scrubbed in SOURCE mode, where an
* assignment's value must look like a generated literal; everything else
* (.env*, ini, cfg, conf, yml, yaml, toml, json, properties, txt, md, no or
- * unknown extension), plus the envelope's git diff and the trace, is scrubbed
- * in CONFIG mode with the permissive rule. A `.patch` or `.diff` workspace
- * file carries code and is SOURCE.
+ * unknown extension), plus the trace, is scrubbed in CONFIG mode with the
+ * permissive rule. A `.patch` or `.diff` workspace file carries code and is
+ * SOURCE. The file text the trace and the envelope's git diff carry (file
+ * tool content, diffs, read output: see fileAwareRecord and filterUploadDiff)
+ * gets the mode of the file it belongs to.
*/
export type RedactMode = "source" | "config";
const SOURCE_EXTENSIONS = new Set([
@@ -1188,7 +1190,7 @@ function nextAssignmentKeyword(text: string, from: number): number {
export type RedactUploadTextOptions = {
/** Text that counts as context for context-dependent rules: the file path, or the JSON key a value sits under. */
context?: string;
- /** Value rule mode; CONFIG when omitted (git diffs, the trace, JSON). See redactModeForPath. */
+ /** Value rule mode; CONFIG when omitted (the trace, JSON). See redactModeForPath. */
mode?: RedactMode;
/** The enclosing JSON object or array names an AWS key: the AWS rule runs whatever the line count. */
awsContext?: boolean;
@@ -1255,8 +1257,17 @@ function membersNameAws(members: ReadonlyArray<readonly [string, unknown]>): boo
return members.some(([key, item]) => namesAws(key) || namesAws(item));
}
-function redactJsonValue(value: unknown, key: string | undefined, tally: JsonTally, depth = 0, awsContext = false, maxDepth = Infinity): unknown {
+function redactJsonValue(
+ value: unknown,
+ key: string | undefined,
+ tally: JsonTally,
+ depth = 0,
+ awsContext = false,
+ maxDepth = Infinity,
+ fileAware = false,
+): unknown {
if (depth > maxDepth) throw new JsonTooDeep();
+ if (value instanceof Prescrubbed) return value.text;
if (typeof value === "string") {
// A JSON string is always a quoted literal, scrubbed in CONFIG mode.
// A path-like key (`{"src/admin_auth.py": "<sha256>"}`) names a file, never a secret.
@@ -1276,7 +1287,7 @@ function redactJsonValue(value: unknown, key: string | undefined, tally: JsonTal
// environments) is redacted although `value` names nothing.
if (Array.isArray(value)) {
const context = awsContext || value.some(namesAws);
- return value.map((item) => redactJsonValue(item, key, tally, depth + 1, context, maxDepth));
+ return value.map((item) => redactJsonValue(item, key, tally, depth + 1, context, maxDepth, fileAware));
}
if (value instanceof JsonPairs) {
tally.members += value.pairs.length;
@@ -1284,12 +1295,13 @@ function redactJsonValue(value: unknown, key: string | undefined, tally: JsonTal
return new JsonPairs(value.pairs.map(([childKey, childValue]) => {
const scrubbedKey = redactUploadText(childKey, { awsContext: context });
tally.count += scrubbedKey.count;
- return [scrubbedKey.text, redactJsonValue(childValue, childKey, tally, depth + 1, context, maxDepth)];
+ return [scrubbedKey.text, redactJsonValue(childValue, childKey, tally, depth + 1, context, maxDepth, fileAware)];
}));
}
const serializable = value as { toJSON?: unknown };
- if (typeof serializable.toJSON === "function") return redactJsonValue((serializable.toJSON as () => unknown)(), key, tally, depth, awsContext, maxDepth);
- const entries = Object.entries(value as Record<string, unknown>);
+ if (typeof serializable.toJSON === "function") return redactJsonValue((serializable.toJSON as () => unknown)(), key, tally, depth, awsContext, maxDepth, fileAware);
+ const record = fileAware ? fileAwareRecord(value as Record<string, unknown>, tally) : value as Record<string, unknown>;
+ const entries = Object.entries(record);
tally.members += entries.length;
const context = awsContext || membersNameAws(entries);
// No prototype, so a `__proto__` key stays an ordinary member.
@@ -1297,7 +1309,7 @@ function redactJsonValue(value: unknown, key: string | undefined, tally: JsonTal
for (const [childKey, childValue] of entries) {
const scrubbedKey = redactUploadText(childKey, { awsContext: context });
tally.count += scrubbedKey.count;
- output[scrubbedKey.text] = redactJsonValue(childValue, childKey, tally, depth + 1, context, maxDepth);
+ output[scrubbedKey.text] = redactJsonValue(childValue, childKey, tally, depth + 1, context, maxDepth, fileAware);
}
return output;
}
@@ -1310,9 +1322,9 @@ function redactJsonValue(value: unknown, key: string | undefined, tally: JsonTal
* never produce a broken escape, which scrubbing serialised JSON as text could.
* The backend's _redact_json_value.
*/
-export function redactUploadJson(value: unknown): { value: unknown; count: number } {
+export function redactUploadJson(value: unknown, options: { fileAware?: boolean } = {}): { value: unknown; count: number } {
const tally: JsonTally = { count: 0, members: 0 };
- return { value: redactJsonValue(value, undefined, tally), count: tally.count };
+ return { value: redactJsonValue(value, undefined, tally, 0, false, Infinity, options.fileAware === true), count: tally.count };
}
/**
@@ -1474,6 +1486,231 @@ function redactUploadContentCounted(path: string, text: string): { text: string;
return redactUploadText(text, { context: path, mode: redactModeForPath(path) });
}
+// --- file-aware trace scrub --------------------------------------------------
+// The trace is scrubbed in CONFIG mode, except for the strings that carry a
+// workspace file's text: the content a file tool writes or replaces, the
+// diffs and patches it reports, a read tool's output and a "turn.diff"
+// event's hunks. Those are scrubbed with the mode the target file's path
+// selects (redactModeForPath), exactly as a snapshot of that file is, so
+// `f(token:string){...}` in a .ts file survives a write tool call the way it
+// survives the file upload. A diff or patch naming several files is scrubbed
+// file by file. Everything else (shell commands and output, chat text, other
+// tools' input and output, a file tool call with no path) stays CONFIG. The
+// backend's _file_aware_* helpers apply the identical rule; a change here
+// must land there too.
+
+/** A string the file-aware pass already scrubbed: the JSON walker keeps it as it is. */
+class Prescrubbed {
+ constructor(readonly text: string) {}
+}
+
+const FILE_WRITE_TOOLS = new Set(["write", "edit", "multiedit"]);
+const FILE_PATCH_TOOLS = new Set(["patch", "apply_patch"]);
+const FILE_READ_TOOLS = new Set(["read"]);
+/** A file tool's target path, in the order looked for (opencode, pi, other engines). */
+const FILE_TOOL_PATH_KEYS = ["filePath", "path", "file_path"];
+/** A file tool input's whole-file text: scrubbed as the file's snapshot is. */
+const FILE_TOOL_CONTENT_KEYS = ["content"];
+/** A file tool input's replaced and replacing text. */
+const FILE_TOOL_FRAGMENT_KEYS = ["oldString", "newString", "oldText", "newText", "old_string", "new_string"];
+/** A patch tool input's patch text. */
+const FILE_TOOL_PATCH_KEYS = ["patchText", "patch", "input"];
+/** A file entry's path in tool metadata (apply_patch `files`, edit `filediff`). */
+const FILE_ENTRY_PATH_KEYS = ["filePath", "file", "relativePath", "path"];
+// A diff or patch starts a new file at a git header, a jsdiff `Index:` line
+// (opencode's edit and apply_patch metadata) or an apply_patch file marker.
+const FILE_SEGMENT_START = /(?:^|(?<=\n))(?=diff --git |Index: |\*\*\* (?:Add|Update|Delete) File: )/;
+const PATCH_FILE_HEADER = /^\*\*\* (?:Add|Update|Delete) File: ([\s\S]*)$/;
+const MAX_TOOL_PATH_CHARS = 4096;
+
+function fileToolPath(value: unknown): string | null {
+ if (typeof value !== "string" || !value || value.length > MAX_TOOL_PATH_CHARS || value.includes("\n") || value.includes("\r")) return null;
+ return value;
+}
+
+function firstFilePath(records: readonly Record<string, unknown>[], keys: readonly string[]): string | null {
+ for (const record of records) {
+ for (const key of keys) {
+ const path = fileToolPath(record[key]);
+ if (path !== null) return path;
+ }
+ }
+ return null;
+}
+
+function trimBlanks(text: string): string {
+ return text.replace(/^[ \t]+|[ \t]+$/g, "");
+}
+
+/** The path a diff segment names in its first line, or `fallback` for a segment with no file header. */
+function diffSegmentPath(segment: string, fallback: string | null): string | null {
+ const newline = segment.indexOf("\n");
+ let first = newline === -1 ? segment : segment.slice(0, newline);
+ if (first.endsWith("\r")) first = first.slice(0, -1);
+ if (first.startsWith("diff --git ")) return diffHeaderPath(first);
+ if (first.startsWith("Index: ")) return fileToolPath(trimBlanks(first.slice("Index: ".length)));
+ const patch = PATCH_FILE_HEADER.exec(first);
+ if (patch) return fileToolPath(trimBlanks(patch[1] ?? ""));
+ return fallback;
+}
+
+/** Scrubs `text` as a file's text: the mode the path selects, CONFIG with no path. */
+function redactFileText(text: string, path: string | null, tally: { count: number }): string {
+ const result = path === null ? redactUploadText(text) : redactUploadText(text, { context: path, mode: redactModeForPath(path) });
+ tally.count += result.count;
+ return result.text;
+}
+
+/** Scrubs `text` as a whole file is uploaded: a `.json` file structurally, anything else as file text. */
+function redactFileContent(text: string, path: string | null, tally: { count: number }): string {
+ if (path !== null && extname(path).toLowerCase() === ".json") {
+ const json = redactUploadJsonText(text, path);
+ if (json !== null) {
+ if (json !== text) tally.count += 1;
+ return json;
+ }
+ }
+ return redactFileText(text, path, tally);
+}
+
+/**
+ * Scrubs a unified diff or patch file by file: each segment that starts with
+ * a file header (`diff --git`, `Index:`, `*** Update File:`) under the mode
+ * its own path selects, a segment with no header (a single-file diff, a
+ * patch's preamble) under the mode of `fallback`. The backend's
+ * redact_diff_by_file.
+ */
+export function redactDiffByFile(text: string, fallback: string | null, tally: { count: number } = { count: 0 }): string {
+ let output = "";
+ for (const segment of text.split(FILE_SEGMENT_START)) {
+ if (segment) output += redactFileText(segment, diffSegmentPath(segment, fallback), tally);
+ }
+ return output;
+}
+
+function prescrub(record: Record<string, unknown>, key: string, scrub: (text: string) => string): void {
+ const value = record[key];
+ if (typeof value === "string") record[key] = new Prescrubbed(scrub(value));
+}
+
+/** A tool input with its file text prescrubbed (a copy; the original is left alone). */
+function fileAwareToolInput(input: Record<string, unknown>, name: string, path: string | null, tally: { count: number }): Record<string, unknown> {
+ const copy: Record<string, unknown> = { ...input };
+ if (FILE_PATCH_TOOLS.has(name)) {
+ for (const key of FILE_TOOL_PATCH_KEYS) prescrub(copy, key, (text) => redactDiffByFile(text, null, tally));
+ return copy;
+ }
+ if (path === null) return copy;
+ for (const key of FILE_TOOL_CONTENT_KEYS) prescrub(copy, key, (text) => redactFileContent(text, path, tally));
+ for (const key of FILE_TOOL_FRAGMENT_KEYS) prescrub(copy, key, (text) => redactFileText(text, path, tally));
+ const edits = copy.edits;
+ // Some models send `edits` as a JSON string: it is scrubbed as it stands,
+ // escapes and all (SOURCE mode never reads past a `\n` escape).
+ if (typeof edits === "string") copy.edits = new Prescrubbed(redactFileText(edits, path, tally));
+ else if (Array.isArray(edits)) {
+ copy.edits = edits.map((edit) => {
+ if (!isRecord(edit)) return edit;
+ const editPath = firstFilePath([edit], FILE_TOOL_PATH_KEYS) ?? path;
+ const item: Record<string, unknown> = { ...edit };
+ for (const key of FILE_TOOL_FRAGMENT_KEYS) prescrub(item, key, (text) => redactFileText(text, editPath, tally));
+ return item;
+ });
+ }
+ return copy;
+}
+
+/** A file entry of tool metadata (`filediff`, an apply_patch `files` item) with its texts prescrubbed. */
+function fileAwareFileEntry(entry: Record<string, unknown>, path: string | null, tally: { count: number }): Record<string, unknown> {
+ const entryPath = firstFilePath([entry], FILE_ENTRY_PATH_KEYS) ?? path;
+ const copy: Record<string, unknown> = { ...entry };
+ for (const key of ["patch", "diff"]) prescrub(copy, key, (text) => redactDiffByFile(text, entryPath, tally));
+ if (entryPath !== null) {
+ for (const key of ["before", "after"]) prescrub(copy, key, (text) => redactFileContent(text, entryPath, tally));
+ }
+ return copy;
+}
+
+/** File tool metadata (opencode's, pi's details, the 2.x engine's under `v2`) with its diffs prescrubbed. */
+function fileAwareToolMetadata(metadata: Record<string, unknown>, path: string | null, tally: { count: number }, depth = 0): Record<string, unknown> {
+ const copy: Record<string, unknown> = { ...metadata };
+ for (const key of ["diff", "patch"]) prescrub(copy, key, (text) => redactDiffByFile(text, path, tally));
+ if (isRecord(copy.filediff)) copy.filediff = fileAwareFileEntry(copy.filediff, path, tally);
+ if (Array.isArray(copy.files)) copy.files = copy.files.map((entry) => (isRecord(entry) ? fileAwareFileEntry(entry, path, tally) : entry));
+ if (depth === 0) {
+ // multiedit: one edit's metadata per edit.
+ if (Array.isArray(copy.results)) copy.results = copy.results.map((entry) => (isRecord(entry) ? fileAwareToolMetadata(entry, path, tally, 1) : entry));
+ if (isRecord(copy.v2)) copy.v2 = fileAwareToolMetadata(copy.v2, path, tally, 1);
+ }
+ return copy;
+}
+
+/** A read tool's state with the file text it returned prescrubbed. */
+function fileAwareReadState(state: Record<string, unknown>, path: string, tally: { count: number }): void {
+ prescrub(state, "output", (text) => redactFileText(text, path, tally));
+ if (Array.isArray(state.content)) {
+ state.content = state.content.map((item) => {
+ if (!isRecord(item) || item.type !== "text") return item;
+ const copy: Record<string, unknown> = { ...item };
+ prescrub(copy, "text", (text) => redactFileText(text, path, tally));
+ return copy;
+ });
+ }
+ if (isRecord(state.metadata)) {
+ const metadata: Record<string, unknown> = { ...state.metadata };
+ prescrub(metadata, "preview", (text) => redactFileText(text, path, tally));
+ if (isRecord(metadata.display)) {
+ const display: Record<string, unknown> = { ...metadata.display };
+ prescrub(display, "text", (text) => redactFileText(text, path, tally));
+ metadata.display = display;
+ }
+ state.metadata = metadata;
+ }
+}
+
+/** The lower-cased tool name of an engine tool part (`tool`, else the 2.x `name`), or null. */
+function fileToolName(part: Record<string, unknown>): string | null {
+ const raw = typeof part.tool === "string" ? part.tool : typeof part.name === "string" ? part.name : null;
+ if (raw === null) return null;
+ const name = raw.toLowerCase().split(".").at(-1) ?? "";
+ return FILE_WRITE_TOOLS.has(name) || FILE_PATCH_TOOLS.has(name) || FILE_READ_TOOLS.has(name) ? name : null;
+}
+
+/**
+ * The record with the strings that carry file text prescrubbed under their
+ * file's mode, or the record itself when it is neither a file tool part
+ * (`{type: "tool", tool|name, state}`) nor a "turn.diff" event. The
+ * backend's _file_aware_record.
+ */
+function fileAwareRecord(record: Record<string, unknown>, tally: { count: number }): Record<string, unknown> {
+ if (record.type === "turn.diff" && isRecord(record.data) && Array.isArray(record.data.files)) {
+ const files = record.data.files.map((entry) => {
+ if (!isRecord(entry)) return entry;
+ const copy: Record<string, unknown> = { ...entry };
+ prescrub(copy, "diff", (text) => redactDiffByFile(text, fileToolPath(entry.path), tally));
+ return copy;
+ });
+ return { ...record, data: { ...record.data, files } };
+ }
+ if (record.type !== "tool" || !isRecord(record.state)) return record;
+ const name = fileToolName(record);
+ if (name === null) return record;
+ const state: Record<string, unknown> = { ...record.state };
+ const v2 = isRecord(state.v2) ? { ...state.v2 } : null;
+ const inputs = [state.input, v2?.input].filter(isRecord);
+ const path = firstFilePath(inputs, FILE_TOOL_PATH_KEYS);
+ if (FILE_READ_TOOLS.has(name)) {
+ if (path !== null) fileAwareReadState(state, path, tally);
+ return { ...record, state };
+ }
+ if (isRecord(state.input)) state.input = fileAwareToolInput(state.input, name, path, tally);
+ if (isRecord(state.metadata)) state.metadata = fileAwareToolMetadata(state.metadata, path, tally);
+ if (v2 !== null) {
+ if (isRecord(v2.input)) v2.input = fileAwareToolInput(v2.input, name, path, tally);
+ state.v2 = v2;
+ }
+ return { ...record, state };
+}
+
/**
* Removes the userinfo (`user:password@`) from a git remote URL. Both
* scheme URLs (`https://token@host/...`) and scp-like remotes
@@ -1924,7 +2161,7 @@ function boundedTraceBatches(
session_segment: state.segment,
session_resumed: state.resumed,
events,
- }).value as Record<string, unknown> & { events: TraceEvent[] };
+ }, { fileAware: true }).value as Record<string, unknown> & { events: TraceEvent[] };
const encode = (selected: unknown[], truncated: boolean, dropped: number): BoundedTraceBatch & { byteLength: number } => {
const content = JSON.stringify({
...header,
@@ -2240,7 +2477,8 @@ export function filterUploadDiff(raw: string, inputTruncated = false): { diff: s
if (!path || isUploadPathDenied(path)) continue;
if (/^(?:Binary files .* differ|GIT binary patch)/m.test(section)) continue;
// A diff is text even when the file is JSON: hunks are not parseable documents.
- const redacted = redactUploadText(section, { context: path }).text;
+ // Each file's section under the mode its path selects, as the file's snapshot.
+ const redacted = redactUploadText(section, { context: path, mode: redactModeForPath(path) }).text;
const size = Buffer.byteLength(redacted);
if (used + size > MAX_UPLOAD_DIFF_BYTES) {
truncated = true;