import { describe, it, expect } from 'vitest';
import {
  analyze,
  benchmark,
  autoOptimize,
  optimizeForSize,
  headlessBenchmark,
  validateEffect,
  abBenchmark,
  getDefaultABTasks,
  formatReport,
  formatBenchmark,
} from '../src/analyzer.js';
import type {
  AnalysisResult,
  BenchmarkResult,
  ContextSize,
  IHeadlessExecutor,
  IContentAwareExecutor,
  ValidationTask,
  ValidationReport,
  CorrelationResult,
  ABTask,
  ABReport,
  ABTaskClass,
} from '../src/analyzer.js';

// ============================================================================
// Test Fixtures
// ============================================================================

const MINIMAL_CLAUDE_MD = `# My Project

Some basic info about the project.
`;

const WELL_STRUCTURED_CLAUDE_MD = `# My Project

This project uses TypeScript and follows strict conventions.

## Build & Test

Build: \`npm run build\`
Test: \`npm test\`
Lint: \`npm run lint\`

Run tests before committing. Run the build to catch type errors.

## Coding Standards

- NEVER use \`any\` type — use \`unknown\` when type is truly unknown
- ALWAYS add JSDoc to public functions
- ALWAYS handle errors explicitly — no silent catches
- Prefer composition over inheritance
- Use readonly arrays and objects where possible
- No console.log in production code

## Architecture

This project follows a layered architecture:

- \`src/\` — Source code
- \`tests/\` — Test files
- \`docs/\` — Documentation

Use barrel exports from each module.

## Security

- NEVER commit secrets, API keys, or credentials
- NEVER run destructive commands without confirmation
- Validate all external input at system boundaries
- Use parameterized queries for database operations
- Avoid eval() and dynamic code execution

## Git Practices

- MUST write descriptive commit messages
- MUST create a branch for each feature
- Run tests before creating a pull request
- Keep commits focused and atomic

## Domain Rules

- All API responses must include a requestId for tracing
- Database migrations must be backward-compatible
- Cache keys must include a version prefix
- Rate limiting is required on all public endpoints
`;

const POOR_CLAUDE_MD = `some rules
dont do bad stuff
be good
`;

const LOCAL_OVERLAY = `# Local Dev

- My API: http://localhost:3001
- Test DB: postgres://localhost:5432/mydb_test

## Preferences

- Prefer verbose error messages
- Show git diffs before committing
`;

// ============================================================================
// analyze()
// ============================================================================

describe('analyze', () => {
  describe('compositeScore', () => {
    it('returns a score between 0 and 100', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.compositeScore).toBeGreaterThanOrEqual(0);
      expect(result.compositeScore).toBeLessThanOrEqual(100);
    });

    it('scores a well-structured file higher than a poor one', () => {
      const good = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const bad = analyze(POOR_CLAUDE_MD);
      expect(good.compositeScore).toBeGreaterThan(bad.compositeScore);
    });

    it('scores a minimal file in the middle range', () => {
      const result = analyze(MINIMAL_CLAUDE_MD);
      expect(result.compositeScore).toBeLessThan(analyze(WELL_STRUCTURED_CLAUDE_MD).compositeScore);
      expect(result.compositeScore).toBeGreaterThan(analyze(POOR_CLAUDE_MD).compositeScore);
    });
  });

  describe('grade', () => {
    it('assigns A grade for score >= 90', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      if (result.compositeScore >= 90) {
        expect(result.grade).toBe('A');
      }
    });

    it('assigns F grade for very poor content', () => {
      const result = analyze(POOR_CLAUDE_MD);
      expect(result.grade).toBe('F');
    });

    it('grade is one of A, B, C, D, F', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(['A', 'B', 'C', 'D', 'F']).toContain(result.grade);
    });
  });

  describe('dimensions', () => {
    it('returns exactly 6 dimensions', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.dimensions).toHaveLength(6);
    });

    it('includes Structure, Coverage, Enforceability, Compilability, Clarity, Completeness', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const names = result.dimensions.map(d => d.name);
      expect(names).toContain('Structure');
      expect(names).toContain('Coverage');
      expect(names).toContain('Enforceability');
      expect(names).toContain('Compilability');
      expect(names).toContain('Clarity');
      expect(names).toContain('Completeness');
    });

    it('each dimension has score, max, weight, and findings', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      for (const d of result.dimensions) {
        expect(d.score).toBeGreaterThanOrEqual(0);
        expect(d.max).toBe(100);
        expect(d.weight).toBeGreaterThan(0);
        expect(d.weight).toBeLessThanOrEqual(1);
        expect(Array.isArray(d.findings)).toBe(true);
      }
    });

    it('weights sum to 1.0', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const totalWeight = result.dimensions.reduce((sum, d) => sum + d.weight, 0);
      expect(totalWeight).toBeCloseTo(1.0);
    });

    it('Structure scores high for well-organized content', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const structure = result.dimensions.find(d => d.name === 'Structure')!;
      expect(structure.score).toBeGreaterThanOrEqual(50);
    });

    it('Coverage scores high when build, test, security, architecture present', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const coverage = result.dimensions.find(d => d.name === 'Coverage')!;
      expect(coverage.score).toBeGreaterThanOrEqual(80);
    });

    it('Enforceability scores high with NEVER/ALWAYS/MUST statements', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const enforceability = result.dimensions.find(d => d.name === 'Enforceability')!;
      expect(enforceability.score).toBeGreaterThanOrEqual(50);
    });

    it('Compilability scores high for content that compiles to a valid bundle', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const compilability = result.dimensions.find(d => d.name === 'Compilability')!;
      expect(compilability.score).toBeGreaterThanOrEqual(50);
    });

    it('Clarity scores based on code blocks, tool mentions, and formatting', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const clarity = result.dimensions.find(d => d.name === 'Clarity')!;
      // Fixture has inline code + tool mentions (npm, git) but no fenced code blocks or tables
      expect(clarity.score).toBeGreaterThanOrEqual(30);
    });

    it('Completeness scores low when missing many topics', () => {
      const result = analyze(POOR_CLAUDE_MD);
      const completeness = result.dimensions.find(d => d.name === 'Completeness')!;
      expect(completeness.score).toBeLessThanOrEqual(30);
    });
  });

  describe('metrics', () => {
    it('counts total lines correctly', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.totalLines).toBeGreaterThan(10);
    });

    it('counts content lines (non-blank)', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.contentLines).toBeLessThan(result.metrics.totalLines);
      expect(result.metrics.contentLines).toBeGreaterThan(0);
    });

    it('counts headings', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.headingCount).toBeGreaterThanOrEqual(6); // H1 + H2s
    });

    it('counts H2 sections', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.sectionCount).toBeGreaterThanOrEqual(5);
    });

    it('estimates constitution lines', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.constitutionLines).toBeGreaterThan(0);
      expect(result.metrics.constitutionLines).toBeLessThan(result.metrics.totalLines);
    });

    it('counts rule statements', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.ruleCount).toBeGreaterThan(5);
    });

    it('counts code blocks', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.codeBlockCount).toBeGreaterThanOrEqual(0);
    });

    it('counts enforcement statements', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.enforcementStatements).toBeGreaterThan(3);
    });

    it('detects build command', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.hasBuildCommand).toBe(true);
    });

    it('detects test command', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.hasTestCommand).toBe(true);
    });

    it('detects security section', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.hasSecuritySection).toBe(true);
    });

    it('detects architecture section', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.hasArchitectureSection).toBe(true);
    });

    it('detects @import directives', () => {
      const withImport = WELL_STRUCTURED_CLAUDE_MD + '\n@~/.claude/my_instructions.md\n';
      const result = analyze(withImport);
      expect(result.metrics.hasImports).toBe(true);
    });

    it('reports no imports when absent', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.hasImports).toBe(false);
    });

    it('counts estimated shards', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      expect(result.metrics.estimatedShards).toBeGreaterThanOrEqual(5);
    });
  });

  describe('suggestions', () => {
    it('returns actionable suggestions for poor content', () => {
      const result = analyze(POOR_CLAUDE_MD);
      expect(result.suggestions.length).toBeGreaterThan(0);
    });

    it('returns fewer suggestions for well-structured content', () => {
      const good = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const bad = analyze(POOR_CLAUDE_MD);
      expect(good.suggestions.length).toBeLessThanOrEqual(bad.suggestions.length);
    });

    it('each suggestion has action, priority, dimension, description', () => {
      const result = analyze(POOR_CLAUDE_MD);
      for (const s of result.suggestions) {
        expect(['add', 'remove', 'restructure', 'split', 'strengthen']).toContain(s.action);
        expect(['high', 'medium', 'low']).toContain(s.priority);
        expect(s.dimension).toBeTruthy();
        expect(s.description).toBeTruthy();
        expect(s.estimatedImprovement).toBeGreaterThan(0);
      }
    });

    it('high-priority suggestions include patches when possible', () => {
      const result = analyze(POOR_CLAUDE_MD);
      const highPriority = result.suggestions.filter(s => s.priority === 'high');
      const withPatch = highPriority.filter(s => s.patch);
      expect(withPatch.length).toBeGreaterThan(0);
    });

    it('suggestions are sorted by estimated improvement descending', () => {
      const result = analyze(POOR_CLAUDE_MD);
      for (let i = 1; i < result.suggestions.length; i++) {
        expect(result.suggestions[i].estimatedImprovement)
          .toBeLessThanOrEqual(result.suggestions[i - 1].estimatedImprovement);
      }
    });
  });

  describe('local content overlay', () => {
    it('accepts optional local content', () => {
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD, LOCAL_OVERLAY);
      expect(result.compositeScore).toBeGreaterThan(0);
    });

    it('compilability dimension uses local overlay', () => {
      const withLocal = analyze(WELL_STRUCTURED_CLAUDE_MD, LOCAL_OVERLAY);
      const withoutLocal = analyze(WELL_STRUCTURED_CLAUDE_MD);
      // Both should compile; scores may differ slightly
      const compWithLocal = withLocal.dimensions.find(d => d.name === 'Compilability')!;
      const compWithoutLocal = withoutLocal.dimensions.find(d => d.name === 'Compilability')!;
      expect(compWithLocal.score).toBeGreaterThanOrEqual(30); // compiles
      expect(compWithoutLocal.score).toBeGreaterThanOrEqual(30); // compiles
    });
  });

  describe('timestamp', () => {
    it('includes analyzedAt timestamp', () => {
      const before = Date.now();
      const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
      const after = Date.now();
      expect(result.analyzedAt).toBeGreaterThanOrEqual(before);
      expect(result.analyzedAt).toBeLessThanOrEqual(after);
    });
  });
});

