grouproll: depart simulation audits the agent's own drain at terminate
The terminate audit read only the engine's journal for the finished drain. It now also checks the fake agent's drain state when the edge stopped: this transaction's move, result ok, under the running incarnation. A new mutant seam accepts a drain whose result is not ok (report and pre-action check); only the new audit catches it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+27
−3
internal/grouproll/depart.go+8 −3internal/grouproll/fake_test.go+8 −0internal/grouproll/mutation_test.go+2 −0internal/grouproll/roll.go+1 −0internal/grouproll/sim_test.go+8 −0
Intent
diff --git a/internal/grouproll/depart.go b/internal/grouproll/depart.go| index 8edbf9a23..91a9d6256 100644 |
| --- a/internal/grouproll/depart.go |
| +++ b/internal/grouproll/depart.go |
| @@ -370,8 +370,12 @@ func (e *Engine) drain(ctx context.Context) error { |
| } |
| exit := pyNumber(value["exit"]) |
| incarnation, _ := report["incarnation"].(string) |
| - if exit == nil || *exit != 0 || report["transaction"] != tx || report["step"] != "move" || |
| - report["result"] != "ok" || report["fence"] != tx || !incarnationPattern.MatchString(incarnation) { |
| + finished := exit != nil && *exit == 0 && report["result"] == "ok" |
| + if !finished && e.seams.acceptDrainResult { |
| + finished = true |
| + } |
| + if !finished || report["transaction"] != tx || report["step"] != "move" || |
| + report["fence"] != tx || !incarnationPattern.MatchString(incarnation) { |
| reasons, ok := stringList(report["reasons"]) |
| if report["transaction"] != tx || !ok || len(reasons) == 0 { |
| reasons = []string{Text("drain_report")} |
| @@ -635,7 +639,8 @@ func (e *Engine) preAction(ctx context.Context) error { |
| if fence["state"] != "present" || fence["transaction"] != tx { |
| return e.drainFailed([]string{Text("fence_missing")}, nil) |
| } |
| - if d["state"] != "known" || d["transaction"] != tx || d["step"] != "move" || d["result"] != "ok" { |
| + if d["state"] != "known" || d["transaction"] != tx || d["step"] != "move" || |
| + (d["result"] != "ok" && !e.seams.acceptDrainResult) { |
| return e.drainFailed([]string{Text("drain_void")}, nil) |
| } |
| if !hasDrained || !pyEqual(d["incarnation"], drained["incarnation"]) { |
diff --git a/internal/grouproll/fake_test.go b/internal/grouproll/fake_test.go| index be63c1ec8..2340b3b2c 100644 |
| --- a/internal/grouproll/fake_test.go |
| +++ b/internal/grouproll/fake_test.go |
| @@ -1084,6 +1084,11 @@ type heldFake struct { |
| outcomes []termOutcome |
| settles []termSettle |
| edgesBody func(ref string, body map[string]any) |
| + // drainAtStop and incarnationAtStop are the agent's drain and |
| + // incarnation when the edge stopped (the drain socket goes with it): |
| + // what the depart simulation's terminate audit reads. |
| + drainAtStop map[string]any |
| + incarnationAtStop string |
| } |
| func idleDrain(incarnation string) map[string]any { |
| @@ -1383,6 +1388,9 @@ func (f *fake) heldSession(iid, verb string, a BoxArgs) (r any, err error, ok bo |
| } |
| return map[string]any{"main_pid": int64(4242), "active_state": "active"}, nil, true |
| } |
| + if h.drainState["state"] == "known" { |
| + h.drainAtStop, h.incarnationAtStop = copyArgs(h.drainState), h.incarnation |
| + } |
| e := s["edge"].(map[string]any) |
| e["state"], e["pid"], e["identity"] = "unknown", nil, nil |
| s["health"] = map[string]any{"state": "unhealthy", "code": nil, "error": nil} |
diff --git a/internal/grouproll/mutation_test.go b/internal/grouproll/mutation_test.go| index 43db8ef1d..96cba8e7c 100644 |
| --- a/internal/grouproll/mutation_test.go |
| +++ b/internal/grouproll/mutation_test.go |
| @@ -145,6 +145,8 @@ func TestDepartMutations(t *testing.T) { |
| "unpark while the drain may be moving|unpark while the move runs"}, |
| {"restore desired before the terminate check", func(s *departSim) { s.h.seams.restoreDesiredEarly = true }, |
| "desired raised before the terminate check"}, |
| + {"accept a drain whose result is not ok", func(s *departSim) { s.h.seams.acceptDrainResult = true }, |
| + "terminate without the agent's finished drain"}, |
| } |
| for _, m := range mutants { |
| t.Run(m.name, func(t *testing.T) { |
diff --git a/internal/grouproll/roll.go b/internal/grouproll/roll.go| index dda02c58c..c38e5ab30 100644 |
| --- a/internal/grouproll/roll.go |
| +++ b/internal/grouproll/roll.go |
| @@ -64,6 +64,7 @@ type seams struct { |
| skipSecondSweep bool // no lock sweep after the stop |
| unparkUnknownDrain bool // unknown drain status passes the return |
| restoreDesiredEarly bool // desired restored before the terminate check |
| + acceptDrainResult bool // a drain whose result is not ok passes |
| // The probe's mutants (probe.go). |
| skipFreshness bool // ExitStandby on an observation of any age |
diff --git a/internal/grouproll/sim_test.go b/internal/grouproll/sim_test.go| index c7e3e55d9..c0a0f0431 100644 |
| --- a/internal/grouproll/sim_test.go |
| +++ b/internal/grouproll/sim_test.go |
| @@ -1003,6 +1003,14 @@ func (s *departSim) auditTerminate(args map[string]any) { |
| s.check(s.f.held.fence == s.tx, "terminate without this transaction's fence") |
| s.check(s.emptySweepAfter(at, report), "terminate without an empty second sweep") |
| s.check(s.f.edge(fakeIID)["pid"] == nil, "terminate while the edge runs") |
| + // The agent's own drain, not the engine's journal: this transaction's |
| + // move finished ok, under the incarnation that was running. |
| + d, incarnation := s.f.held.drainAtStop, s.f.held.incarnationAtStop |
| + if d == nil { |
| + d, incarnation = s.f.held.drainState, s.f.held.incarnation |
| + } |
| + s.check(d["state"] == "known" && d["transaction"] == s.tx && d["step"] == "move" && d["result"] == "ok" && |
| + d["incarnation"] == incarnation, "terminate without the agent's finished drain") |
| } |
| func (s *departSim) auditExit() { |