Skip to content

fix(terminal): let a terminal app's OSC 52 copy reach the clipboard in iOS Safari - #2925

Merged
devswha merged 6 commits into
mainfrom
fix/osc52-safari-gesture
Nov 5, 2026
Merged

devswha merged 6 commits into
mainfrom
fix/osc52-safari-gesture

Conversation

@devswha

@devswha devswha commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1620

What

A terminal app that reads the mouse copies by sending OSC 52 after its Copy button is clicked. iOS Safari writes the clipboard only during a user gesture, and the OSC 52 comes back a round trip after the tap ended, so navigator.clipboard.writeText was refused ("clipboard write blocked by the browser") and nothing was copied.

Now the click Ovis hands to the program starts the clipboard write itself: navigator.clipboard.write with a ClipboardItem whose text/plain is still a promise (src/lib/osc52Clipboard.ts). The next valid live OSC 52 from that pane fills it in. Nothing filling it within 1 s, or a cancel, rejects the promise, and the clipboard keeps what it had.

  • Armed only on a click forwarded to a program with mouse reporting on (the existing forwardClick path, which already requires no selection and the same pane, plus an OpenCode press, which xterm reports as is), with Settings > pane clipboard on, and the pane neither observed, held nor with stdin disabled. One reservation at a time; a newer click replaces it.
  • Only output received after the click can fill it (frames carry a receive sequence), so an OSC 52 already queued before the click never lands through the click's write.
  • Cancelled on a pane switch or reconnect (retireOutput), unmount, a selection being made, a switch to observing, and when the copy arrives for another pane or output.
  • Replayed output, malformed, query (c;?) and oversized OSC 52 never fill it (the existing replay-provenance and parseOsc52 checks run first).
  • Without a reservation (no click, or a browser without ClipboardItem), the existing writeText path is unchanged. A reserved write that fails is not retried later (that would be a write outside the click).
  • Not adopted: a delayed write after the gesture (agent-of-empires' fallback), arming on arbitrary keys or pointer input, a new setting.

Why now

The issue's first measurement found that herdr 0.9.5's attach stream drops OSC 52 entirely (herdrdev/herdr#4612). herdr-core (hc 0.1.17) forwards it on terminal.attach (checked on an isolated core: both BEL and ST forms arrive), so on a herdr-core server the gesture limit is what blocks the copy.

Evidence (real browsers, real input, real clipboard)

Setup: Ovis server from this branch / from main on 127.0.0.1 with OVIS_RUNTIME=herdr-core on an isolated hc 0.1.17 core (own HC_SESSION, XDG dirs, socket, mktemp state), reached from the test MacBook Air through an ssh reverse tunnel (localhost = secure context). Fixture pane: SGR mouse reporting on, every left release sends ESC ] 52 ; c ; base64("OVIS1620-<run>-<n>-<ms>") BEL and logs it.

main 1b9b36b this branch
macOS Safari 27.0.1, CGEvent clicks, readback pbpaste 10/10 10/10
iOS 27.0 Safari (Simulator), WebDriverAgent taps, readback = native Paste into Safari's address field 0/10 (preseed kept) 10/10

Negative cases on this branch, clipboard preseeded before each try (3 tries per case per browser):

program's answer to the click macOS Safari iOS Safari
nothing preseed kept 3/3 preseed kept 3/3
malformed OSC 52 (c;!!!not-base64!!!) preseed kept 3/3 preseed kept 3/3
query OSC 52 (c;?) preseed kept 3/3 preseed kept 3/3
valid OSC 52 1.5 s after the click (reservation timed out) copied 3/3, by the existing writeText path that main already uses (macOS Safari accepts it without a gesture) preseed kept 3/3

Replayed output, Settings off, observing, a selection, and a pane switch or reconnect follow the code paths listed above; the replay-provenance browser regression (scripts/replay-clipboard-demo-regression.ts) covers the replay part unchanged.

Note: on iOS one tap reaches the program as two releases (already seen in the issue), so the fixture sends two payloads per tap; the first fills the reservation and the second takes the old path, whose refusal shows the "blocked" note although the copy landed. That double release is pre-existing and left for a follow-up.

Validation

  • bun run check fast at 3308540 (contains origin/main): pass (typecheck, build, 11819 unit tests).
  • bun test src/lib/osc52Clipboard.test.ts src/lib/osc52.test.ts: pass; the new test file fails without osc52Clipboard.ts.
  • bun scripts/replay-clipboard-demo-regression.ts: pass (replayed OSC 52 still never writes; live copies still once each).
  • Real Safari / iOS Safari: the tables above ran on the first commit; after the review fixes, e076a49 gave iOS Safari 10/10, and the final code (4cfbee4 build; 3308540 only merges main) gave macOS Safari 10/10 and iOS Safari 10/10, each read back as above.
  • Skipped: bun run check full (CI runs it).

Design reference: a native terminal (iTerm2, Ghostty) writes the system clipboard on OSC 52 directly; a browser cannot without a gesture, so this follows the issue's proposal of a gesture-started write with a promised ClipboardItem rather than any app's after-the-fact fallback.

…OSC 52 copy lands in iOS Safari

Safari writes the clipboard only within a user gesture, and a pane program's OSC 52 answers the click a round trip later. The click forwarded to a mouse-reporting program now starts navigator.clipboard.write with a pending ClipboardItem, and the next valid live OSC 52 of that pane fills it within 1 s; otherwise it is given up and the clipboard is left alone.
…ame after it

Only frames received after the click can fill its reservation; a selection, observing, disabled stdin or a newer press cancels or prevents it; an OpenCode press arms one too; a reserved write that fails is not retried outside the click.
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 24b8a87 and bffde15.

Files selected for processing: 4

  • changelog.d/osc52-safari-gesture-copy.md
  • src/lib/osc52Clipboard.test.ts
  • src/lib/osc52Clipboard.ts
  • src/lib/useTerminalMount.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

…ite; a newer press cancels it

An OSC opened in a frame before the click and ended after it no longer fills the reservation, an older copy leaves the reservation waiting instead of cancelling it, and any new press in the terminal cancels the last click's pending write.
@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Review notes (Codex adversarial review, gpt-6.1-terra xhigh, two passes)

Pass 1 (on 4812638), all fixed in e076a49:

  • HIGH later clipboard action did not cancel the reservation -> a selection cancels it, observing cancels it, arming requires no selection.
  • HIGH output queued before the click could fill it -> frames carry a receive sequence; only output received after the click fills it.
  • MEDIUM failed reserved write fell back to an unguarded later writeText -> removed; a failed reserved write is reported, not retried.
  • MEDIUM OpenCode presses never armed -> an OpenCode press arms on its release.
  • MEDIUM armed while stdin disabled -> arming requires !disableStdin.

Pass 2 (on e076a49), the one fix round in 4cfbee4:

  • HIGH an OSC 52 that began in a frame before the click and ended after it could fill it -> the frame an OSC opened in is tracked; only an OSC that began after the click fills it.
  • MEDIUM an older copy cancelled the reservation the real answer needed -> an older copy now leaves it waiting.
  • MEDIUM a newer press that does not arm (e.g. a link) left the old reservation live -> any new trusted press in the terminal cancels it.
  • HIGH "a copy after the 1 s timeout still writes via writeText": not introduced here. That is main's existing OSC 52 path (writeText on every live OSC 52, unchanged for copies without a reservation); measured: macOS Safari accepts it, iOS Safari refuses it, so the clipboard keeps its value on iOS.
  • MEDIUM (left as is) OpenCode control that copies on mouse-down while the button is held: the reservation starts on release, so such a copy still takes the old path. No such control is known; follow-up only if one shows up.
  • MEDIUM (left as is) turning pane clipboard off and back on within the 1 s window keeps the pending reservation; the setting is checked again when the copy arrives.

Real-device runs on the final code (4cfbee4 build, 3308540 only merges main): see the PR body; updated there when the run completes.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Deploying ovis with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3308540
Status: ✅  Deploy successful!
Preview URL: https://370dd465.ovis.pages.dev
Branch Preview URL: https://fix-osc52-safari-gesture.ovis.pages.dev

View logs

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Local merge gate (owner-approved): bun run check fast passed at this head, omo CLEAR, threads resolved, contains main (incl. #2937). Measured on real browsers via herdr-core: iOS Safari OSC 52 copy 0/10 on main -> 10/10; macOS Safari 10/10 both. Codex HIGHs from both passes fixed except one that is main's existing writeText path (not introduced here; explained above). Earlier CI reds: reconnect-header (fixed on main by #2937) and a 504 download. Merged without waiting for GitHub CI.

@devswha
devswha merged commit a09c22f into main Nov 5, 2026
11 of 12 checks passed
@devswha
devswha deleted the fix/osc52-safari-gesture branch November 5, 2026 09:27
devswha added a commit that referenced this pull request Nov 5, 2026
…#2976)

## What

Settings > Terminal > Draw with libghostty (#2908) is on by default.

- It still only draws on a herdr-core server (`terminal-history`, as the
desktop app runs); on herdr and mirrored panes xterm.js draws exactly as
before.
- The setting stays, per device. A device that turned it off stays off:
a stored `false` is kept, only a missing or invalid value means on.
Note: since #2908 every settings save writes this key, so a device that
saved any setting on a main build after #2908 also has `false` stored
and stays off; released versions never had the key, so their users get
the new default.

## Gates (step 1 checklist, measured on isolated hc 0.1.17 + this stack)

| Gate | Result |
|---|---|
| Phone first load (slow 4G 1.6 Mbps/150 ms, CPU x4, fresh profile,
median of 5) | off 7,510 ms / on 7,918 ms: +408 ms, within +0.5 s (wasm
served br since #2953: 1.11 MB -> 345 KB, re-open fetches no wasm) |
| Low-end phone frames (CPU x6, `seq 1 200000`, ABBA) | after #2959 on
is at or below off: median finish 10.5 s vs 10.8 s, stalls > 50 ms 4 vs
7 |
| Screen reader (real NVDA 2026.2, Windows 11 VM, Edge 156) | on: NVDA
reads new output; off (xterm, screen reader mode off): reads nothing.
Known follow-ups in #2963 |
| iOS long-press copy (iOS 27 Simulator, Safari) | **provisionally
equal**: neither xterm nor libghostty shows a selection/copy menu on a
1.6 s long-press (driven with simctl + AXe touch). Pending one manual
check once Simulator components are installed on the test Mac. Copy
keeps working through the key bar's copy button and OSC 52 (#2925) |

## Validation

- `bun test src/lib/settings`: pass (the two tests that pinned the old
default now pin the new one: missing/invalid means on, a stored `false`
stays off, the key stays out of the whole-record save).
- `bun scripts/terminal-ghostty-demo-regression.ts`: pass (herdr-core
pane drawn with libghostty, herdr's own and off stay xterm).
- `bun run check fast`: pass (exit 0) on this head
devswha added a commit that referenced this pull request Nov 6, 2026
… which selects and scrolls its own history (#2384) (#3126)

## Change

A new per-device setting, off by default: **Settings > Terminal >
Experimental > Codex panes: let Codex select and scroll** (`codexDrag`).
With it on, a plain drag in a Codex pane goes to Codex, the way it
already does in an OpenCode pane (#2368). Codex selects through its
whole history, keeps scrolling that history while the drag is held past
the top or bottom edge, and copies the selection on Ctrl+C or Enter.
With it off, which is the default, every pane behaves exactly as on
main.

With the setting on:
- Shift+drag (Option+drag on a Mac) still makes Ovis's own selection,
which copies on release and offers **Quote in message**, as before.
- On herdr, a note appears where the drag ended: "Codex selected this:
Shift+drag copies or quotes it here" ("Option+drag" on a Mac). herdr
drops Codex's copy (OSC 52), and Ovis cannot read Codex's selection to
quote it, so the note names the way that does both.
- On herdr-core, Codex's OSC 52 copy reaches the page, so no note
appears. This was checked with a real Codex 0.167 through Ovis on hc
0.1.14 in headless Chrome: after a drag, Ctrl+C sent Codex's OSC 52, and
the selected rows landed on the browser clipboard.
- The note appears only after a press that left its cell (a drag), never
after a click. It is shown in the current language.

Every other pane, and links, observe mode, a held pane and touch, keep
their paths.

Refs #2384. The default stays main's behaviour, so this does not close
the issue for everyone. Thanks @matiemer for the report and the
alternate-screen diagnosis.

## Why a setting

Checked on herdr 0.9.5 in an isolated session:
- herdr reports no scroll history for a program on the alternate screen.
A shell after `seq 1 500` reports `max_offset_from_bottom: 462`; after
`ESC[?1049h`, it reports `0`. Ovis's own edge scroll (#634) moves
herdr's viewport, so it has nothing to move there.
- Codex (0.166.0 in a raw pty, and 0.167.0 through Ovis) runs on the
alternate screen with `?1000/1002/1003/1006`. It selects a drag on its
own and scrolls its transcript while the drag is held at the top row.
Through Ovis on herdr with this branch, a real Codex pane scrolled up to
the start of `seq`'s output while the drag was held still past the top
edge.
- Codex copies only with OSC 52, and herdr's attach drops OSC 52: a
shell's `printf '\033]52;c;…\a'` left the browser clipboard unchanged.
Handing every Codex drag to Codex would turn the most common copy on
herdr into one that silently does nothing on another device. Reviewers
flagged that on the first version of this PR, so the hand-over is
opt-in.
- Claude Code (2.1.318) runs on the main screen without mouse reporting,
and its herdr pane keeps its history (`max_offset_from_bottom: 134`).
Ovis's existing edge scroll covers it, so it is not changed here.
- herdr reports which agent runs, never a program's mouse mode, so the
rule keys on the reported agent, as the OpenCode one does.

Design reference: the xterm.js / VS Code / iTerm2 convention, already
followed for OpenCode. A program that reads the mouse gets the drag, and
Shift (Option on macOS) forces the terminal's own selection. Rebuilding
Codex's history inside Ovis from wheel reports was rejected: it would
mean guessing, from screen text, how far Codex had scrolled.

## Known limit (follow-up #3128)

On herdr-core, the drag reaches Codex, and Ctrl+C's OSC 52 copy reaches
the browser clipboard (checked with a real Codex through Ovis on hc
0.1.14). The edge scroll does not happen there yet. While the drag is
held, the pane's stream delivers `ESC[!p` with the mouse and focus modes
off, plus colour and DA queries, two or three times, and Codex stops
scrolling. The same reports sent through `hc attach` in a pty do scroll,
so this is filed as #3128 with the frames.

## Tests

- New `scripts/codex-drag-demo-regression.ts` (demo fixtures, no herdr
or hc), run against both a herdr server and a herdr-core server, with
the setting off and on:
- Off: a plain drag is Ovis's selection, Codex reads no press or
release, and no note appears. This is main's behaviour.
- On: press, motions and release reach Codex in order, and Ovis selects
nothing. On herdr the Shift+drag note shows; on herdr-core it does not.
Shift (Option on a Mac)+drag still makes Ovis's selection and sends
Codex no press, and a plain drag after it is Codex's again and clears
that selection.
- With origin/main's `useTerminalMount.ts`, the "on" cases fail: `herdr,
Codex panes on: the drag never reached Codex`.
- `src/lib/settings.test.ts`: `codexDrag` defaults to off, accepts only
a boolean `true`, and is device-scoped, not shared.
- Translations (ko, ja, zh) for the row and both notes.
`src/lib/i18n.test.ts` passes.

## Screenshots

Taken from the demo build: the Settings row at 1280 and 390, light and
dark, and the herdr note after a drag. The images are attached to the
lane report.

## Validation

- `bun run check fast` at this head: ok, with 12617 unit tests passing.
- In the CI runner image (`herdr-ci-runner:local`):
`codex-drag-demo-regression.ts`, `settings-pages-demo-regression.ts`,
`terminal-arrows-clicks-regression.ts`, `terminal-copy-regression.ts`
and `terminal-copy-off-regression.ts`.
- Not run: the full integration and browser suites.

## Reviews

- First version (`51f39ba5`, hand-over always on): omo BLOCK and Codex
Terra (xhigh) 4×MEDIUM, all about the same point: on herdr, a plain drag
in a Codex pane would no longer copy on another device. The lead chose
an opt-in setting with the note. That is this head.
- Fix round on `501d43ba`:
- Codex Terra LOWs, fixed: a blur now forgets the program's press, so a
later click gets no stale note; the note follows a press that left its
cell, not just press-versus-release cells; and it takes the current
language (`tRef`).
  - omo LOW, fixed: the Shift/Option+drag case is now in the regression.
- omo MEDIUM: this PR body described the first version. It is rewritten
here, and `Fixes` became `Refs`.
- Left as known limits, not changed:
- Codex Terra MEDIUM, Safari on herdr-core: Codex copies on Ctrl+C or
Enter, not on release, so the 1 s clipboard reservation a release starts
(#2925) has expired by then. Desktop Chrome writes the clipboard without
it (checked above); Safari may refuse. Shift/Option+drag stays the copy
that works everywhere.
  - Edge scroll on herdr-core: #3128.
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.

Terminal: let a terminal app's copy reach the clipboard on Safari and iOS

1 participant