@render-foundation/utils 0.0.249 → 0.0.250

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.
@@ -38,70 +38,94 @@ export const EUR_TO_CREDITS = BigInt(4);
38
38
  // emissions-funded ones (counting only emissions-attributed spend over-issued ~2.58M credits, 2026-08-04).
39
39
  export const GRANT_WINDOW_START = '2025-05-01';
40
40
  export const CREDIT_DECIMALS = BigInt(100000000); // 1e8
41
- /**
42
- * Pure core of the grant allotment: fold a user's OTOY grants against what's already been spent.
43
- *
44
- * consumed = attributedSpend (spend already stamped with an otoy_grant_id)
45
- * + correctionSettled (jobs whose grant is already being settled by a burn_correction)
46
- *
47
- * The correctionSettled term is the DOUBLE-SPEND GUARD: an escrow_credit correction means we're
48
- * recovering that job's dollars from escrow, so its grant is already committed. Counting it here stops
49
- * the same grant from also funding a future emissions burn.
50
- *
51
- * fifoGrantId = the oldest grant still holding room, i.e. the one the next spend draws from.
52
- */
53
- /**
54
- * Time-aware reservoir fold. Events = grant issuances (+, at issue time) and job spend (−, at job time),
55
- * processed chronologically: a job may only draw entitlement that existed WHEN IT RAN. Spend beyond the
56
- * balance at that moment is purchase-paid forever — a later grant must not retroactively absorb it
57
- * (the aggregate `granted − allSpend` formula did exactly that, under-issuing 256 users / 44,243 credits;
58
- * it also let the UI attribute jobs to grants issued days after they ran).
59
- *
60
- * Identity used everywhere (SQL + client): with net = Σamt and overshoot = max prefix deficit,
61
- * avail = max(net + overshoot, 0); beyondGrants = overshoot.
62
- */
63
- export const foldTimeAware = (events) => {
64
- // True reservoir semantics, chronological:
65
- // grant + -> capacity += amt
66
- // grant − -> capacity = max(capacity + amt, 0) (a REVERSAL claws back capacity; it is not spend
67
- // and can never create "beyond grants" — the linear min-run identity got this wrong:
68
- // a reversal ordered before its covering positives produced phantom overshoot)
69
- // job -> draw min(need, capacity); the un-drawable remainder is purchase-paid forever
41
+ // A grant is consumable for this long after issuance; whatever is left after that EXPIRES (2026-08-05
42
+ // model change). A job can only draw grants issued within the lookback window before it ran.
43
+ export const GRANT_LOOKBACK_DAYS = 60;
44
+ export const GRANT_LOOKBACK_MS = GRANT_LOOKBACK_DAYS * 24 * 3600 * 1000;
45
+ export const foldGrantWindow = (events, opts = {}) => {
46
+ const lookback = opts.lookbackMs ?? GRANT_LOOKBACK_MS;
47
+ const now = opts.now ?? Date.now();
70
48
  const sorted = [...events].sort((a, b) => a.t - b.t || (a.amt > b.amt ? -1 : 1)); // credits first on ties
71
- let cap = BigInt(0);
49
+ const buckets = [];
50
+ const remaining = (b) => b.amount - b.reversed - b.drawnEmissions - b.drawnMisrouted;
72
51
  let beyond = BigInt(0);
73
- let charged = BigInt(0);
74
52
  for (const e of sorted) {
75
53
  if (e.src === 'grant') {
76
- cap += e.amt;
77
- if (cap < BigInt(0))
78
- cap = BigInt(0);
54
+ if (e.amt > BigInt(0)) {
55
+ buckets.push({
56
+ grantId: e.grantId,
57
+ t: e.t,
58
+ amount: e.amt,
59
+ reversed: BigInt(0),
60
+ drawnEmissions: BigInt(0),
61
+ drawnMisrouted: BigInt(0),
62
+ expired: BigInt(0),
63
+ left: BigInt(0),
64
+ });
65
+ }
66
+ else {
67
+ let claw = -e.amt; // clawback hits remaining capacity oldest-first; excess is dropped
68
+ for (const b of buckets) {
69
+ if (claw <= BigInt(0))
70
+ break;
71
+ const take = remaining(b) < claw ? remaining(b) : claw;
72
+ if (take > BigInt(0)) {
73
+ b.reversed += take;
74
+ claw -= take;
75
+ }
76
+ }
77
+ }
79
78
  }
80
79
  else {
81
- const need = -e.amt; // job events are negative
82
- const take = need < cap ? need : cap;
83
- cap -= take;
84
- charged += take;
85
- beyond += need - take;
80
+ let need = -e.amt;
81
+ let emissionsLeft = e.emissions ?? need; // no split provided -> count it all as emissions
82
+ for (const b of buckets) {
83
+ if (need <= BigInt(0))
84
+ break;
85
+ if (b.t + lookback < e.t)
86
+ continue; // grant expired before this job ran
87
+ const take = remaining(b) < need ? remaining(b) : need;
88
+ if (take <= BigInt(0))
89
+ continue;
90
+ const em = emissionsLeft < take ? emissionsLeft : take;
91
+ b.drawnEmissions += em;
92
+ b.drawnMisrouted += take - em;
93
+ emissionsLeft -= em;
94
+ need -= take;
95
+ }
96
+ beyond += need;
86
97
  }
87
98
  }
88
- return { avail: cap, beyondGrants: beyond, jobCharged: charged };
89
- };
90
- export const foldGrantAllotment = (grants, attributedSpend, correctionSettled) => {
91
- const granted = grants.reduce((s, g) => s + g.amountCredits, BigInt(0));
92
- const consumed = (attributedSpend > BigInt(0) ? attributedSpend : BigInt(0)) +
93
- (correctionSettled > BigInt(0) ? correctionSettled : BigInt(0));
94
- const avail = granted > consumed ? granted - consumed : BigInt(0);
95
- let drawn = consumed;
99
+ let avail = BigInt(0);
100
+ let expired = BigInt(0);
101
+ let usedEmissions = BigInt(0);
102
+ let usedMisrouted = BigInt(0);
96
103
  let fifoGrantId;
97
- for (const g of grants) {
98
- if (drawn < g.amountCredits) {
99
- fifoGrantId = g.id;
100
- break;
104
+ for (const b of buckets) {
105
+ usedEmissions += b.drawnEmissions;
106
+ usedMisrouted += b.drawnMisrouted;
107
+ const rem = remaining(b);
108
+ if (b.t + lookback < now) {
109
+ b.expired = rem;
110
+ }
111
+ else {
112
+ b.left = rem;
113
+ avail += rem;
114
+ if (fifoGrantId === undefined && rem > BigInt(0) && b.grantId !== undefined)
115
+ fifoGrantId = b.grantId;
101
116
  }
102
- drawn -= g.amountCredits;
117
+ expired += b.expired;
103
118
  }
104
- return { granted, consumed, avail, fifoGrantId };
119
+ return {
120
+ avail,
121
+ beyondGrants: beyond,
122
+ jobCharged: usedEmissions + usedMisrouted,
123
+ usedEmissions,
124
+ usedMisrouted,
125
+ expired,
126
+ buckets,
127
+ fifoGrantId,
128
+ };
105
129
  };
106
130
  import { begin } from './base';
107
131
  import moment from 'moment';
@@ -231,75 +255,50 @@ export const pgClient = (config) => {
231
255
  const out = {};
232
256
  if (!p.userIds.length)
233
257
  return out;
234
- const grants = await db
235
- .selectFrom(OTOY_GRANT_TABLE)
236
- .select(['id', 'user_id', 'amount_credits', 'otoy_created_at'])
237
- .where('user_id', 'in', p.userIds)
238
- .orderBy('otoy_created_at', 'asc')
239
- .orderBy('id', 'asc')
240
- .execute();
241
- if (!grants.length)
242
- return out;
243
- // EVERY grant-ledger row retires pool capacity, in either direction:
244
- // - negative delta = an emissions burn. ALL of them count, not just ones stamped with an
245
- // otoy_grant_id. Historical grants are now backfilled to cover the Sep/Oct-2025 burns, so those
246
- // burns must net against them; counting only stamped rows would re-issue ~1.06M credits of
247
- // already-spent grant.
248
- // - positive delta = the grant was CREDITED into current_user_grant_spend (the one-off canary
249
- // route). That balance reaches the split separately, so leaving it in the pool double-issues it.
250
- // Hence abs() over the whole ledger.
251
- // NOT abs() over the whole ledger — that would charge the Sep/Oct-2025 CSV *credits* as if they
252
- // were spend, double-penalising those users (464e8e33: 376,749 burned + 79,304 credited = 456,053).
253
- // A positive only retires capacity when it is tied to a pool grant (the canary route, where the
254
- // grant was moved into current_user_grant_spend and reaches the split from there).
255
- const spent = await db
256
- .selectFrom(USER_GRANT_SPEND_TABLE)
257
- .select((eb) => [
258
- 'user_id',
259
- eb.fn
260
- .sum(sql `case when render_spent_delta < 0 then -render_spent_delta
261
- when otoy_grant_id is not null then render_spent_delta
262
- else 0 end`)
263
- .as('net'),
264
- ])
265
- .where('user_id', 'in', p.userIds)
266
- .groupBy('user_id')
267
- .execute();
268
- // Fetch the raw event stream and fold in code (foldTimeAware — reservoir semantics; reversals
269
- // clamp capacity, only jobs create beyond-grants). SQL window identities can't express the clamp.
258
+ // Raw event stream, folded in code (foldGrantWindow — per-grant buckets, 60-day consumable window,
259
+ // reversals clamp capacity, only jobs create beyond-grants). SQL can't express the bucketed clamp.
270
260
  const evRes = await sql `
271
261
  select user_id::text as u, coalesce(otoy_created_at, ${GRANT_WINDOW_START}::timestamp) as t,
272
- (amount_credits * 4 * 1e8)::numeric::text as amt, 'grant' as src
262
+ (amount_credits * 4 * 1e8)::numeric::text as amt, 'grant' as src, id as gid
273
263
  from otoy_grant where user_id = any(${p.userIds}::uuid[])
274
264
  union all
275
- select user_id::text, ${GRANT_WINDOW_START}::timestamp, sum(render_spent_delta)::text, 'grant'
265
+ select user_id::text, ${GRANT_WINDOW_START}::timestamp, sum(render_spent_delta)::text, 'grant', null
276
266
  from user_grant_spend
277
267
  where user_id = any(${p.userIds}::uuid[]) and render_spent_delta > 0 and otoy_grant_id is null
278
268
  group by user_id
279
269
  union all
280
- select user_id::text, created_at, (-render_spent_delta)::text, 'grant'
270
+ select user_id::text, created_at, (-render_spent_delta)::text, 'grant', null
281
271
  from user_grant_spend
282
272
  where user_id = any(${p.userIds}::uuid[]) and render_spent_delta > 0 and otoy_grant_id is not null
283
273
  union all
284
- select user_id, completed_at, (-render_amt)::text, 'job'
274
+ select user_id, completed_at, (-render_amt)::text, 'job', null
285
275
  from job
286
276
  where user_id = any(${p.userIds}::text[]) and completed_at >= ${GRANT_WINDOW_START}::timestamp
287
277
  `.execute(db); // db is always a Kysely trx at runtime
288
278
  const byUser = new Map();
289
279
  for (const r of evRes.rows) {
290
280
  const arr = byUser.get(r.u) ?? [];
291
- arr.push({ t: new Date(r.t).getTime(), amt: BigInt(Math.round(Number(r.amt))), src: r.src });
281
+ arr.push({
282
+ t: new Date(r.t).getTime(),
283
+ amt: BigInt(Math.round(Number(r.amt))),
284
+ src: r.src,
285
+ grantId: r.gid ?? undefined,
286
+ });
292
287
  byUser.set(r.u, arr);
293
288
  }
294
289
  for (const [u, events] of byUser) {
295
- const cur = out[u] ??
296
- (out[u] = { userId: u, granted: BigInt(0), consumed: BigInt(0), avail: BigInt(0), grants: [] });
297
- const f = foldTimeAware(events);
298
- cur.granted = events.reduce((a, e) => (e.src === 'grant' && e.amt > BigInt(0) ? a + e.amt : a), BigInt(0));
299
- cur.consumed = f.jobCharged;
300
- cur.avail = f.avail;
290
+ const f = foldGrantWindow(events);
291
+ out[u] = {
292
+ userId: u,
293
+ granted: events.reduce((a, e) => (e.src === 'grant' && e.amt > BigInt(0) ? a + e.amt : a), BigInt(0)),
294
+ consumed: f.jobCharged,
295
+ avail: f.avail,
296
+ expired: f.expired,
297
+ fifoGrantId: f.fifoGrantId,
298
+ };
301
299
  if (f.beyondGrants > BigInt(0)) {
302
- log.info(`grant allotment ${u}: ${f.beyondGrants} beyond grants (purchase-paid at the time) avail ${f.avail}`);
300
+ log.info(`grant allotment ${u}: ${f.beyondGrants} beyond grants (purchase-paid at the time), ` +
301
+ `${f.expired} expired past the ${GRANT_LOOKBACK_DAYS}d window — avail ${f.avail}`);
303
302
  }
304
303
  }
305
304
  return out;
@@ -1153,16 +1152,13 @@ export const pgClient = (config) => {
1153
1152
  return;
1154
1153
  }
1155
1154
  }
1156
- // FIFO-attribute this spend to the user's oldest OTOY grant that still has room. Stamping
1157
- // otoy_grant_id is what makes getGrantAllotments count it as consumed an unattributed row
1158
- // would leave the grant looking unspent and let it fund a second burn.
1155
+ // FIFO-attribute this spend to the user's oldest LIVE OTOY grant that still has room (the fold
1156
+ // already applies the lookback window). Stamping otoy_grant_id is what shows the draw in the
1157
+ // admin drill-down — an unattributed row would leave the grant looking unspent there.
1159
1158
  let otoyGrantId = p.otoyGrantId;
1160
1159
  if (otoyGrantId === undefined && p.renderSpentDelta < BigInt(0)) {
1161
1160
  const allots = await getGrantAllotments(db.trx, { userIds: [p.userId] }, log);
1162
- const a = allots[p.userId];
1163
- if (a) {
1164
- otoyGrantId = foldGrantAllotment(a.grants, a.consumed, BigInt(0)).fifoGrantId;
1165
- }
1161
+ otoyGrantId = allots[p.userId]?.fifoGrantId;
1166
1162
  }
1167
1163
  const v = {
1168
1164
  user_id: p.userId,