Skip to content

fix(clipboard): 移动端剪贴板双向互通——随 rich 列表并推 case-10 文本、早到文本暂存回灌 - #250

Merged
VallierDev merged 5 commits into
VallierDev:mainfrom
hsxian:fix/clipboard-mobile-interop
Nov 20, 2026
Merged

VallierDev merged 5 commits into
VallierDev:mainfrom
hsxian:fix/clipboard-mobile-interop

Conversation

@hsxian

@hsxian hsxian commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

问题

安卓/iOS 控制端与主机之间剪贴板双向都不通:

  1. 移动端只认 case-10 纯文本,不收 rich 列表——主机此前只发 rich;
  2. 移动端连接后立即复制的文本早于剪贴板桥就绪,直接丢失。

改动

  • webrtc.rs:仅对已实测的移动端控制端(连接头部 Android/iOS)随 rich 列表并推 case-10 文本(>8KB 分块);桌面控制端维持只发 rich offer,不会同时收到两种形态
  • clipboard_protocol.rs/viewer:read_text_request 改收 &mut Assembly,两侧各复用持久 Assembly,多块 ClipboardChange 完整重组,不再把末块当全文;viewer 对已应用文本判重,消除 case-10+rich 双写
  • webrtc.rs:桥 sink 与早到文本合并为同一把锁的状态,「发布 + 回灌」是一次受锁转换,旧文本无法覆盖更新复制

wl-copy 所有权回接(portal 会话死亡后桌面剪贴板失效)按复审意见拆为独立后续 PR:两条 P2 表明回接必须放在 Portal 关闭路径本身(关闭时所有权仍可确认),并补「先关 TEXT、后关 Portal」定序测试。

验证

安卓真机双向复制粘贴正常;--features wayland,portal 406 过(唯一失败为本机基线同样失败的 terminal 环境用例),--no-default-features --features inproc-av check 过。新增测试:跨请求分块重组、重复应答只写一次。

…文本暂存待桥就绪回灌,portal 死会话断开后用 wl-copy 回接所有权,补 RPC 内层 schema 观测日志
@hsxian
hsxian force-pushed the fix/clipboard-mobile-interop branch from 17a9315 to 47c3449 Compare November 19, 2026 07:23
@VallierDev

Copy link
Copy Markdown
Owner

@hsxian 感谢,移动端剪贴板互通很有用。两点请补充:

  1. 「官方主机随 rich 列表并推 case-10 文本」的依据(官方 Windows/Mac 主机的抓包、日志或字符串证据即可)。现在对所有控制端都并推,桌面端(Mac/Windows 官方控制端)同时收到 case-10 和 rich offer 会不会重复写剪贴板或触发两次同步?如官方是按控制端平台区分,请照做。
  2. TEXT_DATA_CHANNEL 形状日志上限从 40 提到 400,常态下日志量偏大,诊断完建议还原。
    已请 Codex 审查。@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T02:13:10.071038Z 2ae18e2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 47c3449b33

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/webrtc.rs Resolved
Comment thread src/webrtc.rs Resolved
Comment thread src/clipboard.rs Resolved
@VallierDev

Copy link
Copy Markdown
Owner

@hsxian Codex 审查有三条,已逐条核实成立:

  1. case-10 并推对桌面控制端有回归(P1):仓库内 Linux 控制端 src/viewer/clipboard.rs 的 read_text_request() 每个 RPC 都新建 Assembly,不能拼接多块;长文本会先把末块写入剪贴板(截断),短文本会写两次。请按已验证的控制端平台(移动端)选择并推,桌面端保持原路径;并推的官方依据仍请补充(同上次评论第 1 点)。
  2. 早到文本回灌覆盖新值(P2):A 被缓存后,若 B 在 clipboard_sink 发布后、bridge.ready.await 完成前到达,随后无条件回灌 A 会覆盖 B。请把缓存移交与 sink 发布放进同一个受锁转换,或让后到输入淘汰旧缓存。
  3. wl-copy 回接条件在关闭路径上永不成立(P2):c_engine/portal_engine.c 的 session_closed() 与 uu_portal_end() 都会先把 clipboard_owns 清零,之后 clipboard_owns() && !clipboard_ready() 不可能为真,回接不会执行。需要在清零前做经所有权校验的移交,或保留所有权快照。

另外上次第 2 点(形状日志上限 400 还原)也请一并处理。改完回复后我再请 Codex 复审。

🤖 Generated with Claude Code

…c handoff

- Push the case-10 ClipboardChange only to controllers verified against
  it (Android/iOS). Desktop viewers keep the single rich offer, so no
  control endpoint receives the text twice, and the unverified
  official-host claim is gone from the comments (review VallierDev#250).
- read_text_request takes &mut Assembly; the viewer and host keep one
  assembly per direction so a split ClipboardChange reassembles instead
  of applying its last chunk, and the viewer drops a repeat of the text
  it just applied.
- Publish the sink and take the early-held text under one lock, so an
  older held copy can never overwrite a newer copy that arrived while
  the bridge started.
- Sample clipboard ownership while the session is alive and reclaim the
  text this session provided after it dies: uu_portal_end/session_closed
  clear clipboard_owns before the old condition could read it, and
  clipboard_poll never returned the text we set ourselves.
- Shape log cap back to 40.
@hsxian

hsxian commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@VallierDev 两点与 Codex 三条均已修,见 d47cb89:

  1. 官方 case-10 依据——如实说明:没有。 仓库与本机上不存在官方 Windows/Mac 主机随 rich 并推 case-10 的抓包、日志或字符串证据;唯一实测是安卓真机双向复制正常,仓库内 live-verified 记录只证明官方主机作为接收方接受 case-10。已按你建议「按已验证平台区分」:case-10 仅在 Controller::is_mobile()(Android/iOS 连接头部)并推,桌面控制端(Windows/macOS/未知)恢复只发 rich offer,与 PR 之前行为完全一致。若你手头有官方主机按平台下发 case-10 的抓包,可按证据再放宽。注释里未经证实的并推断言已删除。
  2. Codex P1(桌面接收端)一并补齐(与移动端组装同一根源):read_text_request 改收 &mut Assembly,viewer 与主机接收端各复用持久 Assembly,多块 ClipboardChange 完整组装;viewer 收文与 last 判重,短文本双路不再双写。新增测试:跨请求分块重组、分块+重复 rich 应答只写一次。
  3. Codex P2(早到覆盖):sink 与早到文本合并为同一 Mutex 状态,「发布 sink + 取走早到文本」是一次受锁转换;接收侧同锁下有 sink 即 send_replace、无 sink 才暂存。后到输入天然淘汰旧缓存。
  4. Codex P2(wl-copy 回接):确认 uu_portal_end()/session_closed() 先清零 clipboard_owns,原条件永不成立。改为活会话期快照:桥循环在会话存活时采样所有权并记录本会话提供的文本(顺带修正文本来源——clipboard_poll 只回报其他 owner 的文本,本会话 set 的那段从未进过它),会话结束后按快照回接;条件抽成纯函数 reclaim_text 并有单测,portal 组合以 --features wayland,portal 构建+真机验证收口。
  5. 形状日志上限已还原 400→40。

@VallierDev

Copy link
Copy Markdown
Owner

d47cb89 看过:case-10 只对移动端并推、桌面端维持原路径,删除了无依据的注释;桌面接收端改为持久 Assembly + 判重;早到文本与 sink 发布同锁移交;回接改用会话存活期的所有权快照并补单测。CI 失败是 Ubuntu 软件源超时,已重跑。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d47cb89d16

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/clipboard.rs Resolved
Comment thread src/clipboard.rs Resolved
@VallierDev

Copy link
Copy Markdown
Owner

d47cb89 的 Codex 复审又提了两条 P2,核实都成立:

  1. 回接前未确认所有权仍有效(clipboard.rs:209):会话关闭后 owned 停在最后一次采样的 true,若关闭期间本地应用复制了新内容 B,之后仍会用旧文本 A 回接覆盖 B。C 侧在关闭时清除所有权、拒绝从死会话移交,正是因为关闭后无法再确认归属。
  2. 回接时机绑定在桥退出而非 Portal 关闭(clipboard.rs:212):正常断开时 TEXT 通道先结束,桥线程退出时 clipboard_ready() 仍为 true,reclaim_text 返回 None;随后 AssistTick 才调用 end_idle() 关闭 Portal,此时已没有人执行回接。

两条合起来看,比较稳妥的做法是把回接放到 Portal 关闭路径本身(关闭前、所有权仍可确认时由 C 侧或其调用方移交),而不是在桥里事后判断;并补一条「先关 TEXT、后关 Portal」的定序测试。如果这部分改动太大,也可以先把 wl-copy 回接拆出去,让本 PR 只保留移动端 case-10 与桌面接收端组装/判重,先合入。

🤖 Generated with Claude Code

Both fresh P2s hold: ownership sampled while alive goes stale across the
close (a local copy during the window gets overwritten by the old text),
and the normal teardown ends TEXT before end_idle() closes the portal, so
the bridge thread that would reclaim has already exited. A correct reclaim
belongs on the portal close path itself, which reaches into C and the main
loop; per review, keep this PR to the mobile case-10 pairing and the
receiver reassembly/dedup, and land the reclaim separately with ordering
tests.
@hsxian

hsxian commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

按建议把 wl-copy 回接整体拆出(bfe83c1):reclaim_text、所有权采样、reclaim_desktop_clipboard 及 C 侧 uu_portal_clipboard_owns 导出与 FFI 全部移除。本 PR 只保留移动端 case-10 门控与桌面接收端持久 Assembly 组装/判重(含既有跨请求重组、重复应答只写一次测试),cargo test --features wayland,portal 406 过(唯一失败为与基线共有的 terminal 环境偶发用例)。

两条 P2 的定序问题(关闭期间 owned 陈旧可覆盖本地新复制、先关 TEXT 后关 Portal 时桥线程已退出无人回接)在新 PR 按 Portal 关闭路径做:关闭前所有权仍可确认时由 C 侧移交,并补「先关 TEXT、后关 Portal」定序测试。新 PR 提好后在此贴链接。

@hsxian hsxian changed the title fix(clipboard): 移动端剪贴板双向互通——随 rich 列表并推 case-10 文本、早到文本暂存回灌、portal 死会话后 wl-copy 回接所有权 fix(clipboard): 移动端剪贴板双向互通——随 rich 列表并推 case-10 文本、早到文本暂存回灌 Nov 19, 2026
@VallierDev

Copy link
Copy Markdown
Owner

bfe83c1 拆出 wl-copy 回接后本 PR 只剩移动端 case-10 门控与桌面接收端组装/判重,范围清楚。请 Codex 复审;回接的后续 PR 提好后我们再看。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bfe83c1d06

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/webrtc.rs Resolved
Comment thread src/viewer/clipboard.rs Resolved
@VallierDev

Copy link
Copy Markdown
Owner

bfe83c1 的 Codex 复审提了两条新 P2,核实都成立:

  1. rich 文本在桥就绪前会丢(webrtc.rs:936):原来 sink 在 bridge.ready.await 之前就发布,watch sender 能先把早到文本排上;现在推迟到 ready 之后,只有 case-10 分支会写入暂存槽 .1,两个 rich(field 22 / 21 应答)分支看到 sink 为 None 直接丢弃,却照常回成功。建议 rich 分支在 sink 不存在时也写入同一暂存槽(与 case-10 同锁),ready 时一并回灌。
  2. 接收判重以 last 永久代表当前剪贴板(viewer/clipboard.rs:156):本地复制了无文本 MIME 的内容(如图片)或后台跳过写入时 last 不变,之后主机再发同一文本会被当作重复丢掉。建议只在同一次双协议传输内判重(例如记下刚经 case-10 应用的文本,仅对紧随其后的 rich 应答去重后即清除),或在本地 selection 变化时清空 last;并补一条「本地换成非文本后再收同一文本仍会写入」的测试。

改完后我们再请 Codex 复审。

🤖 Generated with Claude Code

…y the rich replay

Rich offers answered before the bridge worker is up dropped their text:
only the case-10 branch filled the held slot while the two rich branches
required a visible sink. Route all four receive branches through one
deliver_or_hold helper. Receiver-side dedup no longer treats the last
synced text as the current clipboard: only the immediate rich replay of
the just-applied case-10 text is dropped, so a host copy of the same
text applies again after a local non-text copy replaced the selection.
@hsxian

hsxian commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

c7fa896 两条 P2 已修:

  1. rich 文本就绪前不再丢:四个接收分支(field 22、21 rich、21 case-10、17)统一走新的 clipboard::deliver_or_hold——sink 可见直通,否则在同一把锁下写入暂存槽 .1,ready 时一并回灌(回灌逻辑原有)。case-10 的 22 ack 也改为与交付解耦:文本进暂存槽即视为收下,照常确认。
  2. 接收判重只限同一次双协议传输:SyncState 新增一次性 fresh 标记,仅丢弃「刚经 case-10/17 应用后的紧随 rich 重放」;last 不再参与接收侧过滤(它只服务发送侧防重发)。本地复制图片后主机再发同一文本会正常重新写入。

测试:accept_transfer 钉住重放只丢一次(plain 直通→rich 丢弃→再次 rich 直通);新增用例「应用过后本地转非文本、再收同一文本仍写入」;deliver_or_hold 钉住就绪前只留最新一份。host 408 / viewer 201 全过(terminal 与 websocket 各 1 条为并行时序偶发,单独复跑均 ok)。

@VallierDev

Copy link
Copy Markdown
Owner

c7fa896 两条 P2 的处理方向符合建议(四个接收分支统一 deliver_or_hold 暂存回灌;接收判重改为一次性 fresh 只吞紧随的 rich 重放),新增测试覆盖到位,CI 全绿。请 Codex 复审,通过后合并。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c7fa89672b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/viewer/clipboard.rs Resolved
@VallierDev

Copy link
Copy Markdown
Owner

Codex 新这条 P2 核实成立:receive() 第 164 行在 apply() 之前就设置 fresh,若这次 plain 写入因窗口未聚焦返回 Ok(false),紧随的 rich 重放会被当成重复吞掉并照常应答,剪贴板保持旧值。建议只在写入实际成功后记录 fresh(或写入返回 false 时清除),并补一条定序测试。改好后我再请 Codex 复审。

🤖 Generated with Claude Code

receive() armed fresh before apply(), so a case-10 write skipped by an
unfocused SDL window (Ok(false)) still swallowed the immediate rich
replay of the same copy and the clipboard kept the stale value. Keep the
text in pending_plain through receive and let apply arm fresh only when
the backend write actually landed; skipped or failed writes leave the
replay eligible. Sequenced test: case-10, focus-skipped write, rich
replay still applies.
@hsxian

hsxian commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

2ae18e2 已按建议修:receive() 不再直接武装 fresh,plain 文本改记入 pending_plain,由 apply() 在 backend 写入真正成功时(Ok(true))才转成 fresh;Ok(false)(未聚焦跳过)与 Err 路径都清空,紧随的 rich 重放保持可写入。定序测试 unfocused_write_does_not_swallow_the_following_rich_replay:case-10 → mock SDL 回 Ok(false) → 断言 fresh/pending_plain 均空且 rich 同文本仍放行。viewer 201 / host 408 全过。

@VallierDev

Copy link
Copy Markdown
Owner

2ae18e2 看过:fresh 改由 apply() 在写入真正成功(Ok(true))后从 pending_plain 武装,Ok(false)/Err 都清空,定序测试覆盖了未聚焦跳过后 rich 重放仍放行,处理了上条 P2,CI 全绿。请 Codex 复审,通过后合并。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 2ae18e2fa9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@VallierDev
VallierDev merged commit e9844a0 into VallierDev:main Nov 20, 2026
6 checks passed
@VallierDev

Copy link
Copy Markdown
Owner

Codex 复审 2ae18e2 无问题、CI 全绿,已 squash 合并,将随下个版本发布。感谢!

🤖 Generated with Claude Code

@hsxian
hsxian deleted the fix/clipboard-mobile-interop branch November 20, 2026 03:31
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.

2 participants