Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ billion-context/
4. **Tee logger**: all proxy logs go through `src/logger.ts` (file + stderr). Do NOT use `console.error` in server-side modules — use `loggerLog()`.
5. **acp-kernel MUST be pinned to an exact version** (e.g. `"acp-kernel": "0.0.17"`, NEVER `"^0.0.17"`). Because acp-kernel is a build-time dependency that tsup bundles inline into `dist`, a caret range makes the resolved version drift if `package-lock.json` is regenerated or absent, breaking reproducible builds. When bumping acp-kernel: set the exact version in `package.json`, run `npm install` to refresh the lockfile, then rebuild. The `package-lock.json` is committed and kept in sync.
6. **Two compression modes with different summary carriers** — `pluginMode` (the `x-bili-plugin` header / registered agent, e.g. `bili pi`) means the ACP-native agent OWNS compression: it executes `compress` locally, the call+result live in its own re-sent history, and the summary carrier on the wire is the **tool call** (the proxy suppresses tool + nudge injection; the agent's view never renders the kernel's `acp_summary`). Proxy mode (plain client, no header) means the proxy executes `compress` server-side: the tool call is ephemeral (never enters the client's history) and preflight blocks have none, so the summary carrier is the **`acp_summary` message**, which the kernel renders as role `system` but `systemToUser` (`src/util.ts`) re-voices as a **`user` message** (leaving it at its anchor) so strict backends (SGLang: exactly one system at index 0, #377) accept it and the head system message stays byte-stable for the prefix cache. The mode is decided per request and bound per session (`session.metadata.pluginAgent`, sticky, upgrade-only). See README "Two compression modes".
7. **修复重启、切模型或失败重试时**:`plugin-conversations` 也包含普通缓存键映射,不能据此认定插件身份;保留旧映射的压缩状态,并以原始历史边界约束重算后的可压缩范围,回归入口是 `tests/subagent-namespace.test.ts` 和 `tests/preflight-fail-fast.test.ts`。

### Kernel Contract: Message Ids Are Never Reused

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

### Fixes



- **Omp compression survives model switches and restored session mappings**: only an actual `omp` registration or persisted plugin ownership bypasses instruction-based subagent partitioning; a plain `prompt_cache_key` does not. Existing conversation mappings restore their original session and compression blocks instead of creating an empty bare-id session. Failed preflight freezes the original history boundary and recomputes remaining viable ranges after each fold, including soft-protected history when needed, without absorbing turns appended during the failure window.
- **Preflight no longer wedges on a stale usage baseline while the prepared payload fits (#739 follow-up)**: the preflight trigger floors on `session.stats.lastInputTokens`, and after an earlier as-is forward under preflight (#300) that measured baseline can stay pinned above the window — a live session kept a 257,613-token floor against a 256,000 window while its real prepared payload was ~127k. Every turn then re-ran the fold walk on ranges the payload no longer needed: three 60s non-stream summarization attempts (~3.4min stall), an upstream 500 → structured 502, a 5-minute dead-end cooldown, repeat. The trigger now forwards as-is when a known-baseline session's payload estimate fits yet its measured baseline sits above the window (#301: as-is is safe; the next usage report self-heals the baseline). Inside-window folding is untouched — the codex headroom target still walks fitting 90-100% payloads. `tests/preflight-fail-fast.test.ts` pins it: fitting payload + stale floor above the window → forwarded with zero summarization calls.
- **Claude Code subagent requests no longer queue behind the main turn's lock (#970)**: Claude Code stamps the SAME `x-claude-code-session-id` on every model request of a session — background subagents (Task tool) included — so main turn and all subagents landed on ONE session and serialized on `withSessionLock` (which covers prepare + forward + the whole streamed response). A slow subagent first token head-of-line-blocked the main turn (and vice versa) for the full upstream latency; observed live as 116-142s first-byte stalls when two subagents launched concurrently. Discrimination is two-signal, both verified against live Claude Code 2.1.274 traffic (`-p` mode, general-purpose AND custom agent types like Explore): (1) the request carries `x-claude-code-agent-id` and/or `x-claude-code-parent-agent-id` — the parent header alone is NOT reliable (`-p` general-purpose subagents send it, custom agent types do not; main turns carry agent-id only from their second request on); (2) the MAIN agent's system always contains a block starting with "You are Claude Code…" (interactive) or "You are an interactive agent…" (`-p`/SDK), while a subagent's system blocks are its agent definition ("This session is a background job…", "You are a file search specialist for…", …) — no stable positive marker exists across agent types, so the check is negative (none of the main prefixes may match). The anthropic identity branch derives `<id>|sub:<agent-id>` only when BOTH signals say subagent (`claudeSubagentAgentId`/`claudeSubagentSplit` in `src/session-id.ts`, mirroring the codex #316 / Responses `subagentNamespace` precedents): each subagent gets its own lock chain and its own compression state (a subagent is its own conversation — same isolation semantics as #150). Failure degrades in the safe direction: a future rewording of the main prompts only stops the split (back to serial), never mis-splits the main session. `anthropicIdentity` itself is NOT rewritten — `affinityToken`/`clientLabel` keep the raw value, so upstream prefix caches and the UI label stay continuous across main and subagent sessions. Two guards keep the plugin layer honest: a split session's first request cannot claim the ROOT's pending register (#317 mirror), and `recordPluginSession` keys a subagent session under its split id instead of the raw conversation value — the single-valued conversations map used to flip between main and subagent sessions on every interleaved request, breaking `/acp` lookups and MCP tool routing. Older Claude Code (no headers), other agents, and curl are untouched (no split, previous behavior). Opt out with `BILI_SUBAGENT_SPLIT=0` or `"subagentSplit": false` in the config file. Tests pin: two-signal discrimination across the captured system shapes, stable namespaces, session isolation with main-identity stability, a deterministic no-stall case (upstream gates the main stream 1200ms; the subagent's first byte must beat it by a wide margin — pre-fix it arrives after, reproduced at 1238ms), and the off-switch restoring single-session behavior. End-to-end verified against live traffic: two concurrent Task subagents through the proxy land on `…|sub:<agent-id>` sessions in the log and complete in parallel.

### Features
Expand Down
12 changes: 11 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url";
import type { ServerResponse } from "node:http";
import fs from "node:fs";
import path from "node:path";
import { acquireInFlight, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js";
import { acquireInFlight, effectiveConfig, findSessionByCanonicalId, getSession, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js";
import { ABSORB_TOOL, ABSORB_TOOL_NAME, ABSORB_TOOL_OPENAI, ABSORB_TOOL_RESPONSES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, PROXY_TOOL_NAMES, SEARCH_CONTEXT_CONVERSATION_ID_PARAM, SEARCH_CONTEXT_TOOL_NAME } from "./compress-tool.js";
import { effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js";
import { executeProxyTool } from "./loop/core.js";
Expand Down Expand Up @@ -240,6 +240,16 @@ export function recordPluginSession(conversationId: string, sessionId: string):
scheduleSaveConversations();
}

/** Omp assigns each subagent its own UUID; a plain prompt_cache_key does not.
* Reuse the mapped session, including an existing |sub: namespace, rather
* than creating an empty bare-id session when the model changes. */
export function registeredPluginSessionId(conversationId: string): string | undefined {
const mappedId = conversations.get(conversationId)?.sessionId;
const mapped = mappedId ? getSession(mappedId) : undefined;
const agent = registeredIds.get(conversationId) ?? mapped?.metadata.pluginAgent;
return agent === "omp" ? mappedId ?? conversationId : undefined;
}

/** Keep the last prepare()'s view for a plugin session so tool-API execution
* sees the exact refs the model was shown (mirrors the wire-mode loop, which
* runs executeProxyTool against prepared.processedMessages). */
Expand Down
38 changes: 32 additions & 6 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16;
// incident class behind #868 (a 1.39x-window payload); it is a fixed depth,
// not scaled to the overshoot — scaling it is a separate design question.

// Freeze the original history boundary, not a worklist: every successful fold
// changes which ranges remain viable. Failure-window turns stay outside it.
function readPreflightPin(metadata: Record<string, unknown>): string | undefined {
const pin = metadata.preflightPin;
if (typeof pin !== "object" || pin === null) return undefined;
const endRef = (pin as Record<string, unknown>).endRef;
return typeof endRef === "string" && /^m\d+$/.test(endRef) ? endRef : undefined;
}

export type PreflightProtocol = "anthropic" | "openai" | "responses";

export interface PreflightDeps {
Expand Down Expand Up @@ -701,6 +710,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
let target = Math.min(limit, deps.compressionTarget ?? limit);
const result: PreflightResult = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0), rangesRemaining: 0, fitsWindow: true };
if (limit <= 0) return result;
let watermark = readPreflightPin(deps.session.metadata);
const budget = Math.max(MIN_CHUNK_TOKENS, Math.floor(limit * CHUNK_FRACTION));
// applyCompression rejects ranges below config.compress.minCompressRange
// chars, so never spend a summarization call on a chunk that can't apply.
Expand Down Expand Up @@ -772,7 +782,20 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
result.payloadEstimate = estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0);
if (startTokens < 0) startTokens = currentTokens;
if (currentTokens < target) break;
const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []);
const maps = refMaps(messages, deps.session.state);
if (!watermark) {
for (const ref of maps.idxToRef.values()) {
if (!watermark || refNum(ref) > refNum(watermark)) watermark = ref;
}
}
const boundary = refNum(watermark ?? "");
let endRef: string | undefined;
for (const ref of maps.idxToRef.values()) {
if (refNum(ref) <= boundary && (!endRef || refNum(ref) > refNum(endRef))) endRef = ref;
}
const ranges = viableRanges(turn.nudge?.compressibleRanges ?? [])
.filter((range) => endRef !== undefined && refNum(range.startRef) <= boundary)
.map((range) => ({ startRef: range.startRef, endRef: refNum(range.endRef) > boundary ? endRef! : range.endRef }));
rangesRemaining = ranges.length;
if (ranges.length === 0) {
// #330: nothing foldable outside the soft-protected recent zone.
Expand Down Expand Up @@ -808,9 +831,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
if (budgetHit) break;
const skipKey = `${range.startRef}:${range.endRef}`;
if (skipSet.has(skipKey)) continue;
const { refToIdx } = refMaps(messages, deps.session.state);
const startIdx = refToIdx.get(range.startRef);
const endIdx = refToIdx.get(range.endRef);
const startIdx = maps.refToIdx.get(range.startRef);
const endIdx = maps.refToIdx.get(range.endRef);
if (startIdx === undefined || endIdx === undefined || startIdx > endIdx) {
skipSet.add(skipKey);
continue;
Expand All @@ -836,7 +858,6 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
const span = spans.pop();
if (!span) break;
const [cs, ce] = span;
const maps = refMaps(messages, deps.session.state);
const startRef = maps.idxToRef.get(cs);
const endRef = maps.idxToRef.get(ce);
if (!startRef || !endRef) continue;
Expand Down Expand Up @@ -1008,6 +1029,12 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
failure = { kind: "exhausted", detail: `the compress budget was exhausted after ${MAX_PREFLIGHT_ROUNDS} rounds${unusableNote}` };
}
}
result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit;
if (result.fitsWindow) {
delete deps.session.metadata.preflightPin;
} else if (failure && failure.kind !== "aborted" && watermark) {
deps.session.metadata.preflightPin = { endRef: watermark };
}
if (result.compressedRanges > 0) {
// #857: never persist the IMAGE FLOOR into the usage baseline — images
// are billed by the upstream and every fit/clamp gate adds their
Expand All @@ -1024,6 +1051,5 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
result.rangesRemaining = rangesRemaining;
result.savedTokens = Math.max(0, startTokens - currentTokens);
if (currentTokens >= limit) result.failure = failure;
result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit;
return result;
}
Loading