clavarium 0.2.395-next.1 → 0.2.396-next.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.
@@ -21,17 +21,18 @@ node "$(dirname "$0")/json-set.mjs" .claude/settings.json "$keys" \
21
21
  # work folder trusted -- in its own file, as it records them itself; nothing to answer in the terminal.
22
22
  node "$(dirname "$0")/json-set.mjs" .claude.json '{"hasCompletedOnboarding": true, "projects": {"/work": {"hasTrustDialogAccepted": true}}}' \
23
23
  || echo "launch: Claude Code may ask its first questions in the terminal" >&2
24
- # ITS TOOLS (MCP), EACH THROUGH THE RELAY (02-11): a tool updated on the machine is taken up between two of
25
- # its requests -- whatever the tool, the session never restarted, nothing for the agent to do. Declared
26
- # once, by Claude Code itself (its other servers kept); an older declaration without the relay is replaced.
24
+ # ITS TOOLS (MCP), EACH THROUGH THE SHARED HOST (11-11, #350; the relay since 02-11): one program for all its
25
+ # tools (mcp-sock.sh -> tools-host.mjs), a tool updated on the machine taken up between two of its requests,
26
+ # the session never restarted. Declared once, by Claude Code itself (its other servers kept); an older
27
+ # declaration (the relay alone) is replaced.
27
28
  # Looked for in its settings first, since asking Claude Code takes seconds. Nothing stops the start.
28
- relay="$(dirname "$0")/mcp-relay.mjs"
29
+ relay="$(dirname "$0")/mcp-sock.sh"
29
30
  tool() { # NAME FOLDER COMMAND ARGS...
30
31
  name="$1"; folder="$2"; shift 2
31
- if grep -q "\"$folder\"" "$HOME/.claude.json" 2>/dev/null && grep -q "mcp-relay.mjs" "$HOME/.claude.json" 2>/dev/null \
32
- && node -e 'const d=require(process.argv[1]);const s=d.mcpServers?.[process.argv[2]];const a=JSON.stringify(s?.args??[]);process.exit(a.includes("mcp-relay.mjs")&&a.includes(JSON.stringify(process.argv[3]))?0:1)' "$HOME/.claude.json" "$name" "$1" 2>/dev/null; then return 0; fi
32
+ if grep -q "\"$folder\"" "$HOME/.claude.json" 2>/dev/null && grep -q "mcp-sock.sh" "$HOME/.claude.json" 2>/dev/null \
33
+ && node -e 'const d=require(process.argv[1]);const s=d.mcpServers?.[process.argv[2]];const a=JSON.stringify(s?.args??[]);process.exit(a.includes("mcp-sock.sh")&&a.includes(JSON.stringify(process.argv[3]))?0:1)' "$HOME/.claude.json" "$name" "$1" 2>/dev/null; then return 0; fi
33
34
  claude mcp remove --scope user "$name" >/dev/null 2>&1
34
- claude mcp add --scope user "$name" -- node "$relay" "$@" >/dev/null 2>&1 \
35
+ claude mcp add --scope user "$name" -- sh "$relay" "$@" >/dev/null 2>&1 \
35
36
  || echo "launch: $name was not given to Claude Code as tools: the command $name still works" >&2
36
37
  }
37
38
  # Parley, the chat of the conversation, when it has one: the version installed on the machine (parley-mcp),
@@ -5,7 +5,7 @@
5
5
  mode="$1"; shift
6
6
  case "$mode" in full|ask) ;; *) echo "launch: the first argument is full or ask" >&2; exit 64 ;; esac
