pi-mega-compact 0.4.20 → 0.4.23
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/conflict-scan.js +201 -0
- package/dist/extensions/dashboard-server.js +3 -3
- package/dist/extensions/mega-compact-driver.js +79 -0
- package/dist/extensions/mega-compact.js +2 -0
- package/dist/extensions/mega-compact.test.js +54 -18
- package/dist/extensions/mega-config.js +10 -0
- package/dist/extensions/mega-conflict-cmds.js +121 -0
- package/dist/extensions/mega-events.js +45 -23
- package/dist/extensions/mega-pipeline.js +80 -8
- package/dist/extensions/mega-runtime.js +14 -20
- package/dist/src/config/dedup.js +4 -1
- package/dist/src/config.js +21 -0
- package/dist/src/dedup/raptor/index.js +28 -6
- package/dist/src/dedup/raptor/promote.test.js +69 -0
- package/dist/src/engine.js +1 -0
- package/dist/src/recall.js +30 -4
- package/dist/src/recall.test.js +28 -0
- package/dist/src/store/backfill.js +5 -6
- package/dist/src/store/compression.js +47 -7
- package/dist/src/store/compression.test.js +48 -0
- package/dist/src/store/sqlite.js +123 -41
- package/dist/src/store.test.js +19 -0
- package/dist/src/vectorStore.js +56 -1
- package/extensions/DASHBOARD.md +3 -3
- package/extensions/conflict-scan.ts +209 -0
- package/extensions/dashboard-server.ts +4 -4
- package/extensions/mega-compact-driver.ts +105 -0
- package/extensions/mega-compact.test.ts +65 -18
- package/extensions/mega-compact.ts +2 -0
- package/extensions/mega-config.ts +25 -0
- package/extensions/mega-conflict-cmds.ts +129 -0
- package/extensions/mega-events.ts +43 -24
- package/extensions/mega-pipeline.ts +86 -9
- package/extensions/mega-runtime.ts +14 -18
- package/package.json +6 -7
- package/src/config/dedup.ts +4 -1
- package/src/config.ts +26 -0
- package/src/dedup/raptor/index.ts +42 -7
- package/src/dedup/raptor/promote.test.ts +82 -0
- package/src/engine.ts +5 -0
- package/src/recall.test.ts +44 -0
- package/src/recall.ts +43 -4
- package/src/store/backfill.ts +10 -11
- package/src/store/compression.test.ts +58 -0
- package/src/store/compression.ts +48 -7
- package/src/store/sqlite.ts +156 -49
- package/src/store.test.ts +22 -0
- package/src/vectorStore.ts +63 -1
- package/dist/extensions/openclaw-mega-compact.js +0 -291
- package/dist/src/minilm.js +0 -92
- package/dist/src/wordpiece.js +0 -129
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* conflict-scan.ts — detect other installed pi extensions that overlap with
|
|
3
|
+
* pi-mega-compact's two owned responsibilities:
|
|
4
|
+
*
|
|
5
|
+
* 1. Conversation auto-compaction (we hook session_before_compact).
|
|
6
|
+
* 2. Durable "save to memory" (we now keep a `memories` table in our SQLite).
|
|
7
|
+
*
|
|
8
|
+
* This is a DETECT-AND-WARN scanner only. pi has no pre-load / veto hook — one
|
|
9
|
+
* extension cannot block another from loading — so we inspect the installed
|
|
10
|
+
* package set at startup and on demand, then report overlaps. No config is
|
|
11
|
+
* mutated. (See memory `pi-memory-mcp-review` for the original conflict pattern.)
|
|
12
|
+
*
|
|
13
|
+
* Pi-agnostic: reads package.json + greps source. No pi runtime types, so it is
|
|
14
|
+
* unit-testable against a fixture node_modules tree.
|
|
15
|
+
*/
|
|
16
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
17
|
+
import { join, dirname } from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
// Marker sets. A package is flagged when its source matches a marker in a
|
|
20
|
+
// category. File-grep (not AST) keeps this dependency-free and fast.
|
|
21
|
+
const MARKERS = {
|
|
22
|
+
// Directly competes with our conversation compaction.
|
|
23
|
+
compaction: [
|
|
24
|
+
"session_before_compact",
|
|
25
|
+
"session_compact",
|
|
26
|
+
"compactSession",
|
|
27
|
+
"autoCompact",
|
|
28
|
+
"auto_compact",
|
|
29
|
+
],
|
|
30
|
+
// Saves durable memory to its own store — the takeover target.
|
|
31
|
+
memory: [
|
|
32
|
+
"MEMORY_TOOL",
|
|
33
|
+
"learn-memory",
|
|
34
|
+
"saveMemory",
|
|
35
|
+
"memoryPolicy",
|
|
36
|
+
"wal_checkpoint",
|
|
37
|
+
"store/db.ts",
|
|
38
|
+
"memoryTool",
|
|
39
|
+
],
|
|
40
|
+
// Tool-output shaping (compact/summarize tool results) — overlap, not a rival.
|
|
41
|
+
toolOutput: [
|
|
42
|
+
"tool_result",
|
|
43
|
+
"ToolResult",
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
/** Resolve the node_modules dir that contains this package (or env override). */
|
|
47
|
+
export function resolveExtensionRoot(selfDir = dirname(fileURLToPath(import.meta.url))) {
|
|
48
|
+
const override = process.env.MEGACOMPACT_EXT_SCAN_DIR;
|
|
49
|
+
if (override && override.trim() !== "")
|
|
50
|
+
return override;
|
|
51
|
+
// selfDir is <root>/extensions or <root>/dist/extensions. Walk up to the
|
|
52
|
+
// node_modules that holds pi-mega-compact.
|
|
53
|
+
let dir = selfDir;
|
|
54
|
+
for (let i = 0; i < 6; i++) {
|
|
55
|
+
const candidate = join(dir, "node_modules");
|
|
56
|
+
if (existsSync(candidate) && existsSync(join(candidate, "pi-mega-compact")))
|
|
57
|
+
return candidate;
|
|
58
|
+
const parent = dirname(dir);
|
|
59
|
+
if (parent === dir)
|
|
60
|
+
break;
|
|
61
|
+
dir = parent;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/** Recursively collect source-ish files under a package, capped to avoid scans. */
|
|
66
|
+
function collectFiles(root, max = 400) {
|
|
67
|
+
const out = [];
|
|
68
|
+
const walk = (dir) => {
|
|
69
|
+
if (out.length >= max)
|
|
70
|
+
return;
|
|
71
|
+
let entries;
|
|
72
|
+
try {
|
|
73
|
+
entries = readdirSync(dir);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
for (const e of entries) {
|
|
79
|
+
if (out.length >= max)
|
|
80
|
+
return;
|
|
81
|
+
const full = join(dir, e);
|
|
82
|
+
let st;
|
|
83
|
+
try {
|
|
84
|
+
st = statSync(full);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (st.isDirectory()) {
|
|
90
|
+
if (e === "node_modules" || e === ".git")
|
|
91
|
+
continue;
|
|
92
|
+
walk(full);
|
|
93
|
+
}
|
|
94
|
+
else if (/\.(ts|js|mjs|cjs|json|md)$/.test(e)) {
|
|
95
|
+
out.push(full);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
walk(root);
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/** Grep a package's source for any marker in `keys`; return matched markers. */
|
|
103
|
+
function matchMarkers(pkgDir, keys) {
|
|
104
|
+
const found = new Set();
|
|
105
|
+
let files;
|
|
106
|
+
try {
|
|
107
|
+
files = collectFiles(pkgDir);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
for (const f of files) {
|
|
113
|
+
let text;
|
|
114
|
+
try {
|
|
115
|
+
text = readFileSync(f, "utf-8");
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
for (const m of keys) {
|
|
121
|
+
if (text.includes(m))
|
|
122
|
+
found.add(m);
|
|
123
|
+
}
|
|
124
|
+
if (found.size === keys.length)
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
return [...found];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Scan installed extensions for overlaps with pi-mega-compact.
|
|
131
|
+
* @param selfName package name to skip (defaults to this package's name).
|
|
132
|
+
*/
|
|
133
|
+
export function detectConflicts(selfName = "pi-mega-compact") {
|
|
134
|
+
const root = resolveExtensionRoot();
|
|
135
|
+
const scanned = [];
|
|
136
|
+
const conflicts = [];
|
|
137
|
+
if (!root || !existsSync(root))
|
|
138
|
+
return { scanned, conflicts };
|
|
139
|
+
let entries;
|
|
140
|
+
try {
|
|
141
|
+
entries = readdirSync(root);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return { scanned, conflicts };
|
|
145
|
+
}
|
|
146
|
+
for (const name of entries) {
|
|
147
|
+
const pkgDir = join(root, name);
|
|
148
|
+
if (!statSync(pkgDir).isDirectory())
|
|
149
|
+
continue;
|
|
150
|
+
const pkgJson = join(pkgDir, "package.json");
|
|
151
|
+
if (!existsSync(pkgJson))
|
|
152
|
+
continue;
|
|
153
|
+
let pkg;
|
|
154
|
+
try {
|
|
155
|
+
pkg = JSON.parse(readFileSync(pkgJson, "utf-8"));
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const pkgName = pkg.name ?? name;
|
|
161
|
+
if (pkgName === selfName)
|
|
162
|
+
continue;
|
|
163
|
+
// Only consider packages that declare pi extensions.
|
|
164
|
+
if (!pkg.pi || !Array.isArray(pkg.pi.extensions) || pkg.pi.extensions.length === 0)
|
|
165
|
+
continue;
|
|
166
|
+
scanned.push(pkgName);
|
|
167
|
+
const memHits = matchMarkers(pkgDir, MARKERS.memory);
|
|
168
|
+
const compHits = matchMarkers(pkgDir, MARKERS.compaction);
|
|
169
|
+
const toolHits = matchMarkers(pkgDir, MARKERS.toolOutput);
|
|
170
|
+
if (compHits.length > 0) {
|
|
171
|
+
conflicts.push({
|
|
172
|
+
package: pkgName,
|
|
173
|
+
severity: "high",
|
|
174
|
+
kind: "compaction",
|
|
175
|
+
evidence: compHits,
|
|
176
|
+
recommendation: "Disabling recommended — competes with pi-mega-compact's conversation compaction.",
|
|
177
|
+
});
|
|
178
|
+
continue; // compaction is the dominant conflict; don't double-flag.
|
|
179
|
+
}
|
|
180
|
+
if (memHits.length > 0) {
|
|
181
|
+
conflicts.push({
|
|
182
|
+
package: pkgName,
|
|
183
|
+
severity: "high",
|
|
184
|
+
kind: "memory",
|
|
185
|
+
evidence: memHits,
|
|
186
|
+
recommendation: "pi-mega-compact now owns save-to-memory (/mega-memory, its own SQLite). Disable this to avoid duplicate memory stores.",
|
|
187
|
+
});
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (toolHits.length > 0) {
|
|
191
|
+
conflicts.push({
|
|
192
|
+
package: pkgName,
|
|
193
|
+
severity: "info",
|
|
194
|
+
kind: "tool-output",
|
|
195
|
+
evidence: toolHits,
|
|
196
|
+
recommendation: "Shapes tool output (summarize/compact tool results). Generally compatible; no action needed.",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { scanned, conflicts };
|
|
201
|
+
}
|
|
@@ -15,7 +15,7 @@ import { existsSync, mkdirSync, readFileSync, unlinkSync, watch, writeFileSync }
|
|
|
15
15
|
import { homedir } from "node:os";
|
|
16
16
|
import { join, dirname } from "node:path";
|
|
17
17
|
import { fileURLToPath } from "node:url";
|
|
18
|
-
import
|
|
18
|
+
import { DatabaseSync } from "node:sqlite";
|
|
19
19
|
// --- Multi-repo index (Phase 5b) ------------------------------------------------
|
|
20
20
|
// The extension writes a machine-wide repo registry into a single SQLite DB
|
|
21
21
|
// (<indexDir>/index.sqlite) as the concurrency-safe write path; the dashboard
|
|
@@ -42,8 +42,8 @@ function readIndex() {
|
|
|
42
42
|
let db;
|
|
43
43
|
try {
|
|
44
44
|
// Read-only + immutable WAL so a concurrent writer's WAL never blocks us.
|
|
45
|
-
db = new
|
|
46
|
-
db.
|
|
45
|
+
db = new DatabaseSync(indexPath, { readOnly: true });
|
|
46
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
47
47
|
const rows = db
|
|
48
48
|
.prepare("SELECT * FROM repo_registry ORDER BY last_seen DESC")
|
|
49
49
|
.all();
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-compact-driver.ts — the durable-trim driver (Fix B).
|
|
3
|
+
*
|
|
4
|
+
* The read-path token-growth bug: the old design cancelled pi's native
|
|
5
|
+
* compaction (`{ cancel: true }`) and did its own ephemeral `context`-hook
|
|
6
|
+
* drop. That drop only affected the outgoing request — the on-disk transcript
|
|
7
|
+
* was never trimmed (the session manager is read-only for extensions). So on
|
|
8
|
+
* resume pi reloaded the FULL transcript and we ADDED a recall block on top →
|
|
9
|
+
* more tokens than before compaction.
|
|
10
|
+
*
|
|
11
|
+
* The fix: on `session_before_compact` we RUN the Trident pipeline to produce a
|
|
12
|
+
* genuinely compressed summary, then RETURN it as a `CompactionResult`. pi
|
|
13
|
+
* durably writes our summary into a `compactionSummary` entry AND truncates the
|
|
14
|
+
* transcript from `firstKeptEntryId`. After that, resume reloads the already-
|
|
15
|
+
* trimmed transcript (summary baked in) — no additive re-injection, no token
|
|
16
|
+
* growth.
|
|
17
|
+
*
|
|
18
|
+
* We reuse pi's `preparation.firstKeptEntryId` (pi already computed the cut
|
|
19
|
+
* honoring the anchor-floor + tool-pair guards — PREVENT-PI-002) rather than
|
|
20
|
+
* recomputing it, so we cannot hand pi a boundary that splits a tool pair.
|
|
21
|
+
*/
|
|
22
|
+
import { compactSession } from "../src/engine.js";
|
|
23
|
+
import { toEngineMessages } from "../src/adapt.js";
|
|
24
|
+
import { estimateBlockTokens, estimateSessionTokens } from "../src/tokens.js";
|
|
25
|
+
import { recallRaptorRootSummary } from "../src/dedup/raptor/index.js";
|
|
26
|
+
/**
|
|
27
|
+
* Build our durable compaction result from pi's pre-computed preparation.
|
|
28
|
+
*
|
|
29
|
+
* Returns undefined when there is nothing to summarize (pi will then run its
|
|
30
|
+
* own native compaction, or skip). Never throws for "empty" — best-effort.
|
|
31
|
+
*/
|
|
32
|
+
export function driveNativeCompaction(event, runtime, config) {
|
|
33
|
+
const prep = event.preparation;
|
|
34
|
+
if (!prep)
|
|
35
|
+
return undefined;
|
|
36
|
+
const sid = runtime.rt.sessionId;
|
|
37
|
+
const messagesToSummarize = prep.messagesToSummarize ?? [];
|
|
38
|
+
if (messagesToSummarize.length === 0)
|
|
39
|
+
return undefined;
|
|
40
|
+
const engineView = toEngineMessages(messagesToSummarize);
|
|
41
|
+
// We don't drop anything here — pi keeps from prep.firstKeptEntryId. We only
|
|
42
|
+
// summarize the region pi is about to discard.
|
|
43
|
+
const keepFrom = engineView.length;
|
|
44
|
+
const result = compactSession({
|
|
45
|
+
sessionId: sid,
|
|
46
|
+
messages: engineView,
|
|
47
|
+
keepFrom,
|
|
48
|
+
timestamp: Date.now(),
|
|
49
|
+
useExtractiveSummary: true,
|
|
50
|
+
}, runtime.store);
|
|
51
|
+
if (result.skipped)
|
|
52
|
+
return undefined;
|
|
53
|
+
// Prefer the RAPTOR root summary when the tree is built + enabled (Fix D):
|
|
54
|
+
// it is a session-level compressed summary, broader than one slice's. Fall
|
|
55
|
+
// back to the extractive topicSummary of this slice.
|
|
56
|
+
let summary = result.summary;
|
|
57
|
+
if (config.raptorEnabled) {
|
|
58
|
+
const root = recallRaptorRootSummary(sid, runtime.currentStateDir);
|
|
59
|
+
if (root)
|
|
60
|
+
summary = root;
|
|
61
|
+
}
|
|
62
|
+
const tokensBefore = prep.tokensBefore ?? estimateSessionTokens(engineView);
|
|
63
|
+
const summaryTokens = estimateBlockTokens(summary);
|
|
64
|
+
// pi keeps the tail from firstKeptEntryId; our summary replaces the discarded
|
|
65
|
+
// region. Honest saved = discarded-region tokens − our summary tokens.
|
|
66
|
+
const savedTokens = Math.max(0, tokensBefore - summaryTokens);
|
|
67
|
+
runtime.rt.lastCompactedFrom = keepFrom;
|
|
68
|
+
runtime.rt.lastCompactedTokens = tokensBefore;
|
|
69
|
+
runtime.rt.tokensSaved += savedTokens;
|
|
70
|
+
runtime.rt.persistedThisSession = true;
|
|
71
|
+
return {
|
|
72
|
+
compaction: {
|
|
73
|
+
summary,
|
|
74
|
+
firstKeptEntryId: prep.firstKeptEntryId,
|
|
75
|
+
tokensBefore,
|
|
76
|
+
estimatedTokensAfter: summaryTokens,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -29,10 +29,12 @@ import { MegaRuntime } from "./mega-runtime.js";
|
|
|
29
29
|
import { registerEventHandlers } from "./mega-events.js";
|
|
30
30
|
import { registerCommands } from "./mega-commands.js";
|
|
31
31
|
import { registerDashboardCommands } from "./mega-dashboard-cmds.js";
|
|
32
|
+
import { registerConflictCommands } from "./mega-conflict-cmds.js";
|
|
32
33
|
export default function (pi) {
|
|
33
34
|
const config = loadConfig();
|
|
34
35
|
const runtime = new MegaRuntime(config);
|
|
35
36
|
registerEventHandlers(pi, runtime, config);
|
|
36
37
|
registerCommands(pi, runtime, config);
|
|
37
38
|
registerDashboardCommands(pi, runtime);
|
|
39
|
+
registerConflictCommands(pi, runtime);
|
|
38
40
|
}
|
|
@@ -42,6 +42,7 @@ function harness(opts = {}) {
|
|
|
42
42
|
let statusKey;
|
|
43
43
|
let statusText;
|
|
44
44
|
const notifies = [];
|
|
45
|
+
const compactCalls = [];
|
|
45
46
|
// Minimal AgentMessage factory for the session we project into the extension.
|
|
46
47
|
function msg(role, text, toolName) {
|
|
47
48
|
if (role === "assistant" && toolName) {
|
|
@@ -100,7 +101,27 @@ function harness(opts = {}) {
|
|
|
100
101
|
hasPendingMessages: () => false,
|
|
101
102
|
shutdown: () => { },
|
|
102
103
|
getContextUsage: () => ({ tokens: 200000, contextWindow: 200000, percent: 100 }),
|
|
103
|
-
|
|
104
|
+
// Faithful mock: ctx.compact() starts pi's flow, which fires the
|
|
105
|
+
// session_before_compact handler (where WE supply the durable trim).
|
|
106
|
+
compact: (opts) => {
|
|
107
|
+
compactCalls.push(opts);
|
|
108
|
+
if (handlers["session_before_compact"]) {
|
|
109
|
+
return handlers["session_before_compact"]({
|
|
110
|
+
type: "session_before_compact",
|
|
111
|
+
reason: "threshold",
|
|
112
|
+
willRetry: false,
|
|
113
|
+
signal: undefined,
|
|
114
|
+
// pi computed the cut honoring anchor floor + tool-pair (PREVENT-PI-002);
|
|
115
|
+
// our handler reuses it as firstKeptEntryId.
|
|
116
|
+
preparation: {
|
|
117
|
+
firstKeptEntryId: "e2",
|
|
118
|
+
messagesToSummarize: session.slice(0, 2),
|
|
119
|
+
tokensBefore: 500,
|
|
120
|
+
},
|
|
121
|
+
}, makeCtx());
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
},
|
|
104
125
|
getSystemPrompt: () => "system base",
|
|
105
126
|
...over,
|
|
106
127
|
};
|
|
@@ -133,13 +154,13 @@ function harness(opts = {}) {
|
|
|
133
154
|
const mod = require("./mega-compact.js");
|
|
134
155
|
mod.default(pi);
|
|
135
156
|
return {
|
|
136
|
-
stateDir, handlers, commands, appended, get status() { return { statusKey, statusText }; }, notifies,
|
|
157
|
+
stateDir, handlers, commands, appended, get status() { return { statusKey, statusText }; }, notifies, compactCalls,
|
|
137
158
|
fire: (ev, event, ctx) => handlers[ev](event, ctx),
|
|
138
159
|
ctx: makeCtx,
|
|
139
160
|
session,
|
|
140
161
|
};
|
|
141
162
|
}
|
|
142
|
-
test("auto-trigger: past threshold persists a chkpt and
|
|
163
|
+
test("auto-trigger: past threshold persists a chkpt and starts a durable trim", async () => {
|
|
143
164
|
const h = harness();
|
|
144
165
|
const messages = h.session;
|
|
145
166
|
const ctx = h.ctx({ getContextUsage: () => ({ tokens: 200000, contextWindow: 200000, percent: 100 }) });
|
|
@@ -148,25 +169,40 @@ test("auto-trigger: past threshold persists a chkpt and drops context", async ()
|
|
|
148
169
|
const { listCheckpoints } = await import("../src/store/sqlite.js");
|
|
149
170
|
assert.ok(listCheckpoints("sess_ext_001", h.stateDir).length > 0, "checkpoint persisted to local vector db");
|
|
150
171
|
assert.equal(h.appended.some((a) => a.t === "mega-compact-marker"), true, "marker sentinel appended");
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
172
|
+
// The context handler no longer drops messages itself (that was ephemeral —
|
|
173
|
+
// the read-path token-growth bug). It triggers pi's compaction flow, which
|
|
174
|
+
// calls our session_before_compact handler to supply the DURABLE trim.
|
|
175
|
+
assert.equal(res, undefined, "context handler returns nothing (no local drop)");
|
|
176
|
+
assert.equal(h.compactCalls.length, 1, "ctx.compact() called to start durable trim");
|
|
177
|
+
// The durable trim was supplied (summary + firstKeptEntryId from pi's prep).
|
|
178
|
+
assert.ok(h.compactCalls[0] !== undefined, "compaction flow executed");
|
|
154
179
|
});
|
|
155
|
-
test("session_before_compact
|
|
180
|
+
test("session_before_compact supplies our durable trim (not pi's summary)", async () => {
|
|
156
181
|
const h = harness();
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
182
|
+
// pi fires session_before_compact with its own computed preparation.
|
|
183
|
+
const res = await h.fire("session_before_compact", {
|
|
184
|
+
type: "session_before_compact",
|
|
185
|
+
reason: "overflow",
|
|
186
|
+
willRetry: true,
|
|
187
|
+
preparation: { firstKeptEntryId: "e2", messagesToSummarize: h.session.slice(0, 2), tokensBefore: 500 },
|
|
188
|
+
signal: undefined,
|
|
189
|
+
}, h.ctx());
|
|
190
|
+
assert.ok(res && res.compaction, "returns a compaction result");
|
|
191
|
+
assert.equal(res.compaction.firstKeptEntryId, "e2", "reuses pi's cut boundary (PREVENT-PI-002 safe)");
|
|
192
|
+
assert.ok(typeof res.compaction.summary === "string" && res.compaction.summary.length > 0, "our summary supplied");
|
|
193
|
+
assert.ok(res.compaction.tokensBefore >= 0, "tokensBefore reported");
|
|
163
194
|
});
|
|
164
|
-
test("session_before_compact
|
|
195
|
+
test("session_before_compact falls back to pi when nothing to summarize", async () => {
|
|
165
196
|
const h = harness();
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
197
|
+
// Empty preparation → no messages to summarize → return {} so pi compacts natively.
|
|
198
|
+
const res = await h.fire("session_before_compact", {
|
|
199
|
+
type: "session_before_compact",
|
|
200
|
+
reason: "threshold",
|
|
201
|
+
willRetry: false,
|
|
202
|
+
preparation: { firstKeptEntryId: "e0", messagesToSummarize: [], tokensBefore: 0 },
|
|
203
|
+
signal: undefined,
|
|
204
|
+
}, h.ctx());
|
|
205
|
+
assert.deepEqual(res, {}, "no compaction supplied → pi runs its own");
|
|
170
206
|
});
|
|
171
207
|
test("resume auto-inline stages recall into the system prompt", async () => {
|
|
172
208
|
const h = harness();
|
|
@@ -46,6 +46,12 @@ function resolveThreshold() {
|
|
|
46
46
|
const tier = (raw in COMPACT_TIERS ? raw : "low");
|
|
47
47
|
return { tier, thresholdTokens: COMPACT_TIERS[tier] };
|
|
48
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Pressure helpers for adaptive compression (Fix E) live in src/config.ts
|
|
51
|
+
* (pi-agnostic) so unit tests can import them without the pi runtime. Re-export
|
|
52
|
+
* here so the extension has one import surface.
|
|
53
|
+
*/
|
|
54
|
+
export { pressureFromPct, preserveRecentForPressure } from "../src/config.js";
|
|
49
55
|
/** Build the resolved config from env + defaults. */
|
|
50
56
|
export function loadConfig() {
|
|
51
57
|
const { tier, thresholdTokens } = resolveThreshold();
|
|
@@ -58,10 +64,14 @@ export function loadConfig() {
|
|
|
58
64
|
thresholdTokens,
|
|
59
65
|
anchorUserMessages: envFlag("MEGACOMPACT_ANCHOR_USER_MESSAGES", 3),
|
|
60
66
|
preserveRecent: envFlag("MEGACOMPACT_PRESERVE_RECENT", 4),
|
|
67
|
+
preserveRecentMin: envFlag("MEGACOMPACT_PRESERVE_RECENT_MIN", 2),
|
|
61
68
|
auto: envBool("MEGACOMPACT_AUTO", true),
|
|
62
69
|
autoInline: envBool("MEGACOMPACT_AUTO_INLINE", true),
|
|
63
70
|
autoInlineK: envFlag("MEGACOMPACT_AUTO_INLINE_K", 3),
|
|
64
71
|
dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
|
|
72
|
+
raptorEnabled: envBool("MEGACOMPACT_RAPTOR_ENABLED", true),
|
|
73
|
+
recallMaxTokens: envFlag("MEGACOMPACT_RECALL_MAX_TOKENS", 1500),
|
|
74
|
+
windowDedupe: envBool("MEGACOMPACT_WINDOW_DEDUPE", true),
|
|
65
75
|
debug: envBool("MEGACOMPACT_DEBUG", false),
|
|
66
76
|
};
|
|
67
77
|
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-conflict-cmds.ts — extension conflict validator + save-to-memory command.
|
|
3
|
+
*
|
|
4
|
+
* Detects other installed extensions that overlap with pi-mega-compact
|
|
5
|
+
* (conversation compaction, or save-to-memory) and WARNs — pi has no pre-load
|
|
6
|
+
* veto hook, so this is detect-and-warn only. Also registers /mega-memory, our
|
|
7
|
+
* own durable memory store in SQLite (the takeover of memory extensions).
|
|
8
|
+
*/
|
|
9
|
+
import { detectConflicts } from "./conflict-scan.js";
|
|
10
|
+
import { addMemory, listMemories, searchMemories, recallMemory } from "../src/store/sqlite.js";
|
|
11
|
+
import { resolveRepoRoot } from "./mega-config.js";
|
|
12
|
+
/** Run the conflict scan and format a human-readable report. */
|
|
13
|
+
export function validateExtensions() {
|
|
14
|
+
const report = detectConflicts();
|
|
15
|
+
const lines = [];
|
|
16
|
+
if (report.conflicts.length === 0) {
|
|
17
|
+
lines.push(`[mega-compact] conflict check: ${report.scanned.length} extensions scanned, no overlaps.`);
|
|
18
|
+
return { report, lines };
|
|
19
|
+
}
|
|
20
|
+
const high = report.conflicts.filter((c) => c.severity === "high");
|
|
21
|
+
lines.push(`[mega-compact] conflict check: ${report.scanned.length} scanned, ${report.conflicts.length} overlap(s), ${high.length} high-severity.`);
|
|
22
|
+
for (const c of report.conflicts) {
|
|
23
|
+
const tag = c.severity === "high" ? "⚠ HIGH" : "ℹ info";
|
|
24
|
+
lines.push(` ${tag} ${c.package} — ${c.kind} — ${c.recommendation}`);
|
|
25
|
+
}
|
|
26
|
+
return { report, lines };
|
|
27
|
+
}
|
|
28
|
+
/** Run the scan at activation and surface a one-line warning if needed. */
|
|
29
|
+
export function runLoadTimeConflictCheck() {
|
|
30
|
+
try {
|
|
31
|
+
const { lines } = validateExtensions();
|
|
32
|
+
const high = lines.filter((l) => l.includes("⚠ HIGH"));
|
|
33
|
+
if (high.length > 0) {
|
|
34
|
+
// Non-fatal: just inform on stderr; the dashboard/commands carry details.
|
|
35
|
+
console.warn(lines.join("\n"));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
/* best-effort; never block session load */
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function memoryLine(m) {
|
|
43
|
+
const tags = m.tags.length ? ` [${m.tags.join(", ")}]` : "";
|
|
44
|
+
const snap = m.content.length > 80 ? m.content.slice(0, 77) + "…" : m.content;
|
|
45
|
+
return `#${m.id} (${m.kind})${tags}: ${snap}`;
|
|
46
|
+
}
|
|
47
|
+
/** Register conflict-check + memory commands. */
|
|
48
|
+
export function registerConflictCommands(pi, runtime) {
|
|
49
|
+
runLoadTimeConflictCheck();
|
|
50
|
+
pi.registerCommand("mega-compat-check", {
|
|
51
|
+
description: "Scan installed extensions for overlaps with pi-mega-compact (compaction / save-to-memory) and warn.",
|
|
52
|
+
handler: async (_args, ctx) => {
|
|
53
|
+
const { lines } = validateExtensions();
|
|
54
|
+
for (const l of lines)
|
|
55
|
+
ctx.ui.notify(l);
|
|
56
|
+
if (lines.length === 1) {
|
|
57
|
+
ctx.ui.notify("[mega-compact] You own compaction + memory; no conflicting extensions detected.");
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
pi.registerCommand("mega-memory", {
|
|
62
|
+
description: "Save and recall durable memory in pi-mega-compact's SQLite store. Usage: /mega-memory save <text> | list | search <q> | recall <id>",
|
|
63
|
+
handler: async (args, ctx) => {
|
|
64
|
+
const repo = resolveRepoRoot(ctx.cwd) ?? runtime.currentStateDir;
|
|
65
|
+
const parts = args.trim().split(/\s+/);
|
|
66
|
+
const sub = parts[0]?.toLowerCase() ?? "list";
|
|
67
|
+
if (sub === "save") {
|
|
68
|
+
const text = args.trim().slice(4).trim();
|
|
69
|
+
if (!text) {
|
|
70
|
+
ctx.ui.notify("[mega-memory] usage: /mega-memory save <text>");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
// Optional "#tag #tag" parsing from the tail.
|
|
74
|
+
const tagMatches = [...text.matchAll(/#([\w-]+)/g)].map((m) => m[1]);
|
|
75
|
+
const content = text.replace(/#[\w-]+/g, "").trim();
|
|
76
|
+
const id = addMemory({ content, tags: tagMatches }, repo, runtime.currentStateDir);
|
|
77
|
+
ctx.ui.notify(`[mega-memory] saved #${id} to ${repo.split(/[\\/]/).pop()}`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (sub === "search") {
|
|
81
|
+
const q = parts.slice(1).join(" ").trim();
|
|
82
|
+
if (!q) {
|
|
83
|
+
ctx.ui.notify("[mega-memory] usage: /mega-memory search <query>");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const hits = searchMemories(q, repo, 50, runtime.currentStateDir);
|
|
87
|
+
if (!hits.length) {
|
|
88
|
+
ctx.ui.notify("[mega-memory] no memories match.");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const m of hits)
|
|
92
|
+
ctx.ui.notify(memoryLine(m));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (sub === "recall") {
|
|
96
|
+
const id = Number(parts[1]);
|
|
97
|
+
if (!Number.isFinite(id) || parts[1] === undefined) {
|
|
98
|
+
ctx.ui.notify("[mega-memory] usage: /mega-memory recall <id>");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (recallMemory(id, runtime.currentStateDir)) {
|
|
102
|
+
const found = listMemories(repo, 1000, runtime.currentStateDir).find((m) => m.id === id);
|
|
103
|
+
ctx.ui.notify(found ? `[mega-memory] ${memoryLine(found)}` : `[mega-memory] recalled #${id}`);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
ctx.ui.notify(`[mega-memory] #${id} not found.`);
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// default: list
|
|
111
|
+
const all = listMemories(repo, 50, runtime.currentStateDir);
|
|
112
|
+
if (!all.length) {
|
|
113
|
+
ctx.ui.notify("[mega-memory] no saved memories yet. Use /mega-memory save <text>.");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
ctx.ui.notify(`[mega-memory] ${all.length} saved to ${repo.split(/[\\/]/).pop()}:`);
|
|
117
|
+
for (const m of all)
|
|
118
|
+
ctx.ui.notify(memoryLine(m));
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|