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

246 KB
Raw Blame
import { feedbackPatternsSchema, validateFeedbackPatterns } from '../memory/feedback-patterns.js';
/**
 * Hooks MCP Tools
 * Provides intelligent hooks functionality via MCP protocol
 */

import { mkdirSync, writeFileSync, existsSync, readFileSync, statSync, unlinkSync, readdirSync, rmSync } from 'fs';
import * as nodeFs from 'fs';
import { dirname, join, resolve, sep } from 'path';
import { type MCPTool, getProjectCwd } from './types.js';
import { validateIdentifier, validateText, validatePath } from './validate-input.js';
import { commandRecordingDisabled, redactCommandForStorage } from '../services/command-recording.js';
import { checkCommandLoop, recordCommandOutcome } from './tool-loop-guardrail.js';
import {
  buildLearnedRoutingPatterns,
  type LearnedRoutingOutcome,
  type LearnedRoutingPattern,
} from '../services/learned-routing.js';
import { applyTypesafeRouting, getTypesafeRouter } from '../ruvector/typesafe-router.js';
import {
  DEFAULT_ROUTER_EMBEDDER,
  embedForRouter,
  resolveRouterEmbedder,
  type RouterEmbedderKind,
} from '../ruvector/router-embedder.js';

// Real vector search functions - lazy loaded to avoid circular imports
let searchEntriesFn: ((options: {
  query: string;
  namespace?: string;
  limit?: number;
  threshold?: number;
}) => Promise<{
  success: boolean;
  results: { id: string; key: string; content: string; score: number; namespace: string }[];
  searchTime: number;
  error?: string;
}>) | null = null;

/**
 * Strip extended-thinking blocks from text before it enters a learning
 * trajectory (hermes-agent think_scrubber pattern). Claude models with extended
 * thinking emit <thinking>/<think>/<reasoning> blocks; if those land in a
 * trajectory's action/result text, the DISTILL step embeds reasoning-token
 * content that does not generalize, contaminating pattern confidence. Boundary-
 * gated: only strips well-formed paired tags, leaving prose that merely mentions
 * the tag names untouched.
 */
export function scrubReasoningBlocks(text: string): string {
  if (typeof text !== 'string' || text.indexOf('<') === -1) return text;
  return text
    .replace(/<think>[\s\S]*?<\/think>/gi, '')
    .replace(/<thinking>[\s\S]*?<\/thinking>/gi, '')
    .replace(/<reasoning>[\s\S]*?<\/reasoning>/gi, '')
    .replace(/<thought>[\s\S]*?<\/thought>/gi, '')
    .replace(/<REASONING_SCRATCHPAD>[\s\S]*?<\/REASONING_SCRATCHPAD>/gi, '')
    .trim();
}

async function getRealSearchFunction() {
  if (!searchEntriesFn) {
    try {
      const { searchEntries } = await import('../memory/memory-initializer.js');
      searchEntriesFn = searchEntries;
    } catch {
      searchEntriesFn = null;
    }
  }
  return searchEntriesFn;
}

// Real store function - lazy loaded
let storeEntryFn: ((options: {
  key: string;
  value: string;
  namespace?: string;
  generateEmbeddingFlag?: boolean;
  tags?: string[];
  ttl?: number;
  upsert?: boolean;
}) => Promise<{
  success: boolean;
  id: string;
  embedding?: { dimensions: number; model: string };
  error?: string;
}>) | null = null;

async function getRealStoreFunction() {
  if (!storeEntryFn) {
    try {
      const { storeEntry } = await import('../memory/memory-initializer.js');
      storeEntryFn = storeEntry;
    } catch {
      storeEntryFn = null;
    }
  }
  return storeEntryFn;
}

// =============================================================================
// Neural Module Lazy Loaders (SONA, EWC++, MoE, LoRA, Flash Attention)
// =============================================================================

// SONA Optimizer - lazy loaded
let sonaOptimizer: Awaited<ReturnType<typeof import('../memory/sona-optimizer.js').getSONAOptimizer>> | null = null;
async function getSONAOptimizer() {
  if (!sonaOptimizer) {
    try {
      const { getSONAOptimizer: getSona } = await import('../memory/sona-optimizer.js');
      sonaOptimizer = await getSona();
    } catch {
      sonaOptimizer = null;
    }
  }
  return sonaOptimizer;
}

// EWC++ Consolidator - lazy loaded
let ewcConsolidator: Awaited<ReturnType<typeof import('../memory/ewc-consolidation.js').getEWCConsolidator>> | null = null;
async function getEWCConsolidator() {
  if (!ewcConsolidator) {
    try {
      const { getEWCConsolidator: getEWC } = await import('../memory/ewc-consolidation.js');
      ewcConsolidator = await getEWC();
    } catch {
      ewcConsolidator = null;
    }
  }
  return ewcConsolidator;
}

// MoE Router - lazy loaded
// #1773 item 4 — moe-router migrated to @claude-flow/neural
let moeRouter: Awaited<ReturnType<typeof import('@claude-flow/neural').getMoERouter>> | null = null;
async function getMoERouter() {
  if (!moeRouter) {
    try {
      const { getMoERouter: getMoE } = await import('@claude-flow/neural');
      moeRouter = await getMoE();
    } catch {
      moeRouter = null;
    }
  }
  return moeRouter;
}

// Semantic Router - lazy loaded
// Tries native VectorDb first (16k+ routes/s HNSW), falls back to pure JS (47k routes/s cosine)
let semanticRouter: import('../ruvector/semantic-router.js').SemanticRouter | null = null;
let nativeVectorDb: unknown = null;
let routerBackend: 'native' | 'pure-js' | 'none' = 'none';

// Pre-computed embeddings for common task patterns (cached)
const TASK_PATTERN_EMBEDDINGS: Map<string, Float32Array> = new Map();

// ADR-390: the router's embedder (MiniLM or the historical hash). Pattern and
// query vectors always come from the SAME embedder; `routerEmbedder` records the
// one that actually built the current index (after any degradation to hash).
let routerEmbedder: RouterEmbedderKind = DEFAULT_ROUTER_EMBEDDER;
let routerEmbedderReason: string | undefined;
let routerRequestedEmbedder: RouterEmbedderKind | null = null;
let routerInitPromise: Promise<SemanticRouterHandle> | null = null;

// ── Runtime routing outcome persistence ──────────────────────────────
// Closes the learning loop: post-task records outcomes → route loads them.

const ROUTING_OUTCOMES_PATH = join(resolve('.'), '.claude-flow/routing-outcomes.json');

const ROUTING_STOPWORDS = new Set([
  'the','a','an','is','are','was','were','be','been','being','have','has','had',
  'do','does','did','will','would','could','should','may','might','shall','can',
  'to','of','in','for','on','with','at','by','from','as','into','through','during',
  'before','after','above','below','between','under','again','further','then','once',
  'it','its','this','that','these','those','i','me','my','we','our','you','your',
  'he','she','they','them','and','but','or','nor','not','no','so','if','when','than',
  'very','just','also','only','both','each','all','any','few','more','most','other',
  'some','such','same','new','now','here','there','where','how','what','which','who',
]);

type RoutingOutcome = LearnedRoutingOutcome;

interface RoutingPattern {
  keywords: string[];
  agents: string[];
  source?: 'static' | 'learned';
  support?: number;
  reliability?: number;
}

function extractKeywords(text: string): string[] {
  if (!text) return [];
  return text.toLowerCase()
    .replace(/[^a-z0-9\s-]/g, ' ')
    .split(/\s+/)
    .filter(w => w.length > 2 && !ROUTING_STOPWORDS.has(w));
}

function loadRoutingOutcomes(): RoutingOutcome[] {
  try {
    if (existsSync(ROUTING_OUTCOMES_PATH)) {
      const data = JSON.parse(readFileSync(ROUTING_OUTCOMES_PATH, 'utf-8'));
      return data.outcomes || [];
    }
  } catch { /* corrupt file, start fresh */ }
  return [];
}

function saveRoutingOutcomes(outcomes: RoutingOutcome[]): void {
  try {
    const dir = dirname(ROUTING_OUTCOMES_PATH);
    if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
    // Cap at 500 entries to bound file size
    const capped = outcomes.slice(-500);
    writeFileSync(ROUTING_OUTCOMES_PATH, JSON.stringify({ outcomes: capped }, null, 2));
    // A long-lived MCP process must observe the new label on the next route.
    // The prior singleton cache made the learned store inert until restart.
    semanticRouter = null;
    nativeVectorDb = null;
    routerInitPromise = null;
    routerRequestedEmbedder = null;
    routerBackend = 'none';
    TASK_PATTERN_EMBEDDINGS.clear();
  } catch { /* non-critical */ }
}

/**
 * Build learned routing patterns from successful task outcomes.
 * Returns patterns in the same shape as TASK_PATTERNS so they can be
 * merged into both the native HNSW and pure-JS semantic routers.
 */
function loadLearnedPatterns(): Record<string, LearnedRoutingPattern> {
  return buildLearnedRoutingPatterns(loadRoutingOutcomes());
}

/**
 * Merge static TASK_PATTERNS with runtime-learned patterns.
 * Static patterns take precedence (learned patterns won't overwrite them).
 */
function getMergedTaskPatterns(): Record<string, RoutingPattern> {
  const merged = { ...TASK_PATTERNS };
  const learned = loadLearnedPatterns();
  for (const [key, pattern] of Object.entries(learned)) {
    if (!merged[key]) {
      merged[key] = pattern;
    }
  }
  return merged;
}

// ── Static task patterns (used by both native and pure-JS routers) ───

const TASK_PATTERNS: Record<string, RoutingPattern> = {
  'security-task': {
    keywords: ['authentication', 'security', 'auth', 'password', 'encryption', 'vulnerability', 'cve', 'audit'],
    agents: ['security-architect', 'security-auditor', 'reviewer'],
  },
  'testing-task': {
    keywords: ['test', 'testing', 'spec', 'coverage', 'unit test', 'integration test', 'e2e'],
    agents: ['tester', 'reviewer'],
  },
  'api-task': {
    keywords: ['api', 'endpoint', 'rest', 'graphql', 'route', 'handler', 'controller'],
    agents: ['architect', 'coder', 'tester'],
  },
  'performance-task': {
    keywords: ['performance', 'optimize', 'speed', 'memory', 'benchmark', 'profiling', 'bottleneck'],
    agents: ['performance-engineer', 'coder', 'tester'],
  },
  'refactor-task': {
    keywords: ['refactor', 'restructure', 'clean', 'organize', 'modular', 'decouple'],
    agents: ['architect', 'coder', 'reviewer'],
  },
  'bugfix-task': {
    keywords: ['bug', 'fix', 'error', 'issue', 'broken', 'crash', 'debug'],
    agents: ['coder', 'tester', 'reviewer'],
  },
  'feature-task': {
    keywords: ['feature', 'implement', 'add', 'new', 'create', 'build'],
    agents: ['architect', 'coder', 'tester'],
  },
  'database-task': {
    keywords: ['database', 'sql', 'query', 'schema', 'migration', 'orm'],
    agents: ['architect', 'coder', 'tester'],
  },
  'frontend-task': {
    keywords: ['frontend', 'ui', 'component', 'react', 'css', 'style', 'layout'],
    agents: ['coder', 'reviewer', 'tester'],
  },
  'devops-task': {
    keywords: ['deploy', 'ci', 'cd', 'pipeline', 'docker', 'kubernetes', 'infrastructure'],
    agents: ['devops', 'coder', 'tester'],
  },
  'swarm-task': {
    keywords: ['swarm', 'agent', 'coordinator', 'hive', 'mesh', 'topology'],
    agents: ['swarm-specialist', 'coordinator', 'architect'],
  },
  'memory-task': {
    keywords: ['memory', 'cache', 'store', 'vector', 'embedding', 'persistence'],
    agents: ['memory-specialist', 'architect', 'coder'],
  },
};

/** Wrap hooks_route so the opt-in typesafe router (src/ruvector/typesafe-router.ts) can override the legacy pick. */
function withTypesafeRouting(legacy: (params: Record<string, unknown>) => Promise<object>) {
  return async (params: Record<string, unknown>) =>
    applyTypesafeRouting(params, (await legacy(params)) as Record<string, unknown>, TASK_PATTERNS, getTypesafeRouter());
}

interface SemanticRouterHandle {
  router: import('../ruvector/semantic-router.js').SemanticRouter | null;
  backend: 'native' | 'pure-js' | 'none';
  native: unknown;
  /** Embedder that built this index (ADR-390); the query MUST use the same one. */
  embedder: RouterEmbedderKind;
  embedderReason?: string;
}

/**
 * Get the semantic router with environment detection.
 * Tries native VectorDb first (HNSW, 16k routes/s), falls back to pure JS (47k routes/s cosine).
 *
 * ADR-390: `requested` (else CLAUDE_FLOW_ROUTER_EMBEDDER, else the default)
 * picks the embedder. The index is rebuilt when the requested embedder changes.
 * Concurrent callers share one in-flight build.
 */
async function getSemanticRouter(requested?: RouterEmbedderKind): Promise<SemanticRouterHandle> {
  const selection = resolveRouterEmbedder(requested);
  if (routerInitPromise && routerRequestedEmbedder === selection.kind) {
    return routerInitPromise;
  }
  semanticRouter = null;
  nativeVectorDb = null;
  routerBackend = 'none';
  TASK_PATTERN_EMBEDDINGS.clear();
  routerRequestedEmbedder = selection.kind;
  routerInitPromise = buildSemanticRouter(selection.kind, selection.reason);
  return routerInitPromise;
}

/** Test hook: drop the cached semantic index so the next route rebuilds it. */
export function resetSemanticRouterForTests(): void {
  semanticRouter = null;
  nativeVectorDb = null;
  routerBackend = 'none';
  routerInitPromise = null;
  routerRequestedEmbedder = null;
  TASK_PATTERN_EMBEDDINGS.clear();
}

/** Embed every pattern keyword with ONE embedder (ADR-390: never mix spaces). */
async function embedPatternKeywords(
  patterns: Record<string, RoutingPattern>,
  kind: RouterEmbedderKind,
): Promise<{ vectors: Map<string, Float32Array[]>; embedder: RouterEmbedderKind; reason?: string }> {
  const entries = Object.entries(patterns);
  const flat = entries.flatMap(([, p]) => p.keywords);
  const res = await embedForRouter(flat, kind);
  const vectors = new Map<string, Float32Array[]>();
  let i = 0;
  for (const [name, p] of entries) {
    vectors.set(name, res.vectors.slice(i, i + p.keywords.length));
    i += p.keywords.length;
  }
  return { vectors, embedder: res.embedder, reason: res.reason };
}

async function buildSemanticRouter(kind: RouterEmbedderKind, selectionReason?: string): Promise<SemanticRouterHandle> {
  const patterns = getMergedTaskPatterns();
  const embedded = await embedPatternKeywords(patterns, kind);
  routerEmbedder = embedded.embedder;
  routerEmbedderReason = embedded.reason ?? selectionReason;
  const handle = (): SemanticRouterHandle => ({
    router: semanticRouter,
    backend: routerBackend,
    native: nativeVectorDb,
    embedder: routerEmbedder,
    ...(routerEmbedderReason ? { embedderReason: routerEmbedderReason } : {}),
  });

  // STEP 1: Try native VectorDb from @ruvector/router (HNSW-backed)
  // Note: Native VectorDb uses a persistent database file which can have lock issues
  // in concurrent environments. We try it first but fall back gracefully to pure JS.
  // Deterministic/test and lock-constrained environments may force the
  // pure-JS router; production continues to prefer the native backend.
  if (process.env.CLAUDE_FLOW_DISABLE_NATIVE_ROUTER !== '1') try {
    // Use createRequire for ESM compatibility with native modules
    const { createRequire } = await import('module');
    const require = createRequire(import.meta.url);
    const router = require('@ruvector/router');

    if (router.VectorDb && router.DistanceMetric) {
      // Try to create VectorDb - may fail with lock error in concurrent envs
      const db = new router.VectorDb({
        dimensions: 384,
        distanceMetric: router.DistanceMetric.Cosine,
        hnswM: 16,
        hnswEfConstruction: 200,
        hnswEfSearch: 100,
      });

      // Initialize with static + runtime-learned task patterns
      for (const [patternName, { keywords }] of Object.entries(patterns)) {
        const embeddings = embedded.vectors.get(patternName) ?? [];
        keywords.forEach((keyword, i) => {
          db.insert(`${patternName}:${keyword}`, embeddings[i]);
          TASK_PATTERN_EMBEDDINGS.set(`${patternName}:${keyword}`, embeddings[i]);
        });
      }

      nativeVectorDb = db;
      routerBackend = 'native';
      console.log(`[hooks] Semantic router initialized: native VectorDb (HNSW, 16k+ routes/s), embedder=${routerEmbedder}`);
      return handle();
    }
  } catch (err) {
    // Native not available or database locked - fall back to pure JS
    // Common errors: "Database already open. Cannot acquire lock." or "MODULE_NOT_FOUND"
    // This is expected in concurrent environments or when binary isn't installed
  }

  // STEP 2: Fall back to pure JS SemanticRouter
  try {
    const { SemanticRouter } = await import('../ruvector/semantic-router.js');
    semanticRouter = new SemanticRouter({ dimension: 384 });

    for (const [patternName, { keywords, agents, source, support, reliability }] of Object.entries(patterns)) {
      const embeddings = embedded.vectors.get(patternName) ?? [];
      semanticRouter.addIntentWithEmbeddings(patternName, embeddings, {
        agents,
        keywords,
        source: source ?? 'static',
        support,
        reliability,
      });

      // Cache embeddings for keywords
      keywords.forEach((kw, i) => {
        TASK_PATTERN_EMBEDDINGS.set(kw, embeddings[i]);
      });
    }

    routerBackend = 'pure-js';
    console.log(`[hooks] Semantic router initialized: pure JS (cosine, 47k routes/s), embedder=${routerEmbedder}`);
  } catch {
    semanticRouter = null;
    routerBackend = 'none';
    console.log('[hooks] Semantic router initialized: none (no backend available)');
  }

  return handle();
}

/**
 * Get router backend info for status display.
 */
function getRouterBackendInfo(): { backend: string; speed: string } {
  switch (routerBackend) {
    case 'native':
      return { backend: 'native VectorDb (HNSW)', speed: '16k+ routes/s' };
    case 'pure-js':
      return { backend: 'pure JS (cosine)', speed: '47k routes/s' };
    default:
      return { backend: 'none', speed: 'N/A' };
  }
}

// Flash Attention - lazy loaded
// #1773 item 4 — flash-attention migrated to @claude-flow/neural
let flashAttention: Awaited<ReturnType<typeof import('@claude-flow/neural').getFlashAttention>> | null = null;
async function getFlashAttention() {
  if (!flashAttention) {
    try {
      const { getFlashAttention: getFlash } = await import('@claude-flow/neural');
      flashAttention = await getFlash();
    } catch {
      flashAttention = null;
    }
  }
  return flashAttention;
}

// LoRA Adapter - lazy loaded
let loraAdapter: Awaited<ReturnType<typeof import('../ruvector/lora-adapter.js').getLoRAAdapter>> | null = null;
async function getLoRAAdapter() {
  if (!loraAdapter) {
    try {
      const { getLoRAAdapter: getLora } = await import('../ruvector/lora-adapter.js');
      loraAdapter = await getLora();
    } catch {
      loraAdapter = null;
    }
  }
  return loraAdapter;
}

// Trajectory storage for SONA learning
interface TrajectoryStep {
  action: string;
  result: string;
  quality: number;
  timestamp: string;
}

interface TrajectoryData {
  id: string;
  task: string;
  agent: string;
  steps: TrajectoryStep[];
  startedAt: string;
  success?: boolean;
  endedAt?: string;
}

// Cache pending trajectories; the existing memory store also persists checkpoints.
const activeTrajectories = new Map<string, TrajectoryData>();
const dirtyTrajectories = new Set<string>();
const trajectoryOperations = new Map<string, Promise<void>>();

/** Serialize read/modify/checkpoint operations for a trajectory in this process. */
async function withTrajectoryOperation<T>(id: string, operation: () => Promise<T>): Promise<T> {
  const previous = trajectoryOperations.get(id) ?? Promise.resolve();
  let release!: () => void;
  const gate = new Promise<void>(resolve => { release = resolve; });
  const current = previous.then(() => gate);
  trajectoryOperations.set(id, current);
  await previous;
  try { return await operation(); }
  finally {
    release();
    if (trajectoryOperations.get(id) === current) trajectoryOperations.delete(id);
  }
}


async function persistPendingTrajectory(trajectory: TrajectoryData): Promise<boolean> {
  dirtyTrajectories.add(trajectory.id);
  try {
    const store = await getRealStoreFunction();
    const result = await store?.({
      key: `trajectory-pending-${trajectory.id}`, value: JSON.stringify(trajectory),
      namespace: 'trajectories', tags: [trajectory.agent, 'pending', 'sona-trajectory'], upsert: true,
    });
    if (result?.success === true) dirtyTrajectories.delete(trajectory.id);
    return result?.success === true;
  } catch { return false; }
}

async function loadPendingTrajectory(id: string): Promise<TrajectoryData | undefined> {
  try {
    const { getEntry } = await import('../memory/memory-initializer.js');
    // A completed record is authoritative even if pending-record cleanup failed
    // or another process still has an old cache entry.
    const completed = await getEntry({ key: `trajectory-${id}`, namespace: 'trajectories' });
    if (completed.success && completed.found) {
      activeTrajectories.delete(id);
      dirtyTrajectories.delete(id);
      return undefined;
    }
    // Failed checkpoints must not erase already-acknowledged live steps.
    // Clean caches still reload so another process's later checkpoint is seen.
    if (dirtyTrajectories.has(id) && activeTrajectories.has(id)) return activeTrajectories.get(id);
    const pending = await getEntry({ key: `trajectory-pending-${id}`, namespace: 'trajectories' });
    if (pending.success && pending.found) {
      const value = JSON.parse(pending.entry?.content ?? 'null');
      if (!value || value.id !== id || typeof value.task !== 'string' || typeof value.agent !== 'string'
        || typeof value.startedAt !== 'string' || !Number.isFinite(Date.parse(value.startedAt))
        || !Array.isArray(value.steps) || !value.steps.every((step: any) => step
          && typeof step.action === 'string' && typeof step.result === 'string'
          && typeof step.quality === 'number' && Number.isFinite(step.quality)
          && typeof step.timestamp === 'string')) return undefined;
      activeTrajectories.set(id, value);
      return value;
    }
  } catch { /* store unavailable: retain same-process best-effort tracking */ }
  return activeTrajectories.get(id);
}

// Memory store types and helpers
interface MemoryEntry {
  key: string;
  value: unknown;
  metadata?: Record<string, unknown>;
  // #2797 — the post-task dual-write persists `namespace: 'patterns'`
  // on routing-decision entries; the pattern counter reads it.
  namespace?: string;
  storedAt: string;
  accessCount: number;
  lastAccessed: string;
  expiresAt?: string;
}

interface MemoryStore {
  entries: Record<string, MemoryEntry>;
  version: string;
}

const MEMORY_DIR = '.claude-flow/memory';
const MEMORY_FILE = 'store.json';

function getMemoryPath(): string {
  return resolve(join(getProjectCwd(), MEMORY_DIR, MEMORY_FILE));
}

function loadMemoryStore(): MemoryStore {
  try {
    const path = getMemoryPath();
    if (existsSync(path)) {
      const data = readFileSync(path, 'utf-8');
      return JSON.parse(data);
    }
  } catch {
    // Return empty store on error
  }
  return { entries: {}, version: '3.0.0' };
}

interface ActiveSessionState {
  id: string;
  startedAt: string;
  metrics?: {
    edits?: number;
    commands?: number;
    tasks?: number;
    errors?: number;
  };
}

interface SessionActivity {
  tasksCompleted: number;
  patternsLearned: number;
  editsRecorded: number;
  commandsRecorded: number;
  errorsRecorded: number;
}

function isLearnedPatternEntry(entry: MemoryEntry): boolean {
  return entry.key.includes('pattern') ||
    entry.metadata?.type === 'pattern' ||
    entry.key.startsWith('learned-') ||
    entry.namespace === 'patterns' ||
    entry.metadata?.type === 'routing-decision';
}

function timestampInRange(value: unknown, start: number, end: number): boolean {
  if (typeof value !== 'string' && typeof value !== 'number') return false;
  const timestamp = typeof value === 'number' ? value : Date.parse(value);
  return Number.isFinite(timestamp) && timestamp >= start && timestamp <= end;
}

function nonNegativeInteger(value: unknown): number {
  return typeof value === 'number' && Number.isFinite(value)
    ? Math.max(0, Math.floor(value))
    : 0;
}

function loadActiveSessionState(): ActiveSessionState | null {
  try {
    const sessionPath = join(getProjectCwd(), '.claude-flow', 'sessions', 'current.json');
    if (!existsSync(sessionPath)) return null;
    const session = JSON.parse(readFileSync(sessionPath, 'utf-8')) as Partial<ActiveSessionState>;
    if (typeof session.id !== 'string' || session.id.trim().length === 0) return null;
    if (typeof session.startedAt !== 'string' || !Number.isFinite(Date.parse(session.startedAt))) return null;
    return session as ActiveSessionState;
  } catch {
    return null;
  }
}

function loadSessionActivity(session: ActiveSessionState, endedAt: number): SessionActivity {
  const startedAt = Date.parse(session.startedAt);
  let tasksCompleted = 0;

  try {
    const taskPath = join(getProjectCwd(), '.claude-flow', 'tasks', 'store.json');
    if (existsSync(taskPath)) {
      const store = JSON.parse(readFileSync(taskPath, 'utf-8')) as {
        tasks?: Record<string, { status?: string; completedAt?: string }>;
      };
      for (const task of Object.values(store.tasks ?? {})) {
        if (task.status === 'completed' && timestampInRange(task.completedAt, startedAt, endedAt)) {
          tasksCompleted++;
        }
      }
    }
  } catch {
    // Missing or malformed task state contributes no completed tasks.
  }

  const patternsLearned = Object.values(loadMemoryStore().entries)
    .filter(entry => isLearnedPatternEntry(entry) && timestampInRange(entry.storedAt, startedAt, endedAt))
    .length;

  return {
    tasksCompleted,
    patternsLearned,
    editsRecorded: nonNegativeInteger(session.metrics?.edits),
    commandsRecorded: nonNegativeInteger(session.metrics?.commands),
    errorsRecorded: nonNegativeInteger(session.metrics?.errors),
  };
}

function buildSessionSummary(activity: SessionActivity, duration: number): string {
  const durationMinutes = Math.max(0, Math.round(duration / 60000));
  const count = (value: number, singular: string, plural = `${singular}s`) =>
    `${value} ${value === 1 ? singular : plural}`;
  return [
    `${count(activity.tasksCompleted, 'task')} completed`,
    `${count(activity.patternsLearned, 'pattern')} learned`,
    `${count(activity.editsRecorded, 'edit')} recorded`,
    `${count(activity.commandsRecorded, 'command')} recorded`,
    `${count(activity.errorsRecorded, 'error')} recorded`,
    `duration ${count(durationMinutes, 'minute')}`,
  ].join('; ');
}

/**
 * Get real intelligence statistics from memory store
 */
