LibreChat/packages/data-schemas/misc/documentdb/sweep.documentdb.spec.ts
Marco Beretta 2be35b2f91 💬 refactor: ChatForm UI redesign with unified palette (#14473)
* 💬 refactor: ChatForm UI redesign with unified palette

Replace the scattered composer controls (badge row, tools dropdown, attach
menu, floating steer chips) with one system: a searchable palette with
favourites, a composer bar holding active tools plus effort and speech
controls, a tray staging files and manual context, and a queue rail that
buffers messages while a reply is running.

Collapses the 94 development and review-fix commits of PR #14473 into one
commit so the branch could be rebased onto dev once; the upstream merge of
this pull request is a squash merge either way. The pre-rebase history is
kept locally at tag prerebase-backup (afde3e4b9a).

* fix: finish the dev rebase of the composer redesign

The rebase onto dev left four defects the merge itself produced, plus one
review finding the branch had never answered:

- ChatForm's `min-w-0` JSX comment lost its closing brace, so the file did
  not parse at all.
- `useSteering` carried both dev's local draft consumer and the host-injected
  one under the same name; the host's `consumeDraft` owns the storage key
  autosave actually parked on, so the local copy is gone.
- `useUploadOptions` kept dev's `awaitingAgentProvider` gate after the merge
  replaced the block that derived `isSavedAgent`; the predicate is back, from
  the same `isEphemeralAgentId` the shared allowance helper uses.
- `MessagesView` still reserved a band for the in-flight steer overlay and
  read `steerOverlayHeightFamily`, which nothing writes now that pending
  steers render inside the streaming reply. The reservation is gone and
  `ScrollButton`'s lift is optional; composer panels keep standing the button
  down through `composerOverlayCountFamily`.
- The aggregate MCP chip keyed on `mcp:pinned`, which a server literally
  named `pinned` also produces (`mcp:${serverName}`), so both entries shared
  one React key and one packing slot. It uses a key outside that namespace.

Also drops the locale keys whose only consumers the redesign deleted, ports
dev's extracted-text delivery path into `useAttachExisting` (every
attach-existing surface routes through it now), repoints the mock e2e
built-in helpers at the palette, and repairs test fixtures the merge left
stale (a query mock that blanked agent metadata, a Jotai store mismatch, an
ambiguous tray query, an ephemeral-agent fixture standing in for a saved one).

* test: cover the composer redesign's acceptance scenarios end to end

Twelve mock-harness scenarios, one `@scenario:` tagged test each, over the
behaviours this change is accountable for: the single searchable palette
(open, search, favourite across a reload, keyboard-only operation), the
composer bar and tray (active tool chip, a staged file riding with the
message, a per-turn thinking level that applies to the next turn only), the
queue rail (Enter buffers during a run and drains after it, keyboard reorder
changes the send order, the draft survives run completion), and the composer's
portaled surfaces (Stop reaches the run from inside a portal, a pending tool
approval in the bar resumes the run).

Two contract scenarios are deliberately absent, with reasons: split-pane Stop
isolation is not runtime-reachable because `ChatRoute` mounts one pane, and
code-approval mode mid-run needs an attached stateful code environment the
mock lab does not configure, so dev's `ChatForm.approvalMode` unit spec stays
its coverage.

* fix: make the composer's acceptance run and the whole client suite green

The first verification run of the twelve scenarios passed six and failed six,
and the full client jest suite (which CI runs sharded, unlike the diff-scoped
local precheck) failed fourteen tests. One of each group was a real defect; the
rest were queries and fixtures still aimed at surfaces the redesign replaced.

Defects:

- `useQueueDrain` held its send lock after a revealed queued turn settled, so
  the next server-owned row could never be selected. The reveal path now owns
  its own lock and releases it when the revealed turn clears.
- `ChatForm` recomputed the during-run slot without `consumeComposer` in its
  dependency list, and still carried a landing flag the host prop replaced.

Tests and lab:

- The five `ChatForm.attachments` focus cases now drive the palette dialog and
  the tray's quote chips instead of the deleted attach menu and quote popup,
  with the same strict focus assertions; the harness gains the `main` landmark
  the palette portals into, and the touch fixture a complete `matchMedia`.
- `Palette.spec` no longer closes a hoisted `jest.mock` factory over a later
  `const`; `useSteering.spec` follows dev's toast wording.
- The six failing scenarios were repointed at what this lab actually renders:
  uploads go through the palette's local-computer row, the reasoning control
  needs `interface.parameters`, the queued-row contract is the row and its
  drain rather than a control the submit consumes, the palette's keyboard path
  uses a built-in the lab grants, the slow-reply wait uses the fixture's chunk
  marker, and the approval scenario selects its agent through the builder flow
  the passing approval specs use.

* fix: close the eight findings Codex raised on the rebased head

Codex reviewed the rebased heads and reported eight issues; all eight were
real. Two of them concern code this session added.

Behaviour:

- A custom or OpenRouter endpoint that declares neither `paramDefinitions`
  nor a `reasoningFormat` no longer gets a reasoning control inferred from the
  generic settings list. Capability now has to be declared (a reasoning-key
  parameter definition, a non-disabled `reasoningFormat`, or a known reasoning
  provider as the default params endpoint); a `disabled` format always
  resolves nothing, and every consumer — the composer control, the pre-send
  validation and the server — asks the same resolver, so a gateway backed by a
  non-reasoning model can no longer offer a level the turn would fail on.
- Restoring a queued attachment keeps its `llmDeliveryPath`, so an extracted
  document is routed as its text rather than as raw provider content.
- The placeholder-to-durable reasoning migration now requires this pane's own
  live submission, so opening another conversation from a new chat no longer
  carries the pending level into it. The created/sync case it was written for
  still carries.
- The queue rail treats an acknowledged server-owned row as an ordering
  boundary: local rows still reorder among themselves, a server-owned row
  cannot move, and a move that would cross one is refused out loud — the
  queued-turn API has enqueue, list and cancel only, so a client-side reorder
  across that boundary could not be made authoritative.
- The palette resolves an agent's skill scope with `resolveAgentSkillsScope`
  instead of treating an empty list as an open catalog, and the attach target
  resolves `useResponsesApi` with `resolveUseResponsesApi`, matching what the
  run will actually do.

Policy:

- The reasoning-override behaviour moved out of the CJS middleware into
  `packages/api/src/modelSpecs/reasoningOverride.ts`, which returns a
  discriminated result; the middleware is wiring again, and the behavioural
  cases moved with it.
- `showComposerTips` and `showFilesDialog` are Jotai atoms now, the tips
  preference keeping its storage key and reaching the composer as a prop.

* fix: make the redesigned composer reachable from the mock e2e helpers

CI's Playwright shard failed four `file-provisioning` tests on this branch
because the shared upload helpers still clicked `#attach-file-menu-button` and
`#attach-file-button`, entry points the redesign removed — attaching now goes
through the palette, which also owns the file input. Both helpers drive the
palette now, `uploadViaLegacyOption` mapping its two destination names onto the
palette rows that route to the code environment and the vector store, and the
specs that drove the old surfaces directly (`code-upload`, `thread-renderers`,
`unified-upload`) follow.

Also closes the last client/server disagreement the acceptance run exposed: a
custom endpoint that declares a reasoning key in `paramDefinitions` had that
declaration merged into the provider defaults and then discarded by model-name
inference, so the composer offered a level for `mock-model-a` that the server
rejected with "Invalid reasoning override". An explicitly declared key now
selects the merged setting before inference, while an undeclared generic custom
or OpenRouter target still resolves nothing.

