# prune ![Prune recall](assets/prune.gif) Deterministic Pi session compaction with searchable recall. ## Install / load `prune` is loaded through the root `pi-ext` package. See the [root README](../../README.md). ## Commands / tools / settings Commands: | Command | Description | | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `/prune` | Starts Pi compaction through `ctx.compact()`. With `prune.overrideDefault` enabled, `session_before_compact` supplies the prune summary. Command args are not read. | | `/prune-recall [all]` | Searches session history and shows formatted matches. Default scope is current branch lineage. A trailing `all` searches all entries. Command limit is `10`. | Tool: | Tool | Arguments | Behavior | | -------------- | ------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | | `prune_recall` | `query: string`, `scope?: "lineage" \| "all"`, `limit?: number`, `expand?: number[]` | Searches session entries and returns scored matches. Default scope: `lineage`. Default limit: `5`. `limit` max: `20`. | `prune_recall` is registered at load time, but it is active for the agent only after a prune compaction exists in the current branch. ```typescript prune_recall({ query: "auth token" }) prune_recall({ query: "auth token", scope: "all" }) prune_recall({ query: "auth token", expand: [12, 14] }) ``` Hooks: | Hook | Behavior | | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `session_before_compact` | Returns prune compaction when `prune.overrideDefault` is true. Hard-caps raw kept tail at ~32k tokens when a safe boundary exists. Includes `firstKeptEntryId`, `tokensBefore`, and details `{ kind: "prune", version: 1, sourceEntriesProcessed, tailKept, splitPrefixMessages }`. | | `session_start` | Enables/disables `prune_recall` from current branch state. | | `session_tree` | Enables/disables `prune_recall` from current branch state. | | `session_compact` | Enables `prune_recall` after a prune compaction, logs in debug mode, and notifies UI when present. | Settings: | Setting | Default | Behavior | | ----------------------- | ------: | --------------------------------------------------------------------- | | `prune.overrideDefault` | `true` | Return prune compaction from `session_before_compact`. | | `prune.sectionLimit` | `8` | Max lines per structured section in formatted output. | | `prune.transcriptLimit` | `60` | Max transcript lines kept in summary tail. | | `prune.toolCapPerTurn` | `8` | Max tool-call transcript lines per user turn. Errors are always kept. | | `prune.debug` | `false` | Log prune stats, skipped roles, and compaction status. | ## Behavior - With `prune.overrideDefault` enabled, compaction is local and deterministic. The prune hook does not call an LLM. - `/prune` starts normal Pi compaction; the hook replaces the default summary only when enabled. - Settings are read once from `settingsManager.getSettings().prune` when the extension loads. - Pi roles mapped for compaction: `user`, `assistant`, `tool`, `toolResult`, `bashExecution`, `branchSummary`, `compactionSummary`. `custom` is skipped. - Assistant `thinking` and `image` parts are skipped during compaction. Assistant `toolCall` parts become transcript tool lines. - Extracted sections: - `[Session Goal]`: first user task lines plus later scope changes. - `[Files And Changes]`: paths inferred from read/write/edit tool args and simple bash path matches. - `[Outstanding Context]`: tool errors and recent blocker/failure lines. - `[User Preferences]`: explicit user preference lines. - `[Continue Now]`: deterministic resume block from the transcript tail. - Empty structured sections are omitted. - Transcript entries keep stable `#N` source refs from the compaction input order. - Tool calls render as `* tool "arg" (#N)` when a command/path is available, otherwise `* tool (#N)`. - Bash tool commands are shortened: leading `cd ... &&` is removed, and a trailing simple pipe such as `tail`/`head` is removed. - Tool errors render as `[tool_error] [ERROR] tool (#N)`. - Existing prune summaries are merged with fresh summaries. Goals/preferences/files are deduped, context and continue block are refreshed, transcript keeps previous entries before fresh entries. - Prune compactions are detected by `details.kind === "prune"`; older hook summaries are also detected when they contain the prune recall note. - Recall searches current branch lineage by default. `scope: "all"` searches all session entries. - Recall includes message entries, compaction summaries, and `custom_message` content. - Recall message text includes text parts, tool call names/args, and thinking parts. Images are ignored. - Regex-looking query strings are tried as case-insensitive regex. Invalid regex falls back to term matching. - Regex matches score `10`; term matches score by matched-term count. - `expand` returns exact `#N` entries even when the query does not match. ## Output Format ```text [Session Goal] - Fix the auth bug - [Scope change] - also update token refresh logic [Files And Changes] - Modified: session.ts [Outstanding Context] - lint still failing [Continue Now] - Current request: Fix the auth bug (#1) - Last action: read src/auth/session.ts (#3) - Next: continue current request; use prune_recall before redoing old work. --- [user] Fix the auth bug (#1) * read "src/auth/session.ts" (#3) --- prune: 10 entries summarized; before 18.9k tok; raw kept 3 entries; split-prefix 0 --- Use prune_recall to search for prior work, decisions, and context from before this summary. Do not redo work already completed. ``` ## Testing ```bash bun install bun test # fast default: unit + harness, no network bun run test:unit # pure funcs only bun run test:harness # pi-test-harness extension wiring bun run test:watch # watch fast tests bun run test:coverage # coverage for fast tests bun run test:real # real MiniMax integ test, costs quota bun run test:all # fast tests + real MiniMax ``` Notes: - `bun test` intentionally excludes `__tests__/real/`. - `test:real` uses MiniMax, temp cwd/agentDir/session, `noExtensions: true`, and only prune loaded.