repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/overkill/__tests__/aftermath-mining.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import {
	assignSplits,
	chunkConsecutively,
	estimateMiningCalls,
	mineTaskProposals,
	validateAndFreezeTasks,
} from "../lib/aftermath-mining.ts";

describe("Aftermath mining contracts", () => {
	it("chunks consecutively and estimates recursive aggregation", () => {
		expect(
			chunkConsecutively(
				Array.from({ length: 21 }, (_, index) => index),
				20,
			).map((group) => group.length),
		).toEqual([20, 1]);
		expect(estimateMiningCalls(401)).toEqual({
			mining: 21,
			aggregation: 3,
			total: 24,
		});
	});

	it("mines deterministic bounded chunks through fresh structured sessions", async () => {
		const prompts: string[] = [];
		const proposals = await mineTaskProposals({
			digests: [{ sessionId: "a" }, { sessionId: "b" }],
			onCall: async () => {
				prompts.push("attempt persisted");
			},
			async createSession() {
				let listener: ((event: any) => void) | undefined;
				return {
					subscribe(next: (event: any) => void) {
						listener = next;
						return () => {};
					},
					async prompt(prompt: string) {
						prompts.push(prompt);
						listener?.({
							type: "tool_execution_end",
							toolName: "structured_output",
							isError: false,
							result: { details: { tasks: [{ intent: "x" }] } },
						});
					},
					async abort() {},
					dispose() {},
				};
			},
		});
		expect(proposals).toEqual([{ intent: "x" }]);
		expect(prompts).toEqual([
			"attempt persisted",
			'phase:mine\nExtract no more than 10 recurring, checkable task proposals. Return only {"tasks": [...]}.\ndigests:[{"sessionId":"a"},{"sessionId":"b"}]',
		]);
	});

	it("freezes only recurring exact proposals with extension-derived IDs", () => {
		const tasks = validateAndFreezeTasks({
			proposals: [
				{
					intent: " Answer ",
					context: "evidence",
					sourceSessionIds: ["b", "a"],
					checks: [{ type: "contains", value: "yes", caseSensitive: true }],
				},
				{
					intent: "Answer",
					context: "evidence",
					sourceSessionIds: ["a", "b"],
					checks: [{ type: "contains", value: "yes", caseSensitive: true }],
				},
				{ intent: "bad", context: "x", sourceSessionIds: ["a"], rubric: "x" },
			],
			harvestedSessionIds: new Set(["a", "b"]),
		});
		expect(tasks).toHaveLength(1);
		expect(tasks[0]?.id).toMatch(/^task-[a-f0-9]{64}$/);
		expect(tasks[0]?.sourceSessionIds).toEqual(["a", "b"]);
	});

	it("normalizes every hashed string boundary before deduplication", () => {
		const tasks = validateAndFreezeTasks({
			proposals: [
				{
					intent: " Intent ",
					context: " Context ",
					sourceSessionIds: ["a", "b"],
					observedOutcome: " Outcome ",
					checks: [{ type: "contains", value: " Value ", caseSensitive: true }],
				},
				{
					intent: "Intent",
					context: "Context",
					sourceSessionIds: ["b", "a"],
					observedOutcome: "Outcome",
					checks: [{ type: "contains", value: "Value", caseSensitive: true }],
				},
			],
			harvestedSessionIds: new Set(["a", "b"]),
		});
		expect(tasks).toHaveLength(1);
	});

	it("creates deterministic nonempty disjoint splits", () => {
		const tasks = validateAndFreezeTasks({
			proposals: ["a", "b", "c"].map((intent) => ({
				intent,
				context: "context",
				sourceSessionIds: ["x", "y"],
				rubric: "judge",
			})),
			harvestedSessionIds: new Set(["x", "y"]),
		});
		const splits = assignSplits(tasks);
		expect(splits.train.length).toBeGreaterThan(0);
		expect(splits.validation.length).toBeGreaterThan(0);
		expect(splits.test.length).toBeGreaterThan(0);
		expect(
			new Set([...splits.train, ...splits.validation, ...splits.test]).size,
		).toBe(3);
	});
});