pi-crew 0.1.43 → 0.1.44

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 (38) hide show
  1. package/docs/research-phase10-distillation.md +199 -0
  2. package/docs/research-phase11-distillation.md +201 -0
  3. package/package.json +1 -1
  4. package/src/agents/discover-agents.ts +1 -0
  5. package/src/config/config.ts +19 -0
  6. package/src/extension/register.ts +127 -8
  7. package/src/extension/registration/team-tool.ts +2 -1
  8. package/src/extension/run-index.ts +19 -0
  9. package/src/extension/team-tool/api.ts +1 -1
  10. package/src/extension/team-tool/cancel.ts +103 -31
  11. package/src/extension/team-tool/context.ts +1 -0
  12. package/src/extension/team-tool/respond.ts +67 -0
  13. package/src/extension/team-tool/run.ts +2 -2
  14. package/src/extension/team-tool/status.ts +7 -1
  15. package/src/extension/team-tool-types.ts +4 -0
  16. package/src/extension/team-tool.ts +2 -0
  17. package/src/observability/event-to-metric.ts +6 -0
  18. package/src/runtime/completion-guard.ts +190 -103
  19. package/src/runtime/crash-recovery.ts +30 -0
  20. package/src/runtime/crew-agent-runtime.ts +2 -1
  21. package/src/runtime/delivery-coordinator.ts +143 -0
  22. package/src/runtime/model-fallback.ts +5 -2
  23. package/src/runtime/overflow-recovery.ts +157 -0
  24. package/src/runtime/process-status.ts +1 -1
  25. package/src/runtime/session-resources.ts +25 -0
  26. package/src/runtime/session-snapshot.ts +59 -0
  27. package/src/runtime/stale-reconciler.ts +179 -0
  28. package/src/runtime/supervisor-contact.ts +59 -0
  29. package/src/runtime/task-runner.ts +14 -0
  30. package/src/runtime/team-runner.ts +6 -4
  31. package/src/schema/config-schema.ts +1 -0
  32. package/src/schema/team-tool-schema.ts +6 -1
  33. package/src/state/contracts.ts +6 -2
  34. package/src/ui/crew-widget.ts +5 -4
  35. package/src/ui/powerbar-publisher.ts +3 -3
  36. package/src/ui/run-snapshot-cache.ts +275 -1
  37. package/src/ui/status-colors.ts +4 -0
  38. package/src/utils/atomic-write.ts +33 -0