7
7
  [ $# -gt 0 ] || { echo "launch: no command to run" >&2; exit 64; }
8
- here="$(dirname "$0")"; relay="$here/mcp-relay.mjs"
8
+ here="$(dirname "$0")"; relay="$here/mcp-sock.sh"
9
9
  chat="${PARLEY_DIR:-/work/.parley}"; docket="/work/.docket"; tether="/work/.tether"
10
10
  # ITS SETTINGS (~/.codex/config.toml), in a block of their own, the rest kept: its work folder trusted (no
11
11
  # question at its first start), and its tools (MCP) each through the relay -- a tool updated on the
@@ -14,15 +14,15 @@ block='[projects."/work"]
14
14
  trust_level = "trusted"'
15
15
  if [ -f "$chat/server.mjs" ] && command -v parley-mcp >/dev/null 2>&1; then block="$block
16
16
  [mcp_servers.parley]
17
- command = \"node\"
17
+ command = \"sh\"
18
18
  args = [\"$relay\", \"parley-mcp\", \"--dir\", \"$chat\"]"; fi
19
19
  if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then block="$block
20
20
  [mcp_servers.docket]
21
- command = \"node\"
21
+ command = \"sh\"
22
22
  args = [\"$relay\", \"docket-mcp\", \"--dir\", \"$docket\"]"; fi
23
23
  if command -v tether-mcp >/dev/null 2>&1; then block="$block
24
24
  [mcp_servers.tether]
25
- command = \"node\"
25
+ command = \"sh\"
26
26
  args = [\"$relay\", \"tether-mcp\", \"--dir\", \"$tether\"]"; fi
27
27
  node "$here/toml-block.mjs" .codex/config.toml tools "$block" \
28
28
  || echo "launch: Codex may ask its first questions, and has not its tools (MCP)" >&2
@@ -0,0 +1,31 @@
1
+ #!/bin/sh
2
+ # A TOOL OF THE CONVERSATION, THROUGH THE SHARED HOST (11-11, card #350): what the agent starts for each of its
3
+ # tools (MCP) -- the same arguments as mcp-relay.mjs, COMMAND ARGS... -- but instead of a Node program of its
4
+ # own, a few lines of sh and netcat: the session goes to tools-host.mjs, one program for all the tools of this
5
+ # user. Anything missing (netcat, the host not starting): the relay of before, as if nothing changed.
6
+ here="$(dirname "$0")"
7
+ fallback() { exec node "$here/mcp-relay.mjs" "$@"; }
8
+ command -v nc >/dev/null 2>&1 || fallback "$@"
9
+ dir="${XDG_RUNTIME_DIR:-/tmp}/clavarium-tools-$(id -u)"
10
+ # Its folder: this user's alone (700), or the relay of before.
11
+ mkdir -p "$dir" 2>/dev/null && [ -O "$dir" ] && chmod 700 "$dir" 2>/dev/null || fallback "$@"
12
+ sock="$dir/host.sock"
13
+ up() { [ -S "$sock" ] && nc -zU "$sock" 2>/dev/null; }
14
+ if ! up; then
15
+ (
16
+ flock 9 # the host itself never holds the lock (9>&- below): two tools starting at once both get in
17
+ if ! up; then
18
+ # its own session (setsid): an agent ending its tools never ends the host the others use
19
+ ns=""; command -v setsid >/dev/null 2>&1 && ns=setsid
20
+ $ns nohup node "$here/tools-host.mjs" "$dir" >>"$dir/host.log" 2>&1 </dev/null 9>&- &
21
+ i=0; while [ $i -lt 30 ] && ! up; do sleep 0.1; i=$((i + 1)); done
22
+ fi
23
+ ) 9>"$dir/lock"
24
+ fi
25
+ up || fallback "$@"
26
+ # Its first line: which tool, with which arguments (JSON); then the session, both ways.
27
+ json() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'; }
28
+ head="{\"command\":\"$(json "$1")\",\"args\":["; shift
29
+ sep=""; for a in "$@"; do head="$head$sep\"$(json "$a")\""; sep=","; done
30
+ head="$head]}"
31
+ { printf '%s\n' "$head"; exec cat; } | exec nc -NU "$sock"
@@ -0,0 +1,93 @@
1
+ /* ONE PROGRAM FOR ALL THE TOOLS OF A CONVERSATION (11-11, card #350). Each tool (Parley, Docket, Tether...) was
2
+ * given to the agent as its own Node program, under its own relay: two Node programs a tool, ~435 MB for three.
3
+ * Here they share one: each session of a tool runs in a worker thread of this host, the same file started the
4
+ * same way (its own argv, stdin and stdout), so a tool runs unchanged. ~100 MB for three.
5
+ *
6
+ * The agent still starts one command a tool (mcp-sock.sh, a few lines of sh and netcat): it connects to this
7
+ * host's socket, says on its first line which tool and with which arguments, then the session flows through.
8
+ * The relay is the same as before (mcp-relay.mjs): a version installed under a session is taken up between two
9
+ * requests -- its worker replaced, the session told its tools may have changed.
10
+ *
11
+ * The socket lives in a folder of this user alone (700): in a machine shared by several conversations, one
12
+ * conversation never reaches another's tools. Nobody connected for 10 minutes: the host stops; the next tool
13
+ * started starts it again. */
14
+ import { createServer } from 'node:net';
15
+ import { Worker } from 'node:worker_threads';
16
+ import { spawn } from 'node:child_process';
17
+ import { PassThrough } from 'node:stream';
18
+ import { createInterface } from 'node:readline';
19
+ import { existsSync, realpathSync, rmSync, statSync, readFileSync } from 'node:fs';
20
+ import { delimiter, join, isAbsolute } from 'node:path';
21
+ import { fileURLToPath } from 'node:url';
22
+ import { relay, packageOf } from './mcp-relay.mjs';
23
+
24
+ const say = (text) => process.stderr.write(`${new Date().toISOString()} tools-host: ${text}\n`);
25
+
26
+ /* The script a command runs, when it is a Node script (node FILE, or a command whose file is one): its path. */
27
+ export function scriptOf(command, args, path = process.env.PATH ?? '') {
28
+ if (/(^|\/)node$/.test(command)) return args[0] ? { file: args[0], args: args.slice(1) } : null;
29
+ const found = isAbsolute(command) ? command : path.split(delimiter).map((d) => join(d, command)).find((f) => { try { return statSync(f).isFile(); } catch { return false; } });
30
+ if (!found) return null;
31
+ let file; try { file = realpathSync(found); } catch { return null; }
32
+ if (/\.(mjs|cjs|js)$/.test(file)) return { file, args };
33
+ try { if (/^#!.*\bnode\b/.test(readFileSync(file, 'utf8').slice(0, 120))) return { file, args }; } catch { /* unreadable */ }
34
+ return null;
35
+ }
36
+
37
+ /* A worker that looks like a child process to the relay: stdin, stdout, 'exit', kill. Not a Node script: a real
38
+ child, as before. */
39
+ export function workerSpawner(command, args, opts = {}) {
40
+ const script = scriptOf(command, args);
41
+ if (!script) return spawn(command, args, opts);
42
+ const w = new Worker(script.file, { argv: script.args, stdin: true, stdout: true, env: { ...(opts.env ?? process.env) } });
43
+ w.on('error', (error) => say(`${script.file}: ${error?.stack ?? error}`));
44
+ const child = {
45
+ stdin: w.stdin, stdout: w.stdout,
46
+ on(event, f) { if (event === 'exit') w.on('exit', f); return child; },
47
+ kill() { void w.terminate(); },
48
+ };
49
+ return child;
50
+ }
51
+
52
+ /* One session: its first line names the tool { command, args }; the rest is the session, relayed to a worker. */
53
+ export function session(socket, { spawner = workerSpawner } = {}) {
54
+ const input = new PassThrough();
55
+ let header = null;
56
+ const lines = createInterface({ input: socket });
57
+ lines.on('line', (line) => {
58
+ if (header) { input.write(`${line}\n`); return; }
59
+ try { header = JSON.parse(line); } catch { header = null; }
60
+ if (!header || typeof header.command !== 'string' || !Array.isArray(header.args)) { socket.end(); return; }
61
+ const script = scriptOf(header.command, header.args);
62
+ const target = script?.file ?? header.command;
63
+ relay({ command: header.command, args: header.args.map(String), packageJson: packageOf(target) ?? '/nonexistent/package.json',
64
+ input, output: socket, spawner, log: say });
65
+ });
66
+ lines.on('close', () => input.end());
67
+ socket.on('error', () => { /* the agent went: its worker ends with its input */ });
68
+ }
69
+
70
+ export function host(dir, { idleMs = 10 * 60_000 } = {}) {
71
+ const sock = join(dir, 'host.sock');
72
+ rmSync(sock, { force: true });
73
+ let open = 0; let idle = null;
74
+ const server = createServer((socket) => {
75
+ open++; clearTimeout(idle);
76
+ socket.on('close', () => { open--; if (open === 0) idle = setTimeout(() => { server.close(); rmSync(sock, { force: true }); process.exit(0); }, idleMs); });
77
+ session(socket);
78
+ });
79
+ server.listen(sock, () => say(`listening (${process.pid})`));
80
+ idle = setTimeout(() => { if (open === 0) { server.close(); rmSync(sock, { force: true }); process.exit(0); } }, idleMs);
81
+ return server;
82
+ }
83
+
84
+ const HERE = fileURLToPath(import.meta.url);
85
+ const main = (() => { try { return realpathSync(process.argv[1] ?? '') === realpathSync(HERE); } catch { return false; } })();
86
+ if (main) {
87
+ const dir = process.argv[2];
88
+ if (!dir || !existsSync(dir)) { say('a folder of its own, given first'); process.exit(64); }
89
+ /* Its folder: this user's, closed to others -- or nothing is served. */
90
+ const st = statSync(dir);
91
+ if (st.uid !== process.getuid() || (st.mode & 0o077) !== 0) { say(`${dir} is not closed to others: nothing served`); process.exit(77); }
92
+ host(dir);
93
+ }
@@ -5,7 +5,7 @@
5
5
  mode="$1"; shift
6
6
  case "$mode" in full|ask) ;; *) echo "launch: the first argument is full or ask" >&2; exit 64 ;; esac
7
7
  [ $# -gt 0 ] || { echo "launch: no command to run" >&2; exit 64; }
8
- here="$(dirname "$0")"; relay="$here/mcp-relay.mjs"
8
+ here="$(dirname "$0")"; relay="$here/mcp-sock.sh"
9
9
  chat="${PARLEY_DIR:-/work/.parley}"; docket="/work/.docket"; tether="/work/.tether"
10
10
  # ITS KEY: a secret named MISTRAL_API_KEY given to the conversation is in its environment; Vibe reads it
11
11
  # there, and asks nothing. Without it, Vibe asks for it in its terminal at its first start.
@@ -15,19 +15,19 @@ if [ -f "$chat/server.mjs" ] && command -v parley-mcp >/dev/null 2>&1; then bloc
15
15
  [[mcp_servers]]
16
16
  name = \"parley\"
17
17
  transport = \"stdio\"
18
- command = \"node\"
18
+ command = \"sh\"
19
19
  args = [\"$relay\", \"parley-mcp\", \"--dir\", \"$chat\"]"; fi
20
20
  if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then block="$block
21
21
  [[mcp_servers]]
22
22
  name = \"docket\"
23
23
  transport = \"stdio\"
24
- command = \"node\"
24
+ command = \"sh\"
25
25
  args = [\"$relay\", \"docket-mcp\", \"--dir\", \"$docket\"]"; fi
26
26
  if command -v tether-mcp >/dev/null 2>&1; then block="$block
27
27
  [[mcp_servers]]
28
28
  name = \"tether\"
29
29
  transport = \"stdio\"
30
- command = \"node\"
30
+ command = \"sh\"
31
31
  args = [\"$relay\", \"tether-mcp\", \"--dir\", \"$tether\"]"; fi
32
32
  node "$here/toml-block.mjs" .vibe/config.toml tools "$block" \
33
33
  || echo "launch: Vibe has not its tools (MCP)" >&2
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.395-next.1",
3
+ "version": "0.2.396-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.395-next.1",
9
+ "version": "0.2.396-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.395-next.1",
3
+ "version": "0.2.396-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",