The twelfth acceptance scenario is rescoped: a paused approval keeps the
composer usable and resumes to completion, but the composer-hosted approval
panel and chip are not asserted, because the pending action is keyed by the
stream's conversation id while the composer stays bound to the new-chat
sentinel — in code byte-identical to dev, and mirroring the write onto the
sentinel did not surface the panel. Tracked at berry-13/LibreChat#39.

* test: finish the acceptance run and keep the client typecheck green

- The thinking scenario read `reasoningOverride` from a `userMessage` wrapper
  the chat request does not have; the trace shows the request carrying
  `{ key: 'effort', value: 'max' }` at the root, so the assertion reads it
  there. Source behaviour was already correct.
- The approval scenario timed out on mobile because the Agent Builder drawer
  stayed open after selecting the agent and swallowed the pointer events meant
  for the composer; it now closes the sidebar and waits for the drawer to go
  inert before sending.
- Two specs typed a named custom endpoint's label as an `EModelEndpoint`, which
  CI's client typecheck rejects (the label is exactly what the case is about).

All twelve acceptance scenarios now pass in desktop light, desktop dark and
mobile emulation.

* fix: close the nine findings Codex raised on the verified head

Two were defects in code this session added while closing earlier findings:

- A reasoning key declared in `paramDefinitions` but absent from the generic
  settings list was accepted as capability and then never inserted, so the
  resolver exposed and validated the generic `reasoning_effort` instead. A
  declared key is now appended to the effective settings and resolved before
  any fallback, for `effort`, `thinkingLevel` and `thinkingBudget` alike.
- The queue's reorder guard validated from the destination row's own index, so
  `target === index` rejected every pointer drag and only keyboard reordering
  worked. It validates the dragged row's identity now; the server-owned
  boundary, its announcement and the disabled grips are unchanged.

The rest:

- Stop no longer falls through to the document-wide control once a focused
  pane has been resolved: focus in an idle pane while another generates used
  to abort the other pane's run.
- `PendingSteers` is mounted once in `MessagesView`, above the renderer
  switch, so a sent or failed steer keeps its in-flight, retry and
  send-as-new surface with `FLAT_THREAD` off, where the recursive renderer
  had none and the composer no longer carries a fallback.
- The file-list policy moved out of the CJS route into
  `packages/api/src/files/list.ts`, and its cap is `interface`-level config
  (`fileListLimit`, default 100) instead of a literal in the route.
- The steer-arm confirmation window is `interface.steerArmConfirmationTimeoutMs`
  with its current 10s as the default, travelling the same path as the other
  client-consumed timings.
- The pause predicates judge the active branch's tail rather than the last
  assistant in array order, so an inactive sibling branch can no longer
  enable or disable the inline escalation control.

* fix: move persisted reasoning validation into api package

* fix: restore composer entry points and defaults

* style: normalize reasoning helper formatting

* test: open mobile sidebar before account menu

* test: use settled sidebar layout for mobile account menu

* test: handle empty mobile sidebar in account scenario

* test: target account menu in mobile drawer

* fix: keep composer surfaces and mobile drawer reachable

* fix: let thread approvals receive pointer events

* fix: stabilize composer steering and shortcuts

* fix: commit queue drags and centralize file modal

* fix: return a canceled steer's text to the composer

Canceling a pending steer dropped the words the user had written: the
bubble disappeared and nothing carried the text back, so a reclaim that
the server confirmed still lost the message.

Hand the reclaimed text to `pendingComposerTextByConvoId`, which the
composer already drains on its first render, and append rather than
overwrite when the composer holds a draft the user typed while the cancel
request was in flight.

* fix: close the six findings Codex raised on the pre-rebase head

Pending steers keyed off the Recoil conversation id, which lags the route
during warm-cache navigation, so the surface could sit at the destination
thread's tail while its Cancel and Escalate acted on the source run. It
now keys off the rendered tree, as the mount window already does.

Arming a steer removes the control that was activated, dropping keyboard
focus on the document body. `useSteerEscalate` already exposes `onArmed`
for exactly that; the bubble now passes one that moves focus to the row's
Cancel action.

Opening the file manager from the account menu left its dialog with no
opener: the modal rejects an active element inside a menu, and nothing
supplied a fallback, so closing it returned focus to the body. The opener
is published alongside the open flag and cleared on close, which keeps the
shortcut path's composer-focus behaviour intact.

A steer the server rejected was swept into the queue with no marking, and
the run-end drain then sent those words as a fresh turn without the user
touching Retry or Send as new. Converted failures now carry
`needsExplicitSend`, which the drain skips the same way it skips a row the
rail is mid-edit on.

The queued-turn reconciliation window was a hard-coded 60s, unreachable by
an operator behind a slow proxy or a delayed read replica. It ships as
`interface.queuedTurnReconciliationTimeoutMs` with that value as default,
beside the steer arm timeout it belongs with.

Reasoning-override shape validation moved to
`parseReasoningOverrideRequest` in packages/api, leaving the middleware to
map a rejection onto its own error response.

* fix: keep the composer's reasoning level in step with the parameters panel

`stableConversation` memoized the conversation without any reasoning field
in its key, so editing the level in the Parameters panel produced a new
conversation object that every listed dependency ignored. The composer
went on showing the old level while the next ordinary send already used
the new one.

* test: stop the sidebar helper waiting for a drawer desktop never renders

`ensureSidebarOnScreen` measured `#mobile-drawer` whenever the sidebar had
no Pinned region. Desktop renders no drawer, and `boundingBox()` on a
locator that matches nothing waits for it, so every scenario that reached
that branch at a desktop viewport timed out instead of continuing. Check
for the drawer's absence rather than measuring it.

* test: open the mobile drawer from its inert state, not its geometry

A closed drawer stays mounted and keeps its layout box, so measuring the
Pinned region reported it on screen while every control inside was still
untappable. `inert` is the state the component actually sets when it
closes, so the helper reads that and clicks the header opener before
anything else measures.

* fix: return a canceled steer whole, through one recovery boundary

A steer that stops being a steer was handed back three different ways,
and each one lost something. The immediate cancel wrote `steer.text`
into the composer handoff atom, dropping the attachments, quoted
excerpts, manual skill picks and reasoning override that `settleReclaimed`
then deleted with the pending object. The cancel that won its race
against its own POST cleared the pending marker and restored nothing at
all, so those words disappeared. A definitively rejected steer could
only be resent unchanged, because the row offering to edit it was gone.

Give the recovery one boundary. `useSteerRehome` hands the whole steer
to the composer through the guarded restore the queue rail already uses,
and converts it into an ordinary queued follow-up when the composer
refuses: a draft in the box, another chat on screen, a paused question,
or a composer that unmounted while the reclaim was in flight. The chip
goes either way, since a pending row claiming the server still holds the
words is a lie once they live somewhere else.

The composer is a sibling of the message list rather than its ancestor,
so the thread had no way to reach that restore. The pane's host now
holds the reference and both sides read it there, which also means a
reclaim resolving after the composer is gone refuses instead of writing
into a dead form.

Because the restore refuses rather than overwrites, a draft typed while
a cancel is in flight is no longer glued to the returned steer; the
steer becomes its own queued message and the draft is left as typed.

* test: drive the surfaces these scenarios actually reach

Four scenarios were failing against surfaces the user never touches.

The approval scenario clicked Approve on the thread's copy of the
pending card. The composer's review panel floats over the tail of the
thread and owns the batch submit, so that copy sits behind it and the
click retried until the test timed out. Drive the panel instead; the
thread card stays asserted as the record of the request.

The palette scenario counted rows the moment the dialog painted, then
expected clearing the filter to restore that number. The catalog arrives
in waves: the MCP server list is released by the app-wide warmup timer
and the skills catalog only starts fetching once the dialog's open
effect has run, so the count kept climbing under the test. Wait for the
last wave and for the count to hold still, then snapshot.

