8000 Deck dies when you close the terminal it was started in (#29) by simon-inkie · Pull Request #56 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions .specs/29-deck-survives-terminal-91csgr/SPEC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# #29: Deck dies when you close the terminal it was started in

- Issue: #29
- Branch: 29-deck-survives-terminal-91csgr
- Started: 2026-10-07

## TL;DR

INSTALL.md starts Deck in the foreground, so closing that terminal stops it.
This adds a "Keep it running" option to INSTALL.md's start section: a detached
tmux session for the quick case, and an example systemd user unit
(`deck.service`, `Restart=always`, `EnvironmentFile=.env.production`) with the
`loginctl enable-linger` step a user unit needs to outlive logout. Start on
boot is out of scope and tracked elsewhere.

## Scope

In scope:
- INSTALL.md: a "Keep it running" subsection after "Build, and start", with the
tmux option and the systemd user unit option.
- An example unit, `deck.service`, in the repository.
- Whatever the rig needs so INSTALL.md still passes its shape stage.

Out of scope (and where it goes instead):
- Starting Deck at boot as a hard requirement: tracked separately per the issue.
- Changes to `start-production.mjs` or `deploy.mjs`.

## Acceptance

- INSTALL.md's "start Deck" section offers a "keep it running" option.
- The example unit is in the repo and starts Deck on an Ubuntu 24.04 box with
`systemctl --user start deck`.

## Test plan

Automated:
- `node rig/run.mjs --self-test`, and the shape stage's coverage check run
directly against INSTALL.md: no unclaimed or unknown-language blocks, and the
long-running prose probes still match.
- The repository checks in CONTRIBUTING.md. On 2026-10-07 typecheck,
boundaries, leaks, licence, lib-version, dashes and the deck suite
(2168 passed) were green. Two base tests fail identically on `main` in the
environment this was done in, so they are not this change's:
`create-agent.test.ts` "reports CANNOT VERIFY" (a model CLI is on `PATH`
there) and `shell-suites.test.ts` "inbox.test.sh". `check:leaks:messages`
flags only commits already on `main`.

Manual, because the rig cannot run a systemd user manager (2026-10-07):
- `systemd-analyze --user verify` on the unit with the placeholders filled in.
- An `ubuntu:24.04` container booted with systemd as PID 1, Node and a built
clone copied in, a normal user `you`. As that user, from the clone root, the
INSTALL.md blocks run byte for byte: the four roots and `.env`, then the unit
install block. `systemctl --user start deck` came up active and
`GET /` showed "No agents yet" and "Nothing needs you".
- Without linger, logging out stopped Deck (connection refused). With linger,
Deck still answered 15 seconds after logout.
- `loginctl enable-linger "$USER"` as the user was refused ("Access denied")
in that container, which has no polkit; root enabling it stood in for the
documented `sudo` fallback.
- `kill -9` of the main process: systemd restarted it (`NRestarts=1`) and it
answered again. `systemctl --user stop deck` left no node process behind.
- The tmux block run from the clone root; Deck answered from a later, separate
shell, and `tmux kill-session -t deck` stopped it.

## Decisions log

### 2026-10-07: work picked up
- What changed: SPEC created.
- Why: first commit on the branch, per DEVELOPMENT.md. The folder uses the
branch name as given.
- Supersedes: nothing, additive.
- Commits: docs(install): start SPEC for keeping Deck running (#29)

### 2026-10-07: tmux and systemd options, rig language
- What changed: INSTALL.md gains "Keep it running" after "Build, and start",
with a detached tmux session and a systemd user unit at
`packages/deck/deploy/deck.service`, plus `loginctl enable-linger`. The
rig's plan learns a non-executed `sh-manual` block language.
- Why:
- Unit with `@DECK_DIR@` and `@NODE_DIR@` placeholders, filled by a `sed`
in the document: the clone can be anywhere, and a user unit cannot use a
relative `WorkingDirectory`. `%h/inkie-deck` would assume a path the
document never sets.
- Node by absolute path, and first on the unit's `PATH`: the launcher spawns
the voice proxy as `node` from `PATH`, and its own comments record a
service manager resolving the wrong Node once.
- `EnvironmentFile=-.../.env.production`, optional: INSTALL.md already has
the reader write `.env`, which the launcher reads anyway. Requiring a
second file would duplicate the four roots. This keeps the existing claim
true that the EnvironmentFile wins over `.env`.
- `Restart=always`, `RestartSec=5`, `WantedBy=default.target`, default
`KillMode`: the launcher's SIGTERM handler does not kill the app server
(rig/container-image-note.md), and killing by control group covers that.
- tmux session named `deck`, not `agents`, so it stays out of tier 3's
session.
- New `sh-manual` language rather than `sh`: every `sh` block must be
claimed by a rig run, a container has no user systemd, and a detached
second Deck would collide with the run's own on 4242. Adding a new rig
stage is out of scope; listed as a follow-up. The document says these
blocks are not executed.
- The WSL and start-on-boot sentences say "unverified" or "not checked"
because neither was measured here.
- Supersedes: nothing, additive.
- Commits: docs(install): keep Deck running after the terminal closes (#29)

## Follow-ups

- `packages/deck/scripts/deploy.mjs` defaults `MC_SERVICE` to
`inkie-deck.service`, while the example unit is `deck.service` as the issue
asks. Not changed here: deploy.mjs also needs a tailnet front and is not part
of the documented install. Needs an issue.
- Have the rig execute the Keep it running blocks, which needs a container
stage that boots systemd. Needs an issue.
- Start on boot: tracked elsewhere, per #29.
64 changes: 60 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ wrong, the rig goes red.
quietly stop being tested.
- Blocks tagged `text` are recorded output, printed here so you can compare.
- Blocks tagged `zsh` are the macOS path. Containers are Linux, so **the macOS
instructions in this document are unverified**, and are the one place here
where you are reading a belief rather than an observation.
instructions in this document are unverified**: there you are reading a
belief rather than an observation.
- Blocks tagged `sh-manual` are not executed either: a container has no
systemd user manager, and a second Deck would collide with the one the run
already started. They were run by hand when written, and can drift without
the rig going red. They are all in [Keep it running](#keep-it-running).

`rig/README.md` describes the harness. `node rig/run.mjs` runs it.

Expand Down Expand Up @@ -276,6 +280,8 @@ pnpm build
That keeps running too. The subshell is deliberate: the launcher must run with
`packages/deck` as its working directory (it resolves the build and the `.env`
relative to it), and the brackets mean your own shell stays where it was.
It also stops when you close that terminal; [Keep it running](#keep-it-running)
below has two ways round that.

It prints every root it resolved, and whether it found a `.env`, before it
listens. A missing or relative or nonexistent root is fatal at that point and
Expand Down Expand Up @@ -307,6 +313,55 @@ Agents appear once something writes into `INBOX_ROOT`, which is tier 3.
to the web server and skips the launcher that validates the roots and loads
the `.env`. `node scripts/start-production.mjs` is the supported way in.

### Keep it running

Started as above, Deck is a child of your terminal and dies with it. Two ways
to keep it up, from the root of your clone, after `pnpm build`. Use one, not
both: they want the same port.

**In a detached tmux session.** Quick, needs nothing but tmux, and survives
closing every terminal. Nothing restarts it if it crashes, and nothing starts
it again after a reboot.

```sh-manual
tmux new-session -d -s deck -c "$PWD/packages/deck" node scripts/start-production.mjs
```

`tmux attach -t deck` shows its output; `Ctrl-b d` leaves it running again.
`tmux kill-session -t deck` stops it. The session is called `deck` so it stays
out of the `agents` session tier 3 creates.

**As a systemd user service.** `packages/deck/deploy/deck.service` is an
example unit: `Restart=always`, so a crash is restarted, and logs go to the
journal. Install it with your clone and your Node filled in:

```sh-manual
mkdir -p ~/.config/systemd/user
sed -e "s|@DECK_DIR@|$PWD/packages/deck|g" -e "s|@NODE_DIR@|$(dirname "$(command -v node)")|g" \
packages/deck/deploy/deck.service > ~/.config/systemd/user/deck.service
systemctl --user daemon-reload
systemctl --user start deck
systemctl --user status deck
```

It reads the same `packages/deck/.env` as a hand-run start, so nothing else
needs configuring. `packages/deck/.env.production` is the unit's optional
`EnvironmentFile`: a value there beats `.env`.

A user service still stops when you log out, unless your user lingers:

```sh-manual
loginctl enable-linger "$USER"
```

If that is refused, run it with `sudo`. After a rebuild, `systemctl --user
restart deck` serves the new build, and `journalctl --user -u deck` shows the
launcher's output. `systemctl --user enable deck` also starts it when your
user manager starts, but starting Deck on boot is a separate piece of work and
has not been checked here. On WSL, systemd has to be switched on in
`/etc/wsl.conf`, which recent Ubuntu images already do; that is unverified
here too.

## 5. Bring your agents to life

**This tier is manual, and cannot be otherwise from here.** An agent is a
Expand Down Expand Up @@ -634,8 +689,9 @@ bind-mounted state directory handing you back files you do not own, and that is
worked through in `rig/container-image-note.md`.

**macOS and Windows are unverified.** The rig runs Linux containers. The
Ubuntu path in this document is executed on every run; the macOS block is not,
and no one has run this on Windows.
Ubuntu path in this document is executed on every run, apart from the
`sh-manual` blocks in Keep it running; the macOS block is not, and no one has
run this on Windows.

**`MC_USER` defaults to `alex`.** Set it deliberately, as above. It is not
only a display name: it is a filename in your inbox root.
Expand Down
32 changes: 32 additions & 0 deletions packages/deck/deploy/deck.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# deck.service: an example systemd USER unit that keeps Inkie Deck running
# after the terminal that started it is closed, and restarts it if it dies.
#
# Install it with the commands in INSTALL.md, "Keep it running". They replace
# the two placeholders below with absolute paths for your machine:
# @DECK_DIR@ your clone's packages/deck directory
# @NODE_DIR@ the directory holding the node you build with (`command -v node`)
# A copy with a placeholder left in will not start.
#
# The launcher still reads packages/deck/.env, exactly as a hand-run start does,
# so the four roots you already wrote there are enough. .env.production is
# optional; anything in it beats .env, because the process environment wins.
#
# After `pnpm build`, run `systemctl --user restart deck` to serve the new build.

[Unit]
Description=Inkie Deck
After=network.target

[Service]
Type=simple
WorkingDirectory=@DECK_DIR@
EnvironmentFile=-@DECK_DIR@/.env.production
# The launcher spawns the voice proxy as `node` from PATH, so put the same node
# first; tmux, which the application calls, is found further along.
Environment=PATH=@NODE_DIR@:/usr/local/bin:/usr/bin:/bin
ExecStart=@NODE_DIR@/node scripts/start-production.mjs
Restart=always
RestartSec=5

[Install]
WantedBy=default.target
5 changes: 4 additions & 1 deletion rig/install-plan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export const EXECUTABLE_LANGS = ["sh"];
*
* `text` is recorded output and `zsh` is the macOS path, which a Linux
* container cannot run and which the document therefore marks as unverified.
* `sh-manual` is the "Keep it running" section: a container has no systemd
* user manager, and a detached second Deck would collide with the one the run
* already started. The document says those blocks are not executed.
* A language outside this list fails the shape stage: unhandled, it would be
* neither executed nor accounted for.
*/
export const KNOWN_LANGS = ["sh", "text", "zsh"];
export const KNOWN_LANGS = ["sh", "text", "zsh", "sh-manual"];

const PREREQUISITES = ["1-prerequisites/sh#1", "1-prerequisites/sh#2", "1-prerequisites/sh#3"];
const DEPENDENCIES = ["2-install-the-dependencies/sh#1"];
Expand Down
Loading
0