Zig 重写的
高性能 coding agent

A high-performance
coding agent in Zig

原生二进制、低内存、毫秒级启动;核心 runtime、TUI、session、 provider、工具链与 TypeScript 扩展兼容层已经进入可替代状态。

Native binary, low memory, sub-second startup. Core runtime, TUI, sessions, providers, tools, and the TypeScript extension compatibility layer are now replacement-ready for the main workflows.

独立可用度 ≈ 96%Standalone ready ≈ 96% 核心 runtime parity 94%Core runtime parity 94% TS-RPC parity 已有 golden 覆盖TS-RPC parity covered by goldens 25+ 内置 provider25+ built-in providers

为什么选 Zig 版?

Why the Zig rewrite?

不是为了重写而重写,是为了把 coding agent 跑得更轻、更快、更可控。

Not a rewrite for its own sake — to make the coding agent lighter, faster, more controllable.

原生二进制

Native binary

单文件分发,启动从秒级降到毫秒级。无 Node、无 V8 启动开销。

Single-file distribution. Startup goes from seconds to milliseconds. No Node, no V8 cold start.

低内存占用

Low memory

显式内存所有权,长会话不再吃光内存。空闲态开销显著低于 Node 版。

Explicit ownership. Long sessions no longer eat memory. Idle footprint far below the Node version.

真·并行 Tool 执行

True parallel tools

基于 Zig 线程模型的并行 tool 调度,配合流式更新链路。

Parallel tool scheduling on Zig threads, paired with a streaming update pipeline.

完整交互式 TUI

Full interactive TUI

差分渲染、Unicode 宽度、剪贴板图片、session tree、fork selector、missing-cwd selector 等都已实现。

Diff rendering, Unicode width, clipboard images, session tree, fork selector, and missing-cwd selector are implemented.

多 Provider & OAuth

Multi-provider & OAuth

OpenAI / Anthropic / Mistral / Kimi / Google / Bedrock / Azure / MiniMax 等 provider; API-key env 映射、Vertex ADC 检查与主要 OAuth 流程已接入。

Providers include OpenAI, Anthropic, Mistral, Kimi, Google, Bedrock, Azure, and MiniMax. API-key env mapping, Vertex ADC checks, and major OAuth flows are wired in.

兼容 TS 扩展生态

TS extension compat

通过 Bun 子进程 + JSON-RPC 复用既有 TS 扩展;工具、命令、快捷键、flag、provider、UI hook 和包管理路径已打通。

Reuse existing TS extensions through Bun + JSON-RPC. Tools, commands, shortcuts, flags, providers, UI hooks, and package flows are wired.

与 TypeScript 版的能力对齐

Parity with the TypeScript version

基于 2026-05-05 当前 Zig 代码的 Review 摘要。完整内容见 REVIEW.md

Review summary based on the current Zig code as of 2026-05-05. Full details in REVIEW.md.

模块Area Parity 状态Status
Agent 核心 loop / runtimeAgent core loop / runtime 94% prompt、tool、abort、retry、compaction、queue steering 已有 parity 覆盖prompt, tools, abort, retry, compaction, and queue steering covered by parity tests
JSON mode / event wire 96% schema 校验、CLI JSON 输出与 TS-RPC golden 覆盖schema validation, CLI JSON output, and TS-RPC goldens
CLI / bootstrap 94% print/json/ts-rpc/session/export/package/extension flags 已覆盖print/json/ts-rpc/session/export/package/extension flags covered
内置 toolsBuilt-in tools 92% read / bash / edit / write / grep / find / ls + parallel mutation queue
TUI / interactive 91% 模块化 TUI、selector、session tree、fork、clipboard image、M8 parity 测试modular TUI, selectors, session tree, fork, clipboard image, M8 parity tests
Sessions / 分支 / 导出Sessions / branching / export 93% session v3、resume/continue/fork/clone、missing-cwd 选择器、HTML/JSONL exportsession v3, resume/continue/fork/clone, missing-cwd selector, HTML/JSONL export
Providers / auth / model registry 86% 主要 provider 已注册;moonshot/xiaomi/cloudflare 独立 provider 仍是缺口major providers registered; standalone moonshot/xiaomi/cloudflare providers remain gaps
RPC 协议兼容RPC protocol compat 92% TS-RPC prompt/tool/compaction/retry/extension UI/M6 host golden 已通过TS-RPC prompt/tool/compaction/retry/extension UI/M6 host goldens pass
扩展 / 包管理 / 自定义 UIExtensions / packages / custom UI 84% Bun host、注册协议、UI hook、package CLI 已实现;动态刷新细节需继续验证Bun host, registration protocol, UI hooks, and package CLI implemented; dynamic refresh details need more verification
MCP 20% 仍主要依赖扩展通道,原生 MCP host 不是当前已完成项still mostly reached through extensions; native MCP host is not complete

架构总览

Architecture

当前形态:Zig 内核 + Bun 扩展兼容层;Wasm 原生扩展仍是后续方向。

