-
Notifications
You must be signed in to change notification settings - Fork 403
fix: correct recursion depth tracking in agent validation #1288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: correct recursion depth tracking in agent validation #1288
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a critical bug in agent validation where the tState.with() method mutated shared state instead of creating independent copies, causing two issues: (1) depth counters incorrectly counted siblings instead of nesting depth, and (2) visited agents leaked across branches, potentially causing false cycle detection in diamond-shaped agent graphs.
Changes:
- Modified
tState.with()to use copy-on-write semantics, returning a newtStatewith independent depth counter and copiedvisitedAgentsslice - Added comprehensive test coverage for flat sibling lists, deep nesting, cycle detection, and diamond patterns
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| go/internal/controller/translator/agent/adk_api_translator.go | Replaced mutating with() method with copy-on-write version to fix shared state bugs |
| go/internal/controller/translator/agent/adk_api_translator_test.go | Added comprehensive test suite covering flat lists (12 agents), deep nesting (10 and 12 levels), cycles, and diamond patterns |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The tState.with() method mutated shared state (incrementing depth and appending to the same visitedAgents slice) instead of creating copies. This caused flat agent tool lists to incorrectly hit the recursion limit because each sibling tool incremented the same depth counter, making 10+ sibling agent tools appear as 10+ levels of nesting. Replace with copy-on-write semantics: with() now returns a new tState with an independent depth counter and a copied visitedAgents slice. Fixes kagent-dev#1287 Signed-off-by: opspawn <opspawnhq@gmail.com> Signed-off-by: OpSpawn <opspawn@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: OpSpawn <opspawn@users.noreply.github.com>
7220ca2 to
7d98e36
Compare
|
Hi, @opspawn . I have faced the same issue this PR solves. AFAIK, if you merge with main, the kagent team will be able to progress this PR. Thanks! |
Summary
Fixes #1287
The
tState.with()method inadk_api_translator.gomutated shared state instead of creating independent copies. This caused two bugs:Depth counter counted siblings instead of nesting depth: Each call to
with()incremented the samedepthfield on the sharedtState, so an agent with 11+ flat sibling agent tools would hit theMAX_DEPTH=10recursion limit even though the actual nesting depth was only 1.Visited agents list leaked across branches:
append()on the shared slice meant that agents visited in one branch of the tool tree could appear as "visited" in unrelated branches, potentially causing false cycle detection in diamond-shaped agent graphs.The Fix
Replace the mutating
with()method with a copy-on-write version that returns a newtStatewith an independent depth counter and a copiedvisitedAgentsslice.Test Cases Added
Test plan
with()method changed + tests added