repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/overkill/__tests__/aftermath-scoring.test.ts
RawFile outline
- Aftermath deterministic response scoring line 8
- uses literal matching with optional Unicode lowercase matching line 9
- normalizes only ATX headings with closing hashes and whitespace line 27
- counts Unicode code points and preserves fractional all-check semantics line 42
- adapts immutable task intent/context and local scores without a model line 57
- uses one fresh terminating judge session, bounded rationale, cache key, cancellation, and disposal line 83
- rejects malformed judge output and disposes on abort line 145
- preserves observed outcomes in immutable rubric prompts line 181
- returns a completed structured judgment despite concurrent cancellation line 219
- hashes the normalized bounded response as lowercase SHA-256 line 258
import { describe, expect, it } from "vitest";
import {
evaluateDeterministicChecks,
responseHash,
runRubricJudge,
} from "../lib/aftermath-scoring.ts";
describe("Aftermath deterministic response scoring", () => {
it("uses literal matching with optional Unicode lowercase matching", () => {
expect(
evaluateDeterministicChecks("A Straße and σ", [
{ type: "contains", value: "Straße", caseSensitive: true },
{ type: "contains", value: "STRAẞE", caseSensitive: false },
{ type: "contains", value: "Σ", caseSensitive: false },
]),
).toEqual({
score: 1,
passed: true,
checks: [
{ type: "contains", passed: true },
{ type: "contains", passed: true },
{ type: "contains", passed: true },
],
});
});
it("normalizes only ATX headings with closing hashes and whitespace", () => {
const scored = evaluateDeterministicChecks(
"Title\r\n## Acceptance Criteria ###\rbody\nAcceptance Criteria\n---",
[
{ type: "section_present", heading: "acceptance criteria" },
{ type: "section_present", heading: "title" },
],
);
expect(scored).toMatchObject({ score: 0.5, passed: false });
expect(scored.checks).toEqual([
{ type: "section_present", passed: true },
{ type: "section_present", passed: false },
]);
});
it("counts Unicode code points and preserves fractional all-check semantics", () => {
const scored = evaluateDeterministicChecks("a😀", [
{ type: "min_chars", count: 2 },
{ type: "max_chars", count: 2 },
{ type: "contains", value: "missing", caseSensitive: true },
]);
expect(scored.score).toBe(2 / 3);
expect(scored.passed).toBe(false);
expect(scored.checks.map((check) => check.passed)).toEqual([
true,
true,
false,
]);
});
it("adapts immutable task intent/context and local scores without a model", async () => {
const { createGeneratedTaskScorer, generatedTaskPrompt } = await import(
"../lib/aftermath-scoring.ts"
);
const task = {
id: "task-local",
intent: "Explain the change",
context: "Use only frozen evidence.",
sourceSessionIds: ["a", "b"],
checks: [
{ type: "contains" as const, value: "done", caseSensitive: false },
],
};
expect(generatedTaskPrompt(task)).toBe(
"Explain the change\n\nContext:\nUse only frozen evidence.",
);
await expect(
createGeneratedTaskScorer([task])({ id: task.id, response: "DONE" }),
).resolves.toEqual({
score: 1,
passed: true,
feedback: "1/1 deterministic checks passed",
checks: [{ type: "contains", passed: true }],
});
});
it("uses one fresh terminating judge session, bounded rationale, cache key, cancellation, and disposal", async () => {
const listeners = new Set<(event: any) => void>();
const session = {
prompts: [] as string[],
disposed: 0,
aborted: 0,
subscribe(listener: (event: any) => void) {
listeners.add(listener);
return () => listeners.delete(listener);
},
async prompt(prompt: string) {
this.prompts.push(prompt);
for (const listener of listeners)
listener({
type: "tool_execution_end",
toolName: "structured_output",
isError: false,
result: {
details: { score: 0.75, passed: false, rationale: "evidence" },
},
});
},
async abort() {
this.aborted += 1;
},
dispose() {
this.disposed += 1;
},
};
let attemptedCalls = 0;
const judged = await runRubricJudge(
{
id: "task-abc",
intent: "write a response",
context: "frozen context",
sourceSessionIds: ["one", "two"],
rubric: "assess it",
},
"final response",
{
async createSession() {
return session;
},
},
undefined,
async () => {
attemptedCalls += 1;
},
);
expect(attemptedCalls).toBe(1);
expect(judged).toMatchObject({
score: 0.75,
passed: false,
rationale: "evidence",
});
expect(judged.cacheKey).toBe(`task-abc:${responseHash("final response")}`);
expect(session.prompts).toHaveLength(1);
expect(session.prompts[0]).toContain('"judgePromptVersion":1');
expect(session.prompts[0]).toContain("frozen context");
expect(session.disposed).toBe(1);
});
it("rejects malformed judge output and disposes on abort", async () => {
const controller = new AbortController();
const session = {
subscribe() {
return () => {};
},
async prompt() {
controller.abort();
},
async abort() {},
disposed: 0,
dispose() {
this.disposed += 1;
},
};
await expect(
runRubricJudge(
{
id: "task-x",
intent: "x",
context: "y",
sourceSessionIds: ["a", "b"],
rubric: "z",
},
"response",
{
async createSession() {
return session;
},
},
controller.signal,
),
).rejects.toThrow("cancelled");
expect(session.disposed).toBe(1);
});
it("preserves observed outcomes in immutable rubric prompts", async () => {
const session = {
recordedPrompt: "",
subscribe(listener: (event: any) => void) {
this.listener = listener;
return () => {};
},
async prompt(value: string) {
this.recordedPrompt = value;
this.listener({
type: "tool_execution_end",
toolName: "structured_output",
isError: false,
result: { details: { score: 1, passed: true, rationale: "ok" } },
});
},
async abort() {},
dispose() {},
};
await runRubricJudge(
{
id: "task-outcome",
intent: "x",
context: "y",
sourceSessionIds: ["a", "b"],
observedOutcome: "the user accepted the result",
rubric: "z",
},
"response",
{
async createSession() {
return session;
},
},
);
expect(session.recordedPrompt).toContain("the user accepted the result");
});
it("returns a completed structured judgment despite concurrent cancellation", async () => {
const controller = new AbortController();
const session = {
subscribe(listener: (event: any) => void) {
this.listener = listener;
return () => {};
},
async prompt() {
this.listener({
type: "tool_execution_end",
toolName: "structured_output",
isError: false,
result: { details: { score: 1, passed: true, rationale: "saved" } },
});
controller.abort();
},
async abort() {},
dispose() {},
};
await expect(
runRubricJudge(
{
id: "task-cache",
intent: "x",
context: "y",
sourceSessionIds: ["a", "b"],
rubric: "z",
},
"response",
{
async createSession() {
return session;
},
},
controller.signal,
),
).resolves.toMatchObject({ rationale: "saved" });
});
it("hashes the normalized bounded response as lowercase SHA-256", () => {
expect(responseHash("a\r\nb")).toBe(
"7e18f737311b2dc3b2f269dd78396b0351f14fb66efa879f768cb23181883c78",
);
});
});