LibreChat/api/server/controllers/agents/errors.spec.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

42 lines
1.7 KiB
JavaScript

const { ViolationTypes } = require('librechat-data-provider');
const { classifyScheduleOutcome, isBalanceViolationError } = require('./errors');
/**
* Both agent clients SWALLOW a failed generation into an error content part instead of
* throwing, so the interactive UX can finalize with the error visible. That left the
* scheduled bookkeeping recording `success` for runs that produced nothing but an
* error: the consecutive-failure streak reset every time, `autoDisableAfterFailures`
* never tripped, and a schedule failing on every provider call retried on its cadence
* forever.
*/
describe('classifyScheduleOutcome', () => {
const balanceError = new Error(
JSON.stringify({ type: ViolationTypes.TOKEN_BALANCE, balance: 0, tokenCost: 10 }),
);
it('records success when the run swallowed nothing', () => {
expect(classifyScheduleOutcome(null, 'fallback')).toEqual({ status: 'success' });
expect(classifyScheduleOutcome(undefined, 'fallback')).toEqual({ status: 'success' });
});
it('routes a balance refusal to the insufficient_balance streak, not the failure streak', () => {
expect(isBalanceViolationError(balanceError)).toBe(true);
expect(classifyScheduleOutcome(balanceError, 'fallback')).toEqual({
status: 'skipped_balance',
});
});
it('records every other swallowed failure as an error, carrying its message', () => {
expect(classifyScheduleOutcome(new Error('upstream 503'), 'fallback')).toEqual({
status: 'error',
error: 'upstream 503',
});
});
it('falls back when the error carries no message', () => {
expect(classifyScheduleOutcome({}, 'Run ended in error')).toEqual({
status: 'error',
error: 'Run ended in error',
});
});
});