tryharness 0.7.45 → 0.7.54
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 +239 -14
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +497 -196
- package/lib/actorRoster.mjs +28 -5
- package/lib/adapters.mjs +165 -222
- package/lib/agentRuntimeConfig.mjs +36 -0
- package/lib/agentRuntimes.mjs +570 -0
- package/lib/agentSignIn.mjs +226 -0
- package/lib/atlas.mjs +7 -2
- package/lib/audit.mjs +643 -0
- package/lib/backupSetup.mjs +141 -0
- package/lib/boardAccess.mjs +8 -1
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardCommands.mjs +5 -10
- package/lib/boardFileCommands.mjs +2 -1
- package/lib/boardMirror.mjs +523 -0
- package/lib/boardProtocol.json +263 -6
- package/lib/boardTools.mjs +2 -6
- package/lib/boardWaits.mjs +102 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +26 -4
- package/lib/browserWake.mjs +60 -0
- package/lib/cards.mjs +159 -16
- package/lib/cloudBoot.mjs +124 -0
- package/lib/commands.mjs +25 -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/context.mjs +18 -4
- package/lib/contractGate.mjs +377 -0
- package/lib/coordinator.mjs +152 -39
- package/lib/coreModels.mjs +78 -0
- package/lib/daemon.mjs +973 -54
- package/lib/desk.mjs +239 -0
- package/lib/desktop.mjs +113 -8
- package/lib/digest.mjs +32 -0
- package/lib/directLink.mjs +283 -0
- package/lib/environments.mjs +825 -0
- package/lib/folders.mjs +4 -4
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +58 -14
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +263 -152
- package/lib/landed.mjs +887 -0
- package/lib/lessons.mjs +6 -0
- package/lib/lineup.mjs +324 -0
- package/lib/live.mjs +284 -13
- 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 +189 -120
- package/lib/mcpRelay.mjs +119 -0
- package/lib/members.json +26 -0
- package/lib/members.mjs +33 -0
- package/lib/modelMemory.mjs +100 -0
- package/lib/models.mjs +48 -15
- package/lib/museSession.mjs +122 -0
- package/lib/nativeHost.mjs +203 -0
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +7 -2
- package/lib/nudge.mjs +57 -18
- package/lib/onboard.mjs +430 -1
- package/lib/oneclawLocal.mjs +150 -0
- package/lib/ownMachines.mjs +828 -39
- package/lib/pairing.mjs +182 -0
- package/lib/paths.mjs +8 -0
- package/lib/personModel.mjs +89 -0
- package/lib/phones.mjs +119 -3
- package/lib/placeCommands.mjs +27 -0
- package/lib/places.mjs +337 -9
- package/lib/plans.mjs +151 -27
- package/lib/profile.mjs +152 -0
- package/lib/projects.mjs +624 -137
- package/lib/readiness.mjs +400 -0
- package/lib/record.mjs +115 -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 +37 -9
- package/lib/runner.mjs +74 -11
- 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 +386 -96
- package/lib/sessionIndex.mjs +5 -3
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +58 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/store.mjs +829 -29
- package/lib/subagents.mjs +8 -4
- package/lib/swarm.mjs +483 -37
- package/lib/swarmMemory.mjs +947 -0
- package/lib/t3.mjs +56 -17
- package/lib/taskCommands.mjs +91 -13
- package/lib/taskRuns.mjs +68 -11
- package/lib/team.json +253 -144
- package/lib/team.mjs +538 -147
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +78 -12
- package/lib/threadView.mjs +66 -16
- 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/lib/worktrees.mjs +51 -13
- package/package.json +10 -5
package/lib/pairing.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { loadMachine, saveMachine } from "./machineIdentity.mjs";
|
|
5
|
+
import { newPairCode, pairClaim } from "./machineSeal.mjs";
|
|
6
|
+
|
|
7
|
+
// Connecting a computer to the person's Harness account (src/server/bridge/pairHub.ts is the other end): setup asks
|
|
8
|
+
// Harness to hold a pairing, shows the code, and waits on one held stream until the person approves it, which releases
|
|
9
|
+
// this computer's connection token down that stream.
|
|
10
|
+
//
|
|
11
|
+
// Setup makes the code itself, and it is one code for the whole join (machineSeal.mjs): approving it in a signed-in
|
|
12
|
+
// browser connects this computer to the account, and typing it on a computer already connected (the buddy's Add a
|
|
13
|
+
// computer) also brings it into the swarm, because the daemon here takes it as its join code. Harness hears only the
|
|
14
|
+
// code's first 4 characters and proof of the whole. A Harness from before this takes neither and makes a code of its
|
|
15
|
+
// own (an 8-character one, for the account only).
|
|
16
|
+
//
|
|
17
|
+
// Once connected, a computer Harness added to an account that already has a home is told where to type its code, and
|
|
18
|
+
// works on its own meanwhile (joinSwarm).
|
|
19
|
+
|
|
20
|
+
/** Ask Harness to hold a pairing for this computer. Returns { pairId, code, own } (own: setup made the code). */
|
|
21
|
+
export async function startPairing(origin, agents) {
|
|
22
|
+
// The name the computer goes by everywhere else (machineIdentity.mjs).
|
|
23
|
+
const hostname = os
|
|
24
|
+
.hostname()
|
|
25
|
+
.replace(/\.local$/, "")
|
|
26
|
+
.slice(0, 64);
|
|
27
|
+
const start = (body) =>
|
|
28
|
+
fetch(origin + "/api/pair/start", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: { "content-type": "application/json" },
|
|
31
|
+
body: JSON.stringify(body),
|
|
32
|
+
signal: AbortSignal.timeout(15_000),
|
|
33
|
+
});
|
|
34
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
35
|
+
const code = newPairCode();
|
|
36
|
+
const response = await start({ hostname, agents, ...pairClaim(code) });
|
|
37
|
+
// Another computer's setup holds a code with the same first 4 characters.
|
|
38
|
+
if (response.status === 409) continue;
|
|
39
|
+
if (response.status === 400) break;
|
|
40
|
+
if (!response.ok)
|
|
41
|
+
throw new Error(`pairing unavailable (HTTP ${response.status})`);
|
|
42
|
+
const { pairId } = await response.json();
|
|
43
|
+
return { pairId, code, own: true };
|
|
44
|
+
}
|
|
45
|
+
const response = await start({ hostname, agents });
|
|
46
|
+
if (!response.ok)
|
|
47
|
+
throw new Error(`pairing unavailable (HTTP ${response.status})`);
|
|
48
|
+
const { pairId, code } = await response.json();
|
|
49
|
+
return { pairId, code, own: false };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Keep setup's code for the daemon, which takes it as this computer's join code. */
|
|
53
|
+
export function keepJoinCode(machineFile, code) {
|
|
54
|
+
saveMachine(machineFile, { ...loadMachine(machineFile), joinCode: code });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Wait for the person's approval. Resolves with the connection token; rejects on expiry or a dropped stream. */
|
|
58
|
+
export async function waitForApproval(origin, pairId) {
|
|
59
|
+
const response = await fetch(origin + "/api/pair/wait/" + pairId, {
|
|
60
|
+
headers: { accept: "text/event-stream" },
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok || !response.body) throw new Error("pairing stream failed");
|
|
63
|
+
const reader = response.body.getReader();
|
|
64
|
+
const decoder = new TextDecoder();
|
|
65
|
+
let buffer = "";
|
|
66
|
+
for (;;) {
|
|
67
|
+
const { done, value } = await reader.read();
|
|
68
|
+
if (done) break;
|
|
69
|
+
buffer += decoder.decode(value, { stream: true });
|
|
70
|
+
const frames = buffer.split("\n\n");
|
|
71
|
+
buffer = frames.pop() || "";
|
|
72
|
+
for (const frame of frames) {
|
|
73
|
+
const line = frame
|
|
74
|
+
.split("\n")
|
|
75
|
+
.find((entry) => entry.startsWith("data: "));
|
|
76
|
+
if (!line) continue;
|
|
77
|
+
const payload = JSON.parse(line.slice(6));
|
|
78
|
+
if (payload.token) return payload.token;
|
|
79
|
+
if (payload.error) throw new Error(payload.error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
throw new Error("pairing stream ended without approval");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A computer that runs on a battery sleeps with its lid; one without stays on, and the board is reachable only while
|
|
86
|
+
// the computer holding it is on (ownMachines.mjs). Unknown reads as a battery, so nothing is suggested.
|
|
87
|
+
export function hasBattery() {
|
|
88
|
+
if (process.platform === "darwin")
|
|
89
|
+
return /InternalBattery/.test(
|
|
90
|
+
spawnSync("pmset", ["-g", "batt"], { encoding: "utf8" }).stdout || ""
|
|
91
|
+
);
|
|
92
|
+
if (process.platform === "linux") {
|
|
93
|
+
try {
|
|
94
|
+
return fs
|
|
95
|
+
.readdirSync("/sys/class/power_supply")
|
|
96
|
+
.some((name) => name.startsWith("BAT"));
|
|
97
|
+
} catch {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// How long setup waits, at its end, for the person to type this computer's code on home. It works on its own meanwhile,
|
|
105
|
+
// so the wait only lets setup say it joined; the person may close it.
|
|
106
|
+
const JOIN_WAIT_MS = 10 * 60_000;
|
|
107
|
+
// How long a computer approved from home's buddy usually takes to be confirmed there, before setup says where to type.
|
|
108
|
+
const JOIN_QUIET_MS = 8_000;
|
|
109
|
+
|
|
110
|
+
const names = (labels) =>
|
|
111
|
+
labels.length > 1
|
|
112
|
+
? `${labels.slice(0, -1).join(", ")} and ${labels.at(-1)}`
|
|
113
|
+
: labels[0];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* At the end of setup, once the daemon is connected: Harness has made this computer home or added it. An added one not
|
|
117
|
+
* confirmed yet works on its own board until the person types its code on home (ownMachines.mjs); setup says where and
|
|
118
|
+
* waits for it, then may offer home to a computer that suits that better. On home, the computers that still need their
|
|
119
|
+
* code typed here are named. Returns the role. `daemonRequest` reaches this computer's daemon.
|
|
120
|
+
*/
|
|
121
|
+
export async function joinSwarm({
|
|
122
|
+
ui,
|
|
123
|
+
code,
|
|
124
|
+
daemonRequest,
|
|
125
|
+
battery = hasBattery,
|
|
126
|
+
quietMs = JOIN_QUIET_MS,
|
|
127
|
+
waitMs = JOIN_WAIT_MS,
|
|
128
|
+
}) {
|
|
129
|
+
const read = () =>
|
|
130
|
+
daemonRequest({ command: "machines.list" }).catch(() => null);
|
|
131
|
+
let listed = await read();
|
|
132
|
+
if (!listed?.ok) return "home";
|
|
133
|
+
if (listed.self.role !== "worker") {
|
|
134
|
+
const waiting = listed.machines
|
|
135
|
+
.filter((machine) => machine.role === "worker" && !machine.confirmed)
|
|
136
|
+
.map((machine) => machine.label);
|
|
137
|
+
if (waiting.length)
|
|
138
|
+
ui.info(
|
|
139
|
+
`${names(waiting)} ${waiting.length > 1 ? "are" : "is"} on your account but not in your swarm here yet, and ${waiting.length > 1 ? "each works" : "works"} on its own meanwhile. To bring ${waiting.length > 1 ? "each" : "it"} in, run npx tryharness machines there and type the code it shows here: Add a Computer in the Harness menu.`
|
|
140
|
+
);
|
|
141
|
+
return "home";
|
|
142
|
+
}
|
|
143
|
+
const homeName = () =>
|
|
144
|
+
listed.machines.find((machine) => machine.role === "home")?.label ||
|
|
145
|
+
"your home computer";
|
|
146
|
+
if (listed.joinCode) {
|
|
147
|
+
const spin = ui.spinner();
|
|
148
|
+
spin.start(`Joining your swarm on ${homeName()}`);
|
|
149
|
+
const started = Date.now();
|
|
150
|
+
let told = false;
|
|
151
|
+
while (listed?.joinCode && Date.now() - started < waitMs) {
|
|
152
|
+
if (!told && Date.now() - started > quietMs) {
|
|
153
|
+
told = true;
|
|
154
|
+
spin.message(
|
|
155
|
+
`Waiting for ${homeName()}. There, click the Harness icon in the menu bar, choose Add a Computer, and type ${listed.joinCode}${listed.joinCode === code ? " (the same code)" : ""}. Or run there: npx tryharness machines add ${listed.joinCode}. This computer already works on its own, so you can close this.`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
await new Promise((resolve) => setTimeout(resolve, 1_000));
|
|
159
|
+
listed = (await read()) ?? listed;
|
|
160
|
+
}
|
|
161
|
+
if (listed.joinCode) {
|
|
162
|
+
spin.stop(
|
|
163
|
+
`This computer works on its own until it joins your swarm. To bring it in, type ${listed.joinCode} on ${homeName()}: Add a Computer in the Harness menu there, or npx tryharness machines add ${listed.joinCode}. What it does meanwhile goes to ${homeName()} when it joins.`
|
|
164
|
+
);
|
|
165
|
+
return "worker";
|
|
166
|
+
}
|
|
167
|
+
spin.stop(`Joined your swarm. ${homeName()} holds the board.`);
|
|
168
|
+
} else ui.success(`In your swarm. ${homeName()} holds the board.`);
|
|
169
|
+
if (!ui.interactive || battery()) return "worker";
|
|
170
|
+
const move = await ui.confirm({
|
|
171
|
+
message: `This computer stays on, so work could go on here while ${homeName()} sleeps. Make it your main computer? Your threads and board come with it.`,
|
|
172
|
+
initialValue: false,
|
|
173
|
+
});
|
|
174
|
+
if (!move) return "worker";
|
|
175
|
+
const moved = await daemonRequest({ command: "machines.home" }, 20_000);
|
|
176
|
+
if (!moved.ok) {
|
|
177
|
+
ui.warn(`Not moved: ${moved.error}`);
|
|
178
|
+
return "worker";
|
|
179
|
+
}
|
|
180
|
+
ui.success("This is your main computer now.");
|
|
181
|
+
return "home";
|
|
182
|
+
}
|
package/lib/paths.mjs
CHANGED
|
@@ -18,7 +18,15 @@ export const paths = {
|
|
|
18
18
|
: path.join(harnessHome, "daemon.sock"),
|
|
19
19
|
log: path.join(harnessHome, "daemon.log"),
|
|
20
20
|
phoneIdentity: path.join(harnessHome, "phone-identity.json"),
|
|
21
|
+
// The browser link setup makes for the page that approves this computer (phones.mjs writeBrowserOffer).
|
|
22
|
+
browserOffer: path.join(harnessHome, "browser-offer.json"),
|
|
21
23
|
machine: path.join(harnessHome, "machine.json"),
|
|
24
|
+
// The person's name and preferences from their Harness profile (profile.mjs).
|
|
25
|
+
profile: path.join(harnessHome, "profile.json"),
|
|
26
|
+
// The terminal app setup last ran in (places.mjs noteSetupTerminal).
|
|
27
|
+
terminal: path.join(harnessHome, "terminal.json"),
|
|
28
|
+
// A cloud computer's seeded link offer (cloudBoot.mjs); absent everywhere else.
|
|
29
|
+
cloud: path.join(harnessHome, "cloud.json"),
|
|
22
30
|
runs: path.join(harnessHome, "runs"),
|
|
23
31
|
files: path.join(harnessHome, "files"),
|
|
24
32
|
runtime: path.join(harnessHome, "runtime"),
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
// The person's own language model on this computer, when they run one for pi (its models.json, in PI_CODING_AGENT_DIR
|
|
6
|
+
// or ~/.pi/agent): an OpenAI-compatible server on this machine. Harness uses it only for small labels of the person's
|
|
7
|
+
// own words (threadLine.mjs), so nothing they wrote leaves the computer for it. A provider anywhere else is not theirs
|
|
8
|
+
// to lend: it is never used.
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How long one label may take. Measured on the owner's M5 Pro with Flint (a reasoning model on llama.cpp), 80 labels of
|
|
12
|
+
* real thread messages, October 23, while e2e suites ran beside it: median 18.6 s and 22.0 s in two rounds, slowest
|
|
13
|
+
* 48.6 s. 90 s leaves about 1.85x over the slowest; a label is made once, in the background, so waiting costs nothing.
|
|
14
|
+
*/
|
|
15
|
+
const LABEL_TIMEOUT_MS = 90_000;
|
|
16
|
+
const LOCAL_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
17
|
+
|
|
18
|
+
const modelsFile = () =>
|
|
19
|
+
path.join(
|
|
20
|
+
process.env.PI_CODING_AGENT_DIR || path.join(os.homedir(), ".pi", "agent"),
|
|
21
|
+
"models.json"
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/** The first model pi lists on a server on this computer, or null. */
|
|
25
|
+
export function localModel(file = modelsFile()) {
|
|
26
|
+
let config;
|
|
27
|
+
try {
|
|
28
|
+
config = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
for (const provider of Object.values(config?.providers ?? {})) {
|
|
33
|
+
let url;
|
|
34
|
+
try {
|
|
35
|
+
url = new URL(provider.baseUrl);
|
|
36
|
+
} catch {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const model = provider.models?.[0]?.id;
|
|
40
|
+
if (!LOCAL_HOSTS.has(url.hostname) || !model) continue;
|
|
41
|
+
return {
|
|
42
|
+
baseUrl: provider.baseUrl.replace(/\/$/, ""),
|
|
43
|
+
model,
|
|
44
|
+
apiKey: provider.apiKey || "local",
|
|
45
|
+
reasoning: provider.models[0].reasoning === true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Asks the person's local model a short question and returns its JSON answer, or null when there is no local model,
|
|
53
|
+
* it does not answer in time, or what it says is not JSON.
|
|
54
|
+
*/
|
|
55
|
+
export async function askLocal(system, user, { fetchImpl = fetch } = {}) {
|
|
56
|
+
const local = localModel();
|
|
57
|
+
if (!local) return null;
|
|
58
|
+
try {
|
|
59
|
+
const res = await fetchImpl(`${local.baseUrl}/chat/completions`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: {
|
|
62
|
+
"content-type": "application/json",
|
|
63
|
+
authorization: `Bearer ${local.apiKey}`,
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify({
|
|
66
|
+
model: local.model,
|
|
67
|
+
temperature: 0,
|
|
68
|
+
// A reasoning model thinks before it answers: room for both, and as little thinking as it allows.
|
|
69
|
+
max_tokens: local.reasoning ? 3000 : 200,
|
|
70
|
+
...(local.reasoning
|
|
71
|
+
? { chat_template_kwargs: { reasoning_effort: "low" } }
|
|
72
|
+
: {}),
|
|
73
|
+
messages: [
|
|
74
|
+
{ role: "system", content: system },
|
|
75
|
+
{ role: "user", content: user },
|
|
76
|
+
],
|
|
77
|
+
}),
|
|
78
|
+
signal: AbortSignal.timeout(LABEL_TIMEOUT_MS),
|
|
79
|
+
});
|
|
80
|
+
if (!res.ok) return null;
|
|
81
|
+
const body = await res.json();
|
|
82
|
+
const text = String(body.choices?.[0]?.message?.content ?? "")
|
|
83
|
+
.replace(/^```(?:json)?|```$/g, "")
|
|
84
|
+
.trim();
|
|
85
|
+
return JSON.parse(text);
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
package/lib/phones.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import os from "node:os";
|
|
2
|
+
import fs from "node:fs";
|
|
2
3
|
import { randomBytes } from "node:crypto";
|
|
4
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
3
5
|
import { boardCommandsFor } from "./boardProtocol.mjs";
|
|
6
|
+
import { isPairCode, normalizeJoinCode } from "./machineSeal.mjs";
|
|
4
7
|
import {
|
|
5
8
|
b64u,
|
|
6
9
|
channelKey,
|
|
@@ -36,22 +39,43 @@ const BROWSER_PERSON_COMMANDS = boardCommandsFor("browserPerson");
|
|
|
36
39
|
// back, and only as the person, never in a call Harness's chat makes.
|
|
37
40
|
const LANE_COMMANDS = boardCommandsFor("browserLane");
|
|
38
41
|
const BROWSER_READS = boardCommandsFor("browserReads");
|
|
42
|
+
// The person's linked window adds a computer to the swarm (docs/cloud-swarm.md §5.5): how a cloud worker joins, with
|
|
43
|
+
// home pinning its key exactly as for a code the person typed here. Only as the person, never in a Harness chat call.
|
|
44
|
+
const MACHINE_COMMANDS = boardCommandsFor("browserMachines");
|
|
45
|
+
// The person's window signs a cloud computer's agents in (agentSignIn.mjs): only there, and only as the person.
|
|
46
|
+
const SIGN_IN_COMMANDS = boardCommandsFor("browserSignIn");
|
|
39
47
|
|
|
40
48
|
export async function createPhoneDesk({
|
|
41
49
|
store,
|
|
42
50
|
identityFile,
|
|
51
|
+
// A browser link setup made before this daemon ran (writeBrowserOffer). The file stays until the offer is used or
|
|
52
|
+
// expires, so a daemon that restarts in between (setup's own, an update, the service's keep-alive) still has it.
|
|
53
|
+
setupOfferFile = null,
|
|
43
54
|
boardCommand,
|
|
44
55
|
reads = {},
|
|
45
56
|
version,
|
|
46
57
|
onMessage = () => {},
|
|
47
58
|
machine = () => null,
|
|
48
59
|
lane = null,
|
|
60
|
+
machines = () => null,
|
|
61
|
+
// A cloud computer's link offer (cloudBoot.mjs): made by Harness, which runs the computer, and kept until the
|
|
62
|
+
// computer is removed, so any of the person's windows can link to it. Every other computer has none.
|
|
63
|
+
seededOffer = null,
|
|
64
|
+
signIn = null,
|
|
49
65
|
}) {
|
|
50
66
|
const identity = await loadOrCreateIdentity(identityFile);
|
|
51
67
|
const offers = new Map();
|
|
52
68
|
const keys = new Map();
|
|
53
69
|
const hostname = os.hostname().slice(0, 64);
|
|
54
70
|
|
|
71
|
+
const setupOffer = setupOfferFile ? readSetupOffer(setupOfferFile) : null;
|
|
72
|
+
if (setupOffer)
|
|
73
|
+
offers.set(setupOffer.offerId, {
|
|
74
|
+
secret: setupOffer.secret,
|
|
75
|
+
kind: "browser",
|
|
76
|
+
expiresAt: setupOffer.expiresAt,
|
|
77
|
+
});
|
|
78
|
+
|
|
55
79
|
const sweep = () => {
|
|
56
80
|
const now = Date.now();
|
|
57
81
|
for (const [id, offer] of offers)
|
|
@@ -101,9 +125,15 @@ export async function createPhoneDesk({
|
|
|
101
125
|
!KEY.test(String(devicePublicKey))
|
|
102
126
|
)
|
|
103
127
|
return { ok: false, error: "invalid pairing request" };
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
128
|
+
const seeded = seededOffer?.offerId === String(offerId);
|
|
129
|
+
const pending = seeded
|
|
130
|
+
? { secret: seededOffer.secret, kind: "browser" }
|
|
131
|
+
: offers.get(String(offerId));
|
|
132
|
+
// One attempt per code, right or wrong: a guessed proof burns the code. The cloud offer's secret is 256 random bits
|
|
133
|
+
// that only Harness and the person's windows hold, so it is not burned.
|
|
134
|
+
if (!seeded) offers.delete(String(offerId));
|
|
135
|
+
if (setupOffer && String(offerId) === setupOffer.offerId)
|
|
136
|
+
removeFile(setupOfferFile);
|
|
107
137
|
if (!pending)
|
|
108
138
|
return {
|
|
109
139
|
ok: false,
|
|
@@ -239,6 +269,12 @@ export async function createPhoneDesk({
|
|
|
239
269
|
const command = String(callInput?.command || "");
|
|
240
270
|
if (command === "phone.hello")
|
|
241
271
|
return { ok: true, hostname, version, kind: device.kind };
|
|
272
|
+
// A device sends a file's bytes. A path names a file already on this computer, which only an agent here may attach.
|
|
273
|
+
if (command === "board.file.add" && callInput?.path != null)
|
|
274
|
+
return {
|
|
275
|
+
ok: false,
|
|
276
|
+
error: "a phone or browser attaches a file's content, not a path",
|
|
277
|
+
};
|
|
242
278
|
if (device.kind === "browser") {
|
|
243
279
|
if (LANE_COMMANDS.has(command)) {
|
|
244
280
|
if (!lane || callInput.as === "harness")
|
|
@@ -251,6 +287,39 @@ export async function createPhoneDesk({
|
|
|
251
287
|
credentialSource: callInput.credentialSource,
|
|
252
288
|
});
|
|
253
289
|
}
|
|
290
|
+
if (SIGN_IN_COMMANDS.has(command)) {
|
|
291
|
+
if (!signIn || callInput.as === "harness")
|
|
292
|
+
return { ok: false, error: `a browser cannot call ${command} here` };
|
|
293
|
+
try {
|
|
294
|
+
if (command === "agents.signin.status") return signIn.status();
|
|
295
|
+
if (command === "agents.signin.start")
|
|
296
|
+
return signIn.start(String(callInput.agent || ""));
|
|
297
|
+
if (command === "agents.signin.read")
|
|
298
|
+
return signIn.read(callInput.id);
|
|
299
|
+
if (command === "agents.signin.send")
|
|
300
|
+
return signIn.send(callInput.id, callInput.text);
|
|
301
|
+
if (command === "agents.signin.key")
|
|
302
|
+
return signIn.useKey(String(callInput.agent || ""), callInput.key);
|
|
303
|
+
return signIn.stop(callInput.id);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
return { ok: false, error: error.message };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (MACHINE_COMMANDS.has(command)) {
|
|
309
|
+
const own = machines();
|
|
310
|
+
if (!own || callInput.as === "harness")
|
|
311
|
+
return { ok: false, error: `a browser cannot call ${command} here` };
|
|
312
|
+
if (command === "machines.list") return own.list();
|
|
313
|
+
// Only a join code, which confirms a computer already connected to the account and waiting to join; setup's
|
|
314
|
+
// 16-character code would also release a new computer's connection, which stays the buddy's and the CLI's.
|
|
315
|
+
const code = normalizeJoinCode(callInput.code);
|
|
316
|
+
if (!code || isPairCode(code))
|
|
317
|
+
return {
|
|
318
|
+
ok: false,
|
|
319
|
+
error: "a browser adds a computer only with its join code",
|
|
320
|
+
};
|
|
321
|
+
return own.add(code);
|
|
322
|
+
}
|
|
254
323
|
if (BROWSER_READS.has(command)) {
|
|
255
324
|
const read = reads[command];
|
|
256
325
|
if (!read)
|
|
@@ -298,6 +367,12 @@ export async function createPhoneDesk({
|
|
|
298
367
|
agent: as,
|
|
299
368
|
// A Harness window's own call, which daemon.mjs checks before it takes a task given to Harness.
|
|
300
369
|
fromWindow: as === "harness",
|
|
370
|
+
// A connected app's or a secret's call from Harness's window is hosted, whatever the browser says: grants
|
|
371
|
+
// that keep an app's content or a secret's use on this computer stay here (connectors.mjs, vault.mjs).
|
|
372
|
+
...(as === "harness" &&
|
|
373
|
+
/^board\.(connector|vault)\./.test(String(command))
|
|
374
|
+
? { hostedCaller: true }
|
|
375
|
+
: {}),
|
|
301
376
|
...(writtenBy.length
|
|
302
377
|
? { provenance: { trust: "agent", chain: writtenBy } }
|
|
303
378
|
: {}),
|
|
@@ -413,3 +488,44 @@ export async function createPhoneDesk({
|
|
|
413
488
|
identity,
|
|
414
489
|
};
|
|
415
490
|
}
|
|
491
|
+
|
|
492
|
+
// Setup's browser link (cli.mjs): the page that approves this computer links the same browser in the same step, so
|
|
493
|
+
// setup makes the offer itself, before the daemon runs, and puts it in that page's address fragment with the pairing
|
|
494
|
+
// code. The daemon takes it in when it starts. Same secret, key, ten minutes and one use as a link the daemon makes.
|
|
495
|
+
export async function writeBrowserOffer({ identityFile, offerFile }) {
|
|
496
|
+
const identity = await loadOrCreateIdentity(identityFile);
|
|
497
|
+
const offer = {
|
|
498
|
+
offerId: b64u(randomBytes(16)),
|
|
499
|
+
secret: b64u(randomBytes(32)),
|
|
500
|
+
expiresAt: Date.now() + OFFER_TTL_MS,
|
|
501
|
+
};
|
|
502
|
+
writeJsonAtomic(offerFile, offer);
|
|
503
|
+
return new URLSearchParams({
|
|
504
|
+
v: "1",
|
|
505
|
+
o: offer.offerId,
|
|
506
|
+
s: offer.secret,
|
|
507
|
+
k: identity.publicKey,
|
|
508
|
+
// The name the computer goes by everywhere else (pairing.mjs, machineIdentity.mjs).
|
|
509
|
+
h: os
|
|
510
|
+
.hostname()
|
|
511
|
+
.replace(/\.local$/, "")
|
|
512
|
+
.slice(0, 64),
|
|
513
|
+
}).toString();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function removeFile(file) {
|
|
517
|
+
try {
|
|
518
|
+
fs.unlinkSync(file);
|
|
519
|
+
} catch {}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function readSetupOffer(file) {
|
|
523
|
+
const offer = readJson(file, null);
|
|
524
|
+
const usable =
|
|
525
|
+
offer &&
|
|
526
|
+
ID.test(String(offer.offerId)) &&
|
|
527
|
+
typeof offer.secret === "string" &&
|
|
528
|
+
offer.expiresAt > Date.now();
|
|
529
|
+
if (!usable) removeFile(file);
|
|
530
|
+
return usable ? offer : null;
|
|
531
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Places on the person's computer: the desktop buddy reaches the outside networks its swarm is in through this
|
|
2
|
+
// daemon, which asks Harness over its bridge (`place_op`, src/server/places/places.ts). Harness holds each place's key
|
|
3
|
+
// and makes every request; this side only carries the person's ops. Only the person ("user") calls these: agents
|
|
4
|
+
// use the `place` tool, which Harness answers itself.
|
|
5
|
+
|
|
6
|
+
export function createPlaceCommands({ askHarness }) {
|
|
7
|
+
const command = (request, agent) => {
|
|
8
|
+
if (agent !== "user")
|
|
9
|
+
return { ok: false, error: "only the person manages places from here" };
|
|
10
|
+
if (request.command !== "board.place.op")
|
|
11
|
+
return { ok: false, error: `unknown command ${request.command}` };
|
|
12
|
+
const { command: _command, agent: _agent, ...input } = request;
|
|
13
|
+
return {
|
|
14
|
+
pending: askHarness("place_op", input).then(
|
|
15
|
+
(answer) =>
|
|
16
|
+
answer.ok
|
|
17
|
+
? answer
|
|
18
|
+
: {
|
|
19
|
+
ok: false,
|
|
20
|
+
error: answer.error || "Harness refused the request",
|
|
21
|
+
},
|
|
22
|
+
(error) => ({ ok: false, error: error.message })
|
|
23
|
+
),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
return { command };
|
|
27
|
+
}
|