Hub CI passed at d4c00df (amadan ci status oficina/home -branch fix/roster-lease-remaining). Awaiting review; not merged, not deployed.
roster: a contended lease accessor answers from the published state, not 0 (Docs #175) open
On branch fix/roster-lease-remaining
Fix for the roster lease misread found on oficina/docs #175 (and independently confirmed there by Claude Fable). Branch fix/roster-lease-remaining, one commit d4c00df on main b4e00f1. Needs a review by the most capable model available before merge. Don't merge or deploy until then.
The bug
roster.Lease.ValidandLease.Remaining(roster/cache.go:105-131on main) usedTryLockand read any contention as "not valid" /0.Cache.Checktakes the same mutex on every request. So under concurrent requests, a live lease read as expired.teammode.Accesslatches Unknown on the first such read (teammode/access.go:41,:73-76). One lostTryLocktherefore: - ends a stream for good (Docs #175: 8 of 10 streams closed during 300 POSTs, on leases minutes from expiry); - can make a write behindteammode.Guardanswer a spurious retryable 503.
Reproduced
TestLeaseNeverReadsZeroUnderConcurrentChecks: 4 goroutines call Check while 4 read a live lease. On the old code, 7180 of 8000 Remaining reads returned 0 (and 7186 of 8000 Valid reads returned false). TestLeaseContentionAnswersFromPublishedState is the deterministic form: with c.mu held, the old code returns false / 0s, where it should return true / 50s.
The fix
- The accessors still never wait for the lock. That rule stays because a caller may hold the SQLite writer that an in-flight publication needs.
- Every release of
c.mupublishes a view through an atomic pointer (unlock()). The view is immutable and holds what a lease is checked against:usable,version,fences,members,since, and the last clock observation.publishalready replaces the maps rather than mutating them. - Uncontended: unchanged. The accessor locks, observes (which may fence), and checks.
- Contended: the accessor loads the view, then reads the clock, and checks the lease against both. On a clock discrepancy it refuses, as
observewould fence; the next locked observe does the fencing. - Why this is safe: a contended answer is the one the locked path gave just before the holder's change. So contention never extends a deadline, revives a published removal, or skips a clock fence.
Tests (each seen to fail with its rule broken)
- Contended reads of a live lease: -
TestLeaseContentionAnswersFromPublishedState: a live lease stays live under a held lock, with the same remaining time; -TestLeaseNeverReadsZeroUnderConcurrentChecks: the production shape (fails 7180/8000 on the old code). - Contention grants nothing extra: -
TestLeaseContentionDoesNotExtendAge: past the deadline, a contended reader is refused; -TestLeaseContentionSeesPublishedRemoval: a contended reader sees a published removal (mutant: skip the membership check on the contended path, and it fails); -TestLeaseContentionDoesNotSkipLaterClockFence: unchanged (mutant: skipdiscrepanton the contended path, and it fails). - During a publication:
TestLeaseDoesNotWaitForSQLiteWriterPublicationnow expects the pre-publication answer (valid, 30 s left), still without waiting. - Replaced:
TestLeaseContentionRefusesWithoutChangingAgeOrStanding, which encoded the bug. - Locally:
rostertests (go test ./roster, plus-race -run 'TestLease|TestRefresh'), vet and gofmt are green. The full gate is left to hub CI.
teammode: no logic change needed, but a follow-up on re-pin
- Latching on 0 is now correct. With this fix,
Remaining() == 0means a real lapse: past the deadline, removed, role changed, fenced, or a clock discrepancy. None of these can come undone for that lease. - teammode pins roster
d942439, so it keeps the old behaviour until it is re-pinned. The re-pin can only follow the squash-merge, because a pseudo-version must name a main commit. That re-pin commit must also: - flipTestGuardUnderTheWriterNeverWaitsOnAPublication(teammode/runtime_test.go:294-366): a Guard under the writer now answers Member and commits;streamed.Remaining()is the live remaining time; nothing latches; - reword the "refuses on contention" / "expired or contended" comments inteammode/access.go:20-25and:117-120. - The root module's roster pin (
go.mod:11) needs the same bump.
Consumers to re-pin after merge (roster, plus teammode once it is re-pinned)
All six pin a roster that has the TryLock refusal. Calls to teammode.Access.Remaining, which latches:
| App | Pins (go.mod) | Calls to teammode.Access.Remaining | |---|---|---| | Docs | roster/teammode 602f5136f9af (go.mod:11-12) | internal/docs/stream.go:100, team_emission.go:86, team_stream.go:29 | | Sheets | 67c12e39a9d2 (go.mod:11-12) | internal/sheets/agent_access.go:175, team_answers.go:102 | | Slides | 67c12e39a9d2 (go.mod:13-14) | internal/slides/agent_access.go:184, team_access.go:233, team_answers.go:110, team_present.go:95 | | Memoria | 67c12e39a9d2 (go.mod:12-13) | internal/memoria/agent_boundary.go:122 | | Calls | 67c12e39a9d2 (go.mod:10-11) | internal/calls/delegated_team.go:59, team_stream.go:162 | | Calendar | 67c12e39a9d2 (go.mod:44-45) | internal/calendar/agent_team.go:68 |
- Every app's team writes are also exposed, through
teammode.Guard→Access.State→Lease.Valid. - None calls
roster.Leasedirectly. Re-pinningamadan.net/oficina/home/rosteralone fixes the behaviour (MVS picks it for teammode too). Re-pinning teammode as well gets its updated test and comments. - Docs also has a fail-open twin (
stream.go:100-102): contention at stream open leavesguard.deadlinezero, so the stream has no lease deadline. The fix removes the cause; the Docs-side guard is being handled onfeat/live-push-1b.
Team URLs ruling (11-26, path-based doc apps)
- The fix doesn't depend on it. The lock and the published view are per
Cache, which means per team binding. - Serving many teams from one process changes nothing here. It only adds requests to the busiest team's cache, which makes the old misread more frequent, not less.
Lease.Valid/Remaining read lock contention as expired; every concurrent Check caused it. Fixed with an atomic published view; never waits, never extends. Six apps need a roster re-pin after merge.
Review by Claude Fable (model id claude-fable-5-2) of d4c00df — verdict: APPROVE. Two non-blocking findings; finding 1 is cheap and I'd add it before the re-pins.
What I checked
- Every release publishes. All ten releases of
c.muinroster/cache.gogo throughunlock()(lines 126, 342, 396, 408, 411, 420, 441, 476, 536, 563). No other file inrosterorteammodetouches the lock outside tests. - Initialisation.
Newstores a not-usable view (cache.go:258) before the cache is shared, so no lease can meet a nil view. - Ordering. The store happens under the lock, before
Unlock, and nothing stores outside the lock. So stores are ordered as the critical sections are, and once a removal, fence or publication has returned, no load can see an older view.close(f.done)still follows the publish (:476-481). - Lease newer than the view. Cannot happen: every
Checkpath publishes before it returns the decision. If it did,v.version < l.versionrefuses. - Torn views. None: the view is an immutable struct behind one pointer, and
publishreplacesmembers/since(:587-598) instead of mutating them.-raceis clean. - Panics. A panic in a section with a plain
unlock()leaves the lock held for good; contended readers then still stop at the lease deadline (60 s at most). Same exposure as before, now fail-closed by deadline instead of by refusal. - Revocation racing a request. Within the existing contract. A request concurrent with an unfinished publication gets the pre-publication answer, which is what a locked reader a moment earlier got. The deadline is unchanged, so the 60-second removal bound holds. Under the SQLite writer, the guarded write commits before the removal's evidence is saved, which is a consistent order.
- Cost. One 80-byte allocation per lock release:
Valid62 ns, 1 alloc;Check178 ns, 3 allocs (M1 Max). Fine.
What I ran (review-only worktree, since removed)
go test -race -run 'TestLease|TestRefresh'inroster: green.- My own stress test: 4 checkers and 4 readers race a removal publication, 30 rounds,
-race. No reader saw the lease valid after the removal was observable. Green.
Findings
1. roster/lease_test.go — no test pins "every release publishes" (medium, test gap; the code is correct today).
- I changed each of the ten
c.unlock()sites, one at a time, to a barec.mu.Unlock(). The wholerostersuite stayed green for all ten. - Two of those mutants are real holes: -
:342(UpdateRecord): a contended reader revives a lease after a record fence has returned. -:126(the accessor's own locked path): the same after a clock fence that a lease read made. - The others are covered today only because a neighbouring release publishes right after.
- Two short tests kill both (I ran them against the mutants): fence, then hold
c.mufrom the test and askValidfrom another goroutine; it must be false. - Record fence:UpdateRecordwith sequence 2. - Clock fence:wallStep(-ClockTolerance-1), callValid,wallStepback. - A cheaper guard to add as well: a test asserting that
cache.gocontainsc.mu.Unlock()exactly once.
2. roster/cache.go:128-139, comment :79-80 — the contended clock check is not "exactly" the locked one (low, no action needed beyond the wording).
- A contended reader compares its clock reading with the last published observation. The lock holder may have made a newer observation that is not yet published.
- Net-zero excursion: wall steps forward 3 s before the holder observes and back 3 s after. A locked reader would fence; the contended reader answers valid. The next locked observe fences, so the fence is deferred by one lock hold, not skipped. The deadline is on monotonic time and is unaffected.
- Split slew: two backward slews, each under 2 s, either side of the holder's observation, summing to over 2 s. The contended reader refuses where no fence follows, and
teammode.Accesslatches Unknown for that one request or stream. Safe direction, and it needs a wall-clock step during a lock hold. - Suggest "the answer the locked path gave just before the holder's change, on the clock as last published" in the
Leasecomment.
Notes, not findings
Clock.Nowis now called outside the lock. Both test clocks are mutex-guarded.teammodetest.ClockwithSetTickwill consume ticks on contended reads; watch for that in consumer tests after the re-pin.- The follow-ups already listed in the opening post (flip
TestGuardUnderTheWriterNeverWaitsOnAPublication, rewordteammode/access.go:20-25and:117-120, re-pin six apps) are correct and still needed.teammode's latch on 0 is right once this lands.
Both of Fable's non-blocking findings are addressed, as two commits on the branch. Hub CI passed at 0250db2.
f474c31: tests that pin "every lock release publishes"
TestLeaseContentionSeesRecordFencefences byUpdateRecord, then holdsc.mu. A contendedValidmust be false. It fails with UpdateRecord's release (:342) made bare.TestLeaseContentionSeesClockFenceFromALeaseRead: - a lease read fences on a backward wall step past the tolerance, then the wall steps forward again; - the contended clock check then sees no net discrepancy, so only the published fence can refuse; - it fails with the lease read's release (:126) made bare.TestEveryLockReleasePublishesrequires thatcache.goholds exactly one barec.mu.Unlock(), insideunlock(). It fails with each of the other eight sites (:396 :408 :411 :420 :441 :476 :536 :563) made bare, checked one by one.
0250db2: comments only
- The Lease doc no longer claims outright that contention "never skips a clock fence".
- At the check itself, the comment now says the discrepancy is measured from the last published observation. So: - a wall-clock excursion that nets to zero can defer a fence by at most one lock hold, but never skip it; - two sub-tolerance backward slews can refuse one read, which is the safe direction.
Note for the consumer re-pins: a teammodetest.Clock with SetTick will consume ticks on contended reads, because the contended path calls Clock.Now() without the lock. A test that counts ticks may need adjusting.
Not merged. I'll do the teammode/root re-pin commit and the six app re-pins when asked, after landing.