pi-crew 0.1.41 → 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.
- package/CHANGELOG.md +47 -0
- package/README.md +51 -0
- package/agents/analyst.md +11 -11
- package/agents/critic.md +11 -11
- package/agents/executor.md +11 -11
- package/agents/explorer.md +11 -11
- package/agents/planner.md +11 -11
- package/agents/reviewer.md +11 -11
- package/agents/security-reviewer.md +11 -11
- package/agents/test-engineer.md +11 -11
- package/agents/verifier.md +11 -11
- package/agents/writer.md +11 -11
- package/docs/refactor-tasks-phase3.md +394 -394
- package/docs/refactor-tasks-phase4.md +564 -564
- package/docs/refactor-tasks-phase5.md +402 -402
- package/docs/refactor-tasks-phase6.md +662 -662
- package/docs/research-extension-examples.md +297 -297
- package/docs/research-extension-system.md +324 -324
- package/docs/research-optimization-plan.md +548 -548
- package/docs/research-phase10-distillation.md +199 -0
- package/docs/research-phase11-distillation.md +201 -0
- package/docs/research-pi-coding-agent.md +357 -357
- package/docs/research-source-pi-crew-reference.md +174 -174
- package/docs/runtime-flow.md +148 -148
- package/docs/source-runtime-refactor-map.md +83 -83
- package/index.ts +6 -6
- package/package.json +1 -1
- package/src/agents/agent-serializer.ts +34 -34
- package/src/agents/discover-agents.ts +5 -4
- package/src/config/config.ts +28 -4
- package/src/extension/cross-extension-rpc.ts +82 -82
- package/src/extension/management.ts +37 -8
- package/src/extension/notification-router.ts +2 -2
- package/src/extension/register.ts +130 -8
- package/src/extension/registration/commands.ts +11 -9
- package/src/extension/registration/compaction-guard.ts +125 -125
- package/src/extension/registration/subagent-tools.ts +28 -19
- package/src/extension/registration/team-tool.ts +2 -1
- package/src/extension/result-watcher.ts +4 -4
- package/src/extension/run-bundle-schema.ts +8 -4
- package/src/extension/run-import.ts +4 -0
- package/src/extension/run-index.ts +23 -1
- package/src/extension/run-maintenance.ts +43 -24
- package/src/extension/team-tool/api.ts +2 -2
- package/src/extension/team-tool/cancel.ts +76 -4
- package/src/extension/team-tool/context.ts +1 -0
- package/src/extension/team-tool/doctor.ts +8 -1
- package/src/extension/team-tool/handle-settings.ts +188 -0
- package/src/extension/team-tool/inspect.ts +41 -41
- package/src/extension/team-tool/lifecycle-actions.ts +79 -79
- package/src/extension/team-tool/plan.ts +19 -19
- package/src/extension/team-tool/respond.ts +67 -0
- package/src/extension/team-tool/run.ts +6 -4
- package/src/extension/team-tool/status.ts +99 -93
- package/src/extension/team-tool-types.ts +4 -0
- package/src/extension/team-tool.ts +5 -1
- package/src/i18n.ts +184 -0
- package/src/observability/correlation.ts +2 -2
- package/src/observability/event-to-metric.ts +10 -3
- package/src/observability/exporters/adapter.ts +7 -1
- package/src/observability/exporters/otlp-exporter.ts +14 -2
- package/src/observability/exporters/prometheus-exporter.ts +9 -2
- package/src/observability/metric-registry.ts +18 -3
- package/src/observability/metric-retention.ts +11 -3
- package/src/observability/metric-sink.ts +9 -4
- package/src/observability/metrics-primitives.ts +4 -3
- package/src/prompt/prompt-runtime.ts +72 -68
- package/src/runtime/agent-control.ts +63 -63
- package/src/runtime/agent-memory.ts +72 -72
- package/src/runtime/agent-observability.ts +114 -114
- package/src/runtime/async-marker.ts +26 -26
- package/src/runtime/attention-events.ts +28 -23
- package/src/runtime/background-runner.ts +53 -53
- package/src/runtime/child-pi.ts +4 -4
- package/src/runtime/completion-guard.ts +95 -4
- package/src/runtime/concurrency.ts +1 -1
- package/src/runtime/crash-recovery.ts +32 -1
- package/src/runtime/crew-agent-runtime.ts +59 -58
- package/src/runtime/deadletter.ts +14 -4
- package/src/runtime/delivery-coordinator.ts +143 -0
- package/src/runtime/direct-run.ts +35 -35
- package/src/runtime/foreground-control.ts +82 -82
- package/src/runtime/green-contract.ts +46 -46
- package/src/runtime/group-join.ts +106 -106
- package/src/runtime/heartbeat-gradient.ts +28 -28
- package/src/runtime/heartbeat-watcher.ts +48 -4
- package/src/runtime/live-agent-control.ts +87 -87
- package/src/runtime/live-agent-manager.ts +85 -85
- package/src/runtime/live-control-realtime.ts +36 -36
- package/src/runtime/live-session-runtime.ts +305 -305
- package/src/runtime/manifest-cache.ts +2 -2
- package/src/runtime/model-fallback.ts +272 -261
- package/src/runtime/overflow-recovery.ts +157 -0
- package/src/runtime/parallel-research.ts +44 -44
- package/src/runtime/parallel-utils.ts +1 -1
- package/src/runtime/pi-json-output.ts +111 -111
- package/src/runtime/policy-engine.ts +79 -78
- package/src/runtime/post-exit-stdio-guard.ts +2 -2
- package/src/runtime/process-status.ts +56 -56
- package/src/runtime/progress-event-coalescer.ts +43 -43
- package/src/runtime/recovery-recipes.ts +74 -74
- package/src/runtime/retry-executor.ts +5 -0
- package/src/runtime/role-permission.ts +39 -39
- package/src/runtime/runtime-resolver.ts +1 -1
- package/src/runtime/session-resources.ts +25 -0
- package/src/runtime/session-snapshot.ts +59 -0
- package/src/runtime/session-usage.ts +79 -79
- package/src/runtime/sidechain-output.ts +29 -29
- package/src/runtime/stale-reconciler.ts +179 -0
- package/src/runtime/subagent-manager.ts +3 -3
- package/src/runtime/supervisor-contact.ts +59 -0
- package/src/runtime/task-display.ts +38 -38
- package/src/runtime/task-output-context.ts +127 -127
- package/src/runtime/task-runner/live-executor.ts +101 -101
- package/src/runtime/task-runner/progress.ts +119 -111
- package/src/runtime/task-runner/result-utils.ts +14 -14
- package/src/runtime/task-runner/state-helpers.ts +22 -22
- package/src/runtime/task-runner.ts +14 -0
- package/src/runtime/team-runner.ts +9 -10
- package/src/runtime/worker-heartbeat.ts +21 -21
- package/src/runtime/worker-startup.ts +57 -57
- package/src/schema/config-schema.ts +2 -1
- package/src/schema/team-tool-schema.ts +115 -109
- package/src/state/artifact-store.ts +4 -2
- package/src/state/atomic-write.ts +12 -4
- package/src/state/contracts.ts +109 -105
- package/src/state/event-log.ts +3 -4
- package/src/state/jsonl-writer.ts +4 -1
- package/src/state/locks.ts +9 -1
- package/src/state/task-claims.ts +44 -42
- package/src/state/usage.ts +29 -29
- package/src/subagents/async-entry.ts +1 -1
- package/src/subagents/index.ts +3 -3
- package/src/subagents/live/control.ts +1 -1
- package/src/subagents/live/manager.ts +1 -1
- package/src/subagents/live/realtime.ts +1 -1
- package/src/subagents/live/session-runtime.ts +1 -1
- package/src/subagents/manager.ts +1 -1
- package/src/subagents/spawn.ts +1 -1
- package/src/teams/discover-teams.ts +2 -2
- package/src/teams/team-serializer.ts +38 -38
- package/src/types/diff.d.ts +18 -18
- package/src/ui/crew-footer.ts +101 -101
- package/src/ui/crew-select-list.ts +111 -111
- package/src/ui/crew-widget.ts +5 -4
- package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
- package/src/ui/dynamic-border.ts +25 -25
- package/src/ui/layout-primitives.ts +106 -106
- package/src/ui/live-run-sidebar.ts +1 -1
- package/src/ui/loaders.ts +158 -158
- package/src/ui/mascot.ts +3 -2
- package/src/ui/powerbar-publisher.ts +7 -6
- package/src/ui/render-diff.ts +119 -119
- package/src/ui/render-scheduler.ts +54 -14
- package/src/ui/run-dashboard.ts +39 -11
- package/src/ui/run-snapshot-cache.ts +336 -36
- package/src/ui/spinner.ts +17 -17
- package/src/ui/status-colors.ts +58 -54
- package/src/ui/syntax-highlight.ts +116 -116
- package/src/ui/theme-adapter.ts +1 -1
- package/src/ui/transcript-viewer.ts +7 -2
- package/src/utils/atomic-write.ts +33 -0
- package/src/utils/completion-dedupe.ts +63 -63
- package/src/utils/file-coalescer.ts +5 -3
- package/src/utils/frontmatter.ts +68 -36
- package/src/utils/git.ts +262 -262
- package/src/utils/ids.ts +12 -12
- package/src/utils/internal-error.ts +1 -1
- package/src/utils/names.ts +27 -26
- package/src/utils/paths.ts +1 -1
- package/src/utils/redaction.ts +44 -41
- package/src/utils/safe-paths.ts +47 -34
- package/src/utils/sleep.ts +2 -2
- package/src/utils/timings.ts +2 -0
- package/src/utils/visual.ts +9 -1
- package/src/workflows/discover-workflows.ts +4 -1
- package/src/workflows/validate-workflow.ts +40 -40
- package/src/worktree/branch-freshness.ts +45 -45
- package/src/worktree/worktree-manager.ts +6 -1
- package/teams/default.team.md +12 -12
- package/teams/fast-fix.team.md +11 -11
- package/teams/implementation.team.md +18 -18
- package/teams/parallel-research.team.md +14 -14
- package/teams/research.team.md +11 -11
- package/teams/review.team.md +12 -12
- package/workflows/default.workflow.md +29 -29
- package/workflows/fast-fix.workflow.md +22 -22
- package/workflows/implementation.workflow.md +38 -38
- package/workflows/parallel-research.workflow.md +46 -46
- package/workflows/research.workflow.md +22 -22
- package/workflows/review.workflow.md +30 -30
|
@@ -1,548 +1,548 @@
|
|
|
1
|
-
# Plan: pi-crew Optimization Opportunities
|
|
2
|
-
|
|
3
|
-
> Ngày: 2026-04-29 | Revised: 2026-04-29 (after design review)
|
|
4
|
-
> Based on: research-pi-coding-agent.md, research-extension-system.md, research-extension-examples.md
|
|
5
|
-
|
|
6
|
-
## Overview
|
|
7
|
-
|
|
8
|
-
Sau khi đọc sâu extension system của pi-mono và toàn bộ 60+ example extensions, dưới đây là
|
|
9
|
-
danh sách cơ hội tối ưu cho pi-crew, được phân loại theo effort và impact.
|
|
10
|
-
|
|
11
|
-
**Revision notes (2026-04-29):**
|
|
12
|
-
- Re-order Phase 1 để compliance-required task (permission gate) đi trước optimization task.
|
|
13
|
-
- Tách `terminate: true` thành 2 sub-task vì rủi ro UX khác nhau.
|
|
14
|
-
- Hạ "custom compaction model" từ Phase 2 xuống Phase 3 (risk vs ROI).
|
|
15
|
-
- Đổi cancel-compaction thành **defer + retry** (tránh context overflow).
|
|
16
|
-
- Threshold compaction động theo `contextWindow` thay vì hardcode 150k.
|
|
17
|
-
- Thêm rollback strategy ở cấp roadmap + gap research bổ sung.
|
|
18
|
-
|
|
19
|
-
## Priority Matrix
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
Impact
|
|
23
|
-
↑
|
|
24
|
-
│ HIGH │ HIGH │
|
|
25
|
-
│ Effort │ Effort │
|
|
26
|
-
│ LOW │ MEDIUM │
|
|
27
|
-
│ ───────┼─────────│
|
|
28
|
-
│ MEDIUM │ LOW │
|
|
29
|
-
│ Effort │ Effort │
|
|
30
|
-
│ LOW │ MEDIUM │
|
|
31
|
-
└──────────────────→ Effort
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Implementation Status (2026-04-29)
|
|
35
|
-
|
|
36
|
-
Implemented in code:
|
|
37
|
-
|
|
38
|
-
- Phase 1.4 permission gate for destructive `team` tool calls.
|
|
39
|
-
- Phase 1.6 telemetry baseline fields for subagent completion (`turnCount`, `terminated`, `durationMs`).
|
|
40
|
-
- Phase 1.2 compaction guard as defer + retry, moved into `src/extension/registration/compaction-guard.ts`.
|
|
41
|
-
- Phase 1.1a `terminate: true` for background/queued subagent launches.
|
|
42
|
-
- Phase 1.3 public event bus events (`crew.subagent.completed`, `crew.run.completed`, `crew.run.failed`, `crew.run.cancelled`).
|
|
43
|
-
- Phase 1.5 auto session naming for new team runs when no custom session name exists.
|
|
44
|
-
- Phase 2.1 proactive compaction with dynamic context-window threshold.
|
|
45
|
-
- Phase 2.3 Pi session entries for run start/completion (`crew:run-started`, `crew:run-completed`).
|
|
46
|
-
- Phase 2.4 config-driven subagent tool aliases via `config.tools`.
|
|
47
|
-
- Phase 2.5 foreground working indicator, using optional API compatibility shim because older `pi-coding-agent` type surfaces may not expose `ctx.ui.setWorkingIndicator`.
|
|
48
|
-
- Phase 3.3 safe mailbox event bus publication (`crew.mailbox.message`, `crew.mailbox.acknowledged`).
|
|
49
|
-
|
|
50
|
-
Deferred by design:
|
|
51
|
-
|
|
52
|
-
- Phase 1.1b foreground `terminate: true` is implemented as opt-in via `config.tools.terminateOnForeground=true`; default remains safe/off pending telemetry.
|
|
53
|
-
- Phase 3.4 structured artifact index is implemented for pi-crew-triggered compactions via `crew:artifact-index` session entries plus compaction custom instructions. Direct `CompactionEntry.details` augmentation is not available through the current upstream extension API without replacing default compaction.
|
|
54
|
-
- Phase 3.1, 3.3b, 3.5, and 4.2 are now marked won't-do/research-only after deeper risk/ROI analysis.
|
|
55
|
-
- Phase 3.2 remains conditional on agent-level opt-in design. Phase 4.1 remains deferred pending format-compat research.
|
|
56
|
-
|
|
57
|
-
Validation:
|
|
58
|
-
|
|
59
|
-
- `npm run typecheck` passes.
|
|
60
|
-
- `npm test` passes: 283 unit tests + 26 integration tests.
|
|
61
|
-
|
|
62
|
-
## Roadmap-level Rollback Strategy
|
|
63
|
-
|
|
64
|
-
- **1 sub-task = 1 commit** có thể revert độc lập. KHÔNG gộp toàn bộ Phase 1 vào 1 commit.
|
|
65
|
-
- Mỗi commit phải có test riêng. Nếu fail trong production, `git revert <sha>` không kéo theo task khác.
|
|
66
|
-
- Phase 1.6 (telemetry) làm trước Phase 1.1 để có baseline đo lường.
|
|
67
|
-
|
|
68
|
-
---
|
|
69
|
-
|
|
70
|
-
## Phase 1: Quick Wins & Compliance (HIGH impact, LOW effort)
|
|
71
|
-
|
|
72
|
-
Thời gian ước tính: 2-3 sessions. **Thứ tự đã re-order so với research gốc.**
|
|
73
|
-
|
|
74
|
-
### 1.4 (FIRST) Permission gate cho destructive team actions
|
|
75
|
-
|
|
76
|
-
**Lý do làm trước:** AGENTS.md quy định *"Management deletes must require confirm: true; referenced
|
|
77
|
-
resources blocked unless force: true"* — đây là **rule bắt buộc**, không phải optimization.
|
|
78
|
-
|
|
79
|
-
**Files cần sửa:** `src/extension/registration/team-tool.ts` (hoặc file mới)
|
|
80
|
-
|
|
81
|
-
**Hiện tại:** Có check trong handler nhưng không có `tool_call` hook → message lỗi không nhất quán.
|
|
82
|
-
|
|
83
|
-
**Tối ưu:**
|
|
84
|
-
```typescript
|
|
85
|
-
pi.on("tool_call", async (event, ctx) => {
|
|
86
|
-
if (event.toolName !== "team") return;
|
|
87
|
-
const input = event.input as Record<string, unknown>;
|
|
88
|
-
const destructiveActions = ["delete", "forget", "prune", "cleanup"];
|
|
89
|
-
|
|
90
|
-
if (destructiveActions.includes(input.action as string)) {
|
|
91
|
-
if (!input.confirm && !input.force) {
|
|
92
|
-
return {
|
|
93
|
-
block: true,
|
|
94
|
-
reason: `Destructive action '${input.action}' requires confirm=true (or force=true to bypass)`,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
**Note về precedence:** Nếu schema validate đã check `confirm`, **CHỌN 1 chỗ duy nhất**:
|
|
102
|
-
- Option A: Để schema validate → bỏ hook (đơn giản hơn).
|
|
103
|
-
- Option B: Để hook validate → gỡ check trong handler (consistent error message).
|
|
104
|
-
|
|
105
|
-
→ Đề nghị Option B vì hook gate tất cả entry points (kể cả nếu sau này có entry point bypass schema).
|
|
106
|
-
|
|
107
|
-
**Expected benefit:** Compliance với AGENTS.md, safety net production.
|
|
108
|
-
|
|
109
|
-
---
|
|
110
|
-
|
|
111
|
-
### 1.6 (NEW) Telemetry baseline cho terminate impact
|
|
112
|
-
|
|
113
|
-
**Lý do làm trước 1.1:** Plan gốc claim "giảm 30-50% LLM turns" — chỉ là phỏng đoán. Cần baseline đo lường thực tế.
|
|
114
|
-
|
|
115
|
-
**Files cần sửa:** `src/runtime/subagent-manager.ts`, `src/extension/register.ts`
|
|
116
|
-
|
|
117
|
-
**Tối ưu:** Log `turnCount` + `terminated: boolean` vào event `crew.subagent.completed`:
|
|
118
|
-
```typescript
|
|
119
|
-
pi.events.emit("crew.subagent.completed", {
|
|
120
|
-
id: record.id,
|
|
121
|
-
runId: record.runId,
|
|
122
|
-
type: record.type,
|
|
123
|
-
status: record.status,
|
|
124
|
-
usage: record.usage,
|
|
125
|
-
turnCount: record.turnCount, // ← NEW
|
|
126
|
-
terminated: record.terminated, // ← NEW (false trước Phase 1.1)
|
|
127
|
-
durationMs: record.durationMs, // ← NEW
|
|
128
|
-
});
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
**Expected benefit:** Đo trước/sau Phase 1.1 để xác định ROI thực tế. Nếu < 10% turn saving, có thể quyết định không deploy 1.1b.
|
|
132
|
-
|
|
133
|
-
---
|
|
134
|
-
|
|
135
|
-
### 1.2 `session_before_compact` guard cho foreground runs (DEFER, không CANCEL)
|
|
136
|
-
|
|
137
|
-
**Files cần sửa:** `src/extension/register.ts`
|
|
138
|
-
|
|
139
|
-
**Hiện tại:** Không hook compaction → có thể compact giữa chừng foreground run.
|
|
140
|
-
|
|
141
|
-
**Tối ưu (revised):** Defer + retry thay vì cancel cứng (tránh context overflow):
|
|
142
|
-
```typescript
|
|
143
|
-
let pendingCompactReason: string | null = null;
|
|
144
|
-
|
|
145
|
-
pi.on("session_before_compact", async (event, ctx) => {
|
|
146
|
-
if (foregroundControllers.size > 0) {
|
|
147
|
-
pendingCompactReason = "deferred-during-foreground-run";
|
|
148
|
-
ctx.ui.notify("Compaction deferred until foreground run completes", "info");
|
|
149
|
-
return { cancel: true };
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// Retry sau khi run xong:
|
|
154
|
-
pi.on("turn_end", (_event, ctx) => {
|
|
155
|
-
if (foregroundControllers.size === 0 && pendingCompactReason) {
|
|
156
|
-
pendingCompactReason = null;
|
|
157
|
-
ctx.compact({
|
|
158
|
-
onComplete: () => ctx.ui.notify("Deferred compaction completed", "info"),
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
**Expected benefit:** Ngăn lỗi context mất mát trong foreground run, vẫn đảm bảo compact eventually chạy.
|
|
165
|
-
|
|
166
|
-
**Risk:** Nếu run cực dài + foregroundControllers chưa bao giờ về 0 → vẫn overflow. Mitigation: hard threshold (vd 95% context window) bypass deferral, force compact.
|
|
167
|
-
|
|
168
|
-
---
|
|
169
|
-
|
|
170
|
-
### 1.1a `terminate: true` cho **background queued** results (SAFE)
|
|
171
|
-
|
|
172
|
-
**Lý do tách:** Background queue không có UX risk, foreground completed có risk (xem 1.1b).
|
|
173
|
-
|
|
174
|
-
**Files cần sửa:** `src/extension/registration/subagent-tools.ts`
|
|
175
|
-
|
|
176
|
-
**Tối ưu:**
|
|
177
|
-
```typescript
|
|
178
|
-
// Agent tool — khi background: terminate ngay sau khi đã queued
|
|
179
|
-
if (params.run_in_background) {
|
|
180
|
-
return {
|
|
181
|
-
...subagentToolResult(...),
|
|
182
|
-
terminate: true, // ← Tiết kiệm 1 LLM turn, không có rủi ro UX
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
**Expected benefit:** Giảm LLM turn cho mọi background spawn. Verify bằng telemetry từ 1.6.
|
|
188
|
-
|
|
189
|
-
---
|
|
190
|
-
|
|
191
|
-
### 1.3 Public events qua `pi.events`
|
|
192
|
-
|
|
193
|
-
**Files cần sửa:** `src/extension/register.ts`
|
|
194
|
-
|
|
195
|
-
**Hiện tại:** Event bus chỉ dùng cho internal `subagent.stuck-blocked`.
|
|
196
|
-
|
|
197
|
-
**Naming convention (revised):** Thống nhất với upstream pattern `dot.kebab` (đã dùng cho `subagent.stuck-blocked`):
|
|
198
|
-
```typescript
|
|
199
|
-
// Document trong README là PUBLIC API:
|
|
200
|
-
pi.events.emit("crew.subagent.completed", { ... });
|
|
201
|
-
pi.events.emit("crew.run.completed", { runId, team, workflow, status, taskCount, totalUsage });
|
|
202
|
-
pi.events.emit("crew.run.failed", { runId, team, workflow, error, failedTaskId });
|
|
203
|
-
pi.events.emit("crew.run.cancelled", { runId, team, workflow, status, taskCount });
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
**Versioning:** Note trong README rằng event payload là semver-stable từ pi-crew 0.2.0.
|
|
207
|
-
|
|
208
|
-
**Expected benefit:** Extension khác (logging, notification, metrics) có thể subscribe.
|
|
209
|
-
|
|
210
|
-
---
|
|
211
|
-
|
|
212
|
-
### 1.5 Auto session name từ team run context
|
|
213
|
-
|
|
214
|
-
**Files cần sửa:** `src/extension/registration/team-tool.ts`
|
|
215
|
-
|
|
216
|
-
**Tối ưu:**
|
|
217
|
-
```typescript
|
|
218
|
-
// Trong team tool execute, trước khi start run:
|
|
219
|
-
pi.setSessionName(`pi-crew: ${team}/${workflow} — ${goal.slice(0, 60)}`);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
**Expected benefit:** Better session organization khi xem session list.
|
|
223
|
-
|
|
224
|
-
---
|
|
225
|
-
|
|
226
|
-
### 1.1b (OPT-IN DONE, DEFAULT OFF) `terminate: true` cho **foreground completed** results
|
|
227
|
-
|
|
228
|
-
**Lý do default off:** UX risk — nếu LLM không có turn để summarize result, user có thể không hiểu output.
|
|
229
|
-
|
|
230
|
-
**Implementation:** opt-in flag, default safe:
|
|
231
|
-
|
|
232
|
-
```json
|
|
233
|
-
{
|
|
234
|
-
"tools": {
|
|
235
|
-
"terminateOnForeground": true
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
When enabled, foreground `Agent`/`crew_agent` completed results set `terminate: true` and persist `record.terminated=true` for telemetry. Decision to make this default-on still requires telemetry evidence:
|
|
241
|
-
|
|
242
|
-
- Average turn count sau Agent foreground completion ≥ 2.
|
|
243
|
-
- Output đã đủ self-explanatory (đo qua user feedback hoặc retry rate).
|
|
244
|
-
|
|
245
|
-
---
|
|
246
|
-
|
|
247
|
-
## Phase 2: Medium Effort Optimizations
|
|
248
|
-
|
|
249
|
-
Thời gian ước tính: 2-3 sessions. (Đã giảm 1 task so với plan gốc.)
|
|
250
|
-
|
|
251
|
-
### 2.1 Proactive compaction monitoring (DYNAMIC threshold)
|
|
252
|
-
|
|
253
|
-
**Files cần sửa:** File mới `src/extension/registration/compaction-guard.ts`
|
|
254
|
-
|
|
255
|
-
**Hiện tại:** Chỉ dựa vào built-in auto-compaction (có thể chậm).
|
|
256
|
-
|
|
257
|
-
**Tối ưu (revised):** Threshold động theo `contextWindow`:
|
|
258
|
-
```typescript
|
|
259
|
-
export function registerCompactionGuard(pi: ExtensionAPI) {
|
|
260
|
-
const TRIGGER_RATIO = 0.75; // 75% context window → trigger
|
|
261
|
-
|
|
262
|
-
pi.on("turn_end", (_event, ctx) => {
|
|
263
|
-
const usage = ctx.getContextUsage();
|
|
264
|
-
const ctxWindow = ctx.model?.contextWindow ?? 200_000;
|
|
265
|
-
const threshold = ctxWindow * TRIGGER_RATIO;
|
|
266
|
-
|
|
267
|
-
if (usage?.tokens && usage.tokens > threshold) {
|
|
268
|
-
// Foreground guard từ Phase 1.2 sẽ defer nếu cần
|
|
269
|
-
ctx.compact({
|
|
270
|
-
customInstructions: "Prioritize keeping team run state, task results, and artifact references. Keep the conversation context brief.",
|
|
271
|
-
onComplete: () => ctx.ui.notify("Auto-compacted context during team run", "info"),
|
|
272
|
-
onError: (err) => ctx.ui.notify(`Compaction failed: ${err.message}`, "error"),
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
**Lý do dùng ratio thay vì hardcode:** Claude Haiku 200k, Gemini Pro 2M, GPT-4o 128k, model nhỏ 32k. Hardcode 150k sai cho 90% trường hợp.
|
|
280
|
-
|
|
281
|
-
**Expected benefit:** Tránh context overflow error khi foreground run quá dài.
|
|
282
|
-
|
|
283
|
-
---
|
|
284
|
-
|
|
285
|
-
### 2.3 `pi.appendEntry` cho cross-session run awareness
|
|
286
|
-
|
|
287
|
-
**Files cần sửa:** `src/extension/register.ts`
|
|
288
|
-
|
|
289
|
-
**Tối ưu:**
|
|
290
|
-
```typescript
|
|
291
|
-
// Khi bắt đầu run:
|
|
292
|
-
pi.appendEntry("crew:run-started", {
|
|
293
|
-
runId, team, workflow, goal, timestamp: Date.now(),
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
// Khi hoàn thành run:
|
|
297
|
-
pi.appendEntry("crew:run-completed", {
|
|
298
|
-
runId, status, taskCount, totalUsage, timestamp: Date.now(),
|
|
299
|
-
});
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
**Expected benefit:**
|
|
303
|
-
- Khi reload session, biết được các run liên quan.
|
|
304
|
-
- Session export bao gồm run context.
|
|
305
|
-
- Dễ dàng track history.
|
|
306
|
-
|
|
307
|
-
---
|
|
308
|
-
|
|
309
|
-
### 2.4 Config-driven tool registration
|
|
310
|
-
|
|
311
|
-
**Files cần sửa:** `src/extension/registration/subagent-tools.ts`
|
|
312
|
-
|
|
313
|
-
**Hiện tại:** Luôn register 6 tool variants (Agent, crew_agent, + result + steer).
|
|
314
|
-
|
|
315
|
-
**Tối ưu:**
|
|
316
|
-
```typescript
|
|
317
|
-
export function registerSubagentTools(pi: ExtensionAPI, subagentManager: SubagentManager) {
|
|
318
|
-
const cfg = loadConfig(pi.getFlag("cwd") as string || process.cwd());
|
|
319
|
-
|
|
320
|
-
// Conflict-safe tools (luôn register)
|
|
321
|
-
pi.registerTool(crewAgentTool);
|
|
322
|
-
pi.registerTool(crewAgentResultTool);
|
|
323
|
-
|
|
324
|
-
// Claude-style aliases: only if not disabled
|
|
325
|
-
if (cfg.config.tools?.enableClaudeStyleAliases !== false) {
|
|
326
|
-
try { pi.registerTool(agentTool); } catch {}
|
|
327
|
-
try { pi.registerTool(getSubagentResultTool); } catch {}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// Steer: only if supported
|
|
331
|
-
if (cfg.config.tools?.enableSteer !== false) {
|
|
332
|
-
try { pi.registerTool(crewAgentSteerTool); } catch {}
|
|
333
|
-
try { pi.registerTool(steerSubagentTool); } catch {}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
**Expected benefit:** Tránh pollute tool namespace, fine-grained control cho user.
|
|
339
|
-
|
|
340
|
-
---
|
|
341
|
-
|
|
342
|
-
### 2.5 Custom working indicator trong foreground runs
|
|
343
|
-
|
|
344
|
-
**Files cần sửa:** `src/extension/register.ts`
|
|
345
|
-
|
|
346
|
-
**Tối ưu:**
|
|
347
|
-
```typescript
|
|
348
|
-
// Khi foreground run active:
|
|
349
|
-
ctx.ui.setWorkingIndicator({
|
|
350
|
-
frames: ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"],
|
|
351
|
-
intervalMs: 80,
|
|
352
|
-
});
|
|
353
|
-
ctx.ui.setWorkingMessage(
|
|
354
|
-
`Team run: ${completedTasks}/${totalTasks} tasks done...`
|
|
355
|
-
);
|
|
356
|
-
|
|
357
|
-
// Khi kết thúc:
|
|
358
|
-
ctx.ui.setWorkingIndicator(); // Restore default
|
|
359
|
-
ctx.ui.setWorkingMessage(); // Clear
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
**Compat shim note:** Implementation dùng optional API compatibility shim:
|
|
363
|
-
|
|
364
|
-
```typescript
|
|
365
|
-
(ctx.ui as { setWorkingIndicator?: (...) => void }).setWorkingIndicator?.(...)
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
Lý do: một số version/type surface của `@mariozechner/pi-coding-agent` chưa expose
|
|
369
|
-
`setWorkingIndicator` trên `ExtensionUIContext`. Optional shim giữ backward compatibility và
|
|
370
|
-
tránh crash/runtime type mismatch; nếu API không tồn tại thì chỉ bỏ qua custom spinner và vẫn dùng
|
|
371
|
-
`setWorkingMessage()`.
|
|
372
|
-
|
|
373
|
-
**Expected benefit:** Better UX, cho user biết team run đang chạy.
|
|
374
|
-
|
|
375
|
-
---
|
|
376
|
-
|
|
377
|
-
## Phase 3: Future Considerations (HIGH effort hoặc Risky)
|
|
378
|
-
|
|
379
|
-
### 3.1 (WON'T DO unless concrete pain point appears) Branch-level task isolation
|
|
380
|
-
|
|
381
|
-
Dùng `ctx.fork()` để tạo branch mới cho mỗi task trong team run.
|
|
382
|
-
|
|
383
|
-
**Decision:** không triển khai mặc định. Worktree isolation đã giải quyết phần quan trọng nhất (file-system/task isolation). Branch-level isolation tạo branch explosion, navigation UX phức tạp, và state-sync risk giữa flat run manifest/tasks/events với Pi session tree. Chỉ reconsider nếu có user complaint cụ thể về context contamination không giải quyết được bằng worktree/dependency-context controls.
|
|
384
|
-
|
|
385
|
-
### 3.2 Session handoff cho long-running tasks
|
|
386
|
-
|
|
387
|
-
Khi 1 task quá dài, handoff sang session mới (pattern từ `handoff.ts`), isolate context.
|
|
388
|
-
|
|
389
|
-
**Conditional trigger:** chỉ enable cho agent/task opt-in, ví dụ agent frontmatter `handoff: true`, hoặc heuristic token estimate > 30% context window.
|
|
390
|
-
|
|
391
|
-
**Result transport:** child session trả về artifact reference hoặc mailbox message để parent session vẫn aggregate được kết quả mà không cần import toàn bộ transcript.
|
|
392
|
-
|
|
393
|
-
### 3.3 Mailbox qua `pi.events`
|
|
394
|
-
|
|
395
|
-
#### 3.3a (DONE) Publish mailbox lifecycle events while preserving file-backed mailbox
|
|
396
|
-
|
|
397
|
-
Implementation publishes safe public events without changing the durable mailbox source of truth:
|
|
398
|
-
|
|
399
|
-
```typescript
|
|
400
|
-
pi.events.emit("crew.mailbox.message", { runId, id, direction, from, to, taskId, source });
|
|
401
|
-
pi.events.emit("crew.mailbox.acknowledged", { runId, messageId, delivery });
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
This keeps file-backed mailbox semantics intact while enabling observers/notification extensions.
|
|
405
|
-
|
|
406
|
-
#### 3.3b (WON'T DO) Replace file-backed mailbox with pure event-bus mailbox
|
|
407
|
-
|
|
408
|
-
Thay vì file-based mailbox, dùng event bus làm transport chính cho real-time communication giữa tasks.
|
|
409
|
-
|
|
410
|
-
**Decision:** won't do. Latency gain is marginal; durability/restart/replay loss is catastrophic for long-running pi-crew runs. 3.3a gives best-of-both-worlds: durable file-backed mailbox remains source of truth, event bus is an observer/notification layer.
|
|
411
|
-
|
|
412
|
-
### 3.4 (PROMOTED + DONE) Compaction với structured artifact index
|
|
413
|
-
|
|
414
|
-
Preserve pi-crew artifact references across compaction.
|
|
415
|
-
|
|
416
|
-
**Implementation:** `compaction-guard.ts` collects recent run artifacts and:
|
|
417
|
-
|
|
418
|
-
- appends a structured `crew:artifact-index` session entry for machine-readable continuity;
|
|
419
|
-
- adds a markdown artifact index to pi-crew-triggered compaction `customInstructions` so the compaction summary preserves run IDs and artifact paths.
|
|
420
|
-
|
|
421
|
-
**Note:** Directly augmenting `CompactionEntry.details` is not supported by the current upstream `session_before_compact` result contract unless pi-crew replaces default compaction entirely. We intentionally avoid full custom compaction because summary quality/regression risk is higher.
|
|
422
|
-
|
|
423
|
-
### 3.5 (WON'T DO unless cost telemetry shows pain) Custom compaction với model nhẹ
|
|
424
|
-
|
|
425
|
-
**Decision:** won't do by default.
|
|
426
|
-
|
|
427
|
-
- Phụ thuộc vào auth setup của user cho Gemini Flash / Haiku — pi-crew không kiểm soát được.
|
|
428
|
-
- Bad summary làm mất context → ảnh hưởng cả run.
|
|
429
|
-
- ROI không rõ: compaction chạy không thường xuyên.
|
|
430
|
-
|
|
431
|
-
Reconsider only if telemetry/user feedback shows compaction cost is a real pain point. Reference remains `examples/extensions/custom-compaction.ts` upstream.
|
|
432
|
-
|
|
433
|
-
---
|
|
434
|
-
|
|
435
|
-
## Phase 4 (NEW): Research bổ sung
|
|
436
|
-
|
|
437
|
-
Hai pattern upstream chưa được khai thác trong plan gốc:
|
|
438
|
-
|
|
439
|
-
### 4.1 (DEFER — research format compat first) `resources_discover` event integration
|
|
440
|
-
|
|
441
|
-
Pi-crew có thể inject builtin agents/teams như Pi resources native (skills/prompts):
|
|
442
|
-
```typescript
|
|
443
|
-
pi.on("resources_discover", () => ({
|
|
444
|
-
skillPaths: [path.join(__dirname, "..", "agents")],
|
|
445
|
-
promptPaths: [path.join(__dirname, "..", "workflows")],
|
|
446
|
-
}));
|
|
447
|
-
```
|
|
448
|
-
|
|
449
|
-
**Decision:** defer. Cần research format compat giữa pi-crew agent markdown vs Pi skill/prompt format trước khi implement. Key risk: dual exposure UX confusion (same capability reachable via `Agent` tool and native skill/prompt) plus loss of pi-crew durable run semantics if exposed as stateless skills.
|
|
450
|
-
|
|
451
|
-
### 4.2 (RESEARCH-ONLY) `pi.registerProvider` cho virtual "team" model
|
|
452
|
-
|
|
453
|
-
Đăng ký team như virtual provider để user gọi:
|
|
454
|
-
```bash
|
|
455
|
-
pi --model crew/researcher
|
|
456
|
-
```
|
|
457
|
-
Thay vì dùng tool `Agent`.
|
|
458
|
-
|
|
459
|
-
**Decision:** research-only / not an implementation target. Provider API semantics (single LLM stream, context window, thinking levels, token pricing) do not map cleanly to orchestrator semantics (multi-agent task events, aggregate usage/cost, per-worker contexts). Likely requires upstream provider API changes.
|
|
460
|
-
|
|
461
|
-
---
|
|
462
|
-
|
|
463
|
-
## Implementation Order (REVISED)
|
|
464
|
-
|
|
465
|
-
```
|
|
466
|
-
Phase 1 (Quick Wins & Compliance):
|
|
467
|
-
[x] 1.4 permission gate destructive team actions ← FIRST (compliance)
|
|
468
|
-
[x] 1.6 telemetry baseline ← SECOND (measure first)
|
|
469
|
-
[x] 1.2 session_before_compact defer (not cancel)
|
|
470
|
-
[x] 1.1a terminate: true on background queued (safe)
|
|
471
|
-
[x] 1.3 public crew.* events
|
|
472
|
-
[x] 1.5 auto session name
|
|
473
|
-
[x] 1.1b terminate: true on foreground (OPT-IN, default off; default-on conditional on telemetry)
|
|
474
|
-
|
|
475
|
-
Phase 2 (Medium):
|
|
476
|
-
[x] 2.1 proactive compaction (dynamic threshold)
|
|
477
|
-
[x] 2.3 pi.appendEntry cross-session awareness
|
|
478
|
-
[x] 2.4 config-driven tool registration
|
|
479
|
-
[x] 2.5 custom working indicator
|
|
480
|
-
|
|
481
|
-
Phase 3 (Future / Risky):
|
|
482
|
-
[-] 3.1 branch-level task isolation (WON'T DO unless concrete pain point appears)
|
|
483
|
-
[ ] 3.2 session handoff for long tasks (CONDITIONAL on agent opt-in)
|
|
484
|
-
[x] 3.3a publish mailbox lifecycle events (safe subset)
|
|
485
|
-
[-] 3.3b replace file-backed mailbox with pure event bus (WON'T DO)
|
|
486
|
-
[x] 3.4 structured artifact index in compaction (promoted/done)
|
|
487
|
-
[-] 3.5 custom compaction with cheap model (WON'T DO unless cost telemetry shows pain)
|
|
488
|
-
|
|
489
|
-
Phase 4 (Research):
|
|
490
|
-
[ ] 4.1 resources_discover integration (DEFER; format compat research first)
|
|
491
|
-
[-] 4.2 virtual team provider (RESEARCH-ONLY)
|
|
492
|
-
```
|
|
493
|
-
|
|
494
|
-
## Files affected
|
|
495
|
-
|
|
496
|
-
```
|
|
497
|
-
PHASE 1:
|
|
498
|
-
src/extension/registration/team-tool.ts ← 1.4 permission gate
|
|
499
|
-
src/extension/registration/subagent-tools.ts ← 1.1a terminate + 1.1b opt-in terminate
|
|
500
|
-
src/extension/register.ts ← 1.2 defer guard, 1.3 events, 1.5 session name
|
|
501
|
-
src/runtime/subagent-manager.ts ← 1.6 telemetry fields
|
|
502
|
-
|
|
503
|
-
PHASE 2:
|
|
504
|
-
src/extension/registration/compaction-guard.ts ← NEW: 1.2 defer guard + 2.1 proactive + 3.4 artifact index
|
|
505
|
-
src/extension/register.ts ← 2.3 appendEntry, 2.5 working indicator
|
|
506
|
-
src/extension/registration/subagent-tools.ts ← 2.4 config-driven
|
|
507
|
-
|
|
508
|
-
PHASE 3:
|
|
509
|
-
src/extension/team-tool/api.ts ← 3.3a mailbox lifecycle events
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
## Risk Assessment (REVISED)
|
|
513
|
-
|
|
514
|
-
| Change | Risk | Mitigation |
|
|
515
|
-
|---|---|---|
|
|
516
|
-
| Permission gate (1.4) | Block legitimate use | Allow `force=true` bypass, document trong README |
|
|
517
|
-
| Telemetry (1.6) | Privacy / log size | No PII in subagent telemetry payload; opt-out applied via `config.telemetry.enabled=false`; no sampling currently because payload is small/local event-bus data |
|
|
518
|
-
| Defer compaction (1.2) | Run dài infinite → overflow | Hard threshold 95% bypass deferral |
|
|
519
|
-
| `terminate: true` background (1.1a) | None significant | Background không cần LLM follow-up by design |
|
|
520
|
-
| Public events (1.3) | Event storm, breaking change | Rate limit, semver document |
|
|
521
|
-
| Auto session name (1.5) | Override user-set name | Applied: chỉ set nếu chưa có name custom (`!pi.getSessionName()`) |
|
|
522
|
-
| `terminate: true` foreground (1.1b) | LLM không summarize khi enabled | OPT-IN flag (`config.tools.terminateOnForeground`, default off); default-on requires telemetry evidence |
|
|
523
|
-
| Dynamic threshold (2.1) | contextWindow undefined | Default 200_000 fallback |
|
|
524
|
-
| Artifact index in compaction (3.4) | Index size bloat / format drift | Cap recent index (10 runs / 80 artifacts), structured `crew:artifact-index` session entry, non-replacing default compaction |
|
|
525
|
-
| appendEntry (2.3) | Session bloat | TTL/cleanup strategy |
|
|
526
|
-
| Config-driven tools (2.4) | User confused | Default = current behavior, opt-in change |
|
|
527
|
-
| Working indicator (2.5) | Conflict với extension khác / older Pi UI type surface | Applied: restore default on finally; compat shim makes `setWorkingIndicator` optional |
|
|
528
|
-
| Custom compaction model (3.5) | Bad summary, auth missing | Fall back to default, multi-model retry |
|
|
529
|
-
|
|
530
|
-
## Testing Strategy
|
|
531
|
-
|
|
532
|
-
- **Unit tests:**
|
|
533
|
-
- `terminate: true` flag in tool results (1.1a/b).
|
|
534
|
-
- Permission gate blocks/allows correctly với confirm/force matrix (1.4).
|
|
535
|
-
- Threshold calculation từ contextWindow (2.1).
|
|
536
|
-
- Telemetry payload schema (1.6).
|
|
537
|
-
- Artifact index payload structure + cap behavior (3.4).
|
|
538
|
-
- **Integration tests:**
|
|
539
|
-
- Foreground run + compaction interaction (1.2 defer + 2.1 trigger).
|
|
540
|
-
- Multiple concurrent runs + permission gate (1.4).
|
|
541
|
-
- Event publish/subscribe round-trip (1.3).
|
|
542
|
-
- Compaction with N artifacts includes artifact index in custom instructions (3.4).
|
|
543
|
-
- **Manual:**
|
|
544
|
-
- UI behavior với working indicator + session name (1.5, 2.5).
|
|
545
|
-
- Real LLM turn count trước/sau 1.1b với telemetry data (1.6 → 1.1b decision).
|
|
546
|
-
- **Regression:**
|
|
547
|
-
- Run full suite (`npm test`) sau mỗi commit, không gộp Phase.
|
|
548
|
-
- Doctor tests phải dùng `--test-timeout=90000` trên Windows.
|
|
1
|
+
# Plan: pi-crew Optimization Opportunities
|
|
2
|
+
|
|
3
|
+
> Ngày: 2026-04-29 | Revised: 2026-04-29 (after design review)
|
|
4
|
+
> Based on: research-pi-coding-agent.md, research-extension-system.md, research-extension-examples.md
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
Sau khi đọc sâu extension system của pi-mono và toàn bộ 60+ example extensions, dưới đây là
|
|
9
|
+
danh sách cơ hội tối ưu cho pi-crew, được phân loại theo effort và impact.
|
|
10
|
+
|
|
11
|
+
**Revision notes (2026-04-29):**
|
|
12
|
+
- Re-order Phase 1 để compliance-required task (permission gate) đi trước optimization task.
|
|
13
|
+
- Tách `terminate: true` thành 2 sub-task vì rủi ro UX khác nhau.
|
|
14
|
+
- Hạ "custom compaction model" từ Phase 2 xuống Phase 3 (risk vs ROI).
|
|
15
|
+
- Đổi cancel-compaction thành **defer + retry** (tránh context overflow).
|
|
16
|
+
- Threshold compaction động theo `contextWindow` thay vì hardcode 150k.
|
|
17
|
+
- Thêm rollback strategy ở cấp roadmap + gap research bổ sung.
|
|
18
|
+
|
|
19
|
+
## Priority Matrix
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
Impact
|
|
23
|
+
↑
|
|
24
|
+
│ HIGH │ HIGH │
|
|
25
|
+
│ Effort │ Effort │
|
|
26
|
+
│ LOW │ MEDIUM │
|
|
27
|
+
│ ───────┼─────────│
|
|
28
|
+
│ MEDIUM │ LOW │
|
|
29
|
+
│ Effort │ Effort │
|
|
30
|
+
│ LOW │ MEDIUM │
|
|
31
|
+
└──────────────────→ Effort
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Implementation Status (2026-04-29)
|
|
35
|
+
|
|
36
|
+
Implemented in code:
|
|
37
|
+
|
|
38
|
+
- Phase 1.4 permission gate for destructive `team` tool calls.
|
|
39
|
+
- Phase 1.6 telemetry baseline fields for subagent completion (`turnCount`, `terminated`, `durationMs`).
|
|
40
|
+
- Phase 1.2 compaction guard as defer + retry, moved into `src/extension/registration/compaction-guard.ts`.
|
|
41
|
+
- Phase 1.1a `terminate: true` for background/queued subagent launches.
|
|
42
|
+
- Phase 1.3 public event bus events (`crew.subagent.completed`, `crew.run.completed`, `crew.run.failed`, `crew.run.cancelled`).
|
|
43
|
+
- Phase 1.5 auto session naming for new team runs when no custom session name exists.
|
|
44
|
+
- Phase 2.1 proactive compaction with dynamic context-window threshold.
|
|
45
|
+
- Phase 2.3 Pi session entries for run start/completion (`crew:run-started`, `crew:run-completed`).
|
|
46
|
+
- Phase 2.4 config-driven subagent tool aliases via `config.tools`.
|
|
47
|
+
- Phase 2.5 foreground working indicator, using optional API compatibility shim because older `pi-coding-agent` type surfaces may not expose `ctx.ui.setWorkingIndicator`.
|
|
48
|
+
- Phase 3.3 safe mailbox event bus publication (`crew.mailbox.message`, `crew.mailbox.acknowledged`).
|
|
49
|
+
|
|
50
|
+
Deferred by design:
|
|
51
|
+
|
|
52
|
+
- Phase 1.1b foreground `terminate: true` is implemented as opt-in via `config.tools.terminateOnForeground=true`; default remains safe/off pending telemetry.
|
|
53
|
+
- Phase 3.4 structured artifact index is implemented for pi-crew-triggered compactions via `crew:artifact-index` session entries plus compaction custom instructions. Direct `CompactionEntry.details` augmentation is not available through the current upstream extension API without replacing default compaction.
|
|
54
|
+
- Phase 3.1, 3.3b, 3.5, and 4.2 are now marked won't-do/research-only after deeper risk/ROI analysis.
|
|
55
|
+
- Phase 3.2 remains conditional on agent-level opt-in design. Phase 4.1 remains deferred pending format-compat research.
|
|
56
|
+
|
|
57
|
+
Validation:
|
|
58
|
+
|
|
59
|
+
- `npm run typecheck` passes.
|
|
60
|
+
- `npm test` passes: 283 unit tests + 26 integration tests.
|
|
61
|
+
|
|
62
|
+
## Roadmap-level Rollback Strategy
|
|
63
|
+
|
|
64
|
+
- **1 sub-task = 1 commit** có thể revert độc lập. KHÔNG gộp toàn bộ Phase 1 vào 1 commit.
|
|
65
|
+
- Mỗi commit phải có test riêng. Nếu fail trong production, `git revert <sha>` không kéo theo task khác.
|
|
66
|
+
- Phase 1.6 (telemetry) làm trước Phase 1.1 để có baseline đo lường.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Phase 1: Quick Wins & Compliance (HIGH impact, LOW effort)
|
|
71
|
+
|
|
72
|
+
Thời gian ước tính: 2-3 sessions. **Thứ tự đã re-order so với research gốc.**
|
|
73
|
+
|
|
74
|
+
### 1.4 (FIRST) Permission gate cho destructive team actions
|
|
75
|
+
|
|
76
|
+
**Lý do làm trước:** AGENTS.md quy định *"Management deletes must require confirm: true; referenced
|
|
77
|
+
resources blocked unless force: true"* — đây là **rule bắt buộc**, không phải optimization.
|
|
78
|
+
|
|
79
|
+
**Files cần sửa:** `src/extension/registration/team-tool.ts` (hoặc file mới)
|
|
80
|
+
|
|
81
|
+
**Hiện tại:** Có check trong handler nhưng không có `tool_call` hook → message lỗi không nhất quán.
|
|
82
|
+
|
|
83
|
+
**Tối ưu:**
|
|
84
|
+
```typescript
|
|
85
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
86
|
+
if (event.toolName !== "team") return;
|
|
87
|
+
const input = event.input as Record<string, unknown>;
|
|
88
|
+
const destructiveActions = ["delete", "forget", "prune", "cleanup"];
|
|
89
|
+
|
|
90
|
+
if (destructiveActions.includes(input.action as string)) {
|
|
91
|
+
if (!input.confirm && !input.force) {
|
|
92
|
+
return {
|
|
93
|
+
block: true,
|
|
94
|
+
reason: `Destructive action '${input.action}' requires confirm=true (or force=true to bypass)`,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Note về precedence:** Nếu schema validate đã check `confirm`, **CHỌN 1 chỗ duy nhất**:
|
|
102
|
+
- Option A: Để schema validate → bỏ hook (đơn giản hơn).
|
|
103
|
+
- Option B: Để hook validate → gỡ check trong handler (consistent error message).
|
|
104
|
+
|
|
105
|
+
→ Đề nghị Option B vì hook gate tất cả entry points (kể cả nếu sau này có entry point bypass schema).
|
|
106
|
+
|
|
107
|
+
**Expected benefit:** Compliance với AGENTS.md, safety net production.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### 1.6 (NEW) Telemetry baseline cho terminate impact
|
|
112
|
+
|
|
113
|
+
**Lý do làm trước 1.1:** Plan gốc claim "giảm 30-50% LLM turns" — chỉ là phỏng đoán. Cần baseline đo lường thực tế.
|
|
114
|
+
|
|
115
|
+
**Files cần sửa:** `src/runtime/subagent-manager.ts`, `src/extension/register.ts`
|
|
116
|
+
|
|
117
|
+
**Tối ưu:** Log `turnCount` + `terminated: boolean` vào event `crew.subagent.completed`:
|
|
118
|
+
```typescript
|
|
119
|
+
pi.events.emit("crew.subagent.completed", {
|
|
120
|
+
id: record.id,
|
|
121
|
+
runId: record.runId,
|
|
122
|
+
type: record.type,
|
|
123
|
+
status: record.status,
|
|
124
|
+
usage: record.usage,
|
|
125
|
+
turnCount: record.turnCount, // ← NEW
|
|
126
|
+
terminated: record.terminated, // ← NEW (false trước Phase 1.1)
|
|
127
|
+
durationMs: record.durationMs, // ← NEW
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Expected benefit:** Đo trước/sau Phase 1.1 để xác định ROI thực tế. Nếu < 10% turn saving, có thể quyết định không deploy 1.1b.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
### 1.2 `session_before_compact` guard cho foreground runs (DEFER, không CANCEL)
|
|
136
|
+
|
|
137
|
+
**Files cần sửa:** `src/extension/register.ts`
|
|
138
|
+
|
|
139
|
+
**Hiện tại:** Không hook compaction → có thể compact giữa chừng foreground run.
|
|
140
|
+
|
|
141
|
+
**Tối ưu (revised):** Defer + retry thay vì cancel cứng (tránh context overflow):
|
|
142
|
+
```typescript
|
|
143
|
+
let pendingCompactReason: string | null = null;
|
|
144
|
+
|
|
145
|
+
pi.on("session_before_compact", async (event, ctx) => {
|
|
146
|
+
if (foregroundControllers.size > 0) {
|
|
147
|
+
pendingCompactReason = "deferred-during-foreground-run";
|
|
148
|
+
ctx.ui.notify("Compaction deferred until foreground run completes", "info");
|
|
149
|
+
return { cancel: true };
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Retry sau khi run xong:
|
|
154
|
+
pi.on("turn_end", (_event, ctx) => {
|
|
155
|
+
if (foregroundControllers.size === 0 && pendingCompactReason) {
|
|
156
|
+
pendingCompactReason = null;
|
|
157
|
+
ctx.compact({
|
|
158
|
+
onComplete: () => ctx.ui.notify("Deferred compaction completed", "info"),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Expected benefit:** Ngăn lỗi context mất mát trong foreground run, vẫn đảm bảo compact eventually chạy.
|
|
165
|
+
|
|
166
|
+
**Risk:** Nếu run cực dài + foregroundControllers chưa bao giờ về 0 → vẫn overflow. Mitigation: hard threshold (vd 95% context window) bypass deferral, force compact.
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
### 1.1a `terminate: true` cho **background queued** results (SAFE)
|
|
171
|
+
|
|
172
|
+
**Lý do tách:** Background queue không có UX risk, foreground completed có risk (xem 1.1b).
|
|
173
|
+
|
|
174
|
+
**Files cần sửa:** `src/extension/registration/subagent-tools.ts`
|
|
175
|
+
|
|
176
|
+
**Tối ưu:**
|
|
177
|
+
```typescript
|
|
178
|
+
// Agent tool — khi background: terminate ngay sau khi đã queued
|
|
179
|
+
if (params.run_in_background) {
|
|
180
|
+
return {
|
|
181
|
+
...subagentToolResult(...),
|
|
182
|
+
terminate: true, // ← Tiết kiệm 1 LLM turn, không có rủi ro UX
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**Expected benefit:** Giảm LLM turn cho mọi background spawn. Verify bằng telemetry từ 1.6.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
### 1.3 Public events qua `pi.events`
|
|
192
|
+
|
|
193
|
+
**Files cần sửa:** `src/extension/register.ts`
|
|
194
|
+
|
|
195
|
+
**Hiện tại:** Event bus chỉ dùng cho internal `subagent.stuck-blocked`.
|
|
196
|
+
|
|
197
|
+
**Naming convention (revised):** Thống nhất với upstream pattern `dot.kebab` (đã dùng cho `subagent.stuck-blocked`):
|
|
198
|
+
```typescript
|
|
199
|
+
// Document trong README là PUBLIC API:
|
|
200
|
+
pi.events.emit("crew.subagent.completed", { ... });
|
|
201
|
+
pi.events.emit("crew.run.completed", { runId, team, workflow, status, taskCount, totalUsage });
|
|
202
|
+
pi.events.emit("crew.run.failed", { runId, team, workflow, error, failedTaskId });
|
|
203
|
+
pi.events.emit("crew.run.cancelled", { runId, team, workflow, status, taskCount });
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Versioning:** Note trong README rằng event payload là semver-stable từ pi-crew 0.2.0.
|
|
207
|
+
|
|
208
|
+
**Expected benefit:** Extension khác (logging, notification, metrics) có thể subscribe.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
### 1.5 Auto session name từ team run context
|
|
213
|
+
|
|
214
|
+
**Files cần sửa:** `src/extension/registration/team-tool.ts`
|
|
215
|
+
|
|
216
|
+
**Tối ưu:**
|
|
217
|
+
```typescript
|
|
218
|
+
// Trong team tool execute, trước khi start run:
|
|
219
|
+
pi.setSessionName(`pi-crew: ${team}/${workflow} — ${goal.slice(0, 60)}`);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Expected benefit:** Better session organization khi xem session list.
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
### 1.1b (OPT-IN DONE, DEFAULT OFF) `terminate: true` cho **foreground completed** results
|
|
227
|
+
|
|
228
|
+
**Lý do default off:** UX risk — nếu LLM không có turn để summarize result, user có thể không hiểu output.
|
|
229
|
+
|
|
230
|
+
**Implementation:** opt-in flag, default safe:
|
|
231
|
+
|
|
232
|
+
```json
|
|
233
|
+
{
|
|
234
|
+
"tools": {
|
|
235
|
+
"terminateOnForeground": true
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
When enabled, foreground `Agent`/`crew_agent` completed results set `terminate: true` and persist `record.terminated=true` for telemetry. Decision to make this default-on still requires telemetry evidence:
|
|
241
|
+
|
|
242
|
+
- Average turn count sau Agent foreground completion ≥ 2.
|
|
243
|
+
- Output đã đủ self-explanatory (đo qua user feedback hoặc retry rate).
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Phase 2: Medium Effort Optimizations
|
|
248
|
+
|
|
249
|
+
Thời gian ước tính: 2-3 sessions. (Đã giảm 1 task so với plan gốc.)
|
|
250
|
+
|
|
251
|
+
### 2.1 Proactive compaction monitoring (DYNAMIC threshold)
|
|
252
|
+
|
|
253
|
+
**Files cần sửa:** File mới `src/extension/registration/compaction-guard.ts`
|
|
254
|
+
|
|
255
|
+
**Hiện tại:** Chỉ dựa vào built-in auto-compaction (có thể chậm).
|
|
256
|
+
|
|
257
|
+
**Tối ưu (revised):** Threshold động theo `contextWindow`:
|
|
258
|
+
```typescript
|
|
259
|
+
export function registerCompactionGuard(pi: ExtensionAPI) {
|
|
260
|
+
const TRIGGER_RATIO = 0.75; // 75% context window → trigger
|
|
261
|
+
|
|
262
|
+
pi.on("turn_end", (_event, ctx) => {
|
|
263
|
+
const usage = ctx.getContextUsage();
|
|
264
|
+
const ctxWindow = ctx.model?.contextWindow ?? 200_000;
|
|
265
|
+
const threshold = ctxWindow * TRIGGER_RATIO;
|
|
266
|
+
|
|
267
|
+
if (usage?.tokens && usage.tokens > threshold) {
|
|
268
|
+
// Foreground guard từ Phase 1.2 sẽ defer nếu cần
|
|
269
|
+
ctx.compact({
|
|
270
|
+
customInstructions: "Prioritize keeping team run state, task results, and artifact references. Keep the conversation context brief.",
|
|
271
|
+
onComplete: () => ctx.ui.notify("Auto-compacted context during team run", "info"),
|
|
272
|
+
onError: (err) => ctx.ui.notify(`Compaction failed: ${err.message}`, "error"),
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
**Lý do dùng ratio thay vì hardcode:** Claude Haiku 200k, Gemini Pro 2M, GPT-4o 128k, model nhỏ 32k. Hardcode 150k sai cho 90% trường hợp.
|
|
280
|
+
|
|
281
|
+
**Expected benefit:** Tránh context overflow error khi foreground run quá dài.
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
### 2.3 `pi.appendEntry` cho cross-session run awareness
|
|
286
|
+
|
|
287
|
+
**Files cần sửa:** `src/extension/register.ts`
|
|
288
|
+
|
|
289
|
+
**Tối ưu:**
|
|
290
|
+
```typescript
|
|
291
|
+
// Khi bắt đầu run:
|
|
292
|
+
pi.appendEntry("crew:run-started", {
|
|
293
|
+
runId, team, workflow, goal, timestamp: Date.now(),
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// Khi hoàn thành run:
|
|
297
|
+
pi.appendEntry("crew:run-completed", {
|
|
298
|
+
runId, status, taskCount, totalUsage, timestamp: Date.now(),
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
**Expected benefit:**
|
|
303
|
+
- Khi reload session, biết được các run liên quan.
|
|
304
|
+
- Session export bao gồm run context.
|
|
305
|
+
- Dễ dàng track history.
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
### 2.4 Config-driven tool registration
|
|
310
|
+
|
|
311
|
+
**Files cần sửa:** `src/extension/registration/subagent-tools.ts`
|
|
312
|
+
|
|
313
|
+
**Hiện tại:** Luôn register 6 tool variants (Agent, crew_agent, + result + steer).
|
|
314
|
+
|
|
315
|
+
**Tối ưu:**
|
|
316
|
+
```typescript
|
|
317
|
+
export function registerSubagentTools(pi: ExtensionAPI, subagentManager: SubagentManager) {
|
|
318
|
+
const cfg = loadConfig(pi.getFlag("cwd") as string || process.cwd());
|
|
319
|
+
|
|
320
|
+
// Conflict-safe tools (luôn register)
|
|
321
|
+
pi.registerTool(crewAgentTool);
|
|
322
|
+
pi.registerTool(crewAgentResultTool);
|
|
323
|
+
|
|
324
|
+
// Claude-style aliases: only if not disabled
|
|
325
|
+
if (cfg.config.tools?.enableClaudeStyleAliases !== false) {
|
|
326
|
+
try { pi.registerTool(agentTool); } catch {}
|
|
327
|
+
try { pi.registerTool(getSubagentResultTool); } catch {}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Steer: only if supported
|
|
331
|
+
if (cfg.config.tools?.enableSteer !== false) {
|
|
332
|
+
try { pi.registerTool(crewAgentSteerTool); } catch {}
|
|
333
|
+
try { pi.registerTool(steerSubagentTool); } catch {}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
**Expected benefit:** Tránh pollute tool namespace, fine-grained control cho user.
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
### 2.5 Custom working indicator trong foreground runs
|
|
343
|
+
|
|
344
|
+
**Files cần sửa:** `src/extension/register.ts`
|
|
345
|
+
|
|
346
|
+
**Tối ưu:**
|
|
347
|
+
```typescript
|
|
348
|
+
// Khi foreground run active:
|
|
349
|
+
ctx.ui.setWorkingIndicator({
|
|
350
|
+
frames: ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"],
|
|
351
|
+
intervalMs: 80,
|
|
352
|
+
});
|
|
353
|
+
ctx.ui.setWorkingMessage(
|
|
354
|
+
`Team run: ${completedTasks}/${totalTasks} tasks done...`
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
// Khi kết thúc:
|
|
358
|
+
ctx.ui.setWorkingIndicator(); // Restore default
|
|
359
|
+
ctx.ui.setWorkingMessage(); // Clear
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
**Compat shim note:** Implementation dùng optional API compatibility shim:
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
(ctx.ui as { setWorkingIndicator?: (...) => void }).setWorkingIndicator?.(...)
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
Lý do: một số version/type surface của `@mariozechner/pi-coding-agent` chưa expose
|
|
369
|
+
`setWorkingIndicator` trên `ExtensionUIContext`. Optional shim giữ backward compatibility và
|
|
370
|
+
tránh crash/runtime type mismatch; nếu API không tồn tại thì chỉ bỏ qua custom spinner và vẫn dùng
|
|
371
|
+
`setWorkingMessage()`.
|
|
372
|
+
|
|
373
|
+
**Expected benefit:** Better UX, cho user biết team run đang chạy.
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## Phase 3: Future Considerations (HIGH effort hoặc Risky)
|
|
378
|
+
|
|
379
|
+
### 3.1 (WON'T DO unless concrete pain point appears) Branch-level task isolation
|
|
380
|
+
|
|
381
|
+
Dùng `ctx.fork()` để tạo branch mới cho mỗi task trong team run.
|
|
382
|
+
|
|
383
|
+
**Decision:** không triển khai mặc định. Worktree isolation đã giải quyết phần quan trọng nhất (file-system/task isolation). Branch-level isolation tạo branch explosion, navigation UX phức tạp, và state-sync risk giữa flat run manifest/tasks/events với Pi session tree. Chỉ reconsider nếu có user complaint cụ thể về context contamination không giải quyết được bằng worktree/dependency-context controls.
|
|
384
|
+
|
|
385
|
+
### 3.2 Session handoff cho long-running tasks
|
|
386
|
+
|
|
387
|
+
Khi 1 task quá dài, handoff sang session mới (pattern từ `handoff.ts`), isolate context.
|
|
388
|
+
|
|
389
|
+
**Conditional trigger:** chỉ enable cho agent/task opt-in, ví dụ agent frontmatter `handoff: true`, hoặc heuristic token estimate > 30% context window.
|
|
390
|
+
|
|
391
|
+
**Result transport:** child session trả về artifact reference hoặc mailbox message để parent session vẫn aggregate được kết quả mà không cần import toàn bộ transcript.
|
|
392
|
+
|
|
393
|
+
### 3.3 Mailbox qua `pi.events`
|
|
394
|
+
|
|
395
|
+
#### 3.3a (DONE) Publish mailbox lifecycle events while preserving file-backed mailbox
|
|
396
|
+
|
|
397
|
+
Implementation publishes safe public events without changing the durable mailbox source of truth:
|
|
398
|
+
|
|
399
|
+
```typescript
|
|
400
|
+
pi.events.emit("crew.mailbox.message", { runId, id, direction, from, to, taskId, source });
|
|
401
|
+
pi.events.emit("crew.mailbox.acknowledged", { runId, messageId, delivery });
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
This keeps file-backed mailbox semantics intact while enabling observers/notification extensions.
|
|
405
|
+
|
|
406
|
+
#### 3.3b (WON'T DO) Replace file-backed mailbox with pure event-bus mailbox
|
|
407
|
+
|
|
408
|
+
Thay vì file-based mailbox, dùng event bus làm transport chính cho real-time communication giữa tasks.
|
|
409
|
+
|
|
410
|
+
**Decision:** won't do. Latency gain is marginal; durability/restart/replay loss is catastrophic for long-running pi-crew runs. 3.3a gives best-of-both-worlds: durable file-backed mailbox remains source of truth, event bus is an observer/notification layer.
|
|
411
|
+
|
|
412
|
+
### 3.4 (PROMOTED + DONE) Compaction với structured artifact index
|
|
413
|
+
|
|
414
|
+
Preserve pi-crew artifact references across compaction.
|
|
415
|
+
|
|
416
|
+
**Implementation:** `compaction-guard.ts` collects recent run artifacts and:
|
|
417
|
+
|
|
418
|
+
- appends a structured `crew:artifact-index` session entry for machine-readable continuity;
|
|
419
|
+
- adds a markdown artifact index to pi-crew-triggered compaction `customInstructions` so the compaction summary preserves run IDs and artifact paths.
|
|
420
|
+
|
|
421
|
+
**Note:** Directly augmenting `CompactionEntry.details` is not supported by the current upstream `session_before_compact` result contract unless pi-crew replaces default compaction entirely. We intentionally avoid full custom compaction because summary quality/regression risk is higher.
|
|
422
|
+
|
|
423
|
+
### 3.5 (WON'T DO unless cost telemetry shows pain) Custom compaction với model nhẹ
|
|
424
|
+
|
|
425
|
+
**Decision:** won't do by default.
|
|
426
|
+
|
|
427
|
+
- Phụ thuộc vào auth setup của user cho Gemini Flash / Haiku — pi-crew không kiểm soát được.
|
|
428
|
+
- Bad summary làm mất context → ảnh hưởng cả run.
|
|
429
|
+
- ROI không rõ: compaction chạy không thường xuyên.
|
|
430
|
+
|
|
431
|
+
Reconsider only if telemetry/user feedback shows compaction cost is a real pain point. Reference remains `examples/extensions/custom-compaction.ts` upstream.
|
|
432
|
+
|
|
433
|
+
---
|
|
434
|
+
|
|
435
|
+
## Phase 4 (NEW): Research bổ sung
|
|
436
|
+
|
|
437
|
+
Hai pattern upstream chưa được khai thác trong plan gốc:
|
|
438
|
+
|
|
439
|
+
### 4.1 (DEFER — research format compat first) `resources_discover` event integration
|
|
440
|
+
|
|
441
|
+
Pi-crew có thể inject builtin agents/teams như Pi resources native (skills/prompts):
|
|
442
|
+
```typescript
|
|
443
|
+
pi.on("resources_discover", () => ({
|
|
444
|
+
skillPaths: [path.join(__dirname, "..", "agents")],
|
|
445
|
+
promptPaths: [path.join(__dirname, "..", "workflows")],
|
|
446
|
+
}));
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
**Decision:** defer. Cần research format compat giữa pi-crew agent markdown vs Pi skill/prompt format trước khi implement. Key risk: dual exposure UX confusion (same capability reachable via `Agent` tool and native skill/prompt) plus loss of pi-crew durable run semantics if exposed as stateless skills.
|
|
450
|
+
|
|
451
|
+
### 4.2 (RESEARCH-ONLY) `pi.registerProvider` cho virtual "team" model
|
|
452
|
+
|
|
453
|
+
Đăng ký team như virtual provider để user gọi:
|
|
454
|
+
```bash
|
|
455
|
+
pi --model crew/researcher
|
|
456
|
+
```
|
|
457
|
+
Thay vì dùng tool `Agent`.
|
|
458
|
+
|
|
459
|
+
**Decision:** research-only / not an implementation target. Provider API semantics (single LLM stream, context window, thinking levels, token pricing) do not map cleanly to orchestrator semantics (multi-agent task events, aggregate usage/cost, per-worker contexts). Likely requires upstream provider API changes.
|
|
460
|
+
|
|
461
|
+
---
|
|
462
|
+
|
|
463
|
+
## Implementation Order (REVISED)
|
|
464
|
+
|
|
465
|
+
```
|
|
466
|
+
Phase 1 (Quick Wins & Compliance):
|
|
467
|
+
[x] 1.4 permission gate destructive team actions ← FIRST (compliance)
|
|
468
|
+
[x] 1.6 telemetry baseline ← SECOND (measure first)
|
|
469
|
+
[x] 1.2 session_before_compact defer (not cancel)
|
|
470
|
+
[x] 1.1a terminate: true on background queued (safe)
|
|
471
|
+
[x] 1.3 public crew.* events
|
|
472
|
+
[x] 1.5 auto session name
|
|
473
|
+
[x] 1.1b terminate: true on foreground (OPT-IN, default off; default-on conditional on telemetry)
|
|
474
|
+
|
|
475
|
+
Phase 2 (Medium):
|
|
476
|
+
[x] 2.1 proactive compaction (dynamic threshold)
|
|
477
|
+
[x] 2.3 pi.appendEntry cross-session awareness
|
|
478
|
+
[x] 2.4 config-driven tool registration
|
|
479
|
+
[x] 2.5 custom working indicator
|
|
480
|
+
|
|
481
|
+
Phase 3 (Future / Risky):
|
|
482
|
+
[-] 3.1 branch-level task isolation (WON'T DO unless concrete pain point appears)
|
|
483
|
+
[ ] 3.2 session handoff for long tasks (CONDITIONAL on agent opt-in)
|
|
484
|
+
[x] 3.3a publish mailbox lifecycle events (safe subset)
|
|
485
|
+
[-] 3.3b replace file-backed mailbox with pure event bus (WON'T DO)
|
|
486
|
+
[x] 3.4 structured artifact index in compaction (promoted/done)
|
|
487
|
+
[-] 3.5 custom compaction with cheap model (WON'T DO unless cost telemetry shows pain)
|
|
488
|
+
|
|
489
|
+
Phase 4 (Research):
|
|
490
|
+
[ ] 4.1 resources_discover integration (DEFER; format compat research first)
|
|
491
|
+
[-] 4.2 virtual team provider (RESEARCH-ONLY)
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
## Files affected
|
|
495
|
+
|
|
496
|
+
```
|
|
497
|
+
PHASE 1:
|
|
498
|
+
src/extension/registration/team-tool.ts ← 1.4 permission gate
|
|
499
|
+
src/extension/registration/subagent-tools.ts ← 1.1a terminate + 1.1b opt-in terminate
|
|
500
|
+
src/extension/register.ts ← 1.2 defer guard, 1.3 events, 1.5 session name
|
|
501
|
+
src/runtime/subagent-manager.ts ← 1.6 telemetry fields
|
|
502
|
+
|
|
503
|
+
PHASE 2:
|
|
504
|
+
src/extension/registration/compaction-guard.ts ← NEW: 1.2 defer guard + 2.1 proactive + 3.4 artifact index
|
|
505
|
+
src/extension/register.ts ← 2.3 appendEntry, 2.5 working indicator
|
|
506
|
+
src/extension/registration/subagent-tools.ts ← 2.4 config-driven
|
|
507
|
+
|
|
508
|
+
PHASE 3:
|
|
509
|
+
src/extension/team-tool/api.ts ← 3.3a mailbox lifecycle events
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
## Risk Assessment (REVISED)
|
|
513
|
+
|
|
514
|
+
| Change | Risk | Mitigation |
|
|
515
|
+
|---|---|---|
|
|
516
|
+
| Permission gate (1.4) | Block legitimate use | Allow `force=true` bypass, document trong README |
|
|
517
|
+
| Telemetry (1.6) | Privacy / log size | No PII in subagent telemetry payload; opt-out applied via `config.telemetry.enabled=false`; no sampling currently because payload is small/local event-bus data |
|
|
518
|
+
| Defer compaction (1.2) | Run dài infinite → overflow | Hard threshold 95% bypass deferral |
|
|
519
|
+
| `terminate: true` background (1.1a) | None significant | Background không cần LLM follow-up by design |
|
|
520
|
+
| Public events (1.3) | Event storm, breaking change | Rate limit, semver document |
|
|
521
|
+
| Auto session name (1.5) | Override user-set name | Applied: chỉ set nếu chưa có name custom (`!pi.getSessionName()`) |
|
|
522
|
+
| `terminate: true` foreground (1.1b) | LLM không summarize khi enabled | OPT-IN flag (`config.tools.terminateOnForeground`, default off); default-on requires telemetry evidence |
|
|
523
|
+
| Dynamic threshold (2.1) | contextWindow undefined | Default 200_000 fallback |
|
|
524
|
+
| Artifact index in compaction (3.4) | Index size bloat / format drift | Cap recent index (10 runs / 80 artifacts), structured `crew:artifact-index` session entry, non-replacing default compaction |
|
|
525
|
+
| appendEntry (2.3) | Session bloat | TTL/cleanup strategy |
|
|
526
|
+
| Config-driven tools (2.4) | User confused | Default = current behavior, opt-in change |
|
|
527
|
+
| Working indicator (2.5) | Conflict với extension khác / older Pi UI type surface | Applied: restore default on finally; compat shim makes `setWorkingIndicator` optional |
|
|
528
|
+
| Custom compaction model (3.5) | Bad summary, auth missing | Fall back to default, multi-model retry |
|
|
529
|
+
|
|
530
|
+
## Testing Strategy
|
|
531
|
+
|
|
532
|
+
- **Unit tests:**
|
|
533
|
+
- `terminate: true` flag in tool results (1.1a/b).
|
|
534
|
+
- Permission gate blocks/allows correctly với confirm/force matrix (1.4).
|
|
535
|
+
- Threshold calculation từ contextWindow (2.1).
|
|
536
|
+
- Telemetry payload schema (1.6).
|
|
537
|
+
- Artifact index payload structure + cap behavior (3.4).
|
|
538
|
+
- **Integration tests:**
|
|
539
|
+
- Foreground run + compaction interaction (1.2 defer + 2.1 trigger).
|
|
540
|
+
- Multiple concurrent runs + permission gate (1.4).
|
|
541
|
+
- Event publish/subscribe round-trip (1.3).
|
|
542
|
+
- Compaction with N artifacts includes artifact index in custom instructions (3.4).
|
|
543
|
+
- **Manual:**
|
|
544
|
+
- UI behavior với working indicator + session name (1.5, 2.5).
|
|
545
|
+
- Real LLM turn count trước/sau 1.1b với telemetry data (1.6 → 1.1b decision).
|
|
546
|
+
- **Regression:**
|
|
547
|
+
- Run full suite (`npm test`) sau mỗi commit, không gộp Phase.
|
|
548
|
+
- Doctor tests phải dùng `--test-timeout=90000` trên Windows.
|