Skip to content

fix(core): consolidate diff size constants into packages/core/src/dif…#31928

Open
qian-jiahong wants to merge 1 commit into
anomalyco:devfrom
qian-jiahong:dev
Open

fix(core): consolidate diff size constants into packages/core/src/dif…#31928
qian-jiahong wants to merge 1 commit into
anomalyco:devfrom
qian-jiahong:dev

Conversation

@qian-jiahong

@qian-jiahong qian-jiahong commented Jun 11, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #31655 (fix: #31655)

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

  • Three-layer defense: generation (snapshot), dedicated endpoint (summary), messages serialization (message-v2)
  • 100KB threshold for messages API (batch returns all diffs at once) vs 1MB for per-file diff endpoints (one file at a time) — conservative at batch API to prevent frontend freeze
  • Frontend guard (MAX_RECONSTRUCT_BYTES) as defense-in-depth: skip expensive content reconstruction + re-diff, fall through to fast unified-diff parser
  • Pre-check file content size in snapshot before attempting Number.MAX_SAFE_INTEGER to avoid wasted Myers diff on large files

How did you verify your code works?

  1. Reproduced the freeze: Opened a project with a stalled session containing large diffs (multi-MB patches). Confirmed renderer freeze on fileDiffFromPatch → completePatchContents → parseDiffFromFile path.
  2. Applied fixes and rebuilt: Ran electron-vite build in packages/desktop to produce a production build with all four fix layers.
  3. Confirmed the fix: Reopened the same project. The session loaded without freeze. Large patches were:
    Capped at 1MB during snapshot generation (snapshot/index.ts)
    Discarded >1MB at the per-file diff endpoint (summary.ts)
    Stripped >100KB from messages API responses (message-v2.ts)
    Skipped content reconstruction on the frontend for patches >100KB, falling through to the fast parsePatchFiles path (session-diff.ts)
  4. Verified diff display still works: Normal-sized diffs continued to render correctly with full before/after content reconstruction.
  5. Checked no regression in snapshot generation: Ran existing snapshot tests via bun test in packages/opencode.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Jun 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@qian-jiahong

Copy link
Copy Markdown
Author

Issue for this PR

Closes #31655 (fix: #31655)

Type of change

* [x]  Bug fix

* [ ]  New feature

* [ ]  Refactor / code improvement

* [ ]  Documentation

What does this PR do?

* Three-layer defense: generation (snapshot), dedicated endpoint (summary), messages serialization (message-v2)

* 100KB threshold for messages API (batch returns all diffs at once) vs 1MB for per-file diff endpoints (one file at a time) — conservative at batch API to prevent frontend freeze

* Frontend guard (`MAX_RECONSTRUCT_BYTES`) as defense-in-depth: skip expensive content reconstruction + re-diff, fall through to fast unified-diff parser

* Pre-check file content size in snapshot before attempting `Number.MAX_SAFE_INTEGER` to avoid wasted Myers diff on large files

How did you verify your code works?

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

* [x]  I have tested my changes locally

* [x]  I have not included unrelated changes in this PR

verify :

1. Reproduced the freeze: Opened a project with a stalled session containing large diffs (multi-MB patches). Confirmed renderer freeze on fileDiffFromPatch → completePatchContents → parseDiffFromFile path.

2. Applied fixes and rebuilt: Ran electron-vite build in packages/desktop to produce a production build with all four fix layers.

3. Confirmed the fix: Reopened the same project. The session loaded without freeze. Large patches were:
   Capped at 1MB during snapshot generation (snapshot/index.ts)
   Discarded >1MB at the per-file diff endpoint (summary.ts)
   Stripped >100KB from messages API responses (message-v2.ts)
   Skipped content reconstruction on the frontend for patches >100KB, falling through to the fast parsePatchFiles path (session-diff.ts)

4. Verified diff display still works: Normal-sized diffs continued to render correctly with full before/after content reconstruction.

5. Checked no regression in snapshot generation: Ran existing snapshot tests via bun test in packages/opencode.

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

* "How did you verify your code works?" section is empty. Please explain how you tested.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

updated.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Found potential related PRs:

  1. fix(ui): avoid blocking on large file diffs (PR fix(ui): avoid blocking on large file diffs #30962)

    • Related: Addresses renderer freezing issues when handling large file diffs
  2. fix(ui): prepare diffs off the render thread (PR fix(ui): prepare diffs off the render thread #31309)

    • Related: Also tackles performance issues with diff processing to prevent blocking the render thread

These PRs are related to the same problem domain (preventing renderer freeze on large diffs), though PR #31928 takes a different approach by consolidating diff size constants and implementing a three-layer defense strategy at the core/API level rather than just UI optimization.

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.

Window becomes completely frozen/unresponsive after reopening a project that had a stalled session

1 participant