pi-crew 0.1.46 → 0.1.49

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 (253) hide show
  1. package/CHANGELOG.md +97 -0
  2. package/agents/analyst.md +11 -11
  3. package/agents/critic.md +11 -11
  4. package/agents/executor.md +11 -11
  5. package/agents/explorer.md +11 -11
  6. package/agents/planner.md +11 -11
  7. package/agents/reviewer.md +11 -11
  8. package/agents/security-reviewer.md +11 -11
  9. package/agents/test-engineer.md +11 -11
  10. package/agents/verifier.md +11 -11
  11. package/agents/writer.md +11 -11
  12. package/docs/next-upgrade-roadmap.md +117 -42
  13. package/docs/refactor-tasks-phase3.md +394 -394
  14. package/docs/refactor-tasks-phase4.md +564 -564
  15. package/docs/refactor-tasks-phase5.md +402 -402
  16. package/docs/refactor-tasks-phase6.md +662 -662
  17. package/docs/research/AGENT-EXECUTION-ARCHITECTURE.md +261 -0
  18. package/docs/research/AGENT-LIFECYCLE-COMPARISON.md +111 -0
  19. package/docs/research/AUDIT_OH_MY_PI.md +261 -0
  20. package/docs/research/AUDIT_PI_CREW.md +457 -0
  21. package/docs/research/CAVEMAN-DEEP-RESEARCH.md +281 -0
  22. package/docs/research/COMPARISON_OH_MY_PI_VS_PI_CREW.md +264 -0
  23. package/docs/research/DEEP-RESEARCH-PI-POWERBAR.md +343 -0
  24. package/docs/research/DEEP_RESEARCH_SUBAGENT_ARCHITECTURE.md +480 -0
  25. package/docs/research/GAP_CLOSURE_IMPLEMENTATION_PLAN.md +354 -0
  26. package/docs/research/IMPLEMENTATION_PLAN.md +385 -0
  27. package/docs/research/LIVE-SESSION-PRODUCTION-READY-PLAN.md +502 -0
  28. package/docs/research/OH-MY-PI-DEEP-RESEARCH-v14.7.6.md +266 -0
  29. package/docs/research/REMAINING-GAPS-PLAN.md +363 -0
  30. package/docs/research/SESSION-SUMMARY-2026-05-08.md +146 -0
  31. package/docs/research/UI-RESPONSIVENESS-AUDIT.md +173 -0
  32. package/docs/research-awesome-agent-skills-distillation.md +100 -100
  33. package/docs/research-extension-examples.md +297 -297
  34. package/docs/research-extension-system.md +324 -324
  35. package/docs/research-oh-my-pi-distillation.md +56 -9
  36. package/docs/research-optimization-plan.md +548 -548
  37. package/docs/research-phase10-distillation.md +198 -198
  38. package/docs/research-phase11-distillation.md +201 -201
  39. package/docs/research-pi-coding-agent.md +357 -357
  40. package/docs/research-source-pi-crew-reference.md +174 -174
  41. package/docs/runtime-flow.md +148 -148
  42. package/docs/source-runtime-refactor-map.md +107 -107
  43. package/index.ts +6 -6
  44. package/package.json +99 -98
  45. package/schema.json +8 -0
  46. package/skills/async-worker-recovery/SKILL.md +42 -42
  47. package/skills/context-artifact-hygiene/SKILL.md +52 -52
  48. package/skills/delegation-patterns/SKILL.md +54 -54
  49. package/skills/mailbox-interactive/SKILL.md +40 -40
  50. package/skills/model-routing-context/SKILL.md +39 -39
  51. package/skills/multi-perspective-review/SKILL.md +58 -58
  52. package/skills/observability-reliability/SKILL.md +41 -41
  53. package/skills/orchestration/SKILL.md +157 -0
  54. package/skills/ownership-session-security/SKILL.md +41 -41
  55. package/skills/pi-extension-lifecycle/SKILL.md +39 -39
  56. package/skills/requirements-to-task-packet/SKILL.md +63 -63
  57. package/skills/resource-discovery-config/SKILL.md +41 -41
  58. package/skills/runtime-state-reader/SKILL.md +44 -44
  59. package/skills/secure-agent-orchestration-review/SKILL.md +45 -45
  60. package/skills/state-mutation-locking/SKILL.md +42 -42
  61. package/skills/systematic-debugging/SKILL.md +67 -67
  62. package/skills/ui-render-performance/SKILL.md +39 -39
  63. package/skills/verification-before-done/SKILL.md +57 -57
  64. package/skills/worktree-isolation/SKILL.md +39 -39
  65. package/src/agents/agent-config.ts +6 -0
  66. package/src/agents/agent-search.ts +98 -0
  67. package/src/agents/agent-serializer.ts +4 -0
  68. package/src/agents/discover-agents.ts +17 -4
  69. package/src/config/config.ts +24 -0
  70. package/src/config/defaults.ts +11 -0
  71. package/src/extension/autonomous-policy.ts +26 -33
  72. package/src/extension/cross-extension-rpc.ts +82 -82
  73. package/src/extension/help.ts +1 -0
  74. package/src/extension/management.ts +5 -0
  75. package/src/extension/register.ts +58 -13
  76. package/src/extension/registration/commands.ts +33 -1
  77. package/src/extension/registration/compaction-guard.ts +125 -125
  78. package/src/extension/registration/team-tool.ts +6 -4
  79. package/src/extension/run-bundle-schema.ts +89 -89
  80. package/src/extension/run-index.ts +24 -18
  81. package/src/extension/run-maintenance.ts +68 -62
  82. package/src/extension/team-tool/api.ts +23 -2
  83. package/src/extension/team-tool/cancel.ts +86 -11
  84. package/src/extension/team-tool/context.ts +3 -0
  85. package/src/extension/team-tool/handle-settings.ts +188 -188
  86. package/src/extension/team-tool/inspect.ts +41 -41
  87. package/src/extension/team-tool/intent-policy.ts +42 -0
  88. package/src/extension/team-tool/lifecycle-actions.ts +47 -18
  89. package/src/extension/team-tool/parallel-dispatch.ts +156 -0
  90. package/src/extension/team-tool/plan.ts +19 -19
  91. package/src/extension/team-tool/respond.ts +10 -2
  92. package/src/extension/team-tool/run.ts +3 -2
  93. package/src/extension/team-tool/status.ts +1 -1
  94. package/src/extension/team-tool-types.ts +1 -0
  95. package/src/extension/team-tool.ts +13 -3
  96. package/src/hooks/registry.ts +61 -0
  97. package/src/hooks/types.ts +41 -0
  98. package/src/i18n.ts +184 -184
  99. package/src/observability/exporters/otlp-exporter.ts +77 -77
  100. package/src/prompt/prompt-runtime.ts +72 -72
  101. package/src/runtime/agent-control.ts +108 -2
  102. package/src/runtime/agent-memory.ts +72 -72
  103. package/src/runtime/agent-observability.ts +114 -114
  104. package/src/runtime/async-marker.ts +26 -26
  105. package/src/runtime/async-runner.ts +3 -1
  106. package/src/runtime/attention-events.ts +28 -28
  107. package/src/runtime/background-runner.ts +19 -0
  108. package/src/runtime/cancellation-token.ts +89 -0
  109. package/src/runtime/cancellation.ts +61 -51
  110. package/src/runtime/capability-inventory.ts +116 -0
  111. package/src/runtime/child-pi.ts +2 -1
  112. package/src/runtime/code-summary.ts +247 -0
  113. package/src/runtime/completion-guard.ts +190 -190
  114. package/src/runtime/crash-recovery.ts +181 -0
  115. package/src/runtime/crew-agent-records.ts +35 -7
  116. package/src/runtime/crew-agent-runtime.ts +1 -0
  117. package/src/runtime/custom-tools/irc-tool.ts +201 -0
  118. package/src/runtime/custom-tools/submit-result-tool.ts +90 -0
  119. package/src/runtime/delivery-coordinator.ts +3 -1
  120. package/src/runtime/direct-run.ts +35 -35
  121. package/src/runtime/effectiveness.ts +81 -76
  122. package/src/runtime/event-stream-bridge.ts +90 -0
  123. package/src/runtime/foreground-control.ts +82 -82
  124. package/src/runtime/green-contract.ts +46 -46
  125. package/src/runtime/group-join.ts +106 -106
  126. package/src/runtime/heartbeat-gradient.ts +28 -28
  127. package/src/runtime/heartbeat-watcher.ts +124 -124
  128. package/src/runtime/live-agent-control.ts +88 -88
  129. package/src/runtime/live-agent-manager.ts +78 -2
  130. package/src/runtime/live-control-realtime.ts +36 -36
  131. package/src/runtime/live-extension-bridge.ts +150 -0
  132. package/src/runtime/live-irc.ts +92 -0
  133. package/src/runtime/live-session-health.ts +100 -0
  134. package/src/runtime/live-session-runtime.ts +297 -7
  135. package/src/runtime/mcp-proxy.ts +113 -0
  136. package/src/runtime/notebook-helpers.ts +90 -0
  137. package/src/runtime/orphan-sentinel.ts +7 -0
  138. package/src/runtime/output-validator.ts +187 -0
  139. package/src/runtime/parallel-research.ts +44 -44
  140. package/src/runtime/parallel-utils.ts +57 -0
  141. package/src/runtime/parent-guard.ts +80 -0
  142. package/src/runtime/pi-json-output.ts +111 -111
  143. package/src/runtime/policy-engine.ts +79 -79
  144. package/src/runtime/progress-event-coalescer.ts +43 -43
  145. package/src/runtime/prose-compressor.ts +164 -0
  146. package/src/runtime/recovery-recipes.ts +74 -74
  147. package/src/runtime/result-extractor.ts +121 -0
  148. package/src/runtime/role-permission.ts +39 -39
  149. package/src/runtime/runtime-resolver.ts +1 -4
  150. package/src/runtime/semaphore.ts +131 -0
  151. package/src/runtime/sensitive-paths.ts +92 -0
  152. package/src/runtime/session-resources.ts +25 -25
  153. package/src/runtime/session-snapshot.ts +59 -59
  154. package/src/runtime/session-usage.ts +79 -79
  155. package/src/runtime/sidechain-output.ts +29 -29
  156. package/src/runtime/stream-preview.ts +177 -0
  157. package/src/runtime/subagent-manager.ts +3 -2
  158. package/src/runtime/subprocess-tool-registry.ts +67 -0
  159. package/src/runtime/supervisor-contact.ts +59 -59
  160. package/src/runtime/task-display.ts +38 -38
  161. package/src/runtime/task-output-context.ts +59 -9
  162. package/src/runtime/task-runner/capabilities.ts +78 -78
  163. package/src/runtime/task-runner/live-executor.ts +2 -0
  164. package/src/runtime/task-runner/progress.ts +119 -119
  165. package/src/runtime/task-runner/prompt-builder.ts +70 -8
  166. package/src/runtime/task-runner/prompt-pipeline.ts +64 -64
  167. package/src/runtime/task-runner/result-utils.ts +14 -14
  168. package/src/runtime/task-runner/run-projection.ts +104 -0
  169. package/src/runtime/task-runner/state-helpers.ts +22 -22
  170. package/src/runtime/task-runner.ts +75 -4
  171. package/src/runtime/team-runner.ts +60 -8
  172. package/src/runtime/worker-heartbeat.ts +21 -21
  173. package/src/runtime/worker-startup.ts +57 -57
  174. package/src/runtime/workspace-tree.ts +298 -0
  175. package/src/runtime/yield-handler.ts +189 -0
  176. package/src/schema/config-schema.ts +6 -0
  177. package/src/schema/team-tool-schema.ts +11 -1
  178. package/src/skills/discover-skills.ts +67 -0
  179. package/src/state/active-run-registry.ts +4 -2
  180. package/src/state/artifact-store.ts +4 -1
  181. package/src/state/atomic-write.ts +50 -1
  182. package/src/state/blob-store.ts +117 -0
  183. package/src/state/contracts.ts +1 -0
  184. package/src/state/event-log-rotation.ts +158 -0
  185. package/src/state/event-log.ts +52 -2
  186. package/src/state/mailbox.ts +87 -7
  187. package/src/state/state-store.ts +24 -4
  188. package/src/state/task-claims.ts +44 -44
  189. package/src/state/types.ts +20 -0
  190. package/src/state/usage.ts +29 -29
  191. package/src/subagents/async-entry.ts +1 -1
  192. package/src/subagents/index.ts +3 -3
  193. package/src/subagents/live/control.ts +1 -1
  194. package/src/subagents/live/manager.ts +1 -1
  195. package/src/subagents/live/realtime.ts +1 -1
  196. package/src/subagents/live/session-runtime.ts +1 -1
  197. package/src/subagents/manager.ts +1 -1
  198. package/src/subagents/spawn.ts +1 -1
  199. package/src/teams/team-serializer.ts +38 -38
  200. package/src/types/diff.d.ts +18 -18
  201. package/src/ui/agent-management-overlay.ts +144 -0
  202. package/src/ui/crew-footer.ts +101 -101
  203. package/src/ui/crew-select-list.ts +111 -111
  204. package/src/ui/crew-widget.ts +11 -2
  205. package/src/ui/dashboard-panes/cancellation-pane.ts +43 -0
  206. package/src/ui/dashboard-panes/capability-pane.ts +60 -0
  207. package/src/ui/dashboard-panes/mailbox-pane.ts +35 -11
  208. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  209. package/src/ui/dynamic-border.ts +25 -25
  210. package/src/ui/layout-primitives.ts +106 -106
  211. package/src/ui/live-run-sidebar.ts +4 -0
  212. package/src/ui/loaders.ts +158 -158
  213. package/src/ui/powerbar-publisher.ts +77 -15
  214. package/src/ui/render-coalescer.ts +51 -0
  215. package/src/ui/render-diff.ts +119 -119
  216. package/src/ui/render-scheduler.ts +143 -143
  217. package/src/ui/run-dashboard.ts +4 -0
  218. package/src/ui/run-event-bus.ts +209 -0
  219. package/src/ui/run-snapshot-cache.ts +68 -16
  220. package/src/ui/snapshot-types.ts +8 -0
  221. package/src/ui/spinner.ts +17 -17
  222. package/src/ui/status-colors.ts +58 -58
  223. package/src/ui/syntax-highlight.ts +116 -116
  224. package/src/ui/transcript-entries.ts +258 -0
  225. package/src/utils/atomic-write.ts +33 -33
  226. package/src/utils/completion-dedupe.ts +63 -63
  227. package/src/utils/frontmatter.ts +68 -68
  228. package/src/utils/git.ts +262 -262
  229. package/src/utils/ids.ts +17 -12
  230. package/src/utils/incremental-reader.ts +104 -0
  231. package/src/utils/names.ts +27 -27
  232. package/src/utils/redaction.ts +44 -44
  233. package/src/utils/safe-paths.ts +47 -47
  234. package/src/utils/scan-cache.ts +137 -0
  235. package/src/utils/sleep.ts +32 -32
  236. package/src/utils/sse-parser.ts +134 -0
  237. package/src/utils/task-name-generator.ts +337 -0
  238. package/src/utils/visual.ts +33 -2
  239. package/src/workflows/validate-workflow.ts +40 -40
  240. package/src/worktree/branch-freshness.ts +45 -45
  241. package/src/worktree/cleanup.ts +2 -1
  242. package/teams/default.team.md +12 -12
  243. package/teams/fast-fix.team.md +11 -11
  244. package/teams/implementation.team.md +18 -18
  245. package/teams/parallel-research.team.md +14 -14
  246. package/teams/research.team.md +11 -11
  247. package/teams/review.team.md +12 -12
  248. package/workflows/default.workflow.md +29 -29
  249. package/workflows/fast-fix.workflow.md +22 -22
  250. package/workflows/implementation.workflow.md +38 -38
  251. package/workflows/parallel-research.workflow.md +46 -46
  252. package/workflows/research.workflow.md +22 -22
  253. package/workflows/review.workflow.md +30 -30
