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(pi): 优先解析发现根中的停用技能入口

xd-bobo149d0d7b2084
diff --git a/docs/dev-rules/pi-harness.md b/docs/dev-rules/pi-harness.md
index 21c88aa7bb..bca6943194 100644
--- a/docs/dev-rules/pi-harness.md
+++ b/docs/dev-rules/pi-harness.md
@@ -78,7 +78,8 @@ provider/model/contextWindow,因为 Pi 会用进程初始 CLI route 重建
Skill 停用适配保留用户指定的物理路径,再尽力解析发现目录中的符号链接别名;该额外扫描
-共享 2048 个条目、16 层深度和 100ms 的遍历预算,目录流逐项读取并在退出时关闭。
+共享 2048 个条目、16 层深度和 100ms 的遍历预算,先检查各发现根的直接入口,再逐层
+进入子目录,避免无关子树抢先耗尽预算;目录流逐项读取并在退出时关闭。
预算耗尽后保留已解析路径,不继续扫描;原生 Pi 仍负责资源加载,不能因 Cindy 的扫描
截断而拒绝加载其它资源。时间预算在文件系统调用之间检查,不是对单次系统调用的超时。
diff --git a/packages/maker-core/src/agents/pi/__tests__/skill-activation.test.ts b/packages/maker-core/src/agents/pi/__tests__/skill-activation.test.ts
index e5dc69df46..d860327431 100644
--- a/packages/maker-core/src/agents/pi/__tests__/skill-activation.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/skill-activation.test.ts
@@ -37,6 +37,48 @@ describe('bounded Pi disabled Skill alias discovery', () => {
expect(scan).not.toHaveBeenCalled();
});
+ it.each(['entries', 'time'])('resolves direct aliases before an unrelated subtree exhausts %s', (budget) => {
+ const discovery = path.join(root, 'skills');
+ const large = path.join(discovery, 'a-large');
+ const source = path.join(root, 'source');
+ const alias = path.join(discovery, 'z-alias');
+ const secondRoot = path.join(root, 'other-skills');
+ const secondAlias = path.join(secondRoot, 'alias');
+ fs.mkdirSync(large, { recursive: true });
+ fs.mkdirSync(source);
+ fs.writeFileSync(path.join(source, 'SKILL.md'), 'fixture');
+ link(source, alias);
+ link(source, secondAlias);
+ let elapsed = 0;
+ vi.spyOn(performance, 'now').mockImplementation(() => elapsed);
+ const open = fs.opendirSync.bind(fs);
+ let largeReads = 0;
+ const closed = vi.fn();
+ vi.spyOn(fs, 'opendirSync').mockImplementation((entry, options) => {
+ if (entry === discovery) {
+ // Force the pathological ordering independently of filesystem order.
+ const entries = fs.readdirSync(entry, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
+ return { readSync: () => entries.shift() ?? null, closeSync: vi.fn() } as unknown as fs.Dir;
+ }
+ if (entry === large) {
+ // A directory stream with arbitrarily many irrelevant entries.
+ return {
+ readSync: () => {
+ largeReads += 1;
+ if (budget === 'time') elapsed = 101;
+ return { name: '.ignored' };
+ },
+ closeSync: closed,
+ } as unknown as fs.Dir;
+ }
+ return open(entry, options);
+ });
+ expect(piDisabledDiscoveryPaths([source], [discovery, secondRoot])).toEqual([source, alias, secondAlias]);
+ expect(largeReads).toBeGreaterThan(0);
+ expect(largeReads).toBeLessThanOrEqual(2048);
+ expect(closed).toHaveBeenCalledOnce();
+ });
+
it('caps traversal through a linked deep directory while retaining the disabled source', () => {
const deepest = deepTree();
const alias = path.join(root, 'alias');
diff --git a/packages/maker-core/src/agents/pi/skill-activation.ts b/packages/maker-core/src/agents/pi/skill-activation.ts
index b093412f95..094b0b8e9f 100644
--- a/packages/maker-core/src/agents/pi/skill-activation.ts
+++ b/packages/maker-core/src/agents/pi/skill-activation.ts
@@ -32,6 +32,7 @@ export function piDisabledDiscoveryPaths(disabled: readonly string[], roots: rea
const disabledKeys = new Set(disabled.map(canonicalSkillPath));
let remainingEntries = 2048;
const exhausted = () => remainingEntries <= 0 || performance.now() >= deadline;
+ const pending: Array<{ entry: string; ancestors: Set<string>; depth: number }> = [];
const visit = (entry: string, ancestors: Set<string>, depth: number) => {
if (depth > 16 || exhausted()) return;
try {
@@ -39,24 +40,31 @@ export function piDisabledDiscoveryPaths(disabled: readonly string[], roots: rea
if (disabledKeys.has(key)) { result.add(entry); return; }
if (!fs.statSync(entry).isDirectory() || ancestors.has(key)) return;
if (fs.existsSync(path.join(entry, 'SKILL.md')) || fs.existsSync(path.join(entry, 'skill.md'))) return;
- const next = new Set([...ancestors, key]);
+ if (depth < 16) pending.push({ entry, ancestors: new Set([...ancestors, key]), depth });
+ } catch { /* Missing/unreadable discovery roots are handled by native Pi. */ }
+ };
+ for (const root of roots) {
+ if (exhausted()) break;
+ remainingEntries -= 1;
+ visit(root, new Set(), 0);
+ }
+ // Inspect every root's direct candidates before descending into any subtree.
+ // FIFO preserves this priority at subsequent levels within the shared budget.
+ for (let index = 0; index < pending.length && !exhausted(); index += 1) {
+ const { entry, ancestors, depth } = pending[index]!;
+ try {
const directory = fs.opendirSync(entry);
try {
while (!exhausted()) {
const child = directory.readSync();
if (!child) break;
remainingEntries -= 1;
- if (!child.name.startsWith('.')) visit(path.join(entry, child.name), next, depth + 1);
+ if (!child.name.startsWith('.')) visit(path.join(entry, child.name), ancestors, depth + 1);
}
} finally {
directory.closeSync();
}
} catch { /* Missing/unreadable discovery roots are handled by native Pi. */ }
- };
- for (const root of roots) {
- if (exhausted()) break;
- remainingEntries -= 1;
- visit(root, new Set(), 0);
}
return [...result];
}