fix(image): load Photon via runtime eval on compiled binaries; surface typed errors in omitted-image message#69
Merged
Conversation
…e typed errors in omitted-image message
The Bun compiled binary on Windows could not resolve photon_rs_bg.wasm
from the patched photon_rs.js fallback path under the bunfs `__dirname`,
so `image.normalize()` always failed with PhotonUnavailableError. Two
upstream bugs stacked to hide this from users:
1. The Photon import error was swallowed silently in the catch block.
2. The session processor hard-coded the omitted-image message to the
size-failure wording regardless of which Image.Error tag fired.
Result: every screenshot and image-file read on the v0.1.5 Windows
binary returned `[1 image omitted: could not be resized below the
inline image size limit.]` — even for sub-200 byte PNGs — and the
underlying load failure left no trace in logs.
This commit:
- Logs the swallowed Photon import error via log.warn so future
regressions surface in debug logs.
- Evaluates the patched photon_rs.js via `new Function(...)` over
`await Bun.file(photonJs).text()` when `with: { type: 'file' }`
yields an asset path (compiled binary), after setting the
`__OPENCODE_PHOTON_WASM_PATH` global so the patched module reads
from the embedded wasm asset. Falls back to plain dynamic import in
dev/test where the import returns the module record directly.
- Switches the omitted-image system message in
`session/processor.ts` on the typed error tag extracted from the
Effect Cause via `Cause.findErrorOption`:
PhotonUnavailableError -> image processor unavailable in this runtime
InvalidDataUrlError -> attachment URL malformed
DecodeError -> could not decode image data
SizeError -> could not be resized below the inline image size limit
Groups multiple failures by reason so a single tool call emitting
several dropped images still reports one line per distinct reason.
Closes #68
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #68.
Symptom
On the Windows v0.1.5 release binary, every
browser_executescreenshot and everyreadof an image file returnseven for sub-200-byte PNGs that cannot physically exceed the 4.5 MB ceiling. The misleading message hid a real Photon load failure and burned hours of user + agent debugging.
Root cause (two stacked bugs)
photon_rs.jsfalls back torequire("path").join(__dirname, "photon_rs_bg.wasm")whenglobalThis.__OPENCODE_PHOTON_WASM_PATHis unset at the right moment, and Bun's static bundling evaluates thatrequirebefore the global is observable, so the wasm path never resolves underB:/~BUN/root/.image.normalize()then fails withPhotonUnavailableError.catch { return null }swallowed the import error silently, andsession/processor.tshard-coded the omitted-image message to the size-failure wording regardless of whichImage.Errortag fired.Fix
packages/opencode/src/image/image.tslog.warn("photon image processor unavailable", { error, stack }).with: { type: "file" }yields an asset path (compiled binary), evaluatephoton_rs.jsvianew Function(...)overawait Bun.file(photonJs).text()after setting__OPENCODE_PHOTON_WASM_PATH. The patched module then reads from the embedded wasm asset. Falls back to plainawait import("@silvia-odwyer/photon-node")in dev/test where the import returns the module record directly.packages/opencode/src/session/processor.tsCause.findErrorOptionand switch on_tag:ImagePhotonUnavailableError-> "image processor unavailable in this runtime"ImageInvalidDataUrlError-> "attachment URL malformed"ImageDecodeError-> "could not decode image data"ImageSizeError-> keep the original "could not be resized below the inline image size limit"Verification
bun typecheckfrompackages/opencode/: clean.bun test test/image/image.test.ts: 3 pass, 0 fail (existing tests unchanged).bcode.exe:Readtool): image attaches, no omitted line.[1 image omitted: could not decode image data.](accurateDecodeError, not misleading size).bcode 0.1.5binary reproducing the original symptom before the fix.Summary by cubic
Fixes Photon loading in Windows compiled binaries and shows accurate reasons when images are omitted. Screenshots and file reads now work on the Windows build instead of always being dropped.
@silvia-odwyer/photon-nodein compiled binaries by evaluating the embeddedphoton_rs.jsvianew Function(...)after setting__OPENCODE_PHOTON_WASM_PATH. Falls back to dynamic import in dev/test.log.warninstead of swallowing the error.Written for commit 698f524. Summary will update on new commits. Review in cubic