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): 剥除旧合并堆在 begin 标记之前的重复受管标量,config.toml 不再因重复键无法恢复(#4946) (#4947)

FicoWang06a349f97843
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 92b4e7bc4d..f4d6eba28f 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
@@ -364,6 +364,87 @@ describe('mergeManagedMcpBlock helper-scalar residue (#4776)', () => {
return parsed;
};
+ /**
+ * The *actual* output of the old merge after N refreshes (#4946): it stopped stripping the
+ * managed table at `enabled`, kept that scalar group beneath whatever table preceded the
+ * managed block (here the project table) and appended the rebuilt block at EOF — so the
+ * orphan groups stack up *before* BEGIN, not after END.
+ */
+ const legacyPrefixResidue = (orphanCopies: number): string[] => [
+ 'model = "gpt-5.5"',
+ '',
+ '[projects."/home/u/repo"]',
+ 'trust_level = "trusted"',
+ '',
+ ...Array.from({ length: orphanCopies }, () => [
+ 'enabled = false',
+ 'startup_timeout_sec = 600',
+ 'tool_timeout_sec = 600',
+ '',
+ ]).flat(),
+ 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"',
+ 'enabled = false',
+ 'startup_timeout_sec = 600',
+ 'tool_timeout_sec = 600',
+ END,
+ '',
+ ];
+
+ it('recovers duplicated helper scalars the old merge left before the begin marker (#4946)', () => {
+ const broken = legacyPrefixResidue(3).join('\n');
+ // Three old refreshes → three copies under the project table → duplicate keys, codex refuses to start.
+ expect(() => parseToml(broken)).toThrow();
+ let config = broken;
+ for (let refresh = 0; refresh < 5; refresh += 1) {
+ const { next, strippedUserServers } = mergeManagedMcpBlock(config, block, { serverNames: HELPER_SERVERS });
+ expect(strippedUserServers, `refresh ${refresh}`).toEqual([]);
+ expect(() => parseToml(next), `refresh ${refresh}`).not.toThrow();
+ 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');
+ // The orphan groups never belonged to the project table; none of them may survive there.
+ expect(parsed.projects['/home/u/repo'].enabled, `refresh ${refresh}`).toBeUndefined();
+ expect(parsed.projects['/home/u/repo'].startup_timeout_sec, `refresh ${refresh}`).toBeUndefined();
+ expect(parsed.mcp_servers.cindy_helper.enabled).toBe(false);
+ expect(next.match(/# >>> cindy-remote-mcp/g), `refresh ${refresh}`).toHaveLength(1);
+ config = next;
+ }
+ expect(mergeManagedMcpBlock(config, block, { serverNames: HELPER_SERVERS }).changed).toBe(false);
+ });
+
+ it('keeps a single legitimate user scalar group that sits before the begin marker (#4946)', () => {
+ // A user-authored table right before the managed block: unique keys, valid TOML — must not be touched.
+ const config = [
+ 'model = "gpt-5.5"',
+ '',
+ '[mcp_servers.my_custom]',
+ 'url = "http://127.0.0.1:22222/mcp/my_custom"',
+ 'enabled = true',
+ 'startup_timeout_sec = 30',
+ '',
+ 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"',
+ END,
+ '',
+ ].join('\n');
+ const { next } = mergeManagedMcpBlock(config, block, { serverNames: HELPER_SERVERS });
+ const parsed = parseToml(next) as Record<string, any>;
+ expect(parsed.mcp_servers.my_custom.enabled).toBe(true);
+ expect(parsed.mcp_servers.my_custom.startup_timeout_sec).toBe(30);
+ expect(parsed.mcp_servers.my_custom.url).toBe('http://127.0.0.1:22222/mcp/my_custom');
+ });
+
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) {
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 232020b378..018f850b69 100644
--- a/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts
+++ b/apps/desktop/src/main/remote-ssh/codex-remote-mcp.ts
@@ -317,7 +317,41 @@ function isManagedResidueLine(line: string): boolean {
* 会留在 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);
+ return managedScalarKey(t) !== null;
+}
+
+function managedScalarKey(t: string): string | null {
+ const m = /^(url|bearer_token_env_var|enabled|startup_timeout_sec|tool_timeout_sec)\s*=/.exec(t);
+ return m ? m[1] : null;
+}
+
+/**
+ * begin 标记**之前**的旧版残留 (#4946):旧合并剥受管段时在 `enabled` 处停手, 把该标量组
+ * 留在前一个 table 之下, 再把重建段追加到文末 —— 每刷新一次就多叠一组, 全部堆在 begin
+ * 之前。只回看紧邻 begin 的连续「空行 / 受管标量」段, 仅当某个受管键在段内出现不止一次
+ * (TOML 本已非法, 重复即证明其来自被剥掉的受管 table) 才把该键的全部副本剥掉;
+ * 只出现一次的键 (用户自己写在 begin 前的合法配置) 原样保留, 遇到 header 或其他 key 即停。
+ */
+function stripDuplicatedManagedScalarsBeforeBegin(kept: string[]): void {
+ let start = kept.length;
+ while (start > 0) {
+ const t = kept[start - 1].trim();
+ if (t !== '' && !isManagedScalarLine(t)) break;
+ start -= 1;
+ }
+ const run = kept.slice(start);
+ const counts = new Map<string, number>();
+ for (const line of run) {
+ const key = managedScalarKey(line.trim());
+ if (key) counts.set(key, (counts.get(key) ?? 0) + 1);
+ }
+ if (![...counts.values()].some((n) => n > 1)) return;
+ const cleaned = run.filter((line) => {
+ const key = managedScalarKey(line.trim());
+ return !(key && (counts.get(key) ?? 0) > 1);
+ });
+ kept.length = start;
+ kept.push(...cleaned);
}
/**
@@ -327,7 +361,8 @@ function isManagedScalarLine(t: string): boolean {
* - managed 段原位剥除后在文末重建 (TOML 与顺序无关, 幂等收敛);
* orphan begin (缺 end) 只剥连续的 managed 残留形态行, 不波及用户配置;
* end 之后紧跟的受管标量 (旧版本留下的 enabled / timeout 孤儿) 一并剥除,
- * 同样遇到 header 或其他 key 即停;
+ * 同样遇到 header 或其他 key 即停; begin 之前重复堆叠的受管标量组 (更旧版本的
+ * 真实输出, #4946) 按重复键剥除, 见 stripDuplicatedManagedScalarsBeforeBegin;
* - managed 段之外用户手写的同名 `[mcp_servers.<name>]` table 一并剥离
* (重复 table 是非法 TOML, codex 会直接起不来), 由 managed 段接管,
* 名字经 strippedUserServers 返回给调用方记 warn;
@@ -351,6 +386,7 @@ export function mergeManagedMcpBlock(
for (const line of existing.split('\n')) {
const t = line.trim();
if (t === MANAGED_BEGIN) {
+ stripDuplicatedManagedScalarsBeforeBegin(kept);
inManaged = true;
afterManagedEnd = false;
continue;