console: getting started completion cannot undo a reset
Completed is now written by CompleteGettingStarted, one INSERT ... SELECT that needs both manual steps still marked done or skipped for the same person and account. A page read that saw every step satisfied can no longer store completed over a Show pressed since. SetGettingStartedMark refuses completed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+128
−3
internal/console/gettingstarted.go+7 −1internal/console/gettingstarted_page_test.go+29 −0internal/console/gettingstarted_store.go+38 −1internal/console/gettingstarted_store_test.go+51 −1internal/console/server.go+3 −0
diff --git a/internal/console/gettingstarted.go b/internal/console/gettingstarted.go| index 5e13acaee..30bc75eca 100644 |
| --- a/internal/console/gettingstarted.go |
| +++ b/internal/console/gettingstarted.go |
| @@ -165,7 +165,13 @@ func (s *Server) fillGettingStarted(p *page, acct *Account, addr, role string, a |
| } |
| view, complete := buildGettingStarted(marks, connected, anyLive, role, firstApp, s.consoleName()) |
| if complete { |
| - if err := s.store.SetGettingStartedMark(acct.ID, addr, gsCompleted, now); err != nil { |
| + if s.gsBeforeCompleteHook != nil { |
| + s.gsBeforeCompleteHook() |
| + } |
| + // Conditional on the manual steps' marks still being there: a Show |
| + // pressed since the read above wins (CompleteGettingStarted). Either |
| + // way this response offers Show; the next one reads the live state. |
| + if _, err := s.store.CompleteGettingStarted(acct.ID, addr, now); err != nil { |
| log.Printf("console: getting started completed %s: %v", acct.Sqid, err) |
| } |
| p.ShowGettingStarted = true |
diff --git a/internal/console/gettingstarted_page_test.go b/internal/console/gettingstarted_page_test.go| index 12ee8c941..e52fd4b00 100644 |
| --- a/internal/console/gettingstarted_page_test.go |
| +++ b/internal/console/gettingstarted_page_test.go |
| @@ -222,6 +222,35 @@ func TestGettingStartedCompletionSticks(t *testing.T) { |
| } |
| } |
| +// A Show pressed while an account-home read is in flight wins: the read |
| +// saw every step satisfied, then the person reset, then the read's |
| +// completion write lands. It must not store completed over the reset. |
| +func TestGettingStartedResetBeatsStaleCompletion(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + gsStaticSite(t, fx, "site") |
| + cliToken(t, fx, "paul@keymail.dev", "code-gs-race-cli") |
| + paul, _ := signInAs(t, fx, "paul@keymail.dev", "code-gs-race") |
| + for _, m := range []gsMark{gsSecretsDone, gsDomainSkip} { |
| + if err := fx.st.SetGettingStartedMark(OpsAccountID, "paul@keymail.dev", m, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + fx.srv.gsBeforeCompleteHook = func() { |
| + if err := fx.st.ResetGettingStarted(OpsAccountID, "paul@keymail.dev"); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + gsHome(t, fx, paul, "") |
| + fx.srv.gsBeforeCompleteHook = nil |
| + if gsMarksOf(t, fx, "paul@keymail.dev")[gsCompleted] { |
| + t.Fatal("a stale read stored completed over the reset") |
| + } |
| + body := gsHome(t, fx, paul, "") |
| + if got := gsRenderedState(body, "secrets"); got != "open" { |
| + t.Fatalf("after the reset, secrets = %q, want open (card shown: %v)", got, gsCardShown(body)) |
| + } |
| +} |
| + |
| // Browser domain attach is owner-only: with the domain step open, the owner |
| // gets the settings link, a member only the CLI route. |
| func TestGettingStartedMemberNoDomainLink(t *testing.T) { |
diff --git a/internal/console/gettingstarted_store.go b/internal/console/gettingstarted_store.go| index e0d966ce3..f7278bfa7 100644 |
| --- a/internal/console/gettingstarted_store.go |
| +++ b/internal/console/gettingstarted_store.go |
| @@ -1,6 +1,9 @@ |
| package console |
| -import "time" |
| +import ( |
| + "errors" |
| + "time" |
| +) |
| // gsMark is one person's note on the Getting started checklist in one |
| // account. |
| @@ -52,9 +55,17 @@ func (s *Store) GettingStartedMarks(accountID int64, address string) (map[gsMark |
| return out, rows.Err() |
| } |
| +// errGettingStartedCompleted: completed is never set unconditionally; see |
| +// CompleteGettingStarted. |
| +var errGettingStartedCompleted = errors.New("getting started: completed is written by CompleteGettingStarted") |
| + |
| // SetGettingStartedMark records m; repeating it is a no-op. Setting a done |
| // mark clears its skip mark, and the other way round, in one transaction. |
| +// It refuses gsCompleted. |
| func (s *Store) SetGettingStartedMark(accountID int64, address string, m gsMark, now time.Time) error { |
| + if m == gsCompleted { |
| + return errGettingStartedCompleted |
| + } |
| tx, err := s.db.Begin() |
| if err != nil { |
| return err |
| @@ -72,6 +83,32 @@ ON CONFLICT (account_id, address, mark) DO NOTHING`, accountID, address, string( |
| return tx.Commit() |
| } |
| +// CompleteGettingStarted records completed for address in the account, but |
| +// only while both manual steps are still satisfied in the database (secrets |
| +// done or skipped, and domain done or skipped), checked in the same |
| +// statement as the write. A page read that saw every step satisfied cannot |
| +// then undo a Show pressed since: the reset deleted those marks, so the |
| +// write finds nothing to stand on. ok reports whether completed is stored |
| +// (already present counts). |
| +func (s *Store) CompleteGettingStarted(accountID int64, address string, now time.Time) (ok bool, err error) { |
| + if _, err := s.db.Exec(`INSERT INTO getting_started_marks (account_id, address, mark, marked_at) |
| +SELECT ?, ?, ?, ? |
| +WHERE EXISTS (SELECT 1 FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark IN (?, ?)) |
| + AND EXISTS (SELECT 1 FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark IN (?, ?)) |
| +ON CONFLICT (account_id, address, mark) DO NOTHING`, |
| + accountID, address, string(gsCompleted), now.Unix(), |
| + accountID, address, string(gsSecretsDone), string(gsSecretsSkip), |
| + accountID, address, string(gsDomainDone), string(gsDomainSkip)); err != nil { |
| + return false, err |
| + } |
| + var n int |
| + if err := s.db.QueryRow(`SELECT COUNT(*) FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark = ?`, |
| + accountID, address, string(gsCompleted)).Scan(&n); err != nil { |
| + return false, err |
| + } |
| + return n > 0, nil |
| +} |
| + |
| // ResetGettingStarted deletes every mark address has in the account (Show). |
| func (s *Store) ResetGettingStarted(accountID int64, address string) error { |
| _, err := s.db.Exec(`DELETE FROM getting_started_marks WHERE account_id = ? AND address = ?`, accountID, address) |
diff --git a/internal/console/gettingstarted_store_test.go b/internal/console/gettingstarted_store_test.go| index 1167dec71..1403fcf7e 100644 |
| --- a/internal/console/gettingstarted_store_test.go |
| +++ b/internal/console/gettingstarted_store_test.go |
| @@ -47,11 +47,14 @@ func TestGettingStartedDoneReplacesSkip(t *testing.T) { |
| func TestGettingStartedReset(t *testing.T) { |
| st := openTestStore(t) |
| now := time.Now() |
| - for _, m := range []gsMark{gsHidden, gsCompleted, gsSecretsDone, gsDomainSkip} { |
| + for _, m := range []gsMark{gsHidden, gsSecretsDone, gsDomainSkip} { |
| if err := st.SetGettingStartedMark(1, "a@example.com", m, now); err != nil { |
| t.Fatal(err) |
| } |
| } |
| + if ok, err := st.CompleteGettingStarted(1, "a@example.com", now); err != nil || !ok { |
| + t.Fatalf("complete: %v %v", ok, err) |
| + } |
| if err := st.SetGettingStartedMark(1, "b@example.com", gsHidden, now); err != nil { |
| t.Fatal(err) |
| } |
| @@ -116,3 +119,50 @@ func TestGettingStartedCleanup(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Completed is written only while both manual steps are still satisfied in |
| +// the database, in the same statement, and SetGettingStartedMark refuses it. |
| +func TestGettingStartedCompleteConditional(t *testing.T) { |
| + st := openTestStore(t) |
| + now := time.Now() |
| + const addr = "a@example.com" |
| + complete := func(want bool) { |
| + t.Helper() |
| + ok, err := st.CompleteGettingStarted(1, addr, now) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + got, _ := st.GettingStartedMarks(1, addr) |
| + if ok != want || got[gsCompleted] != want { |
| + t.Fatalf("complete = %v, stored %v, want %v (marks %v)", ok, got[gsCompleted], want, got) |
| + } |
| + } |
| + complete(false) // nothing marked |
| + if err := st.SetGettingStartedMark(1, addr, gsSecretsDone, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + complete(false) // domain unmarked |
| + // Another person's or account's domain mark does not count. |
| + if err := st.SetGettingStartedMark(2, addr, gsDomainSkip, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := st.SetGettingStartedMark(1, "b@example.com", gsDomainSkip, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + complete(false) |
| + if err := st.SetGettingStartedMark(1, addr, gsDomainSkip, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + complete(true) |
| + complete(true) // repeating it is a no-op |
| + if err := st.ResetGettingStarted(1, addr); err != nil { |
| + t.Fatal(err) |
| + } |
| + complete(false) |
| + if err := st.SetGettingStartedMark(1, addr, gsCompleted, now); err == nil { |
| + t.Fatal("SetGettingStartedMark wrote completed unconditionally") |
| + } |
| + if got, _ := st.GettingStartedMarks(1, addr); got[gsCompleted] { |
| + t.Fatal("refused completed was stored") |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index 799359a79..c014d759f 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1545,6 +1545,9 @@ type Server struct { |
| // the follower check and the final record comparison, holding the app |
| // lock. Tests only. |
| retireBeforeDeleteHook func() |
| + // gsBeforeCompleteHook, when set, runs on the account home between the |
| + // Getting started marks read and the completion write. Tests only. |
| + gsBeforeCompleteHook func() |
| // startedAt overrides when this console started (zero: the process |
| // start, processStart; tests set it), and deletions is when it last |
| // began deleting an instance or sidecar record of each app (acct + "/" |