Skip to content

Commit 0886ec3

Browse files
committed
fix(pool): poll-0 flapping fix — 0 readings after recent success are flagged unreliable (#8)
Review catch: noteSuccess cleared a stale 0 reading, but the NEXT poll wrote 0 back — remainingUnreliable flapped between polls and most-remaining-quota saw 0 in the gaps. Fix: pool tracks lastSuccessAtMs per account; updateQuota keeps a 0 reading but flags it remainingUnreliable when the account served a successful request within 30min. The flag travels on the runtime snapshot so strategy decisions can skip it. A 0 reading WITHOUT recent success is still trusted (real exhaustion signal). Also: re-backed up the store after acc2 addition (2 accounts, verified decode).
1 parent 6cf56f0 commit 0886ec3

3 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎src/auth/pool.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ describe("AccountPool — global-401 escape hatch", () => {
623623
});
624624

625625
describe("AccountPool — request outranks balance poll (#7 finding)", () => {
626-
it("a successful request invalidates a stale remaining=0 poll reading", async () => {
626+
it("a successful request invalidates a stale remaining=0 poll reading; next 0 is flagged unreliable", async () => {
627627
const store = new FakeStore();
628628
store.accounts = [stored("a")];
629629
const clock = new FakeClock();
@@ -635,6 +635,15 @@ describe("AccountPool — request outranks balance poll (#7 finding)", () => {
635635
// A request succeeds — ground truth: the account HAS quota.
636636
pool.noteSuccess("a");
637637
expect(pool.runtimeFor("a")!.remainingUnits).toBeUndefined(); // stale 0 cleared
638+
// The NEXT poll reports 0 again — recorded, but flagged unreliable so
639+
// strategy/parsers never read it as fact (no more flapping blind spot):
640+
clock.advance(5 * 60_000);
641+
pool.updateQuota("a", 0, clock.now());
642+
expect(pool.runtimeFor("a")!.remainingUnits).toBe(0);
643+
expect(pool.runtimeFor("a")!.remainingUnreliable).toBe(true);
644+
// A later poll with a non-zero reading clears the flag:
645+
pool.updateQuota("a", 300, clock.now() + 60_000);
646+
expect(pool.runtimeFor("a")!.remainingUnreliable).toBeFalsy(); // not flagged
638647
});
639648

640649
it("internal cooling is clamped even with a huge upstream Retry-After", async () => {
@@ -648,3 +657,38 @@ describe("AccountPool — request outranks balance poll (#7 finding)", () => {
648657
expect(retryAt - clock.now()).toBeLessThanOrEqual(6 * 3600_000 + 1000); // clamped to 6h
649658
});
650659
});
660+
661+
describe("AccountPool — poll-0 flapping fix (review follow-up)", () => {
662+
it("poll reports 0 again after a success → flagged unreliable, not silently trusted", async () => {
663+
const store = new FakeStore();
664+
store.accounts = [stored("a")];
665+
const clock = new FakeClock();
666+
const pool = await makePool(store, clock);
667+
// Poll cycle 1: 0 (stale under-report)
668+
pool.updateQuota("a", 0, clock.now());
669+
expect(pool.runtimeFor("a")!.remainingUnits).toBe(0);
670+
// Request succeeds → ground truth, 0 cleared + flagged
671+
pool.noteSuccess("a");
672+
expect(pool.runtimeFor("a")!.remainingUnits).toBeUndefined();
673+
expect(pool.runtimeFor("a")!.remainingUnreliable).toBe(true);
674+
// Poll cycle 2: 0 again (within 30min of the success) → KEPT but flagged,
675+
// so most-remaining-quota never penalizes the working account:
676+
pool.updateQuota("a", 0, clock.now() + 5 * 60_000);
677+
expect(pool.runtimeFor("a")!.remainingUnits).toBe(0);
678+
expect(pool.runtimeFor("a")!.remainingUnreliable).toBe(true);
679+
// And noteSuccess clears it again on the next successful request:
680+
pool.noteSuccess("a");
681+
expect(pool.runtimeFor("a")!.remainingUnits).toBeUndefined();
682+
});
683+
684+
it("0 reading WITHOUT recent success is trusted (real exhaustion signal)", async () => {
685+
const store = new FakeStore();
686+
store.accounts = [stored("a")];
687+
const clock = new FakeClock();
688+
const pool = await makePool(store, clock);
689+
// No noteSuccess ever recorded — a 0 poll is the only signal we have:
690+
pool.updateQuota("a", 0, clock.now());
691+
expect(pool.runtimeFor("a")!.remainingUnits).toBe(0);
692+
expect(pool.runtimeFor("a")!.remainingUnreliable).toBeUndefined();
693+
});
694+
});

‎src/auth/pool.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export interface AccountRuntime {
126126
quotaExpiresAtMs?: number;
127127
/** From the quota poller (ticket #6); optional pick hint. */
128128
remainingUnits?: number;
129+
/** True when the 0 reading conflicts with observed successful traffic
130+
* (billing balance can under-report — live finding, #7). */
131+
remainingUnreliable?: boolean;
129132
/** From the poller; freshness marker (stale data never gates picking). */
130133
lastPolledAtMs?: number;
131134
inFlight: number;
@@ -174,6 +177,8 @@ export class AccountPool {
174177
private recent401s: Array<{ id: string; at: number }> = [];
175178
/** Recent 429 timestamps per account — the global-rate-limit detector. */
176179
private recent429s: Array<{ id: string; at: number }> = [];
180+
/** Last successful request per account (ground truth vs balance polls). */
181+
private lastSuccessAtMs = new Map<string, number>();
177182

178183
private accounts = new Map<string, StoredAccount>();
179184
private runtime = new Map<string, AccountRuntime>();
@@ -440,7 +445,19 @@ export class AccountPool {
440445
const r = this.runtime.get(accountId);
441446
if (!r) return;
442447
r.lastPolledAtMs = polledAtMs;
443-
if (remainingUnits !== undefined) r.remainingUnits = remainingUnits;
448+
if (remainingUnits !== undefined) {
449+
r.remainingUnits = remainingUnits;
450+
// A 0 reading from an account with a recent successful request is kept
451+
// but flagged unreliable (billing balance can under-report claimed-plan
452+
// entitlements — live finding #7). Without the flag,
453+
// most-remaining-quota would penalize a working account between polls
454+
// (the flapping the review called out).
455+
const lastSuccess = this.lastSuccessAtMs.get(accountId);
456+
r.remainingUnreliable =
457+
remainingUnits === 0 && lastSuccess !== undefined && polledAtMs - lastSuccess < 30 * 60_000
458+
? true
459+
: undefined;
460+
}
444461
}
445462

446463
/** Poller reports the account's entitlement expiry (quotaExpiresAt). */
@@ -465,10 +482,16 @@ export class AccountPool {
465482
* traffic fine — the claimed-plan entitlement does not surface there).
466483
*/
467484
noteSuccess(accountId: string): void {
485+
this.lastSuccessAtMs.set(accountId, this.now());
468486
const r = this.runtime.get(accountId);
469487
if (r) {
470488
r.consecutiveFailures = 0;
471-
if (r.remainingUnits === 0) r.remainingUnits = undefined; // stale poll — request proved otherwise
489+
if (r.remainingUnits === 0) {
490+
// Stale 0 cleared AND flagged: the next poll may report 0 again, and
491+
// the flag keeps most-remaining-quota from penalizing a working acc.
492+
r.remainingUnits = undefined;
493+
r.remainingUnreliable = true;
494+
}
472495
if (r.status === "expired" || r.status === "exhausted") {
473496
// Healed: a successful authenticated interaction proves the
474497
// credential works and quota exists.

‎src/server/routes-quota.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,9 @@ export async function handleQuota(
333333
...(a.label ? { label: a.label } : {}),
334334
status: r?.status ?? "healthy",
335335
...(r?.remainingUnits !== undefined ? { remainingUnits: r.remainingUnits } : {}),
336-
// Billing balance can report 0 for an account that serves traffic fine
337-
// (claimed-plan entitlements don't surface there — live finding #7).
338-
// A healthy account reading 0 is flagged unreliable rather than
339-
// presented as fact.
340-
...(r?.remainingUnits === 0 && (r?.status ?? "healthy") === "healthy" ? { remainingUnreliable: true } : {}),
336+
// remainingUnreliable is set by the pool when a 0 reading lands shortly
337+
// after successful traffic (balance-endpoint blind spot, #7).
338+
...(r?.remainingUnreliable ? { remainingUnreliable: true } : {}),
341339
...(lastPolledAtMs !== undefined ? { lastPolledAt: Math.floor(lastPolledAtMs / 1000) } : {}),
342340
...(lastPolledAtMs !== undefined && Date.now() - lastPolledAtMs > STALE_MS ? { stale: true } : {}),
343341
// tokenExpiresAt: credential expiry (OAuth) — JWTs here carry no exp.

0 commit comments

Comments
 (0)