docs, infra: SHADOW-02 measurements; box setup --threads

implementation.md gains the SHADOW-02 entry (tests, the two-box rehearsal,
per-box cost, the trace's own cost on the live loop and why a writer thread
was rejected). The runbook's resources and box choice carry the measured
numbers; fly-shadow-remote-setup takes --threads (default 4) with --cpus in
one drop-in.
This commit is contained in:
acamilo 2026-09-30 18:29:36 +00:00
parent b03ddc957d
commit c418a9b352
4 changed files with 98 additions and 18 deletions

View file

@ -683,6 +683,70 @@ a timing, load or segment-handling bug.
whose trace could not be created. In `FLY_TRACE_DIR` mode, a trace-creation failure no longer
stops flysim from starting. The test adds such a process, untraced, and the shadow fails.
### SHADOW-02 — The shadow on a build box (port slice)
**2026-09-30: built** on `port/shadow-02` off main (v0.7.0), and awaiting review. It changes no
live behaviour until `fly-shadow-run start` runs; the flysim binary is unchanged. The contract is
the SHADOW-02 amendment of [legacy-gameboy-v1](legacy-gameboy-v1.md) section 18.
**Why.** On the release container the shadow (v0.7.0, started 2026-09-30) compared 1,119 brain
seconds with zero divergence, but at about 27 ms a frame on the page and encoder CPUs it fell
further behind every hour, and the live realtime factor fell from 0.9998 to 0.93-0.98. It went
back to 0.999-1.0 the moment the shadow was stopped.
- **`shadow::remote`**: the frame protocol (a JSON header line and a body), the name checks that
guard every path, the forwarded settings (`FORWARDED_ENV`: game, macro mode, accepted adapters,
pins; no path, no secret).
- **`shadow::relay`** (`fly-shadow relay`, the container's `flyshadow.service` through a drop-in):
per quarter-second tick, the new saves, then the trace bytes of this run in file order, then
the journal read before the listing. A writer thread owns the ssh pipe, so a stalled link never
blocks it: saves are read as they appear and queued in memory (256 MiB), trace bytes wait on
disk, and a newer file and the journal wait for every older file. The heartbeat is touched only
while healthy (connected, the box's shadow of this run alive within 30 s, the box has every
byte that was on disk a minute ago). The box's verdict is written back only when it is this
run's, with the unsynced tail added to `lagTransitions` and a `relay` member.
- **`shadow::ingest`** (`fly-shadow ingest --root`, the forced command of the relay's key on the
box): the release directory and binary hashes must equal its own; a new run id resets the mirror
and restarts the shadow; appends only at the offset it holds (else `resync`); a file the shadow
finished is never written again (`done`, and the relay then deletes the container's copy);
reports liveness (heartbeat fresh, verdict this run's and running or passed).
- **The shadow**: `--run-id-file` (the verdict's `runId`; a new id exits 4); a `no-consumer` trace
stop while following is a `coverage` divergence; coverage reads the journal before listing the
traces. `shadow::release` holds the release hashing the binary and `check` share.
- **infra.** `fly-shadow-run start` runs remote when `/etc/fly/shadow-remote.env` exists and
refuses otherwise (`--local` keeps the old placement); `check` also needs `relay.json` healthy
and under a minute old; `status` shows the relay. `infra/box/` (never deployed by 05-deploy):
`flyshadow-remote.service` (the same release at the same path, `--run-id-file`, no lag back-off,
`Restart=always`, stays stopped on exit 3, `CPUWeight=1000`), `flyshadow-remote.path` (starts it
on a new run id) and `fly-shadow-remote-setup` (MANIFEST-verified root-owned release, the
`flyshadow` user, a root-owned `authorized_keys` with `restrict,command=` and optional `from=`).
`02-base.sh` installs `openssh-client` (no sshd). Lint holds all of it; the runbook's shadow
section has the set-up.
**Tests.** `tests/shadow_remote.rs` (ROM, the toy connectome) runs the real `flysim`, the relay and
the real `fly-shadow ingest` binary (over pipes where production has ssh) and a remote shadow:
a fresh fly and a restart with sugar pass and come back with the run id; a dead remote shadow stops
the heartbeat and flysim's trace, and the new shadow skips that stop as history (`trace-cap`) and
follows the next process; a stalled ingest leaves the relay reporting "not healthy", flysim stops
its trace, and when the link is back the box fails the verdict as `coverage` and the relay brings
it and `divergence.json` back and exits 3. Unit tests: frames, names, the release refusal, offsets
and `resync`, the run reset, the env allowlist.
**Two-box rehearsal** (FAFB, macros, row 67, real time, sugar every 10-30 s; a real flysim on one
build box standing in for the release container, the remote shadow on another behind a real sshd
with a `restrict,command=` key): see the SHADOW-02 run report. Cost of the shadow per live frame
(unthrottled replay of a recorded trace, mean of the step): fly-build-1 14.1 ms with 3 threads,
11.9 ms with 6; fly-build-2 13.9 / 13.0 / 12.2 ms with 3 / 4 / 6; fly-build-3 11.9 / 11.8 ms with
3 / 4 (beside another agent's 1.6-core soak). The relay took about 0.5 % of a core; the stream to
the box is about 2.1 GB per live hour (a 2.7 MB hot save every 5 s, and about 140 MB of trace).
**The trace's own cost on the live loop** (the coordinator's question): +1.3 ms a frame on the
sim thread (11.14 to 12.45 ms, unpaced, 3 threads, 4 A/B rounds on a build box), about half in
the post-frame digests (frame and work RAM) and half in the record's digests and JSON. A writer
thread for the digests and the file was tried and rejected: on flysim's pinned CPUs it preempts
the brain's parallel sweep and the frame took 19 ms. The live guard's baseline is taken before
the trace is on, so the trace's cost counts against it.
### STATE-01 — Coherent all-participant checkpoint/recovery
**Depends on:** SESSION-02, MEDIA-01; validate with fake agents first, then AGENT-01/ENV-01.

View file

@ -894,7 +894,10 @@ verdict comes back (`shadow::remote`, `shadow::relay`, `shadow::ingest`).
(oldest first; a file's size read after the directory listing, so an older file is complete
when a newer one reaches the box), then the sugar journal as read before the listing. Trace files
that started before the run (`FLY_SHADOW_RUN_ID`, the start's Unix ms) and saves older than a
minute before it are not sent.
minute before it are not sent. A writer thread owns the link, so a stall never blocks the relay:
new saves are read as they appear and held in memory (256 MiB, about 8 minutes; a hot save lives
about ten seconds), trace bytes wait on disk, and a newer file and the journal wait until every
older file is complete on its way.
- *The ingest* (`fly-shadow ingest --root <dir>`) is the forced command of the relay's key on the
box (`authorized_keys` `restrict,command=`); the box holds no credential for the container. It
refuses a relay whose release directory or binary SHA-256 differ from its own, so the box runs the

View file

@ -3,7 +3,7 @@
# Run as root ON THE BOX (never on the release container, never on the host), by the coordinator.
# Idempotent: re-run it for every release the container runs.
#
# fly-shadow-remote-setup RELEASE_TARBALL ROM_FILE RELAY_PUBKEY [--from ADDRS] [--cpus LIST]
# fly-shadow-remote-setup RELEASE_TARBALL ROM_FILE RELAY_PUBKEY [--from ADDRS] [--cpus LIST] [--threads N]
#
# RELEASE_TARBALL the exact flybrain-<version>.tar.gz deployed to the release container. It is
# installed at the SAME path, /opt/fly/releases/<version>, MANIFEST-verified,
@ -14,7 +14,10 @@
# RELAY_PUBKEY the public half of the relay's key (the private half lives only on the
# release container, /etc/fly/shadow-remote/id_ed25519, owned by fly).
# --from ADDRS restricts the key to the container's address (authorized_keys from=).
# --cpus LIST AllowedCPUs for the shadow (e.g. 3 cores of this box), a drop-in.
# --cpus LIST AllowedCPUs for the shadow, a drop-in: its own cores, never shared with the
# live fly's NUMA node.
# --threads N the shadow's sweep threads (default 4; one per core in --cpus). It must run
# clearly faster than real time: see the runbook's "Which box".
#
# What it sets up:
# - user flyshadow, home /srv/fly-shadow-remote (the mirror; 0750);
@ -32,13 +35,14 @@ die() { echo "fly-shadow-remote-setup: $*" >&2; exit 2; }
log() { echo "fly-shadow-remote-setup: $*"; }
[ "$(id -u)" = 0 ] || die "run as root on the build box"
[ $# -ge 3 ] || die "usage: fly-shadow-remote-setup RELEASE_TARBALL ROM_FILE RELAY_PUBKEY [--from ADDRS] [--cpus LIST]"
[ $# -ge 3 ] || die "usage: fly-shadow-remote-setup RELEASE_TARBALL ROM_FILE RELAY_PUBKEY [--from ADDRS] [--cpus LIST] [--threads N]"
tarball="$1"; rom="$2"; pubkey="$3"; shift 3
from=""; cpus=""
from=""; cpus=""; threads=4
while [ $# -gt 0 ]; do
case "$1" in
--from) from="$2"; shift 2 ;;
--cpus) cpus="$2"; shift 2 ;;
--threads) threads="$2"; shift 2 ;;
*) die "unknown option $1" ;;
esac
done
@ -51,6 +55,7 @@ base="$(basename "$tarball")"; version="${base#flybrain-}"; version="${version%.
[[ "$version" =~ ^[A-Za-z0-9._-]+$ ]] || die "cannot parse a version from $base"
[[ "$from" =~ ^[0-9A-Fa-f.:,/*]*$ ]] || die "--from takes addresses or patterns only"
[[ "$cpus" =~ ^[0-9,-]*$ ]] || die "--cpus takes a CPU list"
[[ "$threads" =~ ^[1-9][0-9]?$ ]] || die "--threads takes a number"
key="$(head -n 1 "$pubkey")"
[[ "$key" =~ ^ssh-(ed25519|rsa)\ [A-Za-z0-9+/=]+( [^\"]*)?$ ]] || die "$pubkey is not one ssh public key line"
command -v sshd >/dev/null || [ -x /usr/sbin/sshd ] || die "this box has no sshd"
@ -98,11 +103,12 @@ log "ROM sha256 $rom_sha (must equal the container's ROM_SHA256)"
# The units.
install -m 0644 "$here/flyshadow-remote.service" /etc/systemd/system/flyshadow-remote.service
install -m 0644 "$here/flyshadow-remote.path" /etc/systemd/system/flyshadow-remote.path
if [ -n "$cpus" ]; then
install -d -m 0755 /etc/systemd/system/flyshadow-remote.service.d
printf '[Service]\nAllowedCPUs=%s\n' "$cpus" > /etc/systemd/system/flyshadow-remote.service.d/cpuset.conf
log "AllowedCPUs=$cpus"
fi
{
printf '# written by fly-shadow-remote-setup\n[Service]\nEnvironment=FLY_SHADOW_THREADS=%s\n' "$threads"
[ -n "$cpus" ] && printf 'AllowedCPUs=%s\n' "$cpus"
} > /etc/systemd/system/flyshadow-remote.service.d/10-cores.conf
log "${threads} threads${cpus:+ on CPUs $cpus}"
systemctl daemon-reload
systemctl enable --now flyshadow-remote.path
systemctl enable flyshadow-remote.service

View file

@ -610,9 +610,12 @@ pct exec <ctid> -- /opt/fly/bin/fly-shadow-run stop --restart-flysim # shadow,
ends.
- **Resources**: the shadow is a second whole brain, at about the live fly's CPU per frame.
Remote, it runs on the box's own cores at normal priority (`flyshadow-remote.service`), and the
container pays the relay: a few ms of CPU a second, about 2.1 GB an hour over the network
(the hot save every 5 s is 2.7 MB; the trace about 160 MB an hour), and the trace writer inside
flysim. `--local` runs it `SCHED_IDLE` on the page and encoder CPUs, never on flysim's (05-deploy.sh
container pays the relay (about 0.5 % of a core, measured; about 2.1 GB an hour over the network:
the hot save every 5 s is 2.7 MB, the trace about 140 MB an hour) and the trace writer inside
flysim: about 1.3 ms of the sim thread a frame (measured unpaced on a build box's Haswell, 3
threads: 11.1 to 12.5 ms). The guard's baseline is taken before the trace is on, so that cost
counts against it; if the guard trips on a remote run, the trace alone is too much for the live
fly's headroom. `--local` runs it `SCHED_IDLE` on the page and encoder CPUs, never on flysim's (05-deploy.sh
writes its `cpuset.conf`), pausing for a minute while flysim's lag grows faster than its
baseline rate. Disk on the container: the trace until the box has compared it (under 8 GiB);
on the box: the mirrored saves (under 3 GiB) and a spool of at most 4 GiB.
@ -641,7 +644,7 @@ on the build box can reach the release container: the container dials out.
The container needs an ssh client (`openssh-client`; `02-base.sh` installs it) and no sshd.
2. **The box** (as root on the box): copy the release tarball the container runs, the cartridge,
`infra/box/` and the public key there, then
`infra/box/fly-shadow-remote-setup flybrain-<version>.tar.gz <rom> id_ed25519.pub --from <container address> --cpus <3 of the box's cores>`.
`infra/box/fly-shadow-remote-setup flybrain-<version>.tar.gz <rom> id_ed25519.pub --from <container address> --cpus <its cores> --threads <one per core>`.
It installs the release at the same `/opt/fly/releases/<version>` path (MANIFEST-verified,
root-owned), a `flyshadow` user whose home is the mirror `/srv/fly-shadow-remote`, a
root-owned `authorized_keys` whose only line is
@ -660,10 +663,14 @@ on the build box can reach the release container: the container dials out.
differ from the box's (another release on the container: re-run step 2 with its tarball);
`journalctl -u flyshadow` on the container shows it.
Which box: one whose cores the live fly does not share, preferably on the other NUMA node or
another host (flysim is memory-bandwidth-bound). Keep other heavy work off the shadow's cores for
the 3 h (the shadow runs at `CPUWeight=1000`, `Nice=-5`). The shadow must run faster than real
time to catch up after a restart: measured in the SHADOW-02 report.
Which box: never one on the live fly's NUMA node (flysim is memory-bandwidth-bound: in the
SHADOW-02 rehearsal an unpaced flysim on a neighbouring box of the same node took a paced one from
1.0 to 0.68-0.88). The shadow must also run clearly faster than real time (16.7 ms a frame) to
catch up after a restart; measured per frame (mean, unthrottled replay): 11.8 ms on the second
host's box with 4 threads, 11.9-12.2 ms on the build boxes of the release host's other node with
6 threads (13-14 ms with 3-4). Give it 4 cores (second host) or all 6 (same host, other node)
with `--cpus` and `--threads`; keep other heavy work (bx) off that box
for the 3 h (the shadow runs at `CPUWeight=1000`, `Nice=-5`).
## Cutover to the session runtime (CUT-01)