@leavewhite9088/harness-bus 0.2.0 → 0.2.1
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/ARCHITECTURE.md +81 -81
- package/LICENSE +21 -21
- package/README.md +86 -86
- package/SETUP_FOR_AI.md +79 -79
- package/package.json +4 -2
- package/packages/agentd/agentd.config.json +3 -1
- package/packages/agentd/cli/commands.mjs +854 -810
- package/packages/agentd/cli/format.mjs +294 -238
- package/packages/agentd/cli/hbus.mjs +14 -14
- package/packages/agentd/cli/index.mjs +4 -4
- package/packages/agentd/cli/skill/SKILL.md +66 -61
- package/packages/agentd/cli/skill/references/custom.md +12 -0
- package/packages/agentd/cli/skill/references/playbooks.md +89 -86
- package/packages/agentd/cli/skill/references/routing.md +43 -35
- package/packages/agentd/cli/skill-install.mjs +157 -140
- package/packages/agentd/cli/update.mjs +221 -221
- package/packages/agentd/client/index.mjs +3 -3
- package/packages/agentd/contexts/executor.md +13 -12
- package/packages/agentd/contexts/templates/architect.md +12 -12
- package/packages/agentd/contexts/templates/coder.md +12 -12
- package/packages/agentd/contexts/templates/debugger.md +12 -12
- package/packages/agentd/contexts/templates/researcher.md +12 -12
- package/packages/agentd/contexts/templates/reviewer.md +12 -12
- package/packages/agentd/contexts/templates/tester.md +12 -12
- package/packages/agentd/core/base/data-dir.mjs +39 -39
- package/packages/agentd/core/base/index.mjs +5 -5
- package/packages/agentd/core/base/protocol.mjs +13 -13
- package/packages/agentd/core/env/config.mjs +52 -1
- package/packages/agentd/core/env/index.mjs +6 -1
- package/packages/agentd/core/env/install.mjs +82 -4
- package/packages/agentd/core/pi/index.mjs +1 -1
- package/packages/agentd/core/pi/rpc.mjs +206 -206
- package/packages/agentd/core/pool/acp/client.mjs +7 -9
- package/packages/agentd/core/pool/acp/process.mjs +27 -18
- package/packages/agentd/core/pool/acp/run-tracker.mjs +293 -293
- package/packages/agentd/core/pool/acp/sessions.mjs +465 -465
- package/packages/agentd/core/pool/acp/turn-runner.mjs +5 -3
- package/packages/agentd/core/pool/index.mjs +2 -2
- package/packages/agentd/core/pool/instances.mjs +3 -5
- package/packages/agentd/core/pool/probe.mjs +91 -85
- package/packages/agentd/core/pool/registry.mjs +45 -1
- package/packages/agentd/core/pool/support/compaction.mjs +2 -43
- package/packages/agentd/core/pool/support/index.mjs +1 -1
- package/packages/agentd/core/prompts/index.mjs +12 -12
- package/packages/agentd/core/prompts/rules.mjs +22 -22
- package/packages/agentd/core/prompts/templates.mjs +172 -172
- package/packages/agentd/core/providers/index.mjs +14 -14
- package/packages/agentd/core/providers/store.mjs +206 -206
- package/packages/agentd/core/records/cleanup.mjs +51 -51
- package/packages/agentd/core/records/index.mjs +8 -8
- package/packages/agentd/core/records/metrics.mjs +73 -73
- package/packages/agentd/core/records/recovery.mjs +39 -39
- package/packages/agentd/core/records/transcripts.mjs +122 -122
- package/packages/agentd/core/threads/index.mjs +18 -17
- package/packages/agentd/core/threads/manager.mjs +498 -476
- package/packages/agentd/core/threads/paths.mjs +78 -78
- package/packages/agentd/core/threads/store.mjs +146 -125
- package/packages/agentd/daemon/agentd.mjs +58 -58
- package/packages/agentd/daemon/events.mjs +126 -126
- package/packages/agentd/daemon/guard.mjs +127 -127
- package/packages/agentd/daemon/lifecycle.mjs +61 -61
- package/packages/agentd/daemon/ops/index.mjs +15 -15
- package/packages/agentd/daemon/ops/providers.mjs +46 -46
- package/packages/agentd/daemon/ops/run.mjs +413 -400
- package/packages/agentd/daemon/ops/system.mjs +76 -69
- package/packages/agentd/daemon/ops/threads.mjs +156 -153
- package/packages/agentd/daemon/server.mjs +130 -130
- package/packages/agentd/package.json +1 -1
- package/packages/agentd/pi-adapter/acp-adapter.mjs +501 -501
- package/packages/agentd/scripts/restart-daemon.mjs +74 -74
- package/packages/agentd/scripts/run-isolated.mjs +42 -42
- package/packages/agentd/scripts/run-unit.mjs +28 -28
- package/packages/backend/src/daemon.mjs +69 -69
- package/packages/backend/src/server.mjs +270 -270
- package/packages/drivers/{agy.mjs → agy/index.mjs} +2 -1
- package/packages/drivers/{claude.mjs → claude/index.mjs} +23 -1
- package/packages/drivers/{codex.mjs → codex/index.mjs} +2 -1
- package/packages/drivers/{dsh.mjs → dsh/index.mjs} +3 -2
- package/packages/drivers/grok/index.mjs +96 -0
- package/packages/drivers/index.mjs +25 -12
- package/packages/drivers/{kimi.mjs → kimi/index.mjs} +2 -1
- package/packages/drivers/{mcode.mjs → mcode/index.mjs} +2 -1
- package/packages/drivers/{opencode.mjs → opencode/index.mjs} +2 -1
- package/packages/drivers/{pi.mjs → pi/index.mjs} +3 -2
- package/packages/drivers/probes/acp.mjs +109 -109
- package/packages/drivers/probes/index.mjs +131 -129
- package/packages/drivers/probes/provider.mjs +222 -222
- package/packages/drivers/qoder/index.mjs +32 -0
- package/packages/drivers/{bin.mjs → shared/bin.mjs} +2 -0
- package/packages/drivers/shared/index.mjs +2 -0
- package/packages/preflight/bin/preflight.mjs +116 -116
- package/packages/preflight/src/index.mjs +5 -5
- package/packages/preflight/src/report.mjs +18 -1
- package/packages/web/dist/apple-touch-icon.png +0 -0
- package/packages/web/dist/assets/index-CT8dIwav.css +2 -0
- package/packages/web/dist/assets/index-CypTUMFB.js +79 -0
- package/packages/web/dist/favicon-32.png +0 -0
- package/packages/web/dist/favicon.svg +19 -15
- package/packages/web/dist/index.html +5 -5
- package/packages/web/dist/assets/index-B2kdkHNw.js +0 -79
- package/packages/web/dist/assets/index-DQTyjC4x.css +0 -2
- /package/packages/drivers/{dsh-sessions.mjs → dsh/sessions.mjs} +0 -0
- /package/packages/drivers/{pi-catalog.mjs → pi/catalog.mjs} +0 -0
- /package/packages/drivers/{lib.mjs → shared/lib.mjs} +0 -0
|
@@ -1,400 +1,413 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
let
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
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
|
-
let
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
const
|
|
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
|
-
ev.
|
|
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
|
-
if (!
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const
|
|
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
|
-
if (
|
|
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
|
-
|
|
1
|
+
import { enrichAuthError, formatAuthHint } from '../../core/env/index.mjs';
|
|
2
|
+
import { getAgent, pool } from '../../core/pool/index.mjs';
|
|
3
|
+
import { appendReceipt, clearInterrupted, endRun, fitReply, journalEnd, journalStart, planRun, recordRun } from '../../core/records/index.mjs';
|
|
4
|
+
import * as threads from '../../core/threads/index.mjs';
|
|
5
|
+
import { allocEvent, onEvent, publish, subscribe } from '../events.mjs';
|
|
6
|
+
|
|
7
|
+
function tailSnapshot(a, jobId, mode = 'summary') {
|
|
8
|
+
const snap = a.tail(jobId);
|
|
9
|
+
if (!snap) return null;
|
|
10
|
+
let reply = snap.reply ?? null;
|
|
11
|
+
if (!snap.running && !reply) {
|
|
12
|
+
reply = fitReply(snap.jobId, snap.text ?? '');
|
|
13
|
+
const rec = a.jobs.get(snap.jobId);
|
|
14
|
+
if (rec) rec.reply = reply;
|
|
15
|
+
}
|
|
16
|
+
const base = { ...snap, thread: a.name, agent: a.name, reply };
|
|
17
|
+
if (mode === 'tools') {
|
|
18
|
+
return {
|
|
19
|
+
jobId: base.jobId,
|
|
20
|
+
thread: base.thread,
|
|
21
|
+
running: base.running,
|
|
22
|
+
phase: base.phase,
|
|
23
|
+
elapsedMs: base.elapsedMs,
|
|
24
|
+
idleMs: base.idleMs,
|
|
25
|
+
toolCalls: base.toolCalls,
|
|
26
|
+
openTools: base.openTools,
|
|
27
|
+
openToolMs: base.openToolMs,
|
|
28
|
+
openToolSummary: base.openToolSummary,
|
|
29
|
+
lastTool: base.lastTool,
|
|
30
|
+
recentTools: base.recentTools,
|
|
31
|
+
error: base.metrics?.error ?? null,
|
|
32
|
+
outcome: base.metrics?.outcome ?? null,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (mode === 'summary') {
|
|
36
|
+
const { pendingThought: _pt, text: _tx, ...rest } = base;
|
|
37
|
+
return { ...rest, preview: reply?.text ?? (typeof base.text === 'string' ? base.text.slice(-1200) : '') };
|
|
38
|
+
}
|
|
39
|
+
return base;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const inflightJobs = new Map();
|
|
43
|
+
|
|
44
|
+
function findJobOwner(jobId) {
|
|
45
|
+
const inflightKey = inflightJobs.get(jobId);
|
|
46
|
+
if (inflightKey && pool.has(inflightKey)) return pool.get(inflightKey);
|
|
47
|
+
return (
|
|
48
|
+
[...pool.values()].find(
|
|
49
|
+
(a) => a.run?.jobId === jobId || a.jobs.has(jobId) || a.waitingJobs().some((w) => w.jobId === jobId),
|
|
50
|
+
) ?? null
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function checkCallChain(a, { callerAgent, maxDepth }) {
|
|
55
|
+
let callDepth = 1;
|
|
56
|
+
let callChain = [a.name];
|
|
57
|
+
if (!callerAgent) return { callDepth, callChain };
|
|
58
|
+
|
|
59
|
+
let parent = null;
|
|
60
|
+
try {
|
|
61
|
+
parent = getAgent(callerAgent);
|
|
62
|
+
} catch {
|
|
63
|
+
parent = null;
|
|
64
|
+
}
|
|
65
|
+
const parentDepth = parent?.run ? (parent.run.callDepth ?? 1) : 1;
|
|
66
|
+
const parentChain = parent?.run?.callChain?.length ? parent.run.callChain : [callerAgent];
|
|
67
|
+
const limit = Number.isFinite(Number(maxDepth)) && Number(maxDepth) >= 1 ? Math.floor(Number(maxDepth)) : 1;
|
|
68
|
+
if (parentDepth >= limit) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`cross-CLI delegation depth limit reached (caller "${callerAgent}" is at depth ${parentDepth}, maxDepth=${limit})`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (a.name === callerAgent || parentChain.includes(a.name)) {
|
|
74
|
+
throw new Error(`circular cross-CLI delegation refused: ${[...parentChain, a.name].join(' -> ')}`);
|
|
75
|
+
}
|
|
76
|
+
callDepth = parentDepth + 1;
|
|
77
|
+
callChain = [...parentChain, a.name];
|
|
78
|
+
return { callDepth, callChain };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function executeRun(params, sock) {
|
|
82
|
+
const {
|
|
83
|
+
thread,
|
|
84
|
+
agent,
|
|
85
|
+
cwd,
|
|
86
|
+
topic,
|
|
87
|
+
cli,
|
|
88
|
+
role,
|
|
89
|
+
model,
|
|
90
|
+
effort,
|
|
91
|
+
provider,
|
|
92
|
+
dir,
|
|
93
|
+
prompt,
|
|
94
|
+
timeoutMs,
|
|
95
|
+
waitMs,
|
|
96
|
+
reset,
|
|
97
|
+
sessionId,
|
|
98
|
+
async: isAsync,
|
|
99
|
+
subscribe: wantSub,
|
|
100
|
+
retryOf,
|
|
101
|
+
caller,
|
|
102
|
+
callerAgent,
|
|
103
|
+
maxDepth,
|
|
104
|
+
} = params ?? {};
|
|
105
|
+
|
|
106
|
+
if (!prompt || typeof prompt !== 'string' || !prompt.trim()) {
|
|
107
|
+
throw new Error('prompt is required');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let targetKey = thread ?? agent ?? null;
|
|
111
|
+
let resolvedMeta = null;
|
|
112
|
+
if (!targetKey) {
|
|
113
|
+
if (!cwd) throw new Error('thread key or cwd is required');
|
|
114
|
+
const isManaged = Boolean(callerAgent) || caller === 'managed';
|
|
115
|
+
const res = await threads.resolveThread({ cwd, topic, cli, role, model, effort, provider, dir, allowMutateExisting: !isManaged });
|
|
116
|
+
targetKey = res.key;
|
|
117
|
+
resolvedMeta = res.thread;
|
|
118
|
+
publish(allocEvent('threads.changed', { workspaceId: res.thread.workspaceId, thread: targetKey }));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const a = getAgent(targetKey);
|
|
122
|
+
const { callDepth, callChain } = checkCallChain(a, { callerAgent, maxDepth });
|
|
123
|
+
|
|
124
|
+
const found = threads.findThreadByKey(a.name);
|
|
125
|
+
if (!resolvedMeta && found) threads.touchThreadByKey(a.name);
|
|
126
|
+
const effectiveCwd = cwd ? (found ? threads.resolveInside(found.ws.root, cwd) : cwd) : undefined;
|
|
127
|
+
const plan = planRun({ agent: a.name, prompt, retryOf, counted: false });
|
|
128
|
+
const task = {
|
|
129
|
+
taskId: plan.taskId,
|
|
130
|
+
attempt: plan.attempt,
|
|
131
|
+
retryOf: plan.retryOf,
|
|
132
|
+
maxAttempts: plan.maxAttempts,
|
|
133
|
+
caller: caller ?? (callerAgent ? `agent:${callerAgent}` : 'user'),
|
|
134
|
+
thread: a.name,
|
|
135
|
+
topic: found?.th?.topic ?? resolvedMeta?.topic ?? null,
|
|
136
|
+
cli: a.type,
|
|
137
|
+
role: a.role ?? null,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (model && !resolvedMeta) await a.setConfigOption('model', model, sessionId);
|
|
141
|
+
|
|
142
|
+
const started = a.startPrompt(prompt, {
|
|
143
|
+
timeoutMs,
|
|
144
|
+
cwd: effectiveCwd,
|
|
145
|
+
reset,
|
|
146
|
+
sessionId,
|
|
147
|
+
callDepth,
|
|
148
|
+
callChain,
|
|
149
|
+
meta: { taskId: task.taskId, attempt: task.attempt, retryOf: task.retryOf },
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
recordRun(plan, started.jobId);
|
|
153
|
+
inflightJobs.set(started.jobId, a.name);
|
|
154
|
+
if (wantSub && sock) subscribe(sock, started.jobId);
|
|
155
|
+
|
|
156
|
+
task.queued = started.ahead > 0;
|
|
157
|
+
task.ahead = started.ahead;
|
|
158
|
+
journalStart({ jobId: started.jobId, agent: a.name, project: a.project, prompt, ...task });
|
|
159
|
+
clearInterrupted(a.name);
|
|
160
|
+
publish(
|
|
161
|
+
allocEvent('job.started', {
|
|
162
|
+
jobId: started.jobId,
|
|
163
|
+
...task,
|
|
164
|
+
agent: a.name,
|
|
165
|
+
sessionId: a.sessionId,
|
|
166
|
+
prompt: prompt.slice(0, 160),
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const finish = (result, error) => {
|
|
171
|
+
inflightJobs.delete(started.jobId);
|
|
172
|
+
journalEnd(started.jobId);
|
|
173
|
+
endRun(started.jobId, error ? (error.metrics?.outcome ?? 'failed') : (result?.metrics?.outcome ?? 'done'));
|
|
174
|
+
let payloadResult = result;
|
|
175
|
+
if (!error && result) {
|
|
176
|
+
const reply = fitReply(started.jobId, result.text ?? '');
|
|
177
|
+
const rec = a.jobs.get(started.jobId);
|
|
178
|
+
if (rec) rec.reply = reply;
|
|
179
|
+
payloadResult = { ...result, reply };
|
|
180
|
+
}
|
|
181
|
+
const ev = allocEvent('job.done', {
|
|
182
|
+
jobId: started.jobId,
|
|
183
|
+
...task,
|
|
184
|
+
agent: a.name,
|
|
185
|
+
...(error
|
|
186
|
+
? { error: error.message, metrics: error.metrics ?? { error: error.message, attention: [], health: 'error' } }
|
|
187
|
+
: { result: payloadResult }),
|
|
188
|
+
});
|
|
189
|
+
if (!error?.neverStarted) {
|
|
190
|
+
ev.receipt = appendReceipt(
|
|
191
|
+
a.name,
|
|
192
|
+
error ? { metrics: ev.metrics, sessionId: error.sessionId ?? null } : payloadResult,
|
|
193
|
+
prompt,
|
|
194
|
+
ev.id,
|
|
195
|
+
task,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
publish(ev);
|
|
199
|
+
return ev;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const settled = started.done.then(
|
|
203
|
+
(r) => ({ ok: true, ev: finish(r), result: r }),
|
|
204
|
+
(e) => ({ ok: false, ev: finish(null, e), error: e }),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
if (isAsync) {
|
|
208
|
+
return { jobId: started.jobId, async: true, running: true, ...task };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const softWait = Number(waitMs);
|
|
212
|
+
if (Number.isFinite(softWait) && softWait > 0) {
|
|
213
|
+
let timer = null;
|
|
214
|
+
const outcome = await Promise.race([
|
|
215
|
+
settled,
|
|
216
|
+
new Promise((resolve) => {
|
|
217
|
+
timer = setTimeout(() => resolve({ timedOut: true }), softWait);
|
|
218
|
+
}),
|
|
219
|
+
]);
|
|
220
|
+
if (timer) clearTimeout(timer);
|
|
221
|
+
if (outcome.timedOut) {
|
|
222
|
+
return {
|
|
223
|
+
jobId: started.jobId,
|
|
224
|
+
running: true,
|
|
225
|
+
timedOut: true,
|
|
226
|
+
...task,
|
|
227
|
+
tail: tailSnapshot(a, started.jobId, 'tools'),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (!outcome.ok) throw enrichAuthError(a.type, outcome.error);
|
|
231
|
+
return { ...(outcome.ev.result ?? outcome.result), jobId: started.jobId, ...task, receipt: outcome.ev.receipt };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const outcome = await settled;
|
|
235
|
+
if (!outcome.ok) throw enrichAuthError(a.type, outcome.error);
|
|
236
|
+
return { ...(outcome.ev.result ?? outcome.result), jobId: started.jobId, ...task, receipt: outcome.ev.receipt };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function runOps(_ctx) {
|
|
240
|
+
return {
|
|
241
|
+
run: (params, sock) => executeRun(params, sock),
|
|
242
|
+
|
|
243
|
+
fanout: async ({ cwd, prompt, targets, timeoutMs, waitMs, caller, callerAgent, maxDepth } = {}, sock) => {
|
|
244
|
+
if (!Array.isArray(targets) || targets.length === 0) throw new Error('targets array (1-8 items) is required');
|
|
245
|
+
if (targets.length > 8) throw new Error('fanout supports at most 8 targets at once');
|
|
246
|
+
|
|
247
|
+
const startedAt = Date.now();
|
|
248
|
+
const settled = await Promise.allSettled(
|
|
249
|
+
targets.map((t, index) => {
|
|
250
|
+
const item = typeof t === 'string' ? { cli: t } : t;
|
|
251
|
+
const itemPrompt = item.prompt || prompt;
|
|
252
|
+
return executeRun(
|
|
253
|
+
{
|
|
254
|
+
cwd: item.cwd || cwd,
|
|
255
|
+
topic: item.topic ?? null,
|
|
256
|
+
cli: item.cli ?? null,
|
|
257
|
+
role: item.role ?? null,
|
|
258
|
+
model: item.model ?? null,
|
|
259
|
+
effort: item.effort ?? null,
|
|
260
|
+
provider: item.provider,
|
|
261
|
+
dir: item.dir ?? '.',
|
|
262
|
+
prompt: itemPrompt,
|
|
263
|
+
timeoutMs: item.timeoutMs ?? timeoutMs,
|
|
264
|
+
waitMs: item.waitMs ?? waitMs,
|
|
265
|
+
reset: item.reset,
|
|
266
|
+
caller,
|
|
267
|
+
callerAgent,
|
|
268
|
+
maxDepth,
|
|
269
|
+
},
|
|
270
|
+
sock,
|
|
271
|
+
).then((res) => ({ index, target: item, ok: true, ...res }));
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const results = settled.map((s, index) => {
|
|
276
|
+
const target = typeof targets[index] === 'string' ? { cli: targets[index] } : targets[index];
|
|
277
|
+
if (s.status === 'fulfilled') return s.value;
|
|
278
|
+
const errMsg = s.reason?.message ?? String(s.reason);
|
|
279
|
+
const cliName = target?.cli ?? null;
|
|
280
|
+
const authHint = errMsg.includes('登录提示(') ? null : formatAuthHint(cliName, errMsg);
|
|
281
|
+
return {
|
|
282
|
+
index,
|
|
283
|
+
target,
|
|
284
|
+
ok: false,
|
|
285
|
+
cli: cliName,
|
|
286
|
+
role: target?.role ?? null,
|
|
287
|
+
error: errMsg,
|
|
288
|
+
...(authHint ? { authHint } : {}),
|
|
289
|
+
};
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
totalMs: Date.now() - startedAt,
|
|
294
|
+
succeeded: results.filter((r) => r.ok && !r.timedOut).length,
|
|
295
|
+
running: results.filter((r) => r.ok && r.timedOut).length,
|
|
296
|
+
failed: results.filter((r) => !r.ok).length,
|
|
297
|
+
results,
|
|
298
|
+
};
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
tail: ({ thread, agent, jobId, cwd, topic, mode = 'summary' } = {}) => {
|
|
302
|
+
let key = thread ?? agent ?? null;
|
|
303
|
+
if (!key && cwd && topic) {
|
|
304
|
+
key = threads.findThread({ cwd, topic })?.key ?? null;
|
|
305
|
+
}
|
|
306
|
+
if (key) return tailSnapshot(getAgent(key), jobId, mode);
|
|
307
|
+
if (!jobId) throw new Error('thread, cwd+topic, or jobId is required');
|
|
308
|
+
const owner = findJobOwner(jobId);
|
|
309
|
+
if (!owner) return null;
|
|
310
|
+
return tailSnapshot(owner, jobId, mode);
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
wait: async ({ jobIds = null, timeoutMs = 60_000, stallWarnMs = 120_000 } = {}) => {
|
|
314
|
+
const watchSet = Array.isArray(jobIds) && jobIds.length ? new Set(jobIds.map(String)) : null;
|
|
315
|
+
|
|
316
|
+
const checkImmediate = () => {
|
|
317
|
+
if (watchSet) {
|
|
318
|
+
for (const jid of watchSet) {
|
|
319
|
+
const owner = findJobOwner(jid);
|
|
320
|
+
if (!owner) continue;
|
|
321
|
+
const snap = tailSnapshot(owner, jid, 'summary');
|
|
322
|
+
if (snap && !snap.running) {
|
|
323
|
+
const outcome = snap.metrics?.outcome ?? null;
|
|
324
|
+
const errMsg = snap.metrics?.error ?? null;
|
|
325
|
+
const failed = Boolean(errMsg) || outcome === 'failed' || outcome === 'cancelled' || outcome === 'error';
|
|
326
|
+
const authHint = failed && !String(errMsg ?? '').includes('登录提示(') ? formatAuthHint(owner.type, errMsg) : null;
|
|
327
|
+
return {
|
|
328
|
+
reason: failed ? 'error' : 'done',
|
|
329
|
+
jobId: jid,
|
|
330
|
+
thread: owner.name,
|
|
331
|
+
cli: owner.type ?? null,
|
|
332
|
+
...(failed ? { error: errMsg || outcome || 'failed' } : {}),
|
|
333
|
+
...(authHint ? { authHint } : {}),
|
|
334
|
+
snapshot: snap,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const active = [...watchSet].filter((jid) => {
|
|
339
|
+
if (inflightJobs.has(jid)) return true;
|
|
340
|
+
const o = findJobOwner(jid);
|
|
341
|
+
return o && (o.run?.jobId === jid || o.waitingJobs().some((w) => w.jobId === jid));
|
|
342
|
+
});
|
|
343
|
+
if (active.length === 0) return { reason: 'empty', jobIds: [...watchSet] };
|
|
344
|
+
} else {
|
|
345
|
+
const running = [...pool.values()].filter((a) => a.run);
|
|
346
|
+
if (running.length === 0 && inflightJobs.size === 0) return { reason: 'empty', jobIds: [] };
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const immediate = checkImmediate();
|
|
352
|
+
if (immediate) return immediate;
|
|
353
|
+
|
|
354
|
+
return new Promise((resolve) => {
|
|
355
|
+
let done = false;
|
|
356
|
+
let off = () => {};
|
|
357
|
+
let timeoutTimer = null;
|
|
358
|
+
let pollTimer = null;
|
|
359
|
+
|
|
360
|
+
const finish = (payload) => {
|
|
361
|
+
if (done) return;
|
|
362
|
+
done = true;
|
|
363
|
+
off();
|
|
364
|
+
if (timeoutTimer) clearTimeout(timeoutTimer);
|
|
365
|
+
if (pollTimer) clearInterval(pollTimer);
|
|
366
|
+
resolve(payload);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
off = onEvent((ev) => {
|
|
370
|
+
if (ev.event !== 'job.done') return;
|
|
371
|
+
if (watchSet && !watchSet.has(ev.jobId)) return;
|
|
372
|
+
const owner = findJobOwner(ev.jobId);
|
|
373
|
+
const cliName = ev.cli ?? owner?.type ?? null;
|
|
374
|
+
const authHint = ev.error && !String(ev.error).includes('登录提示(') ? formatAuthHint(cliName, ev.error) : null;
|
|
375
|
+
finish({
|
|
376
|
+
reason: ev.error ? 'error' : 'done',
|
|
377
|
+
jobId: ev.jobId,
|
|
378
|
+
thread: ev.agent ?? null,
|
|
379
|
+
cli: cliName,
|
|
380
|
+
error: ev.error ?? null,
|
|
381
|
+
...(authHint ? { authHint } : {}),
|
|
382
|
+
snapshot: owner ? tailSnapshot(owner, ev.jobId, 'summary') : null,
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
const warnLimit = Number(stallWarnMs);
|
|
387
|
+
if (Number.isFinite(warnLimit) && warnLimit > 0) {
|
|
388
|
+
pollTimer = setInterval(() => {
|
|
389
|
+
const candidates = watchSet
|
|
390
|
+
? [...watchSet].map((jid) => ({ jid, owner: findJobOwner(jid) })).filter((x) => x.owner?.run?.jobId === x.jid)
|
|
391
|
+
: [...pool.values()].filter((a) => a.run).map((owner) => ({ jid: owner.run.jobId, owner }));
|
|
392
|
+
for (const { jid, owner } of candidates) {
|
|
393
|
+
const snap = tailSnapshot(owner, jid, 'tools');
|
|
394
|
+
if (snap && snap.running && Math.max(snap.idleMs ?? 0, snap.openToolMs ?? 0) >= warnLimit) {
|
|
395
|
+
finish({ reason: 'stall', jobId: jid, thread: owner.name, snapshot: snap });
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}, 2_000);
|
|
400
|
+
pollTimer.unref?.();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const limit = Math.max(100, Number(timeoutMs) || 60_000);
|
|
404
|
+
timeoutTimer = setTimeout(() => {
|
|
405
|
+
const active = [...pool.values()]
|
|
406
|
+
.filter((a) => a.run && (!watchSet || watchSet.has(a.run.jobId)))
|
|
407
|
+
.map((a) => tailSnapshot(a, a.run.jobId, 'tools'));
|
|
408
|
+
finish({ reason: 'timeout', active });
|
|
409
|
+
}, limit);
|
|
410
|
+
});
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
}
|