From 9e8bd49bb7ce37ce0dc4178896d7ba49cf9f9c0d Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 22:38:04 +0800 Subject: [PATCH 1/2] fix(agent): run the incremental single-flight guard before planning the turn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A turn that the guard discards had already claimed its msgIds client-side (all-or-nothing, #4491). The burned ids were never released, so the host's retry of the same turn lost the whole msgIds field and the server pinned those rows to content-derived identity — the exact host/server divergence #4834 just fixed, self-inflicted here by the client lanes themselves. Hoist the #3370/#3378 single-flight guard above planIncrementalTurn in both native lanes (dsh, omp). The guard only reads the pending-ack table, so the reorder changes nothing for the guard itself; planning now only happens for turns that will actually be dispatched. Regression test: a guard-blocked delta attempt must not burn its ids — after the seed acks, the retry still pins msgIds (pre-fix: the field is silently dropped). --- src/agent/dsh-native.ts | 24 ++++++++++++--------- src/agent/omp-native.ts | 26 +++++++++++++---------- tests/dsh-msgids.test.ts | 45 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5904fba40e..c77cea4773 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -2415,6 +2415,20 @@ export function apply(ctx: PluginContext): void { if (base === undefined) return; if (!(await probeIncrementalTurns(base))) return; sweepStale(incPendingAcks, INC_PENDING_TTL_MS); + // #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. + // The guard must run BEFORE planIncrementalTurn: planning claims the + // turn's msgIds client-side (#4491), and a plan this guard then + // discards would burn those ids — the host's retry screens against + // the burned claims and silently drops the whole msgIds field. + 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; const sid = attr.sid; // #4491: pin the turn to the host's own message ids — the ids captured // on the llm/stream options are only trustworthy when they line up @@ -2433,16 +2447,6 @@ export function apply(ctx: PluginContext): void { msgIds, }); if (plan.mode === "skip") 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; 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 diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 5bbf786891..3021ba03e3 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -135,6 +135,21 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | try { if (init === undefined || typeof init.body !== "string") { return; } sweepStale(incPendingOmp, INC_PENDING_TTL_MS); + // #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). + // The guard must run BEFORE planIncrementalTurn: planning claims the + // turn's msgIds client-side (#4491), and a plan this guard then + // discards would burn those ids — the host's retry screens against + // the burned claims and silently drops the whole msgIds field. + 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; } let parsed: unknown; try { parsed = JSON.parse(init.body); @@ -151,17 +166,6 @@ 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, msgIds: ompTurnMsgIds(sid, b.messages as IncrementalWireMessage[]) }); 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; } 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 diff --git a/tests/dsh-msgids.test.ts b/tests/dsh-msgids.test.ts index fc603770f1..f9f6915783 100644 --- a/tests/dsh-msgids.test.ts +++ b/tests/dsh-msgids.test.ts @@ -341,3 +341,48 @@ test("ids the server would hard-fail on (duplicate/reserved/control) are dropped } }); }); + +test("a turn blocked by the single-flight guard does not burn its msgIds (#4491/#3370)", async () => { + await withSetup(async (handle) => { + // No ambient initiator — the whole identity (sid + ids) rides the + // llm/stream options, same shape as the sibling cases above. + handle.setInitiator(undefined); + + // Seed: converted and left UN-ACKED, so the next conversion on this + // URL is still inside the guard's single-flight window. + const hostMsgs1 = [ + { id: "u-1", role: "user", content: "hello" }, + { id: "a-1", role: "assistant", content: "hi there" }, + ]; + const turn1 = await dispatchTurn(handle, { sessionId: "sess-guard-burn", messages: hostMsgs1 }, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, + ]); + assert.equal(turn1.marker, "1", "seed converted to the incremental lane"); + + // Turn 2 while the seed is still pending: the guard leaves the + // request on the normal pipeline. The blocked attempt must not + // CLAIM this turn's ids client-side — a discarded plan's ids would + // be unreclaimable (the all-or-nothing screen screens the retry + // against them) and the host's retry would silently lose the whole + // msgIds field, pinning the server to content-derived identity. + const hostMsgs2 = [...hostMsgs1, { id: "u-2", role: "user", content: "and again" }]; + const wire2 = [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, + { role: "user", content: "and again" }, + ]; + const turn2blocked = await dispatchTurn(handle, { sessionId: "sess-guard-burn", messages: hostMsgs2 }, wire2); + assert.equal(turn2blocked.marker, null, "single-flight guard: the delta attempt rides the normal pipeline untouched"); + + // The seed finally acks; the host retries turn 2 — the retry must + // still pin the host ids ("u-2" must not have been burned by the + // blocked attempt). + ackTurn(turn1.init, 2); + const turn2 = await dispatchTurn(handle, { sessionId: "sess-guard-burn", messages: hostMsgs2 }, wire2); + assert.equal(turn2.marker, "1", "retry converts once the guard window closes"); + const body2 = JSON.parse(turn2.init.body as string) as TurnBody; + assert.equal(body2.sinceSeq, 2, "delta against the acked base"); + assert.deepEqual(body2.msgIds, ["u-2"], "the blocked attempt did not burn the delta ids"); + }); +}); From 8457807c0051579a39cb7e64da6f4ab96cff6883 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 22:49:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(agent):=20opencode=20lane=20=E2=80=94?= =?UTF-8?q?=20same=20single-flight-before-planning=20hoist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit opencode-native.ts had the same claim-before-guard order as dsh/omp (planIncrementalTurn with the harvested msgIds at :979, the #3378 single-flight guard below it). A guard-blocked turn burned its harvested ids the same way; hoist the guard above the planning call. alignedMsgIdsForWire() is a pure read of the harvest map, so it is safe to keep inline in the (now post-guard) plan call. Regression test mirrors the dsh one: blocked attempt must not burn the ids; after the seed acks, the retry still pins them (pre-fix: actual undefined). --- src/agent/opencode-native.ts | 8 ++++++-- tests/opencode-msgids.test.ts | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index b723363168..e0a734b95f 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -976,19 +976,23 @@ export async function maybeIncrementalOpencode(e: V2HttpRequestEvent, s: V2State 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 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, msgIds: alignedMsgIdsForWire(sid, b.messages) }); - 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). + // The guard must run BEFORE planIncrementalTurn: planning claims the + // turn's msgIds client-side (#4491), and a plan this guard then + // discards would burn those ids — the host's retry screens against + // the burned claims and silently drops the whole msgIds field. 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 plan = planIncrementalTurn({ conversationId: sid, path: "/v1/messages", messages: b.messages as IncrementalWireMessage[], system: b.system as unknown, request: b as { model?: unknown } & Record, msgIds: alignedMsgIdsForWire(sid, b.messages) }); + if (plan.mode === "skip") 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 diff --git a/tests/opencode-msgids.test.ts b/tests/opencode-msgids.test.ts index 984a460b98..ae4b8d455e 100644 --- a/tests/opencode-msgids.test.ts +++ b/tests/opencode-msgids.test.ts @@ -201,3 +201,41 @@ test("opencode msgids: content drift between harvest and wire degrades instead o const body2 = await plannedBody(t2.e); assert.deepEqual(body2.msgIds, ["msg_c1"], "cache_control markers do not break the 1:1 proof"); }); + +test("opencode msgids: a single-flight-blocked turn does not burn its ids (#4491/#3378)", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const hooks = createV1ServerHooks(() => lane.base, {}, {}); + + const sid = "ses_guard_burn"; + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [hostMessage("msg_u1", "user", "hello", sid)] }); + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [wmsg("user", "hello")] }, sid); + await maybeIncrementalOpencode(t1.e, s); + assert.equal((t1.e.request as Request).headers.get("x-bili-plugin-turn"), "1", "seed converted, left UN-acked"); + + // While the seed is still pending, the next turn hits the single-flight + // guard: the request must ride the normal pipeline untouched — and the + // blocked attempt must NOT claim this turn's ids. + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [ + hostMessage("msg_u1", "user", "hello", sid), + hostMessage("msg_a1", "assistant", "hi there", sid), + hostMessage("msg_u2", "user", "next", sid), + ] }); + const wire2 = { model: "claude-test", max_tokens: 512, system: "be terse", messages: [wmsg("user", "hello"), wmsg("assistant", "hi there"), wmsg("user", "next")] }; + const t2b = fakeEvent(wire2, sid); + await maybeIncrementalOpencode(t2b.e, s); + assert.equal((t2b.e.request as Request).headers.get("x-bili-plugin-turn"), null, "guard: blocked attempt rides the normal pipeline"); + assert.deepEqual(await plannedBody(t2b.e), JSON.parse(JSON.stringify(wire2)), "body untouched by the blocked attempt"); + + // The seed acks; the host retries the same turn — the delta ids must + // still ride (a burned claim would silently drop the whole field). + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + const t2 = fakeEvent(wire2, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.deepEqual(body2.msgIds, ["msg_a1", "msg_u2"], "blocked attempt did not burn the delta ids"); +});