pi-condense 2.7.0 → 2.8.0

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/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ Published to npm as [`pi-condense`](https://www.npmjs.com/package/pi-condense) (
7
7
  Pushing a `vX.Y.Z` tag triggers `.github/workflows/release.yml`, which runs the tests and
8
8
  publishes via OIDC trusted publishing. See `.agents/skills/release/SKILL.md`.
9
9
 
10
+ ## [2.8.0] - 2026-08-13
11
+
12
+ - **Both token-budget flush triggers now cap the context window they reason about at 300,000 tokens** (`MAX_BUDGET_WINDOW`, `src/budget.ts`) ([#7](https://github.com/jjuraszek/pi-condense/issues/7)). Previously both scaled purely off the model's advertised window, which made them unreachable as windows grew: on a 1M-window model `autoBudgetThreshold: 0.9` meant 900k tokens - a session ends long before that, so pruning never fired - while the same setting worked on a 200k model. `budgetTurnDelta: 0.1` was worse than late: it meant +100k context growth inside a single turn, which effectively never happens, so the re-arm trigger was dead.
13
+ - `autoBudgetThreshold` now fires at `min(300_000, threshold * contextWindow)` tokens - your percentage of the model's window, or 300k tokens, whichever comes first. The threshold keeps its literal meaning; the cap is only a ceiling.
14
+ - `budgetTurnDelta` applies the same ceiling in the other shape - a 300k ceiling on a single turn's *growth* could never bind - so the fraction is measured against `min(contextWindow, 300_000)`: `0.1` means +30k tokens in one turn on any model at or above 300k (+20k on a 200k model, unchanged).
15
+ - **No new setting, and no behavior change for any model advertising 300k or less, at any setting** - a 256k window at `0.9` still fires at 230.4k. Above 300k flushes happen earlier; nothing ever flushes later than before. Downward control is unaffected: `0.1` on a 1M model still means 100k tokens.
16
+ - `usageFraction` may now exceed `1.0` above the ceiling (600k tokens on a 1M window returns `2.0`) and is deliberately **not** clamped - clamping would saturate the delta trigger and stop it re-arming.
17
+ - Docs updated in step: `README.md`, `doc/configuration.md`, `PRUNING.md`. Spec: `doc/specs/2026-08-13-budget-window-cap.md`.
18
+
10
19
  ## [2.7.0] - 2026-08-12
11
20
 
12
21
  - **Single-chain observability + reload trigger repair ([#6](https://github.com/jjuraszek/pi-condense/issues/6)).** A 5-hour, 256-turn session running one uninterrupted tool chain (no text-only assistant reply ever closed it) accumulated ~195k tokens of raw toolResults while `/pruner status` showed near-zero activity: chain compression (Phase 3) requires *closed* chains by design, and a session reload cleared the in-memory pending queue, leaving the automatic flush trigger stranded even though the branch rescan could recover the work.
package/PRUNING.md CHANGED
@@ -816,15 +816,17 @@ ACON demonstrates that **compression not only saves tokens but can improve agent
816
816
 
817
817
  ### Token-budget auto-flush trigger
818
818
 
819
- `autoBudgetThreshold` (default `null`) is an ADDITIONAL flush trigger orthogonal to `pruneOn`. When set to a fraction in `(0, 1]`, the extension evaluates `tokens / contextWindow` at the end of every tool-using turn; when the ratio meets the threshold, all pending batches are flushed immediately regardless of the configured `pruneOn` mode.
819
+ `autoBudgetThreshold` (default `null`) is an ADDITIONAL flush trigger orthogonal to `pruneOn`. When set to a fraction in `(0, 1]`, the extension evaluates context usage at the end of every tool-using turn; when `tokens` reaches `min(MAX_BUDGET_WINDOW, threshold * contextWindow)` - i.e. the configured share of the model's window, or 300,000 tokens, whichever comes first - all pending batches are flushed immediately regardless of the configured `pruneOn` mode.
820
820
 
821
- Why we compute the ratio ourselves rather than using `ContextUsage.percent`: the provider's `percent` field is a 0100 value, and both it and `tokens` are `null` immediately after a provider-side compaction. Using `tokens / contextWindow` directly gives a 0–1 fraction that matches the config unit and is independently null-safe a `null` tokens value makes the trigger a no-op until usage is reported again.
821
+ **Why we compute this ourselves rather than using `ContextUsage.percent`:** the provider's `percent` field is a 0-100 value, and both it and `tokens` are `null` immediately after a provider-side compaction. Comparing `tokens` against a token level we derive ourselves keeps the unit unambiguous and is independently null-safe - a `null` tokens value makes the trigger a no-op until usage is reported again. Note this trigger never divides: the fraction form (`tokens / min(contextWindow, MAX_BUDGET_WINDOW)`) belongs to `usageFraction` and the delta trigger below, and reading the threshold as a share of a capped window would be wrong - on a 1M-window model, `0.4` fires at 300,000 tokens, not at 120,000.
822
+
823
+ **Why the 300k ceiling (`MAX_BUDGET_WINDOW`, `src/budget.ts`):** the trigger was originally a pure fraction of the advertised window, which made it unreachable as windows grew. On a 1M-window model, `0.9` means 900k tokens - a session ends long before that, so users observed "pruning never happens" (issue #7), while the same setting worked on a 200k model. The ceiling is deliberately chosen so it never binds at or below a 300k advertised window (a 256k model at `0.9` still fires at 230.4k), so it changes behavior only where the fraction was already unusable. Users keep full downward control: `0.1` on a 1M model still means 100k tokens.
822
824
 
823
825
  Lineage: simplified take on DCP's `maxContextLimit` nudging — a single threshold that forces a flush rather than separate nudge/force thresholds.
824
826
 
825
827
  ### Budget-delta flush
826
828
 
827
- `budgetTurnDelta: number | null` (default `null`) is a per-turn usage-jump trigger ORed with `autoBudgetThreshold`. When set to a fraction in `(0, 1]`, the extension compares the current turn's usage fraction (`tokens / contextWindow`) to the previous turn's and forces a flush if the jump meets or exceeds the delta.
829
+ `budgetTurnDelta: number | null` (default `null`) is a per-turn usage-jump trigger ORed with `autoBudgetThreshold`. When set to a fraction in `(0, 1]`, the extension compares the current turn's usage fraction (`tokens / min(contextWindow, MAX_BUDGET_WINDOW)`) to the previous turn's and forces a flush if the jump meets or exceeds the delta. Because the denominator carries the same 300k ceiling, the required growth is `delta * min(contextWindow, MAX_BUDGET_WINDOW)` tokens: `0.1` = +30k on any model at or above 300k, +20k on a 200k model. The ceiling enters through the denominator here rather than bounding a level, because a 300k ceiling on one turn's growth could never bind. Note the fraction is therefore not bounded by 1 above the ceiling (600k tokens on a 1M window = 2.0) and is deliberately not clamped - clamping would saturate this trigger and stop it re-arming.
828
830
 
829
831
  Use case: a single enormous tool result can jump context usage by 20–30 percentage points in one turn; `autoBudgetThreshold` misses this until the next turn. `budgetTurnDelta` catches the spike immediately.
830
832
 
@@ -971,7 +973,7 @@ Phase 3 (chain compression) only ever acts on **closed** chains - a chain closes
971
973
 
972
974
  Phase 1 (per-batch summarization) is unaffected by chain closure and remains the only lever for this shape of session. For long autonomous runs where no chain is expected to close:
973
975
 
974
- - Lower `autoBudgetThreshold` (e.g. `0.5`-`0.6` instead of the default `0.8`) so the budget trigger fires well before the open segment dominates the window.
976
+ - Lower `autoBudgetThreshold` (e.g. `0.5`-`0.6` instead of `0.8`) so the budget trigger fires well before the open segment dominates the window. On a window larger than 300k the ceiling already caps the trigger point at 300,000 tokens once `threshold * contextWindow` exceeds it - that is any setting at or above `300k / contextWindow` (`0.3` on a 1M window, `0.75` on a 400k one) - so lowering the setting only matters below that point.
975
977
  - Set `budgetTurnDelta` so a single turn's sudden context jump force-flushes even between budget-threshold crossings - this catches spikes a static threshold misses until the next turn.
976
978
 
977
979
  Neither knob makes a chain close; they just keep Phase 1 flushing on schedule so raw toolResults do not pile up unsummarized for the whole run.
package/README.md CHANGED
@@ -160,7 +160,7 @@ Settings live under `contextPrune` in `<agent-dir>/settings.json` (`$PI_CODING_A
160
160
  | `enabled` | `false` | Master switch (or just use `/pruner on`) |
161
161
  | `summarizerModel` | `"default"` | Pin a cheap model instead of reusing your active one - see the plan-by-plan table in [doc/configuration.md](doc/configuration.md#choosing-a-summarizer-model) |
162
162
  | `pruneOn` | `agent-message` | Trigger mode - see Architecture above |
163
- | `autoBudgetThreshold` | `null` | Fraction (e.g. `0.8`) of the context window that force-flushes everything regardless of `pruneOn` |
163
+ | `autoBudgetThreshold` | `null` | Fraction (e.g. `0.8`) of the context window that force-flushes everything regardless of `pruneOn`; the trigger point is capped at 300k tokens |
164
164
  | `protectedTools` / `protectedPaths` | `[]` / `["**/skills/**/*.md"]` | Tool names / path globs that are never pruned |
165
165
  | `spillThreshold` | `65536` | Chars above which a single oversized result spills straight to a sidecar file |
166
166
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-condense",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "Pi coding-agent extension that summarizes completed tool-call batches, replaces raw outputs with short stubs, compresses closed tool-call chains, and recovers any original on demand via context_tree_query.",
5
5
  "author": "Jacek Juraszek",
6
6
  "license": "MIT",
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from "bun:test";
2
- import { shouldBudgetFlush, shouldDeltaFlush, usageFraction } from "./budget.js";
2
+ import { shouldBudgetFlush, shouldDeltaFlush, usageFraction, MAX_BUDGET_WINDOW } from "./budget.js";
3
3
 
4
4
  const usage = (tokens: number | null, contextWindow: number) =>
5
5
  ({ tokens, contextWindow, percent: null }) as any;
@@ -31,6 +31,29 @@ describe("shouldBudgetFlush", () => {
31
31
  expect(shouldBudgetFlush(usage(1000, 1000), 1)).toBe(true);
32
32
  expect(shouldBudgetFlush(usage(999, 1000), 1)).toBe(false);
33
33
  });
34
+
35
+ it("exposes the window ceiling as 300k", () => {
36
+ expect(MAX_BUDGET_WINDOW).toBe(300_000);
37
+ });
38
+
39
+ it("caps the trigger level at MAX_BUDGET_WINDOW on a huge window", () => {
40
+ expect(shouldBudgetFlush(usage(300_000, 1_000_000), 0.4)).toBe(true);
41
+ expect(shouldBudgetFlush(usage(299_999, 1_000_000), 0.4)).toBe(false);
42
+ expect(shouldBudgetFlush(usage(300_000, 1_000_000), 0.9)).toBe(true);
43
+ });
44
+
45
+ it("leaves models at or below the ceiling unchanged", () => {
46
+ expect(shouldBudgetFlush(usage(80_000, 200_000), 0.4)).toBe(true);
47
+ expect(shouldBudgetFlush(usage(79_999, 200_000), 0.4)).toBe(false);
48
+ expect(shouldBudgetFlush(usage(230_400, 256_000), 0.9)).toBe(true);
49
+ expect(shouldBudgetFlush(usage(230_399, 256_000), 0.9)).toBe(false);
50
+ expect(shouldBudgetFlush(usage(300_000, 300_000), 1)).toBe(true);
51
+ });
52
+
53
+ it("lets a low threshold govern below the ceiling on a huge window", () => {
54
+ expect(shouldBudgetFlush(usage(100_000, 1_000_000), 0.1)).toBe(true);
55
+ expect(shouldBudgetFlush(usage(99_999, 1_000_000), 0.1)).toBe(false);
56
+ });
34
57
  });
35
58
 
36
59
  describe("usageFraction", () => {
@@ -39,9 +62,14 @@ describe("usageFraction", () => {
39
62
  expect(usageFraction(usage(null, 1000))).toBeNull();
40
63
  expect(usageFraction(usage(900, 0))).toBeNull();
41
64
  });
42
- it("returns the 0–1 fraction", () => {
65
+ it("returns the fraction against the effective window", () => {
43
66
  expect(usageFraction(usage(750, 1000))).toBe(0.75);
44
67
  });
68
+
69
+ it("is not clamped above 1 when the window exceeds the ceiling", () => {
70
+ expect(usageFraction(usage(600_000, 1_000_000))).toBe(2);
71
+ expect(usageFraction(usage(80_000, 200_000))).toBe(0.4);
72
+ });
45
73
  });
46
74
 
47
75
  describe("shouldDeltaFlush", () => {
@@ -63,4 +91,23 @@ describe("shouldDeltaFlush", () => {
63
91
  expect(shouldDeltaFlush(usage(640, 1000), 0.5, 0.15)).toBe(false); // 0.14 < 0.15
64
92
  expect(shouldDeltaFlush(usage(600, 1000), 0.5, 0.15)).toBe(false); // 0.10 < 0.15
65
93
  });
94
+
95
+ it("measures growth against the capped window on a huge-window model", () => {
96
+ // previousFraction for 100k tokens on a 1M window = 100_000 / 300_000
97
+ const prev = 100_000 / MAX_BUDGET_WINDOW;
98
+ expect(shouldDeltaFlush(usage(130_000, 1_000_000), prev, 0.1)).toBe(true);
99
+ expect(shouldDeltaFlush(usage(120_000, 1_000_000), prev, 0.1)).toBe(false);
100
+ });
101
+
102
+ it("leaves the growth requirement unchanged at or below the ceiling", () => {
103
+ const prev = 40_000 / 200_000; // 0.2
104
+ expect(shouldDeltaFlush(usage(100_000, 200_000), prev, 0.3)).toBe(true); // +60k
105
+ expect(shouldDeltaFlush(usage(99_000, 200_000), prev, 0.3)).toBe(false); // +59k
106
+ });
107
+
108
+ it("keeps re-arming above the ceiling, where the fraction exceeds 1", () => {
109
+ const prev = 600_000 / MAX_BUDGET_WINDOW; // 2.0 - unclamped by design
110
+ expect(shouldDeltaFlush(usage(630_000, 1_000_000), prev, 0.1)).toBe(true);
111
+ expect(shouldDeltaFlush(usage(620_000, 1_000_000), prev, 0.1)).toBe(false);
112
+ });
66
113
  });
package/src/budget.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import type { ContextUsage } from "@earendil-works/pi-coding-agent";
2
2
 
3
+ // Ceiling on what the budget triggers treat as the context window. Advertised
4
+ // windows reach 1M, which makes any (0,1] fraction unreachable in a real session.
5
+ // The two triggers apply it in different shapes on purpose: the threshold is a
6
+ // LEVEL, so the cap bounds the level itself (min(CAP, threshold * window)); the
7
+ // delta is a GROWTH RATE, where a 300k ceiling could never bind, so the cap
8
+ // enters through the denominator instead (delta * min(window, CAP)).
9
+ export const MAX_BUDGET_WINDOW = 300_000;
10
+
3
11
  /**
4
- * True iff a budget-triggered flush should fire. Computes the ratio ourselves
5
- * (tokens / contextWindow, a 0–1 fraction) rather than using ContextUsage.percent
6
- * (a 0–100 value, null when tokens is null). tokens is also null right after a
7
- * compaction — guarded here.
12
+ * True iff a budget-triggered flush should fire: at `threshold` of the model's
13
+ * window, or at MAX_BUDGET_WINDOW tokens, whichever comes first. Computes the
14
+ * level ourselves rather than using ContextUsage.percent (a 0–100 value, null
15
+ * when tokens is null). tokens is also null right after a compaction — guarded here.
8
16
  */
9
17
  export function shouldBudgetFlush(
10
18
  usage: ContextUsage | undefined,
@@ -12,17 +20,23 @@ export function shouldBudgetFlush(
12
20
  ): boolean {
13
21
  if (threshold == null || threshold <= 0 || threshold > 1) return false;
14
22
  if (!usage || usage.tokens == null || !(usage.contextWindow > 0)) return false;
15
- return usage.tokens / usage.contextWindow >= threshold;
23
+ return usage.tokens >= Math.min(MAX_BUDGET_WINDOW, threshold * usage.contextWindow);
16
24
  }
17
25
 
18
- /** 0–1 usage fraction, or null when usage is missing / tokens null / window non-positive. */
26
+ /**
27
+ * Usage fraction against the effective window (min(contextWindow, MAX_BUDGET_WINDOW)),
28
+ * or null when usage is missing / tokens null / window non-positive. NOT bounded by 1:
29
+ * 600k tokens on a 1M window returns 2.0. Deliberately unclamped — clamping would make
30
+ * shouldDeltaFlush saturate above the ceiling and stop re-arming.
31
+ */
19
32
  export function usageFraction(usage: ContextUsage | undefined): number | null {
20
33
  if (!usage || usage.tokens == null || !(usage.contextWindow > 0)) return null;
21
- return usage.tokens / usage.contextWindow;
34
+ return usage.tokens / Math.min(usage.contextWindow, MAX_BUDGET_WINDOW);
22
35
  }
23
36
 
24
37
  /**
25
- * True iff this turn's usage fraction rose by at least `delta` versus the previous turn.
38
+ * True iff this turn's usage fraction rose by at least `delta` versus the previous turn,
39
+ * i.e. growth of at least `delta * min(contextWindow, MAX_BUDGET_WINDOW)` tokens.
26
40
  * Mirrors shouldBudgetFlush's guards. previousFraction === null (first turn or post-restart)
27
41
  * never fires; the absolute autoBudgetThreshold covers that gap.
28
42
  */
package/src/commands.ts CHANGED
@@ -24,6 +24,7 @@ import {
24
24
  } from "./types.js";
25
25
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
26
26
  import { saveConfig } from "./config.js";
27
+ import { MAX_BUDGET_WINDOW } from "./budget.js";
27
28
  import { formatTokens, formatCost, formatCharProgress, formatCompactCount } from "./stats.js";
28
29
  import { Container, Text, SettingsList, type SettingItem } from "@earendil-works/pi-tui";
29
30
  import { DynamicBorder, getSettingsListTheme } from "@earendil-works/pi-coding-agent";
@@ -234,10 +235,12 @@ function maxTimeoutDescription(config: ContextPruneConfig): string {
234
235
  }
235
236
 
236
237
  function autoBudgetThresholdDescription(config: ContextPruneConfig): string {
238
+ const cap = `${MAX_BUDGET_WINDOW / 1000}k`;
237
239
  if (config.autoBudgetThreshold == null) {
238
- return `Token-budget auto-flush: force a prune when context usage reaches this share of the window, regardless of prune-on mode. Currently off. Pick a percentage to enable.`;
240
+ return `Token-budget auto-flush: force a prune when context usage reaches this share of the window (or ${cap} tokens, whichever comes first), regardless of prune-on mode. Currently off. Pick a percentage to enable.`;
239
241
  }
240
- return `Token-budget auto-flush: force a prune when context usage reaches ${Math.round(config.autoBudgetThreshold * 100)}% of the window, regardless of prune-on mode. Set to Off to disable.`;
242
+ const pct = Math.round(config.autoBudgetThreshold * 100);
243
+ return `Token-budget auto-flush: force a prune when context usage reaches ${pct}% of the window or ${cap} tokens, whichever comes first, regardless of prune-on mode. The ${cap} ceiling keeps this reachable on huge-window models. Set to Off to disable.`;
241
244
  }
242
245
 
243
246
  function protectedToolsDisplay(list: string[]): string {
package/src/types.ts CHANGED
@@ -239,7 +239,8 @@ export const SUMMARIZER_MAX_TIMEOUT_PRESETS: { value: string; label: string }[]
239
239
  /**
240
240
  * Cycling presets for the `autoBudgetThreshold` setting (stored as strings;
241
241
  * the settings UI cycles string values). "0" is the disabled sentinel → null.
242
- * Other values are 0–1 fractions of the context window (e.g. "0.8" = flush at 80%).
242
+ * Other values are 0–1 fractions of the context window (e.g. "0.8" = flush at
243
+ * 80% of the window, or at MAX_BUDGET_WINDOW tokens, whichever comes first).
243
244
  */
244
245
  export const AUTO_BUDGET_PRESETS: { value: string; label: string }[] = [
245
246
  { value: "0", label: "Off (default)" },
@@ -387,10 +388,14 @@ export interface ContextPruneConfig {
387
388
  /**
388
389
  * Token-budget auto-flush trigger. A fraction in (0, 1] (a 0–1 share of the
389
390
  * context window, NOT a 0–100 percentage; e.g. 0.8 = flush at 80% of the
390
- * window). When set, a flush of all pending batches is forced at the end of
391
- * any tool-using turn once context usage (tokens / contextWindow) reaches the
392
- * threshold regardless of `pruneOn`. An ADDITIONAL trigger on top of
393
- * `pruneOn`, not a replacement.
391
+ * window, capped at 300k tokens - see below). When set, a flush of all
392
+ * pending batches is forced at the end of
393
+ * any tool-using turn once context usage reaches `threshold * contextWindow`
394
+ * tokens OR 300,000 tokens (MAX_BUDGET_WINDOW in src/budget.ts), whichever
395
+ * comes first — regardless of `pruneOn`. The ceiling keeps the setting
396
+ * reachable on huge-window models, where 0.9 of 1M would mean 900k tokens; it
397
+ * never binds on a model advertising 300k or less. An ADDITIONAL trigger on
398
+ * top of `pruneOn`, not a replacement.
394
399
  *
395
400
  * null (default) = disabled, preserving pre-feature behavior. Out-of-range
396
401
  * values (<= 0 or > 1) normalize to null.
@@ -402,7 +407,11 @@ export interface ContextPruneConfig {
402
407
  spillPreviewBytes: number;
403
408
  /**
404
409
  * Per-turn usage-fraction increase (0–1) that forces a flush, independent of
405
- * autoBudgetThreshold. null (default) = disabled. Out-of-range (<= 0 or > 1) normalizes to null.
410
+ * autoBudgetThreshold. The fraction is measured against the effective window
411
+ * `min(contextWindow, MAX_BUDGET_WINDOW)` (300_000), so the required growth is
412
+ * `delta * min(contextWindow, MAX_BUDGET_WINDOW)` tokens - e.g. 0.1 means +30k
413
+ * tokens in one turn on any model at or above 300k, and +20k on a 200k model.
414
+ * null (default) = disabled. Out-of-range (<= 0 or > 1) normalizes to null.
406
415
  */
407
416
  budgetTurnDelta: number | null;
408
417
  }