🗂️ fix: Resolve Code Interpreter Destination Collisions (#15449)

* 🗂️ fix: Resolve Code Interpreter Destination Collisions

Codeapi mounts each `/exec` input at a destination derived from its `name`
and rejects the whole request when two entries land on one. LibreChat never
enforced that: `primeFiles` sent `name: file.filename` verbatim, and the
graph seed deduped by `(storage_session_id, id)` — identity, not destination.

Two conversation files sharing a filename therefore killed code execution for
the rest of the conversation. The rejected request never reaches the sandbox,
so no result comes back to collapse the pair, and every later turn re-primes
both and fails identically. Only code-generated outputs are covered by the
`(filename, conversationId, context, tenantId)` partial unique index; uploads
carry `context: message_attachment`, so one conversation can hold several
records sharing a filename — two uploads of `image.png`, or an upload whose
name a later execution wrote back in place.

`packages/api/src/files/code/destinations.ts` now owns the name → destination
mapping for every contributor to an `/exec` file list, mirroring codeapi's
conflict rule including directory-prefix conflicts:

- `primeFiles` claims per pushed file, disambiguating with a `-2`, `-3`
  counter and echoing the resolved path into the tool context, so the model
  is told where each file actually mounts. Claim order comes from immutable
  `createdAt`, not `getFiles`'s `updatedAt` sort, which usage accounting and
  re-upload both bump — order-dependent destinations would silently repoint
  a path an earlier turn had already handed the model. Newest wins the bare
  name, matching `ToolNode.updateCodeSession` and giving an in-place rewrite
  precedence over the original it superseded.
- `seedCodeFilesIntoSessions` drops a later file whose destination is already
  claimed. Renaming there would invent a path nothing told the model about,
  and a conflict at that layer means one logical file arrived twice under
  different storage pointers.

Fixes #15443

* 🧹 chore: Tolerate Holes in the Destination Priority Sort

`primeFiles` sorts the raw `getFiles` result before its own per-entry guard
runs, so the comparator has to survive a null row rather than throw ahead of
the loop that was already defending against one.

Also tightens the tool-context assertion in the upload-vs-output case, which
was passing on a trailing space rather than on the path.

* 🔁 fix: Address Codex Review on Destination Assignment

Four findings from the review of `b0f61d0`.

**Identity-derived suffixes instead of a collision counter.** A counter is
assigned from whatever is in the set, so a third file arriving under the
literal name a displaced file had been given pushed that file along, and code
the model wrote in an earlier turn would silently start reading the newcomer.
The fallback now hashes the file's own `file_id`, so a displaced file keeps
its destination for the life of the conversation and only the winner of the
bare name can change. A trailing counter still guards a hash collision so the
search terminates on any input.

**Rank by last content write, not record creation.** `processCodeOutput`
claims one row per `(filename, conversationId)` and rewrites it in place, so
a repeatedly written output keeps its original `createdAt` while recording the
latest writer in `metadata.sourceDispatchedAt`. Ranking on `createdAt` alone
handed the bare path to an upload the output had since been rewritten over.
Recency is now `max(sourceDispatchedAt, createdAt)` — still never `updatedAt`,
which usage accounting bumps without any content changing.

**Advertise a displaced generated output.** Generated files are left out of
the tool context because the model already knows what it wrote — which stops
being true once a newer same-named file takes the bare path and the output
mounts somewhere the model has never seen. Displaced outputs are now listed
with the name they were written as, and displaced uploads name their original
too.

**Rank conversation files above an agent's private ones.** Each agent claims
destinations over its own candidate set — the conversation's files plus its
own — so a private file taking a bare name from a shared file in one agent but
not in another left two agents advertising different paths for the same file
into one shared mount namespace. Shared files now rank first, so the majority
that every agent sees resolves identically. The private-versus-private case
still drops at the merge and is documented there; closing it needs one
assignment across contributors, made before any agent renders its context.

* 📌 test: Pin Alias Ownership on an In-Place Rewrite

Codex round 2 flagged that the identity alias still falls back to a counter
when the alias itself is literally claimed, so a displaced file can move after
all. The mechanism is real and my JSDoc overstated the guarantee, which this
corrects.

The behaviour stays as it is. The realistic way a file arrives under another
file's alias is the model rewriting the displaced file in place: it was told
`<stem>-<hash>.ext`, wrote back to it, and `processCodeOutput` registered an
output under that literal name. Keeping the alias with the newest holder is
what makes the model's familiar path resolve to its own edit; reserving it
against the newcomer would hand that path back to the bytes it replaced. The
test pins that ordering so a later change cannot invert it silently.
This commit is contained in:
Danny Avila 2026-09-01 00:55:54 -04:00 • committed by GitHub
parent 09722f4b16
commit 224512851a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 989 additions and 20 deletions

View file

@ -1,4 +1,5 @@
import { Constants } from '@librechat/agents';
import { logger } from '@librechat/data-schemas';
import type { FileRefs, CodeEnvFile, ToolSessionMap, CodeSessionContext } from '@librechat/agents';
import type { StatefulCodeEnvironment } from 'librechat-data-provider';
import {
@ -6,6 +7,7 @@ import {
resolveCodeExecutionContext,
type CodeExecutionContext,
} from './execution';
import { createCodeDestinationSet, reserveCodeDestination } from '~/files/code/destinations';
/**
* Minimal shape for an agent that may contribute primed code files to its
@ -125,6 +127,27 @@ export function collectCodeExecutionProfileRoutes(
* dedupe `_injected_files` would grow proportionally to agent count and
* inflate every `/exec` POST. First-seen wins so the original ordering /
* source is preserved.
*
* Identity dedupe alone cannot keep the seed valid: codeapi rejects the
* whole `/exec` request when two entries mount at one destination, and a
* file re-uploaded by one agent but cache-hit by another arrives twice
* under different `storage_session_id`s with a single `name`. Sources are
* merged in trust order — skill seed, then primary agent, then the rest —
* so first-seen also wins the destination, and the later copy of an
* already-mounted name is dropped rather than renamed to a path nothing
* told the model about.
*
* Each agent resolves destinations over its own candidate set, so this can
* still drop a genuinely distinct file: two agents whose *private*
* resources share a filename each claim the bare name locally, and only the
* first survives here while the second agent's tool context keeps
* advertising its own file at that path. `sortCodeFilesByDestinationPriority`
* ranks conversation-scoped files above private ones precisely so the shared
* majority cannot diverge that way; closing the private-versus-private case
* needs one assignment across contributors, which means resolving
* destinations before any agent renders its tool context. Until then this
* drop is the failure floor — before it, the pair reached codeapi together
* and took the whole run down with a rejected request.
*/
export function seedCodeFilesIntoSessions(
files: CodeEnvFile[] | undefined,
@ -139,16 +162,26 @@ export function seedCodeFilesIntoSessions(
const prior = sessions.get(sessionKey) as CodeSessionContext | undefined;
/**
* Compose `(storage_session_id, id)` as a stable identity. `name` alone
* isn't sufficient — two distinct primed uploads can share a filename
* (different storage sessions, different file_ids). The composite stays
* cheap to compute and the keys are short uuids.
* Identity is `(storage_session_id, id)`, not `name` — two distinct primed
* uploads can share a filename across different storage sessions and
* file_ids, and collapsing those would drop a file the caller resolved to
* its own mount path. The composite stays cheap to compute and the keys
* are short uuids. Destination uniqueness is enforced separately below,
* against the sandbox's one-file-per-path constraint.
*/
const seenKeys = new Set<string>();
const destinations = createCodeDestinationSet();
const mergedFiles: FileRefs = [];
const pushIfFresh = (f: { id?: string; storage_session_id?: string; name?: string }): void => {
const key = `${f.storage_session_id ?? ''}\0${f.id ?? ''}`;
if (seenKeys.has(key)) return;
if (f.name != null && !reserveCodeDestination(destinations, f.name)) {
logger.debug(
`[seedCodeFilesIntoSessions] dropped id=${f.id} name=${f.name} ` +
`reason=destination-taken sessionKey=${sessionKey}`,
);
return;
}
seenKeys.add(key);
mergedFiles.push(f as FileRefs[number]);
};