@zookanalytics/agent-env 0.1.1 → 0.1.2
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/bin/agent-env.js +0 -0
- package/config/baseline/post-create.sh +0 -0
- package/dist/cli.js +536 -39
- package/package.json +20 -14
package/bin/agent-env.js
CHANGED
|
File without changes
|
|
File without changes
|
package/dist/cli.js
CHANGED
|
@@ -39,7 +39,7 @@ import React2 from "react";
|
|
|
39
39
|
// package.json
|
|
40
40
|
var package_default = {
|
|
41
41
|
name: "@zookanalytics/agent-env",
|
|
42
|
-
version: "0.1.
|
|
42
|
+
version: "0.1.2",
|
|
43
43
|
description: "CLI for creating isolated, AI-ready development environments",
|
|
44
44
|
type: "module",
|
|
45
45
|
bin: {
|
|
@@ -51,6 +51,11 @@ var package_default = {
|
|
|
51
51
|
import: "./dist/cli.js"
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
|
+
repository: {
|
|
55
|
+
type: "git",
|
|
56
|
+
url: "https://github.com/zookanalytics/bmad-orchestrator",
|
|
57
|
+
directory: "packages/agent-env"
|
|
58
|
+
},
|
|
54
59
|
engines: {
|
|
55
60
|
node: ">=20"
|
|
56
61
|
},
|
|
@@ -73,6 +78,7 @@ var package_default = {
|
|
|
73
78
|
},
|
|
74
79
|
dependencies: {
|
|
75
80
|
"@inkjs/ui": "^2.0.0",
|
|
81
|
+
chalk: "^5.3.0",
|
|
76
82
|
commander: "^14.0.2",
|
|
77
83
|
execa: "^9.6.1",
|
|
78
84
|
ink: "^6.6.0",
|
|
@@ -100,6 +106,8 @@ import { homedir as homedir3 } from "os";
|
|
|
100
106
|
var DEVCONTAINER_UP_TIMEOUT = 12e4;
|
|
101
107
|
var DOCKER_INSPECT_TIMEOUT = 1e4;
|
|
102
108
|
var DOCKER_INFO_TIMEOUT = 5e3;
|
|
109
|
+
var DOCKER_STOP_TIMEOUT = 3e4;
|
|
110
|
+
var DOCKER_RM_TIMEOUT = 1e4;
|
|
103
111
|
function createContainerLifecycle(executor = createExecutor()) {
|
|
104
112
|
async function isDockerAvailable() {
|
|
105
113
|
const result = await executor("docker", ["info"], { timeout: DOCKER_INFO_TIMEOUT });
|
|
@@ -219,11 +227,50 @@ function createContainerLifecycle(executor = createExecutor()) {
|
|
|
219
227
|
containerId
|
|
220
228
|
};
|
|
221
229
|
}
|
|
230
|
+
async function containerStop(containerName) {
|
|
231
|
+
const result = await executor("docker", ["stop", containerName], {
|
|
232
|
+
timeout: DOCKER_STOP_TIMEOUT
|
|
233
|
+
});
|
|
234
|
+
if (result.ok) {
|
|
235
|
+
return { ok: true };
|
|
236
|
+
}
|
|
237
|
+
if (result.stderr.includes("No such") || result.stderr.includes("not found") || result.stderr.includes("is not running")) {
|
|
238
|
+
return { ok: true };
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
ok: false,
|
|
242
|
+
error: {
|
|
243
|
+
code: "CONTAINER_STOP_TIMEOUT",
|
|
244
|
+
message: `Failed to stop container '${containerName}': ${result.stderr}`,
|
|
245
|
+
suggestion: `Try manually: docker rm -f ${containerName}`
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
async function containerRemove(containerName) {
|
|
250
|
+
const result = await executor("docker", ["rm", containerName], {
|
|
251
|
+
timeout: DOCKER_RM_TIMEOUT
|
|
252
|
+
});
|
|
253
|
+
if (result.ok) {
|
|
254
|
+
return { ok: true };
|
|
255
|
+
}
|
|
256
|
+
if (result.stderr.includes("No such") || result.stderr.includes("not found")) {
|
|
257
|
+
return { ok: true };
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
ok: false,
|
|
261
|
+
error: {
|
|
262
|
+
code: "CONTAINER_ERROR",
|
|
263
|
+
message: `Failed to remove container '${containerName}': ${result.stderr}`
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
222
267
|
return {
|
|
223
268
|
isDockerAvailable,
|
|
224
269
|
containerStatus,
|
|
225
270
|
getContainerNameById,
|
|
226
|
-
devcontainerUp
|
|
271
|
+
devcontainerUp,
|
|
272
|
+
containerStop,
|
|
273
|
+
containerRemove
|
|
227
274
|
};
|
|
228
275
|
}
|
|
229
276
|
|
|
@@ -235,12 +282,12 @@ import {
|
|
|
235
282
|
readdir as readdir3,
|
|
236
283
|
readFile as readFile3,
|
|
237
284
|
rename as rename2,
|
|
238
|
-
rm,
|
|
285
|
+
rm as rm2,
|
|
239
286
|
stat as stat3,
|
|
240
287
|
writeFile as writeFile3
|
|
241
288
|
} from "fs/promises";
|
|
242
289
|
import { homedir as homedir2 } from "os";
|
|
243
|
-
import { dirname as
|
|
290
|
+
import { dirname as dirname2 } from "path";
|
|
244
291
|
|
|
245
292
|
// src/lib/devcontainer.ts
|
|
246
293
|
import { access, cp, mkdir, readdir, readFile, stat, writeFile } from "fs/promises";
|
|
@@ -302,7 +349,7 @@ async function patchContainerName(workspacePath, containerName, deps = defaultFs
|
|
|
302
349
|
|
|
303
350
|
// src/lib/state.ts
|
|
304
351
|
import { appendFile, mkdir as mkdir2, readFile as readFile2, rename, writeFile as writeFile2 } from "fs/promises";
|
|
305
|
-
import {
|
|
352
|
+
import { join as join2 } from "path";
|
|
306
353
|
|
|
307
354
|
// src/lib/types.ts
|
|
308
355
|
var AGENT_ENV_DIR = ".agent-env";
|
|
@@ -339,8 +386,8 @@ async function readState(wsPath, deps = defaultStateFsDeps) {
|
|
|
339
386
|
}
|
|
340
387
|
}
|
|
341
388
|
async function writeStateAtomic(wsPath, state, deps = defaultStateFsDeps) {
|
|
342
|
-
const tmpPath = join2(
|
|
343
|
-
await deps.mkdir(
|
|
389
|
+
const tmpPath = join2(wsPath.agentEnvDir, STATE_FILE_TMP);
|
|
390
|
+
await deps.mkdir(wsPath.agentEnvDir, { recursive: true });
|
|
344
391
|
const content = JSON.stringify(state, null, 2) + "\n";
|
|
345
392
|
await deps.writeFile(tmpPath, content, "utf-8");
|
|
346
393
|
await deps.rename(tmpPath, wsPath.stateFile);
|
|
@@ -382,7 +429,7 @@ async function ensureGitExclude(workspaceRoot, deps = defaultStateFsDeps) {
|
|
|
382
429
|
}
|
|
383
430
|
|
|
384
431
|
// src/lib/workspace.ts
|
|
385
|
-
import { mkdir as mkdir3, readdir as readdir2, stat as stat2 } from "fs/promises";
|
|
432
|
+
import { mkdir as mkdir3, readdir as readdir2, rm, stat as stat2 } from "fs/promises";
|
|
386
433
|
import { homedir } from "os";
|
|
387
434
|
import { join as join3 } from "path";
|
|
388
435
|
var defaultFsDeps2 = { mkdir: mkdir3, readdir: readdir2, stat: stat2, homedir };
|
|
@@ -445,6 +492,10 @@ async function scanWorkspaces(deps = defaultFsDeps2) {
|
|
|
445
492
|
throw err;
|
|
446
493
|
}
|
|
447
494
|
}
|
|
495
|
+
var defaultDeleteFsDeps = { rm };
|
|
496
|
+
async function deleteWorkspace(wsPath, deps = defaultDeleteFsDeps) {
|
|
497
|
+
await deps.rm(wsPath.root, { recursive: true, force: true });
|
|
498
|
+
}
|
|
448
499
|
|
|
449
500
|
// src/lib/create-instance.ts
|
|
450
501
|
var GIT_CLONE_TIMEOUT = 12e4;
|
|
@@ -518,7 +569,7 @@ function createDefaultDeps() {
|
|
|
518
569
|
workspaceFsDeps: { mkdir: mkdir4, readdir: readdir3, stat: stat3, homedir: homedir2 },
|
|
519
570
|
stateFsDeps: { readFile: readFile3, writeFile: writeFile3, rename: rename2, mkdir: mkdir4, appendFile: appendFile2 },
|
|
520
571
|
devcontainerFsDeps: { cp: cp2, mkdir: mkdir4, readFile: readFile3, stat: stat3, writeFile: writeFile3 },
|
|
521
|
-
rm
|
|
572
|
+
rm: rm2
|
|
522
573
|
};
|
|
523
574
|
}
|
|
524
575
|
async function createInstance(instanceName, repoUrl, deps) {
|
|
@@ -548,7 +599,18 @@ async function createInstance(instanceName, repoUrl, deps) {
|
|
|
548
599
|
}
|
|
549
600
|
};
|
|
550
601
|
}
|
|
551
|
-
|
|
602
|
+
try {
|
|
603
|
+
await deps.workspaceFsDeps.mkdir(dirname2(wsPath.root), { recursive: true });
|
|
604
|
+
} catch (err) {
|
|
605
|
+
return {
|
|
606
|
+
ok: false,
|
|
607
|
+
error: {
|
|
608
|
+
code: "WORKSPACE_ERROR",
|
|
609
|
+
message: `Failed to create workspace directory: ${err instanceof Error ? err.message : String(err)}`,
|
|
610
|
+
suggestion: "Check filesystem permissions for the ~/.agent-env directory."
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
}
|
|
552
614
|
const cloneResult = await deps.executor("git", ["clone", repoUrl, wsPath.root], {
|
|
553
615
|
timeout: GIT_CLONE_TIMEOUT
|
|
554
616
|
});
|
|
@@ -563,7 +625,19 @@ async function createInstance(instanceName, repoUrl, deps) {
|
|
|
563
625
|
}
|
|
564
626
|
};
|
|
565
627
|
}
|
|
566
|
-
|
|
628
|
+
try {
|
|
629
|
+
await deps.workspaceFsDeps.mkdir(wsPath.agentEnvDir, { recursive: true });
|
|
630
|
+
} catch (err) {
|
|
631
|
+
await safeRollback(wsPath.root, deps.rm);
|
|
632
|
+
return {
|
|
633
|
+
ok: false,
|
|
634
|
+
error: {
|
|
635
|
+
code: "WORKSPACE_ERROR",
|
|
636
|
+
message: `Failed to create .agent-env directory: ${err instanceof Error ? err.message : String(err)}`,
|
|
637
|
+
suggestion: "Check filesystem permissions in the cloned workspace directory."
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
567
641
|
const hasConfig = await hasDevcontainerConfig(wsPath.root, deps.devcontainerFsDeps);
|
|
568
642
|
if (!hasConfig) {
|
|
569
643
|
try {
|
|
@@ -654,7 +728,7 @@ async function findWorkspaceByName(instanceName, deps) {
|
|
|
654
728
|
}
|
|
655
729
|
return { found: false, reason: "not-found" };
|
|
656
730
|
}
|
|
657
|
-
async function attachInstance(instanceName, deps, onContainerStarting) {
|
|
731
|
+
async function attachInstance(instanceName, deps, onContainerStarting, onAttaching) {
|
|
658
732
|
const lookup = await findWorkspaceByName(instanceName, deps.workspaceFsDeps);
|
|
659
733
|
if (!lookup.found) {
|
|
660
734
|
if (lookup.reason === "ambiguous") {
|
|
@@ -714,6 +788,7 @@ async function attachInstance(instanceName, deps, onContainerStarting) {
|
|
|
714
788
|
};
|
|
715
789
|
}
|
|
716
790
|
}
|
|
791
|
+
onAttaching?.();
|
|
717
792
|
const attachResult = await attachToInstance(containerName, deps.executor);
|
|
718
793
|
if (!attachResult.ok) {
|
|
719
794
|
return attachResult;
|
|
@@ -729,9 +804,17 @@ async function attachInstance(instanceName, deps, onContainerStarting) {
|
|
|
729
804
|
// src/commands/attach.ts
|
|
730
805
|
var attachCommand = new Command("attach").description("Attach to an instance's tmux session").argument("<name>", "Instance name to attach to").action(async (name) => {
|
|
731
806
|
const deps = createAttachDefaultDeps();
|
|
732
|
-
const result = await attachInstance(
|
|
733
|
-
|
|
734
|
-
|
|
807
|
+
const result = await attachInstance(
|
|
808
|
+
name,
|
|
809
|
+
deps,
|
|
810
|
+
() => {
|
|
811
|
+
console.log("Starting container...");
|
|
812
|
+
},
|
|
813
|
+
() => {
|
|
814
|
+
console.log();
|
|
815
|
+
console.log("Attaching to tmux session...");
|
|
816
|
+
}
|
|
817
|
+
);
|
|
735
818
|
if (!result.ok) {
|
|
736
819
|
const { code, message, suggestion } = result.error;
|
|
737
820
|
console.error(formatError(createError(code, message, suggestion)));
|
|
@@ -951,6 +1034,7 @@ var createCommand = new Command3("create").description("Create a new isolated de
|
|
|
951
1034
|
console.log(` Workspace: ${workspacePath.root}`);
|
|
952
1035
|
console.log(` Container: ${containerName}`);
|
|
953
1036
|
if (options.attach) {
|
|
1037
|
+
console.log();
|
|
954
1038
|
console.log("Attaching to instance...");
|
|
955
1039
|
const attachResult = await attachToInstance(containerName, deps.executor);
|
|
956
1040
|
if (!attachResult.ok) {
|
|
@@ -1120,17 +1204,22 @@ function createGitStateDetector(executor = createExecutor()) {
|
|
|
1120
1204
|
}
|
|
1121
1205
|
};
|
|
1122
1206
|
}
|
|
1123
|
-
const { hasStaged, hasUnstaged, hasUntracked } = parseStatus(status.stdout);
|
|
1124
|
-
const stashCount =
|
|
1125
|
-
const { unpushedBranches, neverPushedBranches } = parseBranches(branch);
|
|
1207
|
+
const { hasStaged, stagedCount, hasUnstaged, unstagedCount, hasUntracked, untrackedCount } = parseStatus(status.stdout);
|
|
1208
|
+
const { stashCount, firstStashMessage } = parseStash(stash);
|
|
1209
|
+
const { unpushedBranches, unpushedCommitCounts, neverPushedBranches } = parseBranches(branch);
|
|
1126
1210
|
const isDetachedHead = !head || !head.ok;
|
|
1127
1211
|
const isClean = !hasStaged && !hasUnstaged && !hasUntracked && stashCount === 0 && unpushedBranches.length === 0 && neverPushedBranches.length === 0 && !isDetachedHead;
|
|
1128
1212
|
const state = {
|
|
1129
1213
|
hasStaged,
|
|
1214
|
+
stagedCount,
|
|
1130
1215
|
hasUnstaged,
|
|
1216
|
+
unstagedCount,
|
|
1131
1217
|
hasUntracked,
|
|
1218
|
+
untrackedCount,
|
|
1132
1219
|
stashCount,
|
|
1220
|
+
firstStashMessage,
|
|
1133
1221
|
unpushedBranches,
|
|
1222
|
+
unpushedCommitCounts,
|
|
1134
1223
|
neverPushedBranches,
|
|
1135
1224
|
isDetachedHead,
|
|
1136
1225
|
isClean
|
|
@@ -1140,40 +1229,64 @@ function createGitStateDetector(executor = createExecutor()) {
|
|
|
1140
1229
|
return { getGitState };
|
|
1141
1230
|
}
|
|
1142
1231
|
function parseStatus(stdout) {
|
|
1143
|
-
let
|
|
1144
|
-
let
|
|
1145
|
-
let
|
|
1232
|
+
let stagedCount = 0;
|
|
1233
|
+
let unstagedCount = 0;
|
|
1234
|
+
let untrackedCount = 0;
|
|
1146
1235
|
if (!stdout.trim()) {
|
|
1147
|
-
return {
|
|
1236
|
+
return {
|
|
1237
|
+
hasStaged: false,
|
|
1238
|
+
stagedCount: 0,
|
|
1239
|
+
hasUnstaged: false,
|
|
1240
|
+
unstagedCount: 0,
|
|
1241
|
+
hasUntracked: false,
|
|
1242
|
+
untrackedCount: 0
|
|
1243
|
+
};
|
|
1148
1244
|
}
|
|
1149
1245
|
for (const line of stdout.split("\n")) {
|
|
1150
1246
|
if (!line || line.length < 2) continue;
|
|
1151
1247
|
if (line.startsWith("??")) {
|
|
1152
|
-
|
|
1248
|
+
untrackedCount++;
|
|
1249
|
+
continue;
|
|
1250
|
+
}
|
|
1251
|
+
if (line.startsWith("!!")) {
|
|
1153
1252
|
continue;
|
|
1154
1253
|
}
|
|
1155
1254
|
const indexStatus = line[0];
|
|
1156
1255
|
const workTreeStatus = line[1];
|
|
1157
1256
|
if ("MADRC".includes(indexStatus)) {
|
|
1158
|
-
|
|
1257
|
+
stagedCount++;
|
|
1159
1258
|
}
|
|
1160
1259
|
if (workTreeStatus !== " ") {
|
|
1161
|
-
|
|
1260
|
+
unstagedCount++;
|
|
1162
1261
|
}
|
|
1163
1262
|
}
|
|
1164
|
-
return {
|
|
1263
|
+
return {
|
|
1264
|
+
hasStaged: stagedCount > 0,
|
|
1265
|
+
stagedCount,
|
|
1266
|
+
hasUnstaged: unstagedCount > 0,
|
|
1267
|
+
unstagedCount,
|
|
1268
|
+
hasUntracked: untrackedCount > 0,
|
|
1269
|
+
untrackedCount
|
|
1270
|
+
};
|
|
1165
1271
|
}
|
|
1166
|
-
function
|
|
1272
|
+
function parseStash(result) {
|
|
1167
1273
|
if (!result || !result.ok || !result.stdout.trim()) {
|
|
1168
|
-
return 0;
|
|
1274
|
+
return { stashCount: 0, firstStashMessage: "" };
|
|
1275
|
+
}
|
|
1276
|
+
const lines = result.stdout.trim().split("\n");
|
|
1277
|
+
let firstStashMessage = "";
|
|
1278
|
+
if (lines[0]) {
|
|
1279
|
+
const colonIndex = lines[0].indexOf(": ");
|
|
1280
|
+
firstStashMessage = colonIndex !== -1 ? lines[0].slice(colonIndex + 2) : lines[0];
|
|
1169
1281
|
}
|
|
1170
|
-
return
|
|
1282
|
+
return { stashCount: lines.length, firstStashMessage };
|
|
1171
1283
|
}
|
|
1172
1284
|
function parseBranches(result) {
|
|
1173
1285
|
const unpushedBranches = [];
|
|
1286
|
+
const unpushedCommitCounts = {};
|
|
1174
1287
|
const neverPushedBranches = [];
|
|
1175
1288
|
if (!result || !result.ok || !result.stdout.trim()) {
|
|
1176
|
-
return { unpushedBranches, neverPushedBranches };
|
|
1289
|
+
return { unpushedBranches, unpushedCommitCounts, neverPushedBranches };
|
|
1177
1290
|
}
|
|
1178
1291
|
for (const line of result.stdout.trim().split("\n")) {
|
|
1179
1292
|
if (!line.trim()) continue;
|
|
@@ -1183,10 +1296,13 @@ function parseBranches(result) {
|
|
|
1183
1296
|
if (parts.length === 1) {
|
|
1184
1297
|
neverPushedBranches.push(branchName);
|
|
1185
1298
|
} else if (trackInfo.includes("[ahead")) {
|
|
1299
|
+
const aheadMatch = trackInfo.match(/\[ahead (\d+)/);
|
|
1300
|
+
const count = aheadMatch ? parseInt(aheadMatch[1], 10) : 0;
|
|
1186
1301
|
unpushedBranches.push(branchName);
|
|
1302
|
+
unpushedCommitCounts[branchName] = count;
|
|
1187
1303
|
}
|
|
1188
1304
|
}
|
|
1189
|
-
return { unpushedBranches, neverPushedBranches };
|
|
1305
|
+
return { unpushedBranches, unpushedCommitCounts, neverPushedBranches };
|
|
1190
1306
|
}
|
|
1191
1307
|
|
|
1192
1308
|
// src/lib/list-instances.ts
|
|
@@ -1373,18 +1489,399 @@ var purposeCommand = new Command5("purpose").description("Get or set the purpose
|
|
|
1373
1489
|
});
|
|
1374
1490
|
|
|
1375
1491
|
// src/commands/remove.ts
|
|
1492
|
+
import chalk2 from "chalk";
|
|
1376
1493
|
import { Command as Command6 } from "commander";
|
|
1377
|
-
|
|
1494
|
+
import { createInterface } from "readline";
|
|
1495
|
+
|
|
1496
|
+
// src/lib/audit-log.ts
|
|
1497
|
+
import { appendFile as appendFile5, mkdir as mkdir7 } from "fs/promises";
|
|
1498
|
+
import { homedir as homedir5 } from "os";
|
|
1499
|
+
import { join as join4 } from "path";
|
|
1500
|
+
function createAuditLogDefaultDeps() {
|
|
1501
|
+
return { appendFile: appendFile5, mkdir: mkdir7, homedir: homedir5 };
|
|
1502
|
+
}
|
|
1503
|
+
function getAuditLogPath(deps) {
|
|
1504
|
+
return join4(deps.homedir(), AGENT_ENV_DIR, "audit.log");
|
|
1505
|
+
}
|
|
1506
|
+
async function writeAuditLogEntry(entry, deps = createAuditLogDefaultDeps()) {
|
|
1507
|
+
const logPath = getAuditLogPath(deps);
|
|
1508
|
+
await deps.mkdir(join4(deps.homedir(), AGENT_ENV_DIR), { recursive: true });
|
|
1509
|
+
const line = JSON.stringify(entry) + "\n";
|
|
1510
|
+
await deps.appendFile(logPath, line, "utf-8");
|
|
1511
|
+
}
|
|
1512
|
+
function createAuditEntry(instanceName, gitState, confirmationMethod) {
|
|
1513
|
+
return {
|
|
1514
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1515
|
+
action: "force-remove",
|
|
1516
|
+
instanceName,
|
|
1517
|
+
gitState,
|
|
1518
|
+
confirmationMethod
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
// src/lib/remove-instance.ts
|
|
1523
|
+
import { mkdir as mkdir8, readdir as readdir6, readFile as readFile6, rm as rm3, stat as stat6 } from "fs/promises";
|
|
1524
|
+
import { homedir as homedir6 } from "os";
|
|
1525
|
+
function createRemoveDefaultDeps() {
|
|
1526
|
+
const executor = createExecutor();
|
|
1527
|
+
return {
|
|
1528
|
+
executor,
|
|
1529
|
+
container: createContainerLifecycle(executor),
|
|
1530
|
+
gitDetector: createGitStateDetector(executor),
|
|
1531
|
+
workspaceFsDeps: { mkdir: mkdir8, readdir: readdir6, stat: stat6, homedir: homedir6 },
|
|
1532
|
+
stateFsDeps: { readFile: readFile6 },
|
|
1533
|
+
deleteFsDeps: { rm: rm3 }
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1536
|
+
function evaluateSafetyChecks(gitState) {
|
|
1537
|
+
const blockers = [];
|
|
1538
|
+
if (gitState.hasStaged) {
|
|
1539
|
+
const count = gitState.stagedCount;
|
|
1540
|
+
blockers.push(`${count} staged ${count === 1 ? "file" : "files"} detected`);
|
|
1541
|
+
}
|
|
1542
|
+
if (gitState.hasUnstaged) {
|
|
1543
|
+
const count = gitState.unstagedCount;
|
|
1544
|
+
blockers.push(`${count} unstaged ${count === 1 ? "change" : "changes"} detected`);
|
|
1545
|
+
}
|
|
1546
|
+
if (gitState.hasUntracked) {
|
|
1547
|
+
const count = gitState.untrackedCount;
|
|
1548
|
+
blockers.push(`${count} untracked ${count === 1 ? "file" : "files"} detected`);
|
|
1549
|
+
}
|
|
1550
|
+
if (gitState.stashCount > 0) {
|
|
1551
|
+
const stashLabel = `${gitState.stashCount} stash${gitState.stashCount === 1 ? "" : "es"}`;
|
|
1552
|
+
const message = gitState.firstStashMessage ? ` \u2014 "${gitState.firstStashMessage}"` : "";
|
|
1553
|
+
blockers.push(`stashed changes detected (${stashLabel}${message})`);
|
|
1554
|
+
}
|
|
1555
|
+
if (gitState.unpushedBranches.length > 0) {
|
|
1556
|
+
const branchDetails = gitState.unpushedBranches.map((b) => {
|
|
1557
|
+
const count = gitState.unpushedCommitCounts[b];
|
|
1558
|
+
return count ? `${b} (${count} ${count === 1 ? "commit" : "commits"})` : b;
|
|
1559
|
+
}).join(", ");
|
|
1560
|
+
blockers.push(`unpushed commits on branches: ${branchDetails}`);
|
|
1561
|
+
}
|
|
1562
|
+
if (gitState.neverPushedBranches.length > 0) {
|
|
1563
|
+
blockers.push(`branches never pushed: ${gitState.neverPushedBranches.join(", ")}`);
|
|
1564
|
+
}
|
|
1565
|
+
if (gitState.isDetachedHead) {
|
|
1566
|
+
blockers.push("detached HEAD state (investigate manually)");
|
|
1567
|
+
}
|
|
1568
|
+
return blockers;
|
|
1569
|
+
}
|
|
1570
|
+
async function removeInstance(instanceName, deps, force = false) {
|
|
1571
|
+
const lookup = await findWorkspaceByName(instanceName, deps.workspaceFsDeps);
|
|
1572
|
+
if (!lookup.found) {
|
|
1573
|
+
if (lookup.reason === "ambiguous") {
|
|
1574
|
+
return {
|
|
1575
|
+
ok: false,
|
|
1576
|
+
error: {
|
|
1577
|
+
code: "AMBIGUOUS_MATCH",
|
|
1578
|
+
message: `Multiple instances match '${instanceName}': ${lookup.matches.join(", ")}`,
|
|
1579
|
+
suggestion: "Use the full workspace name to specify which instance."
|
|
1580
|
+
}
|
|
1581
|
+
};
|
|
1582
|
+
}
|
|
1583
|
+
return {
|
|
1584
|
+
ok: false,
|
|
1585
|
+
error: {
|
|
1586
|
+
code: "WORKSPACE_NOT_FOUND",
|
|
1587
|
+
message: `Instance '${instanceName}' not found`,
|
|
1588
|
+
suggestion: "Use `agent-env list` to see available instances."
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
}
|
|
1592
|
+
const wsPath = getWorkspacePathByName(lookup.workspaceName, deps.workspaceFsDeps);
|
|
1593
|
+
const state = await readState(wsPath, deps.stateFsDeps);
|
|
1594
|
+
const containerName = state.containerName;
|
|
1595
|
+
const dockerAvailable = await deps.container.isDockerAvailable();
|
|
1596
|
+
if (!dockerAvailable) {
|
|
1597
|
+
return {
|
|
1598
|
+
ok: false,
|
|
1599
|
+
error: {
|
|
1600
|
+
code: "ORBSTACK_REQUIRED",
|
|
1601
|
+
message: "Docker is not available. OrbStack or Docker Desktop must be running.",
|
|
1602
|
+
suggestion: "Start OrbStack or Docker Desktop, then try again."
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
}
|
|
1606
|
+
const gitResult = await deps.gitDetector.getGitState(wsPath.root);
|
|
1607
|
+
let detectedGitState;
|
|
1608
|
+
let detectedBlockers;
|
|
1609
|
+
if (!gitResult.ok) {
|
|
1610
|
+
if (!force) {
|
|
1611
|
+
return {
|
|
1612
|
+
ok: false,
|
|
1613
|
+
error: {
|
|
1614
|
+
code: "GIT_ERROR",
|
|
1615
|
+
message: `Failed to check git state: ${gitResult.error.message}`,
|
|
1616
|
+
suggestion: "Ensure the workspace contains a valid git repository."
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
}
|
|
1620
|
+
} else {
|
|
1621
|
+
detectedGitState = gitResult.state;
|
|
1622
|
+
const blockers = evaluateSafetyChecks(gitResult.state);
|
|
1623
|
+
if (blockers.length > 0) {
|
|
1624
|
+
detectedBlockers = blockers;
|
|
1625
|
+
if (!force) {
|
|
1626
|
+
return {
|
|
1627
|
+
ok: false,
|
|
1628
|
+
error: {
|
|
1629
|
+
code: "SAFETY_CHECK_FAILED",
|
|
1630
|
+
message: "Safety checks failed",
|
|
1631
|
+
suggestion: "Resolve the issues above, or use --force to bypass safety checks."
|
|
1632
|
+
},
|
|
1633
|
+
gitState: gitResult.state,
|
|
1634
|
+
blockers
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
let containerCleanupFailed = false;
|
|
1640
|
+
const stopResult = await deps.container.containerStop(containerName);
|
|
1641
|
+
if (!stopResult.ok) {
|
|
1642
|
+
if (force) {
|
|
1643
|
+
console.error(
|
|
1644
|
+
`Warning: Failed to stop container ${containerName}: ${stopResult.error.message}`
|
|
1645
|
+
);
|
|
1646
|
+
containerCleanupFailed = true;
|
|
1647
|
+
} else {
|
|
1648
|
+
return {
|
|
1649
|
+
ok: false,
|
|
1650
|
+
error: stopResult.error
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
const removeResult = await deps.container.containerRemove(containerName);
|
|
1655
|
+
if (!removeResult.ok) {
|
|
1656
|
+
if (force) {
|
|
1657
|
+
console.error(
|
|
1658
|
+
`Warning: Failed to remove container ${containerName}: ${removeResult.error.message}`
|
|
1659
|
+
);
|
|
1660
|
+
containerCleanupFailed = true;
|
|
1661
|
+
} else {
|
|
1662
|
+
return {
|
|
1663
|
+
ok: false,
|
|
1664
|
+
error: removeResult.error
|
|
1665
|
+
};
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
try {
|
|
1669
|
+
await deleteWorkspace(wsPath, deps.deleteFsDeps);
|
|
1670
|
+
} catch (err) {
|
|
1671
|
+
return {
|
|
1672
|
+
ok: false,
|
|
1673
|
+
error: {
|
|
1674
|
+
code: "WORKSPACE_DELETE_FAILED",
|
|
1675
|
+
message: `Failed to delete workspace: ${err instanceof Error ? err.message : String(err)}`,
|
|
1676
|
+
suggestion: `Container was removed but workspace remains at ${wsPath.root}. Remove manually with: rm -rf ${wsPath.root}`
|
|
1677
|
+
}
|
|
1678
|
+
};
|
|
1679
|
+
}
|
|
1680
|
+
if (force) {
|
|
1681
|
+
return {
|
|
1682
|
+
ok: true,
|
|
1683
|
+
forced: true,
|
|
1684
|
+
gitState: detectedGitState,
|
|
1685
|
+
blockers: detectedBlockers,
|
|
1686
|
+
containerCleanupFailed
|
|
1687
|
+
};
|
|
1688
|
+
}
|
|
1689
|
+
return { ok: true };
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
// src/lib/safety-report.ts
|
|
1693
|
+
import chalk from "chalk";
|
|
1694
|
+
var BLOCKER_NEVER_PUSHED = "never pushed";
|
|
1695
|
+
var BLOCKER_DETACHED_HEAD = "detached HEAD";
|
|
1696
|
+
function classifyBlocker(blocker) {
|
|
1697
|
+
if (blocker.includes(BLOCKER_NEVER_PUSHED) || blocker.includes(BLOCKER_DETACHED_HEAD)) {
|
|
1698
|
+
return "Danger";
|
|
1699
|
+
}
|
|
1700
|
+
return "Warning";
|
|
1701
|
+
}
|
|
1702
|
+
var SUGGESTION_CONFIG = [
|
|
1703
|
+
{
|
|
1704
|
+
condition: (gitState) => gitState.hasStaged,
|
|
1705
|
+
generator: () => "Run `git commit` to commit staged changes"
|
|
1706
|
+
},
|
|
1707
|
+
{
|
|
1708
|
+
condition: (gitState) => gitState.hasUnstaged,
|
|
1709
|
+
generator: () => "Run `git add` and `git commit` to save unstaged changes"
|
|
1710
|
+
},
|
|
1711
|
+
{
|
|
1712
|
+
condition: (gitState) => gitState.hasUntracked,
|
|
1713
|
+
generator: () => "Run `git add` to track untracked files, then commit"
|
|
1714
|
+
},
|
|
1715
|
+
{
|
|
1716
|
+
condition: (gitState) => gitState.stashCount > 0,
|
|
1717
|
+
generator: () => "Run `git stash pop` to apply stashed changes, or `git stash drop` to discard them"
|
|
1718
|
+
},
|
|
1719
|
+
{
|
|
1720
|
+
condition: (gitState) => gitState.unpushedBranches.length > 0,
|
|
1721
|
+
generator: (gitState) => `Run \`git push\` to push unpushed commits on: ${gitState.unpushedBranches.join(", ")}`
|
|
1722
|
+
},
|
|
1723
|
+
{
|
|
1724
|
+
condition: (gitState) => gitState.neverPushedBranches.length > 0,
|
|
1725
|
+
generator: (gitState) => `Run \`git push -u origin <branch>\` for never-pushed branches: ${gitState.neverPushedBranches.join(", ")}`
|
|
1726
|
+
},
|
|
1727
|
+
{
|
|
1728
|
+
condition: (gitState) => gitState.isDetachedHead,
|
|
1729
|
+
generator: () => "Run `git checkout <branch>` to reattach HEAD, or `git checkout -b <new-branch>` to save work"
|
|
1730
|
+
}
|
|
1731
|
+
];
|
|
1732
|
+
function getSuggestions(gitState) {
|
|
1733
|
+
return SUGGESTION_CONFIG.filter((entry) => entry.condition(gitState)).map(
|
|
1734
|
+
(entry) => entry.generator(gitState)
|
|
1735
|
+
);
|
|
1736
|
+
}
|
|
1737
|
+
function formatSafetyReport(instanceName, blockers, gitState) {
|
|
1738
|
+
const lines = [];
|
|
1739
|
+
lines.push(`Cannot remove '${instanceName}' \u2014 safety checks failed:`);
|
|
1740
|
+
lines.push("");
|
|
1741
|
+
const items = blockers.map((blocker) => ({
|
|
1742
|
+
severity: classifyBlocker(blocker),
|
|
1743
|
+
message: blocker
|
|
1744
|
+
}));
|
|
1745
|
+
for (const item of items) {
|
|
1746
|
+
const prefix = item.severity === "Danger" ? chalk.red("[Danger]") : chalk.yellow("[Warning]");
|
|
1747
|
+
lines.push(` ${prefix} ${item.message}`);
|
|
1748
|
+
}
|
|
1749
|
+
const suggestions = getSuggestions(gitState);
|
|
1750
|
+
if (suggestions.length > 0) {
|
|
1751
|
+
lines.push("");
|
|
1752
|
+
lines.push(chalk.bold("Suggestions:"));
|
|
1753
|
+
for (const suggestion of suggestions) {
|
|
1754
|
+
lines.push(` - ${suggestion}`);
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
lines.push("");
|
|
1758
|
+
lines.push(chalk.gray("Or use --force to bypass safety checks (data loss is permanent)."));
|
|
1759
|
+
return lines.join("\n");
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
// src/commands/remove.ts
|
|
1763
|
+
function promptForConfirmation(instanceName) {
|
|
1764
|
+
return new Promise((resolve) => {
|
|
1765
|
+
let resolved = false;
|
|
1766
|
+
const rl = createInterface({
|
|
1767
|
+
input: process.stdin,
|
|
1768
|
+
output: process.stderr
|
|
1769
|
+
});
|
|
1770
|
+
rl.question(`
|
|
1771
|
+
Type the instance name "${instanceName}" to confirm: `, (answer) => {
|
|
1772
|
+
if (!resolved) {
|
|
1773
|
+
resolved = true;
|
|
1774
|
+
rl.close();
|
|
1775
|
+
resolve(answer);
|
|
1776
|
+
}
|
|
1777
|
+
});
|
|
1778
|
+
rl.on("close", () => {
|
|
1779
|
+
if (!resolved) {
|
|
1780
|
+
resolved = true;
|
|
1781
|
+
resolve(null);
|
|
1782
|
+
}
|
|
1783
|
+
});
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
async function writeAuditLog(name, gitState, confirmationMethod) {
|
|
1787
|
+
try {
|
|
1788
|
+
const auditDeps = createAuditLogDefaultDeps();
|
|
1789
|
+
const entry = createAuditEntry(name, gitState, confirmationMethod);
|
|
1790
|
+
await writeAuditLogEntry(entry, auditDeps);
|
|
1791
|
+
} catch {
|
|
1792
|
+
console.error("Warning: Failed to write audit log entry.");
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
function showForceWarning(name, blockers, gitState) {
|
|
1796
|
+
console.error("");
|
|
1797
|
+
console.error(chalk2.red.bold("\u26A0\uFE0F WARNING: Force removing with unsaved work"));
|
|
1798
|
+
console.error("");
|
|
1378
1799
|
console.error(
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
`Remove command not yet implemented for instance: ${name}.`,
|
|
1383
|
-
"Actual implementation will be in Epic 5. For now, this is a placeholder."
|
|
1384
|
-
)
|
|
1800
|
+
formatSafetyReport(name, blockers, gitState).replace(
|
|
1801
|
+
/Or use --force.*/,
|
|
1802
|
+
chalk2.red("This data will be PERMANENTLY DELETED.")
|
|
1385
1803
|
)
|
|
1386
1804
|
);
|
|
1387
|
-
|
|
1805
|
+
}
|
|
1806
|
+
async function resolveConfirmation(name, options) {
|
|
1807
|
+
if (options.yes) {
|
|
1808
|
+
return "yes-flag";
|
|
1809
|
+
}
|
|
1810
|
+
if (!process.stdin.isTTY) {
|
|
1811
|
+
console.error("");
|
|
1812
|
+
console.error(
|
|
1813
|
+
chalk2.red(
|
|
1814
|
+
"Cannot prompt for confirmation in non-interactive mode. Use --yes to skip confirmation."
|
|
1815
|
+
)
|
|
1816
|
+
);
|
|
1817
|
+
return null;
|
|
1818
|
+
}
|
|
1819
|
+
const answer = await promptForConfirmation(name);
|
|
1820
|
+
if (answer === null || answer !== name) {
|
|
1821
|
+
console.log("Removal cancelled");
|
|
1822
|
+
return null;
|
|
1823
|
+
}
|
|
1824
|
+
return "typed-name";
|
|
1825
|
+
}
|
|
1826
|
+
var removeCommand = new Command6("remove").description("Remove an instance with safety checks").argument("<name>", "Instance name to remove").option("--force", "Force removal, bypassing safety checks").option(
|
|
1827
|
+
"--yes",
|
|
1828
|
+
"Skip confirmation prompt for --force (DANGEROUS: skips all safety prompts, for scripts only)"
|
|
1829
|
+
).action(async (name, options) => {
|
|
1830
|
+
if (options.yes && !options.force) {
|
|
1831
|
+
console.error(
|
|
1832
|
+
formatError(
|
|
1833
|
+
createError(
|
|
1834
|
+
"INVALID_OPTIONS",
|
|
1835
|
+
"--yes can only be used with --force",
|
|
1836
|
+
"Use `agent-env remove <name> --force --yes` to force-remove without confirmation."
|
|
1837
|
+
)
|
|
1838
|
+
)
|
|
1839
|
+
);
|
|
1840
|
+
process.exit(1);
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
const deps = createRemoveDefaultDeps();
|
|
1844
|
+
const checkResult = await removeInstance(name, deps, false);
|
|
1845
|
+
if (checkResult.ok) {
|
|
1846
|
+
if (options.force) {
|
|
1847
|
+
console.log(`Instance '${name}' force-removed. Data permanently deleted.`);
|
|
1848
|
+
await writeAuditLog(name, checkResult.gitState ?? null, "not-required");
|
|
1849
|
+
} else {
|
|
1850
|
+
console.log(`Instance '${name}' removed`);
|
|
1851
|
+
}
|
|
1852
|
+
return;
|
|
1853
|
+
}
|
|
1854
|
+
if (checkResult.error.code !== "SAFETY_CHECK_FAILED") {
|
|
1855
|
+
const { code, message, suggestion } = checkResult.error;
|
|
1856
|
+
console.error(formatError(createError(code, message, suggestion)));
|
|
1857
|
+
process.exit(1);
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
if (!options.force) {
|
|
1861
|
+
if (checkResult.gitState && checkResult.blockers) {
|
|
1862
|
+
console.error(formatSafetyReport(name, checkResult.blockers, checkResult.gitState));
|
|
1863
|
+
}
|
|
1864
|
+
process.exit(1);
|
|
1865
|
+
return;
|
|
1866
|
+
}
|
|
1867
|
+
const { gitState, blockers } = checkResult;
|
|
1868
|
+
if (gitState && blockers && blockers.length > 0) {
|
|
1869
|
+
showForceWarning(name, blockers, gitState);
|
|
1870
|
+
}
|
|
1871
|
+
const confirmationMethod = await resolveConfirmation(name, options);
|
|
1872
|
+
if (!confirmationMethod) {
|
|
1873
|
+
process.exit(1);
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1876
|
+
const forceResult = await removeInstance(name, deps, true);
|
|
1877
|
+
if (!forceResult.ok) {
|
|
1878
|
+
const { code, message, suggestion } = forceResult.error;
|
|
1879
|
+
console.error(formatError(createError(code, message, suggestion)));
|
|
1880
|
+
process.exit(1);
|
|
1881
|
+
return;
|
|
1882
|
+
}
|
|
1883
|
+
console.log(`Instance '${name}' force-removed. Data permanently deleted.`);
|
|
1884
|
+
await writeAuditLog(name, gitState ?? null, confirmationMethod);
|
|
1388
1885
|
});
|
|
1389
1886
|
|
|
1390
1887
|
// src/components/InteractiveMenu.tsx
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zookanalytics/agent-env",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "CLI for creating isolated, AI-ready development environments",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
"import": "./dist/cli.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/zookanalytics/bmad-orchestrator",
|
|
18
|
+
"directory": "packages/agent-env"
|
|
19
|
+
},
|
|
15
20
|
"engines": {
|
|
16
21
|
"node": ">=20"
|
|
17
22
|
},
|
|
@@ -22,18 +27,9 @@
|
|
|
22
27
|
"README.md",
|
|
23
28
|
"LICENSE"
|
|
24
29
|
],
|
|
25
|
-
"scripts": {
|
|
26
|
-
"dev": "tsx src/cli.ts",
|
|
27
|
-
"build": "tsup",
|
|
28
|
-
"test": "vitest",
|
|
29
|
-
"test:run": "vitest run",
|
|
30
|
-
"test:coverage": "vitest run --coverage",
|
|
31
|
-
"lint": "eslint src/",
|
|
32
|
-
"type-check": "tsc --noEmit",
|
|
33
|
-
"check": "pnpm type-check && pnpm lint && pnpm test:run"
|
|
34
|
-
},
|
|
35
30
|
"dependencies": {
|
|
36
31
|
"@inkjs/ui": "^2.0.0",
|
|
32
|
+
"chalk": "^5.3.0",
|
|
37
33
|
"commander": "^14.0.2",
|
|
38
34
|
"execa": "^9.6.1",
|
|
39
35
|
"ink": "^6.6.0",
|
|
@@ -43,9 +39,19 @@
|
|
|
43
39
|
"devDependencies": {
|
|
44
40
|
"@types/node": "^20.14.10",
|
|
45
41
|
"@types/react": "^19.2.8",
|
|
46
|
-
"@zookanalytics/shared": "workspace:*",
|
|
47
42
|
"ink-testing-library": "^4.0.0",
|
|
48
43
|
"tsup": "^8.5.1",
|
|
49
|
-
"tsx": "^4.21.0"
|
|
44
|
+
"tsx": "^4.21.0",
|
|
45
|
+
"@zookanalytics/shared": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "tsx src/cli.ts",
|
|
49
|
+
"build": "tsup",
|
|
50
|
+
"test": "vitest",
|
|
51
|
+
"test:run": "vitest run",
|
|
52
|
+
"test:coverage": "vitest run --coverage",
|
|
53
|
+
"lint": "eslint src/",
|
|
54
|
+
"type-check": "tsc --noEmit",
|
|
55
|
+
"check": "pnpm type-check && pnpm lint && pnpm test:run"
|
|
50
56
|
}
|
|
51
|
-
}
|
|
57
|
+
}
|