repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ultra/__tests__/prompt.test.ts

Raw
File outline
import * as fs from "node:fs";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { VALIDATOR_CONTRACT_VERSION } from "../dynamic-contract.ts";
import {
	DYNAMIC_EXTENSION_TOOL_NAMES,
	parseWorkflow,
	WorkflowSpecSchema,
} from "../spec.ts";

const PROMPT_PATH = fileURLToPath(
	new URL("../prompts/authoring-workflows.md", import.meta.url),
);
const SPEC_FORMAT_PATH = fileURLToPath(
	new URL("../prompts/references/spec-format.md", import.meta.url),
);
const README_PATH = fileURLToPath(new URL("../README.md", import.meta.url));
const BUILDER_PROMPT_PATH = fileURLToPath(
	new URL("../prompts/dynamic-extension-builder.md", import.meta.url),
);

describe("authoring-workflows injected prompt", () => {
	it("does not bundle a model-invocable skill", () => {
		const packageJson = JSON.parse(
			fs.readFileSync(
				fileURLToPath(new URL("../package.json", import.meta.url)),
				"utf8",
			),
		);
		expect(packageJson.pi).toEqual({ extensions: ["index.ts"] });
		expect(
			fs.existsSync(fileURLToPath(new URL("../skills", import.meta.url))),
		).toBe(false);
	});

	it("keeps invocation-critical semantics in prompt assets", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");

		expect(prompt).toContain("# Authoring ultra workflows");
		expect(prompt).toContain("/ultra run <name>");
		expect(prompt).toContain("`run_workflow`");
		expect(prompt).not.toContain("name: authoring-workflows");
		expect(reference).toContain("/ultra run <name>");
		expect(reference).toContain("Every step returns a JSON object");
		expect(prompt).toContain("Ultra refuses to start the workflow");
		expect(reference).toContain("Ultra refuses to start the workflow");
		expect(prompt).toContain("When confidence is high, act on that choice");
		expect(prompt).toContain(
			"When confidence is not high, ask one focused question",
		);
		expect(prompt).toContain(
			"Do not treat `/ultra exec` itself or generic creation verbs as evidence for persistence",
		);
		expect(prompt).not.toContain(
			"When in genuine doubt with creation language, persist",
		);
		const readme = fs.readFileSync(README_PATH, "utf8");
		const selectionPolicy =
			"independent parallel work, staged agent handoffs, or isolated specialist contexts materially improve the result";
		expect(prompt).toContain(selectionPolicy);
		expect(readme).toContain(selectionPolicy);
		expect(readme).toContain("`/ultra exec <instructions>`");
		expect(readme).toContain("`/ultra run <name> [args]`");
		expect(readme).toContain("`run_workflow` tool");
		expect(readme).not.toContain("`/ultra history`");
		expect(readme).not.toContain("`/ultra runs`");
	});

	it("exposes invocation-critical semantics through the tool schema", () => {
		const toolGuidance = JSON.stringify(WorkflowSpecSchema);
		for (const rule of [
			"exact key in top-level schemas",
			"{PHASE.results[].FIELD[]}",
			"single brace-wrapped selector",
			"Every listed tool must be available",
			"Loaded extensions may activate additional tools",
			"read, grep, find, ls, and bash",
			"forbid mutation",
		]) {
			expect(toolGuidance).toContain(rule);
		}
	});

	it("keeps U008 runtime, orchestrator guidance, and builder contract synchronized", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");
		const builder = fs.readFileSync(BUILDER_PROMPT_PATH, "utf8");
		const schema = JSON.stringify(WorkflowSpecSchema);

		for (const tool of DYNAMIC_EXTENSION_TOOL_NAMES) {
			expect(prompt).toContain(tool);
			expect(reference).toContain(tool);
		}
		expect(schema).toContain("dynamicExtensions");
		expect(schema).toContain("writeIsolation");
		expect(prompt).toContain("Builder failure blocks later phases");
		expect(reference).toContain("u008_builder_failure_blocker");
		expect(builder).toContain(
			`dynamic-extension builder contract v${VALIDATOR_CONTRACT_VERSION}`,
		);
		for (const field of [
			"topic",
			"outcome",
			"intended consumers",
			"done conditions",
		])
			expect(prompt).toContain(field);
	});

	it("requires authored progress summaries beside exact prompts", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");
		const schema = JSON.stringify(WorkflowSpecSchema);

		expect(prompt).toContain("every step has a short `summary`");
		expect(reference).toContain("exact resolved `prompt`");
		expect(schema).toContain("progress-UI sentence");
	});

	it("keeps the exact interpolation grammar synchronized", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		expect(prompt).toContain("{EXPR | where FIELD}");
		expect(prompt).not.toContain("{… | where FIELD}");
	});

	it("keeps phase identifier guidance synchronized", () => {
		expect(JSON.stringify(WorkflowSpecSchema)).toContain(
			"reserved identifier args",
		);
		for (const content of [
			fs.readFileSync(PROMPT_PATH, "utf8"),
			fs.readFileSync(SPEC_FORMAT_PATH, "utf8"),
		]) {
			expect(content).toContain("reserved phase ID `args`");
		}
	});

	it("requires deliberate per-step model tiers and thinking levels", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");

		expect(prompt).toContain("Assign compute deliberately");
		expect(prompt).toContain("Explicitly set both fields");
		for (const tier of ["`small`", "`medium`", "`big`"]) {
			expect(prompt).toContain(tier);
			expect(reference).toContain(tier);
		}
		expect(reference).toContain("relative to that step's difficulty");
		expect(reference).toContain("re-evaluate each assignment");
	});

	it("keeps max thinking guidance synchronized", () => {
		expect(JSON.stringify(WorkflowSpecSchema)).toContain('"const":"max"');
		for (const content of [
			fs.readFileSync(PROMPT_PATH, "utf8"),
			fs.readFileSync(SPEC_FORMAT_PATH, "utf8"),
			fs.readFileSync(README_PATH, "utf8"),
		]) {
			expect(content).toContain("`max`");
		}
	});

	it("matches runtime positional and structured-output semantics", () => {
		const prompt = fs.readFileSync(PROMPT_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");

		expect(reference).toContain(
			"last declared parameter absorbs all remaining tokens",
		);
		expect(reference).not.toContain("extra tokens ignored");
		expect(reference).toContain("Every step returns a JSON object");
		expect(reference).toContain("`schema` validates");
		expect(prompt).toContain(
			"how tightly its structured JSON output must be constrained by a schema",
		);
	});

	it("keeps the reference doc and validator in lockstep: every complete JSON example parses", () => {
		const md = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");
		const blocks = [...md.matchAll(/```json\n([\s\S]*?)```/g)].map((m) => m[1]);
		expect(blocks.length).toBeGreaterThan(0);

		let completeSpecs = 0;
		for (const block of blocks) {
			let parsed: unknown;
			expect(() => {
				parsed = JSON.parse(block);
			}).not.toThrow();

			const isCompleteSpec =
				typeof parsed === "object" &&
				parsed !== null &&
				!Array.isArray(parsed) &&
				"name" in parsed &&
				"phases" in parsed;
			if (isCompleteSpec) {
				completeSpecs += 1;
				const spec = parseWorkflow(parsed);
				for (const phase of spec.phases) {
					expect(phase.step.model).toMatch(/^(small|medium|big)$/);
					expect(phase.step.thinkingLevel).toMatch(
						/^(off|minimal|low|medium|high|xhigh|max)$/,
					);
				}
			}
		}
		expect(completeSpecs).toBeGreaterThanOrEqual(1);
	});
});