Skip to content

feat(agent): skill system, on-demand compaction, unified truncation, runSession helper, shouldStop hook, /compact command, settings preview#25

Merged
ddutchie merged 2 commits into
mainfrom
ddutchie/agentimprovement2
May 11, 2026
Merged

feat(agent): skill system, on-demand compaction, unified truncation, runSession helper, shouldStop hook, /compact command, settings preview#25
ddutchie merged 2 commits into
mainfrom
ddutchie/agentimprovement2

Conversation

@ddutchie
Copy link
Copy Markdown
Owner

@ddutchie ddutchie commented May 10, 2026

What does this PR do?

Brings the pi-agent harness to parity with the Pi monorepo coding agent across two independently-useful areas:

v1.4.0 refactorsrunSession() helper eliminates ~120 lines of duplicated IPC handler code; truncateOutput() replaces ad-hoc byte/line capping across all coding tools; AgentLoopCallbacks.shouldStop? hook enables clean early exit; on-demand compaction via /compact slash command backed by pi-agent:compact-now; compaction state persisted across multi-turn sessions.

Skill system — the agent discovers SKILL.md files by walking from the code directory up to the git root, checking .cairn/skills/, .opencode/skills/, .cline/skills/, .claude/skills/, and .agents/skills/ at every ancestor. Metadata is injected into the system prompt as <available_skills> XML; full body loaded on demand via a skill tool call. Compatible with OpenCode, Cline, and Claude Code skill format. Settings → Coding Agents shows discovered skills and the full assembled system prompt with a mode toggle and copy button.

Type of change

  • Bug fix
  • New feature
  • Refactor / code quality
  • Docs / changelog
  • Tests

Screenshots / recording

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

Notes for reviewer

  • discoverSkills(cwd) walks up to the git root (stops at .git/) — skills placed at the repo root are always found even when codeDirectory is a subdirectory like packages/web/
  • makeSkillToolDefinition(skills) is dynamic — the tool description lists available skill names, so the model knows what to ask for without reading the body
  • pi-agent:preview-prompt is a ipcMain.handle (invoke, not fire-and-forget) — returns { systemPrompt, skills } synchronously for the settings panel
  • Compaction transformer stored on PiAgentSession (not rebuilt per prompt) so cachedSummary survives multi-turn sessions; pi-agent:clear resets it
  • runSession() is the single mandatory path to runAgentLoop in production; tests call runAgentLoop directly and are unaffected
  • shouldStop? fires after tool results, before the next LLM call — triggers onDone, not onError

ddutchie added 2 commits May 10, 2026 19:43
Introduce on-demand context compaction and a shared truncation utility, refactor agent loop wiring, and update coding tools/UI accordingly.

- New truncation library (electron/lib/truncation.ts) with truncateOutput, defaults and structured TruncationResult; replaces ad-hoc byte/line slicing across tools.
- /compact slash command: pi-agent:compact-now IPC handler, compactNow API in compaction.ts, and renderer + preload hooks to trigger/display compact results (PiAgentPane, preload.ts, PiMessageBubble rendering for system messages).
- Extracted runSession() in electron/ipc/pi-agent.ts to centralise compaction setup, IPC callbacks, and runAgentLoop invocation; persist compactionTransformer on PiAgentSession and reset it on pi-agent:clear.
- Compaction fixes: buildCompactionTransformer now reads session.abortCtrl.signal live; compactNow awaits LLM summary and emits pi-agent:compact-result.
- runAgentLoop additions: shouldStop hook (AgentLoopCallbacks) to allow semantic stop conditions; documentation/invariant advising callers to use runSession in Electron main process.
- Update coding tools (bash, grep, find, ls, read) to use truncateOutput and DEFAULT limits and standardised hint formatting; small UX/format adjustments for streamed hints.

These changes unify output truncation, enable explicit user-triggered compaction, preserve compaction cache across prompts, remove duplicated IPC wiring, and add a clean semantic loop-stop hook.
Introduce a SKILL.md-based skill system and UI preview for the pi-agent.

- Add electron/lib/skills.ts: discoverSkills(), loadSkill(), renderSkillsXml() and types (SkillMeta, SkillContent). Scans project-local and global skill directories, validates SKILL.md frontmatter, and lists co-located resources. Metadata is token-efficiently injected into prompts; full bodies are lazy-loaded.
- Add electron/lib/coding-tools/skill.ts: skill tool implementation and dynamic makeSkillToolDefinition(skills). Exposes SkillArgs and integrates with coding tools barrel export.
- Integrate skills into agent loop and prompts: pi-agent-loop gains skills in AgentToolContext, conditionally includes the skill tool, allows "skill" in plan/execute, and routes tool calls to skillTool; pi-agent-prompt injects a <available_skills> XML block into both plan and execute prompts.
- IPC & preload: electron/ipc/pi-agent.ts calls discoverSkills() and renderSkillsXml() when building prompts, passes skills to tool context, and adds pi-agent:preview-prompt handler; preload exposes piAgent.previewPrompt to the renderer.
- UI: src/components/settings/AgentSettings.tsx adds a Skills & System Prompt preview section (skill list, mode toggle, refresh, collapsible prompt with copy), plus SkillRow and PromptPreview components.
- Update changelog to document the feature and touched files.

This implements a compatible, token-conscious skill workflow (OpenCode/Cline/Claude Code formats supported) and a renderer preview for developers to inspect available skills and the assembled system prompt.
@ddutchie ddutchie changed the title feat(agent): on-demand compaction, unified truncation, runSession helper, shouldStop hook, /compact slash command feat(agent): skill system, on-demand compaction, unified truncation, runSession helper, shouldStop hook, /compact command, settings preview May 11, 2026
@ddutchie ddutchie merged commit 1be1920 into main May 11, 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