Skip to content

feat(codemode): let a synchronous global block on an Effect - #64363

Merged
rekram1-node merged 2 commits into
devfrom
codemode-sync-effect
Nov 17, 2026
Merged

rekram1-node merged 2 commits into
devfrom
codemode-sync-effect

Conversation

@rekram1-node

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

Copy link
Copy Markdown
Collaborator

Issue for this PR

None.

Type of change

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

What does this PR do?

A synchronous Code Mode global that returns an Effect now blocks the program until the Effect finishes and returns its value, or throws its error. This is what a host needs to offer Node-style sync APIs such as fs.readFileSync backed by Effect services. It's split out of #61186, which builds fs on it.

const text = fs.readFileSync("a")            // before: a pending promise, so `text` was not the file
try { fs.failSync() } catch (e) { e.message } // before: an unhandled rejection; now caught here
 startExtensionCall(host, args)
+  sync global returned an Effect   → settle in place: the program blocks until it completes
   Effect from an async global, or any Promise → program promise
   plain value or throw             → now

A global is async when it's an async function or marked with Extension.async, as before. For the published @opencode/codemode package this is a behavior change: an Effect from an unmarked global used to reach the program as a promise and now blocks, so several such calls in one Promise.all run one after another. Hosts that want concurrency mark the global async. The only host in core today, fetch, is marked async, so nothing in OpenCode changes behavior. Extension.Services now collects an Effect global's requirements at any depth (fs.promises.readFile), not just one level in.

How did you verify your code works?

packages/codemode bun test (1647 pass), including a new test where a sync Effect global's value and error reach the next statement; bun typecheck in codemode.

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Public API: @opencode/codemode: a non-async global's (or result function's) Effect now blocks instead of returning a promise; Extension.Services collects requirements at any depth (OpenCode's public API unchanged)

@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.

I recommend approving: a synchronous global that returns an Effect now blocks and gives back its value, or throws its error. The new test fails on the base, and with this change the full packages/codemode suite passes (1647 tests), as does tsc --noEmit. I couldn't run the package's typecheck script here because this Bun build has no check command.

Public API: the OpenCode server, SDK, plugin API, config and CLI are unchanged. The published @opencode/codemode package changes in two ways:

  • Behaviour change: an Effect returned by a global that isn't marked async (or by a function inside a returned value) used to reach the program as a promise and now blocks. Programs that await it still work, but several such calls in a Promise.all now run one after another. Core's only host, fetch, is marked Extension.async, so OpenCode itself is unaffected.
  • Type change: Extension.Services now collects requirements from Effect globals at any depth. This only adds requirements, so it is backwards compatible.

One small inline comment on code and doc comments left over from the old behaviour.

const request = { stage: "settle", ...call, host: result, args, threw } as const
// A synchronous global's Effect blocks the program until it completes, as `fs.readFileSync` does; the request
// settles it in place. An asynchronous global's Effect, and any Promise, become a program promise.
if (!threw && Effect.isEffect(result) && !isAsync(host)) return request

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.

Async hosts never get here: wrap sends them to pending with stage: "run" (line 255). So !isAsync(host) is always true, and the Effect.isEffect(result) part of the next line can no longer be reached. This could be just if (!threw && result instanceof Promise) return pending(...). The doc comments above startExtensionCall and callExtension (src/host.ts) still say a returned Effect becomes a program promise, so they need updating too.

@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.

I recommend approving: the new commits address my earlier point. startExtensionCall now sends only a returned Promise to pending, and the doc comments above it and above callExtension now say that a returned Effect blocks. Behaviour is unchanged from the last review. With this change the full packages/codemode suite passes (1647 tests), as does tsc --noEmit.

Public API: the same as in my first review. OpenCode's public API is unchanged. For @opencode/codemode, an Effect from a global that isn't marked async (or from a function inside a returned value) now blocks instead of returning a promise, and Extension.Services now collects requirements at any depth. The description now spells out the behaviour change.

Nothing new to raise.

@rekram1-node
rekram1-node merged commit 042badd into dev Nov 17, 2026
14 checks passed
@rekram1-node
rekram1-node deleted the codemode-sync-effect branch November 17, 2026 05:54
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