import assert from "node:assert/strict"; import { resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { randomUUID } from "node:crypto"; const harness = process.argv[2]; const loaderPath = process.env.SUBSTRATE_HARNESS_LOADER; const endpoint = process.env.SUBSTRATE_GUEST_ENDPOINT; if ((harness !== "pi" && harness !== "omp") || !loaderPath || !endpoint) throw new Error("Usage: smoke-harness.ts pi|omp with SUBSTRATE_HARNESS_LOADER pointing to that harness's real extensions/loader module and SUBSTRATE_GUEST_ENDPOINT to an isolated upstream guest."); const { loadExtensions } = await import(pathToFileURL(resolve(loaderPath)).href); // Select the installed host's loader at runtime; do not bundle either harness. const loaded = await loadExtensions([resolve(`src/${harness}.ts`)], process.cwd()); assert.deepEqual(loaded.errors, []); const extension = loaded.extensions[0]; assert.ok(extension.commands.has("substrate")); loaded.runtime.flagValues.set("substrate-endpoint", endpoint); const messages: unknown[] = []; loaded.runtime.sendMessage = (message: unknown) => messages.push(message); const sessionId = randomUUID(); let branch: unknown[] = [{ type: "custom", customType: "pi-agent-substrate.binding", data: { version: 1, sessionId, endpoint, atespace: "ate-env", environmentId: "guest-smoke", processIds: [], uncertain: false } }]; loaded.runtime.appendEntry = (customType: string, data: unknown) => branch.push({ type: "custom", customType, data }); const notices: string[] = []; const ctx = { sessionManager: { getSessionId: () => sessionId, getBranch: () => branch }, hasUI: false, ui: { notify: (message: string) => notices.push(message), setStatus() {}, confirm: async () => false } }; for (const handler of extension.handlers.get("session_start") ?? []) await handler({ type: "session_start", reason: "resume" }, ctx); assert.deepEqual(notices, []); const call = async (name: string, args: Record) => extension.tools.get(name).definition.execute(randomUUID(), args, undefined, undefined, ctx); const name = `harness-${harness}-${randomUUID()}`; await call("substrate_write", { path: name, content: "live remote file" }); assert.equal((await call("substrate_read", { path: name })).content[0].text, "live remote file"); assert.match((await call("substrate_shell", { command: "printf 'live remote shell'", timeoutSeconds: 1.0001 })).content[0].text, /live remote shell\n\[exit 0\]/); const background = await call("substrate_shell", { command: "sleep 60", background: true }); await call("substrate_process", { processId: background.details.processId, action: "kill" }); await call("substrate_shell", { command: `rm -- '${name}'` }); await extension.commands.get("substrate").handler("detach", ctx); assert.match(JSON.stringify(messages), /Detached/); await assert.rejects(call("substrate_read", { path: name }), /attach/i); for (const handler of extension.handlers.get("session_shutdown") ?? []) await handler({ type: "session_shutdown" }, ctx); console.log(JSON.stringify({ scenario: "real-harness-loader-and-live-guest-tools", harness, runtime: process.versions.bun ? `bun ${process.versions.bun}` : `node ${process.versions.node}`, toolsExercised: [...extension.tools.keys()], remoteReadWriteShellAndKill: true, detachRejectsRemoteCalls: true, modelRequests: 0, suspensionVerified: false }, null, 2));