opencode-swarm 6.81.1 → 6.82.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -319,6 +319,835 @@ Disable entirely with `context_budget.enabled: false`.
319
319
 
320
320
  Hard lock on `plan.json` (serialized writes), advisory lock on `events.jsonl` (append-only log). Stale locks auto-expire via `proper-lockfile`.
321
321
 
322
+ ### Agent Categories
323
+
324
+ Agents are classified into four categories for the monitor server `/metadata` endpoint:
325
+
326
+ | Category | Agents |
327
+ |----------|--------|
328
+ | `orchestrator` | architect |
329
+ | `pipeline` | explorer, coder, test_engineer |
330
+ | `qa` | reviewer, critic, critic_sounding_board, critic_drift_verifier |
331
+ | `support` | sme, docs, designer |
332
+
333
+ Use `getAgentCategory(agentName)` from `src/config/agent-categories.ts` to resolve an agent's category at runtime.
334
+
335
+ ---
336
+
337
+ <details>
338
+ <summary><strong>Full Execution Pipeline (Technical Detail)</strong></summary>
339
+
340
+ ### The Pipeline
341
+
342
+ Every task goes through this sequence. No exceptions, no overrides.
343
+
344
+ ```
345
+ MODE: EXECUTE (per task)
346
+
347
+ ├── 5a. @coder implements (ONE task only)
348
+ ├── 5b. diff + imports (contract + dependency analysis + semantic diff context)
349
+ │ └── @system-enhancer injects AST-based semantic diff summary with blast radius
350
+ │ into @reviewer context (up to 10 files, conditional on declared scope)
351
+ ├── 5c. syntax_check (parse validation)
352
+ ├── 5d. placeholder_scan (catches TODOs, stubs, incomplete code)
353
+ ├── 5e. lint fix → lint check
354
+ ├── 5f. build_check (does it compile?)
355
+ ├── 5g. pre_check_batch (4 parallel: lint, secretscan, SAST, quality budget)
356
+ ├── 5h. @reviewer (correctness pass)
357
+ ├── 5i. @reviewer (security pass, if security-sensitive files changed)
358
+ ├── 5j. @test_engineer (verification tests + coverage ≥70%)
359
+ ├── 5k. @test_engineer (adversarial tests)
360
+ ├── 5l. architect regression sweep (scope:"graph" to find cross-task test regressions)
361
+ ├── 5l-ter. test drift detection (conditional — fires when changes involve command behaviour,
362
+ │ parsing/routing logic, user-visible output, public contracts, assertion-heavy areas,
363
+ │ or helper lifecycle changes; validates tests still align with current behaviour)
364
+ ├── 5m. ⛔ Pre-commit checklist (all 4 items required, no override)
365
+ └── 5n. Task marked complete, evidence written
366
+ ```
367
+
368
+ If any step fails, the coder gets structured feedback and retries. After 5 failures on the same task, it escalates to you.
369
+
370
+ ### Architect Workflow Modes
371
+
372
+ The architect moves through these modes automatically:
373
+
374
+ | Mode | What It Means |
375
+ |---|---|
376
+ | `RESUME` | Existing `.swarm/` state was found, so Swarm continues where it left off |
377
+ | `CLARIFY` | Swarm asks for missing information it cannot infer |
378
+ | `DISCOVER` | Explorer scans the codebase; co-change dark matter analysis runs automatically to detect hidden file couplings (v6.41) |
379
+ | `CONSULT` | SME agents provide domain guidance |
380
+ | `PLAN` | Architect writes or updates the phased plan (includes CODEBASE REALITY CHECK on brownfield projects) |
381
+ | `CRITIC-GATE` | Critic reviews the plan before execution |
382
+ | `EXECUTE` | Tasks are implemented one at a time through the QA pipeline |
383
+ | `PHASE-WRAP` | A phase closes out, including: explorer rescan, docs update, `context.md` update, `write_retro`, evidence check, `sbom_generate`, **`@critic_drift_verifier` delegation** (drift check — blocking gate), `write_drift_evidence` call with verdict, mandatory gate evidence verification (`completion-verify.json` + `drift-verifier.json` both required), then `phase_complete` |
384
+
385
+ > **CODEBASE REALITY CHECK (v6.29.2):** Before any planning, the Architect dispatches Explorer to verify the current state of every referenced item. Produces a CODEBASE REALITY REPORT with statuses: NOT STARTED, PARTIALLY DONE, ALREADY COMPLETE, or ASSUMPTION INCORRECT. This prevents planning against stale assumptions. Skipped for greenfield projects with no existing codebase references.
386
+
387
+ > **Phase Completion Gates (v6.33.4):** Before a phase can be marked complete, two mandatory gates are enforced: (1) completion-verify — deterministic check that plan task identifiers exist in source files, and (2) critic_drift_verifier evidence — verification that the drift verifier approved the implementation. Both gates are automatically bypassed when turbo mode is active.
388
+
389
+ ### Important
390
+
391
+ A second or later run does **not** necessarily look like a first run.
392
+
393
+ If `.swarm/plan.md` already exists, the architect may enter `RESUME` and then go directly into `EXECUTE`. That is expected and does **not** mean Swarm stopped using agents.
394
+
395
+ Use `/swarm status` if you are unsure what Swarm is doing.
396
+
397
+ Release automation uses release-please and requires conventional commit prefixes such as `fix:` or `feat:` on changes merged to `main`.
398
+
399
+ </details>
400
+
401
+ <details>
402
+ <summary><strong>Persistent Memory (What's in .swarm/)</strong></summary>
403
+
404
+ ### plan.md: Your Project Roadmap
405
+
406
+ ```markdown
407
+ # Project: Auth System
408
+ Current Phase: 2
409
+
410
+ ## Phase 1: Foundation [COMPLETE]
411
+ - [x] Task 1.1: Create user model [SMALL]
412
+ - [x] Task 1.2: Add password hashing [SMALL]
413
+ - [x] Task 1.3: Database migrations [MEDIUM]
414
+
415
+ ## Phase 2: Core Auth [IN PROGRESS]
416
+ - [x] Task 2.1: Login endpoint [MEDIUM]
417
+ - [ ] Task 2.2: JWT generation [MEDIUM] (depends: 2.1) ← CURRENT
418
+ - Acceptance: Returns valid JWT with user claims, 15-minute expiry
419
+ - Attempt 1: REJECTED — missing expiration claim
420
+ - [ ] Task 2.3: Token validation middleware [MEDIUM]
421
+ ```
422
+
423
+ ### context.md: What's Been Decided
424
+
425
+ ```markdown
426
+ ## Technical Decisions
427
+ - bcrypt cost factor: 12
428
+ - JWT TTL: 15 minutes; refresh TTL: 7 days
429
+
430
+ ## SME Guidance (cached, never re-asked)
431
+ ### security (Phase 1)
432
+ - Never log tokens or passwords
433
+ - Rate-limit login: 5 attempts / 15 min per IP
434
+
435
+ ### api (Phase 1)
436
+ - Return 401 for invalid credentials (not 404)
437
+ ```
438
+
439
+ ### Evidence Bundles
440
+
441
+ Every completed task writes structured evidence to `.swarm/evidence/`:
442
+
443
+ | Type | What It Captures |
444
+ |------|--------------------|
445
+ | review | Verdict, risk level, specific issues |
446
+ | test | Pass/fail counts, coverage %, failure messages |
447
+ | diff | Files changed, additions/deletions |
448
+ | retrospective | Phase metrics, lessons learned, error taxonomy classification (injected into next phase) |
449
+ | secretscan | Secret scan results: findings count, files scanned, skipped files (v6.33) |
450
+ | completion-verify | Deterministic gate: verifies plan task identifiers exist in source files (written automatically by `completion-verify` tool; required before `phase_complete`) |
451
+ | drift-verifier | Phase-close drift gate: `critic_drift_verifier` verdict (APPROVED/NEEDS_REVISION) and summary (written by architect via `write_drift_evidence`; required before `phase_complete`) |
452
+
453
+ ### telemetry.jsonl: Session Observability
454
+
455
+ Swarm emits structured JSONL events to `.swarm/telemetry.jsonl` for observability tooling (dashboards, alerting, audit logs). Events are fire-and-forget — failures never affect execution.
456
+
457
+ ```json
458
+ {"timestamp":"2026-03-25T14:30:00.000Z","event":"session_started","sessionId":"abc123","agentName":"architect"}
459
+ {"timestamp":"2026-03-25T14:30:05.000Z","event":"delegation_begin","sessionId":"abc123","agentName":"coder","taskId":"1.1"}
460
+ {"timestamp":"2026-03-25T14:31:00.000Z","event":"delegation_end","sessionId":"abc123","agentName":"coder","taskId":"1.1","result":"success"}
461
+ {"timestamp":"2026-03-25T14:31:10.000Z","event":"gate_passed","sessionId":"abc123","gate":"reviewer","taskId":"1.1"}
462
+ {"timestamp":"2026-03-25T14:32:00.000Z","event":"phase_changed","sessionId":"abc123","oldPhase":1,"newPhase":2}
463
+ ```
464
+
465
+ | Event | When Emitted |
466
+ |-------|-------------|
467
+ | `session_started` | New agent session created |
468
+ | `session_ended` | Session ends (reason: normal, timeout, error) |
469
+ | `agent_activated` | Agent identity confirmed via chat.message |
470
+ | `delegation_begin` | Task dispatched to a sub-agent |
471
+ | `delegation_end` | Sub-agent returns (success, rejected, error) |
472
+ | `task_state_changed` | Task workflow state transitions |
473
+ | `gate_passed` | Evidence written to `.swarm/evidence/{taskId}.json` |
474
+ | `gate_failed` | Gate check blocked task completion |
475
+ | `phase_changed` | Phase completed and new phase started |
476
+ | `budget_updated` | Context budget crossed warning/critical threshold |
477
+ | `hard_limit_hit` | Tool call/duration/repetition limit reached |
478
+ | `revision_limit_hit` | Coder revision limit exceeded |
479
+ | `loop_detected` | Repetitive tool call pattern detected |
480
+ | `scope_violation` | Architect wrote outside declared scope |
481
+ | `qa_skip_violation` | QA gate skipped without valid reason |
482
+ | `model_fallback` | Transient error triggered model fallback |
483
+ | `heartbeat` | 30-second throttled keep-alive signal |
484
+
485
+ File rotates automatically at 10MB to `.swarm/telemetry.jsonl.1`.
486
+
487
+ </details>
488
+
489
+ <details>
490
+ <summary><strong>Save Plan Tool: Target Workspace Requirement</strong></summary>
491
+
492
+ The `save_plan` tool requires an explicit target workspace path. It does **not** fall back to `process.cwd()`.
493
+
494
+ ### Explicit Workspace Requirement
495
+
496
+ - The `working_directory` parameter must be provided
497
+ - Providing no value or relying on implicit directory resolution will result in deterministic failure
498
+
499
+ ### Failure Conditions
500
+
501
+ | Condition | Behavior |
502
+ |-----------|----------|
503
+ | Missing (`undefined` / `null`) | Fails with: "Target workspace is required" |
504
+ | Empty or whitespace-only | Fails with: "Target workspace cannot be empty or whitespace" |
505
+ | Path traversal (`..`) | Fails with: "Target workspace cannot contain path traversal" |
506
+
507
+ ### Usage Contract
508
+
509
+ When using `save_plan`, always pass a valid `working_directory`:
510
+
511
+ ```typescript
512
+ save_plan({
513
+ title: "My Project",
514
+ swarm_id: "mega",
515
+ phases: [{ id: 1, name: "Setup", tasks: [{ id: "1.1", description: "Initialize project" }] }],
516
+ working_directory: "/path/to/project" // Required - no fallback
517
+ })
518
+ ```
519
+
520
+ </details>
521
+
522
+ <details>
523
+ <summary><strong>Guardrails and Circuit Breakers</strong></summary>
524
+
525
+ Every agent runs inside a circuit breaker that kills runaway behavior before it burns your credits.
526
+
527
+ | Signal | Default Limit | What Happens |
528
+ |--------|:---:|-------------|
529
+ | Tool calls | 200 | Agent is stopped |
530
+ | Duration | 30 min | Agent is stopped |
531
+ | Same tool repeated | 10x | Agent is warned, then stopped |
532
+ | Consecutive errors | 5 | Agent is stopped |
533
+
534
+ Limits reset per task. A coder working on Task 2.3 is not penalized for tool calls made during Task 2.2.
535
+
536
+ #### Architect Self-Coding Block
537
+
538
+ If the architect writes files directly instead of delegating to the coder, a hard block fires:
539
+
540
+ | Write count | Behavior |
541
+ |:-----------:|----------|
542
+ | 1–2 | Warning injected into next architect message |
543
+ | ≥ 3 | `Error` thrown with `SELF_CODING_BLOCK` — identifies file paths written and count |
544
+
545
+ The counter resets only when a coder delegation is dispatched. This is a hard enforcement — not advisory.
546
+
547
+ Per-agent overrides:
548
+
549
+ ```json
550
+ {
551
+ "guardrails": {
552
+ "profiles": {
553
+ "coder": { "max_tool_calls": 500, "max_duration_minutes": 60 },
554
+ "explorer": { "max_tool_calls": 50 }
555
+ }
556
+ }
557
+ }
558
+ ```
559
+
560
+ </details>
561
+
562
+ <details>
563
+ <summary><strong>File Authority (Per-Agent Write Permissions)</strong></summary>
564
+
565
+ Swarm enforces per-agent file write authority — each agent can only write to specific paths. By default, these rules are hardcoded, but you can override them via config.
566
+
567
+ ### Default Rules
568
+
569
+ | Agent | Can Write | Blocked | Zones |
570
+ |-------|-----------|---------|-------|
571
+ | `architect` | Everything (except plan files) | `.swarm/plan.md`, `.swarm/plan.json` | `generated` |
572
+ | `coder` | `src/`, `tests/`, `docs/`, `scripts/` | `.swarm/` (entire directory) | `generated`, `config` |
573
+ | `reviewer` | `.swarm/evidence/`, `.swarm/outputs/` | `src/`, `.swarm/plan.md`, `.swarm/plan.json` | `generated` |
574
+ | `test_engineer` | `tests/`, `.swarm/evidence/` | `src/`, `.swarm/plan.md`, `.swarm/plan.json` | `generated` |
575
+ | `explorer` | Read-only | Everything | — |
576
+ | `sme` | Read-only | Everything | — |
577
+ | `docs` | `docs/`, `.swarm/outputs/` | — | `generated` |
578
+ | `designer` | `docs/`, `.swarm/outputs/` | — | `generated` |
579
+ | `critic` | `.swarm/evidence/` | — | `generated` |
580
+
581
+ ### Prefixed Agents
582
+
583
+ Prefixed agents (e.g., `paid_coder`, `mega_reviewer`, `local_architect`) inherit defaults from their canonical base agent via `stripKnownSwarmPrefix`. The lookup order is:
584
+
585
+ 1. Exact match for the prefixed name (if explicitly defined in user config)
586
+ 2. Fall back to the canonical agent's defaults (e.g., `paid_coder` → `coder`)
587
+
588
+ ```json
589
+ {
590
+ "authority": {
591
+ "rules": {
592
+ "coder": { "allowedPrefix": ["src/", "lib/"] },
593
+ "paid_coder": { "allowedPrefix": ["vendor/", "plugins/"] }
594
+ }
595
+ }
596
+ }
597
+ ```
598
+
599
+ In this example, `paid_coder` gets its own explicit rule, while other prefixed coders (e.g., `mega_coder`) fall back to `coder`.
600
+
601
+ ### Runtime Enforcement
602
+
603
+ Architect direct writes are enforced at runtime via `toolBefore` hook. This tracks writes to source code paths outside `.swarm/` and protects `.swarm/plan.md` and `.swarm/plan.json` from direct modification.
604
+
605
+ ### Configuration
606
+
607
+ Override default rules in `.opencode/opencode-swarm.json`:
608
+
609
+ ```json
610
+ {
611
+ "authority": {
612
+ "enabled": true,
613
+ "rules": {
614
+ "coder": {
615
+ "allowedPrefix": ["src/", "lib/", "scripts/"],
616
+ "blockedPrefix": [".swarm/"],
617
+ "blockedZones": ["generated"]
618
+ },
619
+ "explorer": {
620
+ "readOnly": false,
621
+ "allowedPrefix": ["notes/", "scratch/"]
622
+ }
623
+ }
624
+ }
625
+ }
626
+ ```
627
+
628
+ ### Rule Fields
629
+
630
+ | Field | Type | Description |
631
+ |-------|------|-------------|
632
+ | `readOnly` | boolean | If `true`, agent cannot write anywhere |
633
+ | `blockedExact` | string[] | Exact file paths that are blocked |
634
+ | `allowedExact` | string[] | Exact file paths that are allowed (overrides prefix/glob restrictions) |
635
+ | `blockedPrefix` | string[] | Path prefixes that are blocked (e.g., `.swarm/`) |
636
+ | `allowedPrefix` | string[] | Only these path prefixes are allowed. Omit to remove restriction; set `[]` to deny all |
637
+ | `blockedGlobs` | string[] | Glob patterns that are blocked (uses picomatch: `**`, `*`, `?`) |
638
+ | `allowedGlobs` | string[] | Glob patterns that are allowed (uses picomatch: `**`, `*`, `?`) |
639
+ | `blockedZones` | string[] | File zones to block: `production`, `test`, `config`, `generated`, `docs`, `build` |
640
+
641
+ ### Merge Behavior
642
+
643
+ - User rules **override** hardcoded defaults for the specified agent
644
+ - Scalar fields (`readOnly`) — user value replaces default
645
+ - Array fields (`blockedPrefix`, `allowedPrefix`, etc.) — user array **replaces** entirely (not merged)
646
+ - If a field is omitted in the user rule for a **known agent** (one with hardcoded defaults), the default value for that field is preserved
647
+ - If a field is omitted in the user rule for a **custom agent** (not in the defaults list), that field is `undefined` — there are no defaults to inherit
648
+ - `allowedPrefix: []` explicitly denies all writes; omitting `allowedPrefix` entirely means no allowlist restriction is applied (all paths are evaluated against blocklist rules only)
649
+ - Setting `enabled: false` ignores all custom rules and uses hardcoded defaults
650
+
651
+ ### Custom Agents
652
+
653
+ Custom agents (not in the defaults list) start with no rules. Their write authority depends entirely on what you configure:
654
+
655
+ - **Not in config at all** — agent is denied with `Unknown agent` (no rule exists; this is not the same as "blocked from all writes")
656
+ - **In config without `allowedPrefix`** — no allowlist restriction applies; only any `blockedPrefix`, `blockedZones`, or `readOnly` rules you explicitly set will enforce limits
657
+ - **In config with `allowedPrefix: []`** — all writes are denied
658
+
659
+ To safely restrict a custom agent, always set `allowedPrefix` explicitly:
660
+
661
+ ```json
662
+ {
663
+ "authority": {
664
+ "rules": {
665
+ "my_custom_agent": {
666
+ "allowedPrefix": ["plugins/", "extensions/"],
667
+ "blockedZones": ["generated"]
668
+ }
669
+ }
670
+ }
671
+ }
672
+ ```
673
+
674
+ ### Advanced Examples
675
+
676
+ #### Glob Pattern Support
677
+
678
+ Use glob patterns for complex path matching:
679
+
680
+ ```json
681
+ {
682
+ "authority": {
683
+ "rules": {
684
+ "coder": {
685
+ "allowedGlobs": ["src/**/*.ts", "tests/**/*.test.ts"],
686
+ "blockedGlobs": ["src/**/*.generated.ts", "**/*.d.ts"],
687
+ "allowedExact": ["src/index.ts", "package.json"]
688
+ },
689
+ "docs_agent": {
690
+ "allowedGlobs": ["docs/**/*.md", "*.md"],
691
+ "blockedExact": [".swarm/plan.md"]
692
+ }
693
+ }
694
+ }
695
+ }
696
+ ```
697
+
698
+ **Glob Pattern Features:**
699
+ - `**` — Match any number of directories: `src/**/*.ts` matches all TypeScript files in src/ and subdirectories
700
+ - `*` — Match any characters except path separators: `*.md` matches all Markdown files in current directory
701
+ - `?` — Match single character: `test?.js` matches `test1.js`, `testa.js`
702
+ - Uses [picomatch](https://github.com/micromatch/picomatch) for cross-platform compatibility
703
+
704
+ **Path Normalization and Symlinks:**
705
+ Paths are resolved via `realpathSync` before matching, which resolves symlinks and prevents path-traversal escapes. However, if a symlink's target does not exist, `realpathSync` throws and the fallback returns the symlink's own path (unresolved). A dangling symlink inside an `allowedPrefix` directory will therefore pass prefix-based checks even if its intended target is outside the project. Use `blockedExact` or `blockedGlobs` to deny known dangling-symlink paths explicitly.
706
+
707
+ **Evaluation Order:**
708
+ 1. `readOnly` check (if true, deny all writes)
709
+ 2. `blockedExact` (exact path matches, highest priority)
710
+ 3. `blockedGlobs` (glob pattern matches)
711
+ 4. `allowedExact` (exact path matches, overrides prefix/glob restrictions)
712
+ 5. `allowedGlobs` (glob pattern matches)
713
+ 6. `blockedPrefix` (prefix matches)
714
+ 7. `allowedPrefix` (prefix matches)
715
+ 8. `blockedZones` (zone classification)
716
+
717
+ </details>
718
+
719
+ <details>
720
+ <summary><strong>Context Budget Guard</strong></summary>
721
+
722
+ The Context Budget Guard monitors how much context Swarm is injecting into the conversation. It helps prevent context overflow before it becomes a problem.
723
+
724
+ ### Default Behavior
725
+
726
+ - **Enabled automatically** — No setup required. Swarm starts tracking context usage right away.
727
+ - **What it measures** — Only the context that Swarm injects (plan, context, evidence, retrospectives). It does **not** count your chat history or the model's responses.
728
+ - **Warning threshold (0.7 ratio)** — When swarm-injected context reaches ~2800 tokens (70% of 4000), the architect receives a one-time advisory warning. This is informational — execution continues normally.
729
+ - **Critical threshold (0.9 ratio)** — When context reaches ~3600 tokens (90% of 4000), the architect receives a critical alert with a recommendation to run `/swarm handoff`. This is also one-time only.
730
+ - **Non-nagging** — Alerts fire once per session, not repeatedly. You won't be pestered every turn.
731
+ - **Who sees warnings** — Only the architect receives these warnings. Other agents are unaware of the budget.
732
+
733
+ To disable entirely, set `context_budget.enabled: false` in your swarm config.
734
+
735
+ ### Configuration Reference
736
+
737
+ | Key | Type | Default | Description |
738
+ |-----|------|---------|-------------|
739
+ | `context_budget.enabled` | boolean | `true` | Enable or disable the context budget guard entirely |
740
+ | `context_budget.max_injection_tokens` | number | `4000` | Token budget for swarm-injected context per turn. This is NOT the model's context window — it's the swarm plugin's own contribution |
741
+ | `context_budget.warn_threshold` | number | `0.7` | Ratio (0.0-1.0) of `max_injection_tokens` that triggers a warning advisory |
742
+ | `context_budget.critical_threshold` | number | `0.9` | Ratio (0.0-1.0) of `max_injection_tokens` that triggers a critical alert with handoff recommendation |
743
+ | `context_budget.enforce` | boolean | `true` | When true, enforces budget limits and may trigger handoffs |
744
+ | `context_budget.prune_target` | number | `0.7` | Ratio (0.0-1.0) of context to preserve when pruning occurs |
745
+ | `context_budget.preserve_last_n_turns` | number | `4` | Number of recent turns to preserve when pruning |
746
+ | `context_budget.recent_window` | number | `10` | Number of turns to consider as "recent" for scoring |
747
+ | `context_budget.tracked_agents` | string[] | `['architect']` | Agents to track for context budget warnings |
748
+ | `context_budget.enforce_on_agent_switch` | boolean | `true` | Enforce budget limits when switching agents |
749
+ | `context_budget.model_limits` | record | `{ default: 128000 }` | Per-model token limits (model name -> max tokens) |
750
+ | `context_budget.tool_output_mask_threshold` | number | `2000` | Threshold for masking tool outputs (chars) |
751
+ | `context_budget.scoring.enabled` | boolean | `false` | Enable context scoring/ranking |
752
+ | `context_budget.scoring.max_candidates` | number | `100` | Maximum items to score (10-500) |
753
+ | `context_budget.scoring.weights` | object | `{ recency: 0.3, ... }` | Scoring weights for priority |
754
+ | `context_budget.scoring.decision_decay` | object | `{ mode: 'exponential', half_life_hours: 24 }` | Decision relevance decay |
755
+ | `context_budget.scoring.token_ratios` | object | `{ prose: 0.25, code: 0.4, ... }` | Token cost multipliers |
756
+
757
+ ### Example Configurations
758
+
759
+ **Minimal (disable):**
760
+ ```json
761
+ {
762
+ "context_budget": {
763
+ "enabled": false
764
+ }
765
+ }
766
+ ```
767
+
768
+ **Default (reference):**
769
+ ```json
770
+ {
771
+ "context_budget": {
772
+ "enabled": true,
773
+ "max_injection_tokens": 4000,
774
+ "warn_threshold": 0.7,
775
+ "critical_threshold": 0.9,
776
+ "enforce": true,
777
+ "prune_target": 0.7,
778
+ "preserve_last_n_turns": 4,
779
+ "recent_window": 10,
780
+ "tracked_agents": ["architect"],
781
+ "enforce_on_agent_switch": true,
782
+ "model_limits": { "default": 128000 },
783
+ "tool_output_mask_threshold": 2000,
784
+ "scoring": {
785
+ "enabled": false,
786
+ "max_candidates": 100,
787
+ "weights": { "recency": 0.3, "relevance": 0.4, "importance": 0.3 },
788
+ "decision_decay": { "mode": "exponential", "half_life_hours": 24 },
789
+ "token_ratios": { "prose": 0.25, "code": 0.4, "json": 0.6, "logs": 0.1 }
790
+ }
791
+ }
792
+ }
793
+ ```
794
+
795
+ **Aggressive (for long-running sessions):**
796
+ ```json
797
+ {
798
+ "context_budget": {
799
+ "enabled": true,
800
+ "max_injection_tokens": 2000,
801
+ "warn_threshold": 0.5,
802
+ "critical_threshold": 0.75,
803
+ "enforce": true,
804
+ "prune_target": 0.6,
805
+ "preserve_last_n_turns": 2,
806
+ "recent_window": 5,
807
+ "tracked_agents": ["architect"],
808
+ "enforce_on_agent_switch": true,
809
+ "model_limits": { "default": 128000 },
810
+ "tool_output_mask_threshold": 1500,
811
+ "scoring": {
812
+ "enabled": true,
813
+ "max_candidates": 50,
814
+ "weights": { "recency": 0.5, "relevance": 0.3, "importance": 0.2 },
815
+ "decision_decay": { "mode": "linear", "half_life_hours": 12 },
816
+ "token_ratios": { "prose": 0.2, "code": 0.35, "json": 0.5, "logs": 0.05 }
817
+ }
818
+ }
819
+ }
820
+ ```
821
+
822
+ ### What This Does NOT Do
823
+
824
+ - **Does NOT prune chat history** — Your conversation with the model is untouched
825
+ - **Does NOT modify tool outputs** — What tools return is unchanged
826
+ - **Does NOT block execution** — The guard is advisory only; it warns but never stops the pipeline
827
+ - **Does NOT interact with compaction.auto** — Separate feature with separate configuration
828
+ - **Only measures swarm's injected context** — Not the full context window, just what Swarm adds
829
+
830
+ </details>
831
+
832
+ <details>
833
+ <summary><strong>Quality Gates (Technical Detail)</strong></summary>
834
+
835
+ ### Built-in Tools
836
+
837
+ | Tool | What It Does |
838
+ |------|-------------|
839
+ | syntax_check | Tree-sitter validation across 12 languages |
840
+ | placeholder_scan | Catches TODOs, FIXMEs, stubs, placeholder text |
841
+ | sast_scan | Offline security analysis, 63+ rules, 9 languages |
842
+ | sbom_generate | CycloneDX dependency tracking, 8 ecosystems |
843
+ | build_check | Runs your project's native build/typecheck |
844
+ | incremental_verify | Post-coder typecheck for TS/JS, Go, Rust, C# (v6.29.2) |
845
+ | quality_budget | Enforces complexity, duplication, and test ratio limits |
846
+ | pre_check_batch | Runs lint, secretscan, SAST, and quality budget in parallel (~15s vs ~60s sequential) |
847
+ | phase_complete | Enforces phase completion, verifies required agents, requires a valid retrospective evidence bundle, logs events, and resets state; appends to `events.jsonl` with file locking |
848
+ | mutation_test | Applies LLM-generated mutation patches to source files and runs tests to measure kill rate; verdict is pass/warn/fail based on configurable thresholds; used by the mutation_test gate (opt-in, off by default) |
849
+ | generate_mutants | Architect-only: generates LLM-based mutation patches (5–10 per function across 6 types: off-by-one, null substitution, operator swap, guard removal, branch swap, side-effect deletion) for direct consumption by the mutation_test tool; returns SKIP verdict on LLM failure rather than throwing |
850
+ | write_mutation_evidence | Architect-only: writes mutation gate results atomically to `.swarm/evidence/{phase}/mutation-gate.json`; accepts verdict (PASS/WARN/FAIL/SKIP), kill rate metrics, and optional survived mutant details; normalizes uppercase-to-lowercase before persisting |
851
+
852
+
853
+ All tools run locally. No Docker, no network calls, no external APIs.
854
+
855
+ Optional enhancement: Semgrep (if on PATH).
856
+
857
+ ### Gate Configuration
858
+
859
+ ```json
860
+ {
861
+ "gates": {
862
+ "syntax_check": { "enabled": true },
863
+ "placeholder_scan": { "enabled": true },
864
+ "sast_scan": { "enabled": true },
865
+ "quality_budget": {
866
+ "enabled": true,
867
+ "max_complexity_delta": 5,
868
+ "min_test_to_code_ratio": 0.3
869
+ }
870
+ }
871
+ }
872
+ ```
873
+
874
+ </details>
875
+
876
+ <details>
877
+ <summary><strong>File Locking for Concurrent Write Safety</strong></summary>
878
+
879
+ Swarm uses file locking to protect shared state files from concurrent write corruption. The locking strategy differs by file: `plan.json` uses hard locking (write blocked on contention), while `events.jsonl` uses advisory locking (write proceeds with a warning on contention).
880
+
881
+ ### Locking Implementation
882
+
883
+ - **Library**: `proper-lockfile` with `retries: 0` (fail-fast — no polling retries)
884
+ - **Scope**: Each tool acquires an exclusive lock on the target file before writing
885
+ - **Agents**: Lock is tagged with the current agent name and task context for diagnostics
886
+
887
+ ### Protected Files
888
+
889
+ | File | Tool | Lock Key |
890
+ |------|------|----------|
891
+ | `.swarm/plan.json` | `update_task_status` | `plan.json` |
892
+ | `.swarm/events.jsonl` | `phase_complete` | `events.jsonl` |
893
+
894
+ ### Lock Semantics
895
+
896
+ The two protected tools use different strategies:
897
+
898
+ **`update_task_status` — Hard lock on `plan.json`**
899
+
900
+ When two calls contend for `plan.json`:
901
+ 1. **Exactly one call wins** — only the first to acquire the lock proceeds
902
+ 2. **Winner writes** — the lock holder writes to the file, then releases the lock
903
+ 3. **Losers receive `success: false`** — with `recovery_guidance: "retry"` and an error message identifying the lock holder
904
+
905
+ ```json
906
+ {
907
+ "success": false,
908
+ "message": "Task status write blocked: plan.json is locked by architect (task: update-task-status-1.1-1234567890)",
909
+ "errors": ["Concurrent plan write detected — retry after the current write completes"],
910
+ "recovery_guidance": "Wait a moment and retry update_task_status. The lock will expire automatically if the holding agent fails."
911
+ }
912
+ ```
913
+
914
+ **What the caller should do**: Retry `update_task_status` after a short delay.
915
+
916
+ **`phase_complete` — Advisory lock on `events.jsonl`**
917
+
918
+ When two calls contend for `events.jsonl`:
919
+ 1. **Lock is attempted** — if acquired, write is serialized
920
+ 2. **If lock unavailable** — a warning is added to the result and the write proceeds anyway
921
+ 3. **Both callers return `success: true`** — duplicate concurrent appends are possible but `events.jsonl` is an append-only log and duplicate phase entries do not corrupt state
922
+
923
+ This asymmetry is intentional: `plan.json` stores mutable structured JSON where concurrent overwrites produce malformed files; `events.jsonl` is an append-only log where a duplicate entry is a recoverable nuisance.
924
+
925
+ ### Lock Recovery
926
+
927
+ If a lock-holding agent crashes or hangs, the lock file will eventually expire (handled by `proper-lockfile` stale-lock cleanup). On the next retry, the call will succeed. Swarm does not auto-retry on lock contention — the architect receives the error and decides when to retry.
928
+
929
+ </details>
930
+
931
+ <details>
932
+ <summary id="configuration-reference"><strong>Full Configuration Reference</strong></summary>
933
+
934
+ Config file location: `~/.config/opencode/opencode-swarm.json` (global) or `.opencode/opencode-swarm.json` (project). Project config merges over global.
935
+
936
+ ```json
937
+ {
938
+ "agents": {
939
+ "architect": { "model": "anthropic/claude-opus-4-6" },
940
+ "coder": { "model": "minimax-coding-plan/MiniMax-M2.5", "fallback_models": ["minimax-coding-plan/MiniMax-M2.1"] },
941
+ "explorer": { "model": "minimax-coding-plan/MiniMax-M2.1" },
942
+ "sme": { "model": "kimi-for-coding/k2p5" },
943
+ "critic": { "model": "zai-coding-plan/glm-5" },
944
+ "reviewer": { "model": "zai-coding-plan/glm-5", "fallback_models": ["opencode/big-pickle"] },
945
+ "test_engineer": { "model": "minimax-coding-plan/MiniMax-M2.5" },
946
+ "docs": { "model": "zai-coding-plan/glm-4.7-flash" },
947
+ "designer": { "model": "kimi-for-coding/k2p5" }
948
+ },
949
+ "guardrails": {
950
+ "max_tool_calls": 200,
951
+ "max_duration_minutes": 30,
952
+ "profiles": {
953
+ "coder": { "max_tool_calls": 500 }
954
+ }
955
+ },
956
+ "authority": {
957
+ "enabled": true,
958
+ "rules": {
959
+ "coder": {
960
+ "allowedPrefix": ["src/", "lib/"],
961
+ "blockedPrefix": [".swarm/"],
962
+ "blockedZones": ["generated"]
963
+ }
964
+ }
965
+ },
966
+ "review_passes": {
967
+ "always_security_review": false,
968
+ "security_globs": ["**/*auth*", "**/*crypto*", "**/*session*"]
969
+ },
970
+ "automation": {
971
+ "mode": "manual",
972
+ "capabilities": {
973
+ "plan_sync": true,
974
+ "phase_preflight": false,
975
+ "config_doctor_on_startup": false,
976
+ "config_doctor_autofix": false,
977
+ "evidence_auto_summaries": true,
978
+ "decision_drift_detection": true
979
+ }
980
+ },
981
+ "knowledge": {
982
+ "enabled": true,
983
+ "swarm_max_entries": 100,
984
+ "hive_max_entries": 1000,
985
+ "auto_promote_days": 30,
986
+ "max_inject_count": 5,
987
+ "dedup_threshold": 0.6,
988
+ "scope_filter": ["global"],
989
+ "hive_enabled": true,
990
+ "rejected_max_entries": 200,
991
+ "validation_enabled": true,
992
+ "evergreen_confidence": 0.8,
993
+ "evergreen_utility": 0.5,
994
+ "low_utility_threshold": 0.2,
995
+ "min_retrievals_for_utility": 3,
996
+ "schema_version": "v6.17"
997
+ }
998
+ }
999
+ ```
1000
+
1001
+ ### Automation
1002
+
1003
+ ## Mode Detection (v6.13)
1004
+
1005
+ Swarm now explicitly distinguishes five architect modes:
1006
+
1007
+ - **`DISCOVER`** — After the explorer finishes scanning the codebase.
1008
+ - **`PLAN`** — When the architect writes or updates the plan.
1009
+ - **`EXECUTE`** — During task implementation (the normal pipeline).
1010
+ - **`PHASE-WRAP`** — After all tasks in a phase are completed, before docs are updated.
1011
+ - **`UNKNOWN`** — Fallback when the current state does not match any known mode.
1012
+
1013
+ Each mode determines which injection blocks are added to the LLM prompt (e.g., plan cursor is injected in `PLAN`, tool output truncation in `EXECUTE`, etc.).
1014
+
1015
+ Default mode: `manual`. No background automation — all actions require explicit slash commands.
1016
+
1017
+ Modes:
1018
+
1019
+ - `manual` — No background automation. All actions via slash commands (default).
1020
+ - `hybrid` — Background automation for safe operations, manual for sensitive ones.
1021
+ - `auto` — Full background automation.
1022
+
1023
+ Capability defaults:
1024
+
1025
+ - `plan_sync`: `true` — Background plan synchronization using `fs.watch` with debounced writes (300ms) and 2-second polling fallback
1026
+ - `phase_preflight`: `false` — Phase preflight checks before agent execution (opt-in)
1027
+ - `config_doctor_on_startup`: `false` — Validate configuration on startup
1028
+ - `config_doctor_autofix`: `false` — Auto-fix for config doctor (opt-in, security-sensitive)
1029
+ - `evidence_auto_summaries`: `true` — Automatic summaries for evidence bundles
1030
+ - `decision_drift_detection`: `true` — Detect drift between planned and actual decisions
1031
+
1032
+ ## Plan Cursor (v6.13)
1033
+
1034
+ The `plan_cursor` config compresses the plan that is injected into the LLM context.
1035
+
1036
+ ```json
1037
+ {
1038
+ "plan_cursor": {
1039
+ "enabled": true,
1040
+ "max_tokens": 1500,
1041
+ "lookahead_tasks": 2
1042
+ }
1043
+ }
1044
+ ```
1045
+
1046
+ - **enabled** – When `true` (default) Swarm injects a compact plan cursor instead of the full `plan.md`.
1047
+ - **max_tokens** – Upper bound on the number of tokens emitted for the cursor (default 1500). The cursor contains the current phase summary, the full current task, and up to `lookahead_tasks` upcoming tasks. Earlier phases are reduced to one‑line summaries.
1048
+ - **lookahead_tasks** – Number of future tasks to include in full detail (default 2). Set to `0` to show only the current task.
1049
+
1050
+ Disabling (`"enabled": false`) falls back to the pre‑v6.13 behavior of injecting the entire plan text.
1051
+
1052
+ ## Tool Output Truncation (v6.13)
1053
+
1054
+ Control the size of tool outputs that are sent back to the LLM.
1055
+
1056
+ ```json
1057
+ {
1058
+ "tool_output": {
1059
+ "truncation_enabled": true,
1060
+ "max_lines": 150,
1061
+ "per_tool": {
1062
+ "diff": 200,
1063
+ "symbols": 100
1064
+ }
1065
+ }
1066
+ }
1067
+ ```
1068
+
1069
+ - **truncation_enabled** – Global switch (default true).
1070
+ - **max_lines** – Default line limit for any tool output.
1071
+ - **per_tool** – Overrides `max_lines` for specific tools. The `diff` and `symbols` tools are truncated by default because their outputs can be very large.
1072
+
1073
+ When truncation is active, a footer is appended:
1074
+
1075
+ ```
1076
+ ---
1077
+ [output truncated to {maxLines} lines – use `tool_output.per_tool.<tool>` to adjust]
1078
+ ```
1079
+
1080
+ ## Summarization Settings
1081
+
1082
+ Control how tool outputs are summarized for LLM context.
1083
+
1084
+ ```json
1085
+ {
1086
+ "summaries": {
1087
+ "threshold_bytes": 102400,
1088
+ "exempt_tools": ["retrieve_summary", "task", "read"]
1089
+ }
1090
+ }
1091
+ ```
1092
+
1093
+ - **threshold_bytes** – Output size threshold in bytes before summarization is triggered (default 102400 = 100KB).
1094
+ - **exempt_tools** – Tools whose outputs are never summarized. Defaults to `["retrieve_summary", "task", "read"]` to prevent re-summarization loops.
1095
+
1096
+ > **Note:** The `retrieve_summary` tool supports paginated retrieval via `offset` and `limit` parameters to fetch large summarized outputs in chunks.
1097
+
1098
+ ---
1099
+
1100
+ ### Disabling Agents
1101
+
1102
+ ```json
1103
+ {
1104
+ "sme": { "disabled": true },
1105
+ "designer": { "disabled": true },
1106
+ "test_engineer": { "disabled": true }
1107
+ }
1108
+ ```
1109
+
1110
+ </details>
1111
+
1112
+ <details>
1113
+ <summary><strong>All Slash Commands</strong></summary>
1114
+
1115
+ | Command | Description |
1116
+ |---------|-------------|
1117
+ | `/swarm status` | Current phase, task progress, agent count |
1118
+ | `/swarm plan [N]` | Full plan or filtered by phase |
1119
+ | `/swarm agents` | Registered agents with models and permissions |
1120
+ | `/swarm history` | Completed phases with status |
1121
+ | `/swarm config` | Current resolved configuration |
1122
+ | `/swarm diagnose` | Health check for `.swarm/` files and config |
1123
+ | `/swarm export` | Export plan and context as portable JSON |
1124
+ | `/swarm evidence [task]` | Evidence bundles for a task or all tasks |
1125
+ | `/swarm archive [--dry-run]` | Archive old evidence with retention policy |
1126
+ | `/swarm benchmark` | Performance benchmarks |
1127
+ | `/swarm retrieve [id]` | Retrieve auto-summarized tool outputs (supports offset/limit pagination) |
1128
+ | `/swarm reset --confirm` | Clear swarm state files |
1129
+ | `/swarm reset-session` | Clear session state files in `.swarm/session/` (preserves plan and context) |
1130
+ | `/swarm preflight` | Run phase preflight checks |
1131
+ | `/swarm config doctor [--fix]` | Config validation with optional auto-fix |
1132
+ | `/swarm doctor tools` | Tool registration coherence and binary readiness check |
1133
+ | `/swarm sync-plan` | Force plan.md regeneration from plan.json |
1134
+ | `/swarm specify [description]` | Generate or import a feature specification |
1135
+ | `/swarm clarify [topic]` | Clarify and refine an existing feature specification |
1136
+ | `/swarm analyze` | Analyze spec.md vs plan.md for requirement coverage gaps |
1137
+ | `/swarm close [--prune-branches]` | Idempotent session close-out: retrospectives, lesson curation, evidence archive, context.md reset, config-backup cleanup, optional branch pruning |
1138
+ | `/swarm write-retro` | Write a phase retrospective manually |
1139
+ | `/swarm handoff` | Generate a handoff summary for context-budget-critical sessions |
1140
+ | `/swarm simulate` | Simulate plan execution without writing code |
1141
+ | `/swarm promote` | Promote swarm-scoped knowledge to hive (global) knowledge |
1142
+ | `/swarm evidence summary` | Generate a summary across all evidence bundles with completion ratio and blockers |
1143
+ | `/swarm knowledge` | List knowledge entries |
1144
+ | `/swarm knowledge migrate` | Migrate knowledge entries to the current format |
1145
+ | `/swarm knowledge quarantine [id]` | Move a knowledge entry to quarantine |
1146
+ | `/swarm knowledge restore [id]` | Restore a quarantined knowledge entry |
1147
+ | `/swarm turbo` | Enable turbo mode for the current session (bypasses QA gates) |
1148
+ | `/swarm full-auto` | Toggle Full-Auto Mode for the current session [on|off] |
1149
+ | `/swarm checkpoint` | Save a git checkpoint for the current state |
1150
+
322
1151
  </details>
323
1152
 
324
1153
  ---