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
43 changes: 33 additions & 10 deletions src/loop/tag-echo-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ export function createBiliArtifactFilter(onDrop?: (snippet: string) => void): Ta
};
}

export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boolean) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter {
export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boolean) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string, onDropVerdict?: (leavesInterior: boolean) => void): TagEchoFilter {
let held = "";
/** Settled prose deferred alongside a NON-definite tag-head tail: it
* carries a dead ref-close unit whose verdict is content-determined
Expand Down Expand Up @@ -1107,7 +1107,16 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
// below (spent by the response's FIRST drop) left it with no line at all.
// It gets its own one-shot budget so a held head at EOF is always audible.
let drainNoticed = false;
const recordDrop = (snippet: string, duringDrain = false) => {
/** #6034 (option E): delivered on EVERY drop (the one-shot notices above
* only reach the first) — does this dropped artifact leave interior
* bytes behind that a consumer's answer ledger must count as markup
* (true: a session-start open whose body verdict is still open, and a
* close that releases its prose body), or does the artifact die whole /
* release content (false: complete refs-only units, dead orphans, a
* refs-only close, discarded payloads)? Default false; only the sites
* below that provably leave or release interior pass true. */
const recordDrop = (snippet: string, duringDrain = false, leavesInterior = false) => {
onDropVerdict?.(leavesInterior);
droppedAny = true;
if (duringDrain) {
if (onDrop && !drainNoticed) {
Expand All @@ -1122,17 +1131,17 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
}
};
/** One self-contained artifact resolved by a single drop call. */
const drop = (snippet: string, duringDrain = false) => {
const drop = (snippet: string, duringDrain = false, leavesInterior = false) => {
dropCount += 1;
recordDrop(snippet, duringDrain);
recordDrop(snippet, duringDrain, leavesInterior);
};
/** Any drop inside a live swallow session — the session counts once. */
const dropSwallowSpan = (snippet: string, duringDrain = false) => {
const dropSwallowSpan = (snippet: string, duringDrain = false, leavesInterior = false) => {
if (!swallowSessionCounted) {
dropCount += 1;
swallowSessionCounted = true;
}
recordDrop(snippet, duringDrain);
recordDrop(snippet, duringDrain, leavesInterior);
};
/** #2862: strip dead ref-close units (ref + unterminated close head) from
* a span about to be RELEASED as prose; each unit is one self-contained
Expand Down Expand Up @@ -1190,7 +1199,17 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
if (swallowReleases) {
const dspan = degenCloseAfterRef(combined);
if (dspan !== null) {
dropSwallowSpan(combined.slice(0, dspan.end));
// #6034: a single-ref body terminates on ANY short close (#2190),
// so this path also carries the CLEAN echo (proper close name).
// Verdict by name: the proper close is a well-formed kernel tag
// quote — nothing left behind, the frame stays an answer; a
// mangled close name is the unclosed-echo shape (the model opened
// a render tag and botched the close), so whatever the frame still
// releases after the dead unit is suspect markup — exactly as the
// one-shot era armed it (the unclosed-echo retry tests in
// plugin-empty-turn-retry pin that direction).
const mangledClose = !combined.startsWith("\x3c/acp\x3e", dspan.start);
dropSwallowSpan(combined.slice(0, dspan.end), false, mangledClose);
swallowed = "";
swallowUntilClose = false;
buf = combined.slice(dspan.end);
Expand Down Expand Up @@ -1254,10 +1273,10 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
const t = TRUNC_OPEN.exec(inner);
if (t !== null) {
out += stripDeadRefCloses(inner.slice(0, t.index));
dropSwallowSpan(combined.slice(t.index, span.end));
dropSwallowSpan(combined.slice(t.index, span.end), false, true);
buf = combined.slice(span.end);
} else {
dropSwallowSpan(combined.slice(span.start, span.end));
dropSwallowSpan(combined.slice(span.start, span.end), false, true);
buf = inner + combined.slice(span.end);
}
}
Expand Down Expand Up @@ -1296,6 +1315,10 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
swallowed = "";
swallowUntilClose = false;
buf = combined;
// #644: the over-budget tail is content, not the tag's
// interior — un-arm any pending verdict so the released
// payload counts as the answer (#6034 class).
onDropVerdict?.(false);
if (onResidueWarn && containsEchoResidue(combined)) onResidueWarn(combined);
continue;
}
Expand Down Expand Up @@ -1480,7 +1503,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo
continue;
}
swallowSessionCounted = false;
recordDrop(m[0]);
recordDrop(m[0], false, true);
// An odd number of quotes means the opening's attribute list
// never closed: the model wrapped its turn inside the value (the
// sibling shape opens with such a value and then runs into a `<`,
Expand Down
24 changes: 20 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2921,11 +2921,23 @@ export async function pipePluginChatWithStrip(
// every count taken from bili.log.
const who = session ? `[${session.id}] ` : "";
const onTagDrop = (snippet: string, duringDrain?: boolean) => {
droppedTagInFrame = true;
sawStrippedEcho = true;
const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (plugin passthrough)";
loggerLog("warn", `${who}[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
};
// #6034 (option E): the answer ledger's markup flag is VERDICT-driven,
// not "any drop arms it". A complete refs-only echo unit dies whole — it
// leaves no interior behind, so the answer text around it must count as
// the answer; arming on every drop miscounted a 108-char real answer as
// markup after such an echo and killed a healthy turn twice (the sample
// in #6034). Only a session-start open (verdict pending until the span
// resolves) and a close that releases its prose body leave interior.
// Last-write-wins is safe: the filter delivers verdicts in release order
// within a frame (a unit's drop precedes its interior's resolution), and
// the frame's released text is counted only after push() returns.
const onTagDropVerdict = (leavesInterior: boolean) => {
droppedTagInFrame = leavesInterior;
};
// #2190: the filter released bytes that still carry echo-residue shape —
// the leak is now observable instead of silent. Log-only; the bytes were
// already decided by the state machine.
Expand Down Expand Up @@ -2961,7 +2973,7 @@ export async function pipePluginChatWithStrip(
// openai reasoning fields — the thinking channel is NEVER
// rewritten, only the visible text channel is.
const signedThinking = field === "thinking" || (protocol === "openai" && (field === "reasoning_content" || field === "reasoning"));
s = { filter: signedThinking ? createIdentityStreamFilter() : composeStreamFilters(composeStreamFilters(createTagEchoFilter(onTagDrop, onResidueWarn, absorbInstructed, requestText), createMarkerLineFilter(onMarkerDrop)), createBiliArtifactFilter(onBiliDrop)), field, index };
s = { filter: signedThinking ? createIdentityStreamFilter() : composeStreamFilters(composeStreamFilters(createTagEchoFilter(onTagDrop, onResidueWarn, absorbInstructed, requestText, onTagDropVerdict), createMarkerLineFilter(onMarkerDrop)), createBiliArtifactFilter(onBiliDrop)), field, index };
streams.set(key, s);
}
return s;
Expand Down Expand Up @@ -3009,8 +3021,12 @@ export async function pipePluginChatWithStrip(
* unclosed-tag case): markup the filter declined to swallow. A turn whose
* only visible output is this is as dead to the host as an empty one. */
let releasedMarkupChars = 0;
/** Set while a frame is being rewritten because a render tag was dropped:
* the text that survives such a frame is the tag's own interior. */
/** VERDICT-driven (onTagDropVerdict, #6034 option E): true while the
* filter has just dropped an artifact whose interior the stream still
* releases — the text surviving such a frame is the tag's own interior
* (markup), so the answer ledger must not count it as the answer. A
* whole-dead unit (complete refs-only echo) or a content release keeps
* it false and the released text stays answer. */
let droppedTagInFrame = false;
let finalFinishReason: string | undefined;
// #2328 Q2/Q4: termination diagnostics — every truncation emit below
Expand Down
Loading
Loading