Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(browser-extension): capture gzip Claude prompts and stop ChatGPT answer duplication

shukieshaha1e3f7bfcd1f
diff --git a/browser-extension/README.md b/browser-extension/README.md
index ccfc8bdf..ad437a8f 100644
--- a/browser-extension/README.md
+++ b/browser-extension/README.md
@@ -346,13 +346,19 @@ dashboard), including interleaved sessions with no cross-contamination.
> **Fixtures.** The committed `*.sse` files are **real, sanitized** captures (via
> `npm run record:fixtures`): `fixtures/claude/simple-turn.sse` (claude.ai) and
-> `fixtures/chatgpt/simple-turn.sse` (ChatGPT). `claude/with-tool-call.sse` is synthetic (documented
-> Anthropic tool-use shape) pending a real tool-call capture. Raw recorder output (`*.page.html`,
+> `fixtures/chatgpt/simple-turn.sse` (ChatGPT). `claude/web-search.sse` and
+> `chatgpt/web-search-split.sse` are sanitized and shortened from October 2026 captures and keep
+> their event and op order. `claude/with-tool-call.sse` is synthetic (documented Anthropic
+> tool-use shape). Raw recorder output (`*.page.html`,
> `*.meta.json`, `real-*.sse`, `diag-*.sse`) is git-ignored — it can contain personal chat/account
> data, so only hand-sanitized `*.sse` fixtures are committed.
>
-> Real-format notes: **claude.ai** emits a leading `conversation_ready` event and nests the model in
-> `message_start.message.model` (typed Anthropic events). **ChatGPT** posts to
-> `/backend-api/f/conversation` and streams OpenAI `delta_encoding: v1` JSON-patch ops (add/append/patch
-> with bare-value path inheritance), model in `metadata.model_slug`. Neither site streams token usage,
-> so `gen_ai.usage.*` is absent for both.
+> Real-format notes: **claude.ai** sends the completion request body gzip-compressed (a `Uint8Array`
+> with `Content-Encoding: gzip`), which the interceptor decompresses (`src/interceptor/request-body.ts`).
+> Its stream nests the model in `message_start.message.model` (typed Anthropic events) and adds
+> `tool_result`, `thinking` and citation blocks plus `ping` and `message_limit` events. **ChatGPT**
+> posts to `/backend-api/f/conversation` and streams OpenAI `delta_encoding: v1` JSON-patch ops
+> (add/append/replace/remove/patch with bare-value path inheritance). Each op's `c` names the message
+> it applies to, and ops for different messages interleave; an answer interrupted by a search
+> streams per-segment messages plus a merged one (`metadata.is_merged_message`). Model in
+> `metadata.model_slug`. Neither site streams token usage, so `gen_ai.usage.*` is absent for both.
diff --git a/browser-extension/e2e/chatgpt.replay.spec.ts b/browser-extension/e2e/chatgpt.replay.spec.ts
index 6a61cd61..8712d1a4 100644
--- a/browser-extension/e2e/chatgpt.replay.spec.ts
+++ b/browser-extension/e2e/chatgpt.replay.spec.ts
@@ -62,3 +62,47 @@ test('captures a simple ChatGPT turn end-to-end into normalized OTLP', async ({
{ role: 'assistant', parts: [{ type: 'text', content: 'Good evening! Hello to you as well. Hope your day is going well.' }] },
]);
});
+
+test('captures an answer split around a web search once, with the search as a tool call', async ({
+ context,
+ serviceWorker,
+ replay,
+ mockCollector,
+}) => {
+ const SPLIT_CONV = '6adb0000-0000-8000-8000-000000000001';
+ replay.setCase({
+ site: 'chatgpt',
+ name: 'web-search-split',
+ conversationId: SPLIT_CONV,
+ prompt: 'Boston Marathon',
+ completionPath: COMPLETION_PATH,
+ });
+ await seedSettings(serviceWorker, {
+ enabled: true,
+ retention: 'full',
+ endpoint: mockCollector.url,
+ sites: { claude_web: true, chatgpt_web: true },
+ });
+
+ const page = await context.newPage();
+ await page.goto('https://chatgpt.com/');
+ await expect(page.locator('[data-testid="assistant-message"]')).toHaveAttribute(
+ 'data-complete',
+ '1',
+ );
+ await expect
+ .poll(() => recordByAction(mockCollector.received, 'tool.invoked'), { timeout: 10_000 })
+ .toBeTruthy();
+
+ const resp = flatAttrs(recordByAction(mockCollector.received, 'agent.response.completed')!.attributes);
+ expect(JSON.parse(String(resp['gen_ai.output.messages']))).toEqual([
+ {
+ role: 'assistant',
+ parts: [{ type: 'text', content: "The Boston Marathon is the world's oldest annual marathon.\n\n**Next race:** Monday, April 19, 2027." }],
+ },
+ ]);
+ const tool = flatAttrs(recordByAction(mockCollector.received, 'tool.invoked')!.attributes);
+ expect(tool['gen_ai.tool.call.name']).toBe('web_search');
+ expect(JSON.parse(String(tool['gen_ai.tool.call.arguments']))).toEqual({ queries: ['2027 Boston Marathon date'] });
+ expect(JSON.parse(String(tool['gen_ai.tool.call.result'])).search_result_groups).toHaveLength(1);
+});
diff --git a/browser-extension/e2e/claude.replay.spec.ts b/browser-extension/e2e/claude.replay.spec.ts
index d95b8e00..cfeea7d9 100644
--- a/browser-extension/e2e/claude.replay.spec.ts
+++ b/browser-extension/e2e/claude.replay.spec.ts
@@ -123,6 +123,65 @@ test('captures the prompt when the page uses fetch(new Request(...))', async ({
expect(p['beacon.prompt.text']).toBe('What is the capital of France?');
});
+// claude.ai as of October 2026: the completion body is a gzip-compressed
+// Uint8Array, and web_search runs server side with its results streamed back
+// as tool_result blocks.
+test('captures a gzip-compressed prompt and server-side tool results', async ({
+ context,
+ serviceWorker,
+ replay,
+ mockCollector,
+}) => {
+ replay.setCase({
+ site: 'claude',
+ name: 'web-search',
+ conversationId: CONV,
+ prompt: 'Search the web for the latest NASA news.',
+ completionPath: COMPLETION_PATH,
+ requestStyle: 'gzip',
+ });
+ await seedSettings(serviceWorker, {
+ enabled: true,
+ retention: 'full',
+ endpoint: mockCollector.url,
+ sites: { claude_web: true, chatgpt_web: true },
+ });
+
+ const page = await context.newPage();
+ await page.goto('https://claude.ai/chat');
+ await expect(page.locator('[data-testid="assistant-message"]')).toHaveAttribute(
+ 'data-complete',
+ '1',
+ );
+
+ await expect
+ .poll(() => allRecords(mockCollector.received).filter((r) => flatAttrs(r.attributes)['beacon.event.action'] === 'tool.invoked').length, { timeout: 10_000 })
+ .toBe(2);
+ const p = flatAttrs(recordByAction(mockCollector.received, 'prompt.submitted')!.attributes);
+ expect(p['beacon.prompt.text']).toBe('Search the web for the latest NASA news.');
+
+ const tools = allRecords(mockCollector.received)
+ .map((r) => flatAttrs(r.attributes))
+ .filter((a) => a['beacon.event.action'] === 'tool.invoked');
+ expect(tools.map((a) => JSON.parse(String(a['gen_ai.tool.call.arguments'])).query)).toEqual([
+ 'NASA news today',
+ 'NASA October 2026',
+ ]);
+ const results = tools.map((a) => JSON.parse(String(a['gen_ai.tool.call.result'])) as { url: string }[]);
+ expect(results.map((r) => r.map((x) => x.url))).toEqual([
+ ['https://news.example.com/nasa-today', 'https://space.example.org/updates'],
+ ['https://space.example.org/artemis'],
+ ]);
+
+ const resp = flatAttrs(recordByAction(mockCollector.received, 'agent.response.completed')!.attributes);
+ expect(JSON.parse(String(resp['gen_ai.output.messages']))).toEqual([
+ {
+ role: 'assistant',
+ parts: [{ type: 'text', content: 'One recent NASA headline: a new country signed the Artemis Accords.' }],
+ },
+ ]);
+});
+
test('respects the disabled toggle (no delivery)', async ({
context,
serviceWorker,
diff --git a/browser-extension/e2e/helpers/sse-replay-server.ts b/browser-extension/e2e/helpers/sse-replay-server.ts
index d25214c9..2f3aea98 100644
--- a/browser-extension/e2e/helpers/sse-replay-server.ts
+++ b/browser-extension/e2e/helpers/sse-replay-server.ts
@@ -32,8 +32,10 @@ export interface ReplayCase {
completionPath: string;
/** optional CSP header to prove injection survives it */
csp?: string;
- /** how the page issues the completion fetch: 'init' (default) or a Request object */
- requestStyle?: 'init' | 'request';
+ /** how the page issues the completion fetch: 'init' (default), a Request object, or
+ * 'gzip' (init.body a gzip-compressed Uint8Array with Content-Encoding: gzip, as
+ * claude.ai sends it) */
+ requestStyle?: 'init' | 'request' | 'gzip';
}
export interface ReplayServer {
@@ -157,6 +159,14 @@ function pageHtml(c: ReplayCase): string {
headers: { 'content-type': 'application/json' },
body: ${JSON.stringify(bodyStr)},
};
+ ${
+ c.requestStyle === 'gzip'
+ ? `init.body = new Uint8Array(await new Response(
+ new Blob([init.body]).stream().pipeThrough(new CompressionStream('gzip')),
+ ).arrayBuffer());
+ init.headers['content-encoding'] = 'gzip';`
+ : ''
+ }
const res = await ${
c.requestStyle === 'request' ? 'fetch(new Request(url, init))' : 'fetch(url, init)'
};
diff --git a/browser-extension/fixtures/chatgpt/web-search-split.sse b/browser-extension/fixtures/chatgpt/web-search-split.sse
new file mode 100644
index 00000000..64b9228e
--- /dev/null
+++ b/browser-extension/fixtures/chatgpt/web-search-split.sse
@@ -0,0 +1,92 @@
+event: delta_encoding
+data: "v1"
+
+data: {"type":"resume_conversation_token","kind":"topic","token":"REDACTED","conversation_id":"6adb0000-0000-8000-8000-000000000001"}
+
+event: delta
+data: {"p":"","o":"add","v":{"message":{"id":"seed-system","author":{"role":"system","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":0}
+
+event: delta
+data: {"v":{"message":{"id":"user-0001","author":{"role":"user","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":["First, without searching, say what the Boston Marathon is. Then search for the date of the next one."]},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":1}
+
+data: {"type":"input_message","conversation_id":"6adb0000-0000-8000-8000-000000000001"}
+
+event: delta
+data: {"v":{"message":{"id":"context-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"model_editable_context","model_set_context":""},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":2}
+
+event: delta
+data: {"v":{"message":{"id":"seg-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"in_progress","end_turn":null,"weight":1.0,"metadata":{"model_slug":"gpt-6","merged_message_id":"merged-0001"},"recipient":"all","channel":"final"},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":3}
+
+event: delta
+data: {"v":{"message":{"id":"merged-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"in_progress","end_turn":null,"weight":1.0,"metadata":{"model_slug":"gpt-6","is_merged_message":true,"source_message_ids":["seg-0001"]},"recipient":"all","channel":"final"},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":4}
+
+event: delta
+data: {"p":"/message/content/parts/0","o":"append","v":"The Boston Marathon is ","c":3}
+
+data: {"type":"title_generation","title":"Boston Marathon","conversation_id":"6adb0000-0000-8000-8000-000000000001"}
+
+data: {"type":"message_marker","conversation_id":"6adb0000-0000-8000-8000-000000000001","message_id":"merged-0001","marker":"user_visible_token","event":"first"}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/update_time","o":"replace","v":1792775744.8},{"p":"/message/content/parts/0","o":"append","v":"The Boston Marathon is "}],"c":4}
+
+event: delta
+data: {"p":"/message/content/parts/0","o":"append","v":"the world's oldest annual ","c":3}
+
+event: delta
+data: {"v":"marathon.\n\n","c":3}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/content/parts/0","o":"append","v":"the world's oldest annual "}],"c":4}
+
+event: delta
+data: {"v":[{"p":"/message/content/parts/0","o":"append","v":"marathon.\n\n"},{"p":"/message/metadata","o":"append","v":{"citations":[]}}],"c":4}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/status","o":"replace","v":"finished_successfully"},{"p":"/message/end_turn","o":"replace","v":false},{"p":"/message/metadata","o":"append","v":{"finish_details":{"type":"interrupted"}}}],"c":3}
+
+event: delta
+data: {"p":"","o":"add","v":{"message":{"id":"call-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{"reasoning_title":"Searching the web"},"recipient":"web.run","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":5}
+
+event: delta
+data: {"v":{"message":{"id":"tool-query-0001","author":{"role":"tool","name":"web.run","metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{"search_model_queries":{"type":"search_model_queries","queries":["2027 Boston Marathon date"]}},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":6}
+
+event: delta
+data: {"v":{"message":{"id":"tool-result-0001","author":{"role":"tool","name":"web.run","metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{"search_result_groups":[{"type":"search_result_group","domain":"www.example.org","entries":[{"type":"search_result","url":"https://www.example.org/boston-2027","title":"2027 Boston Marathon","snippet":"","ref_id":{"turn_index":1,"ref_type":"search","ref_index":0},"attribution":"example.org"}]}]},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":7}
+
+event: delta
+data: {"v":{"message":{"id":"seg-0002","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"text","parts":[""]},"status":"in_progress","end_turn":null,"weight":1.0,"metadata":{"model_slug":"gpt-6","merged_message_id":"merged-0001"},"recipient":"all","channel":"final"},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":8}
+
+data: {"type":"streaming_parent_patch","conversation_id":"6adb0000-0000-8000-8000-000000000001","logical_answer_id":"merged-0001","revision":1,"updates":[{"message_id":"call-0001","streaming_parent_id":"seg-0001"}]}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/metadata/source_message_ids","o":"append","v":["seg-0002"]},{"p":"/message/metadata/continuation_message_id","o":"replace","v":"seg-0002"}],"c":4}
+
+event: delta
+data: {"p":"/message/content/parts/0","o":"append","v":"**Next race:** Monday, ","c":8}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/content/parts/0","o":"append","v":"**Next race:** Monday, "}],"c":4}
+
+event: delta
+data: {"p":"/message/content/parts/0","o":"append","v":"April 19, 2027.","c":8}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/content/parts/0","o":"append","v":"April 19, 2027."},{"p":"/message/metadata","o":"append","v":{"search_result_groups":[{"type":"search_result_group","domain":"www.example.org","entries":[{"type":"search_result","url":"https://www.example.org/boston-2027","title":"2027 Boston Marathon","snippet":"","ref_id":{"turn_index":1,"ref_type":"search","ref_index":0},"attribution":"example.org"}]}]}}],"c":4}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/metadata/finish_details","o":"remove"},{"p":"/message/status","o":"replace","v":"finished_successfully"},{"p":"/message/end_turn","o":"replace","v":true}],"c":8}
+
+event: delta
+data: {"p":"","o":"add","v":{"message":{"id":"thoughts-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"thoughts","thoughts":[{"summary":"Checking the date","content":"The 2027 race falls on Patriots' Day."}],"source_analysis_msg_id":"REDACTED"},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":9}
+
+event: delta
+data: {"v":{"message":{"id":"recap-0001","author":{"role":"assistant","name":null,"metadata":{}},"create_time":1792775744.0,"update_time":null,"content":{"content_type":"reasoning_recap","content":"Thought for 2s"},"status":"finished_successfully","end_turn":null,"weight":1.0,"metadata":{},"recipient":"all","channel":null},"conversation_id":"6adb0000-0000-8000-8000-000000000001","error":null},"c":10}
+
+event: delta
+data: {"p":"","o":"patch","v":[{"p":"/message/status","o":"replace","v":"finished_successfully"},{"p":"/message/end_turn","o":"replace","v":true}],"c":4}
+
+data: {"type":"message_stream_complete","conversation_id":"6adb0000-0000-8000-8000-000000000001"}
+
+data: [DONE]
+
diff --git a/browser-extension/fixtures/claude/web-search.sse b/browser-extension/fixtures/claude/web-search.sse
new file mode 100644
index 00000000..311b0574
--- /dev/null
+++ b/browser-extension/fixtures/claude/web-search.sse
@@ -0,0 +1,81 @@
+event: message_start
+data: {"type":"message_start","message":{"id":"chatcompl_REDACTED","type":"message","role":"assistant","model":"claude-opus-5-5","parent_uuid":"00000000-0000-7000-8000-000000000001","uuid":"00000000-0000-7000-8000-000000000002","content":[],"stop_reason":null,"stop_sequence":null,"stop_details":null,"trace_id":"REDACTED","request_id":"req_REDACTED"},"discarded_parent_message_uuid":null}
+
+event: ping
+data: {"type":"ping" }
+
+event: content_block_start
+data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_search_1","name":"web_search","input":{},"message":"Searching the web","integration_name":null,"icon_name":"globe","start_timestamp":"2026-10-23T16:49:11.284862563Z","tool_origin":"first_party"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"mode\": \"standard\", "}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"\"query\": \"NASA news today\"}"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":0,"stop_timestamp":"2026-10-23T16:49:12.100000000Z"}
+
+event: content_block_start
+data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_result","tool_use_id":"toolu_search_1","name":"web_search","content":[{"type":"knowledge","title":"NASA News Today","url":"https://news.example.com/nasa-today","metadata":{"type":"webpage_metadata","site_domain":"news.example.com","favicon_url":"https://news.example.com/favicon.ico","site_name":"Example News"},"is_missing":false},{"type":"knowledge","title":"Latest NASA Updates","url":"https://space.example.org/updates","metadata":{"type":"webpage_metadata","site_domain":"space.example.org","favicon_url":"https://space.example.org/favicon.ico","site_name":"Example Space"},"is_missing":false}],"is_error":false,"icon_name":"globe","start_timestamp":"2026-10-23T16:49:12.690740565Z","tool_origin":"first_party"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":1,"stop_timestamp":"2026-10-23T16:49:12.700000000Z"}
+
+event: content_block_start
+data: {"type":"content_block_start","index":2,"content_block":{"type":"thinking","thinking":"","summaries":[],"cut_off":false,"truncated":false,"start_timestamp":"2026-10-23T16:49:13.240249935Z"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":2,"stop_timestamp":"2026-10-23T16:49:13.500000000Z"}
+
+event: content_block_start
+data: {"type":"content_block_start","index":3,"content_block":{"type":"tool_use","id":"toolu_search_2","name":"web_search","input":{},"message":"Searching the web","integration_name":null,"icon_name":"globe","start_timestamp":"2026-10-23T16:49:13.600000000Z","tool_origin":"first_party"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":3,"delta":{"type":"input_json_delta","partial_json":"{\"mode\": \"standard\", \"query\": \"NASA October 2026\"}"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":3,"stop_timestamp":"2026-10-23T16:49:14.400000000Z"}
+
+event: content_block_start
+data: {"type":"content_block_start","index":4,"content_block":{"type":"tool_result","tool_use_id":"toolu_search_2","name":"web_search","content":[{"type":"knowledge","title":"Artemis Accords Signatories","url":"https://space.example.org/artemis","metadata":{"type":"webpage_metadata","site_domain":"space.example.org","favicon_url":"https://space.example.org/favicon.ico","site_name":"Example Space"},"is_missing":false}],"is_error":false,"icon_name":"globe","start_timestamp":"2026-10-23T16:49:14.549390158Z","tool_origin":"first_party"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":4,"stop_timestamp":"2026-10-23T16:49:14.600000000Z"}
+
+event: content_block_start
+data: {"type":"content_block_start","index":5,"content_block":{"type":"thinking","thinking":"","summaries":[],"cut_off":false,"truncated":false,"start_timestamp":"2026-10-23T16:49:15.080121582Z"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":5,"delta":{"type":"thinking_summary_delta","summary":{"summary":"Finding the latest dated news item in the archive."}}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":5,"stop_timestamp":"2026-10-23T16:49:16.200000000Z","summary_pending":false}
+
+event: content_block_start
+data: {"type":"content_block_start","index":6,"content_block":{"type":"text","text":"","citations":[],"start_timestamp":"2026-10-23T16:49:16.246654873Z"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"One recent NASA headline: "}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":6,"delta":{"type":"citation_start_delta","citation":{"uuid":"00000000-0000-4000-8000-000000000003","title":"Artemis Accords Signatories","url":"https://space.example.org/artemis","metadata":{"type":"webpage_metadata","site_domain":"space.example.org","favicon_url":"https://space.example.org/favicon.ico","site_name":"Example Space"},"origin_tool_name":"web_search","sources":[{"uuid":"00000000-0000-4000-8000-000000000004","title":"Artemis Accords Signatories","url":"https://space.example.org/artemis","icon_url":"https://space.example.org/favicon.ico","source":"Example Space"}]}}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"a new country signed the Artemis Accords."}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":6,"delta":{"type":"citation_end_delta","citation_uuid":"00000000-0000-4000-8000-000000000003"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":6,"stop_timestamp":"2026-10-23T16:49:17.000000000Z"}
+
+event: message_delta
+data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}
+
+event: message_limit
+data: {"type":"message_limit","message_limit":{"type":"within_limit","resolved":true}}
+
+event: message_stop
+data: {"type":"message_stop"}
+
diff --git a/browser-extension/src/adapters/chatgpt.ts b/browser-extension/src/adapters/chatgpt.ts
index aab399f7..463a684b 100644
--- a/browser-extension/src/adapters/chatgpt.ts
+++ b/browser-extension/src/adapters/chatgpt.ts
@@ -13,9 +13,18 @@
// data: [DONE]
//
// A frame carrying only `v` inherits the previous frame's `o` (op) and `p`
-// (path). We maintain a single "root" object and apply add/append/replace/patch
-// to it, tracking the visible assistant message (author.role === 'assistant',
-// recipient 'all') and accumulating its content/parts text.
+// (path). Each root `add` creates a message, numbered from 0 in stream order;
+// a frame's `c` names the message it applies to and is inherited the same way.
+// Ops for different messages interleave, so applying every op to the newest
+// message duplicates text.
+//
+// One turn adds many messages: system and context messages, the assistant's
+// call to a tool (recipient `web.run`), the tool's replies (author.role 'tool',
+// with metadata.search_model_queries and metadata.search_result_groups),
+// reasoning (`thoughts`, `reasoning_recap`), and the visible answer. An answer
+// interrupted by a tool call streams one text message per segment and, when
+// there are several, also a merged message (metadata.is_merged_message) whose
+// text is all of them joined.
//
// NOTE: ChatGPT sometimes hands the stream off to a resume endpoint instead of
// streaming inline on this POST (a `stream_handoff` event). That resume stream
@@ -39,9 +48,10 @@ class ChatGptParser implements TurnParser {
// delta_encoding v1 cursor + document state
private lastOp = '';
private lastPath = '';
- private root: any = null;
- /** Reference to the visible assistant message object (mutated in place by ops). */
- private assistantMsg: any = null;
+ private lastC = -1;
+ private nextC = 0;
+ /** Every message the stream added, keyed by its `c` index. */
+ private messages = new Map<number, any>();
constructor(reqId: string) {
this.reqId = reqId;
@@ -101,50 +111,33 @@ class ChatGptParser implements TurnParser {
return;
}
- // Delta op: resolve op/path with bare-value inheritance.
+ // Delta op: resolve op, path and target message with bare-value inheritance.
if ('o' in obj) this.lastOp = obj.o;
if ('p' in obj) this.lastPath = obj.p;
- this.apply(this.lastOp, this.lastPath, obj.v);
- }
-
- private apply(op: string, path: string, v: any): void {
- switch (op) {
- case 'add':
- if (path === '' || path === '/') {
- this.root = v;
- this.trackAssistant();
- } else {
- setAt(this.root, path, v);
- }
- break;
- case 'append':
- appendAt(this.root, path, v);
- break;
- case 'replace':
- setAt(this.root, path, v);
- break;
- case 'patch':
- if (Array.isArray(v)) for (const s of v) this.apply(s.o, s.p ?? '', s.v);
- break;
+ if (typeof obj.c === 'number') this.lastC = obj.c;
+ if (this.lastOp === 'add' && (this.lastPath === '' || this.lastPath === '/')) {
+ const c = typeof obj.c === 'number' ? obj.c : this.nextC;
+ if (obj.v != null && typeof obj.v === 'object') this.messages.set(c, withTextParts(obj.v));
+ this.lastC = c;
+ this.nextC = Math.max(this.nextC, c + 1);
+ return;
}
- }
-
- /** After an add at root, record the visible assistant message reference. */
- private trackAssistant(): void {
- const m = this.root?.message;
- if (!m || m.author?.role !== 'assistant') return;
- if (m.recipient != null && m.recipient !== 'all') return; // tool/aside message
- if (!m.content || !Array.isArray(m.content.parts)) m.content = { parts: [''] };
- this.assistantMsg = m; // subsequent ops mutate this in place
+ const root = this.messages.get(this.lastC);
+ if (root) apply(root, this.lastOp, this.lastPath, obj.v);
}
getTurn(): ChatTurn | null {
- const m = this.assistantMsg;
- const responseText = m?.content?.parts?.filter((p: any) => typeof p === 'string').join('') ?? '';
+ const messages = [...this.messages.entries()]
+ .sort(([a], [b]) => a - b)
+ .map(([, root]) => root?.message)
+ .filter((m) => m != null && typeof m === 'object');
+ const answer = answerMessages(messages);
+ const responseText = answer.map(textOf).join('');
if (!responseText && !this.promptText) return null;
+ const m = answer[answer.length - 1];
const responseModel = m?.metadata?.model_slug ?? this.requestModel;
- const messageId: string = m?.id ?? '';
+ const messageId: string = typeof m?.id === 'string' ? m.id : '';
const sessionId = this.sessionId || `chatgpt-unknown-${this.reqId}`;
const turnKey = messageId || `r${this.reqId}`;
@@ -158,7 +151,7 @@ class ChatGptParser implements TurnParser {
inputMessages: this.inputMessages,
outputMessages: [{ role: 'assistant', text: responseText }],
responseText,
- toolCalls: extractToolCalls(m, messageId),
+ toolCalls: extractToolCalls(messages, m, messageId),
usage: {}, // ChatGPT web does not stream token usage
startedAt: this.startedAt,
completedAt: this.completedAt,
@@ -167,15 +160,122 @@ class ChatGptParser implements TurnParser {
}
}
-/** Best-effort web-search/tool capture from the assistant message metadata. */
-function extractToolCalls(m: any, messageId: string): ToolCall[] {
- const groups = m?.metadata?.search_result_groups;
+function apply(root: any, op: string, path: string, v: any): void {
+ switch (op) {
+ case 'add':
+ case 'replace':
+ setAt(root, path, v);
+ break;
+ case 'append':
+ appendAt(root, path, v);
+ break;
+ case 'remove':
+ removeAt(root, path);
+ break;
+ case 'patch':
+ if (Array.isArray(v)) for (const s of v) apply(root, s?.o, s?.p ?? '', s?.v);
+ break;
+ }
+}
+
+/** Give an assistant text message with no parts an empty one, so the appends
+ * to /message/content/parts/0 that follow have somewhere to land. */
+function withTextParts(root: any): any {
+ const m = root.message;
+ if (m && typeof m === 'object' && m.author?.role === 'assistant' && !Array.isArray(m.content?.parts)) {
+ const type = m.content?.content_type ?? 'text';
+ if (type === 'text') m.content = { ...m.content, content_type: 'text', parts: [''] };
+ }
+ return root;
+}
+
+/** The messages whose text is the visible answer: the merged message when
+ * the stream has one, otherwise every answer segment in order. */
+function answerMessages(messages: any[]): any[] {
+ const visible = messages.filter(isAnswerText);
+ const merged = visible.filter((m) => m.metadata?.is_merged_message === true);
+ return merged.length > 0 ? [merged[merged.length - 1]] : visible;
+}
+
+function isAnswerText(m: any): boolean {
+ if (m.author?.role !== 'assistant') return false;
+ if (m.recipient != null && m.recipient !== 'all') return false; // a call to a tool
+ const type = m.content?.content_type;
+ if (type !== 'text' && type !== 'multimodal_text') return false; // thoughts, context
+ return m.channel == null || m.channel === 'final';
+}
+
+function textOf(m: any): string {
+ const parts = m?.content?.parts;
+ return Array.isArray(parts) ? parts.filter((p: unknown) => typeof p === 'string').join('') : '';
+}
+
+/**
+ * Tool calls from the turn's messages. An assistant message addressed to a
+ * tool opens a call; the tool's replies that follow fill in the search queries
+ * it ran (the arguments) and the results it returned. The call message itself
+ * carries no input for web.run.
+ */
+function extractToolCalls(messages: any[], answer: any, messageId: string): ToolCall[] {
+ const calls: ToolCall[] = [];
+ const open = new Map<string, ToolCall>();
+ for (const m of messages) {
+ const role = m.author?.role;
+ if (role === 'assistant' && typeof m.recipient === 'string' && m.recipient !== 'all') {
+ const text = textOf(m).trim();
+ const call: ToolCall = { id: String(m.id ?? ''), name: toolName(m.recipient), arguments: text ? parseJson(text) : undefined };
+ calls.push(call);
+ open.set(m.recipient, call);
+ } else if (role === 'tool') {
+ const name = typeof m.author?.name === 'string' ? m.author.name : 'tool';
+ let call = open.get(name);
+ if (!call) {
+ call = { id: String(m.id ?? ''), name: toolName(name) };
+ calls.push(call);
+ open.set(name, call);
+ }
+ absorbToolReply(call, m);
+ }
+ }
+ if (calls.length > 0) return calls;
+ // A stream that names no tool messages may still attach its search results
+ // to the answer.
+ const groups = answer?.metadata?.search_result_groups;
if (Array.isArray(groups) && groups.length > 0) {
- return [{ id: `${messageId}:search`, name: 'web_search', arguments: { search_result_groups: groups } }];
+ return [{ id: `${messageId}:search`, name: 'web_search', result: { search_result_groups: groups } }];
}
return [];
}
+function absorbToolReply(call: ToolCall, m: any): void {
+ const queries = m.metadata?.search_model_queries?.queries;
+ if (Array.isArray(queries) && queries.length > 0 && call.arguments === undefined) {
+ call.arguments = { queries };
+ }
+ const groups = m.metadata?.search_result_groups;
+ if (Array.isArray(groups) && groups.length > 0) {
+ const prev = (call.result as { search_result_groups?: unknown[] } | undefined)?.search_result_groups ?? [];
+ call.result = { search_result_groups: [...prev, ...groups] };
+ return;
+ }
+ const text = textOf(m);
+ if (text && call.result === undefined) call.result = text;
+}
+
+/** web.run is ChatGPT's web tool; it is recorded as web_search, the name the
+ * Claude adapter and the threat rules use for the same thing. */
+function toolName(recipient: string): string {
+ return recipient === 'web.run' || recipient === 'web' ? 'web_search' : recipient;
+}
+
+function parseJson(text: string): unknown {
+ try {
+ return JSON.parse(text);
+ } catch {
+ return text;
+ }
+}
+
/** Navigate to the parent of `path` and return [container, key]. */
function locate(root: any, path: string): [any, string | number] | null {
const segs = path.split('/').filter(Boolean);
@@ -198,6 +298,8 @@ function appendAt(root: any, path: string, v: any): void {
const cur = (obj as any)[key];
if (typeof v === 'string') {
(obj as any)[key] = (typeof cur === 'string' ? cur : '') + v;
+ } else if (Array.isArray(v) && Array.isArray(cur)) {
+ (obj as any)[key] = [...cur, ...v];
} else if (v && typeof v === 'object' && !Array.isArray(v) && cur && typeof cur === 'object') {
(obj as any)[key] = { ...cur, ...v }; // merge (e.g. metadata) — preserves model_slug
} else {
@@ -212,6 +314,14 @@ function setAt(root: any, path: string, v: any): void {
(obj as any)[key] = v;
}
+function removeAt(root: any, path: string): void {
+ const loc = locate(root, path);
+ if (!loc) return;
+ const [obj, key] = loc;
+ if (Array.isArray(obj) && typeof key === 'number') obj.splice(key, 1);
+ else delete (obj as any)[key];
+}
+
function extractPrompt(body: any): { text: string; messages: Msg[] } {
const messages: Msg[] = [];
let text = '';
diff --git a/browser-extension/src/adapters/claude.ts b/browser-extension/src/adapters/claude.ts
index 2e2846e0..458d097c 100644
--- a/browser-extension/src/adapters/claude.ts
+++ b/browser-extension/src/adapters/claude.ts
@@ -11,6 +11,13 @@
// message_stop
// A legacy shape ({ completion: "…delta" }) is also tolerated.
//
+// claude.ai adds blocks of its own: `tool_result` (content_block_start carrying
+// tool_use_id and the whole result as `content`, for tools it runs server side),
+// `thinking` with thinking_summary_delta, and citation deltas on text blocks,
+// plus `ping` and `message_limit` events. As of October 2026 it sends no
+// `usage` at all. The request body is {prompt, model, ...}, sent gzip-compressed;
+// the interceptor decompresses it (src/interceptor/request-body.ts).
+//
// The exact claude.ai envelope must be confirmed against recorded live traffic
// (the live-smoke drift alarm exists for exactly this); the parser is written
// defensively so minor wrapping differences degrade gracefully.
@@ -107,6 +114,12 @@ class ClaudeParser implements TurnParser {
};
if (cb.type === 'tool_use') {
entry.tool = { id: String(cb.id ?? ''), name: String(cb.name ?? '') };
+ } else if (cb.type === 'tool_result') {
+ // claude.ai runs its tools (web search, weather, connectors) server
+ // side and streams each result whole, as its own block naming the
+ // tool_use it answers.
+ const call = this.toolCall(cb.tool_use_id);
+ if (call && cb.content !== undefined) call.result = cb.content;
}
this.blocks.set(obj.index ?? this.blocks.size, entry);
break;
@@ -137,6 +150,12 @@ class ClaudeParser implements TurnParser {
}
}
+ private toolCall(id: unknown): ToolCall | undefined {
+ if (typeof id !== 'string' || id === '') return undefined;
+ for (const entry of this.blocks.values()) if (entry.tool?.id === id) return entry.tool;
+ return undefined;
+ }
+
/** Merge the input-side counts of an Anthropic `usage` object into the turn.
*
* Read from both `message_start` and `message_delta` because which of the two
@@ -220,8 +239,9 @@ function extractPrompt(body: any): { text: string; messages: Msg[] } {
const messages: Msg[] = [];
let text = '';
if (typeof body?.prompt === 'string') {
+ // A retry_completion (regenerate) sends an empty prompt: no new message.
text = body.prompt;
- messages.push({ role: 'user', text });
+ if (text) messages.push({ role: 'user', text });
} else if (Array.isArray(body?.messages)) {
for (const m of body.messages) {
const t = coerceContent(m?.content);
diff --git a/browser-extension/src/interceptor/interceptor.ts b/browser-extension/src/interceptor/interceptor.ts
index 97d98125..9a7a7ba9 100644
--- a/browser-extension/src/interceptor/interceptor.ts
+++ b/browser-extension/src/interceptor/interceptor.ts
@@ -16,6 +16,7 @@
import type { InterceptEvent } from '../shared/types.js';
import { looksLikeChatRequest } from '../adapters/adapter.js';
import { offerChannel } from '../shared/channel.js';
+import { captureRequestBody } from './request-body.js';
declare global {
interface Window {
@@ -50,33 +51,28 @@ declare global {
const matched = looksLikeChatRequest(url);
- // Capture the request body BEFORE fetch consumes it. Prefer a string
- // init.body; otherwise read a clone of the Request (fetch(new Request(...))),
- // whose body would otherwise never be seen and the prompt would be lost.
- let body: string | null = null;
- if (matched) {
- if (typeof init?.body === 'string') {
- body = init.body;
- } else if (input instanceof Request) {
- try {
- body = await input.clone().text();
- } catch {
- body = null;
- }
- }
- }
+ // Snapshot the request body before fetch consumes it, and decode it while
+ // the request is in flight. claude.ai sends it gzip-compressed, so it is
+ // not always a string (see request-body.ts).
+ const body = matched ? captureRequestBody(input, init) : null;
const response = await originalFetch(input as RequestInfo, init);
- if (matched && response.body) {
+ if (body && response.body) {
const reqId = randomId();
- post({ kind: 'request', reqId, url, method, body });
-
- // Tee the stream via a clone so the page's own reader is untouched.
+ // Clone now, before the page starts reading, so its own reader is
+ // untouched; the clone buffers until the body is decoded.
const clone = response.clone();
- teeStream(clone, reqId, post).catch((err) =>
- post({ kind: 'error', reqId, message: String(err).slice(0, 1024) }),
- );
+ void body
+ .then((text) => {
+ post({ kind: 'request', reqId, url, method, body: text });
+ return teeStream(clone, reqId, post).catch((err) =>
+ post({ kind: 'error', reqId, message: String(err).slice(0, 1024) }),
+ );
+ })
+ .catch(() => {
+ /* the channel is gone; nothing to report to */
+ });
}
return response;
diff --git a/browser-extension/src/interceptor/request-body.ts b/browser-extension/src/interceptor/request-body.ts
new file mode 100644
index 00000000..ddcf420d
--- /dev/null
+++ b/browser-extension/src/interceptor/request-body.ts
@@ -0,0 +1,122 @@
+// Reads the body a page passes to fetch as text, for the MAIN-world
+// interceptor. The prompt lives in that body, so a body this cannot read is a
+// turn recorded with no prompt.
+//
+// claude.ai sends its completion body as a gzip-compressed Uint8Array with
+// `Content-Encoding: gzip`, so a string-only read sees nothing. Bytes, Blobs
+// and Request bodies are read here, and gzip or deflate is decompressed with
+// the browser's own DecompressionStream.
+//
+// Everything that must happen before fetch consumes or the page reuses the
+// body (copying bytes, cloning a Request) is done synchronously, in
+// captureRequestBody. The decoding runs alongside the request instead of
+// ahead of it, so the page's fetch is never delayed.
+
+import { LIMITS } from '../shared/validate.js';
+
+/** A decoded body longer than this would fail the service worker's request
+ * check and lose the whole turn, so it is dropped and only the prompt is lost. */
+export const MAX_BODY_CHARS = LIMITS.body;
+
+type Format = 'gzip' | 'deflate';
+
+/**
+ * Snapshot the request body now and decode it in the background. Never
+ * throws and never rejects: anything it cannot read resolves to null.
+ */
+export function captureRequestBody(input: RequestInfo | URL, init?: RequestInit): Promise<string | null> {
+ try {
+ const body = init?.body;
+ if (body != null) return fromBodyInit(body, contentEncoding(init?.headers, input));
+ if (typeof Request !== 'undefined' && input instanceof Request && input.body != null) {
+ // Clone before fetch consumes the original; read the clone later.
+ const clone = input.clone();
+ const encoding = contentEncoding(init?.headers, input);
+ return clone
+ .arrayBuffer()
+ .then((buf) => decodeBytes(new Uint8Array(buf), encoding))
+ .catch(() => null);
+ }
+ } catch {
+ // A used Request, an invalid header list: nothing readable.
+ }
+ return Promise.resolve(null);
+}
+
+function fromBodyInit(body: BodyInit, encoding: string | null): Promise<string | null> {
+ if (typeof body === 'string') return Promise.resolve(capChars(body));
+ if (body instanceof URLSearchParams) return Promise.resolve(capChars(body.toString()));
+ if (body instanceof ArrayBuffer) return decodeBytes(new Uint8Array(body.slice(0)), encoding);
+ if (ArrayBuffer.isView(body)) {
+ // Copy now: the page owns the buffer and may reuse it once fetch returns.
+ const view = new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
+ return decodeBytes(view.slice(), encoding);
+ }
+ if (typeof Blob !== 'undefined' && body instanceof Blob) {
+ // Blobs are immutable, so reading later sees what was sent.
+ return body
+ .arrayBuffer()
+ .then((buf) => decodeBytes(new Uint8Array(buf), encoding))
+ .catch(() => null);
+ }
+ // FormData and ReadableStream bodies cannot be read without consuming them.
+ return Promise.resolve(null);
+}
+
+/** The request's Content-Encoding, from init.headers or else the Request's own. */
+function contentEncoding(headers: HeadersInit | undefined, input: RequestInfo | URL): string | null {
+ if (headers != null) return new Headers(headers).get('content-encoding');
+ if (typeof Request !== 'undefined' && input instanceof Request) return input.headers.get('content-encoding');
+ return null;
+}
+
+/**
+ * Bytes to text: decompressed when the header (or, without one, the gzip
+ * magic number) says so, UTF-8 otherwise. An encoding the browser cannot
+ * decompress, a corrupt stream, or output over MAX_BODY_CHARS gives null.
+ */
+export async function decodeBytes(bytes: Uint8Array, encoding: string | null): Promise<string | null> {
+ const format = compressionFormat(bytes, encoding);
+ if (format === undefined) return null;
+ try {
+ if (format == null) return capChars(new TextDecoder().decode(bytes));
+ return await decompress(bytes, format);
+ } catch {
+ return null;
+ }
+}
+
+/** null for an uncompressed body, undefined for one that cannot be decoded. */
+function compressionFormat(bytes: Uint8Array, encoding: string | null): Format | null | undefined {
+ const enc = encoding?.trim().toLowerCase() ?? '';
+ if (enc === 'gzip' || enc === 'x-gzip') return 'gzip';
+ if (enc === 'deflate') return 'deflate';
+ if (enc !== '' && enc !== 'identity') return undefined; // br, zstd, a chain of codings
+ return bytes[0] === 0x1f && bytes[1] === 0x8b ? 'gzip' : null;
+}
+
+async function decompress(bytes: Uint8Array, format: Format): Promise<string | null> {
+ if (typeof DecompressionStream === 'undefined') return null;
+ const stream = new Blob([bytes as BlobPart]).stream().pipeThrough(new DecompressionStream(format));
+ const reader = stream.getReader();
+ const decoder = new TextDecoder();
+ let text = '';
+ let size = 0;
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ // UTF-8 never takes fewer bytes than UTF-16 code units, so a byte count
+ // within the cap keeps the text within it too.
+ size += value.byteLength;
+ if (size > MAX_BODY_CHARS) {
+ await reader.cancel().catch(() => {});
+ return null;
+ }
+ text += decoder.decode(value, { stream: true });
+ }
+ return text + decoder.decode();
+}
+
+function capChars(text: string): string | null {
+ return text.length > MAX_BODY_CHARS ? null : text;
+}
diff --git a/browser-extension/src/shared/normalize.ts b/browser-extension/src/shared/normalize.ts
index b3f457ac..8bc726f9 100644
--- a/browser-extension/src/shared/normalize.ts
+++ b/browser-extension/src/shared/normalize.ts
@@ -146,9 +146,11 @@ export function normalizeTurn(
// converter lifts the text into the top-level Event.prompt.text field (it
// only does so for prompt-category events). Without this, the prompt stays
// buried in raw.attributes and never reaches the queryable prompt.text column.
+ // A turn with no prompt (a regenerated answer) gets no prompt record.
+ const hasPrompt = turn.promptText !== '' || turn.inputMessages.length > 0;
const prompt = shapeText(turn.promptText, retention);
const inputMsgs = shapeMessages(turn.inputMessages, retention);
- if (prompt || inputMsgs) {
+ if (hasPrompt && (prompt || inputMsgs)) {
const attrs = base('prompt.submitted');
if (turn.requestModel) attrs.push(str('gen_ai.request.model', turn.requestModel));
let truncated = false;
@@ -270,7 +272,8 @@ function toolRecord(
str('gen_ai.tool.call.id', tool.id),
str('gen_ai.tool.call.name', tool.name),
];
- const args = shapeText(stringify(tool.arguments), retention);
+ // A call whose input the site does not expose has no arguments, not "null".
+ const args = tool.arguments === undefined ? null : shapeText(stringify(tool.arguments), retention);
let truncated = args?.truncated ?? false;
if (args) attrs.push(str('gen_ai.tool.call.arguments', args.text));
if (tool.result !== undefined) {
diff --git a/browser-extension/test/unit/chatgpt.adapter.test.ts b/browser-extension/test/unit/chatgpt.adapter.test.ts
index 4b4c9982..7a4472a0 100644
--- a/browser-extension/test/unit/chatgpt.adapter.test.ts
+++ b/browser-extension/test/unit/chatgpt.adapter.test.ts
@@ -67,3 +67,49 @@ describe('chatgpt adapter — existing conversation', () => {
expect(turn.sessionId).toBe('body-conv-123');
});
});
+
+// web-search-split.sse follows the op pattern of an October 2026 chatgpt.com
+// turn (sanitized and shortened): an answer interrupted by a web.run search.
+// Each delta's `c` names the message it applies to, and the ops for the answer
+// segments interleave with the same text sent to a merged message.
+describe('chatgpt adapter — answer split around a web search', () => {
+ const ANSWER = "The Boston Marathon is the world's oldest annual marathon.\n\n**Next race:** Monday, April 19, 2027.";
+ const turn = run('web-search-split', 'Boston Marathon', null)!.getTurn()!;
+
+ it('records the whole answer once, not the newest segment or a doubled copy', () => {
+ expect(turn.responseText).toBe(ANSWER);
+ expect(turn.outputMessages).toEqual([{ role: 'assistant', text: ANSWER }]);
+ });
+
+ it('is the same for every chunk size', () => {
+ for (const n of [1, 5, 64, 100000]) {
+ expect(run('web-search-split', 'x', null, n).getTurn()!.responseText).toBe(ANSWER);
+ }
+ });
+
+ it('keys the turn by the merged answer message', () => {
+ expect(turn.turnId).toBe('6adb0000-0000-8000-8000-000000000001:merged-0001');
+ expect(turn.responseModel).toBe('gpt-6');
+ });
+
+ it('records the search with its query as arguments and its results as the result', () => {
+ expect(turn.toolCalls).toHaveLength(1);
+ const [call] = turn.toolCalls;
+ expect(call.id).toBe('call-0001');
+ expect(call.name).toBe('web_search');
+ expect(call.arguments).toEqual({ queries: ['2027 Boston Marathon date'] });
+ const result = call.result as { search_result_groups: { entries: { url: string }[] }[] };
+ expect(result.search_result_groups.flatMap((g) => g.entries.map((e) => e.url))).toEqual([
+ 'https://www.example.org/boston-2027',
+ ]);
+ });
+
+ it('joins the segments in order when the stream has no merged message', () => {
+ const frames = fixture('web-search-split').split('\n\n').filter((f) => !f.includes('"c":4}'));
+ const parser = chatgptAdapter.createParser('req-0002');
+ parser.onRequest('https://chatgpt.com/backend-api/f/conversation', 'POST', reqBody('x', null));
+ parser.onChunk(frames.join('\n\n'));
+ parser.onDone();
+ expect(parser.getTurn()!.responseText).toBe(ANSWER);
+ });
+});
diff --git a/browser-extension/test/unit/claude.adapter.test.ts b/browser-extension/test/unit/claude.adapter.test.ts
index 57183b6b..6fbb1348 100644
--- a/browser-extension/test/unit/claude.adapter.test.ts
+++ b/browser-extension/test/unit/claude.adapter.test.ts
@@ -132,3 +132,55 @@ describe('claude adapter -- partial stream usage', () => {
expect(turn.usage?.cacheReadInputTokens).toBe(18500);
});
});
+
+// web-search.sse is a sanitized October 2026 claude.ai capture: claude.ai runs
+// web_search itself and streams each result as a tool_result block, between
+// thinking blocks, pings and citation deltas.
+describe('claude adapter -- server-side tool results', () => {
+ const turn = run('web-search', 'Search the web for the latest NASA news.')!.getTurn()!;
+
+ it('pairs each tool_result with the tool_use it answers', () => {
+ expect(turn.toolCalls.map((t) => [t.id, t.name, t.arguments])).toEqual([
+ ['toolu_search_1', 'web_search', { mode: 'standard', query: 'NASA news today' }],
+ ['toolu_search_2', 'web_search', { mode: 'standard', query: 'NASA October 2026' }],
+ ]);
+ const [first, second] = turn.toolCalls.map((t) => t.result as { title: string }[]);
+ expect(first.map((r) => r.title)).toEqual(['NASA News Today', 'Latest NASA Updates']);
+ expect(second.map((r) => r.title)).toEqual(['Artemis Accords Signatories']);
+ });
+
+ it('keeps only the answer text, not thinking summaries or citations', () => {
+ expect(turn.responseText).toBe('One recent NASA headline: a new country signed the Artemis Accords.');
+ });
+
+ it('takes the model and turn key from message_start', () => {
+ expect(turn.responseModel).toBe('claude-opus-5-5');
+ expect(turn.turnId).toBe(`${CONV}:00000000-0000-7000-8000-000000000002`);
+ expect(turn.completedAt).toBeTypeOf('number');
+ });
+
+ it('ignores a tool_result that names no captured tool_use', () => {
+ const parser = claudeAdapter.createParser('req-0002');
+ parser.onRequest(URL, 'POST', JSON.stringify({ prompt: 'hi' }));
+ parser.onChunk(
+ 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":' +
+ '{"type":"tool_result","tool_use_id":"toolu_missing","content":[{"type":"text","text":"x"}]}}\n\n',
+ );
+ parser.onDone();
+ expect(parser.getTurn()!.toolCalls).toEqual([]);
+ });
+});
+
+describe('claude adapter -- regenerate', () => {
+ it('records no prompt for a retry_completion, which sends an empty one', () => {
+ const parser = claudeAdapter.createParser('req-0003');
+ parser.onRequest(URL.replace(/completion$/, 'retry_completion'), 'POST', JSON.stringify({ prompt: '', model: 'claude-opus-5-5' }));
+ const body = fixture('simple-turn');
+ parser.onChunk(body);
+ parser.onDone();
+ const turn = parser.getTurn()!;
+ expect(turn.promptText).toBe('');
+ expect(turn.inputMessages).toEqual([]);
+ expect(turn.responseText).toBe('Paris.');
+ });
+});
diff --git a/browser-extension/test/unit/normalize.test.ts b/browser-extension/test/unit/normalize.test.ts
index 72e23f39..e1ee37f9 100644
--- a/browser-extension/test/unit/normalize.test.ts
+++ b/browser-extension/test/unit/normalize.test.ts
@@ -163,6 +163,15 @@ describe('normalizeTurn — full retention, simple completed turn', () => {
});
});
+describe('normalizeTurn — a turn with no prompt', () => {
+ // A regenerated answer: claude.ai's retry_completion sends an empty prompt.
+ it('emits no prompt.submitted record, only the response', () => {
+ const { logRecords } = normalize(baseTurn({ promptText: '', inputMessages: [] }));
+ expect(byAction(logRecords, 'prompt.submitted')).toBeUndefined();
+ expect(byAction(logRecords, 'agent.response.completed')).toBeTruthy();
+ });
+});
+
describe('normalizeTurn — retention modes', () => {
it('metadata retention drops the prompt.submitted record and all text', () => {
const { logRecords } = normalize(baseTurn(), 'metadata');
diff --git a/browser-extension/test/unit/request-body.test.ts b/browser-extension/test/unit/request-body.test.ts
new file mode 100644
index 00000000..36fa3f6f
--- /dev/null
+++ b/browser-extension/test/unit/request-body.test.ts
@@ -0,0 +1,98 @@
+import { describe, it, expect } from 'vitest';
+import { gzipSync, deflateSync, brotliCompressSync } from 'node:zlib';
+import { captureRequestBody, decodeBytes, MAX_BODY_CHARS } from '../../src/interceptor/request-body.js';
+
+const BODY = JSON.stringify({ prompt: 'What is the capital of France? Café ☕', model: 'claude-opus-5-5' });
+const URL = 'https://claude.ai/api/organizations/o/chat_conversations/c/completion';
+const gzipped = () => new Uint8Array(gzipSync(BODY));
+
+describe('captureRequestBody', () => {
+ it('reads a string body as is', async () => {
+ expect(await captureRequestBody(URL, { method: 'POST', body: BODY })).toBe(BODY);
+ });
+
+ // What claude.ai sends as of October 2026.
+ it('decompresses a gzip Uint8Array body sent with Content-Encoding: gzip', async () => {
+ const init = { method: 'POST', body: gzipped(), headers: { 'Content-Type': 'application/json', 'Content-Encoding': 'gzip' } };
+ expect(await captureRequestBody(URL, init)).toBe(BODY);
+ });
+
+ it('recognizes gzip by its magic number when the header is missing', async () => {
+ expect(await captureRequestBody(URL, { method: 'POST', body: gzipped() })).toBe(BODY);
+ });
+
+ it('decompresses deflate when the header names it', async () => {
+ const body = new Uint8Array(deflateSync(BODY));
+ expect(await captureRequestBody(URL, { method: 'POST', body, headers: [['content-encoding', 'deflate']] })).toBe(BODY);
+ });
+
+ it('reads uncompressed bytes, ArrayBuffers, DataViews and Blobs as UTF-8', async () => {
+ const bytes = new TextEncoder().encode(BODY);
+ expect(await captureRequestBody(URL, { method: 'POST', body: bytes })).toBe(BODY);
+ expect(await captureRequestBody(URL, { method: 'POST', body: bytes.buffer.slice(0) })).toBe(BODY);
+ expect(await captureRequestBody(URL, { method: 'POST', body: new DataView(bytes.buffer.slice(0)) })).toBe(BODY);
+ expect(await captureRequestBody(URL, { method: 'POST', body: new Blob([gzipped()]), headers: { 'content-encoding': 'gzip' } })).toBe(BODY);
+ });
+
+ it('reads only the viewed slice of a larger buffer', async () => {
+ const bytes = new TextEncoder().encode(`xx${BODY}yy`);
+ expect(await captureRequestBody(URL, { method: 'POST', body: bytes.subarray(2, bytes.length - 2) })).toBe(BODY);
+ });
+
+ it('copies the bytes before the page can reuse its buffer', async () => {
+ const bytes = gzipped();
+ const pending = captureRequestBody(URL, { method: 'POST', body: bytes });
+ bytes.fill(0);
+ expect(await pending).toBe(BODY);
+ });
+
+ it('reads a compressed body from a Request, leaving the original unread for fetch', async () => {
+ const request = new Request(URL, { method: 'POST', body: gzipped(), headers: { 'content-encoding': 'gzip' } });
+ expect(await captureRequestBody(request)).toBe(BODY);
+ expect(request.bodyUsed).toBe(false);
+ });
+
+ it('reads URLSearchParams as a form string', async () => {
+ expect(await captureRequestBody(URL, { method: 'POST', body: new URLSearchParams({ a: '1' }) })).toBe('a=1');
+ });
+
+ it('gives null for bodies it cannot read without consuming them, or no body', async () => {
+ const form = new FormData();
+ form.set('prompt', 'hi');
+ expect(await captureRequestBody(URL, { method: 'POST', body: form })).toBeNull();
+ expect(await captureRequestBody(URL, { method: 'POST', body: new Blob([BODY]).stream(), duplex: 'half' } as RequestInit)).toBeNull();
+ expect(await captureRequestBody(URL)).toBeNull();
+ expect(await captureRequestBody(new Request(URL))).toBeNull();
+ });
+
+ it('gives null, never throws, for a Request whose body was already read', async () => {
+ const request = new Request(URL, { method: 'POST', body: BODY });
+ await request.text();
+ expect(await captureRequestBody(request)).toBeNull();
+ });
+});
+
+describe('decodeBytes', () => {
+ it('gives null for a coding the browser cannot decompress', async () => {
+ expect(await decodeBytes(new Uint8Array(brotliCompressSync(BODY)), 'br')).toBeNull();
+ expect(await decodeBytes(gzipped(), 'gzip, br')).toBeNull();
+ });
+
+ it('gives null for a corrupt gzip stream', async () => {
+ const bytes = gzipped().slice(0, 12);
+ expect(await decodeBytes(bytes, 'gzip')).toBeNull();
+ });
+
+ // The service worker refuses a request event over the limit, which would lose
+ // the whole turn, so an oversized body is dropped and only the prompt is lost.
+ it('gives null for a body that decompresses past the limit', async () => {
+ const bomb = new Uint8Array(gzipSync(Buffer.alloc(MAX_BODY_CHARS + 1, 0x61)));
+ expect(bomb.length).toBeLessThan(64 * 1024);
+ expect(await decodeBytes(bomb, 'gzip')).toBeNull();
+ expect(await decodeBytes(new Uint8Array(gzipSync(Buffer.alloc(1024, 0x61))), 'gzip')).toBe('a'.repeat(1024));
+ });
+
+ it('gives null for an uncompressed string over the limit', async () => {
+ expect(await captureRequestBody(URL, { method: 'POST', body: 'a'.repeat(MAX_BODY_CHARS + 1) })).toBeNull();
+ });
+});
diff --git a/docs/runtimes/chatgpt-web.mdx b/docs/runtimes/chatgpt-web.mdx
index 23761faf..91b2060d 100644
--- a/docs/runtimes/chatgpt-web.mdx
+++ b/docs/runtimes/chatgpt-web.mdx
@@ -24,11 +24,12 @@ The extension's main-world interceptor tees the streamed response from the messa
|---------------|------------------------------|
| The outbound request body | Prompt text, the input messages, and the requested model |
| `conversation_id` in the request body or the resume token | `session.id` and `gen_ai.conversation.id` |
-| `add`, `append`, `replace`, and `patch` delta frames | Assistant response text, assembled as it streams |
+| `add`, `append`, `replace`, `remove`, and `patch` delta frames | Assistant response text, assembled as it streams |
+| Messages addressed to `web.run`, and the tool's replies | Web searches: the queries the site reports and the results it returned |
| The assistant message metadata | The model that actually answered, which can differ from the requested one |
| `message_stream_complete` or `[DONE]` | Turn completion |
-Only the visible assistant message is tracked. Tool and aside messages addressed to another recipient are skipped, so the recorded response is what the user saw.
+One turn streams several messages, and each delta frame names the message it applies to. The response is built from the visible answer only: tool calls, tool replies, and reasoning messages are left out of it. When a web search interrupts the answer, the site streams each part of the answer as its own message and also a merged copy of the whole answer. Beacon records the merged copy, or the parts joined in order when there is none, so the response appears once and in full.
The service worker then posts the turn to the local collector as OTLP GenAI logs. Events carry `harness.collection_method` of `otlp`.
@@ -40,10 +41,10 @@ One ChatGPT turn produces:
| Event | When |
|-------|------|
-| `prompt.submitted` | The request body carried prompt text or input messages |
+| `prompt.submitted` | The request body carried prompt text or input messages. A regenerated answer carries none and gets no prompt event |
| `agent.response.completed` | The stream finished normally |
| `agent.response` | The stream ended early, so the response is partial |
-| `tool.invoked` | The turn included web search results, recorded as a `web_search` call |
+| `tool.invoked` | Once per web search, recorded as a `web_search` call with the queries as arguments, when the site reports them, and the search results as the result |
Turns from one conversation share a session id, so a conversation reads as a session in the dashboard even though the site sends no session start or stop signal.
@@ -53,7 +54,7 @@ Turns from one conversation share a session id, so a conversation reads as a ses
|------|---------|
| Prompt telemetry | Supported, with full prompt text under `full` and `redacted` retention |
| Assistant response telemetry | Supported, including the response text, the requested model, and the model that answered |
-| Tool telemetry | Partial. Web search activity is captured from the assistant message metadata; other built-in tools are not exposed in a form the adapter can read |
+| Tool telemetry | Partial. Web searches are captured with their results, and with their queries when the site reports them, which it does not do for every search. Other built-in tools are recorded by name, with any result text they stream |
| Token usage | Not available. ChatGPT web does not report token counts in its stream |
| Cost | Not available. ChatGPT web does not report per-turn cost to the page |
| Command and file telemetry | Not applicable. Browser chat performs no local commands or file edits |
@@ -62,7 +63,7 @@ Turns from one conversation share a session id, so a conversation reads as a ses
| Local JSONL and dashboard | Supported after events reach the collector |
| MDM deployment | Not covered by Beacon's MDM assets. Distribute through browser policy instead |
-Token usage is the main difference from [Claude.ai](/runtimes/claude-web), which does report counts. Reports that roll up token usage will show ChatGPT web turns with no token attribution.
+[Claude.ai](/runtimes/claude-web) has the same gap in practice: its adapter reads counts when they are present, but claude.ai does not send them. Reports that roll up token usage will show browser chat turns with no token attribution.
## Confirm it is working
diff --git a/docs/runtimes/claude-web.mdx b/docs/runtimes/claude-web.mdx
index 73885a6e..c80c8aa2 100644
--- a/docs/runtimes/claude-web.mdx
+++ b/docs/runtimes/claude-web.mdx
@@ -20,11 +20,12 @@ The extension's main-world interceptor tees the streamed response from claude.ai
| Stream signal | What Beacon takes from it |
|---------------|------------------------------|
-| The outbound request body | Prompt text and the input messages |
+| The outbound request body | Prompt text and the input messages. claude.ai sends this body gzip-compressed, and the interceptor decompresses it in the page |
| The conversation id in the request URL or body | `session.id` and `gen_ai.conversation.id` |
| `message_start` | Response model and input token count |
| `content_block_delta` | Assistant response text, and tool call arguments as they stream |
| `content_block_start` for a `tool_use` block | Tool call id and name |
+| `content_block_start` for a `tool_result` block | The result of a tool claude.ai ran itself, attached to the call it answers |
| `message_delta` | Output token count |
| `message_stop` | Turn completion |
@@ -38,10 +39,10 @@ One claude.ai turn produces:
| Event | When |
|-------|------|
-| `prompt.submitted` | The request body carried prompt text or input messages |
+| `prompt.submitted` | The request body carried prompt text. A regenerated answer sends an empty prompt and gets no prompt event |
| `agent.response.completed` | The stream finished normally |
| `agent.response` | The stream ended early, so the response is partial |
-| `tool.invoked` | Once per `tool_use` block in the turn, with the call id, name, and arguments |
+| `tool.invoked` | Once per `tool_use` block in the turn, with the call id, name, arguments, and the result when the stream carried one |
Turns from one conversation share a session id, so a conversation reads as a session in the dashboard even though the site sends no session start or stop signal.
@@ -52,7 +53,8 @@ Turns from one conversation share a session id, so a conversation reads as a ses
| Prompt telemetry | Supported, with full prompt text under `full` and `redacted` retention |
| Assistant response telemetry | Supported, including the response text and the model reported by the stream |
| Tool telemetry | Supported. Tool call id, name, and arguments are captured from `tool_use` blocks |
-| Tool results | Not available. The chat stream carries the tool call, not the result Anthropic computed for it |
+| Tool results | Supported for tools claude.ai runs on its side, such as web search and weather. The stream sends each result as a `tool_result` block, recorded in `gen_ai.tool.call.result` |
+| Reasoning | Not captured. claude.ai streams short thinking summaries, which the adapter skips |
| Token usage | Parsed when the stream carries it, which recorded traffic so far does not. See [Limits](#limits) |
| Cost | Not available. claude.ai does not report per-turn cost to the page |
| Command and file telemetry | Not applicable. Browser chat performs no local commands or file edits |