pi-legion 0.16.0 → 2.0.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/CONTRIBUTING.md +58 -0
- package/README.md +313 -640
- package/agents/advisor.md +7 -5
- package/agents/centurion.md +53 -0
- package/agents/designer.md +33 -33
- package/agents/planner.md +1 -2
- package/agents/prototype.md +2 -2
- package/agents/reviewer.md +67 -31
- package/agents/scout.md +6 -2
- package/agents/worker.md +12 -3
- package/docs/contracts/esc-abort-v1.md +131 -0
- package/docs/contracts/esc-cascade-v2.md +100 -0
- package/docs/design.md +44 -25
- package/docs/legion-v2-contract.md +461 -0
- package/docs/legion-v2-delivery.md +62 -0
- package/docs/retire-hardening-contract.md +125 -0
- package/docs/subagents-teams.md +76 -0
- package/docs/tui-viewer-contract.md +276 -0
- package/docs/vexillatio-contract.md +162 -0
- package/index.ts +1462 -286
- package/package.json +12 -9
- package/skills/pi-legion/SKILL.md +74 -96
- package/src/agents/agents.ts +6 -11
- package/src/agents/overrides.ts +2 -1
- package/src/agents/paths.ts +1 -1
- package/src/agents/settings.ts +268 -20
- package/src/api.ts +47 -48
- package/src/{herdr → backends/legacy}/client.ts +14 -69
- package/src/{shared → backends/legacy}/cursor-chat.ts +2 -2
- package/src/{herdr → backends/legacy}/runner.ts +22 -9
- package/src/backends/legacy/types.ts +138 -0
- package/src/extension/auto-doctor.ts +144 -0
- package/src/extension/budget.ts +2 -2
- package/src/extension/child-guard.ts +214 -201
- package/src/extension/event-bus.ts +263 -0
- package/src/extension/legion-commands.ts +719 -0
- package/src/extension/legion-doctor.ts +888 -0
- package/src/extension/legion-stop.ts +150 -0
- package/src/extension/legion-tools.ts +816 -0
- package/src/extension/notify.ts +24 -19
- package/src/extension/playbook.ts +20 -89
- package/src/extension/recycle.ts +2 -7
- package/src/extension/runtime.ts +655 -105
- package/src/extension/scout-guard.ts +483 -0
- package/src/extension/slash.ts +89 -438
- package/src/extension/spawn-hints.ts +79 -0
- package/src/extension/summary.ts +17 -58
- package/src/legion/budget.ts +165 -0
- package/src/legion/db.ts +242 -0
- package/src/legion/events.ts +150 -0
- package/src/legion/mail.ts +412 -0
- package/src/legion/nodes.ts +461 -0
- package/src/legion/rebuild.ts +533 -0
- package/src/legion/state-machine.ts +313 -0
- package/src/legion/usage.ts +101 -0
- package/src/runs/acceptance.ts +82 -25
- package/src/runs/args.ts +51 -5
- package/src/runs/hygiene.ts +335 -0
- package/src/runs/orchestrator.ts +790 -1683
- package/src/runs/owner.ts +61 -0
- package/src/runs/provision.ts +135 -0
- package/src/runs/store.ts +44 -17
- package/src/runs/verify-queue.ts +195 -0
- package/src/shared/name.ts +29 -0
- package/src/shared/output-cap.ts +57 -0
- package/src/shared/progress.ts +57 -135
- package/src/shared/session.ts +243 -171
- package/src/shared/types.ts +107 -208
- package/src/supervisor/child-memory.ts +140 -0
- package/src/supervisor/proc-tree.ts +103 -0
- package/src/supervisor/rpc-supervisor.ts +1033 -0
- package/src/supervisor/types.ts +153 -0
- package/src/supervisor/ui-proxy.ts +226 -0
- package/src/tui/chat-items.ts +330 -0
- package/src/tui/live-overlay.ts +874 -0
- package/src/tui/status.ts +553 -27
- package/src/tui/tree-view.ts +808 -0
- package/src/tui/viz/canvas.ts +119 -0
- package/src/tui/viz/layout.ts +111 -0
- package/src/tui/viz/model.ts +123 -0
- package/src/tui/viz/palette.ts +188 -0
- package/src/tui/viz/panels.ts +530 -0
- package/src/tui/viz/railway.ts +313 -0
- package/src/tui/viz/rate.ts +211 -0
- package/src/tui/viz/view.ts +228 -0
- package/src/extension/parent-label.ts +0 -115
- package/src/profiles/classify.ts +0 -375
- package/src/profiles/profiles.ts +0 -730
- package/src/runs/layout.ts +0 -258
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/** Panel renderers for the /legion full view. All drawing is clipped to its rect. */
|
|
2
|
+
|
|
3
|
+
import { Canvas, ellipsize } from "./canvas.ts";
|
|
4
|
+
import type { Rect } from "./layout.ts";
|
|
5
|
+
import { formatCount, formatDuration, isAgent, type VizNode } from "./model.ts";
|
|
6
|
+
import { nodeToken, roleColor } from "./palette.ts";
|
|
7
|
+
import type { RateSampler } from "./rate.ts";
|
|
8
|
+
|
|
9
|
+
export type Pane = "galaxy" | "chart" | "log";
|
|
10
|
+
|
|
11
|
+
export interface ViewData {
|
|
12
|
+
nodes: readonly VizNode[];
|
|
13
|
+
sampler: RateSampler;
|
|
14
|
+
now: number;
|
|
15
|
+
/** Shared output tok/s (ΔtokensOut / ΔgenerationMs over trailing 60s). */
|
|
16
|
+
rate: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function frame(cv: Canvas, r: Rect, title: string, focused: boolean, hint?: string): Rect {
|
|
20
|
+
if (r.w < 4 || r.h < 3) return { x: r.x, y: r.y, w: Math.max(0, r.w), h: Math.max(0, r.h) };
|
|
21
|
+
const col = focused ? "borderAccent" : "borderMuted";
|
|
22
|
+
const [tl, tr, bl, br, hz, vt] = focused ? ["┏", "┓", "┗", "┛", "━", "┃"] : ["╭", "╮", "╰", "╯", "─", "│"];
|
|
23
|
+
const maxX = r.x + r.w;
|
|
24
|
+
cv.text(r.x, r.y, tl + hz.repeat(r.w - 2) + tr, col, { maxX });
|
|
25
|
+
cv.text(r.x, r.y + r.h - 1, bl + hz.repeat(r.w - 2) + br, col, { maxX });
|
|
26
|
+
for (let j = 1; j < r.h - 1; j++) {
|
|
27
|
+
cv.set(r.x, r.y + j, vt, col);
|
|
28
|
+
cv.set(r.x + r.w - 1, r.y + j, vt, col);
|
|
29
|
+
}
|
|
30
|
+
const titleText = ` ${ellipsize(title, Math.max(0, r.w - 6))} `;
|
|
31
|
+
const after = cv.text(r.x + 2, r.y, titleText, focused ? "borderAccent" : "accent", { bold: true, maxX: maxX - 1 });
|
|
32
|
+
if (hint) {
|
|
33
|
+
const h = ` ${hint} `;
|
|
34
|
+
const hx = r.x + r.w - h.length - 2;
|
|
35
|
+
if (hx > after + 1) cv.text(hx, r.y, h, "dim", { maxX: maxX - 1 });
|
|
36
|
+
}
|
|
37
|
+
return { x: r.x + 1, y: r.y + 1, w: r.w - 2, h: r.h - 2 };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function childrenMap(nodes: readonly VizNode[]): Map<string | null, VizNode[]> {
|
|
41
|
+
const map = new Map<string | null, VizNode[]>();
|
|
42
|
+
for (const node of nodes) {
|
|
43
|
+
const key = node.parentId;
|
|
44
|
+
const list = map.get(key) ?? [];
|
|
45
|
+
list.push(node);
|
|
46
|
+
map.set(key, list);
|
|
47
|
+
}
|
|
48
|
+
for (const list of map.values()) list.sort((a, b) => a.startedAt - b.startedAt || a.name.localeCompare(b.name));
|
|
49
|
+
return map;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Depth-first order (roots first), used for keyboard navigation. */
|
|
53
|
+
export function dfsOrder(nodes: readonly VizNode[]): VizNode[] {
|
|
54
|
+
const kids = childrenMap(nodes);
|
|
55
|
+
const out: VizNode[] = [];
|
|
56
|
+
const visit = (node: VizNode, guard: Set<string>): void => {
|
|
57
|
+
if (guard.has(node.id)) return;
|
|
58
|
+
guard.add(node.id);
|
|
59
|
+
out.push(node);
|
|
60
|
+
for (const child of kids.get(node.id) ?? []) visit(child, guard);
|
|
61
|
+
};
|
|
62
|
+
const guard = new Set<string>();
|
|
63
|
+
for (const root of kids.get(null) ?? []) visit(root, guard);
|
|
64
|
+
for (const node of nodes) if (!guard.has(node.id)) visit(node, guard);
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---------------------------------------------------------------- galaxy
|
|
69
|
+
|
|
70
|
+
const BRAILLE_BITS = [[0x1, 0x8], [0x2, 0x10], [0x4, 0x20], [0x40, 0x80]] as const;
|
|
71
|
+
|
|
72
|
+
export function drawGalaxy(cv: Canvas, r: Rect, data: ViewData, selectedId: string | undefined): void {
|
|
73
|
+
if (r.w < 6 || r.h < 3) return;
|
|
74
|
+
const legendRows = 1;
|
|
75
|
+
const CW = r.w;
|
|
76
|
+
const CH = r.h - legendRows;
|
|
77
|
+
const PW = CW * 2;
|
|
78
|
+
const PH = CH * 4;
|
|
79
|
+
const px = Array.from({ length: CH }, () => Array.from({ length: CW }, () => ({ m: 0, col: "", z: -1 })));
|
|
80
|
+
const dot = (x: number, y: number, col: string, z: number): void => {
|
|
81
|
+
const ix = Math.round(x);
|
|
82
|
+
const iy = Math.round(y);
|
|
83
|
+
if (ix < 0 || iy < 0 || ix >= PW || iy >= PH) return;
|
|
84
|
+
const cell = px[iy >> 2]![ix >> 1]!;
|
|
85
|
+
cell.m |= BRAILLE_BITS[iy & 3]![ix & 1]!;
|
|
86
|
+
if (z >= cell.z) {
|
|
87
|
+
cell.z = z;
|
|
88
|
+
cell.col = col;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
let seed = 99;
|
|
92
|
+
const rnd = (): number => (seed = (seed * 16807) % 2147483647) / 2147483647;
|
|
93
|
+
for (let i = 0; i < (CW * CH) / 12; i++) dot(rnd() * PW, rnd() * PH, "borderMuted", 0);
|
|
94
|
+
|
|
95
|
+
const nodes = data.nodes;
|
|
96
|
+
if (!nodes.some(isAgent)) {
|
|
97
|
+
const msg = "no subagents yet";
|
|
98
|
+
cv.text(r.x + Math.max(0, Math.floor((r.w - msg.length) / 2)), r.y + Math.floor(r.h / 2) + 2, ellipsize(msg, r.w), "dim");
|
|
99
|
+
}
|
|
100
|
+
const kids = childrenMap(nodes);
|
|
101
|
+
const roots = kids.get(null) ?? [];
|
|
102
|
+
const virtualRoot: VizNode | undefined = roots.length === 1 ? undefined : {
|
|
103
|
+
id: "\u0000center", name: "legion", role: "centurion", parentId: null, depth: 0, status: "running", startedAt: data.now, tokens: 0, tokensOut: 0, generationMs: 0,
|
|
104
|
+
};
|
|
105
|
+
const childrenOf = (node: VizNode): VizNode[] => node === virtualRoot ? roots : kids.get(node.id) ?? [];
|
|
106
|
+
const center = virtualRoot ?? roots[0]!;
|
|
107
|
+
if (!center) return;
|
|
108
|
+
const leafMemo = new Map<string, number>();
|
|
109
|
+
const leaves = (node: VizNode, guard = new Set<string>()): number => {
|
|
110
|
+
if (leafMemo.has(node.id)) return leafMemo.get(node.id)!;
|
|
111
|
+
if (guard.has(node.id)) return 1;
|
|
112
|
+
guard.add(node.id);
|
|
113
|
+
const children = childrenOf(node);
|
|
114
|
+
const value = children.length ? children.reduce((sum, child) => sum + leaves(child, guard), 0) : 1;
|
|
115
|
+
leafMemo.set(node.id, value);
|
|
116
|
+
return value;
|
|
117
|
+
};
|
|
118
|
+
let maxRel = 1;
|
|
119
|
+
const rel = new Map<string, number>();
|
|
120
|
+
(function depth(node: VizNode, d: number, guard: Set<string>): void {
|
|
121
|
+
if (guard.has(node.id)) return;
|
|
122
|
+
guard.add(node.id);
|
|
123
|
+
rel.set(node.id, d);
|
|
124
|
+
maxRel = Math.max(maxRel, d);
|
|
125
|
+
for (const child of childrenOf(node)) depth(child, d + 1, guard);
|
|
126
|
+
})(center, 0, new Set());
|
|
127
|
+
const cx = PW / 2;
|
|
128
|
+
const cy = PH / 2;
|
|
129
|
+
const RY = Math.max(2, PH / 2 - 3);
|
|
130
|
+
const RX = Math.max(2, PW / 2 - 6);
|
|
131
|
+
const pos = new Map<string, [number, number]>();
|
|
132
|
+
(function lay(node: VizNode, a0: number, a1: number, guard: Set<string>): void {
|
|
133
|
+
if (guard.has(node.id)) return;
|
|
134
|
+
guard.add(node.id);
|
|
135
|
+
const a = (a0 + a1) / 2;
|
|
136
|
+
const d = rel.get(node.id) ?? 0;
|
|
137
|
+
const k = Math.pow(d / maxRel, 0.85);
|
|
138
|
+
pos.set(node.id, [cx + Math.cos(a) * RX * k, cy + Math.sin(a) * RY * k]);
|
|
139
|
+
let start = a0;
|
|
140
|
+
const total = leaves(node);
|
|
141
|
+
for (const child of childrenOf(node)) {
|
|
142
|
+
const span = ((a1 - a0) * leaves(child)) / total;
|
|
143
|
+
lay(child, start, start + span, guard);
|
|
144
|
+
start += span;
|
|
145
|
+
}
|
|
146
|
+
})(center, -Math.PI / 2, Math.PI * 1.5, new Set());
|
|
147
|
+
|
|
148
|
+
for (let d = 1; d <= maxRel; d++) {
|
|
149
|
+
const k = Math.pow(d / maxRel, 0.85);
|
|
150
|
+
for (let a = 0; a < Math.PI * 2; a += 0.02) if (Math.floor(a * 60) % 4 === 0) dot(cx + Math.cos(a) * RX * k, cy + Math.sin(a) * RY * k, "borderMuted", 0);
|
|
151
|
+
}
|
|
152
|
+
const all = virtualRoot ? [virtualRoot, ...nodes] : [...nodes];
|
|
153
|
+
for (const node of all) {
|
|
154
|
+
const parentId = node === virtualRoot ? undefined : node.parentId ?? (virtualRoot ? virtualRoot.id : undefined);
|
|
155
|
+
if (!parentId) continue;
|
|
156
|
+
const a = pos.get(parentId);
|
|
157
|
+
const b = pos.get(node.id);
|
|
158
|
+
if (!a || !b) continue;
|
|
159
|
+
const dist = Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
160
|
+
const col = node.status === "running" ? roleColor(node.role) : node.status === "failed" ? "error" : "borderMuted";
|
|
161
|
+
for (let t = 0; t < dist; t += 1.7) dot(a[0] + ((b[0] - a[0]) * t) / dist, a[1] + ((b[1] - a[1]) * t) / dist, col, 1);
|
|
162
|
+
}
|
|
163
|
+
const maxTokens = Math.max(1, ...nodes.map((node) => node.tokens));
|
|
164
|
+
const radii = new Map<string, number>();
|
|
165
|
+
for (const node of all) {
|
|
166
|
+
const p = pos.get(node.id);
|
|
167
|
+
if (!p) continue;
|
|
168
|
+
const [x, y] = p;
|
|
169
|
+
const isCenter = node === center;
|
|
170
|
+
const col = isCenter ? roleColor(node.role) : nodeToken(node);
|
|
171
|
+
let rad = isCenter ? Math.max(2, Math.min(5, PH / 14)) : Math.max(node.status === "failed" ? 1.5 : 0.8, 0.8 + Math.sqrt(node.tokens / maxTokens) * Math.min(4, PH / 16));
|
|
172
|
+
radii.set(node.id, rad);
|
|
173
|
+
if (node.status === "running" && !isCenter) {
|
|
174
|
+
const act = Math.min(1, data.sampler.nodeRate(node.id, data.now) / 250);
|
|
175
|
+
for (let i = 0; i < 12 + rad * 6; i++) {
|
|
176
|
+
const a = rnd() * Math.PI * 2;
|
|
177
|
+
const d = rad + 1.5 + rnd() * (2 + act * 3);
|
|
178
|
+
dot(x + Math.cos(a) * d, y + Math.sin(a) * d, col, 2);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (isCenter) {
|
|
182
|
+
for (let i = 0; i < 60; i++) {
|
|
183
|
+
const a = rnd() * Math.PI * 2;
|
|
184
|
+
const d = rad + 1.5 + Math.pow(rnd(), 2) * 6;
|
|
185
|
+
dot(x + Math.cos(a) * d * 1.6, y + Math.sin(a) * d, col, 2);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const xs = isCenter ? 1.6 : 1;
|
|
189
|
+
for (let yy = -Math.ceil(rad); yy <= Math.ceil(rad); yy++) {
|
|
190
|
+
for (let xx = -Math.ceil(rad * xs); xx <= Math.ceil(rad * xs); xx++) {
|
|
191
|
+
if (Math.hypot(xx / xs, yy) <= rad) dot(x + xx, y + yy, col, 3);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (node.id === selectedId) {
|
|
195
|
+
rad += 3;
|
|
196
|
+
for (let a = 0; a < Math.PI * 2; a += 0.12) dot(x + Math.cos(a) * rad * xs, y + Math.sin(a) * rad, "text", 5);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (let j = 0; j < CH; j++) {
|
|
200
|
+
for (let i = 0; i < CW; i++) {
|
|
201
|
+
const cell = px[j]![i]!;
|
|
202
|
+
if (cell.m) cv.set(r.x + i, r.y + j, String.fromCharCode(0x2800 + cell.m), cell.col);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Labels: selected, center, depth-1 live, failed, and the heaviest running agents.
|
|
206
|
+
const running = nodes.filter((node) => node.status === "running" && isAgent(node)).map((node) => node.tokens).sort((a, b) => b - a);
|
|
207
|
+
const heavy = running[Math.min(running.length - 1, 4)] ?? Number.POSITIVE_INFINITY;
|
|
208
|
+
const candidates = all.filter((node) =>
|
|
209
|
+
node.id === selectedId || node === center || node.status === "failed" ||
|
|
210
|
+
((rel.get(node.id) ?? 9) <= 1 && node.status !== "retired") ||
|
|
211
|
+
(node.status === "running" && node.tokens >= heavy),
|
|
212
|
+
).sort((a, b) =>
|
|
213
|
+
Number(b.id === selectedId) - Number(a.id === selectedId) ||
|
|
214
|
+
Number(b === center) - Number(a === center) ||
|
|
215
|
+
b.tokens - a.tokens,
|
|
216
|
+
);
|
|
217
|
+
const occupied: Array<{ x: number; y: number; w: number }> = [];
|
|
218
|
+
const free = (x: number, y: number, w: number): boolean =>
|
|
219
|
+
x >= r.x && x + w <= r.x + r.w && y >= r.y && y < r.y + CH &&
|
|
220
|
+
!occupied.some((o) => o.y === y && x < o.x + o.w + 1 && o.x < x + w + 1);
|
|
221
|
+
for (const node of candidates) {
|
|
222
|
+
const p = pos.get(node.id);
|
|
223
|
+
if (!p) continue;
|
|
224
|
+
const lx = r.x + Math.round(p[0] / 2);
|
|
225
|
+
const ly = r.y + Math.round(p[1] / 4);
|
|
226
|
+
const label = ellipsize(node.name + (node.status === "failed" ? " ✗" : ""), Math.max(4, Math.floor(r.w / 3)));
|
|
227
|
+
const len = Array.from(label).length;
|
|
228
|
+
const rw = Math.ceil((radii.get(node.id) ?? 1) * (node === center ? 1.6 : 1) / 2) + (node.id === selectedId ? 3 : 1);
|
|
229
|
+
const right = p[0] >= cx - 4;
|
|
230
|
+
let placed = false;
|
|
231
|
+
for (const dy of [0, -1, 1, -2, 2]) {
|
|
232
|
+
for (const side of right ? [1, -1] : [-1, 1]) {
|
|
233
|
+
if (placed) break;
|
|
234
|
+
const sx = side > 0 ? lx + rw : lx - rw - len;
|
|
235
|
+
if (!free(sx, ly + dy, len)) continue;
|
|
236
|
+
const selected = node.id === selectedId;
|
|
237
|
+
cv.text(sx, ly + dy, label, node === center ? roleColor(node.role) : nodeToken(node), { bold: true, inverse: selected });
|
|
238
|
+
occupied.push({ x: sx, y: ly + dy, w: len });
|
|
239
|
+
placed = true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// Legend: roles that actually appear (dynamic), then status hints.
|
|
244
|
+
const ly = r.y + r.h - 1;
|
|
245
|
+
let lx = r.x + 1;
|
|
246
|
+
const roles: string[] = [];
|
|
247
|
+
for (const node of dfsOrder(nodes)) if (isAgent(node) && !roles.includes(node.role)) roles.push(node.role);
|
|
248
|
+
for (const role of roles) {
|
|
249
|
+
const w = Array.from(role).length + 4;
|
|
250
|
+
if (lx + w > r.x + r.w - 1) break;
|
|
251
|
+
cv.set(lx, ly, "⣿", roleColor(role));
|
|
252
|
+
cv.text(lx + 2, ly, role, "dim");
|
|
253
|
+
lx += w;
|
|
254
|
+
}
|
|
255
|
+
for (const [label, col] of [["✗ failed", "error"], ["⣿ retired", "dim"]] as const) {
|
|
256
|
+
if (lx + label.length + 1 > r.x + r.w - 1) break;
|
|
257
|
+
cv.text(lx, ly, label, col);
|
|
258
|
+
lx += label.length + 2;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ---------------------------------------------------------------- tok/s chart
|
|
263
|
+
|
|
264
|
+
export interface ChartStats {
|
|
265
|
+
now: number;
|
|
266
|
+
avg: number;
|
|
267
|
+
peak: number;
|
|
268
|
+
peakAt: number;
|
|
269
|
+
total: number;
|
|
270
|
+
from: number;
|
|
271
|
+
byRole: Array<{ role: string; tokens: number }>;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function chartWindow(data: ViewData): { from: number; to: number } {
|
|
275
|
+
const starts = data.nodes.filter(isAgent).map((node) => node.startedAt);
|
|
276
|
+
const from = Math.min(data.sampler.startedAt ?? data.now, ...(starts.length ? starts : [data.now]));
|
|
277
|
+
return { from: Math.min(from, data.now - 60_000), to: data.now };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function computeStats(data: ViewData, buckets: number): ChartStats {
|
|
281
|
+
const { from, to } = chartWindow(data);
|
|
282
|
+
const series = data.sampler.buckets(from, to, buckets);
|
|
283
|
+
let peak = 0;
|
|
284
|
+
let peakIndex = -1;
|
|
285
|
+
series.total.forEach((value, i) => {
|
|
286
|
+
if (value > peak) {
|
|
287
|
+
peak = value;
|
|
288
|
+
peakIndex = i;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
// "now" is the shared header definition, never the last bucket.
|
|
292
|
+
peak = Math.max(peak, data.rate);
|
|
293
|
+
const peakAt = peakIndex >= 0 && peak > data.rate ? series.times[peakIndex]! : to;
|
|
294
|
+
const roleTotals = new Map<string, number>();
|
|
295
|
+
let total = 0;
|
|
296
|
+
for (const node of data.nodes) {
|
|
297
|
+
if (!isAgent(node)) continue;
|
|
298
|
+
total += node.tokens;
|
|
299
|
+
roleTotals.set(node.role, (roleTotals.get(node.role) ?? 0) + node.tokens);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
now: data.rate,
|
|
303
|
+
// Same definition as header "now", averaged over the chart window.
|
|
304
|
+
avg: data.sampler.totalRate(to, Math.max(1, to - from)),
|
|
305
|
+
peak,
|
|
306
|
+
peakAt,
|
|
307
|
+
total,
|
|
308
|
+
from,
|
|
309
|
+
byRole: [...roleTotals.entries()].map(([role, tokens]) => ({ role, tokens })).sort((a, b) => b.tokens - a.tokens),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const PARTIAL = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
314
|
+
|
|
315
|
+
export function drawChart(cv: Canvas, r: Rect, data: ViewData, selected: VizNode | undefined, wideStats: boolean): void {
|
|
316
|
+
if (r.w < 12 || r.h < 4) return;
|
|
317
|
+
const statsW = wideStats && r.w >= 64 ? 28 : 0;
|
|
318
|
+
const statsBelow = statsW === 0 ? Math.min(selected ? 4 : 3, Math.max(0, r.h - 6)) : 0;
|
|
319
|
+
const axisW = 6;
|
|
320
|
+
const plotX = r.x + axisW;
|
|
321
|
+
// With side stats, keep room for the "◂ now" label so it never runs into the stats column.
|
|
322
|
+
const plotW = Math.max(4, r.w - axisW - statsW - (statsW ? 9 : 0) - 1);
|
|
323
|
+
const plotTop = r.y + 1;
|
|
324
|
+
const plotH = Math.max(1, r.h - 2 - statsBelow);
|
|
325
|
+
const axisY = plotTop + plotH;
|
|
326
|
+
const { from, to } = chartWindow(data);
|
|
327
|
+
const series = data.sampler.buckets(from, to, plotW);
|
|
328
|
+
const stats = computeStats(data, plotW);
|
|
329
|
+
const max = Math.max(1, stats.peak * 1.15);
|
|
330
|
+
const rowsFor = (v: number): number => (v / max) * plotH;
|
|
331
|
+
const roles = stats.byRole.map((entry) => entry.role);
|
|
332
|
+
for (const role of series.byRole.keys()) if (!roles.includes(role)) roles.push(role);
|
|
333
|
+
|
|
334
|
+
for (let i = 0; i < plotW; i++) {
|
|
335
|
+
let acc = 0;
|
|
336
|
+
const bands: Array<{ top: number; role: string }> = [];
|
|
337
|
+
for (const role of roles) {
|
|
338
|
+
const value = series.byRole.get(role)?.[i] ?? 0;
|
|
339
|
+
if (value <= 0) continue;
|
|
340
|
+
acc += value;
|
|
341
|
+
bands.push({ top: rowsFor(acc), role });
|
|
342
|
+
}
|
|
343
|
+
const top = rowsFor(acc);
|
|
344
|
+
for (let j = 0; j < plotH; j++) {
|
|
345
|
+
const center = j + 0.5;
|
|
346
|
+
const y = axisY - 1 - j;
|
|
347
|
+
if (top >= j + 1) {
|
|
348
|
+
const band = bands.find((b) => b.top >= center) ?? bands[bands.length - 1];
|
|
349
|
+
if (band) cv.set(plotX + i, y, "█", roleColor(band.role));
|
|
350
|
+
} else if (top > j) {
|
|
351
|
+
const eighth = Math.round((top - j) * 8);
|
|
352
|
+
if (eighth > 0) cv.set(plotX + i, y, PARTIAL[eighth]!, roleColor(bands[bands.length - 1]?.role ?? "worker"));
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// Peak marker: dotted rule at the peak height plus a label in the top row.
|
|
358
|
+
if (stats.peak > 0) {
|
|
359
|
+
const peakRow = Math.min(plotH - 1, Math.floor(rowsFor(stats.peak)));
|
|
360
|
+
const y = axisY - 1 - peakRow;
|
|
361
|
+
for (let i = 0; i < plotW; i++) if (cv.get(plotX + i, y)?.ch === " ") cv.set(plotX + i, y, "┄", "dim");
|
|
362
|
+
const label = `peak ${formatCount(stats.peak)} @ ${formatDuration(stats.peakAt - from)}`;
|
|
363
|
+
cv.text(Math.max(plotX, plotX + plotW - label.length), r.y, label, "text", { bold: true, maxX: plotX + plotW });
|
|
364
|
+
}
|
|
365
|
+
// Selected agent's own rate line.
|
|
366
|
+
if (selected && isAgent(selected)) {
|
|
367
|
+
const line = data.sampler.nodeBuckets(selected.id, from, to, plotW);
|
|
368
|
+
line.forEach((value, i) => {
|
|
369
|
+
if (value <= 0) return;
|
|
370
|
+
const row = Math.min(plotH - 1, Math.floor(rowsFor(value)));
|
|
371
|
+
cv.set(plotX + i, axisY - 1 - row, "━", "text", { bold: true });
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
// Y axis.
|
|
375
|
+
for (let j = 0; j < plotH; j++) cv.set(plotX - 1, axisY - 1 - j, "│", "borderMuted");
|
|
376
|
+
const yLabel = (v: number, y: number): void => {
|
|
377
|
+
cv.text(r.x, y, formatCount(v).padStart(axisW - 2), "dim", { maxX: plotX - 1 });
|
|
378
|
+
};
|
|
379
|
+
if (stats.peak > 0) yLabel(max, plotTop);
|
|
380
|
+
if (plotH >= 5 && max >= 20) yLabel(max / 2, axisY - 1 - Math.floor(plotH / 2));
|
|
381
|
+
yLabel(0, axisY - 1);
|
|
382
|
+
// X axis with "now" at the right edge.
|
|
383
|
+
cv.text(plotX - 1, axisY, "└" + "─".repeat(plotW), "borderMuted", { maxX: plotX + plotW });
|
|
384
|
+
cv.text(plotX, axisY, "0m", "dim");
|
|
385
|
+
const mid = formatDuration((to - from) / 2);
|
|
386
|
+
if (plotW > 24) cv.text(plotX + Math.floor(plotW / 2) - Math.floor(mid.length / 2), axisY, mid, "dim");
|
|
387
|
+
cv.text(plotX + plotW - 3, axisY, "now", "text", { bold: true });
|
|
388
|
+
const nowLabel = `◂ ${formatCount(stats.now)}`;
|
|
389
|
+
if (statsW === 0) cv.text(r.x, r.y, `now ${formatCount(stats.now)} tok/s`, "accent", { bold: true, maxX: Math.max(r.x, plotX + plotW - 22) });
|
|
390
|
+
else cv.text(plotX + plotW + 1, axisY - 1 - Math.min(plotH - 1, Math.floor(rowsFor(stats.now))), nowLabel, "text", { maxX: r.x + r.w - statsW - 1 });
|
|
391
|
+
|
|
392
|
+
const selLines = (): Array<[string, string]> => {
|
|
393
|
+
if (!selected) return [];
|
|
394
|
+
const rate = data.sampler.nodeRate(selected.id, data.now);
|
|
395
|
+
const share = stats.total > 0 ? (selected.tokens / stats.total) * 100 : 0;
|
|
396
|
+
return [
|
|
397
|
+
[`▸ ${selected.name}`, roleColor(selected.role)],
|
|
398
|
+
[`${formatCount(rate)} tok/s now · ${formatCount(selected.tokens)} tok`, "text"],
|
|
399
|
+
[`${share.toFixed(1)}% of run · ━ bright line`, "dim"],
|
|
400
|
+
];
|
|
401
|
+
};
|
|
402
|
+
if (statsW) {
|
|
403
|
+
const sx = r.x + r.w - statsW;
|
|
404
|
+
const rows: Array<[string, string, string]> = [
|
|
405
|
+
["now", `${formatCount(stats.now)} tok/s`, "text"],
|
|
406
|
+
["avg", `${formatCount(stats.avg)} tok/s`, "text"],
|
|
407
|
+
["peak", stats.peak > 0 ? `${formatCount(stats.peak)} @ ${formatDuration(stats.peakAt - from)}` : "—", "text"],
|
|
408
|
+
["total", `${formatCount(stats.total)} tok`, "text"],
|
|
409
|
+
];
|
|
410
|
+
let y = r.y;
|
|
411
|
+
for (const [k, v, col] of rows) {
|
|
412
|
+
if (y >= r.y + r.h) break;
|
|
413
|
+
cv.text(sx, y, k, "dim");
|
|
414
|
+
cv.text(sx + 6, y, v, col, { bold: true, maxX: r.x + r.w });
|
|
415
|
+
y++;
|
|
416
|
+
}
|
|
417
|
+
y++;
|
|
418
|
+
const roomForRoles = r.h - (y - r.y) - (selected ? 4 : 0);
|
|
419
|
+
const top = stats.byRole[0]?.tokens ?? 1;
|
|
420
|
+
for (const entry of stats.byRole.slice(0, Math.max(0, roomForRoles))) {
|
|
421
|
+
const name = ellipsize(entry.role, 10);
|
|
422
|
+
cv.text(sx, y, name, roleColor(entry.role));
|
|
423
|
+
const barW = Math.max(0, Math.round((entry.tokens / Math.max(1, top)) * 9));
|
|
424
|
+
cv.text(sx + 11, y, "▆".repeat(barW), roleColor(entry.role));
|
|
425
|
+
const t = formatCount(entry.tokens);
|
|
426
|
+
cv.text(r.x + r.w - t.length, y, t, "dim");
|
|
427
|
+
y++;
|
|
428
|
+
}
|
|
429
|
+
const sl = selLines();
|
|
430
|
+
let sy = r.y + r.h - sl.length;
|
|
431
|
+
for (const [text, col] of sl) cv.text(sx, sy++, ellipsize(text, statsW), col, { bold: col !== "dim" });
|
|
432
|
+
} else if (statsBelow > 0) {
|
|
433
|
+
let y = axisY + 1;
|
|
434
|
+
const line1 = `avg ${formatCount(stats.avg)} · peak ${formatCount(stats.peak)} @ ${formatDuration(stats.peakAt - from)} · total ${formatCount(stats.total)}`;
|
|
435
|
+
cv.text(r.x, y++, ellipsize(line1, r.w), "dim");
|
|
436
|
+
if (y < r.y + r.h) {
|
|
437
|
+
let x = r.x;
|
|
438
|
+
for (const entry of stats.byRole) {
|
|
439
|
+
const pct = stats.total > 0 ? Math.round((entry.tokens / stats.total) * 100) : 0;
|
|
440
|
+
const label = `${entry.role} ${pct}%`;
|
|
441
|
+
if (x + label.length + 2 > r.x + r.w) break;
|
|
442
|
+
cv.set(x, y, "■", roleColor(entry.role));
|
|
443
|
+
cv.text(x + 2, y, label, "dim");
|
|
444
|
+
x += label.length + 4;
|
|
445
|
+
}
|
|
446
|
+
y++;
|
|
447
|
+
}
|
|
448
|
+
const sl = selLines();
|
|
449
|
+
if (sl.length && y < r.y + r.h) {
|
|
450
|
+
const [name, col] = sl[0]!;
|
|
451
|
+
const rest = sl[1]![0];
|
|
452
|
+
cv.text(r.x, y, ellipsize(`${name} ${rest}`, r.w), col, { bold: true });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ---------------------------------------------------------------- git-log history
|
|
458
|
+
|
|
459
|
+
export interface LogEvent {
|
|
460
|
+
ts: number;
|
|
461
|
+
kind: "spawn" | "settled" | "failed" | "retired";
|
|
462
|
+
node: VizNode;
|
|
463
|
+
parentName?: string;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export function buildLog(nodes: readonly VizNode[]): LogEvent[] {
|
|
467
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
468
|
+
const out: LogEvent[] = [];
|
|
469
|
+
for (const node of nodes) {
|
|
470
|
+
if (!isAgent(node)) continue;
|
|
471
|
+
const parentName = node.parentId ? byId.get(node.parentId)?.name : undefined;
|
|
472
|
+
out.push({ ts: node.startedAt, kind: "spawn", node, ...(parentName ? { parentName } : {}) });
|
|
473
|
+
if (node.status !== "running" && node.endedAt !== undefined) out.push({ ts: node.endedAt, kind: node.status, node });
|
|
474
|
+
}
|
|
475
|
+
return out.sort((a, b) => b.ts - a.ts || (a.kind === "spawn" ? 1 : -1));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const KIND_GLYPH: Record<LogEvent["kind"], [string, string]> = {
|
|
479
|
+
spawn: ["+", "text"],
|
|
480
|
+
settled: ["✓", "success"],
|
|
481
|
+
failed: ["✗", "error"],
|
|
482
|
+
retired: ["○", "dim"],
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
export function drawLog(
|
|
486
|
+
cv: Canvas,
|
|
487
|
+
r: Rect,
|
|
488
|
+
events: readonly LogEvent[],
|
|
489
|
+
state: { offset: number; cursor: number; focused: boolean; selectedId?: string; origin: number },
|
|
490
|
+
): void {
|
|
491
|
+
if (r.w < 8 || r.h < 2) return;
|
|
492
|
+
const filter = state.selectedId ? events.find((e) => e.node.id === state.selectedId)?.node.name : undefined;
|
|
493
|
+
cv.text(r.x, r.y, ellipsize(`${events.length} events${filter ? ` · highlight: ${filter}` : ""}`, r.w), "dim");
|
|
494
|
+
const rows = r.h - 1;
|
|
495
|
+
const maxDepth = Math.max(1, ...events.map((e) => e.node.depth));
|
|
496
|
+
const graphW = Math.min(8, maxDepth * 2);
|
|
497
|
+
const timeW = 7;
|
|
498
|
+
for (let i = 0; i < rows; i++) {
|
|
499
|
+
const index = state.offset + i;
|
|
500
|
+
const event = events[index];
|
|
501
|
+
if (!event) break;
|
|
502
|
+
const y = r.y + 1 + i;
|
|
503
|
+
const isCursor = state.focused && index === state.cursor;
|
|
504
|
+
const highlight = state.selectedId === event.node.id;
|
|
505
|
+
const dim = state.selectedId !== undefined && !highlight;
|
|
506
|
+
cv.text(r.x, y, formatDuration(event.ts - state.origin).padStart(timeW - 1), "dim");
|
|
507
|
+
const gx = r.x + timeW;
|
|
508
|
+
if (r.w > timeW + graphW + 10) {
|
|
509
|
+
for (let d = 1; d < Math.min(event.node.depth, graphW / 2 + 1); d++) cv.set(gx + (d - 1) * 2, y, "│", "borderMuted");
|
|
510
|
+
const dx = gx + Math.min(event.node.depth - 1, graphW / 2 - 1) * 2;
|
|
511
|
+
cv.set(dx, y, event.kind === "spawn" ? "●" : "┘", event.kind === "spawn" ? roleColor(event.node.role) : "borderMuted");
|
|
512
|
+
}
|
|
513
|
+
const tx = r.w > timeW + graphW + 10 ? gx + graphW + 1 : gx;
|
|
514
|
+
const [glyph, glyphCol] = KIND_GLYPH[event.kind];
|
|
515
|
+
cv.set(tx, y, glyph, glyphCol, { bold: true });
|
|
516
|
+
const nameW = Math.min(16, Math.max(6, Math.floor((r.x + r.w - tx - 2) * 0.45)));
|
|
517
|
+
const name = ellipsize(event.node.name, nameW);
|
|
518
|
+
cv.text(tx + 2, y, name, dim ? "dim" : roleColor(event.node.role), { bold: !dim, inverse: isCursor });
|
|
519
|
+
const detail = event.kind === "spawn"
|
|
520
|
+
? `${event.parentName ?? "root"} → ${event.node.role}`
|
|
521
|
+
: `${event.kind === "settled" ? "done" : event.kind} · ${formatCount(event.node.tokens)} tok`;
|
|
522
|
+
cv.text(tx + 3 + nameW, y, ellipsize(detail, Math.max(0, r.x + r.w - (tx + 3 + nameW))), highlight ? "text" : "dim");
|
|
523
|
+
if (isCursor && highlight) cv.set(r.x + r.w - 1, y, "◂", "accent", { bold: true });
|
|
524
|
+
}
|
|
525
|
+
if (events.length > rows) {
|
|
526
|
+
const thumb = Math.max(1, Math.round((rows / events.length) * rows));
|
|
527
|
+
const pos = Math.round((state.offset / Math.max(1, events.length - rows)) * (rows - thumb));
|
|
528
|
+
for (let i = 0; i < rows; i++) cv.set(r.x + r.w - 1, r.y + 1 + i, i >= pos && i < pos + thumb ? "┃" : "│", i >= pos && i < pos + thumb ? "scrollbarThumb" : "scrollbarTrack");
|
|
529
|
+
}
|
|
530
|
+
}
|