tryharness 0.7.43 → 0.7.45
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/lib/boardProtocol.json +55 -2
- package/lib/bridge.mjs +2 -0
- package/lib/coordinator.mjs +8 -1
- package/lib/daemon.mjs +145 -8
- package/lib/goalCommands.mjs +6 -1
- package/lib/mcp-stdio.mjs +79 -0
- package/lib/network.mjs +33 -1
- package/lib/projectGit.mjs +328 -0
- package/lib/projectSettings.mjs +120 -0
- package/lib/projects.mjs +2052 -0
- package/lib/store.mjs +13 -1
- package/lib/swarm.mjs +12 -1
- package/lib/taskCommands.mjs +2 -0
- package/lib/threadRuns.mjs +53 -8
- package/lib/threadView.mjs +18 -2
- package/package.json +1 -1
package/lib/projects.mjs
ADDED
|
@@ -0,0 +1,2052 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { nameOf } from "./members.mjs";
|
|
5
|
+
import { secretReason } from "./secrets.mjs";
|
|
6
|
+
import {
|
|
7
|
+
SETTINGS,
|
|
8
|
+
SETTING_KEYS,
|
|
9
|
+
effectiveSettings,
|
|
10
|
+
initialSettings,
|
|
11
|
+
proposalState,
|
|
12
|
+
validSetting,
|
|
13
|
+
widens,
|
|
14
|
+
} from "./projectSettings.mjs";
|
|
15
|
+
import {
|
|
16
|
+
anchorOf,
|
|
17
|
+
behind,
|
|
18
|
+
branchSlug,
|
|
19
|
+
changedFiles,
|
|
20
|
+
cloneInto,
|
|
21
|
+
commitLane,
|
|
22
|
+
fetchCanon,
|
|
23
|
+
findCheckout,
|
|
24
|
+
holds,
|
|
25
|
+
laneFor,
|
|
26
|
+
openPullRequest,
|
|
27
|
+
pullRequestState,
|
|
28
|
+
pushLane,
|
|
29
|
+
removeLane,
|
|
30
|
+
validSha,
|
|
31
|
+
} from "./projectGit.mjs";
|
|
32
|
+
|
|
33
|
+
// A project two people's swarms share (owner, October 20). One room both swarms read and write, mirrored on each
|
|
34
|
+
// computer; a record both keep (who is building what, what the two people decided, notes, the settings they agreed);
|
|
35
|
+
// and a check before any agent starts work, so the two sides never build two systems that later have to be made one.
|
|
36
|
+
//
|
|
37
|
+
// Everything crosses sealed, on a thread between the two people (the relay's person thread): an event per post or
|
|
38
|
+
// record change, each carried until the other computer has it. Harness's server carries ciphertext and never learns
|
|
39
|
+
// what a project is. Every record entry belongs to the side that made it and only that side changes it, so the two
|
|
40
|
+
// copies never conflict: an answer to a proposal is the answerer's own entry, not an edit of the proposal.
|
|
41
|
+
//
|
|
42
|
+
// The repository is named by hashes: its first commit finds the person's checkout among the folders they work in, and
|
|
43
|
+
// each piece of work records the canon commit it started from. Each agent works on its own branch in its own worktree;
|
|
44
|
+
// code reaches the other side only through the shared remote.
|
|
45
|
+
|
|
46
|
+
const NEXT_SEQ = 1_000_000;
|
|
47
|
+
const OPEN_ONLY = new Set([
|
|
48
|
+
"board.project.answer",
|
|
49
|
+
"board.project.withdraw",
|
|
50
|
+
"board.project.setting",
|
|
51
|
+
"board.project.decide",
|
|
52
|
+
"board.project.note",
|
|
53
|
+
]);
|
|
54
|
+
// Room posts both sides see; notes, held posts and the coordinator's bookkeeping stay on the computer that made them.
|
|
55
|
+
const SHARED_KINDS = new Set([
|
|
56
|
+
"post",
|
|
57
|
+
"result",
|
|
58
|
+
"answer",
|
|
59
|
+
"question",
|
|
60
|
+
"job",
|
|
61
|
+
"failed",
|
|
62
|
+
]);
|
|
63
|
+
const OPEN_WORK = ["checking", "clear", "overlap", "clash", "unchecked"];
|
|
64
|
+
const REFRESH_MS = 5 * 60_000;
|
|
65
|
+
const SUMMARY_CHARS = 600;
|
|
66
|
+
const PROPOSAL_CHARS = 4_000;
|
|
67
|
+
|
|
68
|
+
const now = () => new Date().toISOString();
|
|
69
|
+
const json = (value, fallback) => {
|
|
70
|
+
try {
|
|
71
|
+
return value == null ? fallback : JSON.parse(value);
|
|
72
|
+
} catch {
|
|
73
|
+
return fallback;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const oneLine = (text, limit = 120) => {
|
|
77
|
+
const clean = String(text || "")
|
|
78
|
+
.replace(/\s+/g, " ")
|
|
79
|
+
.trim();
|
|
80
|
+
return clean.length > limit ? `${clean.slice(0, limit - 1)}…` : clean;
|
|
81
|
+
};
|
|
82
|
+
const HANDLE = /^[a-z0-9][a-z0-9-]{2,19}$/;
|
|
83
|
+
|
|
84
|
+
export function createProjects({
|
|
85
|
+
store,
|
|
86
|
+
log = () => {},
|
|
87
|
+
// The person's handle on Harness (their swarm's name), or null when they have not claimed one.
|
|
88
|
+
myHandle,
|
|
89
|
+
relayRequest,
|
|
90
|
+
decideRequest,
|
|
91
|
+
invites: listInvites = async () => [],
|
|
92
|
+
// Where each invitation this person sent stands at Harness: { id, status }.
|
|
93
|
+
sentInvites = async () => [],
|
|
94
|
+
sendEvent,
|
|
95
|
+
endChannel = () => {},
|
|
96
|
+
wake,
|
|
97
|
+
wakeRun,
|
|
98
|
+
notifyBoard = () => {},
|
|
99
|
+
notifyDesktop = () => {},
|
|
100
|
+
// Harness's judgment of planned work against the record; null when Harness cannot judge now.
|
|
101
|
+
checkWork = async () => null,
|
|
102
|
+
knownFolders = () => [],
|
|
103
|
+
agents = () => [],
|
|
104
|
+
worktreeRoot,
|
|
105
|
+
maxPosts = 500,
|
|
106
|
+
maxWakes = 50,
|
|
107
|
+
}) {
|
|
108
|
+
const db = store.db;
|
|
109
|
+
db.exec(`
|
|
110
|
+
create table if not exists projects (
|
|
111
|
+
id text primary key,
|
|
112
|
+
room_id text,
|
|
113
|
+
title text not null,
|
|
114
|
+
me text not null,
|
|
115
|
+
partner text not null,
|
|
116
|
+
role text not null,
|
|
117
|
+
status text not null,
|
|
118
|
+
repo text not null,
|
|
119
|
+
folder text,
|
|
120
|
+
agreed text not null,
|
|
121
|
+
roster text not null default '[]',
|
|
122
|
+
next_seq integer not null,
|
|
123
|
+
created_at text not null,
|
|
124
|
+
updated_at text not null
|
|
125
|
+
);
|
|
126
|
+
create table if not exists project_items (
|
|
127
|
+
id text primary key,
|
|
128
|
+
project_id text not null,
|
|
129
|
+
kind text not null,
|
|
130
|
+
side text not null,
|
|
131
|
+
rev integer not null,
|
|
132
|
+
body text not null,
|
|
133
|
+
at text not null,
|
|
134
|
+
updated_at text not null
|
|
135
|
+
);
|
|
136
|
+
create index if not exists project_items_idx on project_items(project_id, kind);
|
|
137
|
+
create table if not exists project_outbox (
|
|
138
|
+
message_id text primary key,
|
|
139
|
+
project_id text not null,
|
|
140
|
+
seq integer not null,
|
|
141
|
+
body text not null,
|
|
142
|
+
created_at text not null,
|
|
143
|
+
delivered_at text
|
|
144
|
+
);
|
|
145
|
+
create table if not exists project_posts (
|
|
146
|
+
post_id text primary key,
|
|
147
|
+
project_id text not null,
|
|
148
|
+
shared_id text not null,
|
|
149
|
+
state text not null
|
|
150
|
+
);
|
|
151
|
+
`);
|
|
152
|
+
const all = (sql, ...values) =>
|
|
153
|
+
db.all(sql, values.length ? values : undefined);
|
|
154
|
+
const run = (sql, ...values) =>
|
|
155
|
+
db.run(sql, values.length ? values : undefined);
|
|
156
|
+
|
|
157
|
+
// ---- the project rows ----
|
|
158
|
+
|
|
159
|
+
const rowOf = (row) =>
|
|
160
|
+
row && {
|
|
161
|
+
id: row.id,
|
|
162
|
+
roomId: row.room_id,
|
|
163
|
+
title: row.title,
|
|
164
|
+
me: row.me,
|
|
165
|
+
partner: row.partner,
|
|
166
|
+
role: row.role,
|
|
167
|
+
status: row.status,
|
|
168
|
+
repo: json(row.repo, {}),
|
|
169
|
+
folder: row.folder,
|
|
170
|
+
agreed: json(row.agreed, initialSettings()),
|
|
171
|
+
roster: json(row.roster, []),
|
|
172
|
+
nextSeq: Number(row.next_seq),
|
|
173
|
+
createdAt: row.created_at,
|
|
174
|
+
updatedAt: row.updated_at,
|
|
175
|
+
};
|
|
176
|
+
const project = (id) =>
|
|
177
|
+
rowOf(all("select * from projects where id = ?", String(id))[0]);
|
|
178
|
+
const projectOfRoom = (roomId) =>
|
|
179
|
+
rowOf(all("select * from projects where room_id = ?", String(roomId))[0]);
|
|
180
|
+
const projects = () =>
|
|
181
|
+
all("select * from projects order by updated_at desc").map(rowOf);
|
|
182
|
+
const save = (id, fields) => {
|
|
183
|
+
const current = project(id);
|
|
184
|
+
const next = { ...current, ...fields };
|
|
185
|
+
run(
|
|
186
|
+
"update projects set room_id = ?, title = ?, status = ?, repo = ?, folder = ?, agreed = ?, roster = ?, next_seq = ?, updated_at = ? where id = ?",
|
|
187
|
+
next.roomId ?? null,
|
|
188
|
+
next.title,
|
|
189
|
+
next.status,
|
|
190
|
+
JSON.stringify(next.repo),
|
|
191
|
+
next.folder ?? null,
|
|
192
|
+
JSON.stringify(next.agreed),
|
|
193
|
+
JSON.stringify(next.roster ?? []),
|
|
194
|
+
next.nextSeq,
|
|
195
|
+
now(),
|
|
196
|
+
id
|
|
197
|
+
);
|
|
198
|
+
return project(id);
|
|
199
|
+
};
|
|
200
|
+
const insertProject = (fields) => {
|
|
201
|
+
const at = now();
|
|
202
|
+
run(
|
|
203
|
+
"insert into projects(id, room_id, title, me, partner, role, status, repo, folder, agreed, roster, next_seq, created_at, updated_at) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '[]', ?, ?, ?)",
|
|
204
|
+
fields.id,
|
|
205
|
+
fields.roomId ?? null,
|
|
206
|
+
fields.title,
|
|
207
|
+
fields.me,
|
|
208
|
+
fields.partner,
|
|
209
|
+
fields.role,
|
|
210
|
+
fields.status,
|
|
211
|
+
JSON.stringify(fields.repo),
|
|
212
|
+
fields.folder ?? null,
|
|
213
|
+
JSON.stringify(fields.agreed),
|
|
214
|
+
NEXT_SEQ,
|
|
215
|
+
at,
|
|
216
|
+
at
|
|
217
|
+
);
|
|
218
|
+
return project(fields.id);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// ---- the record: entries each side makes and alone changes ----
|
|
222
|
+
|
|
223
|
+
const itemOf = (row) =>
|
|
224
|
+
row && {
|
|
225
|
+
id: row.id,
|
|
226
|
+
projectId: row.project_id,
|
|
227
|
+
kind: row.kind,
|
|
228
|
+
side: row.side,
|
|
229
|
+
rev: Number(row.rev),
|
|
230
|
+
body: json(row.body, {}),
|
|
231
|
+
at: row.at,
|
|
232
|
+
updatedAt: row.updated_at,
|
|
233
|
+
};
|
|
234
|
+
const item = (id) =>
|
|
235
|
+
itemOf(all("select * from project_items where id = ?", String(id))[0]);
|
|
236
|
+
const items = (projectId, kind = null) =>
|
|
237
|
+
all(
|
|
238
|
+
`select * from project_items where project_id = ?${kind ? " and kind = ?" : ""} order by at asc`,
|
|
239
|
+
...(kind ? [projectId, kind] : [projectId])
|
|
240
|
+
).map(itemOf);
|
|
241
|
+
// Keeps an entry if it is newer than what this computer has; true when it changed anything.
|
|
242
|
+
const keep = (entry) => {
|
|
243
|
+
const current = item(entry.id);
|
|
244
|
+
if (current && current.rev >= entry.rev) return false;
|
|
245
|
+
if (current && current.side !== entry.side) return false;
|
|
246
|
+
run(
|
|
247
|
+
"insert into project_items(id, project_id, kind, side, rev, body, at, updated_at) values(?, ?, ?, ?, ?, ?, ?, ?) on conflict(id) do update set rev = excluded.rev, body = excluded.body, updated_at = excluded.updated_at",
|
|
248
|
+
entry.id,
|
|
249
|
+
entry.projectId,
|
|
250
|
+
entry.kind,
|
|
251
|
+
entry.side,
|
|
252
|
+
entry.rev,
|
|
253
|
+
JSON.stringify(entry.body),
|
|
254
|
+
entry.at,
|
|
255
|
+
entry.updatedAt
|
|
256
|
+
);
|
|
257
|
+
return true;
|
|
258
|
+
};
|
|
259
|
+
// This side's own entry, new or changed, kept here and sent across.
|
|
260
|
+
const write = (p, kind, body, id = randomUUID()) => {
|
|
261
|
+
const current = item(id);
|
|
262
|
+
const at = now();
|
|
263
|
+
const entry = {
|
|
264
|
+
id,
|
|
265
|
+
projectId: p.id,
|
|
266
|
+
kind,
|
|
267
|
+
side: p.me,
|
|
268
|
+
rev: (current?.rev ?? 0) + 1,
|
|
269
|
+
body: current ? { ...current.body, ...body } : body,
|
|
270
|
+
at: current?.at ?? at,
|
|
271
|
+
updatedAt: at,
|
|
272
|
+
};
|
|
273
|
+
keep(entry);
|
|
274
|
+
send(p, { type: "item", item: entry });
|
|
275
|
+
// Every screen open on the room follows a change made here, as it does one from the other side.
|
|
276
|
+
if (p.roomId) notifyBoard(store.getTask(p.roomId));
|
|
277
|
+
return item(id);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// ---- the channel between the two computers ----
|
|
281
|
+
|
|
282
|
+
const send = (p, body) => {
|
|
283
|
+
if (!["open", "invited", "joining"].includes(p.status)) return;
|
|
284
|
+
const seq = project(p.id).nextSeq;
|
|
285
|
+
run("update projects set next_seq = ? where id = ?", seq + 1, p.id);
|
|
286
|
+
const messageId = `p:${p.id.slice(0, 8)}:${seq}`;
|
|
287
|
+
run(
|
|
288
|
+
"insert into project_outbox(message_id, project_id, seq, body, created_at) values(?, ?, ?, ?, ?)",
|
|
289
|
+
messageId,
|
|
290
|
+
p.id,
|
|
291
|
+
seq,
|
|
292
|
+
JSON.stringify(body),
|
|
293
|
+
now()
|
|
294
|
+
);
|
|
295
|
+
// Before the other person joins there is no thread to carry it; it goes once they have.
|
|
296
|
+
if (project(p.id).status === "open") deliver(messageId);
|
|
297
|
+
};
|
|
298
|
+
const deliver = (messageId) => {
|
|
299
|
+
const row = all(
|
|
300
|
+
"select * from project_outbox where message_id = ? and delivered_at is null",
|
|
301
|
+
messageId
|
|
302
|
+
)[0];
|
|
303
|
+
const p = row && project(row.project_id);
|
|
304
|
+
if (!row || p?.status !== "open") return;
|
|
305
|
+
sendEvent({
|
|
306
|
+
projectId: p.id,
|
|
307
|
+
to: `${p.partner}/person`,
|
|
308
|
+
messageId,
|
|
309
|
+
seq: Number(row.seq),
|
|
310
|
+
body: json(row.body, {}),
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
/** Harness has the event; it waits there sealed for the other computer. */
|
|
314
|
+
const delivered = (messageId) =>
|
|
315
|
+
run(
|
|
316
|
+
"update project_outbox set delivered_at = ? where message_id = ?",
|
|
317
|
+
now(),
|
|
318
|
+
String(messageId)
|
|
319
|
+
);
|
|
320
|
+
/** Everything not yet delivered goes again, as when the connection comes back. */
|
|
321
|
+
const resend = () => {
|
|
322
|
+
for (const row of all(
|
|
323
|
+
"select message_id from project_outbox where delivered_at is null order by seq asc"
|
|
324
|
+
))
|
|
325
|
+
deliver(row.message_id);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// ---- names across the two sides ----
|
|
329
|
+
|
|
330
|
+
// A name on this computer as the other side knows it: the person is handle/person, an agent handle/agent.
|
|
331
|
+
const shared = (p, name) =>
|
|
332
|
+
String(name).includes("/")
|
|
333
|
+
? String(name)
|
|
334
|
+
: `${p.me}/${name === "user" ? "person" : name}`;
|
|
335
|
+
// A name from the other side as this computer knows it.
|
|
336
|
+
const local = (p, name) => {
|
|
337
|
+
const [handle, who] = String(name).split("/");
|
|
338
|
+
if (handle !== p.me || !who) return String(name);
|
|
339
|
+
return who === "person" ? "user" : who;
|
|
340
|
+
};
|
|
341
|
+
/** How a name reads to the person: their own agents by name, the other side's as "igor's Codex". */
|
|
342
|
+
const display = (p, name) => {
|
|
343
|
+
const [handle, who] = String(name).split("/");
|
|
344
|
+
if (!who) return name === "user" ? "You" : nameOf(name);
|
|
345
|
+
if (handle === p.me) return who === "person" ? "You" : nameOf(who);
|
|
346
|
+
return who === "person" ? handle : `${handle}'s ${nameOf(who)}`;
|
|
347
|
+
};
|
|
348
|
+
// The other side's agents a post names, by their address: "@igor/codex".
|
|
349
|
+
const partnerMentions = (p, text) =>
|
|
350
|
+
p.roster
|
|
351
|
+
.map((entry) => `${p.partner}/${entry.id}`)
|
|
352
|
+
.filter((address) =>
|
|
353
|
+
new RegExp(
|
|
354
|
+
`(^|\\s)@${address.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}\\b`,
|
|
355
|
+
"i"
|
|
356
|
+
).test(String(text))
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
// ---- settings ----
|
|
360
|
+
|
|
361
|
+
const settingsOf = (p) => {
|
|
362
|
+
const changes = [];
|
|
363
|
+
for (const entry of items(p.id, "setting"))
|
|
364
|
+
changes.push({ ...entry.body, at: entry.updatedAt });
|
|
365
|
+
for (const proposal of items(p.id, "proposal")) {
|
|
366
|
+
if (proposal.body.type !== "setting") continue;
|
|
367
|
+
const standing = stateOf(p, proposal);
|
|
368
|
+
if (standing.state === "accepted")
|
|
369
|
+
changes.push({
|
|
370
|
+
key: proposal.body.key,
|
|
371
|
+
value: proposal.body.value,
|
|
372
|
+
at: standing.at,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
return effectiveSettings(p.agreed, changes);
|
|
376
|
+
};
|
|
377
|
+
const answersTo = (p, proposalId) =>
|
|
378
|
+
items(p.id, "answer")
|
|
379
|
+
.filter((answer) => answer.body.proposal === proposalId)
|
|
380
|
+
.map((answer) => ({
|
|
381
|
+
side: answer.side,
|
|
382
|
+
yes: answer.body.yes,
|
|
383
|
+
at: answer.updatedAt,
|
|
384
|
+
}));
|
|
385
|
+
const stateOf = (p, proposal) =>
|
|
386
|
+
proposalState(
|
|
387
|
+
{ ...proposal.body, at: proposal.at },
|
|
388
|
+
answersTo(p, proposal.id)
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
// ---- the room ----
|
|
392
|
+
|
|
393
|
+
const roomOf = (p) => (p.roomId ? store.getTask(p.roomId) : null);
|
|
394
|
+
const makeRoom = (p) => {
|
|
395
|
+
if (p.roomId && store.getTask(p.roomId)) return p;
|
|
396
|
+
const room = store.createGoal({
|
|
397
|
+
objective: p.title,
|
|
398
|
+
brief: "",
|
|
399
|
+
originAgent: "user",
|
|
400
|
+
provenance: { trust: "user", chain: [] },
|
|
401
|
+
team: ["harness"],
|
|
402
|
+
facilitator: "harness",
|
|
403
|
+
workspace: p.folder,
|
|
404
|
+
maxPosts,
|
|
405
|
+
maxWakes,
|
|
406
|
+
});
|
|
407
|
+
store.meta(`project-room:${room.id}`, p.id);
|
|
408
|
+
return save(p.id, { roomId: room.id });
|
|
409
|
+
};
|
|
410
|
+
const note = (p, text) => {
|
|
411
|
+
if (!p.roomId) return;
|
|
412
|
+
store.roomPost({ goalId: p.roomId, author: "harness", kind: "note", text });
|
|
413
|
+
notifyBoard(store.getTask(p.roomId));
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
let nextClash = null;
|
|
417
|
+
// Harness's own posts about the project (a clash, a heads up) go across whatever agents may say.
|
|
418
|
+
let saying = false;
|
|
419
|
+
/** A post made in a project's room on this computer: it goes to the other side, unless it must wait for the person. */
|
|
420
|
+
const posted = (goalId, post) => {
|
|
421
|
+
if (!post || String(post.author).includes("/")) return;
|
|
422
|
+
const p = projectOfRoom(goalId);
|
|
423
|
+
if (!p || !SHARED_KINDS.has(post.kind)) return;
|
|
424
|
+
if (nextClash && post.author === "harness")
|
|
425
|
+
store.meta(`project-clash-post:${post.id}`, JSON.stringify(nextClash));
|
|
426
|
+
if (all("select 1 from project_posts where post_id = ?", post.id).length)
|
|
427
|
+
return;
|
|
428
|
+
const secret = secretReason(post.text);
|
|
429
|
+
const waits =
|
|
430
|
+
secret || (post.author !== "user" && !saying && !settingsOf(p).talk);
|
|
431
|
+
run(
|
|
432
|
+
"insert into project_posts(post_id, project_id, shared_id, state) values(?, ?, ?, ?)",
|
|
433
|
+
post.id,
|
|
434
|
+
p.id,
|
|
435
|
+
post.id,
|
|
436
|
+
waits ? "held" : "sent"
|
|
437
|
+
);
|
|
438
|
+
if (secret) {
|
|
439
|
+
note(
|
|
440
|
+
p,
|
|
441
|
+
`Kept on this computer: ${display(p, post.author)}'s post looks like it holds ${secret}. Share it from here if it is safe.`
|
|
442
|
+
);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (waits) return;
|
|
446
|
+
sharePost(p, post);
|
|
447
|
+
};
|
|
448
|
+
const sharePost = (p, post) =>
|
|
449
|
+
send(p, {
|
|
450
|
+
type: "post",
|
|
451
|
+
...clashOf(post),
|
|
452
|
+
id: post.id,
|
|
453
|
+
author: shared(p, post.author),
|
|
454
|
+
kind: post.kind,
|
|
455
|
+
text: post.text,
|
|
456
|
+
mentions: [
|
|
457
|
+
...new Set([
|
|
458
|
+
...(post.mentions ?? []).map((name) => shared(p, name)),
|
|
459
|
+
...partnerMentions(p, post.text),
|
|
460
|
+
]),
|
|
461
|
+
],
|
|
462
|
+
at: post.created_at ?? now(),
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
// A post that names a clash carries the work it is about, so the other side's agents in it may talk it out.
|
|
466
|
+
const clashOf = (post) => {
|
|
467
|
+
const work = json(store.meta(`project-clash-post:${post.id}`), null);
|
|
468
|
+
return Array.isArray(work) ? { clash: work } : {};
|
|
469
|
+
};
|
|
470
|
+
const settleKey = (p, agent) => `project-settle:${p.id}:${agent}`;
|
|
471
|
+
/**
|
|
472
|
+
* The other side's words with their names as they read here: their "@codex" is "@igor/codex" on this computer, and
|
|
473
|
+
* their "@sig/codex" is this person's own "@codex".
|
|
474
|
+
*/
|
|
475
|
+
const addressedHere = (p, text) => {
|
|
476
|
+
const escape = (value) => value.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
477
|
+
const MINE = "\u0000";
|
|
478
|
+
let words = text.replace(
|
|
479
|
+
new RegExp(`(^|\\s)@${escape(p.me)}/([a-z0-9][a-z0-9._-]*)`, "gi"),
|
|
480
|
+
`$1@${MINE}$2`
|
|
481
|
+
);
|
|
482
|
+
for (const entry of p.roster)
|
|
483
|
+
words = words.replace(
|
|
484
|
+
new RegExp(`(^|\\s)@${escape(entry.id)}(?![\\w/-])`, "gi"),
|
|
485
|
+
`$1@${p.partner}/${entry.id}`
|
|
486
|
+
);
|
|
487
|
+
return words.replaceAll(`@${MINE}`, "@");
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
// ---- what arrives from the other computer ----
|
|
491
|
+
|
|
492
|
+
const receive = (projectId, event) => {
|
|
493
|
+
const p = project(projectId);
|
|
494
|
+
if (!p) return { ok: false, error: "no project on this thread" };
|
|
495
|
+
if (!event || typeof event !== "object")
|
|
496
|
+
return { ok: false, error: "an empty project event" };
|
|
497
|
+
// Until the thread under it opens here, Harness keeps what arrives and brings it again.
|
|
498
|
+
if (p.status !== "open" && event.type !== "left")
|
|
499
|
+
return p.status === "left"
|
|
500
|
+
? { ok: true, ignored: true }
|
|
501
|
+
: { ok: false, error: "the project is not open on this computer yet" };
|
|
502
|
+
if (event.type === "hello") {
|
|
503
|
+
// The other side is in: everything this side has made goes to it, and its agents are known here.
|
|
504
|
+
if (Array.isArray(event.agents)) setRoster(p, event.agents);
|
|
505
|
+
if (event.reply !== false) {
|
|
506
|
+
hello(p, false);
|
|
507
|
+
replay(p);
|
|
508
|
+
}
|
|
509
|
+
return { ok: true };
|
|
510
|
+
}
|
|
511
|
+
if (event.type === "roster") {
|
|
512
|
+
if (Array.isArray(event.agents)) setRoster(p, event.agents);
|
|
513
|
+
return { ok: true };
|
|
514
|
+
}
|
|
515
|
+
if (event.type === "left") {
|
|
516
|
+
if (p.status === "open") {
|
|
517
|
+
save(p.id, { status: "left" });
|
|
518
|
+
store.meta(`project-ended:${p.id}`, "they_left");
|
|
519
|
+
note(
|
|
520
|
+
p,
|
|
521
|
+
`${p.partner} left the project. This room stays here; nothing more goes between you.`
|
|
522
|
+
);
|
|
523
|
+
notifyDesktop(
|
|
524
|
+
`${p.partner} left the project "${oneLine(p.title, 60)}".`
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
return { ok: true };
|
|
528
|
+
}
|
|
529
|
+
if (event.type === "post") return receivePost(p, event);
|
|
530
|
+
if (event.type === "item") return receiveItem(p, event.item);
|
|
531
|
+
return { ok: true };
|
|
532
|
+
};
|
|
533
|
+
const setRoster = (p, agents) =>
|
|
534
|
+
save(p.id, {
|
|
535
|
+
roster: agents
|
|
536
|
+
.filter(
|
|
537
|
+
(entry) =>
|
|
538
|
+
entry && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(String(entry.id))
|
|
539
|
+
)
|
|
540
|
+
.slice(0, 20)
|
|
541
|
+
.map((entry) => ({ id: String(entry.id) })),
|
|
542
|
+
});
|
|
543
|
+
const receivePost = (p, event) => {
|
|
544
|
+
if (!String(event.author || "").startsWith(`${p.partner}/`))
|
|
545
|
+
return {
|
|
546
|
+
ok: false,
|
|
547
|
+
error: "a post in the other side's name only comes from them",
|
|
548
|
+
};
|
|
549
|
+
if (
|
|
550
|
+
all("select 1 from project_posts where shared_id = ?", String(event.id))
|
|
551
|
+
.length
|
|
552
|
+
)
|
|
553
|
+
return { ok: true, duplicate: true };
|
|
554
|
+
const room = roomOf(p);
|
|
555
|
+
if (!room) return { ok: false, error: "the project has no room here yet" };
|
|
556
|
+
const mentions = (Array.isArray(event.mentions) ? event.mentions : [])
|
|
557
|
+
.map(String)
|
|
558
|
+
.map((name) => local(p, name));
|
|
559
|
+
const post = store.roomPost({
|
|
560
|
+
goalId: room.id,
|
|
561
|
+
author: String(event.author),
|
|
562
|
+
kind: SHARED_KINDS.has(event.kind) ? event.kind : "post",
|
|
563
|
+
text: addressedHere(p, String(event.text || "")).slice(0, 20_000),
|
|
564
|
+
mentions,
|
|
565
|
+
});
|
|
566
|
+
run(
|
|
567
|
+
"insert into project_posts(post_id, project_id, shared_id, state) values(?, ?, ?, 'in')",
|
|
568
|
+
post.id,
|
|
569
|
+
p.id,
|
|
570
|
+
String(event.id)
|
|
571
|
+
);
|
|
572
|
+
// The other side's words wake only the agents here they name; their unaddressed words are their own swarm's to sort.
|
|
573
|
+
// Harness works on this person's credits, so the other side reaches it only when both let agents start their asks.
|
|
574
|
+
const named = mentions.filter(
|
|
575
|
+
(name) =>
|
|
576
|
+
!name.includes("/") &&
|
|
577
|
+
name !== "user" &&
|
|
578
|
+
(name !== "harness" || settingsOf(p).start)
|
|
579
|
+
);
|
|
580
|
+
if (mentions.includes("harness") && !named.includes("harness"))
|
|
581
|
+
note(
|
|
582
|
+
p,
|
|
583
|
+
`${display(p, event.author)} asked Harness. Harness answers the other side only once you both let agents start work the other side asks for.`
|
|
584
|
+
);
|
|
585
|
+
// A clash the other side found with one of this side's pieces of work: the agent on it talks it out, read-only.
|
|
586
|
+
const clash = Array.isArray(event.clash) ? event.clash.map(String) : [];
|
|
587
|
+
const settling = named.filter((agent) =>
|
|
588
|
+
items(p.id, "work").some(
|
|
589
|
+
(entry) =>
|
|
590
|
+
entry.side === p.me &&
|
|
591
|
+
entry.body.agent === agent &&
|
|
592
|
+
clash.includes(entry.id)
|
|
593
|
+
)
|
|
594
|
+
);
|
|
595
|
+
for (const agent of settling) store.meta(settleKey(p, agent), "1");
|
|
596
|
+
if (named.length && room.status === "open")
|
|
597
|
+
wake(room.id, post, String(event.author), named, {
|
|
598
|
+
go: settingsOf(p).start || inClash(p, named) || settling.length > 0,
|
|
599
|
+
partner: true,
|
|
600
|
+
});
|
|
601
|
+
notifyBoard(store.getTask(room.id));
|
|
602
|
+
return { ok: true };
|
|
603
|
+
};
|
|
604
|
+
const receiveItem = (p, entry) => {
|
|
605
|
+
if (!entry || entry.side !== p.partner || entry.projectId !== p.id)
|
|
606
|
+
return {
|
|
607
|
+
ok: false,
|
|
608
|
+
error: "a record entry in the other side's name only comes from them",
|
|
609
|
+
};
|
|
610
|
+
if (!["work", "proposal", "answer", "setting", "note"].includes(entry.kind))
|
|
611
|
+
return { ok: true };
|
|
612
|
+
const before = settingsOf(p);
|
|
613
|
+
const previous = item(entry.id);
|
|
614
|
+
const kept = keep({
|
|
615
|
+
...entry,
|
|
616
|
+
rev: Number(entry.rev) || 1,
|
|
617
|
+
body: entry.body && typeof entry.body === "object" ? entry.body : {},
|
|
618
|
+
});
|
|
619
|
+
if (!kept) return { ok: true, duplicate: true };
|
|
620
|
+
const fresh = project(p.id);
|
|
621
|
+
if (entry.kind === "proposal") proposed(fresh, item(entry.id));
|
|
622
|
+
if (entry.kind === "answer") answered(fresh, item(entry.body.proposal));
|
|
623
|
+
if (entry.kind === "setting") settingChanged(fresh, before);
|
|
624
|
+
if (entry.kind === "work") workChanged(fresh, item(entry.id), previous);
|
|
625
|
+
if (fresh.roomId) notifyBoard(store.getTask(fresh.roomId));
|
|
626
|
+
return { ok: true };
|
|
627
|
+
};
|
|
628
|
+
const hello = (p, reply = true) =>
|
|
629
|
+
send(p, {
|
|
630
|
+
type: "hello",
|
|
631
|
+
agents: agents().map((id) => ({ id })),
|
|
632
|
+
reply,
|
|
633
|
+
});
|
|
634
|
+
const replay = (p) => {
|
|
635
|
+
for (const entry of items(p.id).filter((own) => own.side === p.me))
|
|
636
|
+
send(p, { type: "item", item: entry });
|
|
637
|
+
if (!p.roomId) return;
|
|
638
|
+
for (const post of store.roomPosts(p.roomId, 0, 500)) {
|
|
639
|
+
const sent = all(
|
|
640
|
+
"select state from project_posts where post_id = ?",
|
|
641
|
+
post.id
|
|
642
|
+
)[0];
|
|
643
|
+
if (sent?.state === "sent") sharePost(p, post);
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
// ---- people's decisions: proposals and answers ----
|
|
648
|
+
|
|
649
|
+
const proposed = (p, proposal) => {
|
|
650
|
+
const standing = stateOf(p, proposal);
|
|
651
|
+
// What waits on the person is told by the room's needs-you items (needsOf), once each, on every surface.
|
|
652
|
+
if (standing.waitingOn.includes(p.me) && p.roomId)
|
|
653
|
+
notifyBoard(store.getTask(p.roomId));
|
|
654
|
+
if (standing.state === "accepted") accepted(p, proposal);
|
|
655
|
+
};
|
|
656
|
+
const answered = (p, proposal) => {
|
|
657
|
+
if (!proposal) return;
|
|
658
|
+
const standing = stateOf(p, proposal);
|
|
659
|
+
if (standing.state === "accepted") accepted(p, proposal);
|
|
660
|
+
if (standing.state === "declined" && proposal.side === p.me)
|
|
661
|
+
note(p, `Not agreed: ${oneLine(proposal.body.text, 200)}`);
|
|
662
|
+
};
|
|
663
|
+
// An agreed proposal takes effect on each computer for what that computer owns: a fix releases this side's work.
|
|
664
|
+
const accepted = (p, proposal) => {
|
|
665
|
+
const done = `project-accepted:${proposal.id}`;
|
|
666
|
+
if (store.meta(done)) return;
|
|
667
|
+
store.meta(done, "1");
|
|
668
|
+
if (proposal.body.type === "setting") {
|
|
669
|
+
note(
|
|
670
|
+
p,
|
|
671
|
+
`Agreed: ${settingLine(proposal.body.key, proposal.body.value)}.`
|
|
672
|
+
);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (proposal.body.type === "decision" || proposal.body.decision)
|
|
676
|
+
note(
|
|
677
|
+
p,
|
|
678
|
+
`Decided: ${oneLine(proposal.body.decision || proposal.body.text, 300)}`
|
|
679
|
+
);
|
|
680
|
+
if (proposal.body.type === "fix")
|
|
681
|
+
for (const id of proposal.body.about ?? [])
|
|
682
|
+
release(p, item(id), proposal);
|
|
683
|
+
};
|
|
684
|
+
const settingLine = (key, value) =>
|
|
685
|
+
SETTINGS[key].choices
|
|
686
|
+
? `${SETTINGS[key].label}: ${SETTINGS[key].choices[value].toLowerCase()}`
|
|
687
|
+
: `${SETTINGS[key].label}: ${value ? "yes" : "no"}`;
|
|
688
|
+
const settingChanged = (p, before) => {
|
|
689
|
+
const after = settingsOf(p);
|
|
690
|
+
for (const key of SETTING_KEYS)
|
|
691
|
+
if (before[key] !== after[key])
|
|
692
|
+
note(p, `${p.partner} changed it: ${settingLine(key, after[key])}.`);
|
|
693
|
+
};
|
|
694
|
+
/**
|
|
695
|
+
* The people a proposal needs: a change to what both decided, or to a setting, needs both; a fix that reshapes only
|
|
696
|
+
* one side's work needs that side's person (owner, October 20: agents settle it first, a person OKs it).
|
|
697
|
+
*/
|
|
698
|
+
const needsFor = (p, { decision, changes }) => {
|
|
699
|
+
if (decision) return [p.me, p.partner];
|
|
700
|
+
const owners = new Set(
|
|
701
|
+
(changes ?? []).map((id) => item(id)?.side).filter(Boolean)
|
|
702
|
+
);
|
|
703
|
+
return owners.size ? [...owners].sort() : [p.me];
|
|
704
|
+
};
|
|
705
|
+
const propose = (p, body, by) => {
|
|
706
|
+
const proposal = write(p, "proposal", {
|
|
707
|
+
...body,
|
|
708
|
+
by: shared(p, by),
|
|
709
|
+
text: String(body.text).slice(0, PROPOSAL_CHARS),
|
|
710
|
+
});
|
|
711
|
+
// A person's own proposal carries their yes.
|
|
712
|
+
if (by === "user")
|
|
713
|
+
write(
|
|
714
|
+
p,
|
|
715
|
+
"answer",
|
|
716
|
+
{ proposal: proposal.id, yes: true },
|
|
717
|
+
`${proposal.id}:${p.me}`
|
|
718
|
+
);
|
|
719
|
+
proposed(project(p.id), proposal);
|
|
720
|
+
return proposal;
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// ---- work and the check before it ----
|
|
724
|
+
|
|
725
|
+
const openWork = (p, agent) =>
|
|
726
|
+
items(p.id, "work").find(
|
|
727
|
+
(entry) =>
|
|
728
|
+
entry.side === p.me &&
|
|
729
|
+
entry.body.agent === agent &&
|
|
730
|
+
OPEN_WORK.includes(entry.body.status)
|
|
731
|
+
) ?? null;
|
|
732
|
+
const inClash = (p, names) =>
|
|
733
|
+
names.some((agent) => openWork(p, agent)?.body.status === "clash");
|
|
734
|
+
const laneOf = (p, work) =>
|
|
735
|
+
path.join(worktreeRoot, p.id.slice(0, 8), branchSlug(work.body.branch));
|
|
736
|
+
const describe = (p, work) => ({
|
|
737
|
+
id: work.id,
|
|
738
|
+
who: display(p, `${work.side}/${work.body.agent}`),
|
|
739
|
+
side: work.side === p.me ? "this person" : p.partner,
|
|
740
|
+
summary: work.body.summary,
|
|
741
|
+
...(work.body.touches?.length ? { touches: work.body.touches } : {}),
|
|
742
|
+
...(work.body.files?.length ? { files: work.body.files } : {}),
|
|
743
|
+
status: work.body.status,
|
|
744
|
+
});
|
|
745
|
+
const decisionsOf = (p) =>
|
|
746
|
+
items(p.id, "proposal")
|
|
747
|
+
.filter(
|
|
748
|
+
(proposal) =>
|
|
749
|
+
(proposal.body.type === "decision" || proposal.body.decision) &&
|
|
750
|
+
stateOf(p, proposal).state === "accepted"
|
|
751
|
+
)
|
|
752
|
+
.map((proposal) => ({
|
|
753
|
+
id: proposal.id,
|
|
754
|
+
text: proposal.body.decision || proposal.body.text,
|
|
755
|
+
by: proposal.body.by,
|
|
756
|
+
at: stateOf(p, proposal).at,
|
|
757
|
+
}));
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Compares one piece of planned work with what both sides agreed and with every other open piece, and keeps the answer
|
|
761
|
+
* on the work: clear, overlap (the same area, compatible), or clash (incompatible, or against a decision). Files both
|
|
762
|
+
* change make it at least an overlap. A check Harness cannot make leaves it unchecked, and the room says so.
|
|
763
|
+
*/
|
|
764
|
+
const check = async (p, work, phase = "start") => {
|
|
765
|
+
const others = items(p.id, "work").filter(
|
|
766
|
+
(entry) => entry.id !== work.id && OPEN_WORK.includes(entry.body.status)
|
|
767
|
+
);
|
|
768
|
+
const decisions = decisionsOf(p);
|
|
769
|
+
const mine = new Set(work.body.files ?? []);
|
|
770
|
+
const sharedFiles = others
|
|
771
|
+
.map((entry) => ({
|
|
772
|
+
id: entry.id,
|
|
773
|
+
files: (entry.body.files ?? []).filter((file) => mine.has(file)),
|
|
774
|
+
}))
|
|
775
|
+
.filter((entry) => entry.files.length);
|
|
776
|
+
let judged = null;
|
|
777
|
+
if (others.length || decisions.length)
|
|
778
|
+
judged = await checkWork({
|
|
779
|
+
project: p.title,
|
|
780
|
+
repository: p.repo.name,
|
|
781
|
+
phase,
|
|
782
|
+
work: describe(p, work),
|
|
783
|
+
others: others.slice(0, 12).map((entry) => describe(p, entry)),
|
|
784
|
+
decisions: decisions.slice(0, 12).map(({ id, text }) => ({ id, text })),
|
|
785
|
+
}).catch((error) => {
|
|
786
|
+
log(`project check for ${work.id}: ${error.message}`);
|
|
787
|
+
return null;
|
|
788
|
+
});
|
|
789
|
+
const unchecked = (others.length || decisions.length) && !judged;
|
|
790
|
+
const verdict =
|
|
791
|
+
judged?.verdict === "clash"
|
|
792
|
+
? "clash"
|
|
793
|
+
: judged?.verdict === "overlap" || sharedFiles.length
|
|
794
|
+
? "overlap"
|
|
795
|
+
: unchecked
|
|
796
|
+
? "unchecked"
|
|
797
|
+
: "clear";
|
|
798
|
+
return {
|
|
799
|
+
verdict,
|
|
800
|
+
with: [
|
|
801
|
+
...new Set([
|
|
802
|
+
...(judged?.with ?? []).filter((id) =>
|
|
803
|
+
others.some((entry) => entry.id === id)
|
|
804
|
+
),
|
|
805
|
+
...sharedFiles.map((entry) => entry.id),
|
|
806
|
+
]),
|
|
807
|
+
],
|
|
808
|
+
decision: decisions.some((entry) => entry.id === judged?.decision)
|
|
809
|
+
? judged.decision
|
|
810
|
+
: null,
|
|
811
|
+
reason: judged?.reason ? String(judged.reason).slice(0, 600) : null,
|
|
812
|
+
files: sharedFiles.flatMap((entry) => entry.files).slice(0, 10),
|
|
813
|
+
at: now(),
|
|
814
|
+
};
|
|
815
|
+
};
|
|
816
|
+
// One piece of work can be checked twice at once (a new ask while the agent states its plan): only the newest check's
|
|
817
|
+
// answer stands, and anyone waiting on an older one gets the newest's.
|
|
818
|
+
const newestCheck = new Map();
|
|
819
|
+
const checkNewest = async (p, work, phase = "start") => {
|
|
820
|
+
const mine = check(p, work, phase);
|
|
821
|
+
newestCheck.set(work.id, mine);
|
|
822
|
+
let current = mine;
|
|
823
|
+
let result = await mine;
|
|
824
|
+
while (newestCheck.get(work.id) !== current) {
|
|
825
|
+
current = newestCheck.get(work.id);
|
|
826
|
+
result = await current;
|
|
827
|
+
}
|
|
828
|
+
return result;
|
|
829
|
+
};
|
|
830
|
+
const clashWords = (p, work, result) => {
|
|
831
|
+
const against = result.with.map((id) => item(id)).filter(Boolean);
|
|
832
|
+
const decision = decisionsOf(p).find(
|
|
833
|
+
(entry) => entry.id === result.decision
|
|
834
|
+
);
|
|
835
|
+
const parts = [
|
|
836
|
+
...against.map(
|
|
837
|
+
(entry) =>
|
|
838
|
+
`${display(p, `${entry.side}/${entry.body.agent}`)}'s "${oneLine(entry.body.summary, 90)}"`
|
|
839
|
+
),
|
|
840
|
+
...(decision
|
|
841
|
+
? [`what you decided: "${oneLine(decision.text, 120)}"`]
|
|
842
|
+
: []),
|
|
843
|
+
];
|
|
844
|
+
return parts.join(" and ") || "other work here";
|
|
845
|
+
};
|
|
846
|
+
/**
|
|
847
|
+
* The run an agent is woken for in a project's room. The agent's open piece of work goes on as it is; new work is
|
|
848
|
+
* checked first, and starts once it is clear (or only overlaps, or the person set the project to warn). A clash
|
|
849
|
+
* holds that piece alone: the agents talk it out read-only, and it starts once a person OKs their fix.
|
|
850
|
+
*/
|
|
851
|
+
const gate = ({ task, agent, reason, asker, go, mode, words }, start) => {
|
|
852
|
+
const p = projectOfRoom(task.id);
|
|
853
|
+
if (!p || p.status !== "open") return start(mode);
|
|
854
|
+
const settings = settingsOf(p);
|
|
855
|
+
// The other side asking: it waits for the person unless they both let agents start such work. Talking out a clash
|
|
856
|
+
// the other side found is read-only, and never waits.
|
|
857
|
+
const fromPartner = String(asker).startsWith(`${p.partner}/`);
|
|
858
|
+
if (fromPartner && store.meta(settleKey(p, agent))) {
|
|
859
|
+
store.meta(settleKey(p, agent), "");
|
|
860
|
+
return start("review");
|
|
861
|
+
}
|
|
862
|
+
// Checked like any work, so the person sees whether it clashes before they press Start.
|
|
863
|
+
const needsStart = fromPartner && !settings.start && !go;
|
|
864
|
+
if (needsStart && !store.meta(`project-asked-note:${p.id}:${agent}`)) {
|
|
865
|
+
store.meta(`project-asked-note:${p.id}:${agent}`, "1");
|
|
866
|
+
note(
|
|
867
|
+
p,
|
|
868
|
+
`${display(p, asker)} asked ${nameOf(agent)} for work. It waits for your Start: agents here don't start work the other side asks for on their own.`
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
const begin = () => start(needsStart ? "wait" : mode);
|
|
872
|
+
let current = openWork(p, agent);
|
|
873
|
+
if (current?.body.status === "clash") {
|
|
874
|
+
write(p, "work", { waiting: { reason, asker, go } }, current.id);
|
|
875
|
+
return start("review");
|
|
876
|
+
}
|
|
877
|
+
// The asker's own words when the wake carries them, else the wake's reason without its "… asked you:" lead.
|
|
878
|
+
const summary = String(
|
|
879
|
+
words ??
|
|
880
|
+
String(reason).replace(/^[^:]{0,80}(?:mentioned|asked) you: /, "")
|
|
881
|
+
)
|
|
882
|
+
.replace(/(^|\s)@[a-z0-9][a-z0-9./-]*/gi, "$1")
|
|
883
|
+
.replace(/\s+/g, " ")
|
|
884
|
+
.trim()
|
|
885
|
+
.slice(0, SUMMARY_CHARS);
|
|
886
|
+
// A person's new ask is what the agent builds next: its piece of work takes those words and is checked again; its
|
|
887
|
+
// branch takes the new name while nothing is on it yet. An agent's follow-up goes on with the piece as it is.
|
|
888
|
+
const person = asker === "user" || String(asker).endsWith("/person");
|
|
889
|
+
if (current && person && summary !== current.body.summary)
|
|
890
|
+
current = write(
|
|
891
|
+
p,
|
|
892
|
+
"work",
|
|
893
|
+
{
|
|
894
|
+
summary,
|
|
895
|
+
status: "checking",
|
|
896
|
+
...(current.body.head || fs.existsSync(laneOf(p, current))
|
|
897
|
+
? {}
|
|
898
|
+
: { branch: `${p.me}/${agent}/${branchSlug(summary)}` }),
|
|
899
|
+
},
|
|
900
|
+
current.id
|
|
901
|
+
);
|
|
902
|
+
else if (current && current.body.status !== "checking") return begin();
|
|
903
|
+
const work =
|
|
904
|
+
current ??
|
|
905
|
+
write(p, "work", {
|
|
906
|
+
agent,
|
|
907
|
+
summary,
|
|
908
|
+
touches: [],
|
|
909
|
+
files: [],
|
|
910
|
+
status: "checking",
|
|
911
|
+
base: p.repo.head,
|
|
912
|
+
branch: `${p.me}/${agent}/${branchSlug(summary)}`,
|
|
913
|
+
startedFor: shared(p, asker),
|
|
914
|
+
});
|
|
915
|
+
void (async () => {
|
|
916
|
+
const result = await checkNewest(project(p.id), work);
|
|
917
|
+
const fresh = project(p.id);
|
|
918
|
+
const holdsBack =
|
|
919
|
+
result.verdict === "clash" && settingsOf(fresh).check === "stop";
|
|
920
|
+
write(
|
|
921
|
+
fresh,
|
|
922
|
+
"work",
|
|
923
|
+
{
|
|
924
|
+
status: result.verdict,
|
|
925
|
+
check: result,
|
|
926
|
+
...(holdsBack ? { waiting: { reason, asker, go } } : {}),
|
|
927
|
+
},
|
|
928
|
+
work.id
|
|
929
|
+
);
|
|
930
|
+
if (result.verdict === "unchecked")
|
|
931
|
+
note(
|
|
932
|
+
fresh,
|
|
933
|
+
`${nameOf(agent)} starts without a check against ${p.partner}'s work: Harness could not check it just now.`
|
|
934
|
+
);
|
|
935
|
+
if (
|
|
936
|
+
result.verdict === "overlap" ||
|
|
937
|
+
(result.verdict === "clash" && !holdsBack)
|
|
938
|
+
)
|
|
939
|
+
say(
|
|
940
|
+
fresh,
|
|
941
|
+
`Heads up: ${nameOf(agent)}'s "${oneLine(work.body.summary, 90)}" ${result.verdict === "clash" ? "clashes" : "overlaps"} with ${clashWords(fresh, work, result)}${result.files.length ? `, in ${result.files.join(", ")}` : ""}.${result.reason ? ` ${result.reason}` : ""}`,
|
|
942
|
+
[]
|
|
943
|
+
);
|
|
944
|
+
if (!holdsBack) return begin();
|
|
945
|
+
// Each agent in the clash hears it; the other side's agents through the shared post.
|
|
946
|
+
const against = result.with.map((id) => item(id)).filter(Boolean);
|
|
947
|
+
const theirs = against
|
|
948
|
+
.filter((entry) => entry.side !== p.me)
|
|
949
|
+
.map((entry) => `${entry.side}/${entry.body.agent}`);
|
|
950
|
+
const ours = against
|
|
951
|
+
.filter((entry) => entry.side === p.me)
|
|
952
|
+
.map((entry) => entry.body.agent);
|
|
953
|
+
say(
|
|
954
|
+
fresh,
|
|
955
|
+
`Before ${nameOf(agent)} starts "${oneLine(work.body.summary, 90)}": it clashes with ${clashWords(fresh, work, result)}.${result.reason ? ` ${result.reason}` : ""} ${[agent, ...ours, ...theirs].map((name) => `@${name}`).join(" ")}, agree how these fit together, then one of you proposes it with propose (work ${work.id}). The work starts once a person OKs it.`,
|
|
956
|
+
[agent, ...ours, ...theirs],
|
|
957
|
+
[work.id, ...result.with]
|
|
958
|
+
);
|
|
959
|
+
notifyDesktop(
|
|
960
|
+
`Work clashes in "${oneLine(p.title, 60)}": ${nameOf(agent)} waits while the agents settle it.`
|
|
961
|
+
);
|
|
962
|
+
start(
|
|
963
|
+
"review",
|
|
964
|
+
`Your planned work clashes, so it waits. ${result.reason ?? ""} Read the thread, agree with the other agent how the two pieces fit, and propose it (propose, about work ${work.id}). Do not change files until it is agreed.`
|
|
965
|
+
);
|
|
966
|
+
})();
|
|
967
|
+
};
|
|
968
|
+
// Harness's post in the room, shared with the other side; `clash` names the work a clash post is about.
|
|
969
|
+
const say = (p, text, mentions, clash = null) => {
|
|
970
|
+
if (!p.roomId) return null;
|
|
971
|
+
// The room's listener shares the post as it is made (posted), so the clash it names is set first.
|
|
972
|
+
nextClash = clash;
|
|
973
|
+
saying = true;
|
|
974
|
+
const post = store.roomPost({
|
|
975
|
+
goalId: p.roomId,
|
|
976
|
+
author: "harness",
|
|
977
|
+
kind: "post",
|
|
978
|
+
text,
|
|
979
|
+
mentions,
|
|
980
|
+
});
|
|
981
|
+
nextClash = null;
|
|
982
|
+
saying = false;
|
|
983
|
+
notifyBoard(store.getTask(p.roomId));
|
|
984
|
+
return post;
|
|
985
|
+
};
|
|
986
|
+
// A fix both sides agreed: this side's work held by the clash starts.
|
|
987
|
+
const release = (p, work, proposal) => {
|
|
988
|
+
if (!work || work.side !== p.me || work.body.status !== "clash") return;
|
|
989
|
+
const waiting = work.body.waiting;
|
|
990
|
+
write(
|
|
991
|
+
p,
|
|
992
|
+
"work",
|
|
993
|
+
{ status: "clear", fix: proposal.id, waiting: null },
|
|
994
|
+
work.id
|
|
995
|
+
);
|
|
996
|
+
if (waiting && p.roomId)
|
|
997
|
+
wakeRun(
|
|
998
|
+
p.roomId,
|
|
999
|
+
work.body.agent,
|
|
1000
|
+
`The fix is agreed: ${oneLine(proposal.body.text, 400)}. Go ahead with your work in that light.`,
|
|
1001
|
+
{
|
|
1002
|
+
go: true,
|
|
1003
|
+
checked: true,
|
|
1004
|
+
}
|
|
1005
|
+
);
|
|
1006
|
+
};
|
|
1007
|
+
/**
|
|
1008
|
+
* The other side's work, new or with a new plan, arrived here. Each side's check sees only what has reached it, so two
|
|
1009
|
+
* pieces planned at nearly the same moment can each pass: this side checks its own open work planned since then
|
|
1010
|
+
* again. Of the two, the newer piece is the one held, so only one side holds and says so.
|
|
1011
|
+
*/
|
|
1012
|
+
const workChanged = (p, theirs, previous) => {
|
|
1013
|
+
if (!theirs || theirs.side === p.me) return;
|
|
1014
|
+
if (!OPEN_WORK.includes(theirs.body.status)) return;
|
|
1015
|
+
if (previous && previous.body.summary === theirs.body.summary) return;
|
|
1016
|
+
const mine = items(p.id, "work").filter(
|
|
1017
|
+
(entry) =>
|
|
1018
|
+
entry.side === p.me &&
|
|
1019
|
+
["clear", "overlap", "unchecked"].includes(entry.body.status) &&
|
|
1020
|
+
entry.at >= theirs.at
|
|
1021
|
+
);
|
|
1022
|
+
for (const work of mine)
|
|
1023
|
+
void (async () => {
|
|
1024
|
+
const result = await checkNewest(project(p.id), item(work.id));
|
|
1025
|
+
const fresh = project(p.id);
|
|
1026
|
+
const now = item(work.id);
|
|
1027
|
+
if (
|
|
1028
|
+
!result.with.includes(theirs.id) ||
|
|
1029
|
+
result.verdict === now.body.status
|
|
1030
|
+
)
|
|
1031
|
+
return;
|
|
1032
|
+
const holdsBack =
|
|
1033
|
+
result.verdict === "clash" && settingsOf(fresh).check === "stop";
|
|
1034
|
+
write(
|
|
1035
|
+
fresh,
|
|
1036
|
+
"work",
|
|
1037
|
+
{ status: result.verdict, check: result },
|
|
1038
|
+
work.id
|
|
1039
|
+
);
|
|
1040
|
+
const started = now.body.head
|
|
1041
|
+
? ` It has already changed ${now.body.files?.length ?? 0} file(s) on ${now.body.branch}.`
|
|
1042
|
+
: "";
|
|
1043
|
+
if (holdsBack)
|
|
1044
|
+
say(
|
|
1045
|
+
fresh,
|
|
1046
|
+
`${nameOf(now.body.agent)}'s "${oneLine(now.body.summary, 90)}" clashes with ${clashWords(fresh, now, result)}, planned at nearly the same moment.${started} @${now.body.agent} @${theirs.side}/${theirs.body.agent}, agree how these fit together, then one of you proposes it with propose (work ${now.id}). Its next turn waits until a person OKs it.`,
|
|
1047
|
+
[now.body.agent, `${theirs.side}/${theirs.body.agent}`],
|
|
1048
|
+
[now.id, theirs.id]
|
|
1049
|
+
);
|
|
1050
|
+
else
|
|
1051
|
+
say(
|
|
1052
|
+
fresh,
|
|
1053
|
+
`Heads up: ${nameOf(now.body.agent)}'s "${oneLine(now.body.summary, 90)}" ${result.verdict === "clash" ? "clashes" : "overlaps"} with ${clashWords(fresh, now, result)}.`,
|
|
1054
|
+
[]
|
|
1055
|
+
);
|
|
1056
|
+
})().catch((error) => log(`project late check: ${error.message}`));
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
/** Where an agent's run in a project's room works: its own branch for its open piece of work. */
|
|
1060
|
+
const folderFor = (task, agent) => {
|
|
1061
|
+
const p = projectOfRoom(task.id);
|
|
1062
|
+
if (!p || !p.folder) return null;
|
|
1063
|
+
const work = openWork(p, agent);
|
|
1064
|
+
if (!work) return null;
|
|
1065
|
+
const where = laneOf(p, work);
|
|
1066
|
+
// Canon's commit the work started from, when this computer has it; else canon as this computer last fetched it.
|
|
1067
|
+
const base = holds(p.folder, work.body.base)
|
|
1068
|
+
? work.body.base
|
|
1069
|
+
: `origin/${p.repo.canon}`;
|
|
1070
|
+
const made = laneFor({
|
|
1071
|
+
checkout: p.folder,
|
|
1072
|
+
where,
|
|
1073
|
+
branch: work.body.branch,
|
|
1074
|
+
base,
|
|
1075
|
+
});
|
|
1076
|
+
if (!made.ok) throw new Error(made.error);
|
|
1077
|
+
return where;
|
|
1078
|
+
};
|
|
1079
|
+
/** A finished turn's changes, committed on the agent's own branch; pushed when both people let agents push. */
|
|
1080
|
+
const committed = (task) => {
|
|
1081
|
+
const p = projectOfRoom(task.parent_id);
|
|
1082
|
+
if (!p) return undefined;
|
|
1083
|
+
const work = openWork(p, task.target_agent);
|
|
1084
|
+
// A run with no piece of work of its own (a review) worked in the thread's copy, which commits as any thread's does.
|
|
1085
|
+
if (!work) return undefined;
|
|
1086
|
+
const where = laneOf(p, work);
|
|
1087
|
+
const sha = commitLane(
|
|
1088
|
+
where,
|
|
1089
|
+
`${task.target_agent}: ${oneLine(work.body.summary, 150)}`
|
|
1090
|
+
);
|
|
1091
|
+
if (!sha) return null;
|
|
1092
|
+
const files = changedFiles(where, work.body.base);
|
|
1093
|
+
write(p, "work", { head: sha, files }, work.id);
|
|
1094
|
+
if (settingsOf(p).push) void push(p, item(work.id));
|
|
1095
|
+
return sha;
|
|
1096
|
+
};
|
|
1097
|
+
const push = async (p, work) => {
|
|
1098
|
+
const pushed = await pushLane(laneOf(p, work), work.body.branch);
|
|
1099
|
+
if (pushed.ok)
|
|
1100
|
+
write(project(p.id), "work", { pushed: work.body.head ?? null }, work.id);
|
|
1101
|
+
else
|
|
1102
|
+
note(
|
|
1103
|
+
p,
|
|
1104
|
+
`${nameOf(work.body.agent)}'s branch did not reach the remote: ${pushed.error}`
|
|
1105
|
+
);
|
|
1106
|
+
return pushed;
|
|
1107
|
+
};
|
|
1108
|
+
const pullRequest = async (p, work) => {
|
|
1109
|
+
if (work.body.pr?.number) return { ok: true };
|
|
1110
|
+
if (work.body.pushed !== work.body.head) {
|
|
1111
|
+
const pushed = await push(p, work);
|
|
1112
|
+
if (!pushed.ok) return pushed;
|
|
1113
|
+
}
|
|
1114
|
+
const made = await openPullRequest(laneOf(p, work), {
|
|
1115
|
+
branch: work.body.branch,
|
|
1116
|
+
canon: p.repo.canon,
|
|
1117
|
+
title: oneLine(work.body.summary, 120),
|
|
1118
|
+
body: `${work.body.summary}\n\nFrom ${display(p, `${p.me}/${work.body.agent}`)} in the project "${p.title}", shared with ${p.partner} in Harness.`,
|
|
1119
|
+
});
|
|
1120
|
+
if (!made.ok) {
|
|
1121
|
+
note(
|
|
1122
|
+
p,
|
|
1123
|
+
`No pull request for ${nameOf(work.body.agent)}'s work: ${made.error}`
|
|
1124
|
+
);
|
|
1125
|
+
return made;
|
|
1126
|
+
}
|
|
1127
|
+
write(
|
|
1128
|
+
project(p.id),
|
|
1129
|
+
"work",
|
|
1130
|
+
{
|
|
1131
|
+
pr: {
|
|
1132
|
+
number: made.number,
|
|
1133
|
+
url: made.url,
|
|
1134
|
+
state: "open",
|
|
1135
|
+
checks: "running",
|
|
1136
|
+
},
|
|
1137
|
+
},
|
|
1138
|
+
work.id
|
|
1139
|
+
);
|
|
1140
|
+
return made;
|
|
1141
|
+
};
|
|
1142
|
+
|
|
1143
|
+
// ---- the person's commands ----
|
|
1144
|
+
|
|
1145
|
+
const repos = () => {
|
|
1146
|
+
const seen = new Set();
|
|
1147
|
+
const listed = [];
|
|
1148
|
+
for (const folder of knownFolders().slice(0, 40)) {
|
|
1149
|
+
const anchored = anchorOf(folder);
|
|
1150
|
+
if (!anchored.ok || seen.has(anchored.folder)) continue;
|
|
1151
|
+
seen.add(anchored.folder);
|
|
1152
|
+
listed.push({
|
|
1153
|
+
folder: anchored.folder,
|
|
1154
|
+
name: anchored.repo.name,
|
|
1155
|
+
canon: anchored.repo.canon,
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
return listed;
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
// What the person chose for agents when they start a project: any setting they set, the rest as they start.
|
|
1162
|
+
const chosenSettings = (chosen) => {
|
|
1163
|
+
const settings = initialSettings();
|
|
1164
|
+
if (chosen && typeof chosen === "object")
|
|
1165
|
+
for (const key of SETTING_KEYS)
|
|
1166
|
+
if (validSetting(key, chosen[key])) settings[key] = chosen[key];
|
|
1167
|
+
return effectiveSettings(settings, []);
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
const create = async ({ folder, partner, title, settings }) => {
|
|
1171
|
+
const handle = String(partner || "")
|
|
1172
|
+
.toLowerCase()
|
|
1173
|
+
.replace(/^@/, "")
|
|
1174
|
+
.trim();
|
|
1175
|
+
if (!HANDLE.test(handle))
|
|
1176
|
+
throw new Error("Write the other person's swarm name, for example igor.");
|
|
1177
|
+
const anchored = anchorOf(String(folder || ""));
|
|
1178
|
+
if (!anchored.ok) throw new Error(anchored.error);
|
|
1179
|
+
const me = await myHandle();
|
|
1180
|
+
if (!me)
|
|
1181
|
+
throw new Error("Claim your swarm's name first (Swarms, Your card).");
|
|
1182
|
+
if (me === handle) throw new Error("That's your own swarm's name.");
|
|
1183
|
+
const name =
|
|
1184
|
+
String(title || "")
|
|
1185
|
+
.trim()
|
|
1186
|
+
.slice(0, 120) || path.basename(anchored.repo.name);
|
|
1187
|
+
const offer = {
|
|
1188
|
+
v: 1,
|
|
1189
|
+
title: name,
|
|
1190
|
+
from: me,
|
|
1191
|
+
to: handle,
|
|
1192
|
+
repo: anchored.repo,
|
|
1193
|
+
settings: chosenSettings(settings),
|
|
1194
|
+
};
|
|
1195
|
+
const sent = await relayRequest({
|
|
1196
|
+
to: `${handle}/person`,
|
|
1197
|
+
fromAgent: "person",
|
|
1198
|
+
about: `Project: ${name}`,
|
|
1199
|
+
brief: `${me} would like your swarm and theirs to work on ${anchored.repo.name} together in Harness.`,
|
|
1200
|
+
refs: [],
|
|
1201
|
+
project: offer,
|
|
1202
|
+
});
|
|
1203
|
+
if (!sent.sealed)
|
|
1204
|
+
throw new Error(
|
|
1205
|
+
`${handle}'s computer can't take a sealed project yet. Ask them to update Harness.`
|
|
1206
|
+
);
|
|
1207
|
+
const p = makeRoom(
|
|
1208
|
+
insertProject({
|
|
1209
|
+
id: String(sent.threadId),
|
|
1210
|
+
title: name,
|
|
1211
|
+
me,
|
|
1212
|
+
partner: handle,
|
|
1213
|
+
role: "inviter",
|
|
1214
|
+
status: "invited",
|
|
1215
|
+
repo: anchored.repo,
|
|
1216
|
+
folder: anchored.folder,
|
|
1217
|
+
agreed: offer.settings,
|
|
1218
|
+
})
|
|
1219
|
+
);
|
|
1220
|
+
note(
|
|
1221
|
+
p,
|
|
1222
|
+
`Invited ${handle}. Your agents and theirs work here together once they join.`
|
|
1223
|
+
);
|
|
1224
|
+
return p;
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
const join = async ({ id, folder, clone }) => {
|
|
1228
|
+
const invite = (await listInvites()).find((entry) => entry.id === id);
|
|
1229
|
+
if (!invite) throw new Error("That invitation is no longer waiting.");
|
|
1230
|
+
const offer = invite.project;
|
|
1231
|
+
let checkout = folder
|
|
1232
|
+
? findCheckout([String(folder)], offer.repo.first)
|
|
1233
|
+
: null;
|
|
1234
|
+
if (folder && !checkout)
|
|
1235
|
+
throw new Error(`That folder doesn't hold ${offer.repo.name}.`);
|
|
1236
|
+
checkout ??= findCheckout(knownFolders(), offer.repo.first);
|
|
1237
|
+
if (!checkout && clone) {
|
|
1238
|
+
const cloned = await cloneInto(offer.repo.remote, offer.repo.name);
|
|
1239
|
+
if (!cloned.ok) throw new Error(cloned.error);
|
|
1240
|
+
checkout = findCheckout([cloned.folder], offer.repo.first);
|
|
1241
|
+
}
|
|
1242
|
+
if (!checkout)
|
|
1243
|
+
return {
|
|
1244
|
+
ok: false,
|
|
1245
|
+
code: "no_folder",
|
|
1246
|
+
error: `${offer.repo.name} isn't on this computer yet.`,
|
|
1247
|
+
};
|
|
1248
|
+
if (!project(id))
|
|
1249
|
+
insertProject({
|
|
1250
|
+
id,
|
|
1251
|
+
title: offer.title,
|
|
1252
|
+
me: offer.to,
|
|
1253
|
+
partner: offer.from,
|
|
1254
|
+
role: "invitee",
|
|
1255
|
+
status: "joining",
|
|
1256
|
+
repo: offer.repo,
|
|
1257
|
+
folder: checkout,
|
|
1258
|
+
agreed: offer.settings,
|
|
1259
|
+
});
|
|
1260
|
+
else save(id, { folder: checkout, status: "joining" });
|
|
1261
|
+
await decideRequest(id, "accept");
|
|
1262
|
+
return { ok: true, project: view(project(id)) };
|
|
1263
|
+
};
|
|
1264
|
+
|
|
1265
|
+
/** The person thread under a project opened on this computer: the project is open, and the other side hears so. */
|
|
1266
|
+
const channelOpened = (taskId) => {
|
|
1267
|
+
const p = project(taskId);
|
|
1268
|
+
if (!p || !["invited", "joining"].includes(p.status)) return;
|
|
1269
|
+
const opened = makeRoom(save(p.id, { status: "open" }));
|
|
1270
|
+
if (opened.role === "inviter") {
|
|
1271
|
+
note(
|
|
1272
|
+
opened,
|
|
1273
|
+
`${opened.partner} joined. Their agents work here with yours from now on.`
|
|
1274
|
+
);
|
|
1275
|
+
notifyDesktop(`${opened.partner} joined "${oneLine(opened.title, 60)}".`);
|
|
1276
|
+
} else
|
|
1277
|
+
note(
|
|
1278
|
+
opened,
|
|
1279
|
+
`You joined ${opened.partner}'s project. Your agents work here with theirs.`
|
|
1280
|
+
);
|
|
1281
|
+
hello(opened);
|
|
1282
|
+
resend();
|
|
1283
|
+
void refresh().catch((error) => log(`projects: ${error.message}`));
|
|
1284
|
+
};
|
|
1285
|
+
|
|
1286
|
+
/** The thread under a project ended from the other side: they left. */
|
|
1287
|
+
const channelEnded = (taskId) => {
|
|
1288
|
+
const p = project(taskId);
|
|
1289
|
+
if (!p || p.status !== "open") return;
|
|
1290
|
+
save(p.id, { status: "left" });
|
|
1291
|
+
store.meta(`project-ended:${p.id}`, "they_left");
|
|
1292
|
+
note(
|
|
1293
|
+
project(p.id),
|
|
1294
|
+
`${p.partner} left the project. This room stays here; nothing more goes between you.`
|
|
1295
|
+
);
|
|
1296
|
+
notifyDesktop(`${p.partner} left the project "${oneLine(p.title, 60)}".`);
|
|
1297
|
+
};
|
|
1298
|
+
|
|
1299
|
+
const leave = (p) => {
|
|
1300
|
+
if (p.status === "open") send(p, { type: "left", at: now() });
|
|
1301
|
+
save(p.id, { status: "left" });
|
|
1302
|
+
store.meta(`project-ended:${p.id}`, "you_left");
|
|
1303
|
+
endChannel(p.id, "Left the project.");
|
|
1304
|
+
note(
|
|
1305
|
+
project(p.id),
|
|
1306
|
+
"You left the project. This room stays here; nothing more goes between you."
|
|
1307
|
+
);
|
|
1308
|
+
};
|
|
1309
|
+
|
|
1310
|
+
const setting = (p, key, value) => {
|
|
1311
|
+
if (!validSetting(key, value))
|
|
1312
|
+
throw new Error("That setting can't take that value.");
|
|
1313
|
+
const current = settingsOf(p);
|
|
1314
|
+
if (current[key] === value) return { ok: true };
|
|
1315
|
+
if (!widens(key, current[key], value)) {
|
|
1316
|
+
write(p, "setting", { key, value });
|
|
1317
|
+
note(project(p.id), `You changed it: ${settingLine(key, value)}.`);
|
|
1318
|
+
return { ok: true };
|
|
1319
|
+
}
|
|
1320
|
+
const open = items(p.id, "proposal").find(
|
|
1321
|
+
(proposal) =>
|
|
1322
|
+
proposal.body.type === "setting" &&
|
|
1323
|
+
proposal.body.key === key &&
|
|
1324
|
+
stateOf(p, proposal).state === "waiting"
|
|
1325
|
+
);
|
|
1326
|
+
if (open) return { ok: true, waiting: true };
|
|
1327
|
+
propose(
|
|
1328
|
+
p,
|
|
1329
|
+
{
|
|
1330
|
+
type: "setting",
|
|
1331
|
+
key,
|
|
1332
|
+
value,
|
|
1333
|
+
needs: [p.me, p.partner],
|
|
1334
|
+
text: settingLine(key, value),
|
|
1335
|
+
},
|
|
1336
|
+
"user"
|
|
1337
|
+
);
|
|
1338
|
+
return { ok: true, waiting: true };
|
|
1339
|
+
};
|
|
1340
|
+
|
|
1341
|
+
const answer = (p, proposalId, yes) => {
|
|
1342
|
+
const proposal = item(proposalId);
|
|
1343
|
+
if (!proposal || proposal.projectId !== p.id)
|
|
1344
|
+
throw new Error("That proposal isn't here.");
|
|
1345
|
+
if (!(proposal.body.needs ?? []).includes(p.me))
|
|
1346
|
+
throw new Error("That one doesn't need your OK.");
|
|
1347
|
+
write(
|
|
1348
|
+
p,
|
|
1349
|
+
"answer",
|
|
1350
|
+
{ proposal: proposal.id, yes: Boolean(yes) },
|
|
1351
|
+
`${proposal.id}:${p.me}`
|
|
1352
|
+
);
|
|
1353
|
+
answered(project(p.id), item(proposal.id));
|
|
1354
|
+
return { ok: true };
|
|
1355
|
+
};
|
|
1356
|
+
|
|
1357
|
+
const withdraw = (p, proposalId) => {
|
|
1358
|
+
const proposal = item(proposalId);
|
|
1359
|
+
if (!proposal || proposal.side !== p.me)
|
|
1360
|
+
throw new Error("Only a proposal from your side can be withdrawn here.");
|
|
1361
|
+
write(p, "proposal", { withdrawn: true }, proposal.id);
|
|
1362
|
+
return { ok: true };
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
const workAction = async (p, workId, action) => {
|
|
1366
|
+
const work = item(workId);
|
|
1367
|
+
if (!work || work.projectId !== p.id || work.side !== p.me)
|
|
1368
|
+
throw new Error("Only your side's work can be changed from here.");
|
|
1369
|
+
if (action === "push") {
|
|
1370
|
+
const pushed = await push(p, work);
|
|
1371
|
+
if (!pushed.ok) throw new Error(pushed.error);
|
|
1372
|
+
return { ok: true };
|
|
1373
|
+
}
|
|
1374
|
+
if (action === "pull_request") {
|
|
1375
|
+
const made = await pullRequest(p, work);
|
|
1376
|
+
if (!made.ok) throw new Error(made.error);
|
|
1377
|
+
return { ok: true };
|
|
1378
|
+
}
|
|
1379
|
+
if (action === "stop") {
|
|
1380
|
+
write(p, "work", { status: "stopped" }, work.id);
|
|
1381
|
+
return { ok: true };
|
|
1382
|
+
}
|
|
1383
|
+
throw new Error("unknown action");
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
// ---- what agents call ----
|
|
1387
|
+
|
|
1388
|
+
/** An agent's own piece of work: what it plans now, what it touches, or that it is done. A changed plan is checked again. */
|
|
1389
|
+
const plan = async (p, agent, { summary, touches, done, stopped }) => {
|
|
1390
|
+
let work = openWork(p, agent);
|
|
1391
|
+
if (!work && !summary) throw new Error("say what you plan to do (summary)");
|
|
1392
|
+
if (!work)
|
|
1393
|
+
work = write(p, "work", {
|
|
1394
|
+
agent,
|
|
1395
|
+
summary: String(summary).slice(0, SUMMARY_CHARS),
|
|
1396
|
+
touches: [],
|
|
1397
|
+
files: [],
|
|
1398
|
+
status: "checking",
|
|
1399
|
+
base: p.repo.head,
|
|
1400
|
+
branch: `${p.me}/${agent}/${branchSlug(summary)}`,
|
|
1401
|
+
});
|
|
1402
|
+
if (done || stopped) {
|
|
1403
|
+
write(p, "work", { status: done ? "review" : "stopped" }, work.id);
|
|
1404
|
+
const fresh = project(p.id);
|
|
1405
|
+
if (done && settingsOf(fresh).mergeDecisions) {
|
|
1406
|
+
const result = await check(fresh, item(work.id), "merge");
|
|
1407
|
+
if (result.decision)
|
|
1408
|
+
say(
|
|
1409
|
+
fresh,
|
|
1410
|
+
`${nameOf(agent)}'s "${oneLine(work.body.summary, 90)}" changes what you decided: ${oneLine(decisionsOf(fresh).find((entry) => entry.id === result.decision)?.text, 160)}. It needs a new decision before it merges.`,
|
|
1411
|
+
[]
|
|
1412
|
+
);
|
|
1413
|
+
write(fresh, "work", { mergeCheck: result }, work.id);
|
|
1414
|
+
}
|
|
1415
|
+
if (done && settingsOf(fresh).pullRequests)
|
|
1416
|
+
await pullRequest(fresh, item(work.id));
|
|
1417
|
+
return { ok: true, work: workView(fresh, item(work.id)) };
|
|
1418
|
+
}
|
|
1419
|
+
const changed =
|
|
1420
|
+
(summary && String(summary) !== work.body.summary) ||
|
|
1421
|
+
(Array.isArray(touches) &&
|
|
1422
|
+
JSON.stringify(touches) !== JSON.stringify(work.body.touches));
|
|
1423
|
+
write(
|
|
1424
|
+
p,
|
|
1425
|
+
"work",
|
|
1426
|
+
{
|
|
1427
|
+
...(summary
|
|
1428
|
+
? { summary: String(summary).slice(0, SUMMARY_CHARS) }
|
|
1429
|
+
: {}),
|
|
1430
|
+
...(Array.isArray(touches)
|
|
1431
|
+
? { touches: touches.map(String).slice(0, 30) }
|
|
1432
|
+
: {}),
|
|
1433
|
+
},
|
|
1434
|
+
work.id
|
|
1435
|
+
);
|
|
1436
|
+
if (!changed && work.body.status !== "checking")
|
|
1437
|
+
return { ok: true, work: workView(p, item(work.id)) };
|
|
1438
|
+
const result = await checkNewest(project(p.id), item(work.id));
|
|
1439
|
+
const fresh = project(p.id);
|
|
1440
|
+
const holdsBack =
|
|
1441
|
+
result.verdict === "clash" && settingsOf(fresh).check === "stop";
|
|
1442
|
+
write(fresh, "work", { status: result.verdict, check: result }, work.id);
|
|
1443
|
+
return {
|
|
1444
|
+
ok: true,
|
|
1445
|
+
work: workView(fresh, item(work.id)),
|
|
1446
|
+
...(holdsBack
|
|
1447
|
+
? {
|
|
1448
|
+
note: `This clashes with ${clashWords(fresh, work, result)}. ${result.reason ?? ""} Don't change files for it until it's settled: agree with the other agent and propose the fix.`,
|
|
1449
|
+
}
|
|
1450
|
+
: result.verdict === "overlap"
|
|
1451
|
+
? {
|
|
1452
|
+
note: `This overlaps with ${clashWords(fresh, work, result)}. Go ahead, and keep the other side in the loop.`,
|
|
1453
|
+
}
|
|
1454
|
+
: {}),
|
|
1455
|
+
};
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
const agentPropose = (p, agent, { text, decision, about, changes }) => {
|
|
1459
|
+
const body = String(text || "").trim();
|
|
1460
|
+
if (!body) throw new Error("say what you propose (text)");
|
|
1461
|
+
const named = [
|
|
1462
|
+
...new Set([...(about ?? []), ...(changes ?? [])].map(String)),
|
|
1463
|
+
].filter((id) => item(id)?.projectId === p.id);
|
|
1464
|
+
const proposal = propose(
|
|
1465
|
+
p,
|
|
1466
|
+
{
|
|
1467
|
+
type: named.length ? "fix" : "decision",
|
|
1468
|
+
text: body,
|
|
1469
|
+
...(decision
|
|
1470
|
+
? { decision: String(decision).slice(0, PROPOSAL_CHARS) }
|
|
1471
|
+
: {}),
|
|
1472
|
+
about: named,
|
|
1473
|
+
needs: needsFor(p, {
|
|
1474
|
+
decision: decision || !named.length,
|
|
1475
|
+
changes: changes?.length ? changes : named,
|
|
1476
|
+
}),
|
|
1477
|
+
},
|
|
1478
|
+
agent
|
|
1479
|
+
);
|
|
1480
|
+
return {
|
|
1481
|
+
ok: true,
|
|
1482
|
+
proposal: proposal.id,
|
|
1483
|
+
waitingOn: stateOf(p, proposal).waitingOn,
|
|
1484
|
+
};
|
|
1485
|
+
};
|
|
1486
|
+
|
|
1487
|
+
// ---- what the person reads ----
|
|
1488
|
+
|
|
1489
|
+
const workView = (p, work) => {
|
|
1490
|
+
const mine = work.side === p.me;
|
|
1491
|
+
const where = mine ? laneOf(p, work) : null;
|
|
1492
|
+
return {
|
|
1493
|
+
id: work.id,
|
|
1494
|
+
side: work.side,
|
|
1495
|
+
you: mine,
|
|
1496
|
+
agent: work.body.agent,
|
|
1497
|
+
who: display(p, `${work.side}/${work.body.agent}`),
|
|
1498
|
+
summary: work.body.summary,
|
|
1499
|
+
status: work.body.status,
|
|
1500
|
+
branch: work.body.branch,
|
|
1501
|
+
files: work.body.files?.length ?? 0,
|
|
1502
|
+
behind:
|
|
1503
|
+
mine && p.folder && validSha(p.repo.head)
|
|
1504
|
+
? behind(p.folder, work.body.base, p.repo.head)
|
|
1505
|
+
: null,
|
|
1506
|
+
pushed: Boolean(work.body.pushed) && work.body.pushed === work.body.head,
|
|
1507
|
+
committed: Boolean(work.body.head),
|
|
1508
|
+
pr: work.body.pr ?? null,
|
|
1509
|
+
check: work.body.check
|
|
1510
|
+
? {
|
|
1511
|
+
verdict: work.body.check.verdict,
|
|
1512
|
+
reason: work.body.check.reason,
|
|
1513
|
+
with: work.body.check.with
|
|
1514
|
+
.map((id) => item(id))
|
|
1515
|
+
.filter(Boolean)
|
|
1516
|
+
.map((entry) => ({
|
|
1517
|
+
id: entry.id,
|
|
1518
|
+
who: display(p, `${entry.side}/${entry.body.agent}`),
|
|
1519
|
+
summary: entry.body.summary,
|
|
1520
|
+
})),
|
|
1521
|
+
}
|
|
1522
|
+
: null,
|
|
1523
|
+
needsDecision: Boolean(work.body.mergeCheck?.decision),
|
|
1524
|
+
folder: where,
|
|
1525
|
+
updatedAt: work.updatedAt,
|
|
1526
|
+
};
|
|
1527
|
+
};
|
|
1528
|
+
const proposalView = (p, proposal) => {
|
|
1529
|
+
const standing = stateOf(p, proposal);
|
|
1530
|
+
return {
|
|
1531
|
+
id: proposal.id,
|
|
1532
|
+
type: proposal.body.type,
|
|
1533
|
+
text: proposal.body.text,
|
|
1534
|
+
decision: proposal.body.decision ?? null,
|
|
1535
|
+
// The pieces of work a fix settles.
|
|
1536
|
+
about: proposal.body.about ?? [],
|
|
1537
|
+
by: display(p, proposal.body.by),
|
|
1538
|
+
yours: proposal.side === p.me,
|
|
1539
|
+
state: standing.state,
|
|
1540
|
+
needsYou: standing.waitingOn.includes(p.me),
|
|
1541
|
+
waitingOn: standing.waitingOn.map((side) =>
|
|
1542
|
+
side === p.me ? "you" : side
|
|
1543
|
+
),
|
|
1544
|
+
at: proposal.at,
|
|
1545
|
+
};
|
|
1546
|
+
};
|
|
1547
|
+
const view = (p) => {
|
|
1548
|
+
const settings = settingsOf(p);
|
|
1549
|
+
const proposals = items(p.id, "proposal").map((proposal) =>
|
|
1550
|
+
proposalView(p, proposal)
|
|
1551
|
+
);
|
|
1552
|
+
const work = items(p.id, "work")
|
|
1553
|
+
.filter(
|
|
1554
|
+
(entry) =>
|
|
1555
|
+
!["stopped", "merged"].includes(entry.body.status) ||
|
|
1556
|
+
Date.parse(entry.updatedAt) > Date.now() - 3 * 86_400_000
|
|
1557
|
+
)
|
|
1558
|
+
.map((entry) => workView(p, entry));
|
|
1559
|
+
const held = all(
|
|
1560
|
+
"select post_id from project_posts where project_id = ? and state = 'held'",
|
|
1561
|
+
p.id
|
|
1562
|
+
).length;
|
|
1563
|
+
return {
|
|
1564
|
+
id: p.id,
|
|
1565
|
+
roomId: p.roomId,
|
|
1566
|
+
title: p.title,
|
|
1567
|
+
status: p.status,
|
|
1568
|
+
role: p.role,
|
|
1569
|
+
// Why it ended, as the list says it (declined, expired, they_left, you_left), or null.
|
|
1570
|
+
ended: store.meta(`project-ended:${p.id}`) || null,
|
|
1571
|
+
me: p.me,
|
|
1572
|
+
partner: p.partner,
|
|
1573
|
+
repo: {
|
|
1574
|
+
...p.repo,
|
|
1575
|
+
short: p.repo.head?.slice(0, 7) ?? null,
|
|
1576
|
+
folder: p.folder,
|
|
1577
|
+
},
|
|
1578
|
+
people: [
|
|
1579
|
+
{
|
|
1580
|
+
handle: p.me,
|
|
1581
|
+
you: true,
|
|
1582
|
+
agents: agents().map((id) => ({ id, name: nameOf(id) })),
|
|
1583
|
+
},
|
|
1584
|
+
{
|
|
1585
|
+
handle: p.partner,
|
|
1586
|
+
you: false,
|
|
1587
|
+
agents: p.roster.map((entry) => ({
|
|
1588
|
+
id: `${p.partner}/${entry.id}`,
|
|
1589
|
+
name: nameOf(entry.id),
|
|
1590
|
+
})),
|
|
1591
|
+
},
|
|
1592
|
+
],
|
|
1593
|
+
work,
|
|
1594
|
+
decisions: decisionsOf(p).map((entry) => ({
|
|
1595
|
+
...entry,
|
|
1596
|
+
by: display(p, entry.by),
|
|
1597
|
+
})),
|
|
1598
|
+
proposals: proposals.filter((proposal) => proposal.state === "waiting"),
|
|
1599
|
+
notes: items(p.id, "note").map((entry) => ({
|
|
1600
|
+
id: entry.id,
|
|
1601
|
+
text: entry.body.text,
|
|
1602
|
+
by: display(p, entry.body.by ?? `${entry.side}/person`),
|
|
1603
|
+
at: entry.at,
|
|
1604
|
+
})),
|
|
1605
|
+
settings: SETTING_KEYS.map((key) => {
|
|
1606
|
+
const pending = items(p.id, "proposal").find(
|
|
1607
|
+
(proposal) =>
|
|
1608
|
+
proposal.body.type === "setting" &&
|
|
1609
|
+
proposal.body.key === key &&
|
|
1610
|
+
stateOf(p, proposal).state === "waiting"
|
|
1611
|
+
);
|
|
1612
|
+
return {
|
|
1613
|
+
key,
|
|
1614
|
+
label: SETTINGS[key].label,
|
|
1615
|
+
value: settings[key],
|
|
1616
|
+
choices: SETTINGS[key].choices ?? null,
|
|
1617
|
+
pending: pending
|
|
1618
|
+
? {
|
|
1619
|
+
id: pending.id,
|
|
1620
|
+
value: pending.body.value,
|
|
1621
|
+
needsYou: stateOf(p, pending).waitingOn.includes(p.me),
|
|
1622
|
+
by: display(p, pending.body.by),
|
|
1623
|
+
}
|
|
1624
|
+
: null,
|
|
1625
|
+
};
|
|
1626
|
+
}),
|
|
1627
|
+
needsYou: proposals.filter((proposal) => proposal.needsYou).length + held,
|
|
1628
|
+
held,
|
|
1629
|
+
updatedAt: p.updatedAt,
|
|
1630
|
+
};
|
|
1631
|
+
};
|
|
1632
|
+
const summaryOf = (p) => {
|
|
1633
|
+
const full = view(p);
|
|
1634
|
+
return {
|
|
1635
|
+
id: p.id,
|
|
1636
|
+
roomId: p.roomId,
|
|
1637
|
+
title: p.title,
|
|
1638
|
+
status: p.status,
|
|
1639
|
+
partner: p.partner,
|
|
1640
|
+
repo: { name: p.repo.name },
|
|
1641
|
+
// Why a project ended: declined, expired (the invitation ran out), they_left or you_left; null while it hasn't.
|
|
1642
|
+
ended: store.meta(`project-ended:${p.id}`) || null,
|
|
1643
|
+
working: full.work.filter((entry) => OPEN_WORK.includes(entry.status))
|
|
1644
|
+
.length,
|
|
1645
|
+
needsYou: full.needsYou,
|
|
1646
|
+
updatedAt: p.updatedAt,
|
|
1647
|
+
};
|
|
1648
|
+
};
|
|
1649
|
+
|
|
1650
|
+
// ---- the agents' brief ----
|
|
1651
|
+
|
|
1652
|
+
/** What an agent woken in a project's room is told about the project, beside the thread itself. */
|
|
1653
|
+
const brief = (task, agent) => {
|
|
1654
|
+
const p = projectOfRoom(task.id);
|
|
1655
|
+
if (!p || p.status !== "open") return "";
|
|
1656
|
+
const settings = settingsOf(p);
|
|
1657
|
+
const work = items(p.id, "work").filter(
|
|
1658
|
+
(entry) =>
|
|
1659
|
+
OPEN_WORK.includes(entry.body.status) || entry.body.status === "review"
|
|
1660
|
+
);
|
|
1661
|
+
const own = openWork(p, agent);
|
|
1662
|
+
const theirs = p.roster.map((entry) => `@${p.partner}/${entry.id}`);
|
|
1663
|
+
const decisions = decisionsOf(p);
|
|
1664
|
+
const notes = items(p.id, "note").slice(-10);
|
|
1665
|
+
return [
|
|
1666
|
+
`This thread is a project shared with ${p.partner}'s swarm, on ${p.repo.name} (canon branch ${p.repo.canon}${p.repo.head ? ` at ${p.repo.head.slice(0, 7)}` : ""}). Posts here reach both sides; ${p.partner}'s agents read and write here too${theirs.length ? `: ${theirs.join(", ")} (ask one by its full name)` : ""}.`,
|
|
1667
|
+
own
|
|
1668
|
+
? `Your piece of work: "${oneLine(own.body.summary, 200)}" on your own branch ${own.body.branch}, made from canon. Your changes there are committed for you after each turn. Never push to canon or to another agent's branch.`
|
|
1669
|
+
: "",
|
|
1670
|
+
decisions.length
|
|
1671
|
+
? `What the two people decided (keep to it; changing it needs a proposal both OK):\n${decisions.map((entry, n) => `${n + 1}. ${oneLine(entry.text, 300)}`).join("\n")}`
|
|
1672
|
+
: "",
|
|
1673
|
+
work.length
|
|
1674
|
+
? `Who is building what:\n${work.map((entry) => `- ${display(p, `${entry.side}/${entry.body.agent}`)} (work ${entry.id}): "${oneLine(entry.body.summary, 160)}" [${entry.body.status}]${entry.body.files?.length ? ` changing ${entry.body.files.slice(0, 8).join(", ")}` : ""}`).join("\n")}`
|
|
1675
|
+
: "",
|
|
1676
|
+
notes.length
|
|
1677
|
+
? `Project notes:\n${notes.map((entry) => `- ${oneLine(entry.body.text, 300)}`).join("\n")}`
|
|
1678
|
+
: "",
|
|
1679
|
+
[
|
|
1680
|
+
"In this project:",
|
|
1681
|
+
"- Before you change code for something new, say what you plan with plan_work (summary, and the files or interfaces it touches). It is checked against the decisions and the other side's work; if it clashes, it waits.",
|
|
1682
|
+
"- When your work is ready, call plan_work with done.",
|
|
1683
|
+
"- To settle a clash or change a decision, agree with the other agent here, then call propose. A person OKs it before it counts.",
|
|
1684
|
+
"- Save what the other side's agents should know too with save_learning; it is shared.",
|
|
1685
|
+
settings.push
|
|
1686
|
+
? "- Your branch is pushed to the shared remote for you."
|
|
1687
|
+
: "- Your branch stays on this computer until the person pushes it.",
|
|
1688
|
+
].join("\n"),
|
|
1689
|
+
]
|
|
1690
|
+
.filter(Boolean)
|
|
1691
|
+
.join("\n\n");
|
|
1692
|
+
};
|
|
1693
|
+
|
|
1694
|
+
/** A note an agent saved in a project's room is the project's too: both sides' agents read it. */
|
|
1695
|
+
const learned = (goalId, agent, text) => {
|
|
1696
|
+
const p = projectOfRoom(goalId);
|
|
1697
|
+
if (!p || p.status !== "open") return;
|
|
1698
|
+
write(p, "note", {
|
|
1699
|
+
text: String(text).slice(0, 2_000),
|
|
1700
|
+
by: shared(p, agent),
|
|
1701
|
+
});
|
|
1702
|
+
};
|
|
1703
|
+
|
|
1704
|
+
// ---- keeping up with the remote ----
|
|
1705
|
+
|
|
1706
|
+
const refresh = async () => {
|
|
1707
|
+
for (const p of projects().filter(
|
|
1708
|
+
(entry) => entry.status === "open" && entry.folder
|
|
1709
|
+
)) {
|
|
1710
|
+
const head = await fetchCanon(p.folder, p.repo.canon);
|
|
1711
|
+
if (head && head !== p.repo.head)
|
|
1712
|
+
save(p.id, { repo: { ...p.repo, head } });
|
|
1713
|
+
for (const work of items(p.id, "work").filter(
|
|
1714
|
+
(entry) =>
|
|
1715
|
+
entry.side === p.me &&
|
|
1716
|
+
entry.body.pr?.number &&
|
|
1717
|
+
entry.body.pr.state === "open"
|
|
1718
|
+
)) {
|
|
1719
|
+
const pr = await pullRequestState(laneOf(p, work), work.body.pr.number);
|
|
1720
|
+
if (!pr) continue;
|
|
1721
|
+
if (
|
|
1722
|
+
pr.state !== work.body.pr.state ||
|
|
1723
|
+
pr.checks !== work.body.pr.checks
|
|
1724
|
+
)
|
|
1725
|
+
write(
|
|
1726
|
+
project(p.id),
|
|
1727
|
+
"work",
|
|
1728
|
+
{
|
|
1729
|
+
pr: { ...work.body.pr, ...pr },
|
|
1730
|
+
...(pr.state === "merged" ? { status: "merged" } : {}),
|
|
1731
|
+
},
|
|
1732
|
+
work.id
|
|
1733
|
+
);
|
|
1734
|
+
if (pr.state === "merged") {
|
|
1735
|
+
removeLane(p.folder, laneOf(p, work));
|
|
1736
|
+
note(
|
|
1737
|
+
project(p.id),
|
|
1738
|
+
`${nameOf(work.body.agent)}'s "${oneLine(work.body.summary, 90)}" is in canon.`
|
|
1739
|
+
);
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
// A roster that changed (an agent set up here since) goes across.
|
|
1743
|
+
const roster = JSON.stringify(agents());
|
|
1744
|
+
if (store.meta(`project-roster:${p.id}`) !== roster) {
|
|
1745
|
+
store.meta(`project-roster:${p.id}`, roster);
|
|
1746
|
+
send(p, { type: "roster", agents: agents().map((id) => ({ id })) });
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
};
|
|
1750
|
+
const timer = setInterval(
|
|
1751
|
+
() => void refresh().catch((error) => log(`projects: ${error.message}`)),
|
|
1752
|
+
REFRESH_MS
|
|
1753
|
+
);
|
|
1754
|
+
timer.unref?.();
|
|
1755
|
+
|
|
1756
|
+
// ---- the board commands ----
|
|
1757
|
+
|
|
1758
|
+
const answeredWith = (promise) => ({
|
|
1759
|
+
pending: promise.then(
|
|
1760
|
+
(result) => ({ ok: true, ...result }),
|
|
1761
|
+
(error) => ({ ok: false, error: error.message })
|
|
1762
|
+
),
|
|
1763
|
+
});
|
|
1764
|
+
// An invitation the other person declined, or that ran out unanswered, says so on this side.
|
|
1765
|
+
const noteAnswers = async () => {
|
|
1766
|
+
const waiting = projects().filter((p) => p.status === "invited");
|
|
1767
|
+
if (!waiting.length) return;
|
|
1768
|
+
const answers = await sentInvites().catch(() => []);
|
|
1769
|
+
for (const p of waiting) {
|
|
1770
|
+
const answer = answers.find((entry) => entry.id === p.id);
|
|
1771
|
+
if (!["declined", "blocked", "expired"].includes(answer?.status))
|
|
1772
|
+
continue;
|
|
1773
|
+
save(p.id, { status: "declined" });
|
|
1774
|
+
store.meta(
|
|
1775
|
+
`project-ended:${p.id}`,
|
|
1776
|
+
answer.status === "expired" ? "expired" : "declined"
|
|
1777
|
+
);
|
|
1778
|
+
note(
|
|
1779
|
+
project(p.id),
|
|
1780
|
+
answer.status === "expired"
|
|
1781
|
+
? `${p.partner} didn't answer the invitation in a week, so it ran out.`
|
|
1782
|
+
: `${p.partner} said no to working on this together.`
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
const projectFor = (request) =>
|
|
1787
|
+
project(String(request.id || "")) ??
|
|
1788
|
+
projectOfRoom(String(request.id || ""));
|
|
1789
|
+
|
|
1790
|
+
const command = (request, agent) => {
|
|
1791
|
+
const name = String(request.command || "");
|
|
1792
|
+
if (agent !== "user") return agentCommand(name, request, agent);
|
|
1793
|
+
if (name === "board.project.list")
|
|
1794
|
+
return answeredWith(
|
|
1795
|
+
noteAnswers()
|
|
1796
|
+
.then(listInvites)
|
|
1797
|
+
.then((invites) => ({
|
|
1798
|
+
projects: projects()
|
|
1799
|
+
.filter((p) => p.status !== "joining")
|
|
1800
|
+
.map(summaryOf),
|
|
1801
|
+
invites: invites
|
|
1802
|
+
.filter(
|
|
1803
|
+
(invite) =>
|
|
1804
|
+
!project(invite.id) || project(invite.id).status === "joining"
|
|
1805
|
+
)
|
|
1806
|
+
.map((invite) => ({
|
|
1807
|
+
id: invite.id,
|
|
1808
|
+
// Joined here and waiting for the other computer to hear: shown as joining, not counted as waiting on you.
|
|
1809
|
+
joining: project(invite.id)?.status === "joining",
|
|
1810
|
+
title: invite.project.title,
|
|
1811
|
+
from: invite.project.from,
|
|
1812
|
+
repo: {
|
|
1813
|
+
name: invite.project.repo.name,
|
|
1814
|
+
remote: invite.project.repo.remote,
|
|
1815
|
+
},
|
|
1816
|
+
here: findCheckout(knownFolders(), invite.project.repo.first),
|
|
1817
|
+
settings: SETTING_KEYS.map((key) => ({
|
|
1818
|
+
key,
|
|
1819
|
+
label: SETTINGS[key].label,
|
|
1820
|
+
value:
|
|
1821
|
+
invite.project.settings?.[key] ?? SETTINGS[key].initial,
|
|
1822
|
+
choices: SETTINGS[key].choices ?? null,
|
|
1823
|
+
})),
|
|
1824
|
+
})),
|
|
1825
|
+
}))
|
|
1826
|
+
);
|
|
1827
|
+
if (name === "board.project.repos")
|
|
1828
|
+
return {
|
|
1829
|
+
ok: true,
|
|
1830
|
+
repos: repos(),
|
|
1831
|
+
// What agents may do in a new project, as it starts, for the person to change before they invite.
|
|
1832
|
+
settings: SETTING_KEYS.map((key) => ({
|
|
1833
|
+
key,
|
|
1834
|
+
label: SETTINGS[key].label,
|
|
1835
|
+
value: SETTINGS[key].initial,
|
|
1836
|
+
choices: SETTINGS[key].choices ?? null,
|
|
1837
|
+
})),
|
|
1838
|
+
};
|
|
1839
|
+
if (name === "board.project.create")
|
|
1840
|
+
return answeredWith(create(request).then((p) => ({ project: view(p) })));
|
|
1841
|
+
if (name === "board.project.join")
|
|
1842
|
+
return {
|
|
1843
|
+
pending: join({
|
|
1844
|
+
id: String(request.id || ""),
|
|
1845
|
+
folder: request.folder,
|
|
1846
|
+
clone: request.clone === true,
|
|
1847
|
+
}).catch((error) => ({ ok: false, error: error.message })),
|
|
1848
|
+
};
|
|
1849
|
+
if (name === "board.project.decline")
|
|
1850
|
+
return answeredWith(
|
|
1851
|
+
decideRequest(String(request.id || ""), "decline").then(() => ({}))
|
|
1852
|
+
);
|
|
1853
|
+
const p = projectFor(request);
|
|
1854
|
+
if (!p) return { ok: false, error: "project not found" };
|
|
1855
|
+
// A project that isn't open (waiting for the other person, declined, left) takes no new decisions, notes or settings.
|
|
1856
|
+
if (OPEN_ONLY.has(name) && p.status !== "open")
|
|
1857
|
+
return {
|
|
1858
|
+
ok: false,
|
|
1859
|
+
error:
|
|
1860
|
+
p.status === "left" || p.status === "declined"
|
|
1861
|
+
? "This project has ended."
|
|
1862
|
+
: `This waits until ${p.partner} joins.`,
|
|
1863
|
+
};
|
|
1864
|
+
try {
|
|
1865
|
+
switch (name) {
|
|
1866
|
+
case "board.project.read":
|
|
1867
|
+
return { ok: true, project: view(p) };
|
|
1868
|
+
case "board.project.answer":
|
|
1869
|
+
return answer(
|
|
1870
|
+
p,
|
|
1871
|
+
String(request.proposal || ""),
|
|
1872
|
+
request.yes === true
|
|
1873
|
+
);
|
|
1874
|
+
case "board.project.withdraw":
|
|
1875
|
+
return withdraw(p, String(request.proposal || ""));
|
|
1876
|
+
case "board.project.setting":
|
|
1877
|
+
return setting(p, String(request.key || ""), request.value);
|
|
1878
|
+
case "board.project.decide": {
|
|
1879
|
+
const text = String(request.text || "").trim();
|
|
1880
|
+
if (!text) return { ok: false, error: "write what you decide" };
|
|
1881
|
+
propose(
|
|
1882
|
+
p,
|
|
1883
|
+
{ type: "decision", text, needs: [p.me, p.partner] },
|
|
1884
|
+
"user"
|
|
1885
|
+
);
|
|
1886
|
+
return { ok: true, project: view(project(p.id)) };
|
|
1887
|
+
}
|
|
1888
|
+
case "board.project.note": {
|
|
1889
|
+
const text = String(request.text || "").trim();
|
|
1890
|
+
if (!text) return { ok: false, error: "write the note" };
|
|
1891
|
+
write(p, "note", {
|
|
1892
|
+
text: text.slice(0, 2_000),
|
|
1893
|
+
by: `${p.me}/person`,
|
|
1894
|
+
});
|
|
1895
|
+
return { ok: true };
|
|
1896
|
+
}
|
|
1897
|
+
case "board.project.share": {
|
|
1898
|
+
const postId = String(request.post || "");
|
|
1899
|
+
const row = all(
|
|
1900
|
+
"select state from project_posts where post_id = ? and project_id = ?",
|
|
1901
|
+
postId,
|
|
1902
|
+
p.id
|
|
1903
|
+
)[0];
|
|
1904
|
+
if (row?.state !== "held")
|
|
1905
|
+
return { ok: false, error: "that post isn't waiting" };
|
|
1906
|
+
const post = store
|
|
1907
|
+
.roomPosts(p.roomId, 0, 500)
|
|
1908
|
+
.find((entry) => entry.id === postId);
|
|
1909
|
+
if (!post) return { ok: false, error: "that post is gone" };
|
|
1910
|
+
run(
|
|
1911
|
+
"update project_posts set state = 'sent' where post_id = ?",
|
|
1912
|
+
postId
|
|
1913
|
+
);
|
|
1914
|
+
sharePost(p, post);
|
|
1915
|
+
return { ok: true };
|
|
1916
|
+
}
|
|
1917
|
+
case "board.project.held":
|
|
1918
|
+
return {
|
|
1919
|
+
ok: true,
|
|
1920
|
+
posts: all(
|
|
1921
|
+
"select post_id from project_posts where project_id = ? and state = 'held'",
|
|
1922
|
+
p.id
|
|
1923
|
+
)
|
|
1924
|
+
.map((row) =>
|
|
1925
|
+
store
|
|
1926
|
+
.roomPosts(p.roomId, 0, 500)
|
|
1927
|
+
.find((entry) => entry.id === row.post_id)
|
|
1928
|
+
)
|
|
1929
|
+
.filter(Boolean)
|
|
1930
|
+
.map((post) => ({
|
|
1931
|
+
id: post.id,
|
|
1932
|
+
who: display(p, post.author),
|
|
1933
|
+
text: post.text,
|
|
1934
|
+
at: post.created_at,
|
|
1935
|
+
})),
|
|
1936
|
+
};
|
|
1937
|
+
case "board.project.work":
|
|
1938
|
+
return answeredWith(
|
|
1939
|
+
workAction(
|
|
1940
|
+
p,
|
|
1941
|
+
String(request.work || ""),
|
|
1942
|
+
String(request.action || "")
|
|
1943
|
+
)
|
|
1944
|
+
);
|
|
1945
|
+
case "board.project.leave":
|
|
1946
|
+
leave(p);
|
|
1947
|
+
return { ok: true };
|
|
1948
|
+
default:
|
|
1949
|
+
return { ok: false, error: `unknown command ${name}` };
|
|
1950
|
+
}
|
|
1951
|
+
} catch (error) {
|
|
1952
|
+
return { ok: false, error: error.message };
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
|
|
1956
|
+
const agentCommand = (name, request, agent) => {
|
|
1957
|
+
const p = projectFor(request);
|
|
1958
|
+
if (!p || p.status !== "open")
|
|
1959
|
+
return { ok: false, error: "this thread is not an open shared project" };
|
|
1960
|
+
const room = roomOf(p);
|
|
1961
|
+
if (name === "board.project.read")
|
|
1962
|
+
return { ok: true, record: brief(room, agent) };
|
|
1963
|
+
if (name === "board.project.plan")
|
|
1964
|
+
return answeredWith(plan(p, agent, request));
|
|
1965
|
+
if (name === "board.project.propose")
|
|
1966
|
+
try {
|
|
1967
|
+
return agentPropose(p, agent, request);
|
|
1968
|
+
} catch (error) {
|
|
1969
|
+
return { ok: false, error: error.message };
|
|
1970
|
+
}
|
|
1971
|
+
return { ok: false, error: `unknown command ${name}` };
|
|
1972
|
+
};
|
|
1973
|
+
|
|
1974
|
+
return {
|
|
1975
|
+
command,
|
|
1976
|
+
receive,
|
|
1977
|
+
delivered,
|
|
1978
|
+
resend,
|
|
1979
|
+
channelOpened,
|
|
1980
|
+
channelEnded,
|
|
1981
|
+
posted,
|
|
1982
|
+
gate,
|
|
1983
|
+
folderFor,
|
|
1984
|
+
committed,
|
|
1985
|
+
brief,
|
|
1986
|
+
learned,
|
|
1987
|
+
refresh,
|
|
1988
|
+
isProjectRoom: (goalId) => Boolean(projectOfRoom(goalId)),
|
|
1989
|
+
/**
|
|
1990
|
+
* What a project's room waits on the person for, as the thread's needs-you items: an OK the other side or an agent
|
|
1991
|
+
* asked for (agree), and an agent's post kept here until the person shares it (share).
|
|
1992
|
+
*/
|
|
1993
|
+
needsOf: (goalId) => {
|
|
1994
|
+
const p = projectOfRoom(goalId);
|
|
1995
|
+
if (!p || p.status !== "open") return [];
|
|
1996
|
+
const asks = items(p.id, "proposal")
|
|
1997
|
+
.filter((proposal) => stateOf(p, proposal).waitingOn.includes(p.me))
|
|
1998
|
+
.map((proposal) => ({
|
|
1999
|
+
id: `agree:${proposal.id}`,
|
|
2000
|
+
kind: "agree",
|
|
2001
|
+
agent: local(p, proposal.body.by),
|
|
2002
|
+
who: display(p, proposal.body.by),
|
|
2003
|
+
text: proposal.body.text,
|
|
2004
|
+
...(proposal.body.decision
|
|
2005
|
+
? { decision: proposal.body.decision }
|
|
2006
|
+
: {}),
|
|
2007
|
+
projectId: p.id,
|
|
2008
|
+
proposalId: proposal.id,
|
|
2009
|
+
at: proposal.at,
|
|
2010
|
+
}));
|
|
2011
|
+
const held = all(
|
|
2012
|
+
"select post_id from project_posts where project_id = ? and state = 'held'",
|
|
2013
|
+
p.id
|
|
2014
|
+
)
|
|
2015
|
+
.map((row) =>
|
|
2016
|
+
store
|
|
2017
|
+
.roomPosts(goalId, 0, 500)
|
|
2018
|
+
.find((post) => post.id === row.post_id)
|
|
2019
|
+
)
|
|
2020
|
+
.filter(Boolean)
|
|
2021
|
+
.map((post) => ({
|
|
2022
|
+
id: `share:${post.id}`,
|
|
2023
|
+
kind: "share",
|
|
2024
|
+
agent: post.author,
|
|
2025
|
+
who: display(p, post.author),
|
|
2026
|
+
text: `${display(p, post.author)}'s post waits for you to share it with ${p.partner}.`,
|
|
2027
|
+
projectId: p.id,
|
|
2028
|
+
postId: post.id,
|
|
2029
|
+
seq: post.seq,
|
|
2030
|
+
at: post.created_at,
|
|
2031
|
+
}));
|
|
2032
|
+
return [...asks, ...held];
|
|
2033
|
+
},
|
|
2034
|
+
addressesPartner: (goalId, text) => {
|
|
2035
|
+
const p = projectOfRoom(goalId);
|
|
2036
|
+
return Boolean(p) && partnerMentions(p, text).length > 0;
|
|
2037
|
+
},
|
|
2038
|
+
/** What a thread list says of a project's room: the project and who it is shared with. */
|
|
2039
|
+
roomInfo: (goalId) => {
|
|
2040
|
+
const p = projectOfRoom(goalId);
|
|
2041
|
+
return p ? { id: p.id, partner: p.partner, status: p.status } : null;
|
|
2042
|
+
},
|
|
2043
|
+
/** Whether a relay thread is a project's channel, which Swarms does not list as a conversation. */
|
|
2044
|
+
isChannel: (threadId) => Boolean(project(threadId)),
|
|
2045
|
+
// A post from the other side: its own swarm sorts it; this side's coordinator does not take a round on it.
|
|
2046
|
+
fromPartner: (goalId, post) => {
|
|
2047
|
+
const p = projectOfRoom(goalId);
|
|
2048
|
+
return Boolean(p) && String(post.author).startsWith(`${p.partner}/`);
|
|
2049
|
+
},
|
|
2050
|
+
stop: () => clearInterval(timer),
|
|
2051
|
+
};
|
|
2052
|
+
}
|