-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·474 lines (409 loc) · 15.1 KB
/
Copy pathsetup
File metadata and controls
executable file
·474 lines (409 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env bash
set -euo pipefail
# lspmux-cc setup: install a shared lspmux service, validate rust-analyzer
# availability, and print host-specific integration guidance.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OS="$(uname -s)"
HOME_DIR="${HOME}"
CARGO_HOME="${CARGO_HOME:-$HOME_DIR/.cargo}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME_DIR/.local/share}"
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}"
RUNTIME_BASE="${XDG_RUNTIME_DIR%/}"
if [ "${OS}" = "Darwin" ]; then
LSPMUX_CONFIG_DIR="${HOME_DIR}/Library/Application Support/lspmux"
else
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME_DIR/.config}"
LSPMUX_CONFIG_DIR="${XDG_CONFIG_HOME}/lspmux"
fi
LSPMUX_CONFIG_PATH="${LSPMUX_CONFIG_DIR}/config.toml"
LSPMUX_SOCKET_DIR="${RUNTIME_BASE}/lspmux"
LSPMUX_SOCKET_PATH="${LSPMUX_SOCKET_DIR}/lspmux.sock"
LSPMUX_LOG_DIR="${XDG_DATA_HOME}/lspmux/log"
LAUNCHD_DIR="${HOME_DIR}/Library/LaunchAgents"
SYSTEMD_USER_DIR="${XDG_CONFIG_HOME:-$HOME_DIR/.config}/systemd/user"
info() { echo "==> $*"; }
warn() { echo "WARNING: $*" >&2; }
die() { echo "ERROR: $*" >&2; exit 1; }
ensure_supported_platform() {
case "${OS}" in
Darwin|Linux) ;;
*) die "unsupported platform: ${OS} (expected Darwin or Linux)" ;;
esac
}
require_commands() {
for cmd in jq cargo; do
command -v "${cmd}" >/dev/null 2>&1 || die "missing required command: ${cmd}"
done
}
ensure_directories() {
mkdir -p "${LSPMUX_CONFIG_DIR}" "${LSPMUX_SOCKET_DIR}" "${LSPMUX_LOG_DIR}"
chmod 700 "${LSPMUX_SOCKET_DIR}"
if [ "${OS}" = "Darwin" ]; then
mkdir -p "${LAUNCHD_DIR}"
else
mkdir -p "${SYSTEMD_USER_DIR}"
fi
}
resolve_rust_analyzer() {
if [ -n "${RUST_ANALYZER_PATH:-}" ] && [ -x "${RUST_ANALYZER_PATH}" ]; then
printf '%s\n' "${RUST_ANALYZER_PATH}"
return 0
fi
if command -v rust-analyzer >/dev/null 2>&1; then
command -v rust-analyzer
return 0
fi
return 1
}
require_rust_analyzer() {
local ra_binary
if ra_binary="$(resolve_rust_analyzer)"; then
info "Using rust-analyzer: ${ra_binary}"
return 0
fi
die "rust-analyzer not found; set RUST_ANALYZER_PATH or add rust-analyzer to PATH (for Nix: nix build .#rust-analyzer or use the flake dev shell)"
}
install_lspmux() {
if command -v lspmux >/dev/null 2>&1; then
info "lspmux already installed: $(command -v lspmux)"
else
info "Installing lspmux via cargo..."
cargo install --locked lspmux
fi
command -v lspmux >/dev/null 2>&1 || die "lspmux not found after install"
}
write_config() {
info "Writing lspmux config to ${LSPMUX_CONFIG_PATH}..."
sed "s|\${SOCKET_PATH}|${LSPMUX_SOCKET_PATH}|g" \
"${SCRIPT_DIR}/config/lspmux.toml" > "${LSPMUX_CONFIG_PATH}"
}
# Service-manager deployment (deploy_launchd_plist / deploy_systemd_units /
# start_core_service) was removed in M5. The MCP server spawns lspmux on
# demand. Users who explicitly want auto-start-on-login can install the
# in-tree plist/unit themselves and set LSPMUX_ALLOW_MANAGER_BOOTSTRAP=1.
cmd_migrate() {
info "Removing legacy lspmux service-manager units..."
local removed=0
if [ "${OS}" = "Darwin" ]; then
local label="com.lspmux.server"
local plist="${LAUNCHD_DIR}/${label}.plist"
if launchctl print "gui/$(id -u)/${label}" >/dev/null 2>&1; then
info " unloading ${label}"
launchctl bootout "gui/$(id -u)/${label}" 2>/dev/null || true
removed=$((removed + 1))
fi
if [ -f "${plist}" ]; then
info " removing ${plist}"
rm -f "${plist}"
removed=$((removed + 1))
fi
else
local unit="${SYSTEMD_USER_DIR}/lspmux.service"
if command -v systemctl >/dev/null 2>&1; then
# Gate on is-enabled or is-active rather than is-active alone:
# an enabled-but-stopped (or failed) pre-M5 unit otherwise leaves
# a dangling default.target.wants/lspmux.service symlink that
# reloads or fails on the next user session.
if systemctl --user is-enabled --quiet lspmux.service 2>/dev/null \
|| systemctl --user is-active --quiet lspmux.service 2>/dev/null; then
info " disabling lspmux.service"
systemctl --user disable --now lspmux.service 2>/dev/null || true
removed=$((removed + 1))
fi
fi
if [ -f "${unit}" ]; then
info " removing ${unit}"
rm -f "${unit}"
removed=$((removed + 1))
fi
fi
if [ "${removed}" -eq 0 ]; then
info "No legacy units found. Nothing to migrate."
else
info "Migration complete."
info "If you use nix-darwin or home-manager to install the plist, remove"
info "that module declaration too — this script can't manage it."
fi
}
print_core_summary() {
local ra_binary
ra_binary="$(resolve_rust_analyzer)"
echo
echo "Core setup complete."
echo "lspmux binary: $(command -v lspmux)"
echo "rust-analyzer binary: ${ra_binary}"
echo "Config: ${LSPMUX_CONFIG_PATH}"
echo "Socket: ${LSPMUX_SOCKET_PATH}"
echo "Logs: ${LSPMUX_LOG_DIR}"
echo "Daemon model: spawned on-demand by the MCP server (no system service)."
}
cmd_core() {
ensure_supported_platform
require_commands
ensure_directories
require_rust_analyzer
install_lspmux
write_config
# No service-manager deployment post-M5: the MCP server spawns lspmux
# on demand. Run `./setup migrate` to remove a pre-M5 install.
print_core_summary
}
cmd_host_claude_code() {
cat <<EOF
Claude Code host adapter
Prerequisites:
1. Run core setup first (starts the shared service outside the sandbox):
./setup core
2. Configure the sandbox to allow Unix socket access:
./setup sandbox claude-code
(or manually add "${LSPMUX_SOCKET_PATH}" to
sandbox.network.allowUnixSockets in ~/.claude/settings.json)
Install:
3. Register the marketplace entry:
claude plugin add-marketplace ${SCRIPT_DIR}
4. Disable Claude's built-in rust-analyzer:
claude plugin disable rust-analyzer-lsp --scope user
5. Install the lspmux Rust plugin:
claude plugin install lspmux-rust-cc --scope user
Verify:
./setup doctor
Reference: ${SCRIPT_DIR}/docs/hosts/claude-code.md
EOF
}
cmd_host_codex() {
local fallback_lspmux
fallback_lspmux="\$HOME/.cargo/bin/lspmux"
cat <<EOF
Codex host adapter
Set these environment variables for the MCP process:
WORKSPACE_ROOT=/absolute/path/to/workspace
LSPMUX_BOOTSTRAP=auto
LSPMUX_PATH=$(command -v lspmux 2>/dev/null || echo "${fallback_lspmux}")
RUST_ANALYZER_PATH=$(resolve_rust_analyzer 2>/dev/null || echo /absolute/path/to/rust-analyzer)
LSPMUX_CONFIG_PATH=${LSPMUX_CONFIG_PATH}
LSPMUX_SOCKET_PATH=${LSPMUX_SOCKET_PATH}
LSPMUX_CLIENT_KIND=codex_mcp
LSPMUX_CLIENT_HOST=codex
LSPMUX_SESSION_ID=codex-\$(date +%s)-\$$
Launch the MCP server with:
${SCRIPT_DIR}/bin/lspmux-cc-mcp
Reference: ${SCRIPT_DIR}/docs/hosts/codex.md
EOF
}
cmd_host_opencode() {
local ra_binary
ra_binary="$(resolve_rust_analyzer 2>/dev/null || echo /absolute/path/to/rust-analyzer)"
cat <<EOF
opencode host adapter
opencode wires lspmux-cc through opencode.json: an MCP server (always) and,
optionally, an LSP override of the built-in "rust" server.
MCP server "environment" block:
WORKSPACE_ROOT=/absolute/path/to/workspace
LSPMUX_BOOTSTRAP=auto
LSPMUX_CONNECT=tcp://127.0.0.1:27631
RUST_ANALYZER_PATH=${ra_binary}
LSPMUX_CONFIG_PATH=${LSPMUX_CONFIG_PATH}
LSPMUX_CLIENT_KIND=opencode_mcp
LSPMUX_CLIENT_HOST=opencode
MCP server "command":
${SCRIPT_DIR}/bin/lspmux-cc-mcp
Optional LSP override (lsp.rust), to pool opencode's native Rust intelligence:
command: ${SCRIPT_DIR}/bin/lspmux client --server-path ${ra_binary}
env: LSPMUX_CONNECT=tcp://127.0.0.1:27631, LSPMUX_CLIENT_KIND=opencode_lsp,
LSPMUX_CLIENT_HOST=opencode
Keep WORKSPACE_ROOT in a project-scoped opencode.json, or set it to
"{env:WORKSPACE_ROOT}" in a global config. The paste-ready opencode.json
(MCP-only and MCP+LSP) is in the reference below.
Reference: ${SCRIPT_DIR}/docs/hosts/opencode.md
EOF
}
cmd_host_generic_mcp() {
cat <<EOF
Generic MCP adapter
Use the same environment contract as Codex and launch:
${SCRIPT_DIR}/bin/lspmux-cc-mcp
Reference: ${SCRIPT_DIR}/docs/hosts/generic-mcp.md
EOF
}
cmd_sandbox_claude_code() {
local settings_file="${HOME_DIR}/.claude/settings.json"
local socket_path="${LSPMUX_SOCKET_PATH}"
info "Configuring Claude Code sandbox for lspmux..."
if ! command -v jq >/dev/null 2>&1; then
die "jq is required for sandbox configuration"
fi
# Create settings file if missing
if [ ! -f "${settings_file}" ]; then
mkdir -p "$(dirname "${settings_file}")"
echo '{}' > "${settings_file}"
info "Created ${settings_file}"
fi
# Check if allowAllUnixSockets is true (warn against it)
if jq -e '.sandbox.network.allowAllUnixSockets == true' "${settings_file}" >/dev/null 2>&1; then
warn "allowAllUnixSockets is true in ${settings_file}"
warn "This exposes Docker, SSH agent, and other daemon sockets to the sandbox."
warn "Consider replacing with specific socket paths."
fi
# Check if socket path already present
if jq -e --arg path "${socket_path}" '.sandbox.network.allowUnixSockets // [] | index($path) != null' "${settings_file}" >/dev/null 2>&1; then
info "Socket path already in allowUnixSockets: ${socket_path}"
return 0
fi
# Add socket path to allowUnixSockets
local tmp_file
tmp_file="$(mktemp)"
jq --arg path "${socket_path}" '
.sandbox.network.allowUnixSockets = ((.sandbox.network.allowUnixSockets // []) + [$path])
' "${settings_file}" > "${tmp_file}" && mv "${tmp_file}" "${settings_file}"
info "Added ${socket_path} to allowUnixSockets in ${settings_file}"
}
cmd_doctor() {
ensure_supported_platform
local failures=0
echo "lspmux doctor"
echo " platform: ${OS}"
echo " config: ${LSPMUX_CONFIG_PATH}"
echo " socket: ${LSPMUX_SOCKET_PATH}"
if command -v lspmux >/dev/null 2>&1; then
echo " [ok] lspmux: $(command -v lspmux)"
else
echo " [!!] lspmux: missing"
failures=$((failures + 1))
fi
local ra_binary
if ra_binary="$(resolve_rust_analyzer)"; then
echo " [ok] rust-analyzer: ${ra_binary}"
else
echo " [!!] rust-analyzer: missing (set RUST_ANALYZER_PATH or add rust-analyzer to PATH)"
failures=$((failures + 1))
fi
if [ -f "${LSPMUX_CONFIG_PATH}" ]; then
echo " [ok] config file: present"
else
echo " [!!] config file: missing"
failures=$((failures + 1))
fi
if [ -S "${LSPMUX_SOCKET_PATH}" ]; then
echo " [ok] shared socket: ready (daemon is up)"
else
echo " [--] shared socket: not present (MCP will spawn lspmux on demand)"
fi
# Post-M5: presence of a service-manager unit is a *migration* signal,
# not a missing-prerequisite. Warn so the user runs `./setup migrate`.
if [ "${OS}" = "Darwin" ]; then
if launchctl print "gui/$(id -u)/com.lspmux.server" >/dev/null 2>&1; then
echo " [~~] legacy launchd unit loaded — run './setup migrate' to remove"
else
echo " [ok] no legacy launchd unit loaded"
fi
else
if command -v systemctl >/dev/null 2>&1 \
&& systemctl --user is-active --quiet lspmux.service 2>/dev/null; then
echo " [~~] legacy lspmux.service active — run './setup migrate' to remove"
else
echo " [ok] no legacy systemd unit active"
fi
fi
# Claude Code checks (optional)
if command -v claude >/dev/null 2>&1; then
echo
echo " Claude Code integration (advisory):"
# Check plugin installed
if claude plugin list 2>/dev/null | grep -q "lspmux-rust-cc"; then
echo " [ok] lspmux-rust-cc plugin installed"
else
echo " [~~] lspmux-rust-cc plugin not installed"
fi
# Check built-in rust-analyzer disabled
if claude plugin list 2>/dev/null | grep -q "rust-analyzer-lsp.*enabled"; then
echo " [~~] built-in rust-analyzer-lsp is still enabled"
else
echo " [ok] built-in rust-analyzer-lsp disabled (or not present)"
fi
# Check allowUnixSockets
local settings_file="${HOME_DIR}/.claude/settings.json"
if [ -f "${settings_file}" ]; then
if jq -e --arg path "${LSPMUX_SOCKET_PATH}" '.sandbox.network.allowUnixSockets // [] | index($path) != null' "${settings_file}" >/dev/null 2>&1; then
echo " [ok] socket path in allowUnixSockets"
elif jq -e '.sandbox.network.allowAllUnixSockets == true' "${settings_file}" >/dev/null 2>&1; then
echo " [~~] allowAllUnixSockets is true (insecure; use specific paths)"
else
echo " [~~] socket path not in allowUnixSockets"
echo " run: ./setup sandbox claude-code"
fi
else
echo " [--] ~/.claude/settings.json not found (skipping sandbox check)"
fi
else
echo
echo " [--] claude CLI not found (skipping Claude Code checks)"
fi
# Socket directory permissions
if [ -d "${LSPMUX_SOCKET_DIR}" ]; then
local perms
perms="$(stat -f '%Lp' "${LSPMUX_SOCKET_DIR}" 2>/dev/null || stat -c '%a' "${LSPMUX_SOCKET_DIR}" 2>/dev/null || echo "unknown")"
if [ "${perms}" = "700" ]; then
echo " [ok] socket directory permissions: ${perms}"
else
echo " [!!] socket directory permissions: ${perms} (expected 700)"
fi
fi
[ "${failures}" -eq 0 ] || exit 1
}
cmd_help() {
cat <<EOF
Usage:
./setup core
./setup host claude-code
./setup host codex
./setup host opencode
./setup host generic-mcp
./setup sandbox claude-code
./setup doctor
./setup migrate # remove pre-M5 launchd/systemd unit
Running ./setup with no arguments is equivalent to:
./setup core
./setup host claude-code
EOF
}
main() {
local command="${1:-default}"
case "${command}" in
default)
cmd_core
echo
cmd_host_claude_code
;;
core)
cmd_core
;;
host)
case "${2:-}" in
claude-code) cmd_host_claude_code ;;
codex) cmd_host_codex ;;
opencode) cmd_host_opencode ;;
generic-mcp) cmd_host_generic_mcp ;;
*) die "unknown host adapter: ${2:-<missing>}" ;;
esac
;;
sandbox)
case "${2:-}" in
claude-code) cmd_sandbox_claude_code ;;
*) die "unknown sandbox target: ${2:-<missing>}" ;;
esac
;;
doctor)
cmd_doctor
;;
migrate)
cmd_migrate
;;
help|-h|--help)
cmd_help
;;
*)
die "unknown command: ${command}"
;;
esac
}
main "$@"