fix(compact): move force tier to agent_settled
Manual compaction (ctx.compact) aborts the active run and never resumes the interrupted turn (Pi 1.5.2 agent-session.js:2321-2327), so triggering it from turn_end destroyed in-progress agent work. The force tier now fires at agent_settled, where ctx.isIdle() is guaranteed and compaction aborts nothing; Pi's built-in mid-run threshold check stays the actor and preserves runs. Also: the settled handler awaits compaction completion so prompts deferred from settled handlers cannot race an in-flight compaction; per-field config validation falls back to defaults (null token values previously zeroed thresholds via Math.min, strings silently NaN-disabled them); the cached token count is cleared on session_compact so stale pre-compaction counts cannot re-trigger. Adds harness.ts: 15-test mock harness (node --test, type stripping, zero deps) covering both regressions, locks, cooldown, validation, and cycle resets. README corrected accordingly.
This commit is contained in:
1 parent
2c6d98995a
commit
a6cfb51626
3 files changed
+332
-71
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# Smart Compact
|
||||
|
||||
**Two-tier context management for [Pi](https://github.com/earendil-works/pi): a gentle heads-up to the agent when context fills up, and a deliberate compaction at a clean turn boundary before the window is exhausted. Designed to compose with pi-lcm and pi-dcp instead of fighting them.**
|
||||
**Two-tier context management for [Pi](https://github.com/earendil-works/pi): a gentle heads-up to the agent when context fills up, and a deliberate compaction when the session settles, well before the window is exhausted. Designed to compose with pi-lcm and pi-dcp instead of fighting them.**
|
||||
|
||||
## Why Smart Compact
|
||||
|
||||
@@ -14,9 +14,9 @@ Smart Compact adds the two missing tiers:
|
||||
| Tier | Trigger | Action |
|
||||
|------|---------|--------|
|
||||
| **Warn** | `min(warnTokens, warnFraction × window)` — default 100k or 55% | One-shot `custom_message` into the context (invisible in the UI, ~60 tokens): prefer targeted searches over full files, consolidate findings, keep key facts stated explicitly. |
|
||||
| **Force** | `min(forceTokens, forceFraction × window)` — default 200k or 80% | `ctx.compact()` at a clean turn boundary, with a UI notification. Always fires *before* Pi's built-in threshold compaction. |
|
||||
| **Force** | `min(forceTokens, forceFraction × window)` — default 200k or 80% | `ctx.compact()` at the `agent_settled` boundary — the first moment no run is active, so nothing in progress is aborted. With a UI notification. |
|
||||
|
||||
The fractional floors keep both tiers correctly ordered on any window size. On a 128k model: warn ≈ 70k, force ≈ 102k, built-in emergency at 112k. On a 200k model: warn 100k, force 160k, built-in at 184k. The absolute values act as ceilings for very large windows.
|
||||
The fractional floors keep both tiers correctly ordered on any window size. On a 128k model: warn ≈ 70k, force ≈ 102k, built-in emergency at 112k. On a 200k model: warn 100k, force 160k, built-in at 184k. The absolute values act as ceilings for very large windows. When `(1 − forceFraction) × window > reserveTokens` (defaults: windows above ~82k) the force threshold sits below the built-in trigger, but execution order is not guaranteed: a run that crosses both thresholds before settling lets Pi's built-in check compact first — which is safe, it preserves the run.
|
||||
|
||||
## Compatibility
|
||||
|
||||
@@ -30,13 +30,13 @@ This is the design center. Smart Compact deliberately touches only the events th
|
||||
|
||||
[pi-dcp](https://www.npmjs.com/package/@davecodes/pi-dcp) nudges the model (30k soft / 70k hard by default) toward its `compress` tool, which shrinks old tool results in place. Smart Compact's warn message uses deliberately different language — general frugality and advance notice, not compress-the-tool — so the model doesn't get two copies of the same instruction. It also copies DCP's two hardening patterns:
|
||||
|
||||
- **Null-token window.** `ctx.getContextUsage().tokens` returns `null` immediately after compaction (before the first post-compaction response). The last non-null value is cached and used as a fallback.
|
||||
- **Null-token window.** `ctx.getContextUsage().tokens` returns `null` immediately after compaction (before the first post-compaction response). The last non-null value is cached and used as a fallback — and the cache is cleared on `session_compact`, so a stale pre-compaction count cannot re-trigger the force tier.
|
||||
- **Cycle reset.** State resets on `session_compact`, so the warn can fire once per compaction cycle rather than once per session. This also catches `/compact`, `/lcm compact`, and Pi's own threshold/overflow compactions.
|
||||
|
||||
## Guards
|
||||
|
||||
- Warn fires once per cycle (`warnedThisCycle` flag).
|
||||
- Force is once per cycle with an in-flight lock, plus a 60s cooldown after a failed compaction. Pi's overflow recovery remains the final backstop.
|
||||
- Force fires only at `agent_settled` (never mid-run), once per cycle with an in-flight lock, plus a 60s cooldown after a failed compaction. Mid-run, Pi's built-in threshold check and overflow recovery remain the actors and preserve the run.
|
||||
- `continue: true` on the warn entry only forces a model request if the run was ending anyway — mid-run it rides along with the next request free.
|
||||
|
||||
## Configuration
|
||||
@@ -81,10 +81,11 @@ Pi discovers `*/index.ts` one level deep, following the symlink. Reload with `/r
|
||||
|
||||
- **Warn is a `custom_message`, not a system-prompt addendum.** Dynamic prompt addendum changes break provider prompt caching; an in-conversation message costs ~60 tokens once and is a valid compaction cut point.
|
||||
- **Force uses `ctx.compact()` rather than returning a compaction from `session_before_compact`** so pi-lcm (or any other compaction-owning extension) stays in charge of summary generation.
|
||||
- **Force runs at `agent_settled`, not `turn_end`.** `ctx.compact()` aborts any active run and never resumes the interrupted turn, so triggering it mid-run would destroy agent work. At `agent_settled` the session is idle (`ctx.isIdle()` is guaranteed) and compaction aborts nothing.
|
||||
- **Thresholds are computed per turn** from `ctx.getContextUsage()`, so model switches mid-session (e.g. to a different window size) take effect immediately.
|
||||
|
||||
## Verification
|
||||
|
||||
- Loads clean under Pi 1.5.2 (`pi -p` smoke test).
|
||||
- 16/16 mock-harness assertions: warn fires once per cycle at the effective threshold; force calls `ctx.compact` before the built-in trigger on both 200k and 128k windows; in-flight and failure-cooldown guards; null-token fallback; cycle reset on `session_compact`; status command output.
|
||||
- 14-test committed harness: `node --test harness.ts` (Node ≥ 22.18 with type stripping). Covers warn-once-per-cycle at the effective threshold, force only at `agent_settled` (never from `turn_end`, not when busy), awaiting compaction completion in the settled handler, the in-flight lock and failure cooldown, the stale-token guard after `session_compact`, per-field config validation (`null`/string/zero/oversized values), cycle reset, `enabled: false`, and the status command.
|
||||
- Live-session validation of the first real 100k crossing is still pending — the warn message is a single string in `index.ts` if the wording needs tuning.
|
||||
Reference in new issue
Block a user