install.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/bin/sh
  2. # dsh one-line installer.
  3. #
  4. # curl -fsSL https://raw.githubusercontent.com/deepseek-harness/deepseek-harness/master/scripts/install.sh | sh
  5. #
  6. # It clones the harness under ~/.dsh/source (the master clone at
  7. # ~/.dsh/source/master), adds a per-install staging worktree at
  8. # ~/.dsh/source/staging-<timestamp> on branch dsh-staging/<timestamp>, checks
  9. # host dependencies (git, Node, pnpm) and offers to install a missing pnpm, runs
  10. # `pnpm install` (no build — the `bin/dsh` launcher runs the TypeScript source
  11. # through the repo's own tsx), points the stable `~/.dsh/source/current` symlink
  12. # at that staging worktree and symlinks `dsh` onto PATH at `current/bin/dsh`,
  13. # records your API credentials in the Harness home (`~/.dsh`) dsh reads at boot,
  14. # and drops you into `dsh`. Keeping every checkout under ~/.dsh/source keeps
  15. # successive upgrades in one place instead of scattered sibling clones, and lets
  16. # staging worktrees share the master clone's object store. The PATH symlink
  17. # resolves through `current`, so an upgrade repoints one stable symlink instead
  18. # of relinking PATH: the `dsh` on PATH never moves and can never dangle.
  19. #
  20. # When run from inside an existing checkout (e.g. `sh scripts/install.sh` rather
  21. # than `curl ... | sh`) it reuses that checkout in place and skips the
  22. # clone/worktree setup, leaving the working tree untouched and linking `dsh`
  23. # straight at that checkout's `bin/dsh` (no `current` indirection — the checkout
  24. # is not a managed staging worktree under the source container); DSH_REF is
  25. # ignored in that mode. Setting DSH_SOURCE to a different directory opts back
  26. # into the normal clone/worktree path.
  27. #
  28. # When run through `curl | sh` the script text arrives on stdin, so every
  29. # prompt and the final launch read the controlling terminal (/dev/tty) directly;
  30. # with no terminal the script prints the manual next steps instead.
  31. #
  32. # Overridable via environment:
  33. # DSH_REF branch or tag to clone/checkout (default: master)
  34. # DSH_REPO clone URL (default: the GitHub repo)
  35. # DSH_SOURCE source container directory (default: ~/.dsh/source)
  36. # DSH_MASTER master clone directory (default: $DSH_SOURCE/master)
  37. # DSH_CURRENT stable symlink to the active worktree (default: $DSH_SOURCE/current)
  38. # DSH_BIN_DIR directory the `dsh` symlink lands in (default: ~/.local/bin)
  39. # DSH_HOME Harness home holding the personal config (default: ~/.dsh)
  40. # FIXME(install-ts): Move the post-checkout workflow into a tested TypeScript
  41. # entrypoint; keep this POSIX shell file as the curl/source bootstrap.
  42. set -eu
  43. DSH_REF=${DSH_REF:-master}
  44. DSH_REPO=${DSH_REPO:-https://github.com/deepseek-harness/deepseek-harness.git}
  45. # DSH_SOURCE is the container directory that holds the master clone and every
  46. # staging worktree; DSH_MASTER is the one real clone inside it. Remember whether
  47. # the caller pinned the source container before defaulting it, so in-repo
  48. # detection only repoints an unset DSH_SOURCE.
  49. if [ -n "${DSH_SOURCE:-}" ]; then DSH_SOURCE_EXPLICIT=1; else DSH_SOURCE_EXPLICIT=0; fi
  50. DSH_SOURCE=${DSH_SOURCE:-$HOME/.dsh/source}
  51. DSH_MASTER=${DSH_MASTER:-$DSH_SOURCE/master}
  52. # The stable symlink the PATH launcher resolves through: PATH -> current/bin/dsh
  53. # -> <staging>/bin/dsh. Fresh installs and upgrades repoint this one symlink; the
  54. # PATH launcher itself is written once and never moves. In-repo reuse ignores it.
  55. DSH_CURRENT=${DSH_CURRENT:-$DSH_SOURCE/current}
  56. DSH_BIN_DIR=${DSH_BIN_DIR:-$HOME/.local/bin}
  57. # One UTC basic timestamp names this install's staging branch and worktree.
  58. DSH_STAMP=$(date -u +%Y%m%dT%H%M%SZ)
  59. DSH_STAGING_BRANCH=dsh-staging/$DSH_STAMP
  60. DSH_STAGING=$DSH_SOURCE/staging-$DSH_STAMP
  61. # --- in-repo detection ---------------------------------------------------------
  62. # Under `curl ... | sh` the script text arrives on stdin, so $0 is the shell
  63. # name and no file path resolves; running a checked-out copy (`sh
  64. # scripts/install.sh`) makes $0 the script file. When $0 is a readable file whose
  65. # parent is a scripts/ dir inside a real dsh checkout (bin/dsh launcher present),
  66. # reuse that checkout in place — link `dsh` straight at it and skip the
  67. # clone/worktree setup. An explicit DSH_SOURCE pointing elsewhere opts back into
  68. # the clone/worktree path.
  69. IN_REPO=0
  70. if [ -f "$0" ]; then
  71. _self_dir=$(CDPATH= cd -- "$(dirname -- "$0")" 2>/dev/null && pwd -P) || _self_dir=''
  72. if [ -n "$_self_dir" ]; then
  73. _repo_root=$(dirname -- "$_self_dir")
  74. if [ "$(basename -- "$_self_dir")" = scripts ] \
  75. && [ -x "$_repo_root/bin/dsh" ] && [ -f "$_repo_root/scripts/install.sh" ]; then
  76. if [ "$DSH_SOURCE_EXPLICIT" = 0 ] || [ "$DSH_SOURCE" = "$_repo_root" ]; then
  77. IN_REPO=1
  78. # In-repo reuse links `dsh` at this checkout as-is; the master/staging
  79. # split applies only to fresh clone installs.
  80. DSH_STAGING=$_repo_root
  81. fi
  82. fi
  83. fi
  84. fi
  85. # --- terminal-aware prompting --------------------------------------------------
  86. # stdin is the piped script, so read the controlling terminal for input.
  87. if { true </dev/tty; } 2>/dev/null; then
  88. HAS_TTY=1
  89. # Restore terminal echo on exit or interrupt: ask_secret disables echo between
  90. # its stty toggles, and dash (a common `sh`) does not run an EXIT trap when the
  91. # shell is killed by a signal, so the fatal signals need their own handler. A
  92. # successful run ends in exec, which replaces this process and drops the traps.
  93. trap 'stty echo </dev/tty 2>/dev/null || true' EXIT
  94. trap 'stty echo </dev/tty 2>/dev/null || true; exit 130' INT TERM HUP
  95. else
  96. HAS_TTY=0
  97. fi
  98. # Colour only when writing to a terminal.
  99. if [ -t 1 ]; then
  100. B=$(printf '\033[1m'); DIM=$(printf '\033[2m'); RED=$(printf '\033[31m')
  101. GRN=$(printf '\033[32m'); YEL=$(printf '\033[33m'); RST=$(printf '\033[0m')
  102. else
  103. B=''; DIM=''; RED=''; GRN=''; YEL=''; RST=''
  104. fi
  105. info() { printf '%s==>%s %s\n' "$GRN" "$RST" "$1"; }
  106. step() { printf '\n%s==>%s %s%s%s\n' "$GRN" "$RST" "$B" "$1" "$RST"; }
  107. warn() { printf '%s warn%s %s\n' "$YEL" "$RST" "$1" >&2; }
  108. die() { printf '%serror%s %s\n' "$RED" "$RST" "$1" >&2; exit 1; }
  109. # ask PROMPT [DEFAULT] -> answer on stdout (plain-text line).
  110. ask() {
  111. [ "$HAS_TTY" = 1 ] || die "no terminal available for input; re-run in an interactive shell"
  112. printf '%s%s%s ' "$B" "$1" "$RST" >/dev/tty
  113. IFS= read -r _ans </dev/tty || _ans=''
  114. [ -n "$_ans" ] || _ans=${2:-}
  115. printf '%s' "$_ans"
  116. }
  117. # ask_secret PROMPT -> answer on stdout, with terminal echo suppressed.
  118. ask_secret() {
  119. [ "$HAS_TTY" = 1 ] || die "no terminal available for input; re-run in an interactive shell"
  120. printf '%s%s%s ' "$B" "$1" "$RST" >/dev/tty
  121. stty -echo </dev/tty 2>/dev/null || true
  122. IFS= read -r _sec </dev/tty || _sec=''
  123. stty echo </dev/tty 2>/dev/null || true
  124. printf '\n' >/dev/tty
  125. printf '%s' "$_sec"
  126. }
  127. # confirm PROMPT [Y] -> exit 0 on yes. Default is no unless second arg is "Y".
  128. confirm() {
  129. _def=${2:-N}
  130. if [ "$HAS_TTY" != 1 ]; then
  131. [ "$_def" = Y ] # non-interactive: take the default
  132. return
  133. fi
  134. if [ "$_def" = Y ]; then _hint='[Y/n]'; else _hint='[y/N]'; fi
  135. printf '%s%s%s %s ' "$B" "$1" "$RST" "$_hint" >/dev/tty
  136. IFS= read -r _r </dev/tty || _r=''
  137. [ -n "$_r" ] || _r=$_def
  138. case "$_r" in [yY]|[yY][eE][sS]) return 0 ;; *) return 1 ;; esac
  139. }
  140. printf '%s\n' "${B}DeepSeek Harness — dsh installer${RST}"
  141. if [ "$IN_REPO" = 1 ]; then
  142. printf '%ssource %s (in-repo reuse) @ %s%s\n' "$DIM" "$DSH_STAGING" "$DSH_REF" "$RST"
  143. else
  144. printf '%smaster %s @ %s%s\n' "$DIM" "$DSH_MASTER" "$DSH_REF" "$RST"
  145. printf '%sstaging %s%s\n' "$DIM" "$DSH_STAGING" "$RST"
  146. printf '%scurrent %s%s\n' "$DIM" "$DSH_CURRENT" "$RST"
  147. fi
  148. # --- 1. dependency check -------------------------------------------------------
  149. step "Checking dependencies"
  150. command -v git >/dev/null 2>&1 || die "git is required but not found. Install git, then re-run."
  151. info "git ... ok"
  152. # Node ^22.19.0 || >=24.0.0 (see the root package.json "engines" field).
  153. node_ok() {
  154. command -v node >/dev/null 2>&1 || return 1
  155. _v=$(node -v 2>/dev/null) || return 1
  156. _v=${_v#v}
  157. _major=${_v%%.*}
  158. _rest=${_v#*.}
  159. _minor=${_rest%%.*}
  160. case "$_major" in ''|*[!0-9]*) return 1 ;; esac
  161. case "$_minor" in ''|*[!0-9]*) _minor=0 ;; esac
  162. [ "$_major" -ge 24 ] && return 0
  163. [ "$_major" -eq 22 ] && [ "$_minor" -ge 19 ] && return 0
  164. return 1
  165. }
  166. if node_ok; then
  167. info "node $(node -v) ... ok"
  168. else
  169. if command -v node >/dev/null 2>&1; then
  170. die "Node $(node -v) is unsupported. dsh needs ^22.19.0 || >=24.0.0 — upgrade Node, then re-run."
  171. fi
  172. die "Node is required but not found. Install Node ^22.19.0 || >=24, then re-run."
  173. fi
  174. # pnpm is the only dependency we offer to install for you.
  175. if command -v pnpm >/dev/null 2>&1; then
  176. info "pnpm $(pnpm --version 2>/dev/null) ... ok"
  177. else
  178. warn "pnpm is not installed."
  179. if confirm "Install pnpm now?" Y; then
  180. if command -v corepack >/dev/null 2>&1 && corepack enable pnpm >/dev/null 2>&1; then
  181. info "enabled pnpm via corepack"
  182. elif command -v npm >/dev/null 2>&1 && npm install -g pnpm >/dev/null 2>&1; then
  183. info "installed pnpm via npm"
  184. else
  185. die "could not install pnpm automatically. Install it (https://pnpm.io/installation), then re-run."
  186. fi
  187. command -v pnpm >/dev/null 2>&1 || die "pnpm still not on PATH after install. Open a new shell, then re-run."
  188. else
  189. die "pnpm is required. Install it (https://pnpm.io/installation), then re-run."
  190. fi
  191. fi
  192. # --- 2. clone the master and lay out the staging worktree ---------------------
  193. # Fresh installs keep one real clone at $DSH_MASTER and check the running code
  194. # out as a git worktree at $DSH_STAGING, so every checkout lives under
  195. # $DSH_SOURCE and shares one object store. In-repo reuse links `dsh` at the
  196. # existing checkout untouched.
  197. if [ "$IN_REPO" = 1 ]; then
  198. step "Using existing checkout at $DSH_STAGING"
  199. info "running from inside the repo — skipping clone (DSH_REF ignored, working tree left untouched)"
  200. else
  201. step "Fetching source into $DSH_MASTER"
  202. if [ -d "$DSH_MASTER/.git" ]; then
  203. info "existing master clone found — updating"
  204. git -C "$DSH_MASTER" fetch origin "$DSH_REF"
  205. # Reset the master checkout to the freshly fetched tip. FETCH_HEAD (not
  206. # origin/<ref>) so this resolves for a tag as well as a branch, and -B makes
  207. # the re-run idempotent whether or not DSH_REF changed since the last install.
  208. git -C "$DSH_MASTER" checkout -q -B "$DSH_REF" FETCH_HEAD
  209. else
  210. mkdir -p "$DSH_SOURCE"
  211. git clone --branch "$DSH_REF" "$DSH_REPO" "$DSH_MASTER"
  212. fi
  213. step "Adding staging worktree at $DSH_STAGING"
  214. [ -e "$DSH_STAGING" ] && die "staging path $DSH_STAGING already exists — remove it or set DSH_SOURCE elsewhere, then re-run."
  215. # The staging worktree owns the branch dsh runs from; the master clone stays on
  216. # $DSH_REF as the fetch/upgrade base. Exclude the per-worktree merge lock in the
  217. # master clone's info/exclude, which every linked worktree inherits.
  218. git -C "$DSH_MASTER" worktree add -b "$DSH_STAGING_BRANCH" "$DSH_STAGING" FETCH_HEAD 2>/dev/null \
  219. || git -C "$DSH_MASTER" worktree add -b "$DSH_STAGING_BRANCH" "$DSH_STAGING" HEAD
  220. _exclude="$DSH_MASTER/.git/info/exclude"
  221. if [ -f "$_exclude" ] && ! grep -qxF '.agents/merge.lock' "$_exclude" 2>/dev/null; then
  222. printf '.agents/merge.lock\n' >>"$_exclude"
  223. fi
  224. mkdir -p "$DSH_STAGING/.agents"
  225. : >"$DSH_STAGING/.agents/merge.lock"
  226. fi
  227. # --- 3. install dependencies (no build; the launcher runs from source) --------
  228. step "Installing dependencies with pnpm (this can take a while)"
  229. ( cd "$DSH_STAGING" && pnpm install )
  230. [ -x "$DSH_STAGING/bin/dsh" ] || die "launcher $DSH_STAGING/bin/dsh missing after install — is DSH_REF a branch that ships apps/cli?"
  231. # --- 4. put `dsh` on PATH ------------------------------------------------------
  232. # Clone installs go through a stable `current` symlink so an upgrade repoints
  233. # one symlink (current -> new worktree) and the PATH launcher never moves:
  234. # PATH/dsh -> current/bin/dsh -> <staging>/bin/dsh. In-repo reuse links PATH
  235. # straight at the checkout, since that checkout is not a managed worktree.
  236. step "Linking dsh into $DSH_BIN_DIR"
  237. mkdir -p "$DSH_BIN_DIR"
  238. if [ "$IN_REPO" = 1 ]; then
  239. DSH_LAUNCH_TARGET=$DSH_STAGING/bin/dsh
  240. ln -sf "$DSH_LAUNCH_TARGET" "$DSH_BIN_DIR/dsh"
  241. info "linked $DSH_BIN_DIR/dsh -> $DSH_LAUNCH_TARGET"
  242. else
  243. # Point `current` at this staging worktree with `ln -sfn`: -f replaces an
  244. # existing `current` (re-run or upgrade) and -n stops `ln` from dereferencing
  245. # an existing symlink-to-directory and dropping the new link *inside* the old
  246. # worktree. `mv` is unusable here — BSD/macOS `mv` follows the existing dir
  247. # symlink the same way. The swap is one unlink+symlink pair on a local fs; the
  248. # installer holds no other process racing this path.
  249. ln -sfn "$DSH_STAGING" "$DSH_CURRENT"
  250. info "pointed $DSH_CURRENT -> $DSH_STAGING"
  251. DSH_LAUNCH_TARGET=$DSH_CURRENT/bin/dsh
  252. ln -sf "$DSH_LAUNCH_TARGET" "$DSH_BIN_DIR/dsh"
  253. info "linked $DSH_BIN_DIR/dsh -> $DSH_LAUNCH_TARGET"
  254. fi
  255. case ":$PATH:" in
  256. *":$DSH_BIN_DIR:"*) ON_PATH=1 ;;
  257. *) ON_PATH=0 ;;
  258. esac
  259. if [ "$ON_PATH" = 0 ]; then
  260. warn "$DSH_BIN_DIR is not on your PATH."
  261. _line="export PATH=\"$DSH_BIN_DIR:\$PATH\""
  262. _rc=''
  263. _sh=${SHELL:-} # SHELL may be unset; word-removal on an unset var trips set -u under dash.
  264. case "${_sh##*/}" in
  265. zsh) _rc="$HOME/.zshrc" ;;
  266. bash) _rc="$HOME/.bashrc" ;;
  267. esac
  268. if [ -n "$_rc" ] && [ -f "$_rc" ] && grep -qF "$_line" "$_rc" 2>/dev/null; then
  269. info "$_rc already exports $DSH_BIN_DIR — open a new shell to pick it up"
  270. elif [ -n "$_rc" ] && confirm "Add it to $_rc?" Y; then
  271. printf '\n# Added by the dsh installer\n%s\n' "$_line" >>"$_rc"
  272. info "updated $_rc — run 'source $_rc' or open a new shell to pick it up"
  273. else
  274. warn "add this line to your shell profile yourself:"
  275. printf ' %s\n' "$_line"
  276. fi
  277. fi
  278. # --- 5. credentials ------------------------------------------------------------
  279. # Mirror app-boot's resolveDshHome precedence ($DSH_HOME, else ~/.dsh) so creds land where dsh reads them.
  280. if [ -n "${DSH_HOME:-}" ]; then
  281. CONF="$DSH_HOME"
  282. else
  283. CONF="$HOME/.dsh"
  284. fi
  285. ENV_FILE="$CONF/.env"
  286. step "Configuring credentials"
  287. if [ -f "$ENV_FILE" ] && grep -q '^DEEPSEEK_API_KEY=' "$ENV_FILE" 2>/dev/null; then
  288. info "DEEPSEEK_API_KEY already set in $ENV_FILE"
  289. if ! confirm "Replace it?" N; then
  290. SKIP_CREDS=1
  291. fi
  292. fi
  293. if [ "${SKIP_CREDS:-0}" != 1 ]; then
  294. if [ "$HAS_TTY" = 1 ]; then
  295. API_KEY=$(ask_secret "DeepSeek API key (input hidden):")
  296. if [ -z "$API_KEY" ]; then
  297. warn "no key entered — skipping. Set DEEPSEEK_API_KEY in $ENV_FILE before using dsh."
  298. else
  299. BASE_URL=$(ask "DeepSeek base URL (optional, Enter to skip):")
  300. mkdir -p "$CONF"
  301. # The installer owns exactly the two DEEPSEEK_* lines; any other lines the
  302. # user keeps in this .env are preserved. The rewrite happens in a subshell
  303. # so umask 077 (which closes the create-time permission race) does not leak
  304. # into the exec'd dsh, and lands atomically via a same-dir temp + mv.
  305. _tmp="$ENV_FILE.dsh.$$"
  306. (
  307. umask 077
  308. if [ -f "$ENV_FILE" ]; then
  309. grep -v -e '^DEEPSEEK_API_KEY=' -e '^DEEPSEEK_BASE_URL=' "$ENV_FILE" >"$_tmp" || true
  310. else
  311. : >"$_tmp"
  312. fi
  313. printf 'DEEPSEEK_API_KEY=%s\n' "$API_KEY" >>"$_tmp"
  314. if [ -n "$BASE_URL" ]; then printf 'DEEPSEEK_BASE_URL=%s\n' "$BASE_URL" >>"$_tmp"; fi
  315. )
  316. mv "$_tmp" "$ENV_FILE"
  317. chmod 600 "$ENV_FILE" 2>/dev/null || true
  318. info "wrote $ENV_FILE"
  319. fi
  320. else
  321. warn "no terminal for credential input — set DEEPSEEK_API_KEY in $ENV_FILE before using dsh."
  322. fi
  323. fi
  324. # --- 6. launch -----------------------------------------------------------------
  325. step "Done"
  326. if [ "$HAS_TTY" = 1 ]; then
  327. info "launching dsh — run 'dsh' anytime to start again"
  328. exec "$DSH_BIN_DIR/dsh" </dev/tty
  329. else
  330. info "install complete. Start it with:"
  331. printf ' %s\n' "$DSH_BIN_DIR/dsh"
  332. fi