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
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
// The git half of a shared project. The repository is named by hashes, the same on every computer that has it: its
|
|
7
|
+
// first commit says which repository it is, and the shared remote's canon branch says where work starts and ends.
|
|
8
|
+
// Each agent works on a branch of its own, in a worktree of the person's checkout made from canon's head; code moves
|
|
9
|
+
// between the two people only through the remote.
|
|
10
|
+
|
|
11
|
+
const IDENTITY = [
|
|
12
|
+
"-c",
|
|
13
|
+
"user.name=Harness",
|
|
14
|
+
"-c",
|
|
15
|
+
"user.email=harness@localhost",
|
|
16
|
+
];
|
|
17
|
+
const LONG_MS = 120_000;
|
|
18
|
+
|
|
19
|
+
function git(cwd, args, { timeout = 10_000 } = {}) {
|
|
20
|
+
const result = spawnSync("git", [...IDENTITY, ...args], {
|
|
21
|
+
cwd,
|
|
22
|
+
encoding: "utf8",
|
|
23
|
+
timeout,
|
|
24
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
25
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
ok: result.status === 0,
|
|
29
|
+
out: (result.stdout || "").trim(),
|
|
30
|
+
err: (result.stderr || result.error?.message || "").trim(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The network calls (fetch, push, clone) run without blocking the daemon.
|
|
35
|
+
function gitLater(cwd, args) {
|
|
36
|
+
return new Promise((resolve) => {
|
|
37
|
+
const child = spawn("git", [...IDENTITY, ...args], {
|
|
38
|
+
cwd,
|
|
39
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
40
|
+
});
|
|
41
|
+
let out = "";
|
|
42
|
+
let err = "";
|
|
43
|
+
const timer = setTimeout(() => child.kill("SIGTERM"), LONG_MS);
|
|
44
|
+
child.stdout.on("data", (chunk) => (out += chunk));
|
|
45
|
+
child.stderr.on("data", (chunk) => (err += chunk));
|
|
46
|
+
child.on("error", (error) => {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
resolve({ ok: false, out: "", err: error.message });
|
|
49
|
+
});
|
|
50
|
+
child.on("close", (code) => {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
resolve({ ok: code === 0, out: out.trim(), err: err.trim() });
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A remote's address without credentials in it, so it can be shown and shared. */
|
|
58
|
+
export const cleanRemote = (remote) =>
|
|
59
|
+
String(remote || "").replace(/\/\/[^@/]+@/, "//");
|
|
60
|
+
|
|
61
|
+
/** The same repository named alike on every computer: github.com/a/b for both git@github.com:a/b.git and https. */
|
|
62
|
+
export const remoteName = (remote) =>
|
|
63
|
+
// A remote that is a folder on disk is named by its last part.
|
|
64
|
+
/^(\/|file:\/\/)/.test(String(remote))
|
|
65
|
+
? path.basename(String(remote).replace(/\/+$/, "")).replace(/\.git$/, "")
|
|
66
|
+
: cleanRemote(remote)
|
|
67
|
+
.replace(/^[a-z+]+:\/\//i, "")
|
|
68
|
+
.replace(/^[^@/]+@/, "")
|
|
69
|
+
.replace(/^([^/:]+):(?!\d)/, "$1/")
|
|
70
|
+
.replace(/\.git\/?$/, "")
|
|
71
|
+
.replace(/\/+$/, "")
|
|
72
|
+
.toLowerCase();
|
|
73
|
+
|
|
74
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
75
|
+
export const validSha = (value) => SHA.test(String(value || ""));
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* What names a folder's repository for a project: its top folder, its first commit, its shared remote and that remote's
|
|
79
|
+
* main branch with the commit it is at. An error the person can act on when the folder cannot be shared.
|
|
80
|
+
*/
|
|
81
|
+
export function anchorOf(folder) {
|
|
82
|
+
if (!folder || !fs.existsSync(folder))
|
|
83
|
+
return { ok: false, error: "That folder is not on this computer." };
|
|
84
|
+
const top = git(folder, ["rev-parse", "--show-toplevel"]);
|
|
85
|
+
if (!top.ok)
|
|
86
|
+
return { ok: false, error: "That folder is not a git repository." };
|
|
87
|
+
const root = top.out;
|
|
88
|
+
// A repository with more than one first commit (merged histories) is named by the oldest.
|
|
89
|
+
const firsts = git(root, ["rev-list", "--max-parents=0", "HEAD"]);
|
|
90
|
+
const first = firsts.ok
|
|
91
|
+
? firsts.out.split("\n").filter(validSha).at(-1)
|
|
92
|
+
: null;
|
|
93
|
+
if (!first)
|
|
94
|
+
return { ok: false, error: "That repository has no commits yet." };
|
|
95
|
+
const remote = git(root, ["remote", "get-url", "origin"]);
|
|
96
|
+
if (!remote.ok || !remote.out)
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
error:
|
|
100
|
+
"That repository has no shared remote. Push it somewhere you can both reach first (GitHub, for example).",
|
|
101
|
+
};
|
|
102
|
+
const canon =
|
|
103
|
+
git(root, [
|
|
104
|
+
"symbolic-ref",
|
|
105
|
+
"--short",
|
|
106
|
+
"refs/remotes/origin/HEAD",
|
|
107
|
+
]).out.replace(/^origin\//, "") ||
|
|
108
|
+
["main", "master"].find(
|
|
109
|
+
(name) => git(root, ["rev-parse", "--verify", `origin/${name}`]).ok
|
|
110
|
+
) ||
|
|
111
|
+
git(root, ["rev-parse", "--abbrev-ref", "HEAD"]).out;
|
|
112
|
+
const head =
|
|
113
|
+
git(root, ["rev-parse", `origin/${canon}`]).out ||
|
|
114
|
+
git(root, ["rev-parse", canon]).out;
|
|
115
|
+
return {
|
|
116
|
+
ok: true,
|
|
117
|
+
folder: fs.realpathSync(root),
|
|
118
|
+
repo: {
|
|
119
|
+
first,
|
|
120
|
+
remote: cleanRemote(remote.out),
|
|
121
|
+
name: remoteName(remote.out),
|
|
122
|
+
canon,
|
|
123
|
+
head: validSha(head) ? head : null,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether a folder's repository holds a commit. */
|
|
129
|
+
export const holds = (folder, sha) =>
|
|
130
|
+
validSha(sha) &&
|
|
131
|
+
fs.existsSync(folder) &&
|
|
132
|
+
git(folder, ["cat-file", "-e", `${sha}^{commit}`]).ok;
|
|
133
|
+
|
|
134
|
+
/** The person's own checkout of a repository among the folders they work in: the one holding its first commit. */
|
|
135
|
+
export function findCheckout(folders, first) {
|
|
136
|
+
for (const folder of folders) {
|
|
137
|
+
if (!holds(folder, first)) continue;
|
|
138
|
+
const top = git(folder, ["rev-parse", "--show-toplevel"]).out;
|
|
139
|
+
if (top) return fs.realpathSync(top);
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A new checkout of the shared remote in the home folder, named for the repository, next to nothing already there. */
|
|
145
|
+
export async function cloneInto(remote, name) {
|
|
146
|
+
const base = path.basename(String(name || "project")) || "project";
|
|
147
|
+
let target = path.join(os.homedir(), base);
|
|
148
|
+
for (let n = 2; fs.existsSync(target); n += 1)
|
|
149
|
+
target = path.join(os.homedir(), `${base}-${n}`);
|
|
150
|
+
const cloned = await gitLater(os.homedir(), ["clone", remote, target]);
|
|
151
|
+
if (!cloned.ok)
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
error: `Could not copy the repository from ${remote}: ${cloned.err.split("\n").at(-1)}`,
|
|
155
|
+
};
|
|
156
|
+
return { ok: true, folder: target };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Canon's newest commit on the remote, fetched now; null when the remote could not be reached. */
|
|
160
|
+
export async function fetchCanon(folder, canon) {
|
|
161
|
+
const fetched = await gitLater(folder, ["fetch", "--quiet", "origin", canon]);
|
|
162
|
+
if (!fetched.ok) return null;
|
|
163
|
+
const head = git(folder, ["rev-parse", `origin/${canon}`]).out;
|
|
164
|
+
return validSha(head) ? head : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** How many commits canon has that `sha` does not: how far a branch made from `sha` is behind. */
|
|
168
|
+
export function behind(folder, sha, canonHead) {
|
|
169
|
+
if (!validSha(sha) || !validSha(canonHead) || sha === canonHead) return 0;
|
|
170
|
+
const counted = git(folder, ["rev-list", "--count", `${sha}..${canonHead}`]);
|
|
171
|
+
return counted.ok ? Number(counted.out) || 0 : 0;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export const branchSlug = (text) =>
|
|
175
|
+
String(text)
|
|
176
|
+
.toLowerCase()
|
|
177
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
178
|
+
.replace(/^-|-$/g, "")
|
|
179
|
+
.slice(0, 40) || "work";
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* An agent's own copy for one piece of work: a worktree of the person's checkout on the agent's branch, made from
|
|
183
|
+
* canon's head. One that is already there is used as it is.
|
|
184
|
+
*/
|
|
185
|
+
export function laneFor({ checkout, where, branch, base }) {
|
|
186
|
+
if (fs.existsSync(path.join(where, ".git"))) return { ok: true, path: where };
|
|
187
|
+
fs.mkdirSync(path.dirname(where), { recursive: true });
|
|
188
|
+
const existing = git(checkout, [
|
|
189
|
+
"rev-parse",
|
|
190
|
+
"--verify",
|
|
191
|
+
`refs/heads/${branch}`,
|
|
192
|
+
]).ok;
|
|
193
|
+
const added = existing
|
|
194
|
+
? git(checkout, ["worktree", "add", where, branch])
|
|
195
|
+
: git(checkout, ["worktree", "add", "-b", branch, where, base]);
|
|
196
|
+
return added.ok
|
|
197
|
+
? { ok: true, path: where }
|
|
198
|
+
: {
|
|
199
|
+
ok: false,
|
|
200
|
+
error: `could not make ${branch}: ${added.err.split("\n")[0]}`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Commits what a turn changed on the lane's branch; the new head, or null when nothing changed. */
|
|
205
|
+
export function commitLane(where, message) {
|
|
206
|
+
if (!fs.existsSync(where)) return null;
|
|
207
|
+
git(where, ["add", "-A"]);
|
|
208
|
+
if (git(where, ["diff", "--cached", "--quiet"]).ok) return null;
|
|
209
|
+
const committed = git(where, [
|
|
210
|
+
"commit",
|
|
211
|
+
"-q",
|
|
212
|
+
"-m",
|
|
213
|
+
String(message).slice(0, 200),
|
|
214
|
+
]);
|
|
215
|
+
if (!committed.ok) return null;
|
|
216
|
+
return git(where, ["rev-parse", "HEAD"]).out || null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The files a lane changed since it was made from canon. */
|
|
220
|
+
export function changedFiles(where, base) {
|
|
221
|
+
if (!fs.existsSync(where) || !validSha(base)) return [];
|
|
222
|
+
const listed = git(where, ["diff", "--name-only", `${base}..HEAD`]);
|
|
223
|
+
return listed.ok ? listed.out.split("\n").filter(Boolean).slice(0, 200) : [];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Sends the lane's branch to the shared remote. */
|
|
227
|
+
export async function pushLane(where, branch) {
|
|
228
|
+
const pushed = await gitLater(where, [
|
|
229
|
+
"push",
|
|
230
|
+
"--quiet",
|
|
231
|
+
"-u",
|
|
232
|
+
"origin",
|
|
233
|
+
branch,
|
|
234
|
+
]);
|
|
235
|
+
return pushed.ok
|
|
236
|
+
? { ok: true }
|
|
237
|
+
: { ok: false, error: pushed.err.split("\n").at(-1) || "git push failed" };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function removeLane(checkout, where) {
|
|
241
|
+
git(checkout, ["worktree", "remove", "--force", where]);
|
|
242
|
+
git(checkout, ["worktree", "prune"]);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Pull requests through GitHub's own command line, signed in as the person.
|
|
246
|
+
function gh(cwd, args) {
|
|
247
|
+
return new Promise((resolve) => {
|
|
248
|
+
const child = spawn("gh", args, { cwd, env: process.env });
|
|
249
|
+
let out = "";
|
|
250
|
+
let err = "";
|
|
251
|
+
const timer = setTimeout(() => child.kill("SIGTERM"), LONG_MS);
|
|
252
|
+
child.stdout.on("data", (chunk) => (out += chunk));
|
|
253
|
+
child.stderr.on("data", (chunk) => (err += chunk));
|
|
254
|
+
child.on("error", (error) => {
|
|
255
|
+
clearTimeout(timer);
|
|
256
|
+
resolve({ ok: false, out: "", err: error.message });
|
|
257
|
+
});
|
|
258
|
+
child.on("close", (code) => {
|
|
259
|
+
clearTimeout(timer);
|
|
260
|
+
resolve({ ok: code === 0, out: out.trim(), err: err.trim() });
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Opens a pull request from the lane's branch into canon; its number and address. */
|
|
266
|
+
export async function openPullRequest(where, { branch, canon, title, body }) {
|
|
267
|
+
const made = await gh(where, [
|
|
268
|
+
"pr",
|
|
269
|
+
"create",
|
|
270
|
+
"--head",
|
|
271
|
+
branch,
|
|
272
|
+
"--base",
|
|
273
|
+
canon,
|
|
274
|
+
"--title",
|
|
275
|
+
String(title).slice(0, 200),
|
|
276
|
+
"--body",
|
|
277
|
+
String(body).slice(0, 20_000),
|
|
278
|
+
]);
|
|
279
|
+
if (!made.ok)
|
|
280
|
+
return {
|
|
281
|
+
ok: false,
|
|
282
|
+
error: made.err.split("\n").at(-1) || "gh pr create failed",
|
|
283
|
+
};
|
|
284
|
+
const url = made.out.split("\n").find((line) => /^https?:\/\//.test(line));
|
|
285
|
+
return {
|
|
286
|
+
ok: true,
|
|
287
|
+
url: url ?? null,
|
|
288
|
+
number: Number(url?.split("/").at(-1)) || null,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** A pull request as the project shows it: open, merged or closed, and whether its checks pass. */
|
|
293
|
+
export async function pullRequestState(where, number) {
|
|
294
|
+
const read = await gh(where, [
|
|
295
|
+
"pr",
|
|
296
|
+
"view",
|
|
297
|
+
String(number),
|
|
298
|
+
"--json",
|
|
299
|
+
"state,url,statusCheckRollup",
|
|
300
|
+
]);
|
|
301
|
+
if (!read.ok) return null;
|
|
302
|
+
try {
|
|
303
|
+
const pr = JSON.parse(read.out);
|
|
304
|
+
const checks = Array.isArray(pr.statusCheckRollup)
|
|
305
|
+
? pr.statusCheckRollup
|
|
306
|
+
: [];
|
|
307
|
+
const outcome = (check) => check.conclusion || check.state || check.status;
|
|
308
|
+
const failing = checks.some((check) =>
|
|
309
|
+
["FAILURE", "ERROR", "TIMED_OUT", "CANCELLED"].includes(outcome(check))
|
|
310
|
+
);
|
|
311
|
+
const pending = checks.some((check) =>
|
|
312
|
+
["PENDING", "QUEUED", "IN_PROGRESS", "EXPECTED"].includes(outcome(check))
|
|
313
|
+
);
|
|
314
|
+
return {
|
|
315
|
+
state: String(pr.state || "").toLowerCase(),
|
|
316
|
+
url: pr.url ?? null,
|
|
317
|
+
checks: !checks.length
|
|
318
|
+
? "none"
|
|
319
|
+
: failing
|
|
320
|
+
? "failing"
|
|
321
|
+
: pending
|
|
322
|
+
? "running"
|
|
323
|
+
: "passing",
|
|
324
|
+
};
|
|
325
|
+
} catch {
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// What a shared project lets its agents do, and how the two people change it (owner, October 20): anything that gives
|
|
2
|
+
// agents more reach needs both people; anything that takes reach away takes one, at once. Both computers work the
|
|
3
|
+
// settings out the same way from the same record, so they never disagree on what an agent may do.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Each setting, narrowest first in `values`. `label` is how the person reads it; `on`/`off` read its two states for a
|
|
7
|
+
* yes/no setting, `choices` for one with words.
|
|
8
|
+
*/
|
|
9
|
+
export const SETTINGS = {
|
|
10
|
+
talk: {
|
|
11
|
+
label: "Agents talk to each other here without waiting for you",
|
|
12
|
+
values: [false, true],
|
|
13
|
+
initial: true,
|
|
14
|
+
},
|
|
15
|
+
start: {
|
|
16
|
+
label: "Agents start work the other side asks for",
|
|
17
|
+
values: [false, true],
|
|
18
|
+
initial: false,
|
|
19
|
+
},
|
|
20
|
+
push: {
|
|
21
|
+
label: "Agents push their own branches",
|
|
22
|
+
values: [false, true],
|
|
23
|
+
initial: false,
|
|
24
|
+
},
|
|
25
|
+
pullRequests: {
|
|
26
|
+
label: "Agents open pull requests",
|
|
27
|
+
values: [false, true],
|
|
28
|
+
initial: false,
|
|
29
|
+
},
|
|
30
|
+
check: {
|
|
31
|
+
label: "When planned work clashes",
|
|
32
|
+
values: ["stop", "warn"],
|
|
33
|
+
choices: { stop: "That piece waits", warn: "Warn and go on" },
|
|
34
|
+
initial: "stop",
|
|
35
|
+
},
|
|
36
|
+
mergeChecks: {
|
|
37
|
+
label: "A merge needs its checks to pass",
|
|
38
|
+
values: [true, false],
|
|
39
|
+
initial: true,
|
|
40
|
+
},
|
|
41
|
+
mergeDecisions: {
|
|
42
|
+
label: "A change to something you decided needs a new decision first",
|
|
43
|
+
values: [true, false],
|
|
44
|
+
initial: true,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const SETTING_KEYS = Object.keys(SETTINGS);
|
|
49
|
+
|
|
50
|
+
export const initialSettings = () =>
|
|
51
|
+
Object.fromEntries(SETTING_KEYS.map((key) => [key, SETTINGS[key].initial]));
|
|
52
|
+
|
|
53
|
+
/** A value this setting can hold; anything else is refused. */
|
|
54
|
+
export const validSetting = (key, value) =>
|
|
55
|
+
Object.hasOwn(SETTINGS, key) && SETTINGS[key].values.includes(value);
|
|
56
|
+
|
|
57
|
+
/** Whether moving `key` from `from` to `to` gives agents more reach, so the other person has to agree. */
|
|
58
|
+
export const widens = (key, from, to) => {
|
|
59
|
+
const order = SETTINGS[key].values;
|
|
60
|
+
return order.indexOf(to) > order.indexOf(from);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The narrower of two values of one setting. */
|
|
64
|
+
const narrower = (key, a, b) =>
|
|
65
|
+
SETTINGS[key].values.indexOf(a) <= SETTINGS[key].values.indexOf(b) ? a : b;
|
|
66
|
+
|
|
67
|
+
/** Pushing and pull requests depend on each other: an agent opens a pull request only from a branch it pushed. */
|
|
68
|
+
const consistent = (settings) =>
|
|
69
|
+
settings.pullRequests && !settings.push
|
|
70
|
+
? { ...settings, pullRequests: false }
|
|
71
|
+
: settings;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The settings in force: those both people agreed to at the start, then each change by when it took effect (a narrowing
|
|
75
|
+
* when it was made, a widening when the other person agreed). Two changes at the same moment keep the narrower. Every
|
|
76
|
+
* change is `{ key, value, at }`.
|
|
77
|
+
*/
|
|
78
|
+
export function effectiveSettings(agreed, changes) {
|
|
79
|
+
const latest = {};
|
|
80
|
+
for (const change of changes) {
|
|
81
|
+
if (!validSetting(change.key, change.value)) continue;
|
|
82
|
+
const current = latest[change.key];
|
|
83
|
+
if (!current || change.at > current.at) latest[change.key] = change;
|
|
84
|
+
else if (change.at === current.at)
|
|
85
|
+
latest[change.key] = {
|
|
86
|
+
...change,
|
|
87
|
+
value: narrower(change.key, change.value, current.value),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const settings = { ...initialSettings() };
|
|
91
|
+
for (const key of SETTING_KEYS) {
|
|
92
|
+
if (validSetting(key, agreed?.[key])) settings[key] = agreed[key];
|
|
93
|
+
if (latest[key]) settings[key] = latest[key].value;
|
|
94
|
+
}
|
|
95
|
+
return consistent(settings);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Where a proposal stands: every person it needs said yes (accepted, at the last yes), one said no (declined), its
|
|
100
|
+
* proposer withdrew it, or it waits on the people who have not answered. `answers` are `{ side, yes, at }`; a later
|
|
101
|
+
* answer from the same side replaces its earlier one.
|
|
102
|
+
*/
|
|
103
|
+
export function proposalState(proposal, answers) {
|
|
104
|
+
if (proposal.withdrawn) return { state: "withdrawn", waitingOn: [] };
|
|
105
|
+
const bySide = new Map();
|
|
106
|
+
for (const answer of [...answers].sort((a, b) =>
|
|
107
|
+
String(a.at).localeCompare(String(b.at))
|
|
108
|
+
))
|
|
109
|
+
bySide.set(answer.side, answer);
|
|
110
|
+
const needed = proposal.needs ?? [];
|
|
111
|
+
if (needed.some((side) => bySide.get(side)?.yes === false))
|
|
112
|
+
return { state: "declined", waitingOn: [] };
|
|
113
|
+
const waitingOn = needed.filter((side) => bySide.get(side)?.yes !== true);
|
|
114
|
+
if (waitingOn.length) return { state: "waiting", waitingOn };
|
|
115
|
+
const at = needed
|
|
116
|
+
.map((side) => String(bySide.get(side).at))
|
|
117
|
+
.sort()
|
|
118
|
+
.at(-1);
|
|
119
|
+
return { state: "accepted", waitingOn: [], at: at ?? proposal.at };
|
|
120
|
+
}
|