diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 3730a12a04..03c88c9d3c 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -698,7 +698,14 @@ function codexInstall(): string { const text = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : ""; const existing = /^[ \t]*\[mcp_servers\.bili\][ \t]*$/m.exec(text); if (existing !== null) { - const block = text.slice(existing.index, text.indexOf("\n[", existing.index + 1) === -1 ? undefined : text.indexOf("\n[", existing.index + 1)); + // End the block at the next TABLE header line (optional indent + `[`), + // matching codexRemove. A plain indexOf("\n[") misses an indented next + // table and lets the block run to EOF, deleting everything after it. + const after = text.slice(existing.index); + const firstNewline = after.indexOf("\n"); + const nextTable = firstNewline < 0 ? -1 : after.slice(firstNewline + 1).search(/^[ \t]*\[/m); + const blockEnd = nextTable >= 0 ? existing.index + firstNewline + 1 + nextTable : text.length; + const block = text.slice(existing.index, blockEnd); const canonical = codexBlock().replace(/^\n/, ""); if (block.trimEnd() === canonical.trimEnd()) return `codex: already installed (${file})`; const refreshed = text.slice(0, existing.index) + canonical + text.slice(existing.index + block.length); diff --git a/src/server.ts b/src/server.ts index 8632518731..6b385ba7d2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -110,7 +110,7 @@ import { BILI_HOP_HEADER, anthropicBetaContextWindow, LAUNCHER_MODEL_WINDOWS, LA import { buildForwardHeaders, connectionNamedHeaders, NO_IDENTITY_MESSAGE, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; import { isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard } from "./server/side-request.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, estimateInputTokens, estimateWireOverhead } from "./server/budget.js"; -import { bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./server/stream-io.js"; +import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./server/stream-io.js"; // #1073: a forward-proxy-style (absolute-form) request whose authority IS this // instance's own listening endpoint — e.g. a health prober configured with our @@ -2695,8 +2695,11 @@ function prepareResponses( let echoReplaced = false; if (Array.isArray(parsed.input)) { const { items, replaced, dropped } = replaceBiliCompactionItems(parsed.input); - if (replaced > 0 || dropped > 0) { - echoReplaced = true; + if (replaced + dropped > 0) { + // Only a real replacement carries a summary into the history; a + // drop-only echo removed a marker blob without inserting one, so the + // forge-time captured summaries must still be re-injected (#1064). + if (replaced > 0) echoReplaced = true; log("info", `[${sessionId}] replaced ${replaced} echoed bili compaction item(s) with summary handoff message(s)${dropped > 0 ? `, dropped ${dropped} legacy marker item(s)` : ""}`); parsed.input = items as typeof parsed.input; } @@ -4510,10 +4513,7 @@ async function forward( } res.write(chunk); if (res.writableNeedDrain) { - await Promise.race([ - new Promise((r) => res.once("drain", () => r())), - new Promise((r) => res.once("close", () => r())), - ]); + await awaitDrain(res); } if (res.destroyed || res.writableEnded) break; } diff --git a/src/server/stream-io.ts b/src/server/stream-io.ts index e089a20f11..4ec9b28eec 100644 --- a/src/server/stream-io.ts +++ b/src/server/stream-io.ts @@ -45,6 +45,7 @@ export interface DrainableResponse { destroyed?: boolean; writableEnded?: boolean; once(event: string, cb: () => void): unknown; + removeListener?(event: string, cb: () => void): unknown; } // Backpressure wait that also resolves when the CLIENT goes away (#100). A @@ -56,9 +57,17 @@ export interface DrainableResponse { export function awaitDrain(res: DrainableResponse): Promise { if (res.destroyed || res.writableEnded) return Promise.resolve(); return new Promise((resolve) => { - res.once("drain", resolve); - res.once("close", resolve); - res.once("error", resolve); + // One shared settle: when any of the three fires, drop the other two or + // they leak on every backpressure wait (MaxListenersExceededWarning). + const done = (): void => { + res.removeListener?.("drain", done); + res.removeListener?.("close", done); + res.removeListener?.("error", done); + resolve(); + }; + res.once("drain", done); + res.once("close", done); + res.once("error", done); }); } diff --git a/src/session.ts b/src/session.ts index cf08d902d6..c83cef198a 100644 --- a/src/session.ts +++ b/src/session.ts @@ -252,6 +252,15 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot reloaded.lastSeen = Date.now(); reloaded.restored = false; reloaded.persisted = true; + // A memory-miss reload puts a NEW resident entry back into the pool; the + // new-session cap guard below is unreachable on this path, so enforce it + // here or evict-then-revisit grows the pool past MAX_SESSIONS (#1064). + if (sessions.size >= MAX_SESSIONS) { + const evicted = evictOldest(); + if (!evicted) { + throw new Error(`session pool exhausted (MAX_SESSIONS=${MAX_SESSIONS}; all in-flight)`); + } + } sessions.set(id, reloaded); return reloaded; } diff --git a/tests/backpressure-drain.test.ts b/tests/backpressure-drain.test.ts index 347183de03..51703f5ebf 100644 --- a/tests/backpressure-drain.test.ts +++ b/tests/backpressure-drain.test.ts @@ -55,6 +55,18 @@ test("awaitDrain resolves on error", async () => { assert.equal(resolved, true, "error must resolve"); }); +test("awaitDrain removes its one-shot listeners once resolved (no listener leak)", async () => { + const res = new EventEmitter(); + void awaitDrain(res as never); + assert.equal(res.listenerCount("drain"), 1, "drain listener armed while pending"); + assert.equal(res.listenerCount("close"), 1, "close listener armed while pending"); + assert.equal(res.listenerCount("error"), 1, "error listener armed while pending"); + res.emit("drain"); + assert.equal(res.listenerCount("drain"), 0, "drain listener removed after resolving"); + assert.equal(res.listenerCount("close"), 0, "close listener removed after resolving"); + assert.equal(res.listenerCount("error"), 0, "error listener removed after resolving"); +}); + test("awaitDrain resolves immediately for an already-dead response without registering listeners", async () => { let listenerCalls = 0; const res = { destroyed: true, writableEnded: false, once: () => { listenerCalls += 1; } }; diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index c93c3e4613..8f93aae3f4 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -338,6 +338,48 @@ test("e2e E2 (trigger form): post-forge turn — echo replaced by a history-born }); }); +test("e2e E2 (trigger form): drop-only legacy marker echo still re-injects forge-time summaries (#1064)", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + + // Turn 2: native auto-compact trigger → intercepted + forged. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const captured = s.metadata.codexForgedSummaries as string[] | undefined; + assert.ok(Array.isArray(captured) && captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "forge captured the active block summaries"); + + // Turn 3: codex echoes back a LEGACY compaction item — id-prefix only, + // no sentinel blob (older build) — so nothing is extractable and the + // item is DROPPED rather than replaced. The drop must NOT suppress the + // forge-time summary fallback (#1064): the summaries come back via the + // developer message instead of being silently lost. + const r3 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [{ type: "compaction", id: "fc_bili_legacy-no-blob" }, ...conversation().slice(-2), { type: "message", role: "user", content: "continue the work" }] }), + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(h.bodies.length, afterSetup + 1, "drop-only turn forwarded to upstream exactly once"); + + const fwd = h.bodies[h.bodies.length - 1]; + assert.ok(!fwd.includes("fc_bili_"), "legacy marker dropped before forwarding"); + const fwdBody = JSON.parse(fwd) as { input: Array<{ type?: string; role?: string }> }; + const handoff = fwdBody.input.find((i) => JSON.stringify(i).includes("[bili] context summary after compaction")); + assert.ok(!handoff, "no replacement handoff — nothing extractable from a blob-less legacy marker"); + const dev = fwdBody.input.find((i) => i.type === "message" && i.role === "developer"); + assert.ok(dev, "developer message present on the drop-only turn"); + assert.ok(JSON.stringify(dev).includes("MAIN-SUMMARY-SETUP"), "drop-only echo must not suppress the forge-time summary re-injection"); + }); +}); + test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output}, upstream untouched", async () => { await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { const afterSetup = await setupCompressedSession(h); diff --git a/tests/codex-install-block-end.test.ts b/tests/codex-install-block-end.test.ts new file mode 100644 index 0000000000..94e0f47482 --- /dev/null +++ b/tests/codex-install-block-end.test.ts @@ -0,0 +1,71 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pluginInstall, selfPackageRoot } from "../src/plugin-install.ts"; + +// #1064: codexInstall ended the [mcp_servers.bili] block at indexOf("\n["), +// which misses an indented next-table header — the block ran to EOF and a +// refresh deleted everything after it. End detection must match codexRemove +// (next table-header line, optional indent). +const FIXTURE = [ + 'model = "gpt-test"', + "", + "[mcp_servers.bili]", + 'command = "stale-command"', + 'args = ["stale"]', + "", + " [mcp_servers.other]", + 'command = "other-command"', + "", + "[sandbox]", + 'mode = "workspace-write"', +].join("\n") + "\n"; + +test("codex install refresh keeps content after an indented next table (#1064)", () => { + const home = mkdtempSync(join(tmpdir(), "bili-codex-home-")); + const file = join(home, "config.toml"); + writeFileSync(file, FIXTURE); + const prevHome = process.env.CODEX_HOME; + const prevProxy = process.env.BILI_MCP_PROXY; + process.env.CODEX_HOME = home; + process.env.BILI_MCP_PROXY = "http://127.0.0.1:9999"; + try { + assert.match(pluginInstall("codex"), /^codex: refreshed /); + const out = readFileSync(file, "utf8"); + assert.ok(out.includes('model = "gpt-test"'), "top-level key survives"); + assert.ok(out.includes("[mcp_servers.other]"), "indented next-table header survives"); + assert.ok(out.includes('command = "other-command"'), "next-table body survives"); + assert.ok(out.includes("[sandbox]") && out.includes('mode = "workspace-write"'), "trailing table survives"); + assert.ok(!out.includes("stale-command"), "stale bili block replaced"); + // codexBlock serializes the args via JSON.stringify, which escapes + // Windows separators — match the exact serialized form, not a raw path. + assert.ok(out.includes(JSON.stringify(join(selfPackageRoot(), "dist", "mcp.js"))), "canonical bili block installed"); + assert.match(pluginInstall("codex"), /already installed/, "second run is a no-op"); + } finally { + if (prevHome === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = prevHome; + if (prevProxy === undefined) delete process.env.BILI_MCP_PROXY; else process.env.BILI_MCP_PROXY = prevProxy; + rmSync(home, { recursive: true, force: true }); + } +}); + +test("codex install refresh keeps content after a column-0 next table", () => { + const home = mkdtempSync(join(tmpdir(), "bili-codex-home-")); + const file = join(home, "config.toml"); + writeFileSync(file, FIXTURE.replace(" [mcp_servers.other]", "[mcp_servers.other]")); + const prevHome = process.env.CODEX_HOME; + const prevProxy = process.env.BILI_MCP_PROXY; + process.env.CODEX_HOME = home; + process.env.BILI_MCP_PROXY = "http://127.0.0.1:9999"; + try { + assert.match(pluginInstall("codex"), /^codex: refreshed /); + const out = readFileSync(file, "utf8"); + assert.ok(out.includes('[mcp_servers.other]') && out.includes('command = "other-command"'), "next table survives"); + assert.ok(out.includes("[sandbox]") && out.includes('mode = "workspace-write"'), "trailing table survives"); + } finally { + if (prevHome === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = prevHome; + if (prevProxy === undefined) delete process.env.BILI_MCP_PROXY; else process.env.BILI_MCP_PROXY = prevProxy; + rmSync(home, { recursive: true, force: true }); + } +}); diff --git a/tests/fix-config-session.test.ts b/tests/fix-config-session.test.ts index 444b7eea96..328c4e2571 100644 --- a/tests/fix-config-session.test.ts +++ b/tests/fix-config-session.test.ts @@ -1,5 +1,8 @@ import test from "node:test"; import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { loadOptions, parseUpstreamProxyMode } from "../src/config.ts"; import { resolveProxyDecision } from "../src/upstream-proxy.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -143,6 +146,27 @@ test("getSession: evicts an idle session to make room when at MAX", () => { } }); +test("getSession: evict-then-revisit reload enforces MAX_SESSIONS (#1064)", () => { + const dir = mkdtempSync(join(tmpdir(), "bili-sesscap-")); + _setStoreForTest(new SessionStore({ dir, debounceMs: 5, enabled: true })); + _resetSessionsForTest(1); + try { + getSession("a"); + assert.equal(_sessionsSizeForTest(), 1); + // New session b evicts idle a; eviction flushes a to disk first. + getSession("b"); + assert.equal(_sessionsSizeForTest(), 1, "eviction keeps pool at MAX"); + // Revisit a: memory miss → disk reload. Before #1064 the reload path + // returned before the cap guard, so the pool grew past MAX_SESSIONS. + const reloaded = getSession("a"); + assert.equal(reloaded.id, "a", "reloaded from disk"); + assert.equal(_sessionsSizeForTest(), 1, "reload must evict first — pool stays at MAX"); + } finally { + _resetSessionsForTest(); + rmSync(dir, { recursive: true, force: true }); + } +}); + test("getSession: below MAX, no eviction, no throw", () => { _setStoreForTest(new SessionStore({ enabled: false })); _resetSessionsForTest(256);