Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. π₯
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
refactor(billing): remove the direct Autumn billing path (#5483)
Rakshith Ramprakashbb0d35f6d41f
diff --git a/apps/api/src/config.ts b/apps/api/src/config.tsindex 1d93e9e44..f856a6d24 100644--- a/apps/api/src/config.ts+++ b/apps/api/src/config.ts@@ -773,9 +773,9 @@ const configSchema = z.object({// BillingAUTO_RECHARGE_ENABLED: z.stringbool().default(false),- // firebill β durable usage-event store that sits in front of Autumn. When- // both URL and SECRET are set, every org's usage is routed through firebill- // instead of directly to Autumn.+ // firebill β durable usage-event store that sits in front of Autumn. Every+ // billing write (check, lock, track, refund, finalize) goes through it;+ // without both URL and SECRET set, billing is skipped.FIREBILL_URL: emptyStringAsUndefined(z.string().url()),FIREBILL_SECRET: emptyStringAsUndefined(z.string().trim().min(1)),// How long "this team is not partner-provisioned" is trusted. Only thediff --git a/apps/api/src/services/autumn/__tests__/autumn.service.org-source.test.ts b/apps/api/src/services/autumn/__tests__/autumn.service.org-source.test.tsindex 1dc9448d9..a40938251 100644--- a/apps/api/src/services/autumn/__tests__/autumn.service.org-source.test.ts+++ b/apps/api/src/services/autumn/__tests__/autumn.service.org-source.test.ts@@ -8,29 +8,6 @@import { vi } from "vitest";-const { mockCheck, mockTrack, mockAutumnClient } = vi.hoisted(() => {- const mockCheck = vi- .fn<(args: any) => Promise<any>>()- .mockResolvedValue({ allowed: true, customerId: "org-1", balance: null });- const mockTrack = vi- .fn<(args: any) => Promise<void>>()- .mockResolvedValue(undefined);- return {- mockCheck,- mockTrack,- mockAutumnClient: {- customers: { getOrCreate: vi.fn().mockResolvedValue({ id: "org-1" }) },- entities: {- get: vi.fn().mockResolvedValue({ balances: { CREDITS: { usage: 0 } } }),- create: vi.fn().mockResolvedValue({ id: "team-1" }),- },- balances: { finalize: vi.fn().mockResolvedValue(undefined) },- check: mockCheck,- track: mockTrack,- },- };-});-// The seam: importing either module at all is the failure.vi.mock("../../../controllers/auth", () => {throw new Error("autumn.service must not import controllers/auth");@@ -41,7 +18,7 @@ vi.mock("../../../lib/team-org", () => {});vi.mock("../client", () => ({- autumnClient: mockAutumnClient,+ autumnClient: null,}));vi.mock("../../../db/connection", () => ({@@ -54,7 +31,10 @@ vi.mock("../../../db/connection", () => ({vi.mock("../../../config", () => ({// Stubbed so importing the real config (which parses env) is avoided.- config: {},+ config: {+ FIREBILL_URL: "http://firebill.test",+ FIREBILL_SECRET: "fb-secret",+ },}));// Import AFTER the mocks are wired up.@@ -62,6 +42,25 @@ import { AutumnService } from "../autumn.service";const CALLER_ORG = "3f2c1b8e-7a4d-4c1e-9b6a-0d5e8f2a1c74";+const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();+const sentBody = () => JSON.parse(mockFetch.mock.calls[0]![1].body);++beforeEach(() => {+ mockFetch.mockReset();+ mockFetch.mockImplementation(+ async () =>+ new Response(+ JSON.stringify({ success: true, allowed: true, remaining: 0 }),+ { status: 200 },+ ),+ );+ vi.stubGlobal("fetch", mockFetch);+});++afterEach(() => {+ vi.unstubAllGlobals();+});+describe("AutumnService resolves no org of its own", () => {it("checks credits against the org the caller handed in", async () => {const result = await new AutumnService().checkCredits({@@ -71,9 +70,10 @@ describe("AutumnService resolves no org of its own", () => {});expect(result).toEqual({ allowed: true, remaining: 0 });- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({ customerId: CALLER_ORG, entityId: "team-1" }),- );+ expect(sentBody()).toMatchObject({+ customer_id: CALLER_ORG,+ entity_id: "team-1",+ });});it("tracks credits against the org the caller handed in", async () => {@@ -83,8 +83,9 @@ describe("AutumnService resolves no org of its own", () => {value: 7,});- expect(mockTrack).toHaveBeenCalledWith(- expect.objectContaining({ customerId: CALLER_ORG, entityId: "team-1" }),- );+ expect(sentBody()).toMatchObject({+ customer_id: CALLER_ORG,+ entity_id: "team-1",+ });});});diff --git a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts b/apps/api/src/services/autumn/__tests__/autumn.service.test.tsindex 9afa22eb7..a67d659d8 100644--- a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts@@ -10,7 +10,6 @@*/import { vi } from "vitest";-import { AutumnDefaultError } from "autumn-js";// ---------------------------------------------------------------------------// Mocks β vi.mock is hoisted above the module body, so the mock backing objects@@ -133,7 +132,6 @@ import {import {autumnCustomerGetOrCreateTotal,autumnEntityCreatedInlineTotal,- billingRouteTotal,} from "../metrics";// ---------------------------------------------------------------------------@@ -144,15 +142,39 @@ function makeService() {return new AutumnService();}-async function routeCount(route: string, operation: string): Promise<number> {- const { values } = await billingRouteTotal.get();- return (- values.find(- v => v.labels.route === route && v.labels.operation === operation,- )?.value ?? 0- );+const FIREBILL_CONFIG = {+ FIREBILL_URL: "http://firebill.test",+ FIREBILL_SECRET: "fb-secret",+};++const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();++/**+ * Configures firebill for the enclosing describe and answers every request+ * with `answer`, in a fresh Response each time (firebill reads the body, and a+ * reused one looks like a failure and triggers its retry).+ */+function useFirebill(answer: Record<string, unknown>) {+ beforeEach(() => {+ state.configRef = { ...FIREBILL_CONFIG };+ mockFetch.mockReset();+ mockFetch.mockImplementation(+ async () => new Response(JSON.stringify(answer), { status: 200 }),+ );+ vi.stubGlobal("fetch", mockFetch);+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });}+const firebillPaths = () =>+ mockFetch.mock.calls.map(([url]) => new URL(String(url)).pathname);++const firebillBodies = () =>+ mockFetch.mock.calls.map(([, init]) => JSON.parse(init.body));+function makeEntity(usage: number) {return { balances: { CREDITS: { usage } } };}@@ -303,12 +325,8 @@ describe("ensureTeamProvisioned", () => {const svc = makeService();mockEntityGet.mockRejectedValueOnce(error);- // Provisioning remains best-effort: a lookup failure must not stop billing.- await expect(- svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 5 }),- ).resolves.toBe(true);+ await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });expect(mockEntityCreate).not.toHaveBeenCalled();- expect(mockTrack).toHaveBeenCalledTimes(1);// The failed lookup must not mark the team as provisioned; retry can recover.await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });@@ -376,115 +394,86 @@ describe("ensureTrackingContext warm-cache short-circuit", () => {// ---------------------------------------------------------------------------// Org moves: the caller names the org on every billing call, so a move follows-// it β including re-provisioning the entity under the new customer.+// it.// ---------------------------------------------------------------------------describe("team org change", () => {- it("bills the new org and provisions the entity under it", async () => {+ useFirebill({ success: true, allowed: true, remaining: 0 });++ it("bills the new org", async () => {const svc = makeService();await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 1 });- expect(mockTrack).toHaveBeenLastCalledWith(- expect.objectContaining({ customerId: "org-1", entityId: "team-1" }),- );- const entityGetsBefore = mockEntityGet.mock.calls.length;-- // The entity does not exist under the new customer yet.- mockEntityGet.mockResolvedValue(null);-await svc.trackCredits({ teamId: "team-1", orgId: "org-2", value: 1 });- // Provisioning ran again for the (org-2, team-1) pair...- expect(mockEntityGet.mock.calls.length).toBe(entityGetsBefore + 1);- expect(mockEntityGet).toHaveBeenLastCalledWith(- expect.objectContaining({ customerId: "org-2", entityId: "team-1" }),- );- expect(mockEntityCreate).toHaveBeenCalledWith(- expect.objectContaining({ customerId: "org-2", entityId: "team-1" }),- );- // ...and usage is billed to the new org.- expect(mockTrack).toHaveBeenLastCalledWith(- expect.objectContaining({ customerId: "org-2", entityId: "team-1" }),- );-- // Warm again under org-2: no further provisioning calls.- await svc.trackCredits({ teamId: "team-1", orgId: "org-2", value: 1 });- expect(mockEntityGet.mock.calls.length).toBe(entityGetsBefore + 1);+ expect(firebillBodies().map(body => body.customer_id)).toEqual([+ "org-1",+ "org-2",+ ]);});it("checkCredits follows the org move too", async () => {const svc = makeService();await svc.checkCredits({ teamId: "team-1", orgId: "org-1", value: 1 });- expect(mockCheck).toHaveBeenLastCalledWith(- expect.objectContaining({ customerId: "org-1" }),- );-await svc.checkCredits({ teamId: "team-1", orgId: "org-2", value: 1 });- expect(mockCheck).toHaveBeenLastCalledWith(- expect.objectContaining({ customerId: "org-2" }),- );++ expect(firebillBodies().map(body => body.customer_id)).toEqual([+ "org-1",+ "org-2",+ ]);});});// ----------------------------------------------------------------------------// lockCredits+// Billing needs firebill, and only firebill: no Autumn client is consulted.// ----------------------------------------------------------------------------describe("lockCredits", () => {- function sdkError(status: number, body: string) {- return new AutumnDefaultError("Reservation failed", {- request: new Request("https://example.com/check"),- response: new Response(body, { status }),- body,- });- }+describe("billing without an Autumn client", () => {+ useFirebill({+ success: true,+ allowed: true,+ remaining: 5,+ lock_id: "lock-1",+ });- it("rejects an existing direct reservation without claiming its hold", async () => {- mockCheck.mockRejectedValue(- sdkError(409, JSON.stringify({ code: "lock_already_exists" })),- );- await expect(- makeService().lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 1,- lockId: "monitor_check-1",- }),- ).rejects.toMatchObject({- name: "ExistingCreditsLockError",- lockId: "monitor_check-1",- });- expect(mockFinalize).not.toHaveBeenCalled();- expect(mockTrack).not.toHaveBeenCalled();+ beforeEach(() => {+ state.autumnClientRef = null;});- it.each([- [409, JSON.stringify({ code: "other_conflict" })],- [409, JSON.stringify({ message: "lock_already_exists" })],- [409, "lock_already_exists"],- [409, "null"],- [500, JSON.stringify({ code: "lock_already_exists" })],- ])(- "retains the unavailable fallback for status %s and body %s",- async (status, body) => {- mockCheck.mockRejectedValue(sdkError(status, body));- await expect(- makeService().lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 1,- }),- ).resolves.toEqual({ status: "skipped" });- },- );+ it("still checks, locks, tracks, refunds and finalizes through firebill", async () => {+ const svc = makeService();+ const team = { teamId: "team-1", orgId: "org-1" };- it("does not identify contention from a plain error message", async () => {- mockCheck.mockRejectedValue(new Error("409 lock_already_exists"));- await expect(- makeService().lockCredits({ teamId: "team-1", orgId: "org-1", value: 1 }),- ).resolves.toEqual({ status: "skipped" });+ expect(await svc.checkCredits({ ...team, value: 1 })).toEqual({+ allowed: true,+ remaining: 5,+ });+ expect(+ await svc.lockCredits({ ...team, value: 1, lockId: "lock-1" }),+ ).toEqual({ status: "locked", lockId: "lock-1" });+ expect(await svc.trackCredits({ ...team, value: 1 })).toBe(true);+ expect(await svc.refundCredits({ ...team, value: 1 })).toBe(true);+ expect(+ await svc.finalizeCreditsLock({ lockId: "lock-1", action: "release" }),+ ).toBe(true);++ expect(firebillPaths()).toEqual([+ "/v1/check",+ "/v1/lock",+ "/v1/track",+ "/v1/refund",+ "/v1/finalize",+ ]);});+});++// ---------------------------------------------------------------------------+// lockCredits+// ---------------------------------------------------------------------------++describe("lockCredits", () => {+ useFirebill({ success: true, allowed: true, lock_id: "monitor_check-1" });it("preserves successful zero-credit reservations", async () => {await expect(@@ -495,13 +484,11 @@ describe("lockCredits", () => {lockId: "monitor_check-1",}),).resolves.toEqual({ status: "locked", lockId: "monitor_check-1" });- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({ requiredBalance: 0 }),- );+ expect(firebillBodies()[0]).toMatchObject({ value: 0 });});- it("returns skipped when autumnClient is null", async () => {- state.autumnClientRef = null;+ it("returns skipped without asking anyone when firebill is not configured", async () => {+ state.configRef = {};const svc = makeService();const result = await svc.lockCredits({teamId: "team-1",@@ -509,73 +496,31 @@ describe("lockCredits", () => {value: 10,});expect(result).toEqual({ status: "skipped" });+ expect(mockFetch).not.toHaveBeenCalled();expect(mockCheck).not.toHaveBeenCalled();});- it("returns skipped for preview teams", async () => {- const svc = makeService();- const result = await svc.lockCredits({- teamId: "preview_abc",- orgId: "org-1",- value: 10,- });- expect(result).toEqual({ status: "skipped" });- expect(mockCheck).not.toHaveBeenCalled();- });-- it("returns the lockId on happy path", async () => {- const svc = makeService();-- const result = await svc.lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 42,- lockId: "lock-123",- properties: { source: "billTeam", endpoint: "extract" },- });-- expect(result).toEqual({ status: "locked", lockId: "lock-123" });- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({- customerId: "org-1",- entityId: "team-1",- featureId: "CREDITS",- requiredBalance: 42,- properties: { source: "billTeam", endpoint: "extract" },- lock: expect.objectContaining({- enabled: true,- lockId: "lock-123",- }),- }),- );- });-- it("returns denied when Autumn denies the lock", async () => {- mockCheck.mockResolvedValue({- allowed: false,- customerId: "org-1",- balance: null,- });- const svc = makeService();- const result = await svc.lockCredits({+ it("denies a gated hold when firebill is not configured, rather than skipping the gate", async () => {+ state.configRef = {};+ const result = await makeService().lockCredits({teamId: "team-1",orgId: "org-1",value: 10,- lockId: "lock-123",+ partnerJobToken: "job-token",});- expect(result).toEqual({ status: "denied" });+ expect(result).toEqual({ status: "denied", reason: "gate_unavailable" });+ expect(mockFetch).not.toHaveBeenCalled();});- it("returns skipped when the billing API throws (fallback)", async () => {- mockCheck.mockRejectedValue(new Error("autumn down"));+ it("returns skipped for preview teams", async () => {const svc = makeService();const result = await svc.lockCredits({- teamId: "team-1",+ teamId: "preview_abc",orgId: "org-1",value: 10,- lockId: "lock-123",});expect(result).toEqual({ status: "skipped" });+ expect(mockFetch).not.toHaveBeenCalled();});});@@ -584,8 +529,10 @@ describe("lockCredits", () => {// ---------------------------------------------------------------------------describe("checkCredits", () => {- it("returns null when autumnClient is null", async () => {- state.autumnClientRef = null;+ useFirebill({ success: true, allowed: true, remaining: 500 });++ it("returns null without asking anyone when firebill is not configured", async () => {+ state.configRef = {};const svc = makeService();const result = await svc.checkCredits({teamId: "team-1",@@ -593,51 +540,19 @@ describe("checkCredits", () => {value: 10,});expect(result).toBeNull();+ expect(mockFetch).not.toHaveBeenCalled();expect(mockCheck).not.toHaveBeenCalled();});- it("returns allowed and remaining on happy path without a lock", async () => {- mockCheck.mockResolvedValue({- allowed: true,- customerId: "org-1",- balance: { remaining: 500 },- });- const svc = makeService();- const result = await svc.checkCredits({- teamId: "team-1",- orgId: "org-1",- value: 42,- properties: { source: "checkCreditsMiddleware" },- });-- expect(result).toEqual({ allowed: true, remaining: 500 });- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({- customerId: "org-1",- entityId: "team-1",- featureId: "CREDITS",- requiredBalance: 42,- properties: { source: "checkCreditsMiddleware" },- }),- );- expect(mockCheck).toHaveBeenCalledWith(- expect.not.objectContaining({ lock: expect.anything() }),- );- });-- it("returns allowed false with remaining 0 when balance is null", async () => {- mockCheck.mockResolvedValue({- allowed: false,- customerId: "org-1",- balance: null,- });+ it("returns null for preview teams", async () => {const svc = makeService();const result = await svc.checkCredits({- teamId: "team-1",+ teamId: "preview_abc",orgId: "org-1",value: 10,});- expect(result).toEqual({ allowed: false, remaining: 0 });+ expect(result).toBeNull();+ expect(mockFetch).not.toHaveBeenCalled();});it("checks against SEARCH_CREDITS when featureId is provided", async () => {@@ -648,9 +563,9 @@ describe("checkCredits", () => {value: 5,featureId: "SEARCH_CREDITS",});- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({ featureId: "SEARCH_CREDITS" }),- );+ expect(firebillBodies()[0]).toMatchObject({+ feature_id: "SEARCH_CREDITS",+ });});});@@ -659,8 +574,10 @@ describe("checkCredits", () => {// ---------------------------------------------------------------------------describe("trackCredits", () => {- it("returns false when autumnClient is null", async () => {- state.autumnClientRef = null;+ useFirebill({ success: true });++ it("returns false without asking anyone when firebill is not configured", async () => {+ state.configRef = {};const svc = makeService();const result = await svc.trackCredits({teamId: "team-1",@@ -668,6 +585,7 @@ describe("trackCredits", () => {value: 10,});expect(result).toBe(false);+ expect(mockFetch).not.toHaveBeenCalled();expect(mockTrack).not.toHaveBeenCalled();});@@ -679,37 +597,7 @@ describe("trackCredits", () => {value: 10,});expect(result).toBe(false);- expect(mockTrack).not.toHaveBeenCalled();- });-- it("calls track with correct feature and value on happy path", async () => {- const svc = makeService();-- const result = await svc.trackCredits({- teamId: "team-1",- orgId: "org-1",- value: 42,- properties: { source: "test", endpoint: "extract" },- });-- expect(result).toBe(true);- // track should have been called for the actual usage event (at minimum).- const trackCalls = mockTrack.mock.calls;- const usageCall = trackCalls.find(- (c: any[]) => c[0].featureId === "CREDITS" && c[0].value === 42,- );- expect(usageCall).toBeDefined();- expect((usageCall as any[])[0].properties?.endpoint).toBe("extract");- expect((usageCall as any[])[0].overageBehavior).toBe("overflow");- });-- it("returns false when the Autumn track request fails", async () => {- mockTrack.mockRejectedValueOnce(new Error("track failed"));- const svc = makeService();-- expect(- await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 42 }),- ).toBe(false);+ expect(mockFetch).not.toHaveBeenCalled();});it("tracks against SEARCH_CREDITS when featureId is provided", async () => {@@ -724,9 +612,10 @@ describe("trackCredits", () => {});expect(result).toBe(true);- const usageCall = mockTrack.mock.calls.find((c: any[]) => c[0].value === 7);- expect(usageCall).toBeDefined();- expect((usageCall as any[])[0].featureId).toBe("SEARCH_CREDITS");+ expect(firebillBodies()[0]).toMatchObject({+ feature_id: "SEARCH_CREDITS",+ value: 7,+ });});});@@ -735,26 +624,22 @@ describe("trackCredits", () => {// ---------------------------------------------------------------------------describe("finalizeCreditsLock", () => {- it("calls balances.finalize with confirm", async () => {- const svc = makeService();- await svc.finalizeCreditsLock({- lockId: "lock-123",- action: "confirm",- properties: { source: "test" },- });+ useFirebill({ success: true });- expect(mockFinalize).toHaveBeenCalledWith({- lockId: "lock-123",- action: "confirm",- overrideValue: undefined,- properties: { source: "test" },- });- });-- it("is a no-op when autumnClient is null", async () => {- state.autumnClientRef = null;+ // No hold can have been taken without firebill, so there is nothing to+ // settle β and Autumn is not asked to settle it instead.+ it("is a no-op that reports settled when firebill is not configured", async () => {+ state.configRef = {};const svc = makeService();- await svc.finalizeCreditsLock({ lockId: "lock-123", action: "release" });++ expect(+ await svc.finalizeCreditsLock({+ lockId: "lock-123",+ action: "release",+ team: { teamId: "team-1", orgId: "org-1" },+ }),+ ).toBe(true);+ expect(mockFetch).not.toHaveBeenCalled();expect(mockFinalize).not.toHaveBeenCalled();});});@@ -764,24 +649,7 @@ describe("finalizeCreditsLock", () => {// ---------------------------------------------------------------------------describe("refundCredits", () => {- it("calls track with the negated value", async () => {- const svc = makeService();- await svc.refundCredits({- teamId: "team-1",- orgId: "org-1",- value: 30,- properties: { endpoint: "extract" },- });-- const refundCall = mockTrack.mock.calls.find(- (c: any[]) => c[0].value === -30,- );- expect(refundCall).toBeDefined();- expect((refundCall as any[])[0].properties?.source).toBe("autumn_refund");- expect((refundCall as any[])[0].properties?.endpoint).toBe("extract");- expect((refundCall as any[])[0].featureId).toBe("CREDITS");- expect((refundCall as any[])[0].overageBehavior).toBe("overflow");- });+ useFirebill({ success: true });it("refunds against SEARCH_CREDITS when featureId is provided", async () => {const svc = makeService();@@ -793,17 +661,20 @@ describe("refundCredits", () => {featureId: "SEARCH_CREDITS",});- const refundCall = mockTrack.mock.calls.find(- (c: any[]) => c[0].value === -1,- );- expect(refundCall).toBeDefined();- expect((refundCall as any[])[0].featureId).toBe("SEARCH_CREDITS");+ expect(firebillPaths()).toEqual(["/v1/refund"]);+ expect(firebillBodies()[0]).toMatchObject({+ feature_id: "SEARCH_CREDITS",+ value: 1,+ });});- it("is a no-op when autumnClient is null", async () => {- state.autumnClientRef = null;+ it("is a no-op when firebill is not configured", async () => {+ state.configRef = {};const svc = makeService();- await svc.refundCredits({ teamId: "team-1", orgId: "org-1", value: 30 });+ expect(+ await svc.refundCredits({ teamId: "team-1", orgId: "org-1", value: 30 }),+ ).toBe(false);+ expect(mockFetch).not.toHaveBeenCalled();expect(mockTrack).not.toHaveBeenCalled();});@@ -814,7 +685,7 @@ describe("refundCredits", () => {orgId: "org-1",value: 30,});- expect(mockTrack).not.toHaveBeenCalled();+ expect(mockFetch).not.toHaveBeenCalled();});});@@ -1048,10 +919,10 @@ describe("getTeamLimits", () => {});// ----------------------------------------------------------------------------// firebill routing+// firebill requests// ----------------------------------------------------------------------------describe("firebill routing", () => {+describe("firebill requests", () => {const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();const firebillConfig = () => ({@@ -1338,21 +1209,6 @@ describe("firebill routing", () => {expect(mockCheck).not.toHaveBeenCalled();});- it("checks directly in Autumn when firebill is not configured", async () => {- state.configRef = {};- const svc = makeService();-- await svc.checkCredits({- teamId: "team-1",- orgId: "org-1",- value: 1,- properties: {},- });-- expect(mockCheck).toHaveBeenCalledTimes(1);- expect(mockFetch).not.toHaveBeenCalled();- });-it("routes refundCredits to /v1/refund with the POSITIVE value", async () => {state.configRef = firebillConfig();const svc = makeService();@@ -1405,14 +1261,16 @@ describe("firebill routing", () => {expect(await svc.hostedMcpFeatureId("team-1", "CREDITS")).toBe("CREDITS");});- it("uses the direct Autumn path when FIREBILL_SECRET is missing", async () => {+ it("skips billing when FIREBILL_SECRET is missing β no Autumn fallback", async () => {state.configRef = { ...firebillConfig(), FIREBILL_SECRET: undefined };const svc = makeService();- await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 42 });+ expect(+ await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 42 }),+ ).toBe(false);expect(mockFetch).not.toHaveBeenCalled();- expect(mockTrack).toHaveBeenCalledTimes(1);+ expect(mockTrack).not.toHaveBeenCalled();});it("returns false when firebill responds success:false β no Autumn fallback", async () => {@@ -1748,23 +1606,6 @@ describe("firebill routing", () => {).toEqual({ status: "denied" });});- // Unreachable (#4403), but a token here would silently skip the gate.- it("refuses a hold when a job token reaches the direct-Autumn path", async () => {- state.configRef = {};- const svc = makeService();-- expect(- await svc.lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 10,- lockId: "lock-1",- partnerJobToken: "job-token-abc",- }),- ).toEqual({ status: "denied", reason: "gate_unavailable" });- expect(mockCheck).not.toHaveBeenCalled();- });-// firebill cannot fail closed on its own behalf when it is what is down.it("denies a gated run when firebill itself cannot answer", async () => {state.configRef = firebillConfig();@@ -1979,22 +1820,6 @@ describe("firebill routing", () => {);});- it("uses the direct Autumn lock path when firebill is not configured", async () => {- state.configRef = {};- const svc = makeService();-- const result = await svc.lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 10,- lockId: "lock-1",- });-- expect(result).toEqual({ status: "locked", lockId: "lock-1" });- expect(mockFetch).not.toHaveBeenCalled();- expect(mockCheck).toHaveBeenCalledTimes(1);- });-it("routes finalizeCreditsLock through /v1/finalize for a team", async () => {state.configRef = firebillConfig();const svc = makeService();@@ -2042,8 +1867,6 @@ describe("firebill routing", () => {it("routes an orgless finalize through /v1/finalize, keyed like any other", async () => {state.configRef = firebillConfig();const svc = makeService();- const before = await routeCount("firebill", "finalize");-const settled = await svc.finalizeCreditsLock({lockId: "monitor_check-1",action: "confirm",@@ -2064,11 +1887,10 @@ describe("firebill routing", () => {properties: { source: "monitorCheck", endpoint: "monitor" },idempotency_key: "fc:finalize:confirm:monitor_check-1",});- expect(await routeCount("firebill", "finalize")).toBe(before + 1);});- // Same contract as the direct settle it replaces: one attempt, and `false`- // back to the caller when it did not land. No retry is added here.+ // One attempt, and `false` back to the caller when it did not land. No+ // retry is added here.it("answers false for an orgless finalize firebill did not take, after one attempt", async () => {state.configRef = firebillConfig();mockFetch.mockResolvedValue(new Response("nope", { status: 503 }));@@ -2085,32 +1907,6 @@ describe("firebill routing", () => {expect(mockFinalize).not.toHaveBeenCalled();expect(retryQueueAdd).not.toHaveBeenCalled();});-- it("finalizes an orgless lock directly in Autumn, and counts it, when firebill is not configured", async () => {- state.configRef = {};- const svc = makeService();- const before = await routeCount("direct", "finalize");-- await svc.finalizeCreditsLock({ lockId: "lock-1", action: "release" });-- expect(mockFetch).not.toHaveBeenCalled();- expect(mockFinalize).toHaveBeenCalledTimes(1);- expect(await routeCount("direct", "finalize")).toBe(before + 1);- });-- it("finalizes a team's lock directly in Autumn when firebill is not configured", async () => {- state.configRef = {};- const svc = makeService();-- await svc.finalizeCreditsLock({- lockId: "lock-1",- action: "confirm",- team: { teamId: "team-1", orgId: "org-1" },- });-- expect(mockFetch).not.toHaveBeenCalled();- expect(mockFinalize).toHaveBeenCalledTimes(1);- });});// ---------------------------------------------------------------------------@@ -2123,21 +1919,6 @@ describe("the org the caller supplies", () => {// That the service has no way to look an org up at all is guarded by// autumn.service.org-source.test.ts, which mocks controllers/auth to throw.- it("bills the caller's org", async () => {- const svc = makeService();-- const result = await svc.checkCredits({- teamId: "team-1",- orgId: CALLER_ORG,- value: 42,- });-- expect(result).toEqual({ allowed: true, remaining: 0 });- expect(mockCheck).toHaveBeenCalledWith(- expect.objectContaining({ customerId: CALLER_ORG, entityId: "team-1" }),- );- });-it("sends the caller's org to firebill as the customer", async () => {const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();mockFetch.mockResolvedValue(@@ -2237,13 +2018,12 @@ describe("inline provisioning counters", () => {});// ----------------------------------------------------------------------------// Who provisions, by route (Β§2 of the middleware credit-check spec). On the-// firebill route the monolith no longer runs its own customer/entity prefix:-// firebill creates a missing entity itself, off the 404 Autumn answers the-// billing call with. The direct route keeps provisioning exactly as it did.+// Who provisions (Β§2 of the middleware credit-check spec). The monolith no+// longer runs its own customer/entity prefix: firebill creates a missing+// entity itself, off the 404 Autumn answers the billing call with.// ----------------------------------------------------------------------------describe("provisioning by route", () => {+describe("provisioning", () => {const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();const firebillConfig = () => ({@@ -2251,10 +2031,6 @@ describe("provisioning by route", () => {FIREBILL_SECRET: "fb-secret",});- // The same team with firebill unconfigured, so it bills straight to Autumn.- // The route is the only thing that differs between the two halves.- const directConfig = () => ({});-// A fresh Response per call: firebill reads the body, and a reused one looks// like a failure and triggers its retry.const answers = (body: Record<string, unknown>) => () =>@@ -2447,80 +2223,4 @@ describe("provisioning by route", () => {expect(body.feature_id).toBe("CREDITS");expect(body.held_value).toBe(12);});-- // Skipping the prefix must not also claim the team is provisioned: nothing- // here checked the entity, so a later direct-route operation still has to.- it("leaves ensuredTeams cold, so the direct route still provisions", async () => {- const svc = makeService();-- await svc.checkCredits({ teamId: "team-1", orgId: "org-1", value: 1 });- expectNoProvisioning();-- state.configRef = directConfig();- await svc.checkCredits({ teamId: "team-1", orgId: "org-1", value: 1 });-- expect(mockGetOrCreate).toHaveBeenCalledTimes(1);- expect(mockEntityGet).toHaveBeenCalledTimes(1);- });-- const operations: Record<string, (svc: AutumnService) => Promise<unknown>> = {- check: svc =>- svc.checkCredits({ teamId: "team-1", orgId: "org-1", value: 1 }),- lock: svc =>- svc.lockCredits({- teamId: "team-1",- orgId: "org-1",- value: 1,- lockId: "lock-1",- }),- track: svc =>- svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 1 }),- refund: svc =>- svc.refundCredits({ teamId: "team-1", orgId: "org-1", value: 1 }),- };-- describe.each(Object.entries(operations))(- "the direct route's %s",- (_name, run) => {- beforeEach(() => {- state.configRef = directConfig();- });-- it("provisions a cold team: getOrCreate then get, no create", async () => {- const svc = makeService();-- await run(svc);-- expect(mockGetOrCreate).toHaveBeenCalledTimes(1);- expect(mockEntityGet).toHaveBeenCalledTimes(1);- expect(mockEntityCreate).not.toHaveBeenCalled();- expect(mockFetch).not.toHaveBeenCalled();- });-- it("creates the entity Autumn answers 404 for", async () => {- mockEntityGet.mockRejectedValue({ statusCode: 404 });- const svc = makeService();-- await run(svc);-- expect(mockEntityCreate).toHaveBeenCalledTimes(1);- expect(mockEntityCreate).toHaveBeenCalledWith({- customerId: "org-1",- entityId: "team-1",- featureId: "TEAM",- });- expect(mockFetch).not.toHaveBeenCalled();- });-- it("provisions nothing once the team is ensured", async () => {- const svc = makeService();-- await run(svc);- vi.clearAllMocks();- await run(svc);-- expectNoProvisioning();- });- },- );});diff --git a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts b/apps/api/src/services/autumn/__tests__/mcp-credits.test.tsindex 68e0da6f0..0fa1d5400 100644--- a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts+++ b/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts@@ -102,68 +102,6 @@ describe("hostedMcpFeatureId", () => {});describe("the entity a charge is made against", () => {- it("tracks an MCP credits charge once, whole, on the customer", async () => {- expect(- await new AutumnService().trackCredits({- ...charge,- featureId: MCP_CREDITS_FEATURE_ID,- }),- ).toBe(true);-- expect(mockTrack).toHaveBeenCalledOnce();- const [track] = mockTrack.mock.calls[0];- expect(track).toMatchObject({- customerId: "org-1",- featureId: MCP_CREDITS_FEATURE_ID,- value: 5,- overageBehavior: "overflow",- });- expect(track.entityId).toBeUndefined();- expect(mockAutumnClient.customers.get).not.toHaveBeenCalled();- });-- it("refunds MCP credits on the customer and other features on the team", async () => {- const service = new AutumnService();-- await service.refundCredits({- ...charge,- featureId: MCP_CREDITS_FEATURE_ID,- });- await service.refundCredits(charge);-- const [[mcp], [search]] = mockTrack.mock.calls;- expect(mcp).toMatchObject({ featureId: MCP_CREDITS_FEATURE_ID, value: -5 });- expect(mcp.entityId).toBeUndefined();- expect(search).toMatchObject({- featureId: "SEARCH_CREDITS",- entityId: "team-1",- value: -5,- });- });-- it("checks and holds MCP credits on the customer and other features on the team", async () => {- const service = new AutumnService();-- await service.checkCredits({- ...charge,- featureId: MCP_CREDITS_FEATURE_ID,- });- await service.lockCredits({ ...charge, featureId: MCP_CREDITS_FEATURE_ID });- await service.checkCredits(charge);-- const [[check], [hold], [search]] = mockCheck.mock.calls;- expect(check.entityId).toBeUndefined();- expect(hold).toMatchObject({- featureId: MCP_CREDITS_FEATURE_ID,- lock: expect.objectContaining({ enabled: true }),- });- expect(hold.entityId).toBeUndefined();- expect(search).toMatchObject({- featureId: "SEARCH_CREDITS",- entityId: "team-1",- });- });-describe("through firebill", () => {const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();const bodies = (path: string) =>@@ -204,6 +142,33 @@ describe("the entity a charge is made against", () => {]);});+ it("refunds MCP credits with an empty entity and other features on the team", async () => {+ mockFetch.mockImplementation(+ async () => new Response(JSON.stringify({ success: true })),+ );+ const service = new AutumnService();++ await service.refundCredits({+ ...charge,+ featureId: MCP_CREDITS_FEATURE_ID,+ });+ await service.refundCredits(charge);++ expect(mockTrack).not.toHaveBeenCalled();+ expect(bodies("/v1/refund")).toEqual([+ expect.objectContaining({+ entity_id: "",+ feature_id: MCP_CREDITS_FEATURE_ID,+ value: 5,+ }),+ expect.objectContaining({+ entity_id: "team-1",+ feature_id: "SEARCH_CREDITS",+ value: 5,+ }),+ ]);+ });+it("carries the MCP allowance's reset time from firebill", async () => {mockFetch.mockImplementation(async () =>@@ -230,7 +195,7 @@ describe("the entity a charge is made against", () => {);});- it("checks and holds MCP credits with an empty entity", async () => {+ it("checks and holds MCP credits with an empty entity and other features on the team", async () => {mockFetch.mockImplementation(async () =>new Response(@@ -253,6 +218,7 @@ describe("the entity a charge is made against", () => {featureId: MCP_CREDITS_FEATURE_ID,lockId: "lock-1",});+ await service.checkCredits(charge);expect(mockCheck).not.toHaveBeenCalled();expect(bodies("/v1/check")).toEqual([@@ -260,6 +226,10 @@ describe("the entity a charge is made against", () => {entity_id: "",feature_id: MCP_CREDITS_FEATURE_ID,}),+ expect.objectContaining({+ entity_id: "team-1",+ feature_id: "SEARCH_CREDITS",+ }),]);expect(bodies("/v1/lock")).toEqual([expect.objectContaining({diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.tsindex e5b92fa04..1f2d6b8b6 100644--- a/apps/api/src/services/autumn/autumn.service.ts+++ b/apps/api/src/services/autumn/autumn.service.ts@@ -1,5 +1,4 @@import { randomUUID } from "crypto";-import { AutumnError } from "autumn-js";import { logger } from "../../lib/logger";import { eq } from "drizzle-orm";import { dbRr } from "../../db/connection";@@ -16,7 +15,6 @@ import {import {autumnCustomerGetOrCreateTotal,autumnEntityCreatedInlineTotal,- billingRouteTotal,} from "./metrics";import type {CreateEntityParams,@@ -29,9 +27,7 @@ import type {LockCreditsParams,LockCreditsResult,TrackCreditsParams,- TrackParams,} from "./types";-import { ExistingCreditsLockError } from "./types";export const TEAM_FEATURE_ID = "TEAM";export const CREDITS_FEATURE_ID = "CREDITS";@@ -314,89 +310,6 @@ export class AutumnService {}}- /**- * Whether this team's usage is currently routed through firebill. Used by- * billers to pick route-specific failure handling (on the firebill route a- * recorded charge stands and dedupes, so compensating refunds and duplicate- * enqueues behave differently). Every non-preview team routes whenever- * firebill is configured.- */- async isRoutedThroughFirebill(- teamId: string,- _orgId: string,- ): Promise<boolean> {- if (this.isPreviewTeam(teamId)) return false;- return firebillConfigured();- }-- /**- * `routed` is decided by the caller (see {@link resolveBillingRoute}) rather- * than asked again here: the answer costs a `partner_provisioned_accounts`- * read on a cold team, and the caller already needed it to know whether to- * provision.- */- private async track(- {- customerId,- entityId,- featureId,- value,- properties,- idempotencyKey,- externalRequestId,- durableRetry,- }: TrackParams,- routed: boolean,- ): Promise<boolean> {- // Every org bills through firebill when it is configured. No fallback- // to Autumn on failure β firebill may already own the event, and the SDK- // sends no idempotency key, so the pair could not be deduped.- if (routed) {- billingRouteTotal.labels("firebill", "track").inc();- return await firebillTrack({- customerId,- entityId,- featureId,- value,- properties,- idempotencyKey,- externalRequestId,- durableRetry,- });- }-- billingRouteTotal.labels("direct", "track").inc();-- if (!autumnClient) return false;-- try {- await autumnClient.track({- customerId,- entityId,- featureId,- value,- properties,- overageBehavior: "overflow",- });- logger.info("Autumn track succeeded", {- customerId,- entityId,- featureId,- value,- });- return true;- } catch (error) {- logger.error("Autumn track failed β billing API may be unavailable", {- customerId,- entityId,- featureId,- value,- error,- });- return false;- }- }-/*** Ensures the Autumn customer exists for an org, caching successful lookups in-process.*/@@ -470,38 +383,9 @@ export class AutumnService {}/**- * Decides the route a team's billing takes, and warms the customer/entity- * context the direct route needs. The customer is the caller's own org,- * which this service never looks up for itself.- *- * **Provisioning runs on the direct route only.** firebill creates a missing- * entity itself now, off the 404 `entity_not_found` Autumn answers the- * billing call with, so the get/create prefix here would be a second,- * redundant round trip β one that cannot change the answer either way,- * because its failure is caught and the operation proceeds regardless.- *- * A team already provisioned under this org skips ensureTeamProvisioned- * entirely.- */- private async resolveBillingRoute(- teamId: string,- orgId: string,- ): Promise<{- customerId: string;- routed: boolean;- gatewayProvisioned: boolean;- }> {- const gatewayProvisioned = await this.isGatewayProvisioned(teamId);- const routed = firebillConfigured();- if (!routed && !this.ensuredTeams.has(this.ensuredTeamKey(orgId, teamId))) {- await this.ensureTeamProvisioned({ teamId, orgId });- }- return { customerId: orgId, routed, gatewayProvisioned };- }-- /**- * Checks whether a team has enough Autumn balance to cover a request.- * Returns null when Autumn gating is unavailable and callers should fall back.+ * Checks whether a team has enough balance to cover a request, through+ * firebill. Returns null when gating is unavailable and callers should fall+ * back.*/async checkCredits({teamId,@@ -517,68 +401,31 @@ export class AutumnService {/** Set only for a Gateway account, funded by the platform that created it. */gatewayProvisioned?: boolean;} | null> {- if (!autumnClient || this.isPreviewTeam(teamId)) {+ if (!firebillConfigured() || this.isPreviewTeam(teamId)) {return null;}try {- const { customerId, routed, gatewayProvisioned } =- await this.resolveBillingRoute(teamId, orgId);-- // Mirrors track() and lockCredits(). Without this branch the gate reads- // the ghost's balance alone, and a gateway ghost is designed to spend- // credits it does not have β so it would 402 exactly the requests the- // partner pool exists to fund. firebill answers with the same arithmetic- // settlement uses.- //- // `unavailable` becomes `null`, which this method's existing contract- // already means "fail open" β the same answer a null from Autumn gets- // below, for the same reason.- //- // `gatewayProvisioned` matters more here than on the charge paths. A- // partner-provisioned org that misses firebill on a *charge* is billed to- // the wrong account; one that misses firebill on the *gate* is refused- // outright, because Autumn is asked about a balance the org was never- // meant to pay from.- if (routed) {- const result = await firebillCheck({- customerId,- entityId: billingEntityId(featureId, teamId) ?? "",- featureId,- value,- properties,- });- if (result.status === "unavailable") return null;- return {- allowed: result.allowed,- remaining: result.remaining,- ...(result.resetsAt ? { resetsAt: result.resetsAt } : {}),- ...(gatewayProvisioned ? { gatewayProvisioned } : {}),- };- }+ const gatewayProvisioned = await this.isGatewayProvisioned(teamId);- const { allowed, balance } = await autumnClient.check({- customerId,- entityId: billingEntityId(featureId, teamId),- featureId,- requiredBalance: value,- properties,- });-- const remaining = balance?.remaining ?? 0;- const resetsAt = balance?.nextResetAt ?? undefined;-- logger.debug("Autumn checkCredits completed", {- customerId,- entityId: teamId,+ // firebill answers with the same arithmetic settlement uses, so a+ // gateway ghost's gate counts the partner pool rather than the ghost's+ // own balance β which a ghost is designed to overspend, and which would+ // otherwise 402 exactly the requests the pool exists to fund.+ //+ // `unavailable` becomes `null`, which this method's contract already+ // means "fail open".+ const result = await firebillCheck({+ customerId: orgId,+ entityId: billingEntityId(featureId, teamId) ?? "",featureId,value,- allowed,- remaining,+ properties,});+ if (result.status === "unavailable") return null;return {- allowed,- remaining,- ...(resetsAt ? { resetsAt } : {}),+ allowed: result.allowed,+ remaining: result.remaining,+ ...(result.resetsAt ? { resetsAt: result.resetsAt } : {}),...(gatewayProvisioned ? { gatewayProvisioned } : {}),};} catch (error) {@@ -634,7 +481,8 @@ export class AutumnService {}/**- * Attempts to reserve a team's credits in Autumn. See {@link LockCreditsResult}.+ * Attempts to reserve a team's credits through firebill. See+ * {@link LockCreditsResult}.*/async lockCredits({teamId,@@ -646,7 +494,16 @@ export class AutumnService {partnerJobToken,orgId,}: LockCreditsParams): Promise<LockCreditsResult> {- if (!autumnClient || this.isPreviewTeam(teamId)) {+ // Only firebill can ask the partner, so a gated run with no firebill would+ // silently skip the gate. Refused, as the direct lock path used to.+ if (partnerJobToken && !firebillConfigured()) {+ logger.error(+ "A partner job token reached lockCredits with firebill unconfigured, where no partner can be asked; refusing the hold",+ { teamId },+ );+ return { status: "denied", reason: "gate_unavailable" };+ }+ if (!firebillConfigured() || this.isPreviewTeam(teamId)) {return { status: "skipped" };}const resolvedLockId = lockId ?? `billing_${randomUUID()}`;@@ -662,105 +519,40 @@ export class AutumnService {: { status: "skipped" };try {- const { customerId, routed } = await this.resolveBillingRoute(- teamId,- orgId,- );-- // Mirroring track(): holds go through firebill when it is configured. The hold still lives in Autumn (firebill- // keeps no lock state), but only firebill pins the retry/timeout budget- // around the call. An unavailable answer maps to "skipped" β proceed- // unlocked β like a direct-Autumn check failure below, except when a- // partner gate is involved; see `unreachable` above.- if (routed) {- const result = await firebillLock({- customerId,- entityId: billingEntityId(featureId, teamId) ?? "",- featureId,- value,- lockId: resolvedLockId,- // firebill requires an expiry (Autumn releasing the hold by itself- // is what makes firebill lock-table-free); default to an hour, the- // monitor runner's convention, when the caller sets none.- expiresAt: expiresAt ?? Date.now() + 60 * 60 * 1000,- properties,- partnerJobToken,- });- if (result.status === "locked") {- return {- status: "locked",- lockId: result.lockId,- ...(result.operationToken- ? { operationToken: result.operationToken }- : {}),- };- }- if (result.status === "denied") {- return {- status: "denied",- ...(result.reason ? { reason: result.reason } : {}),- };- }- return unreachable();- }-- // Unreachable β a partner-provisioned org always routes to firebill- // (#4403) β but a token here would silently skip the gate.- if (partnerJobToken) {- logger.error(- "A partner job token reached the direct-Autumn lock path, where no partner can be asked; refusing the hold",- { teamId, lockId: resolvedLockId },- );- return { status: "denied", reason: "gate_unavailable" };- }-- const entityId = billingEntityId(featureId, teamId);- const { allowed } = await autumnClient- .check({- customerId,- entityId,- featureId,- requiredBalance: value,- properties,- lock: {- enabled: true,- lockId: resolvedLockId,- expiresAt,- },- })- .catch(error => {- if (error instanceof AutumnError && error.statusCode === 409) {- let body: { code?: unknown } | null = null;- try {- body = JSON.parse(error.body);- } catch {}- if (body?.code === "lock_already_exists") {- throw new ExistingCreditsLockError(resolvedLockId);- }- }- throw error;- });-- if (!allowed) {- logger.info("Autumn lockCredits denied", {- teamId,- value,- lockId: resolvedLockId,- });- return { status: "denied" };- }-- logger.info("Autumn lockCredits succeeded", {- customerId,- entityId,+ // The hold still lives in Autumn (firebill keeps no lock state), but+ // only firebill pins the retry/timeout budget around the call. An+ // unavailable answer maps to "skipped" β proceed unlocked β except when+ // a partner gate is involved; see `unreachable` above.+ const result = await firebillLock({+ customerId: orgId,+ entityId: billingEntityId(featureId, teamId) ?? "",featureId,value,lockId: resolvedLockId,+ // firebill requires an expiry (Autumn releasing the hold by itself+ // is what makes firebill lock-table-free); default to an hour, the+ // monitor runner's convention, when the caller sets none.+ expiresAt: expiresAt ?? Date.now() + 60 * 60 * 1000,properties,+ partnerJobToken,});- return { status: "locked", lockId: resolvedLockId };+ if (result.status === "locked") {+ return {+ status: "locked",+ lockId: result.lockId,+ ...(result.operationToken+ ? { operationToken: result.operationToken }+ : {}),+ };+ }+ if (result.status === "denied") {+ return {+ status: "denied",+ ...(result.reason ? { reason: result.reason } : {}),+ };+ }+ return unreachable();} catch (error) {- if (error instanceof ExistingCreditsLockError) throw error;logger.error("Autumn lockCredits failed β billing API may be unavailable, falling back",{@@ -776,20 +568,13 @@ export class AutumnService {}/**- * Finalizes a previously-acquired Autumn lock.- *- * When firebill is configured the settle goes through it, which queues it- * durably and retries delivery β a dropped direct finalize means the hold- * just expires, leaving a confirm's work unbilled. Either route lands on the- * same Autumn lock, so routing is a durability choice, not a correctness one.+ * Finalizes a previously-acquired Autumn lock through firebill, which+ * queues the settle durably and retries delivery β a dropped settle means+ * the hold just expires, leaving a confirm's work unbilled.** A caller that cannot name the org passes no team. The lock id is all a- * finalize needs, so an ordinary settle loses nothing by not knowing the org- * (a run-token settle loses its partner report, as below).- *- * **Except with a run token in hand**, where it is a correctness choice: only- * firebill reports the operation to the partner. The token is proof of the- * route the lock took, so it wins over asking again.+ * finalize needs, so an ordinary settle loses nothing by not knowing the org;+ * a run-token settle loses its partner report, as below.*/async finalizeCreditsLock({lockId,@@ -801,79 +586,42 @@ export class AutumnService {featureId = CREDITS_FEATURE_ID,heldValue,}: FinalizeCreditsLockParams): Promise<boolean> {- const gated = Boolean(externalRequestId) && firebillConfigured();- if (- gated ||- (team- ? await this.isRoutedThroughFirebill(team.teamId, team.orgId)- : firebillConfigured())- ) {- // Named only for a gated settle: firebill needs the org to split the- // settle and to find the integration to report to. An ordinary finalize- // does neither, so it does not carry one. Nothing is provisioned here β- // every settle on this branch goes through firebill, which provisions- // what it needs (see resolveBillingRoute).- //- // A caller that cannot name the org passes no team and the settle still- // lands, without the label. There is no durable retry on this path β- // `billMonitorCheck`'s only caller catches, writes- // `billing_status: "failed"`, and moves on, and nothing ever reads that- // back β so refusing would abandon the finalize entirely: the hold- // expires and the run goes unbilled at Autumn as well as unreported.- // Settling and losing the label is the lesser loss, and firebill counts- // the lost label as `partner_events_total{outcome="no_customer"}`.- const customerId = externalRequestId && team ? team.orgId : null;- billingRouteTotal.labels("firebill", "finalize").inc();- // Surfaced, not discarded. firebill answers `false` for a refusal, a- // timeout, or a non-OK β none of which throw β so a caller that ignores- // this records a run as billed that nobody billed.- return await firebillFinalize({- lockId,- action,- overrideValue,- properties,- externalRequestId,- customerId,- featureId: customerId ? featureId : null,- heldValue: customerId ? heldValue : null,- });- }-- billingRouteTotal.labels("direct", "finalize").inc();-- // No client means no hold was ever taken, so nothing can have gone+ // Without firebill no hold was ever taken, so nothing can have gone// unsettled.- if (!autumnClient) return true;-- try {- await autumnClient.balances.finalize({- lockId,- action,- overrideValue,- properties,- });- logger.info("Autumn finalizeCreditsLock succeeded", {- lockId,- action,- overrideValue,- });- return true;- } catch (error) {- logger.error(- "Autumn finalizeCreditsLock failed β billing API may be unavailable",- {- lockId,- action,- overrideValue,- error,- },- );- return false;- }+ if (!firebillConfigured()) return true;++ // Named only for a gated settle: firebill needs the org to split the+ // settle and to find the integration to report to. An ordinary finalize+ // does neither, so it does not carry one.+ //+ // A gated caller that cannot name the org passes no team and the settle+ // still lands, without the label. There is no durable retry on this path β+ // `billMonitorCheck`'s only caller catches, writes+ // `billing_status: "failed"`, and moves on, and nothing ever reads that+ // back β so refusing would abandon the finalize entirely: the hold+ // expires and the run goes unbilled at Autumn as well as unreported.+ // Settling and losing the label is the lesser loss, and firebill counts+ // the lost label as `partner_events_total{outcome="no_customer"}`.+ const customerId = externalRequestId && team ? team.orgId : null;+ // Surfaced, not discarded. firebill answers `false` for a refusal, a+ // timeout, or a non-OK β none of which throw β so a caller that ignores+ // this records a run as billed that nobody billed.+ return await firebillFinalize({+ lockId,+ action,+ overrideValue,+ properties,+ externalRequestId,+ customerId,+ featureId: customerId ? featureId : null,+ heldValue: customerId ? heldValue : null,+ });}/**- * Records a credit usage event directly in Autumn. Returns true on success.+ * Records a credit usage event through firebill. Returns true on success.+ *+ * No fallback to Autumn on failure β firebill may already own the event.*/async trackCredits({teamId,@@ -885,27 +633,20 @@ export class AutumnService {durableRetry,orgId,}: TrackCreditsParams): Promise<boolean> {- if (!autumnClient) return false;+ if (!firebillConfigured()) return false;if (this.isPreviewTeam(teamId)) return false;try {- const { customerId, routed } = await this.resolveBillingRoute(- teamId,- orgId,- );- return await this.track(- {- customerId,- entityId: billingEntityId(featureId, teamId),- featureId,- value,- properties,- idempotencyKey,- externalRequestId,- durableRetry,- },- routed,- );+ return await firebillTrack({+ customerId: orgId,+ entityId: billingEntityId(featureId, teamId),+ featureId,+ value,+ properties,+ idempotencyKey,+ externalRequestId,+ durableRetry,+ });} catch (error) {logger.error("Autumn trackCredits failed β billing API may be unavailable",@@ -981,27 +722,20 @@ export class AutumnService {durableRetry,orgId,}: TrackCreditsParams): Promise<boolean> {- if (!autumnClient) return false;+ if (!firebillConfigured()) return false;if (this.isPreviewTeam(teamId)) return false;try {- const { customerId, routed } = await this.resolveBillingRoute(- teamId,- orgId,- );- return await this.track(- {- customerId,- entityId: billingEntityId(featureId, teamId),- featureId,- value: -value,- properties: { ...properties, source: "autumn_refund" },- idempotencyKey,- externalRequestId,- durableRetry,- },- routed,- );+ return await firebillTrack({+ customerId: orgId,+ entityId: billingEntityId(featureId, teamId),+ featureId,+ value: -value,+ properties: { ...properties, source: "autumn_refund" },+ idempotencyKey,+ externalRequestId,+ durableRetry,+ });} catch (error) {logger.error("Autumn refundCredits failed β billing API may be unavailable",diff --git a/apps/api/src/services/autumn/firebill.ts b/apps/api/src/services/autumn/firebill.tsindex 0c5d4fb96..1a9ecf043 100644--- a/apps/api/src/services/autumn/firebill.ts+++ b/apps/api/src/services/autumn/firebill.ts@@ -44,7 +44,7 @@ function lockDeniedReason(value: unknown): LockDeniedReason | undefined {// firebill's own internal budget (durable write + forward attempt) is up to// ~3.5s worst case, so this is deliberately looser than the 2s timeout on the-// direct Autumn client.+// Autumn client.const FIREBILL_TIMEOUT_MS = 5000;// A gated lock legitimately does more: firebill asks the partner (2.5s ceiling)@@ -276,8 +276,8 @@ const lockAnswerSchema = z.discriminatedUnion("success", []);/**- * Whether firebill is configured. When it is, every org's usage goes through- * firebill rather than straight to Autumn.+ * Whether firebill is configured. Every billing write goes through firebill;+ * without it, billing is skipped.*/export function firebillConfigured(): boolean {return Boolean(config.FIREBILL_URL && config.FIREBILL_SECRET);@@ -298,8 +298,7 @@ function firebillUrl(path: string): string {* firebill's /v1/refund endpoint expects the POSITIVE amount and negates it* itself, so the absolute value is sent either way.*- * Returns true when firebill accepted the event, mirroring the boolean- * contract of the direct Autumn track path. A `false` means firebill did not+ * Returns true when firebill accepted the event. A `false` means firebill did not* confirm it. An unanswered event is then handed to a durable retry that* re-sends the identical event under the same idempotency key, so it is billed* once whether or not the lost attempt landed; an explicit refusal is not.@@ -538,8 +537,7 @@ async function firebillAttempt("content-type": "application/json",},// No overage flag needed: firebill itself pins Autumn's- // overage_behavior to "overflow" on every upstream call, matching the- // direct-Autumn path below.+ // overage_behavior to "overflow" on every upstream call.body: JSON.stringify({customer_id: customerId,// Required by firebill; empty for a customer-level charge.@@ -972,13 +970,12 @@ export async function firebillLock({* Settles a lock via firebill's `/v1/finalize`: `confirm` bills the hold,* `release` gives the balance back. firebill queues the settle durably and* retries it until it lands in Autumn, instead of dropping it when Autumn- * blinks (the direct-Autumn path's failure mode β the lock then just expires,- * which for a confirm means the work goes unbilled).+ * blinks (a dropped settle just lets the lock expire, which for a confirm+ * means the work goes unbilled).*- * Returns true when firebill accepted the settle for delivery, mirroring the- * void-and-log contract of the direct finalize path. No direct-Autumn- * fallback, for the same reason as track: firebill may have durably queued- * the settle before the call failed.+ * Returns true when firebill accepted the settle for delivery. No+ * direct-Autumn fallback, for the same reason as track: firebill may have+ * durably queued the settle before the call failed.*/export async function firebillFinalize({lockId,diff --git a/apps/api/src/services/autumn/metrics.ts b/apps/api/src/services/autumn/metrics.tsindex 678fcda66..9e9cde7dc 100644--- a/apps/api/src/services/autumn/metrics.ts+++ b/apps/api/src/services/autumn/metrics.ts@@ -1,20 +1,5 @@import { Counter } from "prom-client";-/**- * Which path a usage event or a lock settle took. The ratio of `firebill` to- * `direct` is the live read on the rollout percentage β during a ramp it is the- * first thing to look at, because a routing bug shows up here long before it- * shows up in a balance. `route="direct"` staying at 0 is the signal that no- * usage event or settle goes to Autumn directly any more; direct lock holds- * are not counted here.- */-export const billingRouteTotal = new Counter({- name: "firecrawl_billing_route_total",- help: "Usage events and lock settles by the path they took to Autumn",- // route: firebill | direct operation: track | finalize- labelNames: ["route", "operation"] as const,-});-/*** What firebill said. `refused` is an explicit `success: false` β firebill does* not own the event and nobody else does, so usage is being dropped.diff --git a/apps/api/src/services/autumn/types.ts b/apps/api/src/services/autumn/types.tsindex a2dcc585c..0e75ed8dd 100644--- a/apps/api/src/services/autumn/types.ts+++ b/apps/api/src/services/autumn/types.ts@@ -1,12 +1,3 @@-export class ExistingCreditsLockError extends Error {- constructor(readonly lockId: string) {- super(- "Credit reservation already exists; execution ownership is unresolved.",- );- this.name = "ExistingCreditsLockError";- }-}-export type GetOrCreateCustomerParams = {customerId: string;name?: string | null;@@ -36,23 +27,21 @@ export type TrackParams = {properties?: Record<string, unknown>;/*** The partner's own operation id (the caller's `External-Request-Id`).- * firebill route only: sent as `external_request_id`, so firebill reports- * the operation under it without a lookup. Opaque, forwarded verbatim.+ * Sent to firebill as `external_request_id`, so firebill reports the+ * operation under it without a lookup. Opaque, forwarded verbatim.*/externalRequestId?: string | null;/**- * Stable per-charge identity, honored on the firebill route only (the- * direct Autumn SDK does not expose its Idempotency-Key header). When set,- * a caller retry β or a requeued job re-billing the same work β dedupes+ * Stable per-charge identity, sent to firebill as the idempotency key. When+ * set, a caller retry β or a requeued job re-billing the same work β dedupes* instead of double-billing. Must be unique per CHARGE, never a shared id* like a crawl id (every page shares it: collision = underbilling).*/idempotencyKey?: string;/**- * firebill route only. `false` keeps an unanswered event out of the durable- * retry, for a caller that treats `false` as "did not happen" and releases- * something on it: a late retry landing would bill what it already undid.- * Defaults to retrying.+ * `false` keeps an unanswered event out of the durable retry, for a caller+ * that treats `false` as "did not happen" and releases something on it: a+ * late retry landing would bill what it already undid. Defaults to retrying.*/durableRetry?: boolean;};diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.tsindex 4c159e082..a91976a33 100644--- a/apps/api/src/services/billing/credit_billing.ts+++ b/apps/api/src/services/billing/credit_billing.ts@@ -46,7 +46,7 @@ export async function billTeam(let trackedInRequest = false;if (org_id !== null) {- // Stable per-charge key (firebill route only): a caller retry or re-run+ // Stable per-charge key: a caller retry or re-run// job with the same chargeId dedupes instead of double-billing.trackedInRequest = await autumnService.trackCredits({teamId: team_id,diff --git a/apps/api/src/services/billing/types.ts b/apps/api/src/services/billing/types.tsindex bad44dc3b..3508686db 100644--- a/apps/api/src/services/billing/types.ts+++ b/apps/api/src/services/billing/types.ts@@ -21,7 +21,7 @@ export type BillingMetadata = {* Unique-per-CHARGE identity, set by the call site that knows what "one* charge" is. billTeam derives the firebill idempotency key from it* (`fc:track:{endpoint}:{chargeId}`), so a retried call β or a re-run job β- * dedupes instead of double-billing on the firebill route.+ * dedupes instead of double-billing.** Rules: it must never be shared by two charges that should BOTH bill* (collision = silent underbilling). A jobId shared with another charge ondiff --git a/apps/api/src/services/monitoring/runner.reservation.test.ts b/apps/api/src/services/monitoring/runner.reservation.test.tsindex 1d85f1861..80cde1568 100644--- a/apps/api/src/services/monitoring/runner.reservation.test.ts+++ b/apps/api/src/services/monitoring/runner.reservation.test.ts@@ -1,7 +1,6 @@-import { AutumnDefaultError } from "autumn-js";import type { MonitorCheckRow, MonitorRow } from "./types";-const { sdk, channel, logger, bill } = vi.hoisted(() => {+const { sdk, firebill, channel, logger, bill } = vi.hoisted(() => {const logger = {info: vi.fn(),warn: vi.fn(),@@ -12,6 +11,12 @@ const { sdk, channel, logger, bill } = vi.hoisted(() => {return {logger,bill: vi.fn(),+ firebill: {+ lock: vi.fn(),+ finalize: vi.fn(),+ track: vi.fn(),+ check: vi.fn(),+ },sdk: {check: vi.fn(),track: vi.fn(),@@ -38,12 +43,15 @@ vi.mock("../../config", () => ({vi.mock("../../lib/logger", () => ({ logger }));vi.mock("../autumn/client", () => ({ autumnClient: sdk }));vi.mock("../autumn/firebill", () => ({- firebillConfigured: () => false,+ firebillConfigured: () => true,+ firebillLock: firebill.lock,+ firebillFinalize: firebill.finalize,+ firebillTrack: firebill.track,+ firebillCheck: firebill.check,}));vi.mock("../autumn/metrics", () => ({autumnCustomerGetOrCreateTotal: { inc: vi.fn() },autumnEntityCreatedInlineTotal: { inc: vi.fn() },- billingRouteTotal: { labels: () => ({ inc: vi.fn() }) },}));vi.mock("../../db/connection", () => ({dbRr: {@@ -109,7 +117,6 @@ import {processMonitorCheckJob,reconcileRunningMonitorChecks,} from "./runner";-import { consumeMonitorCheckJobs } from "./queue";import * as store from "./store";import { addScrapeJob } from "../queue-jobs";import { getACUCTeam } from "../../controllers/auth";@@ -117,7 +124,6 @@ import { redisEvictConnection } from "../redis";import { trackMonitorCheckStartedInterest } from "./interest";import { runSearchTarget } from "./search/run";import { config } from "../../config";-import { autumnService } from "../autumn/autumn.service";function deferred<T = void>() {let resolve!: (value: T) => void;@@ -129,16 +135,7 @@ function deferred<T = void>() {return { promise, resolve, reject };}-function conflict() {- const body = JSON.stringify({ code: "lock_already_exists" });- return new AutumnDefaultError("Reservation failed", {- request: new Request("https://example.com/check"),- response: new Response(body, { status: 409 }),- body,- });-}--describe("direct monitor reservation ownership", () => {+describe("monitor reservation ownership", () => {let current: MonitorCheckRow;let monitor: MonitorRow;let recordedPages: Array<{@@ -156,8 +153,8 @@ describe("direct monitor reservation ownership", () => {addScrapeJob,runSearchTarget,trackMonitorCheckStartedInterest,- sdk.balances.finalize,- sdk.track,+ firebill.finalize,+ firebill.track,bill,];const snapshot = () => ({@@ -190,10 +187,11 @@ describe("direct monitor reservation ownership", () => {],} as MonitorRow;vi.mocked(getACUCTeam).mockResolvedValue({ org_id: "org-1" } as any);- sdk.check.mockResolvedValue({ allowed: true });- sdk.balances.finalize.mockResolvedValue(undefined);- sdk.customers.getOrCreate.mockResolvedValue({ id: "org-1" });- sdk.entities.get.mockResolvedValue({ id: "team-1" });+ firebill.lock.mockImplementation(async ({ lockId }) => ({+ status: "locked",+ lockId,+ }));+ firebill.finalize.mockResolvedValue(true);vi.mocked(trackMonitorCheckStartedInterest).mockResolvedValue(undefined);vi.mocked(store.getMonitorForUpdate).mockImplementation(async () =>structuredClone(monitor),@@ -262,14 +260,14 @@ describe("direct monitor reservation ownership", () => {removed_count: Number(status === "removed"),error_count: Number(status === "error"),});- expect(sdk.balances.finalize).toHaveBeenCalledExactlyOnceWith(+ expect(firebill.finalize).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({lockId: "monitor_check-1",action: "confirm",overrideValue: credits,}),);- expect(sdk.track).not.toHaveBeenCalled();+ expect(firebill.track).not.toHaveBeenCalled();if (credits > 0) {expect(bill).toHaveBeenCalledExactlyOnceWith("bill_team",@@ -279,120 +277,6 @@ describe("direct monitor reservation ownership", () => {} else expect(bill).not.toHaveBeenCalled();}- it.each([false, true])(- "leaves a live winner untouched with stored hold=%s",- async stored => {- const paused = deferred();- const resume = deferred();- if (stored) {- vi.mocked(store.updateMonitorCheckIfRunning)- .mockImplementationOnce(async (_id, patch) => {- Object.assign(current, patch);- return structuredClone(current);- })- .mockImplementationOnce(async (_id, patch) => {- paused.resolve();- await resume.promise;- Object.assign(current, patch);- return structuredClone(current);- });- } else {- sdk.check.mockImplementationOnce(async () => {- paused.resolve();- await resume.promise;- return { allowed: true };- });- }- if (stored) sdk.check.mockResolvedValueOnce({ allowed: true });- sdk.check.mockRejectedValueOnce(conflict());- const winner = processMonitorCheckJob(job);- await paused.promise;- const before = snapshot();- try {- await expect(processMonitorCheckJob(job)).rejects.toMatchObject({- name: "ExistingCreditsLockError",- lockId: "monitor_check-1",- });- expect(snapshot()).toEqual(before);- } finally {- resume.resolve();- await winner;- }- expect(addScrapeJob).toHaveBeenCalledTimes(1);- await settleWinner();- },- );-- it("does not let the queued claimant overwrite a later reservation winner", async () => {- const reached = deferred();- const pending = deferred<{ allowed: boolean }>();- sdk.check- .mockImplementationOnce(() => {- reached.resolve();- return pending.promise;- })- .mockResolvedValueOnce({ allowed: true });- const claimant = processMonitorCheckJob(job);- const rejected = expect(claimant).rejects.toMatchObject({- name: "ExistingCreditsLockError",- });- await reached.promise;- await processMonitorCheckJob(job);- const before = snapshot();- pending.reject(conflict());- await rejected;- expect(snapshot()).toEqual(before);- expect(addScrapeJob).toHaveBeenCalledTimes(1);- await settleWinner();- });-- it("leaves terminal billing untouched when a contender's conflict arrives late", async () => {- await processMonitorCheckJob(job);- const reached = deferred();- const pending = deferred<{ allowed: boolean }>();- sdk.check.mockImplementationOnce(() => {- reached.resolve();- return pending.promise;- });- const contender = processMonitorCheckJob(job);- const rejected = expect(contender).rejects.toMatchObject({- name: "ExistingCreditsLockError",- });- await reached.promise;- await settleWinner();- const before = snapshot();- pending.reject(conflict());- await rejected;- expect(snapshot()).toEqual(before);- });-- it("reconciles a completed persisted search target without redelivery work", async () => {- current.status = "running";- current.started_at = new Date().toISOString();- current.autumn_lock_id = "monitor_check-1";- current.billing_status = "reserved";- monitor.targets = [- { id: "target-1", type: "search" },- ] as MonitorRow["targets"];- current.target_results = [- {- targetId: "target-1",- type: "search",- searchCompleted: true,- searchCredits: 1,- },- ];- sdk.check.mockRejectedValueOnce(conflict());- const before = snapshot();- await expect(processMonitorCheckJob(job)).rejects.toMatchObject({- name: "ExistingCreditsLockError",- });- expect(snapshot()).toEqual(before);- await settleWinner();- expect(runSearchTarget).not.toHaveBeenCalled();- expect(addScrapeJob).not.toHaveBeenCalled();- });-it("confirms a successful hold with zero actual credits without a ledger debit", async () => {await processMonitorCheckJob(job);await settleWinner(0);@@ -411,71 +295,8 @@ describe("direct monitor reservation ownership", () => {billing_status: "not_applicable",autumn_lock_id: null,});- expect(sdk.check).not.toHaveBeenCalled();- expect(sdk.balances.finalize).not.toHaveBeenCalled();- expect(bill).not.toHaveBeenCalled();- });-- it("dead-letters recovery of an unlinked orphan hold and leaves expiry to the provider", async () => {- current.status = "running";- current.started_at = new Date().toISOString();- const held = new Set<string>();- sdk.check.mockImplementation(async ({ lock }) => {- if (held.has(lock.lockId)) throw conflict();- held.add(lock.lockId);- return { allowed: true };- });- await expect(- autumnService.lockCredits({- teamId: job.teamId,- orgId: "org-1",- value: 1,- lockId: "monitor_check-1",- }),- ).resolves.toEqual({ status: "locked", lockId: "monitor_check-1" });- expect(held.has("monitor_check-1")).toBe(true);- expect(current.autumn_lock_id).toBeNull();- await consumeMonitorCheckJobs(processMonitorCheckJob);- const message = { content: Buffer.from(JSON.stringify(job)) };- const before = snapshot();- await channel.consume.mock.calls[0][1](message);- expect(snapshot()).toEqual(before);- expect(channel.ack).not.toHaveBeenCalled();- expect(channel.nack).toHaveBeenCalledExactlyOnceWith(message, false, false);- expect(logger.error).toHaveBeenCalledWith(- "Monitor check job failed",- expect.objectContaining({- error: expect.objectContaining({ name: "ExistingCreditsLockError" }),- }),- );- expect(channel.assertExchange).toHaveBeenCalledWith(- "monitor.checks.dlx",- "direct",- { durable: true },- );- expect(channel.assertQueue).toHaveBeenCalledWith(- "monitor.checks",- expect.objectContaining({- arguments: expect.objectContaining({- "x-dead-letter-exchange": "monitor.checks.dlx",- "x-dead-letter-routing-key": "monitor.checks",- }),- }),- );- expect(channel.bindQueue).toHaveBeenCalledWith(- "monitor.checks.dlq",- "monitor.checks.dlx",- "monitor.checks",- );- current.started_at = new Date(- Date.now() - 2 * 60 * 60 * 1000,- ).toISOString();- await reconcileRunningMonitorChecks();- expect(current).toMatchObject({- status: "failed",- billing_status: "not_applicable",- });- expect(sdk.balances.finalize).not.toHaveBeenCalled();+ expect(firebill.lock).not.toHaveBeenCalled();+ expect(firebill.finalize).not.toHaveBeenCalled();expect(bill).not.toHaveBeenCalled();});@@ -488,12 +309,51 @@ describe("direct monitor reservation ownership", () => {];await reconcileRunningMonitorChecks();expect(current.status).toBe("running");- expect(sdk.balances.finalize).not.toHaveBeenCalled();+ expect(firebill.finalize).not.toHaveBeenCalled();expect(bill).not.toHaveBeenCalled();await settleWinner(status === "error" ? 0 : 1, status);},);+ it("skips a check without running it when firebill denies the hold", async () => {+ firebill.lock.mockResolvedValue({ status: "denied" });+ await processMonitorCheckJob(job);+ expect(current).toMatchObject({+ status: "skipped_no_credits",+ billing_status: "not_applicable",+ autumn_lock_id: null,+ });+ expect(addScrapeJob).not.toHaveBeenCalled();+ expect(firebill.finalize).not.toHaveBeenCalled();+ expect(bill).not.toHaveBeenCalled();+ });++ it("runs a check unlocked when firebill cannot be reached for the hold", async () => {+ firebill.lock.mockResolvedValue({ status: "unavailable" });+ await processMonitorCheckJob(job);+ expect(current).toMatchObject({ status: "running", autumn_lock_id: null });+ expect(addScrapeJob).toHaveBeenCalledTimes(1);+ expect(firebill.finalize).not.toHaveBeenCalled();+ });++ it("releases a stale check's linked hold through firebill", async () => {+ current.status = "running";+ current.started_at = new Date(+ Date.now() - 2 * 60 * 60 * 1000,+ ).toISOString();+ current.autumn_lock_id = "monitor_check-1";+ current.billing_status = "reserved";+ await reconcileRunningMonitorChecks();+ expect(current).toMatchObject({+ status: "failed",+ billing_status: "released",+ });+ expect(firebill.finalize).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ lockId: "monitor_check-1", action: "release" }),+ );+ expect(bill).not.toHaveBeenCalled();+ });+it("does not reserve a running delivery that became terminal after its initial read", async () => {current.status = "running";const initial = structuredClone(current);@@ -503,7 +363,7 @@ describe("direct monitor reservation ownership", () => {const before = snapshot();await processMonitorCheckJob(job);expect(snapshot()).toEqual(before);- expect(sdk.check).not.toHaveBeenCalled();+ expect(firebill.lock).not.toHaveBeenCalled();});it("does not fail or release a winner when the admission reread is unavailable", async () => {@@ -518,7 +378,7 @@ describe("direct monitor reservation ownership", () => {const before = snapshot();await expect(processMonitorCheckJob(job)).rejects.toThrow(failure);expect(snapshot()).toEqual(before);- expect(sdk.check).not.toHaveBeenCalled();+ expect(firebill.lock).not.toHaveBeenCalled();});it.each(["queued", "running"] as const)(@@ -539,7 +399,7 @@ describe("direct monitor reservation ownership", () => {pending.resolve({ org_id: "org-1" });await delivery;expect(snapshot()).toEqual(before);- expect(sdk.check).not.toHaveBeenCalled();+ expect(firebill.lock).not.toHaveBeenCalled();},);@@ -548,8 +408,8 @@ describe("direct monitor reservation ownership", () => {current.autumn_lock_id = "monitor_check-1";current.billing_status = "reserved";const reached = deferred();- const pending = deferred<{ allowed: boolean }>();- sdk.check.mockImplementationOnce(() => {+ const pending = deferred<{ status: "locked"; lockId: string }>();+ firebill.lock.mockImplementationOnce(() => {reached.resolve();return pending.promise;});@@ -558,41 +418,16 @@ describe("direct monitor reservation ownership", () => {current.status = "completed";current.billing_status = "confirmed";const terminal = structuredClone(current);- pending.resolve({ allowed: true });+ pending.resolve({ status: "locked", lockId: "monitor_check-1" });await delivery;expect(current).toEqual(terminal);expect(store.markMonitorRunning).not.toHaveBeenCalled();expect(trackMonitorCheckStartedInterest).not.toHaveBeenCalled();- expect(sdk.balances.finalize).not.toHaveBeenCalled();+ expect(firebill.finalize).not.toHaveBeenCalled();expect(addScrapeJob).not.toHaveBeenCalled();expect(bill).not.toHaveBeenCalled();});- it("rejects linked orphan recovery and only stale reconciliation releases its hold", async () => {- current.status = "running";- current.started_at = new Date().toISOString();- current.autumn_lock_id = "monitor_check-1";- current.billing_status = "reserved";- sdk.check.mockRejectedValueOnce(conflict());- const before = snapshot();- await expect(processMonitorCheckJob(job)).rejects.toMatchObject({- name: "ExistingCreditsLockError",- });- expect(snapshot()).toEqual(before);- current.started_at = new Date(- Date.now() - 2 * 60 * 60 * 1000,- ).toISOString();- await reconcileRunningMonitorChecks();- expect(current).toMatchObject({- status: "failed",- billing_status: "released",- });- expect(sdk.balances.finalize).toHaveBeenCalledExactlyOnceWith(- expect.objectContaining({ lockId: "monitor_check-1", action: "release" }),- );- expect(bill).not.toHaveBeenCalled();- });-it("releases a newly acquired unpersisted hold if later admission fails", async () => {vi.mocked(store.updateMonitorCheckIfRunning).mockRejectedValueOnce(new Error("Admission failed"),@@ -605,7 +440,7 @@ describe("direct monitor reservation ownership", () => {billing_status: "released",autumn_lock_id: null,});- expect(sdk.balances.finalize).toHaveBeenCalledExactlyOnceWith(+ expect(firebill.finalize).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ action: "release", lockId: "monitor_check-1" }),);expect(addScrapeJob).not.toHaveBeenCalled();@@ -624,7 +459,7 @@ describe("direct monitor reservation ownership", () => {billing_status: "released",autumn_lock_id: "monitor_check-1",});- expect(sdk.balances.finalize).toHaveBeenCalledExactlyOnceWith(+ expect(firebill.finalize).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ action: "release", lockId: "monitor_check-1" }),);expect(addScrapeJob).not.toHaveBeenCalled();diff --git a/apps/api/src/services/monitoring/runner.ts b/apps/api/src/services/monitoring/runner.tsindex c79361f32..9f18e9e4a 100644--- a/apps/api/src/services/monitoring/runner.ts+++ b/apps/api/src/services/monitoring/runner.ts@@ -14,7 +14,6 @@ import { ScrapeJobData } from "../../types";import { includesFormat } from "../../lib/format-utils";import { normalizeMonitorFormats } from "./diff";import { autumnService } from "../autumn/autumn.service";-import { ExistingCreditsLockError } from "../autumn/types";import { getBillingQueue } from "../queue-service";import {crawlToCrawler,@@ -1249,7 +1248,6 @@ export async function processMonitorCheckJob();}} catch (error) {- if (error instanceof ExistingCreditsLockError) throw error;// Atomically flip running -> failed. Returns null when the check already// reached a terminal status β i.e. the reconciler finalized it (completed,// billed, lock confirmed) before this late catch ran. In that case we must