Skip to content

fix(opencode): graceful error handling for PDF/image file read failures - #31329

Closed
zhiyiwang-byte wants to merge 3 commits into
anomalyco:devfrom
zhiyiwang-byte:fix/pdf-read-crash-graceful-error-handling
Closed

zhiyiwang-byte wants to merge 3 commits into
anomalyco:devfrom
zhiyiwang-byte:fix/pdf-read-crash-graceful-error-handling

Conversation

@zhiyiwang-byte

@zhiyiwang-byte zhiyiwang-byte commented Jun 8, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #21390

Type of change

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

What does this PR do?

When a PDF file is unreadable (corrupted, permissions issue, truncated), the session crashes. The root cause is in prompt.ts:993 where Effect.catch(Effect.die) converts file read IO errors into unrecoverable defects that bypass all error recovery.

The same file already handles text/plain and directory attachments gracefully using Effect.exit + Exit.isFailure, but the PDF/image path was missing this protection.

Changes:

  1. prompt.ts — Replace Effect.catch(Effect.die) with Effect.exit + Exit.isFailure (matches the existing pattern for text/directory in the same function). On failure, logs the error, publishes an error event, and returns a text message instead of crashing.

  2. read.ts — Wrap fs.readFile in Effect.catch so when the Read tool fails to read a PDF, the error message is descriptive (e.g. "Cannot read PDF file: /path (permission denied)") rather than an opaque defect.

  3. transform.ts — Add empty base64 PDF validation (mirrors the existing empty image check at lines 381-393) to catch corrupted PDFs before they reach the provider API.

How did you verify your code works?

  • tsc --noEmit passes with no errors on all modified files
  • Verified the error propagation path: prompt.ts defect → session crash is eliminated by using Effect.exit which is the same pattern already proven working for text/directory in the same function
  • The check-standards and check-compliance CI checks pass

Screenshots / recordings

N/A — not a UI change.

Checklist

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

Previously, when a PDF or image file was unreadable (corrupted, permissions
error, truncated, etc.), the session would crash because:

1. prompt.ts used Effect.catch(Effect.die) to convert read errors into
   unrecoverable defects during user attachment processing
2. read.ts let fs.readFile errors propagate as raw defects through Effect.orDie
3. transform.ts had no validation for empty/corrupted PDF base64 data

This commit fixes all three crash paths:

- prompt.ts: Replace Effect.catch(Effect.die) with Effect.exit + Exit.isFailure
  pattern (consistent with text/plain and directory handling in same file),
  converting read failures into user-visible error messages instead of defects
- read.ts: Wrap fs.readFile in Effect.catch to produce a descriptive error
  message that surfaces through the tool-error event to the model
- transform.ts: Add empty base64 PDF data check (mirrors existing empty image
  check) to catch corrupted PDFs before they hit the provider API

Fixes anomalyco#21390
@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 8, 2026
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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

@zhiyiwang-byte

Copy link
Copy Markdown
Author

@Hona @thdxr @Brendonovich could you please review this PR? It's been open for a week.

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trying to read pdf files is causing issues

1 participant