@pasko70/pibo 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/README.md +183 -183
  2. package/context/codex-base-prompt.md +148 -148
  3. package/context/compute-worker.md +23 -23
  4. package/context/pibo-compaction-prompt.md +100 -100
  5. package/context/pibo-native-tooling.md +18 -18
  6. package/context/pibo-system-prompt.md +77 -77
  7. package/dist/apps/chat/agent-store.js +82 -82
  8. package/dist/apps/chat/data/project-service.js +166 -166
  9. package/dist/apps/chat/data/read-state-service.js +18 -18
  10. package/dist/apps/chat/data/timeline-query-service.js +8 -8
  11. package/dist/apps/chat/model-catalog.js +5 -1
  12. package/dist/apps/chat/static-assets.js +853 -853
  13. package/dist/apps/chat/workflow-persistence.js +255 -255
  14. package/dist/apps/chat-ui/assets/{dist-7YaJd19a.js → dist-B2BEpL7n.js} +1 -1
  15. package/dist/apps/chat-ui/assets/{dist-CiYeO8nN.js → dist-BAGS_xkV.js} +1 -1
  16. package/dist/apps/chat-ui/assets/{dist-BIvPnn_C.js → dist-BAXNalar.js} +1 -1
  17. package/dist/apps/chat-ui/assets/{dist-CjSI6y5z.js → dist-BwUvs6Ph.js} +1 -1
  18. package/dist/apps/chat-ui/assets/{dist-Ubstha8t.js → dist-C0zsJ8II.js} +1 -1
  19. package/dist/apps/chat-ui/assets/{dist-KXCMNKIL.js → dist-C3PnEkhb.js} +1 -1
  20. package/dist/apps/chat-ui/assets/{dist-CVQU42Fn.js → dist-CYPL-B2Z.js} +1 -1
  21. package/dist/apps/chat-ui/assets/{dist-BbNE72h8.js → dist-CiDSXgtg.js} +1 -1
  22. package/dist/apps/chat-ui/assets/{dist-D7TCkoFT.js → dist-DnACFKyO.js} +1 -1
  23. package/dist/apps/chat-ui/assets/{dist-ChSZNqKE.js → dist-ZB1-ui2y.js} +1 -1
  24. package/dist/apps/chat-ui/assets/{dist-8Noo5eCN.js → dist-vKlxFkTa.js} +1 -1
  25. package/dist/apps/chat-ui/assets/{index-CmxtUVG1.js → index-0x7tuTNX.js} +3 -3
  26. package/dist/apps/chat-ui/assets/{index-C25VYnyb.css → index-B-qaya1G.css} +1 -1
  27. package/dist/apps/chat-ui/index.html +18 -18
  28. package/dist/apps/chat-ui/manifest.webmanifest +25 -25
  29. package/dist/apps/chat-ui/sw.js +41 -41
  30. package/dist/apps/chat-vscode-web/index.html +12 -12
  31. package/dist/apps/cli-ui/cliSessionsCommand.js +23 -23
  32. package/dist/apps/context-files-ui/index.html +11 -11
  33. package/dist/apps/vscode-artifacts/latest.vsix +0 -0
  34. package/dist/apps/vscode-artifacts/pibo-vscode-ext-1.4.2.vsix +0 -0
  35. package/dist/bin/pibo.js +0 -0
  36. package/dist/bin/rg.js +0 -0
  37. package/dist/cli.js +35 -35
  38. package/dist/compute/cli.js +54 -54
  39. package/dist/core/runtime.js +2 -0
  40. package/dist/core/session-router.js +1 -0
  41. package/dist/cron/cli.js +15 -15
  42. package/dist/cron/store.js +49 -49
  43. package/dist/data/cli.js +23 -23
  44. package/dist/data/event-log.js +23 -23
  45. package/dist/data/message-store.js +20 -20
  46. package/dist/data/navigation-store.js +9 -9
  47. package/dist/data/observation-store.js +4 -4
  48. package/dist/data/payload-store.js +17 -17
  49. package/dist/data/schema.js +429 -429
  50. package/dist/data/session-store.js +4 -4
  51. package/dist/data/telemetry-queries.js +54 -54
  52. package/dist/data/telemetry.js +156 -156
  53. package/dist/debug/events.js +12 -12
  54. package/dist/debug/failures.js +6 -6
  55. package/dist/debug/index.js +207 -207
  56. package/dist/debug/messages.js +6 -6
  57. package/dist/debug/pty.js +124 -124
  58. package/dist/debug/session.js +29 -29
  59. package/dist/debug/tools.js +5 -5
  60. package/dist/debug/web-snapshot-browser-scripts.js +294 -294
  61. package/dist/debug/web-streaming-browser-library.js +925 -925
  62. package/dist/debug/web-streaming-browser-scripts.js +232 -232
  63. package/dist/debug/web-streaming-provider-telemetry.js +4 -4
  64. package/dist/debug/web.js +93 -93
  65. package/dist/gateway/cli.js +19 -19
  66. package/dist/mcp/config-command.js +53 -53
  67. package/dist/mcp/index.js +21 -21
  68. package/dist/mcp/registry.js +11 -11
  69. package/dist/pi-packages/cli.js +11 -11
  70. package/dist/plugins/context-files-store.js +110 -110
  71. package/dist/plugins/context-files.js +4 -4
  72. package/dist/providers/glm.js +59 -0
  73. package/dist/ralph/cli.js +18 -18
  74. package/dist/ralph/templates.js +140 -140
  75. package/dist/reliability/store.js +226 -226
  76. package/dist/sessions/pibo-data-store.js +16 -16
  77. package/dist/sessions/sqlite-store.js +53 -53
  78. package/dist/setup/cli.js +58 -58
  79. package/dist/tools/agent-browser-wrapper.js +80 -80
  80. package/dist/tools/browser-use-cdp.js +12 -12
  81. package/dist/tools/browser-use-wrapper.js +762 -762
  82. package/dist/tools/guides.js +455 -455
  83. package/dist/tools/index.js +99 -99
  84. package/dist/tools/runtime/node-worker-source.js +205 -205
  85. package/dist/tools/runtime/python-worker-source.js +177 -177
  86. package/dist/vscode/cli.js +9 -9
  87. package/dist/web-annotations/cdp.js +900 -900
  88. package/dist/web-annotations/store.js +96 -96
  89. package/docs/README.md +23 -23
  90. package/docs/ops/install-developer-host.md +112 -112
  91. package/docs/ops/install-user-host.md +96 -96
  92. package/docs/ops/upgrade-user-to-developer-host.md +69 -69
  93. package/docs/ops/vscode-extension-release.md +160 -160
  94. package/package.json +1 -1
  95. package/skills/builtin/pi-agent-harness/SKILL.md +319 -319
  96. package/skills/builtin/pi-agent-harness/agents/openai.yaml +4 -4
  97. package/skills/builtin/pibo-docker-system/SKILL.md +170 -170
  98. package/skills/builtin/pibo-spec-writing/SKILL.md +330 -330
  99. package/skills/builtin/prd/SKILL.md +143 -143
  100. package/skills/builtin/ralph-loop/SKILL.md +359 -359
  101. package/skills/builtin/ralph-prd-json/SKILL.md +123 -123
  102. package/skills/builtin/skill-creator/LICENSE.txt +201 -201
  103. package/skills/builtin/skill-creator/SKILL.md +513 -513
  104. package/skills/builtin/skill-creator/agents/analyzer.md +274 -274
  105. package/skills/builtin/skill-creator/agents/comparator.md +202 -202
  106. package/skills/builtin/skill-creator/agents/grader.md +223 -223
  107. package/skills/builtin/skill-creator/assets/eval_review.html +146 -146
  108. package/skills/builtin/skill-creator/eval-viewer/generate_review.py +471 -471
  109. package/skills/builtin/skill-creator/eval-viewer/viewer.html +1325 -1325
  110. package/skills/builtin/skill-creator/references/schemas.md +430 -430
  111. package/skills/builtin/skill-creator/scripts/aggregate_benchmark.py +401 -401
  112. package/skills/builtin/skill-creator/scripts/generate_report.py +326 -326
  113. package/skills/builtin/skill-creator/scripts/improve_description.py +247 -247
  114. package/skills/builtin/skill-creator/scripts/package_skill.py +136 -136
  115. package/skills/builtin/skill-creator/scripts/quick_validate.py +102 -102
  116. package/skills/builtin/skill-creator/scripts/run_eval.py +310 -310
  117. package/skills/builtin/skill-creator/scripts/run_loop.py +328 -328
  118. package/skills/builtin/skill-creator/scripts/utils.py +47 -47
  119. package/skills/builtin/web-annotations/SKILL.md +93 -93
  120. package/src/mcp/LICENSE.mcp-cli +21 -21
  121. package/dist/apps/vscode-artifacts/pibo-vscode-1.3.0.vsix +0 -0
  122. package/dist/apps/vscode-artifacts/pibo-vscode-ext-1.3.3.vsix +0 -0
  123. package/dist/apps/vscode-artifacts/pibo-vscode-ext-1.3.4.vsix +0 -0
  124. package/dist/apps/vscode-artifacts/pibo-vscode-ext-1.3.5.vsix +0 -0
  125. package/dist/core/shared-app.js +0 -17
  126. package/dist/data/final-app-space-cutover-migration.js +0 -728
  127. package/dist/data/shared-app-migration.js +0 -757
  128. package/dist/session-ui/ownerViewModel.js +0 -27
  129. package/dist/shared-app.js +0 -4
