fix: enforce one Pi runtime per user
This commit is contained in:
@@ -181,15 +181,25 @@ test("POST /sessions usa i settings (workspace/provider/model/thinking) e crea+a
|
||||
unlinkSync(modelKey);
|
||||
});
|
||||
|
||||
test("POST /sessions reclaims a finalized runtime before enforcing the Pi limit", async () => {
|
||||
test("POST /sessions stops the user's previous open Pi runtime before creating another", async () => {
|
||||
const runtimes = new Map<string, any>();
|
||||
const statuses = new Map<string, string>();
|
||||
const tornDown: string[] = [];
|
||||
const order: string[] = [];
|
||||
let nextId = 0;
|
||||
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
mgr: {
|
||||
get: (id: string) => runtimes.get(id),
|
||||
teardownForPrincipal: () => {
|
||||
const stopped: string[] = [];
|
||||
for (const [id, runtime] of [...runtimes]) {
|
||||
runtimes.delete(id);
|
||||
stopped.push(id);
|
||||
tornDown.push(id);
|
||||
order.push(`teardown:${id}`);
|
||||
runtime.bridge.emitClientEvent?.({ type: "system_event", event: "agent_end" });
|
||||
}
|
||||
return stopped;
|
||||
},
|
||||
createFor: (id: string) => {
|
||||
order.push(`create:${id}`);
|
||||
if (runtimes.size >= 1) throw new Error("max Pi processes reached");
|
||||
@@ -211,13 +221,8 @@ test("POST /sessions reclaims a finalized runtime before enforcing the Pi limit"
|
||||
sessionNew: async () => {
|
||||
const id = `s${++nextId}`;
|
||||
order.push(`new:${id}`);
|
||||
statuses.set(id, "open");
|
||||
return { id };
|
||||
},
|
||||
sessionShow: async (id: string) => {
|
||||
order.push(`show:${id}`);
|
||||
return { status: statuses.get(id) };
|
||||
},
|
||||
searchPack: async () => {},
|
||||
failSession: async () => {},
|
||||
} as any,
|
||||
@@ -227,7 +232,6 @@ test("POST /sessions reclaims a finalized runtime before enforcing the Pi limit"
|
||||
|
||||
expect((await app.inject({ method: "POST", url: "/sessions", payload: { question: "one" } })).statusCode)
|
||||
.toBe(200);
|
||||
statuses.set("s1", "finalized");
|
||||
order.length = 0;
|
||||
|
||||
const second = await app.inject({ method: "POST", url: "/sessions", payload: { question: "two" } });
|
||||
@@ -235,7 +239,7 @@ test("POST /sessions reclaims a finalized runtime before enforcing the Pi limit"
|
||||
expect(second.statusCode).toBe(200);
|
||||
expect(second.json()).toEqual({ id: "s2" });
|
||||
expect(tornDown).toEqual(["s1"]);
|
||||
expect(order).toEqual(["show:s1", "teardown:s1", "new:s2", "create:s2"]);
|
||||
expect(order).toEqual(["teardown:s1", "new:s2", "create:s2"]);
|
||||
expect(runtimes.has("s2")).toBe(true);
|
||||
});
|
||||
|
||||
@@ -401,6 +405,52 @@ test("POST /sessions/:id/resume usa il thinking globale se manca nel manifest",
|
||||
expect(configured.thinking).toBe("low");
|
||||
});
|
||||
|
||||
test("resuming a different session stops the user's previous Pi runtime", async () => {
|
||||
const runtimes = new Map<string, any>();
|
||||
const mgr = {
|
||||
get: (id: string) => runtimes.get(id),
|
||||
teardownForPrincipal: () => {
|
||||
const stopped = [...runtimes.keys()];
|
||||
runtimes.clear();
|
||||
return stopped;
|
||||
},
|
||||
createFor: (id: string) => {
|
||||
if (runtimes.size >= 1) throw new Error("max Pi processes reached");
|
||||
const runtime = { bridge: { onClientEvent: () => {}, turnState: () => "running" } };
|
||||
runtimes.set(id, runtime);
|
||||
return runtime;
|
||||
},
|
||||
configure: async () => {},
|
||||
start: () => {},
|
||||
teardownIfCurrent: (id: string, expected: any) => {
|
||||
if (runtimes.get(id) !== expected) return false;
|
||||
runtimes.delete(id);
|
||||
return true;
|
||||
},
|
||||
} as any;
|
||||
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
mgr,
|
||||
thtRunner: {
|
||||
sessionNew: async () => ({ id: "s1" }),
|
||||
sessionShow: async (id: string) => ({ id, status: "open", archived: false }),
|
||||
reopenSession: async () => {},
|
||||
searchPack: async () => {},
|
||||
failSession: async () => {},
|
||||
} as any,
|
||||
readiness: { ensure: async () => ({ ok: true }) } as any,
|
||||
getSettings: () => ({ workspace: "local" }) as any,
|
||||
});
|
||||
expect((await app.inject({ method: "POST", url: "/sessions", payload: { question: "one" } })).statusCode)
|
||||
.toBe(200);
|
||||
|
||||
const resumed = await app.inject({ method: "POST", url: "/sessions/s2/resume" });
|
||||
|
||||
expect(resumed.statusCode).toBe(200);
|
||||
expect(resumed.json()).toEqual({ id: "s2", alreadyActive: false });
|
||||
expect(runtimes.has("s1")).toBe(false);
|
||||
expect(runtimes.has("s2")).toBe(true);
|
||||
});
|
||||
|
||||
test.each(["running", "waiting"])(
|
||||
"POST resume preserves a %s runtime",
|
||||
async (state) => {
|
||||
|
||||
Reference in New Issue
Block a user