Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/app/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export async function evaluateAi<T = JsonValue>(
}
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,
Expand Down
24 changes: 21 additions & 3 deletions tests/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonValue>({ 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<JsonValue>({ 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.';
Expand Down Expand Up @@ -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 });
}
});

Expand Down Expand Up @@ -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);
Expand All @@ -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?' });

Expand Down
6 changes: 3 additions & 3 deletions tests/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/ai-checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down