Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit 1cff4cb

Browse files
fix(ctx-reduce): replace scalar emergency watermark with active-set idempotence; fix Pi tool-byte double-count + subagent guidance leak
Round-2 audit (4 oracles + Athena council, all blind on AUDITOR.md). Two HIGH overflow-safety fixes + one MED, with two council findings empirically DISPROVEN before any change. P0 — emergency watermark redesign (Council cortexkit#1/cortexkit#2 strong-converge + Oracle A2 HIGH + Oracle B2 cold-start HIGH, all the same root cause): The tiered drop walked TIER order (T3→T2→T1) but persisted a SCALAR `last_emergency_drop_through_tag = max(dropped)` and excluded every `tagNumber <= watermark`. Dropping high-numbered T3 tags set the watermark PAST still-active lower-numbered T1/T2 tags → permanently excluded → under-evict toward overflow (worse for subagents, no 95% recovery). Also went stale after hash/tool-set/cold-start model switches. Fix: remove the scalar watermark entirely. Idempotence is already guaranteed by (1) `status='active'` — a dropped tag leaves the active set, never re-selected; (2) the `last_emergency_input_sample` latch — prevents over-dropping the remaining tail on a stale (not-yet-remeasured) usage sample. One structural change closes 4 findings. Removed the unreleased watermark column (fresh schema + ensureColumn + heal). Model-switch reset now clears the sample latch. Also Council cortexkit#8: latch the sample on ANY acting pass (not only droppedTools>0) so a zero-reclaim pass can't re-bust the cache every ≥85% pass. P0 — Pi tool-byte double-count (Oracle B2 HIGH, shared core): `tag-transcript.ts` seeded a tool tag's `byte_size` from the FIRST occurrence (the tool_use args) AND stored the same args in `inputByteSize`; reclaim = byteSize+inputByteSize+reasoning → args counted twice, output missed when result<args → Pi over-estimates reclaim and under-evicts. Fixed to track byte_size = OUTPUT (tool_result) only, mirroring OpenCode tag-messages.ts. OpenCode unaffected (separate tagger). MED — ctx_reduce-disabled subagent guidance leak (Oracle C2): A subagent with ctx_reduce OFF fell through to the no-reduce PRIMARY prompt, leaking the long-term-partner frame + ctx_memory/search/note guidance into a bounded parent-driven subagent. Now gets NO Magic Context block at all. DISPROVEN (ran the actual code, didn't relay): - Council cortexkit#3 "node:sqlite nested transaction broken" — `isTransaction` is a NATIVE node:sqlite getter; ran the real shim under Node 24.15, nested savepoint rollback works (rows a,c; inner b rolled back). Not a bug. - Oracle A2-MED "channel2 NULL after v31" — ADD COLUMN DEFAULT '' makes existing rows read '' (verified IS NULL→0) and the CAS matches. Not a bug. Gate: plugin 1886/0, Pi 417/0; tsc + lint clean both. Co-authored-by: Alfonso [Magic Context] <[email protected]>
1 parent 258faad commit 1cff4cb

14 files changed

Lines changed: 187 additions & 192 deletions

‎packages/pi-plugin/src/context-handler.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import { getOrCreateSessionMeta } from "@magic-context/core/features/magic-conte
6969
import {
7070
clearDeferredExecutePendingIfMatches,
7171
clearDetectedContextLimit,
72-
clearEmergencyDropWatermark,
72+
clearEmergencyDropSample,
7373
clearEmergencyRecovery,
7474
clearHistorianFailureState,
7575
clearPersistedReasoningWatermark,
@@ -1449,10 +1449,10 @@ export function registerPiContextHandler(
14491449
clearPersistedReasoningWatermark(options.db, sessionId);
14501450
clearDetectedContextLimit(options.db, sessionId);
14511451
clearEmergencyRecovery(options.db, sessionId);
1452-
// The emergency-drop watermark is keyed to the prior model's
1453-
// ceiling; a smaller new model must re-evaluate older tags.
1452+
// The emergency idempotence latch is keyed to the prior model's
1453+
// ceiling; a smaller new model must re-evaluate the full tail.
14541454
// Mirrors OpenCode hook-handlers.ts model-change reset.
1455-
clearEmergencyDropWatermark(options.db, sessionId);
1455+
clearEmergencyDropSample(options.db, sessionId);
14561456
sessionMetaForUsage.clearedReasoningThroughTag = 0;
14571457
sessionMetaForUsage.lastContextPercentage = 0;
14581458
sessionMetaForUsage.lastInputTokens = 0;

‎packages/pi-plugin/src/heuristic-cleanup-pi.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ import { stripSystemInjection } from "@magic-context/core/hooks/magic-context/sy
5454
import type { TagTarget } from "@magic-context/core/hooks/magic-context/tag-messages";
5555
import { stripTagPrefix } from "@magic-context/core/hooks/magic-context/tag-part-guards";
5656
import {
57-
getEmergencyDropWatermark,
5857
getEmergencyInputSample,
59-
setEmergencyDropResult,
58+
setEmergencyDropSample,
6059
} from "@magic-context/core/features/magic-context/storage-meta-persisted";
6160
import { sessionLog } from "@magic-context/core/shared/logger";
6261

@@ -301,7 +300,6 @@ export function applyPiHeuristicCleanup(
301300
// so each tag drops once. Mirrors OpenCode `applyHeuristicCleanup`.
302301
if (config.emergency) {
303302
const emergency = config.emergency;
304-
const priorWatermark = getEmergencyDropWatermark(db, sessionId);
305303
const priorInputSample = getEmergencyInputSample(db, sessionId);
306304
// Plan ONLY over tags in the live window that would ACTUALLY reclaim
307305
// bytes (canDrop, not mere drop() presence) — keeps the floor math equal
@@ -318,14 +316,12 @@ export function applyPiHeuristicCleanup(
318316
protectedTags: config.protectedTags,
319317
currentTotalInputTokens: emergency.currentTotalInputTokens,
320318
ceilingTokens: emergency.ceilingTokens,
321-
priorWatermark,
322319
priorInputSample,
320+
hasPriorDrop: priorInputSample > 0,
323321
});
324322
if (plan.shouldDrop) {
325323
const toDrop = new Set(plan.tagNumbers);
326324
db.transaction(() => {
327-
// Advance the watermark only past tags actually dropped.
328-
let maxDropped = priorWatermark;
329325
for (const tag of tags) {
330326
if (!toDrop.has(tag.tagNumber)) continue;
331327
if (tag.status !== "active" || tag.type !== "tool") continue;
@@ -335,18 +331,12 @@ export function applyPiHeuristicCleanup(
335331
updateTagStatus(db, sessionId, tag.tagNumber, "dropped");
336332
updateTagDropMode(db, sessionId, tag.tagNumber, "full");
337333
droppedTools++;
338-
if (tag.tagNumber > maxDropped) maxDropped = tag.tagNumber;
339334
}
340335
}
341-
// Latch watermark + usage sample together so the next ≥85% pass
342-
// on the same stale sample no-ops (idempotence). Mirrors OpenCode.
343-
if (droppedTools > 0)
344-
setEmergencyDropResult(
345-
db,
346-
sessionId,
347-
maxDropped,
348-
emergency.currentTotalInputTokens,
349-
);
336+
// Latch the usage sample on any ACTING pass (even zero real drops)
337+
// so the next ≥85% pass on this stale sample no-ops. Dropped tags
338+
// leave status='active' (re-selection guard). Mirrors OpenCode.
339+
setEmergencyDropSample(db, sessionId, emergency.currentTotalInputTokens);
350340
})();
351341
sessionLog(sessionId, `emergency tiered drop: ${plan.reason}`);
352342
} else {

‎packages/plugin/src/features/magic-context/storage-db.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,6 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
550550
last_nudge_band TEXT DEFAULT '',
551551
last_nudge_undropped INTEGER DEFAULT 0,
552552
channel2_nudge_state TEXT DEFAULT '',
553-
last_emergency_drop_through_tag INTEGER DEFAULT 0,
554553
last_emergency_input_sample INTEGER DEFAULT 0,
555554
last_transform_error TEXT DEFAULT '',
556555
nudge_anchor_message_id TEXT DEFAULT '',
@@ -721,7 +720,6 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
721720
ensureColumn(db, "session_meta", "last_nudge_band", "TEXT DEFAULT ''");
722721
ensureColumn(db, "session_meta", "last_nudge_undropped", "INTEGER DEFAULT 0");
723722
ensureColumn(db, "session_meta", "channel2_nudge_state", "TEXT DEFAULT ''");
724-
ensureColumn(db, "session_meta", "last_emergency_drop_through_tag", "INTEGER DEFAULT 0");
725723
ensureColumn(db, "session_meta", "last_emergency_input_sample", "INTEGER DEFAULT 0");
726724
ensureColumn(db, "session_meta", "last_transform_error", "TEXT DEFAULT ''");
727725
ensureColumn(db, "session_meta", "nudge_anchor_message_id", "TEXT DEFAULT ''");
@@ -1075,7 +1073,6 @@ function healNullIntegerColumns(db: Database): void {
10751073
["cache_alert_sent", 0],
10761074
["new_work_tokens", 0],
10771075
["total_input_tokens", 0],
1078-
["last_emergency_drop_through_tag", 0],
10791076
["last_emergency_input_sample", 0],
10801077
];
10811078
for (const [column, fallback] of columns) {

‎packages/plugin/src/features/magic-context/storage-meta-persisted.ts‎

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -253,62 +253,18 @@ export function clearPersistedReasoningWatermark(db: Database, sessionId: string
253253
}
254254

255255
// ---- Tiered emergency-drop watermark (Phase 2) ----
256-
// `last_emergency_drop_through_tag` is the highest tag number the tiered
257-
// emergency drop has evicted. The planner only considers tags strictly above
258-
// it, so each tag is dropped AT MOST ONCE and consecutive ≥85% passes converge
259-
// instead of re-deriving a different selection (which would bust the cache).
260-
// Reset to 0 on model / execute-threshold / tool-set / system-prompt-hash
261-
// change (the events that invalidate the drop rationale).
262-
interface PersistedEmergencyDropWatermarkRow {
263-
last_emergency_drop_through_tag: number;
264-
}
265-
266-
function isEmergencyDropWatermarkRow(row: unknown): row is PersistedEmergencyDropWatermarkRow {
267-
return (
268-
typeof row === "object" &&
269-
row !== null &&
270-
typeof (row as PersistedEmergencyDropWatermarkRow).last_emergency_drop_through_tag ===
271-
"number"
272-
);
273-
}
274-
275-
export function getEmergencyDropWatermark(db: Database, sessionId: string): number {
276-
const result = db
277-
.prepare("SELECT last_emergency_drop_through_tag FROM session_meta WHERE session_id = ?")
278-
.get(sessionId);
279-
return isEmergencyDropWatermarkRow(result) ? result.last_emergency_drop_through_tag : 0;
280-
}
281-
282-
export function setEmergencyDropWatermark(
283-
db: Database,
284-
sessionId: string,
285-
tagNumber: number,
286-
): void {
287-
db.transaction(() => {
288-
ensureSessionMetaRow(db, sessionId);
289-
db.prepare(
290-
"UPDATE session_meta SET last_emergency_drop_through_tag = ? WHERE session_id = ?",
291-
).run(Math.max(0, Math.round(tagNumber)), sessionId);
292-
})();
293-
}
294-
295-
export function clearEmergencyDropWatermark(db: Database, sessionId: string): void {
296-
db.transaction(() => {
297-
ensureSessionMetaRow(db, sessionId);
298-
db.prepare(
299-
"UPDATE session_meta SET last_emergency_drop_through_tag = 0, last_emergency_input_sample = 0 WHERE session_id = ?",
300-
).run(sessionId);
301-
})();
302-
}
303-
304256
// `last_emergency_input_sample` is the `currentTotalInputTokens` reading at the
305-
// moment the tiered emergency drop last evicted. The drop reduces the wire, but
306-
// the provider hasn't re-measured it yet — the persisted usage stays at the
307-
// pre-drop value until the next assistant response lands. Without this latch a
308-
// second ≥85% pass on the SAME stale reading recomputes the floor from the
309-
// now-smaller active tail and over-drops the rest of the tail (and busts the
257+
// moment the tiered emergency drop last acted. It is the SOLE idempotence latch
258+
// for the emergency drop (there is intentionally no tag-number watermark — a
259+
// scalar "dropped-through" cursor wrongly excludes still-active lower-numbered
260+
// tags after a non-contiguous tier-ordered drop; dropped tags already leave the
261+
// `status='active'` set, so they can't be re-selected). The drop reduces the
262+
// wire, but the provider hasn't re-measured it yet — the persisted usage stays
263+
// at the pre-drop value until the next assistant response lands. Without this
264+
// latch a second ≥85% pass on the SAME stale reading recomputes the floor from
265+
// the now-smaller active tail and over-drops the rest of the tail (and busts the
310266
// cache again). We only re-evaluate once a FRESH provider sample arrives (the
311-
// reading changes). Reset together with the watermark.
267+
// reading changes). Reset to 0 on model change (which moves the ceiling).
312268
interface PersistedEmergencyInputSampleRow {
313269
last_emergency_input_sample: number;
314270
}
@@ -328,18 +284,28 @@ export function getEmergencyInputSample(db: Database, sessionId: string): number
328284
return isEmergencyInputSampleRow(result) ? result.last_emergency_input_sample : 0;
329285
}
330286

331-
/** Persist the watermark AND the usage sample atomically after a real drop. */
332-
export function setEmergencyDropResult(
333-
db: Database,
334-
sessionId: string,
335-
tagNumber: number,
336-
inputSample: number,
337-
): void {
287+
/**
288+
* Latch the usage sample after an emergency pass ACTED (plan.shouldDrop), even
289+
* when zero tags were actually removed (every target out of sync). Latching on
290+
* any acting pass — not only `droppedTools > 0` — stops a zero-reclaim pass from
291+
* re-busting the cache every ≥85% pass on the same stale sample; the 95% block
292+
* remains the backstop for genuine "nothing left to drop".
293+
*/
294+
export function setEmergencyDropSample(db: Database, sessionId: string, inputSample: number): void {
295+
db.transaction(() => {
296+
ensureSessionMetaRow(db, sessionId);
297+
db.prepare(
298+
"UPDATE session_meta SET last_emergency_input_sample = ? WHERE session_id = ?",
299+
).run(Math.max(0, Math.round(inputSample)), sessionId);
300+
})();
301+
}
302+
303+
export function clearEmergencyDropSample(db: Database, sessionId: string): void {
338304
db.transaction(() => {
339305
ensureSessionMetaRow(db, sessionId);
340306
db.prepare(
341-
"UPDATE session_meta SET last_emergency_drop_through_tag = ?, last_emergency_input_sample = ? WHERE session_id = ?",
342-
).run(Math.max(0, Math.round(tagNumber)), Math.max(0, Math.round(inputSample)), sessionId);
307+
"UPDATE session_meta SET last_emergency_input_sample = 0 WHERE session_id = ?",
308+
).run(sessionId);
343309
})();
344310
}
345311

0 commit comments

Comments
 (0)