[upstream-sync] Port upstream PR #664: register sessions before RPC + :on-event handler#50
Closed
github-actions[bot] wants to merge 1 commit intomainfrom
Closed
Conversation
- Add :on-event optional config key to create-session and resume-session configs. Registers an event handler before the session.create/session.resume RPC so early events (e.g. session.start) emitted during session creation are not dropped. - Refactor session creation to register session state in client state BEFORE issuing the RPC call. Session ID is now generated client-side (UUID) when not provided, so the session can be registered before the server assigns one. - Add update-workspace-path! and cleanup-failed-session! internal helpers in session.clj to support the two-phase session registration pattern. - Add ::on-event fn? spec and update session-config-keys / resume-session-config-keys sets in specs.clj. - Update API.md to document :on-event config option. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Ports upstream PR #664: Register sessions before RPC to prevent dropped events.
Upstream Changes Ported
PR #664 — Register sessions before RPC
Problem: Events emitted by the CLI during
session.create(e.g.session.start) were being dropped because the Clojure SDK only registered the session in its local state after the RPC completed. By the time the session was registered, the events had already been dispatched.Fix: Session state (tool handlers, permission handler, event channel/mult) is now registered in the client's state atom before the
session.create/session.resumeRPC is issued. The session ID is generated client-side using a UUID when not provided by the caller, so the session can be registered before the server assigns one.New
:on-eventconfig optionAdded to both
create-sessionandresume-sessionconfigs:The handler is registered before the RPC call, ensuring
session.startand any other events emitted during session creation are delivered without being dropped. This is equivalent to callingsubscribe-events+ processing the channel, but executes earlier in the lifecycle.Changes Skipped
session.logandlog!were already ported in v0.1.32.0joinSessionfor extension.tsImplementation Details
session.clj: Added:on-eventparameter tocreate-session. If provided, starts ago-loopthat taps the session'sevent-multand calls the handler for each event. The loop stops automatically when the session is disconnected (event channel closed).session.clj: Addedupdate-workspace-path!— updates workspace-path in state after RPC and returns a newCopilotSessionrecord.session.clj: Addedcleanup-failed-session!— closes the event channel (stopping anyon-eventgo-loop) and removes session state if the RPC fails.client.clj: Replacedfinalize-sessionwithregister-and-create-session!(pre-RPC) +update-workspace-path!(post-RPC).specs.clj: Added::on-event fn?spec; added:on-eventto bothsession-config-keysandresume-session-config-keys.doc/reference/API.md: Documented:on-eventin the config options table.Testing
bb validate-docspasses ✓bb test) requires network access to download Maven dependencies, which is not available in this workflow environment. The code changes are structurally sound and follow established patterns.