Skip to content

fix(composer): replace execFileSync with async execFile to prevent main-process hang - #2227

Merged
vastsa merged 13 commits into
vastsa:mainfrom
jiaxiangjun202315310204:fix/clipboard-powershell-timeout
Nov 9, 2026
Merged

vastsa merged 13 commits into
vastsa:mainfrom
jiaxiangjun202315310204:fix/clipboard-powershell-timeout

Conversation

@jiaxiangjun202315310204

@jiaxiangjun202315310204 jiaxiangjun202315310204 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Keep the Windows clipboard file-list probe asynchronous so PowerShell startup cannot block the Electron main process.
  • Pass Finder plist data to plutil through stdin when reading macOS clipboard files.
  • Add regression coverage for the async PowerShell adapter, child-process failure, and Finder plist input.
  • Show the Composer E2E window so its animation-frame waits cannot stall.

Tests

  • node --test apps/desktop/test/composer-clipboard-directories.test.mjs: 12 passed, 1 skipped (macOS-only).
  • The Finder plutil stdin path is exercised with a simulated child process on Windows. No macOS hardware validation was performed.
  • pnpm build:js: passed.
  • pnpm --filter @pi-desktop/desktop typecheck: passed.

E2E: NOT RUN (blocked by a pre-existing defect)

pnpm test:e2e:composer-paste passes its 4 draft-race scenarios, then stops at cancel did not close and restore attachment focus.

  • Cause: restoreFocus returns focus to the image chip and then unconditionally restores the previously captured text range inside the contenteditable. Chromium moves focus back to the editor, so the assertion fails.
  • This is pre-existing and unrelated to this change. restoreFocus (b6648d1e6, abfe50a5a) and the assertion (78a073a5b) are all in upstream/main, and replacing this branch's product file with the upstream/main version reproduces the identical failure.
  • Alternative validation: the clipboard adapter is covered by the unit tests above, which exercise the async child-process boundary, the timeout/output bounds, and the plutil stdin path.
  • Residual risk: the clipboard directory path is not verified end to end through a running Composer on Windows or macOS.
  • test:e2e:composer-paste is not part of CI, which is why the existing focus defect went unnoticed.

扩展桥接只修改运行时公开值,Pi Agent 仍携带旧档位。同步更新请求状态,保留模型能力校验,避免 getter 和实际 reasoning 参数不一致。

Refs vastsa#1974
主线已合入停止失败时保留运行状态的修复。保留独立问题的行为说明和新增停止场景,使共享分支继续包含当前主线且不改写历史。
@jiaxiangjun202315310204
jiaxiangjun202315310204 force-pushed the fix/clipboard-powershell-timeout branch 3 times, most recently from 3d001e7 to 5f69a28 Compare November 9, 2026 14:19
…in-process hang

The Windows paste-image path used spawnSync to probe the clipboard,
which blocks the main process. On machines where PowerShell startup
exceeds the 2s timeout, this manifests as a hard freeze on paste.
Switch to async execFile with a Promise wrapper so the probe no longer
holds up the main process, regardless of paste type.

nativeClipboardPaths and readComposerClipboardDirectories are now
async, so their existing callers and tests await the result. Two
regression tests cover a probe slower than the former 2s bound and a
failing probe, and both assert the event loop keeps running.

closes vastsa#2220
@jiaxiangjun202315310204
jiaxiangjun202315310204 force-pushed the fix/clipboard-powershell-timeout branch from 5f69a28 to c870341 Compare November 9, 2026 14:42
@jiaxiangjun202315310204
jiaxiangjun202315310204 deleted the fix/clipboard-powershell-timeout branch November 9, 2026 15:56
vastsa and others added 5 commits November 10, 2026 00:04
Exercise the production async child-process boundary with controlled output and failure events. This guards the Windows clipboard probe against blocking the main process while preserving its process timeout and output bounds.
Write the Finder property list to plutil through the child stdin stream. The asynchronous execFile API does not support the synchronous input option, so keep the macOS paste path functional without blocking the main process.
A never-shown window throttles animation frames to roughly 1 Hz and then stops them, so the probe helpers wait on requestAnimationFrame forever and the suite is killed after 45 s with no output. Show the window, as the other frame-dependent suites already do.
fix(runtime): 扩展思考强度调整同步到实际请求

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The async direction addresses a real blocking call, but the production adapter currently crashes on macOS. Independently ran node --test apps/desktop/test/composer-clipboard-directories.test.mjs on macOS after a normal integration with main 036dfa0: 12 pass, 1 FAIL. The real plutil case throws uncaught ERR_INVALID_ARG_TYPE at Buffer.concat([stdout, chunk]): chunk is a string, because execFile defaults to utf8 encoding. The simulated children emit Buffers and therefore hide this production failure. The same helper serves Windows too.

