From be78d5a800b894191898dec3571f1010cab0df87 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 16:55:23 +0800 Subject: [PATCH 1/2] fix(agent): restore or refuse lane-mutated payloads on proxy-death direct fallback (#3304) --- src/agent/dsh-native.ts | 11 +++- src/agent/incremental-client.ts | 75 +++++++++++++++++++++++ src/agent/native-intercept.ts | 103 ++++++++++++++++++++++++++++---- src/agent/omp-native.ts | 11 +++- src/agent/opencode-native.ts | 26 +++++++- src/agent/pi-native.ts | 6 +- src/agent/pi.ts | 71 +++++++++++++++++++++- 7 files changed, 282 insertions(+), 21 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 86cff1b3c1..b8ce1c5f63 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -53,7 +53,7 @@ import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScr import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, type GateVerdict, type NativeInterceptState } from "./native-intercept.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js"; import { createForkAdopter, sideShapedBody } from "./fork-adopt.js"; -import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; import { registerCompactionDisable } from "./dsh-compaction-disable.js"; export { sideShapedBody } from "./fork-adopt.js"; @@ -1588,6 +1588,9 @@ export function apply(ctx: PluginContext): void { await maybeForkAdoptBeforeSend(ctx, init); await maybeIncrementalNative(url, init); }; + // #3304: the dsh swap stamps the turn id atomically with the body, so the + // strict shared guard applies — mutated + unrestorable ⇒ refuse locally. + state.prepareDirectSend = createPrepareDirectSend(); // #3248 incremental turns (#3370 hardened): reference wiring for the // native fetch lanes; opencode/omp-native follow the same recipe: @@ -1652,8 +1655,12 @@ export function apply(ctx: PluginContext): void { if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; } } if (inflight) return; - init.body = (plan as { body: string }).body; const turnId = newIncrementalTurnId(); + // #3304: stash the pre-swap body under this turn's id — if the proxy + // dies before the send lands, the direct-send guard restores these + // bytes instead of forwarding the lane-only payload upstream. + rememberOriginalBody(turnId, body); + init.body = (plan as { body: string }).body; const headers = new Headers(init.headers); headers.set(INCREMENTAL_TURN_MARKER_HEADER, "1"); headers.set(INCREMENTAL_TURN_ID_HEADER, turnId); diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index 92a53b21f4..dc1451d52b 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -17,6 +17,7 @@ // content ids server-side, so hosts without their own message ids work. import { createHash } from "node:crypto"; +import type { DirectSendPrep } from "./native-intercept.js"; /** Marker header the lane's divert recognizes. Client-side copy of * INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side @@ -42,6 +43,79 @@ export function newIncrementalTurnId(): string { return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; } +// #3304: originals stash for the direct-send guard. When an adapter swaps a +// request's body for the incremental payload, it stashes the ORIGINAL body +// string under that turn's id. If the proxy dies before the send lands, +// native-intercept's fall-back consults createPrepareDirectSend below and +// either restores these bytes (availability) or refuses the direct send +// (safety) — a lane-only payload never reaches a provider that cannot parse +// it. Entries age out by TTL (a single turn never outlives minutes) and are +// capped so a leak cannot grow the map. +const ORIGINAL_TTL_MS = 5 * 60_000; +const ORIGINAL_MAX_ENTRIES = 256; +const originalBodies = new Map(); + +/** Stash the pre-swap body of one planned turn under its turn id. */ +export function rememberOriginalBody(turnId: string, body: string): void { + const now = Date.now(); + for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key); + if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) { + let oldestKey: string | undefined; + let oldestAt = Number.POSITIVE_INFINITY; + for (const [key, entry] of originalBodies) { + if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } + } + if (oldestKey !== undefined) originalBodies.delete(oldestKey); + } + originalBodies.set(turnId, { body, at: now }); +} + +/** Non-consuming lookup: a host-level retry re-dispatches the SAME request + * (same turn id) and must get the original back again, so entries are kept + * until TTL instead of being deleted on read. */ +export function originalBodyForTurn(turnId: string): string | null { + const entry = originalBodies.get(turnId); + if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null; + return entry.body; +} + +/** #3304: prepareDirectSend implementation for lanes that stamp the turn id + * ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of + * x-bili-turn-id on the outgoing request proves the body was replaced, so + * a missing/expired original means "mutated but unrecoverable" → refuse + * rather than risk sending lane-only bytes upstream. Lanes without the hook + * (every non-incremental traffic) keep today's byte-identical behavior. */ +export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { + return async (_url, input, init) => { + let turnId: string | null = null; + try { + const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters[0] | undefined; + if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER); + } catch { + turnId = null; + } + if (turnId === null) return { outcome: "as-is" }; + const body = originalBodyForTurn(turnId); + return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; + }; +} + +/** #3304: is this parsed body an incremental envelope — i.e. WOULD the + * lane's marker divert claim it? Mirrors incrementalTurnBodyShape in + * src/incremental-lane.ts (keep in sync): such a body is lane-only, so it + * must never be forwarded to the real upstream as-is. Returns the + * conversationId when it matches (pi correlates its stash through it), + * undefined otherwise. */ +export function incrementalEnvelopeConversationId(body: unknown): string | undefined { + if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined; + const b = body as Record; + if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined; + if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined; + if (!Array.isArray(b.messages)) return undefined; + if (b.model !== undefined) return undefined; + return b.conversationId; +} + /** #3329: drop pending entries older than `ttlMs` — a planned turn whose * response never comes (network-dead request) must not pin its slot forever. * Memory hygiene only since attribution is exact per request: correctness no @@ -297,4 +371,5 @@ export function __resetIncrementalClientForTests(): void { rejectionCircuit.clear(); droppedParamWarned.clear(); droppedParamWarnPairs = 0; + originalBodies.clear(); } diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index 4b8020b609..03905674c2 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -22,6 +22,13 @@ import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME, * context window. */ export type GateVerdict = boolean | "refuse-oversized"; +/** #3304: prepareDirectSend's verdict for one fall-back send to the real + * upstream (see the state field). */ +export type DirectSendPrep = + | { outcome: "as-is" } + | { outcome: "restored"; body: string } + | { outcome: "unrestorable" }; + export interface NativeInterceptState { /** Proxy origin ("http://127.0.0.1:PORT") once the bootstrap resolved. * Written by the owner (pi-native.ts); read synchronously on each call. */ @@ -62,7 +69,22 @@ export interface NativeInterceptState { * the request init (body) and may spend async time; wire-mode requests * (headersFor returns nothing) still pass through it — the hook itself * gates on its own conditions. */ - beforeSend?: (url: string, init: RequestInit | undefined) => Promise; + beforeSend?: (url: string, init: RequestInit | undefined) => Promise; + /** #3304: consulted immediately before ANY fall-back send to the REAL + * upstream (proxy-death give-up, bootstrap-fail, unattributed direct, + * signed passthrough). Lanes that mutate the outgoing payload (the + * incremental-turn adapters) arm it; every other lane leaves it + * undefined and each fall-back behaves byte-identically to pre-#3304. + * The hook sees the SAME (input, init) pair the fall-back would send + * (headers/body already mutated by the lane's own swap) and answers: + * - "as-is" — the payload is a standalone request → send it; + * - "restored" — send `body` INSTEAD (the pre-mutation original); + * - "unrestorable" — the payload was mutated and cannot be undone → + * the direct send is refused locally (TypeError) so + * a lane-only body never reaches a provider that + * cannot parse it; the host's retry re-runs the full + * decision (re-probe, re-plan) from scratch. */ + prepareDirectSend?: (url: string, input: string | URL | Request, init: RequestInit | undefined) => DirectSendPrep | Promise; /** #3248 incremental turns: response hook — fired with the routed * Response BEFORE it is returned to the host, so response headers are * readable without consuming the stream. Native hosts use it to read @@ -627,6 +649,26 @@ export function replaceRequestPayload(e: { request?: unknown }, body: string, ex } } +/** #3304: rebuild a fall-back dispatch carrying `body` INSTEAD of whatever + * `init`/the Request carried. The restored body rides INIT so fetch-spec + * precedence (init over Request fields) stays exactly what the original + * dispatch had; a string body needs no duplex. */ +export function withRestoredBody(input: string | URL | Request, init: RequestInit | undefined, body: string): { input: string | URL | Request; init: RequestInit | undefined } { + if (input instanceof Request) { + try { + const plain = new Request(input.url, { method: input.method, headers: input.headers }); + return { input: plain, init: { ...(init ?? {}), body } }; + } catch { + const headers: Record = {}; + try { + for (const [k, v] of input.headers) headers[k] = v; + } catch {} + return { input: input.url, init: { ...(init ?? {}), method: input.method, headers, body } }; + } + } + return { input, init: { ...(init ?? {}), body } }; +} + /** Install the global fetch patch. Idempotent: a second call is a no-op * (returns false) so double-loading the entry cannot double-wrap. */ export function installNativeFetchIntercept(state: NativeInterceptState): boolean { @@ -683,6 +725,47 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea return again; }; + // #3304: ask the lane whether this fall-back payload may go to the + // real upstream AS-IS. Fail-safe direction: an armed hook that + // throws is treated as "unrestorable" — never risk sending bytes the + // lane may have mutated. + const prepareForDirectSend = async (url: string, input: string | URL | Request, init: RequestInit | undefined): Promise => { + const hook = state.prepareDirectSend; + if (hook === undefined) return { outcome: "as-is" }; + try { + return await hook(url, input, init); + } catch (err) { + logHostEvent("error", "[native-client]", `bili-native: prepareDirectSend hook failed (${err instanceof Error ? err.message : String(err)}) — refusing the direct send (#3304)`); + return { outcome: "unrestorable" }; + } + }; + + // #3304: the guarded fall-back send. `defaultInput` is what the site + // would send without the guard (usually `input`; makeTarget(routedTarget) + // on the routed give-up). Restored bodies ride INIT so fetch-spec + // precedence (init over Request) stays exactly what the original + // dispatch had. + const directSendOrRefuse = async (dispatchUrl: string, defaultInput: string | URL | Request, init: RequestInit | undefined): Promise => { + const prep = await prepareForDirectSend(dispatchUrl, defaultInput, init); + if (prep.outcome === "as-is") { + state.onDispatch?.(dispatchUrl, "direct"); + return send(defaultInput, init); + } + if (prep.outcome === "unrestorable") { + // A lane-only payload (e.g. an incremental delta) cannot be + // forwarded to a provider that cannot parse it — refusing + // locally keeps the host's retry loop driving the decision + // again (re-probe / re-plan) instead of burning the turn on + // an upstream 400. + state.onDispatch?.(dispatchUrl, "refused"); + logHostEvent("error", "[native-client]", `bili-native: no usable proxy and the request carries a lane-only payload — failing locally instead of sending it to the provider; retry the turn (#3304): ${dispatchUrl}`); + throw new TypeError("billion-context: proxy unavailable — the request payload is lane-specific (incremental turn) and cannot be forwarded to the provider directly; retry the turn (#3304)"); + } + const restored = withRestoredBody(defaultInput, init, prep.body); + state.onDispatch?.(dispatchUrl, "direct"); + return send(restored.input, restored.init); + }; + // Already-routed `/bili/` model requests (launcher settings overlay): // routing is done, but plugin headers still decide wire vs plugin // mode — stamp and pass through untouched otherwise. @@ -708,8 +791,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea } if (verdict !== true) { const stamped = withHeaders(input, init, { [BILI_PASSTHROUGH_HEADER]: "1" }); - state.onDispatch?.(url, "direct"); - return send(stamped.input, stamped.init); + return directSendOrRefuse(url, stamped.input, stamped.init); } } // #1365: attributed routed traffic pins this process's model channel @@ -792,8 +874,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea warned = true; logHostEvent("error", "[native-client]", `bili-native: no live proxy — model requests go direct (uncompressed): ${routedTarget}`); } - state.onDispatch?.(routedTarget, "direct"); - return send(makeTarget(routedTarget), init); + return directSendOrRefuse(routedTarget, makeTarget(routedTarget), init); } } if (!isModelApiUrl(url)) { @@ -850,8 +931,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea } clearSignedRefusal(signedScheme); noteSignedDirect(url, signedScheme, "built-in passthrough opt-in (#1884) — byte-untouched, no compression"); - state.onDispatch?.(url, "direct"); - return send(input, init); + return directSendOrRefuse(url, input, init); } resignExtra = { [APIG_RESIGN_HEADER]: APIG_RESIGN_SCHEME, @@ -897,8 +977,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea state.onDispatch?.(url, "refused"); return oversizeRefusal(url); } - state.onDispatch?.(url, "direct"); - return send(input, init); + return directSendOrRefuse(url, input, init); } } @@ -910,8 +989,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea warned = true; logHostEvent("error", "[native-client]", `bili-native: proxy not ready — model request goes direct (uncompressed): ${url}`); } - state.onDispatch?.(url, "direct"); - return send(input, init); + return directSendOrRefuse(url, input, init); } // Attach mode rewrites exactly like spawn mode (#809 semantics — // opencode's attach probe+rewrite; the V1 fetch patch relies on it to @@ -974,8 +1052,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea warned = true; logHostEvent("error", "[native-client]", `bili-native: proxy respawn failed — model requests go direct (uncompressed): ${url}`); } - state.onDispatch?.(url, "direct"); - return send(input, init); + return directSendOrRefuse(url, input, init); } throw err; } diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 2b959fc47d..51dea31cb2 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -25,7 +25,7 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { createBiliPlugin } from "./pi.js"; import { applyOmpFirstEventTimeout, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; -import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; @@ -140,12 +140,16 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; } } if (inflight) { return; } + const turnId = newIncrementalTurnId(); + // #3304: stash the pre-swap body under this turn's id — if the proxy + // dies before the send lands, the direct-send guard restores these + // bytes instead of forwarding the lane-only payload upstream. + rememberOriginalBody(turnId, init.body); init.body = plan.body; const headers = new Headers(init.headers); headers.set("x-bili-plugin", "omp"); headers.set("x-bili-plugin-conversation", sid); headers.set(INCREMENTAL_TURN_MARKER_HEADER, "1"); - const turnId = newIncrementalTurnId(); headers.set(INCREMENTAL_TURN_ID_HEADER, turnId); init.headers = headers; incPendingOmp.set(turnId, { sid, url, messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, base, at: Date.now() }); @@ -155,6 +159,9 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | } state.beforeSend = ompIncrementalBeforeSend; +// #3304: the omp swap stamps the turn id atomically with the body, so the +// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. +state.prepareDirectSend = createPrepareDirectSend(); export function ompIncrementalOnResponse(_url: string, response: Response, headers?: Record): void { const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 387c38bf59..1aaa6da8c4 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -77,7 +77,7 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST, unwrapUpstream, wrapUpstream import { createAcpCommandHooks, showAcpText } from "./opencode-acp-command.js"; import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js"; import { createLiveOriginResolver, installNativeFetchIntercept, isModelApiUrl, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, replaceRequestPayload, replaceRequestTarget, routedBiliModelUrl, type LiveOriginResolverDeps, type NativeInterceptState } from "./native-intercept.js"; -import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import { createOpencodeV2Setup, type V2HttpRequestEvent, type V2State } from "./opencode-v2.js"; import { asciiHeaderValue, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, waitForProxyVersion, type ForwardedToolResult } from "./shared.js"; import { callLegacyAcpConfig, isLegacyAcpSession, loadLegacyAcp, type LegacyAcpModule } from "./opencode-legacy.js"; @@ -213,6 +213,9 @@ export function createNativeRoute(state: NativeInterceptState, deps: OpencodeNat } const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; +// #3304: the opencode swap stamps the turn id atomically with the body, so the +// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. +state.prepareDirectSend = createPrepareDirectSend(); async function bootstrap(): Promise { try { @@ -858,7 +861,21 @@ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State if (!sid || !base || req === undefined || typeof req.clone !== "function") return; const url = req.url; sweepStale(incPendingOpencode, INC_PENDING_TTL_MS); - const body: unknown = await req.clone().json().catch(() => null); + // #3304: keep the ORIGINAL body string — stashed under the turn id + // below so the direct-send guard can restore it when the proxy dies + // before the swapped payload lands. + let origText: string; + try { + origText = await req.clone().text(); + } catch { + return; + } + let body: unknown; + try { + body = JSON.parse(origText); + } catch { + return; + } if (body === null || typeof body !== "object") return; const b = body as { messages?: unknown; model?: unknown; system?: unknown }; if (typeof b.model !== "string" || !wireMessagesLookAnthropic(b.messages)) return; // anthropic-wire only; OpenAI-tool shapes resend full @@ -877,6 +894,11 @@ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State } if (inflight) return; const turnId = newIncrementalTurnId(); + // #3304: stash before the swap — the opencode replacement happens in + // this hook, BEFORE the fetch intercept ever sees the request, so + // every direct-send fall-back (not just the give-up paths) relies on + // these bytes. + rememberOriginalBody(turnId, origText); replaceRequestPayload(e, plan.body, { [INCREMENTAL_TURN_MARKER_HEADER]: "1", [INCREMENTAL_TURN_ID_HEADER]: turnId }); incPendingOpencode.set(turnId, { sid, url, messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, base, at: Date.now() }); } catch { diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index 295a3ccc66..018a3dc7c2 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -29,7 +29,7 @@ import os from "node:os"; import path from "node:path"; import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { piPackageStrings } from "../plugin-install.js"; -import { createBiliPlugin } from "./pi.js"; +import { createBiliPlugin, createPiDirectSendGuard } from "./pi.js"; import { isLegacyBcpEntry, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { loadConfigFile } from "../config.js"; import { getInlineEngine, INLINE_ORIGIN } from "../inline-engine.js"; @@ -118,6 +118,10 @@ function errMessage(err: unknown): string { } const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; +// #3304: the pi extension swaps payloads inside its runner — the guard +// sniffs the outgoing envelope and restores the stashed original before any +// direct-send fall-back reaches the real upstream. +state.prepareDirectSend = createPiDirectSendGuard(); async function bootstrap(): Promise { try { diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 2b3a56059f..38f1541cd0 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -14,7 +14,8 @@ import { disposeSubagentSelfReg, selfRegisterForSession, type SubagentSelfRegSta import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; -import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; +import type { DirectSendPrep } from "./native-intercept.js"; import { asciiHeaderValue, detectProxyBase, destinationRoutedThroughProxy, fetchManifest, forwardTool, fetchStatus, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, armedIdleNotice, noSessionWarning, nonHttpProvidersFromEnv, type ForwardedToolResult, type ManifestTool } from "./shared.js"; import { createForkAdopter } from "./fork-adopt.js"; import { logHostEvent } from "./host-log.js"; @@ -746,6 +747,69 @@ async function registerTools(pi: ExtensionAPI, ctx: Ctx, state: RegisterState, a } } +// #3304: pi's payload swap happens INSIDE the runner (before_provider_request +// returns the replacement), so the fetch layer cannot correlate a request by +// object identity. Instead it sniffs the outgoing body for the incremental +// envelope — the same shape test the lane's divert runs server-side — and +// restores the original stashed under the envelope's conversationId. Pi sends +// model requests strictly sequentially per session (the same invariant +// pendingIncrementalAcks relies on), so one entry per conversation is exact. +const PI_ORIGINAL_TTL_MS = 5 * 60_000; +const piIncrementalOriginals = new Map(); + +function stashPiIncrementalOriginal(conversationId: string, payload: unknown): void { + const now = Date.now(); + sweepStale(piIncrementalOriginals, PI_ORIGINAL_TTL_MS); + piIncrementalOriginals.set(conversationId, { body: JSON.stringify(payload), at: now }); +} + +function takePiIncrementalOriginal(conversationId: string): string | null { + const entry = piIncrementalOriginals.get(conversationId); + if (entry === undefined || Date.now() - entry.at >= PI_ORIGINAL_TTL_MS) return null; + return entry.body; +} + +export function __resetPiIncrementalOriginalsForTests(): void { + piIncrementalOriginals.clear(); +} + +/** Test-only: seed the stash directly (driving the full extension event + * chain just for this is not worth it). */ +export function __stashPiIncrementalOriginalForTests(conversationId: string, payload: unknown): void { + stashPiIncrementalOriginal(conversationId, payload); +} + +/** #3304: prepareDirectSend for the pi/omp extension lanes (pi-native arms + * it on its NativeInterceptState). A body that IS an incremental envelope + * was swapped by maybeIncrementalTurn → restore the stashed original + * (or refuse when the stash aged out); any other body is a standalone + * full-resend request → as-is. */ +export function createPiDirectSendGuard(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { + return async (_url, input, init) => { + let text: string | undefined; + try { + if (typeof init?.body === "string") { + text = init.body; + } else if (input instanceof Request) { + text = await input.clone().text(); + } + } catch { + text = undefined; // unreadable body — treat as standalone + } + if (text === undefined || text.length === 0) return { outcome: "as-is" }; + let parsed: unknown; + try { + parsed = JSON.parse(text); + } catch { + return { outcome: "as-is" }; + } + const sid = incrementalEnvelopeConversationId(parsed); + if (sid === undefined) return { outcome: "as-is" }; + const body = takePiIncrementalOriginal(sid); + return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; + }; +} + export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalMs?: number }): (pi: ExtensionAPI) => void { return function biliPlugin(pi: ExtensionAPI): void { const agent = agentName(agentOverride); @@ -1238,6 +1302,10 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM request: payload as Record, }); if (plan.mode === "skip") return undefined; + // #3304: stash the ORIGINAL payload under this conversation — the + // direct-send guard restores it if the proxy dies before the + // swapped envelope lands on the wire. + stashPiIncrementalOriginal(sid, payload); pendingIncrementalAcks.set(sid, { messages: payload.messages as IncrementalWireMessage[], system: "system" in payload ? payload.system : undefined, base: proxyBase }); return JSON.parse((plan as { body: string }).body) as unknown; } @@ -1349,6 +1417,7 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM return; } pendingIncrementalAcks.delete(sid); + piIncrementalOriginals.delete(sid); // #3304: turn settled — original no longer needed if (status === 200) { noteIncrementalAck(sid, Number(seq), pending.messages, pending.system); return; From 5b201102002fab31edac73e7cfa836cc703a28b3 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 16:55:23 +0800 Subject: [PATCH 2/2] test(agent): pin the #3304 direct-send guard across all four incremental instances --- .../issue3304-incremental-direct-send.test.ts | 448 ++++++++++++++++++ 1 file changed, 448 insertions(+) create mode 100644 tests/issue3304-incremental-direct-send.test.ts diff --git a/tests/issue3304-incremental-direct-send.test.ts b/tests/issue3304-incremental-direct-send.test.ts new file mode 100644 index 0000000000..8fb07944ca --- /dev/null +++ b/tests/issue3304-incremental-direct-send.test.ts @@ -0,0 +1,448 @@ +// #3304 — proxy-death direct fallback must never forward a lane-mutated +// (incremental) payload to the real upstream. The pre-fix behavior sent the +// delta envelope verbatim → upstream 400. Now every fall-back send goes +// through state.prepareDirectSend: lanes that swapped the body restore the +// stashed original (availability), and when the original is gone the direct +// send is refused locally (safety — the host's retry re-runs the decision). +// Lanes without the hook keep today's byte-identical fall-back behavior. +import test from "node:test"; +import assert from "node:assert/strict"; +import { installNativeFetchIntercept, withRestoredBody, _resetForTest, type NativeInterceptState } from "../src/agent/native-intercept.ts"; +import { createPrepareDirectSend, incrementalEnvelopeConversationId, INCREMENTAL_TURN_ID_HEADER, rememberOriginalBody, originalBodyForTurn, __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts"; + +const ORIGINAL_BODY = JSON.stringify({ model: "claude-test", max_tokens: 1024, messages: [{ role: "user", content: "hello" }] }); +const DELTA_BODY = JSON.stringify({ conversationId: "conv-1", sinceSeq: 3, path: "/v1/messages", request: { model: "claude-test" }, messages: [{ role: "assistant", content: "hi" }] }); + +// Captured before any intercept install in this file — the install replaces +// globalThis.fetch with an accessor and _resetForTest only restores the +// descriptor that existed AT INSTALL TIME, so a per-test "saved" value +// poisons every later test (the probe would hit a dead proxy stub). +const REAL_FETCH: typeof fetch = globalThis.fetch; + +// ── A. strict shared hook (dsh / omp / opencode) ──────────────────────────── + +test("#3304 createPrepareDirectSend: no turn id ⇒ as-is (init headers)", async () => { + const hook = createPrepareDirectSend(); + const prep = await hook("http://x/v1/messages", "http://x/v1/messages", { method: "POST", headers: { "content-type": "application/json" }, body: DELTA_BODY }); + assert.deepEqual(prep, { outcome: "as-is" }); +}); + +test("#3304 createPrepareDirectSend: no turn id on bare string input ⇒ as-is", async () => { + const hook = createPrepareDirectSend(); + const prep = await hook("http://x/v1/messages", "http://x/v1/messages", undefined); + assert.deepEqual(prep, { outcome: "as-is" }); +}); + +test("#3304 createPrepareDirectSend: turn id + stashed original ⇒ restored (init headers)", async () => { + try { + __resetIncrementalClientForTests(); + rememberOriginalBody("t-init", ORIGINAL_BODY); + const hook = createPrepareDirectSend(); + const prep = await hook("http://x/v1/messages", "http://x/v1/messages", { method: "POST", headers: { [INCREMENTAL_TURN_ID_HEADER]: "t-init" }, body: DELTA_BODY }); + assert.deepEqual(prep, { outcome: "restored", body: ORIGINAL_BODY }); + // non-consuming: a host-level retry of the SAME request reads again + const again = await hook("http://x/v1/messages", "http://x/v1/messages", { method: "POST", headers: { [INCREMENTAL_TURN_ID_HEADER]: "t-init" }, body: DELTA_BODY }); + assert.deepEqual(again, { outcome: "restored", body: ORIGINAL_BODY }); + } finally { + __resetIncrementalClientForTests(); + } +}); + +test("#3304 createPrepareDirectSend: turn id on a Request-object input ⇒ restored", async () => { + try { + __resetIncrementalClientForTests(); + rememberOriginalBody("t-req", ORIGINAL_BODY); + const req = new Request("http://x/v1/messages", { method: "POST", headers: { [INCREMENTAL_TURN_ID_HEADER]: "t-req" }, body: DELTA_BODY }); + const prep = await createPrepareDirectSend()("http://x/v1/messages", req, undefined); + assert.deepEqual(prep, { outcome: "restored", body: ORIGINAL_BODY }); + } finally { + __resetIncrementalClientForTests(); + } +}); + +test("#3304 createPrepareDirectSend: turn id WITHOUT stashed original ⇒ unrestorable", async () => { + try { + __resetIncrementalClientForTests(); + const prep = await createPrepareDirectSend()("http://x/v1/messages", "http://x/v1/messages", { method: "POST", headers: { [INCREMENTAL_TURN_ID_HEADER]: "t-missing" }, body: DELTA_BODY }); + assert.deepEqual(prep, { outcome: "unrestorable" }); + } finally { + __resetIncrementalClientForTests(); + } +}); + +test("#3304 originalBodyForTurn: unknown id ⇒ null", () => { + __resetIncrementalClientForTests(); + assert.equal(originalBodyForTurn("never-stashed"), null); +}); + +// ── B. envelope shape test (pi correlation) ───────────────────────────────── + +test("#3304 incrementalEnvelopeConversationId: accepts the buildBody envelope", () => { + assert.equal(incrementalEnvelopeConversationId(JSON.parse(DELTA_BODY)), "conv-1"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", sinceSeq: 0, path: "/v1/messages", request: {}, messages: [], resync: true }), "c"); +}); + +test("#3304 incrementalEnvelopeConversationId: rejects ordinary anthropic bodies and malformed envelopes", () => { + assert.equal(incrementalEnvelopeConversationId(JSON.parse(ORIGINAL_BODY)), undefined, "a top-level model disqualifies it"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", sinceSeq: 1, path: "/v1/messages", request: {}, messages: [], model: "m" }), undefined, "a top-level model means it is not an envelope"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", path: "/v1/messages", request: {}, messages: [] }), undefined, "missing sinceSeq is load-bearing"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "", sinceSeq: 1, path: "/v1/messages", request: {}, messages: [] }), undefined, "empty conversationId"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", sinceSeq: 1.5, path: "/v1/messages", request: {}, messages: [] }), undefined, "non-integer sinceSeq"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", sinceSeq: -1, path: "/v1/messages", request: {}, messages: [] }), undefined, "negative sinceSeq"); + assert.equal(incrementalEnvelopeConversationId({ conversationId: "c", sinceSeq: 1, path: "/v1/messages", request: {} }), undefined, "missing messages array"); + assert.equal(incrementalEnvelopeConversationId(["conversationId"]), undefined); + assert.equal(incrementalEnvelopeConversationId(null), undefined); + assert.equal(incrementalEnvelopeConversationId("nope"), undefined); +}); + +// ── C. withRestoredBody ───────────────────────────────────────────────────── + +test("#3304 withRestoredBody: string input keeps the url, body rides init", () => { + const out = withRestoredBody("http://up/v1/messages", { method: "POST", headers: { a: "1" } }, ORIGINAL_BODY); + assert.equal(out.input, "http://up/v1/messages"); + assert.equal(out.init?.body, ORIGINAL_BODY); + assert.equal((out.init?.method ?? "GET"), "POST"); +}); + +test("#3304 withRestoredBody: an existing init.body (the delta) is overridden", () => { + const out = withRestoredBody("http://up/v1/messages", { method: "POST", body: DELTA_BODY }, ORIGINAL_BODY); + assert.equal(out.init?.body, ORIGINAL_BODY); +}); + +test("#3304 withRestoredBody: Request input keeps url/method/headers, body rides init", async () => { + const req = new Request("http://up/v1/messages", { method: "POST", headers: { "x-bili-turn-id": "t9", authorization: "Bearer sk" }, body: DELTA_BODY }); + const out = withRestoredBody(req, undefined, ORIGINAL_BODY); + assert.ok(out.input instanceof Request); + assert.equal(out.input.url, "http://up/v1/messages"); + assert.equal(out.input.method, "POST"); + assert.equal(out.input.headers.get("x-bili-turn-id"), "t9"); + assert.equal(out.input.headers.get("authorization"), "Bearer sk"); + assert.equal(out.init?.body, ORIGINAL_BODY); +}); + +// ── D. integration: installNativeFetchIntercept fall-back sites ───────────── + +interface Call { + url: string; + body: string | null; + headers: Record; +} + +function fakeFetchRecording(sink: Call[]) { + return (async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + let body: string | null = null; + try { + if (typeof init?.body === "string") body = init.body; + else if (input instanceof Request && !input.bodyUsed) body = await input.clone().text(); + } catch {} + const headers: Record = {}; + try { + const src = init?.headers !== undefined ? init.headers : input instanceof Request ? input.headers : undefined; + if (src !== undefined) for (const [k, v] of new Headers(src).entries()) headers[k] = v; + } catch {} + sink.push({ url, body, headers }); + return new Response("{}", { status: 200 }); + }) as typeof fetch; +} + +async function withIntercept(state: NativeInterceptState, inner: typeof fetch, fn: () => Promise): Promise<{ result?: T; error?: unknown }> { + _resetForTest(); + __resetIncrementalClientForTests(); + globalThis.fetch = inner; + try { + assert.equal(installNativeFetchIntercept(state), true); + let result: T | undefined; + let error: unknown; + try { + result = await fn(); + } catch (err) { + error = err; + } + return { result, error }; + } finally { + // reset FIRST (restores the pre-install descriptor), then pin the + // pristine fetch — the reverse order would resurrect the stub. + _resetForTest(); + globalThis.fetch = REAL_FETCH; + __resetIncrementalClientForTests(); + } +} + +const PROXY_ORIGIN = "http://127.0.0.1:40001"; +const UPSTREAM_URL = "http://127.0.0.1:8199/v1/messages"; + +/** Records every call; proxy-bound attempts die with a network-level + * TypeError (the observed death) instead of returning a response. */ +function deadProxyFetch(sink: Call[]) { + return (async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + if (url.startsWith(`${PROXY_ORIGIN}/bili/`)) { + sink.push({ url, body: null, headers: {} }); + throw new TypeError("fetch failed"); + } + let body: string | null = null; + try { + if (typeof init?.body === "string") body = init.body; + else if (input instanceof Request && !input.bodyUsed) body = await input.clone().text(); + } catch {} + const headers: Record = {}; + try { + const src = init?.headers !== undefined ? init.headers : input instanceof Request ? input.headers : undefined; + if (src !== undefined) for (const [k, v] of new Headers(src).entries()) headers[k] = v; + } catch {} + sink.push({ url, body, headers }); + return new Response("{}", { status: 200 }); + }) as typeof fetch; +} + +/** State whose held proxy is dead and whose respawn yields nothing — the + * give-up condition of #3304 (probe said alive ≤30s earlier, adapter + * swapped, then the proxy died). */ +function deadProxyState(extra: Partial = {}): NativeInterceptState { + return { + origin: PROXY_ORIGIN, + ready: Promise.resolve(PROXY_ORIGIN), + respawn: () => Promise.resolve(undefined), + ...extra, + }; +} + +test("#3304 rewrite give-up: swapped payload is RESTORED before the direct send", async () => { + const state = deadProxyState({ + prepareDirectSend: createPrepareDirectSend(), + onGiveUp: () => {}, + onDispatch: () => {}, + }); + const calls: Call[] = []; + const { result } = await withIntercept(state, deadProxyFetch(calls), async () => { + rememberOriginalBody("t-giveup", ORIGINAL_BODY); + const res = await globalThis.fetch(UPSTREAM_URL, { + method: "POST", + headers: { "content-type": "application/json", [INCREMENTAL_TURN_ID_HEADER]: "t-giveup" }, + body: DELTA_BODY, + }); + return res.status; + }); + assert.equal(result, 200); + assert.equal(calls.length, 2, "one proxy attempt + one restored direct send"); + assert.ok(calls[0].url.startsWith(`${PROXY_ORIGIN}/bili/${UPSTREAM_URL}`)); + assert.equal(calls[1].url, UPSTREAM_URL); + assert.equal(calls[1].body, ORIGINAL_BODY, "the direct send carries the ORIGINAL bytes, not the delta"); + assert.equal(calls[1].headers["x-bili-turn-id"], "t-giveup", "lane headers ride along untouched"); +}); + +test("#3304 routed give-up: makeTarget(routedTarget) fall-back is restored too", async () => { + const state = deadProxyState({ + prepareDirectSend: createPrepareDirectSend(), + onGiveUp: () => {}, + onDispatch: () => {}, + }); + const calls: Call[] = []; + const { result } = await withIntercept(state, deadProxyFetch(calls), async () => { + rememberOriginalBody("t-routed", ORIGINAL_BODY); + const res = await globalThis.fetch(`${PROXY_ORIGIN}/bili/${UPSTREAM_URL}`, { + method: "POST", + headers: { "content-type": "application/json", [INCREMENTAL_TURN_ID_HEADER]: "t-routed" }, + body: DELTA_BODY, + }); + return res.status; + }); + assert.equal(result, 200); + assert.equal(calls.length, 2); + assert.equal(calls[1].url, UPSTREAM_URL, "degrades to the embedded upstream"); + assert.equal(calls[1].body, ORIGINAL_BODY); +}); + +test("#3304 give-up with a turn id but NO stashed original refuses instead of sending the delta", async () => { + const state = deadProxyState({ + prepareDirectSend: createPrepareDirectSend(), + onGiveUp: () => {}, + onDispatch: () => {}, + }); + const calls: Call[] = []; + const { error } = await withIntercept(state, deadProxyFetch(calls), async () => { + await globalThis.fetch(UPSTREAM_URL, { + method: "POST", + headers: { "content-type": "application/json", [INCREMENTAL_TURN_ID_HEADER]: "t-ghost" }, + body: DELTA_BODY, + }); + }); + assert.ok(error instanceof TypeError, "the request fails locally for the host to retry"); + assert.match(String(error), /retry the turn/); + assert.equal(calls.length, 1, "NO direct send reached the upstream"); + assert.ok(calls[0].url.startsWith(`${PROXY_ORIGIN}/bili/`)); +}); + +test("#3304 a throwing prepareDirectSend hook fails safe (refusal, not send)", async () => { + const state = deadProxyState({ + prepareDirectSend: async () => { + throw new Error("boom"); + }, + onGiveUp: () => {}, + onDispatch: () => {}, + }); + const calls: Call[] = []; + const { error } = await withIntercept(state, deadProxyFetch(calls), async () => { + await globalThis.fetch(UPSTREAM_URL, { method: "POST", body: DELTA_BODY }); + }); + assert.ok(error instanceof TypeError); + assert.equal(calls.length, 1, "hook failure ⇒ no direct send"); +}); + +test("#3304 bootstrap-fail (origin never landed) restores a swapped Request-object input (opencode shape)", async () => { + // opencode replaces e.request BEFORE the intercept sees it: at + // bootstrap-fail time the input IS the swapped Request. + const state: NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + prepareDirectSend: createPrepareDirectSend(), + onDispatch: () => {}, + }; + const calls: Call[] = []; + const { result } = await withIntercept(state, deadProxyFetch(calls), async () => { + rememberOriginalBody("t-boot", ORIGINAL_BODY); + const req = new Request(UPSTREAM_URL, { + method: "POST", + headers: { "content-type": "application/json", [INCREMENTAL_TURN_ID_HEADER]: "t-boot" }, + body: DELTA_BODY, + }); + const res = await globalThis.fetch(req); + return res.status; + }); + assert.equal(result, 200); + assert.equal(calls.length, 1, "bootstrap-fail goes straight to the direct send"); + assert.equal(calls[0].url, UPSTREAM_URL); + assert.equal(calls[0].body, ORIGINAL_BODY); +}); + +test("#3304 no hook armed ⇒ fall-back is byte-identical to pre-fix (unswapped lanes unaffected)", async () => { + const state = deadProxyState({ onGiveUp: () => {}, onDispatch: () => {} }); + const plainBody = JSON.stringify({ model: "m", messages: [{ role: "user", content: "x" }] }); + const calls: Call[] = []; + const { result } = await withIntercept(state, deadProxyFetch(calls), async () => { + const res = await globalThis.fetch(UPSTREAM_URL, { method: "POST", headers: { "content-type": "application/json" }, body: plainBody }); + return res.status; + }); + assert.equal(result, 200); + assert.equal(calls.length, 2); + assert.equal(calls[1].url, UPSTREAM_URL); + assert.equal(calls[1].body, plainBody, "sent exactly as the host built it"); +}); + +// ── E. pi guard (envelope sniffing) ───────────────────────────────────────── + +const { createPiDirectSendGuard, __stashPiIncrementalOriginalForTests, __resetPiIncrementalOriginalsForTests } = await import("../src/agent/pi.ts"); + +test("#3304 pi guard: envelope body + stashed original ⇒ restored", async () => { + try { + __resetPiIncrementalOriginalsForTests(); + __stashPiIncrementalOriginalForTests("conv-1", JSON.parse(ORIGINAL_BODY)); + const guard = createPiDirectSendGuard(); + const prep = await guard(UPSTREAM_URL, UPSTREAM_URL, { method: "POST", body: DELTA_BODY }); + assert.deepEqual(prep, { outcome: "restored", body: ORIGINAL_BODY }); + } finally { + __resetPiIncrementalOriginalsForTests(); + } +}); + +test("#3304 pi guard: envelope body WITHOUT stashed original ⇒ unrestorable", async () => { + try { + __resetPiIncrementalOriginalsForTests(); + const prep = await createPiDirectSendGuard()(UPSTREAM_URL, UPSTREAM_URL, { method: "POST", body: DELTA_BODY }); + assert.deepEqual(prep, { outcome: "unrestorable" }); + } finally { + __resetPiIncrementalOriginalsForTests(); + } +}); + +test("#3304 pi guard: ordinary anthropic body (full resend) ⇒ as-is", async () => { + __resetPiIncrementalOriginalsForTests(); + const prep = await createPiDirectSendGuard()(UPSTREAM_URL, UPSTREAM_URL, { method: "POST", body: ORIGINAL_BODY }); + assert.deepEqual(prep, { outcome: "as-is" }); +}); + +test("#3304 pi guard: unreadable/stream body ⇒ as-is (cannot inspect, treat as standalone)", async () => { + __resetPiIncrementalOriginalsForTests(); + const req = new Request(UPSTREAM_URL, { + method: "POST", + body: ReadableStream.from([new TextEncoder().encode(DELTA_BODY)]), + duplex: "half", + } as RequestInit & { duplex?: string }); + // consume the stream so clone().text() cannot read it + await req.arrayBuffer(); + const prep = await createPiDirectSendGuard()(UPSTREAM_URL, req, undefined); + assert.deepEqual(prep, { outcome: "as-is" }); +}); + +// ── F. adapter instance wiring: the swap stashes the exact original ──────── + +process.env.NODE_TEST_CONTEXT = "1"; +import { after } from "node:test"; +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; + +const { maybeIncrementalOpencode } = await import("../src/agent/opencode-native.ts"); +const { ompIncrementalBeforeSend, _setOmpOriginForTest, __resetOmpIncrementalForTests } = await import("../src/agent/omp-native.ts"); + +function fakeLane(enabled: boolean): Promise<{ base: string; close: () => Promise }> { + const srv = createServer((req, res) => { + if (req.method === "GET" && req.url === "/__bili/plugin/turn") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, protocol: 1, feature: "pluginIncrementalTurns", enabled, storage: enabled ? "unified" : "legacy-only" })); + return; + } + res.writeHead(404); + res.end("{}"); + }); + return new Promise((r) => srv.listen(0, "127.0.0.1", () => r({ base: `http://127.0.0.1:${(srv.address() as AddressInfo).port}`, close: () => new Promise((done) => srv.close(() => done())) }))); +} + +after(async () => { + _setOmpOriginForTest(undefined); +}); + +test("#3304 opencode adapter: planned turn stashes the ORIGINAL body text under its turn id", async () => { + try { + __resetIncrementalClientForTests(); + const lane = await fakeLane(true); + try { + const origText = JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [{ role: "user", content: "hi" }] }); + const e = { request: new Request(UPSTREAM_URL, { method: "POST", headers: { "content-type": "application/json" }, body: origText }), sessionID: "oc-sid-1" }; + await maybeIncrementalOpencode(e as never, { proxyBase: lane.base } as never); + const swapped = (e as { request: Request }).request; + const turnId = swapped.headers.get(INCREMENTAL_TURN_ID_HEADER); + assert.ok(turnId, "the swap stamps its per-request turn id"); + assert.notEqual(await swapped.text(), origText, "the request body IS the delta now"); + assert.equal(originalBodyForTurn(turnId), origText, "the stash holds the EXACT pre-swap bytes"); + } finally { + await lane.close(); + } + } finally { + __resetIncrementalClientForTests(); + } +}); + +test("#3304 omp adapter: planned turn stashes the ORIGINAL init.body under its turn id", async () => { + try { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(true); + try { + _setOmpOriginForTest(lane.base); + const origText = JSON.stringify({ model: "claude-test", max_tokens: 64, prompt_cache_key: "omp-sid-1", messages: [{ role: "user", content: "hi" }] }); + const init: RequestInit = { method: "POST", headers: { "content-type": "application/json" }, body: origText }; + await ompIncrementalBeforeSend(UPSTREAM_URL, init); + const headers = init.headers as Headers; + const turnId = headers.get(INCREMENTAL_TURN_ID_HEADER); + assert.ok(turnId, "the swap stamps its per-request turn id"); + assert.notEqual(init.body, origText, "init.body IS the delta now"); + assert.equal(originalBodyForTurn(turnId), origText, "the stash holds the EXACT pre-swap bytes"); + } finally { + _setOmpOriginForTest(undefined); + await lane.close(); + } + } finally { + __resetIncrementalClientForTests(); + } +});