pi-mega-compact 0.11.1 → 0.11.3

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 (109) hide show
  1. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +28 -1
  2. package/dist/extensions/dashboard-server/api-contracts/maintenance.js +7 -0
  3. package/dist/extensions/dashboard-server/api-contracts/provider-cache.js +7 -0
  4. package/dist/extensions/dashboard-server/api-contracts.test.js +3 -2
  5. package/dist/extensions/dashboard-server/index-reader.js +22 -0
  6. package/dist/extensions/dashboard-server/routes-cache.js +80 -0
  7. package/dist/extensions/dashboard-server/routes-cache.test.js +248 -0
  8. package/dist/extensions/dashboard-server/routes-maintenance.js +293 -0
  9. package/dist/extensions/dashboard-server/routes-repo.js +24 -0
  10. package/dist/extensions/dashboard-server/routes.js +2 -0
  11. package/dist/extensions/dashboard-server/server.js +5 -1
  12. package/dist/extensions/mega-cache-replay.test.js +233 -24
  13. package/dist/extensions/mega-compact-s38.test.js +201 -6
  14. package/dist/extensions/mega-config.js +3 -0
  15. package/dist/extensions/mega-events/agent-handlers.js +23 -7
  16. package/dist/extensions/mega-events/context-handler.js +37 -19
  17. package/dist/extensions/mega-events/error-classifier.js +27 -16
  18. package/dist/extensions/mega-runtime/runtime-snapshot.js +14 -4
  19. package/dist/extensions/mega-runtime/snapshot.js +1 -1
  20. package/dist/extensions/mega-runtime/snapshot.test.js +127 -0
  21. package/dist/extensions/mega-runtime/widget.js +4 -4
  22. package/dist/extensions/mega-runtime/widget.test.js +103 -12
  23. package/dist/src/pricing.js +78 -0
  24. package/dist/src/store/sqlite/maintenance.js +20 -1
  25. package/dist/src/store/sqlite/perf-samples.js +104 -0
  26. package/dist/src/store/sqlite/perf-samples.test.js +93 -1
  27. package/extensions/dashboard-client/dist/assets/{AchievementTiles-DZp_xQOW.js → AchievementTiles-vEo2MtE2.js} +2 -2
  28. package/extensions/dashboard-client/dist/assets/{AchievementTiles-DZp_xQOW.js.map → AchievementTiles-vEo2MtE2.js.map} +1 -1
  29. package/extensions/dashboard-client/dist/assets/AchievementsTab-DjxfYrDc.js +2 -0
  30. package/extensions/dashboard-client/dist/assets/{AchievementsTab-BzFHpAHM.js.map → AchievementsTab-DjxfYrDc.js.map} +1 -1
  31. package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js +2 -0
  32. package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js.map +1 -0
  33. package/extensions/dashboard-client/dist/assets/{ConfigTab-DQ4FG33J.js → ConfigTab-Cm6ymbmx.js} +2 -2
  34. package/extensions/dashboard-client/dist/assets/{ConfigTab-DQ4FG33J.js.map → ConfigTab-Cm6ymbmx.js.map} +1 -1
  35. package/extensions/dashboard-client/dist/assets/{EventsTab-D5HoEVYo.js → EventsTab-DXWl_YzI.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{EventsTab-D5HoEVYo.js.map → EventsTab-DXWl_YzI.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/{GameTab-Jzsc4yeY.js → GameTab-BLDudcO5.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{GameTab-Jzsc4yeY.js.map → GameTab-BLDudcO5.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/MaintenanceTab-DLkKCztn.js +2 -0
  40. package/extensions/dashboard-client/dist/assets/MaintenanceTab-DLkKCztn.js.map +1 -0
  41. package/extensions/dashboard-client/dist/assets/{MetricsTab-D_XnsEYX.js → MetricsTab-Cq4u-rZF.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{MetricsTab-D_XnsEYX.js.map → MetricsTab-Cq4u-rZF.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/{OverviewTab-BMREvAth.js → OverviewTab-D4kGvsiQ.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{OverviewTab-BMREvAth.js.map → OverviewTab-D4kGvsiQ.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js +2 -0
  46. package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js.map +1 -0
  47. package/extensions/dashboard-client/dist/assets/{SessionsTab-mz_vz8i2.js → SessionsTab-DZC2-GCn.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{SessionsTab-mz_vz8i2.js.map → SessionsTab-DZC2-GCn.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js +2 -0
  50. package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js.map +1 -0
  51. package/extensions/dashboard-client/dist/assets/{TopicsTab-DKuct4oD.js → TopicsTab-01G71hv1.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{TopicsTab-DKuct4oD.js.map → TopicsTab-01G71hv1.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{TurnsTab-11MGLxY8.js → TurnsTab-DLRg0LZT.js} +2 -2
  54. package/extensions/dashboard-client/dist/assets/{TurnsTab-11MGLxY8.js.map → TurnsTab-DLRg0LZT.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/index-BEdKlpAB.css +1 -0
  56. package/extensions/dashboard-client/dist/assets/{index-CqLN7STl.js → index-BX0bcgLu.js} +10 -10
  57. package/extensions/dashboard-client/dist/assets/{index-CqLN7STl.js.map → index-BX0bcgLu.js.map} +1 -1
  58. package/extensions/dashboard-client/dist/assets/{useSSE-CXrQSbVY.js → useSSE-BBYt5UPB.js} +2 -2
  59. package/extensions/dashboard-client/dist/assets/{useSSE-CXrQSbVY.js.map → useSSE-BBYt5UPB.js.map} +1 -1
  60. package/extensions/dashboard-client/dist/index.html +2 -2
  61. package/extensions/dashboard-client/src/App.tsx +5 -1
  62. package/extensions/dashboard-client/src/api/client.ts +29 -0
  63. package/extensions/dashboard-client/src/components/ActiveReposTable.tsx +13 -1
  64. package/extensions/dashboard-client/src/components/ProviderCacheCard.tsx +141 -0
  65. package/extensions/dashboard-client/src/components/SavingsByModelTable.tsx +31 -3
  66. package/extensions/dashboard-client/src/main.tsx +1 -0
  67. package/extensions/dashboard-client/src/styles/maintenance.css +267 -0
  68. package/extensions/dashboard-client/src/styles/overview-extra.css +9 -0
  69. package/extensions/dashboard-client/src/tabs/CacheTab.tsx +83 -29
  70. package/extensions/dashboard-client/src/tabs/MaintenanceTab.tsx +458 -0
  71. package/extensions/dashboard-client/src/utils/format.ts +42 -7
  72. package/extensions/dashboard-server/api-contracts/endpoints.ts +54 -1
  73. package/extensions/dashboard-server/api-contracts/index.ts +16 -0
  74. package/extensions/dashboard-server/api-contracts/maintenance.ts +84 -0
  75. package/extensions/dashboard-server/api-contracts/multi-repo.ts +6 -0
  76. package/extensions/dashboard-server/api-contracts/provider-cache.ts +30 -0
  77. package/extensions/dashboard-server/api-contracts.test.ts +3 -2
  78. package/extensions/dashboard-server/api-contracts.ts +9 -0
  79. package/extensions/dashboard-server/index-reader.ts +23 -0
  80. package/extensions/dashboard-server/routes-cache.test.ts +302 -0
  81. package/extensions/dashboard-server/routes-cache.ts +123 -0
  82. package/extensions/dashboard-server/routes-maintenance.ts +352 -0
  83. package/extensions/dashboard-server/routes-repo.ts +31 -4
  84. package/extensions/dashboard-server/routes.ts +2 -0
  85. package/extensions/dashboard-server/server.ts +4 -0
  86. package/extensions/mega-cache-replay.test.ts +481 -166
  87. package/extensions/mega-compact-s38.test.ts +210 -6
  88. package/extensions/mega-config.ts +6 -0
  89. package/extensions/mega-events/agent-handlers.ts +24 -6
  90. package/extensions/mega-events/context-handler.ts +59 -24
  91. package/extensions/mega-events/error-classifier.ts +30 -13
  92. package/extensions/mega-runtime/runtime-snapshot.ts +31 -9
  93. package/extensions/mega-runtime/snapshot.test.ts +139 -0
  94. package/extensions/mega-runtime/snapshot.ts +6 -4
  95. package/extensions/mega-runtime/widget.test.ts +290 -105
  96. package/extensions/mega-runtime/widget.ts +29 -6
  97. package/package.json +1 -1
  98. package/src/pricing.ts +95 -0
  99. package/src/store/sqlite/maintenance.ts +20 -1
  100. package/src/store/sqlite/perf-samples.test.ts +107 -3
  101. package/src/store/sqlite/perf-samples.ts +142 -6
  102. package/extensions/dashboard-client/dist/assets/AchievementsTab-BzFHpAHM.js +0 -2
  103. package/extensions/dashboard-client/dist/assets/CacheTab-DEQbTq2T.js +0 -2
  104. package/extensions/dashboard-client/dist/assets/CacheTab-DEQbTq2T.js.map +0 -1
  105. package/extensions/dashboard-client/dist/assets/ReposTab-DuyhOktG.js +0 -2
  106. package/extensions/dashboard-client/dist/assets/ReposTab-DuyhOktG.js.map +0 -1
  107. package/extensions/dashboard-client/dist/assets/TimeSavedCard-B8AIjNTY.js +0 -2
  108. package/extensions/dashboard-client/dist/assets/TimeSavedCard-B8AIjNTY.js.map +0 -1
  109. package/extensions/dashboard-client/dist/assets/index-gn66lYD2.css +0 -1
@@ -576,9 +576,11 @@ test("R6(e): success resets retry-nudge-pending state", async () => {
576
576
  });
577
577
  test("R3: repeated identical transient error text upgrades to poisoned-context at threshold", async () => {
578
578
  // The stateful repeat signal: 3 consecutive identical transient errors
579
- // (default threshold) upgrade to poisoned. Uses "5xx server error" (5xx
580
- // marker transient) so the classifier returns transient, then the repeat
581
- // tracker upgrades it. auto=false to skip the compact attempt.
579
+ // (default threshold) upgrade to poisoned. Uses "5xx server error"
580
+ // deliberately NO known-retryable marker ("5xx" is not /5\d\d/, and
581
+ // "server error" alone matches nothing): the classifier returns transient
582
+ // via the generic 'error' fallthrough, then the repeat tracker upgrades it.
583
+ // auto=false to skip the compact attempt.
582
584
  const prevAuto = process.env.MEGACOMPACT_AUTO;
583
585
  const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
584
586
  process.env.MEGACOMPACT_AUTO = "false";
@@ -608,6 +610,195 @@ test("R3: repeated identical transient error text upgrades to poisoned-context a
608
610
  process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
609
611
  }
610
612
  });
613
+ // ---- R7 regression tests: network/throughput errors must NEVER upgrade to ----
614
+ // ---- poisoned-context (2026-07-30 false-alarm incident) ----
615
+ /** R7 helper: fire the same transient error text `count` times with turn_start
616
+ * + elapsed backoff between turns so each turn's nudge can fire. */
617
+ async function r7RepeatTurns(h, text, count) {
618
+ for (let i = 0; i < count; i++) {
619
+ await s38TurnEnd(h, "error", text);
620
+ await h.fire("turn_start", { type: "turn_start", turnIndex: i + 2 }, h.ctx());
621
+ await new Promise((r) => setTimeout(r, 3));
622
+ }
623
+ }
624
+ /** R7 helper: shared assertions for "stays transient" — no poisoned upgrade,
625
+ * no /clear advise, and the transient retry path actually ran. */
626
+ function assertStaysTransient(h, label) {
627
+ assert.ok(!eventTypes(h.stateDir).includes("poisoned_context"), `${label}: no poisoned_context event (network errors never upgrade)`);
628
+ assert.ok(!h.sendUserMessages.some((m) => m.includes("/clear")), `${label}: no /clear advise message`);
629
+ assert.ok(eventTypes(h.stateDir).includes("error_retry"), `${label}: transient retry path ran (error_retry event)`);
630
+ }
631
+ test("R7(a): repeated 'timed out' phrasing (2026-07-30 incident text) stays transient — no poisoned upgrade", async () => {
632
+ // The incident error text was "Request timed out or failed. Try again" —
633
+ // "timed out" (two words) does NOT match the old guard's /timeout/, so the
634
+ // 3rd repeat fired the /clear poisoned advise. Must stay transient.
635
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
636
+ const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
637
+ process.env.MEGACOMPACT_AUTO = "false";
638
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = "1";
639
+ try {
640
+ const h = harness();
641
+ await r7RepeatTurns(h, "Request timed out or failed. Try again", 3);
642
+ assertStaysTransient(h, "timed out x3");
643
+ }
644
+ finally {
645
+ if (prevAuto === undefined)
646
+ delete process.env.MEGACOMPACT_AUTO;
647
+ else
648
+ process.env.MEGACOMPACT_AUTO = prevAuto;
649
+ if (prevBackoff === undefined)
650
+ delete process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
651
+ else
652
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
653
+ }
654
+ });
655
+ test("R7(b): repeated ETIMEDOUT errno stays transient", async () => {
656
+ // Node's timeout errno lowercases to "etimedout" — does NOT contain the
657
+ // substring "timeout". Slipped through the old guard.
658
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
659
+ const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
660
+ process.env.MEGACOMPACT_AUTO = "false";
661
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = "1";
662
+ try {
663
+ const h = harness();
664
+ await r7RepeatTurns(h, "connect ETIMEDOUT 142.250.80.46:443", 3);
665
+ assertStaysTransient(h, "ETIMEDOUT x3");
666
+ }
667
+ finally {
668
+ if (prevAuto === undefined)
669
+ delete process.env.MEGACOMPACT_AUTO;
670
+ else
671
+ process.env.MEGACOMPACT_AUTO = prevAuto;
672
+ if (prevBackoff === undefined)
673
+ delete process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
674
+ else
675
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
676
+ }
677
+ });
678
+ test("R7(c): repeated 'socket hang up' stays transient", async () => {
679
+ // Node surfaces ECONNRESET as the message "socket hang up" (the errno lives
680
+ // in error.code, which extractErrorSignature never sees).
681
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
682
+ const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
683
+ process.env.MEGACOMPACT_AUTO = "false";
684
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = "1";
685
+ try {
686
+ const h = harness();
687
+ await r7RepeatTurns(h, "socket hang up", 3);
688
+ assertStaysTransient(h, "socket hang up x3");
689
+ }
690
+ finally {
691
+ if (prevAuto === undefined)
692
+ delete process.env.MEGACOMPACT_AUTO;
693
+ else
694
+ process.env.MEGACOMPACT_AUTO = prevAuto;
695
+ if (prevBackoff === undefined)
696
+ delete process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
697
+ else
698
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
699
+ }
700
+ });
701
+ test("R7(d): repeated 429 rate-limit stays transient", async () => {
702
+ // /clear cannot fix a rate limit — the classifier explicitly keeps 429
703
+ // transient, so the repeat-upgrade must not override that verdict.
704
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
705
+ const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
706
+ process.env.MEGACOMPACT_AUTO = "false";
707
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = "1";
708
+ try {
709
+ const h = harness();
710
+ await r7RepeatTurns(h, "429 Too Many Requests: rate limit exceeded", 3);
711
+ assertStaysTransient(h, "429 x3");
712
+ }
713
+ finally {
714
+ if (prevAuto === undefined)
715
+ delete process.env.MEGACOMPACT_AUTO;
716
+ else
717
+ process.env.MEGACOMPACT_AUTO = prevAuto;
718
+ if (prevBackoff === undefined)
719
+ delete process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
720
+ else
721
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
722
+ }
723
+ });
724
+ test("R7(e): 0-token 'timed out' turn is NOT poisoned on first occurrence", async () => {
725
+ // Deeper gap: with usage PRESENT at 0 tokens, the classifier's 0-token
726
+ // poisoned signal fires on turn ONE for "timed out" phrasing (the stopReason
727
+ // 'error' is in the text blob and no transient marker matched). The network
728
+ // markers must be checked before that signal so this returns transient.
729
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
730
+ process.env.MEGACOMPACT_AUTO = "false";
731
+ try {
732
+ const h = harness();
733
+ await s38TurnEndUsage(h, "error", "Request timed out or failed. Try again", 0);
734
+ assert.ok(!eventTypes(h.stateDir).includes("poisoned_context"), "0-token timed out: no poisoned_context on first turn");
735
+ assert.ok(!h.sendUserMessages.some((m) => m.includes("/clear")), "0-token timed out: no /clear advise on first turn");
736
+ }
737
+ finally {
738
+ if (prevAuto === undefined)
739
+ delete process.env.MEGACOMPACT_AUTO;
740
+ else
741
+ process.env.MEGACOMPACT_AUTO = prevAuto;
742
+ }
743
+ });
744
+ test("R7(f): repeated non-network transient still upgrades to poisoned-context (control)", async () => {
745
+ // True-positive control: an error with NO known-retryable marker that
746
+ // repeats identically must still upgrade (the R3 feature itself).
747
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
748
+ const prevBackoff = process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
749
+ process.env.MEGACOMPACT_AUTO = "false";
750
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = "1";
751
+ try {
752
+ const h = harness();
753
+ await r7RepeatTurns(h, "upstream rejected the request", 3);
754
+ assert.ok(eventTypes(h.stateDir).includes("poisoned_context"), "non-network repeat: poisoned_context event logged");
755
+ assert.ok(h.sendUserMessages.some((m) => m.includes("/clear")), "non-network repeat: /clear advise fired");
756
+ }
757
+ finally {
758
+ if (prevAuto === undefined)
759
+ delete process.env.MEGACOMPACT_AUTO;
760
+ else
761
+ process.env.MEGACOMPACT_AUTO = prevAuto;
762
+ if (prevBackoff === undefined)
763
+ delete process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS;
764
+ else
765
+ process.env.MEGACOMPACT_ERROR_RETRY_BACKOFF_MS = prevBackoff;
766
+ }
767
+ });
768
+ test("R7(g): 'Request failed — please retry.' 0-token stays poisoned-context (2026-07-28 control)", async () => {
769
+ // The deterministic-rejection incident that motivated R3 must remain
770
+ // poisoned: the shared marker pattern must NOT match this text.
771
+ const prevAuto = process.env.MEGACOMPACT_AUTO;
772
+ process.env.MEGACOMPACT_AUTO = "false";
773
+ try {
774
+ const h = harness();
775
+ await s38TurnEndUsage(h, "error", "Request failed — please retry.", 0);
776
+ assert.ok(eventTypes(h.stateDir).includes("poisoned_context"), "deterministic rejection: poisoned_context event logged");
777
+ assert.ok(h.sendUserMessages.some((m) => m.includes("/clear")), "deterministic rejection: /clear advise fired");
778
+ }
779
+ finally {
780
+ if (prevAuto === undefined)
781
+ delete process.env.MEGACOMPACT_AUTO;
782
+ else
783
+ process.env.MEGACOMPACT_AUTO = prevAuto;
784
+ }
785
+ });
786
+ test("R7 classifier: 0-token 'timed out' error → transient (not poisoned)", () => {
787
+ // Unit-level pin of the R7(e) gap.
788
+ assert.equal(classifyErrorFn({
789
+ stopReason: "error",
790
+ content: "Request timed out or failed. Try again",
791
+ usage: { inputTokens: 0, outputTokens: 0 },
792
+ }), "transient");
793
+ });
794
+ test("R7 classifier: bare network phrasings → transient", () => {
795
+ assert.equal(classifyErrorFn("socket hang up"), "transient");
796
+ assert.equal(classifyErrorFn("the operation timed out"), "transient");
797
+ assert.equal(classifyErrorFn("connect ETIMEDOUT 10.0.0.1:443"), "transient");
798
+ });
799
+ test("R7 classifier: 429/rate-limit → transient (control)", () => {
800
+ assert.equal(classifyErrorFn("429 Too Many Requests: rate limit exceeded"), "transient");
801
+ });
611
802
  test("cleanup", async () => {
612
803
  // PGlite WASM close can hang; race with a timeout to prevent 40-min hangs.
613
804
  try {
@@ -618,8 +809,12 @@ test("cleanup", async () => {
618
809
  // Force-exit: each harness() creates a MegaRuntime with an fs.watch
619
810
  // game-state watcher that is never disposed (no session_shutdown in tests).
620
811
  // Those handles keep the event loop alive indefinitely after all tests
621
- // complete, so `node --test` (without --test-force-exit) would hang. The
622
- // streaming reporter has already printed every test result by this point;
623
- // process.exit(0) just forces the exit the watchers are preventing.
812
+ // complete, so `node --test` (without --test-force-exit) would hang.
813
+ // Drain stdout/stderr, then defer the exit: a bare process.exit() discards
814
+ // unflushed pipe buffers observed 2026-07-30: when piped, the trailing
815
+ // tests' results and the run summary silently vanished from the report.
816
+ await new Promise((r) => process.stdout.write("", r));
817
+ await new Promise((r) => process.stderr.write("", r));
818
+ await new Promise((r) => setTimeout(r, 1500));
624
819
  process.exit(0);
625
820
  });
@@ -165,6 +165,9 @@ export function loadConfig() {
165
165
  recallMaxTokens: envFlag("MEGACOMPACT_RECALL_MAX_TOKENS", 1500),
166
166
  windowDedupe: envBool("MEGACOMPACT_WINDOW_DEDUPE", true),
167
167
  tuiWidget: envBool("MEGACOMPACT_TUI_WIDGET", true),
168
+ // D.1: env-overridable recompact delta (minimum context growth % before
169
+ // re-compacting instead of replaying the cached live trim). Default 50.
170
+ recompactPctDelta: envFlag("MEGACOMPACT_RECOMPACT_PCT_DELTA", 50),
168
171
  debug: envBool("MEGACOMPACT_DEBUG", false),
169
172
  };
170
173
  }
@@ -1,11 +1,11 @@
1
1
  import { piCompactWouldNoop, runMemoryReview } from "../mega-pipeline.js";
2
2
  import { memoryReviewCadence } from "../mega-config.js";
3
- import { recordScore } from "../../src/store/sqlite.js";
3
+ import { recordScore, readLatestCacheHitPct } from "../../src/store/sqlite.js";
4
4
  import { evaluateAndUnlockAchievements } from "../../src/store/sqlite/game-achievements.js";
5
5
  import { ensureConversationIdFor, recordTurnWrite, } from "../mega-turn-store.js";
6
6
  import { isMegaCache } from "../../src/game/scoring.js";
7
7
  import { resolveRepoRoot } from "../mega-config.js";
8
- import { classifyError, errorRetryBackoffMs, extractErrorSignature, } from "./error-classifier.js";
8
+ import { classifyError, errorRetryBackoffMs, extractErrorSignature, isKnownRetryableTransient, } from "./error-classifier.js";
9
9
  import { safeSendUserMessage } from "./send-safe.js";
10
10
  import { vectorStats } from "../../src/vectorStore.js";
11
11
  /** Register agent/turn tracking event handlers. */
@@ -255,7 +255,9 @@ export function registerAgentHandlers(pi, runtime, config) {
255
255
  if (runtime.getCachedGameState().game_mode_on) {
256
256
  const repo = resolveRepoRoot(ctx.cwd) ?? runtime.currentStateDir;
257
257
  const st = vectorStats(runtime.store, runtime.rt.sessionId);
258
- const cachePct = st.dedupHitRate * 100;
258
+ // C.3: prefer provider cache hit rate, fall back to dedup hit rate
259
+ const providerPct = readLatestCacheHitPct(runtime.currentStateDir);
260
+ const cachePct = providerPct != null ? providerPct : st.dedupHitRate * 100;
259
261
  const modelId = runtime.currentModel?.modelId ?? "unknown";
260
262
  recordScore(runtime.currentStateDir, {
261
263
  repo_root: repo,
@@ -339,9 +341,11 @@ export function registerAgentHandlers(pi, runtime, config) {
339
341
  // upgrade happens here. A 'transient' (or 'poisoned-context') turn
340
342
  // whose normalized error signature matches the previous one bumps
341
343
  // errorTextRepeatCount; once it crosses the threshold, the category
342
- // is upgraded to 'poisoned-context' (the request is deterministic).
343
- // Conservative: only upgrade, never downgrade, and only for transient
344
- // (a 'permanent' auth error repeating is still permanent).
344
+ // is upgraded to 'poisoned-context' (the request is deterministic)
345
+ // R7: unless the signature carries a known-retryable network/
346
+ // throughput marker (guard below). Conservative: only upgrade, never
347
+ // downgrade, and only for transient (a 'permanent' auth error
348
+ // repeating is still permanent).
345
349
  let effectiveCategory = category;
346
350
  const errSig = extractErrorSignature(event.message);
347
351
  if (category === "transient" || category === "poisoned-context") {
@@ -356,7 +360,19 @@ export function registerAgentHandlers(pi, runtime, config) {
356
360
  if (runtime.rt.errorTextRepeatCount >=
357
361
  config.poisonedContextRepeatThreshold &&
358
362
  category === "transient") {
359
- effectiveCategory = "poisoned-context";
363
+ // R3/R7: upgrade only when the repeating error carries NO
364
+ // known-retryable marker. Network/throughput failures (timeout,
365
+ // ECONNRESET, 5xx, 429, connection lost, …) that repeat are
366
+ // still retryable — the API is just flaky, and /clear cannot fix
367
+ // them (2026-07-30 false-alarm incident). Only upgrade
368
+ // transient → poisoned-context for errors that repeat identically
369
+ // with no known-retryable cause, like the 2026-07-28 incident
370
+ // where the provider returned "Request failed — please retry."
371
+ // deterministically. The marker set is shared with classifyError
372
+ // (error-classifier.ts) so the two never drift.
373
+ if (!isKnownRetryableTransient(errSig)) {
374
+ effectiveCategory = "poisoned-context";
375
+ }
360
376
  }
361
377
  }
362
378
  }
@@ -170,30 +170,24 @@ export function registerContextHandler(pi, runtime, config) {
170
170
  runtime.diagCtxFastGate++;
171
171
  return;
172
172
  }
173
- // Debounce so we don't fire on every context event past threshold.
174
- const now = Date.now();
175
- if (now < runtime.debounceUntil) {
176
- runtime.diagCtxDebounce++;
177
- return;
178
- }
179
- runtime.debounceUntil = now + 2000;
173
+ // D.2: Replay MUST be exempt from debounce replay is free (no compute,
174
+ // no re-write) and prevents unnecessary KV-cache invalidation. Check
175
+ // replay FIRST, before debounce, so two context events <2s apart both
176
+ // return the cached sentinel verbatim (re-stabilises the provider prefix).
177
+ //
180
178
  // v0.8.6 cache-stability: replay the cached trim view when still in the
181
179
  // same compaction epoch AND context hasn't grown enough to warrant a
182
- // re-compact. This stabilizes the provider KV-cache prefix (the summary +
183
- // cut are reused verbatim) instead of regenerating a fresh summary +
184
- // sentinel every fire, which invalidated the prefix on every other turn
185
- // (the alternating cache-miss regression). Re-compact only when context
186
- // grew >=10% of the window (percent basis) or >=50% of the effective
187
- // threshold (token basis, when percent is unavailable). The cached `cut`
188
- // is only valid while the transcript grows within the epoch — it is
189
- // cleared on session_compact (durable truncation) + resetRuntime, so we
190
- // never replay a stale cut into a truncated transcript (PREVENT-PI-001/002).
191
- const RECOMPACT_PCT_DELTA = 10;
180
+ // re-compact. Re-compact only when context grew >=config.recompactPctDelta%
181
+ // of the window (percent basis) or >=50% of the effective threshold
182
+ // (token basis, when percent is unavailable). The cached `cut` is only
183
+ // valid while the transcript grows within the epoch it is cleared on
184
+ // session_compact (durable truncation) + resetRuntime, so we never replay
185
+ // a stale cut into a truncated transcript (PREVENT-PI-001/002).
192
186
  if (runtime.trimCache &&
193
187
  runtime.trimCache.checkpointId === runtime.rt.lastCheckpointId &&
194
188
  runtime.trimCache.cut <= messages.length) {
195
189
  const grewEnough = pct != null && runtime.trimCache.ctxPct != null
196
- ? pct - runtime.trimCache.ctxPct >= RECOMPACT_PCT_DELTA
190
+ ? pct - runtime.trimCache.ctxPct >= config.recompactPctDelta
197
191
  : currentTokens - (runtime.trimCache.ctxTokens ?? 0) >=
198
192
  runtime.effectiveThreshold * 0.5;
199
193
  if (!grewEnough) {
@@ -209,6 +203,14 @@ export function registerContextHandler(pi, runtime, config) {
209
203
  }
210
204
  // else: context grew enough → fall through to re-compact (cache is stale)
211
205
  }
206
+ // Debounce so we don't fire on every context event past threshold.
207
+ // (Replay already returned above — only fresh compacts reach this point.)
208
+ const now = Date.now();
209
+ if (now < runtime.debounceUntil) {
210
+ runtime.diagCtxDebounce++;
211
+ return;
212
+ }
213
+ runtime.debounceUntil = now + 2000;
212
214
  // Adaptive compression (Fix E): scale compression strength + keepFrom depth
213
215
  // with how close we are to the model context limit. Null-safe: when the
214
216
  // token-fallback path ran (pct unavailable) use the token-basis pressure
@@ -219,8 +221,22 @@ export function registerContextHandler(pi, runtime, config) {
219
221
  const ran = runCompact(pi, runtime, config, ctx, messages, {
220
222
  compressionPressure: pressure,
221
223
  });
224
+ // D.3: skip paths fall back to replay instead of returning empty.
225
+ // If runCompact skipped and we have a valid trimCache, replay it
226
+ // (free stability win) — otherwise defer to the next event.
222
227
  if (ran.skipped) {
223
228
  runtime.diagCtxRunSkipped++;
229
+ if (runtime.trimCache &&
230
+ runtime.trimCache.checkpointId === runtime.rt.lastCheckpointId &&
231
+ runtime.trimCache.cut <= messages.length) {
232
+ const recent = messages.slice(runtime.trimCache.cut); // guardrails-allow PREVENT-PI-002
233
+ runtime.diagLiveTrimFires++;
234
+ runtime.diagLiveTrimReplays++;
235
+ runtime.snapshot(ctx);
236
+ return {
237
+ messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent],
238
+ };
239
+ }
224
240
  return;
225
241
  }
226
242
  // S27 DB-mirror: write checkpoint_epoch with deterministic nonce.
@@ -277,7 +293,9 @@ export function registerContextHandler(pi, runtime, config) {
277
293
  }
278
294
  }
279
295
  catch (wikiErr) {
280
- runtime.logger.warn("wiki_rebuild_failed", { error: String(wikiErr) });
296
+ runtime.logger.warn("wiki_rebuild_failed", {
297
+ error: String(wikiErr),
298
+ });
281
299
  }
282
300
  // S27 Task 6: Fire-and-forget dedup pipeline.
283
301
  // Deduplicates raw_transcript rows for the compacted range.
@@ -11,6 +11,25 @@
11
11
  * compact per signature). Conservative: when unsure between transient and
12
12
  * poisoned, choose transient (bounded by R1 dedup + R2 session cap).
13
13
  */
14
+ /** R7: known-retryable transient markers — network/throughput failures that
15
+ * retry can fix and /clear cannot. SINGLE SOURCE OF TRUTH: classifyError uses
16
+ * this for its transient-marker stage, and the agent-handlers R3 repeat
17
+ * upgrade guard uses it to keep these errors out of poisoned-context. Add new
18
+ * transient phrasings HERE only.
19
+ *
20
+ * Non-obvious alternatives:
21
+ * - `time(?:d[\s-]?out|out)` covers "timeout", "timed out", "timed-out" —
22
+ * "timed out" does NOT match /timeout/ (the 2026-07-30 incident phrasing).
23
+ * - `etimedout` is Node's timeout errno lowercased — does NOT contain the
24
+ * substring "timeout".
25
+ * - `socket hang up` is Node's ECONNRESET *message*; the errno lives in
26
+ * error.code, which extractErrorSignature never sees. */
27
+ export const KNOWN_RETRYABLE_TRANSIENT_PATTERN = /max(imum)? output token|rate[\s.-]?limit|429|too many requests|overloaded|5\d\d|internal server|bad gateway|service unavailable|network|time(?:d[\s-]?out|out)|etimedout|econnreset|econnrefused|epipe|eai_again|socket hang up|premature close|other side closed|connection (lost|refused|reset|aborted?)|stream (interrupted|closed|ended|failed)|disconnected/;
28
+ /** R7: true when the error text carries a known-retryable transient marker.
29
+ * Defensively lowercases so callers can pass raw or normalized text. */
30
+ export function isKnownRetryableTransient(text) {
31
+ return KNOWN_RETRYABLE_TRANSIENT_PATTERN.test(text.toLowerCase());
32
+ }
14
33
  /** S38.2: classify a turn-end error/stop signal into a retry category.
15
34
  *
16
35
  * `length` is returned as null — S28 owns the max-output-token length stopReason
@@ -134,22 +153,14 @@ export function classifyError(message) {
134
153
  if (/too long|context window|maximum context length|context length exceeded|requires at least \d+ tokens|even after compaction|reduce the conversation|reduce your input/.test(s)) {
135
154
  return 'context-overflow';
136
155
  }
137
- // --- transient (specific markers FIRST — these override poisoned signals) ---
138
- // R3: network failures (timeout, ECONNRESET, 5xx, 429) MUST stay transient
139
- // even when usage is 0 tokens, because they are retryable. The specific
140
- // markers below return before any poisoned signal is evaluated.
141
- if (/max(imum)? output token/.test(s))
142
- return 'transient';
143
- if (/rate[\s.-]?limit|429|too many requests/.test(s))
144
- return 'transient';
145
- if (/5\d\d|internal server|bad gateway|service unavailable/.test(s))
146
- return 'transient';
147
- // 'connection aborted' is a network failure (ECONNABORTED), NOT a user ESC
148
- // (which is stopReason 'aborted', handled by the early-return above). Added
149
- // so "Connection aborted by peer" stays transient under R3. ECONNRESET and
150
- // common errno names are included so a bare 0-token 'ECONNRESET' turn is not
151
- // misclassified as poisoned (R6: ECONNRESET → transient).
152
- if (/network|timeout|econnreset|econnrefused|epipe|connection (lost|refused|reset|aborted)|stream (interrupted|closed|ended|failed)|disconnected/.test(s))
156
+ // --- transient (known-retryable markers FIRST — these override poisoned signals) ---
157
+ // R3: network/throughput failures (timeout, ECONNRESET, 5xx, 429) MUST stay
158
+ // transient even when usage is 0 tokens, because they are retryable and
159
+ // /clear cannot fix them. 'connection aborted' is a network failure
160
+ // (ECONNABORTED), NOT a user ESC (stopReason 'aborted', early-return above).
161
+ // The marker set is shared with the agent-handlers R3 repeat-upgrade guard
162
+ // (isKnownRetryableTransient) so the two never drift.
163
+ if (isKnownRetryableTransient(s))
153
164
  return 'transient';
154
165
  // --- poisoned-context (R3: ORDER AFTER specific transient markers, BEFORE
155
166
  // the generic 'error' transient fallthrough) ---
@@ -18,8 +18,8 @@
18
18
  */
19
19
  import { join } from "node:path";
20
20
  import { vectorStats, vectorRepoStats, vectorDataInvariant, } from "../../src/vectorStore.js";
21
- import { latestModelSnapshot, recordPerfSample, recordSessionHeartbeat, appendTokenSample, } from "../../src/store/sqlite.js";
22
- import { resolveRepoRoot } from "../mega-config.js";
21
+ import { latestModelSnapshot, readLatestCacheHitPct, recordPerfSample, recordSessionHeartbeat, appendTokenSample, } from "../../src/store/sqlite.js";
22
+ import { resolveRepoRoot, } from "../mega-config.js";
23
23
  import { computeMegaSnapshot } from "./snapshot.js";
24
24
  import { buildDashboardSnapshot } from "./dashboard-snapshot.js";
25
25
  import { materialSigImpl, embedderNameImpl, driftStatusImpl, getTurnLevelImpl, } from "./runtime-helpers.js";
@@ -134,6 +134,16 @@ export function snapshotImpl(self, ctx) {
134
134
  }
135
135
  // ── gather widget data (computed per snapshot, rendered per frame) ────
136
136
  const modelSnap = latestModelSnapshot(self.currentStateDir);
137
+ // Provider prompt cache hit % for the widget (B/C): latest
138
+ // cache_hit_pct sample. Non-fatal — one extra sync open per
139
+ // material-change-gated recompute, acceptably cheap.
140
+ let providerCachePct = 0;
141
+ try {
142
+ providerCachePct = readLatestCacheHitPct(self.currentStateDir);
143
+ }
144
+ catch {
145
+ /* non-fatal */
146
+ }
137
147
  const _snapResult = computeMegaSnapshot({
138
148
  lastCtxTokens: self.lastCtxTokens,
139
149
  lastCtxWindow: self.lastCtxWindow,
@@ -165,6 +175,7 @@ export function snapshotImpl(self, ctx) {
165
175
  ready,
166
176
  armed,
167
177
  modelSnap,
178
+ providerCachePct,
168
179
  });
169
180
  self.widgetData = _snapResult.widgetData;
170
181
  // S33: consume the flare after copying it into widgetData so it fires
@@ -186,8 +197,7 @@ export function snapshotImpl(self, ctx) {
186
197
  // (effectBorderSgr returns '' once expired), so this is bookkeeping to
187
198
  // free the slot and prevent a stale effect lingering between snapshots.
188
199
  if (self.activeEffect &&
189
- Date.now() - self.activeEffect.startedAt >=
190
- self.activeEffect.durationMs) {
200
+ Date.now() - self.activeEffect.startedAt >= self.activeEffect.durationMs) {
191
201
  self.activeEffect = null;
192
202
  }
193
203
  // Auto-fit: register a factory so pi re-renders the panel at the REAL
@@ -91,7 +91,7 @@ export function computeMegaSnapshot(p) {
91
91
  // ── S31: game-mode state ──────────────────────────────────────────────
92
92
  const gs = p.getCachedGameState();
93
93
  const curLevel = p.getTurnLevel();
94
- const cachePct = st.dedupHitRate * 100;
94
+ const cachePct = p.providerCachePct;
95
95
  const widgetData = {
96
96
  version: ownVersion(),
97
97
  tierLabel,
@@ -0,0 +1,127 @@
1
+ /**
2
+ * snapshot.test.ts — C.4 computeMegaSnapshot tests for providerCachePct
3
+ * and megaCacheFlare fields.
4
+ *
5
+ * Uses MEGACOMPACT_STATE_DIR + mkdtemp (G7). No pi runtime.
6
+ */
7
+ import { describe, it, before, after } from "node:test";
8
+ import assert from "node:assert/strict";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import { mkdtempSync, rmSync } from "node:fs";
12
+ import { computeMegaSnapshot } from "./snapshot.js";
13
+ function baseInput(overrides = {}) {
14
+ return {
15
+ rtTokensSaved: 1000,
16
+ lastCtxPercent: null,
17
+ lastCtxTokens: 0,
18
+ lastCtxWindow: 0,
19
+ activeAgents: 0,
20
+ currentTurn: 0,
21
+ statusKey: undefined,
22
+ ready: false,
23
+ armed: false,
24
+ st: {
25
+ totalTokenEstimate: 800,
26
+ originalTokens: 1200,
27
+ storageDedupRate: 0.12,
28
+ checkpointCount: 3,
29
+ lastCheckpointId: undefined,
30
+ lastSummary: undefined,
31
+ injectedCount: 0,
32
+ dedupHitRate: 0.05,
33
+ tokensSaved: 200,
34
+ dedupAttempts: 40,
35
+ dedupCollapsed: 5,
36
+ },
37
+ repo: {
38
+ tokensSaved: 5000,
39
+ totalTokenEstimate: 4000,
40
+ checkpointCount: 9,
41
+ sessionCount: 2,
42
+ originalTokens: 6000,
43
+ dedupAttempts: 100,
44
+ dedupCollapsed: 30,
45
+ storageDedupRate: 0.3,
46
+ },
47
+ pressureBand: "low",
48
+ configTier: "default",
49
+ modelSnap: undefined,
50
+ lastCompactAt: null,
51
+ embedderName: () => "Trigram",
52
+ driftStatus: () => "ok",
53
+ getCachedGameState: () => ({
54
+ game_mode_on: false,
55
+ theme: "transparent",
56
+ tui_display_mode: "full",
57
+ }),
58
+ getTurnLevel: () => 1,
59
+ providerCachePct: 56.2,
60
+ megaCacheFlare: false,
61
+ megaCacheFlarePct: 0,
62
+ levelUpFlare: false,
63
+ achievementFlare: false,
64
+ achievementFlareTitles: [],
65
+ activeEffect: null,
66
+ lastActivityAt: Date.now(),
67
+ ticker: [],
68
+ lastWhy: undefined,
69
+ tierTrace: undefined,
70
+ pulsing: false,
71
+ ...overrides,
72
+ };
73
+ }
74
+ describe("computeMegaSnapshot (C.4)", () => {
75
+ let dir;
76
+ before(() => {
77
+ dir = mkdtempSync(join(tmpdir(), "mc-snap-"));
78
+ process.env.MEGACOMPACT_STATE_DIR = dir;
79
+ });
80
+ after(() => {
81
+ delete process.env.MEGACOMPACT_STATE_DIR;
82
+ rmSync(dir, { recursive: true, force: true });
83
+ });
84
+ // ── C.1: providerCachePct flows into cachePct ─────────────────────────
85
+ it("cachePct equals providerCachePct (not dedup hit rate)", () => {
86
+ const res = computeMegaSnapshot(baseInput({ providerCachePct: 56.2 }));
87
+ assert.equal(res.widgetData.cachePct, 56.2, "cachePct is providerCachePct");
88
+ });
89
+ it("cachePct is 0 when providerCachePct is 0", () => {
90
+ const res = computeMegaSnapshot(baseInput({ providerCachePct: 0 }));
91
+ assert.equal(res.widgetData.cachePct, 0, "cachePct is 0");
92
+ });
93
+ it("cachePct is independent of dedup hit rate", () => {
94
+ const res = computeMegaSnapshot(baseInput({
95
+ providerCachePct: 12.5,
96
+ st: {
97
+ totalTokenEstimate: 99999,
98
+ originalTokens: 99999,
99
+ storageDedupRate: 0.99,
100
+ checkpointCount: 1,
101
+ lastCheckpointId: undefined,
102
+ lastSummary: undefined,
103
+ injectedCount: 0,
104
+ dedupHitRate: 0.05,
105
+ tokensSaved: 200,
106
+ dedupAttempts: 40,
107
+ dedupCollapsed: 5,
108
+ },
109
+ }));
110
+ assert.equal(res.widgetData.cachePct, 12.5, "cachePct ignores dedup");
111
+ });
112
+ // ── megaCacheFlare fields pass through ────────────────────────────────
113
+ it("megaCacheFlare + megaCacheFlarePct pass through to widgetData", () => {
114
+ const res = computeMegaSnapshot(baseInput({ megaCacheFlare: true, megaCacheFlarePct: 287 }));
115
+ assert.equal(res.widgetData.megaCacheFlare, true);
116
+ assert.equal(res.widgetData.megaCacheFlarePct, 287);
117
+ });
118
+ it("megaCacheFlare is false by default", () => {
119
+ const res = computeMegaSnapshot(baseInput());
120
+ assert.equal(res.widgetData.megaCacheFlare, false);
121
+ });
122
+ // ── level pass-through ────────────────────────────────────────────────
123
+ it("level flows from getTurnLevel", () => {
124
+ const res = computeMegaSnapshot(baseInput({ getTurnLevel: () => 42 }));
125
+ assert.equal(res.curLevel, 42);
126
+ });
127
+ });