This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
alexpaynex 1754ab1dbc Task #27: Complete cost-routing + free-tier gate — all critical fixes applied
Fix 1 — Add `estimateRequestCost(request, model)` to PricingService (pricing.ts)
  - Unified: 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 — 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 already with infra+margin
    (calculateWorkFeeUsd), convert directly to sats — no second calculateActualChargeUsd
  - absorbedSats capped at actual cost post-execution to prevent over-absorption

Fix 4 — Correct isFree derivation for partial jobs in advanceJob() (jobs.ts)
  - isFreeExecution = workAmountSats === 0 (not job.freeTier)
  - Partial jobs (freeTier=true, workAmountSats>0) run the paid accounting path:
    actual sats, refund eligibility, pool credit, and deferred grant recording

Fix 5 — Atomic pool deduction + daily absorption in recordGrant (free-tier.ts)
  - Pool: SQL GREATEST(value::int - N, 0)::text inside transaction, RETURNING actual value
  - Daily absorption: SQL CASE expression checks absorbed_reset_at age in DB
    → reset counter on new day, increment atomically otherwise
  - No more application-layer read-modify-write for either counter

Fix 6 — Remove fire-and-forget from all recordGrant() call sites
  - Removed `void` prefix from all three call sites (jobs.ts x2, sessions.ts x1)
  - Grant persistence failures now propagate correctly instead of silently diverging
  - Removed unused createHash import from free-tier.ts
2026-03-19 16:55:03 +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 — Lightning-gated AI agent (TypeScript/Express)
30 MiB
Languages
TypeScript 68.2%
JavaScript 14.8%
Shell 12.4%
HTML 4.1%
CSS 0.4%