@cogitator-ai/bridge 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +242 -8
- package/dist/adapter.d.ts +130 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +2 -0
- package/dist/adapter.js.map +1 -0
- package/dist/adapters/claude.d.ts +145 -0
- package/dist/adapters/claude.d.ts.map +1 -0
- package/dist/adapters/claude.js +537 -0
- package/dist/adapters/claude.js.map +1 -0
- package/dist/adapters/codex.d.ts +71 -0
- package/dist/adapters/codex.d.ts.map +1 -0
- package/dist/adapters/codex.js +379 -0
- package/dist/adapters/codex.js.map +1 -0
- package/dist/adapters/exec.d.ts +70 -0
- package/dist/adapters/exec.d.ts.map +1 -0
- package/dist/adapters/exec.js +161 -0
- package/dist/adapters/exec.js.map +1 -0
- package/dist/adapters/hosted.d.ts +168 -0
- package/dist/adapters/hosted.d.ts.map +1 -0
- package/dist/adapters/hosted.js +361 -0
- package/dist/adapters/hosted.js.map +1 -0
- package/dist/adapters/tools-server.d.ts +21 -0
- package/dist/adapters/tools-server.d.ts.map +1 -0
- package/dist/adapters/tools-server.js +19 -0
- package/dist/adapters/tools-server.js.map +1 -0
- package/dist/client.d.ts +9 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -3
- package/dist/client.js.map +1 -1
- package/dist/dispatcher.d.ts +69 -17
- package/dist/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher.js +243 -17
- package/dist/dispatcher.js.map +1 -1
- package/dist/host.d.ts +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/index.d.ts +18 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/inputs.d.ts +40 -1
- package/dist/inputs.d.ts.map +1 -1
- package/dist/inputs.js +25 -2
- package/dist/inputs.js.map +1 -1
- package/dist/paths.d.ts +6 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +19 -0
- package/dist/paths.js.map +1 -1
- package/dist/protocol.d.ts +5 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +5 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +6 -1
- package/dist/server.js.map +1 -1
- package/dist/setup/claude.d.ts +53 -0
- package/dist/setup/claude.d.ts.map +1 -0
- package/dist/setup/claude.js +261 -0
- package/dist/setup/claude.js.map +1 -0
- package/dist/setup/codex.d.ts +22 -0
- package/dist/setup/codex.d.ts.map +1 -0
- package/dist/setup/codex.js +95 -0
- package/dist/setup/codex.js.map +1 -0
- package/dist/setup/files.d.ts +22 -0
- package/dist/setup/files.d.ts.map +1 -0
- package/dist/setup/files.js +86 -0
- package/dist/setup/files.js.map +1 -0
- package/dist/setup/index.d.ts +6 -0
- package/dist/setup/index.d.ts.map +1 -0
- package/dist/setup/index.js +3 -0
- package/dist/setup/index.js.map +1 -0
- package/dist/setup/setup.d.ts +52 -0
- package/dist/setup/setup.d.ts.map +1 -0
- package/dist/setup/setup.js +2 -0
- package/dist/setup/setup.js.map +1 -0
- package/dist/testing/conformance.d.ts +65 -0
- package/dist/testing/conformance.d.ts.map +1 -0
- package/dist/testing/conformance.js +217 -0
- package/dist/testing/conformance.js.map +1 -0
- package/dist/testing/fake-harnesses.d.ts +121 -0
- package/dist/testing/fake-harnesses.d.ts.map +1 -0
- package/dist/testing/fake-harnesses.js +303 -0
- package/dist/testing/fake-harnesses.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +3 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tools/caller.d.ts +39 -0
- package/dist/tools/caller.d.ts.map +1 -0
- package/dist/tools/caller.js +92 -0
- package/dist/tools/caller.js.map +1 -0
- package/dist/tools/guidance.d.ts +33 -0
- package/dist/tools/guidance.d.ts.map +1 -0
- package/dist/tools/guidance.js +92 -0
- package/dist/tools/guidance.js.map +1 -0
- package/dist/tools/index.d.ts +27 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +24 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/tools.d.ts +38 -0
- package/dist/tools/tools.d.ts.map +1 -0
- package/dist/tools/tools.js +354 -0
- package/dist/tools/tools.js.map +1 -0
- package/dist/types.d.ts +3 -39
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -1
- package/dist/waiter.d.ts +36 -0
- package/dist/waiter.d.ts.map +1 -0
- package/dist/waiter.js +92 -0
- package/dist/waiter.js.map +1 -0
- package/fakes/claude.mjs +715 -0
- package/fakes/codex.mjs +571 -0
- package/fakes/lib.mjs +585 -0
- package/fakes/mcp.mjs +151 -0
- package/fakes/sandbox.mjs +84 -0
- package/package.json +19 -1
package/dist/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { connect } from 'node:net';
|
|
2
2
|
import { JsonRpcPeer, encodeNdjsonLine, readNdjson, } from '@cogitator-ai/server-shared';
|
|
3
3
|
import { BridgeError } from './errors.js';
|
|
4
|
+
import { DEFAULT_WAIT_MS, } from './inputs.js';
|
|
4
5
|
import { BRIDGE_METHODS, BRIDGE_NOTIFICATIONS, BRIDGE_PROTOCOL_VERSION, fromJsonRpcError, } from './protocol.js';
|
|
5
6
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
6
7
|
const NO_DISPATCHER_CODES = new Set(['ENOENT', 'ECONNREFUSED', 'ENOTSOCK']);
|
|
@@ -119,6 +120,19 @@ export class BridgeClient {
|
|
|
119
120
|
acknowledge(letter) {
|
|
120
121
|
return this.call(BRIDGE_METHODS.acknowledge, { letter });
|
|
121
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Waits for the session's mail, see `BridgeDispatcher.wait`. The connection answers nothing
|
|
125
|
+
* else until the wait ends: a process that waits uses a connection of its own. Closing the
|
|
126
|
+
* client ends the wait
|
|
127
|
+
*/
|
|
128
|
+
wait(input = {}) {
|
|
129
|
+
const waitMs = input.timeoutMs ?? DEFAULT_WAIT_MS;
|
|
130
|
+
return this.call(BRIDGE_METHODS.wait, input, waitMs + this.timeoutMs);
|
|
131
|
+
}
|
|
132
|
+
/** Says the letters a wait returned reached the session */
|
|
133
|
+
delivered(letters) {
|
|
134
|
+
return this.call(BRIDGE_METHODS.delivered, { letters });
|
|
135
|
+
}
|
|
122
136
|
cancel(job, reason) {
|
|
123
137
|
return this.call(BRIDGE_METHODS.cancelJob, { job, ...(reason !== undefined && { reason }) });
|
|
124
138
|
}
|
|
@@ -182,7 +196,7 @@ export class BridgeClient {
|
|
|
182
196
|
}
|
|
183
197
|
/** Ends the connection. Calls still waiting for an answer reject */
|
|
184
198
|
async close() {
|
|
185
|
-
this.peer.close();
|
|
199
|
+
this.peer.close(new BridgeError('not_running', 'The connection to the bridge was closed'));
|
|
186
200
|
if (this.socket.destroyed)
|
|
187
201
|
return;
|
|
188
202
|
await new Promise((resolve) => {
|
|
@@ -190,9 +204,9 @@ export class BridgeClient {
|
|
|
190
204
|
this.socket.end();
|
|
191
205
|
});
|
|
192
206
|
}
|
|
193
|
-
async call(method, params) {
|
|
207
|
+
async call(method, params, timeoutMs = this.timeoutMs) {
|
|
194
208
|
try {
|
|
195
|
-
return await this.peer.request(method, params, { timeoutMs
|
|
209
|
+
return await this.peer.request(method, params, { timeoutMs });
|
|
196
210
|
}
|
|
197
211
|
catch (error) {
|
|
198
212
|
throw fromJsonRpcError(error);
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,UAAU,CAAC;AAChD,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,UAAU,GAEX,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client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EAAoE;IACpE,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,WAAW,CAAC,aAAa,EAAE,yCAAyC,CAAC,CAAC,CAAC;QAC3F,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS;YAAE,OAAO;QAClC,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAClC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,IAAI,CAAI,MAAc,EAAE,MAAe,EAAE,SAAS,GAAG,IAAI,CAAC,SAAS;QAC/E,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAI,MAAM,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;QACnE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,gBAAgB,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CACF"}
|
package/dist/dispatcher.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
2
|
import { type BridgeLimitsInput } from './guards.js';
|
|
3
|
-
import { type CreateJobInput, type InboxInput, type SendLetterInput } from './inputs.js';
|
|
3
|
+
import { type CreateJobInput, type CreateJobResult, type InboxInput, type SendLetterInput, type SendLetterResult, type WaitInput, type WaitResult } from './inputs.js';
|
|
4
4
|
import type { AgentFilter, BridgeStore, JobTaskFilter, JournalQuery, LetterFilter } from './store/store.js';
|
|
5
|
-
import { type AgentRecord, type
|
|
5
|
+
import { type AgentRecord, type BridgeJob, type BridgeLimits, type CallerIdentity, type JobVerification, type JournalEntry, type Letter, type RenderedLetter } from './types.js';
|
|
6
|
+
import type { BridgeAdapter, BridgeCalls } from './adapter.js';
|
|
6
7
|
export interface BridgeDispatcherOptions {
|
|
7
8
|
store: BridgeStore;
|
|
8
9
|
/** How the bridge reaches the sessions of each harness. A harness without one reads its inbox */
|
|
@@ -21,18 +22,7 @@ export interface BridgeDispatcherOptions {
|
|
|
21
22
|
*/
|
|
22
23
|
assertWritable?: () => void;
|
|
23
24
|
}
|
|
24
|
-
export
|
|
25
|
-
job: BridgeJob;
|
|
26
|
-
letter: Letter;
|
|
27
|
-
/** True when the job was created before with the same `letterId`, and nothing changed now */
|
|
28
|
-
duplicate: boolean;
|
|
29
|
-
}
|
|
30
|
-
export interface SendLetterResult {
|
|
31
|
-
letter: Letter;
|
|
32
|
-
job: BridgeJob;
|
|
33
|
-
/** True when the letter was sent before with the same id, and nothing changed now */
|
|
34
|
-
duplicate: boolean;
|
|
35
|
-
}
|
|
25
|
+
export type { CreateJobResult, SendLetterResult } from './inputs.js';
|
|
36
26
|
export interface SweepResult {
|
|
37
27
|
/** Jobs failed for running out of time or for a letter nobody reacted to */
|
|
38
28
|
failed: number[];
|
|
@@ -45,6 +35,10 @@ export interface DispatcherStatus {
|
|
|
45
35
|
queuedLetters: number;
|
|
46
36
|
/** The harnesses the dispatcher has an adapter for */
|
|
47
37
|
adapters: string[];
|
|
38
|
+
/** The harnesses it can start a session of, for a job handed to one that has none */
|
|
39
|
+
startable: string[];
|
|
40
|
+
/** The agents with a waiter armed: a letter for one of them wakes its session at once */
|
|
41
|
+
waiting: string[];
|
|
48
42
|
}
|
|
49
43
|
export interface BridgeDispatcherEvents {
|
|
50
44
|
/** Every entry of the journal, once what it tells is stored */
|
|
@@ -71,7 +65,7 @@ export interface BridgeDispatcherEvents {
|
|
|
71
65
|
* await bridge.send(codex.name, { job: job.number, kind: 'report', body: 'Done: 14 tests' });
|
|
72
66
|
* ```
|
|
73
67
|
*/
|
|
74
|
-
export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvents> {
|
|
68
|
+
export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvents> implements BridgeCalls {
|
|
75
69
|
readonly limits: BridgeLimits;
|
|
76
70
|
private readonly store;
|
|
77
71
|
private readonly tasks;
|
|
@@ -88,6 +82,18 @@ export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvent
|
|
|
88
82
|
* adapter reports back, since it may have handed the job's first letter over itself
|
|
89
83
|
*/
|
|
90
84
|
private readonly starting;
|
|
85
|
+
/** The waiter of each agent that has one, see `wait` */
|
|
86
|
+
private readonly waiters;
|
|
87
|
+
/**
|
|
88
|
+
* Letters somebody holds right now: a waiter that has not confirmed them yet, or an adapter in
|
|
89
|
+
* the middle of pushing one. Nobody else is given a letter in hand, so a session reached two
|
|
90
|
+
* ways does not get it twice
|
|
91
|
+
*/
|
|
92
|
+
private readonly inHand;
|
|
93
|
+
/** What each harness with an adapter that lists its sessions said of them, see `checkPresence` */
|
|
94
|
+
private readonly presence;
|
|
95
|
+
private readonly listing;
|
|
96
|
+
private presenceCheckedAt;
|
|
91
97
|
/** Set once `close` was called: nothing new is delivered, what runs is waited for */
|
|
92
98
|
private closing;
|
|
93
99
|
private closed;
|
|
@@ -125,6 +131,39 @@ export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvent
|
|
|
125
131
|
* always comes first. `afterSeq` looks further than one call returns.
|
|
126
132
|
*/
|
|
127
133
|
inbox(caller: string, input?: InboxInput): Promise<RenderedLetter[]>;
|
|
134
|
+
/**
|
|
135
|
+
* Waits for the caller's mail: answers at once when letters are queued for it, otherwise as
|
|
136
|
+
* soon as one arrives, or with none when `timeoutMs` passes. This is how a session nothing can
|
|
137
|
+
* be pushed into still wakes: a process of its harness waits here and hands what it gets to
|
|
138
|
+
* the session, as `cogitator bridge wait` does in a hook of Claude Code. The letters stay
|
|
139
|
+
* queued until the waiter says it handed them over (`delivered`), so a waiter that dies with
|
|
140
|
+
* them in hand loses nothing. A session has one waiter: a new one ends the one before it.
|
|
141
|
+
* Aborting `signal` ends the wait, as the socket server does when the connection closes.
|
|
142
|
+
*/
|
|
143
|
+
wait(caller: string, input?: WaitInput, signal?: AbortSignal): Promise<WaitResult>;
|
|
144
|
+
/**
|
|
145
|
+
* Says the caller's waiter handed these letters to its session. A job handed over this way
|
|
146
|
+
* counts as taken, as one an adapter pushed does. A letter that is not queued any more, such
|
|
147
|
+
* as one the session read from its inbox meanwhile, stays as it is.
|
|
148
|
+
*
|
|
149
|
+
* @throws BridgeError `unknown_letter` for a letter that is not addressed to the caller
|
|
150
|
+
*/
|
|
151
|
+
delivered(caller: string, letterIds: readonly string[]): Promise<Letter[]>;
|
|
152
|
+
private arm;
|
|
153
|
+
/**
|
|
154
|
+
* The letters queued for an agent that nobody holds, oldest first, as many as one answer
|
|
155
|
+
* carries.
|
|
156
|
+
*/
|
|
157
|
+
private queuedFor;
|
|
158
|
+
private held;
|
|
159
|
+
/**
|
|
160
|
+
* Notes that a waiter was given these letters. They are in its hand until it confirms them,
|
|
161
|
+
* its connection closes, a newer waiter of the session takes over, or `deliveryTimeoutMs`
|
|
162
|
+
* passes, whichever comes first.
|
|
163
|
+
*/
|
|
164
|
+
private hand;
|
|
165
|
+
/** Frees what the waiters of an agent hold: a newer waiter is about to take their place. */
|
|
166
|
+
private letGo;
|
|
128
167
|
/**
|
|
129
168
|
* Says the caller is done with a letter that needs no answer. A question, a permission request
|
|
130
169
|
* and a job's first letter are not dismissed this way: they are marked read, and are done with
|
|
@@ -168,6 +207,18 @@ export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvent
|
|
|
168
207
|
* the deliveries that are due again. The host calls it on a timer.
|
|
169
208
|
*/
|
|
170
209
|
sweep(): Promise<SweepResult>;
|
|
210
|
+
/**
|
|
211
|
+
* Asks the adapters that can list their sessions which of them still run, once in
|
|
212
|
+
* `limits.presenceCheckMs`. A session that was listed and then is not, two times in a row, is
|
|
213
|
+
* gone, and its jobs fail with that reason rather than after the reply timeout: one miss may
|
|
214
|
+
* be a session between two states of its harness. A session with a waiter armed is there
|
|
215
|
+
* whatever the list says. One that was never listed (its harness shows it nowhere) is left to
|
|
216
|
+
* the idle rule, unless the adapter runs every session of its harness itself (`ownsSessions`):
|
|
217
|
+
* then what it does not list is gone at once. A session heard from after the list was asked
|
|
218
|
+
* for is not judged by it: it may have started meanwhile.
|
|
219
|
+
*/
|
|
220
|
+
private checkPresence;
|
|
221
|
+
private applyPresence;
|
|
171
222
|
/**
|
|
172
223
|
* Takes sessions that are not there out of the address book, so a harness word keeps reaching
|
|
173
224
|
* the session that is: one whose start never finished (the dispatcher that started it is
|
|
@@ -180,8 +231,9 @@ export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvent
|
|
|
180
231
|
/** Resolves once every call made so far is handled and every delivery it started has ended. */
|
|
181
232
|
idle(): Promise<void>;
|
|
182
233
|
/**
|
|
183
|
-
*
|
|
184
|
-
* takes no more. A
|
|
234
|
+
* Ends the waits, waits for the calls being handled, the deliveries and the session starts they
|
|
235
|
+
* began, closes the adapters, then takes no more. A session a call asks for after this began
|
|
236
|
+
* is not started: the next dispatcher finds it never started and fails its job. A delivery that hangs is given up on after `limits.deliveryTimeoutMs` and a
|
|
185
237
|
* session start after `limits.sessionStartTimeoutMs`, so this always ends.
|
|
186
238
|
*/
|
|
187
239
|
close(): Promise<void>;
|
package/dist/dispatcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../src/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,EAOL,KAAK,iBAAiB,EACvB,MAAM,aAAa,CAAC;AACrB,OAAO,
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../src/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,EAOL,KAAK,iBAAiB,EACvB,MAAM,aAAa,C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uDvB;;;OAGG;IACH,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,MAAM;IA6Bd,sGAAsG;IACtG,OAAO,CAAC,UAAU;IAWlB,OAAO,CAAC,cAAc;IA6DtB,OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,WAAW;IAkBnB;;;;OAIG;YACW,aAAa;IA0B3B,OAAO,CAAC,KAAK;IAOb,mGAAmG;IACnG,OAAO,CAAC,IAAI;YAWE,SAAS;IAyDvB,OAAO,CAAC,cAAc;IA0BtB,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,WAAW;YASL,eAAe;CAyE9B"}
|
package/dist/dispatcher.js
CHANGED
|
@@ -3,10 +3,10 @@ import { isAbsolute, resolve as resolvePath } from 'node:path';
|
|
|
3
3
|
import { TaskManager, textPart, toMessage } from '@cogitator-ai/a2a';
|
|
4
4
|
import { BridgeError } from './errors.js';
|
|
5
5
|
import { deliveryBackoff, exhaustedBudget, formatDuration, nextDepth, resolveBudget, resolveLimits, } from './guards.js';
|
|
6
|
-
import { AgentsFilterSchema, CreateJobInputSchema, DEFAULT_INBOX_LIMIT, DEFAULT_LIST_LIMIT, InboxInputSchema, JobsFilterSchema, JournalQuerySchema, LettersFilterSchema, SendLetterInputSchema, VerificationInputSchema, } from './inputs.js';
|
|
6
|
+
import { AgentsFilterSchema, CreateJobInputSchema, DEFAULT_INBOX_LIMIT, DEFAULT_LIST_LIMIT, DEFAULT_WAIT_MS, DeliveredInputSchema, InboxInputSchema, JobsFilterSchema, JournalQuerySchema, LettersFilterSchema, SendLetterInputSchema, VerificationInputSchema, WaitInputSchema, } from './inputs.js';
|
|
7
7
|
import { isFinishedJobState, jobOf, jobRecordOf } from './job-record.js';
|
|
8
8
|
import { expectationOf, forHistory, newLetterId, normalizeBody, oneLine, quoted, renderLetter, repeats, titleOf, } from './letters.js';
|
|
9
|
-
import { findCheckoutRoot, findProjectRoot, isInside } from './paths.js';
|
|
9
|
+
import { canonicalPath, findCheckoutRoot, findProjectRoot, isInside } from './paths.js';
|
|
10
10
|
import { allocateAgentName, resolveAddress } from './registry.js';
|
|
11
11
|
import { BRIDGE_JOB_KEY, BRIDGE_SENDER, CallerIdentitySchema, DEFAULT_LIMITS, HarnessNameSchema, INBOX_DELIVERY, NO_CAPABILITIES, } from './types.js';
|
|
12
12
|
const ACTIVE_JOB_STATES = ['submitted', 'working', 'input-required'];
|
|
@@ -118,6 +118,18 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
118
118
|
* adapter reports back, since it may have handed the job's first letter over itself
|
|
119
119
|
*/
|
|
120
120
|
starting = new Set();
|
|
121
|
+
/** The waiter of each agent that has one, see `wait` */
|
|
122
|
+
waiters = new Map();
|
|
123
|
+
/**
|
|
124
|
+
* Letters somebody holds right now: a waiter that has not confirmed them yet, or an adapter in
|
|
125
|
+
* the middle of pushing one. Nobody else is given a letter in hand, so a session reached two
|
|
126
|
+
* ways does not get it twice
|
|
127
|
+
*/
|
|
128
|
+
inHand = new Map();
|
|
129
|
+
/** What each harness with an adapter that lists its sessions said of them, see `checkPresence` */
|
|
130
|
+
presence = new Map();
|
|
131
|
+
listing = new Set();
|
|
132
|
+
presenceCheckedAt = Number.NEGATIVE_INFINITY;
|
|
121
133
|
/** Set once `close` was called: nothing new is delivered, what runs is waited for */
|
|
122
134
|
closing = false;
|
|
123
135
|
closed = false;
|
|
@@ -137,6 +149,8 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
137
149
|
}
|
|
138
150
|
this.adapters.set(harness, adapter);
|
|
139
151
|
}
|
|
152
|
+
for (const adapter of this.adapters.values())
|
|
153
|
+
adapter.attach?.(this);
|
|
140
154
|
}
|
|
141
155
|
/**
|
|
142
156
|
* Puts the calling session into the address book, or finds it there. A session is known by
|
|
@@ -147,8 +161,8 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
147
161
|
const caller = parse(CallerIdentitySchema, identity, 'Cannot register the session');
|
|
148
162
|
return this.mutate(async (tx) => {
|
|
149
163
|
const now = this.iso();
|
|
150
|
-
const cwd =
|
|
151
|
-
const project =
|
|
164
|
+
const cwd = canonicalPath(caller.cwd);
|
|
165
|
+
const project = canonicalPath(caller.project ?? findProjectRoot(cwd));
|
|
152
166
|
const adapter = this.adapters.get(caller.harness);
|
|
153
167
|
const reach = {
|
|
154
168
|
delivery: adapter?.delivery ?? INBOX_DELIVERY,
|
|
@@ -278,9 +292,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
278
292
|
}
|
|
279
293
|
const budget = resolveBudget(request.budget, this.limits);
|
|
280
294
|
const cwd = request.cwd
|
|
281
|
-
? isAbsolute(request.cwd)
|
|
282
|
-
? resolvePath(request.cwd)
|
|
283
|
-
: resolvePath(from.cwd, request.cwd)
|
|
295
|
+
? canonicalPath(isAbsolute(request.cwd) ? request.cwd : resolvePath(from.cwd, request.cwd))
|
|
284
296
|
: from.cwd;
|
|
285
297
|
if (!isInside(from.project, cwd) && !isInside(findCheckoutRoot(from.cwd), cwd)) {
|
|
286
298
|
throw new BridgeError('invalid_input', `A job is done inside the project it is handed over in (${from.project}) or the checkout you work in, and ${cwd} is outside both`, { cwd, project: from.project });
|
|
@@ -536,6 +548,128 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
536
548
|
return rendered;
|
|
537
549
|
});
|
|
538
550
|
}
|
|
551
|
+
/**
|
|
552
|
+
* Waits for the caller's mail: answers at once when letters are queued for it, otherwise as
|
|
553
|
+
* soon as one arrives, or with none when `timeoutMs` passes. This is how a session nothing can
|
|
554
|
+
* be pushed into still wakes: a process of its harness waits here and hands what it gets to
|
|
555
|
+
* the session, as `cogitator bridge wait` does in a hook of Claude Code. The letters stay
|
|
556
|
+
* queued until the waiter says it handed them over (`delivered`), so a waiter that dies with
|
|
557
|
+
* them in hand loses nothing. A session has one waiter: a new one ends the one before it.
|
|
558
|
+
* Aborting `signal` ends the wait, as the socket server does when the connection closes.
|
|
559
|
+
*/
|
|
560
|
+
async wait(caller, input = {}, signal) {
|
|
561
|
+
const request = parse(WaitInputSchema, input, 'Cannot wait for letters');
|
|
562
|
+
const limit = request.limit ?? DEFAULT_INBOX_LIMIT;
|
|
563
|
+
const outcome = await this.asCaller(caller, async () => {
|
|
564
|
+
const agent = this.requireCaller(caller);
|
|
565
|
+
this.waiters.get(agent.name)?.end('replaced');
|
|
566
|
+
this.letGo(agent.name);
|
|
567
|
+
if (signal?.aborted || this.closing)
|
|
568
|
+
return { letters: [], reason: 'closed' };
|
|
569
|
+
const letters = this.starting.has(agent.name) ? [] : this.queuedFor(agent.name, limit);
|
|
570
|
+
if (letters.length > 0) {
|
|
571
|
+
this.hand(agent.name, letters, signal);
|
|
572
|
+
return { letters, reason: 'letters' };
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
later: this.arm(agent.name, limit, request.timeoutMs ?? DEFAULT_WAIT_MS, signal),
|
|
576
|
+
};
|
|
577
|
+
});
|
|
578
|
+
return 'later' in outcome ? outcome.later : outcome;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Says the caller's waiter handed these letters to its session. A job handed over this way
|
|
582
|
+
* counts as taken, as one an adapter pushed does. A letter that is not queued any more, such
|
|
583
|
+
* as one the session read from its inbox meanwhile, stays as it is.
|
|
584
|
+
*
|
|
585
|
+
* @throws BridgeError `unknown_letter` for a letter that is not addressed to the caller
|
|
586
|
+
*/
|
|
587
|
+
async delivered(caller, letterIds) {
|
|
588
|
+
const { letters: ids } = parse(DeliveredInputSchema, { letters: [...letterIds] }, 'Cannot mark the letters delivered');
|
|
589
|
+
return this.asCaller(caller, async (tx) => {
|
|
590
|
+
const agent = this.requireCaller(caller);
|
|
591
|
+
const marked = [];
|
|
592
|
+
for (const id of new Set(ids)) {
|
|
593
|
+
if (this.store.getLetter(id)?.to !== agent.name) {
|
|
594
|
+
throw new BridgeError('unknown_letter', `There is no letter ${id} addressed to you`, {
|
|
595
|
+
letter: id,
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
await this.markDelivered(tx, id, { via: 'wait' });
|
|
599
|
+
const letter = this.store.getLetter(id);
|
|
600
|
+
if (letter)
|
|
601
|
+
marked.push(letter);
|
|
602
|
+
}
|
|
603
|
+
tx.wake.add(agent.name);
|
|
604
|
+
return marked;
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
arm(name, limit, timeoutMs, signal) {
|
|
608
|
+
return new Promise((resolve) => {
|
|
609
|
+
const waiter = {
|
|
610
|
+
limit,
|
|
611
|
+
signal,
|
|
612
|
+
end: (reason, letters = []) => {
|
|
613
|
+
if (this.waiters.get(name) === waiter)
|
|
614
|
+
this.waiters.delete(name);
|
|
615
|
+
clearTimeout(timer);
|
|
616
|
+
signal?.removeEventListener('abort', onAbort);
|
|
617
|
+
resolve({ letters, reason });
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
const timer = setTimeout(() => waiter.end('timeout'), timeoutMs);
|
|
621
|
+
const onAbort = () => waiter.end('closed');
|
|
622
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
623
|
+
this.waiters.set(name, waiter);
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* The letters queued for an agent that nobody holds, oldest first, as many as one answer
|
|
628
|
+
* carries.
|
|
629
|
+
*/
|
|
630
|
+
queuedFor(name, limit) {
|
|
631
|
+
const queued = this.store
|
|
632
|
+
.listLetters({ to: name, states: ['queued'], limit })
|
|
633
|
+
.filter((letter) => !this.held(letter.id));
|
|
634
|
+
return fitting(queued, (letter) => 2 * letter.body.length).map((letter) => renderLetter(letter, this.requireJob(letter.job).job));
|
|
635
|
+
}
|
|
636
|
+
held(letterId) {
|
|
637
|
+
const holder = this.inHand.get(letterId);
|
|
638
|
+
if (!holder)
|
|
639
|
+
return false;
|
|
640
|
+
if (holder.until > this.now())
|
|
641
|
+
return true;
|
|
642
|
+
this.inHand.delete(letterId);
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Notes that a waiter was given these letters. They are in its hand until it confirms them,
|
|
647
|
+
* its connection closes, a newer waiter of the session takes over, or `deliveryTimeoutMs`
|
|
648
|
+
* passes, whichever comes first.
|
|
649
|
+
*/
|
|
650
|
+
hand(name, letters, signal) {
|
|
651
|
+
const holder = { agent: name, until: this.now() + this.limits.deliveryTimeoutMs };
|
|
652
|
+
for (const letter of letters)
|
|
653
|
+
this.inHand.set(letter.id, holder);
|
|
654
|
+
signal?.addEventListener('abort', () => {
|
|
655
|
+
let dropped = false;
|
|
656
|
+
for (const letter of letters) {
|
|
657
|
+
if (this.inHand.get(letter.id) !== holder)
|
|
658
|
+
continue;
|
|
659
|
+
this.inHand.delete(letter.id);
|
|
660
|
+
dropped = true;
|
|
661
|
+
}
|
|
662
|
+
if (dropped)
|
|
663
|
+
this.wakeIfKnown(name);
|
|
664
|
+
}, { once: true });
|
|
665
|
+
}
|
|
666
|
+
/** Frees what the waiters of an agent hold: a newer waiter is about to take their place. */
|
|
667
|
+
letGo(name) {
|
|
668
|
+
for (const [letterId, holder] of this.inHand) {
|
|
669
|
+
if (holder.agent === name && !holder.pushing)
|
|
670
|
+
this.inHand.delete(letterId);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
539
673
|
/**
|
|
540
674
|
* Says the caller is done with a letter that needs no answer. A question, a permission request
|
|
541
675
|
* and a job's first letter are not dismissed this way: they are marked read, and are done with
|
|
@@ -660,6 +794,10 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
660
794
|
activeJobs: this.store.jobTasks({ states: ACTIVE_JOB_STATES }).length,
|
|
661
795
|
queuedLetters: this.store.countLetters({ states: ['queued'] }),
|
|
662
796
|
adapters: [...this.adapters.keys()],
|
|
797
|
+
startable: [...this.adapters.values()]
|
|
798
|
+
.filter((adapter) => adapter.createSession !== undefined)
|
|
799
|
+
.map((adapter) => adapter.harness),
|
|
800
|
+
waiting: [...this.waiters.keys()].sort(),
|
|
663
801
|
}));
|
|
664
802
|
}
|
|
665
803
|
/**
|
|
@@ -719,8 +857,58 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
719
857
|
tx.wake.add(agent);
|
|
720
858
|
return { failed: [...new Set(failed)], reminded };
|
|
721
859
|
});
|
|
860
|
+
this.checkPresence();
|
|
722
861
|
return result;
|
|
723
862
|
}
|
|
863
|
+
/**
|
|
864
|
+
* Asks the adapters that can list their sessions which of them still run, once in
|
|
865
|
+
* `limits.presenceCheckMs`. A session that was listed and then is not, two times in a row, is
|
|
866
|
+
* gone, and its jobs fail with that reason rather than after the reply timeout: one miss may
|
|
867
|
+
* be a session between two states of its harness. A session with a waiter armed is there
|
|
868
|
+
* whatever the list says. One that was never listed (its harness shows it nowhere) is left to
|
|
869
|
+
* the idle rule, unless the adapter runs every session of its harness itself (`ownsSessions`):
|
|
870
|
+
* then what it does not list is gone at once. A session heard from after the list was asked
|
|
871
|
+
* for is not judged by it: it may have started meanwhile.
|
|
872
|
+
*/
|
|
873
|
+
checkPresence() {
|
|
874
|
+
if (this.closing)
|
|
875
|
+
return;
|
|
876
|
+
const now = this.now();
|
|
877
|
+
if (now - this.presenceCheckedAt < this.limits.presenceCheckMs)
|
|
878
|
+
return;
|
|
879
|
+
this.presenceCheckedAt = now;
|
|
880
|
+
for (const [harness, adapter] of this.adapters) {
|
|
881
|
+
if (!adapter.liveSessions || this.listing.has(harness))
|
|
882
|
+
continue;
|
|
883
|
+
this.listing.add(harness);
|
|
884
|
+
const asked = now;
|
|
885
|
+
this.track(within(adapter.liveSessions(), this.limits.deliveryTimeoutMs, `Listing the ${harness} sessions`)
|
|
886
|
+
.then((sessions) => this.mutate((tx) => this.applyPresence(tx, harness, new Set(sessions.map((session) => session.sessionId)), adapter.ownsSessions === true, asked)))
|
|
887
|
+
.finally(() => this.listing.delete(harness)));
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
async applyPresence(tx, harness, live, certain, asked) {
|
|
891
|
+
const known = this.presence.get(harness) ?? { live, seen: new Set(), missed: new Set() };
|
|
892
|
+
const missed = new Set();
|
|
893
|
+
for (const sessionId of live)
|
|
894
|
+
known.seen.add(sessionId);
|
|
895
|
+
for (const agent of this.store.listAgents({ harness, statuses: ['active'] })) {
|
|
896
|
+
const id = agent.sessionId;
|
|
897
|
+
if (live.has(id) || this.waiters.has(agent.name) || this.starting.has(agent.name))
|
|
898
|
+
continue;
|
|
899
|
+
if (Date.parse(agent.lastSeenAt) >= asked)
|
|
900
|
+
continue;
|
|
901
|
+
if (!certain && !known.seen.has(id))
|
|
902
|
+
continue;
|
|
903
|
+
if (!certain && !known.missed.has(id)) {
|
|
904
|
+
missed.add(id);
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
known.seen.delete(id);
|
|
908
|
+
await this.agentGone(tx, agent, `the session of ${agent.name} is not running any more`);
|
|
909
|
+
}
|
|
910
|
+
this.presence.set(harness, { live, seen: known.seen, missed });
|
|
911
|
+
}
|
|
724
912
|
/**
|
|
725
913
|
* Takes sessions that are not there out of the address book, so a harness word keeps reaching
|
|
726
914
|
* the session that is: one whose start never finished (the dispatcher that started it is
|
|
@@ -739,6 +927,10 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
739
927
|
seenBefore: now - this.limits.agentIdleMs,
|
|
740
928
|
});
|
|
741
929
|
for (const agent of silent) {
|
|
930
|
+
if (this.waiters.has(agent.name))
|
|
931
|
+
continue;
|
|
932
|
+
if (this.presence.get(agent.harness)?.live.has(agent.sessionId))
|
|
933
|
+
continue;
|
|
742
934
|
const busy = this.store.jobTasks({ party: agent.name, states: ACTIVE_JOB_STATES, limit: 1 });
|
|
743
935
|
if (busy.length > 0)
|
|
744
936
|
continue;
|
|
@@ -776,13 +968,24 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
776
968
|
}
|
|
777
969
|
}
|
|
778
970
|
/**
|
|
779
|
-
*
|
|
780
|
-
* takes no more. A
|
|
971
|
+
* Ends the waits, waits for the calls being handled, the deliveries and the session starts they
|
|
972
|
+
* began, closes the adapters, then takes no more. A session a call asks for after this began
|
|
973
|
+
* is not started: the next dispatcher finds it never started and fails its job. A delivery that hangs is given up on after `limits.deliveryTimeoutMs` and a
|
|
781
974
|
* session start after `limits.sessionStartTimeoutMs`, so this always ends.
|
|
782
975
|
*/
|
|
783
976
|
async close() {
|
|
784
977
|
this.closing = true;
|
|
978
|
+
for (const waiter of [...this.waiters.values()])
|
|
979
|
+
waiter.end('closed');
|
|
785
980
|
await this.idle();
|
|
981
|
+
await Promise.all([...this.adapters.values()].map(async (adapter) => {
|
|
982
|
+
try {
|
|
983
|
+
await adapter.close?.();
|
|
984
|
+
}
|
|
985
|
+
catch (error) {
|
|
986
|
+
this.onError(asError(error));
|
|
987
|
+
}
|
|
988
|
+
}));
|
|
786
989
|
this.closed = true;
|
|
787
990
|
}
|
|
788
991
|
iso(ms = this.now()) {
|
|
@@ -827,8 +1030,10 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
827
1030
|
this.onError(asError(error));
|
|
828
1031
|
}
|
|
829
1032
|
}
|
|
830
|
-
|
|
831
|
-
|
|
1033
|
+
if (!this.closing) {
|
|
1034
|
+
for (const spawn of tx.spawns)
|
|
1035
|
+
this.startSession(spawn.agent, spawn.job, spawn.letter);
|
|
1036
|
+
}
|
|
832
1037
|
for (const agent of tx.wake)
|
|
833
1038
|
this.wake(agent);
|
|
834
1039
|
return result;
|
|
@@ -1112,7 +1317,8 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1112
1317
|
* taken: the harness has it and woke the session, which may work without calling the bridge
|
|
1113
1318
|
* until it reports. The letter is read only once the session calls about the job.
|
|
1114
1319
|
*/
|
|
1115
|
-
async markDelivered(tx, letterId) {
|
|
1320
|
+
async markDelivered(tx, letterId, data) {
|
|
1321
|
+
this.inHand.delete(letterId);
|
|
1116
1322
|
const letter = this.store.getLetter(letterId);
|
|
1117
1323
|
if (letter?.state !== 'queued')
|
|
1118
1324
|
return;
|
|
@@ -1127,6 +1333,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1127
1333
|
job: letter.job,
|
|
1128
1334
|
letter: letter.id,
|
|
1129
1335
|
agent: letter.to,
|
|
1336
|
+
...(data && { data }),
|
|
1130
1337
|
});
|
|
1131
1338
|
if (taken && this.requireJob(letter.job).job.state === 'submitted') {
|
|
1132
1339
|
await this.transition(tx, letter.job, 'working');
|
|
@@ -1160,8 +1367,18 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1160
1367
|
const agent = this.store.getAgent(name);
|
|
1161
1368
|
if (agent?.status !== 'active')
|
|
1162
1369
|
return undefined;
|
|
1370
|
+
const waiter = this.waiters.get(name);
|
|
1371
|
+
if (waiter) {
|
|
1372
|
+
const letters = this.queuedFor(name, waiter.limit);
|
|
1373
|
+
if (letters.length > 0) {
|
|
1374
|
+
this.hand(name, letters, waiter.signal);
|
|
1375
|
+
waiter.end('letters', letters);
|
|
1376
|
+
}
|
|
1377
|
+
return undefined;
|
|
1378
|
+
}
|
|
1163
1379
|
const adapter = this.adapters.get(agent.harness);
|
|
1164
|
-
|
|
1380
|
+
const deliver = adapter?.capabilities.push ? adapter.deliver?.bind(adapter) : undefined;
|
|
1381
|
+
if (!deliver)
|
|
1165
1382
|
return undefined;
|
|
1166
1383
|
const letter = this.store.listLetters({
|
|
1167
1384
|
to: name,
|
|
@@ -1169,23 +1386,32 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1169
1386
|
attemptsBelow: this.limits.deliveryAttempts,
|
|
1170
1387
|
limit: 1,
|
|
1171
1388
|
})[0];
|
|
1172
|
-
if (!letter)
|
|
1389
|
+
if (!letter || this.held(letter.id))
|
|
1173
1390
|
return undefined;
|
|
1174
1391
|
if (letter.nextAttemptAt && Date.parse(letter.nextAttemptAt) > this.now())
|
|
1175
1392
|
return undefined;
|
|
1176
1393
|
const { job } = this.requireJob(letter.job);
|
|
1177
|
-
|
|
1394
|
+
this.inHand.set(letter.id, {
|
|
1395
|
+
agent: name,
|
|
1396
|
+
until: this.now() + this.limits.deliveryTimeoutMs,
|
|
1397
|
+
pushing: true,
|
|
1398
|
+
});
|
|
1399
|
+
return { agent, deliver, job, letter: renderLetter(letter, job) };
|
|
1178
1400
|
});
|
|
1179
1401
|
if (!next)
|
|
1180
1402
|
return;
|
|
1403
|
+
let note;
|
|
1181
1404
|
try {
|
|
1182
|
-
await within(next.
|
|
1405
|
+
const receipt = await within(next.deliver({ agent: next.agent, letter: next.letter, job: next.job }), this.limits.deliveryTimeoutMs, `Delivering the letter ${next.letter.id} to ${name}`);
|
|
1406
|
+
note = receipt?.note;
|
|
1183
1407
|
}
|
|
1184
1408
|
catch (error) {
|
|
1409
|
+
this.inHand.delete(next.letter.id);
|
|
1185
1410
|
await this.mutate(async (tx) => this.deliveryFailed(tx, next.letter.id, asError(error)));
|
|
1186
1411
|
return;
|
|
1187
1412
|
}
|
|
1188
|
-
|
|
1413
|
+
this.inHand.delete(next.letter.id);
|
|
1414
|
+
await this.mutate((tx) => this.markDelivered(tx, next.letter.id, note ? { note } : undefined));
|
|
1189
1415
|
}
|
|
1190
1416
|
}
|
|
1191
1417
|
deliveryFailed(tx, letterId, error) {
|