Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/agents/base_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ export abstract class BaseAgent {

/**
* Root agent of this agent.
* Computed dynamically by traversing up the parent chain.
*/
readonly rootAgent: BaseAgent;
get rootAgent(): BaseAgent {
return getRootAgent(this);
}

/**
* The parent agent of this agent.
Expand Down Expand Up @@ -143,7 +146,6 @@ export abstract class BaseAgent {
this.description = config.description;
this.parentAgent = config.parentAgent;
this.subAgents = config.subAgents || [];
this.rootAgent = getRootAgent(this);
this.beforeAgentCallback = getCannonicalCallback(
config.beforeAgentCallback,
);
Expand Down
43 changes: 43 additions & 0 deletions core/test/agents/base_agent_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {LlmAgent} from '@google/adk';

describe('BaseAgent', () => {
describe('rootAgent', () => {
it('should return the actual root agent for sub-agents', () => {
const subAgent = new LlmAgent({
name: 'sub_agent',
description: 'A sub agent',
});

const rootAgent = new LlmAgent({
name: 'root_agent',
description: 'The root agent',
subAgents: [subAgent],
});

expect(subAgent.rootAgent).toBe(rootAgent);
expect(rootAgent.rootAgent).toBe(rootAgent);
});

it('should traverse multiple levels of nesting', () => {
const leafAgent = new LlmAgent({name: 'leaf_agent'});
const middleAgent = new LlmAgent({
name: 'middle_agent',
subAgents: [leafAgent],
});
const rootAgent = new LlmAgent({
name: 'root_agent',
subAgents: [middleAgent],
});

expect(leafAgent.rootAgent).toBe(rootAgent);
expect(middleAgent.rootAgent).toBe(rootAgent);
expect(rootAgent.rootAgent).toBe(rootAgent);
});
});
});
Loading