Compare commits

...

1 Commits

Author SHA1 Message Date
Saoud Rizwan 7ffee5636c fix(cli): scope interactive agent events to active session 2026-06-06 20:02:39 -07:00
2 changed files with 29 additions and 3 deletions
@@ -16,11 +16,15 @@ const {
mockCreateRuntimeHooks,
mockLoadInteractiveResumeMessages,
mockSetActiveCliSession,
mockSubscribeToAgentEvents,
mockSubscribeToPendingPromptEvents,
} = vi.hoisted(() => ({
mockCreateCliCore: vi.fn(),
mockCreateRuntimeHooks: vi.fn(),
mockLoadInteractiveResumeMessages: vi.fn(),
mockSetActiveCliSession: vi.fn(),
mockSubscribeToAgentEvents: vi.fn(() => vi.fn()),
mockSubscribeToPendingPromptEvents: vi.fn(() => vi.fn()),
}));
vi.mock("../../session/session", () => ({
@@ -48,8 +52,8 @@ vi.mock("../active-runtime", () => ({
}));
vi.mock("../session-events", () => ({
subscribeToAgentEvents: vi.fn(() => vi.fn()),
subscribeToPendingPromptEvents: vi.fn(() => vi.fn()),
subscribeToAgentEvents: mockSubscribeToAgentEvents,
subscribeToPendingPromptEvents: mockSubscribeToPendingPromptEvents,
}));
import { createInteractiveSessionRuntime } from "./session-runtime";
@@ -205,6 +209,19 @@ describe("createInteractiveSessionRuntime", () => {
expect(runtime.getActiveSessionId()).toBe("session-2");
});
it("subscribes agent events to the active interactive session only", async () => {
const manager = makeManager();
const runtime = makeRuntime(manager);
await runtime.ensureReady();
expect(mockSubscribeToAgentEvents).toHaveBeenCalledWith(
manager,
expect.any(Function),
{ sessionId: "session-1" },
);
});
it("starts fresh after resetting an initially resumed session", async () => {
const manager = makeManager();
const runtime = makeRuntime(manager, {
@@ -83,11 +83,21 @@ export function createInteractiveSessionRuntime(input: {
let pendingResumeSessionId = input.resumeSessionId?.trim() || undefined;
const clearActiveSession = (): void => {
unsubscribeAgent();
unsubscribeAgent = () => {};
activeSessionId = "";
setActiveCliSession(undefined);
};
const applyStartedSession = (started: StartedSession): void => {
if (sessionManager) {
unsubscribeAgent();
unsubscribeAgent = subscribeToAgentEvents(
sessionManager,
input.onAgentEvent,
{ sessionId: started.sessionId },
);
}
setActiveCliSession({
manifest: started.manifest,
});
@@ -140,7 +150,6 @@ export function createInteractiveSessionRuntime(input: {
await manager.ingestHookEvent(payload);
},
});
unsubscribeAgent = subscribeToAgentEvents(manager, input.onAgentEvent);
unsubscribePendingPrompts = subscribeToPendingPromptEvents(manager, {
onPendingPrompts: input.onPendingPrompts,
onPendingPromptSubmitted: input.onPendingPromptSubmitted,