pi-mega-compact 0.11.3 → 0.11.5
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/endpoints.js +19 -0
- package/dist/extensions/dashboard-server/api-contracts/memory.js +8 -0
- package/dist/extensions/dashboard-server/api-contracts/provider-cache.js +1 -0
- package/dist/extensions/dashboard-server/api-contracts/setup.js +10 -0
- package/dist/extensions/dashboard-server/index-reader.js +6 -2
- package/dist/extensions/dashboard-server/routes-cache.js +115 -55
- package/dist/extensions/dashboard-server/routes-memory.js +51 -0
- package/dist/extensions/dashboard-server/routes-repo.js +2 -2
- package/dist/extensions/dashboard-server/routes-setup.js +162 -0
- package/dist/extensions/dashboard-server/routes.js +2 -0
- package/dist/extensions/dashboard-server/server.js +7 -1
- package/dist/extensions/mega-commands.js +189 -1
- package/dist/extensions/mega-config.js +3 -0
- package/dist/extensions/mega-events/agent-handlers.js +41 -6
- package/dist/extensions/mega-events/compact-handlers.js +1 -0
- package/dist/extensions/mega-events/context-handler.js +37 -92
- package/dist/extensions/mega-events/context-hwm.js +51 -0
- package/dist/extensions/mega-events/error-classifier.js +118 -128
- package/dist/extensions/mega-events/mirror-append.js +107 -0
- package/dist/extensions/mega-events/outage-advisor.js +59 -0
- package/dist/extensions/mega-events/perf-handler.js +51 -1
- package/dist/extensions/mega-events/recall-tail.js +59 -0
- package/dist/extensions/mega-events/session-handlers.js +15 -8
- package/dist/extensions/mega-runtime/reset-runtime.js +6 -0
- package/dist/extensions/mega-runtime/runtime.js +7 -0
- package/dist/src/embedder.js +41 -2
- package/dist/src/memoryStats.js +75 -0
- package/dist/src/prefix-break.js +43 -0
- package/dist/src/pricing.js +1 -1
- package/dist/src/store/sqlite/perf-samples.js +144 -16
- package/dist/src/store/turns/memory-store.js +8 -0
- package/dist/src/store/turns/sqlite-store.js +14 -3
- package/dist/src/store/turns/types.js +18 -1
- package/dist/src/store/vectorIndex.js +26 -5
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js → AchievementTiles-BoL7qBOJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js.map → AchievementTiles-BoL7qBOJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/AchievementsTab-BFDE3HYc.js +2 -0
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-DjxfYrDc.js.map → AchievementsTab-BFDE3HYc.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js +2 -0
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js → ConfigTab-DDFE-ckA.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js.map → ConfigTab-DDFE-ckA.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js → EventsTab-B6UxZ7PM.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js.map → EventsTab-B6UxZ7PM.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js → GameTab-DehUX-KE.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js.map → GameTab-DehUX-KE.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js → MaintenanceTab-0GWRt4wn.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js.map → MaintenanceTab-0GWRt4wn.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js → MetricsTab-C3rwMVsN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js.map → MetricsTab-C3rwMVsN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/OverviewTab-39O9_x5j.js +2 -0
- package/extensions/dashboard-client/dist/assets/{OverviewTab-D4kGvsiQ.js.map → OverviewTab-39O9_x5j.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js +2 -0
- package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js → SessionsTab-DXDTCi3r.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js.map → SessionsTab-DXDTCi3r.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js +2 -0
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js +2 -0
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js → TopicsTab-BAw972BV.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js.map → TopicsTab-BAw972BV.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js → TurnsTab-Cf5mGIdl.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js.map → TurnsTab-Cf5mGIdl.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js +2 -0
- package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{index-BX0bcgLu.js → index-4gQ7auad.js} +10 -10
- package/extensions/dashboard-client/dist/assets/index-4gQ7auad.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js +2 -0
- package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js → useSSE-DNgMuyP6.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js.map → useSSE-DNgMuyP6.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/App.tsx +4 -0
- package/extensions/dashboard-client/src/api/client.ts +14 -0
- package/extensions/dashboard-client/src/components/ActiveReposTable.tsx +21 -12
- package/extensions/dashboard-client/src/components/ProviderCacheCard.tsx +56 -0
- package/extensions/dashboard-client/src/components/SavingsByModelTable.tsx +38 -57
- package/extensions/dashboard-client/src/tabs/CacheTab.tsx +1 -0
- package/extensions/dashboard-client/src/tabs/SetupTab.tsx +355 -0
- package/extensions/dashboard-client/tsconfig.json +3 -2
- package/extensions/dashboard-client/vite.config.ts +2 -0
- package/extensions/dashboard-server/api-contracts/endpoints.ts +36 -3
- package/extensions/dashboard-server/api-contracts/index.ts +7 -0
- package/extensions/dashboard-server/api-contracts/memory.ts +27 -0
- package/extensions/dashboard-server/api-contracts/multi-repo.ts +231 -231
- package/extensions/dashboard-server/api-contracts/provider-cache.ts +37 -0
- package/extensions/dashboard-server/api-contracts/setup.ts +58 -0
- package/extensions/dashboard-server/index-reader.ts +138 -121
- package/extensions/dashboard-server/routes-cache.ts +161 -85
- package/extensions/dashboard-server/routes-memory.ts +63 -0
- package/extensions/dashboard-server/routes-repo.ts +36 -36
- package/extensions/dashboard-server/routes-setup.ts +181 -0
- package/extensions/dashboard-server/routes.ts +2 -0
- package/extensions/dashboard-server/server.ts +6 -0
- package/extensions/mega-commands.ts +211 -1
- package/extensions/mega-config.ts +18 -0
- package/extensions/mega-events/agent-handlers.ts +44 -5
- package/extensions/mega-events/compact-handlers.ts +1 -0
- package/extensions/mega-events/context-handler.ts +45 -110
- package/extensions/mega-events/context-hwm.ts +85 -0
- package/extensions/mega-events/error-classifier.ts +156 -124
- package/extensions/mega-events/mirror-append.ts +155 -0
- package/extensions/mega-events/outage-advisor.ts +79 -0
- package/extensions/mega-events/perf-handler.ts +79 -5
- package/extensions/mega-events/recall-tail.ts +88 -0
- package/extensions/mega-events/session-handlers.ts +14 -7
- package/extensions/mega-runtime/helpers.ts +14 -0
- package/extensions/mega-runtime/reset-runtime.ts +6 -0
- package/extensions/mega-runtime/runtime.ts +7 -0
- package/package.json +5 -2
- package/src/embedder.ts +42 -2
- package/src/memoryStats.ts +107 -0
- package/src/prefix-break.ts +83 -0
- package/src/pricing.ts +2 -2
- package/src/store/sqlite/perf-samples.ts +215 -22
- package/src/store/turns/memory-store.ts +10 -1
- package/src/store/turns/sqlite-store.ts +27 -15
- package/src/store/turns/types.ts +21 -0
- package/src/store/vectorIndex.ts +33 -4
- package/dist/extensions/conflict-scan.test.js +0 -115
- package/dist/extensions/dashboard-server/api-contracts.test.js +0 -884
- package/dist/extensions/dashboard-server/html.test.js +0 -52
- package/dist/extensions/dashboard-server/perf-server.test.js +0 -80
- package/dist/extensions/dashboard-server/routes-cache.test.js +0 -248
- package/dist/extensions/dashboard-server/server.test.js +0 -120
- package/dist/extensions/dashboard-server-s32.test.js +0 -181
- package/dist/extensions/dashboard-server.test.js +0 -339
- package/dist/extensions/mega-cache-replay.test.js +0 -400
- package/dist/extensions/mega-compact-s38.test.js +0 -820
- package/dist/extensions/mega-compact.test.js +0 -1112
- package/dist/extensions/mega-events.test.js +0 -47
- package/dist/extensions/mega-game-cmds.test.js +0 -163
- package/dist/extensions/mega-runtime/snapshot.test.js +0 -127
- package/dist/extensions/mega-runtime/state.test.js +0 -266
- package/dist/extensions/mega-runtime/widget.test.js +0 -343
- package/dist/extensions/mega-shutdown-widget.test.js +0 -121
- package/dist/extensions/mega-teamrun.test.js +0 -156
- package/dist/extensions/mega-trim.test.js +0 -58
- package/dist/extensions/mega-turn-store.test.js +0 -68
- package/dist/src/boundary.test.js +0 -170
- package/dist/src/compact.test.js +0 -78
- package/dist/src/config/themes.test.js +0 -94
- package/dist/src/dedup/dedup.test.js +0 -41
- package/dist/src/dedup/l1.test.js +0 -50
- package/dist/src/dedup/raptor/buildHistory.test.js +0 -292
- package/dist/src/dedup/raptor/multilevel-serve.test.js +0 -229
- package/dist/src/dedup/raptor/multilevel.test.js +0 -238
- package/dist/src/dedup/raptor/promote.test.js +0 -69
- package/dist/src/dedup/raptor/raptor.test.js +0 -248
- package/dist/src/dedup/raptor/retrieval.test.js +0 -95
- package/dist/src/dedup/raptor/serve-gate.test.js +0 -298
- package/dist/src/dedup/sprint12.test.js +0 -219
- package/dist/src/dedup-engine.test.js +0 -472
- package/dist/src/driftDetection.test.js +0 -87
- package/dist/src/e2e.test.js +0 -698
- package/dist/src/engine.test.js +0 -111
- package/dist/src/extractive.test.js +0 -147
- package/dist/src/fork.test.js +0 -81
- package/dist/src/game/scoring.test.js +0 -98
- package/dist/src/httpEmbedder.test.js +0 -277
- package/dist/src/importance.test.js +0 -372
- package/dist/src/intent.test.js +0 -78
- package/dist/src/log.test.js +0 -42
- package/dist/src/mechanical-fix.test.js +0 -65
- package/dist/src/memory.test.js +0 -70
- package/dist/src/memoryConsolidate.test.js +0 -38
- package/dist/src/memoryOps.test.js +0 -129
- package/dist/src/memoryRecall.test.js +0 -171
- package/dist/src/memoryRoundtrip.test.js +0 -137
- package/dist/src/metrics/turns.test.js +0 -152
- package/dist/src/mirror/mirror.test.js +0 -185
- package/dist/src/raptor-inject-summaries.test.js +0 -162
- package/dist/src/ratio.bench.test.js +0 -897
- package/dist/src/recall.integration.test.js +0 -77
- package/dist/src/recall.test.js +0 -379
- package/dist/src/sprint14.test.js +0 -219
- package/dist/src/sprint4x-rag-verification.test.js +0 -93
- package/dist/src/store/compression.test.js +0 -139
- package/dist/src/store/memoryIndex.test.js +0 -51
- package/dist/src/store/migrate.test.js +0 -139
- package/dist/src/store/pgOpenGuard.test.js +0 -74
- package/dist/src/store/phase01.test.js +0 -81
- package/dist/src/store/phase04.test.js +0 -54
- package/dist/src/store/sprint10.test.js +0 -186
- package/dist/src/store/sqlite/game-achievements.test.js +0 -67
- package/dist/src/store/sqlite/game-scores.test.js +0 -106
- package/dist/src/store/sqlite/game-state.test.js +0 -76
- package/dist/src/store/sqlite/global-index.test.js +0 -136
- package/dist/src/store/sqlite/mechanical-fix.test.js +0 -146
- package/dist/src/store/sqlite/perf-samples.test.js +0 -146
- package/dist/src/store/sqlite/raptor.test.js +0 -106
- package/dist/src/store/sqlite/turns.test.js +0 -183
- package/dist/src/store/sqlite.cachehit.test.js +0 -55
- package/dist/src/store/sqlite.dbmaint.test.js +0 -158
- package/dist/src/store/sqlite.dbmirror.test.js +0 -184
- package/dist/src/store/turns/contract-compliance.test.js +0 -428
- package/dist/src/store/turns/memory-store.test.js +0 -9
- package/dist/src/store/turns/migrations.test.js +0 -138
- package/dist/src/store/turns/sqlite-store.test.js +0 -144
- package/dist/src/store/vectorIndex.test.js +0 -123
- package/dist/src/store.test.js +0 -161
- package/dist/src/supersede.test.js +0 -36
- package/dist/src/topics/cluster.test.js +0 -150
- package/dist/src/topics/store.test.js +0 -117
- package/dist/src/types.test.js +0 -9
- package/dist/src/vector-search-cache.test.js +0 -157
- package/dist/src/vectorStore.test.js +0 -479
- package/dist/src/wiki.test.js +0 -171
- package/extensions/conflict-scan.test.ts +0 -129
- package/extensions/dashboard-client/dist/assets/AchievementsTab-DjxfYrDc.js +0 -2
- package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js +0 -2
- package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/OverviewTab-D4kGvsiQ.js +0 -2
- package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js +0 -2
- package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js +0 -2
- package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-BX0bcgLu.js.map +0 -1
- package/extensions/dashboard-server/api-contracts.test.ts +0 -1065
- package/extensions/dashboard-server/html.test.ts +0 -62
- package/extensions/dashboard-server/perf-server.test.ts +0 -101
- package/extensions/dashboard-server/routes-cache.test.ts +0 -302
- package/extensions/dashboard-server/server.test.ts +0 -131
- package/extensions/dashboard-server-s32.test.ts +0 -195
- package/extensions/dashboard-server.test.ts +0 -382
- package/extensions/mega-cache-replay.test.ts +0 -526
- package/extensions/mega-compact-s38.test.ts +0 -872
- package/extensions/mega-compact.test.ts +0 -1509
- package/extensions/mega-events.test.ts +0 -55
- package/extensions/mega-game-cmds.test.ts +0 -197
- package/extensions/mega-runtime/snapshot.test.ts +0 -139
- package/extensions/mega-runtime/state.test.ts +0 -262
- package/extensions/mega-runtime/widget.test.ts +0 -468
- package/extensions/mega-shutdown-widget.test.ts +0 -141
- package/extensions/mega-teamrun.test.ts +0 -176
- package/extensions/mega-trim.test.ts +0 -64
- package/extensions/mega-turn-store.test.ts +0 -108
- package/src/boundary.test.ts +0 -187
- package/src/compact.test.ts +0 -99
- package/src/config/themes.test.ts +0 -116
- package/src/dedup/dedup.test.ts +0 -46
- package/src/dedup/l1.test.ts +0 -57
- package/src/dedup/raptor/buildHistory.test.ts +0 -353
- package/src/dedup/raptor/multilevel-serve.test.ts +0 -273
- package/src/dedup/raptor/multilevel.test.ts +0 -325
- package/src/dedup/raptor/promote.test.ts +0 -82
- package/src/dedup/raptor/raptor.test.ts +0 -297
- package/src/dedup/raptor/retrieval.test.ts +0 -118
- package/src/dedup/raptor/serve-gate.test.ts +0 -348
- package/src/dedup/sprint12.test.ts +0 -242
- package/src/dedup-engine.test.ts +0 -670
- package/src/driftDetection.test.ts +0 -100
- package/src/e2e.test.ts +0 -843
- package/src/engine.test.ts +0 -123
- package/src/extractive.test.ts +0 -174
- package/src/fork.test.ts +0 -94
- package/src/game/scoring.test.ts +0 -103
- package/src/httpEmbedder.test.ts +0 -286
- package/src/importance.test.ts +0 -538
- package/src/intent.test.ts +0 -87
- package/src/log.test.ts +0 -47
- package/src/mechanical-fix.test.ts +0 -70
- package/src/memory.test.ts +0 -92
- package/src/memoryConsolidate.test.ts +0 -47
- package/src/memoryOps.test.ts +0 -148
- package/src/memoryRecall.test.ts +0 -194
- package/src/memoryRoundtrip.test.ts +0 -155
- package/src/metrics/turns.test.ts +0 -165
- package/src/mirror/mirror.test.ts +0 -240
- package/src/raptor-inject-summaries.test.ts +0 -228
- package/src/ratio.bench.test.ts +0 -1316
- package/src/recall.integration.test.ts +0 -96
- package/src/recall.test.ts +0 -467
- package/src/sprint14.test.ts +0 -245
- package/src/sprint4x-rag-verification.test.ts +0 -119
- package/src/store/compression.test.ts +0 -168
- package/src/store/memoryIndex.test.ts +0 -61
- package/src/store/migrate.test.ts +0 -158
- package/src/store/pgOpenGuard.test.ts +0 -89
- package/src/store/phase01.test.ts +0 -88
- package/src/store/phase04.test.ts +0 -58
- package/src/store/sprint10.test.ts +0 -214
- package/src/store/sqlite/game-achievements.test.ts +0 -80
- package/src/store/sqlite/game-scores.test.ts +0 -132
- package/src/store/sqlite/game-state.test.ts +0 -89
- package/src/store/sqlite/global-index.test.ts +0 -222
- package/src/store/sqlite/mechanical-fix.test.ts +0 -162
- package/src/store/sqlite/perf-samples.test.ts +0 -169
- package/src/store/sqlite/raptor.test.ts +0 -139
- package/src/store/sqlite/turns.test.ts +0 -218
- package/src/store/sqlite.cachehit.test.ts +0 -68
- package/src/store/sqlite.dbmaint.test.ts +0 -193
- package/src/store/sqlite.dbmirror.test.ts +0 -229
- package/src/store/turns/contract-compliance.test.ts +0 -562
- package/src/store/turns/memory-store.test.ts +0 -15
- package/src/store/turns/migrations.test.ts +0 -161
- package/src/store/turns/sqlite-store.test.ts +0 -161
- package/src/store/vectorIndex.test.ts +0 -140
- package/src/store.test.ts +0 -191
- package/src/supersede.test.ts +0 -42
- package/src/topics/cluster.test.ts +0 -176
- package/src/topics/store.test.ts +0 -132
- package/src/types.test.ts +0 -10
- package/src/vector-search-cache.test.ts +0 -190
- package/src/vectorStore.test.ts +0 -533
- package/src/wiki.test.ts +0 -217
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Captures cheap, local-only telemetry into the `perf_samples` SQLite table for
|
|
5
5
|
* the dashboard's Perf tab: turn + provider latency, TPS, cache hit %, and (via
|
|
6
|
-
* MegaRuntime.ensurePerfInterval) a 5s cpu/mem interval. All capture is wrapped
|
|
7
|
-
* try/catch — instrumentation NEVER blocks the agent loop (non-fatal).
|
|
6
|
+
* MegaRuntime.ensurePerfInterval) a 5s cpu/mem interval. All capture is wrapped
|
|
7
|
+
* in try/catch — instrumentation NEVER blocks the agent loop (non-fatal).
|
|
8
|
+
*
|
|
9
|
+
* S53A: detects significant cache_hit_pct drops (delta > 20 pp) between
|
|
10
|
+
* consecutive turns and records a `prefix_break` sample with a classified cause.
|
|
8
11
|
*
|
|
9
12
|
* PREVENT-PI-004: Date.now / process.cpuUsage / process.memoryUsage + local
|
|
10
13
|
* SQLite only, zero network.
|
|
@@ -13,6 +16,7 @@
|
|
|
13
16
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
14
17
|
import { type MegaRuntime } from "../mega-runtime.js";
|
|
15
18
|
import { recordPerfSample } from "../../src/store/sqlite.js";
|
|
19
|
+
import { classifyPrefixBreak, type EventTimestamps } from "../../src/prefix-break.js";
|
|
16
20
|
|
|
17
21
|
/** Structural view of an AssistantMessage usage block (no pi-ai import). */
|
|
18
22
|
interface UsageBlock {
|
|
@@ -30,6 +34,51 @@ function usageOf(
|
|
|
30
34
|
return msg.usage;
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Attempt to record a prefix_break sample when a significant cache_hit_pct
|
|
39
|
+
* drop is detected between two consecutive turns.
|
|
40
|
+
*
|
|
41
|
+
* Drop threshold: 20 percentage points. When triggered, calls
|
|
42
|
+
* classifyPrefixBreak with the three candidate event timestamps from the
|
|
43
|
+
* runtime and records a `prefix_break` sample. Best-effort + non-fatal.
|
|
44
|
+
*
|
|
45
|
+
* PREVENT-PI-004: guardrails-allow PREVENT-PI-004: local SQLite write only.
|
|
46
|
+
* PREVENT-002: SQL is fully parameterized via recordPerfSample.
|
|
47
|
+
*/
|
|
48
|
+
function tryRecordPrefixBreak(
|
|
49
|
+
stateDir: string,
|
|
50
|
+
prevHitPct: number,
|
|
51
|
+
currHitPct: number,
|
|
52
|
+
breakAt: number,
|
|
53
|
+
runtime: MegaRuntime,
|
|
54
|
+
): void {
|
|
55
|
+
try {
|
|
56
|
+
if (prevHitPct - currHitPct < 20) return; // no significant drop
|
|
57
|
+
|
|
58
|
+
const events: EventTimestamps = {
|
|
59
|
+
lastRecallAt: runtime.rt.lastInjectAt, // S53: inject fires alongside recall
|
|
60
|
+
lastCompactAt: runtime.rt.lastCompactAt,
|
|
61
|
+
lastInjectAt: runtime.rt.lastInjectAt,
|
|
62
|
+
};
|
|
63
|
+
const { cause, confidence } = classifyPrefixBreak(breakAt, events);
|
|
64
|
+
|
|
65
|
+
recordPerfSample(
|
|
66
|
+
stateDir,
|
|
67
|
+
"prefix_break",
|
|
68
|
+
0,
|
|
69
|
+
{
|
|
70
|
+
cause,
|
|
71
|
+
confidence,
|
|
72
|
+
prevHitPct,
|
|
73
|
+
currHitPct,
|
|
74
|
+
breakAt,
|
|
75
|
+
},
|
|
76
|
+
); // guardrails-allow PREVENT-PI-004: local SQLite write (loopback perf instrumentation)
|
|
77
|
+
} catch {
|
|
78
|
+
/* non-fatal: prefix-break detection never blocks the agent loop */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
33
82
|
/** Register perf instrumentation handlers + start the 5s cpu/mem interval. */
|
|
34
83
|
export function registerPerfHandler(
|
|
35
84
|
pi: ExtensionAPI,
|
|
@@ -51,6 +100,7 @@ export function registerPerfHandler(
|
|
|
51
100
|
|
|
52
101
|
// turn_end: compute turn latency + TPS + cache hit % from the assistant
|
|
53
102
|
// message's usage block. One perf_samples row per metric per turn.
|
|
103
|
+
// S53A: tracks prevHitPct across turns to detect prefix breaks.
|
|
54
104
|
pi.on("turn_end", async (event) => {
|
|
55
105
|
try {
|
|
56
106
|
if (runtime.perfTurnStart > 0) {
|
|
@@ -73,10 +123,34 @@ export function registerPerfHandler(
|
|
|
73
123
|
runtime.currentStateDir,
|
|
74
124
|
"cache_hit_pct",
|
|
75
125
|
hitPct,
|
|
76
|
-
{
|
|
126
|
+
{
|
|
127
|
+
input: u.input,
|
|
128
|
+
cacheRead: u.cacheRead,
|
|
129
|
+
cacheWrite: u.cacheWrite,
|
|
130
|
+
modelName: runtime.currentModel?.modelName,
|
|
131
|
+
modelId: runtime.currentModel?.modelId,
|
|
132
|
+
},
|
|
77
133
|
);
|
|
134
|
+
|
|
135
|
+
// S53A: detect significant cache hit % drop → record prefix_break.
|
|
136
|
+
// Only trigger when we have a previous value AND the drop is >20pp.
|
|
137
|
+
if (
|
|
138
|
+
runtime.rt._prevCacheHitPct != null &&
|
|
139
|
+
runtime.rt._prevCacheHitPct - hitPct >= 20
|
|
140
|
+
) {
|
|
141
|
+
const breakAt = runtime.perfTurnStart;
|
|
142
|
+
tryRecordPrefixBreak(
|
|
143
|
+
runtime.currentStateDir,
|
|
144
|
+
runtime.rt._prevCacheHitPct,
|
|
145
|
+
hitPct,
|
|
146
|
+
breakAt,
|
|
147
|
+
runtime,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
// Stamp for the next turn's comparison (even when no break fires).
|
|
151
|
+
runtime.rt._prevCacheHitPct = hitPct;
|
|
152
|
+
}
|
|
78
153
|
}
|
|
79
|
-
}
|
|
80
154
|
} catch {
|
|
81
155
|
/* non-fatal: instrumentation must never break the agent loop */
|
|
82
156
|
}
|
|
@@ -110,4 +184,4 @@ export function registerPerfHandler(
|
|
|
110
184
|
// Start the 5s cpu/mem sampling interval (one per MegaRuntime; cleared in
|
|
111
185
|
// runtime.dispose()). Idempotent — safe to call again after a dispose().
|
|
112
186
|
runtime.ensurePerfInterval();
|
|
113
|
-
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* recall-tail.ts — S53 Recall Tail Injection: append staged recall/memory blocks
|
|
3
|
+
* as a single user-role message at the very tail of the context view, instead
|
|
4
|
+
* of prepending them as part of systemPrompt (the pre-S53 behavior).
|
|
5
|
+
*
|
|
6
|
+
* Key invariants (enforced / tested):
|
|
7
|
+
* - PREVENT-PI-001/002: append-only at the very tail; never splits a
|
|
8
|
+
* toolCall/toolResult pair.
|
|
9
|
+
* - PREVENT-PI-003: user-role message, never system role.
|
|
10
|
+
* - Turn-scoped pin: staged blocks inject on EVERY context event of the turn
|
|
11
|
+
* (tool loops); consumed at turn_end ONLY if actually injected.
|
|
12
|
+
* - Non-fatal: any failure returns the unmodified messages array.
|
|
13
|
+
* - Flag OFF: no-op (returns messages unchanged — byte-identical pre-sprint;
|
|
14
|
+
* session-handlers.ts keeps the legacy systemPrompt prepend path).
|
|
15
|
+
*/
|
|
16
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
17
|
+
import type { MegaRuntime } from "../mega-runtime.js";
|
|
18
|
+
import type { MegaConfig } from "../mega-config.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* True when recall blocks are staged AND the feature flag is ON. Called by the
|
|
22
|
+
* context handler to decide whether to call withRecallTail.
|
|
23
|
+
*/
|
|
24
|
+
export function stagedForTail(
|
|
25
|
+
runtime: MegaRuntime,
|
|
26
|
+
config: MegaConfig,
|
|
27
|
+
): boolean {
|
|
28
|
+
if (!config.recallTailInject) return false;
|
|
29
|
+
return (
|
|
30
|
+
runtime.pendingRecallBlock != null ||
|
|
31
|
+
runtime.pendingMemoryRecallBlock != null
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Append staged recall/memory blocks as a single user-role message at the very
|
|
37
|
+
* tail of the messages array. Returns a NEW array (does not mutate input).
|
|
38
|
+
*
|
|
39
|
+
* Sets `runtime.recallInjectedThisTurn = true` when it actually appends, so the
|
|
40
|
+
* turn_end handler (agent-handlers.ts) consumes the staged blocks only if they
|
|
41
|
+
* reached a view this turn. If a turn ends with no context event, the flag
|
|
42
|
+
* stays false and the blocks remain staged for the next turn.
|
|
43
|
+
*
|
|
44
|
+
* Stable timestamp (runtime.perfTurnStart ?? Date.now()) so replays within the
|
|
45
|
+
* turn are byte-identical (mirrors the v0.8.6 summaryAgentMsg rationale).
|
|
46
|
+
*
|
|
47
|
+
* Non-fatal: on any failure, returns messages unmodified and does NOT set the
|
|
48
|
+
* injected flag. PREVENT-PI-003: the injected message has role "user", never
|
|
49
|
+
* "system".
|
|
50
|
+
*/
|
|
51
|
+
export function withRecallTail(
|
|
52
|
+
messages: readonly AgentMessage[],
|
|
53
|
+
runtime: MegaRuntime,
|
|
54
|
+
config: MegaConfig,
|
|
55
|
+
): AgentMessage[] {
|
|
56
|
+
if (!config.recallTailInject) return messages as AgentMessage[];
|
|
57
|
+
try {
|
|
58
|
+
const blocks: string[] = [];
|
|
59
|
+
if (runtime.pendingRecallBlock != null) {
|
|
60
|
+
blocks.push(runtime.pendingRecallBlock);
|
|
61
|
+
}
|
|
62
|
+
if (runtime.pendingMemoryRecallBlock != null) {
|
|
63
|
+
blocks.push(runtime.pendingMemoryRecallBlock);
|
|
64
|
+
}
|
|
65
|
+
if (blocks.length === 0) return messages as AgentMessage[];
|
|
66
|
+
|
|
67
|
+
const tailText = blocks.join("\n\n");
|
|
68
|
+
const timestamp =
|
|
69
|
+
runtime.perfTurnStart ?? // stable within a turn
|
|
70
|
+
Date.now();
|
|
71
|
+
|
|
72
|
+
const tailMsg = {
|
|
73
|
+
role: "user" as const,
|
|
74
|
+
content: tailText,
|
|
75
|
+
timestamp,
|
|
76
|
+
} as unknown as AgentMessage;
|
|
77
|
+
|
|
78
|
+
runtime.rt.recallInjectedThisTurn = true;
|
|
79
|
+
// Append at the very tail — never splits toolCall/toolResult.
|
|
80
|
+
// PREVENT-PI-001/002: a single message appended after a complete
|
|
81
|
+
// transcript prefix cannot split a toolCall/toolResult pair.
|
|
82
|
+
return [...messages, tailMsg];
|
|
83
|
+
} catch {
|
|
84
|
+
// Non-fatal: leave blocks staged (flag stays unset) and return the
|
|
85
|
+
// unmodified view. The context event still proceeds normally.
|
|
86
|
+
return messages as AgentMessage[];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -169,13 +169,20 @@ export function registerSessionHandlers(
|
|
|
169
169
|
} catch {
|
|
170
170
|
/* non-fatal: intent polling never breaks the agent loop */
|
|
171
171
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
172
|
+
// S53: when recallTailInject is ON, the staged blocks are injected as a
|
|
173
|
+
// user-role tail message by the context handler (recall-tail.ts), NOT
|
|
174
|
+
// here. This prepend path is the legacy behavior (flag OFF = byte-
|
|
175
|
+
// identical pre-sprint). captureModel + rewind-intent consumption above
|
|
176
|
+
// stay unconditional regardless of the flag.
|
|
177
|
+
if (!config.recallTailInject) {
|
|
178
|
+
const cpBlock = runtime.pendingRecallBlock;
|
|
179
|
+
const memBlock = runtime.pendingMemoryRecallBlock;
|
|
180
|
+
if (!cpBlock && !memBlock) return;
|
|
181
|
+
runtime.pendingRecallBlock = undefined;
|
|
182
|
+
runtime.pendingMemoryRecallBlock = undefined;
|
|
183
|
+
const composed = [cpBlock, memBlock].filter(Boolean).join("\n\n");
|
|
184
|
+
return { systemPrompt: `${event.systemPrompt}\n\n${composed}` };
|
|
185
|
+
}
|
|
179
186
|
});
|
|
180
187
|
|
|
181
188
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
@@ -37,6 +37,11 @@ export interface SessionRuntime {
|
|
|
37
37
|
dedupAttempts: number; // total compaction attempts (for hit-rate denominator)
|
|
38
38
|
tokensSaved: number; // this session-instance only: reset on session_start
|
|
39
39
|
lastCompactAt: number | null; // wall-clock ms of the last compaction this session
|
|
40
|
+
// S53A: prefix-break classification timestamps (wall-clock ms).
|
|
41
|
+
lastRecallAt: number | null; // last recall injection this session
|
|
42
|
+
lastInjectAt: number | null; // last tail injection this session
|
|
43
|
+
// S53A: previous cache_hit_pct for drop detection.
|
|
44
|
+
_prevCacheHitPct: number | null;
|
|
40
45
|
lastNativeCompactAt: number | null; // COMPACT-DEDUP FIX: wall-clock ms of the last NATIVE pi compaction (session_compact event) — used by the agent_end/legacy race guard to skip a redundant ctx.compact() that would throw "Already compacted".
|
|
41
46
|
// S25: live dashboard counters (reset on session_start, mirrored to SQLite).
|
|
42
47
|
compactCount: number; // compactions performed this session-instance
|
|
@@ -67,11 +72,20 @@ export interface SessionRuntime {
|
|
|
67
72
|
errorTextRepeatCount: number; // consecutive count of identical error signatures
|
|
68
73
|
// R3b: one-per-session /clear advise message throttle.
|
|
69
74
|
poisonedAdviseSent: boolean; // true after the advise message fires once
|
|
75
|
+
// R10: provider-outage advisory throttle (one per flapping episode).
|
|
76
|
+
providerOutageAdvised: boolean; // true after the outage advise message fires once
|
|
70
77
|
// R3c: one guarded compact per error signature (avoids re-compacting the
|
|
71
78
|
// same poisoned region repeatedly).
|
|
72
79
|
poisonedCompactSignatures: Set<string>; // signatures already attempted a compact for
|
|
80
|
+
// S53: recall tail injection state — tracks whether a tail was injected this turn.
|
|
81
|
+
recallInjectedThisTurn: boolean;
|
|
73
82
|
// R7: poisoned-context event counter for the dashboard.
|
|
74
83
|
poisonedCount: number;
|
|
84
|
+
// R13: set when the extension sends a nudge (error-retry, resume, compact)
|
|
85
|
+
// that will trigger the next turn. The turn_end handler checks this flag and
|
|
86
|
+
// skips error classification for extension-initiated turns — prevents the
|
|
87
|
+
// feedback loop where our own nudges get misclassified as errors.
|
|
88
|
+
extensionInitiatedTurn: boolean;
|
|
75
89
|
}
|
|
76
90
|
|
|
77
91
|
// ── ownVersion ─────────────────────────────────────────────────────────────
|
|
@@ -54,6 +54,9 @@ export function resetRuntimeImpl(
|
|
|
54
54
|
dedupAttempts: 0,
|
|
55
55
|
tokensSaved: 0,
|
|
56
56
|
lastCompactAt: null,
|
|
57
|
+
lastRecallAt: null,
|
|
58
|
+
lastInjectAt: null,
|
|
59
|
+
_prevCacheHitPct: null,
|
|
57
60
|
lastNativeCompactAt: null,
|
|
58
61
|
compactCount: 0,
|
|
59
62
|
recallInjections: 0,
|
|
@@ -68,8 +71,11 @@ export function resetRuntimeImpl(
|
|
|
68
71
|
lastErrorText: undefined,
|
|
69
72
|
errorTextRepeatCount: 0,
|
|
70
73
|
poisonedAdviseSent: false,
|
|
74
|
+
providerOutageAdvised: false,
|
|
71
75
|
poisonedCompactSignatures: new Set(),
|
|
76
|
+
recallInjectedThisTurn: false,
|
|
72
77
|
poisonedCount: 0,
|
|
78
|
+
extensionInitiatedTurn: false,
|
|
73
79
|
};
|
|
74
80
|
self.trimCache = null; // v0.8.6: never replay a stale trim into a new session
|
|
75
81
|
self.statusKey = undefined;
|
|
@@ -87,6 +87,9 @@ export class MegaRuntime {
|
|
|
87
87
|
dedupAttempts: 0,
|
|
88
88
|
tokensSaved: 0,
|
|
89
89
|
lastCompactAt: null,
|
|
90
|
+
lastRecallAt: null,
|
|
91
|
+
lastInjectAt: null,
|
|
92
|
+
_prevCacheHitPct: null,
|
|
90
93
|
lastNativeCompactAt: null,
|
|
91
94
|
compactCount: 0,
|
|
92
95
|
recallInjections: 0,
|
|
@@ -101,8 +104,12 @@ export class MegaRuntime {
|
|
|
101
104
|
lastErrorText: undefined,
|
|
102
105
|
errorTextRepeatCount: 0,
|
|
103
106
|
poisonedAdviseSent: false,
|
|
107
|
+
providerOutageAdvised: false,
|
|
104
108
|
poisonedCompactSignatures: new Set(),
|
|
109
|
+
// S53: tracks whether withRecallTail injected this turn (consumed at turn_end).
|
|
110
|
+
recallInjectedThisTurn: false,
|
|
105
111
|
poisonedCount: 0,
|
|
112
|
+
extensionInitiatedTurn: false,
|
|
106
113
|
};
|
|
107
114
|
// v0.8.6 cache-stability: the cached live-trim view for the current
|
|
108
115
|
// compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mega-compact",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5",
|
|
4
4
|
"description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
@@ -28,7 +28,10 @@
|
|
|
28
28
|
"extensions/dashboard-client/dist",
|
|
29
29
|
"src",
|
|
30
30
|
"README.md",
|
|
31
|
-
"LICENSE"
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"!dist/**/*.test.js",
|
|
33
|
+
"!dist/**/*.test.js.map",
|
|
34
|
+
"!**/*.test.ts"
|
|
32
35
|
],
|
|
33
36
|
"pi": {
|
|
34
37
|
"extensions": [
|
package/src/embedder.ts
CHANGED
|
@@ -59,21 +59,62 @@ function fnv1a(str: string): number {
|
|
|
59
59
|
* Default embedder: character 3-gram bag-of-counts, hashed into a fixed-dim
|
|
60
60
|
* vector, L2-normalized. Captures local lexical/structure overlap well enough
|
|
61
61
|
* for checkpoint relevance ranking.
|
|
62
|
+
*
|
|
63
|
+
* S53B: includes a 256-entry (configurable) FIFO cache keyed by FNV-1a(text).
|
|
64
|
+
* Each cache entry holds a DEFENSIVE COPY of the embedding to prevent mutation
|
|
65
|
+
* from corrupting stored vectors. Set MEGACOMPACT_EMBED_CACHE=0 to disable.
|
|
62
66
|
*/
|
|
63
67
|
export class TrigramEmbedder implements Embedder {
|
|
64
68
|
readonly dim: number;
|
|
65
69
|
private readonly seed: number;
|
|
70
|
+
/** @type {Map<string, Vector>} */
|
|
71
|
+
private readonly _cache: Map<string, Vector>;
|
|
72
|
+
private readonly _cacheSize: number;
|
|
73
|
+
private _hits = 0;
|
|
74
|
+
private _misses = 0;
|
|
66
75
|
|
|
67
76
|
constructor(dim = 512, seed = 0x9e3779b9) {
|
|
68
77
|
this.dim = dim;
|
|
69
78
|
this.seed = seed >>> 0;
|
|
79
|
+
const cap = Number(process.env.MEGACOMPACT_EMBED_CACHE ?? "256");
|
|
80
|
+
this._cacheSize = cap > 0 ? Math.floor(cap) : 0;
|
|
81
|
+
this._cache = new Map();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Cache statistics — always { hits: 0, misses: 0 } when cache is disabled. */
|
|
85
|
+
getEmbedCacheStats(): { hits: number; misses: number } {
|
|
86
|
+
return { hits: this._hits, misses: this._misses };
|
|
70
87
|
}
|
|
71
88
|
|
|
72
89
|
embed(text: string): Vector {
|
|
90
|
+
if (this._cacheSize === 0) {
|
|
91
|
+
return this._embedRaw(text);
|
|
92
|
+
}
|
|
93
|
+
const key = fnv1a(text).toString(36);
|
|
94
|
+
const cached = this._cache.get(key);
|
|
95
|
+
if (cached !== undefined) {
|
|
96
|
+
this._hits++;
|
|
97
|
+
// Defensive copy: caller mutating the returned vector must not corrupt cache.
|
|
98
|
+
return cached.slice() as Vector;
|
|
99
|
+
}
|
|
100
|
+
this._misses++;
|
|
101
|
+
const vec = this._embedRaw(text);
|
|
102
|
+
// Store a defensive copy (slice) so caller mutation cannot corrupt stored value.
|
|
103
|
+
this._cache.set(key, vec.slice() as Vector);
|
|
104
|
+
// FIFO eviction: remove the oldest entry when at capacity.
|
|
105
|
+
if (this._cache.size > this._cacheSize) {
|
|
106
|
+
// Map insertion order is guaranteed — first key is the oldest.
|
|
107
|
+
const firstKey = this._cache.keys().next().value;
|
|
108
|
+
if (firstKey !== undefined) this._cache.delete(firstKey);
|
|
109
|
+
}
|
|
110
|
+
return vec;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Raw embed computation (no cache logic). Public so tests can bypass cache. */
|
|
114
|
+
_embedRaw(text: string): Vector {
|
|
73
115
|
const vec = new Array<number>(this.dim).fill(0);
|
|
74
116
|
const norm = text.toLowerCase().replace(/\s+/g, " ");
|
|
75
117
|
if (norm.length === 0) return l2Normalize(vec);
|
|
76
|
-
// Whole-string + word + char-trigram signals.
|
|
77
118
|
vec[fnv1a(norm) % this.dim] += 1;
|
|
78
119
|
for (const word of norm.split(" ")) {
|
|
79
120
|
if (word.length === 0) continue;
|
|
@@ -84,7 +125,6 @@ export class TrigramEmbedder implements Embedder {
|
|
|
84
125
|
vec[idx] += 1;
|
|
85
126
|
}
|
|
86
127
|
}
|
|
87
|
-
// Edge: very short tokens still get a slot.
|
|
88
128
|
if (norm.length < 3) vec[fnv1a(norm) % this.dim] += 1;
|
|
89
129
|
return l2Normalize(vec);
|
|
90
130
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memoryStats.ts — aggregate memory-store statistics (S53B).
|
|
3
|
+
*
|
|
4
|
+
* Reads from the durable SQLite memories table to produce:
|
|
5
|
+
* - totalMemories: all memories in the store
|
|
6
|
+
* - memoriesInLast30Days: memories created within the last 30 days
|
|
7
|
+
* - topStableMemories: top-N memories by recall count (desc)
|
|
8
|
+
* - avgRecallScore: mean recallCount across all memories
|
|
9
|
+
*
|
|
10
|
+
* recallCount = 1 if last_recalled_at IS NOT NULL, else 0.
|
|
11
|
+
* This is the durable signal from recallMemory() — a memory that was
|
|
12
|
+
* recalled at least once is "stable" (still relevant). The exact recall
|
|
13
|
+
* frequency is tracked via turn_recall rows (source='memory') for
|
|
14
|
+
* cross-session analysis, but the memories table provides a reliable
|
|
15
|
+
* on-store signal that is always present without a schema migration.
|
|
16
|
+
*
|
|
17
|
+
* Real-store only (PREVENT-PI-004). No mocks. No network.
|
|
18
|
+
*/
|
|
19
|
+
import { openStore } from "./store/sqlite/utils.js";
|
|
20
|
+
|
|
21
|
+
export interface MemoryStatsResult {
|
|
22
|
+
totalMemories: number;
|
|
23
|
+
memoriesInLast30Days: number;
|
|
24
|
+
topStableMemories: Array<{
|
|
25
|
+
id: number;
|
|
26
|
+
text: string;
|
|
27
|
+
recallCount: number;
|
|
28
|
+
lastRecalledAt: number | null;
|
|
29
|
+
}>;
|
|
30
|
+
avgRecallScore: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface MemoryStatsOptions {
|
|
34
|
+
/** Max memories in topStableMemories (default 5). */
|
|
35
|
+
topN?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Aggregate memory statistics from the SQLite store.
|
|
40
|
+
* Always returns a valid MemoryStatsResult; never throws.
|
|
41
|
+
*/
|
|
42
|
+
export async function memoryStats(
|
|
43
|
+
stateDir: string,
|
|
44
|
+
opts: MemoryStatsOptions = {},
|
|
45
|
+
): Promise<MemoryStatsResult> {
|
|
46
|
+
const topN = opts.topN ?? 5;
|
|
47
|
+
try {
|
|
48
|
+
const db = openStore(stateDir);
|
|
49
|
+
// ── totalMemories ──────────────────────────────────────────────────────
|
|
50
|
+
const totalRow = db
|
|
51
|
+
.prepare("SELECT COUNT(*) AS n FROM memories")
|
|
52
|
+
.get() as { n: number };
|
|
53
|
+
const totalMemories = totalRow.n;
|
|
54
|
+
|
|
55
|
+
// ── memoriesInLast30Days ───────────────────────────────────────────────
|
|
56
|
+
const thirtyDaysAgo = Math.floor(Date.now() / 1000) - 30 * 86_400;
|
|
57
|
+
const recentRow = db
|
|
58
|
+
.prepare(
|
|
59
|
+
"SELECT COUNT(*) AS n FROM memories WHERE created_at >= ?",
|
|
60
|
+
)
|
|
61
|
+
.get(thirtyDaysAgo) as { n: number };
|
|
62
|
+
const memoriesInLast30Days = recentRow.n;
|
|
63
|
+
|
|
64
|
+
// ── topStableMemories + avgRecallScore ─────────────────────────────────
|
|
65
|
+
// recallCount: 1 if last_recalled_at IS NOT NULL (recalled at least once),
|
|
66
|
+
// 0 otherwise. Stable memories score high.
|
|
67
|
+
const topRows = db
|
|
68
|
+
.prepare(
|
|
69
|
+
`SELECT
|
|
70
|
+
m.id,
|
|
71
|
+
m.content AS text,
|
|
72
|
+
m.last_recalled_at AS lastRecalledAt,
|
|
73
|
+
CASE WHEN m.last_recalled_at IS NOT NULL THEN 1 ELSE 0 END AS recallCount
|
|
74
|
+
FROM memories m
|
|
75
|
+
ORDER BY recallCount DESC, m.id ASC
|
|
76
|
+
LIMIT ?`,
|
|
77
|
+
)
|
|
78
|
+
.all(topN) as Array<Record<string, unknown>>;
|
|
79
|
+
|
|
80
|
+
// avgRecallScore = (count of recalled memories) / totalMemories.
|
|
81
|
+
// This represents the fraction of memories that are "stable" (recalled at least once).
|
|
82
|
+
const recalledRow = db
|
|
83
|
+
.prepare(
|
|
84
|
+
"SELECT COUNT(*) AS n FROM memories WHERE last_recalled_at IS NOT NULL",
|
|
85
|
+
)
|
|
86
|
+
.get() as { n: number };
|
|
87
|
+
const avgRecallScore =
|
|
88
|
+
totalMemories > 0 ? recalledRow.n / totalMemories : 0;
|
|
89
|
+
|
|
90
|
+
const topStableMemories = topRows.map((r) => ({
|
|
91
|
+
id: r.id as number,
|
|
92
|
+
text: (r.text as string) ?? "",
|
|
93
|
+
recallCount: (r.recallCount as number) ?? 0,
|
|
94
|
+
lastRecalledAt: (r.lastRecalledAt as number | null) ?? null,
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
return { totalMemories, memoriesInLast30Days, topStableMemories, avgRecallScore };
|
|
98
|
+
} catch {
|
|
99
|
+
// Non-fatal: return zeros rather than propagating an error.
|
|
100
|
+
return {
|
|
101
|
+
totalMemories: 0,
|
|
102
|
+
memoriesInLast30Days: 0,
|
|
103
|
+
topStableMemories: [],
|
|
104
|
+
avgRecallScore: 0,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* prefix-break.ts — S53A prefix-break classification.
|
|
3
|
+
*
|
|
4
|
+
* Classifies WHY the provider cache prefix broke on a given turn by comparing
|
|
5
|
+
* the break timestamp against the three candidate event timestamps with a
|
|
6
|
+
* configurable tolerance window.
|
|
7
|
+
*
|
|
8
|
+
* The classification is best-effort (non-fatal): null timestamps are absent,
|
|
9
|
+
* and a break outside all tolerance windows is classified as 'other'.
|
|
10
|
+
*
|
|
11
|
+
* Pi-agnostic: no pi runtime types, no network.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface PrefixBreakMeta {
|
|
15
|
+
/** Which event caused the break. */
|
|
16
|
+
cause: "recall" | "compaction" | "inject" | "other";
|
|
17
|
+
/** Confidence score 0.0–1.0 based on proximity to the matched event. */
|
|
18
|
+
confidence: number;
|
|
19
|
+
/** Cache hit % immediately before the break. */
|
|
20
|
+
prevHitPct: number;
|
|
21
|
+
/** Cache hit % of the first degraded sample. */
|
|
22
|
+
currHitPct: number;
|
|
23
|
+
/** Wall-clock ms of the break sample. */
|
|
24
|
+
breakAt: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PrefixBreak {
|
|
28
|
+
id: number;
|
|
29
|
+
ts: number;
|
|
30
|
+
kind: "prefix_break";
|
|
31
|
+
value: number;
|
|
32
|
+
meta: PrefixBreakMeta;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Timestamps of recent events that can cause a prefix break.
|
|
37
|
+
* All values are wall-clock ms (Date.now()) or null if no event has occurred.
|
|
38
|
+
*/
|
|
39
|
+
export interface EventTimestamps {
|
|
40
|
+
lastRecallAt: number | null;
|
|
41
|
+
lastCompactAt: number | null;
|
|
42
|
+
lastInjectAt: number | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Classify why the provider cache prefix broke at `breakTimestamp`.
|
|
47
|
+
*
|
|
48
|
+
* @param breakTimestamp Wall-clock ms of the cache hit % drop sample.
|
|
49
|
+
* @param events Recent event timestamps from the runtime.
|
|
50
|
+
* @param toleranceMs Max ms gap to consider a match (default 2000).
|
|
51
|
+
* @returns Classification result with cause + confidence.
|
|
52
|
+
*
|
|
53
|
+
* Priority: recall > compaction > inject. Confidence = 1 − (gap / (2·tolerance)),
|
|
54
|
+
* capped at [0.5, 1.0]. Returns 'other' when no event is within tolerance.
|
|
55
|
+
*/
|
|
56
|
+
export function classifyPrefixBreak(
|
|
57
|
+
breakTimestamp: number,
|
|
58
|
+
events: EventTimestamps,
|
|
59
|
+
toleranceMs = 2000,
|
|
60
|
+
): { cause: "recall" | "compaction" | "inject" | "other"; confidence: number } {
|
|
61
|
+
// Ordered by priority (recall wins if multiple match).
|
|
62
|
+
const candidates: Array<{
|
|
63
|
+
cause: "recall" | "compaction" | "inject";
|
|
64
|
+
ts: number | null;
|
|
65
|
+
}> = [
|
|
66
|
+
{ cause: "recall", ts: events.lastRecallAt },
|
|
67
|
+
{ cause: "compaction", ts: events.lastCompactAt },
|
|
68
|
+
{ cause: "inject", ts: events.lastInjectAt },
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
for (const { cause, ts } of candidates) {
|
|
72
|
+
if (ts == null) continue;
|
|
73
|
+
const gap = Math.abs(breakTimestamp - ts);
|
|
74
|
+
if (gap <= toleranceMs) {
|
|
75
|
+
// confidence = 1 − (gap / (2·tolerance)) ∈ [0.5, 1.0]
|
|
76
|
+
// exact match (gap=0) → 1.0; at boundary (gap=tolerance) → 0.5
|
|
77
|
+
const confidence = Math.max(0.5, 1 - gap / (2 * toleranceMs));
|
|
78
|
+
return { cause, confidence };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { cause: "other", confidence: 0 };
|
|
83
|
+
}
|
package/src/pricing.ts
CHANGED
|
@@ -39,7 +39,7 @@ export const MODEL_INPUT_RATES: Record<string, number> = {
|
|
|
39
39
|
"gemini-2.0-flash": 0.1e-6,
|
|
40
40
|
"deepseek-chat": 0.14e-6,
|
|
41
41
|
"deepseek-reasoner": 0.55e-6,
|
|
42
|
-
|
|
42
|
+
codestral: 0.3e-6,
|
|
43
43
|
"mistral-large": 2e-6,
|
|
44
44
|
"llama-3.1-405b": 1.33e-6,
|
|
45
45
|
"llama-3.1-70b": 0.8e-6,
|
|
@@ -92,4 +92,4 @@ export function computeCacheSavings(
|
|
|
92
92
|
cacheWriteCost,
|
|
93
93
|
netSaved: cacheReadSaved - cacheWriteCost,
|
|
94
94
|
};
|
|
95
|
-
}
|
|
95
|
+
}
|