Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·425 lines (384 loc) · 15.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·425 lines (384 loc) · 15.3 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
#!/bin/sh
# install.sh — Install lean-ctx (download pre-built binary or build from source)
#
# Usage:
# ./install.sh # download pre-built binary (no Rust needed)
# ./install.sh --download # download pre-built binary (no Rust needed)
# ./install.sh --cuda # download Linux x86_64 CUDA-enabled binary
# ./install.sh --build-only # build only, don't install
# ./install.sh --release # source install with the production release profile
# ./install.sh --uninstall # fully remove lean-ctx (processes, configs, autostart, data, binary)
#
# One-liner (no Rust required):
# curl -fsSL https://leanctx.com/install.sh | sh
# curl -fsSL https://leanctx.com/install.sh | bash
#
# Uninstall one-liner:
# curl -fsSL https://leanctx.com/install.sh | sh -s -- --uninstall
set -eu
REPO="yvgude/lean-ctx"
INSTALL_DIR="${LEAN_CTX_INSTALL_DIR:-$HOME/.local/bin}"
INSTALL_FLAVOR="${LEAN_CTX_INSTALL_FLAVOR:-cpu}"
# Resolve the script's directory when invoked as a file. When piped via
# `curl ... | sh`, $0 is "sh" (or similar) — the [ -f "$0" ] guard then
# falls back to pwd, which is what the bottom-of-file dispatcher expects:
# RUST_DIR check fails outside the repo, so we route to install_download.
SCRIPT_DIR="$(
src="$0"
if [ -n "$src" ] && [ -f "$src" ]; then
cd "$(dirname "$src")" 2>/dev/null && pwd
else
pwd
fi
)"
RUST_DIR="$SCRIPT_DIR/rust"
echo "LeanCTX installer — the Context Gateway for AI Systems"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
finish() {
# --- Auto-fix PATH if needed ---
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*)
echo ""
shell_name="$(basename "${SHELL:-bash}" 2>/dev/null || echo bash)"
rc="$HOME/.bashrc"
case "$shell_name" in
zsh) rc="$HOME/.zshrc" ;;
fish) rc="$HOME/.config/fish/config.fish" ;;
esac
if [ "${LEAN_CTX_NO_PATH_FIX:-}" = "1" ]; then
echo "Warning: $INSTALL_DIR is not in your PATH."
echo " Add it manually, then run: lean-ctx onboard"
else
echo "Adding $INSTALL_DIR to PATH..."
if [ "$shell_name" = "fish" ]; then
printf '\nfish_add_path %s\n' "$INSTALL_DIR" >> "$rc" 2>/dev/null || true
else
printf '\nexport PATH="%s:$PATH"\n' "$INSTALL_DIR" >> "$rc" 2>/dev/null || true
if [ "$shell_name" = "bash" ] && [ "$(uname -s)" = "Darwin" ]; then
grep -qs '.bashrc' "$HOME/.bash_profile" 2>/dev/null || \
printf '\n[ -f ~/.bashrc ] && . ~/.bashrc\n' >> "$HOME/.bash_profile" 2>/dev/null || true
fi
fi
export PATH="$INSTALL_DIR:$PATH"
echo " Done. PATH updated in $rc and current session."
fi
;;
esac
echo ""
echo "Done! lean-ctx $(\"$INSTALL_DIR/lean-ctx\" --version 2>/dev/null || echo 'installed')."
# --- Auto-onboard unless opted out ---
if [ "${LEAN_CTX_NO_ONBOARD:-}" = "1" ]; then
echo ""
echo "Next step: Run 'lean-ctx onboard' to connect your AI tools."
return
fi
echo ""
echo "Running onboard (connecting your AI tools)..."
if [ -n "${LEAN_CTX_AGENTS:-}" ]; then
"$INSTALL_DIR/lean-ctx" onboard --agents "$LEAN_CTX_AGENTS" 2>&1 || true
elif ( : </dev/tty ) 2>/dev/null; then
# `curl | sh` feeds the script on stdin; read the agent choice from the
# terminal instead. Without one (CI, agents) onboard connects every tool.
"$INSTALL_DIR/lean-ctx" onboard </dev/tty 2>&1 || true
else
"$INSTALL_DIR/lean-ctx" onboard 2>&1 || true
fi
# --- Detect installed agents and suggest wrap ---
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Setup complete! Quick start:"
echo ""
echo " lean-ctx wrap cursor # one-command setup for Cursor"
echo " lean-ctx wrap claude # one-command setup for Claude Code"
echo " lean-ctx wrap codex # one-command setup for Codex CLI"
echo ""
echo " lean-ctx doctor # verify installation"
echo " lean-ctx inspect # inspect recorded context operations and evidence"
echo " lean-ctx gain # usage estimates and calculated cost"
echo ""
echo "Full control: lean-ctx setup (interactive wizard)"
echo "Preview: lean-ctx setup --dry-run (writes nothing)"
echo "Pick tools: curl ... | LEAN_CTX_AGENTS=claude,cursor sh"
echo "Skip auto-onboard: curl ... | LEAN_CTX_NO_ONBOARD=1 sh"
}
detect_target() {
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$arch" in
x86_64) arch="x86_64" ;;
arm64|aarch64) arch="aarch64" ;;
*)
echo "Error: unsupported architecture '$arch'"
echo "Build from source instead: ./install.sh"
exit 1 ;;
esac
case "$os" in
linux)
libc="musl"
if command -v ldd >/dev/null 2>&1; then
glibc_ver="$(ldd --version 2>&1 | head -1 | grep -oE '[0-9]+\.[0-9]+$' || true)"
if [ -n "$glibc_ver" ]; then
major="${glibc_ver%%.*}"
minor="${glibc_ver##*.}"
if [ "$major" -gt 2 ] || { [ "$major" -eq 2 ] && [ "$minor" -ge 35 ]; }; then
libc="gnu"
fi
fi
fi
echo "${arch}-unknown-linux-${libc}"
;;
darwin) echo "${arch}-apple-darwin" ;;
*)
echo "Error: unsupported OS '$os'"
echo "Windows: download from https://github.com/${REPO}/releases/latest"
exit 1 ;;
esac
}
# Checksum verification fails closed (GHSA-rxv5-4c3x-g4q6 D): a missing
# SHA256SUMS, a missing entry or a missing hash tool aborts before anything
# is installed, so a blocked or stripped checksum fetch cannot downgrade the
# install to an unverified binary.
require_sha256_tool() {
if command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1; then
return 0
fi
echo "Error: neither sha256sum nor shasum is available; refusing to install an unverified binary." >&2
echo " Install coreutils (sha256sum) or perl (shasum) and re-run the installer." >&2
exit 1
}
verify_checksum() {
file="$1"
expected="$2"
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "$file" | cut -d' ' -f1)"
elif command -v shasum >/dev/null 2>&1; then
actual="$(shasum -a 256 "$file" | cut -d' ' -f1)"
else
require_sha256_tool
fi
if [ -z "$expected" ] || [ "$actual" != "$expected" ]; then
echo "Error: checksum mismatch!"
echo " Expected: $expected"
echo " Got: $actual"
exit 1
fi
echo " Checksum verified ✓"
}
# Stop any running lean-ctx before swapping the binary. The proxy runs as a
# LaunchAgent/systemd unit with KeepAlive, so without this it keeps a stale
# binary alive (and may respawn mid-swap); `lean-ctx stop` boots it out so the
# freshly installed binary is picked up cleanly on next use. No-op on a first
# install (nothing on PATH yet). Best-effort — never fails the install.
stop_running_instance() {
if command -v lean-ctx >/dev/null 2>&1; then
echo "Stopping running lean-ctx (if any)..."
lean-ctx stop >/dev/null 2>&1 || true
fi
}
install_download() {
target="$(detect_target)"
case "$INSTALL_FLAVOR" in
cuda|gpu)
if [ "$target" != "x86_64-unknown-linux-gnu" ]; then
echo "Error: CUDA pre-built binary is currently published for x86_64 GNU/Linux only."
echo "Detected: $target"
exit 1
fi
target="${target}-cuda"
;;
cpu|"") ;;
*)
echo "Error: unknown install flavor '$INSTALL_FLAVOR' (expected cpu or cuda)"
exit 1
;;
esac
echo "Mode: download pre-built binary"
echo "Platform: $target"
echo ""
require_sha256_tool
echo "Fetching latest release..."
latest="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' | head -1 | cut -d'"' -f4)"
if [ -z "$latest" ]; then
echo "Error: could not determine latest release."
exit 1
fi
echo "Latest: $latest"
asset_url="https://github.com/${REPO}/releases/download/${latest}/lean-ctx-${target}.tar.gz"
sums_url="https://github.com/${REPO}/releases/download/${latest}/SHA256SUMS"
tmpdir="$(mktemp -d)"
tmp_bin=""
trap 'rm -rf "${tmpdir:-}"; [ -n "${tmp_bin:-}" ] && rm -f "${tmp_bin:-}" 2>/dev/null || true' EXIT
echo "Downloading binary..."
if ! curl -fsSL "$asset_url" -o "$tmpdir/lean-ctx.tar.gz"; then
echo "Error: download failed. Check: https://github.com/${REPO}/releases"
exit 1
fi
echo "Downloading checksums..."
if ! curl -fsSL "$sums_url" -o "$tmpdir/SHA256SUMS"; then
echo "Error: could not download SHA256SUMS for ${latest}; refusing to install an unverified binary." >&2
echo " Check your network/proxy and retry, or verify manually: https://github.com/${REPO}/releases/tag/${latest}" >&2
exit 1
fi
# Exact file-name match (sha256sum text or binary mode); exactly one entry.
expected="$(awk -v name="lean-ctx-${target}.tar.gz" \
'{ f = $2; sub(/^\*/, "", f) } f == name { print $1; n++ } END { if (n != 1) exit 1 }' \
"$tmpdir/SHA256SUMS")" || expected=""
if [ -z "$expected" ]; then
echo "Error: SHA256SUMS for ${latest} has no unique entry for lean-ctx-${target}.tar.gz; refusing to install." >&2
exit 1
fi
verify_checksum "$tmpdir/lean-ctx.tar.gz" "$expected"
echo " Signed checksums: verify SHA256SUMS with cosign, see https://github.com/${REPO}/blob/main/SECURITY.md#verifying-release-artifacts"
tar -xzf "$tmpdir/lean-ctx.tar.gz" -C "$tmpdir"
mkdir -p "$INSTALL_DIR"
tmp_bin="$INSTALL_DIR/.lean-ctx.new.$$"
install -m755 "$tmpdir/lean-ctx" "$tmp_bin"
if [ "$(uname -s)" = "Darwin" ]; then
xattr -cr "$tmp_bin" 2>/dev/null || true
codesign --force --sign - "$tmp_bin" 2>/dev/null || true
fi
stop_running_instance
mv -f "$tmp_bin" "$INSTALL_DIR/lean-ctx"
tmp_bin=""
echo " Installed: $INSTALL_DIR/lean-ctx"
finish
}
install_from_source() {
if ! command -v cargo >/dev/null 2>&1; then
echo "Error: cargo not found. Install Rust: https://rustup.rs"
echo "Or download a pre-built binary: $0 --download"
exit 1
fi
build_only="${1:-}"
source_profile="${2:-dev-fast}"
echo "Mode: build from source"
echo ""
echo "Building lean-ctx ($source_profile)..."
if [ -d "$RUST_DIR" ]; then
if [ "$source_profile" = "dev-fast" ]; then
(cd "$RUST_DIR" && cargo fast)
else
(cd "$RUST_DIR" && cargo build --locked --release --timings)
fi
# Honour CARGO_TARGET_DIR / a config.toml [build] target-dir override
# (GH #671): with a hardcoded ./target, a stale binary from an earlier
# default-layout build gets linked instead of the one just built.
# `|| true` keeps a failing `cargo metadata` on the fallback path.
target_dir=$( (cd "$RUST_DIR" && cargo metadata --no-deps --format-version=1 2>/dev/null) \
| grep -o '"target_directory":"[^"]*"' \
| head -1 \
| sed -E 's/^"target_directory":"(.*)"$/\1/' \
| sed 's/\\\\/\//g' || true)
binary="${target_dir:-$RUST_DIR/target}/$source_profile/lean-ctx"
else
echo "Error: source builds require the lean-ctx checkout. Use --download for a pre-built binary." >&2
exit 1
fi
if [ ! -x "$binary" ]; then
echo "Error: build failed — binary not found at $binary"
echo "Hint: is CARGO_TARGET_DIR or ~/.cargo/config.toml [build] target-dir pointing elsewhere?"
exit 1
fi
echo "Built: $binary"
if [ "$build_only" = "--build-only" ]; then
echo "Done (build only)."
return
fi
mkdir -p "$INSTALL_DIR"
tmp_bin="$INSTALL_DIR/.lean-ctx.new.$$"
trap '[ -n "${tmp_bin:-}" ] && rm -f "$tmp_bin" 2>/dev/null || true' EXIT
install -m755 "$binary" "$tmp_bin"
if [ "$(uname -s)" = "Darwin" ]; then
xattr -cr "$tmp_bin" 2>/dev/null || true
codesign --force --sign - "$tmp_bin" 2>/dev/null || true
fi
stop_running_instance
mv -f "$tmp_bin" "$INSTALL_DIR/lean-ctx"
tmp_bin=""
echo " Installed: $INSTALL_DIR/lean-ctx"
finish
}
uninstall() {
echo "Mode: uninstall"
echo ""
# The binary's own `uninstall` does the thorough cleanup — stops every process, then
# removes hooks, MCP configs, rules, autostart (LaunchAgent/systemd), data, and the
# binary itself. Prefer it; forward any extra flags (e.g. --keep-config, --dry-run).
if command -v lean-ctx >/dev/null 2>&1; then
lean-ctx uninstall "$@" || true
else
echo "lean-ctx not on PATH — removing known artifacts directly."
if [ "$(uname -s)" = "Darwin" ]; then
for label in com.leanctx.proxy com.leanctx.daemon; do
plist="$HOME/Library/LaunchAgents/$label.plist"
[ -f "$plist" ] && launchctl unload "$plist" 2>/dev/null || true
rm -f "$plist" 2>/dev/null || true
done
else
for svc in lean-ctx-proxy lean-ctx-daemon; do
systemctl --user disable --now "$svc" 2>/dev/null || true
rm -f "$HOME/.config/systemd/user/$svc.service" 2>/dev/null || true
done
systemctl --user daemon-reload 2>/dev/null || true
fi
rm -rf "$HOME/.lean-ctx" "$HOME/.config/lean-ctx" 2>/dev/null || true
echo " Removed autostart + data dir."
echo " (Reinstall the binary and run 'lean-ctx uninstall' for full editor-config cleanup.)"
fi
# Belt-and-suspenders: ensure the binary + PATH symlinks install.sh created are gone,
# even if the self-delete failed or the binary was never on PATH.
for b in "$INSTALL_DIR/lean-ctx" "/usr/local/bin/lean-ctx"; do
if [ -e "$b" ] || [ -L "$b" ]; then
rm -f "$b" 2>/dev/null && echo " Removed $b" || true
fi
done
echo ""
echo "lean-ctx uninstalled. Restart your shell to drop stale aliases."
echo "Verify with: command -v lean-ctx # should print nothing"
}
case "${1:-}" in
--download) install_download ;;
--cuda|--gpu) INSTALL_FLAVOR="cuda"; install_download ;;
--build-only)
case "$#:${2:-}" in
1:) install_from_source --build-only ;;
2:--release) install_from_source --build-only release ;;
*) echo "Usage: $0 --build-only [--release]" >&2; exit 2 ;;
esac
;;
--release)
[ "$#" = 1 ] || { echo "Usage: $0 --release" >&2; exit 2; }
install_from_source "" release
;;
--uninstall) shift; uninstall "$@" ;;
--help|-h)
echo "Usage: $0 [--download|--cuda|--build-only [--release]|--release|--uninstall|--help]"
echo ""
echo " (no args) Download pre-built binary (builds from source if run inside the lean-ctx repo)"
echo " --download Download pre-built binary (no Rust needed)"
echo " --cuda Download Linux x86_64 CUDA-enabled binary"
echo " --build-only Fast incremental source build only, don't install"
echo " --release Source install with the production release profile"
echo " --uninstall Fully remove lean-ctx (processes, configs, autostart, data, binary)"
echo ""
echo "Uninstall one-liner:"
echo " curl -fsSL https://leanctx.com/install.sh | sh -s -- --uninstall"
echo ""
echo "Environment:"
echo " LEAN_CTX_INSTALL_DIR Custom install directory (default: ~/.local/bin)"
echo " LEAN_CTX_INSTALL_FLAVOR Binary flavor: cpu or cuda (default: cpu)"
echo " LEAN_CTX_AGENTS AI tools to connect, e.g. claude,cursor (default: ask on a"
echo " terminal, otherwise every detected tool)"
echo " LEAN_CTX_NO_ONBOARD=1 Install the binary only; connect tools later"
;;
"")
if [ -d "$RUST_DIR" ]; then
install_from_source
else
install_download
fi
;;
*) echo "Error: unknown option '$1'; use --help" >&2; exit 2 ;;
esac