Skip to content

refactor: replace static pnpm patch with postinstall script for claude-agent-sdk#13139

Merged
DeJeune merged 2 commits intomainfrom
DeJeune/postinstall-patch
Mar 2, 2026
Merged

refactor: replace static pnpm patch with postinstall script for claude-agent-sdk#13139
DeJeune merged 2 commits intomainfrom
DeJeune/postinstall-patch

Conversation

@DeJeune
Copy link
Copy Markdown
Collaborator

@DeJeune DeJeune commented Mar 2, 2026

What this PR does

Before this PR:
claude-agent-sdk is patched via a static pnpm .patch file that replaces entire minified lines. This breaks every time the SDK is upgraded because obfuscated variable names change with each minification.

After this PR:
A Node.js postinstall script (scripts/patch-claude-agent-sdk.mjs) uses semantic regex patterns to apply the same 3 patches. Since it matches structural patterns (not variable names), it survives SDK version bumps as long as the code structure remains the same.

Why we need it and why it was done in this way

The following tradeoffs were made:

  • Regex-based patching is slightly less precise than a static .patch file, but far more resilient to minified code changes.
  • The script validates all 3 patches applied and exits with error if patterns don't match, so SDK structure changes are caught immediately.

The following alternatives were considered:

  • Keeping the pnpm patch approach — rejected because it requires manual regeneration on every SDK upgrade.
  • Using AST-based patching — rejected as overkill for 3 targeted replacements in minified code.

Breaking changes

None. The same 3 modifications are applied (spawn→fork, remove command destructuring, IPC stdio), just via a different mechanism.

Special notes for your reviewer

The 3 patches applied by the script:

  1. import{spawn as X}import{fork as X} — enables IPC channel
  2. Remove command:VAR, from spawnLocalProcess destructuring
  3. Rewrite spawn call to fork(args[0], args.slice(1), ...) with IPC stdio, removing windowsHide:!0

43 unit tests cover: variable name variations, idempotency, partial matches, and no-match detection.

Checklist

  • PR: The PR description is expressive enough and will help future contributors
  • Code: Write code that humans can understand and Keep it simple
  • Refactor: You have left the code cleaner than you found it (Boy Scout Rule)
  • Upgrade: Impact of this change on upgrade flows was considered and addressed if required
  • Documentation: Not required — internal build tooling change
  • Self-review: I have reviewed my own code before requesting review from others

Release note

NONE

…e-agent-sdk

The SDK ships minified/obfuscated code, so the static .patch file breaks
on every version bump. Use a Node.js postinstall script with semantic
regex patterns that survive variable name changes.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@DeJeune DeJeune requested a review from EurFelux March 2, 2026 05:53
…tions

- Use TypeScript with proper types for patch functions
- Export patch functions so tests import real implementation instead of duplicating regex logic
- Use tsx to run postinstall script, consistent with other scripts

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Copy link
Copy Markdown
Collaborator

@EurFelux EurFelux left a comment

Choose a reason for hiding this comment

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

Note

This review was translated by Claude.

When does this script run? Can the modified code be packaged into the production version?


Original Content

该脚本在什么时机运行?生产版本能打包进去修改的代码吗?

@DeJeune
Copy link
Copy Markdown
Collaborator Author

DeJeune commented Mar 2, 2026

Note

This comment was translated by Claude.

The postinstall hook triggers the script to patch. I verified locally and it can indeed patch successfully.


Original Content

postinstall钩子触发脚本patch,我在本地验证了,的确能够patch成功

Copy link
Copy Markdown
Collaborator

@GeorgeDong32 GeorgeDong32 left a comment

Choose a reason for hiding this comment

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

LGTM! Code quality is solid, test coverage is comprehensive, and the solution is more robust than a static patch.

@DeJeune DeJeune merged commit e72bde3 into main Mar 2, 2026
15 checks passed
@DeJeune DeJeune deleted the DeJeune/postinstall-patch branch March 2, 2026 17:23
@kangfenmao kangfenmao mentioned this pull request Mar 3, 2026
4 tasks
EurFelux pushed a commit that referenced this pull request Mar 3, 2026
…e-agent-sdk (#13139)

### What this PR does

Before this PR:
`claude-agent-sdk` is patched via a static pnpm `.patch` file that
replaces entire minified lines. This breaks every time the SDK is
upgraded because obfuscated variable names change with each
minification.

After this PR:
A Node.js postinstall script (`scripts/patch-claude-agent-sdk.mjs`) uses
semantic regex patterns to apply the same 3 patches. Since it matches
structural patterns (not variable names), it survives SDK version bumps
as long as the code structure remains the same.

### Why we need it and why it was done in this way

The following tradeoffs were made:
- Regex-based patching is slightly less precise than a static `.patch`
file, but far more resilient to minified code changes.
- The script validates all 3 patches applied and exits with error if
patterns don't match, so SDK structure changes are caught immediately.

The following alternatives were considered:
- Keeping the pnpm patch approach — rejected because it requires manual
regeneration on every SDK upgrade.
- Using AST-based patching — rejected as overkill for 3 targeted
replacements in minified code.

### Breaking changes

None. The same 3 modifications are applied (spawn→fork, remove command
destructuring, IPC stdio), just via a different mechanism.

### Special notes for your reviewer

The 3 patches applied by the script:
1. `import{spawn as X}` → `import{fork as X}` — enables IPC channel
2. Remove `command:VAR,` from `spawnLocalProcess` destructuring
3. Rewrite spawn call to `fork(args[0], args.slice(1), ...)` with IPC
stdio, removing `windowsHide:!0`

43 unit tests cover: variable name variations, idempotency, partial
matches, and no-match detection.

### Checklist

- [x] PR: The PR description is expressive enough and will help future
contributors
- [x] Code: Write code that humans can understand and Keep it simple
- [x] Refactor: You have left the code cleaner than you found it (Boy
Scout Rule)
- [ ] Upgrade: Impact of this change on upgrade flows was considered and
addressed if required
- [ ] Documentation: Not required — internal build tooling change
- [x] Self-review: I have reviewed my own code before requesting review
from others

### Release note

```release-note
NONE
```

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
DeJeune added a commit that referenced this pull request Mar 4, 2026
### What this PR does

This is a release PR for **Cherry Studio v1.7.23**.

**Changes included:**
- Bump version from 1.7.22 to 1.7.23
- Update release notes with user-facing bug fixes

### Release Notes

<!--LANG:en-->
Cherry Studio 1.7.23 - Bug Fixes

🐛 Bug Fixes
- [Selection] Fix app crash on Windows when closing action window
- [MiniApp] Fix settings state synchronization and region filter
consistency
- [Plugin Browser] Make detail modal text selectable and improve search
experience
- [Tools] Fix approval card not showing for builtin and provider tools

### Included Commits

- fix(Selection): prevent Windows crash when closing transparent action
window (#13177)
- fix(renderer): synchronize miniapp settings state and respect region
filter (#13166)
- fix: improve plugin browser UX with three small fixes (#13153)
- fix: show approval card for builtin and provider tools (#13154)
- fix: support esc to close modal (#13159)
- fix: open external editor in new window instead of reusing existing
one (#13160)
- fix: render directory Select options with optionRender (#13152)
- refactor: replace static pnpm patch with postinstall script for
claude-agent-sdk (#13139)
- feat: add dev-only message data inspection button (#13142)
- docs: add review workflow to CLAUDE.md (#13145)
- chore(deps): upgrade @uiw/codemirror packages to 4.25.7 (#13149)
- fix(ci): skip CI on PR body/title edits, only re-run on base branch
changes (#13150)

### Review Checklist

- [ ] Review generated release notes in `electron-builder.yml`
- [ ] Verify version bump in `package.json`
- [ ] CI passes
- [ ] Merge to trigger release build

### Release note

```release-note
Cherry Studio 1.7.23 - Bug Fixes

🐛 Bug Fixes
- [Selection] Fix app crash on Windows when closing action window
- [MiniApp] Fix settings state synchronization and region filter consistency
- [Plugin Browser] Make detail modal text selectable and improve search experience
- [Tools] Fix approval card not showing for builtin and provider tools
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: suyao <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
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