Skip to content

Commit da03b91

Browse files
committed
fix(plugin): include reasoning bytes in pending ctx_reduce drop projection
Pending message drops via ctx_reduce now project both byteSize and reasoningByteSize since dropping a message also clears its thinking parts. The reasoning loop also skips pending-drop tags to prevent double counting. Removed a shadowed pendingDropTagIds redeclaration.
1 parent eb0eaff commit da03b91

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/plugin/src/hooks/magic-context/compartment-trigger.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ function estimateProjectedPostDropPercentage(
5858

5959
let droppableBytes = 0;
6060

61-
// 1. Pending user-queued drops (from ctx_reduce)
61+
// 1. Pending user-queued drops (from ctx_reduce) — include both text and reasoning bytes
62+
// because dropping a message tag also clears its associated reasoning parts
6263
const pendingDrops = getPendingOps(db, sessionId).filter((op) => op.operation === "drop");
64+
const pendingDropTagIds = new Set(pendingDrops.map((op) => op.tagId));
6365
if (pendingDrops.length > 0) {
64-
const pendingDropTagIds = new Set(pendingDrops.map((op) => op.tagId));
6566
droppableBytes += activeTags
6667
.filter((tag) => pendingDropTagIds.has(tag.tagNumber))
67-
.reduce((sum, tag) => sum + tag.byteSize, 0);
68+
.reduce((sum, tag) => sum + tag.byteSize + tag.reasoningByteSize, 0);
6869
}
6970

7071
// 2. Heuristic auto-drop: old tool outputs outside protected tail
@@ -73,7 +74,6 @@ function estimateProjectedPostDropPercentage(
7374
if (autoDropToolAge !== undefined && protectedTags !== undefined) {
7475
const toolAgeCutoff = maxTag - autoDropToolAge;
7576
const protectedCutoff = maxTag - protectedTags;
76-
const pendingDropTagIds = new Set(pendingDrops.map((op) => op.tagId));
7777

7878
for (const tag of activeTags) {
7979
// Skip already counted pending drops
@@ -89,6 +89,8 @@ function estimateProjectedPostDropPercentage(
8989
const reasoningAgeCutoff = maxTag - clearReasoningAge;
9090
for (const tag of activeTags) {
9191
if (tag.type !== "message") continue;
92+
// Skip tags already fully counted in pending drops (text + reasoning)
93+
if (pendingDropTagIds.has(tag.tagNumber)) continue;
9294
// Only count reasoning not yet cleared (between watermark and age cutoff)
9395
if (tag.tagNumber <= clearedReasoningThroughTag) continue;
9496
if (tag.tagNumber > reasoningAgeCutoff) continue;

0 commit comments

Comments
 (0)