fix: serialize ACP requests to prevent session/prompt race condition (#499)#628
Open
MrRealORG wants to merge 1 commit into
Open
fix: serialize ACP requests to prevent session/prompt race condition (#499)#628MrRealORG wants to merge 1 commit into
MrRealORG wants to merge 1 commit into
Conversation
…iaomiMiMo#499) The @agentclientprotocol/sdk calls #processMessage without await, causing rapid requests (e.g. session/new followed immediately by session/prompt) to execute concurrently. Since session/new makes an async HTTP call to create the session, session/prompt can fire before the session is stored in the in-memory Map, resulting in 'Session not found' errors. Add a lightweight promise-chain mutex (serialize()) that ensures ACP requests are processed sequentially. Key session-affecting methods (newSession, loadSession, prompt, cancel, setSessionMode, setSessionConfigOption, unstable_setSessionModel) are wrapped. This makes MiMo Code work correctly as an ACP agent backend with clients like AgentClient/MoClaw that send requests in quick succession via stdin/stdout JSON-RPC. Fixes XiaomiMiMo#499
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 a race condition in
mimo acpmode wheresession/promptfails with "Session not found" even when using a valid sessionId just returned bysession/new.Root Cause
The
@agentclientprotocol/sdkcalls#processMessage()withoutawaitin its#receive()loop. This means rapid JSON-RPC requests are processed concurrently, not sequentially. Whensession/newyields at an async HTTP call (sdk.session.create()), the event loop is free to process the next message (session/prompt), which looks up the session in the in-memoryMap— but the session hasn't been stored yet because the HTTP call hasn't completed.Fix
Add a lightweight promise-chain mutex (
serialize()) to theAgentclass that ensures all session-affecting ACP requests are processed sequentially. This is a minimal, non-invasive workaround for the SDK's fire-and-forget behavior.Methods wrapped:
newSession/_newSessionloadSession/_loadSessionprompt/_promptcancel/_cancelsetSessionMode/_setSessionModesetSessionConfigOption/_setSessionConfigOptionunstable_setSessionModel/_unstable_setSessionModelTesting
The fix can be verified by sending rapid ACP requests via stdin:
Fixes #499