From c68313eb0c8b0875320de2301444c7da20aaf907 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 14:37:58 +0800 Subject: [PATCH 1/4] feat: neutralize TRAE Code native auto-compact instruction (#2411) Stage A of the owner-triaged plan: when bili injects its own compression surface, remove the host's conflicting auto-compact instruction line from the system prompt so the model sees exactly one compression philosophy. Tracked sentence pinned as a versioned constant with drift tolerance (shape drift -> passthrough + warn). No config surface (behavior first per triage). --- src/server/prepare-openai.ts | 20 +++ src/server/traecode-compact.ts | 97 ++++++++++++ tests/traecode-native-compact.test.ts | 203 ++++++++++++++++++++++++++ 3 files changed, 320 insertions(+) create mode 100644 src/server/traecode-compact.ts create mode 100644 tests/traecode-native-compact.test.ts diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 0767039e0b..e2492a64bf 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -35,6 +35,7 @@ import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js"; import { effectiveAbsorbBlock } from "./prepare-responses.js"; +import { stripTraeCodeNativeCompactInstruction } from "./traecode-compact.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; @@ -103,6 +104,25 @@ export async function prepareOpenai( const markerCounts = stripRawStage(parsed, "openai"); logRawStripCounts(markerCounts, log, sessionId); + // #2411 Phase 1: TRAE Code's native auto-compact instruction ("the system + // will automatically compress prior messages…") reaches the model in the + // SAME request as bili's injected compress philosophy — two mutually + // exclusive "who compresses" stories. Neutralize it exactly when bili + // presents its own compression surface (injectTools), so the model sees one + // philosophy; runs BEFORE openaiToCore so kernel state and every rebuilt + // view carry the clean system. Evidence-permitlist match on the tracked + // sentence (src/server/traecode-compact.ts). No opt-out knob for now — + // behavior first, escape hatch only if users ask (dsh-guard rollout order). + if (injectTools) { + const trae = stripTraeCodeNativeCompactInstruction(parsed.messages); + if (trae.neutralized > 0 && session.metadata.traeCompactNeutralized !== true) { + session.metadata.traeCompactNeutralized = true; + log("info", `[${sessionId}] traecode native auto-compact instruction neutralized (${trae.neutralized} system message line(s)) — single compression philosophy now (#2411)`); + } else if (trae.shapeDrift && session.metadata.traeCompactShapeDrift !== true) { + session.metadata.traeCompactShapeDrift = true; + log("warn", `[${sessionId}] traecode tracked compact sentence present but line shape drifted — NOT neutralized; refresh TRAE_NATIVE_COMPACT_SENTENCE from a fresh dump (#2411)`); + } + } try { // Kernel 0.0.37 hoists the contiguous leading system/developer prefix diff --git a/src/server/traecode-compact.ts b/src/server/traecode-compact.ts new file mode 100644 index 0000000000..952fe0e6b6 --- /dev/null +++ b/src/server/traecode-compact.ts @@ -0,0 +1,97 @@ +// #2411 Phase 1: TRAE Code native auto-compact instruction neutralizer. +// +// TRAE Code (ByteDance's closed-source AI IDE; no plugin system) ships its own +// native context compression and tells the model about it in its system prompt +// with the tracked sentence below: a bullet at char offset 1270 of a single +// 42,385-char role=system string on OpenAI Chat Completions +// (POST /v1/chat/completions). Routed through bili in proxy mode, that sentence +// reaches the model in the SAME request as bili's injected COMPRESS_PHILOSOPHY +// + acp tool surface — two mutually exclusive "who compresses" stories, and if +// the native compaction ever lands it rewrites history outside the kernel's +// knowledge, destroying the fold substrate (same mechanism family as dsh #2360, +// detection already in src/fold-reconcile.ts). +// +// The neutralizer removes the tracked bullet line from the system text exactly +// when bili presents its own compression surface into this request (the +// prepare-openai injectTools gate), so the model sees one philosophy. +// +// Evidence-permitlist discipline (KDD #9 / #970 precedent): the trigger is the +// exact tracked sentence itself — no UA or shape heuristics (at bili ingress +// the client's real headers terminate at TraeCode's sidecar; only +// Go-http-client is visible). Stability was verified by the reporter across 4 +// dumps / 2 sessions (#2411): system sha256 prefix e477d92761e39731 byte-stable +// within and across sessions, so the deterministic removal keeps each session's +// head prefix byte-stable (prefix-cache neutral). If a future release rewords +// the sentence, the match simply misses and behavior degrades to the +// pre-fix passthrough — refresh TRAE_NATIVE_COMPACT_SENTENCE from a fresh dump. +// +// Line-shape guard: we delete only when the ENTIRE line is exactly a markdown +// bullet carrying the sentence (" - "). A future reflow that embeds +// the sentence mid-line sets shapeDrift instead of guessing at host-prompt +// surgery; the caller warns once per session. Parts-array system content +// degrades to no-op (evidence covers the plain-string form only). +// +// Phase 2 (trigger takeover if Case A / recovery if Case B) awaits a capture of +// a natural native-compaction event (#2411); this module is Phase 1 only. + +export const TRAE_NATIVE_COMPACT_SENTENCE = + "The system will automatically compress prior messages in your conversation as it approaches context limits. This means your conversation with the user is not limited by the context window."; + +const BULLET_PREFIX = /^\s*[-*+]\s+/; + +interface TraeNeutralizeResult { + text: string; + removed: number; + /** tracked sentence present but not alone on its bullet line — NOT modified */ + shapeDrift: boolean; +} + +/** Remove every line that is exactly a bullet carrying the tracked sentence. */ +export function neutralizeTraeNativeCompactInstruction(systemText: string): TraeNeutralizeResult { + let removed = 0; + let shapeDrift = false; + let text = systemText; + for (;;) { + const idx = text.indexOf(TRAE_NATIVE_COMPACT_SENTENCE); + if (idx === -1) break; + const lineStart = text.lastIndexOf("\n", idx - 1) + 1; + const lineEndRaw = text.indexOf("\n", idx + TRAE_NATIVE_COMPACT_SENTENCE.length); + const lineEnd = lineEndRaw === -1 ? text.length : lineEndRaw; + const line = text.slice(lineStart, lineEnd); + const m = line.match(BULLET_PREFIX); + if (!m || line.slice(m[0].length).trim() !== TRAE_NATIVE_COMPACT_SENTENCE) { + shapeDrift = true; + break; + } + // delete the whole line including its trailing newline (or to EOF) + text = text.slice(0, lineStart) + (lineEndRaw === -1 ? "" : text.slice(lineEndRaw + 1)); + removed++; + } + return { text, removed, shapeDrift }; +} + +/** + * Neutralize the tracked instruction in place across an openai messages array. + * Only role=system messages with STRING content are touched (see module + * header). Returns how many lines were removed and whether any message showed + * the drift shape (sentence present, line not exactly the tracked bullet). + */ +export function stripTraeCodeNativeCompactInstruction(messages: unknown): { neutralized: number; shapeDrift: boolean } { + if (!Array.isArray(messages)) return { neutralized: 0, shapeDrift: false }; + let neutralized = 0; + let shapeDrift = false; + for (const rec of messages) { + if (rec === null || typeof rec !== "object") continue; + const msg = rec as Record; + if (msg.role !== "system") continue; + if (typeof msg.content !== "string") continue; + if (!msg.content.includes(TRAE_NATIVE_COMPACT_SENTENCE)) continue; + const r = neutralizeTraeNativeCompactInstruction(msg.content); + if (r.removed > 0) { + msg.content = r.text; + neutralized += r.removed; + } + if (r.shapeDrift) shapeDrift = true; + } + return { neutralized, shapeDrift }; +} diff --git a/tests/traecode-native-compact.test.ts b/tests/traecode-native-compact.test.ts new file mode 100644 index 0000000000..61cace395f --- /dev/null +++ b/tests/traecode-native-compact.test.ts @@ -0,0 +1,203 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { TRAE_NATIVE_COMPACT_SENTENCE, neutralizeTraeNativeCompactInstruction, stripTraeCodeNativeCompactInstruction } from "../src/server/traecode-compact.js"; + +process.env.NODE_ENV = "test"; + +// #2411 Phase 1: TRAE Code's native auto-compact instruction must not reach +// the model in the same request as bili's injected compression philosophy. +// Fixtures reproduce the reporter's verbatim local context (#2411 floor 4): +// bullet at char offset 1270 of the single 42,385-char role=system string, +// system sha256 prefix e477d92761e39731 stable across 4 dumps / 2 sessions. + +const PREV_BULLET = "…terminal status, linter errors, and current mode. This information is provided in case it is helpful to the task."; +const SYSTEM_HEAD = `${PREV_BULLET}\n`; +const TRACKED_LINE = ` - ${TRAE_NATIVE_COMPACT_SENTENCE}`; +const SYSTEM_TAIL = `\n\n# Doing tasks\nbe decisive.`; +const TRAECODE_SYSTEM = SYSTEM_HEAD + TRACKED_LINE + SYSTEM_TAIL; +const EXPECTED_CLEAN = `${SYSTEM_HEAD}\n# Doing tasks\nbe decisive.`; + +test("neutralizer: removes exactly the tracked bullet line, head/tail bytes intact", () => { + const r = neutralizeTraeNativeCompactInstruction(TRAECODE_SYSTEM); + assert.equal(r.removed, 1); + assert.equal(r.shapeDrift, false); + assert.equal(r.text, EXPECTED_CLEAN); + assert.ok(r.text.startsWith(SYSTEM_HEAD), "head prefix byte-stable (cache-neutral)"); + assert.ok(!r.text.includes(TRAE_NATIVE_COMPACT_SENTENCE)); +}); + +test("neutralizer: idempotent — second pass removes nothing", () => { + const once = neutralizeTraeNativeCompactInstruction(TRAECODE_SYSTEM); + const twice = neutralizeTraeNativeCompactInstruction(once.text); + assert.equal(twice.removed, 0); + assert.equal(twice.text, once.text); +}); + +test("neutralizer: no tracked sentence → byte-identical passthrough", () => { + const r = neutralizeTraeNativeCompactInstruction(SYSTEM_HEAD + "# Doing tasks\ntext"); + assert.equal(r.removed, 0); + assert.equal(r.shapeDrift, false); + assert.equal(r.text, SYSTEM_HEAD + "# Doing tasks\ntext"); +}); + +test("neutralizer: sentence embedded mid-line → shapeDrift, NOT modified", () => { + const drifted = SYSTEM_HEAD + ` - ${TRAE_NATIVE_COMPACT_SENTENCE} Additionally, context may reset.\n\n# Doing tasks`; + const r = neutralizeTraeNativeCompactInstruction(drifted); + assert.equal(r.removed, 0); + assert.equal(r.shapeDrift, true); + assert.equal(r.text, drifted); +}); + +test("neutralizer: sentence without a bullet marker → shapeDrift, NOT modified", () => { + const drifted = SYSTEM_HEAD + `${TRAE_NATIVE_COMPACT_SENTENCE}\n\n# Doing tasks`; + const r = neutralizeTraeNativeCompactInstruction(drifted); + assert.equal(r.removed, 0); + assert.equal(r.shapeDrift, true); + assert.equal(r.text, drifted); +}); + +test("neutralizer: CRLF line endings handled", () => { + const crlf = `A\r\n - ${TRAE_NATIVE_COMPACT_SENTENCE}\r\n\r\nB`; + const r = neutralizeTraeNativeCompactInstruction(crlf); + assert.equal(r.removed, 1); + assert.equal(r.shapeDrift, false); + assert.equal(r.text, `A\r\n\r\nB`); +}); + +test("strip: system string rewritten in place, other messages untouched", () => { + const user = { role: "user", content: "hi" }; + const assistant = { role: "assistant", content: "yo" }; + const msgs = [ + { role: "system", content: TRAECODE_SYSTEM }, + user, + assistant, + { role: "tool", content: "result" }, + ]; + const r = stripTraeCodeNativeCompactInstruction(msgs); + assert.equal(r.neutralized, 1); + assert.equal(r.shapeDrift, false); + assert.equal((msgs[0] as { content: string }).content, EXPECTED_CLEAN); + assert.deepEqual(user.content, "hi"); + assert.deepEqual(assistant.content, "yo"); +}); + +test("strip: parts-array system content degrades to no-op (evidence covers string form only)", () => { + const part = { type: "text", text: TRAECODE_SYSTEM }; + const msgs = [{ role: "system", content: [part] }, { role: "user", content: "hi" }]; + const r = stripTraeCodeNativeCompactInstruction(msgs); + assert.equal(r.neutralized, 0); + assert.equal(r.shapeDrift, false); + assert.equal(part.text, TRAECODE_SYSTEM); +}); + +test("strip: non-array input → zero", () => { + assert.deepEqual(stripTraeCodeNativeCompactInstruction(undefined), { neutralized: 0, shapeDrift: false }); + assert.deepEqual(stripTraeCodeNativeCompactInstruction({}), { neutralized: 0, shapeDrift: false }); +}); + +function sseLine(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +function startMockUpstream(bodies: string[]): http.Server { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + bodies.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "ok" } }] })); + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 2 } })); + res.write("data: [DONE]\n\n"); + res.end(); + }); + }); + server.listen(0, "127.0.0.1"); + return server; +} + +async function postChat(url: string, messages: Array<{ role: string; content: string }>): Promise { + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-test", stream: true, max_tokens: 1024, messages }), + }); + if (!res.ok) assert.fail(`HTTP ${res.status}: ${await res.text()}`); + await res.arrayBuffer(); +} + +test("e2e: TraeCode-shaped chat-completions request forwards ONE compression philosophy (#2411)", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: string[] = []; + const upstream = startMockUpstream(bodies); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: {} as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + try { + // the conflicting native instruction is gone, the acp tool surface is + // present, surrounding system bytes are intact + await postChat(url, [ + { role: "system", content: TRAECODE_SYSTEM }, + { role: "user", content: "first question" }, + { role: "assistant", content: "first answer" }, + { role: "user", content: "second question" }, + ]); + assert.equal(bodies.length, 1, "request reached upstream"); + const sent1 = JSON.parse(bodies[0]) as { messages: Array<{ role: string; content: string }>; tools?: Array<{ function?: { name?: string } }> }; + const sys1 = sent1.messages.find((m) => m.role === "system")?.content ?? ""; + assert.ok(!sys1.includes(TRAE_NATIVE_COMPACT_SENTENCE), "native auto-compact instruction stripped"); + assert.ok(sys1.startsWith(PREV_BULLET), "system head prefix intact"); + assert.ok(sys1.includes("# Doing tasks"), "system tail intact after removal"); + assert.ok(sent1.tools?.some((t) => t.function?.name === "compress"), "acp compress tool injected in the same request"); + + // continuation of the same conversation stays clean + await postChat(url, [ + { role: "system", content: TRAECODE_SYSTEM }, + { role: "user", content: "first question" }, + { role: "assistant", content: "first answer" }, + { role: "user", content: "second question" }, + { role: "assistant", content: "second answer" }, + { role: "user", content: "third question" }, + ]); + const sent2 = JSON.parse(bodies[1]) as { messages: Array<{ role: string; content: string }> }; + const sys2 = sent2.messages.find((m) => m.role === "system")?.content ?? ""; + assert.ok(!sys2.includes(TRAE_NATIVE_COMPACT_SENTENCE), "continuation request stripped too"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); From 366c3902d34ec3c59bb5bbe6e75378e363ff1ba9 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 15:55:16 +0800 Subject: [PATCH 2/4] fix: stand down ACP compression surface on host-native compaction requests (#2411) When a host (unified Trae client) fires its own auto-compact, it sends its summarization template as the last user message on the normal chat endpoint. bili used to inject its own compression philosophy + compress tool + preflight folding into that same request - two compression philosophies in one turn, and bili's preflight fighting the host's compaction act. Detection is an evidence-permitlist predicate (src/server/native-compact-shape.ts): last user message string starts with the template lead anchor AND includes its tail anchor, or is trim-exact /compact (manual form from static bytecode). Fails open on any structural deviation. The compaction product shape is deliberately not matched (model free-form). On match: tool/doctrine/nudge injection off for that turn only, tags-only system note off, overhead measurement skipped (title-gen-style), preflight compression skipped (codexForge precedent); processTurn/snapshot/unannounced-rewrite detection stay active so session state continuity and the #1001/#2373 aftermath paths keep working. OpenAI lane only - no evidence of other wires yet. --- src/server.ts | 7 ++ src/server/handle.ts | 7 +- src/server/native-compact-shape.ts | 65 +++++++++++ src/server/prepare-openai.ts | 41 +++++-- tests/traecode-native-compact.test.ts | 161 ++++++++++++++++++++++++++ 5 files changed, 271 insertions(+), 10 deletions(-) create mode 100644 src/server/native-compact-shape.ts diff --git a/src/server.ts b/src/server.ts index 2dd74daee8..c6961c81a5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1743,6 +1743,13 @@ export type Prepared = { * success response was forged locally (BILI_CODEX_COMPACT=intercept + * gate passed). forward() serves `body` without contacting upstream. */ codexForge?: { kind: "endpoint" | "trigger" | "prompt"; body: string; contentType: string }; + /** #2411 Class A: set by prepareOpenai when the request IS a host-native + * compaction act (summarization template as last user message — see + * src/server/native-compact-shape.ts). The compression surface was stood + * down for this turn; preflight must skip it too — folding the same + * history the host is asking to summarize would put two compressors on + * one turn (#2432/#2373 pathology). */ + nativeCompact?: boolean; /** #2740: set when a Claude Code auto-mode classifier one-shot was answered * locally (BILI_AUTO_MODE_FORGE, default on). forward() serves `body` * without contacting upstream — zero egress, zero billing for the lane. */ diff --git a/src/server/handle.ts b/src/server/handle.ts index 79d088af05..f2940a8bac 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -2591,8 +2591,11 @@ async function handleRequest( } // A locally-served codex compaction forge never reaches an // upstream: preflight would burn summarization rounds for a - // reply that is already forged (#3008 prompt form). - if (!countTokens && !responsesCompact && !prepared.codexForge && !prepared.threadsLane) { + // reply that is already forged (#3008 prompt form). #2411 Class + // A: a host-native compaction request IS the compression act — + // folding the same history the host is asking to summarize + // would put two compressors on one turn; skip like codexForge. + if (!countTokens && !responsesCompact && !prepared.codexForge && !prepared.nativeCompact && !prepared.threadsLane) { const outcome = await preflightCompressIfNeeded( prepared, runPrepare, diff --git a/src/server/native-compact-shape.ts b/src/server/native-compact-shape.ts new file mode 100644 index 0000000000..dbaa039c34 --- /dev/null +++ b/src/server/native-compact-shape.ts @@ -0,0 +1,65 @@ +// #2411 Class A: detect host-native compaction requests on the OpenAI wire so +// bili can stand down its own compression surface for that one turn. +// +// Evidence (ezjbc live captures, #2411 thread, unified Trae client via +// sidecar -> bili, 2026-10-18~23 window): +// - 17/17 native auto-compaction requests carry the host's summarization +// template as the LAST user message; 38/40 session-store copies are +// byte-identical across 21 days / 9 models / 2 channels. Store original: +// 6,861 raw-LF bytes, sha256 d56eeae339bd8007… (reporter's correction — +// his first-posted e5a1804e… was the CRLF-normalized digest). The +// thread-pasted copy embedded in the test fixture lost 4 bytes (newlines) +// in markdown transit — hence anchors below instead of a full-text hash. +// - Both known variants (6861-char Trae mainstream, 6980-char legacy with an +// block) share line 1 and the final instruction sentence. The +// Trae-only "" line and the / tags are +// deliberately NOT anchors: variant-specific, and model free-form products +// never matched them anyway (#2849's phrase pair has zero hits here). +// - Secondary anchor "/compact": manual slash-command form from static +// bytecode (module 79484 QP predicate); zero occurrences in the 17 live +// samples, kept as a cheap secondary signal for manual triggers. +// - The compaction PRODUCT (re-injected head-of-history user turn such as +// "## 1. Primary Request and Intent") is deliberately NOT matched — it is +// model free-form per the template's , so product-shape matching +// is fragile; instruction-shape matching is stable (40/40). +// +// Evidence-permitlist discipline (#970/#1104): this predicate gates ACP +// surface suppression on ONE wire family only. It fails open — any structural +// deviation (parts-array content, non-user last message, missing either +// anchor, unparseable body) leaves the request fully instrumented, exactly as +// before this module existed. + +/** First line of both known template variants (offset 0, 40/40 hits). */ +const NATIVE_COMPACT_LEAD_ANCHOR = "Your task is to create a detailed summary of the conversation so far"; +/** Final instruction sentence shared by both variants (false-positive guard). */ +const NATIVE_COMPACT_TAIL_ANCHOR = "Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response."; +/** Manual slash-command trigger form (static bytecode evidence only). */ +const NATIVE_COMPACT_SLASH_FORM = "/compact"; + +// Leading ACP render-tag prefix(es) the host may echo back verbatim — bili +// renders refs into outbound messages (renderTags), clients persist those +// bytes and resend them. Grammar mirrors src/server/chain-artifacts.ts +// (unescaped wire form); hex escapes per KDD #2. +const LEADING_ACP_TAGS = /^\s*(?:\x3cacp\s+tokens="\d+(?:\.\d+)?K?"\s+type="[^"]*"\s*\x3em\d{1,8}\x3c\/acp\x3e\s*)+/; + +/** Strip leading ACP render-tag prefix(es) plus surrounding whitespace. */ +export function stripLeadingAcpTags(text: string): string { + return text.replace(LEADING_ACP_TAGS, "").trimStart(); +} + +/** True when the request IS a host-native compaction act: the last user + * message is the host's summarization template (or the manual "/compact" + * form). See the module header for anchors and evidence. Fails open. */ +export function looksLikeNativeCompactionRequest(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const messages = (parsed as { messages?: unknown }).messages; + if (!Array.isArray(messages) || messages.length === 0) return false; + const last = messages[messages.length - 1]; + if (!last || typeof last !== "object") return false; + const rec = last as { role?: unknown; content?: unknown }; + if (rec.role !== "user" || typeof rec.content !== "string") return false; + const t = stripLeadingAcpTags(rec.content).trimEnd(); + if (t === "") return false; + if (t === NATIVE_COMPACT_SLASH_FORM) return true; + return t.startsWith(NATIVE_COMPACT_LEAD_ANCHOR) && t.includes(NATIVE_COMPACT_TAIL_ANCHOR); +} diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index e2492a64bf..8f9cc72f21 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -36,6 +36,7 @@ import { applyImageRescalePass } from "../image-rescale.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js"; import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { stripTraeCodeNativeCompactInstruction } from "./traecode-compact.js"; +import { looksLikeNativeCompactionRequest } from "./native-compact-shape.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; @@ -90,7 +91,20 @@ export async function prepareOpenai( // system prompt bytes (compress prompt added/removed) — which breaks the // provider prefix cache for every subsequent turn. const isTitleGen = maxTokens <= 200; - const shouldInject = opts.compress.injectTool && !isTitleGen; + // #2411 Class A: a host-native compaction request (the host's summarization + // template as last user message) IS a compression act — bili stands down its + // whole compression surface for that turn. Live evidence: on 11/17 captured + // compactions the outbound system was sys_char 11417 = 69 host chars + + // 11346 of bili's own compress doctrine, directly contradicting the host's + // "output only a summary" instruction, plus 6890 chars of compress tool + // schema the call must not use. Detection fails open — anchors + evidence in + // src/server/native-compact-shape.ts. + const nativeCompact = looksLikeNativeCompactionRequest(parsed); + if (nativeCompact && session.metadata.nativeCompactSuppressed !== true) { + session.metadata.nativeCompactSuppressed = true; + log("info", `[${sessionId}] host-native compaction request detected (summarization template as last user message) — ACP compression surface stood down for this turn (#2411)`); + } + const shouldInject = opts.compress.injectTool && !isTitleGen && !nativeCompact; const injectTools = shouldInject && !pluginMode; // #3177: DSH registers bili's ACP tools host-scope (invisible to the preset // agent — 0/3939 request headers carry them), so model-driven compression is @@ -215,7 +229,10 @@ export async function prepareOpenai( // backfill can never fire after a wire-shape flip. const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs) }); - if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); + // Native compaction requests carry the host's summarization system — + // a one-off side shape that must not perturb the conversation's + // fingerprint record (same rationale as the title-gen skip). + if (!isTitleGen && !nativeCompact) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. const foldCoveredBefore = session.stats.pendingFoldUsage === true @@ -350,7 +367,10 @@ export async function prepareOpenai( if (openaiSystemText) sysParts.push(openaiSystemText); let acpStandingPushed = false; if (laneDecisions.compressSurfaceOn) { sysParts.push(withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, conversationPromptSections(surface?.promptSections, externalSummaryEnabled(config))), externalSummaryEnabled(config)), visibilityMarkers), laneDecisions.mcpDialect))); acpStandingPushed = true; } - else if (!isTitleGen && !knobRenderNone()) { + // #2411 Class A: native compaction requests get NO bili system text at + // all — not even the tags-only note (the host's 69-char summarization + // system must pass through byte-stable; see shouldInject above). + else if (!isTitleGen && !nativeCompact && !knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) { sysParts.push(tagsOnly); acpStandingPushed = true; } @@ -495,15 +515,20 @@ export async function prepareOpenai( (rebuilt as Record).stream_options = { include_usage: true }; } // #532: title-gen side requests carry their own tiny system and would - // clobber the conversation's measured overhead — skip them. - if (!isTitleGen && openaiOutboundSystem !== undefined) { + // clobber the conversation's measured overhead — skip them. #2411 Class A: + // native compaction requests are the same class (69-char host system, zero + // tools) — measuring them would record near-zero overhead against a full + // conversation. + if (!isTitleGen && !nativeCompact && openaiOutboundSystem !== undefined) { session.metadata.systemPromptTokens = countSystemAndToolsTokens(openaiOutboundSystem, toolsOut); } // #728: record this turn's outbound payload upper bound as the fallback // token source for upstreams that never report usage (see effectiveTokenCount). // Title-gen side requests are skipped like the overhead row above — their - // tiny payload would clobber the conversation's measurement. - if (!isTitleGen) { + // tiny payload would clobber the conversation's measurement. #2411 Class A: + // native compaction payloads are one-off outliers (full history + template) + // and would feed the k̂ learning pair a non-steady-state sample. + if (!isTitleGen && !nativeCompact) { session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) + imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin); @@ -517,7 +542,7 @@ export async function prepareOpenai( } snapshotMessages(session, originalMessages); markDirty(session); - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, nativeCompact, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } /** Append the ephemeral nudge to a Gemini `contents` array. Gemini is diff --git a/tests/traecode-native-compact.test.ts b/tests/traecode-native-compact.test.ts index 61cace395f..ecb92e9c5a 100644 --- a/tests/traecode-native-compact.test.ts +++ b/tests/traecode-native-compact.test.ts @@ -7,7 +7,9 @@ import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import type { ProxyOptions } from "../src/config.ts"; +import { createHash } from "node:crypto"; import { TRAE_NATIVE_COMPACT_SENTENCE, neutralizeTraeNativeCompactInstruction, stripTraeCodeNativeCompactInstruction } from "../src/server/traecode-compact.js"; +import { looksLikeNativeCompactionRequest, stripLeadingAcpTags } from "../src/server/native-compact-shape.js"; process.env.NODE_ENV = "test"; @@ -201,3 +203,162 @@ test("e2e: TraeCode-shaped chat-completions request forwards ONE compression phi await once(upstream, "close"); } }); + +// #2411 Class A: host-native compaction requests (the host summarization +// template as last user message) must NOT carry bili compression surface — +// not the doctrine system text, not the compress tool schema, not the nudge. +// Fixture = the live template verbatim from the reporter thread. Canonical +// store original per reporter correction: 6861 bytes raw-LF, +// sha256 d56eeae339bd80074e43d4531e23776958cb7d9d84fc2740511d29d2dc392068; +// the thread-pasted copy below lost 4 bytes (newlines) in markdown transit — +// the pin covers THIS fixture so any accidental edit fails loudly. Anchors +// are the contract, not the full hash. +const TEMPLATE_6861 = "Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.\nThis summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.\nThis summary should be in the SAME LANGUAGE as section.\n\nBefore providing your final summary,you should ensure you've covered all necessary points.\n1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:\n - The user's explicit requests and intents\n - Your approach to addressing the user's requests\n - Key decisions, technical concepts and code patterns\n - Specific details like:\n - file names\n - full code snippets\n - function signatures\n - file edits\n - Errors that you ran into and how you fixed them\n - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.\n2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.\n\nYour summary should include the following sections:\n1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail\n2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.\n3. Files and Code Sections: Document ALL files and code sections examined, modified, or created during the conversation. Use absolute paths for all files. Pay special attention to the most recent messages and include full code snippets where applicable. For each file operation, include a summary of why this file read or edit is important. PRIORITIZATION: Focus detailed code snippets and descriptions on files that show signs of ongoing development (incomplete implementations, recent modifications, files referenced in current work, or files likely needed for next steps). For completed/stable files from earlier in the conversation, provide brief mentions unless they remain relevant to current tasks.\n4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.\n5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.\n6. All user messages: Document actual user communications from the conversation history, preserving original language and wording (DO NOT translate - record exact original text verbatim). For lengthy messages, focus on key user instructions and requests. **CRITICAL: DO NOT include this current summarization request.** EXCLUDE: tool results, system-generated content, content within tags, and quoted materials. Focus on user's direct personal communication. (Note: quoted materials include GitHub issues, documentation excerpts, error messages, and other external referenced content)\n7. Pending Tasks: Outline any pending tasks that you have been explicitly asked to work on.\n8. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.\n9. Working on Plan/Spec: If you are following or working on any plan or spec, include its **file path(s)** here. If you have worked on multiple specs, include only the lastest one.\n10. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests without confirming with the user first. If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.\n11. Conversation Language: Identify the primary language the user uses for their own thoughts, questions, and instructions. Focus ONLY on the user's direct personal communication, excluding quoted content, code, documentation, system-reminder tags, and third-party materials. When mixed languages appear, prioritize the language used for personal instructions and task descriptions over quoted technical content.\n\n\n\n\n1. Primary Request and Intent:\n [Detailed description]\n\n2. Key Technical Concepts:\n - [Concept 1]\n - [Concept 2]\n - [...]\n\n3. Files and Code Sections:\n Recent/Ongoing Development (detailed):\n - [/absolute/path/to/file1.ext]:\n - Status: [Created/Modified]\n - Why important: [Relevance to current work/next steps]\n - Changes: [Summary of modifications made OR current implementation status]\n - Key code snippets: [Important implementation details - include actual code when relevant]\n - [/absolute/path/to/file2.ext]:\n - Status: [Examined]\n - Why important: [How future work depends on this file OR incomplete implementation needing attention]\n - Current state: [Brief status and key functionality summary]\n - Key code snippets: [Critical interfaces, configurations, or implementations that future work will reference]\n - [...]\n\n Stable/Completed Files (brief mentions):\n - [/absolute/path/to/file3.ext]: [Brief description of completed functionality]\n - [...]\n\n4. Errors and fixes:\n - [Detailed description of error 1]:\n - [How you fixed the error]\n - [User feedback on the error if any]\n - [...]\n\n5. Problem Solving:\n [Description of solved problems and ongoing troubleshooting]\n\n6. All user messages:\n - [User's key instructions in original language - exclude quoted content]\n - [User's direct requests and feedback in original language]\n - [Focus on user's personal communication, not system/quoted materials]\n - [NOTE: This section should NOT include the current \"create a summary\" request]\n\n7. Pending Tasks:\n - [Task 1]\n - [Task 2]\n - [...]\n\n8. Current Work:\n [Precise description of current work]\n\n9. Working on Plan/Spec (Optional):\n [File Path(s) of the plan or spec are following or working on]\n\n10. Optional Next Step:\n [Optional Next step to take]\n\n11. Conversation Language:\n Primary language: [Chinese/English/etc.] - based on user's direct personal communication, excluding quoted materials and system content\n\n\n\n\nPlease provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response."; +assert.equal(TEMPLATE_6861.length, 6857, "fixture length drift"); +assert.equal(createHash("sha256").update(TEMPLATE_6861, "utf8").digest("hex"), "eaa2c6926063d25bde6b3a30fd1ed458a4c7a139ad7b082484a5deb44d5451d3", "fixture byte-drift"); + +const HOST_SUMMARY_SYSTEM = "You are a helpful AI assistant tasked with summarizing conversations."; +const NATIVE_TOOLS = [{ type: "function", function: { name: "run_mcp", description: "mcp" } }]; + +function compactBody(lastUserContent: string): { messages: Array<{ role: string; content: string }> } { + return { messages: [ + { role: "system", content: HOST_SUMMARY_SYSTEM }, + { role: "user", content: "earlier question" }, + { role: "assistant", content: "earlier answer" }, + { role: "user", content: lastUserContent }, + ] }; +} + +test("shape: full live template as last user message → native compaction", () => { + assert.equal(looksLikeNativeCompactionRequest(compactBody(TEMPLATE_6861)), true); +}); + +test("shape: minimal lead+tail synthetic → native compaction (both variants share these)", () => { + const min = "Your task is to create a detailed summary of the conversation so far, paying close attention.\n\nPlease provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response."; + assert.equal(looksLikeNativeCompactionRequest(compactBody(min)), true); +}); + +test("shape: manual /compact form (trim-exact) → native compaction", () => { + assert.equal(looksLikeNativeCompactionRequest(compactBody("/compact")), true); + assert.equal(looksLikeNativeCompactionRequest(compactBody(" /compact ")), true); +}); + +test("shape: ACP render-tag prefix on the template is tolerated", () => { + const tagged = "\x3cacp tokens=\"1.2K\" type=\"text\"\x3em00042\x3c/acp\x3e" + TEMPLATE_6861; + assert.equal(looksLikeNativeCompactionRequest(compactBody(tagged)), true); +}); + +test("shape: lead without tail → NOT native compaction (fail open)", () => { + assert.equal(looksLikeNativeCompactionRequest(compactBody("Your task is to create a detailed summary of the conversation so far, please.")), false); +}); + +test("shape: tail without lead → NOT native compaction (fail open)", () => { + assert.equal(looksLikeNativeCompactionRequest(compactBody("Here you go. Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.")), false); +}); + +test("shape: lead+tail in a non-last message → NOT native compaction (fail open)", () => { + const body = compactBody(TEMPLATE_6861); + body.messages.push({ role: "user", content: "and now continue" }); + assert.equal(looksLikeNativeCompactionRequest(body), false); +}); + +test("shape: template carried by an assistant message → NOT native compaction (fail open)", () => { + const body = compactBody("normal turn"); + body.messages.push({ role: "assistant", content: TEMPLATE_6861 }); + assert.equal(looksLikeNativeCompactionRequest(body), false); +}); + +test("shape: parts-array content → NOT native compaction (evidence covers string form only)", () => { + const body = { messages: [{ role: "user", content: [{ type: "text", text: TEMPLATE_6861 }] }] }; + assert.equal(looksLikeNativeCompactionRequest(body), false); +}); + +test("shape: degenerate inputs fail open", () => { + assert.equal(looksLikeNativeCompactionRequest(undefined), false); + assert.equal(looksLikeNativeCompactionRequest({}), false); + assert.equal(looksLikeNativeCompactionRequest({ messages: [] }), false); + assert.equal(looksLikeNativeCompactionRequest({ messages: [{ role: "user", content: " " }] }), false); + assert.equal(looksLikeNativeCompactionRequest({ messages: [{ role: "user", content: null }] }), false); +}); + +test("shape: case-mutated lead → NOT native compaction (anchors are case-sensitive)", () => { + const lower = TEMPLATE_6861[0].toLowerCase() + TEMPLATE_6861.slice(1); + assert.equal(looksLikeNativeCompactionRequest(compactBody(lower)), false); +}); + +test("e2e: host-native compaction request forwards with ZERO bili compression surface (#2411 Class A)", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: string[] = []; + const upstream = startMockUpstream(bodies); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: {} as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + try { + // THE compaction act: host summarization system + template as last user + await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-test", stream: true, max_tokens: 1024, tools: NATIVE_TOOLS, messages: compactBody(TEMPLATE_6861).messages }), + }).then((r) => r.arrayBuffer()); + assert.equal(bodies.length, 1, "compaction request reached upstream"); + const sentA = JSON.parse(bodies[0]) as { messages: Array<{ role: string; content: string }>; tools?: Array<{ type?: string; function?: { name?: string } }> }; + const sysA = sentA.messages.find((m) => m.role === "system")?.content ?? ""; + assert.equal(sysA, HOST_SUMMARY_SYSTEM, "host summarization system passes through byte-stable (no doctrine, no tags-only note)"); + assert.ok(!bodies[0].includes("Compression Philosophy"), "no compress doctrine anywhere in the outbound body"); + assert.ok(!sentA.tools?.some((t) => t.function?.name === "compress"), "no compress tool schema added"); + assert.deepEqual(sentA.tools, NATIVE_TOOLS, "native tool list untouched"); + assert.equal(sentA.messages.length, 4, "no injected ephemeral messages (nudge/retrieval notes)"); + assert.equal(stripLeadingAcpTags(sentA.messages[3].content), TEMPLATE_6861, "template reaches upstream intact modulo bili's standard outbound render-tag/separator normalization"); + + // control: an ordinary chat request on a different session keeps the full surface + await postChat(url, [ + { role: "system", content: "You are a coding assistant." }, + { role: "user", content: "hello" }, + ]); + assert.equal(bodies.length, 2); + const sentB = JSON.parse(bodies[1]) as { messages: Array<{ role: string; content: string }>; tools?: Array<{ function?: { name?: string } }> }; + const sysB = sentB.messages.find((m) => m.role === "system")?.content ?? ""; + assert.ok(sysB.includes("Compression Philosophy"), "control request carries the doctrine (gate is per-request, not global)"); + assert.ok(sentB.tools?.some((t) => t.function?.name === "compress"), "control request gets the compress tool"); + + // continuation: the surface is NOT latched off after a compaction turn + await postChat(url, [ + { role: "system", content: "You are a coding assistant." }, + { role: "user", content: "hello" }, + { role: "assistant", content: "hi" }, + { role: "user", content: "next question" }, + ]); + const sentC = JSON.parse(bodies[2]) as { tools?: Array<{ function?: { name?: string } }> }; + assert.ok(sentC.tools?.some((t) => t.function?.name === "compress"), "surface restored on the next ordinary turn"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); From 3984067076683ecf5dadb0404a096300420bfe26 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 19:10:04 +0800 Subject: [PATCH 3/4] fix: forge local completion for host-native compaction requests (#2411) --- src/server.ts | 5 + src/server/openai-compact-forge.ts | 56 +++++++++ src/server/prepare-openai.ts | 24 +++- src/server/relay.ts | 8 ++ tests/traecode-native-compact.test.ts | 160 ++++++++++++++++++++++++-- 5 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 src/server/openai-compact-forge.ts diff --git a/src/server.ts b/src/server.ts index c6961c81a5..76a9e390c3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1750,6 +1750,11 @@ export type Prepared = { * history the host is asking to summarize would put two compressors on * one turn (#2432/#2373 pathology). */ nativeCompact?: boolean; + /** #2411 Stage B: set with nativeCompact when bili's own fold summaries are + * served AS the completion for the host-native compaction request — same + * zero-egress contract as codexForge; forward() serves `body` without + * contacting upstream. */ + nativeCompactForge?: { body: string; contentType: string }; /** #2740: set when a Claude Code auto-mode classifier one-shot was answered * locally (BILI_AUTO_MODE_FORGE, default on). forward() serves `body` * without contacting upstream — zero egress, zero billing for the lane. */ diff --git a/src/server/openai-compact-forge.ts b/src/server/openai-compact-forge.ts new file mode 100644 index 0000000000..4e9a6a3375 --- /dev/null +++ b/src/server/openai-compact-forge.ts @@ -0,0 +1,56 @@ +import { randomUUID } from "node:crypto"; + +// #2411 Stage B: local forge for host-native compaction requests on the openai +// lane (TRAE Code / unified Trae client). When the host fires its own auto-compact +// it sends its ~6.9KB summarization template as the LAST user message on the +// normal chat endpoint; bili intercepts that request (nativeCompact predicate, +// src/server/native-compact-shape.ts) and serves its OWN fold summaries as the +// completion — upstream never contacted (zero-egress contract, same class as +// codexForge). E2 capture (reporter, #2411 floor 36): the backend's response to +// this request shape is an ORDINARY chat completion — no private progress +// frames, no pseudo-tool events, terminal shape = final data frame with +// finish_reason:"stop" + usage followed by `data: [DONE]`. So the forge is a +// standard chat.completion / chat.completion.chunk pair, nothing more. The +// id prefix marks bili-forged completions for downstream greps, mirroring +// codex's fc_bili_ sentinel. + +export const OPENAI_COMPACT_ID_PREFIX = "chatcmpl_bili_"; + +interface OpenaiForgeUsage { + inputTokens: number; + outputTokens: number; + totalTokens: number; +} + +function usageJson(u: OpenaiForgeUsage): { prompt_tokens: number; completion_tokens: number; total_tokens: number } { + return { prompt_tokens: u.inputTokens, completion_tokens: u.outputTokens, total_tokens: u.totalTokens }; +} + +export function buildOpenaiCompactForgeBody( + summaryText: string, + model: string, + usage: OpenaiForgeUsage, + stream: boolean, +): { body: string; contentType: string } { + const id = `${OPENAI_COMPACT_ID_PREFIX}${randomUUID()}`; + const created = Math.floor(Date.now() / 1000); + if (!stream) { + const body = JSON.stringify({ + id, + object: "chat.completion", + created, + model, + choices: [{ index: 0, message: { role: "assistant", content: summaryText }, finish_reason: "stop" }], + usage: usageJson(usage), + }); + return { body, contentType: "application/json" }; + } + const frame = (obj: unknown) => `data: ${JSON.stringify(obj)}\n\n`; + const base = { id, object: "chat.completion.chunk" as const, created, model }; + let body = ""; + body += frame({ ...base, choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] }); + body += frame({ ...base, choices: [{ index: 0, delta: { content: summaryText }, finish_reason: null }] }); + body += frame({ ...base, choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: "stop" }], usage: usageJson(usage) }); + body += "data: [DONE]\n\n"; + return { body, contentType: "text/event-stream" }; +} diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 8f9cc72f21..87ead62147 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -37,6 +37,8 @@ import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, e import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { stripTraeCodeNativeCompactInstruction } from "./traecode-compact.js"; import { looksLikeNativeCompactionRequest } from "./native-compact-shape.js"; +import { buildOpenaiCompactForgeBody } from "./openai-compact-forge.js"; +import { codexCompactGate, mergeForgedSummaries } from "../codex-compact.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; @@ -138,6 +140,7 @@ export async function prepareOpenai( } } + let openaiTransformOk = true; try { // Kernel 0.0.37 hoists the contiguous leading system/developer prefix // OUT of the fold space: system content is host runtime state and @@ -470,6 +473,7 @@ export async function prepareOpenai( attachedRetrievalNoteIds = retrNotes.map((n) => n.id); } } catch (err) { + openaiTransformOk = false; log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); if (attachedRetrievals.length > 0) dropRetrievals(session, attachedRetrievals.map((i) => i.ref), "prepare failed; forwarded unprocessed"); processedMessages = []; @@ -542,7 +546,25 @@ export async function prepareOpenai( } snapshotMessages(session, originalMessages); markDirty(session); - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, nativeCompact, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + // #2411 Stage B: a host-native compaction request with healthy ACP state + // gets bili's own fold summaries served AS the completion — upstream never + // contacted (zero-egress contract, codexForge class). E2 capture proved the + // backend answer to this request shape is an ordinary chat completion, so + // the forge is a standard SSE/JSON pair (src/server/openai-compact-forge.ts). + // No active block yet → gate fails → honest passthrough (nothing folded to + // hand off). Captured summaries are append-only deduped per session so a + // second compaction in the same session hands off everything folded so far. + let nativeCompactForge: Prepared["nativeCompactForge"]; + if (nativeCompact && openaiTransformOk && codexCompactGate(session, config.modelContextLimit, true)) { + const prevForged = session.metadata.nativeCompactForgedSummaries as string[] | undefined; + const captured = mergeForgedSummaries(prevForged, session.state.blocks); + if (captured.length !== (prevForged?.length ?? 0)) session.metadata.nativeCompactForgedSummaries = captured; + const est = estimateCoreMessagesUpper(originalMessages) + countSystemAndToolsTokens(openaiSystemText || "", []); + const total = Math.max(session.stats.lastInputTokens, est, 1); + nativeCompactForge = buildOpenaiCompactForgeBody(captured.join("\n\n"), typeof parsed.model === "string" ? parsed.model : "gpt", { inputTokens: total, outputTokens: 0, totalTokens: total }, stream); + log("info", `[${sessionId}] host-native compaction intercepted; forged ${stream ? "SSE" : "JSON"} completion with ${captured.length} block summary(s), upstream not contacted (#2411)`); + } + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, nativeCompact, nativeCompactForge, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } /** Append the ephemeral nudge to a Gemini `contents` array. Gemini is diff --git a/src/server/relay.ts b/src/server/relay.ts index e085989315..319fc9344b 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -275,6 +275,14 @@ async function forwardCaptured( res.end(prepared.codexForge.body); return; } + // #2411 Stage B: host-native compaction request answered with bili's own + // fold summaries — same zero-egress contract as codexForge. + if (prepared?.nativeCompactForge) { + log("info", `[${prepared.session.id}] host-native compaction served locally (openai lane); upstream not contacted (#2411)`); + if (!res.headersSent) res.writeHead(200, { "content-type": prepared.nativeCompactForge.contentType }); + res.end(prepared.nativeCompactForge.body); + return; + } // #2740: auto-mode classifier one-shots are answered locally — same // zero-egress contract as codexForge; the audit line was written in // prepare (this line is the egress-proof for the serve itself). diff --git a/tests/traecode-native-compact.test.ts b/tests/traecode-native-compact.test.ts index ecb92e9c5a..6ca79bbffb 100644 --- a/tests/traecode-native-compact.test.ts +++ b/tests/traecode-native-compact.test.ts @@ -10,6 +10,7 @@ import type { ProxyOptions } from "../src/config.ts"; import { createHash } from "node:crypto"; import { TRAE_NATIVE_COMPACT_SENTENCE, neutralizeTraeNativeCompactInstruction, stripTraeCodeNativeCompactInstruction } from "../src/server/traecode-compact.js"; import { looksLikeNativeCompactionRequest, stripLeadingAcpTags } from "../src/server/native-compact-shape.js"; +import { buildOpenaiCompactForgeBody, OPENAI_COMPACT_ID_PREFIX } from "../src/server/openai-compact-forge.js"; process.env.NODE_ENV = "test"; @@ -197,10 +198,10 @@ test("e2e: TraeCode-shaped chat-completions request forwards ONE compression phi const sys2 = sent2.messages.find((m) => m.role === "system")?.content ?? ""; assert.ok(!sys2.includes(TRAE_NATIVE_COMPACT_SENTENCE), "continuation request stripped too"); } finally { - proxy.close(); - await once(proxy, "close"); - upstream.close(); - await once(upstream, "close"); + proxy.closeAllConnections?.(); + await new Promise((r) => proxy.close(() => r())); + upstream.closeAllConnections?.(); + await new Promise((r) => upstream.close(() => r())); } }); @@ -356,9 +357,152 @@ test("e2e: host-native compaction request forwards with ZERO bili compression su const sentC = JSON.parse(bodies[2]) as { tools?: Array<{ function?: { name?: string } }> }; assert.ok(sentC.tools?.some((t) => t.function?.name === "compress"), "surface restored on the next ordinary turn"); } finally { - proxy.close(); - await once(proxy, "close"); - upstream.close(); - await once(upstream, "close"); + proxy.closeAllConnections?.(); + await new Promise((r) => proxy.close(() => r())); + upstream.closeAllConnections?.(); + await new Promise((r) => upstream.close(() => r())); + } +}); + +// #2411 Stage B: the host-native compaction request gets bili's OWN fold +// summaries served as the completion — upstream never contacted. E2 capture +// (reporter): the backend's real answer to this request shape is an ordinary +// chat completion, so the forge is a standard SSE/JSON pair. + +test("forge body: stream shape matches the captured terminal form", () => { + const { body, contentType } = buildOpenaiCompactForgeBody("SUMMARY-TEXT-ONE", "gpt-test", { inputTokens: 1234, outputTokens: 0, totalTokens: 1234 }, true); + assert.equal(contentType, "text/event-stream"); + type Frame = { object?: string; id?: string; model?: string; choices?: Array<{ index: number; delta: Record; finish_reason: string | null }>; usage?: Record }; + const frames = body.split("\n\n").filter((l) => l.startsWith("data: ") && l !== "data: [DONE]").map((l) => JSON.parse(l.slice(6)) as Frame); + assert.equal(frames.length, 3, "three data frames"); + assert.ok(body.endsWith("data: [DONE]\n\n"), "terminal [DONE] frame"); + for (const f of frames) { + assert.equal(f.object, "chat.completion.chunk"); + assert.ok(String(f.id).startsWith(OPENAI_COMPACT_ID_PREFIX), "bili-forged id sentinel"); + assert.equal(f.model, "gpt-test"); + } + assert.deepEqual(frames[0].choices?.[0]?.delta, { role: "assistant" }); + assert.equal(frames[0].choices?.[0]?.finish_reason, null); + assert.deepEqual(frames[1].choices?.[0]?.delta, { content: "SUMMARY-TEXT-ONE" }); + assert.equal(frames[2].choices?.[0]?.finish_reason, "stop", "terminal finish_reason on the last frame"); + assert.deepEqual(frames[2].usage, { prompt_tokens: 1234, completion_tokens: 0, total_tokens: 1234 }); +}); + +test("forge body: non-stream shape is a standard chat.completion", () => { + const { body, contentType } = buildOpenaiCompactForgeBody("SUMMARY-TEXT-TWO", "gpt-test", { inputTokens: 55, outputTokens: 7, totalTokens: 62 }, false); + assert.equal(contentType, "application/json"); + const j = JSON.parse(body) as Record; + assert.equal(j.object, "chat.completion"); + assert.ok(String(j.id).startsWith(OPENAI_COMPACT_ID_PREFIX)); + assert.equal(j.model, "gpt-test"); + const choice = (j.choices as Array>)[0]; + assert.equal(choice.finish_reason, "stop"); + assert.deepEqual(choice.message, { role: "assistant", content: "SUMMARY-TEXT-TWO" }); + assert.deepEqual(j.usage, { prompt_tokens: 55, completion_tokens: 7, total_tokens: 62 }); +}); + +const FORGE_SUMMARY_TEXT = "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK"; +const FILLER = "lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(50); // ~2.9KB per message — pair must clear the kernel 5000-char min + +test("e2e: host-native compaction request served locally from ACP blocks; upstream untouched (#2411 Stage B)", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: string[] = []; + let calls = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + bodies.push(Buffer.concat(chunks).toString("utf8")); + calls += 1; + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (calls === 1) { + // round 1: the model folds the first two messages — bili executes + // it server-side and re-requests with the refreshed view + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: null, tool_calls: [{ index: 0, id: "call_p", type: "function", function: { name: "compress", arguments: JSON.stringify({ content: [{ startId: "m00001", endId: "m00004", topic: "setup", summary: FORGE_SUMMARY_TEXT }] }) } }] }, finish_reason: null }] })); + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })); + } else { + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "ok" } }] })); + res.write(sseLine({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 2 } })); + } + res.write("data: [DONE]\n\n"); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: {} as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + const conversation = [ + { role: "system", content: "You are a coding assistant." }, + { role: "user", content: FILLER + "question 1" }, + { role: "assistant", content: FILLER + "answer 2" }, + { role: "user", content: FILLER + "question 3" }, + { role: "assistant", content: FILLER + "answer 4" }, + { role: "user", content: FILLER + "question 5" }, + { role: "assistant", content: FILLER + "answer 6" }, + { role: "user", content: FILLER + "question 7" }, + { role: "assistant", content: FILLER + "answer 8" }, + ]; + try { + // setup: same conversation prefix as the later compaction request so + // prefix affinity lands both on ONE session; round 1 folds m00001..m00004 + await postChat(url, conversation); + assert.equal(bodies.length, 2, "setup consumed initial + post-fold re-request"); + + // THE compaction act: full history replay + template as last user message + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-test", stream: true, max_tokens: 1024, tools: NATIVE_TOOLS, messages: [...conversation, { role: "user", content: TEMPLATE_6861 }] }), + }); + assert.equal(res.status, 200); + assert.ok(String(res.headers.get("content-type")).includes("text/event-stream"), "SSE response (request was stream:true)"); + const text = await res.text(); + assert.ok(text.includes("[Compressed conversation section]"), "forge carries the ACP fold summaries"); + assert.ok(text.includes(FORGE_SUMMARY_TEXT), "folded-range summary text present"); + assert.ok(!text.includes(TRAE_NATIVE_COMPACT_SENTENCE)); + assert.ok(text.trimEnd().endsWith("data: [DONE]"), "terminal frame well-formed"); + assert.equal(bodies.length, 2, "ZERO egress — the compaction request never reached upstream"); + + // gate fail-open: template-shaped request on a session WITHOUT active + // blocks passes through to upstream (nothing folded to hand off) + const res2 = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-test", stream: true, max_tokens: 1024, messages: compactBody(TEMPLATE_6861).messages }), + }); + assert.equal(res2.status, 200); + await res2.arrayBuffer(); + assert.equal(bodies.length, 3, "no-blocks session forwards normally (honest passthrough)"); + } finally { + proxy.closeAllConnections?.(); + await new Promise((r) => proxy.close(() => r())); + upstream.closeAllConnections?.(); + await new Promise((r) => upstream.close(() => r())); } }); From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 4/4] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": {