Skip to content
nurmdrafiPublic

About

A performance-focused Pi coding-agent harness with native Rust tools, fail-closed permission gates, and token-efficient context management.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

pi-native

A measured performance harness for the official Pi coding agent: native Rust tools, fail-closed permission gates, and token-efficient context management layered on top of unmodified Pi.

pi-native logo

stars CI tests release last commit license XERJ Corpus Hub contributor pi coding agent macOSLinux rust tmux skills extensions issues contributors
node

pi-native is not a rewrite of Pi Coding Agent. It is a performance layer on top of the official agent, earendil-works/pi-coding-agent. The agent you run stays official; pi-native never forks it.

Nothing is migrated or replaced. Existing sessions, skills, extensions, and auth keep working where they are: pi-native drops in as the ~/.pi/agent config directory and swaps the tool layer underneath.

Same Pi, much faster tools + much less context bloat. Both halves are measured; every number below comes from the benchmark harness in this repo.

Why this exists

Coding-agent configs decay: always-on prompt bloat, machine-specific paths, undocumented setup rituals. This harness treats the config as an engineered product: one ~/.pi/agent/ directory configures a coding-agent workstation on any macOS or Linux machine — lean behavioral core, skills loaded only on invocation, always-on extensions enforcing tool-call rules, permanent prompt prefix under a kilotoken. Every harness change ships with a before/after number.

What it does

  • Native Rust tools (piread, piedit, pibash, piwrite, pidx) replace read, edit, bash, write, and session-log parsing — same parameters, so a tuned pi setup runs unchanged (docs/native-tools.md)
  • Every tool call crosses a permission gate before it runs (docs/rust-tools-architecture.md)
  • Context stays small by default: windowed reads, bounded diffs, hard output caps (docs/native-tools.md)
  • Dashboard tracks cost and health live: overview, sessions, subagents, permission-gate, and live tabs over SSE, with token spend, cache hit rate, tool/subagent/gate metrics, and per-session USD budget alerts (dashboard/)
  • Subagents run sandboxed with the gate re-enabled inside; each child declares its write scope, so parallel work never clobbers files (docs/subagents.md)
  • /handoff <goal> drafts a compaction-aware context-transfer prompt and seeds a new session with it; auto-offers once per session at the turn cadence limit (extensions/README.md)
  • Compact editor caps the prompt-editor height so a long paste cannot push the transcript off screen (extensions/README.md)
  • Self-heal repairs the PATH pin, native binaries, and the PCRE2 rg on session start after a pull or pi update (extensions/README.md)
  • Zero init: clone into ~/.pi/agent; every later pull re-checks the setup itself (docs/zero-init.md)
  • No silent fallback: native tools always execute the Rust binary; terminal failure throws an error plus one stderr line; built-ins only by explicit configuration (docs/native-tools.md)

Benchmarks

Measured p50, built-in Pi tools vs Pi Native Rust tools, matched workloads. Bars are exactly proportional within each row (each row scaled to its own max); rows trading speed for smaller context say so in the chart notes:

Chart, two panels: dispatch latency p50 in ms, five workloads, Pi Native 1.4x to 3.5x faster than Pi Coding Agent; and throughput in MB/s, wins from 1.88x to 18x with the pibash pipe 0.70x and pidx cold-index 0.68x losses kept visible in grey

Chart, three panels: peak RSS in KiB, 48x to 171x lower on three identical workloads; model-visible context bytes, 273,312 before vs 15,569 after, minus 94.3 percent; and pidx session-log re-scan, cold 2.1 s vs warm 28 ms with ~17x payback

The all-tools chart regenerates with node scripts/bench-charts.mjs — output defaults to docs/assets/benchmarks/all-tools.svg; set PI_BENCH_CHART_OUT to write it elsewhere.

Quick start

Prereqs: git; Rust (cargo); macOS or Linux; tmux only for subagents.

# 1. Node ≥ 22.19 (`.nvmrc` pins 24)
nvm install 24

# 2. Install pi (official installer; pins deps, adds `pi update`)
curl -fsSL https://pi.dev/install.sh | sh

# 3. Clone the harness into pi's config dir
git clone https://github.com/nurmdrafi/pi-native.git ~/.pi/agent

