rucan 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +48 -2
- package/agents/THIRD_PARTY_LICENSES.txt +6535 -0
- package/agents/darwin-arm64/rucan-agent +0 -0
- package/agents/darwin-x64/rucan-agent +0 -0
- package/agents/linux-arm64/rucan-agent +0 -0
- package/agents/linux-x64/rucan-agent +0 -0
- package/agents/manifest.json +9 -0
- package/dist/agent/artifacts.js +52 -0
- package/dist/agent/local.js +80 -0
- package/dist/agent/ssh.js +165 -0
- package/dist/commands/doctor.js +56 -0
- package/dist/commands/remote.js +28 -0
- package/dist/commands/sessions.js +94 -0
- package/dist/commands/tmux.js +42 -0
- package/dist/commands/update.js +17 -0
- package/dist/config/load.js +78 -0
- package/dist/config/schema.js +161 -0
- package/dist/explorer/explorer.js +220 -0
- package/dist/main.js +62 -0
- package/dist/protocol/client.js +124 -0
- package/dist/protocol/framing.js +57 -0
- package/dist/protocol/operation.js +1 -0
- package/dist/release/updates.js +142 -0
- package/dist/release/version.js +30 -0
- package/dist/terminal/input.js +94 -0
- package/dist/terminal/interactive.js +676 -0
- package/dist/terminal/mouse.js +27 -0
- package/dist/terminal/raw-input.js +42 -0
- package/dist/terminal/screen.js +182 -0
- package/dist/terminal/snapshot.js +114 -0
- package/dist/terminal/tmux.js +38 -0
- package/dist/terminal/view.js +64 -0
- package/dist/terminal/watch.js +66 -0
- package/dist/terminal/workspaces.js +85 -0
- package/dist/ui/workspace.js +48 -0
- package/package.json +36 -4
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.1.1",
|
|
3
|
+
"platforms": {
|
|
4
|
+
"linux-x64": "d13f19b03587571576a8b192694deac21a6973fd6ed812fd6f1f4783cd903601",
|
|
5
|
+
"linux-arm64": "a4d1ab1cec2a389cadf4991c6c2d6787ebcbef1f39818a69a843a54009343829",
|
|
6
|
+
"darwin-x64": "acb21632c46edf48ff9b7a7e6db82ae82923adb9c2b4229b1ce55b01c99c30c8",
|
|
7
|
+
"darwin-arm64": "47102a78a9dc3d8974f86d05165291e0876847cf5e22def931768e8776a86605"
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { lstat, readFile } from "node:fs/promises";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { version } from "../release/version.js";
|
|
6
|
+
export async function bundledAgent(os, arch) {
|
|
7
|
+
if (!(os === "linux" || os === "darwin") ||
|
|
8
|
+
!(arch === "x64" || arch === "arm64")) {
|
|
9
|
+
throw new Error(`Unsupported agent platform: ${os}/${arch}`);
|
|
10
|
+
}
|
|
11
|
+
const directory = new URL("../../agents/", import.meta.url);
|
|
12
|
+
let text;
|
|
13
|
+
try {
|
|
14
|
+
text = await readFile(new URL("manifest.json", directory), "utf8");
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (typeof error === "object" &&
|
|
18
|
+
error !== null &&
|
|
19
|
+
"code" in error &&
|
|
20
|
+
error.code === "ENOENT")
|
|
21
|
+
return undefined;
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
const manifest = JSON.parse(text);
|
|
25
|
+
if (typeof manifest !== "object" ||
|
|
26
|
+
manifest === null ||
|
|
27
|
+
!("version" in manifest) ||
|
|
28
|
+
manifest.version !== version ||
|
|
29
|
+
!("platforms" in manifest) ||
|
|
30
|
+
typeof manifest.platforms !== "object" ||
|
|
31
|
+
manifest.platforms === null) {
|
|
32
|
+
throw new Error("Invalid bundled agent manifest");
|
|
33
|
+
}
|
|
34
|
+
const platform = `${os}-${arch}`;
|
|
35
|
+
const entries = { ...manifest.platforms };
|
|
36
|
+
const digest = entries[platform];
|
|
37
|
+
if (typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest))
|
|
38
|
+
throw new Error(`Missing trusted digest for ${platform}`);
|
|
39
|
+
const path = fileURLToPath(new URL(`${platform}/rucan-agent`, directory));
|
|
40
|
+
const stats = await lstat(path);
|
|
41
|
+
if (!stats.isFile() ||
|
|
42
|
+
stats.size > 32 * 1024 * 1024 ||
|
|
43
|
+
(stats.mode & 0o111) === 0) {
|
|
44
|
+
throw new Error(`Invalid bundled binary for ${platform}`);
|
|
45
|
+
}
|
|
46
|
+
const hash = createHash("sha256");
|
|
47
|
+
for await (const bytes of createReadStream(path))
|
|
48
|
+
hash.update(bytes);
|
|
49
|
+
if (hash.digest("hex") !== digest)
|
|
50
|
+
throw new Error(`Agent integrity check failed for ${platform}`);
|
|
51
|
+
return { path, sha256: digest };
|
|
52
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { lstat } from "node:fs/promises";
|
|
3
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { bundledAgent } from "./artifacts.js";
|
|
7
|
+
import { SessionClient } from "../protocol/client.js";
|
|
8
|
+
function retryable(error) {
|
|
9
|
+
return (typeof error === "object" &&
|
|
10
|
+
error !== null &&
|
|
11
|
+
"code" in error &&
|
|
12
|
+
(error.code === "ENOENT" || error.code === "ECONNREFUSED"));
|
|
13
|
+
}
|
|
14
|
+
export function localSocketPath() {
|
|
15
|
+
const configured = process.env.RUCAN_AGENT_SOCKET;
|
|
16
|
+
if (configured) {
|
|
17
|
+
if (!isAbsolute(configured))
|
|
18
|
+
throw new Error("RUCAN_AGENT_SOCKET must be absolute");
|
|
19
|
+
return configured;
|
|
20
|
+
}
|
|
21
|
+
const uid = process.getuid?.();
|
|
22
|
+
if (uid === undefined)
|
|
23
|
+
throw new Error("Native sessions currently require Unix");
|
|
24
|
+
const runtime = process.env.XDG_RUNTIME_DIR;
|
|
25
|
+
return runtime && isAbsolute(runtime)
|
|
26
|
+
? join(runtime, "rucan", "agent.sock")
|
|
27
|
+
: join("/tmp", `rucan-${uid}`, "agent.sock");
|
|
28
|
+
}
|
|
29
|
+
async function connect(path) {
|
|
30
|
+
const directory = await lstat(dirname(path));
|
|
31
|
+
const uid = process.getuid?.();
|
|
32
|
+
if (!directory.isDirectory() ||
|
|
33
|
+
directory.uid !== uid ||
|
|
34
|
+
(directory.mode & 0o077) !== 0) {
|
|
35
|
+
throw new Error(`Agent socket directory is not private or owned by the current user: ${dirname(path)}`);
|
|
36
|
+
}
|
|
37
|
+
const socket = await lstat(path);
|
|
38
|
+
if (!socket.isSocket() || socket.uid !== uid || (socket.mode & 0o077) !== 0) {
|
|
39
|
+
throw new Error(`Agent socket is not private or owned by the current user: ${path}`);
|
|
40
|
+
}
|
|
41
|
+
return SessionClient.connect(path);
|
|
42
|
+
}
|
|
43
|
+
export async function connectLocalAgent(start = true) {
|
|
44
|
+
const path = localSocketPath();
|
|
45
|
+
try {
|
|
46
|
+
return await connect(path);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (!start || !retryable(error))
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
const binary = process.env.RUCAN_AGENT_BINARY ??
|
|
53
|
+
(await bundledAgent(process.platform, process.arch))?.path ??
|
|
54
|
+
fileURLToPath(new URL("../../../../target/debug/rucan-agent", import.meta.url));
|
|
55
|
+
const child = spawn(binary, ["serve", "--socket", path], {
|
|
56
|
+
detached: true,
|
|
57
|
+
stdio: "ignore",
|
|
58
|
+
});
|
|
59
|
+
let spawnError;
|
|
60
|
+
child.once("error", (error) => {
|
|
61
|
+
spawnError = error;
|
|
62
|
+
});
|
|
63
|
+
child.unref();
|
|
64
|
+
// Multiple clients can attempt startup. The agent owns the startup lock.
|
|
65
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
66
|
+
try {
|
|
67
|
+
return await connect(path);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (!retryable(error))
|
|
71
|
+
throw error;
|
|
72
|
+
if (spawnError)
|
|
73
|
+
throw new Error(`Cannot launch ${binary}. Run pnpm build.`, {
|
|
74
|
+
cause: spawnError,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
await delay(Math.min(25 * (attempt + 1), 150));
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Agent did not start at ${path}`);
|
|
80
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { open } from "node:fs/promises";
|
|
4
|
+
import { isAbsolute } from "node:path";
|
|
5
|
+
import { Duplex } from "node:stream";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { SessionClient } from "../protocol/client.js";
|
|
8
|
+
const commonSshArguments = [
|
|
9
|
+
"-T",
|
|
10
|
+
"-o",
|
|
11
|
+
"ConnectTimeout=5",
|
|
12
|
+
"-o",
|
|
13
|
+
"ServerAliveInterval=15",
|
|
14
|
+
"-o",
|
|
15
|
+
"ServerAliveCountMax=2",
|
|
16
|
+
"-o",
|
|
17
|
+
"BatchMode=yes",
|
|
18
|
+
"-o",
|
|
19
|
+
"StrictHostKeyChecking=yes",
|
|
20
|
+
];
|
|
21
|
+
const remotePaths = 'bin="$HOME/.local/lib/rucan/rucan-agent"; socket="/tmp/rucan-$(id -u)/agent.sock"';
|
|
22
|
+
const bridge = `${remotePaths}
|
|
23
|
+
if [ ! -x "$bin" ]; then
|
|
24
|
+
printf '%s\\n' 'rucan-agent is missing; install a verified binary with rucan remote install' >&2
|
|
25
|
+
exit 127
|
|
26
|
+
fi
|
|
27
|
+
exec "$bin" connect --socket "$socket"`;
|
|
28
|
+
const detect = `
|
|
29
|
+
uname -s
|
|
30
|
+
uname -m
|
|
31
|
+
if [ "$(uname -s)" = Linux ]; then
|
|
32
|
+
{ getconf GNU_LIBC_VERSION 2>/dev/null || ldd --version; } 2>&1 | head -n 1
|
|
33
|
+
if command -v loginctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then
|
|
34
|
+
loginctl show-user "$(id -un)" --value -p Linger 2>/dev/null || printf '%s\\n' unknown
|
|
35
|
+
else
|
|
36
|
+
printf '%s\\n' unmanaged
|
|
37
|
+
fi
|
|
38
|
+
else
|
|
39
|
+
sw_vers -productVersion
|
|
40
|
+
printf '%s\\n' launchd
|
|
41
|
+
fi`;
|
|
42
|
+
export function validSshHost(host) {
|
|
43
|
+
return /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/u.test(host);
|
|
44
|
+
}
|
|
45
|
+
function sshArguments(host, script) {
|
|
46
|
+
if (!validSshHost(host))
|
|
47
|
+
throw new Error("Expected an OpenSSH alias or DNS hostname");
|
|
48
|
+
const quoted = "'" + script.replaceAll("'", "'\\''") + "'";
|
|
49
|
+
const config = process.env.RUCAN_SSH_CONFIG;
|
|
50
|
+
if (config && !isAbsolute(config))
|
|
51
|
+
throw new Error("RUCAN_SSH_CONFIG must be absolute");
|
|
52
|
+
const options = config ? ["-F", config] : [];
|
|
53
|
+
return [...commonSshArguments, ...options, "--", host, `sh -c ${quoted}`];
|
|
54
|
+
}
|
|
55
|
+
export async function inspectRemote(host) {
|
|
56
|
+
const { stdout } = await promisify(execFile)("ssh", sshArguments(host, detect), {
|
|
57
|
+
encoding: "utf8",
|
|
58
|
+
maxBuffer: 32768,
|
|
59
|
+
timeout: 10000,
|
|
60
|
+
});
|
|
61
|
+
const [system, machine, runtime = "unknown", loginManager = "unknown"] = stdout.trim().split("\n");
|
|
62
|
+
const os = system === "Linux" ? "linux" : system === "Darwin" ? "darwin" : undefined;
|
|
63
|
+
const arch = machine === "x86_64"
|
|
64
|
+
? "x64"
|
|
65
|
+
: machine === "arm64" || machine === "aarch64"
|
|
66
|
+
? "arm64"
|
|
67
|
+
: undefined;
|
|
68
|
+
if (!os || !arch)
|
|
69
|
+
throw new Error(`Unsupported remote platform: ${system ?? "unknown"} ${machine ?? "unknown"}`);
|
|
70
|
+
return { os, arch, runtime, loginManager };
|
|
71
|
+
}
|
|
72
|
+
export async function connectRemoteAgent(host) {
|
|
73
|
+
const child = spawn("ssh", sshArguments(host, bridge), {
|
|
74
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
75
|
+
});
|
|
76
|
+
const stream = Duplex.from({ readable: child.stdout, writable: child.stdin });
|
|
77
|
+
let diagnostic = "";
|
|
78
|
+
child.stderr.on("data", (chunk) => {
|
|
79
|
+
if (diagnostic.length < 4096)
|
|
80
|
+
diagnostic += chunk.toString("utf8").slice(0, 4096 - diagnostic.length);
|
|
81
|
+
});
|
|
82
|
+
child.on("error", (error) => stream.destroy(error));
|
|
83
|
+
stream.once("close", () => {
|
|
84
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
85
|
+
child.kill();
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
return await SessionClient.negotiate(stream, 12000);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const detail = diagnostic
|
|
92
|
+
.replace(/[\x00-\x09\x0b-\x1f\x7f-\x9f]/gu, "")
|
|
93
|
+
.trim();
|
|
94
|
+
throw new Error(`Cannot connect to ${host}${detail ? `: ${detail}` : ""}`, {
|
|
95
|
+
cause: error,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
export async function installRemote(host, path, expectedHash, knownPlatform) {
|
|
100
|
+
const digest = expectedHash.toLowerCase();
|
|
101
|
+
if (!/^[a-f0-9]{64}$/u.test(digest))
|
|
102
|
+
throw new Error("Expected a trusted SHA-256 digest");
|
|
103
|
+
const file = await open(path, "r");
|
|
104
|
+
let artifact;
|
|
105
|
+
try {
|
|
106
|
+
const stats = await file.stat();
|
|
107
|
+
if (!stats.isFile() || stats.size < 1 || stats.size > 32 * 1024 * 1024)
|
|
108
|
+
throw new Error("Expected a regular agent artifact up to 32 MiB");
|
|
109
|
+
artifact = Buffer.alloc(stats.size + 1);
|
|
110
|
+
let received = 0;
|
|
111
|
+
while (received < artifact.length) {
|
|
112
|
+
const read = await file.read(artifact, received);
|
|
113
|
+
if (read.bytesRead === 0)
|
|
114
|
+
break;
|
|
115
|
+
received += read.bytesRead;
|
|
116
|
+
}
|
|
117
|
+
if (received !== stats.size)
|
|
118
|
+
throw new Error("Agent artifact changed while reading");
|
|
119
|
+
artifact = artifact.subarray(0, received);
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
await file.close();
|
|
123
|
+
}
|
|
124
|
+
if (createHash("sha256").update(artifact).digest("hex") !== digest)
|
|
125
|
+
throw new Error("Agent artifact SHA-256 mismatch");
|
|
126
|
+
const platform = knownPlatform ?? (await inspectRemote(host));
|
|
127
|
+
const transfer = `set -eu
|
|
128
|
+
umask 077
|
|
129
|
+
${remotePaths}
|
|
130
|
+
directory=${"$"}{bin%/*}
|
|
131
|
+
mkdir -p "$directory"
|
|
132
|
+
tmp=$(mktemp "$directory/.agent.XXXXXXXX")
|
|
133
|
+
trap 'unlink "$tmp" 2>/dev/null || :' EXIT HUP INT TERM
|
|
134
|
+
cat > "$tmp"
|
|
135
|
+
if command -v sha256sum >/dev/null 2>&1; then
|
|
136
|
+
actual=$(sha256sum "$tmp")
|
|
137
|
+
else
|
|
138
|
+
actual=$(shasum -a 256 "$tmp")
|
|
139
|
+
fi
|
|
140
|
+
[ "${"$"}{actual%% *}" = '${digest}' ] || { printf '%s\\n' 'agent SHA-256 mismatch' >&2; exit 1; }
|
|
141
|
+
chmod 700 "$tmp"
|
|
142
|
+
"$tmp" --version
|
|
143
|
+
if [ -e "$bin" ]; then
|
|
144
|
+
cmp -s "$tmp" "$bin" && exit 0
|
|
145
|
+
printf '%s\\n' 'an existing agent artifact differs; stop sessions and remove or archive the old binary explicitly before upgrading' >&2
|
|
146
|
+
exit 1
|
|
147
|
+
fi
|
|
148
|
+
ln "$tmp" "$bin"`;
|
|
149
|
+
await new Promise((resolve, reject) => {
|
|
150
|
+
const child = spawn("ssh", sshArguments(host, transfer), {
|
|
151
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
152
|
+
timeout: 30_000,
|
|
153
|
+
});
|
|
154
|
+
child.once("error", reject);
|
|
155
|
+
child.stdin.once("error", reject);
|
|
156
|
+
child.once("close", (code) => {
|
|
157
|
+
if (code === 0)
|
|
158
|
+
resolve();
|
|
159
|
+
else
|
|
160
|
+
reject(new Error(`Agent installation on ${host} exited with status ${code}`));
|
|
161
|
+
});
|
|
162
|
+
child.stdin.end(artifact);
|
|
163
|
+
});
|
|
164
|
+
return platform;
|
|
165
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import { bundledAgent } from "../agent/artifacts.js";
|
|
4
|
+
import { connectLocalAgent, localSocketPath } from "../agent/local.js";
|
|
5
|
+
import { inspectRemote } from "../agent/ssh.js";
|
|
6
|
+
import { configPath, loadConfig } from "../config/load.js";
|
|
7
|
+
import { version } from "../release/version.js";
|
|
8
|
+
export async function runDoctorCommand(args) {
|
|
9
|
+
if (args[0] !== "doctor")
|
|
10
|
+
return false;
|
|
11
|
+
if (args.length !== 1 &&
|
|
12
|
+
(args.length !== 3 || args[1] !== "--host" || !args[2])) {
|
|
13
|
+
throw new Error("Usage: rucan doctor [--host <alias>]");
|
|
14
|
+
}
|
|
15
|
+
const summary = {
|
|
16
|
+
rucan: version,
|
|
17
|
+
node: process.version,
|
|
18
|
+
platform: `${process.platform}/${process.arch}`,
|
|
19
|
+
config: configPath(),
|
|
20
|
+
};
|
|
21
|
+
try {
|
|
22
|
+
await loadConfig();
|
|
23
|
+
summary.configurationValid = true;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
summary.configurationError = String(error);
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const { stderr } = await promisify(execFile)("ssh", ["-V"], {
|
|
30
|
+
timeout: 2000,
|
|
31
|
+
maxBuffer: 8192,
|
|
32
|
+
});
|
|
33
|
+
summary.openSsh = stderr.trim();
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
summary.openSsh = "unavailable";
|
|
37
|
+
}
|
|
38
|
+
if (args[2])
|
|
39
|
+
summary.remote = await inspectRemote(args[2]);
|
|
40
|
+
else {
|
|
41
|
+
const client = await connectLocalAgent(false).catch(() => undefined);
|
|
42
|
+
client?.close();
|
|
43
|
+
summary.localAgent = {
|
|
44
|
+
path: localSocketPath(),
|
|
45
|
+
connected: Boolean(client),
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
summary.bundledArtifact = Boolean(await bundledAgent(process.platform, process.arch));
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
summary.artifactError = String(error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { bundledAgent } from "../agent/artifacts.js";
|
|
2
|
+
import { connectRemoteAgent, inspectRemote, installRemote, } from "../agent/ssh.js";
|
|
3
|
+
export async function runRemoteCommand(args) {
|
|
4
|
+
const [category, command, host, artifact, hash] = args;
|
|
5
|
+
if (category !== "remote" || command === "open")
|
|
6
|
+
return false;
|
|
7
|
+
if (command === "inspect" && host && args.length === 3) {
|
|
8
|
+
console.log(JSON.stringify(await inspectRemote(host), null, 2));
|
|
9
|
+
}
|
|
10
|
+
else if (command === "install" &&
|
|
11
|
+
host &&
|
|
12
|
+
(args.length === 3 || (artifact && hash && args.length === 5))) {
|
|
13
|
+
// Invoking install with an explicit host and trusted digest is the installation approval.
|
|
14
|
+
const supplied = artifact && hash ? { path: artifact, sha256: hash } : undefined;
|
|
15
|
+
const target = await inspectRemote(host);
|
|
16
|
+
const selected = supplied ?? (await bundledAgent(target.os, target.arch));
|
|
17
|
+
if (!selected)
|
|
18
|
+
throw new Error("No bundled artifact. Provide a binary and trusted SHA-256 digest.");
|
|
19
|
+
const platform = await installRemote(host, selected.path, selected.sha256, target);
|
|
20
|
+
const client = await connectRemoteAgent(host);
|
|
21
|
+
client.close();
|
|
22
|
+
console.log(`Agent ready on ${host} (${platform.os}/${platform.arch}).`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Error("Usage: rucan remote inspect <host> | remote install <host> [<binary> <trusted-sha256>] | remote open <host> [session]");
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { loadConfig } from "../config/load.js";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { connectRemoteAgent } from "../agent/ssh.js";
|
|
5
|
+
import { connectLocalAgent } from "../agent/local.js";
|
|
6
|
+
const names = new Set([
|
|
7
|
+
"status",
|
|
8
|
+
"ls",
|
|
9
|
+
"new",
|
|
10
|
+
"pin",
|
|
11
|
+
"unpin",
|
|
12
|
+
"kill",
|
|
13
|
+
"detach",
|
|
14
|
+
"rename",
|
|
15
|
+
"shutdown",
|
|
16
|
+
]);
|
|
17
|
+
export async function runSessionCommand(args) {
|
|
18
|
+
const [command, ...arguments_] = args;
|
|
19
|
+
const host = arguments_[0] === "--host" ? arguments_[1] : undefined;
|
|
20
|
+
if (arguments_[0] === "--host" && !host)
|
|
21
|
+
throw new Error("--host requires an OpenSSH alias");
|
|
22
|
+
const [argument, ...rest] = host ? arguments_.slice(2) : arguments_;
|
|
23
|
+
if (!command || !names.has(command))
|
|
24
|
+
return false;
|
|
25
|
+
if ((command === "ls" || command === "status") && argument !== undefined) {
|
|
26
|
+
throw new Error(`Usage: rucan ${command}`);
|
|
27
|
+
}
|
|
28
|
+
if (command !== "new" && command !== "rename" && rest.length > 0) {
|
|
29
|
+
throw new Error(`Usage: rucan ${command} [session]`);
|
|
30
|
+
}
|
|
31
|
+
const session = argument ?? process.env.RUCAN_SESSION_ID;
|
|
32
|
+
const client = host
|
|
33
|
+
? await connectRemoteAgent(host)
|
|
34
|
+
: await connectLocalAgent(command === "new" || command === "ls");
|
|
35
|
+
try {
|
|
36
|
+
if (command === "status") {
|
|
37
|
+
console.log(`${host ?? "Local agent"} connected (protocol 1)`);
|
|
38
|
+
}
|
|
39
|
+
else if (command === "ls") {
|
|
40
|
+
console.log(JSON.stringify(await client.request({ type: "list" }), null, 2));
|
|
41
|
+
}
|
|
42
|
+
else if (command === "shutdown") {
|
|
43
|
+
if (argument && argument !== "--force")
|
|
44
|
+
throw new Error("Usage: rucan shutdown [--force]");
|
|
45
|
+
console.log(`Stopping agent on ${host ?? "the local host"}${argument ? " and terminating managed sessions" : ""}`);
|
|
46
|
+
await client.request({ type: "shutdown", force: argument === "--force" });
|
|
47
|
+
}
|
|
48
|
+
else if (command === "new") {
|
|
49
|
+
const config = await loadConfig();
|
|
50
|
+
const shell = argument ?? (host ? undefined : process.env.SHELL) ?? "/bin/sh";
|
|
51
|
+
const cols = Math.min(500, process.stdout.columns || 80);
|
|
52
|
+
const description = await client.request({
|
|
53
|
+
type: "create",
|
|
54
|
+
command: shell,
|
|
55
|
+
args: rest,
|
|
56
|
+
cwd: host ? "~" : process.cwd() || homedir(),
|
|
57
|
+
name: basename(shell),
|
|
58
|
+
cols,
|
|
59
|
+
rows: Math.min(200, process.stdout.rows || 24, Math.floor(12000 / cols)),
|
|
60
|
+
persistent: config.defaults.persistence === "persistent",
|
|
61
|
+
scrollback: config.terminal.scrollback_lines,
|
|
62
|
+
});
|
|
63
|
+
console.log(JSON.stringify(description, null, 2));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
if (!session)
|
|
67
|
+
throw new Error(`Usage: rucan ${command} <session>`);
|
|
68
|
+
if (command === "kill") {
|
|
69
|
+
console.log(`Stopping session ${session} on ${host ?? "local agent"}`);
|
|
70
|
+
await client.request({ type: "kill", session });
|
|
71
|
+
}
|
|
72
|
+
else if (command === "detach") {
|
|
73
|
+
await client.request({ type: "detach", session, force: true });
|
|
74
|
+
console.log(`Detached input from session ${session} on ${host ?? "the local host"}`);
|
|
75
|
+
}
|
|
76
|
+
else if (command === "rename") {
|
|
77
|
+
if (rest.length !== 1 || !rest[0])
|
|
78
|
+
throw new Error("Usage: rucan rename <session> <new name>");
|
|
79
|
+
console.log(JSON.stringify(await client.request({ type: "rename", session, name: rest[0] }), null, 2));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
console.log(JSON.stringify(await client.request({
|
|
83
|
+
type: "pin",
|
|
84
|
+
session,
|
|
85
|
+
persistent: command === "pin",
|
|
86
|
+
}), null, 2));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
client.close();
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { connectLocalAgent } from "../agent/local.js";
|
|
2
|
+
import { connectRemoteAgent } from "../agent/ssh.js";
|
|
3
|
+
import { loadConfig } from "../config/load.js";
|
|
4
|
+
import { runInteractive } from "../terminal/interactive.js";
|
|
5
|
+
import { geometry } from "../terminal/screen.js";
|
|
6
|
+
import { listTmux, openTmux } from "../terminal/tmux.js";
|
|
7
|
+
export async function runTmuxCommand(args) {
|
|
8
|
+
if (args[0] !== "tmux")
|
|
9
|
+
return false;
|
|
10
|
+
const host = args[2] === "--host" ? args[3] : undefined;
|
|
11
|
+
if (args[2] === "--host" && !host)
|
|
12
|
+
throw new Error("--host requires an OpenSSH alias");
|
|
13
|
+
const [target, ...overflow] = args.slice(host ? 4 : 2);
|
|
14
|
+
const action = args[1];
|
|
15
|
+
if (overflow.length ||
|
|
16
|
+
!((action === "ls" && !target) ||
|
|
17
|
+
(action === "open" && target) ||
|
|
18
|
+
(action === "new" && !target))) {
|
|
19
|
+
throw new Error("Usage: rucan tmux ls | tmux open [--host <host>] <session> | tmux new [--host <host>]");
|
|
20
|
+
}
|
|
21
|
+
if (action !== "ls" && (!process.stdin.isTTY || !process.stdout.isTTY))
|
|
22
|
+
throw new Error("Opening tmux requires a terminal");
|
|
23
|
+
const client = host
|
|
24
|
+
? await connectRemoteAgent(host)
|
|
25
|
+
: await connectLocalAgent();
|
|
26
|
+
let id;
|
|
27
|
+
try {
|
|
28
|
+
if (action === "ls")
|
|
29
|
+
console.log(JSON.stringify(await listTmux(client), null, 2));
|
|
30
|
+
else {
|
|
31
|
+
const layout = geometry(process.stdout.rows || 24, process.stdout.columns || 80);
|
|
32
|
+
id = (await openTmux(client, layout, host ? "~" : process.cwd(), target ?? null)).id;
|
|
33
|
+
await client.request({ type: "detach", session: id });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
client.close();
|
|
38
|
+
}
|
|
39
|
+
if (id)
|
|
40
|
+
await runInteractive(await loadConfig(), id, host);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { availableUpdate, installUpdate } from "../release/updates.js";
|
|
2
|
+
import { version } from "../release/version.js";
|
|
3
|
+
export async function runUpdateCommand(args) {
|
|
4
|
+
if (args[0] !== "update")
|
|
5
|
+
return false;
|
|
6
|
+
const apply = args.length === 2 && args[1] === "--apply";
|
|
7
|
+
if (!apply && args.length !== 1)
|
|
8
|
+
throw new Error("Usage: rucan update [--apply]");
|
|
9
|
+
const target = await availableUpdate(true);
|
|
10
|
+
if (!target)
|
|
11
|
+
console.log(`No newer stable release found (installed v${version}).`);
|
|
12
|
+
else if (apply)
|
|
13
|
+
await installUpdate(target);
|
|
14
|
+
else
|
|
15
|
+
console.log(`Update available: v${version} → v${target}\nRun rucan update --apply to install.`);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { mkdtemp, mkdir, writeFile, readFile, link, rm, } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { parseDocument } from "yaml";
|
|
5
|
+
import { validateConfig } from "./schema.js";
|
|
6
|
+
const sample = `version: 1
|
|
7
|
+
defaults:
|
|
8
|
+
backend: native
|
|
9
|
+
persistence: temporary
|
|
10
|
+
on_exit: ask
|
|
11
|
+
on_disconnect: preserve
|
|
12
|
+
terminal:
|
|
13
|
+
scrollback_lines: 10000
|
|
14
|
+
prefix_key: ctrl+g
|
|
15
|
+
updates:
|
|
16
|
+
check_on_startup: true
|
|
17
|
+
channel: stable
|
|
18
|
+
explorer:
|
|
19
|
+
exclusions: [.git, node_modules, target, .venv]
|
|
20
|
+
editor:
|
|
21
|
+
command: vi
|
|
22
|
+
args: []
|
|
23
|
+
workspaces:
|
|
24
|
+
- name: local-shell
|
|
25
|
+
cwd: "~"
|
|
26
|
+
command: bash
|
|
27
|
+
# - name: gpu-server
|
|
28
|
+
# host: gpu-server
|
|
29
|
+
# cwd: "~/project"
|
|
30
|
+
# persistence: persistent
|
|
31
|
+
`;
|
|
32
|
+
export function configPath(xdg = process.env.XDG_CONFIG_HOME, home = homedir()) {
|
|
33
|
+
return join(xdg && isAbsolute(xdg) ? xdg : join(home, ".config"), "rucan", "config.yaml");
|
|
34
|
+
}
|
|
35
|
+
function hasCode(error, code) {
|
|
36
|
+
return (typeof error === "object" &&
|
|
37
|
+
error !== null &&
|
|
38
|
+
"code" in error &&
|
|
39
|
+
error.code === code);
|
|
40
|
+
}
|
|
41
|
+
async function createSample(path) {
|
|
42
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
43
|
+
const staging = await mkdtemp(join(dirname(path), ".config-"));
|
|
44
|
+
const file = join(staging, "sample");
|
|
45
|
+
try {
|
|
46
|
+
await writeFile(file, sample, { mode: 0o600, flag: "wx" });
|
|
47
|
+
// Publishing via link keeps concurrent readers from seeing a partial config.
|
|
48
|
+
await link(file, path).catch((error) => {
|
|
49
|
+
if (!hasCode(error, "EEXIST"))
|
|
50
|
+
throw error;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
await rm(staging, { recursive: true, force: true });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function parseConfig(text) {
|
|
58
|
+
const document = parseDocument(text, { strict: true, uniqueKeys: true });
|
|
59
|
+
const error = document.errors[0] ?? document.warnings[0];
|
|
60
|
+
if (error)
|
|
61
|
+
throw error;
|
|
62
|
+
const data = document.toJS({ maxAliasCount: 0 });
|
|
63
|
+
return validateConfig(data);
|
|
64
|
+
}
|
|
65
|
+
export async function loadConfig(path = configPath()) {
|
|
66
|
+
try {
|
|
67
|
+
const content = await readFile(path, "utf8").catch(async (error) => {
|
|
68
|
+
if (!hasCode(error, "ENOENT"))
|
|
69
|
+
throw error;
|
|
70
|
+
await createSample(path);
|
|
71
|
+
return readFile(path, "utf8");
|
|
72
|
+
});
|
|
73
|
+
return parseConfig(content);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new Error(`${path}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
77
|
+
}
|
|
78
|
+
}
|