Skip to content

Breaking: give TemporalAgent the worker model's context window and model id - #194

Merged
campoy merged 7 commits into
mainfrom
fix/strands-python-model-context-window-hidden
Oct 28, 2026
Merged

campoy merged 7 commits into
mainfrom
fix/strands-python-model-context-window-hidden

Conversation

@campoy

@campoy campoy commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

TemporalAgent now gives Strands the worker model's model_id and context_window_limit in
Workflow code. Before, TemporalModel.get_config() returned {}, so proactive compression and
context_manager="auto" divided by Strands' 200,000-token fallback for every model, and Strands'
spans had no gen_ai.request.model.

  • New Activity strands_get_model_config on ModelActivity. It returns only the model's
    model_id and context_window_limit. Nothing else from the provider config reaches History,
    since a config can hold credentials or clients.
  • TemporalAgent reads them once per agent, with the agent's Activity options, in an override of
    Strands' Agent._convert_prompt_to_messages. stream_async awaits that method after it takes
    the invocation lock and registers the idempotency token, and before it starts the agent span.
    So Strands still rejects, deduplicates or aborts overlapping invocations as it does natively,
    also when the first one is cancelled during the read.
  • TemporalModel.get_config() and a new config property, which Strands' tracing reads, return
    them. Callers that overlap the read share it; a failed read is retried by the next invocation.
  • Duplicate invocations wait with workflow.wait_condition, keeping Strands' token and lock
    bookkeeping. Its asyncio.wrap_future path calls methods the Workflow loop does not implement
    on Python before 3.13 and leaves the duplicate waiting forever.
  • README: what the agent reads, the upgrade note, and the migration note below.
  • Tests: one new test in tests/test_model.py. Existing Activity lists include the config read.
    The pre-bytes histories now assert the documented replay mismatch at the config read.
    test_model_context_overflow_legacy_replay.py loses the test that replays an agent turn recorded
    before Let Strands recover from model context window overflow in TemporalAgent #146, with its fixture: that history fails on the new Activity (see below). The file's
    other test, whose history has no agent turn, stays.

Breaking change: proactive compression now uses the model's own window. Code that scaled
compression_threshold by window / 200,000 to work around the old behavior must remove that
scaling, or it compresses too early. Every agent run schedules one more Activity before its first
model call, and Workers that run the model Activities need this version. The title starts with
Breaking: because release notes come from commit subjects.

The read has no version gate. A run whose agent ran before this version schedules
strands_get_model_config on replay where its History has invoke_model, and fails with
[TMPRL1100]. Open runs fail their Workflow Tasks; finished runs fail when they are replayed. Runs
without a TemporalAgent are not affected.

Upgrade: let runs whose agents already ran finish on the earlier version before you upgrade, or
keep them there with Worker Versioning. The same applies to rolling back: an earlier version does not
read the config, so runs that read it are not expected to replay there.

Why?

See #193.

Checklist

  1. Closes [Bug] strands_agents: TemporalAgent hides the worker model's context window and model id from Strands #193

  2. How was this tested:

    • test_model_config_reaches_workflow in tests/test_model.py runs a Workflow on a model whose
      config has a model_id, a context_window_limit and an API key. It checks that Workflow code
      gets only the first two; that two overlapping first invocations end as natively (the second
      raises ConcurrencyException); that successful duplicates share one model call and result;
      that a duplicate of a first invocation cancelled during the read is aborted
      (IdempotencyAbortedError) and never runs its prompt; the Activity list; and replay. It fails on main and passes with the change.
    • make lint and the full suite pass locally (132 tests, Python 3.12.13). The extended model
      test also passes with successful and aborted duplicates and replay. Local uv 0.11.21 is
      outside the plugin's required range, so these targets used a temporary uv config with the
      same dependency settings and no version check. CI uses the required uv version.
    • [Bug] strands_agents: TemporalAgent hides the worker model's context window and model id from Strands #193's reproduction shows the defect on main and none with the change: the agent keeps and
      drops turns as the native agent does, with no fallback warning.
    • Overlapping first invocations on a Worker against a dev server, with and without an
      idempotency token and with a delayed, failed or cancelled read, end as on a native agent, with
      one config read per successful load.
    • Old runs: the stored pre-Let Strands recover from model context window overflow in TemporalAgent #146 agent-turn history from test_model_context_overflow_legacy_replay.py
      fails on this change with [TMPRL1100] ... scheduled event 'invoke_model' does not match ... 'strands_get_model_config'. A replay check on a Worker against a dev server, run on the
      earlier, gated version of this change and not rerun since, recorded main histories that start
      with invoke_model; its control, an extra first Activity, fails with [TMPRL1100].
  3. Any docs updates needed?
    The README section on model selection now says what the agent reads, how to upgrade, and how to
    migrate a scaled compression_threshold.

