fix: causality inversion and addToolMessage ordering#29
Conversation
|
Warning Review limit reached
Next review available in: 46 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughChangesThe runtime buffers Runtime message ordering
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/runtime/src/engine/index.ts`:
- Around line 129-141: Update runTurn and the buffered pendingAddToolMessages
flow so messages queued by addToolMessage are preserved when a later tool throws
or the turn aborts, including the unknown-tool path and the additional abort
path. Drain the buffer in a finally/error path or persist it to session state
before rethrowing, while retaining the existing normal flush ordering.
- Around line 129-141: Update the tool execution flow in the engine around
pendingAddToolMessages and the tool result commit so fire-and-forget
addToolMessage RPCs are tracked and awaited before flushing or committing
messages. Ensure late calls cannot append after pendingToolMessages has been
moved into session.history; preserve queued messages rather than clearing them
until all outstanding worker calls complete.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 1d05214e-7ef4-430e-a6f0-3d87e59fdae4
📒 Files selected for processing (1)
packages/runtime/src/engine/index.ts
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/runtime/src/engine/index.ts`:
- Around line 706-710: Update the tool-call handling in runTurn so an undefined
registry entry for call.name is processed through the existing execution catch
path instead of being thrown before it. Generate the same fallback error
response used for execution failures, associated with the original call.id, and
preserve normal execution for registered tools.
In `@packages/runtime/src/plugin/loader.ts`:
- Around line 308-315: Update the addToolMessage handling around requireCtx so
orphanedAddToolMessages is used only when the invocationId is a string with no
matching pending context. Avoid catching TypeError or errors from
ctx.addToolMessage; let malformed invocation IDs and message-delivery failures
propagate, while preserving buffering for unknown string invocation IDs.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 1f370425-3afe-48ac-ac63-4c1faaf5c61a
📒 Files selected for processing (2)
packages/runtime/src/engine/index.tspackages/runtime/src/plugin/loader.ts
Closes #27, closes #28. - Buffer addToolMessage calls and flush after all tool responses to preserve tool_use/tool_result adjacency (OAI/Anthropic invariant). - Skip last-user-message splice when assistant/toolResponse activity follows it, preventing causality inversion mid-agentic-loop. - Handle unknown-tool defs inline as error results instead of throwing, producing proper toolResponse entries with call.id in history. - Drain the addToolMessage buffer in a finally block on tool-loop errors so messages from earlier tools in the batch are not lost. - Guard addToolMessage against late calls after session.history commit via toolMessagesCommitted flag; late arrivals route to a safe fallback. - In the plugin loader, narrow the addToolMessage catch to only buffer late arrivals (valid string invocationId, no matching context). TypeError and addToolMessage-internal errors now propagate.
Closes #27 and #28
Summary
ctx.addToolMessage()calls made during tool execution (instead of appending immediately) and flush them intosession.pendingToolMessagesonly after all tool responses in the batch (and afterdisableNotificationsinjection), preserving provider-requiredtool_use/tool_resultadjacency.toolMessagesCommittedflag so lateaddToolMessageRPCs don’t mutate an already-committed history batch; if the tool loop throws, bufferedaddToolMessageinjections are drained in afinallyblock.addToolMessagedelivery resilient to late arrivals: if the invocation context is already cleaned up, messages are queued (orphanedAddToolMessages) and drained into the next invocation’sToolContextbefore executing it.No security-critical subsystems (path isolation/verification, sandboxing, or parsing/validation) are changed by this update.