test(server): update stale coalescing and worker title tests

This commit is contained in:
Mohamed Boudra
2026-05-01 19:18:16 +07:00
parent aa547cf66b
commit 68d32eb183
2 changed files with 28 additions and 22 deletions
@@ -5,6 +5,7 @@ import { afterEach, describe, expect, test, vi } from "vitest";
import { createTestLogger } from "../../test-utils/test-logger.js"; import { createTestLogger } from "../../test-utils/test-logger.js";
import { AgentManager, type AgentManagerEvent } from "./agent-manager.js"; import { AgentManager, type AgentManagerEvent } from "./agent-manager.js";
import { AGENT_STREAM_COALESCE_DEFAULT_WINDOW_MS } from "./agent-stream-coalescer.js";
import type { AgentTimelineRow } from "./agent-timeline-store-types.js"; import type { AgentTimelineRow } from "./agent-timeline-store-types.js";
import type { import type {
AgentCapabilityFlags, AgentCapabilityFlags,
@@ -27,6 +28,9 @@ import type {
* Assistant/reasoning chunks coalesce before recordTimeline() assigns canonical seqs. * Assistant/reasoning chunks coalesce before recordTimeline() assigns canonical seqs.
*/ */
const COALESCE_WINDOW_MS = AGENT_STREAM_COALESCE_DEFAULT_WINDOW_MS;
const BEFORE_COALESCE_WINDOW_MS = Math.max(COALESCE_WINDOW_MS - 1, 0);
const TEST_CAPABILITIES: AgentCapabilityFlags = { const TEST_CAPABILITIES: AgentCapabilityFlags = {
supportsStreaming: false, supportsStreaming: false,
supportsSessionPersistence: false, supportsSessionPersistence: false,
@@ -369,7 +373,7 @@ afterEach(() => {
}); });
describe("target coalesced behavior", () => { describe("target coalesced behavior", () => {
test("coalesces a same-tick assistant burst after the 200ms window", async () => { test(`coalesces a same-tick assistant burst after the ${COALESCE_WINDOW_MS}ms window`, async () => {
vi.useFakeTimers(); vi.useFakeTimers();
const harness = createHarness(); const harness = createHarness();
try { try {
@@ -380,7 +384,7 @@ describe("target coalesced behavior", () => {
} }
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(199); await vi.advanceTimersByTimeAsync(BEFORE_COALESCE_WINDOW_MS);
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0); expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0);
expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0); expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0);
@@ -408,7 +412,7 @@ describe("target coalesced behavior", () => {
} }
}); });
test("coalesces same-tick reasoning chunks after the 200ms window", async () => { test(`coalesces same-tick reasoning chunks after the ${COALESCE_WINDOW_MS}ms window`, async () => {
vi.useFakeTimers(); vi.useFakeTimers();
const harness = createHarness(); const harness = createHarness();
try { try {
@@ -419,7 +423,7 @@ describe("target coalesced behavior", () => {
} }
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -448,7 +452,7 @@ describe("target coalesced behavior", () => {
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0); expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0);
expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0); expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0);
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
expect(getTimelineItems(await harness.manager.getTimelineRows(agentId))).toEqual([ expect(getTimelineItems(await harness.manager.getTimelineRows(agentId))).toEqual([
{ type: "assistant_message", text: "a".repeat(500) }, { type: "assistant_message", text: "a".repeat(500) },
runningToolCall, runningToolCall,
@@ -459,7 +463,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(assistant("b")); session.pushEvent(assistant("b"));
} }
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(199); await vi.advanceTimersByTimeAsync(BEFORE_COALESCE_WINDOW_MS);
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(2); expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(2);
await vi.advanceTimersByTimeAsync(1); await vi.advanceTimersByTimeAsync(1);
@@ -489,7 +493,7 @@ describe("target coalesced behavior", () => {
} }
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(199); await vi.advanceTimersByTimeAsync(BEFORE_COALESCE_WINDOW_MS);
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0); expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(0);
expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0); expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(0);
@@ -517,7 +521,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(timelineEvent(toolCall({ callId: "tool-1", output: "one-b" }))); session.pushEvent(timelineEvent(toolCall({ callId: "tool-1", output: "one-b" })));
session.pushEvent(timelineEvent(toolCall({ callId: "tool-2", output: "two-b" }))); session.pushEvent(timelineEvent(toolCall({ callId: "tool-2", output: "two-b" })));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -559,7 +563,7 @@ describe("target coalesced behavior", () => {
expectContiguousRowSeqs(rows, [1, 2]); expectContiguousRowSeqs(rows, [1, 2]);
expectContiguousLiveSeqs(events, [1, 2]); expectContiguousLiveSeqs(events, [1, 2]);
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(2); expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(2);
expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(2); expect(getTimelineStreamEvents(harness.events, agentId)).toHaveLength(2);
} finally { } finally {
@@ -579,7 +583,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(timelineEvent(toolCall({ output: "latest" }))); session.pushEvent(timelineEvent(toolCall({ output: "latest" })));
session.pushEvent(assistant("b")); session.pushEvent(assistant("b"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -609,7 +613,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(reasoning("r2")); session.pushEvent(reasoning("r2"));
session.pushEvent(assistant("a2")); session.pushEvent(assistant("a2"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -638,7 +642,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(reasoning(`r${i}`)); session.pushEvent(reasoning(`r${i}`));
} }
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -689,7 +693,7 @@ describe("target coalesced behavior", () => {
first.session.pushEvent(assistant("a")); first.session.pushEvent(assistant("a"));
second.session.pushEvent(assistant("b")); second.session.pushEvent(assistant("b"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const firstRows = await harness.manager.getTimelineRows(first.agentId); const firstRows = await harness.manager.getTimelineRows(first.agentId);
const secondRows = await harness.manager.getTimelineRows(second.agentId); const secondRows = await harness.manager.getTimelineRows(second.agentId);
@@ -719,7 +723,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(assistant("codex", "codex")); session.pushEvent(assistant("codex", "codex"));
session.pushEvent(assistant("claude", "claude")); session.pushEvent(assistant("claude", "claude"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -752,7 +756,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(assistant("one", "codex", "turn-1")); session.pushEvent(assistant("one", "codex", "turn-1"));
session.pushEvent(assistant("two", "codex", "turn-2")); session.pushEvent(assistant("two", "codex", "turn-2"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -793,7 +797,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(assistant("\n\t")); session.pushEvent(assistant("\n\t"));
session.pushEvent(assistant("done")); session.pushEvent(assistant("done"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
const rows = await harness.manager.getTimelineRows(agentId); const rows = await harness.manager.getTimelineRows(agentId);
const events = getTimelineStreamEvents(harness.events, agentId); const events = getTimelineStreamEvents(harness.events, agentId);
@@ -851,7 +855,7 @@ describe("target coalesced behavior", () => {
: undefined, : undefined,
).toBe(undefined); ).toBe(undefined);
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
expect(await harness.manager.getTimelineRows(agentId)).toHaveLength( expect(await harness.manager.getTimelineRows(agentId)).toHaveLength(
expectedTimelineEventCount, expectedTimelineEventCount,
); );
@@ -951,7 +955,7 @@ describe("target coalesced behavior", () => {
}); });
second.session.pushEvent(assistant("new")); second.session.pushEvent(assistant("new"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
expect(getTimelineItems(await reuseHarness.manager.getTimelineRows(second.agentId))).toEqual([ expect(getTimelineItems(await reuseHarness.manager.getTimelineRows(second.agentId))).toEqual([
{ type: "assistant_message", text: "new" }, { type: "assistant_message", text: "new" },
@@ -1025,7 +1029,7 @@ describe("target coalesced behavior", () => {
session.pushEvent(assistant("A")); session.pushEvent(assistant("A"));
await waitForSessionEventQueue(); await waitForSessionEventQueue();
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
await waitForSessionEventQueue(); await waitForSessionEventQueue();
const rowsAfterFirstFlush = await reentryHarness.manager.getTimelineRows(agentId); const rowsAfterFirstFlush = await reentryHarness.manager.getTimelineRows(agentId);
@@ -1034,7 +1038,7 @@ describe("target coalesced behavior", () => {
]); ]);
expect(getTimelineStreamEvents(reentryHarness.events, agentId)).toHaveLength(1); expect(getTimelineStreamEvents(reentryHarness.events, agentId)).toHaveLength(1);
await vi.advanceTimersByTimeAsync(200); await vi.advanceTimersByTimeAsync(COALESCE_WINDOW_MS);
await waitForSessionEventQueue(); await waitForSessionEventQueue();
const rowsAfterSecondFlush = await reentryHarness.manager.getTimelineRows(agentId); const rowsAfterSecondFlush = await reentryHarness.manager.getTimelineRows(agentId);
@@ -199,8 +199,10 @@ it("refreshes cached terminal title after worker title changes", async () => {
const session = trackTerminal( const session = trackTerminal(
await manager.createTerminal({ await manager.createTerminal({
cwd, cwd,
command: "/bin/sh", ...nodeTerminalCommand(`
args: ["-lc", "printf '\\033]0;Build Output\\007'; sleep 2"], process.stdout.write("\\u001b]0;Build Output\\u0007");
setTimeout(() => {}, 2000);
`),
}), }),
); );