Skip to content

Conversation

@knziiy
Copy link
Contributor

@knziiy knziiy commented Oct 4, 2025

Description of Changes

This PR implements a configurable maximum iteration limit for the Agent Core Runtime to resolve issue #1308 and prevent potential infinite loops.

Changes Made:

  • Added iteration limit configuration: Introduced get_max_iterations() function in config.py that reads from MAX_ITERATIONS environment variable (defaults to 20)
  • Implemented iteration tracking: Added iteration counter and limit validation to AgentManager class
  • Created custom exception: Added IterationLimitExceededError for proper error handling when limit is exceeded
  • Integrated callback handler: Connected iteration limit enforcement to agent's callback system

Impact on Existing Users:

  • No breaking changes: Existing functionality remains unchanged
  • Backward compatible: Default limit of 20 iterations ensures existing agents continue to work
  • Optional configuration: Users can adjust limits via environment variable if needed
  • Improved stability: Prevents runaway processes that could affect system performance

The changes are non-breaking and provide additional safety without affecting existing user workflows.

Checklist

  • Modified relevant documentation
  • Verified operation in local environment
  • Executed npm run cdk:test and if there are snapshot differences, execute npm run cdk:test:update-snapshot to update snapshots

Related Issues

@tbrand
Copy link
Contributor

tbrand commented Oct 6, 2025

I apologize for the lack of guidance in the documentation. Could you please address the following error that appears in CI?

Linting changes detected. Please run 'uv run ruff check --fix .' and 'uv run ruff format .' locally and commit the changes.

knziiy added 2 commits October 6, 2025 10:45
Update container image hash in test snapshots to reflect Docker
image rebuild triggered by ruff formatting applied to Python
agent core runtime code.
@knziiy
Copy link
Contributor Author

knziiy commented Oct 6, 2025

Thanks for highlighting the CI issue! I've resolved the errors by:

  • Ruff linting errors: Fixed import statement ordering and added proper blank lines
  • CDK snapshot test errors: Updated test snapshots after the code changes

@tbrand
Copy link
Contributor

tbrand commented Oct 6, 2025

Currently, there are 2 places where self.iteration_count = 0 is set, but I think the most accurate code would be to set self.iteration_count = 0 when receiving the init_event_loop event. What do you think?

Everything else LGTM.

Handle 'init_event_loop' event to reset iteration count,
ensuring proper limit tracking for new agent sessions.
@knziiy
Copy link
Contributor Author

knziiy commented Oct 6, 2025

Thank you for your feedback. I agree with your suggestion, so I've updated the code to set self.iteration_count = 0 when receiving the init_event_loop event.

Note that I've kept the initialization in the constructor to make the instance variable definition clear.
Please let me know if this should also be removed.

@tbrand tbrand merged commit 30d609d into aws-samples:main Oct 6, 2025
3 checks passed
@knziiy knziiy deleted the add-iterationlimit-to-agentcore-agent branch October 6, 2025 07:17
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.

2 participants