function getIntelligenceStatsFromMemory(): {
  trajectories: { total: number; successful: number };
  patterns: {
    learned: number;
    successful: number;
    failed: number;
    unknown: number;
    described: number;
    categories: Record<string, number>;
  };
  memory: { indexSize: number; totalAccessCount: number; memorySizeBytes: number };
  routing: { decisions: number; avgConfidence: number };
} {
  const store = loadMemoryStore();
  const entries = Object.values(store.entries);

  // Count trajectories (keys starting with "trajectory-" or containing trajectory data)
  const trajectoryEntries = entries.filter(e =>
    e.key.includes('trajectory') ||
    (e.metadata?.type === 'trajectory')
  );
  const successfulTrajectories = trajectoryEntries.filter(e =>
    e.metadata?.success === true ||
    (typeof e.value === 'object' && e.value !== null && (e.value as Record<string, unknown>).success === true)
  );

  // Count patterns (#2797). The original filter matched only
  // `key.includes('pattern')` / `metadata.type==='pattern'` /
  // `learned-*`, but NO writer in the codebase ever produces that
  // shape — so Pattern Learning was permanently 0. The post-task
  // dual-write (#2786 fix-2) persists learned patterns as
  // `routing-decision:*` entries in the `patterns` namespace with
  // `metadata.type: 'routing-decision'`. Those ARE the learned
  // patterns the metric is meant to count, so include them: any entry
  // whose namespace is `patterns`, plus the original shapes for
  // forward-compatibility with a future explicit `pattern` writer.
  const patternEntries = entries.filter(isLearnedPatternEntry);

  // Categorize patterns
  const categories: Record<string, number> = {};
  patternEntries.forEach(e => {
    const category = (e.metadata?.category as string) || 'general';
    categories[category] = (categories[category] || 0) + 1;
  });
  const successfulPatterns = patternEntries.filter(e => e.metadata?.success === true).length;
  const failedPatterns = patternEntries.filter(e => e.metadata?.success === false).length;
  const describedPatterns = patternEntries.filter(e => {
    if (typeof e.metadata?.task === 'string' && e.metadata.task.trim()) return true;
    try {
      const value = typeof e.value === 'string' ? JSON.parse(e.value) : e.value;
      return typeof (value as Record<string, unknown> | null)?.task === 'string' &&
        Boolean(String((value as Record<string, unknown>).task).trim());
    } catch {
      return false;
    }
  }).length;

  // Count routing decisions
  const routingEntries = entries.filter(e =>
    e.key.includes('routing') ||
    e.metadata?.type === 'routing-decision'
  );

  // Calculate average confidence from routing decisions
  let totalConfidence = 0;
  let confidenceCount = 0;
  routingEntries.forEach(e => {
    const confidence = e.metadata?.confidence as number;
    if (typeof confidence === 'number') {
      totalConfidence += confidence;
      confidenceCount++;
    }
  });

  // Calculate total access count
  const totalAccessCount = entries.reduce((sum, e) => sum + (e.accessCount || 0), 0);

  // Calculate memory file size
  let memorySizeBytes = 0;
  try {
    const memPath = getMemoryPath();
    if (existsSync(memPath)) {
      memorySizeBytes = statSync(memPath).size;
    }
  } catch {
    // Ignore
  }

  return {
    trajectories: {
      total: trajectoryEntries.length,
      successful: successfulTrajectories.length,
    },
    patterns: {
      learned: patternEntries.length,
      successful: successfulPatterns,
      failed: failedPatterns,
      unknown: Math.max(0, patternEntries.length - successfulPatterns - failedPatterns),
      described: describedPatterns,
      categories,
    },
    memory: {
      indexSize: entries.length,
      totalAccessCount,
      memorySizeBytes,
    },
    routing: {
      decisions: routingEntries.length,
      avgConfidence: confidenceCount > 0 ? totalConfidence / confidenceCount : 0,
    },
  };
}

// Agent routing configuration - maps file types to recommended agents
const AGENT_PATTERNS: Record<string, string[]> = {
  '.ts': ['coder', 'architect', 'tester'],
  '.tsx': ['coder', 'architect', 'reviewer'],
  '.test.ts': ['tester', 'reviewer'],
  '.spec.ts': ['tester', 'reviewer'],
  '.md': ['researcher', 'documenter'],
  '.json': ['coder', 'architect'],
  '.yaml': ['coder', 'devops'],
  '.yml': ['coder', 'devops'],
  '.sh': ['devops', 'coder'],
  '.py': ['coder', 'ml-developer', 'researcher'],
  '.sql': ['coder', 'architect'],
  '.css': ['coder', 'designer'],
  '.scss': ['coder', 'designer'],
};

// Keyword patterns for fallback routing (when semantic routing doesn't match)
const KEYWORD_PATTERNS: Record<string, { agents: string[]; confidence: number }> = {
  'authentication': { agents: ['security-architect', 'coder', 'tester'], confidence: 0.9 },
  'auth': { agents: ['security-architect', 'coder', 'tester'], confidence: 0.85 },
  'api': { agents: ['architect', 'coder', 'tester'], confidence: 0.85 },
  'test': { agents: ['tester', 'reviewer'], confidence: 0.95 },
  'refactor': { agents: ['architect', 'coder', 'reviewer'], confidence: 0.9 },
  'performance': { agents: ['performance-engineer', 'coder', 'tester'], confidence: 0.88 },
  'security': { agents: ['security-architect', 'security-auditor', 'reviewer'], confidence: 0.92 },
  'database': { agents: ['architect', 'coder', 'tester'], confidence: 0.85 },
  'frontend': { agents: ['coder', 'designer', 'tester'], confidence: 0.82 },
  'backend': { agents: ['architect', 'coder', 'tester'], confidence: 0.85 },
  'bug': { agents: ['coder', 'tester', 'reviewer'], confidence: 0.88 },
  'fix': { agents: ['coder', 'tester', 'reviewer'], confidence: 0.85 },
  'feature': { agents: ['architect', 'coder', 'tester'], confidence: 0.8 },
  'swarm': { agents: ['swarm-specialist', 'coordinator', 'architect'], confidence: 0.9 },
  'memory': { agents: ['memory-specialist', 'architect', 'coder'], confidence: 0.88 },
  'deploy': { agents: ['devops', 'coder', 'tester'], confidence: 0.85 },
  'ci/cd': { agents: ['devops', 'coder'], confidence: 0.9 },
};

function getFileExtension(filePath: string): string {
  const match = filePath.match(/\.[a-zA-Z0-9]+$/);
  return match ? match[0] : '';
}

function suggestAgentsForFile(filePath: string): string[] {
  const ext = getFileExtension(filePath);

  // Check for test files first
  if (filePath.includes('.test.') || filePath.includes('.spec.')) {
    return AGENT_PATTERNS['.test.ts'] || ['tester', 'reviewer'];
  }

  return AGENT_PATTERNS[ext] || ['coder', 'architect'];
}

// Whole-word matchers for KEYWORD_PATTERNS. A bare `includes()` matched
// substrings: 'test' hit "latest" (tester @ 0.95), 'auth' hit "author",
// 'fix' hit "prefix", 'api' hit "capitalize". Single words get \b anchors plus
// simple inflections (tests, testing, fixes, deployed); phrases containing
// whitespace or '/' (e.g. 'ci/cd') match literally between word boundaries.
const KEYWORD_MATCHERS = Object.entries(KEYWORD_PATTERNS).map(([keyword, result]) => {
  const escaped = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const body = /[\s/]/.test(keyword) ? escaped : `${escaped}(?:s|es|ing|ed)?`;
  return { regex: new RegExp(`\\b${body}\\b`, 'i'), result };
});

/**
 * Confidence for a task nothing matched (#3567). It must rank below every real
 * signal: keyword hits are >= 0.8, learned outcomes >= 0.7, and a semantic
 * match is only eligible above 0.4. Matches the helper router's 0.3 fall-through.
 */
export const NO_MATCH_CONFIDENCE = 0.3;

export interface TaskAgentSuggestion {
  agents: string[];
  confidence: number;
  /** false when no keyword or learned pattern matched; the agents are a default, not a decision. */
  matched: boolean;
  reason?: 'no-match-default';
  note?: string;
}

/** True when the task has at least one letter or digit in any script. */
export function hasRoutableText(task: string): boolean {
  return /[\p{L}\p{N}]/u.test(task);
}

// Keyword matchers are English regexes; letters outside Latin script can never hit them.
const NON_LATIN_LETTER = /(?![A-Za-zÀ-ɏ])\p{L}/u;

function noMatchNote(task: string): string {
  if (!hasRoutableText(task)) return 'Task has no words to match.';
  if (NON_LATIN_LETTER.test(task)) {
    return 'Keyword matchers are English-only, so non-English text cannot match them.';
  }
  return 'No keyword or learned pattern matched.';
}

/** Exported for tests. */
export function suggestAgentsForTask(task: string): TaskAgentSuggestion {
  const noMatch = (): TaskAgentSuggestion => ({
    agents: ['coder', 'researcher', 'tester'],
    confidence: NO_MATCH_CONFIDENCE,
    matched: false,
    reason: 'no-match-default',
    note: noMatchNote(task),
  });
  if (!hasRoutableText(task)) return noMatch();

  // Check static keyword patterns first
  for (const { regex, result } of KEYWORD_MATCHERS) {
    if (regex.test(task)) {
      return { ...result, matched: true };
    }
  }

  // Check runtime-learned patterns from successful task outcomes
  const taskKeywords = extractKeywords(task);
  if (taskKeywords.length > 0) {
    const outcomes = loadRoutingOutcomes();
    let bestAgent = '';
    let bestOverlap = 0;

    for (const outcome of outcomes) {
      if (!outcome.success || !outcome.agent || !outcome.keywords?.length) continue;
      const overlap = taskKeywords.filter(kw => outcome.keywords.includes(kw)).length;
      if (overlap > bestOverlap) {
        bestOverlap = overlap;
        bestAgent = outcome.agent;
      }
    }

    // Require at least 2 keyword overlap to prevent false positives
    if (bestAgent && bestOverlap >= 2) {
      return { agents: [bestAgent], confidence: Math.min(0.6 + bestOverlap * 0.05, 0.85), matched: true };
    }
  }

  return noMatch();
}

function assessCommandRisk(command: string): { risk: string; level: number; warnings: string[] } {
  const warnings: string[] = [];
  let level = 0;

  // High risk commands
  if (command.includes('rm -rf') || command.includes('rm -r')) {
    level = Math.max(level, 0.9);
    warnings.push('Recursive deletion detected - verify target path');
  }
  if (command.includes('sudo')) {
    level = Math.max(level, 0.7);
    warnings.push('Elevated privileges requested');
  }
  if (command.includes('> /') || command.includes('>> /')) {
    level = Math.max(level, 0.6);
    warnings.push('Writing to system path');
  }
  if (command.includes('chmod') || command.includes('chown')) {
    level = Math.max(level, 0.5);
    warnings.push('Permission modification');
  }
  if (command.includes('curl') && command.includes('|')) {
    level = Math.max(level, 0.8);
    warnings.push('Piping remote content to shell');
  }

  // Safe commands
  if (command.startsWith('npm ') || command.startsWith('npx ')) {
    level = Math.min(level, 0.3);
  }
  if (command.startsWith('git ')) {
    level = Math.min(level, 0.2);
  }
  if (command.startsWith('ls ') || command.startsWith('cat ') || command.startsWith('echo ')) {
    level = Math.min(level, 0.1);
  }

  const risk = level >= 0.7 ? 'high' : level >= 0.4 ? 'medium' : 'low';

  return { risk, level, warnings };
}

// MCP Tool implementations - return raw data for direct CLI use
export const hooksPreEdit: MCPTool = {
  name: 'hooks_pre-edit',
  description: 'Get context and agent suggestions before editing a file Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      filePath: { type: 'string', description: 'Path to the file being edited' },
      operation: { type: 'string', description: 'Type of operation (create, update, delete, refactor)' },
      context: { type: 'string', description: 'Additional context' },
    },
    required: ['filePath'],
  },
  handler: async (params: Record<string, unknown>) => {
    const filePath = params.filePath as string;
    const operation = (params.operation as string) || 'update';

    { const v = validatePath(filePath, 'filePath'); if (!v.valid) return { success: false, error: v.error }; }

    const suggestedAgents = suggestAgentsForFile(filePath);
    const ext = getFileExtension(filePath);

    return {
      filePath,
      operation,
      context: {
        fileExists: true,
        fileType: ext || 'unknown',
        relatedFiles: [],
        suggestedAgents,
        patterns: [
          { pattern: `${ext} file editing`, confidence: 0.85 },
        ],
        risks: operation === 'delete' ? ['File deletion is irreversible'] : [],
      },
      recommendations: [
        `Recommended agents: ${suggestedAgents.join(', ')}`,
        'Run tests after changes',
      ],
    };
  },
};

export const hooksPostEdit: MCPTool = {
  name: 'hooks_post-edit',
  description: 'Record editing outcome for learning Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      filePath: { type: 'string', description: 'Path to the edited file' },
      success: { type: 'boolean', description: 'Whether the edit was successful' },
      agent: { type: 'string', description: 'Agent that performed the edit' },
    },
    required: ['filePath'],
  },
  handler: async (params: Record<string, unknown>) => {
    const filePath = params.filePath as string;
    const success = params.success !== false;
    const agent = params.agent as string | undefined;

    { const v = validatePath(filePath, 'filePath'); if (!v.valid) return { success: false, error: v.error }; }
    if (agent) { const v = validateIdentifier(agent, 'agent'); if (!v.valid) return { success: false, error: v.error }; }

    // Wire recordFeedback through bridge (issue #1209)
    let feedbackResult: { success: boolean; controller: string; updated: number } | null = null;
    try {
      const bridge = await import('../memory/memory-bridge.js');
      feedbackResult = await bridge.bridgeRecordFeedback({
        taskId: `edit-${filePath}-${Date.now()}`,
        success,
        quality: success ? 0.85 : 0.3,
        agent,
      });
    } catch {
      // Bridge not available — continue with basic response
    }

    // #2245 Round B — also feed the trajectory pipeline so globalStats
    // (and the unified-stats aggregator in ADR-075) reflects the activity.
    // Synthesises a one-step trajectory from the edit outcome.
    let learningPath: 'trajectory-pipeline' | 'recorded-only' = 'recorded-only';
    let trajectoriesDelta = 0;
    try {
      const intel = await import('../memory/intelligence.js');
      const before = intel.getIntelligenceStats().trajectoriesRecorded;
      await intel.recordTrajectory(
        [{
          type: 'action',
          content: `Edit ${filePath}${agent ? ` by ${agent}` : ''}: ${success ? 'success' : 'failure'}`,
          metadata: { hook: 'post-edit', filePath, agent, success },
          timestamp: Date.now(),
        }],
        success ? 'success' : 'failure',
      );
      trajectoriesDelta = intel.getIntelligenceStats().trajectoriesRecorded - before;
      if (trajectoriesDelta > 0) learningPath = 'trajectory-pipeline';
    } catch { /* intelligence module not yet initialised — keep recorded-only */ }

    return {
      recorded: true,
      filePath,
      success,
      timestamp: new Date().toISOString(),
      learningUpdate: success ? 'pattern_reinforced' : 'pattern_adjusted',
      learningPath,                  // ADR-074 / ADR-075 — honest path naming
      trajectoriesDelta,
      feedback: feedbackResult ? {
        recorded: feedbackResult.success,
        controller: feedbackResult.controller,
        updates: feedbackResult.updated,
      } : { recorded: false, controller: 'unavailable', updates: 0 },
      note: learningPath === 'trajectory-pipeline'
        ? `Edit outcome fed to the SONA + EWC++ trajectory pipeline (trajectoriesRecorded +${trajectoriesDelta}).`
        : 'Edit outcome stored via memory-bridge only; the trajectory pipeline was not reachable in this process.',
    };
  },
};

export const hooksPreCommand: MCPTool = {
  name: 'hooks_pre-command',
  description: 'Assess risk before executing a command Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      command: { type: 'string', description: 'Command to execute' },
    },
    required: ['command'],
  },
  handler: async (params: Record<string, unknown>) => {
    const command = params.command as string;

    { const v = validateText(command, 'command'); if (!v.valid) return { success: false, error: v.error }; }

    const assessment = assessCommandRisk(command);

    const riskLevel = assessment.level >= 0.8 ? 'critical'
      : assessment.level >= 0.6 ? 'high'
        : assessment.level >= 0.3 ? 'medium'
          : 'low';

    // #6: tool-loop circuit breaker — warn/block when this exact command has
    // failed repeatedly in a row (an agent stuck looping on a failing call).
    const loop = checkCommandLoop(command);
    const recommendations = assessment.warnings.length > 0
      ? ['Review warnings before proceeding', 'Consider using safer alternative']
      : ['Command appears safe to execute'];
    if (loop.hint) recommendations.unshift(loop.hint);

    return {
      command,
      riskLevel,
      risks: assessment.warnings.map((warning, i) => ({
        type: `risk-${i + 1}`,
        severity: assessment.level >= 0.6 ? 'high' : 'medium',
        description: warning,
      })),
      recommendations,
      loopGuard: { verdict: loop.verdict, consecutiveFailures: loop.consecutiveFailures },
      safeAlternatives: [],
      // Don't proceed on a high-risk command OR a hard loop-block.
      shouldProceed: assessment.level < 0.7 && loop.verdict !== 'block',
    };
  },
};

export const hooksPostCommand: MCPTool = {
  name: 'hooks_post-command',
  description: 'Record command execution outcome Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      command: { type: 'string', description: 'Executed command' },
      exitCode: { type: 'number', description: 'Command exit code' },
      success: { type: 'boolean', description: 'Explicit execution outcome; false records a failure even without a nonzero exit code' },
      ttl: { type: 'integer', minimum: 1, maximum: 2147483647, description: 'Command history lifetime in seconds (default: 30 days)' },
    },
    required: ['command'],
  },
  handler: async (params: Record<string, unknown>) => {
    const command = params.command as string;
    const exitCode = (params.exitCode as number) || 0;
    const success = exitCode === 0 && params.success !== false;
    const ttl = params.ttl ?? 30 * 24 * 60 * 60;
    if (typeof ttl !== 'number' || !Number.isSafeInteger(ttl) || ttl < 1 || ttl > 2147483647) {
      return { success: false, recorded: false, error: 'ttl must be a positive integer number of seconds (at most 2147483647)' };
    }

    { const v = validateText(command, 'command'); if (!v.valid) return { success: false, error: v.error }; }

    // #6: feed the tool-loop circuit breaker so pre-command can warn/block on
    // repeated consecutive failures of the same command.
    recordCommandOutcome(command, success);

    // #4056: command history is opt-out (CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=0,
    // CLAUDE_FLOW_HOOKS_ENABLED=false, or hooks.recordCommands=false in the
    // config). The circuit breaker above is in-memory only; nothing below
    // runs when recording is off, so no command text reaches the store or
    // the trajectory pipeline.
    if (commandRecordingDisabled(process.env, getProjectCwd())) {
      return {
        recorded: false,
        disabled: true,
        command: undefined,
        exitCode,
        success,
        timestamp: new Date().toISOString(),
        _storedIn: 'none' as const,
        learningPath: 'recorded-only' as const,
        trajectoriesDelta: 0,
        note: 'Command recording is switched off (CLAUDE_FLOW_HOOKS_RECORD_COMMANDS / CLAUDE_FLOW_HOOKS_ENABLED / hooks.recordCommands); nothing was stored.',
      };
    }

    // What is stored is the redacted, length-capped form, never the raw text.
    const storedCommand = redactCommandForStorage(command);

    // Persist command outcome via AgentDB
    let _storedIn: 'agentdb' | 'json-store' | 'none' = 'none';
    try {
      const bridge = await import('../memory/memory-bridge.js');
      const stored = await bridge.bridgeStoreEntry({
        key: `cmd-${Date.now()}`,
        value: JSON.stringify({ command: storedCommand, exitCode, success }),
        namespace: 'commands',
        tags: [success ? 'success' : 'error'],
        ttl,
        generateEmbeddingFlag: false,
      });
      if (!stored?.success) throw new Error(stored?.error || 'Native bridge did not store the command');
      _storedIn = 'agentdb';
    } catch {
      // AgentDB not available — store in JSON
      try {
        const store = loadMemoryStore();
        const now = Date.now();
        for (const [entryKey, entry] of Object.entries(store.entries)) {
          if (entry.namespace === 'commands' && entry.expiresAt && Date.parse(entry.expiresAt) <= now) {
            delete store.entries[entryKey];
          }
        }
        const key = `cmd-${now}`;
        store.entries[key] = {
          key, value: JSON.stringify({ command: storedCommand, exitCode, success }), namespace: 'commands',
          storedAt: new Date(now).toISOString(), lastAccessed: new Date(now).toISOString(), accessCount: 0,
          expiresAt: new Date(now + ttl * 1000).toISOString(),
        };
        const memDir = dirname(getMemoryPath());
        if (!existsSync(memDir)) mkdirSync(memDir, { recursive: true });
        writeFileSync(getMemoryPath(), JSON.stringify(store, null, 2), 'utf-8');
        _storedIn = 'json-store';
      } catch { /* non-critical */ }
    }

    // #2245 Round B — feed the trajectory pipeline so globalStats reflects
    // command outcomes alongside the AgentDB entry that already gets written.
    let learningPath: 'trajectory-pipeline' | 'recorded-only' = 'recorded-only';
    let trajectoriesDelta = 0;
    try {
      const intel = await import('../memory/intelligence.js');
      const before = intel.getIntelligenceStats().trajectoriesRecorded;
      await intel.recordTrajectory(
        [{
          type: 'action',
          content: `Command \`${storedCommand.slice(0, 200)}\` exited ${exitCode} (${success ? 'success' : 'failure'})`,
          metadata: { hook: 'post-command', command: storedCommand.slice(0, 500), exitCode, success },
          timestamp: Date.now(),
        }],
        success ? 'success' : 'failure',
      );
      trajectoriesDelta = intel.getIntelligenceStats().trajectoriesRecorded - before;
      if (trajectoriesDelta > 0) learningPath = 'trajectory-pipeline';
    } catch { /* intelligence module not yet initialised — keep recorded-only */ }

    return {
      recorded: _storedIn !== 'none',
      command,
      exitCode,
      success,
      timestamp: new Date().toISOString(),
      _storedIn,
      learningPath,                  // 'trajectory-pipeline' | 'recorded-only'
      trajectoriesDelta,
      note: learningPath === 'trajectory-pipeline'
        ? `Command outcome fed to the SONA + EWC++ trajectory pipeline (trajectoriesRecorded +${trajectoriesDelta}).`
        : `Command outcome stored via ${_storedIn}; the trajectory pipeline was not reachable in this process.`,
    };
  },
};

export const hooksRoute: MCPTool = {
  name: 'hooks_route',
  description: 'Get a 3-tier routing recommendation for a task: Tier 1 (deterministic codemod, ~0ms / $0 — for var-to-const, remove-console, add-logging), Tier 2 (Haiku — simple), Tier 3 (Sonnet/Opus — complex). Use this BEFORE spawning an agent to avoid sending simple transforms to Sonnet. Native tools have no equivalent — Claude Code does not introspect its own model-selection cost. Returns the recommended model + a `[CODEMOD_AVAILABLE]` literal when a deterministic codemod can fully apply the edit (then call hooks_codemod). Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'Task description' },
      context: { type: 'string', description: 'Additional context' },
      useSemanticRouter: { type: 'boolean', description: 'Use semantic similarity routing (default: true)' },
    },
    required: ['task'],
  },
  // Opt-in @ruvector/typesafe augmentation (CLAUDE_FLOW_ROUTER_TYPESAFE=1); returns the legacy result unchanged when unset.
  handler: withTypesafeRouting(async (params: Record<string, unknown>) => {
    const task = params.task as string;
    const context = params.context as string | undefined;
    const useSemanticRouter = params.useSemanticRouter !== false;

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }
    if (context) { const v = validateText(context, 'context'); if (!v.valid) return { success: false, error: v.error }; }

    // Phase 5: Try AgentDB's SemanticRouter / LearningSystem first
    if (useSemanticRouter) {
      try {
        const bridge = await import('../memory/memory-bridge.js');
        const agentdbRoute = await bridge.bridgeRouteTask({ task, context });
        if (agentdbRoute && agentdbRoute.confidence > 0.5) {
          const agents = agentdbRoute.agents.length > 0 ? agentdbRoute.agents : ['coder', 'researcher'];
          const complexity = task.length > 200 ? 'high' : task.length < 50 ? 'low' : 'medium';
          return {
            task,
            routing: {
              method: `agentdb-${agentdbRoute.controller}`,
              backend: agentdbRoute.controller,
              latencyMs: 0,
              throughput: 'N/A',
            },
            matchedPattern: agentdbRoute.route,
            semanticMatches: [{ pattern: agentdbRoute.route, score: agentdbRoute.confidence }],
            primaryAgent: {
              type: agents[0],
              confidence: Math.round(agentdbRoute.confidence * 100) / 100,
              reason: `AgentDB ${agentdbRoute.controller}: "${agentdbRoute.route}" (${Math.round(agentdbRoute.confidence * 100)}%)`,
            },
            alternativeAgents: agents.slice(1).map((agent, i) => ({
              type: agent,
              confidence: Math.round((agentdbRoute.confidence - (0.1 * (i + 1))) * 100) / 100,
              reason: `Alternative from ${agentdbRoute.controller}`,
            })),
            estimatedMetrics: {
              successProbability: Math.round(agentdbRoute.confidence * 100) / 100,
              estimatedDuration: complexity === 'high' ? '2-4 hours' : complexity === 'medium' ? '30-60 min' : '10-30 min',
              complexity,
            },
            swarmRecommendation: agents.length > 2 ? { topology: 'hierarchical', agents, coordination: 'queen-led' } : null,
          };
        }
      } catch {
        // AgentDB router not available — fall through to local routing
      }

      // Trajectory learning persists SONA outcomes separately from the local
      // semantic index. Only consume an eligible learned pattern here; native,
      // Q-learning and keyword suggestions retain their existing routing paths.
      try {
        const sonaStart = performance.now();
        // #4581: fold in any verified outcomes the ruflo-mods learning loop
        // recorded since the last call (one stat when nothing changed).
        await ingestVerifiedQuietly();
        const sona = await getSONAOptimizer();
        const suggestion = await sona?.getRoutingSuggestion(task);
        const { isSupportedSonaAgent } = await import('../memory/sona-optimizer.js');
        if (suggestion?.source === 'sona-pattern'
          && Number.isFinite(suggestion.confidence)
          && suggestion.confidence >= 0.6 && suggestion.confidence <= 1
          && isSupportedSonaAgent(suggestion.agent)) {
          const confidence = Math.round(suggestion.confidence * 100) / 100;
          const complexity = task.length > 200 ? 'high' : task.length < 50 ? 'low' : 'medium';
          return {
            task,
            routing: { method: 'sona-pattern', backend: 'SONA persisted keyword patterns', latencyMs: performance.now() - sonaStart, throughput: 'N/A' },
            matched: true,
            matchedPattern: 'sona-pattern',
            semanticMatches: [],
            primaryAgent: {
              type: suggestion.agent,
              confidence,
              reason: `Persisted SONA outcome pattern (${Math.round(suggestion.confidence * 100)}% confidence)`,
            },
            alternativeAgents: suggestion.alternatives
              .filter(alternative => isSupportedSonaAgent(alternative.agent) && Number.isFinite(alternative.score))
              .map(alternative => ({
                type: alternative.agent,
                confidence: Math.round(alternative.score * 100) / 100,
                reason: 'Alternative from persisted SONA patterns',
              })),
            estimatedMetrics: {
              successProbability: confidence,
              estimatedDuration: complexity === 'high' ? '2-4 hours' : complexity === 'medium' ? '30-60 min' : '10-30 min',
              complexity,
            },
            swarmRecommendation: null,
            learning: { source: suggestion.source, matchedKeywords: suggestion.matchedKeywords },
          };
        }
      } catch {
        // Optional SONA state unavailable — preserve the existing local route.
      }
    }

    return routeTaskLocal(task, context, useSemanticRouter);
  }),
};

/**
 * hooks_route's local routing (semantic index + keyword fallback), run after the
 * AgentDB and eligible SONA pre-routes. Shared with {@link routeTaskForBench}
 * so the benchmark measures this local path. (Body kept at its original
 * indentation to keep this refactor's diff small.)
 */
