LibreChat/api/server/services
Danny Avila 696bcb6c1e
🏦 fix: Reserve Credits for In-Flight Requests (#15879)
* 💳 fix: Reserve Balance for In-Flight Requests

Admit each balance-checked request against the credits that other
in-flight requests have not already reserved, and hold its prompt cost
on the balance record until the turn settles. Apply a due auto-refill in
the same fenced write so one refill window credits exactly once.

- data-schemas: reserveBalance / releaseBalanceReservation replace
  createAutoRefillTransaction; the write is a compare-and-swap on
  tokenCredits, reservations and lastRefill (no pipeline updates)
- balance.reservations is select:false so existing reads never see it
- checkBalance returns a reservation handle; BaseClient and the
  assistants controllers release it once the turn settles
- balance.reservationTtlMs bounds how long an unreleased reservation
  counts (default 30 minutes)

* 🧮 refactor: Admit Reservations Without Contention and Record Refills Durably

- admission is a conditional $push/$inc against a reservedCredits total,
  so concurrent admissions of a funded balance commit on their first write
- expired reservations are pruned per element with their credits
- auto-refill is fenced on every setting its decision reads and leaves a
  pendingRefill ledger marker in the same write; any later read records the
  transaction under the fixed id and clears the marker
- a missing record is created with $setOnInsert inside reserveBalance
- reservation lifecycle moves to packages/api (createBalanceReservations,
  withBalanceReservations); BaseClient releases once usage is recorded

* 🪪 fix: Resolve Each User to One Balance Record

- balance records a creator inserts are keyed by the user id, so creators
  racing on a missing record converge on one document; every balance read
  and write resolves a user to their oldest record, so existing duplicates
  stay inert
- upsertBalanceFields takes insert-only fields; the balance-config
  middleware, schedules and lazy initialization pass the starting credit
  through them so it never overwrites a record another writer created
- the auto-refill write is also fenced on reservedCredits
- the scheduled-chat balance pre-skip counts reserved credits as unavailable

* ⏳ fix: Count Only Unexpired Reservations in Scheduled Balance Checks

findBalanceByUser with includeReservedCredits now totals the reservations
that have not expired, so a reservation left by a crashed request stops
pre-skipping scheduled chats at its expiry instead of when a later
reservation write prunes it.

* 🔁 fix: Renew Balance Reservations While Their Request Runs

A reservation is renewed every half TTL until it is released, so only a
reservation whose process stopped expires. Pruning removes a reservation
only while it is still expired at the write, so one renewed after the
pruning read survives.

* 🛟 fix: Keep Reservations Alive Through Renewal Failures and Lost Acks

- renewal reschedules itself: every half TTL, capped to the timer range,
  and a failed renewal retries within a tenth of that window
- an admission write that errors removes its possibly committed
  reservation before rethrowing

* 🛑 fix: Hold a Stopped Turn's Reservation Until Its Charge Lands

- an aborted turn lets its reservations lapse after a minute (or the TTL)
  instead of releasing them, so the Stop request's charge lands before the
  credits are admitted again
- balance.reservationTtlMs has a 10 second minimum, enforced in the schema
  and at runtime, so renewal stays bounded

* ↩️ revert: Release a Stopped Turn's Reservation When the Turn Settles

An aborted turn releases its reservations when it settles, the same as
every other exit, instead of holding them for a fixed lapse window that
refused funded requests after every Stop. The minimum reservation TTL
stays.

* ⏸️ fix: Hold Assistants Reservations Through Background Run Retries

An in-progress Assistants run continues in the background after the
handler responds; the turn's reservations now stay held until that run
settles (createBalanceReservations().holdUntil).

* 🔁 fix: Apply an Auto-Refill Once per Admission

A refill interval that is due again the moment it is applied, such as a zero
interval, let one admission refill repeatedly until the retry bound. Each
admission now applies at most one refill, as the previous check did.

* 🧹 fix: Prune Expired Reservations in One Write

An admission that found expired reservations issued one write per hold, so a
backlog left by a stopped process fanned out into as many concurrent writes.
The prune is now a single write fenced on every hold it removes; a hold renewed
after the read makes the write miss and the admission re-read.

* ⏱️ fix: Renew Reservations From Their Stored Expiry

The next renewal was scheduled half a TTL after the previous write settled,
and the first one half a TTL after the admission returned, so a slow write
could let a live hold expire before its renewal. Renewals are now scheduled
from the expiry the last write stored.

Expired holds are pruned as one bulk write of per-hold fenced updates, so a
hold that changed after the read is skipped while the rest are removed, and
the admission no longer spends a retry on the whole prune.
2026-09-13 12:53:19 -04:00
..
__tests__ 🔎 fix: Enforce WEB_SEARCH Role Permission on Agents (#15875) 2026-09-12 23:00:10 -04:00
Agents 🪬 fix: Fence Agent Checkpoint Ownership and Cleanup Recovery (#15680) 2026-09-07 22:04:59 -04:00
Artifacts 🧵 fix: Preserve Fenced Markdown Artifacts (#14121) 2026-07-05 12:04:59 -04:00
Config 🔐 fix: Stop an MCP Credential Refresh From Fencing Its Own Connection (#15863) 2026-09-12 12:39:47 -04:00
Endpoints 🔎 fix: Enforce WEB_SEARCH Role Permission on Agents (#15875) 2026-09-12 23:00:10 -04:00
Files 🦘 feat: Share Run-Scoped Files With Subagents (#15848) 2026-09-12 20:14:22 -04:00
MCP 🫧 fix: Retire Cancelled MCP OAuth State (#15702) 2026-09-07 15:43:02 -04:00
Runs 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
Schedules 🏦 fix: Reserve Credits for In-Flight Requests (#15879) 2026-09-13 12:53:19 -04:00
Skills 📜 feat: Add Skills Discovery and Authoring to the Management API (#15642) 2026-09-06 17:26:26 -04:00
start 🧫 chore: Keep Jest Specs Out of Tool Discovery (#15766) 2026-09-08 23:58:04 -04:00
Threads ⏳ fix: Separate Temporary and General Chat Retention (#15734) 2026-09-08 11:22:54 -04:00
Tools 📇 fix: Stop an MCP Credential Refresh From Fencing Its Own Catalog Recovery (#15865) 2026-09-12 19:50:28 -04:00
ActionService.js 🔣 refactor: Replace Mongo Filters With Typed Criteria (#15580) 2026-09-04 11:54:21 -04:00
ActionService.spec.js 🛑 fix: Forward Run Abort Signal to Foreground Tool Calls (#15517) 2026-09-02 13:32:33 -04:00
AssistantService.js 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
AuthService.js 🧲 fix: Match Directory Users to Existing Principals (#15855) 2026-09-12 07:36:22 -04:00
AuthService.spec.js 🧲 fix: Match Directory Users to Existing Principals (#15855) 2026-09-12 07:36:22 -04:00
cleanup.js
createRunBody.js 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
createRunBody.spec.js 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
GraphApiService.js 🕰️ fix: Guard expires_in So a Token Response Cannot Outlive Its Credential (#15321) 2026-08-28 10:47:09 -04:00
GraphApiService.spec.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
GraphTokenService.js
initializeMCPs.js 🪂 fix: Release Stalled MCP Catalog Recovery Without Dropping Refreshed Tokens (#15871) 2026-09-12 22:23:49 -04:00
initializeMCPs.plugins.spec.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
initializeMCPs.spec.js 🪂 fix: Release Stalled MCP Catalog Recovery Without Dropping Refreshed Tokens (#15871) 2026-09-12 22:23:49 -04:00
initializeOAuthReconnectManager.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
initializeOAuthReconnectManager.spec.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
MCP.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
MCP.spec.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
MCPAuthorizationFenceRetry.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
MCPAuthorizationFenceRetry.spec.js 🛂 fix: Back Off MCP OAuth Discovery (#15783) 2026-09-09 22:21:34 -04:00
MCPRequestContext.js 🪢 fix: Tie MCP Cleanup To Resumable Runs (#13769) 2026-06-15 15:26:03 -04:00
OboPolicyService.js
OboTokenService.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
OboTokenService.spec.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
OpenIDRefreshFlight.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
OpenIDRefreshFlight.spec.js 🪫 fix: Recharge Direct OpenID MCP Bearer Sessions (#15704) 2026-09-07 21:05:26 -04:00
OpenIDRefreshRecovery.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
OpenIDSessionRefresh.js 🪫 fix: Recharge Direct OpenID MCP Bearer Sessions (#15704) 2026-09-07 21:05:26 -04:00
OpenIDSessionRefresh.spec.js 🪫 fix: Recharge Direct OpenID MCP Bearer Sessions (#15704) 2026-09-07 21:05:26 -04:00
PermissionService.js 🧲 fix: Match Directory Users to Existing Principals (#15855) 2026-09-12 07:36:22 -04:00
PermissionService.spec.js 🧲 fix: Match Directory Users to Existing Principals (#15855) 2026-09-12 07:36:22 -04:00
PluginService.js 🕸️ feat: Complete Keenable Web Search Provider (#15288) 2026-08-27 14:29:20 -04:00
PluginService.spec.js 🕸️ feat: Complete Keenable Web Search Provider (#15288) 2026-08-27 14:29:20 -04:00
RefreshTokenBridge.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
RefreshTokenBridge.spec.js 🔒 fix: Refresh MCP OBO Tokens From the Live OpenID Session (#15334) 2026-08-29 23:55:58 -04:00
systemGrant.spec.js
ToolService.js 🔎 fix: Enforce WEB_SEARCH Role Permission on Agents (#15875) 2026-09-12 23:00:10 -04:00
twoFactorService.js 🥁 fix: Compare TOTP Codes in Constant Time (#15157) 2026-08-24 09:10:03 -04:00
twoFactorService.spec.js 🥁 fix: Compare TOTP Codes in Constant Time (#15157) 2026-08-24 09:10:03 -04:00