carlos / platform Public

settle: a foreign restart's stop half is not an operator stop (#452 review 1)

Astra's review of the first commit found two holes.

1. Another caller's systemctl restart finishes its stop half and then sits
   as a queued start job: inactive/dead with Job set. settleUnit read that
   as an operator's stop (errUnitStopped), which callers take as 'not
   judged', so whatever the queued start then did went unchecked. The
   comment already said 'nothing pending'; now it is checked. Show reads
   the unit's Job property, and only inactive/dead with no job is a stop.

2. A permission error reading one invocation's /proc/<pid>/exe waived the
   binary check for the rest of the settle, so a foreign restart onto a
   readable OLD build could pass. A new invocation now gets its own exe
   check.

Also: the invocation-change handling moved above the switch, so a failed
unit after a foreign restart carries the 'restarted by something other than
this pass' note too; the crash-loop test now includes the RestartSec wait
(old invocation, NRestarts 0) before the bumped counter; spec §2.3 amended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary

Only inactive/dead with no pending Job is an operator stop; each new invocation gets its own exe check; spec amended.
claude agent, operated by paul@keymail.dev 776d3748fd96098e3ff1f8be888f0f6e43fefdc8 parent 9156bc8
4 files changed, +91 −17
  • docs/superpowers/specs/2026-10-06-console-release-defences-design.md +12 −0
  • internal/host/agent_test.go +1 −1
  • internal/host/unitsettle.go +20 −13
  • internal/host/unitsettle_test.go +58 −3
