Repository navigation
Let Strands recover from model context window overflow in TemporalAgent - #146
brianstrauch merged 2 commits into
Conversation
A model provider raises ContextWindowOverflowException when the prompt is too long, and the Strands Agent catches it, reduces the conversation through its conversation manager, and retries. Under TemporalAgent the exception crossed the model Activity as a non-retryable ApplicationError, so the Workflow saw an ActivityError and the recovery never ran. TemporalModel.stream now rebuilds ContextWindowOverflowException from that ApplicationError so Strands handles it as it would without Temporal. The plugin registers it as a Workflow failure type so an overflow the conversation manager cannot reduce fails the Workflow instead of failing the Workflow Task forever. Other model failures are left unchanged.
brianstrauch
left a comment
There was a problem hiding this comment.
One P1 finding: the new overflow recovery path needs a patch marker to preserve replay compatibility with existing histories.
| if ( | ||
| isinstance(cause, ApplicationError) | ||
| and cause.type == ContextWindowOverflowException.__name__ | ||
| ): | ||
| raise ContextWindowOverflowException(cause.message) from error |
There was a problem hiding this comment.
[P1] Preserve replay compatibility for existing overflow histories
Please gate this exception translation with workflow.patched(...) so histories created before this change retain the ActivityError behavior. An existing workflow can catch that error and schedule a fallback activity; after upgrading, Strands instead catches the rebuilt exception, reduces the conversation, and schedules another invoke_model, which does not match the recorded commands and can block running workflows.
I reproduced this by recording a workflow on the PR's base commit (5e91c26) that catches ActivityError and schedules old_overflow_fallback. The history replays successfully with the base implementation, but replaying it with this PR raises NondeterminismError: Activity type of scheduled event 'old_overflow_fallback' does not match activity type of activity command 'invoke_model'. Please add a regression test using a pre-change history; the current replay test only covers histories generated with the new behavior.
Gate the ContextWindowOverflowException rebuild in TemporalModel.stream with
workflow.patched("strands-agents.context-window-overflow-recovery"). A Workflow
recorded before this change may have caught the ActivityError and scheduled a
fallback Activity; replaying it with the translation would make Strands reduce
the conversation and schedule another invoke_model, which fails replay with a
NondeterminismError. Histories without the marker keep the ActivityError
behavior, and new executions record the marker and get the recovery.
The plugin's workflow_failure_exception_types entry needs no patch: it only
changes what happens when the exception escapes Workflow code, which a
pre-change execution could only do by failing its Workflow Task forever.
Tests replay a history recorded on 5e91c26 in which a Workflow catches the
ActivityError and runs a fallback Activity, replay a recorded history whose
Workflow Task fails on an escaped overflow, and assert that a new execution
records the patch marker.
…ore 1.0 Replay is promised only within a major, and the next Strands release is 1.0, so its current major is 1. The strands-agents.context-window-overflow-recovery patch (#146) only kept 0.x runs on the old path, so it goes: a model Activity that fails with a context window overflow always raises ContextWindowOverflowException in the Workflow now. REPLAY_MAJOR moves to 1. The four MCP histories recorded on main after the MCP Activities moved to temporalio-mcp go to 1.x/ and keep replaying. The five recorded on 0.1.0's code stay in 0.x/, and the tests that only replayed them go.
…ore 1.0 Replay is promised only within a major, and the next Strands release is 1.0. The strands-agents.context-window-overflow-recovery patch (#146) only kept 0.x runs on the old path, so it goes: a model Activity that fails with a context window overflow always raises ContextWindowOverflowException in the Workflow now.
…ore 1.0 Replay is promised only within a major, and the next Strands release is 1.0. The strands-agents.context-window-overflow-recovery patch (#146) only kept 0.x runs on the old path, so it goes: a model Activity that fails with a context window overflow always raises ContextWindowOverflowException in the Workflow now.
…gate before 1.0 (#575) ## What was changed Strands Python ships 1.0 next, so its current major is 1, and replay is promised only within a major (the rule #565 adds to AGENTS.md). The `strands-agents.context-window-overflow-recovery` patch from #146 only keeps runs without its marker on the old path. Those are 0.x runs, so let's remove it before 1.0. A model Activity that fails with a context window overflow now always raises `ContextWindowOverflowException` in the Workflow, so Strands reduces the conversation and calls the model again. The overflow test no longer checks for the marker. This PR doesn't need #565: no committed history reaches this gate. ## What will you notice? (Breaking) A 0.x run whose Workflow got a model overflow as an `ActivityError` fails replay on 1.0 with `[TMPRL1100]`. In practice that adds nothing new: every 0.x agent run already fails replay on 1.0, at the model config read from #194 (`invoke_model` where 1.0 schedules `strands_get_model_config`). A run on a `main` build since #146 that hit an overflow recorded the marker, so it doesn't replay either. No release wrote that marker. It breaks the other way too. A run that hits an overflow on this version doesn't record the marker, so it fails replay with `[TMPRL1100]` on a `main` build that has the gate. Runs that never hit an overflow replay on both. ### What should you do? The README now says this next to the other upgrade notes: - Let runs that hit an overflow before you upgrade finish on their original workers, or keep them there with Worker Versioning. - Don't roll workers back to an earlier version while runs that hit an overflow on this version are open. Use Worker Versioning if you need to roll back. - Runs that never hit an overflow need nothing new. ## Why? We decided that replay is promised only within a major, and that a gate that only protects an older major's runs is removed before the next major ships. Strands' next release is 1.0, so this gate goes before it. ## Checklist 1. No issue. 2. How was this tested: - `make format`, `make lint` and `make test` for `python/strands_agents` on Python 3.12: 282 passed, 1 skipped. After the README change, `make sync`, `make lint` and `make test` on Python 3.13: 282 passed, 1 skipped. - Replay, across builds. On Workers with no Workflow cache, I recorded a run that recovers from an overflow, a run whose model always overflows inside a Workflow that catches `ActivityError` and runs a fallback Activity, and a plain run. I recorded them on `main` and on this PR. On `main` I also recorded both overflow runs with `workflow.patched()` returning `False` for this gate, which is what a Workflow without the gate (0.x) does. Then I replayed every one on both builds, along with #146's fallback history (recorded on 0.1.0's code) and a history with a renamed Activity. - On this PR, only these fail, each with `[TMPRL1100]`: the two old-path runs, the two overflow runs from `main` (`Non-deprecated patch marker encountered`), #146's 0.x history (at the model config read, as on `main`), and the renamed Activity. Everything recorded on this PR replays, and so does the plain run from `main`. - Rollback to `main`: this PR's two overflow histories fail with `[TMPRL1100]`; the plain one replays. 3. Any docs updates needed? Yes, and they're in this PR. The Strands README now says what happens to runs that hit an overflow, and how to upgrade and roll back with them open.
What was changed
TemporalModel.streamwraps the model activity call. When it fails with anActivityErrorwhose cause is anApplicationErrorof typeContextWindowOverflowException, it raisesstrands.types.exceptions.ContextWindowOverflowExceptionwith the original message, chained from theActivityError. Every other failure is re-raised unchanged. Both the plain and thestreaming_topicactivity go through it.StrandsPluginpassesworkflow_failure_exception_types=[ContextWindowOverflowException], so an overflow that Strands' conversation manager cannot reduce fails the workflow instead of failing the workflow task on every retry._TERMINAL_EXCEPTIONSin_failure_converter.pyno longer refers to aStrandsWorkflowErrorrewrap that does not exist; it says which exception is rebuilt on the workflow side.MaxTokensReachedException,StructuredOutputExceptionandSessionExceptionstill reach the workflow as anActivityError.Why?
Strands recovers from a model
ContextWindowOverflowExceptionby reducing the conversation and retrying the cycle, but underTemporalAgentthe exception stayed inside the activity failure, so the first overflow failed the whole workflow. With this changeTemporalAgentfollows the same path as a plainAgent: in the repro in the issue the model sees 5 messages, then 3, and the run completes, the same as the control.Checklist
Closes [Bug] strands_agents: a model ContextWindowOverflowException fails the workflow instead of letting Strands reduce the conversation and retry #145
How was this tested:
New tests in
tests/test_model_context_overflow.py(8). A workflow test runs a model that raises the overflow when it is given more than 3 messages: the same model through a plainAgentis the control (calls see[q1 a1 q2 a2 q3], then a shorter list), and theTemporalAgentrun completes with the same result, the model sees exactly the same message lists, history is twoinvoke_modelactivities with oneActivityTaskFailed, andReplayerreplays it. A second workflow test uses a model that always overflows: the workflow fails with a non-retryableApplicationErrorof typeContextWindowOverflowExceptioninstead of timing out. Model-level tests callTemporalModel.streaminside a workflow and check that the overflow arrives asContextWindowOverflowExceptionchained fromActivityError, for both the plain and thestreaming_topicactivity, and that aRuntimeErrorand aMaxTokensReachedExceptionfrom the model still arrive as anActivityErrorwith their own type (6 of the 8 cases: three failure kinds, each for both activities). Onmain(install rebuilt withmake syncand checked against the source) 4 of the 8 fail, the 4 that exercise the overflow; the other 4 guard that nothing else changes and pass either way. With only theTemporalModelchange (no plugin change) the always-overflowing test is the one that fails. With the full changemake lintis clean andmake testpasses (60, 8 of them new). Not run against a real model provider.Any docs updates needed?
No. The README does not describe this behaviour.