Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"watch": "^1.0.2"
},
"dependencies": {
"@paralleldrive/cuid2": "^3.3.0",
"cheerio": "1.2.0",
"dotignore": "^0.1.2",
"error-causes": "^3.0.2",
Expand Down
23 changes: 4 additions & 19 deletions source/agent-config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { readFile } from 'fs/promises';
import { z } from 'zod';
import { createError } from 'error-causes';
import { ValidationError } from './ai-errors.js';
import { ValidationError, AgentConfigReadError, AgentConfigParseError, AgentConfigValidationError, formatZodError } from './ai-errors.js';
import { parseOpenCodeNDJSON } from './agent-parser.js';

/**
* Format Zod validation errors into a human-readable message.
* @param {any} zodError - Zod validation error
* @returns {string} Formatted error message
*/
export const formatZodError = (zodError) => {
const issues = zodError.issues || zodError.errors;
return issues
? issues.map(e => `${e.path.join('.')}: ${e.message}`).join('; ')
: zodError.message || 'Validation failed';
};

/**
* Get agent configuration based on agent name.
* Supports 'claude', 'opencode', and 'cursor' agents.
Expand Down Expand Up @@ -62,9 +50,8 @@ const readAgentConfigFile = async ({ configPath }) => {
return await readFile(configPath, 'utf-8');
} catch (err) {
throw createError({
...ValidationError,
...AgentConfigReadError,
message: `Failed to read agent config file: ${configPath}`,
code: 'AGENT_CONFIG_READ_ERROR',
cause: err
});
}
Expand All @@ -75,9 +62,8 @@ const parseJson = ({ configPath, raw }) => {
return JSON.parse(raw);
} catch (err) {
throw createError({
...ValidationError,
...AgentConfigParseError,
message: `Agent config file is not valid JSON: ${configPath}`,
code: 'AGENT_CONFIG_PARSE_ERROR',
cause: err
});
}
Expand All @@ -88,9 +74,8 @@ const validateAgentConfig = (parsed) => {
return agentConfigFileSchema.parse(parsed);
} catch (zodError) {
throw createError({
...ValidationError,
...AgentConfigValidationError,
message: `Invalid agent config: ${formatZodError(zodError)}`,
code: 'AGENT_CONFIG_VALIDATION_ERROR',
cause: zodError
});
}
Expand Down
86 changes: 25 additions & 61 deletions source/agent-config.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, test } from 'vitest';
import { assert } from './vitest.js';
import { Try } from './riteway.js';
import { formatZodError, getAgentConfig, loadAgentConfig } from './agent-config.js';
import { handleAIErrors, allNoop, formatZodError } from './ai-errors.js';
import { getAgentConfig, loadAgentConfig } from './agent-config.js';

describe('formatZodError()', () => {
test('formats a single issue', () => {
Expand Down Expand Up @@ -162,18 +163,14 @@ describe('getAgentConfig()', () => {
test('throws ValidationError for invalid agent name', () => {
const error = Try(getAgentConfig, 'invalid-agent');

assert({
given: 'invalid agent name',
should: 'throw Error with cause',
actual: error instanceof Error && error.cause !== undefined,
expected: true
});
const invoked = [];
handleAIErrors({ ...allNoop, ValidationError: () => invoked.push('ValidationError') })(error);

assert({
given: 'invalid agent name',
should: 'have ValidationError name in cause',
actual: error?.cause?.name,
expected: 'ValidationError'
should: 'throw an error that routes to the ValidationError handler',
actual: invoked,
expected: ['ValidationError']
});

assert({
Expand Down Expand Up @@ -218,78 +215,45 @@ describe('loadAgentConfig()', () => {
});
});

test('throws ValidationError with AGENT_CONFIG_PARSE_ERROR for invalid JSON', async () => {
test('throws AgentConfigParseError for invalid JSON', async () => {
const error = await Try(loadAgentConfig, './source/fixtures/invalid-agent-config.txt');

assert({
given: 'invalid JSON file',
should: 'throw Error with cause',
actual: error instanceof Error && error.cause !== undefined,
expected: true
});

assert({
given: 'invalid JSON file',
should: 'have ValidationError name in cause',
actual: error?.cause?.name,
expected: 'ValidationError'
});
const invoked = [];
handleAIErrors({ ...allNoop, AgentConfigParseError: () => invoked.push('AgentConfigParseError') })(error);

assert({
given: 'invalid JSON file',
should: 'have AGENT_CONFIG_PARSE_ERROR code in cause',
actual: error?.cause?.code,
expected: 'AGENT_CONFIG_PARSE_ERROR'
should: 'throw an error that routes to the AgentConfigParseError handler',
actual: invoked,
expected: ['AgentConfigParseError']
});
});

test('throws ValidationError with AGENT_CONFIG_VALIDATION_ERROR when command field missing', async () => {
test('throws AgentConfigValidationError when command field missing', async () => {
const error = await Try(loadAgentConfig, './source/fixtures/no-command-agent-config.json');

assert({
given: 'config file missing command field',
should: 'throw Error with cause',
actual: error instanceof Error && error.cause !== undefined,
expected: true
});

assert({
given: 'config file missing command field',
should: 'have ValidationError name in cause',
actual: error?.cause?.name,
expected: 'ValidationError'
});
const invoked = [];
handleAIErrors({ ...allNoop, AgentConfigValidationError: () => invoked.push('AgentConfigValidationError') })(error);

assert({
given: 'config file missing command field',
should: 'have AGENT_CONFIG_VALIDATION_ERROR code in cause',
actual: error?.cause?.code,
expected: 'AGENT_CONFIG_VALIDATION_ERROR'
should: 'throw an error that routes to the AgentConfigValidationError handler',
actual: invoked,
expected: ['AgentConfigValidationError']
});
});

test('throws ValidationError with AGENT_CONFIG_READ_ERROR for nonexistent file', async () => {
test('throws AgentConfigReadError for nonexistent file', async () => {
const error = await Try(loadAgentConfig, './nonexistent/path.json');

assert({
given: 'nonexistent file path',
should: 'throw Error with cause',
actual: error instanceof Error && error.cause !== undefined,
expected: true
});

assert({
given: 'nonexistent file path',
should: 'have ValidationError name in cause',
actual: error?.cause?.name,
expected: 'ValidationError'
});
const invoked = [];
handleAIErrors({ ...allNoop, AgentConfigReadError: () => invoked.push('AgentConfigReadError') })(error);

assert({
given: 'nonexistent file path',
should: 'have AGENT_CONFIG_READ_ERROR code in cause',
actual: error?.cause?.code,
expected: 'AGENT_CONFIG_READ_ERROR'
should: 'throw an error that routes to the AgentConfigReadError handler',
actual: invoked,
expected: ['AgentConfigReadError']
});
});
});
24 changes: 22 additions & 2 deletions source/ai-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export const [aiErrors, handleAIErrors] = errorCauses({
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' }
ExtractionValidationError: { code: 'EXTRACTION_VALIDATION_FAILURE', message: 'Invalid extraction result' },
AgentConfigReadError: { code: 'AGENT_CONFIG_READ_ERROR', message: 'Failed to read agent config file' },
AgentConfigParseError: { code: 'AGENT_CONFIG_PARSE_ERROR', message: 'Agent config file is not valid JSON' },
AgentConfigValidationError: { code: 'AGENT_CONFIG_VALIDATION_ERROR', message: 'Invalid agent config' }
});

// handleAIErrors is exhaustive — every registered type must have a handler.
Expand All @@ -26,5 +29,22 @@ export const {
AITestError,
OutputError,
ExtractionParseError,
ExtractionValidationError
ExtractionValidationError,
AgentConfigReadError,
AgentConfigParseError,
AgentConfigValidationError
} = aiErrors;

/**
* Format Zod validation errors into a human-readable message.
* Lives here because ai-runner.js also needs Zod error formatting; importing
* a utility from agent-config.js would create a wrong dependency direction.
* @param {any} zodError - Zod validation error
* @returns {string} Formatted error message
*/
export const formatZodError = (zodError) => {
const issues = zodError.issues || zodError.errors;
return issues
? issues.map(e => `${e.path.join('.')}: ${e.message}`).join('; ')
: zodError.message || 'Validation failed';
};
Loading