The sidebar helper opened the mobile drawer twice through two signals
that disagree. It waited for `inert` to clear, which happens on a state
commit several frames ahead of the slide, then fell through to a
bounding box still reading off-screen and clicked the opener again, into
a machine already opening. One open, through `inert`, then wait for the
drawer to settle in the viewport.

Splitting the cancel scenario follows the recovery change: an empty
composer takes the steer back, and a staged draft keeps the box while
the steer becomes a queued message of its own.

* fix: hold a server-rejected steer for an explicit send

Editing a definitively failed steer falls back to the queue when the
composer will not take it. Queued like any other follow-up, the run-end
drain would send it the moment the reply finished, which turns the
server's refusal into an automatic new turn carrying the payload it
refused.

Mark that fallback the way the rest of the rejected-steer path is
marked, so the row waits for the user to send it.

* test: restore two assertions this branch broke while rewriting specs

Both specs were updated for the redesigned surfaces and both lost a step
they needed, and both fail deterministically in CI as a result.

thread-fold pressed Enter mid-run and waited for a queued row, under a
comment claiming Enter queues by default. It does not: the default
during-run action is steer, so Enter produced a pending steer and the
queued row the next line waits for could never appear. Ctrl/Cmd+Enter is
what takes the other action, which is the one this test needs.

mcp-oauth-readiness asserted on the server card's Cancel button straight
after clicking Connect. Connect opens the OAuth dialog over the panel,
and it is modal, so while it is open the card underneath is marked
aria-hidden and Cancel is not in the accessibility tree at all, which is
why the failure reads "element(s) not found" rather than "not visible".
The rewrite dropped the dismissal the previous flow performed; restore it
as a helper both call sites share.

* test: reach the account menu without the pinned section

The two account-menu tests borrowed `ensureSidebarOnScreen`, which also
settles the Pinned section's placement for the pinned specs. They never
touch that region, and on a run where an earlier test left a pinned
conversation the section exists but has not been laid out yet, so both
tests spent fifteen seconds polling it and failed on the wait rather than
on anything they assert.

Give them a helper that does only what they need: open the mobile drawer
from `inert`, the one signal that distinguishes a closed drawer from an
open one while it stays mounted, then wait for the slide to reach the
viewport. Desktop renders no drawer and needs nothing.

* test: open the drawer by what it shows, not one early read

The helper decided once, from a single read of `inert` taken right after
navigation. On a fresh mobile load the drawer can still be mounting, so
that read reports a state the app has not settled into, the helper
returns believing the drawer is open, and the account menu inside it is
never shown.

Retry against the thing the caller actually needs, the account menu being
visible, re-checking before each attempt. While the drawer is open its
control is labelled "Close sidebar", so the retry cannot toggle it shut.

* fix: keep the composer observing MCP authorization where its tools apply

The old badge row mounted its provider with observeToolAuthorization set
whenever the composer showed ephemeral tools, so on a plain endpoint the
chat kept MCP connection status and discovered tools in the query cache.
The redesigned composer mounts the same provider without the prop, so it
defaults to off and nothing on the chat side loads that state any more.

The Agent Builder's tool library reads that cache. With it empty, every
server reads as Unknown with no tools, and clicking one opens its detail
dialog offering to connect instead of adding it to the agent.

Pass the same gate the badge row used: showTools is the old
showEphemeralBadges expression.

* test: let the palette close before reopening it, and name what is asserted

enableBuiltinTool pressed Escape and returned while the palette was still
animating out. A caller that reopened it straight away resumed the same
popover, scroll offset included, so the Attach rows at the top sat outside
the virtualized window and upload helpers timed out looking for them. Wait
for the dialog to be gone before returning.

The staged-file assertions matched the bare file name, which the palette
now also lists under recent uploads while it closes; scope them to the
tray they are about.

The OAuth dialog dismissal waited for a title built from the server's
display name, but the dialog is titled with the server name.

* feat: make the queued-send lock expiry configurable

The per-pane lock that stops the run-end drain and an explicit Send now
from submitting the same queued message twice gives up a broken claim
after a fixed 60 seconds. Until then every drain and every Send now for
that pane is refused, and a deployment had no way to tune that window.

Expose it as interface.queuedSendLockTimeoutMs beside the queued-turn
reconciliation timeout, defaulting to the same 60 seconds so nothing
changes unless an operator sets it. The interface loader copies each
field explicitly, so the value is added there too; without that line a
configured value would never reach the client.

* fix: decline interrupt-and-steer while reasoning is staged

With a reasoning choice staged, the Interrupt & steer row is disabled,
but its keyboard chord still reached interruptSteer, which fell back to
interruptAndSend: it aborted the run and threw away the partial answer
the action promises to keep.

A staged reasoning choice belongs to a new generation that a steer into
the live one cannot carry, so decline the chord exactly as the row does,
the same way the function already declines while paused on an approval.
The words stay in the composer, where Enter queues them as a turn of
their own.

* fix: wake the queue drain when a late steer retry is queued

Retry marks a failed steer sending, and a sending steer is kept out of
the run-end sweep so its own POST settles it. When the run finishes
while that POST is in flight, the drain spends its one-shot signal on a
queue the steer is not in yet; the late ack or NO_ACTIVE_RUN then moves
it into the queue, where it sat until the user sent it by hand or
another run ended.

Every other late conversion re-posts the last completed run end through
the composer's rewakeDrain. The retry lives in the thread, so the
composer now publishes that function through the same host it already
uses to hand a reclaimed steer back to the draft, and the retry calls it
after converting. It stays a no-op unless the run completed cleanly and
no newer signal is armed, or when no composer is mounted.

* fix: honour thinking-budget ranges wider than 200,000

The request-scoped reasoning override capped thinkingBudget at 200,000
in the shared schema. An operator's paramDefinitions may advertise a
wider range, and the composer renders it, but every value above the cap
was dropped by the control's parse and rejected by the server's before
the range-aware check could run.

That check already exists: resolveReasoningOverride validates the value
against the endpoint's resolved range. Drop the fixed ceiling so the
resolved range is the bound, keeping the integer and lower-bound rules.
The new case covers both directions on a custom endpoint whose range
goes to 500,000.

* refactor: persist the preliminary reasoning override through the shared helper

The agents controller validated the request's reasoning override with
the schema itself before writing the preliminary user message. That is
behavior in the CJS layer, and it duplicated resolvePersistedReasoningOverride,
which @librechat/api already exports and BaseClient already uses for the
same field.

Call the helper instead. A preliminary message is always a fresh turn,
so it is never an edit or a compaction. The two controller specs that
replace @librechat/api with a hand-built mock now take the real helper.

* fix: clear the file manager's state at the session boundary

The file manager's open flag and its opener live in Jotai's default
store, which outlives the authenticated route. A file manager left open
at logout reopened for the next account, still holding a ref to the
previous session's unmounted opener. Reset both in useClearStates, next
to the other composer state this branch already clears there.

* fix: decline interrupt-and-steer on the first turn while reasoning is staged

* test: read the phone layout from the viewport and keep the drawer open

* fix: name both reasons a queued message cannot be edited yet

* test: reuse a published file through the composer palette

* style: move the composer onto the design-system rules

* style: move the file row's new layout styles into classes

* fix: keep the composer's reused thumbnails, focus ring and disabled dimming intact

* fix: bring a steer back when its deferred cancel does not remove it

* fix: treat a run as over only when every pane holding it is idle

* refactor: pass enter-to-send into the composer from its host

* feat: make the palette's recent-file count configurable

* fix: let a file already in the composer be reattached at the limit