async function routeTaskLocal(
  task: string,
  context: string | undefined,
  useSemanticRouter: boolean,
  embedderOverride?: RouterEmbedderKind,
): Promise<LocalRouteResult> {
    // Get router (tries native VectorDb first, falls back to pure JS)
    let handle: SemanticRouterHandle = useSemanticRouter
      ? await getSemanticRouter(embedderOverride)
      : {
        router: null, backend: 'none', native: null,
        embedder: resolveRouterEmbedder(embedderOverride).kind,
        embedderReason: 'semantic router disabled (useSemanticRouter=false)',
      };

    let semanticResult: { intent: string; score: number; metadata: Record<string, unknown> }[] = [];
    let routingMethod = 'keyword';
    let routingLatencyMs = 0;
    let backendInfo = '';

    const queryText = context ? `${task} ${context}` : task;
    // ADR-390: the query is embedded with the embedder that built the index.
    let queryEmbedding: Float32Array | null = null;
    if (handle.router || handle.native) {
      const q = await embedForRouter([queryText], handle.embedder);
      if (q.embedder !== handle.embedder) {
        // MiniLM failed on the query after building a MiniLM index: rebuild the
        // index with the hash so patterns and query share one space again.
        routerInitPromise = buildSemanticRouter('hash', q.reason);
        handle = await routerInitPromise;
      }
      queryEmbedding = q.vectors[0];
    }
    const { router, backend, native } = handle;

    // Try native VectorDb (HNSW-backed)
    if (native && backend === 'native' && queryEmbedding) {
      const routeStart = performance.now();
      try {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const results = (native as any).search(queryEmbedding, 5);
        routingLatencyMs = performance.now() - routeStart;
        routingMethod = 'semantic-native';
        backendInfo = 'native VectorDb (HNSW)';

        // Convert results to semantic format
        const mergedPatterns = getMergedTaskPatterns();
        semanticResult = results.map((r: { id: string; score: number }) => {
          const [patternName] = r.id.split(':');
          const pattern = mergedPatterns[patternName];
          return {
            intent: patternName,
            score: 1 - r.score, // Native uses distance (lower is better), convert to similarity
            metadata: {
              agents: pattern?.agents || (patternName.startsWith('learned-') ? [patternName.slice(8)] : ['coder']),
              source: pattern?.source ?? (patternName.startsWith('learned-') ? 'learned' : 'static'),
              support: pattern?.support,
              reliability: pattern?.reliability,
            },
          };
        });
      } catch {
        // Native failed, try pure JS fallback
      }
    }

    // Try pure JS SemanticRouter fallback
    if (router && backend === 'pure-js' && queryEmbedding && semanticResult.length === 0) {
      const routeStart = performance.now();
      semanticResult = router.routeWithEmbedding(queryEmbedding, 3);
      routingLatencyMs = performance.now() - routeStart;
      routingMethod = 'semantic-pure-js';
      backendInfo = 'pure JS (cosine similarity)';
    }

    // Get agents from semantic routing or fall back to keyword
    let agents: string[];
    let confidence: number;
    let matchedPattern = '';
    let noMatchDetail: string | null = null;

    // Both static and learned patterns are gated on the same similarity
    // score. Learned patterns additionally require support/reliability as a
    // quality guard, but do NOT need a higher score bar — a learned pattern
    // that outscores every static candidate must not lose to one anyway
    // (#2864: a 25pp higher threshold made a top-scoring learned-researcher
    // match at 0.57 lose to a static match at 0.52, discarding the learned
    // store's output on the majority of routes).
    // A task with no letters or digits has nothing for the embedder to mean;
    // any similarity it scores is noise, so it never becomes a semantic match.
    const eligibleSemantic = !hasRoutableText(task) ? undefined : semanticResult.find((match) => {
      if (match.score <= 0.4) return false;
      const learned = match.intent.startsWith('learned-') || match.metadata.source === 'learned';
      if (!learned) return true;
      return Number(match.metadata.support ?? 0) >= 2
        && Number(match.metadata.reliability ?? 0) >= 0.75;
    });
    if (eligibleSemantic) {
      const topMatch = eligibleSemantic;
      agents = (topMatch.metadata.agents as string[]) || ['coder', 'researcher'];
      confidence = topMatch.score;
      matchedPattern = topMatch.intent;
    } else {
      // Fall back to keyword matching
      const suggestion = suggestAgentsForTask(task);
      agents = suggestion.agents;
      confidence = suggestion.confidence;
      routingMethod = 'keyword';
      if (suggestion.matched) {
        matchedPattern = 'keyword-fallback';
        backendInfo = 'keyword matching';
      } else {
        matchedPattern = 'no-match-default';
        backendInfo = 'keyword matching (no match)';
        noMatchDetail = suggestion.note ?? 'No pattern matched.';
      }
    }

    // Determine complexity
    const taskLower = task.toLowerCase();
    const complexity = taskLower.includes('complex') || taskLower.includes('architecture') || task.length > 200
      ? 'high'
      : taskLower.includes('simple') || taskLower.includes('fix') || task.length < 50
        ? 'low'
        : 'medium';

    return {
      task,
      routing: {
        method: routingMethod,
        backend: backendInfo,
        latencyMs: routingLatencyMs,
        throughput: routingLatencyMs > 0 ? `${Math.round(1000 / routingLatencyMs)} routes/s` : 'N/A',
      },
      matchedPattern,
      // #3567: callers branch on `matched`; a no-match result is a default, not a decision.
      matched: noMatchDetail === null,
      ...(noMatchDetail !== null ? { reason: 'no-match-default', note: noMatchDetail } : {}),
      semanticMatches: semanticResult.slice(0, 3).map(r => ({
        pattern: r.intent,
        score: Math.round(r.score * 100) / 100,
      })),
      primaryAgent: {
        type: agents[0],
        confidence: Math.round(confidence * 100) / 100,
        reason: routingMethod.startsWith('semantic')
          ? `Semantic similarity to "${matchedPattern}" pattern (${Math.round(confidence * 100)}%)`
          : noMatchDetail !== null
            ? `Nothing matched; default suggestion only. ${noMatchDetail}`
            : `Task contains keywords matching ${agents[0]} specialization`,
      },
      alternativeAgents: agents.slice(1).map((agent, i) => ({
        type: agent,
        confidence: Math.max(0, Math.round((confidence - (0.1 * (i + 1))) * 100) / 100),
        reason: noMatchDetail !== null ? 'Default suggestion (nothing matched)' : `Alternative agent for ${agent} capabilities`,
      })),
      estimatedMetrics: {
        // No pattern means no basis for a success estimate.
        successProbability: noMatchDetail !== null ? null : Math.round(confidence * 100) / 100,
        estimatedDuration: complexity === 'high' ? '2-4 hours' : complexity === 'medium' ? '30-60 min' : '10-30 min',
        complexity,
      },
      swarmRecommendation: agents.length > 2 ? {
        topology: 'hierarchical',
        agents,
        coordination: 'queen-led',
      } : null,
      // ADR-390: which embedder the semantic index + query used ('hash' when degraded).
      embedder: handle.embedder,
      ...(handle.embedderReason ? { embedderReason: handle.embedderReason } : {}),
    };
}

type LocalRouteResult = Record<string, unknown> & {
  primaryAgent: { type: string; confidence: number; reason: string };
  matchedPattern: string;
  embedder: RouterEmbedderKind;
  embedderReason?: string;
};

/** Result of {@link routeTaskForBench}. */
export interface BenchRouteResult {
  primaryAgent: string;
  confidence: number;
  /** Matched pattern name, or 'keyword-fallback'. */
  pattern: string;
  /** Embedder actually used ('hash' if MiniLM was requested but unavailable). */
  embedder: RouterEmbedderKind;
  embedderReason?: string;
  /** routing.method of the underlying route ('semantic-native' | 'semantic-pure-js' | 'keyword'). */
  method: string;
}

/**
 * INTERNAL / BENCH-ONLY (ADR-391). Not a public API; may change without notice.
 *
 * Routes `task` through the same local path `hooks_route` uses (semantic index +
 * keyword fallback) with an explicit embedder, without the MCP layer. It skips
 * the two steps that are neither ADR-391 candidate A nor B: the AgentDB
 * pre-route (`bridgeRouteTask`, which answers first when its confidence > 0.5)
 * and the opt-in typesafe wrapper (CLAUDE_FLOW_ROUTER_TYPESAFE). With those
 * inactive, `primaryAgent` equals `hooks_route`'s `primaryAgent.type`.
 *
 * Switching `embedder` between calls rebuilds the index; benchmark in blocks.
 */
export async function routeTaskForBench(
  task: string,
  opts: { embedder: RouterEmbedderKind; context?: string },
): Promise<BenchRouteResult> {
  const r = await routeTaskLocal(task, opts.context, true, opts.embedder);
  return {
    primaryAgent: r.primaryAgent.type,
    confidence: r.primaryAgent.confidence,
    pattern: r.matchedPattern,
    embedder: r.embedder,
    ...(r.embedderReason ? { embedderReason: r.embedderReason } : {}),
    method: String((r.routing as { method?: unknown } | undefined)?.method ?? ''),
  };
}

export const hooksMetrics: MCPTool = {
  name: 'hooks_metrics',
  description: 'View learning metrics dashboard Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      period: { type: 'string', description: 'Metrics period (1h, 24h, 7d, 30d)' },
      includeV3: { type: 'boolean', description: 'Include V3 performance metrics' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const period = (params.period as string) || '24h';

    // ADR-093 F1: read from the same trajectory/pattern store that
    // hooks_post-task and hooks_intelligence_stats write to. Previously
    // this handler key-substring-filtered the memory store for "pattern",
    // "route", "task" — none of which match the trajectory keys that
    // post-task actually writes — so counters stayed at 0 forever (#1686).
    const stats = getIntelligenceStatsFromMemory();

    // Routing outcomes are persisted to a separate file (loadRoutingOutcomes)
    // by post-task; surface them so the dashboard sees command counters too.
    let routingOutcomes: Array<{ success: boolean; agent?: string }> = [];
    try {
      routingOutcomes = loadRoutingOutcomes() as Array<{ success: boolean; agent?: string }>;
    } catch { /* non-fatal */ }

    const totalCommands = routingOutcomes.length;
    const successfulCommands = routingOutcomes.filter(o => o.success).length;
    const successRate = totalCommands > 0 ? successfulCommands / totalCommands : null;

    // Compute top agent from routing outcomes
    const agentCounts: Record<string, number> = {};
    for (const o of routingOutcomes) {
      if (o.agent) agentCounts[o.agent] = (agentCounts[o.agent] || 0) + 1;
    }
    const topAgent = Object.entries(agentCounts).sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;

    return {
      _real: true,
      _dataSource: 'intelligence-stats + routing-outcomes',
      period,
      patterns: {
        total: stats.patterns.learned,
        successful: stats.patterns.successful,
        failed: stats.patterns.failed,
        unknown: stats.patterns.unknown,
        described: stats.patterns.described,
        descriptionCoverage: stats.patterns.learned > 0
          ? stats.patterns.described / stats.patterns.learned
          : null,
        avgConfidence: stats.routing.avgConfidence || null,
      },
      agents: {
        // Confidence is a model self-score, not observed routing accuracy
        // (#2809). Keep the old key as an explicit null for compatibility
        // while exposing the truthful additive field.
        routingAccuracy: null,
        averageConfidence: stats.routing.avgConfidence || null,
        outcomeSuccessRate: successRate,
        totalRoutes: stats.routing.decisions,
        topAgent,
      },
      commands: {
        totalExecuted: totalCommands,
        successRate,
        avgRiskScore: null,
      },
      _note: stats.patterns.learned === 0 && totalCommands === 0
        ? 'No metrics data collected yet. Run hooks_post-task / hooks_intelligence_trajectory-end / hooks_route to populate.'
        : undefined,
      lastUpdated: new Date().toISOString(),
    };
  },
};

export const hooksList: MCPTool = {
  name: 'hooks_list',
  description: 'List all registered hooks Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {},
  },
  handler: async () => {
    return {
      hooks: [
        // Core hooks
        { name: 'pre-edit', type: 'PreToolUse', status: 'active' },
        { name: 'post-edit', type: 'PostToolUse', status: 'active' },
        { name: 'pre-command', type: 'PreToolUse', status: 'active' },
        { name: 'post-command', type: 'PostToolUse', status: 'active' },
        { name: 'pre-task', type: 'PreToolUse', status: 'active' },
        { name: 'post-task', type: 'PostToolUse', status: 'active' },
        // Routing hooks
        { name: 'route', type: 'intelligence', status: 'active' },
        { name: 'explain', type: 'intelligence', status: 'active' },
        // Session hooks
        { name: 'session-start', type: 'SessionStart', status: 'active' },
        { name: 'session-end', type: 'SessionEnd', status: 'active' },
        { name: 'session-restore', type: 'SessionStart', status: 'active' },
        // Learning hooks
        { name: 'pretrain', type: 'intelligence', status: 'active' },
        { name: 'build-agents', type: 'intelligence', status: 'active' },
        { name: 'transfer', type: 'intelligence', status: 'active' },
        { name: 'metrics', type: 'analytics', status: 'active' },
        // System hooks
        { name: 'init', type: 'system', status: 'active' },
        { name: 'notify', type: 'coordination', status: 'active' },
        // Intelligence subcommands
        { name: 'intelligence', type: 'intelligence', status: 'active' },
        { name: 'intelligence_trajectory-start', type: 'intelligence', status: 'active' },
        { name: 'intelligence_trajectory-step', type: 'intelligence', status: 'active' },
        { name: 'intelligence_trajectory-end', type: 'intelligence', status: 'active' },
        { name: 'intelligence_pattern-store', type: 'intelligence', status: 'active' },
        { name: 'intelligence_pattern-search', type: 'intelligence', status: 'active' },
        { name: 'intelligence_stats', type: 'analytics', status: 'active' },
        { name: 'intelligence_learn', type: 'intelligence', status: 'active' },
        { name: 'intelligence_attention', type: 'intelligence', status: 'active' },
      ],
      total: 26,
    };
  },
};

export const hooksPreTask: MCPTool = {
  name: 'hooks_pre-task',
  description: 'Record task start and get agent suggestions with intelligent model routing (ADR-026) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      taskId: { type: 'string', description: 'Task identifier' },
      description: { type: 'string', description: 'Task description' },
      filePath: { type: 'string', description: 'Optional file path for AST analysis' },
    },
    required: ['taskId', 'description'],
  },
  handler: async (params: Record<string, unknown>) => {
    const taskId = params.taskId as string;
    const description = params.description as string;
    const filePath = params.filePath as string | undefined;

    { const v = validateIdentifier(taskId, 'taskId'); if (!v.valid) return { success: false, error: v.error }; }
    { const v = validateText(description, 'description'); if (!v.valid) return { success: false, error: v.error }; }
    if (filePath) { const v = validatePath(filePath, 'filePath'); if (!v.valid) return { success: false, error: v.error }; }

    const suggestion = suggestAgentsForTask(description);

    // Determine complexity
    const descLower = description.toLowerCase();
    const complexity: 'low' | 'medium' | 'high' = descLower.includes('complex') || descLower.includes('architecture') || description.length > 200
      ? 'high'
      : descLower.includes('simple') || descLower.includes('fix') || description.length < 50
        ? 'low'
        : 'medium';

    // Enhanced model routing with deterministic Tier-1 codemods (ADR-026, ADR-143)
    let modelRouting: Record<string, unknown> | undefined;
    try {
      const { getEnhancedModelRouter } = await import('../ruvector/enhanced-model-router.js');
      const router = getEnhancedModelRouter();
      const routeResult = await router.route(description, { filePath });

      if (routeResult.tier === 1) {
        // Deterministic codemod can apply this edit with $0 / no LLM (ADR-143)
        const intentType = routeResult.codemodIntent?.type ?? routeResult.agentBoosterIntent?.type;
        modelRouting = {
          tier: 1,
          handler: 'codemod',
          canSkipLLM: true,
          deterministic: true,
          codemodIntent: intentType,
          intentDescription: routeResult.codemodIntent?.description ?? routeResult.agentBoosterIntent?.description,
          confidence: routeResult.confidence,
          estimatedLatencyMs: routeResult.estimatedLatencyMs,
          estimatedCost: routeResult.estimatedCost,
          recommendation: `[CODEMOD_AVAILABLE] Skip LLM — call hooks_codemod with intent="${intentType}" (deterministic, $0)`,
        };
      } else {
        // LLM required
        modelRouting = {
          tier: routeResult.tier,
          handler: routeResult.handler,
          model: routeResult.model,
          complexity: routeResult.complexity,
          confidence: routeResult.confidence,
          estimatedLatencyMs: routeResult.estimatedLatencyMs,
          estimatedCost: routeResult.estimatedCost,
          recommendation: `[TASK_MODEL_RECOMMENDATION] Use model="${routeResult.model}" for this task`,
        };
      }
    } catch {
      // Enhanced router not available
    }

    return {
      taskId,
      description,
      agentsMatched: suggestion.matched,
      suggestedAgents: suggestion.agents.map((agent, i) => ({
        type: agent,
        confidence: suggestion.confidence - (0.05 * i),
        reason: !suggestion.matched
          ? 'Default suggestion (nothing matched)'
          : i === 0
            ? `Primary agent for ${agent} tasks based on learned patterns`
            : `Alternative agent with ${agent} capabilities`,
      })),
      complexity,
      estimatedDuration: complexity === 'high' ? '2-4 hours' : complexity === 'medium' ? '30-60 min' : '10-30 min',
      risks: complexity === 'high' ? ['Complex task may require multiple iterations'] : [],
      recommendations: [
        `Use ${suggestion.agents[0]} as primary agent`,
        suggestion.agents.length > 2 ? 'Consider using swarm coordination' : 'Single agent recommended',
      ],
      modelRouting,
      timestamp: new Date().toISOString(),
    };
  },
};

export const hooksPostTask: MCPTool = {
  name: 'hooks_post-task',
  description: 'Record task completion for learning Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      taskId: { type: 'string', description: 'Task identifier' },
      patterns: feedbackPatternsSchema,
      success: { type: 'boolean', description: 'Whether task was successful' },
      agent: { type: 'string', description: 'Agent that completed the task' },
      quality: { type: 'number', minimum: 0, maximum: 1, description: 'Quality score (0-1)' },
      task: { type: 'string', description: 'Task description text (used for learning keyword extraction)' },
      duration: { type: 'number', description: 'Observed task duration in milliseconds (used by pheromone-adaptive topology)' },
      latencyBudgetMs: { type: 'number', description: 'Latency budget used to normalize duration (default 60000ms)' },
      consensusAlignment: { type: 'number', description: 'Agreement with accepted swarm consensus in [0,1] (default quality)' },
      agentRole: { type: 'string', description: 'Role for role-local pheromone normalization (default agent)' },
      storeDecisions: { type: 'boolean', description: 'Also store routing decision in memory DB' },
      // ADR-147 P2: nested-subagent spawn-tree capture
      parentAgentId: { type: 'string', description: 'ID of the parent agent (from Claude Code\'s parent_agent_id OTel span tag / x-claude-code-parent-agent-id header). Omit for top-level work.' },
      depth: { type: 'number', description: 'Chain depth from root lead session (0 = lead, 1+ = subagent). Used by ADR-147 P3 depth-aware guardrail.' },
    },
    required: ['taskId'],
  },
  handler: async (params: Record<string, unknown>) => {
    const taskId = params.taskId as string;
    const success = params.success !== false;
    const agent = params.agent as string | undefined;
    if (params.quality !== undefined && (typeof params.quality !== 'number'
      || !Number.isFinite(params.quality) || params.quality < 0 || params.quality > 1)) {
      return { success: false, error: 'quality must be a finite number between 0 and 1' };
    }
    const quality = params.quality === undefined ? (success ? 0.85 : 0.3) : params.quality as number;
    const startTime = Date.now();
    let patterns: string[] | undefined;
    try { patterns = validateFeedbackPatterns(params.patterns); }
    catch (error) { return { success: false, error: (error as Error).message }; }

    { const v = validateIdentifier(taskId, 'taskId'); if (!v.valid) return { success: false, error: v.error }; }
    if (agent) { const v = validateIdentifier(agent, 'agent'); if (!v.valid) return { success: false, error: v.error }; }

    // ADR-147 P2: validate spawn-tree lineage if provided
    const parentAgentId = params.parentAgentId as string | undefined;
    if (parentAgentId !== undefined) {
      const v = validateIdentifier(parentAgentId, 'parentAgentId');
      if (!v.valid) return { success: false, error: v.error };
    }
    const depthRaw = params.depth;
    let depth: number | undefined;
    if (depthRaw !== undefined && depthRaw !== null) {
      const n = Number(depthRaw);
      if (!Number.isInteger(n) || n < 0 || n > 32) {
        return { success: false, error: 'depth must be a non-negative integer ≤ 32' };
      }
      depth = n;
    }

    // Phase 3: Wire recordFeedback through bridge → LearningSystem + ReasoningBank
    let feedbackResult: { success: boolean; controller: string; updated: number } | null = null;
    try {
      const bridge = await import('../memory/memory-bridge.js');
      feedbackResult = await bridge.bridgeRecordFeedback({
        taskId,
        task: (params.task as string) || undefined,
        success,
        quality,
        agent,
        duration: (params.duration as number) || undefined,
        patterns,
        // ADR-147 P2: forward spawn-tree lineage so it lands in feedback + memory
        parentAgentId,
        depth,
      });
    } catch {
      // Bridge not available — continue with basic response
    }

    // Phase 3: Record causal edge (task → outcome)
    try {
      const bridge = await import('../memory/memory-bridge.js');
      await bridge.bridgeRecordCausalEdge({
        sourceId: taskId,
        targetId: `outcome-${taskId}`,
        relation: success ? 'succeeded' : 'failed',
        weight: quality,
      });
    } catch {
      // Non-fatal
    }

    // Record trajectory via intelligence module (SONA + ReasoningBank).
    // #3353: keep the observed result instead of discarding it.
    let trajectoryRecorded = false;
    try {
      const intelligence = await import('../memory/intelligence.js');
      trajectoryRecorded = (await intelligence.recordTrajectory(
        [{ type: 'result' as const, content: (params.task as string) || taskId, metadata: { success, agent, quality }, timestamp: Date.now() }],
        success ? 'success' : 'failure'
      )) === true;
    } catch {
      // Intelligence module not available — non-fatal
    }

    // ADR-130 Phase 3: "reinforced-by" edge on task success.
    // Writes: context node → task pattern node (relation: "reinforced-by")
    //
    // #2961 — this used to be fire-and-forget (no `await` on the IIFE). That
    // was invisible from the long-running MCP server, where the process
    // stays alive long enough for the detached write to finish in the
    // background. But `hooks post-task` (CLI) is the exact same handler
    // invoked from a one-shot process that calls `process.exit(0)`
    // immediately after this action resolves (bin/cli.js, #1552) — the
    // still-pending dynamic `import(...)` + `insertGraphEdge()` call was
    // killed before its first tick, so the CLI form dropped this edge on
    // every single success, not intermittently. Await it — this is a
    // single fast DB write, not worth losing correctness on one call site
    // to save latency on the other.
    if (success) {
      try {
        const { insertGraphEdge } = await import('../memory/graph-edge-writer.js');
        const sessionCtxId = `task:${taskId}`;
        const patternId = `pattern:${taskId}`;
        await insertGraphEdge({
          sourceId: sessionCtxId,
          targetId: patternId,
          relation: 'reinforced-by',
          weight: quality,
          confidence: quality,
          lastReinforced: new Date().toISOString(),
          metadata: { success, agent, taskId },
        });
      } catch { /* non-fatal */ }
    }

    // Persist routing outcome for runtime learning (file-based, always reliable)
    const taskText = (params.task as string) || '';
    const outcomeKeywords = extractKeywords(taskText);
    let outcomePersisted = false;
    // #3064 — the previous ad-hoc regex `/^[a-zA-Z0-9_-]+$/` silently dropped
    // colon-namespaced plugin agents (`ruflo-core:reviewer`, etc.) — i.e. every
    // Claude Code plugin agent. Colons are already allowed by the canonical
    // validateIdentifier() check on line 1456 above (IDENTIFIER_RE includes ':'
    // and '.'), so no additional charset gating is needed here.
    if (taskText && agent) {
      try {
        const outcomes = loadRoutingOutcomes();
        outcomes.push({
          task: taskText,
          agent,
          success,
          quality,
          keywords: outcomeKeywords,
          timestamp: new Date().toISOString(),
        });
        saveRoutingOutcomes(outcomes);
        outcomePersisted = true;
      } catch { /* non-critical */ }
    }

    // Optionally store in memory DB for cross-session vector retrieval
    if (params.storeDecisions && taskText && agent) {
      try {
        const storeFn = await getRealStoreFunction();
        if (storeFn) {
          await storeFn({
            key: `routing-decision:${taskId}`,
            namespace: 'patterns',
            value: JSON.stringify({ task: taskText, agent, success, quality, keywords: outcomeKeywords }),
            tags: ['routing-decision'],
          });
        }
      } catch { /* non-critical */ }

      // #2786 fix-2 — also mirror into the JSON memory store so
      // `getIntelligenceStatsFromMemory()` (which reads .claude-flow/memory/store.json
      // synchronously) counts this routing decision in the hooks_metrics dashboard.
      // The AgentDB write above is authoritative for cross-session retrieval; this
      // is just the counter surface the sync reader depends on.
      try {
        const key = `routing-decision:${taskId}`;
        const store = loadMemoryStore();
        store.entries[key] = {
          key,
          value: JSON.stringify({ task: taskText, agent, success, quality, keywords: outcomeKeywords }),
          namespace: 'patterns',
          createdAt: new Date().toISOString(),
          metadata: { type: 'routing-decision', confidence: typeof quality === 'number' ? quality : undefined, agent, success },
        } as any;
        const memDir = resolve(MEMORY_DIR);
        if (!existsSync(memDir)) mkdirSync(memDir, { recursive: true });
        writeFileSync(getMemoryPath(), JSON.stringify(store, null, 2), 'utf-8');
      } catch { /* non-critical */ }
    }

    let pheromone: unknown;
    if (agent) {
      try {
        const { recordSwarmPheromoneSignal } = await import('./swarm-tools.js');
        const observedDuration = Math.max(0, Number(params.duration ?? (Date.now() - startTime)));
        const latencyBudgetMs = Math.max(1, Number(params.latencyBudgetMs ?? 60_000));
        pheromone = recordSwarmPheromoneSignal({
          agentId: agent,
          role: String(params.agentRole ?? agent),
          taskSuccess: success ? 1 : 0,
          normalizedLatency: Math.max(0, Math.min(1, observedDuration / latencyBudgetMs)),
          consensusAlignment: Math.max(0, Math.min(1, Number(params.consensusAlignment ?? quality))),
        });
      } catch { /* no active APSC swarm or optional path unavailable */ }
    }

    const duration = Date.now() - startTime;

    // Persist to auto-memory-store for statusline visibility
    try {
      const dataDir = join(getProjectCwd(), '.claude-flow', 'data');
      if (!existsSync(dataDir)) mkdirSync(dataDir, { recursive: true });
      const storePath = join(dataDir, 'auto-memory-store.json');
      let store: Array<Record<string, unknown>> = [];
      try {
        if (existsSync(storePath)) {
          const parsed = JSON.parse(readFileSync(storePath, 'utf-8'));
          store = Array.isArray(parsed) ? parsed : [];
        }
      } catch { /* start fresh */ }
      store.push({
        id: `task-${taskId}`,
        key: taskId,
        content: `Task ${success ? 'completed' : 'failed'}: ${taskText || taskId}${agent ? ` (agent: ${agent})` : ''}`,
        namespace: 'tasks',
        type: 'task-outcome',
        metadata: { agent, success, quality },
        createdAt: Date.now(),
      });
      writeFileSync(storePath, JSON.stringify(store, null, 2), 'utf-8');
    } catch { /* non-critical */ }

    // #3353: report only observed learning results. The previous
    // `feedbackResult?.updated || (success ? 2 : 1)` / `newPatterns: success ? 1 : 0`
    // invented counts whenever the feedback controller was unavailable (and the
    // `||` turned an observed 0 into 2). No path reports pattern *creation*, so
    // newPatterns is null (unknown) rather than a guess; the trajectory has no
    // real id to surface, so trajectoryId is null.
    const feedbackRecorded = feedbackResult?.success === true;
    const learningAvailable = feedbackRecorded;
    return {
      taskId,
      success,
      duration,
      learningUpdates: {
        patternsUpdated: feedbackRecorded ? (feedbackResult?.updated ?? 0) : 0,
        newPatterns: null as number | null,
        trajectoryId: null as string | null,
        controller: feedbackResult?.controller || 'none',
        outcomePersisted,
        available: learningAvailable,
        ...(learningAvailable ? {} : {
          reason: feedbackResult
            ? `feedback controller '${feedbackResult.controller}' did not record the outcome`
            : 'feedback controller unavailable',
        }),
      },
      trajectory: { recorded: trajectoryRecorded },
      quality,
      pheromone,
      feedback: feedbackResult ? {
        recorded: feedbackResult.success,
        controller: feedbackResult.controller,
        updates: feedbackResult.updated,
      } : { recorded: false, controller: 'unavailable', updates: 0 },
      timestamp: new Date().toISOString(),
    };
  },
};

