dbd09cf4ee
- Added an agent loop test proving aside messages are delivered after tool results, before the next model request, without interrupting tool execution. - Added a yield-queue test confirming stale entries are excluded, the queue is cleared, and re-draining yields no messages.
174 lines
5.3 KiB
TypeScript
174 lines
5.3 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
|
|
import { YieldQueue } from "@oh-my-pi/pi-coding-agent/session/yield-queue";
|
|
|
|
type Entry = {
|
|
id: string;
|
|
stale?: boolean;
|
|
};
|
|
|
|
function userMessage(text: string): AgentMessage {
|
|
return {
|
|
role: "user",
|
|
content: [{ type: "text", text }],
|
|
timestamp: 0,
|
|
};
|
|
}
|
|
|
|
function messageText(message: AgentMessage): string {
|
|
if (!("content" in message) || !Array.isArray(message.content)) return "";
|
|
const block = message.content[0];
|
|
return block?.type === "text" ? block.text : "";
|
|
}
|
|
|
|
function createHarness(initialStreaming: boolean) {
|
|
let streaming = initialStreaming;
|
|
const streamingMessages: AgentMessage[] = [];
|
|
const idleBatches: AgentMessage[][] = [];
|
|
const scheduledFlushes: Array<() => Promise<void>> = [];
|
|
const queue = new YieldQueue({
|
|
isStreaming: () => streaming,
|
|
injectStreaming: message => {
|
|
streamingMessages.push(message);
|
|
},
|
|
injectIdle: async messages => {
|
|
idleBatches.push(messages);
|
|
},
|
|
scheduleIdleFlush: run => {
|
|
scheduledFlushes.push(run);
|
|
},
|
|
});
|
|
return {
|
|
queue,
|
|
streamingMessages,
|
|
idleBatches,
|
|
scheduledFlushes,
|
|
setStreaming: (value: boolean) => {
|
|
streaming = value;
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("YieldQueue", () => {
|
|
test("enqueue while streaming defers until streaming flush", async () => {
|
|
const harness = createHarness(true);
|
|
harness.queue.register<Entry>("items", {
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
|
|
harness.queue.enqueue("items", { id: "a" });
|
|
|
|
expect(harness.scheduledFlushes).toHaveLength(0);
|
|
expect(harness.streamingMessages).toHaveLength(0);
|
|
expect(harness.queue.has("items")).toBe(true);
|
|
|
|
await harness.queue.flush("streaming");
|
|
|
|
expect(harness.queue.has()).toBe(false);
|
|
expect(harness.streamingMessages.map(messageText)).toEqual(["a"]);
|
|
});
|
|
|
|
test("enqueue while idle schedules one debounced idle flush", async () => {
|
|
const harness = createHarness(false);
|
|
harness.queue.register<Entry>("items", {
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
|
|
harness.queue.enqueue("items", { id: "a" });
|
|
harness.queue.enqueue("items", { id: "b" });
|
|
|
|
expect(harness.scheduledFlushes).toHaveLength(1);
|
|
expect(harness.idleBatches).toHaveLength(0);
|
|
|
|
await harness.scheduledFlushes[0]!();
|
|
|
|
expect(harness.idleBatches).toHaveLength(1);
|
|
expect(harness.idleBatches[0]?.map(messageText)).toEqual(["a,b"]);
|
|
});
|
|
|
|
test("isStale drops stale entries and keeps survivors", async () => {
|
|
const harness = createHarness(true);
|
|
let survivorIds: string[] = [];
|
|
harness.queue.register<Entry>("items", {
|
|
isStale: entry => entry.stale === true,
|
|
build: entries => {
|
|
survivorIds = entries.map(entry => entry.id);
|
|
return userMessage(survivorIds.join(","));
|
|
},
|
|
});
|
|
|
|
harness.queue.enqueue("items", { id: "old", stale: true });
|
|
harness.queue.enqueue("items", { id: "fresh" });
|
|
await harness.queue.flush("streaming");
|
|
|
|
expect(survivorIds).toEqual(["fresh"]);
|
|
expect(harness.streamingMessages.map(messageText)).toEqual(["fresh"]);
|
|
});
|
|
|
|
test("build returning null does not inject", async () => {
|
|
const harness = createHarness(true);
|
|
harness.queue.register<Entry>("items", {
|
|
build: () => null,
|
|
});
|
|
|
|
harness.queue.enqueue("items", { id: "a" });
|
|
await harness.queue.flush("streaming");
|
|
|
|
expect(harness.streamingMessages).toHaveLength(0);
|
|
expect(harness.idleBatches).toHaveLength(0);
|
|
});
|
|
|
|
test("one kind failing in build does not abort other kinds", async () => {
|
|
const harness = createHarness(true);
|
|
harness.queue.register<Entry>("bad", {
|
|
build: () => {
|
|
throw new Error("boom");
|
|
},
|
|
});
|
|
harness.queue.register<Entry>("good", {
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
|
|
harness.queue.enqueue("bad", { id: "bad" });
|
|
harness.queue.enqueue("good", { id: "good" });
|
|
await harness.queue.flush("streaming");
|
|
|
|
expect(harness.streamingMessages.map(messageText)).toEqual(["good"]);
|
|
});
|
|
|
|
test("flush preserves registration order across kinds", async () => {
|
|
const harness = createHarness(true);
|
|
harness.queue.register<Entry>("second", {
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
harness.queue.register<Entry>("first", {
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
|
|
harness.queue.enqueue("first", { id: "first" });
|
|
harness.queue.enqueue("second", { id: "second" });
|
|
await harness.queue.flush("streaming");
|
|
|
|
expect(harness.streamingMessages.map(messageText)).toEqual(["second", "first"]);
|
|
});
|
|
|
|
test("drainMessages builds non-stale entries, clears the queue, returns nothing on re-drain", async () => {
|
|
const harness = createHarness(true);
|
|
harness.queue.register<Entry>("items", {
|
|
isStale: entry => entry.stale === true,
|
|
build: entries => userMessage(entries.map(entry => entry.id).join(",")),
|
|
});
|
|
|
|
harness.queue.enqueue("items", { id: "keep" });
|
|
harness.queue.enqueue("items", { id: "drop", stale: true });
|
|
|
|
const drained = harness.queue.drainMessages();
|
|
expect(drained.map(messageText)).toEqual(["keep"]);
|
|
// Pull-based drain has no injection side effects and empties the queue.
|
|
expect(harness.streamingMessages).toHaveLength(0);
|
|
expect(harness.idleBatches).toHaveLength(0);
|
|
expect(harness.queue.has()).toBe(false);
|
|
expect(harness.queue.drainMessages()).toEqual([]);
|
|
});
|
|
});
|