Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): reset enrollment backoff counter only on full success
Jose Ramirez0ed5c1f4035f
diff --git a/control-api/src/services/memberRegistrationEnrollment.ts b/control-api/src/services/memberRegistrationEnrollment.tsindex 06f9ed4a1..980634e68 100644--- a/control-api/src/services/memberRegistrationEnrollment.ts+++ b/control-api/src/services/memberRegistrationEnrollment.ts@@ -126,11 +126,6 @@ async function enroll(domain: string): Promise<MemberRegistrationCredential> {) {throw transientFailure(domain, 'member-registration hub returned a malformed mint response')}- // Only reset the backoff counter once the mint body is known well-formed —- // resetting as soon as response.ok is true (before this check) would let a- // hub that keeps returning 2xx with a malformed body re-mint at the 5s base- // forever instead of escalating toward BACKOFF_CAP_MS.- consecutiveTransientFailures.delete(domain)let inserted: booleanlet winner: MemberRegistrationCredential | null@@ -151,6 +146,14 @@ async function enroll(domain: string): Promise<MemberRegistrationCredential> {if (!winner) {throw transientFailure(domain, 'credential row missing after insert')}+ // Only reset the backoff counter on genuine full success — after the mint+ // body is well-formed AND the credential is durably persisted/adopted. The+ // counter must survive every failure path up to and including a persist+ // failure or a missing post-insert row, or a sustained outage on any of+ // those paths would re-mint at the 5s base forever instead of escalating+ // toward BACKOFF_CAP_MS (the exact orphan storm the design forbids).+ consecutiveTransientFailures.delete(domain)+if (!inserted) {rootLogger.warn({diff --git a/control-api/test/services.memberRegistrationEnrollment.test.ts b/control-api/test/services.memberRegistrationEnrollment.test.tsindex a278635cf..2962d793e 100644--- a/control-api/test/services.memberRegistrationEnrollment.test.ts+++ b/control-api/test/services.memberRegistrationEnrollment.test.ts@@ -284,6 +284,45 @@ describe('memberRegistrationEnrollment', () => {expect(fetchMock).toHaveBeenCalledTimes(1)})+ it('escalates the backoff on repeated persist failures during a sustained Postgres outage', async () => {+ // Mirrors 'escalates the backoff on repeated malformed 2xx mint bodies':+ // a single 5s-window sample isn't enough to prove escalation, because a+ // counter that gets reset on every attempt (bug: deleting the counter+ // right after the mint body validates, before the persist block) also+ // produces a 5s backoff on the FIRST retry — it only diverges from the+ // correct behavior on the SECOND retry, which must wait 10s, not 5s.+ const fetchMock = vi.fn().mockImplementation(async () => mintResponse())+ vi.stubGlobal('fetch', fetchMock)+ store.insertMemberRegistrationCredential.mockRejectedValue(new Error('pg down'))++ await expect(ensureEnrollment('profile.acme.com')).rejects.toBeInstanceOf(+ MemberRegistrationUnavailableError+ )+ expect(fetchMock).toHaveBeenCalledTimes(1)++ vi.advanceTimersByTime(5_000 + 1) // past the 5s base backoff (failures=1)+ await expect(ensureEnrollment('profile.acme.com')).rejects.toBeInstanceOf(+ MemberRegistrationUnavailableError+ )+ expect(fetchMock).toHaveBeenCalledTimes(2)++ vi.advanceTimersByTime(5_000 + 1) // still inside the escalated 10s window (failures=2)+ await expect(ensureEnrollment('profile.acme.com')).rejects.toBeInstanceOf(+ MemberRegistrationUnavailableError+ )+ // Fails against the unfixed code: the counter is deleted before the+ // persist block, so every failure computes failures=1 and re-mints every+ // 5s instead of escalating — this call would observe fetchMock called 3+ // times instead of still being served from the negative cache.+ expect(fetchMock).toHaveBeenCalledTimes(2) // served from cache — no re-mint yet++ vi.advanceTimersByTime(5_000 + 1) // now past the escalated 10s window+ await expect(ensureEnrollment('profile.acme.com')).rejects.toBeInstanceOf(+ MemberRegistrationUnavailableError+ )+ expect(fetchMock).toHaveBeenCalledTimes(3)+ })+it('rejects localhost / IP-literal / dotless hosts with the misconfiguration error, never fetching', async () => {const fetchMock = vi.fn()vi.stubGlobal('fetch', fetchMock)