From bfd3c803633680289f72a1d67e5c7d6262e8a4ef Mon Sep 17 00:00:00 2001 From: Baudbot Date: Sat, 21 Feb 2026 18:38:45 -0500 Subject: [PATCH 1/4] extensions: add idle-compact for context compaction during idle periods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compacts conversation context at 40% capacity when the agent is truly idle — no active dev agents, no in-progress todos, no recent turns. This prevents shipping massive context on every heartbeat turn when default auto-compaction only triggers at ~95%. Configurable via IDLE_COMPACT_DELAY_MS, IDLE_COMPACT_THRESHOLD_PCT, and IDLE_COMPACT_ENABLED env vars. --- AGENTS.md | 1 + CONFIGURATION.md | 8 ++ pi/extensions/idle-compact.ts | 235 ++++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+) create mode 100644 pi/extensions/idle-compact.ts diff --git a/AGENTS.md b/AGENTS.md index 58544b3..8bb42e8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -39,6 +39,7 @@ pi/ heartbeat.ts periodic health check loop auto-name.ts session naming control.ts inter-session communication + idle-compact.ts compact context during idle periods (40% threshold) ... skills/ source of truth for agent skill templates control-agent/ orchestration agent diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 961c302..97172ec 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -164,6 +164,14 @@ Set during `setup.sh` / `baudbot install` via env vars: | `HEARTBEAT_FILE` | Path to heartbeat checklist file | `~/.pi/agent/HEARTBEAT.md` | | `HEARTBEAT_ENABLED` | Set to `0` or `false` to disable heartbeats | enabled | +### Idle Compaction + +| Variable | Description | Default | +|----------|-------------|---------| +| `IDLE_COMPACT_DELAY_MS` | Idle time before checking for compaction (milliseconds, min 60000) | `300000` (5 min) | +| `IDLE_COMPACT_THRESHOLD_PCT` | Context usage % to trigger compaction (10–90) | `40` | +| `IDLE_COMPACT_ENABLED` | Set to `0`, `false`, or `no` to disable idle compaction | enabled | + ### Bridge | Variable | Description | Default | diff --git a/pi/extensions/idle-compact.ts b/pi/extensions/idle-compact.ts new file mode 100644 index 0000000..73f67f9 --- /dev/null +++ b/pi/extensions/idle-compact.ts @@ -0,0 +1,235 @@ +/** + * Idle Compaction Extension + * + * Compacts the conversation context when the agent is truly idle — not just + * "no recent turns" but "no active work at all". This prevents compacting + * in the middle of a long-running dev agent task where we'd lose critical + * context (which repo, which todo, which Slack thread to reply to). + * + * Compaction triggers when ALL of these are true: + * 1. No turns for IDLE_DELAY_MS (default 5 min) + * 2. No active dev-agent-* sessions (checked via session-control sockets) + * 3. No in-progress todos (checked via todo files) + * 4. Context usage exceeds COMPACT_THRESHOLD_PCT of the context window + * + * Configuration (env vars): + * IDLE_COMPACT_DELAY_MS — idle time before checking (default: 300000 = 5 min) + * IDLE_COMPACT_THRESHOLD_PCT — context % to trigger (default: 40) + * IDLE_COMPACT_ENABLED — set to "0" or "false" to disable + */ + +import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent"; +import { readdirSync, readFileSync, readlinkSync, existsSync } from "node:fs"; +import { join } from "node:path"; +import { homedir } from "node:os"; +import * as net from "node:net"; + +const DEFAULT_IDLE_DELAY_MS = 5 * 60 * 1000; // 5 minutes +const DEFAULT_THRESHOLD_PCT = 40; +const MIN_IDLE_DELAY_MS = 60 * 1000; // 1 minute minimum + +const CONTROL_DIR = join(homedir(), ".pi", "session-control"); +const TODO_DIR = process.env.PI_TODO_PATH || join(".pi", "todos"); + +function getConfig() { + const envDelay = parseInt(process.env.IDLE_COMPACT_DELAY_MS || "", 10); + const idleDelayMs = Math.max( + MIN_IDLE_DELAY_MS, + Number.isFinite(envDelay) ? envDelay : DEFAULT_IDLE_DELAY_MS + ); + + const envThreshold = parseInt(process.env.IDLE_COMPACT_THRESHOLD_PCT || "", 10); + const thresholdPct = Number.isFinite(envThreshold) + ? Math.max(10, Math.min(90, envThreshold)) + : DEFAULT_THRESHOLD_PCT; + + const envEnabled = process.env.IDLE_COMPACT_ENABLED?.trim().toLowerCase(); + const enabled = envEnabled !== "0" && envEnabled !== "false" && envEnabled !== "no"; + + return { idleDelayMs, thresholdPct, enabled }; +} + +// --------------------------------------------------------------------------- +// Check for active dev agents by probing session-control sockets +// --------------------------------------------------------------------------- + +function isSocketAlive(socketPath: string): Promise { + return new Promise((resolve) => { + const socket = net.createConnection(socketPath); + const timeout = setTimeout(() => { + socket.destroy(); + resolve(false); + }, 300); + + socket.once("connect", () => { + clearTimeout(timeout); + socket.end(); + resolve(true); + }); + socket.once("error", () => { + clearTimeout(timeout); + resolve(false); + }); + }); +} + +async function hasActiveDevAgents(): Promise { + try { + const entries = readdirSync(CONTROL_DIR, { withFileTypes: true }); + + for (const entry of entries) { + if (!entry.name.endsWith(".alias")) continue; + const aliasName = entry.name.slice(0, -".alias".length); + if (!aliasName.startsWith("dev-agent-")) continue; + + // Resolve the symlink to find the socket file + try { + const target = readlinkSync(join(CONTROL_DIR, entry.name)); + const socketPath = join(CONTROL_DIR, target); + if (await isSocketAlive(socketPath)) { + return true; + } + } catch { + // Broken symlink — skip + } + } + + return false; + } catch { + // If we can't read the directory, assume no active agents + return false; + } +} + +// --------------------------------------------------------------------------- +// Check for in-progress todos +// --------------------------------------------------------------------------- + +function hasInProgressTodos(): boolean { + try { + const todoPath = TODO_DIR.startsWith("/") ? TODO_DIR : join(process.cwd(), TODO_DIR); + if (!existsSync(todoPath)) return false; + + const files = readdirSync(todoPath).filter((f) => f.endsWith(".md")); + + for (const file of files) { + try { + const content = readFileSync(join(todoPath, file), "utf-8"); + // Front matter is a JSON block at the start of the file + const jsonEnd = content.indexOf("\n\n"); + const jsonStr = jsonEnd > 0 ? content.slice(0, jsonEnd) : content; + const frontMatter = JSON.parse(jsonStr.trim()); + if (frontMatter.status === "in-progress" || frontMatter.status === "in_progress") { + return true; + } + } catch { + // Skip files we can't parse + } + } + + return false; + } catch { + return false; + } +} + +// --------------------------------------------------------------------------- +// Extension +// --------------------------------------------------------------------------- + +export default function idleCompactExtension(pi: ExtensionAPI): void { + let idleTimer: ReturnType | null = null; + let lastCtx: ExtensionContext | null = null; + let compacting = false; + let enabled = true; + let idleDelayMs = DEFAULT_IDLE_DELAY_MS; + let thresholdPct = DEFAULT_THRESHOLD_PCT; + + function cancelTimer() { + if (idleTimer) { + clearTimeout(idleTimer); + idleTimer = null; + } + } + + function armTimer() { + cancelTimer(); + if (!enabled || !lastCtx) return; + + idleTimer = setTimeout(() => { + idleTimer = null; + void checkAndCompact(); + }, idleDelayMs); + } + + async function checkAndCompact() { + if (!lastCtx || compacting) return; + + // Check 1: context usage above threshold? + const usage = lastCtx.getContextUsage(); + if (!usage || usage.tokens === null || usage.contextWindow === null) return; + + const pctUsed = (usage.tokens / usage.contextWindow) * 100; + if (pctUsed < thresholdPct) { + return; // Not worth compacting yet + } + + // Check 2: any active dev agents? + if (await hasActiveDevAgents()) { + // Dev agent running — don't compact, re-arm and check again later + armTimer(); + return; + } + + // Check 3: any in-progress todos? + if (hasInProgressTodos()) { + // Work in progress — don't compact, re-arm and check again later + armTimer(); + return; + } + + // All clear — compact + compacting = true; + lastCtx.compact({ + customInstructions: + "This is an idle-period compaction. Preserve all operational context: " + + "active session names, repo paths, Slack thread references (channel + thread_ts), " + + "any pending user requests, and memory file locations. Be thorough — the full " + + "conversation history won't be available after this.", + onComplete: () => { + compacting = false; + }, + onError: () => { + compacting = false; + // Re-arm to try again later + armTimer(); + }, + }); + } + + // ── Events ──────────────────────────────────────────────────────────────── + + pi.on("session_start", async () => { + const config = getConfig(); + enabled = config.enabled; + idleDelayMs = config.idleDelayMs; + thresholdPct = config.thresholdPct; + }); + + // When a turn starts, cancel any pending idle compaction — we're active + pi.on("turn_start", async () => { + cancelTimer(); + }); + + // When a turn ends, start the idle countdown + pi.on("turn_end", async (_event, ctx) => { + lastCtx = ctx; + if (enabled) { + armTimer(); + } + }); + + pi.on("session_shutdown", async () => { + cancelTimer(); + }); +} From a21b5cd5574f5f6a4c8fa2d0a0272cd49115ad62 Mon Sep 17 00:00:00 2001 From: Baudbot Date: Sat, 21 Feb 2026 18:43:52 -0500 Subject: [PATCH 2/4] extensions: improve socket cleanup in idle-compact to match control.ts pattern Address Greptile review: use removeAllListeners() and destroy() in error/timeout paths, matching the cleanup pattern in control.ts. --- pi/extensions/idle-compact.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pi/extensions/idle-compact.ts b/pi/extensions/idle-compact.ts index 73f67f9..8829a1e 100644 --- a/pi/extensions/idle-compact.ts +++ b/pi/extensions/idle-compact.ts @@ -57,19 +57,21 @@ function isSocketAlive(socketPath: string): Promise { return new Promise((resolve) => { const socket = net.createConnection(socketPath); const timeout = setTimeout(() => { + socket.removeAllListeners(); socket.destroy(); resolve(false); }, 300); - socket.once("connect", () => { + const cleanup = (alive: boolean) => { clearTimeout(timeout); - socket.end(); - resolve(true); - }); - socket.once("error", () => { - clearTimeout(timeout); - resolve(false); - }); + socket.removeAllListeners(); + if (alive) socket.end(); + else socket.destroy(); + resolve(alive); + }; + + socket.once("connect", () => cleanup(true)); + socket.once("error", () => cleanup(false)); }); } From 395555312920999be72af5daa5ccc47cb3d8a52a Mon Sep 17 00:00:00 2001 From: Baudbot Date: Sat, 21 Feb 2026 18:50:17 -0500 Subject: [PATCH 3/4] extensions: lower idle-compact threshold to 25% and prioritize recency in compaction When truly idle (no dev agents, no todos, no recent turns), there's no reason to keep a fat context. Lower default from 40% to 25%. Update compaction instructions to prioritize recent conversations, task results, and decisions over older history. --- CONFIGURATION.md | 2 +- pi/extensions/idle-compact.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 97172ec..025ecdc 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -169,7 +169,7 @@ Set during `setup.sh` / `baudbot install` via env vars: | Variable | Description | Default | |----------|-------------|---------| | `IDLE_COMPACT_DELAY_MS` | Idle time before checking for compaction (milliseconds, min 60000) | `300000` (5 min) | -| `IDLE_COMPACT_THRESHOLD_PCT` | Context usage % to trigger compaction (10–90) | `40` | +| `IDLE_COMPACT_THRESHOLD_PCT` | Context usage % to trigger compaction (10–90) | `25` | | `IDLE_COMPACT_ENABLED` | Set to `0`, `false`, or `no` to disable idle compaction | enabled | ### Bridge diff --git a/pi/extensions/idle-compact.ts b/pi/extensions/idle-compact.ts index 8829a1e..13878b2 100644 --- a/pi/extensions/idle-compact.ts +++ b/pi/extensions/idle-compact.ts @@ -14,7 +14,7 @@ * * Configuration (env vars): * IDLE_COMPACT_DELAY_MS — idle time before checking (default: 300000 = 5 min) - * IDLE_COMPACT_THRESHOLD_PCT — context % to trigger (default: 40) + * IDLE_COMPACT_THRESHOLD_PCT — context % to trigger (default: 25) * IDLE_COMPACT_ENABLED — set to "0" or "false" to disable */ @@ -25,7 +25,7 @@ import { homedir } from "node:os"; import * as net from "node:net"; const DEFAULT_IDLE_DELAY_MS = 5 * 60 * 1000; // 5 minutes -const DEFAULT_THRESHOLD_PCT = 40; +const DEFAULT_THRESHOLD_PCT = 25; const MIN_IDLE_DELAY_MS = 60 * 1000; // 1 minute minimum const CONTROL_DIR = join(homedir(), ".pi", "session-control"); @@ -194,10 +194,10 @@ export default function idleCompactExtension(pi: ExtensionAPI): void { compacting = true; lastCtx.compact({ customInstructions: - "This is an idle-period compaction. Preserve all operational context: " + - "active session names, repo paths, Slack thread references (channel + thread_ts), " + - "any pending user requests, and memory file locations. Be thorough — the full " + - "conversation history won't be available after this.", + "Prioritize recency: the most recent conversations, task results, and decisions " + + "are more important than older history. Preserve active Slack thread references " + + "(channel + thread_ts), in-progress work context, recent user requests, and " + + "current operational state. Older completed tasks can be summarized briefly.", onComplete: () => { compacting = false; }, From b373e5897630c588194c7dd70c66ffb5cea358f8 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sat, 21 Feb 2026 18:56:55 -0500 Subject: [PATCH 4/4] extensions: address idle-compact review fixes --- pi/extensions/idle-compact.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pi/extensions/idle-compact.ts b/pi/extensions/idle-compact.ts index 13878b2..db367b4 100644 --- a/pi/extensions/idle-compact.ts +++ b/pi/extensions/idle-compact.ts @@ -56,13 +56,11 @@ function getConfig() { function isSocketAlive(socketPath: string): Promise { return new Promise((resolve) => { const socket = net.createConnection(socketPath); - const timeout = setTimeout(() => { - socket.removeAllListeners(); - socket.destroy(); - resolve(false); - }, 300); + let settled = false; const cleanup = (alive: boolean) => { + if (settled) return; + settled = true; clearTimeout(timeout); socket.removeAllListeners(); if (alive) socket.end(); @@ -70,6 +68,8 @@ function isSocketAlive(socketPath: string): Promise { resolve(alive); }; + const timeout = setTimeout(() => cleanup(false), 300); + socket.once("connect", () => cleanup(true)); socket.once("error", () => cleanup(false)); }); @@ -121,7 +121,12 @@ function hasInProgressTodos(): boolean { const jsonEnd = content.indexOf("\n\n"); const jsonStr = jsonEnd > 0 ? content.slice(0, jsonEnd) : content; const frontMatter = JSON.parse(jsonStr.trim()); - if (frontMatter.status === "in-progress" || frontMatter.status === "in_progress") { + if ( + frontMatter.status === "in-progress" || + frontMatter.status === "in_progress" || + (typeof frontMatter.assigned_to_session === "string" && + frontMatter.assigned_to_session.trim().length > 0) + ) { return true; } } catch { @@ -169,7 +174,8 @@ export default function idleCompactExtension(pi: ExtensionAPI): void { // Check 1: context usage above threshold? const usage = lastCtx.getContextUsage(); - if (!usage || usage.tokens === null || usage.contextWindow === null) return; + if (!usage || usage.tokens === null || usage.contextWindow === null || usage.contextWindow <= 0) + return; const pctUsed = (usage.tokens / usage.contextWindow) * 100; if (pctUsed < thresholdPct) {