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(desktop): 插件 Node worker 透传用户身份路径变量 (#1874)

Lizi184830e1634b
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
index 1bd98d18a6..a6db2ee589 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
@@ -115,6 +115,10 @@ describe('nodeRuntimeBroker · Electron utilityProcess 适配', () => {
vi.stubEnv('PATH', '/usr/bin');
vi.stubEnv('NODE_OPTIONS', '--inspect=0.0.0.0:9229');
vi.stubEnv('ANTHROPIC_API_KEY', 'secret');
+ vi.stubEnv('USERPROFILE', 'C:\\Users\\demo');
+ vi.stubEnv('APPDATA', 'C:\\Users\\demo\\AppData\\Roaming');
+ vi.stubEnv('GH_CONFIG_DIR', 'D:\\gh-config');
+ vi.stubEnv('XDG_CONFIG_HOME', 'D:\\xdg-config');
const child = new FakeUtilityProcess();
const fork = vi.fn((modulePath: unknown, entryArgs: unknown, options: unknown) => {
void modulePath;
@@ -146,6 +150,12 @@ describe('nodeRuntimeBroker · Electron utilityProcess 适配', () => {
expect(forkOptions.env).not.toHaveProperty('ELECTRON_RUN_AS_NODE');
expect(forkOptions.env).not.toHaveProperty('NODE_OPTIONS');
expect(forkOptions.env).not.toHaveProperty('ANTHROPIC_API_KEY');
+ // 用户身份路径变量必须透传——Windows 上的 gh 靠 APPDATA / USERPROFILE 定位
+ // 登录配置,裁掉会让 worker 里的 gh 误报“未登录”(keyring 登录读不到)。
+ expect(forkOptions.env.USERPROFILE).toBe('C:\\Users\\demo');
+ expect(forkOptions.env.APPDATA).toBe('C:\\Users\\demo\\AppData\\Roaming');
+ expect(forkOptions.env.GH_CONFIG_DIR).toBe('D:\\gh-config');
+ expect(forkOptions.env.XDG_CONFIG_HOME).toBe('D:\\xdg-config');
const spawned = vi.fn();
worker.once('spawn', spawned);
diff --git a/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts b/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
index a72426e274..aa39635fac 100644
--- a/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
+++ b/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
@@ -263,6 +263,20 @@ export function createUtilityNodeWorkerProcess(
'TMP',
'LANG',
'LC_ALL',
+ // 用户身份路径变量(是路径不是秘密):gh / git 等 CLI 依赖它们定位用户级
+ // 配置。gh 会按 GH_CONFIG_DIR → XDG_CONFIG_HOME → 平台默认目录查找;
+ // Windows 上默认目录依赖 APPDATA / 用户目录,缺少这些变量会让 keyring 里明明
+ // 有登录的 gh 仍误报“未登录”。HOME 是 Unix 侧同义变量,一并透传。
+ 'HOME',
+ 'USERPROFILE',
+ 'APPDATA',
+ 'LOCALAPPDATA',
+ 'HOMEDRIVE',
+ 'HOMEPATH',
+ // gh 允许用 GH_CONFIG_DIR 指定自定义配置目录;同属路径类变量,不含秘密。
+ 'GH_CONFIG_DIR',
+ // XDG_CONFIG_HOME 也可指定 gh 配置目录,且在 Windows 上同样可能优先于 APPDATA。
+ 'XDG_CONFIG_HOME',
] as const;
const env: NodeJS.ProcessEnv = {
CINDY_GHOST_ID: ghostId,
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/subagent-definitions.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/subagent-definitions.test.ts
index 0224098ef2..e945b1c256 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/subagent-definitions.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/subagent-definitions.test.ts
@@ -33,6 +33,25 @@ beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-subagent-'));
});
+// Windows 开发机的 os.tmpdir() 位于用户 profile 之下,项目作用域的向上走查会路过真实的
+// `~/.claude/agents`(实现刻意对齐 cc 的平台语义,见 projectAgentsDirs 头注释,不是 bug)。
+// 测试只关心 fixture 内的定义:统一按 root 前缀过滤掉本机漏进来的真实定义,保持封闭。
+// 已知残余暴露:漏进来的真实文件仍消耗扫描预算(MAX_FILES=200)。预算类用例都是
+// 「远超上限抛错」形态不受影响;但若本机真实定义数量超过上限,常规用例会被顶爆——
+// 真到那天需要给实现加可注入的走查上界,而不是继续在测试里兜。
+async function discoverInFixture(
+ options: Parameters<typeof discoverSubagentDefinitions>[0],
+): ReturnType<typeof discoverSubagentDefinitions> {
+ const found = await discoverSubagentDefinitions(options);
+ const prefixes = new Set([root]);
+ try {
+ prefixes.add(await fs.realpath(root));
+ } catch {
+ /* fixture 根拿不到 realpath 时保持字面前缀 */
+ }
+ return found.filter((f) => [...prefixes].some((p) => f.filePath.startsWith(p + path.sep)));
+}
+
afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});
@@ -47,7 +66,7 @@ describe('discoverSubagentDefinitions', () => {
'你负责搜 X。',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: wd,
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -66,7 +85,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(dir, 'b.md', 'name: b\nmodel: " "');
await writeAgent(dir, 'c.md', 'name: c');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -82,7 +101,7 @@ describe('discoverSubagentDefinitions', () => {
'name: security-review\nmodel: opus',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -99,7 +118,7 @@ describe('discoverSubagentDefinitions', () => {
'name: dup\nmodel: opus',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: home },
});
@@ -115,7 +134,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(path.join(outer, '.claude', 'agents'), 'n.md', 'name: n\nmodel: far');
await writeAgent(path.join(inner, '.claude', 'agents'), 'n.md', 'name: n\nmodel: near');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: inner,
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -128,7 +147,7 @@ describe('discoverSubagentDefinitions', () => {
const home = path.join(root, 'home', '.claude');
await writeAgent(path.join(home, 'agents'), 'u.md', 'name: u\nmodel: sonnet');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo-without-agents'),
env: { CLAUDE_CONFIG_DIR: home },
});
@@ -162,7 +181,7 @@ describe('discoverSubagentDefinitions', () => {
);
await fs.writeFile(path.join(dir, 'plain.md'), '没有 frontmatter 的普通 md\n', 'utf8');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -181,7 +200,7 @@ describe('discoverSubagentDefinitions', () => {
// 故意让 process.env 指向另一个(空)目录:传入的 env 必须胜出。
process.env.CLAUDE_CONFIG_DIR = path.join(root, 'wrong-home');
try {
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo-without-agents'),
env: { CLAUDE_CONFIG_DIR: isolated },
});
@@ -199,7 +218,7 @@ describe('discoverSubagentDefinitions', () => {
const hostHome = path.join(root, 'host-home');
await writeAgent(path.join(hostHome, 'agents'), 'h.md', 'name: from-host-env\nmodel: opus');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo-without-agents'),
env: {},
hostEnv: { CLAUDE_CONFIG_DIR: hostHome },
@@ -214,7 +233,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(path.join(childHome, 'agents'), 'c.md', 'name: from-child-env\nmodel: opus');
await writeAgent(path.join(hostHome, 'agents'), 'h.md', 'name: from-host-env\nmodel: opus');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo-without-agents'),
env: { CLAUDE_CONFIG_DIR: childHome },
hostEnv: { CLAUDE_CONFIG_DIR: hostHome },
@@ -245,7 +264,7 @@ describe('discoverSubagentDefinitions', () => {
await fs.mkdir(agents, { recursive: true });
await fs.symlink(path.join(real, 'reviewer.md'), path.join(agents, 'reviewer.md'));
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -261,7 +280,7 @@ describe('discoverSubagentDefinitions', () => {
await fs.mkdir(agents, { recursive: true });
await fs.symlink(real, path.join(agents, 'shared'), directoryLinkType);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -281,7 +300,7 @@ describe('discoverSubagentDefinitions', () => {
});
expect(kind).toBeUndefined();
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -294,7 +313,7 @@ describe('discoverSubagentDefinitions', () => {
path.join(agents, 'dead.md'),
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -308,7 +327,7 @@ describe('discoverSubagentDefinitions', () => {
// agents/loop -> agents 自己
await fs.symlink(agents, path.join(agents, 'loop'), directoryLinkType);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -332,7 +351,7 @@ describe('discoverSubagentDefinitions', () => {
);
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
}),
@@ -343,7 +362,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(path.join(root, 'repo', '.claude', 'agents'), 'a.md', 'name: a');
// now() 注入一个「已经超时」的起点,不依赖真实慢盘也能锁住这条分支。
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
now: () => Date.now() - 60_000,
@@ -362,7 +381,7 @@ describe('discoverSubagentDefinitions', () => {
);
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
deadlineMs: 0,
@@ -373,7 +392,7 @@ describe('discoverSubagentDefinitions', () => {
it('deadline 充裕时正常返回(定时器不会误伤正常路径)', async () => {
await writeAgent(path.join(root, 'repo', '.claude', 'agents'), 'a.md', 'name: a\nmodel: opus');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
deadlineMs: 5_000,
@@ -395,7 +414,7 @@ describe('discoverSubagentDefinitions', () => {
const link = path.join(linkParent, 'app-link');
await fs.symlink(workSub, link, directoryLinkType);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: link,
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -415,7 +434,7 @@ describe('discoverSubagentDefinitions', () => {
'utf8',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -435,7 +454,7 @@ describe('discoverSubagentDefinitions', () => {
);
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
}),
@@ -453,7 +472,7 @@ describe('discoverSubagentDefinitions', () => {
'utf8',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -474,7 +493,7 @@ describe('discoverSubagentDefinitions', () => {
);
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
}),
@@ -487,7 +506,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(deep, 'x.md', 'name: deep\nmodel: opus');
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
}),
@@ -503,7 +522,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(agents, 'old.md.bak.1', 'name: bak-backup\nmodel: opus');
await writeAgent(path.join(agents, '.archive'), 'z.md', 'name: in-hidden-dir\nmodel: opus');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -522,7 +541,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(dir, 'a-plain.md', 'name: dup\ndescription: 没写 model');
await writeAgent(dir, 'z-declared.md', 'name: dup\ndescription: 写了 model\nmodel: xai/grok-4.5');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -536,7 +555,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(dir, 'a-declared.md', 'name: dup\ndescription: 写了 model\nmodel: opus');
await writeAgent(dir, 'z-plain.md', 'name: dup\ndescription: 没写 model');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -555,7 +574,7 @@ describe('discoverSubagentDefinitions', () => {
'name: dup\ndescription: 项目',
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: home },
});
@@ -572,7 +591,7 @@ describe('discoverSubagentDefinitions', () => {
`name: hm\ndescription: d\nmodel: ${'x'.repeat(5000)}`,
);
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -594,7 +613,7 @@ describe('discoverSubagentDefinitions', () => {
);
await writeAgent(path.join(inner, '.claude', 'agents'), 'n.md', 'name: n\ndescription: 近者没写');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: inner,
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -611,7 +630,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(agents, 'x.md', 'name: dup\ndescription: 根下没写 model');
await writeAgent(path.join(agents, 'review'), 'x.md', 'name: dup\ndescription: 子目录写了\nmodel: opus');
- const found = await discoverSubagentDefinitions({
+ const found = await discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
});
@@ -622,14 +641,14 @@ describe('discoverSubagentDefinitions', () => {
it('目录不存在 / workingDir 非绝对路径都安全返回空,不抛错', async () => {
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: path.join(root, 'nope'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'also-nope') },
}),
).resolves.toEqual([]);
await expect(
- discoverSubagentDefinitions({
+ discoverInFixture({
workingDir: 'relative/path',
env: { CLAUDE_CONFIG_DIR: path.join(root, 'also-nope') },
}),