# Following a channel on hosts

`engram follow` watches an engram channel on a Unix host, installs each newer signed build into a managed home directory, restarts the target program, checks that it started cleanly, and rolls back to the previous build if health checks fail.

## Setup with systemd

Create one config file per managed program (the filename without `.conf` becomes the systemd instance name `engram-follow@<name>`):

```ini
# acme.conf
url = https://pkg.example.org
project = acme
channel = stable
signer = release@example.org namespaces="engram" ssh-ed25519 AAAA...
unit = acme.service
listen = tcp:443 udp:443
require = /etc/acme/secret
```

Set `ExecStart` in the managed program's systemd unit to the symlink inside the follower home:

```ini
ExecStart=/var/lib/engram/acme/bin/acme
```

Install and start the follower as root, then inspect its logs (one line per decision):

```sh
engram follow install acme.conf
journalctl -u engram-follow@acme
```

`engram follow install` copies the config to `/etc/engram/follow/<name>.conf`, writes `/etc/systemd/system/engram-follow@.service`, enables and restarts `engram-follow@<name>.service`, and runs `systemctl try-restart` on all existing `engram-follow@*.service` instances.

Outside systemd, omit `unit` and provide both `restart` and `health` shell commands, then run `engram follow run <config>` under your service manager.

## Configuration reference

Unknown keys are rejected as errors. Durations use Go syntax (`30s`, `5m`, `2h`).

| Key | Default | Meaning |
| --- | --- | --- |
| `url`, `project`, `channel` | required | Channel location `<url>/<project>/<channel>` |
| `signer` | required, repeatable | OpenSSH `allowed_signers` line for a pinned signing key |
| `artifact` | `<project>` | Artifact name in the manifest and executable filename under `bin/` (`.tar.gz` and `.tgz` archives are unpacked) |
| `home` | `/var/lib/engram/<name>` | Absolute path storing `builds/<commit>/`, `bin` symlink, and `state` |
| `unit` | | systemd unit to restart; must reach `active` state |
| `restart` | `systemctl restart <unit>` | Custom restart shell command (required when `unit` is empty) |
| `listen` | | Space-separated `tcp:PORT` or `udp:PORT` sockets the unit's main process must bind (Linux `/proc`, requires `unit`, repeatable) |
| `health` | | Shell command that must exit 0 when the program is healthy (required when `unit` is empty; `$MAINPID` is the unit's main PID, or `0` without `unit`) |
| `health_timeout`, `settle` | `30s`, `5s` | Time allowed to become healthy, then time the same PID must remain healthy |
| `interval`, `jitter` | `5m`, `1m` | Poll interval (minimum `1s`) and random added jitter; polls `head` with `If-None-Match` |
| `delay` | `0` | Wait time after this host first observes a build before installing it |
| `require` | | Repeatable path that must exist on disk before any install runs |
| `pause` | `<home>/pause` | Path whose existence pauses all installs |
| `token_file` | | File containing a private channel bearer token |
| `self` | `false` | Follow `engram` itself; cannot be combined with `unit`, `restart`, `health`, or `listen` |

## Subcommands

| Command | Action |
| --- | --- |
| `engram follow install <config>` | Copy config to `/etc/engram/follow/`, write the systemd template unit, enable, and start |
| `engram follow run <config>` | Poll continuously until signaled or until a self-update replaces `engram` |
| `engram follow once <config>` | Run a single poll and update cycle, then exit |
| `engram follow check <config>` | Validate config syntax, `<home>/state`, channel access, and manifest signature verification without modifying state |
| `engram follow rollback <config>` | Refuse the currently installed build and switch `bin` back to the previous build |
| `engram follow allow <config> <commit>` | Clear a previously refused commit so the follower may try it again |

## Restart, health checks, and rollback

When a newer build is ready to install, `engram follow` downloads and verifies it into `<home>/builds/<commit>/`, records it as pending, atomically updates `<home>/bin`, and runs the restart command.

- Restart waits until `systemctl restart` (or `restart`) returns; graceful draining belongs to the managed service (`KillSignal=` and `TimeoutStopSec=` in its unit file). The `health_timeout` clock starts after restart returns.
- If the follower is interrupted mid-update (for example by a reboot), the next start finishes verifying the pending build; on Linux (`/proc`), it skips a second restart if the unit is already running the new binary.
- If the build fails to come up or exits during `settle`, the follower marks that commit refused in `<home>/state`, switches `bin` back to the previous build, and restarts it. A refused commit is skipped until cleared with `engram follow allow`.
- Create `<home>/pause` (`touch /var/lib/engram/acme/pause`) to freeze installs on a host, and remove it to resume.

## Staggered rollouts across hosts

Give canary hosts `delay = 0` and production hosts a non-zero `delay` (for example `delay = 2h`). Each host records when it first saw a commit on `head`; if the build is scrapped during the delay window, waiting hosts see `404`/`403` when they fetch its manifest and never install it.

## Following `engram` itself

Create a self-follow config (follow `stable` so untested commits do not update host updaters):

```ini
# engram.conf
url = https://pkg.neuroplast.io
project = engram
channel = stable
signer = release@neuroplast.io namespaces="engram" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDCsEZcn1tiubvKQzEVs5pJ4QVXoFmSDtO+k6SdetUdf
self = true
```

Run `engram follow install engram.conf`. `engram` fetches itself into `/var/lib/engram/engram/` and updates `engram-follow@.service` to execute `/var/lib/engram/engram/bin/engram`.

Before switching `bin` to a newer `engram` build, the new binary must pass its own `engram follow check` against every config in `/etc/engram/follow/`. If any check fails, the new build is refused and nothing changes. Once `bin` switches, each running follower detects the replaced symlink between polls and exits cleanly so systemd restarts it on the new binary.

If an `engram` build passes `check` but fails later, roll back manually using the previous binary:

```sh
/var/lib/engram/engram/builds/<previous-commit>/engram follow rollback /etc/engram/follow/engram.conf
```
