@linxiraos/pi-messenger 1.1.0
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 +676 -0
- package/README.md +424 -0
- package/config-overlay.ts +171 -0
- package/config.ts +209 -0
- package/crew/agents/crew-plan-sync.md +64 -0
- package/crew/agents/crew-planner.md +172 -0
- package/crew/agents/crew-reviewer.md +58 -0
- package/crew/agents/crew-worker.md +129 -0
- package/crew/agents.ts +434 -0
- package/crew/handlers/coordination.ts +332 -0
- package/crew/handlers/plan.ts +906 -0
- package/crew/handlers/review.ts +333 -0
- package/crew/handlers/revise.ts +376 -0
- package/crew/handlers/status.ts +295 -0
- package/crew/handlers/sync.ts +233 -0
- package/crew/handlers/task.ts +792 -0
- package/crew/handlers/team.ts +235 -0
- package/crew/handlers/work.ts +425 -0
- package/crew/id-allocator.ts +30 -0
- package/crew/index.ts +257 -0
- package/crew/live-progress.ts +58 -0
- package/crew/lobby.ts +473 -0
- package/crew/prompt.ts +252 -0
- package/crew/registry.ts +114 -0
- package/crew/spawn.ts +111 -0
- package/crew/state-autonomous.ts +164 -0
- package/crew/state-planning.ts +263 -0
- package/crew/state.ts +20 -0
- package/crew/store.ts +642 -0
- package/crew/task-actions.ts +131 -0
- package/crew/team/store.ts +479 -0
- package/crew/team/subagent-roles.ts +87 -0
- package/crew/team/types.ts +50 -0
- package/crew/types.ts +194 -0
- package/crew/utils/artifacts.ts +62 -0
- package/crew/utils/config.ts +156 -0
- package/crew/utils/discover.ts +219 -0
- package/crew/utils/install.ts +85 -0
- package/crew/utils/progress.ts +187 -0
- package/crew/utils/result.ts +16 -0
- package/crew/utils/risk-labels.ts +7 -0
- package/crew/utils/task-format.ts +14 -0
- package/crew/utils/team-roles.ts +28 -0
- package/crew/utils/truncate.ts +79 -0
- package/crew/utils/verdict.ts +55 -0
- package/feed.ts +268 -0
- package/handlers.ts +1107 -0
- package/index.ts +1206 -0
- package/install.mjs +189 -0
- package/lib.ts +467 -0
- package/overlay-actions.ts +511 -0
- package/overlay-coordinator.ts +150 -0
- package/overlay-render.ts +676 -0
- package/overlay.ts +830 -0
- package/package.json +62 -0
- package/skills/pi-messenger-crew/SKILL.md +347 -0
- package/store.ts +1139 -0
- package/vitest.config.ts +10 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { truncateToWidth } from "@earendil-works/pi-tui";
|
|
4
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import {
|
|
6
|
+
formatDuration,
|
|
7
|
+
formatRelativeTime,
|
|
8
|
+
buildSelfRegistration,
|
|
9
|
+
coloredAgentName,
|
|
10
|
+
computeStatus,
|
|
11
|
+
STATUS_INDICATORS,
|
|
12
|
+
agentHasTask,
|
|
13
|
+
type Dirs,
|
|
14
|
+
type MessengerState,
|
|
15
|
+
} from "./lib.ts";
|
|
16
|
+
import * as store from "./store.ts";
|
|
17
|
+
import * as crewStore from "./crew/store.ts";
|
|
18
|
+
import * as teamStore from "./crew/team/store.ts";
|
|
19
|
+
import {
|
|
20
|
+
autonomousState,
|
|
21
|
+
getPlanningUpdateAgeMs,
|
|
22
|
+
isAutonomousForCwd,
|
|
23
|
+
isPlanningForCwd,
|
|
24
|
+
isPlanningStalled,
|
|
25
|
+
planningState,
|
|
26
|
+
PLANNING_STALE_TIMEOUT_MS,
|
|
27
|
+
} from "./crew/state.ts";
|
|
28
|
+
import type { Task } from "./crew/types.ts";
|
|
29
|
+
import { getLiveWorkers, type LiveWorkerInfo } from "./crew/live-progress.ts";
|
|
30
|
+
import type { ToolEntry } from "./crew/utils/progress.ts";
|
|
31
|
+
import { formatFeedLine as sharedFormatFeedLine, sanitizeFeedEvent, type FeedEvent } from "./feed.ts";
|
|
32
|
+
import { discoverCrewAgents } from "./crew/utils/discover.ts";
|
|
33
|
+
import { loadConfig } from "./config.ts";
|
|
34
|
+
import { loadCrewConfig } from "./crew/utils/config.ts";
|
|
35
|
+
import { getLobbyWorkerCount } from "./crew/lobby.ts";
|
|
36
|
+
import type { CrewViewState } from "./overlay-actions.ts";
|
|
37
|
+
|
|
38
|
+
const STATUS_ICONS: Record<string, string> = { done: "✓", in_progress: "●", todo: "○", blocked: "✗" };
|
|
39
|
+
|
|
40
|
+
function formatElapsed(ms: number): string {
|
|
41
|
+
const s = Math.floor(Math.max(0, ms) / 1000);
|
|
42
|
+
const m = Math.floor(s / 60);
|
|
43
|
+
return m > 0 ? `${m}:${String(s % 60).padStart(2, "0")}` : `0:${String(s).padStart(2, "0")}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function renderActivityLog(
|
|
47
|
+
tools: ToolEntry[],
|
|
48
|
+
currentTool: string | undefined,
|
|
49
|
+
currentToolArgs: string | undefined,
|
|
50
|
+
startedAt: number,
|
|
51
|
+
width: number,
|
|
52
|
+
): string[] {
|
|
53
|
+
const lines: string[] = [];
|
|
54
|
+
for (const entry of tools) {
|
|
55
|
+
const elapsed = formatElapsed(entry.startMs - startedAt);
|
|
56
|
+
const args = entry.args ? ` ${entry.args}` : "";
|
|
57
|
+
lines.push(truncateToWidth(` [${elapsed}] ${entry.tool}${args}`, width));
|
|
58
|
+
}
|
|
59
|
+
if (currentTool) {
|
|
60
|
+
const elapsed = formatElapsed(Date.now() - startedAt);
|
|
61
|
+
const args = currentToolArgs ? ` ${currentToolArgs}` : "";
|
|
62
|
+
lines.push(truncateToWidth(` → [${elapsed}] ${currentTool}${args}`, width));
|
|
63
|
+
} else {
|
|
64
|
+
lines.push(` → thinking...`);
|
|
65
|
+
}
|
|
66
|
+
return lines;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hasLiveWorker(cwd: string, taskId: string): boolean {
|
|
70
|
+
return getLiveWorkers(cwd).has(taskId);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readPlanningTail(cwd: string, maxLines: number): string[] {
|
|
74
|
+
const progressPath = path.join(crewStore.getCrewDir(cwd), "planning-progress.md");
|
|
75
|
+
if (!fs.existsSync(progressPath)) return [];
|
|
76
|
+
try {
|
|
77
|
+
const lines = fs.readFileSync(progressPath, "utf-8").split("\n").map(l => l.trim()).filter(Boolean);
|
|
78
|
+
if (lines.length === 0) return [];
|
|
79
|
+
return lines.slice(-maxLines);
|
|
80
|
+
} catch {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function appendUniversalHints(text: string): string {
|
|
86
|
+
return `${text} [^T] [^B]`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function idleLabel(timestamp: string | undefined): string {
|
|
90
|
+
if (!timestamp) return "idle";
|
|
91
|
+
const ageMs = Math.max(0, Date.now() - new Date(timestamp).getTime());
|
|
92
|
+
if (!Number.isFinite(ageMs) || ageMs < 30_000) return "active";
|
|
93
|
+
return `idle ${formatDuration(ageMs)}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function renderStatusBar(theme: Theme, cwd: string, width: number, tasks?: Task[]): string {
|
|
97
|
+
const plan = crewStore.getPlan(cwd);
|
|
98
|
+
const autonomousActive = isAutonomousForCwd(cwd);
|
|
99
|
+
const crewDir = crewStore.getCrewDir(cwd);
|
|
100
|
+
const crewConfig = loadCrewConfig(crewDir);
|
|
101
|
+
|
|
102
|
+
if (isPlanningForCwd(cwd)) {
|
|
103
|
+
const updated = planningState.updatedAt ? formatRelativeTime(planningState.updatedAt) : "unknown";
|
|
104
|
+
const stalled = isPlanningStalled(cwd);
|
|
105
|
+
const label = stalled ? "Planning stalled" : "Planning";
|
|
106
|
+
const lobbyCount = getLobbyWorkerCount(cwd);
|
|
107
|
+
const workerNote = lobbyCount > 0 ? ` │ ${lobbyCount} in lobby` : "";
|
|
108
|
+
const coordLevel = crewConfig.coordination;
|
|
109
|
+
return truncateToWidth(`${label} ${planningState.pass}/${planningState.maxPasses} │ ${planningState.phase} │ ${updated}${workerNote} │ ${crewConfig.dependencies} │ ${coordLevel}`, width);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!plan) {
|
|
113
|
+
const liveCount = getLiveWorkers(cwd).size;
|
|
114
|
+
const team = teamStore.getActiveTeam(cwd);
|
|
115
|
+
const profileText = team?.profile && team.profile !== team.name ? `/${team.profile}` : "";
|
|
116
|
+
const teamText = team ? ` │ Team: ${team.name}${profileText}` : "";
|
|
117
|
+
return truncateToWidth(`No active plan │ ⚙ ${liveCount}/${autonomousState.concurrency} workers${teamText}`, width);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const taskSnapshot = tasks ?? crewStore.getTasks(cwd);
|
|
121
|
+
const ready = crewStore.getReadyTasksFrom(taskSnapshot, { advisory: crewConfig.dependencies === "advisory" });
|
|
122
|
+
const team = teamStore.getActiveTeam(cwd);
|
|
123
|
+
const needsLead = taskSnapshot.filter(teamStore.taskPendingApproval).length;
|
|
124
|
+
const rejected = taskSnapshot.filter(teamStore.taskNeedsRevision).length;
|
|
125
|
+
const progress = `${plan.completed_count}/${plan.task_count}`;
|
|
126
|
+
const planLabel = crewStore.getPlanLabel(plan, 40);
|
|
127
|
+
let base = `📋 ${planLabel}: ${progress}`;
|
|
128
|
+
if (ready.length > 0) {
|
|
129
|
+
const readyLabel = crewConfig.dependencies === "advisory" ? "available" : "ready";
|
|
130
|
+
base += ` │ ${ready.length} ${readyLabel}`;
|
|
131
|
+
}
|
|
132
|
+
const liveCount = getLiveWorkers(cwd).size;
|
|
133
|
+
base += ` │ ⚙ ${liveCount}/${autonomousState.concurrency} workers`;
|
|
134
|
+
const coordLevel = crewConfig.coordination;
|
|
135
|
+
base += ` │ ${crewConfig.dependencies} │ ${coordLevel}`;
|
|
136
|
+
if (team) {
|
|
137
|
+
const profileText = team.profile && team.profile !== team.name ? `/${team.profile}` : "";
|
|
138
|
+
base += ` │ Team: ${team.name}${profileText}${needsLead > 0 ? ` │ Needs lead: ${needsLead}` : ""}${rejected > 0 ? ` │ Rejected: ${rejected}` : ""}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!autonomousActive) {
|
|
142
|
+
return truncateToWidth(base, width);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const parts = ["● AUTO", `W${autonomousState.waveNumber}`];
|
|
146
|
+
if (autonomousState.startedAt) {
|
|
147
|
+
const elapsedMs = Date.now() - new Date(autonomousState.startedAt).getTime();
|
|
148
|
+
const mm = Math.floor(elapsedMs / 60000).toString();
|
|
149
|
+
const ss = Math.floor((elapsedMs % 60000) / 1000).toString().padStart(2, "0");
|
|
150
|
+
parts.push(`⏱ ${mm}:${ss}`);
|
|
151
|
+
}
|
|
152
|
+
return truncateToWidth(`${base} │ ${theme.fg("accent", parts.join(" "))}`, width);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function renderWorkersSection(theme: Theme, cwd: string, width: number, maxLines: number): string[] {
|
|
156
|
+
if (maxLines <= 0) return [];
|
|
157
|
+
|
|
158
|
+
const workers = Array.from(getLiveWorkers(cwd).values()).slice(0, maxLines);
|
|
159
|
+
if (workers.length === 0) return [];
|
|
160
|
+
|
|
161
|
+
const lines: string[] = [];
|
|
162
|
+
for (const info of workers) {
|
|
163
|
+
const activity = info.progress.currentTool
|
|
164
|
+
? `${info.progress.currentTool}${info.progress.currentToolArgs ? `(${info.progress.currentToolArgs})` : ""}`
|
|
165
|
+
: "thinking";
|
|
166
|
+
const elapsed = formatDuration(Date.now() - info.startedAt);
|
|
167
|
+
const tokens = info.progress.tokens > 1000
|
|
168
|
+
? `${(info.progress.tokens / 1000).toFixed(0)}k`
|
|
169
|
+
: `${info.progress.tokens}`;
|
|
170
|
+
const line = `⚡ ${info.name} ${formatTaskLabel(info.taskId)} ${activity} ${theme.fg("dim", `${elapsed} ${tokens} tok`)}`;
|
|
171
|
+
lines.push(truncateToWidth(line, width));
|
|
172
|
+
}
|
|
173
|
+
return lines;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function renderTaskList(theme: Theme, cwd: string, width: number, height: number, viewState: CrewViewState, tasks?: Task[]): string[] {
|
|
177
|
+
const taskSnapshot = tasks ?? crewStore.getTasks(cwd);
|
|
178
|
+
const lines: string[] = [];
|
|
179
|
+
|
|
180
|
+
if (taskSnapshot.length === 0) {
|
|
181
|
+
lines.push(theme.fg("dim", "(no tasks yet)"));
|
|
182
|
+
while (lines.length < height) lines.push("");
|
|
183
|
+
return lines.slice(0, height);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
viewState.selectedTaskIndex = Math.max(0, Math.min(viewState.selectedTaskIndex, taskSnapshot.length - 1));
|
|
187
|
+
const liveWorkers = getLiveWorkers(cwd);
|
|
188
|
+
|
|
189
|
+
for (let i = 0; i < taskSnapshot.length; i++) {
|
|
190
|
+
lines.push(renderTaskLine(theme, taskSnapshot[i], i === viewState.selectedTaskIndex, width, liveWorkers.get(taskSnapshot[i].id)));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (lines.length <= height) {
|
|
194
|
+
viewState.scrollOffset = 0;
|
|
195
|
+
return lines;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const selectedLine = Math.min(viewState.selectedTaskIndex, lines.length - 1);
|
|
199
|
+
if (selectedLine < viewState.scrollOffset) {
|
|
200
|
+
viewState.scrollOffset = selectedLine;
|
|
201
|
+
} else if (selectedLine >= viewState.scrollOffset + height) {
|
|
202
|
+
viewState.scrollOffset = selectedLine - height + 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
viewState.scrollOffset = Math.max(0, Math.min(viewState.scrollOffset, lines.length - height));
|
|
206
|
+
return lines.slice(viewState.scrollOffset, viewState.scrollOffset + height);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function renderTaskSummary(theme: Theme, cwd: string, width: number, height: number, tasks?: Task[]): string[] {
|
|
210
|
+
const taskSnapshot = tasks ?? crewStore.getTasks(cwd);
|
|
211
|
+
const counts: Record<string, number> = { done: 0, in_progress: 0, blocked: 0, todo: 0 };
|
|
212
|
+
const activeNames: string[] = [];
|
|
213
|
+
for (const t of taskSnapshot) {
|
|
214
|
+
counts[t.status] = (counts[t.status] || 0) + 1;
|
|
215
|
+
if (t.status === "in_progress" && t.assigned_to) activeNames.push(t.assigned_to);
|
|
216
|
+
}
|
|
217
|
+
const parts: string[] = [];
|
|
218
|
+
if (counts.done > 0) parts.push(theme.fg("accent", `${counts.done} done`));
|
|
219
|
+
if (counts.in_progress > 0) parts.push(theme.fg("warning", `${counts.in_progress} active`));
|
|
220
|
+
if (counts.blocked > 0) parts.push(theme.fg("error", `${counts.blocked} blocked`));
|
|
221
|
+
if (counts.todo > 0) parts.push(theme.fg("dim", `${counts.todo} todo`));
|
|
222
|
+
const line1 = truncateToWidth(`Tasks: ${parts.join(" ")} (${taskSnapshot.length} total)`, width);
|
|
223
|
+
const line2 = activeNames.length > 0
|
|
224
|
+
? truncateToWidth(theme.fg("dim", ` Active: ${activeNames.join(", ")}`), width)
|
|
225
|
+
: "";
|
|
226
|
+
const lines = [line1];
|
|
227
|
+
if (line2) lines.push(line2);
|
|
228
|
+
while (lines.length < height) lines.push("");
|
|
229
|
+
return lines.slice(0, height);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const DIM_EVENTS = new Set(["join", "leave", "reserve", "release", "plan.pass.start", "plan.pass.done", "plan.review.start", "plan.review.done"]);
|
|
233
|
+
|
|
234
|
+
export function renderFeedSection(theme: Theme, events: FeedEvent[], width: number, lastSeenTs: string | null): string[] {
|
|
235
|
+
if (events.length === 0) return [];
|
|
236
|
+
const lines: string[] = [];
|
|
237
|
+
let lastWasMessage = false;
|
|
238
|
+
|
|
239
|
+
for (const event of events) {
|
|
240
|
+
const sanitized = sanitizeFeedEvent(event);
|
|
241
|
+
const isNew = lastSeenTs === null || sanitized.ts > lastSeenTs;
|
|
242
|
+
const isMessage = sanitized.type === "message";
|
|
243
|
+
|
|
244
|
+
if (lines.length > 0 && isMessage !== lastWasMessage) {
|
|
245
|
+
lines.push(theme.fg("dim", " ·"));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (isMessage) {
|
|
249
|
+
lines.push(...renderMessageLines(theme, sanitized, width));
|
|
250
|
+
} else {
|
|
251
|
+
const formatted = sharedFormatFeedLine(sanitized);
|
|
252
|
+
const dimmed = DIM_EVENTS.has(sanitized.type) || !isNew;
|
|
253
|
+
lines.push(truncateToWidth(dimmed ? theme.fg("dim", formatted) : formatted, width));
|
|
254
|
+
}
|
|
255
|
+
lastWasMessage = isMessage;
|
|
256
|
+
}
|
|
257
|
+
return lines;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function formatTaskLabel(taskId: string): string {
|
|
261
|
+
if (taskId === "__planner__") return "(planner)";
|
|
262
|
+
if (taskId === "__reviser__") return "(reviser)";
|
|
263
|
+
if (taskId.startsWith("__lobby-") && taskId.endsWith("__")) return "(lobby)";
|
|
264
|
+
return taskId;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function wrapText(text: string, maxWidth: number): string[] {
|
|
268
|
+
if (text.length <= maxWidth) return [text];
|
|
269
|
+
const lines: string[] = [];
|
|
270
|
+
let remaining = text;
|
|
271
|
+
while (remaining.length > 0) {
|
|
272
|
+
if (remaining.length <= maxWidth) {
|
|
273
|
+
lines.push(remaining);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
let breakAt = remaining.lastIndexOf(" ", maxWidth);
|
|
277
|
+
if (breakAt <= 0) breakAt = maxWidth;
|
|
278
|
+
lines.push(remaining.slice(0, breakAt));
|
|
279
|
+
remaining = remaining.slice(breakAt).trimStart();
|
|
280
|
+
}
|
|
281
|
+
return lines;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function renderMessageLines(theme: Theme, event: FeedEvent, width: number): string[] {
|
|
285
|
+
const time = new Date(event.ts).toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hour12: false });
|
|
286
|
+
const agentStyled = coloredAgentName(event.agent);
|
|
287
|
+
const rawPreview = event.preview?.trim() ?? "";
|
|
288
|
+
|
|
289
|
+
const direction = event.target ? `\u2192 ${event.target}` : "\u2726";
|
|
290
|
+
const singleLen = time.length + 1 + event.agent.length + 1 + (event.target ? 2 + event.target.length : 1) + (rawPreview ? 1 + rawPreview.length : 0);
|
|
291
|
+
|
|
292
|
+
if (singleLen <= width && rawPreview) {
|
|
293
|
+
return [truncateToWidth(`${time} ${agentStyled} ${theme.fg("accent", direction)} ${rawPreview}`, width)];
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const header = `${time} ${agentStyled} ${theme.fg("accent", direction)}`;
|
|
297
|
+
if (!rawPreview) return [truncateToWidth(header, width)];
|
|
298
|
+
|
|
299
|
+
const indent = " ";
|
|
300
|
+
const maxBody = width - indent.length;
|
|
301
|
+
const wrapped = wrapText(rawPreview, maxBody);
|
|
302
|
+
const result = [truncateToWidth(header, width)];
|
|
303
|
+
for (const bodyLine of wrapped) {
|
|
304
|
+
result.push(truncateToWidth(`${indent}${bodyLine}`, width));
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function renderAgentsRow(
|
|
310
|
+
cwd: string,
|
|
311
|
+
width: number,
|
|
312
|
+
state: MessengerState,
|
|
313
|
+
dirs: Dirs,
|
|
314
|
+
stuckThresholdMs: number,
|
|
315
|
+
taskSnapshots: Map<string, Task[]> = new Map(),
|
|
316
|
+
): string {
|
|
317
|
+
const allClaims = store.getClaims(dirs);
|
|
318
|
+
const rowParts: string[] = [];
|
|
319
|
+
const seen = new Set<string>();
|
|
320
|
+
|
|
321
|
+
const self = buildSelfRegistration(state);
|
|
322
|
+
rowParts.push(`🟢 You (${idleLabel(self.activity?.lastActivityAt ?? self.startedAt)})`);
|
|
323
|
+
seen.add(self.name);
|
|
324
|
+
|
|
325
|
+
for (const agent of store.getActiveAgents(state, dirs)) {
|
|
326
|
+
if (seen.has(agent.name)) continue;
|
|
327
|
+
let tasks = taskSnapshots.get(agent.cwd);
|
|
328
|
+
if (!tasks) {
|
|
329
|
+
tasks = crewStore.getTasks(agent.cwd);
|
|
330
|
+
taskSnapshots.set(agent.cwd, tasks);
|
|
331
|
+
}
|
|
332
|
+
const computed = computeStatus(
|
|
333
|
+
agent.activity?.lastActivityAt ?? agent.startedAt,
|
|
334
|
+
agentHasTask(agent.name, allClaims, tasks),
|
|
335
|
+
(agent.reservations?.length ?? 0) > 0,
|
|
336
|
+
stuckThresholdMs,
|
|
337
|
+
);
|
|
338
|
+
const indicator = STATUS_INDICATORS[computed.status];
|
|
339
|
+
const idle = computed.idleFor ? ` ${computed.idleFor}` : "";
|
|
340
|
+
rowParts.push(`${indicator} ${coloredAgentName(agent.name)}${idle}`);
|
|
341
|
+
seen.add(agent.name);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
for (const worker of getLiveWorkers(cwd).values()) {
|
|
345
|
+
if (seen.has(worker.taskId)) continue;
|
|
346
|
+
rowParts.push(`🔵 ${worker.name} ${formatTaskLabel(worker.taskId)}`);
|
|
347
|
+
seen.add(worker.taskId);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return truncateToWidth(rowParts.join(" "), width);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function renderEmptyState(theme: Theme, cwd: string, width: number, height: number): string[] {
|
|
354
|
+
const lines: string[] = [];
|
|
355
|
+
const agents = discoverCrewAgents(cwd);
|
|
356
|
+
const config = loadConfig(cwd);
|
|
357
|
+
const crewConfig = loadCrewConfig(crewStore.getCrewDir(cwd));
|
|
358
|
+
|
|
359
|
+
lines.push("Crew agents:");
|
|
360
|
+
if (agents.length === 0) {
|
|
361
|
+
lines.push(theme.fg("dim", " (none discovered)"));
|
|
362
|
+
} else {
|
|
363
|
+
for (const agent of agents) {
|
|
364
|
+
const effectiveModel = crewConfig.models?.[agent.crewRole ?? "worker"] ?? agent.model;
|
|
365
|
+
const model = effectiveModel ? ` (model: ${effectiveModel})` : "";
|
|
366
|
+
lines.push(` ${agent.name}${model}`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
lines.push("");
|
|
371
|
+
lines.push("Config:");
|
|
372
|
+
lines.push(` Workers: ${crewConfig.concurrency.workers} │ Stuck threshold: ${config.stuckThreshold}s`);
|
|
373
|
+
lines.push(` Auto-overlay: ${config.autoOverlay ? "on" : "off"} │ Feed retention: ${config.feedRetention}`);
|
|
374
|
+
lines.push("");
|
|
375
|
+
lines.push("Create a plan:");
|
|
376
|
+
lines.push(" pi_messenger({ action: \"plan\", prd: \"docs/PRD.md\" })");
|
|
377
|
+
|
|
378
|
+
if (lines.length > height) {
|
|
379
|
+
return lines.slice(0, height).map(line => truncateToWidth(line, width));
|
|
380
|
+
}
|
|
381
|
+
while (lines.length < height) lines.push("");
|
|
382
|
+
return lines.map(line => truncateToWidth(line, width));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function renderPlanningState(theme: Theme, cwd: string, width: number, height: number): string[] {
|
|
386
|
+
const lines: string[] = [];
|
|
387
|
+
const updated = planningState.updatedAt ? formatRelativeTime(planningState.updatedAt) : "unknown";
|
|
388
|
+
const stalled = isPlanningStalled(cwd);
|
|
389
|
+
const ageMs = getPlanningUpdateAgeMs(cwd);
|
|
390
|
+
|
|
391
|
+
const plannerWorker = getLiveWorkers(cwd).get("__planner__");
|
|
392
|
+
const reviewerWorker = getLiveWorkers(cwd).get("__reviewer__");
|
|
393
|
+
const activeWorker = plannerWorker ?? reviewerWorker;
|
|
394
|
+
|
|
395
|
+
lines.push(stalled ? theme.fg("warning", "Planning stalled") : "Planning in progress");
|
|
396
|
+
lines.push(` Pass: ${planningState.pass}/${planningState.maxPasses} │ Phase: ${planningState.phase} │ ${updated}`);
|
|
397
|
+
|
|
398
|
+
if (stalled) {
|
|
399
|
+
const staleFor = ageMs === null ? "unknown" : formatDuration(ageMs);
|
|
400
|
+
lines.push(theme.fg("warning", ` Health: stalled (${staleFor}, timeout ${formatDuration(PLANNING_STALE_TIMEOUT_MS)})`));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
lines.push("");
|
|
404
|
+
|
|
405
|
+
if (activeWorker) {
|
|
406
|
+
const p = activeWorker.progress;
|
|
407
|
+
const tokens = p.tokens > 1000 ? `${(p.tokens / 1000).toFixed(0)}k` : `${p.tokens}`;
|
|
408
|
+
const elapsed = formatElapsed(Date.now() - activeWorker.startedAt);
|
|
409
|
+
lines.push(` ${activeWorker.agent} │ ${p.toolCallCount} calls ${tokens} tokens ${elapsed}`);
|
|
410
|
+
lines.push("");
|
|
411
|
+
const activityLines = renderActivityLog(p.recentTools, p.currentTool, p.currentToolArgs, activeWorker.startedAt, width);
|
|
412
|
+
lines.push(...activityLines);
|
|
413
|
+
} else {
|
|
414
|
+
const tail = readPlanningTail(cwd, 5);
|
|
415
|
+
if (tail.length > 0) {
|
|
416
|
+
lines.push(theme.fg("dim", " recent:"));
|
|
417
|
+
for (const item of tail) {
|
|
418
|
+
lines.push(theme.fg("dim", ` ${item}`));
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
lines.push("");
|
|
422
|
+
lines.push(theme.fg("dim", " progress: .pi/messenger/crew/planning-progress.md"));
|
|
423
|
+
lines.push(theme.fg("dim", " outline: .pi/messenger/crew/planning-outline.md"));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (lines.length > height) {
|
|
427
|
+
return lines.slice(-height).map(line => truncateToWidth(line, width));
|
|
428
|
+
}
|
|
429
|
+
while (lines.length < height) lines.push("");
|
|
430
|
+
return lines.map(line => truncateToWidth(line, width));
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export function renderLegend(
|
|
434
|
+
theme: Theme,
|
|
435
|
+
cwd: string,
|
|
436
|
+
width: number,
|
|
437
|
+
viewState: CrewViewState,
|
|
438
|
+
task: Task | null,
|
|
439
|
+
): string {
|
|
440
|
+
if (viewState.confirmAction) {
|
|
441
|
+
const text = renderConfirmBar(viewState.confirmAction.taskId, viewState.confirmAction.label, viewState.confirmAction.type);
|
|
442
|
+
return truncateToWidth(theme.fg("warning", appendUniversalHints(text)), width);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (viewState.inputMode === "block-reason") {
|
|
446
|
+
const text = renderBlockReasonBar(viewState.blockReasonInput);
|
|
447
|
+
return truncateToWidth(theme.fg("warning", appendUniversalHints(text)), width);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (viewState.inputMode === "message") {
|
|
451
|
+
const text = renderMessageBar(viewState.messageInput);
|
|
452
|
+
return truncateToWidth(theme.fg("accent", text + " [^T] [^B]"), width);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (viewState.inputMode === "revise-prompt") {
|
|
456
|
+
const label = viewState.reviseScope === "tree" ? "Revise tree" : "Revise";
|
|
457
|
+
const text = `${label}: ${viewState.revisePromptInput}█ [Enter] Send [Esc] Cancel`;
|
|
458
|
+
return truncateToWidth(theme.fg("accent", appendUniversalHints(text)), width);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (viewState.notification) {
|
|
462
|
+
if (Date.now() < viewState.notification.expiresAt) {
|
|
463
|
+
return truncateToWidth(appendUniversalHints(viewState.notification.message), width);
|
|
464
|
+
}
|
|
465
|
+
viewState.notification = null;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (viewState.mode === "detail" && task) {
|
|
469
|
+
return truncateToWidth(theme.fg("dim", appendUniversalHints(renderDetailStatusBar(cwd, task))), width);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (task) {
|
|
473
|
+
return truncateToWidth(theme.fg("dim", appendUniversalHints(renderListStatusBar(cwd, task))), width);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (isPlanningForCwd(cwd)) {
|
|
477
|
+
return truncateToWidth(
|
|
478
|
+
theme.fg("dim", appendUniversalHints(`c:Cancel v:${coordHint(cwd)} +/-:Wkrs Esc:Close`)),
|
|
479
|
+
width,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return truncateToWidth(theme.fg("dim", appendUniversalHints(`m:Chat v:${coordHint(cwd)} +/-:Wkrs Esc:Close`)), width);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export function renderDetailView(cwd: string, task: Task, width: number, height: number, viewState: CrewViewState): string[] {
|
|
487
|
+
const live = getLiveWorkers(cwd).get(task.id);
|
|
488
|
+
|
|
489
|
+
const lines: string[] = [];
|
|
490
|
+
const tokens = live ? (live.progress.tokens > 1000 ? `${(live.progress.tokens / 1000).toFixed(0)}k` : `${live.progress.tokens}`) : "";
|
|
491
|
+
const elapsed = live ? formatElapsed(Date.now() - live.startedAt) : "";
|
|
492
|
+
|
|
493
|
+
lines.push(`${task.id}: ${task.title}`);
|
|
494
|
+
if (live) {
|
|
495
|
+
lines.push(`Status: ${task.status} │ ${live.name} │ ${live.progress.toolCallCount} calls ${tokens} tokens ${elapsed}`);
|
|
496
|
+
} else {
|
|
497
|
+
const typeText = task.milestone ? " │ Type: milestone" : "";
|
|
498
|
+
const assignedText = task.assigned_to ? ` │ Assigned: ${task.assigned_to}` : "";
|
|
499
|
+
lines.push(`Status: ${task.status} │ Attempts: ${task.attempt_count} │ Created: ${formatRelativeTime(task.created_at)}${typeText}${assignedText}`);
|
|
500
|
+
}
|
|
501
|
+
lines.push("");
|
|
502
|
+
|
|
503
|
+
if (task.status === "in_progress" && !live) {
|
|
504
|
+
const startedText = task.started_at ? ` (started ${formatRelativeTime(task.started_at)})` : "";
|
|
505
|
+
lines.push(`⚠ Worker not running${startedText} — press [q] to stop and unassign`);
|
|
506
|
+
lines.push("");
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (live) {
|
|
510
|
+
const activityLines = renderActivityLog(
|
|
511
|
+
live.progress.recentTools,
|
|
512
|
+
live.progress.currentTool,
|
|
513
|
+
live.progress.currentToolArgs,
|
|
514
|
+
live.startedAt,
|
|
515
|
+
width,
|
|
516
|
+
);
|
|
517
|
+
lines.push(...activityLines);
|
|
518
|
+
} else {
|
|
519
|
+
if (task.depends_on.length > 0) {
|
|
520
|
+
lines.push("Dependencies:");
|
|
521
|
+
for (const depId of task.depends_on) {
|
|
522
|
+
const dep = crewStore.getTask(cwd, depId);
|
|
523
|
+
if (!dep) lines.push(` ○ ${depId}: (missing)`);
|
|
524
|
+
else lines.push(` ${dep.status === "done" ? "✓" : "○"} ${dep.id}: ${dep.title} (${dep.status})`);
|
|
525
|
+
}
|
|
526
|
+
lines.push("");
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const progress = crewStore.getTaskProgress(cwd, task.id);
|
|
530
|
+
if (progress) {
|
|
531
|
+
lines.push("Progress:");
|
|
532
|
+
for (const line of progress.trimEnd().split("\n")) lines.push(` ${line}`);
|
|
533
|
+
lines.push("");
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (task.status === "blocked") {
|
|
537
|
+
lines.push(`Block Reason: ${task.blocked_reason ?? "Unknown"}`);
|
|
538
|
+
const blockContext = crewStore.getBlockContext(cwd, task.id);
|
|
539
|
+
if (blockContext) {
|
|
540
|
+
lines.push("", "Block Context:");
|
|
541
|
+
for (const line of blockContext.trimEnd().split("\n")) lines.push(` ${line}`);
|
|
542
|
+
}
|
|
543
|
+
lines.push("");
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (task.last_review) {
|
|
547
|
+
const icon = task.last_review.verdict === "SHIP" ? "✓" : task.last_review.verdict === "NEEDS_WORK" ? "✗" : "⚠";
|
|
548
|
+
lines.push(`Last Review: ${icon} ${task.last_review.verdict} (${formatRelativeTime(task.last_review.reviewed_at)})`);
|
|
549
|
+
if (task.last_review.issues.length > 0) {
|
|
550
|
+
lines.push(" Issues:");
|
|
551
|
+
for (const issue of task.last_review.issues) lines.push(` - ${issue}`);
|
|
552
|
+
}
|
|
553
|
+
if (task.last_review.suggestions.length > 0) {
|
|
554
|
+
lines.push(" Suggestions:");
|
|
555
|
+
for (const suggestion of task.last_review.suggestions) lines.push(` - ${suggestion}`);
|
|
556
|
+
}
|
|
557
|
+
lines.push("");
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (task.status === "done") {
|
|
561
|
+
lines.push(`Completion Summary: ${task.summary ?? "(none)"}`);
|
|
562
|
+
const evidence = task.evidence;
|
|
563
|
+
if (evidence && (evidence.commits?.length || evidence.tests?.length || evidence.prs?.length)) {
|
|
564
|
+
lines.push("Evidence:");
|
|
565
|
+
if (evidence.commits?.length) lines.push(` Commits: ${evidence.commits.join(", ")}`);
|
|
566
|
+
if (evidence.tests?.length) lines.push(` Tests: ${evidence.tests.join(", ")}`);
|
|
567
|
+
if (evidence.prs?.length) lines.push(` PRs: ${evidence.prs.join(", ")}`);
|
|
568
|
+
}
|
|
569
|
+
lines.push("");
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
lines.push("Spec:");
|
|
573
|
+
const spec = crewStore.getTaskSpec(cwd, task.id);
|
|
574
|
+
if (!spec || spec.trimEnd().length === 0) lines.push(" *No spec available*");
|
|
575
|
+
else for (const line of spec.trimEnd().split("\n")) lines.push(` ${line}`);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const maxScroll = Math.max(0, lines.length - height);
|
|
579
|
+
if (live && viewState.detailAutoScroll) {
|
|
580
|
+
viewState.detailScroll = maxScroll;
|
|
581
|
+
}
|
|
582
|
+
viewState.detailScroll = Math.max(0, Math.min(viewState.detailScroll, maxScroll));
|
|
583
|
+
const visible = lines.slice(viewState.detailScroll, viewState.detailScroll + height).map(line => truncateToWidth(line, width));
|
|
584
|
+
while (visible.length < height) visible.push("");
|
|
585
|
+
return visible;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function coordHint(cwd: string): string {
|
|
589
|
+
return loadCrewConfig(crewStore.getCrewDir(cwd)).coordination ?? "chatty";
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function renderDetailStatusBar(cwd: string, task: Task): string {
|
|
593
|
+
const hints: string[] = [];
|
|
594
|
+
if (task.status === "in_progress") hints.push("q:Stop");
|
|
595
|
+
if (["done", "blocked", "in_progress"].includes(task.status)) hints.push("r:Reset");
|
|
596
|
+
if (task.status === "blocked") hints.push("u:Unblock");
|
|
597
|
+
if (task.status !== "done" && !task.milestone) hints.push("S:Split");
|
|
598
|
+
if (task.status === "todo" && !task.milestone) hints.push("s:Start");
|
|
599
|
+
if (task.status === "in_progress") hints.push("b:Block");
|
|
600
|
+
if (task.status !== "in_progress" && !task.milestone) hints.push("p:Revise");
|
|
601
|
+
if (task.status !== "in_progress" && !task.milestone) hints.push("P:Tree");
|
|
602
|
+
if (!(task.status === "in_progress" && hasLiveWorker(cwd, task.id))) hints.push("x:Del");
|
|
603
|
+
if (!isPlanningForCwd(cwd)) hints.push("m:Chat");
|
|
604
|
+
hints.push(`v:${coordHint(cwd)}`, "f:Feed", "+/-:Wkrs", "←→:Nav");
|
|
605
|
+
return hints.join(" ");
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function renderListStatusBar(cwd: string, task: Task): string {
|
|
609
|
+
const hints: string[] = ["Enter:Detail"];
|
|
610
|
+
if (task.status === "in_progress") hints.push("q:Stop");
|
|
611
|
+
if (["done", "blocked", "in_progress"].includes(task.status)) hints.push("r:Reset");
|
|
612
|
+
if (task.status === "blocked") hints.push("u:Unblock");
|
|
613
|
+
if (task.status !== "done" && !task.milestone) hints.push("S:Split");
|
|
614
|
+
if (task.status === "todo" && !task.milestone) hints.push("s:Start");
|
|
615
|
+
if (task.status === "in_progress") hints.push("b:Block");
|
|
616
|
+
if (task.status !== "in_progress" && !task.milestone) hints.push("p:Revise");
|
|
617
|
+
if (!(task.status === "in_progress" && hasLiveWorker(cwd, task.id))) hints.push("x:Del");
|
|
618
|
+
if (!isPlanningForCwd(cwd)) hints.push("m:Chat");
|
|
619
|
+
hints.push(`v:${coordHint(cwd)}`, "f:Feed", "+/-:Wkrs");
|
|
620
|
+
return hints.join(" ");
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function renderConfirmBar(taskId: string, label: string, type: "reset" | "cascade-reset" | "delete" | "cancel-planning"): string {
|
|
624
|
+
if (type === "cancel-planning") return "⚠ Cancel planning? [y] Confirm [n] Cancel";
|
|
625
|
+
if (type === "reset") return `⚠ Reset ${taskId} \"${label}\"? [y] Confirm [n] Cancel`;
|
|
626
|
+
if (type === "cascade-reset") return `⚠ Cascade reset ${taskId} and dependents? [y] Confirm [n] Cancel`;
|
|
627
|
+
return `⚠ Delete ${taskId} \"${label}\"? [y] Confirm [n] Cancel`;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function renderBlockReasonBar(input: string): string {
|
|
631
|
+
return `Block reason: ${input}█ [Enter] Confirm [Esc] Cancel`;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function renderMessageBar(input: string): string {
|
|
635
|
+
const isAt = input.startsWith("@");
|
|
636
|
+
const hint = isAt ? "DM" : "broadcast";
|
|
637
|
+
const tabHint = isAt && !input.includes(" ") ? " [Tab] Complete" : "";
|
|
638
|
+
return `${hint}: ${input}█ [Enter] Send${tabHint} [Esc] Cancel`;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function renderTaskLine(theme: Theme, task: Task, isSelected: boolean, width: number, liveWorker?: LiveWorkerInfo): string {
|
|
642
|
+
const select = isSelected ? theme.fg("accent", "▸ ") : " ";
|
|
643
|
+
const icon = STATUS_ICONS[task.status] ?? "?";
|
|
644
|
+
const coloredIcon = task.status === "done"
|
|
645
|
+
? theme.fg("accent", icon)
|
|
646
|
+
: task.status === "in_progress"
|
|
647
|
+
? theme.fg("warning", icon)
|
|
648
|
+
: task.status === "blocked"
|
|
649
|
+
? theme.fg("error", icon)
|
|
650
|
+
: theme.fg("dim", icon);
|
|
651
|
+
|
|
652
|
+
let suffix = "";
|
|
653
|
+
if (task.status === "in_progress" && liveWorker) {
|
|
654
|
+
suffix = ` (${liveWorker.name})`;
|
|
655
|
+
} else if (task.status === "in_progress" && task.assigned_to) {
|
|
656
|
+
suffix = ` (${task.assigned_to})`;
|
|
657
|
+
} else if (task.status === "todo" && task.depends_on.length > 0) {
|
|
658
|
+
suffix = ` → ${task.depends_on.join(", ")}`;
|
|
659
|
+
} else if (task.status === "blocked" && task.blocked_reason) {
|
|
660
|
+
const reason = task.blocked_reason.slice(0, 28);
|
|
661
|
+
suffix = ` [${reason}${task.blocked_reason.length > 28 ? "…" : ""}]`;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const labels: string[] = [];
|
|
665
|
+
if (task.role) labels.push(`[${task.role}]`);
|
|
666
|
+
if (task.approval?.required) labels.push(`[${task.approval.status}]`);
|
|
667
|
+
const labelText = labels.length > 0 ? `${labels.join(" ")} ` : "";
|
|
668
|
+
|
|
669
|
+
if (task.milestone) suffix += `${suffix ? " " : ""}· milestone`;
|
|
670
|
+
return truncateToWidth(`${select}${coloredIcon} ${task.id} ${labelText}${task.title}${theme.fg("dim", suffix)}`, width);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
export function navigateTask(viewState: CrewViewState, direction: 1 | -1, taskCount: number): void {
|
|
674
|
+
if (taskCount === 0) return;
|
|
675
|
+
viewState.selectedTaskIndex = Math.max(0, Math.min(taskCount - 1, viewState.selectedTaskIndex + direction));
|
|
676
|
+
}
|