Add repository: - New remote verb `add-repo KEY OWNER/NAME`. Off unless config.toml has [repo_add] (base_url + owners). Only listed owners; the dev box builds the URL itself, checks it with `git ls-remote` (bot access), then appends to [repos] under the lock. No URL crosses the SSH link. - `repos` also reports where adding is allowed, so the page shows the form only when it's on and rejects other hosts before calling SSH. - set_toml_string inserts after the section's last key, not after trailing blank lines / the next section's comments. Responsiveness: - web/static/app.js (progressive enhancement; every form still works without JS): forms go via fetch and get a JSON answer instead of POST-redirect-GET; buttons/cards show the change immediately; the list refreshes itself (15 s, 3 s while something is starting). - `repos` is cached for 5 min (refreshed on add-repo), so a page view is one SSH call instead of two. - `list` reads all unit states with one `systemctl show` instead of one per session: 676 -> 374 ms on the dev box with 21 sessions. - Static assets are content-hashed and cached immutably. Design: slate greys, hairline borders, black primary action, tinted status badges, monospace repo/branch, two columns on desktop, stacked on phones, light and dark. Docs: README security model, AGENTS.md (JS convention, add-repo invariant), config.example.toml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MCUMXh8zZConC75Mptbeou
226 lines
13 KiB
Markdown
226 lines
13 KiB
Markdown
# 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:<port>)
|
|
│ ssh, key locked to one command
|
|
▼
|
|
dev box: sessions-ctl --remote ──▶ systemd: claude-session@<name>
|
|
└─ tmux -L claude-<name>
|
|
└─ 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), [`web/static/`](web/static) | prod, container | session list + buttons; stdlib, optional 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="<IP of reudy.net>",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 add-repo status create start stop archive unarchive health`.
|
|
Names, titles and branches are checked against strict patterns; repos are
|
|
picked by key from an allowlist in the config, so no URL crosses the link.
|
|
`add-repo KEY OWNER/NAME` (see below) sends an owner/name path, never a URL.
|
|
- **`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 dev box host key is pinned (`StrictHostKeyChecking yes`).
|
|
- The page has a strict CSP: `script-src 'self'`, no inline script, no
|
|
third-party anything. `web/static/app.js` is progressive enhancement only:
|
|
every form also works with JavaScript off (POST, then redirect). With it,
|
|
forms are sent with `fetch` and answered as JSON, and the list refreshes
|
|
itself (every 15 s, every 3 s while a session is starting or stopping, and
|
|
only while the tab is visible).
|
|
- 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.
|
|
|
|
**Adding repos from the page** is off unless `config.toml` has a `[repo_add]`
|
|
section. With it, the page's *Add repository* form sends `add-repo KEY
|
|
OWNER/NAME`. The dev box accepts only owners listed in `repo_add.owners`,
|
|
builds the URL itself from `repo_add.base_url`, checks with `git ls-remote`
|
|
that the bot account can read the repo, and then appends it to `[repos]`. So
|
|
a hijacked prod could at most allowlist repos that (a) belong to an owner you
|
|
listed and (b) the bot account was already given access to. That is the same
|
|
set you'd trust anyway, but it does move the decision for that set from
|
|
editing `config.toml` to clicking a button. Removing a repo is still a
|
|
config edit.
|
|
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[<dir>].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 TITLE REPO [BASE]`: the title is what you type, e.g. `Fix login bug`
|
|
(letters, digits, `-` and single spaces, up to 48 chars). The session's NAME
|
|
is derived from it (`fix-login-bug`) and used for the directory, branch, unit
|
|
and tmux socket. The title is shown on the page and is the session's name in
|
|
the Claude app. It 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 <uuid>` and later launches use `--resume
|
|
<uuid>`, 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.
|
|
- **Archive:** stops the session and moves it to the page's "Archived" section.
|
|
The checkout and the conversation are kept; **Unarchive** puts it back in the
|
|
list, stopped. An archived session can't be started until it's unarchived.
|
|
- **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@<dev box>: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 --init \
|
|
--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 |
|
|
| "dev box unreachable: … Connection refused" | The dev box's UFW `limit` rule refuses the 6th new SSH connection from one address within 30 s (`journalctl -k \| grep "UFW LIMIT BLOCK"`). The app reuses one connection (`ControlPersist`), so this needs an unusual burst or a container that can't keep its control socket in `/tmp` |
|
|
| Container log: "running as PID 1" | Start it with `--init` (compose: `init: true`), otherwise exited SSH master connections pile up as zombies |
|
|
| "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; the page picks it up within seconds |
|
|
| *Add repository* missing | `[repo_add]` isn't set in `config.toml` (it's off by default). The page caches the repo list for 5 min |
|
|
| "can't reach OWNER/NAME" when adding | Typo, or the Forgejo bot account hasn't been added to that repo |
|
|
|
|
## 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
|
|
```
|