@@ -0,0 +1,199 @@
1
+ # Phase 10: Source Distillation & Development Roadmap
2
+
3
+ > Synthesized from deep-reads of `pi-mono`, `pi-subagents`, and `pi-crew@melihmucuk` reference fork.
4
+ > Date: 2026-05-04
5
+
6
+ ---
7
+
8
+ ## 1. Source Insights
9
+
10
+ ### 1.1 pi-mono (v0.72.1)
11
+
12
+ | Insight | Impact on pi-crew |
13
+ |---|---|
14
+ | **Compact read rendering** — AGENTS.md, SKILL.md, Pi docs auto-collapsed in TUI | Our agents' prompts that reference these files still work, but users won't see full content inline. Ensure tool-call descriptions are self-contained. |
15
+ | **Session resource cleanup registry** — Providers register cleanup fns; `dispose()` calls all | Our `child-pi.ts` should register cleanup for child processes. Currently we handle SIGINT/beforeExit — align with Pi's new `registerSessionResourceCleanup()`. |
16
+ | **Codex WebSocket SSE fallback** — Transparent fallback on WS failure | No direct impact, but note: child Pi processes may switch transports mid-session. |
17
+ | **Xiaomi per-region token plan providers** | No impact — provider list is internal to Pi. |
18
+ | **Model catalog generator with overrides** | Our `model-fallback.ts` should track new models as Pi adds them. |
19
+
20
+ ### 1.2 pi-subagents (v0.24.0)
21
+
22
+ | Insight | Impact on pi-crew |
23
+ |---|---|
24
+ | **Chain directories** — Dedicated `.pi/chains/` and `~/.pi/agent/chains/` | Our workflows are similar but directory-based discovery with `listMarkdownFilesRecursive` is a good pattern. |
25
+ | **Supervisor contact** — Children call `contact_supervisor` | Our mailbox system already serves this purpose, but subagent-initiated communication is one-directional. Consider adding `supervisor_contact` event for child→parent. |
26
+ | **Model thinking levels** — Respect `thinking` from agent frontmatter | We already have `model-fallback.ts` but don't propagate thinking levels to child Pi. |
27
+ | **Session-scoped status** — Filter status by session | Our `run-index.ts` already merges scopes, but individual run status should be session-scoped to avoid cross-contamination. |
28
+ | **Foreground kept alive during intercom** | Our `completion-guard.ts` handles some of this, but the pattern of pausing parent while child waits for supervisor is worth aligning. |
29
+ | **File-only outputs** — Some subagents only write to files | Our `task-output-context.ts` already supports file-only output extraction. Validate compatibility. |
30
+ | **Packaged recursive agents** — Agents can spawn sub-agents | Our task-runner already supports this via child Pi, but we should document the recursive depth guard. |
31
+ | **UI simplification** — Removed overlays, consolidated to tool actions | Our dashboard is more advanced but we should ensure TUI simplicity is preserved. |
32
+
33
+ ### 1.3 pi-crew reference fork (melihmucuk v1.0.14)
34
+
35
+ | Insight | Impact on pi-crew |
36
+ |---|---|
37
+ | **CrewRuntime singleton** — Process-level, survives session replacement | Our `crew-agent-runtime.ts` is similar but not a true singleton. Consider hardening. |
38
+ | **DeliveryCoordinator** — Routes results to owner session, queues when inactive | We lack this pattern. Our result delivery goes through artifacts + notification, but not session-aware routing. |
39
+ | **Ownership model** — `abortOwned()` returns `{ abortedIds, missingIds, foreignIds }` | Our `cancel.ts` returns `results[]` but doesn't distinguish foreign IDs. Adopt. |
40
+ | **Interactive subagents** — `interactive: true` → `waiting` state, `crew_respond`/`crew_done` | We don't have this. Our agents run to completion. Interactive subagents would enable oracle/planner patterns. |
41
+ | **Overflow recovery** — Detect context overflow → compaction → auto_retry → recovered, with 120s timeout | We have no overflow recovery. Child Pi processes that hit context limits silently fail. |
42
+ | **3-tier agent discovery with JSON overrides** | Our discovery uses teams/agents/workflows with schema validation. JSON overrides for model/thinking/tools are worth adding. |
43
+ | **BootstrapSession** — Excludes own extension, uses `SessionManager.create().newSession()` | Our `child-pi.ts` uses `--extension` flags. Align with Pi 0.65+ `session_start` API. |
44
+ | **Bundled subagents inherit parent model** | Our `model-fallback.ts` resolves model chain differently. Consider simplifying. |
45
+
46
+ ---
47
+
48
+ ## 2. Distilled Development Axes
49
+
50
+ ### Axis A: Runtime Hardening (Critical)
51
+
52
+ **A1. Session-aware result delivery**
53
+ - Current: Results go to artifacts + notification router
54
+ - Target: Add `DeliveryCoordinator` pattern that routes results to the **owner session** specifically, queues when inactive, flushes on `session_start`
55
+ - Why: Prevents result loss when a session is replaced/reloaded; matches Pi's lifecycle
56
+
57
+ **A2. Overflow recovery for child processes**
58
+ - Current: Child Pi hitting context limits fails silently or with generic errors
59
+ - Target: Detect `agent_end` → `compaction_start/end` → `auto_retry_start/end` event sequence; mark task as `"overflow_recovering"` → `"recovered"` or `"failed"`
60
+ - Why: Long tasks with large context currently fail unrecoverably
61
+
62
+ **A3. Interactive subagent protocol**
63
+ - Current: All agents run to completion; no mid-run interaction
64
+ - Target: `interactive: true` in agent frontmatter → agent pauses after response, enters `waiting` state; parent sends `crew_respond` to continue, `crew_done` to finalize
65
+ - Why: Enables oracle (decision evaluation), planner (multi-turn refinement), and any agent that needs human/team guidance mid-task
66
+
67
+ **A4. Session resource cleanup alignment**
68
+ - Current: SIGINT + beforeExit handlers
69
+ - Target: Register cleanup via Pi's `registerSessionResourceCleanup()` when available; fall back to current handlers
70
+ - Why: Aligns with Pi's new lifecycle; prevents orphan processes on session reload
71
+
72
+ ### Axis B: Discovery & Configuration (High)
73
+
74
+ **B1. JSON config overrides for agents/teams**
75
+ - Current: Agent frontmatter is the sole source of truth
76
+ - Target: `~/.pi/agent/pi-crew.json` (global) and `.pi/pi-crew.json` (project) can override `model`, `thinking`, `tools`, `skills` for any agent
77
+ - Why: Per-project model tuning without editing bundled agents; environment-specific tool access
78
+
79
+ **B2. Thinking level propagation**
80
+ - Current: Agent frontmatter has `model` but no `thinking` field
81
+ - Target: Add `thinking` to agent schema; propagate to child Pi via `--thinking` flag or session params
82
+ - Why: Aligns with Pi's thinking levels; cost control for expensive models
83
+
84
+ **B3. Parent model inheritance for bundled agents**
85
+ - Current: `model-fallback.ts` has a complex chain with config fallbacks
86
+ - Target: Simplify: agent frontmatter model → parent session model → config default
87
+ - Why: Reduces configuration burden; bundled agents work with whatever model the parent uses
88
+
89
+ ### Axis C: Ownership & Safety (High)
90
+
91
+ **C1. Foreign-aware ownership model**
92
+ - Current: `cancel.ts` returns flat results array
93
+ - Target: `cancelOwned(runId, taskIds)` returns `{ abortedIds, missingIds, foreignIds }`; tool responses clearly distinguish "you can't abort foreign tasks"
94
+ - Why: Prevents confusion in multi-session scenarios; security improvement
95
+
96
+ **C2. Supervisor contact event (child→parent)**
97
+ - Current: Mailbox is parent→child only; child can write artifacts
98
+ - Target: Add `supervisor_contact` event type where child signals "I need a decision" with structured data; parent can respond via mailbox or `steer_subagent`
99
+ - Why: Enables interactive subagent protocol (A3); currently children are fire-and-forget
100
+
101
+ **C3. Session-scoped status filtering**
102
+ - Current: `run-index.ts` merges project + user scope runs
103
+ - Target: Default status/inspect to session-scoped; cross-scope access only via explicit `scope:` parameter
104
+ - Why: Prevents accidental cross-contamination; matches pi-subagents' session scoping
105
+
106
+ ### Axis D: Compatibility & Polish (Medium)
107
+
108
+ **D1. Compact read rendering awareness**
109
+ - Current: Agent prompts reference AGENTS.md, SKILL.md, etc.
110
+ - Target: Ensure agent prompts are self-contained enough that collapsed reads don't lose critical instructions; add fallback descriptions in team/workflow frontmatter
111
+ - Why: Pi v0.72+ collapses these files in TUI; agents still receive full content via tool calls
112
+
113
+ **D2. Pi 0.65+ API alignment**
114
+ - Current: `child-pi.ts` uses CLI flags (`--model`, `--extension`, etc.)
115
+ - Target: When Pi SDK exposes `SessionManager.create()` + `session_start` event in extension API, migrate child session creation to programmatic API
116
+ - Why: More reliable than CLI flag parsing; better lifecycle control; Pi is moving toward SDK-first
117
+
118
+ **D3. UI simplification**
119
+ - Current: Full dashboard with 6 panes
120
+ - Target: Ensure each pane works as a standalone tool action; no pane depends on another's state. Consider adding compact/expanded modes.
121
+ - Why: pi-subagents removed overlays entirely; our dashboard should be usable without full TUI
122
+
123
+ ### Axis E: Observability Gaps (Medium)
124
+
125
+ **E1. Overflow recovery metrics**
126
+ - Add `tasks_overflow_recovering` and `tasks_overflow_recovered` counters to MetricRegistry
127
+
128
+ **E2. Interactive subagent state tracking**
129
+ - Add `tasks_waiting` state to heartbeat/watcher; track wait duration
130
+
131
+ **E3. Foreign ownership audit logging**
132
+ - Log foreign access attempts with session ID; detect potential conflicts
133
+
134
+ ---
135
+
136
+ ## 3. Priority Matrix
137
+
138
+ | Priority | Item | Axis | Effort | Impact |
139
+ |---|---|---|---|---|
140
+ | 🔴 P0 | A1: Session-aware result delivery | A | M | High — prevents result loss |
141
+ | 🔴 P0 | A2: Overflow recovery for child processes | A | M | High — long tasks currently fail silently |
142
+ | 🟡 P1 | C1: Foreign-aware ownership model | C | S | High — security + UX |
143
+ | 🟡 P1 | A4: Session resource cleanup alignment | A | S | Medium — aligns with Pi lifecycle |
144
+ | 🟡 P1 | B1: JSON config overrides | B | M | Medium — per-project customization |
145
+ | 🟡 P1 | B2: Thinking level propagation | B | S | Medium — cost control |
146
+ | 🟡 P1 | D1: Compact read rendering awareness | D | S | Medium — compatibility |
147
+ | 🟢 P2 | A3: Interactive subagent protocol | A | L | High — enables oracle/planner |
148
+ | 🟢 P2 | B3: Parent model inheritance | B | S | Low — simplification |
149
+ | 🟢 P2 | C2: Supervisor contact event | C | M | Medium — depends on A3 |
150
+ | 🟢 P2 | C3: Session-scoped status | C | S | Low — UX improvement |
151
+ | 🟢 P2 | D2: Pi 0.65+ API alignment | D | L | Low — future-proofing |
152
+ | 🟢 P2 | D3: UI simplification | D | M | Low — nice to have |
153
+ | 🔵 P3 | E1-E3: Observability gaps | E | S | Low — monitoring |
154
+
155
+ ---
156
+
157
+ ## 4. Implementation Order (Proposed)
158
+
159
+ ### Phase 10a: Runtime Hardening (P0 + P1)
160
+ 1. **A1: DeliveryCoordinator** — session-aware result routing
161
+ 2. **A2: OverflowRecoveryTracker** — detect context overflow → compaction → retry
162
+ 3. **C1: Foreign-aware ownership** — `abortOwned()` with foreign detection
163
+ 4. **A4: Session resource cleanup** — `registerSessionResourceCleanup()` adapter
164
+
165
+ ### Phase 10b: Discovery & Configuration (P1)
166
+ 5. **B1: JSON config overrides** — `.pi/pi-crew.json` per-project settings
167
+ 6. **B2: Thinking level propagation** — `thinking` frontmatter field
168
+ 7. **D1: Compact read awareness** — self-contained agent prompts
169
+
170
+ ### Phase 10c: Interactive Protocol (P2)
171
+ 8. **A3: Interactive subagent** — `waiting` state + `crew_respond`/`crew_done` pattern
172
+ 9. **C2: Supervisor contact event** — child→parent communication channel
173
+ 10. **B3: Parent model inheritance** — simplified resolve chain
174
+
175
+ ### Phase 10d: Polish & Compatibility (P2-P3)
176
+ 11. **C3: Session-scoped status** — default filter to session
177
+ 12. **D3: UI compact/expanded modes** — standalone pane usability
178
+ 13. **E1-E3: Observability gaps** — overflow, waiting, foreign metrics
179
+ 14. **D2: Pi 0.65+ API alignment** — programmatic session creation (when SDK available)
180
+
181
+ ---
182
+
183
+ ## 5. Key Code References
184
+
185
+ | Pattern | Source File | Lines |
186
+ |---|---|---|
187
+ | Compact read rendering | `pi-mono/packages/coding-agent/src/core/tools/read.ts` | `CompactReadClassification`, `formatCompactReadCall()` |
188
+ | Session resource cleanup | `pi-mono/packages/ai/src/session-resources.ts` | `registerSessionResourceCleanup()`, `cleanupSessionResources()` |
189
+ | Codex WS SSE fallback | `pi-mono/packages/ai/src/providers/openai-codex-responses.ts` | `isWebSocketSseFallbackActive()` |
190
+ | Chain directories | `pi-subagents/src/agents/agents.ts` | `getUserChainDir()`, `resolveNearestProjectChainDirs()` |
191
+ | Supervisor contact | `pi-subagents/src/runs/shared/supervisor-contact.ts` | `contact_supervisor` event |
192
+ | Thinking levels | `pi-subagents/src/agents/agents.ts` | frontmatter `thinking` field |
193
+ | Session scoping | `pi-subagents/src/runs/foreground/foreground-run-queue.ts` | session-scoped filtering |
194
+ | CrewRuntime singleton | `pi-crew-ref/extension/runtime/crew-runtime.ts` | Process-level singleton |
195
+ | DeliveryCoordinator | `pi-crew-ref/extension/runtime/delivery-coordinator.ts` | Owner-session routing |
196
+ | Ownership model | `pi-crew-ref/extension/integration/tools/crew-abort.ts` | `abortOwned()` |
197
+ | Interactive subagent | `pi-crew-ref/extension/runtime/subagent-state.ts` | `waiting` state |
198
+ | Overflow recovery | `pi-crew-ref/extension/runtime/overflow-recovery.ts` | `OverflowRecoveryTracker` |
199
+ | Bootstrap session | `pi-crew-ref/extension/bootstrap-session.ts` | Extension exclusion, parent model |
@@ -0,0 +1,201 @@
1
+ # Phase 10+ Deep Distillation — Round 2
2
+
3
+ **Date**: 2026-05-04
4
+ **Sources**: `pi-mono` v0.72.1 (`324aa1d`), `pi-subagents` v0.24.0 (`3ee17de`), `pi-crew` ref v1.0.14 (`c0631a3`)
5
+
6
+ ## Executive Summary
7
+
8
+ Sau khi deep-read lần 2 vào runtime internals của cả 3 repos, phát hiện **15 insights mới** chưa được implement trong pi-crew. Phân thành 4 axes: Runtime Architecture, Extension API Adoption, Observability/Reliability, và Developer Experience.
9
+
10
+ ---
11
+
12
+ ## Axis F: Runtime Architecture Alignment
13
+
14
+ ### F1. Process-Level Singleton for CrewRuntime ⭐⭐⭐
15
+ **Source**: pi-crew ref `crew-runtime.ts`
16
+ **Finding**: Module-level singleton (`export const crewRuntime = new CrewRuntime()`) sống xuyên suốt process lifetime. Khi Pi thay extension instance (session switch), singleton vẫn tồn tại vì Node.js module cache. New extension instance chỉ cần gọi `crewRuntime.activateSession(binding)`.
17
+ **Current pi-crew**: Mỗi session tạo mới state. Chưa có survive-across-session mechanism.
18
+ **Action**: Refactor `SubagentManager` thành process-level singleton với `activateSession()` pattern. In-flight child processes survive session switches.
19
+
20
+ ### F2. Fire-and-Forget Spawn với Immediate ID Return ⭐⭐⭐
21
+ **Source**: pi-crew ref `crew-runtime.ts`
22
+ **Finding**: `spawn()` tạo state → return ID ngay lập tức → chạy `spawnSession()` async (fire-and-forget). Caller không block.
23
+ **Current pi-crew**: `runChildPi` là async block. Task runner phải await.
24
+ **Action**: Tách spawn thành sync ID allocation + async execution. Task runner fire-and-forget, poll status qua event log.
25
+
26
+ ### F3. Final Drain Window Pattern ⭐⭐
27
+ **Source**: pi-subagents `execution.ts`
28
+ **Finding**: Khi `message_end` với `stopReason === "stop"` và không có tool calls → start 1s grace timer → SIGTERM → 3s → SIGKILL. Giúp child process flush output cuối cùng.
29
+ **Current pi-crew**: Child Pi timeout đơn giản, không có grace period sau completion signal.
30
+ **Action**: Implement `FINAL_STOP_GRACE_MS` drain window trong `child-pi.ts`.
31
+
32
+ ### F4. Atomic JSON Writes cho Status Persistence ⭐⭐
33
+ **Source**: pi-subagents `async-execution.ts`
34
+ **Finding**: `writeAtomicJson()` ghi file temp → rename. Tránh torn writes khi process crash giữa chừng.
35
+ **Current pi-crew**: `JSON.stringify` + `writeFileSync` trực tiếp — rủi ro torn write.
36
+ **Action**: Implement `writeAtomicJson()` utility. Apply cho status.json, manifest writes.
37
+
38
+ ### F5. Two-Level Process Hierarchy cho Async ⭐
39
+ **Source**: pi-subagents `subagent-runner.ts`
40
+ **Finding**: Orchestrator spawn runner (detached) → runner spawn Pi children. Runner track PIDs, write status.json. Orchestrator poll status.json.
41
+ **Current pi-crew**: Async run chỉ fire background, không có intermediate runner process.
42
+ **Action**: (Low priority) Xem xét thêm intermediate runner cho reliable async tracking.
43
+
44
+ ### F6. Stale Run Reconciler — Three-Phase Pattern ⭐⭐
45
+ **Source**: pi-subagents `stale-run-reconciler.ts`
46
+ **Finding**: 3-phase: (1) check result file exists → use it, (2) check PID liveness, (3) for dead PIDs → repair immediately, for alive PIDs → fail only if stale > 24h.
47
+ **Current pi-crew**: Có `crash-recovery.ts` nhưng chưa có full 3-phase reconciliation.
48
+ **Action**: Nâng cấp crash recovery với 3-phase pattern: result-check → PID-check → stale-threshold.
49
+
50
+ ---
51
+
52
+ ## Axis G: Extension API Adoption
53
+
54
+ ### G1. `session_before_compact` Hook — Custom Compaction ⭐⭐⭐
55
+ **Source**: pi-mono `extensions/types.ts`
56
+ **Finding**: Hook `session_before_compact` returns `{ cancel?, compaction?: CompactionResult }`. Extensions có thể **thay thế hoàn toàn** compaction logic — bao gồm structured details (artifact indices, version markers). Đây là extensibility point mạnh nhất.
57
+ **Current pi-crew**: `compaction-guard.ts` chỉ phát hiện compaction events, không can thiệp.
58
+ **Action**: Implement `session_before_compact` handler để cung cấp structured compaction thay vì raw text summarization. Preserve team run state across compaction.
59
+
60
+ ### G2. `session_before_switch` Hook — Pre-Switch State Save ⭐⭐
61
+ **Source**: pi-mono `extensions/types.ts`
62
+ **Finding**: `session_before_switch` fires trước khi Pi switches session (new/resume). Return `{ cancel? }`. Pi-crew có thể save in-memory state → file trước khi switch.
63
+ **Current pi-crew**: Không hook vào session switch. State mất khi switch.
64
+ **Action**: Hook `session_before_switch` để flush pending deliveries và save subagent state snapshot.
65
+
66
+ ### G3. `resources_discover` Hook — Dynamic Agent/Team Discovery ⭐⭐⭐
67
+ **Source**: pi-mono `extensions/types.ts`
68
+ **Finding**: `resources_discover` event returns `{ additionalSkillPaths?, additionalPromptPaths?, additionalThemePaths? }`. Extensions có thể dynamically inject resources.
69
+ **Current pi-crew**: Discovery chỉ đọc từ filesystem. Không dynamic.
70
+ **Action**: Hook `resources_discover` để inject team-specific skills/prompts dựa trên config. VD: auto-inject `safe-bash` skill cho projects có `package.json`.
71
+
72
+ ### G4. `before_agent_start` — System Prompt Override ⭐⭐
73
+ **Source**: pi-mono `extensions/types.ts`
74
+ **Finding**: Can inject `message` and/or override `systemPrompt` before agent loop begins. Powerful for child agents.
75
+ **Current pi-crew**: Child Pi system prompt built từ task packet, không override qua hook.
76
+ **Action**: (Low priority — already handled via task packet prompt builder)
77
+
78
+ ### G5. `tool_result` Event — Post-Execution Output Modification ⭐
79
+ **Source**: pi-mono `extensions/types.ts`
80
+ **Finding**: Can modify tool output `content`, `details`, `isError` after execution. Useful for enrichment/filtering.
81
+ **Current pi-crew**: Không hook vào tool results.
82
+ **Action**: Hook `tool_result` cho `team` tool để enrich output với structured metadata (run URL, artifact count, duration).
83
+
84
+ ### G6. `input` Event — User Input Interception ⭐
85
+ **Source**: pi-mono `extensions/types.ts`
86
+ **Finding**: Can transform user input text/images or fully handle it (`action: "continue" | "transform" | "handled"`).
87
+ **Current pi-crew**: Không intercept user input.
88
+ **Action**: Hook `input` để detect `@team-name` mentions → auto-route to team run.
89
+
90
+ ---
91
+
92
+ ## Axis H: Observability & Reliability Gaps
93
+
94
+ ### H1. Completion Mutation Guard ⭐⭐
95
+ **Source**: pi-subagents `completion-guard.ts`
96
+ **Finding**: Sau khi subagent trả về "success", check xem nếu task là "implementation" nhưng **không có file edits** → mutate completion thành warning. Tránh false-positive completions.
97
+ **Current pi-crew**: Task complete khi child Pi exits 0. Không verify actual work done.
98
+ **Action**: Implement completion guard: verify artifacts exist, files changed, hoặc output non-trivial.
99
+
100
+ ### H2. Snapshot-Before-Emit Pattern ⭐
101
+ **Source**: pi-subagents `execution.ts`
102
+ **Finding**: Progress object snapshotted (spread) trước mỗi `onUpdate` callback. Tránh mutation during callback.
103
+ **Current pi-crew**: Task state mutated directly, events emit references.
104
+ **Action**: Snapshot task state trước khi emit events để avoid race conditions.
105
+
106
+ ### H3. Intercom Bridge với Delivery Confirmation ⭐⭐
107
+ **Source**: pi-subagents `intercom-bridge.ts`
108
+ **Finding**: Bidirectional intercom: `deliverSubagentResultIntercomEvent()` emit event → wait for confirmation với 500ms timeout. Agent injection pattern: mutate config để add `contact_supervisor` tool + instructions.
109
+ **Current pi-crew**: Có `supervisor-contact.ts` parse từ stdout, nhưng không có bidirectional confirmation.
110
+ **Action**: Nếu Pi expose intercom API, upgrade supervisor contact thành bidirectional với delivery confirmation.
111
+
112
+ ### H4. writeAtomicJson Utility ⭐⭐
113
+ **Source**: pi-subagents (pervasive)
114
+ **Finding**: Atomic file writes used everywhere: status, manifest, results. Pattern: `writeFileSync(path + ".tmp", data) → renameSync(path + ".tmp", path)`.
115
+ **Action**: Shared utility trong `src/utils/atomic-write.ts`.
116
+
117
+ ---
118
+
119
+ ## Axis I: Developer Experience
120
+
121
+ ### I1. Tool Presentation — Emoji + Grouping ⭐
122
+ **Source**: pi-crew ref `tool-presentation.ts`
123
+ **Finding**: `crew_spawn` renders "🚀 Spawning {agent}...", `crew_respond` renders "💬 Sending response...". Grouped tool calls have custom collapse UI.
124
+ **Current pi-crew**: Tool output plain text.
125
+ **Action**: Add emoji prefixes và structured formatting cho tool output.
126
+
127
+ ### I2. renderCall/renderResult cho Team Tool ⭐⭐
128
+ **Source**: pi-mono `tools/index.ts`
129
+ **Finding**: `ToolDefinition` supports `renderCall` và `renderResult` callbacks returning TUI Components. Allows rich rendering in Pi terminal UI.
130
+ **Current pi-crew**: Không có custom renderers.
131
+ **Action**: Implement `renderCall` cho `team` tool để show spinner/agent-list thay vì raw JSON. Implement `renderResult` để show summary dashboard.
132
+
133
+ ### I3. Prompt Snippet + Guidelines trong Tool Definition ⭐
134
+ **Source**: pi-mono `tools/index.ts`
135
+ **Finding**: `promptSnippet` — one-liner in system prompt. `promptGuidelines` — bullets appended to system prompt. Tools without `promptSnippet` are excluded from LLM awareness.
136
+ **Current pi-crew**: Tool description chỉ trong JSON schema description.
137
+ **Action**: Khi Pi hỗ trợ `promptSnippet`/`promptGuidelines` trong custom tools, adopt để improve LLM tool usage.
138
+
139
+ ---
140
+
141
+ ## Priority Matrix
142
+
143
+ | ID | Feature | Impact | Effort | Priority |
144
+ |---|---|---|---|---|
145
+ | F1 | Process-level singleton | High | High | P1 |
146
+ | F2 | Fire-and-forget spawn | Medium | Medium | P2 |
147
+ | F3 | Final drain window | Medium | Low | P2 |
148
+ | F4 | Atomic JSON writes | High | Low | P1 |
149
+ | F5 | Two-level async hierarchy | Low | High | P3 |
150
+ | F6 | 3-phase stale reconciliation | Medium | Medium | P2 |
151
+ | G1 | Custom compaction hook | High | Medium | P1 |
152
+ | G2 | Pre-switch state save | Medium | Low | P2 |
153
+ | G3 | Dynamic resource discovery | High | Medium | P1 |
154
+ | G4 | System prompt override | Low | Low | P3 |
155
+ | G5 | Post-execution output mod | Low | Low | P3 |
156
+ | G6 | User input interception | Medium | Medium | P3 |
157
+ | H1 | Completion mutation guard | High | Low | P1 |
158
+ | H2 | Snapshot-before-emit | Medium | Low | P2 |
159
+ | H3 | Bidirectional intercom | Medium | High | P3 |
160
+ | H4 | writeAtomicJson utility | High | Low | P1 |
161
+ | I1 | Tool presentation emojis | Low | Low | P3 |
162
+ | I2 | Custom TUI renderers | High | High | P2 (when API available) |
163
+ | I3 | Prompt snippet/guidelines | Medium | Low | P3 (when API available) |
164
+
165
+ ---
166
+
167
+ ## Recommended Implementation Order
168
+
169
+ ### Phase 11a: Reliability Foundations (F4 + H4 + H1 + H2)
170
+ - `src/utils/atomic-write.ts` — writeAtomicJson utility
171
+ - Apply atomic writes to all manifest/state writes
172
+ - Completion mutation guard for task results
173
+ - Snapshot-before-emit for task state events
174
+
175
+ ### Phase 11b: Extension API Hooks (G1 + G2 + G3)
176
+ - `session_before_compact` handler — structured compaction
177
+ - `session_before_switch` handler — pre-switch state flush
178
+ - `resources_discover` handler — dynamic skill/prompt injection
179
+
180
+ ### Phase 11c: Runtime Architecture (F1 + F2 + F3)
181
+ - Refactor SubagentManager → process-level singleton
182
+ - Fire-and-forget spawn pattern
183
+ - Final drain window for child process cleanup
184
+
185
+ ### Phase 11d: Reconciliation & Recovery (F6 + H3)
186
+ - 3-phase stale run reconciliation
187
+ - Upgrade supervisor contact toward bidirectional (if API available)
188
+
189
+ ---
190
+
191
+ ## Already Implemented (Phase 10a-10d) ✅
192
+ - DeliveryCoordinator (session-aware routing with queue/flush)
193
+ - OverflowRecoveryTracker (compaction → retry state machine)
194
+ - Foreign-aware cancel (ownership detection)
195
+ - Session resource cleanup adapter
196
+ - Interactive subagent waiting state + respond action
197
+ - Supervisor contact parsing from child stdout
198
+ - Parent model inheritance
199
+ - Session-scoped run listing
200
+ - Observability metrics for overflow/waiting/supervisor
201
+ - Skills override + .pi/pi-crew.json config path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-crew",
3
- "version": "0.1.43",
3
+ "version": "0.1.44",
4
4
  "description": "Pi extension for coordinated AI teams, workflows, worktrees, and async task orchestration",
5
5
  "author": "baphuongna",
6
6
  "license": "MIT",
@@ -80,6 +80,7 @@ function applyAgentOverrides(agents: AgentConfig[], cwd: string): AgentConfig[]
80
80
  fallbackModels: override.fallbackModels === false ? undefined : override.fallbackModels ?? agent.fallbackModels,
81
81
  thinking: override.thinking === false ? undefined : override.thinking ?? agent.thinking,
82
82
  tools: override.tools === false ? undefined : override.tools ?? agent.tools,
83
+ skills: override.skills === false ? undefined : override.skills ?? agent.skills,
83
84
  override: { source: "config", path: loaded.path },
84
85
  };
85
86
  });
@@ -80,6 +80,7 @@ export interface AgentOverrideConfig {
80
80
  fallbackModels?: string[] | false;
81
81
  thinking?: string | false;
82
82
  tools?: string[] | false;
83
+ skills?: string[] | false;
83
84
  }
84
85
 
85
86
  export interface CrewAgentsConfig {
@@ -189,6 +190,14 @@ export function projectConfigPath(cwd: string): string {
189
190
  return path.join(projectCrewRoot(cwd), "config.json");
190
191
  }
191
192
 
193
+ /**
194
+ * Alternative project config path: `.pi/pi-crew.json` in the project root.
195
+ * This is a convenience path alongside the standard `config.json` in crewRoot.
196
+ */
197
+ export function projectPiCrewJsonPath(cwd: string): string {
198
+ return path.join(cwd, ".pi", "pi-crew.json");
199
+ }
200
+
192
201
  function withoutUndefined<T extends Record<string, unknown>>(value: T): Partial<T> {
193
202
  return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined)) as Partial<T>;
194
203
  }
@@ -536,6 +545,7 @@ function parseAgentOverride(value: unknown): AgentOverrideConfig | undefined {
536
545
  fallbackModels: parseStringArrayOrFalse(obj.fallbackModels),
537
546
  thinking: parseWithSchema(Type.Union([Type.String(), Type.Literal(false)]), obj.thinking),
538
547
  tools: parseStringArrayOrFalse(obj.tools),
548
+ skills: parseStringArrayOrFalse(obj.skills),
539
549
  };
540
550
  return Object.values(override).some((entry) => entry !== undefined) ? override : undefined;
541
551
  }
@@ -731,6 +741,15 @@ export function loadConfig(cwd?: string): LoadedPiTeamsConfig {
731
741
  const projectSafeConfig = sanitizeProjectConfig(projectPath, config, projectConfig.config);
732
742
  warnings.push(...projectConfig.warnings.map((warning) => `${projectPath}: ${warning}`), ...projectSafeConfig.warnings);
733
743
  config = mergeConfig(config, projectSafeConfig.config);
744
+ // Also load .pi/pi-crew.json from project root if it exists
745
+ const piCrewJsonPath = projectPiCrewJsonPath(cwd);
746
+ if (fs.existsSync(piCrewJsonPath)) {
747
+ const piCrewJsonConfig = parseConfigWithWarnings(readConfigRecord(piCrewJsonPath));
748
+ const piCrewJsonSafeConfig = sanitizeProjectConfig(piCrewJsonPath, config, piCrewJsonConfig.config);
749
+ warnings.push(...piCrewJsonConfig.warnings.map((warning) => `${piCrewJsonPath}: ${warning}`), ...piCrewJsonSafeConfig.warnings);
750
+ config = mergeConfig(config, piCrewJsonSafeConfig.config);
751
+ paths.push(piCrewJsonPath);
752
+ }
734
753
  }
735
754
  return { path: filePath, paths, config, warnings: warnings.length > 0 ? warnings : undefined };
736
755
  } catch (error) {