pi-mega-compact 0.7.1 → 0.7.2
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-pipeline.js +17 -52
- package/dist/extensions/mega-runtime.js +96 -183
- package/extensions/mega-compact-driver.ts +56 -56
- package/extensions/mega-pipeline.ts +394 -481
- package/extensions/mega-runtime.ts +739 -912
- package/package.json +1 -1
|
@@ -30,13 +30,13 @@ import type { MegaConfig } from "./mega-config.js";
|
|
|
30
30
|
import { recallRaptorRootSummary } from "../src/dedup/raptor/index.js";
|
|
31
31
|
|
|
32
32
|
export interface NativeCompactionResult {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
/** Our trimmed summary + the pi entry to keep from (durable trim). */
|
|
34
|
+
compaction: {
|
|
35
|
+
summary: string;
|
|
36
|
+
firstKeptEntryId: string;
|
|
37
|
+
tokensBefore: number;
|
|
38
|
+
estimatedTokensAfter: number;
|
|
39
|
+
};
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
@@ -46,61 +46,61 @@ export interface NativeCompactionResult {
|
|
|
46
46
|
* own native compaction, or skip). Never throws for "empty" — best-effort.
|
|
47
47
|
*/
|
|
48
48
|
export function driveNativeCompaction(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
event: SessionBeforeCompactEvent,
|
|
50
|
+
runtime: MegaRuntime,
|
|
51
|
+
config: MegaConfig,
|
|
52
52
|
): NativeCompactionResult | undefined {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
const prep = event.preparation;
|
|
54
|
+
if (!prep) return undefined;
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const sid = runtime.rt.sessionId;
|
|
57
|
+
const messagesToSummarize: AgentMessage[] = prep.messagesToSummarize ?? [];
|
|
58
|
+
if (messagesToSummarize.length === 0) return undefined;
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const engineView = toEngineMessages(messagesToSummarize);
|
|
61
|
+
// We don't drop anything here — pi keeps from prep.firstKeptEntryId. We only
|
|
62
|
+
// summarize the region pi is about to discard.
|
|
63
|
+
const keepFrom = engineView.length;
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
65
|
+
const result = compactSession(
|
|
66
|
+
{
|
|
67
|
+
sessionId: sid,
|
|
68
|
+
messages: engineView,
|
|
69
|
+
keepFrom,
|
|
70
|
+
timestamp: Date.now(),
|
|
71
|
+
useExtractiveSummary: true,
|
|
72
|
+
},
|
|
73
|
+
runtime.store,
|
|
74
|
+
);
|
|
75
|
+
if (result.skipped) return undefined;
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
77
|
+
// Prefer the RAPTOR root summary when the tree is built + enabled (Fix D):
|
|
78
|
+
// it is a session-level compressed summary, broader than one slice's. Fall
|
|
79
|
+
// back to the extractive topicSummary of this slice.
|
|
80
|
+
let summary = result.summary;
|
|
81
|
+
if (config.raptorEnabled) {
|
|
82
|
+
const root = recallRaptorRootSummary(sid, runtime.currentStateDir);
|
|
83
|
+
if (root) summary = root;
|
|
84
|
+
}
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
const tokensBefore = prep.tokensBefore ?? estimateSessionTokens(engineView);
|
|
87
|
+
const summaryTokens = estimateBlockTokens(summary);
|
|
88
|
+
// pi keeps the tail from firstKeptEntryId; our summary replaces the discarded
|
|
89
|
+
// region. Honest saved = discarded-region tokens − our summary tokens.
|
|
90
|
+
const savedTokens = Math.max(0, tokensBefore - summaryTokens);
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
runtime.rt.lastCompactedFrom = keepFrom;
|
|
93
|
+
runtime.rt.lastCompactedTokens = tokensBefore;
|
|
94
|
+
runtime.rt.tokensSaved += savedTokens;
|
|
95
|
+
runtime.rt.lastCompactAt = Date.now();
|
|
96
|
+
runtime.rt.persistedThisSession = true;
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
98
|
+
return {
|
|
99
|
+
compaction: {
|
|
100
|
+
summary,
|
|
101
|
+
firstKeptEntryId: prep.firstKeptEntryId,
|
|
102
|
+
tokensBefore,
|
|
103
|
+
estimatedTokensAfter: summaryTokens,
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
106
|
}
|