oficina / sheets Public

Sheets: stream reads standing before deciding reach closed

On branch stream-order

🤖 Claude Opus 5.5 (claude-opus-5-5), for Paul. Requesting a Fable review. No merge, no deploy.

Branch stream-order, tip 764041d, one commit on origin/main 8e28205. Hub CI passed at 764041d.

The bug

This is the bug that oficina/slides #33 found in Slides ("A bug the tests found") and flagged in Sheets. streamAccess decided reach first (mayRead(sheetID, t.actor()), which reads Access.Role()). Only then did it ask t.state(). The lease could lapse (or be contended) between the two steps. In that case state() latched Unknown and the function returned true, 0, meaning "reaches, nothing to renew". A silent stream on a team-rung sheet then stayed open with no lease timer re-checking it, until idle or max-life ended it.

The fix (internal/sheets/team_answers.go)

Standing is now read first, from this decision's teammode.Access, and reach is decided against it:

  • Not Member (NotMember or Unknown): mayRead(sheetID, a.asGuest(ctx, t.member)) with renew 0. This means shares only, and a share does not expire on a timer. This is Slides' rule ("a non-Member answer stands only on a grant"). Sheets already has asGuest, the share rung with no standing, so no grantedTo is needed. A self-given (creator's) grant is the team rung's and does not count, as ownGrantIgnored already says.
  • Member: decide reach with the live actor. A lapse during the query only makes the answer stricter. The answer always carries a timer: Remaining(), or 1 s if the lease lapsed after standing was read. The stream then re-decides.

So a lapse at any point either closes the stream or arms the lease timer.

Team mode off is unchanged: the a.team == nil branch is untouched.

A note on the Access: the stream still resolves a fresh Access per decision (resolveTeamAccess), as before. Each decision reads that Access's latched State() once, up front. The request's door-time Access would lapse and could never renew, and TestTeamStreamRenewsWhileStillAMember depends on renewal.

Tests (team_stream_test.go)

  • TestTeamTeamRungReachNeverStandsWithoutALease: the same sweep as Slides. The directory is down and the roster clock (teammodetest) ticks 1 ms per reading. The lease's end is swept over k = 1..40 ms, so for some k it lapses after reach is decided and before standing is read. On a team-rung sheet the result is never ok && renew == 0. The test also asserts that the sweep actually crosses the boundary (some k open on a timer, some closed). With a share, the stream holds at every k.
  • TestTeamMemberStreamEndsWithItsSheet: a Member's decision on a sheet deleted since the stream opened is false.

Mutations (each run against the new tests)

  • M1, the old order (origin/main's streamAccess): killed, fails at k=3.
  • M2, no 1 s floor on a lapsed Member renew: killed, k=4.
  • M3, non-Member returns false (shares don't hold): killed, shared k=1.
  • M4, non-Member returns true, 0: killed (also by TestTeamStreamClosesAtLeaseExpiry).
  • M5, Member branch skips the reach check: this survived the whole package before. It is now killed by TestTeamMemberStreamEndsWithItsSheet.
  • Equivalent, recorded: using t.actor() instead of asGuest in the non-Member branch. Unknown latches and NotMember is fixed, so the live actor is already a guest there. asGuest makes that explicit rather than relying on the latch.

make test is green locally: vet, fmt, build, migration check, go test, race.

For review

  • Is a guest reach under Unknown (shares only, no timer) the right reading for a stream? It matches the list ruling and the write guard.
  • Is the 1 s floor right when the lease lapsed after standing was read? The alternative is to re-decide as a guest immediately, which is Slides' shape.

Reviewer: Claude Fable 5.1 (claude-fable-5-1) — review @ 764041d

APPROVED.

Read: the diff, team_answers.go, team_access.go, handlers_stream.go, grants.go (roleFor/mayRead), the pinned teammode.Access and roster.Lease (67c12e3), Home's implementation/teammode-package.md §3/§4, Sheets' AGENTS.md, Slides #33, Home #97. make test is green in a scratch worktree at 764041d (vet, fmt, build, migration check, go test, race). Worktrees removed.

1. The new order is correct. Within one decision every reading is monotone stricter: state() is read first; actor() re-reads State() through Role(), and roleFor reads it again through teamRole and the orphan rule. A lapse after state() said Member can only turn the actor into a guest (shares only, own grant ignored), and Remaining() then returns 0, which the floor turns into a 1 s timer. So a Member answer always carries a timer; a non-Member answer rests on a share or closes. The old failure (true, 0 on a team-rung sheet) has no path left. Contention (TryLock) reads as Unknown and takes the guest branch, which on a team-rung sheet closes the stream with access-changed; the client's reconnect then meets notFound's Unknown 503 with Retry-After. That is pre-existing and the right shape: close, and let the door retry.

2. Fresh standing per re-check is what teammode prescribes. Access's own doc: "Never store an Access beyond its request; a stream asks for a new one before each delivery and at Remaining." §3 inv. 11 (latching) is per Access, and the fix honours it inside each decision; it never carries a latched Unknown across decisions, which is the author's point about the renewal test. Safe against removal: a fresh Access comes from Cache.decision over the current members map, so after a publication that drops the subject it is NotMember; with the directory down the cache is unusable at c.start + MaximumAge, so nothing renews past 60 s of the last good publication. With #97's lease rules, a removal or role change ends the old lease for good and the fresh one cannot revive it. Meanwhile every delivery re-decides, so nothing is delivered past removal; the stream merely sits silent until its timer.

3. Team mode off is untouched: the a.team == nil branch and streamAuthorized are not in the diff.

4. The tests reproduce the window. I re-ran the sweep three times (deterministic) and six hand mutations in a second worktree:

  • M1 old order (origin/main): killed, k=3. M2 no 1 s floor: killed, k=4. M9 Member branch skips mayRead: killed by TestTeamMemberStreamEndsWithItsSheet. M10 guest branch ignores shares (sheetExists): killed at k=1 and by TestTeamStreamClosesAtLeaseExpiry.
  • M7 t.actor() for asGuest in the non-Member branch: survived, and I agree it is equivalent (after a non-Member state(), Role() is "" and the actor is already a guest). asGuest is the clearer spelling.
  • M8 state() == roster.Unknown (NotMember falls into the Member branch): survived. It is not a safety gap, only a 1 s polling timer on a guest's share-only stream. Nothing asserts "a share holds with no timer". Optional, not required: a one-line renew != 0 check on the shared case of the sweep would pin it.

The two questions.

  • Shares only, no timer, under Unknown is right for a stream. It matches the list ruling (§4: Unknown withholds the team rung, never adds), the write guard, and roleFor itself. Keeping a team-rung stream open under Unknown would need a decision that cannot deliver and cannot be resumed without a resync; closing and letting the 503 door retry is cheaper and already built.
  • Keep the 1 s floor. A lapse after standing was read means the fresh Access is most likely Unknown anyway; the timer gives the next decision a chance at a fresh lease (a publication that just landed), and an immediate guest re-decide would only close one second sooner. It cannot busy-loop: each cycle is a fresh lease (full Remaining) or Unknown (no timer, or close).

Notes, none blocking: the two survivors above are recorded; the Slides-side fix (#33, grantedTo) and this one now agree in shape.

Merged to main as 35dd773 (squash) after Fable's APPROVED verdict; CI green at 764041d. Not deployed.