diff --git a/packages/agent/src/session/session-additions.ts b/packages/agent/src/session/session-additions.ts index 71c6e3a..c3333ff 100644 --- a/packages/agent/src/session/session-additions.ts +++ b/packages/agent/src/session/session-additions.ts @@ -79,7 +79,9 @@ type Queue = { * keeps no per-item removal, so its queue is taken out and put back in order: only inputs still in * it go back; one pi already took (in flight, not yet announced) is in neither list, is not added * twice, and switching it reports false. Anything pi held that is not matched (another source, or - * text a skill command expanded) goes back to its own queue unchanged. + * text a skill command expanded) goes back to its own queue unchanged. Each queue is restored in + * the order clearQueue returned it, and the switched input joins its new queue after the inputs + * already there. */ export async function requeue( additions: SessionAdditions, @@ -88,21 +90,29 @@ export async function requeue( mode: "steer" | "followUp", ): Promise { const cleared = session.clearQueue(); - const left = { steer: [...cleared.steering], followUp: [...cleared.followUp] }; - const back: Input[] = []; + // A tracked entry keeps its native slot; an unmatched entry stays where it was; the + // switched entry joins its new queue last. + const lists = { + steer: cleared.steering.map((text) => ({ text })), + followUp: cleared.followUp.map((text) => ({ text })), + }; let switched = false; + let moved: Input | undefined; for (const [key, input] of additions.queued) { - const at = left[input.mode].indexOf(input.text); + const list = lists[input.mode]; + const at = list.findIndex((entry) => entry.text === input.text); if (at < 0) continue; - left[input.mode].splice(at, 1); if (key === id) { + list.splice(at, 1); input.mode = mode; switched = true; + moved = input; + } else { + list[at] = input; } - back.push(input); } - for (const text of left.steer) await session.steer(text); - for (const text of left.followUp) await session.followUp(text); - for (const input of back) await session[input.mode](input.text, input.images); + if (moved) lists[mode].push(moved); + for (const input of lists.steer) await session.steer(input.text, input.images); + for (const input of lists.followUp) await session.followUp(input.text, input.images); return switched; } diff --git a/packages/agent/test/session-additions.test.ts b/packages/agent/test/session-additions.test.ts new file mode 100644 index 0000000..1a4d2fb --- /dev/null +++ b/packages/agent/test/session-additions.test.ts @@ -0,0 +1,68 @@ +import type { ImageContent } from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { requeue, SessionAdditions } from "../src/session/session-additions.ts"; + +type Call = { mode: "steer" | "followUp"; text: string; images?: ImageContent[] }; + +type TrackedInput = { text: string; mode: "steer" | "followUp"; images?: ImageContent[] }; + +function fakeQueue(steering: string[], followUp: string[]) { + const calls: Call[] = []; + return { + calls, + session: { + clearQueue: () => ({ steering: [...steering], followUp: [...followUp] }), + steer: async (text: string, images?: ImageContent[]) => { + calls.push({ mode: "steer", text, ...(images ? { images } : {}) }); + }, + followUp: async (text: string, images?: ImageContent[]) => { + calls.push({ mode: "followUp", text, ...(images ? { images } : {}) }); + }, + }, + }; +} + +function tracked(entries: [string, TrackedInput][]) { + const additions = new SessionAdditions(); + for (const [id, input] of entries) additions.queued.set(id, input); + return additions; +} + +function order(calls: Call[]) { + return calls.map((call) => `${call.mode}:${call.text}`); +} + +describe("requeue", () => { + it("keeps each queue in native order and puts the switched input last", async () => { + const { calls, session } = fakeQueue(["first", "sdk-only", "second"], ["later"]); + const images = [{} as ImageContent]; + const additions = tracked([ + ["second-id", { text: "second", mode: "steer" }], + ["first-id", { text: "first", mode: "steer", images }], + ["later-id", { text: "later", mode: "followUp" }], + ]); + expect(await requeue(additions, session, "later-id", "steer")).toBe(true); + expect(order(calls)).toEqual(["steer:first", "steer:sdk-only", "steer:second", "steer:later"]); + expect(calls.find((call) => call.text === "first")?.images).toBe(images); + }); + + it("leaves a queue in native order when nothing switches", async () => { + const { calls, session } = fakeQueue(["b", "a"], []); + const additions = tracked([ + ["a-id", { text: "a", mode: "steer" }], + ["b-id", { text: "b", mode: "steer" }], + ]); + expect(await requeue(additions, session, "missing-id", "followUp")).toBe(false); + expect(order(calls)).toEqual(["steer:b", "steer:a"]); + }); + + it("drops an input pi already took and reports switching it as false", async () => { + const { calls, session } = fakeQueue(["kept"], []); + const additions = tracked([ + ["kept-id", { text: "kept", mode: "steer" }], + ["gone-id", { text: "gone", mode: "steer" }], + ]); + expect(await requeue(additions, session, "gone-id", "followUp")).toBe(false); + expect(order(calls)).toEqual(["steer:kept"]); + }); +});