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
15 changes: 15 additions & 0 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { log as loggerLog } from "./logger.js";
import { METADATA_CANONICAL_SYS_FP, METADATA_FOLD_COVERAGE, METADATA_LAST_RESPONSES_MODE, METADATA_SYSTEM_FP } from "./fold-reconcile.js";
import { clearPostRebuildAnchor, markDirty, type Session } from "./session.js";
import { isForeignMembership } from "./request-membership.js";
import { normalizeUpstreamOrigin } from "./util.js";
import { toolFail, toolOk, type ProxyToolResult } from "./proxy-tool-result.js";

Expand Down Expand Up @@ -1804,6 +1805,18 @@ export function settleUsageReport(
// relay echo) — it must not clobber the last trusted lastInputTokens.
if (s.total > 0) {
session.stats.inputTokens += s.total;
// #4805: id-membership gate — a report produced by a request whose
// message ids do not join this session's known id family is foreign
// billing data under a colliding session key (auxiliary host personas
// stamp the host conversation id on their own tiny payloads). It
// still counts toward cumulative usage, but it is not this
// conversation's truth and must not touch the anchor family. Abstain
// paths (no evidence / first turns / #2821 re-mints) settle exactly
// as before: the gate only ever refuses the clearly-foreign.
if (isForeignMembership(session.transient?.requestMembership)) {
const m = session.transient!.requestMembership!;
loggerLog("info", `[${session.id}] [membership] usage sample (total=${s.total}) refused anchor write: its request did not carry the conversation's latest message id (needle=${m.needle ?? "-"}) — foreign payload under a shared session id (#4805)`);
} else {
// Net out this turn's compress credit: the post-compress re-request
// re-sends the unfolded history, so its usage report over-reports the
// context the NEXT request will actually carry (see stream.ts applyRanges).
Expand All @@ -1813,6 +1826,7 @@ export function settleUsageReport(
// never by estimate-grade samples or arming paths; dropped at native-
// compaction boundaries via resetSessionCompression (session.ts).
session.stats.lastUsageGradeTokens = session.stats.lastInputTokens;
session.stats.lastUsagePayloadUpper = session.stats.localInputEstimate ?? 0;
session.stats.lastInputTokensSource = "usage";
// #3148: a real usage report supersedes the failed-round arm — drop the
// visibility marker so acp_status stops advertising a stale emergency state.
Expand Down Expand Up @@ -1849,6 +1863,7 @@ export function settleUsageReport(
// re-arms both in one event).
delete session.stats.overflowArmTokens;
delete session.stats.overflowArmCompletionReserve;
}
}
// #1933 F1: roll the pending pair forward to the current turn's value —
// it will be consumed by the NEXT report (or cleared when the lane
Expand Down
1 change: 1 addition & 0 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ interface PersistedSession {
// pendingFoldUsage / lastLocalTextEstimate(+Origin), which ride along
// but are deliberately not restored — see buildSession).
localInputEstimate?: number;
lastUsagePayloadUpper?: number;
lastUsageGradeTokens?: number;
calibratedEstimate?: number;
calibratedEstimateOrigin?: string;
Expand Down
9 changes: 9 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,15 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr
// takes precedence immediately (#728 invariant), and every prepare*
// overwrites it with the measured outbound each turn.
child.stats.localInputEstimate = child.stats.contextTokens;
// #4805: the child inherits the parent's membership needle — its
// resent history IS the parent's, so the probe must not
// cold-start (an auxiliary persona striking the fork child first
// would sail through an unplanted needle and pollute the fresh
// anchor before any guard has evidence).
if (parent.transient?.latestKnownMsgId) {
(child.transient ??= {}).latestKnownMsgId = parent.transient.latestKnownMsgId;
child.transient.needlePayloadUpper = parent.transient.needlePayloadUpper;
}
const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 2, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false };
child.metadata.publicForkReceipt = { requestHash, response };
publishForkSession(child);
Expand Down
74 changes: 74 additions & 0 deletions src/request-membership.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { CoreMessage } from "acp-kernel";
import { log as loggerLog } from "./logger.js";
import type { Session } from "./session.js";

// #4805: settle-side membership probe. A usage report is only "this
// conversation's truth" when the request that produced it actually carried
// this conversation's messages. Hosts stamp auxiliary personas (dsh
// auto-review and its relatives) with the SAME conversation id, so the
// session key alone cannot tell a main-turn report from a foreign
// 2-message one — but one remembered id can: a main turn resents the whole
// history and therefore contains the conversation's latest admitted
// message id, while an auxiliary payload never does. The probe key updates
// ONLY from member / first-turn / main-scale requests, so a foreign
// payload can neither pass nor hijack the needle (#2821 re-minted main
// turns are main-scale: they lose one settle and move the needle, healing
// on the next resend). Evidence ids are id-based ONLY (no content
// hashing): plugin lanes send the biliMsgIds envelope (#4491, any rollout
// state); every wire carries resent tool-call ids (model-minted,
// client-echoed — the only ids the raw protocol wires carry).
const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25;

type RequestMembership = { member: boolean; needle: string | null };

// The payload upper estimate of the request that planted/moved the needle:
// before an anchor exists (fresh or fork child), the needle's own scale is
// the main-scale reference, so a tiny polluter cannot hijack it.

export function collectRequestIds(envelopeIds: unknown, msgs: CoreMessage[]): string[] {
const out: string[] = [];
const seen = new Set<string>();
if (Array.isArray(envelopeIds)) {
for (const v of envelopeIds) {
if (typeof v === "string" && v.length > 0 && v.length <= 512 && !v.startsWith("acp_summary_") && !seen.has(v)) {
seen.add(v);
out.push(v);
}
}
if (out.length > 0) return out;
}
for (const m of msgs) {
const id = m.toolCallId;
if (typeof id === "string" && id.length > 0 && !seen.has(id)) {
seen.add(id);
out.push(id);
}
}
return out;
}

export function isForeignMembership(m: RequestMembership | null | undefined): boolean {
return m?.member === false;
}

export function probeRequestMembership(session: Session, ids: string[], payloadUpper: number): RequestMembership | null {
if (ids.length === 0) return null;
const t = session.transient;
if (!t) return null;
const needle = t.latestKnownMsgId ?? null;
// First turn (no needle yet) abstains and plants one.
const member = needle === null || ids.includes(needle);
const verdict: RequestMembership = { member, needle };
t.requestMembership = verdict;
loggerLog("debug", `[${session.id}] [#4805] membership probe: ids=${ids.length} needle=${member ? "member" : "miss"} payloadUpper=${payloadUpper}`);
// Needle discipline: only member / first-turn / main-scale requests may
// move it — a tiny foreign payload must never own the needle, or the
// next main turn would probe against the polluter's id and be gated.
const referenceScale = session.stats.lastInputTokens > 0 ? session.stats.lastInputTokens : (t.needlePayloadUpper ?? 0);
const mainScale = payloadUpper >= referenceScale * MEMBERSHIP_RECORD_SCALE_RATIO;
if (member || mainScale) {
t.latestKnownMsgId = ids[ids.length - 1];
t.needlePayloadUpper = payloadUpper;
}
return verdict;
}
31 changes: 29 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,20 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin
// all require usage >= their pct lines). #728 fix: such sessions fall back to
// the PREVIOUS turn's locally-measured outbound payload upper bound
// (session.stats.localInputEstimate, recorded in prepare* each turn). The
// #4805: the meter-side size guard (anchorImpossible below) also requires an ABSOLUTE gap,
// not just the ratio: on early/small sessions the char-count upper bound is already an order
// of magnitude above the real token count, so a pure-ratio test would demote healthy small
// anchors to estimates and lose usage-grade sizing exactly where it is cheapest to keep.
const SIDE_LANE_ANCHOR_MIN_ABS_DELTA = 25_000;
// The anchor's OWN request snapshot (lastUsagePayloadUpper, taken at settle)
// carries the honesty margin: a usage report can legitimately under-report a
// text-heavy payload by ~8x (#1820 B — char-bound 220k over a 5k report), so the
// anchor size alone cannot prove pollution; requiring the payload to ALSO tower
// 8x over the payload of the request that produced the anchor keeps #1820 B's
// honest anchors on the fast path while #4805's side-lane sample (anchor request
// ~3k upper, main payload ~680k upper) is refused with 2-20x margin.
const SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO = 8;

// estimator only errs EARLY (char-count upper bound → compress earlier, never
// later), is active only while lastInputTokens == 0 (a real usage report takes
// precedence immediately — same invariant as #604's armFailureShrink
Expand Down Expand Up @@ -2505,7 +2519,20 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou
// #4383 (both guards): stale-small — a sidecar/side-request sample under
// the same session id, superseded by later main sends — must not freeze the
// meter either; fall through to the fresher measurement.
if (!anchorStale && session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") && staleSmallAnchorFallback(session.stats, session.stats.lastInputTokens) === null) return { tokens: session.stats.lastInputTokens, source: "usage" };
// #4805 (size guard): a FRESH tiny anchor can still be lane pollution — a
// 2-message side request's usage (5100 tok) overwrote a 1700-message
// conversation's anchor and froze compression until the context blew the
// window. Time-based staleness cannot catch it (it is seconds old), but the
// size dimension can: a usage sample cannot describe a resent history whose
// own upper bound is ~an order of magnitude larger. Fall through to the
// local estimate then — the nudge sizes the payload it actually sees.
const payloadUpperNow = estimateCoreMessagesUpper(msgs) + inboundImageTokens;
const anchorImpossible = session.stats.lastInputTokens > 0
&& (session.stats.lastUsagePayloadUpper ?? 0) > 0
&& payloadUpperNow >= session.stats.lastInputTokens * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO
&& payloadUpperNow >= session.stats.lastUsagePayloadUpper! * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO
&& payloadUpperNow - session.stats.lastInputTokens > SIDE_LANE_ANCHOR_MIN_ABS_DELTA;
if (!anchorStale && !anchorImpossible && session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") && staleSmallAnchorFallback(session.stats, session.stats.lastInputTokens) === null) return { tokens: session.stats.lastInputTokens, source: "usage" };
// #1820: right after a preflight rebuild the usage-grade baseline above is
// momentarily absent (the rebuild request's own report hasn't landed yet,
// or the upstream never reports), and every branch below sizes on the
Expand All @@ -2517,7 +2544,7 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou
// reporting upstreams fall back to legacy sizing rather than a frozen meter.
const anchored = postRebuildAnchorTokens(session);
if (anchored > 0) return { tokens: anchored, source: "estimate" };
const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens;
const raw = payloadUpperNow;
// #1569/#1839: while the latest baseline is not usage-grade (the transient
// window right after a failed turn), sizing on ANY re-derived view is how
// ghosts enter: #1569 first tried min(est, raw) — the char-count upper
Expand Down
6 changes: 6 additions & 0 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { injectSystem, injectTool } from "./inject.js";
import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js";
import { featureEnabled } from "../rollout.js";
import { MSG_IDS_BODY_FIELD, MSG_SEQS_BODY_FIELD, applyMsgIdPin, applyMsgSeqStamps, validateMsgIds, validateMsgSeqs } from "../msgid-pin.js";
import { collectRequestIds, probeRequestMembership } from "../request-membership.js";
import { alignIdSet, looksLikeSummaryCarrier } from "../msgid-algebra.js";

export async function prepareAnthropic(
Expand Down Expand Up @@ -594,6 +595,11 @@ export async function prepareAnthropic(
session.stats.lastLocalTextEstimate = estimateCoreMessages(processedMessages.length > 0 ? processedMessages : originalMessages)
+ countSystemAndToolsTokens(extractSystem(systemOut), toolsOut)
+ imageReserveFor(session, "anthropic", rebuilt, opts, upstreamOrigin);
// #4805: id-membership evidence for the settle gate — the envelope ids
// when the lane sends them (any rollout state), else the tool-call ids
// resent in the history. Placed after the payload row above so the
// main-scale tiebreak sees this request's own upper bound.
probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0);
if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin;
return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared;
}
Expand Down
5 changes: 5 additions & 0 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, e
import { effectiveAbsorbBlock } from "./prepare-responses.js";
import { injectOpenaiTool, injectTool } from "./inject.js";
import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js";
import { collectRequestIds, probeRequestMembership } from "../request-membership.js";

export async function prepareOpenai(
parsed: OpenAIRequestBody,
Expand Down Expand Up @@ -494,6 +495,10 @@ export async function prepareOpenai(
+ imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin);
const openaiPairOrigin = billingUpstream ?? upstreamOrigin;
if (openaiPairOrigin) session.stats.lastLocalTextEstimateOrigin = openaiPairOrigin;
// #4805: id-membership evidence for the settle gate (see the
// anthropic-lane counterpart) — chat wire has no msgid envelope, so
// evidence is the tool-call ids resent in the history.
probeRequestMembership(session, collectRequestIds(undefined, processedMessages.length > 0 ? processedMessages : originalMessages), session.stats.localInputEstimate ?? 0);
}
snapshotMessages(session, originalMessages);
markDirty(session);
Expand Down
15 changes: 15 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ export type Session = {
* bili's rebuild. */
inBody?: InboundSlot;
settledInBody?: InboundSlot;
/** #4805: the conversation's latest ADMITTED message id — the single
* needle the settle-side membership probe tests each resent history
* against. Updated only by member / first-turn / main-scale
* requests, so a foreign payload can neither pass nor hijack it.
* In-memory only; a restart replants it on the first turn. */
latestKnownMsgId?: string;
needlePayloadUpper?: number;
/** #4805: the in-flight request's probe verdict ({member, needle}),
* recorded at prepare and consumed by settleUsageReport. */
requestMembership?: { member: boolean; needle: string | null };
};
id: string;
/** Identity / descriptive metadata. Populated on first request. */
Expand Down Expand Up @@ -396,6 +406,11 @@ export type Session = {
* Cleared by resetSessionCompression (native-compaction boundary).
* Persisted (survives restart like the rest of stats). */
localInputEstimate?: number;
/** #4805: the payload upper estimate of the request whose usage report
* wrote the current usage-grade anchor — the meter-side size guard
* compares it against the payload at hand to refuse anchors that
* cannot describe this conversation (side-lane pollution). */
lastUsagePayloadUpper?: number;
/** #2117: billing-caliber estimate of the CURRENT outbound request —
* estimateCoreMessages + wire overhead, k̂-scaled only where the
* factor's route+model provenance matches (#1933 F1 / #2117 B), plus
Expand Down
Loading
Loading