console: new-person budget across sign-up paths (social sign-in 1)
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
internal/console/emailsignin.go+9 −0internal/console/emailsignin_limits.go+33 −2internal/console/emailsignin_limits_test.go+106 −5internal/console/newperson_test.go+140 −0internal/console/signincopy.go+3 −0internal/console/socialsignin_copy.go+17 −0internal/console/socialsignin_copy_test.go+54 −0
Intent
<task-notification> <task-id>bpcpliot5</task-id> <tool-use-id>toolu_01Hgn1nzZgntKaBozhqcCyUZ</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1730ddc5-7bb6-4ebb-ab9b-85efe2e437e3/tasks/bpcpliot5.output</output-file> <status>completed</status> <summary>Background command "Fix prompt and run Astra on Task 2" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>a7544c6cfb64c5eda</task-id> <tool-use-id>toolu_01K5PkG8VNrZWLTYYMk6XK7q</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1730ddc5-7bb6-4ebb-ab9b-85efe2e437e3/tasks/a7544c6cfb64c5eda.output</output-file> <status>completed</status> <summary>Agent "Review Task 2 (spec + quality)" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**Spec verdict:** Compliant. Two things I couldn't check from the diff alone: - In later plans, code that reads the Keymail grant by the session's address will look under the person while the grant sits under the proved address. That's harmless in Plan 1, where the two are always equal. - `Keymail.Exchange` returns the address without trimming or lower-casing it, and `ResolveSignIn` stores it as is. Sign-in always worked this way, so it's not new. **Quality verdict:** Approved. **Critical/Important findings:** none. There are three Minor items: the new `method_id` ALTER has no comment of its own, no test covers a mapped Keymail sign-in (grant under the proved address, session under the person), and the audit check reads only the first row. The full review is at `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/console-social-signin/.superpowers/sdd/2026-11-02-console-social-signin-1-methods/task-2-review.md`.</result> <usage><subagent_tokens>63720</subagent_tokens><tool_uses>10</tool_uses><duration_ms>88554</duration_ms></usage> </task-notification>
Attributed to this commit: 11.1 g CO₂e (1,368,605 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-console-social-signin, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/emailsignin.go b/internal/console/emailsignin.go| index ebbbf7bd7..c261d6080 100644 |
| --- a/internal/console/emailsignin.go |
| +++ b/internal/console/emailsignin.go |
| @@ -256,6 +256,15 @@ func safeNext(raw string) string { |
| // for minutes later. The current account is resolved (minted for a new user) |
| // before any redirect, so onboarding runs before next. |
| func (s *Server) finishSignin(w http.ResponseWriter, r *http.Request, proved, keymailToken, method, next string) { |
| + fresh, err := s.store.PersonWouldBeNew(method, proved) |
| + if err != nil { |
| + s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not record your account. Please try again.") |
| + return |
| + } |
| + if fresh && !s.limits.newPerson(s.clientIP(r)) { |
| + s.renderMessage(w, r, http.StatusTooManyRequests, copyText("signup.busy_title"), copyText("signup.busy")) |
| + return |
| + } |
| now := time.Now() |
| m, created, err := s.store.ResolveSignIn(method, proved, now) |
| if err != nil { |
diff --git a/internal/console/emailsignin_limits.go b/internal/console/emailsignin_limits.go| index f9955dc6c..c7c297cad 100644 |
| --- a/internal/console/emailsignin_limits.go |
| +++ b/internal/console/emailsignin_limits.go |
| @@ -21,6 +21,8 @@ const ( |
| signinPerAddrDay = 20 // sign-in emails per address |
| signinPerIPHour = 10 // sign-in emails per IP |
| signinGlobalHour = 200 // sign-in emails console-wide |
| + newPersonPerIPHour = 10 // new people per IP |
| + newPersonGlobalHour = 200 // new people console-wide |
| stepUpPerAddrHour = 10 // step-up emails per address |
| wrongCodesPerAddrDay = 10 // wrong codes per address |
| wrongCodesPerIPHour = 30 // wrong codes per IP |
| @@ -140,7 +142,9 @@ type emailLimits struct { |
| addrWrong keyedWindow // wrong codes, by address |
| ipWrong keyedWindow // wrong codes, by IP |
| - alertConfirms keyedWindow // alert confirmation sends, by alert scope |
| + alertConfirms keyedWindow // alert confirmation sends, by alert scope |
| + ipNewPeople keyedWindow // new people, by IP |
| + globalNewPeople []time.Time // new people, console-wide |
| sweepNext int // index into sweepWindows() where the next sweep starts |
| } |
| @@ -170,7 +174,34 @@ func (l *emailLimits) submit(ip string) bool { |
| } |
| func (l *emailLimits) sweepWindows() []*keyedWindow { |
| - return []*keyedWindow{&l.submits, &l.addrSends, &l.ipSends, &l.stepUps, &l.addrWrong, &l.ipWrong, &l.alertConfirms} |
| + return []*keyedWindow{&l.submits, &l.addrSends, &l.ipSends, &l.stepUps, &l.addrWrong, &l.ipWrong, &l.alertConfirms, &l.ipNewPeople} |
| +} |
| + |
| +// newPerson reserves one new-person slot across all sign-in paths (social |
| +// sign-in spec §9). A refusal charges neither hourly window. |
| +func (l *emailLimits) newPerson(ip string) bool { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + now := l.clock() |
| + hour := now.Add(-time.Hour) |
| + l.sweep(now) |
| + l.ipNewPeople.trim(ip, now) |
| + if l.ipNewPeople.count(ip, hour) >= newPersonPerIPHour { |
| + return false |
| + } |
| + kept := l.globalNewPeople[:0] |
| + for _, t := range l.globalNewPeople { |
| + if t.After(hour) { |
| + kept = append(kept, t) |
| + } |
| + } |
| + l.globalNewPeople = kept |
| + if len(kept) >= newPersonGlobalHour { |
| + return false |
| + } |
| + l.ipNewPeople.add(ip, now) |
| + l.globalNewPeople = append(l.globalNewPeople, now) |
| + return true |
| } |
| // sweep is one bounded step of cleanup: it inspects at most limitSweepBudget |
diff --git a/internal/console/emailsignin_limits_test.go b/internal/console/emailsignin_limits_test.go| index fffd5ba11..74d7994cd 100644 |
| --- a/internal/console/emailsignin_limits_test.go |
| +++ b/internal/console/emailsignin_limits_test.go |
| @@ -19,6 +19,107 @@ func (c *limitClock) advance(d time.Duration) { c.t = c.t.Add(d) } |
| func newTestLimits(c *limitClock) *emailLimits { return &emailLimits{now: c.now} } |
| +func TestNewPersonBudget(t *testing.T) { |
| + c := newLimitClock() |
| + l := newTestLimits(c) |
| + for i := 0; i < 10; i++ { |
| + if !l.newPerson("a") { |
| + t.Fatalf("new person %d from a refused", i+1) |
| + } |
| + } |
| + for i := 0; i < 200; i++ { |
| + if l.newPerson("a") { |
| + t.Fatal("11th new person from a allowed") |
| + } |
| + } |
| + if !l.newPerson("b") { |
| + t.Fatal("new person from b refused") |
| + } |
| + // Refusals from a must leave all 189 remaining global slots available. |
| + for i := 0; i < 189; i++ { |
| + if !l.newPerson(fmt.Sprintf("other%d", i)) { |
| + t.Fatalf("global slot %d refused after IP refusals", i+12) |
| + } |
| + } |
| + c.advance(61 * time.Minute) |
| + if !l.newPerson("a") { |
| + t.Fatal("new person from a refused after the hour") |
| + } |
| + |
| + c = newLimitClock() |
| + l = newTestLimits(c) |
| + for i := 0; i < 200; i++ { |
| + if !l.newPerson(fmt.Sprintf("ip%d", i/10)) { |
| + t.Fatalf("new person %d console-wide refused", i+1) |
| + } |
| + } |
| + c.advance(30 * time.Minute) |
| + for i := 0; i < 20; i++ { |
| + if l.newPerson("fresh") { |
| + t.Fatal("201st new person console-wide allowed") |
| + } |
| + } |
| + c.advance(31 * time.Minute) |
| + for i := 0; i < 10; i++ { |
| + if !l.newPerson("fresh") { |
| + t.Fatalf("new person %d refused: global refusals charged a window", i+1) |
| + } |
| + } |
| +} |
| + |
| +func TestNewPersonBudgetConcurrentBoundary(t *testing.T) { |
| + for _, global := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("global=%v", global), func(t *testing.T) { |
| + l := newTestLimits(newLimitClock()) |
| + ip := func(i int) string { return "a" } |
| + cap := 10 |
| + if global { |
| + ip = func(i int) string { return fmt.Sprintf("ip%d", i) } |
| + cap = 200 |
| + } |
| + for i := 0; i < cap-1; i++ { |
| + if !l.newPerson(ip(i)) { |
| + t.Fatal("setup refused") |
| + } |
| + } |
| + var wg sync.WaitGroup |
| + var allowed atomic.Int32 |
| + start := make(chan struct{}) |
| + for i := 0; i < 32; i++ { |
| + wg.Add(1) |
| + go func(i int) { |
| + defer wg.Done() |
| + <-start |
| + if l.newPerson(ip(cap + i)) { |
| + allowed.Add(1) |
| + } |
| + }(i) |
| + } |
| + close(start) |
| + wg.Wait() |
| + if got := allowed.Load(); got != 1 { |
| + t.Fatalf("%d concurrent calls allowed at boundary, want 1", got) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestNewPersonBudgetSwept(t *testing.T) { |
| + c := newLimitClock() |
| + l := newTestLimits(c) |
| + if !l.newPerson("old") { |
| + t.Fatal("first new person refused") |
| + } |
| + if got := limitKeyCount(l); got != 1 { |
| + t.Fatalf("%d keys before sweep, want new-person IP", got) |
| + } |
| + c.advance(25 * time.Hour) |
| + l.submit("live") |
| + if got := limitKeyCount(l); got != 1 { |
| + t.Fatalf("%d keys after sweep, want only live submit", got) |
| + } |
| +} |
| + |
| func TestSigninSendLimits(t *testing.T) { |
| c := newLimitClock() |
| l := newTestLimits(c) |
| @@ -397,21 +498,21 @@ func TestSigninSendSweepIsBounded(t *testing.T) { |
| func TestSigninSendSweepCleansStaleAcrossCalls(t *testing.T) { |
| c := newLimitClock() |
| l := newTestLimits(c) |
| - // 50 stale keys in each of the seven windows: 350 in all, well over one |
| - // call's budget, including stepUps and the alert confirmations. |
| + // 50 stale keys in each window, well over one call's budget. |
| + stale := 50 * len(l.sweepWindows()) |
| for i := 0; i < 50; i++ { |
| for _, w := range l.sweepWindows() { |
| w.add(fmt.Sprintf("old%d", i), c.now()) |
| } |
| } |
| - if got := limitKeyCount(l); got != 350 { |
| + if got := limitKeyCount(l); got != stale { |
| t.Fatalf("setup: %d keys", got) |
| } |
| c.advance(25 * time.Hour) |
| l.submit("11.0.0.1") |
| left := limitKeyCount(l) |
| - if left == 0 || left < 350+1-limitSweepBudget { |
| - t.Fatalf("one submit left %d of 351 keys; want a bounded step (dropped at most %d)", left, limitSweepBudget) |
| + if left == 0 || left < stale+1-limitSweepBudget { |
| + t.Fatalf("one submit left %d of %d keys; want a bounded step (dropped at most %d)", left, stale+1, limitSweepBudget) |
| } |
| for i := 0; i < 20 && limitKeyCount(l) > 1; i++ { |
| l.submit("11.0.0.1") |
diff --git a/internal/console/newperson_test.go b/internal/console/newperson_test.go| new file mode 100644 |
| index 000000000..f2efaf10c |
| --- /dev/null |
| +++ b/internal/console/newperson_test.go |
| @@ -0,0 +1,140 @@ |
| +package console |
| + |
| +import ( |
| + "fmt" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func exhaustNewPersonBudget(t *testing.T, fx *consoleFixture, global bool) { |
| + t.Helper() |
| + c := newLimitClock() |
| + fx.srv.limits.mu.Lock() |
| + fx.srv.limits.now = c.now |
| + fx.srv.limits.mu.Unlock() |
| + n := 10 |
| + if global { |
| + n = 200 |
| + } |
| + for i := 0; i < n; i++ { |
| + ip := "127.0.0.1" |
| + if global { |
| + ip = fmt.Sprintf("192.0.2.%d", i/10+1) |
| + } |
| + if !fx.srv.limits.newPerson(ip) { |
| + t.Fatalf("budget setup refused new person %d", i+1) |
| + } |
| + } |
| +} |
| + |
| +func assertNoNewPerson(t *testing.T, fx *consoleFixture, addr string) { |
| + t.Helper() |
| + if n := userRows(t, fx, addr); n != 0 { |
| + t.Errorf("refusal created %d user rows", n) |
| + } |
| + for _, table := range []string{"sign_in_methods", "sessions", "grants"} { |
| + var n int |
| + if err := fx.st.db.QueryRow(`SELECT COUNT(*) FROM `+table+` WHERE address = ?`, addr).Scan(&n); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n != 0 { |
| + t.Errorf("refusal created %d rows in %s", n, table) |
| + } |
| + } |
| + if accts, err := fx.st.AccountsFor(addr); err != nil || len(accts) != 0 { |
| + t.Errorf("accounts after refusal = %v, %v", accts, err) |
| + } |
| +} |
| + |
| +func TestNewPersonBudgetRefusalWritesNothing(t *testing.T) { |
| + for _, global := range []bool{false, true} { |
| + for _, path := range []string{"code", "link", "keymail"} { |
| + t.Run(fmt.Sprintf("%s/global=%v", path, global), func(t *testing.T) { |
| + fx, m := newEmailFixture(t, true) |
| + exhaustNewPersonBudget(t, fx, global) |
| + const addr = "newperson@example.com" |
| + c := newBrowser(t) |
| + var status int |
| + var body string |
| + if path == "keymail" { |
| + fx.setUser(addr) |
| + status, _ = keymailWalk(t, fx, c, "", "newperson-code") |
| + } else { |
| + lookupAnswers(fx, addr, http.StatusNotFound) |
| + code, link := askSignin(t, fx, m, c, addr, "") |
| + if path == "code" { |
| + status, _, body = postCode(t, fx, c, code) |
| + } else { |
| + _, _, nonce := openLink(t, fx, c, link) |
| + status, _, body = postLink(t, fx, c, link, nonce) |
| + } |
| + } |
| + if status != http.StatusTooManyRequests { |
| + t.Fatalf("sign-in = %d, want 429: %s", status, body) |
| + } |
| + if path != "keymail" && !strings.Contains(body, copyText("signup.busy")) { |
| + t.Errorf("refusal missing approved copy: %s", body) |
| + } |
| + assertNoNewPerson(t, fx, addr) |
| + if raw := browserCookie(t, fx, c, sessionCookie); raw != "" { |
| + t.Error("refusal set a session cookie") |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +func TestNewPersonBudgetSparesReturning(t *testing.T) { |
| + for _, legacy := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("legacy=%v", legacy), func(t *testing.T) { |
| + fx, m := newEmailFixture(t, true) |
| + const addr = "returning@example.com" |
| + if legacy { |
| + if _, err := fx.st.EnsureUser(addr, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } else if _, _, err := fx.st.ResolveSignIn(MethodEmail, addr, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + exhaustNewPersonBudget(t, fx, true) |
| + lookupAnswers(fx, addr, http.StatusNotFound) |
| + c := newBrowser(t) |
| + code, _ := askSignin(t, fx, m, c, addr, "") |
| + status, _, body := postCode(t, fx, c, code) |
| + if status != http.StatusSeeOther { |
| + t.Fatalf("returning sign-in = %d: %s", status, body) |
| + } |
| + emailSession(t, fx, c, addr) |
| + // Even consulting the budget for a returning person is a failure. |
| + fx.srv.limits.mu.Lock() |
| + fx.srv.limits.now = func() time.Time { panic("returning person touched budget") } |
| + fx.srv.limits.mu.Unlock() |
| + w := httptest.NewRecorder() |
| + fx.srv.finishSignin(w, httptest.NewRequest(http.MethodGet, "/", nil), addr, "", MethodEmail, "") |
| + if w.Code != http.StatusSeeOther { |
| + t.Fatalf("returning finishSignin = %d", w.Code) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestNewPersonBudgetLookupError(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + if err := fx.st.Close(); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.limits.now = func() time.Time { panic("lookup error touched budget") } |
| + w := httptest.NewRecorder() |
| + fx.srv.finishSignin(w, httptest.NewRequest(http.MethodGet, "/", nil), "newperson@example.com", "", MethodEmail, "") |
| + if w.Code != http.StatusInternalServerError || !strings.Contains(w.Body.String(), "Could not record your account. Please try again.") { |
| + t.Fatalf("lookup error = %d: %s", w.Code, w.Body.String()) |
| + } |
| + for _, ck := range w.Result().Cookies() { |
| + if ck.Name == sessionCookie { |
| + t.Error("lookup error set a session cookie") |
| + } |
| + } |
| +} |
diff --git a/internal/console/signincopy.go b/internal/console/signincopy.go| index 9f1a848d5..18835b295 100644 |
| --- a/internal/console/signincopy.go |
| +++ b/internal/console/signincopy.go |
| @@ -52,6 +52,9 @@ var signinCopy = map[string]string{ |
| // a programming error and panics, so a typo cannot ship blank copy. |
| func copyText(id string, kv ...string) string { |
| text, ok := signinCopy[id] |
| + if !ok { |
| + text, ok = socialSigninCopy[id] |
| + } |
| if !ok { |
| panic("console: unknown copy id " + id) |
| } |
diff --git a/internal/console/socialsignin_copy.go b/internal/console/socialsignin_copy.go| new file mode 100644 |
| index 000000000..43fbe93ed |
| --- /dev/null |
| +++ b/internal/console/socialsignin_copy.go |
| @@ -0,0 +1,17 @@ |
| +package console |
| + |
| +// socialSigninCopy is pinned to the approved Plan 1 copy by |
| +// TestSocialSigninCopyMatchesApproved. |
| +var socialSigninCopy = map[string]string{ |
| + "methods.heading": "Sign-in methods", |
| + "methods.lede": "Every way you can sign in as {address}.", |
| + "methods.col_method": "Method", |
| + "methods.col_identity": "Signs in as", |
| + "methods.col_added": "Added", |
| + "methods.col_used": "Last used", |
| + "methods.kind_email": "Email link or code", |
| + "methods.kind_keymail": "Keymail", |
| + "methods.never_used": "Not yet", |
| + "signup.busy_title": "Try again later", |
| + "signup.busy": "Too many new accounts have been made from here in the last hour. Try again later, or sign in to an account you already have.", |
| +} |
diff --git a/internal/console/socialsignin_copy_test.go b/internal/console/socialsignin_copy_test.go| new file mode 100644 |
| index 000000000..f3b4b5751 |
| --- /dev/null |
| +++ b/internal/console/socialsignin_copy_test.go |
| @@ -0,0 +1,54 @@ |
| +package console |
| + |
| +import ( |
| + "encoding/json" |
| + "os" |
| + "testing" |
| +) |
| + |
| +func TestSocialSigninCopyMatchesApproved(t *testing.T) { |
| + raw, err := os.ReadFile("../../docs/superpowers/specs/2026-11-02-console-social-signin-copy-approved.json") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var approved struct { |
| + Action string `json:"action"` |
| + Strings []struct { |
| + ID string `json:"id"` |
| + Text string `json:"text"` |
| + } `json:"strings"` |
| + } |
| + if err := json.Unmarshal(raw, &approved); err != nil { |
| + t.Fatal(err) |
| + } |
| + if approved.Action != "approve" { |
| + t.Fatalf("action = %q, want approve", approved.Action) |
| + } |
| + seen := map[string]bool{} |
| + for _, s := range approved.Strings { |
| + if seen[s.ID] { |
| + t.Errorf("duplicate approved id %q", s.ID) |
| + } |
| + seen[s.ID] = true |
| + if got, ok := socialSigninCopy[s.ID]; !ok || got != s.Text { |
| + t.Errorf("socialSigninCopy[%q] = %q (present %v), approved %q", s.ID, got, ok, s.Text) |
| + } |
| + } |
| + for id, text := range socialSigninCopy { |
| + if !seen[id] { |
| + t.Errorf("unapproved socialSigninCopy id %q", id) |
| + } |
| + if _, ok := signinCopy[id]; ok { |
| + t.Errorf("copy id %q appears in both maps", id) |
| + } |
| + if got := copyText(id); got != text { |
| + t.Errorf("copyText(%q) = %q, want %q", id, got, text) |
| + } |
| + } |
| + if len(socialSigninCopy) != len(approved.Strings) { |
| + t.Errorf("socialSigninCopy has %d strings, approved copy has %d", len(socialSigninCopy), len(approved.Strings)) |
| + } |
| + if got := copyText("methods.lede", "address", "bob@example.com"); got != "Every way you can sign in as bob@example.com." { |
| + t.Errorf("social copy placeholder = %q", got) |
| + } |
| +} |