Skip to content

Conversation

@opspawn
Copy link
Contributor

@opspawn opspawn commented Feb 12, 2026

Summary

Fixes #1287

The tState.with() method in adk_api_translator.go mutated shared state instead of creating independent copies. This caused two bugs:

  1. Depth counter counted siblings instead of nesting depth: Each call to with() incremented the same depth field on the shared tState, so an agent with 11+ flat sibling agent tools would hit the MAX_DEPTH=10 recursion limit even though the actual nesting depth was only 1.

  2. 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 new tState with an independent depth counter and a copied visitedAgents slice.

Test Cases Added

  • Flat list of 12 agent tools (all leaf agents) — PASSES (was failing before fix)
  • Deep nesting of 10 levels — PASSES (within limit)
  • Deep nesting of 12 levels — correctly FAILS with recursion error
  • True cycle A->B->A — correctly FAILS with cycle detection
  • Diamond pattern A->B, A->C, B->D, C->D — PASSES (shared agent D is not a cycle)

Test plan

  • All new test cases pass
  • All existing tests pass (golden test failures are pre-existing on main, unrelated to this change)
  • Diff is minimal — only with() method changed + tests added

Copilot AI review requested due to automatic review settings February 12, 2026 10:21
@opspawn opspawn requested a review from yuval-k as a code owner February 12, 2026 10:21
Copy link
Contributor

Copilot AI left a 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 new tState with independent depth counter and copied visitedAgents slice
  • 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.

EItanya
EItanya previously approved these changes Feb 12, 2026
opspawn and others added 2 commits February 12, 2026 15:25
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>
@opspawn opspawn force-pushed the fix/1287-recursion-limit-flat-tools branch from 7220ca2 to 7d98e36 Compare February 12, 2026 15:25
@antonioberben
Copy link
Contributor

antonioberben commented Feb 12, 2026

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!

@EItanya EItanya merged commit a7fe733 into kagent-dev:main Feb 12, 2026
16 checks passed
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.

[BUG] Recursion limit reached with flat agent tool list

3 participants