diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5ef669814b..b90ec72d19 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, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; import { registerCompactionDisable } from "./dsh-compaction-disable.js"; export { sideShapedBody } from "./fork-adopt.js"; @@ -1410,11 +1410,12 @@ export function apply(ctx: PluginContext): void { const target = sessionModelTargetOf(ctx) ?? globalModelTarget(modelInfo.services); refreshModelInfo(register.base, target); const headers: Record = { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": sid }; - // #3370: the incremental-turn marker is no longer stamped here — a - // closure-level armed flag shared by every session raced concurrent - // requests across the awaited beforeSend → headersFor boundary. The - // marker now rides init.headers from the conversion itself (see - // maybeIncrementalNative), atomic with the body swap. + // #3370: the incremental-turn marker (and, since #3329, the turn id) + // is no longer stamped here — a closure-level armed flag shared by + // every session raced concurrent requests across the awaited + // beforeSend → headersFor boundary. Both now ride init.headers from + // the conversion itself (see maybeIncrementalNative), atomic with + // the body swap. if (target !== undefined) { const cached = modelInfo.byKey.get(modelInfoKey(target.provider, target.model)); if (cached !== undefined) { @@ -1441,19 +1442,30 @@ export function apply(ctx: PluginContext): void { // #3248 incremental turns (#3370 hardened): reference wiring for the // native fetch lanes; opencode/omp-native follow the same recipe: // beforeSend — parse the anthropic body, plan (probe-gated), replace + // #3248 incremental turns (#3370 hardened, #3329 attribution): reference + // wiring for the native fetch lanes; opencode/omp-native follow the same + // recipe: + // beforeSend — parse the anthropic body, plan (probe-gated), replace // init.body with the delta payload and stamp the marker - // header on the SAME init (atomic: omp pattern) + stash - // the ack context keyed by URL; - // onResponse — read x-bili-applied-seq and record the ack (or fall - // back to full resend on 409/503). + // + turn-id headers on the SAME init (atomic: omp + // pattern) + stash the ack context keyed by the + // per-request turn id (#3329 — keyed by URL, a late + // response on a shared model endpoint could be + // attributed to whichever conversation planned last); + // onResponse — attribute the response via the turn id THIS dispatch + // carried (threaded from the dispatch's outgoing + // headers), read x-bili-applied-seq, record the ack (or + // fall back to full resend on 409/503). // The old arms-a-shared-flag/consume-it-in-headersFor split raced when two - // sessions crossed the awaited hook boundary: the flag could be consumed - // by the WRONG request, shipping a delta body WITHOUT the marker — the - // proxy forwarded it verbatim and the model answered with no history - // (#3370). init.headers is merged by withHeaders and survives headersFor's - // own stamping, so the marker and the body can never part ways again. + // sessions crossed the awaited hook boundary (#3370) — init.headers is + // merged by withHeaders and survives headersFor's own stamping, so the + // marker and the body can never part ways again. The #3370 single-flight + // guard per URL is KEPT on top of turn-id keying (#3340 rebase decision, + // issuecomment-6212900441): while a marker'd request for this URL is + // still awaiting its ack, later requests on the same URL ride the normal + // pipeline — dropping it is a separate follow-up, not this PR. const INC_PENDING_TTL_MS = 30_000; // matches omp-native / opencode-native - const incPendingAcks = new Map(); + const incPendingAcks = new Map(); async function maybeIncrementalNative(url: string, init: RequestInit | undefined): Promise { if (!register.toolsReady || init === undefined) return; const attr = attributionOf(ctx); @@ -1470,6 +1482,7 @@ export function apply(ctx: PluginContext): void { const base = register.base ?? state.origin; if (base === undefined) return; if (!(await probeIncrementalTurns(base))) return; + sweepStale(incPendingAcks, INC_PENDING_TTL_MS); const sid = attr.sid; const plan = planIncrementalTurn({ conversationId: sid, @@ -1479,21 +1492,29 @@ export function apply(ctx: PluginContext): void { request: parsed as Record, }); if (plan.mode === "skip") return; - // #3370 single-flight (omp/opencode pattern): while a marker'd request - // for this URL is still awaiting its ack, later requests ride the - // normal pipeline — their acks could otherwise collide with the - // pending entry keyed by the same shared model endpoint URL. - const inflight = incPendingAcks.get(url); - if (inflight !== undefined && Date.now() - inflight.at < INC_PENDING_TTL_MS) return; + // #3370 single-flight (omp/opencode pattern; kept over #3329's + // removal per the rebase decision): while a marker'd request for + // this URL is still awaiting its ack, later requests ride the normal + // pipeline — pending entries are now keyed by turn id (#3329), so + // the guard scans for a live entry stashed on THIS url. + let inflight = false; + for (const pending of incPendingAcks.values()) { + 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(); const headers = new Headers(init.headers); headers.set(INCREMENTAL_TURN_MARKER_HEADER, "1"); + headers.set(INCREMENTAL_TURN_ID_HEADER, turnId); init.headers = headers; - incPendingAcks.set(url, { sid, messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, base, at: Date.now() }); + incPendingAcks.set(turnId, { sid, url, messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, base, at: Date.now() }); } - state.onResponse = (url, response) => { - const pending = incPendingAcks.get(url); - if (pending === undefined) return; + state.onResponse = (_url, response, headers) => { + const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; + if (turnId === undefined) return; // not an incremental dispatch — nothing to attribute (#3329) + const pending = incPendingAcks.get(turnId); + if (pending === undefined) return; // unknown/stale turn — nothing to attribute const seq = response.headers.get("x-bili-applied-seq"); if (response.status === 200 && (seq === null || !/^\d+$/.test(seq))) { // 200 without an ack header CANNOT be the marker'd response — the @@ -1504,7 +1525,7 @@ export function apply(ctx: PluginContext): void { // flight, or its ack is lost and the next turn 409s into a resync. return; } - incPendingAcks.delete(url); + incPendingAcks.delete(turnId); if (response.status === 200) { noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system); return; @@ -1694,7 +1715,7 @@ export function _stateBeforeSendForTest(): ((url: string, init: RequestInit | un return state.beforeSend; } -export function _stateOnResponseForTest(): ((url: string, response: Response) => void) | undefined { +export function _stateOnResponseForTest(): ((url: string, response: Response, headers?: Record) => void) | undefined { return state.onResponse; } diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index 166a05367d..92a53b21f4 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -23,6 +23,34 @@ import { createHash } from "node:crypto"; * helper must not import server modules. Keep the two in sync. */ export const INCREMENTAL_TURN_MARKER_HEADER = "x-bili-plugin-turn"; +/** #3329: per-request correlation id stamped on each planned incremental + * turn. The ack channel (native-intercept's onResponse) only carries the URL, + * and several conversations share one provider URL — keying the pending-ack + * table by URL mis-attributes a late response to whichever conversation + * planned last on that URL. The id rides the outgoing request and is read + * back from THAT dispatch's own headers when its response lands, so + * attribution is exact per request. It never travels to the proxy for any + * purpose (the lane ignores it) — pure in-process correlation. */ +export const INCREMENTAL_TURN_ID_HEADER = "x-bili-turn-id"; + +let turnIdCounter = 0; + +/** Fresh per-request turn id: time base + process counter + random tail. + * Uniqueness only needs to hold inside this process (the id never crosses a + * wire boundary for correlation purposes). */ +export function newIncrementalTurnId(): string { + return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; +} + +/** #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 + * longer depends on how long an entry survives. */ +export function sweepStale(map: Map, ttlMs: number): void { + const now = Date.now(); + for (const [key, entry] of map) if (now - entry.at >= ttlMs) map.delete(key); +} + /** Same allowlist the lane accepts in `request` (mirror of * REQUEST_PARAM_KEYS in src/incremental-lane.ts — keep in sync). */ const REQUEST_PARAM_KEYS = [ diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index ff951aa59b..4b8020b609 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -68,9 +68,16 @@ export interface NativeInterceptState { * readable without consuming the stream. Native hosts use it to read * x-bili-applied-seq (the ack channel). Always fires with the * HOST-REQUESTED url (the fetch argument) — never a rerouted or retried - * target: consumers key their in-flight plans by the url they saw in - * beforeSend/the hook. Best-effort: never throws into the request path. */ - onResponse?: (url: string, response: Response) => void; + * target. #3329: the optional third argument carries THIS DISPATCH'S + * outgoing headers (lowercase-name record; init.headers wins over a + * Request-object input's own headers, exactly as fetch itself resolves + * them) — consumers correlate a response back to THE REQUEST that + * produced it via their per-request correlation id stamped on send, + * because several conversations share one provider URL and keying by + * url alone mis-attributes a late response to whichever conversation + * happened to plan last on that URL. Undefined for a dispatch with no + * readable headers. Best-effort: never throws into the request path. */ + onResponse?: (url: string, response: Response, headers?: Record) => void; /** #1117: attribution gate — called per model-API request with the * (pre-rewrite) target URL. Returning false means the caller is NOT the * host itself (e.g. a third-party in-process plugin riding the host's @@ -302,6 +309,23 @@ function withHeaders(input: string | URL | Request, init: RequestInit | undefine return { input, init }; } +/** #3329: the outgoing headers of a dispatch exactly as fetch resolves them — + * init.headers wins over a Request-object input's own headers (spec: init + * overrides), normalized to a lowercase-name record so response-time + * consumers can look up their per-request correlation id without re-deriving + * the merge rules. undefined when no headers exist or none can be read. */ +function outgoingHeadersOf(input: string | URL | Request, init?: RequestInit): Record | undefined { + try { + const src = init?.headers !== undefined ? init.headers : input instanceof Request ? input.headers : undefined; + if (src === undefined) return undefined; + const out: Record = {}; + for (const [k, v] of new Headers(src)) out[k.toLowerCase()] = v; + return out; + } catch { + return undefined; + } +} + /** #2592/#2709: client-side mirror of the passthrough lane's cheap ASCII * prefilter — true when the dispatch body contains DSH_COMPACTION_MARKER_SNIFF * (the exact bytes the server sniffs; imported, not re-derived). A hit is NOT @@ -717,7 +741,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea state.onDispatch?.(target, target === url ? "self" : "retry"); const resp = await send(stamped.input, stamped.init); try { - state.onResponse?.(url, resp); + state.onResponse?.(url, resp, outgoingHeadersOf(stamped.input, stamped.init)); } catch { // observability hook only — never break the response } @@ -757,7 +781,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea state.onDispatch?.(retried, "retry"); const resp = await send(restamped.input, restamped.init); try { - state.onResponse?.(url, resp); + state.onResponse?.(url, resp, outgoingHeadersOf(restamped.input, restamped.init)); } catch { // observability hook only — never break the response } @@ -914,7 +938,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea const stamped = withHeaders(first, init, resignExtra !== undefined ? { ...(state.headersFor?.(url) ?? {}), ...resignExtra } : state.headersFor?.(url)); const resp = await send(stamped.input, stamped.init); try { - state.onResponse?.(url, resp); + state.onResponse?.(url, resp, outgoingHeadersOf(stamped.input, stamped.init)); } catch { // observability hook only — never break the response } @@ -935,7 +959,7 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea const stamped = withHeaders(retried, init, resignExtra !== undefined ? { ...(state.headersFor?.(url) ?? {}), ...resignExtra } : state.headersFor?.(url)); const resp = await send(stamped.input, stamped.init); try { - state.onResponse?.(url, resp); + state.onResponse?.(url, resp, outgoingHeadersOf(stamped.input, stamped.init)); } catch { // observability hook only — never break the response } diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 4c445e98ac..2b959fc47d 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 { INCREMENTAL_TURN_MARKER_HEADER, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, clearIncrementalProbe, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.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 { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; @@ -73,24 +73,35 @@ const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve( // `prompt_cache_key` body field the omp extension stamps (pi.ts // stampPromptCacheKey — present exactly when the plugin is active). The // beforeSend hook reads it as the conversation id, plans the incremental -// body, swaps init.body and stamps the plugin headers DIRECTLY ON init -// (per-request, so concurrent sessions on one provider URL cannot cross -// attributions) — plugin mode is safe here: x-bili-plugin-conversation gets -// the same value wire mode already resolves to via -// preferPromptCacheKeyIdentity, so the session id does not fork. One planned -// in-flight request per URL (onResponse acks are URL-keyed); a second -// concurrent request on the same URL falls back to full resend. +// body, swaps init.body and stamps the plugin headers DIRECTLY ON init — +// plugin mode is safe here: x-bili-plugin-conversation gets the same value +// wire mode already resolves to via preferPromptCacheKeyIdentity, so the +// session id does not fork. #3329: each planned turn is keyed by a fresh +// per-request turn id stamped onto init's headers (x-bili-turn-id) — +// several sessions share one provider URL, and keying the pending table by +// URL mis-attributes a late response to whichever session planned last on +// that URL. The #3378 single-flight guard per URL is KEPT on top of turn-id +// keying (#3340 rebase decision): while a marker'd request for this URL is +// still awaiting its ack, later requests on the same URL ride the normal +// pipeline — attribution answers "which ack consumes which pending", the +// guard answers "no second conversion on this URL until the first is acked +// or ages out"; dropping the guard is a separate follow-up. interface IncrementalPendingTurn { sid: string; + url: string; messages: IncrementalWireMessage[]; system: unknown; base: string; at: number; } +/** Keyed by per-request turn id (#3329); each entry remembers the URL it + * was planned on so the #3378 single-flight guard can scan for live + * same-URL entries. */ const incPendingOmp = new Map(); /** Planned turns age out (onResponse only fires on successful sends — a - * network-dead request must not block the URL forever). Mirrors opencode. */ + * network-dead request must not pin its slot forever). Memory hygiene only + * (#3329): attribution is exact per turn id. Mirrors opencode. */ const INC_PENDING_TTL_MS = 30_000; /** Test hook: drop all planned turns (module state across suites). */ @@ -101,9 +112,7 @@ export function __resetOmpIncrementalForTests(): void { export async function ompIncrementalBeforeSend(url: string, init: RequestInit | undefined): Promise { try { if (init === undefined || typeof init.body !== "string") { return; } - const stale = incPendingOmp.get(url); - if (stale !== undefined && Date.now() - stale.at < INC_PENDING_TTL_MS) { return; } - incPendingOmp.delete(url); + sweepStale(incPendingOmp, INC_PENDING_TTL_MS); let parsed: unknown; try { parsed = JSON.parse(init.body); @@ -120,13 +129,26 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | if (!(await probeIncrementalTurns(base))) { return; } const plan = planIncrementalTurn({ conversationId: sid, path: "/v1/messages", messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, request: b as { model?: unknown } & Record }); if (plan.mode === "skip") { return; } + // #3378 single-flight (kept over #3329's removal per the rebase + // decision): while a marker'd request for this URL is still awaiting + // its ack, later requests ride the normal pipeline. Pending entries + // are keyed by turn id (#3329), so the guard scans for a live entry + // stashed on THIS url (sweepStale above already aged out the stale + // ones — an aged entry must not block a fresh conversion). + let inflight = false; + for (const pending of incPendingOmp.values()) { + if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; } + } + if (inflight) { return; } 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(url, { sid, messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, base, at: Date.now() }); + incPendingOmp.set(turnId, { sid, url, messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, base, at: Date.now() }); } catch (e) { // never break the host's request on adapter failure } @@ -134,9 +156,11 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | state.beforeSend = ompIncrementalBeforeSend; -export function ompIncrementalOnResponse(url: string, response: Response): void { - const pending = incPendingOmp.get(url); - if (pending === undefined) return; +export function ompIncrementalOnResponse(_url: string, response: Response, headers?: Record): void { + const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; + if (turnId === undefined) return; // not an incremental dispatch — nothing to attribute (#3329) + const pending = incPendingOmp.get(turnId); + if (pending === undefined) return; // unknown/stale turn — nothing to attribute const seq = response.headers.get("x-bili-applied-seq"); if (response.status === 200 && (seq === null || !Number.isFinite(Number(seq)))) { // 200 without an ack header CANNOT be the marker'd response — the @@ -146,7 +170,7 @@ export function ompIncrementalOnResponse(url: string, response: Response): void // keep the pending entry for the marker'd response still in flight. return; } - incPendingOmp.delete(url); + incPendingOmp.delete(turnId); try { if (response.status === 200) { noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system); diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 46164f0637..62818d15ee 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 { INCREMENTAL_TURN_MARKER_HEADER, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, clearIncrementalProbe, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.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 { 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"; @@ -832,10 +832,17 @@ export default { id: "billion-context-opencode-native", setup: createOpencodeV2S * http.request hook (routing + header stamping already happened, so the * stamped plugin headers ride the SAME request object we replace). The * conversation id is opencode's own session id (x-bili-plugin-conversation - * was just stamped with it — identity upgrades, never forks). One planned - * in-flight request per URL: concurrent sessions on the same provider fall - * back to full resend (ack attribution must stay unambiguous — onResponse - * is keyed by URL). Exported for the native suites; wired above. */ + * was just stamped with it — identity upgrades, never forks). #3329: each + * planned turn is keyed by a fresh per-request turn id stamped onto the + * outgoing request (x-bili-turn-id) — several sessions share one provider + * URL, and keying by URL mis-attributes a late response to whichever + * session planned last on that URL. The #3378 single-flight guard per URL + * is KEPT on top of turn-id keying (#3340 rebase decision): while a + * marker'd request for this URL is still awaiting its ack, later requests + * on the same URL ride the normal pipeline — attribution answers "which + * ack consumes which pending", the guard answers "no second conversion on + * this URL until the first is acked or ages out"; dropping the guard is a + * separate follow-up. Exported for the native suites; wired above. */ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State): Promise { try { const sid = typeof e.sessionID === "string" ? e.sessionID : ""; @@ -843,9 +850,7 @@ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State const req = e.request as Request | undefined; if (!sid || !base || req === undefined || typeof req.clone !== "function") return; const url = req.url; - const stale = incPendingOpencode.get(url); - if (stale !== undefined && Date.now() - stale.at < INC_PENDING_TTL_MS) return; // single in-flight per URL - incPendingOpencode.delete(url); + sweepStale(incPendingOpencode, INC_PENDING_TTL_MS); const body: unknown = await req.clone().json().catch(() => null); if (body === null || typeof body !== "object") return; const b = body as { messages?: unknown; model?: unknown; system?: unknown }; @@ -853,15 +858,31 @@ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State if (!(await probeIncrementalTurns(base))) return; const plan = planIncrementalTurn({ conversationId: sid, path: "/v1/messages", messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, request: b as { model?: unknown } & Record }); if (plan.mode === "skip") return; - replaceRequestPayload(e, plan.body, { [INCREMENTAL_TURN_MARKER_HEADER]: "1" }); - incPendingOpencode.set(url, { sid, messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, base, at: Date.now() }); + // #3378 single-flight (kept over #3329's removal per the rebase + // decision): while a marker'd request for this URL is still awaiting + // its ack, later requests ride the normal pipeline. Pending entries + // are keyed by turn id (#3329), so the guard scans for a live entry + // stashed on THIS url (sweepStale above already aged out the stale + // ones — an aged entry must not block a fresh conversion). + let inflight = false; + for (const pending of incPendingOpencode.values()) { + if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; } + } + if (inflight) return; + const turnId = newIncrementalTurnId(); + 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 { // never break the host's request on adapter failure } } interface IncrementalPendingTurn { + /** Conversation (opencode session id) this planned turn belongs to. */ sid: string; + /** Model-endpoint URL this turn was planned on — carried for the #3378 + * single-flight scan; attribution itself is by turn id (#3329). */ + url: string; messages: IncrementalWireMessage[]; system: unknown; base: string; @@ -869,11 +890,14 @@ interface IncrementalPendingTurn { } /** Planned turns age out: onResponse only fires on a SUCCESSFUL send, so a - * request that died network-level would otherwise block incremental for its - * URL forever (single in-flight guard). 30s covers any sane model round-trip - * while keeping concurrent-request attribution unambiguous. */ + * request that died network-level would otherwise pin its slot forever. + * Memory hygiene only (#3329) — attribution is exact per turn id, so a + * lingering entry can no longer mis-attribute another request's ack. */ const INC_PENDING_TTL_MS = 30_000; +/** Keyed by per-request turn id (#3329); each entry remembers the URL it + * was planned on so the #3378 single-flight guard can scan for live + * same-URL entries — see maybeIncrementalOpencode. */ const incPendingOpencode = new Map(); /** Test hook: drop all planned turns (the map is module state across suites). */ @@ -882,11 +906,17 @@ export function __resetOpencodeIncrementalForTests(): void { } /** Ack collector registered on the module's NativeInterceptState: the - * model-API and routed branches both fire onResponse(url, resp) after a - * successful send. Exported for the native suites; wired below. */ -export function opencodeIncrementalOnResponse(url: string, response: Response): void { - const pending = incPendingOpencode.get(url); - if (pending === undefined) return; // non-incremental responses just clear nothing + * model-API and routed branches both fire onResponse(url, resp, headers) + * after a successful send. #3329: attribution is by the per-request turn id + * read back from THIS dispatch's outgoing headers (third argument) — the + * url is only ever shared context, never the key. A response without a + * turn-id header is not an incremental ack at all. Exported for the native + * suites; wired below. */ +export function opencodeIncrementalOnResponse(_url: string, response: Response, headers?: Record): void { + const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; + if (turnId === undefined) return; // not an incremental dispatch — nothing to attribute (#3329) + const pending = incPendingOpencode.get(turnId); + if (pending === undefined) return; // unknown/stale turn — nothing to attribute const seq = response.headers.get("x-bili-applied-seq"); if (response.status === 200 && (seq === null || !Number.isFinite(Number(seq)))) { // 200 without an ack header CANNOT be the marker'd response — the @@ -896,7 +926,7 @@ export function opencodeIncrementalOnResponse(url: string, response: Response): // keep the pending entry for the marker'd response still in flight. return; } - incPendingOpencode.delete(url); + incPendingOpencode.delete(turnId); try { if (response.status === 200) { noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system); diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 8a8430cd75..293b6af152 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -41,6 +41,8 @@ test("#3370: dsh incremental conversion is atomic (marker + body on the same ini assert.equal(body1.conversationId, "dsh-inc-1", "seed body swapped in"); assert.equal(body1.resync, true); assert.equal(new Headers(init1.headers).get("x-bili-plugin-turn"), "1", "#3370: marker stamped on the same init as the body"); + const turnId1 = new Headers(init1.headers).get("x-bili-turn-id"); + assert.ok(turnId1, "#3329: per-request turn id stamped alongside the marker"); const extra = headersFor(url) ?? {}; assert.equal(extra["x-bili-plugin-turn"], undefined, "headersFor no longer stamps/consumes the marker"); assert.equal(extra["x-bili-plugin"], "dsh", "plugin identity headers still stamped"); @@ -57,11 +59,18 @@ test("#3370: dsh incremental conversion is atomic (marker + body on the same ini // 2b. that skipped request's plain 200 (no ack header) arrives // BEFORE the marker'd response — it must not consume the // pending entry, or the seed's ack below is lost and turn 3 - // replans a resync instead of a delta (#3378 review). + // replans a resync instead of a delta (#3378 review). Under + // turn-id keying (#3329) the 2-arg call carries no outgoing + // headers at all — a response with no turn-id header is not + // an incremental ack and consumes nothing, which is exactly + // the no-op this step pins. onResponse(url, new Response("{}", { status: 200 })); - // 3. ack the seed → the next turn plans a DELTA (sinceSeq advanced) - onResponse(url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(msgs1.length) } })); + // 3. ack the seed → the next turn plans a DELTA (sinceSeq advanced). + // #3329: the ack is attributed via the seed's OWN turn id, read + // back from its outgoing headers (what native-intercept threads + // into onResponse in production). + onResponse(url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(msgs1.length) } }), { "x-bili-turn-id": turnId1 }); const init3: RequestInit = { method: "POST", body: original2 }; await beforeSend(url, init3); const body3 = JSON.parse(init3.body as string) as { sinceSeq?: number; messages?: unknown[]; resync?: boolean }; diff --git a/tests/native-intercept.test.ts b/tests/native-intercept.test.ts index 511ff6090e..4ec5be2c08 100644 --- a/tests/native-intercept.test.ts +++ b/tests/native-intercept.test.ts @@ -408,6 +408,44 @@ test("install: TypeError triggers one respawn + retry", async () => { } }); +test("install: onResponse carries THIS dispatch's outgoing headers, per request (#3329)", async () => { + const seen: Array<{ url: string; headers?: Record }> = []; + let n = 0; + const state: NativeInterceptState = { + origin: "http://127.0.0.1:40001", + ready: Promise.resolve("http://127.0.0.1:40001"), + beforeSend: async (_url, init) => { + // Stamp a per-request correlation id, exactly like the + // incremental adapters do. + n += 1; + if (init !== undefined) { + const h = new Headers(init.headers); + h.set("x-bili-turn-id", `t-${n}`); + init.headers = h; + } + }, + onResponse: (url, _resp, headers) => { seen.push({ url, headers }); }, + }; + await withPatchRecording(state, async (fetch) => { + // Two dispatches on the SAME provider URL (string + init shape — the + // dsh/omp lanes): each must get back its OWN stamped headers. + await fetch("http://127.0.0.1:8199/v1/messages", { method: "POST", headers: { "content-type": "application/json" }, body: "{}" }); + await fetch("http://127.0.0.1:8199/v1/messages", { method: "POST", headers: { "content-type": "application/json", "x-extra": "second" }, body: "{}" }); + // Request-object shape (the opencode lane): the host stamps the + // correlation id onto the Request itself before fetch. + const req = new Request("http://127.0.0.1:8199/v1/messages", { method: "POST", headers: { "x-req": "r", "x-bili-turn-id": "t-pre" }, body: "{}" }); + await fetch(req); + }); + assert.equal(seen.length, 3, "onResponse fired once per dispatch"); + for (const s of seen) assert.equal(s.url, "http://127.0.0.1:8199/v1/messages", "host-requested url stays the key context"); + assert.equal(seen[0].headers?.["x-bili-turn-id"], "t-1"); + assert.equal(seen[0].headers?.["content-type"], "application/json", "original outgoing headers ride along"); + assert.equal(seen[1].headers?.["x-bili-turn-id"], "t-2", "the second same-URL response carries the second dispatch's id, not the first's"); + assert.equal(seen[1].headers?.["x-extra"], "second"); + assert.equal(seen[2].headers?.["x-bili-turn-id"], "t-pre", "Request-object inputs carry their own pre-stamped id through the rewrite"); + assert.equal(seen[2].headers?.["x-req"], "r"); +}); + test("install: second install is a no-op while active", async () => { const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; const saved = globalThis.fetch; diff --git a/tests/omp-native-incremental.test.ts b/tests/omp-native-incremental.test.ts index 1a5fa8d4dd..fb39afa649 100644 --- a/tests/omp-native-incremental.test.ts +++ b/tests/omp-native-incremental.test.ts @@ -2,7 +2,10 @@ // events, so the ONLY identity signal at the fetch layer is prompt_cache_key // (stamped into the body by the omp extension). These suites drive the two // exported seams (ompIncrementalBeforeSend / ompIncrementalOnResponse) with a -// fake lane origin pinned via _setOmpOriginForTest. +// fake lane origin pinned via _setOmpOriginForTest. #3329: ack calls pass the +// OUTGOING headers of the very request being acknowledged (the init.headers +// the plan stamped, exactly as native-intercept threads them into onResponse) +// — attribution is by the per-request turn id, never by URL. import { after, test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:http"; @@ -33,6 +36,14 @@ function initOf(body: unknown): RequestInit { return { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }; } +/** The outgoing headers of a planned init, normalized exactly like + * native-intercept's outgoingHeadersOf does (init.headers wins). */ +function headersOf(h: Headers): Record { + const out: Record = {}; + for (const [k, v] of h) out[k.toLowerCase()] = v; + return out; +} + const lanes: { close: () => Promise }[] = []; after(async () => { for (const l of lanes) await l.close(); @@ -59,8 +70,9 @@ test("omp incremental: prompt_cache_key becomes the conversation id; plugin head assert.equal(headers.get("x-bili-plugin"), "omp"); assert.equal(headers.get("x-bili-plugin-conversation"), "omp-inc-1"); assert.equal(headers.get("x-bili-plugin-turn"), "1"); + assert.notEqual(headers.get("x-bili-turn-id"), null, "turn id stamped on the outgoing init (#3329)"); - ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } })); + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(headers)); const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "next" }]; const init2 = initOf({ model: "claude-test", max_tokens: 512, prompt_cache_key: "omp-inc-1", messages: msgs2 }); @@ -94,3 +106,87 @@ test("omp incremental: probe disabled leaves the body untouched", async () => { await ompIncrementalBeforeSend(MODEL_URL, init); assert.deepEqual(JSON.parse(String(init.body)), original); }); + +test("omp incremental: late response crossing the TTL attributes by turn id, never by URL (#3329)", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(true); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + // Two conversations sharing ONE provider URL and the SAME host system + // prompt — the shape #3329 reports in the field. Sequenced for the #3378 + // single-flight guard (kept over #3329's removal per the #3340 rebase + // decision): while a marker'd request for this URL is un-acked, a second + // conversion on the same URL rides full resend — so B's post-ack turn + // can only be exercised after B's OWN ack, and A's reseed after B's + // pending is consumed. Every original assertion intent survives the + // reordering (documented inline). + const system = "be terse"; + const a1msgs = [{ role: "user", content: "a1" }]; + const b1msgs = [{ role: "user", content: "b1" }]; + + const realNow = Date.now; + let t = realNow(); + Date.now = () => t; + try { + const initA1 = initOf({ model: "claude-test", max_tokens: 512, system, prompt_cache_key: "omp-late-a", messages: a1msgs }); + await ompIncrementalBeforeSend(MODEL_URL, initA1); + const aHeaders = headersOf(initA1.headers as Headers); + assert.notEqual(aHeaders["x-bili-turn-id"], undefined, "turn id stamped on A's outgoing init"); + + // A's round-trip outlives the pending TTL (slow non-streaming request) + // — the sweep frees the URL slot, so B converts under the guard. + t += 31_000; + const initB1 = initOf({ model: "claude-test", max_tokens: 512, system, prompt_cache_key: "omp-late-b", messages: b1msgs }); + await ompIncrementalBeforeSend(MODEL_URL, initB1); + const bHeaders = headersOf(initB1.headers as Headers); + assert.notEqual(bHeaders["x-bili-turn-id"], undefined, "turn id stamped on B's outgoing init"); + assert.notEqual(bHeaders["x-bili-turn-id"], aHeaders["x-bili-turn-id"], "each dispatch carries its own correlation id"); + const b1Body = JSON.parse(String(initB1.body)) as Record; + assert.equal(b1Body.sinceSeq, 0, "B's first turn seeds (not credited with anything yet)"); + assert.equal(b1Body.resync, true, "B's first turn seeds"); + assert.equal(b1Body.system, system, "seed carries B's own system — no taint from A"); + + // A's LATE response lands after B planned on the same URL. The old + // URL-keyed table credited this ack to B; now A's entry was swept and + // the response carries no live turn id — it cannot touch either state + // machine except through its own (expired) id. + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "42" } }), aHeaders); + + // B's OWN response then credits exactly B — and the delta it plans + // rides watermark 7, never 42: if A's late ack had been attributed to + // B (the URL-keyed bug), this sinceSeq would read 42. + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "7" } }), bHeaders); + const b2msgs = [...b1msgs, { role: "assistant", content: "b1r" }, { role: "user", content: "b2" }]; + const initB2 = initOf({ model: "claude-test", max_tokens: 512, system, prompt_cache_key: "omp-late-b", messages: b2msgs }); + await ompIncrementalBeforeSend(MODEL_URL, initB2); + const b2Body = JSON.parse(String(initB2.body)) as Record; + assert.equal(b2Body.sinceSeq, 7, "B was never credited with A's ack (42) — it rides its own (7)"); + assert.equal((b2Body.messages as unknown[]).length, 2, "delta only — B's own watermark carries"); + + // B's chain keeps riding its own acks: ack b2 FIRST — the #3378 + // single-flight guard holds the URL slot until b2's ack lands. + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "9" } }), headersOf(initB2.headers as Headers)); + + // A's expired turn is an honest loss: A reseeds instead of riding a + // watermark it never actually received (b2's pending was consumed by + // its own ack above, so the URL slot is free again). + const a2msgs = [...a1msgs, { role: "assistant", content: "a1r" }, { role: "user", content: "a2" }]; + const initA2 = initOf({ model: "claude-test", max_tokens: 512, system, prompt_cache_key: "omp-late-a", messages: a2msgs }); + await ompIncrementalBeforeSend(MODEL_URL, initA2); + const a2Body = JSON.parse(String(initA2.body)) as Record; + assert.equal(a2Body.sinceSeq, 0, "A's expired entry was swept — honest reseed"); + assert.equal(a2Body.resync, true, "A reseeds wholesale, not on a watermark it never saw"); + + // A's reseed acks on ITS OWN turn id (a1's expired id stays dead); + // B's next delta is then free to convert under the guard. + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "11" } }), headersOf(initA2.headers as Headers)); + const b3msgs = [...b2msgs, { role: "assistant", content: "b2r" }, { role: "user", content: "b3" }]; + const initB3 = initOf({ model: "claude-test", max_tokens: 512, system, prompt_cache_key: "omp-late-b", messages: b3msgs }); + await ompIncrementalBeforeSend(MODEL_URL, initB3); + const b3Body = JSON.parse(String(initB3.body)) as { sinceSeq: number }; + assert.equal(b3Body.sinceSeq, 9, "B rides its own watermark"); + } finally { + Date.now = realNow; + } +}); diff --git a/tests/opencode-incremental.test.ts b/tests/opencode-incremental.test.ts index a3f475a521..c9d20a9112 100644 --- a/tests/opencode-incremental.test.ts +++ b/tests/opencode-incremental.test.ts @@ -2,8 +2,11 @@ // seams directly (maybeIncrementalOpencode / opencodeIncrementalOnResponse) // against a fake lane origin: the probe endpoint decides enablement, the // body/marker assertions pin the divert contract, and the ack→delta pair -// pins the state machine ride-along. The full e2e (marker divert through -// the real server) lives in incremental-lane.test.ts. +// pins the state machine ride-along. #3329: ack calls pass the OUTGOING +// headers of the very request being acknowledged (read back off the swapped +// Request, exactly as native-intercept threads them into onResponse) — +// attribution is by the per-request turn id, never by URL. The full e2e +// (marker divert through the real server) lives in incremental-lane.test.ts. import { after, test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:http"; @@ -45,6 +48,14 @@ function state(base: string): V2State { return { proxyBase: base } as unknown as V2State; } +/** The outgoing headers of a swapped Request, normalized exactly like + * native-intercept's outgoingHeadersOf does for a Request-object input. */ +function headersOf(req: Request): Record { + const out: Record = {}; + for (const [k, v] of req.headers) out[k.toLowerCase()] = v; + return out; +} + const lanes: { close: () => Promise }[] = []; after(async () => { for (const l of lanes) await l.close(); @@ -72,8 +83,9 @@ test("opencode incremental: seed swap (marker + shaped body, headers preserved), assert.equal(swapped.headers.get("x-bili-plugin-turn"), "1", "marker stamped on the replaced request"); assert.equal(swapped.headers.get("authorization"), "Bearer sk-test", "original headers survive the swap"); - // ack advances the helper state machine - opencodeIncrementalOnResponse(url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } })); + // ack advances the helper state machine (attributed by this request's own turn id) + opencodeIncrementalOnResponse(url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(swapped)); + assert.notEqual(headersOf(swapped)["x-bili-turn-id"], undefined, "turn id stamped on the outgoing request"); const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "next" }]; const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: msgs2 }, "oc-inc-1"); @@ -119,14 +131,14 @@ test("opencode incremental: #3369 — one 409 re-seeds the next turn; three cons const msgs1 = [{ role: "user", content: "hello" }]; const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: msgs1 }, "oc-inc-dead"); await maybeIncrementalOpencode(t1.e, s); - opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } })); + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "again" }]; const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: msgs2 }, "oc-inc-dead"); await maybeIncrementalOpencode(t2.e, s); const t2Body = JSON.parse(await (t2.e.request as Request).clone().json().then((j) => JSON.stringify(j))) as { sinceSeq: number }; assert.equal(t2Body.sinceSeq, 1, "delta planned"); // the delta was parsed from the CLONED request — ack with the ORIGINAL t2 url - opencodeIncrementalOnResponse(t2.url, new Response("{}", { status: 409, headers: { "x-bili-server-seq": "1" } })); + opencodeIncrementalOnResponse(t2.url, new Response("{}", { status: 409, headers: { "x-bili-server-seq": "1" } }), headersOf(t2.e.request as Request)); const original = { model: "claude-test", max_tokens: 512, messages: [...msgs2, { role: "user", content: "more" }] }; const t3 = fakeEvent(original, "oc-inc-dead"); await maybeIncrementalOpencode(t3.e, s); @@ -137,11 +149,11 @@ test("opencode incremental: #3369 — one 409 re-seeds the next turn; three cons // the re-seed landed and was acked (circuit cleared) — now trip it for // good: three consecutive 409s with no ack between → sticky fallback. - opencodeIncrementalOnResponse(t3.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(original.messages.length) } })); + opencodeIncrementalOnResponse(t3.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(original.messages.length) } }), headersOf(t3.e.request as Request)); for (let i = 0; i < 3; i++) { const t = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [...original.messages, { role: "user", content: `x${i}` }] }, "oc-inc-dead"); await maybeIncrementalOpencode(t.e, s); - opencodeIncrementalOnResponse(t.url, new Response("{}", { status: 409, headers: { "x-bili-server-seq": "1" } })); + opencodeIncrementalOnResponse(t.url, new Response("{}", { status: 409, headers: { "x-bili-server-seq": "1" } }), headersOf(t.e.request as Request)); } const last = { model: "claude-test", max_tokens: 512, messages: [...original.messages, { role: "user", content: "final" }] }; const t4 = fakeEvent(last, "oc-inc-dead"); @@ -161,11 +173,14 @@ test("opencode incremental: a 200 without an ack header does not steal the pendi await maybeIncrementalOpencode(t1.e, s); // seed — pending entry set for the model URL // a concurrent full-resend request on the same URL answers first WITHOUT - // the ack header — it must not consume the marker'd turn's pending slot + // the ack header — it must not consume the marker'd turn's pending slot. + // #3329: the 2-arg call carries no outgoing headers at all — a response + // with no turn-id header is not an incremental ack and consumes nothing. opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200 })); - // the marker'd turn's own 200 still records its ack - opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } })); + // the marker'd turn's own 200 still records its ack (attributed by its + // own turn id, threaded from its outgoing headers) + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); // ...so the next turn rides the delta instead of re-seeding const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "next" }]; @@ -175,3 +190,87 @@ test("opencode incremental: a 200 without an ack header does not steal the pendi assert.equal(body2.sinceSeq, 1, "ack survived the interleaved headerless 200"); assert.equal((body2.messages as unknown[]).length, 2, "delta rides — no re-seed after the interleaving"); }); + +test("opencode incremental: late response crossing the TTL attributes by turn id, never by URL (#3329)", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + const lane = await fakeLane(true); + lanes.push(lane); + const s = state(lane.base); + // Two conversations sharing ONE provider URL and the SAME host system + // prompt — the shape #3329 reports in the field. Sequenced for the #3378 + // single-flight guard (kept over #3329's removal per the #3340 rebase + // decision): while a marker'd request for this URL is un-acked, a second + // conversion on the same URL rides full resend — so B's post-ack turn + // can only be exercised after B's OWN ack, and A's reseed after B's + // pending is consumed. Every original assertion intent survives the + // reordering (documented inline). + const system = "be terse"; + const a1msgs = [{ role: "user", content: "a1" }]; + const b1msgs = [{ role: "user", content: "b1" }]; + + const realNow = Date.now; + let t = realNow(); + Date.now = () => t; + try { + const a1 = fakeEvent({ model: "claude-test", max_tokens: 512, system, messages: a1msgs }, "oc-late-a"); + await maybeIncrementalOpencode(a1.e, s); + const aHeaders = headersOf(a1.e.request as Request); + assert.notEqual(aHeaders["x-bili-turn-id"], undefined, "turn id stamped on A's outgoing request"); + + // A's round-trip outlives the pending TTL (slow non-streaming request) + // — the sweep frees the URL slot, so B converts under the guard. + t += 31_000; + const b1 = fakeEvent({ model: "claude-test", max_tokens: 512, system, messages: b1msgs }, "oc-late-b"); + await maybeIncrementalOpencode(b1.e, s); + const bHeaders = headersOf(b1.e.request as Request); + assert.notEqual(bHeaders["x-bili-turn-id"], undefined, "turn id stamped on B's outgoing request"); + assert.notEqual(bHeaders["x-bili-turn-id"], aHeaders["x-bili-turn-id"], "each dispatch carries its own correlation id"); + const b1Body = JSON.parse(await (b1.e.request as Request).clone().json().then((j) => JSON.stringify(j))) as Record; + assert.equal(b1Body.sinceSeq, 0, "B's first turn seeds (not credited with anything yet)"); + assert.equal(b1Body.resync, true, "B's first turn seeds"); + assert.equal(b1Body.system, system, "seed carries B's own system — no taint from A"); + + // A's LATE response lands after B planned on the same URL. The old + // URL-keyed table credited this ack to B; now A's entry was swept and + // the response carries no live turn id — it cannot touch either state + // machine except through its own (expired) id. + opencodeIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "42" } }), aHeaders); + + // B's OWN response then credits exactly B — and the delta it plans + // rides watermark 7, never 42: if A's late ack had been attributed to + // B (the URL-keyed bug), this sinceSeq would read 42. + opencodeIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "7" } }), bHeaders); + const b2msgs = [...b1msgs, { role: "assistant", content: "b1r" }, { role: "user", content: "b2" }]; + const b2 = fakeEvent({ model: "claude-test", max_tokens: 512, system, messages: b2msgs }, "oc-late-b"); + await maybeIncrementalOpencode(b2.e, s); + const b2Body = JSON.parse(await (b2.e.request as Request).clone().json().then((j) => JSON.stringify(j))) as Record; + assert.equal(b2Body.sinceSeq, 7, "B was never credited with A's ack (42) — it rides its own (7)"); + assert.equal((b2Body.messages as unknown[]).length, 2, "delta only — B's own watermark carries"); + + // B's chain keeps riding its own acks: ack b2 FIRST — the #3378 + // single-flight guard holds the URL slot until b2's ack lands. + opencodeIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "9" } }), headersOf(b2.e.request as Request)); + + // A's expired turn is an honest loss: A reseeds instead of riding a + // watermark it never actually received (b2's pending was consumed by + // its own ack above, so the URL slot is free again). + const a2msgs = [...a1msgs, { role: "assistant", content: "a1r" }, { role: "user", content: "a2" }]; + const a2 = fakeEvent({ model: "claude-test", max_tokens: 512, system, messages: a2msgs }, "oc-late-a"); + await maybeIncrementalOpencode(a2.e, s); + const a2Body = JSON.parse(await (a2.e.request as Request).clone().json().then((j) => JSON.stringify(j))) as Record; + assert.equal(a2Body.sinceSeq, 0, "A's expired entry was swept — honest reseed"); + assert.equal(a2Body.resync, true, "A reseeds wholesale, not on a watermark it never saw"); + + // A's reseed acks on ITS OWN turn id (a1's expired id stays dead); + // B's next delta is then free to convert under the guard. + opencodeIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "11" } }), headersOf(a2.e.request as Request)); + const b3msgs = [...b2msgs, { role: "assistant", content: "b2r" }, { role: "user", content: "b3" }]; + const b3 = fakeEvent({ model: "claude-test", max_tokens: 512, system, messages: b3msgs }, "oc-late-b"); + await maybeIncrementalOpencode(b3.e, s); + const b3Body = JSON.parse(await (b3.e.request as Request).clone().json().then((j) => JSON.stringify(j))) as { sinceSeq: number }; + assert.equal(b3Body.sinceSeq, 9, "B rides its own watermark"); + } finally { + Date.now = realNow; + } +});