oficina / sheets Public

Sheets: stream reads team standing before deciding reach. streamAccess now reads standing first: a NotMember or Unknown person reaches the sheet only through a share (asGuest, no timer); a Member's reach always comes with a timer (the lease's Remaining, or 1 s if it lapsed after standing was read), so a lease lapsing between the reach decision and the standing read can no longer leave a team-rung stream open and unwatched. Found while porting Slides (slides #33). Team mode off is untouched. Tests sweep the lease end with the teammodetest ticking clock (old order fails at k=3) and cover a deleted sheet. Reviewed by Claude Fable 5.1 (claude-fable-5-1), oficina/sheets #54, APPROVED; CI green at 764041d. Not deployed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from stream-order into main.
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 35dd77363376566fe877e048fd8223f4dcb8ff03 parent 8e28205
2 files changed, +94 −3
  • internal/sheets/team_answers.go +12 −3
  • internal/sheets/team_stream_test.go +82 −0

CI passed — run details

diff --git a/internal/sheets/team_answers.go b/internal/sheets/team_answers.go
index e0eb94d..35941ae 100644
--- a/internal/sheets/team_answers.go
+++ b/internal/sheets/team_answers.go
@@ -76,6 +76,15 @@ func (a *App) teamMemberOnly(w http.ResponseWriter, r *http.Request) bool {
// standing-backed answer may stand before it must be asked again: the
// Member lease's remaining time, or 0 when nothing expires on a timer
// (outside team mode, or when the answer did not rest on a lease).
+//
+// Standing is read first, from this decision's Access, and reach is
+// decided against it. Not Member: only a share reaches, and a share does
+// not expire on a timer. Member: the reach may rest on the team rung, so
+// the answer always carries a timer — the lease's remainder, or a second
+// if it lapsed after standing was read — and the stream re-decides then.
+// Deciding reach first and asking standing after (oficina/slides #33)
+// let a lease that lapsed between the two read as "reaches, nothing to
+// renew", and a silent stream on a team-rung sheet stayed open unwatched.
func (a *App) streamAccess(r *http.Request, sheetID uint64) (bool, time.Duration) {
if a.team == nil {
return a.streamAuthorized(r, sheetID), 0
@@ -84,12 +93,12 @@ func (a *App) streamAccess(r *http.Request, sheetID uint64) (bool, time.Duration
if err != nil {
return false, 0
}
+ if t.state() != roster.Member {
+ return a.mayRead(sheetID, a.asGuest(r.Context(), t.member)), 0
+ }
if !a.mayRead(sheetID, t.actor()) {
return false, 0
}
- if t.state() != roster.Member {
- return true, 0
- }
renew := t.Remaining()
if renew <= 0 {
renew = time.Second
diff --git a/internal/sheets/team_stream_test.go b/internal/sheets/team_stream_test.go
index aacc080..8986a9b 100644
--- a/internal/sheets/team_stream_test.go
+++ b/internal/sheets/team_stream_test.go
@@ -10,6 +10,8 @@ import (
"time"
"amadan.net/oficina/home/roster"
+
+ "amadan.net/oficina/sheets/internal/sheets/store"
)
func openTeamStream(t *testing.T, f *teamFixture, base string, s SheetSummary) *http.Response {
@@ -127,3 +129,83 @@ func TestTeamLeaseEndsAHeldDecision(t *testing.T) {
t.Fatal("an expired lease was not Unknown")
}
}
+
+// A reach through the team rung never stands without a lease. The roster
+// clock ticks a millisecond on every reading, and the lease's end is
+// swept across each reading streamAccess makes in turn, so for some step
+// it lapses after the reach has been decided and before standing is read
+// (oficina/slides #33 found that window in Slides; Sheets had the same
+// order). Whatever the step, the stream either closes or re-decides on a
+// timer, never "reaches, nothing to renew". A share is the one thing
+// that holds without a lease, and it holds at every step.
+func TestTeamTeamRungReachNeverStandsWithoutALease(t *testing.T) {
+ for _, shared := range []bool{false, true} {
+ var opened, closed int
+ for k := 1; k <= 40; k++ {
+ f := newTeamFixture(t)
+ plan := teamSheet(t, f, "Team plan")
+ a := teamSubject('a')
+ f.dir.Set(a, "member")
+ f.signInHome(t, a)
+ if shared {
+ colleague, err := f.app.memberFor("colleague@example.com")
+ if err != nil {
+ t.Fatal(err)
+ }
+ me := f.viewer(t)
+ if err := f.app.db.Create(&Grant{SheetID: plan.ID, MemberID: me.ID, Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ r := httptest.NewRequest("GET", testOrigin+"/api/sheets", nil)
+ for _, c := range f.jar {
+ r.AddCookie(c)
+ }
+
+ f.dir.SetDown(true)
+ f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond)
+ f.clock.SetTick(time.Millisecond)
+ ok, renew := f.app.streamAccess(r, plan.ID)
+ f.clock.SetTick(0)
+
+ switch {
+ case shared && !ok:
+ t.Fatalf("k=%d: a share did not hold through a lapse", k)
+ case !shared && ok && renew <= 0:
+ t.Fatalf("k=%d: a stream on the team rung stands with nothing to renew", k)
+ case ok:
+ opened++
+ default:
+ closed++
+ }
+ }
+ // The sweep must cross the lease's end: some steps decide before
+ // it (open, on a timer) and some after (closed, or a share).
+ if !shared && (opened == 0 || closed == 0) {
+ t.Fatalf("the sweep did not cross the lease's end: %d open, %d closed", opened, closed)
+ }
+ }
+}
+
+// Standing is not reach: a Member's stream on a sheet deleted since it
+// opened ends at the next decision, lease or no lease.
+func TestTeamMemberStreamEndsWithItsSheet(t *testing.T) {
+ f := newTeamFixture(t)
+ plan := teamSheet(t, f, "Team plan")
+ a := teamSubject('a')
+ f.dir.Set(a, "member")
+ f.signInHome(t, a)
+ r := httptest.NewRequest("GET", testOrigin+"/api/sheets", nil)
+ for _, c := range f.jar {
+ r.AddCookie(c)
+ }
+ if ok, renew := f.app.streamAccess(r, plan.ID); !ok || renew <= 0 {
+ t.Fatalf("a member's stream on a team sheet = %v, %v; want open on a lease", ok, renew)
+ }
+ if err := f.app.db.Model(&store.Sheet{}).Where("id = ?", plan.ID).Update("deleted_at", time.Now()).Error; err != nil {
+ t.Fatal(err)
+ }
+ if ok, _ := f.app.streamAccess(r, plan.ID); ok {
+ t.Fatal("a member's stream outlived its deleted sheet")
+ }
+}