Skip to content

fix(sdk): register agent event handlers before connect - #504

Open
willwashburn wants to merge 1 commit into
mainfrom
fix/sdk-on-before-connect-492
Open

willwashburn wants to merge 1 commit into
mainfrom
fix/sdk-on-before-connect-492

Conversation

@willwashburn

@willwashburn willwashburn commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

AgentClient.on registers event handlers before connect(). Lazy ensureWs() constructs the WebSocket client without opening a socket, and connect() opens that same instance. Registering a handler does not open a socket.

Fixes #492

Replaces draft #494.

Checks

  • npm run test:types in packages/sdk-typescript — pass
  • npx vitest run src/__tests__/agent-ws.test.ts in packages/sdk-typescript — 40 passed

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

View guided diff


Note

Low Risk
SDK-only behavior change that relaxes a prior error path; WebSocket open timing is unchanged and tests cover the new registration order.

Overview
The TypeScript SDK AgentClient no longer requires connect() before registering agent.on.* handlers. Previously those calls threw "WebSocket not connected. Call connect() first."

A private ensureWs() lazily builds the same WsClient instance (including open/close lifecycle wiring) without opening a socket; connect() only calls ensureWs().connect(). Handlers registered early attach to that client and run after the socket opens. Registering handlers alone still does not create a WebSocket.

Tests in agent-ws.test.ts were flipped from expecting throws to covering pre-connect() registration for message, lifecycle, reconnect, and wildcard events. Root and package changelogs note this as an [Unreleased - Patch] fix.

Reviewed by Cursor Bugbot for commit c021fe6. Bugbot is set up for automated code reviews on this repo. Configure here.

agent.on.<event>(...) threw "WebSocket not connected. Call connect()
first." whenever a handler was registered before connect(), even
though the underlying WsClient.on() never required a live socket.
Split WsClient construction into a lazy ensureWs() that both on.* and
connect() share, so handlers registered up front attach to the same
instance connect() later opens.

Fixes AgentWorkforce/skills#157.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T09:52:10.771522Z c021fe6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a00061b9-21c9-4606-a0ba-f2c0f44bdcd0

📥 Commits

Reviewing files that changed from the base of the PR and between 3894b8b and c021fe6.


📒 Files selected for processing (4)
  • CHANGELOG.md
  • packages/sdk-typescript/CHANGELOG.md
  • packages/sdk-typescript/src/__tests__/agent-ws.test.ts
  • packages/sdk-typescript/src/agent.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.



📝 Walkthrough

Walkthrough

The TypeScript SDK now allows event handlers to be registered before connect(). It lazily creates a shared WebSocket client for connection and event registration. Tests and changelogs cover the updated behavior.

Changes

Event registration

Layer / File(s) Summary
Shared WebSocket client
packages/sdk-typescript/src/agent.ts
ensureWs() creates and configures the shared WebSocket client. connect() uses this client.
Pre-connect event handlers
packages/sdk-typescript/src/agent.ts, packages/sdk-typescript/src/__tests__/agent-ws.test.ts, CHANGELOG.md, packages/sdk-typescript/CHANGELOG.md
Typed, lifecycle, and wildcard handlers register through the shared client. Tests cover pre-connect registration, event delivery, and the absence of a WebSocket instance before connecting. Changelogs document the fix.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: khaliqgant


Merge Risk: ⚪ Minimal · up to c021f

Pre-connect event registration is supported, and repeated connect calls do not open an additional socket. The change is ready to merge after normal checks.

Pre-merge checks | Passed 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: allowing agent event handlers to register before connect.
Description check Passed The description directly explains the pre-connect handler registration behavior, lazy WebSocket creation, tests, and linked issue.
Linked Issues check Passed Issue [#492] requires agent.on.* handlers to register before connect() without the previous connection error. AgentClient.onEvent() now calls lazy ensureWs(), and connect() opens the same `W…
Out of Scope Changes check Passed The changes stay within issue [#492]. They update AgentClient WebSocket initialization, add focused automated tests, and document the SDK fix in the changelogs. No unrelated product behavior is show…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR


🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit clicks “connect” with care,
Then finds its handlers waiting there.
The socket opens; events hop,
Message by message, they never stop.
The rabbit thumps: “Pre-connect is fair!”

Comment @coderabbitai help to get the list of available commands.

@devin-ai-integration devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

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.

@relaycast/sdk: agent.on.* throws if registered before connect()

1 participant