Skip to content
Open
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
7 changes: 4 additions & 3 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3103,9 +3103,10 @@ async function handleRequest(
if (!countTokens && !responsesCompact && protocol && prepared && bodyMayCarryImages) {
session.incomingImageIndex = refreshIncomingImageIndex(parsed, protocol, session.state, session.id, session.incomingImageIndex);
}
// #2155 self-heal round: one evaluation per request, on the
// nudge-carrying prepared result (bypass lanes have nudge
// undefined and the hook no-ops inside). Side requests
// #2155 self-heal round: one evaluation per request, whenever
// prepare produced a nudge decision — every main round (the
// kernel emits one whether it injects or not; #4578). Bypass
// lanes leave nudge undefined and skip it. Side requests
// (title-gen etc.) never carry a nudge, so they cannot burn
// zombie credit either. #3004: nudgeActive is the POST-GATE
// delivery fact (prepared.nudgeOutcome === "injected") — the
Expand Down
34 changes: 20 additions & 14 deletions src/session-self-heal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ type LogFn = (level: "info" | "warn", message: string) => void;

/** Evaluate one prepared round. Pure w.r.t. inputs; mutates only the WeakMap
* counters and (on transitions) session.metadata.selfHeal. Called once per
* HTTP request after prepare (nudge-carrying rounds only — bypass lanes
* never reach it). */
* HTTP request after prepare, on EVERY main round — the kernel emits a
* NudgeDecision whether it injects or not (#4578); bypass lanes leave nudge
* undefined and never reach it. */
export function evaluateSelfHealRound(session: Session, inputs: SelfHealRoundInputs, log: LogFn): void {
const st = counters.get(session) ?? { lastReductionKey: undefined, zombieStreak: 0, idleStreak: 0 };
counters.set(session, st);
Expand Down Expand Up @@ -237,18 +238,23 @@ export function evaluateSelfHealRound(session: Session, inputs: SelfHealRoundInp
}

const pluginBound = typeof session.metadata["pluginAgent"] === "string";
// Streaks advance only on rounds that actually carried a nudge — an idle
// session (no traffic, side requests) must not accumulate zombie credit,
// and a reduction or an agent-side tool use always resets both streaks.
if (inputs.nudgeActive) {
const zombieSignal = pluginBound && !inputs.pluginHeaderPresent && !inputs.biliToolsDeclared;
if (reduction || inputs.biliToolUses > 0) {
st.zombieStreak = 0;
st.idleStreak = 0;
} else {
st.idleStreak += 1;
st.zombieStreak = zombieSignal ? st.zombieStreak + 1 : 0;
}
// #4578: the IDLE streak counts IGNORED nudges, so its reset is NOT
// gated on the nudge — this hook runs on every main round (the kernel
// emits a NudgeDecision whether it injects or not), and a fold lands
// BETWEEN injections, first observed by an ordinary round. Old code
// consumed that reduction without resetting, so every 5th INJECTED
// nudge armed suppression regardless of intervening compressions.
// Evidence resets whenever seen; only nudge-carrying rounds earn idle
// credit (an idle session must not accrue it). Zombie credit stays
// nudge-gated: a dead lane surviving on preflight refolds must still
// degrade.
const zombieSignal = pluginBound && !inputs.pluginHeaderPresent && !inputs.biliToolsDeclared;
if (reduction || inputs.biliToolUses > 0) {
st.idleStreak = 0;
if (inputs.nudgeActive) st.zombieStreak = 0;
} else if (inputs.nudgeActive) {
st.idleStreak += 1;
st.zombieStreak = zombieSignal ? st.zombieStreak + 1 : 0;
}

if (selfHealOf(session) !== undefined) return;
Expand Down
95 changes: 95 additions & 0 deletions tests/issue4578-self-heal-idle-reset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import assert from "node:assert/strict";
import test from "node:test";

process.env.NODE_ENV = "test";

import type { Session } from "../src/session.ts";
import {
SELF_HEAL_WINDOW,
evaluateSelfHealRound,
nudgeSuppressed,
pluginLaneDegraded,
} from "../src/session-self-heal.ts";

// #4578: self-heal counted INJECTED nudges, not ignored ones. The hook runs
// on every main request round (the kernel emits a NudgeDecision whether it
// injects or not; only injected/gated rounds carry nudgeActive=true), but a
// fold lands BETWEEN injections and its new block is first observed by an
// ordinary non-nudge round. That round consumed the reduction key WITHOUT
// resetting idleStreak, so the next injected round saw reduction=false and
// kept climbing — on plugin-route hosts (biliToolUses forever 0) exactly the
// 5th injected nudge armed "nudge idle for 5 rounds" even though compressions
// had landed in every window in between.

const noop = () => {};

function mkSession(): Session {
return {
id: "s1",
metadata: {},
state: { nextBlockId: 1 },
lastCompress: undefined,
} as unknown as Session;
}

const NUDGE_ROUND = { pluginHeaderPresent: false, biliToolsDeclared: true, nudgeActive: true, biliToolUses: 0, degradeAvailable: true };
const PLAIN_ROUND = { ...NUDGE_ROUND, nudgeActive: false };

// The field symptom is the ARMING EVENT (the "nudge idle" warn line in the
// #4578 log), not the end state: under the old gate the 5th injected round
// armed AND the following evidence round lifted it inside the same cycle, so
// end-state asserts alone pass under both old and new code.
const NEVER_ARMED = "no false 'nudge idle' arming across five obeyed cycles";

test("unit: #4578 a fold landing in a non-nudge round resets the idle streak — obeyed nudges never arm", () => {
const s = mkSession();
const logs: string[] = [];
const log = (l: string, m: string) => logs.push(`${l}:${m}`);
for (let i = 0; i < SELF_HEAL_WINDOW; i++) {
// Injected nudge, then the model obeys and folds: the new block is
// first seen by the very next ordinary round (nudgeActive=false).
evaluateSelfHealRound(s, NUDGE_ROUND, log);
s.state.nextBlockId += 1;
evaluateSelfHealRound(s, PLAIN_ROUND, log);
assert.equal(nudgeSuppressed(s), false, `cycle ${i + 1}: an intervening compression is not an ignored nudge`);
assert.equal(pluginLaneDegraded(s), false, `cycle ${i + 1}: no degrade either`);
}
assert.ok(!logs.some((x) => x.includes("nudge idle")), NEVER_ARMED);
});

test("unit: #4578 a visible bili tool use seen on a non-nudge round also resets the idle streak", () => {
const s = mkSession();
const logs: string[] = [];
const log = (l: string, m: string) => logs.push(`${l}:${m}`);
for (let i = 0; i < SELF_HEAL_WINDOW; i++) {
evaluateSelfHealRound(s, NUDGE_ROUND, log);
evaluateSelfHealRound(s, { ...PLAIN_ROUND, biliToolUses: 1 }, log);
assert.equal(nudgeSuppressed(s), false, `cycle ${i + 1}: tool use is engagement evidence wherever observed`);
}
assert.ok(!logs.some((x) => x.includes("nudge idle")), NEVER_ARMED);
});

test("unit: #4578 non-nudge rounds without evidence are neutral — they neither reset nor earn credit", () => {
const s = mkSession();
for (let i = 0; i < SELF_HEAL_WINDOW - 1; i++) {
evaluateSelfHealRound(s, NUDGE_ROUND, noop);
evaluateSelfHealRound(s, PLAIN_ROUND, noop);
assert.equal(nudgeSuppressed(s), false, `cycle ${i + 1}: window not reached`);
}
evaluateSelfHealRound(s, NUDGE_ROUND, noop);
evaluateSelfHealRound(s, PLAIN_ROUND, noop);
assert.equal(nudgeSuppressed(s), true, "five ignored nudges with quiet rounds between them still arm");
});

test("unit: five consecutive injected rounds with no evidence still arm (no over-correction)", () => {
const s = mkSession();
for (let i = 0; i < SELF_HEAL_WINDOW - 1; i++) {
evaluateSelfHealRound(s, NUDGE_ROUND, noop);
assert.equal(nudgeSuppressed(s), false, `round ${i + 1}: window not reached`);
}
const logs: string[] = [];
evaluateSelfHealRound(s, NUDGE_ROUND, (l: string, m: string) => logs.push(`${l}:${m}`));
assert.equal(nudgeSuppressed(s), true, "genuinely ignored five times: suppression arms");
assert.equal((s.metadata.selfHeal as { detected: string }).detected, "nudge-idle");
assert.ok(logs.some((x) => x.includes("nudge idle")), "arming is logged");
});
Loading