Discord bot that maps channels to OpenCode agents. Multi-server, one opencode serve per project path, always-on thread passthrough.
- Runtime: Node.js + TypeScript (strict mode, ES2022 target)
- Package manager: pnpm
- Discord: discord.js v14
- OpenCode: @opencode-ai/sdk/v2 (ALWAYS /v2 -- the root import is legacy v1)
- Config: YAML + Zod validation
- Test: vitest
This project follows strict TDD. No production code without a failing test first.
- RED — Write a failing test that describes the desired behavior
- Verify RED — Run the test, confirm it fails for the expected reason (missing implementation, not typo)
- GREEN — Write the minimal code to make the test pass
- Verify GREEN — Run the test, confirm it passes along with all other tests
- REFACTOR — Clean up while keeping tests green
- COMMIT — Frequent, small commits after each green cycle
Violations: If code was written before its test, delete it. Write the test first, then reimplement from scratch. No exceptions.
What to test per module:
config/— Schema validation (valid, invalid, edge cases), hot-reload events, default valuesstate/— Load/save cycle, atomic write guarantees, concurrent access, accessor correctnessqueue/— Enqueue/dequeue ordering, deduplication, clear, persistenceopencode/— Server start/stop/crash, cache invalidation, session lifecycle, SSE event handlingdiscord/commands/— Permission checks, argument validation, response format, error pathsutils/— Path security, formatter splitting, table detection, error construction
- Don't implement features not explicitly specified in PLAN.md
- Don't add "nice-to-have" parameters, options, or abstractions
- Extract shared logic only after the second occurrence (not preemptively)
- If the plan doesn't ask for it, don't build it
- Prefer simple, obvious solutions over clever ones
- Smaller files with one responsibility each
- Flat structures over deep nesting
- If a function is hard to test, the design needs simplification
- Named exports only, no default exports
typekeyword for type-only imports- Errors: always throw
BotErrorwith an error code (seeerror-handlingskill) - Async/await everywhere, no raw callbacks or .then() chains
constby default,letonly when reassignment is needed- File naming: camelCase for files, PascalCase for classes/types/interfaces
- One class or major function per file (exceptions: small related utilities)
- All public module functions must have JSDoc with @param and @returns
- All runtime state goes through
StateManager(never raw fs read/write) - Atomic writes: write to tmp file, then
fs.renameSync - All reads from in-memory object (never disk I/O on hot path)
- Every state mutation MUST call
save()immediately after
- MCP/cache fetch fails → degrade gracefully, never crash
- Session history replay fails → skip, post confirmation anyway
- Autocomplete cache miss → return empty results
- SSE disconnect → retry 3x, then notify user
- Never let a non-critical failure block core functionality
This project uses two-stage review gates after each implementation task:
A reviewer checks the implementation against the PLAN.md specification:
- Every requirement in the spec has corresponding implementation
- No extra functionality was added beyond the spec
- Types/interfaces match what the spec describes
- Error codes and messages match the spec
Must pass before Stage 2. If gaps found → fix → re-review.
A reviewer checks against project conventions:
- Named exports, type imports, BotError usage, async/await
- JSDoc on public functions
- Module boundary compliance (import direction DAG)
- State mutations call save() immediately
- Graceful degradation for non-critical paths
- Tests written first and cover the behavior
Must pass before marking complete. If issues found → fix → re-review.
- After each sub-agent completes a task (in
/implement) - After each round completes (integration review)
- Before merging any branch (final review via
/review) - Security audit before first deployment (via
/security-review)
- Create a branch per implementation round:
round-N/description - Commit after every successful TDD cycle (RED→GREEN→REFACTOR = one commit)
- Commit messages:
feat:,fix:,test:,refactor:prefixes - Never commit failing tests (except the deliberate RED step which gets amended in GREEN)
- Squash-merge rounds into main when review passes
This project uses subagent-driven-development for parallel implementation. See IMPLEMENTATION.md for the full plan with checkbox steps.
Execution: Use the /implement command which dispatches sub-agents with two-stage review per task.
Process per round:
- Extract all tasks for the round from IMPLEMENTATION.md
- For each task: dispatch implementer → spec review → code quality review → mark complete
- After all tasks: verify integration (
pnpm tsc --noEmit+pnpm test) - Commit the round
Sub-agent prompt requirements:
- Include the full task text (never reference the plan file)
- Include TypeScript interfaces from prior rounds (inline)
- Include exact file paths owned by the sub-agent
- Instruct TDD methodology (test first)
- Instruct to load relevant skills for domain context
After writing or modifying code, always run:
pnpm tsc --noEmit && pnpm test
Both must pass. Type errors are fixed immediately, not deferred.
sdk-reference— OpenCode SDK v2 calling conventions and typesdiscord-patterns— discord.js v14 API patternserror-handling— BotError class, error codes, correlation IDsmodule-boundaries— File ownership, import direction, interface contractsprocess-lifecycle— Spawning/monitoring/shutting down opencode serve processes
superpowers:test-driven-development— TDD methodology (MANDATORY for all implementation)superpowers:subagent-driven-development— Task dispatch with two-stage reviewsuperpowers:writing-plans— Plan creation with checkbox formatsuperpowers:brainstorming— Design refinement before implementationsuperpowers:systematic-debugging— 4-phase root cause analysissuperpowers:verification-before-completion— Ensure fixes are actually fixedsuperpowers:executing-plans— Batch execution with human checkpoints
code-review— Multi-agent PR review with confidence-filtered cross-checkssecurity-review— OWASP-bucketed audit with concrete PoC requirementcode-explorer— Deep codebase analysis, trace features end-to-endcode-architect— Architecture blueprint with file-level implementation mapcode-reviewer— Two-pass adversarial review with edge-case checklistagents-md-improver— Audit and update this AGENTS.md against current codebase
/implement <round>— Execute a round with subagent-driven-development + two-stage review/verify— Type-check and fix errors/review [target]— Multi-agent code review on changes or PR/security-review [target]— Security audit with project-specific attack surface focus/plan [description]— Create or update implementation plan
context7— Look up library documentation (discord.js, zod, chokidar, satori, etc.)