// Explain hook - transparent routing explanation
export const hooksExplain: MCPTool = {
  name: 'hooks_explain',
  description: 'Explain routing decision with full transparency Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'Task description' },
      agent: { type: 'string', description: 'Specific agent to explain' },
      verbose: { type: 'boolean', description: 'Verbose explanation' },
    },
    required: ['task'],
  },
  handler: async (params: Record<string, unknown>) => {
    const task = params.task as string;

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }

    const suggestion = suggestAgentsForTask(task);
    const taskLower = task.toLowerCase();

    // Determine matched patterns
    const matchedPatterns: Array<{ pattern: string; matchScore: number; examples: string[] }> = [];
    for (const [pattern, _result] of Object.entries(TASK_PATTERNS)) {
      if (taskLower.includes(pattern)) {
        matchedPatterns.push({
          pattern,
          matchScore: pattern.length / Math.max(taskLower.length, 1), // real ratio: pattern length vs task length
          examples: [`Keyword "${pattern}" matched in task description`],
        });
      }
    }

    // Calculate real historical success rate from routing outcomes file
    let historicalSuccess: number | null = null;
    let historicalNote = 'No historical data yet';
    try {
      const outcomesPath = join(resolve('.'), '.claude-flow/routing-outcomes.json');
      if (existsSync(outcomesPath)) {
        const data = JSON.parse(readFileSync(outcomesPath, 'utf-8'));
        const outcomes: Array<{ success: boolean }> = data.outcomes || [];
        if (outcomes.length > 0) {
          historicalSuccess = outcomes.filter(o => o.success).length / outcomes.length;
          historicalNote = `Calculated from ${outcomes.length} recorded outcomes`;
        }
      }
    } catch {
      // File unreadable; leave as null
    }

    // #3567: when nothing matched, say so instead of explaining a match that did not happen.
    if (!suggestion.matched) {
      return {
        task,
        matched: false,
        reason: 'no-match-default',
        explanation: `No keyword or learned pattern matched this task. ${suggestion.note ?? ''} ` +
          `"${suggestion.agents[0]}" is a default suggestion, not a routing decision.`.trim(),
        factors: [
          { factor: 'Keyword Match', weight: 0.4, value: null, impact: 'No keyword matched' },
          { factor: 'Historical Success', weight: 0.3, value: historicalSuccess, impact: historicalNote },
          { factor: 'Agent Availability', weight: 0.2, value: null, impact: 'Agent availability tracking not implemented' },
          { factor: 'Task Complexity', weight: 0.1, value: task.length > 100 ? 0.8 : 0.3, impact: 'Complexity assessment' },
        ],
        patterns: matchedPatterns,
        decision: {
          agent: suggestion.agents[0],
          confidence: suggestion.confidence,
          reasoning: [
            'No pattern matched; the agent is a default suggestion',
            `Confidence ${(suggestion.confidence * 100).toFixed(0)}% is below every real match`,
          ],
        },
      };
    }

    return {
      task,
      matched: true,
      explanation: `The routing decision was made based on keyword analysis of the task description. ` +
        `The task contains keywords that match the "${suggestion.agents[0]}" specialization with ${(suggestion.confidence * 100).toFixed(0)}% confidence.`,
      factors: [
        { factor: 'Keyword Match', weight: 0.4, value: suggestion.confidence, impact: 'Primary routing signal' },
        { factor: 'Historical Success', weight: 0.3, value: historicalSuccess, impact: historicalNote },
        { factor: 'Agent Availability', weight: 0.2, value: null, impact: 'Agent availability tracking not implemented' },
        { factor: 'Task Complexity', weight: 0.1, value: task.length > 100 ? 0.8 : 0.3, impact: 'Complexity assessment' },
      ],
      patterns: matchedPatterns.length > 0 ? matchedPatterns : [
        { pattern: 'general-task', matchScore: 0.7, examples: ['Default pattern for unclassified tasks'] }
      ],
      decision: {
        agent: suggestion.agents[0],
        confidence: suggestion.confidence,
        reasoning: [
          `Task analysis identified ${matchedPatterns.length || 1} relevant patterns`,
          `"${suggestion.agents[0]}" has highest capability match for this task type`,
          historicalSuccess !== null
            ? `Historical success rate for similar tasks: ${(historicalSuccess * 100).toFixed(0)}%`
            : `No historical outcome data available yet`,
          `Confidence threshold met (${(suggestion.confidence * 100).toFixed(0)}% >= 70%)`,
        ],
      },
    };
  },
};

// Pretrain hook - repository analysis for intelligence bootstrap
export const hooksPretrain: MCPTool = {
  name: 'hooks_pretrain',
  description: 'Analyze repository to bootstrap intelligence (4-step pipeline) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      path: { type: 'string', description: 'Repository path' },
      depth: { type: 'string', description: 'Analysis depth (shallow, medium, deep)' },
      skipCache: { type: 'boolean', description: 'Skip cached analysis' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const repoPath = resolve((params.path as string) || '.');
    const depth = (params.depth as string) || 'medium';
    const startTime = performance.now();

    // Real file scanning — count files by extension, extract patterns.
    // (readdirSync/statSync already imported statically at the top.)
    const extCounts: Record<string, number> = {};
    let filesAnalyzed = 0;
    // #1953: separate budget for code files. The old code gated the
    // import-pattern extraction on `filesAnalyzed <= 50`, which counts
    // EVERY directory entry (including .md/.yaml/.db/.log). In any
    // markdown/docs-heavy repo, the depth-first walker burned through the
    // 50-file budget on non-code files before reaching any source — so
    // `patternsExtracted: 0` even when hundreds of `.ts`/`.js` files existed.
    let codeFilesScanned = 0;
    let totalLines = 0;
    const maxDepth = depth === 'shallow' ? 2 : depth === 'deep' ? 6 : 4;
    const patterns: string[] = [];

    // #1953: recurse into directories that typically contain code first
    // (`src/`, `apps/`, `packages/`, `lib/`, `crates/`, `workers/`, `server/`)
    // before docs / specs / planning dirs, so the import-extraction budget
    // is spent on the highest-signal directories even in mixed repos.
    const CODE_DIR_PREFIXES = new Set([
      'src', 'apps', 'packages', 'lib', 'crates', 'workers',
      'server', 'backend', 'frontend', 'app', 'cli', 'core',
    ]);
    const scoreEntry = (name: string): number => {
      if (CODE_DIR_PREFIXES.has(name)) return 0;
      // Deprioritise common docs / output directories.
      if (/^(docs?|specs?|_.*|examples?|samples?|out|build|target|coverage|tests?)$/.test(name)) return 2;
      return 1;
    };

    const scan = (dir: string, currentDepth: number) => {
      if (currentDepth > maxDepth) return;
      try {
        const entries = readdirSync(dir, { withFileTypes: true });
        // Sort: code-likely dirs first, files mixed in by name, deprioritised
        // dirs last. Stable for deterministic test behaviour.
        entries.sort((a, b) => {
          const sa = a.isDirectory() ? scoreEntry(a.name) : 1;
          const sb = b.isDirectory() ? scoreEntry(b.name) : 1;
          return sa - sb;
        });
        for (const entry of entries) {
          if (entry.name.startsWith('.') || entry.name === 'node_modules' || entry.name === 'dist') continue;
          const full = join(dir, entry.name);
          if (entry.isDirectory()) {
            scan(full, currentDepth + 1);
          } else if (entry.isFile()) {
            const ext = entry.name.includes('.') ? entry.name.slice(entry.name.lastIndexOf('.')) : '';
            if (ext) extCounts[ext] = (extCounts[ext] || 0) + 1;
            filesAnalyzed++;
            // For code files, count lines and extract imports
            if (['.ts', '.js', '.tsx', '.jsx', '.mjs', '.cjs', '.py', '.go', '.rs', '.java'].includes(ext)) {
              try {
                const content = readFileSync(full, 'utf-8');
                const lines = content.split('\n');
                totalLines += lines.length;
                // #1953: gate on the code-file count, not every-file count.
                // Also widened the per-file scan window from 30 → 80 lines:
                // modern TS files often have license headers + JSDoc + type
                // imports before the first `import` statement.
                if (++codeFilesScanned <= 50) {
                  for (const line of lines.slice(0, 80)) {
                    if (line.startsWith('import ') || line.startsWith('from ') || (line.startsWith('const ') && line.includes('require('))) {
                      const trimmed = line.trim();
                      if (trimmed.length < 120 && !patterns.includes(trimmed)) patterns.push(trimmed);
                      if (patterns.length >= 100) break;
                    }
                  }
                }
              } catch { /* skip unreadable */ }
            }
          }
        }
      } catch { /* skip inaccessible dirs */ }
    };

    scan(repoPath, 0);
    const elapsed = Math.round(performance.now() - startTime);

    // Persist extracted patterns. Two stores get written so the user can find
    // them where they expect:
    //   1. memory-bridge `pretrain` namespace — one summary bundle
    //   2. neural store — one row PER pattern so `neural_patterns list` reflects them
    // #2245 — without (2), the dashboards reported "0 patterns" after pretrain.
    let patternsBundled = 0;
    let patternsIndexed = 0;
    try {
      const bridge = await import('../memory/memory-bridge.js');
      await bridge.bridgeStoreEntry({
        key: `pretrain-${Date.now()}`,
        value: JSON.stringify({ filesAnalyzed, totalLines, topExtensions: Object.entries(extCounts).sort((a, b) => b[1] - a[1]).slice(0, 10), importPatterns: patterns.slice(0, 20) }),
        namespace: 'pretrain',
        tags: ['pretrain', depth],
      });
      patternsBundled = patterns.length;
    } catch { /* AgentDB not available */ }

    try {
      const neural = await import('./neural-tools.js');
      const items = patterns.map((p) => ({
        name: p.length > 200 ? p.slice(0, 200) : p,
        type: 'import-pattern',
        content: p,
        metadata: { source: 'hooks_pretrain', depth },
      }));
      const result = await neural.storeNeuralPatterns(items);
      patternsIndexed = result.stored;
    } catch { /* neural store unavailable */ }

    // Back-compat field
    const patternsStored = patternsBundled;

    // #1847: when the corpus contains files but no patterns were extracted
    // (typical for Markdown vaults), make the source-code-only extraction
    // contract explicit so users don't conclude the hook system is broken.
    const SUPPORTED_EXTRACTION_EXTS = ['.ts', '.js', '.tsx', '.jsx', '.mjs', '.cjs', '.py', '.go', '.rs', '.java'];
    let note: string | undefined;
    if (filesAnalyzed > 0 && patterns.length === 0) {
      const codeFileCount = SUPPORTED_EXTRACTION_EXTS.reduce(
        (sum, ext) => sum + (extCounts[ext] ?? 0),
        0,
      );
      note = codeFileCount === 0
        ? `No source-code patterns found. hooks_pretrain extracts import/require lines from ${SUPPORTED_EXTRACTION_EXTS.join('/')} files only — Markdown/text/asset corpora produce zero patterns by design. This is not a hook-system failure; live trajectories and statusline are independent.`
        : `Found ${codeFileCount} source-code file(s) but extracted zero import/require patterns. They may be empty, generated, or use non-standard module syntax.`;
    }

    return {
      success: true,
      _real: true,
      path: repoPath,
      depth,
      durationMs: elapsed,
      stats: {
        filesAnalyzed,
        totalLines,
        patternsExtracted: patterns.length,
        patternsBundled,                  // #2245: 1 summary row in memory-bridge `pretrain` namespace
        patternsIndexed,                  // #2245: per-pattern rows in neural store — surfaced by neural_patterns list
        patternsStored,                   // back-compat alias for patternsBundled
        fileTypes: Object.entries(extCounts).sort((a, b) => b[1] - a[1]).slice(0, 15).map(([ext, count]) => ({ ext, count })),
        // #1847: explicit extraction contract so callers can tell pretrain
        // patterns apart from live trajectories and hook statusline state.
        // #2245: also call out exactly which stores got written.
        sources: {
          extractedFrom: SUPPORTED_EXTRACTION_EXTS,
          scope: 'pretrain-only (live trajectories + statusline are tracked separately)',
          stores: {
            'memory-bridge:pretrain': patternsBundled > 0 ? 1 : 0, // one bundle row
            'neural-store (neural_patterns list)': patternsIndexed,
          },
        },
      },
      ...(note ? { note } : {}),
    };
  },
};

// Build agents hook - generate optimized agent configs
export const hooksBuildAgents: MCPTool = {
  name: 'hooks_build-agents',
  description: 'Generate optimized agent configurations from pretrain data Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      outputDir: { type: 'string', description: 'Output directory for configs' },
      focus: { type: 'string', description: 'Focus area (v3-implementation, security, performance, all)' },
      format: { type: 'string', description: 'Config format (yaml, json)' },
      persist: { type: 'boolean', description: 'Write configs to disk' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    // #4558: outputDir was never validated and resolved against the process
    // cwd, so a default call from another directory overwrote files there.
    if (params.outputDir !== undefined) {
      const v = validatePath(params.outputDir, 'outputDir');
      if (!v.valid) return { success: false, error: v.error };
    }
    const outputDir = resolve(getProjectCwd(), (params.outputDir as string) || './agents');
    const focus = (params.focus as string) || 'all';
    const format = (params.format as string) || 'yaml';
    const persist = params.persist !== false; // Default to true

    const agents = [
      { type: 'coder', configFile: join(outputDir, `coder.${format}`), capabilities: ['code-generation', 'refactoring', 'debugging'], optimizations: ['flash-attention', 'token-reduction'] },
      { type: 'architect', configFile: join(outputDir, `architect.${format}`), capabilities: ['system-design', 'api-design', 'documentation'], optimizations: ['context-caching', 'memory-persistence'] },
      { type: 'tester', configFile: join(outputDir, `tester.${format}`), capabilities: ['unit-testing', 'integration-testing', 'coverage'], optimizations: ['parallel-execution'] },
      { type: 'security-architect', configFile: join(outputDir, `security-architect.${format}`), capabilities: ['threat-modeling', 'vulnerability-analysis', 'security-review'], optimizations: ['pattern-matching'] },
      { type: 'reviewer', configFile: join(outputDir, `reviewer.${format}`), capabilities: ['code-review', 'quality-analysis', 'best-practices'], optimizations: ['incremental-analysis'] },
    ];

    const filteredAgents = focus === 'all' ? agents :
      focus === 'security' ? agents.filter(a => a.type.includes('security') || a.type === 'reviewer') :
      focus === 'performance' ? agents.filter(a => ['coder', 'tester'].includes(a.type)) :
      agents;

    // Persist configs to disk if requested
    if (persist) {
      // Ensure output directory exists
      if (!existsSync(outputDir)) {
        mkdirSync(outputDir, { recursive: true });
      }

      // Write each agent config
      for (const agent of filteredAgents) {
        const config = {
          type: agent.type,
          capabilities: agent.capabilities,
          optimizations: agent.optimizations,
          version: '3.0.0',
          createdAt: new Date().toISOString(),
        };

        const content = format === 'json'
          ? JSON.stringify(config, null, 2)
          : `# ${agent.type} agent configuration\ntype: ${agent.type}\nversion: "3.0.0"\ncapabilities:\n${agent.capabilities.map(c => `  - ${c}`).join('\n')}\noptimizations:\n${agent.optimizations.map(o => `  - ${o}`).join('\n')}\ncreatedAt: "${config.createdAt}"\n`;

        writeFileSync(agent.configFile, content, 'utf-8');
      }
    }

    return {
      outputDir,
      focus,
      persisted: persist,
      agents: filteredAgents,
      stats: {
        configsGenerated: filteredAgents.length,
        // #4558: was agents * 3; no pretrain data is read, the configs are templates.
        patternsApplied: 0,
        optimizationsIncluded: filteredAgents.reduce((acc, a) => acc + a.optimizations.length, 0),
      },
      _note: 'Configs are built-in templates; pretrain data is not read.',
    };
  },
};

// Transfer hook - transfer patterns from another project
export const hooksTransfer: MCPTool = {
  name: 'hooks_transfer',
  description: 'Transfer learned patterns from another project Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      sourcePath: { type: 'string', description: 'Source project path' },
      filter: { type: 'string', description: 'Filter patterns by type' },
      minConfidence: { type: 'number', description: 'Minimum confidence threshold' },
    },
    required: ['sourcePath'],
  },
  handler: async (params: Record<string, unknown>) => {
    const sourcePath = params.sourcePath as string;
    const minConfidence = (params.minConfidence as number) || 0.7;
    const filter = params.filter as string;

    { const v = validatePath(sourcePath, 'sourcePath'); if (!v.valid) return { success: false, error: v.error }; }
    if (filter) { const v = validateIdentifier(filter, 'filter'); if (!v.valid) return { success: false, error: v.error }; }

    // Try to load patterns from source project's memory store
    const sourceMemoryPath = join(resolve(sourcePath), MEMORY_DIR, MEMORY_FILE);
    let sourceStore: MemoryStore = { entries: {}, version: '3.0.0' };

    try {
      if (existsSync(sourceMemoryPath)) {
        sourceStore = JSON.parse(readFileSync(sourceMemoryPath, 'utf-8'));
      }
    } catch {
      // Fall back to empty store
    }

    const classifyType = (key: string, metadata?: Record<string, unknown>): string | null => {
      if (key.includes('file') || metadata?.type === 'file-pattern') return 'file-patterns';
      if (key.includes('routing') || metadata?.type === 'routing') return 'task-routing';
      if (key.includes('command') || metadata?.type === 'command-risk') return 'command-risk';
      if (key.includes('agent') || metadata?.type === 'agent-success') return 'agent-success';
      return null;
    };

    const candidates = Object.entries(sourceStore.entries)
      .map(([key, entry]) => ({ key, entry, type: classifyType(key, entry.metadata) }))
      .filter((c): c is { key: string; entry: MemoryEntry; type: string } => c.type !== null)
      .filter(c => !filter || c.type.includes(filter));

    // If source has no patterns, report honestly instead of substituting demo data
    if (candidates.length === 0) {
      return {
        success: false,
        message: 'No patterns found in source project',
        sourcePath,
        transferred: 0,
      };
    }

    // #2859 — this used to count source patterns, then invent skip counts
    // as fixed percentages of that count and a fixed avgConfidence/avgAge,
    // without ever reading or writing the destination store. An operator
    // could believe state moved between projects when nothing changed.
    // Perform a real merge: skip entries below the confidence threshold,
    // skip exact duplicates, skip real conflicts (destination already has
    // a different value for that key — never silently overwritten), and
    // actually write whatever remains into this project's own memory store.
    const destStore = loadMemoryStore();
    const byType: Record<string, number> = {};
    let lowConfidence = 0;
    let duplicates = 0;
    let conflicts = 0;
    let transferredCount = 0;
    let confidenceSum = 0;
    let confidenceCount = 0;
    let ageSumMs = 0;
    let ageCount = 0;
    const now = Date.now();

    for (const { key, entry, type } of candidates) {
      const confidenceRaw = entry.metadata?.confidence ?? entry.metadata?.reliability;
      const confidence = typeof confidenceRaw === 'number' ? confidenceRaw : undefined;
      if (confidence !== undefined && confidence < minConfidence) {
        lowConfidence++;
        continue;
      }

      const existing = destStore.entries[key];
      if (existing) {
        const same = JSON.stringify(existing.value) === JSON.stringify(entry.value);
        if (same) { duplicates++; continue; }
        conflicts++;
        continue;
      }

      destStore.entries[key] = entry;
      transferredCount++;
      byType[type] = (byType[type] ?? 0) + 1;
      if (confidence !== undefined) { confidenceSum += confidence; confidenceCount++; }
      const storedAtMs = Date.parse(entry.storedAt ?? '');
      if (!Number.isNaN(storedAtMs)) { ageSumMs += now - storedAtMs; ageCount++; }
    }

    if (transferredCount > 0) {
      const memDir = resolve(MEMORY_DIR);
      if (!existsSync(memDir)) mkdirSync(memDir, { recursive: true });
      writeFileSync(getMemoryPath(), JSON.stringify(destStore, null, 2), 'utf-8');
    }

    return {
      success: true,
      sourcePath,
      transferred: {
        total: transferredCount,
        byType,
      },
      skipped: {
        lowConfidence,
        duplicates,
        conflicts,
      },
      stats: {
        avgConfidence: confidenceCount > 0 ? Number((confidenceSum / confidenceCount).toFixed(3)) : null,
        avgAgeDays: ageCount > 0 ? Number((ageSumMs / ageCount / 86_400_000).toFixed(1)) : null,
      },
      dataSource: 'source-project',
    };
  },
};

// Session start hook - auto-starts daemon
export const hooksSessionStart: MCPTool = {
  name: 'hooks_session-start',
  description: 'Initialize a new session and auto-start daemon Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      sessionId: { type: 'string', description: 'Optional session ID' },
      restoreLatest: { type: 'boolean', description: 'Restore latest session state' },
      startDaemon: { type: 'boolean', description: 'Start worker daemon (default: false — opt-in to prevent unintended token usage)' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const sessionId = (params.sessionId as string) || `session-${Date.now()}`;
    const restoreLatest = params.restoreLatest as boolean;
    const shouldStartDaemon = params.startDaemon === true;

    if (params.sessionId) { const v = validateIdentifier(params.sessionId as string, 'sessionId'); if (!v.valid) return { success: false, error: v.error }; }

    // Auto-regenerate statusline if outdated (fixes older installs)
    // Checks for the old fake heuristic: "Math.floor(sizeKB / 2)"
    try {
      const statuslinePath = join(getProjectCwd(), '.claude', 'helpers', 'statusline.cjs');
      if (existsSync(statuslinePath)) {
        const content = readFileSync(statuslinePath, 'utf-8');
        if (content.includes('Math.floor(sizeKB / 2)') || content.includes('Maturity fallback')) {
          // Old version detected — regenerate from current generator
          const { generateStatuslineScript } = await import('../init/statusline-generator.js');
          const newContent = generateStatuslineScript({
            runtime: { maxAgents: 15, topology: 'hierarchical', strategy: 'specialized' },
          } as any);
          writeFileSync(statuslinePath, newContent, 'utf-8');
        }
      }
    } catch {
      // Non-critical — old statusline continues to work, just with stale heuristics
    }

    // Auto-start daemon if enabled
    let daemonStatus: { started: boolean; pid?: number; error?: string } = { started: false };
    if (shouldStartDaemon) {
      try {
        // Dynamic import to avoid circular dependencies
        const { startDaemon } = await import('../services/worker-daemon.js');
        const daemon = await startDaemon(getProjectCwd());
        const status = daemon.getStatus();
        daemonStatus = {
          started: true,
          pid: status.pid,
        };
      } catch (error) {
        daemonStatus = {
          started: false,
          error: error instanceof Error ? error.message : String(error),
        };
      }
    }

    // Initialize intelligence module (SONA + local ReasoningBank)
    let intelligenceStatus: { sonaEnabled: boolean; reasoningBankEnabled: boolean } = { sonaEnabled: false, reasoningBankEnabled: false };
    try {
      const intelligence = await import('../memory/intelligence.js');
      const initResult = await intelligence.initializeIntelligence();
      intelligenceStatus = { sonaEnabled: initResult.sonaEnabled, reasoningBankEnabled: initResult.reasoningBankEnabled };
    } catch {
      // Intelligence module not available — non-fatal
    }

    // Phase 5: Wire ReflexionMemory session start via bridge
    let sessionMemory: { controller: string; restoredPatterns: number } | null = null;
    try {
      const bridge = await import('../memory/memory-bridge.js');
      const result = await bridge.bridgeSessionStart({
        sessionId,
        context: restoreLatest ? 'restore previous session patterns' : 'new session',
      });
      if (result) {
        sessionMemory = {
          controller: result.controller,
          restoredPatterns: result.restoredPatterns,
        };
      }
    } catch {
      // Bridge not available
    }

    // Persist session record to auto-memory-store for statusline visibility
    try {
      const dataDir = join(getProjectCwd(), '.claude-flow', 'data');
      if (!existsSync(dataDir)) mkdirSync(dataDir, { recursive: true });
      const storePath = join(dataDir, 'auto-memory-store.json');
      let store: Array<Record<string, unknown>> = [];
      try {
        if (existsSync(storePath)) {
          const raw = readFileSync(storePath, 'utf-8');
          const parsed = JSON.parse(raw);
          store = Array.isArray(parsed) ? parsed : [];
        }
      } catch { /* start fresh */ }
      // Add session entry (dedup by session ID)
      const entryId = `session-${sessionId}`;
      const existing = store.findIndex((e: Record<string, unknown>) => e.id === entryId);
      const entry = {
        id: entryId,
        key: sessionId,
        content: `Session started: ${sessionId}`,
        namespace: 'sessions',
        type: 'session',
        createdAt: Date.now(),
      };
      if (existing >= 0) store[existing] = entry;
      else store.push(entry);
      writeFileSync(storePath, JSON.stringify(store, null, 2), 'utf-8');
    } catch {
      // Non-critical — statusline just won't show this session
    }

    return {
      sessionId,
      started: new Date().toISOString(),
      restored: restoreLatest,
      config: {
        intelligenceEnabled: true,
        hooksEnabled: true,
        memoryPersistence: true,
        daemonEnabled: shouldStartDaemon,
      },
      daemon: daemonStatus,
      sessionMemory: sessionMemory || { controller: 'none', restoredPatterns: 0 },
      previousSession: restoreLatest ? {
        id: `session-${Date.now() - 86400000}`,
        tasksRestored: sessionMemory?.restoredPatterns || 0,
        memoryRestored: sessionMemory?.restoredPatterns || 0,
      } : null,
    };
  },
};

// Session end hook - stops daemon
export const hooksSessionEnd: MCPTool = {
  name: 'hooks_session-end',
  description: 'End current session, stop daemon, and persist state Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      saveState: { type: 'boolean', description: 'Save session state' },
      exportMetrics: { type: 'boolean', description: 'Export session metrics' },
      stopDaemon: { type: 'boolean', description: 'Stop worker daemon (default: true)' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const saveState = params.saveState !== false;
    const shouldStopDaemon = params.stopDaemon !== false;
    const session = loadActiveSessionState();
    if (!session) {
      throw new Error('No active session state found at .claude-flow/sessions/current.json');
    }
    const sessionId = session.id;
    const endedAt = Date.now();
    const duration = Math.max(0, endedAt - Date.parse(session.startedAt));
    const activity = loadSessionActivity(session, endedAt);
    const summary = buildSessionSummary(activity, duration);

    // Stop daemon if enabled
    let daemonStopped = false;
    if (shouldStopDaemon) {
      try {
        const { stopDaemon } = await import('../services/worker-daemon.js');
        await stopDaemon();
        daemonStopped = true;
      } catch {
        // Daemon may not be running
      }
    }

    // Read aggregate store data for the remaining compatibility metrics.
    const store = loadMemoryStore();
    const allEntries = Object.values(store.entries);
    const agentCount = allEntries.filter(e => e.key.includes('agent')).length;
    const trajectoryCount = activeTrajectories.size;

    // Check for pending-insights.jsonl
    let insightCount = 0;
    try {
      const insightsPath = resolve(join('.claude-flow', 'data', 'pending-insights.jsonl'));
      if (existsSync(insightsPath)) {
        const content = readFileSync(insightsPath, 'utf-8').trim();
        insightCount = content ? content.split('\n').length : 0;
      }
    } catch {
      // File not available
    }

    // Phase 5: Wire ReflexionMemory session end + NightlyLearner consolidation via bridge
    let sessionPersistence: { controller: string; persisted: boolean } | null = null;
    let bridge: typeof import('../memory/memory-bridge.js') | null = null;
    try {
      bridge = await import('../memory/memory-bridge.js');
      const result = await bridge.bridgeSessionEnd({
        sessionId,
        summary,
        tasksCompleted: activity.tasksCompleted,
        patternsLearned: activity.patternsLearned,
      });
      if (result) {
        sessionPersistence = {
          controller: result.controller,
          persisted: result.persisted,
        };
      }
    } catch {
      // Bridge not available
    } finally {
      // Release AgentDB/ONNX resources after one-shot session persistence.
      // A partially initialized native pool can otherwise keep Node alive
      // after the command has completed all logical work (#2691).
      try {
        await bridge?.shutdownBridge();
      } catch (err) {
        // Cleanup is best-effort and must not fail session-end, but a failed
        // AgentDB persist (lock/concurrent writer) is never silent.
        if ((err as { code?: unknown } | null)?.code === 'AGENTDB_LOCK_UNRECOVERABLE') {
          process.stderr.write(`[ruflo] warning: ${(err as Error).message}\n`);
        }
      }
    }

    const verifiedOutcomes = await ingestVerifiedQuietly();

    return {
      sessionId,
      duration,
      verifiedOutcomes,
      statePath: saveState ? `.claude/sessions/${sessionId}.json` : undefined,
      daemon: { stopped: daemonStopped },
      sessionPersistence: sessionPersistence || { controller: 'none', persisted: false },
      summary: {
        tasksExecuted: activity.tasksCompleted,
        filesModified: activity.editsRecorded,
        agentsSpawned: agentCount,
        pendingInsights: insightCount,
        memoryEntries: allEntries.length,
      },
      learningUpdates: {
        patternsLearned: activity.patternsLearned,
        trajectoriesRecorded: trajectoryCount,
      },
    };
  },
};

