@absolutejs/billing 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,18 @@ This file is generated by `absolute-changelog` from the entries in
6
6
  `changelog/`. Edit an entry, not this file — and add new ones under
7
7
  `changelog/unreleased/`.
8
8
 
9
+ ## 0.10.0 — 2026-09-13
10
+
11
+ ### Added
12
+
13
+ - **Transfer reserved credit work to immutable deferred effect authorizations and settle only bound terminal outcomes** (`CreditWork`, `DeferredCreditBinding`, `createPostgresCreditWork`)
14
+
15
+ ## 0.9.1 — 2026-09-11
16
+
17
+ ### Fixed
18
+
19
+ - **Bind serialized ledger, work and checkout JSON as text before casting, preventing PostgreSQL drivers from double-encoding state**
20
+
9
21
  ## 0.9.0 — 2026-09-11
10
22
 
11
23
  ### Added
package/changelog.json CHANGED
@@ -3,24 +3,49 @@
3
3
  "name": "@absolutejs/billing",
4
4
  "releases": [
5
5
  {
6
- "version": "0.9.0",
6
+ "changes": [
7
+ {
8
+ "kind": "added",
9
+ "summary": "Transfer reserved credit work to immutable deferred effect authorizations and settle only bound terminal outcomes",
10
+ "symbols": [
11
+ "CreditWork",
12
+ "DeferredCreditBinding",
13
+ "createPostgresCreditWork"
14
+ ]
15
+ }
16
+ ],
17
+ "date": "2026-09-13",
18
+ "version": "0.10.0"
19
+ },
20
+ {
21
+ "changes": [
22
+ {
23
+ "kind": "fixed",
24
+ "summary": "Bind serialized ledger, work and checkout JSON as text before casting, preventing PostgreSQL drivers from double-encoding state"
25
+ }
26
+ ],
7
27
  "date": "2026-09-11",
28
+ "version": "0.9.1"
29
+ },
30
+ {
8
31
  "changes": [
9
32
  {
10
33
  "kind": "added",
11
34
  "summary": "Add bounded customer billing reports, receipt cursors and reconciled usage projections"
12
35
  }
13
- ]
36
+ ],
37
+ "date": "2026-09-11",
38
+ "version": "0.9.0"
14
39
  },
15
40
  {
16
- "version": "0.8.0",
17
- "date": "2026-09-11",
18
41
  "changes": [
19
42
  {
20
43
  "kind": "added",
21
44
  "summary": "Add single-use purchase-only checkout handoffs with hashed capabilities, account binding and CSRF verification"
22
45
  }
23
- ]
46
+ ],
47
+ "date": "2026-09-11",
48
+ "version": "0.8.0"
24
49
  },
