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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, getSessionAsync, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, statusInputBaseline, withSessionLock, type Session } from "./session.js";
import { acquireInFlight, createSession, getSessionAsync, 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";
Expand Down Expand Up @@ -3650,6 +3650,8 @@ export async function pipePluginChatWithStrip(
settleWitnesses();
if (res.destroyed || res.writableEnded) {
log?.("client aborted mid-stream");
// #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;
}
// #721: upstream read failed while the client is still connected —
Expand Down Expand Up @@ -4516,6 +4518,8 @@ export async function pipePluginResponsesWithStrip(
settleWitnesses();
if (res.destroyed || res.writableEnded) {
log?.("client aborted mid-stream");
// #5293: 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 —
Expand Down
4 changes: 3 additions & 1 deletion src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { maybeSnapshotView } from "./snapshot-render.js";
import { storedMessagesFor, storedRowLookupFor } 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";
Expand Down Expand Up @@ -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();
// #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;
adoptContentStore(session, turn.contentStore);
Expand Down
4 changes: 3 additions & 1 deletion src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { maybeSnapshotView } from "./snapshot-render.js";
import { storedMessagesFor, storedRowLookupFor } 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";
Expand Down Expand Up @@ -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();
// #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;
adoptContentStore(session, turn.contentStore);
Expand Down
4 changes: 3 additions & 1 deletion src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { maybeSnapshotView } from "./snapshot-render.js";
import { storedMessagesFor, storedRowLookupFor } 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";
Expand Down Expand Up @@ -212,6 +212,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();
// #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;
adoptContentStore(session, turn.contentStore);
Expand Down
4 changes: 3 additions & 1 deletion src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { buildAnchoredView, maybeSnapshotView, noteAnchoredRender, SnapshotAncho
import { storedMessagesFor, storedRowLookupFor } 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";
Expand Down Expand Up @@ -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();
// #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;
adoptContentStore(session, turn.contentStore);
Expand Down
68 changes: 68 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,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 };
/** #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
* 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. */
Expand Down Expand Up @@ -969,6 +979,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)`);
}

/** #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
* 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}) (#5293)`);
}

// #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
Expand Down
Loading
Loading