mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-17 05:20:55 +00:00
💤 fix: Preserve Dormant Workspace Bindings (#15877)
* fix: Preserve Disabled Workspace Bindings * fix: Resolve Restored Workspace State
This commit is contained in:
parent
f43301f818
commit
17c7b30631
4 changed files with 239 additions and 5 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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()}`,
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue