-
Notifications
You must be signed in to change notification settings - Fork 37
feat(ai): Foundation — Error Types + Constants [PR 1/7] #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ianwhitedeveloper
merged 3 commits into
ai-testing-framework-implementation-consolidation
from
pr/ai-foundation-errors-constants
Feb 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { errorCauses } from 'error-causes'; | ||
|
|
||
| // Error types for AI agent execution and related operations | ||
| export const [aiErrors, handleAIErrors] = errorCauses({ | ||
| ParseError: { code: 'PARSE_FAILURE', message: 'Failed to parse AI response' }, | ||
| ValidationError: { code: 'VALIDATION_FAILURE', message: 'Invalid input parameters' }, | ||
| SecurityError: { code: 'SECURITY_VIOLATION', message: 'Security violation detected' }, | ||
| TimeoutError: { code: 'AGENT_TIMEOUT', message: 'AI agent timed out' }, | ||
| AgentProcessError: { code: 'AGENT_PROCESS_FAILURE', message: 'AI agent process failed' }, | ||
| AITestError: { code: 'AI_TEST_ERROR', message: 'AI test execution failed' }, | ||
| OutputError: { code: 'OUTPUT_ERROR', message: 'Test output recording failed' }, | ||
| ExtractionParseError: { code: 'EXTRACTION_PARSE_FAILURE', message: 'Failed to parse extraction result' }, | ||
| ExtractionValidationError: { code: 'EXTRACTION_VALIDATION_FAILURE', message: 'Invalid extraction result' } | ||
| }); | ||
|
|
||
| export const { | ||
| ParseError, | ||
| ValidationError, | ||
| SecurityError, | ||
| TimeoutError, | ||
| AgentProcessError, | ||
| AITestError, | ||
| OutputError, | ||
| ExtractionParseError, | ||
| ExtractionValidationError | ||
| } = aiErrors; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { describe, test } from 'vitest'; | ||
| import { assert } from './vitest.js'; | ||
| import { createError } from 'error-causes'; | ||
| import { | ||
| aiErrors, | ||
| handleAIErrors, | ||
| ParseError, | ||
| ValidationError, | ||
| SecurityError, | ||
| TimeoutError, | ||
| AgentProcessError, | ||
| AITestError, | ||
| OutputError, | ||
| ExtractionParseError, | ||
| ExtractionValidationError | ||
| } from './ai-errors.js'; | ||
|
|
||
| const errorTable = [ | ||
| ['ParseError', 'PARSE_FAILURE', ParseError], | ||
| ['ValidationError', 'VALIDATION_FAILURE', ValidationError], | ||
| ['SecurityError', 'SECURITY_VIOLATION', SecurityError], | ||
| ['TimeoutError', 'AGENT_TIMEOUT', TimeoutError], | ||
| ['AgentProcessError', 'AGENT_PROCESS_FAILURE', AgentProcessError], | ||
| ['AITestError', 'AI_TEST_ERROR', AITestError], | ||
| ['OutputError', 'OUTPUT_ERROR', OutputError], | ||
| ['ExtractionParseError', 'EXTRACTION_PARSE_FAILURE', ExtractionParseError], | ||
| ['ExtractionValidationError','EXTRACTION_VALIDATION_FAILURE', ExtractionValidationError], | ||
| ]; | ||
|
|
||
| describe('ai-errors module', () => { | ||
| describe('error descriptors', () => { | ||
| test.each(errorTable)('%s descriptor has correct name', (name) => { | ||
| assert({ | ||
| given: `${name} descriptor`, | ||
| should: 'have the correct error name', | ||
| actual: aiErrors[name].name, | ||
| expected: name | ||
| }); | ||
| }); | ||
|
|
||
| test.each(errorTable)('%s descriptor has correct code', (name, code) => { | ||
| assert({ | ||
| given: `${name} descriptor`, | ||
| should: 'have the correct error code', | ||
| actual: aiErrors[name].code, | ||
| expected: code | ||
| }); | ||
| }); | ||
|
|
||
| test.each(errorTable)('%s named export matches aiErrors entry', (name, _code, descriptor) => { | ||
| assert({ | ||
| given: `destructured ${name} export`, | ||
| should: 'equal aiErrors entry', | ||
| actual: descriptor, | ||
| expected: aiErrors[name] | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createError integration', () => { | ||
| test.each([ | ||
| ['ParseError', ParseError, 'PARSE_FAILURE'], | ||
| ['SecurityError', SecurityError, 'SECURITY_VIOLATION'], | ||
| ])('%s produces an Error with structured cause', (name, descriptor, code) => { | ||
| const err = createError(descriptor); | ||
|
|
||
| assert({ | ||
| given: `a ${name} descriptor`, | ||
| should: 'produce an Error instance', | ||
| actual: err instanceof Error, | ||
| expected: true | ||
| }); | ||
|
|
||
| assert({ | ||
| given: `a ${name} descriptor`, | ||
| should: `set cause.name to ${name}`, | ||
| actual: err.cause.name, | ||
| expected: name | ||
| }); | ||
|
|
||
| assert({ | ||
| given: `a ${name} descriptor`, | ||
| should: `set cause.code to ${code}`, | ||
| actual: err.cause.code, | ||
| expected: code | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleAIErrors', () => { | ||
| // handleAIErrors is exhaustive: every registered error type must have a handler. | ||
| // Build a no-op map for all types, then override the one under test. | ||
| const noop = () => {}; | ||
| const allNoop = { | ||
| ParseError: noop, ValidationError: noop, SecurityError: noop, | ||
| TimeoutError: noop, AgentProcessError: noop, AITestError: noop, | ||
| OutputError: noop, ExtractionParseError: noop, ExtractionValidationError: noop | ||
| }; | ||
|
|
||
| test('routes a ParseError to the ParseError handler', () => { | ||
| const err = createError(ParseError); | ||
| const invoked = []; | ||
|
|
||
| handleAIErrors({ ...allNoop, ParseError: () => invoked.push('ParseError') })(err); | ||
|
|
||
| assert({ | ||
| given: 'an error created from ParseError', | ||
| should: 'invoke only the ParseError handler', | ||
| actual: invoked, | ||
| expected: ['ParseError'] | ||
| }); | ||
| }); | ||
|
|
||
| test('routes a ValidationError to the ValidationError handler', () => { | ||
| const err = createError(ValidationError); | ||
| const invoked = []; | ||
|
|
||
| handleAIErrors({ ...allNoop, ValidationError: () => invoked.push('ValidationError') })(err); | ||
|
|
||
| assert({ | ||
| given: 'an error created from ValidationError', | ||
| should: 'invoke only the ValidationError handler', | ||
| actual: invoked, | ||
| expected: ['ValidationError'] | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const defaults = { | ||
| runs: 4, | ||
| threshold: 75, | ||
| concurrency: 4, | ||
| agent: 'claude', | ||
| timeoutMs: 300_000, | ||
| color: false, | ||
| debug: false, | ||
| debugLog: false | ||
| }; | ||
|
|
||
| export const constraints = { | ||
| thresholdMin: 0, | ||
| thresholdMax: 100, | ||
| runsMin: 1, | ||
| runsMax: 1000, | ||
| concurrencyMin: 1, | ||
| concurrencyMax: 50, | ||
| timeoutMinMs: 1000, | ||
| timeoutMaxMs: 3_600_000, | ||
| supportedAgents: ['claude', 'opencode', 'cursor'] | ||
| }; | ||
|
|
||
| export const runsSchema = z.number() | ||
| .int({ message: 'runs must be an integer' }) | ||
| .min(constraints.runsMin, { message: `runs must be at least ${constraints.runsMin}` }) | ||
| .max(constraints.runsMax, { message: `runs must be at most ${constraints.runsMax}` }); | ||
|
|
||
| export const thresholdSchema = z.number() | ||
| .finite({ message: 'threshold must be a finite number' }) | ||
| .min(constraints.thresholdMin, { message: `threshold must be at least ${constraints.thresholdMin}` }) | ||
| .max(constraints.thresholdMax, { message: `threshold must be at most ${constraints.thresholdMax}` }); | ||
|
|
||
| export const concurrencySchema = z.number() | ||
| .int({ message: 'concurrency must be an integer' }) | ||
| .min(constraints.concurrencyMin, { message: `concurrency must be at least ${constraints.concurrencyMin}` }) | ||
| .max(constraints.concurrencyMax, { message: `concurrency must be at most ${constraints.concurrencyMax}` }); | ||
|
|
||
| export const timeoutSchema = z.number() | ||
| .int({ message: 'timeout must be an integer' }) | ||
| .min(constraints.timeoutMinMs, { message: `timeout must be at least ${constraints.timeoutMinMs}ms` }) | ||
| .max(constraints.timeoutMaxMs, { message: `timeout must be at most ${constraints.timeoutMaxMs}ms` }); | ||
|
|
||
| export const agentSchema = z.enum(constraints.supportedAgents, { | ||
| message: `agent must be one of: ${constraints.supportedAgents.join(', ')}` | ||
| }); | ||
|
|
||
| export const calculateRequiredPassesSchema = z.object({ | ||
| runs: runsSchema, | ||
| threshold: thresholdSchema | ||
| }); | ||
|
|
||
| export const aiTestOptionsSchema = z.object({ | ||
| filePath: z.string().min(1, { message: 'Test file path is required' }), | ||
| runs: runsSchema.default(defaults.runs), | ||
| threshold: thresholdSchema.default(defaults.threshold), | ||
| timeout: timeoutSchema.default(defaults.timeoutMs), | ||
| concurrency: concurrencySchema.default(defaults.concurrency), | ||
| agent: agentSchema.default(defaults.agent), | ||
| agentConfigPath: z.string().optional(), | ||
| debug: z.boolean().default(defaults.debug), | ||
| debugLog: z.boolean().default(defaults.debugLog), | ||
| color: z.boolean().default(defaults.color), | ||
| // Lazy default — evaluated at parse time, not module load time | ||
| cwd: z.string().default(() => process.cwd()), | ||
| projectRoot: z.string().optional() | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment to this testing file. We're kind just testing the error causes API.
If you want to test these at all, you should probably test them as pairs of getters and setters:
The
createErroris the setter,handleAIErrorsis the getter. That's the only contract worth testing — that errors you create actually route to the right handler with the right cause. The descriptor shape tests (name,code, named export equality) are just testing thaterrorCausesreturns what you passed in.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just testing error causes. Don't do that. Do test that code that uses these throws the correct errors, and that functions handle them correctly. In other words, test how your consuming code USES errors, instead of testing the already tested error-causes API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remediated in PR #412: the
error descriptorsdescribe block (27 tests over 9 error types testing descriptor shape) and thecreateError integrationdescribe block have been deleted. Only thehandleAIErrorsbehavioral routing tests remain — createError as setter, handleAIErrors as getter. See: #412