Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Ignore a sign-in report from an attempt the user abandoned

shukieshahd74edfd0763a
diff --git a/cli/beacon/internal/onboarding/wizard.go b/cli/beacon/internal/onboarding/wizard.go
index 0e500101..26729606 100644
--- a/cli/beacon/internal/onboarding/wizard.go
+++ b/cli/beacon/internal/onboarding/wizard.go
@@ -112,9 +112,12 @@ type wizardModel struct {
cancelSignIn context.CancelFunc
prompt SignInPrompt
signInErr error
- startedAt time.Time
- now time.Time
- frame int
+ // attempt counts sign-in attempts, so a report from an abandoned one can be
+ // told apart from the current one's.
+ attempt int
+ startedAt time.Time
+ now time.Time
+ frame int
}
// spinnerFrames animate the wait. Braille is what every terminal capable of the
@@ -123,18 +126,31 @@ var spinnerFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "
type tickMsg time.Time
-type signInPromptMsg SignInPrompt
+// Sign-in events carry the attempt that produced them.
+//
+// Cancelling an attempt does not unschedule its goroutine: a sign-in abandoned
+// with esc still reports, eventually, with context.Canceled. If the user has
+// already started a retry by then, that stale report would otherwise be applied to
+// the new attempt and shown as its failure.
+type signInPromptMsg struct {
+ attempt int
+ prompt SignInPrompt
+}
type signInDoneMsg struct {
+ attempt int
account Account
err error
}
-type chanReporter struct{ events chan<- tea.Msg }
+type chanReporter struct {
+ events chan<- tea.Msg
+ attempt int
+}
func (r chanReporter) Prompt(p SignInPrompt) {
select {
- case r.events <- signInPromptMsg(p):
+ case r.events <- signInPromptMsg{attempt: r.attempt, prompt: p}:
default:
}
}
@@ -211,7 +227,11 @@ func (m wizardModel) Update(message tea.Msg) (tea.Model, tea.Cmd) {
m.frame++
return m, tickCmd()
case signInPromptMsg:
- m.prompt = SignInPrompt(msg)
+ // Re-arm either way: exactly one reader stays live for the whole program,
+ // so ignoring a stale event must not leave the channel unread.
+ if msg.attempt == m.attempt {
+ m.prompt = msg.prompt
+ }
return m, waitForEvent(m.events)
case signInDoneMsg:
return m.signInFinished(msg)
@@ -275,34 +295,49 @@ func (m wizardModel) startSignIn() (tea.Model, tea.Cmd) {
m.frame = 0
m.screen = signInWaitScreen
+ // The first attempt arms the single channel reader. Every handler re-arms it,
+ // so retries must not arm a second one: two readers would split the stream and
+ // each would need its own re-arm bookkeeping.
+ first := m.attempt == 0
+ m.attempt++
+ attempt := m.attempt
+
events, signIn := m.events, m.options.SignIn
req := SignInRequest{NoBrowser: m.options.NoBrowser}
go func() {
- account, err := signIn(ctx, req, chanReporter{events: events})
- events <- signInDoneMsg{account: account, err: err}
+ account, err := signIn(ctx, req, chanReporter{events: events, attempt: attempt})
+ events <- signInDoneMsg{attempt: attempt, account: account, err: err}
}()
- return m, tea.Batch(waitForEvent(m.events), tickCmd())
+ if first {
+ return m, tea.Batch(waitForEvent(m.events), tickCmd())
+ }
+ return m, tickCmd()
}
func (m wizardModel) signInFinished(msg signInDoneMsg) (tea.Model, tea.Cmd) {
+ // An abandoned attempt reporting late says nothing about the current one, and
+ // must not cancel it or be shown as its outcome.
+ if msg.attempt != m.attempt {
+ return m, waitForEvent(m.events)
+ }
m.stopSignIn()
if msg.err != nil {
// A cancel the user asked for has already moved the screen; do not overwrite
// it with a failure they did not cause.
if m.screen != signInWaitScreen {
- return m, nil
+ return m, waitForEvent(m.events)
}
m.signInErr = msg.err
m.selected = 0
m.screen = signInFailedScreen
- return m, nil
+ return m, waitForEvent(m.events)
}
m.options.SignedIn = true
m.options.Email = msg.account.Email
m.result.SignedInEmail = msg.account.Email
m.selected = 0
m.screen = m.afterSignIn()
- return m, nil
+ return m, waitForEvent(m.events)
}
// afterSignIn is where the wizard resumes once an account exists.
diff --git a/cli/beacon/internal/onboarding/wizard_test.go b/cli/beacon/internal/onboarding/wizard_test.go
index f06faf50..a5fff01e 100644
--- a/cli/beacon/internal/onboarding/wizard_test.go
+++ b/cli/beacon/internal/onboarding/wizard_test.go
@@ -345,7 +345,7 @@ func TestSignInWaitShowsAnUnwrappedURLAndAClock(t *testing.T) {
t.Cleanup(func() { model.stopSignIn() })
url := "https://beacon.sh/cli/auth?port=54123&state=" + strings.Repeat("a", 43)
- next, _ := model.Update(signInPromptMsg{URL: url, WillOpen: true})
+ next, _ := model.Update(signInPromptMsg{attempt: 1, prompt: SignInPrompt{URL: url, WillOpen: true}})
model = next.(wizardModel)
// Seven seconds later.
@@ -403,9 +403,9 @@ func TestSignInFailureOffersRecoveryAndALocalFinish(t *testing.T) {
model := signInWizard(t, neverReturns)
model.screen = signInScreen
model, _ = advanceWizard(t, model, "enter")
- next, _ := model.Update(signInPromptMsg{URL: "https://beacon.sh/cli/auth?port=1&state=x", WillOpen: true})
+ next, _ := model.Update(signInPromptMsg{attempt: 1, prompt: SignInPrompt{URL: "https://beacon.sh/cli/auth?port=1&state=x", WillOpen: true}})
model = next.(wizardModel)
- next, _ = model.Update(signInDoneMsg{err: errors.New("connection refused")})
+ next, _ = model.Update(signInDoneMsg{attempt: 1, err: errors.New("connection refused")})
model = next.(wizardModel)
if model.screen != signInFailedScreen {
@@ -477,7 +477,7 @@ func TestSignInSuccessContinuesToTheDestinationQuestion(t *testing.T) {
model := signInWizard(t, neverReturns)
model.screen = signInScreen
model, _ = advanceWizard(t, model, "enter")
- next, _ := model.Update(signInDoneMsg{account: Account{Email: "[email protected]"}})
+ next, _ := model.Update(signInDoneMsg{attempt: 1, account: Account{Email: "[email protected]"}})
model = next.(wizardModel)
if model.screen != destinationScreen {
@@ -522,9 +522,9 @@ func TestWholeFirstRunHappensInOneScreenSession(t *testing.T) {
}
_ = quitCmd
- next, _ := model.Update(signInPromptMsg{URL: "https://beacon.sh/cli/auth?port=1&state=x", WillOpen: true})
+ next, _ := model.Update(signInPromptMsg{attempt: 1, prompt: SignInPrompt{URL: "https://beacon.sh/cli/auth?port=1&state=x", WillOpen: true}})
model = next.(wizardModel)
- next, _ = model.Update(signInDoneMsg{account: Account{Email: "[email protected]"}})
+ next, _ = model.Update(signInDoneMsg{attempt: 1, account: Account{Email: "[email protected]"}})
model = next.(wizardModel)
if model.screen != destinationScreen {
t.Fatalf("after sign-in completed: screen = %v", model.screen)
@@ -542,3 +542,35 @@ func TestWholeFirstRunHappensInOneScreenSession(t *testing.T) {
t.Fatalf("the signed-in account should reach the caller: %#v", model.result)
}
}
+
+// esc then enter: the cancelled attempt's completion must not be shown as a
+// failure of the new one.
+func TestStaleSignInDoesNotPoisonRetry(t *testing.T) {
+ model := signInWizard(t, neverReturns)
+ model.screen = signInScreen
+ model, _ = advanceWizard(t, model, "enter") // attempt A
+ model, _ = advanceWizard(t, model, "esc") // cancel A, back to sign-in
+ model, _ = advanceWizard(t, model, "enter") // attempt B
+ t.Cleanup(func() { model.stopSignIn() })
+
+ // A's goroutine now notices the cancellation and reports it, late.
+ next, _ := model.Update(signInDoneMsg{attempt: 1, err: context.Canceled})
+ model = next.(wizardModel)
+
+ if model.screen == signInFailedScreen {
+ t.Fatalf("a cancelled attempt was shown as a failure of the retry: err=%v", model.signInErr)
+ }
+ if model.screen != signInWaitScreen {
+ t.Fatalf("screen = %v, want still waiting on the retry", model.screen)
+ }
+ if model.signInErr != nil {
+ t.Fatalf("signInErr = %v, want none", model.signInErr)
+ }
+
+ // And B's own result, attempt 2, still lands.
+ next, _ = model.Update(signInDoneMsg{attempt: 2, err: errors.New("connection refused")})
+ model = next.(wizardModel)
+ if model.screen != signInFailedScreen {
+ t.Fatalf("the current attempt's failure was dropped: screen = %v", model.screen)
+ }
+}