Files
Codewalkers/src/cli/index.ts
Lukas May cbf0ed28cb fix: wire database and repositories into server startup for tRPC context
The server never created a database or instantiated repositories, so all
tRPC procedures requiring initiativeRepository (and other repos) threw
"Initiative repository not available" on every request.

- Create ensureSchema() for shared database table initialization
- Extend TrpcAdapterOptions to accept all repository/manager dependencies
- Add ServerContextDeps to CoordinationServer for dependency injection
- Wire database, schema, and repositories in CLI startServer()
- Refactor test-helpers to use shared ensureSchema()
2026-02-04 21:18:30 +01:00

1095 lines
37 KiB
TypeScript

/**
* Codewalk District CLI
*
* Commander-based CLI with help system and version display.
* Supports server mode via --server flag.
*/
import { Command } from 'commander';
import { VERSION } from '../index.js';
import { CoordinationServer } from '../server/index.js';
import { GracefulShutdown } from '../server/shutdown.js';
import { ProcessManager, ProcessRegistry } from '../process/index.js';
import { LogManager } from '../logging/index.js';
import { createEventBus } from '../events/index.js';
import { createDefaultTrpcClient } from './trpc-client.js';
import {
createDatabase,
ensureSchema,
DrizzleInitiativeRepository,
DrizzlePhaseRepository,
DrizzlePlanRepository,
DrizzleTaskRepository,
DrizzleMessageRepository,
} from '../db/index.js';
/** Environment variable for custom port */
const CW_PORT_ENV = 'CW_PORT';
/**
* Starts the coordination server in foreground mode.
* Server runs until terminated via SIGTERM/SIGINT.
*/
async function startServer(port?: number): Promise<void> {
// Get port from option, env var, or default
const serverPort = port ??
(process.env[CW_PORT_ENV] ? parseInt(process.env[CW_PORT_ENV], 10) : undefined);
// Create dependencies
const registry = new ProcessRegistry();
const eventBus = createEventBus();
const processManager = new ProcessManager(registry, eventBus);
const logManager = new LogManager();
// Create database and ensure schema
const db = createDatabase();
ensureSchema(db);
// Create repositories
const initiativeRepository = new DrizzleInitiativeRepository(db);
const phaseRepository = new DrizzlePhaseRepository(db);
const planRepository = new DrizzlePlanRepository(db);
const taskRepository = new DrizzleTaskRepository(db);
const messageRepository = new DrizzleMessageRepository(db);
// Create and start server
const server = new CoordinationServer(
{ port: serverPort },
processManager,
logManager,
eventBus,
{
initiativeRepository,
phaseRepository,
planRepository,
taskRepository,
messageRepository,
}
);
try {
await server.start();
} catch (error) {
console.error('Failed to start server:', (error as Error).message);
process.exit(1);
}
// Install graceful shutdown handlers
const shutdown = new GracefulShutdown(server, processManager, logManager);
shutdown.install();
}
/**
* Creates and configures the CLI program.
*
* @param serverHandler - Optional handler to be called for server mode
* @returns Configured Commander program ready for parsing
*/
export function createCli(serverHandler?: (port?: number) => Promise<void>): Command {
const program = new Command();
program
.name('cw')
.description('Multi-agent workspace for orchestrating multiple Claude Code agents')
.version(VERSION, '-v, --version', 'Display version number');
// Server mode option (global flag)
program
.option('-s, --server', 'Start the coordination server')
.option('-p, --port <number>', 'Port for the server (default: 3847, env: CW_PORT)', parseInt);
// Handle the case where --server is provided without a command
// This makes --server work as a standalone action
program.hook('preAction', async (_thisCommand, _actionCommand) => {
const opts = program.opts();
if (opts.server && serverHandler) {
await serverHandler(opts.port);
process.exit(0);
}
});
// Status command - shows workspace status via tRPC
program
.command('status')
.description('Show workspace status')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const health = await client.health.query();
console.log('Coordination Server Status');
console.log('==========================');
console.log(`Status: ${health.status}`);
console.log(`Uptime: ${health.uptime}s`);
console.log(`Processes: ${health.processCount}`);
} catch {
console.log('Server not running or unreachable. Start with: cw --server');
}
});
// Agent command group
const agentCommand = program
.command('agent')
.description('Manage agents');
// cw agent spawn --name <name> --task <taskId> <prompt>
agentCommand
.command('spawn <prompt>')
.description('Spawn a new agent to work on a task')
.requiredOption('--name <name>', 'Human-readable name for the agent (e.g., gastown)')
.requiredOption('--task <taskId>', 'Task ID to assign to agent')
.option('--cwd <path>', 'Working directory for agent')
.action(async (prompt: string, options: { name: string; task: string; cwd?: string }) => {
try {
const client = createDefaultTrpcClient();
const agent = await client.spawnAgent.mutate({
name: options.name,
taskId: options.task,
prompt,
cwd: options.cwd,
});
console.log(`Agent '${agent.name}' spawned`);
console.log(` ID: ${agent.id}`);
console.log(` Task: ${agent.taskId}`);
console.log(` Status: ${agent.status}`);
console.log(` Worktree: ${agent.worktreeId}`);
} catch (error) {
console.error('Failed to spawn agent:', (error as Error).message);
process.exit(1);
}
});
// cw agent stop <name>
agentCommand
.command('stop <name>')
.description('Stop a running agent by name')
.action(async (name: string) => {
try {
const client = createDefaultTrpcClient();
const result = await client.stopAgent.mutate({ name });
console.log(`Agent '${result.name}' stopped`);
} catch (error) {
console.error('Failed to stop agent:', (error as Error).message);
process.exit(1);
}
});
// cw agent list
agentCommand
.command('list')
.description('List all agents')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const agents = await client.listAgents.query();
if (agents.length === 0) {
console.log('No agents found');
return;
}
console.log('Agents:');
for (const agent of agents) {
const status = agent.status === 'waiting_for_input' ? 'WAITING' : agent.status.toUpperCase();
console.log(` ${agent.name} [${status}] - ${agent.taskId}`);
}
} catch (error) {
console.error('Failed to list agents:', (error as Error).message);
process.exit(1);
}
});
// cw agent get <name>
agentCommand
.command('get <name>')
.description('Get agent details by name')
.action(async (name: string) => {
try {
const client = createDefaultTrpcClient();
const agent = await client.getAgent.query({ name });
if (!agent) {
console.log(`Agent '${name}' not found`);
return;
}
console.log(`Agent: ${agent.name}`);
console.log(` ID: ${agent.id}`);
console.log(` Task: ${agent.taskId}`);
console.log(` Session: ${agent.sessionId ?? '(none)'}`);
console.log(` Worktree: ${agent.worktreeId}`);
console.log(` Status: ${agent.status}`);
console.log(` Created: ${agent.createdAt}`);
console.log(` Updated: ${agent.updatedAt}`);
} catch (error) {
console.error('Failed to get agent:', (error as Error).message);
process.exit(1);
}
});
// cw agent resume <name> <answers>
// Accepts either JSON object {"q1": "answer1", "q2": "answer2"} or single answer
agentCommand
.command('resume <name> <answers>')
.description('Resume an agent with answers (JSON object or single answer string)')
.action(async (name: string, answersInput: string) => {
try {
const client = createDefaultTrpcClient();
// Try parsing as JSON first, fallback to single answer format
let answers: Record<string, string>;
try {
const parsed = JSON.parse(answersInput);
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
answers = parsed;
} else {
// Not a valid object, treat as single answer
answers = { q1: answersInput };
}
} catch {
// Not valid JSON, treat as single answer with default question ID
answers = { q1: answersInput };
}
const result = await client.resumeAgent.mutate({ name, answers });
console.log(`Agent '${result.name}' resumed`);
} catch (error) {
console.error('Failed to resume agent:', (error as Error).message);
process.exit(1);
}
});
// cw agent result <name>
agentCommand
.command('result <name>')
.description('Get agent execution result')
.action(async (name: string) => {
try {
const client = createDefaultTrpcClient();
const result = await client.getAgentResult.query({ name });
if (!result) {
console.log('No result available (agent may still be running)');
return;
}
console.log(`Result: ${result.success ? 'SUCCESS' : 'FAILED'}`);
console.log(` Message: ${result.message}`);
if (result.filesModified?.length) {
console.log(` Files modified: ${result.filesModified.join(', ')}`);
}
} catch (error) {
console.error('Failed to get result:', (error as Error).message);
process.exit(1);
}
});
// Task command group
const taskCommand = program
.command('task')
.description('Manage tasks');
// cw task list --plan <planId>
taskCommand
.command('list')
.description('List tasks for a plan')
.requiredOption('--plan <planId>', 'Plan ID to list tasks for')
.action(async (options: { plan: string }) => {
try {
const client = createDefaultTrpcClient();
const tasks = await client.listTasks.query({ planId: options.plan });
if (tasks.length === 0) {
console.log('No tasks found for this plan');
return;
}
// Count by status
const pending = tasks.filter(t => t.status === 'pending' || t.status === 'pending_approval').length;
const inProgress = tasks.filter(t => t.status === 'in_progress').length;
const completed = tasks.filter(t => t.status === 'completed').length;
const blocked = tasks.filter(t => t.status === 'blocked').length;
console.log('Tasks:');
console.log('');
for (const task of tasks) {
const statusLabel = task.status === 'in_progress' ? 'IN_PROGRESS' : task.status.toUpperCase();
const priorityLabel = task.priority === 'high' ? '[HIGH]' : task.priority === 'low' ? '[low]' : '';
console.log(` ${task.order}. ${task.name} [${statusLabel}] ${task.type} ${priorityLabel}`);
}
console.log('');
console.log(`Summary: ${pending} pending, ${inProgress} in progress, ${completed} completed, ${blocked} blocked`);
} catch (error) {
console.error('Failed to list tasks:', (error as Error).message);
process.exit(1);
}
});
// cw task get <taskId>
taskCommand
.command('get <taskId>')
.description('Get task details by ID')
.action(async (taskId: string) => {
try {
const client = createDefaultTrpcClient();
const task = await client.getTask.query({ id: taskId });
console.log(`Task: ${task.name}`);
console.log(` ID: ${task.id}`);
console.log(` Plan: ${task.planId}`);
console.log(` Description: ${task.description ?? '(none)'}`);
console.log(` Type: ${task.type}`);
console.log(` Priority: ${task.priority}`);
console.log(` Status: ${task.status}`);
console.log(` Order: ${task.order}`);
console.log(` Created: ${task.createdAt}`);
console.log(` Updated: ${task.updatedAt}`);
} catch (error) {
console.error('Failed to get task:', (error as Error).message);
process.exit(1);
}
});
// cw task status <taskId> <status>
taskCommand
.command('status <taskId> <status>')
.description('Update task status (pending, in_progress, completed, blocked)')
.action(async (taskId: string, status: string) => {
const validStatuses = ['pending', 'in_progress', 'completed', 'blocked'];
if (!validStatuses.includes(status)) {
console.error(`Invalid status: ${status}`);
console.error(`Valid statuses: ${validStatuses.join(', ')}`);
process.exit(1);
}
try {
const client = createDefaultTrpcClient();
const task = await client.updateTaskStatus.mutate({
id: taskId,
status: status as 'pending' | 'in_progress' | 'completed' | 'blocked',
});
console.log(`Task '${task.name}' status updated to: ${task.status}`);
} catch (error) {
console.error('Failed to update task status:', (error as Error).message);
process.exit(1);
}
});
// Message command group
const messageCommand = program
.command('message')
.description('View agent messages and questions');
// cw message list [--agent <agentId>] [--status <status>]
messageCommand
.command('list')
.description('List messages from agents')
.option('--agent <agentId>', 'Filter by agent ID')
.option('--status <status>', 'Filter by status (pending, read, responded)')
.action(async (options: { agent?: string; status?: string }) => {
// Validate status if provided
if (options.status && !['pending', 'read', 'responded'].includes(options.status)) {
console.error(`Invalid status: ${options.status}`);
console.error('Valid statuses: pending, read, responded');
process.exit(1);
}
try {
const client = createDefaultTrpcClient();
const messages = await client.listMessages.query({
agentId: options.agent,
status: options.status as 'pending' | 'read' | 'responded' | undefined,
});
if (messages.length === 0) {
console.log('No messages found');
return;
}
// Count pending
const pendingCount = messages.filter(m => m.status === 'pending').length;
if (pendingCount > 0) {
console.log(`(${pendingCount} pending)\n`);
}
console.log('Messages:');
console.log('');
for (const msg of messages) {
const idShort = msg.id.slice(0, 8);
const agentId = msg.senderId?.slice(0, 8) ?? 'user';
const content = msg.content.length > 50 ? msg.content.slice(0, 47) + '...' : msg.content;
const statusLabel = msg.status.toUpperCase();
const createdAt = new Date(msg.createdAt).toLocaleString();
console.log(` ${idShort} ${agentId} ${msg.type} [${statusLabel}] ${createdAt}`);
console.log(` ${content}`);
}
} catch (error) {
console.error('Failed to list messages:', (error as Error).message);
process.exit(1);
}
});
// cw message read <messageId>
messageCommand
.command('read <messageId>')
.description('Read full message content')
.action(async (messageId: string) => {
try {
const client = createDefaultTrpcClient();
const message = await client.getMessage.query({ id: messageId });
console.log(`Message: ${message.id}`);
console.log(` From: ${message.senderType} (${message.senderId ?? 'user'})`);
console.log(` To: ${message.recipientType} (${message.recipientId ?? 'user'})`);
console.log(` Type: ${message.type}`);
console.log(` Status: ${message.status}`);
console.log(` Created: ${new Date(message.createdAt).toLocaleString()}`);
console.log('');
console.log('Content:');
console.log(message.content);
if (message.status === 'pending' && message.requiresResponse) {
console.log('');
console.log('---');
console.log('This message requires a response.');
console.log(`Use: cw message respond ${message.id} "<your response>"`);
}
} catch (error) {
console.error('Failed to read message:', (error as Error).message);
process.exit(1);
}
});
// cw message respond <messageId> <response>
messageCommand
.command('respond <messageId> <response>')
.description('Respond to a message')
.action(async (messageId: string, response: string) => {
try {
const client = createDefaultTrpcClient();
const responseMessage = await client.respondToMessage.mutate({
id: messageId,
response,
});
console.log(`Response recorded (${responseMessage.id.slice(0, 8)})`);
console.log('');
console.log('If the agent is waiting, you may want to resume it:');
console.log(' cw agent list (to see waiting agents)');
console.log(' cw agent resume <name> "<response>"');
} catch (error) {
console.error('Failed to respond to message:', (error as Error).message);
process.exit(1);
}
});
// Dispatch command group
const dispatchCommand = program
.command('dispatch')
.description('Control task dispatch queue');
// cw dispatch queue <taskId>
dispatchCommand
.command('queue <taskId>')
.description('Queue a task for dispatch')
.action(async (taskId: string) => {
try {
const client = createDefaultTrpcClient();
await client.queueTask.mutate({ taskId });
console.log(`Task '${taskId}' queued for dispatch`);
} catch (error) {
console.error('Failed to queue task:', (error as Error).message);
process.exit(1);
}
});
// cw dispatch next
dispatchCommand
.command('next')
.description('Dispatch next available task to an agent')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const result = await client.dispatchNext.mutate();
if (result.success) {
console.log('Task dispatched successfully');
console.log(` Task: ${result.taskId}`);
console.log(` Agent: ${result.agentId}`);
} else {
console.log('Dispatch failed');
console.log(` Task: ${result.taskId || '(none)'}`);
console.log(` Reason: ${result.reason}`);
}
} catch (error) {
console.error('Failed to dispatch:', (error as Error).message);
process.exit(1);
}
});
// cw dispatch status
dispatchCommand
.command('status')
.description('Show dispatch queue status')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const state = await client.getQueueState.query();
console.log('Dispatch Queue Status');
console.log('=====================');
console.log(`Queued: ${state.queued.length}`);
console.log(`Ready: ${state.ready.length}`);
console.log(`Blocked: ${state.blocked.length}`);
if (state.ready.length > 0) {
console.log('');
console.log('Ready tasks:');
for (const task of state.ready) {
const priority = task.priority === 'high' ? '[HIGH]' : task.priority === 'low' ? '[low]' : '';
console.log(` ${task.taskId} ${priority}`);
}
}
if (state.blocked.length > 0) {
console.log('');
console.log('Blocked tasks:');
for (const bt of state.blocked) {
console.log(` ${bt.taskId}: ${bt.reason}`);
}
}
} catch (error) {
console.error('Failed to get queue status:', (error as Error).message);
process.exit(1);
}
});
// cw dispatch complete <taskId>
dispatchCommand
.command('complete <taskId>')
.description('Mark a task as complete')
.action(async (taskId: string) => {
try {
const client = createDefaultTrpcClient();
await client.completeTask.mutate({ taskId });
console.log(`Task '${taskId}' marked as complete`);
} catch (error) {
console.error('Failed to complete task:', (error as Error).message);
process.exit(1);
}
});
// Merge command group
const mergeCommand = program
.command('merge')
.description('Manage merge queue');
// cw merge queue <taskId>
mergeCommand
.command('queue <taskId>')
.description('Queue a completed task for merge')
.action(async (taskId: string) => {
try {
const client = createDefaultTrpcClient();
await client.queueMerge.mutate({ taskId });
console.log(`Task '${taskId}' queued for merge`);
} catch (error) {
console.error('Failed to queue for merge:', (error as Error).message);
process.exit(1);
}
});
// cw merge status
mergeCommand
.command('status')
.description('Show merge queue status')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const state = await client.getMergeQueueStatus.query();
console.log('Merge Queue Status');
console.log('==================');
console.log(`Queued: ${state.queued.length}`);
console.log(`In Progress: ${state.inProgress.length}`);
console.log(`Merged: ${state.merged.length}`);
console.log(`Conflicted: ${state.conflicted.length}`);
if (state.conflicted.length > 0) {
console.log('');
console.log('Conflicts:');
for (const c of state.conflicted) {
console.log(` ${c.taskId}: ${c.conflicts.join(', ')}`);
}
}
} catch (error) {
console.error('Failed to get merge queue status:', (error as Error).message);
process.exit(1);
}
});
// cw merge next
mergeCommand
.command('next')
.description('Show next task ready to merge')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const next = await client.getNextMergeable.query();
if (next) {
console.log(`Next mergeable: ${next.taskId} (priority: ${next.priority})`);
} else {
console.log('No tasks ready to merge');
}
} catch (error) {
console.error('Failed to get next mergeable:', (error as Error).message);
process.exit(1);
}
});
// Coordinate command - process all ready merges
program
.command('coordinate')
.description('Process all ready merges in dependency order')
.option('-t, --target <branch>', 'Target branch for merges', 'main')
.action(async (options: { target: string }) => {
try {
const client = createDefaultTrpcClient();
console.log(`Processing merges to ${options.target}...`);
const { results } = await client.processMerges.mutate({
targetBranch: options.target,
});
const succeeded = results.filter(r => r.success).length;
const conflicted = results.filter(r => !r.success).length;
console.log('');
console.log('Results:');
console.log(` Merged: ${succeeded}`);
console.log(` Conflicts: ${conflicted}`);
if (conflicted > 0) {
console.log('');
console.log('Conflicted tasks (bounce-back tasks created):');
for (const r of results.filter(r => !r.success)) {
console.log(` ${r.taskId}: ${r.conflicts?.join(', ')}`);
}
}
} catch (error) {
console.error('Failed to process merges:', (error as Error).message);
process.exit(1);
}
});
// Initiative command group
const initiativeCommand = program
.command('initiative')
.description('Manage initiatives');
// cw initiative create <name>
initiativeCommand
.command('create <name>')
.description('Create a new initiative')
.option('-d, --description <description>', 'Initiative description')
.action(async (name: string, options: { description?: string }) => {
try {
const client = createDefaultTrpcClient();
const initiative = await client.createInitiative.mutate({
name,
description: options.description,
});
console.log(`Created initiative: ${initiative.id}`);
console.log(` Name: ${initiative.name}`);
if (initiative.description) {
console.log(` Description: ${initiative.description}`);
}
} catch (error) {
console.error('Failed to create initiative:', (error as Error).message);
process.exit(1);
}
});
// cw initiative list
initiativeCommand
.command('list')
.description('List all initiatives')
.option('-s, --status <status>', 'Filter by status (active, completed, archived)')
.action(async (options: { status?: string }) => {
// Validate status if provided
const validStatuses = ['active', 'completed', 'archived'];
if (options.status && !validStatuses.includes(options.status)) {
console.error(`Invalid status: ${options.status}`);
console.error(`Valid statuses: ${validStatuses.join(', ')}`);
process.exit(1);
}
try {
const client = createDefaultTrpcClient();
const initiatives = await client.listInitiatives.query(
options.status ? { status: options.status as 'active' | 'completed' | 'archived' } : undefined
);
if (initiatives.length === 0) {
console.log('No initiatives found');
return;
}
for (const init of initiatives) {
console.log(`${init.id} ${init.status.padEnd(10)} ${init.name}`);
}
} catch (error) {
console.error('Failed to list initiatives:', (error as Error).message);
process.exit(1);
}
});
// cw initiative get <id>
initiativeCommand
.command('get <id>')
.description('Get initiative details')
.action(async (id: string) => {
try {
const client = createDefaultTrpcClient();
const initiative = await client.getInitiative.query({ id });
console.log(`ID: ${initiative.id}`);
console.log(`Name: ${initiative.name}`);
console.log(`Status: ${initiative.status}`);
if (initiative.description) {
console.log(`Description: ${initiative.description}`);
}
console.log(`Created: ${new Date(initiative.createdAt).toISOString()}`);
} catch (error) {
console.error('Failed to get initiative:', (error as Error).message);
process.exit(1);
}
});
// cw initiative phases <initiative-id>
initiativeCommand
.command('phases <initiativeId>')
.description('List phases for an initiative')
.action(async (initiativeId: string) => {
try {
const client = createDefaultTrpcClient();
const phases = await client.listPhases.query({ initiativeId });
if (phases.length === 0) {
console.log('No phases found');
return;
}
for (const phase of phases) {
console.log(`${phase.number.toString().padStart(2)}. ${phase.name} [${phase.status}]`);
if (phase.description) {
console.log(` ${phase.description}`);
}
}
} catch (error) {
console.error('Failed to list phases:', (error as Error).message);
process.exit(1);
}
});
// Architect command group
const architectCommand = program
.command('architect')
.description('Architect agent workflow');
// cw architect discuss <initiative-id>
architectCommand
.command('discuss <initiativeId>')
.description('Start discussion phase for an initiative')
.requiredOption('--name <name>', 'Agent name')
.option('-c, --context <context>', 'Initial context')
.action(async (initiativeId: string, options: { name: string; context?: string }) => {
try {
const client = createDefaultTrpcClient();
const agent = await client.spawnArchitectDiscuss.mutate({
name: options.name,
initiativeId,
context: options.context,
});
console.log(`Started architect agent in discuss mode`);
console.log(` Agent: ${agent.name} (${agent.id})`);
console.log(` Mode: ${agent.mode}`);
console.log(` Initiative: ${initiativeId}`);
} catch (error) {
console.error('Failed to start discuss:', (error as Error).message);
process.exit(1);
}
});
// cw architect breakdown <initiative-id>
architectCommand
.command('breakdown <initiativeId>')
.description('Start breakdown phase for an initiative')
.requiredOption('--name <name>', 'Agent name')
.option('-s, --summary <summary>', 'Context summary from discuss phase')
.action(async (initiativeId: string, options: { name: string; summary?: string }) => {
try {
const client = createDefaultTrpcClient();
const agent = await client.spawnArchitectBreakdown.mutate({
name: options.name,
initiativeId,
contextSummary: options.summary,
});
console.log(`Started architect agent in breakdown mode`);
console.log(` Agent: ${agent.name} (${agent.id})`);
console.log(` Mode: ${agent.mode}`);
console.log(` Initiative: ${initiativeId}`);
} catch (error) {
console.error('Failed to start breakdown:', (error as Error).message);
process.exit(1);
}
});
// cw architect decompose <plan-id>
architectCommand
.command('decompose <planId>')
.description('Decompose a plan into tasks')
.requiredOption('--name <name>', 'Agent name')
.option('-c, --context <context>', 'Additional context')
.action(async (planId: string, options: { name: string; context?: string }) => {
try {
const client = createDefaultTrpcClient();
const agent = await client.spawnArchitectDecompose.mutate({
name: options.name,
planId,
context: options.context,
});
console.log(`Started architect agent in decompose mode`);
console.log(` Agent: ${agent.name} (${agent.id})`);
console.log(` Mode: ${agent.mode}`);
console.log(` Plan: ${planId}`);
} catch (error) {
console.error('Failed to start decompose:', (error as Error).message);
process.exit(1);
}
});
// Phase command group
const phaseCommand = program
.command('phase')
.description('Phase dependency and dispatch management');
// cw phase add-dependency --phase <id> --depends-on <id>
phaseCommand
.command('add-dependency')
.description('Add a dependency between phases')
.requiredOption('--phase <id>', 'Phase that depends on another')
.requiredOption('--depends-on <id>', 'Phase that must complete first')
.action(async (options: { phase: string; dependsOn: string }) => {
try {
const client = createDefaultTrpcClient();
await client.createPhaseDependency.mutate({
phaseId: options.phase,
dependsOnPhaseId: options.dependsOn,
});
console.log(`Added dependency: phase ${options.phase} depends on ${options.dependsOn}`);
} catch (error) {
console.error('Failed to add dependency:', (error as Error).message);
process.exit(1);
}
});
// cw phase dependencies <phaseId>
phaseCommand
.command('dependencies <phaseId>')
.description('List dependencies for a phase')
.action(async (phaseId: string) => {
try {
const client = createDefaultTrpcClient();
const result = await client.getPhaseDependencies.query({ phaseId });
if (result.dependencies.length === 0) {
console.log('No dependencies');
return;
}
console.log('Dependencies:');
for (const dep of result.dependencies) {
console.log(` ${dep}`);
}
} catch (error) {
console.error('Failed to get dependencies:', (error as Error).message);
process.exit(1);
}
});
// cw phase queue <phaseId>
phaseCommand
.command('queue <phaseId>')
.description('Queue a phase for execution')
.action(async (phaseId: string) => {
try {
const client = createDefaultTrpcClient();
await client.queuePhase.mutate({ phaseId });
console.log(`Phase ${phaseId} queued for execution`);
} catch (error) {
console.error('Failed to queue phase:', (error as Error).message);
process.exit(1);
}
});
// cw phase dispatch
phaseCommand
.command('dispatch')
.description('Dispatch next available phase')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const result = await client.dispatchNextPhase.mutate();
if (result.success) {
console.log('Phase dispatched successfully');
console.log(` Phase: ${result.phaseId}`);
} else {
console.log('Dispatch failed');
console.log(` Phase: ${result.phaseId || '(none)'}`);
console.log(` Reason: ${result.reason}`);
}
} catch (error) {
console.error('Failed to dispatch phase:', (error as Error).message);
process.exit(1);
}
});
// cw phase queue-status
phaseCommand
.command('queue-status')
.description('Show phase queue status')
.action(async () => {
try {
const client = createDefaultTrpcClient();
const state = await client.getPhaseQueueState.query();
console.log('Phase Queue Status');
console.log('==================');
console.log(`Queued: ${state.queued.length}`);
console.log(`Ready: ${state.ready.length}`);
console.log(`Blocked: ${state.blocked.length}`);
if (state.ready.length > 0) {
console.log('');
console.log('Ready phases:');
for (const phase of state.ready) {
console.log(` ${phase.phaseId}`);
}
}
if (state.blocked.length > 0) {
console.log('');
console.log('Blocked phases:');
for (const bp of state.blocked) {
console.log(` ${bp.phaseId}: ${bp.reason}`);
}
}
} catch (error) {
console.error('Failed to get queue status:', (error as Error).message);
process.exit(1);
}
});
// Plan command group
const planCommand = program
.command('plan')
.description('Plan management');
// cw plan list --phase <phaseId>
planCommand
.command('list')
.description('List plans in a phase')
.requiredOption('--phase <id>', 'Phase ID')
.action(async (options: { phase: string }) => {
try {
const client = createDefaultTrpcClient();
const plans = await client.listPlans.query({ phaseId: options.phase });
if (plans.length === 0) {
console.log('No plans found');
return;
}
console.table(plans.map(p => ({
id: p.id,
number: p.number,
name: p.name,
status: p.status,
})));
} catch (error) {
console.error('Failed to list plans:', (error as Error).message);
process.exit(1);
}
});
// cw plan create --phase <phaseId> --name <name>
planCommand
.command('create')
.description('Create a plan in a phase')
.requiredOption('--phase <id>', 'Phase ID')
.requiredOption('--name <name>', 'Plan name')
.option('--description <desc>', 'Plan description')
.action(async (options: { phase: string; name: string; description?: string }) => {
try {
const client = createDefaultTrpcClient();
const plan = await client.createPlan.mutate({
phaseId: options.phase,
name: options.name,
description: options.description,
});
console.log(`Created plan: ${plan.id} (${plan.name})`);
} catch (error) {
console.error('Failed to create plan:', (error as Error).message);
process.exit(1);
}
});
// cw plan get <id>
planCommand
.command('get <id>')
.description('Get plan details')
.action(async (id: string) => {
try {
const client = createDefaultTrpcClient();
const plan = await client.getPlan.query({ id });
console.log(JSON.stringify(plan, null, 2));
} catch (error) {
console.error('Failed to get plan:', (error as Error).message);
process.exit(1);
}
});
// cw plan tasks <id>
planCommand
.command('tasks <id>')
.description('List tasks in a plan')
.action(async (id: string) => {
try {
const client = createDefaultTrpcClient();
const tasks = await client.listTasks.query({ planId: id });
if (tasks.length === 0) {
console.log('No tasks found');
return;
}
console.table(tasks.map(t => ({
id: t.id,
order: t.order,
name: t.name,
type: t.type,
status: t.status,
})));
} catch (error) {
console.error('Failed to list tasks:', (error as Error).message);
process.exit(1);
}
});
return program;
}
/**
* Runs the CLI, handling server mode and commands.
*/
export async function runCli(): Promise<void> {
// Check for server flag early, before Commander processes
const hasServerFlag = process.argv.includes('--server') || process.argv.includes('-s');
if (hasServerFlag) {
// Get port from args if present
const portIndex = process.argv.findIndex(arg => arg === '-p' || arg === '--port');
const port = portIndex !== -1 && process.argv[portIndex + 1]
? parseInt(process.argv[portIndex + 1], 10)
: undefined;
await startServer(port);
// Server runs indefinitely until signal
return;
}
// Normal CLI processing
const program = createCli();
program.parse(process.argv);
}