@cogitator-ai/bridge 0.7.0 → 0.8.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 +65 -20
- package/dist/board.d.ts +76 -0
- package/dist/board.d.ts.map +1 -0
- package/dist/board.js +19 -0
- package/dist/board.js.map +1 -0
- package/dist/client.d.ts +11 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +15 -0
- package/dist/client.js.map +1 -1
- package/dist/dispatcher.d.ts +70 -1
- package/dist/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher.js +361 -8
- 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/handover.d.ts +72 -0
- package/dist/handover.d.ts.map +1 -0
- package/dist/handover.js +225 -0
- package/dist/handover.js.map +1 -0
- package/dist/host.d.ts +7 -0
- 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 +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/inputs.d.ts +77 -0
- package/dist/inputs.d.ts.map +1 -1
- package/dist/inputs.js +41 -0
- package/dist/inputs.js.map +1 -1
- package/dist/job-record.d.ts +1 -0
- package/dist/job-record.d.ts.map +1 -1
- package/dist/job-record.js +1 -0
- package/dist/job-record.js.map +1 -1
- package/dist/memory/index.d.ts +35 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +192 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/protocol.d.ts +3 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +3 -0
- package/dist/server.js.map +1 -1
- package/dist/tools/guidance.d.ts.map +1 -1
- package/dist/tools/guidance.js +2 -0
- package/dist/tools/guidance.js.map +1 -1
- package/dist/tools/tools.d.ts.map +1 -1
- package/dist/tools/tools.js +126 -0
- package/dist/tools/tools.js.map +1 -1
- package/dist/types.d.ts +16 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -1
- package/package.json +11 -1
package/dist/dispatcher.js
CHANGED
|
@@ -2,10 +2,12 @@ import { EventEmitter } from 'node:events';
|
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { isAbsolute, resolve as resolvePath } from 'node:path';
|
|
4
4
|
import { TaskManager, textPart, toMessage } from '@cogitator-ai/a2a';
|
|
5
|
+
import { MAX_RECALL_LIMIT } from './board.js';
|
|
5
6
|
import { runCheck, skippedCheck, summarizeChecks } from './checks.js';
|
|
6
7
|
import { BridgeError } from './errors.js';
|
|
7
8
|
import { deliveryBackoff, exhaustedBudget, formatDuration, nextDepth, resolveBudget, resolveLimits, } from './guards.js';
|
|
8
|
-
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, RejectInputSchema, 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, PersonAnswerInputSchema, SendLetterInputSchema, StuckInputSchema, VerdictInputSchema, VerificationInputSchema, AcceptInputSchema, DiffInputSchema, ForgetInputSchema, RecallInputSchema, RejectInputSchema, RememberInputSchema, WaitInputSchema, WithdrawInputSchema, } from './inputs.js';
|
|
10
|
+
import { HANDOVER_GAP, buildHandover } from './handover.js';
|
|
9
11
|
import { isFinishedJobState, jobOf, jobRecordOf } from './job-record.js';
|
|
10
12
|
import { expectationOf, forHistory, newLetterId, normalizeBody, oneLine, quoted, renderLetter, repeats, titleOf, } from './letters.js';
|
|
11
13
|
import { canonicalPath, findCheckoutRoot, findProjectRoot, inCheckout, isInside } from './paths.js';
|
|
@@ -35,6 +37,57 @@ function summaryOf(changes, at) {
|
|
|
35
37
|
at,
|
|
36
38
|
};
|
|
37
39
|
}
|
|
40
|
+
/** The folder a job is done in: the caller's, or the one it names from there. */
|
|
41
|
+
function jobFolder(from, named) {
|
|
42
|
+
if (!named)
|
|
43
|
+
return from.cwd;
|
|
44
|
+
return canonicalPath(isAbsolute(named) ? named : resolvePath(from.cwd, named));
|
|
45
|
+
}
|
|
46
|
+
/** How much of a job's text the board is asked about. */
|
|
47
|
+
const ABOUT_CHARS = 4_000;
|
|
48
|
+
/** How many notes of earlier jobs the handover of a job looks at. */
|
|
49
|
+
const EARLIER_NOTES = 6;
|
|
50
|
+
/**
|
|
51
|
+
* What earlier jobs like this one left on the board: the lessons and outcomes that fit its
|
|
52
|
+
* text, and the other lessons of the jobs those belong to, since a job is found by what it was
|
|
53
|
+
* about and its lessons are worded as they are. Lessons come before outcomes.
|
|
54
|
+
*/
|
|
55
|
+
async function earlierLike(board, project, about) {
|
|
56
|
+
const fitting = await board.recall({
|
|
57
|
+
project,
|
|
58
|
+
text: about,
|
|
59
|
+
kinds: ['lesson', 'outcome'],
|
|
60
|
+
limit: EARLIER_NOTES,
|
|
61
|
+
});
|
|
62
|
+
const jobs = new Set(fitting.flatMap((note) => note.source.job ?? []));
|
|
63
|
+
const lessons = jobs.size === 0
|
|
64
|
+
? []
|
|
65
|
+
: (await board.recall({ project, kinds: ['lesson'], limit: MAX_RECALL_LIMIT })).filter((note) => note.source.job !== undefined && jobs.has(note.source.job));
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
return [...lessons, ...fitting]
|
|
68
|
+
.filter((note) => !seen.has(note.id) && seen.add(note.id))
|
|
69
|
+
.sort((a, b) => Number(a.kind === 'outcome') - Number(b.kind === 'outcome'))
|
|
70
|
+
.slice(0, EARLIER_NOTES);
|
|
71
|
+
}
|
|
72
|
+
/** How much of a report the note of a job's outcome repeats. */
|
|
73
|
+
const REPORT_NOTED = 300;
|
|
74
|
+
/** How a job ended, in one line for the board. */
|
|
75
|
+
function outcomeText(job, reported) {
|
|
76
|
+
const checked = job.verification
|
|
77
|
+
? job.verification.verified
|
|
78
|
+
? ', and its checks confirmed the result'
|
|
79
|
+
: ', but its checks did not confirm the result'
|
|
80
|
+
: '';
|
|
81
|
+
const how = job.state === 'completed'
|
|
82
|
+
? `completed${checked}`
|
|
83
|
+
: `${job.state}${job.reason ? ` (${oneLine(job.reason)})` : ''}`;
|
|
84
|
+
const quote = (text) => oneLine(text).replaceAll('"', "'");
|
|
85
|
+
const said = reported ? quote(reported) : '';
|
|
86
|
+
const report = said
|
|
87
|
+
? ` Its report began: "${said.length > REPORT_NOTED ? `${said.slice(0, REPORT_NOTED)}...` : said}"`
|
|
88
|
+
: '';
|
|
89
|
+
return `Job #${job.number} "${quote(job.title)}", done by ${job.to} for ${job.from}: ${how}.${report}`;
|
|
90
|
+
}
|
|
38
91
|
/** What git said when it refused, as an error an agent can act on. */
|
|
39
92
|
function gitRefusal(error, what) {
|
|
40
93
|
if (error instanceof BridgeError)
|
|
@@ -145,6 +198,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
145
198
|
assertWritable;
|
|
146
199
|
rights;
|
|
147
200
|
person;
|
|
201
|
+
board;
|
|
148
202
|
/** When each agent's other parties were last told it waits at its window, see `stuck` */
|
|
149
203
|
stuckTold = new Map();
|
|
150
204
|
/** Those waiting for the verdict of a permission request, by its id, see `verdict` */
|
|
@@ -196,6 +250,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
196
250
|
this.assertWritable = options.assertWritable;
|
|
197
251
|
this.rights = options.rights;
|
|
198
252
|
this.person = options.person;
|
|
253
|
+
this.board = options.board;
|
|
199
254
|
for (const adapter of options.adapters ?? []) {
|
|
200
255
|
const harness = parse(HarnessNameSchema, adapter.harness, 'Cannot use the adapter');
|
|
201
256
|
if (this.adapters.has(harness)) {
|
|
@@ -300,12 +355,17 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
300
355
|
async createJob(caller, input) {
|
|
301
356
|
const request = parse(CreateJobInputSchema, input, 'Cannot create the job');
|
|
302
357
|
this.checkBody(request.body);
|
|
358
|
+
const handover = await this.handoverFor(caller, request);
|
|
359
|
+
const withContext = handover ? `${request.body}${HANDOVER_GAP}${handover}` : request.body;
|
|
360
|
+
const body = withContext.length <= this.limits.maxBodyLength ? withContext : request.body;
|
|
303
361
|
const outcome = await this.asCaller(caller, async (tx) => {
|
|
304
362
|
const from = this.requireCaller(caller);
|
|
305
363
|
if (request.letterId) {
|
|
306
364
|
const sent = this.store.getLetter(request.letterId);
|
|
307
365
|
if (sent) {
|
|
308
|
-
if (sent.kind !== 'task' ||
|
|
366
|
+
if (sent.kind !== 'task' ||
|
|
367
|
+
sent.from !== from.name ||
|
|
368
|
+
this.taskText(sent) !== request.body) {
|
|
309
369
|
throw new BridgeError('invalid_letter', `The letter id ${request.letterId} belongs to another letter. Give a new job a new id`, { letter: request.letterId });
|
|
310
370
|
}
|
|
311
371
|
return { job: this.requireJob(sent.job).job, letter: sent, duplicate: true };
|
|
@@ -350,9 +410,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
350
410
|
return new Refusal(error);
|
|
351
411
|
}
|
|
352
412
|
const budget = resolveBudget(request.budget, this.limits);
|
|
353
|
-
const cwd = request.cwd
|
|
354
|
-
? canonicalPath(isAbsolute(request.cwd) ? request.cwd : resolvePath(from.cwd, request.cwd))
|
|
355
|
-
: from.cwd;
|
|
413
|
+
const cwd = jobFolder(from, request.cwd);
|
|
356
414
|
if (!isInside(from.project, cwd) && !isInside(findCheckoutRoot(from.cwd), cwd)) {
|
|
357
415
|
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 });
|
|
358
416
|
}
|
|
@@ -401,6 +459,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
401
459
|
to: to.name,
|
|
402
460
|
project: from.project,
|
|
403
461
|
cwd,
|
|
462
|
+
...(body !== request.body && { taskLength: request.body.length }),
|
|
404
463
|
...(request.worktree && { isolation: 'worktree' }),
|
|
405
464
|
checks: request.checks ?? [],
|
|
406
465
|
budget,
|
|
@@ -411,7 +470,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
411
470
|
deadlineAt: this.iso(Math.min(now + budget.durationMs, parent ? Date.parse(parent.deadlineAt) : Infinity)),
|
|
412
471
|
};
|
|
413
472
|
const firstLetterId = request.letterId ?? this.letterId();
|
|
414
|
-
const history = userMessage(forHistory(
|
|
473
|
+
const history = userMessage(forHistory(body, firstLetterId));
|
|
415
474
|
const task = await this.tasks.createTask(history, undefined, {
|
|
416
475
|
agentName: to.name,
|
|
417
476
|
metadata: { [BRIDGE_JOB_KEY]: record },
|
|
@@ -428,7 +487,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
428
487
|
kind: 'task',
|
|
429
488
|
from: from.name,
|
|
430
489
|
to: to.name,
|
|
431
|
-
body
|
|
490
|
+
body,
|
|
432
491
|
replyDueAt: this.iso(now + budget.replyTimeoutMs),
|
|
433
492
|
});
|
|
434
493
|
if (resolution.kind === 'spawn') {
|
|
@@ -607,6 +666,19 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
607
666
|
this.closeLetters(tx, job.number, letter.id);
|
|
608
667
|
await this.stopWaiting(job.number, now, false);
|
|
609
668
|
await this.cancelChildren(tx, job);
|
|
669
|
+
if (this.board) {
|
|
670
|
+
for (const lesson of request.lessons ?? []) {
|
|
671
|
+
tx.notes.push({
|
|
672
|
+
kind: 'lesson',
|
|
673
|
+
text: lesson,
|
|
674
|
+
project: job.project,
|
|
675
|
+
source: { by: from.name, job: job.number, at: this.iso(now) },
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
if (!(state === 'completed' && job.checks.length > 0)) {
|
|
679
|
+
this.noteOutcome(tx, job.number, request.body);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
610
682
|
}
|
|
611
683
|
else if (isAssignee && NEEDS_ANSWER.includes(request.kind)) {
|
|
612
684
|
await this.transition(tx, job.number, 'input-required', forHistory(request.body, letter.id));
|
|
@@ -1441,6 +1513,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1441
1513
|
});
|
|
1442
1514
|
this.journal(tx, { type: 'job.verified', job: number, data: { verified, summary } });
|
|
1443
1515
|
const report = this.store.getLetter(fresh.checking.letter);
|
|
1516
|
+
this.noteOutcome(tx, number, report?.body);
|
|
1444
1517
|
if (report?.state === 'held') {
|
|
1445
1518
|
this.store.updateLetter(report.id, { state: 'queued' });
|
|
1446
1519
|
tx.wake.add(report.to);
|
|
@@ -1761,6 +1834,282 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
1761
1834
|
}
|
|
1762
1835
|
});
|
|
1763
1836
|
}
|
|
1837
|
+
/** The board of this bridge, or the refusal of a bridge that was given none. */
|
|
1838
|
+
requireBoard() {
|
|
1839
|
+
if (this.board)
|
|
1840
|
+
return this.board;
|
|
1841
|
+
throw new BridgeError('no_board', 'This bridge keeps no notes: whoever runs it gave it no board to keep them on');
|
|
1842
|
+
}
|
|
1843
|
+
/** The project a call about the board is for: the caller's own, or the one the person names. */
|
|
1844
|
+
boardProject(agent, named) {
|
|
1845
|
+
if (agent)
|
|
1846
|
+
return agent.project;
|
|
1847
|
+
if (named)
|
|
1848
|
+
return canonicalPath(findProjectRoot(named));
|
|
1849
|
+
throw new BridgeError('invalid_input', 'Say which project the note is for');
|
|
1850
|
+
}
|
|
1851
|
+
/** The jobs whose own notes a caller may see: an agent's are those it is a party of. */
|
|
1852
|
+
jobsSeenBy(agent, project) {
|
|
1853
|
+
return this.store
|
|
1854
|
+
.jobTasks(agent ? { party: agent.name } : { project })
|
|
1855
|
+
.flatMap((task) => jobOf(task) ?? [])
|
|
1856
|
+
.filter((job) => job.project === project)
|
|
1857
|
+
.map((job) => job.number);
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Puts a note on the board of a project, for the agents that work there to find: a fact, or
|
|
1861
|
+
* what a job taught. The note carries who said it, on which job and when. `caller` is the
|
|
1862
|
+
* agent that says it, or `null` for the person, who names the project. A note kept to a job
|
|
1863
|
+
* (`jobOnly`) is seen by the two agents of that job only.
|
|
1864
|
+
*
|
|
1865
|
+
* @throws BridgeError `no_board`, `not_registered`, `unknown_job`, `not_a_party` (the job named
|
|
1866
|
+
* is not the caller's), `invalid_input`
|
|
1867
|
+
*/
|
|
1868
|
+
async remember(caller, input) {
|
|
1869
|
+
const request = parse(RememberInputSchema, input, 'Cannot remember that');
|
|
1870
|
+
const board = this.requireBoard();
|
|
1871
|
+
const fact = await this.enqueue(async () => {
|
|
1872
|
+
const agent = caller === null ? undefined : this.requireCaller(caller);
|
|
1873
|
+
let project = this.boardProject(agent, request.project);
|
|
1874
|
+
let job = request.job;
|
|
1875
|
+
if (job !== undefined) {
|
|
1876
|
+
const found = this.requireJob(job).job;
|
|
1877
|
+
const party = agent ? agent.name === found.from || agent.name === found.to : undefined;
|
|
1878
|
+
if (party)
|
|
1879
|
+
project = found.project;
|
|
1880
|
+
if (found.project !== project || party === false) {
|
|
1881
|
+
throw new BridgeError('not_a_party', `Job ${found.number} is between ${found.from} and ${found.to}, so a note cannot be said on it from here`, { job: found.number });
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
else if (agent) {
|
|
1885
|
+
const carried = this.store
|
|
1886
|
+
.jobTasks({ to: agent.name, states: ACTIVE_JOB_STATES })
|
|
1887
|
+
.flatMap((task) => jobOf(task) ?? []);
|
|
1888
|
+
if (carried.length === 1)
|
|
1889
|
+
job = carried[0].number;
|
|
1890
|
+
}
|
|
1891
|
+
return {
|
|
1892
|
+
kind: request.kind ?? 'fact',
|
|
1893
|
+
text: request.text,
|
|
1894
|
+
project,
|
|
1895
|
+
...(request.jobOnly && job !== undefined && { job }),
|
|
1896
|
+
source: {
|
|
1897
|
+
by: agent?.name ?? PERSON,
|
|
1898
|
+
...(job !== undefined && { job }),
|
|
1899
|
+
at: this.iso(),
|
|
1900
|
+
},
|
|
1901
|
+
};
|
|
1902
|
+
});
|
|
1903
|
+
const kept = await board.remember(fact);
|
|
1904
|
+
await this.journalNote(kept, fact.source.by);
|
|
1905
|
+
return kept;
|
|
1906
|
+
}
|
|
1907
|
+
/**
|
|
1908
|
+
* Writes down that a note went on the board, and who said it this time. Everybody can read
|
|
1909
|
+
* the journal, so the text of a note kept to one job is left out of it.
|
|
1910
|
+
*/
|
|
1911
|
+
async journalNote(kept, by) {
|
|
1912
|
+
await this.mutate(async (tx) => {
|
|
1913
|
+
this.journal(tx, {
|
|
1914
|
+
type: 'fact.remembered',
|
|
1915
|
+
agent: by,
|
|
1916
|
+
...(kept.source.job !== undefined && { job: kept.source.job }),
|
|
1917
|
+
data: {
|
|
1918
|
+
id: kept.id,
|
|
1919
|
+
kind: kept.kind,
|
|
1920
|
+
project: kept.project,
|
|
1921
|
+
...(kept.job === undefined && { text: kept.text }),
|
|
1922
|
+
...(kept.source.by !== by && { saidBefore: kept.source.by }),
|
|
1923
|
+
},
|
|
1924
|
+
});
|
|
1925
|
+
});
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Looks at the board of a project: the notes that fit `text` best, or the newest. An agent
|
|
1929
|
+
* sees the notes of its project and those kept to the jobs it is a party of, the person
|
|
1930
|
+
* (`caller` is `null`) every note of the project they name.
|
|
1931
|
+
*
|
|
1932
|
+
* @throws BridgeError `no_board`, `not_registered`, `invalid_input`
|
|
1933
|
+
*/
|
|
1934
|
+
async recall(caller, input = {}) {
|
|
1935
|
+
const request = parse(RecallInputSchema, input, 'Cannot look at the board');
|
|
1936
|
+
const board = this.requireBoard();
|
|
1937
|
+
const query = await this.enqueue(async () => {
|
|
1938
|
+
const agent = caller === null ? undefined : this.requireCaller(caller);
|
|
1939
|
+
const project = this.boardProject(agent, request.project);
|
|
1940
|
+
return { project, jobs: this.jobsSeenBy(agent, project) };
|
|
1941
|
+
});
|
|
1942
|
+
return board.recall({
|
|
1943
|
+
...query,
|
|
1944
|
+
...(request.text && { text: request.text }),
|
|
1945
|
+
...(request.kinds && { kinds: request.kinds }),
|
|
1946
|
+
...(request.limit !== undefined && { limit: request.limit }),
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
/**
|
|
1950
|
+
* Lets a note of the board go: it is found no more, and is not deleted. An agent lets go what
|
|
1951
|
+
* it said itself, the person (`caller` is `null`) any note of the project they name.
|
|
1952
|
+
*
|
|
1953
|
+
* @throws BridgeError `no_board`, `not_registered`, `invalid_input` (no such note there),
|
|
1954
|
+
* `not_a_party` (the note is another's)
|
|
1955
|
+
*/
|
|
1956
|
+
async forget(caller, input) {
|
|
1957
|
+
const request = parse(ForgetInputSchema, input, 'Cannot forget that');
|
|
1958
|
+
const board = this.requireBoard();
|
|
1959
|
+
const { agent, project, jobs } = await this.enqueue(async () => {
|
|
1960
|
+
const found = caller === null ? undefined : this.requireCaller(caller);
|
|
1961
|
+
const where = this.boardProject(found, request.project);
|
|
1962
|
+
return { agent: found, project: where, jobs: this.jobsSeenBy(found, where) };
|
|
1963
|
+
});
|
|
1964
|
+
const fact = await board.find(request.id);
|
|
1965
|
+
const seen = fact?.project === project && (fact.job === undefined || jobs.includes(fact.job));
|
|
1966
|
+
if (!fact || !seen) {
|
|
1967
|
+
throw new BridgeError('invalid_input', `There is no note ${request.id} on the board of ${project}`, {
|
|
1968
|
+
id: request.id,
|
|
1969
|
+
});
|
|
1970
|
+
}
|
|
1971
|
+
if (agent && fact.source.by !== agent.name) {
|
|
1972
|
+
throw new BridgeError('not_a_party', `The note ${fact.id} is what ${fact.source.by === PERSON ? 'the person' : fact.source.by} said: only they, or the person, let it go`, { id: fact.id });
|
|
1973
|
+
}
|
|
1974
|
+
await board.forget(fact.id);
|
|
1975
|
+
await this.mutate(async (tx) => {
|
|
1976
|
+
this.journal(tx, {
|
|
1977
|
+
type: 'fact.forgotten',
|
|
1978
|
+
agent: agent?.name ?? PERSON,
|
|
1979
|
+
data: { id: fact.id, kind: fact.kind, project: fact.project, by: fact.source.by },
|
|
1980
|
+
});
|
|
1981
|
+
});
|
|
1982
|
+
return fact;
|
|
1983
|
+
}
|
|
1984
|
+
/** The text of a job as its caller wrote it: its first letter without what the bridge added. */
|
|
1985
|
+
taskText(first, job) {
|
|
1986
|
+
const length = (job ?? this.jobIfAny(first.job))?.taskLength;
|
|
1987
|
+
return length === undefined ? first.body : first.body.slice(0, length);
|
|
1988
|
+
}
|
|
1989
|
+
/**
|
|
1990
|
+
* Whether the rules of an agent let it read a file without asking: its own, and those of every
|
|
1991
|
+
* agent up the chain of the jobs it carries out. True on a bridge that was given no judge:
|
|
1992
|
+
* what an agent names in its own project it could read itself.
|
|
1993
|
+
*/
|
|
1994
|
+
async mayRead(from, cwd, path) {
|
|
1995
|
+
if (!this.rights)
|
|
1996
|
+
return true;
|
|
1997
|
+
const ask = { tool: 'Read', access: 'read', paths: [path], cwd };
|
|
1998
|
+
try {
|
|
1999
|
+
const now = this.iso();
|
|
2000
|
+
const own = await this.rights({
|
|
2001
|
+
job: {
|
|
2002
|
+
number: 0,
|
|
2003
|
+
taskId: '',
|
|
2004
|
+
title: '',
|
|
2005
|
+
from: from.name,
|
|
2006
|
+
to: from.name,
|
|
2007
|
+
project: from.project,
|
|
2008
|
+
cwd,
|
|
2009
|
+
checks: [],
|
|
2010
|
+
budget: this.limits.budget,
|
|
2011
|
+
lettersUsed: 0,
|
|
2012
|
+
depth: 1,
|
|
2013
|
+
createdAt: now,
|
|
2014
|
+
deadlineAt: now,
|
|
2015
|
+
state: 'submitted',
|
|
2016
|
+
updatedAt: now,
|
|
2017
|
+
},
|
|
2018
|
+
delegator: from,
|
|
2019
|
+
ask,
|
|
2020
|
+
});
|
|
2021
|
+
if (own !== 'allow')
|
|
2022
|
+
return false;
|
|
2023
|
+
const carried = await this.enqueue(async () => this.store
|
|
2024
|
+
.jobTasks({ to: from.name, states: ACTIVE_JOB_STATES })
|
|
2025
|
+
.flatMap((task) => jobOf(task) ?? []));
|
|
2026
|
+
for (const job of carried) {
|
|
2027
|
+
if ((await this.rightsFor(job.number, ask)) !== 'allow')
|
|
2028
|
+
return false;
|
|
2029
|
+
}
|
|
2030
|
+
return true;
|
|
2031
|
+
}
|
|
2032
|
+
catch (error) {
|
|
2033
|
+
this.onError(asError(error));
|
|
2034
|
+
return false;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
/**
|
|
2038
|
+
* The block the bridge puts under the text of a job, see `buildHandover`: what the caller
|
|
2039
|
+
* named, the lines of the files it pointed at, and the notes of the board that fit the job.
|
|
2040
|
+
* Undefined when there is nothing to add. It never fails the hand-over and never holds it up
|
|
2041
|
+
* for long: what cannot be read or found in time is left out, and the block is kept small
|
|
2042
|
+
* enough for the letter to take it.
|
|
2043
|
+
*/
|
|
2044
|
+
async handoverFor(caller, request) {
|
|
2045
|
+
const from = await this.enqueue(async () => this.store.getAgent(caller));
|
|
2046
|
+
if (!from)
|
|
2047
|
+
return undefined;
|
|
2048
|
+
const room = Math.floor((this.limits.maxBodyLength - request.body.length - HANDOVER_GAP.length) / 4);
|
|
2049
|
+
const budget = Math.min(request.contextTokens ?? this.limits.contextTokens, room);
|
|
2050
|
+
if (budget <= 0)
|
|
2051
|
+
return undefined;
|
|
2052
|
+
const build = async () => {
|
|
2053
|
+
const about = `${request.title ?? ''}\n${request.body}`.slice(0, ABOUT_CHARS);
|
|
2054
|
+
const board = this.board;
|
|
2055
|
+
const [facts, earlier] = board
|
|
2056
|
+
? await Promise.all([
|
|
2057
|
+
board.recall({ project: from.project, text: about, kinds: ['fact'], limit: 8 }),
|
|
2058
|
+
earlierLike(board, from.project, about),
|
|
2059
|
+
])
|
|
2060
|
+
: [[], []];
|
|
2061
|
+
return buildHandover(request.context, {
|
|
2062
|
+
from: from.name,
|
|
2063
|
+
cwd: from.cwd,
|
|
2064
|
+
roots: [from.project, findCheckoutRoot(from.cwd)],
|
|
2065
|
+
top: findCheckoutRoot(from.cwd),
|
|
2066
|
+
mayRead: (path) => this.mayRead(from, from.cwd, path),
|
|
2067
|
+
facts,
|
|
2068
|
+
earlier,
|
|
2069
|
+
}, budget);
|
|
2070
|
+
};
|
|
2071
|
+
try {
|
|
2072
|
+
return await within(build(), this.limits.contextTimeoutMs, 'Putting the context of the job together');
|
|
2073
|
+
}
|
|
2074
|
+
catch (error) {
|
|
2075
|
+
this.onError(asError(error));
|
|
2076
|
+
return undefined;
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
/**
|
|
2080
|
+
* Notes how a job ended on the board of its project, in the bridge's own name: who did it for
|
|
2081
|
+
* whom, how it ended, what its checks showed and the start of what was reported. A later job
|
|
2082
|
+
* like it finds the note in its handover.
|
|
2083
|
+
*/
|
|
2084
|
+
noteOutcome(tx, number, reported) {
|
|
2085
|
+
if (!this.board)
|
|
2086
|
+
return;
|
|
2087
|
+
const { job } = this.requireJob(number);
|
|
2088
|
+
tx.notes.push({
|
|
2089
|
+
kind: 'outcome',
|
|
2090
|
+
text: outcomeText(job, reported),
|
|
2091
|
+
project: job.project,
|
|
2092
|
+
source: { by: BRIDGE_SENDER, job: job.number, at: this.iso() },
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
/** Puts on the board what a call left for it, after the call is stored. */
|
|
2096
|
+
keepNotes(notes) {
|
|
2097
|
+
const board = this.board;
|
|
2098
|
+
if (!board || notes.length === 0)
|
|
2099
|
+
return;
|
|
2100
|
+
this.track((async () => {
|
|
2101
|
+
for (const note of notes) {
|
|
2102
|
+
try {
|
|
2103
|
+
const kept = await board.remember(note);
|
|
2104
|
+
if (kept.kind !== 'outcome')
|
|
2105
|
+
await this.journalNote(kept, note.source.by);
|
|
2106
|
+
}
|
|
2107
|
+
catch (error) {
|
|
2108
|
+
this.onError(asError(error));
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
})());
|
|
2112
|
+
}
|
|
1764
2113
|
/** @throws BridgeError `unknown_job` */
|
|
1765
2114
|
async job(number) {
|
|
1766
2115
|
return this.enqueue(async () => this.requireJob(number).job);
|
|
@@ -2076,6 +2425,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2076
2425
|
decided: new Set(),
|
|
2077
2426
|
notices: [],
|
|
2078
2427
|
checks: [],
|
|
2428
|
+
notes: [],
|
|
2079
2429
|
};
|
|
2080
2430
|
const result = await this.store.transaction(() => {
|
|
2081
2431
|
this.assertWritable?.();
|
|
@@ -2100,6 +2450,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2100
2450
|
if (!this.closing)
|
|
2101
2451
|
for (const number of tx.checks)
|
|
2102
2452
|
this.startChecks(number);
|
|
2453
|
+
this.keepNotes(tx.notes);
|
|
2103
2454
|
this.tellPerson(tx);
|
|
2104
2455
|
return result;
|
|
2105
2456
|
}
|
|
@@ -2247,6 +2598,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2247
2598
|
await this.patchRecord(job.taskId, { reason });
|
|
2248
2599
|
this.closeLetters(tx, number);
|
|
2249
2600
|
await this.stopWaiting(number, this.now(), false);
|
|
2601
|
+
this.noteOutcome(tx, number);
|
|
2250
2602
|
const text = `Job #${number} has failed: ${reason}.`;
|
|
2251
2603
|
this.systemNote(tx, job, job.from, text);
|
|
2252
2604
|
this.systemNote(tx, job, job.to, `${text} Stop working on it.`);
|
|
@@ -2260,6 +2612,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2260
2612
|
await this.patchRecord(job.taskId, { reason });
|
|
2261
2613
|
this.closeLetters(tx, number);
|
|
2262
2614
|
await this.stopWaiting(number, this.now(), false);
|
|
2615
|
+
this.noteOutcome(tx, number);
|
|
2263
2616
|
this.systemNote(tx, job, job.to, note);
|
|
2264
2617
|
await this.cancelChildren(tx, job);
|
|
2265
2618
|
}
|
|
@@ -2292,7 +2645,7 @@ export class BridgeDispatcher extends EventEmitter {
|
|
|
2292
2645
|
if (job.cwd !== cwd || !sameAgent(job))
|
|
2293
2646
|
return false;
|
|
2294
2647
|
const first = this.store.listLetters({ job: job.number, kinds: ['task'], limit: 1 })[0];
|
|
2295
|
-
return first !== undefined && normalizeBody(first
|
|
2648
|
+
return first !== undefined && normalizeBody(this.taskText(first, job)) === wanted;
|
|
2296
2649
|
});
|
|
2297
2650
|
if (same) {
|
|
2298
2651
|
return new BridgeError('loop_detected', `You already handed this job to ${same.to} as job #${same.number}, and it is still ${same.state}. Wait for its report, or cancel it first`, { job: same.number });
|