fix: preconfigured-client example and singleton client manager dispose recovery#27
Merged
Merged
Conversation
…red client Step 2 handed its preconfigured client to the process-wide singleton client manager, which step 1 had already initialized, so the client was ignored and step 2's requests flowed through step 1's client while the demo reported success. Pass an isolated manager via createInstance() instead, echo each request's x-demo-source header so the output proves which client served it, drop the spurious await on the synchronous v2 createOpencodeClient(), and allow overriding the model via OPENCODE_MODEL.
dispose() set isDisposed but left the static singleton pointing at the dead instance, so every later createOpencode() without an explicit clientManager received the disposed manager and threw "Client manager has been disposed" on first use. Release the singleton slot in dispose() so getInstance() builds a fresh manager, and update the already-initialized warnings to recommend the escape hatches that actually work (createInstance() with the clientManager setting, or resetInstance()).
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.
Summary
Fixes the
client-options.tsexample, which claimed to demonstrate the preconfigured-client pattern but did not, and fixes the underlying client-manager lifecycle issue uncovered while verifying it.Example: step 2 never used the preconfigured client
createOpencode({ client })hands the client to the process-wide singletonOpencodeClientManager, which step 1 had already initialized. The singleton warned and kept using step 1's client, so step 2's requests carried step 1'sx-demo-source: client-options-exampleheader while stdout misleadingly printed a success response.createOpencode({ clientManager: OpencodeClientManager.createInstance({ client }) }), with a comment explaining why.fetchto echo each outgoing request'sx-demo-sourceheader, so the demo output itself proves which client served each request.awaitoncreateOpencodeClient()(synchronous in@opencode-ai/sdk/v2).OPENCODE_MODEL(default unchanged, matching the other examples).Client manager: dispose() bricked future providers
dispose()setisDisposedbut left the static singleton pointing at the dead instance, so any latercreateOpencode()without an explicitclientManagerreceived the disposed manager and threwClient manager has been disposedon first use. This also made the existing warnings' advice ("call dispose() first") incorrect.dispose()now releases the singleton slot when the disposed manager is the singleton, so the nextgetInstance()builds a fresh manager. Non-singleton managers fromcreateInstance()are unaffected.createInstance()with theclientManagersetting, orresetInstance().Also bumps to 3.0.5 with CHANGELOG entries, following the per-fix release convention.
Testing
npm run cipasses (typecheck, lint, 359 tests including the new regression test).opencode serve --hostname 127.0.0.1 --port 4096:npx tsx examples/client-options.ts— header echo confirms step 1's requests carryx-demo-source: client-options-exampleand step 2's carryx-demo-source: preconfigured-client-example.createOpencode({ baseUrl })→ generateText succeeds (previously threwClient manager has been disposed).createOpencode({ client })after singleton init) emits the updated warning with the corrected guidance.