Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2420,6 +2420,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
Expand All @@ -2438,16 +2452,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
Expand Down
26 changes: 15 additions & 11 deletions src/agent/omp-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<string, unknown>, 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
Expand Down
8 changes: 6 additions & 2 deletions src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>, 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<string, unknown>, 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
Expand Down
45 changes: 45 additions & 0 deletions tests/dsh-msgids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
38 changes: 38 additions & 0 deletions tests/opencode-msgids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Loading