diff --git a/docs/configuration.md b/docs/configuration.md index 6eaf046056..c3e6953e34 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1359,6 +1359,27 @@ A URL-keyed lane (or a named entry with `bind`) can send **its own** credential Compression behaviour is controlled by the `compress` block, which can appear at three levels. They merge **per-field, deepest wins**: a field set at a deeper level overrides the same field higher up, but an *unset* field at a deeper level never clears a value set higher up. In other words, the child covers the parent field-by-field — it never replaces the whole object. +### Per-session opt-out (#5300) + +The merge above is the only *static* scope. For one session whose history must never be folded (e.g. a long-lived companion/roleplay conversation where the original wording itself is the asset), exclude it at runtime — no config key, **exact session id** (deliberately no prefix/label matching: a mis-hit would fail silently, defeating the purpose), and the flag persists in the session record across restarts: + +```bash +# turn compression OFF for one session +curl -s -X POST -H 'content-type: application/json' -d '{"enabled":false}' \ + http://127.0.0.1:/__bili/session//compress + +# read it back — the guard against silent re-enablement (resident sessions +# answer from memory, cold ones from the store; unknown ids 404) +curl -s http://127.0.0.1:/__bili/session//compress +# → {"ok":true,"sessionId":"…","compression":{"enabled":false},"resident":true} + +# re-enable when you are done +curl -s -X POST -H 'content-type: application/json' -d '{"enabled":true}' \ + http://127.0.0.1:/__bili/session//compress +``` + +Semantics while excluded (the same verbatim tail as [`passthrough`](#passthrough), scoped to one session): no fold, no nudge, no tool injection, no preflight, no marker — the client's bytes go out untouched, so the conversation grows unbounded and **window overflow surfaces as the upstream's own error** instead of a quiet fold (that is the point: an explicit failure you can see beats irreversible silent loss). Already-folded blocks stay folded (`decompress` still restores them), and plugin-lane hosts calling the compress tool get a loud refusal (`409 COMPRESSION_DISABLED`) instead of a silent no-op. Every other session on the same model keeps compressing normally. Loopback-only like every `/__bili/` route. + ### Shared external summary service The optional `compress.externalSummary` block sends compression summaries to one or more separately configured models. It lives at all three `compress` levels like every other field, with whole-chain semantics: a chain set at a deeper level (provider or model) **replaces** the entire chain above it — there is no per-target or per-budget sub-merge, exactly like `tiers`. It is disabled unless `enabled` is `true`; when enabled, targets are tried in order and a failed or unusable target falls through to the next one. The main request provider, model, and authorization are never reused for these calls. This feature changes the compression tool contract so `summary` is an optional non-authoritative hint; the proxy keeps the original messages recoverable and commits only a validated returned summary. Because the conversation model no longer writes the committed summary, its injected compress prompt drops the summary-writing rules (`HOW TO COMPRESS`, `KEEP VERBATIM`, `DROP`, `PRIORITY`) and the per-summary length budget while the chain is enabled; the summary requests themselves still carry the full rules (#4460). diff --git a/docs/configuration.zh-CN.md b/docs/configuration.zh-CN.md index 6209c06540..e1d263b77e 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -1356,6 +1356,26 @@ URL 键通道(或带 `bind` 的命名条目)可以向上游发送**自己的 压缩行为由 `compress` 块控制,它可以出现在三个层级。它们按**逐字段、最深层胜出**的方式合并:在更深层设置的字段会覆盖上层同名字段,但更深层*未设置*的字段**永远不会**清除上层已设置的值。换言之,子级按字段覆盖父级 —— 它绝不是整体替换对象。 +### 按会话排除压缩(#5300) + +上面的三级合并是唯一的**静态**作用域。如果某个会话的历史绝不能被折叠(例如长期陪伴 / 角色扮演会话,原始措辞本身就是资产),可以在运行时把它排除——没有配置键,**精确匹配会话 id**(刻意不做前缀 / 标签匹配:误命中会静默失效,正好毁掉这个功能的目的),开关存进会话记录、跨重启生效: + +```bash +# 对单个会话关闭压缩 +curl -s -X POST -H 'content-type: application/json' -d '{"enabled":false}' \ + http://127.0.0.1:/__bili/session//compress + +# 读回——防「静默恢复压缩」的保险(驻留会话从内存回答,冷会话从存储回答,未知 id 返回 404) +curl -s http://127.0.0.1:/__bili/session//compress +# → {"ok":true,"sessionId":"…","compression":{"enabled":false},"resident":true} + +# 用完后重新开启 +curl -s -X POST -H 'content-type: application/json' -d '{"enabled":true}' \ + http://127.0.0.1:/__bili/session//compress +``` + +排除期间的语义(与 [`passthrough`](#passthrough) 同一条 verbatim 转发尾,但只作用于一个会话):不折叠、不 nudge、不注入工具、不 preflight、不打 marker——客户端的字节原样发出,会话因此无限增长,**窗口溢出会以上游自己的错误形式显式冒出来**,而不是被悄悄折掉(这正是设计目的:看得见的显式失败好过不可逆的静默丢失)。已折叠的块保持折叠(`decompress` 仍能恢复),插件车道宿主调用 compress 工具会得到响亮的拒绝(`409 COMPRESSION_DISABLED`),而不是静默空转。同一模型上的其余会话照常压缩。与所有 `/__bili/` 路由一样仅限 loopback。 + ### 共享外部摘要服务 可选的 `compress.externalSummary` 会把压缩摘要交给一个或多个独立配置的模型。它与其余 `compress` 字段一样存在于全部三个层级,采用整链替换语义:在更深层级(provider 或 model)设置的链会**整体替换**上层链,不做按目标或按预算的子字段合并,与 `tiers` 完全一致。只有 `enabled` 为 `true` 时才启用;启用后按顺序调用目标,目标失败或返回不可用摘要时继续使用下一个目标。摘要请求不会复用主请求的 provider、模型或认证信息。启用该功能后,压缩工具中的 `summary` 变为可选的、非权威提示;代理仍保留原文可恢复,只提交通过校验的外部摘要。由于会话模型不再撰写最终摘要,链启用期间注入的压缩系统提示会去掉摘要撰写规则(`HOW TO COMPRESS`、`KEEP VERBATIM`、`DROP`、`PRIORITY`)和单条摘要长度预算;摘要请求本身仍携带完整规则(#4460)。 diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 9b8f6b4d9c..63ac2b91eb 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -62,7 +62,7 @@ import { createHash } from "node:crypto"; import { Readable } from "node:stream"; import { anthropicToCore, coreToAnthropic, type BiliMessage } from "acp-kernel/wire"; import { getStore } from "./persist.js"; -import { peekSession } from "./session.js"; +import { isCompressionDisabled, peekSession, snapshotMessages } from "./session.js"; import { pluginAgentHeader, pluginReportedModel } from "./plugin.js"; import { headerValue, readBody } from "./server.js"; import { featureEnabled } from "./rollout.js"; @@ -1034,6 +1034,16 @@ export async function handleIncrementalTurn( (res as { writeHead?: unknown }).writeHead = originalWriteHead as unknown as never; }; log("info", `[incremental] turn ${conversationId} since=${String(sinceSeq)} applied=${String(appliedSeq)} agent=${agent} model=${String(model)} → forward ${path}`); + // #5300: the pipeline gate forwards excluded sessions verbatim, which + // skips prepare-anthropic — the one place rawTail durability gets + // armed. Arm it here from the lane's authoritative core view so the + // post-forward scheduleSave ingests THIS turn; without it the canonical + // table would miss excluded turns and a cold rebuild would 409-resync + // against history the client already pruned past its acked seq. + const liveSession = peekSession(sessionId); + if (liveSession && isCompressionDisabled(liveSession)) { + snapshotMessages(liveSession, entry.msgs); + } // Best-effort durability close-out: flush the session once the // response has fully left so the canonical table catches up with the // ack before the client can act on the next turn. diff --git a/src/plugin.ts b/src/plugin.ts index 45cf4e2e38..7466393485 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -8,7 +8,7 @@ import path from "node:path"; import { createHash } from "node:crypto"; import { CORRUPT_SIBLING_CAP, getStore, pruneCorruptSiblings, storageStatusLabel, kvStore } from "./persist.js"; import { cloneStoreForRefs } from "./store.js"; -import { acquireInFlight, createSession, getSessionAsync, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, noteAbortedTurn, noteTruncatedUpstream, peekSession, releaseInFlight, releaseNudgeHold, statusInputBaseline, withSessionLock, type Session } from "./session.js"; +import { acquireInFlight, createSession, getSessionAsync, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, isCompressionDisabled, listSessions, markCompactionBoundary, markDirty, noteAbortedTurn, noteTruncatedUpstream, peekSession, releaseInFlight, releaseNudgeHold, statusInputBaseline, withSessionLock, type Session } from "./session.js"; import { clientConversationHeader } from "./session-id.js"; import { buildContextView } from "./context-view.js"; import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, blockOpsTools, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; @@ -2422,6 +2422,14 @@ export async function handlePluginTool( })); return; } + // #5300: an excluded session's pipeline never folds, so a compress call can + // only fail — refuse it loudly instead of letting the model believe a fold + // happened. decompress stays available (restoring a pre-exclusion block is + // still meaningful while the session record retains it). + if (tool === "compress" && isCompressionDisabled(session)) { + deps.log("info", `[${session.id}] [plugin] compress refused: compression is disabled for this session (#5300)`); + return forkReply(res, 409, { ok: false, code: "COMPRESSION_DISABLED", error: `compression is disabled for session "${session.id}" (#5300 per-session opt-out) — nothing was folded. Re-enable it (POST /__bili/session/${encodeURIComponent(session.id)}/compress {"enabled":true}) or continue without folding.` }); + } // Absorb/rules enablement is per-session (last resolved config), so the // gate needs the session — it runs after the lookup above. if (!isProxyToolFor(tool, session, deps.config)) { diff --git a/src/server/admin.ts b/src/server/admin.ts index dd3684131b..265dcd28e4 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -28,7 +28,7 @@ import { dismissHostPlugin, visibleHostPluginNotices } from "../host-plugin-noti import { dismissUpgradeNotice } from "../pck-notice.js"; import { mergedNotices, localDismissKey, localDismissMode, markInstanceDismissal } from "../announcements.js"; import { remoteAnnouncementPayloads, remoteDismissKey, remoteDismissMode } from "../remote-notices.js"; -import { listSessions, memoryReport, peekSession, splitSessionWarnings, totalInFlight } from "../session.js"; +import { isCompressionDisabled, listSessions, markDirty, memoryReport, peekSession, splitSessionWarnings, totalInFlight } from "../session.js"; import { BILI_TUNNEL_HEADER } from "../tunnel-guard.js"; import { isLoopbackAddress } from "../util.js"; import { clearUpstreamAlertsForHost, getUpstreamAlerts, getUpstreamHealthReport } from "../upstream-alerts.js"; @@ -199,6 +199,28 @@ function auxProxyShot(opts: ProxyOptions): { proxy: string | null; source: strin } } +// #5300: parse /__bili/session//compress → decoded id, or undefined when +// the shape doesn't match (method is judged by the caller; query strings are +// tolerated and stripped). Exact-match discipline: ids never contain "/", +// overlong or malformed encodings are rejected rather than guessed. +const SESSION_COMPRESS_PREFIX = "/__bili/session/"; +const SESSION_COMPRESS_SUFFIX = "/compress"; +function sessionCompressIdFromUrl(url: string | undefined): string | undefined { + if (!url || !url.startsWith(SESSION_COMPRESS_PREFIX)) return undefined; + const rest = url.slice(SESSION_COMPRESS_PREFIX.length); + const q = rest.indexOf("?"); + const pathPart = q === -1 ? rest : rest.slice(0, q); + if (!pathPart.endsWith(SESSION_COMPRESS_SUFFIX) || pathPart.length <= SESSION_COMPRESS_SUFFIX.length) return undefined; + let id: string; + try { + id = decodeURIComponent(pathPart.slice(0, pathPart.length - SESSION_COMPRESS_SUFFIX.length)); + } catch { + return undefined; + } + if (id.length === 0 || id.length > 512 || id.includes("/")) return undefined; + return id; +} + export async function handleAdminRoute(req: http.IncomingMessage, res: http.ServerResponse, ctx: AdminCtx): Promise { const { opts, core, config, log, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid, watcherLastSeen } = ctx; // SECURITY: the /__bili/ management endpoints (config read/write, reload, @@ -695,6 +717,62 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv return; } } + // #5300: per-session compression opt-out — the scope between the + // global/provider/model compress merge and all-or-nothing route + // passthrough. Flag lives in session.metadata (persisted with the record, + // survives restarts); EXACT id match only — a silent prefix/label mis-hit + // would defeat the feature's purpose (no silent folding). Excluded + // sessions take the pipeline's verbatim-forward tail (handle.ts): no fold, + // no nudge, no injection, no preflight, no marker; window overflow surfaces + // as an explicit upstream error instead of a quiet fold. Loopback-gated + // like every /__bili/ route. + const sessionCompressId = req.method === "GET" || req.method === "POST" ? sessionCompressIdFromUrl(req.url) : undefined; + if (req.method === "GET" && sessionCompressId !== undefined) { + const id = sessionCompressId; + const resident = peekSession(id); + if (resident) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, sessionId: id, compression: { enabled: !isCompressionDisabled(resident) }, resident: true })); + return; + } + const loaded = await getStore().loadAsync(id); + if (!loaded) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: `unknown session "${id}" — no such record in this proxy instance` })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, sessionId: id, compression: { enabled: !isCompressionDisabled(loaded) }, resident: false })); + return; + } + if (req.method === "POST" && sessionCompressId !== undefined) { + const id = sessionCompressId; + try { + const parsed = JSON.parse((await readBody(req)).toString("utf8")) as { enabled?: unknown }; + if (typeof parsed.enabled !== "boolean") { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: 'expected { "enabled": true|false }' })); + return; + } + const session = peekSession(id); + if (!session) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: `unknown session "${id}" — not resident in this proxy instance; a model request under its id re-binds it` })); + return; + } + if (parsed.enabled) delete session.metadata.compressionDisabled; + else session.metadata.compressionDisabled = true; + markDirty(session); + log("info", `[admin] POST /__bili/session/${id}/compress — compression ${parsed.enabled ? "re-enabled" : "disabled"} (#5300)`); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, sessionId: id, compression: { enabled: parsed.enabled } })); + return; + } catch (err) { + res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: String(err) })); + return; + } + } // #2671 Phase 2: replica/checkpoint management over the unified store. // Loopback-gated like every /__bili/ route; 501 in legacy mode so the // operator knows the knob, not the URL, is what is missing. diff --git a/src/server/handle.ts b/src/server/handle.ts index 00122ada6a..f8b86af9bc 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -20,7 +20,7 @@ import { hostIdForLog, maskHeadersForLog, maskUrlForLog, maskUrlsInText } from " import { bodyMightCarryImages, buildIncomingImageIndex, pruneRetrieveImgExports, refreshIncomingImageIndex, type IndexedImage } from "../image-restore.js"; import { durableMessageToolPaths, withDurableProtection } from "../durable-message-guards.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, declaresMcpBiliToolsOnWire, evaluateSelfHealRound, pluginLaneDegraded, pluginLaneRestore } from "../session-self-heal.js"; -import { acquireInFlight, getSessionAsync, hasProcessedState, markDirty, peekSession, releaseInFlight, storeEffectiveConfig, tickPostRebuildAnchor, withSessionLock, type Session } from "../session.js"; +import { acquireInFlight, getSessionAsync, hasProcessedState, isCompressionDisabled, markDirty, peekSession, releaseInFlight, storeEffectiveConfig, tickPostRebuildAnchor, withSessionLock, type Session } from "../session.js"; import { buildCompressSystemPrompt } from "../compress-tool.js"; import { storeEffectiveSearchPlanAware } from "../decompress-shared.js"; import { externalSummaryEnabled, messageTimestampSections } from "../external-summary-surface.js"; @@ -1770,6 +1770,22 @@ async function handleRequest( // be able to reload from the DB instead of starting a fresh session. const session = await getSessionAsync(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? `prefix-affinity ${anonAffinity.tailHash.slice(0, 8)}` : undefined) }); memProbe(opts.debug, log, sessionId, "inbound-parsed", bodyBuffer.length); + // #5300: per-session compression opt-out (exact session id, runtime-set + // via POST /__bili/session//compress). The request rides the SAME + // verbatim tail as global/route passthrough: no prepare (no fold-ledger + // update, no nudge/tool/philosophy injection), no preflight fold, no + // relay-loop rounds — the client's bytes go out untouched and window + // overflow surfaces as the upstream's own error. Placed before ANY + // bookkeeping below on purpose: an excluded session's record stays + // byte-stable except for the flag itself. The incremental lane pre-arms + // its canonical durability before dispatching its synthetic request + // (src/incremental-lane.ts), so skipping prepare* loses nothing there. + if (isCompressionDisabled(session)) { + log("info", `[session-compress-off] ${sessionId}: compression disabled for this session (#5300) — forwarding verbatim, kernel bypassed (re-enable: POST /__bili/session/${sessionId}/compress {"enabled":true})`); + forwarded = true; + await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, reqConfig, log, route, instanceId, undefined, undefined, undefined, throttleModel); + return; + } // #5490: the first-sight warn above needs no session identity; this one // does — recurrence across DISTINCT sessions is what turns "a model I // haven't declared" into "an alias this host resolves internally that diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..c8fb078acb 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1738,6 +1738,16 @@ export function findSessionByCanonicalId(canonicalId: string): Session | undefin return undefined; } +/** #5300: per-session compression opt-out — exact session id, runtime-set via + * POST /__bili/session//compress, read back via GET on the same path. + * While true the pipeline forwards that session's model requests verbatim + * (the passthrough tail): no fold, no nudge, no tool/philosophy injection, + * no preflight; explicit compress tool calls are refused loudly. Prospective + * only — spans folded before exclusion stay folded (decompress restores). */ +export function isCompressionDisabled(session: Session): boolean { + return session.metadata.compressionDisabled === true; +} + /** Overwrite the session's full-conversation snapshot with the latest client * raw request messages (originalMessages from prepare*). One array per * session, replaced every request — bounded, always the newest state. Empty diff --git a/tests/issue5300-session-compress-optout.test.ts b/tests/issue5300-session-compress-optout.test.ts new file mode 100644 index 0000000000..4c8ed57445 --- /dev/null +++ b/tests/issue5300-session-compress-optout.test.ts @@ -0,0 +1,326 @@ +// #5300: per-session compression opt-out — POST/GET /__bili/session//compress +// gates ONE session onto the pipeline's verbatim-forward tail (no fold, no +// nudge, no injection, no preflight, no marker) while every other session on +// the same model keeps compressing. Exact id match only; the flag lives in +// session.metadata so it survives restarts. Both directions asserted (#3617): +// disable AND re-enable, plus the plugin-lane compress-tool refusal. + +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import type { AddressInfo } from "node:net"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import * as path from "node:path"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +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 { peekSession, _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +interface Captured { + url: string; + body: string; +} + +interface Harness { + proxyPort: number; + upstreamPort: number; + captured: Captured[]; + store: SessionStore; + storeEnabled: boolean; + close(): Promise; +} + +async function startHarness(storeDir?: string): Promise { + const captured: Captured[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ url: req.url ?? "", body: Buffer.concat(chunks).toString("utf8") }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl_5300", + object: "chat.completion", + created: 1, + model: "gpt-test", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 5, completion_tokens: 1, total_tokens: 6 }, + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as AddressInfo).port; + // Real file-backed persistence only for the restart-simulation test below. + const storeEnabled = storeDir !== undefined; + const store = storeEnabled + ? new SessionStore({ dir: storeDir, enabled: true, debounceMs: 0 }) + : new SessionStore({ enabled: false }); + _setStoreForTest(store); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + promptCache: { routing: "auto" }, + compress: { injectTool: true, injectNudge: false }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + proxyPort: (proxy.address() as AddressInfo).port, + upstreamPort, + captured, + store, + storeEnabled, + async close() { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + if (storeEnabled) store.closeQuietly(); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + }, + }; +} + +function baseUrl(h: Harness): string { + return `http://127.0.0.1:${h.proxyPort}`; +} + +async function modelRequest(h: Harness, sessionId: string, extraHeaders: Record = {}): Promise { + const body = JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: "hi" }] }); + const resp = await fetch(`${baseUrl(h)}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId, ...extraHeaders }, + body, + }); + await resp.text(); + return resp.status; +} + +async function injectedToolNames(h: Harness, index: number): Promise { + const sent = JSON.parse(h.captured[index].body) as { tools?: Array<{ function?: { name?: string } }> }; + return (sent.tools ?? []).map((t) => t.function?.name).filter((n): n is string => typeof n === "string"); +} + +async function setCompression(h: Harness, id: string, enabled: boolean): Promise<{ status: number; json: { ok: boolean; compression?: { enabled: boolean }; error?: string } }> { + const resp = await fetch(`${baseUrl(h)}/__bili/session/${encodeURIComponent(id)}/compress`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ enabled }), + }); + return { status: resp.status, json: (await resp.json()) as never }; +} + +async function getCompression(h: Harness, id: string): Promise<{ status: number; json: { ok: boolean; compression?: { enabled: boolean }; resident?: boolean; error?: string } }> { + const resp = await fetch(`${baseUrl(h)}/__bili/session/${encodeURIComponent(id)}/compress`); + return { status: resp.status, json: (await resp.json()) as never }; +} + +test("#5300: baseline — a non-excluded session rides the pipeline (compress tool injected)", async () => { + const h = await startHarness(); + try { + assert.equal(await modelRequest(h, "sess-5300-base"), 200); + assert.equal(h.captured.length, 1); + assert.ok((await injectedToolNames(h, 0)).includes("compress"), "unexcluded request rides the pipeline (ACP surface injected)"); + } finally { + await h.close(); + } +}); + +test("#5300: disable — the excluded session forwards byte-identical, read-back reports disabled", async () => { + const h = await startHarness(); + try { + assert.equal(await modelRequest(h, "sess-5300-excl"), 200); + assert.ok((await injectedToolNames(h, 0)).includes("compress"), "pre-disable: pipeline active"); + + const off = await setCompression(h, "sess-5300-excl", false); + assert.equal(off.status, 200); + assert.deepEqual(off.json.compression, { enabled: false }); + + const body = JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: "hi" }] }); + const resp = await fetch(`${baseUrl(h)}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "sess-5300-excl" }, + body, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(h.captured.length, 2); + // the point of the feature: NOTHING is rewritten — no injection, no + // re-serialization, no marker; the bytes out equal the bytes in + assert.equal(h.captured[1].body, body); + + const got = await getCompression(h, "sess-5300-excl"); + assert.equal(got.status, 200); + assert.deepEqual(got.json.compression, { enabled: false }); + assert.equal(got.json.resident, true); + + // sibling session on the same model is untouched + assert.equal(await modelRequest(h, "sess-5300-sibling"), 200); + assert.ok((await injectedToolNames(h, 2)).includes("compress"), "sibling session keeps compressing"); + } finally { + await h.close(); + } +}); + +test("#5300: re-enable — injection returns for the same session (cycle closes both ways)", async () => { + const h = await startHarness(); + try { + assert.equal(await modelRequest(h, "sess-5300-cycle"), 200); + assert.equal((await setCompression(h, "sess-5300-cycle", false)).status, 200); + const on = await setCompression(h, "sess-5300-cycle", true); + assert.equal(on.status, 200); + assert.deepEqual(on.json.compression, { enabled: true }); + + assert.equal(await modelRequest(h, "sess-5300-cycle"), 200); + assert.ok((await injectedToolNames(h, 1)).includes("compress"), "re-enabled session rides the pipeline again"); + + const got = await getCompression(h, "sess-5300-cycle"); + assert.deepEqual(got.json.compression, { enabled: true }); + } finally { + await h.close(); + } +}); + +test("#5300: unknown session ids — POST and GET both 404 (exact-match discipline)", async () => { + const h = await startHarness(); + try { + const post = await setCompression(h, "sess-5300-ghost", false); + assert.equal(post.status, 404); + assert.equal(post.json.ok, false); + const get = await getCompression(h, "sess-5300-ghost"); + assert.equal(get.status, 404); + assert.equal(get.json.ok, false); + } finally { + await h.close(); + } +}); + +test("#5300: malformed bodies rejected with 400, state untouched", async () => { + const h = await startHarness(); + try { + assert.equal(await modelRequest(h, "sess-5300-malformed"), 200); + const bad = await fetch(`${baseUrl(h)}/__bili/session/sess-5300-malformed/compress`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ enabled: "nope" }), + }); + assert.equal(bad.status, 400); + (await bad.text()); + const notJson = await fetch(`${baseUrl(h)}/__bili/session/sess-5300-malformed/compress`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: "not-json-at-all", + }); + assert.equal(notJson.status, 400); + (await notJson.text()); + // the failed writes must not have flipped the flag + const got = await getCompression(h, "sess-5300-malformed"); + assert.deepEqual(got.json.compression, { enabled: true }); + } finally { + await h.close(); + } +}); + +test("#5300: plugin lane — compress tool call refused loudly on an excluded session, not on the control", async () => { + const h = await startHarness(); + try { + // bind two plugin-lane sessions (dsh shape: x-bili-plugin + conversation header) + assert.equal(await modelRequest(h, "conv-5300-excl", { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-5300-excl" }), 200); + assert.equal(await modelRequest(h, "conv-5300-ctl", { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-5300-ctl" }), 200); + + assert.equal((await setCompression(h, "conv-5300-excl", false)).status, 200); + + const refused = await fetch(`${baseUrl(h)}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: "conv-5300-excl", tool: "compress" }), + }); + assert.equal(refused.status, 409); + const rj = (await refused.json()) as { ok: boolean; code?: string; error?: string }; + assert.equal(rj.ok, false); + assert.equal(rj.code, "COMPRESSION_DISABLED"); + assert.ok(typeof rj.error === "string" && rj.error.length > 0, "refusal carries an actionable error message"); + + // negative row: the control session's compress call is NOT our refusal + const control = await fetch(`${baseUrl(h)}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: "conv-5300-ctl", tool: "compress" }), + }); + await control.text(); + assert.notEqual(control.status, 409); + } finally { + await h.close(); + } +}); + +test("#5300: the flag survives a restart — persisted with the record, re-armed from disk", async () => { + const dir = mkdtempSync(path.join(tmpdir(), "bili-5300-persist-")); + try { + // Phase 1 (live instance): bind the session, disable compression, pin it to disk. + _resetSessionsForTest(); + const h1 = await startHarness(dir); + try { + assert.equal(await modelRequest(h1, "sess-5300-restart"), 200); + assert.equal((await setCompression(h1, "sess-5300-restart", false)).status, 200); + const live = peekSession("sess-5300-restart"); + assert.ok(live, "session resident after its first model request"); + await h1.store.writeNow(live); + const got1 = await getCompression(h1, "sess-5300-restart"); + assert.deepEqual(got1.json.compression, { enabled: false }); + assert.equal(got1.json.resident, true); + } finally { + await h1.close(); + } + + // Phase 2 (process-death stand-in): empty resident pool, fresh store over the same dir. + // The flag must come back THROUGH THE FILE — a serialization change that thins + // metadata would silently re-enable compression and break both assertions below. + _resetSessionsForTest(); + const h2 = await startHarness(dir); + try { + const got2 = await getCompression(h2, "sess-5300-restart"); + assert.equal(got2.status, 200); + assert.deepEqual(got2.json.compression, { enabled: false }); + const body = JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: "hi" }] }); + const resp = await fetch(`${baseUrl(h2)}/bili/http://127.0.0.1:${h2.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "sess-5300-restart" }, + body, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(h2.captured[0].body, body, "restarted instance still forwards the excluded session verbatim"); + } finally { + await h2.close(); + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } +});