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。

test(maker-core): 缩小 subagent 扫描预算固件 (#2082)

fmfsaisai7ca78fc9199d
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 e945b1c256..7ab03559c1 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
@@ -339,9 +339,10 @@ describe('discoverSubagentDefinitions', () => {
// 于是又把覆盖用的 env 设回去。抛出来才能走调用方的显式降级。
it('文件数超预算 → 抛 SubagentScanBudgetError', async () => {
const agents = path.join(root, 'repo', '.claude', 'agents');
+ const maxFiles = 5;
await fs.mkdir(agents, { recursive: true });
await Promise.all(
- Array.from({ length: 210 }, (_, i) =>
+ Array.from({ length: maxFiles + 1 }, (_, i) =>
fs.writeFile(
path.join(agents, `a${i}.md`),
`---\nname: a${i}\ndescription: d\n---\nbody\n`,
@@ -354,6 +355,7 @@ describe('discoverSubagentDefinitions', () => {
discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
+ maxFiles,
}),
).rejects.toBeInstanceOf(SubagentScanBudgetError);
});
@@ -485,9 +487,10 @@ describe('discoverSubagentDefinitions', () => {
// 同步排序还堵住事件循环让外层定时器都没机会触发。改 opendir 流式并就地封顶。
it('单目录条目数超上限 → 抛(不物化、不排序)', async () => {
const agents = path.join(root, 'repo', '.claude', 'agents');
+ const maxDirEntries = 5;
await fs.mkdir(agents, { recursive: true });
await Promise.all(
- Array.from({ length: 520 }, (_, i) =>
+ Array.from({ length: maxDirEntries + 1 }, (_, i) =>
fs.writeFile(path.join(agents, `x${i}.txt`), 'not a definition', 'utf8'),
),
);
@@ -496,19 +499,22 @@ describe('discoverSubagentDefinitions', () => {
discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
+ maxDirEntries,
}),
).rejects.toBeInstanceOf(SubagentScanBudgetError);
});
// 深度上限也是预算:静默返回空会让上层判「没人声明 model」→ 又把覆盖用的 env 设回去。
it('递归深度超上限 → 抛(不静默截断)', async () => {
- const deep = path.join(root, 'repo', '.claude', 'agents', ...Array(10).fill('d'));
+ const maxDepth = 2;
+ const deep = path.join(root, 'repo', '.claude', 'agents', ...Array(3).fill('d'));
await writeAgent(deep, 'x.md', 'name: deep\nmodel: opus');
await expect(
discoverInFixture({
workingDir: path.join(root, 'repo'),
env: { CLAUDE_CONFIG_DIR: path.join(root, 'empty-home') },
+ maxDepth,
}),
).rejects.toBeInstanceOf(SubagentScanBudgetError);
});
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 fda4077944..2c90472709 100644
--- a/packages/maker-core/src/agents/claude-code/subagent-definitions.ts
+++ b/packages/maker-core/src/agents/claude-code/subagent-definitions.ts
@@ -121,7 +121,13 @@ const MAX_ELAPSED_MS = 1_500;
class ScanBudget {
private files = 0;
private dirs = 0;
- constructor(private readonly startedAt: number, private readonly deadlineMs: number) {}
+ constructor(
+ private readonly startedAt: number,
+ private readonly deadlineMs: number,
+ readonly maxDepth: number,
+ readonly maxFiles: number,
+ readonly maxDirEntries: number,
+ ) {}
countDir(): void {
if (++this.dirs > MAX_DIRS) throw new SubagentScanBudgetError(`dirs>${MAX_DIRS}`);
@@ -129,7 +135,9 @@ class ScanBudget {
}
countFile(): void {
- if (++this.files > MAX_FILES) throw new SubagentScanBudgetError(`files>${MAX_FILES}`);
+ if (++this.files > this.maxFiles) {
+ throw new SubagentScanBudgetError(`files>${this.maxFiles}`);
+ }
this.checkTime();
}
@@ -199,7 +207,7 @@ async function isDirectory(p: string): Promise<boolean> {
}
/**
- * 流式读一个目录的条目,条目数超 {@link MAX_DIR_ENTRIES} 立即抛。
+ * 流式读一个目录的条目,条目数超扫描预算立即抛。
*
* 打不开的目录(权限 / 竞态删除)当作空目录,只跳过它自己;但**条目过多要抛** —— 那种情况下
* 结果不可信,静默截断会让上层以为「扫完了,没人声明 model」。
@@ -215,8 +223,8 @@ async function readDirEntriesBounded(dir: string, budget: ScanBudget): Promise<D
const entries: Dirent[] = [];
// for-await 在 break / throw 时会自动关闭目录句柄。
for await (const ent of handle) {
- if (entries.length >= MAX_DIR_ENTRIES) {
- throw new SubagentScanBudgetError(`dirEntries>${MAX_DIR_ENTRIES}`);
+ if (entries.length >= budget.maxDirEntries) {
+ throw new SubagentScanBudgetError(`dirEntries>${budget.maxDirEntries}`);
}
entries.push(ent);
budget.checkTime();
@@ -278,7 +286,9 @@ async function collectMarkdownFiles(
): Promise<string[]> {
// 深度上限也是预算的一部分,超了同样**抛**而不是返回空:深层目录里若有声明了 model 的定义,
// 静默截断会让上层判成「没人声明」→ 又把覆盖用的 env 设回去(本 PR 要修的 bug)。
- if (depth > MAX_DEPTH) throw new SubagentScanBudgetError(`depth>${MAX_DEPTH}`);
+ if (depth > budget.maxDepth) {
+ throw new SubagentScanBudgetError(`depth>${budget.maxDepth}`);
+ }
// 软链环兜底:按真实路径去重。realpath 失败(悬空链)就跳过这个目录。
let real: string;
try {
@@ -451,6 +461,27 @@ export interface DiscoverSubagentsOptions {
deadlineMs?: number;
/** 测试注入起始时刻(避免依赖真实时钟)。 */
now?: () => number;
+ /**
+ * 递归深度上限,有效范围 1..{@link MAX_DEPTH};非法值或超过缺省上限时回退缺省值。
+ * 测试可注入较小值,避免为触发预算创建大量目录。
+ */
+ maxDepth?: number;
+ /**
+ * .md 文件数上限,有效范围 1..{@link MAX_FILES};非法值或超过缺省上限时回退缺省值。
+ * 测试可注入较小值,避免为触发预算创建大量文件。
+ */
+ maxFiles?: number;
+ /**
+ * 单目录条目数上限,有效范围 1..{@link MAX_DIR_ENTRIES};非法值或超过缺省上限时回退缺省值。
+ * 测试可注入较小值,避免为触发预算创建大量目录条目。
+ */
+ maxDirEntries?: number;
+}
+
+function resolveBudgetLimit(value: number | undefined, fallback: number): number {
+ return typeof value === 'number' && Number.isInteger(value) && value > 0 && value <= fallback
+ ? value
+ : fallback;
}
/**
@@ -475,7 +506,13 @@ async function scanSubagentDefinitions(
opts: DiscoverSubagentsOptions,
deadlineMs: number,
): Promise<DiscoveredSubagent[]> {
- const budget = new ScanBudget((opts.now ?? Date.now)(), deadlineMs);
+ const budget = new ScanBudget(
+ (opts.now ?? Date.now)(),
+ deadlineMs,
+ resolveBudgetLimit(opts.maxDepth, MAX_DEPTH),
+ resolveBudgetLimit(opts.maxFiles, MAX_FILES),
+ resolveBudgetLimit(opts.maxDirEntries, MAX_DIR_ENTRIES),
+ );
const visitedDirs = new Set<string>();
const scoped: Array<{ dir: string; scope: DiscoveredSubagent['scope'] }> = [
// 顺序 = 跨作用域优先级从高到低(项目近者 > 项目远者 > 用户)。