Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

checkpoint: File edit:

rUv5e5039c1c803
diff --git a/v3/@claude-flow/cli/src/commands/hooks.ts b/v3/@claude-flow/cli/src/commands/hooks.ts
index ac5579376f..34aadc5d8e 100644
--- a/v3/@claude-flow/cli/src/commands/hooks.ts
+++ b/v3/@claude-flow/cli/src/commands/hooks.ts
@@ -1304,6 +1304,401 @@ const listCommand: Command = {
}
};
+// Pre-task subcommand
+const preTaskCommand: Command = {
+ name: 'pre-task',
+ description: 'Record task start and get agent suggestions',
+ options: [
+ {
+ name: 'task-id',
+ short: 'i',
+ description: 'Unique task identifier',
+ type: 'string',
+ required: true
+ },
+ {
+ name: 'description',
+ short: 'd',
+ description: 'Task description',
+ type: 'string',
+ required: true
+ },
+ {
+ name: 'auto-spawn',
+ short: 'a',
+ description: 'Auto-spawn suggested agents',
+ type: 'boolean',
+ default: false
+ }
+ ],
+ examples: [
+ { command: 'claude-flow hooks pre-task -i task-123 -d "Fix auth bug"', description: 'Record task start' },
+ { command: 'claude-flow hooks pre-task -i task-456 -d "Implement feature" --auto-spawn', description: 'With auto-spawn' }
+ ],
+ action: async (ctx: CommandContext): Promise<CommandResult> => {
+ const taskId = ctx.flags.taskId as string;
+ const description = ctx.args[0] || ctx.flags.description as string;
+
+ if (!taskId || !description) {
+ output.printError('Task ID and description are required.');
+ return { success: false, exitCode: 1 };
+ }
+
+ output.printInfo(`Starting task: ${output.highlight(taskId)}`);
+
+ try {
+ const result = await callMCPTool<{
+ taskId: string;
+ description: string;
+ suggestedAgents: Array<{
+ type: string;
+ confidence: number;
+ reason: string;
+ }>;
+ complexity: 'low' | 'medium' | 'high';
+ estimatedDuration: string;
+ risks: string[];
+ recommendations: string[];
+ }>('hooks/pre-task', {
+ taskId,
+ description,
+ autoSpawn: ctx.flags.autoSpawn || false,
+ timestamp: Date.now(),
+ });
+
+ if (ctx.flags.format === 'json') {
+ output.printJson(result);
+ return { success: true, data: result };
+ }
+
+ output.writeln();
+ output.printBox(
+ [
+ `Task ID: ${result.taskId}`,
+ `Description: ${result.description}`,
+ `Complexity: ${result.complexity.toUpperCase()}`,
+ `Est. Duration: ${result.estimatedDuration}`
+ ].join('\n'),
+ 'Task Registered'
+ );
+
+ if (result.suggestedAgents.length > 0) {
+ output.writeln();
+ output.writeln(output.bold('Suggested Agents'));
+ output.printTable({
+ columns: [
+ { key: 'type', header: 'Agent Type', width: 20 },
+ { key: 'confidence', header: 'Confidence', width: 12, align: 'right', format: (v) => `${(Number(v) * 100).toFixed(1)}%` },
+ { key: 'reason', header: 'Reason', width: 35 }
+ ],
+ data: result.suggestedAgents
+ });
+ }
+
+ if (result.risks.length > 0) {
+ output.writeln();
+ output.writeln(output.bold(output.error('Potential Risks')));
+ output.printList(result.risks.map(r => output.warning(r)));
+ }
+
+ if (result.recommendations.length > 0) {
+ output.writeln();
+ output.writeln(output.bold('Recommendations'));
+ output.printList(result.recommendations);
+ }
+
+ return { success: true, data: result };
+ } catch (error) {
+ if (error instanceof MCPClientError) {
+ output.printError(`Pre-task hook failed: ${error.message}`);
+ } else {
+ output.printError(`Unexpected error: ${String(error)}`);
+ }
+ return { success: false, exitCode: 1 };
+ }
+ }
+};
+
+// Post-task subcommand
+const postTaskCommand: Command = {
+ name: 'post-task',
+ description: 'Record task completion for learning',
+ options: [
+ {
+ name: 'task-id',
+ short: 'i',
+ description: 'Unique task identifier',
+ type: 'string',
+ required: true
+ },
+ {
+ name: 'success',
+ short: 's',
+ description: 'Whether the task succeeded',
+ type: 'boolean',
+ required: true
+ },
+ {
+ name: 'quality',
+ short: 'q',
+ description: 'Quality score (0-1)',
+ type: 'number'
+ },
+ {
+ name: 'agent',
+ short: 'a',
+ description: 'Agent that executed the task',
+ type: 'string'
+ }
+ ],
+ examples: [
+ { command: 'claude-flow hooks post-task -i task-123 --success true', description: 'Record successful completion' },
+ { command: 'claude-flow hooks post-task -i task-456 --success false -q 0.3', description: 'Record failed task' }
+ ],
+ action: async (ctx: CommandContext): Promise<CommandResult> => {
+ const taskId = ctx.flags.taskId as string;
+ const success = ctx.flags.success as boolean;
+
+ if (!taskId) {
+ output.printError('Task ID is required. Use --task-id or -i flag.');
+ return { success: false, exitCode: 1 };
+ }
+
+ if (success === undefined) {
+ output.printError('Success flag is required. Use --success true/false.');
+ return { success: false, exitCode: 1 };
+ }
+
+ output.printInfo(`Recording outcome for task: ${output.highlight(taskId)}`);
+
+ try {
+ const result = await callMCPTool<{
+ taskId: string;
+ success: boolean;
+ duration: number;
+ learningUpdates: {
+ patternsUpdated: number;
+ newPatterns: number;
+ trajectoryId: string;
+ };
+ }>('hooks/post-task', {
+ taskId,
+ success,
+ quality: ctx.flags.quality,
+ agent: ctx.flags.agent,
+ timestamp: Date.now(),
+ });
+
+ if (ctx.flags.format === 'json') {
+ output.printJson(result);
+ return { success: true, data: result };
+ }
+
+ output.writeln();
+ output.printSuccess(`Task outcome recorded: ${success ? 'SUCCESS' : 'FAILED'}`);
+
+ output.writeln();
+ output.writeln(output.bold('Learning Updates'));
+ output.printTable({
+ columns: [
+ { key: 'metric', header: 'Metric', width: 25 },
+ { key: 'value', header: 'Value', width: 20, align: 'right' }
+ ],
+ data: [
+ { metric: 'Patterns Updated', value: result.learningUpdates.patternsUpdated },
+ { metric: 'New Patterns', value: result.learningUpdates.newPatterns },
+ { metric: 'Duration', value: `${(result.duration / 1000).toFixed(1)}s` },
+ { metric: 'Trajectory ID', value: result.learningUpdates.trajectoryId }
+ ]
+ });
+
+ return { success: true, data: result };
+ } catch (error) {
+ if (error instanceof MCPClientError) {
+ output.printError(`Post-task hook failed: ${error.message}`);
+ } else {
+ output.printError(`Unexpected error: ${String(error)}`);
+ }
+ return { success: false, exitCode: 1 };
+ }
+ }
+};
+
+// Session-end subcommand
+const sessionEndCommand: Command = {
+ name: 'session-end',
+ description: 'End current session and persist state',
+ options: [
+ {
+ name: 'save-state',
+ short: 's',
+ description: 'Save session state for later restoration',
+ type: 'boolean',
+ default: true
+ }
+ ],
+ examples: [
+ { command: 'claude-flow hooks session-end', description: 'End and save session' },
+ { command: 'claude-flow hooks session-end --save-state false', description: 'End without saving' }
+ ],
+ action: async (ctx: CommandContext): Promise<CommandResult> => {
+ output.printInfo('Ending session...');
+
+ try {
+ const result = await callMCPTool<{
+ sessionId: string;
+ duration: number;
+ statePath?: string;
+ summary: {
+ tasksExecuted: number;
+ tasksSucceeded: number;
+ tasksFailed: number;
+ commandsExecuted: number;
+ filesModified: number;
+ agentsSpawned: number;
+ };
+ }>('hooks/session-end', {
+ saveState: ctx.flags.saveState ?? true,
+ timestamp: Date.now(),
+ });
+
+ if (ctx.flags.format === 'json') {
+ output.printJson(result);
+ return { success: true, data: result };
+ }
+
+ output.writeln();
+ output.printSuccess(`Session ${result.sessionId} ended`);
+
+ output.writeln();
+ output.writeln(output.bold('Session Summary'));
+ output.printTable({
+ columns: [
+ { key: 'metric', header: 'Metric', width: 25 },
+ { key: 'value', header: 'Value', width: 15, align: 'right' }
+ ],
+ data: [
+ { metric: 'Duration', value: `${(result.duration / 1000 / 60).toFixed(1)} min` },
+ { metric: 'Tasks Executed', value: result.summary.tasksExecuted },
+ { metric: 'Tasks Succeeded', value: output.success(String(result.summary.tasksSucceeded)) },
+ { metric: 'Tasks Failed', value: output.error(String(result.summary.tasksFailed)) },
+ { metric: 'Commands Executed', value: result.summary.commandsExecuted },
+ { metric: 'Files Modified', value: result.summary.filesModified },
+ { metric: 'Agents Spawned', value: result.summary.agentsSpawned }
+ ]
+ });
+
+ if (result.statePath) {
+ output.writeln();
+ output.writeln(output.dim(`State saved to: ${result.statePath}`));
+ }
+
+ return { success: true, data: result };
+ } catch (error) {
+ if (error instanceof MCPClientError) {
+ output.printError(`Session-end hook failed: ${error.message}`);
+ } else {
+ output.printError(`Unexpected error: ${String(error)}`);
+ }
+ return { success: false, exitCode: 1 };
+ }
+ }
+};
+
+// Session-restore subcommand
+const sessionRestoreCommand: Command = {
+ name: 'session-restore',
+ description: 'Restore a previous session',
+ options: [
+ {
+ name: 'session-id',
+ short: 'i',
+ description: 'Session ID to restore (use "latest" for most recent)',
+ type: 'string',
+ default: 'latest'
+ },
+ {
+ name: 'restore-agents',
+ short: 'a',
+ description: 'Restore spawned agents',
+ type: 'boolean',
+ default: true
+ },
+ {
+ name: 'restore-tasks',
+ short: 't',
+ description: 'Restore active tasks',
+ type: 'boolean',
+ default: true
+ }
+ ],
+ examples: [
+ { command: 'claude-flow hooks session-restore', description: 'Restore latest session' },
+ { command: 'claude-flow hooks session-restore -i session-12345', description: 'Restore specific session' }
+ ],
+ action: async (ctx: CommandContext): Promise<CommandResult> => {
+ const sessionId = ctx.args[0] || ctx.flags.sessionId as string || 'latest';
+
+ output.printInfo(`Restoring session: ${output.highlight(sessionId)}`);
+
+ try {
+ const result = await callMCPTool<{
+ sessionId: string;
+ originalSessionId: string;
+ restoredState: {
+ tasksRestored: number;
+ agentsRestored: number;
+ memoryRestored: number;
+ };
+ warnings?: string[];
+ }>('hooks/session-restore', {
+ sessionId,
+ restoreAgents: ctx.flags.restoreAgents ?? true,
+ restoreTasks: ctx.flags.restoreTasks ?? true,
+ timestamp: Date.now(),
+ });
+
+ if (ctx.flags.format === 'json') {
+ output.printJson(result);
+ return { success: true, data: result };
+ }
+
+ output.writeln();
+ output.printSuccess(`Session restored from ${result.originalSessionId}`);
+ output.writeln(output.dim(`New session ID: ${result.sessionId}`));
+
+ output.writeln();
+ output.writeln(output.bold('Restored State'));
+ output.printTable({
+ columns: [
+ { key: 'item', header: 'Item', width: 25 },
+ { key: 'count', header: 'Count', width: 15, align: 'right' }
+ ],
+ data: [
+ { item: 'Tasks', count: result.restoredState.tasksRestored },
+ { item: 'Agents', count: result.restoredState.agentsRestored },
+ { item: 'Memory Entries', count: result.restoredState.memoryRestored }
+ ]
+ });
+
+ if (result.warnings && result.warnings.length > 0) {
+ output.writeln();
+ output.writeln(output.bold(output.warning('Warnings')));
+ output.printList(result.warnings.map(w => output.warning(w)));
+ }
+
+ return { success: true, data: result };
+ } catch (error) {
+ if (error instanceof MCPClientError) {
+ output.printError(`Session-restore hook failed: ${error.message}`);
+ } else {
+ output.printError(`Unexpected error: ${String(error)}`);
+ }
+ return { success: false, exitCode: 1 };
+ }
+ }
+};
+
// Main hooks command
export const hooksCommand: Command = {
name: 'hooks',
@@ -1313,6 +1708,10 @@ export const hooksCommand: Command = {
postEditCommand,
preCommandCommand,
postCommandCommand,
+ preTaskCommand,
+ postTaskCommand,
+ sessionEndCommand,
+ sessionRestoreCommand,
routeCommand,
explainCommand,
pretrainCommand,