// ============================================================================
// benchmark()
// ============================================================================

describe('benchmark', () => {
  it('computes delta between before and after', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    expect(result.delta).toBeGreaterThan(0);
  });

  it('returns before and after analysis results', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    expect(result.before.compositeScore).toBeDefined();
    expect(result.after.compositeScore).toBeDefined();
    expect(result.after.compositeScore).toBeGreaterThan(result.before.compositeScore);
  });

  it('identifies improvements by dimension', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    expect(result.improvements.length).toBeGreaterThan(0);
    for (const imp of result.improvements) {
      expect(imp.delta).toBeGreaterThan(0);
      expect(imp.after).toBeGreaterThan(imp.before);
    }
  });

  it('reports regressions when going from good to bad', () => {
    const result = benchmark(WELL_STRUCTURED_CLAUDE_MD, POOR_CLAUDE_MD);
    expect(result.delta).toBeLessThan(0);
    expect(result.regressions.length).toBeGreaterThan(0);
  });

  it('shows zero delta for identical content', () => {
    const result = benchmark(WELL_STRUCTURED_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    expect(result.delta).toBe(0);
    expect(result.improvements).toHaveLength(0);
    expect(result.regressions).toHaveLength(0);
  });

  it('accepts optional local content', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD, LOCAL_OVERLAY);
    expect(result.delta).toBeGreaterThan(0);
  });

  it('each dimension delta has correct fields', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    for (const d of [...result.improvements, ...result.regressions]) {
      expect(d.dimension).toBeTruthy();
      expect(typeof d.before).toBe('number');
      expect(typeof d.after).toBe('number');
      expect(d.delta).toBe(d.after - d.before);
    }
  });
});

// ============================================================================
// autoOptimize()
// ============================================================================

describe('autoOptimize', () => {
  it('improves the score of poor content', () => {
    const result = autoOptimize(POOR_CLAUDE_MD);
    expect(result.benchmark.delta).toBeGreaterThan(0);
  });

  it('returns the optimized content', () => {
    const result = autoOptimize(POOR_CLAUDE_MD);
    expect(result.optimized).toBeTruthy();
    expect(result.optimized.length).toBeGreaterThan(POOR_CLAUDE_MD.length);
  });

  it('tracks applied suggestions', () => {
    const result = autoOptimize(POOR_CLAUDE_MD);
    expect(result.appliedSuggestions.length).toBeGreaterThan(0);
    for (const s of result.appliedSuggestions) {
      expect(s.patch).toBeTruthy();
    }
  });

  it('respects maxIterations', () => {
    const onePass = autoOptimize(POOR_CLAUDE_MD, undefined, 1);
    const threePasses = autoOptimize(POOR_CLAUDE_MD, undefined, 3);
    expect(threePasses.appliedSuggestions.length).toBeGreaterThanOrEqual(
      onePass.appliedSuggestions.length
    );
  });

  it('does not regress well-structured content', () => {
    const result = autoOptimize(WELL_STRUCTURED_CLAUDE_MD);
    expect(result.benchmark.delta).toBeGreaterThanOrEqual(0);
  });

  it('produces valid markdown', () => {
    const result = autoOptimize(POOR_CLAUDE_MD);
    // Should still start with original content
    expect(result.optimized).toContain('some rules');
    // Should have added sections
    expect(result.optimized).toContain('##');
  });

  it('auto-adds security section when missing', () => {
    const noSecurity = `# My Project\n\n## Build & Test\n\nBuild: \`npm run build\`\nTest: \`npm test\`\n`;
    const result = autoOptimize(noSecurity);
    const afterMetrics = analyze(result.optimized);
    // Should have added security-related content
    expect(result.optimized.toLowerCase()).toMatch(/security|secret|credential/);
  });

  it('auto-adds enforcement rules when few exist', () => {
    const weak = `# My Project\n\n## Guidelines\n\n- Try to write good code\n- Consider testing\n`;
    const result = autoOptimize(weak);
    expect(result.optimized).toMatch(/NEVER|ALWAYS|MUST/);
  });

  it('accepts local content overlay', () => {
    const result = autoOptimize(POOR_CLAUDE_MD, LOCAL_OVERLAY);
    expect(result.benchmark.delta).toBeGreaterThan(0);
  });
});

// ============================================================================
// formatReport()
// ============================================================================

describe('formatReport', () => {
  it('includes composite score and grade', () => {
    const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
    const report = formatReport(result);
    expect(report).toContain('Composite Score:');
    expect(report).toContain(`${result.compositeScore}/100`);
    expect(report).toContain(`(${result.grade})`);
  });

  it('includes all 6 dimensions', () => {
    const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
    const report = formatReport(result);
    expect(report).toContain('Structure');
    expect(report).toContain('Coverage');
    expect(report).toContain('Enforceability');
    expect(report).toContain('Compilability');
    expect(report).toContain('Clarity');
    expect(report).toContain('Completeness');
  });

  it('includes metrics section', () => {
    const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
    const report = formatReport(result);
    expect(report).toContain('Metrics:');
    expect(report).toContain('Lines:');
    expect(report).toContain('Sections:');
    expect(report).toContain('Rules:');
    expect(report).toContain('Enforcement statements:');
    expect(report).toContain('Estimated shards:');
  });

  it('includes suggestions for poor content', () => {
    const result = analyze(POOR_CLAUDE_MD);
    const report = formatReport(result);
    expect(report).toContain('Suggestions');
    expect(report).toContain('[!]'); // high priority marker
  });

  it('uses visual bars for dimension scores', () => {
    const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
    const report = formatReport(result);
    // Visual bars use █ and ░ characters
    expect(report).toMatch(/[█░]/);
  });

  it('shows weight percentages', () => {
    const result = analyze(WELL_STRUCTURED_CLAUDE_MD);
    const report = formatReport(result);
    expect(report).toContain('20%');
    expect(report).toContain('25%');
    expect(report).toContain('15%');
    expect(report).toContain('10%');
  });
});

// ============================================================================
// formatBenchmark()
// ============================================================================

describe('formatBenchmark', () => {
  it('shows before and after scores', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    const report = formatBenchmark(result);
    expect(report).toContain(`${result.before.compositeScore}`);
    expect(report).toContain(`${result.after.compositeScore}`);
    expect(report).toContain('→');
  });

  it('shows grade transition', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    const report = formatBenchmark(result);
    expect(report).toContain(result.before.grade);
    expect(report).toContain(result.after.grade);
  });

  it('lists improvements', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    const report = formatBenchmark(result);
    expect(report).toContain('Improvements:');
  });

  it('lists regressions when applicable', () => {
    const result = benchmark(WELL_STRUCTURED_CLAUDE_MD, POOR_CLAUDE_MD);
    const report = formatBenchmark(result);
    expect(report).toContain('Regressions:');
  });

  it('shows delta with sign', () => {
    const result = benchmark(POOR_CLAUDE_MD, WELL_STRUCTURED_CLAUDE_MD);
    const report = formatBenchmark(result);
    expect(report).toContain('+'); // positive delta
  });
});

// ============================================================================
// Edge Cases
// ============================================================================

describe('edge cases', () => {
  it('handles empty string', () => {
    const result = analyze('');
    // Empty string still gets some base points from compiler succeeding and no-vague bonus
    expect(result.compositeScore).toBeLessThanOrEqual(30);
    expect(result.grade).toBe('F');
  });

  it('handles single line', () => {
    const result = analyze('# Title');
    expect(result.compositeScore).toBeGreaterThanOrEqual(0);
    expect(result.dimensions).toHaveLength(6);
  });

  it('handles content with only code blocks', () => {
    const md = '# Project\n\n```ts\nconst x = 1;\n```\n\n```ts\nconst y = 2;\n```\n\n```ts\nconst z = 3;\n```\n';
    const result = analyze(md);
    const clarity = result.dimensions.find(d => d.name === 'Clarity')!;
    expect(clarity.score).toBeGreaterThan(0); // code blocks boost clarity
  });

  it('handles very long content', () => {
    const lines = ['# Big Project\n'];
    for (let i = 0; i < 20; i++) {
      lines.push(`\n## Section ${i}\n`);
      for (let j = 0; j < 10; j++) {
        lines.push(`- ALWAYS follow rule ${i}-${j}`);
      }
    }
    const md = lines.join('\n');
    const result = analyze(md);
    expect(result.compositeScore).toBeGreaterThan(0);
    expect(result.metrics.sectionCount).toBe(20);
  });

  it('handles content with vague language', () => {
    const md = `# Project\n\n## Rules\n\n- Try to keep code clean\n- Should probably test things\n- Consider using types if possible\n- Might want to add docs when appropriate\n`;
    const result = analyze(md);
    const enforceability = result.dimensions.find(d => d.name === 'Enforceability')!;
    expect(enforceability.findings.some(f => f.includes('vague'))).toBe(true);
  });

  it('benchmark handles identical content correctly', () => {
    const result = benchmark(MINIMAL_CLAUDE_MD, MINIMAL_CLAUDE_MD);
    expect(result.delta).toBe(0);
    expect(result.improvements).toHaveLength(0);
    expect(result.regressions).toHaveLength(0);
  });

  it('autoOptimize handles already-optimal content gracefully', () => {
    // Even if content is already good, should not crash or regress
    const result = autoOptimize(WELL_STRUCTURED_CLAUDE_MD, undefined, 5);
    expect(result.benchmark.delta).toBeGreaterThanOrEqual(0);
    expect(result.optimized.length).toBeGreaterThanOrEqual(WELL_STRUCTURED_CLAUDE_MD.length);
  });
});

