diff --git a/infra/05-deploy.sh b/infra/05-deploy.sh index c10d480..09ecea6 100644 --- a/infra/05-deploy.sh +++ b/infra/05-deploy.sh @@ -386,6 +386,28 @@ The 'current' symlink has NOT been moved; the running release is untouched." fi fi + # The session runtime (SERVE-01). While `fly-runtime session` has pointed flysim.service at + # flysim-session (the drop-in below), a release without that binary would leave the fly + # unable to start at all, and one whose session string differs from flysim's would write + # checkpoints the other runtime refuses. Refused here, before the symlink moves; to deploy + # such a release anyway, `fly-runtime legacy` first. + if ct_exec "$CTID" -- test -f /etc/systemd/system/flysim.service.d/10-runtime.conf; then + ct_exec "$CTID" -- test -x "${release_path}/flysim-session" \ + || die "05-deploy: REFUSING to deploy release ${version}: flysim.service runs the session runtime (fly-runtime session) and this release has no flysim-session. Run \`fly-runtime legacy\` in the container first, or deploy a release that ships it. The 'current' symlink has NOT been moved." + session_compat="$(ct_exec "$CTID" -- env \ + "FLY_GAME=${GAME}" \ + "FLY_DATASET=${release_path}/data/fafb-v783" \ + "FLY_ROM=/srv/fly/rom/${ROM_SHA256:-none}.gb" \ + "${release_path}/flysim-session" --print-compatibility 2>/dev/null | tr -d '\r' || true)" + if [ -n "${new_compat:-}" ] && [ "$session_compat" != "$new_compat" ]; then + die "05-deploy: REFUSING to deploy release ${version}: flysim.service runs the session runtime and its compatibility string differs from flysim's. + flysim: ${new_compat} + flysim-session: ${session_compat} +The 'current' symlink has NOT been moved." + fi + log "05-deploy: flysim.service runs the session runtime; this release ships flysim-session with flysim's compatibility string" + fi + log "05-deploy: flipping /opt/fly/current -> ${release_path} atomically" ct_exec "$CTID" -- sh -c "ln -sfn '$release_path' /opt/fly/current.tmp && mv -T /opt/fly/current.tmp /opt/fly/current" else @@ -676,9 +698,11 @@ if [ -n "${CPUSET:-}" ]; then tmp_dropin="$(mktemp)" # flyedge is off by default, but its drop-in is written with the rest so that the day # it is enabled it serves the page from the page's CPUs, never from flysim's. - for u in flysim xvfb flystage flystage-web flycast pulse mediamtx flyedge; do + # flysim-session (SERVE-01) is the session runtime's standalone unit: flysim's cores. + # (`fly-runtime session` runs it AS flysim.service, which already has them.) + for u in flysim xvfb flystage flystage-web flycast pulse mediamtx flyedge flysim-session; do case "$u" in - flysim) cpus="$sim_cpus" ;; + flysim|flysim-session) cpus="$sim_cpus" ;; flycast) cpus="$encoder_cpus" ;; *) cpus="$page_cpus" ;; esac @@ -707,7 +731,7 @@ fi # --------------------------------------------------------------------------- log "05-deploy: converging bin/ helpers to /opt/fly/bin" ct_exec "$CTID" -- mkdir -p /opt/fly/bin -for name in fly-watchdog fly-loop-recover fly-loop-reset fly-recap fly-retention fly-reset-to-milestone flypush flystage-launch flycast-launch wait-for-x wait-for-stage wait-for-health; do +for name in fly-watchdog fly-loop-recover fly-loop-reset fly-recap fly-retention fly-reset-to-milestone fly-runtime flypush flystage-launch flycast-launch wait-for-x wait-for-stage wait-for-health; do converge_file "$CTID" "$INFRA_DIR/bin/$name" "/opt/fly/bin/$name" 0755 root:root >/dev/null done diff --git a/infra/bin/fly-runtime b/infra/bin/fly-runtime new file mode 100755 index 0000000..e297267 --- /dev/null +++ b/infra/bin/fly-runtime @@ -0,0 +1,206 @@ +#!/usr/bin/env bash +# infra/bin/fly-runtime — which runtime the live fly runs: the legacy loop (`flysim`) or the +# session runtime (`flysim-session`, SERVE-01). +# +# Usage (INSIDE the container, as root): +# fly-runtime status print `legacy` or `session`, and what flysim.service runs +# fly-runtime session [--no-restart] [--no-fallback] +# fly-runtime legacy [--no-restart] +# +# CUT-01 calls `fly-runtime session` after its zero-divergence shadow; `fly-runtime legacy` is +# the one-command rollback (port decisions, 2026-09-23). Neither migrates any data: both +# binaries read and write the same FLYSIM01 stores, event log, sugar journal and chat sidecar. +# +# How the switch works. flysim.service stays the fly's one unit name, and the runtime it runs +# is chosen by one drop-in, ${DROPIN}: +# +# [Service] +# ExecStart= +# ExecStart=/opt/fly/current/flysim-session +# Environment=FLY_SESSION_DIR=/run/fly/session +# +# i.e. flysim-session.service's ExecStart and its one extra variable (the standalone unit and +# this drop-in are kept identical by infra/tests/lint.sh). Everything else that names +# flysim.service -- fly.target, flyedge.service's Requires=, fly-watchdog's checks and escalation, +# fly-loop-recover's `sudo systemctl restart flysim.service` and its sudoers line, +# fly-loop-reset, fly-reset-to-milestone, the unstick rule, `journalctl -u flysim`, the cpuset +# drop-in -- therefore keeps working unchanged, a reboot keeps the choice, and 05-deploy.sh (which +# converges unit files but never removes a drop-in) keeps it across deploys. 05-deploy.sh refuses +# a release without flysim-session while the drop-in is present. +# +# `session` refuses unless the release has flysim-session and its --print-compatibility equals +# flysim's (the store is shared, so the two must restore each other's checkpoints). After the +# restart it waits for /healthz (FLY_RUNTIME_HEALTH_TIMEOUT, default 300 s: the connectome load and +# the restore) and a snapshot that has advanced; if the session runtime does not come up healthy +# it switches back to legacy by itself and exits 1 (--no-fallback leaves it to the operator). +# +# Every switch is logged to the journal (tag fly-runtime) and appended to ${RUNTIME_LOG}. +set -euo pipefail + +: "${FLY_RELEASE_DIR:=/opt/fly/current}" +: "${FLY_ENV_FILE:=/etc/fly/fly.env}" +: "${FLY_SERVICE:=flysim.service}" +: "${FLY_RUNTIME_HEALTH_TIMEOUT:=300}" +: "${FLY_CONTROL_URL:=http://127.0.0.1:7401}" +: "${FLY_SYSTEMD_DIR:=/etc/systemd/system}" +: "${FLY_RUNTIME_LOG:=/var/lib/fly/runtime.log}" +DROPIN_DIR="${FLY_SYSTEMD_DIR}/${FLY_SERVICE}.d" +DROPIN="${DROPIN_DIR}/10-runtime.conf" +RUNTIME_LOG="$FLY_RUNTIME_LOG" +SESSION_BIN="${FLY_RELEASE_DIR}/flysim-session" +LEGACY_BIN="${FLY_RELEASE_DIR}/flysim" +SESSION_DIR=/run/fly/session + +log() { echo "fly-runtime: $*" >&2; } +die() { log "$*"; exit 1; } + +usage() { + echo "usage: fly-runtime status | session [--no-restart] [--no-fallback] | legacy [--no-restart]" >&2 + exit 2 +} + +current() { + if [ -f "$DROPIN" ]; then echo session; else echo legacy; fi +} + +record() { + local line + line="$(date -u '+%Y-%m-%dT%H:%M:%SZ') $*" + if command -v logger >/dev/null 2>&1; then + logger -t fly-runtime -- "$*" || true + fi + mkdir -p "$(dirname "$RUNTIME_LOG")" 2>/dev/null || true + echo "$line" >>"$RUNTIME_LOG" 2>/dev/null || log "could not append to $RUNTIME_LOG" +} + +# The compatibility string a binary would write, with the service's own environment: fly.env is +# systemd's EnvironmentFile syntax, read as KEY=VALUE lines rather than sourced as shell. +compat_of() { + local -a vars=() + local line key value + if [ -f "$FLY_ENV_FILE" ]; then + while IFS= read -r line || [ -n "$line" ]; do + [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || continue + key="${line%%=*}" + value="${line#*=}" + value="${value%\"}" + value="${value#\"}" + vars+=("${key}=${value}") + done <"$FLY_ENV_FILE" + fi + env "${vars[@]}" "$1" --print-compatibility 2>/dev/null | tr -d '\r' +} + +write_dropin() { + mkdir -p "$DROPIN_DIR" + local tmp="${DROPIN}.tmp.$$" + cat >"$tmp" </dev/null; then + local frame + frame="$(curl -fsS "${FLY_CONTROL_URL}/status" 2>/dev/null \ + | sed -n 's/.*"frame":\([0-9][0-9]*\).*/\1/p' | head -n1)" + if [ -n "$frame" ]; then + if [ -z "$first" ]; then + first="$frame" + elif [ "$frame" -gt "$first" ]; then + log "healthy: frame ${first} -> ${frame}" + return 0 + fi + fi + fi + sleep 1 + done + return 1 +} + +restart_and_check() { + systemctl daemon-reload + systemctl reset-failed "$FLY_SERVICE" 2>/dev/null || true + log "restarting ${FLY_SERVICE} on the $(current) runtime" + systemctl restart "$FLY_SERVICE" + healthy +} + +[ $# -ge 1 ] || usage +command="$1"; shift +restart=1 +fallback=1 +for arg in "$@"; do + case "$arg" in + --no-restart) restart=0 ;; + --no-fallback) fallback=0 ;; + *) usage ;; + esac +done + +case "$command" in + status) + current + systemctl show -p ExecStart --value "$FLY_SERVICE" 2>/dev/null | sed -n 's/.*path=\([^ ;]*\).*/ flysim.service runs \1/p' || true + exit 0 + ;; + session|legacy) ;; + *) usage ;; +esac + +[ "$(id -u)" -eq 0 ] || die "run as root (it writes ${DROPIN} and restarts ${FLY_SERVICE})" + +if [ "$command" = session ]; then + [ -x "$SESSION_BIN" ] || die "this release has no ${SESSION_BIN}; the session runtime ships from SERVE-01 on" + [ -x "$LEGACY_BIN" ] || die "no ${LEGACY_BIN}" + legacy_compat="$(compat_of "$LEGACY_BIN")" + session_compat="$(compat_of "$SESSION_BIN")" + [ -n "$session_compat" ] || die "${SESSION_BIN} --print-compatibility printed nothing (see its stderr); not switching" + if [ "$legacy_compat" != "$session_compat" ]; then + die "the two runtimes would write different checkpoints; not switching. + flysim: ${legacy_compat} + flysim-session: ${session_compat}" + fi + before="$(current)" + write_dropin + record "session (was ${before}); compatibility ${session_compat}" + [ "$restart" -eq 1 ] || { systemctl daemon-reload; log "drop-in written; restart ${FLY_SERVICE} to apply"; exit 0; } + if restart_and_check; then + log "flysim.service runs the session runtime" + exit 0 + fi + log "the session runtime did not come up healthy within ${FLY_RUNTIME_HEALTH_TIMEOUT}s" + if [ "$fallback" -eq 1 ]; then + rm -f "$DROPIN" + record "legacy (automatic fallback: the session runtime was not healthy)" + if restart_and_check; then + log "fell back to the legacy runtime; it is healthy" + else + log "fell back to the legacy runtime, and it is NOT healthy either: check journalctl -u ${FLY_SERVICE}" + fi + fi + exit 1 +fi + +# legacy +before="$(current)" +rm -f "$DROPIN" +rmdir "$DROPIN_DIR" 2>/dev/null || true +record "legacy (was ${before})" +[ "$restart" -eq 1 ] || { systemctl daemon-reload; log "drop-in removed; restart ${FLY_SERVICE} to apply"; exit 0; } +if restart_and_check; then + log "flysim.service runs the legacy runtime" + exit 0 +fi +die "the legacy runtime did not come up healthy within ${FLY_RUNTIME_HEALTH_TIMEOUT}s: check journalctl -u ${FLY_SERVICE}" diff --git a/infra/build/build-flysim.sh b/infra/build/build-flysim.sh index 3a56ca7..97d1799 100755 --- a/infra/build/build-flysim.sh +++ b/infra/build/build-flysim.sh @@ -85,6 +85,11 @@ log "building in $crate_dir for target-cpu=haswell (the host is E5-2660 v3, Hasw # without a rebuild. It lands next to OUT_PATH, where package-release.sh # looks for it. RUSTFLAGS="-C target-cpu=haswell" cargo build --release --target "$CARGO_TARGET" --bin fly-edge + # flysim-session (SERVE-01): the live fly on the session runtime, a drop-in for flysim + # that `fly-runtime session` points flysim.service at; and fly-session, its worker program + # (FLY_SESSION_MODE=process only). Same features as flysim, built every time so a release + # can switch runtimes without a rebuild. Both land next to OUT_PATH. + RUSTFLAGS="-C target-cpu=haswell" cargo build --release --target "$CARGO_TARGET" --bin flysim-session --bin fly-session "${features_args[@]}" ) built="${crate_dir}/target/${CARGO_TARGET}/release/flysim" @@ -118,4 +123,12 @@ edge_out="$(dirname "$OUT_PATH")/fly-edge" cp "$edge_built" "$edge_out" chmod 0755 "$edge_out" log "built $edge_out ($(du -h "$edge_out" | cut -f1))" +for extra in flysim-session fly-session; do + extra_built="${crate_dir}/target/${CARGO_TARGET}/release/${extra}" + [ -x "$extra_built" ] || die "expected binary not found after build: $extra_built" + extra_out="$(dirname "$OUT_PATH")/${extra}" + cp "$extra_built" "$extra_out" + chmod 0755 "$extra_out" + log "built $extra_out ($(du -h "$extra_out" | cut -f1))" +done log "next: infra/build/package-release.sh VERSION $OUT_PATH " diff --git a/infra/build/package-release.sh b/infra/build/package-release.sh index 0610db2..32d43e2 100755 --- a/infra/build/package-release.sh +++ b/infra/build/package-release.sh @@ -14,6 +14,10 @@ # fly-edge (the feed-bus edge, mode 0755, when build-flysim.sh # left one beside FLYSIM_BIN; flyedge.service stays # inactive on a release without it) +# flysim-session (the session runtime, SERVE-01, and fly-session, its +# fly-session worker program; mode 0755, when build-flysim.sh left +# them beside FLYSIM_BIN. `fly-runtime session` refuses +# a release without flysim-session) # stage/... (apps/stage's build output) # bridge/... (services/bridge + node_modules) # data/fafb-v783/... (the connectome, from the repo; FLY_DATASET points here) @@ -82,6 +86,15 @@ if [ -x "$EDGE_BIN" ]; then else log "no fly-edge beside $FLYSIM_BIN; packaging without it (FLY_FEED_VIA=bus unavailable in this release)" fi +for extra in flysim-session fly-session; do + EXTRA_BIN="$(dirname "$FLYSIM_BIN")/${extra}" + if [ -x "$EXTRA_BIN" ]; then + cp "$EXTRA_BIN" "${release_dir}/${extra}" + chmod 0755 "${release_dir}/${extra}" + else + log "no ${extra} beside $FLYSIM_BIN; packaging without it (fly-runtime session unavailable in this release)" + fi +done cp -a "$STAGE_DIR" "${release_dir}/stage" cp -a "$BRIDGE_DIR" "${release_dir}/bridge" diff --git a/infra/config/fly-tmpfiles.conf b/infra/config/fly-tmpfiles.conf index 4aafd6a..870e96b 100644 --- a/infra/config/fly-tmpfiles.conf +++ b/infra/config/fly-tmpfiles.conf @@ -9,6 +9,8 @@ d /run/fly/wd 0750 fly fly - # ADDED: the feed bus (FLY_FEED_VIA=bus, docs/design/flybus.md): flysim's # router socket and artifact store. flysim also creates it, 0700, on start. d /run/fly/bus 0700 fly fly - +# The session runtime's router store and worker sockets (SERVE-01, flysim-session). +d /run/fly/session 0700 fly fly - d /var/lib/fly 0750 fly fly - d /var/lib/fly/chrome 0700 fly fly - d /srv/fly/state 0750 fly fly - diff --git a/infra/docs/runbook.md b/infra/docs/runbook.md index a8748a2..bc87c1c 100644 --- a/infra/docs/runbook.md +++ b/infra/docs/runbook.md @@ -443,6 +443,50 @@ One `ln -sfn` plus a restart, per `docs/design/infra.md` section 2. Nothing else touching: `flycast`/`flypush`/`mediamtx` do not read anything under `/opt/fly/current`. On the release container, `` is a tag (e.g. `v0.1.0`) — "Cutting a release" above. +## Switch the runtime (legacy loop or session runtime) + +Two binaries in every release from SERVE-01 on run the same fly: `flysim`, the legacy loop, and +`flysim-session`, the same composition on the session framework (agent, Game Boy environment and +macro task as participants). They read the same `/etc/fly/fly.env`, write the same checkpoint +stores, event log, sugar journal and chat sidecar, and serve the same feed, control API and +metrics through the same listener code, so nothing else on the container can tell them apart. +`flysim.service` is the fly's one unit name either way; which binary it runs is one drop-in, +written and removed by `fly-runtime` (`infra/bin/fly-runtime`, installed to `/opt/fly/bin`). + +``` +pct exec $CTID -- /opt/fly/bin/fly-runtime status # legacy | session +pct exec $CTID -- /opt/fly/bin/fly-runtime session # CUT-01, after the shadow's verdict +pct exec $CTID -- /opt/fly/bin/fly-runtime legacy # the one-command rollback +``` + +- **What it does.** `session` writes `/etc/systemd/system/flysim.service.d/10-runtime.conf` + (`ExecStart=/opt/fly/current/flysim-session`, `FLY_SESSION_DIR=/run/fly/session`), + daemon-reloads and restarts `flysim.service`; `legacy` deletes the file and does the same. No + data moves: the store is shared, and each runtime restores the other's checkpoints. +- **What it checks.** `session` refuses a release without `flysim-session`, and refuses when + `flysim --print-compatibility` and `flysim-session --print-compatibility` differ. After the + restart both wait for `/healthz` and a `/status` frame that advances + (`FLY_RUNTIME_HEALTH_TIMEOUT`, 300 s). If the session runtime does not come up healthy, it + switches back to legacy by itself and exits 1 (`--no-fallback` leaves it to the operator). + Every switch is logged (`journalctl -t fly-runtime`, `/var/lib/fly/runtime.log`). +- **What stays the same.** Everything that names `flysim.service` keeps working unchanged: + `fly.target`, `flyedge.service`'s `Requires=`, the watchdog, `fly-loop-recover`'s restart and + its sudoers line, `fly-loop-reset`, `fly-reset-to-milestone` (run with the service stopped, on + the shared store), the unstick rule's `systemctl restart flysim.service`, `journalctl -u + flysim`, the cpuset drop-in. The choice survives a reboot and a deploy: `05-deploy.sh` + converges unit files but never removes a drop-in, and it refuses a release without + `flysim-session` while the drop-in is present (`fly-runtime legacy` first to deploy one). +- **What differs, by declaration.** `POST /reward` is always 403 on the session runtime (the + operator pulse has no session-framework counterpart, `legacy-gameboy-v1` section 15); a + fresh start publishes no audio for its one setup frame; `FLY_TRACE` and + `FLY_PROFILE_SECONDS` are legacy-loop tools (the session runtime logs a `session profile` + line of per-phase timings every minute instead). The sugar journal's boot header says + `runtime: fly-session`. +- **The standalone unit.** `flysim-session.service` is the same service as a unit of its own + (flysim's limits, cpuset and environment; `Conflicts=flysim.service`; no `[Install]`, so it + cannot be enabled). It is for a rehearsal or a soak on a container whose `flysim.service` is + stopped, not for switching the stream. + ## CPU partition (cpuset) `05-deploy.sh` derives the in-guest `AllowedCPUs=` drop-ins for every app unit diff --git a/infra/tests/lint.sh b/infra/tests/lint.sh index 82e9897..c364f22 100755 --- a/infra/tests/lint.sh +++ b/infra/tests/lint.sh @@ -533,6 +533,152 @@ else pass "flysim.service leaves FLY_FEED_VIA to fly.env" fi +# --------------------------------------------------------------------------- +# 3b3. The session runtime (SERVE-01): flysim-session, its standalone unit and +# the fly-runtime switch CUT-01 calls. +# +# flysim.service stays the fly's one unit name; `fly-runtime session` points it at +# flysim-session with a drop-in, `fly-runtime legacy` removes it. What would break +# that: the standalone unit drifting from flysim.service (limits, env, ports), +# becoming enable-able or pulled in beside flysim, the drop-in naming another +# binary or session dir, the release not shipping the binary, the deploy +# forgetting it. fly-runtime itself is driven for real against stubs. +# --------------------------------------------------------------------------- +echo "--- the session runtime: flysim-session.service and fly-runtime ---" +SESSION_UNIT="$INFRA_DIR/units/flysim-session.service" +if [ ! -f "$SESSION_UNIT" ]; then + fail "units/flysim-session.service is missing" +else + grep -qE '^ExecStart=/opt/fly/current/flysim-session$' "$SESSION_UNIT" \ + && pass "flysim-session.service runs the release's flysim-session" \ + || fail "flysim-session.service ExecStart must be /opt/fly/current/flysim-session" + grep -qE '^Conflicts=flysim\.service$' "$SESSION_UNIT" \ + && pass "flysim-session.service Conflicts=flysim.service (same ports, same stores)" \ + || fail "flysim-session.service must Conflict with flysim.service" + if grep -qE '^\[Install\]' "$SESSION_UNIT"; then + fail "flysim-session.service has an [Install] section; it must not be enable-able beside flysim" + else + pass "flysim-session.service cannot be enabled (no [Install])" + fi + # The [Service] section, comments and blank lines dropped: flysim.service's, line for + # line, but for ExecStart and the one variable of its own. + service_lines() { + awk '/^\[/{sec=$0; next} sec=="[Service]" && !/^[[:space:]]*(#|$)/' "$1" \ + | grep -vE '^(ExecStart=|Environment=FLY_SESSION_DIR=)' || true + } + if [ "$(service_lines "$INFRA_DIR/units/flysim.service")" = "$(service_lines "$SESSION_UNIT")" ]; then + pass "flysim-session.service [Service] is flysim.service's but for ExecStart and FLY_SESSION_DIR" + else + fail "flysim-session.service [Service] drifted from flysim.service: $(diff <(service_lines "$INFRA_DIR/units/flysim.service") <(service_lines "$SESSION_UNIT") | tr '\n' ' ')" + fi + grep -qE '^Environment=FLY_SESSION_DIR=/run/fly/session$' "$SESSION_UNIT" \ + && pass "flysim-session.service keeps its session dir on /run/fly/session" \ + || fail "flysim-session.service must set FLY_SESSION_DIR=/run/fly/session" +fi +if target_pulls "$INFRA_DIR/units/fly.target" | grep -qx 'flysim-session.service'; then + fail "fly.target pulls flysim-session.service in beside flysim.service" +else + pass "fly.target does not pull flysim-session.service in" +fi +if grep -E '^(ALWAYS_ON_UNITS|APP_UNITS)=' "$INFRA_DIR/07-enable.sh" "$INFRA_DIR/verify.sh" | grep -q 'flysim-session'; then + fail "07-enable.sh or verify.sh lists flysim-session.service" +else + pass "07-enable.sh and verify.sh leave flysim-session.service alone" +fi +grep -qE '^d /run/fly/session +0700 fly +fly' "$INFRA_DIR/config/fly-tmpfiles.conf" \ + && pass "tmpfiles creates /run/fly/session 0700 fly" \ + || fail "config/fly-tmpfiles.conf must create /run/fly/session 0700 fly fly" +grep -qE 'for name in .*\bfly-runtime\b.*; do$' "$INFRA_DIR/05-deploy.sh" \ + && pass "05-deploy.sh installs fly-runtime" \ + || fail "05-deploy.sh must converge bin/fly-runtime to /opt/fly/bin" +grep -qE '^[[:space:]]*for u in flysim .*\bflysim-session\b.*; do$' "$INFRA_DIR/05-deploy.sh" \ + && pass "05-deploy.sh writes flysim-session.service's cpuset drop-in" \ + || fail "05-deploy.sh cpuset loop must include flysim-session (flysim's cores)" +grep -qF 'flysim.service.d/10-runtime.conf' "$INFRA_DIR/05-deploy.sh" \ + && grep -qF '"${release_path}/flysim-session"' "$INFRA_DIR/05-deploy.sh" \ + && pass "05-deploy.sh refuses a release without flysim-session while the session runtime runs" \ + || fail "05-deploy.sh must refuse a release without flysim-session while 10-runtime.conf is present" +grep -qE -- '--bin flysim-session' "$INFRA_DIR/build/build-flysim.sh" \ + && grep -qE 'for extra in flysim-session fly-session' "$INFRA_DIR/build/package-release.sh" \ + && pass "build-flysim.sh builds flysim-session and package-release.sh ships it" \ + || fail "build-flysim.sh must build flysim-session (and fly-session) and package-release.sh ship them" + +rt_dir="$(mktemp -d "${TMPDIR:-/tmp}/fly-lint-runtime.XXXXXX")" +mkdir -p "$rt_dir/bin" "$rt_dir/release" "$rt_dir/systemd" +cat > "$rt_dir/bin/systemctl" <<'RTSTUB' +#!/usr/bin/env bash +echo "$*" >> "$RT_DIR/systemctl.log" +RTSTUB +cat > "$rt_dir/bin/id" <<'RTSTUB' +#!/usr/bin/env bash +echo 0 +RTSTUB +cat > "$rt_dir/bin/logger" <<'RTSTUB' +#!/usr/bin/env bash +true +RTSTUB +cat > "$rt_dir/bin/curl" <<'RTSTUB' +#!/usr/bin/env bash +# /healthz answers per RT_HEALTHY; /status reports a frame that advances per call. +url="${*: -1}" +[ "${RT_HEALTHY:-1}" = 1 ] || exit 22 +case "$url" in + */status) n=$(( $(cat "$RT_DIR/frame" 2>/dev/null || echo 100) + 1 )); echo "$n" > "$RT_DIR/frame" + echo "{\"status\":\"running\",\"frame\":$n}" ;; +esac +RTSTUB +for b in flysim flysim-session; do + printf '#!/usr/bin/env bash\necho "${RT_COMPAT_%s:-same}"\n' "$(echo "$b" | tr 'a-z-' 'A-Z_')" > "$rt_dir/release/$b" +done +chmod +x "$rt_dir"/bin/* "$rt_dir"/release/* +echo 'FLY_GAME=pokemon-red' > "$rt_dir/fly.env" +fly_runtime() { + env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_RELEASE_DIR="$rt_dir/release" \ + FLY_ENV_FILE="$rt_dir/fly.env" FLY_SYSTEMD_DIR="$rt_dir/systemd" \ + FLY_RUNTIME_LOG="$rt_dir/runtime.log" FLY_RUNTIME_HEALTH_TIMEOUT=4 \ + "$@" bash "$INFRA_DIR/bin/fly-runtime" "${RT_ARGS[@]}" >/dev/null 2>&1 +} +rt_dropin="$rt_dir/systemd/flysim.service.d/10-runtime.conf" +RT_ARGS=(status) +[ "$(env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_SYSTEMD_DIR="$rt_dir/systemd" bash "$INFRA_DIR/bin/fly-runtime" status 2>/dev/null | head -n1)" = legacy ] \ + && pass "fly-runtime status: legacy with no drop-in" \ + || fail "fly-runtime status must say legacy with no drop-in" +RT_ARGS=(session) +if fly_runtime && [ -f "$rt_dropin" ] \ + && grep -qx "ExecStart=$rt_dir/release/flysim-session" "$rt_dropin" \ + && grep -qx 'ExecStart=' "$rt_dropin" \ + && grep -qx 'Environment=FLY_SESSION_DIR=/run/fly/session' "$rt_dropin" \ + && grep -qx 'restart flysim.service' "$rt_dir/systemctl.log" \ + && [ "$(env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_SYSTEMD_DIR="$rt_dir/systemd" bash "$INFRA_DIR/bin/fly-runtime" status 2>/dev/null | head -n1)" = session ]; then + pass "fly-runtime session: the drop-in names flysim-session and /run/fly/session, flysim.service restarted, healthy" +else + fail "fly-runtime session: drop-in/restart/health wrong ($(cat "$rt_dropin" 2>/dev/null | tr '\n' ' '))" +fi +RT_ARGS=(legacy) +if fly_runtime && [ ! -f "$rt_dropin" ]; then + pass "fly-runtime legacy: the drop-in is gone, flysim.service restarted" +else + fail "fly-runtime legacy must remove the drop-in and succeed" +fi +RT_ARGS=(session) +if ! fly_runtime RT_COMPAT_FLYSIM_SESSION=other && [ ! -f "$rt_dropin" ]; then + pass "fly-runtime session refuses when the two compatibility strings differ" +else + fail "fly-runtime session must refuse differing compatibility strings and write nothing" +fi +if ! fly_runtime RT_HEALTHY=0 && [ ! -f "$rt_dropin" ] && grep -q 'automatic fallback' "$rt_dir/runtime.log"; then + pass "fly-runtime session falls back to legacy by itself when the session runtime is not healthy" +else + fail "fly-runtime session must fall back to legacy (drop-in removed, logged) when unhealthy" +fi +mv "$rt_dir/release/flysim-session" "$rt_dir/flysim-session.away" +if ! fly_runtime && [ ! -f "$rt_dropin" ]; then + pass "fly-runtime session refuses a release without flysim-session" +else + fail "fly-runtime session must refuse a release without flysim-session" +fi +rm -rf "$rt_dir" + echo "--- fly-watchdog check 2: the feed counters follow FLY_FEED_VIA ---" if ! tail -n1 "$INFRA_DIR/bin/fly-watchdog" | grep -qE '^main "\$@"$'; then fail "fly-watchdog: expected the last line to be 'main \"\$@\"' — the check-2 fixture strips it" diff --git a/infra/units/flysim-session.service b/infra/units/flysim-session.service new file mode 100644 index 0000000..9dab5c8 --- /dev/null +++ b/infra/units/flysim-session.service @@ -0,0 +1,53 @@ +# infra/units/flysim-session.service — pushed to /etc/systemd/system/flysim-session.service. +# +# The live fly on the session runtime (SERVE-01): `flysim-session`, a drop-in for `flysim` that +# reads the same /etc/fly/fly.env, writes the same checkpoint stores, event log, sugar journal and +# chat sidecar, and serves the same feed (:7400, or the feed bus for fly-edge), control API +# (:7401) and metrics (:9101) -- through flysim's own listener code, so the binding contracts +# (docs/feed-protocol.md, docs/control-api.md) are the same bytes. +# +# NOT ENABLED, IN NO TARGET, and it Conflicts= with flysim.service: the two own the same ports +# and the same stores, so at most one of them runs. It is not how the stream is switched either. +# `fly-runtime session` (infra/bin/fly-runtime; the runbook's "Switch the runtime") points +# flysim.service itself at this unit's ExecStart and Environment with a drop-in, so that every +# consumer that names flysim.service -- fly.target, flyedge.service's Requires=, the watchdog, +# fly-loop-recover's restart (and its sudoers line), fly-loop-reset, fly-reset-to-milestone, the +# unstick rule, `journalctl -u flysim` -- keeps working unchanged, a reboot keeps the choice, and +# `fly-runtime legacy` is one command back. This unit is the standalone form of the same +# service: for a rehearsal or a soak on a container whose flysim.service is stopped +# (`systemctl stop flysim.service && systemctl start flysim-session.service`). +# +# Everything below except ExecStart, FLY_SESSION_DIR, Conflicts= and the missing [Install] is +# flysim.service's, line for line (infra/tests/lint.sh checks it), and so are the limits and the cpuset: 05-deploy.sh writes +# flysim-session.service.d/cpuset.conf with flysim's set (docs/design/infra.md section 3 and +# flysim.service's own notes on RAYON_NUM_THREADS, Nice, CPUWeight and AllowedCPUs apply as they +# stand). +[Unit] +Description=flysim-session: the fly brain and its Game Boy on the session runtime +After=network.target +Conflicts=flysim.service +StartLimitIntervalSec=300 +StartLimitBurst=5 + +[Service] +Type=notify +NotifyAccess=main +WatchdogSec=30 +User=fly +EnvironmentFile=/etc/fly/fly.env +Environment=FLY_FEED_BIND=127.0.0.1:7400 +Environment=FLY_BUS_DIR=/run/fly/bus +Environment=FLY_CONTROL_BIND=127.0.0.1:7401 +Environment=FLY_METRICS_ADDR=0.0.0.0:9101 +Environment=FLY_STATE_HOT=/run/fly/state +Environment=FLY_STATE=/srv/fly/state +# The session's own router store and worker sockets (tmpfs, fly 0700, config/fly-tmpfiles.conf). +Environment=FLY_SESSION_DIR=/run/fly/session +ExecStart=/opt/fly/current/flysim-session +Restart=always +RestartSec=2 +Nice=-5 +CPUWeight=400 +MemoryMax=4G +# No [Install] section, deliberately: `systemctl enable flysim-session.service` has nothing to +# do, so it can never be pulled in beside flysim.service by fly.target.