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,457 @@
1
+ # 🔍 pi-crew Codebase Deep Audit
2
+
3
+ > **Ngày**: 2026-05-07
4
+ > **Commit**: `682194c`
5
+ > **Codebase**: `pi-crew/` — `src/` + `test/`
6
+
7
+ ---
8
+
9
+ ## 1. Tổng quan Architecture
10
+
11
+ ### End-to-End Flow
12
+
13
+ ```
14
+ User → Pi CLI → team tool → team-tool.ts (dispatcher)
15
+
16
+ team-tool/run.ts → validate resources → create manifest + tasks
17
+
18
+ team-runner.ts → build task graph → resolve ready tasks → batch by concurrency
19
+
20
+ task-runner.ts → spawn child Pi process → capture stdout/stderr JSONL
21
+
22
+ child-pi.ts → process management (spawn, timeout, kill, drain)
23
+
24
+ State layer (manifest.json, tasks.json, events.jsonl, mailbox.jsonl)
25
+
26
+ UI layer (crew-widget, run-dashboard, powerbar-publisher, snapshot-cache)
27
+ ```
28
+
29
+ ### Design Patterns chính
30
+
31
+ - **Artifact-per-operation**: Mỗi task produces 8-15+ artifacts (prompt, result, log, transcript, verification, startup-evidence, capabilities, prompt-pipeline, diff, diff-stat, task-packet, skills, coordination-bridge, inputs, shared-output, summary)
32
+ - **Event sourcing**: All mutations logged as JSONL events với sequence numbers và fingerprints
33
+ - **Caching với mtime stamps**: Manifest cache và snapshot cache dùng file stat mtime/size comparisons
34
+ - **Safety-by-default**: Scaffold mode disables workers; path containment checks; depth guards; plan approval gates
35
+
36
+ ---
37
+
38
+ ## 2. Key Files & Responsibilities
39
+
40
+ ### Core Runtime
41
+
42
+ | File | ~Lines | Responsibility |
43
+ |------|--------|----------------|
44
+ | `src/runtime/team-runner.ts` | ~520 | Core workflow scheduler, adaptive plan injection, task graph execution, policy application |
45
+ | `src/runtime/task-runner.ts` | ~430 | Per-task execution, model fallback, artifact persistence, worktree management |
46
+ | `src/runtime/child-pi.ts` | ~380 | Child Pi process spawning, stdout/stderr capture, timeout handling, process tree kills |
47
+ | `src/runtime/task-graph-scheduler.ts` | ~130 | Dependency graph resolution, ready/blocked/failed task classification |
48
+ | `src/runtime/retry-executor.ts` | ~90 | Generic retry với exponential backoff và jitter |
49
+ | `src/runtime/agent-control.ts` | ~200 | Attention detection, consecutive failure tracking, long-running task alerts |
50
+ | `src/runtime/crash-recovery.ts` | ~150 | Crash state detection và recovery |
51
+ | `src/runtime/overflow-recovery.ts` | ~130 | Overflow/compaction state machine tracking |
52
+ | `src/runtime/stale-reconciler.ts` | ~100 | Stale run detection và reconciliation |
53
+ | `src/runtime/deadletter.ts` | ~80 | Permanently failed task tracking |
54
+
55
+ ### Extension Layer
56
+
57
+ | File | ~Lines | Responsibility |
58
+ |------|--------|----------------|
59
+ | `src/extension/register.ts` | ~550 | Extension registration, session lifecycle, observability, UI wiring |
60
+ | `src/extension/team-tool.ts` | ~400 | Tool action dispatcher (~25 actions), resume logic, checkpoint recovery |
61
+ | `src/extension/team-tool/run.ts` | ~300 | Run action handler, manifest creation, async/foreground routing |
62
+ | `src/extension/team-tool/api.ts` | ~200 | Public API operations (list, get, status, diff, etc.) |
63
+ | `src/extension/team-tool/cancel.ts` | ~100 | Cancel action handler |
64
+ | `src/extension/team-tool/respond.ts` | ~80 | Mailbox respond action |
65
+ | `src/extension/team-tool/status.ts` | ~100 | Status display action |
66
+
67
+ ### State Layer
68
+
69
+ | File | ~Lines | Responsibility |
70
+ |------|--------|----------------|
71
+ | `src/state/state-store.ts` | ~250 | Run manifest CRUD, path resolution, manifest caching |
72
+ | `src/state/mailbox.ts` | ~380 | Inter-task message passing, inbox/outbox, delivery state, reply support |
73
+ | `src/state/event-log.ts` | ~180 | JSONL event append, sequence numbering, cursor reads |
74
+ | `src/state/types.ts` | ~220 | Core types: manifest, tasks, artifacts, policies, output schema |
75
+ | `src/state/contracts.ts` | ~110 | Status enums, transition tables, event types |
76
+ | `src/state/blob-store.ts` | ~120 | Large binary/text blob storage |
77
+ | `src/state/active-run-registry.ts` | ~60 | In-memory tracking of active runs |
78
+
79
+ ### UI Layer
80
+
81
+ | File | ~Lines | Responsibility |
82
+ |------|--------|----------------|
83
+ | `src/ui/run-dashboard.ts` | ~460 | Interactive TUI dashboard với panes và keybindings |
84
+ | `src/ui/run-snapshot-cache.ts` | ~550 | Aggressive caching cho dashboard render, async preloading, stamp-based staleness |
85
+ | `src/ui/crew-widget.ts` | ~360 | Status bar widget rendering, frame-based animation |
86
+ | `src/ui/powerbar-publisher.ts` | ~130 | Powerbar segment updates (coalesced) |
87
+ | `src/ui/transcript-viewer.ts` | ~335 | Syntax-highlighted transcript display, diff rendering |
88
+ | `src/ui/transcript-entries.ts` | ~200 | Entry-based transcript navigation (expand/collapse) |
89
+ | `src/ui/agent-management-overlay.ts` | ~130 | Agent config viewer |
90
+ | `src/ui/render-coalescer.ts` | ~60 | Render request batching (debounce) |
91
+ | `src/ui/render-scheduler.ts` | ~143 | Scheduled render coordination |
92
+ | `src/ui/run-event-bus.ts` | ~98 | In-process event bus cho UI updates |
93
+
94
+ ### Config
95
+
96
+ | File | ~Lines | Responsibility |
97
+ |------|--------|----------------|
98
+ | `src/config/config.ts` | ~840 | Config loading, merging, validation, autonomous settings |
99
+ | `src/config/defaults.ts` | ~200 | Default values cho all config options |
100
+
101
+ ### Agents & Discovery
102
+
103
+ | File | ~Lines | Responsibility |
104
+ |------|--------|----------------|
105
+ | `src/agents/agent-config.ts` | ~80 | AgentConfig type definition |
106
+ | `src/agents/discover-agents.ts` | ~120 | Agent discovery từ agents/ dir + .md files |
107
+ | `src/agents/agent-search.ts` | ~140 | BM25 agent search (weighted fields) |
108
+ | `src/agents/agent-serializer.ts` | ~60 | Agent config serialization/deserialization |
109
+
110
+ ### Utilities
111
+
112
+ | File | ~Lines | Responsibility |
113
+ |------|--------|----------------|
114
+ | `src/utils/visual.ts` | ~200 | visibleWidth (emoji/CJK aware), truncate, pad, wrapHard |
115
+ | `src/utils/safe-paths.ts` | ~50 | Path traversal prevention, containment validation |
116
+ | `src/utils/redaction.ts` | ~40 | Secret redaction cho logs/artifacts |
117
+ | `src/utils/frontmatter.ts` | ~80 | YAML frontmatter parsing |
118
+ | `src/utils/paths.ts` | ~60 | Project/user path resolution |
119
+ | `src/utils/file-coalescer.ts` | ~40 | File read coalescing (debounce) |
120
+ | `src/utils/sse-parser.ts` | ~120 | SSE event stream parser |
121
+ | `src/utils/scan-cache.ts` | ~80 | Directory scan caching |
122
+
123
+ ### New Runtime Utilities (Phase 1-4 additions)
124
+
125
+ | File | ~Lines | Responsibility |
126
+ |------|--------|----------------|
127
+ | `src/runtime/event-stream-bridge.ts` | ~80 | Bridge child Pi JSON events → RunEventBus |
128
+ | `src/runtime/result-extractor.ts` | ~130 | Structured JSON extraction từ worker output |
129
+ | `src/runtime/stream-preview.ts` | ~140 | Live output capture và preview |
130
+ | `src/runtime/code-summary.ts` | ~240 | Regex-based code summarizer (TS/JS/Python/Rust) |
131
+ | `src/runtime/notebook-helpers.ts` | ~90 | .ipynb parser/editor |
132
+ | `src/runtime/workspace-tree.ts` | ~100 | Workspace tree builder |
133
+ | `src/runtime/task-output-context.ts` | ~180 | Dependency context collection (structured + artifacts + usage) |
134
+ | `src/runtime/task-runner/prompt-builder.ts` | ~200 | Worker prompt construction |
135
+ | `src/runtime/task-runner/run-projection.ts` | ~80 | Task run result projection |
136
+
137
+ ### Worktree
138
+
139
+ | File | ~Lines | Responsibility |
140
+ |------|--------|----------------|
141
+ | `src/worktree/worktree-manager.ts` | ~150 | Git worktree creation, branch management |
142
+ | `src/worktree/setup-hooks.ts` | ~60 | Post-worktree-creation setup scripts |
143
+
144
+ ---
145
+
146
+ ## 3. Bugs & Code Quality Issues
147
+
148
+ ### 🐛 Bug #1: Redundant Ternary trong blob-store.ts (HIGH)
149
+
150
+ **File**: `src/state/blob-store.ts`
151
+ **Line**: ~60
152
+
153
+ ```typescript
154
+ const content = typeof input.content === "string" ? input.content : input.content;
155
+ ```
156
+
157
+ **Vấn đề**: Ternary luôn return `input.content` — condition meaningless. Có thể là copy-paste error, ban đầu intended để handle Buffer/Uint8Array fallback.
158
+
159
+ **Fix**:
160
+ ```typescript
161
+ const content = input.content;
162
+ ```
163
+
164
+ Hoặc nếu intended để validate type:
165
+ ```typescript
166
+ if (typeof input.content !== "string") throw new TypeError("content must be string");
167
+ const content = input.content;
168
+ ```
169
+
170
+ ---
171
+
172
+ ### 🐛 Bug #2: Non-null Assertion trên Potentially Empty Array (MEDIUM)
173
+
174
+ **File**: `src/runtime/team-runner.ts`
175
+ **Line**: ~385
176
+
177
+ ```typescript
178
+ manifest = { ...results.at(-1)!.manifest, ... };
179
+ ```
180
+
181
+ **Vấn đề**: `results.at(-1)!` assert non-null nhưng nếu `batchTasks` empty (race condition hoặc edge case), sẽ throw TypeError.
182
+
183
+ **Fix**:
184
+ ```typescript
185
+ const lastResult = results.at(-1);
186
+ if (!lastResult) break;
187
+ manifest = { ...lastResult.manifest, ... };
188
+ ```
189
+
190
+ ---
191
+
192
+ ### 🐛 Bug #3: Indentation Sai trong Switch Case (LOW)
193
+
194
+ **File**: `src/extension/team-tool.ts`
195
+ **Line**: ~267
196
+
197
+ ```typescript
198
+ case "prune": return handlePrune(params, ctx); // extra tab
199
+ ```
200
+
201
+ **Vấn đề**: Copy-paste artifact — case `"prune"` có 1 tab thừa so với các cases khác.
202
+
203
+ **Fix**: Xóa 1 tab.
204
+
205
+ ---
206
+
207
+ ### 🐛 Bug #4: observeStdoutChunk Creates New Observer Per Call (LOW)
208
+
209
+ **File**: `src/runtime/child-pi.ts`
210
+ **Line**: ~246
211
+
212
+ ```typescript
213
+ function observeStdoutChunk(input: ChildPiRunInput, text: string): void {
214
+ const observer = new ChildPiLineObserver(input);
215
+ observer.observe(text);
216
+ observer.flush();
217
+ }
218
+ ```
219
+
220
+ **Vấn đề**: Tạo `new ChildPiLineObserver` mỗi call. Trong real code path, observer được tạo 1 lần và reuse. Mock path nên làm tương tự.
221
+
222
+ ---
223
+
224
+ ### 🐛 Bug #5: Silent Event Bus Error Swallowing (LOW)
225
+
226
+ **File**: `src/state/event-log.ts`
227
+ **Line**: ~151
228
+
229
+ ```typescript
230
+ try { emitFromTeamEvent(fullEvent); } catch { /* event bus errors are non-fatal */ }
231
+ ```
232
+
233
+ **Vấn đề**: Nếu event bus throws repeatedly (e.g., bug in subscriber), mỗi `appendEvent` call silently continues. Mask real issues.
234
+
235
+ **Fix**: Thêm debug-level logging:
236
+ ```typescript
237
+ try { emitFromTeamEvent(fullEvent); } catch (error) { logInternalError("event-log.emit", error); }
238
+ ```
239
+
240
+ ---
241
+
242
+ ## 4. Test Coverage Analysis
243
+
244
+ ### Test Coverage Matrix
245
+
246
+ | Module | ~Lines | Has Tests? | Test File | Test Count | Coverage |
247
+ |--------|--------|-----------|-----------|------------|----------|
248
+ | `event-stream-bridge.ts` | 80 | ✅ | `event-stream-bridge.test.ts` | 11 | Good |
249
+ | `render-coalescer.ts` | 60 | ✅ | `render-coalescer.test.ts` | 7 | Good |
250
+ | `result-extractor.ts` | 130 | ✅ | `result-extractor.test.ts` | 17 | Good |
251
+ | `mailbox.ts` | 380 | ✅ | `mailbox-reply.test.ts` | 6 | Partial |
252
+ | `task-output-context.ts` | 180 | ✅ | `dependency-context-enhanced.test.ts` | 6 | Partial |
253
+ | `transcript-entries.ts` | 200 | ✅ | `transcript-entries.test.ts` | 10 | Good |
254
+ | `agent-management-overlay.ts` | 130 | ✅ | `agent-management-overlay.test.ts` | 10 | Good |
255
+ | `agent-search.ts` | 140 | ✅ | `agent-search.test.ts` | 8 | Good |
256
+ | `code-summary.ts` | 240 | ✅ | `code-summary.test.ts` | 22 | Good |
257
+ | `sse-parser.ts` | 120 | ✅ | `sse-parser.test.ts` | 13 | Good |
258
+ | `stream-preview.ts` | 140 | ✅ | `stream-preview.test.ts` | 14 | Good |
259
+ | `notebook-helpers.ts` | 90 | ✅ | `notebook-helpers.test.ts` | 12 | Good |
260
+ | `visual.ts` | 200 | ✅ | `visual.test.ts` | 5 | Good |
261
+ | `frontmatter.ts` | 80 | ✅ | `frontmatter.test.ts` | ~8 | Good |
262
+ | `config.ts` | 840 | ✅ | `config.test.ts` + others | ~20 | Partial |
263
+ | **`child-pi.ts`** | **380** | ❌ | — | 0 | **NONE** |
264
+ | **`team-runner.ts`** | **520** | ❌ | — | 0 | **NONE** |
265
+ | **`team-tool.ts`** | **400** | ❌ | — | 0 | **NONE** |
266
+ | **`state-store.ts`** | **250** | ❌ | — | 0 | **NONE** |
267
+ | **`run-dashboard.ts`** | **460** | ❌ | — | 0 | **NONE** |
268
+ | **`worktree-manager.ts`** | **150** | ❌ | — | 0 | **NONE** |
269
+ | **`event-log.ts`** | **180** | ✅ | `run-event-bus.test.ts` | 4 | Minimal |
270
+ | **`run-snapshot-cache.ts`** | **550** | ❌ | — | 0 | **NONE** |
271
+ | **`task-runner.ts`** | **430** | ✅ | Integration | 3 | Minimal |
272
+ | `blob-store.ts` | 120 | ✅ | `blob-store.test.ts` | ~6 | Good |
273
+ | `powerbar-publisher.ts` | 130 | ✅ | `powerbar-publisher.test.ts` | 6 | Good |
274
+ | `discover-agents.ts` | 120 | ✅ | `discovery.test.ts` | ~8 | Good |
275
+ | `hooks/registry.ts` | — | ✅ | `hooks.test.ts` | ~5 | Good |
276
+
277
+ ### Coverage Summary
278
+
279
+ ```
280
+ Total src/ modules: ~50
281
+ Tested: ~32 (64%)
282
+ Untested: ~18 (36%)
283
+
284
+ Critical untested (HIGH risk):
285
+ - child-pi.ts (380 lines) — process lifecycle, kill logic
286
+ - team-runner.ts (520 lines) — scheduler, adaptive planning
287
+ - team-tool.ts (400 lines) — 25+ action dispatch
288
+ - state-store.ts (250 lines) — manifest CRUD
289
+
290
+ Important untested (MEDIUM risk):
291
+ - run-dashboard.ts (460 lines) — UI rendering
292
+ - run-snapshot-cache.ts (550 lines) — caching logic
293
+ - worktree-manager.ts (150 lines) — git operations
294
+ - event-log.ts (180 lines) — event persistence
295
+ ```
296
+
297
+ ### Test Count Breakdown
298
+
299
+ ```
300
+ Unit tests: ~74 new (Phase 1-4) + ~30 existing = ~104
301
+ Integration tests: 46 (test/integration/)
302
+ Total: ~150 tests
303
+ ```
304
+
305
+ ---
306
+
307
+ ## 5. Performance Concerns
308
+
309
+ ### 5.1 File-Polling Overhead
310
+
311
+ **Files**: `run-snapshot-cache.ts`, `powerbar-publisher.ts`, `crew-widget.ts`
312
+
313
+ UI components poll files mỗi 200-500ms cho active runs. Mỗi poll:
314
+ 1. `stat()` manifest, tasks, agents, events, mailbox, output files
315
+ 2. If stale → re-read và parse JSON
316
+ 3. Build snapshot → render
317
+
318
+ **Impact**: Với 2+ active runs, mỗi tick reads 12+ files. Đã cải thiện bằng `RenderCoalescer` (Phase 1), nhưng polling vẫn là bottleneck.
319
+
320
+ **Mitigation**: Event Stream Bridge (Phase 1) bypass polling cho real-time events. Nhưng snapshot cache vẫn poll cho full state.
321
+
322
+ ### 5.2 Event Log Append-Only Growth
323
+
324
+ **File**: `src/state/event-log.ts`
325
+
326
+ Events JSONL file grows unbounded. Large runs (100+ tasks) có thể produce 10,000+ events → file becomes slow to tail.
327
+
328
+ **Mitigation**: `MAX_TAIL_LINES = 500` trong snapshot cache. Nhưng file I/O vẫn O(file_size) cho append.
329
+
330
+ ### 5.3 Manifest JSON Rewrite on Every Status Change
331
+
332
+ **Files**: `state-store.ts`, `team-runner.ts`
333
+
334
+ Mỗi task status change → rewrite toàn bộ `manifest.json` và `tasks.json`. Với 10 concurrent tasks updating every second → 20 full JSON serializations/sec.
335
+
336
+ **Mitigation**: Có thể dùng append-only format (JSONL) cho tasks, chỉ rewrite manifest khi structure changes.
337
+
338
+ ---
339
+
340
+ ## 6. Security Analysis
341
+
342
+ ### ✅ Good Practices
343
+
344
+ | Area | File | Detail |
345
+ |------|------|--------|
346
+ | Path traversal | `safe-paths.ts` | Containment validation cho all file paths |
347
+ | Secret redaction | `redaction.ts` | Redacts JSON lines containing secrets |
348
+ | Depth guards | `pi-args.ts` | `checkCrewDepth()` prevents recursive spawning |
349
+ | Plan approval | `team-runner.ts` | Plan approval gate cho implementation workflow |
350
+ | Symlink containment | `safe-paths.ts` | `resolveRealContainedPath()` resolves symlinks |
351
+
352
+ ### ⚠️ Concerns
353
+
354
+ | # | Severity | File | Issue |
355
+ |---|----------|------|-------|
356
+ | 1 | MEDIUM | `worktree-manager.ts:107` | `JSON.parse(trimmed) as { syntheticPaths?: unknown }` — unchecked cast, could throw on malformed JSON |
357
+ | 2 | MEDIUM | `config.ts:303-405` | ~20 `as Record<string, unknown>` casts bypass type safety trong config merge |
358
+ | 3 | LOW | `task-runner.ts` | Worker stdout parsing assumes valid JSON lines — malformed lines could cause silent data loss |
359
+ | 4 | LOW | `child-pi.ts` | `killProcessTree` uses `taskkill /t /f` on Windows — potential for killing wrong processes if PID reused |
360
+
361
+ ---
362
+
363
+ ## 7. Code Hygiene
364
+
365
+ ### 7.1 `__test__` Export Pattern
366
+
367
+ Files exporting test-only helpers (pollute public API):
368
+
369
+ | File | Exports |
370
+ |------|---------|
371
+ | `team-runner.ts` | `__test__parseAdaptivePlan`, `__test__repairAdaptivePlan`, `__test__mergeTaskUpdates` |
372
+ | `state-store.ts` | `__test__manifestCacheSize`, `__test__clearManifestCache` |
373
+ | `config/i18n.ts` | `__test__resetI18n` |
374
+ | `runtime/pi-spawn.ts` | `__test__subagentSpawnParams` |
375
+ | `utils/visual.ts` | `__test__clearVisibleWidthCache`, `__test__visibleWidthCacheSize` |
376
+
377
+ **Count**: 7+ files, 9+ exports
378
+
379
+ **Recommendation**: Move test helpers vào separate `internal` export hoặc dùng `export type` conditional pattern.
380
+
381
+ ### 7.2 Config Merge Type Safety
382
+
383
+ **File**: `src/config/config.ts` (lines 303-405)
384
+
385
+ ```typescript
386
+ const overrideRecord = override as Record<string, unknown>;
387
+ ```
388
+
389
+ ~20 unsafe casts trong config merge function. Bypass type safety cho nested config field merging.
390
+
391
+ **Recommendation**: Dùng validated schema approach (similar to oh-my-pi's `settings-schema.ts`).
392
+
393
+ ### 7.3 Unused / Dead Code
394
+
395
+ | Item | File | Detail |
396
+ |------|------|--------|
397
+ | `observeStdoutChunk` | `child-pi.ts:246` | Defined but only used in mock path — creates new observer each call |
398
+ | Redundant ternary | `blob-store.ts:60` | `typeof x === "string" ? x : x` — always returns same value |
399
+ | `formatTranscriptEvent` export | `transcript-viewer.ts` | Exported but may not be imported externally |
400
+
401
+ ---
402
+
403
+ ## 8. Dependency Graph — Module Coupling
404
+
405
+ ```
406
+ extension/register.ts (550 lines) — GOD FILE
407
+ ├── depends on: team-tool.ts, run-dashboard.ts, crew-widget.ts,
408
+ │ powerbar-publisher.ts, config.ts, run-event-bus.ts,
409
+ │ state-store.ts, manifest-cache.ts, active-run-registry.ts,
410
+ │ skill-instructions.ts, capability-inventory.ts,
411
+ │ team-runner.ts, pi-spawn.ts, run-index.ts
412
+ └── 20+ imports — highest coupling in codebase
413
+
414
+ team-tool.ts (400 lines)
415
+ ├── depends on: run.ts, cancel.ts, status.ts, respond.ts, api.ts,
416
+ │ state-store.ts, team-runner.ts, manifest-cache.ts
417
+ └── 15+ imports
418
+
419
+ team-runner.ts (520 lines)
420
+ ├── depends on: task-runner.ts, task-graph-scheduler.ts, state-store.ts,
421
+ │ event-log.ts, mailbox.ts, agent-control.ts, policy.ts
422
+ └── 12+ imports
423
+ ```
424
+
425
+ `register.ts` là coupling hotspot — 20+ imports. Nên tách thành smaller registration modules.
426
+
427
+ ---
428
+
429
+ ## 9. Recommendations (Priority Order)
430
+
431
+ ### P0 — Fix Bugs
432
+ 1. Fix blob-store redundant ternary
433
+ 2. Fix team-runner non-null assertion
434
+ 3. Add error logging cho silent event bus catch
435
+
436
+ ### P1 — Test Coverage
437
+ 4. Add unit tests cho `child-pi.ts` (mock ChildProcess)
438
+ 5. Add unit tests cho `team-runner.ts` (mock task execution)
439
+ 6. Add unit tests cho `team-tool.ts` (action dispatch)
440
+ 7. Add unit tests cho `state-store.ts` (manifest CRUD)
441
+
442
+ ### P2 — Performance
443
+ 8. Implement append-only tasks format (JSONL thay vì full JSON rewrite)
444
+ 9. Add event log rotation/compaction cho long runs
445
+ 10. Implement incremental event reading (seek to offset thay vì tail toàn bộ)
446
+
447
+ ### P3 — Code Quality
448
+ 11. Extract test helpers vào `internal/` exports
449
+ 12. Reduce register.ts coupling — tách thành smaller registration modules
450
+ 13. Validate config merge với schema thay vì unsafe casts
451
+ 14. Fix worktree-manager.ts JSON parse error handling
452
+
453
+ ### P4 — Architecture
454
+ 15. Typed event channels (worker:progress, worker:lifecycle, worker:stream)
455
+ 16. Yield tool enforcement (schema validation + retry reminders)
456
+ 17. SubprocessToolRegistry pattern cho worker result extraction
457
+ 18. MCP proxy tools cho child processes