25
50
  {
26
51
  "changes": [
@@ -58,7 +58,7 @@ var createPostgresCheckoutHandoffs = (db, now = Date.now) => ({
58
58
  const code = secret();
59
59
  const id = randomUUID();
60
60
  const expiresAt = now() + lifetimeMs;
61
- await db.query(`INSERT INTO billing_checkout.handoffs(id,account_id,quote,code_hash,expires_at) VALUES ($1,$2,$3::jsonb,$4,$5)`, [id, accountId, JSON.stringify(quote), hash(code), expiresAt]);
61
+ await db.query(`INSERT INTO billing_checkout.handoffs(id,account_id,quote,code_hash,expires_at) VALUES ($1,$2,$3::text::jsonb,$4,$5)`, [id, accountId, JSON.stringify(quote), hash(code), expiresAt]);
62
62
  return { id, code, expiresAt };
63
63
  },
64
64
  async exchange(code, browserAccountId = null) {
@@ -104,5 +104,5 @@ export {
104
104
  createPostgresCheckoutHandoffs
105
105
  };
106
106
 
107
- //# debugId=F96D1D9F1E1CA67A64756E2164756E21
107
+ //# debugId=13C01DD2B5D2689364756E2164756E21
108
108
  //# sourceMappingURL=checkoutHandoff.js.map
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/checkoutHandoff.ts"],
4
4
  "sourcesContent": [
5
- "import { createHash, randomBytes, randomUUID } from \"node:crypto\";\nimport type { CreditSqlClient } from \"./prepaidPostgres\";\n\nexport type CheckoutQuote = {\n productId: string;\n amountCents: number;\n currency: string;\n credits: number;\n};\nexport type CheckoutHandoff = {\n id: string;\n accountId: string;\n quote: CheckoutQuote;\n expiresAt: number;\n};\nconst lifetimeMs = 15 * 60 * 1000;\nconst hash = (secret: string) =>\n createHash(\"sha256\").update(secret).digest(\"hex\");\nconst secret = () => randomBytes(32).toString(\"base64url\");\nconst validSecret = (value: string) => /^[A-Za-z0-9_-]{43}$/.test(value);\nconst validQuote = (value: CheckoutQuote) =>\n typeof value.productId === \"string\" &&\n value.productId.length > 0 &&\n value.productId.length <= 128 &&\n Number.isSafeInteger(value.amountCents) &&\n value.amountCents > 0 &&\n /^[A-Z]{3}$/.test(value.currency) &&\n Number.isSafeInteger(value.credits) &&\n value.credits > 0;\n\n/** Apply explicitly during deployment. Secrets are stored only as SHA-256 hashes. */\nexport const checkoutHandoffPostgresSchemaSql = () => `\nCREATE SCHEMA IF NOT EXISTS billing_checkout;\nCREATE TABLE IF NOT EXISTS billing_checkout.handoffs (\n id text PRIMARY KEY, account_id text NOT NULL, quote jsonb NOT NULL,\n code_hash text NOT NULL UNIQUE, expires_at bigint NOT NULL,\n session_hash text UNIQUE, csrf_hash text, session_expires_at bigint\n);\nCREATE INDEX IF NOT EXISTS handoffs_account ON billing_checkout.handoffs(account_id, id);\n`;\nconst decode = (\n row: Record<string, unknown> | undefined,\n): CheckoutHandoff | null => {\n if (!row) return null;\n const quote = row.quote;\n if (\n !quote ||\n typeof quote !== \"object\" ||\n !(\"productId\" in quote) ||\n !(\"amountCents\" in quote) ||\n !(\"currency\" in quote) ||\n !(\"credits\" in quote) ||\n typeof quote.productId !== \"string\" ||\n typeof quote.amountCents !== \"number\" ||\n typeof quote.currency !== \"string\" ||\n typeof quote.credits !== \"number\" ||\n typeof row.id !== \"string\" ||\n typeof row.account_id !== \"string\"\n )\n throw new Error(\"Invalid checkout record\");\n const parsed = {\n productId: quote.productId,\n amountCents: quote.amountCents,\n currency: quote.currency,\n credits: quote.credits,\n };\n if (!validQuote(parsed)) throw new Error(\"Invalid checkout quote\");\n return {\n id: row.id,\n accountId: row.account_id,\n quote: parsed,\n expiresAt: Number(row.expires_at),\n };\n};\n/** This is a purchase-only capability, never a login or a saved-card authorization.\n * The application binds the authenticated account and prices the quote. */\nexport const createPostgresCheckoutHandoffs = (\n db: CreditSqlClient,\n now = Date.now,\n) => ({\n async issue(accountId: string, quote: CheckoutQuote) {\n if (!accountId || !validQuote(quote))\n throw new Error(\"Invalid checkout request\");\n const code = secret();\n const id = randomUUID();\n const expiresAt = now() + lifetimeMs;\n await db.query(\n `INSERT INTO billing_checkout.handoffs(id,account_id,quote,code_hash,expires_at) VALUES ($1,$2,$3::jsonb,$4,$5)`,\n [id, accountId, JSON.stringify(quote), hash(code), expiresAt],\n );\n return { id, code, expiresAt };\n },\n /** Invoke only on a deliberate same-origin POST, never a preview GET.\n * Atomic exchange makes concurrent redemption and link replay fail closed. */\n async exchange(code: string, browserAccountId: string | null = null) {\n if (!validSecret(code)) return null;\n const session = secret();\n const csrf = secret();\n const expiresAt = now() + lifetimeMs;\n const { rows } = await db.query(\n `UPDATE billing_checkout.handoffs SET session_hash=$2,csrf_hash=$3,session_expires_at=$4 WHERE code_hash=$1 AND session_hash IS NULL AND expires_at>$5 AND ($6::text IS NULL OR account_id=$6) RETURNING *`,\n [\n hash(code),\n hash(session),\n hash(csrf),\n expiresAt,\n now(),\n browserAccountId,\n ],\n );\n const handoff = decode(rows[0]);\n return handoff ? { handoff, session, csrf, expiresAt } : null;\n },\n async authorize(\n session: string,\n csrf: string,\n browserAccountId: string | null = null,\n ) {\n if (!validSecret(session) || !validSecret(csrf)) return null;\n const { rows } = await db.query(\n `SELECT * FROM billing_checkout.handoffs WHERE session_hash=$1 AND csrf_hash=$2 AND session_expires_at>$3 AND ($4::text IS NULL OR account_id=$4)`,\n [hash(session), hash(csrf), now(), browserAccountId],\n );\n return decode(rows[0]);\n },\n /** Public IDs are not capabilities: status always requires account binding. */\n async get(accountId: string, id: string) {\n const { rows } = await db.query(\n `SELECT * FROM billing_checkout.handoffs WHERE id=$1 AND account_id=$2`,\n [id, accountId],\n );\n return decode(rows[0]);\n },\n});\n\n/** Put the code in a fragment so previews, servers and referrers don't receive it.\n * The landing page must remove it before loading any third-party code. */\nexport const checkoutHandoffUrl = (landingUrl: string, code: string) => {\n const url = new URL(landingUrl);\n if (\n url.protocol !== \"https:\" ||\n url.username ||\n url.password ||\n url.search ||\n url.hash ||\n !validSecret(code)\n )\n throw new Error(\"Invalid checkout landing URL\");\n url.hash = code;\n return url.href;\n};\nexport const checkoutSameOrigin = (request: Request, origin: string) =>\n request.method === \"POST\" &&\n request.headers.get(\"origin\") === new URL(origin).origin &&\n [null, \"same-origin\"].includes(request.headers.get(\"sec-fetch-site\"));\n"
5
+ "import { createHash, randomBytes, randomUUID } from \"node:crypto\";\nimport type { CreditSqlClient } from \"./prepaidPostgres\";\n\nexport type CheckoutQuote = {\n productId: string;\n amountCents: number;\n currency: string;\n credits: number;\n};\nexport type CheckoutHandoff = {\n id: string;\n accountId: string;\n quote: CheckoutQuote;\n expiresAt: number;\n};\nconst lifetimeMs = 15 * 60 * 1000;\nconst hash = (secret: string) =>\n createHash(\"sha256\").update(secret).digest(\"hex\");\nconst secret = () => randomBytes(32).toString(\"base64url\");\nconst validSecret = (value: string) => /^[A-Za-z0-9_-]{43}$/.test(value);\nconst validQuote = (value: CheckoutQuote) =>\n typeof value.productId === \"string\" &&\n value.productId.length > 0 &&\n value.productId.length <= 128 &&\n Number.isSafeInteger(value.amountCents) &&\n value.amountCents > 0 &&\n /^[A-Z]{3}$/.test(value.currency) &&\n Number.isSafeInteger(value.credits) &&\n value.credits > 0;\n\n/** Apply explicitly during deployment. Secrets are stored only as SHA-256 hashes. */\nexport const checkoutHandoffPostgresSchemaSql = () => `\nCREATE SCHEMA IF NOT EXISTS billing_checkout;\nCREATE TABLE IF NOT EXISTS billing_checkout.handoffs (\n id text PRIMARY KEY, account_id text NOT NULL, quote jsonb NOT NULL,\n code_hash text NOT NULL UNIQUE, expires_at bigint NOT NULL,\n session_hash text UNIQUE, csrf_hash text, session_expires_at bigint\n);\nCREATE INDEX IF NOT EXISTS handoffs_account ON billing_checkout.handoffs(account_id, id);\n`;\nconst decode = (\n row: Record<string, unknown> | undefined,\n): CheckoutHandoff | null => {\n if (!row) return null;\n const quote = row.quote;\n if (\n !quote ||\n typeof quote !== \"object\" ||\n !(\"productId\" in quote) ||\n !(\"amountCents\" in quote) ||\n !(\"currency\" in quote) ||\n !(\"credits\" in quote) ||\n typeof quote.productId !== \"string\" ||\n typeof quote.amountCents !== \"number\" ||\n typeof quote.currency !== \"string\" ||\n typeof quote.credits !== \"number\" ||\n typeof row.id !== \"string\" ||\n typeof row.account_id !== \"string\"\n )\n throw new Error(\"Invalid checkout record\");\n const parsed = {\n productId: quote.productId,\n amountCents: quote.amountCents,\n currency: quote.currency,\n credits: quote.credits,\n };\n if (!validQuote(parsed)) throw new Error(\"Invalid checkout quote\");\n return {\n id: row.id,\n accountId: row.account_id,\n quote: parsed,\n expiresAt: Number(row.expires_at),\n };\n};\n/** This is a purchase-only capability, never a login or a saved-card authorization.\n * The application binds the authenticated account and prices the quote. */\nexport const createPostgresCheckoutHandoffs = (\n db: CreditSqlClient,\n now = Date.now,\n) => ({\n async issue(accountId: string, quote: CheckoutQuote) {\n if (!accountId || !validQuote(quote))\n throw new Error(\"Invalid checkout request\");\n const code = secret();\n const id = randomUUID();\n const expiresAt = now() + lifetimeMs;\n await db.query(\n `INSERT INTO billing_checkout.handoffs(id,account_id,quote,code_hash,expires_at) VALUES ($1,$2,$3::text::jsonb,$4,$5)`,\n [id, accountId, JSON.stringify(quote), hash(code), expiresAt],\n );\n return { id, code, expiresAt };\n },\n /** Invoke only on a deliberate same-origin POST, never a preview GET.\n * Atomic exchange makes concurrent redemption and link replay fail closed. */\n async exchange(code: string, browserAccountId: string | null = null) {\n if (!validSecret(code)) return null;\n const session = secret();\n const csrf = secret();\n const expiresAt = now() + lifetimeMs;\n const { rows } = await db.query(\n `UPDATE billing_checkout.handoffs SET session_hash=$2,csrf_hash=$3,session_expires_at=$4 WHERE code_hash=$1 AND session_hash IS NULL AND expires_at>$5 AND ($6::text IS NULL OR account_id=$6) RETURNING *`,\n [\n hash(code),\n hash(session),\n hash(csrf),\n expiresAt,\n now(),\n browserAccountId,\n ],\n );\n const handoff = decode(rows[0]);\n return handoff ? { handoff, session, csrf, expiresAt } : null;\n },\n async authorize(\n session: string,\n csrf: string,\n browserAccountId: string | null = null,\n ) {\n if (!validSecret(session) || !validSecret(csrf)) return null;\n const { rows } = await db.query(\n `SELECT * FROM billing_checkout.handoffs WHERE session_hash=$1 AND csrf_hash=$2 AND session_expires_at>$3 AND ($4::text IS NULL OR account_id=$4)`,\n [hash(session), hash(csrf), now(), browserAccountId],\n );\n return decode(rows[0]);\n },\n /** Public IDs are not capabilities: status always requires account binding. */\n async get(accountId: string, id: string) {\n const { rows } = await db.query(\n `SELECT * FROM billing_checkout.handoffs WHERE id=$1 AND account_id=$2`,\n [id, accountId],\n );\n return decode(rows[0]);\n },\n});\n\n/** Put the code in a fragment so previews, servers and referrers don't receive it.\n * The landing page must remove it before loading any third-party code. */\nexport const checkoutHandoffUrl = (landingUrl: string, code: string) => {\n const url = new URL(landingUrl);\n if (\n url.protocol !== \"https:\" ||\n url.username ||\n url.password ||\n url.search ||\n url.hash ||\n !validSecret(code)\n )\n throw new Error(\"Invalid checkout landing URL\");\n url.hash = code;\n return url.href;\n};\nexport const checkoutSameOrigin = (request: Request, origin: string) =>\n request.method === \"POST\" &&\n request.headers.get(\"origin\") === new URL(origin).origin &&\n [null, \"same-origin\"].includes(request.headers.get(\"sec-fetch-site\"));\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAeA,IAAM,aAAa,KAAK,KAAK;AAC7B,IAAM,OAAO,CAAC,WACZ,WAAW,QAAQ,EAAE,OAAO,MAAM,EAAE,OAAO,KAAK;AAClD,IAAM,SAAS,MAAM,YAAY,EAAE,EAAE,SAAS,WAAW;AACzD,IAAM,cAAc,CAAC,UAAkB,sBAAsB,KAAK,KAAK;AACvE,IAAM,aAAa,CAAC,UAClB,OAAO,MAAM,cAAc,YAC3B,MAAM,UAAU,SAAS,KACzB,MAAM,UAAU,UAAU,OAC1B,OAAO,cAAc,MAAM,WAAW,KACtC,MAAM,cAAc,KACpB,aAAa,KAAK,M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S,MAAM,UAAU,IAAI;AAAA;AAAA,OAErD,UAAS,CACb,SACA,MACA,mBAAkC,MAClC;AAAA,IACA,IAAI,CAAC,YAAY,OAAO,KAAK,CAAC,YAAY,IAAI;AAAA,MAAG,OAAO;AAAA,IACxD,QAAQ,SAAS,MAAM,GAAG,MACxB,oJACA,CAAC,KAAK,OAAO,GAAG,KAAK,IAAI,GAAG,IAAI,GAAG,gBAAgB,CACrD;AAAA,IACA,OAAO,OAAO,KAAK,EAAE;AAAA;AAAA,OAGjB,IAAG,CAAC,WAAmB,IAAY;AAAA,IACvC,QAAQ,SAAS,MAAM,GAAG,MACxB,yEACA,CAAC,IAAI,SAAS,CAChB;AAAA,IACA,OAAO,OAAO,KAAK,EAAE;AAAA;AAEzB;AAIO,IAAM,qBAAqB,CAAC,YAAoB,SAAiB;AAAA,EACtE,MAAM,MAAM,IAAI,IAAI,UAAU;AAAA,EAC9B,IACE,IAAI,aAAa,YACjB,IAAI,YACJ,IAAI,YACJ,IAAI,UACJ,IAAI,QACJ,CAAC,YAAY,IAAI;AAAA,IAEjB,MAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD,IAAI,OAAO;AAAA,EACX,OAAO,IAAI;AAAA;AAEN,IAAM,qBAAqB,CAAC,SAAkB,WACnD,QAAQ,WAAW,UACnB,QAAQ,QAAQ,IAAI,QAAQ,MAAM,IAAI,IAAI,MAAM,EAAE,UAClD,CAAC,MAAM,aAAa,EAAE,SAAS,QAAQ,QAAQ,IAAI,gBAAgB,CAAC;",
8
+ "debugId": "13C01DD2B5D2689364756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,10 @@
1
1
  import { type CreditSqlClient } from "./prepaidPostgres";
2
+ export type DeferredCreditBinding = {
3
+ effectId: string;
4
+ authorizationId: string;
5
+ };
2
6
  export type CreditWork = {
7
+ deferred?: DeferredCreditBinding;
3
8
  request: string;
4
9
  budget: number;
5
10
  charged: number;
@@ -12,6 +17,15 @@ export declare const creditWorkPostgresSchemaSql: (schema?: string) => string;
12
17
  * and its credits held: never automatically rerun an uncertain external effect.
13
18
  * Provider cost over the agreed budget is recorded as absorbed, not customer debt. */
14
19
  export declare const createPostgresCreditWork: (client: CreditSqlClient, schema?: string) => {
20
+ /** Call inside the same transaction as begin, authorization binding and outbox
21
+ * enqueue. This transfers a reservation, never starts work or grants a lease. */
22
+ handoff: (accountId: string, workId: string, binding: DeferredCreditBinding) => Promise<CreditWork>;
23
+ /** Trusted worker reattaches accounting after its durable execution claim.
24
+ * This is NOT execution authorization or permission to retry a provider. */
25
+ resumeDeferred: (accountId: string, workId: string, binding: DeferredCreditBinding) => Promise<CreditWork>;
26
+ /** Settle only a durable terminal result after all usage writes are drained.
27
+ * Unknown outcomes keep credits reserved and require operator reconciliation. */
28
+ finishDeferred: (accountId: string, workId: string, binding: DeferredCreditBinding, outcome: "succeeded" | "failed" | "unknown", result: string) => Promise<CreditWork>;
15
29
  get: (accountId: string, workId: string) => Promise<CreditWork | null>;
16
30
  begin: (accountId: string, workId: string, request: string, budget: number) => Promise<{
17
31
  fresh: boolean;
@@ -295,7 +295,7 @@ var createPostgresCreditAccountStore = (client, schema = "billing_credits") => {
295
295
  return {
296
296
  initialize: async (accountId, seed) => {
297
297
  validateCreditAccount(seed);
298
- await client.query(`INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::jsonb, $2::jsonb) ON CONFLICT DO NOTHING`, [accountId, JSON.stringify(seed)]);
298
+ await client.query(`INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::text::jsonb, $2::text::jsonb) ON CONFLICT DO NOTHING`, [accountId, JSON.stringify(seed)]);
299
299
  },
300
300
  read: (accountId) => read(client, accountId),
301
301
  transaction: (accountId, run) => client.transaction(async (sql) => {
@@ -313,14 +313,14 @@ var createPostgresCreditAccountStore = (client, schema = "billing_credits") => {
313
313
  return rows[0]?.state ?? null;
314
314
  },
315
315
  save: async (operationId, receipt) => {
316
- await sql.query(`UPDATE ${n}.accounts SET state = $2::jsonb, updated_at = now() WHERE account_id = $1`, [accountId, JSON.stringify(receipt.account)]);
316
+ await sql.query(`UPDATE ${n}.accounts SET state = $2::text::jsonb, updated_at = now() WHERE account_id = $1`, [accountId, JSON.stringify(receipt.account)]);
317
317
  if (receipt.reservation)
318
- await sql.query(`INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`, [
318
+ await sql.query(`INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::text::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`, [
319
319
  accountId,
320
320
  receipt.reservation.id,
321
321
  JSON.stringify(receipt.reservation)
322
322
  ]);
323
- await sql.query(`INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::jsonb)`, [accountId, operationId, JSON.stringify(receipt)]);
323
+ await sql.query(`INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::text::jsonb)`, [accountId, operationId, JSON.stringify(receipt)]);
324
324
  }
325
325
  });
326
326
  })
@@ -360,9 +360,89 @@ var createPostgresCreditWork = (client, schema = "billing_credits") => {
360
360
  const { rows } = await sql.query(`SELECT state FROM ${n}.work WHERE account_id = $1 AND work_id = $2 FOR UPDATE`, [accountId, workId]);
361
361
  return rows[0]?.state;
362
362
  };
363
- const save = (sql, accountId, workId, state) => sql.query(`UPDATE ${n}.work SET state = $3::jsonb, updated_at = now() WHERE account_id = $1 AND work_id = $2`, [accountId, workId, JSON.stringify(state)]);
363
+ const save = (sql, accountId, workId, state) => sql.query(`UPDATE ${n}.work SET state = $3::text::jsonb, updated_at = now() WHERE account_id = $1 AND work_id = $2`, [accountId, workId, JSON.stringify(state)]);
364
364
  const ledger = (sql) => createCreditAccountLedger(createPostgresCreditAccountStore({ ...sql, transaction: (run) => run(sql) }, schema));
365
+ const bindingMatches = (work, binding) => {
366
+ id(binding.effectId);
367
+ id(binding.authorizationId);
368
+ if (work.deferred?.effectId !== binding.effectId || work.deferred.authorizationId !== binding.authorizationId)
369
+ throw new Error("Deferred credit authorization mismatch");
370
+ };
371
+ const finish = (accountId, workId, result, failed, binding) => {
372
+ id(accountId);
373
+ id(workId);
374
+ return client.transaction(async (sql) => {
375
+ await sql.query(`SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`, [accountId]);
376
+ const work = await read(sql, accountId, workId);
377
+ if (!work)
378
+ throw new Error("Credit work does not exist");
379
+ if (binding)
380
+ bindingMatches(work, binding);
381
+ else if (work.deferred)
382
+ throw new Error("Deferred work requires its bound worker outcome");
383
+ if (work.status !== "running")
384
+ return work;
385
+ await ledger(sql).execute(accountId, `work-settle:${workId}`, {
386
+ kind: "settle",
387
+ reservationId: `work:${workId}`,
388
+ credits: work.charged
389
+ });
390
+ work.status = failed ? "failed" : "completed";
391
+ work.result = result;
392
+ await save(sql, accountId, workId, work);
393
+ return work;
394
+ });
395
+ };
365
396
  return {
397
+ handoff: (accountId, workId, binding) => {
398
+ id(accountId);
399
+ id(workId);
400
+ id(binding.effectId);
401
+ id(binding.authorizationId);
402
+ return client.transaction(async (sql) => {
403
+ const work = await read(sql, accountId, workId);
404
+ if (!work)
405
+ throw new Error("Credit work does not exist");
406
+ if (work.deferred) {
407
+ bindingMatches(work, binding);
408
+ return work;
409
+ }
410
+ if (work.status !== "running")
411
+ throw new Error("Credit work already finished");
412
+ work.deferred = {
413
+ effectId: binding.effectId,
414
+ authorizationId: binding.authorizationId
415
+ };
416
+ await save(sql, accountId, workId, work);
417
+ return work;
418
+ });
419
+ },
420
+ resumeDeferred: (accountId, workId, binding) => {
421
+ id(accountId);
422
+ id(workId);
423
+ return client.transaction(async (sql) => {
424
+ const work = await read(sql, accountId, workId);
425
+ if (!work)
426
+ throw new Error("Credit work does not exist");
427
+ bindingMatches(work, binding);
428
+ return work;
429
+ });
430
+ },
431
+ finishDeferred: async (accountId, workId, binding, outcome, result) => {
432
+ if (outcome !== "succeeded" && outcome !== "failed" && outcome !== "unknown")
433
+ throw new Error("Invalid deferred outcome");
434
+ if (outcome !== "unknown")
435
+ return finish(accountId, workId, result, outcome === "failed", binding);
436
+ id(accountId);
437
+ id(workId);
438
+ return client.transaction(async (sql) => {
439
+ const work = await read(sql, accountId, workId);
440
+ if (!work)
441
+ throw new Error("Credit work does not exist");
442
+ bindingMatches(work, binding);
443
+ return work;
444
+ });
445
+ },
366
446
  get: (accountId, workId) => {
367
447
  id(accountId);
368
448
  id(workId);
@@ -395,7 +475,7 @@ var createPostgresCreditWork = (client, schema = "billing_credits") => {
395
475
  status: "running",
396
476
  result: null
397
477
  };
398
- await sql.query(`INSERT INTO ${n}.work (account_id, work_id, state) VALUES ($1, $2, $3::jsonb)`, [accountId, workId, JSON.stringify(work)]);
478
+ await sql.query(`INSERT INTO ${n}.work (account_id, work_id, state) VALUES ($1, $2, $3::text::jsonb)`, [accountId, workId, JSON.stringify(work)]);
399
479
  return { fresh: true, work };
400
480
  });
401
481
  },
@@ -428,27 +508,7 @@ var createPostgresCreditWork = (client, schema = "billing_credits") => {
428
508
  return { charged, fresh: true };
429
509
  });
430
510
  },
431
- finish: (accountId, workId, result, failed = false) => {
432
- id(accountId);
433
- id(workId);
434
- return client.transaction(async (sql) => {
435
- await sql.query(`SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`, [accountId]);
436
- const work = await read(sql, accountId, workId);
437
- if (!work)
438
- throw new Error("Credit work does not exist");
439
- if (work.status !== "running")
440
- return work;
441
- await ledger(sql).execute(accountId, `work-settle:${workId}`, {
442
- kind: "settle",
443
- reservationId: `work:${workId}`,
444
- credits: work.charged
445
- });
446
- work.status = failed ? "failed" : "completed";
447
- work.result = result;
448
- await save(sql, accountId, workId, work);
449
- return work;
450
- });
451
- }
511
+ finish: (accountId, workId, result, failed = false) => finish(accountId, workId, result, failed)
452
512
  };
453
513
  };
454
514
  export {
@@ -456,5 +516,5 @@ export {
456
516
  creditWorkPostgresSchemaSql
457
517
  };
458
518
 
459
- //# debugId=AA6CCB44B9F64D8964756E2164756E21
519
+ //# debugId=72A8784DCF4A1CD464756E2164756E21
460
520
  //# sourceMappingURL=creditWork.js.map
@@ -3,10 +3,10 @@
3
3
  "sources": ["../src/prepaid.ts", "../src/prepaidPostgres.ts", "../src/creditWork.ts"],
4
4
  "sourcesContent": [
5
5
  "/** Service-credit accounting. Host authorization and payment verification happen\n * before these commands. Every command and its receipt commit in one transaction. */\nexport type CreditAccount = {\n periodId: string;\n periodEnd: string | null;\n periodAllowance: number;\n periodRemaining: number;\n purchasedRemaining: number;\n promotionalRemaining: number;\n debt: number;\n reserved: number;\n consumed: number;\n};\nexport type CreditAllocation = {\n period: number;\n purchased: number;\n promotional: number;\n};\nexport type CreditReservation = {\n id: string;\n periodId: string;\n allocation: CreditAllocation;\n status: \"reserved\" | \"settled\" | \"released\";\n charged: number | null;\n};\nexport type CreditCommand =\n | { kind: \"grant\"; bucket: \"purchased\" | \"promotional\"; credits: number }\n | { kind: \"reverse\"; bucket: \"purchased\" | \"promotional\"; credits: number }\n | {\n kind: \"period\";\n periodId: string;\n periodEnd: string | null;\n allowance: number;\n }\n | { kind: \"reserve\"; reservationId: string; credits: number }\n | { kind: \"settle\"; reservationId: string; credits: number }\n | { kind: \"release\"; reservationId: string }\n | { kind: \"debit\"; credits: number; allowDebt?: boolean; reference?: string };\nexport type CreditReceipt = {\n command: CreditCommand;\n account: CreditAccount;\n reservation?: CreditReservation;\n};\nexport type CreditTransaction = {\n account: CreditAccount;\n receipt: (operationId: string) => Promise<CreditReceipt | null>;\n reservation: (id: string) => Promise<CreditReservation | null>;\n save: (operationId: string, receipt: CreditReceipt) => Promise<void>;\n};\nexport type CreditAccountStore = {\n /** Persist seed once. Never overwrite an existing account. */\n initialize: (accountId: string, seed: CreditAccount) => Promise<void>;\n read: (accountId: string) => Promise<CreditAccount | null>;\n /** Lock one account, serialize its commands across processes, and roll back\n * account, reservation and receipt together if the callback fails. */\n transaction: <T>(\n accountId: string,\n run: (tx: CreditTransaction) => Promise<T>,\n ) => Promise<T>;\n};\nconst integer = (value: number) => {\n if (!Number.isSafeInteger(value) || value < 0)\n throw new Error(\"Credits must be nonnegative safe integers\");\n return value;\n};\nconst periodIdentity = (value: string) => {\n if (\n !Number.isFinite(Date.parse(value)) ||\n new Date(value).toISOString() !== value\n )\n throw new Error(\"Credit period ID must be an ISO UTC start timestamp\");\n return value;\n};\nconst identity = (value: string) => {\n if (!value || value.length > 256)\n throw new Error(\"Credit identity is invalid\");\n return value;\n};\nexport const validateCreditAccount = (account: CreditAccount) => {\n periodIdentity(account.periodId);\n if (\n account.periodEnd !== null &&\n periodIdentity(account.periodEnd) <= account.periodId\n )\n throw new Error(\"Credit period end must follow its start\");\n for (const key of [\n \"periodAllowance\",\n \"periodRemaining\",\n \"purchasedRemaining\",\n \"promotionalRemaining\",\n \"debt\",\n \"reserved\",\n \"consumed\",\n ] as const)\n integer(account[key]);\n integer(\n account.periodRemaining +\n account.purchasedRemaining +\n account.promotionalRemaining,\n );\n if (account.periodRemaining > account.periodAllowance)\n throw new Error(\"Period balance exceeds its allowance\");\n};\nexport const availableCredits = (account: CreditAccount) =>\n Math.max(\n 0,\n account.periodRemaining +\n account.purchasedRemaining +\n account.promotionalRemaining -\n account.debt,\n );\n\n/** Preserve the currently observable legacy balance without inventing purchase\n * provenance. Historical mixed bonus grants remain promotional carryover. */\nexport const migrateLegacyCreditAccount = (input: {\n periodId: string;\n periodEnd?: string | null;\n periodAllowance: number;\n bonusCredits: number;\n consumed: number;\n}): CreditAccount => {\n integer(input.periodAllowance);\n integer(input.consumed);\n if (!Number.isSafeInteger(input.bonusCredits))\n throw new Error(\"Invalid legacy bonus credits\");\n const account: CreditAccount = {\n periodId: input.periodId,\n periodEnd: input.periodEnd ?? null,\n periodAllowance: input.periodAllowance,\n periodRemaining: Math.max(0, input.periodAllowance - input.consumed),\n purchasedRemaining: 0,\n promotionalRemaining: Math.max(\n 0,\n input.bonusCredits - Math.max(0, input.consumed - input.periodAllowance),\n ),\n debt:\n Math.max(0, -input.bonusCredits) +\n Math.max(\n 0,\n input.consumed -\n input.periodAllowance -\n Math.max(0, input.bonusCredits),\n ),\n reserved: 0,\n consumed: input.consumed,\n };\n validateCreditAccount(account);\n return account;\n};\nconst allocationTotal = (value: CreditAllocation) =>\n value.period + value.promotional + value.purchased;\nconst take = (account: CreditAccount, credits: number): CreditAllocation => {\n // A reversal liability consumes existing available credits exactly once.\n let owed = account.debt;\n for (const key of [\n \"periodRemaining\",\n \"promotionalRemaining\",\n \"purchasedRemaining\",\n ] as const) {\n const covered = Math.min(account[key], owed);\n account[key] -= covered;\n owed -= covered;\n }\n account.debt = owed;\n const period = Math.min(account.periodRemaining, credits);\n const promotional = Math.min(account.promotionalRemaining, credits - period);\n const purchased = Math.min(\n account.purchasedRemaining,\n credits - period - promotional,\n );\n account.periodRemaining -= period;\n account.promotionalRemaining -= promotional;\n account.purchasedRemaining -= purchased;\n return { period, promotional, purchased };\n};\nconst grant = (\n account: CreditAccount,\n key: \"periodRemaining\" | \"purchasedRemaining\" | \"promotionalRemaining\",\n credits: number,\n) => {\n const covered = Math.min(account.debt, credits);\n account.debt -= covered;\n account[key] = integer(account[key] + credits - covered);\n};\nconst normalize = (command: CreditCommand): CreditCommand => {\n switch (command.kind) {\n case \"grant\":\n case \"reverse\":\n if (command.bucket !== \"purchased\" && command.bucket !== \"promotional\")\n throw new Error(\"Invalid credit bucket\");\n return {\n kind: command.kind,\n bucket: command.bucket,\n credits: integer(command.credits),\n };\n case \"period\":\n return {\n kind: command.kind,\n periodId: periodIdentity(command.periodId),\n periodEnd:\n command.periodEnd === null ? null : periodIdentity(command.periodEnd),\n allowance: integer(command.allowance),\n };\n case \"reserve\":\n case \"settle\":\n return {\n kind: command.kind,\n reservationId: identity(command.reservationId),\n credits: integer(command.credits),\n };\n case \"release\":\n return {\n kind: command.kind,\n reservationId: identity(command.reservationId),\n };\n case \"debit\":\n return {\n kind: command.kind,\n credits: integer(command.credits),\n allowDebt: command.allowDebt === true,\n ...(command.reference === undefined\n ? {}\n : { reference: identity(command.reference) }),\n };\n default:\n throw new Error(\"Invalid credit command\");\n }\n};\nexport const createCreditAccountLedger = (store: CreditAccountStore) => ({\n initialize: async (accountId: string, seed: CreditAccount) => {\n identity(accountId);\n validateCreditAccount(seed);\n await store.initialize(accountId, seed);\n },\n receipt: (accountId: string, operationId: string) =>\n store.transaction(identity(accountId), (tx) =>\n tx.receipt(identity(operationId)),\n ),\n balance: (accountId: string) => store.read(identity(accountId)),\n execute: async (\n accountId: string,\n operationId: string,\n input: CreditCommand,\n ): Promise<CreditReceipt> => {\n identity(accountId);\n identity(operationId);\n const command = normalize(input);\n return store.transaction(accountId, async (tx) => {\n const previous = await tx.receipt(operationId);\n if (previous) {\n if (\n JSON.stringify(normalize(previous.command)) !==\n JSON.stringify(command)\n )\n throw new Error(\"Credit operation ID reused with different input\");\n return previous;\n }\n const account = { ...tx.account };\n validateCreditAccount(account);\n let reservation: CreditReservation | undefined;\n switch (command.kind) {\n case \"grant\":\n grant(\n account,\n command.bucket === \"purchased\"\n ? \"purchasedRemaining\"\n : \"promotionalRemaining\",\n command.credits,\n );\n break;\n case \"reverse\": {\n const key =\n command.bucket === \"purchased\"\n ? \"purchasedRemaining\"\n : \"promotionalRemaining\";\n const covered = Math.min(account[key], command.credits);\n account[key] -= covered;\n account.debt += command.credits - covered;\n break;\n }\n case \"period\":\n if (command.periodId < account.periodId)\n throw new Error(\"Credit period cannot move backwards\");\n if (command.periodId === account.periodId) {\n // Upgrades add only the increase. Downgrades take effect next period.\n const increase = Math.max(\n 0,\n command.allowance - account.periodAllowance,\n );\n if (account.periodEnd === null)\n account.periodEnd = command.periodEnd;\n account.periodAllowance += increase;\n grant(account, \"periodRemaining\", increase);\n } else {\n account.periodId = command.periodId;\n account.periodEnd = command.periodEnd;\n account.periodAllowance = command.allowance;\n account.periodRemaining = 0;\n account.consumed = 0;\n grant(account, \"periodRemaining\", command.allowance);\n }\n break;\n case \"debit\": {\n if (!command.allowDebt && availableCredits(account) < command.credits)\n throw new Error(\"Insufficient credits\");\n const used = allocationTotal(take(account, command.credits));\n account.debt += command.credits - used;\n account.consumed += command.credits;\n break;\n }\n case \"reserve\":\n if (command.credits === 0)\n throw new Error(\"Reservation must be positive\");\n if (await tx.reservation(command.reservationId))\n throw new Error(\"Credit reservation already exists\");\n if (availableCredits(account) < command.credits)\n throw new Error(\"Insufficient credits\");\n reservation = {\n id: command.reservationId,\n periodId: account.periodId,\n allocation: take(account, command.credits),\n status: \"reserved\",\n charged: null,\n };\n account.reserved += command.credits;\n break;\n case \"settle\":\n case \"release\": {\n const held = await tx.reservation(command.reservationId);\n if (!held || held.status !== \"reserved\")\n throw new Error(\"Credit reservation is not active\");\n const total = allocationTotal(held.allocation);\n const charged = command.kind === \"settle\" ? command.credits : 0;\n if (charged > total)\n throw new Error(\"Charge exceeds reserved credits\");\n let refund = total - charged;\n for (const [bucket, key] of [\n [\"purchased\", \"purchasedRemaining\"],\n [\"promotional\", \"promotionalRemaining\"],\n [\"period\", \"periodRemaining\"],\n ] as const) {\n const restored = Math.min(held.allocation[bucket], refund);\n refund -= restored;\n if (bucket !== \"period\" || held.periodId === account.periodId)\n grant(account, key, restored);\n }\n account.reserved -= total;\n if (held.periodId === account.periodId) account.consumed += charged;\n reservation = {\n ...held,\n status: command.kind === \"settle\" ? \"settled\" : \"released\",\n charged,\n };\n break;\n }\n }\n validateCreditAccount(account);\n const receipt: CreditReceipt = {\n command,\n account,\n ...(reservation ? { reservation } : {}),\n };\n await tx.save(operationId, receipt);\n return receipt;\n });\n },\n});\n\n/** Portal access and funded MCP access are independent. Period-only free\n * allowances do not silently unlock prepaid work; carryover grants may. */\nexport const creditEntitlements = (input: {\n subscribed: boolean;\n prepaidEnabled: boolean;\n account: CreditAccount | null;\n}) => {\n const funded =\n input.prepaidEnabled &&\n input.account !== null &&\n input.account.purchasedRemaining + input.account.promotionalRemaining > 0 &&\n availableCredits(input.account) > 0;\n return {\n portal: input.subscribed,\n mcp: input.subscribed\n ? (\"member\" as const)\n : funded\n ? (\"prepaid\" as const)\n : (\"free\" as const),\n };\n};\n",
6
- "import {\n validateCreditAccount,\n type CreditAccount,\n type CreditAccountStore,\n type CreditReceipt,\n type CreditReservation,\n} from \"./prepaid\";\nexport type CreditSql = {\n query: (\n query: string,\n parameters: readonly unknown[],\n ) => Promise<{ rows: Record<string, unknown>[] }>;\n};\nexport type CreditSqlClient = CreditSql & {\n transaction: <T>(run: (sql: CreditSql) => Promise<T>) => Promise<T>;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nexport const creditAccountPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE SCHEMA IF NOT EXISTS ${n};\nCREATE TABLE IF NOT EXISTS ${n}.accounts (\n account_id text PRIMARY KEY, state jsonb NOT NULL, initial_state jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()\n);\nCREATE TABLE IF NOT EXISTS ${n}.operations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), operation_id text NOT NULL,\n receipt jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, operation_id)\n);\nCREATE TABLE IF NOT EXISTS ${n}.reservations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), reservation_id text NOT NULL,\n state jsonb NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, reservation_id)\n);`;\n};\nexport const createPostgresCreditAccountStore = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n): CreditAccountStore => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, lock = false) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.accounts WHERE account_id = $1${lock ? \" FOR UPDATE\" : \"\"}`,\n [accountId],\n );\n if (!rows[0]) return null;\n const state = rows[0].state as CreditAccount;\n validateCreditAccount(state);\n return state;\n };\n return {\n initialize: async (accountId, seed) => {\n validateCreditAccount(seed);\n await client.query(\n `INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::jsonb, $2::jsonb) ON CONFLICT DO NOTHING`,\n [accountId, JSON.stringify(seed)],\n );\n },\n read: (accountId) => read(client, accountId),\n transaction: (accountId, run) =>\n client.transaction(async (sql) => {\n const account = await read(sql, accountId, true);\n if (!account) throw new Error(\"Credit account is not initialized\");\n return run({\n account,\n receipt: async (operationId) => {\n const { rows } = await sql.query(\n `SELECT receipt FROM ${n}.operations WHERE account_id = $1 AND operation_id = $2`,\n [accountId, operationId],\n );\n return (rows[0]?.receipt as CreditReceipt | undefined) ?? null;\n },\n reservation: async (id) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.reservations WHERE account_id = $1 AND reservation_id = $2`,\n [accountId, id],\n );\n return (rows[0]?.state as CreditReservation | undefined) ?? null;\n },\n save: async (operationId, receipt) => {\n await sql.query(\n `UPDATE ${n}.accounts SET state = $2::jsonb, updated_at = now() WHERE account_id = $1`,\n [accountId, JSON.stringify(receipt.account)],\n );\n if (receipt.reservation)\n await sql.query(\n `INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`,\n [\n accountId,\n receipt.reservation.id,\n JSON.stringify(receipt.reservation),\n ],\n );\n await sql.query(\n `INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::jsonb)`,\n [accountId, operationId, JSON.stringify(receipt)],\n );\n },\n });\n }),\n };\n};\n",
7
- "import { createCreditAccountLedger } from \"./prepaid\";\nimport {\n createPostgresCreditAccountStore,\n type CreditSql,\n type CreditSqlClient,\n} from \"./prepaidPostgres\";\nexport type CreditWork = {\n request: string;\n budget: number;\n charged: number;\n absorbed: number;\n status: \"running\" | \"completed\" | \"failed\";\n result: string | null;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nconst id = (value: string) => {\n if (!value || value.length > 128) throw new Error(\"Invalid credit work ID\");\n};\nconst integer = (value: number) => {\n if (!Number.isSafeInteger(value) || value < 0)\n throw new Error(\"Invalid credit work amount\");\n};\nexport const creditWorkPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE TABLE IF NOT EXISTS ${n}.work (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), work_id text NOT NULL,\n state jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY(account_id, work_id)\n );\n CREATE TABLE IF NOT EXISTS ${n}.work_usage (\n account_id text NOT NULL, work_id text NOT NULL, event_id text NOT NULL,\n credits bigint NOT NULL CHECK (credits >= 0), charged bigint NOT NULL CHECK (charged >= 0 AND charged <= credits), reference text NOT NULL,\n PRIMARY KEY(account_id, work_id, event_id), FOREIGN KEY(account_id, work_id) REFERENCES ${n}.work(account_id, work_id)\n );`;\n};\n/** Durable execution claim + capped customer charge. A crash leaves work running\n * and its credits held: never automatically rerun an uncertain external effect.\n * Provider cost over the agreed budget is recorded as absorbed, not customer debt. */\nexport const createPostgresCreditWork = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n) => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, workId: string) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.work WHERE account_id = $1 AND work_id = $2 FOR UPDATE`,\n [accountId, workId],\n );\n return rows[0]?.state as CreditWork | undefined;\n };\n const save = (\n sql: CreditSql,\n accountId: string,\n workId: string,\n state: CreditWork,\n ) =>\n sql.query(\n `UPDATE ${n}.work SET state = $3::jsonb, updated_at = now() WHERE account_id = $1 AND work_id = $2`,\n [accountId, workId, JSON.stringify(state)],\n );\n const ledger = (sql: CreditSql) =>\n createCreditAccountLedger(\n createPostgresCreditAccountStore(\n { ...sql, transaction: (run) => run(sql) },\n schema,\n ),\n );\n return {\n get: (accountId: string, workId: string) => {\n id(accountId);\n id(workId);\n return client.transaction(\n async (sql) => (await read(sql, accountId, workId)) ?? null,\n );\n },\n begin: (\n accountId: string,\n workId: string,\n request: string,\n budget: number,\n ) => {\n id(accountId);\n id(workId);\n integer(budget);\n if (!budget || !request || request.length > 256)\n throw new Error(\"Invalid credit work request\");\n return client.transaction(async (sql) => {\n // One account lock serializes competing claims, including absent work rows.\n await sql.query(\n `SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`,\n [accountId],\n );\n const existing = await read(sql, accountId, workId);\n if (existing) {\n if (existing.request !== request || existing.budget !== budget)\n throw new Error(\"Credit work ID reused with different input\");\n return { fresh: false, work: existing };\n }\n await ledger(sql).execute(accountId, `work-reserve:${workId}`, {\n kind: \"reserve\",\n reservationId: `work:${workId}`,\n credits: budget,\n });\n const work: CreditWork = {\n request,\n budget,\n charged: 0,\n absorbed: 0,\n status: \"running\",\n result: null,\n };\n await sql.query(\n `INSERT INTO ${n}.work (account_id, work_id, state) VALUES ($1, $2, $3::jsonb)`,\n [accountId, workId, JSON.stringify(work)],\n );\n return { fresh: true, work };\n });\n },\n record: (\n accountId: string,\n workId: string,\n eventId: string,\n credits: number,\n reference: string,\n ) => {\n id(accountId);\n id(workId);\n id(eventId);\n integer(credits);\n id(reference);\n return client.transaction(async (sql) => {\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n const { rows } = await sql.query(\n `SELECT credits, charged, reference FROM ${n}.work_usage WHERE account_id = $1 AND work_id = $2 AND event_id = $3`,\n [accountId, workId, eventId],\n );\n const previous = rows[0];\n if (previous) {\n if (\n Number(previous.credits) !== credits ||\n previous.reference !== reference\n )\n throw new Error(\"Credit work event reused with different input\");\n return { charged: Number(previous.charged), fresh: false };\n }\n if (work.status !== \"running\")\n throw new Error(\"Credit work already finished\");\n const charged = Math.min(credits, work.budget - work.charged);\n work.charged += charged;\n work.absorbed += credits - charged;\n integer(work.charged);\n integer(work.absorbed);\n await sql.query(\n `INSERT INTO ${n}.work_usage (account_id, work_id, event_id, credits, charged, reference) VALUES ($1, $2, $3, $4, $5, $6)`,\n [accountId, workId, eventId, credits, charged, reference],\n );\n await save(sql, accountId, workId, work);\n return { charged, fresh: true };\n });\n },\n finish: (\n accountId: string,\n workId: string,\n result: string,\n failed = false,\n ) => {\n id(accountId);\n id(workId);\n return client.transaction(async (sql) => {\n // Match begin's account -> work lock order to avoid a retry/finalize deadlock.\n await sql.query(\n `SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`,\n [accountId],\n );\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n if (work.status !== \"running\") return work;\n await ledger(sql).execute(accountId, `work-settle:${workId}`, {\n kind: \"settle\",\n reservationId: `work:${workId}`,\n credits: work.charged,\n });\n work.status = failed ? \"failed\" : \"completed\";\n work.result = result;\n await save(sql, accountId, workId, work);\n return work;\n });\n },\n };\n};\n"
6
+ "import {\n validateCreditAccount,\n type CreditAccount,\n type CreditAccountStore,\n type CreditReceipt,\n type CreditReservation,\n} from \"./prepaid\";\nexport type CreditSql = {\n query: (\n query: string,\n parameters: readonly unknown[],\n ) => Promise<{ rows: Record<string, unknown>[] }>;\n};\nexport type CreditSqlClient = CreditSql & {\n transaction: <T>(run: (sql: CreditSql) => Promise<T>) => Promise<T>;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nexport const creditAccountPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE SCHEMA IF NOT EXISTS ${n};\nCREATE TABLE IF NOT EXISTS ${n}.accounts (\n account_id text PRIMARY KEY, state jsonb NOT NULL, initial_state jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()\n);\nCREATE TABLE IF NOT EXISTS ${n}.operations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), operation_id text NOT NULL,\n receipt jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, operation_id)\n);\nCREATE TABLE IF NOT EXISTS ${n}.reservations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), reservation_id text NOT NULL,\n state jsonb NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, reservation_id)\n);`;\n};\nexport const createPostgresCreditAccountStore = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n): CreditAccountStore => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, lock = false) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.accounts WHERE account_id = $1${lock ? \" FOR UPDATE\" : \"\"}`,\n [accountId],\n );\n if (!rows[0]) return null;\n const state = rows[0].state as CreditAccount;\n validateCreditAccount(state);\n return state;\n };\n return {\n initialize: async (accountId, seed) => {\n validateCreditAccount(seed);\n await client.query(\n `INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::text::jsonb, $2::text::jsonb) ON CONFLICT DO NOTHING`,\n [accountId, JSON.stringify(seed)],\n );\n },\n read: (accountId) => read(client, accountId),\n transaction: (accountId, run) =>\n client.transaction(async (sql) => {\n const account = await read(sql, accountId, true);\n if (!account) throw new Error(\"Credit account is not initialized\");\n return run({\n account,\n receipt: async (operationId) => {\n const { rows } = await sql.query(\n `SELECT receipt FROM ${n}.operations WHERE account_id = $1 AND operation_id = $2`,\n [accountId, operationId],\n );\n return (rows[0]?.receipt as CreditReceipt | undefined) ?? null;\n },\n reservation: async (id) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.reservations WHERE account_id = $1 AND reservation_id = $2`,\n [accountId, id],\n );\n return (rows[0]?.state as CreditReservation | undefined) ?? null;\n },\n save: async (operationId, receipt) => {\n await sql.query(\n `UPDATE ${n}.accounts SET state = $2::text::jsonb, updated_at = now() WHERE account_id = $1`,\n [accountId, JSON.stringify(receipt.account)],\n );\n if (receipt.reservation)\n await sql.query(\n `INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::text::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`,\n [\n accountId,\n receipt.reservation.id,\n JSON.stringify(receipt.reservation),\n ],\n );\n await sql.query(\n `INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::text::jsonb)`,\n [accountId, operationId, JSON.stringify(receipt)],\n );\n },\n });\n }),\n };\n};\n",
7
+ "import { createCreditAccountLedger } from \"./prepaid\";\nimport {\n createPostgresCreditAccountStore,\n type CreditSql,\n type CreditSqlClient,\n} from \"./prepaidPostgres\";\nexport type DeferredCreditBinding = {\n effectId: string;\n authorizationId: string;\n};\nexport type CreditWork = {\n deferred?: DeferredCreditBinding;\n request: string;\n budget: number;\n charged: number;\n absorbed: number;\n status: \"running\" | \"completed\" | \"failed\";\n result: string | null;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nconst id = (value: string) => {\n if (!value || value.length > 128) throw new Error(\"Invalid credit work ID\");\n};\nconst integer = (value: number) => {\n if (!Number.isSafeInteger(value) || value < 0)\n throw new Error(\"Invalid credit work amount\");\n};\nexport const creditWorkPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE TABLE IF NOT EXISTS ${n}.work (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), work_id text NOT NULL,\n state jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY(account_id, work_id)\n );\n CREATE TABLE IF NOT EXISTS ${n}.work_usage (\n account_id text NOT NULL, work_id text NOT NULL, event_id text NOT NULL,\n credits bigint NOT NULL CHECK (credits >= 0), charged bigint NOT NULL CHECK (charged >= 0 AND charged <= credits), reference text NOT NULL,\n PRIMARY KEY(account_id, work_id, event_id), FOREIGN KEY(account_id, work_id) REFERENCES ${n}.work(account_id, work_id)\n );`;\n};\n/** Durable execution claim + capped customer charge. A crash leaves work running\n * and its credits held: never automatically rerun an uncertain external effect.\n * Provider cost over the agreed budget is recorded as absorbed, not customer debt. */\nexport const createPostgresCreditWork = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n) => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, workId: string) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.work WHERE account_id = $1 AND work_id = $2 FOR UPDATE`,\n [accountId, workId],\n );\n return rows[0]?.state as CreditWork | undefined;\n };\n const save = (\n sql: CreditSql,\n accountId: string,\n workId: string,\n state: CreditWork,\n ) =>\n sql.query(\n `UPDATE ${n}.work SET state = $3::text::jsonb, updated_at = now() WHERE account_id = $1 AND work_id = $2`,\n [accountId, workId, JSON.stringify(state)],\n );\n const ledger = (sql: CreditSql) =>\n createCreditAccountLedger(\n createPostgresCreditAccountStore(\n { ...sql, transaction: (run) => run(sql) },\n schema,\n ),\n );\n const bindingMatches = (work: CreditWork, binding: DeferredCreditBinding) => {\n id(binding.effectId);\n id(binding.authorizationId);\n if (\n work.deferred?.effectId !== binding.effectId ||\n work.deferred.authorizationId !== binding.authorizationId\n )\n throw new Error(\"Deferred credit authorization mismatch\");\n };\n const finish = (\n accountId: string,\n workId: string,\n result: string,\n failed: boolean,\n binding?: DeferredCreditBinding,\n ) => {\n id(accountId);\n id(workId);\n return client.transaction(async (sql) => {\n await sql.query(\n `SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`,\n [accountId],\n );\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n if (binding) bindingMatches(work, binding);\n else if (work.deferred)\n throw new Error(\"Deferred work requires its bound worker outcome\");\n if (work.status !== \"running\") return work;\n await ledger(sql).execute(accountId, `work-settle:${workId}`, {\n kind: \"settle\",\n reservationId: `work:${workId}`,\n credits: work.charged,\n });\n work.status = failed ? \"failed\" : \"completed\";\n work.result = result;\n await save(sql, accountId, workId, work);\n return work;\n });\n };\n return {\n /** Call inside the same transaction as begin, authorization binding and outbox\n * enqueue. This transfers a reservation, never starts work or grants a lease. */\n handoff: (\n accountId: string,\n workId: string,\n binding: DeferredCreditBinding,\n ) => {\n id(accountId);\n id(workId);\n id(binding.effectId);\n id(binding.authorizationId);\n return client.transaction(async (sql) => {\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n if (work.deferred) {\n bindingMatches(work, binding);\n return work;\n }\n if (work.status !== \"running\")\n throw new Error(\"Credit work already finished\");\n work.deferred = {\n effectId: binding.effectId,\n authorizationId: binding.authorizationId,\n };\n await save(sql, accountId, workId, work);\n return work;\n });\n },\n /** Trusted worker reattaches accounting after its durable execution claim.\n * This is NOT execution authorization or permission to retry a provider. */\n resumeDeferred: (\n accountId: string,\n workId: string,\n binding: DeferredCreditBinding,\n ) => {\n id(accountId);\n id(workId);\n return client.transaction(async (sql) => {\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n bindingMatches(work, binding);\n return work;\n });\n },\n /** Settle only a durable terminal result after all usage writes are drained.\n * Unknown outcomes keep credits reserved and require operator reconciliation. */\n finishDeferred: async (\n accountId: string,\n workId: string,\n binding: DeferredCreditBinding,\n outcome: \"succeeded\" | \"failed\" | \"unknown\",\n result: string,\n ) => {\n if (\n outcome !== \"succeeded\" &&\n outcome !== \"failed\" &&\n outcome !== \"unknown\"\n )\n throw new Error(\"Invalid deferred outcome\");\n if (outcome !== \"unknown\")\n return finish(accountId, workId, result, outcome === \"failed\", binding);\n id(accountId);\n id(workId);\n return client.transaction(async (sql) => {\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n bindingMatches(work, binding);\n return work;\n });\n },\n get: (accountId: string, workId: string) => {\n id(accountId);\n id(workId);\n return client.transaction(\n async (sql) => (await read(sql, accountId, workId)) ?? null,\n );\n },\n begin: (\n accountId: string,\n workId: string,\n request: string,\n budget: number,\n ) => {\n id(accountId);\n id(workId);\n integer(budget);\n if (!budget || !request || request.length > 256)\n throw new Error(\"Invalid credit work request\");\n return client.transaction(async (sql) => {\n // One account lock serializes competing claims, including absent work rows.\n await sql.query(\n `SELECT account_id FROM ${n}.accounts WHERE account_id = $1 FOR UPDATE`,\n [accountId],\n );\n const existing = await read(sql, accountId, workId);\n if (existing) {\n if (existing.request !== request || existing.budget !== budget)\n throw new Error(\"Credit work ID reused with different input\");\n return { fresh: false, work: existing };\n }\n await ledger(sql).execute(accountId, `work-reserve:${workId}`, {\n kind: \"reserve\",\n reservationId: `work:${workId}`,\n credits: budget,\n });\n const work: CreditWork = {\n request,\n budget,\n charged: 0,\n absorbed: 0,\n status: \"running\",\n result: null,\n };\n await sql.query(\n `INSERT INTO ${n}.work (account_id, work_id, state) VALUES ($1, $2, $3::text::jsonb)`,\n [accountId, workId, JSON.stringify(work)],\n );\n return { fresh: true, work };\n });\n },\n record: (\n accountId: string,\n workId: string,\n eventId: string,\n credits: number,\n reference: string,\n ) => {\n id(accountId);\n id(workId);\n id(eventId);\n integer(credits);\n id(reference);\n return client.transaction(async (sql) => {\n const work = await read(sql, accountId, workId);\n if (!work) throw new Error(\"Credit work does not exist\");\n const { rows } = await sql.query(\n `SELECT credits, charged, reference FROM ${n}.work_usage WHERE account_id = $1 AND work_id = $2 AND event_id = $3`,\n [accountId, workId, eventId],\n );\n const previous = rows[0];\n if (previous) {\n if (\n Number(previous.credits) !== credits ||\n previous.reference !== reference\n )\n throw new Error(\"Credit work event reused with different input\");\n return { charged: Number(previous.charged), fresh: false };\n }\n if (work.status !== \"running\")\n throw new Error(\"Credit work already finished\");\n const charged = Math.min(credits, work.budget - work.charged);\n work.charged += charged;\n work.absorbed += credits - charged;\n integer(work.charged);\n integer(work.absorbed);\n await sql.query(\n `INSERT INTO ${n}.work_usage (account_id, work_id, event_id, credits, charged, reference) VALUES ($1, $2, $3, $4, $5, $6)`,\n [accountId, workId, eventId, credits, charged, reference],\n );\n await save(sql, accountId, workId, work);\n return { charged, fresh: true };\n });\n },\n finish: (\n accountId: string,\n workId: string,\n result: string,\n failed = false,\n ) => finish(accountId, workId, result, failed),\n };\n};\n"
8
8
  ],
9
- "mappings": ";;;;;;;;;;;;;;;;;AA4DA,IAAM,UAAU,CAAC,UAAkB;AAAA,EACjC,IAAI,CAAC,OAAO,cAAc,KAAK,KAAK,QAAQ;AAAA,IAC1C,MAAM,IAAI,MAAM,2CAA2C;AAAA,EAC7D,OAAO;AAAA;AAET,IAAM,iBAAiB,CAAC,UAAkB;AAAA,EACxC,IACE,CAAC,OAAO,SAAS,KAAK,MAAM,KAAK,CAAC,KAClC,IAAI,KAAK,KAAK,EAAE,YAAY,MAAM;AAAA,IAElC,MAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE,OAAO;AAAA;AAET,IAAM,WAAW,CAAC,UAAkB;AAAA,EAClC,IAAI,CAAC,SAAS,MAAM,SAAS;AAAA,IAC3B,MAAM,IAAI,MAAM,4BAA4B;AAAA,EAC9C,OAAO;AAAA;AAEF,IAAM,wBAAwB,CAAC,YAA2B;AAAA,EAC/D,eAAe,QAAQ,QAAQ;AAAA,EAC/B,IACE,QAAQ,cAAc,QACtB,eAAe,QAAQ,SAAS,KAAK,QAAQ;AAAA,IAE7C,MAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D,WAAW,OAAO;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,IACE,QAAQ,QAAQ,IAAI;AAAA,EACtB,QACE,QAAQ,kBACN,QAAQ,qBACR,QAAQ,oBACZ;AAAA,EACA,IAAI,QAAQ,kBAA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AAAA,UACZ,IACE,OAAO,SAAS,OAAO,MAAM,WAC7B,SAAS,cAAc;AAAA,YAEvB,MAAM,IAAI,MAAM,+CAA+C;AAAA,UACjE,OAAO,EAAE,SAAS,OAAO,SAAS,OAAO,GAAG,OAAO,MAAM;AAAA,QAC3D;AAAA,QACA,IAAI,KAAK,WAAW;AAAA,UAClB,MAAM,IAAI,MAAM,8BAA8B;AAAA,QAChD,MAAM,UAAU,KAAK,IAAI,SAAS,KAAK,SAAS,KAAK,OAAO;AAAA,QAC5D,KAAK,WAAW;AAAA,QAChB,KAAK,YAAY,UAAU;AAAA,QAC3B,SAAQ,KAAK,OAAO;AAAA,QACpB,SAAQ,KAAK,QAAQ;AAAA,QACrB,MAAM,IAAI,MACR,eAAe,6GACf,CAAC,WAAW,QAAQ,SAAS,SAAS,SAAS,SAAS,CAC1D;AAAA,QACA,MAAM,KAAK,KAAK,WAAW,QAAQ,IAAI;AAAA,QACvC,OAAO,EAAE,SAAS,OAAO,KAAK;AAAA,OAC/B;AAAA;AAAA,IAEH,QAAQ,CACN,WACA,QACA,QACA,SAAS,UACN,OAAO,WAAW,QAAQ,QAAQ,MAAM;AAAA,EAC/C;AAAA;",
10
+ "debugId": "72A8784DCF4A1CD464756E2164756E21",
11
11
  "names": []
12
12
  }
@@ -295,7 +295,7 @@ var createPostgresCreditAccountStore = (client, schema = "billing_credits") => {
295
295
  return {
296
296
  initialize: async (accountId, seed) => {
297
297
  validateCreditAccount(seed);
298
- await client.query(`INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::jsonb, $2::jsonb) ON CONFLICT DO NOTHING`, [accountId, JSON.stringify(seed)]);
298
+ await client.query(`INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::text::jsonb, $2::text::jsonb) ON CONFLICT DO NOTHING`, [accountId, JSON.stringify(seed)]);
299
299
  },
300
300
  read: (accountId) => read(client, accountId),
301
301
  transaction: (accountId, run) => client.transaction(async (sql) => {
@@ -313,14 +313,14 @@ var createPostgresCreditAccountStore = (client, schema = "billing_credits") => {
313
313
  return rows[0]?.state ?? null;
314
314
  },
315
315
  save: async (operationId, receipt) => {
316
- await sql.query(`UPDATE ${n}.accounts SET state = $2::jsonb, updated_at = now() WHERE account_id = $1`, [accountId, JSON.stringify(receipt.account)]);
316
+ await sql.query(`UPDATE ${n}.accounts SET state = $2::text::jsonb, updated_at = now() WHERE account_id = $1`, [accountId, JSON.stringify(receipt.account)]);
317
317
  if (receipt.reservation)
318
- await sql.query(`INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`, [
318
+ await sql.query(`INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::text::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`, [
319
319
  accountId,
320
320
  receipt.reservation.id,
321
321
  JSON.stringify(receipt.reservation)
322
322
  ]);
323
- await sql.query(`INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::jsonb)`, [accountId, operationId, JSON.stringify(receipt)]);
323
+ await sql.query(`INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::text::jsonb)`, [accountId, operationId, JSON.stringify(receipt)]);
324
324
  }
325
325
  });
326
326
  })
@@ -331,5 +331,5 @@ export {
331
331
  creditAccountPostgresSchemaSql
332
332
  };
333
333
 
334
- //# debugId=D56D4E1DB5A095C464756E2164756E21
334
+ //# debugId=9E43895B9F56D08664756E2164756E21
335
335
  //# sourceMappingURL=prepaidPostgres.js.map
@@ -3,9 +3,9 @@
3
3
  "sources": ["../src/prepaid.ts", "../src/prepaidPostgres.ts"],
4
4
  "sourcesContent": [
5
5
  "/** Service-credit accounting. Host authorization and payment verification happen\n * before these commands. Every command and its receipt commit in one transaction. */\nexport type CreditAccount = {\n periodId: string;\n periodEnd: string | null;\n periodAllowance: number;\n periodRemaining: number;\n purchasedRemaining: number;\n promotionalRemaining: number;\n debt: number;\n reserved: number;\n consumed: number;\n};\nexport type CreditAllocation = {\n period: number;\n purchased: number;\n promotional: number;\n};\nexport type CreditReservation = {\n id: string;\n periodId: string;\n allocation: CreditAllocation;\n status: \"reserved\" | \"settled\" | \"released\";\n charged: number | null;\n};\nexport type CreditCommand =\n | { kind: \"grant\"; bucket: \"purchased\" | \"promotional\"; credits: number }\n | { kind: \"reverse\"; bucket: \"purchased\" | \"promotional\"; credits: number }\n | {\n kind: \"period\";\n periodId: string;\n periodEnd: string | null;\n allowance: number;\n }\n | { kind: \"reserve\"; reservationId: string; credits: number }\n | { kind: \"settle\"; reservationId: string; credits: number }\n | { kind: \"release\"; reservationId: string }\n | { kind: \"debit\"; credits: number; allowDebt?: boolean; reference?: string };\nexport type CreditReceipt = {\n command: CreditCommand;\n account: CreditAccount;\n reservation?: CreditReservation;\n};\nexport type CreditTransaction = {\n account: CreditAccount;\n receipt: (operationId: string) => Promise<CreditReceipt | null>;\n reservation: (id: string) => Promise<CreditReservation | null>;\n save: (operationId: string, receipt: CreditReceipt) => Promise<void>;\n};\nexport type CreditAccountStore = {\n /** Persist seed once. Never overwrite an existing account. */\n initialize: (accountId: string, seed: CreditAccount) => Promise<void>;\n read: (accountId: string) => Promise<CreditAccount | null>;\n /** Lock one account, serialize its commands across processes, and roll back\n * account, reservation and receipt together if the callback fails. */\n transaction: <T>(\n accountId: string,\n run: (tx: CreditTransaction) => Promise<T>,\n ) => Promise<T>;\n};\nconst integer = (value: number) => {\n if (!Number.isSafeInteger(value) || value < 0)\n throw new Error(\"Credits must be nonnegative safe integers\");\n return value;\n};\nconst periodIdentity = (value: string) => {\n if (\n !Number.isFinite(Date.parse(value)) ||\n new Date(value).toISOString() !== value\n )\n throw new Error(\"Credit period ID must be an ISO UTC start timestamp\");\n return value;\n};\nconst identity = (value: string) => {\n if (!value || value.length > 256)\n throw new Error(\"Credit identity is invalid\");\n return value;\n};\nexport const validateCreditAccount = (account: CreditAccount) => {\n periodIdentity(account.periodId);\n if (\n account.periodEnd !== null &&\n periodIdentity(account.periodEnd) <= account.periodId\n )\n throw new Error(\"Credit period end must follow its start\");\n for (const key of [\n \"periodAllowance\",\n \"periodRemaining\",\n \"purchasedRemaining\",\n \"promotionalRemaining\",\n \"debt\",\n \"reserved\",\n \"consumed\",\n ] as const)\n integer(account[key]);\n integer(\n account.periodRemaining +\n account.purchasedRemaining +\n account.promotionalRemaining,\n );\n if (account.periodRemaining > account.periodAllowance)\n throw new Error(\"Period balance exceeds its allowance\");\n};\nexport const availableCredits = (account: CreditAccount) =>\n Math.max(\n 0,\n account.periodRemaining +\n account.purchasedRemaining +\n account.promotionalRemaining -\n account.debt,\n );\n\n/** Preserve the currently observable legacy balance without inventing purchase\n * provenance. Historical mixed bonus grants remain promotional carryover. */\nexport const migrateLegacyCreditAccount = (input: {\n periodId: string;\n periodEnd?: string | null;\n periodAllowance: number;\n bonusCredits: number;\n consumed: number;\n}): CreditAccount => {\n integer(input.periodAllowance);\n integer(input.consumed);\n if (!Number.isSafeInteger(input.bonusCredits))\n throw new Error(\"Invalid legacy bonus credits\");\n const account: CreditAccount = {\n periodId: input.periodId,\n periodEnd: input.periodEnd ?? null,\n periodAllowance: input.periodAllowance,\n periodRemaining: Math.max(0, input.periodAllowance - input.consumed),\n purchasedRemaining: 0,\n promotionalRemaining: Math.max(\n 0,\n input.bonusCredits - Math.max(0, input.consumed - input.periodAllowance),\n ),\n debt:\n Math.max(0, -input.bonusCredits) +\n Math.max(\n 0,\n input.consumed -\n input.periodAllowance -\n Math.max(0, input.bonusCredits),\n ),\n reserved: 0,\n consumed: input.consumed,\n };\n validateCreditAccount(account);\n return account;\n};\nconst allocationTotal = (value: CreditAllocation) =>\n value.period + value.promotional + value.purchased;\nconst take = (account: CreditAccount, credits: number): CreditAllocation => {\n // A reversal liability consumes existing available credits exactly once.\n let owed = account.debt;\n for (const key of [\n \"periodRemaining\",\n \"promotionalRemaining\",\n \"purchasedRemaining\",\n ] as const) {\n const covered = Math.min(account[key], owed);\n account[key] -= covered;\n owed -= covered;\n }\n account.debt = owed;\n const period = Math.min(account.periodRemaining, credits);\n const promotional = Math.min(account.promotionalRemaining, credits - period);\n const purchased = Math.min(\n account.purchasedRemaining,\n credits - period - promotional,\n );\n account.periodRemaining -= period;\n account.promotionalRemaining -= promotional;\n account.purchasedRemaining -= purchased;\n return { period, promotional, purchased };\n};\nconst grant = (\n account: CreditAccount,\n key: \"periodRemaining\" | \"purchasedRemaining\" | \"promotionalRemaining\",\n credits: number,\n) => {\n const covered = Math.min(account.debt, credits);\n account.debt -= covered;\n account[key] = integer(account[key] + credits - covered);\n};\nconst normalize = (command: CreditCommand): CreditCommand => {\n switch (command.kind) {\n case \"grant\":\n case \"reverse\":\n if (command.bucket !== \"purchased\" && command.bucket !== \"promotional\")\n throw new Error(\"Invalid credit bucket\");\n return {\n kind: command.kind,\n bucket: command.bucket,\n credits: integer(command.credits),\n };\n case \"period\":\n return {\n kind: command.kind,\n periodId: periodIdentity(command.periodId),\n periodEnd:\n command.periodEnd === null ? null : periodIdentity(command.periodEnd),\n allowance: integer(command.allowance),\n };\n case \"reserve\":\n case \"settle\":\n return {\n kind: command.kind,\n reservationId: identity(command.reservationId),\n credits: integer(command.credits),\n };\n case \"release\":\n return {\n kind: command.kind,\n reservationId: identity(command.reservationId),\n };\n case \"debit\":\n return {\n kind: command.kind,\n credits: integer(command.credits),\n allowDebt: command.allowDebt === true,\n ...(command.reference === undefined\n ? {}\n : { reference: identity(command.reference) }),\n };\n default:\n throw new Error(\"Invalid credit command\");\n }\n};\nexport const createCreditAccountLedger = (store: CreditAccountStore) => ({\n initialize: async (accountId: string, seed: CreditAccount) => {\n identity(accountId);\n validateCreditAccount(seed);\n await store.initialize(accountId, seed);\n },\n receipt: (accountId: string, operationId: string) =>\n store.transaction(identity(accountId), (tx) =>\n tx.receipt(identity(operationId)),\n ),\n balance: (accountId: string) => store.read(identity(accountId)),\n execute: async (\n accountId: string,\n operationId: string,\n input: CreditCommand,\n ): Promise<CreditReceipt> => {\n identity(accountId);\n identity(operationId);\n const command = normalize(input);\n return store.transaction(accountId, async (tx) => {\n const previous = await tx.receipt(operationId);\n if (previous) {\n if (\n JSON.stringify(normalize(previous.command)) !==\n JSON.stringify(command)\n )\n throw new Error(\"Credit operation ID reused with different input\");\n return previous;\n }\n const account = { ...tx.account };\n validateCreditAccount(account);\n let reservation: CreditReservation | undefined;\n switch (command.kind) {\n case \"grant\":\n grant(\n account,\n command.bucket === \"purchased\"\n ? \"purchasedRemaining\"\n : \"promotionalRemaining\",\n command.credits,\n );\n break;\n case \"reverse\": {\n const key =\n command.bucket === \"purchased\"\n ? \"purchasedRemaining\"\n : \"promotionalRemaining\";\n const covered = Math.min(account[key], command.credits);\n account[key] -= covered;\n account.debt += command.credits - covered;\n break;\n }\n case \"period\":\n if (command.periodId < account.periodId)\n throw new Error(\"Credit period cannot move backwards\");\n if (command.periodId === account.periodId) {\n // Upgrades add only the increase. Downgrades take effect next period.\n const increase = Math.max(\n 0,\n command.allowance - account.periodAllowance,\n );\n if (account.periodEnd === null)\n account.periodEnd = command.periodEnd;\n account.periodAllowance += increase;\n grant(account, \"periodRemaining\", increase);\n } else {\n account.periodId = command.periodId;\n account.periodEnd = command.periodEnd;\n account.periodAllowance = command.allowance;\n account.periodRemaining = 0;\n account.consumed = 0;\n grant(account, \"periodRemaining\", command.allowance);\n }\n break;\n case \"debit\": {\n if (!command.allowDebt && availableCredits(account) < command.credits)\n throw new Error(\"Insufficient credits\");\n const used = allocationTotal(take(account, command.credits));\n account.debt += command.credits - used;\n account.consumed += command.credits;\n break;\n }\n case \"reserve\":\n if (command.credits === 0)\n throw new Error(\"Reservation must be positive\");\n if (await tx.reservation(command.reservationId))\n throw new Error(\"Credit reservation already exists\");\n if (availableCredits(account) < command.credits)\n throw new Error(\"Insufficient credits\");\n reservation = {\n id: command.reservationId,\n periodId: account.periodId,\n allocation: take(account, command.credits),\n status: \"reserved\",\n charged: null,\n };\n account.reserved += command.credits;\n break;\n case \"settle\":\n case \"release\": {\n const held = await tx.reservation(command.reservationId);\n if (!held || held.status !== \"reserved\")\n throw new Error(\"Credit reservation is not active\");\n const total = allocationTotal(held.allocation);\n const charged = command.kind === \"settle\" ? command.credits : 0;\n if (charged > total)\n throw new Error(\"Charge exceeds reserved credits\");\n let refund = total - charged;\n for (const [bucket, key] of [\n [\"purchased\", \"purchasedRemaining\"],\n [\"promotional\", \"promotionalRemaining\"],\n [\"period\", \"periodRemaining\"],\n ] as const) {\n const restored = Math.min(held.allocation[bucket], refund);\n refund -= restored;\n if (bucket !== \"period\" || held.periodId === account.periodId)\n grant(account, key, restored);\n }\n account.reserved -= total;\n if (held.periodId === account.periodId) account.consumed += charged;\n reservation = {\n ...held,\n status: command.kind === \"settle\" ? \"settled\" : \"released\",\n charged,\n };\n break;\n }\n }\n validateCreditAccount(account);\n const receipt: CreditReceipt = {\n command,\n account,\n ...(reservation ? { reservation } : {}),\n };\n await tx.save(operationId, receipt);\n return receipt;\n });\n },\n});\n\n/** Portal access and funded MCP access are independent. Period-only free\n * allowances do not silently unlock prepaid work; carryover grants may. */\nexport const creditEntitlements = (input: {\n subscribed: boolean;\n prepaidEnabled: boolean;\n account: CreditAccount | null;\n}) => {\n const funded =\n input.prepaidEnabled &&\n input.account !== null &&\n input.account.purchasedRemaining + input.account.promotionalRemaining > 0 &&\n availableCredits(input.account) > 0;\n return {\n portal: input.subscribed,\n mcp: input.subscribed\n ? (\"member\" as const)\n : funded\n ? (\"prepaid\" as const)\n : (\"free\" as const),\n };\n};\n",
6
- "import {\n validateCreditAccount,\n type CreditAccount,\n type CreditAccountStore,\n type CreditReceipt,\n type CreditReservation,\n} from \"./prepaid\";\nexport type CreditSql = {\n query: (\n query: string,\n parameters: readonly unknown[],\n ) => Promise<{ rows: Record<string, unknown>[] }>;\n};\nexport type CreditSqlClient = CreditSql & {\n transaction: <T>(run: (sql: CreditSql) => Promise<T>) => Promise<T>;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nexport const creditAccountPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE SCHEMA IF NOT EXISTS ${n};\nCREATE TABLE IF NOT EXISTS ${n}.accounts (\n account_id text PRIMARY KEY, state jsonb NOT NULL, initial_state jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()\n);\nCREATE TABLE IF NOT EXISTS ${n}.operations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), operation_id text NOT NULL,\n receipt jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, operation_id)\n);\nCREATE TABLE IF NOT EXISTS ${n}.reservations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), reservation_id text NOT NULL,\n state jsonb NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, reservation_id)\n);`;\n};\nexport const createPostgresCreditAccountStore = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n): CreditAccountStore => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, lock = false) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.accounts WHERE account_id = $1${lock ? \" FOR UPDATE\" : \"\"}`,\n [accountId],\n );\n if (!rows[0]) return null;\n const state = rows[0].state as CreditAccount;\n validateCreditAccount(state);\n return state;\n };\n return {\n initialize: async (accountId, seed) => {\n validateCreditAccount(seed);\n await client.query(\n `INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::jsonb, $2::jsonb) ON CONFLICT DO NOTHING`,\n [accountId, JSON.stringify(seed)],\n );\n },\n read: (accountId) => read(client, accountId),\n transaction: (accountId, run) =>\n client.transaction(async (sql) => {\n const account = await read(sql, accountId, true);\n if (!account) throw new Error(\"Credit account is not initialized\");\n return run({\n account,\n receipt: async (operationId) => {\n const { rows } = await sql.query(\n `SELECT receipt FROM ${n}.operations WHERE account_id = $1 AND operation_id = $2`,\n [accountId, operationId],\n );\n return (rows[0]?.receipt as CreditReceipt | undefined) ?? null;\n },\n reservation: async (id) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.reservations WHERE account_id = $1 AND reservation_id = $2`,\n [accountId, id],\n );\n return (rows[0]?.state as CreditReservation | undefined) ?? null;\n },\n save: async (operationId, receipt) => {\n await sql.query(\n `UPDATE ${n}.accounts SET state = $2::jsonb, updated_at = now() WHERE account_id = $1`,\n [accountId, JSON.stringify(receipt.account)],\n );\n if (receipt.reservation)\n await sql.query(\n `INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`,\n [\n accountId,\n receipt.reservation.id,\n JSON.stringify(receipt.reservation),\n ],\n );\n await sql.query(\n `INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::jsonb)`,\n [accountId, operationId, JSON.stringify(receipt)],\n );\n },\n });\n }),\n };\n};\n"
6
+ "import {\n validateCreditAccount,\n type CreditAccount,\n type CreditAccountStore,\n type CreditReceipt,\n type CreditReservation,\n} from \"./prepaid\";\nexport type CreditSql = {\n query: (\n query: string,\n parameters: readonly unknown[],\n ) => Promise<{ rows: Record<string, unknown>[] }>;\n};\nexport type CreditSqlClient = CreditSql & {\n transaction: <T>(run: (sql: CreditSql) => Promise<T>) => Promise<T>;\n};\nconst namespace = (value: string) => {\n if (!/^[a-z][a-z0-9_]*$/.test(value))\n throw new Error(\"Invalid credit schema name\");\n return value;\n};\nexport const creditAccountPostgresSchemaSql = (schema = \"billing_credits\") => {\n const n = namespace(schema);\n return `CREATE SCHEMA IF NOT EXISTS ${n};\nCREATE TABLE IF NOT EXISTS ${n}.accounts (\n account_id text PRIMARY KEY, state jsonb NOT NULL, initial_state jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()\n);\nCREATE TABLE IF NOT EXISTS ${n}.operations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), operation_id text NOT NULL,\n receipt jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, operation_id)\n);\nCREATE TABLE IF NOT EXISTS ${n}.reservations (\n account_id text NOT NULL REFERENCES ${n}.accounts(account_id), reservation_id text NOT NULL,\n state jsonb NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (account_id, reservation_id)\n);`;\n};\nexport const createPostgresCreditAccountStore = (\n client: CreditSqlClient,\n schema = \"billing_credits\",\n): CreditAccountStore => {\n const n = namespace(schema);\n const read = async (sql: CreditSql, accountId: string, lock = false) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.accounts WHERE account_id = $1${lock ? \" FOR UPDATE\" : \"\"}`,\n [accountId],\n );\n if (!rows[0]) return null;\n const state = rows[0].state as CreditAccount;\n validateCreditAccount(state);\n return state;\n };\n return {\n initialize: async (accountId, seed) => {\n validateCreditAccount(seed);\n await client.query(\n `INSERT INTO ${n}.accounts (account_id, state, initial_state) VALUES ($1, $2::text::jsonb, $2::text::jsonb) ON CONFLICT DO NOTHING`,\n [accountId, JSON.stringify(seed)],\n );\n },\n read: (accountId) => read(client, accountId),\n transaction: (accountId, run) =>\n client.transaction(async (sql) => {\n const account = await read(sql, accountId, true);\n if (!account) throw new Error(\"Credit account is not initialized\");\n return run({\n account,\n receipt: async (operationId) => {\n const { rows } = await sql.query(\n `SELECT receipt FROM ${n}.operations WHERE account_id = $1 AND operation_id = $2`,\n [accountId, operationId],\n );\n return (rows[0]?.receipt as CreditReceipt | undefined) ?? null;\n },\n reservation: async (id) => {\n const { rows } = await sql.query(\n `SELECT state FROM ${n}.reservations WHERE account_id = $1 AND reservation_id = $2`,\n [accountId, id],\n );\n return (rows[0]?.state as CreditReservation | undefined) ?? null;\n },\n save: async (operationId, receipt) => {\n await sql.query(\n `UPDATE ${n}.accounts SET state = $2::text::jsonb, updated_at = now() WHERE account_id = $1`,\n [accountId, JSON.stringify(receipt.account)],\n );\n if (receipt.reservation)\n await sql.query(\n `INSERT INTO ${n}.reservations (account_id, reservation_id, state) VALUES ($1, $2, $3::text::jsonb) ON CONFLICT (account_id, reservation_id) DO UPDATE SET state = EXCLUDED.state, updated_at = now()`,\n [\n accountId,\n receipt.reservation.id,\n JSON.stringify(receipt.reservation),\n ],\n );\n await sql.query(\n `INSERT INTO ${n}.operations (account_id, operation_id, receipt) VALUES ($1, $2, $3::text::jsonb)`,\n [accountId, operationId, JSON.stringify(receipt)],\n );\n },\n });\n }),\n };\n};\n"
7
7
  ],
8
- "mappings": ";;;;;;;;;;;;;;;;;AA4DA,IAAM,UAAU,CAAC,UAAkB;AAAA,EACjC,IAAI,CAAC,OAAO,cAAc,KAAK,KAAK,QAAQ;AAAA,IAC1C,MAAM,IAAI,MAAM,2CAA2C;AAAA,EAC7D,OAAO;AAAA;AAET,IAAM,iBAAiB,CAAC,UAAkB;AAAA,EACxC,IACE,CAAC,OAAO,SAAS,KAAK,MAAM,KAAK,CAAC,KAClC,IAAI,KAAK,KAAK,EAAE,YAAY,MAAM;AAAA,IAElC,MAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE,OAAO;AAAA;AAET,IAAM,WAAW,CAAC,UAAkB;AAAA,EAClC,IAAI,CAAC,SAAS,MAAM,SAAS;AAAA,IAC3B,MAAM,IAAI,MAAM,4BAA4B;AAAA,EAC9C,OAAO;AAAA;AAEF,IAAM,wBAAwB,CAAC,YAA2B;AAAA,EAC/D,eAAe,QAAQ,QAAQ;AAAA,EAC/B,IACE,QAAQ,cAAc,QACtB,eAAe,QAAQ,SAAS,KAAK,QAAQ;AAAA,IAE7C,MAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D,WAAW,OAAO;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA;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AAU,MAAM,KAAK,KAAK,WAAW,IAAI;AAAA,MAC/C,IAAI,CAAC;AAAA,QAAS,MAAM,IAAI,MAAM,mCAAmC;AAAA,MACjE,OAAO,IAAI;AAAA,QACT;AAAA,QACA,SAAS,OAAO,gBAAgB;AAAA,UAC9B,QAAQ,SAAS,MAAM,IAAI,MACzB,uBAAuB,4DACvB,CAAC,WAAW,WAAW,CACzB;AAAA,UACA,OAAQ,KAAK,IAAI,WAAyC;AAAA;AAAA,QAE5D,aAAa,OAAO,OAAO;AAAA,UACzB,QAAQ,SAAS,MAAM,IAAI,MACzB,qBAAqB,gEACrB,CAAC,WAAW,EAAE,CAChB;AAAA,UACA,OAAQ,KAAK,IAAI,SAA2C;AAAA;AAAA,QAE9D,MAAM,OAAO,aAAa,YAAY;AAAA,UACpC,MAAM,IAAI,MACR,UAAU,oFACV,CAAC,WAAW,KAAK,UAAU,QAAQ,OAAO,CAAC,CAC7C;AAAA,UACA,IAAI,QAAQ;AAAA,YACV,MAAM,IAAI,MACR,eAAe,yLACf;AAAA,cACE;AAAA,cACA,QAAQ,YAAY;AAAA,cACpB,KAAK,UAAU,QAAQ,WAAW;AAAA,YACpC,CACF;AAAA,UACF,MAAM,IAAI,MACR,eAAe,qFACf,CAAC,WAAW,aAAa,KAAK,UAAU,OAAO,CAAC,CAClD;AAAA;AAAA,MAEJ,CAAC;AAAA,KACF;AAAA,EACL;AAAA;",
9
+ "debugId": "9E43895B9F56D08664756E2164756E21",
10
10
  "names": []
11
11
  }
@@ -27,3 +27,32 @@ A process crash or unknown effect outcome deliberately leaves work running and i
27
27
  `migrateLegacyCreditAccount` preserves `max(0, periodAllowance + bonusCredits - consumed)` without inventing historical purchase provenance. It first allocates known current-period consumption against period allowance, carries only the residual bonus balance, and records any deficit. Legacy carryover is classified as promotional because the old mixed column cannot prove which credits were purchased. Keep the original row and its payment/usage history as immutable migration evidence. Future verified purchases go to the purchased bucket.
28
28
 
29
29
  Dry-run all accounts, compare current availability before and after, and separately review ambiguous historical purchase allocation before changing customer balances or claiming a reconstructed purchase balance. Run under a writer drain or account-level locks; block legacy writers after cutover. Never roll back by re-enabling the old balance formula, which would restore spent permanent credits. An operational rollback should disable new paid work while keeping the new ledger and recovery reads available.
30
+
31
+ ## PostgreSQL driver JSON encoding
32
+
33
+ Version 0.9.1 binds serialized JSON through `::text::jsonb`. Direct postgres.js infers JSON parameters and otherwise JSON-encodes strings a second time; adapters such as Drizzle override that serializer, which can hide the defect in adapter-only tests. This applies to account state, immutable operation receipts, reservations, work records and checkout quotes.
34
+
35
+ For a real-driver regression, set `BILLING_TEST_DATABASE_URL` to a test database and run `bun test tests/postgresJson.test.ts`. It creates and removes a unique schema containing only synthetic records. The ordinary suite skips this test when the database is absent. Existing double-encoded rows need a separately reviewed repair; updating the package does not rewrite financial records. Preserve originals and verify semantic value equality before committing any repair.
36
+
37
+ ## Deferred worker handoff
38
+
39
+ `createPostgresCreditWork` supports `handoff`, `resumeDeferred` and
40
+ `finishDeferred`. Bind an account-owned work reservation to the exact durable
41
+ `effectId` and immutable `authorizationId`. Call `begin`, `handoff` and the outbox
42
+ insert using one application transaction (a transaction-bound SQL client).
43
+ Approval must bind the reviewed input and explicit maximum credits before enqueue.
44
+ These methods reuse the existing JSON work state; no new DDL is required.
45
+
46
+ A worker must hold its execution claim and validate account, effect and action
47
+ identity before `resumeDeferred`. Restore the saved budget and charged amount in
48
+ its metering context. Reattachment grants no provider authorization, lease or
49
+ retry permission. Drain every usage write before finishing. Settle only a durable
50
+ `succeeded` or definite terminal `failed` outcome. `unknown`, metering uncertainty,
51
+ and nonterminal retries retain the reservation. Recovery of a completed effect
52
+ may retry settlement, never execution. `finish` refuses transferred work;
53
+ `finishDeferred` validates its binding even for repeated terminal calls.
54
+
55
+ Charges remain capped at the approved budget; overruns are absorbed. Unused
56
+ credits are released only on definite settlement. An unknown external outcome
57
+ requires the application's audited reconciliation process before settlement or
58
+ any replacement action. No automatic expiry release is safe for an uncertain send.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/billing",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Provider-neutral pricing and invoice computation used by the hosted AbsoluteJS.ai platform. Converts metered usage into exact integer-micro line items with tiers and allowances.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -90,10 +90,11 @@
90
90
  ],
91
91
  "devDependencies": {
92
92
  "@absolutejs/changelog": "^0.6.0",
93
+ "@electric-sql/pglite": "^0.3.14",
93
94
  "@types/bun": "^1.3.14",
95
+ "postgres": "3.4.9",
94
96
  "prettier": "^3.8.3",
95
- "typescript": "^5.9.0",
96
- "@electric-sql/pglite": "^0.3.14"
97
+ "typescript": "^5.9.0"
97
98
  },
98
99
  "dependencies": {
99
100
  "@absolutejs/manifest": "^0.9.0",