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.
Files changed (89) hide show
  1. package/CONTRIBUTING.md +58 -0
  2. package/README.md +313 -640
  3. package/agents/advisor.md +7 -5
  4. package/agents/centurion.md +53 -0
  5. package/agents/designer.md +33 -33
  6. package/agents/planner.md +1 -2
  7. package/agents/prototype.md +2 -2
  8. package/agents/reviewer.md +67 -31
  9. package/agents/scout.md +6 -2
  10. package/agents/worker.md +12 -3
  11. package/docs/contracts/esc-abort-v1.md +131 -0
  12. package/docs/contracts/esc-cascade-v2.md +100 -0
  13. package/docs/design.md +44 -25
  14. package/docs/legion-v2-contract.md +461 -0
  15. package/docs/legion-v2-delivery.md +62 -0
  16. package/docs/retire-hardening-contract.md +125 -0
  17. package/docs/subagents-teams.md +76 -0
  18. package/docs/tui-viewer-contract.md +276 -0
  19. package/docs/vexillatio-contract.md +162 -0
  20. package/index.ts +1462 -286
  21. package/package.json +12 -9
  22. package/skills/pi-legion/SKILL.md +74 -96
  23. package/src/agents/agents.ts +6 -11
  24. package/src/agents/overrides.ts +2 -1
  25. package/src/agents/paths.ts +1 -1
  26. package/src/agents/settings.ts +268 -20
  27. package/src/api.ts +47 -48
  28. package/src/{herdr → backends/legacy}/client.ts +14 -69
  29. package/src/{shared → backends/legacy}/cursor-chat.ts +2 -2
  30. package/src/{herdr → backends/legacy}/runner.ts +22 -9
  31. package/src/backends/legacy/types.ts +138 -0
  32. package/src/extension/auto-doctor.ts +144 -0
  33. package/src/extension/budget.ts +2 -2
  34. package/src/extension/child-guard.ts +214 -201
  35. package/src/extension/event-bus.ts +263 -0
  36. package/src/extension/legion-commands.ts +719 -0
  37. package/src/extension/legion-doctor.ts +888 -0
  38. package/src/extension/legion-stop.ts +150 -0
  39. package/src/extension/legion-tools.ts +816 -0
  40. package/src/extension/notify.ts +24 -19
  41. package/src/extension/playbook.ts +20 -89
  42. package/src/extension/recycle.ts +2 -7
  43. package/src/extension/runtime.ts +655 -105
  44. package/src/extension/scout-guard.ts +483 -0
  45. package/src/extension/slash.ts +89 -438
  46. package/src/extension/spawn-hints.ts +79 -0
  47. package/src/extension/summary.ts +17 -58
  48. package/src/legion/budget.ts +165 -0
  49. package/src/legion/db.ts +242 -0
  50. package/src/legion/events.ts +150 -0
  51. package/src/legion/mail.ts +412 -0
  52. package/src/legion/nodes.ts +461 -0
  53. package/src/legion/rebuild.ts +533 -0
  54. package/src/legion/state-machine.ts +313 -0
  55. package/src/legion/usage.ts +101 -0
  56. package/src/runs/acceptance.ts +82 -25
  57. package/src/runs/args.ts +51 -5
  58. package/src/runs/hygiene.ts +335 -0
  59. package/src/runs/orchestrator.ts +790 -1683
  60. package/src/runs/owner.ts +61 -0
  61. package/src/runs/provision.ts +135 -0
  62. package/src/runs/store.ts +44 -17
  63. package/src/runs/verify-queue.ts +195 -0
  64. package/src/shared/name.ts +29 -0
  65. package/src/shared/output-cap.ts +57 -0
  66. package/src/shared/progress.ts +57 -135
  67. package/src/shared/session.ts +243 -171
  68. package/src/shared/types.ts +107 -208
  69. package/src/supervisor/child-memory.ts +140 -0
  70. package/src/supervisor/proc-tree.ts +103 -0
  71. package/src/supervisor/rpc-supervisor.ts +1033 -0
  72. package/src/supervisor/types.ts +153 -0
  73. package/src/supervisor/ui-proxy.ts +226 -0
  74. package/src/tui/chat-items.ts +330 -0
  75. package/src/tui/live-overlay.ts +874 -0
  76. package/src/tui/status.ts +553 -27
  77. package/src/tui/tree-view.ts +808 -0
  78. package/src/tui/viz/canvas.ts +119 -0
  79. package/src/tui/viz/layout.ts +111 -0
  80. package/src/tui/viz/model.ts +123 -0
  81. package/src/tui/viz/palette.ts +188 -0
  82. package/src/tui/viz/panels.ts +530 -0
  83. package/src/tui/viz/railway.ts +313 -0
  84. package/src/tui/viz/rate.ts +211 -0
  85. package/src/tui/viz/view.ts +228 -0
  86. package/src/extension/parent-label.ts +0 -115
  87. package/src/profiles/classify.ts +0 -375
  88. package/src/profiles/profiles.ts +0 -730
  89. package/src/runs/layout.ts +0 -258
@@ -1,258 +0,0 @@
1
- /**
2
- * Session-wide layout: same agent type → one tab, each child a pane.
3
- *
4
- * The orchestrator used to key tabs by run id. Two parallel `subagent` tool
5
- * calls each created a run, so two scouts landed in two tabs. This registry
6
- * is process-scoped (the extension holds one) so concurrent launches of the
7
- * same type join the same tab. After a reload, herdr tabs are re-adopted by
8
- * a parent-scoped label (`scout@wA:p1`) so another Pi in the same Space
9
- * cannot join this tab.
10
- *
11
- * Panes inside a type tab are tiled automatically as a 3-column grid
12
- * (fill the first row left-to-right, then wrap down) instead of stacking
13
- * every child vertically off the root pane.
14
- *
15
- * Also coalesces `agent list` and reserves herdr names so two racing launches
16
- * do not both try `scout-0`.
17
- */
18
-
19
- import { makeName } from "../shared/name.ts";
20
-
21
- /**
22
- * Tab label for one agent type.
23
- *
24
- * Same type still shares one tab *inside one parent pane*. A second parent Pi
25
- * in the same Space must not adopt that tab — otherwise its recycle can
26
- * `tab close` the first parent's live children. `owner` is the parent
27
- * `HERDR_PANE_ID` (stable across `/reload` in the same pane).
28
- */
29
- export function typeTabLabel(agentType: string, owner?: string): string {
30
- const tag = sanitizeTabOwner(owner);
31
- return tag ? `${agentType}@${tag}` : agentType;
32
- }
33
-
34
- /** Keep the owner fragment a single herdr-label-safe token. */
35
- export function sanitizeTabOwner(owner: string | undefined): string | undefined {
36
- const cleaned = (owner ?? "")
37
- .trim()
38
- .replace(/\s+/g, "")
39
- .replace(/[^A-Za-z0-9:_-]/g, "")
40
- .slice(0, 32);
41
- return cleaned.length > 0 ? cleaned : undefined;
42
- }
43
-
44
- export interface TypeTab {
45
- tabId: string;
46
- rootPaneId: string;
47
- rootPaneUsed: boolean;
48
- /** Live pane ids in creation order, used to pick the next split target. */
49
- panes: string[];
50
- }
51
-
52
- export interface TypeTabCreateResult {
53
- tabId: string;
54
- rootPaneId: string;
55
- /** True when the root pane already hosts a child (adopted tab). */
56
- rootOccupied?: boolean;
57
- /** Live panes already in the tab (adopted). */
58
- panes?: string[];
59
- }
60
-
61
- export interface TileSplit {
62
- target: string;
63
- direction: "right" | "down";
64
- }
65
-
66
- /**
67
- * Next split for a 3-column grid.
68
- *
69
- * 1 pane → split it right → [A | B]
70
- * 2 panes → split B right → [A | B | C]
71
- * 3 panes → split A down → [A/D | B | C]
72
- * 4 panes → split B down → [A/D | B/E | C]
73
- * 5 panes → split C down → [A/D | B/E | C/F]
74
- *
75
- * Empty `panes` means the caller should occupy the tab root instead.
76
- */
77
- export const TILE_COLUMNS = 3;
78
-
79
- export function tileSplit(panes: readonly string[]): TileSplit | undefined {
80
- if (panes.length === 0) return undefined;
81
- if (panes.length < TILE_COLUMNS) {
82
- const target = panes[panes.length - 1];
83
- return target ? { target, direction: "right" } : undefined;
84
- }
85
- const target = panes[panes.length - TILE_COLUMNS];
86
- return target ? { target, direction: "down" } : undefined;
87
- }
88
-
89
- export interface SessionLayout {
90
- liveNames(fetch: () => Promise<Set<string>>): Promise<Set<string>>;
91
- claimName(agentType: string, blocked: (name: string) => boolean): string;
92
- rememberName(name: string): void;
93
- acquireTypeTab(
94
- agentType: string,
95
- create: () => Promise<TypeTabCreateResult>,
96
- ): Promise<TypeTab>;
97
- /**
98
- * Occupy the type-tab root or split a live pane. Serialized per type so
99
- * two racing launches cannot both split the same target.
100
- */
101
- assignPane(
102
- agentType: string,
103
- split: (plan: TileSplit) => Promise<string>,
104
- ): Promise<{ paneId: string; tabId: string }>;
105
- releasePane(
106
- agentType: string,
107
- paneId: string,
108
- ): { tabId: string | undefined; empty: boolean };
109
- getTypeTab(agentType: string): TypeTab | undefined;
110
- dropTypeTab(agentType: string): void;
111
- }
112
-
113
- export interface SessionLayoutOptions {
114
- now?: () => number;
115
- listTtlMs?: number;
116
- }
117
-
118
- const DEFAULT_LIST_TTL_MS = 1_500;
119
-
120
- export function createSessionLayout(
121
- opts: SessionLayoutOptions = {},
122
- ): SessionLayout {
123
- const now = opts.now ?? Date.now;
124
- const listTtlMs = opts.listTtlMs ?? DEFAULT_LIST_TTL_MS;
125
- const claimed = new Set<string>();
126
- const tabs = new Map<string, TypeTab>();
127
- const pendingTabs = new Map<string, Promise<TypeTab>>();
128
- const paneChains = new Map<string, Promise<unknown>>();
129
- let listCache: { names: Set<string>; at: number } | undefined;
130
- let listPending: Promise<Set<string>> | undefined;
131
-
132
- const enqueue = <T>(key: string, work: () => Promise<T>): Promise<T> => {
133
- const prev = paneChains.get(key) ?? Promise.resolve();
134
- const next = prev.then(work, work);
135
- paneChains.set(
136
- key,
137
- next.then(
138
- () => undefined,
139
- () => undefined,
140
- ),
141
- );
142
- return next;
143
- };
144
-
145
- return {
146
- async liveNames(fetch) {
147
- const cached = listCache;
148
- if (cached && now() - cached.at < listTtlMs) {
149
- return new Set([...cached.names, ...claimed]);
150
- }
151
- if (!listPending) {
152
- listPending = fetch()
153
- .then((names) => {
154
- listCache = { names, at: now() };
155
- return names;
156
- })
157
- .finally(() => {
158
- listPending = undefined;
159
- });
160
- }
161
- const names = await listPending;
162
- return new Set([...names, ...claimed]);
163
- },
164
-
165
- claimName(agentType, blocked) {
166
- for (let i = 0; i < 1000; i += 1) {
167
- const candidate = makeName(agentType, i);
168
- if (claimed.has(candidate) || blocked(candidate)) continue;
169
- claimed.add(candidate);
170
- return candidate;
171
- }
172
- const fallback = makeName(agentType, 1000);
173
- claimed.add(fallback);
174
- return fallback;
175
- },
176
-
177
- rememberName(name) {
178
- claimed.add(name);
179
- },
180
-
181
- async acquireTypeTab(agentType, create) {
182
- for (;;) {
183
- const existing = tabs.get(agentType);
184
- if (existing) return existing;
185
- const inflight = pendingTabs.get(agentType);
186
- if (inflight) {
187
- try {
188
- return await inflight;
189
- } catch {
190
- continue;
191
- }
192
- }
193
-
194
- const work = create().then((created) => {
195
- const panes = created.panes
196
- ? [...created.panes]
197
- : created.rootOccupied
198
- ? [created.rootPaneId]
199
- : [];
200
- const slot: TypeTab = {
201
- tabId: created.tabId,
202
- rootPaneId: created.rootPaneId,
203
- rootPaneUsed: panes.length > 0,
204
- panes,
205
- };
206
- tabs.set(agentType, slot);
207
- return slot;
208
- });
209
- pendingTabs.set(agentType, work);
210
- try {
211
- return await work;
212
- } finally {
213
- if (pendingTabs.get(agentType) === work) {
214
- pendingTabs.delete(agentType);
215
- }
216
- }
217
- }
218
- },
219
-
220
- assignPane(agentType, split) {
221
- return enqueue(agentType, async () => {
222
- const slot = tabs.get(agentType);
223
- if (!slot) {
224
- throw new Error(`no type tab registered for ${agentType}`);
225
- }
226
- const plan = tileSplit(slot.panes);
227
- if (!plan) {
228
- slot.panes.push(slot.rootPaneId);
229
- slot.rootPaneUsed = true;
230
- return { paneId: slot.rootPaneId, tabId: slot.tabId };
231
- }
232
- const paneId = await split(plan);
233
- slot.panes.push(paneId);
234
- return { paneId, tabId: slot.tabId };
235
- });
236
- },
237
-
238
- releasePane(agentType, paneId) {
239
- const slot = tabs.get(agentType);
240
- if (!slot) return { tabId: undefined, empty: true };
241
- slot.panes = slot.panes.filter((id) => id !== paneId);
242
- if (slot.panes.length > 0) {
243
- return { tabId: slot.tabId, empty: false };
244
- }
245
- const tabId = slot.tabId;
246
- tabs.delete(agentType);
247
- return { tabId, empty: true };
248
- },
249
-
250
- getTypeTab(agentType) {
251
- return tabs.get(agentType);
252
- },
253
-
254
- dropTypeTab(agentType) {
255
- tabs.delete(agentType);
256
- },
257
- };
258
- }