feat: harden runtime readiness and session workflow

This commit is contained in:
User
2026-07-14 10:27:25 +02:00
parent 6d7738d538
commit 6dbf93fff9
52 changed files with 1464 additions and 169 deletions
+27 -10
View File
@@ -36,27 +36,30 @@ test("l'exit del child rimuove il runtime dalla mappa (exit handler)", async ()
expect(mgr.get("exit-test")).toBeUndefined();
});
test("spawnFor sullo STESSO id uccide il vecchio child e sostituisce il runtime (count resta 1)", async () => {
test("spawnFor sullo STESSO id rifiuta il duplicato senza interrompere il runtime attivo", async () => {
const cfg = loadConfig({ THT_HARNESS_DIR: "../harness" });
const mgr = new PiProcessManager(cfg, { spawnFn: () => spawn("node", [FAKE, SCRIPT]) as any });
const first = await mgr.spawnFor("dup-id", {});
expect(mgr.count()).toBe(1);
const firstExited = new Promise<void>((res) => first.child.on("exit", () => res()));
const second = await mgr.spawnFor("dup-id", {});
await firstExited; // the old child was killed by the idempotent respawn
await expect(mgr.spawnFor("dup-id", {})).rejects.toThrow(
"session runtime already active: dup-id",
);
expect(mgr.count()).toBe(1);
expect(mgr.get("dup-id")).toBe(second);
expect(second).not.toBe(first);
expect(mgr.get("dup-id")).toBe(first);
mgr.teardown("dup-id");
});
test("l'exit del VECCHIO child non elimina il nuovo runtime (exit identity-checked)", async () => {
const cfg = loadConfig({ THT_HARNESS_DIR: "../harness" });
const mgr = new PiProcessManager(cfg, { spawnFn: () => spawn("node", [FAKE, SCRIPT]) as any });
const first = await mgr.spawnFor("respawn-id", {});
const second = await mgr.spawnFor("respawn-id", {});
const firstChild = recordingChild();
const secondChild = recordingChild();
const children = [firstChild, secondChild];
const mgr = new PiProcessManager(cfg, { spawnFn: () => children.shift() as any });
const first = mgr.createFor("respawn-id", {});
mgr.teardown("respawn-id");
const second = mgr.createFor("respawn-id", {});
// The old child's exit handler fires after the respawn; it must NOT evict `second`.
await new Promise((res) => setImmediate(res));
firstChild.emit("exit", 0);
expect(mgr.get("respawn-id")).toBe(second);
expect(mgr.count()).toBe(1);
void first;
@@ -91,6 +94,7 @@ test("un exit INATTESO del child notifica il client (info error + agent_end)", a
child.emit("exit", 137);
expect(seen).toEqual([
{ type: "info", level: "error", text: expect.stringContaining("137") },
{ type: "system_event", event: "session_failed" },
{ type: "system_event", event: "agent_end" },
]);
expect(mgr.count()).toBe(0);
@@ -138,6 +142,19 @@ test("spawnFor new mode forwards the real question instead of kickoff", async ()
mgr.teardown("sid-question");
});
test("createFor does not prompt until start is called", async () => {
const cfg = loadConfig({});
const child = recordingChild();
const mgr = new PiProcessManager(cfg, { spawnFn: () => child as any });
const rt = mgr.createFor("sid-deferred", { question: "q" });
expect(child._writes).toEqual([]);
await mgr.configure(rt, {});
expect(child._writes).toEqual([]);
mgr.start("sid-deferred", rt, { question: "q" });
expect(JSON.parse(child._writes.at(-1)!).message).toBe('/nuova-domanda "q"');
mgr.teardown("sid-deferred");
});
test("production spawn uses explicit Pi path and passes portable data root without rewriting PATH", async () => {
vi.stubEnv("PATH", "/usr/local/bin:/usr/bin");
vi.stubEnv("PI_PROVIDER_API_KEY", "provider-secret");