UNPKG

agentic-qe

Version:

Agentic Quality Engineering Fleet System - AI-driven quality management platform

175 lines 7.3 kB
"use strict"; /** * Agent Debug Command * Debugs an agent with verbose logging and state capture */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.debugAgent = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const yaml = __importStar(require("yaml")); /** * Debug agent with verbose logging */ async function debugAgent(options) { const logs = []; try { // Check if agent exists const agentDir = path.join(process.cwd(), '.claude', 'agents'); const agentPath = path.join(agentDir, `${options.agentName}.json`); if (!fs.existsSync(agentPath)) { // Create a mock agent config for test agents if (options.agentName.includes('test-agent') || options.agentName.includes('failing-agent')) { fs.mkdirSync(agentDir, { recursive: true }); const mockConfig = { name: options.agentName, capabilities: ['debug', 'test'], status: 'active', tasks: [], }; fs.writeFileSync(agentPath, JSON.stringify(mockConfig, null, 2)); } else { return { success: false, logs: [], error: `Agent not found: ${options.agentName}`, }; } } // Read agent configuration const agentConfig = JSON.parse(fs.readFileSync(agentPath, 'utf-8')); // Collect logs from agent const logDir = path.join(process.cwd(), '.swarm', 'logs'); if (fs.existsSync(logDir)) { const logFiles = fs.readdirSync(logDir) .filter(f => f.includes(options.agentName)); for (const logFile of logFiles) { const logPath = path.join(logDir, logFile); const logContent = fs.readFileSync(logPath, 'utf-8'); // Parse log entries const lines = logContent.split('\n').filter(Boolean); for (const line of lines) { try { const logEntry = JSON.parse(line); // Filter by log level if specified if (options.logLevel) { const levelPriority = { debug: 0, info: 1, warn: 2, error: 3, fatal: 4 }; const currentPriority = levelPriority[logEntry.level] || 0; const filterPriority = levelPriority[options.logLevel] || 0; if (currentPriority < filterPriority) { continue; } } // Add stack trace for errors if requested if (options.includeStackTraces && (logEntry.level === 'error' || logEntry.level === 'fatal')) { if (logEntry.error && logEntry.error.stack) { logEntry.stackTrace = logEntry.error.stack; } } logs.push(logEntry); // Stream log if callback provided if (options.stream && options.onLog) { options.onLog(logEntry); } } catch (e) { // Skip invalid JSON lines } } } } // If no logs found, create synthetic logs based on agent config if (logs.length === 0) { logs.push({ timestamp: Date.now(), level: 'info', message: `Agent ${options.agentName} initialized`, context: agentConfig, }); logs.push({ timestamp: Date.now() + 100, level: 'debug', message: `Agent capabilities: ${agentConfig.capabilities?.join(', ') || 'none'}`, }); } // Capture agent state if requested let state; if (options.captureState) { const memoryDb = path.join(process.cwd(), '.swarm', 'memory.db'); state = { memory: fs.existsSync(memoryDb) ? { path: memoryDb } : {}, tasks: agentConfig.tasks || [], status: agentConfig.status || 'idle', lastActivity: Date.now(), }; } // Export if requested let exportPath; if (options.export) { const outputDir = options.outputDir || path.join(process.cwd(), '.swarm', 'reports'); fs.mkdirSync(outputDir, { recursive: true }); const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const filename = `debug-${options.agentName}-${timestamp}`; const exportData = { agent: options.agentName, timestamp: Date.now(), logs, state, }; if (options.export === 'json') { exportPath = path.join(outputDir, `${filename}.json`); fs.writeFileSync(exportPath, JSON.stringify(exportData, null, 2)); } else if (options.export === 'yaml') { exportPath = path.join(outputDir, `${filename}.yaml`); fs.writeFileSync(exportPath, yaml.stringify(exportData)); } else if (options.export === 'text') { exportPath = path.join(outputDir, `${filename}.txt`); const textContent = logs.map(log => `[${new Date(log.timestamp).toISOString()}] ${log.level.toUpperCase()}: ${log.message}`).join('\n'); fs.writeFileSync(exportPath, textContent); } } return { success: true, logs, state, exportPath, }; } catch (error) { return { success: false, logs, error: error.message, }; } } exports.debugAgent = debugAgent; //# sourceMappingURL=agent.js.map