From 2f5c508b33e3fd1b5b6d081ede1d34219320e90c Mon Sep 17 00:00:00 2001 From: Trae AI Date: Tue, 26 May 2026 01:31:05 +0800 Subject: [PATCH] Add evolver error corrector --- gitnexus/src/core/evolver/error-corrector.ts | 34 ++++++++++ gitnexus/src/core/evolver/index.ts | 1 + .../test/unit/evolver/error-corrector.test.ts | 66 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 gitnexus/src/core/evolver/error-corrector.ts create mode 100644 gitnexus/test/unit/evolver/error-corrector.test.ts diff --git a/gitnexus/src/core/evolver/error-corrector.ts b/gitnexus/src/core/evolver/error-corrector.ts new file mode 100644 index 000000000..a6a2bbe8b --- /dev/null +++ b/gitnexus/src/core/evolver/error-corrector.ts @@ -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 = { + '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 = { + '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, + }; +} diff --git a/gitnexus/src/core/evolver/index.ts b/gitnexus/src/core/evolver/index.ts index e4958c92f..5b4dc683d 100644 --- a/gitnexus/src/core/evolver/index.ts +++ b/gitnexus/src/core/evolver/index.ts @@ -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'; diff --git a/gitnexus/test/unit/evolver/error-corrector.test.ts b/gitnexus/test/unit/evolver/error-corrector.test.ts new file mode 100644 index 000000000..1528fd3d6 --- /dev/null +++ b/gitnexus/test/unit/evolver/error-corrector.test.ts @@ -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'); + }); +});