From 1bc539f7001584a99d77eceb4dc8a664e2f64329 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:44:02 +0530 Subject: [PATCH 1/2] feat(kin): one command sets up the worker environment Co-Authored-By: Claude Mythos 5.2 --- ops/kin/host/README.md | 42 ++++-- ops/kin/host/kin-worker.sh | 259 ++++++++++++++++++++++++++++++++++++- 2 files changed, 281 insertions(+), 20 deletions(-) diff --git a/ops/kin/host/README.md b/ops/kin/host/README.md index 1f4f896cb60b..3e68a04414f2 100644 --- a/ops/kin/host/README.md +++ b/ops/kin/host/README.md @@ -168,19 +168,35 @@ What a worker does get: its own checkouts and worktrees in `/home/kin-worker`, i ### Set up workers -Steps marked **root** need sudo. **account** marks one the owner creates outside the host. Run the installer only from `/root/kin-ops` (see [Install](#install)); it refuses a clone that isn't root-owned. After the first run, `/usr/local/sbin/kin-worker` (root-owned) does the rest. Root never writes, chowns or chmods anything under `/home/kin-worker`: the installer does that as kin-worker, so a symlink a worker plants there reaches only what kin-worker could change anyway. - -1. **root:** `sudo git -C /root/kin-ops pull --ff-only` (the clone from step 0 of [Install](#install)). -2. **root:** `sudo /root/kin-ops/ops/kin/host/kin-worker.sh`. It creates `kin-worker` (home mode 700, not in group `kin`), sets `/home/kin` to mode 700, installs `/usr/local/sbin/kin-worker`, the units `kin-worker-server.service` and `kin-worker-link.{service,timer}`, and `/etc/sudoers.d/kin-worker`, which lets `kin` run exactly `systemctl stop`, `start` and `try-restart` on the worker unit and `kin-worker upload-pack `. As kin-worker, it turns on Linked environments in the worker environment's settings and writes `~kin-worker/.claude/settings.json` with only the gateway URL and an `apiKeyHelper` that reads `~kin-worker/.config/kin-worker/gateway-key`. Then it starts the worker server on `127.0.0.1:3874` if Kin has a build and isn't paused. The gateway URL is `KIN_WORKER_GATEWAY_URL` when set (`sudo KIN_WORKER_GATEWAY_URL=https://... /root/kin-ops/...`), else the `ANTHROPIC_BASE_URL` in kin's Claude settings; nothing else is read from kin's file. A changed unit is reloaded and the server restarted. It prints a checklist; the first run fails it until the steps below are done. `--dry-run` changes nothing. -3. **root:** tools in kin-worker's own home, like kin's (step 2 of [Install](#install)): `sudo -iu kin-worker`, then `curl https://mise.run | sh`, `mise use -g node@`, `npm i -g @anthropic-ai/claude-code @openai/codex`. `git`, `gh`, `curl`, `python3`, `sqlite3`, iproute2 (`ss`) and util-linux (`nsenter`, `setpriv`) come from the distribution, system-wide. The worker unit's start check and `check`'s probes run only those root-owned copies, never anything on the worker's `PATH`. -4. **account, then root: Claude.** Create a gateway key for kin-worker alone, never kin's or yours. Write it as kin-worker: `sudo kin-worker run -- sh -c 'umask 077 && mkdir -p ~/.config/kin-worker && cat > ~/.config/kin-worker/gateway-key' < `, shred the file, then `sudo kin-worker run -- claude -p ok` should answer. -5. **account, then root: Codex.** Use a separate OpenAI account or an API key with a spending cap, never the owner's login or token. `sudo kin-worker run -- codex login` and sign in to that account, or `sudo kin-worker run -- codex login --with-api-key < `. -6. **GitHub: Kin publishes, workers only read.** Before you list any repo in `KIN_GITHUB_REPOS`, that repo's default branch must carry the CI guard: every workflow job that reads a secret skips `kin/worker/*` heads (`if: ${{ !startsWith(github.head_ref, 'kin/worker/') }}`, or `github.event.workflow_run.head_branch` for `workflow_run` jobs; see this repo's `.github/workflows`). A worker's PR runs that repo's workflows on the worker's code, so without the guard a worker gets the repo's secrets. The Kin GitHub App must **not** have the **Workflows** permission: Kin's refusal of `.github` changes is backed by GitHub refusing the push. Check it on GitHub under Settings → Developer settings → GitHub Apps → the Kin App → Permissions & events → Repository permissions, where Workflows must read "No access", and on each installation's Configure page, which must not list workflows. On kin-server, set `KIN_GITHUB_APP_ID` (the Kin GitHub App, approvals spec section 10), install the App on every worker repo, and list those repos in `KIN_GITHUB_REPOS` in `~kin/.t3/service.env`: Kin mints its `server` push token only for those. Public repos need nothing on the worker side; Kin's server publishes for workers. For a private repo, **account, then root:** a fine-grained token limited to the worker repos with **Contents: read** and nothing else, never a token with write, never the owner's, kin's or the App's. Then `sudo kin-worker run -- gh auth login --with-token < ` (shred the file after) and `sudo kin-worker run -- gh auth setup-git`. No bot account needs write access to anything. **Hosts set up under the earlier steps,** where workers pushed as a bot account: delete that bot's fine-grained token (the bot's Settings → Developer settings → Personal access tokens → Fine-grained tokens) and remove the bot's Write access on the worker repos, then `sudo kin-worker run -- gh auth logout` and, for private repos, log in again with the read-only token above. `sudo kin-worker check` fails while kin-worker still holds a token that can push. The App's key lives in kin's keyring: set that up first ([GitHub App keyring](#github-app-keyring)). -7. **Disk. root:** put `/home/kin-worker` on its own filesystem, or give `kin-worker` a disk quota, so a worker filling its home can't fill the disk kin-server, its database and its backups live on. -8. **Projects. root:** for each of the lead's worker projects, clone the same repository into the worker environment and register it there: `sudo kin-worker run -- git clone https://github.com// /home/kin-worker/projects/`, then `sudo kin-worker run t3 project add /home/kin-worker/projects/ --base-dir /home/kin-worker/.t3`. Kin matches a lead's worker project to the worker environment's project with the same repository, so `kin lead create --worker-project` keeps naming this host's project. -9. **Linked environments on kin-server:** Settings → Labs → Linked environments, from any client. -10. **root:** `sudo kin-worker link`. Links kin-server to `http://127.0.0.1:3874` with full access and writes `KIN_WORKER_ENVIRONMENT_ID` into `~kin/.t3/service.env`. Restart kin-server at a quiet moment, as it says. A link lasts 30 days; `kin-worker-link.timer` renews it weekly. -11. **root:** `sudo git -C /root/kin-ops pull --ff-only && sudo /root/kin-ops/ops/kin/host/kin-worker.sh` again, then `sudo kin-worker check`. Every line must be ✓ before a lead goes to L2. +One command sets workers up. It does every step that needs no credential, asks on the terminal for the two that do, and ends with the isolation check: + +```bash +sudo git -C /root/kin-ops pull --ff-only +sudo /root/kin-ops/ops/kin/host/kin-worker.sh setup +``` + +Run it on a terminal (`ssh -t`), after Kin has a build ([Install](#install), step 3), and only from `/root/kin-ops`: it refuses a clone that isn't root-owned. Have these ready first; they are yours to create outside the host, and nothing else is asked for: + +- **A Claude gateway key for kin-worker alone**, never kin's or yours. Setup takes it without echo and kin-worker writes it to `~kin-worker/.config/kin-worker/gateway-key` (mode 600). It is never printed, logged or put on a command line, and setup refuses kin's own key. +- **Optional: a Codex API key** on a separate OpenAI account with a spending cap, never the owner's login or token. Press Enter to skip it and workers use Claude only. To sign in to a ChatGPT account instead, skip it and run `sudo kin-worker run -- codex login` afterwards. + +Running it again is safe: each step keeps what is already there, so use it to repair a host or to add a repository (`setup / ...`). `setup --dry-run` changes nothing. It never restarts kin-server. What it does, in order: + +1. **Users, units and sandbox.** It creates `kin-worker` (home mode 700, not in group `kin`), sets `/home/kin` to mode 700, installs `/usr/local/sbin/kin-worker`, the units `kin-worker-server.service` and `kin-worker-link.{service,timer}`, and `/etc/sudoers.d/kin-worker`, which lets `kin` run exactly `systemctl stop`, `start` and `try-restart` on the worker unit and `kin-worker upload-pack `. As kin-worker, it turns on Linked environments in the worker environment's settings and writes `~kin-worker/.claude/settings.json` with only the gateway URL and an `apiKeyHelper` that reads `~kin-worker/.config/kin-worker/gateway-key`. Then it starts the worker server on `127.0.0.1:3874` if Kin has a build and isn't paused. The gateway URL is `KIN_WORKER_GATEWAY_URL` when set (`sudo KIN_WORKER_GATEWAY_URL=https://... /root/kin-ops/... setup`), else the `ANTHROPIC_BASE_URL` in kin's Claude settings; nothing else is read from kin's file. A changed unit is reloaded and the server restarted. +2. **Tools.** As kin-worker, inside the sandbox, it installs mise, the build's Node, Claude Code and Codex into kin-worker's own home. `git`, `gh`, `curl`, `python3`, `sqlite3`, iproute2 (`ss`) and util-linux (`nsenter`, `setpriv`) come from the distribution, system-wide. The worker unit's start check and `check`'s probes run only those root-owned copies, never anything on the worker's `PATH`. +3. **The two credentials**, as above, then `claude -p ok` as kin-worker to show the key works. +4. **Linked environments on kin-server.** kin sets that one key in its own `settings.json`, which the server watches. If the file can't be parsed it is left alone and setup says to turn it on from a client: Settings → Labs → Linked environments. +5. **Projects.** For every repository Kin publishes to (`KIN_GITHUB_REPOS` and Kin's own) and any you name, kin-worker clones `https://github.com//` into `/home/kin-worker/projects/` and registers it in the worker environment. Memory repositories are skipped. A lead starts work in a repository by name, and Kin matches the name to the checkout here by its GitHub remote. A private repository that kin-worker can't read yet is listed with ✗; give it the read-only token below and run setup again. +6. **The link.** `kin-worker link` links kin-server to `http://127.0.0.1:3874` with full access and writes `KIN_WORKER_ENVIRONMENT_ID` into `~kin/.t3/service.env`. A link lasts 30 days; `kin-worker-link.timer` renews it weekly. +7. **`kin-worker check`.** Every line must be ✓ before leads start workers. + +Three things stay with you. Do the GitHub one before setup, so its repositories are there to clone and `check` can pass: + +- **Restart kin-server once, at a quiet moment,** so it reads the new `KIN_WORKER_ENVIRONMENT_ID`. Setup prints the command. Until then leads tell the owner the worker environment isn't set up. +- **GitHub: Kin publishes, workers only read.** Before you list any repo in `KIN_GITHUB_REPOS`, that repo's default branch must carry the CI guard: every workflow job that reads a secret skips `kin/worker/*` heads (`if: ${{ !startsWith(github.head_ref, 'kin/worker/') }}`, or `github.event.workflow_run.head_branch` for `workflow_run` jobs; see this repo's `.github/workflows`). A worker's PR runs that repo's workflows on the worker's code, so without the guard a worker gets the repo's secrets. The Kin GitHub App must **not** have the **Workflows** permission: Kin's refusal of `.github` changes is backed by GitHub refusing the push. Check it on GitHub under Settings → Developer settings → GitHub Apps → the Kin App → Permissions & events → Repository permissions, where Workflows must read "No access", and on each installation's Configure page, which must not list workflows. On kin-server, set `KIN_GITHUB_APP_ID` (the Kin GitHub App, approvals spec section 10), install the App on every worker repo, and list those repos in `KIN_GITHUB_REPOS` in `~kin/.t3/service.env`: Kin mints its `server` push token only for those. Public repos need nothing on the worker side; Kin's server publishes for workers. For a private repo, **account, then root:** a fine-grained token limited to the worker repos with **Contents: read** and nothing else, never a token with write, never the owner's, kin's or the App's. Then `sudo kin-worker run -- gh auth login --with-token < ` (shred the file after) and `sudo kin-worker run -- gh auth setup-git`. No bot account needs write access to anything. **Hosts set up under the earlier steps,** where workers pushed as a bot account: delete that bot's fine-grained token (the bot's Settings → Developer settings → Personal access tokens → Fine-grained tokens) and remove the bot's Write access on the worker repos, then `sudo kin-worker run -- gh auth logout` and, for private repos, log in again with the read-only token above. `sudo kin-worker check` fails while kin-worker still holds a token that can push. The App's key lives in kin's keyring: set that up first ([GitHub App keyring](#github-app-keyring)). +- **Disk.** Put `/home/kin-worker` on its own filesystem, or give `kin-worker` a disk quota, so a worker filling its home can't fill the disk kin-server, its database and its backups live on. Setup says so when the two homes share a filesystem. + +Root never writes, chowns or chmods anything under `/home/kin-worker`: setup does that as kin-worker, so a symlink a worker plants there reaches only what kin-worker could change anyway. The steps also run one at a time: `kin-worker.sh install` (step 1 alone, then a checklist of what is missing), `sudo kin-worker run -- ` for anything as kin-worker in the sandbox (`sudo kin-worker run t3 project add --base-dir /home/kin-worker/.t3`), `sudo kin-worker link` and `sudo kin-worker check`. ### GitHub App keyring diff --git a/ops/kin/host/kin-worker.sh b/ops/kin/host/kin-worker.sh index 42cbb936da7e..c57a1574f9db 100755 --- a/ops/kin/host/kin-worker.sh +++ b/ops/kin/host/kin-worker.sh @@ -3,6 +3,9 @@ # Workers' shells run there, in a sandbox that has no kin home, no owner home and no /tmp of # kin's, so they can't read Kin's database, secrets, memory key or service.env, or run Kin's CLI. # +# sudo /ops/kin/host/kin-worker.sh setup [owner/repo ...] everything, in +# one go: asks for the worker's two credentials on +# the terminal, and ends with `check` # sudo /ops/kin/host/kin-worker.sh [install] [--dry-run] set up or repair # sudo kin-worker link (re)link kin-server; the timer runs it weekly # sudo kin-worker check the isolation checks, as a transcript @@ -13,7 +16,7 @@ # kin-worker upload-pack the worker's repository, read-only, for Kin to publish # kin-worker may-start the unit's ExecCondition: no org of Kin's is paused # -# `install` copies this script to /usr/local/sbin/kin-worker and never restarts kin-server. +# `install` copies this script to /usr/local/sbin/kin-worker. Nothing here restarts kin-server. # Root never writes, chowns or chmods through a path under the worker's home: that work runs as # kin-worker (`worker_do`), so a symlink there reaches only what kin-worker could already change. set -euo pipefail @@ -261,8 +264,7 @@ cmd_upload_pack() { -- /usr/bin/git upload-pack --timeout=60 "$path" | head -c "$UPLOAD_PACK_MAX_BYTES" } -cmd_install() { - if [ "${1:-}" = --dry-run ]; then DRY_RUN=1; fi +install_steps() { if [ "$DRY_RUN" -eq 0 ]; then [ "$(uname -s)" = Linux ] || die "needs Linux with systemd." need_root install @@ -332,6 +334,11 @@ cmd_install() { else say "No Kin build at $KIN_HOME/kin-current yet: run update.sh as $KIN_USER, then this again." fi +} + +cmd_install() { + if [ "${1:-}" = --dry-run ]; then DRY_RUN=1; fi + install_steps if [ "$DRY_RUN" -eq 1 ]; then say "" say "Dry run: nothing was changed." @@ -422,6 +429,242 @@ cmd_link() { say " sudo -u $KIN_USER XDG_RUNTIME_DIR=/run/user/$(id -u "$KIN_USER") systemctl --user restart kin-server" } +# --- setup ----------------------------------------------------------------------------------- + +# One secret from the terminal into SECRET. Echo goes off before the prompt shows, so a paste +# that lands early isn't shown either. It stays in this shell: never printed, logged or put on a +# command line. No terminal reads as skipped. +SECRET="" +TTY_SAVED="" +ask_secret() { + SECRET="" + if ! { : >/dev/tty; } 2>/dev/null; then + say "No terminal to ask on (use ssh -t), so this was skipped." + return 0 + fi + TTY_SAVED="$(stty -g /dev/tty + IFS= read -r SECRET /dev/tty +} + +# mise, the build's Node, Claude Code and Codex in kin-worker's own home, installed by kin-worker +# inside the sandbox, so nothing of root's, kin's or the owner's is within the installers' reach. +setup_tools() { + local version tool + version="$(node_version_of "$KIN_HOME/kin-current")" + [ -n "$version" ] || die "no engines.node in $KIN_HOME/kin-current/package.json." + if ! in_sandbox mise --version >/dev/null 2>&1; then + say "Installing mise for $WORKER_USER." + in_sandbox /bin/sh -c '/usr/bin/curl -fsSL https://mise.run | /bin/sh' >/dev/null || + die "couldn't install mise as $WORKER_USER. Is https://mise.run reachable from the sandbox?" + fi + in_sandbox mise use --global "node@$version" >/dev/null || + die "couldn't install Node $version as $WORKER_USER." + for tool in claude:@anthropic-ai/claude-code codex:@openai/codex; do + if in_sandbox "${tool%%:*}" --version >/dev/null 2>&1; then continue; fi + say "Installing ${tool#*:} for $WORKER_USER." + in_sandbox mise exec "node@$version" -- npm install --global --silent "${tool#*:}" || + die "couldn't install ${tool#*:} as $WORKER_USER." + done + in_sandbox mise reshim + say "Tools are in $WORKER_HOME: mise, Node $version, Claude Code, Codex." +} + +# The first credential: kin-worker's own Claude gateway key, written by kin-worker to a file only +# it can read. A key already there stays. One that is kin's own is taken back out. +setup_gateway_key() { + local wrote=0 + if worker_do /usr/bin/test -s "$GATEWAY_KEY"; then + say "$WORKER_USER already has a gateway key; keeping it. To replace it: sudo kin-worker run -- rm $GATEWAY_KEY, then setup again." + else + ask_secret "Claude gateway key for $WORKER_USER alone, never kin's or yours (Enter to skip): " + if [ -z "$SECRET" ]; then + say "No gateway key yet: workers can't use Claude until setup is run again with one." + return 0 + fi + if ! [[ "$SECRET" =~ ^[[:graph:]]+$ ]]; then + SECRET="" + die "that key has spaces or unprintable characters; nothing was written." + fi + # An empty file from an attempt that failed would block the new one. + worker_do /bin/rm -f "$GATEWAY_KEY" + printf '%s\n' "$SECRET" | worker_write_new "$GATEWAY_KEY" || { + SECRET="" + die "couldn't write the key as $WORKER_USER." + } + SECRET="" + wrote=1 + fi + if ! own_gateway_key; then + if [ "$wrote" -eq 1 ]; then worker_do /bin/rm -f "$GATEWAY_KEY"; fi + die "$WORKER_USER's gateway key is kin's own, which a worker must never hold. Create one for $WORKER_USER alone (and remove an old one: sudo kin-worker run -- rm $GATEWAY_KEY), then setup again." + fi + if [ "$wrote" -eq 1 ]; then say "Wrote the key to $GATEWAY_KEY (mode 600, $WORKER_USER's)."; fi + if in_sandbox /usr/bin/timeout 90 claude -p ok >/dev/null 2>&1; then + say "Claude answers as $WORKER_USER." + else + say "Claude didn't answer as $WORKER_USER yet. Try: sudo kin-worker run -- claude -p ok" + fi +} + +# The second credential, optional: Codex, on an account that is not the owner's. +setup_codex() { + if in_sandbox codex login status >/dev/null 2>&1; then + say "Codex is signed in for $WORKER_USER." + return 0 + fi + ask_secret "Codex API key for a separate account with a spending cap, never the owner's (Enter to skip Codex): " + if [ -z "$SECRET" ]; then + say "Codex skipped: workers use Claude only. To sign in later: sudo kin-worker run -- codex login" + return 0 + fi + if printf '%s\n' "$SECRET" | /usr/local/sbin/kin-worker run -- codex login --with-api-key >/dev/null 2>&1; then + say "Codex is signed in for $WORKER_USER." + else + say "Codex didn't accept that key. To try again: sudo kin-worker run -- codex login --with-api-key < " + fi + SECRET="" +} + +# Linked environments on kin-server, which watches its settings file: kin sets the one key in +# its own file and every other setting stays as it was. A file it can't parse is left alone. +enable_kin_links() { + as_user "$KIN_USER" /usr/bin/python3 -I -c 'import json, os, sys +path = os.path.realpath(sys.argv[1]) +try: + with open(path) as handle: + data = json.load(handle) +except FileNotFoundError: + data = {} +if not isinstance(data, dict): + sys.exit(1) +if data.get("linkedEnvironmentsEnabled") is True: + sys.exit(0) +data["linkedEnvironmentsEnabled"] = True +os.makedirs(os.path.dirname(path), mode=0o700, exist_ok=True) +scratch = path + ".kin-worker-setup" +with os.fdopen(os.open(scratch, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w") as handle: + json.dump(data, handle, indent=2) + handle.write("\n") +os.replace(scratch, path)' "$KIN_HOME/.t3/userdata/settings.json" +} + +# Kin's memory repositories, as owner/repo. Workers get no checkout of those. +memory_repos() { + cat "$KIN_HOME/.t3/service.env" "$KIN_HOME/.config/kin/kin-server.env" 2>/dev/null | + sed -n 's/^KIN_MEMORY_REPOSITORIES=//p' | + grep -oE 'github\.com[:/][A-Za-z0-9-]+/[A-Za-z0-9._-]+' | + sed -e 's#^github\.com[:/]##' -e 's/\.git$//' | sort -u +} +lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } +checkout_origin() { + in_sandbox /usr/bin/git -C "$1" remote get-url origin 2>/dev/null | + sed -n 's#^.*github\.com[:/]\([^/]*/[^/]*\)$#\1#p' | sed 's/\.git$//' +} +# One repository: a checkout of kin-worker's, cloned by kin-worker in the sandbox with whatever +# read access it has, and registered in the worker environment. Run again, it changes nothing. +setup_project() { + local slug="$1" dir="$WORKER_HOME/projects/${1#*/}" origin added + origin="$(checkout_origin "$dir" || true)" + if [ -n "$origin" ] && [ "$(lower "$origin")" != "$(lower "$slug")" ]; then + # Another owner's repository of the same name already has that directory. + dir="$WORKER_HOME/projects/${slug%%/*}-${slug#*/}" + origin="$(checkout_origin "$dir" || true)" + fi + if [ -z "$origin" ]; then + if in_sandbox /usr/bin/test -e "$dir"; then + say " ✗ $slug: $dir is there and isn't a checkout of it." + return 1 + fi + if ! in_sandbox /usr/bin/env GIT_TERMINAL_PROMPT=0 /usr/bin/git clone --quiet "https://github.com/$slug" "$dir"; then + say " ✗ $slug: couldn't clone it. A private repository needs kin-worker's read-only token first (README, \"Set up workers\")." + return 1 + fi + elif [ "$(lower "$origin")" != "$(lower "$slug")" ]; then + say " ✗ $slug: $dir is a checkout of $origin." + return 1 + fi + added="$(in_sandbox t3 project add "$dir" --base-dir "$WORKER_HOME/.t3" 2>&1 || true)" + case "$added" in + *"Added project"*) say " ✓ $slug at $dir" ;; + *"already exists"*) say " ✓ $slug at $dir, already registered" ;; + *) + say " ✗ $slug: it is at $dir, but registering it failed: $(printf '%s\n' "$added" | tail -n1)" + return 1 + ;; + esac +} +# Every repository Kin publishes to (KIN_GITHUB_REPOS and Kin's own), plus any named on the +# command line: the ones a lead's work can end in a pull request on. +setup_projects() { + local slug skip repos missing=0 + skip="$(memory_repos || true)" + repos="$({ + kin_repos || true + printf '%s\n' "$@" + } | sed 's/[[:space:]]//g' | grep -E '^[A-Za-z0-9-]+/[A-Za-z0-9._-]+$' | sort -u || true)" + if [ -z "$repos" ]; then + say "No repositories to clone: KIN_GITHUB_REPOS is empty. Name them: setup / ..." + return 0 + fi + say "Projects in the worker environment:" + for slug in $repos; do + if grep -qixF "$slug" <<<"$skip"; then continue; fi + setup_project "$slug" || missing=1 + done + if [ "$missing" -eq 1 ]; then say "Leads can't start work in the ✗ ones until they are fixed; run setup again then."; fi +} + +# `setup [owner/repo ...]`: every step from a fresh host to a passing `check` that needs no +# credential, and a prompt for the two that do. Safe to run again: each step keeps what is there. +cmd_setup() { + local repos=() arg before + for arg in "$@"; do + case "$arg" in + --dry-run) DRY_RUN=1 ;; + -*) die "setup takes repositories as owner/repo, and --dry-run." ;; + *) repos+=("$arg") ;; + esac + done + if [ "$DRY_RUN" -eq 1 ]; then + install_steps + say "+ as $WORKER_USER, in the sandbox: install mise, the build's Node, Claude Code and Codex" + say "+ ask for $WORKER_USER's Claude gateway key, and optionally a Codex API key" + say "+ turn on Linked environments in kin's settings" + say "+ clone and register every repository in KIN_GITHUB_REPOS and Kin's own ${repos[*]:-}" + say "+ kin-worker link, then kin-worker check" + say "" + say "Dry run: nothing was changed." + return 0 + fi + need_root setup + [ -f "$KIN_HOME/kin-current/apps/server/dist/bin.mjs" ] || + die "no Kin build at $KIN_HOME/kin-current yet: run update.sh as $KIN_USER first (README, \"Install\")." + install_steps + setup_tools + setup_gateway_key + setup_codex + if enable_kin_links; then say "Linked environments are on for kin-server."; else + say "Couldn't turn on Linked environments in kin's settings. From any client: Settings → Labs → Linked environments." + fi + setup_projects ${repos[@]+"${repos[@]}"} + if [ "$(stat -c %d "$WORKER_HOME")" = "$(stat -c %d "$KIN_HOME")" ]; then + say "Note: $WORKER_HOME shares a filesystem with $KIN_HOME. Give $WORKER_USER a disk quota or its own filesystem, so a worker can't fill Kin's disk." + fi + systemctl is-active --quiet kin-worker-server.service || systemctl start kin-worker-server.service || true + before="$(service_env_value KIN_WORKER_ENVIRONMENT_ID)" + cmd_link + if [ "$(service_env_value KIN_WORKER_ENVIRONMENT_ID)" != "$before" ]; then + say "Leads can start workers once kin-server restarts with that id. Nothing here restarts it." + fi + checklist +} + # --- check ----------------------------------------------------------------------------------- live_pid() { [ -n "$1" ] && [ "$1" != 0 ] && kill -0 "$1"; } @@ -528,12 +771,13 @@ print(json.load(open(sys.argv[1])).get("apiKeyHelper", ""))' "$settings" 2>/dev/ env = json.load(open(sys.argv[1])).get("env", {}) print(env.get("ANTHROPIC_API_KEY") or env.get("ANTHROPIC_AUTH_TOKEN") or "")' "$settings" 2>/dev/null || true)" fi - if [ -n "$key" ]; then printf '%s' "$key" | sha; fi + if [ -n "$key" ]; then printf '%s' "$key" | tr -d '\r\n' | sha; fi } # The worker's key is the file its settings' helper reads (settings_hold_no_token checks that). +# Line ends are dropped on both sides: kin's key with a newline after it is still kin's key. own_gateway_key() { local mine theirs - mine="$(worker_do /bin/cat "$GATEWAY_KEY" 2>/dev/null | sha)" + mine="$(worker_do /bin/cat "$GATEWAY_KEY" 2>/dev/null | tr -d '\r\n' | sha)" theirs="$(kin_key_hash)" [ "$mine" != "$(printf '' | sha)" ] && [ "$mine" != "$theirs" ] } @@ -738,6 +982,7 @@ cmd_check() { case "${1:-install}" in install) shift || true; cmd_install "$@" ;; + setup) shift; cmd_setup "$@" ;; --dry-run) cmd_install --dry-run ;; link) cmd_link ;; check) cmd_check ;; @@ -745,6 +990,6 @@ case "${1:-install}" in node) shift; cmd_node "$@" ;; upload-pack) shift; cmd_upload_pack "$@" ;; may-start) cmd_may_start ;; - -h | --help) sed -n '2,16p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' ;; - *) die "unknown command: $1 (install, link, check, run)" ;; + -h | --help) sed -n '2,19p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' ;; + *) die "unknown command: $1 (setup, install, link, check, run)" ;; esac From dbfd13d2400f5d6f77eac7e28d99afcf2d70a85e Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Tue, 10 Nov 2026 21:22:31 +0530 Subject: [PATCH 2/2] fix(kin): worker setup checks keys before writing and fails loudly - Compare the typed gateway key with kin's (line ends dropped) before any byte reaches kin-worker's file; stop when kin's key can't be read. The post-write check stays as a second wall, and `check` now fails closed too. - `kin-worker run` enters through /usr/bin/env, so mise, claude and codex resolve on the worker's PATH inside the sandbox, not root's. - setup refuses to start without a terminal; Ctrl-C at a prompt restores echo and exits 130; worker files are written whole or not at all. - Registration keeps its exit status and recognises "already exists" explicitly; a missing repository makes setup exit non-zero after the isolation transcript. - `link` updates service.env as kin: scratch file beside it, rename, 0600. - The worker server starts only once its tools exist (reset-failed first), link waits up to a minute for HTTP, mise's installer is downloaded whole before it runs, installs have time limits, a missing service.env reads as empty, and Bash 4.4, GNU stat/sed and systemd 249 are checked first. Co-Authored-By: Claude Opus 5.5 --- ops/kin/host/README.md | 16 +-- ops/kin/host/kin-worker.sh | 281 ++++++++++++++++++++++++++++--------- scripts/kin-worker.test.ts | 52 +++++++ 3 files changed, 273 insertions(+), 76 deletions(-) create mode 100644 scripts/kin-worker.test.ts diff --git a/ops/kin/host/README.md b/ops/kin/host/README.md index 3e68a04414f2..39b6838b02b5 100644 --- a/ops/kin/host/README.md +++ b/ops/kin/host/README.md @@ -168,26 +168,26 @@ What a worker does get: its own checkouts and worktrees in `/home/kin-worker`, i ### Set up workers -One command sets workers up. It does every step that needs no credential, asks on the terminal for the two that do, and ends with the isolation check: +One command sets workers up. It does every step that needs no credential, asks on the terminal for the two that do, and ends with the isolation check. A private repository's read-only GitHub token for kin-worker is a separate step you do first (GitHub, under "Three things stay with you" below): ```bash sudo git -C /root/kin-ops pull --ff-only sudo /root/kin-ops/ops/kin/host/kin-worker.sh setup ``` -Run it on a terminal (`ssh -t`), after Kin has a build ([Install](#install), step 3), and only from `/root/kin-ops`: it refuses a clone that isn't root-owned. Have these ready first; they are yours to create outside the host, and nothing else is asked for: +Run it on a terminal (`ssh -t`), with nothing piped in: it refuses to start without one. Run it after Kin has a build ([Install](#install), step 3), and only from `/root/kin-ops`: it refuses a clone that isn't root-owned. Have these ready first; they are yours to create outside the host, and nothing else is asked for: - **A Claude gateway key for kin-worker alone**, never kin's or yours. Setup takes it without echo and kin-worker writes it to `~kin-worker/.config/kin-worker/gateway-key` (mode 600). It is never printed, logged or put on a command line, and setup refuses kin's own key. - **Optional: a Codex API key** on a separate OpenAI account with a spending cap, never the owner's login or token. Press Enter to skip it and workers use Claude only. To sign in to a ChatGPT account instead, skip it and run `sudo kin-worker run -- codex login` afterwards. -Running it again is safe: each step keeps what is already there, so use it to repair a host or to add a repository (`setup / ...`). `setup --dry-run` changes nothing. It never restarts kin-server. What it does, in order: +Before it changes anything it checks for Bash 4.4, GNU `stat` and `sed`, systemd 249 or later, a terminal, the `kin` user and Kin's build. Running it again keeps what is already there (users, units, settings, tools, the gateway key, checkouts and their registration), so use it to repair a host or to add a repository (`setup / ...`). Each run does pair and link kin-server again, as the weekly timer does, and asks for a Codex key again if Codex isn't signed in. It exits non-zero when a check fails or a repository couldn't be cloned or registered; the isolation check still runs in that case. `setup --dry-run` changes nothing. It never restarts kin-server. What it does, in order: -1. **Users, units and sandbox.** It creates `kin-worker` (home mode 700, not in group `kin`), sets `/home/kin` to mode 700, installs `/usr/local/sbin/kin-worker`, the units `kin-worker-server.service` and `kin-worker-link.{service,timer}`, and `/etc/sudoers.d/kin-worker`, which lets `kin` run exactly `systemctl stop`, `start` and `try-restart` on the worker unit and `kin-worker upload-pack `. As kin-worker, it turns on Linked environments in the worker environment's settings and writes `~kin-worker/.claude/settings.json` with only the gateway URL and an `apiKeyHelper` that reads `~kin-worker/.config/kin-worker/gateway-key`. Then it starts the worker server on `127.0.0.1:3874` if Kin has a build and isn't paused. The gateway URL is `KIN_WORKER_GATEWAY_URL` when set (`sudo KIN_WORKER_GATEWAY_URL=https://... /root/kin-ops/... setup`), else the `ANTHROPIC_BASE_URL` in kin's Claude settings; nothing else is read from kin's file. A changed unit is reloaded and the server restarted. -2. **Tools.** As kin-worker, inside the sandbox, it installs mise, the build's Node, Claude Code and Codex into kin-worker's own home. `git`, `gh`, `curl`, `python3`, `sqlite3`, iproute2 (`ss`) and util-linux (`nsenter`, `setpriv`) come from the distribution, system-wide. The worker unit's start check and `check`'s probes run only those root-owned copies, never anything on the worker's `PATH`. -3. **The two credentials**, as above, then `claude -p ok` as kin-worker to show the key works. +1. **Users, units and sandbox.** It creates `kin-worker` (home mode 700, not in group `kin`), sets `/home/kin` to mode 700, installs `/usr/local/sbin/kin-worker`, the units `kin-worker-server.service` and `kin-worker-link.{service,timer}`, and `/etc/sudoers.d/kin-worker`, which lets `kin` run exactly `systemctl stop`, `start` and `try-restart` on the worker unit and `kin-worker upload-pack `. As kin-worker, it turns on Linked environments in the worker environment's settings and writes `~kin-worker/.claude/settings.json` with only the gateway URL and an `apiKeyHelper` that reads `~kin-worker/.config/kin-worker/gateway-key`. The worker server on `127.0.0.1:3874` starts once its tools are installed (step 2), if Kin has a build and isn't paused. The gateway URL is `KIN_WORKER_GATEWAY_URL` when set (`sudo KIN_WORKER_GATEWAY_URL=https://... /root/kin-ops/... setup`), else the `ANTHROPIC_BASE_URL` in kin's Claude settings; nothing else is read from kin's file. A changed unit is reloaded and the server restarted. +2. **Tools.** As kin-worker, inside the sandbox, it installs mise, the build's Node, Claude Code and Codex into kin-worker's own home, then starts the worker server. `git`, `gh`, `curl`, `python3`, `sqlite3`, iproute2 (`ss`) and util-linux (`nsenter`, `setpriv`) come from the distribution, system-wide. The worker unit's start check and `check`'s probes run only those root-owned copies, never anything on the worker's `PATH`. +3. **The two credentials**, as above, then `claude -p ok` as kin-worker to show the key works. The gateway key is compared with kin's before it is written, and setup stops without asking if it can't read kin's key to compare. Ctrl-C at a prompt turns echo back on and writes nothing. 4. **Linked environments on kin-server.** kin sets that one key in its own `settings.json`, which the server watches. If the file can't be parsed it is left alone and setup says to turn it on from a client: Settings → Labs → Linked environments. -5. **Projects.** For every repository Kin publishes to (`KIN_GITHUB_REPOS` and Kin's own) and any you name, kin-worker clones `https://github.com//` into `/home/kin-worker/projects/` and registers it in the worker environment. Memory repositories are skipped. A lead starts work in a repository by name, and Kin matches the name to the checkout here by its GitHub remote. A private repository that kin-worker can't read yet is listed with ✗; give it the read-only token below and run setup again. -6. **The link.** `kin-worker link` links kin-server to `http://127.0.0.1:3874` with full access and writes `KIN_WORKER_ENVIRONMENT_ID` into `~kin/.t3/service.env`. A link lasts 30 days; `kin-worker-link.timer` renews it weekly. +5. **Projects.** For every repository Kin publishes to (`KIN_GITHUB_REPOS` and Kin's own) and any you name, kin-worker clones `https://github.com//` into `/home/kin-worker/projects/` and registers it in the worker environment. Memory repositories are skipped. A lead starts work in a repository by name, and Kin matches the name to the checkout here by its GitHub remote. A private repository that kin-worker can't read yet is listed with ✗ and setup exits non-zero; give kin-worker the read-only token below, which setup doesn't ask for, and run setup again. +6. **The link.** Once the worker server answers on its port (up to a minute), `kin-worker link` links kin-server to `http://127.0.0.1:3874` with full access and, as kin, writes `KIN_WORKER_ENVIRONMENT_ID` into `~kin/.t3/service.env` (replaced whole, mode 600; created if missing). A link lasts 30 days; `kin-worker-link.timer` renews it weekly. 7. **`kin-worker check`.** Every line must be ✓ before leads start workers. Three things stay with you. Do the GitHub one before setup, so its repositories are there to clone and `check` can pass: diff --git a/ops/kin/host/kin-worker.sh b/ops/kin/host/kin-worker.sh index c57a1574f9db..fac60b6488b7 100755 --- a/ops/kin/host/kin-worker.sh +++ b/ops/kin/host/kin-worker.sh @@ -118,11 +118,13 @@ cmd_run() { local io=--pipe allow=() if [ -t 0 ] && [ -t 1 ]; then io=--pty; fi mapfile -t allow < <(allowed_networks) + # systemd-run looks a bare name up on root's PATH. Root's /usr/bin/env looks it up instead, + # inside the sandbox as kin-worker on WORKER_PATH, so the worker's own mise or claude is found. systemd-run --quiet --wait --collect "$io" \ --uid="$WORKER_USER" --gid="$WORKER_USER" \ --working-directory="$WORKER_HOME" \ --setenv=HOME="$WORKER_HOME" --setenv=PATH="$WORKER_PATH" \ - "${SANDBOX[@]}" "${allow[@]}" -- "$@" + "${SANDBOX[@]}" "${allow[@]}" -- /usr/bin/env -- "$@" } refuse_unsafe_users() { @@ -177,11 +179,16 @@ print(json.load(open(sys.argv[1])).get("env", {}).get("ANTHROPIC_BASE_URL", "")) } # Writes stdin to a new file of the worker's, mode 600, as kin-worker; an existing one stays. -# Exits 3 when the file is already there. +# Exits 3 when the file is already there. The file appears whole or not at all: an interrupted +# write leaves no partial file behind. worker_write_new() { worker_do /bin/sh -c 'umask 077 if [ -e "$1" ] || [ -L "$1" ]; then exit 3; fi -/usr/bin/install -d -m 0700 "$(dirname "$1")" && cat >"$1"' sh "$1" +/usr/bin/install -d -m 0700 "$(dirname "$1")" || exit 1 +scratch="$1.new.$$" +trap "/bin/rm -f \"\$scratch\"" EXIT +trap "exit 1" INT TERM HUP +cat >"$scratch" && /bin/mv -f "$scratch" "$1"' sh "$1" } write_claude_settings() { @@ -264,9 +271,27 @@ cmd_upload_pack() { -- /usr/bin/git upload-pack --timeout=60 "$path" | head -c "$UPLOAD_PACK_MAX_BYTES" } +# What this script itself stands on, checked before it changes anything: Bash 4.4 (mapfile, +# empty arrays under `set -u`), GNU stat and sed, and systemd 249 (SocketBindDeny=). +MIN_SYSTEMD=249 +preflight() { + [ "$(uname -s)" = Linux ] || die "needs Linux with systemd." + [ "${BASH_VERSINFO[0]}" -gt 4 ] || { [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -ge 4 ]; } || + die "needs Bash 4.4 or later; this is Bash $BASH_VERSION." + stat --version 2>/dev/null | grep -q GNU || die "needs GNU stat (coreutils)." + sed --version 2>/dev/null | grep -q 'GNU sed' || die "needs GNU sed." + local version + version="$(systemctl --version 2>/dev/null | sed -n '1s/^systemd \([0-9]*\).*/\1/p')" + [ -n "$version" ] && [ "$version" -ge "$MIN_SYSTEMD" ] || + die "needs systemd $MIN_SYSTEMD or later (SocketBindDeny=); this host has ${version:-no systemctl}." +} + +# Tools a worker server starts with: mise, which brings the build's Node. +worker_tools_ready() { in_sandbox mise --version >/dev/null 2>&1; } + install_steps() { if [ "$DRY_RUN" -eq 0 ]; then - [ "$(uname -s)" = Linux ] || die "needs Linux with systemd." + preflight need_root install refuse_unsafe_source fi @@ -326,16 +351,29 @@ install_steps() { # 4. Claude's gateway settings, the worker's own. if [ "$DRY_RUN" -eq 0 ]; then write_claude_settings; fi - # 5. Start what can start. + # 5. Start what can start. The server waits for its tools: started without mise it would + # restart until systemd's start limit, and refuse to start after that. run_cmd systemctl enable kin-worker-server.service kin-worker-link.timer run_cmd systemctl start kin-worker-link.timer - if [ -f "$KIN_HOME/kin-current/apps/server/dist/bin.mjs" ]; then - run_cmd systemctl start kin-worker-server.service - else + if [ ! -f "$KIN_HOME/kin-current/apps/server/dist/bin.mjs" ]; then say "No Kin build at $KIN_HOME/kin-current yet: run update.sh as $KIN_USER, then this again." + elif [ "$DRY_RUN" -eq 1 ]; then + say "+ systemctl start kin-worker-server.service, once $WORKER_USER's tools are installed" + elif worker_tools_ready; then + start_worker_server + else + say "No tools for $WORKER_USER yet, so the worker server isn't started: run setup." fi } +# Starts the worker server, clearing a start limit an earlier failed start left behind. +start_worker_server() { + if systemctl is-active --quiet kin-worker-server.service; then return 0; fi + systemctl reset-failed kin-worker-server.service >/dev/null 2>&1 || true + run_cmd systemctl start kin-worker-server.service || + die "couldn't start kin-worker-server. See: journalctl -u kin-worker-server -n 50" +} + cmd_install() { if [ "${1:-}" = --dry-run ]; then DRY_RUN=1; fi install_steps @@ -364,8 +402,10 @@ trusted_bins() { [ $((8#$(stat -L -c %a "$bin") & 8#022)) -eq 0 ] || return 1 done } +# kin-server reads service.env if it is there (`EnvironmentFile=-`), so a missing one holds nothing. service_env_value() { - sed -n "s/^$1=//p" "$KIN_HOME/.t3/service.env" 2>/dev/null | tail -n1 + [ -f "$KIN_HOME/.t3/service.env" ] || return 0 + sed -n "s/^$1=//p" "$KIN_HOME/.t3/service.env" | tail -n1 } FAILED=0 @@ -402,9 +442,52 @@ checklist() { cmd_check } +# Waits up to a minute for the worker server to answer HTTP, and stops early if it has stopped. +wait_for_worker_server() { + local _ state + for _ in {1..60}; do + if [ "$(/usr/bin/curl -s -m 2 -o /dev/null -w '%{http_code}' "$WORKER_URL/" 2>/dev/null)" != 000 ]; then + return 0 + fi + state="$(systemctl is-active kin-worker-server.service 2>/dev/null || true)" + case "$state" in inactive | failed) break ;; esac + sleep 1 + done + die "kin-worker-server isn't answering on $WORKER_URL (${state:-unknown}). Kin paused, or a start that failed? See: journalctl -u kin-worker-server -n 50" +} + +# Sets KIN_WORKER_ENVIRONMENT_ID in kin's service.env, as kin: every other line stays, and the +# file is replaced whole (a scratch file beside it, then a rename), mode 600. +set_kin_worker_environment_id() { + as_user "$KIN_USER" /usr/bin/python3 -I -c 'import os, sys, tempfile +path = os.path.realpath(sys.argv[1]) +try: + with open(path) as handle: + lines = handle.read().splitlines() +except FileNotFoundError: + lines = [] +lines = [line for line in lines if not line.startswith("KIN_WORKER_ENVIRONMENT_ID=")] +lines.append("KIN_WORKER_ENVIRONMENT_ID=" + sys.argv[2]) +directory = os.path.dirname(path) +os.makedirs(directory, mode=0o700, exist_ok=True) +descriptor, scratch = tempfile.mkstemp(prefix=".service.env.", dir=directory) +try: + with os.fdopen(descriptor, "w") as handle: + handle.write("\n".join(lines) + "\n") + handle.flush() + os.fsync(handle.fileno()) + os.chmod(scratch, 0o600) + os.replace(scratch, path) +except BaseException: + os.unlink(scratch) + raise' "$KIN_HOME/.t3/service.env" "$1" +} + cmd_link() { need_root link - systemctl is-active --quiet kin-worker-server.service || die "kin-worker-server is not running." + systemctl is-active --quiet kin-worker-server.service || + die "kin-worker-server is not running. Kin paused, or a start that failed? See: systemctl status kin-worker-server" + wait_for_worker_server local code linked id # The pairing code stays in this shell: it is never printed or logged. code="$(cmd_run t3 pair --base-dir "$WORKER_HOME/.t3" --label kin-server >"$env_file" + set_kin_worker_environment_id "$id" || die "couldn't set KIN_WORKER_ENVIRONMENT_ID in $env_file as $KIN_USER." say "Set KIN_WORKER_ENVIRONMENT_ID=$id in $env_file. Apply it at a quiet moment:" say " sudo -u $KIN_USER XDG_RUNTIME_DIR=/run/user/$(id -u "$KIN_USER") systemctl --user restart kin-server" } # --- setup ----------------------------------------------------------------------------------- -# One secret from the terminal into SECRET. Echo goes off before the prompt shows, so a paste -# that lands early isn't shown either. It stays in this shell: never printed, logged or put on a -# command line. No terminal reads as skipped. +# setup asks on the terminal, so it refuses to start, before it changes anything, without one. +require_terminal() { + [ -t 0 ] && (exec 3<>/dev/tty) 2>/dev/null || + die "setup asks for credentials on the terminal: run it from an interactive shell (ssh -t), with nothing piped in. Nothing was changed." +} + +# One secret from the terminal into SECRET, line ends dropped. Echo goes off before the prompt +# shows, so a paste that lands early isn't shown either. It stays in this shell: never printed, +# logged or put on a command line. Ctrl-C turns echo back on and exits before anything is written. SECRET="" TTY_SAVED="" +restore_tty() { + if [ -n "$TTY_SAVED" ]; then stty "$TTY_SAVED" /dev/null || true; fi + TTY_SAVED="" +} ask_secret() { SECRET="" - if ! { : >/dev/tty; } 2>/dev/null; then - say "No terminal to ask on (use ssh -t), so this was skipped." - return 0 - fi - TTY_SAVED="$(stty -g /dev/tty 2>/dev/null; exit 130' INT TERM HUP stty -echo /dev/tty IFS= read -r SECRET /dev/tty + SECRET="${SECRET//[$'\r\n']/}" } # mise, the build's Node, Claude Code and Codex in kin-worker's own home, installed by kin-worker # inside the sandbox, so nothing of root's, kin's or the owner's is within the installers' reach. +# Each install has a time limit. setup_tools() { local version tool version="$(node_version_of "$KIN_HOME/kin-current")" [ -n "$version" ] || die "no engines.node in $KIN_HOME/kin-current/package.json." - if ! in_sandbox mise --version >/dev/null 2>&1; then + if ! worker_tools_ready; then say "Installing mise for $WORKER_USER." - in_sandbox /bin/sh -c '/usr/bin/curl -fsSL https://mise.run | /bin/sh' >/dev/null || + # Downloaded whole to the sandbox's own /tmp first: a cut-off download never runs. + in_sandbox /usr/bin/timeout 600 /bin/sh -c 'set -e +installer="$(/usr/bin/mktemp)" +trap "/bin/rm -f \"\$installer\"" EXIT +/usr/bin/curl -fsSL --max-time 120 -o "$installer" https://mise.run +[ -s "$installer" ] +/bin/sh "$installer"' >/dev/null || die "couldn't install mise as $WORKER_USER. Is https://mise.run reachable from the sandbox?" fi - in_sandbox mise use --global "node@$version" >/dev/null || + in_sandbox /usr/bin/timeout 900 mise use --global "node@$version" >/dev/null || die "couldn't install Node $version as $WORKER_USER." for tool in claude:@anthropic-ai/claude-code codex:@openai/codex; do if in_sandbox "${tool%%:*}" --version >/dev/null 2>&1; then continue; fi say "Installing ${tool#*:} for $WORKER_USER." - in_sandbox mise exec "node@$version" -- npm install --global --silent "${tool#*:}" || + in_sandbox /usr/bin/timeout 900 mise exec "node@$version" -- npm install --global --silent "${tool#*:}" || die "couldn't install ${tool#*:} as $WORKER_USER." done - in_sandbox mise reshim + in_sandbox mise reshim || die "couldn't refresh mise's shims as $WORKER_USER." say "Tools are in $WORKER_HOME: mise, Node $version, Claude Code, Codex." } # The first credential: kin-worker's own Claude gateway key, written by kin-worker to a file only -# it can read. A key already there stays. One that is kin's own is taken back out. +# it can read. A key already there stays. The key typed in is compared with kin's before any of it +# reaches kin-worker's file, since a running worker could copy it from there at once; the check +# after writing is a second wall. setup_gateway_key() { - local wrote=0 + local wrote=0 theirs + theirs="$(kin_key_hash)" || + die "couldn't read kin's own gateway key to compare with ($KIN_HOME/.claude/settings.json, its apiKeyHelper run as $KIN_USER), so nothing was asked for or written. Fix that, then setup again." if worker_do /usr/bin/test -s "$GATEWAY_KEY"; then say "$WORKER_USER already has a gateway key; keeping it. To replace it: sudo kin-worker run -- rm $GATEWAY_KEY, then setup again." else @@ -491,6 +588,10 @@ setup_gateway_key() { SECRET="" die "that key has spaces or unprintable characters; nothing was written." fi + if printf '%s' "$SECRET" | is_kin_key "$theirs"; then + SECRET="" + die "that is kin's own gateway key, which a worker must never hold; nothing was written. Create one for $WORKER_USER alone, then setup again." + fi # An empty file from an attempt that failed would block the new one. worker_do /bin/rm -f "$GATEWAY_KEY" printf '%s\n' "$SECRET" | worker_write_new "$GATEWAY_KEY" || { @@ -502,7 +603,7 @@ setup_gateway_key() { fi if ! own_gateway_key; then if [ "$wrote" -eq 1 ]; then worker_do /bin/rm -f "$GATEWAY_KEY"; fi - die "$WORKER_USER's gateway key is kin's own, which a worker must never hold. Create one for $WORKER_USER alone (and remove an old one: sudo kin-worker run -- rm $GATEWAY_KEY), then setup again." + die "$WORKER_USER's gateway key is kin's own, or couldn't be compared with kin's; a worker must never hold kin's. Create one for $WORKER_USER alone (and remove an old one: sudo kin-worker run -- rm $GATEWAY_KEY), then setup again." fi if [ "$wrote" -eq 1 ]; then say "Wrote the key to $GATEWAY_KEY (mode 600, $WORKER_USER's)."; fi if in_sandbox /usr/bin/timeout 90 claude -p ok >/dev/null 2>&1; then @@ -566,10 +667,22 @@ checkout_origin() { in_sandbox /usr/bin/git -C "$1" remote get-url origin 2>/dev/null | sed -n 's#^.*github\.com[:/]\([^/]*/[^/]*\)$#\1#p' | sed 's/\.git$//' } +# How `t3 project add` went, from its exit status and output: added, registered (it already was) +# or failed. Only a failed run that says the project already exists counts as registered. +registration_result() { + if [ "$1" -eq 0 ]; then + echo added + return + fi + case "$2" in + *"Active project "*" already exists for '"*) echo registered ;; + *) echo failed ;; + esac +} # One repository: a checkout of kin-worker's, cloned by kin-worker in the sandbox with whatever # read access it has, and registered in the worker environment. Run again, it changes nothing. setup_project() { - local slug="$1" dir="$WORKER_HOME/projects/${1#*/}" origin added + local slug="$1" dir="$WORKER_HOME/projects/${1#*/}" origin added status=0 origin="$(checkout_origin "$dir" || true)" if [ -n "$origin" ] && [ "$(lower "$origin")" != "$(lower "$slug")" ]; then # Another owner's repository of the same name already has that directory. @@ -589,10 +702,10 @@ setup_project() { say " ✗ $slug: $dir is a checkout of $origin." return 1 fi - added="$(in_sandbox t3 project add "$dir" --base-dir "$WORKER_HOME/.t3" 2>&1 || true)" - case "$added" in - *"Added project"*) say " ✓ $slug at $dir" ;; - *"already exists"*) say " ✓ $slug at $dir, already registered" ;; + added="$(in_sandbox t3 project add "$dir" --base-dir "$WORKER_HOME/.t3" 2>&1)" || status=$? + case "$(registration_result "$status" "$added")" in + added) say " ✓ $slug at $dir" ;; + registered) say " ✓ $slug at $dir, already registered" ;; *) say " ✗ $slug: it is at $dir, but registering it failed: $(printf '%s\n' "$added" | tail -n1)" return 1 @@ -600,7 +713,7 @@ setup_project() { esac } # Every repository Kin publishes to (KIN_GITHUB_REPOS and Kin's own), plus any named on the -# command line: the ones a lead's work can end in a pull request on. +# command line: the ones a lead's work can end in a pull request on. Fails when any is missing. setup_projects() { local slug skip repos missing=0 skip="$(memory_repos || true)" @@ -618,12 +731,14 @@ setup_projects() { setup_project "$slug" || missing=1 done if [ "$missing" -eq 1 ]; then say "Leads can't start work in the ✗ ones until they are fixed; run setup again then."; fi + return "$missing" } # `setup [owner/repo ...]`: every step from a fresh host to a passing `check` that needs no # credential, and a prompt for the two that do. Safe to run again: each step keeps what is there. +# Exits non-zero when a check fails or a repository couldn't be set up; the transcript still runs. cmd_setup() { - local repos=() arg before + local repos=() arg before missing=0 for arg in "$@"; do case "$arg" in --dry-run) DRY_RUN=1 ;; @@ -634,6 +749,7 @@ cmd_setup() { if [ "$DRY_RUN" -eq 1 ]; then install_steps say "+ as $WORKER_USER, in the sandbox: install mise, the build's Node, Claude Code and Codex" + say "+ start kin-worker-server" say "+ ask for $WORKER_USER's Claude gateway key, and optionally a Codex API key" say "+ turn on Linked environments in kin's settings" say "+ clone and register every repository in KIN_GITHUB_REPOS and Kin's own ${repos[*]:-}" @@ -642,27 +758,37 @@ cmd_setup() { say "Dry run: nothing was changed." return 0 fi + # Everything setup stands on, checked before it changes anything. + preflight need_root setup + require_terminal + id -u "$KIN_USER" >/dev/null 2>&1 || die "no $KIN_USER user yet: run /root/kin-ops/ops/kin/host/setup.sh first." [ -f "$KIN_HOME/kin-current/apps/server/dist/bin.mjs" ] || die "no Kin build at $KIN_HOME/kin-current yet: run update.sh as $KIN_USER first (README, \"Install\")." + if [ ! -f "$KIN_HOME/.t3/service.env" ]; then + say "No $KIN_HOME/.t3/service.env yet, so KIN_GITHUB_REPOS and KIN_GITHUB_APP_ID are unset. The link creates it with the worker environment's id." + fi install_steps setup_tools + start_worker_server setup_gateway_key setup_codex if enable_kin_links; then say "Linked environments are on for kin-server."; else say "Couldn't turn on Linked environments in kin's settings. From any client: Settings → Labs → Linked environments." fi - setup_projects ${repos[@]+"${repos[@]}"} + setup_projects ${repos[@]+"${repos[@]}"} || missing=1 if [ "$(stat -c %d "$WORKER_HOME")" = "$(stat -c %d "$KIN_HOME")" ]; then say "Note: $WORKER_HOME shares a filesystem with $KIN_HOME. Give $WORKER_USER a disk quota or its own filesystem, so a worker can't fill Kin's disk." fi - systemctl is-active --quiet kin-worker-server.service || systemctl start kin-worker-server.service || true before="$(service_env_value KIN_WORKER_ENVIRONMENT_ID)" cmd_link if [ "$(service_env_value KIN_WORKER_ENVIRONMENT_ID)" != "$before" ]; then say "Leads can start workers once kin-server restarts with that id. Nothing here restarts it." fi checklist + if [ "$missing" -eq 1 ]; then + die "the isolation checks pass, but a repository marked ✗ above isn't in the worker environment. Fix it, then setup again." + fi } # --- check ----------------------------------------------------------------------------------- @@ -696,7 +822,7 @@ sandbox_matches() { systemctl stop "$probe.service" >/dev/null 2>&1 || true systemctl reset-failed "$probe.service" >/dev/null 2>&1 || true if ! systemd-run --quiet --collect --unit="$probe" --uid="$WORKER_USER" --gid="$WORKER_USER" \ - "${SANDBOX[@]}" "${allow[@]}" -- sleep 120 >/dev/null 2>&1; then + "${SANDBOX[@]}" "${allow[@]}" -- /usr/bin/sleep 120 >/dev/null 2>&1; then printf ' ✗ the sandbox probe unit could not start\n' FAILED=1 return @@ -760,25 +886,39 @@ home_closed() { [ "$(stat -c %a "$1")" = 700 ] && no_acl "$1"; } # The worker's Claude settings as text, read by kin-worker, never by root through its path. worker_settings() { worker_do /bin/cat "$WORKER_HOME/.claude/settings.json"; } sha() { /usr/bin/sha256sum | cut -d' ' -f1; } -# kin's gateway key: its apiKeyHelper's output, else an inline key. +# A gateway key on stdin, as compared: line ends are dropped, so kin's key with a newline after +# it is still kin's key. +key_hash() { tr -d '\r\n' | sha; } +# Whether the key on stdin is the one whose key_hash is $1. No hash, no kin key: never a match. +is_kin_key() { + local mine + mine="$(key_hash)" + [ -n "$1" ] && [ "$mine" = "$1" ] +} +# key_hash of kin's gateway key: its apiKeyHelper's output, else an inline key. Prints nothing +# when kin's Claude settings name no key, and fails when they name one that can't be read. kin_key_hash() { local settings="$KIN_HOME/.claude/settings.json" helper key - [ -f "$settings" ] || return 0 + [ -e "$settings" ] || return 0 helper="$(/usr/bin/python3 -I -c 'import json, sys -print(json.load(open(sys.argv[1])).get("apiKeyHelper", ""))' "$settings" 2>/dev/null || true)" - if [ -n "$helper" ]; then key="$(as_kin sh -c "$helper" 2>/dev/null || true)"; else +print(json.load(open(sys.argv[1])).get("apiKeyHelper") or "")' "$settings" 2>/dev/null)" || return 1 + if [ -n "$helper" ]; then + key="$(as_kin /usr/bin/timeout 30 /bin/sh -c "$helper" /dev/null)" || return 1 + [ -n "$key" ] || return 1 + else key="$(/usr/bin/python3 -I -c 'import json, sys -env = json.load(open(sys.argv[1])).get("env", {}) -print(env.get("ANTHROPIC_API_KEY") or env.get("ANTHROPIC_AUTH_TOKEN") or "")' "$settings" 2>/dev/null || true)" +env = json.load(open(sys.argv[1])).get("env") or {} +print(env.get("ANTHROPIC_API_KEY") or env.get("ANTHROPIC_AUTH_TOKEN") or "")' "$settings" 2>/dev/null)" || return 1 + [ -n "$key" ] || return 0 fi - if [ -n "$key" ]; then printf '%s' "$key" | tr -d '\r\n' | sha; fi + printf '%s' "$key" | key_hash } # The worker's key is the file its settings' helper reads (settings_hold_no_token checks that). -# Line ends are dropped on both sides: kin's key with a newline after it is still kin's key. +# Fails when kin's key can't be read to compare with. own_gateway_key() { local mine theirs - mine="$(worker_do /bin/cat "$GATEWAY_KEY" 2>/dev/null | tr -d '\r\n' | sha)" - theirs="$(kin_key_hash)" + theirs="$(kin_key_hash)" || return 1 + mine="$(worker_do /bin/cat "$GATEWAY_KEY" 2>/dev/null | key_hash)" || return 1 [ "$mine" != "$(printf '' | sha)" ] && [ "$mine" != "$theirs" ] } # The worker's settings carry no token inline, name nothing of kin's, and read the key file. @@ -980,16 +1120,21 @@ cmd_check() { fi } -case "${1:-install}" in - install) shift || true; cmd_install "$@" ;; - setup) shift; cmd_setup "$@" ;; - --dry-run) cmd_install --dry-run ;; - link) cmd_link ;; - check) cmd_check ;; - run) shift; cmd_run "$@" ;; - node) shift; cmd_node "$@" ;; - upload-pack) shift; cmd_upload_pack "$@" ;; - may-start) cmd_may_start ;; - -h | --help) sed -n '2,19p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' ;; - *) die "unknown command: $1 (setup, install, link, check, run)" ;; -esac +main() { + case "${1:-install}" in + install) shift || true; cmd_install "$@" ;; + setup) shift; cmd_setup "$@" ;; + --dry-run) cmd_install --dry-run ;; + link) cmd_link ;; + check) cmd_check ;; + run) shift; cmd_run "$@" ;; + node) shift; cmd_node "$@" ;; + upload-pack) shift; cmd_upload_pack "$@" ;; + may-start) cmd_may_start ;; + -h | --help) sed -n '2,19p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' ;; + *) die "unknown command: $1 (setup, install, link, check, run)" ;; + esac +} + +# Sourced (by scripts/kin-worker.test.ts), it defines the functions and runs nothing. +if [ "${BASH_SOURCE[0]}" = "$0" ]; then main "$@"; fi diff --git a/scripts/kin-worker.test.ts b/scripts/kin-worker.test.ts new file mode 100644 index 000000000000..5a5448be6f50 --- /dev/null +++ b/scripts/kin-worker.test.ts @@ -0,0 +1,52 @@ +// @effect-diagnostics nodeBuiltinImport:off - Sources the real host script's helpers in bash. +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as NodeChildProcess from "node:child_process"; +import * as NodePath from "node:path"; +import { describe, expect, it } from "vite-plus/test"; + +const SCRIPT = NodePath.resolve(import.meta.dirname, "../ops/kin/host/kin-worker.sh"); + +// Sourcing the script defines its functions and runs nothing; `program` then calls them. +function runHelpers(program: string) { + const { status, stdout, stderr } = NodeChildProcess.spawnSync( + "bash", + ["-c", `source "$1"\n${program}`, "bash", SCRIPT], + { encoding: "utf8", env: { ...process.env, LC_ALL: "C" } }, + ); + expect(status, stderr).toBe(0); + return stdout.trim().split("\n"); +} + +// The helpers hash with /usr/bin/sha256sum and run on the Ubuntu host. +describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("kin-worker setup", () => { + it("knows kin's gateway key whatever its line ends, and never matches when kin has none", () => { + expect( + runHelpers(`kin="$(printf 'sk-kin\\r\\n' | key_hash)" +printf 'sk-kin' | is_kin_key "$kin" && echo same || echo different +printf 'sk-kin\\r' | is_kin_key "$kin" && echo same || echo different +printf 'sk-other' | is_kin_key "$kin" && echo same || echo different +printf 'sk-kin' | is_kin_key "" && echo same || echo different`), + ).toEqual(["same", "same", "different", "different"]); + }); + + it("reads registration from the exit status, and 'already registered' only from its message", () => { + expect( + runHelpers(`registration_result 0 "Added project p1 (app) at /home/kin-worker/projects/app." +registration_result 1 "Active project p1 already exists for '/home/kin-worker/projects/app'." +registration_result 1 "ProjectLiveServerRequestError: connect ECONNREFUSED, file already exists" +registration_result 0 "" +registration_result 7 ""`), + ).toEqual(["added", "registered", "failed", "added", "failed"]); + }); + + it("fails when a repository is missing, after trying every other one", () => { + const lines = runHelpers(`kin_repos() { echo GLDRoger/t3code; } +memory_repos() { echo GLDRoger/memory; } +setup_project() { echo "tried $1"; [ "$1" != acme/private ]; } +setup_projects acme/private GLDRoger/memory && echo "status 0" || echo "status $?"`); + expect(lines).toContain("tried GLDRoger/t3code"); + expect(lines).toContain("tried acme/private"); + expect(lines).not.toContain("tried GLDRoger/memory"); + expect(lines.at(-1)).toBe("status 1"); + }); +});