pi-mega-compact 0.13.1 → 0.13.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/dashboard-server/api-contracts/embedder-health.js +11 -0
- package/dist/extensions/dashboard-server/api-contracts/endpoints/registry.js +225 -0
- package/dist/extensions/dashboard-server/api-contracts/endpoints/types.js +1 -0
- package/dist/extensions/dashboard-server/api-contracts/endpoints.js +5 -224
- package/dist/extensions/dashboard-server/api-contracts/raptor.js +8 -0
- package/dist/extensions/dashboard-server/api-contracts.test/_helpers.js +32 -0
- package/dist/extensions/dashboard-server/routes-cache.test/_helpers.js +48 -0
- package/dist/extensions/dashboard-server/routes-embedder-health.js +67 -0
- package/dist/extensions/dashboard-server/routes-raptor.js +88 -0
- package/dist/extensions/dashboard-server/routes-setup.js +1 -1
- package/dist/extensions/dashboard-server/routes.js +2 -0
- package/dist/extensions/dashboard-server/server.js +5 -1
- package/dist/extensions/mega-cache-replay.test/_helpers.js +175 -0
- package/dist/extensions/mega-commands/dataCommands.js +151 -0
- package/dist/extensions/mega-commands/helpers.js +61 -0
- package/dist/extensions/mega-commands/historyCommands.js +97 -0
- package/dist/extensions/mega-commands/setupCommand.js +200 -0
- package/dist/extensions/mega-commands.js +14 -499
- package/dist/extensions/mega-compact-s38.test/_helpers.js +194 -0
- package/dist/extensions/mega-compact.test/_helpers.js +225 -0
- package/dist/extensions/mega-events/agent-handlers/agentEndHandler.js +211 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/cacheStripe.js +49 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/contextHealth.js +33 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/errorRetry.js +387 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/event.js +7 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/gameScoring.js +55 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/lengthStop.js +12 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/memoryReview.js +22 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler/recordTurnRow.js +38 -0
- package/dist/extensions/mega-events/agent-handlers/turnEndHandler.js +36 -0
- package/dist/extensions/mega-events/agent-handlers.js +4 -799
- package/dist/extensions/mega-events/context-handler/afterCompact.js +163 -0
- package/dist/extensions/mega-events/context-handler/messageText.js +12 -0
- package/dist/extensions/mega-events/context-handler/tailResult.js +46 -0
- package/dist/extensions/mega-events/context-handler.js +11 -198
- package/dist/extensions/mega-events/separated-prompt.test/_helpers.js +25 -0
- package/dist/src/config.js +2 -0
- package/dist/src/dedup-engine.test/_helpers.js +55 -0
- package/dist/src/e2e.test/_helpers.js +52 -0
- package/dist/src/embedder.js +1 -0
- package/dist/src/httpEmbedder.js +6 -0
- package/dist/src/hyde.js +146 -0
- package/dist/src/importance.test/_helpers.js +5 -0
- package/dist/src/memoryGraph/sources/checkpoints.js +78 -0
- package/dist/src/memoryGraph/sources/edges.js +17 -0
- package/dist/src/memoryGraph/sources/flags.js +22 -0
- package/dist/src/memoryGraph/sources/helpers.js +14 -0
- package/dist/src/memoryGraph/sources/memories.js +88 -0
- package/dist/src/memoryGraph/sources/raptor.js +26 -0
- package/dist/src/memoryGraph/sources/turns.js +176 -0
- package/dist/src/memoryGraph/sources.js +15 -441
- package/dist/src/memoryGraph-gates.test/_helpers.js +120 -0
- package/dist/src/ratio.bench.test/_helpers.js +198 -0
- package/dist/src/recall/async.js +112 -0
- package/dist/src/recall/format.js +149 -0
- package/dist/src/recall/memory.js +62 -0
- package/dist/src/recall/reformulate.js +82 -0
- package/dist/src/recall/sync.js +169 -0
- package/dist/src/recall/types.js +1 -0
- package/dist/src/recall.js +13 -530
- package/dist/src/store/sqlite/schema/core.js +232 -0
- package/dist/src/store/sqlite/schema/game.js +65 -0
- package/dist/src/store/sqlite/schema/plan-v2.js +107 -0
- package/dist/src/store/sqlite/schema/turns.js +59 -0
- package/dist/src/store/sqlite/schema.js +8 -441
- package/dist/src/store/turns/contract-compliance.test/_helpers.js +21 -0
- package/dist/src/store/turns/sqlite-store/admin.js +171 -0
- package/dist/src/store/turns/sqlite-store/ctx.js +1 -0
- package/dist/src/store/turns/sqlite-store/read.js +111 -0
- package/dist/src/store/turns/sqlite-store/rows.js +42 -0
- package/dist/src/store/turns/sqlite-store/write.js +102 -0
- package/dist/src/store/turns/sqlite-store.js +31 -367
- package/dist/src/vectorStore/class.js +356 -0
- package/dist/src/vectorStore/hash.js +10 -0
- package/dist/src/vectorStore/types.js +2 -0
- package/dist/src/vectorStore.js +8 -359
- package/dist/src/vectorStore.test/_helpers.js +17 -0
- package/extensions/dashboard-client/dist/assets/{CacheTab-8HRroK9K.js → CacheTab-DPRHjYZ0.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-8HRroK9K.js.map → CacheTab-DPRHjYZ0.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-ncobZw-I.js → EventsTab-CgwYq-Qf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-ncobZw-I.js.map → EventsTab-CgwYq-Qf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-C6BI1EZG.js → HealthTab-BgOCjVzR.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-C6BI1EZG.js.map → HealthTab-BgOCjVzR.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/MaintenanceTab-Yw1yFpsy.js +2 -0
- package/extensions/dashboard-client/dist/assets/MaintenanceTab-Yw1yFpsy.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-yHUX7Yzi.js +2 -0
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-yHUX7Yzi.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{MetricsTab-B88XTCMk.js → MetricsTab-CcH9fP8P.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-B88XTCMk.js.map → MetricsTab-CcH9fP8P.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CGZrSia-.js → OverviewTab-BOs8ZNev.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CGZrSia-.js.map → OverviewTab-BOs8ZNev.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-BZo5cNS1.js → ReposTab-B-MfSPvg.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-BZo5cNS1.js.map → ReposTab-B-MfSPvg.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-C2RokJcw.js → SessionsTab-BmeN649Q.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-C2RokJcw.js.map → SessionsTab-BmeN649Q.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/SetupTab-nOknoph5.js +2 -0
- package/extensions/dashboard-client/dist/assets/SetupTab-nOknoph5.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CewYB2aA.js → TimeSavedCard-DnPg0L0z.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CewYB2aA.js.map → TimeSavedCard-DnPg0L0z.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TopicsTab-DpVNIuaL.js → TopicsTab-BODHOg_b.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TopicsTab-DpVNIuaL.js.map → TopicsTab-BODHOg_b.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-KeuKKVC9.js → TurnsTab-BK0I7hKr.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-KeuKKVC9.js.map → TurnsTab-BK0I7hKr.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-Cucgohga.js → index-mOlljTFI.js} +9 -9
- package/extensions/dashboard-client/dist/assets/index-mOlljTFI.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{useSSE-DJsqdPXC.js → useSSE-BYOtv1-3.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-DJsqdPXC.js.map → useSSE-BYOtv1-3.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/client.ts +16 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab/ActionsCard.tsx +178 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab/DbStatsCard.tsx +140 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab/DebugBundleCard.tsx +109 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab/HealthMitigationCard.tsx +98 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab/SchemaHealthCard.tsx +108 -0
- package/extensions/dashboard-client/src/tabs/MaintenanceTab.tsx +12 -598
- package/extensions/dashboard-client/src/tabs/MemoryMapTab/MemoryMapView.tsx +476 -0
- package/extensions/dashboard-client/src/tabs/MemoryMapTab/RaptorTreeView.tsx +163 -0
- package/extensions/dashboard-client/src/tabs/MemoryMapTab.tsx +56 -466
- package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderHealthCard.tsx +109 -0
- package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderSetup.tsx +468 -0
- package/extensions/dashboard-client/src/tabs/SetupTab.tsx +13 -483
- package/extensions/dashboard-server/api-contracts/embedder-health.ts +32 -0
- package/extensions/dashboard-server/api-contracts/endpoints/registry.ts +360 -0
- package/extensions/dashboard-server/api-contracts/endpoints/types.ts +350 -0
- package/extensions/dashboard-server/api-contracts/endpoints.ts +27 -648
- package/extensions/dashboard-server/api-contracts/index.ts +6 -0
- package/extensions/dashboard-server/api-contracts/raptor.ts +49 -0
- package/extensions/dashboard-server/api-contracts.test/_helpers.ts +47 -0
- package/extensions/dashboard-server/routes-cache.test/_helpers.ts +94 -0
- package/extensions/dashboard-server/routes-embedder-health.ts +77 -0
- package/extensions/dashboard-server/routes-raptor.ts +128 -0
- package/extensions/dashboard-server/routes-setup.ts +1 -1
- package/extensions/dashboard-server/routes.ts +2 -0
- package/extensions/dashboard-server/server.ts +4 -0
- package/extensions/mega-cache-replay.test/_helpers.ts +188 -0
- package/extensions/mega-commands/dataCommands.ts +169 -0
- package/extensions/mega-commands/helpers.ts +62 -0
- package/extensions/mega-commands/historyCommands.ts +117 -0
- package/extensions/mega-commands/setupCommand.ts +235 -0
- package/extensions/mega-commands.ts +25 -538
- package/extensions/mega-compact-s38.test/_helpers.ts +201 -0
- package/extensions/mega-compact.test/_helpers.ts +237 -0
- package/extensions/mega-events/agent-handlers/agentEndHandler.ts +238 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/cacheStripe.ts +74 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/contextHealth.ts +50 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/errorRetry.ts +421 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/event.ts +16 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/gameScoring.ts +73 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/lengthStop.ts +29 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/memoryReview.ts +42 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler/recordTurnRow.ts +61 -0
- package/extensions/mega-events/agent-handlers/turnEndHandler.ts +64 -0
- package/extensions/mega-events/agent-handlers.ts +15 -861
- package/extensions/mega-events/context-handler/afterCompact.ts +213 -0
- package/extensions/mega-events/context-handler/messageText.ts +20 -0
- package/extensions/mega-events/context-handler/tailResult.ts +63 -0
- package/extensions/mega-events/context-handler.ts +17 -235
- package/extensions/mega-events/separated-prompt.test/_helpers.ts +35 -0
- package/package.json +1 -1
- package/src/config.ts +4 -0
- package/src/dedup-engine.test/_helpers.ts +79 -0
- package/src/e2e.test/_helpers.ts +59 -0
- package/src/embedder.ts +4 -0
- package/src/httpEmbedder.ts +7 -0
- package/src/hyde.ts +162 -0
- package/src/importance.test/_helpers.ts +5 -0
- package/src/memoryGraph/sources/checkpoints.ts +99 -0
- package/src/memoryGraph/sources/edges.ts +23 -0
- package/src/memoryGraph/sources/flags.ts +26 -0
- package/src/memoryGraph/sources/helpers.ts +13 -0
- package/src/memoryGraph/sources/memories.ts +108 -0
- package/src/memoryGraph/sources/raptor.ts +37 -0
- package/src/memoryGraph/sources/turns.ts +226 -0
- package/src/memoryGraph/sources.ts +19 -544
- package/src/memoryGraph-gates.test/_helpers.ts +164 -0
- package/src/ratio.bench.test/_helpers.ts +268 -0
- package/src/recall/async.ts +142 -0
- package/src/recall/format.ts +173 -0
- package/src/recall/memory.ts +90 -0
- package/src/recall/reformulate.ts +111 -0
- package/src/recall/sync.ts +196 -0
- package/src/recall/types.ts +63 -0
- package/src/recall.ts +25 -712
- package/src/store/sqlite/schema/core.ts +233 -0
- package/src/store/sqlite/schema/game.ts +66 -0
- package/src/store/sqlite/schema/plan-v2.ts +108 -0
- package/src/store/sqlite/schema/turns.ts +60 -0
- package/src/store/sqlite/schema.ts +13 -441
- package/src/store/turns/contract-compliance.test/_helpers.ts +38 -0
- package/src/store/turns/sqlite-store/admin.ts +249 -0
- package/src/store/turns/sqlite-store/ctx.ts +14 -0
- package/src/store/turns/sqlite-store/read.ts +174 -0
- package/src/store/turns/sqlite-store/rows.ts +55 -0
- package/src/store/turns/sqlite-store/write.ts +193 -0
- package/src/store/turns/sqlite-store.ts +33 -514
- package/src/vectorStore/class.ts +433 -0
- package/src/vectorStore/hash.ts +11 -0
- package/src/vectorStore/types.ts +58 -0
- package/src/vectorStore.test/_helpers.ts +20 -0
- package/src/vectorStore.ts +13 -489
- package/extensions/dashboard-client/dist/assets/MaintenanceTab-DZi1MnZI.js +0 -2
- package/extensions/dashboard-client/dist/assets/MaintenanceTab-DZi1MnZI.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-CjUa9zzw.js +0 -2
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-CjUa9zzw.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/SetupTab-C_BwmebC.js +0 -2
- package/extensions/dashboard-client/dist/assets/SetupTab-C_BwmebC.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-Cucgohga.js.map +0 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* routes-raptor.ts — GET /api/raptor-tree route handler (Part B).
|
|
3
|
+
*
|
|
4
|
+
* Returns the hierarchical RAPTOR tree (summary nodes by level) for a session.
|
|
5
|
+
* When no sessionId is given, resolves to the most recent session that has
|
|
6
|
+
* raptor nodes. Reads only from the local SQLite store (PREVENT-PI-004 OK).
|
|
7
|
+
* Uses an inline require to the pi-agnostic src/store/sqlite/raptor.ts reader.
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import { parse as parseUrl } from "node:url";
|
|
11
|
+
import { openStore } from "../../src/store/sqlite.js";
|
|
12
|
+
const _req = createRequire(import.meta.url);
|
|
13
|
+
/** Find the most recent sessionId that has raptor nodes (else null). */
|
|
14
|
+
function latestRaptorSession(stateDir) {
|
|
15
|
+
try {
|
|
16
|
+
const db = openStore(stateDir);
|
|
17
|
+
const row = db
|
|
18
|
+
.prepare("SELECT DISTINCT session_id FROM raptor_nodes ORDER BY built_at DESC LIMIT 1")
|
|
19
|
+
.get();
|
|
20
|
+
return row?.session_id ?? null;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// no raptor_nodes table yet (fresh DB) — treat as empty
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function toDTO(node) {
|
|
28
|
+
return {
|
|
29
|
+
id: node.id,
|
|
30
|
+
sessionId: node.sessionId,
|
|
31
|
+
level: node.level,
|
|
32
|
+
parentId: node.parentId,
|
|
33
|
+
children: node.children,
|
|
34
|
+
summary: node.summary,
|
|
35
|
+
qualityMarker: node.qualityMarker,
|
|
36
|
+
tokenEstimate: node.tokenEstimate,
|
|
37
|
+
builtAt: node.builtAt,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function emptyResponse() {
|
|
41
|
+
return { nodes: [], levels: 0, rootId: null, builtAt: null, empty: true };
|
|
42
|
+
}
|
|
43
|
+
export function handleRaptorTree(req, res, ctx) {
|
|
44
|
+
if (req.method !== "GET")
|
|
45
|
+
return false;
|
|
46
|
+
const parsed = parseUrl(req.url ?? "", true);
|
|
47
|
+
if (parsed.pathname !== "/api/raptor-tree")
|
|
48
|
+
return false;
|
|
49
|
+
try {
|
|
50
|
+
const { listRaptorNodes } = _req("../../src/store/sqlite/raptor.js");
|
|
51
|
+
let sessionId = typeof parsed.query.sessionId === "string" ? parsed.query.sessionId : "";
|
|
52
|
+
if (!sessionId) {
|
|
53
|
+
sessionId = latestRaptorSession(ctx.stateDir) ?? "";
|
|
54
|
+
if (!sessionId) {
|
|
55
|
+
const body = emptyResponse();
|
|
56
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
57
|
+
res.end(JSON.stringify(body));
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const nodes = listRaptorNodes(sessionId, ctx.stateDir);
|
|
62
|
+
if (nodes.length === 0) {
|
|
63
|
+
const body = emptyResponse();
|
|
64
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
65
|
+
res.end(JSON.stringify(body));
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
const dtos = nodes.map(toDTO);
|
|
69
|
+
const levels = dtos.reduce((m, n) => Math.max(m, n.level), 0);
|
|
70
|
+
const root = dtos.find((n) => n.parentId === null);
|
|
71
|
+
const builtAt = dtos.reduce((m, n) => Math.max(m, n.builtAt), 0);
|
|
72
|
+
const body = {
|
|
73
|
+
nodes: dtos,
|
|
74
|
+
levels,
|
|
75
|
+
rootId: root?.id ?? null,
|
|
76
|
+
builtAt,
|
|
77
|
+
empty: false,
|
|
78
|
+
};
|
|
79
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
80
|
+
res.end(JSON.stringify(body));
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
85
|
+
res.end(JSON.stringify({ error: String(e) }));
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -15,7 +15,7 @@ import { join } from "node:path";
|
|
|
15
15
|
// ---------------------------------------------------------------------------
|
|
16
16
|
// handleSetupStatus — "/api/setup-status"
|
|
17
17
|
// ---------------------------------------------------------------------------
|
|
18
|
-
function detectCurrentEmbedder() {
|
|
18
|
+
export function detectCurrentEmbedder() {
|
|
19
19
|
const url = process.env["MEGACOMPACT_EMBEDDING_URL"];
|
|
20
20
|
const minilm = process.env["MEGACOMPACT_MINILM"];
|
|
21
21
|
if (url && url.trim().length > 0)
|
|
@@ -15,5 +15,7 @@ export { handleProviderCache } from "./routes-cache.js";
|
|
|
15
15
|
export { handleMemoryStatus } from "./routes-memory.js";
|
|
16
16
|
export { handleSetupStatus, handleSetupDetect, handleSetupConfigure } from "./routes-setup.js";
|
|
17
17
|
export { handleMemoryMap } from "./routes-memory-map.js";
|
|
18
|
+
export { handleRaptorTree } from "./routes-raptor.js";
|
|
18
19
|
export { handleCacheStripes } from "./routes-cache.js";
|
|
19
20
|
export { handleContextHealth, handleCachePoison, handleHealthSettings } from "./routes-health.js";
|
|
21
|
+
export { handleEmbedderHealth } from "./routes-embedder-health.js";
|
|
@@ -12,7 +12,7 @@ import { join, dirname } from "node:path";
|
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
13
|
import { createRequire } from "node:module";
|
|
14
14
|
import { log, setLogPath, setDashboardServerVersion } from "./state.js";
|
|
15
|
-
import { buildRouteContext, handleIndex, handleRepoIndex, handleEvents, handleGameState, handleGameScores, handlePerf, handleAchievements, handleSessions, handleTopics, handleTurns, handleMaintenance, handleProviderCache, handleCacheStripes, handleMemoryStatus, handleSetupStatus, handleSetupDetect, handleSetupConfigure, handleMemoryMap, handleContextHealth, handleCachePoison, handleHealthSettings, handleStatic, } from "./routes.js";
|
|
15
|
+
import { buildRouteContext, handleIndex, handleRepoIndex, handleEvents, handleGameState, handleGameScores, handlePerf, handleAchievements, handleSessions, handleTopics, handleTurns, handleMaintenance, handleProviderCache, handleCacheStripes, handleMemoryStatus, handleSetupStatus, handleSetupDetect, handleSetupConfigure, handleMemoryMap, handleRaptorTree, handleContextHealth, handleCachePoison, handleHealthSettings, handleEmbedderHealth, handleStatic, } from "./routes.js";
|
|
16
16
|
export async function launchDashboardServer(stateDir) {
|
|
17
17
|
// Our own package version — exposed at /api/version so the launcher can
|
|
18
18
|
// detect a stale server (started by an older build) and replace it on
|
|
@@ -207,12 +207,16 @@ export async function launchDashboardServer(stateDir) {
|
|
|
207
207
|
return;
|
|
208
208
|
if (handleMemoryMap(req, res, ctx))
|
|
209
209
|
return;
|
|
210
|
+
if (handleRaptorTree(req, res, ctx))
|
|
211
|
+
return;
|
|
210
212
|
if (handleContextHealth(req, res, ctx))
|
|
211
213
|
return;
|
|
212
214
|
if (handleCachePoison(req, res, ctx))
|
|
213
215
|
return;
|
|
214
216
|
if (handleHealthSettings(req, res, ctx))
|
|
215
217
|
return;
|
|
218
|
+
if (handleEmbedderHealth(req, res, ctx))
|
|
219
|
+
return;
|
|
216
220
|
handleStatic(req, res, ctx);
|
|
217
221
|
});
|
|
218
222
|
// Bind base + range are env-configurable so tests can use a private,
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared helpers for the mega-cache-replay.test split files.
|
|
3
|
+
* Extracted from extensions/mega-cache-replay.test.ts: baseTmp, env, harness().
|
|
4
|
+
*/
|
|
5
|
+
import { mkdtempSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
export const baseTmp = mkdtempSync(join(tmpdir(), "mc-cache-"));
|
|
10
|
+
let counter = 0;
|
|
11
|
+
export function setupEnv() {
|
|
12
|
+
process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "index");
|
|
13
|
+
process.env.MEGACOMPACT_PGLITE_DISABLED = "true";
|
|
14
|
+
}
|
|
15
|
+
export function harness() {
|
|
16
|
+
const stateDir = join(baseTmp, `run-${counter++}`);
|
|
17
|
+
process.env.MEGACOMPACT_STATE_DIR = stateDir;
|
|
18
|
+
process.env.MEGACOMPACT_DEBUG = "true";
|
|
19
|
+
process.env.MEGACOMPACT_THRESHOLD_TOKENS = "50";
|
|
20
|
+
process.env.MEGACOMPACT_FAST_GATE_PCT = "1";
|
|
21
|
+
process.env.MEGACOMPACT_ANCHOR_USER_MESSAGES = "1";
|
|
22
|
+
process.env.MEGACOMPACT_DURABLE_TRIM_FLOOR = "0";
|
|
23
|
+
process.env.MEGACOMPACT_MEMORY_AUTO_REVIEW = "false";
|
|
24
|
+
process.env.MEGACOMPACT_RAPTOR_ENABLED = "false";
|
|
25
|
+
process.env.MEGACOMPACT_L1_ENABLED = "false";
|
|
26
|
+
process.env.MEGACOMPACT_L2_ENABLED = "false";
|
|
27
|
+
delete process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM;
|
|
28
|
+
const usage = {
|
|
29
|
+
tokens: 200000,
|
|
30
|
+
contextWindow: 200000,
|
|
31
|
+
percent: 100,
|
|
32
|
+
};
|
|
33
|
+
const handlers = {};
|
|
34
|
+
const compactCalls = [];
|
|
35
|
+
function msg(role, text, toolName) {
|
|
36
|
+
if (role === "assistant" && toolName) {
|
|
37
|
+
return {
|
|
38
|
+
role: "assistant",
|
|
39
|
+
content: [
|
|
40
|
+
{ type: "toolCall", name: toolName, id: "c1", arguments: {} },
|
|
41
|
+
],
|
|
42
|
+
api: "anthropic-messages",
|
|
43
|
+
provider: "anthropic",
|
|
44
|
+
model: "m",
|
|
45
|
+
usage: {
|
|
46
|
+
inputTokens: 1,
|
|
47
|
+
outputTokens: 1,
|
|
48
|
+
cacheReadTokens: 0,
|
|
49
|
+
cacheWriteTokens: 0,
|
|
50
|
+
},
|
|
51
|
+
stopReason: "tool_use",
|
|
52
|
+
timestamp: 0,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (role === "toolResult" && toolName) {
|
|
56
|
+
return {
|
|
57
|
+
role: "toolResult",
|
|
58
|
+
content: [{ type: "text", text }],
|
|
59
|
+
toolCallId: "c1",
|
|
60
|
+
toolName,
|
|
61
|
+
isError: false,
|
|
62
|
+
timestamp: 0,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
role: "user",
|
|
67
|
+
content: text,
|
|
68
|
+
timestamp: 0,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function buildSession(tag, n) {
|
|
72
|
+
const s = [];
|
|
73
|
+
for (let i = 0; i < n; i++) {
|
|
74
|
+
s.push(msg("user", `[${tag}] we decided to use approach ${i} for module ${i}`));
|
|
75
|
+
s.push(msg("assistant", `[${tag}] edited module ${i}`, "Edit"));
|
|
76
|
+
s.push(msg("toolResult", `[${tag}] edited module ${i}`, "Edit"));
|
|
77
|
+
}
|
|
78
|
+
return s;
|
|
79
|
+
}
|
|
80
|
+
const toEntry = (m, i) => ({
|
|
81
|
+
type: "message",
|
|
82
|
+
id: `e${i}`,
|
|
83
|
+
parentId: null,
|
|
84
|
+
timestamp: String(i),
|
|
85
|
+
message: m,
|
|
86
|
+
});
|
|
87
|
+
const sessionManager = {
|
|
88
|
+
getSessionId: () => "sess_cache_001",
|
|
89
|
+
getEntries: () => buildSession("A", 14).map(toEntry),
|
|
90
|
+
getBranch: () => buildSession("A", 14).map(toEntry),
|
|
91
|
+
};
|
|
92
|
+
function makeCtx(over = {}) {
|
|
93
|
+
return {
|
|
94
|
+
ui: {
|
|
95
|
+
setStatus: () => { },
|
|
96
|
+
notify: () => { },
|
|
97
|
+
select: () => { },
|
|
98
|
+
confirm: async () => true,
|
|
99
|
+
input: async () => "",
|
|
100
|
+
setWidget: () => { },
|
|
101
|
+
},
|
|
102
|
+
mode: "tui",
|
|
103
|
+
hasUI: true,
|
|
104
|
+
cwd: stateDir,
|
|
105
|
+
sessionManager,
|
|
106
|
+
modelRegistry: {},
|
|
107
|
+
model: undefined,
|
|
108
|
+
isIdle: () => true,
|
|
109
|
+
isProjectTrusted: () => true,
|
|
110
|
+
signal: undefined,
|
|
111
|
+
abort: () => { },
|
|
112
|
+
hasPendingMessages: () => false,
|
|
113
|
+
shutdown: () => { },
|
|
114
|
+
getContextUsage: () => ({ ...usage }),
|
|
115
|
+
compact: (opts) => {
|
|
116
|
+
compactCalls.push(opts);
|
|
117
|
+
return undefined;
|
|
118
|
+
},
|
|
119
|
+
getSystemPrompt: () => "system base",
|
|
120
|
+
...over,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const pi = {
|
|
124
|
+
on: (ev, h) => {
|
|
125
|
+
if (!handlers[ev])
|
|
126
|
+
handlers[ev] = [];
|
|
127
|
+
handlers[ev].push(h);
|
|
128
|
+
},
|
|
129
|
+
registerCommand: () => { },
|
|
130
|
+
registerTool: () => { },
|
|
131
|
+
registerShortcut: () => { },
|
|
132
|
+
registerFlag: () => { },
|
|
133
|
+
getFlag: () => undefined,
|
|
134
|
+
registerMessageRenderer: () => { },
|
|
135
|
+
registerEntryRenderer: () => { },
|
|
136
|
+
sendMessage: () => { },
|
|
137
|
+
sendUserMessage: () => { },
|
|
138
|
+
appendEntry: () => { },
|
|
139
|
+
setSessionName: () => { },
|
|
140
|
+
getSessionName: () => undefined,
|
|
141
|
+
setLabel: () => { },
|
|
142
|
+
exec: async () => ({ stdout: "", stderr: "", code: 0 }),
|
|
143
|
+
getActiveTools: () => [],
|
|
144
|
+
getAllTools: () => [],
|
|
145
|
+
setActiveTools: () => { },
|
|
146
|
+
getCommands: () => [],
|
|
147
|
+
setModel: async () => false,
|
|
148
|
+
getThinkingLevel: () => "off",
|
|
149
|
+
setThinkingLevel: () => { },
|
|
150
|
+
};
|
|
151
|
+
const require = createRequire(import.meta.url);
|
|
152
|
+
const mod = require("../mega-compact.js");
|
|
153
|
+
mod.default(pi);
|
|
154
|
+
const { lastRuntime } = require("../mega-events.js");
|
|
155
|
+
const fire = async (ev, event, ctx) => {
|
|
156
|
+
let r;
|
|
157
|
+
for (const h of handlers[ev] || [])
|
|
158
|
+
r = await h(event, ctx);
|
|
159
|
+
return r;
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
stateDir,
|
|
163
|
+
handlers,
|
|
164
|
+
compactCalls,
|
|
165
|
+
fire,
|
|
166
|
+
ctx: makeCtx,
|
|
167
|
+
usage,
|
|
168
|
+
buildSession,
|
|
169
|
+
runtime: lastRuntime,
|
|
170
|
+
clearDebounce: () => {
|
|
171
|
+
if (lastRuntime)
|
|
172
|
+
lastRuntime.debounceUntil = 0;
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { sessionEntryToContextMessages } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { normalizeSessionId } from "../../src/store.js";
|
|
3
|
+
import { latestModelSnapshot, countInjectedGlobal, listRepoRegistry } from "../../src/store/sqlite.js";
|
|
4
|
+
import { loadMetrics, fpRate, p95, defaultMetricsPath } from "../../src/monitoring.js";
|
|
5
|
+
import { C, recentUserQuery } from "../mega-runtime.js";
|
|
6
|
+
import { runCompact, doRecall, doRecallAsync } from "../mega-pipeline.js";
|
|
7
|
+
import { vectorStats, vectorRepoStats, vectorDataInvariant } from "../../src/vectorStore.js";
|
|
8
|
+
/** Register the data/inspection commands (data group). */
|
|
9
|
+
export function registerDataCommands(pi, runtime, config) {
|
|
10
|
+
pi.registerCommand("mega-compact", {
|
|
11
|
+
description: "Compress current session context into the local vector store.",
|
|
12
|
+
handler: async (args, ctx) => {
|
|
13
|
+
try {
|
|
14
|
+
const sessionEntries = ctx.sessionManager.getEntries();
|
|
15
|
+
// Project entries (branch-aware) into the message view.
|
|
16
|
+
const messages = sessionEntries.flatMap((e) => sessionEntryToContextMessages(e));
|
|
17
|
+
const summaryArg = args.trim();
|
|
18
|
+
const ran = runCompact(pi, runtime, config, ctx, messages, summaryArg ? { summary: summaryArg } : {});
|
|
19
|
+
if ("skipped" in ran && ran.skipped) {
|
|
20
|
+
ctx.ui.notify("[mega-compact] Nothing to compact (session too small).");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const r = ran.result;
|
|
24
|
+
ctx.ui.notify(`[mega-compact] ${r.deduped ? "region already compacted (deduped)" : `persisted ${r.checkpointId}`} · ` +
|
|
25
|
+
`${r.tokenEstimate} tok · ${runtime.currentStateDir}`);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
ctx.ui.notify(`[mega-compact] /mega-compact failed: ${String(e)}`);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
pi.registerCommand("mega-recall", {
|
|
33
|
+
description: "Recall relevant compacted context from the vector store and inline it. Use --cross-repo to search all repos.",
|
|
34
|
+
handler: async (args, ctx) => {
|
|
35
|
+
try {
|
|
36
|
+
// S17: --cross-repo (or --cross repo) runs the async path over every repo's
|
|
37
|
+
// PGlite HNSW index (stricter cosine floor + source labels).
|
|
38
|
+
const crossRepo = /--cross[- ]repo\b/.test(args);
|
|
39
|
+
const query = args.replace(/--cross[- ]repo\b/, "").trim() || recentUserQuery(ctx);
|
|
40
|
+
if (!query) {
|
|
41
|
+
ctx.ui.notify("[mega-compact] /mega-recall needs a query or a prior user message.");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const r = crossRepo
|
|
45
|
+
? await doRecallAsync(runtime, config, ctx, query, "command", { crossRepo: true })
|
|
46
|
+
: doRecall(runtime, config, ctx, query, "command");
|
|
47
|
+
if (r.empty) {
|
|
48
|
+
runtime.logger.info("recall-empty", { query, crossRepo });
|
|
49
|
+
ctx.ui.notify(`[mega-compact] recall found nothing new for "${query}".`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// Stage the block so the next before_agent_start prepends it (actual
|
|
53
|
+
// injection). Report what was selected now for immediate feedback.
|
|
54
|
+
runtime.pendingRecallBlock = r.block;
|
|
55
|
+
const list = r.report.map((l) => l).join("\n");
|
|
56
|
+
runtime.logger.info("recall", { query, crossRepo, injected: r.toInject.map((h) => h.checkpoint.checkpointId) });
|
|
57
|
+
runtime.setStatus(ctx, `mega-compact: recalled ${r.toInject.length} chkpt${crossRepo ? " (cross-repo)" : ""}`);
|
|
58
|
+
ctx.ui.notify(`[mega-compact] recall staged ${r.toInject.length} checkpoint(s) for "${query}"${crossRepo ? " (cross-repo)" : ""}:\n${list}\n` +
|
|
59
|
+
`(injected at the tail of the next turn's context)`);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
ctx.ui.notify(`[mega-compact] /mega-recall failed: ${String(e)}`);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
pi.registerCommand("mega-status", {
|
|
67
|
+
description: "Show mega-compact config, context usage, and the data-safety invariant.",
|
|
68
|
+
handler: async (_args, ctx) => {
|
|
69
|
+
try {
|
|
70
|
+
runtime.bindRepo(ctx.cwd);
|
|
71
|
+
const usage = ctx.getContextUsage();
|
|
72
|
+
const pct = usage?.percent != null ? `${usage.percent}%` : "n/a";
|
|
73
|
+
const tokens = usage?.tokens != null ? `${usage.tokens} tok` : "n/a";
|
|
74
|
+
const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
|
|
75
|
+
const st = vectorStats(runtime.store, sid);
|
|
76
|
+
const repo = vectorRepoStats(runtime.store);
|
|
77
|
+
const di = vectorDataInvariant(runtime.store);
|
|
78
|
+
const fmtB = (b) => b >= 1_048_576 ? `${(b / 1_048_576).toFixed(1)} MiB` :
|
|
79
|
+
b >= 1024 ? `${(b / 1024).toFixed(1)} KiB` : `${b} B`;
|
|
80
|
+
// Real cost: tokens saved × the captured model's input rate (USD/token),
|
|
81
|
+
// read from the model_snapshots table (Phase 5b schema). Falls back to 0
|
|
82
|
+
// when no model has been captured yet. contextWindow ÷ savedRate = context
|
|
83
|
+
// windows extended (how much "extra" conversation the freed space buys).
|
|
84
|
+
const model = latestModelSnapshot(runtime.currentStateDir);
|
|
85
|
+
const rate = model?.inputRate ?? 0;
|
|
86
|
+
const usd = ((repo.tokensSaved ?? 0) * rate).toFixed(4);
|
|
87
|
+
const ctxWindow = usage?.contextWindow ?? 0;
|
|
88
|
+
const daysExtended = ctxWindow > 0 && repo.tokensSaved > 0
|
|
89
|
+
? (repo.tokensSaved / ctxWindow).toFixed(1)
|
|
90
|
+
: "0";
|
|
91
|
+
// Identified model/provider (captured on model_select / session_start).
|
|
92
|
+
// Shows the human model name + provider so the user knows WHICH model's
|
|
93
|
+
// pricing drives the cost figure. Falls back when none captured yet.
|
|
94
|
+
const modelStr = model
|
|
95
|
+
? `${model.modelName ?? model.modelId ?? "?"} · ${model.providerName ?? model.provider ?? "?"}`
|
|
96
|
+
: "unknown (no model captured)";
|
|
97
|
+
const costStr = `≈ $${usd} saved · ${daysExtended} context-windows extended`;
|
|
98
|
+
// Recall-quality badge (Phase 4): trust score from monitoring metrics.
|
|
99
|
+
// H1 fix: loadMetrics expects a *file* path (dashboard.json), not the
|
|
100
|
+
// state dir — passing the dir made existsSync() true (dirs exist) then
|
|
101
|
+
// readFileSync() threw EISDIR, silently caught → metrics always zero.
|
|
102
|
+
const m = loadMetrics(defaultMetricsPath(runtime.currentStateDir));
|
|
103
|
+
const fp = fpRate(m, "L2");
|
|
104
|
+
const p95L2 = p95(m.latency.L2 ?? []);
|
|
105
|
+
const relPct = (st.dedupHitRate * 100).toFixed(0);
|
|
106
|
+
const qualityStr = `recall ${relPct}% relevant · FP ${(fp * 100).toFixed(1)}% · L2 p95 ${p95L2.toFixed(0)}ms`;
|
|
107
|
+
// S18: cross-repo stats from the machine-wide index (best-effort; the
|
|
108
|
+
// index dir may be unset → 0/empty, never throws).
|
|
109
|
+
let crossRepoInjections = 0;
|
|
110
|
+
let repoCount = 0;
|
|
111
|
+
try {
|
|
112
|
+
crossRepoInjections = countInjectedGlobal(process.env.MEGACOMPACT_INDEX_DIR);
|
|
113
|
+
repoCount = listRepoRegistry(process.env.MEGACOMPACT_INDEX_DIR).length;
|
|
114
|
+
}
|
|
115
|
+
catch { /* non-fatal */ }
|
|
116
|
+
const crossRepoStr = `${crossRepoInjections} cross-repo injections recorded · ${repoCount} repos indexed`;
|
|
117
|
+
// Effective compaction threshold = tierPct × model context window (kept
|
|
118
|
+
// BELOW pi's native ~80% auto-compact for any model size). Falls back to
|
|
119
|
+
// the boot token value when the window is unknown (custom tier / pre-
|
|
120
|
+
// model-select). Display matches the dashboard's percentage-based view.
|
|
121
|
+
const effThreshold = config.tierPct != null && ctxWindow > 0
|
|
122
|
+
? Math.round(config.tierPct * ctxWindow)
|
|
123
|
+
: config.thresholdTokens;
|
|
124
|
+
const winStr = ctxWindow > 0
|
|
125
|
+
? (ctxWindow >= 1_000_000 ? `${Math.round(ctxWindow / 1_000_000)}M` : `${Math.round(ctxWindow / 1_000)}k`)
|
|
126
|
+
: "?";
|
|
127
|
+
const tierPctStr = config.tierPct != null ? `${Math.round(config.tierPct * 100)}%` : "n/a";
|
|
128
|
+
ctx.ui.notify(`[mega-compact] pct=${pct} tokens=${tokens} tier=${runtime.pressureBand} (live) preset=${config.tier} ` +
|
|
129
|
+
`pressure=${Math.round(runtime.pressure * 100)}% fastGate=${config.fastGatePct}% ` +
|
|
130
|
+
`threshold=${effThreshold.toLocaleString()} (${tierPctStr} of ${winStr} window) tierPct=${config.tierPct != null ? config.tierPct.toFixed(2) : "n/a"} auto=${config.auto} autoInline=${config.autoInline}\n` +
|
|
131
|
+
`[mega-compact] store: ${st.checkpointCount} chkpt · ` +
|
|
132
|
+
`${st.totalTokenEstimate} tok · last=${st.lastCheckpointId ?? "—"} · ` +
|
|
133
|
+
`injected=${st.injectedCount} · dedup=${(st.dedupHitRate * 100).toFixed(0)}%\n` +
|
|
134
|
+
`[mega-compact] anchor=${config.anchorUserMessages} preserveRecent=${config.preserveRecent} ` +
|
|
135
|
+
`autoInlineK=${config.autoInlineK} dedupSim=${config.dedupSim} debug=${config.debug}\n` +
|
|
136
|
+
`[mega-compact] 🛡 data-safe: ${di.regionsRetained} regions retained ` +
|
|
137
|
+
`(${fmtB(di.compressedOriginalBytes)} compressed-original) · ` +
|
|
138
|
+
`${di.duplicatesCollapsed} dedup-duplicates collapsed · ` +
|
|
139
|
+
`${C.green}0 bytes permanently deleted${C.reset}\n` +
|
|
140
|
+
`[mega-compact] 💰 ${costStr}\n` +
|
|
141
|
+
`[mega-compact] 🤖 model: ${modelStr}\n` +
|
|
142
|
+
`[mega-compact] 🎯 ${qualityStr}\n` +
|
|
143
|
+
`[mega-compact] 🌐 ${crossRepoStr}\n` +
|
|
144
|
+
`[mega-compact] stateDir=${runtime.currentStateDir}`);
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
ctx.ui.notify(`[mega-compact] /mega-status error: ${String(e)}`);
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { listCheckpoints } from "../../src/store/sqlite.js";
|
|
2
|
+
import { defaultEventsPath } from "../../src/monitoring.js";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
/** Resolve a checkpoint by id (or "recent"/"last") from this session's store. */
|
|
6
|
+
export function findCheckpoint(runtime, sid, ref) {
|
|
7
|
+
const all = listCheckpoints(sid, runtime.currentStateDir);
|
|
8
|
+
if (all.length === 0)
|
|
9
|
+
return undefined;
|
|
10
|
+
if (!ref || ref === "recent" || ref === "last")
|
|
11
|
+
return all[all.length - 1];
|
|
12
|
+
return all.find((c) => c.checkpointId === ref) ?? all.find((c) => c.checkpointId.endsWith(ref));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Try to peek at recent events.log and mega-compact.log for low recall quality signals.
|
|
16
|
+
* Best-effort, non-fatal — returns true if a low-quality signal was found.
|
|
17
|
+
*/
|
|
18
|
+
export function checkRecallQuality(stateDir) {
|
|
19
|
+
const eventPaths = [
|
|
20
|
+
defaultEventsPath(stateDir),
|
|
21
|
+
path.join(stateDir, "mega-compact.log"),
|
|
22
|
+
];
|
|
23
|
+
for (const fp of eventPaths) {
|
|
24
|
+
try {
|
|
25
|
+
if (!existsSync(fp))
|
|
26
|
+
continue;
|
|
27
|
+
const buf = readFileSync(fp, { encoding: "utf-8" });
|
|
28
|
+
// Only scan last 64 KiB of the file
|
|
29
|
+
const tail = buf.length > 65536 ? buf.slice(buf.length - 65536) : buf;
|
|
30
|
+
const lines = tail.split("\n").filter(Boolean);
|
|
31
|
+
for (const line of lines) {
|
|
32
|
+
try {
|
|
33
|
+
// guardrails-allow PREVENT-001: JSON.parse with null check
|
|
34
|
+
const parsed = JSON.parse(line);
|
|
35
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
36
|
+
continue;
|
|
37
|
+
// Check for recall_metrics_low_quality event (latest format)
|
|
38
|
+
if (parsed.event === "recall_metrics_low_quality" && parsed.score !== undefined) {
|
|
39
|
+
if (typeof parsed.score === "number" && parsed.score < 0.4)
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
// Check for recall_metrics with low relevanceScore
|
|
43
|
+
if (parsed.event === "recall_metrics" && parsed.relevanceScore !== undefined) {
|
|
44
|
+
if (typeof parsed.relevanceScore === "number" && parsed.relevanceScore < 0.4)
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
// Check for RecallQualityEvent format from monitoring.ts
|
|
48
|
+
if (parsed.score !== undefined && typeof parsed.score === "number" && parsed.score < 0.4)
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// skip unparseable lines
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// non-fatal: skip if can't read
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { normalizeSessionId } from "../../src/store.js";
|
|
2
|
+
import { listCheckpoints } from "../../src/store/sqlite.js";
|
|
3
|
+
import { decompressSmart } from "../../src/store/compression.js";
|
|
4
|
+
import { C } from "../mega-runtime.js";
|
|
5
|
+
import { findCheckpoint } from "./helpers.js";
|
|
6
|
+
/** Register the checkpoint history/inspection commands. */
|
|
7
|
+
export function registerHistoryCommands(pi, runtime) {
|
|
8
|
+
pi.registerCommand("mega-restore", {
|
|
9
|
+
description: "Re-inject a checkpoint's verbatim original region into context. Usage: /mega-restore <chkpt|recent>",
|
|
10
|
+
handler: async (args, ctx) => {
|
|
11
|
+
try {
|
|
12
|
+
runtime.bindRepo(ctx.cwd);
|
|
13
|
+
const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
|
|
14
|
+
const cp = findCheckpoint(runtime, sid, args.trim());
|
|
15
|
+
if (!cp) {
|
|
16
|
+
ctx.ui.notify(`[mega-compact] no checkpoint found${args.trim() ? ` for "${args.trim()}"` : ""} in this session. Try /mega-history.`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!cp.compressedOriginal) {
|
|
20
|
+
ctx.ui.notify(`[mega-compact] ${cp.checkpointId} has no recoverable original (pre-blob or direct add). Cannot restore verbatim.`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const original = decompressSmart(cp.compressedOriginal).toString("utf-8");
|
|
24
|
+
// Re-inject verbatim via before_agent_start (PREVENT-PI-003) — never
|
|
25
|
+
// touches live messages, only prepends the restored region to systemPrompt.
|
|
26
|
+
runtime.pendingRecallBlock = `The following compacted context was RESTORED from checkpoint ${cp.checkpointId} (verbatim original region):\n\n${original}`;
|
|
27
|
+
const files = cp.filesModified?.length ? cp.filesModified.join(", ") : "(no files captured)";
|
|
28
|
+
ctx.ui.notify(`[mega-compact] ♻ restored ${cp.checkpointId} — ${original.length} chars re-injected on next turn.\n` +
|
|
29
|
+
`[mega-compact] files: ${files}`);
|
|
30
|
+
runtime.dashboard.event("restore", { checkpointId: cp.checkpointId, chars: original.length });
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
ctx.ui.notify(`[mega-compact] /mega-restore failed (checkpoint may be corrupt): ${String(e)}`);
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
pi.registerCommand("mega-history", {
|
|
38
|
+
description: "List this session's checkpoints (id, date, files, tokens). Usage: /mega-history",
|
|
39
|
+
handler: async (_args, ctx) => {
|
|
40
|
+
runtime.bindRepo(ctx.cwd);
|
|
41
|
+
const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
|
|
42
|
+
const all = listCheckpoints(sid, runtime.currentStateDir);
|
|
43
|
+
if (all.length === 0) {
|
|
44
|
+
ctx.ui.notify("[mega-compact] no checkpoints in this session yet.");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const rows = all.map((c) => {
|
|
48
|
+
const when = c.timestamp ? new Date(c.timestamp).toISOString().slice(0, 16).replace("T", " ") : "—";
|
|
49
|
+
const files = c.filesModified?.length ? c.filesModified.map((f) => f.split("/").pop() ?? f).join(", ") : "—";
|
|
50
|
+
const orig = c.originalTokenEstimate ?? 0;
|
|
51
|
+
const stored = c.tokenEstimate ?? 0;
|
|
52
|
+
const saved = Math.max(0, orig - stored);
|
|
53
|
+
return ` ${c.checkpointId} ${when} ${C.cyan}${saved}t saved${C.reset} ${files}`;
|
|
54
|
+
});
|
|
55
|
+
ctx.ui.notify(`[mega-compact] ${all.length} checkpoint(s) in this session:\n` + rows.join("\n") +
|
|
56
|
+
`\n[mega-compact] /mega-view <chkpt> to see the original region · /mega-restore <chkpt> to re-inject it`);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
pi.registerCommand("mega-view", {
|
|
60
|
+
description: "Show a checkpoint's verbatim original region. Usage: /mega-view <chkpt|recent>",
|
|
61
|
+
handler: async (args, ctx) => {
|
|
62
|
+
try {
|
|
63
|
+
runtime.bindRepo(ctx.cwd);
|
|
64
|
+
const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
|
|
65
|
+
const cp = findCheckpoint(runtime, sid, args.trim());
|
|
66
|
+
if (!cp) {
|
|
67
|
+
ctx.ui.notify(`[mega-compact] no checkpoint found${args.trim() ? ` for "${args.trim()}"` : ""}. Try /mega-history.`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!cp.compressedOriginal) {
|
|
71
|
+
ctx.ui.notify(`[mega-compact] ${cp.checkpointId} summary:\n${cp.summary.slice(0, 500)}${cp.summary.length > 500 ? "…" : ""}\n(no verbatim original stored)`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const original = decompressSmart(cp.compressedOriginal).toString("utf-8");
|
|
75
|
+
ctx.ui.notify(`[mega-compact] ${cp.checkpointId} — original region (${original.length} chars):\n` +
|
|
76
|
+
`${original.slice(0, 1500)}${original.length > 1500 ? "\n…(truncated)" : ""}`);
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
ctx.ui.notify(`[mega-compact] /mega-view failed (checkpoint may be corrupt): ${String(e)}`);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
pi.registerCommand("mega-help", {
|
|
84
|
+
description: "Plain-language glossary of what mega-compact's stats mean.",
|
|
85
|
+
handler: async (_args, ctx) => {
|
|
86
|
+
ctx.ui.notify(`[mega-compact] glossary — what the numbers mean:\n` +
|
|
87
|
+
`• token — a chunk of text (~4 chars). Context window = how much text fits in memory at once.\n` +
|
|
88
|
+
`• space freed — how much conversation we've compressed away to make room (the win).\n` +
|
|
89
|
+
`• memory held — how much compact summary we're currently keeping as your 'notes'.\n` +
|
|
90
|
+
`• saved checkpoint — a compact summary of an old conversation chunk we stored.\n` +
|
|
91
|
+
`• repeat-skipped — how often new text matched something we already had, so we didn't store a duplicate.\n` +
|
|
92
|
+
`• injected — times we pasted an old saved note back into the chat because it was relevant.\n` +
|
|
93
|
+
`• recall relevance — of those, how often the note was actually on-topic.\n` +
|
|
94
|
+
`• data safety — every compressed region is kept verbatim; nothing is permanently deleted. /mega-restore brings any of it back.`);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|