Persist turn closeout messages during shutdown - #261
Draft
anabellabuckvar wants to merge 1 commit into
Draft
Conversation
Co-authored-by: Shelley <shelley@exe.dev>
|
We require contributors to sign our Contributor License Agreement, and we don't have you on file. In order for us to review and merge your code, please contact @crawshaw at david@bold.dev to get yourself added. |
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.
1. Summary
Preserve tool results and LLM closeout errors when Shelley gracefully shuts down during an active turn.
This PR:
2. Context and motivation
A restart can cancel an active turn after a tool finishes but before its result is stored. The write then uses the cancelled turn context and fails with errors such as:
This can leave the conversation ending at an unanswered
tool_use, without a persisted result or explanation of where the turn stopped.The recent error-recording fix handles expired request contexts, but graceful shutdown also appears as
context.Canceled—the same signal used for intentional user cancellation. These cases require different behavior:CancelConversationowns the synthetic tool result and end-of-turn message.Restarts cannot always be eliminated, so graceful shutdown should preserve enough state for an interrupted conversation to remain understandable and repairable.
3. How this PR fixes the bug
This PR introduces an explicit graceful-shutdown cancellation cause and threads it through the conversation lifecycle.
During graceful shutdown:
loop.ErrShutdown.context.WithoutCancel, preserving context values while removing the cancelled turn lifetime.Ordinary user cancellation remains on the original cancelled context. This allows
CancelConversationto record its existing synthetic closeout without competing late writes.The lifecycle changes also prevent an old cancellation path from clearing the handles of a replacement loop.
4. Tradeoffs, considerations, and further work
SIGKILL, or immediate power loss can still prevent persistence.Validation
go test ./loop ./servergo test -race ./loop ./serverRegression coverage includes: