pi-mega-compact 0.9.2 → 0.10.1

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 (143) hide show
  1. package/README.md +3 -2
  2. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +95 -47
  3. package/dist/extensions/dashboard-server/api-contracts/index.js +1 -1
  4. package/dist/extensions/dashboard-server/api-contracts/turns.js +10 -0
  5. package/dist/extensions/dashboard-server/api-contracts.test.js +3 -2
  6. package/dist/extensions/dashboard-server/routes-topics.js +87 -0
  7. package/dist/extensions/dashboard-server/routes-turns.js +251 -0
  8. package/dist/extensions/dashboard-server/routes.js +3 -1
  9. package/dist/extensions/dashboard-server/server.js +6 -2
  10. package/dist/extensions/mega-compact.js +10 -6
  11. package/dist/extensions/mega-config.js +4 -0
  12. package/dist/extensions/mega-events/agent-handlers.js +62 -49
  13. package/dist/extensions/mega-events/context-handler.js +48 -2
  14. package/dist/extensions/mega-events/session-handlers.js +24 -0
  15. package/dist/extensions/mega-metrics-cmds.js +90 -0
  16. package/dist/extensions/mega-pipeline/recall.js +47 -17
  17. package/dist/extensions/mega-topics-cmds.js +111 -0
  18. package/dist/extensions/mega-turn-store.js +110 -0
  19. package/dist/extensions/mega-turn-store.test.js +68 -0
  20. package/dist/src/config/turns.js +48 -0
  21. package/dist/src/fork.js +33 -0
  22. package/dist/src/fork.test.js +81 -0
  23. package/dist/src/intent.js +51 -0
  24. package/dist/src/intent.test.js +78 -0
  25. package/dist/src/metrics/index.js +4 -0
  26. package/dist/src/metrics/turns.js +118 -0
  27. package/dist/src/metrics/turns.test.js +152 -0
  28. package/dist/src/store/sqlite/raw-transcript.js +5 -3
  29. package/dist/src/store/sqlite.dbmirror.test.js +9 -0
  30. package/dist/src/store/turns/connection.js +123 -0
  31. package/dist/src/store/turns/contract-compliance.test.js +428 -0
  32. package/dist/src/store/turns/index.js +31 -0
  33. package/dist/src/store/turns/memory-store.js +357 -0
  34. package/dist/src/store/turns/memory-store.test.js +9 -0
  35. package/dist/src/store/turns/migrations.js +178 -0
  36. package/dist/src/store/turns/migrations.test.js +138 -0
  37. package/dist/src/store/turns/schema.js +115 -0
  38. package/dist/src/store/turns/sqlite-store.js +455 -0
  39. package/dist/src/store/turns/sqlite-store.test.js +144 -0
  40. package/dist/src/store/turns/types.js +16 -0
  41. package/dist/src/topics/cluster.js +141 -0
  42. package/dist/src/topics/cluster.test.js +150 -0
  43. package/dist/src/topics/index.js +6 -0
  44. package/dist/src/topics/kselection.js +142 -0
  45. package/dist/src/topics/labels.js +56 -0
  46. package/dist/src/topics/store.js +119 -0
  47. package/dist/src/topics/store.test.js +117 -0
  48. package/dist/src/topics/types.js +8 -0
  49. package/dist/src/wiki.js +133 -0
  50. package/dist/src/wiki.test.js +171 -0
  51. package/extensions/dashboard-client/dist/assets/{AchievementTiles-QPo46Pwg.js → AchievementTiles-DUulvXUY.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{AchievementTiles-QPo46Pwg.js.map → AchievementTiles-DUulvXUY.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{AchievementsTab-BDJxRXRY.js → AchievementsTab-Dx0YtCNf.js} +2 -2
  54. package/extensions/dashboard-client/dist/assets/{AchievementsTab-BDJxRXRY.js.map → AchievementsTab-Dx0YtCNf.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/{CacheTab-CUMqXQjd.js → CacheTab-DtkaUo1v.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{CacheTab-CUMqXQjd.js.map → CacheTab-DtkaUo1v.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/{ConfigTab-j1dRzurG.js → ConfigTab-CLa4KfiZ.js} +2 -2
  58. package/extensions/dashboard-client/dist/assets/{ConfigTab-j1dRzurG.js.map → ConfigTab-CLa4KfiZ.js.map} +1 -1
  59. package/extensions/dashboard-client/dist/assets/{EventsTab-CMglXK4p.js → EventsTab-Q6tEIBBi.js} +2 -2
  60. package/extensions/dashboard-client/dist/assets/{EventsTab-CMglXK4p.js.map → EventsTab-Q6tEIBBi.js.map} +1 -1
  61. package/extensions/dashboard-client/dist/assets/{GameTab-fpaDoyxi.js → GameTab-Hq7GNWSY.js} +2 -2
  62. package/extensions/dashboard-client/dist/assets/{GameTab-fpaDoyxi.js.map → GameTab-Hq7GNWSY.js.map} +1 -1
  63. package/extensions/dashboard-client/dist/assets/{MetricsTab-CWG2crG_.js → MetricsTab-BtmBV6lU.js} +2 -2
  64. package/extensions/dashboard-client/dist/assets/{MetricsTab-CWG2crG_.js.map → MetricsTab-BtmBV6lU.js.map} +1 -1
  65. package/extensions/dashboard-client/dist/assets/{OverviewTab-BaV2D2nf.js → OverviewTab-CL10PwVU.js} +2 -2
  66. package/extensions/dashboard-client/dist/assets/{OverviewTab-BaV2D2nf.js.map → OverviewTab-CL10PwVU.js.map} +1 -1
  67. package/extensions/dashboard-client/dist/assets/{ReposTab-C-0kuZ_0.js → ReposTab-DAwANwbI.js} +2 -2
  68. package/extensions/dashboard-client/dist/assets/{ReposTab-C-0kuZ_0.js.map → ReposTab-DAwANwbI.js.map} +1 -1
  69. package/extensions/dashboard-client/dist/assets/{SessionsTab-CSYhddCA.js → SessionsTab-Ec08TZRQ.js} +2 -2
  70. package/extensions/dashboard-client/dist/assets/{SessionsTab-CSYhddCA.js.map → SessionsTab-Ec08TZRQ.js.map} +1 -1
  71. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Dy4KRjSM.js → TimeSavedCard-COb8dpTy.js} +2 -2
  72. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Dy4KRjSM.js.map → TimeSavedCard-COb8dpTy.js.map} +1 -1
  73. package/extensions/dashboard-client/dist/assets/TopicsTab-kvX3F3dY.js +2 -0
  74. package/extensions/dashboard-client/dist/assets/TopicsTab-kvX3F3dY.js.map +1 -0
  75. package/extensions/dashboard-client/dist/assets/TurnsTab-CCUE73Aa.js +2 -0
  76. package/extensions/dashboard-client/dist/assets/TurnsTab-CCUE73Aa.js.map +1 -0
  77. package/extensions/dashboard-client/dist/assets/{index-D_WtU2TV.js → index-UMrussFX.js} +10 -10
  78. package/extensions/dashboard-client/dist/assets/index-UMrussFX.js.map +1 -0
  79. package/extensions/dashboard-client/dist/assets/{index-og3ghxyY.css → index-gn66lYD2.css} +1 -1
  80. package/extensions/dashboard-client/dist/assets/{useSSE-C9yVJlGP.js → useSSE-BDHZ7CeS.js} +2 -2
  81. package/extensions/dashboard-client/dist/assets/{useSSE-C9yVJlGP.js.map → useSSE-BDHZ7CeS.js.map} +1 -1
  82. package/extensions/dashboard-client/dist/index.html +2 -2
  83. package/extensions/dashboard-client/src/App.tsx +10 -2
  84. package/extensions/dashboard-client/src/api/client.ts +84 -1
  85. package/extensions/dashboard-client/src/main.tsx +1 -0
  86. package/extensions/dashboard-client/src/styles/turns.css +142 -0
  87. package/extensions/dashboard-client/src/tabs/TopicsTab.tsx +185 -0
  88. package/extensions/dashboard-client/src/tabs/TurnsTab.tsx +326 -0
  89. package/extensions/dashboard-server/api-contracts/endpoints.ts +389 -291
  90. package/extensions/dashboard-server/api-contracts/game-types.ts +141 -88
  91. package/extensions/dashboard-server/api-contracts/index.ts +153 -133
  92. package/extensions/dashboard-server/api-contracts/turns.ts +114 -0
  93. package/extensions/dashboard-server/api-contracts.test.ts +920 -919
  94. package/extensions/dashboard-server/routes-topics.ts +119 -0
  95. package/extensions/dashboard-server/routes-turns.ts +282 -0
  96. package/extensions/dashboard-server/routes.ts +8 -1
  97. package/extensions/dashboard-server/server.ts +6 -2
  98. package/extensions/mega-compact.ts +75 -59
  99. package/extensions/mega-config.ts +232 -220
  100. package/extensions/mega-events/agent-handlers.ts +194 -151
  101. package/extensions/mega-events/context-handler.ts +50 -1
  102. package/extensions/mega-events/session-handlers.ts +23 -0
  103. package/extensions/mega-metrics-cmds.ts +114 -0
  104. package/extensions/mega-pipeline/recall.ts +215 -152
  105. package/extensions/mega-topics-cmds.ts +151 -0
  106. package/extensions/mega-turn-store.test.ts +108 -0
  107. package/extensions/mega-turn-store.ts +169 -0
  108. package/package.json +3 -1
  109. package/src/config/turns.ts +69 -0
  110. package/src/fork.test.ts +94 -0
  111. package/src/fork.ts +78 -0
  112. package/src/intent.test.ts +87 -0
  113. package/src/intent.ts +115 -0
  114. package/src/metrics/index.ts +9 -0
  115. package/src/metrics/turns.test.ts +165 -0
  116. package/src/metrics/turns.ts +201 -0
  117. package/src/store/sqlite/raw-transcript.ts +9 -3
  118. package/src/store/sqlite.dbmirror.test.ts +10 -0
  119. package/src/store/turns/connection.ts +137 -0
  120. package/src/store/turns/contract-compliance.test.ts +562 -0
  121. package/src/store/turns/index.ts +65 -0
  122. package/src/store/turns/memory-store.test.ts +15 -0
  123. package/src/store/turns/memory-store.ts +469 -0
  124. package/src/store/turns/migrations.test.ts +161 -0
  125. package/src/store/turns/migrations.ts +203 -0
  126. package/src/store/turns/schema.ts +158 -0
  127. package/src/store/turns/sqlite-store.test.ts +161 -0
  128. package/src/store/turns/sqlite-store.ts +656 -0
  129. package/src/store/turns/types.ts +181 -0
  130. package/src/topics/cluster.test.ts +176 -0
  131. package/src/topics/cluster.ts +191 -0
  132. package/src/topics/index.ts +18 -0
  133. package/src/topics/kselection.ts +182 -0
  134. package/src/topics/labels.ts +67 -0
  135. package/src/topics/store.test.ts +132 -0
  136. package/src/topics/store.ts +208 -0
  137. package/src/topics/types.ts +64 -0
  138. package/src/wiki.test.ts +217 -0
  139. package/src/wiki.ts +198 -0
  140. package/dist/extensions/openclaw-mega-compact.js +0 -291
  141. package/dist/src/minilm.js +0 -92
  142. package/dist/src/wordpiece.js +0 -129
  143. package/extensions/dashboard-client/dist/assets/index-D_WtU2TV.js.map +0 -1
@@ -17,11 +17,11 @@ import { execSync } from "node:child_process"; // guardrails-allow PREVENT-PI-00
17
17
  * session trimmed. Explicit MEGACOMPACT_THRESHOLD_TOKENS always wins.
18
18
  */
19
19
  export const COMPACT_TIERS = {
20
- low: 50_000,
21
- medium: 100_000,
22
- high: 200_000,
23
- ultra: 1_000_000,
24
- mega: 10_000_000,
20
+ low: 50_000,
21
+ medium: 100_000,
22
+ high: 200_000,
23
+ ultra: 1_000_000,
24
+ mega: 10_000_000,
25
25
  } as const;
26
26
  export type CompactTier = keyof typeof COMPACT_TIERS;
27
27
 
@@ -33,11 +33,11 @@ export type CompactTier = keyof typeof COMPACT_TIERS;
33
33
  * is NOT scaled by this map — it stays an absolute token count.
34
34
  */
35
35
  export const TIER_PCT: Record<CompactTier, number> = {
36
- low: 0.5,
37
- medium: 0.6,
38
- high: 0.7,
39
- ultra: 0.7,
40
- mega: 0.75,
36
+ low: 0.5,
37
+ medium: 0.6,
38
+ high: 0.7,
39
+ ultra: 0.7,
40
+ mega: 0.75,
41
41
  };
42
42
 
43
43
  /**
@@ -48,125 +48,126 @@ export const TIER_PCT: Record<CompactTier, number> = {
48
48
  * band (see MegaRuntime.pressureBand), which climbs low→mega as context fills.
49
49
  */
50
50
  export interface MegaConfig {
51
- tier: CompactTier | "custom";
52
- /**
53
- * Compaction threshold as a fraction of the model context window (e.g. 0.70
54
- * for "high"). null for `custom` (explicit MEGACOMPACT_THRESHOLD_TOKENS, which
55
- * stays an ABSOLUTE token count, never percent-scaled).
56
- */
57
- tierPct: number | null;
58
- thresholdTokens: number;
59
- stateDir: string;
60
- fastGatePct: number;
61
- anchorUserMessages: number;
62
- preserveRecent: number;
63
- /** High-pressure floor for preserveRecent — when context is near the limit
64
- * we compact deeper, but never below this (keeps recent turns for coherence). */
65
- preserveRecentMin: number;
66
- auto: boolean;
67
- autoInline: boolean;
68
- autoInlineK: number;
69
- /** S28: auto-continue the agent after a max-output-token length stop by
70
- * reusing the existing S16 resume-nudge. Default true. Off = silent (the
71
- * prior behavior). PREVENT-PI-003: restart via user-role sendUserMessage. */
72
- autoContinueLengthStop: boolean;
73
- /** S38: max retries for transient errors (5xx/429/network/max-output-token
74
- * text that is NOT a length stopReason — S28 owns those). Default 5.
75
- * `0` disables all transient retries (reverts to S28-only). */
76
- autoRetryTransientMax: number;
77
- /** S38: max retries for permanent errors (auth/config/malformed). Default 1.
78
- * `0` disables permanent-error retries. */
79
- autoRetryPermanentMax: number;
80
- /** S38.5: strict race-guard — 30s cooldown + deferred ctx.compact() re-check
81
- * (closes the first-race-in-burst window). Default true. `false` reverts to
82
- * the v0.7.4 synchronous 10s-cooldown behavior. */
83
- raceGuardStrict: boolean;
84
- /** S38.6: max consecutive errors before circuit-breaker trips (stops retrying).
85
- * Default 10. When `errorRetryCount` exceeds this across multiple turns,
86
- * the extension stops retrying until a successful turn resets it. */
87
- maxConsecutiveErrors: number;
88
- /** S38.7: hard-stop switch — when true, ALL error retries are disabled.
89
- * Default false. Set via env to force S28-only behavior (length-stop continues
90
- * only). */
91
- errorRetryHardStop: boolean;
92
- /** R1 (retry redesign): base unit (ms) for errorRetryBackoffMs(count) pacing.
93
- * The schedule is base, 2*base, 4*base, 6*base (cap) — so the default 5000
94
- * yields 5s/10s/20s/30s. errorRetryUntil is now GATING (previously it was
95
- * documented as non-gating); a nudge cannot fire before errorRetryUntil
96
- * elapses. */
97
- errorRetryBackoffMs: number;
98
- /** R2: session-global cap on total S38 nudges across ALL bursts. Hitting it
99
- * is terminal for the session — the extension stops nudging entirely,
100
- * independent of the per-burst max and the circuit breaker. Default 3.
101
- * `0` disables (reverts to per-burst + circuit-breaker only). */
102
- errorRetrySessionMax: number;
103
- /** R3: consecutive identical error-text count at which a 'transient'
104
- * classification is upgraded to 'poisoned-context' (the stateful repeat
105
- * signal). Default 3. Raise to make the upgrade less aggressive. */
106
- poisonedContextRepeatThreshold: number;
107
- /** S29: override the auto-compact fire point for tiered configs, as a
108
- * fraction of the context window (e.g. 0.85). null = inherit the tier's
109
- * tierPct (default; preserves existing fire points). The context-handler
110
- * gate fires on context % (reliable), not token count (under-reported),
111
- * so it catches the overshoot that causes max-output-token truncation.
112
- * `custom` (tierPct null) ignores this — it keeps the absolute token gate. */
113
- autoPctTrigger: number | null;
114
- dedupSim: number;
115
- /** RAPTOR hierarchical recall enabled (Fix D). Drives both live recall and
116
- * the durable-trim summary source (root summary). */
117
- raptorEnabled: boolean;
118
- /** Legacy v0.4.28 behavior: auto-trigger calls ctx.compact() (which STOPS
119
- * the agent). Default false — the S16 redesign uses the live context-event
120
- * trim + pi native auto-compaction instead (compact and continue). Kept for
121
- * one release as rollback. */
122
- legacyDurableTrim: boolean;
123
- /** S27: durable raw-transcript DB mirror (MEGACOMPACT_DB_MIRROR). When on,
124
- * raw message bytes + checkpoint-epoch bookkeeping are appended to the
125
- * SQLite store so a compacted window can be rehydrated locally instead of
126
- * from the pi runtime transcript. Default OFF — additive, no behavior
127
- * change until flipped on. legacyDurableTrim takes precedence (the legacy
128
- * v0.4.28 ctx.compact() path does not emit the S27 mirror hook). */
129
- dbMirror: boolean;
130
- /** Cross-repo recall enabled (S17). Resume + /mega-recall --cross-repo can
131
- * pull checkpoints from OTHER repos via the PGlite HNSW index. Default true. */
132
- crossRepoEnabled: boolean;
133
- /** Stricter cosine floor for cross-repo hits (S17). Default 0.90 (trigram) /
134
- * tighter than same-repo so only genuinely-relevant cross-repo context is
135
- * injected. */
136
- crossRepoCosine: number;
137
- /** Memory-RAG auto-review enabled (S20). Every memoryReviewInterval turns the
138
- * conversation is auto-reviewed into durable add/replace/remove memories. */
139
- memoryAutoReview: boolean;
140
- /** Turn cadence for the auto-review scan (S20). Default 10. */
141
- memoryReviewInterval: number;
142
- /** Token ceiling for the re-injected recall block (Fix C). Recall stops
143
- * adding checkpoints once the block would exceed this — bounds read-path
144
- * token cost so it can never net-inflate the window. */
145
- recallMaxTokens: number;
146
- /** Inline-dedupe recalled checkpoints against the live window (Fix C): drop
147
- * a hit whose summary is ≥ dedupSim similar to a live message — "dedupe on
148
- * inline/read" so we never re-inject context already resident. */
149
- windowDedupe: boolean;
150
- /** Render the above-editor TUI widget (MEGACOMPACT_TUI_WIDGET). Default true.
151
- * Set to 0 to suppress the panel entirely — the widget is a persistent,
152
- * animated, full-width region that repaints on its own cadence, which fights
153
- * terminals where the user drives scrollback themselves (notably pi running
154
- * inside a Neovim `:terminal`, where every repaint yanks the view back to
155
- * the bottom). Compaction is unaffected; only the panel is suppressed. */
156
- tuiWidget: boolean;
157
- debug: boolean;
51
+ tier: CompactTier | "custom";
52
+ /**
53
+ * Compaction threshold as a fraction of the model context window (e.g. 0.70
54
+ * for "high"). null for `custom` (explicit MEGACOMPACT_THRESHOLD_TOKENS, which
55
+ * stays an ABSOLUTE token count, never percent-scaled).
56
+ */
57
+ tierPct: number | null;
58
+ thresholdTokens: number;
59
+ stateDir: string;
60
+ fastGatePct: number;
61
+ anchorUserMessages: number;
62
+ preserveRecent: number;
63
+ /** High-pressure floor for preserveRecent — when context is near the limit
64
+ * we compact deeper, but never below this (keeps recent turns for coherence). */
65
+ preserveRecentMin: number;
66
+ auto: boolean;
67
+ autoInline: boolean;
68
+ autoInlineK: number;
69
+ /** S28: auto-continue the agent after a max-output-token length stop by
70
+ * reusing the existing S16 resume-nudge. Default true. Off = silent (the
71
+ * prior behavior). PREVENT-PI-003: restart via user-role sendUserMessage. */
72
+ autoContinueLengthStop: boolean;
73
+ /** S38: max retries for transient errors (5xx/429/network/max-output-token
74
+ * text that is NOT a length stopReason — S28 owns those). Default 5.
75
+ * `0` disables all transient retries (reverts to S28-only). */
76
+ autoRetryTransientMax: number;
77
+ /** S38: max retries for permanent errors (auth/config/malformed). Default 1.
78
+ * `0` disables permanent-error retries. */
79
+ autoRetryPermanentMax: number;
80
+ /** S38.5: strict race-guard — 30s cooldown + deferred ctx.compact() re-check
81
+ * (closes the first-race-in-burst window). Default true. `false` reverts to
82
+ * the v0.7.4 synchronous 10s-cooldown behavior. */
83
+ raceGuardStrict: boolean;
84
+ /** S38.6: max consecutive errors before circuit-breaker trips (stops retrying).
85
+ * Default 10. When `errorRetryCount` exceeds this across multiple turns,
86
+ * the extension stops retrying until a successful turn resets it. */
87
+ maxConsecutiveErrors: number;
88
+ /** S38.7: hard-stop switch — when true, ALL error retries are disabled.
89
+ * Default false. Set via env to force S28-only behavior (length-stop continues
90
+ * only). */
91
+ errorRetryHardStop: boolean;
92
+ /** R1 (retry redesign): base unit (ms) for errorRetryBackoffMs(count) pacing.
93
+ * The schedule is base, 2*base, 4*base, 6*base (cap) — so the default 5000
94
+ * yields 5s/10s/20s/30s. errorRetryUntil is now GATING (previously it was
95
+ * documented as non-gating); a nudge cannot fire before errorRetryUntil
96
+ * elapses. */
97
+ errorRetryBackoffMs: number;
98
+ /** R2: session-global cap on total S38 nudges across ALL bursts. Hitting it
99
+ * is terminal for the session — the extension stops nudging entirely,
100
+ * independent of the per-burst max and the circuit breaker. Default 3.
101
+ * `0` disables (reverts to per-burst + circuit-breaker only). */
102
+ errorRetrySessionMax: number;
103
+ /** R3: consecutive identical error-text count at which a 'transient'
104
+ * classification is upgraded to 'poisoned-context' (the stateful repeat
105
+ * signal). Default 3. Raise to make the upgrade less aggressive. */
106
+ poisonedContextRepeatThreshold: number;
107
+ /** S29: override the auto-compact fire point for tiered configs, as a
108
+ * fraction of the context window (e.g. 0.85). null = inherit the tier's
109
+ * tierPct (default; preserves existing fire points). The context-handler
110
+ * gate fires on context % (reliable), not token count (under-reported),
111
+ * so it catches the overshoot that causes max-output-token truncation.
112
+ * `custom` (tierPct null) ignores this — it keeps the absolute token gate. */
113
+ autoPctTrigger: number | null;
114
+ dedupSim: number;
115
+ /** RAPTOR hierarchical recall enabled (Fix D). Drives both live recall and
116
+ * the durable-trim summary source (root summary). */
117
+ raptorEnabled: boolean;
118
+ /** Legacy v0.4.28 behavior: auto-trigger calls ctx.compact() (which STOPS
119
+ * the agent). Default false — the S16 redesign uses the live context-event
120
+ * trim + pi native auto-compaction instead (compact and continue). Kept for
121
+ * one release as rollback. */
122
+ legacyDurableTrim: boolean;
123
+ /** S27: durable raw-transcript DB mirror (MEGACOMPACT_DB_MIRROR). When on,
124
+ * raw message bytes + checkpoint-epoch bookkeeping are appended to the
125
+ * SQLite store so a compacted window can be rehydrated locally instead of
126
+ * from the pi runtime transcript. Default OFF — additive, no behavior
127
+ * change until flipped on. legacyDurableTrim takes precedence (the legacy
128
+ * v0.4.28 ctx.compact() path does not emit the S27 mirror hook). */
129
+ dbMirror: boolean;
130
+ /** S49: isolated per-turn store (turns.db). Default ON. OFF = legacy main-db
131
+ * turn path (S48 behavior byte-identical). Mirrors TurnsConfig.TURNS_DB_ENABLED. */
132
+ turnsDbEnabled: boolean;
133
+ /** S51: auto-categorizing wiki (k-means + TF-IDF over real embeddings). Default ON.
134
+ * Mirrors TurnsConfig.AUTO_WIKI_ENABLED. Rebuild fires every Nth compaction. */
135
+ autoWikiEnabled: boolean;
136
+ /** Cross-repo recall enabled (S17). Resume + /mega-recall --cross-repo can
137
+ * pull checkpoints from OTHER repos via the PGlite HNSW index. Default true. */
138
+ crossRepoEnabled: boolean;
139
+ /** Stricter cosine floor for cross-repo hits (S17). Default 0.90 (trigram) /
140
+ * tighter than same-repo so only genuinely-relevant cross-repo context is
141
+ * injected. */
142
+ crossRepoCosine: number;
143
+ /** Memory-RAG auto-review enabled (S20). Every memoryReviewInterval turns the
144
+ * conversation is auto-reviewed into durable add/replace/remove memories. */
145
+ memoryAutoReview: boolean;
146
+ /** Turn cadence for the auto-review scan (S20). Default 10. */
147
+ memoryReviewInterval: number;
148
+ /** Token ceiling for the re-injected recall block (Fix C). Recall stops
149
+ * adding checkpoints once the block would exceed this — bounds read-path
150
+ * token cost so it can never net-inflate the window. */
151
+ recallMaxTokens: number;
152
+ /** Inline-dedupe recalled checkpoints against the live window (Fix C): drop
153
+ * a hit whose summary is dedupSim similar to a live message — "dedupe on
154
+ * inline/read" so we never re-inject context already resident. */
155
+ windowDedupe: boolean;
156
+ debug: boolean;
157
+ /** Master reconciliation: TUI shutdown widget (MEGACOMPACT_TUI_WIDGET=0 to disable). */
158
+ tuiWidget: boolean;
158
159
  }
159
160
 
160
161
  function envFlag(name: string, fallback: number): number {
161
- const v = process.env[name];
162
- if (v == null || v === "") return fallback;
163
- const n = Number(v);
164
- return Number.isFinite(n) ? n : fallback;
162
+ const v = process.env[name];
163
+ if (v == null || v === "") return fallback;
164
+ const n = Number(v);
165
+ return Number.isFinite(n) ? n : fallback;
165
166
  }
166
167
  function envBool(name: string, fallback: boolean): boolean {
167
- const v = process.env[name];
168
- if (v == null || v === "") return fallback;
169
- return v === "true" || v === "1";
168
+ const v = process.env[name];
169
+ if (v == null || v === "") return fallback;
170
+ return v === "true" || v === "1";
170
171
  }
171
172
 
172
173
  /**
@@ -179,21 +180,22 @@ function envBool(name: string, fallback: boolean): boolean {
179
180
  * keeps `tierPct: null` and an ABSOLUTE `thresholdTokens` (never percent-scaled).
180
181
  */
181
182
  function resolveThreshold(): {
182
- tier: CompactTier | "custom";
183
- tierPct: number | null;
184
- thresholdTokens: number;
183
+ tier: CompactTier | "custom";
184
+ tierPct: number | null;
185
+ thresholdTokens: number;
185
186
  } {
186
- const explicit = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
187
- if (explicit != null && explicit !== "") {
188
- const n = Number(explicit);
189
- if (Number.isFinite(n)) return { tier: "custom", tierPct: null, thresholdTokens: n };
190
- }
191
- const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
192
- const tier = (raw in COMPACT_TIERS ? raw : "low") as CompactTier;
193
- const tierPct = TIER_PCT[tier];
194
- // Boot fallback: sane gate before the first context event provides a window.
195
- const thresholdTokens = Math.round(tierPct * 200_000);
196
- return { tier, tierPct, thresholdTokens };
187
+ const explicit = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
188
+ if (explicit != null && explicit !== "") {
189
+ const n = Number(explicit);
190
+ if (Number.isFinite(n))
191
+ return { tier: "custom", tierPct: null, thresholdTokens: n };
192
+ }
193
+ const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
194
+ const tier = (raw in COMPACT_TIERS ? raw : "low") as CompactTier;
195
+ const tierPct = TIER_PCT[tier];
196
+ // Boot fallback: sane gate before the first context event provides a window.
197
+ const thresholdTokens = Math.round(tierPct * 200_000);
198
+ return { tier, tierPct, thresholdTokens };
197
199
  }
198
200
 
199
201
  /**
@@ -209,17 +211,17 @@ function resolveThreshold(): {
209
211
  * unit-testable without the pi runtime.
210
212
  */
211
213
  export function effectiveThresholdTokens(opts: {
212
- tierPct: number | null;
213
- fallbackThreshold: number;
214
- window: number;
215
- explicitThreshold?: number;
214
+ tierPct: number | null;
215
+ fallbackThreshold: number;
216
+ window: number;
217
+ explicitThreshold?: number;
216
218
  }): number {
217
- if (opts.tierPct == null) {
218
- // custom: absolute threshold, never percent-scaled
219
- return opts.explicitThreshold ?? opts.fallbackThreshold;
220
- }
221
- if (opts.window > 0) return Math.round(opts.tierPct * opts.window);
222
- return opts.fallbackThreshold;
219
+ if (opts.tierPct == null) {
220
+ // custom: absolute threshold, never percent-scaled
221
+ return opts.explicitThreshold ?? opts.fallbackThreshold;
222
+ }
223
+ if (opts.window > 0) return Math.round(opts.tierPct * opts.window);
224
+ return opts.fallbackThreshold;
223
225
  }
224
226
 
225
227
  /**
@@ -229,12 +231,12 @@ export function effectiveThresholdTokens(opts: {
229
231
  * `custom` (tierPct null) falls back to the legacy 70% default.
230
232
  */
231
233
  function resolveFastGatePct(tierPct: number | null): number {
232
- const raw = process.env.MEGACOMPACT_FAST_GATE_PCT;
233
- if (raw != null && raw !== "") {
234
- const n = Number(raw);
235
- if (Number.isFinite(n)) return n;
236
- }
237
- return tierPct != null ? Math.round(tierPct * 100) : 70;
234
+ const raw = process.env.MEGACOMPACT_FAST_GATE_PCT;
235
+ if (raw != null && raw !== "") {
236
+ const n = Number(raw);
237
+ if (Number.isFinite(n)) return n;
238
+ }
239
+ return tierPct != null ? Math.round(tierPct * 100) : 70;
238
240
  }
239
241
 
240
242
  /**
@@ -245,63 +247,73 @@ function resolveFastGatePct(tierPct: number | null): number {
245
247
  * "how full" signal that drives the tier label, trim depth, and memory cadence.)
246
248
  */
247
249
  export {
248
- pressureFromPct,
249
- preserveRecentForPressure,
250
- pressureRatio,
251
- pressureBand,
252
- memoryReviewCadence,
253
- type PressureBand,
250
+ pressureFromPct,
251
+ preserveRecentForPressure,
252
+ pressureRatio,
253
+ pressureBand,
254
+ memoryReviewCadence,
255
+ type PressureBand,
254
256
  } from "../src/config.js";
255
257
 
256
258
  /** Build the resolved config from env + defaults. */
257
259
  export function loadConfig(): MegaConfig {
258
- const { tier, tierPct, thresholdTokens } = resolveThreshold();
259
- // S29: optional percent-based fire-point override for tiered configs.
260
- // null = inherit tierPct (default; preserves existing fire points). Clamped
261
- // to [0.1, 1] so a bogus env can't disable or invert the gate. Ignored by
262
- // the `custom` tier (tierPct null) which keeps the absolute token gate.
263
- const aptRaw = process.env.MEGACOMPACT_AUTO_PCT_TRIGGER;
264
- const autoPctTrigger =
265
- aptRaw && aptRaw !== "" && Number.isFinite(Number(aptRaw))
266
- ? Math.min(1, Math.max(0.1, Number(aptRaw)))
267
- : null;
268
- return {
269
- tier,
270
- tierPct,
271
- // Global default; the live store/dashboard are rebound per-repo at runtime
272
- // via MegaRuntime.bindRepo() so each git repo gets its own isolated state dir.
273
- stateDir: process.env.MEGACOMPACT_STATE_DIR ?? STATE_DIR_DEFAULT,
274
- fastGatePct: resolveFastGatePct(tierPct),
275
- thresholdTokens,
276
- anchorUserMessages: envFlag("MEGACOMPACT_ANCHOR_USER_MESSAGES", 3),
277
- preserveRecent: envFlag("MEGACOMPACT_PRESERVE_RECENT", 4),
278
- preserveRecentMin: envFlag("MEGACOMPACT_PRESERVE_RECENT_MIN", 2),
279
- auto: envBool("MEGACOMPACT_AUTO", true),
280
- autoInline: envBool("MEGACOMPACT_AUTO_INLINE", true),
281
- autoContinueLengthStop: envBool("MEGACOMPACT_AUTO_CONTINUE_LENGTH_STOP", true),
282
- autoRetryTransientMax: envFlag("MEGACOMPACT_AUTO_RETRY_TRANSIENT_MAX", 5),
283
- autoRetryPermanentMax: envFlag("MEGACOMPACT_AUTO_RETRY_PERMANENT_MAX", 1),
284
- raceGuardStrict: envBool("MEGACOMPACT_RACE_GUARD_STRICT", true),
285
- maxConsecutiveErrors: envFlag("MEGACOMPACT_MAX_CONSECUTIVE_ERRORS", 10),
286
- errorRetryHardStop: envBool("MEGACOMPACT_ERROR_RETRY_HARD_STOP", false),
287
- errorRetryBackoffMs: envFlag("MEGACOMPACT_ERROR_RETRY_BACKOFF_MS", 5000),
288
- errorRetrySessionMax: envFlag("MEGACOMPACT_ERROR_RETRY_SESSION_MAX", 3),
289
- poisonedContextRepeatThreshold: envFlag("MEGACOMPACT_POISONED_REPEAT_THRESHOLD", 3),
290
- autoPctTrigger,
291
- autoInlineK: envFlag("MEGACOMPACT_AUTO_INLINE_K", 3),
292
- dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
293
- raptorEnabled: envBool("MEGACOMPACT_RAPTOR_ENABLED", true),
294
- legacyDurableTrim: envBool("MEGACOMPACT_LEGACY_DURABLE_TRIM", false),
295
- dbMirror: envBool("MEGACOMPACT_DB_MIRROR", false),
296
- crossRepoEnabled: envBool("MEGACOMPACT_CROSSREPO_ENABLED", true),
297
- crossRepoCosine: Number(process.env.MEGACOMPACT_CROSSREPO_COSINE ?? "0.90"),
298
- memoryAutoReview: envBool("MEGACOMPACT_MEMORY_AUTO_REVIEW", true),
299
- memoryReviewInterval: envFlag("MEGACOMPACT_MEMORY_REVIEW_INTERVAL", 10),
300
- recallMaxTokens: envFlag("MEGACOMPACT_RECALL_MAX_TOKENS", 1500),
301
- windowDedupe: envBool("MEGACOMPACT_WINDOW_DEDUPE", true),
302
- tuiWidget: envBool("MEGACOMPACT_TUI_WIDGET", true),
303
- debug: envBool("MEGACOMPACT_DEBUG", false),
304
- };
260
+ const { tier, tierPct, thresholdTokens } = resolveThreshold();
261
+ // S29: optional percent-based fire-point override for tiered configs.
262
+ // null = inherit tierPct (default; preserves existing fire points). Clamped
263
+ // to [0.1, 1] so a bogus env can't disable or invert the gate. Ignored by
264
+ // the `custom` tier (tierPct null) which keeps the absolute token gate.
265
+ const aptRaw = process.env.MEGACOMPACT_AUTO_PCT_TRIGGER;
266
+ const autoPctTrigger =
267
+ aptRaw && aptRaw !== "" && Number.isFinite(Number(aptRaw))
268
+ ? Math.min(1, Math.max(0.1, Number(aptRaw)))
269
+ : null;
270
+ return {
271
+ tier,
272
+ tierPct,
273
+ // Global default; the live store/dashboard are rebound per-repo at runtime
274
+ // via MegaRuntime.bindRepo() so each git repo gets its own isolated state dir.
275
+ stateDir: process.env.MEGACOMPACT_STATE_DIR ?? STATE_DIR_DEFAULT,
276
+ fastGatePct: resolveFastGatePct(tierPct),
277
+ thresholdTokens,
278
+ anchorUserMessages: envFlag("MEGACOMPACT_ANCHOR_USER_MESSAGES", 3),
279
+ preserveRecent: envFlag("MEGACOMPACT_PRESERVE_RECENT", 4),
280
+ preserveRecentMin: envFlag("MEGACOMPACT_PRESERVE_RECENT_MIN", 2),
281
+ auto: envBool("MEGACOMPACT_AUTO", true),
282
+ autoInline: envBool("MEGACOMPACT_AUTO_INLINE", true),
283
+ autoContinueLengthStop: envBool(
284
+ "MEGACOMPACT_AUTO_CONTINUE_LENGTH_STOP",
285
+ true,
286
+ ),
287
+ autoRetryTransientMax: envFlag("MEGACOMPACT_AUTO_RETRY_TRANSIENT_MAX", 5),
288
+ autoRetryPermanentMax: envFlag("MEGACOMPACT_AUTO_RETRY_PERMANENT_MAX", 1),
289
+ raceGuardStrict: envBool("MEGACOMPACT_RACE_GUARD_STRICT", true),
290
+ maxConsecutiveErrors: envFlag("MEGACOMPACT_MAX_CONSECUTIVE_ERRORS", 10),
291
+ errorRetryHardStop: envBool("MEGACOMPACT_ERROR_RETRY_HARD_STOP", false),
292
+ errorRetryBackoffMs: envFlag("MEGACOMPACT_ERROR_RETRY_BACKOFF_MS", 5000),
293
+ errorRetrySessionMax: envFlag("MEGACOMPACT_ERROR_RETRY_SESSION_MAX", 3),
294
+ poisonedContextRepeatThreshold: envFlag(
295
+ "MEGACOMPACT_POISONED_REPEAT_THRESHOLD",
296
+ 3,
297
+ ),
298
+ autoPctTrigger,
299
+ autoInlineK: envFlag("MEGACOMPACT_AUTO_INLINE_K", 3),
300
+ dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
301
+ raptorEnabled: envBool("MEGACOMPACT_RAPTOR_ENABLED", true),
302
+ legacyDurableTrim: envBool("MEGACOMPACT_LEGACY_DURABLE_TRIM", false),
303
+ dbMirror: envBool("MEGACOMPACT_DB_MIRROR", false),
304
+ // S49: isolated per-turn store (turns.db). Default ON. OFF = legacy main-db
305
+ // turn path (S48 behavior). Mirrors src/config/turns.ts TURNS_DB_ENABLED.
306
+ turnsDbEnabled: envBool("MEGACOMPACT_TURNS_DB", true),
307
+ autoWikiEnabled: envBool("MEGACOMPACT_AUTO_WIKI", true),
308
+ crossRepoEnabled: envBool("MEGACOMPACT_CROSSREPO_ENABLED", true),
309
+ crossRepoCosine: Number(process.env.MEGACOMPACT_CROSSREPO_COSINE ?? "0.90"),
310
+ memoryAutoReview: envBool("MEGACOMPACT_MEMORY_AUTO_REVIEW", true),
311
+ memoryReviewInterval: envFlag("MEGACOMPACT_MEMORY_REVIEW_INTERVAL", 10),
312
+ recallMaxTokens: envFlag("MEGACOMPACT_RECALL_MAX_TOKENS", 1500),
313
+ windowDedupe: envBool("MEGACOMPACT_WINDOW_DEDUPE", true),
314
+ tuiWidget: envBool("MEGACOMPACT_TUI_WIDGET", true),
315
+ debug: envBool("MEGACOMPACT_DEBUG", false),
316
+ };
305
317
  }
306
318
 
307
319
  /**
@@ -316,16 +328,16 @@ export function loadConfig(): MegaConfig {
316
328
  * non-git directory (caller falls back to a global state dir).
317
329
  */
318
330
  export function resolveRepoRoot(cwd: string): string | undefined {
319
- try {
320
- const out = execSync("git rev-parse --show-toplevel", {
321
- cwd,
322
- encoding: "utf-8",
323
- stdio: ["ignore", "pipe", "ignore"],
324
- }).trim();
325
- return out || undefined;
326
- } catch {
327
- return undefined;
328
- }
331
+ try {
332
+ const out = execSync("git rev-parse --show-toplevel", {
333
+ cwd,
334
+ encoding: "utf-8",
335
+ stdio: ["ignore", "pipe", "ignore"],
336
+ }).trim();
337
+ return out || undefined;
338
+ } catch {
339
+ return undefined;
340
+ }
329
341
  }
330
342
 
331
343
  /**
@@ -334,7 +346,7 @@ export function resolveRepoRoot(cwd: string): string | undefined {
334
346
  * cwds (the explicit MEGACOMPACT_STATE_DIR override, if set).
335
347
  */
336
348
  export function repoStateDir(cwd: string, fallback: string): string {
337
- const root = resolveRepoRoot(cwd);
338
- if (!root) return fallback;
339
- return join(root, ".pi", "mega-compact");
349
+ const root = resolveRepoRoot(cwd);
350
+ if (!root) return fallback;
351
+ return join(root, ".pi", "mega-compact");
340
352
  }