Current shape: Zig core + Bun extension compatibility layer. Wasm-native extensions remain future work.

┌──────────────────────────────────────────────────────┐
│              Pi Agent · Zig main process              │
│                                                      │
│   ┌──────────────────────────────────────────────┐   │
│   │  built-in tools · agent loop · TUI · RPC     │   │
│   │  session manager · provider/auth · CLI       │   │
│   └──────────────────────────────────────────────┘   │
│                       │                              │
│              ┌────────┴────────┐                     │
│              │   extension     │                     │
│              │ host / registry │                     │
│              └─┬────────────┬──┘                     │
│                │            │                        │
│   ┌────────────▼──┐    ┌────▼─────────────────────┐  │
│   │ Wasm runtime  │    │ Bun subprocess           │  │
│   │ native exts   │    │ TS / npm extensions      │  │
│   │ Zig/Rust/AS   │    │ stdio + JSON-RPC         │  │
│   └───────────────┘    │ hot-reload · crash heal  │  │
│                        └──────────────────────────┘  │
└──────────────────────────────────────────────────────┘

路线图

Roadmap

当前主线已经从“能跑”转为“收敛剩余 parity gap 与 CI/发布稳定性”。

The main track has moved from "make it run" to closing remaining parity gaps and stabilizing CI/release behavior.

当前收敛项

Current convergence items

S

统一 abort 原子读顺序

Unify abort atomic ordering

stream.zig 仍使用 .monotonic,provider/shared 路径使用 .seq_cst

stream.zig still uses .monotonic; provider/shared paths use .seq_cst.

M

补齐 TS KnownProvider 差异

Close TS KnownProvider deltas

Moonshot、Xiaomi、Cloudflare 独立 provider 与 ModelThinkingLevel = "off" 仍未完全对齐。

Standalone Moonshot, Xiaomi, Cloudflare providers and ModelThinkingLevel = "off" are still not fully aligned.

S

补 setup-failure smoke test

Backfill setup-failure smoke tests

openai_responses.zig 仍缺 canonical setup failure regression。

openai_responses.zig still lacks the canonical setup-failure regression.

S–M

Linux CI 恢复 Zig build/test

Restore Linux Zig build/test in CI

Zig 0.16.0 在 ubuntu-latest 上 build-exe 会 SEGV;当前 Linux job 只做 setup/tool smoke。

Zig 0.16.0 SEGVs in build-exe on ubuntu-latest; the Linux job currently only runs setup/tool smoke checks.

S–M

扩展动态刷新验证

Verify extension dynamic refresh

注册/注销、UI hook、package CLI 已实现;动态 registry refresh 还需要更窄的端到端回归。

Registration, UI hooks, and package CLI are implemented; dynamic registry refresh still needs narrower end-to-end regressions.

后续方向

Next direction

持续 ongoing

继续以 Bun 兼容层承载 TS 扩展

Keep TS extensions on the Bun compatibility layer

这是主路径,不重新定义扩展生态;Zig 负责 host、sessions、tools、providers 和 TUI。

This remains the primary path; Zig owns host, sessions, tools, providers, and TUI without redefining the extension ecosystem.

可选优化 · 持续 Stage 2 · ongoing

把热门扩展原生化

Nativize popular extensions

数据驱动,top N 用 Zig 重写为原生扩展,性能 / 体积 / 独立性提升。

Data-driven. Rewrite the top-N extensions in Zig as native ones for perf, size, and independence.

后续 later

Wasm 原生扩展加载器

Wasm-based native extension loader

Zig / Rust / AssemblyScript 写的扩展通过 Wasm 沙箱热加载。

Hot-load extensions written in Zig / Rust / AssemblyScript via a Wasm sandbox.

快速开始

Getting started

zig/ 目录构建;产物是 zig-out/bin/pi

Build from zig/; the binary is zig-out/bin/pi.

构建二进制Build the binary

进入 Zig package 目录并构建 release 产物。

Enter the Zig package directory and build the release binary.

cd zig
zig build -Doptimize=ReleaseFast

交互模式Interactive mode

启动完整 TUI,会自动创建或恢复 session。

Start the full TUI; sessions are created or resumed automatically.

./zig-out/bin/pi

一次性 promptOne-shot prompt

适合脚本、CI 或快速问题。

Use this for scripts, CI, or quick questions.

./zig-out/bin/pi --print "帮我审查 src/main.zig 的内存管理"
./zig-out/bin/pi --print "Review memory management in src/main.zig"

JSON 事件流JSON event stream

输出机器可消费的 JSON lines。

Emit machine-readable JSON lines.

./zig-out/bin/pi --print --mode json "..."

查看模型List models

打印当前 registry 里的 provider 与 model。

Print providers and models from the current registry.

./zig-out/bin/pi --list-models

续上一会话Continue a session

在非交互模式里继续最近 session。

Continue the latest session in non-interactive mode.

./zig-out/bin/pi --print --continue "继续上一轮"
./zig-out/bin/pi --print --continue "Continue the last session"