diff --git a/docs/clients.md b/docs/clients.md index 2529dbfde6..b71a23fe7b 100644 --- a/docs/clients.md +++ b/docs/clients.md @@ -1017,6 +1017,27 @@ healthy, requests go direct (uncompressed) with a one-time warning and recover automatically. Under a `bili opencode` launch this entry is skipped entirely (the launcher owns the proxy). +### Built-in Claude Agent provider (2.x) + +OpenCode 2.x ships a built-in **Claude Agent** provider (`claude-agent/*`) that drives a local Claude Code CLI. That child calls Anthropic directly — it makes no request *from* OpenCode — so the plugin's HTTP-hook routing never sees its traffic and its sessions would otherwise run uncompressed. On a 2.x host, `bili plugin install opencode` routes it by writing `providers.claude-agent.settings.baseURL` in your opencode config, pinned to the live opencode lane port: + +```json +{ + "providers": { + "claude-agent": { + "settings": { + "baseURL": "http://127.0.0.1:/bili/https://api.anthropic.com" + } + } + } +} +``` + +- **Existing installs** — re-run `bili plugin install opencode`; the entry is written at install time, so a pre-existing config is not retro-routed. +- **Port drift** — if the opencode lane port changes, the pinned URL goes stale; `bili doctor` warns on the opencode lane and a re-run re-pins it. +- **Your own proxy** — a non-bili `baseURL` is left untouched (it's yours, not ours to clobber). +- **Known limitation** — compression works, but the `mcp__opencode__*` bili tools still have no session state in these sessions: the proxy keys the session by the Claude Code child's id, not OpenCode's `ses_…` id. + ### OpenAI Responses WebSockets (V2) The V2 plugin also intercepts OpenAI `experimental.ws.handshake` requests. diff --git a/docs/clients.zh-CN.md b/docs/clients.zh-CN.md index 96cd01e225..109c1049d8 100644 --- a/docs/clients.zh-CN.md +++ b/docs/clients.zh-CN.md @@ -316,6 +316,27 @@ HTTPS 走证书 MITM,HTTP 走临时 `opencode.json` 副本(`/bili/` 改写;JSONC 加载时插件自拉起自己的代理(健康的已有实例直接复用不重复起;父进程 pid 看门狗在 opencode 退出时收掉它),把模型流量路由到 `/bili/`,暴露与启动器模式相同的原生 bili 工具 —— 无固定端口、无环境变量、免启动器。退出:`BILI_NATIVE_OPENCODE=0`。若没有任何代理能拉到健康状态,请求直连(不压缩)并给一次性告警,之后自动恢复。在 `bili opencode` 启动下该条目整体跳过(代理归启动器管)。 +### 内置 Claude Agent provider(2.x) + +OpenCode 2.x 内置了一个 **Claude Agent** provider(`claude-agent/*`),它驱动一个本地 Claude Code CLI。这个子进程直连 Anthropic——不从 OpenCode 发请求——所以插件的 HTTP-hook 路由永远看不到它的流量,其会话否则会不压缩地运行。在 2.x 主机上,`bili plugin install opencode` 通过在你的 opencode 配置里写入 `providers.claude-agent.settings.baseURL`(钉在 opencode 泳道的实时端口)来路由它: + +```json +{ + "providers": { + "claude-agent": { + "settings": { + "baseURL": "http://127.0.0.1:/bili/https://api.anthropic.com" + } + } + } +} +``` + +- **已有安装** —— 重新运行 `bili plugin install opencode`;该条目在安装时写入,所以已有配置不会被追溯路由。 +- **端口漂移** —— 如果 opencode 泳道端口变化,钉死的 URL 会失效;`bili doctor` 会在 opencode 泳道告警,重新运行会重新钉死。 +- **你自己的代理** —— 非 bili 的 `baseURL` 会被保留不动(那是你的,不是我们要覆盖的)。 +- **已知限制** —— 压缩可用,但这些会话里 `mcp__opencode__*` bili 工具仍然没有会话状态:代理以 Claude Code 子进程的 id 为键,而非 OpenCode 的 `ses_…` id。 + ### OpenAI Responses WebSocket(V2) V2 插件也接管 OpenAI 的 `experimental.ws.handshake` 请求,两端均使用 diff --git a/docs/message-identity.ja.md b/docs/message-identity.ja.md index f1615cb17f..8083e1d5b1 100644 --- a/docs/message-identity.ja.md +++ b/docs/message-identity.ja.md @@ -247,7 +247,9 @@ id・ハッシュ・形状はいずれもヒント;「私のブロックはど - **Epoch-confirm ゲート**:全面崩壊は `foldEpochConfirmPending` を latch し、 エラーを 1 回出し、preflight はリクエストを逐字通過させる —— folding は*一 時的停止*;何も削除されず、何もサイレントにリセットされない。整列回復(ホ - ストが真の履歴を再送)で latch は自動クリア。 + ストが真の履歴を再送)で latch は自動クリア。この一時停止は逐字ペイロー + ドがモデルウィンドウに収まる間だけ有効:ウィンドウ超過のペイロードは通 + 過できないため、preflight は通常どおり fold する(#6069)。 - **Unresolved-anchor 配置**(kernel `prune.ts`):アンカーがすべて解決失敗し たブロックは、tail ではなく最後の user メッセージに挿入される —— サマリー ブロックがユーザーの最新質問の *後* に乗ることがなくなった(#3640 の diff --git a/docs/message-identity.md b/docs/message-identity.md index 65c291dc61..6cec7928f8 100644 --- a/docs/message-identity.md +++ b/docs/message-identity.md @@ -255,7 +255,10 @@ rule; proof tests in `tests/fold-anchor-doctrine.test.ts`): - **Epoch-confirm gate**: total collapse latches `foldEpochConfirmPending`, emits one error, and preflight passes requests through verbatim — folding *pauses*; nothing is deleted, nothing is silently reset. Alignment - recovery (the host resends the true history) auto-clears the latch. + recovery (the host resends the true history) auto-clears the latch. The + pause holds only while the verbatim payload fits the model window: an + over-window payload cannot pass through, so preflight folds it as usual + (#6069). - **Unresolved-anchor placement** (kernel `prune.ts`): a block whose anchors all failed to resolve now inserts at the last user message instead of the tail — a summary block can no longer ride *after* the user's newest diff --git a/docs/message-identity.zh-CN.md b/docs/message-identity.zh-CN.md index 1fbf0a01d8..76b6bdc5f0 100644 --- a/docs/message-identity.zh-CN.md +++ b/docs/message-identity.zh-CN.md @@ -81,7 +81,7 @@ bili 的应对遵循上文的失败模式公理:**不猜** old→new 配对(猜 - **形状层**(保序):当一次性缺失**成片** covered id(≥ `SHAPE_MASS_MIN`)时,残留散文锚按会话顺序与入站候选配对,受编辑诚实门约束——词袋相等 + simhash ≤ 8 + role + 长度卫兵(sim 层用于 gap 的同一道门,用在 claim 上)。无文本锚(图片族:构造上无 sh/wh)在两侧被已验证的侧翼包夹、且逐桶计数恰相等时按第 k 对第 k 配对——byImage 纪律推广到 bili 自家占位变体之外。 - **残差普查与判决分类**:阶梯认不下的部分被*分类*,而非假定已毁。`shapeExplained`(变换:claim 或近重证据承担了主体)——一次性 info latch,清零 streak。`truncationExplained`(良性删除:残差全部到处缺席、零近重、且**wire 连续性见证**——冻结的旧序尾部仍居于入站头部)——块休眠而非 reap(#2695)。其余照旧是 drift,照旧升级。 - **episode 见证人**:首个 drift pass 冻结旧序尾部(`foldEpisodeOrder`);同会话 id 下的外来会话与它无连续性,整体替换无法冒充截断。 -- **epoch 确认门**:全塌 latch `foldEpochConfirmPending`,发一条 error,preflight 原样透传——折叠*暂停*;不删任何东西,不静默重置。对齐恢复(宿主重发真实历史)自动清 latch。 +- **epoch 确认门**:全塌 latch `foldEpochConfirmPending`,发一条 error,preflight 原样透传——折叠*暂停*;不删任何东西,不静默重置。对齐恢复(宿主重发真实历史)自动清 latch。暂停只在原样载荷放得进模型窗口时成立:超窗载荷无法透传,preflight 照常折叠(#6069)。 - **未解析锚摆位**(内核 `prune.ts`):锚全部解析失败的块,插入点从尾部改为最后一个 user 消息——摘要块再也不会骑在用户最新提问*之后*(#3640 的六个骑尾块)。 诚实边界是有意的:形状层要求**严格**词袋相等,所以系统性内容突变——比如宿主给历史批量加/删尾部 `` 后缀——**不会**被认成同一条消息(模糊内容 join 在 decompress 时认错;见下文被否决备选)。这类事件被分类为截断或全塌,落在休眠/暂停路径上——永不静默重键。批量同后缀重戳(N 对保序对共享完全相同的 delta)是否值得单独立一个*可验证*类,是待生产日志定界的开放跟进项(#3640/#3588)。 diff --git a/src/agent/dsh-compaction-disable.ts b/src/agent/dsh-compaction-disable.ts index 4970e3d159..9836bf598e 100644 --- a/src/agent/dsh-compaction-disable.ts +++ b/src/agent/dsh-compaction-disable.ts @@ -132,6 +132,17 @@ export function _resetDshCompactionGateTelemetryForTest(): void { delete (globalThis as Record)[DSH_COMPACTION_GATE_TELEMETRY_KEY]; } +/** #6770: one-line coverage summary shared by the durable profile notes (#3962) + * and bili-native's first-request log line. Pure over the record; "no entry + * rewritten" is the honest frame because preset configs resolve lazily. */ +export function compactionCoverageText(telemetry: DshCompactionGateTelemetry | null = dshCompactionGateTelemetry()): string { + if (telemetry === null || telemetry.covered.length === 0) { + return "no preset entry rewritten in this process (preset configs resolve lazily — zero at this point does not mean zero later)"; + } + const entries = telemetry.covered.map((c) => `${c.entry}\u00d7${c.rows}`).join(", "); + return `${telemetry.covered.length} preset ${telemetry.covered.length === 1 ? "entry" : "entries"} rewritten (${entries})`; +} + type PresetRow = { name?: unknown; group?: unknown; config?: unknown }; /** Count compaction-basic/command-compact rows the disable walk would mark diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5d83069fb5..8654a7b2b7 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -70,7 +70,7 @@ import { installNativeWebSocketIntercept } from "./native-ws-intercept.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportAttributionHealth, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js"; import { createForkAdopter, sideShapedBody } from "./fork-adopt.js"; import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; -import { declareDshCompactionActive, detectCompactionOverlayGap, dshCompactionGateTelemetry } from "./dsh-compaction-disable.js"; +import { compactionCoverageText, declareDshCompactionActive, detectCompactionOverlayGap } from "./dsh-compaction-disable.js"; import { maskUrlForDiagnostics } from "../log-mask.js"; import { mediaBytesOfWireBody } from "./media-bytes.js"; import { conversationRecallStats, learnConversation, recallBoundaryOfMessages, recallConversation, recallHistoryFingerprints, recallLaneHasEntries, recallLaneKey } from "./conversation-recall.js"; @@ -294,12 +294,7 @@ export { DSH_BUILD_CHECK } from "./client-build-note.js"; * the gate rows' fate implied. Best-effort by necessity: preset configs * resolve lazily, so "so far" is the honest frame. */ function gateCoverageSuffix(): string { - const t = dshCompactionGateTelemetry(); - if (t === null || t.covered.length === 0) { - return " Overlay coverage so far: no preset entry rewritten in this process (preset configs resolve lazily — zero at this point does not mean zero later)."; - } - const entries = t.covered.map((c) => `${c.entry}\u00d7${c.rows}`).join(", "); - return ` Overlay coverage so far: ${t.covered.length} preset ${t.covered.length === 1 ? "entry" : "entries"} rewritten (${entries}).`; + return ` Overlay coverage so far: ${compactionCoverageText()}.`; } /** #1590/#1809: origin of the proxy as currently reachable — register.base @@ -1256,6 +1251,12 @@ interface LlmDispatchScope { const dispatchScope = new AsyncLocalStorage(); let noticedScopedAttribution = false; +// #6770: the #3962 coverage readout only ever rode the profile-bundle note, +// which stays silent wherever profileContext.startedBundles cannot be read +// (observed on both a desktop dsh build and the dsh-go runtime), so the +// coverage reading had no reachable exit. Emit it once per process on the +// first routed request instead. +let compactionCoverageNoted = false; /** #4491: extract the host message ids from llm/stream `options.messages`, * all-or-nothing. Mirrors validateMsgIds in src/msgid-pin.ts (the 400-class @@ -1340,6 +1341,15 @@ function attributionOf(ctx: PluginContext): GateAttribution { // driving), and it survives exactly the shapes the ambient reading does // not (agentless compaction/title lanes, disposed scopes, retried // re-sends whose initiator scope went away between attempts). + // #6770: first routed request is the earliest point where the overlay's + // structural rewrites can have happened (preset configs resolve lazily), + // so the coverage reading goes out here — the durable profile note that + // used to carry it never fires when profileContext.startedBundles cannot + // be read, which is the observed state on every host we checked. + if (!compactionCoverageNoted) { + compactionCoverageNoted = true; + persistClientEvent(`bili-native-dsh: compaction overlay coverage — ${compactionCoverageText()} (#6770)`); + } const scoped = dispatchScope.getStore(); if (scoped !== undefined && scoped.sid.length > 0) { if (!noticedScopedAttribution) { @@ -3119,6 +3129,12 @@ export function _resetDshNewerBuildNoteForTest(): void { dshNewerBuildNoted = false; } +/** Test hook (#6770): reset the once-per-process compaction-coverage readout + * flag so a test can pin the emit wiring, not just the formatter. */ +export function _resetCompactionCoverageNoteForTest(): void { + compactionCoverageNoted = false; +} + /** #2924 direction 2: a `threw` attribution (host agent scope disposed * mid-request) gets a bounded settle-retry before the gate decides. The * defaults cap the added latency of a dying turn at ~(attempts-1)×delayMs; diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 66022f7288..eba718e16b 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -22,11 +22,11 @@ // (BILLION_CONTEXT_PLUGIN=0 / BILI_NATIVE_OMP=0). import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; -import { createBiliPlugin } from "./pi.js"; +import { createBiliPlugin, createOmpDirectSendGuard } from "./pi.js"; import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js"; import { loadDeclaredModelEndpoints } from "../model-endpoints.js"; -import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import * as pfChannel from "./pf-channel.js"; import { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; @@ -214,8 +214,10 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | state.beforeSend = ompIncrementalBeforeSend; // #3304: the omp swap stamps the turn id atomically with the body, so the -// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. -state.prepareDirectSend = createPrepareDirectSend(); +// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. A +// direct send also drops the prompt_cache_key the plugin stamped (no proxy +// strips it on that path). +state.prepareDirectSend = createOmpDirectSendGuard(); export function ompIncrementalOnResponse(_url: string, response: Response, headers?: Record): void { const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 93ea2a9e20..1c6763d6e6 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -17,7 +17,7 @@ import { disposeSubagentSelfReg, selfRegisterForSession, type SubagentSelfRegSta import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents, piCommandsEnabled } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; -import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; import { laneMsgIdsForOpenAiWire, laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; import * as pfChannel from "./pf-channel.js"; import { validateMsgIds } from "../msgid-pin.js"; @@ -553,9 +553,41 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re if (typeof p.prompt_cache_key === "string" && p.prompt_cache_key.trim().length > 0) return undefined; const sid = sessionIdOf(ctx); if (sid === undefined || sid.length === 0) return undefined; + rememberStampedPromptCacheKey(sid); return { ...p, prompt_cache_key: sid }; } +// Values this process stamped as prompt_cache_key. The proxy strips the field +// before it forwards; a request that degrades to a direct send never reaches +// the proxy, so the omp direct-send guard drops the stamp itself — only a +// value recorded here, never a key the host set. Bounded, oldest first out. +const STAMPED_PROMPT_CACHE_KEYS_MAX = 1024; +const stampedPromptCacheKeys = new Set(); + +function rememberStampedPromptCacheKey(sid: string): void { + stampedPromptCacheKeys.delete(sid); + stampedPromptCacheKeys.add(sid); + if (stampedPromptCacheKeys.size > STAMPED_PROMPT_CACHE_KEYS_MAX) { + const oldest = stampedPromptCacheKeys.values().next().value; + if (oldest !== undefined) stampedPromptCacheKeys.delete(oldest); + } +} + +/** The JSON body without the prompt_cache_key this process stamped, or + * undefined when the body carries no such stamp (unchanged, send as-is). */ +function withoutStampedPromptCacheKey(text: string): string | undefined { + let parsed: unknown; + try { + parsed = JSON.parse(text); + } catch { + return undefined; + } + if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return undefined; + const { prompt_cache_key: key, ...rest } = parsed as Record; + if (typeof key !== "string" || !stampedPromptCacheKeys.has(key)) return undefined; + return JSON.stringify(rest); +} + function fmtTok(n: number): string { if (n < 1000) return String(n); if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`; @@ -933,6 +965,28 @@ export function createPiDirectSendGuard(): (url: string, input: string | URL | R let nudgeDebugWired = false; const nudgeNotifiedTurns = new Map(); let nudgeFullPending: NudgeDebugEvent | null = null; +/** #3304 prepareDirectSend for the omp lane: the shared incremental guard + * (restore a swapped body or refuse), then drop the prompt_cache_key the + * plugin stamped — on a direct send no proxy strips it, and strict upstreams + * reject the unknown field with a 400. */ +export function createOmpDirectSendGuard(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { + const shared = createPrepareDirectSend(); + return async (url, input, init) => { + const prep = await shared(url, input, init); + if (prep.outcome === "unrestorable") return prep; + let text: string | undefined = prep.outcome === "restored" ? prep.body : undefined; + if (text === undefined) { + try { + if (typeof init?.body === "string") text = init.body; + else if (input instanceof Request) text = await input.clone().text(); + } catch { + text = undefined; // unreadable body — send as the shared guard decided + } + } + const cleaned = text === undefined ? undefined : withoutStampedPromptCacheKey(text); + return cleaned === undefined ? prep : { outcome: "restored", body: cleaned }; + }; +} /** The pi-lane terminal notification for an injected nudge: a fixed label * prefix + the VERBATIM wire text the model read — nothing appended (strict diff --git a/src/doctor.ts b/src/doctor.ts index c23cae3c9e..b34d4bc7b5 100644 --- a/src/doctor.ts +++ b/src/doctor.ts @@ -9,7 +9,7 @@ import fs from "node:fs"; import path from "node:path"; import { isPidAlive, listInstances, procMainScript } from "./instance.js"; -import { PLUGIN_AGENTS, UPDATE_CHANNEL, claudeDesktopPresent, inspectLanePresence, type PluginAgent } from "./plugin-install.js"; +import { PLUGIN_AGENTS, UPDATE_CHANNEL, claudeDesktopPresent, detectOpencodeMajor, inspectLanePresence, inspectOpencodeClaudeAgent, isBiliClaudeBaseUrl, opencodeClaudeAgentBaseUrl, type PluginAgent } from "./plugin-install.js"; import { findInstallDir, fetchRegistryVersion, hostManagedInstall, isGitWorkingTree, isVersionNewer, lastUpdateCheckTime, normalizeUpdateTag, staleInstallStatus } from "./update.js"; import { describeAdvisory, evaluateAdvisories, type AdvisoryEvaluation } from "./advisory.js"; import { claudeRoutingWarningFile } from "./paths.js"; @@ -314,6 +314,24 @@ function laneRowsFor(agent: PluginAgent, presence: ReturnType 0) row.warnings = warnings; } + if (agent === "opencode" && presence.installed) { + // #6816: the built-in Claude Agent provider (OpenCode 2.x) makes no + // HTTP request, so the plugin's HTTP-hook routing never sees it — an + // installed plugin with an unrouted provider is silently inactive. + // Advisory only: never change the verdict. The 1.x host has no such + // provider, so the check is gated on the host major. + if (detectOpencodeMajor() >= 2) { + const live = opencodeClaudeAgentBaseUrl(); + const { baseUrl } = inspectOpencodeClaudeAgent(); + if (baseUrl === undefined) { + row.warnings = [...(row.warnings ?? []), "claude-agent provider is not routed through bili: OpenCode 2.x ships a built-in Claude Agent provider (claude-agent/*) whose requests never reach the proxy, so its sessions run uncompressed and the mcp__opencode__* tools have no session state — rerun 'bili plugin install opencode' to route it"]; + } else if (!isBiliClaudeBaseUrl(baseUrl)) { + row.warnings = [...(row.warnings ?? []), `claude-agent provider baseURL is not a bili URL (${baseUrl}) — its sessions bypass bili entirely; rerun 'bili plugin install opencode' to route it, or leave it if intentional`]; + } else if (baseUrl !== live) { + row.warnings = [...(row.warnings ?? []), `claude-agent provider baseURL points at a stale port (${baseUrl}) — the live opencode lane is ${live}; rerun 'bili plugin install opencode' to re-pin it`]; + } + } + } return [row]; } diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 81bcea8277..d9657f0f98 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -746,6 +746,63 @@ export function wordBagDigest(text: string): number { return acc; } +/** Word evidence of the reconcile candidates, derived once per candidate. + * The shape pass and the residual census weigh every unclaimed anchor + * against the candidates; canonicalTextOf + wordBagDigest + simhashHex are + * pure functions of a candidate's text, so recomputing them per (anchor, + * candidate) pair made both passes O(anchors × candidates) full-text + * tokenizations — a resumed session with every fold inactive (9.6k stored + * ids against 1.3k inbound messages) held the event loop past the 300 s + * watchdog. Buckets keep candidate order, so a scan of the bucket finds the + * same first match a scan of every candidate would. */ +function candidateWordEvidence(candidates: readonly C[]) { + const canon = new WeakMap(); + const bags = new WeakMap(); + const sketches = new WeakMap(); + const canonicalOf = (m: CoreMessage): string => { + let text = canon.get(m); + if (text === undefined) canon.set(m, (text = canonicalTextOf(m.text ?? ""))); + return text; + }; + const wordBag = (m: CoreMessage): number => { + let wh = bags.get(m); + if (wh === undefined) bags.set(m, (wh = wordBagDigest(canonicalOf(m)))); + return wh; + }; + const sketch = (m: CoreMessage): string => { + let sh = sketches.get(m); + if (sh === undefined) sketches.set(m, (sh = simhashHex(canonicalOf(m)))); + return sh; + }; + const bucketBy = (keyOf: (c: C) => K | undefined): ((key: K) => readonly C[]) => { + let buckets: Map | undefined; + return (key) => { + if (buckets === undefined) { + buckets = new Map(); + for (const c of candidates) { + const k = keyOf(c); + if (k === undefined) continue; + const bucket = buckets.get(k); + if (bucket === undefined) buckets.set(k, [c]); + else bucket.push(c); + } + } + return buckets.get(key) ?? []; + }; + }; + const byWordBag = bucketBy((c) => wordBag(c.message)); + // looseProse's user branch: role user, text content, identity after newline→space unwrap. + const byUnwrappedIdentity = bucketBy((c) => (c.message.role === "user" && c.message.contentType === "text" + ? normalizedIdentity({ ...c.message, text: (c.message.text ?? "").replace(/\r?\n/g, " ") }) + : undefined)); + return { + wordBag, + sketch, + withWordBag: (wh: number): readonly C[] => byWordBag(wh), + withUnwrappedIdentity: (n: string | undefined): readonly C[] => (n === undefined ? [] : byUnwrappedIdentity(n)), + }; +} + function wordHash(w: string): number { let h = 0x811c9dc5; for (let i = 0; i < w.length; i++) { @@ -973,6 +1030,7 @@ export function planReconciliation( candidates.push({ id, message, pos: i }); } const claimedCandidates = new Set(); + const evidence = candidateWordEvidence(candidates); // Missing covered ids inside the churn region, in previous-pass order. const missingMiddle: string[] = []; @@ -1464,18 +1522,21 @@ export function planReconciliation( return normalizedIdentity(unwrapped) === anchor.n; } if (anchor.wh === undefined || anchor.sh === undefined) return false; - const wh = wordBagDigest(canonicalTextOf(m.text ?? "")); - if (wh !== anchor.wh) return false; - const dist = hammingHex(anchor.sh, simhashHex(canonicalTextOf(m.text ?? ""))); + if (evidence.wordBag(m) !== anchor.wh) return false; + const dist = hammingHex(anchor.sh, evidence.sketch(m)); return dist >= 0 && dist <= GAP_SIMHASH_MAX_HAMMING; }; // prose pairing — order-preserving: walk anchors in conversation // order, take the first unclaimed loose candidate (also in order), // so pairs never cross when both sides are locally order-stable. + // Only candidates that can pass looseProse are scanned (same word bag, + // or same unwrapped identity for user text), kept in candidate order, + // so the first match is the one a full scan would find. for (const oldId of residualMiddle) { const anchor = anchors[oldId]; if (anchor === undefined || anchor.sh === undefined || anchor.wh === undefined) continue; - const cand = candidates.find((c) => !claimedCandidates.has(c.id) && looseProse(anchor, c.message)); + const pool = anchor.r === "user" ? evidence.withUnwrappedIdentity(anchor.n) : evidence.withWordBag(anchor.wh); + const cand = pool.find((c) => !claimedCandidates.has(c.id) && looseProse(anchor, c.message)); if (cand === undefined) continue; plan.claims.set(oldId, [cand.id]); claimedCandidates.add(cand.id); @@ -1575,14 +1636,12 @@ export function planReconciliation( }); for (const id of classCarrying) { const anchor = anchors[id]!; - const loose = candidates.some((c) => + // Word evidence needs an equal bag, so only that bucket can match. + const sh = anchor.sh; + const loose = anchor.wh !== undefined && sh !== undefined && evidence.withWordBag(anchor.wh).some((c) => !claimedCandidates.has(c.id) && (anchor.r === undefined || anchor.r === c.message.role) - && ( - (anchor.wh !== undefined && anchor.sh !== undefined - && wordBagDigest(canonicalTextOf(c.message.text ?? "")) === anchor.wh - && (() => { const d = hammingHex(anchor.sh!, simhashHex(canonicalTextOf(c.message.text ?? ""))); return d >= 0 && d <= GAP_SIMHASH_MAX_HAMMING; })()) - )); + && (() => { const d = hammingHex(sh, evidence.sketch(c.message)); return d >= 0 && d <= GAP_SIMHASH_MAX_HAMMING; })()); // textless anchors (no sh/wh — image cells, #3588) carry NO // evidence channel: claimable families were already claimed by // the flanked k-th discipline above, so a textless residual is diff --git a/src/persist.ts b/src/persist.ts index 2746ad49f4..cbc0059ea2 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -202,6 +202,8 @@ interface PersistedSession { cacheSamples?: number; lastInputTokens?: number; lastInputTokensSource?: string; + emergencyShrinkAt?: number; + emergencyShrinkEstimate?: number; lastInputTokensOrigin?: string; overflowArmTokens?: number; overflowArmCompletionReserve?: number; @@ -222,7 +224,8 @@ interface PersistedSession { lastUsageGradeTokens?: number; calibratedEstimate?: number; calibratedEstimateOrigin?: string; - calibrationRing?: { origin: string; values: number[] }; + calibratedEstimateModel?: string; + calibrationRing?: { origin: string; model?: string; values: number[] }; pendingFoldUsage?: boolean; lastLocalTextEstimate?: number; lastLocalTextEstimateOrigin?: string; @@ -4198,14 +4201,15 @@ function restoreCalibratedEstimate(v: unknown): number | undefined { // itself would admit (finite, within the plausibility band) and only the newest // window-size entries; anything else drops the ring wholesale so a malformed // file can neither teach a factor nor mask a fresh learning run on restart. -function restoreCalibrationRing(v: unknown): { origin: string; values: number[] } | undefined { +function restoreCalibrationRing(v: unknown): { origin: string; model?: string; values: number[] } | undefined { if (!v || typeof v !== "object") return undefined; - const ring = v as { origin?: unknown; values?: unknown }; + const ring = v as { origin?: unknown; model?: unknown; values?: unknown }; if (typeof ring.origin !== "string" || ring.origin.length === 0) return undefined; if (!Array.isArray(ring.values)) return undefined; const values = ring.values.filter((x): x is number => typeof x === "number" && Number.isFinite(x) && x >= CALIBRATION_SAMPLE_MIN && x <= CALIBRATION_SAMPLE_MAX); const recent = values.slice(-CALIBRATION_SAMPLE_WINDOW); - return recent.length > 0 ? { origin: ring.origin, values: recent } : undefined; + const model = typeof ring.model === "string" && ring.model.trim().length > 0 ? ring.model : undefined; + return recent.length > 0 ? { origin: ring.origin, ...(model === undefined ? {} : { model }), values: recent } : undefined; } function restoreBlockContents(entries: Iterable): Map { @@ -4266,6 +4270,10 @@ function buildSession(parsed: PersistedSession): Session { // #857: provenance — legacy files lack it; absent stays absent and // evidence-grade consumers treat absent as untrusted. lastInputTokensSource: stats.lastInputTokensSource === "usage" || stats.lastInputTokensSource === "estimate" || stats.lastInputTokensSource === "overflow-arm" ? stats.lastInputTokensSource : undefined, + // Preserve the explanation saved by armFailureShrink alongside its + // raised baseline. A real usage settlement clears both markers. + emergencyShrinkAt: typeof stats.emergencyShrinkAt === "number" && Number.isFinite(stats.emergencyShrinkAt) && stats.emergencyShrinkAt >= 0 ? stats.emergencyShrinkAt : undefined, + emergencyShrinkEstimate: typeof stats.emergencyShrinkEstimate === "number" && Number.isFinite(stats.emergencyShrinkEstimate) && stats.emergencyShrinkEstimate > 0 ? stats.emergencyShrinkEstimate : undefined, // #1110: one-shot overflow arm — legacy files lack it; absent = no arm. overflowArmTokens: typeof stats.overflowArmTokens === "number" && Number.isFinite(stats.overflowArmTokens) && stats.overflowArmTokens > 0 ? stats.overflowArmTokens : undefined, // #3450: proven completion reservation + one-time warn flag — legacy files lack both. @@ -4291,6 +4299,7 @@ function buildSession(parsed: PersistedSession): Session { lastInputTokensOrigin: typeof stats.lastInputTokensOrigin === "string" && stats.lastInputTokensOrigin.trim().length > 0 ? stats.lastInputTokensOrigin : undefined, calibratedEstimate: restoreCalibratedEstimate(stats.calibratedEstimate), calibratedEstimateOrigin: typeof stats.calibratedEstimateOrigin === "string" && stats.calibratedEstimateOrigin.trim().length > 0 ? stats.calibratedEstimateOrigin : undefined, + calibratedEstimateModel: typeof stats.calibratedEstimateModel === "string" && stats.calibratedEstimateModel.trim().length > 0 ? stats.calibratedEstimateModel : undefined, calibrationRing: restoreCalibrationRing(stats.calibrationRing), // Intentionally NOT restored (#2129 triage): pendingFoldUsage is set at // compress execution and consumed only as a log-suffix attribution by the diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 3e46c60eed..735e7c556e 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -1461,6 +1461,106 @@ export function applyOpencodePluginEntry(args: { data: Record; return [`${key} -> ${shimDir} (replaced ${replaced.join(", ")})`, note]; } +// #6816: OpenCode 2.x ships a built-in Claude Agent provider (claude-agent/*) +// that drives a local Claude Code binary and makes NO HTTP request from +// OpenCode — the plugin's HTTP-hook routing never sees its traffic, so the +// provider silently runs uncompressed while `bili doctor` stays green. The +// fix routes it by pointing it at the proxy in the host config (the +// reporter's verified workaround), pinned to the LIVE lane port — the port +// drifts (#5127/#5129), so it is resolved per install, never hardcoded. +const OPENCODE_CLAUDE_AGENT_PROVIDER = "claude-agent"; + +/** #6816: the proxy origin a claude-agent baseURL must point at. */ +export function opencodeClaudeAgentBaseUrl(): string { + return `http://127.0.0.1:${lanePreferredPort("opencode")}/bili/https://api.anthropic.com`; +} + +/** #6816: the claude-agent baseURL currently in the opencode config, if any. + * Fail-open: an unreadable or malformed config reads as "not configured" + * (the install path refuses to overwrite such a file anyway, §7.3). */ +export function inspectOpencodeClaudeAgent(): { file: string; baseUrl: string | undefined } { + const file = opencodeTargetFile(); + let baseUrl: string | undefined; + try { + const { data } = loadOpencodeConfig(file); + const providers = data.providers; + if (isPlainMcpObject(providers)) { + const entry = providers[OPENCODE_CLAUDE_AGENT_PROVIDER]; + if (isPlainMcpObject(entry)) { + const settings = entry.settings; + if (isPlainMcpObject(settings) && typeof settings.baseURL === "string") baseUrl = settings.baseURL; + } + } + } catch { + baseUrl = undefined; + } + return { file, baseUrl }; +} + +// #6816: route the built-in Claude Agent provider at the live lane port. +// Merge-preserving: foreign providers and existing claude-agent settings +// survive; a foreign baseURL is the user's explicit choice and is never +// clobbered (§7.3). 1.x hosts have no such provider — skip there. +function applyOpencodeClaudeAgentEntry(data: Record, touched: Set, notes: string[]): void { + if (detectOpencodeMajor() < 2) { + notes.push("claude-agent baseURL skipped (OpenCode 1.x has no built-in Claude Agent provider)"); + return; + } + const url = opencodeClaudeAgentBaseUrl(); + const providers = data.providers; + if (providers !== undefined && !isPlainMcpObject(providers)) { + notes.push('claude-agent baseURL skipped ("providers" is not an object)'); + return; + } + const existing = isPlainMcpObject(providers) ? providers[OPENCODE_CLAUDE_AGENT_PROVIDER] : undefined; + if (existing !== undefined && !isPlainMcpObject(existing)) { + notes.push('claude-agent baseURL skipped ("providers.claude-agent" is not an object)'); + return; + } + const settings = isPlainMcpObject(existing) ? existing.settings : undefined; + if (settings !== undefined && !isPlainMcpObject(settings)) { + notes.push('claude-agent baseURL skipped ("providers.claude-agent.settings" is not an object)'); + return; + } + const current = isPlainMcpObject(settings) ? settings.baseURL : undefined; + if (typeof current === "string" && !isBiliClaudeBaseUrl(current)) { + notes.push(`claude-agent baseURL untouched (foreign: ${current})`); + return; + } + if (current === url) { + notes.push("claude-agent baseURL present"); + return; + } + const nextProviders = isPlainMcpObject(providers) ? providers : {}; + const nextEntry = isPlainMcpObject(existing) ? existing : {}; + const nextSettings = isPlainMcpObject(settings) ? settings : {}; + nextSettings.baseURL = url; + nextEntry.settings = nextSettings; + nextProviders[OPENCODE_CLAUDE_AGENT_PROVIDER] = nextEntry; + data.providers = nextProviders; + touched.add("providers"); + notes.push(current === undefined ? `claude-agent baseURL -> ${url}` : `claude-agent baseURL re-pinned ${current} -> ${url}`); +} + +// #6816: take back only a baseURL we could have written — a foreign one is +// the user's and stays. Prune the emptied parents so remove leaves no husk. +function removeOpencodeClaudeAgentEntry(data: Record, touched: Set, notes: string[]): void { + const providers = data.providers; + if (!isPlainMcpObject(providers)) return; + const entry = providers[OPENCODE_CLAUDE_AGENT_PROVIDER]; + if (!isPlainMcpObject(entry)) return; + const settings = entry.settings; + if (!isPlainMcpObject(settings)) return; + const current = settings.baseURL; + if (typeof current !== "string" || !isBiliClaudeBaseUrl(current)) return; + delete settings.baseURL; + if (Object.keys(settings).length === 0) delete entry.settings; + if (Object.keys(entry).length === 0) delete providers[OPENCODE_CLAUDE_AGENT_PROVIDER]; + if (Object.keys(providers).length === 0) delete data.providers; + touched.add("providers"); + notes.push("claude-agent baseURL removed"); +} + function opencodeInstall(withMcp = false): string { const file = opencodeTargetFile(); const { original, data } = loadOpencodeConfig(file); @@ -1549,6 +1649,10 @@ function opencodeInstall(withMcp = false): string { notes.push("compaction.auto set to false"); } + // #6816: the built-in Claude Agent provider bypasses HTTP-hook routing — + // point it at the proxy (live lane port) or leave a foreign baseURL alone. + applyOpencodeClaudeAgentEntry(data, touched, notes); + writeOpencodeConfig(file, original, data, touched); return `opencode: installed -> ${file} (${notes.join("; ")})`; } @@ -1601,6 +1705,9 @@ function opencodeRemove(): string { notes.push(`plugin removed (${removed.join(", ")})`); } + // #6816: take back only a bili baseURL — a foreign one is the user's. + removeOpencodeClaudeAgentEntry(data, touched, notes); + if (notes.length > 0) { const cur = data.compaction as Record | undefined; if (cur && cur.auto === false) { diff --git a/src/preflight.ts b/src/preflight.ts index 5f98e14d3f..44c053354a 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -1742,20 +1742,6 @@ export function deterministicDigest(entries: DigestEntry[], startRef: string, en } export async function preflightCompress(deps: PreflightDeps, messages: CoreMessage[]): Promise { - // #3641: epoch confirmation pending — the fold state and the resent - // history share nothing (wholesale replacement, no word evidence), so any - // fold computed here would summarize a substrate whose anchor set can - // never re-anchor. Pause folding for this session (verbatim passthrough) - // and say so once per pass at debug level: the error line lives at the - // escalation site (fold-reconcile). The FIRST realigned pass clears the - // latch (resetFoldDriftState), resuming folding automatically — no - // restart, no manual reset. Import direction: preflight → fold-reconcile - // is acyclic (fold-reconcile never imports preflight). - if (epochConfirmPending(deps.session)) { - deps.log("debug", `[preflight] fold epoch confirmation pending for ${deps.session.id} — compression paused (verbatim passthrough) until the resent history realigns (#3641)`); - const paused = estimateCoreMessages(messages) + (deps.wireOverhead ?? 0); - return { compressedRanges: 0, savedTokens: 0, payloadEstimate: paused, rangesRemaining: 0, fitsWindow: paused <= deps.config.modelContextLimit, hadFoldableCandidates: false }; - } const limit = deps.config.modelContextLimit; // #1843 dual-channel accounting: fold decisions run on the TEXT channel — // text estimate vs `target − imageReserve`. Algebraically the same firing @@ -1812,6 +1798,26 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // failure stamp below). let fitExitForward = false; if (limit <= 0) return result; + // #3641: epoch confirmation pending — the fold state and the resent + // history share nothing (wholesale replacement, no word evidence), so any + // fold computed here would summarize a substrate whose anchor set can + // never re-anchor. Pause folding for this session (verbatim passthrough) + // and say so once per pass at debug level: the error line lives at the + // escalation site (fold-reconcile). The FIRST realigned pass clears the + // latch (resetFoldDriftState), resuming folding automatically — no + // restart, no manual reset. Import direction: preflight → fold-reconcile + // is acyclic (fold-reconcile never imports preflight). + // #6069: the pause holds only while the verbatim payload fits the window, + // judged on the same scale as the loop below. An over-window payload cannot + // pass through, and the latch clears only after a turn is forwarded, so a + // paused over-window session was refused on every turn. Fold it instead. + if (epochConfirmPending(deps.session)) { + if (result.payloadEstimate + viewDeltaCorrection < limit) { + deps.log("debug", `[preflight] fold epoch confirmation pending for ${deps.session.id} — compression paused (verbatim passthrough) until the resent history realigns (#3641)`); + return result; + } + deps.log("warn", `[preflight] fold epoch confirmation pending for ${deps.session.id}, but the verbatim payload (~${Math.round(result.payloadEstimate + viewDeltaCorrection)} tokens) exceeds the model window ${limit} — folding the resent history instead of refusing the turn (#6069)`); + } if (deps.externalSummary === undefined) { try { // The plan rides the request Config rail: deps.config is this diff --git a/src/server/budget.ts b/src/server/budget.ts index fe1fa22c22..06275265f2 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -4,9 +4,54 @@ import { estimateCoreMessages } from "../preflight.js"; import { SELF_HEAL_SUPPRESS_CAP_MS, reductionKeyOf } from "../session-self-heal.js"; import type { Session } from "../session.js"; import { applyEstimateCalibration } from "../util.js"; -import { outputBudgetField, readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; -export { outputBudgetField, readOutputBudget, writeOutputBudget }; +// #6752: the output-budget field plumbing moved here from side-request.ts — +// it is generic wire plumbing (used by the clamp, the restore path, and the +// watermark fastpath), not side-lane logic, and the prepare modules must stay +// decoupled from the side-lane engine (#1440 P2 cut 2). side-request.ts +// re-exports these, so its existing importers are untouched. +export type OutputBudgetField = "max_tokens" | "max_completion_tokens" | "max_output_tokens" | "generationConfig.maxOutputTokens"; + +/** The declared output budget, proto-agnostically. Gemini nests it under + * `generationConfig` (the dotted field name above), the OpenAI/Anthropic + * families keep it flat, so every reader goes through these two accessors. */ +export function outputBudgetField(parsed: unknown): OutputBudgetField | null { + if (!parsed || typeof parsed !== "object") return null; + const p = parsed as Record; + for (const field of ["max_tokens", "max_completion_tokens", "max_output_tokens"] as const) { + if (typeof p[field] === "number" && (p[field] as number) > 0) return field; + } + const gen = p.generationConfig; + if (gen && typeof gen === "object") { + const v = (gen as Record).maxOutputTokens; + if (typeof v === "number" && v > 0) return "generationConfig.maxOutputTokens"; + } + return null; +} + +export function readOutputBudget(parsed: Record, field: OutputBudgetField): number | undefined { + if (field !== "generationConfig.maxOutputTokens") { + const v = parsed[field]; + return typeof v === "number" ? v : undefined; + } + const gen = parsed.generationConfig; + if (!gen || typeof gen !== "object") return undefined; + const v = (gen as Record).maxOutputTokens; + return typeof v === "number" ? v : undefined; +} + +export function writeOutputBudget(parsed: Record, field: OutputBudgetField, value: number): void { + // #2011: output-budget fields are integer-typed on every wire protocol. Coerce at this + // sole writer so NO caller can emit a fractional token count — the clamp path already + // floors, but this also guards the restore path and any future caller at the type boundary. + const tokens = Number.isInteger(value) ? value : Math.floor(value); + if (field !== "generationConfig.maxOutputTokens") { + parsed[field] = tokens; + return; + } + const gen = parsed.generationConfig; + parsed.generationConfig = { ...(gen && typeof gen === "object" ? (gen as Record) : {}), maxOutputTokens: tokens }; +} // #453 hard backstop: cap the forwarded output budget so input+output can never // exceed the window on request-rebuilding upstreams (vLLM rejects an oversized diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index dd1fe0aad4..b69e92d319 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -43,7 +43,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage, type RawStripCounts } from "../marker/engine.js"; import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; -import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; @@ -141,6 +141,16 @@ export async function prepareOpenai( log("debug", `[${session.id}] fastpath ${wpOutcome.hit ? "HIT" : wpOutcome.decision}${wpOutcome.creditedMiss ? "(credited)" : ""}${wpOutcome.measuredT !== undefined ? ` T=${wpOutcome.measuredT}` : ""}`); if (wpOutcome.hit) { wpHit = wpOutcome.hit; + // #6752: handle.ts already ran restoreOutputBudget on `parsed` — + // the rebind below re-parses scalar fields from the raw wire text, + // which would silently discard the restored budget (the log claims + // the restore while the wire still carries the client's shrunk + // value). Carry the restored budget across the rebind. + const budgetField = outputBudgetField(parsed); + const restoredBudget = + budgetField !== null + ? readOutputBudget(parsed as Record, budgetField) + : undefined; const fields: Record = {}; for (const [key, span] of wpHit.fieldSpans) { // "messages" keeps its WIRE POSITION (the scanner records it @@ -149,6 +159,9 @@ export async function prepareOpenai( if (key === "messages") fields[key] = wpHit.windowMessages; else fields[key] = JSON.parse(bodyText.slice(span.start, span.end)); } + if (budgetField !== null && restoredBudget !== undefined) { + writeOutputBudget(fields, budgetField, restoredBudget); + } parsed = fields as OpenAIRequestBody; inboundMessageCount = wpHit.totalT; } diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 81c5cc8886..83b2c40993 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -6,6 +6,12 @@ import { type ResolvedImageBilling } from "../image-tokens.js"; import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, BLOCK_OP_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../compress-tool.js"; import { dshCarrierRawMarkers } from "../durable-message-guards.js"; +import { outputBudgetField, readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./budget.js"; + +// #6752: the output-budget field plumbing lives in budget.ts now (generic wire +// plumbing, not side-lane logic); re-exported here so existing importers +// (server.ts, tests) keep working unchanged. +export { outputBudgetField, readOutputBudget, writeOutputBudget, type OutputBudgetField }; // #388: side requests (title-gen etc.) share the main session key but must not // touch kernel state. Identified by a tiny output budget (same heuristic as @@ -760,49 +766,6 @@ export function stripLeakedBiliTools(parsed: unknown): boolean { return true; } -export type OutputBudgetField = "max_tokens" | "max_completion_tokens" | "max_output_tokens" | "generationConfig.maxOutputTokens"; - -/** The declared output budget, proto-agnostically. Gemini nests it under - * `generationConfig` (the dotted field name above), the OpenAI/Anthropic - * families keep it flat, so every reader goes through these two accessors. */ -export function outputBudgetField(parsed: unknown): OutputBudgetField | null { - if (!parsed || typeof parsed !== "object") return null; - const p = parsed as Record; - for (const field of ["max_tokens", "max_completion_tokens", "max_output_tokens"] as const) { - if (typeof p[field] === "number" && (p[field] as number) > 0) return field; - } - const gen = p.generationConfig; - if (gen && typeof gen === "object") { - const v = (gen as Record).maxOutputTokens; - if (typeof v === "number" && v > 0) return "generationConfig.maxOutputTokens"; - } - return null; -} - -export function readOutputBudget(parsed: Record, field: OutputBudgetField): number | undefined { - if (field !== "generationConfig.maxOutputTokens") { - const v = parsed[field]; - return typeof v === "number" ? v : undefined; - } - const gen = parsed.generationConfig; - if (!gen || typeof gen !== "object") return undefined; - const v = (gen as Record).maxOutputTokens; - return typeof v === "number" ? v : undefined; -} - -export function writeOutputBudget(parsed: Record, field: OutputBudgetField, value: number): void { - // #2011: output-budget fields are integer-typed on every wire protocol. Coerce at this - // sole writer so NO caller can emit a fractional token count — the clamp path already - // floors, but this also guards the restore path and any future caller at the type boundary. - const tokens = Number.isInteger(value) ? value : Math.floor(value); - if (field !== "generationConfig.maxOutputTokens") { - parsed[field] = tokens; - return; - } - const gen = parsed.generationConfig; - parsed.generationConfig = { ...(gen && typeof gen === "object" ? (gen as Record) : {}), maxOutputTokens: tokens }; -} - // #3071: absolute capability floor for the output budget when NO source knows a // ceiling for the model. The client's raw-history-derived budget decays unbounded // (…, 3071, 2061, 252, <=200); "last non-starved wins" then pins the session at a diff --git a/tests/doctor.test.ts b/tests/doctor.test.ts index 65c2cc860b..4de23f284b 100644 --- a/tests/doctor.test.ts +++ b/tests/doctor.test.ts @@ -4,7 +4,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { existsSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { chmodSync, existsSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { dshBundleShadowWarning, globalVerdict, laneVerdict, renderDoctorReport, runDoctor, type DoctorLane, type DoctorProcess, type DoctorReport } from "../src/doctor.ts"; @@ -670,3 +670,95 @@ test("#1577: bili doctor reports a clean advisory state as 'none' and a failed s rmrf(base); } }); + +// #6816: `bili doctor` warns when the built-in Claude Agent provider is +// available but not routed through bili. RED first: no implementation yet. +// The opencode lane is advisory-only: warnings never change the verdict. + +async function opencodeLaneFor6816(t: import("node:test").TestContext, opts: { version: string | null; providers?: Record }): Promise { + const base = mkdtempSync(path.join(tmpdir(), "bc-doctor-6816-")); + const ocCfg = path.join(base, "opencode.json"); + const shimDir = path.join(base, "plugins", "billion-context"); + // The lane verdict is "broken" when its target is missing, so materialize + // the shim the config entry points at (no package.json: no stale check). + mkdirSync(shimDir, { recursive: true }); + writeFileSync(path.join(shimDir, "index.js"), "export default {};\n"); + const ocData: Record = { plugins: [shimDir] }; + if (opts.providers !== undefined) ocData.providers = opts.providers; + writeFileSync(ocCfg, JSON.stringify(ocData, null, 2)); + let bin: string; + if (opts.version !== null) { + bin = path.join(base, "fake-opencode"); + writeFileSync(bin, `#!/bin/sh\necho 'opencode ${opts.version}'\n`); + chmodSync(bin, 0o755); + } else { + bin = "/nonexistent/bili-oc-6816-pin"; + } + t.after(() => rmrf(base)); + let lane: DoctorLane | undefined; + await mockFetch(() => new Response(JSON.stringify({ name: "billion-context", version: "999.0.0" }), { status: 200, headers: { "Content-Type": "application/json" } }))(async () => { + await withEnv({ + HOME: path.join(base, "home"), + XDG_CONFIG_HOME: path.join(base, "xdg-config"), + XDG_DATA_HOME: path.join(base, "xdg-data"), + XDG_CACHE_HOME: path.join(base, "xdg-cache"), + XDG_STATE_HOME: path.join(base, "xdg-state"), + PI_CODING_AGENT_DIR: undefined, PI_HOME: undefined, DSH_HOME: undefined, + HERMES_HOME: undefined, KIMI_CODE_HOME: undefined, CODEX_HOME: undefined, + CLAUDE_CONFIG_DIR: undefined, + OPENCODE_CONFIG: ocCfg, + BILI_CLIENT_BIN: bin, + }, async () => { + const report = await runDoctor({ packageName: "billion-context", runningVersion: "0.1.143" }); + lane = report.lanes.find((l) => l.agent === "opencode"); + }); + }); + return lane; +} + +test("#6816 doctor warns when the Claude Agent provider has no baseURL on a 2.x host", async (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const oc = await opencodeLaneFor6816(t, { version: "2.3.0" }); + assert.equal(oc?.installed, true); + assert.equal(oc?.verdict, "ok", "advisory only — the warning must not flip the verdict"); + assert.ok(oc?.warnings?.some((w) => w.includes("claude-agent") && w.includes("not routed")), `warnings: ${JSON.stringify(oc?.warnings)}`); +}); + +test("#6816 doctor stays silent when the Claude Agent provider is routed at the live lane port", async (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const oc = await opencodeLaneFor6816(t, { version: "2.3.0", providers: { "claude-agent": { settings: { baseURL: "http://127.0.0.1:18787/bili/https://api.anthropic.com" } } } }); + assert.equal(oc?.verdict, "ok"); + assert.ok(!oc?.warnings?.some((w) => w.includes("claude-agent")), `no claude-agent warning when routed: ${JSON.stringify(oc?.warnings)}`); +}); + +test("#6816 doctor warns when the Claude Agent provider points at a foreign baseURL", async (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const oc = await opencodeLaneFor6816(t, { version: "2.3.0", providers: { "claude-agent": { settings: { baseURL: "https://proxy.example.com/anthropic" } } } }); + assert.equal(oc?.verdict, "ok", "advisory only"); + assert.ok(oc?.warnings?.some((w) => w.includes("claude-agent") && w.includes("bypass")), `warnings: ${JSON.stringify(oc?.warnings)}`); +}); + +test("#6816 doctor warns when the Claude Agent baseURL points at a stale port", async (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const oc = await opencodeLaneFor6816(t, { version: "2.3.0", providers: { "claude-agent": { settings: { baseURL: "http://127.0.0.1:18788/bili/https://api.anthropic.com" } } } }); + assert.equal(oc?.verdict, "ok", "advisory only"); + assert.ok(oc?.warnings?.some((w) => w.includes("claude-agent") && w.includes("stale port")), `warnings: ${JSON.stringify(oc?.warnings)}`); +}); + +test("#6816 doctor stays silent on a 1.x host: no built-in Claude Agent provider there", async (t) => { + const oc = await opencodeLaneFor6816(t, { version: null }); + assert.equal(oc?.installed, true); + assert.ok(!oc?.warnings?.some((w) => w.includes("claude-agent")), `no claude-agent warning on 1.x: ${JSON.stringify(oc?.warnings)}`); +}); diff --git a/tests/dsh-llm-stream-attribution.test.ts b/tests/dsh-llm-stream-attribution.test.ts index 5bb5b7d0bd..c5840939e4 100644 --- a/tests/dsh-llm-stream-attribution.test.ts +++ b/tests/dsh-llm-stream-attribution.test.ts @@ -10,7 +10,7 @@ * loses: retry chains, agentless side-calls, disposed initiator scopes. */ import { strict as assert } from "node:assert"; -import { mkdtempSync, rmSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test, { after, before } from "node:test"; @@ -25,6 +25,7 @@ import { apply, forkChildVerdict, _resetRegisterForTest, + _resetCompactionCoverageNoteForTest, _settleNativeForTest, _stateBeforeSendForTest, _nativeStateForTest, @@ -451,3 +452,46 @@ test("#6880: the dispatch purpose is captured into the scope and stamped per fet } }); }); + +// #6770: the durable profile note that used to carry the coverage reading stays +// silent wherever profileContext.startedBundles cannot be read (the observed +// state on every host we checked), so the reading rides the first routed +// request instead. This pins the wiring itself — that it fires, and that it +// fires exactly once per process. +test("#6770: compaction overlay coverage is logged once on the first routed request", { timeout: 15_000 }, async () => { + await withSetup(async (handle) => { + const previousState = process.env.XDG_STATE_HOME; + const state = mkdtempSync(join(tmpdir(), "dsh-coverage-note-")); + process.env.XDG_STATE_HOME = state; + const logFile = join(state, "billion-context", "bili.log"); + const coverageLines = (): number => + existsSync(logFile) ? readFileSync(logFile, "utf8").split("compaction overlay coverage").length - 1 : 0; + const gate = _stateTakeoverGateForTest(); + assert.ok(gate, "takeover gate is installed after apply()"); + const base = async function* (): AsyncIterable { + await gate(MODEL_URL, { bytes: 200 }, { headers: {} }); + yield 1; + }; + try { + _resetCompactionCoverageNoteForTest(); + handle.setInitiator("sess-coverage"); + for await (const _chunk of runWaterfall(handle.events, { sessionId: "sess-coverage" }, base)) { + /* drain */ + } + assert.equal(coverageLines(), 1, "the first routed request carries the coverage readout"); + assert.match( + readFileSync(logFile, "utf8"), + /no preset entry rewritten in this process/, + "an untouched overlay reports the lazy-resolution frame, not a bare zero", + ); + for await (const _chunk of runWaterfall(handle.events, { sessionId: "sess-coverage" }, base)) { + /* drain */ + } + assert.equal(coverageLines(), 1, "a second routed request must not repeat the reading (once per process)"); + } finally { + if (previousState === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = previousState; + rmSync(state, { recursive: true, force: true }); + } + }); +}); diff --git a/tests/fold-anchor-doctrine.test.ts b/tests/fold-anchor-doctrine.test.ts index ffbd7f8b52..eab4710dc0 100644 --- a/tests/fold-anchor-doctrine.test.ts +++ b/tests/fold-anchor-doctrine.test.ts @@ -268,7 +268,9 @@ describe("honesty gates survive the doctrine (no wholesale silencing)", () => { const result = await preflightCompress({ core: {} as never, // untouched on the gated path config: { modelContextLimit: 1000 } as never, - session: session as never, + // #6069: the gate now judges window fit on the calibrated scale, + // so the session carries (empty) stats like a real one. + session: { ...session, stats: {} } as never, prompts: defaultPrompts, protocol: "responses", url: "http://127.0.0.1:1/summary-must-not-be-called", diff --git a/tests/fold-reconcile-residual-scale.test.ts b/tests/fold-reconcile-residual-scale.test.ts new file mode 100644 index 0000000000..095d2c7286 --- /dev/null +++ b/tests/fold-reconcile-residual-scale.test.ts @@ -0,0 +1,78 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import type { CoreMessage } from "acp-kernel"; +import { canonicalTextOf, normalizedIdentity, planReconciliation, wordBagDigest, type FoldAnchor } from "../src/fold-reconcile.ts"; +import { simhashHex } from "../src/affinity-simhash.ts"; + +// A resumed session whose folds are all inactive presents every stored id as +// missing while the wire carries a different, re-minted history. The shape +// pass and the residual census then weigh each unclaimed anchor against the +// candidates on word evidence. Those digests depend only on a candidate's +// text, so the work must stay linear in anchors + candidates: re-tokenizing +// every candidate for every anchor blocked a live proxy's event loop past the +// 300 s watchdog (9.6k stored refs x 1.3k inbound messages). +const WORDS = "alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa".split(" "); + +function prose(seed: number, words: number): string { + const out: string[] = [`msg${seed}`]; // one seed-unique word: no two seeds share a word bag + for (let i = 0; i < words; i++) out.push(`${WORDS[(seed * 7 + i * 3) % WORDS.length]}${(seed + i) % 97}`); + return out.join(" "); +} + +function anchorFor(text: string): FoldAnchor { + const canon = canonicalTextOf(text); + return { r: "assistant", c: "text", b: text.length, wh: wordBagDigest(canon), sh: simhashHex(canon) }; +} + +function remintedSession(stored: number, inbound: number, words: number) { + const order = Array.from({ length: stored }, (_, i) => `old_${i}`); + const anchors: Record = {}; + order.forEach((id, i) => { anchors[id] = anchorFor(prose(i, words)); }); + const msgs: CoreMessage[] = Array.from({ length: inbound }, (_, i) => ({ id: `new_${i}`, role: "assistant", contentType: "text", text: prose(100_000 + i, words) })); + return { order, anchors, msgs, covered: new Set(order) }; +} + +test("residual census over a fully re-minted history stays linear", () => { + const h = remintedSession(3_000, 1_500, 400); + const t0 = process.hrtime.bigint(); + const plan = planReconciliation(h.order, h.anchors, h.msgs, h.covered); + const ms = Number(process.hrtime.bigint() - t0) / 1e6; + assert.equal(plan.claims.size, 0); + assert.equal(plan.unmatched.length, h.order.length); + assert.equal(plan.absentEverywhere.length, h.order.length); + assert.deepEqual(plan.nearDupResidual, []); + // Linear work here is well under a second; the per-pair form re-tokenized + // 4.5M ~2.5 KB texts and took minutes. + assert.ok(ms < 5_000, `3k anchors x 1.5k candidates took ${ms.toFixed(0)}ms`); +}); + +test("shape pass still pairs re-wrapped user text by unwrapped identity, in order", () => { + const stored = Array.from({ length: 6 }, (_, i) => prose(i, 12)); + const order = stored.map((_, i) => `old_${i}`); + const anchors: Record = {}; + stored.forEach((text, i) => { + const canon = canonicalTextOf(text); + anchors[order[i]] = { r: "user", c: "text", b: text.length, n: normalizedIdentity({ id: order[i], role: "user", contentType: "text", text }), wh: wordBagDigest(canon), sh: simhashHex(canon) }; + }); + // The host re-wraps each line under a fresh id; an unrelated user message sits in front. + const msgs: CoreMessage[] = [{ id: "noise", role: "user", contentType: "text", text: prose(99, 12) }]; + stored.forEach((text, i) => msgs.push({ id: `new_${i}`, role: "user", contentType: "text", text: text.replace(/ /g, (_, at: number) => (at % 3 === 0 ? "\n" : " ")) })); + const plan = planReconciliation(order, anchors, msgs, new Set(order)); + for (let i = 0; i < stored.length; i++) assert.deepEqual(plan.claims.get(`old_${i}`), [`new_${i}`]); + assert.equal(plan.byShape, stored.length); +}); + +test("shape pass still pairs a re-minted word-bag twin with the first candidate in order", () => { + const h = remintedSession(8, 8, 40); + // Two inbound copies of old_3's words under fresh ids: the earlier one wins. + h.msgs.splice(2, 0, { id: "twin_a", role: "assistant", contentType: "text", text: prose(3, 40) }); + h.msgs.push({ id: "twin_b", role: "assistant", contentType: "text", text: prose(3, 40) }); + const plan = planReconciliation(h.order, h.anchors, h.msgs, h.covered); + assert.deepEqual(plan.claims.get("old_3"), ["twin_a"]); + assert.equal(plan.byShape, 1); + // The census runs over the pre-shape unmatched list: old_3 is still listed + // there, and the unclaimed twin_b carries its bag — near-duplicate residue. + // No other anchor shares a bag with any candidate. + assert.deepEqual(plan.nearDupResidual, ["old_3"]); + assert.equal(plan.absentEverywhere.length, h.order.length - 1); +}); diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index b2df83705d..9bfc0c1b60 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -95,12 +95,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "507144657ede0a24d6871285d294dbcad0df67e5e6e8e7f18f8ba9ae453cf651", - "stateDigestAligned": "80117f64febac750ec89f8e55da022d84eebb6da8fb07988054075975abc1bbd", + "stateDigest": "201eb6df8c4cf505ffaa628e67f738fe31daf482eae08baeb3b704b68932571a", + "stateDigestAligned": "7b249e7388c7875c8540e7e950f6fda8aed08a57e1412149919e0a70e2eeec82", "sectionDigests": { "state": "37b26b4f6898afff", "blockContents": "dc7d13d250c94bb4", - "stats": "1fec36cc7532a73b", + "stats": "7caf981f99159be9", "metadata": "d5a483de67911378" }, "stateKeyDigests": { diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 4f5add08a1..1f7ba4fa8c 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,12 +95,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "da252c1c9271bc2fcc53a5755208d83ecbf68f86798211da4891e1ff709bcaf1", - "stateDigestAligned": "69c3237b05251c3ce797a20cf2872940cad04c0f1f22e5b74991cef18f760d6e", + "stateDigest": "1377fd8b0e9aee644b0252ed66762fce72ce357778734c1e22a4b99d05c76fe9", + "stateDigestAligned": "402c083e9fcab5ba6b6d91fe2d4b4cf65f3b1fcf3fa88e2c040e81fc017d5765", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", - "stats": "19507b208ba3a6a0", + "stats": "7b6162aef3873d31", "metadata": "57404903cbbf85a1" }, "stateKeyDigests": { diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 38dd111de5..d8799aad56 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -95,12 +95,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "2face5f46c429ea6e78bce91dfa93806afdbf8bf089df467e499d4eb6b0ddacb", - "stateDigestAligned": "5d7bce0adab95181eaa05e3b05b9194d4251f9cd9161f223e475c06c70d665d3", + "stateDigest": "11bc49add04e1408678a44de2cc4142a623c6fa723d69277fd150c99be33ff11", + "stateDigestAligned": "08d9988e139152731ecefae4c320df288ee9a132ec5203ef8edff604c23681b1", "sectionDigests": { "state": "c5243ba17364d3ac", "blockContents": "473264d9df8ba338", - "stats": "dc7ffbd96697b9bd", + "stats": "c90671e484dc5d37", "metadata": "518c072aca3f9f76" }, "stateKeyDigests": { diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 0ddea236f5..3f0b0d5575 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -95,12 +95,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "e0030d14803677e61f58310cae0dbf46f1d510c0aa913e0d0257e8db9cdcaefe", - "stateDigestAligned": "8aaca45bcc9546990021d3796eb08b2840b40c5fcc73b604791114d51773665a", + "stateDigest": "642951dac35e9b2eb4fb0b700f7d23e98f5477eb9c19e718b9deab62ce8485f5", + "stateDigestAligned": "c36820a7b7e8bda7541714cee83ffd4870e46602a6191956deaab15c0c91d12e", "sectionDigests": { "state": "bee6e1ba0c6fba62", "blockContents": "b6701268a0bdcd8a", - "stats": "3d759eaf5f044c60", + "stats": "1b56c993f03cd122", "metadata": "71746aec32a84063" }, "stateKeyDigests": { diff --git a/tests/issue6069-epoch-pause-over-window.test.ts b/tests/issue6069-epoch-pause-over-window.test.ts new file mode 100644 index 0000000000..3a01f6997a --- /dev/null +++ b/tests/issue6069-epoch-pause-over-window.test.ts @@ -0,0 +1,249 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { createCore, defaultConfig, defaultPrompts, type CoreMessage } from "acp-kernel"; +import type { ProxyOptions } from "../src/config.ts"; +import { epochConfirmPending } from "../src/fold-reconcile.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { preflightCompress } from "../src/preflight.ts"; +import * as registry from "../src/registry.ts"; +import { startServer } from "../src/server.ts"; +import { getSession, listSessions, type Session } from "../src/session.ts"; + +// #6069: the #3641 epoch-confirm latch pauses preflight folding and passes the +// request through verbatim. That is only possible while the verbatim payload +// fits the model window. Once it does not, the paused preflight reported +// "0 compressible range(s)" and the request was refused with +// `preflight_compress_failed` (retryable=false) on every turn. The latch +// clears only after a turn is forwarded, so the session could never recover. +// +// Contract pinned here: the pause holds while the payload fits; an over-window +// payload folds as usual. + +const SUMMARY = "summary text long enough to pass the apply gate at fifty chars."; +const WINDOW = 20_000; + +async function summaryUpstream(): Promise<{ url: string; calls: () => number; close: () => Promise }> { + let calls = 0; + const server = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + calls++; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ choices: [{ message: { content: SUMMARY } }] })); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const { port } = server.address() as { port: number }; + return { + url: `http://127.0.0.1:${port}/v1/chat/completions`, + calls: () => calls, + close: async () => { + server.close(); + server.closeIdleConnections?.(); + await once(server, "close"); + }, + }; +} + +function latchedSession(name: string): Session { + _setStoreForTest(new SessionStore({ enabled: false })); + registry._resetForTest(); + const session = getSession(`issue6069-${name}-${Math.random().toString(36).slice(2)}`); + session.metadata.foldEpochConfirmPending = true; + assert.equal(epochConfirmPending(session), true, "precondition: the epoch-confirm latch is pending"); + return session; +} + +/** `turns` large turns between a first and a last user message; each large + * turn is ~5K tokens of distinct words. */ +function conversation(turns: number): CoreMessage[] { + const messages: CoreMessage[] = [{ id: "first", role: "user", contentType: "text", text: "Keep the task goal." }]; + for (let i = 0; i < turns; i++) { + messages.push({ id: `turn${i}`, role: i % 2 === 0 ? "assistant" : "user", contentType: "text", text: `TURN${i}_WORD `.repeat(2000) }); + } + messages.push({ id: "last", role: "user", contentType: "text", text: "Continue the task." }); + return messages; +} + +test("#6069: epoch-confirm pending and the verbatim payload fits the window: folding stays paused", async () => { + const upstream = await summaryUpstream(); + const session = latchedSession("fits"); + try { + const result = await preflightCompress( + { + core: createCore(), session, config: defaultConfig(WINDOW, { preserveRecentMessages: 0, preserveRecentTokens: 0 }), + prompts: defaultPrompts, protocol: "openai", url: upstream.url, headers: {}, model: "test-model", + log: () => { /* quiet */ }, + }, + conversation(2), + ); + assert.equal(upstream.calls(), 0, "paused: no summarization call"); + assert.equal(result.compressedRanges, 0, "paused: no range folded"); + assert.equal(result.fitsWindow, true, "the verbatim payload is forwardable"); + assert.equal(epochConfirmPending(session), true, "the latch is left for fold-reconcile to clear"); + } finally { + await upstream.close(); + } +}); + +test("#6069: epoch-confirm pending and the verbatim payload exceeds the window: preflight folds instead of refusing", async () => { + const upstream = await summaryUpstream(); + const session = latchedSession("over"); + const logs: string[] = []; + try { + const result = await preflightCompress( + { + core: createCore(), session, config: defaultConfig(WINDOW, { preserveRecentMessages: 0, preserveRecentTokens: 0 }), + prompts: defaultPrompts, protocol: "openai", url: upstream.url, headers: {}, model: "test-model", + log: (level, msg) => logs.push(`${level}: ${msg}`), + }, + conversation(8), + ); + assert.ok(upstream.calls() >= 1, `an over-window payload is folded (summarization calls: ${upstream.calls()})`); + assert.ok(result.compressedRanges >= 1, `at least one range folded (got ${result.compressedRanges})`); + assert.equal(result.fitsWindow, true, "the folded payload fits the window and is forwardable"); + assert.ok(session.state.blocks.some((b) => b.active), "the fold is recorded as an active block"); + assert.equal(logs.filter((line) => line.startsWith("warn") && line.includes("#6069")).length, 1, "one warn line names the resumed folding"); + } finally { + await upstream.close(); + } +}); + +// End to end, with the latch set by fold-reconcile itself: a folded history is +// replaced by an unrelated one under the same session id (three passes), then +// the new history grows past the window. + +const E2E_SUMMARY = + "PREFLIGHT SUMMARY: the segment covered a multi-step debugging session. " + + "Key decisions: chose the preflight approach. Files touched: src/a.ts:10. Outcome: fixed and verified by tests."; + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 1000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +function wireHistory(tag: string, count: number, repeat: number): Array<{ role: string; content: string }> { + const messages: Array<{ role: string; content: string }> = []; + for (let i = 0; i < count; i++) { + messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `${tag} message ${i}. ` + `${tag}_${i}_content_`.repeat(repeat) }); + } + return messages; +} + +test("e2e #6069: history replaced under the same session id, then grown past the window: the turn is folded and forwarded", async () => { + const calls: Array<{ stream: boolean; body: string }> = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let parsed: { stream?: boolean } = {}; + try { parsed = JSON.parse(raw); } catch { /* keep {} */ } + calls.push({ stream: !!parsed.stream, body: raw }); + if (parsed.stream) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "msg_summary", type: "message", role: "assistant", model: "claude-small", + content: [{ type: "text", text: E2E_SUMMARY }], stop_reason: "end_turn", + usage: { input_tokens: 500, output_tokens: 50 }, + })); + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + registry._setForTest({}); + 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: { "claude-small": { context: 12_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + const send = async (messages: unknown): Promise => { + const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "issue6069-e2e" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages }), + }); + await response.text(); + return response.status; + }; + const summaryCalls = (): number => calls.filter((c) => !c.stream).length; + const session = (): Session => { + const found = listSessions().find((s) => s.id === "issue6069-e2e"); + assert.ok(found, "the session exists"); + return found; + }; + + try { + // 1. A ~26K-token history on a 12K window: preflight folds it. + assert.equal(await send(wireHistory("OLD", 24, 250)), 200); + assert.ok(session().state.blocks.some((b) => b.active), "the old history is folded"); + + // 2. An unrelated history that fits the window, resent three times: + // fold-reconcile latches the epoch confirmation and keeps the snapshot. + const replaced = wireHistory("NEW", 12, 100); + for (let pass = 0; pass < 3; pass++) assert.equal(await send(replaced), 200, "a fitting history passes through while folding is paused"); + assert.equal(epochConfirmPending(session()), true, "precondition: the epoch-confirm latch is pending"); + const foldedBefore = summaryCalls(); + + // 3. The new history grows past the window. + const grown = [...replaced, ...wireHistory("GROWN", 16, 250)]; + assert.equal(await send(grown), 200, "the over-window turn is folded and forwarded, not refused"); + assert.ok(summaryCalls() > foldedBefore, "preflight summarized the new history"); + const forwarded = calls.filter((c) => c.stream).at(-1); + assert.ok(forwarded, "a forward happened"); + assert.ok(forwarded.body.includes(E2E_SUMMARY), "the new summary is in the forwarded payload"); + assert.ok(!forwarded.body.includes("OLD_1_content_"), "nothing of the replaced history is forwarded"); + assert.ok(forwarded.body.includes("GROWN_15_content_"), "the newest turn stays verbatim"); + assert.equal(epochConfirmPending(session()), false, "the fresh fold starts the new epoch: the latch clears"); + + // 4. The session keeps working. + assert.equal(await send([...grown, { role: "assistant", content: "ok" }, { role: "user", content: "next question" }]), 200); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + } +}); diff --git a/tests/issue6752-watermark-output-budget-restore.test.ts b/tests/issue6752-watermark-output-budget-restore.test.ts new file mode 100644 index 0000000000..0c5c7b0acd --- /dev/null +++ b/tests/issue6752-watermark-output-budget-restore.test.ts @@ -0,0 +1,333 @@ +// #6752 — output-budget restore lost on watermark hits. +// +// A dsh plugin-native turn with a client-shrunk output budget (max_tokens: 1) +// is rescued by restoreOutputBudget in handle.ts (log: "output budget +// restored 1 -> 32768"), but on a watermark-fastpath HIT prepareOpenai +// rebinds `parsed` from the RAW wire text (bodyText) — re-parsing every +// scalar field, including max_tokens — so the restored value never reaches +// the upstream while the log still reports success. +// +// A/B golden: the same two scripted turns run through two proxies — +// PHASE A: pluginFullResendWatermark OFF → legacy full-resend path +// PHASE B: rollout ON (env force) → fastpath (stamped hits) +// Turn 0 seeds the high-water (healthy 64000 budget) and the watermark; +// turn 1 carries the starved budget (max_tokens: 1) with tools present. +// Both phases must forward the RESTORED budget (32768, the declared output +// ceiling) on turn 1. Before the fix, phase B forwards max_tokens: 1. +// +// The residency check proves phase B actually HIT on turn 1 (a miss would +// take the legacy path and forward the restored value vacuously). + +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import http from "node:http"; +import { once } from "node:events"; +import os from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { + SessionStore, + _setStoreForTest, + _resetStoreForTest, +} from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { closeServers } from "./server-close-guard.ts"; + +const ENV = "BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK"; + +interface Capture { + raw: string; +} +interface Harness2 { + root: string; + captured: Capture[]; + upstream: http.Server; + store: SessionStore; +} + +function makeUpstream(sink: Capture[]) { + return http.createServer((req, res) => { + let data = ""; + req.on("data", (c) => (data += c)); + req.on("end", () => { + sink.push({ raw: data }); + res.writeHead(200, { "content-type": "application/json" }); + res.end( + JSON.stringify({ + id: "chatcmpl-test", + object: "chat.completion", + created: 1, + model: "model-test", + choices: [ + { + index: 0, + message: { role: "assistant", content: "answer" }, + finish_reason: "stop", + }, + ], + usage: { + prompt_tokens: 100, + completion_tokens: 4, + total_tokens: 104, + }, + }), + ); + }); + }); +} + +function makeHarness(tag: string): Harness2 { + _resetStoreForTest(); + _resetPluginStateForTest(); + _resetSessionsForTest(); + const root = mkdtempSync(path.join(os.tmpdir(), `obw-${tag}-`)); + for (const k of [ + "HOME", + "APPHOME", + "HERMES_HOME", + "XDG_STATE_HOME", + "XDG_DATA_HOME", + "XDG_CACHE_HOME", + "XDG_CONFIG_HOME", + ]) + process.env[k] = root; + const store = new SessionStore({ + dir: path.join(root, "sessions"), + enabled: false, + }); + _setStoreForTest(store); + const captured: Capture[] = []; + return { root, store, captured, upstream: makeUpstream(captured) }; +} + +async function bringUp(h: Harness2) { + h.upstream.listen(0, "127.0.0.1"); + await once(h.upstream, "listening"); + const uaddr = h.upstream.address(); + assert(uaddr && typeof uaddr === "object"); + const upstreamUrl = `http://127.0.0.1:${uaddr.port}`; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: upstreamUrl, + routes: { + [upstreamUrl]: { + models: { "model-test": { context: 64_000, output: 32_768 } }, + }, + }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: true, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + return { proxy, origin: `http://127.0.0.1:${paddr.port}`, upstreamUrl }; +} + +function idOf(el: unknown): string { + const s = JSON.stringify(el); + let h = 0x811c9dc5; + for (let k = 0; k < s.length; k++) { + h ^= s.charCodeAt(k); + h = Math.imul(h, 0x01000193) >>> 0; + } + return `eid-${h.toString(16)}`; +} + +const TOOL = { + type: "function", + function: { + name: "test_tool", + description: "test tool", + parameters: { type: "object", properties: {} }, + }, +}; + +function scriptTurns(): Array<{ + model: string; + messages: unknown[]; + tools: unknown[]; + max_tokens: number; + stream: boolean; +}> { + // Turn 0: healthy budget + tools — main turn, seeds the high-water and + // the watermark. Turn 1: same history + one more exchange, starved + // budget (max_tokens: 1) + tools — restore must raise it to the declared + // output ceiling (32768) on BOTH paths. + const history: unknown[] = [ + { role: "system", content: "You are TestBuddy v2." }, + ]; + const lines = ["alpha", "beta", "gamma", "delta"]; + for (let i = 0; i < lines.length; i++) { + history.push({ role: "user", content: lines[i] }); + history.push({ role: "assistant", content: `ack ${i}` }); + } + const turn1 = [ + ...history, + { role: "user", content: "epsilon" }, + { role: "assistant", content: "ack 4" }, + { role: "user", content: "zeta" }, + ]; + return [ + { + model: "model-test", + messages: history, + tools: [TOOL], + max_tokens: 64_000, + stream: false, + }, + { + model: "model-test", + messages: turn1, + tools: [TOOL], + max_tokens: 1, + stream: false, + }, + ]; +} + +async function runClient( + origin: string, + upstreamUrl: string, + sid: string, + turns: ReturnType, +): Promise<(number | null)[]> { + const stamps: (number | null)[] = []; + for (let i = 0; i < turns.length; i++) { + const headers: Record = { + "content-type": "application/json", + "x-acp-session": sid, + "x-bili-plugin": "dsh", + }; + const prev = stamps[stamps.length - 1]; + if (prev !== null && prev !== undefined) + headers["x-bili-pf-count"] = String(prev); + const ids = (turns[i].messages as unknown[]).map(idOf); + if (prev !== null && prev !== undefined && ids.length >= prev) { + headers["x-bili-pf-tail-ids"] = ids + .slice(prev - 1) + .map(encodeURIComponent) + .join(","); + } + const res = await fetch( + `${origin}/bili/${upstreamUrl}/v1/chat/completions`, + { + method: "POST", + headers, + body: JSON.stringify(turns[i]), + }, + ); + assert.equal(res.status, 200, `turn ${i}: proxy answers 200`); + const s = res.headers.get("x-bili-pf-seq"); + stamps.push(s !== null && /^\d+$/.test(s) ? Number(s) : null); + await res.arrayBuffer(); + } + return stamps; +} + +test("#6752 watermark hit must not discard the restored output budget", async () => { + const savedEnv = process.env[ENV]; + const roots: string[] = []; + try { + // ---------- PHASE A: feature OFF (legacy full-resend) ---------- + process.env[ENV] = "0"; + const ha0 = makeHarness("phase-a"); + roots.push(ha0.root); + const ha = await bringUp(ha0); + const ra = await runClient( + ha.origin, + ha.upstreamUrl, + "obw-phase-a", + scriptTurns(), + ); + const memA = (await (await fetch(`${ha.origin}/__bili/memory`)).json()) as { + breakdown: { lastMessages: number }; + }; + ha0.store.cancelAll(); + await closeServers([ha.proxy, ha0.upstream], "obw phase-a"); + + // ---------- PHASE B: feature ON (fastpath stamped hits) ---------- + process.env[ENV] = "1"; + const hb0 = makeHarness("phase-b"); + roots.push(hb0.root); + const hb = await bringUp(hb0); + const rb = await runClient( + hb.origin, + hb.upstreamUrl, + "obw-phase-b", + scriptTurns(), + ); + const memB = (await (await fetch(`${hb.origin}/__bili/memory`)).json()) as { + breakdown: { lastMessages: number }; + }; + hb0.store.cancelAll(); + await closeServers([hb.proxy, hb0.upstream], "obw phase-b"); + + assert.equal(ha0.captured.length, 2, "legacy served both turns"); + assert.equal(hb0.captured.length, 2, "fastpath served both turns"); + // Turn 0 stamps (watermark seeded) in phase B; turn 1 must be a HIT, + // so the fastpath pins only the tail window (residency proof). + assert.equal( + rb[0], + scriptTurns()[0].messages.length, + "B stamp 0: measured wire total", + ); + assert.ok( + memA.breakdown.lastMessages > 0, + "legacy pinned resident history", + ); + assert.ok( + memB.breakdown.lastMessages > 0, + "fastpath pinned the tail window", + ); + assert.ok( + memB.breakdown.lastMessages < memA.breakdown.lastMessages, + `fastpath did not hit on turn 1: pinned ${memB.breakdown.lastMessages} vs legacy ${memA.breakdown.lastMessages}`, + ); + + const maxTokensOf = (raw: string): unknown => + (JSON.parse(raw) as { max_tokens?: unknown }).max_tokens; + // Turn 0 forwards the healthy budget CLAMPED to the declared output + // ceiling (32768) in both phases — clampOutgoingOutput caps at the + // ceiling a provider would accept; the restore is not involved here. + assert.equal( + maxTokensOf(ha0.captured[0].raw), + 32_768, + "A turn 0: healthy budget clamped to the ceiling", + ); + assert.equal( + maxTokensOf(hb0.captured[0].raw), + 32_768, + "B turn 0: healthy budget clamped to the ceiling", + ); + // Turn 1: the starved budget must be restored to the declared output + // ceiling (32768) on BOTH paths — the log already claims the restore. + assert.equal( + maxTokensOf(ha0.captured[1].raw), + 32_768, + "A turn 1: starved budget restored to the ceiling", + ); + assert.equal( + maxTokensOf(hb0.captured[1].raw), + 32_768, + "B turn 1: starved budget restored to the ceiling (watermark hit must not discard it)", + ); + void ra; + } finally { + if (savedEnv === undefined) delete process.env[ENV]; + else process.env[ENV] = savedEnv; + for (const root of roots) rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/tests/issue6770-compaction-coverage-text.test.ts b/tests/issue6770-compaction-coverage-text.test.ts new file mode 100644 index 0000000000..d6f9406c15 --- /dev/null +++ b/tests/issue6770-compaction-coverage-text.test.ts @@ -0,0 +1,75 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { + _resetDshCompactionGateTelemetryForTest, + _resetDshCompactionVerdictForTest, + compactionCoverageText, + declareDshCompactionActive, + dshCompactionGateTelemetry, + registerCompactionDisable, +} from "../src/agent/dsh-compaction-disable.ts"; + +// #6770: the #3962 coverage reading needs an exit that does not depend on the +// durable profile-bundle note — that note stays silent wherever +// profileContext.startedBundles cannot be read (the observed state on both a +// desktop dsh build and the dsh-go runtime), so the reading had no reachable +// exit at all. compactionCoverageText() is the one formatter both exits share. + +type Mw = (raw: unknown, next: () => unknown) => unknown; + +function middleware(): Mw { + let mw: Mw | undefined; + registerCompactionDisable({ + on: (_event, listener) => { mw = listener as Mw; }, + reflect: {}, + }); + assert.equal(typeof mw, "function"); + return mw as Mw; +} + +test("#6770: no record yet — the reading states the lazy-resolution caveat instead of reporting zero", () => { + _resetDshCompactionGateTelemetryForTest(); + assert.equal(dshCompactionGateTelemetry(), null); + const text = compactionCoverageText(); + assert.match(text, /^no preset entry rewritten in this process/); + assert.match(text, /resolve lazily/); +}); + +test("#6770: singular and plural follow the covered-entry count", () => { + assert.equal( + compactionCoverageText({ covered: [{ entry: "preset-standard", rows: 2 }], sawFactoryPreset: true }), + "1 preset entry rewritten (preset-standard\u00d72)", + ); + assert.equal( + compactionCoverageText({ + covered: [{ entry: "preset-standard", rows: 2 }, { entry: "preset-go-companion", rows: 1 }], + sawFactoryPreset: true, + }), + "2 preset entries rewritten (preset-standard\u00d72, preset-go-companion\u00d71)", + ); +}); + +test("#6770: an empty covered list uses the empty frame, not a '0 entries rewritten' line", () => { + assert.match(compactionCoverageText({ covered: [], sawFactoryPreset: false }), /^no preset entry rewritten/); +}); + +test("#6770: the argument-less default reads the live record the overlay writes", () => { + try { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + assert.match(compactionCoverageText(), /^no preset entry rewritten/, "unlatched overlay reads empty"); + declareDshCompactionActive(); + const mw = middleware(); + const cfg = { + plugins: [ + { group: true, config: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }, + { name: "@deepseek-ai/dsh-command-compact" }, + ], + }; + mw.call({ entry: { options: { name: "preset-dsh-go-companion" } } }, null, () => cfg); + assert.equal(compactionCoverageText(), "1 preset entry rewritten (preset-dsh-go-companion\u00d72)"); + } finally { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + } +}); diff --git a/tests/omp-direct-send-stamped-pck.test.ts b/tests/omp-direct-send-stamped-pck.test.ts new file mode 100644 index 0000000000..95880200a5 --- /dev/null +++ b/tests/omp-direct-send-stamped-pck.test.ts @@ -0,0 +1,66 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createOmpDirectSendGuard, stampPromptCacheKey } from "../src/agent/pi.ts"; + +// The omp plugin stamps the session id as a top-level prompt_cache_key while a +// proxy is claimed (BILLION_CONTEXT_PROXY). The proxy strips it before it +// forwards. When one request then degrades to a direct send (proxy not ready, +// or its respawn failed) the claim is still set, so the body was stamped, but +// no proxy strips it: a strict upstream (Anthropic Messages) answers 400 +// "prompt_cache_key: Extra inputs are not permitted". The omp direct-send +// guard must drop the stamp it added — and only that. + +const INTERCEPT_FLAG = Symbol.for("billion-context.native-fetch-intercept"); +const PROXY_ORIGIN = "http://127.0.0.1:36011"; +const URL_MESSAGES = "http://127.0.0.1:8199/v1/messages"; + +function withNativeClaim(fn: () => T): T { + const g = globalThis as Record; + const prevFlag = g[INTERCEPT_FLAG]; + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + g[INTERCEPT_FLAG] = true; + process.env.BILLION_CONTEXT_PROXY = PROXY_ORIGIN; + try { + return fn(); + } finally { + if (prevFlag === undefined) delete g[INTERCEPT_FLAG]; + else g[INTERCEPT_FLAG] = prevFlag; + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + } +} + +function stampedBody(sessionId: string): Record { + const ctx = { model: { baseUrl: "http://127.0.0.1:8199/v1" }, sessionManager: { getSessionId: () => sessionId } }; + const event = { type: "before_provider_request", payload: { model: "m", max_tokens: 16, messages: [{ role: "user", content: "hi" }] } }; + const out = withNativeClaim(() => stampPromptCacheKey(event, ctx, "omp")); + assert.equal(out?.prompt_cache_key, sessionId, "fixture: the plugin stamps the session id"); + return out!; +} + +test("direct send drops the prompt_cache_key the plugin stamped", async () => { + const body = stampedBody("sess-direct-1"); + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: JSON.stringify(body) }); + assert.equal(prep.outcome, "restored"); + const sent = JSON.parse((prep as { body: string }).body) as Record; + assert.equal("prompt_cache_key" in sent, false); + assert.deepEqual(sent, { model: "m", max_tokens: 16, messages: [{ role: "user", content: "hi" }] }); +}); + +test("direct send keeps a prompt_cache_key the plugin did not stamp", async () => { + stampedBody("sess-direct-2"); + const own = { model: "m", messages: [{ role: "user", content: "hi" }], prompt_cache_key: "host-own-key" }; + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: JSON.stringify(own) }); + assert.equal(prep.outcome, "as-is"); +}); + +test("direct send leaves a body without prompt_cache_key untouched", async () => { + const plain = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: plain }); + assert.equal(prep.outcome, "as-is"); +}); + +test("a non-JSON body stays as-is", async () => { + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: "not json" }); + assert.equal(prep.outcome, "as-is"); +}); diff --git a/tests/persist-calibration-model.test.ts b/tests/persist-calibration-model.test.ts new file mode 100644 index 0000000000..85de57705c --- /dev/null +++ b/tests/persist-calibration-model.test.ts @@ -0,0 +1,80 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {mkdtempSync, readdirSync, readFileSync, writeFileSync} from 'node:fs'; +import {tmpdir} from 'node:os'; +import path from 'node:path'; +import {createInitialState} from 'acp-kernel'; +import {SessionStore} from '../src/persist.ts'; +import type {Session} from '../src/session.ts'; +import {currentCalibrationFactor} from '../src/util.ts'; +import {rmrf} from './tmp-rm.ts'; + +process.env.BILI_CONFIG_FILE = path.join(tmpdir(), 'calibration-model-no-config.json'); +process.env.BILI_PERSIST_ZSTD = '0'; + +function session(id: string, model?: string): Session { + return {id, meta:{protocol:'anthropic',upstreamOrigin:'http://synthetic.invalid'}, + stats:{requests:3,tokensSaved:0,inputTokens:10,cachedTokens:0,outputTokens:5, + cacheSamples:0,lastInputTokens:10,contextTokens:1000,compressCreditTokens:0, + retrieveCalls:0,retrieveHits:0,retrieveMisses:0,storedBytes:0,storeBytesSaved:0,rangeRestores:0, + calibratedEstimate:0.8,calibratedEstimateOrigin:'http://synthetic.invalid', + calibratedEstimateModel:model, + calibrationRing:{origin:'http://synthetic.invalid',...(model === undefined ? {} : {model}),values:[0.8,0.8]}}, + metadata:{},createdAt:1700000000000,lastSeen:1700000001000,state:createInitialState(), + blockContents:new Map(),inFlight:0,persisted:false,pendingRetrievals:[]}; +} + +for (const mode of ['legacy','full']) { + for (const model of ['synthetic-model-a', undefined]) { + test(`calibration provenance survives ${mode} cold load (${model ?? 'legacy unknown model'})`, async () => { + process.env.BILI_STORAGE_UNIFIED = mode; + const dir = mkdtempSync(path.join(tmpdir(),'bili-calibration-model-')); + const original = session('calibration-roundtrip',model); + let store = new SessionStore({dir,enabled:true,debounceMs:1}); + try { + await store.writeNow(original); + store.closeQuietly(); + store = new SessionStore({dir,enabled:true,debounceMs:1}); + const restored = store.loadSync(original.id,original.meta); + assert.ok(restored); + assert.deepEqual(restored.stats.calibrationRing,original.stats.calibrationRing, + 'Cold load must retain the ring including its exact model provenance'); + assert.equal(restored.stats.calibratedEstimateModel,model); + assert.equal(currentCalibrationFactor(restored.stats,'synthetic-model-a'),0.8); + assert.equal(currentCalibrationFactor(restored.stats,'synthetic-model-b'), + model === undefined ? 0.8 : undefined, + 'Known-model factors must not cross a model switch; unknown legacy models retain existing behavior'); + } finally {store.closeQuietly();rmrf(dir);} + }); + } +} + +for (const invalid of [123,' ',{},null]) { + test(`invalid persisted model provenance is rejected (${JSON.stringify(invalid)})`, async () => { + process.env.BILI_STORAGE_UNIFIED = 'legacy'; + const dir = mkdtempSync(path.join(tmpdir(),'bili-calibration-invalid-model-')); + const original = session('invalid-provenance','synthetic-model-a'); + let store = new SessionStore({dir,enabled:true,debounceMs:1}); + try { + await store.writeNow(original); + store.closeQuietly(); + const files = readdirSync(dir,{recursive:true}).filter(x => typeof x === 'string' && x.endsWith('.json')); + const file = files.map(x => path.join(dir,String(x))).find(x => { + const raw = JSON.parse(readFileSync(x,'utf8')); + return (raw.payload ?? raw).id === original.id; + }); + assert.ok(file,'Fixture must have written its session envelope'); + const raw = JSON.parse(readFileSync(file,'utf8')); + const payload = raw.payload ?? raw; + payload.stats.calibrationRing.model = invalid; + payload.stats.calibratedEstimateModel = invalid; + writeFileSync(file,JSON.stringify(raw)); + store = new SessionStore({dir,enabled:true,debounceMs:1}); + const restored = store.loadSync(original.id,original.meta); + assert.ok(restored); + assert.equal(restored.stats.calibrationRing?.model,undefined); + assert.equal(restored.stats.calibratedEstimateModel,undefined); + assert.deepEqual(restored.stats.calibrationRing?.values,[0.8,0.8]); + } finally {store.closeQuietly();rmrf(dir);} + }); +} diff --git a/tests/persist-emergency-marker.test.ts b/tests/persist-emergency-marker.test.ts new file mode 100644 index 0000000000..b5ddd9f650 --- /dev/null +++ b/tests/persist-emergency-marker.test.ts @@ -0,0 +1,41 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {mkdtempSync} from 'node:fs'; +import {tmpdir} from 'node:os'; +import path from 'node:path'; +import {createInitialState} from 'acp-kernel'; +import {SessionStore} from '../src/persist.ts'; +import type {Session} from '../src/session.ts'; +import {rmrf} from './tmp-rm.ts'; + +for(const mode of ['legacy','full']){ + for(const armed of [true,false]){ + test(`failure-shrink status evidence survives ${mode} restart (armed=${armed})`,async()=>{ + process.env.BILI_STORAGE_UNIFIED=mode; + process.env.BILI_PERSIST_ZSTD='0'; + const dir=mkdtempSync(path.join(tmpdir(),'bili-shrink-marker-test-')); + process.env.BILI_CONFIG_FILE=path.join(dir,'absent-config'); + const session:Session={id:'synthetic-shrink-marker',meta:{protocol:'anthropic',upstreamOrigin:'http://synthetic.invalid'}, + stats:{requests:1,tokensSaved:0,inputTokens:1000,cachedTokens:0,outputTokens:5,cacheSamples:0, + lastInputTokens:10000,lastInputTokensSource:armed?'estimate':'usage',contextTokens:10000,compressCreditTokens:0, + retrieveCalls:0,retrieveHits:0,retrieveMisses:0,storedBytes:0,storeBytesSaved:0,rangeRestores:0, + ...(armed?{emergencyShrinkAt:1700000000000,emergencyShrinkEstimate:10000}:{})},metadata:{}, + createdAt:1700000000000,lastSeen:1700000001000,state:createInitialState(),blockContents:new Map(), + inFlight:0,persisted:false,pendingRetrievals:[]}; + let store=new SessionStore({dir,enabled:true,debounceMs:1}); + try{ + await store.writeNow(session);store.closeQuietly();store=new SessionStore({dir,enabled:true,debounceMs:1}); + const restored=store.loadSync(session.id,session.meta);assert.ok(restored); + assert.equal(restored.stats.emergencyShrinkAt,armed?1700000000000:undefined); + assert.equal(restored.stats.emergencyShrinkEstimate,armed?10000:undefined); + assert.equal(restored.stats.lastInputTokens,10000); + assert.equal(restored.stats.lastInputTokensSource,armed?'estimate':'usage'); + delete restored.stats.emergencyShrinkAt;delete restored.stats.emergencyShrinkEstimate; + await store.writeNow(restored);store.closeQuietly();store=new SessionStore({dir,enabled:true,debounceMs:1}); + const cleared=store.loadSync(session.id,session.meta);assert.ok(cleared); + assert.equal(cleared.stats.emergencyShrinkAt,undefined); + assert.equal(cleared.stats.emergencyShrinkEstimate,undefined); + }finally{store.closeQuietly();rmrf(dir);} + }); + } +} diff --git a/tests/plugin-install-opencode.test.ts b/tests/plugin-install-opencode.test.ts index 7379188a64..b3493fa8cb 100644 --- a/tests/plugin-install-opencode.test.ts +++ b/tests/plugin-install-opencode.test.ts @@ -373,3 +373,142 @@ test("#926 --with-mcp strips a stale pin from an existing entry but keeps the en assert.deepEqual(bili!.command, ["/usr/bin/node", "/opt/old/dist/mcp.js"]); assert.equal(bili!.enabled, true); }); + +// #6816: the built-in Claude Agent provider (claude-agent/*) makes no HTTP +// request from OpenCode, so the plugin's HTTP-hook routing never sees its +// traffic — install must point it at the proxy via providers.claude-agent, +// and remove must take the entry back out. RED first: no implementation yet. + +const CLAUDE_AGENT_URL = "http://127.0.0.1:18787/bili/https://api.anthropic.com"; + +type OcProvidersCfg = { + plugin?: unknown; + plugins?: unknown; + providers?: Record } & Record>; +}; + +function withOc2x(t: import("node:test").TestContext, initial: OcProvidersCfg, version = "2.3.0"): { file: string; shimDir: string; read: () => OcProvidersCfg } { + const prevXdg = process.env.XDG_CONFIG_HOME; + const prevState = process.env.XDG_STATE_HOME; + const prevOpen = process.env.OPENCODE_CONFIG; + const prevBin = process.env.BILI_CLIENT_BIN; + delete process.env.OPENCODE_CONFIG; + const xdg = tempDir("bili-oc-6816-"); + const state = tempDir("bili-oc-6816-state-"); + process.env.XDG_CONFIG_HOME = xdg; + process.env.XDG_STATE_HOME = state; + const bin = path.join(xdg, "fake-opencode"); + fs.writeFileSync(bin, `#!/bin/sh\necho 'opencode ${version}'\n`); + fs.chmodSync(bin, 0o755); + process.env.BILI_CLIENT_BIN = bin; + t.after(() => { + if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME; + else process.env.XDG_CONFIG_HOME = prevXdg; + if (prevState === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = prevState; + if (prevOpen === undefined) delete process.env.OPENCODE_CONFIG; + else process.env.OPENCODE_CONFIG = prevOpen; + if (prevBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevBin; + rmrf(xdg); + rmrf(state); + }); + const file = path.join(xdg, "opencode", "opencode.json"); + const shimDir = path.join(xdg, "opencode", "plugins", "billion-context"); + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync(file, JSON.stringify(initial, null, 2)); + return { file, shimDir, read: () => JSON.parse(fs.readFileSync(file, "utf8")) as OcProvidersCfg }; +} + +function claudeAgentBaseUrl(cfg: OcProvidersCfg): unknown { + return cfg.providers?.["claude-agent"]?.settings?.baseURL; +} + +test("#6816 install on a 2.x host routes the built-in Claude Agent provider through the live lane port", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const { read } = withOc2x(t, {}); + const out = pluginInstall("opencode"); + assert.match(out, /claude-agent baseURL -> http:\/\/127\.0\.0\.1:18787\/bili\/https:\/\/api\.anthropic\.com/); + assert.equal(claudeAgentBaseUrl(read()), CLAUDE_AGENT_URL); + + const again = pluginInstall("opencode"); + assert.match(again, /claude-agent baseURL present/); + assert.equal(claudeAgentBaseUrl(read()), CLAUDE_AGENT_URL, "idempotent — no rewrite on second install"); +}); + +test("#6816 install preserves other providers and existing claude-agent settings", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const { read } = withOc2x(t, { providers: { anthropic: { settings: { apiKey: "k" } }, "claude-agent": { settings: { model: "opus" } } } }); + pluginInstall("opencode"); + const cfg = read(); + assert.deepEqual(cfg.providers?.anthropic, { settings: { apiKey: "k" } }, "foreign provider untouched"); + assert.deepEqual(cfg.providers?.["claude-agent"]?.settings, { model: "opus", baseURL: CLAUDE_AGENT_URL }, "existing settings kept, baseURL added"); +}); + +test("#6816 install leaves a foreign claude-agent baseURL untouched", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const foreign = "https://proxy.example.com/anthropic"; + const { read } = withOc2x(t, { providers: { "claude-agent": { settings: { baseURL: foreign } } } }); + const out = pluginInstall("opencode"); + assert.match(out, /claude-agent baseURL untouched/); + assert.equal(claudeAgentBaseUrl(read()), foreign, "user's own proxy must never be clobbered"); +}); + +test("#6816 install re-pins a stale bili baseURL port to the live lane port", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const stale = "http://127.0.0.1:18788/bili/https://api.anthropic.com"; + const { read } = withOc2x(t, { providers: { "claude-agent": { settings: { baseURL: stale } } } }); + const out = pluginInstall("opencode"); + assert.match(out, /claude-agent baseURL re-pinned/); + assert.equal(claudeAgentBaseUrl(read()), CLAUDE_AGENT_URL); +}); + +test("#6816 install on a 1.x host does not write the provider entry", (t) => { + // 1.x has no built-in Claude Agent provider — the entry would be dead config. + const { file } = withOcConfig(t, {}); + const out = pluginInstall("opencode"); + assert.match(out, /claude-agent baseURL skipped/); + const cfg = JSON.parse(fs.readFileSync(file, "utf8")) as Record; + assert.equal(cfg.providers, undefined); +}); + +test("#6816 remove deletes only a bili baseURL and prunes empty parents", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const { read } = withOc2x(t, { providers: { anthropic: { settings: { apiKey: "k" } } } }); + pluginInstall("opencode"); + assert.equal(claudeAgentBaseUrl(read()), CLAUDE_AGENT_URL); + const rem = pluginRemove("opencode"); + assert.match(rem, /claude-agent baseURL removed/); + const cfg = read(); + assert.equal(cfg.providers?.["claude-agent"], undefined, "empty provider pruned"); + assert.deepEqual(cfg.providers?.anthropic, { settings: { apiKey: "k" } }, "foreign provider survives remove"); +}); + +test("#6816 remove leaves a foreign claude-agent baseURL in place", (t) => { + if (process.platform === "win32") { + t.skip("probe stub is a POSIX shell script"); + return; + } + const foreign = "https://proxy.example.com/anthropic"; + const { file, shimDir, read } = withOc2x(t, { providers: { "claude-agent": { settings: { baseURL: foreign } } } }); + // Give remove something installed to chew on: write the plugin entry in. + fs.writeFileSync(file, JSON.stringify({ plugins: [shimDir], providers: { "claude-agent": { settings: { baseURL: foreign } } } }, null, 2)); + const out = pluginRemove("opencode"); + assert.equal(claudeAgentBaseUrl(read()), foreign, "foreign baseURL is the user's, not ours to remove"); + assert.doesNotMatch(out, /claude-agent baseURL removed/); +}); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 9217adfb35..d851bec4ef 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -16,6 +16,8 @@ import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts" import { _resetSessionsForTest, listSessions, SPLIT_CANARY_FRESH_MS, splitSessionWarnings, type Session } from "../src/session.ts"; import { _setForTest } from "../src/registry.ts"; import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { outputBudgetField as budgetFieldFromBudget, readOutputBudget as readFromBudget, writeOutputBudget as writeFromBudget } from "../src/server/budget.ts"; +import { outputBudgetField as budgetFieldFromSide, readOutputBudget as readFromSide, writeOutputBudget as writeFromSide } from "../src/server/side-request.ts"; // #2170 measure 2: behavioral invariants of the #388 side lane, on the dsh // plugin wire (the host whose regression started this — #2156/#2157/#2164). @@ -329,6 +331,17 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 } }); +// (#6752 / #1440 P2 cut 2): the output-budget field plumbing moved out of the +// side-lane engine into budget.ts so the prepare modules can use it without +// reaching into side-request.js. side-request.ts re-exports it for the existing +// importers — pin the identity so the re-export and the definition can never +// silently diverge (a broken re-export would break every importer in one step). +test("(F2) output-budget plumbing re-export identity (#6752 / #1440 P2 cut 2)", () => { + assert.equal(budgetFieldFromSide, budgetFieldFromBudget, "outputBudgetField re-export is identity"); + assert.equal(readFromSide, readFromBudget, "readOutputBudget re-export is identity"); + assert.equal(writeFromSide, writeFromBudget, "writeOutputBudget re-export is identity"); +}); + // (#1440 P2 cut 3): the extracted pipeline module OWNS the mid-pipeline lane // gates, so it legitimately imports side-request.js — unlike (F)'s modules. // What must not move or appear: the engine's internals (threshold constants,