# sessions Start, stop and resume **Claude Code sessions on the dev box from a web page**, without SSHing in yourself. Each session runs `claude` with **Remote Control**, so you then talk to it from the Claude app or the "Open in Claude" link. ``` phone/browser ──HTTPS──▶ prod: Caddy + Authelia ──▶ sessions-web (container, 127.0.0.1:) │ ssh, key locked to one command ▼ dev box: sessions-ctl --remote ──▶ systemd: claude-session@ └─ tmux -L claude- └─ claude --remote-control ``` | Part | Where | What | |---|---|---| | [`dev/sessions_ctl.py`](dev/sessions_ctl.py) | dev box, `~/.local/bin/sessions-ctl` | CLI + SSH forced command. Stdlib Python ≥ 3.11 | | [`dev/systemd/`](dev/systemd) | dev box, `~/.config/systemd/user/` | one unit per session; hourly key-IP refresh | | [`web/app.py`](web/app.py) | prod, container | session list + buttons; stdlib, no JS, no login of its own | | [`compose.yaml`](compose.yaml) | prod, panel | how the panel builds and runs the container | ## Security model The prod server holds a key that can reach the dev box, so a compromised prod must not mean a compromised dev box. The line in `~/.ssh/authorized_keys` is: ``` from="",restrict,command="…/sessions-ctl --remote" ssh-ed25519 AAAA… sessions-manager ``` - **`command=`**: whatever prod asks for, sshd runs `sessions-ctl --remote`. It reads `$SSH_ORIGINAL_COMMAND`, splits it without a shell, and accepts only `list repos status create start stop health`. Names and branches are checked against strict patterns; repos are picked by key from an allowlist in the config, so no URL or path crosses the link. - **`restrict`**: no PTY, no port/agent/X11 forwarding. (Tested: `-L` is refused.) - **`from=`**: the key only works from the address `reudy.net` resolves to. sshd runs with `UseDNS no`, so a hostname there would never match; instead `sessions-access-refresh.timer` re-resolves it hourly and rewrites the line. If DNS fails it keeps the old line rather than writing an empty one. - **No free text crosses the link.** There is deliberately no "initial prompt". Instructions go through Remote Control, which is guarded by your claude.ai account, not by prod. So the worst a hijacked prod can do is start sessions up to `max_running`, or stop them. - **The web app has no login of its own; Authelia is the login.** It serves only requests carrying `Remote-User`, which Caddy's `forward_auth` sets from Authelia (overwriting anything the client sent). With the header missing it answers 403. That's a tripwire in case the route ever loses forward_auth, not the protection itself. `SESSIONS_ALLOWED_USERS` can narrow it to named users. Authelia's policy for the domain should be `two_factor`. It is by default on prod. - Cross-site POSTs are refused by checking `Sec-Fetch-Site`/`Origin`, so no CSRF secret is needed. The page has a strict CSP and no JavaScript. The dev box host key is pinned (`StrictHostKeyChecking yes`). - The container is configured only by environment variables, including the SSH key (one base64 line). At start it writes the key to a 0600 file on its `/tmp` tmpfs. It runs rootless with a read-only root filesystem and all capabilities dropped, published only on `127.0.0.1`. - Every remote call is logged on the dev box: `journalctl -t sessions-ctl`. **No permission prompts:** sessions run with `--dangerously-skip-permissions`, so Claude acts without asking for approval: shell commands, file edits, git pushes. That's acceptable only because of where the limits already are. The git token is a bot limited to allowlisted repos, and branch protection makes an unmerged PR the worst case. Nothing production lives on the box, and instructions only arrive through Remote Control (your claude.ai account). The remaining risk is prompt injection: a fetched page or a malicious repo file can steer a session into running commands without anyone confirming them. Only allowlist repos you trust, and treat the dev box as disposable. The one-time "Bypass Permissions mode" warning would block an unattended session. `"skipDangerousModePermissionPrompt": true` in `~/.claude/settings.json` suppresses it. Without that setting the page shows *"stuck on the bypass-permissions warning"*. **Folder trust:** Claude Code asks "do you trust this folder?" on first launch in a new directory, which would block an unattended session. There is no CLI flag for that in interactive mode, so `sessions-ctl` records the trust in `~/.claude.json` (`projects[].hasTrustDialogAccepted`) for checkouts it creates. The repo allowlist is the trust decision. This relies on an internal Claude Code field. If a Claude Code update breaks it, the session shows *"stuck on the folder-trust prompt"* on the web page. See Troubleshooting. ## How a session lives - `create NAME REPO [BASE]`: clones the repo into `~/sessions/NAME`, creates branch `session/NAME`, writes `~/.local/state/sessions/NAME.json` (with a fixed conversation UUID) and runs `systemctl --user enable --now claude-session@NAME`. - The unit starts a **dedicated tmux server** (`tmux -L claude-NAME`) running `claude --dangerously-skip-permissions --remote-control dev-box-NAME`. The first launch uses `--session-id ` and later launches use `--resume `, so a restart continues the same conversation and keeps the same Remote Control link. - **Crash:** `Restart=always`, 10 s delay, at most 5 restarts in 10 min. - **Reboot:** enabled units start with the user manager, because `agent` has linger on. - **Stop:** `disable --now`, so the session stays stopped across reboots until you start it again. - **Limits:** `max_running` in the config (default 2). Each unit has `MemoryHigh=1200M` and `MemoryMax=1600M`, which apply to claude and everything it runs. - Attach locally: `tmux -L claude-NAME attach`. - Delete (local only, never over SSH): `sessions-ctl delete NAME --yes`. This refuses if the checkout has uncommitted or unpushed work unless you pass `--force`. ## Install: dev box ```sh git clone ssh://git@git.reudy.net:14922/reudy-net/sessions.git && cd sessions ./dev/install.sh # idempotent; re-run after every pull $EDITOR ~/.config/sessions/config.toml # [repos] allowlist, [ssh_access] allow_from_host sessions-ctl keygen # add --rotate to replace an existing key ``` `keygen` makes the key pair prod uses. It generates it in RAM, writes the public half to the config and `authorized_keys` (replacing the old `sessions-manager` line), and prints the five environment variables for prod to stdout, with a note on stderr. The private key is then deleted from this box, so the printed copy is the only one. Paste it, then clear the terminal. If it's lost, run `keygen --rotate` and paste again. Requires linger (`sudo loginctl enable-linger agent`); `install.sh` warns if it's off. ## Deploy on prod (panel) The repo root has a `compose.yaml`, so the panel can build and run it: 1. **New app** in the panel from the Forgejo repo `reudy-net/sessions`, branch `main`. 2. **Environment**: paste the five lines `sessions-ctl keygen` printed. Optional: `SESSIONS_ALLOWED_USERS=reudy`. 3. **Route**: e.g. `sessions.reudy.net` → service `web`, port `8470`, with **Authelia on**. From the CLI: `panelctl init sessions "sessions.reudy.net|127.0.0.1:18470||web:8470" true` 4. **Deploy.** The container log should say `sessions-web on 0.0.0.0:8470 → agent@:14922 …`. 5. Open `https://sessions.reudy.net`, sign in through Authelia, and you should see the session list. An error there comes from the dev box (see Troubleshooting). **Reboots:** the container has `restart: unless-stopped`. Rootless Podman only honours that at boot if the user's `podman-restart.service` is enabled (`systemctl --user is-enabled podman-restart` as the panel user). That applies to every panel app, so it belongs in the NixOS config. ### Without the panel Any container runtime works. Put the five variables in a `chmod 600` env file and run: ```sh podman build -t localhost/sessions-web web/ podman run -d --name sessions-web --restart unless-stopped \ --env-file ~/.config/sessions-web/env -p 127.0.0.1:8470:8470 \ --read-only --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges \ localhost/sessions-web ``` Then put it behind `forward_auth` in Caddy: ``` sessions.reudy.net { forward_auth 127.0.0.1:9091 { uri /api/authz/forward-auth copy_headers Remote-User Remote-Groups Remote-Email Remote-Name } reverse_proxy 127.0.0.1:8470 } ``` ## Troubleshooting | Symptom | Check | |---|---| | "forbidden: this page must be served behind Authelia" | The route has no forward_auth, or the user isn't in `SESSIONS_ALLOWED_USERS` | | Container exits with "configuration error" | One of the env values was pasted incompletely. Run `sessions-ctl keygen --rotate` again and paste all five lines | | Page says "dev box unreachable: Permission denied" | Prod's outgoing IP isn't the one `reudy.net` resolves to, or the env holds an old key. On the dev box, `sudo journalctl -u ssh -n 20` shows the source IP. `sessions-ctl refresh-access` shows what `from=` holds | | "Host key verification failed" | The dev box's host key changed. Check that the change is expected, then update `SESSIONS_DEV_HOST_KEY`. Never turn off checking | | Session `failed` | `journalctl --user -u claude-session@NAME -n 50`. After 5 crashes in 10 min systemd gives up; fix the cause, then Start again | | "stuck on the folder-trust prompt" | `tmux -L claude-NAME attach`, accept, detach. Then the trust field has moved in a Claude Code update, so fix `trust_workspace()` | | "stuck on the bypass-permissions warning" | Add `"skipDangerousModePermissionPrompt": true` to `~/.claude/settings.json`, then `tmux -L claude-NAME attach`, accept, detach | | No "Open in Claude" link | The link is read from the pane once Remote Control is up; refresh after a few seconds | ## Relation to `sess` The dev box's own `sess` tool (in the dev-box repo) manages the box's role, test and project agents in the **default** tmux server. This manager is separate: its own tmux sockets (`-L claude-NAME`), its own `~/sessions/`, and one systemd unit per session. The two don't see or affect each other, so `sess ls` doesn't list these sessions. Use `sessions-ctl list` for those. ## Tests ```sh python3 -m unittest discover -s dev/tests python3 -m unittest discover -s web/tests ```