diff --git a/src/app/evaluators.ts b/src/app/evaluators.ts index 0c52006..73ec0ac 100644 --- a/src/app/evaluators.ts +++ b/src/app/evaluators.ts @@ -163,7 +163,7 @@ export async function evaluateAi( } return { status: 'violation', - ...(options.violationMessage === undefined ? {} : { message: options.violationMessage }), + message: options.violationMessage ?? (options.instruction ?? options.rule), probability: decision.probability, threshold: runtime.threshold, model: decision.model, diff --git a/tests/contracts.test.ts b/tests/contracts.test.ts index f14daa1..a95c704 100644 --- a/tests/contracts.test.ts +++ b/tests/contracts.test.ts @@ -990,6 +990,24 @@ test('the public ai evaluator forwards examples, a lazy input override, and the assert.equal(JSON.stringify(requestAt(backend)).includes('Must do the thing'), false); }); +test('an ai violation without violationMessage reports the rule text as its message', async () => { + const subject = scopeSubject({ filepath: 'a.go', id: '$', source: 'package a' }); + const policy = 'Allocate the final length before the loop.'; + const backend = new RecordingBackend(0.1); + const evaluator = ai({ instruction: policy }); + const result = await evaluator(subject, new TestContext({ subject, runtime: runtimeFor(backend), project: new MemoryProject() })); + assert.equal(result.status, 'violation'); + assert.equal(result.message, policy); +}); + +test('an ai violation keeps an explicit violationMessage over the rule text', async () => { + const subject = scopeSubject({ filepath: 'a.go', id: '$', source: 'package a' }); + const backend = new RecordingBackend(0.1); + const evaluator = ai({ instruction: 'Allocate the final length.', violationMessage: 'Preallocate it' }); + const result = await evaluator(subject, new TestContext({ subject, runtime: runtimeFor(backend), project: new MemoryProject() })); + assert.equal(result.message, 'Preallocate it'); +}); + test('the public ai evaluator sends the same request for rule and instruction options', async () => { const subject = scopeSubject({ filepath: 'a.json', id: '$', source: '{}' }); const policy = 'Return an error for invalid input.'; @@ -1216,7 +1234,7 @@ test('AI decisions retain the exact runtime threshold and compare probability st const subject = scopeSubject({ filepath: 'a', id: '$', source: 's' }); const result = await evaluateAi(subject, { rule: 'r' }, projectContext(), runtimeFor(backend, threshold)); - assert.deepEqual(result, { status, probability, threshold, model: 'test-model', usage: UNREPORTED_AI_USAGE }); + assert.deepEqual(result, { status, ...(status === 'violation' ? { message: 'r' } : {}), probability, threshold, model: 'test-model', usage: UNREPORTED_AI_USAGE }); } }); @@ -1251,7 +1269,7 @@ test('AI instructions carry the rule, question, and normalized examples', async }); }); -test('AI violations preserve metadata and only supplied messages without leaking them upstream', async () => { +test('AI violations preserve metadata and report the rule text unless a message is supplied', async () => { const subject = scopeSubject({ filepath: 'a', id: '$', source: 's' }); const backend = new RecordingBackend(0.1); @@ -1262,7 +1280,7 @@ test('AI violations preserve metadata and only supplied messages without leaking runtimeFor(backend), ); assert.deepStrictEqual(result, { - status: 'violation', probability: 0.1, threshold: 0.4, model: 'test-model', usage: UNREPORTED_AI_USAGE, + status: 'violation', message: 'r', probability: 0.1, threshold: 0.4, model: 'test-model', usage: UNREPORTED_AI_USAGE, }); assert.deepStrictEqual(requestAt(backend).instructions, { rule: 'r', question: 'Is the rule respected?' }); diff --git a/tests/engine.test.ts b/tests/engine.test.ts index a74e45c..d37261d 100644 --- a/tests/engine.test.ts +++ b/tests/engine.test.ts @@ -1626,7 +1626,7 @@ test('probability equal to the threshold violates; above it passes', async () => const report = reports[0]; assert.ok(report); const expected: readonly CheckResult[] = [ - { status: 'violation', probability: 0.4, threshold: 0.4, model: 'recording-model', usage: UNREPORTED_AI_USAGE }, + { status: 'violation', message: 'Boundary policy', probability: 0.4, threshold: 0.4, model: 'recording-model', usage: UNREPORTED_AI_USAGE }, { status: 'pass', probability: 0.4001, threshold: 0.4, model: 'recording-model', usage: UNREPORTED_AI_USAGE }, ]; assert.deepEqual(report.evaluations.map(evaluation => evaluation.result), expected); @@ -1647,7 +1647,7 @@ test('rule settings threshold overrides the harness default', async () => { assert.ok(report); assert.equal(report.status, 'violation'); assert.deepEqual(report.evaluations[0]?.result, { - status: 'violation', probability: 0.5, threshold: 0.9, model: 'recording-model', + status: 'violation', message: 'Strict policy', probability: 0.5, threshold: 0.9, model: 'recording-model', usage: UNREPORTED_AI_USAGE, }); assert.deepEqual(eventsOfType(harness.events, 'result')[0]?.result, report.evaluations[0]?.result); @@ -1724,7 +1724,7 @@ test('violationMessage is reporting only and examples are normalized', async () fallbackRun.backend.probabilities = [0.1]; const [fell] = await executeRules([loaded('ai-fallback', fallback)], fallbackRun.context); assert.deepEqual(fell?.evaluations[0]?.result, { - status: 'violation', probability: 0.1, threshold: 0.4, model: 'recording-model', + status: 'violation', message: 'Fallback policy', probability: 0.1, threshold: 0.4, model: 'recording-model', usage: UNREPORTED_AI_USAGE, }); diff --git a/website/src/content/docs/ai-checks.md b/website/src/content/docs/ai-checks.md index 775bbc8..4554724 100644 --- a/website/src/content/docs/ai-checks.md +++ b/website/src/content/docs/ai-checks.md @@ -75,7 +75,7 @@ Write `instruction` as the requirement for compliant content. Start with a direc Add only boundaries or real exceptions needed to define the policy. Keep one policy per instruction. Put reporting text in `violationMessage`, examples in `examples`, and cases in `aiTests`. Use `instruction` for new rules; the deprecated `rule` option remains accepted. Provide exactly one option. -Put every test case for an AI rule in `aiTests`; do not add a separate `tests` export. The fixed question is "Is the rule respected?" Yes/true means no problems. No/false means at least one violation. `examples.good` and `examples.bad` condition the model, and `violationMessage` controls reporting. Neither is an instruction. +Put every test case for an AI rule in `aiTests`; do not add a separate `tests` export. The fixed question is "Is the rule respected?" Yes/true means no problems. No/false means at least one violation. `examples.good` and `examples.bad` condition the model, and `violationMessage` controls reporting. Neither is an instruction. Without `violationMessage`, a violation reports the `instruction` text. For an imperative policy, write: