跟成熟 coding agent 學設計(39):TUI 工具結果不閃爍的兩條共同規則
四個 coding agent 的 TUI 都遵守同樣兩條規則:工具高度恆定(1 行摘要,不從 0 跳到 N 行)、不自動收合(只有使用者手動展開才會展開)。looplane 兩條都違反了。
四個 coding agent 的 TUI 都遵守同樣兩條規則:工具高度恆定(1 行摘要,不從 0 跳到 N 行)、不自動收合(只有使用者手動展開才會展開)。looplane 兩條都違反了。
AgentHarness 核心類別、System Prompt 動態組裝流程、Skills 載入與格式化、Prompt Templates 系統、Harness 如何決定 Tool 可用性、Result Handling、Telemetry Schema 註冊、預設 Harness 建構、Extension 如何擴充 Harness。
pi-agent-core 的心臟:agentLoop() → runLoop() 雙層 while(true)。Inner loop 處理 tool calls + steering messages,Outer loop 處理 follow-up + prepareNextTurn(compaction、model switch)。Enter = steering(當前工具跑完插入)、Alt+Enter = follow-up(agent 判定結束插入)。streamAssistantResponse() 如何處理 partial message 更新、tool call 解析、parallel/sequential 執行、before/after hooks。
把 pi 當黑盒用:4 種運行模式怎麼切、Session 樹狀結構怎麼存、怎麼在對話中途換模型、Enter vs Alt+Enter 訊息插隊差異、/tree 怎麼跳回歷史分支。為後續架構篇建立直覺。
Compaction 完整機制:shouldCompact 觸發條件(token 佔比、訊息數)、estimateTokens 計算(字符/單字近似)、findCutPoint 尋找切點(保留最近 N 輪)、generateSummary 生成摘要(LLM 呼叫)、prepareCompaction 整理上下文、Branch Summary 生成、Structured Compaction(Extension 自訂 via fromHook)、CompactionEntry 細節、fromHook 機制、Compaction Settings。
Pi 為什麼不內建 Permission System、Gondolin Extension(微 VM 隔離)、Docker 整合模式、OpenShell 沙盒、Permission Model 設計哲學、三種容器化部署模式、安全邊界對比、micro-VM vs Container vs Process Isolation、Extension 如何實作沙盒。
Extension 系統完整解析:Extension 介面定義、onLoad/onUnload 生命週期、四大 Hook(onAgentStart/onBeforeToolCall/onAfterToolCall/onTurnEnd)、五大擴充點(tools/commands/keybindings/ui/settings)、ExtensionRunner 載入順序與依賴解析、ExtensionAPI 提供的能力、Dynamic Border、Widget、Dialog、Selector 等 UI 元件、Extension 間通訊、熱重載機制、官方範例 Extensions。
pi-ai Model Catalog 自動生成流程、Provider Factory 註冊與 Lazy Loading、OAuth 2.0 + PKCE 流程實作、Credential Store(Keychain/Libsecret/Credential Manager/加密檔案)、Credential Sync 跨裝置同步機制、Model Scope Diagnostics、ModelResolver 解析邏輯、CredentialSynchronizationOperation 狀態機。
從使用者可見功能切入架構:7 個 npm 套件各司其職、依賴圖單向流向、為什麼 pi-tui/pi-telemetry 零依賴、pi-ai 如何隔離 provider 細節、lockstep versioning 怎麼避免 diamond dependency。建立「從外往內」的架構心智模型。
pi-ai 是 pi-mono 的反腐層:上層只見 Message/Tool/Context/streamFunction,下層 15+ providers 各自實作細節。本文拆解:統一介面設計、Provider Factory Registry、Lazy Loading 實現 Tree-shaking、Model Catalog 自動生成流程、OAuth/API Key 統一管理、Credential Sync 機制、Thinking/Reasoning 參數標準化。
Release 完整流程:Lockstep Versioning(所有套件同版本)、CHANGELOG 更新、Local Smoke Test 驗證、Release Script 自動化、Binary Build (Bun + Node)、npm-shrinkwrap.json 鎖死傳遞依賴、GitHub Actions OIDC Trusted Publishing、R2 Release Marker 驗證、pi.dev/api/latest-version 公告、Announcement Verification 確保發佈成功。
pi-protocol JSON-RPC 2.0 定義、pi-client 連線管理與重連指數退避、pi-server Session Registry、WebSocket Transport、心跳機制、Session Snapshot、Remote Session Handle、RPC 模式架構、流式事件傳輸、Steering/Follow-up 遠端插隊。
本系列 17 篇帶你從 CLI 使用者角度切入,逐層深入 pi-mono 的 Agent Loop、Session Tree、Tool System、Extension System、TUI 架構、Remote Session、Telemetry、Compaction、Release 流程等核心機制。適合想自架 Agent、研究 Agent 架構、或想貢獻 pi 的開發者。
SessionManager 核心:JSONL append-only 儲存、id/parentId 形成樹、branch() 移動 leaf pointer 不改歷史、buildSessionContext() 處理 compaction entry、createBranchedSession() fork 新檔案。完整 Entry 類型:message、thinking_level_change、model_change、compaction、branch_summary、custom、custom_message、label、session_info。Migration v1→v2→v3 細節。
pi-telemetry 核心:TelemetrySchema 定義 Span/Event/Attribute、defineTelemetrySchema 建立 TypedSpanStarter、InMemoryTelemetryContext/NOOP_TELEMETRY_CONTEXT 零開銷實作、Conformance Tests 驗證 Adapter 正確性、AI/Harness Telemetry Schema 完整定義、屬性類型系統、為什麼不直接用 OpenTelemetry。
測試策略:Faux Provider(無 API Key 跑 e2e)、Vitest 單元測試、Browser Smoke Test(真實瀏覽器驗證)、Biome Lint/Format、tsgo Type Check、Pinned Dependencies、Shrinkwrap 生成、Install Lock、npm Trusted Publishing、CI Pipeline 完整流程。
pi-coding-agent 8 核心工具完整解析:ToolDefinition(給 LLM 看)vs AgentTool(執行邏輯)、createToolDefinition/createTool Factory、executionMode 決定 parallel/sequential、beforeToolCall/afterToolCall 攔截鏈、withFileMutationQueue 序列化檔案寫入、truncateHead/Line/Tail 輸出截斷、read/write/edit/bash/grep/find/ls/powershell 各工具實作細節。
pi-tui 核心完整解析:Virtual DOM Diff 算法實現無閃爍渲染、Component 生命週期、Layout Engine(Flex-like VStack/HStack/Box)、CSI 2026 同步輸出避免 partial frame tearing、Keybindings Manager、Alt Screen 管理、括號貼上模式、Kitty/iTerm2 圖片協定、Markdown/Editor/Selector/Diff 等內建元件。
2026 年 8 月,不只五個框架在動。OMP 2、Pi v2、Opencode 2、dsh、Claude Code 之外,Google(Antigravity CLI)、Meta(Muse Code)、xAI(Grok Build)三家模型商直接下場做 coding agent,加上 Amp、Cline 2.0、Codex CLI Rust 重寫,共八個以上框架同時有架構級變動。再算進 110+ 個 CLI 工具,H2 2026 是 harness 方法論的分裂期。本文從四條架構路線、一個共同方向、一場信任危機拆解。
Pi v0.84.0(2026-08-06)把 AgentHarness v2 API 升為 stable。Lane-based v4 Session model 讓操作可持久化、可中斷。CBOR 取代 JSON,Unix socket 取代 HTTP。背後的 Earendil Inc.(Armin Ronacher 的 PBC)拿到首輪資金。95.4K stars,still MIT,still 極簡。
omp 是 Pi 的 fork,但不只是插件層堆疊:它多了約 80,000 行 Rust,把 grep/shell/AST/PTY 全部搬進 in-process。內建工具從 Pi 的 7 個變成 31 個,外加 14 個 LSP op、28 個 DAP op、60+ 供應商。同一份 codebase,兩個相反的賭注。
Agent CLI 不是更聰明的補全工具,而是能讀懂 codebase、執行多步驟任務、操作真實環境的 AI 代理。Claude Code、Codex CLI、Gemini CLI、OpenCode、Aider、Pi、Kiro、Amp、Cursor CLI... 工具越來越多,但底層共享一套設計邏輯——理解這套邏輯,才能真正用好它們。
Pi 是 Mario Zechner 打造的極簡 coding agent(TypeScript、MIT、約 93K stars),只有 4 個核心工具與極短 system prompt,其餘全靠 Extensions/Skills/Prompt Templates 自己疊。刻意不做 MCP、sub-agents、plan mode、權限彈窗。repo 已改名 earendil-works/pi,npm scope 換成 @earendil-works。
「OpenClaw 是 Pi 的 Gateway 殼」這個說法已經過期。官方文件現在寫的是:內建 runtime id 是 openclaw,而 pi 是會被正規化掉的 legacy 別名,「已經沒有外部 agent 框架套件」。留下的第三方 pi 相關依賴只剩一個終端機元件工具包。