pi-mega-compact 0.7.8 → 0.8.0

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.
Files changed (122) hide show
  1. package/README.md +11 -12
  2. package/dist/extensions/dashboard-server/html.js +1023 -0
  3. package/dist/extensions/dashboard-server/html.test.js +41 -0
  4. package/dist/extensions/dashboard-server/index-reader.js +133 -0
  5. package/dist/extensions/dashboard-server/server.js +530 -0
  6. package/dist/extensions/dashboard-server/server.test.js +120 -0
  7. package/dist/extensions/dashboard-server/snapshot.js +43 -0
  8. package/dist/extensions/dashboard-server/state.js +30 -0
  9. package/dist/extensions/dashboard-server/types.js +5 -0
  10. package/dist/extensions/dashboard-server-s32.test.js +181 -0
  11. package/dist/extensions/dashboard-server.js +7 -1315
  12. package/dist/extensions/mega-commands.js +162 -134
  13. package/dist/extensions/mega-compact.js +3 -0
  14. package/dist/extensions/mega-compact.test.js +90 -21
  15. package/dist/extensions/mega-conflict-cmds.js +5 -1
  16. package/dist/extensions/mega-dashboard-cmds.js +29 -22
  17. package/dist/extensions/mega-db-cmds.js +11 -2
  18. package/dist/extensions/mega-events/agent-handlers.js +222 -0
  19. package/dist/extensions/mega-events/compact-handlers.js +162 -0
  20. package/dist/extensions/mega-events/context-handler.js +249 -0
  21. package/dist/extensions/mega-events/register.js +21 -0
  22. package/dist/extensions/mega-events/session-handlers.js +142 -0
  23. package/dist/extensions/mega-events.js +15 -699
  24. package/dist/extensions/mega-game-cmds.js +106 -0
  25. package/dist/extensions/mega-game-cmds.test.js +113 -0
  26. package/dist/extensions/mega-pipeline/compact.js +324 -0
  27. package/dist/extensions/mega-pipeline/memory-review.js +38 -0
  28. package/dist/extensions/mega-pipeline/recall.js +147 -0
  29. package/dist/extensions/mega-pipeline.js +9 -480
  30. package/dist/extensions/mega-runtime/helpers.js +40 -0
  31. package/dist/extensions/mega-runtime/query.js +29 -0
  32. package/dist/extensions/mega-runtime/state.js +877 -0
  33. package/dist/extensions/mega-runtime/state.test.js +171 -0
  34. package/dist/extensions/mega-runtime/widget.js +270 -0
  35. package/dist/extensions/mega-runtime/widget.test.js +160 -0
  36. package/dist/extensions/mega-runtime.js +15 -947
  37. package/dist/src/config/themes.js +84 -0
  38. package/dist/src/config/themes.test.js +94 -0
  39. package/dist/src/game/scoring.js +105 -0
  40. package/dist/src/game/scoring.test.js +98 -0
  41. package/dist/src/store/sqlite/checkpoints.js +145 -0
  42. package/dist/src/store/sqlite/dedup-mirror.js +64 -0
  43. package/dist/src/store/sqlite/foundation.js +38 -0
  44. package/dist/src/store/sqlite/game-achievements.js +111 -0
  45. package/dist/src/store/sqlite/game-achievements.test.js +67 -0
  46. package/dist/src/store/sqlite/game-scores.js +105 -0
  47. package/dist/src/store/sqlite/game-scores.test.js +106 -0
  48. package/dist/src/store/sqlite/game-state.js +54 -0
  49. package/dist/src/store/sqlite/game-state.test.js +76 -0
  50. package/dist/src/store/sqlite/global-index.js +224 -0
  51. package/dist/src/store/sqlite/maintenance.js +235 -0
  52. package/dist/src/store/sqlite/memories.js +164 -0
  53. package/dist/src/store/sqlite/meta.js +82 -0
  54. package/dist/src/store/sqlite/model-snapshots.js +47 -0
  55. package/dist/src/store/sqlite/raptor.js +57 -0
  56. package/dist/src/store/sqlite/raw-transcript.js +134 -0
  57. package/dist/src/store/sqlite/schema.js +294 -0
  58. package/dist/src/store/sqlite/session-state.js +28 -0
  59. package/dist/src/store/sqlite/stats.js +66 -0
  60. package/dist/src/store/sqlite/utils.js +120 -0
  61. package/dist/src/store/sqlite.js +23 -1607
  62. package/extensions/dashboard-server/html.test.ts +50 -0
  63. package/extensions/dashboard-server/html.ts +1026 -0
  64. package/extensions/dashboard-server/index-reader.ts +130 -0
  65. package/extensions/dashboard-server/server.test.ts +131 -0
  66. package/extensions/dashboard-server/server.ts +505 -0
  67. package/extensions/dashboard-server/snapshot.ts +44 -0
  68. package/extensions/dashboard-server/state.ts +33 -0
  69. package/extensions/dashboard-server/types.ts +134 -0
  70. package/extensions/dashboard-server-s32.test.ts +195 -0
  71. package/extensions/dashboard-server.ts +7 -1431
  72. package/extensions/mega-commands.ts +33 -10
  73. package/extensions/mega-compact.test.ts +198 -43
  74. package/extensions/mega-compact.ts +3 -0
  75. package/extensions/mega-conflict-cmds.ts +6 -2
  76. package/extensions/mega-dashboard-cmds.ts +30 -23
  77. package/extensions/mega-db-cmds.ts +11 -3
  78. package/extensions/mega-events/agent-handlers.ts +262 -0
  79. package/extensions/mega-events/compact-handlers.ts +192 -0
  80. package/extensions/mega-events/context-handler.ts +290 -0
  81. package/extensions/mega-events/register.ts +37 -0
  82. package/extensions/mega-events/session-handlers.ts +165 -0
  83. package/extensions/mega-events.ts +15 -780
  84. package/extensions/mega-game-cmds.test.ts +137 -0
  85. package/extensions/mega-game-cmds.ts +122 -0
  86. package/extensions/mega-pipeline/compact.ts +366 -0
  87. package/extensions/mega-pipeline/memory-review.ts +46 -0
  88. package/extensions/mega-pipeline/recall.ts +165 -0
  89. package/extensions/mega-pipeline.ts +9 -537
  90. package/extensions/mega-runtime/helpers.ts +68 -0
  91. package/extensions/mega-runtime/query.ts +29 -0
  92. package/extensions/mega-runtime/state.test.ts +171 -0
  93. package/extensions/mega-runtime/state.ts +967 -0
  94. package/extensions/mega-runtime/widget.test.ts +185 -0
  95. package/extensions/mega-runtime/widget.ts +359 -0
  96. package/extensions/mega-runtime.ts +15 -1093
  97. package/package.json +4 -3
  98. package/src/config/themes.test.ts +116 -0
  99. package/src/config/themes.ts +124 -0
  100. package/src/game/scoring.test.ts +103 -0
  101. package/src/game/scoring.ts +158 -0
  102. package/src/store/sqlite/checkpoints.ts +204 -0
  103. package/src/store/sqlite/dedup-mirror.ts +114 -0
  104. package/src/store/sqlite/foundation.ts +63 -0
  105. package/src/store/sqlite/game-achievements.test.ts +80 -0
  106. package/src/store/sqlite/game-achievements.ts +147 -0
  107. package/src/store/sqlite/game-scores.test.ts +132 -0
  108. package/src/store/sqlite/game-scores.ts +168 -0
  109. package/src/store/sqlite/game-state.test.ts +89 -0
  110. package/src/store/sqlite/game-state.ts +87 -0
  111. package/src/store/sqlite/global-index.ts +305 -0
  112. package/src/store/sqlite/maintenance.ts +294 -0
  113. package/src/store/sqlite/memories.ts +217 -0
  114. package/src/store/sqlite/meta.ts +108 -0
  115. package/src/store/sqlite/model-snapshots.ts +83 -0
  116. package/src/store/sqlite/raptor.ts +107 -0
  117. package/src/store/sqlite/raw-transcript.ts +221 -0
  118. package/src/store/sqlite/schema.ts +305 -0
  119. package/src/store/sqlite/session-state.ts +38 -0
  120. package/src/store/sqlite/stats.ts +127 -0
  121. package/src/store/sqlite/utils.ts +125 -0
  122. package/src/store/sqlite.ts +23 -2204
