Compare commits

...
Author SHA1 Message Date
Mohamed Boudra 35b0b35ae0 fix(timeline): reuse managed turn assignment for notices
Resolve notices before waiter matching with the same rule used by live persistence. Verify untagged foreground runtime notices through runAgent and idle Pi notices through timeline paging.
2026-11-18 15:42:24 +01:00
Mohamed Boudra 0f28b5672b test(timeline): keep notice paging assertions compatible with async history 2026-11-18 15:29:16 +01:00
Mohamed Boudra d777c0f7ab fix(timeline): keep replies visible across idle notifications
Resolve notification membership at the manager's live event boundary. An idle notice gets a display turn without starting a run, and in-flight notices join the open run.
2026-11-18 15:27:41 +01:00
4 changed files with 242 additions and 5 deletions
@@ -8,6 +8,10 @@ import { AgentManager, type AgentManagerEvent } from "./agent-manager.js";
import { AGENT_STREAM_COALESCE_DEFAULT_WINDOW_MS } from "./agent-stream-coalescer.js";
import { limitAgentTimelineItemContent } from "./agent-timeline-content.js";
import type { AgentTimelineRow } from "./agent-timeline-store-types.js";
import { OmpAgentClient } from "./providers/omp/agent.js";
import { FakeOmp } from "./providers/omp/test-utils/fake-omp.js";
import { PiRpcAgentClient } from "./providers/pi/agent.js";
import { FakePi } from "./providers/pi/test-utils/fake-pi.js";
import { projectTimelineRows } from "./timeline-projection.js";
import type {
AgentCapabilityFlags,
@@ -394,6 +398,235 @@ afterEach(() => {
vi.useRealTimers();
});
describe("live notification turns", () => {
test.each([
{ sequence: "while idle", agentStarted: false, emptyCycle: false },
{ sequence: "between agent_start and turn_start", agentStarted: true, emptyCycle: false },
{ sequence: "after an empty cycle", agentStarted: true, emptyCycle: true },
])(
"keeps OMP replies in separate turns with a notice $sequence",
async ({ agentStarted, emptyCycle }) => {
const runtime = new FakeOmp();
const provider = "omp";
const logger = createTestLogger();
const manager = new AgentManager({
clients: {
omp: new OmpAgentClient({
runtime,
logger,
runtimeSettings: { command: { mode: "replace", argv: [process.execPath] } },
}),
},
logger,
});
const workdir = mkdtempSync(join(tmpdir(), "omp-live-turns-"));
const events: AgentManagerEvent[] = [];
manager.subscribe((event) => events.push(event), { replayState: false });
const { id } = await manager.createAgent({ provider, cwd: workdir }, undefined, {
workspaceId: undefined,
});
try {
const session = runtime.latestSession();
const finishReply = async (text: string) => {
const completed = () =>
getStreamEvents(events, id).filter(
(event) => event.type === "agent_stream" && event.event.type === "turn_completed",
).length;
const before = completed();
session.streamAssistantText(text, text);
session.finishTurn({ role: "assistant", content: [] });
await vi.waitFor(() => {
expect(completed()).toBe(before + 1);
});
};
const notice = (message: string) => {
session.emit({ type: "extension_ui_request", id: message, method: "notify", message });
};
session.beginTurn();
await finishReply("First reply");
if (agentStarted) session.emit({ type: "agent_start" });
if (emptyCycle) session.emit({ type: "agent_end", messages: [] });
notice("Idle notice");
session.beginTurn();
session.acceptCustomMessage("<system-notice>Completion</system-notice>");
notice("Running notice");
await finishReply("Second reply");
const page = await manager.fetchTimeline(id, { wholeTurns: true, limit: 0 });
const ids = page.rows.map((row) => row.turnId);
expect(
page.rows.map(({ item }) => {
if (item.type === "assistant_message") return item.text;
if (item.type === "notification") return item.message;
return item.type;
}),
).toEqual(["First reply", "Idle notice", "Completion", "Running notice", "Second reply"]);
const [first, idle, wake] = ids;
expect(ids).toEqual([
expect.any(String),
expect.any(String),
expect.any(String),
wake,
wake,
]);
expect(new Set([first, idle, wake]).size).toBe(3);
expect(
getTimelineStreamEvents(events, id).map((event) =>
event.type === "agent_stream" && event.event.type === "timeline"
? event.event.turnId
: null,
),
).toEqual(ids);
expect(
(await manager.fetchTimeline(id, { wholeTurns: true, limit: 1 })).rows.map(
(row) => row.turnId,
),
).toEqual([wake, wake, wake]);
} finally {
await manager.closeAgent(id);
rmSync(workdir, { recursive: true, force: true });
}
},
);
test("keeps a Claude completion with the run it wakes after an independent notice", async () => {
const harness = createHarness({ provider: "claude" });
try {
const { agentId, session } = await createManagedSession(harness);
session.pushEvent(assistant("First reply", "claude", "first"));
session.pushEvent(
timelineEvent({ type: "notification", level: "info", message: "Idle" }, "claude"),
);
const completion = { ...toolCall({ status: "completed" }), name: "task_notification" };
session.pushEvent(
timelineEvent({ ...completion, callId: "stopped-task" }, "claude", "stopped"),
);
session.pushEvent(timelineEvent(completion, "claude", "wake"));
session.pushEvent({ type: "turn_started", provider: "claude", turnId: "wake" });
session.pushEvent(assistant("Second reply", "claude", "wake"));
session.pushEvent({ type: "turn_completed", provider: "claude", turnId: "wake" });
await vi.waitFor(() => {
expect(getStreamEvents(harness.events, agentId).at(-1)).toMatchObject({
event: { type: "turn_completed", turnId: "wake" },
});
});
const page = await harness.manager.fetchTimeline(agentId, { wholeTurns: true, limit: 0 });
expect(page.rows.map((row) => row.turnId)).toEqual([
"first",
expect.any(String),
"stopped",
"wake",
"wake",
]);
expect(page.rows[1]?.turnId).not.toBe("first");
expect(page.rows[1]?.turnId).not.toBe("wake");
expect(
getStreamEvents(harness.events, agentId).filter(
(event) => event.type === "agent_stream" && event.event.type === "turn_started",
),
).toEqual([
expect.objectContaining({
event: { type: "turn_started", provider: "claude", turnId: "wake" },
}),
]);
expect(
(await harness.manager.fetchTimeline(agentId, { wholeTurns: true, limit: 1 })).rows.map(
(row) => row.item,
),
).toEqual([completion, { type: "assistant_message", text: "Second reply" }]);
} finally {
harness.cleanup();
}
});
test("an OMP runtime notice during a foreground turn reaches the run caller", async () => {
const runtime = new FakeOmp();
const logger = createTestLogger();
const manager = new AgentManager({
clients: {
omp: new OmpAgentClient({
runtime,
logger,
runtimeSettings: { command: { mode: "replace", argv: [process.execPath] } },
}),
},
logger,
});
const workdir = mkdtempSync(join(tmpdir(), "omp-foreground-notice-"));
const agent = await manager.createAgent({ provider: "omp", cwd: workdir }, undefined, {
workspaceId: undefined,
});
const session = runtime.latestSession();
try {
const result = manager.runAgent(agent.id, "Work");
await vi.waitFor(() => expect(session.prompts).toHaveLength(1));
session.beginTurn();
session.emit({
type: "notice",
level: "info",
message: "Working",
});
session.streamAssistantText("Done");
session.finishTurn({ role: "assistant", content: [] });
const item = { type: "notification", level: "info", message: "Working" };
expect((await result).timeline).toContainEqual(item);
const rows = (await manager.fetchTimeline(agent.id, { wholeTurns: true, limit: 0 })).rows;
expect(rows.find((row) => row.item.type === "notification")?.turnId).toEqual(
expect.any(String),
);
expect(rows.find((row) => row.item.type === "notification")?.turnId).toEqual(
rows.find((row) => row.item.type === "assistant_message")?.turnId,
);
} finally {
await manager.closeAgent(agent.id);
rmSync(workdir, { recursive: true, force: true });
}
});
test("an idle Pi UI notice gets its own turn without starting a run", async () => {
const runtime = new FakePi();
const logger = createTestLogger();
const manager = new AgentManager({
clients: {
pi: new PiRpcAgentClient({
runtime,
logger,
runtimeSettings: { command: { mode: "replace", argv: [process.execPath] } },
}),
},
logger,
});
const workdir = mkdtempSync(join(tmpdir(), "pi-idle-notice-"));
const events: AgentManagerEvent[] = [];
manager.subscribe((event) => events.push(event), { replayState: false });
const { id } = await manager.createAgent({ provider: "pi", cwd: workdir }, undefined, {
workspaceId: undefined,
});
try {
runtime.latestSession().emit({
type: "extension_ui_request",
id: "notice",
method: "notify",
message: "Idle",
});
await vi.waitFor(() => expect(getTimelineStreamEvents(events, id)).toHaveLength(1));
const { rows } = await manager.fetchTimeline(id, { wholeTurns: true, limit: 1 });
const item = { type: "notification", level: "info", message: "Idle" };
expect(rows).toEqual([expect.objectContaining({ item, turnId: expect.any(String) })]);
expect(getStreamEvents(events, id)).toEqual([
expect.objectContaining({
event: { type: "timeline", provider: "pi", item, turnId: rows[0]?.turnId },
}),
]);
expect(manager.getAgent(id)?.lifecycle).toBe("idle");
} finally {
await manager.closeAgent(id);
rmSync(workdir, { recursive: true, force: true });
}
});
});
describe("target coalesced behavior", () => {
test("bounds tool output before persisting and streaming it", async () => {
const harness = createHarness();
@@ -606,9 +606,11 @@ function attachManagedTurnIdentity(
return turnId ? { event: { ...event, turnId }, turnId } : { event, turnId };
}
case "timeline": {
// Live provider items belong to the foreground turn that owns their dispatch.
// Provider history deliberately keeps absent IDs because it has no daemon turn identity.
const turnId = agent.activeForegroundTurnId ?? agent.activeTurnId ?? undefined;
// A notice with no open run is a display turn of its own.
const turnId =
agent.activeForegroundTurnId ??
agent.activeTurnId ??
(event.item.type === "notification" ? randomUUID() : undefined);
return turnId ? { event: { ...event, turnId }, turnId } : { event, turnId };
}
default:
@@ -4817,6 +4819,10 @@ export class AgentManager {
this.publishBackgroundWork(agent.id, items);
return;
}
// Resolve notice membership before selecting the foreground run's waiters.
if (event.type === "timeline" && event.item.type === "notification") {
event = attachManagedTurnIdentity(agent, event, false).event;
}
const turnId = getAgentStreamEventTurnId(event);
const matchingWaiters = this.runs.getMatchingWaiters(agent, turnId);
this.logger.trace(
@@ -1583,7 +1583,6 @@ export class OmpAgentSession implements AgentSession {
this.emit({
type: "timeline",
provider: this.provider,
turnId: this.currentTurnIdForEvent(),
item: {
type: "notification",
level:
@@ -2074,7 +2074,6 @@ export class PiRpcAgentSession implements AgentSession {
this.emit({
type: "timeline",
provider: this.provider,
turnId: this.currentTurnIdForEvent(),
item: {
type: "notification",
level: toNotificationLevel(event.notifyType),