Skip to content

🧪 feat: Add LIBRECHAT_CODE_DEFAULT_PROFILE to Run Chat Code on the Stateful Profile - #17182

Open
TomasPalsson wants to merge 24 commits into
LibreChat-AI:devfrom
TomasPalsson:feat/stateful-code-default-profile
Open

TomasPalsson wants to merge 24 commits into
LibreChat-AI:devfrom
TomasPalsson:feat/stateful-code-default-profile

Conversation

@TomasPalsson

@TomasPalsson TomasPalsson commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Summary

Deployments whose Code Interpreter runs only the stateful profile reject every code request from an Agent that has not switched on stateful sessions, and from every normal chat with the Code toggle on. Users see "Failed to provision 1 file(s) to the code environment" (409 from the service) and the turn dies. Today the only way out is flipping the toggle on every Agent, which does nothing for normal chats.

This PR adds one deployment setting, LIBRECHAT_CODE_DEFAULT_PROFILE (default | stateful, default default). With stateful, saved Agents with the toggle off or unset, normal chats with Code on, added-conversation Agents and subagents all run on the stateful profile, and code-output downloads with no named profile follow it. Unset or default behaves exactly as today.

How it works

packages/api/src/code/config.ts parses the setting (trimmed, case-sensitive, an unknown value counts as unset and warns once). isStatefulDefaultEligible in packages/api/src/code/agent.ts decides whether it applies to an Agent, and every gate that read stateful_code_sessions === true now reads stateful_code_sessions === true || <eligible>, keeping its existing capability check:

  • resolveAgentCodeFlags (packages/api/src/code/agent.ts), which ToolService and both agent initialisers already delegate to
  • subagent routing (packages/api/src/code/inheritance.ts)
  • the legacy tool factory (api/app/clients/tools/util/handleTools.js)

The setting applies only when the stateful_code_sessions capability is on, the Agent has execute_code, its code machine is not an attached environment (or an id that matches no configured environment), and its stateful scope is allowed by statefulCodeSessions.allowedEnvironments. optsOutOfAttachedCodeEnvironment is unchanged.

Downloads: GET /api/files/code/download/... with no execution_profile uses the setting only when the capability is on; an explicit valid value still wins. createDownloadFallback now writes execution_profile=default for default-profile runs when the setting is stateful, so those links stay on the default service.

Agent builder: the agents endpoint config now carries codeDefaultProfile. When it is stateful, the Run Code dialog hides the "Stateful code sessions" switch (it would read off while code runs stateful), shows the stateful options directly under a "Stateful code sessions" heading styled like the dialog's "User control" heading (role="group", aria-labelledby), and sets the form value on (not dirty) so saving stores what runs. No new strings; without the setting the dialog is unchanged.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Documentation update (.env.example)

Testing

Run on this branch (base a7e6cc89f on dev; merges cleanly with current dev):

  • packages/api: npx jest src/code src/files/provision/callback.spec.ts src/agents/__tests__/initialize.test.ts — 1100 passed, 1 skipped (pre-existing); npx tsc --noEmit -p tsconfig.json exit 0; npm run build exit 0
  • client: npx jest src/components/SidePanel/Agents — 711 passed (new CodeSettings.statefulDefault.spec.tsx); packages/api src/endpoints/config — 109 passed (new endpoints.codeDefaultProfile.spec.ts); client tsc --noEmit exit 0
  • api: npx jest server/routes/files/files.test.js app/clients/tools/util/handleTools.test.js server/services/Endpoints/agents/initialize.spec.js server/services/Endpoints/agents/addedConvo.spec.js server/services/Files/Code/process.spec.js — 418 passed
  • eslint and prettier --check on every changed file: clean
  • New tests cover each criterion (unset parity, saved Agent with toggle off, normal chat, added Agent, subagent, provisioning label, downloads, capability off, attached and missing machines, disallowed scope, bad values); the gate tests were checked to fail with the new term removed.

