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。

修复 Windows 软链测试兼容性

DavidShene126f3d523e7
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 efc7a8bd6e..021410c580 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
@@ -4,11 +4,19 @@ import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
+ classifySubagentEntry,
SubagentScanBudgetError,
discoverSubagentDefinitions,
} from '../subagent-definitions.js';
let root: string;
+const directoryLinkType = process.platform === 'win32' ? 'junction' : 'dir';
+
+const symbolicLinkDirent = {
+ isDirectory: () => false,
+ isFile: () => false,
+ isSymbolicLink: () => true,
+};
// cc 的加载条件是 name + description 都必须是非空字符串(见 subagent-definitions.ts 里
// readSubagentFile 的反编译依据)。固件默认补上 description,让它们代表「cc 真的会加载」的
@@ -218,6 +226,19 @@ describe('discoverSubagentDefinitions', () => {
// 回归:本仓建 worktree 时刻意保留 .claude/agents 里的软链(WorktreeManager 用
// dereference: false)。软链的 Dirent 既非 file 也非 dir,漏掉它 = 误判「没人声明 model」。
it('跟随软链的 agent 定义文件', async () => {
+ if (process.platform === 'win32') {
+ const link = path.join(root, 'repo', '.claude', 'agents', 'reviewer.md');
+ const visited: string[] = [];
+ const kind = await classifySubagentEntry(symbolicLinkDirent, link, async (entryPath) => {
+ visited.push(entryPath);
+ return { isDirectory: () => false, isFile: () => true };
+ });
+
+ expect(visited).toEqual([link]);
+ expect(kind).toBe('file');
+ return;
+ }
+
const real = path.join(root, 'shared');
await writeAgent(real, 'reviewer.md', 'name: reviewer\nmodel: xai/grok-4.5');
const agents = path.join(root, 'repo', '.claude', 'agents');
@@ -238,7 +259,7 @@ describe('discoverSubagentDefinitions', () => {
await writeAgent(real, 'a.md', 'name: linked-dir-agent\nmodel: opus');
const agents = path.join(root, 'repo', '.claude', 'agents');
await fs.mkdir(agents, { recursive: true });
- await fs.symlink(real, path.join(agents, 'shared'), 'dir');
+ await fs.symlink(real, path.join(agents, 'shared'), directoryLinkType);
const found = await discoverSubagentDefinitions({
workingDir: path.join(root, 'repo'),
@@ -251,7 +272,11 @@ describe('discoverSubagentDefinitions', () => {
it('悬空软链跳过,不影响同目录其它定义', async () => {
const agents = path.join(root, 'repo', '.claude', 'agents');
await writeAgent(agents, 'ok.md', 'name: ok\nmodel: opus');
- await fs.symlink(path.join(root, 'gone', 'nothing.md'), path.join(agents, 'dead.md'));
+ await fs.symlink(
+ path.join(root, 'gone', 'nothing.md'),
+ path.join(agents, 'dead.md'),
+ directoryLinkType,
+ );
const found = await discoverSubagentDefinitions({
workingDir: path.join(root, 'repo'),
@@ -265,7 +290,7 @@ describe('discoverSubagentDefinitions', () => {
const agents = path.join(root, 'repo', '.claude', 'agents');
await writeAgent(agents, 'a.md', 'name: a\nmodel: opus');
// agents/loop -> agents 自己
- await fs.symlink(agents, path.join(agents, 'loop'), 'dir');
+ await fs.symlink(agents, path.join(agents, 'loop'), directoryLinkType);
const found = await discoverSubagentDefinitions({
workingDir: path.join(root, 'repo'),
@@ -352,7 +377,7 @@ describe('discoverSubagentDefinitions', () => {
const linkParent = path.join(root, 'elsewhere');
await fs.mkdir(linkParent, { recursive: true });
const link = path.join(linkParent, 'app-link');
- await fs.symlink(workSub, link, 'dir');
+ await fs.symlink(workSub, link, directoryLinkType);
const found = await discoverSubagentDefinitions({
workingDir: link,
diff --git a/packages/maker-core/src/agents/claude-code/subagent-definitions.ts b/packages/maker-core/src/agents/claude-code/subagent-definitions.ts
index 613b229369..fda4077944 100644
--- a/packages/maker-core/src/agents/claude-code/subagent-definitions.ts
+++ b/packages/maker-core/src/agents/claude-code/subagent-definitions.ts
@@ -51,7 +51,7 @@
* prompt 的合法定义,同样退化成上一条那个 bug。
*/
-import type { Dirent } from 'node:fs';
+import type { Dirent, Stats } from 'node:fs';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
@@ -224,6 +224,37 @@ async function readDirEntriesBounded(dir: string, budget: ScanBudget): Promise<D
return entries;
}
+type EntryKind = 'directory' | 'file' | 'other';
+
+/**
+ * 把目录条目按跟随软链后的目标分类。
+ *
+ * `statEntry` 可注入是为了让 Windows 在未开启 Developer Mode、无法创建文件软链时,
+ * 仍能真实覆盖 Dirent 的软链分支;目录 junction 则继续走端到端测试。
+ */
+export async function classifySubagentEntry(
+ ent: Pick<Dirent, 'isDirectory' | 'isFile' | 'isSymbolicLink'>,
+ fullPath: string,
+ statEntry: (entryPath: string) => Promise<Pick<Stats, 'isDirectory' | 'isFile'>> = (entryPath) =>
+ fs.stat(entryPath),
+): Promise<EntryKind | undefined> {
+ let isDir = ent.isDirectory();
+ let isFile = ent.isFile();
+ if (ent.isSymbolicLink()) {
+ // follow: stat 走目标。悬空 / 无权限的链直接跳过这一条。
+ try {
+ const st = await statEntry(fullPath);
+ isDir = st.isDirectory();
+ isFile = st.isFile();
+ } catch {
+ return undefined;
+ }
+ }
+ if (isDir) return 'directory';
+ if (isFile) return 'file';
+ return 'other';
+}
+
/**
* 递归收集目录下的 .md 文件。单个坏目录只跳过它自己,不影响其余扫描;超预算则整趟抛出。
*
@@ -270,24 +301,14 @@ async function collectMarkdownFiles(
// 以为「有人声明了 model」从而删掉 env —— 用户配的默认值对真正的 agent 反而失效了。
if (ent.name.startsWith('.') || /\.bak\.\d+$/.test(ent.name)) continue;
const full = path.join(dir, ent.name);
- let isDir = ent.isDirectory();
- let isFile = ent.isFile();
- if (ent.isSymbolicLink()) {
- // follow: stat 走目标。悬空 / 无权限的链直接跳过这一条。
- try {
- const st = await fs.stat(full);
- isDir = st.isDirectory();
- isFile = st.isFile();
- } catch {
- continue;
- }
- }
- if (isDir) {
+ const kind = await classifySubagentEntry(ent, full);
+ if (kind === undefined) continue;
+ if (kind === 'directory') {
files.push(...(await collectMarkdownFiles(full, budget, visitedDirs, depth + 1)));
// 扩展名大小写不敏感:`reviewer.MD` 在大小写保留的文件系统上照样是一份定义,
// 本仓既有的 customization-scanner 也是 `toLowerCase().endsWith('.md')`。
// 大小写敏感地漏掉一份声明了 model 的定义 = 又把覆盖用的 env 设回去。
- } else if (isFile && ent.name.toLowerCase().endsWith('.md')) {
+ } else if (kind === 'file' && ent.name.toLowerCase().endsWith('.md')) {
budget.countFile();
files.push(full);
}