* test: hide the chip before replaying a deferred cancel

* refactor: restore the resume request through one packages/api facade

* fix: keep a conversation's staged reasoning across a visit to another model

* fix: keep a connected MCP server's variables configurable from the palette

* fix: resolve every pane-scoped shortcut through the pane holding focus

* refactor: keep every reasoning-override decision out of the /api wiring

* perf: skip paging the skill catalog for an agent that can show no skill

* test: type the reasoning snapshot the override helper records

* test: pin the applied budget and the untouched parameters in the override specs

* style: give the stop button the send button's shared submit recipe

* fix: name only the newline key when no chord sends

* fix: name the stop key only once the stop control can act

* fix: clear every staged reasoning selection at sign-out

* fix: stop advertising send-now while staged reasoning forces a queue

* fix: resolve the reasoning target of an ephemeral agent in the composer and the request

* test: type the staged reasoning the sign-out spec seeds

* fix: check the composer will take a queued row before discarding its parked copy

* style: carry the composer's classes onto Tailwind v4

* style: sort the composer's classes in Tailwind v4 order

* fix: return a refused queued row to the durable queue instead of memory

* i18n: localize the whole attachment-limit message

* refactor: validate the reasoning override inside the packages/api facade

* style: give the composer's submit slot its own IconButton variant

* test: pin the submit slot to IconButton's submit variant

* test: stub the seen-state hook the rebased MessagesView now calls

* fix: turn file search on for an embedded file attached through the shared hook

* chore: record the composer's design-rule counts against the rebased base

* fix: keep the composer's container query inert under Tailwind v4

* test: point the v4 focus scenarios at the redesigned composer's controls

* fix: keep a re-queued row on its own lineage after the run ends

* test: assert a re-queued row carries its lineage

* refactor: pass the dictation preferences into the composer from its host

* test: read the usage atom from the store the ask hook renders under

* fix: read a pending steer's pause from its own pane's branch

* a11y: keep focus on the quote chips until the last one is removed

* fix: keep staged reasoning for the next message across a manual compaction

* test: expect focus on the remaining quote chip after removing one of two

* style: sort the imports the canary rebase merged

* refactor: remove the side-panel files table the account menu replaced

* fix: settle a retried steer's late ack against a racing cancel

A cancel clicked while the retry POST was in flight left the optimistic
marker unread: the ack re-minted a live chip and injected words the user
had asked back, and a failed retry after that cancel lost them entirely,
because the failure path maps chips the optimistic cancel had removed.
The ack path now defers to the marker the way the composer's does, the
failure path re-adds the chip before settling it, and the queued row a
late ack produces binds its recovery identity only under protocol v2,
the same default the composer's degradation path trusts.

* fix: strip a replayed reasoning override that no longer validates on resume

A paused turn's reasoning override is replayed onto its resume request
and then validated as if it were fresh client input, so an operator
changing the endpoint's reasoning config between pause and approve made
the checkpoint permanently unresumable with a 400. The replay now marks
the request, and a replayed override that fails validation is stripped
so the resume proceeds on defaults; a fresh override that fails is still
rejected.

* refactor: derive the enforced spec fields inside the modelSpecs facade

The set of keys an enforced model spec locks is the input contract of
the reasoning-override refusal rule, and deriving it in the route
middleware left the policy untestable from packages/api and free to
diverge from the facade that owns the rest of the preset application.
applyModelSpecPreset now returns the enforced fields beside the private
ones it applies, and the middleware consumes them.

* fix: restore the pre-override parameters when a resume degrades

Stripping a replayed reasoning override that no longer validates removed
only the metadata field, while the resume had already replayed the paused
turn's model parameters with the override applied into the endpoint
option, so the provider still received what validation had refused. The
strip now inverts the application from the replayed base: the recorded
pre-override value returns and the thinking flag the override forced on
comes off.

* fix: resolve the effective responses route for upload targets

The attach target returned the conversation or agent's saved
useResponsesApi flag, so an Azure chat auto-routed to the Responses API
by responsesApiRouting preflighted its uploads against the chat
completions route and lost provider-document uploads. The resolver now
applies resolveEffectiveUseResponsesApi with the endpoint, model,
web-search state and routing policy, the same resolution the during-run
upload target makes.

* fix: name the interrupt chord in the hint only while it still interrupts

The during-run hint advertised Alt+Enter Interrupt & send even when the
chord had been claimed: a submitMessage rebound to Alt+Enter, a chord
yielded to a global shortcut, or shortcuts disabled altogether each make
the keydown resolver return something else. The hint now reads the same
resolver verdict the during-run send button does and omits the chord it
no longer owns.

* test: assert the unseen count the scenario adds rather than a zero baseline

The tab title counts every unseen conversation, and a shared
verification run leaves legitimately-unseen conversations from earlier
scenarios behind, so the three title-count scenarios failed on other
scenarios' leftovers instead of the behavior they seed. Each now reads
its baseline and asserts the count it adds and clears.

* test: hold the unseen count to its direction on the mobile project

The away poll that lights a dot also discovers conversations the client
had not counted yet, so on the mobile project the title count can jump
past any baseline taken mid-test. Opening still has to clear this
conversation's contribution and an away reply still has to raise the
count, which is what the assertions now hold.

* test: enable the MCP Apps server through the redesigned palette

The MCP Apps integration spec still drove the old composer's MCP
Servers menu, which the redesign replaced with toggleable rows in the
attach-and-tools palette; the lane failed on the missing menu on every
run. The helper now opens the palette, waits for the server wave, and
flips the row's pressed state.

* fix: preserve numeric reasoning defaults on blur

* test: reopen persisted code cards before checking highlights

* test: follow the current file manager navigation

* fix: offer only reasoning options a request override can carry

* fix: keep the favorited copy when skills share a name

* fix: restore composer focus by the interaction that closed the palette

* test: expect a keyboard close to refocus the composer on every project

* chore: re-record the design-rule backlog on the rebased composer

* chore: drop the files table and label the rebase brought back

* test: tolerate other projects' unseen conversations when marking one unread

* test: open the file table header scenario from the account menu

* fix: let the stop shortcut reach a hidden stop control from a dialog

* fix: keep staged reasoning out of tool-limit recovery and MCP App turns

* fix: degrade a queued turn's stale reasoning override instead of dead-lettering it

* fix: refresh the attach target when web search changes the route

* fix: re-arm the run-end drain after moving a steer to the queue

* test: hover the MCP row before reaching its revealed Connect action

* test: pick the parts scenario's MCP server from the tools palette

* fix: cancel a recovered row's parked steer only after the composer takes it

A recovered row's only durable copy is the steer parked on the server, and
once its run has ended nothing can re-create it, so a composer that refused
the row after the cancel left the words in memory until the next reload.
The composer now takes the words first; a cancel that then fails holds the
row out of the run-end drain instead of sending it a second time.

* fix: name only the idle composer affordances that act

* fix: keep steer escalation off until the pane owns the rendered conversation

The branch tail that decides whether a run is paused is keyed by the pane's
conversation, which can still name the previous chat while a cached one
renders. Until the pane catches up, a pause on any cached branch now keeps
Escalate disabled instead of reading a missing or foreign tail as unpaused.

* fix: route the host's Enter and auto-send preferences into the composer's nested hooks

ChatForm takes enterToSend and autoSendText from its host, but useTextarea,
DuringRunSendButton and both speech engines still read the store atoms, so a
host whose values differ advertised one behavior while the handlers ran
another. Pass the host values through instead.

* fix: settle a browser dictation take only after its final transcript lands

stopListening resolves on the recognizer's end event, after the final result
reached useSpeechRecognition's reducer but before React committed it, so the
take settled while the composer still held the interim text. Settle from an
effect declared after the transcript effect, off a state update queued behind
the result.

