pi-mega-compact 0.6.9 → 0.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.6.9",
3
+ "version": "0.7.1",
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-2-Clause",
@@ -24,6 +24,12 @@ import { autoCompactCheck } from "./compact.js";
24
24
  import { loadDedupConfig, type DedupConfigShape } from "./config/dedup.js";
25
25
  import type { EngineMessage } from "./types.js";
26
26
 
27
+ // Real percentage-based threshold config. Replaces the previous LOCAL replica of
28
+ // COMPACT_TIERS + resolveThresholdFromEnv that asserted the OLD static token
29
+ // amounts — importing the live source of truth keeps tests in sync with the
30
+ // source (thresholds are tierPct × the model's context window, not fixed tokens).
31
+ import { TIER_PCT, effectiveThresholdTokens, loadConfig } from "../extensions/mega-config.js";
32
+
27
33
  // recallAndInline may or may not be exported; import safely.
28
34
  import * as recallMod from "./recall.js";
29
35
 
@@ -540,48 +546,63 @@ describe("Edge Cases", () => {
540
546
  });
541
547
  });
542
548
 
543
- // -------------------- 7. Tier Switching --------------------
549
+ // -------------------- 7. Tier Switching (percentage-based) --------------------
550
+
551
+ // Replaces the previous LOCAL replica of COMPACT_TIERS + resolveThresholdFromEnv
552
+ // that asserted the OLD static token amounts. We now import the REAL config
553
+ // helpers from extensions/mega-config.js so the tests track the live source of
554
+ // truth: thresholds are tierPct × the model's context window (not fixed tokens).
555
+
556
+ describe("Tier Switching — percentage-based thresholds", () => {
557
+ // Documented tierPct fractions (single source of truth in mega-config.ts).
558
+ it("each named tier carries the documented tierPct fraction", () => {
559
+ assert.equal(TIER_PCT.low, 0.5);
560
+ assert.equal(TIER_PCT.medium, 0.6);
561
+ assert.equal(TIER_PCT.high, 0.7);
562
+ assert.equal(TIER_PCT.ultra, 0.7);
563
+ assert.equal(TIER_PCT.mega, 0.75);
564
+ });
544
565
 
545
- describe("Tier Switching", () => {
546
- it("MEGACOMPACT_TIER env changes produce expected thresholds via extension logic", () => {
547
- const tiers: Array<[string, number]> = [
548
- ["low", 50_000],
549
- ["medium", 100_000],
550
- ["high", 200_000],
551
- ["ultra", 1_000_000],
552
- ["mega", 10_000_000],
566
+ // Boot fallback threshold (sane gate before the first context event supplies a
567
+ // window): round(tierPct × 200_000). Resolved through the REAL loadConfig().
568
+ it("MEGACOMPACT_TIER env resolves to the boot fallback threshold via real config", () => {
569
+ const tiers: Array<[keyof typeof TIER_PCT, number]> = [
570
+ ["low", 100_000], // 0.50 × 200_000
571
+ ["medium", 120_000], // 0.60 × 200_000
572
+ ["high", 140_000], // 0.70 × 200_000
573
+ ["ultra", 140_000], // 0.70 × 200_000
574
+ ["mega", 150_000], // 0.75 × 200_000
553
575
  ];
554
-
555
- for (const [tier, expectedThreshold] of tiers) {
576
+ for (const [tier, expectedBoot] of tiers) {
556
577
  const original = process.env.MEGACOMPACT_TIER;
578
+ delete process.env.MEGACOMPACT_THRESHOLD_TOKENS;
557
579
  process.env.MEGACOMPACT_TIER = tier;
558
580
  try {
559
- // Re-import to pick up env change. Since modules are cached, resolve threshold
560
- // directly via COMPACT_TIERS local replica mirroring extensions/mega-compact.ts.
561
- const threshold = resolveThresholdFromEnv();
581
+ const cfg = loadConfig();
582
+ assert.equal(cfg.tier, tier, `tier ${tier} should resolve`);
583
+ assert.equal(cfg.tierPct, TIER_PCT[tier], `tier ${tier} tierPct`);
562
584
  assert.equal(
563
- threshold,
564
- expectedThreshold,
565
- `tier ${tier} should resolve to ${expectedThreshold}`,
585
+ cfg.thresholdTokens,
586
+ expectedBoot,
587
+ `tier ${tier} boot fallback threshold should be ${expectedBoot}`,
566
588
  );
567
589
  } finally {
568
- if (original === undefined) {
569
- delete process.env.MEGACOMPACT_TIER;
570
- } else {
571
- process.env.MEGACOMPACT_TIER = original;
572
- }
590
+ if (original === undefined) delete process.env.MEGACOMPACT_TIER;
591
+ else process.env.MEGACOMPACT_TIER = original;
573
592
  }
574
593
  }
575
594
  });
576
595
 
577
- it("explicit MEGACOMPACT_THRESHOLD_TOKENS overrides tier", () => {
596
+ it("explicit MEGACOMPACT_THRESHOLD_TOKENS overrides tier (custom stays absolute)", () => {
578
597
  const originalTier = process.env.MEGACOMPACT_TIER;
579
598
  const originalThreshold = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
580
- process.env.MEGACOMPACT_TIER = "mega";
599
+ delete process.env.MEGACOMPACT_TIER;
581
600
  process.env.MEGACOMPACT_THRESHOLD_TOKENS = "123456";
582
601
  try {
583
- const threshold = resolveThresholdFromEnv();
584
- assert.equal(threshold, 123_456, "explicit token threshold should win");
602
+ const cfg = loadConfig();
603
+ assert.equal(cfg.tier, "custom", "explicit token threshold custom tier");
604
+ assert.equal(cfg.tierPct, null, "custom tier has no tierPct (stays absolute)");
605
+ assert.equal(cfg.thresholdTokens, 123_456, "explicit token threshold should win");
585
606
  } finally {
586
607
  if (originalTier === undefined) delete process.env.MEGACOMPACT_TIER;
587
608
  else process.env.MEGACOMPACT_TIER = originalTier;
@@ -591,19 +612,59 @@ describe("Tier Switching", () => {
591
612
  });
592
613
  });
593
614
 
594
- function resolveThresholdFromEnv(): number {
595
- const explicit = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
596
- if (explicit != null && explicit !== "") {
597
- const n = Number(explicit);
598
- if (Number.isFinite(n)) return n;
599
- }
600
- const COMPACT_TIERS: Record<string, number> = {
601
- low: 50_000,
602
- medium: 100_000,
603
- high: 200_000,
604
- ultra: 1_000_000,
605
- mega: 10_000_000,
606
- };
607
- const tier = process.env.MEGACOMPACT_TIER ?? "low";
608
- return COMPACT_TIERS[tier.toLowerCase()] ?? COMPACT_TIERS.low;
609
- }
615
+ describe("effectiveThresholdTokens — tierPct × model window", () => {
616
+ // The real compaction fire point. Tiered → scales with the window so it always
617
+ // fires BELOW pi's native ~80% auto-compact for any model size. Custom (null
618
+ // tierPct) absolute explicitThreshold, never percent-scaled.
619
+
620
+ it("scales tierPct × window for a 200k model", () => {
621
+ assert.equal(
622
+ effectiveThresholdTokens({ tierPct: TIER_PCT.low, fallbackThreshold: 100_000, window: 200_000 }),
623
+ 100_000,
624
+ );
625
+ assert.equal(
626
+ effectiveThresholdTokens({ tierPct: TIER_PCT.mega, fallbackThreshold: 150_000, window: 200_000 }),
627
+ 150_000,
628
+ );
629
+ });
630
+
631
+ it("scales tierPct × window for a 1M model", () => {
632
+ assert.equal(
633
+ effectiveThresholdTokens({ tierPct: TIER_PCT.low, fallbackThreshold: 500_000, window: 1_000_000 }),
634
+ 500_000,
635
+ );
636
+ assert.equal(
637
+ effectiveThresholdTokens({ tierPct: TIER_PCT.mega, fallbackThreshold: 750_000, window: 1_000_000 }),
638
+ 750_000,
639
+ );
640
+ });
641
+
642
+ it("falls back to the boot threshold when the window is 0/unknown", () => {
643
+ assert.equal(
644
+ effectiveThresholdTokens({ tierPct: TIER_PCT.mega, fallbackThreshold: 150_000, window: 0 }),
645
+ 150_000,
646
+ );
647
+ assert.equal(
648
+ effectiveThresholdTokens({ tierPct: TIER_PCT.low, fallbackThreshold: 100_000, window: -5 }),
649
+ 100_000,
650
+ );
651
+ });
652
+
653
+ it("custom (tierPct null) stays an absolute threshold regardless of window", () => {
654
+ assert.equal(
655
+ effectiveThresholdTokens({ tierPct: null, fallbackThreshold: 100_000, window: 200_000, explicitThreshold: 123456 }),
656
+ 123456,
657
+ "explicit absolute wins (200k window)",
658
+ );
659
+ assert.equal(
660
+ effectiveThresholdTokens({ tierPct: null, fallbackThreshold: 100_000, window: 1_000_000, explicitThreshold: 123456 }),
661
+ 123456,
662
+ "explicit absolute wins (1M window)",
663
+ );
664
+ assert.equal(
665
+ effectiveThresholdTokens({ tierPct: null, fallbackThreshold: 100_000, window: 200_000 }),
666
+ 100_000,
667
+ "no explicit → boot fallback",
668
+ );
669
+ });
670
+ });