Skip to content

fix(download): preserve dispatched effects when the trigger fails - #351

Merged
iuyo5678 merged 1 commit into
Tencent:mainfrom
NianJiuZst:codex/fix-download-effect-state
Sep 27, 2026
Merged

iuyo5678 merged 1 commit into
Tencent:mainfrom
NianJiuZst:codex/fix-download-effect-state

Conversation

@NianJiuZst

@NianJiuZst NianJiuZst commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A download button can start an asynchronous export before Chrome emits a download event. If the click is cancelled or fails in that interval, captureBrowserDownload can report effect_state: none even though the page's click handler has already run. An agent using that metadata to decide whether to retry may start the export twice.

Use the existing dispatch marker when classifying both returned trigger errors and thrown failures. Fixes #348.

Reproduction and evidence

Failing base: 8cbcc490. Validated PR head: 7efe13a1.

The download-effect-state.browser.test.ts uses the production handleDownload path and an isolated Chrome instance:

  1. Install a button whose native click handler increments window.startedExports.
  2. Start the download action and abort immediately after the real mouseReleased acknowledgement.
  3. Emit no download intent or completion event, representing an export that has started but has not produced a file.
  4. Read the page counter and the returned effect metadata together.
Observation Base This PR
window.startedExports 1 1
RPC error code cancelled cancelled
data.phase trigger trigger
data.effect_state none — contradicts the page counter unknown

The hosted browser log records the corrected result:

{"startedExports":1,"result":{"code":"cancelled","message":"click aborted","data":{"effect_state":"unknown","phase":"trigger"}}}

Implementation

Update the error classification in download-capture.ts:

Available evidence when reporting failure Effect state
A download ID has been uniquely captured committed (existing meaning retained)
Native input was dispatched, or another existing intent/click signal makes an effect possible unknown
No dispatch, download intent, popup intent, or completed click is known none

The marker is set immediately before sending the mouse press. It is evidence of possible delivery, not proof that a download succeeded. The returned-RpcError branch and exception branch both consult it. Existing error codes, messages, attribution and cleanup behavior remain intact.

Validation

Local environment: macOS, Node 26.10.0, pnpm 10.17.0, Vitest 4.1.6, Chrome 153.0.8010.54.

Suite / check Result and coverage
download-effect-state.test.ts 2 tests passed; each checks pre-dispatch and post-dispatch cases, for returned errors and thrown exceptions
download-effect-state.browser.test.ts 1 test passed; real page side effect and RPC metadata checked together
file-transfer.test.ts 28 existing tests passed, including attribution and claimed-download cleanup
TypeScript compile, changed-file Biome, git diff --check Passed

Local focused total: 31 passed. The two new unit cases and the new browser case failed against the original implementation before the fix.

Use the repository's declared pnpm 10.17.0, from the repository root. The local runs used npm exec --yes --package pnpm@10.17.0 -- pnpm ... to select that version. Set BSK_CLICK_CHROME to an installed Chrome executable; without it the browser test is skipped.

pnpm install --frozen-lockfile
pnpm --filter @browser-skill/extension exec wxt prepare
BSK_CLICK_CHROME=/path/to/chrome \
  pnpm --filter @browser-skill/extension exec vitest run \
  src/tools/__tests__/download-effect-state.test.ts \
  src/tools/__tests__/download-effect-state.browser.test.ts \
  src/tools/__tests__/file-transfer.test.ts
pnpm --filter @browser-skill/extension compile

Hosted status for 7efe13a1, checked 2026-09-26 UTC: all 8 reported checks passed, including frontend validation, the new browser regression and existing browser suite, Rust, Windows, Node scripts and CodeCC.

Verification boundary

Chrome receives real native CDP input and executes the fixture's JavaScript. Tab/window metadata and download APIs are test adapters; the fixture does not create a real export file or call a business server. This proves the metadata contradiction, not an observed duplicate export in a production service. The test calls the extension handler directly, rather than exercising the installed CLI/daemon/extension transport end to end.

Trigger deadlines and cancellation cleanup are handled separately in #354. This PR only corrects the effect metadata and adds its regression coverage.

@iuyo5678

Copy link
Copy Markdown
Collaborator

LGTM

@iuyo5678
iuyo5678 merged commit 9ac5104 into Tencent:main Sep 27, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Download cancellation reports effect_state none after the export click reached the page

2 participants