oficina / equipo Public

Mac slice F wb fix 1 #1: Keychain watches clear only by Verify

Whole-branch review round 1, finding 1. The credential recovery no
longer accepts kern.boottime later than the watch's start as proof:
a clock stepped forward moves it with no reboot. A watch now clears
only through Executor.Verify, whose reboot clearance is
boot-qualified (another kern.bootsessionuuid and a later boot time,
under the one proof deadline, acknowledged per consumer). The
macCreds boot field is gone; the darwin rig's reboot is now a
creds.BootForTest boot with another identity, and it ends the dead
runner's CLI as a reboot would.

Regression: TestDarwinWatchHeldWhenOnlyTheClockMoves (same boot id,
boot time now: still held; then a reboot clears it). Mutation: the
boot-time fallback restored fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev ada71470cc43c38699a0f4cfdceba2136697530b parent 0722bc4
6 files changed, +101 −29
  • internal/runner/thin/fakeworker_testbuild.go +1 −1
  • internal/runner/thin/maccreds.go +8 −16
  • internal/runner/thin/maccreds_fix1_test.go +1 −1
  • internal/runner/thin/maccreds_fix2_test.go +31 −6
  • internal/runner/thin/maccreds_test.go +2 −5
  • internal/runner/thin/wbfix1_creds_test.go +58 −0
