Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
5.1 kB · 133 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, beforeEach, describe, expect, it } from "vitest";import { ReplKernelManager } from "../src/core/kernel/index.js";
let tempDir = "";
/** * A runtime stand-in that records every `signatures` request it receives, so a test can * prove the host asked (capable runtime) or never asked (older runtime). * * A runtime that does not advertise the capability answers the request the way the real * protocol does: a ProtocolError event and a failed `done`. */function writeFakeRuntime(options: { capabilities?: string[]; digest?: string; rejectRequest?: boolean } = {}): { python: string; requested: () => { type: string; skills: string[] }[];} { const capable = options.capabilities?.includes("signatures") ?? false; const requestLog = join(tempDir, "signatures-requests"); const ready = { event: "ready", protocol: 3, python: "3.13.11", capabilities: options.capabilities ?? [] }; const digest = options.digest ?? "# skill surface (generated)\ngit.status(cwd?) -> str\n"; const okAnswer = `emit({ event: "done", id: request.id, status: "ok", calls: 1, failed: [], digest: ${JSON.stringify(digest)} });`; const protocolError = `emit({ event: "error", id: null, ename: "ProtocolError", evalue: "unknown request type: signatures", traceback: [] }); emit({ event: "done", id: request.id, status: "error", reason: "unknown request type: signatures" });`; const python = join(tempDir, "python-repl"); writeFileSync( python, `#!/usr/bin/env nodeconst fs = require("node:fs");const readline = require("node:readline");const emit = (event) => process.stdout.write(JSON.stringify(event) + "\\n");emit(${JSON.stringify(ready)});const input = readline.createInterface({ input: process.stdin });input.on("line", (line) => { const request = JSON.parse(line); if (request.type === "signatures") { fs.appendFileSync(${JSON.stringify(requestLog)}, JSON.stringify({ type: request.type, skills: request.skills }) + "\\n"); ${capable && !options.rejectRequest ? okAnswer : protocolError} return; } if (request.type === "execute") { emit({ event: "done", id: request.id, status: "ok" }); return; } if (request.type === "shutdown") { emit({ event: "done", id: request.id, status: "ok" }); process.exit(0); }});`, ); chmodSync(python, 0o755); const requested = () => { try { return readFileSync(requestLog, "utf8") .split("\n") .filter(Boolean) .map((line) => JSON.parse(line) as { type: string; skills: string[] }); } catch { return []; // No request has been recorded yet. } }; return { python, requested };}
describe("ReplKernelManager skill digest", () => { beforeEach(() => { tempDir = mkdtempSync(join(tmpdir(), "prime-agent-repl-digest-")); });
afterEach(() => { if (tempDir) { rmSync(tempDir, { recursive: true, force: true }); tempDir = ""; } });
it("asks for the digest only when the runtime advertises the capability", async () => { const { python, requested } = writeFakeRuntime({ capabilities: ["signatures"] }); const manager = new ReplKernelManager({ python, cwd: tempDir });
try { await manager.start(); await expect(manager.skillDigest(["git", "search"])).resolves.toContain("# skill surface"); expect(requested()).toEqual([{ type: "signatures", skills: ["git", "search"] }]); } finally { await manager.shutdown(); } });
it("returns null and sends nothing to a runtime without the capability", async () => { const { python, requested } = writeFakeRuntime(); const manager = new ReplKernelManager({ python, cwd: tempDir });
try { await manager.start(); await expect(manager.skillDigest(["git"])).resolves.toBeNull(); expect(requested()).toEqual([]); // An older runtime keeps serving: the gate must not have provoked a repair. await expect(manager.execute("1 + 1")).resolves.toMatchObject({ status: "ok" }); } finally { await manager.shutdown(); } });
it("returns null when the capable runtime rejects the request", async () => { const { python, requested } = writeFakeRuntime({ capabilities: ["signatures"], rejectRequest: true }); const manager = new ReplKernelManager({ python, cwd: tempDir });
try { await manager.start(); await expect(manager.skillDigest(["git"])).resolves.toBeNull(); expect(requested()).toHaveLength(1); await expect(manager.execute("1 + 1")).resolves.toMatchObject({ status: "ok" }); } finally { await manager.shutdown(); } });
it("answers null without a request for an empty skill list or a stopped kernel", async () => { const { python, requested } = writeFakeRuntime({ capabilities: ["signatures"] }); const manager = new ReplKernelManager({ python, cwd: tempDir });
await expect(manager.skillDigest(["git"])).resolves.toBeNull(); await manager.start(); await expect(manager.skillDigest([])).resolves.toBeNull(); await manager.shutdown(); await expect(manager.skillDigest(["git"])).resolves.toBeNull(); expect(requested()).toEqual([]); });});