@cogitator-ai/bridge 0.11.0 → 0.12.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/README.md +43 -0
- package/dist/adapter.d.ts +16 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapters/claude.d.ts +44 -0
- package/dist/adapters/claude.d.ts.map +1 -1
- package/dist/adapters/claude.js +98 -2
- package/dist/adapters/claude.js.map +1 -1
- package/dist/adapters/codex.d.ts +8 -2
- package/dist/adapters/codex.d.ts.map +1 -1
- package/dist/adapters/codex.js +24 -5
- package/dist/adapters/codex.js.map +1 -1
- package/dist/adapters/hosted.d.ts +13 -1
- package/dist/adapters/hosted.d.ts.map +1 -1
- package/dist/adapters/hosted.js +25 -3
- package/dist/adapters/hosted.js.map +1 -1
- package/dist/client.d.ts +5 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +8 -0
- package/dist/client.js.map +1 -1
- package/dist/dispatcher.d.ts +82 -3
- package/dist/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher.js +327 -12
- package/dist/dispatcher.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/host.d.ts +3 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +1 -0
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/inputs.d.ts +23 -1
- package/dist/inputs.d.ts.map +1 -1
- package/dist/inputs.js +4 -0
- package/dist/inputs.js.map +1 -1
- package/dist/job-record.d.ts +6 -0
- package/dist/job-record.d.ts.map +1 -1
- package/dist/job-record.js +3 -0
- package/dist/job-record.js.map +1 -1
- package/dist/letters.d.ts +1 -1
- package/dist/letters.d.ts.map +1 -1
- package/dist/letters.js +4 -0
- package/dist/letters.js.map +1 -1
- package/dist/limit.d.ts +37 -0
- package/dist/limit.d.ts.map +1 -0
- package/dist/limit.js +189 -0
- package/dist/limit.js.map +1 -0
- package/dist/protocol.d.ts +5 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -1
- package/dist/recipes/steps.d.ts +4 -2
- package/dist/recipes/steps.d.ts.map +1 -1
- package/dist/recipes/steps.js +24 -9
- package/dist/recipes/steps.js.map +1 -1
- package/dist/scoreboard.d.ts +7 -0
- package/dist/scoreboard.d.ts.map +1 -1
- package/dist/scoreboard.js +17 -3
- package/dist/scoreboard.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +8 -1
- package/dist/server.js.map +1 -1
- package/dist/setup/claude.d.ts +3 -1
- package/dist/setup/claude.d.ts.map +1 -1
- package/dist/setup/claude.js +8 -2
- package/dist/setup/claude.js.map +1 -1
- package/dist/store/sqlite.d.ts +4 -1
- package/dist/store/sqlite.d.ts.map +1 -1
- package/dist/store/sqlite.js +20 -1
- package/dist/store/sqlite.js.map +1 -1
- package/dist/store/store.d.ts +6 -1
- package/dist/store/store.d.ts.map +1 -1
- package/dist/types.d.ts +46 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -1
- package/fakes/claude.mjs +49 -0
- package/package.json +1 -1
package/dist/dispatcher.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { isAbsolute, resolve as resolvePath } from 'node:path';
|
|
4
|
-
import { TaskManager, textPart, toMessage } from '@cogitator-ai/a2a';
|
|
4
|
+
import { TASK_AGENT_KEY, TaskManager, textPart, toMessage } from '@cogitator-ai/a2a';
|
|
5
5
|
import { MAX_RECALL_LIMIT } from './board.js';
|
|
6
6
|
import { runCheck, skippedCheck, summarizeChecks } from './checks.js';
|
|
7
7
|
import { BridgeError } from './errors.js';
|
|
8
8
|
import { deliveryBackoff, exhaustedBudget, formatDuration, nextDepth, resolveBudget, resolveLimits, } from './guards.js';
|
|
9
|
-
import { AgentsFilterSchema, CreateJobInputSchema, DEFAULT_INBOX_LIMIT, DEFAULT_LIST_LIMIT, DEFAULT_VERDICT_WAIT_MS, DEFAULT_WAIT_MS, DeliveredInputSchema, EscalateInputSchema, InboxInputSchema, JobsFilterSchema, JournalQuerySchema, LettersFilterSchema, PersonAnswerInputSchema, SendLetterInputSchema, StuckInputSchema, VerdictInputSchema, VerificationInputSchema, AcceptInputSchema, DiffInputSchema, ForgetInputSchema, RecallInputSchema, RejectInputSchema, RememberInputSchema, ResultsInputSchema, SuggestInputSchema, WaitInputSchema, WithdrawInputSchema, } from './inputs.js';
|
|
9
|
+
import { AgentsFilterSchema, CreateJobInputSchema, DEFAULT_INBOX_LIMIT, DEFAULT_LIST_LIMIT, DEFAULT_VERDICT_WAIT_MS, DEFAULT_WAIT_MS, DeliveredInputSchema, EscalateInputSchema, InboxInputSchema, JobsFilterSchema, JournalQuerySchema, LettersFilterSchema, LimitedInputSchema, PersonAnswerInputSchema, SendLetterInputSchema, StuckInputSchema, VerdictInputSchema, VerificationInputSchema, AcceptInputSchema, DiffInputSchema, ForgetInputSchema, RecallInputSchema, RejectInputSchema, RememberInputSchema, ResultsInputSchema, SuggestInputSchema, WaitInputSchema, WithdrawInputSchema, } from './inputs.js';
|
|
10
10
|
import { parseFindings } from './findings.js';
|
|
11
11
|
import { HANDOVER_GAP, buildHandover } from './handover.js';
|
|
12
12
|
import { isFinishedJobState, jobOf, jobRecordOf } from './job-record.js';
|
|
13
|
+
import { limitClock, limitOf } from './limit.js';
|
|
13
14
|
import { expectationOf, forHistory, newLetterId, normalizeBody, oneLine, quoted, renderLetter, repeats, titleOf, } from './letters.js';
|
|
14
15
|
import { canonicalPath, findCheckoutRoot, findProjectRoot, inCheckout, isInside } from './paths.js';
|
|
15
16
|
import { DISCARDED_REFS, GitError, addJobWorktree, branchTip, busyWith, changesOf, checkoutOf, conflictsOf, patchOf, pointRef, removeJobWorktree, shownPath, snapshotOf, squashInto, } from './worktree.js';
|
|
16
|
-
import { AUTO_ADDRESS, NEW_SESSION, aliasOfHarness, allocateAgentName, autoAddressOf, resolveAddress, } from './registry.js';
|
|
17
|
-
import { resultOf, suggestFrom } from './scoreboard.js';
|
|
17
|
+
import { AUTO_ADDRESS, NEW_SESSION, aliasOfHarness, allocateAgentName, harnessOfAlias, autoAddressOf, resolveAddress, } from './registry.js';
|
|
18
|
+
import { resultOf, suggestFrom, } from './scoreboard.js';
|
|
18
19
|
import { BRIDGE_JOB_KEY, BRIDGE_SENDER, CallerIdentitySchema, DEFAULT_LIMITS, HarnessNameSchema, INBOX_DELIVERY, NO_CAPABILITIES, PERSON, } from './types.js';
|
|
19
20
|
const ACTIVE_JOB_STATES = ['submitted', 'working', 'input-required'];
|
|
20
21
|
const FINISHED_JOB_STATES = ['completed', 'failed', 'canceled'];
|
|
@@ -159,6 +160,13 @@ function asError(error) {
|
|
|
159
160
|
const RECIPE_LETTER_ID = /^(recipe|relay)-/;
|
|
160
161
|
/** How many finished jobs the scoreboard reads, the newest: more than a machine ends in its window. */
|
|
161
162
|
const SCORED_JOBS = 5_000;
|
|
163
|
+
/** How far ahead the end of a limit may lie to be believed: a monthly cap resets within this. */
|
|
164
|
+
const LIMIT_MAX_AHEAD_MS = 32 * 86_400_000;
|
|
165
|
+
/** A limit in words: `out of its limit until 15:07 (You've hit your usage limit)`. */
|
|
166
|
+
function limitWords(limit, now) {
|
|
167
|
+
const until = limitClock(Date.parse(limit.until), now);
|
|
168
|
+
return `out of its limit until ${until}${limit.told ? '' : ', when the bridge tries it again'}${limit.detail ? ` (${limit.detail})` : ''}`;
|
|
169
|
+
}
|
|
162
170
|
/** How many reviews of its work the record of a job keeps, the first ones. */
|
|
163
171
|
const REVIEWS_KEPT = 20;
|
|
164
172
|
const NO_CONDUCTOR = 'This bridge carries out no recipes: its host gave it no conductor (recipeAdapter of @cogitator-ai/bridge/recipes)';
|
|
@@ -208,6 +216,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
208
216
|
rights;
|
|
209
217
|
person;
|
|
210
218
|
board;
|
|
219
|
+
fallback;
|
|
211
220
|
/** When each agent's other parties were last told it waits at its window, see `stuck` */
|
|
212
221
|
stuckTold = new Map();
|
|
213
222
|
/** The agents a listing of their harness showed waiting at their window, until it shows them not */
|
|
@@ -235,6 +244,10 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
235
244
|
* adapter reports back, since it may have handed the job's first letter over itself
|
|
236
245
|
*/
|
|
237
246
|
starting = new Set();
|
|
247
|
+
/** Session starts that wait their turn, see `startSession` */
|
|
248
|
+
startQueue = [];
|
|
249
|
+
/** How many sessions adapters are starting right now */
|
|
250
|
+
startsRunning = 0;
|
|
238
251
|
/** The waiter of each agent that has one, see `wait` */
|
|
239
252
|
waiters = new Map();
|
|
240
253
|
/**
|
|
@@ -262,6 +275,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
262
275
|
this.rights = options.rights;
|
|
263
276
|
this.person = options.person;
|
|
264
277
|
this.board = options.board;
|
|
278
|
+
this.fallback = (options.routing?.fallback ?? []).map((harness) => parse(HarnessNameSchema, harness, 'Cannot use the fallback'));
|
|
265
279
|
for (const adapter of options.adapters ?? []) {
|
|
266
280
|
const harness = parse(HarnessNameSchema, adapter.harness, 'Cannot use the adapter');
|
|
267
281
|
if (this.adapters.has(harness)) {
|
|
@@ -410,6 +424,19 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
410
424
|
: undefined;
|
|
411
425
|
const resolution = taken?.resolution ?? resolveAddress(this.store, request.to, addressed);
|
|
412
426
|
this.checkRecipeAsked(resolution, request.recipe);
|
|
427
|
+
const limited = this.limitRefusal(resolution);
|
|
428
|
+
if (limited) {
|
|
429
|
+
this.journal(tx, {
|
|
430
|
+
type: 'letter.rejected',
|
|
431
|
+
agent: from.name,
|
|
432
|
+
data: {
|
|
433
|
+
reason: limited.code,
|
|
434
|
+
to: request.to,
|
|
435
|
+
title: request.title ?? titleOf(request.body),
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
return new Refusal(limited);
|
|
439
|
+
}
|
|
413
440
|
if (this.closing && resolution.kind === 'spawn') {
|
|
414
441
|
throw new BridgeError('not_running', 'The bridge dispatcher is closing and starts no session now. Hand the job over again when it runs');
|
|
415
442
|
}
|
|
@@ -707,6 +734,9 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
707
734
|
}
|
|
708
735
|
if (request.kind === 'report') {
|
|
709
736
|
const state = letter.outcome === 'failed' ? 'failed' : 'completed';
|
|
737
|
+
if (state === 'completed') {
|
|
738
|
+
this.liftLimit(tx, from.harness, `${from.name} finished a job`);
|
|
739
|
+
}
|
|
710
740
|
await this.transition(tx, job.number, state, forHistory(request.body, letter.id));
|
|
711
741
|
if (state === 'failed')
|
|
712
742
|
await this.patchRecord(job.taskId, {
|
|
@@ -1631,7 +1661,9 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1631
1661
|
* session that is there would not wake, several fit, or the session is one the caller works
|
|
1632
1662
|
* for, which would then wait for the caller while the caller waits for it. A harness the
|
|
1633
1663
|
* bridge cannot start a session of right now is passed over for the next. `avoid` names a
|
|
1634
|
-
* harness to take
|
|
1664
|
+
* harness to take after the others: the one that did the work a review is of. The harnesses
|
|
1665
|
+
* of last resort come after all of those, so one of them takes the job only when no other
|
|
1666
|
+
* harness could, whether that one ran into its limit or simply has no session to give.
|
|
1635
1667
|
*
|
|
1636
1668
|
* @throws BridgeError `no_session` when the job can go to none of them
|
|
1637
1669
|
*/
|
|
@@ -1640,10 +1672,8 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1640
1672
|
const open = suggestion.candidates.filter((candidate) => candidate.available);
|
|
1641
1673
|
const first = open.find((candidate) => candidate.harness === suggestion.best);
|
|
1642
1674
|
const ranked = first ? [first, ...open.filter((candidate) => candidate !== first)] : open;
|
|
1643
|
-
const
|
|
1644
|
-
|
|
1645
|
-
...ranked.filter((candidate) => candidate.harness === avoid),
|
|
1646
|
-
];
|
|
1675
|
+
const rank = (candidate) => (candidate.lastResort ? 2 : 0) + (candidate.harness === avoid ? 1 : 0);
|
|
1676
|
+
const order = [...ranked].sort((a, b) => rank(a) - rank(b));
|
|
1647
1677
|
const above = this.worksFor(from.name);
|
|
1648
1678
|
const passed = [];
|
|
1649
1679
|
for (const candidate of order) {
|
|
@@ -2563,6 +2593,246 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2563
2593
|
return fitting(this.store.readJournal({ ...request, limit: request.limit ?? DEFAULT_LIST_LIMIT }), size);
|
|
2564
2594
|
});
|
|
2565
2595
|
}
|
|
2596
|
+
/**
|
|
2597
|
+
* Notes that the harness of the calling session ran into the limit of its subscription or its
|
|
2598
|
+
* key: an adapter says so when a turn fails with it, a hook of the harness when the harness
|
|
2599
|
+
* tells one. Until the limit is over (the time the harness named, `limits.limitRetryMs` from
|
|
2600
|
+
* now when it named none) no job is handed to that harness: `auto` passes it over, and a job
|
|
2601
|
+
* addressed to it is refused with `limit_reached`. The jobs the session carries out go to
|
|
2602
|
+
* another agent, the one `suggest` names for their type among those that take jobs, with what
|
|
2603
|
+
* the session left in the folder of each. A job nobody else can take fails, and not by the
|
|
2604
|
+
* doing of its agent.
|
|
2605
|
+
*
|
|
2606
|
+
* A time that is past, or further ahead than a limit lasts, is taken for no time at all.
|
|
2607
|
+
*
|
|
2608
|
+
* @throws BridgeError `not_registered`
|
|
2609
|
+
*/
|
|
2610
|
+
async limited(caller, input = {}) {
|
|
2611
|
+
const request = parse(LimitedInputSchema, input, 'Cannot note the limit');
|
|
2612
|
+
const now = this.now();
|
|
2613
|
+
const said = request.until === undefined ? undefined : Date.parse(request.until);
|
|
2614
|
+
const until = said !== undefined && said > now && said - now <= LIMIT_MAX_AHEAD_MS ? said : undefined;
|
|
2615
|
+
await this.asCaller(caller, async () => this.requireCaller(caller));
|
|
2616
|
+
return this.outOfLimit(caller, { ...(until !== undefined && { until }), detail: request.detail ?? '' }, false);
|
|
2617
|
+
}
|
|
2618
|
+
/** The harnesses that are out of their limit right now. */
|
|
2619
|
+
async limitsInForce() {
|
|
2620
|
+
return this.enqueue(async () => this.limitsNow());
|
|
2621
|
+
}
|
|
2622
|
+
/**
|
|
2623
|
+
* Takes the limit of a harness back before its time: the person knows it is over, such as
|
|
2624
|
+
* after they changed their plan. `agent` is a harness word (`codex`, `claude`) or the name of
|
|
2625
|
+
* one of its agents. Returns whether there was a limit.
|
|
2626
|
+
*
|
|
2627
|
+
* @throws BridgeError `unknown_agent` for a name nobody has
|
|
2628
|
+
*/
|
|
2629
|
+
async unlimit(agent) {
|
|
2630
|
+
return this.mutate(async (tx) => {
|
|
2631
|
+
const harness = agent.includes('@')
|
|
2632
|
+
? this.store.getAgent(agent.trim())?.harness
|
|
2633
|
+
: parse(HarnessNameSchema, harnessOfAlias(agent), 'Cannot lift the limit');
|
|
2634
|
+
if (harness === undefined) {
|
|
2635
|
+
throw new BridgeError('unknown_agent', `There is no agent ${agent}`, { address: agent });
|
|
2636
|
+
}
|
|
2637
|
+
return this.liftLimit(tx, harness, 'the person said it is over');
|
|
2638
|
+
});
|
|
2639
|
+
}
|
|
2640
|
+
limitsNow() {
|
|
2641
|
+
const now = this.now();
|
|
2642
|
+
return this.store.listLimits().filter((limit) => Date.parse(limit.until) > now);
|
|
2643
|
+
}
|
|
2644
|
+
limitOfHarness(harness) {
|
|
2645
|
+
return this.limitsNow().find((limit) => limit.harness === harness);
|
|
2646
|
+
}
|
|
2647
|
+
/** Why a job cannot go to the harness an address names, when that harness is out of its limit. */
|
|
2648
|
+
limitRefusal(resolution) {
|
|
2649
|
+
const harness = resolution.kind === 'agent' ? resolution.agent.harness : resolution.harness;
|
|
2650
|
+
const limit = this.limitOfHarness(harness);
|
|
2651
|
+
if (!limit)
|
|
2652
|
+
return undefined;
|
|
2653
|
+
return new BridgeError('limit_reached', `${aliasOfHarness(harness)} is ${limitWords(limit, this.now())}. Hand the job to another agent, or to ${AUTO_ADDRESS} for the one that takes jobs now`, { harness, until: limit.until });
|
|
2654
|
+
}
|
|
2655
|
+
/** Forgets the limit of a harness. Returns whether it had one. */
|
|
2656
|
+
liftLimit(tx, harness, why) {
|
|
2657
|
+
if (!this.store.listLimits().some((limit) => limit.harness === harness))
|
|
2658
|
+
return false;
|
|
2659
|
+
this.store.dropLimit(harness);
|
|
2660
|
+
this.journal(tx, { type: 'harness.unlimited', data: { harness, why } });
|
|
2661
|
+
return true;
|
|
2662
|
+
}
|
|
2663
|
+
/**
|
|
2664
|
+
* What the bridge does when the harness of an agent ran into its limit: notes the limit, and
|
|
2665
|
+
* hands the jobs the agent carries out to others. A limit that is noted already stays as it is
|
|
2666
|
+
* unless the new one lasts longer: a session that names no time must not cut short the time
|
|
2667
|
+
* another one named. `unstarted` says the session never came up, so the agent is gone and not
|
|
2668
|
+
* merely out of work. So is the session of a harness that waits for its mail itself, such as
|
|
2669
|
+
* Claude Code: a turn that ended on an error runs no hook that waits, so nothing wakes the
|
|
2670
|
+
* session until a person opens it, and it is back in the book as soon as it calls.
|
|
2671
|
+
*/
|
|
2672
|
+
async outOfLimit(agentName, signal, unstarted) {
|
|
2673
|
+
const noted = await this.mutate(async (tx) => {
|
|
2674
|
+
const agent = this.store.getAgent(agentName);
|
|
2675
|
+
if (!agent)
|
|
2676
|
+
throw new BridgeError('not_registered', `There is no agent ${agentName}`);
|
|
2677
|
+
const now = this.now();
|
|
2678
|
+
const before = this.limitOfHarness(agent.harness);
|
|
2679
|
+
const until = signal.until ?? now + this.limits.limitRetryMs;
|
|
2680
|
+
const limit = before && Date.parse(before.until) >= until
|
|
2681
|
+
? before
|
|
2682
|
+
: {
|
|
2683
|
+
harness: agent.harness,
|
|
2684
|
+
until: this.iso(until),
|
|
2685
|
+
told: signal.until !== undefined,
|
|
2686
|
+
detail: oneLine(signal.detail).slice(0, 300),
|
|
2687
|
+
since: before?.since ?? this.iso(now),
|
|
2688
|
+
agent: agent.name,
|
|
2689
|
+
};
|
|
2690
|
+
this.store.putLimit(limit);
|
|
2691
|
+
const adapter = this.adapters.get(agent.harness);
|
|
2692
|
+
const deaf = adapter !== undefined && adapter.deliver === undefined;
|
|
2693
|
+
if ((unstarted && agent.status === 'starting') || (deaf && agent.status === 'active')) {
|
|
2694
|
+
this.waiters.get(agent.name)?.end('closed');
|
|
2695
|
+
this.store.putAgent({ ...agent, status: 'gone' });
|
|
2696
|
+
this.journal(tx, {
|
|
2697
|
+
type: 'agent.gone',
|
|
2698
|
+
agent: agent.name,
|
|
2699
|
+
data: {
|
|
2700
|
+
error: unstarted
|
|
2701
|
+
? `the ${agent.harness} session did not start: ${limit.detail}`
|
|
2702
|
+
: `its turn ended on the limit of ${agent.harness}, and nothing wakes the session until it is opened again`,
|
|
2703
|
+
},
|
|
2704
|
+
});
|
|
2705
|
+
}
|
|
2706
|
+
const jobs = this.store
|
|
2707
|
+
.jobTasks({ to: agent.name, states: ACTIVE_JOB_STATES })
|
|
2708
|
+
.flatMap((task) => jobOf(task) ?? [])
|
|
2709
|
+
.sort((a, b) => a.number - b.number);
|
|
2710
|
+
this.journal(tx, {
|
|
2711
|
+
type: 'harness.limited',
|
|
2712
|
+
agent: agent.name,
|
|
2713
|
+
data: {
|
|
2714
|
+
harness: agent.harness,
|
|
2715
|
+
until: limit.until,
|
|
2716
|
+
told: limit.told,
|
|
2717
|
+
detail: limit.detail,
|
|
2718
|
+
},
|
|
2719
|
+
});
|
|
2720
|
+
if (!before) {
|
|
2721
|
+
tx.notices.push({
|
|
2722
|
+
agent: agent.name,
|
|
2723
|
+
jobs,
|
|
2724
|
+
text: `${aliasOfHarness(agent.harness)} is ${limitWords(limit, now)}. The bridge hands it no job until then${jobs.length > 0 ? ', and its jobs go to other agents' : ''}`,
|
|
2725
|
+
});
|
|
2726
|
+
}
|
|
2727
|
+
return { agent, limit, jobs: jobs.map((job) => ({ number: job.number, type: job.type })) };
|
|
2728
|
+
});
|
|
2729
|
+
const redirected = [];
|
|
2730
|
+
const failed = [];
|
|
2731
|
+
for (const job of noted.jobs) {
|
|
2732
|
+
const suggestion = await this.suggest(job.type === undefined ? {} : { type: job.type });
|
|
2733
|
+
const to = await this.mutate((tx) => this.redirect(tx, job.number, noted.agent, noted.limit, suggestion));
|
|
2734
|
+
if (to === undefined)
|
|
2735
|
+
continue;
|
|
2736
|
+
if (to === null)
|
|
2737
|
+
failed.push(job.number);
|
|
2738
|
+
else
|
|
2739
|
+
redirected.push({ job: job.number, to });
|
|
2740
|
+
}
|
|
2741
|
+
return { limit: noted.limit, redirected, failed };
|
|
2742
|
+
}
|
|
2743
|
+
/**
|
|
2744
|
+
* Hands a job whose agent ran into the limit of its harness to a fresh session of another
|
|
2745
|
+
* harness, which goes on in the folder of the job with what is there and gets the time of the
|
|
2746
|
+
* job anew. The letters of the job that waited are done with, the jobs handed over from inside
|
|
2747
|
+
* it are canceled, and whoever handed the job over is told. The agent that had it is told only
|
|
2748
|
+
* when its sessions fetch their mail themselves: a letter pushed to a session that is out of
|
|
2749
|
+
* its limit would start a turn that fails on it again. Returns the new agent, null when nobody
|
|
2750
|
+
* could take the job and it failed, undefined when the job needs no new agent any more.
|
|
2751
|
+
*/
|
|
2752
|
+
async redirect(tx, number, old, limit, suggestion) {
|
|
2753
|
+
const { job } = this.requireJob(number);
|
|
2754
|
+
if (isFinishedJobState(job.state) || job.to !== old.name)
|
|
2755
|
+
return undefined;
|
|
2756
|
+
const now = this.now();
|
|
2757
|
+
const next = suggestion.candidates
|
|
2758
|
+
.filter((candidate) => candidate.available && candidate.harness !== old.harness)
|
|
2759
|
+
.sort((a, b) => Number(b.harness === suggestion.best) - Number(a.harness === suggestion.best) ||
|
|
2760
|
+
Number(a.lastResort === true) - Number(b.lastResort === true))
|
|
2761
|
+
.find((candidate) => this.adapters.get(candidate.harness)?.createSession !== undefined);
|
|
2762
|
+
const why = `ran into the limit of ${aliasOfHarness(old.harness)} (${limit.detail || 'it takes no more requests'})`;
|
|
2763
|
+
if (!next) {
|
|
2764
|
+
await this.failJob(tx, number, `${old.name} ${why}, and no other agent takes jobs now. ${aliasOfHarness(old.harness)} is back at ${limitClock(Date.parse(limit.until), now)}`);
|
|
2765
|
+
return null;
|
|
2766
|
+
}
|
|
2767
|
+
const adapter = this.adapters.get(next.harness);
|
|
2768
|
+
const to = {
|
|
2769
|
+
name: allocateAgentName(this.store, next.harness, job.project),
|
|
2770
|
+
harness: next.harness,
|
|
2771
|
+
sessionId: '',
|
|
2772
|
+
project: job.project,
|
|
2773
|
+
cwd: job.cwd,
|
|
2774
|
+
delivery: adapter?.delivery ?? INBOX_DELIVERY,
|
|
2775
|
+
capabilities: adapter?.capabilities ?? NO_CAPABILITIES,
|
|
2776
|
+
status: 'starting',
|
|
2777
|
+
registeredAt: this.iso(now),
|
|
2778
|
+
lastSeenAt: this.iso(now),
|
|
2779
|
+
};
|
|
2780
|
+
this.store.putAgent(to);
|
|
2781
|
+
this.closeLetters(tx, number);
|
|
2782
|
+
await this.stopWaiting(number, now, false);
|
|
2783
|
+
await this.cancelChildren(tx, job);
|
|
2784
|
+
if (job.state === 'input-required') {
|
|
2785
|
+
await this.transition(tx, number, 'working', `Handed to ${to.name}`);
|
|
2786
|
+
}
|
|
2787
|
+
const task = await this.store.tasks.get(job.taskId);
|
|
2788
|
+
const record = task && jobRecordOf(task);
|
|
2789
|
+
if (!task || !record)
|
|
2790
|
+
throw new BridgeError('storage', `Job ${number} lost its record`);
|
|
2791
|
+
const { reportAskedAt: _asked, ...kept } = record;
|
|
2792
|
+
const parent = record.parent === undefined ? undefined : this.jobIfAny(record.parent);
|
|
2793
|
+
await this.store.tasks.update(job.taskId, {
|
|
2794
|
+
metadata: {
|
|
2795
|
+
...task.metadata,
|
|
2796
|
+
[TASK_AGENT_KEY]: to.name,
|
|
2797
|
+
[BRIDGE_JOB_KEY]: {
|
|
2798
|
+
...kept,
|
|
2799
|
+
to: to.name,
|
|
2800
|
+
lettersUsed: record.lettersUsed + 1,
|
|
2801
|
+
deadlineAt: this.iso(Math.min(now + record.budget.durationMs, parent ? Date.parse(parent.deadlineAt) : Infinity)),
|
|
2802
|
+
redirects: [
|
|
2803
|
+
...(record.redirects ?? []),
|
|
2804
|
+
{ from: old.name, to: to.name, at: this.iso(now), why },
|
|
2805
|
+
],
|
|
2806
|
+
},
|
|
2807
|
+
},
|
|
2808
|
+
});
|
|
2809
|
+
const [first] = this.store.listLetters({ job: number, kinds: ['task'], limit: 1 });
|
|
2810
|
+
if (!first)
|
|
2811
|
+
throw new BridgeError('storage', `The first letter of job ${number} is lost`);
|
|
2812
|
+
const letter = this.acceptLetter(tx, {
|
|
2813
|
+
id: this.letterId(),
|
|
2814
|
+
job: number,
|
|
2815
|
+
kind: 'task',
|
|
2816
|
+
from: job.from,
|
|
2817
|
+
to: to.name,
|
|
2818
|
+
body: first.body,
|
|
2819
|
+
replyDueAt: this.iso(now + job.budget.replyTimeoutMs),
|
|
2820
|
+
});
|
|
2821
|
+
tx.wake.delete(to.name);
|
|
2822
|
+
tx.spawns.push({ agent: to.name, job: number, letter: letter.id });
|
|
2823
|
+
const fresh = this.requireJob(number).job;
|
|
2824
|
+
this.journal(tx, {
|
|
2825
|
+
type: 'job.redirected',
|
|
2826
|
+
job: number,
|
|
2827
|
+
agent: old.name,
|
|
2828
|
+
data: { from: old.name, to: to.name, why },
|
|
2829
|
+
});
|
|
2830
|
+
this.systemNote(tx, fresh, job.from, `Job #${number}: ${old.name} ${why}, so the bridge handed the job to ${to.name}, which goes on in the folder of the job with what is there. Its questions and its report come from ${to.name} now. Nothing for you to do.`);
|
|
2831
|
+
if (this.adapters.get(old.harness)?.deliver === undefined) {
|
|
2832
|
+
this.systemNote(tx, fresh, old.name, `Job #${number} is no longer yours: your harness ran into its limit, and the bridge handed the job to ${to.name}. Stop working on it.`);
|
|
2833
|
+
}
|
|
2834
|
+
return to.name;
|
|
2835
|
+
}
|
|
2566
2836
|
/**
|
|
2567
2837
|
* What became of the jobs that ended within the window of the scoreboard
|
|
2568
2838
|
* (`limits.scoreWindowMs`), newest first: who carried each out, its type, whether anything
|
|
@@ -2585,16 +2855,22 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2585
2855
|
*/
|
|
2586
2856
|
async suggest(input = {}) {
|
|
2587
2857
|
const request = parse(SuggestInputSchema, input, 'Cannot suggest an agent');
|
|
2588
|
-
const { results, harnesses } = await this.enqueue(async () => ({
|
|
2858
|
+
const { results, harnesses, limits } = await this.enqueue(async () => ({
|
|
2589
2859
|
results: this.scored(),
|
|
2590
2860
|
harnesses: this.routable(),
|
|
2861
|
+
limits: new Map(this.limitsNow().map((limit) => [limit.harness, limit])),
|
|
2591
2862
|
}));
|
|
2863
|
+
const now = this.now();
|
|
2592
2864
|
return suggestFrom(results, {
|
|
2593
2865
|
...(request.type !== undefined && { type: request.type }),
|
|
2594
2866
|
minJobs: this.limits.scoreMinJobs,
|
|
2595
2867
|
windowMs: this.limits.scoreWindowMs,
|
|
2596
2868
|
harnesses,
|
|
2597
|
-
|
|
2869
|
+
lastResort: this.fallback.filter((harness) => harnesses.includes(harness)),
|
|
2870
|
+
unavailable: (harness) => {
|
|
2871
|
+
const limit = limits.get(harness);
|
|
2872
|
+
return limit && limitWords(limit, now);
|
|
2873
|
+
},
|
|
2598
2874
|
});
|
|
2599
2875
|
}
|
|
2600
2876
|
/**
|
|
@@ -2647,6 +2923,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2647
2923
|
.filter((adapter) => adapter.createSession !== undefined && !adapter.recipeProblem)
|
|
2648
2924
|
.map((adapter) => adapter.harness),
|
|
2649
2925
|
waiting: [...this.waiters.keys()].sort(),
|
|
2926
|
+
limited: this.limitsNow(),
|
|
2650
2927
|
}));
|
|
2651
2928
|
}
|
|
2652
2929
|
/**
|
|
@@ -2726,6 +3003,10 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2726
3003
|
await this.failJob(tx, job.number, `${letter.to} did not ${pickup ? 'take' : 'answer'} the ${letter.kind} ${letter.id} in ${formatDuration(2 * job.budget.replyTimeoutMs)}`, letter.to === job.to ? 'silent' : 'other');
|
|
2727
3004
|
failed.push(job.number);
|
|
2728
3005
|
}
|
|
3006
|
+
for (const limit of this.store.listLimits()) {
|
|
3007
|
+
if (Date.parse(limit.until) <= now)
|
|
3008
|
+
this.liftLimit(tx, limit.harness, 'its time is over');
|
|
3009
|
+
}
|
|
2729
3010
|
failed.push(...(await this.remindOfReports(tx, now)));
|
|
2730
3011
|
failed.push(...(await this.retireAgents(tx, now)));
|
|
2731
3012
|
for (const agent of this.store.queuedRecipients())
|
|
@@ -3510,11 +3791,29 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
3510
3791
|
nextAttemptAt: this.iso(this.now() + deliveryBackoff(attempts, this.limits)),
|
|
3511
3792
|
});
|
|
3512
3793
|
}
|
|
3794
|
+
/**
|
|
3795
|
+
* Starts the session of an agent for a job, or queues the start when as many sessions are
|
|
3796
|
+
* being started as may be at once (`limits.maxStartingSessions`). A job handed over by an
|
|
3797
|
+
* agent is refused before it gets here, so what waits in the queue is what the bridge itself
|
|
3798
|
+
* hands on: the jobs of an agent that ran into its limit, each of which needs a session.
|
|
3799
|
+
*/
|
|
3513
3800
|
startSession(agentName, jobNumber, letterId) {
|
|
3514
3801
|
this.starting.add(agentName);
|
|
3802
|
+
if (this.startsRunning >= this.limits.maxStartingSessions) {
|
|
3803
|
+
this.startQueue.push({ agent: agentName, job: jobNumber, letter: letterId });
|
|
3804
|
+
return;
|
|
3805
|
+
}
|
|
3806
|
+
this.launchSession(agentName, jobNumber, letterId);
|
|
3807
|
+
}
|
|
3808
|
+
launchSession(agentName, jobNumber, letterId) {
|
|
3809
|
+
this.startsRunning += 1;
|
|
3515
3810
|
this.track(this.runSessionStart(agentName, jobNumber, letterId).finally(() => {
|
|
3811
|
+
this.startsRunning -= 1;
|
|
3516
3812
|
if (this.starting.delete(agentName))
|
|
3517
3813
|
this.wakeIfKnown(agentName);
|
|
3814
|
+
const next = this.closing ? undefined : this.startQueue.shift();
|
|
3815
|
+
if (next)
|
|
3816
|
+
this.launchSession(next.agent, next.job, next.letter);
|
|
3518
3817
|
}));
|
|
3519
3818
|
}
|
|
3520
3819
|
wakeIfKnown(agentName) {
|
|
@@ -3526,12 +3825,23 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
3526
3825
|
}));
|
|
3527
3826
|
}
|
|
3528
3827
|
async runSessionStart(agentName, jobNumber, letterId) {
|
|
3529
|
-
const request = await this.
|
|
3828
|
+
const request = await this.mutate(async (tx) => {
|
|
3530
3829
|
const agent = this.store.getAgent(agentName);
|
|
3531
3830
|
const letter = this.store.getLetter(letterId);
|
|
3532
3831
|
if (!agent || !letter)
|
|
3533
3832
|
return undefined;
|
|
3534
3833
|
const { job } = this.requireJob(jobNumber);
|
|
3834
|
+
if (isFinishedJobState(job.state) || job.to !== agentName) {
|
|
3835
|
+
if (agent.status === 'starting') {
|
|
3836
|
+
this.store.putAgent({ ...agent, status: 'gone' });
|
|
3837
|
+
this.journal(tx, {
|
|
3838
|
+
type: 'agent.gone',
|
|
3839
|
+
agent: agentName,
|
|
3840
|
+
data: { error: `job #${jobNumber} was over before its session was started` },
|
|
3841
|
+
});
|
|
3842
|
+
}
|
|
3843
|
+
return undefined;
|
|
3844
|
+
}
|
|
3535
3845
|
return { agent, job, letter: renderLetter(letter, job) };
|
|
3536
3846
|
});
|
|
3537
3847
|
const adapter = request && this.adapters.get(request.agent.harness);
|
|
@@ -3554,6 +3864,11 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
3554
3864
|
}
|
|
3555
3865
|
catch (error) {
|
|
3556
3866
|
const message = asError(error).message;
|
|
3867
|
+
const signal = limitOf(message, this.now());
|
|
3868
|
+
if (signal) {
|
|
3869
|
+
await this.outOfLimit(agentName, signal, true);
|
|
3870
|
+
return;
|
|
3871
|
+
}
|
|
3557
3872
|
await this.mutate(async (tx) => {
|
|
3558
3873
|
const agent = this.store.getAgent(agentName);
|
|
3559
3874
|
if (agent?.status !== 'starting')
|