From 0d78e3180365aa42708b67348c30dfdb7a05d97f Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 2 Oct 2026 14:32:06 +0800 Subject: [PATCH] fix: seam forensics re-notes outbound body on proxy-initiated re-send hops (#1894) --- src/compress-loop-responses.ts | 8 +- src/server.ts | 35 +++++- tests/issue1894-seam-hop-renote.test.ts | 160 ++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 tests/issue1894-seam-hop-renote.test.ts diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index a94d382b34..768cdf548a 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -3,6 +3,7 @@ import { type Config, type CoreMessage, } from "acp-kernel"; +import { noteForwardedBody } from "./cache-ledger.js"; import { lastCompressSuffix, withSessionLock, type Session } from "./session.js"; import { extractResponsesTextTriggers, PROXY_TOOL_NAMES, MUTATING_PROXY_TOOLS } from "./compress-tool.js"; import { log as loggerLog } from "./logger.js"; @@ -186,10 +187,15 @@ export async function compressLoopResponsesJson( inputItems.push({ type: "message", role: "developer", content: [{ type: "output_text", text: injection.text }] }); } requestBody.input = mergeAdjacentConfigurationUpdates(hoistTrappedToolItems(inputItems)); + // #1894: this re-request's usage settles in the caller (server.ts + // non-streaming lane), so note the exact bytes IT sent — same contract + // as loop/core.ts fetchUpstream. + const wireBytes = JSON.stringify(requestOptions.wireTransform ? requestOptions.wireTransform(requestBody) : requestBody); + noteForwardedBody(ctx.session, wireBytes); const result = await fetchWithRetry(requestOptions.url, { method: "POST", headers: requestOptions.headers, - body: JSON.stringify(requestOptions.wireTransform ? requestOptions.wireTransform(requestBody) : requestBody), + body: wireBytes, ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), }, undefined, undefined, (info) => { // #189: correlate the rejection with the rewrite that preceded it. diff --git a/src/server.ts b/src/server.ts index f9e40b3a53..953db1e194 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5531,8 +5531,14 @@ async function forward( // previous request's body. Loop re-fetches re-note their own rebuilt bodies // (loop/core.ts fetchUpstream); side requests never settle usage and must // not clobber the slot. + // #1894: effective outbound bytes for the rest of this request — the + // re-send hops below (compat ladder / overflow refold / fake-completion) + // each re-note on success so every settle pairs with the send whose usage + // report it is, never the rejected first attempt. + let seamBody: string | undefined; if (prepared?.session && !prepared.sidePassthrough && req.method !== "GET" && req.method !== "HEAD") { - noteForwardedBody(prepared.session, typeof wireBody === "string" ? wireBody : wireBody.toString("utf8")); + seamBody = typeof wireBody === "string" ? wireBody : wireBody.toString("utf8"); + noteForwardedBody(prepared.session, seamBody); } let upstreamResult: Awaited>; try { @@ -5625,6 +5631,12 @@ async function forward( upstreamResult.clearTimer(); remember(target, fixed.rewritten); upstreamResult = r; + // #1894: THIS send's bytes produced the settled usage — + // not the role-rejected first attempt. + if (seamBody !== undefined && prepared?.session) { + seamBody = fixed.body; + noteForwardedBody(prepared.session, seamBody); + } return "ok"; } let errText: string | null = null; @@ -5748,6 +5760,13 @@ async function forward( if (retried.response.ok) { upstreamResult.clearTimer(); upstreamResult = retried; + // #1894: the settled sample pairs with the refolded + // bytes that were actually accepted, not the + // overflow-rejected first attempt. + if (seamBody !== undefined) { + seamBody = typeof refolded === "string" ? refolded : refolded.toString("utf8"); + noteForwardedBody(prepared.session, seamBody); + } log("info", `[${prepared.session.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); } else { let retryErrText: string | null = null; @@ -6380,11 +6399,12 @@ async function forward( const reportedCached: number | null = typeof cached === "number" ? cached : null; const billed = typeof total === "number" ? total : 0; if (billed > 0 || reportedCached !== null) { - // wireBody — not prepared.body — is what actually went out - // (compat-role / steering / chain-stamp rewrites apply after - // prepare); the main chokepoint above already noted it, this - // keeps the pair byte-exact if that ever moves (#1891). - noteForwardedBody(prepared.session, typeof wireBody === "string" ? wireBody : wireBody.toString("utf8")); + // #1894: deliberately NO note here — this site runs AFTER + // the re-send hops, so pinning first-attempt bytes would + // clobber their re-notes. Every send path notes its own + // bytes at send time (main chokepoint, compat/refold hops, + // fake-completion retry, loop re-fetches); the settle pairs + // with whatever was last noted. settleUsageReport(prepared.session, { total: billed, reportedCached, output: out, protocol: prepared.protocol, upstream: targetOrigin }); if (reportedCached !== null) warnCacheCollapse(prepared.session, billed, reportedCached); const hitPct = reportedCached !== null && billed > 0 ? Math.round((100 * reportedCached) / billed) : undefined; @@ -6476,6 +6496,9 @@ async function resolveFakeCompletion( break; } buffer = await readStreamToBuffer(r.response.body, fakeBufCap()); + // #1894: the presented (and settled) bytes came from THIS + // hinted re-send, not the fake-completing first attempt. + noteForwardedBody(opts.session, hinted); } finally { r.clearTimer(); } diff --git a/tests/issue1894-seam-hop-renote.test.ts b/tests/issue1894-seam-hop-renote.test.ts new file mode 100644 index 0000000000..ca0fd65f8f --- /dev/null +++ b/tests/issue1894-seam-hop-renote.test.ts @@ -0,0 +1,160 @@ +// #1894: proxy-initiated re-send hops replace upstreamResult with MODIFIED +// bytes without re-noting the outbound body, so when the turn settles the +// seam-forensic pair compared the FIRST-ATTEMPT bytes against the previous +// baseline. Fix: forward() tracks the effective outbound bytes — each hop +// (compat-role ladder, overflow refold, fake-completion retry, JSON-loop +// re-fetch) re-notes its own bytes on success, and the non-streaming settle +// site no longer re-pins first-attempt bytes after the hops. +// +// This fixture drives the compat-role ladder end-to-end on the Responses +// lane (where client-sent "developer" items reach the wire, hoisted at +// input[0]): the upstream accepts turn 1, then starts rejecting role +// "developer" mid-session (learned-compat-roles kicking in). Turn 2's +// attempt 1 is 400'd, the ladder rewrites developer→system, attempt 2 lands +// with the cache collapsed — and the settled pair must carry the POST-HOP +// bytes, not the rejected first attempt. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { getCacheLedger } from "../src/cache-ledger.ts"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { getSession } from "../src/session.ts"; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function lcpLen(a: string, b: string): number { + const n = Math.min(a.length, b.length); + let i = 0; + while (i < n && a.charCodeAt(i) === b.charCodeAt(i)) i++; + return i; +} + +const msg = (role: string, text: string): Record => ({ type: "message", role, content: [{ type: role === "user" ? "input_text" : "output_text", text }] }); + +test("#1894: compat-ladder hop pairs the settled sample with the post-hop bytes", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + const received: string[] = []; + let reqNo = 0; + let okResponses = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const text = Buffer.concat(chunks).toString("utf8"); + reqNo += 1; + received.push(text); + // Mid-session policy flip: after the baseline turn, role + // "developer" is rejected — the #552 learn-on-failure shape. + if (reqNo > 1 && text.includes('"role":"developer"')) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "Invalid role: developer" } })); + return; + } + okResponses += 1; + // Baseline turn: healthy cache. Post-hop turn: cache collapse + // large enough to trip detectSeam's residual gates (same shape + // as the #1891 fixture, scaled up). + const cached = okResponses === 1 ? 199_000 : 20_000; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: `resp_${reqNo}`, + object: "response", + status: "completed", + output: [{ type: "message", id: `msg_${reqNo}`, role: "assistant", status: "completed", content: [{ type: "output_text", text: `ok ${reqNo}` }] }], + usage: { input_tokens: 200_000, output_tokens: 5, input_tokens_details: { cached_tokens: cached } }, + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + // Tool injection ON: the non-streaming settle branch is only reached + // through the rewriter path (compressInjected), same as the #1891 + // fixture. Messages stay far below any nudge threshold, so the + // injected surface is byte-stable across turns. + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const sessionId = "issue-1894-hop-renote"; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + const post = (input: Array>): Promise => + fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ model: "gpt-test", input }), + }); + + try { + // Turn 1: clean baseline — the "developer" item rides at input[0]. + const resp1 = await post([ + msg("user", "u1"), + { role: "developer", content: "s1" }, + ]); + assert.equal(resp1.status, 200); + await resp1.text(); + + // Turn 2: same head plus a new tail; the upstream now rejects the + // "developer" role. Attempt 1 is 400'd, the learn-on-failure ladder + // rewrites developer→system, attempt 2 lands with the cache collapsed. + const resp2 = await post([ + msg("user", "u1"), + { role: "developer", content: "s1" }, + msg("assistant", "a1"), + msg("user", "u2"), + ]); + assert.equal(resp2.status, 200); + await resp2.text(); + + assert.equal(received.length, 3, "baseline send + rejected attempt + accepted hop"); + const [base, preHop, postHop] = received as [string, string, string]; + assert.ok(preHop.includes('"role":"developer"'), "fixture: attempt 1 carries the rejected role"); + assert.ok(!postHop.includes('"role":"developer"'), "fixture: the hop rewrote the role"); + const expectedLcp = lcpLen(base, postHop); + const buggyLcp = lcpLen(base, preHop); + assert.notEqual(expectedLcp, buggyLcp, "fixture: the two variants diverge from the baseline at different positions"); + + const s = getSession(sessionId); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.seam, 1, "cache collapse after the hop still flags a seam"); + const ev = led.seamEvents?.[led.seamEvents.length - 1]; + assert.ok(ev, "forensic event recorded"); + // Pre-fix the pair carried preHop (lcpBytes == buggyLcp); the settled + // sample must pair with the bytes the accepted hop actually sent. + assert.equal(ev!.lcpBytes, expectedLcp, "settled pair carries the POST-HOP bytes, not the rejected first attempt"); + } finally { + await close(proxy); + await close(upstream); + } +});