Mac slice F task 9 follow-up fix 3: acknowledged, per-marker proofs
Astra's final review of the follow-up, three Important findings: - Proof and acknowledgement are separate. Verify only reads a reboot's completion; Executor.Acknowledge records a consumer, and the completion goes once both have. The step recovery acknowledges after its transaction commits (StepEngine.recoverSteps, and Probe after proveTermination; those step.go lines landed in eda98c02), the Keychain recovery once its watch record is saved as checked or gone (creds.Keychain.Acked). VerifyFor is gone. - Reboot evidence is per marker: Complete takes only the proven marker out of a shared obligation. - The deadline covers the save: Discharge and Complete check the time after d.save. A late discharge writes the debt back; a late completion stays as evidence for the next proof. Tests first: TestRebootCompletionSurvivesACrashBeforeTheCommit, TestRebootEvidencePerMarker, TestDebtsSaveUnderTheDeadline, TestSettledWatchAcknowledgedOnceDurable and TestRecoveryAcknowledgesOnlyAfterItsCommit ran RED first. Mutations G1a to G3c (10) all killed. thin's exemption rises by this round's 23 lines; the parseRecord and settleRecord wrappers left package mac.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+407
−73
.rastrillo/budgets.txt+1 −1docs/superpowers/specs/2026-11-07-equipo-mac-runners.md+24 −10internal/runner/mac/creds/fix9f3_test.go+51 −0internal/runner/mac/creds/keychain.go+13 −1internal/runner/mac/fix9f3_test.go+89 −0internal/runner/mac/fix9f_test.go+9 −4internal/runner/mac/gate.go+2 −2internal/runner/mac/launch.go+20 −21internal/runner/mac/proc/clear.go+55 −20internal/runner/mac/proc/debts.go+9 −5internal/runner/mac/proc/proc_test.go+39 −0internal/runner/thin/mac.go+16 −2internal/runner/thin/mac_ack_test.go+63 −0internal/runner/thin/maccreds.go+14 −6internal/runner/thin/run.go+2 −1
CI cancelled — superseded by a newer push — run details
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 2b6038d2..b1f0c4a3 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -38,7 +38,7 @@ time internal/runner/provider 15s Plan 3 CI repair measured 10.346s for the full |
| perf GET /releases/* skip Verified runner release file downloads, not browser GET screens. |
| perf GET /install.sh skip Mac runners: the one-line bootstrap a terminal pipes to sh, not a browser screen. |
| time internal/runner/containment 15s Plan 3 loaded CI measured 10.817s; |
| -size internal/runner/thin 34396 31329 Protocols 31 to 35, merge credential, publication guards, kept work, scans, Mac pin, paths, bases, endpoint, Mac turns, Mac credentials, worker conns, Engineering, asks, F probes, D1, the fake Mac worker that makes changes; 34396/31329. |
| +size internal/runner/thin 34419 31329 Protocols 31 to 35, merge credential, publication guards, kept work, scans, Mac pin, paths, bases, endpoint, Mac turns, Mac credentials, worker conns, Engineering, asks, F probes, D1, the fake Mac worker that makes changes, reboot acknowledgements; 34419/31329. |
| time internal/thintest 240s Thin contract and page tests, the Assistant's and the models' included, share one harness. Measured 202s on a shared 16 CPU box (2026-11-13); CI runs two lanes. |
| time internal/runner/mac 45s Real processes killed and proven gone: groups, sessions, the runner-user sweep, turns, restarts; probes end to end. Measured 29.3s locally. |
| time internal/runner/thin 45s Thin v1 slice 4 review: real git repositories for the merge and squash checks, a stalled fetch, and a 16 MiB candidate streamed through the review diff and the checks pack (about 5s). |
diff --git a/docs/superpowers/specs/2026-11-07-equipo-mac-runners.md b/docs/superpowers/specs/2026-11-07-equipo-mac-runners.md| index f321158e..60642514 100644 |
| --- a/docs/superpowers/specs/2026-11-07-equipo-mac-runners.md |
| +++ b/docs/superpowers/specs/2026-11-07-equipo-mac-runners.md |
| @@ -1102,11 +1102,22 @@ it: |
| Keychain recovery both prove the same process record, in either |
| order and perhaps in different runner processes. So the reboot's |
| proof is kept on `<lock>.owed` as a completion, keyed by the |
| - attempt and this boot's identity, until every consumer has taken |
| - it. A later proof of the same record finds the completion and |
| - succeeds without checking or signalling anything, across a runner |
| - restart too. A completion that some consumer never takes is dropped |
| - after 30 days. |
| + attempt and this boot's identity. A proof only reads it: a later |
| + proof of the same record finds the completion and succeeds without |
| + checking or signalling anything, across a runner restart too. Each |
| + consumer acknowledges the completion, in a separate step, once its |
| + own result is durable: the step recovery after its transaction |
| + commits (at a restart, or when a later probe records the |
| + termination), and the Keychain recovery once the watch's record is |
| + saved as checked or removed. The completion goes only when both |
| + have acknowledged it, so a crash between a proof and its commit |
| + loses nothing. A completion that some consumer never acknowledges |
| + is dropped after 30 days. |
| + |
| + The evidence is per marker. One turn's obligation can name several |
| + launches (a probe's, or an Engineering operation's). Proving one of |
| + them takes only its marker out of the obligation; the others keep |
| + their reboot evidence, and each is proven on its own. |
| Both clearances run under the attempt's one proof deadline. They remove |
| nothing on their own: what the attempt owes is discharged only once its |
| @@ -1114,11 +1125,14 @@ whole proof, including any cleanup, has succeeded in time. A proof that |
| fails or expires leaves every obligation in place. |
| The deadline covers every step of the proof: the pending user-temp |
| -cleanup (shutting simulators down among it), and the lock on |
| -`<lock>.owed.lock` that the discharge needs. If either cannot finish in |
| -time, the proof fails, and the debt and the pending cleanup stay; a |
| -later proof carries on from there, the cleanup being safe to repeat. |
| -An assessment that cannot open or lock the debts proves nothing. |
| +cleanup (shutting simulators down among it), the lock on |
| +`<lock>.owed.lock` that the discharge needs, and the save itself (its |
| +write, sync and rename). If any of them cannot finish in time, the |
| +proof fails, and the debt and the pending cleanup stay; a later proof |
| +carries on from there, the cleanup being safe to repeat. A discharge |
| +whose save ends late writes the debt back. A reboot's completion saved |
| +late stays: it is the evidence the later proof finishes from. An |
| +assessment that cannot open or lock the debts proves nothing. |
| Otherwise the step stays unterminated (no Retry), and its turn's record |
| stays too. The next turn, in any runner process, settles that record |
diff --git a/internal/runner/mac/creds/fix9f3_test.go b/internal/runner/mac/creds/fix9f3_test.go| new file mode 100644 |
| index 00000000..b87405b8 |
| --- /dev/null |
| +++ b/internal/runner/mac/creds/fix9f3_test.go |
| @@ -0,0 +1,51 @@ |
| +package creds |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "io/fs" |
| + "slices" |
| + "testing" |
| +) |
| + |
| +// Task 9 follow-up, round 3, #1: a watch settled at a restart is |
| +// acknowledged (Keychain.Acked) only once its record is durably checked |
| +// or removed, so the proof of its end it used may go. One whose end is |
| +// not proven, or whose settling could not list the Keychain, is not. |
| +func TestSettledWatchAcknowledgedOnceDurable(t *testing.T) { |
| + ctx := context.Background() |
| + f := &fakeKeychain{} |
| + k := newWatch(t, f) |
| + _ = k.Before(ctx, "signin-1", configDir) |
| + f.add(own()) |
| + restarted := &Keychain{Dir: k.Dir, Login: login, User: "paulequipo", List: f.list, Delete: f.delete} |
| + var acked []string |
| + restarted.Acked = func(w Watch) { |
| + rec, err := restarted.load(w.Marker) |
| + if !errors.Is(err, fs.ErrNotExist) && (err != nil || !rec.Checked) { |
| + t.Errorf("acknowledged %s before its record was settled: %+v %v", w.Marker, rec, err) |
| + } |
| + acked = append(acked, w.Marker) |
| + } |
| + _ = restarted.Settle(ctx, true, func(Watch) bool { return false }) |
| + f.broken = true |
| + _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) |
| + f.broken = false |
| + // The second look lists, but the recheck's listing fails: the record |
| + // is never saved as checked. |
| + n := 0 |
| + restarted.List = func(ctx context.Context, kc string) ([]Item, error) { |
| + if n++; n == 2 { |
| + return nil, errors.New("listing failed") |
| + } |
| + return f.list(ctx, kc) |
| + } |
| + _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) |
| + restarted.List = f.list |
| + if len(acked) != 0 { |
| + t.Fatalf("acknowledged without a settled record: %v", acked) |
| + } |
| + if err := restarted.Settle(ctx, true, func(Watch) bool { return true }); err != nil || !slices.Equal(acked, []string{"signin-1"}) { |
| + t.Fatalf("settled watch: err %v, acknowledged %v", err, acked) |
| + } |
| +} |
diff --git a/internal/runner/mac/creds/keychain.go b/internal/runner/mac/creds/keychain.go| index b11a21ac..a081f3de 100644 |
| --- a/internal/runner/mac/creds/keychain.go |
| +++ b/internal/runner/mac/creds/keychain.go |
| @@ -199,6 +199,9 @@ type Keychain struct { |
| Dir, Login, User string |
| List func(ctx context.Context, keychain string) ([]Item, error) |
| Delete func(context.Context, Item) error |
| + // Acked, if set, is told of each watch Settle proved over, once its |
| + // record is durably checked or gone (the proof is no longer needed). |
| + Acked func(Watch) |
| // op runs one watch operation at a time (a sign-in's second look and |
| // the hourly settling may meet on a record). |
| @@ -456,6 +459,7 @@ func (k *Keychain) Settle(ctx context.Context, restart bool, gone func(Watch) bo |
| } |
| var recs []*record |
| var list []Item |
| + var proven []Watch |
| for _, name := range names { |
| rec, err := k.load(name) |
| if err != nil { |
| @@ -465,7 +469,8 @@ func (k *Keychain) Settle(ctx context.Context, restart bool, gone func(Watch) bo |
| k.mu.Lock() |
| live := k.live[name] |
| k.mu.Unlock() |
| - if !restart || live || gone == nil || !gone(Watch{Marker: rec.Marker, Run: rec.Run, Started: rec.Started}) { |
| + w := Watch{Marker: rec.Marker, Run: rec.Run, Started: rec.Started} |
| + if !restart || live || gone == nil || !gone(w) { |
| continue |
| } |
| if list == nil { |
| @@ -474,6 +479,7 @@ func (k *Keychain) Settle(ctx context.Context, restart bool, gone func(Watch) bo |
| } |
| } |
| rec.Left, rec.Checked = appeared(rec.Before, list), true |
| + proven = append(proven, w) |
| } |
| recs = append(recs, &rec) |
| } |
| @@ -481,6 +487,12 @@ func (k *Keychain) Settle(ctx context.Context, restart bool, gone func(Watch) bo |
| return nil |
| } |
| _, err = k.recheck(ctx, recs) |
| + for _, w := range proven { |
| + // Only a record on disk as checked, or gone, no longer needs it. |
| + if rec, lerr := k.load(w.Marker); k.Acked != nil && (errors.Is(lerr, fs.ErrNotExist) || lerr == nil && rec.Checked) { |
| + k.Acked(w) |
| + } |
| + } |
| return err |
| } |
diff --git a/internal/runner/mac/fix9f3_test.go b/internal/runner/mac/fix9f3_test.go| new file mode 100644 |
| index 00000000..76f03ebb |
| --- /dev/null |
| +++ b/internal/runner/mac/fix9f3_test.go |
| @@ -0,0 +1,89 @@ |
| +package mac |
| + |
| +import ( |
| + "os" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| +) |
| + |
| +// restarted is a new Turns over turns' lock and debts (a runner restart), |
| +// and an executor on it. |
| +func restarted(turns *Turns, x *Executor, tb Procs) (*Turns, *Executor) { |
| + again := NewTurns(turns.lock).Sweep(tb) |
| + again.own, again.Settle, again.boot = turns.own, turns.Settle, turns.boot |
| + return again, &Executor{Turns: again, Settle: x.Settle} |
| +} |
| + |
| +// verifyAs is the proof a consumer's recovery asks for before it |
| +// commits what it found: a read, whoever asks. |
| +func verifyAs(x *Executor, id, _ string) bool { return x.Verify(id) } |
| + |
| +// Round 3, #1: proving a record after a reboot only reads its |
| +// completion. A consumer takes it by acknowledging, once its own result |
| +// is durable: a crash between the step's proof and its commit loses |
| +// nothing, though the Keychain recovery already took its share. |
| +func TestRebootCompletionSurvivesACrashBeforeTheCommit(t *testing.T) { |
| + me := os.Getuid() |
| + tb := parseTable(t, turnBegan) |
| + turns, x, _, before := clearRig(t, tb, laterBoot, nil) |
| + tb.add(row(99400, 1, 99400, 99400, 5000, me, ""), "/Users/paulequipo/w/this-boot") |
| + if err := turns.debts.Owe(owed(before)); err != nil { |
| + t.Fatal(err) |
| + } |
| + id := RecordID(99400, "attempt-c") |
| + if !verifyAs(x, id, ConsumerKeychain) { |
| + t.Fatal("the Keychain recovery's proof failed") |
| + } |
| + x.Acknowledge(id, ConsumerKeychain) |
| + if !verifyAs(x, id, ConsumerStep) { |
| + t.Fatal("the step recovery's proof failed") |
| + } |
| + // The runner dies before the step's transaction commits. |
| + again, y := restarted(turns, x, tb) |
| + if !y.Verify(id) { |
| + t.Fatal("the step's proof was lost to a crash before its commit") |
| + } |
| + y.Acknowledge(id, ConsumerStep) |
| + if again.debts.Owes("attempt-c") { |
| + t.Fatal("the completion stayed after every consumer acknowledged it") |
| + } |
| + if sig := tb.signalled(); len(sig) != 0 { |
| + t.Fatalf("signalled %v", sig) |
| + } |
| +} |
| + |
| +// Round 3, #2: a reboot's evidence is per marker. Proving launch A of a |
| +// turn leaves launch B's evidence, so B, proven after a runner restart, |
| +// neither checks nor signals the old boot's group ids that this boot |
| +// reuses. |
| +func TestRebootEvidencePerMarker(t *testing.T) { |
| + me := os.Getuid() |
| + tb := parseTable(t, turnBegan) |
| + turns, x, _, before := clearRig(t, tb, laterBoot, nil) |
| + tb.add(row(99400, 1, 99400, 99400, 5000, me, ""), "/Users/paulequipo/w/this-boot") |
| + tb.add(row(99401, 1, 99401, 99401, 5000, me, ""), "/Users/paulequipo/w/this-boot-too") |
| + o := proc.Obligation{Markers: []string{"attempt-a", "attempt-b"}, Began: turnStart, BootID: "boot-1", Before: before, FlagsKnown: true, Groups: proc.Span{99400: 1, 99401: 1}} |
| + if err := turns.debts.Owe(o); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !x.Verify(RecordID(99400, "attempt-a")) { |
| + t.Fatal("A not proven by the reboot") |
| + } |
| + _, y := restarted(turns, x, tb) |
| + if !y.Verify(RecordID(99401, "attempt-b")) { |
| + t.Fatal("B lost its reboot evidence when A was proven") |
| + } |
| + if sig := tb.signalled(); len(sig) != 0 { |
| + t.Fatalf("signalled %v", sig) |
| + } |
| +} |
| + |
| +// parseRecord is proc.ParseRecord (tests). |
| +func parseRecord(id string) (int, string, bool) { return proc.ParseRecord(id) } |
| + |
| +// settleRecord is proc.SettleMarked on x's table (tests). |
| +func (x *Executor) settleRecord(pgid int, attempt string, deadline time.Time) bool { |
| + return proc.SettleMarked(x.procs(), x.own(), pgid, attempt, deadline) |
| +} |
diff --git a/internal/runner/mac/fix9f_test.go b/internal/runner/mac/fix9f_test.go| index 79db6883..e3ece7d2 100644 |
| --- a/internal/runner/mac/fix9f_test.go |
| +++ b/internal/runner/mac/fix9f_test.go |
| @@ -184,17 +184,22 @@ func TestRebootIgnoresTheOldBootsGroups(t *testing.T) { |
| } |
| s.close() |
| } |
| - if !x.VerifyFor(RecordID(99400, "attempt-c"), ConsumerStep) || turns.Pending() { |
| + if !x.Verify(RecordID(99400, "attempt-c")) || turns.Pending() { |
| t.Fatalf("record %v: a reused group id of the old boot blocked a proven reboot", viaRecord) |
| } |
| - // Proven again for the same consumer after a runner restart, and |
| - // for the other consumer: the completion stays until both have it. |
| + // Proven again after a runner restart, twice: the completion stays |
| + // until both consumers have acknowledged it. |
| again := NewTurns(turns.lock).Sweep(tb) |
| again.own, again.Settle, again.boot = turns.own, turns.Settle, turns.boot |
| y := &Executor{Turns: again, Settle: x.Settle} |
| - if !y.VerifyFor(RecordID(99400, "attempt-c"), ConsumerStep) || !y.VerifyFor(RecordID(99400, "attempt-c"), ConsumerKeychain) { |
| + if !y.Verify(RecordID(99400, "attempt-c")) || !y.Verify(RecordID(99400, "attempt-c")) { |
| t.Fatalf("record %v: a later proof of the same record failed", viaRecord) |
| } |
| + y.Acknowledge(RecordID(99400, "attempt-c"), ConsumerStep) |
| + if !again.debts.Owes("attempt-c") { |
| + t.Fatalf("record %v: the completion went before the Keychain acknowledged it", viaRecord) |
| + } |
| + y.Acknowledge(RecordID(99400, "attempt-c"), ConsumerKeychain) |
| if again.debts.Owes("attempt-c") { |
| t.Fatalf("record %v: the completion stayed after every consumer had it", viaRecord) |
| } |
diff --git a/internal/runner/mac/gate.go b/internal/runner/mac/gate.go| index 1975e622..d96ffe26 100644 |
| --- a/internal/runner/mac/gate.go |
| +++ b/internal/runner/mac/gate.go |
| @@ -222,8 +222,8 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi |
| // kills every marked process before the runner reports runner_restarted. |
| func (g *Gate) Verify(id string) bool { return g.executor().Verify(id) } |
| -// VerifyFor is Verify by a named consumer (Executor.VerifyFor). |
| -func (g *Gate) VerifyFor(id, consumer string) bool { return g.executor().VerifyFor(id, consumer) } |
| +// Acknowledge is Executor.Acknowledge. |
| +func (g *Gate) Acknowledge(id, consumer string) { g.executor().Acknowledge(id, consumer) } |
| func exitCode(err error) int { |
| if err == nil { |
diff --git a/internal/runner/mac/launch.go b/internal/runner/mac/launch.go| index d16513ce..bf9a2cca 100644 |
| --- a/internal/runner/mac/launch.go |
| +++ b/internal/runner/mac/launch.go |
| @@ -437,7 +437,7 @@ func (r *Run) Finish(ctx context.Context) { |
| // finishPending finishes the user-temp cleanup an earlier attempt left, |
| // only when it is turn's to finish (Turns.mayFinish: its owner, or a |
| -// restart): that attempt's end proven (settleRecord), its simulators |
| +// restart): that attempt's end proven (proc.SettleMarked), its simulators |
| // shut down, then T cleaned against what it held before. Another live |
| // owner's: ErrPendingCleanup, nothing touched. The proof is due by |
| // settleBy, the rest by cleanBy. |
| @@ -449,12 +449,12 @@ func (x *Executor) finishPending(ctx context.Context, turn *Turn, settleBy, clea |
| if !x.Turns.mayFinish(turn, r) { |
| return ErrPendingCleanup |
| } |
| - // settleRecord refuses a proof once settleBy has passed (its last |
| + // SettleMarked refuses a proof once settleBy has passed (its last |
| // act): the cleanup's proof read is under the caller's deadline. |
| // An attempt of an earlier boot is over (its group id is this boot's |
| // to use); otherwise its end is proven first. |
| b, berr := x.Turns.bootNow() |
| - if !proc.Rebooted(r.BootID, r.Began, b, berr) && !x.settleRecord(r.PGID, r.Attempt, settleBy) { |
| + if !proc.Rebooted(r.BootID, r.Began, b, berr) && !proc.SettleMarked(x.procs(), x.own(), r.PGID, r.Attempt, settleBy) { |
| // That attempt's end, not this one's: nothing of this one ran. |
| return errors.Join(ErrPendingCleanup, errors.New("mac: its end is not proven")) |
| } |
| @@ -482,8 +482,6 @@ func (x *Executor) finishPending(ctx context.Context, turn *Turn, settleBy, clea |
| // RecordID is the process record of an attempt: its group and marker. |
| func RecordID(pgid int, attempt string) string { return "pg:" + strconv.Itoa(pgid) + ":" + attempt } |
| -func parseRecord(id string) (int, string, bool) { return proc.ParseRecord(id) } |
| - |
| // Verify reports whether a recorded attempt has ended for sure (after a |
| // restart, or for a step whose end was not proven when it stopped): |
| // |
| @@ -503,14 +501,11 @@ func parseRecord(id string) (int, string, bool) { return proc.ParseRecord(id) } |
| // |
| // Ended only when the group is gone (kill(-pgid, 0) is ESRCH), nothing |
| // carries the marker, its turn is settled and owes nothing, and its |
| -// cleanup is done. |
| -func (x *Executor) Verify(id string) bool { return x.VerifyFor(id, "") } |
| - |
| -// VerifyFor is Verify by a named consumer of the record's proof |
| -// (ConsumerStep, ConsumerKeychain): a reboot's completion is kept until |
| -// each has taken it ("" takes nothing). |
| -func (x *Executor) VerifyFor(id, consumer string) bool { |
| - pgid, attempt, ok := parseRecord(id) |
| +// cleanup is done. It only reads what consumers rely on: after a |
| +// reboot it keeps a completion that every later proof of the |
| +// record reads, until each consumer has acknowledged it (Acknowledge). |
| +func (x *Executor) Verify(id string) bool { |
| + pgid, attempt, ok := proc.ParseRecord(id) |
| if !ok { |
| return false |
| } |
| @@ -540,7 +535,7 @@ func (x *Executor) VerifyFor(id, consumer string) bool { |
| } |
| // A reboot is a boot-qualified clearance: the old boot's group id is |
| // this boot's to use, and nothing is signalled. |
| - if !met || !rebooted && !x.settleRecord(pgid, attempt, deadline) || !time.Now().Before(deadline) { |
| + if !met || !rebooted && !proc.SettleMarked(x.procs(), x.own(), pgid, attempt, deadline) || !time.Now().Before(deadline) { |
| return false |
| } |
| if held != nil && x.UserTemp != "" { |
| @@ -558,23 +553,27 @@ func (x *Executor) VerifyFor(id, consumer string) bool { |
| if rebooted { |
| // Kept for every consumer (and any later proof) of the record. |
| b, err := x.Turns.bootNow() |
| - return err == nil && x.Turns.debts.Complete(attempt, consumer, b.ID, []string{ConsumerStep, ConsumerKeychain}, deadline) == nil |
| + return err == nil && x.Turns.debts.Complete(attempt, b.ID, deadline) == nil |
| } |
| return x.Turns.debts.Discharge(attempt, deadline) == nil |
| } |
| -// The consumers of a record's proof (VerifyFor): the step's recovery |
| +// Acknowledge: consumer (ConsumerStep, ConsumerKeychain) has durably |
| +// kept what Verify proved of id, so its share of a reboot's completion |
| +// may go. Call it only after that consumer's own result is committed. |
| +func (x *Executor) Acknowledge(id, consumer string) { |
| + if _, attempt, ok := proc.ParseRecord(id); ok && x.Turns != nil { |
| + _ = x.Turns.debts.Acknowledge(attempt, consumer, []string{ConsumerStep, ConsumerKeychain}) |
| + } |
| +} |
| + |
| +// The consumers of a record's proof (Acknowledge): the step's recovery |
| // and the credential (Keychain) recovery. |
| const ( |
| ConsumerStep = "step" |
| ConsumerKeychain = "keychain" |
| ) |
| -// settleRecord is proc.SettleMarked on this executor's table. |
| -func (x *Executor) settleRecord(pgid int, attempt string, deadline time.Time) bool { |
| - return proc.SettleMarked(x.procs(), x.own(), pgid, attempt, deadline) |
| -} |
| - |
| // Marked lists this user's processes whose environment carries |
| // EQUIPO_ATTEMPT=<attempt> where the kernel shows it (proc.Marked). |
| func Marked(attempt string) ([]Proc, error) { return proc.Marked(attempt) } |
diff --git a/internal/runner/mac/proc/clear.go b/internal/runner/mac/proc/clear.go| index 98d7b53b..71e85446 100644 |
| --- a/internal/runner/mac/proc/clear.go |
| +++ b/internal/runner/mac/proc/clear.go |
| @@ -136,7 +136,8 @@ func othersClear(all []Obligation, me int, t Table, resolved func(Obligation) bo |
| } |
| // Discharge removes every obligation naming attempt (its end proven), |
| -// before deadline (zero: none), or not at all. |
| +// before deadline (zero: none), or not at all. A save that ends late is |
| +// undone: the debt stays for a later proof. |
| func (d *Debts) Discharge(attempt string, deadline time.Time) error { |
| return d.lockedBy(deadline, func() error { |
| all, err := d.load() |
| @@ -146,25 +147,36 @@ func (d *Debts) Discharge(attempt string, deadline time.Time) error { |
| if d.between != nil { |
| d.between() |
| } |
| - keep := slices.DeleteFunc(all, func(o Obligation) bool { return slices.Contains(o.Markers, attempt) }) |
| - if !deadline.IsZero() && !time.Now().Before(deadline) { |
| + keep := slices.DeleteFunc(slices.Clone(all), func(o Obligation) bool { return slices.Contains(o.Markers, attempt) }) |
| + if late(deadline) { |
| return errLate |
| } |
| - return d.save(keep) |
| + if err := d.save(keep); err != nil { |
| + return err |
| + } |
| + if late(deadline) { |
| + return errors.Join(errLate, d.save(all)) |
| + } |
| + return nil |
| }) |
| } |
| +// late: deadline (zero: none) has passed. |
| +func late(deadline time.Time) bool { return !deadline.IsZero() && !time.Now().Before(deadline) } |
| + |
| // completionLife bounds how long a reboot's completion waits for its |
| -// consumers (a record that only one of them ever verifies). |
| +// consumers (a record that only one of them ever acknowledges). |
| const completionLife = 30 * 24 * time.Hour |
| // Complete records, before deadline, that attempt was proven over by a |
| -// reboot into boot (a boot-qualified completion): what it owed goes, one |
| -// completion stays, and consumer is on it. Every later proof of the |
| -// record, in any runner process, is that completion: no process check, |
| -// no signal. It goes once every one of consumers has it ("" records |
| -// nothing), or after completionLife. |
| -func (d *Debts) Complete(attempt, consumer, boot string, consumers []string, deadline time.Time) error { |
| +// reboot into boot (a boot-qualified completion). attempt leaves every |
| +// obligation that names it (one that names only it goes); another |
| +// marker's evidence stays. Every later proof of the record, in any |
| +// runner process, is that completion: no process check, no signal. It |
| +// goes once every consumer has acknowledged it (Acknowledge), or after |
| +// completionLife. Saved late, it fails but stays: a later proof |
| +// finishes from it. |
| +func (d *Debts) Complete(attempt, boot string, deadline time.Time) error { |
| return d.lockedBy(deadline, func() error { |
| all, err := d.load() |
| if err != nil { |
| @@ -179,21 +191,44 @@ func (d *Debts) Complete(attempt, consumer, boot string, consumers []string, dea |
| keep = append(keep, o) |
| case o.ProvenIn != "": |
| done = o |
| + case len(o.Markers) > 1: |
| + o.Markers = slices.DeleteFunc(slices.Clone(o.Markers), func(m string) bool { return m == attempt }) |
| + keep = append(keep, o) |
| } |
| } |
| - if consumer != "" && !slices.Contains(done.Recorded, consumer) { |
| - done.Recorded = append(done.Recorded, consumer) |
| - } |
| - if !slices.ContainsFunc(consumers, func(c string) bool { return !slices.Contains(done.Recorded, c) }) { |
| - done = Obligation{} |
| + if late(deadline) { |
| + return errLate |
| } |
| - if done.ProvenIn != "" { |
| - keep = append(keep, done) |
| + if err := d.save(append(keep, done)); err != nil { |
| + return err |
| } |
| - if !deadline.IsZero() && !time.Now().Before(deadline) { |
| + if late(deadline) { |
| return errLate |
| } |
| - return d.save(keep) |
| + return nil |
| + }) |
| +} |
| + |
| +// Acknowledge records that consumer has durably kept what a proof of |
| +// attempt found (its own recovery committed): the completion goes once |
| +// every one of consumers has. Nothing else is touched. |
| +func (d *Debts) Acknowledge(attempt, consumer string, consumers []string) error { |
| + return d.locked(func() error { |
| + all, err := d.load() |
| + if err != nil { |
| + return err |
| + } |
| + i := slices.IndexFunc(all, func(o Obligation) bool { return o.ProvenIn != "" && slices.Contains(o.Markers, attempt) }) |
| + if i < 0 { |
| + return nil |
| + } |
| + if !slices.Contains(all[i].Recorded, consumer) { |
| + all[i].Recorded = append(all[i].Recorded, consumer) |
| + } |
| + if !slices.ContainsFunc(consumers, func(c string) bool { return !slices.Contains(all[i].Recorded, c) }) { |
| + all = slices.Delete(all, i, i+1) |
| + } |
| + return d.save(all) |
| }) |
| } |
diff --git a/internal/runner/mac/proc/debts.go b/internal/runner/mac/proc/debts.go| index 9caff366..95bb45b8 100644 |
| --- a/internal/runner/mac/proc/debts.go |
| +++ b/internal/runner/mac/proc/debts.go |
| @@ -20,6 +20,8 @@ type Debts struct { |
| // between runs between a read and its write (tests: another |
| // process's write in the gap). |
| between func() |
| + // saved runs once a save has written (tests: a slow write). |
| + saved func() |
| // LockHook (a test seam) runs before each lock is taken, with its number from 1 |
| // (tests: a lock that fails, or is slow). |
| LockHook func(n int) error |
| @@ -48,9 +50,8 @@ var errLate = errors.New("mac: the debts were not reached in time") |
| // lockedBy is locked, given up at deadline (zero: none). Every error |
| // (the lock file, the lock, the deadline) is the caller's. |
| func (d *Debts) lockedBy(deadline time.Time, fn func() error) error { |
| - late := func() bool { return !deadline.IsZero() && !time.Now().Before(deadline) } |
| for !d.mu.TryLock() { |
| - if late() { |
| + if late(deadline) { |
| return errLate |
| } |
| time.Sleep(10 * time.Millisecond) |
| @@ -62,7 +63,7 @@ func (d *Debts) lockedBy(deadline time.Time, fn func() error) error { |
| } |
| } |
| if d.Path == "" { |
| - if late() { |
| + if late(deadline) { |
| return errLate |
| } |
| return fn() |
| @@ -80,13 +81,13 @@ func (d *Debts) lockedBy(deadline time.Time, fn func() error) error { |
| if !errors.Is(err, syscall.EWOULDBLOCK) { |
| return err |
| } |
| - if late() { |
| + if late(deadline) { |
| return errLate |
| } |
| time.Sleep(10 * time.Millisecond) |
| } |
| defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN) |
| - if late() { |
| + if late(deadline) { |
| return errLate |
| } |
| return fn() |
| @@ -120,6 +121,9 @@ func (d *Debts) load() ([]Obligation, error) { |
| } |
| func (d *Debts) save(all []Obligation) error { |
| + if d.saved != nil { |
| + defer d.saved() |
| + } |
| if d.Path == "" { |
| d.mem = all |
| return nil |
diff --git a/internal/runner/mac/proc/proc_test.go b/internal/runner/mac/proc/proc_test.go| index 249cb4ac..52292f90 100644 |
| --- a/internal/runner/mac/proc/proc_test.go |
| +++ b/internal/runner/mac/proc/proc_test.go |
| @@ -119,3 +119,42 @@ func TestDebtsUnderTheDeadline(t *testing.T) { |
| t.Fatal("a late discharge removed the obligation") |
| } |
| } |
| + |
| +// Round 3, #3: the deadline covers the save itself (the write, its sync |
| +// and rename). A discharge whose save ends late fails and leaves the |
| +// debt; a reboot's completion saved late fails, and stays as the |
| +// evidence a later proof finishes from. |
| +func TestDebtsSaveUnderTheDeadline(t *testing.T) { |
| + dir := t.TempDir() |
| + slow := false |
| + delay := func() { |
| + if slow { |
| + slow = false |
| + time.Sleep(150 * time.Millisecond) |
| + } |
| + } |
| + d := &Debts{Path: filepath.Join(dir, "owed"), saved: delay} |
| + if err := d.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil { |
| + t.Fatal(err) |
| + } |
| + slow = true |
| + if err := d.Discharge("attempt-a", time.Now().Add(50*time.Millisecond)); err == nil { |
| + t.Fatal("a discharge whose save ended after its deadline succeeded") |
| + } |
| + if !d.Owes("attempt-a") { |
| + t.Fatal("a late discharge erased the debt") |
| + } |
| + |
| + c := &Debts{Path: filepath.Join(dir, "owed-c"), saved: delay} |
| + if err := c.Owe(Obligation{Markers: []string{"attempt-b"}, BootID: "boot-1"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + slow = true |
| + if err := c.Complete("attempt-b", "boot-2", time.Now().Add(50*time.Millisecond)); err == nil { |
| + t.Fatal("a completion saved after its deadline succeeded") |
| + } |
| + all, err := c.load() |
| + if err != nil || len(all) != 1 || all[0].ProvenIn != "boot-2" { |
| + t.Fatalf("a late completion was not kept for the next proof: %+v %v", all, err) |
| + } |
| +} |
diff --git a/internal/runner/thin/mac.go b/internal/runner/thin/mac.go| index dd2e68f1..e0eebeaa 100644 |
| --- a/internal/runner/thin/mac.go |
| +++ b/internal/runner/thin/mac.go |
| @@ -46,7 +46,18 @@ func (m macGate) Run(ctx context.Context, attempt, dir string, argv []string, li |
| return gateResult{output: r.Output, reason: r.Reason, code: r.Code, terminated: r.Terminated, sandbox: r.Sandbox} |
| } |
| -func (m macGate) Verify(id string) bool { return m.g.VerifyFor(id, mac.ConsumerStep) } |
| +func (m macGate) Verify(id string) bool { return m.g.Verify(id) } |
| + |
| +// Acknowledge: the step's recovery committed what Verify proved of id. |
| +func (m macGate) Acknowledge(id string) { m.g.Acknowledge(id, mac.ConsumerStep) } |
| + |
| +// acknowledge tells a gate that keeps evidence for its proofs (a Mac's) |
| +// that the step's recovery committed what it proved of id. |
| +func acknowledge(g GateRunner, id string) { |
| + if a, ok := g.(interface{ Acknowledge(string) }); ok { |
| + a.Acknowledge(id) |
| + } |
| +} |
| // macChecks chooses each attempt's gate from the probe report this |
| // process made and still trusts (macProbes.trusted): srt and seatbelt |
| @@ -141,7 +152,10 @@ func unsandboxedChosen(ctx context.Context) bool { |
| // Verify settles a recorded attempt the same way whatever confined it, |
| // with the user temp, so the attempt's own unfinished cleanup is |
| // finished once its end is proven. |
| -func (m *macChecks) Verify(id string) bool { return m.sandboxed.VerifyFor(id, mac.ConsumerStep) } |
| +func (m *macChecks) Verify(id string) bool { return m.sandboxed.Verify(id) } |
| + |
| +// Acknowledge is macGate's, on the gate that verifies. |
| +func (m *macChecks) Acknowledge(id string) { m.sandboxed.Acknowledge(id, mac.ConsumerStep) } |
| // macTaint is sticky: set once a check ran outside the sandbox, kept in |
| // the state dir and by the instance (which says so in every welcome), so |
diff --git a/internal/runner/thin/mac_ack_test.go b/internal/runner/thin/mac_ack_test.go| new file mode 100644 |
| index 00000000..023aaf40 |
| --- /dev/null |
| +++ b/internal/runner/thin/mac_ack_test.go |
| @@ -0,0 +1,63 @@ |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Task 9 follow-up, round 3, #1: the step's recovery acknowledges a |
| +// proven exit only once what it found is committed. A recovery that dies |
| +// before its commit acknowledges nothing; a later one that commits does, |
| +// after; so does the probe once it records the termination. |
| +func TestRecoveryAcknowledgesOnlyAfterItsCommit(t *testing.T) { |
| + ctx := context.Background() |
| + dir := t.TempDir() |
| + state, err := OpenState(ctx, dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, id := range []string{"a1", "b1"} { |
| + d := wire.DispatchCommand{Person: connPerson, TaskID: "tk-" + id, StepID: id, Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}} |
| + if err := state.AcceptDispatch(ctx, "f-"+id, d); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := state.setStepNamespace(ctx, stepRow{DispatchCommand: d}, "run-"+id); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var acked []string |
| + stateOf := func(s *State, step string) string { |
| + var st string |
| + _ = s.db.QueryRow("SELECT state FROM steps WHERE step_id=?", step).Scan(&st) |
| + return st |
| + } |
| + // The runner dies between the proofs and the commit. |
| + crash := &StepEngine{State: state, Acknowledge: func(_ context.Context, id string) { acked = append(acked, id) }, |
| + Verify: func(context.Context, string) bool { state.Close(); return true }} |
| + if err := crash.recoverSteps(ctx); err == nil || len(acked) != 0 { |
| + t.Fatalf("a recovery that never committed: err %v, acknowledged %v", err, acked) |
| + } |
| + state, err = OpenState(ctx, dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer state.Close() |
| + proven := map[string]bool{"run-a1": true} |
| + e := &StepEngine{State: state, Verify: func(_ context.Context, id string) bool { return proven[id] }} |
| + e.Acknowledge = func(_ context.Context, id string) { |
| + if st := stateOf(state, id[len("run-"):]); st != runReported { |
| + t.Errorf("%s acknowledged with its step %q, not committed", id, st) |
| + } |
| + acked = append(acked, id) |
| + } |
| + if err := e.recoverSteps(ctx); err != nil || len(acked) != 1 || acked[0] != "run-a1" { |
| + t.Fatalf("recovery: err %v, acknowledged %v", err, acked) |
| + } |
| + proven["run-b1"] = true |
| + e.Probe(ctx) |
| + if len(acked) != 2 || acked[1] != "run-b1" { |
| + t.Fatalf("probe: acknowledged %v", acked) |
| + } |
| +} |
diff --git a/internal/runner/thin/maccreds.go b/internal/runner/thin/maccreds.go| index 2060c7fa..c63a8d89 100644 |
| --- a/internal/runner/thin/maccreds.go |
| +++ b/internal/runner/thin/maccreds.go |
| @@ -124,7 +124,7 @@ func (h *macHost) newCreds(c runner.Config) *macCreds { |
| }) |
| } |
| } |
| - m.keychain = &creds.Keychain{Dir: filepath.Join(c.StateDir, "keychain"), Login: filepath.Join(h.paths.Home, "Library", "Keychains", "login.keychain-db"), User: m.user, List: h.keychainList, Delete: h.keychainDelete} |
| + m.keychain = &creds.Keychain{Dir: filepath.Join(c.StateDir, "keychain"), Login: filepath.Join(h.paths.Home, "Library", "Keychains", "login.keychain-db"), User: m.user, List: h.keychainList, Delete: h.keychainDelete, Acked: m.acked} |
| return m |
| } |
| @@ -282,17 +282,25 @@ func (m *macCreds) settle(ctx context.Context, restart bool) { |
| // obligations still owed, a boot time that cannot be read) keeps the |
| // watch, the hold and the Mac's connections closed. |
| func (m *macCreds) ended(w creds.Watch) bool { |
| - run := w.Run |
| - if run == "" { |
| - run = mac.RecordID(0, w.Marker) |
| - } |
| - if m.exec.VerifyFor(run, mac.ConsumerKeychain) { |
| + if m.exec.Verify(watchRun(w)) { |
| return true |
| } |
| boot, err := m.boot() |
| return err == nil && !w.Started.IsZero() && boot.After(w.Started) |
| } |
| +// acked: w's watch is settled durably (creds.Keychain.Acked), so the |
| +// Keychain recovery's share of its launch's proof may go. |
| +func (m *macCreds) acked(w creds.Watch) { m.exec.Acknowledge(watchRun(w), mac.ConsumerKeychain) } |
| + |
| +// watchRun is the process record of w's launch. |
| +func watchRun(w creds.Watch) string { |
| + if w.Run != "" { |
| + return w.Run |
| + } |
| + return mac.RecordID(0, w.Marker) |
| +} |
| + |
| // place makes a launch's own CLAUDE_CONFIG_DIR under the runtime root |
| // (<scratch>/homes, which no sandboxed command reaches); its name is the |
| // launch's marker. |
diff --git a/internal/runner/thin/run.go b/internal/runner/thin/run.go| index 7961b546..89ae7041 100644 |
| --- a/internal/runner/thin/run.go |
| +++ b/internal/runner/thin/run.go |
| @@ -863,7 +863,8 @@ func runWorker(ctx context.Context, c runner.Config, o Options, platform wire.Pl |
| // sessions it kept (worker_ask.go). |
| steps := &StepEngine{State: state, Exec: placementGuard(tags, caps.Stale, worker.Exec), Ask: worker.ask, Notify: link.Notify, Logger: o.Logger, |
| Sweep: worker.sweep, |
| - Verify: func(_ context.Context, id string) bool { return w.gate.Verify(workerRecord(id)) }, TerminationKind: wire.TerminationProcessGroup} |
| + Verify: func(_ context.Context, id string) bool { return w.gate.Verify(workerRecord(id)) }, TerminationKind: wire.TerminationProcessGroup, |
| + Acknowledge: func(_ context.Context, id string) { acknowledge(w.gate, workerRecord(id)) }} |
| link.Steps = steps |
| if err := steps.Start(ctx); err != nil { |
| return err |