Skip to content

fix(opencode): guard against large files in diffFull#18986

Closed
elijahmorg wants to merge 1 commit intoanomalyco:devfrom
elijahmorg:elijah/fix_large_diffs_effect
Closed

fix(opencode): guard against large files in diffFull#18986
elijahmorg wants to merge 1 commit intoanomalyco:devfrom
elijahmorg:elijah/fix_large_diffs_effect

Conversation

@elijahmorg
Copy link
Copy Markdown

Prevent large files from being stored in sessions potentially causing oom issues.

This is a small, targeted, non AI slop PR. I have carefully reviewed and understand the changes myself.

Issue for this PR

Closes #17252 #2585 #17226

Type of change

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

What does this PR do?

Use git file-stat -s to check size of text file before reading it in Snapshot.diffFull. If it is larger than 2MB the file is not read.

This simple fix mitigates one avenue that causes OpenCodes memory usage to balloon w/o harming current functionality.

How did you verify your code works?

  1. Have large text file (easy way to generate a large file is by doing Write heap snapshot in opencode).
  2. Start a session and send a message.
  3. Delete large file
  4. send another message
  5. export session via opencode export <session id>

w/ fix the exported session will be small
w/o fix the exported session will be large (at least the size of the heapsnapshot file).

If the heap snapshot is very large then you can just look at memory usage and see it jump. If the jump is temporary grep dev.log for ERROR and you'll see

ERROR 2026-03-24T17:31:26 +90ms service=default e=Out of memory rejection

This is from session/summary.ts:101
await Storage.write(["session_diff", input.sessionID], diffs)

Where it tries to write the large file and blows up.

If you want to verify yourself

--- a/packages/opencode/src/session/summary.ts
+++ b/packages/opencode/src/session/summary.ts
@@ -10,6 +10,7 @@ import { Snapshot } from "@/snapshot"
 import { Storage } from "@/storage/storage"
 import { Bus } from "@/bus"
 import { NotFoundError } from "@/storage/db"
+import { Log } from "@/util/log"

 export namespace SessionSummary {
   function unquoteGitPath(input: string) {
@@ -90,6 +91,7 @@ export namespace SessionSummary {

   async function summarizeSession(input: { sessionID: SessionID; messages: MessageV2.WithParts[] }) {
     const diffs = await computeDiff({ messages: input.messages })
+
     await Session.setSummary({
       sessionID: input.sessionID,
       summary: {
@@ -98,7 +100,17 @@ export namespace SessionSummary {
         files: diffs.length,
       },
     })
-    await Storage.write(["session_diff", input.sessionID], diffs)
+
+    Log.Default.info("session_diff write start", { sessionID: input.sessionID })
+    try {
+      await Storage.write(["session_diff", input.sessionID], diffs)
+      Log.Default.info("session_diff write done", { sessionID: input.sessionID })
+    } catch (err) {
+      Log.Default.error("session_diff write failed", {
+        sessionID: input.sessionID,
+        err: err instanceof Error ? err.message : String(err),
+      })
+    }
     Bus.publish(Session.Event.Diff, {
       sessionID: input.sessionID,
       diff: diffs,

This fix applies to many large text files - the heap snapshot is just an easy way to generate a large text file.

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

If you do not follow this template your PR will be automatically rejected.

Prevent large files from being stored in sessions potentially causing oom
issues or crash in session/summary.ts Storage.write
@github-actions
Copy link
Copy Markdown
Contributor

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

Found a potential duplicate:

PR #17481 - "fix(opencode): guard against large files in diffFull"

You should verify if PR #17481 is still open and if so, consolidate with it or close one as a duplicate.

@elijahmorg
Copy link
Copy Markdown
Author

elijahmorg commented Mar 24, 2026

@thdxr
Copy link
Copy Markdown
Member

thdxr commented Mar 25, 2026

thanks for prodding us on this!

@thdxr thdxr closed this Mar 25, 2026
@elijahmorg elijahmorg deleted the elijah/fix_large_diffs_effect branch March 25, 2026 12:37
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.

Large file tracked by opencode git tool causes OOM

2 participants