Add evolver error corrector

This commit is contained in:
Trae AI 2026-05-26 01:31:05 +08:00
parent 17d2d300ee
commit 2f5c508b33
3 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,34 @@
import type { CorrectionAction, CorrectionActionType, ErrorEvent } from './types.js';
const createdAt = '2026-05-25T00:00:00.000Z';
const ERROR_ACTION_MAP: Record<ErrorEvent['type'], CorrectionActionType> = {
'temporary-failure': 'retry',
'parameter-degradation': 'rollback-parameters',
'algorithm-regression': 'disable-variant',
'structure-risk': 'request-structure-review',
'resource-exhaustion': 'reduce-budget',
'evaluation-distortion': 'freeze-promotion',
'safety-boundary-triggered': 'stop-plan',
};
const ERROR_REASON_MAP: Record<ErrorEvent['type'], string> = {
'temporary-failure': 'temporary failure may succeed on retry',
'parameter-degradation': 'parameter change caused degradation, rollback required',
'algorithm-regression': 'algorithm change caused regression, variant must be disabled',
'structure-risk': 'structure change poses risk, manual review required',
'resource-exhaustion': 'resource limits exceeded, budget must be reduced',
'evaluation-distortion': 'evaluation results are unreliable, promotion must be frozen',
'safety-boundary-triggered': 'safety boundary violated, plan must be stopped',
};
export function selectCorrectionAction(errorEvent: ErrorEvent): CorrectionAction {
return {
id: `correction-${errorEvent.id}`,
errorEventId: errorEvent.id,
type: ERROR_ACTION_MAP[errorEvent.type],
reason: ERROR_REASON_MAP[errorEvent.type],
createdAt,
variantId: errorEvent.variantId,
};
}

View file

@ -6,3 +6,4 @@ export * from './sandbox-runner.js';
export * from './safety-gate.js';
export * from './promotion-controller.js';
export * from './evolution-memory.js';
export * from './error-corrector.js';

View file

@ -0,0 +1,66 @@
import { describe, expect, it } from 'vitest';
import { selectCorrectionAction } from '../../../src/core/evolver/index.js';
import type { ErrorEvent } from '../../../src/core/evolver/index.js';
const occurredAt = '2026-05-25T00:00:00.000Z';
function errorEvent(type: ErrorEvent['type'], variantId = 'variant-1'): ErrorEvent {
return {
id: `error-${type}`,
type,
message: `${type} occurred`,
occurredAt,
variantId,
};
}
describe('selectCorrectionAction', () => {
it('maps temporary-failure to retry', () => {
const action = selectCorrectionAction(errorEvent('temporary-failure'));
expect(action).toEqual({
id: `correction-error-temporary-failure`,
errorEventId: 'error-temporary-failure',
type: 'retry',
reason: 'temporary failure may succeed on retry',
createdAt: occurredAt,
variantId: 'variant-1',
});
});
it('maps parameter-degradation to rollback-parameters', () => {
const action = selectCorrectionAction(errorEvent('parameter-degradation'));
expect(action.type).toBe('rollback-parameters');
expect(action.errorEventId).toBe('error-parameter-degradation');
expect(action.variantId).toBe('variant-1');
});
it('maps algorithm-regression to disable-variant', () => {
const action = selectCorrectionAction(errorEvent('algorithm-regression'));
expect(action.type).toBe('disable-variant');
expect(action.errorEventId).toBe('error-algorithm-regression');
});
it('maps structure-risk to request-structure-review', () => {
const action = selectCorrectionAction(errorEvent('structure-risk'));
expect(action.type).toBe('request-structure-review');
expect(action.errorEventId).toBe('error-structure-risk');
});
it('maps resource-exhaustion to reduce-budget', () => {
const action = selectCorrectionAction(errorEvent('resource-exhaustion'));
expect(action.type).toBe('reduce-budget');
expect(action.errorEventId).toBe('error-resource-exhaustion');
});
it('maps evaluation-distortion to freeze-promotion', () => {
const action = selectCorrectionAction(errorEvent('evaluation-distortion'));
expect(action.type).toBe('freeze-promotion');
expect(action.errorEventId).toBe('error-evaluation-distortion');
});
it('maps safety-boundary-triggered to stop-plan', () => {
const action = selectCorrectionAction(errorEvent('safety-boundary-triggered'));
expect(action.type).toBe('stop-plan');
expect(action.errorEventId).toBe('error-safety-boundary-triggered');
});
});