@beatos/bridge-runner 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +403 -2
- package/dist/agent-reply.js +133 -0
- package/dist/api.js +138 -0
- package/dist/brief.js +71 -0
- package/dist/claude.js +176 -0
- package/dist/config.js +72 -0
- package/dist/main.js +138 -0
- package/dist/outbox.js +109 -0
- package/dist/pair.js +177 -0
- package/dist/policy.js +25 -0
- package/dist/runner.js +762 -0
- package/dist/safety.js +201 -0
- package/dist/workspace.js +161 -0
- package/package.json +53 -4
package/dist/outbox.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable outbox for one run (the PRP v1 idea, BEA-85).
|
|
3
|
+
*
|
|
4
|
+
* Every event is written to disk (append + fsync) BEFORE it is sent, with a
|
|
5
|
+
* seq number one higher than the last. The server answers with the highest
|
|
6
|
+
* seq it applied (ack). Events up to the ack are dropped from the file; the
|
|
7
|
+
* rest are sent again after a crash or a reconnect. The server skips seqs it
|
|
8
|
+
* already has, so nothing is lost and nothing is applied twice.
|
|
9
|
+
*
|
|
10
|
+
* File: <dir>/<runId>.jsonl, one event per line, plus <runId>.ack holding the
|
|
11
|
+
* last ack. Small and boring on purpose: a runner sends a few events a second.
|
|
12
|
+
*/
|
|
13
|
+
import { appendFileSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
export const MAX_BATCH = 200;
|
|
16
|
+
export class Outbox {
|
|
17
|
+
file;
|
|
18
|
+
ackFile;
|
|
19
|
+
pending = [];
|
|
20
|
+
lastSeq = 0;
|
|
21
|
+
ack = 0;
|
|
22
|
+
constructor(dir, runId) {
|
|
23
|
+
mkdirSync(dir, { recursive: true });
|
|
24
|
+
this.file = join(dir, `${runId}.jsonl`);
|
|
25
|
+
this.ackFile = join(dir, `${runId}.ack`);
|
|
26
|
+
this.load();
|
|
27
|
+
}
|
|
28
|
+
load() {
|
|
29
|
+
if (existsSync(this.ackFile))
|
|
30
|
+
this.ack = Number(readFileSync(this.ackFile, "utf8").trim()) || 0;
|
|
31
|
+
this.lastSeq = this.ack;
|
|
32
|
+
if (!existsSync(this.file))
|
|
33
|
+
return;
|
|
34
|
+
for (const line of readFileSync(this.file, "utf8").split("\n")) {
|
|
35
|
+
if (!line.trim())
|
|
36
|
+
continue;
|
|
37
|
+
try {
|
|
38
|
+
const ev = JSON.parse(line);
|
|
39
|
+
if (ev.seq > this.ack)
|
|
40
|
+
this.pending.push(ev);
|
|
41
|
+
if (ev.seq > this.lastSeq)
|
|
42
|
+
this.lastSeq = ev.seq;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// A torn last line from a crash mid-write: it was never acked or sent
|
|
46
|
+
// as complete JSON, so dropping it loses nothing the server has.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
this.pending.sort((a, b) => a.seq - b.seq);
|
|
50
|
+
}
|
|
51
|
+
get ackSeq() {
|
|
52
|
+
return this.ack;
|
|
53
|
+
}
|
|
54
|
+
get size() {
|
|
55
|
+
return this.pending.length;
|
|
56
|
+
}
|
|
57
|
+
/** Persist one event (fsync) and return its seq. */
|
|
58
|
+
append(type, data) {
|
|
59
|
+
const ev = { seq: this.lastSeq + 1, type, data };
|
|
60
|
+
const fd = openSync(this.file, "a");
|
|
61
|
+
try {
|
|
62
|
+
appendFileSync(fd, JSON.stringify(ev) + "\n", "utf8");
|
|
63
|
+
fsyncSync(fd);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
closeSync(fd);
|
|
67
|
+
}
|
|
68
|
+
this.lastSeq = ev.seq;
|
|
69
|
+
this.pending.push(ev);
|
|
70
|
+
return ev.seq;
|
|
71
|
+
}
|
|
72
|
+
/** The server's cursor is the truth after a reconnect: adopt it. */
|
|
73
|
+
adoptServerAck(serverAck) {
|
|
74
|
+
if (serverAck > this.ack)
|
|
75
|
+
this.markAcked(serverAck);
|
|
76
|
+
}
|
|
77
|
+
markAcked(seq) {
|
|
78
|
+
this.ack = seq;
|
|
79
|
+
this.pending = this.pending.filter((e) => e.seq > seq);
|
|
80
|
+
const tmp = this.ackFile + ".tmp";
|
|
81
|
+
writeFileSync(tmp, String(seq), "utf8");
|
|
82
|
+
renameSync(tmp, this.ackFile);
|
|
83
|
+
// Compact the log so it only keeps unsent events.
|
|
84
|
+
const tmpLog = this.file + ".tmp";
|
|
85
|
+
writeFileSync(tmpLog, this.pending.map((e) => JSON.stringify(e) + "\n").join(""), "utf8");
|
|
86
|
+
renameSync(tmpLog, this.file);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Send everything pending, in batches. Returns false when the lease is
|
|
90
|
+
* stale (this attempt must stop). Throws on network errors: the events stay
|
|
91
|
+
* on disk and the next flush sends them again.
|
|
92
|
+
*/
|
|
93
|
+
async flush(send) {
|
|
94
|
+
let guard = 0;
|
|
95
|
+
while (this.pending.length > 0 && guard++ < 1000) {
|
|
96
|
+
const batch = this.pending.slice(0, MAX_BATCH);
|
|
97
|
+
const res = await send(batch);
|
|
98
|
+
if (res.staleLease)
|
|
99
|
+
return false;
|
|
100
|
+
if (res.resendFrom !== undefined && res.resendFrom - 1 > this.ack)
|
|
101
|
+
this.markAcked(res.resendFrom - 1);
|
|
102
|
+
if (res.ackSeq > this.ack)
|
|
103
|
+
this.markAcked(res.ackSeq);
|
|
104
|
+
else if (res.resendFrom === undefined)
|
|
105
|
+
break; // no progress and no hint: try again later
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
package/dist/pair.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `npx @beatos/bridge-runner pair <CODE> [--name "Laptop"] [--url https://beatos.app]`
|
|
3
|
+
* (BEA-88): connect this computer with the one-time code from beatos.app
|
|
4
|
+
* (Bridge, Runners, Connect a computer).
|
|
5
|
+
*
|
|
6
|
+
* Sends the code to the claim route, which answers with a NEW agent key for
|
|
7
|
+
* this runner only. The key goes straight to ~/.beatos-runner/runner.key with
|
|
8
|
+
* owner-only permissions (chmod 600; on Windows icacls keeps only the current
|
|
9
|
+
* user, best effort) and is NEVER printed or logged. config.json gets the
|
|
10
|
+
* runner name, workspace and agent the server registered, so the next
|
|
11
|
+
* heartbeat finds the same runner row. Without --name, BeatOS names the
|
|
12
|
+
* runner "Local-AI-" plus the start of its id; without an agentName in
|
|
13
|
+
* config.json, the key works for BeatOS's default agent ("local-ai").
|
|
14
|
+
*/
|
|
15
|
+
import { spawnSync } from "node:child_process";
|
|
16
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { defaultStateDir, loadConfig } from "./config.js";
|
|
19
|
+
export const PAIR_USAGE = 'Usage: npx @beatos/bridge-runner pair <CODE> [--name "Laptop"] [--url https://beatos.app]';
|
|
20
|
+
/** How a user starts the runner after pairing (the npm package, PUBLISHING.md). */
|
|
21
|
+
export const START_COMMAND = "npx @beatos/bridge-runner";
|
|
22
|
+
export function parsePairArgs(argv) {
|
|
23
|
+
let code = "";
|
|
24
|
+
let name;
|
|
25
|
+
let url;
|
|
26
|
+
const value = (i) => {
|
|
27
|
+
const v = argv[i];
|
|
28
|
+
return v === undefined || v === "" || v.startsWith("--") ? null : v;
|
|
29
|
+
};
|
|
30
|
+
for (let i = 0; i < argv.length; i++) {
|
|
31
|
+
const a = argv[i];
|
|
32
|
+
if (a === "--name" || a === "--url") {
|
|
33
|
+
const v = value(++i);
|
|
34
|
+
if (v === null)
|
|
35
|
+
return null;
|
|
36
|
+
if (a === "--name")
|
|
37
|
+
name = v;
|
|
38
|
+
else
|
|
39
|
+
url = v;
|
|
40
|
+
}
|
|
41
|
+
else if (!a.startsWith("--") && !code)
|
|
42
|
+
code = a;
|
|
43
|
+
else
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (!code)
|
|
47
|
+
return null;
|
|
48
|
+
return { code, name, url };
|
|
49
|
+
}
|
|
50
|
+
function icacls(path, user) {
|
|
51
|
+
const r = spawnSync("icacls", [path, "/inheritance:r", "/grant:r", `${user}:F`], {
|
|
52
|
+
windowsHide: true,
|
|
53
|
+
stdio: "ignore",
|
|
54
|
+
});
|
|
55
|
+
return r.status === 0;
|
|
56
|
+
}
|
|
57
|
+
/** Write the key so only the current user can read it. Never echoes it. */
|
|
58
|
+
export function saveKey(file, key, platform, user, restrictWindows) {
|
|
59
|
+
writeFileSync(file, key + "\n", { encoding: "utf8", mode: 0o600 });
|
|
60
|
+
try {
|
|
61
|
+
chmodSync(file, 0o600);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Windows ignores most of chmod; icacls below does the real work there.
|
|
65
|
+
}
|
|
66
|
+
if (platform !== "win32")
|
|
67
|
+
return true;
|
|
68
|
+
if (!user)
|
|
69
|
+
return false;
|
|
70
|
+
try {
|
|
71
|
+
return restrictWindows(file, user);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export async function pair(argv, deps = {}) {
|
|
78
|
+
const out = deps.out ?? ((l) => console.log(l));
|
|
79
|
+
const env = deps.env ?? process.env;
|
|
80
|
+
const args = parsePairArgs(argv);
|
|
81
|
+
if (!args) {
|
|
82
|
+
out(PAIR_USAGE);
|
|
83
|
+
return 2;
|
|
84
|
+
}
|
|
85
|
+
const cfg = loadConfig(env);
|
|
86
|
+
const baseUrl = (args.url ?? cfg.beatosUrl).replace(/\/+$/, "");
|
|
87
|
+
// No --name: BeatOS picks a unique "Local-AI-XXXX" from the new runner id.
|
|
88
|
+
const runnerName = args.name?.trim() || undefined;
|
|
89
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
90
|
+
let res;
|
|
91
|
+
try {
|
|
92
|
+
res = await fetchImpl(`${baseUrl}/api/v1/bridge/pairing/claim`, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
code: args.code,
|
|
97
|
+
...(runnerName ? { runner_name: runnerName } : {}),
|
|
98
|
+
machine: cfg.machine,
|
|
99
|
+
kind: "pc",
|
|
100
|
+
// No agent yet: BeatOS picks the default and answers with its name.
|
|
101
|
+
...(cfg.agentName ? { agent_name: cfg.agentName } : {}),
|
|
102
|
+
...(deps.runnerVersion ? { runner_version: deps.runnerVersion } : {}),
|
|
103
|
+
capabilities: {
|
|
104
|
+
connectors: ["claude"],
|
|
105
|
+
profile: cfg.profile,
|
|
106
|
+
max_runs_per_day: cfg.maxRunsPerDay,
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
signal: AbortSignal.timeout(30_000),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
out(`Could not reach ${baseUrl} (${err.message}). Check the connection and try again.`);
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
116
|
+
if (res.status === 400) {
|
|
117
|
+
out("That code did not work. It may be mistyped, already used or older than 10 minutes.");
|
|
118
|
+
out("Get a new code in BeatOS: Bridge, Runners, Connect a computer.");
|
|
119
|
+
return 1;
|
|
120
|
+
}
|
|
121
|
+
if (res.status === 409) {
|
|
122
|
+
out(runnerName
|
|
123
|
+
? `A runner named "${runnerName}" already exists. Run it again with --name "Another name".`
|
|
124
|
+
: 'BeatOS could not pick a free name. Run it again with --name "Laptop" (any name not used yet).');
|
|
125
|
+
out("Your code is still good for the rest of its 10 minutes.");
|
|
126
|
+
return 1;
|
|
127
|
+
}
|
|
128
|
+
if (res.status === 429) {
|
|
129
|
+
out("Too many tries. Wait 15 minutes, then try again with a new code.");
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
if (res.status !== 201) {
|
|
133
|
+
out(`BeatOS answered ${res.status}. Try again in a minute.`);
|
|
134
|
+
return 1;
|
|
135
|
+
}
|
|
136
|
+
const body = (await res.json());
|
|
137
|
+
if (!body.key || !/^beatos_[0-9a-f]{40}$/.test(body.key)) {
|
|
138
|
+
out("BeatOS sent an answer without a usable key. Get a new code and try again.");
|
|
139
|
+
return 1;
|
|
140
|
+
}
|
|
141
|
+
const stateDir = cfg.stateDir || defaultStateDir();
|
|
142
|
+
mkdirSync(stateDir, { recursive: true });
|
|
143
|
+
const keyFile = join(stateDir, "runner.key");
|
|
144
|
+
const replaced = existsSync(keyFile);
|
|
145
|
+
const platform = deps.platform ?? process.platform;
|
|
146
|
+
const locked = saveKey(keyFile, body.key, platform, env.USERNAME, deps.restrictWindows ?? icacls);
|
|
147
|
+
const configFile = env.BEATOS_RUNNER_CONFIG || join(stateDir, "config.json");
|
|
148
|
+
let raw = {};
|
|
149
|
+
if (existsSync(configFile)) {
|
|
150
|
+
try {
|
|
151
|
+
raw = JSON.parse(readFileSync(configFile, "utf8"));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
out(`${configFile} is not valid JSON; it was left as it is. Set runnerName to "${body.runner_name ?? runnerName}" in it.`);
|
|
155
|
+
raw = {};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const next = {
|
|
159
|
+
...raw,
|
|
160
|
+
runnerName: body.runner_name ?? runnerName ?? cfg.runnerName,
|
|
161
|
+
...(body.workspace_id ? { workspaceId: body.workspace_id } : {}),
|
|
162
|
+
...(body.agent_name ? { agentName: body.agent_name } : {}),
|
|
163
|
+
...(args.url ? { beatosUrl: baseUrl } : {}),
|
|
164
|
+
};
|
|
165
|
+
writeFileSync(configFile, JSON.stringify(next, null, 2) + "\n", "utf8");
|
|
166
|
+
out(`Connected: this computer is the runner "${next.runnerName}".`);
|
|
167
|
+
out(`The key is saved in ${keyFile}${replaced ? " (it replaced the old key file)" : ""}. It is not shown anywhere.`);
|
|
168
|
+
if (platform === "win32" && !locked) {
|
|
169
|
+
out("Could not limit the key file to your Windows user. Check its permissions.");
|
|
170
|
+
}
|
|
171
|
+
if (env.BEATOS_RUNNER_KEY) {
|
|
172
|
+
out("Note: BEATOS_RUNNER_KEY is set in this shell and wins over the key file. Remove it.");
|
|
173
|
+
}
|
|
174
|
+
out(`Next: start the runner with ${START_COMMAND} (keep the terminal open)`);
|
|
175
|
+
out("It shows up as online in BeatOS, Bridge, Runners, within a minute.");
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
package/dist/policy.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the runner does when the agent asks permission (BEA-87).
|
|
3
|
+
*
|
|
4
|
+
* The workspace's rules come from GET /api/v1/bridge/runner/settings:
|
|
5
|
+
* approval (ask the human), free (go ahead without asking) or forbidden
|
|
6
|
+
* (refuse before asking). The FIXED rules are repeated here on purpose: a
|
|
7
|
+
* server answer can never make the runner merge, deploy or delete data
|
|
8
|
+
* without asking, or move money at all. Unknown settings (server down) or a
|
|
9
|
+
* kind the settings do not name: ask.
|
|
10
|
+
*/
|
|
11
|
+
export const FIXED_RULES = {
|
|
12
|
+
merge: "approval",
|
|
13
|
+
deploy: "approval",
|
|
14
|
+
delete_data: "approval",
|
|
15
|
+
spend: "forbidden",
|
|
16
|
+
};
|
|
17
|
+
export function decideAction(kind, policy) {
|
|
18
|
+
const fixed = FIXED_RULES[kind];
|
|
19
|
+
const mode = fixed ?? policy?.[kind] ?? "approval";
|
|
20
|
+
if (mode === "forbidden")
|
|
21
|
+
return "refuse";
|
|
22
|
+
if (mode === "free")
|
|
23
|
+
return "free";
|
|
24
|
+
return "ask";
|
|
25
|
+
}
|