@@ -1,319 +1,319 @@
1
- ---
2
- name: pi-agent-harness
3
- description: Use when designing, specifying, implementing, or reviewing a modular system built around Pi Coding Agent / Pi Agent as an embedded agent engine. Trigger for Pi Agent docking, createAgentSession, custom tools, SessionManager, ResourceLoader, ExtensionRunner, Pi events, OpenClaw-style harnesses, plugin ecosystems, web chat channels, MCP/tool adapters, system prompt stewardship, policy layers, or minimal runtimes around packages/coding-agent.
4
- ---
5
-
6
- # Pi Agent Harness
7
-
8
- ## Core Idea
9
-
10
- Treat Pi Coding Agent as a small, strong inner engine, not as the whole product.
11
-
12
- Pi should own the model loop, streaming, tool-call execution plumbing, session persistence, compaction, and extension lifecycle. Your outer runtime should own product policy: channels, user identity, tool catalog, sandbox, prompt contract, plugin API, output delivery, approvals, memory, and UI.
13
-
14
- Default rule:
15
-
16
- ```text
17
- Do not expand Pi into your product.
18
- Embed Pi inside your product.
19
- ```
20
-
21
- OpenClaw is the reference pattern, but do not copy its breadth by default. Keep the first implementation raw and narrow: one runtime controller, one channel if needed, one tool registry, one prompt builder, one event aggregator, and a small hook surface.
22
-
23
- ## Source Map
24
-
25
- Read only what the task needs. These are the important paths:
26
-
27
- Pi product layer:
28
-
29
- - `<HOME>/code/pi-mono/packages/coding-agent/README.md` - product philosophy: minimal core, modes, skills, extensions, explicit tools.
30
- - `<HOME>/code/pi-mono/packages/coding-agent/src/main.ts` - CLI entry and mode dispatch.
31
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/sdk.ts` - `createAgentSession(...)`, SDK options, model/auth/settings wiring.
32
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/agent-session.ts` - prompt pipeline, event persistence, compaction, tool registry, extension integration.
33
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/session-manager.ts` - append-only JSONL session tree, branch/fork/compaction context.
34
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/resource-loader.ts` - loads context files, skills, prompts, themes, extensions, `SYSTEM.md`, `APPEND_SYSTEM.md`.
35
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/system-prompt.ts` - default prompt builder and tool/context/skill prompt assembly.
36
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/extensions/types.ts` - extension contract and event names.
37
- - `<HOME>/code/pi-mono/packages/coding-agent/src/core/extensions/runner.ts` - extension lifecycle, stale-context guard, event dispatch.
38
- - `<HOME>/code/pi-mono/packages/coding-agent/src/modes/interactive/interactive-mode.ts` - TUI integration, commands, selectors, rendering of events.
39
-
40
- Lower engine layers:
41
-
42
- - `<HOME>/code/pi-mono/packages/agent/src/agent.ts` - stateful agent wrapper, `subscribe`, `prompt`, `steer`, `followUp`.
43
- - `<HOME>/code/pi-mono/packages/agent/src/agent-loop.ts` - turn loop, streaming, tool execution, event emission.
44
- - `<HOME>/code/pi-mono/packages/ai/src/stream.ts` - provider dispatch.
45
- - `<HOME>/code/pi-mono/packages/tui/src/tui.ts` - terminal rendering if building TUI surfaces.
46
-
47
- OpenClaw reference pattern:
48
-
49
- - `<HOME>/docs/research/tools/openclaw-pi-agent-docking-concept.md` - concise docking analysis.
50
- - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/run/attempt.ts` - embedded runner assembly.
51
- - `<HOME>/code/openclaw/src/agents/pi-tools.ts` - OpenClaw tool catalog assembler.
52
- - `<HOME>/code/openclaw/src/agents/pi-tool-definition-adapter.ts` - OpenClaw tool to Pi `ToolDefinition` adapter.
53
- - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/tool-split.ts` - custom-tool authority pattern.
54
- - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/system-prompt.ts` - external system prompt stewardship.
55
- - `<HOME>/code/openclaw/src/agents/pi-embedded-subscribe.ts` - Pi event subscription to product output.
56
- - `<HOME>/code/openclaw/src/plugins/types.ts` - broad plugin-hook vocabulary, useful as design reference.
57
-
58
- ## Mental Model
59
-
60
- Pi Coding Agent has four relevant surfaces:
61
-
62
- 1. Runtime creation via `createAgentSession(...)`.
63
- 2. Persistent state via `SessionManager`.
64
- 3. Runtime resources via `DefaultResourceLoader` and extensions.
65
- 4. Streamed lifecycle events via `session.subscribe(...)` and underlying `agent.subscribe(...)`.
66
-
67
- The minimal outer harness should look like this:
68
-
69
- ```text
70
- User / Channel / Web App
71
- -> Runtime Controller
72
- -> Prompt Builder
73
- -> Tool Registry + Policy + Sandbox + MCP adapters
74
- -> Hook Runner / Plugin Registry
75
- -> SessionManager
76
- -> Pi createAgentSession(... customTools ...)
77
- -> Event Aggregator
78
- -> Product Reply / Run Result
79
- ```
80
-
81
- Keep product boundaries outside Pi. Pi events are internal engine events; transform them into your own run result before sending anything to a web UI, chat channel, API, or queue.
82
-
83
- ## Minimal Runner Pattern
84
-
85
- Start from a thin runner. Add complexity only when a product requirement forces it.
86
-
87
- ```ts
88
- import {
89
- createAgentSession,
90
- DefaultResourceLoader,
91
- SessionManager,
92
- SettingsManager,
93
- } from "@mariozechner/pi-coding-agent";
94
-
95
- const sessionManager = SessionManager.open(sessionFile, sessionDir, workspaceDir);
96
- const settingsManager = SettingsManager.create(workspaceDir, agentDir);
97
-
98
- const resourceLoader = new DefaultResourceLoader({
99
- cwd: workspaceDir,
100
- agentDir,
101
- settingsManager,
102
- extensionFactories,
103
- });
104
- await resourceLoader.reload();
105
-
106
- const { session } = await createAgentSession({
107
- cwd: workspaceDir,
108
- agentDir,
109
- model,
110
- thinkingLevel,
111
- sessionManager,
112
- settingsManager,
113
- resourceLoader,
114
- noTools: "builtin",
115
- customTools,
116
- });
117
-
118
- const unsubscribe = session.subscribe((event) => {
119
- aggregatePiEventIntoProductRun(event);
120
- });
121
-
122
- await session.prompt(userText, { source: "rpc" });
123
- ```
124
-
125
- Notes:
126
-
127
- - Use `noTools: "builtin"` when your own custom tools should be the authority and Pi built-ins should not be visible.
128
- - If using an explicit `tools` allowlist in current Pi SDK, include every custom tool name you still want exposed; an empty allowlist can filter custom tools too.
129
- - Use `session.steer(...)` for steering while a run is active, `session.followUp(...)` for queued follow-up work after the agent would otherwise stop.
130
- - Dispose/unsubscribe on session replacement or request shutdown.
131
-
132
- ## Tool Authority
133
-
134
- Do not let the model see raw implementation power. Let it see a curated capability catalog.
135
-
136
- Recommended shape:
137
-
138
- ```text
139
- ProductTool
140
- -> policy check
141
- -> sandbox/workspace guard
142
- -> plugin beforeToolCall hooks
143
- -> execute
144
- -> normalize result
145
- -> plugin afterToolCall hooks
146
- -> Pi ToolDefinition result
147
- ```
148
-
149
- Use Pi `customTools` as the transport into the agent loop. Build your own adapter from product tools to Pi `ToolDefinition`:
150
-
151
- - preserve `name`, `description`, `parameters`
152
- - accept `toolCallId`, params, abort signal, update callback
153
- - run policy before execution
154
- - return normalized `content[]` tool results
155
- - convert non-abort failures into structured tool errors
156
- - avoid throwing raw product exceptions into the loop
157
-
158
- For MCP, browser tools, filesystem tools, subprocesses, message sending, or hosted tools, use the same path: register them in your product registry, apply policy there, then adapt them to Pi custom tools. Do not expose MCP as an ungoverned side channel.
159
-
160
- OpenClaw reference: `src/agents/pi-tool-definition-adapter.ts`.
161
-
162
- ## Prompt Stewardship
163
-
164
- Pi can build a good default system prompt, but a product harness usually needs one explicit runtime contract.
165
-
166
- Own the prompt outside Pi when product behavior depends on:
167
-
168
- - channel or delivery rules
169
- - visible tool names
170
- - workspace and sandbox rules
171
- - user identity and trust boundaries
172
- - memory/docs/skills policy
173
- - plugin prompt contributions
174
- - final-output formatting
175
-
176
- Minimal prompt builder inputs:
177
-
178
- ```text
179
- base persona
180
- runtime info
181
- workspace info
182
- visible tool names
183
- channel rules
184
- policy boundaries
185
- optional docs/memory/skills context
186
- plugin prepend/append system context
187
- ```
188
-
189
- Prefer public hooks first. If you must force a full prompt override, isolate that private-field access in one helper and mark it as version-sensitive. OpenClaw uses this pattern in `src/agents/pi-embedded-runner/system-prompt.ts`.
190
-
191
- ## Sessions
192
-
193
- Treat Pi `SessionManager` as the durable transcript layer.
194
-
195
- Important properties:
196
-
197
- - sessions are append-only JSONL files
198
- - entries form a tree with `id` and `parentId`
199
- - the current leaf determines the active LLM context
200
- - branch, fork, tree navigation, compaction, labels, model changes, and custom entries are part of the session model
201
- - `buildSessionContext()` resolves the path from root to leaf and inserts compaction summaries
202
-
203
- Product guidance:
204
-
205
- - Keep product run metadata outside the raw message content unless the model must see it.
206
- - Use custom entries for internal markers that belong in the session file.
207
- - Guard or validate session files if external systems can create or modify them.
208
- - Do not rewrite history casually; branch or append instead.
209
-
210
- ## Extensions And Hooks
211
-
212
- Use Pi extensions for lifecycle integration close to Pi. Use your own plugin API for product integrations.
213
-
214
- Pi extension events worth knowing:
215
-
216
- - `resources_discover` - add skills, prompt templates, themes.
217
- - `input` - transform or handle user input before prompt/template expansion.
218
- - `before_agent_start` - add custom messages or modify the per-turn system prompt.
219
- - `context` - transform message context before provider request.
220
- - `before_provider_request` / `after_provider_response` - observe or rewrite provider payloads.
221
- - `tool_call` / `tool_result` - mutate or block Pi tool calls and inspect results.
222
- - `tool_execution_start/update/end` - stream product-visible tool status.
223
- - `message_start/update/end` - aggregate assistant/user/tool messages.
224
- - `session_before_compact` / `session_compact` - customize or observe compaction.
225
- - `session_before_switch`, `session_before_fork`, `session_before_tree`, `session_shutdown` - protect session transitions.
226
-
227
- Pi extension capabilities:
228
-
229
- - `registerTool(...)`
230
- - `registerCommand(...)`
231
- - `registerShortcut(...)`
232
- - `registerFlag(...)`
233
- - custom UI methods when interactive UI exists
234
- - custom message renderers
235
- - provider registration through runtime actions
236
-
237
- Stale-context rule: after `newSession`, `fork`, `switchSession`, or `reload`, old extension contexts are invalid. Move post-replacement work into the provided `withSession` callback.
238
-
239
- ## Product Plugin Layer
240
-
241
- For a modular system inspired by OpenClaw, plugins should normally dock into your harness, not directly into Pi.
242
-
243
- Start with a tiny hook API:
244
-
245
- ```ts
246
- type Plugin = {
247
- id: string;
248
- tools?: ProductTool[];
249
- beforePromptBuild?(ctx): PromptPatch | void | Promise<PromptPatch | void>;
250
- beforeToolCall?(event, ctx): ToolDecision | void | Promise<ToolDecision | void>;
251
- afterToolCall?(event, ctx): void | Promise<void>;
252
- };
253
- ```
254
-
255
- Keep V1 hooks boring:
256
-
257
- - `beforePromptBuild` can add prepend/append system context.
258
- - `beforeToolCall` can patch params, block, or request approval.
259
- - `afterToolCall` can observe normalized results and side effects.
260
-
261
- Add richer hooks only after a real integration needs them. This keeps Pi small and keeps your product policy testable outside the agent loop.
262
-
263
- ## Event Aggregation
264
-
265
- Never wire Pi events directly to a product channel.
266
-
267
- Build an aggregator that turns engine events into your product contract:
268
-
269
- - final assistant text
270
- - partial assistant updates
271
- - reasoning stream if enabled
272
- - tool start/update/end summaries
273
- - normalized tool results
274
- - usage and model metadata
275
- - compaction/retry status
276
- - side effects such as message sent or file changed
277
- - final run status and error state
278
-
279
- This gives you one stable boundary for web chat, CLI, API, queues, or future channels.
280
-
281
- ## Web-First Minimal Channel
282
-
283
- If the product starts as a web chat, keep channel complexity outside the Pi runner:
284
-
285
- ```text
286
- Web App
287
- -> HTTP/SSE/WebSocket endpoint
288
- -> Runtime Controller
289
- -> Pi Runner
290
- -> Event Aggregator
291
- -> Web stream + persisted run result
292
- ```
293
-
294
- The web channel should provide user/session identity, delivery format, and cancellation. The Pi runner should not know about DOM, routes, Telegram, Slack, WhatsApp, or frontend state. Future channels should reuse the same runtime controller and event aggregator.
295
-
296
- ## Design Checklist
297
-
298
- Before implementing a Pi-based harness, answer these in writing:
299
-
300
- - What does Pi own, and what does the product own?
301
- - Where is the session file, and who may write it?
302
- - Which tools are visible to the model for this run?
303
- - Where do sandbox and workspace rules run?
304
- - Which hook can block or mutate a tool call?
305
- - Who builds the final system prompt?
306
- - How are Pi events converted into product output?
307
- - What is the smallest plugin API that satisfies V1?
308
- - What is explicitly not in V1?
309
-
310
- If the answer requires adding broad product behavior inside Pi itself, redesign the boundary.
311
-
312
- ## Common Mistakes
313
-
314
- - Do not add channels, approvals, memory, MCP, or plugin policy directly to Pi core just because the outer product needs them.
315
- - Do not rely only on prompt text for security-critical tool policy.
316
- - Do not expose Pi built-in tools and product tools together without a deliberate allowlist.
317
- - Do not let plugins mutate Pi internals directly when a harness hook can express the same behavior.
318
- - Do not treat `agent_end` as a UI-ready response; aggregate and normalize first.
319
- - Do not assume OpenClaw's broad implementation is the desired V1. Copy the docking principle, not the feature surface.
1
+ ---
2
+ name: pi-agent-harness
3
+ description: Use when designing, specifying, implementing, or reviewing a modular system built around Pi Coding Agent / Pi Agent as an embedded agent engine. Trigger for Pi Agent docking, createAgentSession, custom tools, SessionManager, ResourceLoader, ExtensionRunner, Pi events, OpenClaw-style harnesses, plugin ecosystems, web chat channels, MCP/tool adapters, system prompt stewardship, policy layers, or minimal runtimes around packages/coding-agent.
4
+ ---
5
+
6
+ # Pi Agent Harness
7
+
8
+ ## Core Idea
9
+
10
+ Treat Pi Coding Agent as a small, strong inner engine, not as the whole product.
11
+
12
+ Pi should own the model loop, streaming, tool-call execution plumbing, session persistence, compaction, and extension lifecycle. Your outer runtime should own product policy: channels, user identity, tool catalog, sandbox, prompt contract, plugin API, output delivery, approvals, memory, and UI.
13
+
14
+ Default rule:
15
+
16
+ ```text
17
+ Do not expand Pi into your product.
18
+ Embed Pi inside your product.
19
+ ```
20
+
21
+ OpenClaw is the reference pattern, but do not copy its breadth by default. Keep the first implementation raw and narrow: one runtime controller, one channel if needed, one tool registry, one prompt builder, one event aggregator, and a small hook surface.
22
+
23
+ ## Source Map
24
+
25
+ Read only what the task needs. These are the important paths:
26
+
27
+ Pi product layer:
28
+
29
+ - `<HOME>/code/pi-mono/packages/coding-agent/README.md` - product philosophy: minimal core, modes, skills, extensions, explicit tools.
30
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/main.ts` - CLI entry and mode dispatch.
31
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/sdk.ts` - `createAgentSession(...)`, SDK options, model/auth/settings wiring.
32
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/agent-session.ts` - prompt pipeline, event persistence, compaction, tool registry, extension integration.
33
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/session-manager.ts` - append-only JSONL session tree, branch/fork/compaction context.
34
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/resource-loader.ts` - loads context files, skills, prompts, themes, extensions, `SYSTEM.md`, `APPEND_SYSTEM.md`.
35
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/system-prompt.ts` - default prompt builder and tool/context/skill prompt assembly.
36
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/extensions/types.ts` - extension contract and event names.
37
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/core/extensions/runner.ts` - extension lifecycle, stale-context guard, event dispatch.
38
+ - `<HOME>/code/pi-mono/packages/coding-agent/src/modes/interactive/interactive-mode.ts` - TUI integration, commands, selectors, rendering of events.
39
+
40
+ Lower engine layers:
41
+
42
+ - `<HOME>/code/pi-mono/packages/agent/src/agent.ts` - stateful agent wrapper, `subscribe`, `prompt`, `steer`, `followUp`.
43
+ - `<HOME>/code/pi-mono/packages/agent/src/agent-loop.ts` - turn loop, streaming, tool execution, event emission.
44
+ - `<HOME>/code/pi-mono/packages/ai/src/stream.ts` - provider dispatch.
45
+ - `<HOME>/code/pi-mono/packages/tui/src/tui.ts` - terminal rendering if building TUI surfaces.
46
+
47
+ OpenClaw reference pattern:
48
+
49
+ - `<HOME>/docs/research/tools/openclaw-pi-agent-docking-concept.md` - concise docking analysis.
50
+ - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/run/attempt.ts` - embedded runner assembly.
51
+ - `<HOME>/code/openclaw/src/agents/pi-tools.ts` - OpenClaw tool catalog assembler.
52
+ - `<HOME>/code/openclaw/src/agents/pi-tool-definition-adapter.ts` - OpenClaw tool to Pi `ToolDefinition` adapter.
53
+ - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/tool-split.ts` - custom-tool authority pattern.
54
+ - `<HOME>/code/openclaw/src/agents/pi-embedded-runner/system-prompt.ts` - external system prompt stewardship.
55
+ - `<HOME>/code/openclaw/src/agents/pi-embedded-subscribe.ts` - Pi event subscription to product output.
56
+ - `<HOME>/code/openclaw/src/plugins/types.ts` - broad plugin-hook vocabulary, useful as design reference.
57
+
58
+ ## Mental Model
59
+
60
+ Pi Coding Agent has four relevant surfaces:
61
+
62
+ 1. Runtime creation via `createAgentSession(...)`.
63
+ 2. Persistent state via `SessionManager`.
64
+ 3. Runtime resources via `DefaultResourceLoader` and extensions.
65
+ 4. Streamed lifecycle events via `session.subscribe(...)` and underlying `agent.subscribe(...)`.
66
+
67
+ The minimal outer harness should look like this:
68
+
69
+ ```text
70
+ User / Channel / Web App
71
+ -> Runtime Controller
72
+ -> Prompt Builder
73
+ -> Tool Registry + Policy + Sandbox + MCP adapters
74
+ -> Hook Runner / Plugin Registry
75
+ -> SessionManager
76
+ -> Pi createAgentSession(... customTools ...)
77
+ -> Event Aggregator
78
+ -> Product Reply / Run Result
79
+ ```
80
+
81
+ Keep product boundaries outside Pi. Pi events are internal engine events; transform them into your own run result before sending anything to a web UI, chat channel, API, or queue.
82
+
83
+ ## Minimal Runner Pattern
84
+
85
+ Start from a thin runner. Add complexity only when a product requirement forces it.
86
+
87
+ ```ts
88
+ import {
89
+ createAgentSession,
90
+ DefaultResourceLoader,
91
+ SessionManager,
92
+ SettingsManager,
93
+ } from "@mariozechner/pi-coding-agent";
94
+
95
+ const sessionManager = SessionManager.open(sessionFile, sessionDir, workspaceDir);
96
+ const settingsManager = SettingsManager.create(workspaceDir, agentDir);
97
+
98
+ const resourceLoader = new DefaultResourceLoader({
99
+ cwd: workspaceDir,
100
+ agentDir,
101
+ settingsManager,
102
+ extensionFactories,
103
+ });
104
+ await resourceLoader.reload();
105
+
106
+ const { session } = await createAgentSession({
107
+ cwd: workspaceDir,
108
+ agentDir,
109
+ model,
110
+ thinkingLevel,
111
+ sessionManager,
112
+ settingsManager,
113
+ resourceLoader,
114
+ noTools: "builtin",
115
+ customTools,
116
+ });
117
+
118
+ const unsubscribe = session.subscribe((event) => {
119
+ aggregatePiEventIntoProductRun(event);
120
+ });
121
+
122
+ await session.prompt(userText, { source: "rpc" });
123
+ ```
124
+
125
+ Notes:
126
+
127
+ - Use `noTools: "builtin"` when your own custom tools should be the authority and Pi built-ins should not be visible.
128
+ - If using an explicit `tools` allowlist in current Pi SDK, include every custom tool name you still want exposed; an empty allowlist can filter custom tools too.
129
+ - Use `session.steer(...)` for steering while a run is active, `session.followUp(...)` for queued follow-up work after the agent would otherwise stop.
130
+ - Dispose/unsubscribe on session replacement or request shutdown.
131
+
132
+ ## Tool Authority
133
+
134
+ Do not let the model see raw implementation power. Let it see a curated capability catalog.
135
+
136
+ Recommended shape:
137
+
138
+ ```text
139
+ ProductTool
140
+ -> policy check
141
+ -> sandbox/workspace guard
142
+ -> plugin beforeToolCall hooks
143
+ -> execute
144
+ -> normalize result
145
+ -> plugin afterToolCall hooks
146
+ -> Pi ToolDefinition result
147
+ ```
148
+
149
+ Use Pi `customTools` as the transport into the agent loop. Build your own adapter from product tools to Pi `ToolDefinition`:
150
+
151
+ - preserve `name`, `description`, `parameters`
152
+ - accept `toolCallId`, params, abort signal, update callback
153
+ - run policy before execution
154
+ - return normalized `content[]` tool results
155
+ - convert non-abort failures into structured tool errors
156
+ - avoid throwing raw product exceptions into the loop
157
+
158
+ For MCP, browser tools, filesystem tools, subprocesses, message sending, or hosted tools, use the same path: register them in your product registry, apply policy there, then adapt them to Pi custom tools. Do not expose MCP as an ungoverned side channel.
159
+
160
+ OpenClaw reference: `src/agents/pi-tool-definition-adapter.ts`.
161
+
162
+ ## Prompt Stewardship
163
+
164
+ Pi can build a good default system prompt, but a product harness usually needs one explicit runtime contract.
165
+
166
+ Own the prompt outside Pi when product behavior depends on:
167
+
168
+ - channel or delivery rules
169
+ - visible tool names
170
+ - workspace and sandbox rules
171
+ - user identity and trust boundaries
172
+ - memory/docs/skills policy
173
+ - plugin prompt contributions
174
+ - final-output formatting
175
+
176
+ Minimal prompt builder inputs:
177
+
178
+ ```text
179
+ base persona
180
+ runtime info
181
+ workspace info
182
+ visible tool names
183
+ channel rules
184
+ policy boundaries
185
+ optional docs/memory/skills context
186
+ plugin prepend/append system context
187
+ ```
188
+
189
+ Prefer public hooks first. If you must force a full prompt override, isolate that private-field access in one helper and mark it as version-sensitive. OpenClaw uses this pattern in `src/agents/pi-embedded-runner/system-prompt.ts`.
190
+
191
+ ## Sessions
192
+
193
+ Treat Pi `SessionManager` as the durable transcript layer.
194
+
195
+ Important properties:
196
+
197
+ - sessions are append-only JSONL files
198
+ - entries form a tree with `id` and `parentId`
199
+ - the current leaf determines the active LLM context
200
+ - branch, fork, tree navigation, compaction, labels, model changes, and custom entries are part of the session model
201
+ - `buildSessionContext()` resolves the path from root to leaf and inserts compaction summaries
202
+
203
+ Product guidance:
204
+
205
+ - Keep product run metadata outside the raw message content unless the model must see it.
206
+ - Use custom entries for internal markers that belong in the session file.
207
+ - Guard or validate session files if external systems can create or modify them.
208
+ - Do not rewrite history casually; branch or append instead.
209
+
210
+ ## Extensions And Hooks
211
+
212
+ Use Pi extensions for lifecycle integration close to Pi. Use your own plugin API for product integrations.
213
+
214
+ Pi extension events worth knowing:
215
+
216
+ - `resources_discover` - add skills, prompt templates, themes.
217
+ - `input` - transform or handle user input before prompt/template expansion.
218
+ - `before_agent_start` - add custom messages or modify the per-turn system prompt.
219
+ - `context` - transform message context before provider request.
220
+ - `before_provider_request` / `after_provider_response` - observe or rewrite provider payloads.
221
+ - `tool_call` / `tool_result` - mutate or block Pi tool calls and inspect results.
222
+ - `tool_execution_start/update/end` - stream product-visible tool status.
223
+ - `message_start/update/end` - aggregate assistant/user/tool messages.
224
+ - `session_before_compact` / `session_compact` - customize or observe compaction.
225
+ - `session_before_switch`, `session_before_fork`, `session_before_tree`, `session_shutdown` - protect session transitions.
226
+
227
+ Pi extension capabilities:
228
+
229
+ - `registerTool(...)`
230
+ - `registerCommand(...)`
231
+ - `registerShortcut(...)`
232
+ - `registerFlag(...)`
233
+ - custom UI methods when interactive UI exists
234
+ - custom message renderers
235
+ - provider registration through runtime actions
236
+
237
+ Stale-context rule: after `newSession`, `fork`, `switchSession`, or `reload`, old extension contexts are invalid. Move post-replacement work into the provided `withSession` callback.
238
+
239
+ ## Product Plugin Layer
240
+
241
+ For a modular system inspired by OpenClaw, plugins should normally dock into your harness, not directly into Pi.
242
+
243
+ Start with a tiny hook API:
244
+
245
+ ```ts
246
+ type Plugin = {
247
+ id: string;
248
+ tools?: ProductTool[];
249
+ beforePromptBuild?(ctx): PromptPatch | void | Promise<PromptPatch | void>;
250
+ beforeToolCall?(event, ctx): ToolDecision | void | Promise<ToolDecision | void>;
251
+ afterToolCall?(event, ctx): void | Promise<void>;
252
+ };
253
+ ```
254
+
255
+ Keep V1 hooks boring:
256
+
257
+ - `beforePromptBuild` can add prepend/append system context.
258
+ - `beforeToolCall` can patch params, block, or request approval.
259
+ - `afterToolCall` can observe normalized results and side effects.
260
+
261
+ Add richer hooks only after a real integration needs them. This keeps Pi small and keeps your product policy testable outside the agent loop.
262
+
263
+ ## Event Aggregation
264
+
265
+ Never wire Pi events directly to a product channel.
266
+
267
+ Build an aggregator that turns engine events into your product contract:
268
+
269
+ - final assistant text
270
+ - partial assistant updates
271
+ - reasoning stream if enabled
272
+ - tool start/update/end summaries
273
+ - normalized tool results
274
+ - usage and model metadata
275
+ - compaction/retry status
276
+ - side effects such as message sent or file changed
277
+ - final run status and error state
278
+
279
+ This gives you one stable boundary for web chat, CLI, API, queues, or future channels.
280
+
281
+ ## Web-First Minimal Channel
282
+
283
+ If the product starts as a web chat, keep channel complexity outside the Pi runner:
284
+
285
+ ```text
286
+ Web App
287
+ -> HTTP/SSE/WebSocket endpoint
288
+ -> Runtime Controller
289
+ -> Pi Runner
290
+ -> Event Aggregator
291
+ -> Web stream + persisted run result
292
+ ```
293
+
294
+ The web channel should provide user/session identity, delivery format, and cancellation. The Pi runner should not know about DOM, routes, Telegram, Slack, WhatsApp, or frontend state. Future channels should reuse the same runtime controller and event aggregator.
295
+
296
+ ## Design Checklist
297
+
298
+ Before implementing a Pi-based harness, answer these in writing:
299
+
300
+ - What does Pi own, and what does the product own?
301
+ - Where is the session file, and who may write it?
302
+ - Which tools are visible to the model for this run?
303
+ - Where do sandbox and workspace rules run?
304
+ - Which hook can block or mutate a tool call?
305
+ - Who builds the final system prompt?
306
+ - How are Pi events converted into product output?
307
+ - What is the smallest plugin API that satisfies V1?
308
+ - What is explicitly not in V1?
309
+
310
+ If the answer requires adding broad product behavior inside Pi itself, redesign the boundary.
311
+
312
+ ## Common Mistakes
313
+
314
+ - Do not add channels, approvals, memory, MCP, or plugin policy directly to Pi core just because the outer product needs them.
315
+ - Do not rely only on prompt text for security-critical tool policy.
316
+ - Do not expose Pi built-in tools and product tools together without a deliberate allowlist.
317
+ - Do not let plugins mutate Pi internals directly when a harness hook can express the same behavior.
318
+ - Do not treat `agent_end` as a UI-ready response; aggregate and normalize first.
319
+ - Do not assume OpenClaw's broad implementation is the desired V1. Copy the docking principle, not the feature surface.
@@ -1,4 +1,4 @@
1
- interface:
2
- display_name: "Pi Agent Harness"
3
- short_description: "Build modular systems around Pi Agent"
4
- default_prompt: "Use $pi-agent-harness to design a minimal modular runtime around Pi Agent."
1
+ interface:
2
+ display_name: "Pi Agent Harness"
3
+ short_description: "Build modular systems around Pi Agent"
4
+ default_prompt: "Use $pi-agent-harness to design a minimal modular runtime around Pi Agent."