Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

💤 fix: Preserve Dormant Workspace Bindings (#15877)

Danny Avila17c7b3063192
diff --git a/api/server/controllers/agents/v1.js b/api/server/controllers/agents/v1.js
index 635387f383..e34647cf2b 100644
--- a/api/server/controllers/agents/v1.js
+++ b/api/server/controllers/agents/v1.js
@@ -38,7 +38,9 @@ const {
isContentTraversalLimitError,
resolveCanonicalFileReferences,
reportLocatorTraversalFailure,
+ isActiveAgentWorkspaceConfiguration,
reconcileAgentWorkspaceDefault,
+ resolveAgentWorkspaceRestoreConfiguration,
shouldValidateAgentWorkspaceDefaultBinding,
validateAgentWorkspaceDefaultBinding,
} = require('@librechat/api');
@@ -1463,6 +1465,7 @@ const duplicateAgentHandler = async (req, res) => {
author: userId,
});
if (
+ isActiveAgentWorkspaceConfiguration(newAgentData) &&
!validateStatefulCodeEnvironment(
req,
res,
@@ -2070,16 +2073,22 @@ const revertAgentVersionHandler = async (req, res) => {
}
const revertVersion = existingAgent.versions?.[version_index];
+ const restoredWorkspaceConfiguration = revertVersion
+ ? resolveAgentWorkspaceRestoreConfiguration({
+ version: revertVersion,
+ current: existingAgent,
+ })
+ : undefined;
if (
- revertVersion &&
+ isActiveAgentWorkspaceConfiguration(restoredWorkspaceConfiguration) &&
!validateStatefulCodeEnvironment(
req,
res,
- revertVersion.stateful_code_sessions,
- revertVersion.stateful_code_environment,
- revertVersion.code_environment_id,
+ restoredWorkspaceConfiguration.stateful_code_sessions,
+ restoredWorkspaceConfiguration.stateful_code_environment,
+ restoredWorkspaceConfiguration.code_environment_id,
false,
- revertVersion.code_workspace_id,
+ restoredWorkspaceConfiguration.code_workspace_id,
)
) {
return;
diff --git a/api/server/controllers/agents/v1.spec.js b/api/server/controllers/agents/v1.spec.js
index a782d3f5b4..d96af401fc 100644
--- a/api/server/controllers/agents/v1.spec.js
+++ b/api/server/controllers/agents/v1.spec.js
@@ -3003,6 +3003,67 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
expect(mockRes.json.mock.calls[0][0].agent.tool_options).toEqual({});
});
+ test('duplicateAgentHandler preserves a disabled stale workspace binding', async () => {
+ const sourceAgent = await Agent.create({
+ id: `agent_${uuidv4()}`,
+ name: 'Disabled BYOM Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ author: mockReq.user.id,
+ stateful_code_sessions: false,
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ });
+ jest.spyOn(db, 'getActions').mockResolvedValueOnce([]);
+ mockReq.config = {
+ endpoints: {
+ agents: {
+ statefulCodeSessions: { environments: [] },
+ },
+ },
+ };
+ mockReq.params.id = sourceAgent.id;
+
+ await duplicateAgentHandler(mockReq, mockRes);
+
+ expect(mockRes.status).toHaveBeenCalledWith(201);
+ expect(mockRes.json.mock.calls[0][0].agent).toEqual(
+ expect.objectContaining({
+ stateful_code_sessions: false,
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ }),
+ );
+ });
+
+ test('duplicateAgentHandler rejects an active stale workspace binding', async () => {
+ const sourceAgent = await Agent.create({
+ id: `agent_${uuidv4()}`,
+ name: 'Active BYOM Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ author: mockReq.user.id,
+ stateful_code_sessions: true,
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ });
+ mockReq.config = {
+ endpoints: {
+ agents: {
+ statefulCodeSessions: { environments: [] },
+ },
+ },
+ };
+ mockReq.params.id = sourceAgent.id;
+
+ await duplicateAgentHandler(mockReq, mockRes);
+
+ expect(mockRes.status).toHaveBeenCalledWith(400);
+ expect(mockRes.json).toHaveBeenCalledWith({
+ error: 'Code workspace defaults require an explicit attached code environment',
+ });
+ });
+
test('revertAgentVersionHandler removes restored programmatic options without Code Interpreter', async () => {
const agent = await Agent.create({
id: `agent_${uuidv4()}`,
@@ -3031,6 +3092,83 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
expect(mockRes.json.mock.calls[0][0].tool_options).toEqual({});
});
+ test('revertAgentVersionHandler restores a disabled stale workspace binding', async () => {
+ const agent = await Agent.create({
+ id: `agent_${uuidv4()}`,
+ name: 'Current Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ author: mockReq.user.id,
+ versions: [
+ {
+ name: 'Disabled Historical BYOM Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ stateful_code_sessions: false,
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ },
+ ],
+ });
+ mockReq.config = {
+ endpoints: {
+ agents: {
+ statefulCodeSessions: { environments: [] },
+ },
+ },
+ };
+ mockReq.params.id = agent.id;
+ mockReq.body = { version_index: 0 };
+
+ await revertAgentVersionHandler(mockReq, mockRes);
+
+ expect(mockRes.status).not.toHaveBeenCalledWith(400);
+ const persisted = await Agent.findOne({ id: agent.id }).lean();
+ expect(persisted).toEqual(
+ expect.objectContaining({
+ stateful_code_sessions: false,
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ }),
+ );
+ });
+
+ test('revertAgentVersionHandler rejects a stale binding that inherits active sessions', async () => {
+ const agent = await Agent.create({
+ id: `agent_${uuidv4()}`,
+ name: 'Current Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ author: mockReq.user.id,
+ stateful_code_sessions: true,
+ versions: [
+ {
+ name: 'Historical BYOM Agent',
+ provider: 'openai',
+ model: 'gpt-4',
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ },
+ ],
+ });
+ mockReq.config = {
+ endpoints: {
+ agents: {
+ statefulCodeSessions: { environments: [] },
+ },
+ },
+ };
+ mockReq.params.id = agent.id;
+ mockReq.body = { version_index: 0 };
+
+ await revertAgentVersionHandler(mockReq, mockRes);
+
+ expect(mockRes.status).toHaveBeenCalledWith(400);
+ expect(mockRes.json).toHaveBeenCalledWith({
+ error: 'Code workspace defaults require an explicit attached code environment',
+ });
+ });
+
test('revertAgentVersionHandler does not update unchanged tool options', async () => {
const agent = await Agent.create({
id: `agent_${uuidv4()}`,
diff --git a/packages/api/src/agents/workspace.spec.ts b/packages/api/src/agents/workspace.spec.ts
index e1caeeb25c..81f3f1a949 100644
--- a/packages/api/src/agents/workspace.spec.ts
+++ b/packages/api/src/agents/workspace.spec.ts
@@ -1,9 +1,56 @@
import {
AGENT_WORKSPACE_ATTACHED_ENVIRONMENT_ERROR,
+ isActiveAgentWorkspaceConfiguration,
reconcileAgentWorkspaceDefault,
+ resolveAgentWorkspaceRestoreConfiguration,
validateAgentWorkspaceDefaultBinding,
} from './workspace';
+describe('restored agent workspace configuration', () => {
+ it('inherits persistent session fields and clears omitted binding fields', () => {
+ const restored = resolveAgentWorkspaceRestoreConfiguration({
+ version: {},
+ current: {
+ stateful_code_sessions: true,
+ stateful_code_environment: 'conversation',
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ },
+ });
+
+ expect(restored).toEqual({
+ stateful_code_sessions: true,
+ stateful_code_environment: 'conversation',
+ code_environment_id: undefined,
+ code_workspace_id: undefined,
+ });
+ expect(isActiveAgentWorkspaceConfiguration(restored)).toBe(true);
+ });
+
+ it('uses explicit historical session and binding fields', () => {
+ const restored = resolveAgentWorkspaceRestoreConfiguration({
+ version: {
+ stateful_code_sessions: false,
+ stateful_code_environment: 'user',
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ },
+ current: {
+ stateful_code_sessions: true,
+ stateful_code_environment: 'conversation',
+ },
+ });
+
+ expect(restored).toEqual({
+ stateful_code_sessions: false,
+ stateful_code_environment: 'user',
+ code_environment_id: 'removed-vm',
+ code_workspace_id: 'project-a',
+ });
+ expect(isActiveAgentWorkspaceConfiguration(restored)).toBe(false);
+ });
+});
+
describe('reconcileAgentWorkspaceDefault', () => {
it('clears a stale default when the attached environment changes', () => {
expect(
diff --git a/packages/api/src/agents/workspace.ts b/packages/api/src/agents/workspace.ts
index 6cebbd41ae..2d8d195149 100644
--- a/packages/api/src/agents/workspace.ts
+++ b/packages/api/src/agents/workspace.ts
@@ -9,9 +9,49 @@ interface AgentWorkspaceEnvironment {
type?: string;
}
+interface AgentWorkspaceConfiguration {
+ stateful_code_sessions?: boolean;
+ stateful_code_environment?: string | null;
+ code_environment_id?: string | null;
+ code_workspace_id?: string;
+}
+
export const AGENT_WORKSPACE_ATTACHED_ENVIRONMENT_ERROR =
'Code workspace defaults require an explicit attached code environment';
+export function isActiveAgentWorkspaceConfiguration(
+ configuration?: AgentWorkspaceConfiguration,
+): boolean {
+ return configuration?.stateful_code_sessions === true;
+}
+
+/** Resolve workspace fields according to the version restore persistence contract. */
+export function resolveAgentWorkspaceRestoreConfiguration({
+ version,
+ current,
+}: {
+ version: AgentWorkspaceConfiguration;
+ current: AgentWorkspaceConfiguration;
+}): AgentWorkspaceConfiguration {
+ return {
+ stateful_code_sessions: Object.prototype.hasOwnProperty.call(version, 'stateful_code_sessions')
+ ? version.stateful_code_sessions
+ : current.stateful_code_sessions,
+ stateful_code_environment: Object.prototype.hasOwnProperty.call(
+ version,
+ 'stateful_code_environment',
+ )
+ ? version.stateful_code_environment
+ : current.stateful_code_environment,
+ code_environment_id: Object.prototype.hasOwnProperty.call(version, 'code_environment_id')
+ ? version.code_environment_id
+ : undefined,
+ code_workspace_id: Object.prototype.hasOwnProperty.call(version, 'code_workspace_id')
+ ? version.code_workspace_id
+ : undefined,
+ };
+}
+
export function shouldValidateAgentWorkspaceDefaultBinding({
workspaceId,
environmentId,