Skip to content

Register squad agents through the plugin API instead of agent files (#85) - #86

Merged
AlexMKX merged 17 commits into
masterfrom
feat/plugin-agents-85
Nov 20, 2026
Merged

AlexMKX merged 17 commits into
masterfrom
feat/plugin-agents-85

Conversation

@AlexMKX

@AlexMKX AlexMKX commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #85. Closes #52. Closes #87. Closes #88. Closes #89. Closes #90. Closes #91. Closes #92.

The squad no longer exists as agent files. The roster lives in squad.json in the OpenCode config dir, and the plugin registers its agents through the OpenCode 2 agent API. With no agent file written, pruned or migrated, the whole class of filesystem bugs is gone: aliasing (#52), ownership markers (#51), damaged headers (#16) and dangling links. The design and both spike rounds are in #85; the review findings on this PR are #87–#92.

How it works

  • src/squad-store.js: one roster file the plugin owns.

    • It is validated with the same schema as squad_patch.
    • A file that does not parse is never overwritten: nothing is registered from it, squad_dump reports it, and squad_patch refuses.
    • Each read carries a revision, a hash of the bytes it was read from.
  • src/squad-agents.js: agents built after OpenCode's config is applied. Agent transforms run in registration order, and a plugin's setup-time transform runs before the built-in config-agent plugin, which appends the user's global rules to every existing agent. So:

    • early (setup) creates only a throwaway probe agent, with an id random per plugin instance: primary, hidden, deny-all. The config plugin appends the global rules to it, and the early transform records the host's fresh-agent defaults. No squad id is touched, so a user's agent at a squad id never sees a squad field (Agent registration changes the effective configuration of a hand-authored agent sharing its ID #88).
    • late is registered on plugin.updated, in the prompt hook (before the host selects the agent; child sessions included) and in the context hook. It runs after the config plugin. It reads the global rules off the probe and removes it, then builds each squad agent from a fresh default: base, global rules, role rules last. With last-match evaluation a drill stays read-only under a global allow-all, and the host hides its denied tools.
    • Until late runs, squad agents do not exist: an early request fails as an unknown agent instead of running with a permissive catalog (A drill’s first direct request exposes tools outside its read-only policy #87).
    • On each request of an agent late built, the context hook checks the role rules are still last. If the host re-activated plugins and the config plugin's transform landed behind ours, late is re-registered at the end, and the request's tool catalog (built before the hook) is cut to what the role allows.
  • Which agents are the squad's. One the late transform built, or a file an earlier version generated in the global agent dir, verified by its marker, and only when the agent OpenCode loaded at that id is exactly that file: equal, field for field, to what the host builds from the file alone (host defaults, the file's fields, its rules). Only the last generator's format is modelled; an older file is never treated as the live agent, and the dump says to delete it. A verified file proves ownership of the file, not of the id (Agent registration changes the effective configuration of a hand-authored agent sharing its ID #88). Every other agent at a squad id is the user's:

    • it is left exactly as OpenCode built it;
    • the squad entry is not registered;
    • squad_dump notes it;
    • squad_patch refuses to add or change that entry.

    squad_patch uses the same test as registration.

  • Colliding ids (Distinct roster model IDs can silently collapse into one registered agent #90). Model ids that map to one agent id (p/m-1, p/m.1) are refused by squad_patch and by the legacy import. A stored roster registers neither entry and squad_dump names both.

  • squad_patch (Concurrent squad_patch calls can silently erase successful roster changes #89).

    • It validates the input and does every await first: variants, then the host's agent list.
    • Then, synchronously and under an exclusive squad.json.lock, it reads the roster and checks the revision the caller got from squad_dump. A stale revision is refused and nothing is written.
    • Still under the lock, it runs the deletion guard and the ownership check, writes atomically, then re-registers.
    • A lock held by another process is never taken over, however old: the patch is refused, and a lock older than 30 s is named with its holder for the user to delete. Release removes the lock only if it still carries this call's token.
    • Another OpenCode process sharing the config dir picks the change up on its next request: the reload decision compares the content revision, not size and mtime (A valid roster revision can leave live agents stale across OpenCode processes #92).
  • model_data.json that cannot be read (An unreadable model_data.json removes owner rules from live Squad agents #91). One contract for startup, reloads and patches:

    • the plugin keeps the settings it last read without a problem;
    • with none read yet, each squad agent's description says the owner's rules are unknown;
    • squad_dump and the patch report name the file.
  • Migration.

    • On first start, under the lock, a squad of generated grunt-*/drill-*/worker-* files in the global agent dir is imported into squad.json once.
    • The files are left in place; squad_dump lists them as deletable.
    • A file that cannot be read, or a collision, blocks the import, and the dump names it.

Breaking changes

  • The squad is global. The directory argument of squad_dump/squad_patch is removed and refused with an explanation.
  • squad_patch requires the revision from squad_dump.
  • An agents entry in an opencode.json, or an agent file, at a squad id makes that agent the user's. It no longer tunes the squad agent; use squad_patch.
  • squad_patch no longer refuses while model_data.json is unreadable. It writes the roster and names the file instead.
  • No agent files are written. Users who kept the squad in dotfiles would keep squad.json there instead.

Version bumped to 0.6.0: merging this PR cuts the release.

Removed

  • the file writer and its safety net (shared-destination scan, AGENT_SOURCES mirroring the loader, dangling/damaged refusals);
  • agentMarkdown;
  • the startup description rewrite;
  • agent-dir scanning for model_data.json;
  • their tests.

Tests

Not covered

  • A squad session resumed without a new prompt before the late transform has ever run fails as an unknown agent. It fails closed, but it does not run until something registers the squad (any prompt, or plugin.updated).
  • Re-registering the late transform disposes the old registration first. For that moment squad agents do not exist, so a request selecting one then fails closed.
  • Several OpenCode processes are synchronised on the next request, not instantly. Patches are serialised by the lock and the revision.
  • A lock left by a crashed process blocks patches until the user deletes it; the refusal names the file.
  • A project agent identical in every field to a generated legacy file cannot be told from it in the merged registry; telling them apart would mean mirroring the loader across config dirs, which Register squad agents through the plugin API instead of writing agent files #85 removed on purpose.

🤖 Generated with Claude Code

AlexMKX and others added 3 commits November 19, 2026 19:51
The roster lives in squad.json (src/squad-store.js); src/squad-agents.js
registers its agents with an early and a late agent transform. No agent
file is written, pruned or migrated. Legacy generated files are imported
once and hidden; a hand-authored agent with a roster id is left alone.
Unit tests run against a throwaway config dir (tests/setup-isolation.js).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…son (#85)

tests-integration/squad-agents.test.js runs the real plugin on OpenCode
2.1.3 with a global allow-all: drill tools are read-only, rm is refused,
a dropped legacy agent is not dispatchable, squad_patch is live on the
next dispatch, and a hand-authored agent with a squad id is left alone.
The directory argument is gone from the tool schemas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Without squad.json, the legacy squad on disk is the current roster: a
  patch that drops an agent is a deletion, and an unreadable legacy file
  makes dump say the squad is unknown and patch refuse.
- Ours = fenced, or a marker-verified legacy file of the global agent dir
  (pre-fence files and worker-* included). Only those are hidden when the
  roster drops them; generated-looking agents from elsewhere are noted.
- A hand-authored agent on an unchanged entry no longer blocks a patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AlexMKX and others added 6 commits November 19, 2026 21:31
… alone (#87, #88)

The early transform now creates one throwaway probe agent (primary, hidden,
deny-all) instead of the squad's agents. The late transform reads the user's
global rules off it, removes it, and builds each squad agent from a fresh
default with the role rules last. Until the late transform runs a squad agent
does not exist, so a request for it fails instead of running with a
permissive tool catalog.

The late transform is registered on plugin.updated, in the prompt hook
(before the host selects the agent, child sessions included) and in the
context hook. On a squad agent's request the context hook checks that the
role rules are still last, moves the late transform back to the end if the
config plugin's was re-registered behind it, and drops the tools the role
denies from the request's catalog.

A legacy file proves ownership of the file, not the id: an agent at a
legacy or roster id is taken over or hidden only when it is that file (same
prompt and model). Any other agent there is left exactly as the host built it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`p/m-1` and `p/m.1` both become `grunt-p-m-1`. squad_patch now refuses such a
roster before writing; a stored roster registers neither entry and squad_dump
names both; the legacy import refuses files that would collide. The file
writer had this check (#52) and #86 dropped it with the writer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
squad_dump prints a `revision` (a hash of squad.json's bytes, read in the
same read as the roster) and squad_patch requires it back. A patch whose base
is no longer current is refused, so two edits of one dump cannot silently
undo each other and the deletion guard always compares against the roster
actually replaced.

The handler finishes every await (variants, the host's agent list) before it
reads the roster; read, check, plan and write then run synchronously under an
exclusive squad.json.lock, which keeps other OpenCode processes sharing the
config dir out. A held lock is refused with "retry", one older than 30 s is
taken over. The legacy import takes the same lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

One contract for startup, reloads and squad_patch. The plugin keeps the
settings it last read without a problem for the life of the instance; with
none read yet, each squad agent's description says the owner's rules are
unknown and to ask the user before giving it secrets. squad_dump and the patch
report name the file. squad_patch no longer refuses over it: the roster write
never carried the rules, and refusing there while registration went ahead was
the inconsistency the ticket describes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README and squad-draft: squad_dump prints a revision that squad_patch needs
back, a stale one is refused; colliding model ids are refused; an agent of
the user's at a squad id keeps its whole definition; a legacy file is taken
over only when it is the agent OpenCode loaded; squad agents exist only once
OpenCode's config is applied; what an unreadable model_data.json does to
descriptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-request check picked squad agents by their prompt fence, so a stray
fenced file of a project's own, or a legacy agent left live while squad.json
is unreadable, had its tools cut and set off a re-registration on every
request. It now covers only the ids the late transform built.

squad_patch's ownership test matches registration: an agent at an added or
changed id is the user's unless the plugin built it or it is our own legacy
file. A fenced file of someone else's no longer passes as ours, which
reported "+1 added" for an entry that was then not registered.

A failed re-registration no longer leaves a disposed handle behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread tests/squad-store.test.js Fixed
An existsSync check followed by a write to the same path is a
js/file-system-race finding even in a test. An exclusive create both checks
that the lock was released and plays the other holder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread tests/squad-store.test.js Fixed
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AlexMKX and others added 3 commits November 19, 2026 22:18
#92)

The reload decision compared mtime:size, while patches identify a roster by
the hash of its bytes. An equally long roster with the old timestamp left a
running process on the previous roster until some later write. The context
hook now compares the content revision; storeSignature is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stale-lock recovery removed the lock by path, so a process that saw an
abandoned lock could delete the fresh one another process had just taken, and
both writers committed. A lock someone else holds is now never removed,
however old: the patch is refused, and a lock older than 30 s is named with
its holder for the user to delete. Release removes the lock only if it still
carries this call's token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…field for field (#88)

Model and prompt equality could not tell the generated file from a project
agent that copied them and changed its description or variant, so an empty
roster hid the user's agent. The live agent must now equal what the host
builds from the file alone: the host's fresh-agent defaults (seen by the
early transform), the file's fields, and the rules (as a multiset: later
transforms such as the browser plugin's append after the file's own). Only
the last generator's format is modelled; any other file is never treated as
the live agent and the dump says to delete it.

The probe's id is now random per plugin instance, so a user's agent named
opencode-squad-probe is no longer merged into it and removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/squad-store.js Fixed
Comment thread src/squad-store.js Fixed
AlexMKX and others added 2 commits November 19, 2026 22:29
…deQL, tsc)

Release no longer reads the lock back by path: the descriptor it was created
with still being linked means it is still ours at the path. The holder is
read through one descriptor, so its age and owner are of the same file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexMKX
AlexMKX merged commit d06a87a into master Nov 20, 2026
7 checks passed
@AlexMKX
AlexMKX deleted the feat/plugin-agents-85 branch November 20, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment