@linxiraos/pi-messenger 1.1.0 → 1.3.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 +741 -676
- package/LICENSE +45 -0
- package/README.md +424 -424
- package/THIRD-PARTY-NOTICES.txt +22951 -0
- package/config-overlay.ts +175 -171
- package/config.ts +221 -209
- package/crew/agents/crew-plan-sync.md +66 -64
- package/crew/agents/crew-planner.md +173 -172
- package/crew/agents/crew-reviewer.md +58 -58
- package/crew/agents/crew-worker.md +130 -129
- package/crew/agents.ts +507 -434
- package/crew/handlers/coordination.ts +350 -332
- package/crew/handlers/plan.ts +988 -906
- package/crew/handlers/review.ts +354 -333
- package/crew/handlers/revise.ts +400 -376
- package/crew/handlers/status.ts +303 -295
- package/crew/handlers/sync.ts +237 -233
- package/crew/handlers/task.ts +820 -792
- package/crew/handlers/team.ts +294 -235
- package/crew/handlers/work.ts +467 -425
- package/crew/id-allocator.ts +30 -30
- package/crew/index.ts +297 -257
- package/crew/live-progress.ts +58 -58
- package/crew/lobby.ts +504 -473
- package/crew/prompt.ts +262 -252
- package/crew/registry.ts +114 -114
- package/crew/spawn.ts +135 -111
- package/crew/state-autonomous.ts +163 -164
- package/crew/state-planning.ts +264 -263
- package/crew/state.ts +20 -20
- package/crew/store.ts +646 -642
- package/crew/task-actions.ts +162 -131
- package/crew/team/store.ts +572 -479
- package/crew/team/subagent-roles.ts +105 -87
- package/crew/team/types.ts +50 -50
- package/crew/types.ts +194 -194
- package/crew/utils/artifacts.ts +57 -62
- package/crew/utils/config.ts +158 -156
- package/crew/utils/discover.ts +221 -219
- package/crew/utils/install.ts +94 -85
- package/crew/utils/migrations.ts +126 -0
- package/crew/utils/progress.ts +191 -187
- package/crew/utils/result.ts +16 -16
- package/crew/utils/risk-labels.ts +5 -7
- package/crew/utils/task-format.ts +14 -14
- package/crew/utils/team-roles.ts +44 -28
- package/crew/utils/truncate.ts +76 -79
- package/crew/utils/verdict.ts +55 -55
- package/editor-binary.ts +150 -0
- package/editor-command.ts +107 -0
- package/editor-target.ts +40 -0
- package/feed.ts +321 -268
- package/handlers.ts +1041 -1107
- package/index.ts +1314 -1206
- package/install.mjs +187 -189
- package/lib.ts +587 -467
- package/overlay-actions.ts +519 -511
- package/overlay-coordinator.ts +150 -150
- package/overlay-render.ts +770 -676
- package/overlay.ts +872 -830
- package/package.json +64 -60
- package/paths.ts +60 -0
- package/skills/pi-messenger-crew/SKILL.md +375 -347
- package/store.ts +1147 -1139
- package/teamagent-command.ts +321 -0
- package/vitest.config.ts +10 -10
package/crew/agents.ts
CHANGED
|
@@ -1,434 +1,507 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Crew - Agent Spawning
|
|
3
|
-
*
|
|
4
|
-
* Spawns
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { spawn } from "node:child_process";
|
|
8
|
-
import { randomUUID } from "node:crypto";
|
|
9
|
-
import * as fs from "node:fs";
|
|
10
|
-
import * as os from "node:os";
|
|
11
|
-
import * as path from "node:path";
|
|
12
|
-
import { fileURLToPath } from "node:url";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} from "./
|
|
31
|
-
import {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Crew - Agent Spawning
|
|
3
|
+
*
|
|
4
|
+
* Spawns zeta CLI subprocesses with progress tracking, truncation, and artifacts.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { CLI_BIN_NAME } from "@linxiraos/pi-utils/dirs";
|
|
14
|
+
import { discoverCrewAgents, type CrewAgentConfig } from "./utils/discover.ts";
|
|
15
|
+
import { getProjectCrewDir } from "../paths.ts";
|
|
16
|
+
import { truncateOutput } from "./utils/truncate.ts";
|
|
17
|
+
import {
|
|
18
|
+
createProgress,
|
|
19
|
+
parseJsonlLine,
|
|
20
|
+
updateProgress,
|
|
21
|
+
getAssistantText,
|
|
22
|
+
compactEventForArtifact,
|
|
23
|
+
getTerminalProviderError,
|
|
24
|
+
} from "./utils/progress.ts";
|
|
25
|
+
import { getArtifactPaths, ensureArtifactsDir, writeArtifact, writeMetadata, appendJsonl } from "./utils/artifacts.ts";
|
|
26
|
+
import { loadCrewConfig, getTruncationForRole, type CrewConfig } from "./utils/config.ts";
|
|
27
|
+
import { removeLiveWorker, updateLiveWorker } from "./live-progress.ts";
|
|
28
|
+
import { autonomousState, waitForConcurrencyChange } from "./state.ts";
|
|
29
|
+
import { registerWorker, unregisterWorker, killAll } from "./registry.ts";
|
|
30
|
+
import type { AgentTask, AgentResult } from "./types.ts";
|
|
31
|
+
import { generateMemorableName } from "../lib.ts";
|
|
32
|
+
|
|
33
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
34
|
+
const __dirname = path.dirname(__filename);
|
|
35
|
+
const EXTENSION_DIR = path.resolve(__dirname, "..");
|
|
36
|
+
|
|
37
|
+
export interface SpawnOptions {
|
|
38
|
+
onProgress?: (results: AgentResult[]) => void;
|
|
39
|
+
crewDir?: string;
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
messengerDirs?: { registry: string; inbox: string };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function shutdownAllWorkers(): void {
|
|
45
|
+
killAll();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function resolveModel(
|
|
49
|
+
taskModel?: string,
|
|
50
|
+
paramModel?: string,
|
|
51
|
+
roleModel?: string,
|
|
52
|
+
configModel?: string,
|
|
53
|
+
sessionModel?: string,
|
|
54
|
+
agentModel?: string,
|
|
55
|
+
): string | undefined {
|
|
56
|
+
return taskModel ?? paramModel ?? roleModel ?? configModel ?? sessionModel ?? agentModel;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Resolve the CLI that crew subprocesses run: the zeta CLI this extension is
|
|
61
|
+
* loaded by — never upstream `pi`, and never the workspace `zeta` product.
|
|
62
|
+
*
|
|
63
|
+
* Priority (mirrors the host's own subprocess resolver, task/omp-command.ts):
|
|
64
|
+
* 1. `PI_SUBPROCESS_CMD` env override (documented escape hatch for embedders).
|
|
65
|
+
* 2. Source/dev run: `process.execPath` is the Bun/Node runtime and
|
|
66
|
+
* `process.argv[1]` the TS/JS CLI entry — spawn `[runtime, entry]`.
|
|
67
|
+
* 3. Compiled binary: `process.execPath` IS the CLI executable.
|
|
68
|
+
* 4. PATH lookup of the published CLI name — `CLI_BIN_NAME` ("zetacode") first,
|
|
69
|
+
* then the `CLI_FALLBACK_NAMES` install list (also just "zetacode"): the
|
|
70
|
+
* product ships exactly one bin name now, so no hyphenated compatibility
|
|
71
|
+
* alias is ever probed. On Windows only real executables qualify:
|
|
72
|
+
* `.cmd`/`.bat` shims cannot be spawned without a shell (EINVAL under Bun
|
|
73
|
+
* and modern Node), and spawning through a shell would expose task text to
|
|
74
|
+
* command injection. When nothing resolves, the primary name is returned
|
|
75
|
+
* anyway so the spawn fails with a clear ENOENT instead of silently
|
|
76
|
+
* running the wrong CLI.
|
|
77
|
+
*/
|
|
78
|
+
const CLI_FALLBACK_NAMES = ["zetacode"];
|
|
79
|
+
|
|
80
|
+
/** Script-file argv[1] discriminates a source-mode CLI run from a compiled binary. */
|
|
81
|
+
const ENTRY_SCRIPT_RE = /\.(?:[cm]?[jt]sx?)$/;
|
|
82
|
+
|
|
83
|
+
interface CliSpawnCommand {
|
|
84
|
+
cmd: string;
|
|
85
|
+
args: string[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let cachedCliCommand: CliSpawnCommand | null = null;
|
|
89
|
+
|
|
90
|
+
function isJsRuntimeExecutable(): boolean {
|
|
91
|
+
const base = path.basename(process.execPath).replace(/\.exe$/i, "").toLowerCase();
|
|
92
|
+
return base === "bun" || base === "node" || base === "electron";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function resolveCliFromPath(): string | null {
|
|
96
|
+
const searchDirs = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean);
|
|
97
|
+
const suffixes = process.platform === "win32" ? [".exe"] : [""];
|
|
98
|
+
for (const dir of searchDirs) {
|
|
99
|
+
for (const name of [CLI_BIN_NAME, ...CLI_FALLBACK_NAMES]) {
|
|
100
|
+
for (const suffix of suffixes) {
|
|
101
|
+
const candidate = path.join(dir, name + suffix);
|
|
102
|
+
try {
|
|
103
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
104
|
+
if (fs.statSync(candidate).isFile()) return candidate;
|
|
105
|
+
} catch {
|
|
106
|
+
// Keep scanning.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function getCliSpawnCommand(): CliSpawnCommand {
|
|
115
|
+
if (cachedCliCommand) return cachedCliCommand;
|
|
116
|
+
|
|
117
|
+
const envCmd = process.env.PI_SUBPROCESS_CMD?.trim();
|
|
118
|
+
const entry = process.argv[1];
|
|
119
|
+
let resolved: CliSpawnCommand;
|
|
120
|
+
if (envCmd) {
|
|
121
|
+
resolved = { cmd: envCmd, args: [] };
|
|
122
|
+
} else if (isJsRuntimeExecutable() && entry && ENTRY_SCRIPT_RE.test(entry)) {
|
|
123
|
+
resolved = { cmd: process.execPath, args: [entry] };
|
|
124
|
+
} else if (!isJsRuntimeExecutable()) {
|
|
125
|
+
resolved = { cmd: process.execPath, args: [] };
|
|
126
|
+
} else {
|
|
127
|
+
resolved = { cmd: resolveCliFromPath() ?? CLI_BIN_NAME, args: [] };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
cachedCliCommand = resolved;
|
|
131
|
+
return resolved;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Test seam: forget the cached CLI resolution so a test can re-resolve. */
|
|
135
|
+
export function resetCliCommandCache(): void {
|
|
136
|
+
cachedCliCommand = null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function pushModelArgs(args: string[], model: string): void {
|
|
140
|
+
const slashIdx = model.indexOf("/");
|
|
141
|
+
if (slashIdx !== -1) {
|
|
142
|
+
args.push("--provider", model.substring(0, slashIdx), "--model", model.substring(slashIdx + 1));
|
|
143
|
+
} else {
|
|
144
|
+
args.push("--model", model);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const THINKING_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
|
|
149
|
+
|
|
150
|
+
export function resolveThinking(configThinking?: string, agentThinking?: string): string | undefined {
|
|
151
|
+
const resolved = configThinking ?? agentThinking;
|
|
152
|
+
if (!resolved || resolved === "off") return undefined;
|
|
153
|
+
return resolved;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function modelHasThinkingSuffix(model: string | undefined): boolean {
|
|
157
|
+
if (!model) return false;
|
|
158
|
+
const colonIdx = model.lastIndexOf(":");
|
|
159
|
+
if (colonIdx === -1) return false;
|
|
160
|
+
return THINKING_LEVELS.has(model.substring(colonIdx + 1));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function raceTimeout(promise: Promise<void>, ms: number): Promise<boolean> {
|
|
164
|
+
return new Promise<boolean>(resolve => {
|
|
165
|
+
const timer = setTimeout(() => resolve(false), ms);
|
|
166
|
+
promise.then(
|
|
167
|
+
() => {
|
|
168
|
+
clearTimeout(timer);
|
|
169
|
+
resolve(true);
|
|
170
|
+
},
|
|
171
|
+
() => {
|
|
172
|
+
clearTimeout(timer);
|
|
173
|
+
resolve(false);
|
|
174
|
+
},
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function discoverWorkerName(pid: number | undefined, registryDir: string | undefined): string | null {
|
|
180
|
+
if (!pid || !registryDir || !fs.existsSync(registryDir)) return null;
|
|
181
|
+
try {
|
|
182
|
+
for (const file of fs.readdirSync(registryDir)) {
|
|
183
|
+
if (!file.endsWith(".json")) continue;
|
|
184
|
+
const reg = JSON.parse(fs.readFileSync(path.join(registryDir, file), "utf-8"));
|
|
185
|
+
if (reg.pid === pid) return reg.name;
|
|
186
|
+
}
|
|
187
|
+
} catch {}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const SHUTDOWN_MESSAGE = `⚠️ SHUTDOWN REQUESTED: Please wrap up your current work.
|
|
192
|
+
1. Release any file reservations
|
|
193
|
+
2. If the task is not complete, leave it as in_progress (do NOT mark done)
|
|
194
|
+
3. Do NOT commit anything
|
|
195
|
+
4. Exit`;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Spawn multiple agents in parallel with concurrency limit.
|
|
199
|
+
*/
|
|
200
|
+
export async function spawnAgents(tasks: AgentTask[], cwd: string, options: SpawnOptions = {}): Promise<AgentResult[]> {
|
|
201
|
+
const crewDir = options.crewDir ?? getProjectCrewDir(cwd);
|
|
202
|
+
const config = loadCrewConfig(crewDir);
|
|
203
|
+
const agents = discoverCrewAgents(cwd);
|
|
204
|
+
const runId = randomUUID().slice(0, 8);
|
|
205
|
+
|
|
206
|
+
// Setup artifacts directory if enabled
|
|
207
|
+
const artifactsDir = path.join(crewDir, "artifacts");
|
|
208
|
+
if (config.artifacts.enabled) {
|
|
209
|
+
ensureArtifactsDir(artifactsDir, config.artifacts.cleanupDays);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const results: AgentResult[] = [];
|
|
213
|
+
const queue = tasks.map((task, index) => ({ task, index }));
|
|
214
|
+
const running: Promise<void>[] = [];
|
|
215
|
+
|
|
216
|
+
while (queue.length > 0 || running.length > 0) {
|
|
217
|
+
if (options.signal?.aborted && running.length === 0) break;
|
|
218
|
+
|
|
219
|
+
while (running.length < autonomousState.concurrency && queue.length > 0) {
|
|
220
|
+
if (options.signal?.aborted) break;
|
|
221
|
+
const { task, index } = queue.shift()!;
|
|
222
|
+
const promise = runAgent(task, index, cwd, agents, config, runId, artifactsDir, options).then(result => {
|
|
223
|
+
results.push(result);
|
|
224
|
+
running.splice(running.indexOf(promise), 1);
|
|
225
|
+
options.onProgress?.(results);
|
|
226
|
+
});
|
|
227
|
+
running.push(promise);
|
|
228
|
+
}
|
|
229
|
+
if (running.length > 0) {
|
|
230
|
+
await Promise.race([...running, waitForConcurrencyChange()]);
|
|
231
|
+
if (options.signal?.aborted) continue;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return results;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function runAgent(
|
|
239
|
+
task: AgentTask,
|
|
240
|
+
index: number,
|
|
241
|
+
cwd: string,
|
|
242
|
+
agents: CrewAgentConfig[],
|
|
243
|
+
config: CrewConfig,
|
|
244
|
+
runId: string,
|
|
245
|
+
artifactsDir: string,
|
|
246
|
+
options: SpawnOptions,
|
|
247
|
+
): Promise<AgentResult> {
|
|
248
|
+
const agentConfig = agents.find(a => a.name === task.agent);
|
|
249
|
+
const progress = createProgress(task.agent);
|
|
250
|
+
const startTime = Date.now();
|
|
251
|
+
const workerName = generateMemorableName();
|
|
252
|
+
|
|
253
|
+
const role = agentConfig?.crewRole ?? "worker";
|
|
254
|
+
const maxOutput = task.maxOutput ?? agentConfig?.maxOutput ?? getTruncationForRole(config, role);
|
|
255
|
+
|
|
256
|
+
let artifactPaths = config.artifacts.enabled ? getArtifactPaths(artifactsDir, runId, task.agent, index) : undefined;
|
|
257
|
+
|
|
258
|
+
if (artifactPaths) {
|
|
259
|
+
try {
|
|
260
|
+
writeArtifact(artifactPaths.inputPath, `# Task for ${task.agent}\n\n${task.task}`);
|
|
261
|
+
} catch {
|
|
262
|
+
artifactPaths = undefined;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return new Promise(resolve => {
|
|
267
|
+
// Build args for the zeta CLI. Flag surface verified against the host
|
|
268
|
+
// parser (packages/coding-agent/src/cli/{args,flag-tables}.ts):
|
|
269
|
+
// --mode json, --no-session, -p/--print, --provider/--model, --thinking,
|
|
270
|
+
// --tools (comma-separated), repeatable --extension,
|
|
271
|
+
// --append-system-prompt, trailing positional prompt. Upstream pi used
|
|
272
|
+
// the same vocabulary, so no per-flag adaptation is needed.
|
|
273
|
+
const args = ["--mode", "json", "--no-session", "-p"];
|
|
274
|
+
const model = task.modelOverride ?? config.models?.[role] ?? agentConfig?.model;
|
|
275
|
+
if (model) pushModelArgs(args, model);
|
|
276
|
+
|
|
277
|
+
const thinking = resolveThinking(config.thinking?.[role], agentConfig?.thinking);
|
|
278
|
+
if (thinking && !modelHasThinkingSuffix(model)) {
|
|
279
|
+
args.push("--thinking", thinking);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (agentConfig?.tools?.length) {
|
|
283
|
+
const builtinTools: string[] = [];
|
|
284
|
+
const extensionPaths: string[] = [];
|
|
285
|
+
for (const tool of agentConfig.tools) {
|
|
286
|
+
if (tool.includes("/") || tool.endsWith(".ts") || tool.endsWith(".js")) {
|
|
287
|
+
extensionPaths.push(tool);
|
|
288
|
+
} else {
|
|
289
|
+
builtinTools.push(tool);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (builtinTools.length > 0) {
|
|
294
|
+
args.push("--tools", builtinTools.join(","));
|
|
295
|
+
}
|
|
296
|
+
for (const extensionPath of extensionPaths) {
|
|
297
|
+
args.push("--extension", extensionPath);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Pass extension so workers can use pi_messenger
|
|
302
|
+
args.push("--extension", EXTENSION_DIR);
|
|
303
|
+
|
|
304
|
+
let promptTmpDir: string | null = null;
|
|
305
|
+
if (agentConfig?.systemPrompt) {
|
|
306
|
+
promptTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-messenger-agent-"));
|
|
307
|
+
const promptPath = path.join(promptTmpDir, `${task.agent.replace(/[^\w.-]/g, "_")}.md`);
|
|
308
|
+
fs.writeFileSync(promptPath, agentConfig.systemPrompt, { mode: 0o600 });
|
|
309
|
+
args.push("--append-system-prompt", promptPath);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
args.push(task.task);
|
|
313
|
+
|
|
314
|
+
const envOverrides = config.work.env ?? {};
|
|
315
|
+
const workerFlag = role === "worker" ? { PI_CREW_WORKER: "1", PI_AGENT_NAME: workerName } : {};
|
|
316
|
+
const env =
|
|
317
|
+
Object.keys(envOverrides).length > 0 || role === "worker"
|
|
318
|
+
? { ...process.env, ...envOverrides, ...workerFlag }
|
|
319
|
+
: undefined;
|
|
320
|
+
|
|
321
|
+
const cli = getCliSpawnCommand();
|
|
322
|
+
const proc = spawn(cli.cmd, [...cli.args, ...args], {
|
|
323
|
+
cwd,
|
|
324
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
325
|
+
...(env ? { env } : {}),
|
|
326
|
+
});
|
|
327
|
+
if (task.taskId) {
|
|
328
|
+
registerWorker({ type: "worker", proc, name: workerName, cwd, taskId: task.taskId });
|
|
329
|
+
}
|
|
330
|
+
let gracefulShutdownRequested = false;
|
|
331
|
+
let discoveredWorkerName: string | null = null;
|
|
332
|
+
|
|
333
|
+
let jsonlBuffer = "";
|
|
334
|
+
let fullOutput = "";
|
|
335
|
+
let terminalProviderError: string | null = null;
|
|
336
|
+
|
|
337
|
+
proc.stdout?.on("data", data => {
|
|
338
|
+
try {
|
|
339
|
+
jsonlBuffer += data.toString();
|
|
340
|
+
const lines = jsonlBuffer.split("\n");
|
|
341
|
+
jsonlBuffer = lines.pop() ?? "";
|
|
342
|
+
|
|
343
|
+
for (const line of lines) {
|
|
344
|
+
const event = parseJsonlLine(line);
|
|
345
|
+
if (event) {
|
|
346
|
+
updateProgress(progress, event, startTime);
|
|
347
|
+
const assistantText = getAssistantText(event);
|
|
348
|
+
if (assistantText) fullOutput = assistantText;
|
|
349
|
+
if (artifactPaths) {
|
|
350
|
+
try {
|
|
351
|
+
appendJsonl(artifactPaths.jsonlPath, JSON.stringify(compactEventForArtifact(event)));
|
|
352
|
+
} catch {
|
|
353
|
+
artifactPaths = undefined;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const providerError = getTerminalProviderError(event);
|
|
357
|
+
if (providerError && !terminalProviderError) {
|
|
358
|
+
terminalProviderError = providerError;
|
|
359
|
+
progress.error = providerError;
|
|
360
|
+
proc.kill("SIGTERM");
|
|
361
|
+
}
|
|
362
|
+
if (task.taskId) {
|
|
363
|
+
updateLiveWorker(cwd, task.taskId, {
|
|
364
|
+
taskId: task.taskId,
|
|
365
|
+
agent: task.agent,
|
|
366
|
+
name: workerName,
|
|
367
|
+
progress: {
|
|
368
|
+
...progress,
|
|
369
|
+
recentTools: progress.recentTools.map(tool => ({ ...tool })),
|
|
370
|
+
},
|
|
371
|
+
startedAt: startTime,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
} catch {}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
let stderr = "";
|
|
380
|
+
proc.stderr?.on("data", data => {
|
|
381
|
+
stderr += data.toString();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
proc.on("close", code => {
|
|
385
|
+
if (task.taskId) {
|
|
386
|
+
removeLiveWorker(cwd, task.taskId);
|
|
387
|
+
unregisterWorker(cwd, task.taskId);
|
|
388
|
+
}
|
|
389
|
+
const exitCode = terminalProviderError ? 1 : (code ?? 1);
|
|
390
|
+
progress.status = exitCode === 0 ? "completed" : "failed";
|
|
391
|
+
progress.durationMs = Date.now() - startTime;
|
|
392
|
+
if (terminalProviderError) progress.error = terminalProviderError;
|
|
393
|
+
else if (stderr && exitCode !== 0) progress.error = stderr;
|
|
394
|
+
|
|
395
|
+
const truncation = truncateOutput(fullOutput, maxOutput, artifactPaths?.outputPath);
|
|
396
|
+
|
|
397
|
+
if (artifactPaths) {
|
|
398
|
+
try {
|
|
399
|
+
writeArtifact(artifactPaths.outputPath, fullOutput);
|
|
400
|
+
writeMetadata(artifactPaths.metadataPath, {
|
|
401
|
+
runId,
|
|
402
|
+
agent: task.agent,
|
|
403
|
+
index,
|
|
404
|
+
exitCode,
|
|
405
|
+
durationMs: progress.durationMs,
|
|
406
|
+
tokens: progress.tokens,
|
|
407
|
+
truncated: truncation.truncated,
|
|
408
|
+
error: progress.error,
|
|
409
|
+
});
|
|
410
|
+
} catch {}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (promptTmpDir) {
|
|
414
|
+
try {
|
|
415
|
+
fs.rmSync(promptTmpDir, { recursive: true, force: true });
|
|
416
|
+
} catch {}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
resolve({
|
|
420
|
+
agent: task.agent,
|
|
421
|
+
exitCode,
|
|
422
|
+
output: truncation.text,
|
|
423
|
+
truncated: truncation.truncated,
|
|
424
|
+
progress,
|
|
425
|
+
config: agentConfig,
|
|
426
|
+
taskId: task.taskId,
|
|
427
|
+
wasGracefullyShutdown: gracefulShutdownRequested,
|
|
428
|
+
error: progress.error,
|
|
429
|
+
artifactPaths: artifactPaths
|
|
430
|
+
? {
|
|
431
|
+
input: artifactPaths.inputPath,
|
|
432
|
+
output: artifactPaths.outputPath,
|
|
433
|
+
jsonl: artifactPaths.jsonlPath,
|
|
434
|
+
metadata: artifactPaths.metadataPath,
|
|
435
|
+
}
|
|
436
|
+
: undefined,
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
if (gracefulShutdownRequested && discoveredWorkerName && options.messengerDirs?.registry) {
|
|
440
|
+
try {
|
|
441
|
+
fs.unlinkSync(path.join(options.messengerDirs.registry, `${discoveredWorkerName}.json`));
|
|
442
|
+
} catch {}
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// Handle abort signal
|
|
447
|
+
if (options.signal) {
|
|
448
|
+
const gracefulShutdown = async () => {
|
|
449
|
+
gracefulShutdownRequested = true;
|
|
450
|
+
|
|
451
|
+
let messageSent = false;
|
|
452
|
+
discoveredWorkerName = discoverWorkerName(proc.pid, options.messengerDirs?.registry);
|
|
453
|
+
if (discoveredWorkerName && options.messengerDirs) {
|
|
454
|
+
try {
|
|
455
|
+
const inboxDir = path.join(options.messengerDirs.inbox, discoveredWorkerName);
|
|
456
|
+
if (fs.existsSync(inboxDir)) {
|
|
457
|
+
const msgFile = path.join(inboxDir, `${Date.now()}-shutdown.json`);
|
|
458
|
+
fs.writeFileSync(
|
|
459
|
+
msgFile,
|
|
460
|
+
JSON.stringify({
|
|
461
|
+
id: randomUUID(),
|
|
462
|
+
from: "crew-orchestrator",
|
|
463
|
+
to: discoveredWorkerName,
|
|
464
|
+
text: SHUTDOWN_MESSAGE,
|
|
465
|
+
timestamp: new Date().toISOString(),
|
|
466
|
+
replyTo: null,
|
|
467
|
+
}),
|
|
468
|
+
);
|
|
469
|
+
messageSent = true;
|
|
470
|
+
}
|
|
471
|
+
} catch {}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (messageSent) {
|
|
475
|
+
const graceMs = config.work.shutdownGracePeriodMs ?? 30000;
|
|
476
|
+
const exitPromise = new Promise<void>(r => proc.once("exit", () => r()));
|
|
477
|
+
const exited = await raceTimeout(exitPromise, graceMs);
|
|
478
|
+
if (exited) return;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (!proc.killed && proc.exitCode === null) {
|
|
482
|
+
proc.kill("SIGTERM");
|
|
483
|
+
const termPromise = new Promise<void>(r => proc.once("exit", () => r()));
|
|
484
|
+
const killed = await raceTimeout(termPromise, 5000);
|
|
485
|
+
if (killed) return;
|
|
486
|
+
} else {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (proc.exitCode === null) {
|
|
491
|
+
proc.kill("SIGKILL");
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
if (options.signal.aborted) {
|
|
495
|
+
gracefulShutdown().catch(() => {});
|
|
496
|
+
} else {
|
|
497
|
+
options.signal.addEventListener(
|
|
498
|
+
"abort",
|
|
499
|
+
() => {
|
|
500
|
+
gracefulShutdown().catch(() => {});
|
|
501
|
+
},
|
|
502
|
+
{ once: true },
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
}
|