diff --git a/scripts/bench-plan-aware-search.ts b/scripts/bench-plan-aware-search.ts new file mode 100644 index 0000000000..dcb0d86b27 --- /dev/null +++ b/scripts/bench-plan-aware-search.ts @@ -0,0 +1,77 @@ +// Run with --import ./tests/preload-test-env.mjs --import tsx. +// Optional argument: module URL relative to this script (for a saved base). +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { performance } from "node:perf_hooks"; +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { applyCompressSettings } from "../src/compress-settings.ts"; +import { createSession } from "../src/session.ts"; +import { applyRanges } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; + +const { executeSearchContext, extractPlanState } = await import( + new URL(process.argv[2] ?? "../src/decompress-shared.ts", import.meta.url).href +) as typeof import("../src/decompress-shared.ts"); +const core = createCore(); +const config = applyCompressSettings(defaultConfig(200_000), 200_000, { + absorb: { enabled: true, minToolTokens: 50 }, + ccr: { enabled: true, minToolTokens: 50 }, +}) as Config; +const session = createSession("plan-normalization-benchmark"); +const messages: CoreMessage[] = Array.from({ length: 40 }, (_, i) => ({ + id: `history_${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", + text: `Historical detail ${i}. ${"retained context evidence. ".repeat(80)}`, + timestamp: 1_700_000_000_000 + i, +})); +const turn = core.processTurn({ messages, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); +session.state = turn.state; +const topics = ["auth", "storage", "validation", "refresh", "ratelimit 缓存路由会话", "metrics"]; +applyRanges(parseCompressInput({ content: topics.map((topic, i) => ({ + startId: `m${String(i * 5 + 1).padStart(5, "0")}`, + endId: `m${String(i * 5 + 5).padStart(5, "0")}`, + topic: `token ${topic}`, summary: `Historical token ${topic} implementation and verification.`, +})) }), { core, config, messages: turn.messages, session, log: () => {} }); +assert.equal(session.state.blocks.filter(b => b.active).length, 6); + +function measure(work: () => string) { + const expected = work(); + for (let i = 0; i < 3; i++) assert.equal(work(), expected); + const elapsed: number[] = []; + for (let i = 0; i < 15; i++) { + const start = performance.now(); + const actual = work(); + elapsed.push(performance.now() - start); + assert.equal(actual, expected); + } + elapsed.sort((a, b) => a - b); + return { + medianMs: elapsed[7], minMs: elapsed[0], maxMs: elapsed[14], + digest: createHash("sha256").update(expected).digest("hex"), + }; +} + +const languages = { + latin: "Implement Ratelimit Middleware And Preserve Session Identity With Tests. ", + cjk: "实现缓存路由并保留会话身份验证和回归测试。", + mixed: "实现Cache路由API和Session身份Verification测试。", +}; +for (const [language, sentence] of Object.entries(languages)) { + for (const characters of [1000, 2000, 4000]) { + const text = sentence.repeat(Math.ceil(characters / sentence.length)).slice(0, characters); + const plan: CoreMessage[] = [ + { id: "user", role: "user", contentType: "text", text }, + { id: "plan", role: "assistant", contentType: "tool-call", toolName: "TodoWrite", + text: JSON.stringify({ todos: [{ content: text, status: "in_progress", activeForm: "Implementing the plan" }] }) }, + ]; + const opts = { messages: plan, session, log: () => {}, protectedPatterns: config.protectedLatestTools }; + const search = () => executeSearchContext({ query: "token", limit: 5 }, core, session.state, undefined, opts).text; + assert.match(search(), /\[plan-aware\]/); + console.log(JSON.stringify({ language, characters, sources: plan.length, workload: "extract", ...measure( + () => JSON.stringify([...extractPlanState(plan)!.terms]), + ) })); + console.log(JSON.stringify({ language, characters, sources: plan.length, workload: "search", ...measure(search) })); + } +} +console.log(JSON.stringify({ workload: "plain-search", ...measure( + () => executeSearchContext({ query: "token", limit: 5 }, core, session.state).text, +) })); diff --git a/scripts/bench-registry-suffix-index.ts b/scripts/bench-registry-suffix-index.ts new file mode 100644 index 0000000000..f78b6acd64 --- /dev/null +++ b/scripts/bench-registry-suffix-index.ts @@ -0,0 +1,45 @@ +import { createHash } from "node:crypto"; +import { performance } from "node:perf_hooks"; +import { _resetForTest, _setForTest, peekRegistryContext, peekRegistryOutputLimit, scanRegistryCostRow, type CostsShape } from "../src/registry.js"; +import snapshot from "../src/registry-snapshot.json" with { type: "json" }; + +// Run from the repository root with: +// node --expose-gc --import ./tests/preload-test-env.mjs --import tsx scripts/bench-registry-suffix-index.ts +// Use the bundled real roster; no network request or configuration mutation. +const costs = snapshot.costs as CostsShape; +const bareNames = [...new Set(Object.keys(costs).map((key) => key.slice(key.lastIndexOf("/") + 1)))]; +const probes = bareNames.slice(0, 500); +const iterations = 5000; +function measure(label: string, names: string[]): void { + _resetForTest(); + _setForTest(snapshot.models, costs); + global.gc?.(); + const heapBefore = process.memoryUsage().heapUsed; + const coldStart = performance.now(); + scanRegistryCostRow(costs, names[0], "relay.example"); + peekRegistryContext(names[0], "relay.example"); + peekRegistryOutputLimit(names[0], "relay.example"); + const coldMs = performance.now() - coldStart; + global.gc?.(); + const retainedBytes = process.memoryUsage().heapUsed - heapBefore; + // Warm conflict diagnostics before measurement; output is hashed below. + for (let i = 0; i < 5000; i++) { + const name = names[i % names.length]; + scanRegistryCostRow(costs, name, "relay.example"); + peekRegistryContext(name, "relay.example"); + peekRegistryOutputLimit(name, "relay.example"); + } + global.gc?.(); + const outputs: unknown[] = []; + const cpuStart = process.cpuUsage(); + const wallStart = performance.now(); + for (let i = 0; i < iterations; i++) { + const name = names[i % names.length]; + outputs.push([scanRegistryCostRow(costs, name, "relay.example"), peekRegistryContext(name, "relay.example"), peekRegistryOutputLimit(name, "relay.example")]); + } + const wallMs = performance.now() - wallStart; + const cpu = process.cpuUsage(cpuStart); + console.log(JSON.stringify({label, rows: Object.keys(costs).length, models: Object.keys(snapshot.models).length, names: names.length, iterations, coldMs, retainedBytes, cpuMs: (cpu.user + cpu.system) / 1000, wallMs, digest: createHash("sha256").update(JSON.stringify(outputs)).digest("hex")})); +} +measure("real-roster-relay-models", probes); +measure("unlisted-relay-models", Array.from({length: 100}, (_, i) => `unlisted-benchmark-model-${i}`)); diff --git a/src/affinity-neutral.ts b/src/affinity-neutral.ts index 11d79f9b5f..ac89d0ceb7 100644 --- a/src/affinity-neutral.ts +++ b/src/affinity-neutral.ts @@ -138,7 +138,7 @@ function sortKeys(value: unknown): unknown { if (Array.isArray(value)) return value.map(sortKeys); if (value !== null && typeof value === "object") { const record = value as Record; - const out: Record = {}; + const out: Record = Object.create(null); for (const key of Object.keys(record).sort()) out[key] = sortKeys(record[key]); return out; } diff --git a/src/claude-mcp-entry.ts b/src/claude-mcp-entry.ts index 7472e02e84..b97fbdb103 100644 --- a/src/claude-mcp-entry.ts +++ b/src/claude-mcp-entry.ts @@ -106,7 +106,9 @@ export function _setClaudeHostPidForTest(pid: number | undefined): void { async function probeProxyHealth(origin: string): Promise { try { const res = await fetch(healthUrl(origin), { signal: AbortSignal.timeout(2000) }); - return res.ok; + if (!res.ok) return false; + const payload = await res.json() as { ok?: unknown } | null; + return payload?.ok === true; } catch { return false; } diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index ae17b50e0a..c02ab4eb96 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -556,10 +556,10 @@ type PlanTerms = Map; function planTokenize(text: string): string[] { const out: string[] = []; // Rendered ref-tag markup () is proxy metadata, not plan content. - const clean = text.replace(/\x3cacp\b[\s\S]*?\x3c\/acp\x3e/g, " "); + const clean = text.replace(/\x3cacp\b[\s\S]*?\x3c\/acp\x3e/g, " ").toLowerCase(); const re = /[a-z0-9]+|[\u4e00-\u9fff]/g; let m: RegExpExecArray | null; - while ((m = re.exec(clean.toLowerCase())) !== null) { + while ((m = re.exec(clean)) !== null) { const t = m[0]; const cjk = /[\u4e00-\u9fff]/.test(t); // Latin tokens need >= 3 chars to carry signal; CJK single chars count. diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index 47387df31f..305c37ec28 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -216,6 +216,8 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom classificationReservation.release(); classificationReservation = undefined; r.clearTimer(); + // A discarded body has no consumer; do not wait for a tee sibling to cancel. + void r.response.body?.cancel().catch(() => undefined); } opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry rejected (HTTP ${r.response.status}); passing the empty turn through`); return null; diff --git a/src/egress-shed.ts b/src/egress-shed.ts index 05bf852bec..a265696e7a 100644 --- a/src/egress-shed.ts +++ b/src/egress-shed.ts @@ -61,6 +61,7 @@ interface Occurrence { entryIndex: number; b64: string; mediaType: string; + exempt: boolean; protectedTail: boolean; /** Set once rung A replaced this occurrence — rung B must never shrink a detached part. */ archived: boolean; @@ -140,17 +141,17 @@ function isExemptOutput(protocol: WireProtocol, item: Part, names: Map { + const addAnthropicImages = (parts: unknown[], entryIndex: number, toolOutput: boolean, exemptOutput = false): void => { parts.forEach((part, pos) => { if (!isObj(part) || part.type !== "image") return; const src = part.source; if (!isObj(src) || src.type !== "base64" || typeof src.data !== "string" || src.data.length === 0) return; - if (toolOutput && matchesOriginalImageReceipt(src.data, parts)) return; const mediaType = typeof src.media_type === "string" ? src.media_type : "image/png"; occ.push({ entryIndex, b64: src.data, mediaType, + exempt: exemptOutput || (toolOutput && matchesOriginalImageReceipt(src.data, parts)), protectedTail: false, archived: false, applyArchive: (note) => { parts[pos] = { type: "text", text: note }; }, @@ -169,12 +170,12 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { const inline = part.inlineData; if (!isObj(inline) || typeof inline.data !== "string" || inline.data.length === 0) return; if (typeof inline.mimeType !== "string" || !/^image\//i.test(inline.mimeType)) return; - if (matchesOriginalImageReceipt(inline.data, parts)) return; const mediaType = inline.mimeType; occ.push({ entryIndex, b64: inline.data, mediaType, + exempt: matchesOriginalImageReceipt(inline.data, parts), protectedTail: false, archived: false, applyArchive: (note) => { parts[pos] = { text: note }; }, @@ -192,16 +193,17 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { if (!isObj(item)) return; const partsKey = item.type === "message" ? "content" : item.type === "function_call_output" || item.type === "custom_tool_call_output" ? "output" : undefined; if (partsKey === undefined || !Array.isArray(item[partsKey])) return; - if ((item.type === "function_call_output" || item.type === "custom_tool_call_output") && isExemptOutput(protocol, item, names)) return; + const exemptOutput = partsKey === "output" && isExemptOutput(protocol, item, names); const parts = item[partsKey] as unknown[]; parts.forEach((part, pos) => { if (!isObj(part) || part.type !== "input_image" || typeof part.image_url !== "string") return; const d = parseDataUrl(part.image_url); - if (!d || (partsKey === "output" && matchesOriginalImageReceipt(d.base64, parts))) return; + if (!d) return; occ.push({ entryIndex, b64: d.base64, mediaType: d.mediaType, + exempt: exemptOutput || (partsKey === "output" && matchesOriginalImageReceipt(d.base64, parts)), protectedTail: false, archived: false, applyArchive: (note) => { parts[pos] = { type: "input_text", text: note }; }, @@ -217,7 +219,7 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { const names = callNameMap(protocol, body); messages.forEach((m, entryIndex) => { if (!isObj(m)) return; - if (protocol === "openai" && m.role === "tool" && isExemptOutput(protocol, m, names)) return; + const exemptOutput = protocol === "openai" && m.role === "tool" && isExemptOutput(protocol, m, names); if (!Array.isArray(m.content)) return; const content = m.content as unknown[]; if (protocol === "anthropic") addAnthropicImages(content, entryIndex, false); @@ -235,14 +237,14 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { entryIndex, b64: d.base64, mediaType: d.mediaType, + exempt: exemptOutput, protectedTail: false, archived: false, applyArchive: (note) => { content[pos] = { type: "text", text: note }; }, applyShrink: (b64) => { p.image_url = typeof iu === "string" ? dataUrl(d.mediaType, b64) : { ...(iu ?? {}), url: dataUrl(d.mediaType, b64) }; }, }); } else if (p.type === "tool_result" && Array.isArray(p.content)) { - if (isExemptOutput(protocol, p, names)) return; - addAnthropicImages(p.content, entryIndex, true); + addAnthropicImages(p.content, entryIndex, true, isExemptOutput(protocol, p, names)); } }); }); @@ -268,7 +270,7 @@ export async function shedWireImages( return { body, stats: noop }; } if (!isObj(parsed)) return { body, stats: noop }; - const occurrences = collectOccurrences(protocol, parsed); + const occurrences = collectOccurrences(protocol, parsed).filter((o) => !o.exempt); if (occurrences.length === 0) return { body, stats: noop }; const totalEntries = protocol === "google" ? (Array.isArray(parsed.contents) ? parsed.contents.length : 0) @@ -336,8 +338,8 @@ export async function shedWireImages( * - the protected tail YIELDS to the cap: an upstream count wall is HARD (sending * over it fails the whole request), unlike the WS byte budget which is soft — * once non-tail images are exhausted, tail images are archived too (oldest-first). - * Exempt read_original_image occurrences never enter the walk (collectOccurrences - * skips them); if exempt images alone exceed the cap the body cannot be brought + * Exempt read_original_image occurrences count toward the cap but never enter the + * archive walk; if exempt images alone exceed the cap the body cannot be brought * under it and the rejection stays visible. Pure on the string; byte-identical * passthrough when the count is already within maxImages or nothing was sheddable. */ export async function shedWireImagesToCount( @@ -371,6 +373,7 @@ export async function shedWireImagesToCount( if (remaining <= maxImages) break; for (const o of occurrences) { if (remaining <= maxImages) break; + if (o.exempt) continue; if (o.archived) continue; if (!allowTail && o.protectedTail) continue; const note = await shedNote(o.mediaType, o.b64, opts.resolveRef); diff --git a/src/image-rescale.ts b/src/image-rescale.ts index b8d1c54332..3c8dc873ae 100644 --- a/src/image-rescale.ts +++ b/src/image-rescale.ts @@ -149,10 +149,10 @@ function loadJimp(): Promise { return jimpLoad; } -// force (#3701 byte-shed ladder): skips the 100 KiB trigger gate and adopts ANY -// strict byte reduction (not just <80%). A within-maxDim image is NOT skipped -// here — a same-dim re-encode is adopted only if strictly smaller; the byte-shed -// caller pre-filters maxDim-fitting images before calling (egress-shed rung B). +// force (#3701 byte-shed ladder): adopts ANY strict byte reduction (not just <80%). +// The 100 KiB trigger gate applies equally to both; a within-maxDim image is +// re-encoded only if it exceeds the trigger. The byte-shed caller pre-filters +// maxDim-fitting images before calling (egress-shed rung B). async function doRescale(mime: RescaledMime, b64: string, origBytes: number, maxDim: number, force = false): Promise { try { const mod = await loadJimp(); @@ -163,7 +163,7 @@ async function doRescale(mime: RescaledMime, b64: string, origBytes: number, max // Re-check the gate against the AUTHORITATIVE dims (the header parse // may have failed for exotic containers). const scale = Math.min(1, maxDim / Math.max(ow, oh)); - if (scale >= 1 && (!force || origBytes <= RESCALE_TRIGGER_BYTES)) return null; + if (scale >= 1 && origBytes <= RESCALE_TRIGGER_BYTES) return null; const tw = Math.max(1, Math.round(ow * scale)); const th = Math.max(1, Math.round(oh * scale)); if (tw !== ow || th !== oh) await img.resize({ w: tw, h: th }); diff --git a/src/listen-policy.ts b/src/listen-policy.ts index ac11fe0e87..b08e054bfa 100644 --- a/src/listen-policy.ts +++ b/src/listen-policy.ts @@ -67,6 +67,13 @@ export interface EaddrinuseContext { lastTriedPort: number; } +/** Use the existing ephemeral fallback when either the retry budget or the + * valid numeric port range is exhausted. Both pre- and post-bind retries + * must avoid handing 65536 to Node's listener. */ +export function nextListenPort(port: number, nextAttempt: number, maxAttempts: number): number { + return nextAttempt >= maxAttempts || port >= 65535 ? 0 : port + 1; +} + export function decideEaddrinuse(ctx: EaddrinuseContext): EaddrinuseAction { // The twin rule outranks everything else: a healthy peer already serves // real traffic on that port, so no wait/ladder variant may touch it — the @@ -89,7 +96,7 @@ export function decideEaddrinuse(ctx: EaddrinuseContext): EaddrinuseAction { return { kind: "wait-predecessor", port: ctx.lastTriedPort }; } const nextAttempts = ctx.attempts + 1; - return { kind: "ladder", nextPort: nextAttempts === ctx.maxAttempts ? 0 : ctx.lastTriedPort + 1 }; + return { kind: "ladder", nextPort: nextListenPort(ctx.lastTriedPort, nextAttempts, ctx.maxAttempts) }; } /** #3044/#2941: ladder rights for the POST-BIND claim check (port-owner's diff --git a/src/output-steering.ts b/src/output-steering.ts index 38059c52c2..96f1432505 100644 --- a/src/output-steering.ts +++ b/src/output-steering.ts @@ -211,8 +211,10 @@ function normalizeGoogle(contents: unknown): StructuralMessage[] { for (const raw of parts) { const p = asRecord(raw); if (!p) return [{ role: "user", text: "" }]; - if (p.functionResponse) blocks.push({ kind: "tool_result" }); - else if (typeof p.text === "string") { + if (p.functionResponse) { + const response = asRecord(asRecord(p.functionResponse)?.response); + blocks.push({ kind: "tool_result", isError: response !== null && Object.hasOwn(response, "error") }); + } else if (typeof p.text === "string") { if (p.text.trim() !== "") blocks.push({ kind: "text" }); } else if (p.inlineData || p.fileData || p.videoMetadata) blocks.push({ kind: "image" }); else return [{ role: "user", text: "" }]; diff --git a/src/port-owner.ts b/src/port-owner.ts index 436ddeefa0..1c881427fd 100644 --- a/src/port-owner.ts +++ b/src/port-owner.ts @@ -1,4 +1,5 @@ import { isProxyInstanceFile, type ProxyInstanceFile } from "./instance.js"; +import { nextListenPort } from "./listen-policy.js"; /** #2941: post-bind port-ownership verification + pointer-clobber guard for * same-lane double-spawn residuals (#707 follow-ups). Conservative by @@ -170,7 +171,7 @@ export async function resolvePortClaim(params: { return { kind: "die", message: `port ${portOf(params.origin)} is shared with ${who} — refusing to serve a contested port (#2941)` }; } const attempts = params.listenAttempts + 1; - const nextPort = attempts >= params.maxListenAttempts ? 0 : portOf(params.origin) + 1; + const nextPort = nextListenPort(portOf(params.origin), attempts, params.maxListenAttempts); return { kind: "ladder", nextPort, foreign }; } diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index e949df7444..d7b1d8b26c 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -264,7 +264,7 @@ function sortKeys(value: unknown): unknown { if (Array.isArray(value)) return value.map(sortKeys); if (value && typeof value === "object") { const record = value as Record; - const out: Record = {}; + const out: Record = Object.create(null); for (const key of Object.keys(record).sort()) out[key] = sortKeys(record[key]); return out; } @@ -289,7 +289,7 @@ function canonicalize(value: unknown): unknown { if (Array.isArray(value)) return value.map(canonicalize); if (value && typeof value === "object") { const record = value as Record; - const out: Record = {}; + const out: Record = Object.create(null); for (const key of Object.keys(record).sort()) { if (key === "cache_control") continue; // #2188: transport-layer hint, not content out[key] = canonicalize(record[key]); diff --git a/src/registry.ts b/src/registry.ts index 8b9eb5d565..b9acc7c476 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -446,8 +446,7 @@ export function scanRegistryCostRow( let chosen: { row: CostRow; key: string; profile: PriceProfile } | undefined; const seen = new Set(); const parts: string[] = []; - for (const { lower, key, value: row } of idx.entries) { - if (!lower.endsWith(suffix)) continue; + for (const { key, value: row } of suffixEntries(idx, suffix)) { const profile = priceProfileFromRow(row); if (!profile) continue; const sig = `${profile.w}|${profile.r}|${profile.q}`; @@ -539,22 +538,44 @@ export function modelVariants(name: string): string[] { // byLower keeps only the FIRST original key per lowercased form (case-only // collisions in one roster are degenerate); entries keeps every key so the // relay scan still sees all providers. +type LowerKeyEntry = { lower: string; key: string; value: T }; type LowerKeyIndex = { - byLower: Map; - entries: Array<{ lower: string; key: string; value: T }>; + byLower: Map>; + entries: Array>; + bySuffix?: Map>>; }; function buildLowerKeyIndex(obj: Record): LowerKeyIndex { - const byLower = new Map(); - const entries: Array<{ lower: string; key: string; value: T }> = []; + const byLower = new Map>(); + const entries: Array> = []; for (const [key, value] of Object.entries(obj)) { const lower = key.toLowerCase(); - if (!byLower.has(lower)) byLower.set(lower, { key, value }); - entries.push({ lower, key, value }); + const entry = { lower, key, value }; + if (!byLower.has(lower)) byLower.set(lower, entry); + entries.push(entry); } return { byLower, entries }; } +// Relay lookups match a slash-delimited suffix, including nested model ids. +// Build the index only when that fallback is used; known-provider/exact hits +// do not need it. Buckets retain every original row in snapshot key order. +function suffixEntries(index: LowerKeyIndex, suffix: string): ReadonlyArray> { + if (!index.bySuffix) { + const bySuffix = new Map>>(); + for (const entry of index.entries) { + for (let slash = entry.lower.indexOf("/"); slash >= 0; slash = entry.lower.indexOf("/", slash + 1)) { + const key = entry.lower.slice(slash); + const bucket = bySuffix.get(key); + if (bucket) bucket.push(entry); + else bySuffix.set(key, [entry]); + } + } + index.bySuffix = bySuffix; + } + return index.bySuffix.get(suffix) ?? []; +} + function registryLowerIndex(reg: RegistryShape): LowerKeyIndex { if (regLowerIdx && regLowerIdx.reg === reg) return regLowerIdx.idx; regLowerIdx = { reg, idx: buildLowerKeyIndex(reg) }; @@ -615,8 +636,7 @@ function registryLookup(reg: RegistryShape | null, model: string, host?: string, let max: number | undefined; const distinct = new Set(); const parts: string[] = []; - for (const { lower, key, value: entry } of idx.entries) { - if (!lower.endsWith(suffix)) continue; + for (const { key, value: entry } of suffixEntries(idx, suffix)) { const value = entry.limit?.[field]; if (typeof value !== "number" || value <= 0) continue; if (max === undefined || value > max) max = value; diff --git a/src/responses-ws.ts b/src/responses-ws.ts index d1c5abddd1..49aac8aa94 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -13,7 +13,7 @@ import { markWsLaneTransport, withFetchTransport } from "./fetch-transport.js"; import { BILI_HOP_HEADER } from "./server/context-window.js"; import { connectionNamedHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; import { BILI_TUNNEL_HEADER } from "./tunnel-guard.js"; -import { normalizeSseLineEndings, finalizeSseLineEndings } from "./sse-util.js"; +import { normalizeSseLineEndings, finalizeSseLineEndings, sseEventData } from "./sse-util.js"; import type { WsBridgeCodec, WsBridgeContext, WsBridgeSession } from "./ws-bridge.js"; type JsonObject = Record; @@ -880,30 +880,29 @@ export class ResponsesWsSession implements WsBridgeSession { let buffer = ""; for (;;) { const { done, value } = await reader.read(); - if (done) break; - buffer += decoder.decode(value, { stream: true }); + buffer += done ? decoder.decode() : decoder.decode(value, { stream: true }); if (buffer.length > this.budget) throw new Error("Warmup output buffer limit exceeded"); + buffer = done ? finalizeSseLineEndings(buffer) : normalizeSseLineEndings(buffer); for (;;) { const index = buffer.indexOf("\n\n"); if (index < 0) break; const block = buffer.slice(0, index); buffer = buffer.slice(index + 2); - for (const line of block.split("\n")) { - if (!line.startsWith("data: ")) continue; - const payload = line.slice(6); - sendFrame(payload); - let frame: unknown; - try { frame = JSON.parse(payload); } catch { continue; } - if (!object(frame)) continue; - if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(frame.type))) { - sawTerminal = true; - // #3342: even a no-generation warmup must establish a usable - // client checkpoint, so a later previous_response_id - // continuation resolves instead of throwing not_found. - if (frame.type === "response.completed" && object(frame.response)) this.history.commit(body, frame.response); - } + const payload = sseEventData(block); + if (payload === null) continue; + sendFrame(payload); + let frame: unknown; + try { frame = JSON.parse(payload); } catch { continue; } + if (!object(frame)) continue; + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(frame.type))) { + sawTerminal = true; + // #3342: even a no-generation warmup must establish a usable + // client checkpoint, so a later previous_response_id + // continuation resolves instead of throwing not_found. + if (frame.type === "response.completed" && object(frame.response)) this.history.commit(body, frame.response); } } + if (done) break; } if (!sawTerminal) { this.history.clear(); diff --git a/src/upstream-alerts.ts b/src/upstream-alerts.ts index 27e3fb2ec8..ecd12a09bb 100644 --- a/src/upstream-alerts.ts +++ b/src/upstream-alerts.ts @@ -121,6 +121,8 @@ export function recordUpstreamAlert(url: string, error: unknown, viaProxy: boole existing.count += 1; existing.hint = hint; if (code !== undefined) existing.code = code; + delete existing.skippedProxy; + delete existing.skippedSource; Object.assign(existing, skippedFields); return; } diff --git a/src/upstream-fail.ts b/src/upstream-fail.ts index 3f870361cd..c2fdd448b6 100644 --- a/src/upstream-fail.ts +++ b/src/upstream-fail.ts @@ -38,7 +38,7 @@ export type UpstreamFailureKind = * replays within the retry budget (#5807/#5831). Distinct from * upstream-timeout: a mid-stream (body) idle expiry stays non-retried. */ | "upstream-headers-timeout" - /** Connect-phase timeout (undici UND_ERR_CONNECT_TIMEOUT): the TCP + /** Connect-phase timeout (undici or an OS timeout proven to be at connect): the TCP * handshake never completed, so nothing reached the upstream and each * attempt costs at most one connect timeout — replayed within the retry * budget (#1453). */ @@ -87,6 +87,16 @@ function chainOf(error: unknown): Array> { return out; } +function isConnectPhaseTimeout(entry: Record): boolean { + if (entry.syscall !== undefined && entry.syscall !== "connect") return false; + if (!Array.isArray(entry.errors)) return entry.syscall === "connect"; + if (entry.errors.length === 0) return false; + for (const attempt of entry.errors) { + if (!(attempt instanceof Error) || (attempt as Error & { syscall?: unknown }).syscall !== "connect") return false; + } + return true; +} + /** Classify a transport-level failure into the diagnostic taxonomy. Pure: * no IO, no clock — safe to call from anywhere, including tests. */ export function classifyUpstreamFailure(error: unknown, ctx: UpstreamFailCtx = {}): UpstreamFailureKind { @@ -106,7 +116,8 @@ export function classifyUpstreamFailure(error: unknown, ctx: UpstreamFailCtx = { continue; } if (code === "UND_ERR_CONNECT_TIMEOUT") return "connect-timeout"; - if (code === "ETIMEDOUT" || code === "UND_ERR_HEADERS_TIMEOUT" || code === "UND_ERR_BODY_TIMEOUT") return "upstream-timeout"; + if (code === "ETIMEDOUT") return isConnectPhaseTimeout(entry) ? "connect-timeout" : "upstream-timeout"; + if (code === "UND_ERR_HEADERS_TIMEOUT" || code === "UND_ERR_BODY_TIMEOUT") return "upstream-timeout"; if (code === "ECONNRESET" || code === "EPIPE" || code === "ECONNABORTED" || code === "UND_ERR_SOCKET") return ctx.viaProxy ? "proxy-reset" : "upstream-reset"; if (code === "ECONNREFUSED") return "connect-refused"; // #2465/#5663: ICMP no-route family — before this it fell through to diff --git a/src/upstream-proxy.ts b/src/upstream-proxy.ts index 4d26784ff8..c2f548a79f 100644 --- a/src/upstream-proxy.ts +++ b/src/upstream-proxy.ts @@ -530,10 +530,12 @@ export function connectThroughProxy(host: string, port: number, proxyUrl: string if (chunk === null) return; buffer = Buffer.concat([buffer, chunk]); const end = buffer.indexOf("\r\n\r\n"); - if (end < 0) { - if (buffer.length > 64 * 1024) finishError(new Error("upstream proxy CONNECT response header is too large")); + const headerBytes = end < 0 ? buffer.length : end + 4; + if (headerBytes > 64 * 1024) { + finishError(new Error("upstream proxy CONNECT response header is too large")); return; } + if (end < 0) return; socket.removeListener("readable", onReadable); const head = buffer.subarray(0, end).toString("latin1"); const statusLine = head.split("\r\n", 1)[0] ?? ""; diff --git a/src/upstream-throttle.ts b/src/upstream-throttle.ts index ae530aaa4f..95977fdccf 100644 --- a/src/upstream-throttle.ts +++ b/src/upstream-throttle.ts @@ -371,12 +371,22 @@ function parseRetryAfterMs(headerValue: string | null, now: number): number | nu function holdKey(url: string, headers: Record): { key: string; host: string; cred: string | undefined } { let host = url; + let cred = credentialFingerprint(headers); try { - host = new URL(url).host; + const parsed = new URL(url); + host = parsed.host; + // Native Google model RPCs also accept query API keys. Keep header + // identities unchanged and do not guess between conflicting values. + if (cred === undefined && /\/models\/[^/]+:(?:generateContent|streamGenerateContent|countTokens)$/.test(parsed.pathname)) { + const values = [...parsed.searchParams.getAll("key"), ...parsed.searchParams.getAll("$key")]; + const queryKey = values[0]; + if (queryKey !== undefined && values.every(value => value === queryKey)) { + cred = credentialFingerprint({ "x-goog-api-key": queryKey }); + } + } } catch { /* opaque url — key by the raw value */ } // Multi-account safety (#2131 fingerprint): two credentials on the same // origin are independent rate-limit pools and must not hold each other. - const cred = credentialFingerprint(headers); return { key: cred === undefined ? host : `${host}\u0000${cred}`, host, cred }; } diff --git a/src/web/blog-refresh.ts b/src/web/blog-refresh.ts index 06fadcedf4..bf90c7f687 100644 --- a/src/web/blog-refresh.ts +++ b/src/web/blog-refresh.ts @@ -69,6 +69,9 @@ const MAX_IMAGE_CANDIDATES = 50; // deduped relative targets per type TextFileMap = Map; // repo-relative path → raw text type BinaryAssetMap = Map; // repo-relative path → bytes +/** A confirmed missing source is distinct from a failed download. */ +class TextFileNotFound extends Error {} + let lastRoundStartedAt = 0; let inflight: Promise | undefined; let warmAttempted = false; @@ -203,13 +206,23 @@ async function runRound(): Promise { // kept in `files` under MANIFEST_REL — buildRemoteBlogPosts re-reads // it by that key when assembling the article section. let entries: ArticleEntry[] = []; + let manifestText: string | undefined; try { - const manifestText = await getTextChain(MANIFEST_REL, chain, account); + manifestText = await getTextChain(MANIFEST_REL, chain, account); + } catch (error) { + // Only a clean absence can remove the previous article set. A + // failed read must keep the last complete overlay, like a post. + if (!(error instanceof TextFileNotFound)) throw error; + } + if (manifestText !== undefined) { files.set(MANIFEST_REL, manifestText); - const parsed = JSON.parse(manifestText) as { articles?: unknown }; - if (Array.isArray(parsed.articles)) entries = parsed.articles.filter(isArticleEntry); - } catch { - entries = []; + try { + const parsed = JSON.parse(manifestText) as { articles?: unknown }; + if (Array.isArray(parsed.articles)) entries = parsed.articles.filter(isArticleEntry); + } catch { + // A completely downloaded but malformed manifest retains the + // existing posts-only behavior; it is not a transport failure. + } } const wanted: string[] = []; for (const e of entries) { @@ -307,7 +320,8 @@ async function getJsonList(url: string): Promise { if (!Array.isArray(raw)) throw new Error("contents listing is not a JSON array"); return raw as ContentsEntry[]; } finally { - result.stopIdleTimer(); // body fully consumed (fetch-util #552 contract) + await releaseUnreadBody(result.response); + result.stopIdleTimer(); } } @@ -317,16 +331,18 @@ async function getJsonList(url: string): Promise { async function fetchPlainText(url: string, account?: (bytes: number) => void): Promise { const result = await fetchWithTimeout(url, ghHeaders(), FETCH_TIMEOUT_MS); try { + if (result.response.status === 404) throw new TextFileNotFound(`plain GET ${url} → HTTP 404`); if (!result.response.ok) throw new Error(`plain GET ${url} → HTTP ${result.response.status}`); const declared = contentLengthOf(result.response.headers); if (declared !== undefined && declared > MAX_TEXT_BYTES) { throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes (content-length ${declared}): ${url}`); } - const buf = Buffer.from(await result.response.arrayBuffer()); - if (buf.byteLength > MAX_TEXT_BYTES) throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${url}`); + const buf = await readCappedBytes(result.response.body, MAX_TEXT_BYTES); + if (buf === undefined) throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${url}`); account?.(buf.byteLength); return buf.toString("utf8"); } finally { + await releaseUnreadBody(result.response); result.stopIdleTimer(); } } @@ -338,20 +354,18 @@ async function fetchApiText(rel: string, account?: (bytes: number) => void): Pro const url = `${GITHUB_CONTENTS_URL}/${encRelPath(rel)}?ref=${BRANCH}`; const result = await fetchWithTimeout(url, ghHeaders("application/json"), FETCH_TIMEOUT_MS); try { + if (result.response.status === 404) throw new TextFileNotFound(`contents GET ${url} → HTTP 404`); if (!result.response.ok) throw new Error(`contents GET ${url} → HTTP ${result.response.status}`); const data = (await result.response.json()) as { content?: unknown; encoding?: unknown }; if (typeof data.content !== "string") { throw new Error(`contents API omitted the body (>1 MiB cap): ${rel}`); } - // Size gate BEFORE decoding: base64 inflates payloads by ~4/3. - if (Math.ceil(data.content.length * 3 / 4) > MAX_TEXT_BYTES) { - throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${rel}`); - } - const buf = Buffer.from(data.content, "base64"); - if (buf.byteLength > MAX_TEXT_BYTES) throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${rel}`); + const buf = decodeCappedBase64(data.content, MAX_TEXT_BYTES); + if (buf === undefined) throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${rel}`); account?.(buf.byteLength); return buf.toString("utf8"); } finally { + await releaseUnreadBody(result.response); result.stopIdleTimer(); } } @@ -359,11 +373,14 @@ async function fetchApiText(rel: string, account?: (bytes: number) => void): Pro /** Text fetch through the fallback chain for ONE repo-relative path. Any * per-source failure (network, HTTP, oversize, omitted body) walks to the * next member; the round only fails when EVERY member fails, and then with - * the last error — half-fetched trees never render. On success the walk + * the last hard error (or a confirmed absence if all attempts are 404s) — + * half-fetched trees never render. On success the walk * point advances so later files skip already-proven-dead sources. */ async function getTextChain(rel: string, chain: ChainState, account?: (bytes: number) => void): Promise { const sources = sourceChainFor(rel); let lastError: unknown = new Error(`all sources failed for ${rel}`); + let lastHardError: unknown; + let hadHardError = false; for (let i = Math.min(chain.start, sources.length - 1); i < sources.length; i++) { try { const text = i === 1 ? await fetchApiText(rel, account) @@ -373,9 +390,13 @@ async function getTextChain(rel: string, chain: ChainState, account?: (bytes: nu return text; } catch (e) { lastError = e; // walk to the next fallback source + if (!(e instanceof TextFileNotFound)) { + lastHardError = e; + hadHardError = true; + } } } - throw lastError; + throw hadHardError ? lastHardError : lastError; } /** Download ONE image from the PLAIN source (raw host); 404/oversize are @@ -394,11 +415,12 @@ async function fetchPlainImage(url: string, account?: (bytes: number) => void): if (!result.response.ok) throw new Error(`image GET ${url} → HTTP ${result.response.status}`); const declared = contentLengthOf(result.response.headers); if (declared !== undefined && declared > MAX_IMAGE_BYTES) return undefined; // never download the body - const buf = Buffer.from(await result.response.arrayBuffer()); - if (buf.byteLength === 0 || buf.byteLength > MAX_IMAGE_BYTES) return undefined; + const buf = await readCappedBytes(result.response.body, MAX_IMAGE_BYTES); + if (buf === undefined || buf.byteLength === 0) return undefined; account?.(buf.byteLength); return { buf, type }; } finally { + await releaseUnreadBody(result.response); result.stopIdleTimer(); } } @@ -419,16 +441,50 @@ async function fetchApiImage(rel: string, account?: (bytes: number) => void): Pr if (!result.response.ok) throw new Error(`contents image GET ${url} → HTTP ${result.response.status}`); const data = (await result.response.json()) as { content?: unknown }; if (typeof data.content !== "string") return undefined; // >1 MiB omission → next source - if (Math.ceil(data.content.length * 3 / 4) > MAX_IMAGE_BYTES) return undefined; - const buf = Buffer.from(data.content, "base64"); - if (buf.byteLength === 0 || buf.byteLength > MAX_IMAGE_BYTES) return undefined; + const buf = decodeCappedBase64(data.content, MAX_IMAGE_BYTES); + if (buf === undefined || buf.byteLength === 0) return undefined; account?.(buf.byteLength); return { buf, type }; } finally { + await releaseUnreadBody(result.response); result.stopIdleTimer(); } } +async function releaseUnreadBody(response: Response): Promise { + if (!response.bodyUsed) await response.body?.cancel().catch(() => {}); +} + +function decodeCappedBase64(content: string, maxBytes: number): Buffer | undefined { + const compact = content.replace(/\s/g, ""); + if (Buffer.byteLength(compact, "base64") > maxBytes) return undefined; + const buf = Buffer.from(compact, "base64"); + return buf.byteLength > maxBytes ? undefined : buf; +} + +async function readCappedBytes(body: Response["body"], maxBytes: number): Promise { + if (body === null) return Buffer.alloc(0); + const reader = body.getReader(); + const chunks: Uint8Array[] = []; + let total = 0; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + if (value.byteLength === 0) continue; + if (value.byteLength > maxBytes - total) { + await reader.cancel().catch(() => {}); + return undefined; + } + total += value.byteLength; + chunks.push(value); + } + } finally { + reader.releaseLock(); + } + return Buffer.concat(chunks, total); +} + /** Image fetch through the fallback chain. Misses (true 404s / >1 MiB API * omissions) keep walking — a file absent on one host is absent nowhere we * look, and walking costs bounded calls; the chain exhausts to a MISS diff --git a/src/web/changelog.ts b/src/web/changelog.ts index e621352c7a..691e925b0c 100644 --- a/src/web/changelog.ts +++ b/src/web/changelog.ts @@ -109,8 +109,9 @@ async function runCheck(): Promise { } state = { releases, lastCheckAt: Date.now() }; } finally { - // Fully consumed the body — stop the idle watchdog so the timer - // cannot pin the event loop (fetch-util's #552 contract). + if (!result.response.bodyUsed) { + void result.response.body?.cancel().catch(() => {}); + } result.stopIdleTimer(); } } catch (e) { diff --git a/src/web/diagnostics.ts b/src/web/diagnostics.ts index e380020503..03a890a356 100644 --- a/src/web/diagnostics.ts +++ b/src/web/diagnostics.ts @@ -47,7 +47,7 @@ import { resolveDshHome } from "../client-config.js"; import { safeReadJson } from "../config.js"; import { recentClientUas } from "../client-ua-registry.js"; import { redactForPublish } from "../log-mask.js"; -import { readLogLines } from "../log-file-lines.js"; +import { readLogFileLines } from "../log-file-lines.js"; import { getLogPath } from "../logger.js"; import { logGenerations } from "../log-retention.js"; import { configFile, defaultLogFile } from "../paths.js"; @@ -96,22 +96,17 @@ function baseLines(): string[] { ]; } -// #3813: count full-pair scans so tests pin the single-read-per-report -// contract (a regression to per-tail reads would re-double the cost). +// #3813: keep tail collection shared if a topic needs both kinds of evidence. let candidateLogReads = 0; -/** All non-empty lines across every retained generation (sealed history oldest- - * first, then the active file); [] on any fs failure. ONE enumeration shared - * with the other default readers, read once per report (#3813). - * Streaming: the retention set is a byte budget of many generations, so this - * keeps only the two bounded tails the report publishes instead of every line - * of every generation. */ +/** Collect bounded chronological tails, starting with the newest generation. + * Once a requested tail is full, older files cannot contribute to it. Each + * needed file is still streamed to its end to select its newest matches. */ async function candidateLogTails(needStorage: boolean, needConflict: boolean): Promise<{ storage: string[]; conflict: string[]; - /** Files that could not be read — the tails are then INCOMPLETE and the - * report must say so rather than present a partial scan as the whole - * retained history. */ + /** Needed files that could not be read — the tails are then INCOMPLETE + * even if older readable generations fill the remaining slots. */ unreadable: string[]; }> { candidateLogReads += 1; @@ -123,12 +118,21 @@ async function candidateLogTails(needStorage: boolean, needConflict: boolean): P if (into.length > cap) into.shift(); }; try { - const files = logGenerations(getLogPath() ?? defaultLogFile()).map((g) => g.path); - for await (const line of readLogLines(files, { onError: (f) => unreadable.push(f) })) { - if (needStorage && line.includes("[storage]")) pushBounded(storage, line, STORAGE_LOG_TAIL_LINES); - if (needConflict && CONFLICT_LOG_PATTERNS.some((p) => line.includes(p))) { - pushBounded(conflict, line, CONFLICT_LOG_TAIL_LINES); + const files = logGenerations(getLogPath() ?? defaultLogFile()); + for (let i = files.length - 1; i >= 0; i--) { + const storageRemaining = needStorage ? STORAGE_LOG_TAIL_LINES - storage.length : 0; + const conflictRemaining = needConflict ? CONFLICT_LOG_TAIL_LINES - conflict.length : 0; + if (storageRemaining === 0 && conflictRemaining === 0) break; + const fileStorage: string[] = []; + const fileConflict: string[] = []; + for await (const line of readLogFileLines(files[i]!.path, { onError: (f) => unreadable.push(f) })) { + if (storageRemaining > 0 && line.includes("[storage]")) pushBounded(fileStorage, line, storageRemaining); + if (conflictRemaining > 0 && CONFLICT_LOG_PATTERNS.some((p) => line.includes(p))) { + pushBounded(fileConflict, line, conflictRemaining); + } } + storage.unshift(...fileStorage); + conflict.unshift(...fileConflict); } } catch { // Enumeration or iteration blew up: report nothing rather than a @@ -138,8 +142,7 @@ async function candidateLogTails(needStorage: boolean, needConflict: boolean): P return { storage, conflict, unreadable }; } -/** Last N `[storage]` lines from the pre-fetched candidate set ([] when none - * match). The caller reads the rotated pair once per report (#3813). #4427: +/** Last N `[storage]` lines collected for this report ([] when none match). #4427: * these are VERBATIM on-disk bytes (written under whatever BILI_LOG_MASK_HOSTS * was in effect), so each line gets the same publish-boundary redaction + * length bound as conflictLogTail — a [storage] line can carry a URL with @@ -466,13 +469,7 @@ function attributionDiagnosticsLines(): string[] { export async function buildDiagnostics(topic?: string): Promise { const t = safePrefix(topic?.trim() ?? "", 64); const def = t ? TOPICS[t] : undefined; - // #3813: read the rotated log pair at most ONCE per report — the storage - // tail and the conflict banner filter the SAME candidate lines, so a - // conflict-banner request used to read the whole pair twice (hundreds of MB - // possible), stalling the event loop synchronously. Both tails are now - // collected in the same streaming pass, which retains only their bounded - // buffers instead of every line of every generation. - const needStorage = Boolean(def); + const needStorage = t === "storage-legacy-only"; const needConflict = t === "conflict-banner"; const tails = needStorage || needConflict ? await candidateLogTails(needStorage, needConflict) @@ -480,7 +477,7 @@ export async function buildDiagnostics(topic?: string): Promise` : ""); + } else { + parts.push(/^(https?:\/\/|\/)/i.test(url) ? `${label}` : label); + } + copied = search = urlEnd + 1; + } + parts.push(s.slice(copied)); + return parts.join(""); + } + function inlineMd(s: string): string { let out = escape(s); out = out.replace(/`([^`]+)`/g, (_m, code: string) => @@ -43,18 +83,12 @@ export function makeMdRender(): MdRenderApi { // panel's own bundled-asset route (/__bili/blog-assets/*, see // web/blogfeed.ts). Quotes are already escaped by escape, so attribute // injection is impossible. - out = out.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (_m, alt: string, url: string) => { - if (/^(?:https?:\/\/|\/__bili\/blog-assets\/)/i.test(url)) return `${alt}`; - return ""; - }); + out = inlineLinks(out, true); out = out.replace(/\*\*([^*]+)\*\*/g, "$1"); out = out.replace(/(^|[^*])\*([^*\n]+)\*/g, "$1$2"); // [text](url) — only http/https/relative URLs survive; anything else // degrades to plain bracketed text (no javascript: vectors). - out = out.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_m, text: string, url: string) => { - if (/^(https?:\/\/|\/)/i.test(url)) return `${text}`; - return text; - }); + out = inlineLinks(out, false); return out; } diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 19933d6dc0..cd996fae8a 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -27,7 +27,7 @@ import * as path from "node:path"; /** #1420/#1937: read-only session browsing for the web UI. Merges the LIVE * in-memory pool (bounded — evicted sessions are gone) with the on-disk * corpus. #1937: the disk side is a BOUNDED SUMMARY INDEX — a directory walk - * over metadata only, per-file summaries cached by (mtime,size), and + * over metadata only, per-file summaries cached by (mtime,size,price), and * sequential decode of changed/new files whose parsed records are discarded * right after extraction. List/overview paths never construct or retain full * Session objects, so resident memory stays O(#sessions × ~1KB) regardless of @@ -361,6 +361,8 @@ type DiskEntry = { savedAt: number; /** null = undecodable/corrupt or renamed file — skipped (logged), never fatal. */ summary: WebSessionSummary | null; + /** Resolved rates used to derive this summary's dollar fields. */ + priceKey?: string; }; // #4990: read surface rides the persist process singleton (getStore) — a @@ -497,8 +499,9 @@ function isPristineSessionsRoot(error: unknown): boolean { return entries.every((entry) => DATA_DIR_INFRA_ENTRIES.has(entry)); } -/** Single-flight index refresh. Steady-state cost is one stat per file; only - * new/changed files (mtime OR size moved) are decoded, one at a time with a +/** Single-flight index refresh. Steady-state cost is one stat per file and + * one price lookup per distinct model/origin; new/changed files or summaries + * whose resolved prices changed are decoded, one at a time with a * GC checkpoint between files, and the parsed record is dropped immediately * after summary extraction. Top-level walk failure: serve the previous * snapshot when one exists, else propagate (→ HTTP 500, visible in UI) — @@ -524,6 +527,17 @@ async function refreshIndex(): Promise { } const prev = byFile ?? new Map(); const next = new Map(); + const prices = new Map(); + const priceKey = (summary: WebSessionSummary, fresh = false): string => { + const modelKey = JSON.stringify([summary.model, summary.upstreamOrigin]); + let key = prices.get(modelKey); + if (fresh || key === undefined) { + const price = livePriceForModel(summary.model, summary.upstreamOrigin); + key = JSON.stringify(price ? [price.input, price.output, price.cacheRead, price.cacheWrite] : null); + prices.set(modelKey, key); + } + return key; + }; const covered = liveCoveredPaths(dir); // #3199: shadow mode adopts fresh unified rows instead of decoding the // files they describe — cold-start cost drops from "decode the whole @@ -538,13 +552,14 @@ async function refreshIndex(): Promise { for (const f of files) { if (covered.has(f.abs)) continue; const p = prev.get(f.abs); - if (p && p.mtimeMs === f.mtimeMs && p.size === f.size) { + if (p && p.mtimeMs === f.mtimeMs && p.size === f.size && (!p.summary || p.priceKey === priceKey(p.summary))) { next.set(f.abs, p); continue; } const claim = claims?.get(path.basename(f.abs)); if (claim && f.mtimeMs - claim.updatedAt <= SHADOW_ROW_STALE_WINDOW_MS) { - next.set(f.abs, { abs: f.abs, mtimeMs: f.mtimeMs, size: f.size, savedAt: claim.updatedAt, summary: summaryFromRow(claim) }); + const summary = summaryFromRow(claim); + next.set(f.abs, { abs: f.abs, mtimeMs: f.mtimeMs, size: f.size, savedAt: claim.updatedAt, summary, priceKey: priceKey(summary, true) }); continue; } decodeCount++; @@ -561,6 +576,9 @@ async function refreshIndex(): Promise { if (fileNameMatchesId(path.basename(f.abs), rec.id, proto, origin)) { entry.savedAt = typeof rec.savedAt === "number" ? rec.savedAt : Date.now(); entry.summary = summaryFromRecord(rec); + // A config reload can run during the file read. Stamp the + // rates used now, not a memo from before that await. + entry.priceKey = priceKey(entry.summary, true); } else { log("warn", `[acp-web] skipping ${relName}: filename does not match record id`); } @@ -663,10 +681,14 @@ function str(v: unknown): string | undefined { * Host-aware like the pricing editor (`handlePricingGet`). */ function livePriceForSummary(s: SummarySource): LivePrice | undefined { const model = typeof s.metadata.lastModel === "string" && s.metadata.lastModel.length > 0 ? s.metadata.lastModel : undefined; + return livePriceForModel(model, s.meta.upstreamOrigin); +} + +function livePriceForModel(model: string | undefined, upstreamOrigin: string | undefined): LivePrice | undefined { if (!model) return undefined; let host: string | undefined; - if (s.meta.upstreamOrigin) { - try { host = new URL(s.meta.upstreamOrigin).host; } catch { host = undefined; } + if (upstreamOrigin) { + try { host = new URL(upstreamOrigin).host; } catch { host = undefined; } } return resolveBillingPrice(model, host); } @@ -1056,7 +1078,12 @@ async function mergedSummaries(): Promise { } if (!fromRows) { const files = await ensureIndex(); - for (const e of files.values()) if (e.summary) out.set(e.summary.id, e.summary); + // The index already chooses each id's freshest savedAt, then mtime. + // Replaying every file here would let directory order pick a stale twin. + for (const abs of byId?.values() ?? []) { + const summary = files.get(abs)?.summary; + if (summary) out.set(summary.id, summary); + } } for (const s of listSessions()) out.set(s.id, summaryOf(s, true)); return [...out.values()].sort((a, b) => b.lastSeen.localeCompare(a.lastSeen)); diff --git a/tests/affinity-json-own-keys.test.ts b/tests/affinity-json-own-keys.test.ts new file mode 100644 index 0000000000..c6a5892d95 --- /dev/null +++ b/tests/affinity-json-own-keys.test.ts @@ -0,0 +1,237 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import { after, before, test } from "node:test"; +import { neutralTurns } from "../src/affinity-neutral.ts"; +import { PrefixAffinityResolver, rawChainFingerprint, setSimhashAdoptionEnabled, stableStringify } from "../src/prefix-affinity.ts"; + +type Wire = "openai" | "anthropic" | "responses" | "google"; +const wires: Wire[] = ["openai", "anthropic", "responses", "google"]; +const question = "Inspect the JSON setting and report its exact retained value."; +const result = "Inspection completed."; + +before(() => setSimhashAdoptionEnabled(false)); +after(() => setSimhashAdoptionEnabled(true)); + +function argumentsWith(value: string): Record { + return JSON.parse('{"path":"settings.json","patch":{"__proto__":{"label":"' + value + '"}}}') as Record; +} + +function renders(args: Record): Record { + return { + openai: [ + { role: "user", content: question }, + { role: "assistant", content: null, tool_calls: [{ id: "call1", type: "function", function: { name: "inspect_json", arguments: JSON.stringify(args) } }] }, + { role: "tool", tool_call_id: "call1", content: result }, + ], + anthropic: [ + { role: "user", content: [{ type: "text", text: question }] }, + { role: "assistant", content: [{ type: "tool_use", id: "call1", name: "inspect_json", input: args }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "call1", content: result }] }, + ], + responses: [ + { type: "message", role: "user", content: [{ type: "input_text", text: question }] }, + { type: "function_call", call_id: "call1", name: "inspect_json", arguments: JSON.stringify(args) }, + { type: "function_call_output", call_id: "call1", output: result }, + ], + google: [ + { role: "user", parts: [{ text: question }] }, + { role: "model", parts: [{ functionCall: { name: "inspect_json", args } }] }, + { role: "user", parts: [{ functionResponse: { name: "inspect_json", response: { output: result } } }] }, + ], + }; +} + +function note(resolver: PrefixAffinityResolver, history: unknown[]): string { + const resolved = resolver.resolve(history); + assert.ok(resolved); + resolver.note(resolved.sessionId, resolved.incomingDepth, resolved.tailHash, + resolved.itemHashes, false, resolved.sketches, resolved.userFlags); + return resolved.sessionId; +} + +for (const value of ["null", "false", "0", '"literal"', '{"z":2,"a":1}', '["nested",{"x":3}]']) { + test(`affinity canonical JSON retains an own __proto__ value: ${value}`, () => { + const input = JSON.parse('{"z":1,"nested":[{"__proto__":' + value + ',"a":2}]}') as { nested: Array> }; + const before = JSON.stringify(input); + const actual = JSON.parse(stableStringify(input)) as { nested: Array> }; + assert.equal(Object.hasOwn(actual.nested[0]!, "__proto__"), true); + assert.deepEqual(actual, input); + assert.equal(JSON.stringify(input), before, "canonicalization does not mutate arguments"); + assert.equal(Object.getPrototypeOf(input.nested[0]), Object.prototype); + assert.equal(Object.getPrototypeOf(actual.nested[0]), Object.prototype); + }); +} + +test("affinity canonical JSON distinguishes omission and different own prototype-named values", () => { + const values = ["null", "false", "0", '"literal"', '{"label":"alpha"}', '{"label":"beta"}']; + const canonical = values.map((value) => stableStringify(JSON.parse('{"__proto__":' + value + '}'))); + assert.equal(new Set([...canonical, stableStringify({})]).size, values.length + 1); +}); + +for (const wire of wires) { + test(`${wire}: changed own JSON fields cannot reuse an exact-prefix session`, () => { + const resolver = new PrefixAffinityResolver(); + const first = renders(argumentsWith("alpha"))[wire]; + const changed = renders(argumentsWith("beta"))[wire]; + const sessionId = note(resolver, first); + const resolved = resolver.resolve(changed); + assert.ok(resolved); + assert.notEqual(resolved.sessionId, sessionId); + assert.equal(resolved.via, "new"); + assert.equal(resolved.matchedDepth, 0); + }); + + test(`${wire}: reordered own JSON fields preserve cross-wire session continuity`, () => { + const original = argumentsWith("alpha"); + const reordered = JSON.parse('{"patch":{"__proto__":{"label":"alpha"}},"path":"settings.json"}') as Record; + const resolver = new PrefixAffinityResolver(); + const sessionId = note(resolver, renders(original).openai); + const resolved = resolver.resolve(renders(reordered)[wire]); + assert.ok(resolved); + assert.equal(resolved.sessionId, sessionId); + assert.equal(resolved.via, "prefix"); + const calls = neutralTurns(renders(reordered)[wire]).find((turn) => turn.kind === "assistant")!.calls!; + assert.deepEqual(calls, ['inspect_json\u0000{"patch":{"__proto__":{"label":"alpha"}},"path":"settings.json"}']); + }); + + test(`${wire}: a legacy raw snapshot matches its own JSON but rejects changed arguments`, () => { + const original = renders(argumentsWith("alpha"))[wire]; + const raw = rawChainFingerprint(original); + assert.ok(raw); + const resolver = new PrefixAffinityResolver(); + assert.equal(resolver.importSnapshot([{ sessionId: "pfa-legacy-json-key", ...raw, lastSeen: Date.now() }]), 1); + const same = resolver.resolve(original); + assert.ok(same); + assert.equal(same.sessionId, "pfa-legacy-json-key"); + assert.equal(same.via, "prefix"); + const changed = resolver.resolve(renders(argumentsWith("beta"))[wire]); + assert.ok(changed); + assert.notEqual(changed.sessionId, same.sessionId); + assert.equal(changed.via, "new"); + }); +} + +test("opaque content and structured tool results retain prototype-named JSON fields", () => { + for (const role of ["user", "tool"]) { + const first = [{ role, content: [{ type: "document", data: argumentsWith("alpha") }] }]; + const changed = [{ role, content: [{ type: "document", data: argumentsWith("beta") }] }]; + assert.notDeepEqual(neutralTurns(first), neutralTurns(changed)); + } +}); + +test("legacy cache hints are still excluded while own content keys contribute", () => { + const plain = renders(argumentsWith("alpha")).anthropic; + const withHint = structuredClone(plain) as Array>; + withHint[1]!.cache_control = { type: "ephemeral" }; + assert.deepEqual(rawChainFingerprint(plain), rawChainFingerprint(withHint)); + assert.notDeepEqual(rawChainFingerprint(plain), rawChainFingerprint(renders(argumentsWith("beta")).anthropic)); +}); + +test("ordinary JSON keeps its established canonical output and affinity fingerprints", () => { + assert.equal(stableStringify({ z: 2, constructor: "data", prototype: { toString: "literal", a: 1 }, a: [3, 4] }), + '{"a":[3,4],"constructor":"data","prototype":{"a":1,"toString":"literal"},"z":2}'); + const ordinary = renders({ path: "settings.json", patch: { label: "alpha" } }).openai; + assert.equal(rawChainFingerprint(ordinary)!.tailHash, "58f574ee4de7076691a874488960ffbdb7f4e0ab5d6f56fc142989d93dba53f8"); + const resolved = new PrefixAffinityResolver().resolve(ordinary)!; + assert.equal(resolved.tailHash, "5fac85a550621046cb9580d52c586249e30ea482883a9adea6919afc2998394f"); + assert.equal(resolved.sessionId, "pfa-f1fc8d1a71e03bed"); +}); + +// Captured on 548e396: these snapshots omitted the original own JSON key. +const lossySnapshots = [ + { + hv: 2 as const, + tailHash: "cf7f81bf0b56e3bb42884784df9e02f0a54070bec37f0616e1016d51e84b869f", + itemHashes: [ + "1253c021a11fd1c64abe469fb110e0bad0e48feeb48a730cad1d7daa0609e40f", + "a0e20066388df753f74fdec00e3fd43140bcbf8be4ddc7764981f3756c0bcccd", + "d42bf03d7ef0e8782bd520b2834c491c31d5cb29465963c935afb4aeff5ea568", + ], + }, + { + tailHash: "08d14c2a28a75ca317f1263752919eadc90179433b3ef802e57eaaaba2ade370", + itemHashes: [ + "9e33ce45f62f5906c030b289713c93759b9008643e63d6f1c679e24ac8392423", + "cce60e5880ba4fc48a1a2755377ab0c303e38d7bc81c5926cd734fd594522543", + "2f7ad1c96536faac720256ec7a82ffa7177554b27bfa3a34b222fbcb3fdefea8", + ], + }, +]; + +for (const snapshot of lossySnapshots) { + test(`old hash domain ${"hv" in snapshot ? snapshot.hv : 1} snapshots cannot recover discarded JSON fields`, () => { + const resolver = new PrefixAffinityResolver(); + assert.equal(resolver.importSnapshot([{ sessionId: "old-lossy-key", depth: 3, lastSeen: Date.now(), ...snapshot }]), 1); + for (const value of ["alpha", "beta"]) { + const resolved = resolver.resolve(renders(argumentsWith(value)).anthropic)!; + assert.notEqual(resolved.sessionId, "old-lossy-key"); + assert.equal(resolved.via, "new"); + } + }); +} + +test("anonymous HTTP requests retain tool arguments and separate changed own JSON keys", { timeout: 30_000 }, async () => { + const { startServer } = await import("../src/server.ts"); + const { loadOptions } = await import("../src/config.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { prefixAffinity } = await import("../src/prefix-affinity.ts"); + const captured: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + captured.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write(`data: ${JSON.stringify({ id: "reply", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "Acknowledged." } }] })}\n\n`); + res.write(`data: ${JSON.stringify({ id: "reply", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 3 } })}\n\n`); + res.end("data: [DONE]\n\n"); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamAddress = upstream.address(); + assert.ok(upstreamAddress && typeof upstreamAddress !== "string"); + const origin = `http://127.0.0.1:${upstreamAddress.port}`; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + let proxy: http.Server | undefined; + try { + proxy = await startServer({ + ...loadOptions({}), port: 0, host: "127.0.0.1", upstream: origin, + routes: { [origin]: { models: { "json-key-probe": { context: 200_000 } } } }, + modelContextLimit: 200_000, compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, passthrough: false, autoUpdate: false, + autoRestartOnUpdate: false, advisoryCheck: false, releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, log: false, debug: false, + }); + await once(proxy, "listening"); + const address = proxy.address(); + assert.ok(address && typeof address !== "string"); + const url = `http://127.0.0.1:${address.port}/bili/${origin}/v1/chat/completions`; + const reordered = JSON.parse('{"patch":{"__proto__":{"label":"beta"}},"path":"settings.json"}') as Record; + const argumentsList = [argumentsWith("alpha"), argumentsWith("beta"), reordered]; + const sessionCounts: number[] = []; + for (const args of argumentsList) { + const response = await fetch(url, { + method: "POST", headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "json-key-probe", stream: true, messages: renders(args).openai }), + }); + const body = await response.text(); + assert.equal(response.status, 200, body); + sessionCounts.push(prefixAffinity.trackedSessionIds().length); + } + assert.deepEqual(sessionCounts, [1, 2, 2], "changed content forks, equivalent key order replays"); + assert.equal(captured.length, argumentsList.length); + for (const [index, raw] of captured.entries()) { + const request = JSON.parse(raw) as { messages: Array<{ tool_calls?: Array<{ function: { name: string; arguments: string } }> }> }; + const call = request.messages.flatMap((message) => message.tool_calls ?? []).find((item) => item.function.name === "inspect_json"); + assert.ok(call); + assert.equal(call.function.arguments, JSON.stringify(argumentsList[index]), "forwarded argument bytes remain exact"); + } + } finally { + if (proxy) await new Promise((resolve, reject) => proxy!.close((error) => error ? reject(error) : resolve())); + await new Promise((resolve, reject) => upstream.close((error) => error ? reject(error) : resolve())); + } +}); diff --git a/tests/billing-web.test.ts b/tests/billing-web.test.ts index 3c9a05a01b..15215e4f7c 100644 --- a/tests/billing-web.test.ts +++ b/tests/billing-web.test.ts @@ -1,7 +1,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; -import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, statSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { Readable } from "node:stream"; @@ -25,7 +25,7 @@ process.env.XDG_STATE_HOME = `${import.meta.dirname ?? "."}/.billing-web-state-$ process.env.BILI_SESSIONS_DIR = mkdtempSync(path.join(tmpdir(), "bili-billing-web-sessions-")); import { handleConfigPut, handlePricingGet } from "../src/web/api.js"; -import { buildOverview, buildSessionList, _resetDiskCacheForTest } from "../src/web/sessions-data.ts"; +import { buildOverview, buildSessionList, _resetDiskCacheForTest, _diskScanStatsForTest } from "../src/web/sessions-data.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _resetSessionsForTest, getSession } from "../src/session.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -51,6 +51,108 @@ function seedSession(id: string, model: string | undefined): void { /** Display-precision rounding the panel applies at the summary boundary. */ const r4 = (x: number): number => Math.round(x * 1e4) / 1e4; +for (const mode of ["legacy", "shadow"] as const) { + test(`cached ${mode} disk summaries follow current billing prices`, async (t) => { + resetState(); + const previousDir = process.env.BILI_SESSIONS_DIR; + const previousMode = process.env.BILI_STORAGE_UNIFIED; + const dir = mkdtempSync(path.join(tmpdir(), "bili-disk-prices-")); + process.env.BILI_SESSIONS_DIR = dir; + process.env.BILI_STORAGE_UNIFIED = mode; + _resetDiskCacheForTest(); + t.after(() => { + _resetSessionsForTest(); + _resetDiskCacheForTest(); + process.env.BILI_SESSIONS_DIR = previousDir; + process.env.BILI_STORAGE_UNIFIED = previousMode; + setBillingOverrides(undefined); + rmrf(dir); + }); + const store = new SessionStore({ dir, enabled: true, debounceMs: 0 }); + if (mode === "shadow") assert.ok(store.replicasAvailable(), "exercise real shadow rows"); + _setStoreForTest(store); + const decodedFiles = new Map(); + const readRawFile = store.readRawFile.bind(store); + let editPriceDuringRead = false; + t.mock.method(store, "readRawFile", async (file: string) => { + const raw = await readRawFile(file); + const record = raw as { id: string }; + decodedFiles.set(file, record.id); + if (editPriceDuringRead) { + editPriceDuringRead = false; + setBillingOverrides({ "claude-bill": { input: 6, output: 20, cacheRead: 0.5, cacheWrite: 7 } }); + } + return raw; + }); + for (const id of ["disk-estimated", "disk-stamped", "disk-unrelated"]) { + const s = getSession(id, { protocol: "anthropic", upstreamOrigin: "http://api.somehost.com" }); + s.metadata.lastModel = id === "disk-unrelated" ? "unrelated-unpriced" : "claude-bill"; + s.stats.requests = 2; + s.stats.inputTokens = 1_000_000; + s.stats.cachedTokens = 500_000; + s.stats.outputTokens = 10_000; + s.metadata.cacheLedger = { folds: [{ S: 50000, sigma: 5000, T: 3000, requestsAfter: 10 }] }; + if (id === "disk-stamped") s.stats.costUsd = { input: 1, output: 2, cacheRead: 3, cacheWrite: 4 }; + await store.writeNow(s); + } + _resetSessionsForTest(); + const first = await buildSessionList(); + assert.equal(first.length, 3); + assert.ok(first.every((row) => !row.live)); + assert.equal(r4(first.find((row) => row.id === "disk-estimated")!.costUsd!.total), 1.8); + const decoded = _diskScanStatsForTest().decodedTotal; + await buildSessionList(); + assert.equal(_diskScanStatsForTest().decodedTotal, decoded, "unchanged prices keep the warm index"); + + setBillingOverrides({ "claude-bill": { input: 6, output: 20, cacheRead: 0.5, cacheWrite: 7 } }); + const updated = await buildSessionList(); + const estimated = updated.find((row) => row.id === "disk-estimated")!; + assert.equal(r4(estimated.costUsd!.total), 3.45, "price edit reaches unchanged disk files"); + assert.equal(updated.find((row) => row.id === "disk-stamped")!.costUsd!.total, 10, "stamped spend stays exact"); + assert.deepEqual(estimated.rates, { w: 7, r: 0.5, q: 20 }); + assert.ok(Math.abs(estimated.savedUsd! - 0.1305) < 1e-12); + assert.equal(estimated.payback!.spentUsd, 0.0945); + assert.equal(estimated.payback!.normalOutputUsd, 0.1); + const overview = await buildOverview(); + assert.equal(r4(overview.costUsdTotal), 13.45); + assert.deepEqual(overview.moneyRates, { w: 7, r: 0.5, q: 20 }); + + const repricedDecodes = _diskScanStatsForTest().decodedTotal; + if (mode === "legacy") assert.equal(repricedDecodes, decoded + 2, "only the two affected models are decoded again"); + setBillingOverrides({ "claude-bill": { input: 6, output: 20, cacheRead: 0.5, cacheWrite: 7 } }); + await buildSessionList(); + assert.equal(_diskScanStatsForTest().decodedTotal, repricedDecodes, "equal replacement prices retain the cache"); + if (mode === "shadow") assert.equal(repricedDecodes, 0, "fresh SQL rows avoid envelope decoding even after repricing"); + + setBillingOverrides(undefined); + setRegistryForTest({}, {}); + const unpriced = (await buildSessionList()).find((row) => row.id === "disk-estimated")!; + assert.equal(unpriced.costUsd, undefined, "removed prices do not leave cached dollars"); + assert.equal(unpriced.rates, undefined); + setRegistryForTest({}, { "somehost/claude-bill": { input: 8, output: 20, cache_read: 0.5 } }); + const registry = (await buildSessionList()).find((row) => row.id === "disk-estimated")!; + assert.equal(r4(registry.costUsd!.total), 4.45, "registry reload can price a previously unpriced disk row"); + if (mode === "legacy") { + // Leave the first priced file cached, then change prices while the + // second is being decoded. Its new summary must not inherit an old + // price key memoized before the asynchronous read. + const pricedFiles = [...decodedFiles].filter(([, id]) => id !== "disk-unrelated"); + assert.equal(pricedFiles.length, 2); + const target = pricedFiles[1][0]; + const st = statSync(target); + utimesSync(target, st.atime, new Date(st.mtimeMs + 1000)); + editPriceDuringRead = true; + await buildSessionList(); + assert.equal(editPriceDuringRead, false, "the changed file was actually decoded"); + setBillingOverrides(undefined); + const reverted = await buildSessionList(); + for (const row of reverted.filter((item) => item.id !== "disk-unrelated")) { + assert.deepEqual(row.rates, { w: 8, r: 0.5, q: 20 }, "a price rollback cannot retain a mislabelled summary"); + } + } + }); +} + async function getPricing(): Promise<{ status: number; payload: Record }> { let status = 0; let payloadText = ""; diff --git a/tests/claude-watchdog-health-payload.test.ts b/tests/claude-watchdog-health-payload.test.ts new file mode 100644 index 0000000000..f2843e6235 --- /dev/null +++ b/tests/claude-watchdog-health-payload.test.ts @@ -0,0 +1,173 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { test, type TestContext } from "node:test"; +import { watchdogTick, type MutableWatchdogState, type WatchdogDeps } from "../src/claude-mcp-entry.ts"; +import { forwardTool } from "../src/mcp.ts"; +import { readClaudeManagedBaseUrl, repinClaudeManagedBaseUrl } from "../src/plugin-install.ts"; +import { rmrf } from "./tmp-rm.ts"; + +type HealthReply = { status: number; body: string; contentType?: string; stall?: "headers" | "body" }; +type Request = { listener: "old" | "replacement"; url: string; body?: Record }; + +async function fixture(t: TestContext, initialReply: HealthReply) { + let reply = initialReply; + const requests: Request[] = []; + const servers: http.Server[] = []; + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-health-payload-")); + const env = { CLAUDE_CONFIG_DIR: dir }; + const previousPin = process.env.BILI_MCP_PROXY; + t.after(async () => { + if (previousPin === undefined) delete process.env.BILI_MCP_PROXY; + else process.env.BILI_MCP_PROXY = previousPin; + for (const server of servers) { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } + rmrf(dir); + }); + + const listen = async (listener: "old" | "replacement"): Promise => { + const server = http.createServer(async (req, res) => { + let raw = ""; + for await (const chunk of req) raw += chunk; + const body = raw ? JSON.parse(raw) as Record : undefined; + requests.push({ listener, url: req.url ?? "", body }); + if (req.url === "/__bili/health" && listener === "old") { + if (reply.stall === "headers") return; + res.writeHead(reply.status, { "content-type": reply.contentType ?? "application/json" }); + if (reply.stall === "body") { + res.write(reply.body); + return; + } + res.end(reply.body); + return; + } + res.setHeader("content-type", "application/json"); + res.end(JSON.stringify(req.url === "/__bili/plugin/tool" + ? { ok: true, result: `ACP receipt from ${listener}`, outcome: "success" } + : { ok: true, version: "0.1.216" })); + }); + servers.push(server); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address === "object"); + return `http://127.0.0.1:${address.port}`; + }; + + const origin = await listen("old"); + const replacement = await listen("replacement"); + const settingsPath = path.join(dir, "settings.json"); + fs.writeFileSync(settingsPath, JSON.stringify({ + env: { ANTHROPIC_BASE_URL: `${origin}/bili/https://api.anthropic.com`, KEEP_SETTING: "unchanged" }, + permissions: { allow: ["Read"] }, + })); + process.env.BILI_MCP_PROXY = origin; + const state: MutableWatchdogState = { origin, failures: 0 }; + let ensureCalls = 0; + const deps: WatchdogDeps = { + ensure: async (opts) => { + ensureCalls += 1; + assert.equal(opts.port, 0); + assert.equal(opts.strictPort, false); + assert.equal(opts.lane, "claude"); + return { origin: replacement, port: Number(new URL(replacement).port), attached: true }; + }, + repin: (next) => repinClaudeManagedBaseUrl(next, env), + }; + const tick = () => watchdogTick(state, { port: 0, strict: false }, deps, () => {}); + const assertRouting = async (listener: "old" | "replacement") => { + const expected = listener === "replacement" ? replacement : origin; + assert.equal(state.origin, expected); + assert.equal(process.env.BILI_MCP_PROXY, expected); + assert.equal(readClaudeManagedBaseUrl(env), expected); + const settings = JSON.parse(fs.readFileSync(settingsPath, "utf8")); + assert.equal(settings.env.KEEP_SETTING, "unchanged"); + assert.deepEqual(settings.permissions, { allow: ["Read"] }); + const receipt = await forwardTool("acp_status", { detail: "preserved" }, 2_000, "health-payload-session"); + assert.equal(receipt.text, `ACP receipt from ${listener}`); + const posts = requests.filter((request) => request.url === "/__bili/plugin/tool"); + assert.equal(posts.length, 1); + assert.equal(posts[0].listener, listener); + assert.equal(posts[0].body?.conversationId, "health-payload-session"); + assert.deepEqual(posts[0].body?.args, { detail: "preserved" }); + }; + return { state, tick, assertRouting, ensureCalls: () => ensureCalls, setReply: (next: HealthReply) => { reply = next; } }; +} + +for (const [name, reply] of [ + ["HTML with status 200", { status: 200, body: "Development app", contentType: "text/html" }], + ["an explicit not-ok reply", { status: 200, body: '{"ok":false}' }], + ["a string ok flag", { status: 200, body: '{"ok":"true"}' }], + ["a numeric ok flag", { status: 200, body: '{"ok":1}' }], + ["a missing ok flag", { status: 200, body: '{"version":"other-service"}' }], + ["JSON null", { status: 200, body: "null" }], + ["a JSON primitive", { status: 200, body: "true" }], + ["malformed JSON", { status: 200, body: '{"ok":true' }], + ["an empty 204 reply", { status: 204, body: "" }], +] satisfies Array<[string, HealthReply]>) { + test(`Claude watchdog recovers after three health replies containing ${name}`, async (t) => { + const f = await fixture(t, reply); + await f.tick(); + assert.equal(f.state.failures, 1); + await f.tick(); + assert.equal(f.state.failures, 2); + assert.equal(f.ensureCalls(), 0); + await f.tick(); + assert.equal(f.ensureCalls(), 1); + assert.equal(f.state.failures, 0); + await f.assertRouting("replacement"); + await f.tick(); + assert.equal(f.ensureCalls(), 1, "the replacement's valid health response prevents another recovery"); + }); +} + +for (const contentType of ["application/json", "text/plain"]) { + test(`Claude watchdog accepts a valid health object served as ${contentType}`, async (t) => { + const f = await fixture(t, { status: 200, body: '{"ok":true,"version":"0.1.216"}', contentType }); + f.state.failures = 2; + await f.tick(); + assert.equal(f.state.failures, 0); + assert.equal(f.ensureCalls(), 0); + await f.assertRouting("old"); + }); +} + +test("Claude watchdog still rejects an HTTP error even with an ok payload", async (t) => { + const f = await fixture(t, { status: 503, body: '{"ok":true}' }); + for (let i = 0; i < 3; i++) await f.tick(); + assert.equal(f.ensureCalls(), 1); + await f.assertRouting("replacement"); +}); + +test("Claude watchdog requires consecutive invalid payloads after a healthy reset", async (t) => { + const f = await fixture(t, { status: 200, body: '{"ok":false}' }); + await f.tick(); + await f.tick(); + assert.equal(f.state.failures, 2); + f.setReply({ status: 200, body: '{"ok":true}' }); + await f.tick(); + assert.equal(f.state.failures, 0); + f.setReply({ status: 200, body: '{"ok":false}' }); + await f.tick(); + await f.tick(); + assert.equal(f.ensureCalls(), 0); + await f.tick(); + assert.equal(f.ensureCalls(), 1); + await f.assertRouting("replacement"); +}); + +for (const stall of ["headers", "body"] as const) { + test(`Claude watchdog bounds a stalled health ${stall} with its existing timeout`, { timeout: 10_000 }, async (t) => { + const f = await fixture(t, { status: 200, body: '{"ok":', stall }); + f.state.failures = 2; + await f.tick(); + assert.equal(f.ensureCalls(), 1, "an incomplete health reply cannot reset the failure counter"); + assert.equal(f.state.failures, 0); + await f.assertRouting("replacement"); + }); +} diff --git a/tests/continuation-error-body-cleanup.test.ts b/tests/continuation-error-body-cleanup.test.ts new file mode 100644 index 0000000000..24b1cec579 --- /dev/null +++ b/tests/continuation-error-body-cleanup.test.ts @@ -0,0 +1,151 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { Agent } from "undici"; + +import { makeContinuationRefetch } from "../src/degenerate-retry.ts"; +import { withFetchTransport } from "../src/fetch-transport.ts"; +import { _liveUpstreamTimersForTest, fetchWithTimeout } from "../src/fetch-util.ts"; +import { _resetUpstreamThrottleForTest, activeThrottleHolds } from "../src/upstream-throttle.ts"; + +const encoder = new TextEncoder(); +const body = JSON.stringify({ model: "cleanup-model", messages: [{ role: "user", content: "continue" }] }); + +function options(url: string, signal: AbortSignal = new AbortController().signal) { + return { + protocol: "openai" as const, + body, + model: "cleanup-model", + upstreamUrl: url, + reqHeaders: {}, + signal, + log: () => {}, + label: "continuation-error-body-cleanup", + }; +} + +for (const status of [302, 403, 503]) { + test(`a discarded HTTP ${status} continuation cancels its unfinished body`, async () => { + let canceled = 0; + const response = new Response(new ReadableStream({ + start(controller) { controller.enqueue(encoder.encode("upstream error")); }, + cancel() { canceled++; }, + }), { status }); + const result = await withFetchTransport(async () => response, + () => makeContinuationRefetch(options(`http://cleanup-${status}.invalid/v1/chat/completions`))()); + assert.equal(result, null); + assert.equal(canceled, 1, "the discarded body has no reader that could release it later"); + assert.equal(_liveUpstreamTimersForTest(), 0); + }); +} + +for (const cancelKind of ["reject", "never settle"] as const) { + test(`a discarded continuation returns even when cancellation will ${cancelKind}`, { timeout: 2_000 }, async () => { + let canceled = 0; + const response = new Response(new ReadableStream({ + start(controller) { controller.enqueue(encoder.encode("error")); }, + cancel() { + canceled++; + if (cancelKind === "reject") throw new Error("transport cancellation failed"); + return new Promise(() => {}); + }, + }), { status: 500 }); + const result = await withFetchTransport(async () => response, + () => makeContinuationRefetch(options(`http://cleanup-cancel-${canceled}.invalid/v1/chat/completions`))()); + assert.equal(result, null); + assert.equal(canceled, 1); + assert.equal(_liveUpstreamTimersForTest(), 0); + }); +} + +test("a successful continuation retains its byte-exact stream for the caller", async () => { + const bytes = Uint8Array.from([0, 1, 13, 10, 127, 128, 255]); + let canceled = 0; + const response = new Response(new ReadableStream({ + start(controller) { + controller.enqueue(bytes); + controller.close(); + }, + cancel() { canceled++; }, + })); + const result = await withFetchTransport(async () => response, + () => makeContinuationRefetch(options("http://cleanup-ok.invalid/v1/chat/completions"))()); + assert.ok(result); + assert.deepEqual(new Uint8Array(await new Response(result).arrayBuffer()), bytes); + assert.equal(canceled, 0); + assert.equal(_liveUpstreamTimersForTest(), 0); +}); + +test("a bodyless continuation response still returns null", async () => { + const result = await withFetchTransport(async () => new Response(null, { status: 204 }), + () => makeContinuationRefetch(options("http://cleanup-empty.invalid/v1/chat/completions"))()); + assert.equal(result, null); + assert.equal(_liveUpstreamTimersForTest(), 0); +}); + +test("a 429 continuation keeps its body for model-scope classification", async () => { + _resetUpstreamThrottleForTest(); + let canceled = 0; + const response = new Response(new ReadableStream({ + start(controller) { + controller.enqueue(encoder.encode(JSON.stringify({ error: { + code: "keyring_model_temporarily_unavailable", model: "cleanup-model", message: "model unavailable", + } }))); + controller.close(); + }, + cancel() { canceled++; }, + }), { status: 429, headers: { "retry-after": "10" } }); + try { + const result = await withFetchTransport(async () => response, + () => makeContinuationRefetch(options("http://cleanup-throttled.invalid/v1/chat/completions"))()); + assert.equal(result, null); + assert.equal(canceled, 0, "429 bodies are consumed by the existing scope classifier"); + assert.equal(activeThrottleHolds()[0]?.model, "cleanup-model"); + assert.equal(_liveUpstreamTimersForTest(), 0); + } finally { + _resetUpstreamThrottleForTest(); + } +}); + +test("an unfinished HTTP error releases a real single-connection pool for the next request", { timeout: 5_000 }, async (t) => { + let requests = 0; + let rejectedResponseClosed = false; + const server = http.createServer((request, response) => { + request.resume(); + requests++; + if (requests === 1) { + response.on("close", () => { rejectedResponseClosed = true; }); + response.writeHead(503, { "content-type": "text/plain" }); + response.write("retry unavailable"); + } else { + response.end("the next request completed"); + } + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const url = `http://127.0.0.1:${address.port}/v1/chat/completions`; + const dispatcher = new Agent({ connections: 1, pipelining: 1 }); + const clientAbort = new AbortController(); + try { + const result = await makeContinuationRefetch({ ...options(url, clientAbort.signal), dispatcher })(); + assert.equal(result, null); + const next = await fetchWithTimeout(url, { dispatcher }, 1_500); + try { + assert.equal(await next.response.text(), "the next request completed"); + assert.equal(requests, 2); + assert.equal(rejectedResponseClosed, true, "the server observes cancellation of the unfinished error response"); + } finally { + next.clearTimer(); + } + assert.equal(_liveUpstreamTimersForTest(), 0); + } finally { + t.diagnostic(JSON.stringify({ requests, rejectedResponseClosed })); + clientAbort.abort(); + await dispatcher.destroy(); + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); + } +}); diff --git a/tests/google-query-throttle.test.ts b/tests/google-query-throttle.test.ts new file mode 100644 index 0000000000..be411af218 --- /dev/null +++ b/tests/google-query-throttle.test.ts @@ -0,0 +1,199 @@ +import test, { type TestContext } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +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 { closeServers } from "./server-close-guard.ts"; +import { + _resetUpstreamThrottleForTest, + activeThrottleHolds, + noteUpstreamThrottle429, + noteUpstreamThrottleResolved, + upstreamThrottleGate, + upstreamThrottlePeekReject, +} from "../src/upstream-throttle.ts"; + +const rpcPath = "/v1beta/models/gemini-test:generateContent"; +const accountA = "synthetic-query-account-a"; +const accountB = "synthetic-query-account-b"; +const now = 1_000_000; + +function enableThrottle(t: TestContext): void { + const env = { + BILI_THROTTLE_429: "1", + BILI_THROTTLE_429_MAX_CONSECUTIVE: "5", + BILI_THROTTLE_429_BASE_DELAY_MS: "1000", + BILI_THROTTLE_429_MAX_DELAY_MS: "8000", + BILI_THROTTLE_429_SCOPE: "credential", + }; + const saved = new Map(Object.keys(env).map(name => [name, process.env[name]])); + Object.assign(process.env, env); + _resetUpstreamThrottleForTest(); + t.after(() => { + _resetUpstreamThrottleForTest(); + for (const [name, value] of saved) { + if (value === undefined) delete process.env[name]; + else process.env[name] = value; + } + }); +} + +test("a Google query-key 429 holds its account without rejecting another account", t => { + enableThrottle(t); + const origin = `https://generativelanguage.googleapis.com${rpcPath}`; + const first = `${origin}?key=${accountA}`; + const second = `${origin}?key=${accountB}`; + noteUpstreamThrottle429(first, {}, "300", now); + assert.ok(upstreamThrottleGate(first, {}, now + 1).reject); + assert.equal(upstreamThrottleGate(second, {}, now + 1).reject, undefined); + assert.equal(upstreamThrottlePeekReject(second, {}, now + 1), undefined); +}); + +for (const method of ["generateContent", "streamGenerateContent", "countTokens"]) { + for (const field of ["key", "$key"]) { + test(`Google ${method} isolates accounts authenticated with ${field}`, t => { + enableThrottle(t); + const origin = `https://google-compatible.example/v1/models/test:${method}`; + const first = `${origin}?${new URLSearchParams({ [field]: accountA, alt: "sse" })}`; + const second = `${origin}?${new URLSearchParams({ [field]: accountB, alt: "json" })}`; + noteUpstreamThrottle429(first, {}, "300", now); + assert.ok(upstreamThrottlePeekReject(first, {}, now + 1)); + assert.equal(upstreamThrottleGate(second, {}, now + 1).reject, undefined); + noteUpstreamThrottleResolved(second, {}, 200, now + 2); + assert.ok(upstreamThrottlePeekReject(first, {}, now + 3), "another account's success must not clear the held account"); + }); + } +} + +for (const queryFirst of [false, true]) { + test(`Google header and query authentication identify one account (${queryFirst ? "query" : "header"} first)`, t => { + enableThrottle(t); + const origin = `https://google-compatible.example${rpcPath}`; + const key = "synthetic key +&/=?"; + const query = `${origin}?${new URLSearchParams({ key })}`; + const headers = { "X-Goog-Api-Key": key }; + noteUpstreamThrottle429(queryFirst ? query : origin, queryFirst ? {} : headers, "300", now); + assert.ok(upstreamThrottleGate(queryFirst ? origin : query, queryFirst ? headers : {}, now + 1).reject); + }); +} + +test("matching repeated Google query aliases preserve the account identity", t => { + enableThrottle(t); + const origin = `https://google-compatible.example${rpcPath}`; + noteUpstreamThrottle429(`${origin}?key=${accountA}&%24key=${accountA}&key=${accountA}`, {}, "300", now); + assert.ok(upstreamThrottlePeekReject(origin, { "x-goog-api-key": accountA }, now + 1)); + assert.equal(upstreamThrottlePeekReject(`${origin}?key=${accountB}`, {}, now + 1), undefined); +}); + +test("Google header-authenticated requests retain their existing identity with query parameters", t => { + enableThrottle(t); + const origin = `https://google-compatible.example${rpcPath}`; + const headers = { authorization: "Bearer synthetic-header-account" }; + noteUpstreamThrottle429(`${origin}?key=${accountA}`, headers, "300", now); + assert.ok(upstreamThrottleGate(`${origin}?key=${accountB}`, headers, now + 1).reject); + assert.equal(upstreamThrottleGate(`${origin}?key=${accountA}`, { authorization: "Bearer independent-header-account" }, now + 1).reject, undefined); +}); + +for (const [label, suffix] of [ + ["none", ""], + ["empty", "?key="], + ["repeated conflict", `?key=${accountA}&key=${accountB}`], + ["empty duplicate", `?key=${accountA}&key=`], + ["alias conflict", `?key=${accountA}&%24key=${accountB}`], + ["ordinary parameters", "?quotaUser=first&alt=sse"], +]) { + test(`absent, empty, conflicting, or unrelated Google query values keep the anonymous hold (${label})`, t => { + enableThrottle(t); + const origin = `https://google-compatible.example${rpcPath}`; + noteUpstreamThrottle429(origin + suffix, {}, "300", now); + assert.ok(upstreamThrottleGate(origin, {}, now + 1).reject); + }); +} + +for (const path of ["/v1/chat/completions", "/v1/models", "/v1beta/files", "/something:generateContent"]) { + test(`a query named key on ${path} does not create a Google account identity`, t => { + enableThrottle(t); + const origin = `https://other-provider.example${path}`; + noteUpstreamThrottle429(`${origin}?key=${accountA}`, {}, "300", now); + assert.ok(upstreamThrottleGate(`${origin}?key=${accountB}`, {}, now + 1).reject); + }); +} + +test("Google query-key terminal holds remain scoped and expose no query credentials", t => { + enableThrottle(t); + const origin = `https://google-compatible.example${rpcPath}`; + const first = `${origin}?key=${accountA}`; + for (let i = 0; i < 5; i++) noteUpstreamThrottle429(first, {}, "300", now + i); + const rejected = upstreamThrottleGate(first, {}, now + 10).reject; + assert.equal(rejected?.status, 424); + assert.equal(upstreamThrottleGate(`${origin}?key=${accountB}`, {}, now + 10).reject, undefined); + const diagnostics = JSON.stringify({ holds: activeThrottleHolds(), rejected }); + assert.ok(!diagnostics.includes(accountA)); + assert.ok(!diagnostics.includes("?key=")); +}); + +for (const large of [false, true]) { + test(`Google query-key account isolation through HTTP (${large ? "pre-ingest" : "forward"} gate)`, { timeout: 20000 }, async t => { + enableThrottle(t); + const received: string[] = []; + const upstream = http.createServer((req, res) => { + const key = new URL(req.url ?? "/", "http://localhost").searchParams.get("key"); + const account = key === accountA ? "A" : key === accountB ? "B" : "unknown"; + req.resume(); + req.on("end", () => { + received.push(account); + if (account === "A") { + res.writeHead(429, { "content-type": "application/json", "retry-after": "300" }); + res.end(JSON.stringify({ error: { code: 429, message: "Synthetic account quota exhausted.", status: "RESOURCE_EXHAUSTED" } })); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "ok" }] }, finishReason: "STOP" }], usageMetadata: { promptTokenCount: 100, candidatesTokenCount: 1, totalTokenCount: 101 } })); + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const address = upstream.address(); + assert.ok(address && typeof address === "object"); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: `http://127.0.0.1:${address.port}`, + routes: {}, proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: 2_000_000, kernelConfig: defaultConfig(2_000_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, chainContentDetection: false, + autoUpdate: false, mitm: { enabled: false, domains: [] }, + }; + let proxy: http.Server | undefined; + try { + proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const proxyAddress = proxy.address(); + assert.ok(proxyAddress && typeof proxyAddress === "object"); + const post = async (key: string, text: string): Promise<{ status: number; body: string }> => { + const response = await fetch(`http://127.0.0.1:${proxyAddress.port}${rpcPath}?key=${encodeURIComponent(key)}`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `query-key-${key === accountA ? "A" : "B"}` }, + body: JSON.stringify({ contents: [{ role: "user", parts: [{ text }] }] }), + }); + return { status: response.status, body: await response.text() }; + }; + const first = await post(accountA, "Hello."); + const nextText = large ? "ordinary words ".repeat(75000) : "A fresh request."; + const independent = await post(accountB, nextText); + const held = await post(accountA, nextText); + assert.equal(first.status, 429); + assert.equal(held.status, 429); + assert.ok(held.body.includes("bili_throttle_hold")); + assert.equal(independent.status, 200, "the independent account must reach its own available provider quota"); + assert.deepEqual(received, ["A", "B"], "only the held account retry is answered locally"); + } finally { + await closeServers(proxy ? [proxy, upstream] : [upstream]); + } + }); +} diff --git a/tests/issue4124-image-count-cap.test.ts b/tests/issue4124-image-count-cap.test.ts index 72f2f991d0..b83b3a3e7d 100644 --- a/tests/issue4124-image-count-cap.test.ts +++ b/tests/issue4124-image-count-cap.test.ts @@ -229,7 +229,7 @@ test("#4124 D2: read_original_image outputs stay exempt (best-effort boundary)", ], }); const r = await shedWireImagesToCount(body, "responses", 1); - assert.equal(r.stats.archived, 2, "only the two oldest ARCHIVABLE images go"); + assert.equal(r.stats.archived, 3, "the exempt original consumes the only available image slot"); const parsed = JSON.parse(r.body) as { input: Array> }; const out = parsed.input.find((x) => x.type === "function_call_output")!; assert.ok(JSON.stringify(out.output).includes(b64(99)), "the exempt output rides untouched"); diff --git a/tests/issue7120-connect-phase-timeout.test.ts b/tests/issue7120-connect-phase-timeout.test.ts new file mode 100644 index 0000000000..cb225bdaf4 --- /dev/null +++ b/tests/issue7120-connect-phase-timeout.test.ts @@ -0,0 +1,110 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { classifyUpstreamFailure, isFailFastUpstreamKind } from "../src/upstream-fail.js"; +import { ALERT_KINDS } from "../src/upstream-alerts.js"; +import { _resetFetchUtilForTest, fetchWithRetry, fetchWithTransportRetry } from "../src/fetch-util.js"; + +function timeout(syscall?: string): Error { + return Object.assign(new Error("connect ETIMEDOUT 192.0.2.1:443"), { code: "ETIMEDOUT", syscall }); +} + +function familyFailure(errors: unknown[] = [ + timeout("connect"), + Object.assign(new Error("connect ENETUNREACH 2001:db8::1:443"), { code: "ENETUNREACH", syscall: "connect" }), +]): Error { + const aggregate = Object.assign(new AggregateError(errors, ""), { code: "ETIMEDOUT" }); + return new TypeError("fetch failed", { cause: aggregate }); +} + +for (const [name, error] of [ + ["direct connect timeout", timeout("connect")], + ["wrapped connect timeout", new TypeError("fetch failed", { cause: timeout("connect") })], + ["Node dual-stack aggregate", familyFailure()], +] as const) { + test(`${name} uses the existing replayable and alertable connect category`, () => { + for (const viaProxy of [false, true]) { + const kind = classifyUpstreamFailure(error, { viaProxy }); + assert.equal(kind, "connect-timeout"); + assert.equal(isFailFastUpstreamKind(kind), true); + assert.equal(ALERT_KINDS.has(kind), true); + } + }); +} + +for (const [name, error] of [ + ["unknown phase despite connect-shaped prose", timeout()], + ["read timeout", timeout("read")], + ["write timeout", timeout("write")], + ["empty aggregate", familyFailure([])], + ["sparse aggregate", new TypeError("fetch failed", { cause: Object.assign(new AggregateError([], ""), { code: "ETIMEDOUT", errors: new Array(2) }) })], + ["aggregate with an unknown phase", familyFailure([timeout("connect"), timeout()])], + ["aggregate with a read failure", familyFailure([timeout("connect"), timeout("read")])], + ["aggregate with a non-error child", familyFailure([timeout("connect"), { syscall: "connect" }])], +] as const) { + test(`${name} remains a non-replayed timeout`, () => { + const kind = classifyUpstreamFailure(error); + assert.equal(kind, "upstream-timeout"); + assert.equal(isFailFastUpstreamKind(kind), false); + assert.equal(ALERT_KINDS.has(kind), false); + }); +} + +test("client cancellation overrides a proven connect phase", () => { + for (const error of [timeout("connect"), familyFailure()]) { + assert.equal(classifyUpstreamFailure(error, { externalAborted: true }), "client-abort"); + } +}); + +for (const send of [fetchWithRetry, fetchWithTransportRetry]) { + test(`${send.name} recovers from the Node connect aggregate without changing the request`, async (t) => { + const previousDelay = process.env.BILI_REPLAY_RETRY_BASE_MS; + const previousMax = process.env.BILI_REPLAY_RETRY_MAX; + process.env.BILI_REPLAY_RETRY_BASE_MS = "0"; + process.env.BILI_REPLAY_RETRY_MAX = "3"; + t.after(() => { + if (previousDelay === undefined) delete process.env.BILI_REPLAY_RETRY_BASE_MS; + else process.env.BILI_REPLAY_RETRY_BASE_MS = previousDelay; + if (previousMax === undefined) delete process.env.BILI_REPLAY_RETRY_MAX; + else process.env.BILI_REPLAY_RETRY_MAX = previousMax; + _resetFetchUtilForTest(); + }); + const attempts: Array<{ url: string; method: string | undefined; body: RequestInit["body"] }> = []; + t.mock.method(globalThis, "fetch", async (url: string | URL | Request, init?: RequestInit) => { + attempts.push({ url: String(url), method: init?.method, body: init?.body }); + if (attempts.length === 1) throw familyFailure(); + return new Response("ok", { status: 200 }); + }); + const retries: string[] = []; + const result = await send("https://example.com/v1/messages", { method: "POST", body: '{"messages":[]}' }, 5000, undefined, + (info) => retries.push(info.detail)); + try { + assert.equal(await result.response.text(), "ok"); + } finally { + result.clearTimer(); + } + assert.equal(attempts.length, 2); + assert.deepEqual(attempts[0], attempts[1]); + assert.equal(retries.length, 1); + assert.match(retries[0]!, /^connect-timeout /); + }); + + test(`${send.name} keeps the existing retry budget for persistent connect timeouts`, async (t) => { + const previousDelay = process.env.BILI_REPLAY_RETRY_BASE_MS; + const previousMax = process.env.BILI_REPLAY_RETRY_MAX; + process.env.BILI_REPLAY_RETRY_BASE_MS = "0"; + process.env.BILI_REPLAY_RETRY_MAX = "2"; + t.after(() => { + if (previousDelay === undefined) delete process.env.BILI_REPLAY_RETRY_BASE_MS; + else process.env.BILI_REPLAY_RETRY_BASE_MS = previousDelay; + if (previousMax === undefined) delete process.env.BILI_REPLAY_RETRY_MAX; + else process.env.BILI_REPLAY_RETRY_MAX = previousMax; + _resetFetchUtilForTest(); + }); + const failure = familyFailure(); + let attempts = 0; + t.mock.method(globalThis, "fetch", async () => { attempts++; throw failure; }); + await assert.rejects(send("https://example.com/v1/messages", { method: "POST", body: "{}" }, 5000, undefined), + (error) => error === failure); + assert.equal(attempts, 2); + }); +} diff --git a/tests/issue7140-retained-image-budget.test.ts b/tests/issue7140-retained-image-budget.test.ts new file mode 100644 index 0000000000..60e9afd5ba --- /dev/null +++ b/tests/issue7140-retained-image-budget.test.ts @@ -0,0 +1,183 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { defaultConfig } from "acp-kernel"; +import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../src/compress-tool.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { shedWireImages, shedWireImagesToCount } from "../src/egress-shed.ts"; +import { _setStoreForTest, SessionStore } from "../src/persist.ts"; +import { startServer } from "../src/server.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import type { WireProtocol } from "../src/util.ts"; +import { closeServers } from "./server-close-guard.ts"; + +const originals = [1, 2].map(n => Buffer.alloc(120, n).toString("base64")); +const ordinary = [3, 4, 5].map(n => Buffer.alloc(120, n).toString("base64")); +const dataUrl = (data: string) => `data:image/png;base64,${data}`; +const receipt = (data: string) => `Billion original image receipt v1 sha256=${createHash("sha256").update(Buffer.from(data, "base64")).digest("hex")}`; +const anthropicImage = (data: string) => ({ type: "image", source: { type: "base64", media_type: "image/png", data } }); + +function fixture(protocol: WireProtocol, viaReceipt = false, custom = false): Record { + if (protocol === "google") { + return { contents: [{ role: "user", parts: [ + ...originals.flatMap(data => [{ text: receipt(data) }, { inlineData: { mimeType: "image/png", data } }]), + ...ordinary.map(data => ({ inlineData: { mimeType: "image/png", data } })), + ] }] }; + } + if (protocol === "responses") { + return { input: [ + ...originals.flatMap((data, i) => [ + { type: custom ? "custom_tool_call" : "function_call", call_id: `restore${i}`, name: viaReceipt ? "run_code" : READ_ORIGINAL_IMAGE_TOOL_NAME, arguments: "{}" }, + { type: custom ? "custom_tool_call_output" : "function_call_output", call_id: `restore${i}`, output: [ + ...(viaReceipt ? [{ type: "input_text", text: receipt(data) }] : []), + { type: "input_image", image_url: dataUrl(data) }, + ] }, + ]), + { type: "message", role: "user", content: ordinary.map(data => ({ type: "input_image", image_url: dataUrl(data) })) }, + ] }; + } + if (protocol === "openai") { + return { messages: [ + { role: "assistant", tool_calls: originals.map((_, i) => ({ id: `restore${i}`, type: "function", function: { name: READ_ORIGINAL_IMAGE_TOOL_NAME, arguments: "{}" } })) }, + ...originals.map((data, i) => ({ role: "tool", tool_call_id: `restore${i}`, content: [{ type: "image_url", image_url: { url: dataUrl(data) } }] })), + { role: "user", content: ordinary.map(data => ({ type: "image_url", image_url: { url: dataUrl(data) } })) }, + ] }; + } + return { messages: [ + { role: "user", content: "Retrieve the original images" }, + { role: "assistant", content: originals.map((_, i) => ({ type: "tool_use", id: `restore${i}`, name: viaReceipt ? "run_code" : READ_ORIGINAL_IMAGE_TOOL_NAME, input: { imageRef: `m0000${i + 1}` } })) }, + { role: "user", content: [ + ...originals.map((data, i) => ({ type: "tool_result", tool_use_id: `restore${i}`, content: [ + ...(viaReceipt ? [{ type: "text", text: receipt(data) }] : []), + anthropicImage(data), + ] })), + ...ordinary.map(anthropicImage), + ] }, + ] }; +} + +// A wire-shape oracle independent of the production collection/filter logic. +function countImages(value: unknown): number { + if (Array.isArray(value)) return value.reduce((n, item) => n + countImages(item), 0); + if (typeof value !== "object" || value === null) return 0; + const part = value as Record; + if (part.type === "image" || part.type === "image_url" || part.type === "input_image") return 1; + if (typeof part.inlineData === "object" && part.inlineData !== null && "mimeType" in part.inlineData + && typeof part.inlineData.mimeType === "string" && part.inlineData.mimeType.startsWith("image/")) return 1; + return Object.values(part).reduce((n, item) => n + countImages(item), 0); +} + +const cases: Array<{ protocol: WireProtocol; viaReceipt?: boolean; custom?: boolean }> = [ + { protocol: "anthropic" }, { protocol: "openai" }, { protocol: "responses" }, + { protocol: "responses", custom: true }, { protocol: "google" }, + { protocol: "anthropic", viaReceipt: true }, { protocol: "responses", viaReceipt: true }, +]; + +for (const options of cases) { + test(`#7140 ${options.protocol} ${options.viaReceipt ? "receipt" : options.custom ? "custom tool" : "original"} images consume count capacity`, async () => { + const body = JSON.stringify(fixture(options.protocol, options.viaReceipt, options.custom)); + const result = await shedWireImagesToCount(body, options.protocol, 3, { resolveRef: async () => "m00042" }); + assert.equal(result.stats.archived, 2); + assert.equal(countImages(JSON.parse(result.body)), 3); + for (const data of originals) assert.ok(result.body.includes(data), "protected original bytes survive"); + assert.ok(!result.body.includes(ordinary[0])); + assert.ok(!result.body.includes(ordinary[1])); + assert.ok(result.body.includes(ordinary[2]), "oldest-first removal retains the newest ordinary image"); + assert.match(result.body, /m00042/); + assert.equal((await shedWireImagesToCount(result.body, options.protocol, 3)).body, result.body, "a converged resend is byte-stable"); + assert.equal(JSON.stringify(fixture(options.protocol, options.viaReceipt, options.custom)), body); + }); +} + +test("#7140 a fitting mixed request is byte-identical and performs no restoration lookup", async () => { + for (const { protocol, viaReceipt, custom } of cases) { + const body = JSON.stringify(fixture(protocol, viaReceipt, custom), null, 2); + const result = await shedWireImagesToCount(body, protocol, 5, { resolveRef: async () => assert.fail("under-cap lookup") }); + assert.equal(result.body, body); + assert.equal(result.stats.changed, false); + } +}); + +test("#7140 an unattainable cap keeps all originals and archives every eligible image", async () => { + const body = JSON.stringify(fixture("anthropic")); + const result = await shedWireImagesToCount(body, "anthropic", 0); + assert.equal(result.stats.archived, 3); + assert.equal(countImages(JSON.parse(result.body)), 2); + for (const data of originals) assert.ok(result.body.includes(data)); + assert.equal((await shedWireImagesToCount(result.body, "anthropic", 0)).body, result.body); +}); + +test("#7140 byte shedding still excludes restored originals from its worklist", async () => { + const parsed = fixture("anthropic"); + const messages = parsed.messages as unknown[]; + messages.push(...Array.from({ length: 8 }, (_, i) => ({ role: i % 2 ? "user" : "assistant", content: `tail ${i}` }))); + const body = JSON.stringify(parsed); + const result = await shedWireImages(body, "anthropic", 1); + assert.equal(result.stats.archived, 3); + assert.equal(result.stats.downscaled, 0); + for (const data of originals) assert.ok(result.body.includes(data)); + assert.equal(countImages(JSON.parse(result.body)), 2); +}); + +test("#7140 Google audio is not charged against the image cap", async () => { + const audio = { inlineData: { mimeType: "audio/wav", data: Buffer.alloc(120, 9).toString("base64") } }; + const body = JSON.stringify({ contents: [{ parts: [audio, { inlineData: { mimeType: "image/png", data: ordinary[0] } }] }] }); + const result = await shedWireImagesToCount(body, "google", 1); + assert.equal(result.body, body); + assert.equal(result.stats.changed, false); +}); + +for (const plugin of [false, true]) { + test(`#7140 real ${plugin ? "plugin" : "proxy"} relay rescues and remembers a cap with restored originals`, { timeout: 20_000 }, async () => { + _resetSessionsForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + const received: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + received.push(body); + const count = countImages(JSON.parse(body)); + res.writeHead(count > 4 ? 400 : 200, { "content-type": "application/json" }); + res.end(count > 4 + ? JSON.stringify({ error: { message: `Too many images in request: ${count} > 4` } }) + : JSON.stringify({ id: "msg-ok", type: "message", role: "assistant", model: "test", content: [{ type: "text", text: "ok" }], stop_reason: "end_turn", usage: { input_tokens: 10, output_tokens: 1 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as import("node:net").AddressInfo).port}`; + let proxy: http.Server | undefined; + try { + proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: origin, routes: {}, + modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as import("node:net").AddressInfo).port; + const messages = fixture("anthropic").messages as unknown[]; + for (let turn = 0; turn < 2; turn++) { + const response = await fetch(`http://127.0.0.1:${port}/bili/${origin}/v1/messages`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `retained-budget-${plugin}`, + ...(plugin ? { "x-bili-plugin": "pi" } : {}) }, + body: JSON.stringify({ model: "test", stream: false, max_tokens: 1024, messages }), + }); + assert.equal(response.status, 200, await response.text()); + assert.equal(received.length, turn + 2, "first turn retries once; the learned cap avoids the next rejection"); + messages.push({ role: "assistant", content: "ok" }, { role: "user", content: "continue" }); + } + assert.deepEqual(received.map(body => countImages(JSON.parse(body))), [5, 3, 3]); + for (const body of received) for (const data of originals) assert.ok(body.includes(data), "even the reduced sends preserve restored originals"); + assert.equal(countImages(messages), 5, "client-owned history retains every image"); + } finally { + await closeServers(proxy ? [proxy, upstream] : [upstream], "retained image count relay"); + } + }); +} diff --git a/tests/issue7146-image-byte-trigger.test.ts b/tests/issue7146-image-byte-trigger.test.ts new file mode 100644 index 0000000000..d0eddcfcbf --- /dev/null +++ b/tests/issue7146-image-byte-trigger.test.ts @@ -0,0 +1,204 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import test from "node:test"; +import { Jimp } from "jimp"; +import { defaultConfig } from "acp-kernel"; +import type { BiliMessage } from "acp-kernel/wire"; +import type { ProxyOptions } from "../src/config.js"; +import { applyImageRescaleItems, applyImageRescalePass, clearRescaleEncodeCache, shedDownscaleImage } from "../src/image-rescale.js"; +import { SessionStore, _setStoreForTest } from "../src/persist.js"; +import { _setForTest as setRegistryForTest } from "../src/registry.js"; +import { createSession, peekSession } from "../src/session.js"; +import { startServer } from "../src/server.js"; +import { startFakeUpstream } from "./wire-contract-fakes.js"; + +const pngImage = new Jimp({ width: 256, height: 256, color: 0x3070a0ff }); +const png = await pngImage.getBuffer("image/png", { deflateLevel: 0 }); +const pngExpected = await (await Jimp.read(png)).getBuffer("image/png"); +const pngBase64 = png.toString("base64"); +const pngExpectedBase64 = pngExpected.toString("base64"); + +function context(id: string) { + const session = createSession(id); + return { session, sessionId: id, maxDim: 1600, toolArmed: false, log: () => {} }; +} + +function anthropicMessage(data: string, mime = "image/png"): BiliMessage { + return { id: "picture", role: "user", contentType: "text", text: "look", + rawAnthropicBlock: { type: "image", source: { type: "base64", media_type: mime, data } } }; +} + +function anthropicData(message: BiliMessage): string { + return (message.rawAnthropicBlock as { source: { data: string } }).source.data; +} + +test.beforeEach(() => { + clearRescaleEncodeCache(); + delete process.env.BILI_IMAGE_RESCALE; +}); + +const carriers: Array<{ name: string; message: () => BiliMessage; read: (m: BiliMessage) => string }> = [ + { name: "Anthropic", message: () => anthropicMessage(pngBase64), read: anthropicData }, + { name: "OpenAI", message: () => ({ id: "picture", role: "user", contentType: "text", text: "look", + rawOpenaiContentParts: [{ type: "image_url", image_url: { url: `data:image/png;base64,${pngBase64}` } }] }), + read: m => (m.rawOpenaiContentParts as Array<{ image_url: { url: string } }>)[0].image_url.url.split(",")[1] }, + { name: "Google", message: () => ({ id: "picture", role: "user", contentType: "text", text: "look", + rawGoogleParts: [{ inlineData: { mimeType: "image/png", data: pngBase64 } }] }), + read: m => (m.rawGoogleParts as Array<{ inlineData: { data: string } }>)[0].inlineData.data }, + { name: "Responses core", message: () => ({ id: "picture", role: "user", contentType: "text", text: "look", + rawResponsesItem: { type: "message", role: "user", content: [{ type: "input_image", image_url: `data:image/png;base64,${pngBase64}` }] } }), + read: m => (m.rawResponsesItem as { content: Array<{ image_url: string }> }).content[0].image_url.split(",")[1] }, + { name: "bare sidecar", message: () => ({ id: "picture", role: "user", contentType: "text", text: "look", + imageBase64: pngBase64, imageMediaType: "image/png" }), read: m => m.imageBase64! }, +]; + +for (const carrier of carriers) { + test(`#7146 ${carrier.name}: byte-triggered PNG re-encode keeps dimensions, pixels and original history`, async () => { + assert.ok(png.length > 100 * 1024); + assert.ok(pngExpected.length < png.length * 0.8); + const input = [carrier.message()]; + const before = JSON.stringify(input); + const output = await applyImageRescalePass(input, context(`byte-trigger-${carrier.name}`)); + const actual = carrier.read(output[0]); + assert.equal(actual, pngExpectedBase64); + const decoded = await Jimp.read(Buffer.from(actual, "base64")); + assert.deepEqual([decoded.width, decoded.height], [256, 256]); + assert.deepEqual(decoded.bitmap.data, pngImage.bitmap.data); + assert.equal(JSON.stringify(input), before); + assert.match(JSON.stringify(output), /now 256×256 \(1KB\), original 256×256 \(256KB\)/); + }); +} + +for (const type of ["message", "function_call_output"]) { + test(`#7146 Responses ${type}: item-level byte trigger preserves the input`, async () => { + const key = type === "message" ? "content" : "output"; + const item = { type, role: "user", call_id: "camera", [key]: [{ type: "input_image", image_url: `data:image/png;base64,${pngBase64}` }] }; + const before = JSON.stringify(item); + const output = await applyImageRescaleItems([item], context(`byte-trigger-item-${type}`)); + const parts = (output[0] as Record>>)[key]; + assert.equal(parts[0].image_url, `data:image/png;base64,${pngExpectedBase64}`); + assert.equal(parts[1].type, "input_text"); + assert.equal(JSON.stringify(item), before); + }); +} + +test("#7146 within-limit JPEG uses the existing quality and keep-smaller policy", async () => { + const pixels = new Jimp({ width: 256, height: 256 }); + let seed = 7146; + for (let i = 0; i < pixels.bitmap.data.length; i += 4) { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + pixels.bitmap.data[i] = seed & 255; + pixels.bitmap.data[i + 1] = (seed >>> 8) & 255; + pixels.bitmap.data[i + 2] = (seed >>> 16) & 255; + pixels.bitmap.data[i + 3] = 255; + } + const original = await pixels.getBuffer("image/jpeg", { quality: 100 }); + const expected = await (await Jimp.read(original)).getBuffer("image/jpeg", { quality: 80 }); + assert.ok(original.length > 100 * 1024); + assert.ok(expected.length < original.length * 0.8); + const message = anthropicMessage(original.toString("base64"), "image/jpeg"); + const output = await applyImageRescalePass([message], context("byte-trigger-jpeg")); + assert.equal(anthropicData(output[0]), expected.toString("base64")); + const decoded = await Jimp.read(expected); + assert.deepEqual([decoded.width, decoded.height], [256, 256]); + assert.equal(anthropicData(message), original.toString("base64")); +}); + +for (const [width, expectedChange] of [[199, false], [200, true]] as const) { + test(`#7146 decoded-size threshold: ${width} by 128 PNG`, async () => { + const original = await new Jimp({ width, height: 128, color: 0x3070a0ff }).getBuffer("image/png", { deflateLevel: 0 }); + assert.equal(original.length > 100 * 1024, expectedChange); + const message = anthropicMessage(original.toString("base64")); + const output = await applyImageRescalePass([message], context(`byte-trigger-threshold-${width}`)); + assert.equal(anthropicData(output[0]) !== anthropicData(message), expectedChange); + if (!expectedChange) assert.equal(output[0], message); + }); +} + +test("#7146 large same-dimension BMP with no byte benefit stays untouched", async () => { + const original = await pngImage.getBuffer("image/bmp"); + const expected = await (await Jimp.read(original)).getBuffer("image/bmp"); + assert.ok(original.length > 100 * 1024); + assert.ok(expected.length >= original.length * 0.8); + const message = anthropicMessage(original.toString("base64"), "image/bmp"); + assert.equal((await applyImageRescalePass([message], context("byte-trigger-bmp")))[0], message); +}); + +test("#7146 repeated same-dimension encodes remain byte-identical with and without the cache", async () => { + const input = [anthropicMessage(pngBase64)]; + const ctx = context("byte-trigger-repeat"); + const first = await applyImageRescalePass(input, ctx); + assert.equal(anthropicData(first[0]), pngExpectedBase64); + assert.deepEqual(await applyImageRescalePass(input, ctx), first); + clearRescaleEncodeCache(); + assert.deepEqual(await applyImageRescalePass(input, ctx), first); +}); + +test("#7146 disabling rescale keeps byte-heavy images unchanged", async () => { + process.env.BILI_IMAGE_RESCALE = "0"; + try { + const input = [anthropicMessage(pngBase64)]; + assert.equal(await applyImageRescalePass(input, context("byte-trigger-disabled")), input); + } finally { + delete process.env.BILI_IMAGE_RESCALE; + } +}); + +test("#7146 original-image tool results retain their byte-heavy originals", async () => { + const input: BiliMessage[] = [{ ...anthropicMessage(pngBase64), role: "tool", contentType: "tool-result", toolName: "acp_read_original_image" }]; + assert.equal((await applyImageRescalePass(input, context("byte-trigger-original")))[0], input[0]); + const call = { type: "function_call", call_id: "restore", name: "acp_read_original_image", arguments: "{}" }; + const result = { type: "function_call_output", call_id: "restore", output: [{ type: "input_image", image_url: `data:image/png;base64,${pngBase64}` }] }; + const output = await applyImageRescaleItems([call, result], context("byte-trigger-original-item")); + assert.equal(output[1], result); +}); + +test("#7146 forced same-dimension encoding retains its previous gate", async () => { + assert.equal(await shedDownscaleImage("image/png", pngBase64, 1600), pngExpectedBase64); + assert.equal(await shedDownscaleImage("image/png", pngExpectedBase64, 1600), null); +}); + +for (const plugin of [false, true]) { + test(`#7146 real ${plugin ? "plugin" : "proxy"} request sends compressed bytes and keeps originals`, { timeout: 20_000 }, async () => { + const fake = await startFakeUpstream("anthropic"); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const sessionId = `byte-trigger-relay-${plugin}`; + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: fake.url, + routes: { [fake.url]: { models: { "claude-test": { context: 100_000 } } } }, + modelContextLimit: 100_000, kernelConfig: defaultConfig(100_000), + compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, + log: false, sessionHeader: "x-acp-session", debug: false, passthrough: false, autoUpdate: false, + compat: { roles: {} }, passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", + streamErrorShape: "protocol", advisoryCheck: false, releaseNotesCheck: false, mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + try { + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + const response = await fetch(`http://127.0.0.1:${port}/bili/${fake.url}/v1/messages`, { + method: "POST", headers: { "content-type": "application/json", "x-api-key": "test", "x-acp-session": sessionId, + ...(plugin ? { "x-bili-plugin": "pi" } : {}) }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, + messages: [{ role: "user", content: [{ type: "text", text: "Describe this picture." }, + { type: "image", source: { type: "base64", media_type: "image/png", data: pngBase64 } }] }] }), + }); + assert.equal(response.status, 200); + await response.text(); + const body = fake.requests[0]?.body as { messages?: Array<{ content?: Array<{ type?: string; source?: { data?: string } }> }> } | undefined; + const forwarded = body?.messages?.flatMap(m => Array.isArray(m.content) ? m.content : []).find(c => c.type === "image"); + assert.equal(forwarded?.source?.data, pngExpectedBase64); + const remembered = peekSession(sessionId)?.transient?.pluginMessages; + assert.ok(remembered); + assert.ok(remembered.processed.some(m => { + const block = (m as BiliMessage).rawAnthropicBlock as { source?: { data?: string } } | undefined; + return block?.source?.data === pngBase64; + })); + assert.ok(!remembered.processed.some(m => String(m.id).startsWith("acp_imgnote_"))); + } finally { + await new Promise((resolve, reject) => proxy.close(error => error ? reject(error) : resolve())); + await fake.close(); + } + }); +} diff --git a/tests/listen-retry-boundary.test.ts b/tests/listen-retry-boundary.test.ts new file mode 100644 index 0000000000..c49f9c416e --- /dev/null +++ b/tests/listen-retry-boundary.test.ts @@ -0,0 +1,65 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createServer } from "node:net"; +import { resolveZonePortBase } from "../src/config.ts"; +import { decideEaddrinuse, type EaddrinuseContext } from "../src/listen-policy.ts"; +import { resolvePortClaim } from "../src/port-owner.ts"; + +const base: EaddrinuseContext = { + upgradeReplacement: false, twinHolds: false, newerHolderHolds: false, + launchToken: true, strictPort: false, attempts: 0, maxAttempts: 17, + predecessorTicksLeft: 10, predecessorHolds: false, + lastTriedPort: resolveZonePortBase({ BILI_ZONE_PORT: "65535" }), +}; + +async function bindRetry(port: number): Promise { + const server = createServer(); + try { + // Exercise what the listener does with the decision. The old boundary result is + // 65536, which throws ERR_SOCKET_BAD_PORT before a listen event can be emitted. + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(port, "127.0.0.1", resolve); + }); + const address = server.address(); + assert.ok(address && typeof address === "object" && address.port > 0 && address.port <= 65535); + assert.equal(port, 0, "the OS chooses a free port when the numeric ladder is exhausted"); + } finally { + if (server.listening) await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + } +} + +async function foreignClaim(port: number, canLadder = true) { + return resolvePortClaim({ + origin: `http://127.0.0.1:${port}`, ownPid: 100, canLadder, + listenAttempts: 0, maxListenAttempts: 17, + probe: { fetchImpl: async () => ({ ok: true, json: async () => ({ ok: true, pid: 200 }) }) }, + }); +} + +test("a busy highest valid zone port retries on a bindable ephemeral port", async () => { + assert.equal(base.lastTriedPort, 65535, "the configured zone boundary is valid"); + const action = decideEaddrinuse(base); + assert.equal(action.kind, "ladder"); + if (action.kind === "ladder") await bindRetry(action.nextPort); +}); + +test("post-bind foreign ownership at the port boundary retries on a bindable ephemeral port", async () => { + const action = await foreignClaim(65535); + assert.equal(action.kind, "ladder"); + if (action.kind === "ladder") await bindRetry(action.nextPort); +}); + +test("both retry paths still try the last valid numeric port before using an ephemeral port", async () => { + assert.deepEqual(decideEaddrinuse({ ...base, lastTriedPort: 65534 }), { kind: "ladder", nextPort: 65535 }); + const action = await foreignClaim(65534); + assert.equal(action.kind === "ladder" ? action.nextPort : undefined, 65535); +}); + +test("the numeric boundary does not grant retry rights or bypass a predecessor wait", async () => { + assert.deepEqual(decideEaddrinuse({ ...base, strictPort: true }), { kind: "fail-fast", reason: "strict-port" }); + assert.deepEqual(decideEaddrinuse({ ...base, launchToken: false }), { kind: "fail-fast", reason: "port-busy" }); + assert.deepEqual(decideEaddrinuse({ ...base, upgradeReplacement: true }), { kind: "fail-fast", reason: "upgrade-replacement" }); + assert.deepEqual(decideEaddrinuse({ ...base, predecessorHolds: true }), { kind: "wait-predecessor", port: 65535 }); + assert.equal((await foreignClaim(65535, false)).kind, "die"); +}); diff --git a/tests/mdrender-link-scaling.test.ts b/tests/mdrender-link-scaling.test.ts new file mode 100644 index 0000000000..03b1837845 --- /dev/null +++ b/tests/mdrender-link-scaling.test.ts @@ -0,0 +1,51 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { runInNewContext } from "node:vm"; +import { build } from "esbuild"; +import { markdownToHtml } from "../src/web/markdown.ts"; +import { makeMdRender } from "../src/web/mdrender.ts"; + +const malformed = [ + ["unclosed link labels", "[".repeat(200_000)], + ["unclosed image labels", "![".repeat(100_000)], + ["nested link starts before an invalid URL", "[x](".repeat(30_000) + " invalid"], + ["nested URL starts sharing whitespace before a closing parenthesis", "[x](".repeat(200_000) + " )"], +] as const; + +function boundedRender(render: (source: string) => string, source: string): string { + // Interrupt the synchronous old regex path without hanging the test runner. These inputs + // need only a short scan; the budget leaves ample room for a loaded CI machine. + return runInNewContext("render(source)", { render, source }, { timeout: 2_000 }) as string; +} + +for (const [name, source] of malformed) { + test(`server Markdown finishes ${name} without rescanning every suffix`, () => { + assert.equal(boundedRender(markdownToHtml, source), `