* fix: drop a restored reasoning override the current setting no longer offers

The staged override was only checked for a matching parameter key, so a value
restored after a capability change on the same target stayed staged, blocked
live steering, and was dropped at send time. Validate it against the setting's
options and range.

* test: scan the attach and tools palette for accessibility

The a11y suite still opened the removed tools dropdown. Open the palette
instead, and add a scenario that runs Axe over the open palette.

* fix: make the palette's virtualized list the grid that owns its rows

The rows sat under a wrapper grid behind react-virtualized's scroller, which
always renders aria-label and aria-readonly and so could not be made
presentational. Axe reported both the disallowed attribute and the grid's
invalid child. The scroller is now the labelled grid, with a rowgroup holding
the rows.

* fix: state the palette grid's row total and each row's position

Only the rows in view are mounted, so without aria-rowcount and aria-rowindex
assistive technology reads the rendered window as the whole grid.

* fix: key an empty destination's pending steers to the route

An empty tree has no message naming its conversation, so the pending-steer
surface fell back to the chat context, which still names the source chat
during warm navigation, and its controls acted on the source run. Fall back
to the chat's messages key, which follows the route.

* fix: keep assistant uploads available when the file config fails

Assistants upload directly and never needed the destination policy, which is
why the replaced AttachFileChat rendered their uploader outside the policy
gate. Restore that exception so a failed or paused file config request does
not remove every attachment entry.

(cherry picked from commit f568a4ba30)

Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/14473
2026-10-01 03:46:18 +00:00

733 lines
31 KiB
TypeScript