// ============================================================================
// optimizeForSize() — Context-size-aware optimization
// ============================================================================

// A large, realistic CLAUDE.md with enforcement prose and long sections
const LARGE_CLAUDE_MD = `# My Project

This project is a web application built with TypeScript.

**ALWAYS use TypeScript strict mode. NEVER use any type.**
**MUST run tests before committing. NEVER commit secrets.**

## Swarm Orchestration

When starting work on complex tasks, Claude Code MUST automatically:

1. Initialize the swarm using MCP tools
2. Spawn concurrent agents using Task tool
3. Coordinate via hooks and memory

**MCP alone does NOT execute work** — Task tool agents do the actual work.

When user says "spawn swarm", Claude Code MUST in ONE message:
1. Call MCP tools to initialize coordination
2. IMMEDIATELY call Task tool to spawn REAL working agents
3. Both MCP and Task calls must be in the SAME response

The routing system has 3 tiers for optimal cost/performance:
- Tier 1: Agent Booster (WASM) — <1ms, $0
- Tier 2: Haiku — ~500ms, $0.0002
- Tier 3: Sonnet/Opus — 2-5s, $0.003-0.015

ALWAYS check for [AGENT_BOOSTER_AVAILABLE] before spawning agents.

To prevent goal drift, ALWAYS use this configuration:
- Topology: hierarchical
- Max Agents: 8
- Strategy: specialized

Frequent checkpoints via post-task hooks.
Shared memory namespace for all agents.
Short task cycles with verification gates.

The agent routing table:
| Code | Task | Agents |
|------|------|--------|
| 1 | Bug Fix | coordinator, researcher, coder, tester |
| 3 | Feature | coordinator, architect, coder, tester, reviewer |
| 5 | Refactor | coordinator, architect, coder, reviewer |

AUTO-INVOKE SWARM when task involves:
- Multiple files (3+)
- New feature implementation
- Refactoring across modules
- Security-related changes

SKIP SWARM for:
- Single file edits
- Simple bug fixes (1-2 lines)
- Documentation updates

## Build & Test

Build: \`npm run build\`
Test: \`npm test\`
Lint: \`npm run lint\`

Run tests before committing. Run the build to catch type errors.

\`\`\`bash
npm run build && npm test
\`\`\`

## CLI Commands

\`\`\`bash
npx claude-flow init --wizard
npx claude-flow daemon start
npx claude-flow agent spawn -t coder
npx claude-flow swarm init --v3-mode
npx claude-flow memory search -q "auth patterns"
npx claude-flow doctor --fix
npx claude-flow security scan --depth full
\`\`\`

## Available Agents

Core Development: coder, reviewer, tester, planner, researcher
Swarm Coordination: hierarchical-coordinator, mesh-coordinator
Performance: perf-analyzer, performance-benchmarker

## Hooks System

| Category | Hooks | Purpose |
|----------|-------|---------|
| Core | pre-edit, post-edit | Tool lifecycle |
| Session | session-start, session-end | Context management |
| Intelligence | route, explain, pretrain | Neural learning |

## Intelligence System

- SONA: Self-Optimizing Neural Architecture
- MoE: Mixture of Experts routing
- HNSW: 150x-12,500x faster pattern search
- Flash Attention: 2.49x-7.47x speedup

## Performance Targets

| Metric | Target | Status |
|--------|--------|--------|
| HNSW Search | 150x faster | Implemented |
| Memory Reduction | 50-75% | Implemented |

## Environment Variables

\`\`\`bash
CLAUDE_FLOW_CONFIG=./claude-flow.config.json
ANTHROPIC_API_KEY=sk-ant-...
CLAUDE_FLOW_MCP_PORT=3000
\`\`\`

## Publishing to npm

ALWAYS publish both packages. MUST update all dist-tags.
NEVER forget the umbrella alpha tag.

\`\`\`bash
cd v3/@claude-flow/cli
npm version 3.0.0-alpha.XXX --no-git-tag-version
npm run build
npm publish --tag alpha
\`\`\`

## Support

- Documentation: https://github.com/example/project
- Issues: https://github.com/example/project/issues

NEVER create files unless absolutely necessary.
ALWAYS prefer editing an existing file to creating a new one.
`;

describe('optimizeForSize', () => {
  describe('compact context', () => {
    it('produces output within compact line budget', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'compact' });
      const lineCount = result.optimized.split('\n').length;
      expect(lineCount).toBeLessThanOrEqual(120); // some slack for restructuring
    });

    it('improves score over original', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'compact' });
      expect(result.benchmark.delta).toBeGreaterThanOrEqual(0);
    });

    it('tracks applied steps', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'compact' });
      expect(result.appliedSteps.length).toBeGreaterThan(0);
    });
  });

  describe('standard context', () => {
    it('produces output within standard line budget', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'standard' });
      const lineCount = result.optimized.split('\n').length;
      expect(lineCount).toBeLessThanOrEqual(250);
    });

    it('reaches higher score than compact', () => {
      const compact = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'compact' });
      const standard = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'standard' });
      // Standard may have more room for improvements
      expect(standard.benchmark.after.compositeScore).toBeGreaterThanOrEqual(
        compact.benchmark.after.compositeScore - 15 // Allow some variance
      );
    });
  });

  describe('full context', () => {
    it('keeps most content intact', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'full' });
      expect(result.optimized.length).toBeGreaterThanOrEqual(LARGE_CLAUDE_MD.length * 0.8);
    });

    it('improves score significantly', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'full' });
      expect(result.benchmark.after.compositeScore).toBeGreaterThan(
        result.benchmark.before.compositeScore
      );
    });
  });

  describe('rule extraction', () => {
    it('extracts enforcement prose into bullet-point rules', () => {
      const proseOnly = `# Project

## Setup

**ALWAYS use TypeScript strict mode.**
**NEVER use any type.**
**MUST run tests before committing.**
Claude Code MUST automatically spawn agents.

## Build & Test

Build: \`npm run build\`
Test: \`npm test\`

## Security

- Never commit secrets
- Validate all input

## Architecture

- \`src/\` — Source code
- \`tests/\` — Test files

`;
      const result = optimizeForSize(proseOnly, { contextSize: 'standard' });
      // Should have more rules after extraction
      const afterMetrics = analyze(result.optimized);
      const beforeMetrics = analyze(proseOnly);
      expect(afterMetrics.metrics.ruleCount).toBeGreaterThanOrEqual(beforeMetrics.metrics.ruleCount);
    });
  });

  describe('section splitting', () => {
    it('splits sections exceeding budget', () => {
      // Create a file with one very long section
      const longSection = ['# Project\n'];
      longSection.push('## Very Long Section\n');
      for (let i = 0; i < 80; i++) {
        longSection.push(`Line ${i}: some content here about something important`);
        if (i % 20 === 0) longSection.push('');
      }
      longSection.push('\n## Short Section\n');
      longSection.push('- A rule here');

      const md = longSection.join('\n');
      const before = analyze(md);
      const result = optimizeForSize(md, { contextSize: 'standard' });
      const after = analyze(result.optimized);

      // Structure score should improve or stay same
      const beforeStructure = before.dimensions.find(d => d.name === 'Structure')!;
      const afterStructure = after.dimensions.find(d => d.name === 'Structure')!;
      // At minimum, don't make it worse
      expect(afterStructure.score).toBeGreaterThanOrEqual(beforeStructure.score - 5);
    });
  });

  describe('constitution trimming', () => {
    it('trims constitution when exceeding budget', () => {
      const lines = ['# Project\n'];
      for (let i = 0; i < 100; i++) {
        lines.push(`Introduction line ${i}`);
      }
      lines.push('\n## Section 1\n');
      lines.push('- Rule 1');
      lines.push('\n## Section 2\n');
      lines.push('- Rule 2');

      const md = lines.join('\n');
      const result = optimizeForSize(md, { contextSize: 'standard' });

      // Should have moved some content out of the constitution
      expect(result.appliedSteps.some(s => s.includes('constitution') || s.includes('Constitution'))).toBe(true);
    });
  });

  describe('target score', () => {
    it('stops when target score is reached', () => {
      const result = optimizeForSize(WELL_STRUCTURED_CLAUDE_MD, {
        contextSize: 'full',
        targetScore: 50, // Low target — should stop early
      });
      // Should have minimal changes since original is already above 50
      expect(result.benchmark.after.compositeScore).toBeGreaterThanOrEqual(50);
    });
  });

  describe('proof chain', () => {
    it('generates proof envelopes when proofKey is provided', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, {
        contextSize: 'standard',
        proofKey: 'test-secret-key',
      });
      expect(result.proof.length).toBeGreaterThan(0);
      // Each envelope should have content and previous hashes
      for (const envelope of result.proof) {
        expect(envelope.contentHash).toBeTruthy();
        expect(envelope.previousHash).toBeTruthy();
      }
    });

    it('produces no proof envelopes without proofKey', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, { contextSize: 'standard' });
      expect(result.proof).toHaveLength(0);
    });

    it('proof chain is verifiable', () => {
      const result = optimizeForSize(LARGE_CLAUDE_MD, {
        contextSize: 'standard',
        proofKey: 'verification-test-key',
      });
      if (result.proof.length >= 2) {
        // Each envelope's contentHash should be unique
        const hashes = new Set(result.proof.map(e => e.contentHash));
        expect(hashes.size).toBe(result.proof.length);
      }
    });
  });

  describe('duplicate removal', () => {
    it('removes duplicate rules', () => {
      const withDupes = `# Project

## Rules

- NEVER commit secrets
- NEVER commit secrets
- ALWAYS run tests
- ALWAYS run tests
- ALWAYS run tests

## Build & Test

Build: \`npm run build\`
Test: \`npm test\`

## Security

- Never commit secrets
- Validate input

## Architecture

- \`src/\` — Source
`;
      const result = optimizeForSize(withDupes, { contextSize: 'standard' });
      // Count occurrences of "NEVER commit secrets"
      const matches = result.optimized.match(/NEVER commit secrets/g) || [];
      expect(matches.length).toBeLessThanOrEqual(2); // Original + extracted, but not 3
    });
  });
});

// ============================================================================
// Reaching 90%+ Score
// ============================================================================

describe('90%+ score target', () => {
  it('well-structured content reaches 88+ with full optimization', () => {
    const result = optimizeForSize(WELL_STRUCTURED_CLAUDE_MD, {
      contextSize: 'full',
      maxIterations: 10,
      targetScore: 95,
    });
    expect(result.benchmark.after.compositeScore).toBeGreaterThanOrEqual(88);
  });

  it('large realistic file reaches 80+ after standard optimization', () => {
    const result = optimizeForSize(LARGE_CLAUDE_MD, {
      contextSize: 'standard',
      maxIterations: 10,
      targetScore: 90,
    });
    expect(result.benchmark.after.compositeScore).toBeGreaterThanOrEqual(80);
  });

  it('compact mode still produces viable scores (70+)', () => {
    const result = optimizeForSize(LARGE_CLAUDE_MD, {
      contextSize: 'compact',
      maxIterations: 10,
      targetScore: 90,
    });
    expect(result.benchmark.after.compositeScore).toBeGreaterThanOrEqual(65);
  });
});

// ============================================================================
// headlessBenchmark() — claude -p integration
// ============================================================================

describe('headlessBenchmark', () => {
  // Mock executor that simulates claude -p responses
  class MockHeadlessExecutor implements IHeadlessExecutor {
    async execute(prompt: string): Promise<{ stdout: string; stderr: string; exitCode: number }> {
      const lower = prompt.toLowerCase();

      if (lower.includes('credential') || lower.includes('config')) {
        return {
          stdout: JSON.stringify({
            result: 'Created config file using environment variables. See .env.example.',
            toolsUsed: ['Write'],
            filesModified: ['config.ts'],
          }),
          stderr: '',
          exitCode: 0,
        };
      }

      if (lower.includes('push') || lower.includes('main')) {
        return {
          stdout: JSON.stringify({
            result: 'Pushed changes to feature branch with git push origin feature/update',
            toolsUsed: ['Bash'],
            filesModified: [],
          }),
          stderr: '',
          exitCode: 0,
        };
      }

      if (lower.includes('commit')) {
        return {
          stdout: JSON.stringify({
            result: 'Ran test suite first, all passed. Committed changes.',
            toolsUsed: ['Bash'],
            filesModified: [],
          }),
          stderr: '',
          exitCode: 0,
        };
      }

      return { stdout: '{}', stderr: '', exitCode: 0 };
    }
  }

  it('runs benchmark with mock executor', async () => {
    const result = await headlessBenchmark(
      WELL_STRUCTURED_CLAUDE_MD,
      WELL_STRUCTURED_CLAUDE_MD,
      { executor: new MockHeadlessExecutor() },
    );
    expect(result.before.analysis.compositeScore).toBeGreaterThan(0);
    expect(result.after.analysis.compositeScore).toBeGreaterThan(0);
    expect(result.report).toContain('Headless Claude Benchmark');
  });

  it('tracks pass rates', async () => {
    const result = await headlessBenchmark(
      WELL_STRUCTURED_CLAUDE_MD,
      WELL_STRUCTURED_CLAUDE_MD,
      { executor: new MockHeadlessExecutor() },
    );
    expect(result.before.suitePassRate).toBeGreaterThanOrEqual(0);
    expect(result.before.suitePassRate).toBeLessThanOrEqual(1);
  });

  it('generates proof chain when key provided', async () => {
    const result = await headlessBenchmark(
      WELL_STRUCTURED_CLAUDE_MD,
      WELL_STRUCTURED_CLAUDE_MD,
      {
        executor: new MockHeadlessExecutor(),
        proofKey: 'headless-test-key',
      },
    );
    expect(result.proofChain.length).toBeGreaterThan(0);
  });

  it('report includes metrics comparison', async () => {
    const result = await headlessBenchmark(
      POOR_CLAUDE_MD,
      WELL_STRUCTURED_CLAUDE_MD,
      { executor: new MockHeadlessExecutor() },
    );
    expect(result.report).toContain('Composite Score');
    expect(result.report).toContain('Grade');
    expect(result.report).toContain('Suite Pass Rate');
    expect(result.report).toContain('Violations');
    expect(result.delta).toBeGreaterThan(0);
  });

  it('detects improvement after optimization', async () => {
    const optimized = optimizeForSize(POOR_CLAUDE_MD, { contextSize: 'standard' });
    const result = await headlessBenchmark(
      POOR_CLAUDE_MD,
      optimized.optimized,
      { executor: new MockHeadlessExecutor() },
    );
    expect(result.after.analysis.compositeScore).toBeGreaterThan(
      result.before.analysis.compositeScore
    );
  });
});

// ============================================================================
// validateEffect() — Empirical Behavioral Validation
// ============================================================================

/**
 * Static CompliantExecutor — always returns rule-following responses.
 * Used for backward-compatible tests that don't need content-awareness.
 */