${source}

`); + }); +} + +test("the minified detached browser renderer finishes the same malformed link inputs", async () => { + const built = await build({ entryPoints: ["src/web/mdrender.ts"], bundle: true, format: "cjs", minify: true, write: false, logLevel: "silent" }); + const module_ = { exports: {} as Record }; + new Function("module", "exports", built.outputFiles[0]!.text)(module_, module_.exports); + const bundled = module_.exports.makeMdRender as typeof makeMdRender; + const detached = new Function(`return (${bundled.toString()})();`)() as ReturnType; + for (const [, source] of malformed) assert.equal(boundedRender(detached.render, source), `

${source}

`); +}); + +const compatibility = [ + ["[outer [inner](https://example.test)", '

outer [inner

'], + ["[bad](not [good](https://example.test))", '

[bad](not good)

'], + ["[bad]([good with spaces](https://example.test))", '

[bad](good with spaces)

'], + ["![outer ![inner](https://example.test/a.png)", '

outer ![inner

'], + ["[](/no-label) ![](https://example.test/empty.png)", '

[](/no-label)

'], + ["[x](https://example.test/[path](tail))", '

x)

'], + ["[x](bad url) [ok](/safe)", '

[x](bad url) ok

'], + ["[x](javascript:alert(1))", "

x)

"], +] as const; + +for (const [source, expected] of compatibility) { + test(`link scanning preserves existing delimiter and URL rules: ${source}`, () => { + assert.equal(markdownToHtml(source), expected); + }); +} diff --git a/tests/output-steering-google-errors.test.ts b/tests/output-steering-google-errors.test.ts new file mode 100644 index 0000000000..35c040e0c7 --- /dev/null +++ b/tests/output-steering-google-errors.test.ts @@ -0,0 +1,125 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +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 { applyOutputSteering, applyOutputSteeringJson, type OutputSteeringConfig } from "../src/output-steering.ts"; + +const effortOnly: OutputSteeringConfig = { enabled: true, verbosityLevel: 0, effortRouting: true }; + +function bodyFor(responses: Array>, budget = 8000): Record { + return { + contents: [ + { role: "user", parts: [{ text: "Read the requested files and explain any problems." }] }, + { role: "model", parts: responses.map((_, i) => ({ functionCall: { name: `read_file_${i}`, args: { path: `file${i}` } } })) }, + { role: "user", parts: responses.map((response, i) => ({ functionResponse: { name: `read_file_${i}`, response } })) }, + ], + generationConfig: { thinkingConfig: { thinkingBudget: budget } }, + }; +} + +for (const [name, responses] of [ + ["structured error", [{ error: { code: "PERMISSION_DENIED", message: "Cannot read file0." } }]], + ["string error", [{ error: "File not found." }]], + ["partial output", [{ output: "First page.", error: "Remaining pages unavailable." }]], + ["error before a successful parallel result", [{ error: "Permission denied." }, { output: "Read file1." }]], + ["error after a successful parallel result", [{ output: "Read file0." }, { error: "Permission denied." }]], + ["explicit null error carrier", [{ error: null }]], + ["explicit false error carrier", [{ error: false }]], +] as Array<[string, Array>]>) { + test(`Google effort preserves the requested budget for ${name}`, () => { + const original = bodyFor(responses); + const body = JSON.stringify(original); + assert.deepEqual(applyOutputSteering(body, "google", effortOnly), { body, changed: false, labels: [] }); + assert.deepEqual(applyOutputSteeringJson(original, "google", effortOnly), []); + assert.equal(JSON.stringify(original), body); + }); +} + +for (const [name, responses] of [ + ["output envelope", [{ output: "Read file0." }]], + ["ordinary output object", [{ content: "Read file0." }]], + ["error key inside output data", [{ output: { error: "The name of a log field." } }]], + ["empty output object", [{}]], + ["successful parallel results", [{ output: "Read file0." }, { output: "Read file1." }]], +] as Array<[string, Array>]>) { + test(`Google effort still lowers a clean ${name}`, () => { + const body = bodyFor(responses); + const expected = bodyFor(responses, 128); + const result = applyOutputSteering(JSON.stringify(body), "google", effortOnly); + assert.deepEqual(JSON.parse(result.body), expected); + assert.deepEqual(result.labels, ["effort:low"]); + }); +} + +for (const [name, cfg, budget] of [ + ["disabled steering", { ...effortOnly, enabled: false }, 8000], + ["disabled effort routing", { ...effortOnly, effortRouting: false }, 8000], + ["dynamic budget", effortOnly, -1], + ["budget already at the floor", effortOnly, 128], +] as Array<[string, OutputSteeringConfig, number]>) { + test(`Google error handling leaves ${name} unchanged`, () => { + const body = JSON.stringify(bodyFor([{ error: "Cannot read file0." }], budget)); + assert.deepEqual(applyOutputSteering(body, "google", cfg), { body, changed: false, labels: [] }); + }); +} + +test("Google failed tool effort survives the real HTTP forwarding path", { timeout: 15000 }, async t => { + const seen: Array> = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + seen.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "Observed the tool response." }] }, finishReason: "STOP" }], usageMetadata: { promptTokenCount: 100, candidatesTokenCount: 5, totalTokenCount: 105 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const address = upstream.address(); + assert.ok(address && typeof address === "object"); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: `http://127.0.0.1:${address.port}`, + routes: {}, proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: 200000, kernelConfig: defaultConfig(200000), + compress: { injectTool: false, injectNudge: false, outputSteering: effortOnly }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, chainContentDetection: false, + autoUpdate: false, mitm: { enabled: false, domains: [] }, + }; + let proxy: http.Server | undefined; + try { + proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const proxyAddress = proxy.address(); + assert.ok(proxyAddress && typeof proxyAddress === "object"); + for (const [name, result, budget] of [ + ["clean", { output: "Read file0." }, 128], + ["failed", { error: { code: "PERMISSION_DENIED", message: "Cannot read file0." } }, 8000], + ] as Array<[string, Record, number]>) { + await t.test(name, async () => { + const response = await fetch(`http://127.0.0.1:${proxyAddress.port}/v1beta/models/gemini-2.5-flash:generateContent`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `google-effort-${name}` }, + body: JSON.stringify(bodyFor([result])), + }); + assert.equal(response.status, 200); + await response.text(); + const forwarded = seen.at(-1)!; + assert.equal((forwarded.generationConfig as { thinkingConfig: { thinkingBudget: number } }).thinkingConfig.thinkingBudget, budget); + assert.ok(JSON.stringify(forwarded.contents).includes(JSON.stringify(result))); + }); + } + assert.equal(seen.length, 2); + } finally { + if (proxy) { proxy.closeAllConnections(); await new Promise((resolve, reject) => proxy!.close(error => error ? reject(error) : resolve())); } + upstream.closeAllConnections(); + await new Promise((resolve, reject) => upstream.close(error => error ? reject(error) : resolve())); + } +}); diff --git a/tests/proxy-connect-header-limit.test.ts b/tests/proxy-connect-header-limit.test.ts new file mode 100644 index 0000000000..c8912affac --- /dev/null +++ b/tests/proxy-connect-header-limit.test.ts @@ -0,0 +1,118 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import net from "node:net"; +import { test } from "node:test"; +import { setTimeout as delay } from "node:timers/promises"; +import { connectThroughProxy } from "../src/upstream-proxy.ts"; +import { setupMitm } from "../src/mitm.ts"; + +const limit = 64 * 1024; +const tooLarge = /upstream proxy CONNECT response header is too large/; + +function responseHead(bytes: number): Buffer { + const prefix = "HTTP/1.1 200 Connection Established\r\nX-Pad: "; + return Buffer.from(prefix + "x".repeat(bytes - prefix.length - 4) + "\r\n\r\n"); +} + +async function withProxy(reply: (socket: net.Socket) => void, run: (url: string) => Promise): Promise { + const peers: net.Socket[] = []; + const server = net.createServer(socket => { + peers.push(socket); + socket.on("error", () => {}); + socket.once("data", () => reply(socket)); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + try { + await run(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`); + } finally { + for (const peer of peers) peer.destroy(); + await new Promise(resolve => server.close(() => resolve())); + } +} + +for (const bytes of [limit - 1, limit, limit + 1, 80 * 1024]) { + for (const splitDelimiter of [false, true]) { + test(`CONNECT enforces ${bytes}-byte response head with split delimiter=${splitDelimiter}`, async () => { + const head = responseHead(bytes); + await withProxy(socket => { + if (splitDelimiter) { + socket.write(head.subarray(0, -3)); + void delay(5).then(() => { if (!socket.destroyed) socket.write(head.subarray(-3)); }); + } else socket.write(head); + }, async url => { + const connected = connectThroughProxy("example.invalid", 443, url); + if (bytes > limit) { + await assert.rejects(connected.then(socket => { socket.destroy(); }), tooLarge); + } else { + const socket = await connected; + assert.equal(socket.destroyed, false); + socket.destroy(); + } + }); + }); + } +} + +test("CONNECT retains the existing bound before any header delimiter arrives", async () => { + const head = responseHead(80 * 1024); + head.fill(120, head.length - 4); + await withProxy(socket => { socket.write(head); }, async url => { + await assert.rejects(connectThroughProxy("example.invalid", 443, url), tooLarge); + }); +}); + +const payload = Buffer.from(Array.from({ length: 256 * 1024 }, (_, i) => i % 256)); + +for (const bytes of [1024, limit]) { + test(`CONNECT excludes 256 KiB tunnel data from a ${bytes}-byte header budget`, async () => { + await withProxy(socket => { socket.end(Buffer.concat([responseHead(bytes), payload])); }, async url => { + const socket = await connectThroughProxy("example.invalid", 443, url); + const chunks: Buffer[] = []; + try { + for await (const chunk of socket) chunks.push(chunk as Buffer); + assert.deepEqual(Buffer.concat(chunks), payload); + } finally { + socket.destroy(); + } + }); + }); +} + +for (const oversized of [false, true]) { + test(`blind tunnel ${oversized ? "rejects an oversized head" : "forwards large data after a bounded head"}`, async () => { + await withProxy(socket => { + socket.end(Buffer.concat([responseHead(oversized ? 80 * 1024 : 1024), payload])); + }, async url => { + const bili = http.createServer(); + const peers: net.Socket[] = []; + bili.on("connection", socket => peers.push(socket)); + setupMitm(bili, [], () => {}, () => url); + bili.listen(0, "127.0.0.1"); + await once(bili, "listening"); + const request = http.request({ + host: "127.0.0.1", port: (bili.address() as net.AddressInfo).port, + method: "CONNECT", path: "example.invalid:2525", + }); + const connected = once(request, "connect"); + request.end(); + let client: net.Socket | undefined; + try { + const [response, socket, head] = await connected as [http.IncomingMessage, net.Socket, Buffer]; + client = socket; + assert.equal(response.statusCode, oversized ? 502 : 200); + if (!oversized) { + const chunks: Buffer[] = [head]; + for await (const chunk of socket) chunks.push(chunk as Buffer); + assert.deepEqual(Buffer.concat(chunks), payload); + } + } finally { + client?.destroy(); + request.destroy(); + for (const peer of peers) peer.destroy(); + await new Promise(resolve => bili.close(() => resolve())); + } + }); + }); +} diff --git a/tests/registry-price-profile.test.ts b/tests/registry-price-profile.test.ts index 4887678aec..74e04bf6d0 100644 --- a/tests/registry-price-profile.test.ts +++ b/tests/registry-price-profile.test.ts @@ -1,6 +1,6 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { peekRegistryPriceProfile, providerFromHost, _resetForTest, _setForTest } from "../src/registry.ts"; +import { peekRegistryPriceProfile, scanRegistryCostRow, providerFromHost, _resetForTest, _setForTest } from "../src/registry.ts"; import bundledSnapshot from "../src/registry-snapshot.json" with { type: "json" }; // #1279 follow-up: registry-derived default price profiles. The lookup is @@ -122,3 +122,30 @@ test("bundled snapshot ships cost rows so the offline floor exercises pricing", assert.equal(typeof snap.count, "number"); assert.equal(Object.keys(snap.models ?? {}).length, snap.count); }); + + +test("relay cost suffix lookup retains first usable row and full model-id precedence", () => { + _resetForTest(); + const costs = { + "aaa/Family/Model": { input: 0, output: 0 }, + "bbb/Other/Model": { input: 9, output: 90 }, + "ccc/FAMILY/MODEL": { input: 2, output: 4, cache_read: 0 }, + "ddd/family/model": { input: 3, output: 6 }, + "EEE/FAMILY/MODEL": { input: 4, output: 8 }, + "eee/family/model": { input: 5, output: 10 }, + "fff/family/OtherModel": { input: 6, output: 12 }, + }; + for (let repeat = 0; repeat < 2; repeat++) { + const nested = scanRegistryCostRow(costs, "Family/Model", "relay.example"); + assert.equal(nested?.key, "ccc/FAMILY/MODEL"); + assert.deepEqual(nested?.profile, { w: 2, r: 0, q: 4 }); + assert.equal(scanRegistryCostRow(costs, "family/model-thinking", "relay.example")?.key, "ccc/FAMILY/MODEL"); + assert.equal(scanRegistryCostRow(costs, "MODEL", "relay.example")?.key, "bbb/Other/Model"); + assert.equal(scanRegistryCostRow(costs, "odel", "relay.example"), undefined); + assert.equal(scanRegistryCostRow(costs, "model", "api.openai.com"), undefined); + assert.equal(scanRegistryCostRow(costs, "eee/family/model", "relay.example")?.row.input, 5); + } + const replacement = { "new/family/model": { input: 7, output: 14 } }; + assert.equal(scanRegistryCostRow(replacement, "Family/Model", "relay.example")?.row.input, 7); + assert.equal(scanRegistryCostRow(costs, "Family/Model", "relay.example")?.row.input, 2); +}); diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 11fdfaa514..cdcb58603c 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -1,6 +1,6 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { contextFromRegistry, peekRegistryContext, providerFromHost, newerFallback, bundledSnapshotLookup, bundledRegistryForTestsOnly, _resetForTest, _setForTest } from "../src/registry.ts"; +import { contextFromRegistry, peekRegistryContext, peekRegistryOutputLimit, providerFromHost, newerFallback, bundledSnapshotLookup, bundledRegistryForTestsOnly, _resetForTest, _setForTest } from "../src/registry.ts"; test("providerFromHost maps minimax domains", () => { assert.equal(providerFromHost("api.minimax.chat"), "minimax"); @@ -236,3 +236,29 @@ test("roster lookup is case-insensitive on exact keys and the relay scan (#2074) // Genuinely absent models still miss. assert.equal(peekRegistryContext("nope-xyz", "relay.example"), undefined); }); + + +test("relay suffix lookup preserves nested ids, case collisions and table replacement", () => { + _resetForTest(); + const rows = { + "relayA/Family/Model": { limit: { context: 1000, output: 100 } }, + "relayB/family/model": { limit: { context: 2000, output: 80 } }, + "RELAYB/FAMILY/MODEL": { limit: { context: 3000, output: 90 } }, + "relayC/Other/Model": { limit: { context: 4000, output: 200 } }, + "relayD/Family/OtherModel": { limit: { context: 9000, output: 900 } }, + "relayE/Family/Modeling": { limit: { context: 8000, output: 800 } }, + }; + _setForTest(rows); + for (let repeat = 0; repeat < 2; repeat++) { + assert.equal(peekRegistryContext("Family/Model", "relay.example"), 3000); + assert.equal(peekRegistryOutputLimit("FAMILY/MODEL", "relay.example"), 100); + assert.equal(peekRegistryContext("model-thinking", "relay.example"), 4000); + assert.equal(peekRegistryOutputLimit("model", "relay.example"), 200); + assert.equal(peekRegistryContext("odel", "relay.example"), undefined); + assert.equal(peekRegistryContext("Family/Model", "api.openai.com"), undefined); + assert.equal(peekRegistryContext("relayB/family/model", "relay.example"), 2000); + } + _setForTest({ "new/Family/Model": { limit: { context: 7000, output: 700 } } }); + assert.equal(peekRegistryContext("family/model", "relay.example"), 7000); + assert.equal(peekRegistryOutputLimit("model", "relay.example"), 700); +}); diff --git a/tests/responses-ws-http-fallback.test.ts b/tests/responses-ws-http-fallback.test.ts index ebd644d1f8..72b8e9ebfc 100644 --- a/tests/responses-ws-http-fallback.test.ts +++ b/tests/responses-ws-http-fallback.test.ts @@ -42,7 +42,7 @@ async function until(condition: () => boolean): Promise { } } -async function fixture() { +async function fixture(warmupEncoding?: (frame: Item) => string, splitWarmup = false) { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ws-httpfb-")); _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); @@ -91,12 +91,28 @@ async function fixture() { } const id = `resp_http_${++serial}`; res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); - const send = (type: string, fields: Item): void => { res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...fields })}\n\n`); }; + const warmupChunks: Buffer[] = []; + const send = (type: string, fields: Item): void => { + const frame = { type, ...fields }; + const encoded = parsed.generate === false && warmupEncoding ? warmupEncoding(frame) : `event: ${type}\ndata: ${JSON.stringify(frame)}\n\n`; + if (parsed.generate === false && splitWarmup) warmupChunks.push(Buffer.from(encoded)); + else res.write(encoded); + }; if (parsed.generate === false) { // No-generation warmup: terminal with empty output, whatever the input. send("response.created", { response: { id, status: "in_progress", output: [] } }); send("response.completed", { response: { id, object: "response", status: "completed", output: [], usage: { input_tokens: 1, output_tokens: 0, total_tokens: 1 } } }); - res.end(); + if (splitWarmup) { + void (async () => { + for (const chunk of warmupChunks) { + for (let index = 0; index < chunk.length; index++) { + res.write(chunk.subarray(index, index + 1)); + await new Promise(resolve => setImmediate(resolve)); + } + } + res.end(); + })(); + } else res.end(); return; } const item: Item = toolArguments !== undefined @@ -153,7 +169,9 @@ async function fixture() { const timer = setTimeout(() => { cleanup(); reject(new Error("WS turn timed out")); }, 15000); const onClose = (): void => { cleanup(); reject(new Error("WS client closed")); }; const onMessage = (raw: WebSocket.RawData): void => { - const event = JSON.parse(raw.toString()) as Item; + let event: Item; + try { event = JSON.parse(raw.toString()) as Item; } + catch (error) { cleanup(); reject(error); return; } events.push(event); if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(event.type))) { cleanup(); resolve(events); } }; @@ -192,6 +210,82 @@ const errorEvent = (events: Item[]): Item => { assert.ok(event, `expected an error frame in ${JSON.stringify(events.map(e => e.type))}`); return event; }; + +const warmupEncodings: Array<{ name: string; encode: (frame: Item) => string }> = [ + { name: "LF control", encode: frame => `data: ${JSON.stringify(frame)}\n\n` }, + { name: "CRLF", encode: frame => `event: ${frame.type}\r\ndata: ${JSON.stringify(frame)}\r\n\r\n` }, + { name: "lone CR", encode: frame => `event: ${frame.type}\rdata: ${JSON.stringify(frame)}\r\r` }, + { name: "no optional data space", encode: frame => `data:${JSON.stringify(frame)}\n\n` }, + { name: "multiple data fields", encode: frame => JSON.stringify(frame, null, 2).split("\n").map(line => `data: ${line}`).join("\n") + "\n\n" }, +]; + +for (const { name, encode } of warmupEncodings) { + test(`warmup SSE ${name} preserves the terminal event and usable checkpoint`, { timeout: 30000 }, async () => { + const fx = await fixture(encode); + try { + const events = await fx.probe(); + const warm = completed(events); + assert.deepEqual(events.map(event => event.type), ["response.created", "response.completed"]); + assert.equal(warm.status, "completed"); + assert.deepEqual(warm.output, []); + assert.equal(fx.lastHttpRequest.generate, false); + assert.deepEqual(fx.lastHttpRequest.input, []); + const continued = completed(await fx.turn([user("follow up after warmup")], { previous_response_id: warm.id })); + assert.equal(continued.status, "completed"); + assert.ok(fx.httpRows.some(row => JSON.stringify(row.request.input).includes("follow up after warmup"))); + assert.ok(fx.upgradesRefused > 0, "real WS refusal exercised HTTP fallback"); + assertAllEgress(fx.httpRows); + } finally { + await fx.close(); + } + }); +} + +for (const status of ["failed", "incomplete"]) { + test(`warmup SSE keeps a multiline ${status} terminal unsuccessful`, { timeout: 30000 }, async () => { + const fx = await fixture(frame => { + const event = frame.type === "response.completed" + ? { ...frame, type: `response.${status}`, response: { ...(frame.response as Item), status } } + : frame; + return JSON.stringify(event, null, 2).split("\n").map(line => `data:${line}`).join("\r\n") + "\r\n\r\n"; + }); + try { + const events = await fx.probe(); + const terminal = events[events.length - 1]!; + assert.equal(terminal.type, `response.${status}`); + assert.equal((terminal.response as Item).status, status); + const continued = await fx.turn([user("retry after failed warmup")], { previous_response_id: (terminal.response as Item).id }); + assert.match(JSON.stringify(errorEvent(continued)), /previous_response_not_found/); + } finally { + await fx.close(); + } + }); +} + +test("warmup SSE does not accept a terminal without its final blank line", { timeout: 30000 }, async () => { + const fx = await fixture(frame => `data: ${JSON.stringify(frame)}\n${frame.type === "response.completed" ? "" : "\n"}`); + try { + const events = await fx.probe(); + assert.equal(events[0]!.type, "response.created"); + assert.match(JSON.stringify(errorEvent(events)), /incomplete_response/); + assert.ok(!events.some(event => event.type === "response.completed")); + } finally { + await fx.close(); + } +}); + +test("warmup SSE keeps split CRLF and UTF-8 bytes intact", { timeout: 30000 }, async () => { + const fx = await fixture(frame => `data:${JSON.stringify({ ...frame, marker: "é中🙂" })}\r\n\r\n`, true); + try { + const events = await fx.probe(); + const warm = completed(events); + assert.deepEqual(events.map(event => event.marker), ["é中🙂", "é中🙂"]); + assert.equal(completed(await fx.turn([user("continue after split bytes")], { previous_response_id: warm.id })).status, "completed"); + assertAllEgress(fx.httpRows); + } finally { + await fx.close(); + } +}); // The HTTP egress must carry the full effective input with no local checkpoint // marker and no WS-only frame field, and force streaming so the SSE sink works. function assertHttpEgressShape(request: Item): void { diff --git a/tests/skipped-proxy-hint.test.ts b/tests/skipped-proxy-hint.test.ts index 2ef26bfbcc..0e56b6c99b 100644 --- a/tests/skipped-proxy-hint.test.ts +++ b/tests/skipped-proxy-hint.test.ts @@ -181,6 +181,61 @@ test("recordUpstreamAlert: structured skipped fields ride along for the localize assert.equal(a.hint, UPSTREAM_FAIL_HINTS["connect-refused"]); }); +for (const mode of ["explicit-direct", "auto", "host-bypass"] as const) { + test(`recordUpstreamAlert: ${mode} replaces an earlier skipped-proxy diagnosis`, () => { + _resetUpstreamAlertsForTest(); + try { + const fallback = { biliPort: 42422, httpsProxy: ENV_PROXY, systemProxy: { enabled: false } }; + const first = resolveProxyDecision({}, "", TARGET, { ...fallback, explicitDirect: true, defaultDirect: true }); + const error = netError("UND_ERR_CONNECT_TIMEOUT", "connect timed out"); + recordUpstreamAlert(TARGET, error, !!first.proxy, first.skippedProxy); + const firstSeen = getUpstreamAlerts()[0].firstSeen; + assert.ok(getUpstreamAlerts()[0].skippedProxy); + + const next = mode === "auto" + ? resolveProxyDecision({}, undefined, TARGET, fallback) + : resolveProxyDecision({}, "", TARGET, { + ...fallback, explicitDirect: true, defaultDirect: mode === "host-bypass", + ...(mode === "host-bypass" ? { noProxy: "relay.example.com" } : {}), + }); + assert.equal(next.skippedProxy, undefined); + assert.equal(!!next.proxy, mode === "auto"); + recordUpstreamAlert(TARGET, error, !!next.proxy, next.skippedProxy); + + const alerts = getUpstreamAlerts(); + assert.equal(alerts.length, 1); + const [current] = alerts; + assert.equal(current.count, 2); + assert.equal(current.firstSeen, firstSeen); + assert.equal(current.code, "UND_ERR_CONNECT_TIMEOUT"); + assert.equal(current.hint, UPSTREAM_FAIL_HINTS["connect-timeout"]); + assert.equal(current.skippedProxy, undefined); + assert.equal(current.skippedSource, undefined); + assert.ok(!JSON.stringify(current).includes("7890")); + } finally { + _resetUpstreamAlertsForTest(); + } + }); +} + +test("recordUpstreamAlert: a later skipped proxy replaces the old redacted endpoint and source", () => { + _resetUpstreamAlertsForTest(); + try { + const error = netError("UND_ERR_CONNECT_TIMEOUT", "connect timed out"); + recordUpstreamAlert(TARGET, error, false, { proxy: ENV_PROXY, source: "HTTPS_PROXY" }); + recordUpstreamAlert(TARGET, error, false, { proxy: "http://new-user:new-password@127.0.0.1:7897", source: "windows-system" }); + const [current] = getUpstreamAlerts(); + assert.equal(current.count, 2); + assert.equal(current.skippedProxy, "http://***:***@127.0.0.1:7897"); + assert.equal(current.skippedSource, "windows-system"); + assert.ok(!JSON.stringify(current).includes("new-password")); + assert.ok(!JSON.stringify(current).includes("7890")); + assert.ok(current.hint.includes(current.skippedProxy)); + } finally { + _resetUpstreamAlertsForTest(); + } +}); + test("loadOptions: defaultDirect tracks UNSET mode only (model path)", () => { // unset mode + available env proxy → the full #2809 scenario end-to-end withSandboxEnv(() => {}, () => { diff --git a/tests/upstream-alerts.test.ts b/tests/upstream-alerts.test.ts index fd261a051a..07516732c6 100644 --- a/tests/upstream-alerts.test.ts +++ b/tests/upstream-alerts.test.ts @@ -612,3 +612,29 @@ test("#1682: banner renders active alerts, dismisses per instance, hides on reco _resetUpstreamAlertsForTest(); } }); + +test("banner drops the skipped-proxy advice when the latest failure did not skip a proxy", async () => { + _resetUpstreamAlertsForTest(); + const url = "https://relay.example.com/v1/messages"; + const error = netError("UND_ERR_CONNECT_TIMEOUT"); + const deadPort = await freePort(); + const { proxy, proxyPort } = await startProxyFor(deadPort, false); + const base = `http://127.0.0.1:${proxyPort}`; + try { + recordUpstreamAlert(url, error, false, { proxy: "http://127.0.0.1:7890", source: "HTTPS_PROXY" }); + const first = await runWebClient(base, "#/overview", new Map()); + await waitFor(() => first.els.get("alerts-banner")?.hidden === false, "initial skipped-proxy banner"); + assert.match(first.els.get("alerts-banner")!.children[1].children[0].textContent, /127\.0\.0\.1:7890/); + + recordUpstreamAlert(url, error, true); + const current = await runWebClient(base, "#/overview", new Map()); + await waitFor(() => current.els.get("alerts-banner")?.hidden === false, "current failure banner"); + const message = current.els.get("alerts-banner")!.children[1].children[0].textContent; + assert.match(message, /relay\.example\.com \(connect-timeout ×2/); + assert.ok(!message.includes("127.0.0.1:7890")); + assert.ok(!message.includes("HTTPS_PROXY")); + } finally { + await close(proxy); + _resetUpstreamAlertsForTest(); + } +}); diff --git a/tests/upstream-timeout-diagnostics.test.ts b/tests/upstream-timeout-diagnostics.test.ts index 550426c3da..849678fc95 100644 --- a/tests/upstream-timeout-diagnostics.test.ts +++ b/tests/upstream-timeout-diagnostics.test.ts @@ -17,13 +17,13 @@ function familyFailure(): TypeError { return new TypeError("fetch failed", { cause: aggregate }); } -test("ETIMEDOUT diagnostics do not claim that the idle watchdog expired", () => { +test("ETIMEDOUT diagnostics identify a proven connect phase", () => { for (const error of [connectError("ETIMEDOUT", "192.0.2.1"), familyFailure()]) { const parts = describeUpstreamFailure(error, "https://chatgpt.com/backend-api/codex/responses"); - assert.equal(parts.detail.kind, "upstream-timeout"); + assert.equal(parts.detail.kind, "connect-timeout"); assert.doesNotMatch(parts.detail.hint!, /idle budget expired/); - assert.match(parts.detail.hint!, /phase/); - assert.match(parts.detail.hint!, /not retried/); + assert.match(parts.detail.hint!, /TCP handshake never completed/); + assert.match(parts.detail.hint!, /bounded transparent replay/); } }); @@ -120,14 +120,21 @@ test("redacted causes remain valid JSON with quoted URLs and backslashes", () => assert.ok(causes[0].message.includes("C:\\tmp\\file")); }); -test("diagnostic improvements preserve conservative timeout classification and retry behavior", async () => { +test("unknown-phase and established-connection timeouts stay conservative and non-replayed", async () => { for (const code of ["UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT"]) { assert.equal(classifyUpstreamFailure(Object.assign(new Error("timeout"), { code })), "upstream-timeout"); } const original = globalThis.fetch; try { for (const send of [fetchWithRetry, fetchWithTransportRetry]) { - for (const error of [connectError("ETIMEDOUT", "127.0.0.1"), familyFailure()]) { + for (const error of [ + Object.assign(new Error("connect ETIMEDOUT"), { code: "ETIMEDOUT" }), + Object.assign(new Error("read ETIMEDOUT"), { code: "ETIMEDOUT", syscall: "read" }), + new TypeError("fetch failed", { cause: Object.assign(new AggregateError([ + connectError("ETIMEDOUT", "127.0.0.1"), + Object.assign(new Error("timeout"), { code: "ETIMEDOUT" }), + ], ""), { code: "ETIMEDOUT" }) }), + ]) { let attempts = 0; globalThis.fetch = async () => { attempts++; throw error; }; await assert.rejects(send("https://chatgpt.com/backend-api/codex/responses", { method: "POST", body: "{}" }, 5000, undefined), (actual) => actual === error); diff --git a/tests/web-blog-refresh.test.ts b/tests/web-blog-refresh.test.ts index 58782a9ef1..dc818916d7 100644 --- a/tests/web-blog-refresh.test.ts +++ b/tests/web-blog-refresh.test.ts @@ -226,6 +226,30 @@ test("raw unreachable → the contents-API chain serves the whole round", async "first-party hosts only: no third-party host was ever contacted"); }); +for (const lineWrapped of [false, true]) { + for (const delta of [-1, 0, 1]) { + test(`API text byte cap counts decoded bytes (${lineWrapped ? "wrapped" : "plain"} base64, delta ${delta})`, async () => { + const prefix = "# Fresh\n\n"; + const text = prefix + "x".repeat(512 * 1024 + delta - prefix.length); + const encoded = Buffer.from(text).toString("base64"); + const content = lineWrapped ? encoded.replace(/.{1,60}/g, "$&\n") : encoded; + router = (url) => { + if (url === CONTENTS_URL) return new Response(JSON.stringify([{ name: "2026-10-09_fresh.md", type: "file" }])); + if (url.startsWith("https://raw.githubusercontent.com/")) throw new Error("raw down"); + if (url === apiFileUrl(`${POSTS_REL}/2026-10-09_fresh.md`)) { + return new Response(JSON.stringify({ content, encoding: "base64" })); + } + return new Response("nf", { status: 404 }); + }; + + await runBlogRefreshRound({ force: true }); + + assert.equal(getBlogFeed().error, delta > 0 ? "offline" : undefined); + assert.equal(getBlogFeed().items.some((item) => item.id === "fresh"), delta <= 0); + }); + } +} + test("raw AND contents-API dead (rate-limited) → the round fails soft with the offline marker", async () => { router = (url) => { if (url === CONTENTS_URL) return new Response(CHAIN_LISTING, { status: 200 }); // listing still reachable @@ -450,6 +474,44 @@ test("an oversize document blows the round budget: fail-soft keeps the last good assert.ok(feed.items.some((i) => i.id === "demo"), "previous good overlay survives"); }); +for (const failure of ["http", "network", "raw-missing", "api-missing", "body"] as const) { + test(`a ${failure} failure fetching the optional manifest preserves the last complete overlay`, async () => { + const manifest = JSON.stringify({ articles: [{ file: "website/articles/kept.md", slug: "kept", title: "Kept article" }] }); + const good = (url: string): Response => { + if (url === CONTENTS_URL) return Response.json([{ name: "2026-10-09_fresh.md", type: "file" }]); + if (url === rawUrl("website/blog/posts/2026-10-09_fresh.md")) return new Response("# Original post\n\noriginal post body\n"); + if (url === rawUrl("website/articles.json")) return new Response(manifest); + if (url === rawUrl("website/articles/kept.md")) return new Response("# Kept article\n\nlast complete article\n"); + return new Response("not here", { status: 404 }); + }; + router = good; + await runBlogRefreshRound({ force: true }); + assert.ok(getBlogFeed().items.some((item) => item.id === "kept")); + + router = (url) => { + if (url === rawUrl("website/blog/posts/2026-10-09_fresh.md")) return new Response("# Partial new post\n\nincomplete round\n"); + if (url === rawUrl("website/articles.json") || url === apiFileUrl("website/articles.json")) { + if (failure === "network") throw new Error("fixture manifest unavailable"); + if (failure === "body") return new Response(new ReadableStream({ start(controller) { controller.error(new Error("fixture truncated body")); } })); + const missing = (failure === "raw-missing" && url === rawUrl("website/articles.json")) + || (failure === "api-missing" && url === apiFileUrl("website/articles.json")); + return new Response("unavailable", { status: missing ? 404 : 503 }); + } + return good(url); + }; + await runBlogRefreshRound({ force: true }); + const failed = getBlogFeed(); + assert.ok(failed.items.some((item) => item.id === "kept" && item.html.includes("last complete article"))); + assert.ok(failed.items.some((item) => item.id === "fresh" && item.html.includes("original post body"))); + assert.ok(!failed.items.some((item) => item.html.includes("incomplete round"))); + assert.equal(failed.error, "offline"); + + router = good; + await runBlogRefreshRound({ force: true }); + assert.equal(getBlogFeed().error, undefined, "a complete later refresh clears the failure marker"); + }); +} + test("malformed articles.json degrades to a posts-only round without failing", async () => { router = (url) => { if (url === CONTENTS_URL) return new Response(JSON.stringify([{ name: "2026-10-09_fresh.md", type: "file" }]), { status: 200 }); @@ -536,6 +598,88 @@ test("#4906 P3 lying-small content-length: the post-read byte check stays the au assert.equal(getBlogFeed().error, "offline", "header said 10 bytes; the real 512KB+1 body still blew the limit"); }); +function chunkedBody(chunkSize: number, chunkCount: number, headers?: Record): { + response: Response; + reads(): number; + cancelled(): boolean; +} { + let reads = 0; + let cancelled = false; + const body = new ReadableStream({ + pull(controller) { + if (reads === chunkCount) { + controller.close(); + return; + } + reads++; + controller.enqueue(Buffer.alloc(chunkSize, "x")); + }, + cancel() { cancelled = true; }, + }, { highWaterMark: 0 }); + return { response: new Response(body, { headers }), reads: () => reads, cancelled: () => cancelled }; +} + +for (const headers of [undefined, { "content-length": "10" }]) { + test(`text download stops at its byte cap with ${headers === undefined ? "no" : "understated"} content-length`, async () => { + installGithubRouter(); + await runBlogRefreshRound({ force: true }); + const body = chunkedBody(64 * 1024, 32, headers); + router = (url) => { + if (url === CONTENTS_URL) return new Response(JSON.stringify([{ name: "2026-10-09_fresh.md", type: "file" }])); + if (url === rawUrl("website/blog/posts/2026-10-09_fresh.md")) return body.response; + return new Response("not here", { status: 404 }); + }; + + await runBlogRefreshRound({ force: true }); + + assert.equal(body.cancelled(), true, "oversized body is canceled before EOF"); + assert.ok(body.reads() <= 10, `expected the cap plus one prefetched chunk, read ${body.reads()}`); + assert.equal(getBlogFeed().error, "offline"); + assert.ok(getBlogFeed().items.some((item) => item.id === "demo"), "the last good overlay survives"); + }); +} + +test("an oversized streamed image is canceled and remains a clean miss", async () => { + installGithubRouter(); + const base = router; + const body = chunkedBody(256 * 1024, 32); + router = (url) => url === rawUrl("website/blog/posts/figures/good.png") ? body.response : base(url); + + await runBlogRefreshRound({ force: true }); + + assert.equal(body.cancelled(), true); + assert.ok(body.reads() <= 10, `expected the cap plus one prefetched chunk, read ${body.reads()}`); + assert.equal(getBlogFeed().error, undefined); + assert.equal(getRemoteBlogAsset("website/blog/posts/figures/good.png"), undefined); + assert.doesNotMatch(getBlogFeed().items.find((item) => item.id === "fresh")?.html ?? "", /good\.png/); +}); + +test("a streamed image exactly at the byte cap is retained", async () => { + installGithubRouter(); + const base = router; + const body = chunkedBody(256 * 1024, 8); + router = (url) => url === rawUrl("website/blog/posts/figures/good.png") ? body.response : base(url); + + await runBlogRefreshRound({ force: true }); + + assert.equal(body.cancelled(), false); + assert.equal(getBlogFeed().error, undefined); + assert.equal(getRemoteBlogAsset("website/blog/posts/figures/good.png")?.buf.byteLength, 2 * 1024 * 1024); +}); + +test("content-length rejection cancels the unread image body", async () => { + installGithubRouter(); + const base = router; + const body = chunkedBody(256 * 1024, 32, { "content-length": String(3 * 1024 * 1024) }); + router = (url) => url === rawUrl("website/blog/posts/figures/good.png") ? body.response : base(url); + + await runBlogRefreshRound({ force: true }); + + assert.equal(body.cancelled(), true); + assert.ok(body.reads() <= 1, "only the fetch wrapper's prefetch may run"); + assert.equal(getBlogFeed().error, undefined); +}); + test("#4906 P3 settleRoundImmune: a rejecting round never leaks an unhandledRejection", async () => { let unhandled: unknown = undefined; const onUn = (e: unknown) => { unhandled = e; }; diff --git a/tests/web-changelog.test.ts b/tests/web-changelog.test.ts index 1d44ec9a94..86b8cea9fd 100644 --- a/tests/web-changelog.test.ts +++ b/tests/web-changelog.test.ts @@ -7,7 +7,9 @@ */ import { test, beforeEach, afterEach } from "node:test"; import assert from "node:assert/strict"; -import type { ServerResponse } from "node:http"; +import { createServer, type ServerResponse } from "node:http"; +import type { AddressInfo } from "node:net"; +import { once } from "node:events"; import { getChangelog, latestChangelog, _resetChangelogForTest } from "../src/web/changelog.ts"; const GH = "https://api.github.com/repos/ranxianglei/billion-context/releases?per_page=20"; @@ -120,6 +122,98 @@ test("HTTP 500 fails soft like a network error", async () => { assert.deepEqual(data, { releases: [], error: "offline" }); }); +for (const status of [429, 503]) { + test(`HTTP ${status} cancels an unread response and retains the last good changelog`, async () => { + globalThis.fetch = fetchStub([ghRelease(0)]); + const good = await getChangelog(); + let cancelled = 0; + const body = new ReadableStream({ + start(controller) { controller.enqueue(new TextEncoder().encode("upstream error")); }, + cancel() { cancelled += 1; }, + }); + globalThis.fetch = (async () => new Response(body, { status })) as typeof fetch; + const failed = await getChangelog({ force: true }); + assert.deepEqual(failed, { releases: good.releases, error: "offline" }); + assert.equal(cancelled, 1, "the rejected HTTP body must not retain its transport"); + }); +} + +test("an unread HTTP error body is closed on the actual connection", async t => { + let closed!: () => void; + const bodyClosed = new Promise(resolve => { closed = resolve; }); + const server = createServer((_req, res) => { + res.on("close", closed); + res.writeHead(503, { "content-type": "text/plain" }); + res.write("temporarily unavailable"); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + t.after(async () => { + server.closeAllConnections(); + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + }); + const local = `http://127.0.0.1:${(server.address() as AddressInfo).port}/releases`; + globalThis.fetch = ((input: string | URL | Request, init?: RequestInit) => { + assert.equal(String(input), GH); + return origFetch(local, init); + }) as typeof fetch; + assert.deepEqual(await getChangelog(), { releases: [], error: "offline" }); + let deadline: ReturnType | undefined; + try { + const ended = await Promise.race([ + bodyClosed.then(() => true), + new Promise(resolve => { deadline = setTimeout(() => resolve(false), 1_000); }), + ]); + assert.equal(ended, true, "rejecting the status must close a body that never ends"); + } finally { + clearTimeout(deadline); + } +}); + +test("a successful changelog consumes its response without cancelling it", async () => { + let cancelled = 0; + const body = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode(JSON.stringify([ghRelease(0)]))); + controller.close(); + }, + cancel() { cancelled += 1; }, + }); + globalThis.fetch = (async () => new Response(body)) as typeof fetch; + assert.equal((await getChangelog()).releases[0]?.tag, "v0.9.100"); + assert.equal(cancelled, 0); +}); + +test("a failed cancellation cannot replace the cached offline result", async () => { + let cancelled = 0; + const body = new ReadableStream({ + cancel() { cancelled += 1; throw new Error("fixture cancellation failure"); }, + }); + globalThis.fetch = (async () => new Response(body, { status: 429 })) as typeof fetch; + assert.deepEqual(await getChangelog(), { releases: [], error: "offline" }); + assert.equal(cancelled, 1); + await new Promise(resolve => setImmediate(resolve)); +}); + +test("releasing an unread tee branch does not wait for its other consumer", async () => { + const source = new ReadableStream({ + start(controller) { controller.enqueue(new TextEncoder().encode("busy")); }, + }); + const [body, other] = source.tee(); + globalThis.fetch = (async () => new Response(body, { status: 503 })) as typeof fetch; + let deadline: ReturnType | undefined; + try { + const result = await Promise.race([ + getChangelog(), + new Promise(resolve => { deadline = setTimeout(() => resolve(undefined), 1_000); }), + ]); + assert.deepEqual(result, { releases: [], error: "offline" }, "cleanup must not turn an HTTP rejection into a wait for a sibling stream"); + } finally { + clearTimeout(deadline); + await other.cancel(); + } +}); + test("non-array / non-object payloads fail soft", async () => { globalThis.fetch = fetchStub({ message: "API rate limit exceeded" }); let data = await getChangelog(); diff --git a/tests/web-diagnostics-enrichment.test.ts b/tests/web-diagnostics-enrichment.test.ts index 6e4fa9f018..030c5a006a 100644 --- a/tests/web-diagnostics-enrichment.test.ts +++ b/tests/web-diagnostics-enrichment.test.ts @@ -4,13 +4,15 @@ // hand. These tests pin the enriched sections AND the publish discipline: the // blob lands in a PUBLIC GitHub issue body, so secrets must be masked and no // absolute path (username leak) may survive into it. -import test from "node:test"; +import test, { type TestContext } from "node:test"; import assert from "node:assert/strict"; -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import fs, { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { syncBuiltinESMExports } from "node:module"; import os from "node:os"; import * as path from "node:path"; import { buildDiagnostics, _candidateLogReadsForTest, _resetCandidateLogReadsForTest } from "../src/web/diagnostics.js"; import { isMaskHostsEnabled, setMaskHostsEnabled } from "../src/log-mask.js"; +import { sealActiveLog } from "../src/log-retention.js"; const ENV_KEYS = ["BILI_CONFIG_FILE", "DSH_HOME", "XDG_STATE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"] as const; @@ -73,6 +75,100 @@ function makeFixture(): { root: string; dshHome: string; stateHome: string; conf return { root, dshHome, stateHome, configPath }; } +function observeLogReads(t: TestContext, beforeOpen?: (file: string) => void): string[] { + const files: string[] = []; + const original = fs.createReadStream; + const hook = t.mock.method(fs, "createReadStream", (...args: Parameters) => { + const file = String(args[0]); + files.push(file); + beforeOpen?.(file); + return original(...args); + }); + syncBuiltinESMExports(); + t.after(() => { + hook.mock.restore(); + syncBuiltinESMExports(); + }); + return files; +} + +function writeLogGenerations(file: string, payloads: string[][]): string[] { + const files: string[] = []; + for (let i = 0; i < payloads.length; i++) { + writeFileSync(file, payloads[i]!.join("\n") + "\n"); + if (i < payloads.length - 1) { + const sealed = sealActiveLog(file, Date.UTC(2026, 10, 1) + i).sealed; + assert.ok(sealed); + files.push(sealed); + } else files.push(file); + } + return files; +} + +for (const topic of ["attribution", "snapshot-shadow"]) { + test(`${topic} diagnostics do not read log evidence absent from their report`, async (t) => { + const fx = makeFixture(); + t.after(() => rmSync(fx.root, { recursive: true, force: true })); + const reads = observeLogReads(t); + const report = await withEnv({ DSH_HOME: fx.dshHome, XDG_STATE_HOME: fx.stateHome, XDG_CONFIG_HOME: fx.root, XDG_DATA_HOME: fx.root, XDG_CACHE_HOME: fx.root, BILI_CONFIG_FILE: fx.configPath }, + () => buildDiagnostics(topic)); + assert.ok(report.blob.includes(topic === "attribution" ? "--- takeover attribution ---" : "--- per-session shadow blobs ---")); + assert.deepEqual(reads, [], "a report without a log section must not open retained log files"); + }); +} + +for (const { topic, marker, cap } of [ + { topic: "storage-legacy-only", marker: "[storage] history-", cap: 12 }, + { topic: "conflict-banner", marker: "codex compact intercepted: history-", cap: 20 }, +]) { + for (const sparseActive of [false, true]) { + test(`${topic} reads only the newest generations needed for its ${cap}-line tail (sparse=${sparseActive})`, async (t) => { + const fx = makeFixture(); + t.after(() => rmSync(fx.root, { recursive: true, force: true })); + const entries = (generation: number, count: number) => Array.from({ length: count }, (_, i) => `${marker}${generation}-${i}`); + const oldest = entries(0, cap + 3); + const middle = entries(1, cap + 3); + const newest = entries(2, sparseActive ? 3 : cap + 3); + const files = writeLogGenerations(path.join(fx.stateHome, "billion-context", "bili.log"), [oldest, middle, newest]); + const reads = observeLogReads(t); + const report = await withEnv({ DSH_HOME: fx.dshHome, XDG_STATE_HOME: fx.stateHome, XDG_CONFIG_HOME: fx.root, XDG_DATA_HOME: fx.root, XDG_CACHE_HOME: fx.root, BILI_CONFIG_FILE: fx.configPath }, + () => buildDiagnostics(topic)); + assert.deepEqual(report.blob.split("\n").filter((line) => line.includes(marker)).map((line) => line.trimStart()), + [...middle, ...newest].slice(-cap), "the bounded tail must retain chronological order across generations"); + assert.deepEqual([...reads].sort(), (sparseActive ? [files[2], files[1]] : [files[2]]).sort(), "older generations cannot add a newer matching line once the tail is full"); + assert.ok(!report.blob.includes("INCOMPLETE:")); + }); + } +} + +test("diagnostic tails retain unreadable-generation warnings while filling from older evidence", async (t) => { + const fx = makeFixture(); + t.after(() => rmSync(fx.root, { recursive: true, force: true })); + const file = path.join(fx.stateHome, "billion-context", "bili.log"); + const marker = "[storage] history-"; + const older = Array.from({ length: 15 }, (_, i) => `${marker}${i}`); + const files = writeLogGenerations(file, [older, ["[storage] rotated before open"]]); + const reads = observeLogReads(t, (opening) => { if (opening === file) rmSync(file); }); + const report = await withEnv({ DSH_HOME: fx.dshHome, XDG_STATE_HOME: fx.stateHome, XDG_CONFIG_HOME: fx.root, XDG_DATA_HOME: fx.root, XDG_CACHE_HOME: fx.root, BILI_CONFIG_FILE: fx.configPath }, + () => buildDiagnostics("storage-legacy-only")); + assert.deepEqual(report.blob.split("\n").filter((line) => line.includes(marker)).map((line) => line.trimStart()), older.slice(-12)); + assert.ok(report.blob.includes("INCOMPLETE: 1 log generation(s) could not be read")); + assert.deepEqual([...reads].sort(), [...files].sort()); +}); + +test("diagnostic tails scan all retained generations when matches do not fill the tail", async (t) => { + const fx = makeFixture(); + t.after(() => rmSync(fx.root, { recursive: true, force: true })); + const marker = "[storage] history-"; + const evidence = [`${marker}oldest`, `${marker}newest`]; + const files = writeLogGenerations(path.join(fx.stateHome, "billion-context", "bili.log"), [[evidence[0]!], ["unrelated middle"], [evidence[1]!]]); + const reads = observeLogReads(t); + const report = await withEnv({ DSH_HOME: fx.dshHome, XDG_STATE_HOME: fx.stateHome, XDG_CONFIG_HOME: fx.root, XDG_DATA_HOME: fx.root, XDG_CACHE_HOME: fx.root, BILI_CONFIG_FILE: fx.configPath }, + () => buildDiagnostics("storage-legacy-only")); + assert.deepEqual(report.blob.split("\n").filter((line) => line.includes(marker)).map((line) => line.trimStart()), evidence); + assert.deepEqual([...reads].sort(), [...files].sort()); +}); + test("#3611 conflict-banner report carries versions, masked config, and evidence-log tail", async (t) => { const fx = makeFixture(); t.after(() => rmSync(fx.root, { recursive: true, force: true })); @@ -175,12 +271,8 @@ test("#3611 oversized config is capped with a truncation note", async (t) => { assert.ok(cfgSection.length < 2200, `config section bounded, got ${cfgSection.length} chars`); }); -// #3813: buildDiagnostics used to read the rotated log pair ONCE PER TAIL, so a -// conflict-banner request (which sets both the storage tail and the conflict -// tail) did two full synchronous reads. The fix pre-fetches the candidate lines -// once and hands the SAME array to both tails — output must stay identical while -// each tail still selects ONLY its own pattern (no cross-leak between the two). -test("#3813 single-read: storage tail and conflict tail share one candidate set without cross-leak", async (t) => { +// #3813: each report must collect its own evidence only once. +test("#3813 single-read: each report collects its own tail without cross-leak", async (t) => { const root = mkdtempSync(path.join(os.tmpdir(), "bili-diag-oneread-")); t.after(() => rmSync(root, { recursive: true, force: true })); const stateHome = path.join(root, "state"); @@ -191,12 +283,12 @@ test("#3813 single-read: storage tail and conflict tail share one candidate set const env = { DSH_HOME: path.join(root, "none"), XDG_STATE_HOME: stateHome, XDG_CONFIG_HOME: root, XDG_DATA_HOME: root, XDG_CACHE_HOME: root }; _resetCandidateLogReadsForTest(); const conflictBlob = (await withEnv(env, () => buildDiagnostics("conflict-banner"))).blob; - assert.equal(_candidateLogReadsForTest(), 1, "conflict-banner reads the rotated pair exactly once (pre-fix: twice)"); + assert.equal(_candidateLogReadsForTest(), 1, "conflict-banner collects evidence exactly once (pre-fix: twice)"); const storageBlob = (await withEnv(env, () => buildDiagnostics("storage-legacy-only"))).blob; - assert.equal(_candidateLogReadsForTest(), 2, "each report costs exactly one full-pair read"); - assert.ok(conflictBlob.includes(conflictMarker), "conflict tail captures its signature line from the shared read"); + assert.equal(_candidateLogReadsForTest(), 2, "each report collects its evidence once"); + assert.ok(conflictBlob.includes(conflictMarker), "conflict tail captures its signature line"); assert.ok(!conflictBlob.includes(storageMarker), "conflict tail must not leak [storage] lines"); - assert.ok(storageBlob.includes(storageMarker), "storage tail captures its [storage] line from the shared read"); + assert.ok(storageBlob.includes(storageMarker), "storage tail captures its [storage] line"); assert.ok(!storageBlob.includes("compression substrate appears destroyed"), "storage tail must not leak conflict signatures"); await withEnv(env, () => buildDiagnostics("totally-unknown-topic")); assert.equal(_candidateLogReadsForTest(), 2, "unknown topics must not touch the log at all"); diff --git a/tests/web-sessions.test.ts b/tests/web-sessions.test.ts index de7f299eb8..e88d9b862f 100644 --- a/tests/web-sessions.test.ts +++ b/tests/web-sessions.test.ts @@ -2,7 +2,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; -import { mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { createInitialState, defaultConfig } from "acp-kernel"; @@ -904,6 +904,56 @@ withSessionsDir("#1937: warm scans decode zero files; detail decodes only its ow assert.equal(afterUnknown.detailDecodes, d2.detailDecodes); }); +for (const tieSavedAt of [false, true]) { + withSessionsDir(`disk summaries select the freshest duplicate (${tieSavedAt ? "mtime tie-break" : "savedAt priority"})`, async (dir) => { + const id = "duplicate-summary"; + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(makeSession(id, { protocol: "openai", title: "Current history" }, { requests: 12, inputTokens: 900 })); + const relative = (readdirSync(dir, { recursive: true }) as string[]).find((file) => file.endsWith(".json"))!; + const currentDir = path.join(dir, "a-current"); + const staleDir = path.join(dir, "z-stale"); + mkdirSync(currentDir); + mkdirSync(staleDir); + const currentPath = path.join(currentDir, path.basename(relative)); + const stalePath = path.join(staleDir, path.basename(relative)); + renameSync(path.join(dir, relative), currentPath); + const current = JSON.parse(readFileSync(currentPath, "utf8")); + const stale = structuredClone(current); + const savedAt = Date.parse("2026-11-01T00:00:00Z"); + current.savedAt = current.payload.savedAt = savedAt; + stale.savedAt = stale.payload.savedAt = tieSavedAt ? savedAt : savedAt - 60_000; + stale.payload.meta.title = "Old history"; + stale.payload.stats.requests = 2; + stale.payload.stats.inputTokens = 50; + writeFileSync(currentPath, JSON.stringify(current)); + writeFileSync(stalePath, JSON.stringify(stale)); + utimesSync(currentPath, savedAt / 1000, savedAt / 1000); + // A later filesystem timestamp must not override a newer savedAt. + const staleMtime = (savedAt + (tieSavedAt ? -30_000 : 30_000)) / 1000; + utimesSync(stalePath, staleMtime, staleMtime); + _resetSessionsForTest(); + _resetDiskCacheForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + + const list = await buildSessionList(); + assert.equal(list.length, 1, "duplicate files represent one session"); + assert.equal(list[0].title, "Current history"); + assert.equal(list[0].requests, 12); + assert.equal(list[0].inputTokens, 900); + assert.equal((await buildOverview()).requests, 12); + const page = await buildSessionPage({ q: "Current history", pageSize: 20 }); + assert.equal(page.total, 1, "search uses the same selected summary"); + assert.equal(page.sessions[0].requests, 12); + const cold = _diskScanStatsForTest(); + await buildSessionList(); + assert.equal(_diskScanStatsForTest().decodedTotal, cold.decodedTotal, "warm duplicate selection needs no extra decoding"); + + const live = getSession(id, { protocol: "openai" }); + live.stats.requests = 30; + assert.equal((await buildSessionList())[0].requests, 30, "live summaries still override disk twins"); + }); +} + withSessionsDir("#1937: corrupt and content-store files never block or poison the endpoints", async (dir) => { const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); await store.writeNow(makeSession("ok-1", { protocol: "openai" }, { requests: 2, inputTokens: 10, cachedTokens: 5, contextTokens: 9 }));