fix: block preemptive generation during tool execution#1166
Open
Raysharr wants to merge 2 commits intolivekit:mainfrom
Open
fix: block preemptive generation during tool execution#1166Raysharr wants to merge 2 commits intolivekit:mainfrom
Raysharr wants to merge 2 commits intolivekit:mainfrom
Conversation
When _markGenerationDone() fires after TTS playout but before tool execution completes, _currentSpeech is cleared in mainTask. This allows onPreemptiveGeneration() to snapshot chatCtx while it contains orphaned FunctionCall items (no matching FunctionCallOutput). The corrupted context causes LLM generation failures and session crashes. Add _toolExecutionInProgress flag set around performToolExecutions in both reply task code paths. The flag is checked in onPreemptiveGeneration() guard to prevent preemptive generation while tools are still executing. Addresses TODO(AJS-272).
🦋 Changeset detectedLatest commit: 4bc2a97 The changes in this PR will be included in the next version bump. This PR includes changesets to release 22 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
If an await between setting the flag true and the explicit false assignment throws, the flag stays true forever and permanently blocks preemptive generation. Wrap both pipeline and realtime tool execution sections in try/finally to guarantee cleanup. Addresses Devin review feedback on PR livekit#1166.
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.
Description
Fix a race condition where preemptive generation can fire while tool execution is still in progress, causing LLM chat context corruption and session crashes.
Changes Made
_toolExecutionInProgressflag toAgentActivitythat is settruewhenperformToolExecutionsstarts andfalsewhen tool execution completes (both normal and interrupt paths)_toolExecutionInProgressinonPreemptiveGeneration()to block preemptive generation while tools are executingRoot Cause
When
_markGenerationDone()fires after TTS playout (line 2107) but beforeawait executeToolsTask.result(line 2108),mainTaskclears_currentSpeech. This opens a window whereonPreemptiveGeneration()passes all guards and snapshotschatCtxwhile it contains orphanedFunctionCallitems (no matchingFunctionCallOutput).removeInvalidToolCalls()inutils.tsstrips the orphans, producing"function call missing the corresponding function output, ignoring"warnings. The corrupted context then causes consecutive LLM generation failures.Addresses
TODO(AJS-272).Pre-Review Checklist
Testing
_toolExecutionInProgressguard inonPreemptiveGeneration()outbound-1774267632790crashed with this exact sequenceAdditional Notes
Timeline from production logs:
The fix is minimal and surgical — a single boolean flag checked in the existing guard clause. No changes to tool execution flow, scheduling, or
_markGenerationDone()ordering.