-
Notifications
You must be signed in to change notification settings - Fork 5
refactor(kernel-browser-runtime): Add simple message queue to receiveInternalConnections #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The kernel worker initialization was using a convoluted pattern where every incoming message had to traverse a promise chain, even after the server was initialized. This refactoring updates receiveInternalConnections to accept either a direct handler function or a promise that resolves to a handler, implements a lazy cache pattern where subsequent messages use the cached handler directly with zero promise overhead, and simplifies kernel-worker.ts to pass a handler promise instead of wrapping every message in a promise chain. The synchronous listener setup is preserved to avoid missing early connections, while eliminating the performance overhead of promise chaining on every message after initialization. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| // Support both direct handler and promise-based handler | ||
| let handler: HandleInternalMessage | null = null; | ||
| let handlerReady: Promise<HandleInternalMessage>; | ||
|
|
||
| if (typeof handleInternalMessage === 'function') { | ||
| // Direct handler - use immediately | ||
| handler = handleInternalMessage; | ||
| handlerReady = Promise.resolve(handleInternalMessage); | ||
| } else { | ||
| // Promise-based handler - cache once resolved | ||
| handlerReady = handleInternalMessage.then((resolvedHandler) => { | ||
| handler = resolvedHandler; | ||
| return resolvedHandler; | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut says could-be-a-promise-or-could-be-immediate is an anti-pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about mutually exclusive options via an options bag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude replace the handleInternalMessage option with two mutually exclusive options.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
Replace the union type parameter with discriminated union options: - `handler`: Direct handler function (for tests) - `handlerPromise`: Promise-based handler (for async initialization) This addresses the anti-pattern of accepting either a value or a promise of that value, making the API more explicit and easier to understand. Co-authored-by: Erik Marks <rekmarks@users.noreply.github.com>
This comment was marked as outdated.
This comment was marked as outdated.
|
@claude address the concerns you raised in your review except number 2 (which you note is actually fine) |
This comment was marked as outdated.
This comment was marked as outdated.
- Add test coverage for handlerPromise with delayed resolution - Fix documentation to clarify caching behavior (not buffering) - Add explicit error handling for handlerPromise rejection - Rename handlerReady to handlerResolution for clarity Co-authored-by: Erik Marks <rekmarks@users.noreply.github.com>
This comment was marked as outdated.
This comment was marked as outdated.
packages/kernel-browser-runtime/src/internal-comms/internal-connections.ts
Outdated
Show resolved
Hide resolved
packages/kernel-browser-runtime/src/internal-comms/internal-connections.ts
Outdated
Show resolved
Hide resolved
…-connections Remove overly complex error handling logic that differentiated between handler initialization errors and message handling errors. Replace with simple one-liner handler resolution and single catch block. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This edge case is not expected to occur in practice. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This comment was marked as outdated.
This comment was marked as outdated.
FUDCo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya shoor.
Summary
Eliminates the convoluted promise-chaining pattern in kernel worker initialization where every incoming message had to traverse a promise chain, even after the server was initialized.
Changes
receiveInternalConnectionsto accept either a direct handler function or a promise that resolves to a handlerkernel-worker.tsto pass a handler promise instead of wrapping every message in a promise chainBenefits
Test Plan
🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com
Note
Introduces a flexible message handling API for internal comms and simplifies kernel worker initialization.
receiveInternalConnectionsto accept either ahandleror ahandlerPromise, caching the resolved handler and queuing messages until available; preserves logging, validation, and connection lifecyclekernel-worker.tsto construct aJsonRpcServerand pass its handler viahandlerPromise, removing per-message promise chaininghandlerin tests and switches expectations totoHaveBeenCalledExactlyOnceWithwhere applicableWritten by Cursor Bugbot for commit adfee03. This will update automatically on new commits. Configure here.