diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 5cae5d969a..41cb876080 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -24,6 +24,7 @@ export const LIVE_FILES = [ "tests/index-entry-guard.test.ts", "tests/issue1958-routed-warn.test.ts", "tests/issue2752-forward-partial-outcome.test.ts", + "tests/issue3472-mcp-structured-text.test.ts", "tests/issue4341-early-twin-adoption.test.ts", "tests/issue760-percall-conversation.test.ts", "tests/issue927-opencode-install.test.ts", diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 916500bdae..a16b2b299f 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -358,7 +358,7 @@ function round1(n: number): number { * sum of first-render token counts of all refs chronologically before it. * Refs are assigned in message order and never reused (kernel contract), * so numeric order IS view order. */ -function prefixTokensBeforeRef(session: Session, ref: string): number { +export function prefixTokensBeforeRef(session: Session, ref: string): number { const byRef = session.state?.messageRefs?.byRef; const snap = session.state?.tokenSnapshot; if (!byRef || !snap || refNum(ref) === 0) return 0; @@ -1276,6 +1276,40 @@ export function recordCacheSample( viewBefore: f.V, viewAfter: f.Vp, })); + // #3307 P2-A: f.X (firstFoldStartTokens) is prefixTokensBeforeRef over the + // client's UN-SHRUNKEN resent history — for a stateless proxy client that far + // exceeds the billed input, so decomposeSample zeroes structuralExcess and the + // fold's re-pay falls into the unattributed seam bucket. When a genuine + // bili-side mid-history break is present, measure the stable prefix straight + // off the wire instead: the byte-LCP between this outbound and the previous + // marks where a summary replaced folded content, so (lcpBytes / cur.bytes) * + // billed input is a same-space lower bound on the pre-fold prefix. min() with + // the snapshot X leaves already-correct folds untouched. Gated to mid-list + // breaks only (a tail append lands lcpBytes near the end -> xMeasured ~= input + // -> no effect) and never on a client-side inbound rewrite (#3023), whose + // earlier divergence would understate the fold and mis-credit it. + if (pending.length > 0 && known && s.input > 0) { + const rawMissed = s.input - effCached; + if (rawMissed > 8192 && rawMissed > 0.3 * s.input) { + const curB = session.transient?.sentBody; + const prevB = session.transient?.settledBody; + if (curB !== undefined && prevB !== undefined && curB.bytes <= SEAM_BODY_CAP && prevB.bytes <= SEAM_BODY_CAP) { + const curIn = session.transient?.inBody; + const prevIn = session.transient?.settledInBody; + const clientBreak = curIn !== undefined && prevIn !== undefined && curIn.msgs <= INBOUND_MSG_CAP && prevIn.msgs <= INBOUND_MSG_CAP && (() => { + const k = firstDivergentInboundMsg(prevIn, curIn); + return k !== null && k < Math.min(prevIn.msgs, curIn.msgs); + })(); + if (!clientBreak) { + const lcp = seamLcp(prevB.head, curB.head); + if (lcp.msgIndex !== null && lcp.prevMsgs > 0 && lcp.msgIndex < lcp.prevMsgs && lcp.lcpBytes > 0 && lcp.lcpBytes < curB.head.byteLength) { + const xMeasured = Math.round(s.input * (lcp.lcpBytes / curB.bytes)); + for (const p of pending) p.firstFoldStartTokens = Math.min(p.firstFoldStartTokens ?? Number.POSITIVE_INFINITY, xMeasured); + } + } + } + } + } const rawDec = decomposeSample( prevLine ? { at: prevLine.at, input: prevLine.input, cached: prevLine.cached } : null, { at: s.at, input: s.input, cached: effCached }, @@ -2282,6 +2316,12 @@ export function handleAcpCache(session: Session, args?: Record) // #2867: proxy-side mode/projection switches get their own section only // when observed — a session that never switches modes stays byte-identical. const projText = report.projSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.projSwitches, detail, "PROJECTION SWITCHES (proxy mode/projection change — bili's own doing, e.g. self-heal degrade-to-proxy)") : ""; + // #3307: wire/upstream/restart carry per-event data in the report (switchEvents / + // restartEvents) but only their aggregate counts reached CACHE INVALIDATION — no + // per-line timestamps, so cross-checks (e.g. seam-vs-restart) were impossible. + const wireText = report.wireSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.wireSwitches, detail, "WIRE SWITCHES (client switched protocol wire between requests)") : ""; + const upstreamText = report.upstreamSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.upstreamSwitches, detail, "UPSTREAM SWITCHES (relay/upstream origin changed between requests)") : ""; + const restartText = report.restartDrops.count > 0 ? "\n\n" + formatModelSwitches(report.restartDrops, detail, "RESTART/REFORK (first bill under a fresh daemon boot — prefix re-billed once)") : ""; if (detail === "full" && report.lines.length > FULL_DETAIL_LINES) { const dropped = report.lines.length - FULL_DETAIL_LINES; const capped = formatCacheReport( @@ -2289,9 +2329,9 @@ export function handleAcpCache(session: Session, args?: Record) session.id, { detail }, ); - return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + "\n\n" + formatInvalidation(report))); + return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report))); } - return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + "\n\n" + formatInvalidation(report))); + return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report))); } catch (err) { loggerLog("warn", `[${session.id}] [acp_cache] report failed: ${String(err)}`); return toolFail(`[acp_cache FAILED: ${String(err)}]`); @@ -2337,11 +2377,17 @@ function formatSeam(r: BiliCacheReport): string { if (r.seam.suspects > 0) { out.push("⚠ CACHE SEAM (suspected mid-history prefix breaks)"); out.push(` ${r.seam.suspects} sample(s) · ${fmtTok(r.seam.missed)} tok re-billed with no fold/switch/restart attribution`); - for (const e of r.seam.events) { + // #3307: seamEvents holds BOTH suspect and #3023 client-side events + // (one shared capped ring), so listing every event here echoed host-owned + // rewrites under the "suspected bili rebuild" header — the ✎ CLIENT + // section below already owns those. List only suspects here so the two + // sections stay disjoint and a client insert is never misread as a seam. + const suspectEvents = r.seam.events.filter((e) => !e.client); + for (const e of suspectEvents) { out.push(` #${e.seq} ${fmtTime(e.at)} hit ${e.hitPct.toFixed(1)}% · input ${fmtTok(e.input)} · divergence ≥${fmtTok(e.lcpBytes)}B at message[${e.msgIndex ?? "?"}] of ${e.prevMsgs}→${e.curMsgs}`); } - if (r.seam.events.length === 0) { - out.push(" (no body-pair forensics on this lane — aggregate flag only; report the session + log if this persists)"); + if (suspectEvents.length === 0) { + out.push(" (no body-pair forensics retained for the suspect samples on this lane — aggregate flag only; client-side rewrites are listed separately below)"); } out.push(" if reproducible: /acp-cache detail:\"full\" + bili.log around the timestamps above (likely a #1548-family round-2/steady render seam)"); } diff --git a/src/config.ts b/src/config.ts index f73522c306..077ec0286b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1073,6 +1073,26 @@ function warnInertMitmLaneKeys(routes: ProviderRoutes, env: NodeJS.ProcessEnv): } } +/** #3675: compress-block keys written on the ROUTE ROOT (URL lane or named + * entry) are silently dropped by parseRouteEntry — the same silent-inert + * class as the #1469 routing fields, one level down. Recognition is + * COMPRESS_SETTING_FIELDS (defined below; kept in sync with + * parseCompressSettings): every member is inert at a route root, and none of + * them may be added to parseRouteEntry's whitelist while this warning exists. + * Dialing recipes are skipped — their own parser rejects unknown root fields + * loudly (parseNamedProviderRecipe throws → chain disabled with a warning), + * so a second signal here would only add noise. */ +function warnCompressOnlyFieldsAtRouteRoot(key: string, obj: Record | null): void { + if (!obj) return; + if (obj.baseUrl !== undefined || obj.api !== undefined) return; + const misplaced = Object.keys(obj).filter((f) => obj[f] !== undefined && COMPRESS_SETTING_FIELDS.has(f)); + if (misplaced.length === 0) return; + warnNamedProviderOnce( + `route-root-compress:${key}:${misplaced.join(",")}`, + `providers."${key}" carries compress-block keys [${misplaced.join(", ")}] at the route root — they are inert there (move them under providers."${key}".compress to apply)`, + ); +} + /** True when the providers-map key is itself a URL lane (http/https/mitm * scheme — mitm:// is the README-documented lookup key for MITM traffic). * Anything else is a NAMED key (provider id): routing-inert on its own, @@ -1257,6 +1277,10 @@ export function loadRoutes(env: NodeJS.ProcessEnv = process.env): ProviderRoutes if (!route) return; const obj = value && typeof value === "object" && !Array.isArray(value) ? value as Record : null; const urlLike = isUrlLikeKey(key); + // Before the bind branch on purpose: these keys are inert even on a + // validly bound alias — parseRouteEntry never parses them, so the + // fold onto the target lane cannot carry them. + warnCompressOnlyFieldsAtRouteRoot(key, obj); if (obj && "bind" in obj) { const bind = obj.bind; if (typeof bind !== "string") { @@ -2207,7 +2231,7 @@ const COMPRESS_SETTING_FIELDS = new Set([ "visibilityMarkers", "rules", "injectTool", "injectNudge", "acknowledgePromptsRisk", "absorb", "ccr", "search", "prompts", "promptPack", "reasoningGuard", "outputSteering", "priceProfile", - "reconcile", "streamSummary", + "reconcile", "streamSummary", "externalSummary", // #3635: summary-length caps — previously accepted nowhere, silently dropped. "maxSummaryLength", "minSummaryLength", ]); diff --git a/src/fold-decomposition-lookup.ts b/src/fold-decomposition-lookup.ts new file mode 100644 index 0000000000..6ba7e7bc85 --- /dev/null +++ b/src/fold-decomposition-lookup.ts @@ -0,0 +1,125 @@ +// #3301: stored-decomposition source for fold-anchor backfill. +// +// Anchors persisted before turn keys existed (pre-#2644) carry no p/x, and the +// wire-side backfill can only fire while the old id is still on the wire — a +// wire-shape flip (host metadata refresh re-serializing every past thinking +// turn) puts it off the wire forever — and the flip pass is the LAST pass in +// which those ids sit in the order backbone (claims only iterate backbone ids). +// This module pulls those ids' original message decompositions out of the +// unified index (full mode only) so reconcileFoldCoverage can rebuild p/x +// BEFORE its plan runs and the flip pass itself claims. Ids absent from the index are +// marked tried once (absence is stable for folded-out history); an UNAVAILABLE +// index is not marked tried (it may appear later). + +import type { CoreMessage } from "acp-kernel"; +import type { Session } from "./session.js"; +import { getStore } from "./persist.js"; +import { log as loggerLog } from "./logger.js"; +import { METADATA_ANCHORS, type FoldAnchor } from "./fold-reconcile.js"; + +export interface StoredRow { + seq: number; + msgId: string; + content: string; +} + +export interface DecompositionSource { + /** null = no index available (legacy/shadow/degraded); [] = index has none of them. */ + rows(sessionId: string, msgIds: readonly string[]): StoredRow[] | null; +} + +const MAX_LOOKUP_PER_PASS = 256; +const TRIED_METADATA_KEY = "foldStoredTried"; +const MAX_TRIED = 8192; + +function defaultSource(): DecompositionSource { + return { + rows(sessionId, msgIds) { + try { + return getStore().unifiedMessageRows(sessionId, msgIds); + } catch (err) { + loggerLog("warn", `[fold-reconcile] stored decomposition lookup failed: ${String(err)}`); + return null; + } + }, + }; +} + +/** Wire-site convenience: build the wire-id set and run the lookup in one + * call (used by all four prepare-* lanes). */ +export function storedMessagesFor(session: Session, sessionId: string, msgs: readonly CoreMessage[]): CoreMessage[] | undefined { + const wireIds = new Set(); + for (const m of msgs) if (m.id !== undefined) wireIds.add(m.id); + return resolveStoredDecompositions(session, sessionId, wireIds); +} + +export function resolveStoredDecompositions( + session: Session, + sessionId: string, + wireIds: ReadonlySet, + source: DecompositionSource = defaultSource(), +): CoreMessage[] | undefined { + const metadata = session.metadata; + if (metadata === undefined) return undefined; + const anchors = (metadata[METADATA_ANCHORS] as Record | undefined) ?? {}; + const anchorIds = Object.keys(anchors); + if (anchorIds.length === 0) return undefined; + const tried = (metadata[TRIED_METADATA_KEY] as Record | undefined) ?? {}; + const needed: string[] = []; + for (const id of anchorIds) { + const a = anchors[id]; + if (a.p !== undefined || a.r !== "assistant" || a.t !== undefined) continue; + if (wireIds.has(id)) continue; + if (tried[id] !== undefined) continue; + needed.push(id); + if (needed.length >= MAX_LOOKUP_PER_PASS) break; + } + if (needed.length === 0) return undefined; + const rows = source.rows(sessionId, needed); + if (rows === null) return undefined; // index unavailable: retry next pass + for (const id of needed) tried[id] = 1; + const pruned: Record = {}; + let n = 0; + for (const [id, v] of Object.entries(tried)) { + if (!anchors[id]) continue; + pruned[id] = v; + if (++n >= MAX_TRIED) break; + } + metadata[TRIED_METADATA_KEY] = pruned; + // Latest seq per id wins (edits append rows); neighbors stay in so rc + // pairs keep adjacency for turn-key derivation. Unparseable rows are + // auxiliary-evidence misses, not errors — count them, don't throw. + let skipped = 0; + const latestSeq = new Map(); + const ordered: { seq: number; m: CoreMessage }[] = []; + const orderedIdx = new Map(); + for (const row of rows) { + let parsed: unknown; + try { + parsed = JSON.parse(row.content); + } catch { + skipped++; + continue; + } + if (typeof parsed !== "object" || parsed === null) { + skipped++; + continue; + } + const m = parsed as CoreMessage; + if (typeof m.id !== "string" || typeof m.role !== "string") { + skipped++; + continue; + } + const prev = latestSeq.get(row.msgId); + if (prev !== undefined && prev >= row.seq) continue; + latestSeq.set(row.msgId, row.seq); + const prevIdx = orderedIdx.get(row.msgId); + if (prevIdx !== undefined) ordered.splice(prevIdx, 1); + ordered.push({ seq: row.seq, m }); + orderedIdx.set(row.msgId, ordered.length - 1); + } + if (skipped > 0) loggerLog("debug", `[fold-reconcile] stored decomposition: ${skipped} unparseable row(s) skipped`); + if (ordered.length === 0) return undefined; + ordered.sort((a, b) => a.seq - b.seq); + return ordered.map((o) => o.m); +} diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index f5a3876220..ea16e6c838 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -140,7 +140,7 @@ import { log as loggerLog } from "./logger.js"; type FoldReconcileMode = "off" | "warn" | "repair"; -const METADATA_ANCHORS = "foldAnchors"; +export const METADATA_ANCHORS = "foldAnchors"; const METADATA_ORDER = "foldAnchorOrder"; /** #2480: positional identity copy — canonical fingerprint per message, * aligned 1:1 with foldAnchorOrder (same rolling window, same cap, written @@ -539,6 +539,42 @@ function turnKeyOfSeamless(reasoning: string | undefined, text: string | undefin return h.digest("hex").slice(0, 16); } +/** #3301: split tagged thinking-merge text (host metadata refresh re-serializes + * split-form turns as tagged reasoning spliced into content). MIRRORS + * kernel/src/wire/demoted-thinking.ts FORMS + splitDemotedThinking in lockstep + * (not exported from the kernel root) — keep both lists in sync. Offset-0 tag + * start is the only position demotion renderers produce. Auxiliary evidence + * for turn-key derivation ONLY — never identity minting. */ +const THINKING_MERGE_FORMS: readonly { open: string; close: string }[] = [ + { open: "\x3cthink\x3e\n", close: "\n\x3c/think\x3e" }, + { open: "\x3cthinking\x3e\n", close: "\n\x3c/thinking\x3e" }, + { open: "```thinking\n", close: "\n```" }, +]; + +export function extractThinkingMerge(text: string | undefined): { reasoning: string; text: string } | null { + if (text === undefined || text === "") return null; + let rest = text; + const parts: string[] = []; + for (;;) { + let matched = false; + for (const form of THINKING_MERGE_FORMS) { + if (!rest.startsWith(form.open)) continue; + const end = rest.indexOf(form.close, form.open.length); + if (end < 0) continue; + const inner = rest.slice(form.open.length, end); + if (inner.length === 0) continue; + parts.push(inner); + rest = rest.slice(end + form.close.length); + if (rest.startsWith("\n")) rest = rest.slice(1); + matched = true; + break; + } + if (!matched) break; + } + if (parts.length === 0) return null; + return { reasoning: parts.join("\n"), text: rest }; +} + /** Turn context for the anchor seed loop: per assistant-core id, the turn * key(s) and (for split-form turns) the sibling core id. Built lazily — a * steady-state pass reuses stored anchors and never pays this. */ @@ -563,7 +599,18 @@ export function turnContexts(msgs: CoreMessage[]): Map { const prev = msgs[i - 1]; if (prev === undefined || prev.id === undefined || prev.role !== "assistant" || prev.contentType !== "reasoning" || (prev.toolCallId !== undefined && prev.toolCallId !== "")) { - ctx.set(m.id, { p: turnKeyOf(undefined, m.text) }); + // #3301: merged-form core — its text may be tagged reasoning + reply. + // The extracted pair key bridges anchors seeded from the split form + // (same turn, different wire shape); the raw full-text key stays for + // identical resends of this exact form. + const ex = extractThinkingMerge(m.text); + if (ex !== null) { + const key = turnKeyOf(ex.reasoning, ex.text); + const seamless = turnKeyOfSeamless(ex.reasoning, ex.text); + ctx.set(m.id, seamless !== key ? { p: key, p2: seamless } : { p: key }); + } else { + ctx.set(m.id, { p: turnKeyOf(undefined, m.text) }); + } } } } @@ -1068,10 +1115,20 @@ export function planReconciliation( if (claimedCandidates.has(cand.id)) continue; if (!isBareAssistantCore(cand.message)) continue; if (cand.message.contentType === "text") { - const key = turnKeyOf(undefined, cand.message.text); - const bucket = singleTurnKeys.get(key); - if (bucket === undefined) singleTurnKeys.set(key, [cand.id]); - else bucket.push(cand.id); + // #3301: a merged-form core stands in for the whole turn — + // register the raw full-text key AND the extracted pair key(s) + // so anchors seeded from either wire form match. + const keys = new Set([turnKeyOf(undefined, cand.message.text)]); + const ex = extractThinkingMerge(cand.message.text); + if (ex !== null) { + keys.add(turnKeyOf(ex.reasoning, ex.text)); + keys.add(turnKeyOfSeamless(ex.reasoning, ex.text)); + } + for (const key of keys) { + const bucket = singleTurnKeys.get(key); + if (bucket === undefined) singleTurnKeys.set(key, [cand.id]); + else bucket.push(cand.id); + } } else if (cand.message.contentType === "reasoning") { const next = byPos.get(cand.pos + 1); if (next !== undefined && !claimedCandidates.has(next.id) && isBareAssistantCore(next.message) && next.message.contentType === "text") { @@ -1537,6 +1594,13 @@ export interface ReconcileOptions { /** Inbound wire items before codec expansion; auxiliary requests can * project a few items into dozens of cores without carrying history. */ inboundMessageCount?: number; + /** #3301: stored message decompositions for anchors that predate turn-key + * backfill (pre-#2644) — after a wire-shape flip the wire-side backfill + * can never fire (the old id is gone from the wire), so eligible anchors + * are rebuilt from these rows BEFORE the reconciliation plan runs and the + * flip pass itself claims. Latest-seq-per-id, neighbors included; + * undefined = no source available (legacy-only mode). */ + storedMessages?: CoreMessage[]; } /** Entry point wired into the four wire sites, immediately BEFORE the @@ -1573,7 +1637,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt } if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; - const anchors: Record = + let anchors: Record = (session.metadata[METADATA_ANCHORS] as Record | undefined) ?? {}; const oldOrder: string[] = (session.metadata[METADATA_ORDER] as string[] | undefined) ?? []; // #2480: self-contained positional copy {ids, canon} (#2487's storage @@ -1589,6 +1653,35 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt positions = { ids: storedRaw.ids as string[], canon: storedRaw.canon as string[] }; } + const byId = new Map(); + for (const m of msgs) if (m.id !== undefined) byId.set(m.id, m); + // #3301: a wire-shape flip puts legacy (pre-#2644, no p/x) ids off the wire + // on the FIRST post-flip pass — and this same pass rolls the order backbone + // forward without them, so any backfill landing later can never claim (the + // claim passes only iterate ids still in the backbone). Rebuild eligible + // anchors from the stored decomposition BEFORE the plan runs so the flip + // pass itself can claim. Wire bytes always win: an id present on this pass + // keeps its live-bytes path untouched. + if (opts.storedMessages !== undefined && opts.storedMessages.length > 0) { + const sById = new Map(); + for (const m of opts.storedMessages) if (m.id !== undefined) sById.set(m.id, m); + const sCtx = turnContexts(opts.storedMessages); + let changed = false; + const upgraded: Record = {}; + for (const [id, a] of Object.entries(anchors)) { + if (a.p === undefined && a.r === "assistant" && a.t === undefined && !byId.has(id)) { + const sm = sById.get(id); + if (sm !== undefined) { + upgraded[id] = anchorFrom(sm, sCtx.get(id)); + changed = true; + continue; + } + } + upgraded[id] = a; + } + if (changed) anchors = upgraded; + } + const plan = planReconciliation(oldOrder, anchors, msgs, covered, positions); if (plan.pass0Desync) { loggerLog("warn", `[fold-reconcile] Pass 0 disabled: stored positional copy desynced from order backbone (session=${session.id}, copyIds=${positions?.ids.length ?? -1}, backbone=${oldOrder.length})`); @@ -1603,8 +1696,6 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt // freshly claimed ones — the next churn must re-anchor from post-churn // bytes), prune anchors for ids no longer covered, and roll the order // backbone forward to this pass. - const byId = new Map(); - for (const m of msgs) if (m.id !== undefined) byId.set(m.id, m); // #2636: turn keys for freshly seeded anchors — built lazily so // steady-state passes (no churn) never pay the per-core normalization // (#2334). Only bare assistant cores can carry a turn key diff --git a/src/instance.ts b/src/instance.ts index 002b3b9470..3fdeeb25d8 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -441,6 +441,29 @@ export function isPidAlive(pid: number): boolean { } } +export interface PidProbe { + alive: boolean; + /** Errno code of the failed kill-0 (e.g. "ESRCH"); undefined when the + * probe succeeded or no probe ran at all (invalid pid). */ + errCode?: string; +} + +/** #3890: kill-0 with errno visibility. On Windows Node maps every + * OpenProcess failure other than ERROR_ACCESS_DENIED to ESRCH, so a LIVE + * process can read dead with no way to tell which Win32 error caused it. + * Callers that act destructively on a dead verdict log errCode on first + * sight so field episodes can be attributed. */ +export function probePid(pid: number): PidProbe { + if (!Number.isInteger(pid) || pid <= 0) return { alive: false }; + try { + process.kill(pid, 0); + return { alive: true }; + } catch (err) { + const code = (err as NodeJS.ErrnoException).code; + return { alive: code === "EPERM", errCode: code }; + } +} + export interface RegistryEntry { instanceId: string; pid: number; diff --git a/src/mcp.ts b/src/mcp.ts index 4a9ede4dde..7be30e024e 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -387,9 +387,17 @@ async function handleMessage(msg: { // #2752: the business state rides MCP structuredContent (spec // field alongside content; clients predating it ignore it). // isError semantics are unchanged — partial stays non-error. + // #3472: some hosts (Claude Code via its MCP bridge) prefer + // structuredContent when present and drop the content text — + // with only {outcome, blocksCreated} every success call reached + // the model as bare {"outcome":"success"} and the whole report + // was lost. Carry the full report inside structuredContent too + // (structured.text, byte-identical to content[0].text) so + // structured-only readers see the complete receipt; the machine + // fields stay additive alongside it. const result: Record = { content: [{ type: "text", text: out.text }], isError: out.failed }; if (out.outcome !== undefined || out.blocksCreated !== undefined) { - const structured: Record = {}; + const structured: Record = { text: out.text }; if (out.outcome !== undefined) structured.outcome = out.outcome; if (out.blocksCreated !== undefined) structured.blocksCreated = out.blocksCreated; result.structuredContent = structured; diff --git a/src/persist.ts b/src/persist.ts index ad251ca685..a1dbd560ef 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -215,6 +215,9 @@ interface PersistedSession { * history and must still be pruned at export time. */ messagesFolded?: boolean; pluginSnapshot?: CoreMessage[]; + /** #3301: external-converter import payload (see Session.decompositionLog) — + * accepted on read; buildRecord deliberately does not emit it. */ + decompositionLog?: CoreMessage[]; forkContentStore?: MessageContentStore; } @@ -1618,6 +1621,20 @@ export class SessionStore { } } + /** #3301: stored message decompositions (+ seq±1 neighbors) for + * fold-anchor backfill after a wire-shape flip (consumed by + * fold-decomposition-lookup → reconcileFoldCoverage.storedMessages). + * Full mode only (same gate as the other unified reads); null when + * unavailable — callers fall back to wire-side backfill. Never throws. */ + unifiedMessageRows(sessionId: string, msgIds: readonly string[]): { seq: number; msgId: string; content: string }[] | null { + if (!this.shadow?.readsUnified || msgIds.length === 0) return null; + try { + return this.shadow.messagesForIds(sessionId, msgIds, true); + } catch { + return null; + } + } + replicasAvailable(): boolean { return this.shadow !== undefined && !this.shadow.stats.disabled; } @@ -2196,6 +2213,14 @@ export class SessionStore { const record = buildRecord(session, stamp); const disk = this.staleDiskPayload(record); if (disk === null) { + // #3301: external-converter import payload rides exactly ONE + // save into the unified index (first-open imports take the + // importRecord path), then is consumed so steady saves keep + // the delta fast path (#3133) instead of paying full + // re-ingests forever. buildRecord omits it, so the disk file + // never carries it. + const payload = session.decompositionLog; + if (payload !== undefined) session.decompositionLog = undefined; // #2960 ⑤: the table holds the RAW tail (full fidelity); the // envelope keeps its bounded folded view for legacy readers. // ②a caveat: once foldResidentTail has folded the resident @@ -2209,9 +2234,10 @@ export class SessionStore { // folded saves present the envelope render (no override), // which the delta path already routes to the full ingest. const tail = session.lastMessagesFolded !== true ? session.lastMessages : undefined; - this.shadow?.record(record, tail && tail.length > 0 - ? { lastMessages: tail, lastMessagesFolded: session.lastMessagesFolded } - : undefined, session.contentStore ?? undefined); + this.shadow?.record(payload !== undefined ? { ...record, decompositionLog: payload } : record, + tail && tail.length > 0 + ? { lastMessages: tail, lastMessagesFolded: session.lastMessagesFolded } + : undefined, session.contentStore ?? undefined); this.noteOwnCommit(session.id, record); return record; } @@ -2305,6 +2331,7 @@ export class SessionStore { session.lastMessages = fresh.lastMessages ?? session.lastMessages; session.lastMessagesFolded = fresh.lastMessages !== undefined ? fresh.lastMessagesFolded : session.lastMessagesFolded; session.pluginSnapshot = fresh.pluginSnapshot; + session.decompositionLog = fresh.decompositionLog ?? session.decompositionLog; session.lastSeen = fresh.lastSeen; if (!session.meta.protocol && fresh.meta.protocol) session.meta.protocol = fresh.meta.protocol; if (!session.meta.upstreamOrigin && fresh.meta.upstreamOrigin) session.meta.upstreamOrigin = fresh.meta.upstreamOrigin; @@ -2850,6 +2877,7 @@ function buildSession(parsed: PersistedSession): Session { lastMessages: Array.isArray(parsed.messages) ? parsed.messages : undefined, lastMessagesFolded: parsed.messagesFolded === true, pluginSnapshot: Array.isArray(parsed.pluginSnapshot) ? parsed.pluginSnapshot : undefined, + decompositionLog: Array.isArray(parsed.decompositionLog) ? parsed.decompositionLog : undefined, contentStore: parsed.forkContentStore, inFlight: 0, persisted: true, diff --git a/src/plugin.ts b/src/plugin.ts index 3e44d691c6..2472d6a248 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,4 +1,4 @@ -import { BLOCKED_REF, COMPRESS_TOOL_NAME, type CompressionCore, type Config, type CoreMessage, type NudgeDecision, type RetrievalExport, applyRetrieve, countMessageTokens, parseStoredPlaceholder, prune } from "acp-kernel"; +import { BLOCKED_REF, COMPRESS_TOOL_NAME, type CompressionCore, type Config, type CoreMessage, type NudgeDecision, type RetrievalExport, type ToolPrompts, applyAcpToolOverrides, applyRetrieve, countMessageTokens, parseStoredPlaceholder, prune } from "acp-kernel"; import { imageRescaleEnabled, publicSnapshotCapBytes } from "./knobs.js"; import { buildStatusPanel } from "acp-kernel/panel"; import { fileURLToPath } from "node:url"; @@ -851,7 +851,7 @@ export function handlePluginSessionName(payload: string, res: import("node:http" // arbitration; see tool-ring.ts). Wire-mode injection serves the kernel // constants directly, which never carried the param. -export function handlePluginManifest(res: import("node:http").ServerResponse, config: Config): void { +export function handlePluginManifest(res: import("node:http").ServerResponse, config: Config, toolPrompts?: ToolPrompts): void { // #1192: hosts register whatever the manifest serves verbatim (pi/omp/dsh/ // opencode native plugins, MCP shims), so advertising an opt-in tool this // proxy's config leaves disabled guarantees a rejected call the moment the @@ -861,6 +861,12 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co // enablement stays enforced at execution (isProxyToolFor / executeProxyTool). // #1359: the advertised name is the base-config toolName, matching the // plugin-lane gate (which adjudicates the same base block). + // #3243: toolPrompts is the pack surface's tool descriptions, resolved by the + // caller from the GLOBAL compress settings — the manifest has no route/model + // context (hosts fetch it before any request arrives), so provider/model-level + // promptPack still shapes each request's system prompt while registered tool + // descriptions follow the top-level pack only. Applied exactly like the wire + // injector (applyAcpToolOverrides on the ACP core arrays before extras merge). const absorbOn = absorbEnabled(config); const absorbName = config.absorb?.toolName ?? ABSORB_TOOL_NAME; const absorbTools = absorbOn ? absorbToolsFor(absorbName) : undefined; @@ -890,13 +896,15 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co // run_code SDK docs). The binding note on the ACP entries teaches both // calling conventions, so the model neither slips a direct call the // harness will reject nor dodges the direct call native hosts expect. - const acpAnthropic = withAcpNotes(withExternalSummaryTools(ccrOn ? BILI_ACP_TOOLS_ANTHROPIC : BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, externalSummary), "binding"); - const acpOpenai = withAcpNotesOpenAI(withExternalSummaryTools(ccrOn ? BILI_ACP_TOOLS_OPENAI : BILI_ACP_TOOLS_OPENAI_NO_RANGE, externalSummary), "binding"); + // #3243: the pack surface's tool descriptions (toolPrompts) override the + // stock descriptions where a promptPack declares its own. + const acpAnthropic = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(ccrOn ? BILI_ACP_TOOLS_ANTHROPIC : BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, toolPrompts), externalSummary), "binding"); + const acpOpenai = withAcpNotesOpenAI(withExternalSummaryTools(applyAcpToolOverrides(ccrOn ? BILI_ACP_TOOLS_OPENAI : BILI_ACP_TOOLS_OPENAI_NO_RANGE, toolPrompts), externalSummary), "binding"); // Responses wire: plugin mode structurally disarms CCR there (#1271 — // PLUGIN_CCR_WIRES excludes it), so range restore can never execute for a // registered agent on that wire — always the no-range variant, mirroring // how ccrTools above is never spread into the responses array. - const acpResponses = withAcpNotes(withExternalSummaryTools(BILI_ACP_TOOLS_RESPONSES_NO_RANGE, externalSummary), "binding"); + const acpResponses = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(BILI_ACP_TOOLS_RESPONSES_NO_RANGE, toolPrompts), externalSummary), "binding"); res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: true, diff --git a/src/server.ts b/src/server.ts index eb07e54a3e..200ddc0f38 100644 --- a/src/server.ts +++ b/src/server.ts @@ -98,7 +98,7 @@ import { getStore, setGlobalSessionCommitListener } from "./persist.js"; import { log as loggerLog, configureLogger, getLogPath, closeLogger, isStreamWriteError, isBenignSocketRaceError, enterSessionContext, muteStderrLeg, onFileLegLive } from "./logger.js"; import { queryLogLines } from "./web/logs-query.js"; import { configFile, defaultLogFile, stateDir } from "./paths.js"; -import { atomicWriteInstanceFile, clearProxyInstanceFile, classifySameLanePeer, entryScriptFingerprint, entryShape, findSameLanePredecessor, findVerifiedSameLaneTwin, isPidAlive, listInstances, readProxyInstanceFile, registerInstanceAndWarn, sameInstanceShape, unregisterInstance, warnOnNewPeers, writeSpawnPortLog, type InstanceShape, type ProxyInstanceFile, type SameLanePeer } from "./instance.js"; +import { atomicWriteInstanceFile, clearProxyInstanceFile, classifySameLanePeer, entryScriptFingerprint, entryShape, findSameLanePredecessor, findVerifiedSameLaneTwin, isPidAlive, listInstances, probePid, readProxyInstanceFile, registerInstanceAndWarn, sameInstanceShape, unregisterInstance, warnOnNewPeers, writeSpawnPortLog, type InstanceShape, type PidProbe, type ProxyInstanceFile, type RegistryEntry, type SameLanePeer } from "./instance.js"; import { createServedMarkerWriter, reapStaleWriterMarkers, writerMarkerDir, type ServedMarkerWriter } from "./served-markers.js"; import { recordSplitBrainContest } from "./storage/runtime-status.js"; import { mayClobberPointer, probeOwnerInfo, resolvePortClaim } from "./port-owner.js"; @@ -1496,6 +1496,10 @@ export async function startServer(opts: ProxyOptions): Promise { closeLogger(); process.exit(0); }; + const finishDrain = (): void => { + flushConversations(); + void flushAllSessions().finally(finishShutdown); + }; const shutdown = (sig: string) => { if (shuttingDown) return; shuttingDown = true; @@ -1506,16 +1510,29 @@ export async function startServer(opts: ProxyOptions): Promise { // before invoking cb, so in-flight SSE streams get a chance to finish // rather than being yanked mid-chunk. server.close(() => { - flushConversations(); - void flushAllSessions().finally(finishShutdown); + finishDrain(); }); // Hard fallback: if connections hang (client never closes), don't - // block shutdown forever — force-exit after a grace window. - setTimeout(() => { - log("warn", `shutdown grace window elapsed; forcing exit (liveConns=${connRecords.size})`); - flushConversations(); - void flushAllSessions().finally(finishShutdown); - }, 10_000).unref?.(); + // block shutdown forever — force-exit after a grace window. #3890: + // while connections are still ALIVE the window re-arms (bounded by + // SHUTDOWN_DRAIN_MAX_PASSES) instead of cutting in-flight upstream + // streams mid-response — idle keep-alives don't hold the proxy, the + // keep-alive reaper clears them within one window, so a healthy drain + // still exits promptly and a stuck socket can pin at most the cap. + let drainPasses = 0; + const armDrainBackstop = (): void => { + setTimeout(() => { + if (connRecords.size > 0 && drainPasses < SHUTDOWN_DRAIN_MAX_PASSES) { + drainPasses += 1; + log("warn", `shutdown grace window elapsed but liveConns=${connRecords.size} — extending drain ${drainPasses}/${SHUTDOWN_DRAIN_MAX_PASSES} (#3890)`); + armDrainBackstop(); + return; + } + log("warn", `shutdown grace window elapsed; forcing exit (liveConns=${connRecords.size})`); + finishDrain(); + }, SHUTDOWN_DRAIN_GRACE_MS).unref?.(); + }; + armDrainBackstop(); }; // #3544: POST /__bili/shutdown (stale-proxy rotation from a newer // generation's bring-up) takes this exact graceful path. @@ -1543,21 +1560,44 @@ export async function startServer(opts: ProxyOptions): Promise { let idleSince: number | null = null; let lastDead: number[] = []; let lastStaleHb: number[] = []; + // #3890: consecutive failed kill-0 probes per pid (see WATCHER_DEAD_CONFIRM_TICKS). + const deadStreak = new Map(); const watcher = setInterval(() => { const dead: number[] = []; const staleByHeartbeat: number[] = []; const tickNow = Date.now(); for (const pid of proxyWatchers) { - const alive = isPidAlive(pid); + const probe = probePid(pid); // #3544: heartbeat-opted owners ALSO die by liveness-proof // expiry — kill(pid,0) is an existence check, identity-blind // under Windows pid reuse. Legacy pids (no heartbeat on record) - // keep kill-0-only semantics. - if (watcherIsDead(alive, watcherLastSeen.get(pid), tickNow, WATCHER_HEARTBEAT_GRACE_MS)) { + // keep kill-0-only semantics. #3890: a failed kill-0 must be + // CONFIRMED over consecutive ticks before it removes the pid — + // one transient OpenProcess failure used to be enough to kill + // the whole proxy while its parent lived on. + const decision = tickWatchedPid(probe, watcherLastSeen.get(pid), tickNow, deadStreak.get(pid), { heartbeatGraceMs: WATCHER_HEARTBEAT_GRACE_MS, confirmTicks: WATCHER_DEAD_CONFIRM_TICKS }); + if (decision.removed) { proxyWatchers.delete(pid); watcherLastSeen.delete(pid); + deadStreak.delete(pid); dead.push(pid); - if (alive) staleByHeartbeat.push(pid); + if (decision.staleHeartbeat) staleByHeartbeat.push(pid); + continue; + } + if (probe.alive) { + if ((deadStreak.get(pid) ?? 0) > 0) { + deadStreak.delete(pid); + log("info", `[watchdog] pid ${pid} alive again after failed kill-0 probe(s) — clearing death streak (#3890)`); + } + } else { + if (decision.streak === 1) { + // Field instrumentation (#3890): Windows collapses every + // non-ACCESS_DENIED OpenProcess failure into ESRCH, so + // the errno is the only clue left to attribute false-dead + // episodes — log it once per episode. + log("info", `[watchdog] kill-0 probe failed for pid ${pid} (code=${probe.errCode ?? "?"}) — requiring ${WATCHER_DEAD_CONFIRM_TICKS} consecutive failures before believing it (#3890)`); + } + deadStreak.set(pid, decision.streak); } } if (dead.length > 0) lastDead = dead; @@ -1868,6 +1908,46 @@ export function watcherIsDead(alive: boolean, lastSeen: number | undefined, nowM return lastSeen !== undefined && nowMs - lastSeen > heartbeatGraceMs; } +// #3890: the shutdown backstop window, and how many times it may RE-ARM while +// connections are still alive. One window is the historical hard fallback; +// extra passes exist so an in-flight upstream stream finishes instead of being +// cut mid-response when its owner's death triggers the exit. Worst-case total +// drain ≈ (1 + SHUTDOWN_DRAIN_MAX_PASSES) windows. +const SHUTDOWN_DRAIN_GRACE_MS = 10_000; +const SHUTDOWN_DRAIN_MAX_PASSES = 10; + +// #3890: how many CONSECUTIVE failed kill-0 probes must agree before the +// watchdog believes a watched pid has died. Pre-fix, ONE failed probe removed +// the pid from the watcher set and the empty-set grace window that followed +// never re-probed it — a single transient OpenProcess failure on Windows was +// enough to force-exit the proxy ~6s later while its parent lived on (12 such +// events in 2.5h against one live pid). At the 2s tick, three confirmations +// span ~4–6s: long enough to ride out transient failures, short enough that a +// genuinely dead parent still exits promptly. +const WATCHER_DEAD_CONFIRM_TICKS = 3; + +export interface WatcherTickDecision { + /** Remove this pid from the watcher set on this tick. */ + removed: boolean; + /** Removed because the heartbeat expired while the pid still reads alive (#3544). */ + staleHeartbeat: boolean; + /** Failed-probe streak after this tick (0 when alive or removed). */ + streak: number; +} + +/** #3890: pure per-pid death-streak step. A live probe resets the streak; a + * failed probe increments it and only removes the pid once the streak reaches + * confirmTicks. Heartbeat expiry stays immediate (it is a time-based liveness + * proof, not an existence probe — #3544 semantics). */ +export function tickWatchedPid(probe: PidProbe, lastSeen: number | undefined, nowMs: number, prevStreak: number | undefined, opts: { heartbeatGraceMs: number; confirmTicks: number }): WatcherTickDecision { + if (probe.alive) { + const staleHeartbeat = watcherIsDead(true, lastSeen, nowMs, opts.heartbeatGraceMs); + return { removed: staleHeartbeat, staleHeartbeat, streak: 0 }; + } + const streak = (prevStreak ?? 0) + 1; + return streak >= opts.confirmTicks ? { removed: true, staleHeartbeat: false, streak: 0 } : { removed: false, staleHeartbeat: false, streak }; +} + const ACP_TAG_MARK = "\x3cacp "; diff --git a/src/server/admin.ts b/src/server/admin.ts index a567776c26..6f57a24bf2 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -6,7 +6,7 @@ import { APIG_RESIGN_SCHEME, KNOWN_SIGNATURE_SCHEMES, readPendingRefusals, unres import { handleAcpCache, readKeySwitchStats, readModelSwitchStats, readProjSwitchStats, readPromptSwitchStats } from "../cache-ledger.js"; import type { ProxyOptions } from "../config.js"; import { loadNamedProviders, loadOptions, loadRoutes, resolveResignSettings } from "../config.js"; -import { applyCompressSettings } from "../compress-settings.js"; +import { applyCompressSettings, resolveCompressSurfaceDetailed } from "../compress-settings.js"; import { clearConflictEvents, summarizeConflicts } from "../conflict-watch.js"; import { cannotResolveTarget, getAdvisoryState } from "../advisory.js"; import { detectCostAdvisories } from "../plugin-advisory.js"; @@ -257,6 +257,19 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv res.end(JSON.stringify({ ok: true, upstream: opts.upstream })); return; } + // #3536: browsers probing the proxy origin for a favicon (the dsh desktop + // settings panel frames /__bili/ and the frame fires GET /favicon.ico at + // the ORIGIN ROOT) must be answered locally with 204 — falling through to + // the relay would rewrite the host onto the configured upstream, burning a + // request there and logging a 403 for a static asset no model client ever + // asks for. The panel page itself also declares an empty icon (page.ts) so + // well-behaved browsers never send the probe at all; this is the backstop + // for browsers/timings that still do (probe before redirect, direct visits). + if (req.method === "GET" && req.url?.split("?", 2)[0] === "/favicon.ico") { + res.writeHead(204); + res.end(); + return; + } if (req.method === "GET" && req.url === "/__bili/health") { // #2941: 503 while registration is still pending — every consumer // (launcher spawn-wait, attach discovery, host-native respawn) keys on @@ -429,7 +442,12 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv // (DEFAULT_CCR_CONFIG et al. inside applyCompressSettings); per-request/route overrides // are still enforced at execution time, so the manifest stays conservative as #1192 // requires. Do not "simplify" this back to `config`. - return handlePluginManifest(res, applyCompressSettings(config, opts.modelContextLimit, opts.compress, { ...agentProviderRecipes(), ...opts.namedProviders ?? {} })); + // #3243: pass the pack surface's tool descriptions too — resolved from the SAME + // global settings (no route/model context exists at manifest time), so registered + // tool descriptions follow the top-level promptPack while per-request system prompts + // may still honor deeper provider/model levels. + const packSurface = resolveCompressSurfaceDetailed(opts.compress ?? {}); + return handlePluginManifest(res, applyCompressSettings(config, opts.modelContextLimit, opts.compress, { ...agentProviderRecipes(), ...opts.namedProviders ?? {} }), packSurface.surface.toolPrompts); } if (req.method === "GET" && req.url?.split("?")[0] === "/__bili/plugin/snapshot") { return await handlePluginSnapshot(new URL(req.url, "http://localhost").searchParams.get("conversationId") ?? "", res); diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index d68db32d11..fdc9cab600 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -12,6 +12,7 @@ import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, r import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; import { maybeSnapshotView } from "./snapshot-render.js"; +import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; @@ -338,7 +339,8 @@ export async function prepareAnthropic( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs) }); noteSystemPromptFingerprint(session, clientSystem, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 1a6a931281..53d086d336 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -10,6 +10,7 @@ import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, r import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; import { maybeSnapshotView } from "./snapshot-render.js"; +import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; @@ -181,7 +182,8 @@ export async function prepareGoogle( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs) }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 47d2299950..435ca4cbc3 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -12,6 +12,7 @@ import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, r import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; import { maybeSnapshotView } from "./snapshot-render.js"; +import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; @@ -190,7 +191,10 @@ export async function prepareOpenai( // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). The verdict // feeds the #4131 rewrite gate below (explained churn ≠ destruction). - const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + // #3301: stored decompositions for pre-#2644 anchors whose wire-side + // backfill can never fire after a wire-shape flip. + const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs) }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 0d0e562a40..35f4815604 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -13,6 +13,7 @@ import { mergeAdjacentConfigurationUpdates, patchResponsesInputWithToolImages as import { dropWhitespaceResponsesMessages, normalizeResponsesMessageItems, sanitizeResponsesInputIds } from "../loop/adapter-responses.js"; import { METADATA_LAST_RESPONSES_MODE, noteCanonicalSystemFingerprint, reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; import { maybeSnapshotView } from "./snapshot-render.js"; +import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed, pluginLaneDegraded } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; @@ -290,7 +291,8 @@ export async function prepareResponses( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); +const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); +reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs) }); if (!isCompactionTrigger) { noteSystemPromptFingerprint(session, responsesClientSystem, { sessionId, log }); // #2867: the mode-independent twin — lets the ledger classify a diff --git a/src/session.ts b/src/session.ts index c285835760..fc2daf6161 100644 --- a/src/session.ts +++ b/src/session.ts @@ -546,6 +546,11 @@ export type Session = { * request — the client re-sends full raw history, restoring the invariant. */ lastMessagesFolded?: boolean; pluginSnapshot?: CoreMessage[]; + /** #3301: original message decompositions supplied by an external state + * converter (e.g. opencode-acp migration) at import time. Transient + * import payload: consumed once by the unified ingest (rows land in the + * index for fold re-anchor backfill) and never written back to records. */ + decompositionLog?: CoreMessage[]; /** In-memory only (NOT persisted — buildRecord omits it): monotone counter * bumped by markDirty after every mutation. Keys the fork-revision cache * below so status polling skips re-hashing the whole history (#2017). */ diff --git a/src/storage/ingest.ts b/src/storage/ingest.ts index f1c0404a9d..c3a84aff22 100644 --- a/src/storage/ingest.ts +++ b/src/storage/ingest.ts @@ -60,6 +60,12 @@ export interface LegacySessionLike { lastMessages?: CoreMessage[]; lastMessagesFolded?: boolean; pluginSnapshot?: CoreMessage[]; + /** #3301: external-converter import payload — original message + * decompositions interned BEFORE the snapshot/tail so their rows get the + * lowest seqs (they are the oldest evidence). Consumed by the full + * ingest only; the delta planner falls back to the full path while it is + * present (its rows would otherwise be skipped as "trusted prefix"). */ + decompositionLog?: CoreMessage[]; contentStore?: { version: 1; byHash: Record; byRef: Record }; } @@ -115,6 +121,7 @@ export function toLegacyLike(s: { lastMessages?: LegacySessionLike["lastMessages"]; lastMessagesFolded?: boolean; pluginSnapshot?: LegacySessionLike["pluginSnapshot"]; + decompositionLog?: LegacySessionLike["decompositionLog"]; contentStore?: LegacySessionLike["contentStore"]; }): LegacySessionLike { return { @@ -129,6 +136,7 @@ export function toLegacyLike(s: { lastMessages: s.lastMessages, lastMessagesFolded: s.lastMessagesFolded, pluginSnapshot: s.pluginSnapshot, + decompositionLog: s.decompositionLog, contentStore: s.contentStore, }; } @@ -164,6 +172,9 @@ export function planIngest(session: LegacySessionLike): IngestPlan { log.push({ seq, msgId: m.id, content: entry.content, hash: entry.hash, identity, role: entry.role, kind: entry.kind, ref: byRawRef[m.id] ?? null, ...(entry.media.length > 0 ? { media: entry.media } : {}) }); return seq; }; + // #3301: import payload first — its rows must sit below the snapshot/tail + // seqs so the stored log reads oldest-evidence-first. + if (session.decompositionLog && session.decompositionLog.length > 0) session.decompositionLog.forEach(intern); const pluginSnapshotSeqs = session.pluginSnapshot ? session.pluginSnapshot.map(intern) : null; const tailSeqs = session.lastMessages ? session.lastMessages.map(intern) : null; @@ -229,6 +240,9 @@ export function planIngestDelta( head?: { msgId: string } | null, ): IngestPlan | null { if (session.pluginSnapshot && session.pluginSnapshot.length > 0) return null; + // #3301: an import payload would be skipped as "trusted prefix" here — force + // the full path so its rows actually land. + if (session.decompositionLog && session.decompositionLog.length > 0) return null; const tail = session.lastMessages; const trustedLen = trustedTailSeqs.length; if ( diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index f6d351f8ff..83a97dbac7 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -640,6 +640,14 @@ export class UnifiedShadow { return tip; } + /** #3301: message rows for specific ids (+ seq±1 neighbors) — stored + * decompositions for fold-anchor backfill after a wire-shape flip. */ + messagesForIds(sessionId: string, msgIds: readonly string[], includeNeighbors: boolean): { seq: number; msgId: string; content: string }[] { + this.assertLive(); + if (!this.sync) return []; // network backends cannot serve the sync decomposition lookup (#3931 architecture) + return this.sync.messagesForIds(sessionId, msgIds, includeNeighbors); + } + /** All sessions row ids present in the index (#2960 R2.1). Used by the * full-mode boot union so rows whose legacy file disappeared remain * enumerable. */ diff --git a/src/storage/store.ts b/src/storage/store.ts index f815ddb4ca..ad497897ff 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1241,6 +1241,49 @@ export class UnifiedStore { return row ? row.msg_id : null; } + /** #3301: message rows for the given ids — EVERY seq row matching an id + * (edits append; callers keep the latest per id) plus, when + * includeNeighbors, the seq±1 rows so split-form rc pairs stay adjacent + * for turn-key derivation. Content bytes resolved via CAS; rows whose + * blob is gone are skipped. Read-only. */ + messagesForIds(sessionId: string, msgIds: readonly string[], includeNeighbors: boolean): { seq: number; msgId: string; content: string }[] { + if (msgIds.length === 0) return []; + const CHUNK = 500; + const rows = new Map(); + const neighborSeqs = new Set(); + for (let i = 0; i < msgIds.length; i += CHUNK) { + const chunk = msgIds.slice(i, i + CHUNK); + const ph = chunk.map(() => "?").join(","); + for (const r of this.db + .prepare(`SELECT seq, msg_id, content_hash FROM messages WHERE session_id = ? AND msg_id IN (${ph})`) + .all(sessionId, ...chunk) as { seq: number; msg_id: string; content_hash: string }[]) { + if (!rows.has(r.seq)) rows.set(r.seq, { seq: r.seq, msgId: r.msg_id, contentHash: r.content_hash }); + if (includeNeighbors) { + neighborSeqs.add(r.seq - 1); + neighborSeqs.add(r.seq + 1); + } + } + } + if (neighborSeqs.size > 0) { + const seqs = [...neighborSeqs]; + for (let i = 0; i < seqs.length; i += CHUNK) { + const chunk = seqs.slice(i, i + CHUNK); + const ph = chunk.map(() => "?").join(","); + for (const r of this.db + .prepare(`SELECT seq, msg_id, content_hash FROM messages WHERE session_id = ? AND seq IN (${ph})`) + .all(sessionId, ...chunk) as { seq: number; msg_id: string; content_hash: string }[]) { + if (!rows.has(r.seq)) rows.set(r.seq, { seq: r.seq, msgId: r.msg_id, contentHash: r.content_hash }); + } + } + } + const out: { seq: number; msgId: string; content: string }[] = []; + for (const r of [...rows.values()].sort((a, b) => a.seq - b.seq)) { + const content = this.getContent(r.contentHash); + if (content !== null) out.push({ seq: r.seq, msgId: r.msgId, content }); + } + return out; + } + /** Deletes one session and releases every CAS refcount it holds * (GC parity with the legacy path's file removal). Rows that other * sessions also reference survive via their own refcounts. Checkpoints diff --git a/src/stream.ts b/src/stream.ts index 2cc39836da..895dfc8564 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,7 +1,7 @@ import { createHash, randomBytes } from "node:crypto"; import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, hideConsumedCompressCalls, indexToRef, parseBoundary, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; -import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; +import { handleAcpCache, prefixTokensBeforeRef, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty, residentPressure, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.js"; @@ -1152,8 +1152,17 @@ export function applyRanges(parsed: ReturnType, ctx: // with the kernel's own per-message counter instead — a same-space // denominator makes the ratio structurally ≤1, so #1839's // "trustworthy baseline" gate is subsumed and no longer needed. - const viewMessages = ctx.compressMessages ?? ctx.messages; - // #4227: id→tokens map — one scan of the view serves both meters below. + // #3307: "the current view" is the LIVE context being managed — the + // compressed outbound (ctx.messages / processedMessages), i.e. what gets + // billed next — NOT the client's un-shrunken resend. applyCompression + // still runs on ctx.compressMessages (raw message boundaries, #2627) so + // the fold geometry maps correctly; only the OBSERVABILITY denominator + // moves. A stateless client (codex_webui) re-sends its FULL history every + // turn, so preferring compressMessages made V ~50x the billed input and + // read every real fold as a ~1% shrink — postCtx/ceiling and the + // degenerate-fold gate below all distorted off the wrong basis. + // #4227: id→tokens map over the same live view — one scan serves both meters. + const viewMessages = ctx.messages; const viewTokById = new Map(); let viewTokens = 0; for (const m of viewMessages) { @@ -1163,6 +1172,11 @@ export function applyRanges(parsed: ReturnType, ctx: } // This-fold ratio only: lastCompress, the degenerate-fold gate and the // staged-compress note all judge ONE rewrite, never the session (#4227). + // #3307: keep the raw-inbound total alongside (log-only) so one capture + // shows the view-factor collapse — V=live vs Vraw=inbound — separating it + // from the residual chars/4-vs-BPE estimator factor without a second trip. + let rawViewTokens = 0; + for (const m of ctx.compressMessages ?? ctx.messages) rawViewTokens += countMessageTokens(m); const shrinkRatio = viewTokens > 0 ? Math.min(1, r.tokensCompressed / viewTokens) : 0; const foldPoint = [...ranges].sort((a, b) => refNum(a.startRef) - refNum(b.startRef))[0]?.startRef ?? "unknown"; ctx.session.lastCompress = { at: Date.now(), shrinkRatio, foldPoint, blocks: r.blocksCreated, tokensCompressed: r.tokensCompressed }; @@ -1205,11 +1219,17 @@ export function applyRanges(parsed: ReturnType, ctx: } // #800: feed the cache ledger — the next request's usage report will // attribute its re-pay cliff to these folds via decomposeSample. - recordCacheFoldsFromBlocks( - ctx.session, - res.state.blocks.filter((b) => !beforeIds.has(b.blockId)), - { V: viewTokens, Vp: postCtx }, - ); + const newFoldBlocks = res.state.blocks.filter((b) => !beforeIds.has(b.blockId)); + // #3307: the ledger credits a fold only when firstFoldStartTokens=X < billed + // input, but X is a kernel chars/4 estimate vs provider-billed input — in long + // sessions X can land >= input and the re-pay drops unattributed. Log X plus the + // same-space view tokens (V/Vp) so one capture separates unit-scale from view + // identity as the inflation source. + if (newFoldBlocks.length > 0) { + const xs = newFoldBlocks.map((b) => (b.startRef ? `${b.startRef}=${prefixTokensBeforeRef(ctx.session, b.startRef)}` : `${b.blockId}=none`)).join(" "); + ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vraw=${rawViewTokens} Vp=${postCtx} (#3307)`); + } + recordCacheFoldsFromBlocks(ctx.session, newFoldBlocks, { V: viewTokens, Vp: postCtx }); const warn = r.warnings.length > 0 ? ` ${r.warnings.join("; ")}` : ""; // #1495: apply-layer per-range errors (unknown refs, …) were invisible diff --git a/src/web/page.ts b/src/web/page.ts index e596f9fa6f..6df33d1844 100644 --- a/src/web/page.ts +++ b/src/web/page.ts @@ -18,8 +18,12 @@ export function renderPage(origin: string, version: string, embed = false, theme const caPath = rootCaPath(); const caPathEsc = escapeHtml(caPath); const caReady = existsSync(caPath); - return `billion-context -
billion-contextv${version}
+ // #3536: declare an EMPTY icon so browsers (dsh desktop's Electron frame + // included) never fall back to probing /favicon.ico on the proxy origin — + // that request used to miss every admin route and get relayed onto the + // configured upstream (a wasted request + 403 noise in the log). + return `billion-context +
billion-contextv${version}
diff --git a/tests/cache-ledger.test.ts b/tests/cache-ledger.test.ts index a85ca9722d..feb47e922c 100644 --- a/tests/cache-ledger.test.ts +++ b/tests/cache-ledger.test.ts @@ -683,6 +683,36 @@ test("handleAcpCache renders the CACHE INVALIDATION breakdown incl. unmeasured ( assert.match(text, /unmeasured .*excluded from hit rate/); }); +test("handleAcpCache renders per-line WIRE/UPSTREAM SWITCHES with timestamps like MODEL/KEY (#3307)", () => { + // wire-only flip (mirrors the #1536 wire fixture) — wire wins its own partition + const wireSession = makeSession(); + wireSession.metadata.lastModel = "gpt-5"; + recordCacheSample(wireSession, { at: T0 + 1000, input: 10000, cached: 9000, protocol: "anthropic" }); + recordCacheSample(wireSession, { at: T0 + 2000, input: 11000, cached: 0, protocol: "openai" }); + const wireText = handleAcpCache(wireSession).text; + assert.match(wireText, /WIRE SWITCHES/); + assert.match(wireText, /anthropic → openai/); + // upstream-only flip (mirrors the #1536 upstream fixture) — upstream wins its own partition + const upSession = makeSession(); + upSession.metadata.lastModel = "gpt-5"; + recordCacheSample(upSession, { at: T0 + 1000, input: 10000, cached: 9000, protocol: "openai", upstream: "https://api.openai.com" }); + recordCacheSample(upSession, { at: T0 + 2000, input: 11000, cached: 0, protocol: "openai", upstream: "https://relay.example.com" }); + const upText = handleAcpCache(upSession).text; + assert.match(upText, /UPSTREAM SWITCHES/); + assert.match(upText, /https:\/\/api\.openai\.com → https:\/\/relay\.example\.com/); +}); + +test("handleAcpCache renders a per-line RESTART/REFORK section with its event (#3307)", () => { + const session = makeSession(); + session.metadata.lastModel = "gpt-5"; + recordCacheSample(session, { at: T0 + 1000, input: 10000, cached: 9000 }); + (session.metadata["cacheLedger"] as Record).lastBoot = "previous-boot"; + recordCacheSample(session, { at: T0 + 2000, input: 11000, cached: 0 }); + const text = handleAcpCache(session).text; + assert.match(text, /RESTART\/REFORK/); + assert.match(text, /→ \(restart\)/); +}); + test("per-cause buckets partition a multi-flag sample's residual (#1847)", () => { const session = makeSession(); session.metadata.lastModel = "gpt-5"; diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index 194a7d1d42..44c8c8bb5e 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -1615,8 +1615,10 @@ test("hook e2e: watchdog tracks the claude host, not the transient sh wrapper", assert.ok(await canConnect(port), "proxy survives the transient sh wrapper's death"); // Session end: claude dies, the proxy must follow within a few ticks. + // #3890 widened owner-death→exit by up to ~6s (three consecutive failed + // probes now confirm death instead of one) — deadline re-sized to match. if (claudePid > 1) killPid(claudePid); - const deadline = Date.now() + 15_000; + const deadline = Date.now() + 30_000; while (Date.now() < deadline && (await canConnect(port))) await new Promise((r) => setTimeout(r, 250)); assert.equal(await canConnect(port), false, "proxy exits with the claude session"); } finally { @@ -1697,17 +1699,22 @@ test("watcher route: shared proxies take watcher registrations, daemons refuse ( // Grace: keeper1 dies, and BEFORE the idle grace expires a new owner // registers (the live race: spawner exits right after a second session // starts). The pending shutdown must be cancelled — the proxy stays up - // well past the grace window. + // well past the grace window. #3890: keeper1's removal now takes ~6s + // (three consecutive failed probes confirm death), so the late + // registration lands AFTER removal but INSIDE the 5s idle grace that + // follows it — still exercising the cancellation of an armed pending + // shutdown (which would otherwise fire ~11s after the death). if (keeperPid > 1) killPid(keeperPid); - await new Promise((r) => setTimeout(r, 3000)); + await new Promise((r) => setTimeout(r, 8000)); const late = await post({ pid: keeper2Pid }); assert.equal(late.status, 200, "late registration within the grace window accepted"); - await new Promise((r) => setTimeout(r, 7000)); + await new Promise((r) => setTimeout(r, 8000)); assert.ok(await canConnect(port), "grace registration cancelled the pending shutdown"); // LAST owner dies → armed proxy must follow (≤ a few ticks + grace). + // #3890 widened owner-death→exit by up to ~6s (confirmation ticks). if (keeper2Pid > 1) killPid(keeper2Pid); - const deadline = Date.now() + 15_000; + const deadline = Date.now() + 30_000; while (Date.now() < deadline && (await canConnect(port))) await new Promise((r) => setTimeout(r, 250)); assert.equal(await canConnect(port), false, "armed proxy exits when its watcher dies"); @@ -1915,8 +1922,9 @@ test("hook e2e: shared proxy survives the first session's exit, dies after the l assert.equal(instMid.pid, proxyPid, "same proxy instance throughout — no respawn"); // Last session exits → the proxy must follow within a few ticks. + // #3890 widened owner-death→exit by up to ~6s (confirmation ticks). if (claudeB > 1) killPid(claudeB); - const deadline = Date.now() + 15_000; + const deadline = Date.now() + 30_000; while (Date.now() < deadline && (await canConnect(port))) await new Promise((r) => setTimeout(r, 250)); assert.equal(await canConnect(port), false, "proxy exits after the LAST session"); } finally { diff --git a/tests/fixtures/issue3890-drain-bili.mts b/tests/fixtures/issue3890-drain-bili.mts new file mode 100644 index 0000000000..cb51b79a81 --- /dev/null +++ b/tests/fixtures/issue3890-drain-bili.mts @@ -0,0 +1,38 @@ +// Child-process fixture for tests/issue3890-parent-gone-confirm-drain.test.ts: +// a minimal bili server in its OWN process because finishShutdown ends with +// process.exit(0) — an in-process test would kill the runner. Prints +// "READY " on stdout once listening; all bili logs go to stderr +// (logFile: "off"), which the parent test asserts against. +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../../src/server.js"; +import type { ProxyOptions } from "../../src/config.js"; + +const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + logFile: "off", + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, +}; +startServer(opts).then((server) => { + server.on("listening", () => { + const port = (server.address() as { port: number }).port; + console.log(`READY ${port}`); + }); +}); diff --git a/tests/issue1815-unknown-top-level-keys.test.ts b/tests/issue1815-unknown-top-level-keys.test.ts index 8e3ea49171..3a78e3e9e9 100644 --- a/tests/issue1815-unknown-top-level-keys.test.ts +++ b/tests/issue1815-unknown-top-level-keys.test.ts @@ -75,6 +75,18 @@ test("#1815: root-level promptPack (issue repro) warns through the real config-l } }); +test("#3876: bare top-level externalSummary gets the compress-placement hint", () => { + const { captured, stop } = captureLogs(); + try { + warnUnknownTopLevelKeys({ externalSummary: { enabled: true, targets: ["p/m"] } }); + const warns = unknownKeyWarns(captured); + assert.equal(warns.length, 1, `expected 1 warning, got: ${captured.map((e) => e.msg).join(" | ")}`); + assert.ok(warns[0].msg.includes('"externalSummary" belongs under "compress" (did you mean "compress.externalSummary"?)'), warns[0].msg); + } finally { + stop(); + } +}); + test("#1815: multiple unknown keys list together; fixing some re-warns about the rest", () => { const { captured, stop } = captureLogs(); try { diff --git a/tests/issue2752-forward-partial-outcome.test.ts b/tests/issue2752-forward-partial-outcome.test.ts index 1581013a71..405be4e057 100644 --- a/tests/issue2752-forward-partial-outcome.test.ts +++ b/tests/issue2752-forward-partial-outcome.test.ts @@ -457,7 +457,9 @@ describe("#2752: the shim's tools/call response carries structuredContent on the assert.ok(r.result, "a result object comes back"); assert.notEqual(r.result!.isError, true, "partial is NOT an error on the wire"); assert.equal(r.result!.content?.[0]?.text, `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`); - assert.deepEqual(r.result!.structuredContent, { outcome: "partial", blocksCreated: 1 }); + // #3472: structuredContent also carries the full report text so + // structured-preferring hosts do not lose the receipt body. + assert.deepEqual(r.result!.structuredContent, { text: `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`, outcome: "partial", blocksCreated: 1 }); }); it("failure: isError true with its outcome still on structuredContent", async () => { @@ -465,7 +467,8 @@ describe("#2752: the shim's tools/call response carries structuredContent on the const r = await client.request(2, "tools/call", { name: "decompress", arguments: { blockId: "b999" } }); assert.ok(!r.error, `no JSON-RPC error: ${JSON.stringify(r.error)}`); assert.equal(r.result?.isError, true); - assert.deepEqual(r.result?.structuredContent, { outcome: "failure" }); + // #3472: the failure receipt text rides structuredContent too. + assert.deepEqual(r.result?.structuredContent, { text: FAILURE_TEXT, outcome: "failure" }); }); it("legacy envelope: no structuredContent field at all", async () => { diff --git a/tests/issue3243-manifest-prompt-pack.test.ts b/tests/issue3243-manifest-prompt-pack.test.ts new file mode 100644 index 0000000000..323214f508 --- /dev/null +++ b/tests/issue3243-manifest-prompt-pack.test.ts @@ -0,0 +1,161 @@ +// #3243 regression: the plugin manifest (/__bili/plugin/manifest) must serve +// the pack surface's tool descriptions. Before the fix handlePluginManifest +// built every tool array from kernel constants and never applied toolPrompts — +// a configured compress.promptPack shaped each request's system prompt (proxy +// AND plugin mode) while registered native-lane tools (pi/omp/dsh/opencode/MCP) +// always saw kernel-default descriptions. The manifest resolves the surface +// from the GLOBAL compress settings only (#1192 conservative rule: hosts fetch +// it before any request arrives, no route/model context); route/model-level +// promptPack still shapes per-request system prompts but never the manifest. +// Expectations come from the installed kernel's builtin lean pack via the SAME +// resolver the server uses, so this pins the wiring, not any wording. + +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { resolveCompressSurface } from "../src/compress-settings.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { CompressSettings, ProxyOptions } from "../src/config.ts"; + +type GlobalCompress = CompressSettings & { injectTool?: boolean; injectNudge?: boolean }; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +function leanToolPrompts() { + const tp = resolveCompressSurface({ promptPack: "lean" }).toolPrompts; + assert.ok(tp && Object.keys(tp).length > 0, "builtin lean pack resolves with non-empty toolPrompts"); + assert.ok(tp.compress?.description, "lean compress carries a description override"); + assert.ok(tp.compress?.paramDescriptions?.content, "lean compress carries a content paramDescription"); + return tp; +} + +type Harness = { proxyPort: number; close(): Promise }; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +async function startHarness(globalCompress: GlobalCompress, routeCompress?: CompressSettings): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "r1", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }] })); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { compress: routeCompress } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: globalCompress, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + } as ProxyOptions); + await listen(proxy); + return { + proxyPort: (proxy.address() as { port: number }).port, + close: async () => { + await new Promise((resolve, reject) => proxy.close((error) => (error ? reject(error) : resolve()))); + await new Promise((resolve, reject) => upstream.close((error) => (error ? reject(error) : resolve()))); + }, + }; +} + +async function fetchManifest(h: Harness): Promise> { + const res = await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/plugin/manifest`); + assert.equal(res.status, 200, "manifest endpoint reachable on loopback"); + const doc = (await res.json()) as { ok: boolean; tools: Record }; + assert.ok(doc.ok, "manifest reports ok"); + return doc.tools; +} + +function findTool(tools: unknown[], name: string): Record { + const t = tools.find((x) => { + const o = x as Record; + return o?.name === name || o?.function?.name === name; + }); + assert.ok(t, `${name} advertised`); + return t as Record; +} + +function descriptionOf(t: Record): string { + return typeof t.description === "string" ? t.description : t.function.description; +} + +function parametersOf(t: Record): Record | undefined { + return t.input_schema ?? t.parameters ?? t.function?.parameters; +} + +const WIRES = [["anthropic", ACP_TOOLS_ANTHROPIC], ["openai", ACP_TOOLS_OPENAI], ["responses", ACP_TOOLS_RESPONSES]] as const; + +test("#3243: global promptPack reaches plugin-manifest tool descriptions on all three wires", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true, promptPack: "lean" }); + try { + const tools = await fetchManifest(h); + const tp = leanToolPrompts(); + for (const [wire, defaults] of WIRES) { + for (const [name, overrides] of Object.entries(tp)) { + const tool = findTool(tools[wire], name); + if (overrides.description !== undefined) { + // #3252 appends the calling-convention binding note AFTER the pack + // description — the pack text must be the PREFIX, not the whole string. + assert.ok(descriptionOf(tool)?.startsWith(overrides.description), `${wire}.${name} description from lean pack`); + } + } + // CCR is off in this rig, so decompress here is the NO_RANGE variant — + // the pack override must land on it too (matched by tool name). + const compress = findTool(tools[wire], "compress"); + const kernelDefault = (defaults as readonly Record[]).find((d) => d.name === "compress" || d.function?.name === "compress")!; + const defaultDesc = typeof kernelDefault.description === "string" ? kernelDefault.description : kernelDefault.function.description; + assert.notEqual(descriptionOf(compress), defaultDesc, `${wire}.compress not the kernel default`); + assert.equal(parametersOf(compress)?.properties?.content?.description, tp.compress.paramDescriptions?.content, `${wire}.compress paramDescriptions applied`); + } + } finally { + await h.close(); + } +}); + +test("#3243: no promptPack — manifest keeps kernel-default compress descriptions", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }); + try { + const tools = await fetchManifest(h); + for (const [wire, defaults] of WIRES) { + const compress = findTool(tools[wire], "compress"); + const kernelDefault = (defaults as readonly Record[]).find((d) => d.name === "compress" || d.function?.name === "compress")!; + const defaultDesc = typeof kernelDefault.description === "string" ? kernelDefault.description : kernelDefault.function.description; + // #3252: the binding note rides along after the kernel default text. + assert.ok(descriptionOf(compress)?.startsWith(defaultDesc), `${wire}.compress stays kernel default`); + } + } finally { + await h.close(); + } +}); + +test("#3243: route-level promptPack never leaks into the manifest (#1192 conservative boundary)", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, { promptPack: "lean" }); + try { + const tools = await fetchManifest(h); + for (const [wire, defaults] of WIRES) { + const compress = findTool(tools[wire], "compress"); + const kernelDefault = (defaults as readonly Record[]).find((d) => d.name === "compress" || d.function?.name === "compress")!; + const defaultDesc = typeof kernelDefault.description === "string" ? kernelDefault.description : kernelDefault.function.description; + assert.ok(descriptionOf(compress)?.startsWith(defaultDesc), `${wire}.compress unaffected by route-level pack (#3252 binding note appended)`); + } + } finally { + await h.close(); + } +}); diff --git a/tests/issue3301-shape-flip-reanchor.test.ts b/tests/issue3301-shape-flip-reanchor.test.ts new file mode 100644 index 0000000000..3fde649924 --- /dev/null +++ b/tests/issue3301-shape-flip-reanchor.test.ts @@ -0,0 +1,340 @@ +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { + extractThinkingMerge, + planReconciliation, + reconcileFoldCoverage, + turnContexts, + normalizedIdentity, + type FoldAnchor, + type ReconcileOptions, +} from "../src/fold-reconcile.ts"; +import { resolveStoredDecompositions, type StoredRow } from "../src/fold-decomposition-lookup.ts"; +import { planIngest, planIngestDelta, type LegacySessionLike } from "../src/storage/ingest.ts"; +import type { CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; + +function msg(id: string, role: string, text: string, extra?: Partial): CoreMessage { + return { id, role, contentType: "text", text, ...extra } as CoreMessage; +} + +const THINK_OPEN = "\x3cthink\x3e\n"; +const THINK_CLOSE = "\n\x3c/think\x3e"; +const THINKING_OPEN = "\x3cthinking\x3e\n"; +const THINKING_CLOSE = "\n\x3c/thinking\x3e"; +const FENCE_OPEN = "```thinking\n"; +const FENCE_CLOSE = "\n```"; + +describe("extractThinkingMerge (#3301)", () => { + test("each kernel-listed form splits at offset 0", () => { + assert.deepEqual(extractThinkingMerge(`${THINK_OPEN}R${THINK_CLOSE}\nT`), { reasoning: "R", text: "T" }); + assert.deepEqual(extractThinkingMerge(`${THINKING_OPEN}R${THINKING_CLOSE}\nT`), { reasoning: "R", text: "T" }); + assert.deepEqual(extractThinkingMerge(`${FENCE_OPEN}R${FENCE_CLOSE}\nT`), { reasoning: "R", text: "T" }); + }); + test("multi-block runs join reasoning with \\n (kernel parity)", () => { + const s = `${THINK_OPEN}A${THINK_CLOSE}\n${THINK_OPEN}B${THINK_CLOSE}\nT`; + assert.deepEqual(extractThinkingMerge(s), { reasoning: "A\nB", text: "T" }); + }); + test("thinking-only merge keeps empty text", () => { + assert.deepEqual(extractThinkingMerge(`${THINK_OPEN}R${THINK_CLOSE}`), { reasoning: "R", text: "" }); + }); + test("tag not at offset 0 never matches (prose safety)", () => { + assert.equal(extractThinkingMerge(`prefix ${THINK_OPEN}R${THINK_CLOSE}\nT`), null); + assert.equal(extractThinkingMerge(`\n${THINK_OPEN}R${THINK_CLOSE}\nT`), null); + }); + test("empty or unterminated blocks do not match", () => { + assert.equal(extractThinkingMerge(`${THINK_OPEN}${THINK_CLOSE}\nT`), null); + assert.equal(extractThinkingMerge(`${THINK_OPEN}R never closed`), null); + }); + test("plain prose, empty and undefined return null", () => { + assert.equal(extractThinkingMerge("just prose"), null); + assert.equal(extractThinkingMerge(""), null); + assert.equal(extractThinkingMerge(undefined), null); + }); +}); + +describe("turnContexts bridges split vs merged forms (#3301)", () => { + test("merged tagged core carries the same turn keys as the rc pair", () => { + const R = "alpha beta gamma delta epsilon zeta eta theta"; + const T = "one two three four five six seven eight nine ten"; + const rcPairCtx = turnContexts([ + msg("r1", "assistant", R, { contentType: "reasoning" }), + msg("t1", "assistant", T), + ]); + const mergedCtx = turnContexts([ + msg("m1", "assistant", `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`), + ]); + const pair = rcPairCtx.get("r1"); + const merged = mergedCtx.get("m1"); + assert.ok(pair && merged); + assert.equal(merged!.p, pair!.p, "primary seam key matches"); + assert.equal(merged!.p2 ?? merged!.p, pair!.p2 ?? pair!.p, "seamless key matches"); + }); +}); + +describe("planReconciliation shape flips (#3301)", () => { + const R = "alpha beta gamma delta epsilon zeta eta theta"; + const T = "one two three four five six seven eight nine ten"; + const MERGED = `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`; + + function rcAnchors(): Record { + const r1 = msg("r1", "assistant", R, { contentType: "reasoning" }); + const t1 = msg("t1", "assistant", T); + const ctx = turnContexts([r1, t1]); + const a1 = { n: normalizedIdentity(r1), r: "assistant", c: "reasoning", b: R.length, p: ctx.get("r1")?.p, p2: ctx.get("r1")?.p2, x: "t1" }; + const a2 = { n: normalizedIdentity(t1), r: "assistant", c: "text", b: T.length, p: ctx.get("t1")?.p, p2: ctx.get("t1")?.p2, x: "r1" }; + return { r1: a1, t1: a2 }; + } + + test("rc pair -> merged core: both member ids claim the one witness", () => { + const merged = msg("m1", "assistant", MERGED); + const plan = planReconciliation(["r1", "t1"], rcAnchors(), [merged], new Set(["r1", "t1"])); + assert.deepEqual(plan.claims.get("r1"), ["m1"]); + assert.deepEqual(plan.claims.get("t1"), ["m1"]); + assert.equal(plan.unmatched.length, 0); + assert.ok(plan.byTurn >= 1, `turn-key pass did the bridging (counters: ${JSON.stringify({ byTurn: plan.byTurn, bySim: plan.bySim, byOrder: plan.byOrder })})`); + }); + + test("merged core -> rc pair: one old id claims both new ids", () => { + const merged = msg("m1", "assistant", MERGED); + const ctx = turnContexts([merged]); + const anchors = { m1: { n: normalizedIdentity(merged), r: "assistant", c: "text", b: MERGED.length, p: ctx.get("m1")?.p } }; + const plan = planReconciliation(["m1"], anchors, [ + msg("r2", "assistant", R, { contentType: "reasoning" }), + msg("t2", "assistant", T), + ], new Set(["m1"])); + assert.deepEqual(plan.claims.get("m1"), ["r2", "t2"]); + assert.equal(plan.unmatched.length, 0); + assert.ok(plan.byTurn >= 1); + }); + + test("untagged merges stay bridgeable through the bare-concat keys", () => { + for (const seam of ["\n", ""]) { + const mergedUntagged = msg(`mu-${seam === "\n" ? "nl" : "flat"}`, "assistant", R + seam + T); + const plan = planReconciliation(["r1", "t1"], rcAnchors(), [mergedUntagged], new Set(["r1", "t1"])); + assert.equal(plan.unmatched.length, 0, `seam ${JSON.stringify(seam)} bridged`); + assert.deepEqual(plan.claims.get("r1"), [mergedUntagged.id]); + } + }); + + test("ambiguous duplicate turns skip honestly (never fuzzy)", () => { + const mergedA = msg("mA", "assistant", MERGED); + const mergedB = msg("mB", "assistant", MERGED); + const plan = planReconciliation(["r1", "t1"], rcAnchors(), [mergedA, mergedB], new Set(["r1", "t1"])); + assert.equal(plan.claims.size, 0); + assert.deepEqual(new Set(plan.unmatched), new Set(["r1", "t1"])); + }); +}); + +describe("reconcileFoldCoverage end-to-end flips (#3301)", () => { + function fakeSession(blocks: { effectiveMessageIds: string[]; directMessageIds?: string[] }[]): Session { + return { + state: { blocks: blocks.map((b) => ({ active: true, ...b })) }, + metadata: {}, + } as unknown as Session; + } + const opts = (extra?: Partial) => + ({ mode: "repair", sessionId: "s1", log: () => {}, ...extra }) satisfies ReconcileOptions; + + // 10 fillers + one rc thinking turn (ids f0..f8, r1, t1) + function rcHistory() { + const R = "alpha beta gamma delta epsilon zeta eta theta"; + const T = "one two three four five six seven eight nine ten"; + const fillers = Array.from({ length: 9 }, (_, i) => msg(`f${i}`, "user", `filler context words number ${i}`)); + const r1 = msg("r1", "assistant", R, { contentType: "reasoning" }); + const t1 = msg("t1", "assistant", T); + return { R, T, all: [...fillers.slice(0, 5), r1, t1, ...fillers.slice(5)] }; + } + + test("rc -> merged: covered thinking turn survives the flip", () => { + const { R, T, all } = rcHistory(); + const allIds = all.map((m) => m.id!); + const session = fakeSession([{ effectiveMessageIds: allIds, directMessageIds: allIds }]); + reconcileFoldCoverage(session, all, opts()); + const merged = msg("m1", "assistant", `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`); + const flipped = all.filter((m) => m.id !== "t1").map((m) => (m.id === "r1" ? merged : m)); + const result = reconcileFoldCoverage(session, flipped, opts()); + assert.equal(result.unmatched, 0, `drift residual must be zero (got ${JSON.stringify(result)})`); + const rewritten = allIds.map((id) => (id === "r1" || id === "t1" ? "m1" : id)); + assert.deepEqual(session.state.blocks[0].effectiveMessageIds, rewritten); + assert.deepEqual(session.state.blocks[0].directMessageIds, rewritten); + }); + + test("merged -> rc: the reverse flip survives too", () => { + const { R, T, all } = rcHistory(); + const mergedFirst = all.map((m) => (m.id === "r1" ? msg("m1", "assistant", `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`) : m)).filter((m) => m.id !== "t1"); + const idsAfterMerge = mergedFirst.map((m) => m.id!); + const session = fakeSession([{ effectiveMessageIds: idsAfterMerge, directMessageIds: idsAfterMerge }]); + reconcileFoldCoverage(session, mergedFirst, opts()); + const backToRc = mergedFirst.flatMap((m) => m.id === "m1" + ? [msg("r2", "assistant", R, { contentType: "reasoning" }), msg("t2", "assistant", T)] + : [m]); + const result = reconcileFoldCoverage(session, backToRc, opts()); + assert.equal(result.unmatched, 0, `reverse flip must fully re-anchor (got ${JSON.stringify(result)})`); + const expected = idsAfterMerge.flatMap((id) => (id === "m1" ? ["r2", "t2"] : [id])); + assert.deepEqual(session.state.blocks[0].effectiveMessageIds, expected); + }); + + test("pre-#2644 anchors heal on the flip pass itself from stored decompositions", () => { + const { R, T, all } = rcHistory(); + const allIds = all.map((m) => m.id!); + const session = fakeSession([{ effectiveMessageIds: allIds, directMessageIds: allIds }]); + reconcileFoldCoverage(session, all, opts()); + // emulate anchors persisted before turn keys existed + const anchors = session.metadata.foldAnchors as Record; + for (const id of ["r1", "t1"]) { + delete anchors[id].p; + delete anchors[id].p2; + delete anchors[id].x; + } + const merged = msg("m1", "assistant", `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`); + const flipped = all.filter((m) => m.id !== "t1").map((m) => (m.id === "r1" ? merged : m)); + const stored = [ + msg("r1", "assistant", R, { contentType: "reasoning" }), + msg("t1", "assistant", T), + ]; + // The flip pass is the LAST pass in which r1/t1 sit in the order + // backbone (nextOrder rolls forward to the flipped history), so the + // stored-decomposition backfill must land BEFORE the plan runs and + // this very pass must claim — a later backfill can never do it. + const pass1 = reconcileFoldCoverage(session, flipped, opts({ storedMessages: stored })); + assert.equal(pass1.unmatched, 0, `flip pass claims through stored keys (got ${JSON.stringify(pass1)})`); + const rewritten = allIds.map((id) => (id === "r1" || id === "t1" ? "m1" : id)); + assert.deepEqual(session.state.blocks[0].effectiveMessageIds, rewritten); + const healed = session.metadata.foldAnchors as Record; + assert.ok(healed["m1"] !== undefined && healed["m1"].p !== undefined, "merged core anchored with extracted turn key"); + assert.equal(healed["r1"], undefined, "old form ids retired from anchors"); + assert.equal(healed["t1"], undefined); + const pass2 = reconcileFoldCoverage(session, flipped, opts()); + assert.equal(pass2.unmatched, 0, "steady state after recovery is clean"); + }); + + test("legacy anchors without a stored source stay honestly blind", () => { + const { R, T, all } = rcHistory(); + const allIds = all.map((m) => m.id!); + const session = fakeSession([{ effectiveMessageIds: allIds }]); + reconcileFoldCoverage(session, all, opts()); + const anchors = session.metadata.foldAnchors as Record; + for (const id of ["r1", "t1"]) { + delete anchors[id].p; + delete anchors[id].p2; + delete anchors[id].x; + } + const merged = msg("m1", "assistant", `${THINK_OPEN}${R}${THINK_CLOSE}\n${T}`); + const flipped = all.filter((m) => m.id !== "t1").map((m) => (m.id === "r1" ? merged : m)); + const result = reconcileFoldCoverage(session, flipped, opts()); + assert.equal(result.unmatched, 2, "no index, no wire bytes: honest miss, no fuzzy claim"); + }); +}); + +describe("resolveStoredDecompositions (#3301)", () => { + function lookupSession(anchorCount: number, mutate?: (anchors: Record) => void): Session { + const anchors: Record = {}; + for (let i = 0; i < anchorCount; i++) { + anchors[`a${i}`] = { n: `n${i}`, r: "assistant", c: "text", b: 10 }; + } + mutate?.(anchors); + return { state: { blocks: [] }, metadata: { foldAnchors: anchors } } as unknown as Session; + } + const wireIds = new Set(); + + test("filters to legacy bare-assistant anchors off the wire, caps at 256", () => { + const session = lookupSession(300, (a) => { + a["a0"].p = "has-key"; + a["a1"].r = "user"; + a["a2"].t = "tool-call-id"; + }); + const seenCalls: string[][] = []; + const source = { rows: (_sid: string, ids: readonly string[]) => { seenCalls.push([...ids]); return []; } }; + const out = resolveStoredDecompositions(session, "s1", wireIds, source); + assert.equal(out, undefined, "empty rows yield undefined"); + assert.equal(seenCalls.length, 1, "source consulted once"); + const seen = seenCalls[0]; + assert.equal(seen.length, 256, "cap applied"); + assert.ok(!seen.includes("a0") && !seen.includes("a1") && !seen.includes("a2"), "ineligible anchors excluded"); + }); + + test("rows === null (index unavailable) does not burn the retry budget", () => { + const session = lookupSession(3); + let calls = 0; + const source = { rows: () => { calls++; return null; } }; + assert.equal(resolveStoredDecompositions(session, "s1", wireIds, source), undefined); + assert.equal(resolveStoredDecompositions(session, "s1", wireIds, source), undefined); + assert.equal(calls, 2, "still retried on the next pass"); + }); + + test("absent rows mark tried once; later passes skip the lookup", () => { + const session = lookupSession(3); + let calls = 0; + const source = { rows: () => { calls++; return []; } }; + assert.equal(resolveStoredDecompositions(session, "s1", wireIds, source), undefined); + assert.equal(calls, 1); + assert.equal(resolveStoredDecompositions(session, "s1", wireIds, source), undefined); + assert.equal(calls, 1, "tried-set suppresses repeat lookups"); + }); + + test("latest-seq-per-id wins, neighbors kept, output sorted by seq", () => { + const rows: StoredRow[] = [ + { seq: 1, msgId: "r1", content: JSON.stringify(msg("r1", "assistant", "OLD", { contentType: "reasoning" })) }, + { seq: 2, msgId: "t1", content: JSON.stringify(msg("t1", "assistant", "reply")) }, + { seq: 3, msgId: "r1", content: JSON.stringify(msg("r1", "assistant", "NEW", { contentType: "reasoning" })) }, + { seq: 4, msgId: "u2", content: "{not-json" }, + ]; + const session = lookupSession(2); + delete (session.metadata.foldAnchors as Record)["a1"]; + const source = { rows: () => rows }; + const out = resolveStoredDecompositions(session, "s1", wireIds, source); + assert.ok(out !== undefined); + assert.deepEqual(out!.map((m) => m.id), ["t1", "r1"], "sorted by surviving seq (t1=2 < r1=3), unparseable row dropped"); + assert.equal(out![1].text, "NEW", "latest serialization wins"); + }); + + test("wire-present ids are excluded from the lookup", () => { + const session = lookupSession(2); + let seen: readonly string[] = []; + const source = { rows: (_sid: string, ids: readonly string[]) => { seen = ids; return []; } }; + resolveStoredDecompositions(session, "s1", new Set(["a0"]), source); + assert.deepEqual(seen, ["a1"]); + }); +}); + +describe("decompositionLog ingest (#3301)", () => { + function mkLegacy(id: string, lastMessages: CoreMessage[], decompositionLog?: CoreMessage[]): LegacySessionLike { + return { + id, + createdAt: 1_000, + lastSeen: 2_000, + meta: { protocol: "openai" }, + stats: { requests: 1 }, + metadata: {}, + state: { + blocks: [], + messageRefs: { byRaw: {}, byRef: {} }, + nextBlockId: 1, + nextRunId: 1, + }, + blockContents: new Map(), + lastMessages, + lastMessagesFolded: false, + decompositionLog, + }; + } + + test("import payload rows sit below tail seqs and dedup against the tail", () => { + const r1 = msg("r1", "assistant", "R words", { contentType: "reasoning" }); + const t1 = msg("t1", "assistant", "T words"); + const u2 = msg("u2", "user", "hello"); + // payload: one older message + a byte-identical copy of a tail message + const legacy = mkLegacy("imp", [r1, t1, u2], [msg("d0", "user", "older evidence"), r1]); + const plan = planIngest(legacy); + assert.deepEqual(plan.messages.map((m) => m.msgId), ["d0", "r1", "t1", "u2"], "payload interned first, duplicate not re-logged"); + assert.deepEqual(plan.tailSeqs, [1, 2, 3]); + }); + + test("delta planner forces the full path while an import payload is present", () => { + const r1 = msg("r1", "assistant", "R words", { contentType: "reasoning" }); + const legacy = mkLegacy("imp2", [r1], [msg("d0", "user", "older evidence")]); + const out = planIngestDelta(legacy, 0, [], { msgId: "x", contentHash: "y" }, true); + assert.equal(out, null, "payload must not be skipped as trusted prefix"); + }); +}); diff --git a/tests/issue3307-fold-attribution-wire.test.ts b/tests/issue3307-fold-attribution-wire.test.ts new file mode 100644 index 0000000000..0d7b0d0ce1 --- /dev/null +++ b/tests/issue3307-fold-attribution-wire.test.ts @@ -0,0 +1,102 @@ +// #3307 P2-A regression: a stateless proxy client resends its full un-shrunken +// history every turn, so the snapshot-based fold prefix X (prefixTokensBeforeRef) +// far exceeds the billed input and decomposeSample zeroes structuralExcess — the +// fold's re-pay then falls into the unattributed seam bucket instead of being +// credited to the fold. P2-A measures the stable prefix off the wire (byte-LCP of +// this outbound vs the previous, scaled to billed input) and min()s it into X, so +// a genuine bili-side mid-history break attributes to its fold again. These pin the +// fix AND its two guards: a pure tail-append must NOT be credited to a pending fold +// (no false credit on normal growth), and a client-side inbound rewrite (#3023) must +// NOT be mis-credited either. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { noteForwardedBody, noteInboundMessages, settleUsageReport, getCacheLedger } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `foldwire-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const body = (msgs: string[]): string => JSON.stringify({ model: "m", messages: msgs.map((c) => ({ role: "user", content: c })) }); +// Anchor in the past relative to the live clock: settleUsageReport stamps each +// sample with Date.now(), so a pushed fold's createdAt must precede it to become +// pending (a fixed future literal would silently drop the fold from the window). +const T0 = Date.now() - 3_600_000; + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +// Snapshot whose pre-fold prefix sums to 49*6000 = 294000 tokens — larger than any +// billed input below, so the UNcorrected firstFoldStartTokens zeroes structuralExcess +// exactly like the production stateless-resend case. +function makeSessionWithInflatedPrefix(): Session { + const s = makeSession(); + const st = s.state as unknown as { tokenSnapshot: Record; messageRefs: { byRef: Record } }; + for (let i = 1; i <= 49; i++) { + const r = `m${String(i).padStart(5, "0")}`; + st.tokenSnapshot[r] = 6000; + st.messageRefs.byRef[r] = `raw_${i}`; + } + return s; +} + +function pushFold(s: Session, at: number): void { + const st = s.state as unknown as { blocks: Array> }; + // startRef m00050 -> prefixTokensBeforeRef sums m00001..m00049 = 294000. + st.blocks.push({ blockId: "b1", createdAt: at, compressedTokens: 30_000, summary: "x".repeat(200), startRef: "m00050" }); +} + +test("P2-A: inflated snapshot X + wire mid-history break attributes the fold (not a seam)", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b", "c", "d"])); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Next turn: a summary replaces message[1] (mid-history break), big residual. + noteForwardedBody(s, body(["a", "SUMMARY", "c", "d"])); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.ok(line.foldSeq !== null, "the fold's re-pay must be attributed via the wire-measured prefix (uncorrected X=294000 would zero structuralExcess)"); + assert.notEqual(line.seam, 1, "an attributed fold must not flag as an unexplained seam"); +}); + +test("P2-A: a pure tail-append with a pending inflated-X fold is NOT credited to the fold", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Pure tail append — no mid-history break, so the wire correction must not fire. + noteForwardedBody(s, body(["a", "b", "c"])); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.foldSeq, null, "tail-append growth must not be credited to the fold"); + assert.equal(led.agg.providerSideMisses, 1, "the tail-append miss stays provider-side"); + assert.notEqual(line.seam, 1); +}); + +test("P2-A: a client-side inbound rewrite is NOT credited to a pending fold (#3023)", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b", "c"])); + noteInboundMessages(s, [{ role: "user", content: "a" }, { role: "user", content: "b" }, { role: "user", content: "c" }] as unknown as CoreMessage[]); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Outbound mid-break AND inbound mid-rewrite (index 1 changed) — host churn, not bili. + noteForwardedBody(s, body(["a", "X2", "c"])); + noteInboundMessages(s, [{ role: "user", content: "a" }, { role: "user", content: "CHANGED" }, { role: "user", content: "c" }] as unknown as CoreMessage[]); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.foldSeq, null, "client rewrite must not be credited to the fold"); + assert.equal(line.prov, "client", "classified client-side (#3023)"); + assert.equal(led.agg.clientInserts, 1); +}); diff --git a/tests/issue3307-fold-view-basis.test.ts b/tests/issue3307-fold-view-basis.test.ts new file mode 100644 index 0000000000..144c7cc7f4 --- /dev/null +++ b/tests/issue3307-fold-view-basis.test.ts @@ -0,0 +1,171 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig, countMessageTokens } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { getCacheLedger } from "../src/cache-ledger.ts"; + +// #3307 P1 regression: the observability denominator V must measure the LIVE +// context being managed (ctx.messages / processedMessages — what gets billed +// next), NOT the client's un-shrunken resend (ctx.compressMessages). A stateless +// client (codex_webui) re-sends its FULL history every turn, so the old +// `compressMessages ?? messages` basis made V ~50x the billed input and read every +// real fold as a ~1% shrink. These tests pin the corrected basis and verify the +// numerator/denominator share one view's net change (Vp === V - S). + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeSession() { + return { + id: "fold-view-basis-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +// The raw resend: refs are assigned over THIS array (fold geometry maps onto +// compressMessages), mirroring production where processTurn ran over the client's +// un-shrunken history. Big enough per message to clear the kernel's min range. +function makeRaw(n: number): CoreMessage[] { + const out: CoreMessage[] = []; + for (let i = 1; i <= n; i++) out.push({ id: `raw_${i}`, role: i % 2 === 1 ? "user" : "assistant", contentType: "text", text: `RAW-${i}-` + "x".repeat(20000) }); + return out; +} + +// The compressed live view (ctx.messages / processedMessages) BEFORE this fold: +// an early span already summarized, plus the recent turns still unfolded — so it is +// far smaller than the raw resend yet still LARGER than any single fold target (the +// pre-fold live view contains the about-to-be-folded content unfolded, hence V >= S). +function makeLiveView(): CoreMessage[] { + // Production reality: the live view derives from the SAME resent cores as + // compressMessages, so the about-to-be-folded span carries the SAME ids in + // both views (raw_1/raw_2 here) — the #4224 union map resolves coverage + // through them. Only the already-summarized prefix carries a view-local id. + return [ + { id: "sum_early", role: "user", contentType: "text", text: "[summary] ancient turns folded away" }, + { id: "raw_1", role: "assistant", contentType: "text", text: "LIVE-" + "y".repeat(20000) }, + { id: "raw_2", role: "user", contentType: "text", text: "LIVE-" + "z".repeat(20000) }, + { id: "live_c", role: "assistant", contentType: "text", text: "LIVE-" + "w".repeat(20000) }, + ]; +} + +function totalTokens(msgs: CoreMessage[]): number { + return msgs.reduce((n, m) => n + countMessageTokens(m), 0); +} + +// refs come from the RAW array (geometry space); messages is the smaller live view. +function makeCtx(raw: CoreMessage[], live: CoreMessage[]): Ctx { + const logs: string[] = []; + const session = makeSession(); + const res = assignRefs(raw, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + return { + core: createCore(), + config: defaultConfig(200000), + messages: live, + compressMessages: raw, + session, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +const SUMMARY = "OBS-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK"; +const ARGS_1_2 = { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }; +const ARGS_3_4 = { content: [{ startId: "m00003", endId: "m00004", summary: SUMMARY }] }; + +test("#3307 P1: V tracks the live view (ctx.messages), never the raw resend (compressMessages)", () => { + const raw = makeRaw(6); + const live = makeLiveView(); + const rawTotal = totalTokens(raw); + const liveTotal = totalTokens(live); + assert.ok(rawTotal > liveTotal, `precondition: raw (${rawTotal}) must exceed live (${liveTotal}) so the assertion means something`); + + const ctx = makeCtx(raw, live); + getCacheLedger(ctx.session); // bootstrap the ledger before its first fold (#1911 pattern) + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 1, "one fold recorded"); + assert.equal(folds[0].V, liveTotal, `V is the pre-fold LIVE view size (${liveTotal}), not the raw resend`); + assert.notEqual(folds[0].V, rawTotal, "V must NOT be the bloated raw-inbound total"); +}); + +test("#3307 P1: numerator/denominator share one view's net change (Vp === V - S)", () => { + const ctx = makeCtx(makeRaw(6), makeLiveView()); + getCacheLedger(ctx.session); + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + const folds = getCacheLedger(ctx.session).folds; + const S = folds[0].S, V = folds[0].V, Vp = folds[0].Vp; + assert.ok(S !== undefined, "S recorded"); + assert.ok(V !== undefined, "V recorded"); + assert.ok(Vp !== undefined, "Vp recorded"); + assert.ok(S > 0, "S = tokens removed from the view"); + assert.ok(V > 0, "V = pre-fold live view size"); + assert.equal(Vp, V - S, "post-fold live view === pre-fold live view - removed (same-view net change)"); + assert.ok(Vp >= 0, "post-fold live view is non-negative"); +}); + +test("#3307 P1: multi-level folding keeps tracking the live view on the second fold", () => { + const raw = makeRaw(8); + const live = makeLiveView(); + const liveTotal = totalTokens(live); + const ctx = makeCtx(raw, live); + getCacheLedger(ctx.session); + const o1 = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(o1.startsWith("[Compressed"), `first fold failed: ${o1}`); + const o2 = applyRanges(parseCompressInput(ARGS_3_4), ctx as never).text; + assert.ok(o2.startsWith("[Compressed"), `second fold failed: ${o2}`); + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 2, "two folds recorded"); + assert.equal(folds[0].V, liveTotal, "first fold V = live view"); + assert.equal(folds[1].V, liveTotal, "second fold STILL tracks the live view, not the raw resend"); +}); + +test("#3307 P1: re-folding still measures V off the already-shrunk live view", () => { + const raw = makeRaw(8); + const live0 = makeLiveView(); + const ctx = makeCtx(raw, live0); + getCacheLedger(ctx.session); + const o1 = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(o1.startsWith("[Compressed"), `first fold failed: ${o1}`); + assert.equal(getCacheLedger(ctx.session).folds[0].V, totalTokens(live0), "fold-1 V = pre-fold live view"); + // Re-folding: after fold-1 the live view has SHRUNK — the folded region is now a + // summary. V must track THIS current (smaller, summary-bearing) view, not the raw + // resend nor a stale pre-fold snapshot. This is the topology the existing multi-level + // test did not pin (it held the live view constant across folds). + const live1: CoreMessage[] = [ + { id: "sum_early", role: "user", contentType: "text", text: "[summary] ancient turns folded away" }, + { id: "sum_fold1", role: "user", contentType: "text", text: "[summary] fold-1 region collapsed" }, + { id: "live_b", role: "user", contentType: "text", text: "LIVE-" + "z".repeat(20000) }, + { id: "live_c", role: "assistant", contentType: "text", text: "LIVE-" + "w".repeat(20000) }, + ]; + assert.ok(totalTokens(live1) < totalTokens(live0), "precondition: the live view shrank after fold-1"); + ctx.messages = live1; + const o2 = applyRanges(parseCompressInput(ARGS_3_4), ctx as never).text; + assert.ok(o2.startsWith("[Compressed"), `re-fold failed: ${o2}`); + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 2, "two folds recorded"); + assert.equal(folds[1].V, totalTokens(live1), "re-fold V tracks the CURRENT (shrunken) live view, never the raw resend or a stale snapshot"); +}); + +test("#3307 P1 baseline: when messages == compressMessages (no raw/live split) V is unchanged", () => { + const raw = makeRaw(6); + const total = totalTokens(raw); + const ctx = makeCtx(raw, raw); // single-level: the live view IS the raw array + getCacheLedger(ctx.session); + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + assert.equal(getCacheLedger(ctx.session).folds[0].V, total, "V equals the (single) view size — old behavior preserved when no separate raw resend exists"); +}); diff --git a/tests/issue3307-seam-suspect-vs-client.test.ts b/tests/issue3307-seam-suspect-vs-client.test.ts new file mode 100644 index 0000000000..cce83f8531 --- /dev/null +++ b/tests/issue3307-seam-suspect-vs-client.test.ts @@ -0,0 +1,74 @@ +// #3307: the text report's two seam sections must stay disjoint. detectSeam +// pushes BOTH the #3023 client-side rewrite AND the legacy "suspected rebuild" +// fall-through into ONE shared capped ring (led.seamEvents); formatSeam's +// ⚠ CACHE SEAM section used to iterate every event in that ring without +// filtering, so a host-owned client rewrite was ALSO listed under the +// "suspected mid-history prefix breaks" header (double-listed), contaminating +// the suspect triage. The ✎ CLIENT section already filters to e.client. This +// pins that a session carrying both arms renders each seq under exactly one +// header — the client seq only under ✎, the suspect seq only under ⚠. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { noteForwardedBody, noteInboundMessages, settleUsageReport, getCacheLedger, handleAcpCache } from "../src/cache-ledger.ts"; +import type { CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `seam-disjoint-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const body = (msgs: string[]): string => JSON.stringify({ model: "m", messages: msgs.map((c) => ({ role: "user", content: c })) }); +const inbound = (msgs: string[]): CoreMessage[] => msgs.map((c) => ({ role: "user", content: c })) as unknown as CoreMessage[]; +const T0 = Date.parse("2026-10-16T08:00:00Z"); + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +test("#3307: client rewrite and genuine seam render under disjoint sections", () => { + const s = makeSession(); + + noteInboundMessages(s, inbound(["m0", "m1", "m2"])); + noteForwardedBody(s, body(["m0", "m1", "m2"])); + settle(s, T0, 100_000, 99_000); + + // Arm A — CLIENT mid-list insert at index 1 (inbound breaks mid-list). + noteInboundMessages(s, inbound(["m0", "INS", "m1", "m2"])); + noteForwardedBody(s, body(["m0", "INS", "m1", "m2"])); + settle(s, T0 + 1000, 100_000, 20_000); + const clientSeq = getCacheLedger(s).lines.at(-1)!.seq; + + // Arm B — GENUINE seam: inbound prefix-stable (pure tail append) but the + // outbound rebuild rewrote a MIDDLE message → bili-owned divergence. + noteInboundMessages(s, inbound(["m0", "INS", "m1", "m2", "m3"])); + noteForwardedBody(s, body(["m0", "INS", "CHG", "m2", "m3"])); + settle(s, T0 + 2000, 100_000, 20_000); + const suspectSeq = getCacheLedger(s).lines.at(-1)!.seq; + + const led = getCacheLedger(s); + assert.equal(led.agg.clientInserts, 1, "client arm booked"); + assert.equal(led.agg.seamSuspects, 1, "suspect arm booked"); + assert.notEqual(clientSeq, suspectSeq, "two distinct samples"); + assert.ok(led.seamEvents?.some((e) => e.client && e.seq === clientSeq), "client event carries .client"); + assert.ok(led.seamEvents?.some((e) => !e.client && e.seq === suspectSeq), "suspect event has no .client"); + + const text = handleAcpCache(s).text; + const warnStart = text.indexOf("⚠ CACHE SEAM"); + const cliStart = text.indexOf("✎ CLIENT MID-HISTORY REWRITE"); + assert.ok(warnStart >= 0, "⚠ suspected section present"); + assert.ok(cliStart > warnStart, "✎ client section present after ⚠"); + const warnSection = text.slice(warnStart, cliStart); + const clientSection = text.slice(cliStart); + + assert.ok(warnSection.includes(`#${suspectSeq}`), "suspect seq listed under ⚠"); + assert.ok(!warnSection.includes(`#${clientSeq}`), "client seq NOT echoed under ⚠ (the #3307 bug)"); + assert.ok(clientSection.includes(`#${clientSeq}`), "client seq listed under ✎"); + assert.ok(!clientSection.includes(`#${suspectSeq}`), "suspect seq NOT listed under ✎"); +}); diff --git a/tests/issue3472-mcp-structured-text.test.ts b/tests/issue3472-mcp-structured-text.test.ts new file mode 100644 index 0000000000..3199aa011c --- /dev/null +++ b/tests/issue3472-mcp-structured-text.test.ts @@ -0,0 +1,216 @@ +// #3472: since #2752 the MCP shim attaches structuredContent {outcome, +// blocksCreated} to every result carrying machine state. Some hosts (Claude +// Code via its MCP bridge) prefer structuredContent when present and DROP the +// content text — every success call reached the model as bare +// {"outcome":"success"} and the whole report (compressible ranges, GRAND +// LEDGER, search hits, restored content) was lost. The fix makes +// structuredContent self-contained: it carries the full report itself +// (structured.text = out.text, byte-identical to content[0].text) alongside +// the machine fields. Pinned here through the REAL shim over stdio JSON-RPC: +// 1. success envelopes (acp_status / acp_cache / search_context / decompress): +// structuredContent.text === content[0].text byte-for-byte, outcome kept; +// 2. compress applied/partial: the #2752 machine fields are unchanged, the +// text is present and byte-stable (partial incl. the marker line); +// 3. legacy envelopes (no outcome/blocksCreated): NO structuredContent at +// all — pre-#2752 clients see exactly the old shape; +// 4. isError semantics unchanged (failure/refused stay errors, partial stays +// non-error) and content[0].text stays byte-stable everywhere, so +// content-reading hosts are untouched. +import { afterEach, beforeEach, describe, it } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { spawn } from "node:child_process"; + +import { partialOutcomeNote } from "../src/proxy-tool-result.ts"; + +const STATUS_REPORT = [ + "ACTIVE SURFACE:", + "Breakdown: 10.0K system (30%) | 23.3K tool (70%)", + "Compressible ranges (1, oldest first):", + "- m00001–m00010 1.2K effective tool", +].join("\n"); +const CACHE_REPORT = "GRAND LEDGER:\ntotal input 1,000,000 · cached 960,000 (96.0%)\nFOLD ECONOMICS:\nb1 net saved 12,000 — paid back"; +const SEARCH_REPORT = 'Found 2 block(s) for "auth":\n\n[b2] "Auth System Exploration" — m00010–m00050'; +const DECOMPRESSED_BODY = "[decompress b1 m00001–m00010: restored 10 item(s) — full content follows]\n"; +const APPLIED_TEXT = "[Compressed m00001–m00100 → 2 block(s), ~5000 tokens saved.]"; +const PARTIAL_TEXT = "[Compressed m00001–m00200 → 1 block(s), ~4000 tokens saved. Errors: m00101–m00200: unknown ref]"; +const REFUSED_TEXT = "[Compression FAILED: requested range(s) resolved to no messages]"; + +type Envelope = Record; + +async function startCannedProxy(): Promise<{ origin: string; canned: Map; close(): Promise }> { + const canned = new Map(); + const server = http.createServer((req, res) => { + if (req.url !== "/__bili/plugin/tool") { res.writeHead(404).end("nope"); return; } + let raw = ""; + req.on("data", (d) => { raw += d; }); + req.on("end", () => { + const parsed = JSON.parse(raw) as { tool?: string }; + const payload = canned.get(parsed.tool ?? "") ?? { ok: false, error: "unexpected tool" }; + res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(payload)); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const addr = server.address(); + const port = typeof addr === "object" && addr ? addr.port : 0; + let closed = false; + return { + origin: `http://127.0.0.1:${port}`, + canned, + close: () => closed ? Promise.resolve() : (closed = true, new Promise((r) => server.close(() => r()))), + }; +} + +type WireResult = { content?: Array<{ type?: string; text?: string }>; isError?: boolean; structuredContent?: Record }; + +class WireShimClient { + private child: ReturnType; + private stderr = ""; + private pending = new Map void>(); + private buf = ""; + + constructor(env: NodeJS.ProcessEnv) { + this.child = spawn(process.execPath, ["--import", "tsx", "src/mcp.ts"], { + cwd: process.cwd(), + env, + stdio: ["pipe", "pipe", "pipe"], + }); + this.child.stdout?.setEncoding("utf8"); + this.child.stderr?.setEncoding("utf8"); + this.child.stdout?.on("data", (d: string) => { + this.buf += d; + let nl: number; + while ((nl = this.buf.indexOf("\n")) >= 0) { + const line = this.buf.slice(0, nl).trim(); + this.buf = this.buf.slice(nl + 1); + if (!line) continue; + try { + const msg = JSON.parse(line) as { id?: unknown }; + if (typeof msg.id === "number") { + const waiter = this.pending.get(msg.id); + if (waiter) { + this.pending.delete(msg.id); + waiter(msg); + } + } + } catch { /* non-JSON line — ignore */ } + } + }); + this.child.stderr?.on("data", (d: string) => { this.stderr += d; }); + } + + request(id: number, method: string, params?: Record): Promise<{ result?: WireResult; error?: unknown }> { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(id); + reject(new Error(`shim ${method} (id ${id}) timed out; stderr: ${this.stderr.slice(0, 400)}`)); + }, 20_000); + this.pending.set(id, (msg) => { + clearTimeout(timer); + resolve(msg as { result?: WireResult; error?: unknown }); + }); + this.child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, ...(params ? { params } : {}) })}\n`); + }); + } + + async init(): Promise { + const r = await this.request(0, "initialize", { + protocolVersion: "2025-06-18", + capabilities: {}, + clientInfo: { name: "bili-wire-test", version: "0.1.0" }, + }); + assert.ok(r.result && !r.error, `initialize must succeed; stderr: ${this.stderr.slice(0, 400)}`); + this.child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`); + } + + close(): void { this.child.kill(); } +} + +describe("#3472: structuredContent carries the full report for structured-preferring hosts", () => { + let proxy: Awaited> | null = null; + let client: WireShimClient | null = null; + + beforeEach(async () => { + proxy = await startCannedProxy(); + proxy.canned.set("acp_status", { ok: true, result: STATUS_REPORT, outcome: "success" }); + proxy.canned.set("acp_cache", { ok: true, result: CACHE_REPORT, outcome: "success" }); + proxy.canned.set("search_context", { ok: true, result: SEARCH_REPORT, outcome: "success" }); + proxy.canned.set("decompress", { ok: true, result: DECOMPRESSED_BODY, outcome: "success" }); + proxy.canned.set("compress", { ok: true, result: APPLIED_TEXT, outcome: "applied", blocksCreated: 2 }); + client = new WireShimClient({ ...process.env, BILI_MCP_PROXY: proxy.origin, BILI_CONVERSATION_ID: "conv-wire-3472" }); + await client.init(); + }); + afterEach(() => { + client?.close(); + client = null; + if (proxy) void proxy.close(); + proxy = null; + }); + + const call = async (name: string, args: Record = {}): Promise => { + assert.ok(client); + const r = await client.request(name.length + 1, "tools/call", { name, arguments: args }); + assert.ok(!r.error, `no JSON-RPC error: ${JSON.stringify(r.error)}`); + assert.ok(r.result, "a result object comes back"); + return r.result!; + }; + + it("acp_status success: the complete report is recoverable from structuredContent alone", async () => { + const r = await call("acp_status"); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, STATUS_REPORT); + assert.deepEqual(r.structuredContent, { text: STATUS_REPORT, outcome: "success" }, + "a host that renders only structuredContent must still see the full status report"); + }); + + it("acp_cache / search_context / decompress success: full body inside structuredContent, outcome kept", async () => { + const cases: Array<[string, Record, string]> = [ + ["acp_cache", {}, CACHE_REPORT], + ["search_context", { query: "auth" }, SEARCH_REPORT], + ["decompress", { blockId: "b1" }, DECOMPRESSED_BODY], + ]; + for (const [name, args, body] of cases) { + const r = await call(name, args); + assert.notEqual(r.isError, true, name); + assert.equal(r.content?.[0]?.text, body, `${name}: content text byte-stable`); + assert.deepEqual(r.structuredContent, { text: body, outcome: "success" }, `${name}: structured carries the full body`); + } + }); + + it("compress applied: the #2752 machine fields stay intact, text rides alongside", async () => { + const r = await call("compress", { content: [] }); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, APPLIED_TEXT); + assert.deepEqual(r.structuredContent, { text: APPLIED_TEXT, outcome: "applied", blocksCreated: 2 }); + }); + + it("compress partial: marker line present in BOTH carriers, machine state unchanged", async () => { + assert.ok(proxy); + proxy.canned.set("compress", { ok: true, result: PARTIAL_TEXT, outcome: "partial", blocksCreated: 1 }); + const r = await call("compress", { content: [] }); + assert.notEqual(r.isError, true, "partial stays non-error (work landed)"); + const expected = `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`; + assert.equal(r.content?.[0]?.text, expected); + assert.deepEqual(r.structuredContent, { text: expected, outcome: "partial", blocksCreated: 1 }); + }); + + it("refused: isError true, receipt text in both carriers", async () => { + assert.ok(proxy); + proxy.canned.set("compress", { ok: true, result: REFUSED_TEXT, outcome: "refused", blocksCreated: 0 }); + const r = await call("compress", { content: [] }); + assert.equal(r.isError, true); + assert.equal(r.content?.[0]?.text, REFUSED_TEXT); + assert.deepEqual(r.structuredContent, { text: REFUSED_TEXT, outcome: "refused", blocksCreated: 0 }); + }); + + it("legacy envelope (no outcome/blocksCreated): NO structuredContent — old clients see the old shape", async () => { + assert.ok(proxy); + proxy.canned.set("acp_status", { ok: true, result: "CONTEXT STATUS" }); + const r = await call("acp_status"); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, "CONTEXT STATUS"); + assert.equal(r.structuredContent, undefined, "pre-#2752 envelopes keep their exact wire shape"); + }); +}); diff --git a/tests/issue3536-favicon-local.test.ts b/tests/issue3536-favicon-local.test.ts new file mode 100644 index 0000000000..fae7e08bb9 --- /dev/null +++ b/tests/issue3536-favicon-local.test.ts @@ -0,0 +1,158 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdirSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path 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 { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { renderPage } from "../src/web/page.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// #3536: the dsh desktop settings panel frames /__bili/ and the browser fires +// GET /favicon.ico at the ORIGIN ROOT — which matched no admin route and fell +// through the relay onto the configured upstream (a wasted request + 403 noise +// in the log, upstream host visible in the panel's network tab). Two halves: +// the panel page declares an empty icon so well-behaved browsers never probe, +// and the proxy answers the probe locally (204) as a backstop — while every +// OTHER unknown path keeps the documented relay fallthrough. + +async function waitReady(port: number): Promise { + for (let i = 0; i < 200; i++) { + try { + if ((await fetch(`http://127.0.0.1:${port}/__bili/health`, { signal: AbortSignal.timeout(500) })).status === 200) return; + } catch {} + await new Promise((r) => setTimeout(r, 50)); + } + throw new Error(`proxy on port ${port} did not become ready`); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function get(port: number, reqPath: string): Promise<{ status: number; body: string }> { + const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); + const req = http.request({ host: "127.0.0.1", port, path: reqPath, signal: AbortSignal.timeout(5000) }, (res) => { + let body = ""; + res.on("data", (c: Buffer) => { body += c.toString("utf8"); }); + res.on("end", () => resolve({ status: res.statusCode ?? 0, body })); + }); + req.once("error", reject); + req.end(); + return promise; +} + +/** Mock upstream shaped like the incident's Anthropic 403; records every hit. */ +function startMockUpstream(): Promise<{ origin: string; hits: string[]; close: () => Promise }> { + const hits: string[] = []; + const server = http.createServer((req, res) => { + hits.push(`${req.method} ${req.url}`); + res.writeHead(403, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { type: "forbidden", message: "Request not allowed" } })); + }); + return new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const port = (server.address() as { port: number }).port; + resolve({ + origin: `http://127.0.0.1:${port}`, + hits, + close: async () => { + server.closeAllConnections?.(); + await close(server); + }, + }); + }); + }); +} + +async function makeBili(upstream: string): Promise { + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream, + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + return proxy; +} + +test("#3536: the panel page declares an empty icon so browsers never probe /favicon.ico", () => { + for (const embed of [false, true]) { + const html = renderPage("http://127.0.0.1:8787", "0.0.0-test", embed); + assert.ok( + html.includes(''), + `embed=${embed}: the head must declare the empty icon before `, + ); + assert.ok(html.indexOf('<link rel="icon"') < html.indexOf("<title>"), "icon link sits inside <head>"); + } +}); + +test("#3536: GET /favicon.ico on the proxy origin answers 204 locally and never reaches the upstream; other unknown paths keep the relay fallthrough", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-issue3536-${process.pid}-${Date.now()}`); + mkdirSync(root, { recursive: true }); + const biliConfig = path.join(root, "billion-context.json"); + writeFileSync(biliConfig, '{"providers":{}}\n', "utf8"); + const prevConfig = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = biliConfig; + + const up = await startMockUpstream(); + const bili = await makeBili(up.origin); + const port = (bili.address() as { port: number }).port; + try { + await waitReady(port); + + const fav = await get(port, "/favicon.ico"); + assert.equal(fav.status, 204, `favicon probe must be answered locally (got ${fav.status}: ${fav.body.slice(0, 200)})`); + assert.equal(fav.body, ""); + const favQuery = await get(port, "/favicon.ico?v=1"); + assert.equal(favQuery.status, 204, "the query form is the same asset"); + assert.deepEqual(up.hits, [], "no favicon probe may reach the configured upstream"); + + // Both halves of the fix over real HTTP: the served panel page carries + // the declaration that stops well-behaved browsers from probing at all. + const page = await get(port, "/__bili/?embed=1"); + assert.equal(page.status, 200); + assert.ok(page.body.includes('<link rel="icon" href="data:,">'), "served panel HTML declares the empty icon"); + + // Regression guard: ONLY the favicon path is pinned local — every other + // unknown path still falls through the relay onto the configured + // upstream (the documented zero-config tunnel behavior). + const other = await get(port, "/definitely/not/an/admin/path"); + assert.equal(other.status, 403, `unknown non-favicon paths keep the relay fallthrough (got ${other.status}: ${other.body.slice(0, 200)})`); + assert.deepEqual(up.hits, ["GET /definitely/not/an/admin/path"], "exactly the non-favicon request was relayed"); + } finally { + process.env.BILI_CONFIG_FILE = prevConfig; + bili.closeAllConnections?.(); + await close(bili); + await up.close(); + try { rmrf(root); } catch { /* best-effort */ } + } +}); diff --git a/tests/issue3890-parent-gone-confirm-drain.test.ts b/tests/issue3890-parent-gone-confirm-drain.test.ts new file mode 100644 index 0000000000..04d8da9dee --- /dev/null +++ b/tests/issue3890-parent-gone-confirm-drain.test.ts @@ -0,0 +1,146 @@ +// #3890: Windows parent-gone false positive — a LIVE parent pid (opencode-cli.exe) +// was judged dead by the watchdog and the proxy force-exited 12× in 2.5h, each +// time cutting in-flight upstream streams. Two mechanisms are pinned here: +// (1) a kill-0 death verdict now requires WATCHER_DEAD_CONFIRM_TICKS +// CONSECUTIVE failed probes (tickWatchedPid — pure, unit-tested below); +// one transient OpenProcess failure can no longer kill the proxy, while +// heartbeat expiry for a live pid stays immediate (#3544 unchanged); +// (2) the shutdown backstop RE-ARMS its grace window while connections are +// still alive (bounded passes) instead of force-exiting at 10s and +// yanking in-flight streams (live child-process test — pre-fix behavior +// exits mid-drain). + +import test from "node:test"; +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { mkdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import net from "node:net"; +import path from "node:path"; +import { once } from "node:events"; +import { fileURLToPath } from "node:url"; +import { tickWatchedPid } from "../src/server.ts"; +import type { PidProbe } from "../src/instance.ts"; +import { rmrf } from "./tmp-rm.ts"; + +const OPTS = { heartbeatGraceMs: 15_000, confirmTicks: 3 }; +const NOW = 1_000_000; +const dead = (code = "ESRCH"): PidProbe => ({ alive: false, errCode: code }); +const aliveProbe: PidProbe = { alive: true }; + +test("#3890 tickWatchedPid: one failed probe does NOT remove the pid", () => { + const d = tickWatchedPid(dead(), undefined, NOW, undefined, OPTS); + assert.deepEqual(d, { removed: false, staleHeartbeat: false, streak: 1 }, "a single flaky probe is not conclusive"); +}); + +test("#3890 tickWatchedPid: blip shorter than confirmTicks survives; a live probe resets the streak", () => { + let d = tickWatchedPid(dead(), undefined, NOW, undefined, OPTS); + assert.equal(d.removed, false); + d = tickWatchedPid(dead(), undefined, NOW + 2_000, d.streak, OPTS); + assert.equal(d.removed, false); + assert.equal(d.streak, 2); + d = tickWatchedPid(aliveProbe, undefined, NOW + 4_000, d.streak, OPTS); + assert.equal(d.removed, false); + assert.equal(d.streak, 0, "the live probe clears the pending death"); + d = tickWatchedPid(dead(), undefined, NOW + 6_000, d.streak, OPTS); + assert.equal(d.streak, 1, "a later episode starts from zero again"); +}); + +test("#3890 tickWatchedPid: confirmTicks consecutive failures remove the pid", () => { + let d = tickWatchedPid(dead(), undefined, NOW, undefined, OPTS); + d = tickWatchedPid(dead(), undefined, NOW + 2_000, d.streak, OPTS); + assert.equal(d.removed, false, "two failures are not yet conclusive"); + d = tickWatchedPid(dead(), undefined, NOW + 4_000, d.streak, OPTS); + assert.equal(d.removed, true, "the third consecutive failure confirms death"); + assert.equal(d.staleHeartbeat, false); +}); + +test("#3890 tickWatchedPid: live pids keep legacy semantics (no heartbeat record ⇒ never removed)", () => { + assert.deepEqual(tickWatchedPid(aliveProbe, undefined, NOW, undefined, OPTS), { removed: false, staleHeartbeat: false, streak: 0 }); +}); + +test("#3890 tickWatchedPid: heartbeat expiry stays immediate for a LIVE pid (#3544 unchanged)", () => { + assert.deepEqual(tickWatchedPid(aliveProbe, NOW - 1_000, NOW, undefined, OPTS), { removed: false, staleHeartbeat: false, streak: 0 }, "fresh beat keeps it"); + const stale = tickWatchedPid(aliveProbe, NOW - 16_000, NOW, undefined, OPTS); + assert.equal(stale.removed, true, "missed beats beyond grace mark the slot dead immediately — no confirmation needed for a time-based liveness proof"); + assert.equal(stale.staleHeartbeat, true); +}); + +test("#3890 tickWatchedPid: a DEAD pid never takes the heartbeat fast path", () => { + let d = tickWatchedPid(dead("ESRCH"), NOW - 60_000, NOW, undefined, OPTS); + assert.equal(d.removed, false, "stale lastSeen does not shortcut kill-0 confirmation"); + d = tickWatchedPid(dead("ESRCH"), NOW - 60_000, NOW, d.streak, OPTS); + assert.equal(d.removed, false); + d = tickWatchedPid(dead("ESRCH"), NOW - 60_000, NOW, d.streak, OPTS); + assert.equal(d.removed, true); + assert.equal(d.staleHeartbeat, false, "removal via kill-0 confirmation is never tagged stale-heartbeat"); +}); + +// ── live drain backstop (child process — finishShutdown ends in process.exit) ── + +const FIXTURE = fileURLToPath(new URL("./fixtures/issue3890-drain-bili.mts", import.meta.url)); +const READY_TIMEOUT_MS = 30_000; +// The pre-fix hard backstop force-exited 10s after shutdown() regardless of +// live connections. The fixed proxy must still be ALIVE well past that point +// while a connection is held, and exit promptly once it drains. +const HOLD_CHECK_MS = 13_000; +const EXIT_GRACE_MS = 20_000; + +test("#3890 shutdown backstop extends while a connection is alive, then exits when it drains", { timeout: 120_000 }, async () => { + const root = path.join(tmpdir(), `bili-3890drain-${process.pid}-${Date.now()}`); + const env = { ...process.env }; + env.XDG_CONFIG_HOME = path.join(root, "cfg"); + env.XDG_STATE_HOME = path.join(root, "state"); + env.XDG_CACHE_HOME = path.join(root, "cache"); + for (const d of [env.XDG_CONFIG_HOME, env.XDG_STATE_HOME, env.XDG_CACHE_HOME]) mkdirSync(d, { recursive: true }); + delete env.BILI_PARENT_PID; + delete env.BILLION_CONTEXT_PROXY; + const child = spawn(process.execPath, ["--import", "tsx", FIXTURE], { env, stdio: ["ignore", "pipe", "pipe"] }); + let stdout = ""; + let stderr = ""; + child.stdout!.on("data", (c) => { stdout += String(c); }); + child.stderr!.on("data", (c) => { stderr += String(c); }); + let exitInfo: { code: number | null } | null = null; + child.on("exit", (code) => { exitInfo = { code }; }); + let held: net.Socket | null = null; + try { + const deadline = Date.now() + READY_TIMEOUT_MS; + let port = 0; + while (!port && Date.now() < deadline) { + const m = /^READY (\d+)$/m.exec(stdout); + if (m) port = Number(m[1]); + else await new Promise((r) => setTimeout(r, 100)); + } + assert.ok(port > 0, `fixture reported READY (stderr tail: ${stderr.slice(-500)})`); + // A bare held socket: registered in connRecords, never requests, never + // FINs until destroyed — deterministic liveConns≥1 without depending on + // keep-alive reaper timing. + held = new net.Socket(); + held.connect(port, "127.0.0.1"); + await once(held, "connect"); + const res = await fetch(`http://127.0.0.1:${port}/__bili/shutdown`, { method: "POST" }); + assert.equal(res.status, 202, "admin shutdown accepted"); + // Pre-fix: force-exit at ~10s with liveConns=1. Post-fix: the window + // re-arms and the proxy stays up serving/draining. + const checkDeadline = Date.now() + HOLD_CHECK_MS; + while (Date.now() < checkDeadline) { + if (exitInfo !== null) break; + await new Promise((r) => setTimeout(r, 250)); + } + assert.equal(exitInfo, null, `proxy must stay alive ${HOLD_CHECK_MS}ms into the shutdown drain while a connection is open — pre-#3890 behavior force-exited at 10s (stderr tail: ${stderr.slice(-500)})`); + assert.match(stderr, /extending drain \d+\/\d+/, "the extension is logged, not an accidental slowdown"); + held.destroy(); + held = null; + const exitDeadline = Date.now() + EXIT_GRACE_MS; + while (exitInfo === null && Date.now() < exitDeadline) await new Promise((r) => setTimeout(r, 250)); + if (exitInfo === null) throw new Error(`proxy exits once the last connection drains — still up after ${EXIT_GRACE_MS}ms (stderr tail: ${stderr.slice(-500)})`); + assert.equal(exitInfo.code, 0, "clean exit code after drain"); + } finally { + held?.destroy(); + if (exitInfo === null && child.pid) { + child.kill("SIGKILL"); + await once(child, "exit").catch(() => {}); + } + try { rmrf(root); } catch {} + } +}); diff --git a/tests/named-provider-bind.test.ts b/tests/named-provider-bind.test.ts index 7bb7fe32aa..7a34163344 100644 --- a/tests/named-provider-bind.test.ts +++ b/tests/named-provider-bind.test.ts @@ -241,6 +241,81 @@ test("web config API round-trips named entries verbatim (no silent drop)", async assert.deepEqual(loadRoutes()["https://api.anthropic.com"]?.compress, { maxContextLimitPct: 0.75 }); }); +test("compress-block keys at a URL route root warn instead of sitting silently (#3675)", (t) => { + const warnings = captureWarnings(t); + withConfigFile(t, { + "https://cap.example": { + modelContextLimit: 1000000, + outputHeadroomMaxPct: 0.15, + models: { m1: { context: 200000 } }, + }, + }); + const routes = loadRoutes(); + assert.deepEqual(Object.keys(routes), ["https://cap.example"]); + assert.equal(routes["https://cap.example"].models?.m1?.context, 200000); + assert.equal(routes["https://cap.example"].compress, undefined); + assert.equal(warnings.length, 1); + for (const needle of ['"https://cap.example"', "modelContextLimit", "outputHeadroomMaxPct", '.compress']) { + assert.ok(warnings[0].includes(needle), `warning should mention ${needle}: ${warnings[0]}`); + } +}); + +test("bound alias: compress-block keys at its root stay inert through the fold and warn (#3675)", (t) => { + const warnings = captureWarnings(t); + withConfigFile(t, { + capBridge: { + bind: "https://cap-bound.example", + modelContextLimit: 500000, + nudgeGrowthTokens: 77777, + compress: { maxContextLimitPct: 0.7 }, + }, + }); + const laneRaw = loadRoutes()["https://cap-bound.example"] as Record<string, unknown> | undefined; + assert.ok(laneRaw); + assert.equal((laneRaw.compress as Record<string, unknown> | undefined)?.maxContextLimitPct, 0.7); + assert.equal(laneRaw.modelContextLimit, undefined); + assert.equal(laneRaw.nudgeGrowthTokens, undefined); + assert.equal(warnings.length, 1); + for (const needle of ['"capBridge"', "modelContextLimit", "nudgeGrowthTokens"]) { + assert.ok(warnings[0].includes(needle), `warning should mention ${needle}: ${warnings[0]}`); + } +}); + +test("named entry whose only fields are compress-block keys: previously zero warnings, now one (#3675)", (t) => { + const warnings = captureWarnings(t); + withConfigFile(t, { + capLonely: { outputHeadroomMaxPct: 0.2 }, + }); + const routes = loadRoutes(); + assert.ok("capLonely" in routes); + assert.equal(warnings.length, 1); + assert.ok(warnings[0].includes('"capLonely"') && warnings[0].includes("outputHeadroomMaxPct"), warnings[0]); +}); + +test("dialing recipes keep their own loud rejection — no duplicate route-root warning (#3675)", (t) => { + const warnings = captureWarnings(t); + withConfigFile(t, { + glmLane: { + baseUrl: "https://glm.example", + api: "openai", + apiKeyEnv: "GLM_KEY", + models: { g1: { contextWindow: 100000 } }, + modelContextLimit: 1000000, + }, + }); + loadRoutes(); + assert.deepEqual(warnings, []); +}); + +test("route-root compress-key warning fires once per signature across reloads (#3675)", (t) => { + const warnings = captureWarnings(t); + withConfigFile(t, { "https://cap-reload.example": { modelContextLimit: 250000 } }); + loadRoutes(); + loadRoutes(); + loadRoutes(); + assert.equal(warnings.filter((w) => w.includes('"https://cap-reload.example"')).length, 1); +}); + test("web config API still rejects legacy, invalid-proxy, and non-object entries", async (t) => { captureWarnings(t); withConfigFile(t, {}); diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index 677fb4235d..532cd5a055 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -355,6 +355,16 @@ entries: desc_zh: "w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。" site_en: "<code>priceProfile</code> — <span class='mech'>w / r / q</span> ratios normalized to the input price (write / cache-read / output); unset ⇒ models.dev $/Mtok registry rows, kernel fallback {1, 0.1, 4}. Report-only — never affects triggers or the wire." site_zh: "<code>priceProfile</code> —— <span class='mech'>w / r / q</span> 相对输入价的比率(写入/缓存读/输出);未设置 ⇒ 取 models.dev $/Mtok 注册表行,内核兜底 {1, 0.1, 4}。仅供报告,不影响触发条件或线上行为。" + - key: compress.externalSummary + type: object + default: "opt-in (disabled)" + group: compress + id: co.es + render: obj-item + desc_en: "Sends compression summaries to separately configured models referenced from the providers table; whole-chain replace semantics at all three compress levels." + desc_zh: "把压缩摘要交给 providers 表引用的独立配置模型;三级 compress 均为整链替换语义。" + site_en: "<code>externalSummary</code> — optional chain of external summary models, each target a \"provider/model\" reference into the <code>providers</code> table; disabled unless <code>enabled</code> is true, targets tried in order with fall-through (<span class='mech'>enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens</span>). Whole-chain replace semantics at all three compress levels." + site_zh: "<code>externalSummary</code> —— 可选的外部摘要模型链,每个目标为指向 <code>providers</code> 表的 \"provider/model\" 引用;<code>enabled</code> 为 true 才启用,目标按顺序尝试、失败落到下一个(<span class='mech'>enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens</span>)。三级 compress 均为整链替换语义。" - key: compress.acknowledgePromptsRisk type: boolean default: "false"