@@ -8,12 +8,18 @@
8
8
  import { join, dirname, sep } from "node:path";
9
9
  import { fileURLToPath } from "node:url";
10
10
  import { existsSync, writeFileSync, readFileSync, unlinkSync, openSync, closeSync } from "node:fs";
11
- import { spawn } from "node:child_process"; // guardrails-allow PREVENT-PI-004: spawns the optional, user-triggered localhost dashboard server only
11
+ import { spawn, execSync } from "node:child_process"; // guardrails-allow PREVENT-PI-004: spawns the optional, user-triggered localhost dashboard server only
12
12
  /** Register the dashboard server lifecycle commands. */
13
13
  export function registerDashboardCommands(pi, runtime) {
14
- const portFile = join(runtime.currentStateDir, "port.pid");
15
- const runnerFile = join(runtime.currentStateDir, "_dashboard-runner.mjs");
16
- const launchLog = join(runtime.currentStateDir, "_dashboard-launch.log");
14
+ // H3 fix: read currentStateDir at CALL time, not registration time. The
15
+ // previous `const portFile = join(runtime.currentStateDir, ...)` captured the
16
+ // state dir once at extension load; after a repo switch (bindRepo updates
17
+ // currentStateDir) the dashboard commands would read/write the OLD repo's
18
+ // port.pid — potentially spawning a duplicate server or failing to stop the
19
+ // current one. Functions re-resolve on every call.
20
+ const portFile = () => join(runtime.currentStateDir, "port.pid");
21
+ const runnerFile = () => join(runtime.currentStateDir, "_dashboard-runner.mjs");
22
+ const launchLog = () => join(runtime.currentStateDir, "_dashboard-launch.log");
17
23
  // Whether the runner must be spawned with --experimental-strip-types (true only
18
24
  // when we fall back to the .ts source outside node_modules; false when using
19
25
  // the shipped compiled dist/extensions/dashboard-server.js).
@@ -42,15 +48,15 @@ export function registerDashboardCommands(pi, runtime) {
42
48
  const port = await findLivePort();
43
49
  if (!port) {
44
50
  // Stale marker with no live server behind it — clean up.
45
- if (existsSync(portFile)) {
51
+ if (existsSync(portFile())) {
46
52
  try {
47
- unlinkSync(portFile);
53
+ unlinkSync(portFile());
48
54
  }
49
55
  catch { /* ignore */ }
50
56
  }
51
57
  return null;
52
58
  }
53
- return { port, url: `http://localhost:${port}`, hasPidFile: existsSync(portFile) }; // guardrails-allow PREVENT-PI-004: localhost URL of the dashboard server this extension spawned
59
+ return { port, url: `http://localhost:${port}`, hasPidFile: existsSync(portFile()) }; // guardrails-allow PREVENT-PI-004: localhost URL of the dashboard server this extension spawned
54
60
  }
55
61
  /** Version the running server on `port` reports, or null. */
56
62
  async function serverVersion(port) {
@@ -80,7 +86,6 @@ export function registerDashboardCommands(pi, runtime) {
80
86
  * (Linux/macOS) — best-effort, returns null if unavailable. */
81
87
  function pidOnPort(port) {
82
88
  try {
83
- const { execSync } = require("node:child_process"); // guardrails-allow PREVENT-PI-004: localhost-only, reads our own dashboard port owner
84
89
  const out = execSync(`ss -ltnp 2>/dev/null | grep ':${port} '`, { encoding: "utf-8" });
85
90
  const m = out.match(/pid=(\d+)/);
86
91
  return m ? Number(m[1]) : null;
@@ -95,7 +100,7 @@ export function registerDashboardCommands(pi, runtime) {
95
100
  function killServerOnPort(port) {
96
101
  let pid = null;
97
102
  try {
98
- const info = JSON.parse(readFileSync(portFile, "utf-8"));
103
+ const info = JSON.parse(readFileSync(portFile(), "utf-8"));
99
104
  if (info && info.pid)
100
105
  pid = info.pid;
101
106
  }
@@ -109,7 +114,7 @@ export function registerDashboardCommands(pi, runtime) {
109
114
  catch { /* already gone */ }
110
115
  }
111
116
  try {
112
- unlinkSync(portFile);
117
+ unlinkSync(portFile());
113
118
  }
114
119
  catch { /* ignore */ }
115
120
  }
@@ -154,7 +159,7 @@ export function registerDashboardCommands(pi, runtime) {
154
159
  dashboardNeedsStrip = resolved.needsStripTypes;
155
160
  const script = [
156
161
  `import { appendFileSync } from "node:fs";`,
157
- `const __log = ${JSON.stringify(launchLog)};`,
162
+ `const __log = ${JSON.stringify(launchLog())};`,
158
163
  `function __fail(err) {`,
159
164
  ` const msg = "[mega-compact] dashboard failed: " + (err && err.stack ? err.stack : String(err));`,
160
165
  ` try { appendFileSync(__log, msg + "\\n"); } catch { /* ignore */ }`,
@@ -164,7 +169,7 @@ export function registerDashboardCommands(pi, runtime) {
164
169
  `import { launchDashboardServer } from ${JSON.stringify(resolved.entry)};`,
165
170
  `launchDashboardServer(${JSON.stringify(runtime.currentStateDir)}).catch(__fail);`,
166
171
  ].join("\n");
167
- writeFileSync(runnerFile, script);
172
+ writeFileSync(runnerFile(), script);
168
173
  return true;
169
174
  }
170
175
  /** Open a URL in the default browser. Platform-aware. Uses spawn (not exec) to avoid shell injection. */
@@ -219,14 +224,14 @@ export function registerDashboardCommands(pi, runtime) {
219
224
  // orphan, and truncate the launch log so the next error report shows only
220
225
  // this attempt's output.
221
226
  try {
222
- unlinkSync(portFile);
227
+ unlinkSync(portFile());
223
228
  }
224
229
  catch { /* ignore */ }
225
230
  try {
226
- writeFileSync(launchLog, "");
231
+ writeFileSync(launchLog(), "");
227
232
  }
228
233
  catch { /* ignore */ }
229
- const args = dashboardNeedsStrip ? ["--experimental-strip-types", runnerFile] : [runnerFile];
234
+ const args = dashboardNeedsStrip ? ["--experimental-strip-types", runnerFile()] : [runnerFile()];
230
235
  // Redirect the child's stderr to the launch log so that a CRASH BEFORE the
231
236
  // runner's own __fail handler runs (e.g. an ESM module-load / parse error,
232
237
  // or a missing entry) is still captured. With the old `stdio: "ignore"`
@@ -235,7 +240,7 @@ export function registerDashboardCommands(pi, runtime) {
235
240
  // child; once spawned we close our copy (the child keeps its own dup).
236
241
  let stderrFd;
237
242
  try {
238
- stderrFd = openSync(launchLog, "a");
243
+ stderrFd = openSync(launchLog(), "a");
239
244
  }
240
245
  catch {
241
246
  stderrFd = -1; // fall back to ignored stderr
@@ -265,12 +270,12 @@ export function registerDashboardCommands(pi, runtime) {
265
270
  if (!port) {
266
271
  let detail = "";
267
272
  try {
268
- const log = readFileSync(launchLog, "utf-8").trim();
273
+ const log = readFileSync(launchLog(), "utf-8").trim();
269
274
  if (log)
270
275
  detail = ` — ${log.split("\n").slice(-3).join("; ")}`;
271
276
  }
272
277
  catch { /* no log yet */ }
273
- ctx.ui.notify(`[mega-compact] dashboard server failed to start${detail}. See ${launchLog}`);
278
+ ctx.ui.notify(`[mega-compact] dashboard server failed to start${detail}. See ${launchLog()}`);
274
279
  return;
275
280
  }
276
281
  const url = `http://localhost:${port}`; // guardrails-allow PREVENT-PI-004: localhost URL of the dashboard server this extension spawned
@@ -283,12 +288,13 @@ export function registerDashboardCommands(pi, runtime) {
283
288
  pi.registerCommand("mega-dashboard-stop", {
284
289
  description: "Stop the local dashboard server.",
285
290
  handler: async (_args, ctx) => {
286
- if (!existsSync(portFile)) {
291
+ runtime.bindRepo(ctx.cwd);
292
+ if (!existsSync(portFile())) {
287
293
  ctx.ui.notify("[mega-compact] no dashboard server running.");
288
294
  return;
289
295
  }
290
296
  try {
291
- const info = JSON.parse(readFileSync(portFile, "utf-8"));
297
+ const info = JSON.parse(readFileSync(portFile(), "utf-8"));
292
298
  // Verify the server is actually ours by probing the port before killing
293
299
  try {
294
300
  await fetch(`http://localhost:${info.port}/api/snapshot`, { signal: AbortSignal.timeout(1000) }); // guardrails-allow PREVENT-PI-004: localhost probe to verify the dashboard server is ours before stopping it
@@ -296,7 +302,7 @@ export function registerDashboardCommands(pi, runtime) {
296
302
  catch {
297
303
  // Not responding — just clean up stale pid file
298
304
  try {
299
- unlinkSync(portFile);
305
+ unlinkSync(portFile());
300
306
  }
301
307
  catch { /* ok */ }
302
308
  ctx.ui.notify("[mega-compact] dashboard was not running (stale pid file cleaned up).");
@@ -307,7 +313,7 @@ export function registerDashboardCommands(pi, runtime) {
307
313
  }
308
314
  catch { /* already dead */ }
309
315
  try {
310
- unlinkSync(portFile);
316
+ unlinkSync(portFile());
311
317
  }
312
318
  catch { /* ok */ }
313
319
  ctx.ui.notify("[mega-compact] dashboard stopped.");
@@ -316,6 +322,7 @@ export function registerDashboardCommands(pi, runtime) {
316
322
  pi.registerCommand("mega-dashboard-status", {
317
323
  description: "Check if the dashboard server is running.",
318
324
  handler: async (_args, ctx) => {
325
+ runtime.bindRepo(ctx.cwd);
319
326
  const info = await isServerRunning();
320
327
  if (info) {
321
328
  ctx.ui.notify(`[mega-compact] dashboard running at ${info.url}`);
@@ -22,14 +22,15 @@ function fmtBytes(n) {
22
22
  }
23
23
  /** Register the /mega-db-* maintenance commands. */
24
24
  export function registerDbCommands(pi, runtime) {
25
- const stateDir = runtime.currentStateDir;
26
25
  pi.registerCommand("mega-db-stats", {
27
26
  description: "Show mega-compact SQLite DB stats: table row counts, disk footprint (db + WAL + SHM), page count, freelist, WAL frames.",
28
27
  handler: async (_args, ctx) => {
28
+ runtime.bindRepo(ctx.cwd);
29
+ const stateDir = runtime.currentStateDir;
29
30
  const s = getDbStats(stateDir);
30
31
  ctx.ui.notify(`[mega-compact] DB stats — ${stateDir}`);
31
32
  ctx.ui.notify(` main: ${fmtBytes(s.dbBytes)} wal: ${fmtBytes(s.walBytes)} shm: ${fmtBytes(s.shmBytes)}`);
32
- ctx.ui.notify(` pages: ${s.pageCount} (${s.pageSize}B each), freelist: ${s.freelistPages} (${s.pageCount > 0 ? ((s.freelistPages / s.pageCount) * 100).toFixed(1) : "0"}% reusable), wal frames: ${s.walFrames}`);
33
+ ctx.ui.notify(` pages: ${s.pageCount} (${s.pageSize}B each), freelist: ${s.freelistPages} (${s.pageCount > 0 ? ((s.freelistPages / s.pageCount) * 100).toFixed(1) : "0.0"}% reusable), wal frames: ${s.walFrames}`);
33
34
  const tableLines = Object.entries(s.tableCounts)
34
35
  .sort((a, b) => b[1] - a[1])
35
36
  .map(([t, c]) => ` ${t.padEnd(22)} ${String(c).padStart(8)}`);
@@ -46,6 +47,8 @@ export function registerDbCommands(pi, runtime) {
46
47
  pi.registerCommand("mega-db-prune", {
47
48
  description: "Prune raw_transcript + checkpoint_epochs + orphan dedup_mirror rows older than N days (default 30). Usage: /mega-db-prune [days]",
48
49
  handler: async (args, ctx) => {
50
+ runtime.bindRepo(ctx.cwd);
51
+ const stateDir = runtime.currentStateDir;
49
52
  const days = Number.parseInt(args.trim().split(/\s+/)[0] ?? "30", 10);
50
53
  const d = Number.isFinite(days) && days > 0 ? days : 30;
51
54
  const r = pruneOldRows(stateDir, d);
@@ -55,6 +58,8 @@ export function registerDbCommands(pi, runtime) {
55
58
  pi.registerCommand("mega-db-vacuum", {
56
59
  description: "VACUUM the mega-compact SQLite DB (rebuild pages, reclaim freelist space). Heavy: briefly doubles disk usage.",
57
60
  handler: async (_args, ctx) => {
61
+ runtime.bindRepo(ctx.cwd);
62
+ const stateDir = runtime.currentStateDir;
58
63
  const r = vacuumDb(stateDir);
59
64
  ctx.ui.notify(`[mega-compact] ${r.summary}`);
60
65
  },
@@ -62,6 +67,8 @@ export function registerDbCommands(pi, runtime) {
62
67
  pi.registerCommand("mega-db-check", {
63
68
  description: "Run PRAGMA integrity_check + a WAL checkpoint on the mega-compact SQLite DB. Use after a crash or to fold the WAL into the main file.",
64
69
  handler: async (_args, ctx) => {
70
+ runtime.bindRepo(ctx.cwd);
71
+ const stateDir = runtime.currentStateDir;
65
72
  const lines = integrityCheck(stateDir);
66
73
  const healthy = lines.length === 1 && lines[0] === "ok";
67
74
  ctx.ui.notify(`[mega-compact] integrity_check: ${healthy ? "✓ ok" : `⚠ ${lines.length} issue(s)`}`);
@@ -78,6 +85,8 @@ export function registerDbCommands(pi, runtime) {
78
85
  pi.registerCommand("mega-db-reconcile", {
79
86
  description: "Reconcile dedup_mirror.ref_count vs actual raw_transcript refs: fix drift, delete orphan dedup rows, backfill missing content_ref. Run after /mega-db-prune or a crash.",
80
87
  handler: async (_args, ctx) => {
88
+ runtime.bindRepo(ctx.cwd);
89
+ const stateDir = runtime.currentStateDir;
81
90
  const r = reconcileDedupMirror(stateDir);
82
91
  ctx.ui.notify(`[mega-compact] dedup reconcile: fixed ${r.fixedRefCount} ref_count drift, deleted ${r.orphansDeleted} orphan(s), backfilled ${r.refsBackfilled} content_ref`);
83
92
  },
@@ -0,0 +1,222 @@
1
+ import { piCompactWouldNoop, runMemoryReview, } from "../mega-pipeline.js";
2
+ import { memoryReviewCadence, } from "../mega-config.js";
3
+ import { recordScore } from "../../src/store/sqlite.js";
4
+ import { evaluateAndUnlockAchievements } from "../../src/store/sqlite/game-achievements.js";
5
+ import { isMegaCache } from "../../src/game/scoring.js";
6
+ import { resolveRepoRoot } from "../mega-config.js";
7
+ /** Register agent/turn tracking event handlers. */
8
+ export function registerAgentHandlers(pi, runtime, config) {
9
+ // ---- Agent tracking for real-time widget + status-line updates ---------
10
+ pi.on("agent_start", async (_event, ctx) => {
11
+ runtime.activeAgents++;
12
+ runtime.dashboard.event("agent_start", {
13
+ activeAgents: runtime.activeAgents,
14
+ });
15
+ // Surface live agent activity on the status line (toolbar), not just the
16
+ // above-editor widget — otherwise concurrent agents look frozen.
17
+ runtime.setStatus(ctx, `mega-compact: ▶ ${runtime.activeAgents} agent${runtime.activeAgents === 1 ? "" : "s"}`);
18
+ runtime.snapshot(ctx);
19
+ });
20
+ pi.on("agent_end", async (_event, ctx) => {
21
+ runtime.activeAgents = Math.max(0, runtime.activeAgents - 1);
22
+ runtime.dashboard.event("agent_end", {
23
+ activeAgents: runtime.activeAgents,
24
+ });
25
+ if (runtime.activeAgents > 0) {
26
+ runtime.setStatus(ctx, `mega-compact: ▶ ${runtime.activeAgents} agent${runtime.activeAgents === 1 ? "" : "s"}`);
27
+ }
28
+ else {
29
+ runtime.setStatus(ctx, config.auto ? "mega-compact: ready" : "mega-compact: manual only");
30
+ }
31
+ // S16 continuation fallback: if the turn settled idle right after a live-trim
32
+ // compaction AND there is queued work AND we haven't nudged recently, nudge
33
+ // once so the agent continues (the live trim should make this rare). Guarded
34
+ // to never busy-loop: one nudge per 30s, only when truly idle + queued.
35
+ if ((config.auto || config.autoContinueLengthStop) && runtime.activeAgents === 0) {
36
+ try {
37
+ const idle = ctx.isIdle?.() ?? true;
38
+ const queued = ctx.hasPendingMessages?.() ?? false;
39
+ const now = Date.now();
40
+ // DIAG (team-run relief): surface whether the agent is idle + over
41
+ // threshold at agent_end so we can see if a mid-run durable-trim trigger
42
+ // *should* have fired but didn't.
43
+ const overThreshold = (runtime.lastCtxTokens ?? 0) >= runtime.effectiveThreshold;
44
+ runtime.diagAgentEndIdle++;
45
+ runtime.logger.info("agent-end-idle", {
46
+ sessionId: runtime.rt.sessionId,
47
+ idle,
48
+ queued,
49
+ overThreshold,
50
+ ctxPct: runtime.lastCtxPercent,
51
+ ctxTokens: runtime.lastCtxTokens,
52
+ thresholdTokens: config.thresholdTokens,
53
+ wouldNudge: idle &&
54
+ (queued || overThreshold) &&
55
+ now >= runtime.resumeNudgeUntil,
56
+ });
57
+ // S16+S24: MID-RUN DURABLE TRIM. During a long team run (sub-agents),
58
+ // pi's native durable compaction only fires from _checkCompaction at
59
+ // PARENT settle (agent-session.js:760/844), so the on-disk transcript +
60
+ // context meter balloon to ~150k and never relieve until the very end
61
+ // ("compacts but doesn't resume"). agent_end with activeAgents===0 is a
62
+ // SAFE, settled point: calling ctx.compact() here does NOT abort an
63
+ // in-flight turn (the S16 danger is only mid-turn). ctx.compact() runs
64
+ // pi's flow, which fires our session_before_compact handler to supply
65
+ // the durable trim (truncates the transcript from firstKeptEntryId).
66
+ // Guarded three ways: only when truly idle + over threshold, only when
67
+ // pi would actually compact (piCompactWouldNoop skips the user-facing
68
+ // no-op throw), and debounced (one durable trim per 2s) to avoid
69
+ // thrashing the transcript while sub-agents keep settling.
70
+ //
71
+ // FIX "compacts but doesn't resume": the manual ctx.compact() path
72
+ // STOPS the agent loop (agent-session.js:1345). The old resume-nudge
73
+ // was gated on `queued`, so when a sub-agent settled with no
74
+ // *immediately* queued message, the trim fired but the nudge did not,
75
+ // and the (stopped) session hung. The trim still fires on
76
+ // `idle && overThreshold` — we intentionally do NOT add a `!queued`
77
+ // guard, because that would suppress mid-run relief exactly during
78
+ // team-run waves where queued is usually true and relief is needed
79
+ // most. Instead we DECOUPLE the nudge from `queued`: after a durable
80
+ // trim we ALWAYS nudge so the agent reliably restarts. Debounced 30s.
81
+ let didDurableTrim = false;
82
+ if (config.auto && idle && overThreshold && now >= runtime.debounceUntil) {
83
+ // COMPACT-DEDUP FIX: skip the manual durable-trim trigger when pi's
84
+ // NATIVE auto-compaction just fired (or is in-flight). pi emits
85
+ // agent_end BEFORE its own _checkCompaction (per its docstring:
86
+ // "Called after agent_end and before prompt submission"), so a
87
+ // synchronous `piCompactWouldNoop` branch check misses a native
88
+ // compaction that hasn't appended its entry yet — calling
89
+ // ctx.compact() then races with pi and throws "Already compacted"
90
+ // to the user. The `lastCompactAt` cooldown (updated by the
91
+ // session_compact listener for EVERY compaction, native or
92
+ // extension-supplied) closes that race window.
93
+ const sinceCompact = now - (runtime.rt.lastNativeCompactAt ?? 0);
94
+ if (sinceCompact < 10_000) {
95
+ runtime.diagAgentEndDurableSkipRecent++;
96
+ }
97
+ else if (!piCompactWouldNoop(ctx)) {
98
+ runtime.debounceUntil = now + 2000;
99
+ runtime.diagAgentEndDurable++;
100
+ runtime.logger.info("agent-end-durable-trigger", {
101
+ sessionId: runtime.rt.sessionId,
102
+ ctxTokens: runtime.lastCtxTokens,
103
+ thresholdTokens: config.thresholdTokens,
104
+ queued,
105
+ });
106
+ ctx.compact({ customInstructions: undefined }); // guardrails-allow PREVENT-PI-004: local ctx.compact() — no network; agent settled so no in-flight abort. Race-guarded by lastCompactAt cooldown above (ctx.compact returns void → throw is surfaced by pi as compaction_end; the cooldown prevents the call entirely).
107
+ didDurableTrim = true;
108
+ }
109
+ }
110
+ // Restart the agent after a mid-run durable trim (which stopped it), or
111
+ // when it settled idle with queued work. Decoupled from `queued` for the
112
+ // durable-trim case — see FIX note above. Debounced 30s; never blocks.
113
+ const lengthStop = config.autoContinueLengthStop && runtime.rt.lengthStopPending;
114
+ if (idle &&
115
+ now >= runtime.resumeNudgeUntil &&
116
+ ((config.auto && (didDurableTrim || queued)) || lengthStop)) {
117
+ runtime.resumeNudgeUntil = now + 30_000;
118
+ if (runtime.rt.lengthStopPending) {
119
+ runtime.rt.lengthStopPending = false; // one-shot: never re-fire for same stop
120
+ runtime.dashboard.event("length_stop_continue", { turnIndex: runtime.currentTurn });
121
+ runtime.logger.info("length_stop_continue", {
122
+ sessionId: runtime.rt.sessionId,
123
+ didDurableTrim,
124
+ queued,
125
+ });
126
+ }
127
+ // S28: when a length-stop (max-output-token truncation) fired WITHOUT a durable trim, do NOT claim a compaction happened
128
+ // (nothing was compacted on the low-pressure length path). Branch the message so the nudge matches reality.
129
+ const nudgeMsg = lengthStop && !didDurableTrim
130
+ ? "[mega-compact] the last response hit the output-token cap; continue from where it stopped."
131
+ : "[mega-compact] continue from the compacted context above.";
132
+ pi.sendUserMessage(nudgeMsg);
133
+ }
134
+ }
135
+ catch {
136
+ /* non-fatal: a failed nudge never blocks */
137
+ }
138
+ }
139
+ runtime.snapshot(ctx);
140
+ });
141
+ pi.on("turn_start", async (event, ctx) => {
142
+ runtime.currentTurn = event.turnIndex;
143
+ runtime.rt.lengthStopPending = false; // S28: re-arm defensively each user turn
144
+ runtime.dashboard.event("turn_start", { turnIndex: event.turnIndex });
145
+ runtime.snapshot(ctx);
146
+ });
147
+ pi.on("turn_end", async (event, ctx) => {
148
+ runtime.dashboard.event("turn_end", { turnIndex: event.turnIndex });
149
+ runtime.snapshot(ctx);
150
+ // S33: game-mode scoring — record turns + cache metrics per repo, and arm
151
+ // the MEGA CACHE flare (oopsie gag) when the real dedup hit rate exceeds
152
+ // 100%. Gated behind game_mode_on (no scoring when off). Best-effort +
153
+ // non-fatal: a scoring failure must never break the agent loop (G6).
154
+ try {
155
+ if (runtime.getCachedGameState().game_mode_on) {
156
+ const repo = resolveRepoRoot(ctx.cwd) ?? runtime.currentStateDir;
157
+ const st = runtime.store.stats(runtime.rt.sessionId);
158
+ const cachePct = st.dedupHitRate * 100;
159
+ const modelId = runtime.currentModel?.modelId ?? "unknown";
160
+ recordScore(runtime.currentStateDir, {
161
+ repo_root: repo,
162
+ metric: "turns",
163
+ value: runtime.currentTurn,
164
+ meta: { modelId, turnIndex: event.turnIndex },
165
+ });
166
+ recordScore(runtime.currentStateDir, {
167
+ repo_root: repo,
168
+ metric: "cache",
169
+ value: cachePct,
170
+ meta: {
171
+ hits: st.dedupCollapsed + runtime.rt.recallInjections,
172
+ lookups: st.checkpointCount,
173
+ },
174
+ });
175
+ // MEGA CACHE: the real ratio >1 (dedupHitRate>1) → trophy row + flare.
176
+ if (isMegaCache(cachePct)) {
177
+ recordScore(runtime.currentStateDir, {
178
+ repo_root: repo,
179
+ metric: "mega_cache",
180
+ value: cachePct,
181
+ meta: { peakPct: cachePct, firstSeenTs: Date.now() },
182
+ });
183
+ runtime.armMegaCacheFlare(cachePct);
184
+ }
185
+ // S35: evaluate achievements after scoring; arm a one-time flare for
186
+ // the newly-unlocked ones (consumed by snapshot() → widget toast).
187
+ const newTitles = evaluateAndUnlockAchievements(runtime.currentStateDir);
188
+ if (newTitles.length)
189
+ runtime.armAchievementFlare(newTitles);
190
+ }
191
+ }
192
+ catch {
193
+ /* non-fatal: scoring must never break the agent loop */
194
+ }
195
+ // S20+S24: auto-review the conversation and persist durable memories. The
196
+ // review cadence scales with pressure (memoryReviewCadence): as context
197
+ // fills, the conversation is reviewed more often so memories keep pace with
198
+ // faster churn. Best-effort + non-fatal: a review failure must never break
199
+ // the agent loop. Debounced by the pressure-adjusted interval.
200
+ if (config.memoryAutoReview && runtime.currentTurn > 0) {
201
+ const cadence = memoryReviewCadence(runtime.pressureBand, config.memoryReviewInterval);
202
+ if (runtime.currentTurn % cadence === 0) {
203
+ // S20+S24: review the conversation and persist durable memories. The
204
+ // cadence scales with pressure (memoryReviewCadence): as context fills,
205
+ // the conversation is reviewed more often so memories keep pace with
206
+ // faster churn. Shared runMemoryReview body (also used on compact).
207
+ const entries = ctx.sessionManager.getEntries();
208
+ const view = runtime.engineView(entries.flatMap((e) => (e.message ? [e.message] : [])));
209
+ await runMemoryReview(runtime, view, "turn");
210
+ }
211
+ }
212
+ // S28: detect max-output-token truncation. event.message.stopReason is the
213
+ // pi-ai StopReason union; 'length' == generation hit max_tokens OUTPUT cap
214
+ // (INPUT-orthogonal to context-window overflow). Arm the agent_end nudge.
215
+ if (config.autoContinueLengthStop &&
216
+ event.message.role === "assistant" &&
217
+ event.message.stopReason === "length") {
218
+ runtime.rt.lengthStopPending = true;
219
+ runtime.dashboard.event("length_stop", { turnIndex: event.turnIndex });
220
+ }
221
+ });
222
+ }
@@ -0,0 +1,162 @@
1
+ import { driveNativeCompaction, } from "../mega-compact-driver.js";
2
+ import { estimateBlockTokens } from "../../src/tokens.js";
3
+ import { recordScore, getDedupStats } from "../../src/store/sqlite.js";
4
+ import { evaluateAndUnlockAchievements } from "../../src/store/sqlite/game-achievements.js";
5
+ import { resolveRepoRoot } from "../mega-config.js";
6
+ /**
7
+ * Build a minimal fallback compaction so pi never runs its throwing compact().
8
+ *
9
+ * Used when our Trident/RAPTOR summary is empty or there is nothing to
10
+ * summarize (the anchor floor protects every message). We still record a
11
+ * resume summary + truncate from prep.firstKeptEntryId so the session always
12
+ * gets a compact summary and resumes. Returns undefined only if pi handed us
13
+ * no preparation cut point at all.
14
+ */
15
+ function fallbackCompaction(event) {
16
+ const prep = event.preparation;
17
+ if (!prep?.firstKeptEntryId)
18
+ return undefined;
19
+ // When messagesToSummarize is empty the anchor floor protects everything,
20
+ // so firstKeptEntryId == current first entry and the trim is a no-op — but
21
+ // we still record a resume summary so the session has context after compaction.
22
+ const tokensBefore = prep.tokensBefore ?? 0;
23
+ const summary = `[mega-compact] context compacted at ${tokensBefore.toLocaleString()} tokens ` +
24
+ `(anchor floor active). Continue from the most recent messages above.`;
25
+ return {
26
+ compaction: {
27
+ summary,
28
+ firstKeptEntryId: prep.firstKeptEntryId,
29
+ tokensBefore,
30
+ estimatedTokensAfter: estimateBlockTokens(summary),
31
+ },
32
+ };
33
+ }
34
+ /**
35
+ * Debounced resume-nudge: restart the agent loop after a compaction (which
36
+ * may have stopped it). Idempotent — one nudge per 30s, never blocks.
37
+ */
38
+ function nudgeResume(pi, runtime) {
39
+ try {
40
+ const now = Date.now();
41
+ if (now >= runtime.resumeNudgeUntil) {
42
+ runtime.resumeNudgeUntil = now + 30_000;
43
+ pi.sendUserMessage("[mega-compact] continue from the compacted context above.");
44
+ }
45
+ }
46
+ catch {
47
+ /* non-fatal: a failed nudge never blocks */
48
+ }
49
+ }
50
+ /** Register native compaction event handlers. */
51
+ export function registerCompactHandlers(pi, runtime, config) {
52
+ // ---- Supply a DURABLE trim to pi's native compaction (Fix B) ----------
53
+ // We run the Trident pipeline to produce a compressed summary, then return
54
+ // it as a CompactionResult. pi writes the summary into a compactionSummary
55
+ // entry AND truncates the on-disk transcript from firstKeptEntryId. This is
56
+ // the durable fix for "tokens grow on read": the trim survives resume, so
57
+ // there is no full-reload + additive recall inflation.
58
+ pi.on("session_before_compact", async (event, ctx) => {
59
+ runtime.resetRuntime(ctx.sessionManager.getSessionId());
60
+ // DIAG (team-run relief): this is the ONLY durable-trim entry point. Log
61
+ // every fire + whether we supplied a compaction (truncates transcript) or
62
+ // fell through to {} (pi runs its own). If this is sparse during a team
63
+ // run, the durable trim is firing too late (only at parent settle).
64
+ const prep = event.preparation;
65
+ runtime.diagBeforeCompactFires++;
66
+ runtime.logger.info("before-compact-entry", {
67
+ sessionId: runtime.rt.sessionId,
68
+ reason: event.reason,
69
+ hasPrep: !!prep,
70
+ msgsToSummarize: prep?.messagesToSummarize?.length ?? 0,
71
+ firstKeptEntryId: prep?.firstKeptEntryId ?? null,
72
+ activeAgents: runtime.activeAgents,
73
+ });
74
+ if (!config.auto)
75
+ return {}; // let pi run its own native compaction
76
+ try {
77
+ const result = driveNativeCompaction(event, runtime, config);
78
+ if (result && result.compaction.summary?.trim()) {
79
+ runtime.diagBeforeCompactSupplied++;
80
+ runtime.logger.info("native-compact", {
81
+ sessionId: runtime.rt.sessionId,
82
+ firstKeptEntryId: result.compaction.firstKeptEntryId,
83
+ tokensBefore: result.compaction.tokensBefore,
84
+ summaryTokens: result.compaction.estimatedTokensAfter,
85
+ });
86
+ nudgeResume(pi, runtime);
87
+ return { compaction: result.compaction };
88
+ }
89
+ // FIX "compacts but doesn't resume" + "Nothing to compact" regression:
90
+ // when we have nothing to summarize (anchor floor protects everything →
91
+ // messagesToSummarize empty) or our Trident/RAPTOR summary came back
92
+ // EMPTY, pi's OWN compact() throws "Nothing to compact (session too
93
+ // small)" and leaves the session stuck with no resume context. Instead
94
+ // of returning {} (which makes pi run its throwing compact()), supply a
95
+ // fallback compaction from prep.firstKeptEntryId with a minimal resume
96
+ // summary. This ALWAYS injects a compact summary so the session
97
+ // resumes, and never surfaces the "Nothing to compact" error to the user.
98
+ const fb = fallbackCompaction(event);
99
+ if (fb) {
100
+ runtime.diagBeforeCompactSupplied++;
101
+ runtime.logger.info("native-compact-fallback", {
102
+ sessionId: runtime.rt.sessionId,
103
+ firstKeptEntryId: fb.compaction.firstKeptEntryId,
104
+ tokensBefore: fb.compaction.tokensBefore,
105
+ reason: event.reason,
106
+ });
107
+ nudgeResume(pi, runtime);
108
+ return { compaction: fb.compaction };
109
+ }
110
+ }
111
+ catch (err) {
112
+ runtime.logger.error("native-compact-failed", {
113
+ sessionId: runtime.rt.sessionId,
114
+ error: String(err instanceof Error ? err.message : err),
115
+ });
116
+ }
117
+ // Absolute last resort: let pi run its own (may throw "Nothing to compact").
118
+ return {};
119
+ });
120
+ // COMPACT-DEDUP FIX: track EVERY compaction (native + extension-supplied)
121
+ // so the agent_end durable-trim guard can skip a redundant ctx.compact()
122
+ // when pi just compacted. Without this, agent_end fires ctx.compact()
123
+ // synchronously AFTER pi's native auto-compaction appended a compaction
124
+ // entry but BEFORE our branch read sees it on the next tick — racing
125
+ // into a user-facing "Already compacted" throw. `lastCompactAt` is the
126
+ // race-closing signal: any compaction (manual/threshold/overflow, ours
127
+ // or pi's own) stamps it, and the agent_end guard skips for 10s.
128
+ pi.on("session_compact", async (_event, _ctx) => {
129
+ runtime.rt.lastNativeCompactAt = Date.now();
130
+ runtime.rt.lastCompactAt = Date.now();
131
+ runtime.logger.info("session-compacted", {
132
+ sessionId: runtime.rt.sessionId,
133
+ at: runtime.rt.lastCompactAt,
134
+ });
135
+ // S33: game-mode dedupe scoring — record the DELTA of cumulative dedup
136
+ // collapses since the last compact (leaderboard SUMs the deltas). Gated
137
+ // behind game_mode_on (no scoring when off). Best-effort + non-fatal (G6).
138
+ try {
139
+ if (runtime.getCachedGameState().game_mode_on) {
140
+ const ds = getDedupStats(runtime.currentStateDir);
141
+ const delta = ds.deduped - runtime.lastDedupCollapsed;
142
+ runtime.lastDedupCollapsed = ds.deduped;
143
+ if (delta > 0) {
144
+ const repo = resolveRepoRoot(_ctx.cwd) ?? runtime.currentStateDir;
145
+ recordScore(runtime.currentStateDir, {
146
+ repo_root: repo,
147
+ metric: "dedupe",
148
+ value: delta,
149
+ meta: { compactCount: runtime.rt.compactCount },
150
+ });
151
+ }
152
+ }
153
+ // S35: evaluate achievements after scoring; arm the one-time flare.
154
+ const newTitles = evaluateAndUnlockAchievements(runtime.currentStateDir);
155
+ if (newTitles.length)
156
+ runtime.armAchievementFlare(newTitles);
157
+ }
158
+ catch {
159
+ /* non-fatal: scoring must never break compaction */
160
+ }
161
+ });
162
+ }