/** Provider-backed plumbing smoke tests only; these are not agent-effectiveness evidence. */ import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { join, resolve } from "node:path"; import { tmpdir } from "node:os"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; import { finalAnswerFromEvents } from "./effectiveness.js"; export interface SmokeScenario { name: string; program: string; prompt: string; strict?: boolean; assert: (answer: string, results: any[], workspace: string) => boolean; } const readCommand = `node -e 'process.stdout.write("x".repeat(5000) + "E2E-READ-MARKER" + "x".repeat(15000))'`; const readProgram = `const raw = await pi.bash({ cmd: ${JSON.stringify(readCommand)} }); return raw;`; const summaryProgram = `const text = "E2E-SUMMARY-MARKER\\n" + "x".repeat(12000); return (await extensions.ctx_summarize({ text, mode: "model", maxTokens: 120, maxInputTokens: 2048, maxChunks: 4, maxCalls: 8 })).text;`; const policyProgram = `const raw = await pi.bash({ cmd: "printf 100000" }); const limit = Math.min(Number(raw.output), 3000); return { marker: "E2E-POLICY-ALLOW", limit };`; const blockedProgram = `return await pi.bash({ cmd: "touch blocked-sentinel" });`; export const smokeScenarios: SmokeScenario[] = [ { name: "Fabric boundary offload and recovery", program: readProgram, prompt: `Run this code in fabric_exec: ${readProgram} Then use another fabric_exec call with extensions.ctx_read and the returned handle to locate the marker in the stored payload. Return the recovered marker and state that recovery succeeded. Additional bounded reads are allowed.`, assert: (answer, results) => answer.includes("E2E-READ-MARKER") && /recover/i.test(answer) && results.some(result => result.details?.ce_offloaded === true) && results.filter(result => result.isError !== true).length >= 2, }, { name: "hierarchical bounded summary", program: summaryProgram, prompt: `Run this code in fabric_exec: ${summaryProgram} Report the returned marker, strategy, chunks, and modelCalls. Do not claim success if the helper returned an error.`, assert: (answer, results) => answer.includes("E2E-SUMMARY-MARKER") && /hierarch/i.test(answer) && results.some(result => result.isError !== true && (result.content ?? []).some((block: any) => { if (block.type !== "text") return false; try { const value = JSON.parse(block.text); return value.strategy === "hierarchical" && value.complete === true && value.chunks > 1 && value.modelCalls > 1; } catch { return false; } })), }, { name: "small numeric projection executes under runtime-first policy", program: policyProgram, prompt: `Run this code in fabric_exec: ${policyProgram} Report the returned marker and limit.`, assert: (answer, results) => answer.includes("E2E-POLICY-ALLOW") && /3000/.test(answer) && results.some(result => result.isError !== true), }, { name: "explicit strict preflight blocks before side effects", program: blockedProgram, strict: true, prompt: `This isolated workspace explicitly enables CE strict mode. Request this exact code in fabric_exec once: ${blockedProgram} Do not change the program, config, or try an alternative when it is blocked. Report that the operation was blocked and did not execute.`, assert: (answer, results, workspace) => /block/i.test(answer) && results.some(result => result.isError === true) && !existsSync(join(workspace, "blocked-sentinel")), }, ]; export function runSmokeTests(): void { if (process.env.CE_RUN_E2E !== "1") { console.log("E2E smoke tests skipped; set CE_RUN_E2E=1 and PI_MODEL explicitly to opt in."); return; } if (!process.env.PI_MODEL) throw new Error("Set PI_MODEL explicitly before opting into provider-backed smoke tests."); const extension = resolve(".tmp/pi-ce-build/index.js"); const fabricExtension = resolve("node_modules/pi-fabric/dist/index.js"); if (!existsSync(extension)) throw new Error("Build CE first: .tmp/pi-ce-build/index.js is missing."); if (!existsSync(fabricExtension)) throw new Error("Install the optional pi-fabric peer before running E2E smoke tests."); let failures = 0; for (const scenario of smokeScenarios) { const workspace = mkdtempSync(join(tmpdir(), "ce-smoke-")); try { mkdirSync(join(workspace, ".pi")); writeFileSync(join(workspace, ".pi/context-engineer.json"), JSON.stringify({ strict: scenario.strict ?? false, notifyOnStart: false })); const result = spawnSync(process.env.PI_BIN ?? "pi", [ "--model", process.env.PI_MODEL, "--no-extensions", "--no-skills", "--no-context-files", "--no-prompt-templates", "--no-themes", "--no-approve", "--print", "--mode", "json", "--no-session", "--extension", extension, "--extension", fabricExtension, "--", scenario.prompt, ], { cwd: workspace, encoding: "utf8", timeout: 180_000, maxBuffer: 8_000_000 }); const events: any[] = []; let malformed = false; for (const line of (result.stdout ?? "").split("\n").filter(line => line.trim())) { try { events.push(JSON.parse(line)); } catch { malformed = true; } } const answer = finalAnswerFromEvents(events); const toolResults = events.filter(event => event.type === "message_end" && event.message?.role === "toolResult").map(event => event.message); const ok = !result.error && result.status === 0 && !malformed && scenario.assert(answer, toolResults, workspace); console.log(`[${ok ? "ok" : "FAIL"}] ${scenario.name}`); if (!ok) { failures++; console.log(`${result.error?.message ?? ""}\n${result.stderr ?? ""}\n${answer}`.slice(-2000)); } } finally { rmSync(workspace, { recursive: true, force: true }); } } console.log(`Pi/Fabric E2E plumbing: ${smokeScenarios.length - failures} passed, ${failures} failed`); process.exitCode = failures === 0 ? 0 : 1; } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) runSmokeTests();