# Per machine: copy extras from an existing machine
scp <your-other-machine>:~/.agents/skills/* ~/.pi/agent/skills/  # private skills, not in the repo
scp <your-other-machine>:~/.pi/agent/auth.json ~/.pi/agent/      # secrets, or /login per provider inside pi

After a pull that merges or rebases, Husky runs scripts/after-pull.sh to repair missing project dependencies, refresh builds, and check setup. Install system prerequisites separately. If Git reports "Already up to date", run the script directly to repair setup. See Rust tool development and web research setup.

Verify and start pi:

$ ~/.pi/agent/bin/pidoc
# expect 0 FAIL (warnings OK; e.g. no cargo yet means session-audit uses its Node fallback)

$ pi
# First run regenerates npm/, git/, models-store.json automatically; bin/ came from bootstrap.

Subagents need tmux: brew install tmux (macOS) or sudo apt install tmux (Linux).

Add bin/ to PATH so terminals resolve the same tools (zsh: ~/.zshenv; bash: ~/.bashrc):

export PATH="$HOME/.pi/agent/bin:$PATH"

bin/ is gitignored and regenerated: rg (PCRE2 build), piread, piedit, pibash, piwrite, pidx, fd, pidoc. pidoc verifies all of them; check the rg-pcre2, piread, and shell-path rows.

Architecture

pi-native architecture

▶ Interactive view: pi-native architecture

Every tool call: the extension intercepts the built-in call, the permission gate authorizes it fail-closed, the Rust binary executes it — persistent daemon (--serve, NDJSON over stdio) on the hot path, one-shot spawn as fallback. Terminal failure throws a tool error plus one stderr line, never a silent built-in run. Tests pin daemon and one-shot paths byte-identical; details.via records the serving path. Deep dive: docs/rust-tools-architecture.md.

Repository map: extensions/ integrates with Pi; rust-tools/ implements the native binaries. See the test index for checks, including those for the separate dashboard app. The benchmark guide covers runners and recorded results. rules/ holds written agent guidance loaded on demand; skills/ and docs/ hold task-specific workflows and references.

Skills & subagents

Skills are manual (/skill:<name>) and validator-enforced; bodies enter context only when invoked (docs/skills.md).

Each spawn declares its write scope: overlapping spawns queue FIFO behind the owner; disjoint work runs fully parallel — no hand-planning around file collisions. Subagents run on tmux, gated off by default, sandboxed with the permission gate re-enabled inside. Per-child budgets (wall-clock, turns, USD) stop at the limit; only summaries return to the parent; the dashboard tracks each child's health and cost live. Deep dives: docs/subagents.md · docs/subagent-orchestration.md · extensions/subagents/README.md

Environment keys

Native-tool spawns re-set the session keys from the live tool context (extensions/pibash/index.ts); subagent workers get identity keys on spawn (extensions/subagents/index.ts). User-set knobs pass through the same environments:

Key Set by Carries
PI_SESSION_ID pibash current session id
PI_SESSION_FILE pibash session file path
PI_PROVIDER pibash active model provider
PI_MODEL pibash active model id
PI_REASONING_LEVEL pibash thinking level, when one is active
PI_CODING_AGENT_DIR override agent config dir (default ~/.pi/agent); forwarded to workers
PI_SUBAGENT_NAME subagents worker display name
PI_SUBAGENT_MAX_CONCURRENT user cap on simultaneous subagent spawns (default 5; <1 unlimited)
PI_SUBAGENT_MAX_AUTO_RESUMES user auto-resume attempts after a transient provider error before the child fails (default 2; 0 disables)
PI_PIDX_MAX_INLINE user pidx inline-output cap before spill (default 2000)
PI_DASH_PORT user dashboard server port (npm run dashboard; default 8787)
PI_DASH_SESSIONS_DIR user dashboard sessions tree to index (default ~/.pi/agent/sessions)
PI_DASH_SESSION_BUDGET user dashboard per-session cost budget in USD for alerts (0 disables)
PI_COMPACT_EDITOR_LINES user compact-editor height cap in lines (default 10)
HANDOFF_TURN_LIMIT user turns before the auto handoff offer (default 120)

Workers also inherit the internal parent-to-child plumbing (PI_SUBAGENT_AGENT, PI_SUBAGENT_SESSION, PI_SUBAGENT_ID, PI_SUBAGENT_ALLOWED, PI_SUBAGENT_ACTIVITY_FILE, PI_SUBAGENT_SURFACE, PI_SUBAGENT_AUTO_EXIT), classified in the env-var allowlist in rust-tools/docheck/src/main.rs.

How you can help

  • small: run a benchmark arm from benchmark/runs/ on your hardware and file the numbers — host-noise bounds sharpen with every report.
  • small: docs fixes — broken anchors, stale numbers, clearer quick-start steps.
  • medium: new skills under skills/: one skill, one job, validator-clean (npm run check).
  • medium: portability reports — shells and package managers beyond the tested set (zsh, bash; brew, apt).
  • larger: attack a losing or parity row in the benchmarks; the harness, corpus generator, and raw results are in-tree.

Issues and PRs: issue tracker.

Docs

  • Native tools: tools, env keys, performance, context shaping
  • Architecture: dispatch ladder, daemon protocol, per-tool pipelines
  • Skills: roster and validator-enforced layout
  • Subagents: gating, sandboxing, agent table
  • Zero-init: the two-rung bootstrap ladder and what the hot path costs
  • Design rules: context layers, token budget, portability
  • Harness repo rules: scoped rules loaded only inside this repo; context budget
  • Development: repo layout, scripts, sync, providers
  • Benchmarks: harness arms, raw runs, filing numbers on your hardware
  • extensions/README.md: extension and permission-gate rule catalog
  • CHANGELOG.md: every change with what / why / risk / measured delta

Development

Requirements: Node ≥ 22.19 (.nvmrc pins 24), Rust (cargo) for the native tools, tmux only for subagents. Bootstrap never installs a toolchain; it prints the missing command.

sh scripts/bootstrap.sh  # fresh clone → working setup: npm deps, pi links, pidoc shim, Rust builds
npm run check           # typecheck → agents lint → doc check → tests (full pre-commit gate)
npm run gate:eval       # permission-gate fixture metrics (one final JSON line)
npm run daemons:kill    # kill stale native daemons after a rebuild

Sync is plain git: push from the source machine, git pull on the target (husky re-runs scripts/after-pull.sh); sh scripts/bootstrap.sh covers a fresh clone. Script table, providers, and machine-local paths: docs/development.md.

License

MIT © nurmdrafi

About

A performance-focused Pi coding-agent harness with native Rust tools, fail-closed permission gates, and token-efficient context management.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages