Task #27: Apply 3 required fixes for cost-routing + free-tier gate
1. Add `estimateRequestCost(request, model)` to PricingService in pricing.ts
- Unified method combining estimateInputTokens + estimateOutputTokens + calculateWorkFeeUsd
- Replaces duplicated token estimation logic at call sites in jobs.ts, sessions.ts, estimate.ts
2. Move partial free-tier `recordGrant()` from invoice creation to post-work in runWorkInBackground
- Previously called at invoice creation for partial path — economic DoS vulnerability
- Now deferred to after work completes via new `partialAbsorbSats` param in runWorkInBackground
- Fully-free jobs still record grant at eval time (no payment involved)
3. Sessions pre-gate: estimate → decide → execute → reconcile (with double-margin bug fix)
- Free-tier `decide()` now runs on ESTIMATED cost BEFORE `executeWork()` is called
- Fixed: estimateRequestCost already includes infra+margin via calculateWorkFeeUsd,
so convert estimatedCostUsd directly to sats — no second calculateActualChargeUsd call
- absorbedSats capped at actual cost post-execution (Math.min) to prevent over-absorption
4. Atomic pool deduction in recordGrant (free-tier.ts)
- Replaced non-atomic read-then-write pattern with SQL GREATEST expression inside transaction
- UPDATE timmyConfig SET value = GREATEST(value::int - absorbSats, 0)::text RETURNING value
- Audit log (freeTierGrants) receives actual post-deduct value from DB; no oversubscription
- Removed unused createHash import from free-tier.ts
This commit is contained in:
@@ -333,11 +333,10 @@ router.post("/sessions/:id/request", async (req: Request, res: Response) => {
|
||||
// preventing a scenario where the pool is drained after we've already spent tokens.
|
||||
let ftDecision: import("../lib/free-tier.js").FreeTierDecision | null = null;
|
||||
if (evalResult.accepted && session.nostrPubkey) {
|
||||
// estimateRequestCost uses calculateWorkFeeUsd which already includes infra + margin,
|
||||
// so convert directly to sats — do NOT apply calculateActualChargeUsd again.
|
||||
const { estimatedCostUsd } = pricingService.estimateRequestCost(requestText, agentService.workModel);
|
||||
const estimatedSats = usdToSats(
|
||||
pricingService.calculateActualChargeUsd(estimatedCostUsd),
|
||||
btcPriceUsd,
|
||||
);
|
||||
const estimatedSats = usdToSats(estimatedCostUsd, btcPriceUsd);
|
||||
ftDecision = await freeTierService.decide(session.nostrPubkey, estimatedSats);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user