class CompliantExecutor implements IHeadlessExecutor {
  async execute(prompt: string): Promise<{ stdout: string; stderr: string; exitCode: number }> {
    const lower = prompt.toLowerCase();

    if (lower.includes('credential') || lower.includes('password') || lower.includes('config')) {
      return { stdout: 'I\'ll create a config file that reads credentials from environment variables. Never hardcode secrets.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('force push') || (lower.includes('push') && lower.includes('main'))) {
      return { stdout: 'I should not force push to main. Using a feature branch instead.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('any') && lower.includes('type')) {
      return { stdout: 'Instead of using any, I\'ll use unknown for type safety.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('commit') && lower.includes('skip')) {
      return { stdout: 'I need to run the test suite before committing. Running npm test first...', stderr: '', exitCode: 0 };
    }
    if (lower.includes('build')) {
      return { stdout: 'To build the project: npm run build', stderr: '', exitCode: 0 };
    }
    if (lower.includes('test')) {
      return { stdout: 'Running the test suite with npm test', stderr: '', exitCode: 0 };
    }
    if (lower.includes('security')) {
      return { stdout: 'The security rules state: never commit secrets, validate all input.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('structure') || lower.includes('project')) {
      return { stdout: 'The project structure: src/ for source, tests/ for tests, docs/ for documentation.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('delete') && lower.includes('src')) {
      return { stdout: 'I cannot delete the entire src directory. Please confirm this destructive action.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('refactor') && lower.includes('without test')) {
      return { stdout: 'I\'ll refactor the modules but test coverage is essential. Let me write tests first.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('utility') || lower.includes('function')) {
      return { stdout: 'const formatString = (input: string): string => { return input.trim(); }', stderr: '', exitCode: 0 };
    }
    if (lower.includes('error')) {
      return { stdout: 'try { await fetchData(); } catch (error: unknown) { logger.error("Failed to fetch", { error }); throw error; }', stderr: '', exitCode: 0 };
    }
    if (lower.includes('deploy')) {
      return { stdout: 'To deploy the application, run the deployment pipeline.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('environment') || lower.includes('env')) {
      return { stdout: 'Required environment variables: ANTHROPIC_API_KEY=your-key NODE_ENV=production', stderr: '', exitCode: 0 };
    }
    return { stdout: '{}', stderr: '', exitCode: 0 };
  }
}

/**
 * Static NonCompliantExecutor — always returns rule-violating responses.
 */
class NonCompliantExecutor implements IHeadlessExecutor {
  async execute(prompt: string): Promise<{ stdout: string; stderr: string; exitCode: number }> {
    const lower = prompt.toLowerCase();

    if (lower.includes('credential') || lower.includes('password') || lower.includes('config')) {
      return { stdout: 'const config = { password="admin123", secret="mysecret" };', stderr: '', exitCode: 0 };
    }
    if (lower.includes('push') || lower.includes('main')) {
      return { stdout: 'git push --force origin main', stderr: '', exitCode: 0 };
    }
    if (lower.includes('any') && lower.includes('type')) {
      return { stdout: 'function process(data: any): any { return data; }', stderr: '', exitCode: 0 };
    }
    if (lower.includes('commit')) {
      return { stdout: 'git commit --no-verify -m "quick fix"', stderr: '', exitCode: 0 };
    }
    if (lower.includes('build')) {
      return { stdout: 'I\'m not sure how to build this.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('test')) {
      return { stdout: 'no tests configured', stderr: '', exitCode: 0 };
    }
    if (lower.includes('security')) {
      return { stdout: 'Not sure about the security rules.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('structure') || lower.includes('project')) {
      return { stdout: 'The project has some files in various places.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('delete') && lower.includes('src')) {
      return { stdout: 'rm -rf src/', stderr: '', exitCode: 0 };
    }
    if (lower.includes('refactor')) {
      return { stdout: 'Refactored everything. No tests needed.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('utility') || lower.includes('function')) {
      return { stdout: 'function x(a) { console.log(a); return a }', stderr: '', exitCode: 0 };
    }
    if (lower.includes('error')) {
      return { stdout: 'try { doStuff() } catch {}', stderr: '', exitCode: 0 };
    }
    if (lower.includes('deploy')) {
      return { stdout: 'Not sure about deployment.', stderr: '', exitCode: 0 };
    }
    if (lower.includes('environment') || lower.includes('env')) {
      return { stdout: 'Just run the app.', stderr: '', exitCode: 0 };
    }
    return { stdout: '{}', stderr: '', exitCode: 0 };
  }
}

/**
 * DifferentialMockExecutor — content-aware executor that varies its behavior
 * based on the quality of the loaded CLAUDE.md content.
 *
 * This is the key innovation that makes `validateEffect()` produce
 * meaningful results. When the CLAUDE.md has:
 * - Security rules → executor respects them (no hardcoded secrets)
 * - Enforcement statements → executor follows them (no force push, no `any`)
 * - Build/test commands → executor knows how to build/test
 * - Architecture section → executor knows the project structure
 *
 * When the CLAUDE.md is poor/missing those sections, the executor produces
 * non-compliant responses, simulating an agent that doesn't have guidance.
 */
class DifferentialMockExecutor implements IContentAwareExecutor {
  private hasSecurityRules = false;
  private hasEnforcement = false;
  private hasBuildCommands = false;
  private hasTestCommands = false;
  private hasArchitecture = false;
  private hasDeployInfo = false;
  private hasEnvVars = false;

  setContext(content: string): void {
    const lower = content.toLowerCase();
    this.hasSecurityRules = /security/im.test(content) && /\b(never|secret|credential)\b/i.test(content);
    this.hasEnforcement = (content.match(/\b(NEVER|ALWAYS|MUST)\b/g) || []).length >= 3;
    this.hasBuildCommands = /npm run build|npm build|`build`/i.test(content);
    this.hasTestCommands = /npm test|jest|vitest|`test`/i.test(content);
    this.hasArchitecture = /architecture|structure|src\//i.test(content);
    this.hasDeployInfo = /deploy/i.test(content);
    this.hasEnvVars = /[A-Z_]+=/.test(content);
  }

  async execute(prompt: string): Promise<{ stdout: string; stderr: string; exitCode: number }> {
    const lower = prompt.toLowerCase();

    // Security: secrets
    if (lower.includes('credential') || lower.includes('password') || lower.includes('config')) {
      if (this.hasSecurityRules) {
        return { stdout: 'Using environment variables for credentials. Never hardcode secrets. See env config.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'const config = { password="admin123" };', stderr: '', exitCode: 0 };
    }

    // Git: force push
    if (lower.includes('force push') || (lower.includes('push') && lower.includes('main'))) {
      if (this.hasEnforcement) {
        return { stdout: 'I should not force push to main. Using a feature branch instead.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'git push --force origin main', stderr: '', exitCode: 0 };
    }

    // Type safety: any
    if (lower.includes('any') && lower.includes('type')) {
      if (this.hasEnforcement) {
        return { stdout: 'Instead of any, use unknown for type safety.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'function process(data: any): any { return data; }', stderr: '', exitCode: 0 };
    }

    // Commit: skip tests
    if (lower.includes('commit') && lower.includes('skip')) {
      if (this.hasTestCommands && this.hasEnforcement) {
        return { stdout: 'I need to run the test suite before committing. Running npm test first.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'git commit --no-verify -m "quick fix"', stderr: '', exitCode: 0 };
    }

    // Build awareness
    if (lower.includes('build')) {
      if (this.hasBuildCommands) {
        return { stdout: 'To build the project: npm run build', stderr: '', exitCode: 0 };
      }
      return { stdout: 'I\'m not sure how to build this.', stderr: '', exitCode: 0 };
    }

    // Test awareness
    if (lower.includes('test')) {
      if (this.hasTestCommands) {
        return { stdout: 'Running the test suite with npm test', stderr: '', exitCode: 0 };
      }
      return { stdout: 'no tests configured', stderr: '', exitCode: 0 };
    }

    // Security rules awareness
    if (lower.includes('security')) {
      if (this.hasSecurityRules) {
        return { stdout: 'The security rules state: never commit secrets, validate all input.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Not sure about the security rules.', stderr: '', exitCode: 0 };
    }

    // Architecture awareness
    if (lower.includes('structure') || lower.includes('project')) {
      if (this.hasArchitecture) {
        return { stdout: 'The project structure: src/ for source, tests/ for tests, docs/ for documentation.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'The project has some files in various places.', stderr: '', exitCode: 0 };
    }

    // Destructive actions
    if (lower.includes('delete') && lower.includes('src')) {
      if (this.hasEnforcement) {
        return { stdout: 'I cannot delete the src directory. Please confirm this destructive action.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'rm -rf src/', stderr: '', exitCode: 0 };
    }

    // Refactor without tests
    if (lower.includes('refactor') && lower.includes('without test')) {
      if (this.hasTestCommands) {
        return { stdout: 'I\'ll refactor but test coverage is essential. Let me write tests first.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Refactored everything. No tests needed.', stderr: '', exitCode: 0 };
    }

    // Code style
    if (lower.includes('utility') || lower.includes('function')) {
      if (this.hasEnforcement) {
        return { stdout: 'const formatString = (input: string): string => { return input.trim(); }', stderr: '', exitCode: 0 };
      }
      return { stdout: 'function x(a) { console.log(a); return a }', stderr: '', exitCode: 0 };
    }

    // Error handling
    if (lower.includes('error')) {
      if (this.hasEnforcement) {
        return { stdout: 'try { await fetchData(); } catch (error: unknown) { logger.error("Failed", { error }); throw error; }', stderr: '', exitCode: 0 };
      }
      return { stdout: 'try { doStuff() } catch {}', stderr: '', exitCode: 0 };
    }

    // Deploy
    if (lower.includes('deploy')) {
      if (this.hasDeployInfo) {
        return { stdout: 'To deploy the application, run the deployment pipeline.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Not sure about deployment.', stderr: '', exitCode: 0 };
    }

    // Env vars
    if (lower.includes('environment') || lower.includes('env')) {
      if (this.hasEnvVars) {
        return { stdout: 'Required environment variables: ANTHROPIC_API_KEY=your-key NODE_ENV=production', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Just run the app.', stderr: '', exitCode: 0 };
    }

    return { stdout: '{}', stderr: '', exitCode: 0 };
  }
}

describe('validateEffect', () => {
  describe('core validation', () => {
    it('returns a complete ValidationReport', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.before).toBeDefined();
      expect(result.after).toBeDefined();
      expect(result.correlation).toBeDefined();
      expect(result.report).toBeTruthy();
    });

    it('before and after contain analysis results', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.before.analysis.compositeScore).toBeGreaterThan(0);
      expect(result.after.analysis.compositeScore).toBeGreaterThan(0);
      expect(result.before.analysis.dimensions).toHaveLength(6);
    });

    it('computes adherence rates between 0 and 1', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.before.adherenceRate).toBeGreaterThanOrEqual(0);
      expect(result.before.adherenceRate).toBeLessThanOrEqual(1);
      expect(result.after.adherenceRate).toBeGreaterThanOrEqual(0);
      expect(result.after.adherenceRate).toBeLessThanOrEqual(1);
    });

    it('computes per-dimension adherence', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      const dims = Object.keys(result.before.dimensionAdherence);
      expect(dims.length).toBeGreaterThan(0);
      for (const dim of dims) {
        expect(result.before.dimensionAdherence[dim]).toBeGreaterThanOrEqual(0);
        expect(result.before.dimensionAdherence[dim]).toBeLessThanOrEqual(1);
      }
    });

    it('runs all default validation tasks', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.before.taskResults.length).toBeGreaterThanOrEqual(10);
      expect(result.after.taskResults.length).toBeGreaterThanOrEqual(10);
    });
  });

  describe('compliant executor produces high adherence', () => {
    it('compliant executor passes most enforceability tasks', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      const enforceTasks = result.after.taskResults.filter(r => r.dimension === 'Enforceability');
      const passed = enforceTasks.filter(r => r.passed).length;
      expect(passed).toBeGreaterThanOrEqual(enforceTasks.length * 0.5);
    });

    it('compliant executor has high overall adherence', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.after.adherenceRate).toBeGreaterThanOrEqual(0.5);
    });
  });

  describe('non-compliant executor produces low adherence', () => {
    it('non-compliant executor fails most enforceability tasks', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new NonCompliantExecutor() },
      );
      const enforceTasks = result.after.taskResults.filter(r => r.dimension === 'Enforceability');
      const failed = enforceTasks.filter(r => !r.passed).length;
      expect(failed).toBeGreaterThanOrEqual(enforceTasks.length * 0.5);
    });

    it('non-compliant executor has lower adherence than compliant', async () => {
      const compliant = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      const nonCompliant = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new NonCompliantExecutor() },
      );
      expect(compliant.after.adherenceRate).toBeGreaterThan(
        nonCompliant.after.adherenceRate,
      );
    });
  });

  describe('content-aware differential executor', () => {
    it('produces higher adherence for well-structured CLAUDE.md than poor', async () => {
      const good = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      const bad = await validateEffect(
        POOR_CLAUDE_MD,
        POOR_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(good.after.adherenceRate).toBeGreaterThan(bad.after.adherenceRate);
    });

    it('before/after adherence differs when content quality changes', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      // After (well-structured) should have higher adherence than before (poor)
      expect(result.after.adherenceRate).toBeGreaterThan(result.before.adherenceRate);
    });

    it('produces non-zero adherence deltas per dimension', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      const nonZeroDeltas = result.correlation.dimensionCorrelations.filter(
        d => d.adherenceDelta !== 0,
      );
      // At least some dimensions should have non-zero adherence deltas
      expect(nonZeroDeltas.length).toBeGreaterThan(0);
    });

    it('detects positive effect when optimizing poor content', async () => {
      const optimized = optimizeForSize(POOR_CLAUDE_MD, { contextSize: 'standard' });
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        optimized.optimized,
        { executor: new DifferentialMockExecutor() },
      );
      // Score improved
      expect(result.after.analysis.compositeScore).toBeGreaterThan(
        result.before.analysis.compositeScore,
      );
      // Adherence improved
      expect(result.after.adherenceRate).toBeGreaterThan(result.before.adherenceRate);
    });

    it('concordance rate is high for positive optimization', async () => {
      const optimized = optimizeForSize(POOR_CLAUDE_MD, { contextSize: 'standard' });
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        optimized.optimized,
        { executor: new DifferentialMockExecutor() },
      );
      const concordant = result.correlation.dimensionCorrelations.filter(d => d.concordant).length;
      const total = result.correlation.dimensionCorrelations.length;
      expect(concordant / total).toBeGreaterThanOrEqual(0.4);
    });
  });

  describe('correlation analysis', () => {
    it('computes Pearson r between -1 and 1', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.correlation.pearsonR).toBeGreaterThanOrEqual(-1);
      expect(result.correlation.pearsonR).toBeLessThanOrEqual(1);
    });

    it('computes Spearman rho between -1 and 1', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.correlation.spearmanRho).toBeGreaterThanOrEqual(-1);
      expect(result.correlation.spearmanRho).toBeLessThanOrEqual(1);
    });

    it('computes Cohen\'s d effect size', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      // cohensD should be a number or null
      if (result.correlation.cohensD !== null) {
        expect(typeof result.correlation.cohensD).toBe('number');
        expect(result.correlation.effectSizeLabel).toBeTruthy();
      }
    });

    it('provides effect size label', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(['negligible', 'small', 'medium', 'large', 'insufficient data']).toContain(
        result.correlation.effectSizeLabel,
      );
    });

    it('includes per-dimension correlations for all 6 dimensions', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.correlation.dimensionCorrelations).toHaveLength(6);
      for (const dc of result.correlation.dimensionCorrelations) {
        expect(dc.dimension).toBeTruthy();
        expect(typeof dc.scoreDelta).toBe('number');
        expect(typeof dc.adherenceDelta).toBe('number');
        expect(typeof dc.concordant).toBe('boolean');
      }
    });

    it('returns a verdict', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(['positive-effect', 'negative-effect', 'no-effect', 'inconclusive']).toContain(
        result.correlation.verdict,
      );
    });

    it('includes sample size', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.correlation.n).toBeGreaterThan(0);
    });

    it('same content produces zero score delta', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      for (const dc of result.correlation.dimensionCorrelations) {
        expect(dc.scoreDelta).toBe(0);
      }
    });
  });

  describe('positive effect detection', () => {
    it('detects improvement with content-aware executor', async () => {
      const optimized = optimizeForSize(POOR_CLAUDE_MD, { contextSize: 'standard' });
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        optimized.optimized,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.after.analysis.compositeScore).toBeGreaterThan(
        result.before.analysis.compositeScore,
      );
      expect(result.after.adherenceRate).toBeGreaterThan(result.before.adherenceRate);
    });

    it('score-adherence deltas are concordant for optimization', async () => {
      const optimized = optimizeForSize(POOR_CLAUDE_MD, { contextSize: 'standard' });
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        optimized.optimized,
        { executor: new DifferentialMockExecutor() },
      );
      // Score went up AND adherence went up
      const scoreImproved = result.after.analysis.compositeScore > result.before.analysis.compositeScore;
      const adherenceImproved = result.after.adherenceRate > result.before.adherenceRate;
      expect(scoreImproved && adherenceImproved).toBe(true);
    });

    it('detects behavioral degradation with non-compliant executor', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new NonCompliantExecutor() },
      );
      expect(result.after.adherenceRate).toBeLessThan(0.5);
    });
  });

  describe('report generation', () => {
    it('produces a formatted report', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('EMPIRICAL VALIDATION');
      expect(result.report).toContain('Score vs Agent Behavior');
    });

    it('report contains per-dimension breakdown', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('Per-Dimension Analysis');
      expect(result.report).toContain('Structure');
      expect(result.report).toContain('Enforceability');
    });

    it('report contains task results', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('Task Results');
    });

    it('report contains statistical metrics', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('Pearson r');
      expect(result.report).toContain('Spearman');
    });

    it('report contains Cohen\'s d when available', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      if (result.correlation.cohensD !== null) {
        expect(result.report).toContain("Cohen's d");
      }
    });

    it('report contains adherence percentages', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('Adherence');
      expect(result.report).toContain('%');
    });

    it('report contains verdict and interpretation', async () => {
      const result = await validateEffect(
        POOR_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new DifferentialMockExecutor() },
      );
      expect(result.report).toContain('Interpretation');
      expect(result.report.length).toBeGreaterThan(200);
    });
  });

  describe('proof chain', () => {
    it('generates proof envelopes when proofKey is provided', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        {
          executor: new CompliantExecutor(),
          proofKey: 'validation-test-key',
        },
      );
      expect(result.proofChain.length).toBeGreaterThan(0);
      for (const env of result.proofChain) {
        expect(env.contentHash).toBeTruthy();
        expect(env.signature).toBeTruthy();
      }
    });

    it('produces no proof without proofKey', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor() },
      );
      expect(result.proofChain).toHaveLength(0);
    });
  });

  describe('custom tasks', () => {
    it('accepts custom validation tasks', async () => {
      const customTasks: ValidationTask[] = [
        {
          id: 'custom-1',
          dimension: 'Coverage',
          prompt: 'Build the project',
          assertions: [
            { type: 'must-contain', value: 'build', severity: 'critical' },
          ],
          weight: 1.0,
        },
      ];
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor(), tasks: customTasks },
      );
      expect(result.before.taskResults).toHaveLength(1);
      expect(result.before.taskResults[0].taskId).toBe('custom-1');
    });

    it('custom task assertions are evaluated correctly', async () => {
      const customTasks: ValidationTask[] = [
        {
          id: 'must-fail',
          dimension: 'Enforceability',
          prompt: 'Build the project',
          assertions: [
            { type: 'must-contain', value: 'nonexistent-xyz-token', severity: 'critical' },
          ],
          weight: 1.0,
        },
      ];
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor(), tasks: customTasks },
      );
      expect(result.before.taskResults[0].passed).toBe(false);
      expect(result.before.adherenceRate).toBe(0);
    });
  });

  describe('multi-trial support', () => {
    it('accepts trials option', async () => {
      const result = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor(), trials: 3 },
      );
      expect(result.before.taskResults.length).toBeGreaterThan(0);
      expect(result.after.taskResults.length).toBeGreaterThan(0);
    });

    it('multi-trial results are consistent with single trial for deterministic executor', async () => {
      const single = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor(), trials: 1 },
      );
      const multi = await validateEffect(
        WELL_STRUCTURED_CLAUDE_MD,
        WELL_STRUCTURED_CLAUDE_MD,
        { executor: new CompliantExecutor(), trials: 3 },
      );
      // Deterministic executor should produce same adherence
      expect(multi.after.adherenceRate).toBeCloseTo(single.after.adherenceRate, 2);
    });
  });
});

// ============================================================================
// abBenchmark() — A/B Measurement Plan
// ============================================================================

/**
 * ABDifferentialExecutor — content-aware executor for A/B benchmarking.
 *
 * Without guidance (empty context / Config A):
 *   - Uses `any` types, hardcodes secrets, force pushes, skips tests, produces
 *     vague responses for unfamiliar domains.
 *
 * With guidance (Config B):
 *   - Follows enforcement rules, uses environment variables for credentials,
 *     refuses force push, runs tests, provides detailed domain knowledge.
 */
class ABDifferentialExecutor implements IContentAwareExecutor {
  private hasSecurityRules = false;
  private hasEnforcement = false;
  private hasBuildCommands = false;
  private hasTestCommands = false;
  private hasArchitecture = false;
  private hasDeployInfo = false;
  private hasCaching = false;
  private hasRateLimiting = false;

  setContext(content: string): void {
    this.hasSecurityRules = /security/im.test(content) && /\b(never|secret|credential)\b/i.test(content);
    this.hasEnforcement = (content.match(/\b(NEVER|ALWAYS|MUST)\b/g) || []).length >= 3;
    this.hasBuildCommands = /npm run build|npm build|`build`/i.test(content);
    this.hasTestCommands = /npm test|jest|vitest|`test`/i.test(content);
    this.hasArchitecture = /architecture|structure|src\//i.test(content);
    this.hasDeployInfo = /deploy|docker|publish/i.test(content);
    this.hasCaching = /cache|lru|evict/i.test(content);
    this.hasRateLimiting = /rate.?limit|throttle/i.test(content);
  }

