From 3447ee55960ede33d69fd3fe5900e39016e65796 Mon Sep 17 00:00:00 2001 From: agooll <141763834+agooll@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:10:00 +0800 Subject: [PATCH 1/2] fix(agent): preserve delivery order when switching queued input Rebuild the native queues in their SDK order, keeping tracked images and appending only the moved item at the end of its new queue. Signed-off-by: agooll <141763834+agooll@users.noreply.github.com> --- .../agent/src/session/session-additions.ts | 36 +++++++++++-------- packages/agent/test/session-additions.test.ts | 31 ++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 packages/agent/test/session-additions.test.ts diff --git a/packages/agent/src/session/session-additions.ts b/packages/agent/src/session/session-additions.ts index 71c6e3aa..4a9c7776 100644 --- a/packages/agent/src/session/session-additions.ts +++ b/packages/agent/src/session/session-additions.ts @@ -88,21 +88,27 @@ export async function requeue( mode: "steer" | "followUp", ): Promise { const cleared = session.clearQueue(); - const left = { steer: [...cleared.steering], followUp: [...cleared.followUp] }; - const back: Input[] = []; - let switched = false; - for (const [key, input] of additions.queued) { - const at = left[input.mode].indexOf(input.text); - if (at < 0) continue; - left[input.mode].splice(at, 1); - if (key === id) { - input.mode = mode; - switched = true; + const tracked = new Map( + [...additions.queued].map(([key, input]) => [`${input.mode}:${input.text}`, { key, input }]), + ); + let moved: Input | undefined; + for (const [queue, texts] of [ + ["steer", cleared.steering], + ["followUp", cleared.followUp], + ] as const) { + for (const text of texts) { + const lookup = `${queue}:${text}`; + const match = tracked.get(lookup); + tracked.delete(lookup); + if (match?.key === id) { + moved = match.input; + continue; + } + await session[queue](text, match?.input.images); } - 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); - return switched; + if (!moved) return false; + await session[mode](moved.text, moved.images); + moved.mode = mode; + return true; } diff --git a/packages/agent/test/session-additions.test.ts b/packages/agent/test/session-additions.test.ts new file mode 100644 index 00000000..1fdbd226 --- /dev/null +++ b/packages/agent/test/session-additions.test.ts @@ -0,0 +1,31 @@ +import type { ImageContent } from "@earendil-works/pi-ai"; +import { expect, it, vi } from "vitest"; +import { requeue, SessionAdditions } from "../src/session/session-additions.ts"; + +it("keeps the SDK delivery order when moving one queued input", async () => { + const additions = new SessionAdditions(); + const images: ImageContent[] = [{ type: "image", data: "AA==", mimeType: "image/png" }]; + additions.queued.set("second", { text: "second", mode: "steer" }); + additions.queued.set("moved", { text: "move me", mode: "followUp", images }); + additions.queued.set("first", { text: "first", mode: "steer" }); + additions.queued.set("in-flight", { text: "already consumed", mode: "followUp" }); + const sdk = { + clearQueue: vi.fn(() => ({ + steering: ["first", "sdk input", "second"], + followUp: ["stay", "move me", "sdk follow-up"], + })), + steer: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + followUp: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + }; + + expect(await requeue(additions, sdk, "moved", "steer")).toBe(true); + expect(sdk.steer.mock.calls.map(([text]) => text)).toEqual([ + "first", + "sdk input", + "second", + "move me", + ]); + expect(sdk.steer).toHaveBeenLastCalledWith("move me", images); + expect(sdk.followUp.mock.calls.map(([text]) => text)).toEqual(["stay", "sdk follow-up"]); + expect(additions.queued.get("moved")?.mode).toBe("steer"); +}); From c5051fbd2927cf1568c903aa9efea72f85f741ad Mon Sep 17 00:00:00 2001 From: agooll <141763834+agooll@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:49:48 +0800 Subject: [PATCH 2/2] fix(agent): pair duplicate queued inputs in delivery order Consume tracked matches once per native queue slot so switching the first duplicate preserves the images of both inputs. Document destination tail insertion and cover it in the regression test. Signed-off-by: agooll <141763834+agooll@users.noreply.github.com> --- .../agent/src/session/session-additions.ts | 21 ++++++++------ packages/agent/test/session-additions.test.ts | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/packages/agent/src/session/session-additions.ts b/packages/agent/src/session/session-additions.ts index 4a9c7776..e561500f 100644 --- a/packages/agent/src/session/session-additions.ts +++ b/packages/agent/src/session/session-additions.ts @@ -77,9 +77,10 @@ type Queue = { /** * Switches queued input `id` between "add to the current task" and "after the current task". pi * 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. + * it go back, and the switched input joins the end of its destination queue. 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. */ export async function requeue( additions: SessionAdditions, @@ -88,18 +89,20 @@ export async function requeue( mode: "steer" | "followUp", ): Promise { const cleared = session.clearQueue(); - const tracked = new Map( - [...additions.queued].map(([key, input]) => [`${input.mode}:${input.text}`, { key, input }]), - ); + const tracked = new Map(); + for (const [key, input] of additions.queued) { + const lookup = `${input.mode}:${input.text}`; + const matches = tracked.get(lookup); + if (matches) matches.push({ key, input }); + else tracked.set(lookup, [{ key, input }]); + } let moved: Input | undefined; for (const [queue, texts] of [ ["steer", cleared.steering], ["followUp", cleared.followUp], ] as const) { for (const text of texts) { - const lookup = `${queue}:${text}`; - const match = tracked.get(lookup); - tracked.delete(lookup); + const match = tracked.get(`${queue}:${text}`)?.shift(); if (match?.key === id) { moved = match.input; continue; diff --git a/packages/agent/test/session-additions.test.ts b/packages/agent/test/session-additions.test.ts index 1fdbd226..72193b64 100644 --- a/packages/agent/test/session-additions.test.ts +++ b/packages/agent/test/session-additions.test.ts @@ -29,3 +29,32 @@ it("keeps the SDK delivery order when moving one queued input", async () => { expect(sdk.followUp.mock.calls.map(([text]) => text)).toEqual(["stay", "sdk follow-up"]); expect(additions.queued.get("moved")?.mode).toBe("steer"); }); + +it("matches duplicate queued text in order and keeps each input's images", async () => { + const additions = new SessionAdditions(); + const firstImages: ImageContent[] = [{ type: "image", data: "AA==", mimeType: "image/png" }]; + const secondImages: ImageContent[] = [{ type: "image", data: "AQ==", mimeType: "image/png" }]; + additions.queued.set("first", { text: "continue", mode: "steer", images: firstImages }); + additions.queued.set("second", { text: "continue", mode: "steer", images: secondImages }); + const sdk = { + clearQueue: vi.fn(() => ({ + steering: ["continue", "other", "continue", "continue"], + followUp: ["continue"], + })), + steer: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + followUp: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + }; + + expect(await requeue(additions, sdk, "first", "followUp")).toBe(true); + expect(sdk.steer.mock.calls).toEqual([ + ["other", undefined], + ["continue", secondImages], + ["continue", undefined], + ]); + expect(sdk.followUp.mock.calls).toEqual([ + ["continue", undefined], + ["continue", firstImages], + ]); + expect(additions.queued.get("first")?.mode).toBe("followUp"); + expect(additions.queued.get("second")?.mode).toBe("steer"); +});