// Session restore hook
export const hooksSessionRestore: MCPTool = {
  name: 'hooks_session-restore',
  description: 'Restore a previous session Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      sessionId: { type: 'string', description: 'Session ID to restore (or "latest")' },
      restoreAgents: { type: 'boolean', description: 'Restore spawned agents' },
      restoreTasks: { type: 'boolean', description: 'Restore active tasks' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const requestedId = (params.sessionId as string) || 'latest';
    const restoreAgents = params.restoreAgents !== false;
    const restoreTasks = params.restoreTasks !== false;

    if (params.sessionId) { const v = validateIdentifier(params.sessionId as string, 'sessionId'); if (!v.valid) return { success: false, error: v.error }; }

    const originalSessionId = requestedId === 'latest' ? `session-${Date.now() - 86400000}` : requestedId;
    const newSessionId = `session-${Date.now()}`;

    // Get real memory entry count
    const store = loadMemoryStore();
    const memoryEntryCount = Object.keys(store.entries).length;

    // Count task and agent entries
    const taskEntries = Object.keys(store.entries).filter(k => k.includes('task')).length;
    const agentEntries = Object.keys(store.entries).filter(k => k.includes('agent')).length;

    return {
      sessionId: newSessionId,
      originalSessionId,
      restoredState: {
        tasksRestored: restoreTasks ? Math.min(taskEntries, 10) : 0,
        agentsRestored: restoreAgents ? Math.min(agentEntries, 5) : 0,
        memoryRestored: memoryEntryCount,
      },
      warnings: restoreTasks && taskEntries > 0 ? [`${Math.min(taskEntries, 2)} tasks were in progress and may need review`] : undefined,
      dataSource: 'memory-store',
    };
  },
};

// Notify hook - cross-agent notifications
export const hooksNotify: MCPTool = {
  name: 'hooks_notify',
  description: 'Send cross-agent notification Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      message: { type: 'string', description: 'Notification message' },
      target: { type: 'string', description: 'Target agent or "all"' },
      priority: { type: 'string', description: 'Priority level (low, normal, high, urgent)' },
      data: { type: 'object', description: 'Additional data payload' },
    },
    required: ['message'],
  },
  handler: async (params: Record<string, unknown>) => {
    const message = params.message as string;
    const target = (params.target as string) || 'all';
    const priority = (params.priority as string) || 'normal';

    { const v = validateText(message, 'message'); if (!v.valid) return { success: false, error: v.error }; }
    if (params.target) { const v = validateIdentifier(target, 'target'); if (!v.valid) return { success: false, error: v.error }; }

    return {
      notificationId: `notify-${Date.now()}`,
      message,
      target,
      priority,
      delivered: true,
      recipients: target === 'all' ? ['coder', 'architect', 'tester', 'reviewer'] : [target],
      timestamp: new Date().toISOString(),
    };
  },
};

// Init hook - initialize hooks in project
export const hooksInit: MCPTool = {
  name: 'hooks_init',
  description: 'Initialize hooks in project with .claude/settings.json Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      path: { type: 'string', description: 'Project path' },
      template: { type: 'string', description: 'Template to use (minimal, standard, full)' },
      force: { type: 'boolean', description: 'Overwrite existing configuration' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const path = (params.path as string) || '.';
    const template = (params.template as string) || 'standard';
    const force = params.force as boolean;

    const hooksConfigured = template === 'minimal' ? 4 : template === 'full' ? 16 : 9;

    return {
      path,
      template,
      created: {
        settingsJson: `${path}/.claude/settings.json`,
        hooksDir: `${path}/.claude/hooks`,
      },
      hooks: {
        configured: hooksConfigured,
        types: ['PreToolUse', 'PostToolUse', 'SessionStart', 'SessionEnd'],
      },
      intelligence: {
        enabled: template !== 'minimal',
        sona: template === 'full',
        moe: template === 'full',
        hnsw: template !== 'minimal',
      },
      overwritten: force,
    };
  },
};

// Intelligence hook - RuVector intelligence system
export const hooksIntelligence: MCPTool = {
  name: 'hooks_intelligence',
  description: 'RuVector intelligence system status (shows REAL metrics from memory store) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      mode: { type: 'string', description: 'Intelligence mode' },
      enableSona: { type: 'boolean', description: 'Enable SONA learning' },
      enableMoe: { type: 'boolean', description: 'Enable MoE routing' },
      enableHnsw: { type: 'boolean', description: 'Enable HNSW search' },
      forceTraining: { type: 'boolean', description: 'Force training cycle' },
      showStatus: { type: 'boolean', description: 'Show status only' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const mode = (params.mode as string) || 'balanced';
    const enableSona = params.enableSona !== false;
    const enableMoe = params.enableMoe !== false;
    const enableHnsw = params.enableHnsw !== false;

    // Get REAL statistics from memory store
    const realStats = getIntelligenceStatsFromMemory();

    // Check actual implementation availability
    const sonaAvailable = (await getSONAOptimizer()) !== null;
    const moeAvailable = (await getMoERouter()) !== null;
    const flashAvailable = (await getFlashAttention()) !== null;
    const ewcAvailable = (await getEWCConsolidator()) !== null;
    const loraAvailable = (await getLoRAAdapter()) !== null;

    // #2940: `forceTraining` (CLI `hooks intelligence --train`) was declared
    // on this tool's input schema but never read — the handler always
    // returned the same read-only status dashboard, so `lastAdaptation`
    // (what `--status` reports as "Last Training") could never move no
    // matter how many times `--train` ran. Actually distill when asked,
    // and report honestly rather than always implying success:
    // `attempted: false` (flag not passed), `trained: true` (real work
    // happened, lastAdaptation now current), or `trained: false` with a
    // reason (nothing new to distill, or distillation unavailable).
    const training: { attempted: boolean; trained: boolean; patternsDistilled?: number; ewcPenalty?: number; reason?: string } =
      { attempted: false, trained: false };
    if (params.forceTraining) {
      training.attempted = true;
      try {
        const { distillLearning } = await import('../memory/intelligence.js');
        const result = await distillLearning();
        if (result) {
          training.trained = true;
          training.patternsDistilled = result.patternsDistilled;
          training.ewcPenalty = result.ewcPenalty;
          if (result.patternsDistilled === 0) {
            training.reason = 'no new trajectories to distill since the last training cycle';
          }
        } else {
          training.reason = 'intelligence system unavailable — could not initialize SONA/ReasoningBank';
        }
      } catch (error) {
        training.reason = `distillation failed: ${error instanceof Error ? error.message : String(error)}`;
      }
    }

    return {
      training,
      mode,
      status: 'active',
      components: {
        sona: {
          enabled: enableSona,
          status: sonaAvailable ? 'active' : 'loading',
          implemented: true, // NOW IMPLEMENTED in alpha.102
          trajectoriesRecorded: realStats.trajectories.total,
          trajectoriesSuccessful: realStats.trajectories.successful,
          patternsLearned: realStats.patterns.learned,
          note: sonaAvailable ? 'SONA optimizer active - learning from trajectories' : 'SONA loading...',
        },
        moe: {
          enabled: enableMoe,
          status: moeAvailable ? 'active' : 'loading',
          implemented: true, // NOW IMPLEMENTED in alpha.102
          routingDecisions: realStats.routing.decisions,
          note: moeAvailable ? 'MoE router with 8 experts (coder, tester, reviewer, architect, security, performance, researcher, coordinator)' : 'MoE loading...',
        },
        hnsw: {
          enabled: enableHnsw,
          status: enableHnsw ? 'active' : 'disabled',
          implemented: true,
          indexSize: realStats.memory.indexSize,
          memorySizeBytes: realStats.memory.memorySizeBytes,
          note: 'HNSW vector indexing with 150x-12,500x speedup',
        },
        flashAttention: {
          enabled: true,
          status: flashAvailable ? 'active' : 'loading',
          implemented: true, // NOW IMPLEMENTED in alpha.102
          note: flashAvailable ? 'Flash Attention with O(N) memory (2.49x-7.47x speedup)' : 'Flash Attention loading...',
        },
        ewc: {
          enabled: true,
          status: ewcAvailable ? 'active' : 'loading',
          implemented: true, // NOW IMPLEMENTED in alpha.102
          note: ewcAvailable ? 'EWC++ consolidation prevents catastrophic forgetting' : 'EWC++ loading...',
        },
        lora: {
          enabled: true,
          status: loraAvailable ? 'active' : 'loading',
          implemented: true, // NOW IMPLEMENTED in alpha.102
          note: loraAvailable ? 'LoRA adapter with 128x memory compression (rank=8)' : 'LoRA loading...',
        },
        embeddings: {
          provider: 'transformers',
          model: 'Xenova/all-MiniLM-L6-v2',
          dimension: 384,
          implemented: true,
          note: 'Real ONNX embeddings via Xenova/all-MiniLM-L6-v2',
        },
        ruvllmCoordinator: await (async () => {
          try {
            const { getIntelligenceStats } = await import('../memory/intelligence.js');
            const s = getIntelligenceStats();
            return { status: s._ruvllmBackend || 'unavailable', trajectories: s._ruvllmTrajectories || 0, note: s._ruvllmBackend === 'active' ? 'SonaCoordinator forwarding trajectories' : '@ruvector/ruvllm not loaded' };
          } catch { return { status: 'unavailable', trajectories: 0, note: 'Not initialized' }; }
        })(),
        contrastiveTrainer: await (async () => {
          try {
            const { getSONAStats } = await import('../memory/sona-optimizer.js');
            const s = await getSONAStats();
            return { status: s._contrastiveTrainer !== 'unavailable' ? 'active' : 'unavailable', details: s._contrastiveTrainer, note: s._contrastiveTrainer !== 'unavailable' ? 'Agent embedding learning active' : '@ruvector/ruvllm not loaded' };
          } catch { return { status: 'unavailable', details: null, note: 'Not initialized' }; }
        })(),
        trainingPipeline: await (async () => {
          try {
            const loraInst = await getLoRAAdapter();
            const s = loraInst?.getStats();
            return { status: s?._trainingBackend || 'unavailable', note: s?._trainingBackend === 'ruvllm' ? 'Checkpoint save/load via ruvllm' : 'JS fallback' };
          } catch { return { status: 'unavailable', note: 'Not initialized' }; }
        })(),
        graphDatabase: await (async () => {
          try {
            const { getGraphStats } = await import('../ruvector/graph-backend.js');
            const gs = await getGraphStats();
            return { status: gs.backend, totalNodes: gs.totalNodes, totalEdges: gs.totalEdges, avgDegree: gs.avgDegree, note: gs.backend === 'graph-node' ? 'Native Rust graph with hyperedges and k-hop queries' : '@ruvector/graph-node not loaded' };
          } catch { return { status: 'unavailable', totalNodes: 0, totalEdges: 0, avgDegree: 0, note: 'Not initialized' }; }
        })(),
      },
      realMetrics: {
        trajectories: realStats.trajectories,
        patterns: realStats.patterns,
        memory: realStats.memory,
        routing: realStats.routing,
      },
      implementationStatus: {
        working: [
          'memory-store', 'embeddings', 'trajectory-recording', 'claims', 'swarm-coordination',
          'hnsw-index', 'pattern-storage', 'sona-optimizer', 'ewc-consolidation', 'moe-routing',
          'flash-attention', 'lora-adapter', 'ruvllm-coordinator', 'contrastive-trainer', 'training-pipeline', 'graph-database'
        ],
        partial: [],
        notImplemented: [],
      },
      version: '3.0.0-alpha.102',
    };
  },
};

// Intelligence reset hook
export const hooksIntelligenceReset: MCPTool = {
  name: 'hooks_intelligence-reset',
  description: 'Reset intelligence learning state Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {},
  },
  handler: async () => {
    const cwd = getProjectCwd();
    const cleared = {
      trajectories: 0,
      patterns: 0,
      dataFiles: 0,
      neuralFiles: 0,
    };
    const deletedFiles: string[] = [];

    // Clear intelligence data files if they exist
    const dataFiles = [
      join(cwd, '.claude-flow', 'data', 'auto-memory-store.json'),
      join(cwd, '.claude-flow', 'data', 'graph-state.json'),
      join(cwd, '.claude-flow', 'data', 'ranked-context.json'),
    ];

    for (const filePath of dataFiles) {
      if (existsSync(filePath)) {
        try {
          unlinkSync(filePath);
          cleared.dataFiles++;
          deletedFiles.push(filePath);
        } catch {
          // Skip files that cannot be deleted
        }
      }
    }

    // Clear neural directory if it exists
    const neuralDir = join(cwd, '.claude-flow', 'neural');
    if (existsSync(neuralDir)) {
      try {
        const files = readdirSync(neuralDir);
        for (const file of files) {
          try {
            const filePath = join(neuralDir, file);
            unlinkSync(filePath);
            cleared.neuralFiles++;
            deletedFiles.push(filePath);
          } catch {
            // Skip files that cannot be deleted
          }
        }
      } catch {
        // Directory read failed
      }
    }

    // Reset learned state without abandoning tasks still recording outcomes.
    cleared.trajectories = 0;

    return {
      reset: true,
      cleared,
      deletedFiles,
      timestamp: new Date().toISOString(),
    };
  },
};

// Intelligence trajectory hooks - REAL implementation using activeTrajectories
export const hooksTrajectoryStart: MCPTool = {
  name: 'hooks_intelligence_trajectory-start',
  description: 'Begin SONA trajectory for reinforcement learning Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'Task description' },
      agent: { type: 'string', description: 'Agent type' },
      sessionId: { type: 'string', description: 'Session id for the execution-state tree (default: CLAUDE_FLOW_SESSION_ID or "default")' },
    },
    required: ['task'],
  },
  handler: async (params: Record<string, unknown>) => {
    const task = params.task as string;
    const agent = (params.agent as string) || 'coder';
    const sessionId = (params.sessionId as string) || process.env.CLAUDE_FLOW_SESSION_ID || 'default';

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }
    if (params.agent) { const v = validateIdentifier(params.agent as string, 'agent'); if (!v.valid) return { success: false, error: v.error }; }
    if (params.sessionId) { const v = validateIdentifier(params.sessionId as string, 'sessionId'); if (!v.valid) return { success: false, error: v.error }; }

    const trajectoryId = `traj-${Date.now()}-${Math.random().toString(36).substring(7)}`;
    const startedAt = new Date().toISOString();

    // Create real trajectory entry in memory
    const trajectory: TrajectoryData = {
      id: trajectoryId,
      task,
      agent,
      steps: [],
      startedAt,
    };

    activeTrajectories.set(trajectoryId, trajectory);

    // MAGE-style execution-state-tree mirror (prototype, ruvector/trajectory-tree.ts).
    // Non-fatal by design — the semantic trajectory path below is untouched.
    try {
      const { getTrajectoryTree } = await import('../ruvector/trajectory-tree.js');
      getTrajectoryTree().openTrajectory({ sessionId, trajectoryId, task, agent });
    } catch { /* prototype path — never blocks trajectory recording */ }

    // Checkpoint before handing the id to another process.
    const persisted = await persistPendingTrajectory(trajectory);

    return {
      trajectoryId,
      task,
      agent,
      started: startedAt,
      status: 'recording',
      persisted,
      implementation: 'real-trajectory-tracking',
      activeCount: activeTrajectories.size,
    };
  },
};

export const hooksTrajectoryStep: MCPTool = {
  name: 'hooks_intelligence_trajectory-step',
  description: 'Record step in trajectory for reinforcement learning Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      trajectoryId: { type: 'string', description: 'Trajectory ID' },
      action: { type: 'string', description: 'Action taken' },
      result: { type: 'string', description: 'Action result' },
      quality: { type: 'number', minimum: 0, maximum: 1, description: 'Quality score (0-1)' },
    },
    required: ['trajectoryId', 'action'],
  },
  handler: async (params: Record<string, unknown>) => withTrajectoryOperation(params.trajectoryId as string, async () => {
    const trajectoryId = params.trajectoryId as string;
    // #14: scrub extended-thinking blocks so reasoning tokens don't contaminate
    // the learning signal (DISTILL embeds this text).
    const action = scrubReasoningBlocks(params.action as string);
    const result = scrubReasoningBlocks((params.result as string) || 'success');
    if (params.quality !== undefined && (typeof params.quality !== 'number'
      || !Number.isFinite(params.quality) || params.quality < 0 || params.quality > 1)) {
      return { success: false, error: 'quality must be a finite number between 0 and 1' };
    }
    const quality = params.quality === undefined ? 0.85 : params.quality as number;
    const timestamp = new Date().toISOString();
    const stepId = `step-${Date.now()}`;

    { const v = validateIdentifier(trajectoryId, 'trajectoryId'); if (!v.valid) return { success: false, error: v.error }; }
    { const v = validateText(action, 'action'); if (!v.valid) return { success: false, error: v.error }; }

    // Add step to real trajectory if it exists
    const trajectory = await loadPendingTrajectory(trajectoryId);
    if (trajectory) {
      trajectory.steps.push({
        action,
        result,
        quality,
        timestamp,
      });
    }

    const persisted = trajectory ? await persistPendingTrajectory(trajectory) : false;

    // MAGE-style execution-state-tree mirror (prototype, non-fatal)
    try {
      const { getTrajectoryTree } = await import('../ruvector/trajectory-tree.js');
      getTrajectoryTree().appendStep({ trajectoryId, stepId, action, quality });
    } catch { /* prototype path — never blocks step recording */ }

    // ADR-130 Phase 3: fire-and-forget causal edge write
    // trajectory context node → step node (relation: "trajectory-caused")
    if (result) {
      (async () => {
        try {
          const { insertGraphEdge } = await import('../memory/graph-edge-writer.js');
          await insertGraphEdge({
            sourceId: `task:${trajectoryId}`,
            targetId: `pattern:${stepId}`,
            relation: 'trajectory-caused',
            weight: quality,
            confidence: quality,
            metadata: { action, result, trajectoryId, stepId },
          });
        } catch { /* non-fatal */ }
      })().catch(() => {});
    }

    return {
      trajectoryId,
      stepId,
      action,
      result,
      quality,
      recorded: !!trajectory,
      persisted,
      timestamp,
      totalSteps: trajectory?.steps.length || 0,
      implementation: trajectory ? 'real-step-recording' : 'trajectory-not-found',
    };
  }),
};

export const hooksTrajectoryEnd: MCPTool = {
  name: 'hooks_intelligence_trajectory-end',
  description: 'End trajectory and trigger SONA learning with EWC++ Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      trajectoryId: { type: 'string', description: 'Trajectory ID' },
      success: { type: 'boolean', description: 'Overall success' },
      feedback: { type: 'string', description: 'Optional feedback' },
    },
    required: ['trajectoryId'],
  },
  handler: async (params: Record<string, unknown>) => withTrajectoryOperation(params.trajectoryId as string, async () => {
    const trajectoryId = params.trajectoryId as string;

    { const v = validateIdentifier(trajectoryId, 'trajectoryId'); if (!v.valid) return { success: false, error: v.error }; }

    const success = params.success !== false;
    const feedback = params.feedback as string | undefined;
    const endedAt = new Date().toISOString();
    const startTime = Date.now();

    // Get and finalize real trajectory
    const trajectory = await loadPendingTrajectory(trajectoryId);
    let persistResult: { success: boolean; id?: string; error?: string } = { success: false };

    if (trajectory) {
      trajectory.success = success;
      trajectory.endedAt = endedAt;

      // Persist trajectory to database using real store
      const storeFn = await getRealStoreFunction();
      if (storeFn) {
        try {
          // Create trajectory summary for embedding
          const summary = `Task: ${trajectory.task} | Agent: ${trajectory.agent} | Steps: ${trajectory.steps.length} | Success: ${success}${feedback ? ` | Feedback: ${feedback}` : ''}`;

          persistResult = await storeFn({
            key: `trajectory-${trajectoryId}`,
            value: JSON.stringify({
              ...trajectory,
              feedback,
            }),
            namespace: 'trajectories',
            generateEmbeddingFlag: true, // Generate embedding for semantic search
            tags: [trajectory.agent, success ? 'success' : 'failure', 'sona-trajectory'],
          });
        } catch (error) {
          persistResult = { success: false, error: error instanceof Error ? error.message : String(error) };
        }
      }

      // Failed writes remain retryable. A completed row prevents stale pending
      // data from being replayed even if cleanup itself fails.
      if (persistResult.success) {
        activeTrajectories.delete(trajectoryId);
        dirtyTrajectories.delete(trajectoryId);
        try {
          const { deleteEntry } = await import('../memory/memory-initializer.js');
          await deleteEntry({ key: `trajectory-pending-${trajectoryId}`, namespace: 'trajectories' });
        } catch { /* completed record is the replay guard */ }
      }
    }

    // MAGE-style execution-state-tree mirror (prototype, non-fatal)
    try {
      const { getTrajectoryTree } = await import('../ruvector/trajectory-tree.js');
      getTrajectoryTree().closeTrajectory({ trajectoryId, success });
    } catch { /* prototype path — never blocks trajectory finalization */ }

    // SONA Learning - process trajectory outcome for routing optimization
    let sonaResult: { learned: boolean; patternKey: string; confidence: number; reinforcedNeighbour?: boolean; routingGate?: { threshold: number; confidence: number; reached: boolean; successesNeeded: number } } = {
      learned: false, patternKey: '', confidence: 0
    };
    let ewcResult: { consolidated: boolean; penalty: number } = {
      consolidated: false, penalty: 0
    };

    if (trajectory && persistResult.success) {
      // Try SONA learning
      const sona = await getSONAOptimizer();
      if (sona) {
        try {
          const outcome = {
            trajectoryId,
            task: trajectory.task,
            agent: trajectory.agent,
            success,
            steps: trajectory.steps,
            feedback,
            duration: trajectory.startedAt
              ? new Date(endedAt).getTime() - new Date(trajectory.startedAt).getTime()
              : 0,
          };
          const result = sona.processTrajectoryOutcome(outcome);
          sonaResult = {
            learned: result.learned,
            patternKey: result.patternKey,
            confidence: result.confidence,
            reinforcedNeighbour: result.reinforcedNeighbour,
            routingGate: result.routingGate,
          };
        } catch {
          // SONA learning failed, continue without it
        }
      }

      // Trigger ruvllm background learning after trajectory end
      try {
        const { runBackgroundLearning } = await import('../memory/intelligence.js');
        await runBackgroundLearning();
      } catch { /* best-effort */ }

      // Try EWC++ consolidation on successful trajectories
      if (success) {
        const ewc = await getEWCConsolidator();
        if (ewc) {
          try {
            // AUDIT FIX #4: derive a REAL gradient from the trajectory's
            // embedding (mirrors the DISTILL path, where step content is
            // embedded via generateEmbedding) instead of a synthetic sine
            // wave. The EWC library treats the embedding as the gradient
            // proxy (see recordPatternOutcome in ewc-consolidation.ts).
            let gradients: number[] | null = null;
            try {
              const { generateEmbedding } = await import('../memory/memory-initializer.js');
              // Embed the same summary that was persisted for semantic search,
              // so the Fisher update reflects the actual recorded trajectory.
              const summary = `Task: ${trajectory.task} | Agent: ${trajectory.agent} | Steps: ${trajectory.steps.map(s => `${s.action}=>${s.result}`).join('; ')}${feedback ? ` | Feedback: ${feedback}` : ''}`;
              const embeddingResult = await generateEmbedding(summary);
              if (embeddingResult?.embedding && embeddingResult.embedding.length > 0) {
                gradients = embeddingResult.embedding;
              }
            } catch {
              // Embedding generation unavailable — fall through and skip EWC
            }

            if (gradients) {
              ewc.recordGradient(`trajectory-${trajectoryId}`, gradients, success);
              const stats = ewc.getConsolidationStats();
              ewcResult = {
                consolidated: true,
                penalty: stats.avgPenalty,
              };
            }
            // If no real embedding-derived gradient is available, SKIP the EWC
            // update rather than feeding the Fisher matrix synthetic noise.
          } catch {
            // EWC consolidation failed, continue without it
          }
        }
      }
    }

    // #2245 Round B — also bump globalStats so the trajectory-end MCP path
    // shows up in `hooks_intelligence_unified-stats.global.*` (was only
    // touching sonaCoordinator before — the "MCP trajectory tools feed sona,
    // not globalStats" gap from ADR-075). Maps the recorded steps to the
    // intelligence-module TrajectoryStep shape and runs them through the
    // canonical recordTrajectory() entry point.
    let globalStatsDelta = 0;
    if (trajectory && trajectory.steps && trajectory.steps.length > 0) {
      try {
        const intel = await import('../memory/intelligence.js');
        const before = intel.getIntelligenceStats();
        await intel.recordTrajectory(
          trajectory.steps.map((s: { action?: string; result?: string; content?: string; type?: string }) => ({
            type: (s.type as 'observation' | 'thought' | 'action' | 'result') ?? 'action',
            content: String(s.content ?? `${s.action ?? ''} → ${s.result ?? ''}`).slice(0, 4096),
            timestamp: Date.now(),
          })),
          success ? 'success' : 'failure',
        );
        const after = intel.getIntelligenceStats();
        globalStatsDelta = after.trajectoriesRecorded - before.trajectoriesRecorded;
      } catch { /* intelligence module not loadable — keep sona-only behaviour */ }
    }

    // #2351: when an agent calls trajectory-end with a non-empty `feedback`
    // string, the feedback was previously dropped on the floor —
    // `patternsExtracted` reported 0 and `pattern-search` never surfaced it.
    // Route it through the same store + embed path that pattern-store uses
    // so it becomes searchable. Best-effort: failures here must not turn the
    // trajectory-end call itself into a failure — the trajectory record was
    // already persisted above.
    //
    // #3662: #2351's original fix gated this on `!hasSteps` (step-less
    // trajectories only). The common start -> step -> end path (the one real
    // agents actually use) therefore recorded SONA state while leaving the
    // feedback unindexed. Non-empty feedback is promoted regardless of
    // whether steps were recorded — a trajectory's steps and its distilled
    // feedback are not mutually exclusive evidence.
    let feedbackDistilled: { stored: boolean; patternId?: string; controller?: string } = { stored: false };
    const trimmedFeedback = typeof feedback === 'string' ? feedback.trim() : '';
    if (trajectory && trimmedFeedback.length > 0) {
      const distilledPatternId = `pattern-feedback-${trajectoryId}-${Date.now()}`;
      const patternMetadata: Record<string, unknown> = {
        sourceTrajectoryId: trajectoryId,
        task: trajectory.task,
        agent: trajectory.agent,
        outcome: success ? 'success' : 'failure',
        distilledFrom: 'trajectory-end-feedback',
      };
      // Modest default confidence — step-less feedback hasn't been validated
      // by execution evidence the way a multi-step trajectory has.
      const feedbackConfidence = success ? 0.6 : 0.4;

      try {
        const bridge = await import('../memory/memory-bridge.js');
        const rb = await bridge.bridgeStorePattern({
          pattern: trimmedFeedback,
          type: 'trajectory-feedback',
          confidence: feedbackConfidence,
          metadata: patternMetadata,
        });
        if (rb?.success) {
          feedbackDistilled = { stored: true, patternId: rb.patternId, controller: rb.controller };
        }
      } catch {
        // Bridge unavailable — fall through to direct store
      }

      if (!feedbackDistilled.stored) {
        try {
          const storeFn = await getRealStoreFunction();
          if (storeFn) {
            const r = await storeFn({
              key: distilledPatternId,
              value: JSON.stringify({
                pattern: trimmedFeedback,
                type: 'trajectory-feedback',
                confidence: feedbackConfidence,
                metadata: patternMetadata,
                timestamp: endedAt,
              }),
              namespace: 'pattern',
              generateEmbeddingFlag: true,
              tags: [
                'trajectory-feedback',
                success ? 'success' : 'failure',
                `confidence-${Math.round(feedbackConfidence * 100)}`,
              ],
            });
            if (r?.success) {
              feedbackDistilled = { stored: true, patternId: r.id || distilledPatternId, controller: 'store-fallback' };
            }
          }
        } catch {
          // Both paths failed — leave feedbackDistilled.stored = false.
        }
      }
    }

    const learningTimeMs = Date.now() - startTime;
    // patternsExtracted now reflects either recorded steps (the original
    // semantics) OR a distilled feedback pattern (#2351), so step-less
    // trajectories with useful feedback no longer report 0.
    const patternsExtracted = (trajectory?.steps.length || 0) + (feedbackDistilled.stored ? 1 : 0);

    return {
      trajectoryId,
      success,
      ended: endedAt,
      persisted: persistResult.success,
      persistedId: persistResult.id,
      learning: {
        sonaUpdate: sonaResult.learned,
        sonaPatternKey: sonaResult.patternKey || undefined,
        sonaConfidence: sonaResult.confidence || undefined,
        sonaReinforcedNeighbour: sonaResult.reinforcedNeighbour || undefined,
        sonaRoutingGate: sonaResult.routingGate,
        ewcConsolidation: ewcResult.consolidated,
        ewcPenalty: ewcResult.penalty || undefined,
        patternsExtracted,
        feedbackDistilled: feedbackDistilled.stored ? {
          patternId: feedbackDistilled.patternId,
          controller: feedbackDistilled.controller,
        } : undefined,
        learningTimeMs,
        globalStatsTrajectoriesDelta: globalStatsDelta,  // Round B: was 0, now reflects
      },
      trajectory: trajectory ? {
        task: trajectory.task,
        agent: trajectory.agent,
        totalSteps: trajectory.steps.length,
        duration: trajectory.startedAt ? new Date(endedAt).getTime() - new Date(trajectory.startedAt).getTime() : 0,
      } : null,
      implementation: sonaResult.learned ? 'real-sona-learning' : (persistResult.success ? 'real-persistence' : 'memory-only'),
      note: sonaResult.learned
        ? `SONA learned pattern "${sonaResult.patternKey}" with ${(sonaResult.confidence * 100).toFixed(1)}% confidence`
        : (persistResult.success ? 'Trajectory persisted for future learning' : (persistResult.error || 'Trajectory not found')),
    };
  }),
};

