@loka1/pi-orchestra 0.0.0-stage → 0.6.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 +313 -0
- package/LICENSE +21 -0
- package/README.md +662 -2
- package/assets/preview.html +226 -0
- package/assets/preview.png +0 -0
- package/config/orchestra.json.example +35 -0
- package/config/templates/review-squad.md +11 -0
- package/extensions/lib/agents.ts +110 -0
- package/extensions/lib/chat.ts +171 -0
- package/extensions/lib/commands.ts +118 -0
- package/extensions/lib/config.ts +236 -0
- package/extensions/lib/format.ts +663 -0
- package/extensions/lib/git.ts +99 -0
- package/extensions/lib/log.ts +233 -0
- package/extensions/lib/mail.ts +115 -0
- package/extensions/lib/metrics.ts +365 -0
- package/extensions/lib/providers.ts +355 -0
- package/extensions/lib/redact.ts +57 -0
- package/extensions/lib/retry.ts +61 -0
- package/extensions/lib/state.ts +694 -0
- package/extensions/lib/templates.ts +153 -0
- package/extensions/lib/types.ts +171 -0
- package/extensions/lib/worker.ts +450 -0
- package/extensions/orchestra.ts +3043 -0
- package/extensions/ui/browse.ts +497 -0
- package/extensions/ui/join.ts +289 -0
- package/package.json +56 -4
- package/skills/orchestra/SKILL.md +86 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure formatting helpers: status bar text, widget rows, browse items, tables.
|
|
3
|
+
*
|
|
4
|
+
* No pi imports, so `node scripts/smoke.mjs` exercises all of this directly.
|
|
5
|
+
* Anything width-specific (truncateToWidth) is left to the UI layer: these
|
|
6
|
+
* helpers build short, prioritised rows and the render callback fits them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
type Dispatch,
|
|
11
|
+
type Gate,
|
|
12
|
+
type OrchestraState,
|
|
13
|
+
type Run,
|
|
14
|
+
type Task,
|
|
15
|
+
isLiveStatus,
|
|
16
|
+
} from "./types.ts";
|
|
17
|
+
import { formatModel } from "./providers.ts";
|
|
18
|
+
import { listDispatches, listTasks, totalUsage } from "./state.ts";
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// numbers
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
export function formatTokens(count: number): string {
|
|
25
|
+
if (!count) return "0";
|
|
26
|
+
if (count < 1000) return String(count);
|
|
27
|
+
if (count < 10_000) return `${(count / 1000).toFixed(1)}k`;
|
|
28
|
+
if (count < 1_000_000) return `${Math.round(count / 1000)}k`;
|
|
29
|
+
return `${(count / 1_000_000).toFixed(1)}M`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function formatCost(cost: number): string {
|
|
33
|
+
if (!cost) return "$0";
|
|
34
|
+
return cost < 0.01 ? `$${cost.toFixed(4)}` : `$${cost.toFixed(2)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** "3m", "2h", "4d" — how long something has been running or has been done. */
|
|
38
|
+
export function age(iso: string | undefined, now = Date.now()): string {
|
|
39
|
+
if (!iso) return "";
|
|
40
|
+
const started = Date.parse(iso);
|
|
41
|
+
if (Number.isNaN(started)) return "";
|
|
42
|
+
const seconds = Math.max(0, Math.round((now - started) / 1000));
|
|
43
|
+
if (seconds < 60) return `${seconds}s`;
|
|
44
|
+
if (seconds < 3600) return `${Math.round(seconds / 60)}m`;
|
|
45
|
+
if (seconds < 86_400) return `${Math.round(seconds / 3600)}h`;
|
|
46
|
+
return `${Math.round(seconds / 86_400)}d`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function shortId(id: string): string {
|
|
50
|
+
return id.length > 14 ? id.slice(-10) : id;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// status: dispatch wins over task
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
export type EffectiveStatus = string;
|
|
58
|
+
|
|
59
|
+
export interface Effective {
|
|
60
|
+
status: EffectiveStatus;
|
|
61
|
+
dispatch?: Dispatch;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A dispatch is authoritative while it exists: a task whose worker settled must
|
|
66
|
+
* never render a failure glyph just because the task row was written first.
|
|
67
|
+
*/
|
|
68
|
+
export function effectiveTask(state: OrchestraState, task: Task): Effective {
|
|
69
|
+
const dispatch = task.dispatchId ? state.dispatches[task.dispatchId] : undefined;
|
|
70
|
+
if (!dispatch) return { status: task.status };
|
|
71
|
+
if (dispatch.status === "settled") return { status: "done", dispatch };
|
|
72
|
+
return { status: dispatch.status, dispatch };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const GLYPHS: Record<string, string> = {
|
|
76
|
+
question: "?",
|
|
77
|
+
running: "●",
|
|
78
|
+
starting: "◌",
|
|
79
|
+
ready: "◌",
|
|
80
|
+
pending: "·",
|
|
81
|
+
done: "✓",
|
|
82
|
+
settled: "✓",
|
|
83
|
+
stopped: "◼",
|
|
84
|
+
timeout: "⏱",
|
|
85
|
+
failed: "✗",
|
|
86
|
+
lost: "☠",
|
|
87
|
+
blocked: "⊘",
|
|
88
|
+
active: "●",
|
|
89
|
+
stalled: "⚠",
|
|
90
|
+
orphaned: "⚡",
|
|
91
|
+
abandoned: "◼",
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export function statusGlyph(status: string): string {
|
|
95
|
+
return GLYPHS[status] ?? "·";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Lower sorts first: things that need a human or are moving. */
|
|
99
|
+
const PRIORITY: Record<string, number> = {
|
|
100
|
+
question: 0,
|
|
101
|
+
running: 1,
|
|
102
|
+
starting: 2,
|
|
103
|
+
timeout: 3,
|
|
104
|
+
failed: 4,
|
|
105
|
+
orphaned: 4.5,
|
|
106
|
+
lost: 5,
|
|
107
|
+
ready: 6,
|
|
108
|
+
pending: 7,
|
|
109
|
+
blocked: 8,
|
|
110
|
+
stopped: 9,
|
|
111
|
+
done: 10,
|
|
112
|
+
settled: 10,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export function statusPriority(status: string): number {
|
|
116
|
+
return PRIORITY[status] ?? 20;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function statusLabel(status: string, since?: string): string {
|
|
120
|
+
switch (status) {
|
|
121
|
+
case "running":
|
|
122
|
+
case "starting":
|
|
123
|
+
case "question":
|
|
124
|
+
return since ? `${status} ${age(since)}` : status;
|
|
125
|
+
case "timeout":
|
|
126
|
+
return since ? `timeout ${age(since)}` : "timeout";
|
|
127
|
+
case "orphaned":
|
|
128
|
+
return "orphaned";
|
|
129
|
+
case "stalled":
|
|
130
|
+
return since ? `stalled ${age(since)}` : "stalled";
|
|
131
|
+
default:
|
|
132
|
+
return status;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function dispatchGlyph(dispatch: Dispatch): string {
|
|
137
|
+
// A deliberate stop stays "stopped" even when the exit handler set "settled"
|
|
138
|
+
// first, so the glyph and the report never disagree.
|
|
139
|
+
if (dispatch.stopReason === "user" && dispatch.status === "settled") return statusGlyph("stopped");
|
|
140
|
+
return statusGlyph(dispatch.status);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function taskGlyph(task: Task): string {
|
|
144
|
+
return statusGlyph(task.status);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function liveDispatches(state: OrchestraState, runId?: string): Dispatch[] {
|
|
148
|
+
return listDispatches(state, runId).filter((dispatch) => isLiveStatus(dispatch.status));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
// status line
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
export function statusLine(state: OrchestraState, run?: Run): string {
|
|
156
|
+
const dispatches = listDispatches(state, run?.id);
|
|
157
|
+
const tasks = listTasks(state, run?.id);
|
|
158
|
+
const live = dispatches.filter((d) => d.status === "running" || d.status === "starting").length;
|
|
159
|
+
const questions = dispatches.filter((d) => d.status === "question").length;
|
|
160
|
+
const timeout = dispatches.filter((d) => d.status === "timeout").length;
|
|
161
|
+
const failed = dispatches.filter((d) => d.status === "failed").length;
|
|
162
|
+
const stalled = dispatches.filter((d) => d.stalledAt && (d.status === "running" || d.status === "starting")).length;
|
|
163
|
+
const ready = tasks.filter((t) => effectiveTask(state, t).status === "ready").length;
|
|
164
|
+
const done = tasks.filter((t) => effectiveTask(state, t).status === "done").length;
|
|
165
|
+
|
|
166
|
+
const parts = [`orchestra: ${live} running`];
|
|
167
|
+
if (questions) parts.push(`${questions} question${questions === 1 ? "" : "s"}`);
|
|
168
|
+
if (ready) parts.push(`${ready} ready`);
|
|
169
|
+
if (stalled) parts.push(`${stalled} stalled`);
|
|
170
|
+
const orphans = dispatches.filter((dispatch) => dispatch.status === "orphaned").length;
|
|
171
|
+
if (orphans) parts.push(`${orphans} orphaned`);
|
|
172
|
+
if (timeout) parts.push(`${timeout} timeout`);
|
|
173
|
+
if (failed) parts.push(`${failed} failed`);
|
|
174
|
+
if (done) parts.push(`${done} done`);
|
|
175
|
+
return parts.join(" · ");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// widget rows (the compact, always-on map)
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
export interface WidgetOptions {
|
|
183
|
+
maxLines?: number;
|
|
184
|
+
showSettled?: boolean;
|
|
185
|
+
titleWidth?: number;
|
|
186
|
+
now?: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* True while something is actually *running* in this run: a worker starting, running
|
|
191
|
+
* or waiting on an answer, an orphaned process still alive, or a task that is running
|
|
192
|
+
* or blocked. Ready/pending tasks alone do not count — they wait for the coordinator,
|
|
193
|
+
* and the status line already says how many there are.
|
|
194
|
+
*
|
|
195
|
+
* The widget's `auto` mode mounts only when this is true, so a settled run leaves the
|
|
196
|
+
* viewport alone instead of holding eight rows of finished history.
|
|
197
|
+
*/
|
|
198
|
+
export function hasLiveWork(state: OrchestraState, run?: Run): boolean {
|
|
199
|
+
if (!run) return false;
|
|
200
|
+
const dispatches = listDispatches(state, run.id);
|
|
201
|
+
// `orphaned` counts: the process may still be alive and its task is stuck until you decide.
|
|
202
|
+
if (dispatches.some((dispatch) => isLiveStatus(dispatch.status) || dispatch.status === "orphaned")) return true;
|
|
203
|
+
return listTasks(state, run.id).some((task) => {
|
|
204
|
+
const status = effectiveTask(state, task).status;
|
|
205
|
+
return status === "running" || status === "blocked";
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
interface Row {
|
|
210
|
+
priority: number;
|
|
211
|
+
created: string;
|
|
212
|
+
text: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function widgetRows(state: OrchestraState, run?: Run, options: WidgetOptions = {}): string[] {
|
|
216
|
+
const maxLines = options.maxLines ?? 8;
|
|
217
|
+
const showSettled = options.showSettled ?? false;
|
|
218
|
+
const titleWidth = options.titleWidth ?? 72;
|
|
219
|
+
const now = options.now ?? Date.now();
|
|
220
|
+
|
|
221
|
+
const tasks = listTasks(state, run?.id);
|
|
222
|
+
const dispatches = listDispatches(state, run?.id);
|
|
223
|
+
if (tasks.length === 0 && dispatches.length === 0) return [];
|
|
224
|
+
|
|
225
|
+
const header: string[] = [];
|
|
226
|
+
if (run) {
|
|
227
|
+
const counts = `tasks ${tasks.length} · workers ${dispatches.length}`;
|
|
228
|
+
header.push(`${fit(run.name, 40)} (${shortId(run.id)}) ${counts}`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const rows: Row[] = [];
|
|
232
|
+
let settledCount = 0;
|
|
233
|
+
let timedOut = 0;
|
|
234
|
+
let stalled = 0;
|
|
235
|
+
|
|
236
|
+
for (const task of tasks) {
|
|
237
|
+
const { status, dispatch } = effectiveTask(state, task);
|
|
238
|
+
if (dispatch?.stalledAt && (status === "running" || status === "starting")) stalled += 1;
|
|
239
|
+
if (status === "done" || status === "settled") {
|
|
240
|
+
settledCount += 1;
|
|
241
|
+
if (!showSettled) continue;
|
|
242
|
+
}
|
|
243
|
+
if (status === "timeout") timedOut += 1;
|
|
244
|
+
const since = dispatch?.startedAt ?? task.createdAt;
|
|
245
|
+
const revealStall = Boolean(dispatch?.stalledAt) && (status === "running" || status === "starting");
|
|
246
|
+
const label = statusLabel(revealStall ? "stalled" : status, dispatch?.stalledAt ?? since);
|
|
247
|
+
rows.push({
|
|
248
|
+
priority: revealStall ? 1.5 : statusPriority(status),
|
|
249
|
+
created: task.createdAt,
|
|
250
|
+
text: `${statusGlyph(revealStall ? "stalled" : status)} ${fit(label, 12).padEnd(12)} ${fit(task.title, titleWidth)}`,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
for (const dispatch of dispatches) {
|
|
255
|
+
if (dispatch.taskId && state.tasks[dispatch.taskId]) continue;
|
|
256
|
+
if (dispatch.status === "settled") {
|
|
257
|
+
settledCount += 1;
|
|
258
|
+
if (!showSettled) continue;
|
|
259
|
+
}
|
|
260
|
+
if (dispatch.status === "timeout") timedOut += 1;
|
|
261
|
+
const label = statusLabel(dispatch.status, dispatch.startedAt);
|
|
262
|
+
rows.push({
|
|
263
|
+
priority: statusPriority(dispatch.status),
|
|
264
|
+
created: dispatch.startedAt,
|
|
265
|
+
text: `${dispatchGlyph(dispatch)} ${fit(label, 12).padEnd(12)} ${fit(
|
|
266
|
+
`${shortId(dispatch.id)} ${dispatch.agent}`,
|
|
267
|
+
titleWidth,
|
|
268
|
+
)}`,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
rows.sort((a, b) => a.priority - b.priority || a.created.localeCompare(b.created));
|
|
273
|
+
|
|
274
|
+
const out = [...header];
|
|
275
|
+
const budget = Math.max(1, maxLines);
|
|
276
|
+
const visible = rows.slice(0, budget);
|
|
277
|
+
for (const row of visible) out.push(row.text);
|
|
278
|
+
|
|
279
|
+
const hidden = rows.length - visible.length;
|
|
280
|
+
const notes: string[] = [];
|
|
281
|
+
if (hidden > 0) notes.push(`+${hidden} more`);
|
|
282
|
+
if (settledCount > 0 && !showSettled) notes.push(`✓ ${settledCount} settled`);
|
|
283
|
+
if (timedOut > 0) notes.push(`⏱ ${timedOut} timed out`);
|
|
284
|
+
if (stalled > 0) notes.push(`⚠ ${stalled} stalled`);
|
|
285
|
+
if (notes.length > 0) out.push(`… ${notes.join(" · ")} — /orchestra browse`);
|
|
286
|
+
|
|
287
|
+
const usage = totalUsage(state, run?.id);
|
|
288
|
+
if (usage.turns || usage.toolCalls) {
|
|
289
|
+
out.push(
|
|
290
|
+
`${usage.turns} turns · ${usage.toolCalls} tools · ↑${formatTokens(usage.input)} ↓${formatTokens(
|
|
291
|
+
usage.output,
|
|
292
|
+
)} · ${formatCost(usage.cost)}`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
void now;
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function fit(text: string, width: number): string {
|
|
300
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
301
|
+
if (flat.length <= width) return flat;
|
|
302
|
+
return `${flat.slice(0, Math.max(0, width - 1)).trimEnd()}…`;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ---------------------------------------------------------------------------
|
|
306
|
+
// browse items (interactive list + text tables)
|
|
307
|
+
// ---------------------------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
export type BrowseScope = "tasks" | "workers" | "runs";
|
|
310
|
+
|
|
311
|
+
export interface BrowseItem {
|
|
312
|
+
id: string;
|
|
313
|
+
kind: "task" | "worker" | "run";
|
|
314
|
+
glyph: string;
|
|
315
|
+
status: string;
|
|
316
|
+
title: string;
|
|
317
|
+
meta: string;
|
|
318
|
+
runId: string;
|
|
319
|
+
taskId?: string;
|
|
320
|
+
dispatchId?: string;
|
|
321
|
+
priority: number;
|
|
322
|
+
created: string;
|
|
323
|
+
/** Spend so far, used by the cost sort and `cost:` filters. */
|
|
324
|
+
cost?: number;
|
|
325
|
+
agent?: string;
|
|
326
|
+
model?: string;
|
|
327
|
+
turns?: number;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export interface FilterTerm {
|
|
331
|
+
/** Lowercased field name, e.g. "status". */
|
|
332
|
+
field?: string;
|
|
333
|
+
operator?: ":" | ">" | "<" | ">=" | "<=";
|
|
334
|
+
value: string;
|
|
335
|
+
/** The raw token, for error messages. */
|
|
336
|
+
raw: string;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const FILTER_FIELDS = new Set(["status", "agent", "kind", "run", "id", "task", "cost", "turns", "model"]);
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Split a query into field terms (`status:failed`, `cost:>0.1`) and free text.
|
|
343
|
+
* A `word:value` token is only a field term when the field is known, so
|
|
344
|
+
* `fix: the bug` still searches for text.
|
|
345
|
+
*/
|
|
346
|
+
export function parseFilter(query: string): { terms: FilterTerm[]; text: string[] } {
|
|
347
|
+
const terms: FilterTerm[] = [];
|
|
348
|
+
const text: string[] = [];
|
|
349
|
+
for (const raw of query.trim().split(/\s+/).filter(Boolean)) {
|
|
350
|
+
// `field:value`, `field:>0.1` and `field>0.1` all describe the same field term.
|
|
351
|
+
const match = /^([A-Za-z_]+)(?::(>=|<=|>|<)?|(>=|<=|>|<))(.+)$/.exec(raw);
|
|
352
|
+
if (!match || !FILTER_FIELDS.has((match[1] as string).toLowerCase())) {
|
|
353
|
+
text.push(raw.replace(/:$/, ""));
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
terms.push({
|
|
357
|
+
field: (match[1] as string).toLowerCase(),
|
|
358
|
+
operator: (match[2] ?? match[3] ?? ":") as FilterTerm["operator"],
|
|
359
|
+
value: match[4] as string,
|
|
360
|
+
raw,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return { terms, text };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export interface FilterableItem {
|
|
367
|
+
id: string;
|
|
368
|
+
kind: string;
|
|
369
|
+
status: string;
|
|
370
|
+
title: string;
|
|
371
|
+
meta: string;
|
|
372
|
+
runId: string;
|
|
373
|
+
taskId?: string;
|
|
374
|
+
dispatchId?: string;
|
|
375
|
+
agent?: string;
|
|
376
|
+
model?: string;
|
|
377
|
+
cost?: number;
|
|
378
|
+
turns?: number;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function compareValue(actual: number, operator: FilterTerm["operator"], expected: string): boolean {
|
|
382
|
+
const wanted = Number(expected);
|
|
383
|
+
if (Number.isNaN(wanted)) return false;
|
|
384
|
+
switch (operator) {
|
|
385
|
+
case ">":
|
|
386
|
+
return actual > wanted;
|
|
387
|
+
case "<":
|
|
388
|
+
return actual < wanted;
|
|
389
|
+
case ">=":
|
|
390
|
+
return actual >= wanted;
|
|
391
|
+
case "<=":
|
|
392
|
+
return actual <= wanted;
|
|
393
|
+
default:
|
|
394
|
+
return actual === wanted;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** Structured + free-text match used by browseItems, task_list and worker_list. */
|
|
399
|
+
export function matchesItem(item: FilterableItem, query: string): boolean {
|
|
400
|
+
const { terms, text } = parseFilter(query);
|
|
401
|
+
const hay = [item.title, item.id, item.meta, item.status, item.taskId, item.dispatchId, item.runId]
|
|
402
|
+
.filter(Boolean)
|
|
403
|
+
.join(" ")
|
|
404
|
+
.toLowerCase();
|
|
405
|
+
if (!text.every((token) => hay.includes(token.toLowerCase()))) return false;
|
|
406
|
+
|
|
407
|
+
return terms.every((term) => {
|
|
408
|
+
const value = term.value.toLowerCase();
|
|
409
|
+
switch (term.field) {
|
|
410
|
+
case "status":
|
|
411
|
+
return item.status.toLowerCase().includes(value);
|
|
412
|
+
case "agent":
|
|
413
|
+
return (item.agent ?? "").toLowerCase().includes(value);
|
|
414
|
+
case "model":
|
|
415
|
+
return (item.model ?? "").toLowerCase().includes(value);
|
|
416
|
+
case "kind":
|
|
417
|
+
return item.kind.toLowerCase() === value;
|
|
418
|
+
case "run":
|
|
419
|
+
return item.runId.toLowerCase().includes(value);
|
|
420
|
+
case "id":
|
|
421
|
+
return [item.id, item.taskId, item.dispatchId]
|
|
422
|
+
.filter(Boolean)
|
|
423
|
+
.some((candidate) => (candidate as string).toLowerCase().includes(value));
|
|
424
|
+
case "task":
|
|
425
|
+
return item.title.toLowerCase().includes(value);
|
|
426
|
+
case "cost":
|
|
427
|
+
return compareValue(item.cost ?? 0, term.operator, term.value);
|
|
428
|
+
case "turns":
|
|
429
|
+
return compareValue(item.turns ?? 0, term.operator, term.value);
|
|
430
|
+
default:
|
|
431
|
+
return hay.includes(value);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** AND over whitespace-separated tokens, case-insensitive substring match. */
|
|
437
|
+
export function matchesQuery(query: string, haystack: Array<string | undefined>): boolean {
|
|
438
|
+
const tokens = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
439
|
+
if (tokens.length === 0) return true;
|
|
440
|
+
const hay = haystack.filter(Boolean).join(" ").toLowerCase();
|
|
441
|
+
return tokens.every((token) => hay.includes(token));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export type BrowseSort = "priority" | "cost" | "time";
|
|
445
|
+
|
|
446
|
+
export function browseItems(
|
|
447
|
+
state: OrchestraState,
|
|
448
|
+
run: Run | undefined,
|
|
449
|
+
scope: BrowseScope = "tasks",
|
|
450
|
+
query = "",
|
|
451
|
+
sort: BrowseSort = "priority",
|
|
452
|
+
): BrowseItem[] {
|
|
453
|
+
const items: BrowseItem[] = [];
|
|
454
|
+
|
|
455
|
+
if (scope === "tasks") {
|
|
456
|
+
for (const task of listTasks(state, run?.id)) {
|
|
457
|
+
const { status, dispatch } = effectiveTask(state, task);
|
|
458
|
+
const meta = [
|
|
459
|
+
dispatch ? `${shortId(dispatch.id)} · ${dispatch.agent} · ${formatModel(dispatch)}` : task.agent,
|
|
460
|
+
dispatch ? `${dispatch.usage.turns} turns · ${formatCost(dispatch.usage.cost)}` : undefined,
|
|
461
|
+
task.deps.length ? `after ${task.deps.map(shortId).join(", ")}` : undefined,
|
|
462
|
+
]
|
|
463
|
+
.filter(Boolean)
|
|
464
|
+
.join(" · ");
|
|
465
|
+
items.push({
|
|
466
|
+
id: task.id,
|
|
467
|
+
kind: "task",
|
|
468
|
+
glyph: statusGlyph(status),
|
|
469
|
+
status,
|
|
470
|
+
title: task.title,
|
|
471
|
+
meta,
|
|
472
|
+
runId: task.runId,
|
|
473
|
+
taskId: task.id,
|
|
474
|
+
dispatchId: dispatch?.id,
|
|
475
|
+
priority: statusPriority(status),
|
|
476
|
+
created: task.createdAt,
|
|
477
|
+
cost: dispatch?.usage.cost ?? 0,
|
|
478
|
+
agent: dispatch?.agent ?? task.agent,
|
|
479
|
+
model: dispatch ? formatModel(dispatch) : undefined,
|
|
480
|
+
turns: dispatch?.usage.turns,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
} else if (scope === "workers") {
|
|
484
|
+
for (const dispatch of listDispatches(state, run?.id)) {
|
|
485
|
+
const task = dispatch.taskId ? state.tasks[dispatch.taskId] : undefined;
|
|
486
|
+
items.push({
|
|
487
|
+
id: dispatch.id,
|
|
488
|
+
kind: "worker",
|
|
489
|
+
glyph: dispatchGlyph(dispatch),
|
|
490
|
+
status: dispatch.status,
|
|
491
|
+
title: task?.title ?? fit(dispatch.spec.split("\n")[0] ?? "", 80),
|
|
492
|
+
meta: [
|
|
493
|
+
dispatch.agent,
|
|
494
|
+
formatModel(dispatch),
|
|
495
|
+
`${dispatch.usage.turns} turns · ${dispatch.usage.toolCalls} tools · ${formatCost(dispatch.usage.cost)}`,
|
|
496
|
+
age(dispatch.startedAt),
|
|
497
|
+
]
|
|
498
|
+
.filter(Boolean)
|
|
499
|
+
.join(" · "),
|
|
500
|
+
runId: dispatch.runId,
|
|
501
|
+
taskId: dispatch.taskId,
|
|
502
|
+
dispatchId: dispatch.id,
|
|
503
|
+
priority: statusPriority(dispatch.status),
|
|
504
|
+
created: dispatch.startedAt,
|
|
505
|
+
cost: dispatch.usage.cost,
|
|
506
|
+
agent: dispatch.agent,
|
|
507
|
+
model: formatModel(dispatch),
|
|
508
|
+
turns: dispatch.usage.turns,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
} else {
|
|
512
|
+
for (const item of Object.values(state.runs)) {
|
|
513
|
+
const tasks = listTasks(state, item.id);
|
|
514
|
+
const dispatches = listDispatches(state, item.id);
|
|
515
|
+
items.push({
|
|
516
|
+
id: item.id,
|
|
517
|
+
kind: "run",
|
|
518
|
+
glyph: item.id === state.activeRunId ? "●" : "·",
|
|
519
|
+
status: item.status,
|
|
520
|
+
title: item.name,
|
|
521
|
+
meta: `${tasks.length} tasks · ${dispatches.length} workers · ${age(item.createdAt)}`,
|
|
522
|
+
runId: item.id,
|
|
523
|
+
priority: item.id === state.activeRunId ? 0 : 1,
|
|
524
|
+
created: item.createdAt,
|
|
525
|
+
cost: totalUsage(state, item.id).cost,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const filtered = items.filter((item) => matchesItem(item, query));
|
|
531
|
+
if (sort === "cost") {
|
|
532
|
+
return filtered.sort((a, b) => (b.cost ?? 0) - (a.cost ?? 0) || a.title.localeCompare(b.title));
|
|
533
|
+
}
|
|
534
|
+
if (sort === "time") {
|
|
535
|
+
return filtered.sort((a, b) => b.created.localeCompare(a.created) || a.title.localeCompare(b.title));
|
|
536
|
+
}
|
|
537
|
+
return filtered.sort((a, b) => a.priority - b.priority || a.created.localeCompare(b.created) || a.title.localeCompare(b.title));
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// ---------------------------------------------------------------------------
|
|
541
|
+
// tables with paging
|
|
542
|
+
// ---------------------------------------------------------------------------
|
|
543
|
+
|
|
544
|
+
export interface PageOptions {
|
|
545
|
+
limit?: number;
|
|
546
|
+
offset?: number;
|
|
547
|
+
filter?: string;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function pageOf<T>(items: T[], options: PageOptions = {}): { rows: T[]; footer?: string; total: number } {
|
|
551
|
+
const limit = options.limit && options.limit > 0 ? Math.floor(options.limit) : 20;
|
|
552
|
+
const offset = options.offset && options.offset > 0 ? Math.floor(options.offset) : 0;
|
|
553
|
+
const rows = items.slice(offset, offset + limit);
|
|
554
|
+
const total = items.length;
|
|
555
|
+
if (total <= limit && offset === 0) return { rows, total };
|
|
556
|
+
const end = offset + rows.length;
|
|
557
|
+
const footer =
|
|
558
|
+
end < total
|
|
559
|
+
? `showing ${offset + 1}–${end} of ${total} — offset=${end} for the rest`
|
|
560
|
+
: `showing ${offset + 1}–${end} of ${total}`;
|
|
561
|
+
return { rows, footer, total };
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export function tasksTable(state: OrchestraState, runId?: string, options: PageOptions = {}): string {
|
|
565
|
+
const all = browseItems(state, runId ? state.runs[runId] : undefined, "tasks", options.filter ?? "");
|
|
566
|
+
if (all.length === 0) return "No tasks in this run.";
|
|
567
|
+
const page = pageOf(all, options);
|
|
568
|
+
const lines = page.rows.map((item) => {
|
|
569
|
+
const deps = item.meta.includes("after") ? ` ${item.meta.slice(item.meta.indexOf("after"))}` : "";
|
|
570
|
+
const dispatch = item.dispatchId ? ` → ${shortId(item.dispatchId)}` : "";
|
|
571
|
+
return `${item.glyph} ${shortId(item.id)} ${item.status.padEnd(9)} ${item.title}${deps}${dispatch}`;
|
|
572
|
+
});
|
|
573
|
+
if (page.footer) lines.push(page.footer);
|
|
574
|
+
return lines.join("\n");
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export function workersTable(state: OrchestraState, runId?: string, options: PageOptions = {}): string {
|
|
578
|
+
const all = browseItems(state, runId ? state.runs[runId] : undefined, "workers", options.filter ?? "");
|
|
579
|
+
if (all.length === 0) return "No workers in this run.";
|
|
580
|
+
const page = pageOf(all, options);
|
|
581
|
+
const header = " id agent status turns tools cost task";
|
|
582
|
+
const lines = page.rows.map((item) => {
|
|
583
|
+
const dispatch = item.dispatchId ? state.dispatches[item.dispatchId] : undefined;
|
|
584
|
+
const usage = dispatch?.usage;
|
|
585
|
+
const id = shortId(item.id).padEnd(12);
|
|
586
|
+
const agent = (dispatch?.agent ?? "").padEnd(10);
|
|
587
|
+
const turns = String(usage?.turns ?? 0).padEnd(6);
|
|
588
|
+
const tools = String(usage?.toolCalls ?? 0).padEnd(6);
|
|
589
|
+
const cost = formatCost(usage?.cost ?? 0).padEnd(8);
|
|
590
|
+
return `${item.glyph} ${id} ${agent} ${item.status.padEnd(9)} ${turns} ${tools} ${cost} ${item.title}`;
|
|
591
|
+
});
|
|
592
|
+
if (page.footer) lines.push(page.footer);
|
|
593
|
+
return [header, ...lines].join("\n");
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// ---------------------------------------------------------------------------
|
|
597
|
+
// reports
|
|
598
|
+
// ---------------------------------------------------------------------------
|
|
599
|
+
|
|
600
|
+
export function dispatchReport(dispatch: Dispatch, task?: Task, body?: string): string {
|
|
601
|
+
const title = task?.title ?? dispatch.spec.split("\n")[0]?.slice(0, 80) ?? dispatch.agent;
|
|
602
|
+
const lines: string[] = [];
|
|
603
|
+
// A deliberate stop must never read as "settled", even if the exit handler won
|
|
604
|
+
// the race against the status update.
|
|
605
|
+
const status = dispatch.status === "settled" && dispatch.stopReason === "user" ? "stopped" : dispatch.status;
|
|
606
|
+
lines.push(`${dispatchGlyph(dispatch)} Worker ${shortId(dispatch.id)} (${dispatch.agent}) — ${status}`);
|
|
607
|
+
lines.push(`Task: ${title}`);
|
|
608
|
+
lines.push(`Model: ${formatModel(dispatch)}`);
|
|
609
|
+
if (dispatch.error) lines.push(`Error: ${dispatch.error}`);
|
|
610
|
+
if (dispatch.status === "timeout") {
|
|
611
|
+
lines.push(`Timeout: stopped after ${age(dispatch.startedAt, Date.parse(dispatch.endedAt ?? new Date().toISOString()))}${dispatch.lastToolCall ? ` (last tool: ${dispatch.lastToolCall})` : ""}.`);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const usage = dispatch.usage;
|
|
615
|
+
lines.push(
|
|
616
|
+
`Usage: ${usage.turns} turns · ${usage.toolCalls} tools · ↑${formatTokens(usage.input)} ↓${formatTokens(
|
|
617
|
+
usage.output,
|
|
618
|
+
)} · ${formatCost(usage.cost)}`,
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
const text = (body ?? dispatch.summary ?? "").trim();
|
|
622
|
+
if (text) {
|
|
623
|
+
lines.push("");
|
|
624
|
+
lines.push(text.length > 4000 ? `${text.slice(0, 4000)}\n…[truncated]` : text);
|
|
625
|
+
}
|
|
626
|
+
lines.push("");
|
|
627
|
+
lines.push(
|
|
628
|
+
`Next: \`orchestra_read\` for the full log, \`orchestra_send\` to reply (reopens a settled worker), \`orchestra\` action=worker_list for the run.`,
|
|
629
|
+
);
|
|
630
|
+
return lines.join("\n");
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export function questionReport(gate: Gate): string {
|
|
634
|
+
const lines = [`? Worker ${shortId(gate.dispatchId)} is blocked on a question`, "", gate.question];
|
|
635
|
+
if (gate.options?.length) {
|
|
636
|
+
lines.push("", `Options: ${gate.options.join(" | ")}`);
|
|
637
|
+
}
|
|
638
|
+
lines.push("", `Answer: \`orchestra_answer\` questionId=${gate.id} answer="..."`);
|
|
639
|
+
return lines.join("\n");
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export function runSummary(state: OrchestraState, run: Run): string {
|
|
643
|
+
const tasks = listTasks(state, run.id);
|
|
644
|
+
const dispatches = listDispatches(state, run.id);
|
|
645
|
+
const usage = totalUsage(state, run.id);
|
|
646
|
+
const running = dispatches.filter((d) => isLiveStatus(d.status)).length;
|
|
647
|
+
const done = tasks.filter((t) => effectiveTask(state, t).status === "done").length;
|
|
648
|
+
const failed = tasks.filter((t) => {
|
|
649
|
+
const status = effectiveTask(state, t).status;
|
|
650
|
+
return status === "failed" || status === "timeout";
|
|
651
|
+
}).length;
|
|
652
|
+
return [
|
|
653
|
+
`run ${run.id} "${run.name}" — ${run.status}`,
|
|
654
|
+
run.goal ? `goal: ${run.goal}` : undefined,
|
|
655
|
+
`tasks: ${tasks.length} (${done} done, ${failed} failed) · workers: ${dispatches.length} (${running} active)`,
|
|
656
|
+
`usage: ${usage.turns} turns · ↑${formatTokens(usage.input)} ↓${formatTokens(usage.output)} · ${formatCost(usage.cost)}${
|
|
657
|
+
run.budgetUsd ? ` of ${formatCost(run.budgetUsd)}` : ""
|
|
658
|
+
}`,
|
|
659
|
+
run.budgetPaused ? `⏸ budget paused — ${run.budgetNote ?? "ceiling reached"}` : run.budgetNote,
|
|
660
|
+
]
|
|
661
|
+
.filter(Boolean)
|
|
662
|
+
.join("\n");
|
|
663
|
+
}
|