import mongoose from 'mongoose';
import { AsyncLocalStorage } from 'async_hooks';
import { randomUUID } from 'crypto';
import { MongoMemoryReplSet } from 'mongodb-memory-server';
import fs from 'fs';
import { MongoServerError } from 'mongodb';
import type { Collection } from 'mongodb';
import type { ConnectOptions } from 'mongoose';
import {
Permissions,
PermissionBits,
ResourceType,
PrincipalType,
PrincipalModel,
PermissionTypes,
} from 'librechat-data-provider';
import {
createMCPAuthorityBootRevision,
createMCPAuthorityCredentialRevision,
createMCPAuthorityDatabaseSourceRevision,
} from '~/methods/mcpAuthority';
import type { MCPOptions } from 'librechat-data-provider';
import { tenantStorage } from '~/config/tenantContext';
import type { AllMethods } from '~/methods';
import { createMethods } from '~/methods';
import { createModels } from '~/models';
jest.mock('~/config/winston', () => ({
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
}));
/**
* Method sweep: drives EVERY exported data-schemas method against a real
* engine and records, per method, how many driver queries it issued and
* whether the engine rejected any of them.
*
* This exists because hand-picked probe lists inherit the auditor's blind
* spots: every DocumentDB incompatibility found so far (pipeline updates,
* `$$REMOVE`, `$facet`, the transaction probe, the Mongoose-compiled mixed
* projection) was invisible until someone thought to look for its class. The
* sweep flips that: the engine itself adjudicates every query each method
* emits. Query shapes are parsed before they are matched, so even an empty
* database adjudicates most entry paths.
*
* The sweep is honest about its limits instead of quietly green:
* - `not-driven` — the method issued zero driver queries (input validation
* rejected the synthesized arguments, or a guard early-returned). NOT
* adjudicated; add an override in `ARG_OVERRIDES` to drive it.
* - `queries: N` — how many driver calls the engine actually saw. Deep branches
* behind state the sweep did not seed remain uncovered; the number makes the
* partiality visible.
*
* Runs in two modes with identical mechanics, so matrices diff cleanly:
* SWEEP_BASELINE=true → in-memory real MongoDB (free; CI-able)
* DOCUMENTDB_URI=... → live Amazon DocumentDB (see
* audit.documentdb.spec.ts for the
* connection recipe; every parameter is
* load-bearing through a tunnel)
* SWEEP_REPORT_PATH writes the matrix as JSON for machine diffing.
* DOCUMENTDB_STRICT=true fails the suite on any engine rejection.
*/
const DOCUMENTDB_URI = process.env.DOCUMENTDB_URI ?? '';
const BASELINE = process.env.SWEEP_BASELINE === 'true';
const STRICT = process.env.DOCUMENTDB_STRICT === 'true';
const REPORT_PATH = process.env.SWEEP_REPORT_PATH ?? '';
const describeSweep = DOCUMENTDB_URI || BASELINE ? describe : describe.skip;
const METHOD_TIMEOUT_MS = 20_000;
const runId = randomUUID().slice(0, 8);
interface MethodVerdict {
queries: number;
engineError?: string;
outcome: 'ok' | 'engine-rejected' | 'not-driven' | 'no-queries' | 'threw' | 'timeout';
detail?: string;
}
const verdicts: Record<string, MethodVerdict> = {};
const stragglers: Array<Promise<unknown>> = [];
/** Attribution rides the method's own async context: a straggler that
* outlives its timeout keeps issuing queries under ITS label instead of
* corrupting whichever row the sweep has moved on to. */
const sweepContext = new AsyncLocalStorage<string>();
const activeLabel = (): string | null => sweepContext.getStore() ?? null;
const DRIVER_OPS = [
'find',
'findOne',
'findOneAndUpdate',
'findOneAndDelete',
'findOneAndReplace',
'updateOne',
'updateMany',
'replaceOne',
'insertOne',
'insertMany',
'deleteOne',
'deleteMany',
'aggregate',
'countDocuments',
'distinct',
'bulkWrite',
'createIndex',
'createIndexes',
'dropIndex',
'dropIndexes',
'listIndexes',
] as const;
/** Server errors that are not engine incompatibilities: duplicate keys prove
* the write REACHED the engine and hit a constraint, and `NamespaceExists`
* (48) is the expected outcome of the authority path's idempotent namespace
* preflight when a collection is already present. */
const BENIGN_SERVER_CODES = new Set([11000, 11001, 48]);
function recordEngineError(label: string | null, error: unknown): void {
if (label == null) return;
const err = error as { name?: string; message?: string; code?: number };
/** `instanceof` covers the driver's server-error subclasses
* (MongoWriteConcernError, MongoBulkWriteError, ...) whose `name` differs
* from the base class — a write-concern rejection at commit must not slip
* through as a mere domain throw. */
if (!(error instanceof MongoServerError)) return;
if (err.code != null && BENIGN_SERVER_CODES.has(err.code)) return;
if (/E11000/.test(String(err.message))) return;
const verdict = (verdicts[label] ??= { queries: 0, outcome: 'ok' });
if (verdict.engineError == null) {
verdict.engineError = String(err.message).replace(/\s+/g, ' ').slice(0, 160);
}
}
/** Cursor-returning ops surface parse errors on consumption, not on the call,
* so the cursor's promise-returning members are wrapped with the label that
* was current when the cursor was created. */
function wrapCursor<T extends object>(cursor: T, label: string | null): T {
return new Proxy(cursor, {
get(target, property, receiver) {
const value = Reflect.get(target, property, receiver);
if (typeof value !== 'function') return value;
return function wrapped(this: unknown, ...args: unknown[]) {
const result = value.apply(target, args);
if (result instanceof Promise) {
return result.catch((error: unknown) => {
recordEngineError(label, error);
throw error;
});
}
return result;
};
},
});
}
function instrumentDriver(): void {
const sample = mongoose.connection.db?.collection(`sweep_probe_${runId}`);
if (sample == null) throw new Error('no database handle to instrument');
const proto = Object.getPrototypeOf(sample) as Collection & Record<string, unknown>;
for (const op of DRIVER_OPS) {
const original = proto[op] as (...args: unknown[]) => unknown;
if (typeof original !== 'function' || (original as { __swept?: boolean }).__swept) continue;
const wrapped = function (this: Collection, ...args: unknown[]) {
const label = activeLabel();
if (label != null) {
(verdicts[label] ??= { queries: 0, outcome: 'ok' }).queries += 1;
}
const result = original.apply(this, args);
if (result instanceof Promise) {
return result.catch((error: unknown) => {
recordEngineError(label, error);
throw error;
});
}
if (op === 'find' || op === 'aggregate' || op === 'listIndexes') {
return wrapCursor(result as object, label);
}
return result;
};
(wrapped as { __swept?: boolean }).__swept = true;
(proto as Record<string, unknown>)[op] = wrapped;
}
}
/** Namespace DDL goes through `Db`, not the `Collection` prototype: the
* authority path's `Model.createCollection()` preflight delegates to
* `Db.createCollection`. Without this, a DocumentDB rejection of that DDL is
* laundered by `asMCPError` into `proof_unavailable`, the row reads as a mere
* `threw`, and STRICT passes despite an engine rejection. */
function instrumentDatabase(): void {
const db = mongoose.connection.db;
if (db == null) throw new Error('no database handle to instrument');
const proto = Object.getPrototypeOf(db) as Record<string, unknown>;
for (const op of ['createCollection', 'dropCollection', 'renameCollection']) {
const original = proto[op] as (...args: unknown[]) => unknown;
if (typeof original !== 'function' || (original as { __swept?: boolean }).__swept) continue;
const wrapped = function (this: unknown, ...args: unknown[]) {
const label = activeLabel();
if (label != null) {
(verdicts[label] ??= { queries: 0, outcome: 'ok' }).queries += 1;
}
const result = original.apply(this, args);
if (result instanceof Promise) {
return result.catch((error: unknown) => {
recordEngineError(label, error);
throw error;
});
}
return result;
};
(wrapped as { __swept?: boolean }).__swept = true;
proto[op] = wrapped;
}
}
/** Transaction-lifecycle rejections surface from the session, not from a
* collection operation, and several methods convert them into domain errors —
* without this, a real `commitTransaction` engine rejection classifies as a
* mere `threw` and STRICT passes. */
async function instrumentSessions(): Promise<void> {
const session = await mongoose.startSession();
try {
const proto = Object.getPrototypeOf(session) as Record<string, unknown>;
for (const op of ['commitTransaction', 'abortTransaction', 'withTransaction']) {
const original = proto[op] as (...args: unknown[]) => unknown;
if (typeof original !== 'function' || (original as { __swept?: boolean }).__swept) continue;
const wrapped = function (this: unknown, ...args: unknown[]) {
const label = activeLabel();
if (label != null) {
(verdicts[label] ??= { queries: 0, outcome: 'ok' }).queries += 1;
}
const result = original.apply(this, args);
if (result instanceof Promise) {
return result.catch((error: unknown) => {
recordEngineError(label, error);
throw error;
});
}
return result;
};
(wrapped as { __swept?: boolean }).__swept = true;
proto[op] = wrapped;
}
} finally {
await session.endSession();
}
}
const objectId = () => new mongoose.Types.ObjectId();
const OBJECT_ID_PARAMS =
/^(user|author|owner|principalId|targetId|granteeId|grantedById|revokedBy|agentId)$/;
const DATE_PARAMS = /(At|Until|Before|After|Date|Cutoff)$|^(now|date|fenceStartedAt)$/;
const NUMBER_PARAMS =
/^(limit|count|page|pageSize|size|amount|n|attempt|sequence|version|epoch|tokenCount|priority|expiredMessageRetentionDays)$|(Limit|Count|Ms|Bytes|Days)$/;
const BOOLEAN_PARAMS = /^(is|has|should|include|mark|force|replay|dry)/;
const ARRAY_PARAMS =
/(Ids|Keys|ids|keys|Names)$|^(tags|conversations|files|messages|targets|principals|entries|updates|operations|roles|values)$/;
function valueFor(name: string): unknown {
if (name === 'mongoose') return mongoose;
if (name === 'kind') return 'manual';
/** Typed-criteria parameters (`utils/criteria.ts`) are validated key by key
* and fail closed, so a synthesized string is read as a criteria object and
* rejected at index '0'. An empty criteria object is valid, builds an empty
* filter, and still reaches the engine. `searchMessages` is the sole method
* whose `query` parameter is a genuine string (verified across
* `src/methods`); it carries an `ARG_OVERRIDES` entry so it keeps one. A
* future string-valued `query`/`criteria`/`filter` parameter would need the
* same treatment — the symptom is a method reporting `threw` on a type
* error rather than reaching the database. */
if (/^(query|criteria|filter)$/.test(name)) return {};
if (/pipeline/i.test(name)) return [{ $match: { _id: objectId() } }];
if (OBJECT_ID_PARAMS.test(name)) return objectId();
if (ARRAY_PARAMS.test(name)) return [`sweep-${name}-${runId}`];
if (/Until$/.test(name)) return new Date(Date.now() + 60_000);
if (DATE_PARAMS.test(name)) return new Date();
if (NUMBER_PARAMS.test(name) || /limit/i.test(name)) return 2;
if (BOOLEAN_PARAMS.test(name)) return false;
if (/tenantId/i.test(name)) return undefined;
if (name === 'conversationId' || name === 'threadId') return randomUUID();
if (name === 'email') return `sweep-${runId}@sweep.test`;
return `sweep-${name}-${runId}`;
}
/** Walks the argument tree replacing every exact occurrence of a value. */
function replaceValue(node: unknown, target: string, replacement: unknown): unknown {
if (typeof node === 'string') return node === target ? replacement : node;
if (Array.isArray(node)) return node.map((item) => replaceValue(item, target, replacement));
if (node != null && typeof node === 'object' && !(node instanceof Date)) {
if (node instanceof mongoose.Types.ObjectId) return node;
return Object.fromEntries(
Object.entries(node).map(([key, value]) => [key, replaceValue(value, target, replacement)]),
);
}
return node;
}
/** Repairs synthesized arguments from a validation error's own message, so the
* retry reaches the database instead of being reported `not-driven`. Returns
* the repaired arguments, or null when the error is not machine-repairable. */
function adaptArgs(args: unknown[], error: unknown): unknown[] | null {
const message = String((error as { message?: string })?.message ?? '');
let match = /Cast to (?:\[)?ObjectId(?:\])? failed for value "+?\[?'?"?([^"'\]]+)/.exec(message);
if (match != null) {
return args.map((arg) => replaceValue(arg, match![1], objectId())) as unknown[];
}
match = /Cast to (?:\[)?Number(?:\])? failed for value "([^"]+)"/.exec(message);
if (match != null) {
return args.map((arg) => replaceValue(arg, match![1], 2)) as unknown[];
}
match = /Cast to (?:\[)?date(?:\])? failed for value "([^"]+)"/i.exec(message);
if (match != null) {
return args.map((arg) => replaceValue(arg, match![1], new Date())) as unknown[];
}
match = /Cast to (?:Embedded|\[Embedded\]|Map|Mixed) failed for value "([^"]+)"/.exec(message);
if (match != null) {
return args.map((arg) => replaceValue(arg, match![1], {})) as unknown[];
}
if (/must be an object/.test(message)) {
const repaired = args.map((arg) => {
if (Array.isArray(arg)) return arg.map((item) => (typeof item === 'string' ? {} : item));
if (typeof arg === 'string') return {};
return arg;
});
return JSON.stringify(repaired) === JSON.stringify(args) ? null : repaired;
}
const requiredPaths = [
...message.matchAll(/Path `([A-Za-z_$][A-Za-z0-9_$.]*)` is required/g),
].map((m) => m[1]);
if (requiredPaths.length > 0) {
const objectIndex = args.findIndex((arg) => arg != null && typeof arg === 'object');
const target = (objectIndex >= 0 ? { ...(args[objectIndex] as object) } : {}) as Record<
string,
unknown
>;
for (const path of requiredPaths) {
target[path.split('.')[0]] = valueFor(path.split('.')[0]);
}
const repaired = [...args];
if (objectIndex >= 0) repaired[objectIndex] = target;
else repaired.push(target);
return repaired;
}
return null;
}
interface SweepCase {
args: unknown[];
/** Optional execution wrapper, e.g. to run the call in tenant context. */
within?: <T>(run: () => Promise<T>) => Promise<T>;
}
/** Per-method cases where name-pattern synthesis cannot produce a shape that
* reaches the database (validation rejects it, a guard early-returns, or the
* interesting path needs seeded prerequisite records). Grow this whenever the
* matrix reports `not-driven` for a path worth adjudicating. */
const ARG_OVERRIDES: Record<string, () => SweepCase | Promise<SweepCase>> = {
/** Positional (searchParameter, id): a synthesized string for the first
* parameter lands inside a `$match` and malforms it on both engines. */
getAgentWithVersionCount: () => ({ args: [{ id: `agent-sweep-${runId}` }] }),
/** Takes a genuine search string, not typed criteria: the criteria-shaped
* synthesis above would hand it an object, which a Meili-enabled deployment
* forwards verbatim into `meiliSearch` and adjudicates a harness-generated
* request instead of the method's real behavior. */
searchMessages: () => ({ args: [`sweep-search-${runId}`, { page: 1, limit: 2 }, false] }),
/** The bounded authority snapshot is the one production path that COMMITS a
* transaction; generic synthesis dies on target validation before a session
* ever starts, which would leave the session instrumentation exercising
* nothing. Seeds the full fixture (ported from compat.documentdb.spec.ts)
* and runs the call in the same tenant context. */
resolveMCPAuthorityProof: async () => {
const tenantId = `sweep-authority-${runId}`;
const roleName = `SWEEP_AUTHORITY_${runId}`;
const serverName = `sweep-authority-server-${runId}`;
const models = mongoose.models;
const userId = new mongoose.Types.ObjectId();
const serverId = new mongoose.Types.ObjectId();
const context = { tenantId, userId: userId.toHexString() };
await tenantStorage.run(context, async () => {
await models.User.create({
_id: userId,
name: 'Sweep authority probe',
email: `sweep-authority-${runId}@sweep.test`,
provider: 'local',
role: roleName,
});
await models.Role.create({
name: roleName,
permissions: { [PermissionTypes.MCP_SERVERS]: { [Permissions.USE]: true } },
});
await models.MCPServer.create({
_id: serverId,
serverName,
config: { type: 'sse', url: `https://${serverName}.example/mcp` },
author: userId,
});
await models.Agent.create({
id: `sweep-authority-agent-${runId}`,
name: 'Sweep authority probe agent',
provider: 'openAI',
model: 'probe-model',
author: userId,
mcpServerNames: [serverName],
});
await models.AclEntry.create({
principalType: PrincipalType.USER,
principalId: userId,
principalModel: PrincipalModel.USER,
resourceType: ResourceType.MCPSERVER,
resourceId: serverId,
permBits: PermissionBits.VIEW,
grantedBy: userId,
});
/** The proof transaction READS PluginAuth, Token, Group, and Config;
* with `autoCreate: false` on a fresh run-scoped database those
* collections do not exist, and DocumentDB rejects any statement in a
* transaction that touches a non-existent collection — surfacing as
* `proof_unavailable` before the commit is ever reached. Materialize
* every transaction-read collection outside the transaction. */
await models.PluginAuth.createCollection();
await models.Token.createCollection();
await models.Group.createCollection();
await models.Config.createCollection();
});
const server = await tenantStorage.run(context, () =>
models.MCPServer.findById(serverId).lean<{
_id: mongoose.Types.ObjectId;
serverName: string;
author: mongoose.Types.ObjectId;
config: MCPOptions;
createdAt: Date;
updatedAt: Date;
}>(),
);
if (server == null) {
throw new Error('sweep authority fixture server was not created');
}
const sourceRevision = createMCPAuthorityDatabaseSourceRevision({
databaseId: server._id.toHexString(),
serverName: server.serverName,
author: server.author.toString(),
config: server.config,
createdAt: server.createdAt,
updatedAt: server.updatedAt,
});
return {
args: [
{
userId: userId.toHexString(),
tenantId,
boot: createMCPAuthorityBootRevision(`sweep-${runId}`, { mcpServers: {} }),
targets: [
{
serverName,
source: 'database',
databaseId: serverId.toHexString(),
sourceRevision,
expectedCredentialRevision: createMCPAuthorityCredentialRevision([], []),
expectedOAuthGrantGeneration: null,
resolvedConfig: server.config,
},
],
},
],
within: (run) => tenantStorage.run(context, run),
};
},
};
/** Parses the function source just enough to synthesize a plausible call:
* either one destructured options object or a positional list. */
function synthesizeArgs(fn: (...args: unknown[]) => unknown): unknown[] {
const source = fn.toString();
const header = source.slice(0, source.indexOf(')') + 1);
const paramList = header.slice(header.indexOf('(') + 1, -1).trim();
if (paramList.length === 0) return [];
if (paramList.startsWith('{')) {
const keys = [...paramList.matchAll(/([A-Za-z_$][A-Za-z0-9_$]*)\s*[,:=}]/g)].map((m) => m[1]);
return [Object.fromEntries(keys.map((key) => [key, valueFor(key)]))];
}
const names = paramList
.split(',')
.map((part) => part.trim().split(/[=:\s]/)[0])
.filter((name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name));
return names.map((name) =>
name === 'input' || name === 'params' || name === 'options' || name === 'data'
? synthesizeObjectFromBody(fn)
: valueFor(name),
);
}
/** For `function (input) { ... input.userId ... }` shapes: harvest the
* property names the body reads off the single object parameter. */
function synthesizeObjectFromBody(fn: (...args: unknown[]) => unknown): Record<string, unknown> {
const source = fn.toString();
const parameter = source
.slice(source.indexOf('(') + 1, source.indexOf(')'))
.split(',')[0]
?.trim()
.split(/[=:\s]/)[0];
if (parameter == null || parameter.length === 0) return {};
const reads = [
...source.matchAll(new RegExp(`${parameter}\\.([A-Za-z_$][A-Za-z0-9_$]*)`, 'g')),
].map((m) => m[1]);
return Object.fromEntries([...new Set(reads)].map((key) => [key, valueFor(key)]));
}
const models = createModels(mongoose);
Object.assign(mongoose.models, models);
/** No dependency overrides: `createMethods` supplies a real
* `removeAllPermissions` that delegates to `deleteAclEntries`, so the driven
* deletion cascades exercise their ACL database operations. */
const methods = createMethods(mongoose);
type SweepFn = (...args: unknown[]) => unknown;
/** Every AllMethods member is a function; the per-entry conversion is the
* variadic-unknown view the sweep needs to invoke them generically. */
const methodMap: Record<string, SweepFn> = Object.fromEntries(
Object.entries(methods).flatMap(([name, member]) =>
/** Error classes ride along in some method bundles (e.g. `SessionError`);
* a constructor is not a sweepable method and would inflate the totals as
* a permanently un-driven row. */
typeof member === 'function' && !/^class[\s{]/.test(String(member))
? [[name, member as SweepFn]]
: [],
),
);
const methodNames = Object.keys(methodMap).sort();
let memoryServer: MongoMemoryReplSet | undefined;
describeSweep(`data-schemas method sweep (${BASELINE ? 'MongoDB baseline' : 'DocumentDB'})`, () => {
beforeAll(async () => {
if (BASELINE) {
/** A single-node replica set, not a standalone: driven methods start
* real transactions, and a standalone baseline would reject them for
* topology reasons and manufacture a false matrix divergence. The
* connection options mirror the live branch exactly — model collections
* and indexes must not be pre-created outside instrumentation in one
* mode and absent in the other. */
memoryServer = await MongoMemoryReplSet.create({ replSet: { count: 1 } });
await mongoose.connect(memoryServer.getUri(), {
autoIndex: false,
autoCreate: false,
dbName: `librechat_sweep_${runId}`,
});
} else {
const options: ConnectOptions = { autoIndex: false, autoCreate: false };
if (process.env.DOCUMENTDB_TLS_CA_FILE) {
options.tlsCAFile = process.env.DOCUMENTDB_TLS_CA_FILE;
}
if (process.env.DOCUMENTDB_TLS_ALLOW_INVALID_HOSTNAMES === 'true') {
options.tlsAllowInvalidHostnames = true;
}
/** A run-scoped database: the sweep writes into real model collections
* and drops its database afterwards, and the sibling live suites share
* the URI — dropping THEIR database mid-run would make their results
* nondeterministic. */
await mongoose.connect(DOCUMENTDB_URI, {
...options,
dbName: `librechat_sweep_${runId}`,
});
}
instrumentDriver();
instrumentDatabase();
await instrumentSessions();
}, 120_000);
afterAll(async () => {
/** Settle timed-out invocations (bounded) BEFORE tearing the engine down:
* dropping the database or closing the client would abort their
* outstanding work with closed-client errors that are not engine verdicts,
* leaving the row unadjudicated while STRICT passes. */
if (stragglers.length > 0) {
await Promise.race([
Promise.allSettled(stragglers),
new Promise((resolve) => setTimeout(resolve, 15_000).unref()),
]);
}
if (mongoose.connection.readyState === 1) {
await mongoose.connection.db?.dropDatabase().catch(() => undefined);
await mongoose.disconnect();
}
await memoryServer?.stop();
for (const verdict of Object.values(verdicts)) {
if (verdict.engineError != null) {
verdict.outcome = 'engine-rejected';
}
}
const rows = methodNames.map((name) => {
const verdict = verdicts[name] ?? { queries: 0, outcome: 'no-queries' as const };
return { name, ...verdict };
});
const rejected = rows.filter((row) => row.outcome === 'engine-rejected');
const notDriven = rows.filter(
(row) => row.outcome === 'not-driven' || row.outcome === 'no-queries',
);
const width = Math.max(...rows.map((row) => row.name.length));
console.log(
`\nMethod sweep matrix (run ${runId}, ${BASELINE ? 'baseline' : 'documentdb'}):\n` +
rows
.map(
(row) =>
` ${row.name.padEnd(width)} q=${String(row.queries).padStart(3)} ${row.outcome}` +
(row.engineError ? ` ${row.engineError}` : '') +
(row.detail ? ` (${row.detail})` : ''),
)
.join('\n') +
`\n TOTAL ${rows.length} | rejected ${rejected.length} | not-driven ${notDriven.length}\n`,
);
if (REPORT_PATH) {
/** `rows` is the canonical cross-engine payload: run-specific values are
* normalized out (this run's id, generated ObjectIds/UUIDs, and the
* hex/uuid fragments that synthesized arguments embed in error text), so
* `diff <(jq .rows a.json) <(jq .rows b.json)` shows engine behavior
* only. Run metadata stays outside it. */
const normalize = (text?: string): string | undefined =>
text
?.split(runId)
.join('<run>')
.replace(/[0-9a-f]{24}/gi, '<oid>')
.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '<uuid>');
fs.writeFileSync(
REPORT_PATH,
JSON.stringify(
{
meta: { runId, baseline: BASELINE },
rows: rows
.map((row) => ({
name: row.name,
queries: row.queries,
outcome: row.outcome,
engineError: normalize(row.engineError),
detail: normalize(row.detail),
}))
.sort((left, right) => left.name.localeCompare(right.name)),
},
null,
2,
),
);
}
if (STRICT) {
/** Enforced against the FINALIZED rows: the per-test check has
* already passed for a row that timed out and only later recorded its
* engine rejection. */
const rejectionDetails = rejected.map((row) => `${row.name}: ${row.engineError}`);
if (rejectionDetails.length > 0) {
throw new Error(`DocumentDB method sweep rejected:\n${rejectionDetails.join('\n')}`);
}
}
}, 120_000);
it.each(methodNames)(
'%s',
async (name) => {
/** A fresh bundle per case: factory-local caches (e.g. the session
* methods' `sessionIndexesPromise`) otherwise leak across alphabetically
* ordered rows, so a later method returns a settled promise without
* issuing its own queries and is misreported as `no-queries`. */
const fn = createMethods(mongoose)[name as keyof AllMethods] as SweepFn;
const sweepCase: SweepCase = (await ARG_OVERRIDES[name]?.()) ?? {
args: synthesizeArgs(fn),
};
const args = sweepCase.args;
const within = sweepCase.within ?? (<T>(run: () => Promise<T>) => run());
const verdict = (verdicts[name] ??= { queries: 0, outcome: 'ok' });
let callArgs = args;
const deadline = Date.now() + METHOD_TIMEOUT_MS - 5_000;
await sweepContext.run(name, async () => {
for (let attempt = 0; attempt < 6; attempt += 1) {
try {
const remaining = deadline - Date.now();
if (remaining <= 0) {
verdict.outcome = 'timeout';
break;
}
let raceTimer: NodeJS.Timeout | undefined;
const invocation = Promise.resolve(within(() => Promise.resolve(fn(...callArgs))));
try {
await Promise.race([
invocation,
new Promise((_, reject) => {
raceTimer = setTimeout(() => reject(new Error('sweep-timeout')), remaining);
}),
]);
} catch (raceError) {
if ((raceError as Error)?.message === 'sweep-timeout') {
/** The abandoned invocation keeps running under ITS OWN async
* context; the finalization pass awaits it so a late engine
* rejection still lands on this row before STRICT is enforced. */
stragglers.push(invocation.catch(() => undefined));
}
throw raceError;
} finally {
clearTimeout(raceTimer);
}
verdict.detail = undefined;
break;
} catch (error) {
const err = error as { name?: string; message?: string };
if (err?.message === 'sweep-timeout') {
verdict.outcome = 'timeout';
break;
}
verdict.detail = `${err?.name ?? 'Error'}: ${String(err?.message).slice(0, 80)}`;
const repaired = verdict.engineError == null ? adaptArgs(callArgs, error) : null;
if (repaired == null) {
break;
}
callArgs = repaired;
}
}
});
if (verdict.engineError != null) {
verdict.outcome = 'engine-rejected';
} else if (verdict.queries === 0 && verdict.outcome !== 'timeout') {
/** `not-driven` = validation threw before any query (repairable with an
* override); `no-queries` = returned cleanly without touching the
* database (a pure helper, or a guard the synthesized state
* short-circuited). Both are un-adjudicated; they differ in remedy. */
verdict.outcome = verdict.detail != null ? 'not-driven' : 'no-queries';
} else if (verdict.detail != null && verdict.outcome === 'ok') {
verdict.outcome = 'threw';
}
if (STRICT) {
expect(verdict.outcome).not.toBe('engine-rejected');
}
},
METHOD_TIMEOUT_MS,
);
});