How It Works
How It Works
Section titled “How It Works”@maestria/opencode is a pure plugin with no filesystem side effects. It uses three OpenCode lifecycle hooks to wire itself into every session.
Plugin Hook Architecture
Section titled “Plugin Hook Architecture”The plugin factory runs synchronously at initialization:
- Load agents — Scans the bundled
agents/directory for.mdfiles - Parse frontmatter — Extracts name, description, mode, permissions, and prompt from each file
- Register hooks — Returns an object with
configandsession.compactinghooks
Hook 1: Config
Section titled “Hook 1: Config”The config hook fires when OpenCode initializes:
- Reads all 8 agent
.mdfiles from the bundledagents/directory - Parses YAML frontmatter using a custom parser (~120 lines)
- Merges agents into
input.agent:{ ...input.agent, ...parsedAgents } - Appends the path to
rules/AGENTS.mdintoinput.instructions
This means every agent definition and every global rule is available from the first prompt.
Hook 2: session.compacting
Section titled “Hook 2: session.compacting”The session.compacting hook fires when OpenCode compacts a session (to manage context window size):
- Pushes a context preservation note into
output.context - Notes that task tracking via
todowriteis maintained - Remembers active context: files, decisions, blockers
- Allows the session to continue seamlessly after compaction
Agent File Loading
Section titled “Agent File Loading”The agent loading pipeline:
agents/*.md → readFileSync (all files) → split on "---" (frontmatter delimiter) → parseFrontmatter (custom YAML parser) → { name, config: { description, mode, prompt, permission } } → merged into input.agentRule Injection
Section titled “Rule Injection”Global rules are loaded from rules/AGENTS.md and injected via input.instructions, not via a system.transform hook. This means the rules are part of the session’s system prompt from the start, not appended after the fact.