Skip to content

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

Open
zhiyiwang-byte wants to merge 1 commit into
anomalyco:devfrom
zhiyiwang-byte:fix/pdf-read-crash-graceful-error-handling
Open

fix(opencode): graceful error handling for PDF/image file read failures#31329
zhiyiwang-byte wants to merge 1 commit into
anomalyco:devfrom
zhiyiwang-byte:fix/pdf-read-crash-graceful-error-handling

Conversation

@zhiyiwang-byte
Copy link
Copy Markdown

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

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
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

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

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.

trying to read pdf files is causing issues

1 participant