From 00fd15c356062f2346f65a1915af288600984128 Mon Sep 17 00:00:00 2001 From: Bohdan Triapitsyn Date: Mon, 29 Jun 2026 11:56:40 +0300 Subject: [PATCH] fix(vscode): avoid writing null agent config fields Omit unset agent fields on create Delete cleared agent fields in VS Code config updates Cover null field removal with a regression test --- packages/ui/src/stores/useAgentsStore.ts | 6 ++-- .../vscode/src/bridge-config-runtime.test.js | 33 +++++++++++++++++++ packages/vscode/src/opencodeConfig.ts | 33 ++++++++++++++++++- packages/web/server/lib/opencode/agents.js | 5 ++- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/packages/ui/src/stores/useAgentsStore.ts b/packages/ui/src/stores/useAgentsStore.ts index 9b642017..d9bd2077 100644 --- a/packages/ui/src/stores/useAgentsStore.ts +++ b/packages/ui/src/stores/useAgentsStore.ts @@ -177,6 +177,8 @@ const SLOW_HEALTH_POLL_BASE_MS = 800; const SLOW_HEALTH_POLL_INCREMENT_MS = 200; const SLOW_HEALTH_POLL_MAX_MS = 2000; +const hasValue = (value: T | null | undefined): value is T => value !== null && value !== undefined; + export interface AgentDraft { name: string; scope: AgentScope; @@ -345,8 +347,8 @@ export const useAgentsStore = create()( if (config.description) agentConfig.description = config.description; if (config.model) agentConfig.model = config.model; if (config.variant) agentConfig.variant = config.variant; - if (config.temperature !== undefined) agentConfig.temperature = config.temperature ?? null; - if (config.top_p !== undefined) agentConfig.top_p = config.top_p ?? null; + if (hasValue(config.temperature)) agentConfig.temperature = config.temperature; + if (hasValue(config.top_p)) agentConfig.top_p = config.top_p; if (config.prompt) agentConfig.prompt = config.prompt; if (config.permission) agentConfig.permission = config.permission; if (config.disable !== undefined) agentConfig.disable = config.disable; diff --git a/packages/vscode/src/bridge-config-runtime.test.js b/packages/vscode/src/bridge-config-runtime.test.js index fef9b3de..9cdabc23 100644 --- a/packages/vscode/src/bridge-config-runtime.test.js +++ b/packages/vscode/src/bridge-config-runtime.test.js @@ -52,6 +52,39 @@ afterEach(() => { const readJson = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8')); describe('VS Code config bridge plugin parity', () => { + test('removes agent fields when update payload sends null', async () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-vscode-agent-null-')); + tempRoots.push(root); + const ctx = createCtx(root); + const configDir = path.join(root, '.opencode'); + const configPath = path.join(configDir, 'opencode.json'); + fs.mkdirSync(configDir, { recursive: true }); + fs.writeFileSync(configPath, JSON.stringify({ + agent: { + build: { + variant: 'fast', + temperature: 0.3, + top_p: 0.8, + mode: 'subagent', + }, + }, + }, null, 2), 'utf8'); + + const updated = await handleConfigBridgeMessage({ + id: 'update-agent-null-fields', + type: 'api:config/agents', + payload: { + method: 'PATCH', + name: 'build', + directory: root, + body: { variant: null, temperature: null, top_p: null }, + }, + }, ctx, deps); + + expect(updated?.success).toBe(true); + expect(readJson(configPath).agent.build).toEqual({ mode: 'subagent' }); + }); + test('creates, lists, updates, and deletes project plugin entries', async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-vscode-plugins-')); tempRoots.push(root); diff --git a/packages/vscode/src/opencodeConfig.ts b/packages/vscode/src/opencodeConfig.ts index 9d9c8b5c..8478e1ab 100644 --- a/packages/vscode/src/opencodeConfig.ts +++ b/packages/vscode/src/opencodeConfig.ts @@ -1605,12 +1605,30 @@ export const createAgent = (agentName: string, config: Record, } // Extract scope and prompt from config - scope is only used for path determination, not written to file - const { prompt, scope: _ignored, ...frontmatter } = config as Record & { prompt?: unknown; scope?: unknown }; + const { prompt, scope: _ignored, ...rawFrontmatter } = config as Record & { prompt?: unknown; scope?: unknown }; void _ignored; // Scope is only used for path determination + const frontmatter = Object.fromEntries( + Object.entries(rawFrontmatter).filter(([, value]) => value !== null && value !== undefined) + ); writeMdFile(targetPath, frontmatter, typeof prompt === 'string' ? prompt : ''); resetAgentLookupCache(globalAgentLookupCache); }; +const deleteAgentJsonField = (config: Record, agentName: string, field: string): boolean => { + const agentMap = config.agent as Record | undefined; + const current = agentMap?.[agentName] as Record | undefined; + if (!agentMap || !current || !(field in current)) return false; + + delete current[field]; + if (Object.keys(current).length === 0) { + delete agentMap[agentName]; + } + if (Object.keys(agentMap).length === 0) { + delete config.agent; + } + return true; +}; + export const updateAgent = (agentName: string, updates: Record, workingDirectory?: string) => { ensureDirs(); @@ -1757,6 +1775,19 @@ export const updateAgent = (agentName: string, updates: Record, const hasMdField = Boolean(mdData?.frontmatter?.[field] !== undefined); const hasJsonField = Boolean(jsonSection?.[field] !== undefined); + if (value === null) { + if (hasMdField && mdData) { + delete mdData.frontmatter[field]; + mdModified = true; + } + + if (hasJsonField && deleteAgentJsonField(config, agentName, field)) { + jsonModified = true; + } + + continue; + } + // JSON takes precedence over md, so update JSON first if field exists there if (hasJsonField) { if (!config.agent) config.agent = {}; diff --git a/packages/web/server/lib/opencode/agents.js b/packages/web/server/lib/opencode/agents.js index 09b0e478..d2acff89 100644 --- a/packages/web/server/lib/opencode/agents.js +++ b/packages/web/server/lib/opencode/agents.js @@ -409,7 +409,10 @@ function createAgent(agentName, config, workingDirectory, scope) { targetScope = AGENT_SCOPE.USER; } - const { prompt, scope: _scopeFromConfig, ...frontmatter } = config; + const { prompt, scope: _scopeFromConfig, ...rawFrontmatter } = config; + const frontmatter = Object.fromEntries( + Object.entries(rawFrontmatter).filter(([, value]) => value !== null && value !== undefined) + ); writeMdFile(targetPath, frontmatter, prompt || ''); console.log(`Created new agent: ${agentName} (scope: ${targetScope}, path: ${targetPath})`);