@instruction.md/agents 0.0.0-stage → 0.2.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/NOTICE +73 -0
- package/README.md +204 -2
- package/dist/accounts.js +327 -0
- package/dist/acp.js +268 -0
- package/dist/agent.d.ts +60 -0
- package/dist/agent.js +86 -0
- package/dist/apply.js +392 -0
- package/dist/connect.js +138 -0
- package/dist/converse.js +256 -0
- package/dist/fleet.js +1002 -0
- package/dist/hook.js +406 -0
- package/dist/hosts.js +151 -0
- package/dist/main.js +119 -0
- package/dist/oauth.js +374 -0
- package/dist/pi.d.ts +59 -0
- package/dist/pi.js +89 -0
- package/dist/setup.js +324 -0
- package/dist/sync.js +114 -0
- package/dist/turns.js +143 -0
- package/package.json +70 -3
package/dist/fleet.js
ADDED
|
@@ -0,0 +1,1002 @@
|
|
|
1
|
+
// src/fleet.ts
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { hostname } from "node:os";
|
|
5
|
+
import { basename, join, resolve } from "node:path";
|
|
6
|
+
import { atomicWrite, DEFAULT_URL, ensureGitignore, readState } from "instruction.md/cli";
|
|
7
|
+
import { ClaudeSession, cardOf, converseArgv, responseOf } from "./converse.js";
|
|
8
|
+
var str = (v) => typeof v === "string" && v ? v : void 0;
|
|
9
|
+
var num = (v) => typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
10
|
+
var obj = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : void 0;
|
|
11
|
+
var STATES = /* @__PURE__ */ new Set(["pending_approval", "active", "paused", "retired"]);
|
|
12
|
+
var RELEASES = /* @__PURE__ */ new Set(["live", "restart", "approve"]);
|
|
13
|
+
function normalizeDeployment(raw, rule) {
|
|
14
|
+
const ins = obj(raw.instruction);
|
|
15
|
+
const id = str(raw.id) ?? str(raw.deploymentId);
|
|
16
|
+
const instructionId = str(raw.instructionId) ?? str(raw.instruction_id) ?? str(ins?.id);
|
|
17
|
+
if (!id || !instructionId) return null;
|
|
18
|
+
const controls = { ...obj(rule?.controls) ?? {}, ...obj(raw.controls) ?? {} };
|
|
19
|
+
const state = str(raw.state) ?? "active";
|
|
20
|
+
const onRelease = str(controls.onRelease) ?? str(raw.onRelease) ?? "live";
|
|
21
|
+
const ownerName = str(ins?.address) ?? str(raw.address);
|
|
22
|
+
const [aOwner, aName] = ownerName?.includes("/") ? ownerName.split("/", 2) : [void 0, void 0];
|
|
23
|
+
return {
|
|
24
|
+
id,
|
|
25
|
+
instructionId,
|
|
26
|
+
owner: str(ins?.owner) ?? str(raw.owner) ?? aOwner,
|
|
27
|
+
name: str(ins?.name) ?? str(raw.name) ?? aName,
|
|
28
|
+
title: str(ins?.title) ?? str(raw.title),
|
|
29
|
+
ref: str(raw.ref) ?? str(controls.ref) ?? "latest",
|
|
30
|
+
versionId: str(raw.versionId) ?? str(raw.version_id) ?? str(obj(raw.version)?.id) ?? null,
|
|
31
|
+
version: num(raw.number) ?? num(raw.version) ?? num(obj(raw.version)?.number) ?? null,
|
|
32
|
+
pendingVersionId: str(raw.pendingVersionId) ?? str(raw.pending_version_id) ?? str(obj(raw.pending)?.versionId) ?? null,
|
|
33
|
+
// (a paused rule pauses its deployments: what runs keeps running at its version, nothing new starts)
|
|
34
|
+
state: !STATES.has(state) ? "active" : state === "active" && rule?.paused === true ? "paused" : state,
|
|
35
|
+
ruleId: str(raw.ruleId) ?? str(raw.rule_id) ?? str(rule?.id) ?? null,
|
|
36
|
+
platform: str(raw.platform) ?? str(rule?.platform),
|
|
37
|
+
onRelease: RELEASES.has(onRelease) ? onRelease : "live",
|
|
38
|
+
requireSignature: controls.requireSignature === true || raw.requireSignature === true,
|
|
39
|
+
...obj(raw.parameters) ? { parameters: obj(raw.parameters) } : {}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function viewOf(answer) {
|
|
43
|
+
const rule = obj(answer.rule);
|
|
44
|
+
const list = Array.isArray(answer.deployments) ? answer.deployments : Array.isArray(rule?.deployments) ? rule.deployments : [];
|
|
45
|
+
const deployments = list.map((d) => normalizeDeployment(obj(d) ?? {}, rule)).filter((d) => !!d);
|
|
46
|
+
const maxAgents = num(obj(rule?.controls)?.maxAgents) ?? num(answer.maxAgents);
|
|
47
|
+
return { deployments, ...maxAgents !== void 0 ? { maxAgents } : {}, ...str(rule?.name) ? { ruleName: str(rule?.name) } : {}, ...rule?.paused === true ? { paused: true } : {} };
|
|
48
|
+
}
|
|
49
|
+
function viewFrom(list, rules = /* @__PURE__ */ new Map(), ruleId) {
|
|
50
|
+
const rows = list.map((d) => obj(d) ?? {});
|
|
51
|
+
const deployments = rows.map((d) => normalizeDeployment(d, rules.get(str(d.ruleId) ?? str(d.rule_id) ?? ruleId ?? ""))).filter((d) => !!d);
|
|
52
|
+
const limits = {};
|
|
53
|
+
for (const [id, r] of rules) {
|
|
54
|
+
const m = num(obj(r.controls)?.maxAgents);
|
|
55
|
+
if (m !== void 0 && m > 0) limits[id] = m;
|
|
56
|
+
}
|
|
57
|
+
const main = ruleId ? rules.get(ruleId) : void 0;
|
|
58
|
+
return { deployments, ...Object.keys(limits).length ? { limits } : {}, ...str(main?.name) ? { ruleName: str(main?.name) } : {}, ...main?.paused === true ? { paused: true } : {} };
|
|
59
|
+
}
|
|
60
|
+
function staleIfBehind(dir, versionId) {
|
|
61
|
+
if (!versionId) return false;
|
|
62
|
+
const file = join(dir, "state.json");
|
|
63
|
+
let s;
|
|
64
|
+
try {
|
|
65
|
+
s = JSON.parse(readFileSync(file, "utf8"));
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (!s || typeof s !== "object" || s.versionId === versionId || s.fetchedAt === STALE_AT) return false;
|
|
70
|
+
try {
|
|
71
|
+
atomicWrite(file, `${JSON.stringify({ ...s, fetchedAt: STALE_AT }, null, 2)}
|
|
72
|
+
`);
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
var STALE_AT = (/* @__PURE__ */ new Date(0)).toISOString();
|
|
79
|
+
function folderName(d) {
|
|
80
|
+
const safe = (s) => s.replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^[.-]+/, "").slice(0, 80);
|
|
81
|
+
return d.owner && d.name ? `${safe(d.owner)}__${safe(d.name)}` : safe(d.instructionId);
|
|
82
|
+
}
|
|
83
|
+
function followUri(d) {
|
|
84
|
+
const base = d.owner && d.name ? `instruction://${d.owner}/${d.name}` : `instruction://${d.instructionId}`;
|
|
85
|
+
const held = d.onRelease === "approve" || !!d.pendingVersionId || d.state === "paused";
|
|
86
|
+
const sel = held && d.versionId ? d.versionId : d.ref;
|
|
87
|
+
return `${base}@${sel}`;
|
|
88
|
+
}
|
|
89
|
+
function commandFor(template, d, dir) {
|
|
90
|
+
const label = d.owner && d.name ? `${d.owner}/${d.name}` : d.instructionId;
|
|
91
|
+
return template.map((a) => a.replaceAll("{instruction}", followUri(d)).replaceAll("{name}", label).replaceAll("{dir}", dir));
|
|
92
|
+
}
|
|
93
|
+
function plan(view, running, o = {}) {
|
|
94
|
+
const out = [];
|
|
95
|
+
const limit = Math.min(o.max ?? Infinity, view.maxAgents && view.maxAgents > 0 ? view.maxAgents : Infinity);
|
|
96
|
+
const byId = new Map(view.deployments.map((d) => [d.id, d]));
|
|
97
|
+
for (const [id] of running) {
|
|
98
|
+
const d = byId.get(id);
|
|
99
|
+
if (!d) out.push({ kind: "stop", deploymentId: id, why: "gone" });
|
|
100
|
+
else if (d.state === "retired") out.push({ kind: "stop", deploymentId: id, why: "retired" });
|
|
101
|
+
}
|
|
102
|
+
const stopping = new Set(out.filter((a) => a.kind === "stop").map((a) => a.deploymentId));
|
|
103
|
+
let counting = [...running.values()].filter((r) => r.alive && !stopping.has(r.deploymentId)).sort((a, b) => a.deploymentId.localeCompare(b.deploymentId));
|
|
104
|
+
const perRule = /* @__PURE__ */ new Map();
|
|
105
|
+
const kept = [];
|
|
106
|
+
for (const r of counting) {
|
|
107
|
+
const rid = byId.get(r.deploymentId)?.ruleId;
|
|
108
|
+
const l = rid ? view.limits?.[rid] : void 0;
|
|
109
|
+
if (kept.length >= limit || rid && l && l > 0 && (perRule.get(rid) ?? 0) >= l) {
|
|
110
|
+
out.push({ kind: "stop", deploymentId: r.deploymentId, why: "over-limit" });
|
|
111
|
+
stopping.add(r.deploymentId);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
kept.push(r);
|
|
115
|
+
if (rid) perRule.set(rid, (perRule.get(rid) ?? 0) + 1);
|
|
116
|
+
}
|
|
117
|
+
counting = kept;
|
|
118
|
+
let count = counting.length;
|
|
119
|
+
const ruleFull = (d) => {
|
|
120
|
+
const l = d.ruleId ? view.limits?.[d.ruleId] : void 0;
|
|
121
|
+
return !!l && l > 0 && (perRule.get(d.ruleId) ?? 0) >= l;
|
|
122
|
+
};
|
|
123
|
+
for (const listed of [...view.deployments].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
124
|
+
let d = listed;
|
|
125
|
+
if (d.state === "retired") continue;
|
|
126
|
+
if (stopping.has(d.id)) {
|
|
127
|
+
out.push({ kind: "wait", d, why: "over-limit" });
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const r = running.get(d.id);
|
|
131
|
+
if (d.state === "pending_approval") {
|
|
132
|
+
if (!r) out.push({ kind: "wait", d, why: "approval" });
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (d.state === "paused" || view.paused) {
|
|
136
|
+
d = { ...d, state: "paused" };
|
|
137
|
+
if (r?.alive) {
|
|
138
|
+
if (r.follow && r.follow !== followUri(d)) out.push({ kind: "rewrite", d });
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (!r || !r.alive && o.restartExited) {
|
|
143
|
+
if (count >= limit || ruleFull(d)) {
|
|
144
|
+
out.push({ kind: "wait", d, why: "over-limit" });
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
out.push(r ? { kind: "restart", d, from: r.versionId } : { kind: "start", d });
|
|
148
|
+
count++;
|
|
149
|
+
if (d.ruleId) perRule.set(d.ruleId, (perRule.get(d.ruleId) ?? 0) + 1);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (!r.alive) continue;
|
|
153
|
+
if (d.pendingVersionId) out.push({ kind: "wait", d, why: "release-pending" });
|
|
154
|
+
const moved = !!d.versionId && d.versionId !== r.versionId;
|
|
155
|
+
if (!moved) {
|
|
156
|
+
if (r.follow && r.follow !== followUri(d)) out.push({ kind: "rewrite", d });
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (d.onRelease === "restart") out.push({ kind: "restart", d, from: r.versionId });
|
|
160
|
+
else if (d.onRelease === "approve") out.push({ kind: "rewrite", d });
|
|
161
|
+
else out.push({ kind: "rewrite", d });
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
function appOf(command, _host) {
|
|
166
|
+
const b = basename(command[0] ?? "").replace(/\.(exe|cmd)$/i, "");
|
|
167
|
+
return b === "claude" ? "claude-code" : b === "codex" ? "codex" : null;
|
|
168
|
+
}
|
|
169
|
+
function claudeArgv(argv, folder) {
|
|
170
|
+
const mcp = join(folder, ".instruction", "mcp.json"), settings = join(folder, ".instruction", "claude-settings.json");
|
|
171
|
+
const out = [...argv];
|
|
172
|
+
if (existsSync(settings) && !out.includes("--settings")) out.push("--settings", settings);
|
|
173
|
+
if (existsSync(mcp) && !out.includes("--mcp-config")) out.push("--strict-mcp-config", "--mcp-config", mcp);
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
var STATE_FILE = ".instruction-fleet.json";
|
|
177
|
+
var HOST_FIX = { "claude-code": "claude setup-token, then: instruction-agents accounts host claude-code --env CLAUDE_CODE_OAUTH_TOKEN --from-command '<your secret store>'", codex: "codex login --device-auth (or: instruction-agents accounts host codex --env CODEX_API_KEY --from-command '<your secret store>')" };
|
|
178
|
+
var STDERR_LINES = 20;
|
|
179
|
+
var Fleet = class {
|
|
180
|
+
constructor(client, o) {
|
|
181
|
+
this.client = client;
|
|
182
|
+
this.o = o;
|
|
183
|
+
this.log = o.log ?? ((l) => process.stderr.write(`${l}
|
|
184
|
+
`));
|
|
185
|
+
this.now = o.now ?? Date.now;
|
|
186
|
+
this.state = this.readState();
|
|
187
|
+
}
|
|
188
|
+
agents = /* @__PURE__ */ new Map();
|
|
189
|
+
/** Deployments held back because something they need is missing (RFC-0058 §6), and what. */
|
|
190
|
+
blocked = /* @__PURE__ */ new Map();
|
|
191
|
+
/** Folders of Codex agents, marked trusted in Codex's config. */
|
|
192
|
+
trusted = /* @__PURE__ */ new Set();
|
|
193
|
+
state;
|
|
194
|
+
timer = null;
|
|
195
|
+
closed = false;
|
|
196
|
+
log;
|
|
197
|
+
now;
|
|
198
|
+
lastTable = "";
|
|
199
|
+
last = null;
|
|
200
|
+
readState() {
|
|
201
|
+
try {
|
|
202
|
+
const s = JSON.parse(readFileSync(join(this.o.dir, STATE_FILE), "utf8"));
|
|
203
|
+
return { made: s.made ?? {}, started: s.started ?? {} };
|
|
204
|
+
} catch {
|
|
205
|
+
return { made: {}, started: {} };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
writeState() {
|
|
209
|
+
if (this.o.dryRun) return;
|
|
210
|
+
try {
|
|
211
|
+
mkdirSync(this.o.dir, { recursive: true });
|
|
212
|
+
atomicWrite(join(this.o.dir, STATE_FILE), `${JSON.stringify(this.state, null, 2)}
|
|
213
|
+
`);
|
|
214
|
+
} catch {
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** The running agents, as the plan sees them. */
|
|
218
|
+
runningView() {
|
|
219
|
+
const m = /* @__PURE__ */ new Map();
|
|
220
|
+
for (const [id, a] of this.agents) m.set(id, { deploymentId: id, versionId: a.target, follow: a.follow, alive: isAlive(a) });
|
|
221
|
+
return m;
|
|
222
|
+
}
|
|
223
|
+
/** The folder of a deployment, made (and recorded as made) when it isn't there. */
|
|
224
|
+
folderOf(d) {
|
|
225
|
+
const folder = resolve(this.o.dir, folderName(d));
|
|
226
|
+
if (!existsSync(folder)) {
|
|
227
|
+
mkdirSync(folder, { recursive: true });
|
|
228
|
+
this.state.made[d.id] = folder;
|
|
229
|
+
}
|
|
230
|
+
return folder;
|
|
231
|
+
}
|
|
232
|
+
/** follow.json for a deployment's folder (and the .gitignore beside it). */
|
|
233
|
+
writeFollow(folder, d) {
|
|
234
|
+
const follow = followUri(d);
|
|
235
|
+
const dir = join(folder, ".instruction");
|
|
236
|
+
mkdirSync(dir, { recursive: true });
|
|
237
|
+
const hosted = appOf(this.o.command, this.o.host) === "claude-code" && this.o.converse !== false && !!this.client.converse && !!converseArgv(this.o.command);
|
|
238
|
+
const config = { follow, ...d.requireSignature ? { requireSignature: true } : {}, ...this.o.url ? { url: this.o.url } : {}, deploymentId: d.id, ...hosted ? { converse: true } : {} };
|
|
239
|
+
atomicWrite(join(dir, "follow.json"), `${JSON.stringify(config, null, 2)}
|
|
240
|
+
`);
|
|
241
|
+
ensureGitignore(dir);
|
|
242
|
+
const ignore = join(dir, ".gitignore");
|
|
243
|
+
const have = readFileSync(ignore, "utf8");
|
|
244
|
+
if (!have.split(/\r?\n/).includes("fleet.log")) appendFileSync(ignore, `${have.endsWith("\n") ? "" : "\n"}fleet.log
|
|
245
|
+
`);
|
|
246
|
+
staleIfBehind(dir, d.versionId);
|
|
247
|
+
return follow;
|
|
248
|
+
}
|
|
249
|
+
/** The folder's servers and skills for the version it follows (RFC-0058 §3); the digest applied, or null when nothing was. */
|
|
250
|
+
async applyIn(folder, follow, app) {
|
|
251
|
+
if (this.o.apply === false || !this.client.machinery) return null;
|
|
252
|
+
const [{ applyMachinery, sayApplied }, src] = await Promise.all([import("./apply.js"), this.client.machinery(follow)]);
|
|
253
|
+
const r = await applyMachinery(src, { dir: folder, hosts: [app], fleet: app === "claude-code", cli: this.o.cli ?? fleetCli(), ...this.o.accounts ? { accounts: this.o.accounts } : {}, ...this.o.accountsFile ? { accountsFile: this.o.accountsFile } : {} });
|
|
254
|
+
if (r.changed) this.log(`${src.label}: ${sayApplied(r)}`);
|
|
255
|
+
if (app === "codex") this.trust(folder, true);
|
|
256
|
+
return { servers: r.applied.servers, digest: r.applied.digest };
|
|
257
|
+
}
|
|
258
|
+
/** Codex reads a folder's config only when the folder is trusted: the fleet's folders, in a block of its own. */
|
|
259
|
+
trust(folder, on) {
|
|
260
|
+
if (on === this.trusted.has(folder)) return;
|
|
261
|
+
if (on) this.trusted.add(folder);
|
|
262
|
+
else this.trusted.delete(folder);
|
|
263
|
+
if (this.o.dryRun) return;
|
|
264
|
+
void import("./apply.js").then(({ codexTrust, fleetCodexConfigPath }) => {
|
|
265
|
+
const file = this.o.codexConfig ?? fleetCodexConfigPath();
|
|
266
|
+
let text;
|
|
267
|
+
try {
|
|
268
|
+
text = readFileSync(file, "utf8");
|
|
269
|
+
} catch {
|
|
270
|
+
}
|
|
271
|
+
const next = codexTrust(text, [...this.trusted].sort());
|
|
272
|
+
if (next !== (text ?? "")) atomicWrite(file, next);
|
|
273
|
+
}).catch((e) => this.log(`codex trust: ${e instanceof Error ? e.message : String(e)}`));
|
|
274
|
+
}
|
|
275
|
+
async start(d, previous) {
|
|
276
|
+
const folder = this.folderOf(d);
|
|
277
|
+
const follow = this.writeFollow(folder, d);
|
|
278
|
+
const app = appOf(this.o.command, this.o.host);
|
|
279
|
+
let hostVars = {};
|
|
280
|
+
if (app) {
|
|
281
|
+
let servers = [];
|
|
282
|
+
try {
|
|
283
|
+
servers = (await this.applyIn(folder, follow, app))?.servers ?? [];
|
|
284
|
+
} catch (e) {
|
|
285
|
+
this.log(`${labelOfDeployment(d)}: its servers and skills weren't written (${e instanceof Error ? e.message : String(e)}); it starts without them`);
|
|
286
|
+
}
|
|
287
|
+
let needs = [];
|
|
288
|
+
try {
|
|
289
|
+
const { hostEnv } = await import("./accounts.js");
|
|
290
|
+
if (this.o.accounts) hostVars = hostEnv(this.o.accounts, app);
|
|
291
|
+
} catch (e) {
|
|
292
|
+
needs.push({ kind: "host", name: app, what: `${app}'s sign-in from this machine's accounts: ${e instanceof Error ? e.message : String(e)}`, ...e.fix ? { fix: e.fix } : {} });
|
|
293
|
+
}
|
|
294
|
+
if (!needs.length) needs = await (this.o.check ?? defaultCheck(this.o.accounts))({ host: app, servers, env: { ...process.env, ...hostVars } });
|
|
295
|
+
if (needs.length) {
|
|
296
|
+
const was = this.blocked.get(d.id);
|
|
297
|
+
this.blocked.set(d.id, needs);
|
|
298
|
+
if (JSON.stringify(was) !== JSON.stringify(needs)) this.log(`${labelOfDeployment(d)}: not started, it needs: ${needs.map((n) => `${n.what}${n.fix ? ` — ${n.fix}` : ""}`).join("; ")}`);
|
|
299
|
+
if (previous) this.agents.delete(d.id);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (this.blocked.delete(d.id)) this.log(`${labelOfDeployment(d)}: what it needed is here now`);
|
|
303
|
+
}
|
|
304
|
+
let argv = commandFor(this.o.command, d, folder);
|
|
305
|
+
const conv = app === "claude-code" && this.o.converse !== false && this.client.converse && !this.o.dryRun ? converseArgv(argv) : null;
|
|
306
|
+
if (app === "claude-code" && this.o.converse !== false && this.client.converse && !conv && !this.saidNoConverse) {
|
|
307
|
+
this.saidNoConverse = true;
|
|
308
|
+
this.log(`fleet: its agents' questions can't reach instruction.md: give Claude Code its prompt as \`claude -p "…"\` (without --input-format or --output-format)`);
|
|
309
|
+
}
|
|
310
|
+
if (conv) argv = conv.argv;
|
|
311
|
+
if (app === "claude-code") argv = claudeArgv(argv, folder);
|
|
312
|
+
const env = { ...process.env, ...hostVars, INSTRUCTION_MD_FOLLOW: follow, INSTRUCTION_MD_DEPLOYMENT: d.id, ...this.o.url ? { INSTRUCTION_MD_URL: this.o.url } : {}, ...this.o.accountsFile ? { INSTRUCTION_MD_ACCOUNTS: this.o.accountsFile } : {} };
|
|
313
|
+
const child = (this.o.spawnImpl ?? spawn)(argv[0], argv.slice(1), { cwd: folder, env, stdio: [conv ? "pipe" : "ignore", "pipe", "pipe"] });
|
|
314
|
+
const a = {
|
|
315
|
+
d,
|
|
316
|
+
folder,
|
|
317
|
+
child,
|
|
318
|
+
versionId: d.versionId ?? null,
|
|
319
|
+
target: d.versionId ?? null,
|
|
320
|
+
follow,
|
|
321
|
+
exitCode: null,
|
|
322
|
+
exitSignal: null,
|
|
323
|
+
stderr: [],
|
|
324
|
+
startedAt: this.now(),
|
|
325
|
+
restarts: previous ? previous.restarts + 1 : 0,
|
|
326
|
+
nextStartAt: 0,
|
|
327
|
+
reported: previous?.reported ?? "",
|
|
328
|
+
reportedAt: previous?.reportedAt ?? 0,
|
|
329
|
+
stopping: false,
|
|
330
|
+
lines: [],
|
|
331
|
+
asks: /* @__PURE__ */ new Set()
|
|
332
|
+
};
|
|
333
|
+
const logFile = join(folder, ".instruction", "fleet.log");
|
|
334
|
+
const keep = (chunk, err) => {
|
|
335
|
+
try {
|
|
336
|
+
appendFileSync(logFile, chunk);
|
|
337
|
+
} catch {
|
|
338
|
+
}
|
|
339
|
+
if (!err) return;
|
|
340
|
+
for (const l of chunk.toString("utf8").split(/\r?\n/)) if (l.trim()) a.stderr.push(l);
|
|
341
|
+
if (a.stderr.length > STDERR_LINES) a.stderr.splice(0, a.stderr.length - STDERR_LINES);
|
|
342
|
+
};
|
|
343
|
+
if (conv) {
|
|
344
|
+
a.session = new ClaudeSession(child, {
|
|
345
|
+
raw: (c) => keep(c, false),
|
|
346
|
+
line: (l) => this.lineOf(a, l),
|
|
347
|
+
idle: () => {
|
|
348
|
+
if (this.onceMode) a.session?.end();
|
|
349
|
+
},
|
|
350
|
+
decide: (req, signal) => this.decide(a, req, signal),
|
|
351
|
+
subagents: () => void this.reportOne(a.d.id, this.reportFor(a))
|
|
352
|
+
});
|
|
353
|
+
a.session.begin(conv.prompt);
|
|
354
|
+
} else child.stdout?.on("data", (c) => keep(c, false));
|
|
355
|
+
child.stderr?.on("data", (c) => keep(c, true));
|
|
356
|
+
child.on("error", (e) => {
|
|
357
|
+
a.stderr.push(String(e.message));
|
|
358
|
+
a.exitCode = a.exitCode ?? 127;
|
|
359
|
+
});
|
|
360
|
+
child.on("exit", (code, signal) => {
|
|
361
|
+
a.exitCode = code;
|
|
362
|
+
a.exitSignal = signal;
|
|
363
|
+
if (code !== 0 || signal) void import("./setup.js").then(({ signInFailure }) => {
|
|
364
|
+
a.signIn = signInFailure(a.stderr);
|
|
365
|
+
}).catch(() => {
|
|
366
|
+
});
|
|
367
|
+
a.nextStartAt = this.now() + Math.min(3e5, 5e3 * 2 ** Math.min(a.restarts, 6));
|
|
368
|
+
if (!a.stopping && !this.closed) this.log(`${labelOfDeployment(d)}: exited (${signal ?? `code ${code}`})`);
|
|
369
|
+
});
|
|
370
|
+
this.agents.set(d.id, a);
|
|
371
|
+
this.state.started[d.id] = { pid: child.pid ?? null, versionId: a.versionId, at: new Date(this.now()).toISOString() };
|
|
372
|
+
this.log(`${labelOfDeployment(d)}: started${d.version ? ` at v${d.version}` : ""} in ${folder}`);
|
|
373
|
+
}
|
|
374
|
+
/** Stops one agent: SIGTERM, then SIGKILL after killAfterMs; resolves once it has exited. */
|
|
375
|
+
stop(id) {
|
|
376
|
+
const a = this.agents.get(id);
|
|
377
|
+
if (a?.session) a.session.end();
|
|
378
|
+
if (!a?.child || a.exitCode !== null || a.exitSignal !== null) {
|
|
379
|
+
this.agents.delete(id);
|
|
380
|
+
return Promise.resolve();
|
|
381
|
+
}
|
|
382
|
+
a.stopping = true;
|
|
383
|
+
const child = a.child;
|
|
384
|
+
return new Promise((done) => {
|
|
385
|
+
const t = setTimeout(() => {
|
|
386
|
+
try {
|
|
387
|
+
child.kill("SIGKILL");
|
|
388
|
+
} catch {
|
|
389
|
+
}
|
|
390
|
+
}, this.o.killAfterMs ?? 1e4);
|
|
391
|
+
t.unref?.();
|
|
392
|
+
child.once("exit", () => {
|
|
393
|
+
clearTimeout(t);
|
|
394
|
+
this.agents.delete(id);
|
|
395
|
+
done();
|
|
396
|
+
});
|
|
397
|
+
try {
|
|
398
|
+
child.kill("SIGTERM");
|
|
399
|
+
} catch {
|
|
400
|
+
clearTimeout(t);
|
|
401
|
+
this.agents.delete(id);
|
|
402
|
+
done();
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
/** One reconcile: read, plan, act, report. Returns the actions taken (or planned, on a dry run). */
|
|
407
|
+
async reconcile() {
|
|
408
|
+
if (this.closed) return [];
|
|
409
|
+
const view = await this.client.view();
|
|
410
|
+
if (this.closed) return [];
|
|
411
|
+
const running = this.runningView();
|
|
412
|
+
const restartExited = !!this.o.restartOnExit;
|
|
413
|
+
let actions = plan(view, running, { ...this.o.max !== void 0 ? { max: this.o.max } : {}, restartExited });
|
|
414
|
+
if (restartExited) actions = actions.filter((x) => {
|
|
415
|
+
if (x.kind !== "restart") return true;
|
|
416
|
+
const a = this.agents.get(x.d.id);
|
|
417
|
+
return !a || isAlive(a) || this.now() >= a.nextStartAt;
|
|
418
|
+
});
|
|
419
|
+
if (this.o.dryRun) {
|
|
420
|
+
for (const x of actions) this.log(`would ${describe(x)}`);
|
|
421
|
+
return actions;
|
|
422
|
+
}
|
|
423
|
+
for (const x of actions) {
|
|
424
|
+
if (this.closed) break;
|
|
425
|
+
if (x.kind === "stop") {
|
|
426
|
+
const a = this.agents.get(x.deploymentId);
|
|
427
|
+
this.log(`${a ? labelOfDeployment(a.d) : x.deploymentId}: stopping (${x.why})`);
|
|
428
|
+
await this.stopAndReport(x.deploymentId);
|
|
429
|
+
} else if (x.kind === "start") await this.start(x.d);
|
|
430
|
+
else if (x.kind === "restart") {
|
|
431
|
+
const prev = this.agents.get(x.d.id);
|
|
432
|
+
this.log(`${labelOfDeployment(x.d)}: restarting${x.from && x.d.versionId && x.from !== x.d.versionId ? " for a new version" : ""}`);
|
|
433
|
+
await this.stop(x.d.id);
|
|
434
|
+
await this.start(x.d, prev);
|
|
435
|
+
} else if (x.kind === "rewrite") {
|
|
436
|
+
const a = this.agents.get(x.d.id);
|
|
437
|
+
if (!a) continue;
|
|
438
|
+
const app = appOf(this.o.command, this.o.host);
|
|
439
|
+
if (app && this.o.apply !== false && this.client.machinery && isAlive(a) && x.d.versionId && x.d.versionId !== a.target) {
|
|
440
|
+
try {
|
|
441
|
+
const { readApplied } = await import("./apply.js");
|
|
442
|
+
const next = await this.client.machinery(followUri(x.d));
|
|
443
|
+
if (next.machinery.digest !== readApplied(a.folder)?.digest) {
|
|
444
|
+
this.log(`${labelOfDeployment(x.d)}: restarting, the new version changes its servers or skills`);
|
|
445
|
+
await this.stop(x.d.id);
|
|
446
|
+
await this.start(x.d, a);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
} catch (e) {
|
|
450
|
+
this.log(`${labelOfDeployment(x.d)}: the new version's servers and skills couldn't be read (${e instanceof Error ? e.message : String(e)})`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
a.follow = this.writeFollow(a.folder, x.d);
|
|
454
|
+
a.target = x.d.versionId ?? a.target;
|
|
455
|
+
a.d = x.d;
|
|
456
|
+
this.log(`${labelOfDeployment(x.d)}: now ${x.d.version ? `v${x.d.version}` : x.d.versionId} (taken at the agent's next turn)`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
for (const d of view.deployments) {
|
|
460
|
+
const a = this.agents.get(d.id);
|
|
461
|
+
if (a) a.d = d;
|
|
462
|
+
}
|
|
463
|
+
this.writeState();
|
|
464
|
+
await this.reportAll();
|
|
465
|
+
this.table(view, actions);
|
|
466
|
+
this.last = { view, actions };
|
|
467
|
+
return actions;
|
|
468
|
+
}
|
|
469
|
+
heldReports = /* @__PURE__ */ new Map();
|
|
470
|
+
async reportOne(id, r, force = false) {
|
|
471
|
+
if (this.o.dryRun) return;
|
|
472
|
+
const a = this.agents.get(id);
|
|
473
|
+
const { activity: _lines, ...same } = r;
|
|
474
|
+
const key = JSON.stringify(same);
|
|
475
|
+
const last = a ? { key: a.reported, at: a.reportedAt } : this.heldReports.get(id);
|
|
476
|
+
if (last && !force && last.key === key && this.now() - last.at < (this.o.heartbeatMs ?? 3e5)) return;
|
|
477
|
+
const app = appOf(this.o.command, this.o.host);
|
|
478
|
+
const host = this.o.host ?? app ?? void 0;
|
|
479
|
+
try {
|
|
480
|
+
await this.client.report({ deploymentId: id, ...r, ...host ? { host } : {} });
|
|
481
|
+
if (a) {
|
|
482
|
+
a.reported = key;
|
|
483
|
+
a.reportedAt = this.now();
|
|
484
|
+
} else this.heldReports.set(id, { key, at: this.now() });
|
|
485
|
+
} catch (e) {
|
|
486
|
+
this.log(`report for ${id}: ${e instanceof Error ? e.message : String(e)}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/** The version an agent has: what the hooks in its folder last applied (.instruction/state.json), else the one it
|
|
490
|
+
* was started at. */
|
|
491
|
+
versionOf(a) {
|
|
492
|
+
return readState(join(a.folder, ".instruction"))?.versionId ?? a.versionId;
|
|
493
|
+
}
|
|
494
|
+
/** What to report for an agent: running, its version, its health (exited non-zero or killed: failed, with the last
|
|
495
|
+
* lines it wrote to stderr). A process the runner stopped itself is not a failure. */
|
|
496
|
+
reportFor(a, stoppedHere = false) {
|
|
497
|
+
const alive = isAlive(a);
|
|
498
|
+
const failed = !alive && !stoppedHere && !a.stopping && (a.exitSignal !== null || (a.exitCode ?? 0) !== 0);
|
|
499
|
+
const v = this.versionOf(a);
|
|
500
|
+
const app = appOf(this.o.command, this.o.host);
|
|
501
|
+
const signIn = failed && app ? a.signIn ?? null : null;
|
|
502
|
+
return {
|
|
503
|
+
running: alive,
|
|
504
|
+
...v ? { versionId: v } : {},
|
|
505
|
+
health: signIn ? "needs_sign_in" : failed ? "failed" : "ok",
|
|
506
|
+
...failed ? { error: (a.stderr.slice(-5).join("\n") || `exited (${a.exitSignal ?? `code ${a.exitCode}`})`).slice(0, 2e3) } : {},
|
|
507
|
+
...signIn && app ? { needs: [{ kind: "host", name: app, what: signIn.slice(0, 300), fix: HOST_FIX[app] }] } : {},
|
|
508
|
+
...a.session ? { subagents: alive ? a.session.subagents : [] } : {}
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
async reportAll() {
|
|
512
|
+
for (const [id, a] of this.agents) await this.reportOne(id, this.reportFor(a));
|
|
513
|
+
for (const [id, needs] of this.blocked) if (!this.agents.has(id)) await this.reportOne(id, { running: false, health: "needs_sign_in", needs });
|
|
514
|
+
}
|
|
515
|
+
// ───────────── conversations (RFC-0059) ─────────────
|
|
516
|
+
saidNoConverse = false;
|
|
517
|
+
onceMode = false;
|
|
518
|
+
converseTimer = null;
|
|
519
|
+
/** The cards waiting for a person, each with what checks it again. */
|
|
520
|
+
waiting = /* @__PURE__ */ new Map();
|
|
521
|
+
/** A line of what an agent did: reported within two seconds, twenty at a time. */
|
|
522
|
+
lineOf(a, l) {
|
|
523
|
+
a.lines.push(l);
|
|
524
|
+
if (a.lines.length > 200) a.lines.splice(0, a.lines.length - 200);
|
|
525
|
+
if (a.flush) return;
|
|
526
|
+
a.flush = setTimeout(() => {
|
|
527
|
+
a.flush = void 0;
|
|
528
|
+
const lines = a.lines.splice(0, 20);
|
|
529
|
+
if (lines.length) void this.reportOne(a.d.id, { ...this.reportFor(a), activity: lines }, true).then(() => {
|
|
530
|
+
if (a.lines.length) this.lineOf(a, a.lines.shift());
|
|
531
|
+
});
|
|
532
|
+
}, 2e3);
|
|
533
|
+
a.flush.unref?.();
|
|
534
|
+
}
|
|
535
|
+
/** What Claude Code asks: a card for the people who answer for the agent; their answer, when it comes, or the default. */
|
|
536
|
+
async decide(a, req, signal) {
|
|
537
|
+
const conv = this.client.converse;
|
|
538
|
+
const last = [...a.lines].reverse().find((l) => l.type === "said")?.text;
|
|
539
|
+
const card = cardOf(req, last);
|
|
540
|
+
const made = await conv.ask({ deploymentId: a.d.id, ...card, idempotencyKey: `${a.d.id}:${a.startedAt}:${req.request_id}`.slice(0, 200) });
|
|
541
|
+
a.asks.add(made.id);
|
|
542
|
+
this.log(`${labelOfDeployment(a.d)}: ${card.kind === "question" ? "asks a question" : `asks to use ${card.tool?.name}`} — it waits for an answer on instruction.md`);
|
|
543
|
+
const minutes = made.expiresAt && made.createdAt ? Math.max(1, Math.round((Date.parse(made.expiresAt) - Date.parse(made.createdAt)) / 6e4)) : 30;
|
|
544
|
+
try {
|
|
545
|
+
const ans = await this.answerOf(made.id, made.expiresAt, signal);
|
|
546
|
+
this.log(`${labelOfDeployment(a.d)}: ${ans.state === "answered" ? `answered${ans.by ? ` by ${ans.by}` : ""}` : ans.state === "expired" ? "nobody answered in time" : "the card was withdrawn"}`);
|
|
547
|
+
return responseOf(req, ans, minutes);
|
|
548
|
+
} catch (e) {
|
|
549
|
+
if (signal.aborted) await conv.withdraw(made.id, "the agent no longer waits for it").catch(() => {
|
|
550
|
+
});
|
|
551
|
+
throw e;
|
|
552
|
+
} finally {
|
|
553
|
+
a.asks.delete(made.id);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
/** A card's answer, once it isn't open: read on each change the server announces, every interval, and at its time. */
|
|
557
|
+
answerOf(askId, expiresAt, signal) {
|
|
558
|
+
const conv = this.client.converse;
|
|
559
|
+
return new Promise((done, fail) => {
|
|
560
|
+
let reading = false, again = false, over = false;
|
|
561
|
+
const finish = (f) => {
|
|
562
|
+
if (over) return;
|
|
563
|
+
over = true;
|
|
564
|
+
this.waiting.delete(askId);
|
|
565
|
+
clearTimeout(at);
|
|
566
|
+
signal.removeEventListener("abort", abort);
|
|
567
|
+
f();
|
|
568
|
+
};
|
|
569
|
+
const check = () => {
|
|
570
|
+
if (over) return;
|
|
571
|
+
if (reading) {
|
|
572
|
+
again = true;
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
reading = true;
|
|
576
|
+
void conv.get(askId).then((x) => {
|
|
577
|
+
if (x.state !== "open") finish(() => done(x));
|
|
578
|
+
}, () => {
|
|
579
|
+
}).finally(() => {
|
|
580
|
+
reading = false;
|
|
581
|
+
if (again && !over) {
|
|
582
|
+
again = false;
|
|
583
|
+
check();
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
const abort = () => finish(() => fail(new Error("aborted")));
|
|
588
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
589
|
+
const wait = expiresAt ? Math.max(1e3, Date.parse(expiresAt) - Date.now() + 2e3) : 0;
|
|
590
|
+
const at = setTimeout(check, wait || 2147e6);
|
|
591
|
+
at.unref?.();
|
|
592
|
+
this.waiting.set(askId, check);
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
poking = false;
|
|
596
|
+
/** A change, or the interval: the cards waiting are read again, and each conversing agent is given what people wrote. */
|
|
597
|
+
poke() {
|
|
598
|
+
for (const check of this.waiting.values()) check();
|
|
599
|
+
if (this.poking || !this.client.converse) return;
|
|
600
|
+
this.poking = true;
|
|
601
|
+
void (async () => {
|
|
602
|
+
for (const a of [...this.agents.values()]) {
|
|
603
|
+
if (!a.session || !isAlive(a)) continue;
|
|
604
|
+
let got = [];
|
|
605
|
+
try {
|
|
606
|
+
got = await this.client.converse.take(a.d.id);
|
|
607
|
+
} catch {
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
for (const m of got) {
|
|
611
|
+
if (m.kind === "control" && m.control === "interrupt") {
|
|
612
|
+
a.session.interrupt();
|
|
613
|
+
this.log(`${labelOfDeployment(a.d)}: interrupted${m.by ? ` by ${m.by}` : ""}`);
|
|
614
|
+
this.lineOf(a, { type: "note", text: `Interrupted${m.by ? ` by ${m.by}` : ""}` });
|
|
615
|
+
} else if (m.kind === "control" && m.control === "restart") {
|
|
616
|
+
this.log(`${labelOfDeployment(a.d)}: restarting${m.by ? `, as ${m.by} asked` : ""}`);
|
|
617
|
+
await this.stop(a.d.id);
|
|
618
|
+
await this.start(a.d, a);
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
if (m.text) {
|
|
622
|
+
a.session.say(`${m.by ?? "A person"} wrote to you through instruction.md:
|
|
623
|
+
|
|
624
|
+
${m.text}`);
|
|
625
|
+
this.log(`${labelOfDeployment(a.d)}: a message${m.by ? ` from ${m.by}` : ""} handed over`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
})().finally(() => {
|
|
630
|
+
this.poking = false;
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
/** Stops one agent, and reports it stopped — when it was running; one that had exited keeps its last report. */
|
|
634
|
+
async stopAndReport(id) {
|
|
635
|
+
const a = this.agents.get(id);
|
|
636
|
+
const wasAlive = !!a && isAlive(a);
|
|
637
|
+
await this.stop(id);
|
|
638
|
+
if (a && wasAlive) await this.reportOne(id, { ...this.reportFor(a, true), running: false }, true);
|
|
639
|
+
}
|
|
640
|
+
/** A short table of what runs, printed when it changes. */
|
|
641
|
+
table(view, actions) {
|
|
642
|
+
const waiting = new Map(actions.filter((x) => x.kind === "wait").map((x) => [x.d.id, x.why]));
|
|
643
|
+
const rows = [...view.deployments].sort((a, b) => a.id.localeCompare(b.id)).map((d) => {
|
|
644
|
+
const a = this.agents.get(d.id);
|
|
645
|
+
const proc = a ? a.exitCode === null && a.exitSignal === null ? `running (pid ${a.child?.pid ?? "?"})` : `exited (${a.exitSignal ?? `code ${a.exitCode}`})` : d.state === "retired" ? "retired" : this.blocked.has(d.id) ? "needs sign-in" : WAIT[waiting.get(d.id) ?? ""] ?? "not started";
|
|
646
|
+
return ` ${labelOfDeployment(d).padEnd(36)} ${(d.version ? `v${d.version}` : d.versionId ?? "-").padEnd(8)} ${d.onRelease.padEnd(8)} ${proc}`;
|
|
647
|
+
});
|
|
648
|
+
const text = [`fleet${view.ruleName ? ` ${view.ruleName}` : ""}: ${view.deployments.filter((d) => d.state !== "retired").length} deployment(s), ${[...this.agents.values()].filter((a) => a.exitCode === null && a.exitSignal === null).length} running`, ...rows].join("\n");
|
|
649
|
+
if (text !== this.lastTable) {
|
|
650
|
+
this.log(text);
|
|
651
|
+
this.lastTable = text;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
pass = null;
|
|
655
|
+
again = false;
|
|
656
|
+
/** A reconcile soon: one at a time; asked for during one, one more after it. */
|
|
657
|
+
kick() {
|
|
658
|
+
if (this.closed) return Promise.resolve();
|
|
659
|
+
if (this.pass) {
|
|
660
|
+
this.again = true;
|
|
661
|
+
return this.pass;
|
|
662
|
+
}
|
|
663
|
+
this.pass = (async () => {
|
|
664
|
+
do {
|
|
665
|
+
this.again = false;
|
|
666
|
+
try {
|
|
667
|
+
await this.reconcile();
|
|
668
|
+
} catch (e) {
|
|
669
|
+
this.log(`reconcile: ${e instanceof Error ? e.message : String(e)}`);
|
|
670
|
+
}
|
|
671
|
+
} while (this.again && !this.closed);
|
|
672
|
+
})().finally(() => {
|
|
673
|
+
this.pass = null;
|
|
674
|
+
if (this.last?.view.gone && !this.closed) {
|
|
675
|
+
this.log("fleet: the rule was deleted, so its agents are stopped; nothing left to run");
|
|
676
|
+
void this.close();
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
return this.pass;
|
|
680
|
+
}
|
|
681
|
+
/** Reconciles now, every `interval` seconds, and on each change the server announces (when the client listens), until
|
|
682
|
+
* close(). */
|
|
683
|
+
async run() {
|
|
684
|
+
this.pass = this.reconcile().then(() => {
|
|
685
|
+
});
|
|
686
|
+
try {
|
|
687
|
+
await this.pass;
|
|
688
|
+
} finally {
|
|
689
|
+
this.pass = null;
|
|
690
|
+
}
|
|
691
|
+
if (this.o.dryRun || this.closed) return;
|
|
692
|
+
const every = Math.max(1, this.o.interval ?? 60) * 1e3;
|
|
693
|
+
const listening = await this.client.onChange?.(() => {
|
|
694
|
+
void this.kick();
|
|
695
|
+
this.poke();
|
|
696
|
+
}).catch(() => false);
|
|
697
|
+
if (this.client.converse && this.o.converse !== false) {
|
|
698
|
+
const t = setInterval(() => this.poke(), this.o.converseEveryMs ?? 1e4);
|
|
699
|
+
t.unref?.();
|
|
700
|
+
this.converseTimer = t;
|
|
701
|
+
}
|
|
702
|
+
if (listening) this.log(`fleet: listening for changes (and reading every ${every / 1e3} s)`);
|
|
703
|
+
await new Promise((done) => {
|
|
704
|
+
if (this.closed) return done();
|
|
705
|
+
this.onClose = done;
|
|
706
|
+
const tick = () => {
|
|
707
|
+
this.timer = setTimeout(() => {
|
|
708
|
+
void this.kick().then(() => {
|
|
709
|
+
if (!this.closed) tick();
|
|
710
|
+
});
|
|
711
|
+
}, every);
|
|
712
|
+
};
|
|
713
|
+
tick();
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
onClose = null;
|
|
717
|
+
/** Reconciles once, waits for every agent started to exit (a job each), reports, and says whether all succeeded. */
|
|
718
|
+
async once() {
|
|
719
|
+
this.onceMode = true;
|
|
720
|
+
this.pass = this.reconcile().then(() => {
|
|
721
|
+
});
|
|
722
|
+
try {
|
|
723
|
+
await this.pass;
|
|
724
|
+
} finally {
|
|
725
|
+
this.pass = null;
|
|
726
|
+
}
|
|
727
|
+
if (this.o.dryRun) return true;
|
|
728
|
+
await Promise.all([...this.agents.values()].map(ended));
|
|
729
|
+
await this.reportAll();
|
|
730
|
+
if (this.last) this.table(this.last.view, this.last.actions);
|
|
731
|
+
return [...this.agents.values()].every((a) => a.exitCode === 0);
|
|
732
|
+
}
|
|
733
|
+
/** Stops every agent it started and itself (SIGINT, SIGTERM, the end of a run). */
|
|
734
|
+
async close() {
|
|
735
|
+
if (this.closed) return;
|
|
736
|
+
this.closed = true;
|
|
737
|
+
if (this.timer) clearTimeout(this.timer);
|
|
738
|
+
if (this.converseTimer) clearInterval(this.converseTimer);
|
|
739
|
+
await this.pass?.catch(() => {
|
|
740
|
+
});
|
|
741
|
+
await Promise.all([...this.agents.keys()].map((id) => this.stopAndReport(id)));
|
|
742
|
+
this.writeState();
|
|
743
|
+
await this.client.close?.().catch(() => {
|
|
744
|
+
});
|
|
745
|
+
this.onClose?.();
|
|
746
|
+
}
|
|
747
|
+
/** Deletes the folders it made for deployments that are retired or gone (never one it didn't make). */
|
|
748
|
+
clean(view) {
|
|
749
|
+
const live = new Set(view.deployments.filter((d) => d.state !== "retired").map((d) => d.id));
|
|
750
|
+
const removed = [];
|
|
751
|
+
for (const [id, folder] of Object.entries(this.state.made)) {
|
|
752
|
+
if (live.has(id) || this.agents.has(id)) continue;
|
|
753
|
+
try {
|
|
754
|
+
rmSync(folder, { recursive: true, force: true });
|
|
755
|
+
removed.push(folder);
|
|
756
|
+
delete this.state.made[id];
|
|
757
|
+
this.trust(folder, false);
|
|
758
|
+
} catch {
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
this.writeState();
|
|
762
|
+
return removed;
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
var isAlive = (a) => !!a.child && a.exitCode === null && a.exitSignal === null;
|
|
766
|
+
var ended = (a) => !isAlive(a) ? Promise.resolve() : new Promise((done) => {
|
|
767
|
+
a.child.once("exit", () => done());
|
|
768
|
+
a.child.once("error", () => done());
|
|
769
|
+
});
|
|
770
|
+
var WAIT = { approval: "waiting for approval", "over-limit": "not started: over the agent limit", paused: "paused", "release-pending": "a new version waits for approval" };
|
|
771
|
+
var labelOfDeployment = (d) => d.owner && d.name ? `${d.owner}/${d.name}` : d.instructionId;
|
|
772
|
+
function fleetCli() {
|
|
773
|
+
const q = (s) => /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, `'\\''`)}'`;
|
|
774
|
+
const script = process.argv[1];
|
|
775
|
+
return script && /instruction-agents(\.js)?$|main\.(js|ts)$/.test(script) ? `${q(process.execPath)} ${q(script)}` : "npx -y @instruction.md/agents";
|
|
776
|
+
}
|
|
777
|
+
var defaultCheck = (accounts) => async (o) => {
|
|
778
|
+
const { checkSetup, needsOf } = await import("./setup.js");
|
|
779
|
+
return needsOf(await checkSetup({ hosts: [o.host], servers: o.servers, unattended: true, env: o.env, ...accounts ? { accounts } : {} }));
|
|
780
|
+
};
|
|
781
|
+
function describe(x) {
|
|
782
|
+
switch (x.kind) {
|
|
783
|
+
case "start":
|
|
784
|
+
return `start ${labelOfDeployment(x.d)} (${followUri(x.d)})`;
|
|
785
|
+
case "stop":
|
|
786
|
+
return `stop ${x.deploymentId} (${x.why})`;
|
|
787
|
+
case "restart":
|
|
788
|
+
return `restart ${labelOfDeployment(x.d)} (${x.from ?? "-"} → ${x.d.versionId ?? "-"})`;
|
|
789
|
+
case "rewrite":
|
|
790
|
+
return `point ${labelOfDeployment(x.d)} at ${followUri(x.d)}`;
|
|
791
|
+
case "wait":
|
|
792
|
+
return `leave ${labelOfDeployment(x.d)}: ${WAIT[x.why]}`;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function registryFleetClient(c, q, log = (l) => process.stderr.write(`${l}
|
|
796
|
+
`)) {
|
|
797
|
+
const call = async (name, args) => {
|
|
798
|
+
const { readResult, readFailure } = await import("instruction.md/cli/registry");
|
|
799
|
+
let r;
|
|
800
|
+
try {
|
|
801
|
+
r = await c.client.callTool({ name, arguments: args });
|
|
802
|
+
} catch (e) {
|
|
803
|
+
throw readFailure(name, e);
|
|
804
|
+
}
|
|
805
|
+
return readResult(name, r);
|
|
806
|
+
};
|
|
807
|
+
const unread = /* @__PURE__ */ new Set();
|
|
808
|
+
let seen = false;
|
|
809
|
+
let where = { ...q.org ? { org: q.org } : {}, ...q.platform ? { platform: q.platform } : {} };
|
|
810
|
+
return {
|
|
811
|
+
async view() {
|
|
812
|
+
const list = [];
|
|
813
|
+
for (let cursor, n = 0; n < 50; n++) {
|
|
814
|
+
let r;
|
|
815
|
+
try {
|
|
816
|
+
r = await call("deployments_list", { ...q.rule ? { ruleId: q.rule } : {}, ...q.platform ? { platform: q.platform } : {}, ...q.org ? { orgId: q.org } : {}, ...cursor ? { cursor } : {} });
|
|
817
|
+
} catch (e) {
|
|
818
|
+
if (q.rule && seen && e.errorCode === "not_found") return { deployments: [], gone: true };
|
|
819
|
+
throw e;
|
|
820
|
+
}
|
|
821
|
+
seen = true;
|
|
822
|
+
if (Array.isArray(r.deployments)) list.push(...r.deployments);
|
|
823
|
+
cursor = str(r.nextCursor);
|
|
824
|
+
if (!cursor) break;
|
|
825
|
+
}
|
|
826
|
+
const ids = new Set(q.rule ? [q.rule] : []);
|
|
827
|
+
for (const d of list) {
|
|
828
|
+
const rid = str(obj(d)?.ruleId) ?? str(obj(d)?.rule_id);
|
|
829
|
+
if (rid) ids.add(rid);
|
|
830
|
+
}
|
|
831
|
+
const rules = /* @__PURE__ */ new Map();
|
|
832
|
+
for (const id of ids) {
|
|
833
|
+
try {
|
|
834
|
+
const r = await call("deployments_rules_get", { ruleId: id });
|
|
835
|
+
rules.set(id, obj(r.rule) ?? r);
|
|
836
|
+
} catch (e) {
|
|
837
|
+
if (!unread.has(id)) {
|
|
838
|
+
unread.add(id);
|
|
839
|
+
log(`rule ${id}: ${e instanceof Error ? e.message : String(e)} (its deployments run with the defaults: live, no limit of the rule's)`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
const main = q.rule ? rules.get(q.rule) : void 0;
|
|
844
|
+
if (main) where = { org: where.org ?? str(main.orgId) ?? str(main.org_id), platform: where.platform ?? str(main.platform) };
|
|
845
|
+
return viewFrom(list, rules, q.rule);
|
|
846
|
+
},
|
|
847
|
+
async onChange(changed) {
|
|
848
|
+
if (!where.org || !where.platform) return false;
|
|
849
|
+
const uri = `deployment://${where.org}/${encodeURIComponent(where.platform)}`;
|
|
850
|
+
c.client.setNotificationHandler(null, (n) => {
|
|
851
|
+
if (n.params?.uri === uri) changed();
|
|
852
|
+
});
|
|
853
|
+
try {
|
|
854
|
+
await c.client.subscribeResource({ uri });
|
|
855
|
+
return true;
|
|
856
|
+
} catch {
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
},
|
|
860
|
+
async report(r) {
|
|
861
|
+
await call("deployments_report", { deploymentId: r.deploymentId, running: r.running, ...r.versionId ? { versionId: r.versionId } : {}, ...r.health ? { health: r.health } : {}, ...r.error ? { error: r.error } : {}, ...r.host ? { host: r.host } : {}, ...r.needs?.length ? { needs: r.needs } : {}, ...r.activity?.length ? { activity: r.activity } : {}, ...r.subagents ? { subagents: r.subagents } : {} });
|
|
862
|
+
},
|
|
863
|
+
converse: {
|
|
864
|
+
async ask(card) {
|
|
865
|
+
const r = await call("deployments_asks_create", card);
|
|
866
|
+
const ask = obj(r.ask) ?? {};
|
|
867
|
+
return { id: String(ask.id), expiresAt: str(ask.expiresAt) ?? null, createdAt: str(ask.createdAt) ?? null };
|
|
868
|
+
},
|
|
869
|
+
async get(askId) {
|
|
870
|
+
const ask = obj((await call("deployments_asks_get", { askId })).ask) ?? {};
|
|
871
|
+
return { state: str(ask.state) ?? "open", ...obj(ask.answer) ? { answer: obj(ask.answer) } : {}, ...str(obj(ask.answeredBy)?.name) ? { by: str(obj(ask.answeredBy)?.name) } : {} };
|
|
872
|
+
},
|
|
873
|
+
async withdraw(askId, reason) {
|
|
874
|
+
await call("deployments_asks_withdraw", { askId, reason });
|
|
875
|
+
},
|
|
876
|
+
async take(deploymentId) {
|
|
877
|
+
const r = await call("deployments_messages_list", { deploymentId, take: true });
|
|
878
|
+
return (Array.isArray(r.entries) ? r.entries : []).flatMap((e) => {
|
|
879
|
+
const x = obj(e) ?? {}, b = obj(x.body) ?? {};
|
|
880
|
+
const kind = x.kind === "control" ? "control" : x.kind === "message" ? "message" : null;
|
|
881
|
+
if (!kind) return [];
|
|
882
|
+
const control = b.control === "interrupt" || b.control === "restart" ? b.control : void 0;
|
|
883
|
+
return [{ kind, ...str(b.text) ? { text: str(b.text) } : {}, ...control ? { control } : {}, ...str(obj(x.author)?.name) ? { by: str(obj(x.author)?.name) } : {} }];
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
},
|
|
887
|
+
async machinery(follow) {
|
|
888
|
+
const { machinerySource } = await import("./setup.js");
|
|
889
|
+
return machinerySource(c, follow);
|
|
890
|
+
},
|
|
891
|
+
async close() {
|
|
892
|
+
await c.client.close?.();
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
var FLEET_USAGE = "usage: instruction-agents fleet (--rule dpr_… | --platform LABEL [--org SLUG]) [--dir D] [--max N] [--interval S] [--once] [--dry-run] [--restart-on-exit] [--clean] [--host APP] [--accounts FILE] [--no-apply] [--no-converse] [--cli CMD] [--url U] [--token T] -- <command…>";
|
|
897
|
+
function take(args, flag) {
|
|
898
|
+
const i = args.indexOf(flag);
|
|
899
|
+
if (i < 0) return void 0;
|
|
900
|
+
const v = args[i + 1];
|
|
901
|
+
args.splice(i, 2);
|
|
902
|
+
return v;
|
|
903
|
+
}
|
|
904
|
+
function has(args, flag) {
|
|
905
|
+
const i = args.indexOf(flag);
|
|
906
|
+
if (i < 0) return false;
|
|
907
|
+
args.splice(i, 1);
|
|
908
|
+
return true;
|
|
909
|
+
}
|
|
910
|
+
async function cmdFleet(argv) {
|
|
911
|
+
const sep = argv.indexOf("--");
|
|
912
|
+
const args = sep < 0 ? [...argv] : argv.slice(0, sep);
|
|
913
|
+
const command = sep < 0 ? [] : argv.slice(sep + 1);
|
|
914
|
+
const rule = take(args, "--rule"), platform = take(args, "--platform"), org = take(args, "--org");
|
|
915
|
+
const dir = resolve(take(args, "--dir") ?? "instruction-fleet");
|
|
916
|
+
const max = take(args, "--max"), interval = take(args, "--interval");
|
|
917
|
+
const once = has(args, "--once"), dryRun = has(args, "--dry-run"), restartOnExit = has(args, "--restart-on-exit"), clean = has(args, "--clean");
|
|
918
|
+
const url = take(args, "--url"), token = take(args, "--token"), host = take(args, "--host");
|
|
919
|
+
const accountsFlag = take(args, "--accounts"), noApply = has(args, "--no-apply"), cli = take(args, "--cli"), noConverse = has(args, "--no-converse");
|
|
920
|
+
if (!rule && !platform || rule && platform || org && !platform || !command.length || args.length) {
|
|
921
|
+
console.error(FLEET_USAGE);
|
|
922
|
+
return 2;
|
|
923
|
+
}
|
|
924
|
+
if (max !== void 0 && !(Number.isInteger(Number(max)) && Number(max) > 0)) {
|
|
925
|
+
console.error("--max is a whole number above 0");
|
|
926
|
+
return 2;
|
|
927
|
+
}
|
|
928
|
+
if (interval !== void 0 && !(Number(interval) >= 1)) {
|
|
929
|
+
console.error("--interval is a number of seconds, at least 1");
|
|
930
|
+
return 2;
|
|
931
|
+
}
|
|
932
|
+
const { registryConnection, readResult, CliError } = await import("instruction.md/cli/registry");
|
|
933
|
+
process.env.INSTRUCTION_MD_CLIENT_NAME ??= `instruction-md-cli fleet@${hostname()}`;
|
|
934
|
+
const conn = await registryConnection([...url ? ["--url", url] : [], ...token ? ["--token", token] : []]);
|
|
935
|
+
if (!conn) {
|
|
936
|
+
console.error("instruction-agents fleet reads your organization's deployments: sign in first (`instruction-agents login`, or --token)");
|
|
937
|
+
return 3;
|
|
938
|
+
}
|
|
939
|
+
let orgId;
|
|
940
|
+
if (org) {
|
|
941
|
+
const b = readResult("session_bootstrap", await conn.client.callTool({ name: "session_bootstrap", arguments: {} }));
|
|
942
|
+
const o = (b.orgs ?? []).find((x) => x.id === org || x.slug?.toLowerCase() === org.toLowerCase());
|
|
943
|
+
if (!o) {
|
|
944
|
+
await conn.client.close();
|
|
945
|
+
throw new CliError(`no organization ${org} among yours (${(b.orgs ?? []).map((x) => x.slug ?? x.id).join(", ") || "none"})`, 4);
|
|
946
|
+
}
|
|
947
|
+
orgId = o.id;
|
|
948
|
+
}
|
|
949
|
+
const client = registryFleetClient(conn, { ...rule ? { rule } : {}, ...platform ? { platform } : {}, ...orgId ? { org: orgId } : {} });
|
|
950
|
+
const { accountsPath, loadAccounts } = await import("./accounts.js");
|
|
951
|
+
const accountsFile = resolve(accountsFlag ?? accountsPath());
|
|
952
|
+
const fleet = new Fleet(client, {
|
|
953
|
+
dir,
|
|
954
|
+
command,
|
|
955
|
+
restartOnExit,
|
|
956
|
+
dryRun,
|
|
957
|
+
...max ? { max: Number(max) } : {},
|
|
958
|
+
...interval ? { interval: Number(interval) } : {},
|
|
959
|
+
...host ? { host } : {},
|
|
960
|
+
apply: !noApply,
|
|
961
|
+
accountsFile,
|
|
962
|
+
accounts: loadAccounts(accountsFile),
|
|
963
|
+
...cli ? { cli } : {},
|
|
964
|
+
...noConverse ? { converse: false } : {},
|
|
965
|
+
// (follow.json names the registry only when it isn't the default one)
|
|
966
|
+
...conn.url !== DEFAULT_URL ? { url: conn.url } : {}
|
|
967
|
+
});
|
|
968
|
+
const bye = () => {
|
|
969
|
+
void fleet.close().then(() => process.exit(0));
|
|
970
|
+
};
|
|
971
|
+
process.once("SIGINT", bye);
|
|
972
|
+
process.once("SIGTERM", bye);
|
|
973
|
+
try {
|
|
974
|
+
if (once || dryRun) {
|
|
975
|
+
const ok = await fleet.once();
|
|
976
|
+
if (clean && !dryRun) for (const f of fleet.clean(await client.view())) console.error(`removed ${f}`);
|
|
977
|
+
await fleet.close();
|
|
978
|
+
return ok ? 0 : 1;
|
|
979
|
+
}
|
|
980
|
+
console.error(`fleet: ${rule ? `rule ${rule}` : `platform ${platform}`}, folders in ${dir}; Ctrl-C stops every agent it started`);
|
|
981
|
+
await fleet.run();
|
|
982
|
+
return 0;
|
|
983
|
+
} finally {
|
|
984
|
+
await fleet.close();
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
export {
|
|
988
|
+
FLEET_USAGE,
|
|
989
|
+
Fleet,
|
|
990
|
+
appOf,
|
|
991
|
+
claudeArgv,
|
|
992
|
+
cmdFleet,
|
|
993
|
+
commandFor,
|
|
994
|
+
folderName,
|
|
995
|
+
followUri,
|
|
996
|
+
normalizeDeployment,
|
|
997
|
+
plan,
|
|
998
|
+
registryFleetClient,
|
|
999
|
+
staleIfBehind,
|
|
1000
|
+
viewFrom,
|
|
1001
|
+
viewOf
|
|
1002
|
+
};
|