import { describe, expect, it } from 'bun:test'; import { access, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { execa } from 'execa'; import { buildProjectionIdentity, toProjectionIdentityWire, } from '../../../packages/core/src/evaluation/projection-identity.js'; import packageJson from '../package.json' with { type: 'json' }; import { assertCoreBuild } from './setup-core-build.js'; assertCoreBuild(); interface EvalFixture { readonly baseDir: string; readonly suiteDir: string; readonly testFilePath: string; readonly diagnosticsPath: string; } const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const projectRoot = path.resolve(__dirname, '../../..'); const CLI_ENTRY = path.join(projectRoot, 'apps/cli/src/cli.ts'); const MOCK_RUNNER = path.join(projectRoot, 'apps/cli/test/fixtures/mock-run-evaluation.ts'); function runIndexPath(runDir: string): string { return path.join(runDir, '.internal', 'index.jsonl'); } function runDirFromIndexPath(indexPath: string): string { return path.dirname(path.dirname(indexPath)); } function projectProviderConfigLines(extraLines: readonly string[] = []): string { return [ ...extraLines, 'providers:', ' - id: mock', ' label: default', ' - id: mock', ' label: file-target', ' - id: mock', ' label: cli-target', ' - id: openai:codex-sdk', ' label: codex-target', ' model: gpt-5-default', '', ].join('\n'); } function nestedProviderConfigLines(): string { return ['providers:', ' - id: mock', ' label: default', ''].join('\n'); } function rebuildProjectionIdentityWire( wireIdentity: unknown, dimensions: Record, ): unknown { if (typeof wireIdentity !== 'object' || wireIdentity === null || Array.isArray(wireIdentity)) { return wireIdentity; } return toProjectionIdentityWire( buildProjectionIdentity({ runId: dimensions.run_id as string | undefined, suite: dimensions.suite as string | undefined, evalPath: dimensions.eval_path as string | undefined, testId: dimensions.test_id as string | undefined, target: dimensions.target as string | undefined, sourceTarget: dimensions.source_target as string | undefined, attempt: dimensions.attempt as number | undefined, variant: dimensions.variant as string | null | undefined, envelopeId: dimensions.envelope_id as string | undefined, traceId: dimensions.trace_id as string | undefined, rootSpanId: dimensions.root_span_id as string | undefined, projectionFormat: dimensions.projection_format as string | undefined, projectionVersion: dimensions.projection_version as string | undefined, }), ); } async function createFixture(): Promise { const baseDir = await mkdtemp(path.join(tmpdir(), 'agentv-cli-test-')); const suiteDir = path.join(baseDir, 'suite'); await mkdir(suiteDir, { recursive: true }); const agentvDir = path.join(suiteDir, '.agentv'); await mkdir(agentvDir, { recursive: true }); const providersPath = path.join(agentvDir, 'providers.yaml'); const providersContent = `$schema: agentv-targets-v2.2 providers: - id: mock label: default - id: mock label: file-target - id: mock label: cli-target - id: openai:codex-sdk label: codex-target model: gpt-5-default `; await writeFile(providersPath, providersContent, 'utf8'); await writeFile(path.join(agentvDir, 'config.yaml'), projectProviderConfigLines(), 'utf8'); const testFilePath = path.join(suiteDir, 'sample.test.yaml'); const testFileContent = `description: CLI integration test providers: - file-target prompts: - "{{ input }}" tests: - id: case-alpha criteria: System responds with alpha vars: input: - role: user content: | Please respond with alpha expected_output: - role: assistant content: Alpha - id: case-beta criteria: System responds with beta vars: input: - role: user content: | Please respond with beta expected_output: - role: assistant content: Beta `; await writeFile(testFilePath, testFileContent, 'utf8'); const envPath = path.join(suiteDir, '.env'); await writeFile(envPath, 'CLI_ENV_SAMPLE=from-dotenv\n', 'utf8'); const diagnosticsPath = path.join(baseDir, 'diagnostics.json'); return { baseDir, suiteDir, testFilePath, diagnosticsPath } satisfies EvalFixture; } async function createNestedEnvFixture(): Promise { const baseDir = await mkdtemp(path.join(tmpdir(), 'agentv-cli-nested-env-test-')); const suiteDir = path.join(baseDir, 'suite'); const evalDir = path.join(suiteDir, 'evals', 'foo'); await mkdir(evalDir, { recursive: true }); const agentvDir = path.join(suiteDir, '.agentv'); await mkdir(agentvDir, { recursive: true }); const providersPath = path.join(agentvDir, 'providers.yaml'); const providersContent = `$schema: agentv-targets-v2.2 providers: - id: mock label: default `; await writeFile(providersPath, providersContent, 'utf8'); await writeFile(path.join(agentvDir, 'config.yaml'), nestedProviderConfigLines(), 'utf8'); const testFilePath = path.join(evalDir, 'sample.test.yaml'); const testFileContent = `description: CLI nested env integration test prompts: - "{{ input }}" tests: - id: case-alpha criteria: System responds with alpha vars: input: - role: user content: | Please respond with alpha expected_output: - role: assistant content: Alpha - id: case-beta criteria: System responds with beta vars: input: - role: user content: | Please respond with beta expected_output: - role: assistant content: Beta `; await writeFile(testFilePath, testFileContent, 'utf8'); await writeFile( path.join(suiteDir, '.env'), 'CLI_ENV_SAMPLE=from-root\nCLI_ENV_ROOT_ONLY=from-root\n', 'utf8', ); await writeFile( path.join(evalDir, '.env'), 'CLI_ENV_SAMPLE=from-local\nCLI_ENV_LOCAL_ONLY=from-local\n', 'utf8', ); const diagnosticsPath = path.join(baseDir, 'diagnostics.json'); return { baseDir, suiteDir, testFilePath, diagnosticsPath } satisfies EvalFixture; } async function runCli( fixture: EvalFixture, args: readonly string[], extraEnv: Record = {}, ): Promise<{ stdout: string; stderr: string; exitCode: number | undefined }> { const baseEnv: Record = { ...process.env }; baseEnv.CLI_ENV_SAMPLE = undefined; baseEnv.CLI_ENV_ROOT_ONLY = undefined; baseEnv.CLI_ENV_LOCAL_ONLY = undefined; try { const result = await execa('bun', ['--no-env-file', CLI_ENTRY, ...args], { cwd: fixture.suiteDir, env: { ...baseEnv, CI: 'true', // Disable interactive progress display for tests AGENTEVO_CLI_EVAL_RUNNER: MOCK_RUNNER, AGENTEVO_CLI_EVAL_RUNNER_OUTPUT: fixture.diagnosticsPath, ...extraEnv, }, reject: false, }); return { stdout: result.stdout, stderr: result.stderr, exitCode: result.exitCode }; } catch (error) { console.error('CLI execution failed:', error); throw error; } } function extractOutputPath(stdout: string): string { const lines = stdout.split(/\r?\n/); // Try new format first, then legacy const outputLine = lines.find((line) => line.includes('Results written to:')) ?? lines.find((line) => line.includes('Output path:')); if (!outputLine) { throw new Error(`Unable to parse output path from CLI output:\n${stdout}`); } return outputLine.replace(/^.*?(Results written to:|Output path:)/, '').trim(); } async function readJsonLines(filePath: string): Promise { const raw = await readFile(filePath, 'utf8'); return raw .split(/\r?\n/) .map((line) => line.trim()) .filter((line) => line.length > 0) .map((line) => JSON.parse(line)); } async function readDiagnostics(fixture: EvalFixture): Promise> { for (let attempt = 0; attempt < 20; attempt++) { try { const raw = await readFile(fixture.diagnosticsPath, 'utf8'); return JSON.parse(raw) as Record; } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT' || attempt === 19) { throw error; } await new Promise((resolve) => setTimeout(resolve, 50)); } } throw new Error(`Missing diagnostics file: ${fixture.diagnosticsPath}`); } async function writeTsCacheConfig(fixture: EvalFixture, cachePath: string): Promise { await writeFile( path.join(fixture.suiteDir, 'agentv.config.ts'), `export default { cache: { enabled: true, path: ${JSON.stringify(cachePath)} } };\n`, 'utf8', ); } async function writeTsOutputConfig(fixture: EvalFixture, outputDir: string): Promise { await writeFile( path.join(fixture.suiteDir, 'agentv.config.ts'), `export default { output: { dir: ${JSON.stringify(outputDir)} } };\n`, 'utf8', ); } async function writeRequiredVersionConfig( fixture: EvalFixture, requiredVersion: string, ): Promise { await writeFile( path.join(fixture.suiteDir, '.agentv', 'config.yaml'), projectProviderConfigLines([`required_version: "${requiredVersion}"`]), 'utf8', ); } async function expectFileExists(filePath: string): Promise { await access(filePath); } describe('agentv eval CLI', () => { it('writes results, summary, and prompt dumps using default directories', async () => { const fixture = await createFixture(); try { const { stdout } = await runCli(fixture, ['eval', fixture.testFilePath, '--verbose']); // Don't check stderr - it may contain stack traces or other diagnostics expect(stdout).toContain('Using provider (test-file): file-target [provider=mock]'); expect(stdout).toContain('Mean score: 75%'); // Std deviation is an implementation detail - don't check it const outputPath = extractOutputPath(stdout); expect(outputPath).toContain(`${path.sep}.agentv${path.sep}results${path.sep}`); const results = await readJsonLines(outputPath); expect(results).toHaveLength(2); const [firstResult, secondResult] = results as Array>; expect(firstResult.test_id).toBe('case-alpha'); expect(secondResult.test_id).toBe('case-beta'); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ target: 'file-target', agentTimeoutMs: null, envSample: 'from-dotenv', resultCount: 2, }); // Prompt dump feature has been removed, so we no longer check for it } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('surfaces required_version mismatch note when the eval fails', async () => { const fixture = await createFixture(); try { await writeRequiredVersionConfig(fixture, '>=999.0.0'); const { stdout, stderr, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--threshold', '0.8', ]); expect(exitCode).toBe(1); expect(stdout).toContain('RESULT: FAIL'); expect(stdout).toContain( `note: agentv ${packageJson.version} does not satisfy this project's required_version >=999.0.0 - this may be the cause. Run \`agentv self update\`.`, ); expect(stderr).toContain( `Warning: agentv ${packageJson.version} does not satisfy this project's required_version >=999.0.0. Run \`agentv self update\`.`, ); expect(`${stdout}\n${stderr}`).not.toContain('Update now?'); expect(`${stdout}\n${stderr}`).not.toContain('Update complete'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('keeps required_version mismatch low-noise when the eval passes', async () => { const fixture = await createFixture(); try { await writeRequiredVersionConfig(fixture, '>=999.0.0'); const { stdout, stderr, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--threshold', '0.5', ]); expect(exitCode).toBe(0); expect(stdout).toContain('RESULT: PASS'); expect(stdout).not.toContain('note: agentv'); expect(stderr).toContain( `Warning: agentv ${packageJson.version} does not satisfy this project's required_version >=999.0.0. Run \`agentv self update\`.`, ); expect(`${stdout}\n${stderr}`).not.toContain('Update now?'); expect(`${stdout}\n${stderr}`).not.toContain('Update complete'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('writes canonical artifacts under an explicit --output directory', async () => { const fixture = await createFixture(); try { const outputDir = path.join(fixture.baseDir, 'explicit-run'); const { stdout, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--output', outputDir, ]); expect(exitCode).toBe(0); const indexPath = runIndexPath(outputDir); expect(extractOutputPath(stdout)).toBe(indexPath); expect(stdout).toContain(`Artifact directory: ${outputDir}`); const results = await readJsonLines(indexPath); expect(results).toHaveLength(2); await expectFileExists(path.join(outputDir, 'summary.json')); for (const row of results as Array>) { const resultDir = row.result_dir as string; expect(resultDir).not.toContain('/'); await expectFileExists(path.join(outputDir, resultDir, 'summary.json')); await expectFileExists(path.join(outputDir, resultDir, 'sample-1', 'grading.json')); } } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('uses agentv.config.ts output.dir as the canonical artifact directory fallback', async () => { const fixture = await createFixture(); try { await writeTsOutputConfig(fixture, './configured-results'); const { stdout, exitCode } = await runCli(fixture, ['eval', fixture.testFilePath]); const outputDir = path.join(fixture.suiteDir, 'configured-results'); expect(exitCode).toBe(0); const indexPath = runIndexPath(outputDir); expect(extractOutputPath(stdout)).toBe(indexPath); await expectFileExists(indexPath); await expectFileExists(path.join(outputDir, 'summary.json')); const [firstRow] = (await readJsonLines(indexPath)) as Array>; await expectFileExists(path.join(outputDir, firstRow.result_dir as string, 'summary.json')); await expectFileExists( path.join(outputDir, firstRow.result_dir as string, 'sample-1', 'grading.json'), ); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('rejects removed --export and keeps --output as the canonical manifest location', async () => { const fixture = await createFixture(); try { const outputDir = path.join(fixture.baseDir, 'run'); const flatJsonlPath = path.join(fixture.baseDir, 'flat.jsonl'); const removed = await runCli(fixture, [ 'eval', fixture.testFilePath, '--output', outputDir, '--export', flatJsonlPath, ]); expect(removed.exitCode).not.toBe(0); expect(`${removed.stdout}\n${removed.stderr}`).toContain('Unknown arguments'); const { stdout, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--output', outputDir, '--threshold', '0.8', ]); expect(exitCode).toBe(1); const indexPath = runIndexPath(outputDir); expect(extractOutputPath(stdout)).toBe(indexPath); expect(stdout).not.toContain('Export files:'); const canonicalResults = await readJsonLines(indexPath); expect(canonicalResults).toHaveLength(2); await expectFileExists(path.join(outputDir, 'summary.json')); for (const row of canonicalResults) { expect(row.transcript_path).toMatch(/sample-1\/transcript\.json$/); await expectFileExists(path.join(outputDir, row.transcript_path as string)); expect(row).not.toHaveProperty('transcript_summary'); const resultJsonPath = (row.transcript_path as string).replace( /sample-1\/transcript\.json$/, 'sample-1/result.json', ); const sampleResult = JSON.parse( await readFile(path.join(outputDir, resultJsonPath), 'utf8'), ); expect(sampleResult.transcript_summary).toBeDefined(); expect(row.transcript_raw_path).toMatch(/sample-1\/transcript-raw\.jsonl$/); await expectFileExists(path.join(outputDir, row.transcript_raw_path as string)); } } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('fails with migration guidance for removed eval output flags', async () => { const cases = [ { args: ['--out', 'legacy.jsonl'], expected: [ '--out was removed', '--output ', 'Flat result file export from agentv eval has been removed', ], }, { args: ['--artifacts', 'legacy-artifacts'], expected: ['--artifacts was removed', '--output legacy-artifacts'], }, { args: ['-o', 'junit.xml', '--artifacts', 'legacy-artifacts'], expected: [ '--artifacts was removed', '--output legacy-artifacts', 'JUnit XML export from agentv eval has been removed', ], }, { args: ['--output-format', 'html'], expected: ['--output-format was removed', 'index.jsonl'], }, { args: ['--output', 'results.xml'], expected: [ '--output expects a run directory', 'JUnit XML export from agentv eval has been removed', '/index.jsonl', ], }, ] as const; for (const testCase of cases) { const fixture = await createFixture(); try { const result = await runCli(fixture, ['eval', fixture.testFilePath, ...testCase.args]); expect(result.exitCode).toBe(1); const output = `${result.stdout}\n${result.stderr}`; for (const expected of testCase.expected) { expect(output).toContain(expected); } } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } } }, 30_000); it('loads the nearest .env first and uses parent .env only for missing keys', async () => { const fixture = await createNestedEnvFixture(); try { await runCli(fixture, ['eval', fixture.testFilePath, '--verbose']); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ envSample: 'from-local', envRootOnly: 'from-root', envLocalOnly: 'from-local', resultCount: 2, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('supports repeatable --test-id flags with OR matching', async () => { const fixture = await createFixture(); try { await runCli(fixture, [ 'eval', fixture.testFilePath, '--test-id', 'case-alpha', '--test-id', 'case-beta', ]); const diagnostics = await readDiagnostics(fixture); expect(diagnostics.filter).toEqual(['case-alpha', 'case-beta']); expect(diagnostics.evalCaseIds).toEqual(['case-alpha', 'case-beta']); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('keeps --workspace-path as a static workspace override for existing workspaces', async () => { const fixture = await createFixture(); try { const workspacePath = path.join(fixture.baseDir, 'prepared-workspace'); await mkdir(workspacePath, { recursive: true }); const result = await runCli(fixture, [ 'eval', fixture.testFilePath, '--workspace-path', workspacePath, ]); expect(result.exitCode).toBe(0); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ workspacePath, resultCount: 2, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('uses config.local.yaml workspace_path as a static workspace override', async () => { const fixture = await createFixture(); try { const workspacePath = path.join(fixture.baseDir, 'local-config-workspace'); await mkdir(workspacePath, { recursive: true }); await writeFile( path.join(fixture.suiteDir, '.agentv', 'config.local.yaml'), `execution:\n workspace_path: ${JSON.stringify(workspacePath)}\n`, 'utf8', ); const result = await runCli(fixture, ['eval', fixture.testFilePath]); expect(result.exitCode).toBe(0); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ workspacePath, resultCount: 2, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('passes run-level budget tracking through to the evaluator', async () => { const fixture = await createFixture(); try { await runCli(fixture, ['eval', fixture.testFilePath, '--budget-usd', '0.5']); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ budgetUsd: null, hasRunBudgetTracker: true, runBudgetCapUsd: 0.5, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('lets --budget-usd override evaluate_options.budget_usd', async () => { const fixture = await createFixture(); try { const evalPath = path.join(fixture.suiteDir, 'budget-options.eval.yaml'); await writeFile( evalPath, [ 'description: Budget options integration test', 'providers:', ' - file-target', 'evaluate_options:', ' budget_usd: 1.25', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: case-alpha', ' criteria: System responds with alpha', ' vars:', ' input: alpha', ].join('\n'), 'utf8', ); await runCli(fixture, ['eval', evalPath, '--budget-usd', '0.5']); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ budgetUsd: null, hasRunBudgetTracker: true, runBudgetCapUsd: 0.5, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('runs eval-local target config with suite test selection and run knobs', async () => { const fixture = await createFixture(); try { await writeFile( path.join(fixture.suiteDir, '.agentv', 'config.yaml'), projectProviderConfigLines([ 'eval_patterns:', ' - sample.test.yaml', ' - unused.test.yaml', ]), 'utf8', ); await writeFile( path.join(fixture.suiteDir, 'unused.test.yaml'), [ 'description: unmatched eval file should not resolve targets', 'providers:', ' - missing-target', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: case-unused', ' criteria: System responds with unused', ' vars:', ' input: unused', ' expected_output: unused', ].join('\n'), 'utf8', ); const wrapperPath = path.join(fixture.suiteDir, 'native-exp.eval.yaml'); await writeFile( wrapperPath, [ 'name: native-exp', 'providers:', ' - codex-target', 'timeout_seconds: 12', 'threshold: 0.8', 'evaluate_options:', ' budget_usd: 3', ' repeat: 2', 'tests:', ' - include: sample.test.yaml', ' type: suite', ' select: case-alpha', ' run:', ' threshold: 1.0', ' timeout_seconds: 5', ' budget_usd: 0.75', ' repeat: 3', '', ].join('\n'), 'utf8', ); const { stdout, exitCode } = await runCli(fixture, ['eval', wrapperPath, '--workers', '4']); expect(exitCode).toBe(0); const outputPath = extractOutputPath(stdout); expect(path.dirname(runDirFromIndexPath(outputPath))).toBe( path.join(fixture.suiteDir, '.agentv', 'results'), ); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ target: 'codex-target', targetModel: 'gpt-5-default', agentTimeoutMs: 5000, maxConcurrency: 4, evalCaseIds: ['case-alpha'], budgetUsd: 0.75, threshold: 1, trials: { count: 3, strategy: 'pass_any', }, }); const benchmark = JSON.parse( await readFile(path.join(runDirFromIndexPath(outputPath), 'summary.json'), 'utf8'), ) as { metadata?: Record }; expect(benchmark.metadata?.experiment).toBe('native-exp'); expect(benchmark.metadata).not.toHaveProperty('experiment_config'); expect(benchmark.metadata).not.toHaveProperty('run_config_path'); await expect( readFile( path.join(runDirFromIndexPath(outputPath), '.internal', 'run-config.json'), 'utf8', ), ).rejects.toThrow(); expect(benchmark.metadata?.runtime_source).toMatchObject({ schema_version: 'agentv.runtime_source.v1', config_source: 'mixed', eval_files: ['sample.test.yaml'], wrapper_eval_file: 'native-exp.eval.yaml', }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('resolves default_test threshold below CLI and per-test run overrides but above legacy threshold', async () => { const fixture = await createFixture(); try { const evalPath = path.join(fixture.suiteDir, 'default-threshold.eval.yaml'); await writeFile( evalPath, [ 'name: default-threshold', 'providers:', ' - file-target', 'threshold: 0.9', 'prompts:', ' - "{{ input }}"', 'default_test:', ' threshold: 0.6', 'tests:', ' - id: default-case', ' criteria: ok', ' vars:', ' input: default', ' - id: strict-case', ' criteria: ok', ' run:', ' threshold: 1', ' vars:', ' input: strict', ].join('\n'), 'utf8', ); const firstRun = await runCli(fixture, ['eval', evalPath]); expect(firstRun.exitCode).toBe(0); const firstDiagnostics = await readDiagnostics(fixture); expect(firstDiagnostics.calls).toEqual( expect.arrayContaining([ expect.objectContaining({ evalCaseIds: ['default-case'], threshold: 0.6 }), expect.objectContaining({ evalCaseIds: ['strict-case'], threshold: 1 }), ]), ); await rm(fixture.diagnosticsPath, { force: true }); const cliRun = await runCli(fixture, ['eval', evalPath, '--threshold', '0.4']); expect(cliRun.exitCode).toBe(0); const cliDiagnostics = await readDiagnostics(fixture); expect(cliDiagnostics).toMatchObject({ evalCaseIds: ['default-case', 'strict-case'], threshold: 0.4, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('summarizes multi-file default_test thresholds from per-result execution status', async () => { const fixture = await createFixture(); try { const firstPath = path.join(fixture.suiteDir, 'first-default-threshold.eval.yaml'); const secondPath = path.join(fixture.suiteDir, 'second-default-threshold.eval.yaml'); await writeFile( firstPath, [ 'name: first-default-threshold', 'providers:', ' - file-target', 'prompts:', ' - "{{ input }}"', 'default_test:', ' threshold: 0.6', 'tests:', ' - id: first-default-case', ' criteria: ok', ' vars:', ' input: first', ].join('\n'), 'utf8', ); await writeFile( secondPath, [ 'name: second-default-threshold', 'providers:', ' - file-target', 'prompts:', ' - "{{ input }}"', 'default_test:', ' threshold: 0.7', 'tests:', ' - id: second-default-case', ' criteria: ok', ' vars:', ' input: second', ].join('\n'), 'utf8', ); const { stdout, exitCode } = await runCli(fixture, ['eval', firstPath, secondPath]); expect(exitCode).toBe(0); expect(stdout).toContain('scored >= configured threshold(s)'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('keeps non-concurrency run controls isolated across multiple eval files', async () => { const fixture = await createFixture(); try { const firstPath = path.join(fixture.suiteDir, 'first.eval.yaml'); const secondPath = path.join(fixture.suiteDir, 'second.eval.yaml'); await writeFile( firstPath, [ 'name: first', 'providers:', ' - cli-target', 'timeout_seconds: 11', 'evaluate_options:', ' budget_usd: 0.11', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: first-case', ' criteria: ok', ' vars:', ' input: first', ].join('\n'), 'utf8', ); await writeFile( secondPath, [ 'name: second', 'providers:', ' - file-target', 'timeout_seconds: 22', 'evaluate_options:', ' budget_usd: 0.22', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: second-case', ' criteria: ok', ' vars:', ' input: second', ].join('\n'), 'utf8', ); const { stdout, exitCode } = await runCli(fixture, [ 'eval', firstPath, secondPath, '--workers', '2', ]); expect(exitCode).toBe(0); const outputPath = extractOutputPath(stdout); expect(path.dirname(runDirFromIndexPath(outputPath))).toBe( path.join(fixture.suiteDir, '.agentv', 'results'), ); const diagnostics = await readDiagnostics(fixture); const calls = diagnostics.calls as Array>; expect(calls).toHaveLength(2); expect(calls[0]).toMatchObject({ target: 'cli-target', agentTimeoutMs: 11_000, maxConcurrency: 2, budgetUsd: 0.11, runBudgetCapUsd: 0.11, evalCaseIds: ['first-case'], }); expect(calls[1]).toMatchObject({ target: 'file-target', agentTimeoutMs: 22_000, maxConcurrency: 2, budgetUsd: 0.22, runBudgetCapUsd: 0.22, evalCaseIds: ['second-case'], }); const benchmark = JSON.parse( await readFile(path.join(runDirFromIndexPath(outputPath), 'summary.json'), 'utf8'), ) as { metadata?: Record }; expect(benchmark.metadata?.runtime_source).toMatchObject({ schema_version: 'agentv.runtime_source.v1', config_source: 'mixed', eval_files: ['first.eval.yaml', 'second.eval.yaml'], }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('uses evaluate_options.max_concurrency as the eval-authored concurrency limit', async () => { const fixture = await createFixture(); try { const evalPath = path.join(fixture.suiteDir, 'max-concurrency.eval.yaml'); await writeFile( evalPath, [ 'name: max-concurrency', 'providers:', ' - file-target', 'evaluate_options:', ' max_concurrency: 2', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: first-case', ' criteria: ok', ' vars:', ' input: first', ].join('\n'), 'utf8', ); const { exitCode } = await runCli(fixture, ['eval', evalPath]); expect(exitCode).toBe(0); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ maxConcurrency: 2, evalCaseIds: ['first-case'], }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('reruns only latest failed rows from a canonical run id', async () => { const fixture = await createFixture(); try { const priorRunDir = path.join(fixture.suiteDir, '.agentv', 'results', 'prior-run'); const first = await runCli(fixture, [ 'eval', fixture.testFilePath, '--output', priorRunDir, '--threshold', '0.8', ]); expect(first.exitCode).toBe(1); const priorIndexPath = runIndexPath(priorRunDir); const priorRows = (await readJsonLines(priorIndexPath)) as Array>; const alphaRow = priorRows.find((row) => row.test_id === 'case-alpha'); const betaRow = priorRows.find((row) => row.test_id === 'case-beta'); if (!alphaRow || !betaRow) { throw new Error('Expected prior rows for case-alpha and case-beta'); } await writeFile( priorIndexPath, `${[ ...priorRows.map((row) => ({ ...row, execution_status: row.test_id === 'case-alpha' ? 'ok' : 'quality_failure', })), { ...alphaRow, execution_status: 'quality_failure' }, { ...betaRow, execution_status: 'ok' }, ] .map((row) => JSON.stringify(row)) .join('\n')}\n`, 'utf8', ); await writeFile( fixture.testFilePath, `description: CLI integration test providers: - file-target prompts: - "{{ input }}" tests: - id: case-alpha criteria: System responds with alpha vars: input: - role: user content: | Please respond with alpha expected_output: - role: assistant content: Alpha - id: case-beta criteria: System responds with beta vars: input: - role: user content: | Please respond with beta expected_output: - role: assistant content: Beta - id: case-gamma criteria: System responds with gamma vars: input: - role: user content: | Please respond with gamma expected_output: - role: assistant content: Gamma `, 'utf8', ); const second = await runCli(fixture, [ 'eval', fixture.testFilePath, '--rerun-failed', 'prior-run', '--threshold', '0.5', ]); expect(second.exitCode).toBe(0); expect(second.stdout).toContain('Rerun-failed: found 4 existing result(s), skipping 1'); const diagnostics = await readDiagnostics(fixture); const calls = diagnostics.calls as Array>; expect(calls.at(-1)).toMatchObject({ evalCaseIds: ['case-alpha'], }); const rows = await readJsonLines(priorIndexPath); expect(rows).toHaveLength(5); expect((rows.at(-1) as Record).test_id).toBe('case-alpha'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('does not use coarse fallback keys for precise rerun-failed identities', async () => { const fixture = await createFixture(); try { const firstEvalPath = path.join(fixture.suiteDir, 'collision-a.eval.yaml'); const secondEvalPath = path.join(fixture.suiteDir, 'collision-b.eval.yaml'); const evalContent = (name: string) => `description: ${name} providers: - file-target prompts: - "{{ input }}" tests: - id: shared-case criteria: System responds vars: input: - role: user content: | Please respond for ${name} expected_output: - role: assistant content: Shared `; await writeFile(firstEvalPath, evalContent('collision a'), 'utf8'); await writeFile(secondEvalPath, evalContent('collision b'), 'utf8'); const priorRunDir = path.join(fixture.suiteDir, '.agentv', 'results', 'prior-collision'); const first = await runCli(fixture, [ 'eval', firstEvalPath, '--output', priorRunDir, '--threshold', '0.8', ]); expect(first.exitCode).toBe(0); const priorIndexPath = runIndexPath(priorRunDir); const priorRows = (await readJsonLines(priorIndexPath)) as Array>; expect(priorRows).toHaveLength(1); const baseRow = priorRows[0]; if (!baseRow) { throw new Error('Expected one prior collision row'); } const baseProjection = baseRow.projection_identity as Record; const baseDimensions = baseProjection.dimensions as Record; const secondDimensions = { ...baseDimensions, eval_path: secondEvalPath, }; const secondProjection = rebuildProjectionIdentityWire(baseProjection, secondDimensions); await writeFile( priorIndexPath, `${[ { ...baseRow, execution_status: 'ok' }, { ...baseRow, eval_path: secondEvalPath, source: { ...((baseRow.source as Record | undefined) ?? {}), eval_file_path: secondEvalPath, eval_file_absolute_path: secondEvalPath, }, projection_identity: secondProjection, execution_status: 'quality_failure', }, ] .map((row) => JSON.stringify(row)) .join('\n')}\n`, 'utf8', ); const second = await runCli(fixture, [ 'eval', firstEvalPath, secondEvalPath, '--rerun-failed', 'prior-collision', '--threshold', '0.8', ]); expect(second.exitCode).toBe(0); expect(second.stdout).toContain('Rerun-failed: found 2 existing result(s), skipping 1'); const diagnostics = await readDiagnostics(fixture); const calls = diagnostics.calls as Array>; const rerunCalls = calls.slice(1); expect(rerunCalls).toHaveLength(1); const rerunCall = rerunCalls[0]; if (!rerunCall) { throw new Error('Expected one rerun diagnostics call'); } expect(path.basename(rerunCall.testFilePath as string)).toBe('collision-b.eval.yaml'); expect(rerunCall).toMatchObject({ evalCaseIds: ['shared-case'], }); const rows = await readJsonLines(priorIndexPath); expect(rows).toHaveLength(3); expect((rows.at(-1) as Record).test_id).toBe('shared-case'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('does not multiply max_concurrency across target matrix selections', async () => { const fixture = await createFixture(); try { const evalPath = path.join(fixture.suiteDir, 'target-matrix.eval.yaml'); await writeFile( evalPath, [ 'name: target-matrix', 'providers:', ' - file-target', 'prompts:', ' - "{{ input }}"', 'tests:', ' - id: first-case', ' criteria: ok', ' vars:', ' input: first', ' - id: second-case', ' criteria: ok', ' vars:', ' input: second', ].join('\n'), 'utf8', ); const { exitCode } = await runCli(fixture, [ 'eval', evalPath, '--workers', '2', '--provider', 'file-target', '--provider', 'cli-target', ]); expect(exitCode).toBe(0); const diagnostics = await readDiagnostics(fixture); const calls = diagnostics.calls as Array>; expect(calls).toHaveLength(2); expect(calls.map((call) => call.maxConcurrency)).toEqual([1, 1]); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('records CLI-named experiment namespace separately from default runtime config', async () => { const fixture = await createFixture(); try { const { stdout, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--experiment', 'cli-smoke', ]); expect(exitCode).toBe(0); const outputPath = extractOutputPath(stdout); const benchmark = JSON.parse( await readFile(path.join(runDirFromIndexPath(outputPath), 'summary.json'), 'utf8'), ) as { metadata?: Record }; expect(benchmark.metadata?.runtime_source).toMatchObject({ schema_version: 'agentv.runtime_source.v1', config_source: 'inline_experiment', eval_files: ['sample.test.yaml'], }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('honors agentv.config.ts cache.path when response cache is enabled there', async () => { const fixture = await createFixture(); try { const cachePath = '.agentv/ts-response-cache'; await writeTsCacheConfig(fixture, cachePath); const { stdout } = await runCli(fixture, ['eval', fixture.testFilePath]); const resolvedCachePath = path.resolve(fixture.suiteDir, cachePath); expect(stdout).toContain(`Response cache: enabled (${resolvedCachePath})`); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ hasCache: true, cachePath: resolvedCachePath, useCache: true, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('honors CLI --cache-path as an explicit response cache opt-in', async () => { const fixture = await createFixture(); try { const cachePath = '.agentv/cli-response-cache'; const { stdout } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--cache-path', cachePath, ]); const resolvedCachePath = path.resolve(fixture.suiteDir, cachePath); expect(stdout).toContain(`Response cache: enabled (${resolvedCachePath})`); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ hasCache: true, cachePath: resolvedCachePath, useCache: true, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('lets --no-cache override config-driven response cache settings', async () => { const fixture = await createFixture(); try { await writeTsCacheConfig(fixture, '.agentv/disabled-response-cache'); const { stdout } = await runCli(fixture, ['eval', fixture.testFilePath, '--no-cache']); expect(stdout).not.toContain('Response cache: enabled'); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ hasCache: false, cachePath: null, useCache: false, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('passes --record-replay separately from the response cache', async () => { const fixture = await createFixture(); try { const replayPath = path.join(fixture.baseDir, 'fixtures', 'target-output.jsonl'); const { stdout, exitCode } = await runCli(fixture, [ 'eval', fixture.testFilePath, '--provider', 'cli-target', '--record-replay', replayPath, '--record-replay-variant', 'legal-v1', ]); expect(exitCode).toBe(0); expect(stdout).toContain(`Replay recording: ${replayPath}`); expect(stdout).not.toContain('Response cache: enabled'); const diagnostics = await readDiagnostics(fixture); expect(diagnostics).toMatchObject({ target: 'cli-target', hasCache: false, cachePath: null, useCache: false, replayRecording: { fixturesPath: replayPath, sourceTarget: 'cli-target', variant: 'legal-v1', }, }); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('keeps response cache help separate from transcript replay terminology', async () => { const result = await execa('bun', ['--no-env-file', CLI_ENTRY, 'eval', 'run', '--help'], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); const helpText = `${result.stdout}\n${result.stderr}`; expect(helpText).toContain('--cache'); expect(helpText).toContain('--cache-path'); expect(helpText).toContain('--transcript'); expect(helpText).toContain('--record-replay'); const cacheHelp = helpText .split(/\r?\n/) .filter((line) => line.includes('--cache')) .join('\n') .toLowerCase(); expect(cacheHelp).toContain('response cache'); expect(cacheHelp).not.toContain('replay'); const transcriptHelp = helpText .split(/\r?\n/) .filter((line) => line.includes('--transcript')) .join('\n') .toLowerCase(); expect(transcriptHelp).not.toContain('cache'); const replayHelp = helpText .split(/\r?\n/) .filter((line) => line.includes('--record-replay')) .join('\n') .toLowerCase(); expect(replayHelp).not.toContain('response cache'); }, 30_000); it('omits removed benchmark JSON export flag from help', async () => { const result = await execa('bun', ['--no-env-file', CLI_ENTRY, 'eval', 'run', '--help'], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); const helpText = `${result.stdout}\n${result.stderr}`; expect(helpText).not.toContain('--benchmark-json'); expect(helpText).toContain('--output'); expect(helpText).toContain('summary.json'); }, 30_000); it('omits removed eval dry-run flags from help', async () => { const result = await execa('bun', ['--no-env-file', CLI_ENTRY, 'eval', 'run', '--help'], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); const helpText = `${result.stdout}\n${result.stderr}`; expect(helpText).not.toContain('--dry-run'); expect(helpText).not.toContain('--dry-run-delay'); expect(helpText).not.toContain('--dry-run-delay-min'); expect(helpText).not.toContain('--dry-run-delay-max'); expect(helpText).toContain('--transcript'); expect(helpText).toContain('--record-replay'); }, 30_000); it('keeps non-eval dry-run flags available', async () => { const commands = [ ['results', 'export', '--help'], ['runs', 'rerun', '--help'], ] as const; for (const args of commands) { const result = await execa('bun', ['--no-env-file', CLI_ENTRY, ...args], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); const helpText = `${result.stdout}\n${result.stderr}`; expect(helpText).toContain('--dry-run'); } }, 30_000); it('omits removed promptfoo import command from help', async () => { const helpResult = await execa('bun', ['--no-env-file', CLI_ENTRY, 'import', '--help'], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); const helpText = `${helpResult.stdout}\n${helpResult.stderr}`.toLowerCase(); expect(helpText).not.toContain('promptfoo'); expect(helpText).toContain('claude'); expect(helpText).toContain('codex'); expect(helpText).toContain('copilot'); expect(helpText).toContain('huggingface'); const removedResult = await execa('bun', ['--no-env-file', CLI_ENTRY, 'import', 'promptfoo'], { cwd: projectRoot, env: { ...process.env, CI: 'true' }, reject: false, }); expect(removedResult.exitCode).not.toBe(0); expect(`${removedResult.stdout}\n${removedResult.stderr}`.toLowerCase()).toContain('promptfoo'); }, 30_000); it('rejects the removed benchmark JSON export flag as an unknown argument', async () => { const fixture = await createFixture(); try { const result = await runCli(fixture, [ 'eval', fixture.testFilePath, '--benchmark-json', path.join(fixture.baseDir, 'summary.json'), ]); expect(result.exitCode).not.toBe(0); const output = `${result.stdout}\n${result.stderr}`; expect(output).toContain('Unknown arguments'); expect(output).toContain('--benchmark-json'); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } }, 30_000); it('rejects removed eval dry-run flags as unknown arguments', async () => { const cases = [ ['--dry-run'], ['--dry-run-delay', '10'], ['--dry-run-delay-min', '5'], ['--dry-run-delay-max', '20'], ] as const; for (const args of cases) { const fixture = await createFixture(); try { const result = await runCli(fixture, ['eval', fixture.testFilePath, ...args]); expect(result.exitCode).not.toBe(0); const output = `${result.stdout}\n${result.stderr}`; expect(output).toContain('Unknown arguments'); expect(output).toContain(args[0]); } finally { await rm(fixture.baseDir, { recursive: true, force: true }); } } }, 30_000); });