alexpaynex d899503f5d Task #27: Apply all required fixes for cost-routing + free-tier gate
Fix 1 — Add `estimateRequestCost(request, model)` to PricingService (pricing.ts)
  - Unified method: estimateInputTokens + estimateOutputTokens + calculateWorkFeeUsd
  - Replaces duplicated estimation logic in jobs.ts, sessions.ts, estimate.ts

Fix 2 — Move partial free-tier `recordGrant()` from invoice creation to post-work
  - Was called at invoice creation for partial path — economic DoS vulnerability
  - Now deferred to runWorkInBackground via new `partialAbsorbSats` param
  - Fully-free jobs still record grant at eval time (no payment involved)

Fix 3 — Sessions pre-gate: estimate → decide → execute → reconcile
  - freeTierService.decide() now runs on ESTIMATED cost BEFORE executeWork()
  - Fixed double-margin bug: estimateRequestCost returns cost with infra+margin already
    applied (calculateWorkFeeUsd), so convert directly to sats — no second
    calculateActualChargeUsd wrapping
  - absorbedSats capped at actual cost post-execution to prevent over-absorption

Fix 4 — Correct isFree flag for partial jobs in advanceJob() (jobs.ts)
  - job.freeTier=true for BOTH fully-free and partial jobs
  - isFreeExecution now derived from workAmountSats===0 (user paid nothing)
  - Partial jobs (freeTier=true, workAmountSats>0) run the paid accounting path:
    actualAmountSats, refundState, pool credit, and deferred grant recording

Fix 5 — Atomic pool deduction in recordGrant (free-tier.ts)
  - Replaced non-atomic read-then-write with SQL GREATEST expression inside tx
  - UPDATE timmyConfig SET value = GREATEST(value::int - N, 0)::text RETURNING value
  - Audit log receives actual DB-returned value; no oversubscription under concurrency
  - Removed unused createHash import
2026-03-19 16:50:48 +00:00
2026-03-13 23:21:55 +00:00
2026-03-13 23:21:55 +00:00
2026-03-13 23:21:55 +00:00
2026-03-13 23:21:55 +00:00
Description
Timmy Tower World — sovereign AI agent economy
226 MiB
Languages
TypeScript 68.9%
JavaScript 15.5%
Shell 10.6%
HTML 4.5%
CSS 0.4%