pi-mega-compact 0.20.84 → 0.20.85

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.
@@ -126,6 +126,12 @@ export const SETTINGS = [
126
126
  num("MEGACOMPACT_EMBEDDING_CHARS_PER_TOKEN", "Embedding Chars per Token", "Estimated characters per token used for embedder chunking size", 4, 1, 32),
127
127
  ],
128
128
  },
129
+ {
130
+ name: "Compaction",
131
+ settings: [
132
+ num("MEGACOMPACT_THRESHOLD_PCT", "Compaction Threshold", "Fraction of the actual model context window at which compaction fires — 0.80 fires at 80% used (leaves 20% free). Applies to any model size; a per-model Model Thresholds row overrides it", 0.8, 0.1, 0.95),
133
+ ],
134
+ },
129
135
  VECTOR_CORTEX_SETTINGS,
130
136
  {
131
137
  name: "Cost API",
@@ -66,11 +66,30 @@ function resolveThreshold() {
66
66
  }
67
67
  const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
68
68
  const tier = (raw in COMPACT_TIERS ? raw : "low");
69
- const tierPct = TIER_PCT[tier];
69
+ let tierPct = TIER_PCT[tier];
70
+ // 3WF-2 threshold invariant: under the umbrella, when no named tier is set
71
+ // the fire point is the configurable % of the ACTUAL model window (default
72
+ // 0.80 — "20% free remaining"). Tiered (named preset) keeps its preset pct;
73
+ // both paths still compute the legacy 200k boot fallback below as a display
74
+ // placeholder + the custom-tier absolute companion. Umbrella OFF stays
75
+ // byte-identical to v0.20.83 (default tier=low 0.5).
76
+ const umbrella = envBool("MEGACOMPACT_THREE_WAY_FAILBACK", true);
77
+ if (umbrella && !(process.env.MEGACOMPACT_TIER && process.env.MEGACOMPACT_TIER !== "")) {
78
+ tierPct = clamp(envFlag("MEGACOMPACT_THRESHOLD_PCT", 0.8), 0.1, 0.95);
79
+ }
70
80
  // Boot fallback: sane gate before the first context event provides a window.
81
+ // (NO hardcoded window in the firing path — effectiveThresholdImpl defers
82
+ // when window unknown; this remains only a display placeholder + custom
83
+ // companion under the umbrella.)
71
84
  const thresholdTokens = Math.round(tierPct * 200_000);
72
85
  return { tier, tierPct, thresholdTokens };
73
86
  }
87
+ /** Clamp `n` into [lo, hi]; non-finite → fallback. */
88
+ function clamp(n, lo, hi) {
89
+ if (!Number.isFinite(n))
90
+ return lo;
91
+ return Math.min(hi, Math.max(lo, n));
92
+ }
74
93
  /**
75
94
  * Pure helper: the real compaction fire point, given the model context window.
76
95
  *
@@ -40,11 +40,23 @@ export function evaluateGate(runtime, config, opts) {
40
40
  }
41
41
  else {
42
42
  // custom tier OR tiered-but-pct-unavailable → token gate (S27 fallback).
43
- if (currentTokens < runtime.effectiveThreshold) {
43
+ // 3WF-2: under the umbrella, when the window is known AND the config is
44
+ // tiered, honor the per-model Dashboard override exactly like the percent
45
+ // branch (firePointPct % of the actual window) instead of the bare boot
46
+ // fallback. custom / window-unknown / umbrella-OFF keep the bare
47
+ // runtime.effectiveThreshold (window-unknown defers via +Infinity from
48
+ // effectiveThresholdImpl; custom is the explicit absolute).
49
+ let gateThreshold = runtime.effectiveThreshold;
50
+ if (config.threeWayFailback &&
51
+ config.tierPct != null &&
52
+ runtime.lastCtxWindow > 0) {
53
+ gateThreshold = Math.round((perModelThreshold.firePointPct / 100) * runtime.lastCtxWindow);
54
+ }
55
+ if (currentTokens < gateThreshold) {
44
56
  runtime.diagCtxFastGate++;
45
57
  return { kind: "return", view: tailResult() ?? undefined };
46
58
  }
47
- const check = autoCompactCheck(currentTokens, runtime.effectiveThreshold); // SERVER-STYLE CONFIRM (local)
59
+ const check = autoCompactCheck(currentTokens, gateThreshold); // SERVER-STYLE CONFIRM (local)
48
60
  if (!check.shouldCompact) {
49
61
  runtime.diagCtxNoCompact++;
50
62
  return { kind: "return", view: tailResult() ?? undefined };
@@ -51,6 +51,18 @@ export function pressureImpl(self) {
51
51
  * always below pi's native auto-compaction (~80% of window).
52
52
  */