diff --git a/docs/superpowers/specs/2026-10-06-console-release-defences-design.md b/docs/superpowers/specs/2026-10-06-console-release-defences-design.md
index f331980eb..f64085c26 100644
--- a/docs/superpowers/specs/2026-10-06-console-release-defences-design.md
+++ b/docs/superpowers/specs/2026-10-06-console-release-defences-design.md
@@ -149,6 +149,18 @@ named-unit arm waits for the unit to **settle**:
- **Failed** at the deadline if the unit never satisfied "healthy". That
includes a process that is still running but never became ready.
+**Amended for discussion #452 (2026-10-27).** A further invocation with
+`NRestarts` still 0 was not started by systemd's `Restart=`. Something else
+restarted the unit, such as an operator or an app's own supervisor reacting
+to the live link moving. That is not a failure. The check moves onto the new
+invocation and applies the same proof for the rest of the window: active,
+the exe in the new version's directory, and readiness. When there is no
+probe, the Hold starts again in the new invocation. `inactive`/`dead` counts
+as an operator's stop only when the unit has no pending `Job`. Another
+caller's restart passes through that state with its start job queued. Every
+failure after such a restart says the unit was "restarted N time(s) by
+something other than this pass, not by systemd's Restart=".
+
A unit that was **not running** before the restart (an operator stopped it)
is not checked: `try-restart` started nothing, and that stays a no-op.
diff --git a/internal/host/agent_test.go b/internal/host/agent_test.go
index bb956e1b8..153319965 100644
--- a/internal/host/agent_test.go
+++ b/internal/host/agent_test.go
@@ -957,7 +957,7 @@ func TestAdoptPassStartsTheNamedUnitAgainAfterAFailedRestart(t *testing.T) {
}
// The checked arm's show comes first (its empty fake answer verifies no
// binding, so the restart falls back to the plain try-restart).
- if want := "show console.service -p Id -p FragmentPath -p DropInPaths -p ActiveState -p SubState -p InvocationID -p MainPID -p NRestarts -p ExecStart -p ActiveEnterTimestamp\ntry-restart console.service\nstart console.service\n"; string(got) != want {
+ if want := "show console.service -p Id -p FragmentPath -p DropInPaths -p ActiveState -p SubState -p InvocationID -p MainPID -p NRestarts -p ExecStart -p ActiveEnterTimestamp -p Job\ntry-restart console.service\nstart console.service\n"; string(got) != want {
t.Fatalf("systemctl calls = %q, want %q — the rolled-back unit must be STARTED (try-restart would no-op on it)", got, want)
}
if v, err := release.LiveVersion(optDir, "console.test"); err != nil || v != "old" {
diff --git a/internal/host/unitsettle.go b/internal/host/unitsettle.go
index 04d5def91..7ad0e5e3c 100644
--- a/internal/host/unitsettle.go
+++ b/internal/host/unitsettle.go
@@ -41,6 +41,7 @@ type unitProps struct {
MainPID int
NRestarts int
ExecPath string // ExecStart's executable path
+ Job string // the unit's pending job id; "" when none is queued or running
ActiveEnterTimestamp time.Time // when the unit last entered active; zero when unknown
}
@@ -59,7 +60,7 @@ type systemdUnits struct{}
func (systemdUnits) Show(unit string) (unitProps, error) {
out, err := exec.Command("systemctl", "show", unit,
- "-p", "Id", "-p", "FragmentPath", "-p", "DropInPaths", "-p", "ActiveState", "-p", "SubState", "-p", "InvocationID", "-p", "MainPID", "-p", "NRestarts", "-p", "ExecStart", "-p", "ActiveEnterTimestamp").Output()
+ "-p", "Id", "-p", "FragmentPath", "-p", "DropInPaths", "-p", "ActiveState", "-p", "SubState", "-p", "InvocationID", "-p", "MainPID", "-p", "NRestarts", "-p", "ExecStart", "-p", "ActiveEnterTimestamp", "-p", "Job").Output()
if err != nil {
return unitProps{}, fmt.Errorf("systemctl show %s: %w", unit, err)
}
@@ -141,6 +142,8 @@ func parseShow(out string) unitProps {
}
case "ActiveEnterTimestamp":
p.ActiveEnterTimestamp = parseSystemdTime(v)
+ case "Job":
+ p.Job = v // "Job=" when nothing is queued
}
}
return p
@@ -226,10 +229,15 @@ func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) err
// Inactive with SubState dead and nothing pending is a STOP: a crash
// reads failed, and a crash systemd will restart reads activating
// (auto-restart). try-restart blocks until its start job is done, so
- // this is not the gap between the stop and start halves of the
+ // this is not the gap between the stop and start halves of OUR
// restart either. Somebody stopped the unit on purpose, before or
// during this check; the caller must neither judge nor restart it.
- if beforeInvocation != "" && p.ActiveState == "inactive" && p.SubState == "dead" {
+ //
+ // "Nothing pending" is checked, not assumed: somebody ELSE's restart
+ // (#452) finishes its stop half and then sits as a queued start job,
+ // inactive/dead with Job set. That is a restart in flight; keep
+ // polling and judge whatever it starts.
+ if beforeInvocation != "" && p.ActiveState == "inactive" && p.SubState == "dead" && p.Job == "" {
return errUnitStopped
}
if beforeInvocation == "" && p.ActiveState == "inactive" {
@@ -239,21 +247,20 @@ func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) err
if p.InvocationID != beforeInvocation && p.InvocationID != "" && p.NRestarts > 0 {
return notUp(fmt.Sprintf("systemd restarted it %d time(s)", p.NRestarts))
}
- switch {
- case p.ActiveState == "failed":
- return notUp("unit entered failed state")
- case inv != "" && p.InvocationID != inv:
+ if inv != "" && p.InvocationID != inv {
// Not systemd's Restart=: a fresh invocation carrying NRestarts
// returned above. Somebody else restarted the unit (#452). Watch
// the new invocation instead, from scratch: Hold is "active this
- // long in ONE invocation", so the clock starts again, and the
- // exe and Ready checks below judge the process now running.
+ // long in ONE invocation", so its clock starts again (the case
+ // below re-fixes inv once it is active), and the new process
+ // gets its own exe check, whatever the last one's /proc allowed.
foreign++
log.Printf("adopt: %s: restarted by something other than this pass (invocation %q, ours %q); still checking the new build", unit, p.InvocationID, inv)
- inv, activeSince = "", time.Time{}
- if p.ActiveState == "active" && p.InvocationID != "" {
- inv, activeSince = p.InvocationID, o.Now()
- }
+ inv, activeSince, binCheckSkipped = "", time.Time{}, false
+ }
+ switch {
+ case p.ActiveState == "failed":
+ return notUp("unit entered failed state")
case inv != "" && p.NRestarts > 0:
return notUp(fmt.Sprintf("systemd restarted it %d time(s)", p.NRestarts))
case inv == "" && p.ActiveState == "active" && p.InvocationID != beforeInvocation:
diff --git a/internal/host/unitsettle_test.go b/internal/host/unitsettle_test.go
index c651d2b5a..d5ecd81e4 100644
--- a/internal/host/unitsettle_test.go
+++ b/internal/host/unitsettle_test.go
@@ -82,6 +82,12 @@ func opts(ready func() error) settleOpts {
}
func TestParseShow(t *testing.T) {
+ if p := parseShow("ActiveState=inactive\nSubState=dead\nJob=4711\n"); p.Job != "4711" {
+ t.Fatalf("parseShow Job = %q, want 4711", p.Job)
+ }
+ if p := parseShow("ActiveState=inactive\nSubState=dead\nJob=\n"); p.Job != "" {
+ t.Fatalf("parseShow empty Job = %q, want none", p.Job)
+ }
p := parseShow("ActiveState=active\nSubState=running\nInvocationID=abc\nMainPID=42\nNRestarts=0\n" +
"ExecStart={ path=/opt/carlos/live/platform.example.com ; argv[]=/opt/carlos/live/platform.example.com serve ; ignore_errors=no }\n")
if p.ActiveState != "active" || p.SubState != "running" || p.InvocationID != "abc" || p.MainPID != 42 ||
@@ -634,14 +640,63 @@ func TestSettleCrashLoopAfterAForeignRestartStillFails(t *testing.T) {
f := &fakeUnits{shows: []unitProps{
{ActiveState: "active", InvocationID: "ours", MainPID: 7},
{ActiveState: "active", InvocationID: "theirs", MainPID: 8},
- {ActiveState: "activating", SubState: "auto-restart", InvocationID: "crash", NRestarts: 1},
+ // RestartSec's wait: still the crashed invocation, counter not yet bumped.
+ {ActiveState: "activating", SubState: "auto-restart", InvocationID: "theirs"},
+ {ActiveState: "activating", SubState: "start", InvocationID: "crash", NRestarts: 1},
}, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}}
var nu *release.UnitNotUpError
err := settleUnit(f, "titogo-canary@x.service", "old", opts(func() error { return errors.New("refused") }))
if !errors.As(err, &nu) || !strings.Contains(nu.Reason, "systemd restarted it 1 time(s)") {
t.Fatalf("settle = %v, want the crash-restart caught", err)
}
- if f.n != 3 {
- t.Fatalf("settle took %d polls, want it to fail on the 3rd (no waiting out a crash)", f.n)
+ if f.n != 4 {
+ t.Fatalf("settle took %d polls, want it to fail on the 4th (no waiting out a crash)", f.n)
+ }
+}
+
+// Astra review 1, finding 1: another caller's `systemctl restart` finishes
+// its stop half and then waits as a queued start job, so a poll can land on
+// inactive/dead with a job pending. That is a restart in flight, not an
+// operator's stop: the check waits for the start and judges it.
+func TestSettleWaitsOutTheStopHalfOfAForeignRestart(t *testing.T) {
+ f := &fakeUnits{shows: []unitProps{
+ {ActiveState: "active", SubState: "running", InvocationID: "ours", MainPID: 7},
+ {ActiveState: "inactive", SubState: "dead", InvocationID: "ours", Job: "4711"},
+ {ActiveState: "active", SubState: "running", InvocationID: "theirs", MainPID: 8},
+ }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}}
+ if err := settleUnit(f, "titogo-canary@x.service", "old", opts(readyAfter(1))); err != nil {
+ t.Fatalf("settle = %v, want healthy once the queued start comes up on the new build", err)
+ }
+}
+
+// ...and the same gap followed by a start that fails is a failure, not a
+// stop nobody judges. The message says the extra invocation was not ours.
+func TestSettleStopHalfOfAForeignRestartThenFailed(t *testing.T) {
+ f := &fakeUnits{shows: []unitProps{
+ {ActiveState: "active", SubState: "running", InvocationID: "ours", MainPID: 7},
+ {ActiveState: "inactive", SubState: "dead", InvocationID: "ours", Job: "4711"},
+ {ActiveState: "failed", SubState: "failed", InvocationID: "theirs"},
+ }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo"}}
+ var nu *release.UnitNotUpError
+ err := settleUnit(f, "titogo-canary@x.service", "old", opts(readyAfter(1)))
+ if errors.Is(err, errUnitStopped) || !errors.As(err, &nu) || !strings.Contains(nu.Reason, "unit entered failed state") ||
+ !strings.Contains(nu.Reason, "restarted 1 time(s) by something other than this pass") {
+ t.Fatalf("settle = %v, want a failed-state UnitNotUpError noting the foreign restart", err)
+ }
+}
+
+// Astra review 1, finding 2: a permission error on one invocation's exe
+// must not waive the binary check for the next. A foreign restart onto the
+// old build, whose exe is readable, is caught.
+func TestSettleForeignRestartGetsItsOwnExeCheck(t *testing.T) {
+ f := &fakeUnits{shows: []unitProps{
+ {ActiveState: "active", InvocationID: "ours", MainPID: 7},
+ {ActiveState: "active", InvocationID: "theirs", MainPID: 8},
+ }, exeErr: map[int]error{7: &fs.PathError{Op: "readlink", Path: "/proc/7/exe", Err: syscall.EACCES}},
+ exe: map[int]string{8: "/opt/carlos/versions/old/titogo"}}
+ var nu *release.UnitNotUpError
+ err := settleUnit(f, "titogo-canary@x.service", "old", opts(readyAfter(1)))
+ if !errors.As(err, &nu) || !strings.Contains(nu.Reason, "running /opt/carlos/versions/old/titogo, not the new build") {
+ t.Fatalf("settle = %v, want the old build in the foreign invocation rejected", err)
}
}