Skip to content

Commit 1332579

Browse files
author
Peter Steinberger
committed
fix(ui): preserve question outcomes after pane disposal
1 parent 7557603 commit 1332579

4 files changed

Lines changed: 95 additions & 29 deletions

File tree

‎ui/src/app/question-prompt.shared-state.test.ts‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function requestQuestion(
6060

6161
function connectQuestionState(
6262
client: QuestionClient,
63-
onChange = vi.fn(),
63+
onChange: () => void = vi.fn(),
6464
expiresAtMs?: number,
6565
): QuestionState {
6666
const state = createQuestionPromptState(onChange);
@@ -278,6 +278,46 @@ describe("Gateway-client question outcome ownership", () => {
278278
expect(onPeerChange).not.toHaveBeenCalled();
279279
});
280280

281+
it.each(resolutionCases)(
282+
"publishes a committed $action to surviving peers after the submitting pane is disposed",
283+
async ({ resolve, status }) => {
284+
let finishRequest: ((result: QuestionResolveResult) => void) | undefined;
285+
const client: QuestionClient = {
286+
request: vi.fn(
287+
() =>
288+
new Promise<QuestionResolveResult>((complete) => {
289+
finishRequest = complete;
290+
}),
291+
),
292+
};
293+
const onSubmitterChange = vi.fn();
294+
const submitter = connectQuestionState(client, onSubmitterChange);
295+
const peer = connectQuestionState(client);
296+
const sidebar = connectQuestionState(client);
297+
const submission = resolve(submitter);
298+
onSubmitterChange.mockClear();
299+
disposeQuestionPromptState(submitter);
300+
const result: QuestionResolveResult =
301+
status === "answered"
302+
? { status, answers: { answers: { format: ["Compact"] } } }
303+
: { status };
304+
finishRequest?.(result);
305+
306+
await submission;
307+
308+
expect(submitter.prompts.get("question-1")?.status).toBe("pending");
309+
expect(onSubmitterChange).not.toHaveBeenCalled();
310+
for (const projection of [peer, sidebar]) {
311+
expect(projection.prompts.get("question-1")).toMatchObject({
312+
status,
313+
answeredElsewhere: status === "answered",
314+
localResolutionConfirmed: false,
315+
submitting: false,
316+
});
317+
}
318+
},
319+
);
320+
281321
it("does not deliver an old client result to a peer rebound to another Gateway", async () => {
282322
const firstClient = createQuestionClient();
283323
const submitter = connectQuestionState(firstClient);
@@ -421,22 +461,25 @@ describe("Gateway-client question outcome ownership", () => {
421461
expect(remounted.prompts.has("stale-before-remount")).toBe(false);
422462
});
423463

424-
it("does not publish a retired same-client request after its sender reconnects", async () => {
425-
let finishRequest: ((result: QuestionResolveResult) => void) | undefined;
464+
it("does not publish an old request rejected when its Gateway reconnects", async () => {
465+
let rejectRequest: ((error: Error) => void) | undefined;
426466
const client: QuestionClient = {
427467
request: vi.fn(
428468
() =>
429-
new Promise<QuestionResolveResult>((resolve) => {
430-
finishRequest = resolve;
469+
new Promise<QuestionResolveResult>((_resolve, reject) => {
470+
rejectRequest = reject;
431471
}),
432472
),
433473
};
434474
const submitter = connectQuestionState(client);
435475
const peer = connectQuestionState(client);
436476
const submission = cancelQuestionPrompt(submitter, "question-1");
437-
setQuestionPromptClient(submitter, null);
438-
setQuestionPromptClient(submitter, client);
439-
finishRequest?.({ status: "cancelled" });
477+
// The protocol rejects pending requests before its reconnect lifecycle.
478+
rejectRequest?.(new Error("gateway closed"));
479+
for (const projection of [submitter, peer]) {
480+
setQuestionPromptClient(projection, null);
481+
setQuestionPromptClient(projection, client);
482+
}
440483

441484
await submission;
442485

‎ui/src/app/question-prompt.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ describe("question resolution connection ownership", () => {
553553
);
554554

555555
it.each(questionResolutionCases)(
556-
"retires an old $action when the same gateway client reconnects",
556+
"rejects an old $action when the same gateway transport reconnects",
557557
async ({ resolve }) => {
558558
const stale = createDeferredQuestionRequest();
559559
const current = createDeferredQuestionRequest();
@@ -569,12 +569,13 @@ describe("question resolution connection ownership", () => {
569569
const { state } = createConnectedState(client, requestedPayload());
570570

571571
const staleResolution = resolve(state);
572+
// Socket closure rejects its pending requests before reconnect callbacks.
573+
stale.reject();
572574
setQuestionPromptClient(state, null);
573575
expect(state.prompts.get("question-1")?.submitting).toBe(false);
574576
setQuestionPromptClient(state, client);
575577

576578
const currentResolution = resolve(state);
577-
stale.resolve();
578579
await staleResolution;
579580

580581
expect(state.prompts.get("question-1")).toMatchObject({

‎ui/src/app/question-prompt.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,20 +697,18 @@ async function resolveQuestionPrompt(
697697
submittedAnswers ? { id, answers: submittedAnswers } : { id, cancel: true },
698698
prompt.expiresAtMs,
699699
);
700-
if (state.client !== client || state.clientGeneration !== clientGeneration) {
701-
return;
702-
}
703-
const current = state.prompts.get(id);
704-
if (!current) {
705-
return;
706-
}
707700
const resolved = parseQuestionResolveResult(result);
708701
if (!resolved || resolved.status !== (submittedAnswers ? "answered" : "cancelled")) {
709702
throw new Error("invalid question.resolve response");
710703
}
711-
current.localResolutionConfirmed = true;
704+
if (state.client === client && state.clientGeneration === clientGeneration) {
705+
const current = state.prompts.get(id);
706+
if (current) {
707+
current.localResolutionConfirmed = true;
708+
}
709+
}
712710
// The committed RPC result owns every same-client projection even when
713-
// server event fanout fails; it is not a synthetic Gateway broadcast.
711+
// fanout fails or its submitting pane unmounts before the response.
714712
publishQuestionClientResolution(client, { ...resolved, id });
715713
} catch (error) {
716714
if (state.client !== client || state.clientGeneration !== clientGeneration) {

‎ui/src/e2e/question-flow.e2e.test.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,21 @@ describeControlUiE2e("Control UI Gateway question flow", () => {
356356
});
357357

358358
it.each([
359-
{ action: "answer", status: "answered" as const },
360-
{ action: "cancellation", status: "cancelled" as const },
359+
{ action: "answer", status: "answered" as const, closeSubmittingPane: false },
360+
{ action: "cancellation", status: "cancelled" as const, closeSubmittingPane: false },
361+
{
362+
action: "answer after its submitting pane closes",
363+
status: "answered" as const,
364+
closeSubmittingPane: true,
365+
},
366+
{
367+
action: "cancellation after its submitting pane closes",
368+
status: "cancelled" as const,
369+
closeSubmittingPane: true,
370+
},
361371
])(
362-
"updates both split panes and sidebar from an authoritative $action without a resolution event",
363-
async ({ action, status }) => {
372+
"updates current panes and sidebar from an authoritative $action without a resolution event",
373+
async ({ status, closeSubmittingPane }) => {
364374
const { gateway, page } = await openQuestionPage();
365375
await page.getByRole("button", { name: "Open split view" }).click();
366376
const panes = page.locator("openclaw-chat-pane.chat-split-view__pane");
@@ -369,7 +379,7 @@ describeControlUiE2e("Control UI Gateway question flow", () => {
369379
.poll(async () => (await gateway.getRequests("question.list")).length)
370380
.toBeGreaterThanOrEqual(3);
371381

372-
const request = questionRecord(`question-split-${action}`, [
382+
const request = questionRecord(`question-split-${status}-${closeSubmittingPane}`, [
373383
{
374384
questionId: "deploy_target",
375385
header: "Deploy",
@@ -385,8 +395,14 @@ describeControlUiE2e("Control UI Gateway question flow", () => {
385395
const answers = { answers: { deploy_target: ["Staging"] } };
386396
const result: QuestionResolveResult =
387397
status === "answered" ? { status, answers } : { status };
388-
await gateway.setMethodResponse("question.resolve", result);
389-
const submittingPanel = panels.first();
398+
if (closeSubmittingPane) {
399+
await gateway.deferNext("question.resolve");
400+
} else {
401+
await gateway.setMethodResponse("question.resolve", result);
402+
}
403+
const submittingIndex = closeSubmittingPane ? 1 : 0;
404+
const submittingPane = panes.nth(submittingIndex);
405+
const submittingPanel = panels.nth(submittingIndex);
390406
if (status === "answered") {
391407
await submittingPanel.getByRole("radio", { name: /Staging/ }).click();
392408
}
@@ -398,19 +414,27 @@ describeControlUiE2e("Control UI Gateway question flow", () => {
398414
status === "answered" ? { id: request.id, answers } : { id: request.id, cancel: true },
399415
);
400416
expect(await gateway.getRequests("question.resolve")).toHaveLength(1);
417+
const remainingPanes = page.locator("openclaw-chat-pane");
418+
if (closeSubmittingPane) {
419+
await submittingPane.getByRole("button", { name: "Close pane", exact: true }).click();
420+
await expect.poll(() => remainingPanes.count()).toBe(1);
421+
await expectQuestionAttention(page, true);
422+
await gateway.resolveDeferred("question.resolve", result);
423+
}
424+
const remainingCount = closeSubmittingPane ? 1 : 2;
401425

402426
await expect.poll(() => panels.count()).toBe(0);
403427
await expect
404-
.poll(() => panes.locator(".agent-chat__composer-combobox textarea").count())
405-
.toBe(2);
428+
.poll(() => remainingPanes.locator(".agent-chat__composer-combobox textarea").count())
429+
.toBe(remainingCount);
406430
await expect
407431
.poll(() =>
408432
page
409433
.locator(".chat-question-summary")
410434
.filter({ hasText: status === "answered" ? "Staging" : "Skipped" })
411435
.count(),
412436
)
413-
.toBe(2);
437+
.toBe(remainingCount);
414438
await expectQuestionAttention(page, false);
415439
},
416440
);

0 commit comments

Comments
 (0)