diff --git a/src/squad-apply.js b/src/squad-apply.js index c60470f..9a939a6 100644 --- a/src/squad-apply.js +++ b/src/squad-apply.js @@ -107,12 +107,27 @@ function physicalPath(file) { } /** - * Every agent file name OpenCode could load next to `dir`. OpenCode reads - * every .md file below both `agent` and `agents` of a config dir, recursively - * and through directory links, and names each by its path: a file reached - * through a second root or a linked directory is a second agent name for the - * same file. Names are relative to `dir` (a sibling root's start with - * `../agents/`). + * The directories OpenCode's config-agent loader reads agent Markdown from + * (OpenCode 2.1.3, config/plugin/agent.ts `legacySources`): `deep` ones + * recursively, the others top level only. tests/regress-squad-files.test.js + * compares this with the installed loader, so an OpenCode bump that changes + * them fails there instead of here. + */ +export const AGENT_SOURCES = [ + { name: "agent", deep: true }, + { name: "agents", deep: true }, + { name: "mode", deep: false }, + { name: "modes", deep: false }, +]; + +/** + * Every agent file name OpenCode could load next to `dir`. The pinned + * OpenCode 2.1.3 config-agent loader scans a config dir for + * every .md below `agent/` and `agents/` (subagents, recursive) and the .md + * files directly in `mode/` and `modes/` (primary agents, top level only), following links, and names each by its + * path: a file reached through another root or a linked directory is another + * agent name for the same file. Names are relative to `dir` (a sibling + * root's start with `../agents/`, `../mode/`, ...). * * Linked directories are followed as OpenCode follows them, so every alias * path is listed. A link back into a directory already on the current path @@ -128,17 +143,19 @@ function listAgentFiles(dir) { const names = []; /** @type {string[]} */ const unlisted = []; + // Only when `dir` is one of the sources is its parent a config dir with + // siblings to check. const base = path.basename(dir); - const roots = - base === "agent" || base === "agents" - ? [dir, path.join(path.dirname(dir), base === "agent" ? "agents" : "agent")] - : [dir]; + const roots = AGENT_SOURCES.some((s) => s.name === base) + ? AGENT_SOURCES.map((s) => ({ abs: path.join(path.dirname(dir), s.name), deep: s.deep })) + : [{ abs: dir, deep: true }]; /** * @param {string} abs * @param {Set} ancestors real paths of the directories above * @param {boolean} isRoot + * @param {boolean} deep descend into subdirectories (agent roots) or not (mode roots) */ - const walk = (abs, ancestors, isRoot) => { + const walk = (abs, ancestors, isRoot, deep) => { /** @type {fs.Dirent[]} */ let entries; let real; @@ -166,12 +183,13 @@ function listAgentFiles(dir) { isDir = false; // dangling: a file name at most } } - if (isDir) walk(child, below, false); - else if (ent.name.endsWith(".md")) + if (isDir) { + if (deep) walk(child, below, false, true); + } else if (ent.name.endsWith(".md")) names.push(path.relative(dir, child).split(path.sep).join("/")); } }; - for (const root of roots) walk(root, new Set(), true); + for (const root of roots) walk(root.abs, new Set(), true, root.deep); return { names, unlisted }; } diff --git a/tests/regress-squad-files.test.js b/tests/regress-squad-files.test.js index bfce7b2..3739e6b 100644 --- a/tests/regress-squad-files.test.js +++ b/tests/regress-squad-files.test.js @@ -7,12 +7,19 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; +import { fileURLToPath } from "node:url"; import { afterEach, beforeEach, expect, test, vi } from "vitest"; import { parse } from "yaml"; import { ensureGlobalModelDataFresh } from "../.opencode/lib/runtime.js"; import { readFrontmatter } from "../src/frontmatter.js"; import { parseAgentFile } from "../src/roster.js"; -import { applySquad, formatApplyReport, readSquad, syncDescriptions } from "../src/squad-apply.js"; +import { + AGENT_SOURCES, + applySquad, + formatApplyReport, + readSquad, + syncDescriptions, +} from "../src/squad-apply.js"; import { parseSkillFile } from "../src/v2/skills.js"; import { agentMarkdown } from "../src/workers.js"; @@ -373,6 +380,51 @@ test("a directory link back to the agent root is a gap, not a skipped path", () expect(fs.readFileSync(path.join(agentDir, filename), "utf8")).toBe(content); }); +test("a primary-agent alias in mode/ shares the file; one in mode/sub/ is never loaded", () => { + const { filename, content } = addAgent("grunt", "a/b"); + const mode = path.join(path.dirname(agentDir), "mode"); + fs.mkdirSync(path.join(mode, "sub"), { recursive: true }); + fs.symlinkSync(path.join(agentDir, filename), path.join(mode, "sub", "deep.md")); + // Not loadable (mode/ is read top level only): no conflict yet. + expect(readSquad(agentDir).conflicts).toEqual([]); + fs.symlinkSync(path.join(agentDir, filename), path.join(mode, "review.md")); + const line = `../mode/review.md, ${filename} all lead to ${fs.realpathSync(path.join(agentDir, filename))}; each agent needs its own file`; + expect(readSquad(agentDir).conflicts).toContain(line); + expect(syncDescriptions({ dir: agentDir, describe: () => "new" })).toEqual([]); + const result = applySquad({ + roster: { grunts: { "a/b": { variant: "low" } }, drills: {} }, + dir: agentDir, + }); + expect(result.ok ? null : result.refusal).toBe("collision"); + expect(fs.readFileSync(path.join(agentDir, filename), "utf8")).toBe(content); +}); + +test("the agent sources match the installed OpenCode loader", () => { + // Read the pinned loader's own list, so an OpenCode bump that adds or + // changes a source fails here. + const dist = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "..", + "node_modules", + "@opencode", + "core", + "dist", + "chunks", + ); + const loader = fs + .readdirSync(dist) + .map((f) => fs.readFileSync(path.join(dist, f), "utf8")) + .find((t) => t.includes("var legacySources")); + expect(loader, "OpenCode config-agent loader not found").toBeDefined(); + const patterns = [...(loader ?? "").matchAll(/pattern: "([^"]+)"/g)].map((m) => m[1]); + const ours = []; + for (const deep of [true, false]) { + const names = AGENT_SOURCES.filter((s) => s.deep === deep).map((s) => s.name); + ours.push(`{${names.join(",")}}/${deep ? "**/" : ""}*.md`); + } + expect(patterns).toEqual(ours); +}); + test("a symlink to a file nothing else reaches is still written through", () => { const own = path.join(root, "dotfiles", "grunt-a-b.md"); fs.mkdirSync(path.dirname(own), { recursive: true });