使用轻量级 Pub/Sub 协议和标准化 SwarmCommand 消息协调多智能体集群执行,管控 token 预算并追踪智能体状态。
Swarm Coordinator is a lightweight coordination skill for multi-agent execution. It helps an agent design, validate, and operate a swarm workflow using:
SwarmCommand messagesUse it when the task is not “one agent answers once”, but multiple agents must coordinate without losing control of cost, state, ownership, or completion criteria.
This skill is intentionally control-oriented: a swarm is only valuable when parallelism improves throughput or quality without causing duplicated work, hidden queue drift, or uncontrolled token spend.
Use this skill for:
Do not use it for simple single-agent tasks. If one agent can complete the job directly, avoid swarm overhead.
A swarm is useful only if it improves throughput or quality without causing coordination chaos.
Always protect four invariants:
If any invariant is missing, the swarm can drift, duplicate work, or burn tokens.
Use swarm only when at least one is true:
If not, keep it single-agent.
Specify:
text
commander / coordinator
executor(s)
reviewer / auditor
monitor / verifier
fallback owner
Each task should have one current assigned_to. Multiple reviewers are allowed, but multiple executors writing the same artifact are not unless explicitly coordinated.
A command must include:
json
{
"command_id": "cmd_12345678",
"timestamp": "2026-04-25T12:00:00Z",
"sender": {"type": "coordinator", "id": "001"},
"target": {"type": "developer", "id": "003"},
"command": {
"action": "develop",
"module": "login",
"requirements": ["JWT auth", "tests"],
"output_format": "python_code"
},
"metadata": {
"priority": "high",
"token_budget": 1500,
"deadline": "2026-04-25T13:00:00Z",
"dependencies": []
},
"negotiation": {
"allowed": true,
"timeout": 300
}
}
Prefer using coordinator/swarm_protocol.py for deterministic command creation and validation. If your project has its own agent taxonomy, map local role names to the protocol roles instead of hard-coding private labels in prompts.
Before publishing to Redis or memory queue, validate:
low | medium | high | criticalIf validation fails, do not publish. Return validation errors and ask for correction or auto-fix safe fields.
Use:
python
from coordinator.pubsub import PubSubCoordinator
from coordinator.swarm_protocol import SwarmProtocol
protocol = SwarmProtocol()
coordinator = PubSubCoordinator(use_redis=True)
command = protocol.create_command(
agent_type="developer",
command={"action": "analyze", "module": "performance"},
priority="high",
token_budget=2000,
)
valid, errors = protocol.validate_command(command)
if valid:
coordinator.publish("tasks", command.to_dict())
else:
print(errors)
Track state transitions:
text
pending → assigned → in_progress → completed / failed / cancelled
No command should remain in_progress forever. Use deadline or heartbeat timeout to trigger fallback.
Completion should include:
success: true/falseIf result is missing required artifacts, mark as incomplete instead of completed.
Use token budget as a control mechanism, not just metadata.
Recommended rules:
ConditionActionbudget remaining > 40%continue normal executionbudget remaining 20-40%compress context and reduce parallel agentsbudget remaining < 20%downgrade model/tier or require coordinator approvalbudget exceededstop publishing new subtasks and request confirmationrepeated failurelower concurrency and route to reviewer/monitor
For local implementation, use coordinator/token_budget.py if available.
Allow negotiation when:
Negotiation output should be a decision, not endless discussion:
json
{
"decision": "assign_to_developer_then_review_by_auditor",
"reason": "developer owns implementation; auditor reviews risk",
"budget_adjustment": 500,
"deadline_adjustment": null,
"blocked": false
}
If negotiation exceeds timeout, coordinator decides or escalates to human.
Handle these failures explicitly:
FailureResponseinvalid commandreject before publish; return schema errorstarget unavailablereroute to fallback ownerdependency blockedkeep pending; notify coordinatorbudget exceededpause or downgrade; do not silently continuedeadline missedmark failed or escalateduplicate ownerchoose one owner; cancel duplicate assignmentincomplete resultreopen task with missing artifact listrepeated failurereduce concurrency; route to reviewer/monitor
Never let failures become silent queue drift.
Ask for human confirmation before swarm actions that are:
Swarm coordination amplifies mistakes. High-risk actions need stronger gates than single-agent execution.
When using this skill, return:
markdown
## Swarm Plan
- Goal:
- Swarm justified? yes/no + reason
- Agents and roles:
- Ownership model:
## Commands
| command_id | target | action | budget | dependencies | gate |
|---|---|---|---:|---|---|
## Coordination Flow
pending → assigned → in_progress → completed/failed
## Budget / Downgrade
- Total budget:
- Per-agent budget:
- Downgrade trigger:
## Failure / Fallback
- Main risks:
- Fallback owner:
- Escalation condition:
## Verification
- Required artifacts:
- Tests / review:
- Done criteria:
For code-facing tasks, also mention which files or APIs to use.
Use these resources when needed:
coordinator/swarm_protocol.py — deterministic SwarmCommand creation, validation, assignment, completion, negotiation helpers.schemas/swarm_command.json — JSON Schema for command validation.tests/test_swarm_protocol.py — regression tests for protocol behavior.test-prompts.json — Darwin-style prompts for future skill regression evaluation.Read or run them when modifying the protocol implementation.
Before calling a swarm workflow ready, check:
A good swarm plan should reduce confusion, not add bureaucracy.
It succeeds when:
If the swarm adds agents without improving control, do not use swarm.