Skip to content

fix: Fix State restoration in Agent from a before_run hook - #12253

Merged
sjrl merged 1 commit into
mainfrom
update-agent-state-restoration
Aug 6, 2026
Merged

fix: Fix State restoration in Agent from a before_run hook #12253
sjrl merged 1 commit into
mainfrom
update-agent-state-restoration

Conversation

@sjrl

@sjrl sjrl commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Keep an Agent's execution counter in sync with step_count restored by a before_run hook, so restarted Agents continue from the saved step instead of resetting the count.

Related to the work being done in deepset-ai/hayhooks#253 for durable agent execution.

While I was working on this I realized I could help improve readability of the Agent code by using the keyword args when calling functions.

How did you test it?

Added new tests

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

…step counter is correctly picked up from the restored state
@sjrl
sjrl requested a review from a team as a code owner August 6, 2026 07:00
@sjrl
sjrl requested review from anakin87 and removed request for a team August 6, 2026 07:00
@sjrl sjrl self-assigned this Aug 6, 2026
@vercel

vercel Bot commented Aug 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Aug 6, 2026 7:00am

Request Review

@sjrl sjrl removed their assignment Aug 6, 2026
Comment on lines +877 to +878
# A before_run hook can restore a saved State, so resume the execution counter from its step count.
exe_context.counter = exe_context.state.data.get("step_count", 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the new line that fixes the original issue

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Aug 6, 2026
Comment on lines +960 to +961
# A before_run hook can restore a saved State, so resume the execution counter from its step count.
exe_context.counter = exe_context.state.data.get("step_count", 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same new line in the async method

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  agent.py 744
Project Total  

This report was generated by python-coverage-comment-action

@anakin87 anakin87 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.

👍

@sjrl
sjrl enabled auto-merge (squash) August 6, 2026 08:14
@sjrl
sjrl merged commit e99ca20 into main Aug 6, 2026
27 checks passed
@sjrl
sjrl deleted the update-agent-state-restoration branch August 6, 2026 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants