Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions hooks/useAgentSession.rpc.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,3 +2053,21 @@ test("unmount before replacement open cannot restore stale wrapper registrations
await act(async () => { lateOpen({}); });
assert.equal(world.calls.slice(before).some((c) => c.method === "POST"), false);
});

test("the answering tab keeps its dialog mounted during a synchronized cancel hand-off", async () => {
resetWorld();
primeSession("s1", [userMsg("u0", "q")]);
const { w, es } = await startStreamingRun("s1");
await act(async () => {
es.emit({ type: "extension_ui_request", id: "question", method: "confirm", title: "Keep going?" });
});
assert.equal(w.latest.extensionDialog?.id, "question");
await act(async () => {
const response = w.latest.respondToExtensionUi(w.latest.extensionDialog, { confirmed: true });
es.emit({ type: "extension_ui_request", id: "cancel:question", method: "cancel", targetId: "question" });
await response;
});
assert.equal(w.latest.extensionDialog?.id, "question", "the local hand-off window must survive its own synchronized cancel");
await settle(300);
assert.equal(w.latest.extensionDialog, null);
});
8 changes: 7 additions & 1 deletion hooks/useAgentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
const [sessionStatsOverride, setSessionStatsOverride] = useState<SessionStatsInfo | null>(null);
const [extensionDialog, setExtensionDialog] = useState<ExtensionUiDialogRequest | null>(null);
const extensionDialogClearTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const locallyAnsweredDialogRef = useRef<string | null>(null);
useEffect(() => () => {
if (extensionDialogClearTimerRef.current) clearTimeout(extensionDialogClearTimerRef.current);
}, []);
Expand Down Expand Up @@ -1116,6 +1117,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
setExtensionDialog((current) => current?.id === request.id ? null : current);
return;
}
locallyAnsweredDialogRef.current = request.id;
try {
await sendAgentCommand(sid, {
type: "extension_ui_response",
Expand All @@ -1131,6 +1133,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
if (extensionDialogClearTimerRef.current) clearTimeout(extensionDialogClearTimerRef.current);
extensionDialogClearTimerRef.current = setTimeout(() => {
setExtensionDialog((current) => current?.id === request.id ? null : current);
if (locallyAnsweredDialogRef.current === request.id) locallyAnsweredDialogRef.current = null;
extensionDialogClearTimerRef.current = null;
}, 250);
}
Expand Down Expand Up @@ -1344,10 +1347,13 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
clearTimeout(extensionDialogClearTimerRef.current);
extensionDialogClearTimerRef.current = null;
}
locallyAnsweredDialogRef.current = null;
setExtensionDialog(request);
break;
case "cancel":
setExtensionDialog((current) => current?.id === request.targetId ? null : current);
if (request.targetId !== locallyAnsweredDialogRef.current) {
setExtensionDialog((current) => current?.id === request.targetId ? null : current);
}
break;
case "open_url": {
// OAuth and similar flows: try to open a tab (often blocked outside a
Expand Down
15 changes: 15 additions & 0 deletions lib/rpc-manager.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,3 +1118,18 @@ test("ask dialogs stay pending until a well-formed answers payload is forwarded"
await wrapper.send({ type: "extension_ui_response", id: "ask2", cancelled: true });
assert.deepEqual(sentFrames.at(-1), { type: "extension_ui_response", id: "ask2", cancelled: true });
});

test("answering a dialog in one tab dismisses it in every other attached tab", async (t) => {
const { wrapper, emit } = snapshotSession(t);
const other = [];
wrapper.onEvent((event) => other.push(event));
emit({ type: "extension_ui_request", id: "ask", method: "confirm", title: "Keep going?" });
other.length = 0;
await wrapper.send({ type: "extension_ui_response", id: "ask", confirmed: true });
assert.deepEqual(other.map(({ type, method, targetId }) => ({ type, method, targetId })), [
{ type: "extension_ui_request", method: "cancel", targetId: "ask" },
]);
other.length = 0;
await wrapper.send({ type: "extension_ui_response", id: "ask", confirmed: true });
assert.deepEqual(other, [], "an already-settled dialog emits nothing");
});
4 changes: 4 additions & 0 deletions lib/rpc-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,12 @@ export class AgentSessionWrapper {
if (("answers" in rest || pendingAsk) && !isAskAnswers(rest.answers) && !(pendingAsk && rest.cancelled === true)) {
throw new WebRpcError("Invalid ask dialog answers", "invalid_ask_answers");
}
const wasPending = this.pendingUiRequests.has(id);
this.forgetPendingUiRequest(id);
this.proc.sendFrame({ type: "extension_ui_response", id, ...rest });
// omp sends no cancel for an answered dialog; other tabs on this
// session would keep showing it, so settle it for them here.
if (wasPending) this.emit({ type: "extension_ui_request", id: `cancel:${id}`, method: "cancel", targetId: id });
return null;
}

Expand Down
Loading