From 2046253cf377b1e31ce0d1841ca95b37a0f52db0 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 20:20:58 +0800 Subject: [PATCH 1/2] fix: absorb aborted-turn output into the nudge growth reference (#5298) --- src/plugin.ts | 6 +- src/server/prepare-anthropic.ts | 4 +- src/server/prepare-google.ts | 4 +- src/server/prepare-openai.ts | 4 +- src/server/prepare-responses.ts | 4 +- src/session.ts | 68 ++++++ tests/issue5293-abort-nudge.test.ts | 307 ++++++++++++++++++++++++++++ 7 files changed, 392 insertions(+), 5 deletions(-) create mode 100644 tests/issue5293-abort-nudge.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index ec497fbd87..6696f89aa9 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -8,7 +8,7 @@ import path from "node:path"; import { createHash } from "node:crypto"; import { getStore, storageStatusLabel } from "./persist.js"; import { cloneStoreForRefs } from "./store.js"; -import { acquireInFlight, createSession, getSession, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, statusInputBaseline, withSessionLock, type Session } from "./session.js"; +import { acquireInFlight, createSession, getSession, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, noteAbortedTurn, peekSession, releaseInFlight, statusInputBaseline, withSessionLock, type Session } from "./session.js"; import { clientConversationHeader } from "./session-id.js"; import { buildContextView } from "./context-view.js"; import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; @@ -3541,6 +3541,8 @@ export async function pipePluginChatWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); + // #5298: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. + if (session) noteAbortedTurn(session); return; } // #721: upstream read failed while the client is still connected — @@ -4404,6 +4406,8 @@ export async function pipePluginResponsesWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); + // #5298: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. + if (session) noteAbortedTurn(session); return; } // #721: upstream read failed while the client is still connected — diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 3e81710168..9753df16b3 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -15,7 +15,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL, RULE_TOOL, absorbToolsFor, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote } from "../compress-tool.js"; @@ -357,6 +357,8 @@ export async function prepareAnthropic( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); + // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 4748cd6e59..e3f271cfa0 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -14,7 +14,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { recordConflict } from "../conflict-watch.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; @@ -195,6 +195,8 @@ export async function prepareGoogle( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); + // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 7e2aa4fc01..9f0d17aa99 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -16,7 +16,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; @@ -211,6 +211,8 @@ export async function prepareOpenai( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); + // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 7765110082..d8d85fd423 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -17,7 +17,7 @@ import { buildAnchoredView, maybeSnapshotView, noteAnchoredRender, SnapshotAncho import { storedMessagesFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed, pluginLaneDegraded } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; import { recordConflict } from "../conflict-watch.js"; @@ -336,6 +336,8 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); + // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags, contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); diff --git a/src/session.ts b/src/session.ts index d1083ee4d7..a2ccb58399 100644 --- a/src/session.ts +++ b/src/session.ts @@ -259,6 +259,16 @@ export type Session = { /** #4805: the in-flight request's probe verdict ({member, needle}), * recorded at prepare and consumed by settleUsageReport. */ requestMembership?: { member: boolean; needle: string | null }; + /** #5298: the user aborted (Esc'd) the model stream mid-turn. The + * interrupted turn's partial output never reports usage, so the next + * usage-grade reading jumps by exactly that unreported mass — counted + * as organic growth, the cadence nudge fires on the user's redirect + * turn (pi: esc → continue → immediate compress injection). `anchor` + * is the meter value at abort time; consumed by the first nudge + * decision whose meter moved past it (absorbAbortedTurnGrowth). + * In-memory only: the abort→resume causality spans one process + * lifetime, and a restart replants nothing. */ + abortReanchor?: { anchor: number; at: number }; }; id: string; /** Identity / descriptive metadata. Populated on first request. */ @@ -902,6 +912,64 @@ export function diagnoseSuccessWithoutUsage(session: Session, wire: string): voi loggerLog("warn", `[${session.id}] [${wire}] upstream success without usage report — keeping lastInputTokens=${session.stats.lastInputTokens} (source=${session.stats.lastInputTokensSource ?? "none"}); nudge decisions ride local estimates until a usage-grade sample lands (#1595)`); } +/** #5298: growth debt from a user-interrupted turn. When the user aborts a + * model stream mid-turn (pi's Esc), the host persists the partial assistant + * message and resends it on the next turn, but the interrupted turn never + * reports usage for that output — the next usage-grade reading jumps by + * exactly the unreported partial. The flat-growth cadence (KDD #8) counts + * that jump as organic growth, so the nudge fires within one or two requests + * of the user's redirect — summarizing output they just rejected. + * + * Fix: an event-driven re-anchor in the same category as applyCompression + * resetting lastNudgeShownTokens (kernel compress.ts). The abort site stamps + * transient.abortReanchor (noteAbortedTurn); the FIRST nudge decision whose + * meter moved past the pre-abort anchor re-anchors the low-water reference + * to the current reading (absorbAbortedTurnGrowth). Deliberate exception to + * #3674's low-water rule: #3674 deleted the event-gated re-anchor because it + * dead-zoned small DROPS; here the reference moves UP because a known + * unmeasured mass just landed, gated on a witnessed client action rather than + * drift heuristics. Sessions without a pending stamp see byte-identical + * decisions. Preflight / pressure bands are untouched — only the growth + * cadence reference moves. */ +export const ABORT_REANCHOR_MAX_AGE_MS = 30 * 60 * 1000; + +/** Stamp a pending aborted-turn re-anchor; call from the plugin pipe's + * client-abort path AFTER settleUsage has landed whatever the interrupted + * stream reported (the request's input size — the partial output itself + * never reports). A zero/unknown meter carries no information: skip. */ +export function noteAbortedTurn(session: Session): void { + if (!Number.isFinite(session.stats.lastInputTokens) || session.stats.lastInputTokens <= 0) return; + session.transient ??= {}; + session.transient.abortReanchor = { anchor: session.stats.lastInputTokens, at: Date.now() }; +} + +/** Consume a pending aborted-turn re-anchor if — and only if — the meter has + * actually moved past the pre-abort anchor, i.e. the persisted partial now + * shows up in the measurement. Until then the flag waits (the resume turn + * still reads the stale anchor); a flag older than ABORT_REANCHOR_MAX_AGE_MS + * is dropped without touching state. */ +export function absorbAbortedTurnGrowth(session: Session, tokenCount: number): void { + const t = session.transient; + const flag = t?.abortReanchor; + if (!t || !flag) return; + if (Date.now() - flag.at > ABORT_REANCHOR_MAX_AGE_MS) { + delete t.abortReanchor; + return; + } + if (!(tokenCount > flag.anchor)) return; + const nudge = session.state.nudge; + const oldRef = nudge.lastPerMessageNudgeTokens; + nudge.lastPerMessageNudgeTokens = Math.max(oldRef, tokenCount); + // The pending nudge (if any) died with its turn: clear the shown stamp and + // the per-tier cadence stamps together — the same full-reset shape as + // applyCompression (kernel compress.ts), so no stale half-threshold or + // tier-cadence residue survives the interrupt. + nudge.lastNudgeShownTokens = 0; + nudge.lastShownByTier = {}; + delete t.abortReanchor; + loggerLog("info", `[${session.id}] aborted-turn growth absorbed: nudge reference ${oldRef}→${nudge.lastPerMessageNudgeTokens} (meter ${tokenCount}, pre-abort anchor ${flag.anchor}) (#5298)`); +} + // #1820: post-rebuild meter anchor. Right after a big preflight rebuild the // usage-grade baseline is momentarily absent (the rebuild request's own report // hasn't landed yet, or the upstream never reports one), so effectiveTokenCount diff --git a/tests/issue5293-abort-nudge.test.ts b/tests/issue5293-abort-nudge.test.ts new file mode 100644 index 0000000000..6e9a495183 --- /dev/null +++ b/tests/issue5293-abort-nudge.test.ts @@ -0,0 +1,307 @@ +/** + * #5293 (mirror #5298): pressing Esc mid-stream in pi persists the partial assistant + * message (pi's agent-loop appends the interrupted message to the transcript) and the + * continuation turn resends it — but the interrupted turn never reports usage for that + * output. The next usage-grade reading jumps by exactly the unreported partial, and the + * flat-growth nudge cadence counts it as organic growth, so a "compress now" nudge lands + * right on the user's redirect continuation (esc → continue → immediate compression + * injection). + * + * Fix under test: host-side event-driven re-anchor (src/session.ts). The plugin pipes + * stamp session.transient.abortReanchor on client abort (noteAbortedTurn); the first + * prepare decision whose meter has moved past the pre-abort anchor re-anchors the + * low-water growth reference to the current reading (absorbAbortedTurnGrowth). No kernel + * change. + * + * Scenario (deterministic sizes; token model = ceil(chars/4), matching the mock's + * reported input_tokens): + * r1 warmup baseline stamped (low-water rule) + * r2 small growth below the ready gate → no nudge + * r3 ABORT partial streamed, then stall; client aborts; anchor stamped + * r4 resume meter == anchor → flag survives; growth mass-blocked → no nudge + * r5 debt turn meter jumps past anchor → absorb fires → growth 0 → NO nudge (core) + * (without the fix THIS turn injects the nudge — the reported bug) + * r6 organic below the gate again → no nudge + * r7 organic a full fresh interval grown → nudge present (negative direction) + */ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import { test } from "node:test"; +import type { ProxyOptions } from "../src/server.ts"; + +// XDG isolation BEFORE any server-module import (module-top code runs after import +// hoisting, so every bili import below is dynamic). +const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-e2e-abort-nudge-")); +process.env.XDG_STATE_HOME = path.join(tmpRoot, "state"); +process.env.XDG_CACHE_HOME = path.join(tmpRoot, "cache"); +process.env.XDG_DATA_HOME = path.join(tmpRoot, "data"); +process.env.XDG_CONFIG_HOME = path.join(tmpRoot, "config"); +delete process.env.ACP_DUMP_BODY; + +const LIMIT = 200_000; +const MODEL = "claude-test"; +// step = 4000 → ready gate = max(minGrowthFloor 100, minGrowthRatio 0.1 × 4000) = 400; +// tier-1 mass gate = tierGrowthTokens.t1 = 600 tokens; minCompressRange default 5000 chars. + +const filler = (seed: number, chars: number): string => { + const words = ["context", "compression", "session", "anchor", "baseline", "growth", "interval", "meter"]; + let out = ""; + let i = 0; + while (out.length < chars) { + out += `${words[(i + seed) % words.length]}-${seed}-${i} `; + i += 1; + } + return out.slice(0, chars); +}; + +const SYSTEM = "You are a helpful coding assistant. Keep answers concise.\n" + filler(999, 800); +const COMPRESS_TOOL = { + name: "compress", + description: "Compress a range of conversation into a detailed summary.", + input_schema: { type: "object", properties: { topic: { type: "string" }, content: { type: "string" } }, required: ["content"] }, +}; +// Real pi main turns carry the host's action tools alongside bili's ACP tools. A +// request whose ENTIRE tools array is bili's own set is demoted to the side lane +// (#1897 leak shape: "an agent turn needs a world to act in") — these keep the +// scenario on the main pipeline. +const HOST_TOOLS = [ + { name: "bash", description: "Run a shell command.", input_schema: { type: "object", properties: { command: { type: "string" } }, required: ["command"] } }, + { name: "read", description: "Read a file.", input_schema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] } }, +]; +const TOOLS = [...HOST_TOOLS, COMPRESS_TOOL]; + +type Msg = { role: "user" | "assistant"; content: Array<{ type: "text"; text: string }> }; +const um = (n: number, len: number): Msg => ({ role: "user", content: [{ type: "text", text: `Task ${n}:\n${filler(n, len)}` }] }); +const am = (n: number, len: number): Msg => ({ role: "assistant", content: [{ type: "text", text: `Step ${n} done.\n${filler(n + 20, len)}` }] }); +// Sizes (chars): u1=800, a1=3598, small user=700 (175 tok), small assistant=260 (65 tok), +// abort partial=1200 (300 tok). Small-turn growth = 240 < 400 gate; two small turns = 480 ≥ 400. +const U_BIG = 792, A_BIG = 3585, U_SMALL = 692, A_SMALL = 247, PARTIAL = 1200; + +const estTok = (s: string) => Math.ceil(s.length / 4); +const msgChars = (m: { content: unknown }): number => { + const c = m.content; + if (typeof c === "string") return c.length; + if (Array.isArray(c)) return c.reduce((n: number, p) => n + (typeof (p as { text?: unknown })?.text === "string" ? ((p as { text: string }).text).length : 0), 0); + return 0; +}; + +test("#5298 unit: noteAbortedTurn / absorbAbortedTurnGrowth edge cases", async () => { + const { createSession, noteAbortedTurn, absorbAbortedTurnGrowth, ABORT_REANCHOR_MAX_AGE_MS } = await import("../src/session.ts"); + + const s0 = createSession("u-skip"); + noteAbortedTurn(s0); + assert.equal(s0.transient?.abortReanchor, undefined, "zero meter carries no information — no stamp"); + s0.stats.lastInputTokens = Number.NaN; + noteAbortedTurn(s0); + assert.equal(s0.transient?.abortReanchor, undefined, "non-finite meter — no stamp"); + + const s1 = createSession("u-stamp"); + s1.stats.lastInputTokens = 42_000; + noteAbortedTurn(s1); + assert.equal(s1.transient?.abortReanchor?.anchor, 42_000, "stamp records the pre-abort anchor"); + + const s2 = createSession("u-noop"); + s2.state.nudge.lastPerMessageNudgeTokens = 1000; + absorbAbortedTurnGrowth(s2, 5000); + assert.equal(s2.state.nudge.lastPerMessageNudgeTokens, 1000, "no flag — byte-identical decisions"); + + const s3 = createSession("u-wait"); + s3.stats.lastInputTokens = 42_000; + noteAbortedTurn(s3); + s3.state.nudge.lastPerMessageNudgeTokens = 40_000; + absorbAbortedTurnGrowth(s3, 42_000); + assert.ok(s3.transient?.abortReanchor, "meter at the anchor — the partial is not measured yet, flag waits"); + assert.equal(s3.state.nudge.lastPerMessageNudgeTokens, 40_000); + + const s4 = createSession("u-absorb"); + s4.stats.lastInputTokens = 42_000; + noteAbortedTurn(s4); + s4.state.nudge.lastPerMessageNudgeTokens = 40_000; + s4.state.nudge.lastNudgeShownTokens = 41_000; + s4.state.nudge.lastShownByTier = { 1: 41_000 }; + absorbAbortedTurnGrowth(s4, 43_000); + assert.equal(s4.transient?.abortReanchor, undefined, "consumed"); + assert.equal(s4.state.nudge.lastPerMessageNudgeTokens, 43_000, "reference re-anchored to the current reading"); + assert.equal(s4.state.nudge.lastNudgeShownTokens, 0, "pending nudge died with its turn"); + assert.deepEqual(s4.state.nudge.lastShownByTier, {}, "tier cadence stamps cleared"); + + const s5 = createSession("u-max"); + s5.stats.lastInputTokens = 42_000; + noteAbortedTurn(s5); + s5.state.nudge.lastPerMessageNudgeTokens = 45_000; + absorbAbortedTurnGrowth(s5, 43_000); + assert.equal(s5.state.nudge.lastPerMessageNudgeTokens, 45_000, "never lowers an existing higher reference"); + + const s6 = createSession("u-stale"); + s6.transient = { abortReanchor: { anchor: 42_000, at: Date.now() - ABORT_REANCHOR_MAX_AGE_MS - 60_000 } }; + s6.state.nudge.lastPerMessageNudgeTokens = 40_000; + absorbAbortedTurnGrowth(s6, 50_000); + assert.equal(s6.transient?.abortReanchor, undefined, "stale flag dropped"); + assert.equal(s6.state.nudge.lastPerMessageNudgeTokens, 40_000, "...without touching state"); +}); + +test("#5293: esc-interrupted output is absorbed, not nudged, on the resume turn", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const { rmrf } = await import("./tmp-rm.ts"); + + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + const absorbCount = () => logLines.filter((l) => l.includes("aborted-turn growth absorbed")).length; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + try { + const bodies: string[] = []; + const replies: Record = { 0: am(1, A_BIG).content[0].text, 1: am(2, A_SMALL).content[0].text, 3: am(4, A_SMALL).content[0].text, 4: am(5, A_SMALL).content[0].text, 5: am(6, A_SMALL).content[0].text, 6: am(7, A_SMALL).content[0].text }; + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const idx = bodies.length; + const body = Buffer.concat(chunks).toString("utf8"); + bodies.push(body); + const parsed = JSON.parse(body) as { messages?: Array<{ content: unknown }> }; + const inputTokens = estTok(SYSTEM) + estTok(JSON.stringify((parsed as { tools?: unknown }).tools ?? [])) + (parsed.messages ?? []).reduce((n, m) => n + Math.ceil(msgChars(m) / 4), 0); + const sse = (ev: string, data: object) => res.write(`event: ${ev}\ndata: ${JSON.stringify(data)}\n\n`); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + sse("message_start", { type: "message_start", message: { id: `msg-${idx}`, role: "assistant", model: MODEL, content: [], usage: { input_tokens: inputTokens, output_tokens: 0 } } }); + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }); + if (idx === 2) { + // Abort turn: stream the partial in quick chunks, then stall forever. + const partial = filler(90, PARTIAL); + for (let i = 0; i < partial.length; i += 200) { + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: partial.slice(i, i + 200) } }); + } + return; + } + const reply = replies[idx] ?? "ok"; + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: reply } }); + sse("content_block_stop", { type: "content_block_stop", index: 0 }); + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: Math.ceil(reply.length / 4) } }); + sse("message_stop", { type: "message_stop" }); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: defaultConfig(LIMIT), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + opts.kernelConfig = { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + const headers = { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": "conv-5293" }; + const send = (msgs: Msg[]) => JSON.stringify({ model: MODEL, max_tokens: 4096, stream: true, system: SYSTEM, messages: msgs, tools: TOOLS }); + + const post = async (msgs: Msg[]): Promise => { + const res = await fetch(url, { method: "POST", headers, body: send(msgs) }); + assert.ok(res.ok, `request failed: HTTP ${res.status}`); + await res.text(); + }; + // Stream until silence (the mock stalls on the abort turn), then abort — pi's Esc. + const postAborted = async (msgs: Msg[]): Promise => { + const ac = new AbortController(); + const res = await fetch(url, { method: "POST", headers, body: send(msgs), signal: ac.signal }); + assert.ok(res.ok && res.body, "aborted request must start streaming"); + const reader = res.body.getReader(); + const dec = new TextDecoder(); + let wire = ""; + for (;;) { + const chunk = await Promise.race([ + reader.read(), + new Promise<{ done: boolean; value?: Uint8Array }>((resolve) => setTimeout(() => resolve({ done: true, value: undefined }), 300)), + ]); + if (chunk.value) wire += dec.decode(chunk.value, { stream: true }); + if (chunk.done) break; + } + try { ac.abort(); } catch { /* already settled */ } + let out = ""; + for (const line of wire.split("\n")) { + if (!line.startsWith("data: ")) continue; + try { + const d = JSON.parse(line.slice(6)) as { type?: string; delta?: { type?: string; text?: string } }; + if (d.type === "content_block_delta" && d.delta?.type === "text_delta") out += d.delta.text ?? ""; + } catch { /* incomplete trailing line */ } + } + return out; + }; + + // pi persists the aborted partial message and resends history on every turn. + const H: Msg[] = []; + // r1: warmup — the low-water baseline gets stamped. + await post([um(1, U_BIG)]); + H.push(um(1, U_BIG), am(1, A_BIG)); + // r2: small organic growth — below the ready gate. + await post([...H, um(2, U_SMALL)]); + H.push(um(2, U_SMALL), am(2, A_SMALL)); + // r3: the user presses Esc mid-stream. + const partial = await postAborted([...H, um(3, U_SMALL)]); + assert.ok(partial.length > 500, `client received a meaningful partial before aborting (got ${partial.length} chars)`); + H.push(um(3, U_SMALL), { role: "assistant", content: [{ type: "text", text: partial }] }); + // r4: redirect continuation — the meter still reads the pre-abort anchor, so the + // flag survives this decision (nothing new is measurable yet). + await post([...H, um(4, U_SMALL)]); + H.push(um(4, U_SMALL), am(4, A_SMALL)); + assert.equal(absorbCount(), 0, "resume turn: meter at the pre-abort anchor — the flag must wait one more decision"); + // r5: the debt turn — the persisted partial shows up in the measurement. + await post([...H, um(5, U_SMALL)]); + H.push(um(5, U_SMALL), am(5, A_SMALL)); + // r6/r7: two more organic intervals. + await post([...H, um(6, U_SMALL)]); + H.push(um(6, U_SMALL), am(6, A_SMALL)); + await post([...H, um(7, U_SMALL)]); + + assert.equal(bodies.length, 7, "seven requests reached the upstream"); + const hasNudge = (i: number) => bodies[i]!.includes("efficiency nudge"); + for (const i of [0, 1, 2, 3]) assert.ok(!hasNudge(i), `request ${i + 1}: unexpected nudge`); + assert.ok(!hasNudge(4), "debt turn must NOT nudge — the aborted output was absorbed into the reference (#5298)"); + assert.ok(!hasNudge(5), "one organic interval below the flat step — no nudge yet"); + assert.ok(hasNudge(6), "a full fresh interval of organic growth must still nudge (negative direction)"); + assert.equal(absorbCount(), 1, "absorb fires exactly once, on the debt turn"); + } finally { + await closeServer(proxy); + await closeServer(upstream); + try { rmrf(tmpRoot); } catch { /* ignore */ } + } +}); From e2f3fdef99eb2f73a0f8473e368a7a90383ead7e Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 20:49:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?docs(#5293):=20correct=20issue=20references?= =?UTF-8?q?=20=E2=80=94=20this=20fix=20targets=20#5293,=20not=20#5298?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The original commit's title/message/comments cited #5298, which is an unrelated open decision request (dsh-injected user messages stealing the last-user-message protection slot). The bug report fixed here is #5293 (pi Esc-interrupt triggers compression injection); the test filename and the Closes line already said 5293. Comments/test labels only — zero behavior change. (review fix-up on PR branch) --- src/plugin.ts | 4 ++-- src/server/prepare-anthropic.ts | 2 +- src/server/prepare-google.ts | 2 +- src/server/prepare-openai.ts | 2 +- src/server/prepare-responses.ts | 2 +- src/session.ts | 6 +++--- tests/issue5293-abort-nudge.test.ts | 6 +++--- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 6696f89aa9..be97eba94a 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3541,7 +3541,7 @@ export async function pipePluginChatWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); - // #5298: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. + // #5293: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. if (session) noteAbortedTurn(session); return; } @@ -4406,7 +4406,7 @@ export async function pipePluginResponsesWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); - // #5298: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. + // #5293: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. if (session) noteAbortedTurn(session); return; } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 9753df16b3..f37fa4ef82 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -357,7 +357,7 @@ export async function prepareAnthropic( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); - // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index e3f271cfa0..e26af352d4 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -195,7 +195,7 @@ export async function prepareGoogle( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); - // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 9f0d17aa99..28ee8bf80d 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -211,7 +211,7 @@ export async function prepareOpenai( // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); - // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index d8d85fd423..79cc56f87f 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -336,7 +336,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr // resent history; yield before it so UI/API requests that queued during // this turn are served, not frozen behind the whole turn. await yieldToEventLoop(); - // #5298: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). + // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags, contentStore: contentStoreOf(session) }); session.state = turn.state; diff --git a/src/session.ts b/src/session.ts index a2ccb58399..f8dc41a114 100644 --- a/src/session.ts +++ b/src/session.ts @@ -259,7 +259,7 @@ export type Session = { /** #4805: the in-flight request's probe verdict ({member, needle}), * recorded at prepare and consumed by settleUsageReport. */ requestMembership?: { member: boolean; needle: string | null }; - /** #5298: the user aborted (Esc'd) the model stream mid-turn. The + /** #5293: the user aborted (Esc'd) the model stream mid-turn. The * interrupted turn's partial output never reports usage, so the next * usage-grade reading jumps by exactly that unreported mass — counted * as organic growth, the cadence nudge fires on the user's redirect @@ -912,7 +912,7 @@ export function diagnoseSuccessWithoutUsage(session: Session, wire: string): voi loggerLog("warn", `[${session.id}] [${wire}] upstream success without usage report — keeping lastInputTokens=${session.stats.lastInputTokens} (source=${session.stats.lastInputTokensSource ?? "none"}); nudge decisions ride local estimates until a usage-grade sample lands (#1595)`); } -/** #5298: growth debt from a user-interrupted turn. When the user aborts a +/** #5293: growth debt from a user-interrupted turn. When the user aborts a * model stream mid-turn (pi's Esc), the host persists the partial assistant * message and resends it on the next turn, but the interrupted turn never * reports usage for that output — the next usage-grade reading jumps by @@ -967,7 +967,7 @@ export function absorbAbortedTurnGrowth(session: Session, tokenCount: number): v nudge.lastNudgeShownTokens = 0; nudge.lastShownByTier = {}; delete t.abortReanchor; - loggerLog("info", `[${session.id}] aborted-turn growth absorbed: nudge reference ${oldRef}→${nudge.lastPerMessageNudgeTokens} (meter ${tokenCount}, pre-abort anchor ${flag.anchor}) (#5298)`); + loggerLog("info", `[${session.id}] aborted-turn growth absorbed: nudge reference ${oldRef}→${nudge.lastPerMessageNudgeTokens} (meter ${tokenCount}, pre-abort anchor ${flag.anchor}) (#5293)`); } // #1820: post-rebuild meter anchor. Right after a big preflight rebuild the diff --git a/tests/issue5293-abort-nudge.test.ts b/tests/issue5293-abort-nudge.test.ts index 6e9a495183..2c8e668c02 100644 --- a/tests/issue5293-abort-nudge.test.ts +++ b/tests/issue5293-abort-nudge.test.ts @@ -1,5 +1,5 @@ /** - * #5293 (mirror #5298): pressing Esc mid-stream in pi persists the partial assistant + * #5293: pressing Esc mid-stream in pi persists the partial assistant * message (pi's agent-loop appends the interrupted message to the transcript) and the * continuation turn resends it — but the interrupted turn never reports usage for that * output. The next usage-grade reading jumps by exactly the unreported partial, and the @@ -89,7 +89,7 @@ const msgChars = (m: { content: unknown }): number => { return 0; }; -test("#5298 unit: noteAbortedTurn / absorbAbortedTurnGrowth edge cases", async () => { +test("#5293 unit: noteAbortedTurn / absorbAbortedTurnGrowth edge cases", async () => { const { createSession, noteAbortedTurn, absorbAbortedTurnGrowth, ABORT_REANCHOR_MAX_AGE_MS } = await import("../src/session.ts"); const s0 = createSession("u-skip"); @@ -295,7 +295,7 @@ test("#5293: esc-interrupted output is absorbed, not nudged, on the resume turn" assert.equal(bodies.length, 7, "seven requests reached the upstream"); const hasNudge = (i: number) => bodies[i]!.includes("efficiency nudge"); for (const i of [0, 1, 2, 3]) assert.ok(!hasNudge(i), `request ${i + 1}: unexpected nudge`); - assert.ok(!hasNudge(4), "debt turn must NOT nudge — the aborted output was absorbed into the reference (#5298)"); + assert.ok(!hasNudge(4), "debt turn must NOT nudge — the aborted output was absorbed into the reference (#5293)"); assert.ok(!hasNudge(5), "one organic interval below the flat step — no nudge yet"); assert.ok(hasNudge(6), "a full fresh interval of organic growth must still nudge (negative direction)"); assert.equal(absorbCount(), 1, "absorb fires exactly once, on the debt turn");