The Coordinator Guard is a runtime enforcement mechanism in the opencode-swarm-plugin that prevents coordinators from performing tasks that should be delegated to workers. It is integrated into the tool.execute.before plugin hook.
Core Logic
The guard detects if an agent is in a coordinator context (triggered by hive_create_epic, swarm_decompose, or spawning a swarm-worker). If the agent is identified as a coordinator, the guard checks if the requested tool is forbidden. If a violation is detected, it throws a CoordinatorGuardError, which stops tool execution immediately.
Context Detection
Context is session-scoped. An agent is considered a coordinator when:
- An epic is created via
hive_create_epic. - Decomposition occurs via
swarm_decompose. - A task tool spawns a swarm-worker agent.
Worker Safety
Workers are never blocked. The guard uses the agentContext parameter to distinguish between roles:
agentContext: "coordinator": Guard checks are enforced.agentContext: "worker": Guard is bypassed.
// In tool.execute.before hook
if (isInCoordinatorContext(sessionId)) {
const guardResult = checkCoordinatorGuard({
agentContext: "coordinator",
toolName,
toolArgs: output.args,
});
if (guardResult.blocked && guardResult.error) {
throw guardResult.error; // ❌ BLOCKS tool execution
}
}