@@ -0,0 +1,385 @@
1
+ # Pi-Crew Improvement Plan — 14 Enhancements from oh-my-pi
2
+
3
+ ## Tổng quan
4
+
5
+ 14 cải tiến được chia thành 4 phases. Mỗi phase có thể thực hiện độc lập,
6
+ không conflict file với nhau (trừ khi ghi chú).
7
+
8
+ ---
9
+
10
+ ## Phase 1: Quick Wins (3-4 ngày)
11
+ > Chất lượng cuộc sống, ít risk, ảnh hưởng ngay
12
+
13
+ ### 1.2 Orchestration Skill
14
+ **Mục tiêu**: Tạo orchestration skill cho planner/executor, ép orchestrator làm việc hiệu quả hơn.
15
+
16
+ **Files tạo/sửa**:
17
+ - `skills/orchestration/SKILL.md` — TẠO MỚI (prompt template)
18
+ - `skills/orchestration/SKILL.md` — TẠO MỚI
19
+
20
+ **Chi tiết**:
21
+ - Nội dung dựa trên oh-my-pi `orchestrate.md` pattern
22
+ - Bao gồm: rules (parallelize, verify, respawn), workflow (ingest → plan → dispatch → verify → commit → advance), anti-patterns
23
+ - Điều chỉnh cho pi-crew context: dùng `task` subagent thay vì trực tiếp edit, dùng mailbox cho coordination
24
+ - Thêm `orchestration` vào default skills cho planner role trong `src/runtime/skill-instructions.ts`
25
+
26
+ **Test**: Manual — chạy team run với orchestration skill, verify planner output quality
27
+
28
+ ---
29
+
30
+ ### 1.3 `/retry` Manual Retry Command
31
+ **Mục tiêu**: Thêm slash command cho phép retry failed/aborted tasks thủ công.
32
+
33
+ **Files tạo/sửa**:
34
+ - `src/extension/registration/commands.ts` — thêm `team-retry` command
35
+ - `src/extension/team-tool/cancel.ts` — thêm `handleRetry` function
36
+
37
+ **Chi tiết**:
38
+ - Command: `/team-retry <runId> [taskId]`
39
+ - Logic:
40
+ 1. Load manifest, tìm task failed/aborted
41
+ 2. Nếu chỉ định taskId → retry task đó, set status về "queued"
42
+ 3. Nếu không chỉ định → retry tất cả failed/aborted tasks
43
+ 4. Reset task metadata (error, finishedAt, terminalEvidence)
44
+ 5. Execute hook `before_retry` (nếu có)
45
+ 6. Re-run task qua `runTeamTask()`
46
+ - Pre-check: ownership, run không đang active
47
+ - Result message: "Retried N task(s) in run X"
48
+
49
+ **Test**: Unit test trong `test/unit/` — mock manifest với failed task, verify reset + re-run
50
+
51
+ ---
52
+
53
+ ### 1.9 Intent Text trên Team Tool Actions
54
+ **Mục tiêu**: Mỗi team tool action (run, cancel, status, etc.) trả về intent text hiển thị trong powerbar.
55
+
56
+ **Files tạo/sửa**:
57
+ - `src/extension/team-tool/context.ts` — thêm `intentText` vào result metadata
58
+ - `src/extension/team-tool/run.ts` — thêm intent: `"running {team} team for: {goal[:60]}"`
59
+ - `src/extension/team-tool/cancel.ts` — thêm intent: `"cancelling run {runId}"`
60
+ - `src/extension/team-tool/status.ts` — thêm intent: `"checking status of run {runId}"`
61
+ - `src/extension/team-tool/respond.ts` — thêm intent: `"responding to task {taskId}"`
62
+ - `src/ui/powerbar-publisher.ts` — hiển thị intent text khi có
63
+
64
+ **Chi tiết**:
65
+ - Mỗi action function thêm `intent` field vào result metadata
66
+ - Powerbar segment hiển thị intent text (truncate 60 chars)
67
+ - Intent chỉ xuất hiện khi action đang chạy, không persist
68
+
69
+ **Test**: Unit test — verify mỗi action trả về đúng intent text
70
+
71
+ ---
72
+
73
+ ## Phase 2: Worker Efficiency (3-4 ngày)
74
+ > Tăng hiệu suất worker, giảm context waste
75
+
76
+ ### 2.5 Workspace Tree Context Injection
77
+ **Mục tiêu**: Inject compact directory tree vào worker prompt để worker hiểu project structure mà không cần explore.
78
+
79
+ **Files tạo/sửa**:
80
+ - `src/runtime/workspace-tree.ts` — TẠO MỚI
81
+ - `src/runtime/task-runner/prompt-builder.ts` — thêm tree block vào prompt
82
+ - `src/runtime/task-runner.ts` — gọi buildWorkspaceTree trước khi render prompt
83
+
84
+ **Chi tiết**:
85
+ - `buildWorkspaceTree(cwd, options)`:
86
+ - `maxDepth: 3`, `directoryEntryLimit: 12`, `lineCap: 120`
87
+ - Skip: node_modules, .git, dist, build, target, .venv, .cache
88
+ - Sort by mtime (recency)
89
+ - Output format: indented list với size + age
90
+ - Async, trả về string hoặc "" nếu lỗi
91
+ - Trong `renderTaskPrompt()`:
92
+ - Thêm section `# Workspace Structure` sau worker context
93
+ - Cache tree per-cwd (TTL 30s) để không rebuild cho mỗi task
94
+ - Option: chỉ inject khi task có `injectWorkspaceTree: true` hoặc khi role là explorer/executor
95
+
96
+ **Test**: Unit test — tạo temp dir structure, verify output format và truncation
97
+
98
+ ---
99
+
100
+ ### 2.8 Cache-Stable Worker Prompts
101
+ **Mục tiêu**: Tách worker prompts thành stable prefix + dynamic suffix để tối ưu KV-cache reuse.
102
+
103
+ **Files tạo/sửa**:
104
+ - `src/runtime/task-runner/prompt-builder.ts` — refactor prompt structure
105
+ - `src/runtime/skill-instructions.ts` — tách skill block ra riêng
106
+
107
+ **Chi tiết**:
108
+ - Prompt structure hiện tại: 1 big system message
109
+ - Refactor thành:
110
+ 1. **Stable prefix**: role instructions, coordination contract, workspace tree (ít thay đổi giữa tasks)
111
+ 2. **Dynamic suffix**: task-specific goal, dependency context, skill instructions (thay đổi mỗi task)
112
+ - `renderTaskPrompt()` trả về `{ stablePrefix: string, dynamicSuffix: string }`
113
+ - `runChildPi()` ghép 2 phần, hoặc gửi riêng nếu provider hỗ trợ multi-system-messages
114
+ - Đo lường: verify cache hit rate tăng khi chạy cùng team nhiều tasks
115
+
116
+ **Test**: Unit test — verify prompt structure, verify stable prefix giống nhau giữa 2 tasks cùng role
117
+
118
+ ---
119
+
120
+ ### 2.9 (tiếp) Intent trên powerbar — hoàn thiện
121
+ Xem 1.9 ở Phase 1.
122
+
123
+ ---
124
+
125
+ ## Phase 3: Token Optimization (4-5 ngày)
126
+ > Giảm token usage, chỉ load những gì cần
127
+
128
+ ### 3.1 Tool LoadMode Pattern
129
+ **Mục tiêu**: Phân loại tools thành "essential" (luôn load) vs "discoverable" (chỉ load khi cần), giảm prompt size cho workers.
130
+
131
+ **Files tạo/sửa**:
132
+ - `src/runtime/role-permission.ts` — thêm `loadMode` vào tool definitions
133
+ - `src/runtime/task-runner/prompt-builder.ts` — chỉ inject prompt snippets cho essential tools
134
+ - `src/runtime/capability-inventory.ts` — track discoverable tools
135
+ - `src/runtime/skill-instructions.ts` — thêm `search_tool_bm25` khi có discoverable tools
136
+
137
+ **Chi tiết**:
138
+ - Tool classification:
139
+ - **Essential** (luôn active): `bash`, `read`, `edit`, `write` (cho read-write roles)
140
+ - **Discoverable** (ẩn): `github`, `browser`, `ast-grep`, `ast-edit`, `calculator`, `render-mermaid`, etc.
141
+ - Role-level override: explorer chỉ cần `read` essential, executor cần `bash` + `read` + `edit`
142
+ - Khi có discoverable tools:
143
+ - Inject `search_tool_bm25` tool vào available tools
144
+ - Thêm instruction: "Some tools are hidden. Use search_tool_bm25 to discover them."
145
+ - Worker sees: fewer tool definitions → smaller prompt → more context room
146
+
147
+ **Test**: Unit test — verify role permissions, verify discoverable tools excluded from prompt
148
+
149
+ ---
150
+
151
+ ### 3.7 BM25 Agent Discovery
152
+ **Mục tiêu**: Thêm BM25 search cho agent discovery — tìm agent phù hợp nhất theo task description.
153
+
154
+ **Files tạo/sửa**:
155
+ - `src/agents/agent-search.ts` — TẠO MỚI
156
+ - `src/agents/discover-agents.ts` — thêm search capability
157
+ - `src/runtime/task-runner.ts` — sử dụng agent search khi auto-select agent
158
+
159
+ **Chi tiết**:
160
+ - `buildAgentSearchIndex(agents)`:
161
+ - BM25 trên fields: name (6x), label (4x), description (2x), tags (1x)
162
+ - Reuse BM25 implementation từ `src/skills/discover-skills.ts` (hoặc extract shared)
163
+ - `searchAgents(index, query, limit)` → ranked agent list
164
+ - Khi task không chỉ định agent, hệ thống có thể:
165
+ 1. Extract keywords từ task description
166
+ 2. Search top 3 agents
167
+ 3. Auto-select best match hoặc suggest cho planner
168
+ - Integration: trong `handleRun()` khi `params.agent` không set
169
+
170
+ **Test**: Unit test — tạo test agents, verify search ranking
171
+
172
+ ---
173
+
174
+ ### 3.13 Agent-as-Markdown Frontmatter Enhancement
175
+ **Mục tiêu**: Thêm frontmatter fields vào agent config: `loadMode`, `defaultTools`, `contextMode`.
176
+
177
+ **Files tạo/sửa**:
178
+ - `src/agents/agent-config.ts` — thêm fields vào AgentConfig type
179
+ - `src/agents/agent-serializer.ts` — parse/serialize new fields
180
+ - `src/runtime/task-runner.ts` — respect loadMode và defaultTools
181
+
182
+ **Chi tiết**:
183
+ - New frontmatter fields:
184
+ ```yaml
185
+ ---
186
+ name: explorer
187
+ description: Fast codebase discovery
188
+ loadMode: essential # "essential" | "discoverable"
189
+ defaultTools: [read, bash, find]
190
+ contextMode: fresh # "fresh" | "fork"
191
+ ---
192
+ ```
193
+ - `AgentConfig` thêm optional fields
194
+ - `renderTaskPrompt()` respects `defaultTools` — chỉ inject specified tools
195
+ - `loadMode` maps sang 3.1's loadMode pattern
196
+ - Default values: `loadMode: "essential"`, `defaultTools: null` (all tools)
197
+
198
+ **Test**: Unit test — parse frontmatter với new fields, verify defaults
199
+
200
+ ---
201
+
202
+ ## Phase 4: Advanced Features (5-7 ngày)
203
+ > Tính năng nâng cao, cần nhiều effort hơn
204
+
205
+ ### 4.4 Streaming Preview cho Worker Output
206
+ **Mục tiêu**: Hiển thị preview worker output (file changes) trong TUI khi worker đang chạy.
207
+
208
+ **Files tạo/sửa**:
209
+ - `src/ui/worker-preview.ts` — TẠO MỚI
210
+ - `src/runtime/task-runner.ts` — capture streaming output events
211
+ - `src/ui/powerbar-publisher.ts` — integrate preview
212
+
213
+ **Chi tiết**:
214
+ - Khi worker emit JSON events (tool calls, edits), capture và render preview
215
+ - Preview formats:
216
+ - Write tool: hiển thị last 12 lines với line numbers + syntax highlight
217
+ - Bash tool: hiển thị last 10 lines output
218
+ - Edit tool: hiển thị diff summary
219
+ - Powerbar segment mở rộng khi có preview content
220
+ - Fallback: nếu TUI không support, chỉ hiển thị spinner như hiện tại
221
+ - Streaming events parse từ child Pi JSONL output (đã có `parsePiJsonOutput`)
222
+
223
+ **Test**: Unit test — mock streaming events, verify preview format
224
+
225
+ ---
226
+
227
+ ### 4.6 Structural Code Summary
228
+ **Mục tiêu**: Tạo code summarization capability cho explorer/reviewer roles — đọc structure thay vì toàn bộ file.
229
+
230
+ **Files tạo/sửa**:
231
+ - `src/runtime/code-summary.ts` — TẠO MỚI (TypeScript fallback)
232
+ - `src/runtime/task-runner/prompt-builder.ts` — inject summary thay vì raw content (optional)
233
+
234
+ **Chi tiết**:
235
+ - **Option A** (JS-only): Regex-based summary cho common patterns (functions, classes, imports)
236
+ - Parse: export/function/class/interface/type declarations
237
+ - Elide: function bodies, long arrays, block comments
238
+ - Output: kept/elided segments
239
+ - **Option B** (Rust native): Gọi `@oh-my-pi/pi-natives` `summarize_code` nếu available
240
+ - Check: `try { await import("@oh-my-pi/pi-natives") } catch {}`
241
+ - Fallback to Option A nếu native không available
242
+ - Integration:
243
+ - Explorer role: default inject summaries thay vì raw files
244
+ - Reviewer role: inject summaries + diff context
245
+ - Executor role: vẫn dùng raw files (cần exact content để edit)
246
+ - Config: `agent.summaryMode: "off" | "structure" | "full"` trong frontmatter
247
+
248
+ **Test**: Unit test — verify summary output cho sample TS/Rust/Python files
249
+
250
+ ---
251
+
252
+ ### 4.10 Task Diff API
253
+ **Mục tiêu**: Expose API để external consumers xem diff của những gì worker đã thay đổi.
254
+
255
+ **Files tạo/sửa**:
256
+ - `src/extension/team-tool/api.ts` — thêm `diff` action
257
+ - `src/extension/team-tool/inspect.ts` — thêm diff display
258
+
259
+ **Chi tiết**:
260
+ - New API action: `team api diff <runId> [taskId]`
261
+ - Logic:
262
+ 1. Load manifest, tìm diff artifacts (từ worktree mode hoặc file snapshots)
263
+ 2. Nếu worktree: chạy `git diff` trong worktree
264
+ 3. Nếu không: đọc stored diff artifact
265
+ 4. Format: unified diff + summary stats
266
+ - Team tool inspect: thêm diff section vào inspect output
267
+ - Powerbar: hiển thị diff stats khi task completes
268
+
269
+ **Test**: Unit test — mock diff artifacts, verify formatting
270
+
271
+ ---
272
+
273
+ ### 4.11 Notebook-Aware Path Handling
274
+ **Mục tiêu**: Support `.ipynb` files trong workers — parse cells, edit individual cells.
275
+
276
+ **Files tạo/sửa**:
277
+ - `src/runtime/notebook-helpers.ts` — TẠO MỚI
278
+ - `src/runtime/task-runner/prompt-builder.ts` — inject notebook instructions
279
+
280
+ **Chi tiết**:
281
+ - `parseNotebook(path)` → cells array
282
+ - `readNotebookCell(path, index)` → cell content
283
+ - `editNotebookCell(path, index, content)` → update cell
284
+ - Worker instructions: "For .ipynb files, use notebook helpers instead of raw read/write"
285
+ - Edge case: handle malformed notebooks gracefully
286
+
287
+ **Test**: Unit test — tạo sample .ipynb, verify parse/edit
288
+
289
+ ---
290
+
291
+ ### 4.12 Shared SSE Utility
292
+ **Mục tiêu**: Extract SSE parser cho potential future use (streaming dashboard, external integrations).
293
+
294
+ **Files tạo/sửa**:
295
+ - `src/utils/sse-parser.ts` — TẠO MỚI
296
+
297
+ **Chi tiết**:
298
+ - `readSseEvents(stream, signal)` → async generator of `ServerSentEvent`
299
+ - Handles: `\r\n`, `\n`, multi-line data, event types, `[DONE]` sentinel
300
+ - `readSseJson(stream, signal)` → wrapper parse JSON data
301
+ - Reusable cho:
302
+ - Future: streaming task status endpoint
303
+ - Future: external dashboard integration
304
+ - Current: không dùng ngay, nhưng available
305
+
306
+ **Test**: Unit test — verify SSE parsing cho various input formats
307
+
308
+ ---
309
+
310
+ ### 4.14 Control Notices Enhancement
311
+ **Mục tiêu**: Mở rộng attention tracking: consecutive tool failures, per-task thresholds, notification routing.
312
+
313
+ **Files tạo/sửa**:
314
+ - `src/runtime/agent-control.ts` — thêm control types
315
+ - `src/runtime/crew-agent-records.ts` — track tool failure counts
316
+ - `src/extension/notification-router.ts` — thêm notification channels
317
+
318
+ **Chi tiết**:
319
+ - New control events:
320
+ - `consecutive_tool_failures`: track N consecutive failed tool calls per task
321
+ - `long_running`: alert khi task chạy quá X minutes (configurable)
322
+ - `no_file_mutations`: alert khi implementation role completes without any file changes
323
+ - New config fields:
324
+ ```json
325
+ {
326
+ "control": {
327
+ "consecutiveFailureThreshold": 3,
328
+ "longRunningMinutes": 10,
329
+ "mutationGuardRoles": ["executor", "worker"]
330
+ }
331
+ }
332
+ ```
333
+ - Notification routing:
334
+ - `event`: append event (current behavior)
335
+ - `attention`: set activityState = "needs_attention" (current)
336
+ - `notify`: dispatch external notification (future: webhook, Slack)
337
+ - Integration: control check chạy trong `applyAttentionState()` — thêm checks mới
338
+
339
+ **Test**: Unit test — verify consecutive failure detection, long-running detection
340
+
341
+ ---
342
+
343
+ ## Dependency Graph
344
+
345
+ ```
346
+ Phase 1 (independent):
347
+ 1.2 Orchestration ─┐
348
+ 1.3 Retry ├─→ không phụ thuộc nhau
349
+ 1.9 Intent ─┘
350
+
351
+ Phase 2 (light dependencies):
352
+ 2.5 Workspace Tree ──→ 2.8 Cache-Stable Prompts (tree là stable prefix candidate)
353
+
354
+ Phase 3 (sequential):
355
+ 3.13 Agent Frontmatter ──→ 3.1 Tool LoadMode ──→ 3.7 BM25 Agent Discovery
356
+ (frontmatter định nghĩa loadMode) (loadMode cần agent search khi auto-select)
357
+
358
+ Phase 4 (independent):
359
+ 4.4 Streaming Preview
360
+ 4.6 Code Summary
361
+ 4.10 Task Diff API
362
+ 4.11 Notebook Helpers
363
+ 4.12 SSE Parser
364
+ 4.14 Control Notices
365
+ ```
366
+
367
+ ## Execution Timeline
368
+
369
+ | Week | Phase | Deliverables |
370
+ |------|-------|-------------|
371
+ | 1 | Phase 1 | Orchestration skill + Retry command + Intent text |
372
+ | 1-2 | Phase 2 | Workspace tree + Cache-stable prompts |
373
+ | 2-3 | Phase 3 | Agent frontmatter + LoadMode + BM25 agent search |
374
+ | 3-4 | Phase 4 | Streaming preview + Code summary + Diff API + Control notices |
375
+ | 4 | Phase 4 | Notebook + SSE parser (lower priority) |
376
+
377
+ ## Risk & Mitigation
378
+
379
+ | Risk | Mitigation |
380
+ |------|-----------|
381
+ | Tool loadMode breaking existing workflows | Default: tất cả tools essential (backward compatible) |
382
+ | BM25 agent search chọn sai agent | Fallback: giữ hiện tại behavior khi search score thấp |
383
+ | Code summary mất info quan trọng | Configurable: chỉ dùng cho explorer/reviewer, không phải executor |
384
+ | Streaming preview phức tạp | Phase 4, chỉ implement khi Phase 1-3 ổn định |
385
+ | Workspace tree too large cho context | Hard lineCap: 120 lines, only inject khi config bật |