Skip to content

fix(ui): prevent clipboard callbacks from failing after teardown - #132803

Closed
steipete wants to merge 1 commit into
mainfrom
codex/frv-clipboard-document-lifecycle
Closed

steipete wants to merge 1 commit into
mainfrom
codex/frv-clipboard-document-lifecycle

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Resolves a problem where Control UI verification could finish every assertion successfully and then fail with an unhandled ReferenceError: document is not defined during environment teardown. This caused unnecessary reruns in release verification; this CI job passed all 4,093 assertions before failing in the clipboard callback.

Why This Change Was Made

The shared clipboard fallback now retains the scratch textarea's owning document for deferred focus restoration. Vitest's JSDOM environment can remove the global document before the native timer fires; the callback must use its original owner. The zero-delay restoration, connected-target check, newer-focus protection, and preventScroll behavior remain intact. No retries, timeout changes, caller workarounds, or swallowed exceptions were added.

User Impact

No intended visual or clipboard behavior change. Copy controls still restore focus after being re-enabled, preserve newly focused inputs, and ignore removed targets. Release verification no longer fails because this callback outlives its environment's global document.

Evidence

  • Reproduced on Node 24.20.0 with actual Vitest 4.1.11 JSDOM 29.1.1 setup, clipboard fallback, environment teardown, then native timer execution: baseline throws at clipboard.ts:50; fixed code exits successfully.

  • The added owner regression fails before the production change. The existing real-timer focus test now also covers newer focus and removed targets.

  • Final focused run covered clipboard.test.ts, the original chat-pane-task-suggestions.test.ts caller, and the three broader-suite failure files: all 53 assertions passed across five files.

  • node scripts/run-vitest.mjs run --config test/vitest/vitest.ui-e2e.config.ts --configLoader runner ui/src/e2e/chat-flow.clipboard.e2e.test.ts -t 'message-copy failure|assistant code cannot be copied': all three selected Chromium checks passed against the bundled Control UI, including desktop/mobile copy feedback. No visual behavior changed.

  • Changed-gate guards, dead-code checks, and i18n checks passed. Its first typecheck caught a cross-runtime timer-handle mock in the new regression; that mock was replaced with Vitest fake timers. The final core-test and UI typechecks, targeted Oxlint, Stylelint, formatting, and diff checks all passed. This is composed gate evidence, not an uninterrupted changed-gate pass.

  • The broader UI run passed 11,862 assertions and failed four 1-second lazy-module readiness assertions in app-host-pairing-access.test.ts, lazy-shell-action.test.ts, and chat-tool-cards.highlight.test.ts, without a clipboard teardown exception. Those files pass in bounded runs on both the original helper and this fix. Full-suite ordering/load diagnosis is a separate follow-up; this PR does not weaken those assertions or claim the full suite is green.

Production delta: +4/-2 (one document binding and its ownership comment); tests: +49/-18. The deferred global lookup is removed rather than guarded. Dependency source confirms Vitest teardown deletes populated globals while the native timer can remain pending. Codex autoreview completed with no findings. AI-assisted.

@clawsweeper

clawsweeper Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui size: S maintainer Maintainer-authored PR labels Aug 29, 2026
@steipete
steipete marked this pull request as ready for review August 29, 2026 19:31
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 29, 2026
@clawsweeper

clawsweeper Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 29, 2026, 3:44 PM ET / 19:44 UTC.

ClawSweeper review

What this changes

The PR makes Control UI’s legacy clipboard fallback retain its original document for deferred focus restoration and expands tests for teardown, focus replacement, and removed targets.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open: current main still defers a callback that reads the global document after the copy fallback, while this branch binds that callback to the originating document and adds focused lifecycle coverage. The maintainer-owned PR should await exact-head CI and the normal owner merge decision.

Priority: P3
Reviewed head: e569965ea1194657ad0088bf82648ea2c8501251
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary fix with behavior-preserving regression coverage, pending exact-head CI completion.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable: this is a maintainer-authored PR, so the external-contributor proof gate does not apply; the reviewed source and regression coverage match the body’s stated teardown scenario.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable: this is a maintainer-authored PR, so the external-contributor proof gate does not apply; the reviewed source and regression coverage match the body’s stated teardown scenario.
Evidence reviewed 6 items Current-main failure path: The fetched main base schedules deferred focus restoration and dereferences the global document inside that callback, which matches the teardown failure when the global has already been removed.
Introduced owner-boundary repair: The PR captures textarea.ownerDocument before scheduling, then reads activeElement and body from that stable owner while preserving connected-target and newer-focus checks.
Regression coverage: The added test runs the pending callback after replacing the global document, and the table-driven cases retain the existing focus-restoration, newer-focus, and removed-target behavior.
Findings None None.
Security None None.

How this fits together

Control UI copy controls call a shared clipboard helper, which uses the browser Clipboard API or a legacy execCommand fallback. The fallback briefly moves focus for selection, then restores it asynchronously without overwriting a newer user focus.

flowchart LR
  A[Control UI copy control] --> B[Shared clipboard helper]
  B --> C{Clipboard API available}
  C -->|yes| D[Browser clipboard write]
  C -->|no or rejected| E[Legacy copy fallback]
  E --> F[Deferred focus restoration]
  F --> G[Original document state]
Loading

Decision needed

Question Recommendation
Should this maintainer-owned UI lifecycle fix merge once the current exact-head CI run is green? Merge after exact-head CI: Accept the narrow owner-boundary repair once required checks complete successfully on e569965.

Why: The patch has no concrete correctness finding, but the protected maintainer label requires explicit maintainer handling and checks are still in progress.

Before merge

  • Resolve merge risk (P1) - Production code rises by a net two lines; the stable owner-document binding and its lifetime comment should remain the only added production surface.
  • Resolve merge risk (P1) - The runner-teardown timing described in the PR body could not be independently checked against installed or upstream Vitest/JSDOM source because dependency source was unavailable in this read-only environment.
Agent review details

Security

None.

PR surface

Source +2, Tests +31. Total +33 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 4 2 +2
Tests 1 49 18 +31
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 53 20 +33

Review metrics

Metric Value Why it matters
Production versus regression-test delta production +4/-2 (net +2); tests +49/-18 (net +31) The small production increase is limited to retaining document ownership across the deferred callback; the additional tests cover the lifecycle invariant and preserved focus behavior.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the direct owner-document capture in the shared helper; do not replace it with a callback catch, retry, or caller-specific teardown guard.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the direct owner-document capture in the shared helper; do not replace it with a callback catch, retry, or caller-specific teardown guard.

Do we have a high-confidence way to reproduce the issue?

Yes, source-reproducible: current main’s delayed fallback callback reads the global document, so removing that global before timer execution produces the reported failure. This read-only review did not execute the test suite.

Is this the best way to solve the issue?

Yes. Capturing the textarea’s owner document moves the stale lookup to the object that owns the operation while preserving existing focus guards; catching the later error would mask the lifecycle defect.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against f265c0dfb163.

Labels

Label changes:

  • add P3: This is a narrow Control UI test-environment lifecycle repair with no intended user-visible clipboard behavior change.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable: this is a maintainer-authored PR, so the external-contributor proof gate does not apply; the reviewed source and regression coverage match the body’s stated teardown scenario.

Label justifications:

  • P3: This is a narrow Control UI test-environment lifecycle repair with no intended user-visible clipboard behavior change.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable: this is a maintainer-authored PR, so the external-contributor proof gate does not apply; the reviewed source and regression coverage match the body’s stated teardown scenario.

Evidence

What I checked:

  • Current-main failure path: The fetched main base schedules deferred focus restoration and dereferences the global document inside that callback, which matches the teardown failure when the global has already been removed. (ui/src/lib/clipboard.ts:50, f265c0dfb163)
  • Introduced owner-boundary repair: The PR captures textarea.ownerDocument before scheduling, then reads activeElement and body from that stable owner while preserving connected-target and newer-focus checks. (ui/src/lib/clipboard.ts:50, e569965ea119)
  • Regression coverage: The added test runs the pending callback after replacing the global document, and the table-driven cases retain the existing focus-restoration, newer-focus, and removed-target behavior. (ui/src/lib/clipboard.test.ts:88, e569965ea119)
  • Caller and runner context: The shared helper is used by copy controls, while the UI unit project runs jsdom with a cleanup runner that retires per-file state; this is the relevant lifecycle boundary. (ui/vitest.config.ts:168, e569965ea119)
  • Feature provenance: History shows the shared clipboard helper and its original focus-restoration test entered together in commit e1a38de; the current change is a narrow follow-up to that owner. (ui/src/lib/clipboard.ts:49, e1a38de321a8)
  • Dependency signal and limitation: The UI package directly uses Vitest 4.1.11 and JSDOM 29.1.1; their installed/upstream source was unavailable for direct inspection because this checkout has no installed dependency source and GitHub DNS resolution failed. (ui/package.json:54, e569965ea119)

Likely related people:

  • steipete: Peter Steinberger introduced the helper and original focus-restoration coverage in e1a38de, and authored the focused lifecycle follow-up. (role: shared clipboard helper author and recent area contributor; confidence: high; commits: e1a38de321a8, e569965ea119; files: ui/src/lib/clipboard.ts, ui/src/lib/clipboard.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Let the current exact-head CI run finish green before merging.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete

Copy link
Copy Markdown
Contributor Author

The exact two-file fix from this PR landed in #132878 as part of the combined Browser/clipboard integration repair. The combined head passed fresh hosted CI and all 101 focused owner/caller tests, with clean independent review; the original 21 clipboard tests, three real Chromium copy flows and native Vitest teardown reproduction also passed.

Landed commit: 0dcbb90
CI: https://github.com/openclaw/openclaw/actions/runs/33279134911

Closing this PR as superseded after confirming that main contains both exact clipboard source/test blobs from e569965. This PR was not separately merged.

@steipete steipete closed this Aug 29, 2026
@RomneyDa
RomneyDa deleted the codex/frv-clipboard-document-lifecycle branch October 22, 2026 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant