Repository navigation
feat(codemode): let a synchronous global block on an Effect - #64363
Conversation
There was a problem hiding this comment.
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
awaitit still work, but several such calls in aPromise.allnow run one after another. Core's only host,fetch, is markedExtension.async, so OpenCode itself is unaffected. - Type change:
Extension.Servicesnow 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 |
There was a problem hiding this comment.
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.
…te the extension call docs
There was a problem hiding this comment.
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.
Issue for this PR
None.
Type of change
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.readFileSyncbacked by Effect services. It's split out of #61186, which buildsfson it.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 → nowA global is async when it's an
asyncfunction or marked withExtension.async, as before. For the published@opencode/codemodepackage 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 onePromise.allrun 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.Servicesnow 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/codemodebun test(1647 pass), including a new test where a sync Effect global's value and error reach the next statement;bun typecheckin codemode.Screenshots / recordings
Not a UI change.
Checklist
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)