tryharness 0.7.44 → 0.7.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -12
- package/cli.mjs +406 -184
- package/lib/actorRoster.mjs +28 -5
- package/lib/adapters.mjs +9 -1
- package/lib/agentRuntimeConfig.mjs +36 -0
- package/lib/agentRuntimes.mjs +560 -0
- package/lib/atlas.mjs +7 -2
- package/lib/boardCommands.mjs +5 -10
- package/lib/boardFileCommands.mjs +2 -1
- package/lib/boardMirror.mjs +459 -0
- package/lib/boardProtocol.json +259 -6
- package/lib/boardTools.mjs +2 -6
- package/lib/bridge.mjs +27 -3
- package/lib/browserWake.mjs +60 -0
- package/lib/cards.mjs +35 -2
- package/lib/commands.mjs +20 -5
- package/lib/connectionTools.d.ts +13 -0
- package/lib/connectionTools.js +45 -0
- package/lib/connectorAuth.mjs +309 -0
- package/lib/connectorCatalog.json +41 -0
- package/lib/connectorCommands.mjs +121 -0
- package/lib/connectorGoogle.mjs +513 -0
- package/lib/connectors.mjs +805 -0
- package/lib/coordinator.mjs +16 -2
- package/lib/coreModels.mjs +78 -0
- package/lib/daemon.mjs +775 -46
- package/lib/desktop.mjs +7 -2
- package/lib/digest.mjs +32 -0
- package/lib/directLink.mjs +283 -0
- package/lib/environments.mjs +595 -0
- package/lib/folders.mjs +2 -2
- package/lib/goalCommands.mjs +61 -12
- package/lib/inference.mjs +263 -152
- package/lib/lessons.mjs +6 -0
- package/lib/lineup.mjs +324 -0
- package/lib/localmodels.mjs +10 -0
- package/lib/machineIdentity.mjs +3 -0
- package/lib/machineRoutes.mjs +40 -2
- package/lib/machineRuns.mjs +30 -3
- package/lib/machineSeal.mjs +69 -10
- package/lib/mcp-stdio.mjs +180 -98
- package/lib/mcpRelay.mjs +119 -0
- package/lib/members.json +12 -0
- package/lib/members.mjs +33 -0
- package/lib/modelMemory.mjs +100 -0
- package/lib/modelTiers.json +1183 -0
- package/lib/modelTiers.mjs +133 -0
- package/lib/models.mjs +41 -8
- package/lib/museSession.mjs +119 -0
- package/lib/nativeHost.mjs +203 -0
- package/lib/network.mjs +38 -1
- package/lib/nudge.mjs +10 -2
- package/lib/onboard.mjs +430 -1
- package/lib/oneclawLocal.mjs +150 -0
- package/lib/ownMachines.mjs +521 -36
- package/lib/pairing.mjs +165 -0
- package/lib/paths.mjs +4 -0
- package/lib/phones.mjs +61 -0
- package/lib/placeCommands.mjs +27 -0
- package/lib/places.mjs +10 -2
- package/lib/plans.mjs +151 -27
- package/lib/profile.mjs +152 -0
- package/lib/projectGit.mjs +328 -0
- package/lib/projectSettings.mjs +120 -0
- package/lib/projects.mjs +2539 -0
- package/lib/readiness.mjs +283 -0
- package/lib/repoLinks.mjs +354 -0
- package/lib/roomChannels.mjs +641 -0
- package/lib/runBrief.mjs +32 -2
- package/lib/runRequests.mjs +157 -0
- package/lib/runWorker.mjs +20 -7
- package/lib/runner.mjs +67 -8
- package/lib/secretPatterns.json +55 -0
- package/lib/secrets.mjs +10 -27
- package/lib/seen.mjs +102 -0
- package/lib/service.mjs +11 -6
- package/lib/sessionControl.mjs +10 -8
- package/lib/sessionIndex.mjs +5 -3
- package/lib/store.mjs +559 -30
- package/lib/subagents.mjs +8 -4
- package/lib/swarm.mjs +379 -38
- package/lib/swarmMemory.mjs +947 -0
- package/lib/t3.mjs +43 -13
- package/lib/taskCommands.mjs +58 -5
- package/lib/taskRuns.mjs +61 -11
- package/lib/team.json +17 -15
- package/lib/team.mjs +68 -18
- package/lib/threadRuns.mjs +50 -7
- package/lib/threadView.mjs +68 -15
- package/lib/tokenizer.mjs +405 -0
- package/lib/ui.mjs +16 -2
- package/lib/usageTotals.mjs +148 -0
- package/lib/vault.mjs +1144 -0
- package/lib/vaultCloud.mjs +117 -0
- package/lib/vaultCommands.mjs +201 -0
- package/lib/vaultTools.d.ts +12 -0
- package/lib/vaultTools.js +42 -0
- package/lib/windowAsks.mjs +42 -0
- package/lib/workFocus.mjs +59 -0
- package/lib/workLinks.mjs +90 -0
- package/package.json +9 -4
package/lib/profile.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import pc from "picocolors";
|
|
3
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
4
|
+
import { paths } from "./paths.mjs";
|
|
5
|
+
|
|
6
|
+
// The person's Harness profile on this computer: their name and how they like to work, so every agent here knows who
|
|
7
|
+
// it works for. The profile is the account's, one for all their computers: Harness sends it to the daemon when it
|
|
8
|
+
// connects and whenever it changes anywhere (src/server/bridge/profilePush.ts), and the daemon keeps it here. Setup
|
|
9
|
+
// imports it too, and when the account has no name yet, asks for one and saves it to the account. Each run's brief
|
|
10
|
+
// carries it (runBrief.mjs). The saved copy holds only what the server shares with a computer.
|
|
11
|
+
|
|
12
|
+
const NAME_LIMIT = 80;
|
|
13
|
+
const PREFERENCE_LIMIT = 200;
|
|
14
|
+
|
|
15
|
+
/** The account's profile as this computer last had it, or null. */
|
|
16
|
+
export function readProfile(file = paths.profile) {
|
|
17
|
+
const saved = readJson(file, null);
|
|
18
|
+
return saved?.displayName ? saved : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Keeps the account's profile as Harness sent it; one without a name leaves this computer with none. */
|
|
22
|
+
export function keepProfile(profile, file = paths.profile) {
|
|
23
|
+
if (profile?.displayName) writeJsonAtomic(file, profile);
|
|
24
|
+
else fs.rmSync(file, { force: true });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function request(origin, token, init = {}) {
|
|
28
|
+
const response = await fetch(
|
|
29
|
+
`${String(origin).replace(/\/$/, "")}/api/mcp/profile`,
|
|
30
|
+
{
|
|
31
|
+
...init,
|
|
32
|
+
headers: {
|
|
33
|
+
authorization: `Bearer ${token}`,
|
|
34
|
+
"content-type": "application/json",
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
const body = await response.json().catch(() => null);
|
|
39
|
+
if (!response.ok)
|
|
40
|
+
throw Object.assign(
|
|
41
|
+
new Error(body?.error || `Harness answered ${response.status}`),
|
|
42
|
+
{ status: response.status }
|
|
43
|
+
);
|
|
44
|
+
return body?.profile ?? {};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const preferenceCount = (profile) =>
|
|
48
|
+
(profile.hardPreferences?.length ?? 0) +
|
|
49
|
+
(profile.softPreferences?.length ?? 0);
|
|
50
|
+
|
|
51
|
+
const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
52
|
+
|
|
53
|
+
async function ask(ui, profile) {
|
|
54
|
+
ui.note(
|
|
55
|
+
"Your agents work for you, so they should know who you are. What you write here is saved to your Harness account, so Harness and the agents on all your computers use it. Change it any time in Harness, Settings, Profile.",
|
|
56
|
+
"About you"
|
|
57
|
+
);
|
|
58
|
+
const name = await ui.text({
|
|
59
|
+
message: "What should your agents call you?",
|
|
60
|
+
validate: (value) =>
|
|
61
|
+
String(value ?? "").trim().length > NAME_LIMIT
|
|
62
|
+
? `Keep it under ${NAME_LIMIT} characters`
|
|
63
|
+
: undefined,
|
|
64
|
+
});
|
|
65
|
+
const displayName = String(name ?? "").trim();
|
|
66
|
+
if (!displayName) return null;
|
|
67
|
+
const update = { displayName };
|
|
68
|
+
if (!preferenceCount(profile)) {
|
|
69
|
+
const said = await ui.text({
|
|
70
|
+
message:
|
|
71
|
+
"Anything they should always keep in mind about how you work? Press Enter to skip.",
|
|
72
|
+
placeholder: "Keep answers short, and ask before deleting anything",
|
|
73
|
+
validate: (value) =>
|
|
74
|
+
String(value ?? "").trim().length > PREFERENCE_LIMIT
|
|
75
|
+
? `Keep it under ${PREFERENCE_LIMIT} characters`
|
|
76
|
+
: undefined,
|
|
77
|
+
});
|
|
78
|
+
const preference = String(said ?? "").trim();
|
|
79
|
+
if (preference) update.hardPreferences = [preference];
|
|
80
|
+
}
|
|
81
|
+
return update;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Setup's step: imports the person's profile into this computer, asking for their name (and one preference, when they
|
|
86
|
+
* have none) if their account has no name yet. Never stops setup: a profile Harness cannot give leaves the last import.
|
|
87
|
+
*/
|
|
88
|
+
export async function setupProfile({
|
|
89
|
+
ui,
|
|
90
|
+
origin,
|
|
91
|
+
token,
|
|
92
|
+
file = paths.profile,
|
|
93
|
+
}) {
|
|
94
|
+
let profile;
|
|
95
|
+
try {
|
|
96
|
+
profile = await request(origin, token);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
// A Harness from before profiles reached computers has no such page: nothing is wrong, so nothing is said.
|
|
99
|
+
if (error.status === 404) return readProfile(file);
|
|
100
|
+
ui.warn(
|
|
101
|
+
`Your Harness profile did not load (${error.message}). Your agents will learn your name the next time you run setup.`
|
|
102
|
+
);
|
|
103
|
+
return readProfile(file);
|
|
104
|
+
}
|
|
105
|
+
if (!profile.displayName) {
|
|
106
|
+
const update = ui.interactive ? await ask(ui, profile) : null;
|
|
107
|
+
if (!update) {
|
|
108
|
+
ui.info(
|
|
109
|
+
"Your agents will not know your name yet. Add it any time in Harness, Settings, Profile, then run setup again."
|
|
110
|
+
);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
profile = await request(origin, token, {
|
|
115
|
+
method: "PUT",
|
|
116
|
+
body: JSON.stringify(update),
|
|
117
|
+
});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
// The agents here still get it; the next setup, finding no name in the account, asks again.
|
|
120
|
+
profile = { ...profile, ...update };
|
|
121
|
+
ui.warn(
|
|
122
|
+
`Saved on this computer, but not to your Harness account yet (${error.message}).`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
writeJsonAtomic(file, profile);
|
|
127
|
+
const count = preferenceCount(profile);
|
|
128
|
+
ui.success(
|
|
129
|
+
`Your agents know you as ${pc.bold(profile.displayName)}${count ? `, with your ${plural(count, "preference")} from Harness` : ""}`
|
|
130
|
+
);
|
|
131
|
+
return profile;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The profile as an agent is told it at the start of a run; empty without one. */
|
|
135
|
+
export function profileBrief(profile = readProfile()) {
|
|
136
|
+
if (!profile?.displayName) return "";
|
|
137
|
+
const lines = [
|
|
138
|
+
`You are working for ${profile.displayName}${profile.role ? `, ${profile.role}` : ""}. From their Harness profile:`,
|
|
139
|
+
];
|
|
140
|
+
if (profile.communicationStyle)
|
|
141
|
+
lines.push(`- They prefer ${profile.communicationStyle} communication.`);
|
|
142
|
+
const hard = profile.hardPreferences ?? [];
|
|
143
|
+
const soft = profile.softPreferences ?? [];
|
|
144
|
+
if (hard.length)
|
|
145
|
+
lines.push("Always follow:", ...hard.map((item) => `- ${item}`));
|
|
146
|
+
if (soft.length)
|
|
147
|
+
lines.push(
|
|
148
|
+
"When in doubt, they prefer:",
|
|
149
|
+
...soft.map((item) => `- ${item}`)
|
|
150
|
+
);
|
|
151
|
+
return lines.join("\n");
|
|
152
|
+
}
|
|
@@ -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
|
+
}
|