// Pattern store/search hooks - REAL implementation using storeEntry
export const hooksPatternStore: MCPTool = {
  name: 'hooks_intelligence_pattern-store',
  description: 'Store pattern in ReasoningBank (HNSW-indexed) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      pattern: { type: 'string', description: 'Pattern description' },
      type: { type: 'string', description: 'Pattern type' },
      confidence: { type: 'number', description: 'Confidence score' },
      metadata: { type: 'object', description: 'Additional metadata' },
    },
    required: ['pattern'],
  },
  handler: async (params: Record<string, unknown>) => {
    const pattern = params.pattern as string;
    const type = (params.type as string) || 'general';
    const confidence = (params.confidence as number) || 0.8;
    const metadata = params.metadata as Record<string, unknown> | undefined;
    const timestamp = new Date().toISOString();

    { const v = validateText(pattern, 'pattern'); if (!v.valid) return { success: false, error: v.error }; }
    if (params.type) { const v = validateIdentifier(params.type as string, 'type'); if (!v.valid) return { success: false, error: v.error }; }
    const patternId = `pattern-${Date.now()}-${Math.random().toString(36).substring(7)}`;

    // Phase 3: Try ReasoningBank via bridge first
    let reasoningResult: { success: boolean; patternId: string; controller: string } | null = null;
    try {
      const bridge = await import('../memory/memory-bridge.js');
      reasoningResult = await bridge.bridgeStorePattern({ pattern, type, confidence, metadata: metadata as Record<string, unknown> | undefined });
    } catch {
      // Bridge not available
    }

    // Fallback: persist using memory-initializer store
    let storeResult: { success: boolean; id?: string; embedding?: { dimensions: number; model: string }; error?: string } = { success: false };
    if (!reasoningResult?.success) {
      const storeFn = await getRealStoreFunction();
      if (storeFn) {
        try {
          storeResult = await storeFn({
            key: patternId,
            value: JSON.stringify({ pattern, type, confidence, metadata, timestamp }),
            namespace: 'pattern',
            generateEmbeddingFlag: true,
            tags: [type, `confidence-${Math.round(confidence * 100)}`, 'reasoning-pattern'],
          });
        } catch (error) {
          storeResult = { success: false, error: error instanceof Error ? error.message : String(error) };
        }
      }
    }

    const success = reasoningResult?.success || storeResult.success;
    const controller = reasoningResult?.controller || (storeResult.success ? 'bridge-store' : 'none');
    const hasEmbedding = !!storeResult.embedding || controller === 'reasoningBank' || controller === 'bridge-fallback';

    return {
      patternId: reasoningResult?.patternId || storeResult.id || patternId,
      pattern,
      type,
      confidence,
      indexed: success,
      hnswIndexed: success && hasEmbedding,
      embedding: storeResult.embedding,
      timestamp,
      controller,
      implementation: (controller === 'reasoningBank' || controller === 'bridge-fallback')
        ? 'reasoning-bank-controller'
        : (storeResult.success ? 'real-hnsw-indexed' : 'memory-only'),
      note: controller === 'reasoningBank'
        ? 'Pattern stored via ReasoningBank controller with HNSW indexing'
        : controller === 'bridge-fallback'
          ? 'Pattern stored via bridge with embedding and HNSW indexing'
          : (storeResult.success ? 'Pattern stored with vector embedding for semantic search' : (storeResult.error || 'Store function unavailable')),
    };
  },
};

export const hooksPatternSearch: MCPTool = {
  name: 'hooks_intelligence_pattern-search',
  description: 'Search patterns using REAL vector search (HNSW when available, brute-force fallback) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'Search query' },
      topK: { type: 'number', description: 'Number of results' },
      minConfidence: { type: 'number', description: 'Minimum similarity threshold (0-1)' },
      namespace: { type: 'string', description: 'Namespace to search (default: pattern)' },
      strategy: { type: 'string', enum: ['semantic', 'state-tree'], description: 'Retrieval strategy. Default "semantic" (unchanged behavior). "state-tree" returns the MAGE-style root→current execution-state path for a session instead of embedding search (prototype).' },
      sessionId: { type: 'string', description: 'Session id for strategy="state-tree" (default: CLAUDE_FLOW_SESSION_ID or "default")' },
      depth: { type: 'number', description: 'For strategy="state-tree": max path nodes returned, counted from the current node upward' },
    },
    required: ['query'],
  },
  handler: async (params: Record<string, unknown>) => {
    const query = params.query as string;
    const topK = (params.topK as number) || 5;
    const minConfidence = (params.minConfidence as number) || 0.3;
    const namespace = (params.namespace as string) || 'pattern';
    const strategy = (params.strategy as string) || 'semantic';

    { const v = validateText(query, 'query'); if (!v.valid) return { success: false, error: v.error }; }
    if (params.namespace) { const v = validateIdentifier(params.namespace as string, 'namespace'); if (!v.valid) return { success: false, error: v.error }; }

    // Opt-in MAGE-style positional retrieval (prototype). Default stays the
    // semantic vector path below — zero behavior change unless requested.
    if (strategy === 'state-tree') {
      const sessionId = (params.sessionId as string) || process.env.CLAUDE_FLOW_SESSION_ID || 'default';
      if (params.sessionId) { const v = validateIdentifier(params.sessionId as string, 'sessionId'); if (!v.valid) return { success: false, error: v.error }; }
      try {
        const { getTrajectoryTree } = await import('../ruvector/trajectory-tree.js');
        const recall = getTrajectoryTree().recallPath({
          sessionId,
          depth: typeof params.depth === 'number' ? (params.depth as number) : undefined,
          siblingWindow: topK,
        });
        return {
          query,
          strategy: 'state-tree',
          backend: 'state-tree',
          sessionId: recall.sessionId,
          currentId: recall.currentId,
          path: recall.path,
          siblings: recall.siblings,
          note: 'Positional root→current execution-state path (no embedding search). Prototype — see ruvector/trajectory-tree.ts limitations.',
        };
      } catch (error) {
        return {
          query,
          strategy: 'state-tree',
          backend: 'state-tree-unavailable',
          path: [],
          siblings: [],
          error: String(error),
        };
      }
    }

    // Phase 3: Try ReasoningBank search via bridge first
    try {
      const bridge = await import('../memory/memory-bridge.js');
      const rbResult = await bridge.bridgeSearchPatterns({ query, topK, minConfidence });
      if (rbResult && rbResult.results.length > 0) {
        return {
          query,
          results: rbResult.results.map(r => ({
            patternId: r.id,
            pattern: r.content,
            similarity: r.score,
            confidence: r.score,
            namespace,
          })),
          searchTimeMs: 0,
          backend: rbResult.controller,
          note: `Results from ${rbResult.controller} controller`,
        };
      }
    } catch {
      // Bridge not available — fall through
    }

    // Fallback: Try real vector search via memory-initializer
    const searchFn = await getRealSearchFunction();

    if (searchFn) {
      try {
        const searchResult = await searchFn({
          query,
          namespace,
          limit: topK,
          threshold: minConfidence,
        });

        if (searchResult.success && searchResult.results.length > 0) {
          return {
            query,
            results: searchResult.results.map(r => ({
              patternId: r.id,
              pattern: r.content,
              similarity: r.score,
              confidence: r.score,
              namespace: r.namespace,
              key: r.key,
            })),
            searchTimeMs: searchResult.searchTime,
            backend: 'real-vector-search',
            note: 'Results from HNSW/SQLite vector search (BM25 hybrid)',
          };
        }

        // No results found
        return {
          query,
          results: [],
          searchTimeMs: searchResult.searchTime,
          backend: 'real-vector-search',
          note: searchResult.error || 'No matching patterns found. Store patterns first using memory/store with namespace "pattern".',
        };
      } catch (error) {
        // Fall through to empty response with error
        return {
          query,
          results: [],
          searchTimeMs: 0,
          backend: 'error',
          error: String(error),
          note: 'Vector search failed. Ensure memory database is initialized.',
        };
      }
    }

    // No search function available
    return {
      query,
      results: [],
      searchTimeMs: 0,
      backend: 'unavailable',
      note: 'Real vector search not available. Initialize memory database with: claude-flow memory init',
    };
  },
};

// Intelligence stats hook
export const hooksIntelligenceStats: MCPTool = {
  name: 'hooks_intelligence_stats',
  description: 'Get RuVector intelligence layer statistics Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      detailed: { type: 'boolean', description: 'Include detailed stats' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const detailed = params.detailed as boolean;

    // Get REAL statistics from actual implementations
    const sona = await getSONAOptimizer();
    const ewc = await getEWCConsolidator();
    const moe = await getMoERouter();
    const flash = await getFlashAttention();
    const lora = await getLoRAAdapter();

    // Fallback to memory store for legacy data (may not exist yet)
    let memoryStats: ReturnType<typeof getIntelligenceStatsFromMemory>;
    try {
      memoryStats = getIntelligenceStatsFromMemory();
    } catch {
      memoryStats = {
        trajectories: { total: 0, successful: 0 },
        patterns: { learned: 0, successful: 0, failed: 0, unknown: 0, described: 0, categories: {} },
        memory: { indexSize: 0, totalAccessCount: 0, memorySizeBytes: 0 },
        routing: { decisions: 0, avgConfidence: 0 },
      };
    }

    // SONA stats from real implementation
    let sonaStats = {
      trajectoriesTotal: memoryStats.trajectories.total,
      trajectoriesSuccessful: memoryStats.trajectories.successful,
      avgLearningTimeMs: 0,
      patternsLearned: memoryStats.patterns.learned,
      patternCategories: memoryStats.patterns.categories,
      successRate: 0,
      implementation: 'memory-fallback' as string,
    };
    if (sona) {
      const realSona = sona.getStats();
      const totalRoutes = realSona.successfulRoutings + realSona.failedRoutings;
      sonaStats = {
        trajectoriesTotal: realSona.trajectoriesProcessed,
        trajectoriesSuccessful: realSona.successfulRoutings,
        avgLearningTimeMs: realSona.lastUpdate ? 0.042 : 0, // Theoretical when active
        patternsLearned: realSona.totalPatterns,
        patternCategories: { learned: realSona.totalPatterns }, // Simplified
        successRate: totalRoutes > 0
          ? Math.round((realSona.successfulRoutings / totalRoutes) * 100) / 100
          : 0,
        implementation: 'real-sona',
      };
    }

    // EWC++ stats from real implementation
    let ewcStats = {
      consolidations: 0,
      catastrophicForgettingPrevented: 0,
      fisherUpdates: 0,
      avgPenalty: 0,
      totalPatterns: 0,
      implementation: 'not-loaded' as string,
    };
    if (ewc) {
      const realEwc = ewc.getConsolidationStats();
      ewcStats = {
        consolidations: realEwc.consolidationCount,
        catastrophicForgettingPrevented: realEwc.highImportancePatterns,
        fisherUpdates: realEwc.consolidationCount,
        avgPenalty: Math.round(realEwc.avgPenalty * 1000) / 1000,
        totalPatterns: realEwc.totalPatterns,
        implementation: 'real-ewc++',
      };
    }

    // MoE stats from real implementation
    let moeStats = {
      expertsTotal: 8,
      expertsActive: 0,
      routingDecisions: memoryStats.routing.decisions,
      avgRoutingTimeMs: 0,
      avgConfidence: memoryStats.routing.avgConfidence,
      loadBalance: null as { giniCoefficient: number; coefficientOfVariation: number; expertUsage: Record<string, number> } | null,
      implementation: 'not-loaded' as string,
    };
    if (moe) {
      const loadBalance = moe.getLoadBalance();
      const activeExperts = Object.values(loadBalance.routingCounts).filter((u: number) => u > 0).length;
      // Calculate average utilization as proxy for confidence
      const utilValues = Object.values(loadBalance.utilization) as number[];
      const avgUtil = utilValues.length > 0 ? utilValues.reduce((a, b) => a + b, 0) / utilValues.length : 0;
      moeStats = {
        expertsTotal: 8,
        expertsActive: activeExperts,
        routingDecisions: loadBalance.totalRoutings,
        avgRoutingTimeMs: 0.15, // Theoretical performance
        avgConfidence: Math.round(avgUtil * 100) / 100,
        loadBalance: {
          giniCoefficient: Math.round(loadBalance.giniCoefficient * 1000) / 1000,
          coefficientOfVariation: Math.round(loadBalance.coefficientOfVariation * 1000) / 1000,
          expertUsage: loadBalance.routingCounts,
        },
        implementation: 'real-moe',
      };
    }

    // Flash Attention stats from real implementation
    let flashStats = {
      speedup: 1.0,
      avgComputeTimeMs: 0,
      blockSize: 64,
      implementation: 'not-loaded' as string,
    };
    if (flash) {
      flashStats = {
        speedup: Math.round(flash.getSpeedup() * 100) / 100,
        avgComputeTimeMs: 0, // Would need benchmarking
        blockSize: 64,
        implementation: 'real-flash-attention',
      };
    }

    // LoRA stats from real implementation
    let loraStats = {
      rank: 8,
      alpha: 16,
      adaptations: 0,
      avgLoss: 0,
      implementation: 'not-loaded' as string,
    };
    if (lora) {
      const realLora = lora.getStats();
      loraStats = {
        rank: realLora.rank,
        alpha: 16, // Default alpha from config
        adaptations: realLora.totalAdaptations,
        avgLoss: Math.round(realLora.avgAdaptationNorm * 10000) / 10000,
        implementation: 'real-lora',
      };
    }

    // ruvllm native backend stats
    let ruvllmStats = { coordinator: 'unavailable' as string, trajectories: 0, contrastiveTrainer: 'unavailable' as string | object, trainingBackend: 'unavailable' as string, graphDatabase: { backend: 'unavailable', totalNodes: 0, totalEdges: 0 } as Record<string, unknown> };
    try {
      const { getIntelligenceStats } = await import('../memory/intelligence.js');
      const iStats = getIntelligenceStats();
      ruvllmStats.coordinator = iStats._ruvllmBackend || 'unavailable';
      ruvllmStats.trajectories = iStats._ruvllmTrajectories || 0;
    } catch { /* not initialized */ }
    try {
      const { getSONAStats: getSONA } = await import('../memory/sona-optimizer.js');
      const sStats = await getSONA();
      ruvllmStats.contrastiveTrainer = sStats._contrastiveTrainer || 'unavailable';
    } catch { /* not initialized */ }
    if (lora) {
      const ls = lora.getStats();
      ruvllmStats.trainingBackend = ls._trainingBackend || 'unavailable';
    }
    try {
      const { getGraphStats } = await import('../ruvector/graph-backend.js');
      const gs = await getGraphStats();
      ruvllmStats.graphDatabase = { backend: gs.backend, totalNodes: gs.totalNodes, totalEdges: gs.totalEdges, avgDegree: gs.avgDegree };
    } catch { /* not available */ }

    // ADR-148 — model-router operational counters (per-mechanism, per-backend,
    // A/B disagreement rate). Process-local so this is the most accurate
    // surface; the memory-store path was aggregates-only and lossy.
    let routerStats: ReturnType<typeof import('../ruvector/model-router.js').getModelRouterStats> | null = null;
    let neuralRouter: { enabled: boolean; available: boolean; routedBy: string | null; reason: string } | null = null;
    try {
      const { getModelRouterStats } = await import('../ruvector/model-router.js');
      routerStats = getModelRouterStats();
    } catch { /* router module not loaded */ }
    try {
      const { neuralRouterStatus } = await import('../ruvector/neural-router.js');
      const s = await neuralRouterStatus();
      neuralRouter = { enabled: s.enabled, available: s.available, routedBy: s.routedBy, reason: s.reason };
    } catch { /* neural-router module not loaded */ }

    // ADR-149 iter 42 — surface cost-savings to MCP consumers. Iter 31-34
    // shipped the CLI. This computes the headline numbers (last 7d actual
    // vs heuristic counterfactual) from the trajectory JSONL so Claude Code
    // sessions can ask "is the router saving money?" without shelling out
    // to bash. Best-effort: returns null when trajectory missing/empty.
    let costSavings: {
      windowDays: number;
      pairs: number;
      actualUsd: number;
      counterfactualUsd: number;
      savingsUsd: number;
      savingsPct: number;
    } | null = null;
    try {
      const fs = await import('node:fs');
      const pathMod = await import('node:path');
      const trajectoryPath = process.env.CLAUDE_FLOW_ROUTER_TRAJECTORY_PATH
        ?? pathMod.resolve(process.cwd(), '.swarm', 'model-router-trajectories.jsonl');
      if (fs.existsSync(trajectoryPath)) {
        const { MODEL_PRICES } = await import('../ruvector/model-prices.js');
        const windowMs = 7 * 86_400_000;          // 7-day window — matches iter 41 default
        const cutoffMs = Date.now() - windowMs;
        interface DecisionLite { ts: string; task_hash: string; complexity: number; ab_pair?: { bandit_pick: string } }
        interface OutcomeLite { ts: string; task_hash: string; cost_usd?: number; tokens?: { input: number; output: number } }
        // iter 63 — port iter 62's fix from CLI to MCP. Outcomes track ALL
        // occurrences (Array) instead of deduping by task_hash, so repeat
        // tasks contribute their full cumulative cost.
        const decisions = new Map<string, DecisionLite>();
        const outcomes: OutcomeLite[] = [];
        for (const l of fs.readFileSync(trajectoryPath, 'utf8').split('\n')) {
          if (!l.trim()) continue;
          try {
            const r = JSON.parse(l);
            if (Date.parse(r.ts) < cutoffMs) continue;
            if (r.type === 'decision') decisions.set(r.task_hash, r);
            else if (r.type === 'outcome') outcomes.push(r);
          } catch { /* malformed */ }
        }
        let pairs = 0, actual = 0, cf = 0;
        for (const out of outcomes) {
          if (!out?.cost_usd || !out.tokens) continue;
          const dec = decisions.get(out.task_hash);
          if (!dec) continue;
          pairs++;
          actual += out.cost_usd;
          // Same heuristic counterfactual as iter 32 default.
          const tierModel = dec.complexity < 0.34 ? 'haiku'
            : dec.complexity < 0.67 ? 'sonnet' : 'opus';
          const cfModel = dec.ab_pair?.bandit_pick ?? tierModel;
          const p = MODEL_PRICES[cfModel] ?? { in: 1, out: 1 };
          cf += (out.tokens.input * p.in + out.tokens.output * p.out) / 1_000_000;
        }
        if (pairs > 0) {
          const savings = cf - actual;
          costSavings = {
            windowDays: 7,
            pairs,
            actualUsd: Math.round(actual * 1_000_000) / 1_000_000,
            counterfactualUsd: Math.round(cf * 1_000_000) / 1_000_000,
            savingsUsd: Math.round(savings * 1_000_000) / 1_000_000,
            savingsPct: cf > 0 ? Math.round((savings / cf) * 10000) / 100 : 0,
          };
        }
      }
    } catch { /* trajectory parse failed — leave costSavings null */ }

    // ADR-149 iter 56 — recent activity + warmest bandit cell. Mirrors what
    // iter 49's CLI `stats-summary` shows so MCP consumers don't have to
    // glue together multiple tool calls. Single inline pass over the JSONL
    // (24h window only) + read of model-router-state.json.
    let recent24h: {
      decisions: number;
      fallbacks: number;
      fallbackRatePct: number;
    } | null = null;
    let warmestBanditCell: {
      bucket: string;
      key: string;
      samples: number;
      meanQuality: number;
    } | null = null;
    try {
      const fs = await import('node:fs');
      const pathMod = await import('node:path');
      const trajectoryPath = process.env.CLAUDE_FLOW_ROUTER_TRAJECTORY_PATH
        ?? pathMod.resolve(process.cwd(), '.swarm', 'model-router-trajectories.jsonl');
      if (fs.existsSync(trajectoryPath)) {
        const cutoffMs = Date.now() - 24 * 3600_000;
        let dec = 0, fallback = 0;
        for (const l of fs.readFileSync(trajectoryPath, 'utf8').split('\n')) {
          if (!l.trim()) continue;
          try {
            const r = JSON.parse(l);
            if (r.type !== 'decision') continue;
            if (Date.parse(r.ts) < cutoffMs) continue;
            dec++;
            if (r.routed_by === 'bandit-fallback') fallback++;
          } catch { /* */ }
        }
        if (dec > 0) {
          recent24h = {
            decisions: dec,
            fallbacks: fallback,
            fallbackRatePct: Math.round((fallback / dec) * 10000) / 100,
          };
        }
      }

      // Warmest bandit cell from persisted state.
      const statePath = pathMod.resolve(process.cwd(), '.swarm', 'model-router-state.json');
      if (fs.existsSync(statePath)) {
        const state = JSON.parse(fs.readFileSync(statePath, 'utf8'));
        const priors = state.priorsById ?? state.priors ?? {};
        let bestSamples = 0;
        for (const bucket of ['low', 'med', 'high']) {
          const b = priors[bucket];
          if (!b) continue;
          for (const [k, p] of Object.entries(b as Record<string, { alpha: number; beta: number }>)) {
            const samples = p.alpha + p.beta - 2;
            if (samples > bestSamples) {
              bestSamples = samples;
              warmestBanditCell = { bucket, key: k, samples, meanQuality: p.alpha / (p.alpha + p.beta) };
            }
          }
        }
      }
    } catch { /* best-effort */ }

    // ADR-149 iter 51 — surface forward projection to MCP, mirroring iter 41
    // (CLI cost-projection) but as an additive field. Linear extrapolation
    // from the same 7d measurement window the costSavings block used. JSON
    // shape lets Claude Code sessions answer "what will routing cost over
    // the next 30 days?" in-conversation.
    let costProjection: {
      windowDays: number;
      callsPerDay: number;
      avgActualPerCall: number;
      avgCounterfactualPerCall: number;
      horizons: Record<string, {
        projectedCalls: number;
        projectedActualUsd: number;
        projectedCounterfactualUsd: number;
        projectedSavingsUsd: number;
        projectedSavingsPct: number;
      }>;
    } | null = null;
    if (costSavings && costSavings.pairs > 0) {
      const windowSeconds = 7 * 86400;
      const callsPerSecond = costSavings.pairs / windowSeconds;
      const callsPerDay = callsPerSecond * 86400;
      const avgActualPerCall = costSavings.actualUsd / costSavings.pairs;
      const avgCfPerCall = costSavings.counterfactualUsd / costSavings.pairs;
      const horizonDays = { '30d': 30, '90d': 90, '365d': 365 };
      const horizons: Record<string, { projectedCalls: number; projectedActualUsd: number; projectedCounterfactualUsd: number; projectedSavingsUsd: number; projectedSavingsPct: number }> = {};
      for (const [label, days] of Object.entries(horizonDays)) {
        const projectedCalls = Math.round(callsPerDay * days);
        const projActual = avgActualPerCall * projectedCalls;
        const projCf = avgCfPerCall * projectedCalls;
        const projSavings = projCf - projActual;
        horizons[label] = {
          projectedCalls,
          projectedActualUsd: Math.round(projActual * 1_000_000) / 1_000_000,
          projectedCounterfactualUsd: Math.round(projCf * 1_000_000) / 1_000_000,
          projectedSavingsUsd: Math.round(projSavings * 1_000_000) / 1_000_000,
          projectedSavingsPct: projCf > 0 ? Math.round((projSavings / projCf) * 10000) / 100 : 0,
        };
      }
      costProjection = {
        windowDays: 7,
        callsPerDay: Math.round(callsPerDay * 100) / 100,
        avgActualPerCall: Math.round(avgActualPerCall * 1_000_000) / 1_000_000,
        avgCounterfactualPerCall: Math.round(avgCfPerCall * 1_000_000) / 1_000_000,
        horizons,
      };
    }

    const stats = {
      sona: sonaStats,
      moe: moeStats,
      ewc: ewcStats,
      flash: flashStats,
      lora: loraStats,
      ruvllm: ruvllmStats,
      hnsw: {
        indexSize: memoryStats.memory.indexSize,
        avgSearchTimeMs: 0.12,
        cacheHitRate: memoryStats.memory.totalAccessCount > 0
          ? Math.min(0.95, 0.5 + (memoryStats.memory.totalAccessCount / 1000))
          : 0.78,
        memoryUsageMb: Math.round(memoryStats.memory.memorySizeBytes / 1024 / 1024 * 100) / 100,
      },
      // ADR-148 — model-routing surface
      modelRouter: routerStats ? {
        totalDecisions: routerStats.totalDecisions,
        modelDistribution: routerStats.modelDistribution,
        routedByCounts: routerStats.routedByCounts,
        neuralBackendCounts: routerStats.neuralBackendCounts,
        ab: routerStats.ab,
        avgComplexity: routerStats.avgComplexity,
        avgConfidence: routerStats.avgConfidence,
      } : null,
      neuralRouter,
      // ADR-149 iter 42 — cost-savings surface for MCP consumers (matches
      // `claude-flow neural router cost-savings --since 7d` shape).
      costSavings,
      // ADR-149 iter 51 — forward budget projection (matches `cost-projection`
      // CLI shape). Pairs with costSavings: one says how much we saved, the
      // other extrapolates that savings forward to operational horizons.
      costProjection,
      // ADR-149 iter 56 — recent activity + warmest bandit cell to complete
      // feature parity with the CLI `stats-summary` (iter 49). One MCP tool
      // call now returns everything needed for an in-conversation dashboard.
      recent24h,
      warmestBanditCell,
      dataSource: sona ? 'real-implementations' : 'memory-fallback',
      lastUpdated: new Date().toISOString(),
    };

    if (detailed) {
      return {
        ...stats,
        implementationStatus: {
          sona: sona ? 'loaded' : 'not-loaded',
          ewc: ewc ? 'loaded' : 'not-loaded',
          moe: moe ? 'loaded' : 'not-loaded',
          flash: flash ? 'loaded' : 'not-loaded',
          lora: lora ? 'loaded' : 'not-loaded',
        },
        performance: {
          sonaLearningMs: sonaStats.avgLearningTimeMs,
          moeRoutingMs: moeStats.avgRoutingTimeMs,
          flashSpeedup: flashStats.speedup,
          ewcPenalty: ewcStats.avgPenalty,
        },
      };
    }

    return stats;
  },
};

