check-in: a plugin's failed with a later until waits for it (plugins#503, plugins#793)
A plugin's check-in can answer failed with an until hours away: ZCode
rate limits a claim read, and its plugin (plugins#793) says when to ask
again. magpie asked any failed again after 30 minutes whatever the until
said, the half hour that kept the limit on (#503: "gift plans to claim:
Too Many Requests" 180 times over four days). A review probe on
origin/main 19c45d5d4 saw a failed with until 6h out asked at 31 minutes
and at 2 hours.
Now heldUntil holds a failed whose until is later than its own retry
until that time, at most 24 hours after the failure (checkinHoldMax), so
a wrong far-off until can't stop check-ins for good. Every path that
schedules a check-in follows it:
- settled: the loop doesn't ask before it, across the Beijing day's turn
too; a press (soon) still asks at once.
- the recheck branch (ZCode, Qoder) counts a held failure as waiting,
where it was otherwise asked again every 30 minutes.
- the loop's wake time (wbCheckinLoop.again) is the until, not 30
minutes on.
A failed with no until keeps the 30-minute retry (offline failures their
minute ladder), and one with an until sooner than the retry is asked at
the until, as before. The GUI and TUI show no time for the next ask
("magpie tries again later"), so nothing there changed.
docs/subsystems/provider-plugins.md says it.
Run (mote, through mote-run):
- go test -count=1 -timeout 30m ./internal/provider/ ./internal/tui/
./internal/cli/: ok (193.7s, 17.1s, 76.6s).
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet on
./internal/provider/: ok. gofmt -l: nothing.
- The full suite runs after the push, through the suite lock.
Broken by hand (new tests TestPluginCheckinFailedWaitsForUntil,
TestPluginCheckinFailedUntilBounded):
- the old settled/checkin.go/tick: red, "loop at +31m0s: asked again
(2 asks), until ..." for zcode and a plain plugin, and the far until
case asked at +31m.
- the recheck branch without the held failure: zcode red at +31m.
- the old wake time: "the loop wakes at 14:30, want the until 20:00".
- no 24h cap: "at +24h0m0s: not asked again".
Restored: ok.
Not tried: a real ZCode account rate limited with until; the plugin's
own backoff (plugins#793) still holds off for older magpie.
Reported-by: kongbai01203 (plugins#503)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -113,7 +113,8 @@ func (c checkiner) checkinNow(ctx context.Context, soon bool) []WorkBuddyCheckin
|
||||
// again every 30 minutes, ZCode's claim only answered 3007
|
||||
// again, and the reporter's accounts went on to 3012
|
||||
// (plugins#503). A press still asks.
|
||||
waits := prev.Outcome == CheckinCaptcha || prev.Outcome == CheckinOwnApp
|
||||
// A failure held to its until waits it out (heldUntil).
|
||||
waits := prev.Outcome == CheckinCaptcha || prev.Outcome == CheckinOwnApp || now.Before(prev.heldUntil())
|
||||
settled = settled && !soon && (waits || now.Sub(prev.At) < wbCheckinEvery)
|
||||
if prev.Checked() && !prev.ValidUntil.IsZero() {
|
||||
settled = settled && now.Before(prev.ValidUntil)
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// pluginCheckinFails is a plugin provider whose one account's check-in
|
||||
// answers failed, with until (zero for none) after the ask, counting asks.
|
||||
func pluginCheckinFails(id string, until func(time.Time) time.Time, now *time.Time, calls *int) pluginCheckinProvider {
|
||||
return pluginCheckinProvider{ID: id, accounts: []pluginCheckinAcct{{User: "ann", On: true, key: id + "|ann", card: id, do: func(context.Context) WorkBuddyCheckin {
|
||||
*calls++
|
||||
r := WorkBuddyCheckin{Outcome: CheckinFailed, Msg: "gift plans to claim: Too Many Requests"}
|
||||
if until != nil {
|
||||
r.ValidUntil = until(*now)
|
||||
}
|
||||
return r
|
||||
}}}}
|
||||
}
|
||||
|
||||
// plugins#503 (kongbai01203), plugins#793: ZCode rate limits a claim read,
|
||||
// and the plugin answers failed with an until hours away. magpie asked it
|
||||
// again 30 minutes on all the same, which kept the limit on. A failed with
|
||||
// an until later than its retry is not asked by the loop before the until,
|
||||
// across the Beijing day's turn too; at most 24 hours after the failure; a
|
||||
// press still asks; one with no until is asked again in 30 minutes.
|
||||
func TestPluginCheckinFailedWaitsForUntil(t *testing.T) {
|
||||
for _, id := range []string{"zcode", "fakeco"} { // rechecked or not
|
||||
t.Run(id, func(t *testing.T) {
|
||||
claudeHome(t)
|
||||
// 22:00 Beijing: a 6h until is past the day's turn
|
||||
start := time.Date(2026, 11, 5, 14, 0, 0, 0, time.UTC)
|
||||
now, calls := start, 0
|
||||
sixHours := func(at time.Time) time.Time { return at.Add(6 * time.Hour) }
|
||||
p := pluginCheckinFails(id, sixHours, &now, &calls)
|
||||
c := p.checkiner(func() time.Time { return now })
|
||||
ask := func(press bool) WorkBuddyCheckin {
|
||||
t.Helper()
|
||||
got := c.checkinNow(context.Background(), press)
|
||||
if len(got) != 1 || got[0].Outcome != CheckinFailed {
|
||||
t.Fatalf("at %s: %+v", now, got)
|
||||
}
|
||||
return got[0]
|
||||
}
|
||||
ask(false)
|
||||
for _, at := range []time.Duration{31 * time.Minute, 2 * time.Hour, 3 * time.Hour, 6*time.Hour - time.Minute} {
|
||||
now = start.Add(at)
|
||||
if r := ask(false); r.Asked || calls != 1 {
|
||||
t.Fatalf("loop at +%v: asked again (%d asks), until %v", at, calls, r.ValidUntil)
|
||||
}
|
||||
}
|
||||
now = start.Add(6 * time.Hour)
|
||||
if r := ask(false); !r.Asked || calls != 2 {
|
||||
t.Fatalf("loop at the until: not asked (%d asks)", calls)
|
||||
}
|
||||
|
||||
// a press asks while held
|
||||
now = start.Add(6*time.Hour + 31*time.Minute)
|
||||
if r := ask(true); !r.Asked || calls != 3 {
|
||||
t.Fatalf("press while held: not asked (%d asks)", calls)
|
||||
}
|
||||
|
||||
// the loop's wake time is the until, not 30 minutes on
|
||||
var l wbCheckinLoop
|
||||
now = start.AddDate(0, 0, 2)
|
||||
calls = 0
|
||||
var asked []time.Duration
|
||||
for m := time.Duration(0); m <= 7*time.Hour; m += time.Minute {
|
||||
now = start.AddDate(0, 0, 2).Add(m)
|
||||
for _, r := range l.tick(context.Background(), c) {
|
||||
if r.Asked {
|
||||
asked = append(asked, m)
|
||||
}
|
||||
}
|
||||
if m == 0 && !l.again.Equal(sixHours(now)) {
|
||||
t.Fatalf("the loop wakes at %v, want the until %v", l.again, sixHours(now))
|
||||
}
|
||||
}
|
||||
if want := []time.Duration{0, 6 * time.Hour}; !slices.Equal(asked, want) {
|
||||
t.Fatalf("the loop asked at %v, want %v", asked, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// An until a day or more away holds a failure 24 hours, no longer; a
|
||||
// failure with no until, or one sooner than its retry, is asked as before.
|
||||
func TestPluginCheckinFailedUntilBounded(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
until func(time.Time) time.Time
|
||||
quiet []time.Duration // not asked again then
|
||||
again time.Duration // asked again then
|
||||
}{
|
||||
{"a far until", func(at time.Time) time.Time { return at.AddDate(1, 0, 0) }, []time.Duration{31 * time.Minute, 23*time.Hour + 59*time.Minute}, 24 * time.Hour},
|
||||
{"no until", nil, []time.Duration{29 * time.Minute}, 30 * time.Minute},
|
||||
{"a sooner until", func(at time.Time) time.Time { return at.Add(2 * time.Minute) }, []time.Duration{time.Minute}, 2 * time.Minute},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
claudeHome(t)
|
||||
start := time.Date(2026, 11, 5, 6, 0, 0, 0, time.UTC) // 14:00 Beijing
|
||||
now, calls := start, 0
|
||||
c := pluginCheckinFails("zcode", tc.until, &now, &calls).checkiner(func() time.Time { return now })
|
||||
c.checkinNow(context.Background(), false)
|
||||
for _, at := range tc.quiet {
|
||||
now = start.Add(at)
|
||||
if c.checkinNow(context.Background(), false); calls != 1 {
|
||||
t.Fatalf("at +%v: asked again", at)
|
||||
}
|
||||
}
|
||||
now = start.Add(tc.again)
|
||||
if got := c.checkinNow(context.Background(), false); calls != 2 || len(got) != 1 || !got[0].Asked {
|
||||
t.Fatalf("at +%v: not asked again (%d asks)", tc.again, calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,8 @@ package provider
|
||||
// and account id (no name, no token), so a restart doesn't ask again. An
|
||||
// answer — claimed, already in, not eligible, the event over — holds for
|
||||
// the rest of the day; a request that didn't get one (the network, a
|
||||
// refused token) is tried again after wbCheckinRetry, or, when it never
|
||||
// refused token) is tried again after wbCheckinRetry, or at the until a
|
||||
// plugin gave it (heldUntil), or, when it never
|
||||
// reached WorkBuddy (a machine just woken, its network not back yet), a
|
||||
// minute later, and twice as long each time it still doesn't.
|
||||
//
|
||||
@@ -71,7 +72,7 @@ const (
|
||||
CheckinDone = "done" // in already, pressed in WorkBuddy or before
|
||||
CheckinIneligible = "ineligible" // the account can't take part
|
||||
CheckinInactive = "inactive" // no event running, or it has ended
|
||||
CheckinFailed = "failed" // no answer; tried again later that day
|
||||
CheckinFailed = "failed" // no answer; tried again later, at its until if given
|
||||
// CheckinCaptcha is a plugin's check-in the vendor asked a captcha
|
||||
// of: the user checks in in its own app, magpie never solves one
|
||||
CheckinCaptcha = "captcha"
|
||||
@@ -109,7 +110,8 @@ type WorkBuddyCheckin struct {
|
||||
Msg string `json:"msg,omitempty"`
|
||||
// ValidUntil is a campaign's end, or when a plugin said its vendor's
|
||||
// day turns (auth.checkin's until), when the answer must be checked
|
||||
// again, even if the Beijing day hasn't changed.
|
||||
// again, even if the Beijing day hasn't changed; on a failure, the
|
||||
// time before which it isn't asked again (heldUntil).
|
||||
ValidUntil time.Time `json:"until,omitzero"`
|
||||
// Offline is a failure that never reached WorkBuddy, and Tries how
|
||||
// many before it the same day did the same.
|
||||
@@ -188,6 +190,11 @@ func (r WorkBuddyCheckin) Checked() bool {
|
||||
// Qoder's daily credits open at 10:00 UTC+8, sdytman on Discord), and not
|
||||
// a failure due another try (at now; soon, at once).
|
||||
func (r WorkBuddyCheckin) settled(day string, now time.Time, soon bool) bool {
|
||||
// a failure held to the plugin's until waits it out, the day's turn
|
||||
// too; a press still asks
|
||||
if h := r.heldUntil(); !soon && now.Before(h) {
|
||||
return true
|
||||
}
|
||||
if r.Day != day || !r.ValidUntil.IsZero() && !now.Before(r.ValidUntil) {
|
||||
return false
|
||||
}
|
||||
@@ -205,6 +212,26 @@ func (r WorkBuddyCheckin) retryAfter() time.Duration {
|
||||
return min(wbCheckinRetry, wbCheckinSoon<<min(r.Tries, 5))
|
||||
}
|
||||
|
||||
// checkinHoldMax bounds how long a failure's until holds it (heldUntil),
|
||||
// so a far-off or wrong until can't stop an account's check-ins for good.
|
||||
const checkinHoldMax = 24 * time.Hour
|
||||
|
||||
// heldUntil is when a failure a plugin gave an until later than its own
|
||||
// retry (ZCode's claim read rate limited, its Retry-After or the plugin's
|
||||
// own backoff: plugins#503, plugins#793) is asked again: at the until, at
|
||||
// most checkinHoldMax after the failure, not every wbCheckinRetry, which
|
||||
// kept the limit on. Zero for any other answer: a failure with no until,
|
||||
// or one sooner than its retry, which settled asks again at the until.
|
||||
func (r WorkBuddyCheckin) heldUntil() time.Time {
|
||||
if r.Outcome != CheckinFailed || !r.ValidUntil.After(r.At.Add(r.retryAfter())) {
|
||||
return time.Time{}
|
||||
}
|
||||
if most := r.At.Add(checkinHoldMax); most.Before(r.ValidUntil) {
|
||||
return most
|
||||
}
|
||||
return r.ValidUntil
|
||||
}
|
||||
|
||||
// wbCheckinStatus is the event as an account sees it.
|
||||
type wbCheckinStatus struct {
|
||||
Active bool `json:"active"`
|
||||
@@ -641,7 +668,7 @@ func (l *wbCheckinLoop) tick(ctx context.Context, c checkinRunner) []WorkBuddyCh
|
||||
for _, r := range rs {
|
||||
due := time.Time{}
|
||||
if r.Outcome == CheckinFailed {
|
||||
due = r.At.Add(r.retryAfter())
|
||||
due = cmp.Or(r.heldUntil(), r.At.Add(r.retryAfter()))
|
||||
}
|
||||
// and as the vendor's own day turns, when it said (ValidUntil)
|
||||
if u := r.ValidUntil; u.After(now) && (due.IsZero() || u.Before(due)) {
|
||||
|
||||
Reference in New Issue
Block a user