Please explicitly select buffer encoding (encoding: null) or consistently handle string chunks, and retain a real child-process regression that exercises the default encoding boundary. Ensure asynchronous stream errors reject the Promise rather than escaping the event handler. Re-run native Finder decoding and Composer acceptance before landing; current macOS regression prevents approval.

execFile decodes stdout to utf8 strings by default, so Buffer.concat threw ERR_INVALID_ARG_TYPE from inside the data handler and escaped as an uncaught Main-process error. Pin the buffer encoding, accept string chunks defensively, and reject on stream errors.

The mocked child processes emitted Buffers, which is why the macOS run failed while the suite passed. A regression now reads a real child process so the default-encoding boundary is exercised.
@jiaxiangjun202315310204

Copy link
Copy Markdown
Contributor Author

Fixed in ca6cd58ec. The report was correct: execFile decodes stdout to utf8 strings by default, so Buffer.concat([stdout, chunk]) threw ERR_INVALID_ARG_TYPE inside the data handler and escaped as an uncaught Main-process error. The mocked children emitted Buffers, which hid it.

Changes:

  • execFileAsync now passes encoding: "buffer" to execFile, and still normalizes any string chunk with Buffer.from so a caller cannot break it.
  • stdout/stdin error events reject the promise; close and error are guarded by a single settled flag so a stream error cannot escape the handler or resolve twice.
  • execFileAsync is exported so a regression can use a real child process.

Coverage added in composer-clipboard-directories.test.mjs:

  • a real child process (process.execPath -e ...) exercising the default-encoding boundary, asserting the result is a Buffer and that a non-zero exit rejects;
  • a child that emits string chunks, which now parses correctly;
  • a stream error that rejects instead of crashing.

Verification on this host (Windows):

  • node --test apps/desktop/test/composer-clipboard-directories.test.mjs: 15 passed, 1 skipped (macOS-only case).
  • pnpm --filter @pi-desktop/desktop typecheck: passed.

I cannot re-run the native Finder decode on macOS: I have no Apple hardware, so that case is still covered only by the simulated child process, and Composer acceptance on macOS is not verified here. If you can run the suite on macOS, that remains the missing evidence.

@vastsa

vastsa commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Rechecked the author fix on macOS against current main 036dfa0. The previously reproduced stdout decoding crash is resolved: the real child-process regression and native Finder plutil decode both pass. Clipboard/directory/paste suites: 42/42 pass; Desktop tsc --noEmit: pass. After building the candidate preload, the real Composer Electron suite passes all four draft-race scenarios, the full paste probe (directories, images, gallery/focus, session changes and scratch bytes), and restart persistence. The initial local attempts lacked dependency links/preload output; those were corrected, and are not counted as product failures. No native Windows PowerShell execution was performed.

Merged latest main into the branch with an ordinary push, preserving contributor commits. Validated head c63e7c0; actual GitHub merge ref bda93eb has the same tree 8d0756526041ae0f4174dcdd76b34770a409fbde and current main/head parents. Fresh CI was awaiting external-contributor workflow approval; workflow files are unchanged and both runs have now been approved to execute. Waiting for the required checks before final approval/merge.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The previously reproduced real-child stdout decoding crash is fixed. Independent macOS validation recorded above passed 42 clipboard/directory/paste tests, Desktop typecheck, and the full Composer Electron paste/draft/restart suite. The async subprocess boundary removes the synchronous Main-process wait while retaining timeout and output bounds. No native Windows PowerShell execution is claimed. Fresh five reported CI checks are now SUCCESS. Re-fetched current main 036dfa0 and actual merge ref bda93eb: parents match current main and head c63e7c0, and tree 8d0756526041ae0f4174dcdd76b34770a409fbde matches the validated candidate. Approving for merge.

@vastsa
vastsa merged commit 4ad0094 into vastsa:main Nov 9, 2026
5 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.

3 participants