Skip to content

fix(core): send Bun.file bodies from Code Mode fetch - #65917

Merged
rekram1-node merged 2 commits into
devfrom
codemode-bun-file
Nov 24, 2026
Merged

rekram1-node merged 2 commits into
devfrom
codemode-bun-file

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

No GitHub issue: reported as harness feedback (an upload from Code Mode stored "[object Object]").

Type of change

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

What does this PR do?

In Code Mode, fetch(url, { method: "PUT", body: Bun.file(path) }) didn't throw, but the request body was the 15 bytes [object Object]. A BunFile reaches the host fetch as a plain copy of its fields, and globalThis.fetch turns any object it doesn't recognize into that string. Uploads to presigned URLs then failed later with "file not found".

  • A BunFile body (including a slice()) is now read through the same Bun file handle Bun.write already uses for copies, so it uses the read permission Bun.file already got.
  • Any other object body, such as a fetch response, its stream or a plain object, now fails with A fetch body must be a string, a Uint8Array, URLSearchParams, or a file from Bun.file(). instead of silently sending [object Object].

The other half of the report, bytes.byteLength being undefined, is in the interpreter and is fixed separately in psychoscript.

How did you verify your code works?

A throwaway test (not committed) ran Code Mode against a local Bun.serve server:

  • before: a Bun.file body arrived as [object Object]
  • after: the file's bytes arrive, a slice(1, 3) sends just those bytes, strings, Uint8Array and URLSearchParams are unchanged, and a plain object or a response body throws the error above

bun run check and bun run lint:effect-simplifications pass.

Screenshots / recordings

N/A

Checklist

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

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works: in a throwaway Code Mode test against a local Bun.serve, a Bun.file body and its slice(1, 3) arrived as [object Object] on the base, and arrive as the file's bytes with this PR. Strings, Uint8Array and URLSearchParams are unchanged, and plain objects and response bodies now fail with the new error.

The new fetch.ts → bun.ts import creates an import cycle that blocks merging. With this PR, importing src/permission.ts, src/codemode/tool.ts or src/codemode/fetch.ts first throws ReferenceError: Cannot access 'node' before initialization at file-access.ts:327 (Permission.node). All three import cleanly on the base. As a result, at least 15 packages/core test files now fail before running any test, including tool-execute.test.ts (44 pass on the base), codemode-execute-options, tool-registry, aisdk, session-error, automation*, session-model-request* and tool-target-environment. opencode serve still starts, so the app's own import order avoids the cycle, and bun run check doesn't catch it. See the inline comment.

Also worth fixing: a Bun.file body now goes out with no Content-Type header, where Bun would send the file's type. Presigned URLs that sign the content type can reject that (inline).

tsc -b (the package's typecheck:tsc) passes. The public API is unchanged.

Comment thread packages/core/src/codemode/fetch.ts Resolved
Comment thread packages/core/src/codemode/bun.ts Resolved

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both earlier points are fixed. The import cycle is gone: permission.ts, codemode/tool.ts and codemode/fetch.ts import cleanly, and no packages/core test file I checked fails at load any more (tool-execute, codemode-execute-options and session-error together pass 64/64). A Bun.file body now goes out with the file's type as its Content-Type.

I re-ran a throwaway Code Mode test against a local Bun.serve server. On the base, a Bun.file body and its slice arrive as [object Object]. With this PR they arrive as the file's bytes with text/plain;charset=utf-8. Strings, Uint8Array and URLSearchParams are unchanged, plain objects and response bodies fail with the new error, and a missing file fails with ENOENT. tsc -b passes.

The public API is unchanged. Nothing new to raise.

@rekram1-node
rekram1-node merged commit a5232b2 into dev Nov 24, 2026
10 checks passed
@rekram1-node
rekram1-node deleted the codemode-bun-file branch November 24, 2026 05:10
beyondChang pushed a commit to beyondChang/opencode that referenced this pull request Nov 24, 2026
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.

1 participant