retire-app-lock-407: Astra round 1 P2 fixes (READY AFTER FIXES)
Round 1 (gpt-6-astra, findings in docs/superpowers/specs/CODEX-FINDINGS-RETIRE-APP-LOCK-407.md): no P1, two P2s, all ten record-write census units approved. - P2 1 (lock ownership): the witness now also requires the writing goroutine's own stack to pass through a function that holds an app lock for its whole body: withAppHost, withRecordLock, or any console function that defers s.lockApp or s.lockAppHost (parsed from the source). Another goroutine's lock (a retire in flight) no longer vouches for an unlocked write. TestRecordWriteWitnessNeedsTheWritersOwnLock is the negative probe. On main's writers the witness names createRecordMarked, createSidecarMarked and handleAPIInstanceChannel. - P2 2 (losing conditional writes): the witness judges only Puts the store accepted, against the body read just before the write, after any put hook. TestFenceMakesAnOutstandingWriteHarmless, which tripped it in CI, is the regression and passes 20/20. Tombstones follow nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+164
−15
docs/superpowers/specs/CODEX-FINDINGS-RETIRE-APP-LOCK-407.md+40 −0internal/console/record_write_lock_test.go+113 −10internal/console/view_test.go+11 −5
CI failed — run details
diff --git a/docs/superpowers/specs/CODEX-FINDINGS-RETIRE-APP-LOCK-407.md b/docs/superpowers/specs/CODEX-FINDINGS-RETIRE-APP-LOCK-407.md| new file mode 100644 |
| index 000000000..46e808b06 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/CODEX-FINDINGS-RETIRE-APP-LOCK-407.md |
| @@ -0,0 +1,40 @@ |
| +Reviewed `origin/main...9eba959e78c3c83c24956e0e12eb80199afb9d41`, including merged #415. Later working-tree edits are excluded. |
| + |
| +All ten record-write census entries are **approved**. Their digests match the pinned function bodies, their `record-writer` classes are unchanged, and each retains its original provision call and write arguments. |
| + |
| +| Unit | Verdict | Evidence | |
| +|---|---|---| |
| +| `createRecordMarked` | Approve | `internal/console/hostsused.go:168–174`: lock covers mark and create | |
| +| `createSidecarMarked` | Approve | `internal/console/hostsused.go:180–185`: lock covers mark and create | |
| +| `attachDomainAs` | Approve | `internal/console/domainattach.go:259`: create under record’s app lock | |
| +| `handleAPIInstanceChannel` | Approve | `internal/console/channelmove.go:177–178`: channel update locked | |
| +| `executePendingRouteChannel` | Approve | `internal/console/channelmove.go:215–220`: proposal checks retained; update locked | |
| +| `handleAPIInstanceUpdate` | Approve | `internal/console/apiinstances.go:2427`: update locked | |
| +| `handleAppGate` | Approve | `internal/console/appsettings.go:1122`: update locked | |
| +| `handleAppRateLimit` | Approve | `internal/console/appsettings.go:1249`: update locked | |
| +| `applyUpdates` | Approve | `internal/console/updatescore.go:178`: update locked | |
| +| `requestInstall` | Approve | `internal/console/updatescore.go:218`: each conditional update locked | |
| + |
| +**P1: None found.** |
| + |
| +**P2 — The witness can accept an unlocked follower write.** |
| +Evidence: `internal/console/record_write_lock_test.go:43,95–97,104–115`. `witnessLockHeld` accepts a lock held by any goroutine on any registered server. While retirement holds the app lock, an unlocked sidecar create or another omitted writer would therefore pass this check—the exact overlap it should reject. The dedicated concurrency test covers only ordinary instance creation and direct channel changes (`:171–180`). |
| + |
| +Fix: check ownership by the writing goroutine, using the existing test-only tracking pattern in `internal/console/lifetimes_create_test.go:2310–2350`. Add a negative probe with another goroutine holding the same app lock. |
| +Fault family: **lock-ownership evidence**. |
| + |
| +**P2 — Rejected conditional writes can produce false violations.** |
| +Evidence: `internal/console/view_test.go:76–83` invokes the witness before knowing whether the store accepts the write; `internal/console/record_write_lock_test.go:78–82` compares its body against independently read current bytes. |
| + |
| +For example, a permitted region-only update (`internal/console/fleets.go:694`) reads channel A, then a locked channel change writes B. The region update attempts its old body with its old ETag. The witness reports an unlocked channel change, although the store rejects that stale write and the update retries (`internal/provision/provision.go:1097–1110`). No follower was added. |
| + |
| +Fix: evaluate accepted writes against the predecessor they actually replaced, and add a regression covering this losing-write interleaving. |
| +Fault family: **committed-write evidence**. |
| + |
| +**P3: None.** |
| + |
| +I found no live follower-writing bypass or newly introduced lock-order inversion in the production paths inspected. Retirement holds the app lock through its final listings and pointer deletion; the newly locked store operations receive deadline-bound contexts. The root-read census has no production-source change in scope. |
| + |
| +By source inspection, the concurrency test distinguishes main’s unlocked writers from this implementation. Execution remains unverified: the focused Go test could not start because the sandbox denied its build directory. No full suites ran, and I edited no files. |
| + |
| +READY AFTER FIXES |
| \ No newline at end of file |
diff --git a/internal/console/record_write_lock_test.go b/internal/console/record_write_lock_test.go| index eaab32eba..d01c5190c 100644 |
| --- a/internal/console/record_write_lock_test.go |
| +++ b/internal/console/record_write_lock_test.go |
| @@ -1,9 +1,12 @@ |
| package console |
| import ( |
| - "context" |
| "encoding/json" |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| "net/http" |
| + "path/filepath" |
| "runtime" |
| "strings" |
| "sync" |
| @@ -34,13 +37,13 @@ func witnessRecordWritesLocked(t *testing.T, srv *Server, rel release.Store) { |
| done bool |
| violations []string |
| ) |
| - w := func(key string, body []byte) { |
| + w := func(key string, body, prev []byte) { |
| acct, app, ok := recordOwnerOfKey(key) |
| if !ok { |
| return |
| } |
| caller, console := consoleCaller() |
| - if !console || !followerWrite(ds, key, body) || witnessLockHeld(ds, acct+"/"+app) { |
| + if !console || !followerWrite(body, prev) || (witnessLockHeld(ds, acct+"/"+app) && stackHoldsAppLock(t)) { |
| return |
| } |
| mu.Lock() |
| @@ -60,11 +63,11 @@ func witnessRecordWritesLocked(t *testing.T, srv *Server, rel release.Store) { |
| }) |
| } |
| -// followerWrite reports whether writing body at key could make the record |
| -// follow a channel it does not follow now: no record there yet, or another |
| -// channel. A tombstone (retired) follows nothing. Anything it cannot read |
| -// counts as one. |
| -func followerWrite(ds *deletableStore, key string, body []byte) bool { |
| +// followerWrite reports whether a write that replaced prev (nil: no record |
| +// there) with body could make the record follow a channel it did not |
| +// follow before: no record there yet, or another channel. A tombstone |
| +// (retired) follows nothing. Anything it cannot read counts as one. |
| +func followerWrite(body, prev []byte) bool { |
| var next, cur struct { |
| Channel string `json:"channel"` |
| Retired bool `json:"retired"` |
| @@ -75,13 +78,82 @@ func followerWrite(ds *deletableStore, key string, body []byte) bool { |
| if next.Retired { |
| return false |
| } |
| - prev, _, err := ds.Store.Get(context.Background(), key) |
| - if err != nil || json.Unmarshal(prev, &cur) != nil { |
| + if prev == nil || json.Unmarshal(prev, &cur) != nil || cur.Retired { |
| return true |
| } |
| return cur.Channel != next.Channel |
| } |
| +// stackHoldsAppLock reports whether the calling goroutine is inside a |
| +// function that holds an app lock for its whole body: one that defers |
| +// s.lockApp or s.lockAppHost, or withAppHost and withRecordLock. With |
| +// witnessLockHeld, that is the writing goroutine's own lock, not one some |
| +// other goroutine (a retire) happens to hold. |
| +func stackHoldsAppLock(t *testing.T) bool { |
| + holders := appLockHolders(t) |
| + pcs := make([]uintptr, 128) |
| + n := runtime.Callers(2, pcs) |
| + frames := runtime.CallersFrames(pcs[:n]) |
| + for { |
| + f, more := frames.Next() |
| + name := f.Function[strings.LastIndex(f.Function, ".")+1:] |
| + if strings.Contains(f.Function, "/internal/console.") && holders[name] { |
| + return true |
| + } |
| + if !more { |
| + return false |
| + } |
| + } |
| +} |
| + |
| +var ( |
| + appLockHoldersOnce sync.Once |
| + appLockHoldersSet map[string]bool |
| +) |
| + |
| +// appLockHolders is the console's functions that hold an app lock for their |
| +// whole body, by name, parsed from the package source once. |
| +func appLockHolders(t *testing.T) map[string]bool { |
| + appLockHoldersOnce.Do(func() { |
| + set := map[string]bool{"withAppHost": true, "withRecordLock": true} |
| + fset := token.NewFileSet() |
| + files, err := filepath.Glob("*.go") |
| + if err != nil { |
| + panic(err) |
| + } |
| + for _, file := range files { |
| + if strings.HasSuffix(file, "_test.go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(fset, file, nil, 0) |
| + if err != nil { |
| + panic(err) |
| + } |
| + for _, d := range f.Decls { |
| + fd, ok := d.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + for _, st := range fd.Body.List { |
| + ds, ok := st.(*ast.DeferStmt) |
| + if !ok { |
| + continue |
| + } |
| + inner, ok := ds.Call.Fun.(*ast.CallExpr) |
| + if !ok { |
| + continue |
| + } |
| + if sel, ok := inner.Fun.(*ast.SelectorExpr); ok && (sel.Sel.Name == "lockApp" || sel.Sel.Name == "lockAppHost") { |
| + set[fd.Name.Name] = true |
| + } |
| + } |
| + } |
| + } |
| + appLockHoldersSet = set |
| + }) |
| + return appLockHoldersSet |
| +} |
| + |
| // witnessLockHeld reports whether the app lock key is held on any console |
| // the test built over ds (witnessServer: the fixture's own, and a restarted |
| // one), since the witness cannot tell which server made the write. |
| @@ -235,3 +307,34 @@ func TestRetireSerialisesWithFollowerWrites(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// #407 review (Astra round 1, P2 1): the witness takes the lock as held only |
| +// when the writing goroutine itself is inside a lock holder. A lock another |
| +// goroutine holds (a retire in flight) does not vouch for an unlocked write. |
| +func TestRecordWriteWitnessNeedsTheWritersOwnLock(t *testing.T) { |
| + srv := &Server{} |
| + if h := appLockHolders(t); !h["createRecordMarked"] || !h["createSidecarMarked"] || !h["sweepTrashedApp"] { |
| + t.Fatalf("the holder set misses known holders: %v", appLockHolders(t)) |
| + } |
| + held := make(chan struct{}) |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + _ = srv.withRecordLock("abc", "shop", func() error { |
| + if !stackHoldsAppLock(t) { |
| + t.Error("inside withRecordLock the writer's own lock is not seen") |
| + } |
| + close(held) |
| + <-done |
| + return nil |
| + }) |
| + }() |
| + <-held |
| + if !srv.appLocks.held("abc/shop") { |
| + t.Fatal("the probe's lock is not held") |
| + } |
| + if stackHoldsAppLock(t) { |
| + t.Fatal("a goroutine outside every lock holder counted another goroutine's lock as its own") |
| + } |
| + done <- struct{}{} |
| +} |
diff --git a/internal/console/view_test.go b/internal/console/view_test.go| index 0041f304d..b70331db1 100644 |
| --- a/internal/console/view_test.go |
| +++ b/internal/console/view_test.go |
| @@ -41,9 +41,10 @@ type deletableStore struct { |
| // witness, when set, sees each Delete and DeleteIfMatch before the hook |
| // above: newConsoleFixtureWith's deletion-evidence check (#415). |
| witness atomic.Pointer[func(key string)] |
| - // putWitness, when set, sees each Put before the hooks above: |
| + // putWitness, when set, sees each Put the store accepted, with the body |
| + // that was at the key just before (nil for none): |
| // newConsoleFixtureWith's app-lock check on record writes (#407). |
| - putWitness atomic.Pointer[func(key string, body []byte)] |
| + putWitness atomic.Pointer[func(key string, body, prev []byte)] |
| } |
| func (d *deletableStore) setGetHook(h func(ctx context.Context, key string) error) { |
| @@ -74,13 +75,18 @@ func (d *deletableStore) runDeleteHook(ctx context.Context, key string) error { |
| } |
| func (d *deletableStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| - if w := d.putWitness.Load(); w != nil && *w != nil { |
| - (*w)(key, body) |
| - } |
| if h := d.putHook; h != nil { |
| h(ctx, key) |
| } |
| + var witnessPrev []byte |
| + w := d.putWitness.Load() |
| + if w != nil && *w != nil { |
| + witnessPrev, _, _ = d.Store.Get(ctx, key) |
| + } |
| etag, err := d.Store.Put(ctx, key, body, ifMatch) |
| + if w != nil && *w != nil && err == nil { |
| + (*w)(key, body, witnessPrev) |
| + } |
| if h := d.afterPut; h != nil && err == nil { |
| h(ctx, key) |
| } |