diff --git a/CHANGELOG.md b/CHANGELOG.md index 32cdf67e7f..d114d3eaad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag - **msgid 钉进身份层**:插件增量轮体带 `msgIds` 数组(与 messages 平行)——客户端消息 id 直接成为内核/存储身份(m.id、store msg_id、折叠覆盖锚),不再走内容派生 h_ 哈希。校验严格(非法条目/重复/`acp_summary_` 保留前缀/空白控制字符→整轮降级回内容派生;非 resync 轮重复认领已存在 id→400 拒绝,身份投毒绝不入库);装配体经管线转发前剥离 `biliMsgIds` 字段(上游零感知)。对账=纯函数 `alignIdSet`(msg消失=compressed(活跃块覆盖或压缩签名佐证)/deleted;重叠率<0.5 且双侧≥8 条=世代翻转;edits=同 id 异签名)。 - **渲染端点**:POST `__bili/plugin/render` {conversationId, untilSeq?}(rollout `pluginLaneServerHistory` 门控,缺省 0%)——服务端正史切片出站前缀,折叠覆盖渲染为 acp_summary 锚,degrade-never-guess(任何失步非 2xx,宿主回退发本地原文);探针 `GET /__bili/plugin/turn` 新增 `serverHistory` 字段。 +- **四宿主 msgIds 接线**(dsh/pi/opencode/omp,`#4491` 宿主侧):增量轮体从宿主拿到自己的稳定消息 id 并随 `msgIds` 上车——dsh=`llm/stream` options.messages[].id(挂进既有 #4082 ALS 作用域,发送时 1:1 对齐);pi=扩展层 CoreMessage.id(`pi-msgids.ts` 模块,`validateMsgIds` 直用服务端校验);opencode=V1 `messages.transform` info.id(V2 http.request 只见 wire,降级为内容派生);omp=`sessionManager.getBranch()` 树条目 id(before_provider_request 快照 hint+count/role 对齐)。全或无纪律贯穿:任一消息拿不到合格 id(缺失/重复/保留前缀/控制字符/数量不齐)整轮降级不带字段,宁可降级绝不错配;omp 另持 msgidClaims 本地镜像防重试撞 400。桩:`tests/{pi,dsh,opencode,omp}-msgids.test.ts`(4+5+5+6 例)。 - **车道流 e2e 桩**(`tests/msgid-lane-flow.test.ts` 5 例):钉 id 落行+编辑=追加新行(绝不换回旧 seq,409-loop 契约)/undo-redo 同内容=身份 join 原行复活零新行/delta 重复认领=400/divergent resync 后无 409 循环/无 msgIds 轮=字节等价对照组(含 rollout 门开时管线重钉、门关时仅内存 canonical 钉定的两态验证)。 - **seq ledger foundations — block interval bounds + ledger-numbered refs (#4316 layer 3, phase 1).** Block coverage now carries its ledger interval: `planIngest` fills `blocks.start_seq/end_seq` with the min/max unified-store seq of the covered messages (`-1/-1` when no rows resolve) — coverage bounds are id-independent, so client-side byte churn can never invalidate them. `CoreMessage.ledgerSeq` sidecar: store-served views (snapshot-render prefix rows) stamp the row's `(session, seq)`; `assignRefs` issues the ref as the direct display of that number (seq+1 in the 1-based display space), advancing the allocation cursor past the stamp so counter-issued refs never collide with future stamps. A store-served message's number is assigned once at ingest and is structurally immune to the #4316 churn family (model-switch re-stamps, image re-encodes re-minting every content-hash id). Stakes first in `tests/seq-ledger.test.ts`: interval bounds (S1), ref-set stability across a full id re-mint under snapshot render (S2), and the kernel seam (S3). Follow-ups: incremental-lane stamping, fork `cloneRows` number-preserving copy, and the one-time legacy re-mint window handling — tracked in #4316. diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index a42d811fe3..c7a5b189bf 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -1146,19 +1146,57 @@ type GateAttribution = // id that cannot be lost to chain shape: agentless side-calls (no initiator // but a sessionId), disposed initiator scopes (currentInitiator throws), or // host runtime gaps that reroute the ambient context. -const sidScope = new AsyncLocalStorage(); +// #4491: the same options also carry the host session message ids +// (`options.messages[].id` — dsh stamps a stable UUID on every message), +// so the scope payload grew from a bare sid into { sid, msgIds } and the +// incremental conversion can pin the turn's messages to the host's own +// identity (server-side canonical rows keyed by msgIds instead of +// content-derived hashes). All-or-nothing per dispatch: a sidecar whose +// messages carry no ids (compaction/title shapes) captures none, and the +// field simply stays off that turn — degradation, never a guess. +interface LlmDispatchScope { + sid: string; + msgIds: string[] | undefined; +} +const dispatchScope = new AsyncLocalStorage(); let noticedScopedAttribution = false; -/** Re-enter the sid scope on every iterator advance: generator bodies +/** #4491: extract the host message ids from llm/stream `options.messages`, + * all-or-nothing. Mirrors validateMsgIds in src/msgid-pin.ts (the 400-class + * conditions — bad entry, reserved prefix, control/space, duplicate) so a + * hostile/odd host id can never hard-fail the turn; the host-side helper + * must not import server modules (same discipline as the marker-header + * twin in incremental-client.ts), so the constants are duplicated — keep + * them in sync. The COUNT check is deliberately absent: alignment against + * the actually-sent wire messages happens at send time (maybeIncrementalNative). + */ +function hostMsgIdsOf(messages: unknown): string[] | undefined { + if (!Array.isArray(messages) || messages.length === 0) return undefined; + const ids: string[] = []; + const seen = new Set(); + for (const m of messages) { + if (m === null || typeof m !== "object") return undefined; + const id = (m as { id?: unknown }).id; + if (typeof id !== "string" || id.length === 0 || id.length > 512) return undefined; + if (id.startsWith("acp_summary_")) return undefined; // kernel anchor namespace + if (/[\s\p{C}]/u.test(id)) return undefined; + if (seen.has(id)) return undefined; + seen.add(id); + ids.push(id); + } + return ids; +} + +/** Re-enter the dispatch scope on every iterator advance: generator bodies * resume in the consumer's dynamic context, so each `next()` must run the * adapter's continuation (its awaits, and any fetches it starts) inside the * same scope the dispatch carried. */ -function wrapScopedIterable(iter: AsyncIterable, sid: string): AsyncIterable { +function wrapScopedIterable(iter: AsyncIterable, scope: LlmDispatchScope): AsyncIterable { return { [Symbol.asyncIterator]: () => { const it = iter[Symbol.asyncIterator](); - const run = (op: () => Promise): Promise => sidScope.run(sid, op); + const run = (op: () => Promise): Promise => dispatchScope.run(scope, op); return { next: () => run(() => it.next()), return: (value?: unknown) => run(() => it.return !== undefined ? it.return(value as never) : Promise.resolve({ done: true, value: value as T })), @@ -1174,13 +1212,13 @@ function attributionOf(ctx: PluginContext): GateAttribution { // driving), and it survives exactly the shapes the ambient reading does // not (agentless compaction/title lanes, disposed scopes, retried // re-sends whose initiator scope went away between attempts). - const scoped = sidScope.getStore(); - if (typeof scoped === "string" && scoped.length > 0) { + const scoped = dispatchScope.getStore(); + if (scoped !== undefined && scoped.sid.length > 0) { if (!noticedScopedAttribution) { noticedScopedAttribution = true; persistClientEvent("bili-native-dsh: llm/stream attribution active — session id rides the request object (#4082)"); } - return { state: "ok", sid: scoped }; + return { state: "ok", sid: scoped.sid }; } try { const init = ctx.agents?.currentInitiator?.(); @@ -1693,13 +1731,17 @@ export function apply(ctx: PluginContext): void { if (typeof ctx.inject === "function" && typeof ctx.on === "function") { const onEvent = ctx.on.bind(ctx); ctx.inject(["llm"], () => { - onEvent("llm/stream", (options: { sessionId?: unknown }, next: () => AsyncIterable) => { + onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown }, next: () => AsyncIterable) => { const sid = typeof options?.sessionId === "string" && options.sessionId.length > 0 ? options.sessionId : undefined; if (sid === undefined) return next(); + // #4491: capture the host message ids riding the same options + // — undefined (all-or-nothing) when any message lacks a usable + // id, so sidecar shapes degrade to id-less turns. + const scope: LlmDispatchScope = { sid, msgIds: hostMsgIdsOf(options?.messages) }; // next() may start the adapter's fetch eagerly (inside this // call), and lazy continuations resume on each advance — the // run() below covers the former, wrapScopedIterable the latter. - return sidScope.run(sid, () => wrapScopedIterable(next(), sid)); + return dispatchScope.run(scope, () => wrapScopedIterable(next(), scope)); }); }); } @@ -2237,12 +2279,21 @@ export function apply(ctx: PluginContext): void { if (!(await probeIncrementalTurns(base))) return; sweepStale(incPendingAcks, INC_PENDING_TTL_MS); 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 + // 1:1 with the wire messages ACTUALLY being sent (the adapter may + // drop/reshape entries). Equal count ⇒ same order (the host renders + // its session messages in order); anything else omits the field — + // degrade over mis-pair, never pin a wrong id. + const scopedIds = dispatchScope.getStore()?.msgIds; + const msgIds = scopedIds !== undefined && scopedIds.length === (parsed.messages as unknown[]).length ? scopedIds : undefined; const plan = planIncrementalTurn({ conversationId: sid, path: "/v1/messages", // placeholder — the marker divert uses the ORIGINAL routed URL messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, request: parsed as Record, + msgIds, }); if (plan.mode === "skip") return; // #3370 single-flight (omp/opencode pattern; kept over #3329's diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index ef7b5de9fc..d5b1ef4b0e 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -172,6 +172,37 @@ export interface IncrementalWireMessage { content: unknown; } +/** #4491 host msgids: the host's stable message ids for the NEXT outgoing + * request of a conversation, as (id, role) pairs in session order. Stashed + * by the host-side extension event (it owns the sessionManager) and read + * back by the fetch-layer planner (it owns the actually-sent messages); + * the consumer aligns by count+role against the real wire set and refuses + * the whole field on any mismatch — a misaligned id would pin the wrong + * identity server-side, while a missing field merely falls back to + * content-derived ids. */ +export interface HostMsgIdHintEntry { + id: string; + role: string; +} + +const HOST_MSGID_HINT_TTL_MS = 30_000; +const hostMsgIdHints = new Map(); + +/** Record the branch-entry (id, role) snapshot for a conversation's next + * outgoing request. Overwrites per request — one hint per conversation, + * always the generation of the payload being dispatched. */ +export function noteHostMsgIdHint(conversationId: string, entries: HostMsgIdHintEntry[]): void { + sweepStale(hostMsgIdHints, HOST_MSGID_HINT_TTL_MS); + hostMsgIdHints.set(conversationId, { entries, at: Date.now() }); +} + +/** The stashed hint for a conversation, or undefined when none is fresh. */ +export function hostMsgIdHintFor(conversationId: string): HostMsgIdHintEntry[] | undefined { + const entry = hostMsgIdHints.get(conversationId); + if (entry === undefined || Date.now() - entry.at >= HOST_MSGID_HINT_TTL_MS) return undefined; + return entry.entries; +} + interface ConvState { /** Last server ack (x-bili-applied-seq) — flat message count. */ sinceSeq: number; @@ -185,6 +216,53 @@ interface ConvState { const convs = new Map(); +// #4491 host msgids: ids this client has already put on an envelope for the +// conversation — the client-side mirror of the lane's canonical id set. The +// lane HARD-FAILS a non-resync turn (400) whose msgIds reuse a live canonical +// id ("msgIds reuse an existing canonical id"), so a delta candidate already +// in this set degrades to "no field" instead of killing the request: the +// realistic trigger is a host retry re-planning the same delta after the +// first attempt may have landed server-side. A resync/seed replaces the set +// wholesale, exactly like the server replaces the canonical. +const msgidClaims = new Map>(); + +/** Local mirror of the lane's validateMsgIds reject classes + * (src/msgid-pin.ts — keep in sync; the host bundle must not import server + * modules). The lane hard-fails a turn on a poisoned id array, so the host + * screens candidates BEFORE they ride the envelope: anything the server + * would reject degrades to "no field" here (content-derived ids) instead of + * a 400 that kills the whole request. */ +const MSGID_MAX_LEN = 512; +const MSGID_RESERVED_PREFIX = "acp_summary_"; +const MSGID_BAD_CHARS = /[\s\p{C}]/u; + +function screenMsgIds(ids: unknown): ids is string[] { + if (!Array.isArray(ids) || ids.length === 0) return false; + const seen = new Set(); + for (const v of ids) { + if (typeof v !== "string" || v.length === 0 || v.length > MSGID_MAX_LEN) return false; + if (v.startsWith(MSGID_RESERVED_PREFIX)) return false; + if (MSGID_BAD_CHARS.test(v)) return false; + if (seen.has(v)) return false; + seen.add(v); + } + return true; +} + +/** Delta-turn claim gate: undefined when any candidate id was already sent + * on an earlier envelope of this conversation (the lane would 400 — see + * msgidClaims); otherwise the ids are claimed and returned. */ +function claimableDeltaMsgIds(conversationId: string, ids: string[]): string[] | undefined { + const claims = msgidClaims.get(conversationId); + if (claims === undefined) { + msgidClaims.set(conversationId, new Set(ids)); + return ids; + } + for (const id of ids) if (claims.has(id)) return undefined; + for (const id of ids) claims.add(id); + return ids; +} + const PROBE_TTL_MS = 30_000; const probeCache = new Map(); @@ -287,9 +365,18 @@ interface PlanArgs { /** Anthropic request params from the original body (model, max_tokens…); * optional — missing params ride the lane's defaults. */ request?: Record; + /** #4491: host-stable message ids aligned 1:1 with `messages` (same + * length), or undefined when the host cannot vouch for every entry. + * Carried onto the envelope as `msgIds` parallel to the messages the + * mode actually sends (seed/resync: the full set; delta: the suffix), + * so the server pins the client's identity instead of content-derived + * hashes. All-or-nothing: a length mismatch, a screen failure, or an + * already-claimed id drops the WHOLE field — never a partial or + * misaligned array. */ + msgIds?: string[]; } -function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string { +function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean, msgIds?: string[]): string { // `request` is optional at the call sites (a host may plan with only // messages+system); missing params simply ride their lane defaults. const src = args.request ?? {}; @@ -305,6 +392,9 @@ function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMe }; if (resync) body.resync = true; if (includeSystem) body.system = args.system === undefined ? null : args.system; + // #4491: only a provably-parallel, non-empty array rides the envelope — + // an empty delta (pure sticky-system update) pins nothing and stays off. + if (msgIds !== undefined && msgIds.length === messages.length && msgIds.length > 0) body.msgIds = msgIds; return JSON.stringify(body); } @@ -313,6 +403,9 @@ function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMe export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan { const state = convs.get(args.conversationId); if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" }; + // #4491: vouched ids must be exactly parallel to the outgoing set and + // pass the local screen; anything else means "no field" this turn. + const fullIds = args.msgIds !== undefined && args.msgIds.length === args.messages.length && screenMsgIds(args.msgIds) ? args.msgIds : undefined; const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); if (state === undefined || forceResync) { const includeSystem = state === undefined || systemJson !== state.systemJson; @@ -322,7 +415,8 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm // empty (fresh conversation) or already live (interop with // full-resend traffic: the client's view is authoritative and // identical-or-newer). Only DELTA turns can 409. - return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) }; + if (fullIds !== undefined) msgidClaims.set(args.conversationId, new Set(fullIds)); + return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem, fullIds) }; } const hashes = args.messages.map(wireHash); // Prefix alignment: everything previously acked must still lead the list. @@ -332,7 +426,8 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm if (state.sent[i] !== hashes[i]) { aligned = false; break; } } if (!aligned || hashes.length < state.sent.length) { - return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson) }; + if (fullIds !== undefined) msgidClaims.set(args.conversationId, new Set(fullIds)); + return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson, fullIds) }; } if (hashes.length === state.sent.length) { // Equal message set: normally a retry of the same turn → normal path. @@ -344,9 +439,10 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm } return { mode: "skip" }; // retry of the same turn → normal path } + const deltaIds = fullIds !== undefined ? claimableDeltaMsgIds(args.conversationId, fullIds.slice(state.sent.length)) : undefined; return { mode: "delta", - body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson), + body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson, deltaIds), }; } @@ -411,4 +507,6 @@ export function __resetIncrementalClientForTests(): void { droppedParamWarned.clear(); droppedParamWarnPairs = 0; originalBodies.clear(); + hostMsgIdHints.clear(); + msgidClaims.clear(); } diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 361a3595ed..5bbf786891 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, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, 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"; @@ -104,6 +104,28 @@ const incPendingOmp = new Map(); * (#3329): attribution is exact per turn id. Mirrors opencode. */ const INC_PENDING_TTL_MS = 30_000; +/** #4491 omp msgids: align the host's branch-entry ids (stashed by the omp + * extension's before_provider_request for this conversation id) against the + * ACTUALLY-sent wire messages — count + role, pairwise. Returns the id + * sequence only on a provable 1:1 match; any mismatch (branch out of sync + * with the sent history, a native-compaction summary that became a wire + * message but has no entry id, an aux persona, an older host without + * getBranch) returns undefined and the turn rides without the field. The + * sent messages are the alignment authority: a sequence that cannot be + * proven parallel is never sent — a misaligned id would pin the wrong + * identity server-side, while a missing field merely falls back to + * content-derived ids. */ +function ompTurnMsgIds(sid: string, messages: IncrementalWireMessage[]): string[] | undefined { + const hint = hostMsgIdHintFor(sid); + if (hint === undefined || hint.length !== messages.length) return undefined; + const ids: string[] = []; + for (let i = 0; i < hint.length; i++) { + if (hint[i].role !== messages[i].role) return undefined; + ids.push(hint[i].id); + } + return ids; +} + /** Test hook: drop all planned turns (module state across suites). */ export function __resetOmpIncrementalForTests(): void { incPendingOmp.clear(); @@ -127,7 +149,7 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | const base = state.origin; if (base === undefined) { return; } 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 }); + 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 diff --git a/src/agent/opencode-msgids.ts b/src/agent/opencode-msgids.ts new file mode 100644 index 0000000000..53e08311e6 --- /dev/null +++ b/src/agent/opencode-msgids.ts @@ -0,0 +1,120 @@ +// #4491 host wiring (opencode): client msgIds for incremental-lane turns. +// +// opencode 1.x fires `experimental.chat.messages.transform` per LLM request +// with `{ info, parts }[]` — info.id is the host message id (msg_ + 12 hex + +// 14 base62; zero reuse observed across 378K messages, and a +// streaming-settled message re-fires under the SAME id with grown content — +// edit semantics the server-side history reconciles, so the host side only +// ever stores the LATEST fire per session). That hook is the ONLY opencode +// seam carrying host ids: the V2 `http.request` interception sees the final +// wire body and nothing else, so V2-shaped turns never carry msgIds — they +// degrade to the lane's content-derived identities by design (#4491 host +// wiring audit; opencode is the weakest of the four host seams). +// +// Alignment is proof, not guesswork (宁可降级不可错位): ids ride only when +// every harvested message provably lowers 1:1 onto the wire — text-only +// parts, role user|assistant, and the derived text sequence exactly equal to +// the observed wire messages (cache_control and other block props ignored). +// Any deviation — tool/reasoning/file parts, ignored or synthetic text, a +// missing or foreign-shaped id, a content drift, duplicate ids — drops the +// whole field and the turn rides exactly as before. Order-preserving lowering +// plus per-position equality is what makes the positional pairing safe; a +// compensating merge+split that still byte-matches every position can only +// swap ids between byte-identical content, which the ledger joins as one. + +interface HarvestedEntry { + /** Host message id; "" when unusable (missing/foreign-shaped) — poisons + * alignment for any list containing it, never the lane itself. */ + id: string; + /** Derivable wire shape ({role, ordered text-part texts}), or null when + * this message cannot be proven to lower 1:1 onto one wire message. */ + shape: { role: "user" | "assistant"; texts: string[] } | null; +} + +const MAX_SESSIONS = 256; +const harvests = new Map(); + +/** opencode host ids: msg_ + base62 tail (#4491 audit shape). Restricting to + * this charset keeps every server-poison class out client-side (whitespace + * and control characters, the kernel's acp_summary_ namespace, empty ids). */ +const HOST_MSG_ID = /^msg_[A-Za-z0-9]+$/; + +function deriveEntry(raw: unknown): HarvestedEntry { + const holder = raw as { info?: unknown; parts?: unknown } | null | undefined; + const info = holder !== null && typeof holder === "object" ? holder.info : undefined; + const parts = holder !== null && typeof holder === "object" ? holder.parts : undefined; + const infoObj = info !== null && typeof info === "object" ? (info as { id?: unknown; role?: unknown }) : undefined; + const id = typeof infoObj?.id === "string" && HOST_MSG_ID.test(infoObj.id) ? infoObj.id : ""; + if (infoObj?.role !== "user" && infoObj?.role !== "assistant") return { id, shape: null }; + if (!Array.isArray(parts)) return { id, shape: null }; + const texts: string[] = []; + for (const part of parts) { + const p = part !== null && typeof part === "object" ? (part as { type?: unknown; text?: unknown; ignored?: unknown; synthetic?: unknown }) : undefined; + // ignored text never reaches the model (host-render notices) and + // synthetic text has no proven lowering — both stay underivable. + if (p === undefined || p.type !== "text" || p.ignored === true || p.synthetic === true || typeof p.text !== "string") return { id, shape: null }; + texts.push(p.text); + } + return { id, shape: { role: infoObj.role, texts } }; +} + +/** Record the latest messages.transform fire for one session (V1 hook). The + * host's outgoing messages are left untouched — harvesting never mutates. */ +export function harvestOpencodeMsgIds(sid: string | undefined, messages: unknown): void { + if (typeof sid !== "string" || sid.length === 0 || !Array.isArray(messages)) return; + if (!harvests.has(sid) && harvests.size >= MAX_SESSIONS) { + let oldestKey: string | undefined; + let oldestAt = Number.POSITIVE_INFINITY; + for (const [key, entry] of harvests) { + if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } + } + if (oldestKey !== undefined) harvests.delete(oldestKey); + } + harvests.set(sid, { at: Date.now(), entries: messages.map(deriveEntry) }); +} + +/** Wire text sequence of one wire message: string content → [text]; block + * array → text blocks' text in order (cache_control et al. ignored). Any + * non-text block (tool_use, tool_result, image, thinking…) → null. */ +function wireTexts(content: unknown): string[] | null { + if (typeof content === "string") return [content]; + if (!Array.isArray(content)) return null; + const texts: string[] = []; + for (const block of content) { + const b = block !== null && typeof block === "object" ? (block as { type?: unknown; text?: unknown }) : undefined; + if (b === undefined || b.type !== "text" || typeof b.text !== "string") return null; + texts.push(b.text); + } + return texts; +} + +/** Client ids for the FULL wire message list of one planned turn, or + * undefined when alignment cannot be proven (no harvest for the session, + * count mismatch, underivable/divergent entries, duplicate ids). The + * planner slices the result in lockstep with the delta messages. */ +export function alignedMsgIdsForWire(sid: string, wire: unknown): string[] | undefined { + const stored = harvests.get(sid); + if (stored === undefined || !Array.isArray(wire) || stored.entries.length !== wire.length) return undefined; + const ids: string[] = []; + const seen = new Set(); + for (let i = 0; i < wire.length; i++) { + const entry = stored.entries[i]; + if (entry.id === "" || entry.shape === null) return undefined; + const msg = wire[i] as { role?: unknown; content?: unknown } | null | undefined; + if (msg === null || typeof msg !== "object" || msg.role !== entry.shape.role) return undefined; + const texts = wireTexts(msg.content); + if (texts === null || texts.length !== entry.shape.texts.length) return undefined; + for (let j = 0; j < texts.length; j++) { + if (texts[j] !== entry.shape.texts[j]) return undefined; + } + if (seen.has(entry.id)) return undefined; + seen.add(entry.id); + ids.push(entry.id); + } + return ids; +} + +/** Test hook: drop all harvested sessions (module state across suites). */ +export function __resetOpencodeMsgIdsForTests(): void { + harvests.clear(); +} diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 3e1765d401..12852066a6 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -79,6 +79,7 @@ import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScr import { createLiveOriginResolver, installNativeFetchIntercept, isModelApiUrl, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, replaceRequestPayload, replaceRequestTarget, routedBiliModelUrl, type LiveOriginResolverDeps, type NativeInterceptState, type UnavailableReason } from "./native-intercept.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 { alignedMsgIdsForWire, harvestOpencodeMsgIds } from "./opencode-msgids.js"; import { asciiHeaderValue, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, waitForProxyVersion, type ForwardedToolResult } from "./shared.js"; import { callLegacyAcpConfig, isLegacyAcpSession, loadLegacyAcp, type LegacyAcpModule } from "./opencode-legacy.js"; import { logHostEvent } from "./host-log.js"; @@ -602,6 +603,14 @@ function lastSessionFromMessages(messages: unknown): string | undefined { return undefined; } +/** #4491 host wiring: harvest host message ids per LLM request from the V1 + * messages.transform hook — the only opencode seam that sees them (V2's + * http.request sees just the wire, so V2-shaped turns degrade to + * content-derived identities). Never mutates the host's messages. */ +const harvestMsgIdsTransform = async (_input: unknown, output: { messages?: unknown }): Promise => { + harvestOpencodeMsgIds(lastSessionFromMessages(output?.messages), output?.messages); +}; + // #1135: dynamic origin getter instead of a captured constant — after a // runtime death+recovery the live origin changes, and hooks bound to the // startup origin would keep forwarding tools to the dead port while traffic @@ -692,9 +701,14 @@ export function createV1ServerHooks(getOrigin: () => string | undefined, ctx: V1 const msgT = legacy.hooks["experimental.chat.messages.transform"]; if (typeof msgT === "function") { hooks["experimental.chat.messages.transform"] = async (input, output) => { - if (!isLegacy(lastSessionFromMessages(output?.messages))) return; - await (msgT as (i: unknown, o: unknown) => Promise)(input, output); + if (isLegacy(lastSessionFromMessages(output?.messages))) { + await (msgT as (i: unknown, o: unknown) => Promise)(input, output); + return; + } + await harvestMsgIdsTransform(input, output); }; + } else { + hooks["experimental.chat.messages.transform"] = harvestMsgIdsTransform; } const textC = legacy.hooks["experimental.text.complete"]; if (typeof textC === "function") { @@ -709,6 +723,10 @@ export function createV1ServerHooks(getOrigin: () => string | undefined, ctx: V1 if (typeof evt === "function") { hooks.event = evt as V1Hooks["event"]; } + } else { + // #4491: with no absorbed acp the transform hook is pure msgid + // harvesting — registered for every session, inert-safe. + hooks["experimental.chat.messages.transform"] = harvestMsgIdsTransform; } if (deps.z !== undefined || legacy !== undefined) { hooks["chat.headers"] = async (input, output) => { @@ -902,7 +920,7 @@ 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 }); + 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 diff --git a/src/agent/pi-msgids.ts b/src/agent/pi-msgids.ts new file mode 100644 index 0000000000..80e353743e --- /dev/null +++ b/src/agent/pi-msgids.ts @@ -0,0 +1,238 @@ +// #4491 pi host wiring (plugin-lane server history): resolve the pi session's +// stable message ids (SessionEntry.id — randomUUID first 8 hex, stable across +// every host operation) for the OUTGOING wire messages of a provider request, +// so the incremental lane can pin the client's own ids onto its server +// canonical (`msgIds`, parallel to the lane body's `messages`). +// +// The wire messages are NOT the session messages: pi converts the branch +// through buildContextEntries → convertToLlm → transformMessages → +// convertMessages (drop empty texts, merge consecutive tool results into one +// user message, sanitize lone surrogates, cross-model thinking rules). This +// walker never re-implements that pipeline — it walks wire messages and branch +// entries in lockstep and accepts an id only when the pair provably denotes +// the same message: +// - each step compares an order-preserving content fingerprint (text, +// image bytes, thinking text, redacted payload, tool-call ids) with EXACT +// equality — never similarity, never position alone; +// - entries that provably produce no wire message (empty text) are skipped; +// - anything the walker cannot prove (agent-message roles with derived +// text, compaction/branch summaries, alien block types, any mismatch) +// aborts the whole resolution — all-or-nothing: one unresolvable message +// and the turn ships WITHOUT msgIds, so the lane falls back to +// content-derived ids, exactly today's behavior. +// +// Fingerprint mirroring (verified against pi 0.99.1 dist): text blocks drop +// when whitespace-only on BOTH sides; thinking blocks ride verbatim on the +// same model (empty text kept iff a signature exists) and mismatch → bail on +// cross-model rewrites; tool_use/tool_result match by call id (unique within +// a branch path — ids are minted once per call); lone surrogates are stripped +// with pi's own regex; cache_control decorations are ignored (the wire may +// add them; they never change identity). + +type FpPart = { t: "text" | "img" | "tooluse" | "toolres" | "think" | "redact"; v: string }; + +/** Mirror of pi's sanitizeSurrogates (utils/sanitize-unicode.js): strips + * unpaired surrogates so both sides of a comparison normalize identically. */ +function stripLoneSurrogates(text: string): string { + return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?; + if (block.type === "text") { + const p = textPart(block.text); + if (p !== undefined) parts.push(p); + } else if (block.type === "image") { + if (typeof block.data !== "string") return undefined; + parts.push({ t: "img", v: block.data }); + } else { + return undefined; + } + } + return parts; +} + +/** Fingerprint of an assistant AgentMessage content (text / thinking / + * toolCall blocks). undefined = shape this walker refuses to reason about. */ +function assistantParts(content: unknown): FpPart[] | undefined { + if (content === null || content === undefined) return []; + if (!Array.isArray(content)) return undefined; + const parts: FpPart[] = []; + for (const raw of content) { + if (raw === null || typeof raw !== "object") return undefined; + const block = raw as Record; + if (block.type === "text") { + const p = textPart(block.text); + if (p !== undefined) parts.push(p); + } else if (block.type === "thinking") { + if (block.redacted === true) { + if (typeof block.thinkingSignature !== "string") return undefined; + parts.push({ t: "redact", v: block.thinkingSignature }); + continue; + } + if (typeof block.thinking !== "string") return undefined; + const hasSignature = typeof block.thinkingSignature === "string" && block.thinkingSignature.trim().length > 0; + // The conversion keeps an empty-text thinking block iff a + // signature exists — mirror that, else the block is dropped. + if (block.thinking.trim().length > 0 || hasSignature) parts.push({ t: "think", v: stripLoneSurrogates(block.thinking) }); + } else if (block.type === "toolCall") { + if (typeof block.id !== "string" || block.id.length === 0) return undefined; + parts.push({ t: "tooluse", v: block.id }); + } else { + return undefined; + } + } + return parts; +} + +/** Fingerprint of one OUTGOING wire message (anthropic shape). undefined = + * unprovable (alien role/block); [] = cannot happen on the wire (the + * conversion drops empty messages) — treated as unprovable by the caller. */ +function wireFingerprint(msg: unknown): FpPart[] | undefined { + if (msg === null || typeof msg !== "object" || Array.isArray(msg)) return undefined; + const role = (msg as Record).role; + const content = (msg as Record).content; + if (role !== "user" && role !== "assistant") return undefined; + if (typeof content === "string") { + if (role !== "assistant") { + const p = textPart(content); + return p === undefined ? [] : [p]; + } + return undefined; // pi never sends assistant string content + } + if (!Array.isArray(content)) return undefined; + const parts: FpPart[] = []; + for (const raw of content) { + if (raw === null || typeof raw !== "object") return undefined; + const block = raw as Record; + if (block.type === "text") { + const p = textPart(block.text); + if (p !== undefined) parts.push(p); + } else if (block.type === "thinking") { + if (typeof block.thinking !== "string") return undefined; + parts.push({ t: "think", v: stripLoneSurrogates(block.thinking) }); + } else if (block.type === "redacted_thinking") { + if (typeof block.data !== "string") return undefined; + parts.push({ t: "redact", v: block.data }); + } else if (block.type === "tool_use") { + if (typeof block.id !== "string" || block.id.length === 0) return undefined; + parts.push({ t: "tooluse", v: block.id }); + } else if (block.type === "tool_result") { + if (typeof block.tool_use_id !== "string" || block.tool_use_id.length === 0) return undefined; + parts.push({ t: "toolres", v: block.tool_use_id }); + } else if (block.type === "image") { + const source = block.source; + if (source === null || typeof source !== "object" || Array.isArray(source)) return undefined; + const data = (source as Record).data; + if (typeof data !== "string") return undefined; + parts.push({ t: "img", v: data }); + } else { + return undefined; + } + } + return parts; +} + +/** Fingerprint of one branch entry's context contribution. undefined = + * unprovable (unsupported entry/message shape); [] = provably produces no + * wire message. */ +function entryFingerprint(entry: Record): FpPart[] | undefined { + if (entry.type === "message") { + const raw = entry.message; + if (raw === null || typeof raw !== "object") return undefined; + const msg = raw as Record; + if (msg.role === "user" || msg.role === "custom") return userLikeParts(msg.content); + if (msg.role === "assistant") return assistantParts(msg.content); + if (msg.role === "toolResult") { + if (typeof msg.toolCallId !== "string" || msg.toolCallId.length === 0) return undefined; + return [{ t: "toolres", v: msg.toolCallId }]; + } + return undefined; // bashExecution / branchSummary / compactionSummary / … + } + if (entry.type === "custom_message") return userLikeParts(entry.content); + return undefined; +} + +/** Session entry types that never contribute to the LLM context. */ +const CONTEXT_SILENT_ENTRY_TYPES = new Set(["thinking_level_change", "model_change", "label", "session_info", "custom"]); + +/** Walk the outgoing wire messages and the branch entries in lockstep; return + * the entry id for every wire message (aligned by index), or undefined when + * the alignment cannot be proven end-to-end (all-or-nothing). */ +export function laneMsgIdsForWire(wire: unknown, branch: unknown): string[] | undefined { + if (!Array.isArray(wire) || !Array.isArray(branch)) return undefined; + const entries = branch as unknown[]; + let ei = 0; + // Next visible (wire-producing) entry past every provably silent one. + // "bail" = unprovable shape; "exhausted" = no entries left. + const take = (): { id: string; parts: FpPart[] } | "bail" | "exhausted" => { + while (ei < entries.length) { + const raw = entries[ei]; + ei += 1; + if (raw === null || typeof raw !== "object" || Array.isArray(raw)) return "bail"; + const entry = raw as Record; + if (typeof entry.type !== "string") return "bail"; + if (entry.type === "message" || entry.type === "custom_message") { + const parts = entryFingerprint(entry); + if (parts === undefined) return "bail"; + if (parts.length === 0) continue; // provably invisible on the wire + if (typeof entry.id !== "string" || entry.id.length === 0) return "bail"; + return { id: entry.id, parts }; + } + if (CONTEXT_SILENT_ENTRY_TYPES.has(entry.type)) continue; + // compaction / branch_summary / unknown types: the context shape + // (compaction-aware prefix replacement) is not derivable here. + return "bail"; + } + return "exhausted"; + }; + const ids: string[] = []; + for (const w of wire) { + const wParts = wireFingerprint(w); + if (wParts === undefined || wParts.length === 0) return undefined; + if (wParts.every((p) => p.t === "toolres")) { + // Merged consecutive tool results: ONE wire message, N entries. + for (const part of wParts) { + const n = take(); + if (typeof n === "string" || n.parts.length !== 1 || n.parts[0].t !== "toolres" || n.parts[0].v !== part.v) return undefined; + ids.push(n.id); + } + continue; + } + const n = take(); + if (typeof n === "string") return undefined; + if (n.parts.length !== wParts.length || n.parts.some((p, i) => p.t !== wParts[i].t || p.v !== wParts[i].v)) return undefined; + ids.push(n.id); + } + // A visible entry surviving past the wire's end means the request dropped + // a message this walk cannot explain — refuse rather than guess. + const leftover = take(); + return leftover === "exhausted" ? ids : undefined; +} + +/** #4491 lane contract: `msgIds` pins 1:1 onto the FLATTENED core messages + * (the kernel's anthropicToCore maps every block to its own message), and + * the lane hard-fails (400) when the id count matches the wire count but the + * flattened count differs. A delta message is pinnable only when it flattens + * to exactly one core message: a string, or a single-block content array. */ +export function lanePinnableContent(content: unknown): boolean { + return typeof content === "string" || (Array.isArray(content) && content.length === 1); +} diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 9e0a3c22d6..2182f567e5 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -16,7 +16,9 @@ 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, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; +import { laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; +import { validateMsgIds } from "../msgid-pin.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"; @@ -394,6 +396,34 @@ export function replayedMessageCount(ctx: Ctx): number | undefined { } } +/** #4491 omp msgids wiring: (id, role) of the branch's message entries in + * session order — the hint the fetch-layer incremental planner + * (omp-native) aligns against the ACTUALLY-sent wire messages and carries + * as `msgIds` when they match 1:1. Non-message entries (model_change, + * native-compaction summaries) are skipped — they have no message id, so a + * summary that DID become a wire message leaves the counts short and the + * alignment refuses the whole field (all-or-nothing). An entry without a + * stable id yields an empty one, which the consumer's screen rejects the + * same way. Total: a missing/throwing getBranch yields [] — this request + * cannot vouch ids, and [] forces the degrade instead of leaving a stale + * earlier hint in place. */ +export function ompBranchMsgIdEntries(ctx: Ctx): HostMsgIdHintEntry[] { + const out: HostMsgIdHintEntry[] = []; + try { + const branch = ctx.sessionManager?.getBranch?.(); + if (!Array.isArray(branch)) return out; + for (const entry of branch) { + if (entry === null || typeof entry !== "object" || (entry as { type?: unknown }).type !== "message") continue; + const id = (entry as { id?: unknown }).id; + const role = (entry as { message?: { role?: unknown } | null }).message?.role; + out.push({ id: typeof id === "string" ? id : "", role: typeof role === "string" ? role : "" }); + } + } catch { + return []; + } + return out; +} + // #2583: OMP's Cowork transport (can1357/oh-my-pi 642892ae, proxy handling // e22a5ff2) picks egress PER MODEL: default anthropic/anthropic-messages // requests go out over node:https DIRECTLY — stable header order, HTTP/1.1, @@ -1409,7 +1439,25 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM // 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; + const body = JSON.parse((plan as { body: string }).body) as { messages: IncrementalWireMessage[]; msgIds?: string[] }; + // #4491 pi msgids wiring: attach the pi session's stable message + // ids (aligned to the FULL outgoing wire) sliced to the plan's + // delta messages, all-or-nothing — one unresolvable message, a + // multi-block delta message (the lane pins 1:1 onto the flattened + // core messages and 400s otherwise), or a poison-shaped id leaves + // the field off entirely and the turn rides content-derived ids. + let branch: unknown; + try { + branch = ctx.sessionManager?.getBranch?.(); + } catch { + branch = undefined; + } + const ids = laneMsgIdsForWire(payload.messages, branch); + if (ids !== undefined && body.messages.every((m) => lanePinnableContent(m.content))) { + const sliced = ids.slice(ids.length - body.messages.length); + if (sliced.length === body.messages.length && validateMsgIds(sliced, sliced.length).ok) body.msgIds = sliced; + } + return body; } async function maybeAdoptForkChild(event: unknown, ctx: Ctx): Promise { if (state.toolsReady !== true) return; @@ -1439,6 +1487,16 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM await forkAdopter.maybeAdopt({ base: proxyBase, parent, child: sid, body: (event as { payload?: unknown } | undefined)?.payload }); } pi.on("before_provider_request", async (event, ctx) => { + // #4491 omp msgids: snapshot the branch's stable entry ids BEFORE + // any await below — the hint must be the generation of THIS + // payload. The fetch-layer planner (omp-native) reads it back by + // the prompt_cache_key conversation id and carries `msgIds` only + // when the ids align 1:1 with the actually-sent messages; any + // mismatch degrades to no field, never a misaligned array. + if (agent === "omp") { + const hintSid = sessionIdOf(ctx); + if (hintSid !== undefined && hintSid.length > 0) noteHostMsgIdHint(hintSid, ompBranchMsgIdEntries(ctx)); + } // omp emits this per model request (but never before_provider_headers); // it doubles as the retry driver when the session_start manifest // fetch raced the proxy startup. Cached by sid, throttled by retryAt. diff --git a/tests/dsh-msgids.test.ts b/tests/dsh-msgids.test.ts new file mode 100644 index 0000000000..fc603770f1 --- /dev/null +++ b/tests/dsh-msgids.test.ts @@ -0,0 +1,343 @@ +/** + * #4491 dsh host wiring: llm/stream msgIds on incremental lane turns. + * + * dsh stamps a stable UUID on every session message and hands the list to + * the llm/stream waterfall (options.messages[].id). The plugin captures the + * ids there (all-or-nothing) and — when the adapter's outgoing wire body + * lines up 1:1 with them — carries them on the incremental turn envelope + * (`msgIds`), so the server pins its canonical rows to the host's own + * message identity instead of content-derived hashes. + * + * Stakes (all in one seam, beforeSend under the llm/stream scope): + * - aligned ids ride the turn body, sliced to the DELTA messages; + * - any unusable id (missing/duplicate/reserved/控制字符) drops the whole + * field for that turn — id-less incremental turns keep working; + * - a count mismatch against the actually-sent messages never ships a + * guessed pairing (degrade over misattribute). + */ +import { strict as assert } from "node:assert"; +import http from "node:http"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; + +// Same hermetic posture as the sibling dsh suites: nothing here may depend +// on this machine's real proxy/env wiring (planNativeDsh returns "off" with +// these set, and the plugin would never register). +delete process.env.BILI_PROVIDER_REWRITES; +delete process.env.BILI_NATIVE_DSH; +delete process.env.BILLION_CONTEXT_PLUGIN; + +import { + apply, + _resetRegisterForTest, + _settleNativeForTest, + _stateBeforeSendForTest, + _stateOnResponseForTest, + _stateToolsReadyForTest, + type PluginContext, +} from "../src/agent/dsh-native.ts"; +import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts"; +import { __resetConversationRecallForTests } from "../src/agent/conversation-recall.ts"; + +const MODEL_URL = "https://api.anthropic.com/v1/messages"; + +interface RegisteredToolLike { + name: string; +} + +interface CtxHandle { + ctx: PluginContext; + events: Map unknown) => unknown>>; + setInitiator: (sid: string | undefined) => void; + registered: RegisteredToolLike[]; +} + +function makeCtx(): CtxHandle { + const events = new Map unknown) => unknown>>(); + const registered: RegisteredToolLike[] = []; + let currentSid: string | undefined; + const ctx = { + inject: (deps: readonly string[], cb: (sub: unknown) => void) => { + // Only the llm service matters here; other inject targets + // (webServer, credentials, sessions…) stay cold like the sibling + // harnesses. + if (deps.includes("llm")) cb({}); + }, + on: (name: string, listener: (payload: unknown, next: () => unknown) => unknown) => { + const list = events.get(name) ?? []; + list.push(listener); + events.set(name, list); + }, + emit: () => undefined, + logger: { info: () => undefined, warn: () => undefined, error: () => undefined }, + tools: { register: (tool: RegisteredToolLike) => void registered.push(tool) }, + commands: { register: () => undefined }, + agents: { + currentInitiator: () => + currentSid === undefined ? undefined : { session: { id: currentSid } }, + }, + setInitiator: (sid: string | undefined) => { + currentSid = sid; + }, + } as unknown as PluginContext & { setInitiator: (sid: string | undefined) => void }; + return { ctx, events, setInitiator: (sid) => ctx.setInitiator(sid), registered }; +} + +/** Compose the registered llm/stream listeners like cordis does (outermost + * first, each receives (options, next)) over a base adapter. */ +function runWaterfall( + events: Map unknown) => unknown>>, + options: Record, + base: () => AsyncIterable, +): AsyncIterable { + const listeners = events.get("llm/stream") ?? []; + const composed = listeners.reduceRight<(payload: unknown, next: () => unknown) => unknown>( + (acc, listener) => (payload, next) => listener(payload, () => acc(payload, next)), + () => base(), + ); + return composed(options, () => base()) as AsyncIterable; +} + +/** Mock proxy: manifest + session-name + the incremental-turn probe (GET), + * advertised enabled so the conversion seam actually plans turns. */ +function startMockProxy(): Promise<{ origin: string; server: http.Server }> { + const server = http.createServer((req, res) => { + const url = req.url ?? ""; + if (url === "/__bili/plugin/manifest") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + version: "0.1.119", + tools: { anthropic: [{ name: "compress", description: "Compress a range of messages", input_schema: { type: "object", properties: { summary: { type: "string" } }, required: ["summary"] } }] }, + })); + return; + } + if (url === "/__bili/plugin/session-name") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({})); + return; + } + if (url === "/__bili/plugin/turn") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, protocol: 1, feature: "pluginIncrementalTurns", enabled: true, storage: "unified", serverHistory: true })); + return; + } + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "not found" })); + }); + return new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const addr = server.address() as { port: number }; + resolve({ origin: `http://127.0.0.1:${addr.port}`, server }); + }); + }); +} + +async function waitFor(predicate: () => boolean, what: string): Promise { + const deadline = Date.now() + 15_000; + while (!predicate()) { + if (Date.now() > deadline) throw new Error(`timeout waiting for ${what}`); + await new Promise((resolve) => setTimeout(resolve, 20)); + } +} + +async function withSetup(fn: (handle: CtxHandle) => Promise): Promise { + __resetConversationRecallForTests(); + __resetIncrementalClientForTests(); + const proxy = await startMockProxy(); + const home = mkdtempSync(join(tmpdir(), "dsh-msgids-")); + const stateHome = mkdtempSync(join(tmpdir(), "dsh-msgids-state-")); + const saved: Record = {}; + for (const key of ["BILLION_CONTEXT_PROXY", "DSH_HOME", "XDG_STATE_HOME", "BILLION_CONTEXT_PLUGIN", "BILI_NATIVE_DSH", "BILI_PROVIDER_REWRITES"]) { + saved[key] = process.env[key]; + } + process.env.BILLION_CONTEXT_PROXY = proxy.origin; + process.env.DSH_HOME = home; + process.env.XDG_STATE_HOME = stateHome; + delete process.env.BILLION_CONTEXT_PLUGIN; + delete process.env.BILI_NATIVE_DSH; + delete process.env.BILI_PROVIDER_REWRITES; + _resetRegisterForTest(proxy.origin); + const handle = makeCtx(); + try { + apply(handle.ctx); + await waitFor(() => handle.registered.length === 1, "manifest tool registration"); + await _stateToolsReadyForTest(); + await fn(handle); + } finally { + await _settleNativeForTest(); + proxy.server.close(); + proxy.server.closeIdleConnections?.(); + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmSync(home, { recursive: true, force: true }); + rmSync(stateHome, { recursive: true, force: true }); + } +} + +/** The "adapter": inside the llm/stream scope, drive beforeSend with the + * outgoing wire body and capture the (possibly swapped) init. */ +async function dispatchTurn( + handle: CtxHandle, + options: Record, + wireMessages: Array<{ role: string; content: string }>, +): Promise<{ init: RequestInit; marker: string | null }> { + const beforeSend = _stateBeforeSendForTest(); + assert.ok(beforeSend, "beforeSend seam armed"); + let init: RequestInit | undefined; + const base = async function* (): AsyncIterable { + init = { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "claude-test", max_tokens: 64, system: "be brief", messages: wireMessages }) }; + await beforeSend(MODEL_URL, init); + yield 1; + }; + const stream = runWaterfall(handle.events, options, base); + const it = stream[Symbol.asyncIterator](); + const first = await it.next(); + assert.deepEqual(first, { done: false, value: 1 }, "adapter stream advanced"); + const done = await it.return?.(undefined as never); + assert.ok(done?.done, "adapter stream closed"); + assert.ok(init !== undefined, "beforeSend ran with the wire body"); + return { init, marker: new Headers(init.headers).get("x-bili-plugin-turn") }; +} + +type TurnBody = { conversationId?: string; sinceSeq?: number; resync?: boolean; messages?: unknown[]; msgIds?: string[] }; + +/** Ack one converted turn so the next dispatch on the same URL passes the + * single-flight guard (each case plans exactly one pending turn). */ +function ackTurn(init: RequestInit, applied: number): void { + const onResponse = _stateOnResponseForTest(); + assert.ok(onResponse); + const turnId = new Headers(init.headers).get("x-bili-turn-id"); + assert.ok(turnId, "turn id stamped on the converted init"); + onResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(applied) } }), { "x-bili-turn-id": turnId }); +} + +test("llm/stream ids ride the incremental turn and stay sliced to the delta messages", async () => { + await withSetup(async (handle) => { + // No ambient initiator — the whole identity (sid + ids) rides the + // llm/stream options, exactly the shape the scope exists for. + handle.setInitiator(undefined); + + // Turn 1 (seed): 2 host messages with ids, 2 wire messages. + const hostMsgs1 = [ + { id: "u-1", role: "user", content: "hello" }, + { id: "a-1", role: "assistant", content: "hi there" }, + ]; + const turn1 = await dispatchTurn(handle, { sessionId: "sess-msgids-1", messages: hostMsgs1 }, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, + ]); + assert.equal(turn1.marker, "1", "seed converted to the incremental lane"); + const body1 = JSON.parse(turn1.init.body as string) as TurnBody; + assert.equal(body1.conversationId, "sess-msgids-1"); + assert.equal(body1.messages?.length, 2, "seed carries the full history"); + assert.deepEqual(body1.msgIds, ["u-1", "a-1"], "seed pins the host ids in order"); + + // Ack the seed (2 applied rows) so turn 2 plans a delta. + const onResponse = _stateOnResponseForTest(); + assert.ok(onResponse); + const turnId1 = new Headers(turn1.init.headers).get("x-bili-turn-id"); + assert.ok(turnId1, "turn id stamped on the seed"); + onResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "2" } }), { "x-bili-turn-id": turnId1 }); + + // Turn 2 (delta): one NEW host message appended — only its id may + // ride the envelope (the ids must follow the delta slice, never the + // full history). + const hostMsgs2 = [...hostMsgs1, { id: "u-2", role: "user", content: "and again" }]; + const turn2 = await dispatchTurn(handle, { sessionId: "sess-msgids-1", messages: hostMsgs2 }, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, + { role: "user", content: "and again" }, + ]); + assert.equal(turn2.marker, "1", "delta converted"); + const body2 = JSON.parse(turn2.init.body as string) as TurnBody; + assert.equal(body2.sinceSeq, 2, "delta against the acked base"); + assert.equal(body2.messages?.length, 1, "delta carries only the new message"); + assert.deepEqual(body2.msgIds, ["u-2"], "delta ids are the SLICE — the acked ids are not re-sent"); + }); +}); + +test("one id-less host message drops the whole msgIds field (all-or-nothing)", async () => { + await withSetup(async (handle) => { + handle.setInitiator(undefined); + const turn = await dispatchTurn(handle, { sessionId: "sess-msgids-2", messages: [ + { id: "u-1", role: "user", content: "hello" }, + { role: "assistant", content: "no id on me" }, + ] }, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "no id on me" }, + ]); + assert.equal(turn.marker, "1", "turn still converts — ids are optional"); + const body = JSON.parse(turn.init.body as string) as TurnBody; + assert.equal("msgIds" in body, false, "no partial id array is ever sent"); + }); +}); + +test("sidecar-shaped dispatch (no messages/no ids on the options) carries no msgIds", async () => { + await withSetup(async (handle) => { + handle.setInitiator(undefined); + // Title-gen / compaction sidecar: a sessionId but an options object + // without host messages (or ids) — the turn must not invent any. + // (Distinct wire content per shape: an identical re-send of an + // already-acked set is a retry and legitimately plans "skip".) + const shapes: Array<{ options: Record; wire: string }> = [ + { options: { sessionId: "sess-msgids-3" }, wire: "summarize this session" }, + { options: { sessionId: "sess-msgids-3", messages: [] }, wire: "generate a session title" }, + ]; + for (const shape of shapes) { + const turn = await dispatchTurn(handle, shape.options, [{ role: "user", content: shape.wire }]); + assert.equal(turn.marker, "1", "sidecar turn still converts — ids are optional"); + const body = JSON.parse(turn.init.body as string) as TurnBody; + assert.equal("msgIds" in body, false, `sidecar options (${shape.options.messages === undefined ? "no messages" : "empty messages"}) send no ids`); + ackTurn(turn.init, body.messages?.length ?? 0); + } + }); +}); + +test("captured ids that cannot line up with the sent messages never ship (degrade over mispair)", async () => { + await withSetup(async (handle) => { + handle.setInitiator(undefined); + // 2 host ids vs 3 outgoing wire messages — a same-length guess could + // misattribute; the field must be dropped entirely. + const turn = await dispatchTurn(handle, { sessionId: "sess-msgids-4", messages: [ + { id: "u-1", role: "user", content: "hello" }, + { id: "a-1", role: "assistant", content: "hi" }, + ] }, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi" }, + { role: "user", content: "extra wire message the adapter added" }, + ]); + const body = JSON.parse(turn.init.body as string) as TurnBody; + assert.equal(body.messages?.length, 3, "seed carries the actual wire history"); + assert.equal("msgIds" in body, false, "count mismatch ⇒ no ids — never a shifted pairing"); + }); +}); + +test("ids the server would hard-fail on (duplicate/reserved/control) are dropped locally, turn still converts", async () => { + await withSetup(async (handle) => { + handle.setInitiator(undefined); + const cases: Array<{ name: string; messages: unknown[]; wire: string[] }> = [ + { name: "duplicate id", messages: [ + { id: "u-1", role: "user", content: "hello" }, + { id: "u-1", role: "user", content: "again" }, + ], wire: ["hello", "again"] }, + { name: "reserved acp_summary_ prefix", messages: [ + { id: "acp_summary_x", role: "user", content: "third" }, + ], wire: ["third"] }, + { name: "control character in id", messages: [ + { id: "u-1\n", role: "user", content: "fourth" }, + ], wire: ["fourth"] }, + ]; + for (const c of cases) { + const wire = c.wire.map((content) => ({ role: "user", content })); + const turn = await dispatchTurn(handle, { sessionId: "sess-msgids-5", messages: c.messages }, wire); + const body = JSON.parse(turn.init.body as string) as TurnBody; + assert.equal(turn.marker, "1", `${c.name}: turn still converts`); + assert.equal("msgIds" in body, false, `${c.name}: poisoned id array never leaves the host`); + ackTurn(turn.init, body.messages?.length ?? 0); + } + }); +}); diff --git a/tests/omp-msgids.test.ts b/tests/omp-msgids.test.ts new file mode 100644 index 0000000000..44536b6cae --- /dev/null +++ b/tests/omp-msgids.test.ts @@ -0,0 +1,267 @@ +// #4491 omp host wiring for plugin-lane msgids: omp's session branch carries +// stable entry ids (randomUUID tail), and the incremental lane can pin them as +// server-side message identity. The wiring is two halves — the extension +// event (pi.ts omp branch of before_provider_request) snapshots +// ompBranchMsgIdEntries(sid) into noteHostMsgIdHint, and the fetch-layer +// planner (omp-native ompIncrementalBeforeSend) aligns that hint against the +// ACTUALLY-sent messages (count + role, pairwise) and passes the ids into +// planIncrementalTurn, which splices `msgIds` parallel to the messages the +// mode sends. All-or-nothing: any misalignment drops the WHOLE field (the +// lane hard-400s poisoned ids and a misaligned id would pin the wrong +// identity), so every degrade path here asserts the field is ABSENT, never +// partial. Harness mirrors tests/omp-native-incremental.test.ts (fake lane +// origin + the two exported seams); every converting round is acked because +// the #3378 single-flight guard holds the URL slot until its ack lands. +import { after, test } from "node:test"; +import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; + +process.env.NODE_TEST_CONTEXT = "1"; + +const { ompIncrementalBeforeSend, ompIncrementalOnResponse, _setOmpOriginForTest, __resetOmpIncrementalForTests } = await import("../src/agent/omp-native.ts"); +const { __resetIncrementalClientForTests, noteHostMsgIdHint, noteIncrementalAck, planIncrementalTurn } = await import("../src/agent/incremental-client.ts"); +const { ompBranchMsgIdEntries } = await import("../src/agent/pi.ts"); + +const MODEL_URL = "https://api.openai-anthropic.example/v1/messages"; + +type CtxShape = Parameters[0]; + +async function fakeLane(): 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: true, storage: "unified" })); + return; + } + res.writeHead(404); res.end("{}"); + }); + await new Promise((r) => srv.listen(0, "127.0.0.1", r)); + const { port } = srv.address() as AddressInfo; + return { base: `http://127.0.0.1:${port}`, close: () => new Promise((r) => srv.close(() => r())) }; +} + +function initOf(body: unknown): RequestInit { + return { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }; +} + +function headersOf(h: Headers): Record { + const out: Record = {}; + for (const [k, v] of h) out[k.toLowerCase()] = v; + return out; +} + +/** omp branch entry shapes (pi-family session tree): message entries carry a + * stable id; model_change / native-compaction entries ride alongside without + * one (the compaction summary references firstKeptEntryId instead). */ +const msgEntry = (id: string, role: "user" | "assistant", text: string) => ({ type: "message", id, message: { role, content: [{ type: "text", text }] }, timestamp: "2026-10-24T00:00:00.000Z" }); +const wire = (role: "user" | "assistant", text: string) => ({ role, content: text }); + +/** Drive the exact stash the omp extension performs per model request: the + * (id, role) extraction from the live branch, keyed by the session id the + * prompt_cache_key will carry. */ +function stashHint(sid: string, branch: unknown[]): void { + const ctx = { sessionManager: { getSessionId: () => sid, getBranch: () => branch } } as CtxShape; + noteHostMsgIdHint(sid, ompBranchMsgIdEntries(ctx)); +} + +/** Convert one outgoing request through the fetch-layer seam; `ack` settles + * the round so the next conversion on this URL is not single-flight held. */ +async function convert(sid: string, messages: unknown[]): Promise<{ body: Record; ack: (appliedSeq: number) => void }> { + const init = initOf({ model: "claude-test", max_tokens: 512, prompt_cache_key: sid, messages }); + await ompIncrementalBeforeSend(MODEL_URL, init); + const headers = init.headers as Headers; + const normalized = headersOf(headers); + return { + body: JSON.parse(String(init.body)) as Record, + ack: (appliedSeq: number) => { + ompIncrementalOnResponse(MODEL_URL, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": String(appliedSeq) } }), normalized); + }, + }; +} + +const lanes: { close: () => Promise }[] = []; +after(async () => { + for (const l of lanes) await l.close(); + _setOmpOriginForTest(undefined); + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); +}); + +// ── ① aligned ids ride the seed and the delta, parallel to the sent set ── + +test("omp msgids: a 1:1-aligned branch carries msgIds on the seed turn and the delta suffix", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + const sid = "omp-msgids-1"; + + // Seed: branch [u1, a1, u2] ↔ wire [u1, a1, u2] — ids ride in order. + const seedBranch = [msgEntry("e1a2b3c4", "user", "hello"), msgEntry("e5f6a7b8", "assistant", "hi there"), msgEntry("e9c0d1e2", "user", "continue")]; + const seedWire = [wire("user", "hello"), wire("assistant", "hi there"), wire("user", "continue")]; + stashHint(sid, seedBranch); + const seed = await convert(sid, seedWire); + assert.equal(seed.body.conversationId, sid); + assert.equal(seed.body.resync, true, "first turn seeds"); + assert.deepEqual(seed.body.msgIds, ["e1a2b3c4", "e5f6a7b8", "e9c0d1e2"], "seed msgIds = full id sequence in message order"); + seed.ack(3); + + // Delta: branch grew by [a2, u3] — only the suffix ids ride, parallel to + // the delta messages the envelope actually sends. + const deltaBranch = [...seedBranch, msgEntry("f0e1d2c3", "assistant", "answer two"), msgEntry("f4b5a6d7", "user", "next question")]; + const deltaWire = [...seedWire, wire("assistant", "answer two"), wire("user", "next question")]; + stashHint(sid, deltaBranch); + const delta = await convert(sid, deltaWire); + assert.equal(delta.body.sinceSeq, 3, "acked watermark rides"); + assert.equal((delta.body.messages as unknown[]).length, 2, "delta sends only the suffix"); + assert.deepEqual(delta.body.msgIds, ["f0e1d2c3", "f4b5a6d7"], "delta msgIds = the suffix ids, aligned with the delta messages"); +}); + +// ── ② any id-less entry drops the WHOLE field (all-or-nothing) ── + +test("omp msgids: an entry without a stable id drops the whole field", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + const sid = "omp-msgids-noid"; + + stashHint(sid, [msgEntry("e1a2b3c4", "user", "hello"), msgEntry("", "assistant", "legacy entry without id")]); + const round = await convert(sid, [wire("user", "hello"), wire("assistant", "legacy entry without id")]); + assert.equal(round.body.resync, true, "the turn itself still converts"); + assert.equal("msgIds" in round.body, false, "no id for one entry ⇒ no field at all, never a partial array"); +}); + +test("omp msgids: a native-compaction summary (no message id) drops the whole field", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + const sid = "omp-msgids-compaction"; + + // omp native compaction: the branch keeps [compaction(summary + + // firstKeptEntryId), kept message] while the wire sends [summary-as-user, + // kept message] — the summary has no message id, so the counts diverge + // and the field must vanish. + stashHint(sid, [ + { type: "compaction", id: "cx1", compaction: { firstKeptEntryId: "e2", summary: "earlier turns summarized" } }, + msgEntry("e2", "user", "kept turn"), + ]); + const round = await convert(sid, [wire("user", "Summary of earlier turns:\nearlier turns summarized"), wire("user", "kept turn")]); + assert.equal(round.body.resync, true); + assert.equal("msgIds" in round.body, false, "summary entry has no id ⇒ alignment impossible ⇒ no field"); +}); + +// ── ③ rewind: the carried ids follow the SURVIVING messages ── + +test("omp msgids: after a rewind the resync carries exactly the surviving id sequence", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + const sid = "omp-msgids-rewind"; + + const fullBranch = [msgEntry("r1", "user", "one"), msgEntry("r2", "assistant", "two"), msgEntry("r3", "user", "three")]; + const fullWire = [wire("user", "one"), wire("assistant", "two"), wire("user", "three")]; + stashHint(sid, fullBranch); + const seeded = await convert(sid, fullWire); + seeded.ack(3); + + // Rewind (leaf re-point) drops the last entry: branch and wire both + // shrink to the surviving path — the plan resyncs (prefix no longer + // matches the acked set) and the ids align with what is actually sent. + const keptBranch = fullBranch.slice(0, 2); + const keptWire = fullWire.slice(0, 2); + stashHint(sid, keptBranch); + const resync = await convert(sid, keptWire); + assert.equal(resync.body.resync, true, "shrunk history resyncs wholesale"); + assert.equal((resync.body.messages as unknown[]).length, 2); + assert.deepEqual(resync.body.msgIds, ["r1", "r2"], "rewind carries the SURVIVING ids, aligned with the sent messages"); + resync.ack(2); + + // The next append after the rewind rides a delta whose id is the fresh + // branch entry — never the rewound-away one. + stashHint(sid, [...keptBranch, msgEntry("r4", "user", "three (edited)")]); + const post = await convert(sid, [...keptWire, wire("user", "three (edited)")]); + assert.equal((post.body.messages as unknown[]).length, 1, "delta after the rewind's ack"); + assert.deepEqual(post.body.msgIds, ["r4"], "post-rewind delta carries the fresh entry's id"); +}); + +// ── gate matrix: the degrade direction (#3617 both-ways discipline) ── + +test("omp msgids: extraction skips non-message entries; shape mismatches degrade to no field", async () => { + __resetIncrementalClientForTests(); + __resetOmpIncrementalForTests(); + const lane = await fakeLane(); + lanes.push(lane); + _setOmpOriginForTest(lane.base); + + // model_change entries ride the branch but have no message slot — the + // extractor skips them and the alignment still carries. + const sid = "omp-msgids-modelchange"; + stashHint(sid, [ + msgEntry("m1", "user", "hello"), + { type: "model_change", id: "mc1", from: "model-a", to: "model-b" }, + msgEntry("m2", "assistant", "hi"), + ]); + const body = await convert(sid, [wire("user", "hello"), wire("assistant", "hi")]); + assert.deepEqual(body.body.msgIds, ["m1", "m2"], "model_change entries are transparent to the alignment"); + body.ack(2); + + // Role mismatch between branch and wire (aux/persona divergence) ⇒ no field. + const sid2 = "omp-msgids-roles"; + stashHint(sid2, [msgEntry("p1", "user", "hello"), msgEntry("p2", "assistant", "hi")]); + const body2 = await convert(sid2, [wire("user", "hello"), wire("user", "hi")]); + assert.equal("msgIds" in body2.body, false, "role divergence ⇒ provably not parallel ⇒ no field"); + body2.ack(2); + + // No hint at all (host without getBranch stashes nothing; a stale hint + // ages out) ⇒ no field, conversion itself unaffected. + const body3 = await convert("omp-msgids-nohint", [wire("user", "hello")]); + assert.equal(body3.body.resync, true); + assert.equal("msgIds" in body3.body, false, "no hint ⇒ no field — the turn still converts"); + body3.ack(1); + + // Length mismatch (branch out of sync with the sent history) ⇒ no field. + const sid4 = "omp-msgids-len"; + stashHint(sid4, [msgEntry("l1", "user", "hello")]); + const body4 = await convert(sid4, [wire("user", "hello"), wire("user", "extra")]); + assert.equal("msgIds" in body4.body, false, "count divergence ⇒ no field"); + + // getBranch throwing ⇒ the extractor yields [] ⇒ no field (never a stale hint). + const ctxThrow = { sessionManager: { getSessionId: () => "omp-msgids-throw", getBranch: () => { throw new Error("boom"); } } } as CtxShape; + assert.deepEqual(ompBranchMsgIdEntries(ctxThrow), [], "throwing getBranch degrades to an empty hint"); + assert.deepEqual(ompBranchMsgIdEntries({} as CtxShape), [], "missing sessionManager degrades to an empty hint"); +}); + +// ── claim gate: the client-side mirror of the lane's reuse-400 ── + +test("omp msgids: a re-planned delta never re-claims ids the lane may already hold", () => { + __resetIncrementalClientForTests(); + const sid = "omp-msgids-claim"; + const seedMsgs = [{ role: "user", content: "q1" }, { role: "assistant", content: "a1" }]; + const seed = planIncrementalTurn({ conversationId: sid, messages: seedMsgs, system: undefined, msgIds: ["c1", "c2"] }); + assert.deepEqual(JSON.parse((seed as { body: string }).body).msgIds, ["c1", "c2"], "seed carries the ids"); + noteIncrementalAck(sid, 2, seedMsgs, undefined); + + // First delta plan carries the fresh ids… + const deltaMsgs = [...seedMsgs, { role: "user", content: "q2" }]; + const first = planIncrementalTurn({ conversationId: sid, messages: deltaMsgs, system: undefined, msgIds: ["c1", "c2", "c3"] }); + assert.deepEqual(JSON.parse((first as { body: string }).body).msgIds, ["c3"], "delta carries only the suffix id"); + + // …a re-plan of the SAME delta without an intervening ack (host retry + // whose first attempt may have landed server-side) drops them: re-sending + // c3 would trip the lane's "msgIds reuse an existing canonical id" 400. + const retry = planIncrementalTurn({ conversationId: sid, messages: deltaMsgs, system: undefined, msgIds: ["c1", "c2", "c3"] }); + assert.equal("msgIds" in JSON.parse((retry as { body: string }).body), false, "re-planned delta drops the claimed ids — the turn still lands on derived ids"); + + // A resync re-claims wholesale (the lane replaces the canonical on + // resync:true), so the same ids may ride a resync again. + const resync = planIncrementalTurn({ conversationId: sid, messages: deltaMsgs, system: undefined, msgIds: ["c1", "c2", "c3"] }, true); + assert.deepEqual(JSON.parse((resync as { body: string }).body).msgIds, ["c1", "c2", "c3"], "resync replaces the claim set wholesale"); +}); diff --git a/tests/opencode-msgids.test.ts b/tests/opencode-msgids.test.ts new file mode 100644 index 0000000000..984a460b98 --- /dev/null +++ b/tests/opencode-msgids.test.ts @@ -0,0 +1,203 @@ +// #4491 host wiring (opencode) — msgIds on incremental-lane turns. +// +// Stakes (all unit-level over the two seams the wiring owns, harness shaped +// like tests/opencode-incremental.test.ts): +// 1. ids flow end to end: the V1 `experimental.chat.messages.transform` +// harvest lands on the planned turn body aligned 1:1 with the wire +// messages — seed carries the full list, the post-ack delta carries +// only the delta's ids (sliced in lockstep with the messages); +// 2. all-or-nothing: one message without a usable id (or with a part that +// cannot be proven to lower 1:1) drops the whole msgIds field; +// 3. V2 shape — the dispatch fires with no V1 hook ever having run +// (http.request sees only the wire) → no msgIds field, degrade safe. +import { after, test } from "node:test"; +import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; + +process.env.NODE_TEST_CONTEXT = "1"; + +const { maybeIncrementalOpencode, opencodeIncrementalOnResponse, __resetOpencodeIncrementalForTests, createV1ServerHooks } = await import("../src/agent/opencode-native.ts"); +const { __resetIncrementalClientForTests } = await import("../src/agent/incremental-client.ts"); +const { __resetOpencodeMsgIdsForTests } = await import("../src/agent/opencode-msgids.ts"); +import type { V2HttpRequestEvent, V2State } from "../src/agent/opencode-v2.ts"; + +const MODEL_URL = "https://api.anthropic.com/v1/messages"; + +async function fakeLane(): 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: true, storage: "unified" })); + return; + } + res.writeHead(404); res.end("{}"); + }); + await new Promise((r) => srv.listen(0, "127.0.0.1", r)); + const { port } = srv.address() as AddressInfo; + return { base: `http://127.0.0.1:${port}`, close: () => new Promise((r) => srv.close(() => r())) }; +} + +function fakeEvent(body: unknown, sid: string): { e: V2HttpRequestEvent; url: string } { + const req = new Request(MODEL_URL, { + method: "POST", + headers: { "content-type": "application/json", authorization: "Bearer sk-test" }, + body: JSON.stringify(body), + }); + const e = { request: req, sessionID: sid } as unknown as V2HttpRequestEvent; + return { e, url: req.url }; +} + +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; +} + +async function plannedBody(e: V2HttpRequestEvent): Promise> { + return JSON.parse(await (e.request as Request).clone().json().then((j) => JSON.stringify(j))) as Record; +} + +/** opencode 1.x messages.transform fire: host messages {info, parts}. */ +function hostMessage(id: string, role: "user" | "assistant", text: string, sid: string): { info: { id: string; role: string; sessionID: string }; parts: unknown[] } { + return { info: { id, role, sessionID: sid }, parts: [{ type: "text", text }] }; +} + +const wmsg = (role: string, text: string) => ({ role, content: [{ type: "text", text }] }); + +const lanes: { close: () => Promise }[] = []; +after(async () => { + for (const l of lanes) await l.close(); + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); +}); + +test("opencode msgids: V1 harvest rides the turn body 1:1 — seed full, delta sliced", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const hooks = createV1ServerHooks(() => lane.base, {}, {}); + + const sid = "ses_msgid_1"; + 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); + const body1 = await plannedBody(t1.e); + assert.equal((body1.messages as unknown[]).length, 1, "seed swap happened"); + assert.deepEqual(body1.msgIds, ["msg_u1"], "seed carries the harvested id aligned 1:1 with the wire message"); + assert.equal((t1.e.request as Request).headers.get("x-bili-plugin-turn"), "1", "marker still stamped"); + + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // next turn: the host re-fires the transform with the settled history — + // the streaming-settled assistant arrives under its own id + 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 t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [wmsg("user", "hello"), wmsg("assistant", "hi there"), wmsg("user", "next")] }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, "delta turn rides only the suffix"); + assert.deepEqual(body2.msgIds, ["msg_a1", "msg_u2"], "delta msgIds sliced in lockstep with the delta messages"); +}); + +test("opencode msgids: one unusable message drops the whole field (all-or-nothing)", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const hooks = createV1ServerHooks(() => lane.base, {}, {}); + const sid = "ses_msgid_2"; + + // a message without a host id → no msgIds at all (never mis-align) + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [ + hostMessage("msg_u1", "user", "hello", sid), + { info: { role: "assistant", sessionID: sid }, parts: [{ type: "text", text: "hi" }] }, + hostMessage("msg_u2", "user", "next", sid), + ] }); + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [wmsg("user", "hello"), wmsg("assistant", "hi"), wmsg("user", "next")] }, sid); + await maybeIncrementalOpencode(t1.e, s); + const body1 = await plannedBody(t1.e); + assert.equal((body1.messages as unknown[]).length, 3, "seed swap happened"); + assert.equal("msgIds" in body1, false, "a missing id drops the field for the whole turn"); + + // a tool-bearing part cannot be proven to lower 1:1 → same degradation + const sid2 = "ses_msgid_2b"; + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [ + hostMessage("msg_t1", "assistant", "calling", sid2), + { info: { id: "msg_t2", role: "assistant", sessionID: sid2 }, parts: [{ type: "tool", tool: "bash", state: {} }] }, + ] }); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [wmsg("assistant", "calling"), { role: "user", content: [{ type: "tool_result", tool_use_id: "t", content: "out" }] }] }, sid2); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal("msgIds" in body2, false, "non-text parts degrade the turn to content-derived identities"); +}); + +test("opencode msgids: V2 shape (no V1 hook fired) carries no msgIds — degrade safe", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const sid = "ses_msgid_3"; + + // http.request sees only the wire: no harvest exists for this session + const msgs1 = [{ role: "user", content: "hello opencode" }]; + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: msgs1 }, sid); + await maybeIncrementalOpencode(t1.e, s); + const body1 = await plannedBody(t1.e); + assert.equal((body1.messages as unknown[]).length, 1, "seed swap happened"); + assert.equal("msgIds" in body1, false, "V2-shaped seed carries no msgIds"); + + 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: "next" }]; + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: msgs2 }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, "delta planned"); + assert.equal("msgIds" in body2, false, "V2-shaped delta carries no msgIds either"); +}); + +test("opencode msgids: content drift between harvest and wire degrades instead of mis-aligning", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const hooks = createV1ServerHooks(() => lane.base, {}, {}); + const sid = "ses_msgid_4"; + + // the wire text differs from the harvested parts → ids would mis-align + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [hostMessage("msg_u1", "user", "hello", sid)] }); + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [wmsg("user", "hello EDITED")] }, sid); + await maybeIncrementalOpencode(t1.e, s); + const body1 = await plannedBody(t1.e); + assert.equal((body1.messages as unknown[]).length, 1, "seed swap happened"); + assert.equal("msgIds" in body1, false, "drifted content never inherits an id it cannot prove"); + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // cache_control on the wire is transport-only — alignment must survive it + const sid2 = "ses_msgid_4b"; + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [hostMessage("msg_c1", "user", "cached", sid2)] }); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: [{ type: "text", text: "cached", cache_control: { type: "ephemeral" } }] }] }, sid2); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.deepEqual(body2.msgIds, ["msg_c1"], "cache_control markers do not break the 1:1 proof"); +}); diff --git a/tests/pi-msgids.test.ts b/tests/pi-msgids.test.ts new file mode 100644 index 0000000000..0f35bdc2c9 --- /dev/null +++ b/tests/pi-msgids.test.ts @@ -0,0 +1,273 @@ +// #4491 pi host wiring: the pi extension carries the session's stable message +// ids on incremental lane turns (`msgIds`, parallel to the lane body's +// messages) so the server canonical (and the unified rows) carry the CLIENT's +// identity instead of content-derived hashes. Stakes: +// ① a turn whose messages all resolve ids ships msgIds aligned with the +// delta messages (seed AND delta), and the lane accepts them (200); +// ② all-or-nothing — one unresolvable message and the WHOLE field is left +// off (the lane falls back to content-derived ids, never a guess); +// ③ resync turns (edit / branch divergence) carry msgIds over the full +// history; +// ④ a delta message that flattens to >1 core message (multi-block content) +// omits msgIds — the lane pins ids 1:1 onto FLATTENED core messages and +// hard-fails (400) on a count mismatch, so the host must not offer ids +// it cannot align to that contract; +// ⑤ tool turns: a tool_use assistant message + its single tool_result match +// by tool-call id and pin their entry ids. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; +import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts"; +import { __resetPiIncrementalOriginalsForTests } from "../src/agent/pi.ts"; +import { createBiliPlugin } from "../src/agent/pi.ts"; + +process.env.NODE_ENV = "test"; +process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-pimsgids-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); + +function makeFakePi() { + const events = new Map unknown)[]>(); + const tools: unknown[] = []; + return { + events, + tools, + on: (event: string, handler: (event: never, ctx: never) => unknown) => { + const list = events.get(event) ?? []; + list.push(handler as (event: unknown, ctx: unknown) => unknown); + events.set(event, list); + }, + registerTool: (tool: unknown) => { tools.push(tool); }, + registerCommand: () => {}, + registerProvider: () => {}, + getSessionName: () => "test", + fireAsync: async (name: string, event: unknown, ctx: unknown) => { + const list = events.get(name) ?? []; + let last: unknown; + for (const h of list) last = await h(event, ctx); + return last; + }, + }; +} + +type LaneBody = { conversationId?: string; sinceSeq?: number; resync?: boolean; messages?: Array<{ role?: string; content?: unknown }>; msgIds?: string[]; system?: unknown }; + +type Harness = { + proxyUrl: string; + baseUrl: string; + pi: ReturnType; + ctx: { sessionManager: { getSessionId: () => string; getBranch: () => unknown }; model: { id: string; contextWindow: number; api: string; baseUrl: string }; cwd: string }; + setBranch: (entries: unknown[]) => void; + /** Stamp headers, swap the payload through the extension, POST it to the + * lane and deliver the ack. Returns the swapped lane body + result. */ + turn: (payload: Record) => Promise<{ body: LaneBody; status: number; seq: number | undefined }>; + close: () => Promise; +}; + +async function withHarness(branch: unknown[], fn: (h: Harness) => Promise): Promise { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.BILI_STORAGE_UNIFIED = "full"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalLaneForTests(); + __resetIncrementalClientForTests(); + __resetPiIncrementalOriginalsForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamUrl = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); + const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + let currentBranch = branch; + const ctx = { + sessionManager: { getSessionId: () => "conv-pi-msgids", getBranch: () => currentBranch.slice() }, + model: { id: "claude-test", contextWindow: 400000, api: "anthropic", baseUrl: `${proxyUrl}/bili/${upstreamUrl}/v1` }, + cwd: dir, + }; + const turn = async (payload: Record) => { + const headers: Record = {}; + await pi.fireAsync("before_provider_headers", { headers }, ctx); + const body = await pi.fireAsync("before_provider_request", { payload }, ctx) as LaneBody; + const res = await fetch(`${ctx.model.baseUrl}/messages`, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) }); + const seqHeader = res.headers.get("x-bili-applied-seq"); + const ackHeaders: Record = {}; + res.headers.forEach((v, k) => { ackHeaders[k] = v; }); + await res.text(); + await pi.fireAsync("after_provider_response", { status: res.status, headers: ackHeaders }, ctx); + return { body, status: res.status, seq: seqHeader !== null && /^\d+$/.test(seqHeader) ? Number(seqHeader) : undefined }; + }; + try { + await pi.fireAsync("session_start", {}, ctx); + const deadline = Date.now() + 15000; + while (pi.tools.length < 2 && Date.now() < deadline) await new Promise((r) => setTimeout(r, 25)); + assert.ok(pi.tools.length >= 2, `tools registered: ${pi.tools.length}`); + await fn({ proxyUrl, baseUrl: `${ctx.model.baseUrl}/messages`, pi, ctx, setBranch: (entries) => { currentBranch = entries; }, turn, close: async () => undefined }); + } finally { + await new Promise((resolve) => proxy.close(() => resolve())); + store.closeQuietly(); + await new Promise((resolve) => upstream.close(() => resolve())); + } +} + +// Session-entry shapes (pi SessionMessageEntry: {type:"message", id, parentId, +// timestamp, message}) with the AgentMessage payload the walker reads. +const entry = (id: string, message: Record): Record => ({ type: "message", id, parentId: null, timestamp: "2026-10-24T00:00:00.000Z", message }); +const userMsg = (content: string): Record => ({ role: "user", content, timestamp: 1 }); +const assistantMsg = (blocks: unknown[]): Record => ({ role: "assistant", content: blocks, api: "anthropic", provider: "anthropic", model: "claude-test", stopReason: "stop", timestamp: 1 }); +const toolResultMsg = (toolCallId: string, text: string): Record => ({ role: "toolResult", toolCallId, toolName: "bash", content: [{ type: "text", text }], isError: false, timestamp: 1 }); + +const PAYLOAD = (messages: unknown[]): Record => ({ model: "claude-test", max_tokens: 1024, system: "be brief", messages }); + +test("pi msgids: seed and delta turns carry msgIds aligned with the delta messages", async () => { + const hello = "hello".repeat(50); + await withHarness([entry("e1", userMsg(hello))], async (h) => { + // turn 1: seed — the full history rides the lane body with its ids. + const t1 = await h.turn(PAYLOAD([{ role: "user", content: hello }])); + assert.equal(t1.status, 200, "the lane accepts the pinned seed"); + assert.equal(t1.seq, 1); + assert.deepEqual(t1.body.msgIds, ["e1"], "seed msgIds = the branch entry ids"); + assert.equal(t1.body.messages?.length, 1); + assert.equal(t1.body.msgIds?.length, t1.body.messages?.length, "msgIds parallel to messages"); + + // turn 2: the branch grew the assistant reply + the next user turn. + h.setBranch([ + entry("e1", userMsg(hello)), + entry("e2", assistantMsg([{ type: "text", text: "answer" }])), + entry("e3", userMsg("next".repeat(50))), + ]); + const t2 = await h.turn(PAYLOAD([ + { role: "user", content: hello }, + { role: "assistant", content: [{ type: "text", text: "answer" }] }, + { role: "user", content: "next".repeat(50) }, + ])); + assert.equal(t2.status, 200, "the lane accepts the pinned delta"); + assert.equal(t2.body.sinceSeq, 1, "turn 2 is a delta"); + assert.equal(t2.body.messages?.length, 2, "delta ships only the suffix"); + assert.deepEqual(t2.body.messages?.map((m) => m.role), ["assistant", "user"]); + assert.deepEqual(t2.body.msgIds, ["e2", "e3"], "delta msgIds = the suffix entry ids, aligned with messages"); + assert.equal(t2.body.msgIds?.length, t2.body.messages?.length, "msgIds parallel to messages"); + }); +}); + +test("pi msgids: one unresolvable message omits the whole field (all-or-nothing)", async () => { + const hello = "hello".repeat(50); + // The branch holds an entry only for the first message — the assistant + // reply and the follow-up have no entries (e.g. messages injected outside + // the session, or a shape the walker cannot prove). No partial pinning. + await withHarness([entry("e1", userMsg(hello))], async (h) => { + const t = await h.turn(PAYLOAD([ + { role: "user", content: hello }, + { role: "assistant", content: [{ type: "text", text: "answer" }] }, + { role: "user", content: "next".repeat(50) }, + ])); + assert.ok(!("msgIds" in t.body), "no msgIds when any message has no provable id"); + assert.equal(t.status, 200, "the turn itself still works — content-derived ids"); + assert.equal(t.seq, 3, "seed applied the full history"); + }); +}); + +test("pi msgids: a resync turn carries msgIds over the full diverged history", async () => { + const hello = "hello".repeat(50); + await withHarness([entry("e1", userMsg(hello))], async (h) => { + const t1 = await h.turn(PAYLOAD([{ role: "user", content: hello }])); + assert.equal(t1.status, 200); + + // The client edited the head / rewound the tree: a fresh branch whose + // prefix no longer matches the acked one → the plan re-seeds with + // resync:true and the FULL history — ids ride along. + const edited = "hello-edited".repeat(50); + h.setBranch([ + entry("f1", userMsg(edited)), + entry("f2", assistantMsg([{ type: "text", text: "answer2" }])), + entry("f3", userMsg("next".repeat(50))), + ]); + const t2 = await h.turn(PAYLOAD([ + { role: "user", content: edited }, + { role: "assistant", content: [{ type: "text", text: "answer2" }] }, + { role: "user", content: "next".repeat(50) }, + ])); + assert.equal(t2.status, 200, "resync with ids must not be refused"); + assert.equal(t2.body.resync, true, "diverged prefix forces a resync"); + assert.equal(t2.body.messages?.length, 3, "resync ships the full history"); + assert.deepEqual(t2.body.msgIds, ["f1", "f2", "f3"], "resync msgIds cover the whole branch"); + assert.equal(t2.body.msgIds?.length, t2.body.messages?.length, "msgIds parallel to messages"); + }); +}); + +test("pi msgids: a multi-block delta message omits msgIds (flatten contract, no 400)", async () => { + const hello = "hello".repeat(50); + await withHarness([entry("e1", userMsg(hello))], async (h) => { + const t1 = await h.turn(PAYLOAD([{ role: "user", content: hello }])); + assert.equal(t1.status, 200); + + // Assistant reply says something AND calls a tool: the wire assistant + // message carries 2 blocks, which the lane flattens into 2 core + // messages — an id array parallel to the WIRE messages would 400 + // (msgid length-mismatch), so the field must be withheld entirely. + h.setBranch([ + entry("e1", userMsg(hello)), + entry("e2", assistantMsg([{ type: "text", text: "checking" }, { type: "toolCall", id: "t1", name: "bash", arguments: { cmd: "ls" } }])), + entry("e3", userMsg("next".repeat(50))), + ]); + const t2 = await h.turn(PAYLOAD([ + { role: "user", content: hello }, + { role: "assistant", content: [{ type: "text", text: "checking" }, { type: "tool_use", id: "t1", name: "bash", input: { cmd: "ls" } }] }, + { role: "user", content: "next".repeat(50) }, + ])); + assert.ok(!("msgIds" in t2.body), "multi-block delta message → no msgIds (degrade, never a 400)"); + assert.equal(t2.status, 200, "the turn still applies via content-derived ids"); + assert.equal(t2.seq, 4, "2-block assistant + user flatten to 3 core messages past the seed"); + }); +}); + +test("pi msgids: tool turns pin tool_use and tool_result entry ids by call id", async () => { + const hello = "hello".repeat(50); + await withHarness([entry("e1", userMsg(hello))], async (h) => { + const t1 = await h.turn(PAYLOAD([{ role: "user", content: hello }])); + assert.equal(t1.status, 200); + + h.setBranch([ + entry("e1", userMsg(hello)), + entry("e2", assistantMsg([{ type: "toolCall", id: "t1", name: "bash", arguments: { cmd: "ls" } }])), + entry("e3", toolResultMsg("t1", "file-a\nfile-b")), + entry("e4", userMsg("next".repeat(50))), + ]); + const t2 = await h.turn(PAYLOAD([ + { role: "user", content: hello }, + { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "bash", input: { cmd: "ls" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "file-a\nfile-b" }] }, + { role: "user", content: "next".repeat(50) }, + ])); + assert.equal(t2.status, 200, "the lane accepts the pinned tool delta"); + assert.deepEqual(t2.body.msgIds, ["e2", "e3", "e4"], "tool_use + tool_result + follow-up pin their entry ids"); + assert.equal(t2.body.msgIds?.length, t2.body.messages?.length, "msgIds parallel to messages"); + }); +});