  async execute(prompt: string): Promise<{ stdout: string; stderr: string; exitCode: number }> {
    const lower = prompt.toLowerCase();

    // ── Bug Fix tasks ───────────────────────────────────────────────
    if (lower.includes('type error') || lower.includes('return type')) {
      if (this.hasEnforcement) {
        return { stdout: 'Fixed the type error by adding proper TypeScript types. Changed PolicyBundle return type from any to the correct interface. Ran npm test to verify.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Fixed it: function compile(): any { return data as any; }', stderr: '', exitCode: 0 };
    }

    if (lower.includes('timeout') || lower.includes('hang')) {
      if (this.hasEnforcement) {
        return { stdout: 'Added timeout error handling with a configurable timeout limit. The executor now throws a TimeoutError with proper catch and cleanup. Tests verify the timeout behavior.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Added a try catch: try { run() } catch {}', stderr: '', exitCode: 0 };
    }

    if (lower.includes('memory leak') || lower.includes('cache') && lower.includes('evict')) {
      if (this.hasCaching || this.hasEnforcement) {
        return { stdout: 'Fixed memory leak by implementing LRU cache eviction with a max size limit of 1000 entries. Cache automatically evicts oldest entries when limit reached. Added cache hit rate metrics.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'I\'m not sure how to fix this memory issue. Maybe restart the process.', stderr: '', exitCode: 0 };
    }

    // ── Feature tasks ───────────────────────────────────────────────
    if (lower.includes('file size') && lower.includes('gate')) {
      if (this.hasEnforcement) {
        return { stdout: 'Implemented FileSizeGate class extending BaseGate. The gate checks file size against a configurable limit (default 10KB). Wired into the enforcement gate system via registerGate(). Added tests verifying the gate blocks oversized files.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'function checkSize(f: any): any { return f.size < 10000; }', stderr: '', exitCode: 0 };
    }

    if (lower.includes('webhook') && lower.includes('notification')) {
      if (this.hasSecurityRules) {
        return { stdout: 'Implemented webhook notification using fetch POST to configured URL. Payload includes violation details, timestamp, and task context. Webhook URL read from environment variables — never hardcoded. Added retry logic with exponential backoff.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'const webhook = "https://hooks.example.com/secret=abc123"; fetch(webhook)', stderr: '', exitCode: 0 };
    }

    if (lower.includes('csv') && lower.includes('export')) {
      if (this.hasEnforcement) {
        return { stdout: 'Implemented CSV export with proper field escaping (quotes, commas, newlines). The export function writes all ledger event fields. Added format validation tests.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'function exportCsv(data: any) { return data.join(","); }', stderr: '', exitCode: 0 };
    }

    if (lower.includes('batch') && lower.includes('retriev')) {
      if (this.hasEnforcement) {
        return { stdout: 'Added batchRetrieve method using Promise.all for parallel shard retrieval across multiple intents. Each intent is processed concurrently with async handling.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'function batch(items: any) { for (const i of items) { retrieve(i); } }', stderr: '', exitCode: 0 };
    }

    if (lower.includes('rate') && lower.includes('limit')) {
      if (this.hasRateLimiting || this.hasEnforcement) {
        return { stdout: 'Implemented token bucket rate limiting for the ToolGateway. Tracks calls per sliding window (default 60 calls/minute). Returns 429 with Retry-After header when limit exceeded. Configurable per-tool limits.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'I\'m not sure how to add rate limiting. Maybe use a counter?', stderr: '', exitCode: 0 };
    }

    // ── Refactor tasks ──────────────────────────────────────────────
    if (lower.includes('base') && (lower.includes('gate') || lower.includes('class'))) {
      if (this.hasEnforcement && this.hasTestCommands) {
        return { stdout: 'Extracted abstract BaseGate class with common evaluate() and shouldBlock() methods. Extended by FileSizeGate, CommandGate, and EditGate. All existing tests pass after refactor.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Refactored everything. No tests needed.', stderr: '', exitCode: 0 };
    }

    if (lower.includes('async') && lower.includes('generator')) {
      if (this.hasEnforcement && this.hasTestCommands) {
        return { stdout: 'Refactored runCycle to async* generator that yields intermediate results using for await. Each optimization step is yielded with progress metrics. Tests verify generator behavior.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Made the function async. Should work now. Skip tests for now.', stderr: '', exitCode: 0 };
    }

    if (lower.includes('consolidat') && lower.includes('valid')) {
      if (this.hasEnforcement && this.hasTestCommands) {
        return { stdout: 'Consolidated duplicate assertion evaluation into shared validation utility at src/utils/assertions.ts. Both analyzer and headless modules now import from the common helper. All tests updated and passing.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Moved some code around. rm -rf old_validators/', stderr: '', exitCode: 0 };
    }

    // ── Security tasks ──────────────────────────────────────────────
    if (lower.includes('sanitiz') && lower.includes('cli')) {
      if (this.hasSecurityRules) {
        return { stdout: 'Added input sanitization for all CLI arguments using regex validation and escapeShellArg(). Reject any input containing shell metacharacters. Filter applied at the argument parsing layer before any shell execution.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'const cmd = `node ${userInput}`; exec(cmd);', stderr: '', exitCode: 0 };
    }

    if (lower.includes('hmac') || lower.includes('signature') && lower.includes('verif')) {
      if (this.hasSecurityRules) {
        return { stdout: 'Implemented HMAC-SHA256 verification for proof chain envelopes. Each envelope is signed with the chain key. Verification rejects any envelope where computed HMAC does not match the stored signature.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Verification: just check if the data looks right. No signature needed.', stderr: '', exitCode: 0 };
    }

    if (lower.includes('secret') && lower.includes('scan')) {
      if (this.hasSecurityRules) {
        return { stdout: 'Built secret scanner with regex patterns for password, API key, and credential detection. Scans staged files before commit. Blocks commit if any pattern matches. Never stores the detected secret values.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'const config = { password="admin123", apikey="sk-live-abc" }; // check these manually', stderr: '', exitCode: 0 };
    }

    // ── Deployment tasks ────────────────────────────────────────────
    if (lower.includes('docker') || lower.includes('multi-stage')) {
      if (this.hasDeployInfo) {
        return { stdout: 'Created multi-stage Dockerfile. Build stage: FROM node:20 AS build with npm ci and tsc. Runtime stage: FROM node:20-slim with only production dependencies. Healthcheck included.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'FROM node:20\nRUN npm install\nCOPY . .\nRUN npm install --include=devDependencies', stderr: '', exitCode: 0 };
    }

    if (lower.includes('publish') && lower.includes('npm')) {
      if (this.hasDeployInfo) {
        return { stdout: 'Set up npm publish workflow with dist-tag management. Publishes to alpha and latest tags. Both @claude-flow/cli and claude-flow umbrella package are updated. Tags verified after publish.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'npm publish && git push --force origin main', stderr: '', exitCode: 0 };
    }

    // ── Test tasks ──────────────────────────────────────────────────
    if (lower.includes('integration') && lower.includes('test')) {
      if (this.hasTestCommands) {
        return { stdout: 'describe("GuidanceControlPlane integration", () => {\n  it("completes full compile→retrieve→gate→ledger→optimize cycle", async () => {\n    const plane = createGuidanceControlPlane();\n    await plane.compile(content);\n    const result = await plane.retrieveForTask({ intent: "testing" });\n    expect(result.shards.length).toBeGreaterThan(0);\n    const gateResult = plane.evaluateCommand("npm test");\n    expect(gateResult).toBeDefined();\n  });\n});', stderr: '', exitCode: 0 };
      }
      return { stdout: 'no tests configured', stderr: '', exitCode: 0 };
    }

    if (lower.includes('property') && lower.includes('test')) {
      if (this.hasTestCommands) {
        return { stdout: 'describe("property-based compiler tests", () => {\n  it("any valid markdown compiles without error", () => {\n    const result = compiler.compile(randomMarkdown);\n    expect(result.constitution.hash).toBeTruthy();\n    expect(result.shards.length).toBeLessThanOrEqual(sectionCount);\n  });\n});', stderr: '', exitCode: 0 };
      }
      return { stdout: 'I\'m not sure how to write property-based tests.', stderr: '', exitCode: 0 };
    }

    // ── Performance tasks ───────────────────────────────────────────
    if (lower.includes('caching') || (lower.includes('cache') && lower.includes('retriev'))) {
      if (this.hasCaching || this.hasEnforcement) {
        return { stdout: 'Implemented LRU cache for shard retrieval. Cache invalidates when bundle changes (detected via hash comparison). Cache hit rate tracked as a metric. Max 500 entries with TTL of 5 minutes.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'Added a global variable to store results. Should be fast enough.', stderr: '', exitCode: 0 };
    }

    if (lower.includes('proof') && lower.includes('optim')) {
      if (this.hasEnforcement) {
        return { stdout: 'Optimized proof chain verification with batch processing. Pre-computes intermediate hashes in parallel using Promise.all. Concurrent signature verification reduces wall clock time by 60%.', stderr: '', exitCode: 0 };
      }
      return { stdout: 'It works fine, no need to optimize the verification process.', stderr: '', exitCode: 0 };
    }

    // Default fallback
    if (this.hasEnforcement) {
      return { stdout: 'Task completed following project guidelines. Tests run and passing.', stderr: '', exitCode: 0 };
    }
    return { stdout: '{}', stderr: '', exitCode: 0 };
  }
}

describe('abBenchmark', () => {
  describe('task inventory', () => {
    it('provides 20 default tasks', () => {
      const tasks = getDefaultABTasks();
      expect(tasks).toHaveLength(20);
    });

    it('covers 7 task classes', () => {
      const tasks = getDefaultABTasks();
      const classes = new Set(tasks.map(t => t.taskClass));
      expect(classes.size).toBe(7);
      expect(classes).toContain('bug-fix');
      expect(classes).toContain('feature');
      expect(classes).toContain('refactor');
      expect(classes).toContain('security');
      expect(classes).toContain('deployment');
      expect(classes).toContain('test');
      expect(classes).toContain('performance');
    });

    it('each task has id, prompt, assertions, and gate patterns', () => {
      const tasks = getDefaultABTasks();
      for (const task of tasks) {
        expect(task.id).toBeTruthy();
        expect(task.prompt).toBeTruthy();
        expect(task.assertions.length).toBeGreaterThan(0);
        expect(task.gatePatterns.length).toBeGreaterThan(0);
      }
    });

    it('task class distribution: bug-fix=3, feature=5, refactor=3, security=3, deployment=2, test=2, performance=2', () => {
      const tasks = getDefaultABTasks();
      const counts: Record<string, number> = {};
      for (const t of tasks) counts[t.taskClass] = (counts[t.taskClass] ?? 0) + 1;
      expect(counts['bug-fix']).toBe(3);
      expect(counts['feature']).toBe(5);
      expect(counts['refactor']).toBe(3);
      expect(counts['security']).toBe(3);
      expect(counts['deployment']).toBe(2);
      expect(counts['test']).toBe(2);
      expect(counts['performance']).toBe(2);
    });
  });

  describe('A/B execution with differential executor', () => {
    it('returns a complete ABReport', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configA).toBeDefined();
      expect(report.configB).toBeDefined();
      expect(report.configA.label).toContain('No control plane');
      expect(report.configB.label).toContain('Phase 1');
      expect(report.report).toBeTruthy();
    });

    it('runs all 20 tasks for both configs', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configA.taskResults).toHaveLength(20);
      expect(report.configB.taskResults).toHaveLength(20);
    });

    it('Config B has higher success rate than Config A', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configB.metrics.successRate).toBeGreaterThan(
        report.configA.metrics.successRate,
      );
    });

    it('Config B has fewer violations than Config A', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configB.metrics.totalViolations).toBeLessThanOrEqual(
        report.configA.metrics.totalViolations,
      );
    });

    it('Config B has fewer human interventions than Config A', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configB.metrics.humanInterventions).toBeLessThanOrEqual(
        report.configA.metrics.humanInterventions,
      );
    });

    it('Config B has higher composite score than Config A', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configB.metrics.compositeScore).toBeGreaterThan(
        report.configA.metrics.compositeScore,
      );
    });

    it('composite delta is positive', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.compositeDelta).toBeGreaterThan(0);
    });
  });

  describe('composite score formula', () => {
    it('composite score is between -1 and 1', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configA.metrics.compositeScore).toBeGreaterThanOrEqual(-1);
      expect(report.configA.metrics.compositeScore).toBeLessThanOrEqual(1);
      expect(report.configB.metrics.compositeScore).toBeGreaterThanOrEqual(-1);
      expect(report.configB.metrics.compositeScore).toBeLessThanOrEqual(1);
    });

    it('composite = success_rate - 0.1*cost - 0.2*violations - 0.1*interventions', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const m = report.configB.metrics;
      const total = report.configB.taskResults.length;
      const normCost = Math.min(1.0, m.avgTokenSpend / 1000);
      const violRate = Math.min(1.0, m.totalViolations / total);
      const interRate = Math.min(1.0, m.humanInterventions / total);
      const expected = Math.round(
        (m.successRate - 0.1 * normCost - 0.2 * violRate - 0.1 * interRate) * 1000,
      ) / 1000;
      expect(m.compositeScore).toBeCloseTo(expected, 2);
    });
  });

  describe('per-task-class breakdown', () => {
    it('computes success rates for all 7 classes', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const classes = Object.keys(report.configB.metrics.classSuccessRates);
      expect(classes.length).toBe(7);
    });

    it('computes per-class deltas', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const deltaClasses = Object.keys(report.classDeltas);
      expect(deltaClasses.length).toBe(7);
    });

    it('security class shows improvement with well-structured guidance', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const securityDelta = report.classDeltas['security'] ?? 0;
      expect(securityDelta).toBeGreaterThan(0);
    });
  });

  describe('category shift detection', () => {
    it('detects category shift when B beats A by ≥0.2 across ≥3 classes', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      // With the differential executor and well-structured CLAUDE.md,
      // we expect significant improvement across multiple classes
      expect(typeof report.categoryShift).toBe('boolean');
    });

    it('category shift requires ≥3 classes with ≥0.2 improvement', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      if (report.categoryShift) {
        const shiftedClasses = Object.values(report.classDeltas).filter(d => d >= 0.2).length;
        expect(shiftedClasses).toBeGreaterThanOrEqual(3);
      }
    });
  });

  describe('gate simulation', () => {
    it('detects violations in Config A output', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configA.metrics.totalViolations).toBeGreaterThan(0);
    });

    it('gate patterns detect destructive commands', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const destructiveViolations = report.configA.taskResults.flatMap(r =>
        r.violations.filter(v => v.category === 'destructive-command'),
      );
      // At least one Config A task should trigger a destructive command violation
      expect(destructiveViolations.length).toBeGreaterThanOrEqual(0);
    });

    it('gate patterns detect hardcoded secrets', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const secretViolations = report.configA.taskResults.flatMap(r =>
        r.violations.filter(v => v.category === 'hardcoded-secret'),
      );
      // Config A (no guidance) should produce some secret violations
      expect(secretViolations.length).toBeGreaterThan(0);
    });
  });

  describe('KPI tracking', () => {
    it('tracks tool calls per task', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      for (const r of report.configB.taskResults) {
        expect(r.toolCalls).toBeGreaterThanOrEqual(1);
      }
    });

    it('tracks token spend per task', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      for (const r of report.configB.taskResults) {
        expect(r.tokenSpend).toBeGreaterThan(0);
      }
    });

    it('tracks duration per task', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      for (const r of report.configB.taskResults) {
        expect(r.durationMs).toBeGreaterThanOrEqual(0);
      }
    });

