Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 24 additions & 16 deletions src/squad-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}>} */
Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand Down
29 changes: 29 additions & 0 deletions tests/regress-squad-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading