Skip to content

Let Strands recover from model context window overflow in TemporalAgent - #146

Merged
brianstrauch merged 2 commits into
temporalio:mainfrom
avanish-garg:fix/strands-context-window-overflow
Oct 26, 2026
Merged

brianstrauch merged 2 commits into
temporalio:mainfrom
avanish-garg:fix/strands-context-window-overflow

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

What was changed

  • TemporalModel.stream wraps the model activity call. When it fails with an ActivityError whose cause is an ApplicationError of type ContextWindowOverflowException, it raises strands.types.exceptions.ContextWindowOverflowException with the original message, chained from the ActivityError. Every other failure is re-raised unchanged. Both the plain and the streaming_topic activity go through it.
  • StrandsPlugin passes workflow_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.
  • The comment on _TERMINAL_EXCEPTIONS in _failure_converter.py no longer refers to a StrandsWorkflowError rewrap that does not exist; it says which exception is rebuilt on the workflow side.
  • No change to payloads, activity names or the public API. MaxTokensReachedException, StructuredOutputException and SessionException still reach the workflow as an ActivityError.

Why?

Strands recovers from a model ContextWindowOverflowException by reducing the conversation and retrying the cycle, but under TemporalAgent the exception stayed inside the activity failure, so the first overflow failed the whole workflow. With this change TemporalAgent follows the same path as a plain Agent: in the repro in the issue the model sees 5 messages, then 3, and the run completes, the same as the control.

Checklist

  1. Closes [Bug] strands_agents: a model ContextWindowOverflowException fails the workflow instead of letting Strands reduce the conversation and retry #145

  2. 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 plain Agent is the control (calls see [q1 a1 q2 a2 q3], then a shorter list), and the TemporalAgent run completes with the same result, the model sees exactly the same message lists, history is two invoke_model activities with one ActivityTaskFailed, and Replayer replays it. A second workflow test uses a model that always overflows: the workflow fails with a non-retryable ApplicationError of type ContextWindowOverflowException instead of timing out. Model-level tests call TemporalModel.stream inside a workflow and check that the overflow arrives as ContextWindowOverflowException chained from ActivityError, for both the plain and the streaming_topic activity, and that a RuntimeError and a MaxTokensReachedException from the model still arrive as an ActivityError with their own type (6 of the 8 cases: three failure kinds, each for both activities). On main (install rebuilt with make sync and 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 the TemporalModel change (no plugin change) the always-overflowing test is the one that fails. With the full change make lint is clean and make test passes (60, 8 of them new). Not run against a real model provider.

  3. Any docs updates needed?
    No. The README does not describe this behaviour.

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.
@avanish-garg
avanish-garg marked this pull request as ready for review October 23, 2026 09:54
@avanish-garg
avanish-garg requested a review from a team as a code owner October 23, 2026 09:54

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One P1 finding: the new overflow recovery path needs a patch marker to preserve replay compatibility with existing histories.

Comment on lines +156 to +160
if (
isinstance(cause, ApplicationError)
and cause.type == ContextWindowOverflowException.__name__
):
raise ContextWindowOverflowException(cause.message) from error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@brianstrauch
brianstrauch merged commit 495e507 into temporalio:main Oct 26, 2026
15 checks passed
campoy added a commit that referenced this pull request Nov 16, 2026
…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.
campoy added a commit that referenced this pull request Nov 16, 2026
…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.
campoy added a commit that referenced this pull request Nov 16, 2026
…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.
campoy added a commit that referenced this pull request Nov 17, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] strands_agents: a model ContextWindowOverflowException fails the workflow instead of letting Strands reduce the conversation and retry

2 participants