// Intelligence learn hook
export const hooksIntelligenceLearn: MCPTool = {
  name: 'hooks_intelligence_learn',
  description: 'Force immediate SONA learning cycle with EWC++ consolidation Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      trajectoryIds: { type: 'array', items: { type: 'string' }, description: 'Specific trajectories to learn from' },
      consolidate: { type: 'boolean', description: 'Run EWC++ consolidation' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const consolidate = params.consolidate !== false;
    const startTime = Date.now();

    // AUDIT FIX #5: actually TRIGGER a learning/consolidation cycle instead of
    // only reading and echoing stats. This calls the real DISTILL path
    // (LoRA-style confidence updates with EWC++ consolidation protection) and
    // the background learning pass, then reports the resulting stats.
    let distill: { patternsDistilled: number; ewcPenalty: number } | null = null;
    let distillTriggered = false;
    try {
      const intelligence = await import('../memory/intelligence.js');
      // DISTILL + CONSOLIDATE: real LoRA update with EWC++ protection
      distill = await intelligence.distillLearning();
      distillTriggered = distill !== null;
      // Run background learning (ruvllm) pass as well — best-effort
      try {
        await intelligence.runBackgroundLearning();
      } catch { /* best-effort */ }
    } catch {
      // intelligence layer unavailable — fall back to stats-only reporting
    }

    // Get SONA statistics (AFTER triggering the cycle so they reflect the update)
    let sonaStats = {
      totalPatterns: 0,
      successfulRoutings: 0,
      failedRoutings: 0,
      trajectoriesProcessed: 0,
      avgConfidence: 0,
    };
    const sona = await getSONAOptimizer();
    if (sona) {
      const stats = sona.getStats();
      sonaStats = {
        totalPatterns: stats.totalPatterns,
        successfulRoutings: stats.successfulRoutings,
        failedRoutings: stats.failedRoutings,
        trajectoriesProcessed: stats.trajectoriesProcessed,
        avgConfidence: stats.avgConfidence,
      };
    }

    // Get EWC++ statistics after the consolidation cycle ran
    let ewcStats = {
      consolidation: false,
      fisherUpdated: false,
      forgettingPrevented: 0,
      avgPenalty: 0,
    };
    if (consolidate) {
      const ewc = await getEWCConsolidator();
      if (ewc) {
        const stats = ewc.getConsolidationStats();
        ewcStats = {
          consolidation: true,
          fisherUpdated: stats.consolidationCount > 0,
          forgettingPrevented: stats.highImportancePatterns,
          avgPenalty: distill?.ewcPenalty ?? stats.avgPenalty,
        };
      }
    }

    return {
      // "learned" now reflects whether a real distill cycle actually ran
      learned: distillTriggered || sonaStats.totalPatterns > 0,
      cycleTriggered: distillTriggered,
      patternsDistilled: distill?.patternsDistilled ?? 0,
      duration: Date.now() - startTime,
      updates: {
        trajectoriesProcessed: sonaStats.trajectoriesProcessed,
        patternsLearned: sonaStats.totalPatterns,
        patternsDistilled: distill?.patternsDistilled ?? 0,
        successRate: sonaStats.trajectoriesProcessed > 0
          ? (sonaStats.successfulRoutings / (sonaStats.successfulRoutings + sonaStats.failedRoutings) * 100).toFixed(1) + '%'
          : '0%',
      },
      ewc: consolidate ? ewcStats : null,
      confidence: {
        average: sonaStats.avgConfidence,
        implementation: sona ? 'real-sona' : 'not-available',
      },
      implementation: distillTriggered
        ? 'real-distill-consolidate'
        : (sona ? 'real-sona-learning' : 'placeholder'),
    };
  },
};

// Intelligence attention hook
export const hooksIntelligenceAttention: MCPTool = {
  name: 'hooks_intelligence_attention',
  description: 'Compute attention-weighted similarity using MoE/Flash. Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'Query for attention computation' },
      mode: { type: 'string', description: 'Attention mode (flash, moe)' },
      topK: { type: 'number', description: 'Top-k results' },
    },
    required: ['query'],
  },
  handler: async (params: Record<string, unknown>) => {
    const query = params.query as string;
    const mode = (params.mode as string) || 'flash';
    const topK = (params.topK as number) || 5;
    const startTime = performance.now();

    { const v = validateText(query, 'query'); if (!v.valid) return { success: false, error: v.error }; }
    // #3548: only flash and moe have an implementation. Any other mode
    // (including the previously advertised 'hyperbolic') used to return an
    // empty stub whose note blamed a missing package; say so instead.
    if (mode !== 'flash' && mode !== 'moe') {
      return { success: false, error: `Attention mode "${mode}" is not implemented; supported modes: flash, moe` };
    }

    let implementation = 'placeholder';
    let embeddingSource: 'onnx' | 'hash-fallback' | 'none' = 'none';
    const results: Array<{ index: number; weight: number; pattern: string; expert?: string }> = [];

    // Helper: generate query embedding, preferring real ONNX embeddings over hash fallback
    async function getQueryEmbedding(text: string, dims: number): Promise<{ embedding: Float32Array; source: 'onnx' | 'hash-fallback' }> {
      // Try ONNX via @claude-flow/embeddings
      try {
        const embeddingsModule = await import('@claude-flow/embeddings').catch(() => null);
        if (embeddingsModule?.createEmbeddingService) {
          const service = embeddingsModule.createEmbeddingService({ provider: 'onnx' });
          const result = await service.embed(text);
          const arr = new Float32Array(dims);
          for (let i = 0; i < Math.min(dims, result.embedding.length); i++) {
            arr[i] = result.embedding[i];
          }
          return { embedding: arr, source: 'onnx' };
        }
      } catch {
        // ONNX not available, try agentic-flow
      }

      // Try agentic-flow embeddings
      try {
        const embeddingsModule = await import('@claude-flow/embeddings').catch(() => null);
        if (embeddingsModule?.createEmbeddingService) {
          const service = embeddingsModule.createEmbeddingService({ provider: 'agentic-flow' });
          const result = await service.embed(text);
          const arr = new Float32Array(dims);
          for (let i = 0; i < Math.min(dims, result.embedding.length); i++) {
            arr[i] = result.embedding[i];
          }
          return { embedding: arr, source: 'onnx' };
        }
      } catch {
        // agentic-flow not available
      }

      // Hash-based fallback (deterministic but not semantic)
      const arr = new Float32Array(dims);
      let seed = text.split('').reduce((acc, char, i) => acc + char.charCodeAt(0) * (i + 1), 0);
      for (let i = 0; i < dims; i++) {
        seed = (seed * 1103515245 + 12345) & 0x7fffffff;
        arr[i] = (seed / 0x7fffffff) * 2 - 1;
      }
      return { embedding: arr, source: 'hash-fallback' };
    }

    if (mode === 'moe') {
      // Try MoE routing
      const moe = await getMoERouter();
      if (moe) {
        try {
          const embResult = await getQueryEmbedding(query, 384);
          embeddingSource = embResult.source;

          const routingResult = moe.route(embResult.embedding);
          for (let i = 0; i < Math.min(topK, routingResult.experts.length); i++) {
            const expert = routingResult.experts[i];
            results.push({
              index: i,
              weight: expert.weight,
              pattern: `Expert: ${expert.name}`,
              expert: expert.name,
            });
          }
          implementation = 'real-moe-router';
        } catch {
          // Fall back to placeholder
        }
      }
    } else if (mode === 'flash') {
      // Try Flash Attention. ADR-093 F10: previously this attended over
      // synthetic cosine-derived keys/values with constant-vector values,
      // which produced uniform 0.333 weights and labels like "Flash
      // attention target #1/2/3". Now we attend over actual stored
      // patterns when available — real semantic content yields non-uniform
      // weights and human-readable labels.
      const flash = await getFlashAttention();
      if (flash) {
        try {
          const embResult = await getQueryEmbedding(query, 384);
          embeddingSource = embResult.source;
          const q = embResult.embedding;

          // Pull real stored patterns to attend over. If none exist yet,
          // fall back to the synthetic harness but mark it honestly.
          const realPatterns: Array<{ id: string; content: string; embedding?: number[] }> = [];
          try {
            const { searchEntries: searchFn } = await import('../memory/memory-initializer.js');
            const hits = await searchFn({ query, limit: topK });
            if (Array.isArray(hits)) {
              for (const h of hits.slice(0, topK)) {
                const content = (h as Record<string, unknown>).content ?? (h as Record<string, unknown>).value ?? '';
                const id = String((h as Record<string, unknown>).id ?? (h as Record<string, unknown>).key ?? `pattern-${realPatterns.length}`);
                realPatterns.push({ id, content: String(content) });
              }
            }
          } catch { /* memory not initialized — fall through to synthetic */ }

          const useReal = realPatterns.length > 0;
          const keys: Float32Array[] = [];
          const values: Float32Array[] = [];
          const labels: string[] = [];

          if (useReal) {
            // Build keys from real pattern embeddings (re-embed if no vector cached)
            for (let k = 0; k < realPatterns.length; k++) {
              const p = realPatterns[k];
              let keyEmbedding: Float32Array;
              if (p.embedding && p.embedding.length === 384) {
                keyEmbedding = new Float32Array(p.embedding);
              } else {
                const enc = await getQueryEmbedding(p.content.slice(0, 1024), 384);
                keyEmbedding = enc.embedding;
              }
              const value = new Float32Array(384);
              // Value carries pattern identity strength — magnitude = recency proxy (k position)
              const strength = 1 / (k + 1);
              for (let i = 0; i < 384; i++) value[i] = keyEmbedding[i] * strength;
              keys.push(keyEmbedding);
              values.push(value);
              const label = p.content.length > 0
                ? `${p.id}: ${p.content.slice(0, 60)}${p.content.length > 60 ? '…' : ''}`
                : p.id;
              labels.push(label);
            }
          } else {
            // No real patterns — surface a synthetic harness honestly.
            for (let k = 0; k < topK; k++) {
              const key = new Float32Array(384);
              const value = new Float32Array(384);
              for (let i = 0; i < 384; i++) {
                key[i] = Math.cos((k + 1) * (i + 1) * 0.01);
                value[i] = k + 1;
              }
              keys.push(key);
              values.push(value);
              labels.push(`(synthetic harness) pattern #${k + 1}`);
            }
          }

          const attentionResult = flash.attention([q], keys, values);
          // Compute softmax weights from output magnitudes
          const outputMags = attentionResult.output[0]
            ? Array.from(attentionResult.output[0]).slice(0, keys.length).map(v => Math.abs(v))
            : new Array(keys.length).fill(1);
          const sumMags = outputMags.reduce((a, b) => a + b, 0) || 1;
          for (let i = 0; i < keys.length; i++) {
            results.push({
              index: i,
              weight: outputMags[i] / sumMags,
              pattern: labels[i],
            });
          }
          implementation = useReal ? 'real-flash-attention+memory' : 'real-flash-attention+synthetic-harness';
        } catch {
          // Fall back to placeholder
        }
      }
    }

    // If no real implementation worked, return empty with honest marker
    if (results.length === 0) {
      implementation = 'none';
    }

    const computeTimeMs = performance.now() - startTime;

    return {
      query,
      mode,
      results,
      stats: {
        computeTimeMs,
        implementation,
        _embeddingSource: embeddingSource,
        _stub: implementation === 'none',
        _note: implementation === 'none' ? 'No attention backend available: flash/moe attention from @claude-flow/neural could not be loaded or failed.' : undefined,
        ...(embeddingSource === 'hash-fallback' && implementation !== 'none'
          ? { _embeddingNote: 'Query embeddings are hash-based (not semantic). Install @claude-flow/embeddings for real ONNX embeddings.' }
          : {}),
      },
      implementation,
    };
  },
};

// =============================================================================
// Worker Dispatch Tools (12 Background Workers)
// =============================================================================

/**
 * Worker trigger types matching agentic-flow v3
 */
type WorkerTrigger =
  | 'ultralearn'    // Deep knowledge acquisition
  | 'optimize'      // Performance optimization
  | 'consolidate'   // Memory consolidation
  | 'predict'       // Predictive preloading
  | 'audit'         // Security analysis
  | 'map'           // Codebase mapping
  | 'preload'       // Resource preloading
  | 'deepdive'      // Deep code analysis
  | 'document'      // Auto-documentation
  | 'refactor'      // Refactoring suggestions
  | 'benchmark'     // Performance benchmarks
  | 'testgaps';     // Test coverage analysis

/**
 * Worker trigger patterns for auto-detection
 */
const WORKER_TRIGGER_PATTERNS: Record<WorkerTrigger, RegExp[]> = {
  ultralearn: [
    /learn\s+about/i,
    /understand\s+(how|what|why)/i,
    /deep\s+dive\s+into/i,
    /explain\s+in\s+detail/i,
    /comprehensive\s+guide/i,
    /master\s+this/i,
  ],
  optimize: [
    /optimize/i,
    /improve\s+performance/i,
    /make\s+(it\s+)?faster/i,
    /speed\s+up/i,
    /reduce\s+(memory|time)/i,
    /performance\s+issue/i,
  ],
  consolidate: [
    /consolidate/i,
    /merge\s+memories/i,
    /clean\s+up\s+memory/i,
    /deduplicate/i,
    /memory\s+maintenance/i,
  ],
  predict: [
    /what\s+will\s+happen/i,
    /predict/i,
    /forecast/i,
    /anticipate/i,
    /preload/i,
    /prepare\s+for/i,
  ],
  audit: [
    /security\s+audit/i,
    /vulnerability/i,
    /security\s+check/i,
    /pentest/i,
    /security\s+scan/i,
    /cve/i,
    /owasp/i,
  ],
  map: [
    /map\s+(the\s+)?codebase/i,
    /architecture\s+overview/i,
    /project\s+structure/i,
    /dependency\s+graph/i,
    /code\s+map/i,
    /explore\s+codebase/i,
  ],
  preload: [
    /preload/i,
    /cache\s+ahead/i,
    /prefetch/i,
    /warm\s+(up\s+)?cache/i,
  ],
  deepdive: [
    /deep\s+dive/i,
    /analyze\s+thoroughly/i,
    /in-depth\s+analysis/i,
    /comprehensive\s+review/i,
    /detailed\s+examination/i,
  ],
  document: [
    /document\s+(this|the)/i,
    /generate\s+docs/i,
    /add\s+documentation/i,
    /write\s+readme/i,
    /api\s+docs/i,
    /jsdoc/i,
  ],
  refactor: [
    /refactor/i,
    /clean\s+up\s+code/i,
    /improve\s+code\s+quality/i,
    /restructure/i,
    /simplify/i,
    /make\s+more\s+readable/i,
  ],
  benchmark: [
    /benchmark/i,
    /performance\s+test/i,
    /measure\s+speed/i,
    /stress\s+test/i,
    /load\s+test/i,
  ],
  testgaps: [
    /test\s+coverage/i,
    /missing\s+tests/i,
    /untested\s+code/i,
    /coverage\s+report/i,
    /test\s+gaps/i,
    /add\s+tests/i,
  ],
};

/**
 * Worker configurations
 */
const WORKER_CONFIGS: Record<WorkerTrigger, {
  description: string;
  priority: 'low' | 'normal' | 'high' | 'critical';
  estimatedDuration: string;
  capabilities: string[];
}> = {
  ultralearn: {
    description: 'Deep knowledge acquisition and learning',
    priority: 'normal',
    estimatedDuration: '60s',
    capabilities: ['research', 'analysis', 'synthesis'],
  },
  optimize: {
    description: 'Performance optimization and tuning',
    priority: 'high',
    estimatedDuration: '30s',
    capabilities: ['profiling', 'optimization', 'benchmarking'],
  },
  consolidate: {
    description: 'Memory consolidation and cleanup',
    priority: 'low',
    estimatedDuration: '20s',
    capabilities: ['memory-management', 'deduplication'],
  },
  predict: {
    description: 'Predictive preloading and anticipation',
    priority: 'normal',
    estimatedDuration: '15s',
    capabilities: ['prediction', 'caching', 'preloading'],
  },
  audit: {
    description: 'Security analysis and vulnerability scanning',
    priority: 'critical',
    estimatedDuration: '45s',
    capabilities: ['security', 'vulnerability-scanning', 'audit'],
  },
  map: {
    description: 'Codebase mapping and architecture analysis',
    priority: 'normal',
    estimatedDuration: '30s',
    capabilities: ['analysis', 'mapping', 'visualization'],
  },
  preload: {
    description: 'Resource preloading and cache warming',
    priority: 'low',
    estimatedDuration: '10s',
    capabilities: ['caching', 'preloading'],
  },
  deepdive: {
    description: 'Deep code analysis and examination',
    priority: 'normal',
    estimatedDuration: '60s',
    capabilities: ['analysis', 'review', 'understanding'],
  },
  document: {
    description: 'Auto-documentation generation',
    priority: 'normal',
    estimatedDuration: '45s',
    capabilities: ['documentation', 'writing', 'generation'],
  },
  refactor: {
    description: 'Code refactoring suggestions',
    priority: 'normal',
    estimatedDuration: '30s',
    capabilities: ['refactoring', 'code-quality', 'improvement'],
  },
  benchmark: {
    description: 'Performance benchmarking',
    priority: 'normal',
    estimatedDuration: '60s',
    capabilities: ['benchmarking', 'testing', 'measurement'],
  },
  testgaps: {
    description: 'Test coverage analysis',
    priority: 'normal',
    estimatedDuration: '30s',
    capabilities: ['testing', 'coverage', 'analysis'],
  },
};

// In-memory worker tracking
const activeWorkers: Map<string, {
  id: string;
  trigger: WorkerTrigger;
  context: string;
  status: 'pending' | 'running' | 'completed' | 'failed';
  progress: number;
  phase: string;
  startedAt: Date;
  completedAt?: Date;
  /** True when this record was created by the in-process detect path, not a real executor. */
  synthetic?: boolean;
  /** Why the worker record is synthetic, propagated to worker-status/list. */
  note?: string;
}> = new Map();

let workerIdCounter = 0;

/**
 * Detect triggers from prompt text
 */
function detectWorkerTriggers(text: string): {
  detected: boolean;
  triggers: WorkerTrigger[];
  confidence: number;
  context: string;
} {
  if (!text) return { detected: false, triggers: [], confidence: 0, context: '' };

  const detectedTriggers: WorkerTrigger[] = [];
  let totalMatches = 0;

  for (const [trigger, patterns] of Object.entries(WORKER_TRIGGER_PATTERNS) as [WorkerTrigger, RegExp[]][]) {
    for (const pattern of patterns) {
      if (pattern.test(text)) {
        if (!detectedTriggers.includes(trigger)) {
          detectedTriggers.push(trigger);
        }
        totalMatches++;
      }
    }
  }

  const confidence = detectedTriggers.length > 0
    ? Math.min(1, totalMatches / (detectedTriggers.length * 2))
    : 0;

  return {
    detected: detectedTriggers.length > 0,
    triggers: detectedTriggers,
    confidence,
    context: text.slice(0, 100),
  };
}

// Worker list tool
export const hooksWorkerList: MCPTool = {
  name: 'hooks_worker-list',
  description: 'List all 12 background workers with status and capabilities Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      status: { type: 'string', description: 'Filter by status (all, running, completed, pending)' },
      includeActive: { type: 'boolean', description: 'Include active worker instances' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const statusFilter = (params.status as string) || 'all';
    const includeActive = params.includeActive !== false;

    const workers = Object.entries(WORKER_CONFIGS).map(([trigger, config]) => ({
      trigger,
      ...config,
      patterns: WORKER_TRIGGER_PATTERNS[trigger as WorkerTrigger].length,
    }));

    const activeList = includeActive
      ? Array.from(activeWorkers.values()).filter(w =>
          statusFilter === 'all' || w.status === statusFilter
        )
      : [];

    return {
      workers,
      total: 12,
      active: {
        instances: activeList,
        count: activeList.length,
        byStatus: {
          pending: activeList.filter(w => w.status === 'pending').length,
          running: activeList.filter(w => w.status === 'running').length,
          completed: activeList.filter(w => w.status === 'completed').length,
          failed: activeList.filter(w => w.status === 'failed').length,
        },
      },
      performanceTargets: {
        triggerDetection: '<5ms',
        workerSpawn: '<50ms',
        maxConcurrent: 10,
      },
    };
  },
};

// Worker dispatch tool
export const hooksWorkerDispatch: MCPTool = {
  name: 'hooks_worker-dispatch',
  description: 'Dispatch a background worker for analysis/optimization tasks Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      trigger: {
        type: 'string',
        description: 'Worker trigger type',
        enum: ['ultralearn', 'optimize', 'consolidate', 'predict', 'audit', 'map', 'preload', 'deepdive', 'document', 'refactor', 'benchmark', 'testgaps'],
      },
      context: { type: 'string', description: 'Context for the worker (file path, topic, etc.)' },
      priority: { type: 'string', description: 'Priority (low, normal, high, critical)' },
      background: { type: 'boolean', description: 'Run in background (non-blocking)' },
    },
    required: ['trigger'],
  },
  handler: async (params: Record<string, unknown>) => {
    const trigger = params.trigger as WorkerTrigger;
    const context = (params.context as string) || 'default';
    const priority = (params.priority as string) || WORKER_CONFIGS[trigger]?.priority || 'normal';
    const background = params.background !== false;

    if (params.context) { const v = validateText(params.context as string, 'context'); if (!v.valid) return { success: false, error: v.error }; }

    if (!WORKER_CONFIGS[trigger]) {
      return {
        success: false,
        error: `Unknown worker trigger: ${trigger}`,
        availableTriggers: Object.keys(WORKER_CONFIGS),
      };
    }

    const workerId = `worker_${trigger}_${++workerIdCounter}_${Date.now().toString(36)}`;
    const config = WORKER_CONFIGS[trigger];

    // ADR-093 F2: stop returning status:"completed" for a worker that
    // never ran (#1700 item 1). Detect daemon presence via PID file and
    // surface honest verdicts (`no-daemon` / `queued` / `synthetic`).
    const cwd = getProjectCwd();
    const pidFile = join(cwd, '.claude-flow', 'daemon.pid');
    let daemonPid: number | null = null;
    let daemonAlive = false;
    if (existsSync(pidFile)) {
      try {
        const raw = readFileSync(pidFile, 'utf-8').trim();
        const pid = parseInt(raw, 10);
        if (Number.isFinite(pid) && pid > 0) {
          daemonPid = pid;
          try { process.kill(pid, 0); daemonAlive = true; } catch { daemonAlive = false; }
        }
      } catch { /* unreadable PID file */ }
    }

    const worker: {
      id: string;
      trigger: WorkerTrigger;
      context: string;
      status: 'pending' | 'running' | 'completed' | 'failed';
      progress: number;
      phase: string;
      startedAt: Date;
      completedAt?: Date;
    } = {
      id: workerId,
      trigger,
      context,
      status: daemonAlive ? 'pending' : 'pending',
      progress: 0,
      phase: 'initializing',
      startedAt: new Date(),
    };

    activeWorkers.set(workerId, worker);

    // Determine honest status
    let reportedStatus: 'queued' | 'no-daemon' | 'synthetic-completed' | 'mcp-only';
    let note = '';
    if (!daemonAlive) {
      reportedStatus = 'no-daemon';
      note = 'No worker daemon detected. Run `claude-flow daemon start` to enable real worker execution. The dispatch was recorded in-process but no actual work will run.';
    } else if (background) {
      // #1845: write a durable queue file the daemon polls every 5s. Until
      // 3.7.0-alpha.11 the dispatch only updated a process-local Map that
      // the daemon (separate process) could never see, so `queued` was a
      // lie. The queue file makes it real and inspectable on disk.
      const queueDir = join(cwd, '.claude-flow', 'daemon-queue');
      const queuePath = join(queueDir, `${workerId}.json`);
      let queueWritten = false;
      try {
        if (!existsSync(queueDir)) mkdirSync(queueDir, { recursive: true });
        writeFileSync(
          queuePath,
          JSON.stringify({ workerId, trigger, context, priority, enqueuedAt: new Date().toISOString() }, null, 2),
        );
        queueWritten = true;
      } catch (err) {
        // Filesystem error — fall back to mcp-only status so we never
        // claim queued without proof.
        note = `Daemon detected (pid ${daemonPid}) but queue write to ${queuePath} failed: ${(err as Error).message}. Worker recorded in-process only; use \`ruflo daemon trigger -w ${trigger}\` to run synchronously.`;
      }
      if (queueWritten) {
        reportedStatus = 'queued';
        note = `Worker queued for daemon (pid ${daemonPid}) at ${queuePath}. Daemon polls every 5s; processed entries move to .claude-flow/daemon-queue/.processed/. Poll hooks_worker-status until status === "completed".`;
      } else {
        reportedStatus = 'mcp-only';
      }
    } else {
      // Synchronous mode without a runner — be honest about it
      reportedStatus = 'synthetic-completed';
      worker.progress = 100;
      worker.phase = 'completed';
      worker.status = 'completed';
      worker.completedAt = new Date();
      note = 'Synchronous mode: worker record marked completed but no real work executed (no in-process runner). Use background:true with the daemon for real execution.';
    }

    return {
      success: true,
      workerId,
      trigger,
      context,
      priority,
      config: {
        description: config.description,
        estimatedDuration: config.estimatedDuration,
        capabilities: config.capabilities,
      },
      status: reportedStatus,
      daemonAlive,
      daemonPid: daemonAlive ? daemonPid : null,
      background,
      note,
      timestamp: new Date().toISOString(),
    };
  },
};

// Worker status tool
export const hooksWorkerStatus: MCPTool = {
  name: 'hooks_worker-status',
  description: 'Get status of a specific worker or all active workers Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      workerId: { type: 'string', description: 'Specific worker ID to check' },
      includeCompleted: { type: 'boolean', description: 'Include completed workers' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const workerId = params.workerId as string;
    const includeCompleted = params.includeCompleted !== false;

    if (workerId) { const v = validateIdentifier(workerId, 'workerId'); if (!v.valid) return { success: false, error: v.error }; }

    if (workerId) {
      const worker = activeWorkers.get(workerId);
      if (!worker) {
        return {
          success: false,
          error: `Worker not found: ${workerId}`,
        };
      }
      return {
        success: true,
        worker: {
          ...worker,
          duration: worker.completedAt
            ? worker.completedAt.getTime() - worker.startedAt.getTime()
            : Date.now() - worker.startedAt.getTime(),
        },
      };
    }

    const workers = Array.from(activeWorkers.values())
      .filter(w => includeCompleted || w.status !== 'completed')
      .map(w => ({
        ...w,
        duration: w.completedAt
          ? w.completedAt.getTime() - w.startedAt.getTime()
          : Date.now() - w.startedAt.getTime(),
      }));

    return {
      success: true,
      workers,
      summary: {
        total: workers.length,
        pending: workers.filter(w => w.status === 'pending').length,
        running: workers.filter(w => w.status === 'running').length,
        completed: workers.filter(w => w.status === 'completed').length,
        failed: workers.filter(w => w.status === 'failed').length,
      },
    };
  },
};

// Worker detect tool - detect triggers from prompt
export const hooksWorkerDetect: MCPTool = {
  name: 'hooks_worker-detect',
  description: 'Detect worker triggers from user prompt (for UserPromptSubmit hook) Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      prompt: { type: 'string', description: 'User prompt to analyze' },
      autoDispatch: { type: 'boolean', description: 'Automatically dispatch detected workers' },
      minConfidence: { type: 'number', description: 'Minimum confidence threshold (0-1)' },
    },
    required: ['prompt'],
  },
  handler: async (params: Record<string, unknown>) => {
    const prompt = params.prompt as string;
    const autoDispatch = params.autoDispatch as boolean;
    const minConfidence = (params.minConfidence as number) || 0.5;

    { const v = validateText(prompt, 'prompt'); if (!v.valid) return { success: false, error: v.error }; }

    const detection = detectWorkerTriggers(prompt);

    const result: Record<string, unknown> = {
      prompt: prompt.slice(0, 200) + (prompt.length > 200 ? '...' : ''),
      detection,
      triggersFound: detection.triggers.length,
    };

    if (detection.detected && detection.confidence >= minConfidence) {
      result.triggerDetails = detection.triggers.map(trigger => ({
        trigger,
        ...WORKER_CONFIGS[trigger],
      }));

      if (autoDispatch) {
        const dispatched: string[] = [];
        for (const trigger of detection.triggers) {
          const workerId = `worker_${trigger}_${++workerIdCounter}_${Date.now().toString(36)}`;
          activeWorkers.set(workerId, {
            id: workerId,
            trigger,
            context: prompt.slice(0, 100),
            status: 'running',
            progress: 0,
            phase: 'initializing',
            startedAt: new Date(),
            // hooks_worker-detect has no executor. Unlike its sibling
            // hooks_worker-dispatch, it used to silently fabricate a
            // completed worker after 1.5s. Mark the record and response so
            // callers cannot treat it as real analysis (#4549 automation scan).
            synthetic: true,
            note: 'No executor ran: hooks_worker-detect only records a synthetic worker. Use hooks_worker-dispatch with the daemon for real work.',
          });
          dispatched.push(workerId);

          // Mark worker completion after processing
          setTimeout(() => {
            const w = activeWorkers.get(workerId);
            if (w) {
              w.progress = 100;
              w.phase = 'synthetic-completed';
              w.status = 'completed';
              w.completedAt = new Date();
            }
          }, 1500);
        }
        result.autoDispatched = true;
        result.autoDispatchStatus = 'synthetic-completed';
        result.note = 'No executor ran: these worker records are synthetic placeholders. Use hooks_worker-dispatch with the daemon for real work.';
        result.workerIds = dispatched;
      }
    }

    return result;
  },
};

