Skip to content

fix(strands): keep conversation history across calls with the same session_id - #471

Open
CasperGN wants to merge 4 commits into
mainfrom
fix/strands-session-history
Open

CasperGN wants to merge 4 commits into
mainfrom
fix/strands-session-history

Conversation

@CasperGN

Copy link
Copy Markdown
Contributor

What and why

A Strands agent wrapped in DaprWorkflowAgentRunner could not hold a conversation. Every call started a new workflow with only the new prompt, and the reply was never saved. session_id only ended up in the workflow instance ID and in events. A plain Strands Agent remembers earlier turns, so wrapping a chat agent silently made it single-turn.

DurableAgent had the same problem: it started from the task alone, and its .messages property never changed.

What changed

  • Runner: the workflow now takes session_id as input. It loads the session history in an activity, runs the loop on history plus the new turn, and saves the full list in a second activity after the run finishes successfully. A failed or max-iteration run saves nothing. Both steps are activities, so replays stay deterministic and a retried save writes the same value.
  • Where history is stored: in the state store when there is one (passed in, or the agent-memory component found by auto-discovery). Otherwise it is kept in a dict inside the runner process, with a single warning that it will not survive a restart or be shared between replicas. That is what plain Strands does today.
  • Key: strands-{runner name}-{session_id}-messages, so different session ids or different runner names never share history.
  • Trimming: saved history is cut to the agent's sliding window size (default 40), then messages are dropped from the front until the first is a user message with no toolResult. This avoids a tool result without its tool call, which providers reject, and keeps the workflow payload small.
  • DurableAgent: starts from agent.messages plus the new turn and writes the finished turn back into the same list. No state store is involved. A failed model call leaves .messages untouched.
  • Docs: runner docstring and the Strands README now say session_id means "this conversation", and include the agent-memory component YAML.

Behaviour change (release note worthy)

Callers that reuse one fixed session_id for unrelated tasks (the docstring examples use "session-123") will now carry history between those tasks. Use a new session_id to start fresh.

Not covered (follow-ups)

  • Other framework runners appear to have the same single-turn behaviour; they can reuse the same load/save activities.
  • The agent's own Strands session manager is still ignored by the runner.
  • Two simultaneous calls with the same session_id: the last save wins.
  • DurableAgent history is not trimmed.
  • DaprAgentWorkflow shares one conversation across sessions; separate issue.

How it was tested

  • uv run ruff format: no changes
  • uv run flake8 diagrid tests --ignore=E501,F401,W503,E203,E704: clean
  • uv run mypy --config-file mypy.ini: no issues in 142 source files
  • uv run pytest tests -m "not integration": 1031 passed, 4 skipped
  • New unit tests cover: second call sees the first, different session ids, different runner names, in-process fallback, trimming, tool-pair rule through full tool-using runs, and DurableAgent behaviour including a failed model call.
  • New e2e test (marked integration) against the sidecar state store was not run in CI here.

Checklist

  • uv run ruff format (no path arguments, so examples/ is covered too)
  • uv run flake8 diagrid tests --ignore=E501,F401,W503,E203,E704
  • uv run mypy --config-file mypy.ini
  • uv run pytest tests -m "not integration"
  • New or changed behaviour has tests
  • New files under diagrid/ carry the license header
  • Commits are signed off (git commit -s)

🤖 Generated with Claude Code

CasperGN and others added 3 commits November 16, 2026 15:12
…same session

DaprWorkflowAgentRunner started every workflow from only the new prompt and
never saved the reply, so a wrapped chat agent became single-turn.

The workflow now loads the session's messages in an activity, runs the loop
on history plus the new turn, and saves the full list in a second activity
after a successful run. History is keyed by runner name and session id, goes
to the state store when there is one (explicit or the agent-memory component)
and otherwise stays in the runner process with a single warning. Saved history
is trimmed to the agent's sliding window (default 40) and cut so it starts on
a user message with no tool result.

DurableAgent now starts from agent.messages and writes the finished turn back
to it. Callers that reuse one session_id for unrelated tasks will now carry
history between those tasks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Casper Nielsen <casper@diagrid.io>
… call

Signed-off-by: Casper Nielsen <casper@diagrid.io>
Signed-off-by: Casper Nielsen <casper@diagrid.io>
@CasperGN
CasperGN requested a review from a team as a code owner November 16, 2026 14:17
strands.Agent reads model.stateful at construction; the base Model class
defaults it to False, but the duck-typed test double lacked it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Casper Nielsen <casper@diagrid.io>

This branch has not been deployed

No deployments
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.

1 participant