Not run: a live deployment. We are verifying on a stateful-only staging deployment next.

Risk / compatibility

  • Unset or default: every changed expression reduces to the existing one; the download route makes no extra capability call.
  • Env setting rather than librechat.yaml: it describes the deployment's Code Interpreter (like LIBRECHAT_CODE_BASEURL_STATEFUL), not per-endpoint behaviour.
  • With stateful, Agents on an attached machine, on an unconfigured machine id, or whose stateful scope is not allowed keep their saved setting.
  • Fallback download links created before the setting is turned on carry no profile and resolve to stateful afterwards.

Checklist

…ult profile

Agent code gates and subagent workspace inheritance treat an agent as stateful when the
deployment's default profile is stateful, even if its saved flag is off or unset.
The stateful default profile setting no longer opts an agent out of code in a
no-workspace conversation unless stateful sessions are available and the
agent's resolved environment is attached. Without the capability, or without a
stateful route, the agent keeps code execution instead of silently losing it.
…n the setting is on

A download that names no execution profile now resolves it from
LIBRECHAT_CODE_DEFAULT_PROFILE, so it reaches the same Code API that produced the file.
Explicit profiles and the unset default are unchanged.
…er the stateful default

When LIBRECHAT_CODE_DEFAULT_PROFILE=stateful makes an agent stateful, an agent whose
code_environment_id no longer resolves to a configured environment now opts out of
attached environments in a "No workspace" conversation, the same as an agent with
stateful_code_sessions enabled. Previously it kept code and failed with
"Stateful code environment is not configured".
Describe the default/stateful values and that `stateful` overrides an
agent's saved "stateful sessions: off".
…ed or missing machine

The stateful default setting now applies to an agent only when its resolved
code machine is neither attached nor a dangling environment id. Agents on an
attached or missing machine keep their existing behavior, and the no-workspace
opt-out returns to its original rule.
Drive the provisioning callback and service from an agent's resolved code
context and assert the upload carries the stateful profile when the default
profile setting is stateful, and the default profile when it is unset.
Drive initialize with a flag-false code subagent and assert the lazy
descriptor reports statefulCodeSessions true under
LIBRECHAT_CODE_DEFAULT_PROFILE=stateful and false when it is unset.
…Is Off

A download link with no execution_profile now follows the configured default
profile only when the stateful_code_sessions capability is enabled; otherwise
it stays on the default Code API. An explicit execution_profile still wins.
…allowed

The stateful default setting no longer forces stateful sessions on an agent
whose scope is outside the allowed stateful code environments, so it can no
longer turn a working deployment into a policy error. Explicit stateful
agents are still refused. Resolver tests for the unset, default and
capability-off cases now use a machine the setting applies to.
…ault is stateful

Code output fallback links from default-profile runs carried no profile, so the
download route would resolve them to the configured default profile. Write
execution_profile=default explicitly in that case; links are unchanged when the
default profile is unset or default.
Copilot AI balanced review requested due to automatic review settings October 16, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

TomasPalsson and others added 6 commits October 16, 2026 13:03
Verify ephemeral Code-toggle agents and added-conversation agents saved with
stateful sessions off resolve the stateful profile when the deployment default
is stateful, and stay on the default profile when it is unset.
…Palsson/LibreChat into feat/stateful-code-default-profile
…eful

Advertise the deployment code profile on the agents endpoint config and have
the agent builder treat it as stateful: the switch is hidden and the
workspace options are shown, with the form value forced on.
@TomasPalsson
TomasPalsson marked this pull request as ready for review October 19, 2026 09:38
@codegraph-librechat codegraph-librechat Bot added the 🗺️ Sandbox Provisioning codegraph: the taxonomy area this belongs to (classifier, confidence ≥ 0.9) label Oct 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🗺️ Sandbox Provisioning codegraph: the taxonomy area this belongs to (classifier, confidence ≥ 0.9)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants