Skip to content

feat(agent): Pi agent harness improvements — compaction, retry, parallel tools, diff output, bash cleanup#24

Merged
ddutchie merged 3 commits into
mainfrom
ddutchie/agentimprovements
May 10, 2026
Merged

feat(agent): Pi agent harness improvements — compaction, retry, parallel tools, diff output, bash cleanup#24
ddutchie merged 3 commits into
mainfrom
ddutchie/agentimprovements

Conversation

@ddutchie
Copy link
Copy Markdown
Owner

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

  • New feature
  • Refactor / code quality

Checklist

  • npm run type-check passes
  • npm run lint passes
  • npm test passes
  • npm run test:e2e passes (run before merging UI changes or cutting a release)
  • No hardcoded colours — CSS variables only (var(--accent), var(--text-primary), etc.)
  • No text-[Npx] pixel font classes — rem equivalents only (text-[0.714rem], text-xs, etc.)
  • New IPC handlers wrapped in handle() and return IpcResult<T>
  • New DB migrations appended (not edited) in schema.ts
  • mcp-server.ts changes use inlined SQL only — no import from queries.ts

What 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 edit tool 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: true on Unix. PIDs tracked and killed as a process group on before-quit.

System prompt fix (pi-agent-loop.ts)
System prompt now sent as the system: field in the request body, not prepended as a user message.

AgentToolContext refactor (pi-agent-loop.ts)
runAgentLoop reduced from 11 positional parameters to 6 by grouping DB/IPC/path context into AgentToolContext. Tests updated accordingly.

Notes for reviewer

  • npm run test:e2e not run — no UI flows changed beyond the status bar text additions in PiAgentPane.
  • Compaction is fire-and-forget by design: the first turn above the threshold always uses the sliding-window fallback. This avoids blocking the agent loop. The LLM summary is applied from the second over-threshold turn onward.
  • The contextWindow used for the compaction threshold defaults to 128_000 if not set in AgentLLMConfig. This can be made configurable via Settings in a follow-up.

ddutchie added 3 commits May 10, 2026 18:24
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.
@ddutchie ddutchie merged commit 0cad30c into main May 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant