Skip to content

fix(windows): 粘贴前校验原目标窗口已恢复#121

Merged
appergb merged 3 commits into
Open-Less:mainfrom
Cooper-X-Oak:codex/windows-current-caret-insertion
May 1, 2026
Merged

fix(windows): 粘贴前校验原目标窗口已恢复#121
appergb merged 3 commits into
Open-Less:mainfrom
Cooper-X-Oak:codex/windows-current-caret-insertion

Conversation

@Cooper-X-Oak
Copy link
Copy Markdown
Contributor

@Cooper-X-Oak Cooper-X-Oak commented May 1, 2026

Closes #96

修复要点

Windows 插入链路此前已经会在录音开始时记录原前台窗口,并在插入前尝试恢复。但恢复结果没有被验证:如果 SetForegroundWindow 失败,后续仍可能继续盲发 Ctrl+V,文本就有机会粘到当前错误窗口。

本 PR 把 Windows 插入改成“先证明目标可接收,再发送粘贴”:

  • 插入前恢复录音开始时捕获的原 HWND。
  • 恢复后再次读取 foreground window,确认它就是原目标窗口。
  • 如果目标窗口不存在、无效,或恢复后仍不是前台,则不发送 Ctrl+V
  • 无法证明目标时只复制到剪贴板,并返回 copiedFallback,避免误粘到错误窗口。
  • 保持成功发送粘贴时的 pasteSent 语义:它仍表示“快捷键已发送”,不夸大成已证明控件接收。
  • 收紧 Windows real-ASR smoke:不再接受 copiedFallback 作为通过条件,并要求目标窗口回读包含最新 finalText
  • 修正 real-ASR smoke 的 hook 日志匹配:兼容当前 Windows low-level keyboard hook / hotkey listener installed 日志文案。
  • 新增 -ManualSpeech 模式,允许真人对真实麦克风说话,避免系统 TTS 无法进入麦克风时误判 real-ASR smoke。

测试计划

  • npm run build
  • cargo +stable-x86_64-pc-windows-gnu check --lib --manifest-path openless-all\app\src-tauri\Cargo.toml --target x86_64-pc-windows-gnu
  • npm run tauri build -- --target x86_64-pc-windows-gnu --no-bundle
  • PowerShell parse: openless-all/app/scripts/windows-real-asr-insertion-smoke.ps1
  • git diff --check
  • GitHub Actions: Windows Tauri checks 通过
  • windows-real-asr-insertion-smoke.ps1 -Target notepad -ManualSpeech -DebugHotkeyEvents:真实麦克风完整通过。证据:history raw=剩下一个火山引擎的 secret 是没有用,是吗?insertStatus=pasteSent,notepad readback length=25。
  • windows-real-asr-insertion-smoke.ps1 -Target browser -ManualSpeech -ManualSpeechSeconds 15 -DebugHotkeyEvents:真实麦克风完整通过。证据:history raw=在许多 Microsoft 的时候会随时登记一个个信息。insertStatus=pasteSent,browser readback length=30。

备注

本 PR 已在 Windows 本机用真实麦克风完成 notepad 与 browser 两类目标回写验证。测试过程中曾遇到浏览器首次启动弹窗导致临时窗口句柄失效,guard 正确降级为 copiedFallback,没有盲贴到错误窗口;关闭弹窗并重跑后 browser smoke 通过。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Sorry @Cooper-X-Oak, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@Cooper-X-Oak Cooper-X-Oak changed the title fix(windows): guard paste by restored target fix(windows): 粘贴前校验原目标窗口已恢复 May 1, 2026
@H-Chris233
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: c177634a0c

ℹ️ 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 on lines 1601 to +1604
std::thread::sleep(Duration::from_millis(60));

let foreground = unsafe { GetForegroundWindow() };
if foreground != hwnd {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wait for target activation before aborting paste

After SetForegroundWindow, the code sleeps a fixed 60ms and then does a single GetForegroundWindow check; if it still differs, insertion is downgraded to CopiedFallback. On Windows, foreground activation across input queues can complete asynchronously, so this can produce false negatives under load or with slow target UIs (the target becomes foreground shortly after the check), causing missed auto-paste even when restore would have succeeded. This regression was introduced by the new guard path and can reduce insertion reliability for real users.

Useful? React with 👍 / 👎.

@appergb appergb merged commit 949c511 into Open-Less:main May 1, 2026
2 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.

[Windows][P0] 当前插入链路没有真正的 current-caret 保证,只是盲发 Ctrl+V

3 participants