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: Stop Approval Pauses From Unsealing Code Environments (#15963)

Danny Avilaed95a9b8029a
diff --git a/api/server/controllers/agents/client.codeDecision.spec.js b/api/server/controllers/agents/client.codeDecision.spec.js
new file mode 100644
index 0000000000..fc38cea553
--- /dev/null
+++ b/api/server/controllers/agents/client.codeDecision.spec.js
@@ -0,0 +1,50 @@
+const AgentClient = require('./client');
+
+describe('AgentClient code environment save options', () => {
+ const mac = { environmentId: 'code-mac', workspaceId: 'primary' };
+ const vm = { environmentId: 'code-vm', workspaceId: 'primary' };
+ const conversationId = 'conversation-1';
+
+ const buildClient = (resolvedConversation) => {
+ const client = Object.create(AgentClient.prototype);
+ client.options = {
+ req: {
+ body: { conversationId },
+ config: {},
+ resolvedConversation,
+ _codeEnvironmentDecision: { mode: 'attached', codeWorkspaces: [mac] },
+ },
+ endpoint: 'agents',
+ agent: { id: 'agent_1', provider: 'openai' },
+ };
+ return client;
+ };
+
+ it('recognizes an existing conversation before the client initializes its ID', () => {
+ const client = buildClient({
+ conversationId,
+ codeEnvironmentMode: 'attached',
+ codeWorkspaces: [vm],
+ });
+
+ expect(client.conversationId).toBeUndefined();
+ const initial = client.getSaveOptions();
+ expect(initial).not.toHaveProperty('codeEnvironmentMode');
+ expect(initial).not.toHaveProperty('codeWorkspaces');
+
+ client.conversationId = conversationId;
+ expect(client.getSaveOptions()).toEqual(initial);
+ });
+
+ it('seeds a new conversation and omits its decision after the row becomes available', () => {
+ const client = buildClient(null);
+ const initial = client.getSaveOptions();
+ expect(initial).toMatchObject({ codeEnvironmentMode: 'attached', codeWorkspaces: [mac] });
+
+ client.conversationId = conversationId;
+ client.options.req.resolvedConversation = { conversationId, ...initial };
+ const paused = client.getSaveOptions();
+ expect(paused).not.toHaveProperty('codeEnvironmentMode');
+ expect(paused).not.toHaveProperty('codeWorkspaces');
+ });
+});
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js
index c98ec0c583..9398181489 100644
--- a/api/server/controllers/agents/client.js
+++ b/api/server/controllers/agents/client.js
@@ -1943,7 +1943,7 @@ class AgentClient extends BaseClient {
agentsEConfig?.toolApproval?.enabled !== false,
);
const persistedCodeEnvironmentDecision = resolvePersistableCodeEnvironmentDecision({
- conversationId: this.conversationId,
+ conversationId: this.options.req.body.conversationId,
decision: this.options.req._codeEnvironmentDecision,
conversation: this.options.req.resolvedConversation,
requested: this.options.req.body,
diff --git a/api/server/controllers/agents/client.test.js b/api/server/controllers/agents/client.test.js
index 852487c2de..c084201298 100644
--- a/api/server/controllers/agents/client.test.js
+++ b/api/server/controllers/agents/client.test.js
@@ -107,7 +107,7 @@ describe('AgentClient code approval persistence', () => {
endpoint: EModelEndpoint.agents,
agent: { id: 'attached-agent' },
req: {
- body: {},
+ body: { conversationId: 'convo-1' },
_codeEnvironmentDecision: {
mode: 'attached',
codeWorkspaces: [{ environmentId: 'mac', workspaceId: 'primary' }],
@@ -135,7 +135,7 @@ describe('AgentClient code approval persistence', () => {
endpoint: EModelEndpoint.agents,
agent: { id: 'attached-agent' },
req: {
- body: {},
+ body: { conversationId: 'convo-1' },
_codeEnvironmentDecision: { mode: 'attached', codeWorkspaces },
resolvedConversation: { conversationId: 'convo-1', codeWorkspaces },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
diff --git a/packages/data-schemas/src/methods/conversation.spec.ts b/packages/data-schemas/src/methods/conversation.spec.ts
index 00e3dae652..5fa9ae689f 100644
--- a/packages/data-schemas/src/methods/conversation.spec.ts
+++ b/packages/data-schemas/src/methods/conversation.spec.ts
@@ -1079,6 +1079,88 @@ describe('Conversation Operations', () => {
});
});
+ describe('code environment persistence during ordinary saves', () => {
+ const mac = { environmentId: 'code-mac', workspaceId: 'primary' };
+ const vm = { environmentId: 'code-vm', workspaceId: 'primary' };
+ const userId = 'user123';
+ const unsetFields = { codeEnvironmentMode: 1, codeWorkspaces: 1 };
+
+ it.each([
+ { codeEnvironmentMode: 'attached', codeWorkspaces: [mac] },
+ { codeEnvironmentMode: 'without_attached' },
+ ])(
+ 'preserves $codeEnvironmentMode through an approval pause and later saves',
+ async (decision) => {
+ const conversationId = uuidv4();
+ await saveConvo({ userId }, { conversationId, ...decision });
+
+ for (const title of ['Approval required', 'Resumed', 'Completed']) {
+ await saveConvo({ userId }, { conversationId, title }, { unsetFields });
+ const stored = await getConvo(userId, conversationId);
+ expect(stored?.codeEnvironmentMode).toBe(decision.codeEnvironmentMode);
+ expect(stored?.codeWorkspaces).toEqual(decision.codeWorkspaces);
+ expect(stored?.title).toBe(title);
+ }
+ },
+ );
+
+ it('cannot overwrite an explicit move with a stale turn-start snapshot', async () => {
+ const conversationId = uuidv4();
+ const decision = { codeEnvironmentMode: 'attached' as const, codeWorkspaces: [mac] };
+ await saveConvo({ userId }, { conversationId, ...decision });
+ await methods.replaceConvoCodeEnvironmentDecision({
+ user: userId,
+ conversationId,
+ expected: decision,
+ codeWorkspaces: [vm],
+ });
+
+ await saveConvo({ userId }, { conversationId, ...decision });
+ expect((await getConvo(userId, conversationId))?.codeWorkspaces).toEqual([vm]);
+ await saveConvo(
+ { userId },
+ { conversationId, codeEnvironmentMode: null, codeWorkspaces: [] },
+ );
+ const stored = await getConvo(userId, conversationId);
+ expect(stored?.codeEnvironmentMode).toBe('attached');
+ expect(stored?.codeWorkspaces).toEqual([vm]);
+ });
+
+ it('seeds imported decisions without letting repeated imports undo a move', async () => {
+ const conversationId = uuidv4();
+ const decision = { codeEnvironmentMode: 'attached' as const, codeWorkspaces: [mac] };
+ const imported = { conversationId, user: userId, ...decision };
+ await methods.bulkSaveConvos([imported]);
+ expect((await getConvo(userId, conversationId))?.codeWorkspaces).toEqual([mac]);
+ await methods.replaceConvoCodeEnvironmentDecision({
+ user: userId,
+ conversationId,
+ expected: decision,
+ codeWorkspaces: [vm],
+ });
+ await methods.bulkSaveConvos([imported]);
+ const stored = await getConvo(userId, conversationId);
+ expect(stored?.codeEnvironmentMode).toBe('attached');
+ expect(stored?.codeWorkspaces).toEqual([vm]);
+ });
+
+ it.each([{}, { codeWorkspaces: [mac] }])(
+ 'preserves a legacy decision: %j',
+ async (decision) => {
+ const conversationId = uuidv4();
+ await Conversation.collection.insertOne({ conversationId, user: userId, ...decision });
+ await saveConvo(
+ { userId },
+ { conversationId, codeEnvironmentMode: 'attached', codeWorkspaces: [vm] },
+ { unsetFields },
+ );
+ const stored = await getConvo(userId, conversationId);
+ expect(stored?.codeEnvironmentMode).toBeUndefined();
+ expect(stored?.codeWorkspaces).toEqual(decision.codeWorkspaces);
+ },
+ );
+ });
+
describe('replaceConvoCodeEnvironmentDecision', () => {
const anchor = new Date('2026-09-12T11:22:22.976Z');
const mac = { environmentId: 'code-mac', workspaceId: 'primary' };
diff --git a/packages/data-schemas/src/methods/conversation.ts b/packages/data-schemas/src/methods/conversation.ts
index 10ad260b8d..36b7f365f2 100644
--- a/packages/data-schemas/src/methods/conversation.ts
+++ b/packages/data-schemas/src/methods/conversation.ts
@@ -2181,6 +2181,15 @@ export function createConversationMethods(
delete update.isTemporary;
delete update.expiredAt;
delete update.initial_agent_id;
+ /** Ordinary saves may seed a decision, but only an explicit move may replace it. */
+ const decisionOnInsert = {
+ ...(convo.codeEnvironmentMode != null && {
+ codeEnvironmentMode: convo.codeEnvironmentMode,
+ }),
+ ...(convo.codeWorkspaces != null && { codeWorkspaces: convo.codeWorkspaces }),
+ };
+ delete update.codeEnvironmentMode;
+ delete update.codeWorkspaces;
stripActorCheckpointFields(update);
if (appendMessageIds == null) {
update.messages = await getMessages({ conversationId, user: userId }, '_id');
@@ -2189,6 +2198,8 @@ export function createConversationMethods(
}
const unsetFields: Record<string, number> = { ...(metadata?.unsetFields ?? {}) };
delete unsetFields.initial_agent_id;
+ delete unsetFields.codeEnvironmentMode;
+ delete unsetFields.codeWorkspaces;
stripActorCheckpointFields(unsetFields);
if (Object.prototype.hasOwnProperty.call(update, 'chatProjectId') && update.chatProjectId) {
@@ -2318,6 +2329,7 @@ export function createConversationMethods(
: createdAtOnInsert;
operation.$setOnInsert = {
initial_agent_id: initialAgentId,
+ ...decisionOnInsert,
...retentionOnInsert,
...(createdAtForInsert ? { createdAt: createdAtForInsert } : {}),
};
@@ -2642,7 +2654,7 @@ export function createConversationMethods(
const affectedProjectStats = new Map<string, { user: string; projectId: string }>();
const bulkOps = conversations.map((convo) => {
- const sanitized = { ...convo };
+ const { codeEnvironmentMode, codeWorkspaces, ...sanitized } = convo;
delete sanitized.initial_agent_id;
stripActorCheckpointFields(sanitized);
if (typeof sanitized.user === 'string' && typeof sanitized.chatProjectId === 'string') {
@@ -2676,7 +2688,11 @@ export function createConversationMethods(
},
update: {
$set: sanitized,
- $setOnInsert: { initial_agent_id: null },
+ $setOnInsert: {
+ initial_agent_id: null,
+ ...(codeEnvironmentMode != null && { codeEnvironmentMode }),
+ ...(codeWorkspaces != null && { codeWorkspaces }),
+ },
},
upsert: true,
timestamps: false,