From a423fb6a2eb190420ab9610715e11105542c40d9 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 13 Nov 2026 23:32:45 +0800 Subject: [PATCH] opencode V2: skip the hoisted leading system run in the msgId header (#6525/#6539) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The server pins host ids against the POST-HOIST core count (prepare-openai validates ids.length against openaiToCore(parsed).length, which hoists the contiguous leading system/developer run out of the fold space). opencode V2's x-bili-msg-ids header was stamped per WIRE message, INCLUDING the leading system, so its count always exceeded the core count on the openai wire and the pin was rejected (a safe content-derived degrade — but opencode V2 never actually pinned on the openai wire). Core-align the V2 extractor: skip the contiguous leading system/developer run before collecting ids so the header count equals the post-hoist core count. No-op on the anthropic wire (system is a separate field, never in messages); mid-conversation system messages keep their ids; a leading-system-only turn degrades (no empty header). Whole-turn poison contract unchanged. --- src/agent/opencode-v2-msgids.ts | 36 +++++++++++++++++++++---- tests/opencode-v2-msgids.test.ts | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/agent/opencode-v2-msgids.ts b/src/agent/opencode-v2-msgids.ts index db6d34ce3a..ea01f0e6ac 100644 --- a/src/agent/opencode-v2-msgids.ts +++ b/src/agent/opencode-v2-msgids.ts @@ -14,8 +14,10 @@ // `id` OPTIONAL (ai/src/schema/messages.ts:240-270), so one missing or // foreign-shaped id poisons the whole turn → no header → the session rides // exactly as before. The server re-proves everything anyway (exact count -// parity vs the wire body + canonical validation + role spot-checks); -// client-side we just keep the obvious poison classes out. +// parity vs the POST-HOIST core count + canonical validation); client-side we +// core-align — skip the leading system/developer run the kernel hoists out of +// the fold space (see opencodeV2MsgIdList) — and keep the obvious poison +// classes out. /** Hard ceiling on ids per turn — real sessions are hundreds of messages, not * thousands of KB of header; beyond this the turn degrades. */ @@ -33,17 +35,41 @@ const HOST_MSG_ID = /^msg_[A-Za-z0-9]+$/; type OpencodeV2MessageLike = { id?: unknown; role?: unknown } | null | undefined; -/** Extract the full ordered host-id list from a V2 turn's message array, or - * undefined when ANY entry cannot be proven usable (whole-turn degrade). */ +/** Extract the CORE-ALIGNED ordered host-id list from a V2 turn's message + * array, or undefined when ANY entry cannot be proven usable (whole-turn + * degrade). + * + * #6525/#6539 core alignment: the openai wire hoists the contiguous leading + * system/developer run OUT of the fold space (kernel openaiToCore), so the + * server pins against the POST-HOIST core count — not the raw wire-message + * count. If the header carried the leading system ids, its count would always + * exceed the core count and the pin would be rejected (a safe content-derived + * degrade, but opencode V2 would never actually pin on the openai wire). We + * therefore skip the contiguous leading system/developer run so the header + * count equals the post-hoist core count. The anthropic wire keeps its system + * as a separate field (never inside messages), so this skip is a no-op there. + * A mid-conversation system message is NOT part of the leading run and keeps + * its id. */ export function opencodeV2MsgIdList(messages: readonly OpencodeV2MessageLike[] | null | undefined): string[] | undefined { if (!Array.isArray(messages) || messages.length === 0) return undefined; if (messages.length > MAX_IDS_PER_TURN) return undefined; + // Skip the contiguous leading system/developer run (matches the kernel's + // openaiToCore hoist) so the id list aligns to the post-hoist core count. + let start = 0; + while (start < messages.length) { + const m = messages[start]; + const role = m !== null && typeof m === "object" ? (m as { role?: unknown }).role : undefined; + if (role === "system" || role === "developer") start += 1; + else break; + } const ids: string[] = []; - for (const m of messages) { + for (let i = start; i < messages.length; i++) { + const m = messages[i]; const id = m !== null && typeof m === "object" ? (m as { id?: unknown }).id : undefined; if (typeof id !== "string" || !HOST_MSG_ID.test(id)) return undefined; // poison → no stamp ids.push(id); } + if (ids.length === 0) return undefined; // only a leading system run → nothing to pin return ids; } diff --git a/tests/opencode-v2-msgids.test.ts b/tests/opencode-v2-msgids.test.ts index ecd6bf8f04..5974f3898d 100644 --- a/tests/opencode-v2-msgids.test.ts +++ b/tests/opencode-v2-msgids.test.ts @@ -34,6 +34,52 @@ test("v2 extraction: non-message-array inputs degrade", () => { assert.equal(opencodeV2MsgIdList(["not-an-object" as unknown as { id?: string; role?: string }]), undefined); }); +// #6525/#6539 core alignment: the header must align to the POST-HOIST core +// count the server pins against, so the contiguous leading system/developer +// run (which openaiToCore hoists out of the fold space) is excluded. +const lead = (roles: string[], id = (i: number) => `msg_lead${i}`) => + roles.map((role, i) => ({ id: id(i), role })); + +test("v2 core-align: leading system run is skipped (openai wire case)", () => { + // [system, user, assistant] → only [user, assistant] ids ride. + assert.deepEqual( + opencodeV2MsgIdList(lead(["system", "user", "assistant"])), + ["msg_lead1", "msg_lead2"], + ); + // multiple leading systems + developer all hoist out. + assert.deepEqual( + opencodeV2MsgIdList(lead(["system", "system", "developer", "user", "assistant"])), + ["msg_lead3", "msg_lead4"], + ); +}); + +test("v2 core-align: no leading system is a no-op (anthropic wire case)", () => { + // anthropic keeps system as a separate field, never inside messages → all ride. + assert.deepEqual( + opencodeV2MsgIdList(lead(["user", "assistant", "user"])), + ["msg_lead0", "msg_lead1", "msg_lead2"], + ); +}); + +test("v2 core-align: mid-conversation system keeps its id", () => { + // A system message that is NOT part of the leading run is a real core. + assert.deepEqual( + opencodeV2MsgIdList(lead(["user", "system", "assistant"])), + ["msg_lead0", "msg_lead1", "msg_lead2"], + ); +}); + +test("v2 core-align: only a leading system run → nothing to pin", () => { + assert.equal(opencodeV2MsgIdList(lead(["system"])), undefined); + assert.equal(opencodeV2MsgIdList(lead(["system", "developer"])), undefined); + assert.equal(stampedMsgIdsHeaderFromMessages(lead(["system"])), undefined); +}); + +test("v2 core-align: a poison entry after the leading run still kills the stamp", () => { + // system is skipped, but the missing id on the user message poisons. + assert.equal(opencodeV2MsgIdList([{ id: "msg_s", role: "system" }, { role: "user" }]), undefined); +}); + test("v2 stamp budget: >4096 ids or >16KiB joined degrades without riding", () => { const big = Array.from({ length: 4097 }, (_, i) => ({ id: `msg_x${i}`, role: "user" })); assert.equal(opencodeV2MsgIdList(big), undefined, "per-turn id cap");