pi-mega-compact 0.8.5 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/extensions/mega-events/compact-handlers.js +1 -0
- package/dist/extensions/mega-events/context-handler.js +42 -1
- package/dist/extensions/mega-pipeline/compact.js +13 -6
- package/dist/extensions/mega-runtime/state.js +10 -0
- package/extensions/mega-events/compact-handlers.ts +1 -0
- package/extensions/mega-events/context-handler.ts +46 -1
- package/extensions/mega-pipeline/compact.ts +13 -6
- package/extensions/mega-runtime/state.ts +16 -0
- package/package.json +1 -1
|
@@ -128,6 +128,7 @@ export function registerCompactHandlers(pi, runtime, config) {
|
|
|
128
128
|
pi.on("session_compact", async (_event, _ctx) => {
|
|
129
129
|
runtime.rt.lastNativeCompactAt = Date.now();
|
|
130
130
|
runtime.rt.lastCompactAt = Date.now();
|
|
131
|
+
runtime.trimCache = null; // v0.8.6: durable truncation changes the transcript — never replay the stale cached cut (PREVENT-PI-001/002)
|
|
131
132
|
runtime.logger.info("session-compacted", {
|
|
132
133
|
sessionId: runtime.rt.sessionId,
|
|
133
134
|
at: runtime.rt.lastCompactAt,
|
|
@@ -122,6 +122,34 @@ export function registerContextHandler(pi, runtime, config) {
|
|
|
122
122
|
return;
|
|
123
123
|
}
|
|
124
124
|
runtime.debounceUntil = now + 2000;
|
|
125
|
+
// v0.8.6 cache-stability: replay the cached trim view when still in the
|
|
126
|
+
// same compaction epoch AND context hasn't grown enough to warrant a
|
|
127
|
+
// re-compact. This stabilizes the provider KV-cache prefix (the summary +
|
|
128
|
+
// cut are reused verbatim) instead of regenerating a fresh summary +
|
|
129
|
+
// sentinel every fire, which invalidated the prefix on every other turn
|
|
130
|
+
// (the alternating cache-miss regression). Re-compact only when context
|
|
131
|
+
// grew >=10% of the window (percent basis) or >=50% of the effective
|
|
132
|
+
// threshold (token basis, when percent is unavailable). The cached `cut`
|
|
133
|
+
// is only valid while the transcript grows within the epoch — it is
|
|
134
|
+
// cleared on session_compact (durable truncation) + resetRuntime, so we
|
|
135
|
+
// never replay a stale cut into a truncated transcript (PREVENT-PI-001/002).
|
|
136
|
+
const RECOMPACT_PCT_DELTA = 10;
|
|
137
|
+
if (runtime.trimCache &&
|
|
138
|
+
runtime.trimCache.checkpointId === runtime.rt.lastCheckpointId &&
|
|
139
|
+
runtime.trimCache.cut <= messages.length) {
|
|
140
|
+
const grewEnough = pct != null && runtime.trimCache.ctxPct != null
|
|
141
|
+
? pct - runtime.trimCache.ctxPct >= RECOMPACT_PCT_DELTA
|
|
142
|
+
: currentTokens - (runtime.trimCache.ctxTokens ?? 0) >=
|
|
143
|
+
runtime.effectiveThreshold * 0.5;
|
|
144
|
+
if (!grewEnough) {
|
|
145
|
+
const recent = messages.slice(runtime.trimCache.cut); // guardrails-allow PREVENT-PI-002: cached `cut` was sanitized once by computeLiveTrimCut (src/boundary.ts) and replayed verbatim; the transcript only grows within an epoch (cache is cleared on durable truncation), so the preserved run still starts on a toolPair-safe index.
|
|
146
|
+
runtime.diagLiveTrimFires++; // trim view returned this call (replay counts as a fire)
|
|
147
|
+
runtime.diagLiveTrimReplays++;
|
|
148
|
+
runtime.snapshot(ctx);
|
|
149
|
+
return { messages: [runtime.trimCache.summaryAgentMsg, ...recent] };
|
|
150
|
+
}
|
|
151
|
+
// else: context grew enough → fall through to re-compact (cache is stale)
|
|
152
|
+
}
|
|
125
153
|
// Adaptive compression (Fix E): scale compression strength + keepFrom depth
|
|
126
154
|
// with how close we are to the model context limit. Null-safe: when the
|
|
127
155
|
// token-fallback path ran (pct unavailable) use the token-basis pressure
|
|
@@ -224,9 +252,22 @@ export function registerContextHandler(pi, runtime, config) {
|
|
|
224
252
|
const summaryAgentMsg = {
|
|
225
253
|
role: "user",
|
|
226
254
|
content: summaryMsg.text,
|
|
227
|
-
|
|
255
|
+
// v0.8.6: stable timestamp across the epoch (NOT Date.now()) so the
|
|
256
|
+
// summary message bytes — and thus the KV-cache prefix — don't drift
|
|
257
|
+
// on every replay within the same compaction epoch.
|
|
258
|
+
timestamp: runtime.rt.lastCompactAt ?? Date.now(),
|
|
228
259
|
};
|
|
229
260
|
const recent = messages.slice(cut); // guardrails-allow PREVENT-PI-002: `cut` is the pre-sanitized `compactedFrom` produced by src/boundary.ts computeDropRange, so the preserved run begins on a toolPair-safe index.
|
|
261
|
+
// v0.8.6: cache the trim view so subsequent gated calls in this epoch
|
|
262
|
+
// replay it verbatim (stabilizing the KV-cache prefix) instead of
|
|
263
|
+
// regenerating a fresh summary + sentinel every fire.
|
|
264
|
+
runtime.trimCache = {
|
|
265
|
+
checkpointId: ran.result.checkpointId ?? `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
|
|
266
|
+
cut,
|
|
267
|
+
summaryAgentMsg,
|
|
268
|
+
ctxPct: pct ?? null,
|
|
269
|
+
ctxTokens: currentTokens,
|
|
270
|
+
};
|
|
230
271
|
runtime.snapshot(ctx);
|
|
231
272
|
// DIAG (team-run relief): confirm the live trim actually fires + how big
|
|
232
273
|
// the window still is. The return is non-durable (per-LLM-call only), so
|
|
@@ -152,12 +152,19 @@ function doCompact(view, keepFrom, opts, sid, config, pi, ctx, runtime) {
|
|
|
152
152
|
}
|
|
153
153
|
// Sentinel marker: a non-LLM bookkeeping entry so subsequent triggers can
|
|
154
154
|
// skip re-vectorizing an already-compacted region (zero token cost).
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
// v0.8.6: gate on !result.deduped so the marker ONLY lands when a genuinely
|
|
156
|
+
// new checkpoint was created. Without this, every dedup re-fire appended a
|
|
157
|
+
// fresh sentinel to the real transcript, bloating it and perturbing the
|
|
158
|
+
// provider KV-cache prefix (the alternating cache-miss regression). Matches
|
|
159
|
+
// the RAPTOR + vector-index blocks above, which are already !deduped-gated.
|
|
160
|
+
if (!result.deduped) {
|
|
161
|
+
pi.appendEntry(MARKER_TYPE, {
|
|
162
|
+
checkpointId: result.checkpointId,
|
|
163
|
+
regionHash: result.regionHash,
|
|
164
|
+
tokenEstimate: result.tokenEstimate,
|
|
165
|
+
deduped: result.deduped,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
161
168
|
// Fix D: refresh the RAPTOR tree for this session so live recall (search) can
|
|
162
169
|
// serve high-level summaries. Best-effort + non-fatal: never block compaction.
|
|
163
170
|
// Budget-guarded (RAPTOR_BUDGET_MS) so it can't hang a large session.
|
|
@@ -50,6 +50,14 @@ export class MegaRuntime {
|
|
|
50
50
|
cacheHitTokens: 0,
|
|
51
51
|
lengthStopPending: false,
|
|
52
52
|
};
|
|
53
|
+
// v0.8.6 cache-stability: the cached live-trim view for the current
|
|
54
|
+
// compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
|
|
55
|
+
// replayed verbatim on subsequent gated context events in the SAME epoch
|
|
56
|
+
// (same checkpointId) so the provider KV-cache prefix stays stable instead
|
|
57
|
+
// of being invalidated by a freshly regenerated summary + sentinel every
|
|
58
|
+
// fire. Invalidated on session restart (resetRuntime) and on any native
|
|
59
|
+
// durable compaction (session_compact) that truncates the transcript.
|
|
60
|
+
trimCache = null;
|
|
53
61
|
debounceUntil = 0;
|
|
54
62
|
// S16: debounce for the agent_end resume nudge (avoid busy-loops).
|
|
55
63
|
resumeNudgeUntil = 0;
|
|
@@ -159,6 +167,7 @@ export class MegaRuntime {
|
|
|
159
167
|
* updated and cost nothing).
|
|
160
168
|
*/
|
|
161
169
|
diagLiveTrimFires = 0; // context handler returned a trimmed view
|
|
170
|
+
diagLiveTrimReplays = 0; // v0.8.6: trim view returned via cached replay (skipped re-compact)
|
|
162
171
|
diagBeforeCompactFires = 0; // session_before_compact handler entered
|
|
163
172
|
diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
|
|
164
173
|
diagAgentEndIdle = 0; // agent_end with activeAgents===0
|
|
@@ -714,6 +723,7 @@ export class MegaRuntime {
|
|
|
714
723
|
cacheHitTokens: 0,
|
|
715
724
|
lengthStopPending: false,
|
|
716
725
|
};
|
|
726
|
+
this.trimCache = null; // v0.8.6: never replay a stale trim into a new session
|
|
717
727
|
this.statusKey = undefined;
|
|
718
728
|
this.activeAgents = 0;
|
|
719
729
|
this.currentTurn = 0;
|
|
@@ -159,6 +159,7 @@ export function registerCompactHandlers(
|
|
|
159
159
|
pi.on("session_compact", async (_event: SessionCompactEvent, _ctx: ExtensionContext) => {
|
|
160
160
|
runtime.rt.lastNativeCompactAt = Date.now();
|
|
161
161
|
runtime.rt.lastCompactAt = Date.now();
|
|
162
|
+
runtime.trimCache = null; // v0.8.6: durable truncation changes the transcript — never replay the stale cached cut (PREVENT-PI-001/002)
|
|
162
163
|
runtime.logger.info("session-compacted", {
|
|
163
164
|
sessionId: runtime.rt.sessionId,
|
|
164
165
|
at: runtime.rt.lastCompactAt,
|
|
@@ -157,6 +157,38 @@ export function registerContextHandler(
|
|
|
157
157
|
}
|
|
158
158
|
runtime.debounceUntil = now + 2000;
|
|
159
159
|
|
|
160
|
+
// v0.8.6 cache-stability: replay the cached trim view when still in the
|
|
161
|
+
// same compaction epoch AND context hasn't grown enough to warrant a
|
|
162
|
+
// re-compact. This stabilizes the provider KV-cache prefix (the summary +
|
|
163
|
+
// cut are reused verbatim) instead of regenerating a fresh summary +
|
|
164
|
+
// sentinel every fire, which invalidated the prefix on every other turn
|
|
165
|
+
// (the alternating cache-miss regression). Re-compact only when context
|
|
166
|
+
// grew >=10% of the window (percent basis) or >=50% of the effective
|
|
167
|
+
// threshold (token basis, when percent is unavailable). The cached `cut`
|
|
168
|
+
// is only valid while the transcript grows within the epoch — it is
|
|
169
|
+
// cleared on session_compact (durable truncation) + resetRuntime, so we
|
|
170
|
+
// never replay a stale cut into a truncated transcript (PREVENT-PI-001/002).
|
|
171
|
+
const RECOMPACT_PCT_DELTA = 10;
|
|
172
|
+
if (
|
|
173
|
+
runtime.trimCache &&
|
|
174
|
+
runtime.trimCache.checkpointId === runtime.rt.lastCheckpointId &&
|
|
175
|
+
runtime.trimCache.cut <= messages.length
|
|
176
|
+
) {
|
|
177
|
+
const grewEnough =
|
|
178
|
+
pct != null && runtime.trimCache.ctxPct != null
|
|
179
|
+
? pct - runtime.trimCache.ctxPct >= RECOMPACT_PCT_DELTA
|
|
180
|
+
: currentTokens - (runtime.trimCache.ctxTokens ?? 0) >=
|
|
181
|
+
runtime.effectiveThreshold * 0.5;
|
|
182
|
+
if (!grewEnough) {
|
|
183
|
+
const recent = messages.slice(runtime.trimCache.cut); // guardrails-allow PREVENT-PI-002: cached `cut` was sanitized once by computeLiveTrimCut (src/boundary.ts) and replayed verbatim; the transcript only grows within an epoch (cache is cleared on durable truncation), so the preserved run still starts on a toolPair-safe index.
|
|
184
|
+
runtime.diagLiveTrimFires++; // trim view returned this call (replay counts as a fire)
|
|
185
|
+
runtime.diagLiveTrimReplays++;
|
|
186
|
+
runtime.snapshot(ctx);
|
|
187
|
+
return { messages: [runtime.trimCache.summaryAgentMsg, ...recent] };
|
|
188
|
+
}
|
|
189
|
+
// else: context grew enough → fall through to re-compact (cache is stale)
|
|
190
|
+
}
|
|
191
|
+
|
|
160
192
|
// Adaptive compression (Fix E): scale compression strength + keepFrom depth
|
|
161
193
|
// with how close we are to the model context limit. Null-safe: when the
|
|
162
194
|
// token-fallback path ran (pct unavailable) use the token-basis pressure
|
|
@@ -266,9 +298,22 @@ export function registerContextHandler(
|
|
|
266
298
|
const summaryAgentMsg = {
|
|
267
299
|
role: "user" as const,
|
|
268
300
|
content: summaryMsg.text,
|
|
269
|
-
|
|
301
|
+
// v0.8.6: stable timestamp across the epoch (NOT Date.now()) so the
|
|
302
|
+
// summary message bytes — and thus the KV-cache prefix — don't drift
|
|
303
|
+
// on every replay within the same compaction epoch.
|
|
304
|
+
timestamp: runtime.rt.lastCompactAt ?? Date.now(),
|
|
270
305
|
} as unknown as AgentMessage;
|
|
271
306
|
const recent = messages.slice(cut); // guardrails-allow PREVENT-PI-002: `cut` is the pre-sanitized `compactedFrom` produced by src/boundary.ts computeDropRange, so the preserved run begins on a toolPair-safe index.
|
|
307
|
+
// v0.8.6: cache the trim view so subsequent gated calls in this epoch
|
|
308
|
+
// replay it verbatim (stabilizing the KV-cache prefix) instead of
|
|
309
|
+
// regenerating a fresh summary + sentinel every fire.
|
|
310
|
+
runtime.trimCache = {
|
|
311
|
+
checkpointId: ran.result.checkpointId ?? `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
|
|
312
|
+
cut,
|
|
313
|
+
summaryAgentMsg,
|
|
314
|
+
ctxPct: pct ?? null,
|
|
315
|
+
ctxTokens: currentTokens,
|
|
316
|
+
};
|
|
272
317
|
runtime.snapshot(ctx);
|
|
273
318
|
// DIAG (team-run relief): confirm the live trim actually fires + how big
|
|
274
319
|
// the window still is. The return is non-durable (per-LLM-call only), so
|
|
@@ -192,12 +192,19 @@ function doCompact(
|
|
|
192
192
|
|
|
193
193
|
// Sentinel marker: a non-LLM bookkeeping entry so subsequent triggers can
|
|
194
194
|
// skip re-vectorizing an already-compacted region (zero token cost).
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
195
|
+
// v0.8.6: gate on !result.deduped so the marker ONLY lands when a genuinely
|
|
196
|
+
// new checkpoint was created. Without this, every dedup re-fire appended a
|
|
197
|
+
// fresh sentinel to the real transcript, bloating it and perturbing the
|
|
198
|
+
// provider KV-cache prefix (the alternating cache-miss regression). Matches
|
|
199
|
+
// the RAPTOR + vector-index blocks above, which are already !deduped-gated.
|
|
200
|
+
if (!result.deduped) {
|
|
201
|
+
pi.appendEntry(MARKER_TYPE, {
|
|
202
|
+
checkpointId: result.checkpointId,
|
|
203
|
+
regionHash: result.regionHash,
|
|
204
|
+
tokenEstimate: result.tokenEstimate,
|
|
205
|
+
deduped: result.deduped,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
201
208
|
|
|
202
209
|
// Fix D: refresh the RAPTOR tree for this session so live recall (search) can
|
|
203
210
|
// serve high-level summaries. Best-effort + non-fatal: never block compaction.
|
|
@@ -87,6 +87,20 @@ export class MegaRuntime {
|
|
|
87
87
|
cacheHitTokens: 0,
|
|
88
88
|
lengthStopPending: false,
|
|
89
89
|
};
|
|
90
|
+
// v0.8.6 cache-stability: the cached live-trim view for the current
|
|
91
|
+
// compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
|
|
92
|
+
// replayed verbatim on subsequent gated context events in the SAME epoch
|
|
93
|
+
// (same checkpointId) so the provider KV-cache prefix stays stable instead
|
|
94
|
+
// of being invalidated by a freshly regenerated summary + sentinel every
|
|
95
|
+
// fire. Invalidated on session restart (resetRuntime) and on any native
|
|
96
|
+
// durable compaction (session_compact) that truncates the transcript.
|
|
97
|
+
trimCache: {
|
|
98
|
+
checkpointId: string;
|
|
99
|
+
cut: number;
|
|
100
|
+
summaryAgentMsg: AgentMessage;
|
|
101
|
+
ctxPct: number | null;
|
|
102
|
+
ctxTokens: number | null;
|
|
103
|
+
} | null = null;
|
|
90
104
|
debounceUntil = 0;
|
|
91
105
|
// S16: debounce for the agent_end resume nudge (avoid busy-loops).
|
|
92
106
|
resumeNudgeUntil = 0;
|
|
@@ -199,6 +213,7 @@ export class MegaRuntime {
|
|
|
199
213
|
* updated and cost nothing).
|
|
200
214
|
*/
|
|
201
215
|
diagLiveTrimFires = 0; // context handler returned a trimmed view
|
|
216
|
+
diagLiveTrimReplays = 0; // v0.8.6: trim view returned via cached replay (skipped re-compact)
|
|
202
217
|
diagBeforeCompactFires = 0; // session_before_compact handler entered
|
|
203
218
|
diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
|
|
204
219
|
diagAgentEndIdle = 0; // agent_end with activeAgents===0
|
|
@@ -795,6 +810,7 @@ export class MegaRuntime {
|
|
|
795
810
|
cacheHitTokens: 0,
|
|
796
811
|
lengthStopPending: false,
|
|
797
812
|
};
|
|
813
|
+
this.trimCache = null; // v0.8.6: never replay a stale trim into a new session
|
|
798
814
|
this.statusKey = undefined;
|
|
799
815
|
this.activeAgents = 0;
|
|
800
816
|
this.currentTurn = 0;
|
package/package.json
CHANGED