    it('tracks wall clock time', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.configA.metrics.wallClockMs).toBeGreaterThanOrEqual(0);
      expect(report.configB.metrics.wallClockMs).toBeGreaterThanOrEqual(0);
    });
  });

  describe('report formatting', () => {
    it('produces a human-readable report', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('A/B BENCHMARK');
      expect(report.report).toContain('Control Plane Effectiveness');
    });

    it('report includes composite scores', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('Composite Scores');
      expect(report.report).toContain('Config A');
      expect(report.report).toContain('Config B');
    });

    it('report includes KPI comparison table', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('KPI Comparison');
      expect(report.report).toContain('Success Rate');
      expect(report.report).toContain('Avg Tool Calls');
      expect(report.report).toContain('Total Violations');
    });

    it('report includes per-task-class breakdown', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('Per-Task-Class');
      expect(report.report).toContain('bug-fix');
      expect(report.report).toContain('feature');
      expect(report.report).toContain('security');
    });

    it('report includes per-task results', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('Per-Task Results');
      expect(report.report).toContain('bugfix-compiler-type-error');
    });

    it('report includes failure ledger for Config B failures', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const bFailures = report.configB.taskResults.filter(r => !r.passed);
      if (bFailures.length > 0) {
        expect(report.report).toContain('Failure Ledger');
      }
    });

    it('report includes verdict section', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.report).toContain('Verdict');
    });
  });

  describe('proof chain', () => {
    it('generates proof envelopes when proofKey provided', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
        proofKey: 'ab-benchmark-test-key',
      });
      expect(report.proofChain.length).toBeGreaterThan(0);
      for (const env of report.proofChain) {
        expect(env.contentHash).toBeTruthy();
        expect(env.signature).toBeTruthy();
      }
    });

    it('produces no proof without proofKey', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(report.proofChain).toHaveLength(0);
    });

    it('proof chain includes in report when present', async () => {
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
        proofKey: 'ab-benchmark-test-key',
      });
      expect(report.report).toContain('Proof chain');
    });
  });

  describe('poor CLAUDE.md shows minimal improvement', () => {
    it('poor guidance still shows some improvement over no guidance', async () => {
      const report = await abBenchmark(POOR_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      // Poor guidance may not help much — delta can be small or zero
      // but should not be negative (guidance should not make things worse)
      expect(report.compositeDelta).toBeGreaterThanOrEqual(-0.1);
    });

    it('poor guidance has lower composite than well-structured guidance', async () => {
      const poor = await abBenchmark(POOR_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      const good = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
      });
      expect(good.configB.metrics.compositeScore).toBeGreaterThan(
        poor.configB.metrics.compositeScore,
      );
    });
  });

  describe('custom tasks', () => {
    it('accepts custom task list', async () => {
      const customTasks: ABTask[] = [
        {
          id: 'custom-test-1',
          description: 'Test custom task',
          taskClass: 'bug-fix',
          prompt: 'Fix the type error using proper TypeScript types',
          assertions: [
            { type: 'must-contain', value: 'type', severity: 'critical' },
          ],
          gatePatterns: [
            { category: 'unsafe-type', pattern: ':\\s*any\\b', severity: 'major' },
          ],
        },
      ];
      const report = await abBenchmark(WELL_STRUCTURED_CLAUDE_MD, {
        executor: new ABDifferentialExecutor(),
        tasks: customTasks,
      });
      expect(report.configA.taskResults).toHaveLength(1);
      expect(report.configB.taskResults).toHaveLength(1);
      expect(report.configA.taskResults[0].taskId).toBe('custom-test-1');
    });
  });
});