// Model router - lazy loaded
let modelRouterInstance: Awaited<ReturnType<typeof import('../ruvector/model-router.js').getModelRouter>> | null = null;
async function getModelRouterInstance() {
  if (!modelRouterInstance) {
    try {
      const { getModelRouter } = await import('../ruvector/model-router.js');
      modelRouterInstance = getModelRouter();
    } catch {
      modelRouterInstance = null;
    }
  }
  return modelRouterInstance;
}

// Model route tool - intelligent model selection
export const hooksModelRoute: MCPTool = {
  name: 'hooks_model-route',
  description: 'Route task to optimal Claude model (haiku/sonnet/opus) based on complexity Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'Task description to analyze' },
      preferSpeed: { type: 'boolean', description: 'Prefer faster models when possible' },
      preferCost: { type: 'boolean', description: 'Prefer cheaper models when possible' },
    },
    required: ['task'],
  },
  handler: async (params: Record<string, unknown>) => {
    const task = params.task as string;

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }

    const router = await getModelRouterInstance();

    if (!router) {
      // Fallback to simple heuristic
      const complexity = analyzeComplexityFallback(task);
      return {
        model: complexity > 0.7 ? 'opus' : complexity > 0.4 ? 'sonnet' : 'haiku',
        confidence: 0.7,
        complexity,
        reasoning: 'Fallback heuristic (model router not available)',
        implementation: 'fallback',
      };
    }

    const result = await router.route(task);
    return {
      model: result.model,
      confidence: result.confidence,
      uncertainty: result.uncertainty,
      complexity: result.complexity,
      reasoning: result.reasoning,
      alternatives: result.alternatives,
      inferenceTimeUs: result.inferenceTimeUs,
      costMultiplier: result.costMultiplier,
      // Historical name kept for telemetry / dashboard schema stability.
      // The shipped router is the heuristic + Thompson-bandit described in
      // ruvector/model-router.ts — not a neural network. See #2329.
      implementation: 'heuristic-thompson-bandit',
    };
  },
};

// Model route outcome - record outcome for learning
export const hooksModelOutcome: MCPTool = {
  name: 'hooks_model-outcome',
  description: 'Record model routing outcome for learning Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'Original task' },
      model: { type: 'string', enum: ['haiku', 'sonnet', 'opus'], description: 'Model used' },
      outcome: { type: 'string', enum: ['success', 'failure', 'escalated'], description: 'Task outcome' },
    },
    required: ['task', 'model', 'outcome'],
  },
  handler: async (params: Record<string, unknown>) => {
    const task = params.task as string;
    const model = params.model as 'haiku' | 'sonnet' | 'opus';
    const outcome = params.outcome as 'success' | 'failure' | 'escalated';

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }

    const router = await getModelRouterInstance();
    if (router) {
      router.recordOutcome(task, model, outcome);
    }

    return {
      recorded: true,
      task: task.slice(0, 50),
      model,
      outcome,
      timestamp: new Date().toISOString(),
    };
  },
};

// Model router stats
export const hooksModelStats: MCPTool = {
  name: 'hooks_model-stats',
  description: 'Get model routing statistics Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {},
  },
  handler: async () => {
    const router = await getModelRouterInstance();
    if (!router) {
      return {
        available: false,
        message: 'Model router not initialized',
      };
    }

    const stats = router.getStats();
    return {
      available: true,
      ...stats,
      timestamp: new Date().toISOString(),
    };
  },
};

// Model verify — confidence-gated tier escalation (post-generation).
// The heuristics live in ruvector/output-verifier.ts; this wrapper adds the
// MCP surface and feeds the verdict into the SAME learning stream that
// hooks_model-outcome uses (ModelRouter.recordOutcome → Thompson priors), so
// the router learns from escalations. A dedicated tool (rather than a
// "verify mode" on hooks_model-outcome) was chosen because outcome recording
// is a terminal write while verify is a MID-LOOP decision point that returns
// a verdict the agent acts on — overloading outcome would conflate the two
// and break the route → generate → verify → (escalate) → outcome sequence.
export const hooksModelVerify: MCPTool = {
  name: 'hooks_model-verify',
  description: 'Verify a generated output with CHEAP structural signals ($0, no LLM call) and get an escalation verdict — the post-generation half of confidence-gated tier routing (route → generate → verify → escalate on failure). Checks: empty/truncated output, refusal patterns, degenerate repetition, and real syntax parsing for code/JSON tasks (TypeScript compiler / JSON.parse). Returns {confident, reasons[], suggestedTier, suggestedModel, escalate}. By default the verdict is recorded into the model-routing learning stream (success when confident, escalated when not) so the bandit learns which task shapes the cheap tier fails on. Use when you just generated with the tier hooks_model-route picked and must decide accept-vs-escalate BEFORE acting on the output; accepting cheap-tier output unverified is wrong because structurally unusable results (refusals, truncation, unparseable code) silently propagate downstream, and pre-generation routing alone cannot catch them. Not a semantic-quality judge — it only catches structurally unusable outputs.',
  inputSchema: {
    type: 'object',
    properties: {
      task: { type: 'string', description: 'The task the output was generated for' },
      output: { type: 'string', description: 'The generated output to verify' },
      model: { type: 'string', enum: ['haiku', 'sonnet', 'opus'], description: 'Model that produced the output (drives the escalation ladder; default haiku)' },
      tierUsed: { type: 'number', enum: [1, 2, 3], description: 'Tier that produced the output; derived from model when absent' },
      taskKind: { type: 'string', enum: ['code', 'json', 'text', 'auto'], description: 'Force the task kind; default auto-detect' },
      record: { type: 'boolean', description: 'Record the verdict into the routing learning stream (default true; requires model)' },
    },
    required: ['task', 'output'],
  },
  handler: async (params: Record<string, unknown>) => {
    const task = params.task as string;
    const output = params.output as string;
    const model = params.model as 'haiku' | 'sonnet' | 'opus' | undefined;

    { const v = validateText(task, 'task'); if (!v.valid) return { success: false, error: v.error }; }
    if (typeof output !== 'string') return { success: false, error: 'output must be a string' };

    const { verifyAndEscalate } = await import('../ruvector/output-verifier.js');
    const verdict = await verifyAndEscalate({
      task,
      output,
      model,
      tierUsed: params.tierUsed as 1 | 2 | 3 | undefined,
      taskKind: params.taskKind as 'code' | 'json' | 'text' | 'auto' | undefined,
    });

    // Feed the verdict into the existing outcome/learning stream (same path
    // as hooks_model-outcome) so escalations update the Thompson priors.
    let recorded = false;
    if (params.record !== false && model) {
      const router = await getModelRouterInstance();
      if (router) {
        router.recordOutcome(task, model, verdict.confident ? 'success' : 'escalated');
        recorded = true;
      }
    }

    return {
      ...verdict,
      model: model ?? 'haiku',
      recorded,
      recordedOutcome: recorded ? (verdict.confident ? 'success' : 'escalated') : null,
      timestamp: new Date().toISOString(),
    };
  },
};

// Supported source extensions for codemods.
const CODEMOD_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts']);
const CODEMOD_MAX_FILES = 2000;

function codemodLangForExt(abs: string): 'javascript' | 'typescript' | 'jsx' | 'tsx' {
  const ext = abs.slice(abs.lastIndexOf('.')).toLowerCase();
  if (ext === '.tsx') return 'tsx';
  if (ext === '.jsx') return 'jsx';
  if (ext === '.js' || ext === '.mjs' || ext === '.cjs') return 'javascript';
  return 'typescript';
}

// Deterministic codemod execution — the real Tier-1 path (ADR-143)
export const hooksCodemod: MCPTool = {
  name: 'hooks_codemod',
  description: 'Apply a deterministic, $0 (no-LLM) code transform — the real Tier-1 execution path (ADR-143). Supported intents: var-to-const, remove-console, add-logging. Uses the TypeScript compiler with formatting-preserving edits (comments/whitespace survive). Targets: raw `code` (returns transformed text, writes nothing) | a single `file` | a `files` array | a `glob` pattern (batch — applies the intent across every match in one $0 call). Files are rewritten in place unless `dryRun`. Intents that need reasoning — add-types, add-error-handling, async-await — are NOT supported here; route those to a model via hooks_model-route. Use when hooks_pre-task / hooks_route returned [CODEMOD_AVAILABLE].',
  inputSchema: {
    type: 'object',
    properties: {
      intent: { type: 'string', enum: ['var-to-const', 'remove-console', 'add-logging'], description: 'Deterministic codemod to apply' },
      file: { type: 'string', description: 'Path to a single existing source file to transform in place' },
      files: { type: 'array', items: { type: 'string' }, description: 'Multiple file paths to transform in one batch call' },
      glob: { type: 'string', description: 'Glob pattern (relative to project root, e.g. "src/**/*.ts") — applies the intent to every matching source file' },
      code: { type: 'string', description: 'Raw source to transform instead of files (returns transformed code, writes nothing)' },
      language: { type: 'string', enum: ['javascript', 'typescript', 'jsx', 'tsx'], description: 'Language hint for raw code (default typescript; inferred from extension for files)' },
      dryRun: { type: 'boolean', description: 'Report what would change without writing files' },
    },
    required: ['intent'],
  },
  handler: async (params: Record<string, unknown>) => {
    const intent = params.intent as string;
    const file = params.file as string | undefined;
    const files = Array.isArray(params.files) ? (params.files as string[]) : undefined;
    const glob = params.glob as string | undefined;
    const rawCode = params.code as string | undefined;
    const dryRun = params.dryRun === true;
    const langParam = params.language as string | undefined;

    const { applyCodemod, isDeterministicCodemod } = await import('../ruvector/codemods/engine.js');
    if (!isDeterministicCodemod(intent)) {
      return {
        success: false,
        error: `"${intent}" is not a deterministic codemod. Route it to a model via hooks_model-route (Tier 2/3).`,
      };
    }

    // Mode A: transform raw code (never touches disk)
    if (typeof rawCode === 'string') {
      const language = (langParam as 'javascript' | 'typescript' | 'jsx' | 'tsx') ?? 'typescript';
      const r = applyCodemod(intent, rawCode, { language });
      return {
        success: r.success, intent, mode: 'code', changed: r.changed, edits: r.edits,
        output: r.output, language: r.language, reason: r.reason, cost: 0, tier: 1,
      };
    }

    const cwd = getProjectCwd();

    // Resolve the target file set (single / array / glob), with path containment.
    const resolveTargets = (): { abs: string[]; truncated: boolean; error?: string } => {
      const out = new Set<string>();
      const addRaw = (p: string): string | undefined => {
        const v = validatePath(p, 'path');
        if (!v.valid) return v.error;
        const abs = resolve(cwd, v.sanitized);
        // Anchor containment at a path-separator boundary — an unanchored
        // `abs.startsWith(cwd)` string-prefix check would let an absolute
        // path like `${cwd}-staging/x.ts` pass as "inside" `cwd`. Matches
        // the pattern already used correctly elsewhere in this codebase
        // (@claude-flow/security's `isWithinPrefix`, hooks/workers, plugins/security).
        // KNOWN LIMITATION (disclosed 2026-10-20, not fixed here): this
        // compares the resolved-but-not-realpath'd path, so a symlink
        // inside cwd pointing outside it still escapes containment
        // (CWE-59, a distinct/broader gap than the CWE-22 prefix bug this
        // anchors). Needs fs.realpathSync-based containment as a follow-up.
        if (abs !== cwd && !abs.startsWith(cwd + sep)) return `path escapes project root: ${p}`;
        out.add(abs);
        return undefined;
      };

      if (file) { const e = addRaw(file); if (e) return { abs: [], truncated: false, error: e }; }
      if (files) for (const p of files) { const e = addRaw(p); if (e) return { abs: [], truncated: false, error: e }; }
      if (glob) {
        if (glob.includes('..')) return { abs: [], truncated: false, error: 'glob must not contain ".."' };
        // fs.globSync is Node 22+; @types/node here predates it, so type it locally.
        const globSync = (nodeFs as { globSync?: (p: string, o?: { cwd?: string }) => string[] }).globSync;
        if (typeof globSync !== 'function') {
          return { abs: [], truncated: false, error: 'glob requires Node 22+ (fs.globSync unavailable); pass `files[]` instead' };
        }
        let matches: string[] = [];
        try {
          matches = globSync(glob, { cwd });
        } catch (err) {
          return { abs: [], truncated: false, error: `glob failed: ${(err as Error).message}` };
        }
        for (const m of matches) {
          const abs = resolve(cwd, m);
          if ((abs === cwd || abs.startsWith(cwd + sep)) && CODEMOD_EXTENSIONS.has(abs.slice(abs.lastIndexOf('.')).toLowerCase())) {
            out.add(abs);
          }
        }
      }

      const all = [...out];
      const truncated = all.length > CODEMOD_MAX_FILES;
      return { abs: truncated ? all.slice(0, CODEMOD_MAX_FILES) : all, truncated };
    };

    const targets = resolveTargets();
    if (targets.error) return { success: false, error: targets.error };
    if (targets.abs.length === 0) {
      return { success: false, error: 'No target files. Provide `code`, `file`, `files[]`, or a matching `glob`.' };
    }

    // Apply to each file.
    const results: Array<Record<string, unknown>> = [];
    let filesChanged = 0, totalEdits = 0, failures = 0, skipped = 0;

    for (const abs of targets.abs) {
      const rel = abs.startsWith(cwd) ? abs.slice(cwd.length).replace(/^[/\\]/, '') : abs;
      if (!existsSync(abs)) { results.push({ file: rel, success: false, reason: 'not found' }); failures++; continue; }
      if (!CODEMOD_EXTENSIONS.has(abs.slice(abs.lastIndexOf('.')).toLowerCase())) {
        results.push({ file: rel, success: false, reason: 'unsupported extension' }); skipped++; continue;
      }
      const before = readFileSync(abs, 'utf-8');
      const r = applyCodemod(intent, before, { language: codemodLangForExt(abs) });
      if (!r.success) { results.push({ file: rel, success: false, changed: false, reason: r.reason }); failures++; continue; }
      const written = r.changed && !dryRun;
      if (written) writeFileSync(abs, r.output, 'utf-8');
      if (r.changed) { filesChanged++; totalEdits += r.edits; }
      results.push({ file: rel, success: true, changed: r.changed, edits: r.edits, written });
    }

    const single = targets.abs.length === 1 && !files && !glob;
    return {
      success: failures === 0,
      intent,
      mode: single ? (dryRun ? 'dry-run' : 'file') : (dryRun ? 'batch-dry-run' : 'batch'),
      summary: {
        filesScanned: targets.abs.length,
        filesChanged,
        filesUnchanged: targets.abs.length - filesChanged - failures - skipped,
        totalEdits,
        failures,
        skipped,
        truncatedAt: targets.truncated ? CODEMOD_MAX_FILES : undefined,
      },
      results: results.slice(0, 500),
      resultsTruncated: results.length > 500,
      cost: 0,
      tier: 1,
      timestamp: new Date().toISOString(),
    };
  },
};

// Simple fallback complexity analyzer
function analyzeComplexityFallback(task: string): number {
  const taskLower = task.toLowerCase();

  // High complexity indicators
  const highIndicators = ['architect', 'design', 'refactor', 'security', 'audit', 'complex', 'analyze'];
  const highCount = highIndicators.filter(ind => taskLower.includes(ind)).length;

  // Low complexity indicators
  const lowIndicators = ['simple', 'typo', 'format', 'rename', 'comment'];
  const lowCount = lowIndicators.filter(ind => taskLower.includes(ind)).length;

  // Base on length
  const lengthScore = Math.min(1, task.length / 200);

  return Math.min(1, Math.max(0, 0.3 + highCount * 0.2 - lowCount * 0.15 + lengthScore * 0.2));
}

// Worker cancel tool
export const hooksWorkerCancel: MCPTool = {
  name: 'hooks_worker-cancel',
  description: 'Cancel a running worker Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because you need Ruflo-side state — pattern persistence, neural training signals, model-routing learning, cost tracking, audit chain. For one-off shell commands, plain Bash hooks are fine.',
  inputSchema: {
    type: 'object',
    properties: {
      workerId: { type: 'string', description: 'Worker ID to cancel' },
    },
    required: ['workerId'],
  },
  handler: async (params: Record<string, unknown>) => {
    const workerId = params.workerId as string;

    { const v = validateIdentifier(workerId, 'workerId'); if (!v.valid) return { success: false, error: v.error }; }

    const worker = activeWorkers.get(workerId);

    if (!worker) {
      return {
        success: false,
        error: `Worker not found: ${workerId}`,
      };
    }

    if (worker.status === 'completed' || worker.status === 'failed') {
      return {
        success: false,
        error: `Worker already ${worker.status}`,
      };
    }

    worker.status = 'failed';
    worker.phase = 'cancelled';
    worker.completedAt = new Date();

    return {
      success: true,
      workerId,
      cancelled: true,
      timestamp: new Date().toISOString(),
    };
  },
};

// #1916: the `ruflo hooks teammate-idle` / `ruflo hooks task-completed` CLI
// subcommands (Agent Teams hooks) referenced unregistered tools. Minimal
// acknowledgement handlers with the shapes the CLI expects — auto-assignment
// and pattern-learning are delegated to the task-queue consumer / intelligence
// pipeline (a tracked #1916 follow-up).
export const hooksTeammateIdle: MCPTool = {
  name: 'hooks_teammate-idle',
  description: 'Agent Teams hook — fired when a teammate agent finishes its turn; reports whether a pending task can be auto-assigned. Use when native Task is wrong because you have a persistent multi-agent team with a shared task list and want idle workers picked up automatically rather than re-spawning subagents. For a one-shot Task, native Task is fine. (Auto-assignment is delegated to the task-queue consumer — this acknowledges the event today.)',
  category: 'hooks',
  inputSchema: {
    type: 'object',
    properties: {
      teammateId: { type: 'string', description: 'ID of the idle teammate' },
      teamName: { type: 'string', description: 'Team name' },
      autoAssign: { type: 'boolean', description: 'Auto-assign a pending task if available' },
      checkTaskList: { type: 'boolean', description: 'Consult the shared task list' },
      timestamp: { type: 'number', description: 'Event timestamp (ms)' },
    },
  },
  handler: async (input) => {
    const teammateId = String(input.teammateId ?? '');
    return {
      success: true,
      teammateId,
      action: 'waiting' as const,
      pendingTasks: 0,
      message: 'teammate-idle acknowledged; auto-assignment requires the task-queue consumer (#1916 follow-up)',
    };
  },
};

export const hooksTaskCompleted: MCPTool = {
  name: 'hooks_task-completed',
  description: 'Agent Teams hook — fired when a task is marked complete. Records the completion and, when `trainPatterns:true`, feeds the outcome to the SONA + EWC++ learning pipeline (the same path used by hooks_intelligence trajectory-*). Multiple ways to drive learning exist: (a) call this with trainPatterns:true for a one-step trajectory, (b) use hooks_intelligence trajectory-start/step/end for richer multi-step learning, (c) just record an episode via memory_store if no learning is needed. Each path is honest about what it persists; check the returned `learningPath` field. Use when native TaskUpdate(status:completed) is wrong because the runtime also needs to (i) record the outcome as a learning signal and (ii) emit the standard "task done" pipeline event — TaskUpdate only changes the task row.',
  category: 'hooks',
  inputSchema: {
    type: 'object',
    properties: {
      taskId: { type: 'string', description: 'ID of the completed task' },
      teammateId: { type: 'string', description: 'Teammate that completed it' },
      success: { type: 'boolean', description: 'Whether the task succeeded' },
      quality: { type: 'number', description: 'Quality score 0-1' },
      trainPatterns: { type: 'boolean', description: 'When true, runs the SONA + EWC++ trajectory pipeline on this completion so globalStats.patternsLearned reflects it. When false (default), only records the completion.' },
      notifyLead: { type: 'boolean', description: 'Notify the team lead' },
      content: { type: 'string', description: 'Optional richer task description; used as the trajectory step content when training. Defaults to the taskId.' },
    },
    required: ['taskId'],
  },
  handler: async (input) => {
    const taskId = String(input.taskId ?? '');
    const success = input.success !== false;
    const quality = typeof input.quality === 'number' ? input.quality : (success ? 1 : 0);
    const trainPatterns = input.trainPatterns === true;
    const teammateId = input.teammateId ? String(input.teammateId) : undefined;
    // #2241 (OWASP ASI06 Memory/Context Poisoning) — task content is user-
    // supplied and feeds the SONA learning model. Cap length, strip control
    // chars, and reject obvious prompt-injection sentinels before training.
    const rawContent = typeof input.content === 'string' && input.content.trim()
      ? String(input.content)
      : `Task ${taskId} completed (quality=${quality.toFixed(2)})`;
    const content = rawContent
      // Strip ASCII control chars except newline/tab.
      .replace(/[\x00-\x08\x0B-\x1F\x7F]/g, '')
      // Cap to 4 KB — way over a typical trajectory step, well under a memory bomb.
      .slice(0, 4096);

    let patternsLearned = 0;
    let trajectoriesRecorded = 0;
    let learningPath: 'trajectory-pipeline' | 'recorded-only' = 'recorded-only';
    let learningError: string | undefined;

    if (trainPatterns) {
      // #2245 — actually feed the learning loop. Synthesize a one-step
      // trajectory from {taskId, success, quality} and run it through the
      // same SONA + EWC + globalStats++ path as hooks_intelligence trajectory-end.
      try {
        const intel = await import('../memory/intelligence.js');
        const before = intel.getIntelligenceStats();
        await intel.recordTrajectory(
          [{
            type: 'result',
            content,
            metadata: { taskId, success, quality, teammateId },
            timestamp: Date.now(),
          }],
          success ? 'success' : 'failure',
        );
        const after = intel.getIntelligenceStats();
        patternsLearned = Math.max(0, after.patternsLearned - before.patternsLearned);
        trajectoriesRecorded = Math.max(0, after.trajectoriesRecorded - before.trajectoriesRecorded);
        learningPath = 'trajectory-pipeline';
      } catch (err) {
        learningError = (err as Error).message;
        // Fall back to recorded-only — be honest about it.
      }
    }

    const note = trainPatterns
      ? (learningPath === 'trajectory-pipeline'
        ? `Trained via SONA + EWC++ trajectory pipeline (verdict=${success ? 'success' : 'failure'}, patternsLearned=${patternsLearned}, trajectoriesRecorded=${trajectoriesRecorded}).`
        : `trainPatterns=true but the trajectory pipeline failed (${learningError ?? 'unknown error'}). Completion recorded only.`)
      : 'Completion recorded only. Pass trainPatterns:true (or use hooks_intelligence trajectory-* directly) to feed the learning loop.';

    return {
      success: true,
      taskId,
      patternsLearned,
      trajectoriesRecorded,
      learningPath,                  // 'trajectory-pipeline' | 'recorded-only'
      leadNotified: input.notifyLead === true,
      metrics: { duration: 0, quality, learningUpdates: patternsLearned },
      ...(learningError ? { learningError } : {}),
      note,
    };
  },
};

/**
 * Unified learning-stats aggregator MCP tool (#2245 → ADR-075).
 *
 * One honest call across the four historical stat sources — every sub-view
 * names its store and a `consistency` block flags relationships that drift.
 */
export const hooksIntelligenceUnifiedStats: MCPTool = {
  name: 'hooks_intelligence_unified-stats',
  description: 'One honest view across the four learning stat sources: globalStats (`.claude-flow/neural/stats.json`), the in-memory SONA coordinator, memory-bridge AgentDB entries, and the neural-patterns store. Each sub-view names its source path. The `consistency` block notes cross-store drift (e.g. globalStats reports N patterns but neural_patterns is empty). See ADR-075. Use when calling the four narrow aggregators (`hooks_intelligence stats`, `memory_stats`, `neural_status`, the SONA coordinator getter) one at a time is wrong because they each see only their own slice and cross-store drift goes silent — this tool surfaces that drift in the `consistency` block, which the narrow APIs cannot.',
  category: 'hooks',
  inputSchema: {
    type: 'object',
    properties: {
      verbose: { type: 'boolean', description: 'Include extended breakdowns', default: true },
    },
  },
  handler: async (_input: Record<string, unknown>) => {
    const intel = await import('../memory/intelligence.js');
    return intel.getUnifiedLearningStats();
  },
};

/**
 * #4581 / ADR-502: ingest the ruflo-mods learning loop's verified outcomes.
 * Never throws; null when the module or SONA is unavailable.
 */
async function ingestVerifiedQuietly(force = false) {
  try {
    const { ingestVerifiedOutcomes } = await import('../memory/verified-outcomes.js');
    const sona = await getSONAOptimizer();
    return await ingestVerifiedOutcomes({ force, sona: sona as never });
  } catch {
    return null;
  }
}

export const hooksVerifiedIngest: MCPTool = {
  name: 'hooks_intelligence_verified-ingest',
  description: 'Ingest the ruflo-mods learning loop\'s recorded turns (.claude-flow/data/verified-trajectories.jsonl, #4581): turns a test, build or typecheck process verified train SONA; unverified turns are counted and never train. Writes .claude-flow/data/learned-lessons.json (patterns at or above the 0.6 routing gate, plus repeated verified failures as cautions). hooks_route and hooks_session-end already ingest automatically; call this to see the counts or to force a re-read. Use when native Bash hooks (via Claude Code\'s settings.json) are wrong because the outcome must reach Ruflo-side state — SONA pattern persistence and routing — and must be gated on a verifier\'s exit status rather than recorded unconditionally.',
  inputSchema: {
    type: 'object',
    properties: {
      dryRun: { type: 'boolean', description: 'Count only: train nothing and write nothing' },
      force: { type: 'boolean', description: 'Re-read the file even if it has not changed since the last ingest' },
    },
  },
  handler: async (params: Record<string, unknown>) => {
    const { ingestVerifiedOutcomes } = await import('../memory/verified-outcomes.js');
    const sona = await getSONAOptimizer();
    // ADR-503 §2.3: only this explicit ingest may re-verify (run project code), and only when learningLoop.reverify is on.
    const result = await ingestVerifiedOutcomes({ dryRun: params.dryRun === true, force: params.force === true || params.dryRun === true, sona: sona as never, allowReverify: true });
    return { success: true, ...result };
  },
};

// Export all hooks tools
export const hooksTools: MCPTool[] = [
  hooksIntelligenceUnifiedStats,
  hooksTeammateIdle,
  hooksTaskCompleted,
  hooksPreEdit,
  hooksPostEdit,
  hooksPreCommand,
  hooksPostCommand,
  hooksRoute,
  hooksMetrics,
  hooksList,
  hooksPreTask,
  hooksPostTask,
  // New hooks
  hooksExplain,
  hooksPretrain,
  hooksBuildAgents,
  hooksTransfer,
  hooksSessionStart,
  hooksSessionEnd,
  hooksSessionRestore,
  hooksNotify,
  hooksInit,
  hooksIntelligence,
  hooksIntelligenceReset,
  hooksTrajectoryStart,
  hooksTrajectoryStep,
  hooksTrajectoryEnd,
  hooksVerifiedIngest,
  hooksPatternStore,
  hooksPatternSearch,
  hooksIntelligenceStats,
  hooksIntelligenceLearn,
  hooksIntelligenceAttention,
  // Worker tools
  hooksWorkerList,
  hooksWorkerDispatch,
  hooksWorkerStatus,
  hooksWorkerDetect,
  hooksWorkerCancel,
  // Model routing tools
  hooksModelRoute,
  hooksModelOutcome,
  hooksModelStats,
  hooksModelVerify,
  // Deterministic Tier-1 codemod execution (ADR-143)
  hooksCodemod,
];

export default hooksTools;