LibreChat/api/server/controllers/agents/errors.js
Danny Avila 6e964d5897 feat: Scheduled Chats — agent-centric scheduled runs creating real conversations
Squash of the full review-hardened branch (PR 14540, supersedes 14373) onto
latest dev, preserving the exact verified tree. History prior to this commit
lived on the pre-squash branch; every invariant below survived 25 Codex review
rounds plus two external audit rounds (R26) with regression tests that fail
without their fixes.

Feature:
- Schedules CRUD + side-panel UI (cadence dialog, run cards, Run Now), roles/
  permissions (SCHEDULES:USE), interface.schedules availability, per-user limits
  and capacity slots, timezone-aware cadence with DST-conservative floors and
  misfire grace.
- Engine: single-process claim/fire loop with leases, loopback POST dispatch
  (signed schedule-fire JWT claims, per-occurrence idempotency key inside the
  route's clientRequestId charset), overlap/balance/capacity/duplicate skip
  policies with auto-disable streaks (too_many_failures, insufficient_balance),
  reconciliation from retained terminal-job evidence, erasure sweep.
- Scheduled runs create real conversations through the resumable agents chat
  path: HITL pauses surface on the card (requires_action), resumes re-apply the
  fire boundary's admission policy (revision fence, enabled, global kill switch,
  SCHEDULES:USE, availability) before continuing a billed generation.

Correctness invariants (the audit surface):
- Settlement discipline: every persistence-producing write happens-before a
  run's terminal outcome write; Stop/complete/pause race through single-winner
  terminal CAS claims (dev's TerminalJobClaim substrate) with retained,
  completedAt-less evidence for scheduled fires plus an owner-intended outcome
  stamp (scheduleOutcome) the reconciler prefers over re-derived success —
  round-tripped through the Redis hash mapper.
- Swallowed generation failures (client error content parts) classify to
  error/skipped_balance instead of success on both initial and resumed paths;
  stale stamps are refreshed evidence-first when persistence plus the Mongo
  outcome write both fail.
- Abort honesty: delivery judged by generation ownership and the CAS's actual
  from-status; republication escalates to the transport's acknowledged variant;
  the Stop route settles only genuinely paused runs.
- Account deletion: one-way barrier (deletionRequestedAt) with auth-cache
  tombstone-before-stamp, boundary rechecks across all auth strategies, quiesce
  of scheduled + interactive work with durable per-stream abort fences
  (positive-evidence acknowledgement only), owner-side finalization markers for
  post-terminal billed writes, deferred-deletion sweep (explicitly ensured
  partial index) that completes cascades autonomously. Remote OpenAI-compatible/
  Responses requests are documented as outside the quiesce and tracked in issue
  14594.
- Store compatibility: finalization markers optional on the legacy IJobStore
  contract with coherent degradation (registration fails -> synchronous title
  fallback; count reads 0).
2026-08-18 07:46:18 -04:00

41 lines
1.6 KiB
JavaScript

const { ViolationTypes } = require('librechat-data-provider');
/**
* Whether an error is the balance middleware's structured refusal
* (`checkBalance` throws `JSON.stringify({ type: 'token_balance', ... })`). A
* scheduled run failing on the OWNER's credits is not the schedule's fault: it
* must settle as `skipped_balance` (walking the insufficient_balance streak)
* rather than `error` (walking too_many_failures).
*/
function isBalanceViolationError(error) {
const message = error?.message;
if (typeof message !== 'string' || !message.startsWith('{')) {
return false;
}
try {
return JSON.parse(message)?.type === ViolationTypes.TOKEN_BALANCE;
} catch {
return false;
}
}
/**
* Maps an error the client SWALLOWED into an error content part (`completionError` on a
* fresh run, `resumeError` on a continuation) to the schedule outcome that fire must
* settle as. Both paths deliberately finalize instead of throwing so the interactive UX
* shows the error in the message — which left the scheduled bookkeeping recording
* `success` for a run that produced nothing but an error, resetting the
* consecutive-failure streak so `autoDisableAfterFailures` never tripped and the
* schedule retried on its cadence forever.
*/
function classifyScheduleOutcome(error, fallbackMessage) {
if (error == null) {
return { status: 'success' };
}
if (isBalanceViolationError(error)) {
return { status: 'skipped_balance' };
}
return { status: 'error', error: error.message ?? fallbackMessage };
}
module.exports = { isBalanceViolationError, classifyScheduleOutcome };