feat(hooks): add resume flag to AfterInvocationEvent#1767
feat(hooks): add resume flag to AfterInvocationEvent#1767mkmeral merged 7 commits intostrands-agents:mainfrom
Conversation
…-invocation Add a writable 'resume' field (AgentInput | None) to AfterInvocationEvent. When a hook sets resume to a non-None value, the agent automatically re-invokes with that input, triggering a full new invocation cycle including BeforeInvocationEvent.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
When a hook sets resume on AfterInvocationEvent while an interrupt is active, the resume loop now calls _interrupt_state.resume() to process the interrupt responses before continuing. This enables hooks to automatically handle interrupts by providing interruptResponse input via event.resume, keeping the agent loop running without returning to the caller. If a hook provides invalid input (e.g. a plain string) while an interrupt is active, _interrupt_state.resume() raises TypeError, same as the normal caller-side interrupt resume path. Added four tests covering: - Hook handles interrupt automatically via resume with responses - Invalid resume input during interrupt raises TypeError - Interrupt without resume still returns to caller normally - Interrupt during a resumed invocation handled end-to-end
|
/strands review |
|
Question: The PR links to issue #1181 ("Add support for skills"), but that issue appears to be about loading knowledge files based on task context, which seems unrelated to the resume/re-invocation functionality. Is there a different issue that this PR addresses, or is this feature part of the broader skills implementation? If this is a standalone feature, consider creating a dedicated issue or clarifying the relationship in the PR description. |
|
Issue: Documentation PR section is incomplete The PR description lists "TBD" for the Documentation PR section. Per the contribution guidelines, PRs that introduce new public API features should either:
This feature adds a significant new capability (autonomous looping via hooks) that users need to understand. Documentation should cover:
Action Required: Please either link a documentation PR or provide justification for why documentation is not needed. |
There was a problem hiding this comment.
Assessment: Request Changes
Well-designed feature that enables powerful autonomous looping patterns through the hooks system. The implementation is clean and follows existing SDK patterns.
Review Categories
- Documentation (Blocking): The Documentation PR section is marked "TBD" - this must be addressed before merge
- Safety: Consider adding guidance or guardrails for infinite loop prevention
- API Design: Open discussion on
resumevscontinuenaming should be resolved
Good test coverage with comprehensive edge case handling, including interrupt scenarios.
|
On agent review above: I wanted to keep the same logic for retrying for tools and model providers through hooks. Those "gaps" also exist there. I don't want to overload this feature for now Docs is WIP |
Description
Motivation
Hooks that run after an agent invocation sometimes need to trigger a follow-up invocation automatically. For example, a coding agent hook could run tests and linting after each response, and if checks fail, resume the agent with the error output so it can fix the issues autonomously. Similarly, a validation hook could inspect the agent's output against acceptance criteria and loop until the result passes. Today there's no way to do this from a hook; callers have to build the retry loop externally, which duplicates lifecycle logic and bypasses the hook system entirely.
This PR adds a
resumefield toAfterInvocationEvent. When a hook setsresumeto any validAgentInput, the agent re-invokes itself with that input, firing a full new invocation cycle (includingBeforeInvocationEvent). WhenresumeisNone(the default), behavior is unchanged.Public API Changes
AfterInvocationEventgains a writableresumeattribute:Each resume triggers a complete invocation cycle:
BeforeInvocationEvent→ event loop →AfterInvocationEvent, so all existing hooks participate in every iteration. The resume input is converted to messages via the same_convert_prompt_to_messagespath as normal agent calls.Use Cases
Related Issues
Resolves: #1181
Documentation PR
TBD
Type of Change
New feature
Testing
4 unit tests covering resume triggering, no-resume default, BeforeInvocationEvent firing on resume, and multi-step chaining
3 unit tests for the
resumefield onAfterInvocationEvent(default value, writability, input type acceptance)Manually tested with a Jupyter notebook demo