16d7f9aec0
* feat: monorepo-friendly discovery for AGENTS.md and skills
Support hierarchical config discovery in monorepos by walking up from
cwd through ancestor directories, combining files from all levels
instead of only checking the immediate working directory.
AGENTS.md context files:
- Change dedup key from file.level to file.level + depth, so context
files at different directory levels coexist instead of shadowing
- Root AGENTS.md and sub-project AGENTS.md are both included in the
system prompt, ordered from least specific to most specific
Skills:
- Modify all 4 providers with project-level skill directories (native,
claude, codex, opencode) to walk up from cwd through ancestors,
scanning for skills at each level
- Skills at closer directories win on name conflicts via existing
name-based dedup
Repo root boundary:
- Add findRepoRoot() utility that detects .git to identify repo root
- Add repoRoot field to LoadContext, computed once in loadCapability()
- All walk-up traversals (AGENTS.md, skills, nearest config dir) stop
at the repo root, preventing discovery from leaking outside the repo
- When not in a git repo, falls back to walking to filesystem root
Files changed:
- capability/fs.ts: add findRepoRoot()
- capability/types.ts: add repoRoot to LoadContext
- capability/index.ts: compute repoRoot in loadCapability()
- capability/context-file.ts: depth-aware dedup key
- discovery/agents-md.ts: bound walk-up at repo root
- discovery/builtin.ts: getAncestorDirs stopAt param, skill walk-up
- discovery/claude.ts: skill walk-up with repo root bound
- discovery/codex.ts: skill walk-up with repo root bound
- discovery/opencode.ts: skill walk-up with repo root bound
- extensibility/skills.ts: add repoRoot to inline LoadContext
* feat(agents-provider): add project-level discovery with ancestor walk-up for all capability types
The agents provider (.agent/.agents directories) previously only loaded
capabilities from the user home directory (~/.agent/, ~/.agents/). This
meant project-level .agents/ directories in monorepo roots were not
discovered when sessions started from subdirectories.
Add getProjectPathCandidates() helper that walks from cwd up to repoRoot,
scanning both .agent/ and .agents/ at each ancestor level. Apply this to
all six capability types: skills, rules, prompts, commands, context files
(AGENTS.md), and system prompts (SYSTEM.md). This matches the ancestor
walk-up behavior already present in the builtin (.omp), claude, codex,
and opencode providers.
All loaders now parallelize project-level and user-level scans via
Promise.all. Project-level results appear closest-first so dedup at the
capability layer picks the nearest override.
* fix(discovery): remove hard cap of 20 on ancestor directory walking
All walk-up loops already terminate naturally at repoRoot or filesystem
root. The depth < 20 / .slice(0, 20) / MAX_DEPTH caps were redundant
safety guards that would silently stop discovery in deeply nested
projects.
Removed from: agents.ts, agents-md.ts, builtin.ts, claude.ts, codex.ts,
opencode.ts, and corresponding test files.
* fix(agents-provider): set depth on project-level ContextFile entries for correct dedup
loadContextFiles was creating project-level ContextFile entries without
a depth field. Since contextFileCapability.key uses
`project:${file.depth ?? 0}`, all ancestor-level AGENTS.md files
collapsed to the same key and only the first survived dedup.
Compute depth via calculateDepth(cwd, ancestorDir) where ancestorDir is
two levels up from the file path (past the .agent/.agents config dir).
This gives each ancestor level a distinct dedup key.
* fix(discovery): stop ancestor walk-up at $HOME when not in a git repo
When repoRoot is null (no .git found), walk-up loops traversed all the
way to filesystem root. This caused $HOME to be scanned as a project-
level directory and then again as user-level, producing duplicates.
All providers now use `ctx.repoRoot ?? ctx.home` as the stop boundary:
stop at repoRoot if in a repo, otherwise stop at home. Applied to
agents.ts, agents-md.ts, builtin.ts, claude.ts, codex.ts, and
opencode.ts.
* fix(context-files): clamp depth >= 0 in dedup key and fix provider depth computations
The dedup key `project:${file.depth}` used raw depth, which could be
negative when providers computed it from config subdirectories (e.g.
.claude/, .github/, .gemini/) rather than the ancestor directory. This
caused same-scope cwd-level files to get distinct keys like project:-1
and project:0, bypassing dedup and injecting conflicting instructions.
Two-layer fix:
1. Key function: clamp to Math.max(0, depth) so any file at or below
cwd is treated as cwd-scope (depth 0). Defensive against future
providers.
2. Providers: fix root cause in claude.ts, gemini.ts, github.ts to
compute depth from the ancestor directory (parent of the config
subdir), not the config subdir itself.
---------
Co-authored-by: Can Bölük <can1357@users.noreply.github.com>
398 lines
13 KiB
TypeScript
398 lines
13 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import * as fs from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { type Skill as CapabilitySkill, skillCapability } from "@oh-my-pi/pi-coding-agent/capability/skill";
|
|
import { getCapability } from "@oh-my-pi/pi-coding-agent/discovery";
|
|
import { loadSkills, loadSkillsFromDir, type Skill } from "@oh-my-pi/pi-coding-agent/extensibility/skills";
|
|
|
|
const fixturesDir = path.resolve(import.meta.dirname, "fixtures/skills");
|
|
const collisionFixturesDir = path.resolve(import.meta.dirname, "fixtures/skills-collision");
|
|
|
|
const longSkillName = "this-is-a-very-long-skill-name-that-exceeds-the-sixty-four-character-limit-set-by-the-standard";
|
|
const expectedFixtureSkillOrder: string[] = [
|
|
"bad--name",
|
|
"different-name",
|
|
"Invalid_Name",
|
|
longSkillName,
|
|
"unknown-field",
|
|
"valid-skill",
|
|
];
|
|
|
|
describe("skills", () => {
|
|
describe("loadSkillsFromDir", () => {
|
|
const loadFixtureRoot = () => loadSkillsFromDir({ dir: fixturesDir, source: "test" });
|
|
|
|
it("should load a valid skill from a skills root", async () => {
|
|
const { skills, warnings } = await loadFixtureRoot();
|
|
const validSkill = skills.find(skill => skill.name === "valid-skill");
|
|
|
|
expect(validSkill).toBeDefined();
|
|
expect(validSkill?.description).toBe("A valid skill for testing purposes.");
|
|
expect(validSkill?.source).toBe("test");
|
|
expect(warnings).toHaveLength(0);
|
|
});
|
|
|
|
it("should load skill when name doesn't match parent directory", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "different-name")).toBe(true);
|
|
});
|
|
|
|
it("should load skill with invalid name characters", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "Invalid_Name")).toBe(true);
|
|
});
|
|
|
|
it("should load skill when name exceeds 64 characters", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(
|
|
skills.some(
|
|
skill =>
|
|
skill.name ===
|
|
"this-is-a-very-long-skill-name-that-exceeds-the-sixty-four-character-limit-set-by-the-standard",
|
|
),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("should skip skill when description is missing", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "missing-description")).toBe(false);
|
|
});
|
|
|
|
it("should load skill with unknown frontmatter fields", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "unknown-field")).toBe(true);
|
|
});
|
|
|
|
it("should not load nested skills recursively", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "child-skill")).toBe(false);
|
|
});
|
|
|
|
it("should skip files without frontmatter description", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "no-frontmatter")).toBe(false);
|
|
});
|
|
|
|
it("should load skill with consecutive hyphens in name", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
|
|
expect(skills.some(skill => skill.name === "bad--name")).toBe(true);
|
|
});
|
|
|
|
it("should load all directly nested skills from fixture directory", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
const names = skills.map(skill => skill.name);
|
|
|
|
expect(names).toEqual(
|
|
expect.arrayContaining([
|
|
"valid-skill",
|
|
"different-name",
|
|
"Invalid_Name",
|
|
"this-is-a-very-long-skill-name-that-exceeds-the-sixty-four-character-limit-set-by-the-standard",
|
|
"unknown-field",
|
|
"bad--name",
|
|
]),
|
|
);
|
|
expect(names).not.toContain("child-skill");
|
|
expect(skills).toHaveLength(6);
|
|
});
|
|
|
|
it("should return skills sorted by name (case-insensitive)", async () => {
|
|
const { skills } = await loadFixtureRoot();
|
|
const names = skills.map(skill => skill.name);
|
|
|
|
expect(names).toEqual(expectedFixtureSkillOrder);
|
|
});
|
|
|
|
it("should return empty for non-existent directory", async () => {
|
|
const { skills, warnings } = await loadSkillsFromDir({
|
|
dir: "/non/existent/path",
|
|
source: "test",
|
|
});
|
|
expect(skills).toHaveLength(0);
|
|
expect(warnings).toHaveLength(0);
|
|
});
|
|
|
|
it("should return empty when scanning a single skill directory directly", async () => {
|
|
const { skills } = await loadSkillsFromDir({
|
|
dir: path.join(fixturesDir, "valid-skill"),
|
|
source: "test",
|
|
});
|
|
|
|
expect(skills).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("loadSkills with options", () => {
|
|
it("should load from customDirectories only when built-ins disabled", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
});
|
|
expect(skills.length).toBeGreaterThan(0);
|
|
// Custom directory skills have source "custom:user"
|
|
expect(skills.every(s => s.source.startsWith("custom"))).toBe(true);
|
|
});
|
|
|
|
it("should return customDirectory skills sorted by name (case-insensitive)", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
});
|
|
|
|
expect(skills.map(s => s.name)).toEqual(expectedFixtureSkillOrder);
|
|
});
|
|
|
|
it("should keep user Claude skills when project .claude/skills is missing", async () => {
|
|
const tempHomeDir = await fs.mkdtemp(path.join(os.tmpdir(), "pi-claude-home-"));
|
|
const tempProjectDir = await fs.mkdtemp(path.join(os.tmpdir(), "pi-claude-project-"));
|
|
|
|
try {
|
|
const userSkillDir = path.join(tempHomeDir, ".claude", "skills", "user-only-skill");
|
|
await fs.mkdir(userSkillDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(userSkillDir, "SKILL.md"),
|
|
[
|
|
"---",
|
|
"name: user-only-skill",
|
|
"description: User-only Claude skill",
|
|
"---",
|
|
"",
|
|
"# User-only skill",
|
|
].join("\n"),
|
|
);
|
|
|
|
const capability = getCapability<CapabilitySkill>(skillCapability.id);
|
|
expect(capability).toBeDefined();
|
|
const claudeProvider = capability?.providers.find(provider => provider.id === "claude");
|
|
expect(claudeProvider).toBeDefined();
|
|
|
|
const result = await claudeProvider!.load({ cwd: tempProjectDir, home: tempHomeDir, repoRoot: null });
|
|
expect(result.items.some(skill => skill.name === "user-only-skill" && skill.level === "user")).toBe(true);
|
|
} finally {
|
|
await fs.rm(tempProjectDir, { recursive: true, force: true });
|
|
await fs.rm(tempHomeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("should filter out ignoredSkills", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
ignoredSkills: ["valid-skill"],
|
|
});
|
|
expect(skills.some(s => s.name === "valid-skill")).toBe(false);
|
|
});
|
|
|
|
it("should support glob patterns in ignoredSkills", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
ignoredSkills: ["valid-*"],
|
|
});
|
|
expect(skills.every(s => !s.name.startsWith("valid-"))).toBe(true);
|
|
});
|
|
|
|
it("should have ignoredSkills take precedence over includeSkills", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
includeSkills: ["valid-*"],
|
|
ignoredSkills: ["valid-skill"],
|
|
});
|
|
// valid-skill should be excluded even though it matches includeSkills
|
|
expect(skills.every(s => s.name !== "valid-skill")).toBe(true);
|
|
});
|
|
|
|
it("should expand ~ in customDirectories", async () => {
|
|
const tempHomeSkillsDir = await fs.mkdtemp(path.join(os.homedir(), ".pi-skills-test-"));
|
|
const relativeToHome = path.relative(os.homedir(), tempHomeSkillsDir);
|
|
const tildeDir = `~/${relativeToHome.split(path.sep).join("/")}`;
|
|
const skillDir = path.join(tempHomeSkillsDir, "tilde-skill");
|
|
const skillPath = path.join(skillDir, "SKILL.md");
|
|
await fs.mkdir(skillDir, { recursive: true });
|
|
await fs.writeFile(
|
|
skillPath,
|
|
`---
|
|
name: tilde-skill
|
|
description: Skill loaded from a tilde-expanded custom directory.
|
|
---
|
|
|
|
# Tilde Skill
|
|
`,
|
|
);
|
|
|
|
try {
|
|
const { skills: withTilde } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [tildeDir],
|
|
});
|
|
const { skills: withoutTilde } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [tempHomeSkillsDir],
|
|
});
|
|
expect(withTilde.length).toBe(withoutTilde.length);
|
|
expect(withTilde.some(skill => skill.name === "tilde-skill")).toBe(true);
|
|
} finally {
|
|
await fs.rm(tempHomeSkillsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("should return empty when all sources disabled and no custom dirs", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
});
|
|
expect(skills).toHaveLength(0);
|
|
});
|
|
|
|
it("should filter skills with includeSkills glob patterns", async () => {
|
|
// Load all skills from fixtures
|
|
const { skills: allSkills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
});
|
|
expect(allSkills.length).toBeGreaterThan(0);
|
|
|
|
// Filter to only include "valid-skill"
|
|
const { skills: filtered } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
includeSkills: ["valid-skill"],
|
|
});
|
|
expect(filtered).toHaveLength(1);
|
|
expect(filtered[0].name).toBe("valid-skill");
|
|
});
|
|
|
|
it("should support glob patterns in includeSkills", async () => {
|
|
const { skills } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
includeSkills: ["valid-*"],
|
|
});
|
|
expect(skills.length).toBeGreaterThan(0);
|
|
expect(skills.every(s => s.name.startsWith("valid-"))).toBe(true);
|
|
});
|
|
|
|
it("should return all skills when includeSkills is empty", async () => {
|
|
const { skills: withEmpty } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
includeSkills: [],
|
|
});
|
|
const { skills: withoutOption } = await loadSkills({
|
|
enableCodexUser: false,
|
|
enableClaudeUser: false,
|
|
enableClaudeProject: false,
|
|
enablePiUser: false,
|
|
enablePiProject: false,
|
|
customDirectories: [fixturesDir],
|
|
});
|
|
expect(withEmpty.length).toBe(withoutOption.length);
|
|
});
|
|
});
|
|
|
|
describe("collision handling", () => {
|
|
it("should detect name collisions and keep first skill", async () => {
|
|
// Load from first directory
|
|
const first = await loadSkillsFromDir({
|
|
dir: path.join(collisionFixturesDir, "first"),
|
|
source: "first",
|
|
});
|
|
|
|
const second = await loadSkillsFromDir({
|
|
dir: path.join(collisionFixturesDir, "second"),
|
|
source: "second",
|
|
});
|
|
|
|
// Both directories should have loaded one skill each
|
|
expect(first.skills).toHaveLength(1);
|
|
expect(second.skills).toHaveLength(1);
|
|
|
|
// Both have the same name "calendar"
|
|
expect(first.skills[0].name).toBe("calendar");
|
|
expect(second.skills[0].name).toBe("calendar");
|
|
|
|
// Simulate the collision behavior from loadSkills()
|
|
const skillMap = new Map<string, Skill>();
|
|
const collisionWarnings: Array<{ skillPath: string; message: string }> = [];
|
|
|
|
for (const skill of first.skills) {
|
|
skillMap.set(skill.name, skill);
|
|
}
|
|
|
|
for (const skill of second.skills) {
|
|
const existing = skillMap.get(skill.name);
|
|
if (existing) {
|
|
collisionWarnings.push({
|
|
skillPath: skill.filePath,
|
|
message: `name collision: "${skill.name}" already loaded from ${existing.filePath}`,
|
|
});
|
|
} else {
|
|
skillMap.set(skill.name, skill);
|
|
}
|
|
}
|
|
|
|
expect(skillMap.size).toBe(1);
|
|
expect(skillMap.get("calendar")?.source).toBe("first");
|
|
expect(collisionWarnings).toHaveLength(1);
|
|
expect(collisionWarnings[0].message).toContain("name collision");
|
|
});
|
|
});
|
|
});
|