/** * Module health check command * * Usage: * celilo module health [module-id] [--json] [--debug] */ import { getDb } from '../../db/client'; import type { HealthCheckResult } from '../../services/health-runner'; import { runAllHealthChecks, runModuleHealthCheck } from '../../services/health-runner'; import { getArg, hasFlag } from '../parser'; import type { CommandResult } from '../types'; const STATUS_ICONS: Record = { healthy: '●', degraded: '⚠', unhealthy: '✗', 'no-checks': '—', error: '✗', }; const CHECK_ICONS: Record = { pass: '✓', warn: '⚠', fail: '✗', skip: '○', }; export function formatResult(result: HealthCheckResult): string { const icon = STATUS_ICONS[result.status] || '?'; const lines: string[] = []; if (result.status === 'no-checks') { lines.push(` ${result.moduleId} ${icon} no health check defined`); // Annotate the waiver beside the verdict, never instead of it: the // module is still unmeasured, a human just decided it needs no check // (openspec/changes/health-waiver-mechanism, D3). if (result.waiver) { lines.push( ` waived: ${result.waiver.reason} (by ${result.waiver.by} at ${result.waiver.at})`, ); } return lines.join('\n'); } if (result.status === 'error') { lines.push(` ${result.moduleId} ${icon} error: ${result.error}`); return lines.join('\n'); } const stateNote = result.status === 'healthy' || result.status === 'degraded' ? ' → VERIFIED' : ''; lines.push(` ${result.moduleId} ${icon} ${result.status}${stateNote}`); for (const check of result.checks) { const checkIcon = CHECK_ICONS[check.status] || '?'; lines.push(` ${checkIcon} ${check.name.padEnd(20)} ${check.message}`); if (check.details) { lines.push(` ${check.details}`); } } return lines.join('\n'); } /** * Handle module health command */ export async function handleModuleHealth( args: string[], flags: Record, ): Promise { const db = getDb(); const debug = hasFlag(flags, 'debug'); const jsonOutput = hasFlag(flags, 'json'); const moduleId = getArg(args, 0); let results: HealthCheckResult[]; if (moduleId) { const result = await runModuleHealthCheck(moduleId, db, { debug, quiet: jsonOutput }); results = [result]; } else { results = await runAllHealthChecks(db, { debug, quiet: jsonOutput }); } if (jsonOutput) { return { success: true, message: JSON.stringify({ modules: results }, null, 2), rawOutput: true, }; } if (results.length === 0) { return { success: true, message: 'No deployed modules to check', }; } const output = results.map(formatResult).join('\n\n'); const allHealthy = results.every( (r) => r.status === 'healthy' || r.status === 'degraded' || r.status === 'no-checks', ); if (!allHealthy) { return { success: false, error: output, }; } return { success: true, message: output, }; }