@zookanalytics/agent-env 0.7.1 → 0.9.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/config/baseline/init-host.sh +9 -0
- package/dist/cli.js +688 -110
- package/package.json +1 -1
|
@@ -28,6 +28,15 @@ else
|
|
|
28
28
|
echo "agent-env: Warning: No SSH public keys found in ~/.ssh. SSH access will require manual setup."
|
|
29
29
|
fi
|
|
30
30
|
|
|
31
|
+
# Stage PulseAudio cookie for audio passthrough (if setup-audio was run)
|
|
32
|
+
PULSE_COOKIE_SRC="$HOME/.agent-env/pulse/cookie"
|
|
33
|
+
PULSE_COOKIE_DST="$PWD/.agent-env/pulse"
|
|
34
|
+
if [ -f "$PULSE_COOKIE_SRC" ]; then
|
|
35
|
+
mkdir -p "$PULSE_COOKIE_DST"
|
|
36
|
+
cp -p "$PULSE_COOKIE_SRC" "$PULSE_COOKIE_DST/cookie"
|
|
37
|
+
echo "agent-env: Staged PulseAudio cookie for audio passthrough"
|
|
38
|
+
fi
|
|
39
|
+
|
|
31
40
|
# Verify SSH agent socket exists (provided by Docker Desktop / OrbStack on macOS)
|
|
32
41
|
if [ ! -S "$SSH_SOCKET" ]; then
|
|
33
42
|
echo "agent-env: Warning: SSH agent socket not found at $SSH_SOCKET"
|
package/dist/cli.js
CHANGED
|
@@ -17,10 +17,24 @@ function createError(code, message, suggestion) {
|
|
|
17
17
|
import { execa } from "execa";
|
|
18
18
|
function createExecutor(executor = execa) {
|
|
19
19
|
return async function execute2(command, args = [], options = {}) {
|
|
20
|
-
const
|
|
20
|
+
const { onLine, ...execaOptions } = options;
|
|
21
|
+
const subprocess = executor(command, args, {
|
|
21
22
|
reject: false,
|
|
22
|
-
...
|
|
23
|
+
...execaOptions
|
|
23
24
|
});
|
|
25
|
+
if (onLine) {
|
|
26
|
+
const sub = subprocess;
|
|
27
|
+
const handleChunk = (chunk) => {
|
|
28
|
+
for (const line of chunk.toString().split("\n")) {
|
|
29
|
+
const trimmed = line.trim();
|
|
30
|
+
if (trimmed)
|
|
31
|
+
onLine(trimmed);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
sub.stdout?.on?.("data", handleChunk);
|
|
35
|
+
sub.stderr?.on?.("data", handleChunk);
|
|
36
|
+
}
|
|
37
|
+
const result = await subprocess;
|
|
24
38
|
return {
|
|
25
39
|
ok: !result.failed,
|
|
26
40
|
stdout: String(result.stdout ?? ""),
|
|
@@ -35,14 +49,14 @@ var execute = createExecutor();
|
|
|
35
49
|
import { program } from "commander";
|
|
36
50
|
import { render as render2 } from "ink";
|
|
37
51
|
import { existsSync } from "fs";
|
|
38
|
-
import { dirname as
|
|
52
|
+
import { dirname as dirname4, resolve } from "path";
|
|
39
53
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
40
54
|
import React3 from "react";
|
|
41
55
|
|
|
42
56
|
// package.json
|
|
43
57
|
var package_default = {
|
|
44
58
|
name: "@zookanalytics/agent-env",
|
|
45
|
-
version: "0.
|
|
59
|
+
version: "0.9.0",
|
|
46
60
|
description: "CLI for creating isolated, AI-ready development environments",
|
|
47
61
|
type: "module",
|
|
48
62
|
bin: {
|
|
@@ -311,7 +325,8 @@ function createContainerLifecycle(executor = createExecutor()) {
|
|
|
311
325
|
...buildNoCache ? ["--build-no-cache"] : []
|
|
312
326
|
];
|
|
313
327
|
const result = await executor("devcontainer", args, {
|
|
314
|
-
timeout: buildNoCache ? DEVCONTAINER_UP_NO_CACHE_TIMEOUT : DEVCONTAINER_UP_TIMEOUT
|
|
328
|
+
timeout: buildNoCache ? DEVCONTAINER_UP_NO_CACHE_TIMEOUT : DEVCONTAINER_UP_TIMEOUT,
|
|
329
|
+
onLine: options?.onProgress
|
|
315
330
|
});
|
|
316
331
|
const parsedOutput = parseDevcontainerOutput(result.stdout);
|
|
317
332
|
if (!result.ok) {
|
|
@@ -603,7 +618,7 @@ var defaultMergeDeps = { readFile: readFile2, writeFile: writeFile2, rename, acc
|
|
|
603
618
|
var STATUS_BAR_JSON_FILENAME = "statusBar.json";
|
|
604
619
|
var LABEL_LIFECYCLE_CMDS = {
|
|
605
620
|
postCreateCommand: "/usr/local/bin/post-create.sh",
|
|
606
|
-
postStartCommand: "sudo /usr/local/bin/start-sshd.sh; tmux new-session -d -s
|
|
621
|
+
postStartCommand: "sudo /usr/local/bin/start-sshd.sh; tmux new-session -d -s agent-env 2>/dev/null || true; agent-env tmux-restore 2>/dev/null || true; : > /tmp/install-shared-skills.log; /usr/local/bin/install-shared-skills.sh >> /tmp/install-shared-skills.log 2>&1 || true"
|
|
607
622
|
};
|
|
608
623
|
var COMPOSABLE_LIFECYCLE_CMDS = [
|
|
609
624
|
"postCreateCommand",
|
|
@@ -651,7 +666,7 @@ async function readRepoConfig(workspacePath, deps = defaultMergeDeps, logger) {
|
|
|
651
666
|
}
|
|
652
667
|
return void 0;
|
|
653
668
|
}
|
|
654
|
-
function validateRepoConfig(config, logger) {
|
|
669
|
+
function validateRepoConfig(config, managedImage, logger) {
|
|
655
670
|
const rejectedProps = ["build", "dockerFile", "dockerfile", "dockerComposeFile"];
|
|
656
671
|
for (const prop of rejectedProps) {
|
|
657
672
|
if (prop in config && config[prop] !== void 0) {
|
|
@@ -662,9 +677,11 @@ function validateRepoConfig(config, logger) {
|
|
|
662
677
|
}
|
|
663
678
|
if ("image" in config && config.image !== void 0) {
|
|
664
679
|
const repoImage = String(config.image);
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
680
|
+
if (repoImage !== managedImage) {
|
|
681
|
+
logger?.warn(
|
|
682
|
+
`Repo config specifies image '${repoImage}' which will be overridden by agent-env managed image.`
|
|
683
|
+
);
|
|
684
|
+
}
|
|
668
685
|
}
|
|
669
686
|
}
|
|
670
687
|
function composeName(instanceName, repoName) {
|
|
@@ -1547,14 +1564,7 @@ async function resolveRepoUrl(repoArg, executor) {
|
|
|
1547
1564
|
async function attachToInstance(containerName, executor) {
|
|
1548
1565
|
const result = await executor(
|
|
1549
1566
|
"docker",
|
|
1550
|
-
[
|
|
1551
|
-
"exec",
|
|
1552
|
-
"-it",
|
|
1553
|
-
containerName,
|
|
1554
|
-
"bash",
|
|
1555
|
-
"-c",
|
|
1556
|
-
"tmux attach-session -t main 2>/dev/null || tmux new-session -s main"
|
|
1557
|
-
],
|
|
1567
|
+
["exec", "-it", containerName, "bash", "-c", "/home/node/.local/bin/tmux-session"],
|
|
1558
1568
|
{ stdio: "inherit" }
|
|
1559
1569
|
);
|
|
1560
1570
|
if (!result.ok) {
|
|
@@ -1598,7 +1608,7 @@ async function setupMergedConfig(wsPath, containerName, repoName, purposeText, d
|
|
|
1598
1608
|
});
|
|
1599
1609
|
const repoConfig = await readRepoConfig(wsPath.root, deps.mergeDeps, deps.logger);
|
|
1600
1610
|
if (repoConfig) {
|
|
1601
|
-
validateRepoConfig(repoConfig, deps.logger);
|
|
1611
|
+
validateRepoConfig(repoConfig, defaults.image, deps.logger);
|
|
1602
1612
|
}
|
|
1603
1613
|
const merged = mergeDevcontainerConfigs(managed, repoConfig);
|
|
1604
1614
|
await copyManagedAssets(wsPath.root, deps.devcontainerFsDeps);
|
|
@@ -1727,7 +1737,8 @@ async function createInstance(instanceName, repoUrl, deps, options) {
|
|
|
1727
1737
|
AGENT_INSTANCE: wsPath.name,
|
|
1728
1738
|
AGENT_ENV_PURPOSE: purposeText
|
|
1729
1739
|
},
|
|
1730
|
-
configPath
|
|
1740
|
+
configPath,
|
|
1741
|
+
onProgress: options?.onProgress
|
|
1731
1742
|
});
|
|
1732
1743
|
if (!containerResult.ok) {
|
|
1733
1744
|
return rollbackContainerFailure(containerName, wsPath, containerResult, deps);
|
|
@@ -1924,9 +1935,169 @@ var attachCommand = new Command("attach").description("Attach to an instance's t
|
|
|
1924
1935
|
}
|
|
1925
1936
|
});
|
|
1926
1937
|
|
|
1927
|
-
// src/commands/
|
|
1938
|
+
// src/commands/code.ts
|
|
1928
1939
|
import { Command as Command2 } from "commander";
|
|
1929
1940
|
|
|
1941
|
+
// src/lib/code-instance.ts
|
|
1942
|
+
import {
|
|
1943
|
+
appendFile as appendFile5,
|
|
1944
|
+
lstat,
|
|
1945
|
+
mkdir as mkdir7,
|
|
1946
|
+
readdir as readdir6,
|
|
1947
|
+
readFile as readFile9,
|
|
1948
|
+
readlink,
|
|
1949
|
+
rename as rename6,
|
|
1950
|
+
stat as stat6,
|
|
1951
|
+
symlink,
|
|
1952
|
+
unlink,
|
|
1953
|
+
writeFile as writeFile8
|
|
1954
|
+
} from "fs/promises";
|
|
1955
|
+
import { homedir as homedir5 } from "os";
|
|
1956
|
+
import { join as join8, relative } from "path";
|
|
1957
|
+
function createCodeDefaultDeps() {
|
|
1958
|
+
const executor = createExecutor();
|
|
1959
|
+
return {
|
|
1960
|
+
executor,
|
|
1961
|
+
container: createContainerLifecycle(executor),
|
|
1962
|
+
workspaceFsDeps: { mkdir: mkdir7, readdir: readdir6, stat: stat6, homedir: homedir5 },
|
|
1963
|
+
stateFsDeps: { readFile: readFile9, writeFile: writeFile8, rename: rename6, mkdir: mkdir7, appendFile: appendFile5 },
|
|
1964
|
+
codeFsDeps: { mkdir: mkdir7, lstat, symlink, readlink, unlink }
|
|
1965
|
+
};
|
|
1966
|
+
}
|
|
1967
|
+
var DEVCONTAINER_OPEN_TIMEOUT = 3e4;
|
|
1968
|
+
async function ensureDevcontainerSymlink(wsRoot, deps) {
|
|
1969
|
+
const devcontainerDir = join8(wsRoot, ".devcontainer");
|
|
1970
|
+
const symlinkPath = join8(devcontainerDir, "devcontainer.json");
|
|
1971
|
+
const target = relative(devcontainerDir, join8(wsRoot, AGENT_ENV_DIR, "devcontainer.json"));
|
|
1972
|
+
try {
|
|
1973
|
+
const linkStat = await deps.lstat(symlinkPath);
|
|
1974
|
+
if (linkStat.isSymbolicLink()) {
|
|
1975
|
+
const existing = await deps.readlink(symlinkPath);
|
|
1976
|
+
if (existing === target) return;
|
|
1977
|
+
await deps.unlink(symlinkPath);
|
|
1978
|
+
} else {
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
} catch (err) {
|
|
1982
|
+
if (err.code !== "ENOENT") throw err;
|
|
1983
|
+
}
|
|
1984
|
+
await deps.mkdir(devcontainerDir, { recursive: true });
|
|
1985
|
+
await deps.symlink(target, symlinkPath);
|
|
1986
|
+
}
|
|
1987
|
+
async function codeInstance(instanceName, deps, onContainerStarting, onOpening, repoSlug) {
|
|
1988
|
+
const lookup = await resolveInstance(instanceName, repoSlug, {
|
|
1989
|
+
fsDeps: deps.workspaceFsDeps,
|
|
1990
|
+
readFile: deps.stateFsDeps.readFile
|
|
1991
|
+
});
|
|
1992
|
+
if (!lookup.found) {
|
|
1993
|
+
return { ok: false, error: lookup.error };
|
|
1994
|
+
}
|
|
1995
|
+
const wsPath = getWorkspacePathByName(lookup.workspaceName, deps.workspaceFsDeps);
|
|
1996
|
+
const state = await readState(wsPath, deps.stateFsDeps);
|
|
1997
|
+
const containerName = state.containerName;
|
|
1998
|
+
const dockerAvailable = await deps.container.isDockerAvailable();
|
|
1999
|
+
if (!dockerAvailable) {
|
|
2000
|
+
return {
|
|
2001
|
+
ok: false,
|
|
2002
|
+
error: {
|
|
2003
|
+
code: "ORBSTACK_REQUIRED",
|
|
2004
|
+
message: "Docker is not available. OrbStack or Docker Desktop must be running.",
|
|
2005
|
+
suggestion: "Start OrbStack or Docker Desktop, then try again."
|
|
2006
|
+
}
|
|
2007
|
+
};
|
|
2008
|
+
}
|
|
2009
|
+
const statusResult = await deps.container.containerStatus(containerName);
|
|
2010
|
+
if (!statusResult.ok) {
|
|
2011
|
+
return {
|
|
2012
|
+
ok: false,
|
|
2013
|
+
error: statusResult.error ?? {
|
|
2014
|
+
code: "CONTAINER_ERROR",
|
|
2015
|
+
message: `Failed to check container status for '${containerName}'.`
|
|
2016
|
+
}
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
const configPath = join8(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
2020
|
+
if (statusResult.status === "stopped" || statusResult.status === "not-found") {
|
|
2021
|
+
onContainerStarting?.();
|
|
2022
|
+
const startResult = await deps.container.devcontainerUp(wsPath.root, containerName, {
|
|
2023
|
+
remoteEnv: { AGENT_INSTANCE: wsPath.name },
|
|
2024
|
+
configPath
|
|
2025
|
+
});
|
|
2026
|
+
if (!startResult.ok) {
|
|
2027
|
+
return {
|
|
2028
|
+
ok: false,
|
|
2029
|
+
error: startResult.error ?? {
|
|
2030
|
+
code: "CONTAINER_ERROR",
|
|
2031
|
+
message: `Failed to start container '${containerName}'.`,
|
|
2032
|
+
suggestion: "Check Docker logs for details."
|
|
2033
|
+
}
|
|
2034
|
+
};
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
if (!state.repoConfigDetected) {
|
|
2038
|
+
try {
|
|
2039
|
+
await ensureDevcontainerSymlink(wsPath.root, deps.codeFsDeps);
|
|
2040
|
+
} catch (err) {
|
|
2041
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
2042
|
+
return {
|
|
2043
|
+
ok: false,
|
|
2044
|
+
error: {
|
|
2045
|
+
code: "SYMLINK_FAILED",
|
|
2046
|
+
message: `Failed to create .devcontainer symlink in '${wsPath.root}': ${detail}`,
|
|
2047
|
+
suggestion: "Check file permissions in the workspace directory."
|
|
2048
|
+
}
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
onOpening?.();
|
|
2053
|
+
const openResult = await deps.executor(
|
|
2054
|
+
"devcontainer",
|
|
2055
|
+
["open", wsPath.root, "--config", configPath],
|
|
2056
|
+
{ stdio: "inherit", timeout: DEVCONTAINER_OPEN_TIMEOUT }
|
|
2057
|
+
);
|
|
2058
|
+
if (!openResult.ok) {
|
|
2059
|
+
return {
|
|
2060
|
+
ok: false,
|
|
2061
|
+
error: {
|
|
2062
|
+
code: "VSCODE_OPEN_FAILED",
|
|
2063
|
+
message: `Failed to open VS Code for instance '${instanceName}'.`,
|
|
2064
|
+
suggestion: "Ensure VS Code and the Dev Containers extension are installed."
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
}
|
|
2068
|
+
const updatedState = {
|
|
2069
|
+
...state,
|
|
2070
|
+
lastAttached: (/* @__PURE__ */ new Date()).toISOString()
|
|
2071
|
+
};
|
|
2072
|
+
await writeStateAtomic(wsPath, updatedState, deps.stateFsDeps);
|
|
2073
|
+
return { ok: true };
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
// src/commands/code.ts
|
|
2077
|
+
var codeCommand = new Command2("code").description("Open an instance in VS Code via Dev Containers").argument("<name>", "Instance name to open").option("--repo <slug>", "Repo slug or URL to scope instance lookup").action(async (name, options) => {
|
|
2078
|
+
const deps = createCodeDefaultDeps();
|
|
2079
|
+
const repoSlug = await resolveRepoOrExit({ repo: options.repo, cwd: process.cwd() });
|
|
2080
|
+
const result = await codeInstance(
|
|
2081
|
+
name,
|
|
2082
|
+
deps,
|
|
2083
|
+
() => {
|
|
2084
|
+
console.log("Starting container...");
|
|
2085
|
+
},
|
|
2086
|
+
() => {
|
|
2087
|
+
console.log("Opening VS Code...");
|
|
2088
|
+
},
|
|
2089
|
+
repoSlug
|
|
2090
|
+
);
|
|
2091
|
+
if (!result.ok) {
|
|
2092
|
+
const { code, message, suggestion } = result.error;
|
|
2093
|
+
console.error(formatError(createError(code, message, suggestion)));
|
|
2094
|
+
process.exit(1);
|
|
2095
|
+
}
|
|
2096
|
+
});
|
|
2097
|
+
|
|
2098
|
+
// src/commands/completion.ts
|
|
2099
|
+
import { Command as Command3 } from "commander";
|
|
2100
|
+
|
|
1930
2101
|
// src/lib/completion.ts
|
|
1931
2102
|
var COMMANDS = [
|
|
1932
2103
|
"create",
|
|
@@ -2181,7 +2352,7 @@ compdef _agent_env agent-env`;
|
|
|
2181
2352
|
}
|
|
2182
2353
|
|
|
2183
2354
|
// src/commands/completion.ts
|
|
2184
|
-
var completionCommand = new
|
|
2355
|
+
var completionCommand = new Command3("completion").description(`Generate shell completion script for bash or zsh
|
|
2185
2356
|
|
|
2186
2357
|
${getInstallInstructions()}`).argument("<shell>", "Shell type (bash or zsh)").action((shell) => {
|
|
2187
2358
|
if (!isValidShell(shell)) {
|
|
@@ -2198,7 +2369,35 @@ ${getInstallInstructions()}`).argument("<shell>", "Shell type (bash or zsh)").ac
|
|
|
2198
2369
|
});
|
|
2199
2370
|
|
|
2200
2371
|
// src/commands/create.ts
|
|
2201
|
-
import { Command as
|
|
2372
|
+
import { Command as Command4 } from "commander";
|
|
2373
|
+
|
|
2374
|
+
// src/lib/progress-line.ts
|
|
2375
|
+
var ERASE_LINE = "\x1B[K";
|
|
2376
|
+
function createProgressLine(stream = process.stderr, columns) {
|
|
2377
|
+
if (!stream.isTTY) {
|
|
2378
|
+
return { update() {
|
|
2379
|
+
}, clear() {
|
|
2380
|
+
} };
|
|
2381
|
+
}
|
|
2382
|
+
const maxWidth = columns ?? stream.columns ?? 80;
|
|
2383
|
+
const PREFIX = "\u23F3 ";
|
|
2384
|
+
const prefixLen = 3;
|
|
2385
|
+
let active = false;
|
|
2386
|
+
return {
|
|
2387
|
+
update(line) {
|
|
2388
|
+
const clean = line.replace(/\x1b\[[0-9;]*m/g, "");
|
|
2389
|
+
const truncated = clean.length + prefixLen > maxWidth ? clean.slice(0, maxWidth - prefixLen - 1) + "\u2026" : clean;
|
|
2390
|
+
stream.write(`\r${ERASE_LINE}${PREFIX}${truncated}`);
|
|
2391
|
+
active = true;
|
|
2392
|
+
},
|
|
2393
|
+
clear() {
|
|
2394
|
+
if (active) {
|
|
2395
|
+
stream.write(`\r${ERASE_LINE}`);
|
|
2396
|
+
active = false;
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
};
|
|
2400
|
+
}
|
|
2202
2401
|
|
|
2203
2402
|
// src/lib/list-repos.ts
|
|
2204
2403
|
async function listRepos(deps) {
|
|
@@ -2278,7 +2477,7 @@ async function resolveRepoArg(repoArg, deps) {
|
|
|
2278
2477
|
}
|
|
2279
2478
|
|
|
2280
2479
|
// src/commands/create.ts
|
|
2281
|
-
var createCommand = new
|
|
2480
|
+
var createCommand = new Command4("create").description("Create a new isolated development environment").argument("<name>", "Name for the new instance").option("--repo <url|slug>", 'Git repo URL, registered slug, or "." for current directory').option("--purpose <text>", "Set the instance purpose/label at creation time").option("--attach", "Attach immediately after creation").option("--baseline", "[REMOVED] Config merge replaces this flag").option("--no-baseline", "[REMOVED] Config merge replaces this flag").action(async (name, options) => {
|
|
2282
2481
|
if (options.baseline !== void 0) {
|
|
2283
2482
|
console.error(
|
|
2284
2483
|
formatError(
|
|
@@ -2322,32 +2521,39 @@ var createCommand = new Command3("create").description("Create a new isolated de
|
|
|
2322
2521
|
}
|
|
2323
2522
|
const repoUrl = resolved.url;
|
|
2324
2523
|
console.log(`Creating instance '${name}' from ${repoUrl}...`);
|
|
2325
|
-
const
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
console.log(`\x1B[32m\u2713\x1B[0m Instance '${name}' created successfully`);
|
|
2335
|
-
console.log(` Workspace: ${workspacePath.root}`);
|
|
2336
|
-
console.log(` Container: ${containerName}`);
|
|
2337
|
-
if (options.attach) {
|
|
2338
|
-
console.log();
|
|
2339
|
-
console.log("Attaching to instance...");
|
|
2340
|
-
const attachResult = await attachToInstance(containerName, deps.executor);
|
|
2341
|
-
if (!attachResult.ok) {
|
|
2342
|
-
const { code, message, suggestion } = attachResult.error;
|
|
2524
|
+
const progress = createProgressLine();
|
|
2525
|
+
try {
|
|
2526
|
+
const result = await createInstance(name, repoUrl, deps, {
|
|
2527
|
+
purpose: options.purpose,
|
|
2528
|
+
onProgress: progress.update
|
|
2529
|
+
});
|
|
2530
|
+
progress.clear();
|
|
2531
|
+
if (!result.ok) {
|
|
2532
|
+
const { code, message, suggestion } = result.error;
|
|
2343
2533
|
console.error(formatError(createError(code, message, suggestion)));
|
|
2344
2534
|
process.exit(1);
|
|
2345
2535
|
}
|
|
2536
|
+
const { workspacePath, containerName } = result;
|
|
2537
|
+
console.log(`\x1B[32m\u2713\x1B[0m Instance '${name}' created successfully`);
|
|
2538
|
+
console.log(` Workspace: ${workspacePath.root}`);
|
|
2539
|
+
console.log(` Container: ${containerName}`);
|
|
2540
|
+
if (options.attach) {
|
|
2541
|
+
console.log();
|
|
2542
|
+
console.log("Attaching to instance...");
|
|
2543
|
+
const attachResult = await attachToInstance(containerName, deps.executor);
|
|
2544
|
+
if (!attachResult.ok) {
|
|
2545
|
+
const { code, message, suggestion } = attachResult.error;
|
|
2546
|
+
console.error(formatError(createError(code, message, suggestion)));
|
|
2547
|
+
process.exit(1);
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
} finally {
|
|
2551
|
+
progress.clear();
|
|
2346
2552
|
}
|
|
2347
2553
|
});
|
|
2348
2554
|
|
|
2349
2555
|
// src/commands/list.ts
|
|
2350
|
-
import { Command as
|
|
2556
|
+
import { Command as Command5 } from "commander";
|
|
2351
2557
|
import { render } from "ink";
|
|
2352
2558
|
import React from "react";
|
|
2353
2559
|
|
|
@@ -2694,7 +2900,7 @@ async function listInstances(deps, opts) {
|
|
|
2694
2900
|
}
|
|
2695
2901
|
|
|
2696
2902
|
// src/commands/list.ts
|
|
2697
|
-
var listCommand = new
|
|
2903
|
+
var listCommand = new Command5("list").alias("ps").description("List all instances with status").option("--json", "Output in JSON format").option("--repo <slug>", "Filter instances by repo slug").action(async (options) => {
|
|
2698
2904
|
const result = await listInstances(void 0, {
|
|
2699
2905
|
repoFilter: options.repo
|
|
2700
2906
|
});
|
|
@@ -2746,12 +2952,12 @@ var listCommand = new Command4("list").alias("ps").description("List all instanc
|
|
|
2746
2952
|
});
|
|
2747
2953
|
|
|
2748
2954
|
// src/commands/purpose.ts
|
|
2749
|
-
import { Command as
|
|
2955
|
+
import { Command as Command6 } from "commander";
|
|
2750
2956
|
var defaultPurposeCommandDeps = {
|
|
2751
2957
|
isInsideContainer
|
|
2752
2958
|
};
|
|
2753
2959
|
function createPurposeCommand(deps = defaultPurposeCommandDeps) {
|
|
2754
|
-
return new
|
|
2960
|
+
return new Command6("purpose").description(
|
|
2755
2961
|
"Get or set the purpose/label for an instance. Inside a container: no instance name needed (agent-env purpose [value])"
|
|
2756
2962
|
).argument("[nameOrValue]", "Instance name (host) or purpose value (container)").argument("[value]", "New purpose value (omit to get current, empty string to clear)").option("--repo <slug>", "Repo slug or URL to scope instance lookup (host mode only)").action(async (nameOrValue, value, options) => {
|
|
2757
2963
|
if (deps.isInsideContainer()) {
|
|
@@ -2826,35 +3032,35 @@ function handleSetResult(result) {
|
|
|
2826
3032
|
|
|
2827
3033
|
// src/commands/rebuild.ts
|
|
2828
3034
|
import chalk from "chalk";
|
|
2829
|
-
import { Command as
|
|
3035
|
+
import { Command as Command7 } from "commander";
|
|
2830
3036
|
import { createInterface } from "readline";
|
|
2831
3037
|
|
|
2832
3038
|
// src/lib/rebuild-instance.ts
|
|
2833
3039
|
import {
|
|
2834
3040
|
access as access4,
|
|
2835
3041
|
cp as cp3,
|
|
2836
|
-
mkdir as
|
|
2837
|
-
readdir as
|
|
2838
|
-
readFile as
|
|
2839
|
-
rename as
|
|
3042
|
+
mkdir as mkdir8,
|
|
3043
|
+
readdir as readdir7,
|
|
3044
|
+
readFile as readFile10,
|
|
3045
|
+
rename as rename7,
|
|
2840
3046
|
rm as rm3,
|
|
2841
|
-
stat as
|
|
2842
|
-
writeFile as
|
|
2843
|
-
appendFile as
|
|
3047
|
+
stat as stat7,
|
|
3048
|
+
writeFile as writeFile9,
|
|
3049
|
+
appendFile as appendFile6
|
|
2844
3050
|
} from "fs/promises";
|
|
2845
|
-
import { homedir as
|
|
2846
|
-
import { join as
|
|
3051
|
+
import { homedir as homedir6 } from "os";
|
|
3052
|
+
import { join as join9 } from "path";
|
|
2847
3053
|
function createRebuildDefaultDeps() {
|
|
2848
3054
|
const executor = createExecutor();
|
|
2849
3055
|
return {
|
|
2850
3056
|
executor,
|
|
2851
3057
|
container: createContainerLifecycle(executor),
|
|
2852
|
-
workspaceFsDeps: { mkdir:
|
|
2853
|
-
stateFsDeps: { readFile:
|
|
2854
|
-
devcontainerFsDeps: { cp: cp3, mkdir:
|
|
2855
|
-
mergeDeps: { readFile:
|
|
3058
|
+
workspaceFsDeps: { mkdir: mkdir8, readdir: readdir7, stat: stat7, homedir: homedir6 },
|
|
3059
|
+
stateFsDeps: { readFile: readFile10, writeFile: writeFile9, rename: rename7, mkdir: mkdir8, appendFile: appendFile6 },
|
|
3060
|
+
devcontainerFsDeps: { cp: cp3, mkdir: mkdir8, readdir: readdir7, readFile: readFile10, stat: stat7, writeFile: writeFile9 },
|
|
3061
|
+
mergeDeps: { readFile: readFile10, writeFile: writeFile9, rename: rename7, access: access4 },
|
|
2856
3062
|
rm: rm3,
|
|
2857
|
-
rename:
|
|
3063
|
+
rename: rename7,
|
|
2858
3064
|
logger: { warn: (msg) => console.warn(msg), info: (msg) => console.info(msg) }
|
|
2859
3065
|
};
|
|
2860
3066
|
}
|
|
@@ -2879,11 +3085,11 @@ async function refreshMergedConfig(wsRoot, containerName, wsName, repoSlug, purp
|
|
|
2879
3085
|
};
|
|
2880
3086
|
}
|
|
2881
3087
|
if (repoConfig) {
|
|
2882
|
-
validateRepoConfig(repoConfig, deps.logger);
|
|
3088
|
+
validateRepoConfig(repoConfig, defaults.image, deps.logger);
|
|
2883
3089
|
}
|
|
2884
3090
|
const merged = mergeDevcontainerConfigs(managed, repoConfig);
|
|
2885
3091
|
await copyManagedAssets(wsRoot, deps.devcontainerFsDeps);
|
|
2886
|
-
const configPath =
|
|
3092
|
+
const configPath = join9(wsRoot, AGENT_ENV_DIR, "devcontainer.json");
|
|
2887
3093
|
await writeGeneratedConfig(configPath, merged, deps.mergeDeps);
|
|
2888
3094
|
return { ok: true, repoConfigDetected: repoConfig !== void 0 };
|
|
2889
3095
|
} catch (err) {
|
|
@@ -2968,6 +3174,20 @@ async function executePullStep(wsRoot, pull, deps) {
|
|
|
2968
3174
|
}
|
|
2969
3175
|
return { ok: true, hasDockerfile };
|
|
2970
3176
|
}
|
|
3177
|
+
async function saveTmuxState(containerName, deps) {
|
|
3178
|
+
const result = await deps.executor("docker", [
|
|
3179
|
+
"exec",
|
|
3180
|
+
containerName,
|
|
3181
|
+
"bash",
|
|
3182
|
+
"-lc",
|
|
3183
|
+
"agent-env tmux-save"
|
|
3184
|
+
]);
|
|
3185
|
+
if (result.ok) {
|
|
3186
|
+
deps.logger?.info("Saved tmux session state");
|
|
3187
|
+
} else {
|
|
3188
|
+
deps.logger?.warn("Could not save tmux session state (non-fatal)");
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
2971
3191
|
async function teardownContainer(containerName, container, statusResult) {
|
|
2972
3192
|
if (!statusResult.ok || statusResult.status === "not-found") {
|
|
2973
3193
|
return { ok: true };
|
|
@@ -2982,12 +3202,13 @@ async function teardownContainer(containerName, container, statusResult) {
|
|
|
2982
3202
|
}
|
|
2983
3203
|
return { ok: true };
|
|
2984
3204
|
}
|
|
2985
|
-
async function startAndDiscoverContainer(wsPath, containerName, purpose, buildNoCache, container) {
|
|
2986
|
-
const configPath =
|
|
3205
|
+
async function startAndDiscoverContainer(wsPath, containerName, purpose, buildNoCache, container, onProgress) {
|
|
3206
|
+
const configPath = join9(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
2987
3207
|
const containerResult = await container.devcontainerUp(wsPath.root, containerName, {
|
|
2988
3208
|
buildNoCache,
|
|
2989
3209
|
remoteEnv: { AGENT_INSTANCE: wsPath.name, AGENT_ENV_PURPOSE: purpose },
|
|
2990
|
-
configPath
|
|
3210
|
+
configPath,
|
|
3211
|
+
onProgress
|
|
2991
3212
|
});
|
|
2992
3213
|
if (!containerResult.ok) {
|
|
2993
3214
|
return { ok: false, error: containerResult.error };
|
|
@@ -3002,13 +3223,14 @@ async function startAndDiscoverContainer(wsPath, containerName, purpose, buildNo
|
|
|
3002
3223
|
return { ok: true, containerName: actualName };
|
|
3003
3224
|
}
|
|
3004
3225
|
async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
3005
|
-
const { force = false, pull = true, noCache = true } = options ?? {};
|
|
3226
|
+
const { force = false, pull = true, noCache = true, onProgress } = options ?? {};
|
|
3006
3227
|
const wsLookup = await lookupWorkspace(instanceName, deps, repoSlug);
|
|
3007
3228
|
if (!wsLookup.ok) {
|
|
3008
3229
|
return { ok: false, error: wsLookup.error };
|
|
3009
3230
|
}
|
|
3010
3231
|
const { wsPath, state } = wsLookup;
|
|
3011
|
-
const
|
|
3232
|
+
const oldContainerName = state.containerName;
|
|
3233
|
+
const containerName = deriveContainerName(wsPath.name);
|
|
3012
3234
|
const dockerAvailable = await deps.container.isDockerAvailable();
|
|
3013
3235
|
if (!dockerAvailable) {
|
|
3014
3236
|
return {
|
|
@@ -3037,20 +3259,23 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
3037
3259
|
return { ok: false, error: pullStepResult.error };
|
|
3038
3260
|
}
|
|
3039
3261
|
const { hasDockerfile } = pullStepResult;
|
|
3040
|
-
const statusResult = await deps.container.containerStatus(
|
|
3262
|
+
const statusResult = await deps.container.containerStatus(oldContainerName);
|
|
3041
3263
|
const wasRunning = statusResult.ok && statusResult.status === "running";
|
|
3042
3264
|
if (wasRunning && !force) {
|
|
3043
3265
|
return {
|
|
3044
3266
|
ok: false,
|
|
3045
3267
|
error: {
|
|
3046
3268
|
code: "CONTAINER_RUNNING",
|
|
3047
|
-
message: `Container '${
|
|
3269
|
+
message: `Container '${oldContainerName}' is currently running`,
|
|
3048
3270
|
suggestion: "Stop any active sessions first, or use --force to rebuild a running container."
|
|
3049
3271
|
},
|
|
3050
3272
|
wasRunning: true
|
|
3051
3273
|
};
|
|
3052
3274
|
}
|
|
3053
|
-
|
|
3275
|
+
if (wasRunning) {
|
|
3276
|
+
await saveTmuxState(oldContainerName, deps);
|
|
3277
|
+
}
|
|
3278
|
+
const teardownResult = await teardownContainer(oldContainerName, deps.container, statusResult);
|
|
3054
3279
|
if (!teardownResult.ok) {
|
|
3055
3280
|
return { ok: false, error: teardownResult.error, wasRunning };
|
|
3056
3281
|
}
|
|
@@ -3059,7 +3284,8 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
3059
3284
|
containerName,
|
|
3060
3285
|
state.purpose ?? "",
|
|
3061
3286
|
noCache && hasDockerfile,
|
|
3062
|
-
deps.container
|
|
3287
|
+
deps.container,
|
|
3288
|
+
onProgress
|
|
3063
3289
|
);
|
|
3064
3290
|
if (!startResult.ok) {
|
|
3065
3291
|
return { ok: false, error: startResult.error, wasRunning };
|
|
@@ -3100,7 +3326,7 @@ Any active sessions will be lost. Continue? (y/N) `,
|
|
|
3100
3326
|
});
|
|
3101
3327
|
});
|
|
3102
3328
|
}
|
|
3103
|
-
var rebuildCommand = new
|
|
3329
|
+
var rebuildCommand = new Command7("rebuild").description("Rebuild an instance by recreating its container").argument("<name>", "Instance name to rebuild").option("--force", "Rebuild even if the container is currently running").option("--yes", "Skip confirmation prompt").option("--no-pull", "Skip pulling fresh base images").option("--use-cache", "Allow Docker build layer cache reuse").option("--repo <slug>", "Repo slug or URL to scope instance lookup").action(
|
|
3104
3330
|
async (name, options) => {
|
|
3105
3331
|
const deps = createRebuildDefaultDeps();
|
|
3106
3332
|
const repoSlug = await resolveRepoOrExit({ repo: options.repo, cwd: process.cwd() });
|
|
@@ -3113,55 +3339,62 @@ var rebuildCommand = new Command6("rebuild").description("Rebuild an instance by
|
|
|
3113
3339
|
}
|
|
3114
3340
|
}
|
|
3115
3341
|
const force = options.force || options.yes || confirmed;
|
|
3342
|
+
const progress = createProgressLine();
|
|
3116
3343
|
const rebuildOptions = {
|
|
3117
3344
|
force,
|
|
3118
3345
|
pull: options.pull,
|
|
3119
|
-
noCache: !options.useCache
|
|
3346
|
+
noCache: !options.useCache,
|
|
3347
|
+
onProgress: progress.update
|
|
3120
3348
|
};
|
|
3121
3349
|
if (force) {
|
|
3122
3350
|
console.log(`Force-rebuilding instance '${name}'...`);
|
|
3123
3351
|
} else {
|
|
3124
3352
|
console.log(`Rebuilding instance '${name}'...`);
|
|
3125
3353
|
}
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3354
|
+
try {
|
|
3355
|
+
const result = await rebuildInstance(name, deps, rebuildOptions, repoSlug);
|
|
3356
|
+
progress.clear();
|
|
3357
|
+
if (!result.ok) {
|
|
3358
|
+
if (result.error.code === "CONTAINER_RUNNING") {
|
|
3359
|
+
console.error("");
|
|
3360
|
+
console.error(
|
|
3361
|
+
chalk.yellow("Container is currently running.") + " Active sessions will be terminated during rebuild."
|
|
3362
|
+
);
|
|
3363
|
+
console.error("");
|
|
3364
|
+
console.error(`Use ${chalk.bold("--force")} to rebuild anyway.`);
|
|
3365
|
+
} else {
|
|
3366
|
+
const { code, message, suggestion } = result.error;
|
|
3367
|
+
console.error(formatError(createError(code, message, suggestion)));
|
|
3368
|
+
}
|
|
3369
|
+
process.exit(1);
|
|
3370
|
+
return;
|
|
3138
3371
|
}
|
|
3139
|
-
|
|
3140
|
-
|
|
3372
|
+
console.log(`\x1B[32m\u2713\x1B[0m Instance '${name}' rebuilt successfully`);
|
|
3373
|
+
console.log(` Container: ${result.containerName}`);
|
|
3374
|
+
} finally {
|
|
3375
|
+
progress.clear();
|
|
3141
3376
|
}
|
|
3142
|
-
console.log(`\x1B[32m\u2713\x1B[0m Instance '${name}' rebuilt successfully`);
|
|
3143
|
-
console.log(` Container: ${result.containerName}`);
|
|
3144
3377
|
}
|
|
3145
3378
|
);
|
|
3146
3379
|
|
|
3147
3380
|
// src/commands/remove.ts
|
|
3148
3381
|
import chalk3 from "chalk";
|
|
3149
|
-
import { Command as
|
|
3382
|
+
import { Command as Command8 } from "commander";
|
|
3150
3383
|
import { createInterface as createInterface2 } from "readline";
|
|
3151
3384
|
|
|
3152
3385
|
// src/lib/audit-log.ts
|
|
3153
|
-
import { appendFile as
|
|
3154
|
-
import { homedir as
|
|
3155
|
-
import { join as
|
|
3386
|
+
import { appendFile as appendFile7, mkdir as mkdir9 } from "fs/promises";
|
|
3387
|
+
import { homedir as homedir7 } from "os";
|
|
3388
|
+
import { join as join10 } from "path";
|
|
3156
3389
|
function createAuditLogDefaultDeps() {
|
|
3157
|
-
return { appendFile:
|
|
3390
|
+
return { appendFile: appendFile7, mkdir: mkdir9, homedir: homedir7 };
|
|
3158
3391
|
}
|
|
3159
3392
|
function getAuditLogPath(deps) {
|
|
3160
|
-
return
|
|
3393
|
+
return join10(deps.homedir(), AGENT_ENV_DIR, "audit.log");
|
|
3161
3394
|
}
|
|
3162
3395
|
async function writeAuditLogEntry(entry, deps = createAuditLogDefaultDeps()) {
|
|
3163
3396
|
const logPath = getAuditLogPath(deps);
|
|
3164
|
-
await deps.mkdir(
|
|
3397
|
+
await deps.mkdir(join10(deps.homedir(), AGENT_ENV_DIR), { recursive: true });
|
|
3165
3398
|
const line = JSON.stringify(entry) + "\n";
|
|
3166
3399
|
await deps.appendFile(logPath, line, "utf-8");
|
|
3167
3400
|
}
|
|
@@ -3176,16 +3409,16 @@ function createAuditEntry(instanceName, gitState, confirmationMethod) {
|
|
|
3176
3409
|
}
|
|
3177
3410
|
|
|
3178
3411
|
// src/lib/remove-instance.ts
|
|
3179
|
-
import { mkdir as
|
|
3180
|
-
import { homedir as
|
|
3412
|
+
import { mkdir as mkdir10, readdir as readdir8, readFile as readFile11, rm as rm4, stat as stat8 } from "fs/promises";
|
|
3413
|
+
import { homedir as homedir8 } from "os";
|
|
3181
3414
|
function createRemoveDefaultDeps() {
|
|
3182
3415
|
const executor = createExecutor();
|
|
3183
3416
|
return {
|
|
3184
3417
|
executor,
|
|
3185
3418
|
container: createContainerLifecycle(executor),
|
|
3186
3419
|
gitDetector: createGitStateDetector(executor),
|
|
3187
|
-
workspaceFsDeps: { mkdir:
|
|
3188
|
-
stateFsDeps: { readFile:
|
|
3420
|
+
workspaceFsDeps: { mkdir: mkdir10, readdir: readdir8, stat: stat8, homedir: homedir8 },
|
|
3421
|
+
stateFsDeps: { readFile: readFile11 },
|
|
3189
3422
|
deleteFsDeps: { rm: rm4 }
|
|
3190
3423
|
};
|
|
3191
3424
|
}
|
|
@@ -3465,7 +3698,7 @@ async function resolveConfirmation(name, options) {
|
|
|
3465
3698
|
}
|
|
3466
3699
|
return "typed-name";
|
|
3467
3700
|
}
|
|
3468
|
-
var removeCommand = new
|
|
3701
|
+
var removeCommand = new Command8("remove").description("Remove an instance with safety checks").argument("<name>", "Instance name to remove").option("--repo <slug>", "Repo slug or URL to scope instance lookup").option("--force", "Force removal, bypassing safety checks").option(
|
|
3469
3702
|
"--yes",
|
|
3470
3703
|
"Skip confirmation prompt for --force (DANGEROUS: skips all safety prompts, for scripts only)"
|
|
3471
3704
|
).action(async (name, options) => {
|
|
@@ -3528,8 +3761,8 @@ var removeCommand = new Command7("remove").description("Remove an instance with
|
|
|
3528
3761
|
});
|
|
3529
3762
|
|
|
3530
3763
|
// src/commands/repos.ts
|
|
3531
|
-
import { Command as
|
|
3532
|
-
var reposCommand = new
|
|
3764
|
+
import { Command as Command9 } from "commander";
|
|
3765
|
+
var reposCommand = new Command9("repos").description("List tracked repositories").option("--json", "Output in JSON format").action(async (options) => {
|
|
3533
3766
|
const result = await listRepos();
|
|
3534
3767
|
if (!result.ok) {
|
|
3535
3768
|
const error = createError(
|
|
@@ -3579,13 +3812,353 @@ var reposCommand = new Command8("repos").description("List tracked repositories"
|
|
|
3579
3812
|
}
|
|
3580
3813
|
});
|
|
3581
3814
|
|
|
3815
|
+
// src/commands/setup-audio.ts
|
|
3816
|
+
import { Command as Command10 } from "commander";
|
|
3817
|
+
import { copyFile, mkdir as mkdir11, readFile as readFile12, writeFile as writeFile10, access as access5 } from "fs/promises";
|
|
3818
|
+
import { homedir as homedir9, platform } from "os";
|
|
3819
|
+
|
|
3820
|
+
// src/lib/setup-audio.ts
|
|
3821
|
+
async function setupAudio(deps) {
|
|
3822
|
+
const steps = [];
|
|
3823
|
+
if (deps.platform !== "darwin") {
|
|
3824
|
+
return {
|
|
3825
|
+
ok: false,
|
|
3826
|
+
steps,
|
|
3827
|
+
error: "Audio passthrough is only supported on macOS. On Linux, use native PulseAudio directly."
|
|
3828
|
+
};
|
|
3829
|
+
}
|
|
3830
|
+
const brewPrefix = await deps.execute("brew", ["--prefix", "pulseaudio"]);
|
|
3831
|
+
if (!brewPrefix.ok) {
|
|
3832
|
+
return {
|
|
3833
|
+
ok: false,
|
|
3834
|
+
steps,
|
|
3835
|
+
error: "PulseAudio not found. Install with: brew install pulseaudio"
|
|
3836
|
+
};
|
|
3837
|
+
}
|
|
3838
|
+
const paPrefix = brewPrefix.stdout.trim();
|
|
3839
|
+
steps.push("PulseAudio found");
|
|
3840
|
+
const moduleCheck = await deps.execute("pactl", ["list", "short", "modules"]);
|
|
3841
|
+
const tcpAlreadyLoaded = moduleCheck.ok && moduleCheck.stdout.includes("module-native-protocol-tcp");
|
|
3842
|
+
if (!tcpAlreadyLoaded) {
|
|
3843
|
+
const loadResult = await deps.execute("pactl", [
|
|
3844
|
+
"load-module",
|
|
3845
|
+
"module-native-protocol-tcp",
|
|
3846
|
+
"auth-cookie-enabled=1",
|
|
3847
|
+
"listen=127.0.0.1",
|
|
3848
|
+
"port=4713"
|
|
3849
|
+
]);
|
|
3850
|
+
if (!loadResult.ok) {
|
|
3851
|
+
return {
|
|
3852
|
+
ok: false,
|
|
3853
|
+
steps,
|
|
3854
|
+
error: `Failed to load PulseAudio TCP module: ${loadResult.stderr}`
|
|
3855
|
+
};
|
|
3856
|
+
}
|
|
3857
|
+
steps.push("TCP module loaded");
|
|
3858
|
+
} else {
|
|
3859
|
+
steps.push("TCP module already loaded");
|
|
3860
|
+
}
|
|
3861
|
+
const defaultPaPath = `${paPrefix}/etc/pulse/default.pa`;
|
|
3862
|
+
const tcpModuleLine = "load-module module-native-protocol-tcp auth-cookie-enabled=1 listen=127.0.0.1 port=4713";
|
|
3863
|
+
try {
|
|
3864
|
+
let paContent;
|
|
3865
|
+
try {
|
|
3866
|
+
paContent = await deps.readFile(defaultPaPath, "utf-8");
|
|
3867
|
+
} catch {
|
|
3868
|
+
await deps.mkdir(`${paPrefix}/etc/pulse`, { recursive: true });
|
|
3869
|
+
paContent = "";
|
|
3870
|
+
}
|
|
3871
|
+
const hasActiveModule = paContent.split("\n").some((line) => {
|
|
3872
|
+
const trimmed = line.trim();
|
|
3873
|
+
return !trimmed.startsWith("#") && !trimmed.startsWith(";") && trimmed.includes("load-module module-native-protocol-tcp");
|
|
3874
|
+
});
|
|
3875
|
+
if (!hasActiveModule) {
|
|
3876
|
+
const newContent = paContent ? paContent.trimEnd() + "\n" + tcpModuleLine + "\n" : tcpModuleLine + "\n";
|
|
3877
|
+
await deps.writeFile(defaultPaPath, newContent, "utf-8");
|
|
3878
|
+
steps.push("TCP module persisted to default.pa");
|
|
3879
|
+
} else {
|
|
3880
|
+
steps.push("TCP module already in default.pa");
|
|
3881
|
+
}
|
|
3882
|
+
} catch (err) {
|
|
3883
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
3884
|
+
return {
|
|
3885
|
+
ok: false,
|
|
3886
|
+
steps,
|
|
3887
|
+
error: `Failed to write ${defaultPaPath}: ${msg}`
|
|
3888
|
+
};
|
|
3889
|
+
}
|
|
3890
|
+
const cookieSrc = `${deps.homeDir}/.config/pulse/cookie`;
|
|
3891
|
+
const cookieDstDir = `${deps.homeDir}/.agent-env/pulse`;
|
|
3892
|
+
const cookieDst = `${cookieDstDir}/cookie`;
|
|
3893
|
+
try {
|
|
3894
|
+
await deps.access(cookieSrc);
|
|
3895
|
+
await deps.mkdir(cookieDstDir, { recursive: true });
|
|
3896
|
+
await deps.copyFile(cookieSrc, cookieDst);
|
|
3897
|
+
steps.push("Cookie staged");
|
|
3898
|
+
} catch {
|
|
3899
|
+
return {
|
|
3900
|
+
ok: false,
|
|
3901
|
+
steps,
|
|
3902
|
+
error: `PulseAudio cookie not found at ${cookieSrc}. Is PulseAudio running? Try: pulseaudio --start`
|
|
3903
|
+
};
|
|
3904
|
+
}
|
|
3905
|
+
return { ok: true, steps };
|
|
3906
|
+
}
|
|
3907
|
+
|
|
3908
|
+
// src/commands/setup-audio.ts
|
|
3909
|
+
function createDefaultSetupAudioDeps() {
|
|
3910
|
+
const exec = createExecutor();
|
|
3911
|
+
return {
|
|
3912
|
+
platform: platform(),
|
|
3913
|
+
homeDir: homedir9(),
|
|
3914
|
+
execute: exec,
|
|
3915
|
+
readFile: (path, encoding) => readFile12(path, encoding),
|
|
3916
|
+
writeFile: (path, content, encoding) => writeFile10(path, content, encoding),
|
|
3917
|
+
copyFile,
|
|
3918
|
+
mkdir: (path, opts) => mkdir11(path, opts),
|
|
3919
|
+
access: (path) => access5(path)
|
|
3920
|
+
};
|
|
3921
|
+
}
|
|
3922
|
+
var setupAudioCommand = new Command10("setup-audio").description("Configure macOS PulseAudio for audio passthrough to containers").action(async () => {
|
|
3923
|
+
const deps = createDefaultSetupAudioDeps();
|
|
3924
|
+
const result = await setupAudio(deps);
|
|
3925
|
+
if (!result.ok) {
|
|
3926
|
+
console.error(
|
|
3927
|
+
formatError(createError("AUDIO_SETUP_FAILED", result.error ?? "Unknown error"))
|
|
3928
|
+
);
|
|
3929
|
+
process.exit(1);
|
|
3930
|
+
return;
|
|
3931
|
+
}
|
|
3932
|
+
for (const step of result.steps) {
|
|
3933
|
+
console.log(`\x1B[32m\u2713\x1B[0m ${step}`);
|
|
3934
|
+
}
|
|
3935
|
+
console.log("");
|
|
3936
|
+
console.log(
|
|
3937
|
+
"Audio passthrough configured. Changes take effect on next container start or rebuild."
|
|
3938
|
+
);
|
|
3939
|
+
});
|
|
3940
|
+
|
|
3941
|
+
// src/commands/tmux-restore.ts
|
|
3942
|
+
import { Command as Command11 } from "commander";
|
|
3943
|
+
import { execSync } from "child_process";
|
|
3944
|
+
import { join as join12 } from "path";
|
|
3945
|
+
|
|
3946
|
+
// src/lib/tmux-session.ts
|
|
3947
|
+
import { readFile as readFile13, writeFile as writeFile11, rename as rename8, mkdir as mkdir12 } from "fs/promises";
|
|
3948
|
+
import { dirname as dirname3, join as join11 } from "path";
|
|
3949
|
+
async function readPanesState(path) {
|
|
3950
|
+
try {
|
|
3951
|
+
const content = await readFile13(path, "utf-8");
|
|
3952
|
+
const parsed = JSON.parse(content);
|
|
3953
|
+
if (typeof parsed === "object" && parsed !== null && parsed.version === 1) {
|
|
3954
|
+
return parsed;
|
|
3955
|
+
}
|
|
3956
|
+
return { version: 1 };
|
|
3957
|
+
} catch {
|
|
3958
|
+
return { version: 1 };
|
|
3959
|
+
}
|
|
3960
|
+
}
|
|
3961
|
+
async function writePanesState(path, state) {
|
|
3962
|
+
await mkdir12(dirname3(path), { recursive: true });
|
|
3963
|
+
const tmpPath = path + ".tmp";
|
|
3964
|
+
await writeFile11(tmpPath, JSON.stringify(state), "utf-8");
|
|
3965
|
+
await rename8(tmpPath, path);
|
|
3966
|
+
}
|
|
3967
|
+
async function readSessionState(path) {
|
|
3968
|
+
try {
|
|
3969
|
+
const content = await readFile13(path, "utf-8");
|
|
3970
|
+
const parsed = JSON.parse(content);
|
|
3971
|
+
if (typeof parsed === "object" && parsed !== null && parsed.version === 1 && Array.isArray(parsed.windows)) {
|
|
3972
|
+
return parsed;
|
|
3973
|
+
}
|
|
3974
|
+
return null;
|
|
3975
|
+
} catch {
|
|
3976
|
+
return null;
|
|
3977
|
+
}
|
|
3978
|
+
}
|
|
3979
|
+
async function writeSessionState(path, state) {
|
|
3980
|
+
await mkdir12(dirname3(path), { recursive: true });
|
|
3981
|
+
const tmpPath = path + ".tmp";
|
|
3982
|
+
await writeFile11(tmpPath, JSON.stringify(state, null, 2), "utf-8");
|
|
3983
|
+
await rename8(tmpPath, path);
|
|
3984
|
+
}
|
|
3985
|
+
function pruneStaleEntries(state, activePaneIds) {
|
|
3986
|
+
const pruned = { version: 1 };
|
|
3987
|
+
const activeSet = new Set(activePaneIds);
|
|
3988
|
+
for (const [key, value] of Object.entries(state)) {
|
|
3989
|
+
if (key === "version") continue;
|
|
3990
|
+
if (activeSet.has(key)) {
|
|
3991
|
+
pruned[key] = value;
|
|
3992
|
+
}
|
|
3993
|
+
}
|
|
3994
|
+
return pruned;
|
|
3995
|
+
}
|
|
3996
|
+
function resolveTmuxStateDir(agentInstance) {
|
|
3997
|
+
const instance = agentInstance || process.env.AGENT_INSTANCE;
|
|
3998
|
+
if (!instance) return null;
|
|
3999
|
+
return join11("/shared-data/instance", instance, "tmux");
|
|
4000
|
+
}
|
|
4001
|
+
|
|
4002
|
+
// src/commands/tmux-restore.ts
|
|
4003
|
+
function shellQuote(s) {
|
|
4004
|
+
return "'" + s.replace(/'/g, "'\\''") + "'";
|
|
4005
|
+
}
|
|
4006
|
+
function tmuxExec(cmd) {
|
|
4007
|
+
return execSync(cmd, { encoding: "utf-8", timeout: 5e3 });
|
|
4008
|
+
}
|
|
4009
|
+
function tmuxExecSafe(cmd) {
|
|
4010
|
+
try {
|
|
4011
|
+
execSync(cmd, { encoding: "utf-8", timeout: 5e3 });
|
|
4012
|
+
return true;
|
|
4013
|
+
} catch {
|
|
4014
|
+
return false;
|
|
4015
|
+
}
|
|
4016
|
+
}
|
|
4017
|
+
function ensureTmuxSession(savedName) {
|
|
4018
|
+
try {
|
|
4019
|
+
const sessions = tmuxExec('tmux list-sessions -F "#{session_name}"').trim().split("\n").filter(Boolean);
|
|
4020
|
+
if (sessions.includes(savedName)) {
|
|
4021
|
+
return savedName;
|
|
4022
|
+
}
|
|
4023
|
+
if (sessions.length > 0) {
|
|
4024
|
+
return sessions[0];
|
|
4025
|
+
}
|
|
4026
|
+
} catch {
|
|
4027
|
+
}
|
|
4028
|
+
const name = savedName || "agent-env";
|
|
4029
|
+
tmuxExecSafe(`tmux new-session -d -s ${shellQuote(name)}`);
|
|
4030
|
+
return name;
|
|
4031
|
+
}
|
|
4032
|
+
async function executeTmuxRestore() {
|
|
4033
|
+
const stateDir = resolveTmuxStateDir();
|
|
4034
|
+
if (!stateDir) return;
|
|
4035
|
+
const sessionState = await readSessionState(join12(stateDir, "session.json"));
|
|
4036
|
+
if (!sessionState) return;
|
|
4037
|
+
const sessionName = ensureTmuxSession(sessionState.tmux_session);
|
|
4038
|
+
const quotedSession = shellQuote(sessionName);
|
|
4039
|
+
if (sessionState.windows.length === 0) return;
|
|
4040
|
+
for (const win of sessionState.windows) {
|
|
4041
|
+
tmuxExecSafe(
|
|
4042
|
+
`tmux new-window -t ${quotedSession} -n ${shellQuote(win.name)} -c ${shellQuote(win.cwd)}`
|
|
4043
|
+
);
|
|
4044
|
+
}
|
|
4045
|
+
tmuxExecSafe(`tmux kill-window -t ${quotedSession}:1`);
|
|
4046
|
+
for (let i = 0; i < sessionState.windows.length; i++) {
|
|
4047
|
+
const win = sessionState.windows[i];
|
|
4048
|
+
if (win.program === "claude" && win.claude_session_id) {
|
|
4049
|
+
const windowIndex = i + 1;
|
|
4050
|
+
tmuxExecSafe(
|
|
4051
|
+
`tmux send-keys -t ${quotedSession}:${windowIndex} ${shellQuote(`claude --resume ${win.claude_session_id} || claude`)} Enter`
|
|
4052
|
+
);
|
|
4053
|
+
}
|
|
4054
|
+
}
|
|
4055
|
+
if (sessionState.active_window) {
|
|
4056
|
+
const activeIdx = sessionState.windows.findIndex((w) => w.index === sessionState.active_window);
|
|
4057
|
+
if (activeIdx >= 0) {
|
|
4058
|
+
tmuxExecSafe(`tmux select-window -t ${quotedSession}:${activeIdx + 1}`);
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
}
|
|
4062
|
+
var tmuxRestoreCommand = new Command11("tmux-restore").description("Restore tmux session state from saved state (container only)").action(async () => {
|
|
4063
|
+
try {
|
|
4064
|
+
await executeTmuxRestore();
|
|
4065
|
+
} catch {
|
|
4066
|
+
}
|
|
4067
|
+
});
|
|
4068
|
+
|
|
4069
|
+
// src/commands/tmux-save.ts
|
|
4070
|
+
import { Command as Command12 } from "commander";
|
|
4071
|
+
import { execSync as execSync2 } from "child_process";
|
|
4072
|
+
import { unlink as unlink2 } from "fs/promises";
|
|
4073
|
+
import { join as join13 } from "path";
|
|
4074
|
+
async function executeTmuxSave() {
|
|
4075
|
+
const stateDir = resolveTmuxStateDir();
|
|
4076
|
+
if (!stateDir) return;
|
|
4077
|
+
const sessionPath = join13(stateDir, "session.json");
|
|
4078
|
+
let tmuxOutput;
|
|
4079
|
+
try {
|
|
4080
|
+
tmuxOutput = execSync2(
|
|
4081
|
+
'tmux list-panes -a -F "#{pane_id} #{window_index} #{window_name} #{pane_current_path} #{pane_current_command} #{session_name}"',
|
|
4082
|
+
{ encoding: "utf-8", timeout: 5e3 }
|
|
4083
|
+
);
|
|
4084
|
+
} catch {
|
|
4085
|
+
await unlink2(sessionPath).catch(() => {
|
|
4086
|
+
});
|
|
4087
|
+
return;
|
|
4088
|
+
}
|
|
4089
|
+
const panes = tmuxOutput.trim().split("\n").filter((line) => line.trim()).map((line) => {
|
|
4090
|
+
const [paneId, indexStr, windowName, cwd, command, sessionName2] = line.split(" ");
|
|
4091
|
+
return {
|
|
4092
|
+
paneId,
|
|
4093
|
+
windowIndex: parseInt(indexStr, 10),
|
|
4094
|
+
windowName,
|
|
4095
|
+
cwd,
|
|
4096
|
+
command,
|
|
4097
|
+
sessionName: sessionName2
|
|
4098
|
+
};
|
|
4099
|
+
});
|
|
4100
|
+
if (panes.length === 0) {
|
|
4101
|
+
await unlink2(sessionPath).catch(() => {
|
|
4102
|
+
});
|
|
4103
|
+
return;
|
|
4104
|
+
}
|
|
4105
|
+
const panesPath = join13(stateDir, "claude-sessions.json");
|
|
4106
|
+
const panesState = await readPanesState(panesPath);
|
|
4107
|
+
let activeWindow = 1;
|
|
4108
|
+
try {
|
|
4109
|
+
const activeOutput = execSync2('tmux display-message -p "#{window_index}"', {
|
|
4110
|
+
encoding: "utf-8",
|
|
4111
|
+
timeout: 5e3
|
|
4112
|
+
});
|
|
4113
|
+
activeWindow = parseInt(activeOutput.trim(), 10) || 1;
|
|
4114
|
+
} catch {
|
|
4115
|
+
}
|
|
4116
|
+
const sessionName = panes[0].sessionName;
|
|
4117
|
+
const seenWindows = /* @__PURE__ */ new Set();
|
|
4118
|
+
const windows = [];
|
|
4119
|
+
for (const pane of panes) {
|
|
4120
|
+
if (seenWindows.has(pane.windowIndex)) continue;
|
|
4121
|
+
seenWindows.add(pane.windowIndex);
|
|
4122
|
+
const paneEntry = panesState[pane.paneId];
|
|
4123
|
+
const hasClaudeEntry = typeof paneEntry === "object" && paneEntry !== null && "session_id" in paneEntry;
|
|
4124
|
+
const isClaudeRunning = pane.command === "claude";
|
|
4125
|
+
const window = {
|
|
4126
|
+
index: pane.windowIndex,
|
|
4127
|
+
name: pane.windowName,
|
|
4128
|
+
cwd: pane.cwd,
|
|
4129
|
+
program: isClaudeRunning ? "claude" : null
|
|
4130
|
+
};
|
|
4131
|
+
if (isClaudeRunning && hasClaudeEntry) {
|
|
4132
|
+
window.claude_session_id = paneEntry.session_id;
|
|
4133
|
+
}
|
|
4134
|
+
windows.push(window);
|
|
4135
|
+
}
|
|
4136
|
+
const sessionState = {
|
|
4137
|
+
version: 1,
|
|
4138
|
+
saved_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4139
|
+
tmux_session: sessionName,
|
|
4140
|
+
active_window: activeWindow,
|
|
4141
|
+
windows
|
|
4142
|
+
};
|
|
4143
|
+
await writeSessionState(sessionPath, sessionState);
|
|
4144
|
+
const activePaneIds = panes.map((p) => p.paneId);
|
|
4145
|
+
const pruned = pruneStaleEntries(panesState, activePaneIds);
|
|
4146
|
+
await writePanesState(panesPath, pruned);
|
|
4147
|
+
}
|
|
4148
|
+
var tmuxSaveCommand = new Command12("tmux-save").description("Save current tmux session state for persistence across rebuilds (container only)").action(async () => {
|
|
4149
|
+
try {
|
|
4150
|
+
await executeTmuxSave();
|
|
4151
|
+
} catch {
|
|
4152
|
+
}
|
|
4153
|
+
});
|
|
4154
|
+
|
|
3582
4155
|
// src/commands/tmux-status.ts
|
|
3583
|
-
import { Command as
|
|
4156
|
+
import { Command as Command13 } from "commander";
|
|
3584
4157
|
|
|
3585
4158
|
// src/lib/tmux-status.ts
|
|
3586
|
-
import { readFile as
|
|
4159
|
+
import { readFile as readFile14 } from "fs/promises";
|
|
3587
4160
|
var MAX_PURPOSE_DISPLAY_LEN = 40;
|
|
3588
|
-
var defaultDeps2 = { readFile:
|
|
4161
|
+
var defaultDeps2 = { readFile: readFile14 };
|
|
3589
4162
|
function formatTmuxStatus(instance, purpose) {
|
|
3590
4163
|
if (!instance) return "?";
|
|
3591
4164
|
if (!purpose) return instance;
|
|
@@ -3621,7 +4194,7 @@ var defaultDeps3 = {
|
|
|
3621
4194
|
statePath: CONTAINER_STATE_PATH
|
|
3622
4195
|
};
|
|
3623
4196
|
function createTmuxStatusCommand(deps = defaultDeps3) {
|
|
3624
|
-
return new
|
|
4197
|
+
return new Command13("tmux-status").description("Output formatted instance status for tmux status bar (container only)").action(async () => {
|
|
3625
4198
|
if (!deps.isInsideContainer()) {
|
|
3626
4199
|
console.log("?");
|
|
3627
4200
|
return;
|
|
@@ -3804,19 +4377,24 @@ Purpose for ${name}:`);
|
|
|
3804
4377
|
}
|
|
3805
4378
|
|
|
3806
4379
|
// src/cli.ts
|
|
3807
|
-
var __dirname =
|
|
4380
|
+
var __dirname = dirname4(fileURLToPath2(import.meta.url));
|
|
3808
4381
|
var isLinked = existsSync(resolve(__dirname, "..", "..", "..", "pnpm-workspace.yaml"));
|
|
3809
|
-
var
|
|
4382
|
+
var isBakedDev = resolve(__dirname, "..").startsWith("/opt/agent-env-dev");
|
|
4383
|
+
var version = package_default.version + (isLinked ? "+local" : isBakedDev ? "+dev" : "");
|
|
3810
4384
|
program.name("agent-env").description("CLI for creating isolated, AI-ready development environments").version(version).allowExcessArguments(true);
|
|
3811
4385
|
program.addCommand(createCommand);
|
|
3812
4386
|
program.addCommand(listCommand);
|
|
3813
4387
|
program.addCommand(attachCommand);
|
|
4388
|
+
program.addCommand(codeCommand);
|
|
3814
4389
|
program.addCommand(rebuildCommand);
|
|
3815
4390
|
program.addCommand(removeCommand);
|
|
3816
4391
|
program.addCommand(purposeCommand);
|
|
3817
4392
|
program.addCommand(reposCommand);
|
|
3818
4393
|
program.addCommand(tmuxStatusCommand);
|
|
4394
|
+
program.addCommand(tmuxSaveCommand);
|
|
4395
|
+
program.addCommand(tmuxRestoreCommand);
|
|
3819
4396
|
program.addCommand(completionCommand);
|
|
4397
|
+
program.addCommand(setupAudioCommand);
|
|
3820
4398
|
program.action(async () => {
|
|
3821
4399
|
if (program.args.length > 0) {
|
|
3822
4400
|
const unknown = program.args[0];
|