53
53
  export function effectiveThresholdImpl(self) {
54
+ // 3WF-2 threshold invariant: under the umbrella, a tiered config with an
55
+ // UNKNOWN window (lastCtxWindow <= 0) DEFERS — auto-compaction must never
56
+ // substitute a guessed window. Returning +Infinity keeps every downstream
57
+ // `tokens >= threshold` comparison false (gateCheck token path,
58
+ // agent_end durable trigger, live-trim re-compact), so no compaction fires
59
+ // until the provider reports a real window. custom (tierPct null) and
60
+ // umbrella-OFF fall through to the legacy helper (byte-identical).
61
+ if (self.config.threeWayFailback &&
62
+ self.config.tierPct != null &&
63
+ self.lastCtxWindow <= 0) {
64
+ return Number.POSITIVE_INFINITY;
65
+ }
54
66
  return effectiveThresholdTokens({
55
67
  tierPct: self.config.tierPct,
56
68
  fallbackThreshold: self.config.thresholdTokens,
@@ -274,6 +274,19 @@ export const SETTINGS: ReadonlyArray<SettingGroup> = [
274
274
  num("MEGACOMPACT_EMBEDDING_CHARS_PER_TOKEN", "Embedding Chars per Token", "Estimated characters per token used for embedder chunking size", 4, 1, 32),
275
275
  ],
276
276
  },
277
+ {
278
+ name: "Compaction",
279
+ settings: [
280
+ num(
281
+ "MEGACOMPACT_THRESHOLD_PCT",
282
+ "Compaction Threshold",
283
+ "Fraction of the actual model context window at which compaction fires — 0.80 fires at 80% used (leaves 20% free). Applies to any model size; a per-model Model Thresholds row overrides it",
284
+ 0.8,
285
+ 0.1,
286
+ 0.95,
287
+ ),
288
+ ],
289
+ },
277
290
  VECTOR_CORTEX_SETTINGS,
278
291
  {
279
292
  name: "Cost API",
@@ -78,12 +78,31 @@ function resolveThreshold(): {
78
78
  }
79
79
  const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
80
80
  const tier = (raw in COMPACT_TIERS ? raw : "low") as CompactTier;
81
- const tierPct = TIER_PCT[tier];
81
+ let tierPct = TIER_PCT[tier];
82
+ // 3WF-2 threshold invariant: under the umbrella, when no named tier is set
83
+ // the fire point is the configurable % of the ACTUAL model window (default
84
+ // 0.80 — "20% free remaining"). Tiered (named preset) keeps its preset pct;
85
+ // both paths still compute the legacy 200k boot fallback below as a display
86
+ // placeholder + the custom-tier absolute companion. Umbrella OFF stays
87
+ // byte-identical to v0.20.83 (default tier=low 0.5).
88
+ const umbrella = envBool("MEGACOMPACT_THREE_WAY_FAILBACK", true);
89
+ if (umbrella && !(process.env.MEGACOMPACT_TIER && process.env.MEGACOMPACT_TIER !== "")) {
90
+ tierPct = clamp(envFlag("MEGACOMPACT_THRESHOLD_PCT", 0.8), 0.1, 0.95);
91
+ }
82
92
  // Boot fallback: sane gate before the first context event provides a window.
93
+ // (NO hardcoded window in the firing path — effectiveThresholdImpl defers
94
+ // when window unknown; this remains only a display placeholder + custom
95
+ // companion under the umbrella.)
83
96
  const thresholdTokens = Math.round(tierPct * 200_000);
84
97
  return { tier, tierPct, thresholdTokens };
85
98
  }
86
99
 
100
+ /** Clamp `n` into [lo, hi]; non-finite → fallback. */
101
+ function clamp(n: number, lo: number, hi: number): number {
102
+ if (!Number.isFinite(n)) return lo;
103
+ return Math.min(hi, Math.max(lo, n));
104
+ }
105
+
87
106
  /**
88
107
  * Pure helper: the real compaction fire point, given the model context window.
89
108
  *
@@ -82,11 +82,27 @@ export function evaluateGate(
82
82
  gatePassed = pct / 100 >= firePct;
83
83
  } else {
84
84
  // custom tier OR tiered-but-pct-unavailable → token gate (S27 fallback).
85
- if (currentTokens < runtime.effectiveThreshold) {
85
+ // 3WF-2: under the umbrella, when the window is known AND the config is
86
+ // tiered, honor the per-model Dashboard override exactly like the percent
87
+ // branch (firePointPct % of the actual window) instead of the bare boot
88
+ // fallback. custom / window-unknown / umbrella-OFF keep the bare
89
+ // runtime.effectiveThreshold (window-unknown defers via +Infinity from
90
+ // effectiveThresholdImpl; custom is the explicit absolute).
91
+ let gateThreshold = runtime.effectiveThreshold;
92
+ if (
93
+ config.threeWayFailback &&
94
+ config.tierPct != null &&
95
+ runtime.lastCtxWindow > 0
96
+ ) {
97
+ gateThreshold = Math.round(
98
+ (perModelThreshold.firePointPct / 100) * runtime.lastCtxWindow,
99
+ );
100
+ }
101
+ if (currentTokens < gateThreshold) {
86
102
  runtime.diagCtxFastGate++;
87
103
  return { kind: "return", view: tailResult() ?? undefined };
88
104
  }
89
- const check = autoCompactCheck(currentTokens, runtime.effectiveThreshold); // SERVER-STYLE CONFIRM (local)
105
+ const check = autoCompactCheck(currentTokens, gateThreshold); // SERVER-STYLE CONFIRM (local)
90
106
  if (!check.shouldCompact) {
91
107
  runtime.diagCtxNoCompact++;
92
108
  return { kind: "return", view: tailResult() ?? undefined };
@@ -81,6 +81,20 @@ export function pressureImpl(self: PressureContext): number {
81
81
  * always below pi's native auto-compaction (~80% of window).
82
82
  */
83
83
  export function effectiveThresholdImpl(self: PressureContext): number {
84
+ // 3WF-2 threshold invariant: under the umbrella, a tiered config with an
85
+ // UNKNOWN window (lastCtxWindow <= 0) DEFERS — auto-compaction must never
86
+ // substitute a guessed window. Returning +Infinity keeps every downstream
87
+ // `tokens >= threshold` comparison false (gateCheck token path,
88
+ // agent_end durable trigger, live-trim re-compact), so no compaction fires
89
+ // until the provider reports a real window. custom (tierPct null) and
90
+ // umbrella-OFF fall through to the legacy helper (byte-identical).
91
+ if (
92
+ self.config.threeWayFailback &&
93
+ self.config.tierPct != null &&
94
+ self.lastCtxWindow <= 0
95
+ ) {
96
+ return Number.POSITIVE_INFINITY;
97
+ }
84
98
  return effectiveThresholdTokens({
85
99
  tierPct: self.config.tierPct,
86
100
  fallbackThreshold: self.config.thresholdTokens,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.84",
3
+ "version": "0.20.85",
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",