From e706285e2350583686826b7e6047e64d7c6c6561 Mon Sep 17 00:00:00 2001 From: Alex MKX <25821291+AlexMKX@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:29:12 +0300 Subject: [PATCH] squad files: prune and migration go through the shared-destination check (#52) Co-Authored-By: Claude Opus 5.5 --- README.md | 12 ++++++++++ src/squad-apply.js | 40 ++++++++++++++++++------------- tests/regress-squad-files.test.js | 29 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 392f899..64264d6 100644 --- a/README.md +++ b/README.md @@ -396,6 +396,18 @@ Two properties make the flow safe, and neither is optional: says otherwise. The unit is the agent, which is also the unit on disk, so dropping just a drill is gated exactly like dropping a model. +**One file, one agent.** OpenCode loads agent Markdown from `agent/` and +`agents/` (recursively) and from `mode/` and `modes/` (top level), through +links, so one file can be loaded under several agent names. Before it writes, +prunes or migrates anything, `squad_patch` checks every path it touches against +all of them: if another loadable name reaches the same file, or a directory +cannot be listed, it refuses and names the paths. The startup description +refresh skips such files and logs them, and `squad_dump` lists them. +`allow_remove` permits dropping a roster entry, not breaking another agent. +Known limitation: a plain `agents -> agent` directory link makes OpenCode load +each file twice under the same id, which would be harmless, but Squad still +refuses it. That errs on the safe side. + `squad_patch` additionally refuses to run from a `grunt-`/`drill-` agent. A subagent rewriting the squad mid-task is never intended, and the damage outlives the session. diff --git a/src/squad-apply.js b/src/squad-apply.js index 9a939a6..d5f77de 100644 --- a/src/squad-apply.js +++ b/src/squad-apply.js @@ -363,16 +363,6 @@ export function applySquad({ roster, dir, allowRemove = false, describe }) { plan.push({ role, modelId, entry, filename, content, existing }); } } - for (const shared of sharedDestinations( - dir, - plan.map((p) => p.filename), - )) - problems.push(shared.line); - if (unreadable.length) return refuse("unreadable", unreadable); - if (damaged.length) return refuse("damaged", damaged); - if (dangling.length) return refuse("dangling", dangling); - if (problems.length) return refuse("collision", problems); - /** @type {string[]} */ const migrate = []; /** @type {Array<{file: string, agent: string}>} */ @@ -385,6 +375,27 @@ export function applySquad({ roster, dir, allowRemove = false, describe }) { else duplicates.push({ file: f, agent }); } } + /** Files of removed agents that no kept agent takes over: pruned below. */ + const prune = diff.removed.flatMap((agent) => + (filesByAgent.get(agent) ?? []).filter((f) => !byFile.has(f)), + ); + + // Every path this apply writes, prunes or migrates away. Unlinking a file + // that another loadable name also reaches breaks that agent as surely as + // overwriting it, and allow_remove covers the roster entry, not the other + // agent: so deletions go through the same check as writes, before anything + // changes. + for (const shared of sharedDestinations(dir, [ + ...plan.map((p) => p.filename), + ...prune, + ...duplicates.map((d) => d.file), + ...migrate, + ])) + problems.push(shared.line); + if (unreadable.length) return refuse("unreadable", unreadable); + if (damaged.length) return refuse("damaged", damaged); + if (dangling.length) return refuse("dangling", dangling); + if (problems.length) return refuse("collision", problems); if ((diff.removed.length > 0 || duplicates.length > 0) && !allowRemove) { return refuse("remove", [ ...diff.removed, @@ -418,12 +429,9 @@ export function applySquad({ roster, dir, allowRemove = false, describe }) { } const pruned = []; - for (const agent of diff.removed) { - for (const f of filesByAgent.get(agent) ?? []) { - if (byFile.has(f)) continue; // now holds a kept agent - fs.unlinkSync(path.join(dir, f)); - pruned.push(f); - } + for (const f of prune) { + fs.unlinkSync(path.join(dir, f)); + pruned.push(f); } for (const { file } of duplicates) { fs.unlinkSync(path.join(dir, file)); diff --git a/tests/regress-squad-files.test.js b/tests/regress-squad-files.test.js index 3739e6b..7ff5f75 100644 --- a/tests/regress-squad-files.test.js +++ b/tests/regress-squad-files.test.js @@ -399,6 +399,35 @@ test("a primary-agent alias in mode/ shares the file; one in mode/sub/ is never expect(fs.readFileSync(path.join(agentDir, filename), "utf8")).toBe(content); }); +test("pruning a file another loadable agent links to is refused, allow_remove or not", () => { + const { filename, content } = addAgent("grunt", "a/b"); + const mode = path.join(path.dirname(agentDir), "mode"); + fs.mkdirSync(mode); + fs.symlinkSync(path.join(agentDir, filename), path.join(mode, "review.md")); + const result = applySquad({ + roster: { grunts: {}, drills: {} }, + dir: agentDir, + allowRemove: true, + }); + expect(result.ok ? null : result.refusal).toBe("collision"); + expect(result.ok ? [] : result.problems.join("\n")).toMatch( + /\.\.\/mode\/review\.md, grunt-a-b\.md all lead to/, + ); + expect(fs.readFileSync(path.join(mode, "review.md"), "utf8")).toBe(content); +}); + +test("migrating a legacy worker- file another loadable agent links to is refused", () => { + const { content } = addAgent("grunt", "a/b"); + fs.renameSync(path.join(agentDir, "grunt-a-b.md"), path.join(agentDir, "worker-a-b.md")); + const mode = path.join(path.dirname(agentDir), "mode"); + fs.mkdirSync(mode); + fs.symlinkSync(path.join(agentDir, "worker-a-b.md"), path.join(mode, "review.md")); + const result = applySquad({ roster: readSquad(agentDir).roster, dir: agentDir }); + expect(result.ok ? null : result.refusal).toBe("collision"); + expect(fs.readFileSync(path.join(mode, "review.md"), "utf8")).toBe(content); + expect(fs.existsSync(path.join(agentDir, "grunt-a-b.md"))).toBe(false); +}); + 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.