@cogitator-ai/bridge 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +330 -2
- package/dist/adapter.d.ts +123 -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 +90 -0
- package/dist/adapters/claude.d.ts.map +1 -0
- package/dist/adapters/claude.js +359 -0
- package/dist/adapters/claude.js.map +1 -0
- package/dist/adapters/codex.d.ts +58 -0
- package/dist/adapters/codex.d.ts.map +1 -0
- package/dist/adapters/codex.js +330 -0
- package/dist/adapters/codex.js.map +1 -0
- package/dist/adapters/exec.d.ts +57 -0
- package/dist/adapters/exec.d.ts.map +1 -0
- package/dist/adapters/exec.js +149 -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/client.d.ts +81 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +216 -0
- package/dist/client.js.map +1 -0
- package/dist/dispatcher.d.ts +296 -0
- package/dist/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher.js +1524 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -0
- package/dist/guards.d.ts +36 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +85 -0
- package/dist/guards.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +150 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/inputs.d.ts +184 -0
- package/dist/inputs.d.ts.map +1 -0
- package/dist/inputs.js +116 -0
- package/dist/inputs.js.map +1 -0
- package/dist/job-record.d.ts +47 -0
- package/dist/job-record.d.ts.map +1 -0
- package/dist/job-record.js +65 -0
- package/dist/job-record.js.map +1 -0
- package/dist/letters.d.ts +49 -0
- package/dist/letters.d.ts.map +1 -0
- package/dist/letters.js +96 -0
- package/dist/letters.js.map +1 -0
- package/dist/paths.d.ts +38 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +149 -0
- package/dist/paths.js.map +1 -0
- package/dist/protocol.d.ts +83 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +71 -0
- package/dist/protocol.js.map +1 -0
- package/dist/registry.d.ts +62 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +185 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +31 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +254 -0
- package/dist/server.js.map +1 -0
- package/dist/store/sqlite.d.ts +56 -0
- package/dist/store/sqlite.d.ts.map +1 -0
- package/dist/store/sqlite.js +737 -0
- package/dist/store/sqlite.js.map +1 -0
- package/dist/store/store.d.ts +128 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +2 -0
- package/dist/store/store.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 +101 -0
- package/dist/testing/fake-harnesses.d.ts.map +1 -0
- package/dist/testing/fake-harnesses.js +291 -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/types.d.ts +286 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +132 -0
- package/dist/types.js.map +1 -0
- 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 +450 -0
- package/fakes/codex.mjs +391 -0
- package/fakes/lib.mjs +507 -0
- package/package.json +76 -3
package/dist/client.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { connect } from 'node:net';
|
|
2
|
+
import { JsonRpcPeer, encodeNdjsonLine, readNdjson, } from '@cogitator-ai/server-shared';
|
|
3
|
+
import { BridgeError } from './errors.js';
|
|
4
|
+
import { DEFAULT_WAIT_MS, } from './inputs.js';
|
|
5
|
+
import { BRIDGE_METHODS, BRIDGE_NOTIFICATIONS, BRIDGE_PROTOCOL_VERSION, fromJsonRpcError, } from './protocol.js';
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
7
|
+
const NO_DISPATCHER_CODES = new Set(['ENOENT', 'ECONNREFUSED', 'ENOTSOCK']);
|
|
8
|
+
function open(socketPath) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const socket = connect(socketPath);
|
|
11
|
+
const onError = (error) => {
|
|
12
|
+
reject(error.code && NO_DISPATCHER_CODES.has(error.code)
|
|
13
|
+
? new BridgeError('not_running', 'No bridge dispatcher is running on this machine. Start one: cogitator bridge serve', { socket: socketPath })
|
|
14
|
+
: error);
|
|
15
|
+
};
|
|
16
|
+
socket.once('error', onError);
|
|
17
|
+
socket.once('connect', () => {
|
|
18
|
+
socket.off('error', onError);
|
|
19
|
+
resolve(socket);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A connection to the dispatcher of this machine. Created for a session it acts as that agent:
|
|
25
|
+
* the jobs it hands over and the letters it sends are from it, its inbox is that agent's.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const caller = callerFromEnv(process.env, process.cwd());
|
|
30
|
+
* const bridge = await BridgeClient.connect({ socketPath: bridgePaths().socket, caller });
|
|
31
|
+
* const { job } = await bridge.createJob({ to: 'codex', body: 'Write the tests' });
|
|
32
|
+
* await bridge.close();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class BridgeClient {
|
|
36
|
+
socket;
|
|
37
|
+
peer;
|
|
38
|
+
timeoutMs;
|
|
39
|
+
listeners;
|
|
40
|
+
dispatcher;
|
|
41
|
+
agent;
|
|
42
|
+
/** What the dispatcher sends this connection: one job, everything, or nothing */
|
|
43
|
+
subscribed = null;
|
|
44
|
+
/** The last change of the subscription: they run one after another, each seeing the listeners as they are then */
|
|
45
|
+
syncing = Promise.resolve();
|
|
46
|
+
constructor(socket, peer, timeoutMs, listeners,
|
|
47
|
+
/** The dispatcher the client reached */
|
|
48
|
+
dispatcher,
|
|
49
|
+
/** The agent the client acts as, when it was created for a session */
|
|
50
|
+
agent) {
|
|
51
|
+
this.socket = socket;
|
|
52
|
+
this.peer = peer;
|
|
53
|
+
this.timeoutMs = timeoutMs;
|
|
54
|
+
this.listeners = listeners;
|
|
55
|
+
this.dispatcher = dispatcher;
|
|
56
|
+
this.agent = agent;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @throws BridgeError `not_running` when no dispatcher listens on the socket
|
|
60
|
+
*/
|
|
61
|
+
static async connect(options) {
|
|
62
|
+
const socket = await open(options.socketPath);
|
|
63
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
64
|
+
const peer = new JsonRpcPeer({
|
|
65
|
+
send: (message) => {
|
|
66
|
+
socket.write(encodeNdjsonLine(message));
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const listeners = new Map();
|
|
70
|
+
peer.onNotification(BRIDGE_NOTIFICATIONS.journalEntry, (value) => {
|
|
71
|
+
const entry = value;
|
|
72
|
+
for (const [listener, job] of [...listeners]) {
|
|
73
|
+
if (job === undefined || job === entry.job)
|
|
74
|
+
listener(entry);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const closed = new BridgeError('not_running', 'The connection to the bridge dispatcher closed');
|
|
78
|
+
socket.on('error', () => peer.close(closed));
|
|
79
|
+
socket.on('close', () => peer.close(closed));
|
|
80
|
+
void (async () => {
|
|
81
|
+
try {
|
|
82
|
+
for await (const line of readNdjson(socket)) {
|
|
83
|
+
if ('value' in line) {
|
|
84
|
+
void peer.receive(line.value);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
peer.close(new BridgeError('storage', `The bridge dispatcher sent an answer the client could not read (${line.error.message}). Ask for less at once with a limit`));
|
|
88
|
+
socket.destroy();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
peer.close(closed);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
let hello;
|
|
97
|
+
try {
|
|
98
|
+
hello = await peer.request(BRIDGE_METHODS.hello, { protocol: BRIDGE_PROTOCOL_VERSION, ...(options.caller && { caller: options.caller }) }, { timeoutMs });
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
peer.close();
|
|
102
|
+
socket.destroy();
|
|
103
|
+
throw fromJsonRpcError(error);
|
|
104
|
+
}
|
|
105
|
+
return new BridgeClient(socket, peer, timeoutMs, listeners, hello.dispatcher, hello.agent);
|
|
106
|
+
}
|
|
107
|
+
/** Whether the connection is gone */
|
|
108
|
+
get closed() {
|
|
109
|
+
return this.peer.closed;
|
|
110
|
+
}
|
|
111
|
+
createJob(input) {
|
|
112
|
+
return this.call(BRIDGE_METHODS.createJob, input);
|
|
113
|
+
}
|
|
114
|
+
send(input) {
|
|
115
|
+
return this.call(BRIDGE_METHODS.send, input);
|
|
116
|
+
}
|
|
117
|
+
inbox(input = {}) {
|
|
118
|
+
return this.call(BRIDGE_METHODS.inbox, input);
|
|
119
|
+
}
|
|
120
|
+
acknowledge(letter) {
|
|
121
|
+
return this.call(BRIDGE_METHODS.acknowledge, { letter });
|
|
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
|
+
}
|
|
136
|
+
cancel(job, reason) {
|
|
137
|
+
return this.call(BRIDGE_METHODS.cancelJob, { job, ...(reason !== undefined && { reason }) });
|
|
138
|
+
}
|
|
139
|
+
job(number) {
|
|
140
|
+
return this.call(BRIDGE_METHODS.getJob, { job: number });
|
|
141
|
+
}
|
|
142
|
+
jobs(filter = {}) {
|
|
143
|
+
return this.call(BRIDGE_METHODS.listJobs, filter);
|
|
144
|
+
}
|
|
145
|
+
agents(filter = {}) {
|
|
146
|
+
return this.call(BRIDGE_METHODS.listAgents, filter);
|
|
147
|
+
}
|
|
148
|
+
letters(filter = {}) {
|
|
149
|
+
return this.call(BRIDGE_METHODS.listLetters, filter);
|
|
150
|
+
}
|
|
151
|
+
readJournal(query = {}) {
|
|
152
|
+
return this.call(BRIDGE_METHODS.readJournal, query);
|
|
153
|
+
}
|
|
154
|
+
status() {
|
|
155
|
+
return this.call(BRIDGE_METHODS.status, {});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Hears every entry the journal gets from now on, of one job or of all. Each listener has its
|
|
159
|
+
* own filter. Resolves once the dispatcher is sending, with a function that stops it.
|
|
160
|
+
*/
|
|
161
|
+
async subscribe(listener, options = {}) {
|
|
162
|
+
this.listeners.set(listener, options.job);
|
|
163
|
+
try {
|
|
164
|
+
await this.syncSubscription();
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
this.listeners.delete(listener);
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
return async () => {
|
|
171
|
+
if (!this.listeners.delete(listener) || this.peer.closed)
|
|
172
|
+
return;
|
|
173
|
+
await this.syncSubscription();
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Asks the dispatcher for what the listeners need between them: one job when they all want the
|
|
178
|
+
* same one, everything otherwise, nothing when none is left. Changes run one after another, so
|
|
179
|
+
* one made while another is on its way is not lost.
|
|
180
|
+
*/
|
|
181
|
+
syncSubscription() {
|
|
182
|
+
const sync = this.syncing.then(async () => {
|
|
183
|
+
const jobs = new Set(this.listeners.values());
|
|
184
|
+
const wanted = this.listeners.size === 0 ? null : jobs.size === 1 ? ([...jobs][0] ?? 'all') : 'all';
|
|
185
|
+
if (wanted === this.subscribed)
|
|
186
|
+
return;
|
|
187
|
+
if (wanted === null)
|
|
188
|
+
await this.call(BRIDGE_METHODS.unsubscribeJournal, {});
|
|
189
|
+
else {
|
|
190
|
+
await this.call(BRIDGE_METHODS.subscribeJournal, wanted === 'all' ? {} : { job: wanted });
|
|
191
|
+
}
|
|
192
|
+
this.subscribed = wanted;
|
|
193
|
+
});
|
|
194
|
+
this.syncing = sync.catch(() => undefined);
|
|
195
|
+
return sync;
|
|
196
|
+
}
|
|
197
|
+
/** Ends the connection. Calls still waiting for an answer reject */
|
|
198
|
+
async close() {
|
|
199
|
+
this.peer.close(new BridgeError('not_running', 'The connection to the bridge was closed'));
|
|
200
|
+
if (this.socket.destroyed)
|
|
201
|
+
return;
|
|
202
|
+
await new Promise((resolve) => {
|
|
203
|
+
this.socket.once('close', () => resolve());
|
|
204
|
+
this.socket.end();
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async call(method, params, timeoutMs = this.timeoutMs) {
|
|
208
|
+
try {
|
|
209
|
+
return await this.peer.request(method, params, { timeoutMs });
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
throw fromJsonRpcError(error);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +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;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACL,eAAe,GAQhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,uBAAuB,EACvB,gBAAgB,GAIjB,MAAM,eAAe,CAAC;AAoBvB,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC,CAAC;AAE5E,SAAS,IAAI,CAAC,UAAkB;IAC9B,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC7C,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QACnC,MAAM,OAAO,GAAG,CAAC,KAA4B,EAAE,EAAE;YAC/C,MAAM,CACJ,KAAK,CAAC,IAAI,IAAI,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC;gBAC/C,CAAC,CAAC,IAAI,WAAW,CACb,aAAa,EACb,oFAAoF,EACpF,EAAE,MAAM,EAAE,UAAU,EAAE,CACvB;gBACH,CAAC,CAAC,KAAK,CACV,CAAC;QACJ,CAAC,CAAC;QACF,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC9B,MAAM,C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AE,MAAM,EAAE,CAAC,CAAC;YAC5F,CAAC;YACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC;QAC3B,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,oEAAoE;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"}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { type BridgeLimitsInput } from './guards.js';
|
|
3
|
+
import { type CreateJobInput, type CreateJobResult, type InboxInput, type SendLetterInput, type SendLetterResult, type WaitInput, type WaitResult } from './inputs.js';
|
|
4
|
+
import type { AgentFilter, BridgeStore, JobTaskFilter, JournalQuery, LetterFilter } from './store/store.js';
|
|
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';
|
|
7
|
+
export interface BridgeDispatcherOptions {
|
|
8
|
+
store: BridgeStore;
|
|
9
|
+
/** How the bridge reaches the sessions of each harness. A harness without one reads its inbox */
|
|
10
|
+
adapters?: readonly BridgeAdapter[];
|
|
11
|
+
limits?: BridgeLimitsInput;
|
|
12
|
+
/** Ms since the epoch, `Date.now` by default */
|
|
13
|
+
now?: () => number;
|
|
14
|
+
/** The id of a letter whose sender gave none */
|
|
15
|
+
newLetterId?: () => string;
|
|
16
|
+
/** Hears what went wrong outside a call: a delivery or a session start that threw */
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Called inside every transaction before anything is written, to throw when this dispatcher
|
|
20
|
+
* may not write any more: the host checks its lease here, so a dispatcher that was replaced
|
|
21
|
+
* while its process slept writes nothing after it wakes
|
|
22
|
+
*/
|
|
23
|
+
assertWritable?: () => void;
|
|
24
|
+
}
|
|
25
|
+
export type { CreateJobResult, SendLetterResult } from './inputs.js';
|
|
26
|
+
export interface SweepResult {
|
|
27
|
+
/** Jobs failed for running out of time or for a letter nobody reacted to */
|
|
28
|
+
failed: number[];
|
|
29
|
+
/** Letters whose recipient was reminded */
|
|
30
|
+
reminded: string[];
|
|
31
|
+
}
|
|
32
|
+
export interface DispatcherStatus {
|
|
33
|
+
agents: number;
|
|
34
|
+
activeJobs: number;
|
|
35
|
+
queuedLetters: number;
|
|
36
|
+
/** The harnesses the dispatcher has an adapter for */
|
|
37
|
+
adapters: string[];
|
|
38
|
+
/** The agents with a waiter armed: a letter for one of them wakes its session at once */
|
|
39
|
+
waiting: string[];
|
|
40
|
+
}
|
|
41
|
+
export interface BridgeDispatcherEvents {
|
|
42
|
+
/** Every entry of the journal, once what it tells is stored */
|
|
43
|
+
journal: [JournalEntry];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The bridge itself: it keeps jobs, letters and the address book, decides who a letter is for,
|
|
47
|
+
* applies the limits and hands letters to the adapters of the harnesses. It never calls a model
|
|
48
|
+
* and never waits for an agent: every call answers at once, and whatever comes later arrives as
|
|
49
|
+
* a letter.
|
|
50
|
+
*
|
|
51
|
+
* One dispatcher runs per machine. Calls are handled one at a time, each in a transaction of the
|
|
52
|
+
* store, so a call either leaves everything it did or nothing.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const store = await SqliteBridgeStore.open(':memory:');
|
|
57
|
+
* const bridge = new BridgeDispatcher({ store });
|
|
58
|
+
* const claude = await bridge.register({ harness: 'claude-code', sessionId: 'a', cwd: '/work/app' });
|
|
59
|
+
* const codex = await bridge.register({ harness: 'codex', sessionId: 'b', cwd: '/work/app' });
|
|
60
|
+
*
|
|
61
|
+
* const { job } = await bridge.createJob(claude.name, { to: 'codex', body: 'Write the tests' });
|
|
62
|
+
* const [task] = await bridge.inbox(codex.name);
|
|
63
|
+
* await bridge.send(codex.name, { job: job.number, kind: 'report', body: 'Done: 14 tests' });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare class BridgeDispatcher extends EventEmitter<BridgeDispatcherEvents> implements BridgeCalls {
|
|
67
|
+
readonly limits: BridgeLimits;
|
|
68
|
+
private readonly store;
|
|
69
|
+
private readonly tasks;
|
|
70
|
+
private readonly adapters;
|
|
71
|
+
private readonly now;
|
|
72
|
+
private readonly letterId;
|
|
73
|
+
private readonly onError;
|
|
74
|
+
private readonly assertWritable;
|
|
75
|
+
private queue;
|
|
76
|
+
private readonly deliveries;
|
|
77
|
+
private readonly background;
|
|
78
|
+
/**
|
|
79
|
+
* Agents whose session an adapter is starting right now. Nothing is pushed to them until the
|
|
80
|
+
* adapter reports back, since it may have handed the job's first letter over itself
|
|
81
|
+
*/
|
|
82
|
+
private readonly starting;
|
|
83
|
+
/** The waiter of each agent that has one, see `wait` */
|
|
84
|
+
private readonly waiters;
|
|
85
|
+
/**
|
|
86
|
+
* Letters somebody holds right now: a waiter that has not confirmed them yet, or an adapter in
|
|
87
|
+
* the middle of pushing one. Nobody else is given a letter in hand, so a session reached two
|
|
88
|
+
* ways does not get it twice
|
|
89
|
+
*/
|
|
90
|
+
private readonly inHand;
|
|
91
|
+
/** What each harness with an adapter that lists its sessions said of them, see `checkPresence` */
|
|
92
|
+
private readonly presence;
|
|
93
|
+
private readonly listing;
|
|
94
|
+
private presenceCheckedAt;
|
|
95
|
+
/** Set once `close` was called: nothing new is delivered, what runs is waited for */
|
|
96
|
+
private closing;
|
|
97
|
+
private closed;
|
|
98
|
+
constructor(options: BridgeDispatcherOptions);
|
|
99
|
+
/**
|
|
100
|
+
* Puts the calling session into the address book, or finds it there. A session is known by
|
|
101
|
+
* its harness and the harness's own id, so calling again from the same session returns the
|
|
102
|
+
* same agent.
|
|
103
|
+
*/
|
|
104
|
+
register(identity: CallerIdentity): Promise<AgentRecord>;
|
|
105
|
+
/**
|
|
106
|
+
* Hands a job to another agent and returns at once with its number. The one it is for is
|
|
107
|
+
* found by address, and started when no session fits and the harness's adapter can start one.
|
|
108
|
+
*
|
|
109
|
+
* @throws BridgeError `not_registered`, `unknown_agent`, `ambiguous_agent`, `no_session`,
|
|
110
|
+
* `depth_exceeded`, `invalid_input`
|
|
111
|
+
*/
|
|
112
|
+
createJob(caller: string, input: CreateJobInput): Promise<CreateJobResult>;
|
|
113
|
+
/**
|
|
114
|
+
* Sends a letter on a job to its other party: a question, an answer, a report that ends the
|
|
115
|
+
* job, a note. The call returns once the bridge has the letter, the answer comes as a letter.
|
|
116
|
+
*
|
|
117
|
+
* @throws BridgeError `not_registered`, `unknown_job`, `not_a_party`, `job_finished`,
|
|
118
|
+
* `invalid_letter`, `loop_detected` (the same letter twice in a row), `budget_exhausted` (the
|
|
119
|
+
* job ran out of letters or time, and has failed)
|
|
120
|
+
*/
|
|
121
|
+
send(caller: string, input: SendLetterInput): Promise<SendLetterResult>;
|
|
122
|
+
/**
|
|
123
|
+
* The letters waiting for the caller, oldest first, each with the text to show. Taking them
|
|
124
|
+
* marks them read, and taking a job's first letter starts the job. One call returns `limit`
|
|
125
|
+
* letters at most (20 by default) and no more text than fits one answer: only the letters
|
|
126
|
+
* returned are marked, the rest wait for the next call. A session that may have missed an
|
|
127
|
+
* answer asks again with `unhandled`, which adds the newest of the letters it took before and
|
|
128
|
+
* has not answered or put away, in the room the new letters leave: what was not seen yet
|
|
129
|
+
* always comes first. `afterSeq` looks further than one call returns.
|
|
130
|
+
*/
|
|
131
|
+
inbox(caller: string, input?: InboxInput): Promise<RenderedLetter[]>;
|
|
132
|
+
/**
|
|
133
|
+
* Waits for the caller's mail: answers at once when letters are queued for it, otherwise as
|
|
134
|
+
* soon as one arrives, or with none when `timeoutMs` passes. This is how a session nothing can
|
|
135
|
+
* be pushed into still wakes: a process of its harness waits here and hands what it gets to
|
|
136
|
+
* the session, as `cogitator bridge wait` does in a hook of Claude Code. The letters stay
|
|
137
|
+
* queued until the waiter says it handed them over (`delivered`), so a waiter that dies with
|
|
138
|
+
* them in hand loses nothing. A session has one waiter: a new one ends the one before it.
|
|
139
|
+
* Aborting `signal` ends the wait, as the socket server does when the connection closes.
|
|
140
|
+
*/
|
|
141
|
+
wait(caller: string, input?: WaitInput, signal?: AbortSignal): Promise<WaitResult>;
|
|
142
|
+
/**
|
|
143
|
+
* Says the caller's waiter handed these letters to its session. A job handed over this way
|
|
144
|
+
* counts as taken, as one an adapter pushed does. A letter that is not queued any more, such
|
|
145
|
+
* as one the session read from its inbox meanwhile, stays as it is.
|
|
146
|
+
*
|
|
147
|
+
* @throws BridgeError `unknown_letter` for a letter that is not addressed to the caller
|
|
148
|
+
*/
|
|
149
|
+
delivered(caller: string, letterIds: readonly string[]): Promise<Letter[]>;
|
|
150
|
+
private arm;
|
|
151
|
+
/**
|
|
152
|
+
* The letters queued for an agent that nobody holds, oldest first, as many as one answer
|
|
153
|
+
* carries.
|
|
154
|
+
*/
|
|
155
|
+
private queuedFor;
|
|
156
|
+
private held;
|
|
157
|
+
/**
|
|
158
|
+
* Notes that a waiter was given these letters. They are in its hand until it confirms them,
|
|
159
|
+
* its connection closes, a newer waiter of the session takes over, or `deliveryTimeoutMs`
|
|
160
|
+
* passes, whichever comes first.
|
|
161
|
+
*/
|
|
162
|
+
private hand;
|
|
163
|
+
/** Frees what the waiters of an agent hold: a newer waiter is about to take their place. */
|
|
164
|
+
private letGo;
|
|
165
|
+
/**
|
|
166
|
+
* Says the caller is done with a letter that needs no answer. A question, a permission request
|
|
167
|
+
* and a job's first letter are not dismissed this way: they are marked read, and are done with
|
|
168
|
+
* once answered or reported on.
|
|
169
|
+
*
|
|
170
|
+
* @throws BridgeError `unknown_letter` for a letter that is not addressed to the caller
|
|
171
|
+
*/
|
|
172
|
+
acknowledge(caller: string, letterId: string): Promise<Letter>;
|
|
173
|
+
/**
|
|
174
|
+
* Cancels a job. Only the agent that handed it over can, the one carrying it out reports it
|
|
175
|
+
* as failed instead.
|
|
176
|
+
*
|
|
177
|
+
* @throws BridgeError `not_a_party`, `job_finished`
|
|
178
|
+
*/
|
|
179
|
+
cancel(caller: string, number: number, reason?: string): Promise<BridgeJob>;
|
|
180
|
+
/**
|
|
181
|
+
* Records whether the checks of a job confirmed its result. It is a call of the process that
|
|
182
|
+
* hosts the dispatcher and is not served on the socket: the agent that carries a job out
|
|
183
|
+
* must not be the one to say its result holds.
|
|
184
|
+
*/
|
|
185
|
+
recordVerification(number: number, verification: Pick<JobVerification, 'verified' | 'summary'>): Promise<BridgeJob>;
|
|
186
|
+
/** @throws BridgeError `unknown_job` */
|
|
187
|
+
job(number: number): Promise<BridgeJob>;
|
|
188
|
+
/** Jobs, newest first, 200 at most unless `limit` says otherwise */
|
|
189
|
+
jobs(filter?: JobTaskFilter): Promise<BridgeJob[]>;
|
|
190
|
+
agents(filter?: AgentFilter): Promise<AgentRecord[]>;
|
|
191
|
+
/**
|
|
192
|
+
* Letters, oldest first, 200 at most unless `limit` says otherwise and no more text than fits
|
|
193
|
+
* one answer. Go on from the last one with `afterSeq`
|
|
194
|
+
*/
|
|
195
|
+
letters(filter?: LetterFilter): Promise<Letter[]>;
|
|
196
|
+
/**
|
|
197
|
+
* Entries of the journal, oldest first: the first 200 unless `limit` says otherwise, or the
|
|
198
|
+
* last `tail`, and no more text than fits one answer. Go on from the last one with `afterSeq`
|
|
199
|
+
*/
|
|
200
|
+
readJournal(query?: JournalQuery): Promise<JournalEntry[]>;
|
|
201
|
+
status(): Promise<DispatcherStatus>;
|
|
202
|
+
/**
|
|
203
|
+
* What time does to jobs: fails those that ran past their budget, reminds the recipient of a
|
|
204
|
+
* letter that got no reaction and fails the job when the reminder got none either, and tries
|
|
205
|
+
* the deliveries that are due again. The host calls it on a timer.
|
|
206
|
+
*/
|
|
207
|
+
sweep(): Promise<SweepResult>;
|
|
208
|
+
/**
|
|
209
|
+
* Asks the adapters that can list their sessions which of them still run, once in
|
|
210
|
+
* `limits.presenceCheckMs`. A session that was listed and then is not, two times in a row, is
|
|
211
|
+
* gone, and its jobs fail with that reason rather than after the reply timeout: one miss may
|
|
212
|
+
* be a session between two states of its harness. A session with a waiter armed is there
|
|
213
|
+
* whatever the list says. One that was never listed (its harness shows it nowhere) is left to
|
|
214
|
+
* the idle rule, unless the adapter runs every session of its harness itself (`ownsSessions`):
|
|
215
|
+
* then what it does not list is gone at once. A session heard from after the list was asked
|
|
216
|
+
* for is not judged by it: it may have started meanwhile.
|
|
217
|
+
*/
|
|
218
|
+
private checkPresence;
|
|
219
|
+
private applyPresence;
|
|
220
|
+
/**
|
|
221
|
+
* Takes sessions that are not there out of the address book, so a harness word keeps reaching
|
|
222
|
+
* the session that is: one whose start never finished (the dispatcher that started it is
|
|
223
|
+
* gone) fails its jobs, one that has been silent for `limits.agentIdleMs` and has no job in
|
|
224
|
+
* progress just leaves. Returns the jobs it failed.
|
|
225
|
+
*/
|
|
226
|
+
private retireAgents;
|
|
227
|
+
/** Marks an agent gone and fails every job it was to carry out. Returns those jobs. */
|
|
228
|
+
private agentGone;
|
|
229
|
+
/** Resolves once every call made so far is handled and every delivery it started has ended. */
|
|
230
|
+
idle(): Promise<void>;
|
|
231
|
+
/**
|
|
232
|
+
* Ends the waits, waits for the calls being handled, the deliveries and the session starts they
|
|
233
|
+
* began, closes the adapters, then takes no more. A session a call asks for after this began
|
|
234
|
+
* 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
|
|
235
|
+
* session start after `limits.sessionStartTimeoutMs`, so this always ends.
|
|
236
|
+
*/
|
|
237
|
+
close(): Promise<void>;
|
|
238
|
+
private iso;
|
|
239
|
+
private checkBody;
|
|
240
|
+
private enqueue;
|
|
241
|
+
private mutate;
|
|
242
|
+
/**
|
|
243
|
+
* A call made as an agent, as one entry of the queue: first the sign of life is stored, on its
|
|
244
|
+
* own, so a call the bridge then refuses still counts, then the call itself.
|
|
245
|
+
*/
|
|
246
|
+
private asCaller;
|
|
247
|
+
private transact;
|
|
248
|
+
private journal;
|
|
249
|
+
/**
|
|
250
|
+
* Notes that a session called: the time, and that a session the bridge took for gone is
|
|
251
|
+
* there again.
|
|
252
|
+
*/
|
|
253
|
+
private heardFrom;
|
|
254
|
+
private requireCaller;
|
|
255
|
+
private requireJob;
|
|
256
|
+
private jobFrom;
|
|
257
|
+
private patchRecord;
|
|
258
|
+
private transition;
|
|
259
|
+
private pickUp;
|
|
260
|
+
private failJob;
|
|
261
|
+
private cancelJob;
|
|
262
|
+
/** A job that is over takes the jobs handed over from inside it with it: nobody waits for them. */
|
|
263
|
+
private cancelChildren;
|
|
264
|
+
/**
|
|
265
|
+
* Why an agent may not hand this job over right now, if it may not: it is the job it already
|
|
266
|
+
* handed to the same agent and that is still in progress, it has too many jobs out, it hands
|
|
267
|
+
* jobs over too fast, or too many sessions are being started.
|
|
268
|
+
*/
|
|
269
|
+
private handOverRefusal;
|
|
270
|
+
/**
|
|
271
|
+
* A finished job waits for nothing: its own letter and the questions nobody answered are done
|
|
272
|
+
* with, and no letter of it is due any more.
|
|
273
|
+
*/
|
|
274
|
+
private closeLetters;
|
|
275
|
+
private accept;
|
|
276
|
+
/** A letter the bridge writes itself. It costs the job nothing and is never held back as a repeat. */
|
|
277
|
+
private systemNote;
|
|
278
|
+
private answeredLetter;
|
|
279
|
+
private markRead;
|
|
280
|
+
private markHandled;
|
|
281
|
+
/**
|
|
282
|
+
* Records that an adapter handed a letter to its session. A job handed over this way counts as
|
|
283
|
+
* taken: the harness has it and woke the session, which may work without calling the bridge
|
|
284
|
+
* until it reports. The letter is read only once the session calls about the job.
|
|
285
|
+
*/
|
|
286
|
+
private markDelivered;
|
|
287
|
+
private track;
|
|
288
|
+
/** Delivers what waits for an agent, one letter after another, in the order they were accepted. */
|
|
289
|
+
private wake;
|
|
290
|
+
private deliverTo;
|
|
291
|
+
private deliveryFailed;
|
|
292
|
+
private startSession;
|
|
293
|
+
private wakeIfKnown;
|
|
294
|
+
private runSessionStart;
|
|
295
|
+
}
|
|
296
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +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,EAcL,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,gBAAgB,EAErB,KAAK,SAAS,EACd,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AAerB,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACb,YAAY,EACZ,YAAY,EACb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAQL,KAAK,WAAW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