Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(remote-ssh): 清理受管 MCP 块外的 enabled/timeout 孤儿标量,反复刷新不再撞 TOML duplicate-key(#4776) (#4782)

FicoWangf97842e11613
diff --git a/apps/desktop/src/main/remote-ssh/__tests__/codex-remote-mcp.test.ts b/apps/desktop/src/main/remote-ssh/__tests__/codex-remote-mcp.test.ts
index d71488ec50..92b4e7bc4d 100644
--- a/apps/desktop/src/main/remote-ssh/__tests__/codex-remote-mcp.test.ts
+++ b/apps/desktop/src/main/remote-ssh/__tests__/codex-remote-mcp.test.ts
@@ -5,6 +5,7 @@
*/
import { describe, expect, it, vi, beforeAll } from 'vitest';
+import { parse as parseToml } from 'smol-toml';
import fs from 'node:fs';
import path from 'node:path';
@@ -306,6 +307,129 @@ describe('mergeManagedMcpBlock', () => {
});
});
+describe('mergeManagedMcpBlock helper-scalar residue (#4776)', () => {
+ const HELPER_SERVERS = ['cindy_orca', 'cindy_helper'];
+ const block = renderManagedMcpBlock({ remotePort: 47921, serverNames: HELPER_SERVERS, tokenFingerprint: 'fp-new' });
+ const BEGIN = '# >>> cindy-remote-mcp (managed, do not edit) >>>';
+ const END = '# <<< cindy-remote-mcp <<<';
+ /** Everything that is not inside the managed markers. */
+ const outsideManaged = (text: string): string => {
+ const start = text.indexOf(BEGIN);
+ const end = text.indexOf(END);
+ if (start === -1 || end === -1) return text;
+ return text.slice(0, start) + text.slice(end + END.length);
+ };
+ const helperScalar = /^(enabled|startup_timeout_sec|tool_timeout_sec)\s*=/m;
+ const USER_LINES = [
+ 'model = "gpt-5.5"',
+ '',
+ '[projects."/home/u/repo"]',
+ 'trust_level = "trusted"',
+ '',
+ '[mcp_servers.my_custom]',
+ 'url = "http://127.0.0.1:22222/mcp/my_custom"',
+ '',
+ '[history]',
+ 'persistence = "save-all"',
+ '',
+ ];
+ /** The pre-fix layout: the helper table was rendered without its disabled flag and timeouts, which sat behind the end marker instead. */
+ const legacyManaged = (orphanCopies: number): string[] => [
+ BEGIN,
+ '# cindy-token-fingerprint: fp-old',
+ '[mcp_servers.cindy_orca]',
+ 'url = "http://127.0.0.1:47000/mcp/cindy_orca"',
+ 'bearer_token_env_var = "LIZI_MCP_TOKEN"',
+ '',
+ '[mcp_servers.cindy_helper]',
+ 'url = "http://127.0.0.1:47000/mcp/cindy_helper"',
+ 'bearer_token_env_var = "LIZI_MCP_TOKEN"',
+ END,
+ ...Array.from({ length: orphanCopies }, () => [
+ 'enabled = false',
+ 'startup_timeout_sec = 600',
+ 'tool_timeout_sec = 600',
+ ]).flat(),
+ '',
+ ];
+ const expectUserConfigKept = (next: string) => {
+ const parsed = parseToml(next) as Record<string, any>;
+ expect(parsed.model).toBe('gpt-5.5');
+ expect(parsed.projects['/home/u/repo'].trust_level).toBe('trusted');
+ expect(parsed.mcp_servers.my_custom.url).toBe('http://127.0.0.1:22222/mcp/my_custom');
+ expect(parsed.history.persistence).toBe('save-all');
+ expect(parsed.mcp_servers.cindy_helper.enabled).toBe(false);
+ expect(parsed.mcp_servers.cindy_helper.startup_timeout_sec).toBe(600);
+ expect(parsed.mcp_servers.cindy_orca.url).toBe('http://127.0.0.1:47921/mcp/cindy_orca');
+ return parsed;
+ };
+
+ it('stays valid TOML across five refreshes starting from the pre-fix layout', () => {
+ let config = [...USER_LINES, ...legacyManaged(1)].join('\n');
+ for (let refresh = 0; refresh < 5; refresh += 1) {
+ const { next } = mergeManagedMcpBlock(config, block, { serverNames: HELPER_SERVERS });
+ expect(() => parseToml(next), `refresh ${refresh}`).not.toThrow();
+ expect(next.match(/\[mcp_servers\.cindy_helper\]/g), `refresh ${refresh}`).toHaveLength(1);
+ expect(next.match(/# >>> cindy-remote-mcp/g), `refresh ${refresh}`).toHaveLength(1);
+ expect(outsideManaged(next), `refresh ${refresh}`).not.toMatch(helperScalar);
+ expectUserConfigKept(next);
+ config = next;
+ }
+ // Once converged, a further refresh is a no-op.
+ expect(mergeManagedMcpBlock(config, block, { serverNames: HELPER_SERVERS }).changed).toBe(false);
+ });
+
+ it('recovers a config already broken by duplicated orphan helper scalars', () => {
+ const broken = [...USER_LINES, ...legacyManaged(2)].join('\n');
+ // Two orphan copies are what the user hits: duplicate keys, codex refuses to start.
+ expect(() => parseToml(broken)).toThrow();
+ const { next, changed, strippedUserServers } = mergeManagedMcpBlock(broken, block, { serverNames: HELPER_SERVERS });
+ expect(changed).toBe(true);
+ expect(strippedUserServers).toEqual([]);
+ expect(() => parseToml(next)).not.toThrow();
+ expect(next.match(/\[mcp_servers\.cindy_helper\]/g)).toHaveLength(1);
+ expect(outsideManaged(next)).not.toMatch(helperScalar);
+ expectUserConfigKept(next);
+ });
+
+ it('recovers orphan helper scalars left behind an orphan begin marker', () => {
+ // An interrupted write leaves begin without end; the helper flag must count as managed residue too,
+ // or it and the timeouts behind it survive as user content.
+ const interrupted = [
+ ...USER_LINES,
+ BEGIN,
+ '# cindy-token-fingerprint: fp-old',
+ '[mcp_servers.cindy_helper]',
+ 'url = "http://127.0.0.1:47000/mcp/cindy_helper"',
+ 'bearer_token_env_var = "LIZI_MCP_TOKEN"',
+ 'enabled = false',
+ 'startup_timeout_sec = 600',
+ 'tool_timeout_sec = 600',
+ '',
+ ].join('\n');
+ const { next } = mergeManagedMcpBlock(interrupted, block, { serverNames: HELPER_SERVERS });
+ expect(() => parseToml(next)).not.toThrow();
+ expect(outsideManaged(next)).not.toMatch(helperScalar);
+ expect(next.match(/\[mcp_servers\.cindy_helper\]/g)).toHaveLength(1);
+ expectUserConfigKept(next);
+ });
+
+ it('does not strip user scalars that merely follow the managed block after a blank line and a header', () => {
+ const existing = [
+ ...USER_LINES,
+ ...legacyManaged(0),
+ '[mcp_servers.other_tool]',
+ 'enabled = false',
+ 'startup_timeout_sec = 30',
+ '',
+ ].join('\n');
+ const { next } = mergeManagedMcpBlock(existing, block, { serverNames: HELPER_SERVERS });
+ const parsed = expectUserConfigKept(next);
+ expect(parsed.mcp_servers.other_tool.enabled).toBe(false);
+ expect(parsed.mcp_servers.other_tool.startup_timeout_sec).toBe(30);
+ });
+});
+
describe('ensureRemoteCodexMcpBridge per-host serialization', () => {
it('serializes concurrent ensures for the same host (second runs after first settles)', async () => {
// P1 回归:并发 ensure 同一 host 会互相交错 forward arm 与 config 写入;
diff --git a/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts b/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts
index 8c2e56acc8..232020b378 100644
--- a/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts
+++ b/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts
@@ -308,7 +308,16 @@ function isManagedResidueLine(line: string): boolean {
if (t.startsWith(TOKEN_FINGERPRINT_PREFIX)) return true;
const header = parseTableHeaderKey(t);
if (header && header[0] === 'mcp_servers') return true;
- return /^(url|bearer_token_env_var|startup_timeout_sec|tool_timeout_sec)\s*=/.test(t);
+ return isManagedScalarLine(t);
+}
+
+/**
+ * 受管 table 内我们自己写的标量行 (trim 后)。`enabled` 是 cindy_helper 的禁用标记,
+ * 与两个 timeout 同属受管段; 旧版本没把它当受管内容, 刷新时它连同其后的 timeout
+ * 会留在 end 标记之外, 累积两份就是 TOML duplicate-key, codex 起不来 (#4776)。
+ */
+function isManagedScalarLine(t: string): boolean {
+ return /^(url|bearer_token_env_var|enabled|startup_timeout_sec|tool_timeout_sec)\s*=/.test(t);
}
/**
@@ -317,6 +326,8 @@ function isManagedResidueLine(line: string): boolean {
* marker 文本的内容误判成管理段起点;
* - managed 段原位剥除后在文末重建 (TOML 与顺序无关, 幂等收敛);
* orphan begin (缺 end) 只剥连续的 managed 残留形态行, 不波及用户配置;
+ * end 之后紧跟的受管标量 (旧版本留下的 enabled / timeout 孤儿) 一并剥除,
+ * 同样遇到 header 或其他 key 即停;
* - managed 段之外用户手写的同名 `[mcp_servers.<name>]` table 一并剥离
* (重复 table 是非法 TOML, codex 会直接起不来), 由 managed 段接管,
* 名字经 strippedUserServers 返回给调用方记 warn;
@@ -335,15 +346,18 @@ export function mergeManagedMcpBlock(
const stripped = new Set<string>();
const kept: string[] = [];
let inManaged = false;
+ let afterManagedEnd = false;
let inUserBlock = false;
for (const line of existing.split('\n')) {
const t = line.trim();
if (t === MANAGED_BEGIN) {
inManaged = true;
+ afterManagedEnd = false;
continue;
}
if (t === MANAGED_END) {
inManaged = false;
+ afterManagedEnd = true;
continue;
}
if (inManaged) {
@@ -351,6 +365,13 @@ export function mergeManagedMcpBlock(
// orphan begin: 用户内容开始, 退出 managed 状态并保留该行。
inManaged = false;
}
+ if (afterManagedEnd) {
+ // end 标记之后紧跟的受管标量是旧版本写出的孤儿 (见 isManagedScalarLine):
+ // TOML 里它们仍归属 end 之前的受管 table, 与重建段重复。只剥这一段连续的
+ // 标量/空行, 遇到任何 header 或其他 key 即停, 用户配置不受影响。
+ if (t === '' || isManagedScalarLine(t)) continue;
+ afterManagedEnd = false;
+ }
const hit = userMcpServerHeader(t, serverNames);
if (hit) {
stripped.add(hit);