@aragon-agent/cli 0.6.62 → 0.6.64
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 +70 -2
- package/README.md +143 -27
- package/dist/agent/controller.d.ts +66 -14
- package/dist/agent/controller.js +291 -81
- package/dist/agent/headless.d.ts +16 -0
- package/dist/agent/headless.js +62 -3
- package/dist/agent/team-notices.d.ts +57 -7
- package/dist/agent/team-notices.js +93 -10
- package/dist/app/App.js +199 -77
- package/dist/cli.js +4 -1
- package/dist/commands/builtins.js +2 -1
- package/dist/commands/catalog.js +1 -1
- package/dist/compaction/archive.d.ts +27 -1
- package/dist/compaction/archive.js +8 -0
- package/dist/compaction/budget.d.ts +73 -0
- package/dist/compaction/budget.js +89 -0
- package/dist/compaction/child.js +6 -1
- package/dist/compaction/command.js +147 -1
- package/dist/compaction/compactor.d.ts +132 -15
- package/dist/compaction/compactor.js +288 -95
- package/dist/compaction/context-command.js +21 -0
- package/dist/compaction/memory-agent-prompt.d.ts +32 -0
- package/dist/compaction/memory-agent-prompt.js +76 -0
- package/dist/compaction/memory-agent-tools.d.ts +88 -0
- package/dist/compaction/memory-agent-tools.js +338 -0
- package/dist/compaction/memory-agent.d.ts +114 -0
- package/dist/compaction/memory-agent.js +220 -0
- package/dist/compaction/prune.d.ts +129 -0
- package/dist/compaction/prune.js +386 -0
- package/dist/compaction/retry-plan.d.ts +103 -0
- package/dist/compaction/retry-plan.js +136 -0
- package/dist/compaction/summarize-ladder.d.ts +149 -0
- package/dist/compaction/summarize-ladder.js +273 -0
- package/dist/compaction/wiring.d.ts +19 -0
- package/dist/compaction/wiring.js +65 -1
- package/dist/config/cli-commands.d.ts +2 -2
- package/dist/config/cli-commands.js +27 -0
- package/dist/config/env.d.ts +3 -1
- package/dist/config/env.js +44 -2
- package/dist/exec/events.d.ts +30 -0
- package/dist/exec/index.js +2 -0
- package/dist/exec/runner.js +16 -1
- package/dist/gene/commit-prepare.js +27 -1
- package/dist/gene/curator/curator.d.ts +15 -0
- package/dist/gene/curator/curator.js +26 -2
- package/dist/gene/curator/scripted-lane.d.ts +12 -0
- package/dist/gene/curator/scripted-lane.js +101 -0
- package/dist/gene/curator/task-context.d.ts +21 -0
- package/dist/gene/curator/task-context.js +33 -0
- package/dist/gene/curator/task-runner.d.ts +10 -1
- package/dist/gene/curator/task-runner.js +39 -4
- package/dist/gene/curator/tool-support.js +11 -4
- package/dist/gene/curator/toolkit.d.ts +13 -0
- package/dist/gene/curator/toolkit.js +23 -1
- package/dist/gene/curator/tools-verify.js +2 -2
- package/dist/gene/curator/tools-write.js +6 -1
- package/dist/gene/curator/validate/evidence-tokens.js +133 -22
- package/dist/gene/curator/validate/knowledge.d.ts +41 -1
- package/dist/gene/curator/validate/knowledge.js +74 -11
- package/dist/gene/curator/validate/prompts.d.ts +10 -2
- package/dist/gene/curator/validate/prompts.js +2 -2
- package/dist/gene/learn/draft-guard.d.ts +17 -0
- package/dist/gene/learn/draft-guard.js +113 -0
- package/dist/gene/learn/learn-prompt.d.ts +22 -1
- package/dist/gene/learn/learn-prompt.js +27 -2
- package/dist/gene/learn/learner.js +20 -2
- package/dist/gene/queue.d.ts +16 -0
- package/dist/gene/queue.js +44 -1
- package/dist/governance/store/run-store.d.ts +20 -0
- package/dist/governance/store/run-store.js +54 -8
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/host/interaction.d.ts +8 -17
- package/dist/host/interaction.js +50 -72
- package/dist/host/team-wake-driver.d.ts +90 -0
- package/dist/host/team-wake-driver.js +114 -0
- package/dist/logging/install.d.ts +59 -1
- package/dist/logging/install.js +15 -0
- package/dist/session/validate-session.js +11 -0
- package/dist/team/console-tools.d.ts +12 -0
- package/dist/team/console-tools.js +20 -3
- package/dist/team/console-view.js +11 -1
- package/dist/team/notices.d.ts +81 -4
- package/dist/team/notices.js +187 -22
- package/dist/team/prompt.js +1 -1
- package/dist/team/runtime.d.ts +67 -24
- package/dist/team/runtime.js +250 -31
- package/dist/team/stall-watch.d.ts +54 -0
- package/dist/team/stall-watch.js +119 -0
- package/dist/team/task-tool.js +2 -2
- package/dist/team/wake.d.ts +176 -0
- package/dist/team/wake.js +444 -0
- package/package.json +81 -81
package/dist/agent/headless.js
CHANGED
|
@@ -8,7 +8,7 @@ import { formatCost, formatTokens, formatDuration, computeCost } from '@aragon-a
|
|
|
8
8
|
import { formatStreamError } from '@aragon-agent/tui/model/agent/reducer';
|
|
9
9
|
import { runDurationMs } from '../team/report.js';
|
|
10
10
|
import { derivePlanPhase } from '@aragon-agent/tui/model/todo/acceptance';
|
|
11
|
-
import { advanceBudget, decideFollowThrough, emptyBudget, isTransientRunEnd, } from '@aragon-agent/tui/model/todo/follow-through';
|
|
11
|
+
import { advanceBudget, chargeResume, decideFollowThrough, emptyBudget, emptyResumeCounters, isTransientRunEnd, settleResumeCounters, } from '@aragon-agent/tui/model/todo/follow-through';
|
|
12
12
|
/**
|
|
13
13
|
* Mark time. The only wait `-p` ever takes, and it exists for one caller: the
|
|
14
14
|
* recovery grace in the follow-through loop below (`errorGraceMs`).
|
|
@@ -106,6 +106,9 @@ export async function runHeadless(controller, prompt, options = {}) {
|
|
|
106
106
|
...(event.errorType !== undefined ? { errorType: event.errorType } : {}),
|
|
107
107
|
...(event.retryable !== undefined ? { retryable: event.retryable } : {}),
|
|
108
108
|
...(event.reason !== undefined ? { reason: event.reason } : {}),
|
|
109
|
+
// For the recovery notice's WORDING only (F3): without it the line says "the
|
|
110
|
+
// model stopped responding" and cannot say for how long or doing what.
|
|
111
|
+
...(event.stall !== undefined ? { stall: event.stall } : {}),
|
|
109
112
|
};
|
|
110
113
|
if (!sawTurnEnd && !errored)
|
|
111
114
|
errored = true;
|
|
@@ -272,6 +275,24 @@ export async function runHeadless(controller, prompt, options = {}) {
|
|
|
272
275
|
`${r.tailRelief.messages === 1 ? 'result' : 'results'} in the retained turns ` +
|
|
273
276
|
`(${formatTokens(r.tailRelief.charsRemoved)} chars)\n`);
|
|
274
277
|
}
|
|
278
|
+
// THE v3 SURFACES (context-compaction-v3 W1 / W3 / W4), each only when it has
|
|
279
|
+
// something to say. A `-p` reader cannot open `/compact status`, so a pruned
|
|
280
|
+
// body count and an agent turn count that never reach this stream are
|
|
281
|
+
// invisible to the only audience that reads a machine log.
|
|
282
|
+
if (r.prune && r.prune.actions > 0) {
|
|
283
|
+
err.write(`[compaction] pruned ${r.prune.actions} redundant ` +
|
|
284
|
+
`${r.prune.actions === 1 ? 'tool result' : 'tool results'} ` +
|
|
285
|
+
`(${formatTokens(r.prune.removedChars)} chars)\n`);
|
|
286
|
+
}
|
|
287
|
+
if (r.agent) {
|
|
288
|
+
err.write(`[compaction] agent: ${r.agent.turns} turns, ${r.agent.toolCalls} tool calls` +
|
|
289
|
+
`${r.agent.fallback === 'single-shot' ? ' (single-shot fallback)' : ''}\n`);
|
|
290
|
+
}
|
|
291
|
+
if (r.budget && (r.budget.limitTokens > 0 || r.budget.limitUsd > 0)) {
|
|
292
|
+
err.write(`[compaction] budget: ${formatTokens(r.budget.spentTokens)} tokens` +
|
|
293
|
+
`${r.budget.priceUnknown ? ', cost n/a' : `, ${r.budget.spentUsd.toFixed(2)} USD`}` +
|
|
294
|
+
`${r.budget.stop ? ` - stopped: ${r.budget.stop}` : ''}\n`);
|
|
295
|
+
}
|
|
275
296
|
});
|
|
276
297
|
/**
|
|
277
298
|
* `[accept] <n>/<total> checked`, plus the failure count when there is one.
|
|
@@ -298,6 +319,12 @@ export async function runHeadless(controller, prompt, options = {}) {
|
|
|
298
319
|
const followThrough = options.followThrough ?? 'notify';
|
|
299
320
|
let budget = emptyBudget();
|
|
300
321
|
let wasAutoContinuation = false;
|
|
322
|
+
// A RECOVERY NEEDS THE CONTROLLER TO BE ABLE TO CONTINUE (F3): without `continue`
|
|
323
|
+
// the only way to "retry" would be to resend the user's prompt, which is a
|
|
324
|
+
// different act (a duplicate message in the history), so the capability is absent.
|
|
325
|
+
const resumePolicy = typeof controller.continue === 'function' ? options.resume : undefined;
|
|
326
|
+
let resumeCounters = emptyResumeCounters();
|
|
327
|
+
let resumeNext = false;
|
|
301
328
|
try {
|
|
302
329
|
let message = prompt;
|
|
303
330
|
for (;;) {
|
|
@@ -310,10 +337,19 @@ export async function runHeadless(controller, prompt, options = {}) {
|
|
|
310
337
|
// `turn_end`, the only event that proves a recovery happened.
|
|
311
338
|
sawTurnEnd = false;
|
|
312
339
|
endFacts = {};
|
|
313
|
-
|
|
340
|
+
// A RECOVERY RE-RUNS THE HISTORY AS IT STANDS; it submits nothing, so `message`
|
|
341
|
+
// is neither consumed nor replaced.
|
|
342
|
+
if (resumeNext) {
|
|
343
|
+
resumeNext = false;
|
|
344
|
+
await controller.continue?.();
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
await controller.prompt(message);
|
|
348
|
+
}
|
|
314
349
|
const snapshot = controller.getTodoSnapshot?.() ?? null;
|
|
315
350
|
budget = advanceBudget(budget, snapshot, wasAutoContinuation);
|
|
316
351
|
wasAutoContinuation = false;
|
|
352
|
+
resumeCounters = settleResumeCounters(resumeCounters, { aborted: false, errored });
|
|
317
353
|
const decision = decideFollowThrough({
|
|
318
354
|
mode: followThrough,
|
|
319
355
|
snapshot,
|
|
@@ -326,14 +362,37 @@ export async function runHeadless(controller, prompt, options = {}) {
|
|
|
326
362
|
runEnd: { aborted: false, errored, transient: isTransientRunEnd(endFacts) },
|
|
327
363
|
budget,
|
|
328
364
|
interactive: false,
|
|
365
|
+
...(resumePolicy
|
|
366
|
+
? {
|
|
367
|
+
resume: {
|
|
368
|
+
...resumePolicy,
|
|
369
|
+
consecutive: resumeCounters.consecutive,
|
|
370
|
+
total: resumeCounters.total,
|
|
371
|
+
cause: endFacts,
|
|
372
|
+
},
|
|
373
|
+
}
|
|
374
|
+
: {}),
|
|
329
375
|
});
|
|
330
376
|
if (decision.kind === 'none')
|
|
331
377
|
break;
|
|
332
378
|
if (decision.kind === 'notify') {
|
|
379
|
+
const tag = decision.source === 'resume' ? 'retry' : 'todo';
|
|
333
380
|
if (!quiet)
|
|
334
|
-
err.write(`\n[
|
|
381
|
+
err.write(`\n[${tag}] ${decision.text}\n`);
|
|
335
382
|
break;
|
|
336
383
|
}
|
|
384
|
+
if (decision.kind === 'resume') {
|
|
385
|
+
// CHARGED BEFORE THE WAIT, like the todo continuation below: headless has no
|
|
386
|
+
// timer to charge it in, and an uncharged counter is an uncapped loop in the
|
|
387
|
+
// one environment with no human to press Esc.
|
|
388
|
+
resumeCounters = chargeResume(resumeCounters);
|
|
389
|
+
if (!quiet)
|
|
390
|
+
err.write(`\n[retry] ${decision.notice}\n`);
|
|
391
|
+
if (decision.graceMs > 0)
|
|
392
|
+
await pauseMs(decision.graceMs);
|
|
393
|
+
resumeNext = true;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
337
396
|
// CHARGED BEFORE THE RE-PROMPT, and this line is the whole of the headless
|
|
338
397
|
// cap (P1-2 / I-5 / AC-37). The TUI charges `used` in its grace-timer
|
|
339
398
|
// callback; headless has no timer, so without this `maxAutoContinuesPerList`
|
|
@@ -20,6 +20,14 @@
|
|
|
20
20
|
* into the queue. `TeamNoticeQueue.push` is idempotent by id, so a re-queue can
|
|
21
21
|
* only delay a notice, never duplicate it.
|
|
22
22
|
*
|
|
23
|
+
* THE RECEIPT IS PER STEER (team-lead-wake-signals I-WK6). Core only echoes a
|
|
24
|
+
* `steering_accepted` for a steer that carried an id (`accept-steering.ts:14`), and
|
|
25
|
+
* the old code steered without one: a notices-only batch never produced a receipt,
|
|
26
|
+
* so the ledger could only be emptied by `requeueUndelivered()` - which re-queued a
|
|
27
|
+
* notice that HAD been delivered, and delivered it again. Now every steer carries a
|
|
28
|
+
* `notice:` id and the ledger is keyed by it, so a receipt releases exactly the
|
|
29
|
+
* notices it carried and nothing else.
|
|
30
|
+
*
|
|
23
31
|
* WHY NOT ABSTRACTED TOGETHER WITH THE JOB RELAY (spec 5.3.2). The job relay's
|
|
24
32
|
* channel 1 must never inject while a tool batch is open (its I-5), because a job
|
|
25
33
|
* notice is a courtesy. A settle report is not a courtesy: the lead was told "you
|
|
@@ -27,41 +35,83 @@
|
|
|
27
35
|
* One shared class would have to be parameterised into two different policies and
|
|
28
36
|
* would end up being two classes anyway, one of them hidden.
|
|
29
37
|
*/
|
|
30
|
-
import { type TeamNoticeQueue } from '../team/notices.js';
|
|
38
|
+
import { type TeamNotice, type TeamNoticeQueue } from '../team/notices.js';
|
|
31
39
|
export interface TeamNoticeRelayDeps {
|
|
32
40
|
queue: TeamNoticeQueue;
|
|
33
|
-
/**
|
|
34
|
-
|
|
41
|
+
/**
|
|
42
|
+
* `enqueueSteering(text, false, undefined, true)` - the RECEIPT-carrying path for
|
|
43
|
+
* notices. Returns the id Core will echo in `steering_accepted`.
|
|
44
|
+
*/
|
|
45
|
+
steer: (text: string) => string;
|
|
46
|
+
/**
|
|
47
|
+
* Take one steer back out of Core's own steering queue (`Agent.removeQueuedSteering`).
|
|
48
|
+
* `true` means it was still waiting there and is now gone.
|
|
49
|
+
*
|
|
50
|
+
* Core's queue SURVIVES a run, and `acceptSteering` returns early on an aborted
|
|
51
|
+
* signal, so a steer can be "sent, not accepted, still queued" when the run ends
|
|
52
|
+
* (RV-P1-1). Re-queueing such a notice without withdrawing it would let the next
|
|
53
|
+
* turn steer a second copy and Core accept both.
|
|
54
|
+
*/
|
|
55
|
+
withdraw: (receiptId: string) => boolean;
|
|
35
56
|
/** `activeToolCalls === 0 && isRunning() && !abortRequested` (push channel). */
|
|
36
57
|
isAtSafeSteerPoint: () => boolean;
|
|
37
58
|
/**
|
|
38
|
-
*
|
|
59
|
+
* The FILL channel's precondition: no tool batch open, and nothing being aborted
|
|
60
|
+
* RIGHT NOW (`activeToolCalls === 0 && (!abortRequested || !isRunning())`).
|
|
39
61
|
*
|
|
40
62
|
* NOT `isAtSafeSteerPoint()`, for the reason `job-notices.ts` records at length:
|
|
41
63
|
* the fill runs immediately before `agent.prompt()`, where `isRunning()` is
|
|
42
64
|
* FALSE by contract, so the push predicate would make this channel dead.
|
|
65
|
+
*
|
|
66
|
+
* `abortRequested` alone is the wrong half (RV-P1-5): it is set by `stopEngine()`
|
|
67
|
+
* and cleared only at the next `agent_start`, which is AFTER this check. A session
|
|
68
|
+
* whose last turn was interrupted would then refuse the fill for the very next
|
|
69
|
+
* turn - the wake turn included - and the report would miss it.
|
|
43
70
|
*/
|
|
44
71
|
canSteerBeforeTurn: () => boolean;
|
|
45
72
|
}
|
|
46
73
|
export declare class TeamNoticeRelay {
|
|
47
74
|
private readonly deps;
|
|
48
|
-
/** Notices steered but not yet proven delivered, keyed by their
|
|
75
|
+
/** Notices steered but not yet proven delivered, keyed by the id their steer carried. */
|
|
49
76
|
private readonly pendingSteered;
|
|
50
77
|
constructor(deps: TeamNoticeRelayDeps);
|
|
51
78
|
/** The push channel: called from the controller's one safe steer point. */
|
|
52
79
|
tryPush(): boolean;
|
|
53
80
|
/** The fill channel: just before the next turn starts. */
|
|
54
81
|
flushForTurn(): boolean;
|
|
55
|
-
/**
|
|
56
|
-
|
|
82
|
+
/**
|
|
83
|
+
* A receipt proves ONE steer was taken out of Core's queue and into history, so
|
|
84
|
+
* only the notices that steer carried are delivered. Returns their ids.
|
|
85
|
+
*/
|
|
86
|
+
onSteeringAccepted(receiptIds: readonly string[]): string[];
|
|
57
87
|
/**
|
|
58
88
|
* Put undelivered notices back at `turn_start` and at the end of a run.
|
|
59
89
|
*
|
|
60
90
|
* THIS IS WHAT TURNS "swallowed by clearAllQueues()" INTO "delivered one turn
|
|
61
91
|
* later" - the difference between a report the user gets and a report that
|
|
62
92
|
* exists only in a file.
|
|
93
|
+
*
|
|
94
|
+
* WITHDRAW FIRST, THEN QUEUE. A receipt-less entry in the ledger is one of two
|
|
95
|
+
* things: Core's queue was wiped (`withdraw` answers false), or Core still holds
|
|
96
|
+
* the steer (`withdraw` answers true and removes it). Either way ours is the only
|
|
97
|
+
* copy afterwards, so queueing it cannot deliver it twice. "Already accepted" is
|
|
98
|
+
* not a third case: that receipt arrives first, synchronously, and the ledger is
|
|
99
|
+
* released before anything here can run.
|
|
63
100
|
*/
|
|
64
101
|
requeueUndelivered(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Everything the lead has not yet been PROVEN to have: the queue plus the ledger.
|
|
104
|
+
* The wake governor reads this - a notice that was steered but never receipted is
|
|
105
|
+
* still owed.
|
|
106
|
+
*/
|
|
107
|
+
undelivered(): TeamNotice[];
|
|
108
|
+
/**
|
|
109
|
+
* Remove one notice from the queue AND the ledger without delivering it (the lead
|
|
110
|
+
* already has it in a tool result - `team_collect`). Returns whether it was held.
|
|
111
|
+
*/
|
|
112
|
+
dropById(noticeId: string): boolean;
|
|
113
|
+
/** Forget everything held in the ledger (session reset). Does not touch the queue. */
|
|
114
|
+
reset(): void;
|
|
65
115
|
/** How many notices are waiting. Used by the tests and the diagnostics path. */
|
|
66
116
|
pending(): number;
|
|
67
117
|
private steerQueued;
|
|
@@ -20,6 +20,14 @@
|
|
|
20
20
|
* into the queue. `TeamNoticeQueue.push` is idempotent by id, so a re-queue can
|
|
21
21
|
* only delay a notice, never duplicate it.
|
|
22
22
|
*
|
|
23
|
+
* THE RECEIPT IS PER STEER (team-lead-wake-signals I-WK6). Core only echoes a
|
|
24
|
+
* `steering_accepted` for a steer that carried an id (`accept-steering.ts:14`), and
|
|
25
|
+
* the old code steered without one: a notices-only batch never produced a receipt,
|
|
26
|
+
* so the ledger could only be emptied by `requeueUndelivered()` - which re-queued a
|
|
27
|
+
* notice that HAD been delivered, and delivered it again. Now every steer carries a
|
|
28
|
+
* `notice:` id and the ledger is keyed by it, so a receipt releases exactly the
|
|
29
|
+
* notices it carried and nothing else.
|
|
30
|
+
*
|
|
23
31
|
* WHY NOT ABSTRACTED TOGETHER WITH THE JOB RELAY (spec 5.3.2). The job relay's
|
|
24
32
|
* channel 1 must never inject while a tool batch is open (its I-5), because a job
|
|
25
33
|
* notice is a courtesy. A settle report is not a courtesy: the lead was told "you
|
|
@@ -30,7 +38,7 @@
|
|
|
30
38
|
import { renderTeamNotices } from '../team/notices.js';
|
|
31
39
|
export class TeamNoticeRelay {
|
|
32
40
|
deps;
|
|
33
|
-
/** Notices steered but not yet proven delivered, keyed by their
|
|
41
|
+
/** Notices steered but not yet proven delivered, keyed by the id their steer carried. */
|
|
34
42
|
pendingSteered = new Map();
|
|
35
43
|
constructor(deps) {
|
|
36
44
|
this.deps = deps;
|
|
@@ -47,9 +55,21 @@ export class TeamNoticeRelay {
|
|
|
47
55
|
return false;
|
|
48
56
|
return this.steerQueued();
|
|
49
57
|
}
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
/**
|
|
59
|
+
* A receipt proves ONE steer was taken out of Core's queue and into history, so
|
|
60
|
+
* only the notices that steer carried are delivered. Returns their ids.
|
|
61
|
+
*/
|
|
62
|
+
onSteeringAccepted(receiptIds) {
|
|
63
|
+
const delivered = [];
|
|
64
|
+
for (const receiptId of receiptIds) {
|
|
65
|
+
const carried = this.pendingSteered.get(receiptId);
|
|
66
|
+
if (carried === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
this.pendingSteered.delete(receiptId);
|
|
69
|
+
for (const notice of carried)
|
|
70
|
+
delivered.push(notice.id);
|
|
71
|
+
}
|
|
72
|
+
return delivered;
|
|
53
73
|
}
|
|
54
74
|
/**
|
|
55
75
|
* Put undelivered notices back at `turn_start` and at the end of a run.
|
|
@@ -57,14 +77,69 @@ export class TeamNoticeRelay {
|
|
|
57
77
|
* THIS IS WHAT TURNS "swallowed by clearAllQueues()" INTO "delivered one turn
|
|
58
78
|
* later" - the difference between a report the user gets and a report that
|
|
59
79
|
* exists only in a file.
|
|
80
|
+
*
|
|
81
|
+
* WITHDRAW FIRST, THEN QUEUE. A receipt-less entry in the ledger is one of two
|
|
82
|
+
* things: Core's queue was wiped (`withdraw` answers false), or Core still holds
|
|
83
|
+
* the steer (`withdraw` answers true and removes it). Either way ours is the only
|
|
84
|
+
* copy afterwards, so queueing it cannot deliver it twice. "Already accepted" is
|
|
85
|
+
* not a third case: that receipt arrives first, synchronously, and the ledger is
|
|
86
|
+
* released before anything here can run.
|
|
60
87
|
*/
|
|
61
88
|
requeueUndelivered() {
|
|
62
89
|
if (this.pendingSteered.size === 0)
|
|
63
90
|
return;
|
|
64
|
-
const stranded = [...this.pendingSteered.
|
|
91
|
+
const stranded = [...this.pendingSteered.entries()];
|
|
92
|
+
this.pendingSteered.clear();
|
|
93
|
+
for (const [receiptId, notices] of stranded) {
|
|
94
|
+
try {
|
|
95
|
+
this.deps.withdraw(receiptId);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
// A failed withdraw must not cost the notice: the worst case is the old,
|
|
99
|
+
// duplicate-prone behaviour, which is still better than silence.
|
|
100
|
+
}
|
|
101
|
+
for (const notice of notices)
|
|
102
|
+
this.deps.queue.push(notice);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Everything the lead has not yet been PROVEN to have: the queue plus the ledger.
|
|
107
|
+
* The wake governor reads this - a notice that was steered but never receipted is
|
|
108
|
+
* still owed.
|
|
109
|
+
*/
|
|
110
|
+
undelivered() {
|
|
111
|
+
const seen = new Set();
|
|
112
|
+
const out = [];
|
|
113
|
+
const all = [...this.deps.queue.peek(), ...[...this.pendingSteered.values()].flat()];
|
|
114
|
+
for (const notice of all) {
|
|
115
|
+
if (seen.has(notice.id))
|
|
116
|
+
continue;
|
|
117
|
+
seen.add(notice.id);
|
|
118
|
+
out.push(notice);
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Remove one notice from the queue AND the ledger without delivering it (the lead
|
|
124
|
+
* already has it in a tool result - `team_collect`). Returns whether it was held.
|
|
125
|
+
*/
|
|
126
|
+
dropById(noticeId) {
|
|
127
|
+
let dropped = this.deps.queue.dropById(noticeId);
|
|
128
|
+
for (const [receiptId, notices] of [...this.pendingSteered]) {
|
|
129
|
+
const kept = notices.filter((notice) => notice.id !== noticeId);
|
|
130
|
+
if (kept.length === notices.length)
|
|
131
|
+
continue;
|
|
132
|
+
dropped = true;
|
|
133
|
+
if (kept.length === 0)
|
|
134
|
+
this.pendingSteered.delete(receiptId);
|
|
135
|
+
else
|
|
136
|
+
this.pendingSteered.set(receiptId, kept);
|
|
137
|
+
}
|
|
138
|
+
return dropped;
|
|
139
|
+
}
|
|
140
|
+
/** Forget everything held in the ledger (session reset). Does not touch the queue. */
|
|
141
|
+
reset() {
|
|
65
142
|
this.pendingSteered.clear();
|
|
66
|
-
for (const notice of stranded)
|
|
67
|
-
this.deps.queue.push(notice);
|
|
68
143
|
}
|
|
69
144
|
/** How many notices are waiting. Used by the tests and the diagnostics path. */
|
|
70
145
|
pending() {
|
|
@@ -74,9 +149,17 @@ export class TeamNoticeRelay {
|
|
|
74
149
|
const notices = this.deps.queue.drain();
|
|
75
150
|
if (notices.length === 0)
|
|
76
151
|
return false;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
152
|
+
let receiptId;
|
|
153
|
+
try {
|
|
154
|
+
receiptId = this.deps.steer(renderTeamNotices(notices));
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Nothing was delivered: put them back so the next channel can try.
|
|
158
|
+
for (const notice of notices)
|
|
159
|
+
this.deps.queue.push(notice);
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
this.pendingSteered.set(receiptId, notices);
|
|
80
163
|
return true;
|
|
81
164
|
}
|
|
82
165
|
}
|