[Unit]
# SuperDirt / SuperCollider as a managed unit rather than "a thing running in a
# terminal somewhere".
#
# Why (2026-07-27): an evening was lost to silent orbits, and the one place the
# real error was printed — sclang's post window — was a bare /dev/pts/3. Not
# readable by tooling, not readable after the terminal closes, gone on a crash.
# Meanwhile the process itself was fragile: owned by a terminal, killable by a
# stray probe, unrestartable without PLN's hands.
#
# As a unit we get, for free:
#   * `systemctl --user restart parvagues-sc` — safe, repeatable, scriptable
#   * the post window in the JOURNAL: `journalctl --user -u parvagues-sc`
#     so the boot log can be PARSED into readiness state (midi claimed,
#     synthdefs compiled, sample banks registered) instead of squinted at
#   * survives terminal close, sandbox teardown, and SSH logout
#   * one obvious place for The Bridge to show status and offer restart
#
# QT_QPA_PLATFORM=offscreen is load-bearing, not cosmetic: a Qt-linked sclang
# dies with the display stack, which is how an overnight suspend used to kill
# the music (fixed in d834667). Headless sclang has no such dependency.
Description=ParVagues SuperDirt (sclang start_and_midi.scd, headless)
After=pipewire.service
Wants=pipewire.service
# Bound the restart loop (see Restart= below): three tries in five minutes, then stay
# down and stay loud. This is what makes recovery safe without flapping.
StartLimitIntervalSec=300
StartLimitBurst=3

[Service]
Type=simple
WorkingDirectory=/home/pln/Work/Sound/Tidal
Environment=QT_QPA_PLATFORM=offscreen
# scsynth wants a real-time-capable environment; systemd's default limits are
# lower than a login shell's.
LimitRTPRIO=95
LimitMEMLOCK=infinity
LimitNICE=-20
ExecStart=/usr/bin/sclang start_and_midi.scd
# Deliberately NOT Restart=always: if the boot script fails, a restart loop
# would spam the MIDI graph and hide the failure. Fail loudly, restart on
# purpose. Audio gear should never flap.
#
# 2026-08-01 — that reasoning still holds, so this is on-failure (not always) and
# bounded by StartLimitBurst above: it recovers from a CRASH, and a boot script that
# is genuinely broken still fails loudly after three tries instead of flapping.
#
# Note what this does NOT fix. On 2026-08-01 suspend killed *scsynth* while sclang
# stayed perfectly alive, so the unit never failed and no Restart= setting of any
# kind would have fired. Recovery from that has to watch the audio server itself:
# parvagues-sc-watchdog.service, tools/sc-watchdog.sh.
Restart=on-failure
RestartSec=3
# Give the post window to the journal, tagged so it is easy to filter.
StandardOutput=journal
StandardError=journal
SyslogIdentifier=parvagues-sc

[Install]
WantedBy=default.target
