@aragon-agent/cli 0.6.56 → 0.6.58
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 +94 -0
- package/README.md +159 -3
- package/dist/agent/controller.d.ts +112 -1
- package/dist/agent/controller.js +350 -7
- package/dist/agent/project-guidance-prompt.js +97 -97
- package/dist/agent/redteam-prompt.d.ts +29 -11
- package/dist/agent/redteam-prompt.js +118 -103
- package/dist/agent/redteam-wiring.d.ts +117 -2
- package/dist/agent/redteam-wiring.js +204 -1
- package/dist/agent/team-notices.d.ts +68 -0
- package/dist/agent/team-notices.js +82 -0
- package/dist/app/App.js +208 -5
- package/dist/app/use-redteam-mode.d.ts +1 -1
- package/dist/app/use-redteam-mode.js +14 -1
- package/dist/cli.js +13 -0
- package/dist/commands/catalog.js +6 -2
- package/dist/commands/redteam-command.d.ts +14 -8
- package/dist/commands/redteam-command.js +243 -12
- package/dist/commands/team-command.js +93 -2
- package/dist/config/app-paths.d.ts +19 -0
- package/dist/config/app-paths.js +23 -0
- package/dist/config/load.d.ts +8 -0
- package/dist/gene/catalog-sync.js +37 -16
- package/dist/gene/curator/budget.js +1 -1
- package/dist/gene/curator/curator.d.ts +6 -0
- package/dist/gene/curator/curator.js +8 -0
- package/dist/gene/curator/recheck.js +2 -1
- package/dist/gene/curator/task-runner.js +4 -1
- package/dist/gene/curator/tool-support.js +16 -7
- package/dist/gene/curator/tools-verify.js +54 -1
- package/dist/gene/curator/tools-write.js +41 -1
- package/dist/gene/curator/validate/evidence-tokens.d.ts +7 -19
- package/dist/gene/curator/validate/evidence-tokens.js +174 -17
- package/dist/gene/curator/validate/knowledge.d.ts +22 -1
- package/dist/gene/curator/validate/knowledge.js +108 -25
- package/dist/gene/curator/validate/live-check.js +7 -1
- package/dist/gene/curator/validate/prompts.js +7 -3
- package/dist/gene/curator/validate/types.d.ts +6 -1
- package/dist/gene/learn/learn-call.d.ts +58 -5
- package/dist/gene/learn/learn-call.js +38 -30
- package/dist/gene/learn/learn-prompt.d.ts +18 -2
- package/dist/gene/learn/learn-prompt.js +29 -11
- package/dist/gene/learn/learner.d.ts +6 -0
- package/dist/gene/learn/learner.js +31 -9
- package/dist/gene/learn/proposal-build.js +2 -6
- package/dist/gene/wiring-models.js +4 -1
- package/dist/governance/engine/lifecycle.js +1 -1
- package/dist/governance/store/run-lock.d.ts +9 -2
- package/dist/governance/store/run-lock.js +91 -7
- package/dist/host/controller-factory.d.ts +13 -0
- package/dist/host/controller-factory.js +7 -0
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/host/interaction.d.ts +19 -0
- package/dist/host/interaction.js +75 -0
- package/dist/host/team-control.d.ts +83 -0
- package/dist/host/team-control.js +55 -0
- package/dist/logging/install.d.ts +32 -1
- package/dist/logging/install.js +21 -0
- package/dist/proc/output-streams.d.ts +11 -0
- package/dist/proc/output-streams.js +11 -0
- package/dist/redteam/prompt-resolve.d.ts +61 -0
- package/dist/redteam/prompt-resolve.js +303 -0
- package/dist/redteam/prompt-store.d.ts +96 -0
- package/dist/redteam/prompt-store.js +156 -0
- package/dist/redteam/prompt-template.d.ts +22 -0
- package/dist/redteam/prompt-template.js +72 -0
- package/dist/team/bus.d.ts +18 -0
- package/dist/team/bus.js +70 -18
- package/dist/team/console-tools.d.ts +54 -0
- package/dist/team/console-tools.js +333 -0
- package/dist/team/console-view.d.ts +88 -0
- package/dist/team/console-view.js +173 -0
- package/dist/team/forced-mode.d.ts +61 -1
- package/dist/team/forced-mode.js +75 -0
- package/dist/team/normalize.d.ts +31 -0
- package/dist/team/normalize.js +49 -5
- package/dist/team/notices.d.ts +95 -0
- package/dist/team/notices.js +121 -0
- package/dist/team/pause-gate.d.ts +79 -0
- package/dist/team/pause-gate.js +127 -0
- package/dist/team/prompt.d.ts +25 -9
- package/dist/team/prompt.js +29 -29
- package/dist/team/report.js +33 -0
- package/dist/team/runtime.d.ts +270 -5
- package/dist/team/runtime.js +732 -64
- package/dist/team/settled.d.ts +5 -2
- package/dist/team/settled.js +12 -2
- package/dist/team/subagent.d.ts +7 -0
- package/dist/team/subagent.js +12 -1
- package/dist/team/task-tool.d.ts +39 -1
- package/dist/team/task-tool.js +75 -18
- package/dist/tools/bash-tool.d.ts +11 -8
- package/dist/tools/bash-tool.js +37 -9
- package/dist/tools/index.d.ts +2 -2
- package/dist/tools/index.js +30 -0
- package/package.json +3 -3
package/dist/team/runtime.js
CHANGED
|
@@ -27,13 +27,16 @@
|
|
|
27
27
|
* nobody. Without a supervisor, every path below is byte for byte the
|
|
28
28
|
* pre-feature behaviour.
|
|
29
29
|
*/
|
|
30
|
-
import { TeamBus } from './bus.js';
|
|
30
|
+
import { TeamBus, LEAD_KEY } from './bus.js';
|
|
31
31
|
import { accumulateUsage } from '../fast/review-call.js';
|
|
32
32
|
import { settledRun } from './settled.js';
|
|
33
33
|
import { appendTimeline, withSettleEntry } from '@aragon-agent/tui/model/team/timeline';
|
|
34
34
|
import { TEAM_AGGREGATE_LABEL, TEAM_LIMITS } from '@aragon-agent/tui/model/team/limits';
|
|
35
35
|
import { TeamOverseer, createOverseerUsageCursor, buildReplacementPrompt, nextOverseerLadderStepMs, resolveOverseerFirstCheckMs, } from './overseer.js';
|
|
36
36
|
import { shouldRetryColdStart } from './retry.js';
|
|
37
|
+
import { PauseGate } from './pause-gate.js';
|
|
38
|
+
import { normalizeSubagentSpecs, dedupeLabel } from './normalize.js';
|
|
39
|
+
import { buildDispatchReport } from './report.js';
|
|
37
40
|
import { createSubagent, defaultSubagentFactory, } from './subagent.js';
|
|
38
41
|
/**
|
|
39
42
|
* The pseudo-label aggregate supervisor notices travel under (D-5 / R-P1-6):
|
|
@@ -56,6 +59,33 @@ const DEGRADED_WAIT_DECISION = {
|
|
|
56
59
|
action: 'wait',
|
|
57
60
|
reason: 'fast tier unavailable; waiting unassisted',
|
|
58
61
|
};
|
|
62
|
+
/** The three phases after which a child has no loop left (`paused` is NOT one). */
|
|
63
|
+
function isTerminal(phase) {
|
|
64
|
+
return phase === 'done' || phase === 'failed' || phase === 'aborted';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* One sentence for a refusal the BUS made, in the model's terms.
|
|
68
|
+
*
|
|
69
|
+
* A refusal is not an error and must not read like one (bus D-7): each branch
|
|
70
|
+
* names what happened and what to do instead, because the model can act on all
|
|
71
|
+
* five.
|
|
72
|
+
*/
|
|
73
|
+
function describeSendRefusal(verdict, to) {
|
|
74
|
+
switch (verdict.reason) {
|
|
75
|
+
case 'unknown_recipient':
|
|
76
|
+
return `no subagent labelled "${to.trim()}" in this dispatch`;
|
|
77
|
+
case 'recipient_finished':
|
|
78
|
+
return 'that subagent has already finished and will not read it';
|
|
79
|
+
case 'agent_quota':
|
|
80
|
+
return `the lead's message budget is spent (${verdict.used ?? 0}/${verdict.max ?? 0})`;
|
|
81
|
+
case 'dispatch_quota':
|
|
82
|
+
return `the team has spent its whole message budget (${verdict.used ?? 0}/${verdict.max ?? 0})`;
|
|
83
|
+
case 'too_soon':
|
|
84
|
+
return `one message every ${Math.round(TEAM_LIMITS.minSendIntervalMs / 1000)}s to the same subagent; try again in ${verdict.retryInSeconds ?? 1}s`;
|
|
85
|
+
default:
|
|
86
|
+
return 'the message could not be delivered';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
59
89
|
/**
|
|
60
90
|
* The per-child SILENCE windows (team-overseer §3.2; subagent-overseer-v2
|
|
61
91
|
* keeps only this half). Timers only — no state, no decisions; the runtime
|
|
@@ -295,7 +325,7 @@ class MonitorLoop {
|
|
|
295
325
|
export class TeamRuntime {
|
|
296
326
|
deps;
|
|
297
327
|
listeners = new Set();
|
|
298
|
-
|
|
328
|
+
liveHandles = [];
|
|
299
329
|
bus = null;
|
|
300
330
|
snapshotState = null;
|
|
301
331
|
busy = false;
|
|
@@ -341,6 +371,28 @@ export class TeamRuntime {
|
|
|
341
371
|
/** Edge-latched "a tick ran unassisted" (D-10), per dispatch. */
|
|
342
372
|
overseerDegraded = false;
|
|
343
373
|
overseerDegradedTicks = 0;
|
|
374
|
+
/**
|
|
375
|
+
* Labels a HUMAN (the lead or the user) stopped, for the current dispatch.
|
|
376
|
+
*
|
|
377
|
+
* WHY NOT JUST ASK THE VERDICT (AC-8). The supervisor's abandon and a human's
|
|
378
|
+
* stop share one abort path - that is the point of `abandonChild` - but they do
|
|
379
|
+
* NOT mean the same thing in the report: "the supervisor abandoned it" is a
|
|
380
|
+
* failure with a reason, while "the user stopped it" is an aborted child whose
|
|
381
|
+
* work simply ended. Collapsing both into `failed` would make a deliberate stop
|
|
382
|
+
* read as a defect, which is precisely the confusion the Control section exists
|
|
383
|
+
* to remove.
|
|
384
|
+
*/
|
|
385
|
+
stoppedByHuman = new Set();
|
|
386
|
+
/**
|
|
387
|
+
* The dispatch currently owned by this runtime, or the last one that settled
|
|
388
|
+
* (team-detached-dispatch section 8.1).
|
|
389
|
+
*
|
|
390
|
+
* KEPT AFTER SETTLEMENT ON PURPOSE: `team_collect` must be able to answer
|
|
391
|
+
* "it finished, here is the report" for a while after the children are gone,
|
|
392
|
+
* and a dispatch that settled inside its own turn has no notice channel at
|
|
393
|
+
* all. It is replaced by the next dispatch, so this holds at most one.
|
|
394
|
+
*/
|
|
395
|
+
session = null;
|
|
344
396
|
constructor(deps) {
|
|
345
397
|
this.deps = deps;
|
|
346
398
|
}
|
|
@@ -361,50 +413,558 @@ export class TeamRuntime {
|
|
|
361
413
|
return this.snapshotState;
|
|
362
414
|
}
|
|
363
415
|
// -------------------------------------------------------------------------
|
|
416
|
+
// The lead console (team-detached-dispatch section 5.2)
|
|
417
|
+
//
|
|
418
|
+
// EVERY method here is a PUBLIC, NARROW operation - the tools never touch a
|
|
419
|
+
// handle or a private field. That is what keeps the audit single-sourced
|
|
420
|
+
// (`recordControl`) and the mutation single-sourced (`abandonChild` /
|
|
421
|
+
// `PauseGate`), which are the two things section 5.2.1 forbids a console tool
|
|
422
|
+
// from reimplementing.
|
|
423
|
+
// -------------------------------------------------------------------------
|
|
424
|
+
/** Everything `team_status`, `team_collect` and `/team status` render. */
|
|
425
|
+
inspect() {
|
|
426
|
+
const session = this.session;
|
|
427
|
+
if (session === null)
|
|
428
|
+
return null;
|
|
429
|
+
const now = Date.now();
|
|
430
|
+
const counts = {
|
|
431
|
+
queued: 0,
|
|
432
|
+
running: 0,
|
|
433
|
+
paused: 0,
|
|
434
|
+
done: 0,
|
|
435
|
+
failed: 0,
|
|
436
|
+
aborted: 0,
|
|
437
|
+
};
|
|
438
|
+
const children = session.handles.map((handle) => {
|
|
439
|
+
const { run } = handle;
|
|
440
|
+
switch (run.phase) {
|
|
441
|
+
case 'queued':
|
|
442
|
+
counts.queued += 1;
|
|
443
|
+
break;
|
|
444
|
+
case 'paused':
|
|
445
|
+
counts.paused += 1;
|
|
446
|
+
break;
|
|
447
|
+
case 'done':
|
|
448
|
+
counts.done += 1;
|
|
449
|
+
break;
|
|
450
|
+
case 'failed':
|
|
451
|
+
counts.failed += 1;
|
|
452
|
+
break;
|
|
453
|
+
case 'aborted':
|
|
454
|
+
counts.aborted += 1;
|
|
455
|
+
break;
|
|
456
|
+
default:
|
|
457
|
+
counts.running += 1;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
const pausedMs = session.pauseGate.pausedMs(run.label);
|
|
461
|
+
const by = session.pauseGate.holderOf(run.label);
|
|
462
|
+
return {
|
|
463
|
+
label: run.label,
|
|
464
|
+
description: run.description,
|
|
465
|
+
phase: run.phase,
|
|
466
|
+
elapsedMs: now - (run.startedAt ?? session.startedAt),
|
|
467
|
+
turns: run.turns,
|
|
468
|
+
toolCalls: run.toolCalls,
|
|
469
|
+
...(run.lastTool !== undefined ? { lastTool: run.lastTool } : {}),
|
|
470
|
+
...(pausedMs > 0 ? { pausedMs } : {}),
|
|
471
|
+
...(by !== undefined ? { pausedBy: by } : {}),
|
|
472
|
+
...(run.error !== undefined ? { error: run.error } : {}),
|
|
473
|
+
...(run.summary !== undefined ? { summary: run.summary } : {}),
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
return {
|
|
477
|
+
dispatchId: session.id,
|
|
478
|
+
live: session.state === 'live',
|
|
479
|
+
startedAt: session.startedAt,
|
|
480
|
+
requested: session.requested,
|
|
481
|
+
ceilingMs: session.ceilingMs,
|
|
482
|
+
elapsedMs: now - session.startedAt,
|
|
483
|
+
counts,
|
|
484
|
+
children,
|
|
485
|
+
mail: session.bus?.leadMail() ?? [],
|
|
486
|
+
controls: [...session.controls],
|
|
487
|
+
...(session.reportText !== undefined ? { reportText: session.reportText } : {}),
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Hold one child (section 5.2.4). The RESULT says what will actually happen,
|
|
492
|
+
* because "paused" is cooperative: `paused` when the child is between tools,
|
|
493
|
+
* `deferred` when it is mid-tool or mid-text, `finished` when it has already
|
|
494
|
+
* settled.
|
|
495
|
+
*/
|
|
496
|
+
pauseChild(label, by, reason) {
|
|
497
|
+
const session = this.requireLive();
|
|
498
|
+
if ('ok' in session)
|
|
499
|
+
return session;
|
|
500
|
+
const handle = session.handles.find((h) => h.run.label === label);
|
|
501
|
+
if (handle === undefined)
|
|
502
|
+
return { ok: false, why: `no subagent labelled "${label}"` };
|
|
503
|
+
if (isTerminal(handle.run.phase)) {
|
|
504
|
+
return { ok: true, effect: 'finished' };
|
|
505
|
+
}
|
|
506
|
+
const fresh = session.pauseGate.pause(label, by);
|
|
507
|
+
const midTool = handle.run.phase === 'tool';
|
|
508
|
+
handle.run.phase = 'paused';
|
|
509
|
+
handle.run.pausedAt = session.pauseGate.pausedSince(label) ?? Date.now();
|
|
510
|
+
handle.run.pauseCount = session.pauseGate.pauseCount(label);
|
|
511
|
+
handle.run.controlBy = by;
|
|
512
|
+
this.recordControl(session, 'pause', {
|
|
513
|
+
label,
|
|
514
|
+
by,
|
|
515
|
+
...(reason !== undefined ? { reason } : {}),
|
|
516
|
+
});
|
|
517
|
+
// The supervisor must not read "the user stopped it" as "it is stuck" (R2).
|
|
518
|
+
this.supervision?.clear(label);
|
|
519
|
+
this.monitor?.clear(label);
|
|
520
|
+
this.publish(session.id, handle.run);
|
|
521
|
+
return { ok: true, effect: fresh && !midTool ? 'paused' : 'deferred' };
|
|
522
|
+
}
|
|
523
|
+
/** Release one child (section 5.2.4). */
|
|
524
|
+
resumeChild(label, by, message) {
|
|
525
|
+
const session = this.requireLive();
|
|
526
|
+
if ('ok' in session)
|
|
527
|
+
return session;
|
|
528
|
+
const handle = session.handles.find((h) => h.run.label === label);
|
|
529
|
+
if (handle === undefined)
|
|
530
|
+
return { ok: false, why: `no subagent labelled "${label}"` };
|
|
531
|
+
const wasHeld = session.pauseGate.isPaused(label);
|
|
532
|
+
const totalMs = session.pauseGate.pausedMs(label);
|
|
533
|
+
const counts = session.pauseGate.pauseCount(label);
|
|
534
|
+
session.pauseGate.resume(label);
|
|
535
|
+
if (wasHeld) {
|
|
536
|
+
delete handle.run.pausedAt;
|
|
537
|
+
delete handle.run.controlBy;
|
|
538
|
+
handle.run.pausedMs = totalMs;
|
|
539
|
+
handle.run.pauseCount = counts;
|
|
540
|
+
// Back to the phase it was on, never to `starting`: the child's loop never
|
|
541
|
+
// stopped, so pretending it is starting again would replay a tool.
|
|
542
|
+
handle.run.phase = 'thinking';
|
|
543
|
+
this.supervision?.rearmSilence(label);
|
|
544
|
+
this.monitor?.reset(label);
|
|
545
|
+
this.publish(session.id, handle.run);
|
|
546
|
+
}
|
|
547
|
+
if (message !== undefined && message.trim().length > 0 && !handle.isAborted()) {
|
|
548
|
+
handle.agent.steer(`Resuming: ${message.trim()}`);
|
|
549
|
+
}
|
|
550
|
+
this.recordControl(session, 'resume', {
|
|
551
|
+
label,
|
|
552
|
+
by,
|
|
553
|
+
...(message !== undefined ? { reason: message } : {}),
|
|
554
|
+
});
|
|
555
|
+
return { ok: true, effect: wasHeld ? 'resumed' : 'was-running' };
|
|
556
|
+
}
|
|
557
|
+
/** Stop one child, or every child, through the ONE abandon path (section 5.2.5). */
|
|
558
|
+
stopChild(label, by, reason) {
|
|
559
|
+
const session = this.session;
|
|
560
|
+
if (session === null || session.state !== 'live')
|
|
561
|
+
return { stopped: [], live: [] };
|
|
562
|
+
const why = reason?.trim() || `stopped by ${by}`;
|
|
563
|
+
if (label === 'all') {
|
|
564
|
+
const stopped = session.handles
|
|
565
|
+
.filter((handle) => !isTerminal(handle.run.phase))
|
|
566
|
+
.map((handle) => handle.run.label);
|
|
567
|
+
this.recordControl(session, 'stop', { by, reason: why });
|
|
568
|
+
this.abortAll();
|
|
569
|
+
return { stopped, live: [] };
|
|
570
|
+
}
|
|
571
|
+
const handle = session.handles.find((h) => h.run.label === label);
|
|
572
|
+
if (handle === undefined) {
|
|
573
|
+
// A misspelt label is not "nothing to do": the caller must be able to say which
|
|
574
|
+
// names exist, or a model retries the same typo until its budget runs out.
|
|
575
|
+
return { stopped: [], live: session.handles.map((h) => h.run.label), unknown: true };
|
|
576
|
+
}
|
|
577
|
+
if (isTerminal(handle.run.phase)) {
|
|
578
|
+
return { stopped: [], live: session.handles.map((h) => h.run.label) };
|
|
579
|
+
}
|
|
580
|
+
this.abandonChild(handle, why, by);
|
|
581
|
+
this.recordControl(session, 'stop', { label, by, reason: why });
|
|
582
|
+
return {
|
|
583
|
+
stopped: [label],
|
|
584
|
+
live: session.handles
|
|
585
|
+
.filter((h) => !isTerminal(h.run.phase) && h.run.label !== label)
|
|
586
|
+
.map((h) => h.run.label),
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Send a message from the LEAD to one child or to everyone (section 5.2.3).
|
|
591
|
+
*
|
|
592
|
+
* THE QUOTA IS THE LEAD'S OWN (`messagesFromLeadPerDispatch`), and the interval
|
|
593
|
+
* window is keyed `lead:to` inside the bus (REV-17): one "the user changed the
|
|
594
|
+
* goal" usually has to reach two or three children, and a per-sender window
|
|
595
|
+
* would make the second and third impossible for 15 seconds each.
|
|
596
|
+
*/
|
|
597
|
+
sendFromLead(to, body, options = {}) {
|
|
598
|
+
const session = this.requireLive();
|
|
599
|
+
if ('ok' in session)
|
|
600
|
+
return session;
|
|
601
|
+
if (session.bus === null)
|
|
602
|
+
return { ok: false, why: 'the message channel is not open yet' };
|
|
603
|
+
const quota = TEAM_LIMITS.messagesFromLeadPerDispatch;
|
|
604
|
+
if (session.leadSent >= quota) {
|
|
605
|
+
return {
|
|
606
|
+
ok: false,
|
|
607
|
+
why: `the lead has spent its ${quota} messages for this dispatch`,
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
const subject = options.subject?.trim() || 'from lead';
|
|
611
|
+
const verdict = session.bus.send(LEAD_KEY, to, subject, body);
|
|
612
|
+
if (!verdict.ok) {
|
|
613
|
+
return { ok: false, why: describeSendRefusal(verdict, to) };
|
|
614
|
+
}
|
|
615
|
+
session.leadSent += 1;
|
|
616
|
+
// `interrupt` is the difference between "read this at your next tool result"
|
|
617
|
+
// and "read this before your next request". Both are safe; only the second
|
|
618
|
+
// spends a steer, so it is refused for a held or aborted child - the same
|
|
619
|
+
// guard the supervisor's nudge uses (I-OV4).
|
|
620
|
+
if (options.interrupt !== false) {
|
|
621
|
+
for (const label of verdict.delivered) {
|
|
622
|
+
const handle = session.handles.find((h) => h.run.label === label);
|
|
623
|
+
if (handle === undefined || handle.isAborted())
|
|
624
|
+
continue;
|
|
625
|
+
if (session.pauseGate.isPaused(label))
|
|
626
|
+
continue;
|
|
627
|
+
handle.agent.steer(body);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
this.recordControl(session, 'message', {
|
|
631
|
+
label: to,
|
|
632
|
+
by: options.by ?? 'lead',
|
|
633
|
+
...(options.subject !== undefined ? { reason: subject } : {}),
|
|
634
|
+
});
|
|
635
|
+
return { ok: true, delivered: verdict.delivered, remaining: quota - session.leadSent };
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Add specs to a LIVE dispatch (section 5.2.6).
|
|
639
|
+
*
|
|
640
|
+
* THE CAP APPLIES TO THE TOTAL, NOT TO THE ADDITION: `remaining` is measured
|
|
641
|
+
* against what this dispatch already created, so `team_spawn` cannot become a
|
|
642
|
+
* back door around `hardMaxSubagents`. New children take the next free worker
|
|
643
|
+
* slot through the same pool as the first batch, and a child that finds no slot
|
|
644
|
+
* waits in `queued` exactly as its siblings did.
|
|
645
|
+
*/
|
|
646
|
+
addSpecs(raw, by, reason) {
|
|
647
|
+
const session = this.requireLive();
|
|
648
|
+
if ('ok' in session)
|
|
649
|
+
return session;
|
|
650
|
+
const ceiling = Math.max(1, Math.min(this.deps.getConfig().team.maxSubagents, TEAM_LIMITS.hardMaxSubagents));
|
|
651
|
+
const remaining = Math.max(0, ceiling - session.specs.length);
|
|
652
|
+
if (remaining === 0) {
|
|
653
|
+
return {
|
|
654
|
+
ok: false,
|
|
655
|
+
why: `this dispatch already has ${session.specs.length} subagents, the most ` +
|
|
656
|
+
`${ceiling} it may have. Stop or wait for one, then add.`,
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
const taken = new Set(session.specs.map((spec) => spec.label.toLowerCase()));
|
|
660
|
+
const normalized = normalizeSubagentSpecs(raw, remaining);
|
|
661
|
+
if (normalized.specs.length === 0) {
|
|
662
|
+
return { ok: false, why: 'no usable subagent specs: each needs a description and a prompt' };
|
|
663
|
+
}
|
|
664
|
+
// THE LABELS MUST BE UNIQUE ACROSS THE WHOLE DISPATCH, not just within the
|
|
665
|
+
// new batch: `normalizeSubagentSpecs` seeds only the reserved aggregate label
|
|
666
|
+
// (it has no way to know what already exists). `dedupeLabel` is the same
|
|
667
|
+
// suffixer the first batch used, exported for exactly this caller.
|
|
668
|
+
const unique = normalized.specs.map((spec) => ({
|
|
669
|
+
...spec,
|
|
670
|
+
label: dedupeLabel(spec.label, taken),
|
|
671
|
+
}));
|
|
672
|
+
const added = this.spawnChildren(session, unique);
|
|
673
|
+
this.recordControl(session, 'spawn', {
|
|
674
|
+
by,
|
|
675
|
+
...(reason !== undefined ? { reason } : {}),
|
|
676
|
+
});
|
|
677
|
+
return { added, dropped: Math.max(0, normalized.requested - normalized.specs.length), requested: normalized.requested };
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Create the added children and hand them to the live pool.
|
|
681
|
+
*
|
|
682
|
+
* THE POOL IS RE-ENTERED, NOT REBUILT: `this.liveHandles` is the array the outcome is
|
|
683
|
+
* built from and `worker()` walks it by index, so appending is enough - a new
|
|
684
|
+
* child that finds every worker busy stays `queued` until one finishes, which is
|
|
685
|
+
* exactly how the first batch behaves.
|
|
686
|
+
*/
|
|
687
|
+
spawnChildren(session, specs) {
|
|
688
|
+
const makeHandle = this.makeHandleForSession;
|
|
689
|
+
if (makeHandle === null)
|
|
690
|
+
return [];
|
|
691
|
+
const created = [];
|
|
692
|
+
for (const spec of specs) {
|
|
693
|
+
const handle = makeHandle(spec);
|
|
694
|
+
session.handles.push(handle);
|
|
695
|
+
session.specs.push(spec);
|
|
696
|
+
session.bus?.addParticipant(spec.label);
|
|
697
|
+
this.liveHandles = session.handles;
|
|
698
|
+
created.push(handle);
|
|
699
|
+
this.publish(session.id, handle.run);
|
|
700
|
+
}
|
|
701
|
+
session.requested += specs.length;
|
|
702
|
+
if (this.snapshotState !== null && this.snapshotState.dispatchId === session.id) {
|
|
703
|
+
this.snapshotState = {
|
|
704
|
+
...this.snapshotState,
|
|
705
|
+
requested: session.requested,
|
|
706
|
+
runs: session.handles.map((handle) => ({ ...handle.run })),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
// Hand each new child to the pool. Every spawned worker exits when it walks
|
|
710
|
+
// past the end of the array, so the pool never grows permanently.
|
|
711
|
+
const cursor = { next: session.handles.length - created.length };
|
|
712
|
+
const runCtx = this.liveRunCtx;
|
|
713
|
+
if (runCtx !== null) {
|
|
714
|
+
const workers = Array.from({ length: Math.max(1, Math.min(this.deps.getConfig().team.maxConcurrent, created.length)) }, () => this.worker(runCtx, cursor));
|
|
715
|
+
void Promise.allSettled(workers);
|
|
716
|
+
}
|
|
717
|
+
return created.map((handle) => handle.run.label);
|
|
718
|
+
}
|
|
719
|
+
/** Audit one control action on the session, the event stream and the run. */
|
|
720
|
+
recordControl(session, action, detail) {
|
|
721
|
+
const at = Date.now();
|
|
722
|
+
session.controls.push({
|
|
723
|
+
action,
|
|
724
|
+
...(detail.label !== undefined ? { label: detail.label } : {}),
|
|
725
|
+
by: detail.by,
|
|
726
|
+
...(detail.reason !== undefined ? { reason: detail.reason } : {}),
|
|
727
|
+
at,
|
|
728
|
+
});
|
|
729
|
+
// The report's Control section reads the SAME records the interventions ledger
|
|
730
|
+
// does, because "who paused this child" is one fact with two readers.
|
|
731
|
+
if (detail.label !== undefined) {
|
|
732
|
+
this.interventions.push({
|
|
733
|
+
label: detail.label,
|
|
734
|
+
at,
|
|
735
|
+
action: action === 'stop' ? 'abandon' : 'wait',
|
|
736
|
+
reason: `${action} by ${detail.by}${detail.reason ? `: ${detail.reason}` : ''}`,
|
|
737
|
+
trigger: detail.by === 'supervisor' ? 'clock' : detail.by,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
this.emitControl(session, action, detail);
|
|
741
|
+
}
|
|
742
|
+
emitControl(session, action, detail) {
|
|
743
|
+
this.emit({
|
|
744
|
+
type: 'control',
|
|
745
|
+
dispatchId: session.id,
|
|
746
|
+
action,
|
|
747
|
+
...(detail.label !== undefined ? { label: detail.label } : {}),
|
|
748
|
+
by: detail.by,
|
|
749
|
+
...(detail.reason !== undefined ? { reason: detail.reason } : {}),
|
|
750
|
+
at: Date.now(),
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
/** The live session, or the refusal every control tool shares. */
|
|
754
|
+
requireLive() {
|
|
755
|
+
const session = this.session;
|
|
756
|
+
if (session === null || session.state !== 'live') {
|
|
757
|
+
return { ok: false, why: 'No team dispatch is running. Use task to start one.' };
|
|
758
|
+
}
|
|
759
|
+
return session;
|
|
760
|
+
}
|
|
761
|
+
/** The `makeHandle` closure of the live dispatch, for `team_spawn`. */
|
|
762
|
+
makeHandleForSession = null;
|
|
763
|
+
/** The run context of the live dispatch, so spawned children join the same pool. */
|
|
764
|
+
liveRunCtx = null;
|
|
765
|
+
// -------------------------------------------------------------------------
|
|
364
766
|
// Dispatch
|
|
365
767
|
// -------------------------------------------------------------------------
|
|
366
768
|
async dispatch(specs, requested, signal) {
|
|
769
|
+
const session = this.beginSession(specs, requested);
|
|
367
770
|
try {
|
|
368
|
-
return await this.runDispatch(
|
|
771
|
+
return await this.runDispatch(session, { mode: 'await', signal });
|
|
369
772
|
}
|
|
370
773
|
catch (error) {
|
|
371
774
|
// A throw out of the dispatch body (a child that cannot be constructed) must not
|
|
372
775
|
// leave `busy` latched: every later `task` call, `/team` and busy-guarded command
|
|
373
|
-
// of the session would answer "a dispatch is already running".
|
|
374
|
-
// tears down inline and never reaches this.
|
|
375
|
-
this.resetDispatchState();
|
|
776
|
+
// of the session would answer "a dispatch is already running".
|
|
376
777
|
throw error;
|
|
377
778
|
}
|
|
779
|
+
finally {
|
|
780
|
+
// ONE EXIT FOR BOTH OUTCOMES, and it is the reason this path has a `finally` at
|
|
781
|
+
// all: the inline teardown that used to sit at the end of `runDispatch` moved
|
|
782
|
+
// with the floating promise, and a second copy of a teardown is how a latch
|
|
783
|
+
// leaks (R6). Without this, every BLOCKING dispatch left `busy` true for the
|
|
784
|
+
// rest of the session.
|
|
785
|
+
this.teardown(session);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Start a dispatch and return a handle IMMEDIATELY (team-detached-dispatch
|
|
790
|
+
* section 5.1.1).
|
|
791
|
+
*
|
|
792
|
+
* SYNCHRONOUS ON PURPOSE. The `task` tool returns this handle as its tool
|
|
793
|
+
* result within milliseconds, which is what frees the lead: everything that
|
|
794
|
+
* used to happen before `dispatch()` resolved now happens after `start()` has
|
|
795
|
+
* already returned. `beginSession` is therefore synchronous too - the id, the
|
|
796
|
+
* turn id, the abort controller and the `busy` latch are all taken before the
|
|
797
|
+
* first await.
|
|
798
|
+
*
|
|
799
|
+
* THE FLOATING PROMISE IS SAFE ONLY BECAUSE OF `finally`. Nothing awaits it, so
|
|
800
|
+
* a throw would otherwise leave `busy` true for the rest of the session and
|
|
801
|
+
* every later `task` would be refused - the one failure mode (R6) that turns a
|
|
802
|
+
* feature into an unusable session. `.catch` marks the failure visible; the
|
|
803
|
+
* error itself has nowhere to go but the event stream, which is where a host
|
|
804
|
+
* reads it.
|
|
805
|
+
*/
|
|
806
|
+
start(specs, requested) {
|
|
807
|
+
const session = this.beginSession(specs, requested);
|
|
808
|
+
const handle = {
|
|
809
|
+
dispatchId: session.id,
|
|
810
|
+
labels: session.specs.map((spec) => spec.label),
|
|
811
|
+
requested,
|
|
812
|
+
startedAt: session.startedAt,
|
|
813
|
+
turnId: session.turnId,
|
|
814
|
+
};
|
|
815
|
+
void this.runDispatch(session, { mode: 'detach' })
|
|
816
|
+
.catch((error) => {
|
|
817
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
818
|
+
this.emitControl(session, 'stop', { by: 'supervisor', reason: `dispatch failed: ${reason}` });
|
|
819
|
+
})
|
|
820
|
+
.finally(() => this.teardown(session));
|
|
821
|
+
return handle;
|
|
822
|
+
}
|
|
823
|
+
/** The live dispatch handle, or `null`. The read surface behind `team_status`. */
|
|
824
|
+
live() {
|
|
825
|
+
const session = this.session;
|
|
826
|
+
if (session === null || session.state !== 'live')
|
|
827
|
+
return null;
|
|
828
|
+
return {
|
|
829
|
+
dispatchId: session.id,
|
|
830
|
+
labels: session.specs.map((spec) => spec.label),
|
|
831
|
+
requested: session.requested,
|
|
832
|
+
startedAt: session.startedAt,
|
|
833
|
+
turnId: session.turnId,
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
/** The settled outcome for `dispatchId`, when it is the one on record. */
|
|
837
|
+
outcome(dispatchId) {
|
|
838
|
+
const session = this.session;
|
|
839
|
+
return session !== null && session.id === dispatchId ? session.outcome ?? null : null;
|
|
840
|
+
}
|
|
841
|
+
/** The report text produced AT SETTLEMENT, or `undefined` while live (REV-05). */
|
|
842
|
+
reportText(dispatchId) {
|
|
843
|
+
const session = this.session;
|
|
844
|
+
return session !== null && session.id === dispatchId ? session.reportText : undefined;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* The settle facts the host needs to decide whether the lead gets a follow-up
|
|
848
|
+
* turn (`planSettleContinuation`, section 5.3.3).
|
|
849
|
+
*
|
|
850
|
+
* `settledInsideTurn` is COMPUTED AT SETTLEMENT from `turnId` equality plus
|
|
851
|
+
* `isLeadRunning()` (REV-04), never from a signal or a wall clock: both of those
|
|
852
|
+
* give the wrong answer in the case that most needs reporting - a dispatch that
|
|
853
|
+
* failed fast inside the very turn that started it, where the report has
|
|
854
|
+
* already arrived as a steering message.
|
|
855
|
+
*/
|
|
856
|
+
settleFacts(dispatchId) {
|
|
857
|
+
const session = this.session;
|
|
858
|
+
if (session === null || session.id !== dispatchId)
|
|
859
|
+
return null;
|
|
860
|
+
const outcome = session.outcome ?? null;
|
|
861
|
+
const errored = outcome !== null && outcome.runs.some((run) => run.phase === 'failed');
|
|
862
|
+
return {
|
|
863
|
+
settled: session.state === 'settled',
|
|
864
|
+
settledInsideTurn: session.settledInsideTurn,
|
|
865
|
+
continuedThisDispatch: session.continued,
|
|
866
|
+
aborted: outcome?.aborted === true,
|
|
867
|
+
errored,
|
|
868
|
+
outcome,
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
/** Record that the follow-up turn was spent. At most one per dispatch. */
|
|
872
|
+
markContinued(dispatchId) {
|
|
873
|
+
const session = this.session;
|
|
874
|
+
if (session !== null && session.id === dispatchId)
|
|
875
|
+
session.continued = true;
|
|
876
|
+
}
|
|
877
|
+
/** The id of the dispatch on record, live or settled. */
|
|
878
|
+
currentDispatchId() {
|
|
879
|
+
return this.session?.id ?? null;
|
|
880
|
+
}
|
|
881
|
+
/** Whether the live dispatch was started by the turn identified by `turnId`. */
|
|
882
|
+
belongsToTurn(turnId) {
|
|
883
|
+
return this.session !== null && this.session.state === 'live' && this.session.turnId === turnId;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Take everything a `task` call needs before its first await: the id, the
|
|
887
|
+
* clock, the turn, the abort controller and the `busy` latch.
|
|
888
|
+
*
|
|
889
|
+
* SPLIT OUT OF `runDispatch` (spec REV-04): `start()` cannot return an id it
|
|
890
|
+
* has not minted yet, so the minting has to be synchronous and separable.
|
|
891
|
+
*/
|
|
892
|
+
beginSession(specs, requested) {
|
|
893
|
+
this.counter += 1;
|
|
894
|
+
const config = this.deps.getConfig();
|
|
895
|
+
const session = {
|
|
896
|
+
id: `d${this.counter}`,
|
|
897
|
+
requested,
|
|
898
|
+
specs: [...specs],
|
|
899
|
+
startedAt: Date.now(),
|
|
900
|
+
turnId: this.deps.currentTurnId?.() ?? 0,
|
|
901
|
+
state: 'live',
|
|
902
|
+
controller: new AbortController(),
|
|
903
|
+
// SNAPSHOT, not a live read: `team.dispatchTimeoutMs` and `team.overseer`
|
|
904
|
+
// may change mid-dispatch and the ceiling must not move under it.
|
|
905
|
+
ceilingMs: config.team.overseer ? 0 : config.team.dispatchTimeoutMs,
|
|
906
|
+
settledInsideTurn: false,
|
|
907
|
+
continued: false,
|
|
908
|
+
leadSent: 0,
|
|
909
|
+
controls: [],
|
|
910
|
+
handles: [],
|
|
911
|
+
bus: null,
|
|
912
|
+
pauseGate: new PauseGate(),
|
|
913
|
+
};
|
|
914
|
+
this.session = session;
|
|
915
|
+
this.busy = true;
|
|
916
|
+
this.aborted = false;
|
|
917
|
+
this.interventions = [];
|
|
918
|
+
this.quietNotified.clear();
|
|
919
|
+
this.overseerDegraded = false;
|
|
920
|
+
this.overseerDegradedTicks = 0;
|
|
921
|
+
this.stoppedByHuman.clear();
|
|
922
|
+
return session;
|
|
378
923
|
}
|
|
379
|
-
/**
|
|
380
|
-
|
|
924
|
+
/**
|
|
925
|
+
* Idempotent teardown: puts every per-dispatch field back where
|
|
926
|
+
* `beginSession()` found it.
|
|
927
|
+
*
|
|
928
|
+
* A STRICT SUPERSET OF THE OLD `resetDispatchState()` (spec REV-04 item 2).
|
|
929
|
+
* The success path used to tear down inline and the throw path through this
|
|
930
|
+
* method; with a floating promise there is exactly ONE exit, and it is
|
|
931
|
+
* `finally`, so both halves have to be here or a detached dispatch leaks its
|
|
932
|
+
* timers, its pause gate or its latch.
|
|
933
|
+
*/
|
|
934
|
+
teardown(session) {
|
|
381
935
|
if (this.dispatchTimer)
|
|
382
936
|
clearTimeout(this.dispatchTimer);
|
|
383
937
|
this.dispatchTimer = null;
|
|
384
|
-
for (const handle of this.
|
|
938
|
+
for (const handle of this.liveHandles)
|
|
385
939
|
handle.unsubscribe();
|
|
386
940
|
this.monitor?.dispose();
|
|
387
941
|
this.monitor = null;
|
|
388
942
|
this.supervision?.clearAll();
|
|
389
943
|
this.supervision = null;
|
|
390
944
|
this.overseer = null;
|
|
391
|
-
|
|
945
|
+
// A held child would leave `waitIfPaused` promises (and their worker slots)
|
|
946
|
+
// dangling for ever; releasing is what lets every `runOne` reach its exit.
|
|
947
|
+
session.pauseGate.releaseAll();
|
|
948
|
+
session.state = 'settled';
|
|
949
|
+
session.handles = [];
|
|
950
|
+
session.bus = null;
|
|
951
|
+
this.makeHandleForSession = null;
|
|
952
|
+
this.liveRunCtx = null;
|
|
953
|
+
this.liveHandles = [];
|
|
392
954
|
this.bus = null;
|
|
393
955
|
this.snapshotState = null;
|
|
394
956
|
this.busy = false;
|
|
395
957
|
}
|
|
396
|
-
async runDispatch(
|
|
397
|
-
|
|
398
|
-
const
|
|
958
|
+
async runDispatch(session, run) {
|
|
959
|
+
const { mode } = run;
|
|
960
|
+
const signal = run.signal;
|
|
961
|
+
const dispatchId = session.id;
|
|
962
|
+
const specs = session.specs;
|
|
963
|
+
const requested = session.requested;
|
|
399
964
|
const config = this.deps.getConfig();
|
|
400
965
|
const team = config.team;
|
|
401
|
-
const startedAt =
|
|
402
|
-
|
|
403
|
-
this.aborted = false;
|
|
404
|
-
this.interventions = [];
|
|
405
|
-
this.quietNotified.clear();
|
|
406
|
-
this.overseerDegraded = false;
|
|
407
|
-
this.overseerDegradedTicks = 0;
|
|
966
|
+
const startedAt = session.startedAt;
|
|
967
|
+
session.pauseGate.releaseAll();
|
|
408
968
|
// THE GENE ZONES ARE PLANNED HERE, ONCE, BEFORE ANY CHILD EXISTS (gene-bank 9.6): planning is
|
|
409
969
|
// asynchronous and a child's system prompt is built synchronously, so the awaiting has to
|
|
410
970
|
// happen first. `busy` is already latched, so a second dispatch cannot interleave. A retry
|
|
@@ -494,6 +1054,7 @@ export class TeamRuntime {
|
|
|
494
1054
|
canSend,
|
|
495
1055
|
});
|
|
496
1056
|
this.bus = bus;
|
|
1057
|
+
session.bus = bus;
|
|
497
1058
|
const subDeps = {
|
|
498
1059
|
bus,
|
|
499
1060
|
config,
|
|
@@ -555,6 +1116,10 @@ export class TeamRuntime {
|
|
|
555
1116
|
// others): declared above and not forwarded here, it type-checks and every child quietly runs
|
|
556
1117
|
// without the library, with nothing to say so.
|
|
557
1118
|
...(this.deps.geneTools ? { geneTools: this.deps.geneTools } : {}),
|
|
1119
|
+
// Site two of two for the pause gate: declared on `TeamRuntimeDeps` (as the
|
|
1120
|
+
// session) and forgotten HERE, `team_pause` would flip a phase and stop
|
|
1121
|
+
// nothing - the child's tools would keep running while the UI says `paused`.
|
|
1122
|
+
pauseGate: session.pauseGate,
|
|
558
1123
|
// Same conditional-spread rule: a dispatch without a supervisor builds
|
|
559
1124
|
// the exact pre-feature `SubagentDeps`, and the child's idle watchdog
|
|
560
1125
|
// keeps its un-lengthened ceiling (team-overseer §3.6).
|
|
@@ -589,7 +1154,8 @@ export class TeamRuntime {
|
|
|
589
1154
|
// look live - which fails open rather than closed, so nothing would break
|
|
590
1155
|
// loudly.
|
|
591
1156
|
handles = specs.map(makeHandle);
|
|
592
|
-
this.
|
|
1157
|
+
this.liveHandles = handles;
|
|
1158
|
+
session.handles = handles;
|
|
593
1159
|
this.snapshotState = {
|
|
594
1160
|
dispatchId,
|
|
595
1161
|
active: true,
|
|
@@ -598,18 +1164,31 @@ export class TeamRuntime {
|
|
|
598
1164
|
startedAt,
|
|
599
1165
|
messageCount: 0,
|
|
600
1166
|
};
|
|
601
|
-
this.emit({
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
1167
|
+
this.emit({
|
|
1168
|
+
type: 'dispatch_start',
|
|
1169
|
+
dispatchId,
|
|
1170
|
+
requested,
|
|
1171
|
+
specs,
|
|
1172
|
+
turnId: session.turnId,
|
|
1173
|
+
...(mode === 'detach' ? { detached: true } : {}),
|
|
1174
|
+
});
|
|
1175
|
+
// TWO ABORT SOURCES IN AWAIT MODE, ONE IN DETACH MODE (spec 5.1.1 item 3).
|
|
1176
|
+
// `ctx.signal` belongs to the TURN; a detached dispatch outlives that turn, so
|
|
1177
|
+
// binding it would give the dispatch a death signal that fires when the user
|
|
1178
|
+
// stops something else entirely - and, worse, would make it look protected
|
|
1179
|
+
// while `abortAll()` is the only thing that can actually end it. In detach
|
|
1180
|
+
// mode `session.controller` is therefore the sole authority, and Esc reaches
|
|
1181
|
+
// it through `stopEngine()`'s I-DD11 branch or `team_stop`.
|
|
606
1182
|
const onAbort = () => this.abortAll();
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
1183
|
+
if (mode === 'await') {
|
|
1184
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
1185
|
+
// An abort that arrived while the gene zones were being planned fired before the listener
|
|
1186
|
+
// existed; a listener added to an already-aborted signal never runs. Only when planning
|
|
1187
|
+
// actually awaited, so every other dispatch behaves exactly as it did.
|
|
1188
|
+
if (planned && signal?.aborted === true)
|
|
1189
|
+
onAbort();
|
|
1190
|
+
}
|
|
1191
|
+
session.controller.signal.addEventListener('abort', onAbort, { once: true });
|
|
613
1192
|
// The dispatch ceiling has to be enforced HERE as well as through
|
|
614
1193
|
// `toolTimeoutOverrides.task`: the executor's timeout is COOPERATIVE (I-2),
|
|
615
1194
|
// so the override only bites because `task` listens to `ctx.signal` — and a
|
|
@@ -626,7 +1205,12 @@ export class TeamRuntime {
|
|
|
626
1205
|
// long dispatch, exactly as it already looks at a long child. The legacy
|
|
627
1206
|
// hard `abortAll()` exists ONLY under `team.overseer: false`, the same
|
|
628
1207
|
// regime line `runCtx.subagentTimeoutMs` below draws.
|
|
629
|
-
|
|
1208
|
+
//
|
|
1209
|
+
// THIS IS STILL THE ONLY CEILING (spec REV-02): detaching does not add a
|
|
1210
|
+
// hidden constant, it only changes who is watching. `team_status` shows
|
|
1211
|
+
// `ceiling: none | Nm` so the absence of a limit is a stated fact rather than
|
|
1212
|
+
// a silent risk.
|
|
1213
|
+
const dispatchCeilingMs = session.ceilingMs;
|
|
630
1214
|
this.dispatchTimer =
|
|
631
1215
|
dispatchCeilingMs > 0 ? setTimeout(() => this.abortAll(), dispatchCeilingMs) : null;
|
|
632
1216
|
const runCtx = {
|
|
@@ -638,20 +1222,26 @@ export class TeamRuntime {
|
|
|
638
1222
|
// is the ladder's compat first-check (D-8). The legacy hard abort
|
|
639
1223
|
// exists ONLY under `team.overseer: false`.
|
|
640
1224
|
subagentTimeoutMs: team.overseer ? 0 : team.subagentTimeoutMs,
|
|
1225
|
+
pauseGate: session.pauseGate,
|
|
641
1226
|
};
|
|
1227
|
+
// `team_spawn` re-enters the SAME pool through these two: the worker walks
|
|
1228
|
+
// `handles` by index and `makeHandle` builds a child with identical wiring, so
|
|
1229
|
+
// a late addition is indistinguishable from a first-batch sibling.
|
|
1230
|
+
this.makeHandleForSession = makeHandle;
|
|
1231
|
+
this.liveRunCtx = runCtx;
|
|
642
1232
|
const cursor = { next: 0 };
|
|
643
1233
|
const workers = Array.from({ length: Math.max(1, Math.min(team.maxConcurrent, handles.length)) }, () => this.worker(runCtx, cursor));
|
|
644
1234
|
await Promise.allSettled(workers);
|
|
1235
|
+
// THE ONE EXIT. Everything the success path used to tear down inline now
|
|
1236
|
+
// happens in `teardown()`, which is reached from `dispatch()`'s catch and
|
|
1237
|
+
// from `start()`'s `finally` - so the await path and the floating path share
|
|
1238
|
+
// it instead of keeping two copies that can drift (spec REV-04 item 2).
|
|
645
1239
|
if (this.dispatchTimer)
|
|
646
1240
|
clearTimeout(this.dispatchTimer);
|
|
647
1241
|
this.dispatchTimer = null;
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
this.monitor?.dispose();
|
|
652
|
-
this.monitor = null;
|
|
653
|
-
this.supervision?.clearAll();
|
|
654
|
-
this.supervision = null;
|
|
1242
|
+
if (mode === 'await')
|
|
1243
|
+
signal?.removeEventListener('abort', onAbort);
|
|
1244
|
+
session.controller.signal.removeEventListener('abort', onAbort);
|
|
655
1245
|
// CAPTURED BEFORE THE FIELD IS NULLED: the outcome carries what the
|
|
656
1246
|
// supervisor spent (D-7) and whether any tick ran unassisted (D-10),
|
|
657
1247
|
// both read off the per-dispatch state being torn down here.
|
|
@@ -659,7 +1249,6 @@ export class TeamRuntime {
|
|
|
659
1249
|
const overseerCalls = this.overseer?.calls();
|
|
660
1250
|
const overseerDegraded = this.overseerDegraded;
|
|
661
1251
|
const overseerDegradedTicks = this.overseerDegradedTicks;
|
|
662
|
-
this.overseer = null;
|
|
663
1252
|
// ONE TOTAL, AT THE LEAD'S TABLE. Every child runs the lead's own model
|
|
664
1253
|
// (main-agent parity), so there is exactly one tier to sum and one price to
|
|
665
1254
|
// sum it at; a split would be two answers to one question.
|
|
@@ -688,10 +1277,15 @@ export class TeamRuntime {
|
|
|
688
1277
|
...(overseerDegraded ? { overseerDegraded: true } : {}),
|
|
689
1278
|
...(overseerDegradedTicks > 0 ? { overseerDegradedTicks } : {}),
|
|
690
1279
|
};
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
1280
|
+
// THE REPORT IS GENERATED HERE, ONCE (spec REV-05). The price and model
|
|
1281
|
+
// closures used to be read by `task-tool.ts` after its `await`; a detached
|
|
1282
|
+
// dispatch has no such await, so the runtime holds them and every consumer
|
|
1283
|
+
// (the settle notice, `team_collect`, `/team wait`) reads THIS string. Their
|
|
1284
|
+
// byte equality is therefore a construction fact, not a promise.
|
|
1285
|
+
session.outcome = outcome;
|
|
1286
|
+
session.reportText = buildDispatchReport(outcome, this.deps.reportOptions?.() ?? {});
|
|
1287
|
+
session.settledInsideTurn =
|
|
1288
|
+
this.deps.currentTurnId?.() === session.turnId && this.deps.isLeadRunning?.() === true;
|
|
695
1289
|
this.emit({ type: 'dispatch_end', dispatchId, outcome });
|
|
696
1290
|
return outcome;
|
|
697
1291
|
}
|
|
@@ -751,13 +1345,43 @@ export class TeamRuntime {
|
|
|
751
1345
|
this.supervision?.start(run.label);
|
|
752
1346
|
this.monitor?.arm(run.label);
|
|
753
1347
|
}
|
|
1348
|
+
// --- The pause gate, half two of two (spec REV-12) ------------------
|
|
1349
|
+
// The tool gate stops a child BETWEEN two tools; this stops it BEFORE its
|
|
1350
|
+
// next request. Both are needed: a child that was still `queued` when it was
|
|
1351
|
+
// paused, and a child rebuilt while paused, never reach a tool call at all.
|
|
1352
|
+
// Inside the retry loop on purpose, so every `for(;;)` re-entry asks the
|
|
1353
|
+
// same question.
|
|
1354
|
+
const gate = ctx.pauseGate;
|
|
1355
|
+
await gate.waitIfPaused(run.label);
|
|
754
1356
|
let timedOut = false;
|
|
755
|
-
|
|
756
|
-
|
|
1357
|
+
// THE CHILD'S OWN CEILING IS CREDITED FOR HELD TIME (section 5.2.4 item 6):
|
|
1358
|
+
// a user who pauses a child must not be served a timeout for the minutes
|
|
1359
|
+
// they were in control. The DISPATCH ceiling is deliberately NOT credited -
|
|
1360
|
+
// a paused child is not a slow child, but it is also not a reason to extend
|
|
1361
|
+
// the dispatch.
|
|
1362
|
+
const pausedAtAttemptStart = gate.pausedMs(run.label);
|
|
1363
|
+
let timer = null;
|
|
1364
|
+
const armTimeout = () => {
|
|
1365
|
+
if (timer !== null)
|
|
1366
|
+
clearTimeout(timer);
|
|
1367
|
+
timer = null;
|
|
1368
|
+
if (supervised || subagentTimeoutMs <= 0)
|
|
1369
|
+
return;
|
|
1370
|
+
const held = gate.pausedMs(run.label) - pausedAtAttemptStart;
|
|
1371
|
+
const spent = Date.now() - (run.startedAt ?? Date.now()) - held;
|
|
1372
|
+
const remaining = Math.max(0, subagentTimeoutMs - spent);
|
|
1373
|
+
timer = setTimeout(() => {
|
|
1374
|
+
// A held child cannot time out: it is not doing anything, and the clock
|
|
1375
|
+
// that would kill it is the user's own decision.
|
|
1376
|
+
if (gate.isPaused(run.label)) {
|
|
1377
|
+
void gate.waitIfPaused(run.label).then(() => armTimeout());
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
757
1380
|
timedOut = true;
|
|
758
1381
|
handle.abort();
|
|
759
|
-
},
|
|
760
|
-
|
|
1382
|
+
}, remaining);
|
|
1383
|
+
};
|
|
1384
|
+
armTimeout();
|
|
761
1385
|
try {
|
|
762
1386
|
await agent.prompt(handle.spec.prompt);
|
|
763
1387
|
}
|
|
@@ -798,7 +1422,7 @@ export class TeamRuntime {
|
|
|
798
1422
|
run.endedAt = Date.now();
|
|
799
1423
|
// The supervisor's abandon is an error like any other, with the
|
|
800
1424
|
// supervisor's own reason so the report explains WHO stopped it and why.
|
|
801
|
-
if (verdict !== null && verdict.action === 'abandon') {
|
|
1425
|
+
if (verdict !== null && verdict.action === 'abandon' && !this.stoppedByHuman.has(run.label)) {
|
|
802
1426
|
run.error = run.error ?? `overseer abandoned: ${verdict.reason}`;
|
|
803
1427
|
}
|
|
804
1428
|
this.settlePhase(run, timedOut, subagentTimeoutMs);
|
|
@@ -868,7 +1492,7 @@ export class TeamRuntime {
|
|
|
868
1492
|
/**
|
|
869
1493
|
* Swap one child for a fresh one, carrying only what must survive.
|
|
870
1494
|
*
|
|
871
|
-
* `this.
|
|
1495
|
+
* `this.liveHandles` IS `handles`, so the single assignment below also makes
|
|
872
1496
|
* `abortAll()` abort the NEW child and makes the outcome carry the new run.
|
|
873
1497
|
* Anyone tempted to "clean up" that aliasing into a defensive copy must update
|
|
874
1498
|
* both places.
|
|
@@ -953,11 +1577,41 @@ export class TeamRuntime {
|
|
|
953
1577
|
* exactly this wait) or a confirmation queued on the one human slot.
|
|
954
1578
|
*/
|
|
955
1579
|
isBlocked(label) {
|
|
956
|
-
const handle = this.
|
|
1580
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
1581
|
+
// A HELD CHILD IS NOT A STALLED CHILD (R2). Without this line the supervisor
|
|
1582
|
+
// reads "the user stopped it" as "it is wedged" and nudges or abandons the
|
|
1583
|
+
// very child the user asked to keep - an intervention that contradicts
|
|
1584
|
+
// itself, which is the one failure this feature must not ship.
|
|
1585
|
+
if (this.session?.pauseGate.isPaused(label) === true)
|
|
1586
|
+
return true;
|
|
957
1587
|
if (handle !== undefined && handle.run.phase === 'waiting')
|
|
958
1588
|
return true;
|
|
959
1589
|
return this.deps.humanQueue?.isWaiting(label) === true;
|
|
960
1590
|
}
|
|
1591
|
+
/**
|
|
1592
|
+
* THE ONE ABANDON TAIL (spec REV-03).
|
|
1593
|
+
*
|
|
1594
|
+
* `applyDecision('abandon')` used to end this child inline, and `team_stop`
|
|
1595
|
+
* needs the identical ending - but it cannot call `applyDecision`, which
|
|
1596
|
+
* returns early when there is no supervisor (`team.overseer: false` is a legal
|
|
1597
|
+
* configuration) and whose abandon branch also spends the supervisor's own
|
|
1598
|
+
* budget. Extracting the tail gives both callers ONE abort path, with the two
|
|
1599
|
+
* legitimate differences named as parameters: only the supervisor spends its
|
|
1600
|
+
* budget, and only the supervisor's call is preceded by its own budget check.
|
|
1601
|
+
*/
|
|
1602
|
+
abandonChild(handle, reason, by) {
|
|
1603
|
+
handle.overseerVerdict = { action: 'abandon', reason };
|
|
1604
|
+
// The ONLY difference between the two callers' outcomes: a human's stop settles
|
|
1605
|
+
// as `aborted` (see `stoppedByHuman`), a supervisor's as `failed` with its own
|
|
1606
|
+
// reason. Both are recorded through the same single abort path.
|
|
1607
|
+
if (by !== 'supervisor')
|
|
1608
|
+
this.stoppedByHuman.add(handle.run.label);
|
|
1609
|
+
if (by === 'supervisor')
|
|
1610
|
+
this.overseer?.noteAbandon(handle.run.label);
|
|
1611
|
+
this.supervision?.clear(handle.run.label);
|
|
1612
|
+
this.monitor?.clear(handle.run.label);
|
|
1613
|
+
handle.abort();
|
|
1614
|
+
}
|
|
961
1615
|
/**
|
|
962
1616
|
* One SILENCE trigger handled: look, then apply. Fired from a supervision
|
|
963
1617
|
* timer and therefore async-and-forgotten; nothing here may throw.
|
|
@@ -973,7 +1627,7 @@ export class TeamRuntime {
|
|
|
973
1627
|
const supervision = this.supervision;
|
|
974
1628
|
if (overseer === null || supervision === null || this.aborted)
|
|
975
1629
|
return;
|
|
976
|
-
const handle = this.
|
|
1630
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
977
1631
|
if (handle === undefined)
|
|
978
1632
|
return;
|
|
979
1633
|
const phase = handle.run.phase;
|
|
@@ -1088,11 +1742,9 @@ export class TeamRuntime {
|
|
|
1088
1742
|
this.monitor?.advance(label);
|
|
1089
1743
|
break;
|
|
1090
1744
|
}
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
this.monitor?.clear(label);
|
|
1095
|
-
handle.abort();
|
|
1745
|
+
// The budget check above stays in the SUPERVISOR's branch; the tail is
|
|
1746
|
+
// shared with `team_stop` so there is exactly one abort path (spec REV-03).
|
|
1747
|
+
this.abandonChild(handle, decision.reason, 'supervisor');
|
|
1096
1748
|
break;
|
|
1097
1749
|
}
|
|
1098
1750
|
}
|
|
@@ -1106,7 +1758,7 @@ export class TeamRuntime {
|
|
|
1106
1758
|
reason: decision.reason,
|
|
1107
1759
|
trigger,
|
|
1108
1760
|
});
|
|
1109
|
-
const handle = this.
|
|
1761
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
1110
1762
|
if (handle !== undefined) {
|
|
1111
1763
|
handle.run.interventions = (handle.run.interventions ?? 0) + 1;
|
|
1112
1764
|
// The panel's badge (AC-5): ABSOLUTE `at`, same units as `startedAt`
|
|
@@ -1134,7 +1786,7 @@ export class TeamRuntime {
|
|
|
1134
1786
|
// -------------------------------------------------------------------------
|
|
1135
1787
|
/** R-P1-5: a label ticks only once STARTED and not yet settled. */
|
|
1136
1788
|
isMonitorLive(label) {
|
|
1137
|
-
const handle = this.
|
|
1789
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
1138
1790
|
if (handle === undefined || handle.run.startedAt === undefined)
|
|
1139
1791
|
return false;
|
|
1140
1792
|
const phase = handle.run.phase;
|
|
@@ -1153,7 +1805,7 @@ export class TeamRuntime {
|
|
|
1153
1805
|
return new Map();
|
|
1154
1806
|
const reqs = [];
|
|
1155
1807
|
for (const label of labels) {
|
|
1156
|
-
const handle = this.
|
|
1808
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
1157
1809
|
if (handle === undefined)
|
|
1158
1810
|
continue;
|
|
1159
1811
|
reqs.push({
|
|
@@ -1167,7 +1819,7 @@ export class TeamRuntime {
|
|
|
1167
1819
|
for (const [label, decision] of decisions) {
|
|
1168
1820
|
if (this.aborted)
|
|
1169
1821
|
break;
|
|
1170
|
-
const handle = this.
|
|
1822
|
+
const handle = this.liveHandles.find((h) => h.run.label === label);
|
|
1171
1823
|
const settled = handle === undefined ||
|
|
1172
1824
|
handle.run.phase === 'done' ||
|
|
1173
1825
|
handle.run.phase === 'failed' ||
|
|
@@ -1237,7 +1889,18 @@ export class TeamRuntime {
|
|
|
1237
1889
|
* place.
|
|
1238
1890
|
*/
|
|
1239
1891
|
settlePhase(run, timedOut, subagentTimeoutMs) {
|
|
1240
|
-
|
|
1892
|
+
// THE HOLD ACCOUNTING LANDS HERE, ONCE, at the attempt's terminal phase: the
|
|
1893
|
+
// report's Control section and the UI's `paused 1m02s` both read it from the
|
|
1894
|
+
// run, and a paused child that settles while held must still show the hold.
|
|
1895
|
+
const gate = this.session?.pauseGate;
|
|
1896
|
+
if (gate !== undefined) {
|
|
1897
|
+
const held = gate.pausedMs(run.label);
|
|
1898
|
+
if (held > 0) {
|
|
1899
|
+
run.pausedMs = held;
|
|
1900
|
+
run.pauseCount = gate.pauseCount(run.label);
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
if (this.aborted || this.stoppedByHuman.has(run.label)) {
|
|
1241
1904
|
run.phase = 'aborted';
|
|
1242
1905
|
}
|
|
1243
1906
|
else if (timedOut && subagentTimeoutMs > 0) {
|
|
@@ -1290,7 +1953,12 @@ export class TeamRuntime {
|
|
|
1290
1953
|
if (!this.busy)
|
|
1291
1954
|
return;
|
|
1292
1955
|
this.aborted = true;
|
|
1293
|
-
|
|
1956
|
+
// A HELD CHILD MUST BE RELEASED, not just aborted: its `waitIfPaused` promise
|
|
1957
|
+
// is what its worker is parked on, and `handle.abort()` does not wake it. If
|
|
1958
|
+
// this is missed, one pause plus one Esc wedges a worker slot for the rest of
|
|
1959
|
+
// the session - silently, because the dispatch itself does end.
|
|
1960
|
+
this.session?.pauseGate.releaseAll();
|
|
1961
|
+
for (const handle of this.liveHandles) {
|
|
1294
1962
|
try {
|
|
1295
1963
|
handle.abort();
|
|
1296
1964
|
}
|