Enables parallel agents to communicate through a CLI-based conversation mechanism coordinated via tRPC. Agents can ask questions to peers and receive answers, with target resolution by agent ID, task ID, or phase ID.
7.5 KiB
Agent Module
src/agent/ — Agent lifecycle management, output parsing, multi-provider support, and account failover.
File Inventory
| File | Purpose |
|---|---|
types.ts |
Core types: AgentInfo, AgentManager interface, SpawnOptions, StreamEvent |
manager.ts |
MultiProviderAgentManager — main orchestrator class |
process-manager.ts |
AgentProcessManager — worktree creation, command building, detached spawn |
output-handler.ts |
OutputHandler — JSONL stream parsing, completion detection, proposal creation, task dedup |
file-tailer.ts |
FileTailer — watches output files, fires parser + raw content callbacks |
file-io.ts |
Input/output file I/O: frontmatter writing, signal.json reading, tiptap conversion |
markdown-to-tiptap.ts |
Markdown to Tiptap JSON conversion using MarkdownManager |
index.ts |
Public exports, ClaudeAgentManager deprecated alias |
Sub-modules
| Directory | Purpose |
|---|---|
providers/ |
Provider registry, presets (7 providers), config types |
providers/parsers/ |
Provider-specific output parsers (Claude JSONL, generic line) |
accounts/ |
Account discovery, config dir setup, credential management, usage API |
credentials/ |
AccountCredentialManager — credential injection per account |
lifecycle/ |
LifecycleController — retry policy, signal recovery, missing signal instructions |
prompts/ |
Mode-specific prompt builders (execute, discuss, plan, detail, refine) + shared inter-agent communication instructions |
Key Flows
Spawning an Agent
- tRPC procedure calls
agentManager.spawn(options) - Manager generates alias (adjective-animal), creates DB record
AgentProcessManager.createWorktree()— creates git worktree at.cw-worktrees/agent/<alias>/file-io.writeInputFiles()— writes.cw/input/with assignment files (initiative, pages, phase, task) and read-only context dirs (context/phases/,context/tasks/)- Provider config builds spawn command via
buildSpawnCommand() spawnDetached()— launches detached child process with file output redirectionFileTailerwatches output file, firesonEvent(parsed stream events) andonRawContent(raw JSONL chunks) callbacksonRawContent→ DB insert viacreateLogChunkCallback()→agent:outputevent emitted (single emission point)OutputHandler.handleStreamEvent()processes parsed events (session tracking, result capture — no event emission)- DB record updated with PID, output file path, session ID
agent:spawnedevent emitted
Completion Detection
- Polling detects process exit,
FileTailer.stop()flushes remaining output OutputHandler.handleCompletion()triggered- Primary path: Reads
.cw/output/signal.jsonfrom agent worktree - Signal contains
{ status: "done"|"questions"|"error", result?, questions?, error? } - Agent DB status updated accordingly (idle, waiting_for_input, crashed)
- For
done: proposals created from structured output;agent:stoppedemitted - For
questions: parsed and stored aspendingQuestions;agent:waitingemitted - Fallback: If signal.json missing, lifecycle controller retries with instruction injection
Account Failover
- On usage-limit error,
markAccountExhausted(id, until)called findNextAvailable(provider)returns least-recently-used non-exhausted account- Agent re-spawned with new account's credentials
agent:account_switchedevent emitted
Resume Flow
- tRPC
resumeAgentcalled withanswers: Record<string, string> - Manager looks up agent's session ID and provider config
buildResumeCommand()creates resume command with session flagformatAnswersAsPrompt(answers)converts answers to prompt text- New detached process spawned, same worktree, incremented session number
Provider Configuration
Providers defined in providers/presets.ts:
| Provider | Command | Resume | Prompt Mode |
|---|---|---|---|
| claude | claude |
--resume <id> |
native (-p) |
| claude-code | claude |
--resume <id> |
native |
| codex | codex |
none | flag (--prompt) |
| aider | aider |
none | flag (--message) |
| cline | cline |
none | flag |
| continue | continue |
none | flag |
| cursor-agent | cursor |
none | flag |
Each provider config specifies: command, args, resumeStyle, promptMode, structuredOutput, sessionId extraction, nonInteractive options.
Output Parsing
The OutputHandler processes JSONL streams from Claude CLI:
initevent → session ID extracted and persistedtext_deltaevents → no-op in handler (output streaming handled by DB log chunks)resultevent → final result with structured data captured onActiveAgent- Signal file (
signal.json) → authoritative completion status
Output event flow: FileTailer.onRawContent() → DB insertChunk() → EventBus.emit('agent:output'). This is the single emission point — no events from handleStreamEvent() or processLine().
For providers without structured output, the generic line parser accumulates raw text.
Credential Management
AccountCredentialManager in credentials/ handles OAuth token lifecycle:
read()— extractsclaudeAiOauthfrom.credentials.json. OnlyaccessTokenis required;refreshTokenandexpiresAtmay be null (setup tokens).isExpired()— returns false whenexpiresAtis null (setup tokens never "expire" from our perspective).ensureValid()— if expired andrefreshTokenexists, refreshes. If expired with norefreshToken, returns invalid with error.
Setup Tokens
Setup tokens (from claude setup-token) are long-lived OAuth access tokens with no refresh token or expiry. Register via:
cw account add --token <token> --email user@example.com
Stored as credentials: {"claudeAiOauth":{"accessToken":"<token>"}} and configJson: {"hasCompletedOnboarding":true}.
Log Chunks
Agent output is persisted to agent_log_chunks table and drives all live streaming:
onRawContentcallback fires for every raw JSONL chunk fromFileTailer- DB insert →
agent:outputevent emission (single source of truth for UI) - No FK to agents — survives agent deletion
- Session tracking: spawn=1, resume=previousMax+1
- Read path (
getAgentOutputtRPC): concatenates all DB chunks (no file fallback) - Live path (
onAgentOutputsubscription): listens foragent:outputevents - Frontend: initial query loads from DB, subscription accumulates raw JSONL, both parsed via
parseAgentOutput()
Inter-Agent Communication
Agents can communicate with each other via the conversations table, coordinated through CLI commands.
Prompt Integration
INTER_AGENT_COMMUNICATION constant in prompts/shared.ts is appended to all 5 agent mode prompts. It instructs agents to:
- Start
cw listen --agent-id <ID> &as a background process at session start - Handle incoming questions by answering via
cw answerand restarting the listener - Ask questions to peers via
cw ask --from <ID> --agent-id|--phase-id|--task-id - Kill the listener before writing
signal.json
Agent Identity
manifest.json now includes agentId and agentName fields. The manager passes these from the DB record after agent creation.
CLI Commands
cw listen: PollsgetPendingConversations, prints first pending as JSON, exitscw ask: Creates conversation, pollsgetConversationuntil answered, prints answercw answer: CallsanswerConversation, prints confirmation JSON