diff --git a/internal/runner/thin/fakeworker_testbuild.go b/internal/runner/thin/fakeworker_testbuild.go
index 4c1a1ed4..8618bfce 100644
--- a/internal/runner/thin/fakeworker_testbuild.go
+++ b/internal/runner/thin/fakeworker_testbuild.go
@@ -222,7 +222,7 @@ func (fakeWorker) engineering(ctx context.Context, c *runner.Config, o Options)
m := &macCreds{turns: turns, exec: &mac.Executor{Turns: turns}, cli: pinned,
paths: mac.Paths{Home: home, StateDir: c.StateDir, AccountsRoot: o.AccountsRoot, SessionRoot: SessionRoot(*c), InstallDir: install, UserTemp: fakeUserTemp},
configDir: macConfigDir(*c, home), runner: firstNonEmpty(c.ToolProxyPath, c.Executable), scratch: c.ScratchRoot, taint: loadTaint(filepath.Join(c.StateDir, "tainted")),
- boot: creds.BootTime, user: user, uid: os.Getuid(), log: o.Logger}
+ user: user, uid: os.Getuid(), log: o.Logger}
m.fact = func(p string) (creds.Fact, error) { return creds.Read(p, nil) }
m.keychain = &creds.Keychain{Dir: filepath.Join(c.StateDir, "keychain"), Login: filepath.Join(home, "Library", "Keychains", "login.keychain-db"), User: user,
List: func(context.Context, string) ([]creds.Item, error) { return nil, nil },
diff --git a/internal/runner/thin/maccreds.go b/internal/runner/thin/maccreds.go
index c63a8d89..f4345d52 100644
--- a/internal/runner/thin/maccreds.go
+++ b/internal/runner/thin/maccreds.go
@@ -91,8 +91,6 @@ type macCreds struct {
configDir, runner, scratch string
taint *macTaint
keychain *creds.Keychain
- // boot is when the Mac last booted (creds.BootTime; tests stand in).
- boot func() (time.Time, error)
// fact reads one path's kind, mode, owner and ACL (ls -led and lstat
// on a Mac).
fact func(string) (creds.Fact, error)
@@ -108,7 +106,7 @@ type macCreds struct {
// newCreds is h's credential operations for c.
func (h *macHost) newCreds(c runner.Config) *macCreds {
m := &macCreds{turns: h.turns, exec: &mac.Executor{Turns: h.turns}, cli: h.tools.Claude, paths: h.paths, configDir: macConfigDir(c, h.paths.Home),
- runner: c.Executable, scratch: c.ScratchRoot, taint: h.taint, boot: creds.BootTime, uid: -1, log: h.log}
+ runner: c.Executable, scratch: c.ScratchRoot, taint: h.taint, uid: -1, log: h.log}
ctx, cancel := context.WithTimeout(context.Background(), mac.QueryLimit)
defer cancel()
if a, err := mac.ReadAccount(ctx, h.run); err == nil {
@@ -275,19 +273,13 @@ func (m *macCreds) settle(ctx context.Context, restart bool) {
}
// ended: the watched launch has surely ended, so its watch may take its
-// second look. Only two things say so (spec §8.8; the controller's ruling
-// for task 10, fix round 2): Executor.Verify proves its end by its
-// process record, or the Mac has booted since the watch began (no
-// process outlives a boot). Anything else (an incomplete turn record,
-// 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 {
- if m.exec.Verify(watchRun(w)) {
- return true
- }
- boot, err := m.boot()
- return err == nil && !w.Started.IsZero() && boot.After(w.Started)
-}
+// second look. Only Executor.Verify says so (spec §8.8): its end proven
+// by its process record, or a boot-qualified reboot clearance (another
+// kern.bootsessionuuid and a later boot time, under the one proof
+// deadline, acknowledged per consumer). A boot time alone moves with
+// the clock and proves nothing. Anything else keeps the watch, the hold
+// and the Mac's connections closed.
+func (m *macCreds) ended(w creds.Watch) bool { return m.exec.Verify(watchRun(w)) }
// acked: w's watch is settled durably (creds.Keychain.Acked), so the
// Keychain recovery's share of its launch's proof may go.
diff --git a/internal/runner/thin/maccreds_fix1_test.go b/internal/runner/thin/maccreds_fix1_test.go
index 7b3b8767..bad141ff 100644
--- a/internal/runner/thin/maccreds_fix1_test.go
+++ b/internal/runner/thin/maccreds_fix1_test.go
@@ -150,7 +150,7 @@ func TestDarwinCheckKeychainWatch(t *testing.T) {
if !r.m.Held() || len(r.kc.deleted) != 0 {
t.Fatalf("after a restart without a reboot: held=%v deleted %+v", r.m.Held(), r.kc.deleted)
}
- r.rebooted()
+ r.rebooted(t, pid)
r.connector()
if r.m.Held() || len(r.kc.deleted) != 1 {
t.Fatalf("after a reboot: held=%v deleted %+v", r.m.Held(), r.kc.deleted)
diff --git a/internal/runner/thin/maccreds_fix2_test.go b/internal/runner/thin/maccreds_fix2_test.go
index cc93cebf..e690c4a7 100644
--- a/internal/runner/thin/maccreds_fix2_test.go
+++ b/internal/runner/thin/maccreds_fix2_test.go
@@ -16,6 +16,7 @@ import (
"amadan.net/oficina/equipo/internal/fakeclaude"
"amadan.net/oficina/equipo/internal/runner/mac"
"amadan.net/oficina/equipo/internal/runner/mac/creds"
+ "amadan.net/oficina/equipo/internal/runner/mac/proc"
"amadan.net/oficina/equipo/internal/wire"
)
@@ -24,9 +25,33 @@ import (
// the watch began; until then the Mac keeps its Claude connections
// closed and says so.
-// rebooted makes the Mac's boot time now: later than every watch so far.
-func (r *darwinRig) rebooted() {
- r.m.boot = func() (time.Time, error) { return time.Now(), nil }
+// rebooted is a reboot of the Mac (creds.BootForTest): another boot
+// identity, begun now, later than every watch so far. No process
+// outlives a boot, so each pid in gone is ended first.
+func (r *darwinRig) rebooted(t *testing.T, gone ...int) {
+ t.Helper()
+ for _, pid := range gone {
+ _ = syscall.Kill(pid, syscall.SIGKILL)
+ }
+ now, err := creds.Boot()
+ if err != nil {
+ t.Fatal(err)
+ }
+ creds.BootForTest(func() (proc.Boot, error) { return proc.Boot{ID: now.ID + "-next", Time: time.Now()}, nil })
+ t.Cleanup(func() { creds.BootForTest(nil) })
+}
+
+// clockForward steps the Mac's clock forward with no reboot: the same
+// boot identity, its boot time read as now (kern.boottime moves when the
+// clock is stepped).
+func (r *darwinRig) clockForward(t *testing.T) {
+ t.Helper()
+ now, err := creds.Boot()
+ if err != nil {
+ t.Fatal(err)
+ }
+ creds.BootForTest(func() (proc.Boot, error) { return proc.Boot{ID: now.ID, Time: time.Now()}, nil })
+ t.Cleanup(func() { creds.BootForTest(nil) })
}
// crashCheck starts a check in a second runner process and SIGKILLs it
@@ -75,7 +100,7 @@ func TestDarwinCrashedWatchHeldUntilReboot(t *testing.T) {
}
return nil
}
- r.crashCheck(t, local)
+ hung := r.crashCheck(t, local)
survivor := exec.Command("/bin/sh", "-c", "sleep 1; touch "+filepath.Join(r.dir, "wrote"))
survivor.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if err := survivor.Start(); err != nil {
@@ -102,7 +127,7 @@ func TestDarwinCrashedWatchHeldUntilReboot(t *testing.T) {
if !r.m.Held() {
t.Fatal("released with the Mac booted before the watch began")
}
- r.rebooted()
+ r.rebooted(t, hung)
r.m.settle(r.ctx, true)
if r.kc.has(item) || len(unprovenOf(r.m)) != 0 {
t.Fatalf("after a reboot: item kept=%v unproven=%v", r.kc.has(item), unprovenOf(r.m))
@@ -209,7 +234,7 @@ func TestDarwinClearanceVerifiesTheRecordedRun(t *testing.T) {
t.Fatal(err)
}
r.m.keychain.Abandon(marker)
- if boot, err := r.m.boot(); err != nil || !boot.Before(time.Now()) {
+ if boot, err := creds.Boot(); err != nil || !boot.Time.Before(time.Now()) {
t.Fatalf("boot %v %v", boot, err)
}
r.m.settle(r.ctx, true)
diff --git a/internal/runner/thin/maccreds_test.go b/internal/runner/thin/maccreds_test.go
index efe47599..5e1373d1 100644
--- a/internal/runner/thin/maccreds_test.go
+++ b/internal/runner/thin/maccreds_test.go
@@ -241,7 +241,6 @@ func darwinRigAt(t *testing.T, dir string) *darwinRig {
configDir: filepath.Join(dir, "config"), runner: proxy, scratch: r.cfg.ScratchRoot, taint: loadTaint(filepath.Join(r.cfg.StateDir, "tainted")),
user: r.user, uid: os.Getuid(), log: slog.New(slog.NewTextHandler(&r.logs, nil))}
m.fact = func(p string) (creds.Fact, error) { return creds.Read(p, nil) }
- m.boot = creds.BootTime
m.keychain = &creds.Keychain{Dir: filepath.Join(r.cfg.StateDir, "keychain"), Login: filepath.Join(dir, "Library", "Keychains", "login.keychain-db"), User: r.user, List: r.kc.list, Delete: r.kc.delete}
r.m = m
r.opts = ClaudeOptions{Runner: r.cfg, State: r.state, Mac: m, LockWait: 5 * time.Second}
@@ -477,14 +476,12 @@ func TestDarwinTokenCheck(t *testing.T) {
if _, err := r.opts.CheckToken(context.Background(), b); !errors.Is(err, errCredentialUnproven) {
t.Fatalf("the check after the crash: %v", err)
}
- r.rebooted()
+ // A reboot ends the dead runner's CLI with everything else.
+ r.rebooted(t, pid)
r.connector()
if _, err := r.opts.CheckToken(context.Background(), b); err != nil {
t.Fatalf("the check after a reboot: %v", err)
}
- if pidAlive(pid) {
- t.Fatal("the next turn left the dead runner's CLI running")
- }
})
}
diff --git a/internal/runner/thin/wbfix1_creds_test.go b/internal/runner/thin/wbfix1_creds_test.go
new file mode 100644
index 00000000..2910ae35
--- /dev/null
+++ b/internal/runner/thin/wbfix1_creds_test.go
@@ -0,0 +1,58 @@
+//go:build testbuild
+
+package thin
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+
+ "amadan.net/oficina/equipo/internal/fakeclaude"
+)
+
+// Whole-branch fix 1, #1: a credential watch clears only by the
+// Executor's proof (Verify), which a reboot satisfies only boot-
+// qualified: another kern.bootsessionuuid and a later boot time. A clock
+// stepped forward moves kern.boottime past the watch's start with the
+// same boot identity: the watch, the hold and the closed connections
+// stay.
+func TestDarwinWatchHeldWhenOnlyTheClockMoves(t *testing.T) {
+ r := newDarwinRig(t)
+ local, _ := connected(t, r, fakeclaude.Token("a@example.com"))
+ if err := os.WriteFile(filepath.Join(r.dir, "hang"), nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ helper := exec.Command(os.Args[0], "-test.run=^TestDarwinTokenCheckCrashHelper$", "-test.count=1")
+ helper.Env = append(os.Environ(), "EQUIPO_DARWIN_CRASH_DIR="+r.dir, "EQUIPO_DARWIN_CRASH_LOCAL="+local)
+ if err := helper.Start(); err != nil {
+ t.Fatal(err)
+ }
+ pid := waitPid(t, filepath.Join(r.dir, "hang.pid"))
+ _ = helper.Process.Kill()
+ _ = helper.Wait()
+ // What its CLI stored meanwhile.
+ r.creating("-p ", r.own)
+ r.kc.mu.Lock()
+ delete(r.kc.created, pid)
+ r.kc.mu.Unlock()
+ r.connector()
+ if !r.m.Held() || len(unprovenOf(r.m)) != 1 {
+ t.Fatalf("after the crash: held=%v unproven=%v", r.m.Held(), unprovenOf(r.m))
+ }
+ r.clockForward(t)
+ r.connector()
+ r.m.settle(r.ctx, true)
+ if !r.m.Held() || len(unprovenOf(r.m)) != 1 || len(r.kc.deleted) != 0 {
+ t.Fatalf("the clock moved forward, no reboot: held=%v unproven=%v deleted %+v", r.m.Held(), unprovenOf(r.m), r.kc.deleted)
+ }
+ b, _ := r.binding(local)
+ if _, err := r.opts.CheckToken(r.ctx, b); err == nil {
+ t.Fatal("a check ran with the watch held")
+ }
+ r.rebooted(t, pid)
+ r.connector()
+ if r.m.Held() || len(r.kc.deleted) != 1 {
+ t.Fatalf("after a reboot: held=%v deleted %+v", r.m.Held(), r.kc.deleted)
+ }
+}