benignswarm 0.12.96 → 0.12.98
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/dist/admin-index.js +0 -0
- package/dist/args.js +40 -3
- package/dist/brief.js +5 -1
- package/dist/client.js +248 -0
- package/dist/commands/admin.js +84 -22
- package/dist/commands/agent.js +15 -2
- package/dist/commands/cloud-admin.js +56 -3
- package/dist/commands/codex-start.js +340 -241
- package/dist/commands/environment-admin.js +130 -0
- package/dist/commands/environment.js +53 -0
- package/dist/commands/evidence.js +6 -3
- package/dist/commands/feedback.js +12 -1
- package/dist/commands/flow.js +153 -0
- package/dist/commands/hook.js +29 -9
- package/dist/commands/image-admin.js +77 -0
- package/dist/commands/install.js +81 -3
- package/dist/commands/lists.js +4 -0
- package/dist/commands/machine-admin.js +444 -111
- package/dist/commands/machine-resize.js +234 -0
- package/dist/commands/machine.js +960 -41
- package/dist/commands/operator.js +5 -8
- package/dist/commands/retire.js +4 -0
- package/dist/commands/templates.js +27 -0
- package/dist/commands/turn.js +21 -0
- package/dist/commands/watch.js +256 -21
- package/dist/commands/wu.js +131 -81
- package/dist/container.js +480 -0
- package/dist/environment-checks.js +79 -0
- package/dist/git.js +28 -0
- package/dist/help.js +94 -37
- package/dist/index.js +7 -1
- package/dist/install.js +43 -1
- package/dist/macos-readiness.js +173 -0
- package/dist/output.js +17 -4
- package/dist/templates.js +218 -0
- package/dist/test-workers.js +17 -0
- package/dist/turn-guard.js +5 -5
- package/dist/turn.js +6 -0
- package/dist/usage.js +117 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { createInterface } from "node:readline/promises";
|
|
5
|
+
import { bool, str } from "../args.js";
|
|
6
|
+
import { CliError } from "../errors.js";
|
|
7
|
+
const cents = (n) => (typeof n === "number" ? `$${(n / 100).toFixed(2)}` : "n/a");
|
|
8
|
+
/** Parse whole minutes from `<n>`, `<n>m`, `<n>h`, or `<n>s` (rounded up for seconds). */
|
|
9
|
+
export function parseMinutesFlag(raw, fallbackMinutes, flagName) {
|
|
10
|
+
if (raw === undefined)
|
|
11
|
+
return fallbackMinutes;
|
|
12
|
+
const m = /^(\d+)(s|m|h)?$/i.exec(raw.trim());
|
|
13
|
+
if (!m)
|
|
14
|
+
throw new CliError("usage", `--${flagName} expects minutes (5m), seconds (60s), or hours (1h)`);
|
|
15
|
+
const n = Number(m[1]);
|
|
16
|
+
const unit = (m[2] ?? "m").toLowerCase();
|
|
17
|
+
const minutes = unit === "h" ? n * 60 : unit === "s" ? Math.ceil(n / 60) : n;
|
|
18
|
+
if (!Number.isInteger(minutes) || minutes < 1 || minutes > 120) {
|
|
19
|
+
throw new CliError("usage", `--${flagName} must resolve to 1..120 minutes`);
|
|
20
|
+
}
|
|
21
|
+
return minutes;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Updates `HostName` inside a matching SSH config `Host` stanza without touching other hosts or
|
|
25
|
+
* requiring inbound SSH rules on AWS security groups.
|
|
26
|
+
*/
|
|
27
|
+
export function updateSshConfigHost(configText, opts) {
|
|
28
|
+
const lines = configText.split(/\r?\n/);
|
|
29
|
+
const aliases = opts.hostAliases.filter(Boolean);
|
|
30
|
+
const stanzas = [];
|
|
31
|
+
for (let i = 0; i < lines.length; i++) {
|
|
32
|
+
const trimmed = lines[i].trim();
|
|
33
|
+
if (/^Host\s+/i.test(trimmed)) {
|
|
34
|
+
const patternPart = trimmed.replace(/^Host\s+/i, "").replace(/#.*$/, "").trim();
|
|
35
|
+
const headerAliases = patternPart.split(/\s+/).filter(Boolean);
|
|
36
|
+
stanzas.push({ start: i, end: lines.length, headerAliases });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (let s = 0; s + 1 < stanzas.length; s++) {
|
|
40
|
+
stanzas[s].end = stanzas[s + 1].start;
|
|
41
|
+
}
|
|
42
|
+
let target = stanzas.find((st) => st.headerAliases.some((a) => !/[*?]/.test(a) && aliases.includes(a)));
|
|
43
|
+
if (!target && opts.previousIp) {
|
|
44
|
+
target = stanzas.find((st) => {
|
|
45
|
+
if (st.headerAliases.every((a) => a === "*"))
|
|
46
|
+
return false;
|
|
47
|
+
for (let i = st.start + 1; i < st.end; i++) {
|
|
48
|
+
const hm = /^\s*HostName\s+([^\s#]+)/i.exec(lines[i]);
|
|
49
|
+
if (hm?.[1] === opts.previousIp)
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (!target)
|
|
56
|
+
return { updated: false, matchedHost: null, content: configText };
|
|
57
|
+
const matchedHost = target.headerAliases.find((a) => aliases.includes(a)) ?? target.headerAliases[0] ?? null;
|
|
58
|
+
let hostNameIdx = -1;
|
|
59
|
+
for (let i = target.start + 1; i < target.end; i++) {
|
|
60
|
+
if (/^\s*HostName\s+/i.test(lines[i])) {
|
|
61
|
+
hostNameIdx = i;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const trailingNewline = configText.endsWith("\n") ? "\n" : "";
|
|
66
|
+
if (hostNameIdx >= 0) {
|
|
67
|
+
const orig = lines[hostNameIdx];
|
|
68
|
+
const replaced = orig.replace(/^(\s*HostName\s+)([^\s#]+)(.*)$/i, `$1${opts.newIp}$3`);
|
|
69
|
+
if (replaced === orig)
|
|
70
|
+
return { updated: false, matchedHost, content: configText };
|
|
71
|
+
lines[hostNameIdx] = replaced;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
lines.splice(target.start + 1, 0, ` HostName ${opts.newIp}`);
|
|
75
|
+
}
|
|
76
|
+
const body = lines.join("\n").replace(/\n+$/, "");
|
|
77
|
+
return { updated: true, matchedHost, content: `${body}${trailingNewline || "\n"}` };
|
|
78
|
+
}
|
|
79
|
+
export function applySshIpUpdate(machineName, sshAlias, previousIp, newIp, sshConfigPath = process.env.B9S_SSH_CONFIG || join(homedir(), ".ssh", "config")) {
|
|
80
|
+
if (!newIp || newIp === previousIp) {
|
|
81
|
+
return {
|
|
82
|
+
status: newIp ? "unchanged" : "hosted",
|
|
83
|
+
message: newIp
|
|
84
|
+
? `Public IP unchanged (${newIp}); no SSH config update needed.`
|
|
85
|
+
: "No public IP assigned (hosted/private VPC); no SSH config or firewall changes needed.",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (!existsSync(sshConfigPath)) {
|
|
89
|
+
return {
|
|
90
|
+
status: "no_entry",
|
|
91
|
+
message: `Public IP changed (${previousIp ?? "none"} -> ${newIp}); no ${sshConfigPath} found (hosted machines need no inbound SSH).`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const existing = readFileSync(sshConfigPath, "utf8");
|
|
95
|
+
const aliases = [sshAlias, machineName].filter((x) => Boolean(x));
|
|
96
|
+
const res = updateSshConfigHost(existing, { hostAliases: aliases, previousIp, newIp });
|
|
97
|
+
if (!res.updated) {
|
|
98
|
+
return {
|
|
99
|
+
status: "no_entry",
|
|
100
|
+
message: `Public IP changed (${previousIp ?? "none"} -> ${newIp}); no matching Host entry (${aliases.join(", ")}) in ${sshConfigPath} (hosted/outbound-only machines need no SSH entry).`,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
mkdirSync(dirname(sshConfigPath), { recursive: true });
|
|
104
|
+
writeFileSync(sshConfigPath, res.content, "utf8");
|
|
105
|
+
return {
|
|
106
|
+
status: "updated",
|
|
107
|
+
matchedHost: res.matchedHost,
|
|
108
|
+
message: `Updated ${sshConfigPath} Host ${res.matchedHost} HostName ${previousIp ?? "(none)"} -> ${newIp} (no inbound SSH rules changed on AWS).`,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async function defaultPrompt(question) {
|
|
112
|
+
if (!process.stdin.isTTY)
|
|
113
|
+
throw new CliError("usage", "non-interactive session requires --yes (and --on-running-turn wait|proceed if a turn outlasts drain timeout)");
|
|
114
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
115
|
+
try {
|
|
116
|
+
return (await rl.question(question)).trim();
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
rl.close();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export async function runMachineResize(c, p, out, machineName, io = {}) {
|
|
123
|
+
const targetType = str(p.flags, "type");
|
|
124
|
+
if (!targetType) {
|
|
125
|
+
throw new CliError("usage", "machine resize <machine> --type <instance-type> [--instance-id <id>] [--drain-timeout 5m] [--yes] [--on-running-turn wait|proceed|abort] [--extend 5m] [--ssh <host>]");
|
|
126
|
+
}
|
|
127
|
+
const instanceId = str(p.flags, "instance-id");
|
|
128
|
+
const drainTimeoutMinutes = parseMinutesFlag(str(p.flags, "drain-timeout"), 5, "drain-timeout");
|
|
129
|
+
const extendMinutes = parseMinutesFlag(str(p.flags, "extend"), 5, "extend");
|
|
130
|
+
const pollMsRaw = str(p.flags, "poll-ms");
|
|
131
|
+
const pollMs = pollMsRaw !== undefined ? Math.max(10, Number(pollMsRaw)) : 2000;
|
|
132
|
+
const onRunningTurn = str(p.flags, "on-running-turn");
|
|
133
|
+
if (onRunningTurn && !["wait", "proceed", "abort"].includes(onRunningTurn)) {
|
|
134
|
+
throw new CliError("usage", "--on-running-turn must be wait, proceed, or abort");
|
|
135
|
+
}
|
|
136
|
+
const yes = bool(p.flags, "yes");
|
|
137
|
+
const noWaitVerify = bool(p.flags, "no-wait-verify");
|
|
138
|
+
const sshAlias = str(p.flags, "ssh");
|
|
139
|
+
const sshConfigPath = str(p.flags, "ssh-config") ?? io.sshConfigPath;
|
|
140
|
+
const prompt = io.prompt ?? defaultPrompt;
|
|
141
|
+
const sleep = io.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
142
|
+
const log = io.write ?? ((line) => { if (!bool(p.flags, "json"))
|
|
143
|
+
console.log(line); });
|
|
144
|
+
const pre = await c.callWithStatus("POST", `/machines/${machineName}/resize/preflight`, {
|
|
145
|
+
target_type: targetType,
|
|
146
|
+
...(instanceId ? { instance_id: instanceId } : {}),
|
|
147
|
+
});
|
|
148
|
+
const pj = pre.json;
|
|
149
|
+
log(`Preflight ${machineName}${pj.instance_id ? ` (${pj.instance_id})` : ""}: ${pj.from_type ?? "?"} -> ${pj.to_type ?? targetType}${pj.availability_zone ? ` in ${pj.availability_zone}` : ""}`);
|
|
150
|
+
for (const ch of pj.checks ?? []) {
|
|
151
|
+
log(` ${ch.ok ? "[ok]" : "[FAIL]"} ${ch.name}: ${ch.reason}`);
|
|
152
|
+
}
|
|
153
|
+
if (pj.cost) {
|
|
154
|
+
log(` Cost: ${cents(pj.cost.hourly_before_cents)}/hr -> ${cents(pj.cost.hourly_after_cents)}/hr (${pj.cost.hourly_delta_cents === null ? "delta n/a" : `delta ${pj.cost.hourly_delta_cents >= 0 ? "+" : ""}${cents(pj.cost.hourly_delta_cents)}/hr`}); daily ${cents(pj.cost.daily_before_cents)} -> ${cents(pj.cost.daily_after_cents)} vs ${pj.cost.spend_mode} cap ${cents(pj.cost.launchable_cap_cents)} (headroom ${cents(pj.cost.remaining_headroom_cents)})`);
|
|
155
|
+
}
|
|
156
|
+
if (!pre.ok) {
|
|
157
|
+
throw new CliError(pj.error ?? "resize_preflight_failed", (pj.failed_checks ?? []).map((f) => `${f.name}: ${f.reason}`).join("; ") || "resize preflight checks failed");
|
|
158
|
+
}
|
|
159
|
+
if (!yes) {
|
|
160
|
+
const answer = (await prompt(`Open maintenance window and resize ${machineName} (${pj.from_type} -> ${pj.to_type})? [y/N] `)).toLowerCase();
|
|
161
|
+
if (answer !== "y" && answer !== "yes") {
|
|
162
|
+
return out.print("Resize cancelled before opening maintenance.", { cancelled: true, preflight: pj });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const started = await c.callWithStatus("POST", `/machines/${machineName}/resize`, {
|
|
166
|
+
target_type: targetType,
|
|
167
|
+
...(instanceId ? { instance_id: instanceId } : {}),
|
|
168
|
+
confirm: true,
|
|
169
|
+
drain_timeout_minutes: drainTimeoutMinutes,
|
|
170
|
+
});
|
|
171
|
+
if (!started.ok) {
|
|
172
|
+
throw new CliError(started.json?.error ?? "resize_failed", started.json?.message ?? "could not start resize");
|
|
173
|
+
}
|
|
174
|
+
let op = started.json.operation;
|
|
175
|
+
log(`Maintenance window opened; draining running turns (id=${op.id}, deadline=${op.drain_deadline_at})...`);
|
|
176
|
+
let proceedWithRunningTurns = false;
|
|
177
|
+
while (true) {
|
|
178
|
+
const execRes = await c.callWithStatus("POST", `/machines/${machineName}/resize/${op.id}/execute`, {
|
|
179
|
+
...(proceedWithRunningTurns ? { proceed_with_running_turns: true } : {}),
|
|
180
|
+
});
|
|
181
|
+
proceedWithRunningTurns = false;
|
|
182
|
+
if (execRes.status === 409 && execRes.json?.error === "turns_still_running") {
|
|
183
|
+
await sleep(pollMs);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (execRes.status === 409 && execRes.json?.error === "drain_deadline_reached") {
|
|
187
|
+
const turns = (execRes.json.running_turns ?? []);
|
|
188
|
+
log(`Drain deadline reached with ${turns.length} turn(s) still running: ${turns.map((t) => `${t.agent} (${t.thread ?? "no thread"})`).join(", ")}`);
|
|
189
|
+
let choice = onRunningTurn;
|
|
190
|
+
if (!choice) {
|
|
191
|
+
const ans = (await prompt("Choose [w]ait longer, [p]roceed and record interrupted turn, or [c]ancel: ")).toLowerCase();
|
|
192
|
+
choice = ans.startsWith("w") ? "wait" : ans.startsWith("p") ? "proceed" : "abort";
|
|
193
|
+
}
|
|
194
|
+
if (choice === "wait") {
|
|
195
|
+
const ext = await c.call("POST", `/machines/${machineName}/resize/${op.id}/extend-drain`, { minutes: extendMinutes });
|
|
196
|
+
op = ext.operation;
|
|
197
|
+
log(`Extended drain deadline to ${op.drain_deadline_at}; continuing to wait...`);
|
|
198
|
+
await sleep(pollMs);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (choice === "proceed") {
|
|
202
|
+
proceedWithRunningTurns = true;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
await c.call("POST", `/machines/${machineName}/resize/${op.id}/cancel`);
|
|
206
|
+
return out.print("Resize cancelled at drain deadline; maintenance window closed.", { cancelled: true, operation_id: op.id });
|
|
207
|
+
}
|
|
208
|
+
if (!execRes.ok) {
|
|
209
|
+
throw new CliError(execRes.json?.error ?? "resize_execute_failed", execRes.json?.message ?? "resize execution failed");
|
|
210
|
+
}
|
|
211
|
+
op = execRes.json.operation;
|
|
212
|
+
if (execRes.status === 202) {
|
|
213
|
+
await sleep(pollMs);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
const sshResult = applySshIpUpdate(machineName, sshAlias, op.previous_public_ip, op.new_public_ip, sshConfigPath);
|
|
219
|
+
log(sshResult.message);
|
|
220
|
+
if (!noWaitVerify && op.state === "verifying") {
|
|
221
|
+
for (let attempt = 0; attempt < 60; attempt++) {
|
|
222
|
+
const verifyRes = await c.callWithStatus("POST", `/machines/${machineName}/resize/${op.id}/verify`);
|
|
223
|
+
if (verifyRes.ok && verifyRes.json?.verified) {
|
|
224
|
+
op = verifyRes.json.operation;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
if (verifyRes.status !== 409) {
|
|
228
|
+
throw new CliError(verifyRes.json?.error ?? "resize_verify_failed", verifyRes.json?.message ?? "verification failed");
|
|
229
|
+
}
|
|
230
|
+
await sleep(pollMs);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return out.print(`Resized ${machineName} (${op.instance_id}): ${op.from_type} -> ${op.to_type} [state=${op.state}; cores=${op.after_cpus ?? "pending"}/${op.target_vcpus}; ${sshResult.message}]`, { operation: op, ssh: sshResult });
|
|
234
|
+
}
|