From fc06dbf1b47083ff7c8ffd6ac33eb4e290ef46d1 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:05:07 +1000 Subject: [PATCH] fix(browser): keep stopped navigation aligned with the visible document --- .../gui-plugins/src/browser/navigation-turn.test.ts | 13 +++++++++++++ packages/gui-plugins/src/browser/navigation-turn.ts | 3 ++- .../gui-plugins/src/browser/page-machine.test.ts | 4 ++-- packages/gui-plugins/src/browser/page-machine.ts | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/gui-plugins/src/browser/navigation-turn.test.ts b/packages/gui-plugins/src/browser/navigation-turn.test.ts index 7abe633f1a0..b5c714fb130 100644 --- a/packages/gui-plugins/src/browser/navigation-turn.test.ts +++ b/packages/gui-plugins/src/browser/navigation-turn.test.ts @@ -30,6 +30,19 @@ test("only a cross-document main-frame navigation supersedes a gated load", asyn } }) +test("interrupting an unissued restore preserves its address; stopping an agent navigation does not propose one", async () => { + for (const [method, restoring, keep, expected] of [["back", "https://saved.test", true, "https://saved.test"], ["forward", "https://saved.test", true, "https://saved.test"], ["stop", undefined, true, "https://saved.test"], ["stop", undefined, false, undefined]] as const) { + const gate = Promise.withResolvers() + const stopped: (string | undefined)[] = [] + const navigation = navigationTurn(() => gate.promise, () => {}, () => {}, address => stopped.push(address)) + const visit = navigation.begin(new AbortController().signal, keep)("https://saved.test", () => { throw Error("load was issued") }) + navigation.interrupt(method, restoring) + gate.resolve() + await visit + expect(stopped).toEqual([expected]) + } +}) + test("a gated history traversal settles the spinner when Chromium has no load to stop", async () => { const gate = Promise.withResolvers() const events: string[] = [] diff --git a/packages/gui-plugins/src/browser/navigation-turn.ts b/packages/gui-plugins/src/browser/navigation-turn.ts index d06492a6e27..623f5d6053d 100644 --- a/packages/gui-plugins/src/browser/navigation-turn.ts +++ b/packages/gui-plugins/src/browser/navigation-turn.ts @@ -32,7 +32,8 @@ export function navigationTurn( // initial CDP setup waiting for a renderer that the replacement command cannot start until setup finishes. if (method === "reload" && pending && restoring && /^https?:\/\//i.test(restoring)) return restoring - if (method === "stop" && held !== undefined && heldStop) { + // A not-yet-issued initial restore has no committed history entry to traverse. + if ((method === "stop" || (restoring && (method === "back" || method === "forward"))) && held !== undefined && heldStop) { const address = held spinning = false cancel() diff --git a/packages/gui-plugins/src/browser/page-machine.test.ts b/packages/gui-plugins/src/browser/page-machine.test.ts index 1bcc86942da..80d3e1ab729 100644 --- a/packages/gui-plugins/src/browser/page-machine.test.ts +++ b/packages/gui-plugins/src/browser/page-machine.test.ts @@ -74,7 +74,7 @@ test("Stop before issue publishes the restore address without inventing a commit const address = "https://next.test/report" const restore = createDocumentState({ url: address, title: "Report", generation: 4 }) - for (const before of [restore, replay(visit("https://previous.test/")).state]) { + for (const before of [restore, replay(visit("https://previous.test/")).state, replay(visit("file:///workspace/report.html")).state]) { const cancelled = replay([ { type: "navigation.started", isMainFrame: true, isSameDocument: false, url: address }, { type: "loading.started" }, @@ -82,7 +82,7 @@ test("Stop before issue publishes the restore address without inventing a commit ], before).state const saved = documentTab(cancelled, history) - expect(saved).toMatchObject({ id: history.id, url: address, loading: false }) + expect(saved).toMatchObject({ id: history.id, url: before.status === "loaded" ? before.url : address, loading: false }) expect(saved).not.toHaveProperty("loadError") expect(documentVisible(cancelled)).toBe(documentVisible(before)) expect(documentTab(cancelled, history, false)).toEqual(documentTab(before, history, false)) diff --git a/packages/gui-plugins/src/browser/page-machine.ts b/packages/gui-plugins/src/browser/page-machine.ts index 4ff8533e3de..073550c6a27 100644 --- a/packages/gui-plugins/src/browser/page-machine.ts +++ b/packages/gui-plugins/src/browser/page-machine.ts @@ -130,7 +130,7 @@ export function transitionDocument(state: DocumentState, event: DocumentEvent): switch (event.type) { case "navigation.held-stopped": - return settle(state, { ...state, retry: event.address, busy: false, pending: undefined }, [PUBLISH]) + return settle(state, { ...state, retry: state.status === "loaded" ? undefined : event.address, busy: false, pending: undefined }, [PUBLISH]) case "disposed": return { state: CLOSED, effects: [] } case "loading.started":