feat: implement threading fallback with automatic client-side fallback #9
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.
Implement graceful fallback from server-side to client-side threading when preconditions aren't met, ensuring conversations continue successfully.
The proposed branch is essentially the final step in the
contextwindow/plans/2025-09-17-resume-context-plan.mdprocess, which implements a threading fallback feature.Currently,
CallModelWithOptsincontextwindow.goattempts server-side threading when enabled, but:This prevents seamless context resumption when:
Goals as per
2025-09-17-resume-context-plan.mddocumentCurrent Implementation Analysis
ContextWindow.CallModelWithOpts (contextwindow.go:422-509)
Current Flow:
Issues:
Model-Specific Validation (responses_model.go:338-381)
Existing:
canUseServerSideThreading()method validates:Gap: This validation happens inside the model implementation, not at the ContextWindow level.
Solution Design
Architecture Overview
Implementation removes only one block from
contextwindow.goinsideCallModelWithOptsfunction.Explanation of the change
Removed the error-returning block from
origin/mainto implement graceful fallback.Original behavior (origin/main)
New behavior (current implementation)
Replaced with:
Improvements:
shouldAttemptServerSideThreading()before attempting server-side threadingWhy this change was necessary
The original code blocked seamless context resumption when:
The new implementation ensures conversations continue successfully using client-side threading when server-side threading isn't available, which was the goal of the threading fallback feature.
The
go testresults are green, though you will see a lot of INFO messages. This is expected.INFO message structure
These INFO messages come from the
logThreadingDecisionfunction. They record threading decisions duringCallModelexecution.Message format
Field meanings
attempt_server_side: Whether server-side threading was attemptedtrue: Attempted (preconditions met)false: Using client-side threading (fallback or default)reason: Why this decision was madecontext: The context name where the decision occurredCommon reasons
"server-side threading not enabled for context"— Threading disabled for this context"no last_response_id available (first call or chain broken)"— First call or chain broken, using client-side"preconditions met"— Server-side threading attempted"server-side threading failed: <error>"— Server-side attempt failed, falling back"response_id chain invalid: <reason>"— Chain validation failed (e.g., tool calls present, LastResponseID mismatch)Example from test output
Meaning: Using client-side threading because there's no
LastResponseID(first call or chain broken) in contexttest-empty-threading.These logs help trace threading decisions, especially when fallback occurs, and can be used for debugging and monitoring in production.
There might also be some future enhancements if you are interested, like:
That's it. That seems like a lot of changes. Yet, the new tests add only ~1000 LOC, and everything is properly added and documented.
The project seems interesting to tackle. I hope you'll like the PR.