Repository navigation
Expand file tree
/
Copy pathcli_command.ts
More file actions
346 lines (324 loc) · 10.1 KB
/
Copy pathcli_command.ts
File metadata and controls
346 lines (324 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/** Copyright (C) 2026 Theros <https://github.com/therosin> GPL-3.0-or-later */
import { Command } from "@cliffy/command";
import { dirname, resolve } from "@std/path";
import { exists } from "@std/fs";
import {
cleanupManagedDockerProject,
cleanupManagedDockerResources,
dockerRuntimeIdForProject,
DockerRuntimeManager,
expandPopulation,
listManagedDockerImages,
listManagedDockerRuns,
loadConfig,
type ManagedDockerImage,
type ManagedDockerRun,
type PopulationMember,
} from "@agent-zoo/core";
export interface DockerCliActions {
listRuns(): Promise<ManagedDockerRun[]>;
listImages(): Promise<ManagedDockerImage[]>;
cleanup(
runtimeId: string | undefined,
pruneImages: boolean,
): Promise<{
containers: readonly string[];
volumes: readonly string[];
imagesPruned: boolean;
}>;
cleanupProject(
project: string,
pruneImages: boolean,
): Promise<{
containers: readonly string[];
volumes: readonly string[];
imagesPruned: boolean;
}>;
}
const defaultDockerActions: DockerCliActions = {
listRuns: listManagedDockerRuns,
listImages: listManagedDockerImages,
cleanup: cleanupManagedDockerResources,
cleanupProject: cleanupManagedDockerProject,
};
export interface AttachActions {
/** Attach an interactive shell to the agent's chroot; resolves with the exit code. */
enter(hostName: string, agentId: string, cwd: string): Promise<number>;
}
const defaultAttachActions: AttachActions = {
enter: enterAgentChroot,
};
async function enterAgentChroot(
hostName: string,
agentId: string,
cwd: string,
): Promise<number> {
const child = new Deno.Command("docker.exe", {
args: [
"exec",
"--interactive",
"--tty",
hostName,
"runtime-host",
"chroot-exec",
agentId,
"--command-id",
crypto.randomUUID(),
"--cwd",
cwd,
"--",
"/bin/bash",
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
}).spawn();
const status = await child.status;
return status.code;
}
export function createCli(
run: (configPath: string, useUi: boolean, task?: string) => Promise<void>,
dockerActions: DockerCliActions = defaultDockerActions,
attachActions: AttachActions = defaultAttachActions,
) {
const root = new Command()
.name("agent-zoo")
.version("0.0.1")
.description("Run a configured population of persistent AI agents.")
.option("-c, --config <path:string>", "AgentZoo YAML configuration path")
.option("-t, --task <task:string>", "Run-wide objective")
.option("--no-ui", "Use line-oriented output instead of the live dashboard")
.action(async (options) => {
const configPath = await resolveConfigPath(options.config);
await run(configPath, options.ui !== false, options.task);
});
const docker = new Command()
.description("Inspect or purge labelled AgentZoo Docker resources.");
docker.command(
"list",
new Command()
.description("List managed Docker runtimes, volumes, and images.")
.action(async () => {
const [runs, images] = await Promise.all([
dockerActions.listRuns(),
dockerActions.listImages(),
]);
console.log(formatManagedDockerResources(runs, images));
}),
);
docker.command(
"purge",
new Command()
.description("Permanently remove selected managed Docker runtime state.")
.option(
"-c, --config <path:string>",
"Purge every runtime owned by this config",
)
.option("--runtime <id:string>", "Purge one managed runtime id")
.option("--all", "Purge every managed runtime")
.option(
"--prune-images",
"Also remove unused managed runtime images",
)
.action(async (options) => {
const selectors = [
options.config !== undefined,
options.runtime !== undefined,
options.all === true,
].filter(Boolean).length;
if (selectors !== 1) {
throw new Error(
"Specify exactly one of --config, --runtime, or --all",
);
}
const pruneImages = options.pruneImages ?? false;
const result = options.config
? await dockerActions.cleanupProject(
resolve(options.config),
pruneImages,
)
: await dockerActions.cleanup(
options.all ? undefined : options.runtime,
pruneImages,
);
console.log(
`Purged ${result.containers.length} container(s) and ${result.volumes.length} volume(s).`,
);
if (result.imagesPruned) console.log("Pruned unused managed images.");
}),
);
root.command("docker", docker);
const agent = new Command()
.description("Manage persistent per-agent chroot state.");
agent.command(
"purge",
new Command()
.description(
"Permanently delete one agent chroot while keeping its config runtime.",
)
.arguments("<id:string>")
.option("-c, --config <path:string>", "AgentZoo YAML configuration path")
.action(async (options, id) => {
const configPath = await resolveConfigPath(options.config);
await purgeConfiguredAgent(configPath, id, dockerActions);
console.log(`Purged persistent chroot for ${id}.`);
}),
);
root.command("agent", agent);
root.command(
"attach",
new Command()
.description(
"Open an interactive shell inside an agent's private environment.",
)
.arguments("<who:string>")
.option("-c, --config <path:string>", "AgentZoo YAML configuration path")
.action(async (options, who) => {
const configPath = await resolveConfigPath(options.config);
const code = await attachConfiguredAgent(
configPath,
who,
attachActions,
);
if (code !== 0) Deno.exit(code);
}),
);
return root;
}
async function purgeConfiguredAgent(
configPath: string,
agentId: string,
dockerActions: DockerCliActions,
): Promise<void> {
const runtimeId = dockerRuntimeIdForProject(configPath);
const runtimeState = (await dockerActions.listRuns()).find((run) =>
run.runId === runtimeId
);
if (!runtimeState) {
throw new Error(
`No persistent runtime with id "${runtimeId}" exists for config "${configPath}"`,
);
}
if (
runtimeState.containers.some((container) => container.state === "running")
) {
throw new Error(
`Stop AgentZoo for config "${configPath}" before purging an agent chroot`,
);
}
const config = await loadConfig(configPath);
const workspaceRoot = resolve(
dirname(configPath),
config.runtime.sharedWorkspace,
);
const runtime = new DockerRuntimeManager(
workspaceRoot,
undefined,
undefined,
{ project: configPath },
);
try {
await runtime.purgeEnv(agentId);
} finally {
await runtime.stop();
}
}
/** Resolve `<who>` to a configured agent; the opaque id is authoritative. */
export function resolvePopulationMember(
population: readonly PopulationMember[],
who: string,
): PopulationMember {
const byId = population.find((member) => member.id === who);
if (byId) return byId;
const bySemantic = population.filter((member) =>
member.semanticId.toLowerCase() === who.toLowerCase()
);
if (bySemantic.length === 1) return bySemantic[0];
if (bySemantic.length > 1) {
throw new Error(
`Agent id "${who}" is ambiguous; rerun with one of these opaque ids:\n` +
formatAgentCandidates(bySemantic),
);
}
const byName = population.filter((member) =>
member.name?.toLowerCase() === who.toLowerCase()
);
if (byName.length === 1) return byName[0];
if (byName.length > 1) {
throw new Error(
`Agent name "${who}" is ambiguous; rerun with one of these opaque ids:\n` +
formatAgentCandidates(byName),
);
}
throw new Error(
`No configured agent matches "${who}"; known agents:\n` +
formatAgentCandidates(population),
);
}
function formatAgentCandidates(members: readonly PopulationMember[]): string {
return members.map((member) =>
` ${member.id} (${member.semanticId}${
member.name ? `, ${member.name}` : ""
})`
).join("\n");
}
export async function attachConfiguredAgent(
configPath: string,
who: string,
attachActions: AttachActions = defaultAttachActions,
): Promise<number> {
const config = await loadConfig(configPath);
const member = resolvePopulationMember(await expandPopulation(config), who);
const workspaceRoot = resolve(
dirname(configPath),
config.runtime.sharedWorkspace,
);
const runtime = new DockerRuntimeManager(
workspaceRoot,
undefined,
undefined,
{ project: configPath },
);
// prepare() starts the container if stopped; create-agent re-mounts the
// overlay and revives agent-init after a container restart.
await runtime.createEnv(member.id);
console.log(
`Attaching to ${member.semanticId}${
member.name ? ` (${member.name})` : ""
} [${member.id}]...`,
);
return await attachActions.enter(runtime.hostName, member.id, member.cwd);
}
export function formatManagedDockerResources(
runs: readonly ManagedDockerRun[],
images: readonly ManagedDockerImage[],
): string {
const lines: string[] = [];
if (!runs.length) {
lines.push("Managed runtimes: none");
} else {
lines.push(`Managed runtimes: ${runs.length}`);
for (const run of runs) {
lines.push(` ${run.runId}`);
if (run.createdAt) lines.push(` created: ${run.createdAt}`);
if (run.project) lines.push(` config: ${run.project}`);
for (const container of run.containers) {
lines.push(
` container: ${container.name} (${container.state}, ${container.image})`,
);
}
for (const volume of run.volumes) lines.push(` volume: ${volume}`);
}
}
lines.push(`Managed images: ${images.length}`);
for (const image of images) {
lines.push(` ${image.tags.join(", ") || image.id}`);
}
return lines.join("\n");
}
export async function resolveConfigPath(requested?: string): Promise<string> {
if (requested) return resolve(requested);
const conventional = resolve("agent-zoo.yaml");
if (await exists(conventional)) return conventional;
return resolve("agent-zoo.example.yaml");
}