Not covered:

  • Strands' token estimate still comes from TemporalModel, so a Bedrock model's
    use_native_token_count is not used.
  • Not run: the plugin's default Bedrock model, context_manager="auto" or "agentic", exported
    spans, a mixed rollout with old and new Workers on one Task Queue, and replay of this change's
    histories on main.
  • Let Strands recover from model context window overflow in TemporalAgent #146's strands-agents.context-window-overflow-recovery patch keeps an old path that a
    TemporalAgent no longer reaches in histories recorded before Let Strands recover from model context window overflow in TemporalAgent #146, since they fail on the new
    Activity first. Deprecating that patch is a separate change.
  • temporalio 1.34.x still bundles the old code as temporalio.contrib.strands. No backport here.

Open questions:

  • Activity name: strands_get_model_config (the TypeScript fix in fix(strands): give Strands the worker model's id and context window (breaking) #158 uses
    strandsGetModelConfig), or a plain get_model_config, like invoke_model?
  • The Activity runs for every agent, not only agents with proactive compression, because tracing
    reads the model id too. Is one more Activity per agent run acceptable?
  • The read overrides Strands' private Agent._convert_prompt_to_messages, the only await inside
    the invocation guard before the agent span. Is that acceptable, or should Strands get a public
    hook there?

Draft while these questions are open.

…del id

TemporalModel.get_config() returned {}, so Strands code that runs in the
workflow could not see the worker model's settings. Proactive compression
and context_manager="auto" divided by the 200,000-token fallback for every
model, so an agent on a larger model dropped turns a native agent keeps,
and on a smaller one kept turns a native agent drops. Strands' agent and
model spans had no gen_ai.request.model.

Before its first invocation, TemporalAgent now reads the worker model's
model_id and context_window_limit with a new strands_get_model_config
activity, using the agent's activity options. TemporalModel.get_config()
and a new config property return them. Only those two fields go to
History.

Breaking change: proactive compression now uses the model's own window.
Code that scaled compression_threshold by window / 200,000 to work around
the old limit must remove that scaling, or it compresses too early. Every
TemporalAgent run schedules one more activity before its first model call,
and workers that run the model activities must have this version.

Runs whose agents first ran before this change keep an empty config and
schedule no new activity (patch "strands-model-config"); code without
this patch cannot replay runs that recorded it.
Move the strands_get_model_config read inside Strands' invocation guard,
after the idempotency token is registered and before the agent span
starts, so Strands still rejects, deduplicates or aborts overlapping
invocations as it does natively, including when the first is cancelled
during the read. Callers that do overlap (reentrant mode, or a call after
a cancelled first one) share one read; a failed read is retried by the
next invocation.
Fixes add no version gates for now, so remove the
`strands-model-config` patch. Every `TemporalAgent` now reads the worker
model's config before its first invocation, in every run.

Breaking change: the read is a new activity. A run whose agent ran
before this version schedules `strands_get_model_config` on replay where
its history has `invoke_model`, and fails with a nondeterminism error.
Let those runs finish before you upgrade, or use Worker Versioning.

The checked-in history of an agent turn recorded on 5e91c26, which
tested the old path of the overflow translation, fails that way, so its
test and fixture go. The other legacy history has no agent turn and
still replays.
@campoy
campoy marked this pull request as ready for review October 28, 2026 21:00
@campoy
campoy requested a review from a team as a code owner October 28, 2026 21:00
@campoy

campoy commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Ready for review, @brianstrauch

@campoy
campoy requested a review from brianstrauch October 28, 2026 21:02
@campoy
campoy merged commit 73deca0 into main Oct 28, 2026
27 checks passed
@campoy
campoy deleted the fix/strands-python-model-context-window-hidden branch October 28, 2026 21:47
campoy added a commit that referenced this pull request Oct 28, 2026
Follow-up to #194: a two-turn test like #158's. The first reply reports
800 of the model's 1,000 tokens, so proactive compression drops the
first turn before the second model call. Before #194 the plugin used
Strands' 200,000-token fallback and the second call still got turn 1.
campoy added a commit that referenced this pull request Oct 28, 2026
Follow-up to #194: a two-turn test like #158's. The first reply reports
800 of the model's 1,000 tokens, so proactive compression drops the
first turn before the second model call. Before #194 the plugin used
Strands' 200,000-token fallback and the second call still got turn 1.
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: TemporalAgent hides the worker model's context window and model id from Strands

2 participants