feat(agent): Pi agent harness improvements — compaction, retry, parallel tools, diff output, bash cleanup#24
Merged
Merged
Conversation
Introduce automatic retry/backoff for transient AI endpoint errors and surface retry events to the renderer. Add AgentToolContext to consolidate per-session infrastructure (cwd, db, req, workspacePath, sessionId, send, getWin) and pass it to tools and subagents. Implement a sliding-window context pruner (with configurable contextWindow) and transformContext hook to trim history when approaching the model context limit. Switch tool execution to run in parallel while preserving message ordering, and simplify tool-call plumbing to use toolCtx. Update spawnSubagentTool and IPC handlers to use the new context and emit pi-agent:retry events; preload and PiAgentPane now handle/display retry/backoff state. Misc: propagate lastPromptTokens from usage chunks, add configurable maxRetries/baseRetryDelayMs, and various small refactors to support the above.
Add LLM-based context compaction (electron/lib/compaction.ts) and wire it into the Pi agent loop; emit pi-agent:compact events and expose onCompact via preload so the UI shows a "Compacting context…" indicator. Implement automatic transient-error retries with exponential backoff and onRetry IPC, and run tool calls in parallel (Promise.all). Enhance edit tool to return a unified diff, and improve bash tooling to spawn detached process groups, track PIDs, and kill tracked children on app quit. Update runAgentLoop signature usages (AgentToolContext) and tests, and apply related wiring in electron/ipc/pi-agent.ts, electron/main.ts, electron/preload.ts, electron/lib/*, and src/components/agent/PiAgentPane.tsx.
Update CONTRIBUTING.md and README.md to reflect recent pi-agent changes: runAgentLoop now supports parallel tool execution (Promise.all), an onRetry callback and pi-agent:retry events for automatic retrying of transient API errors, a transformContext hook and an AgentToolContext grouping per-session state. Add documentation for electron/lib/compaction.ts which provides async LLM-based context compaction (with sliding-window fallback, background summarisation, and pi-agent:compact events) and notes about configuration knobs (COMPACT_THRESHOLD, KEEP_RECENT_TURNS). Clarify retry behavior (isRetryable, AgentLLMConfig.maxRetries default 3, baseRetryDelayMs default 2000ms), per-file serialization via file-mutex, and update the README status bar messaging for compaction and retry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Closes the most impactful gaps between Cairn's Pi agent harness and the Pi monorepo coding agent. Seven targeted improvements land in this PR — each independently mergeable but shipped together as a cohesive reliability and performance upgrade.
Type of change
Checklist
npm run type-checkpassesnpm run lintpassesnpm testpassesnpm run test:e2epasses (run before merging UI changes or cutting a release)var(--accent),var(--text-primary), etc.)text-[Npx]pixel font classes — rem equivalents only (text-[0.714rem],text-xs, etc.)handle()and returnIpcResult<T>schema.tsmcp-server.tschanges use inlined SQL only — no import fromqueries.tsWhat changed
Context compaction (
electron/lib/compaction.ts— new file)When token usage hits 80% of the context window, the oldest messages are summarised with a non-streaming LLM call. Compaction is async fire-and-forget — the current turn uses a sliding-window fallback; subsequent turns use the cached summary. A
"Compacting context…"badge appears in the Pi agent status bar while the call is in flight.Automatic retry (
pi-agent-loop.ts)Retryable HTTP errors (429/5xx, overloaded/rate-limit body patterns) trigger up to 3 retries with 2 s / 4 s / 8 s backoff. A countdown badge shows in the status bar. Abort cancels any in-progress sleep.
Parallel tool execution (
pi-agent-loop.ts)All tool calls in a single assistant turn now run via
Promise.all. Results appended in source order. Per-file serialisation handled transparently by the existing file mutex.Edit tool diff output (
coding-tools/edit.ts)The
edittool now returns a unified diff alongside the success string. Implemented inline with LCS — no new dependencies.Bash process-group cleanup (
coding-tools/bash.ts,main.ts)Bash processes spawn with
detached: trueon Unix. PIDs tracked and killed as a process group onbefore-quit.System prompt fix (
pi-agent-loop.ts)System prompt now sent as the
system:field in the request body, not prepended as ausermessage.AgentToolContextrefactor (pi-agent-loop.ts)runAgentLoopreduced from 11 positional parameters to 6 by grouping DB/IPC/path context intoAgentToolContext. Tests updated accordingly.Notes for reviewer
npm run test:e2enot run — no UI flows changed beyond the status bar text additions inPiAgentPane.contextWindowused for the compaction threshold defaults to128_000if not set inAgentLLMConfig. This can be made configurable via Settings in a follow-up.