Skip to content

feat: add parent agent traversal to getConnectedAgents API#877

Open
Husainbw786 wants to merge 1 commit intotestingfrom
feat/connected-agents-full-flow
Open

feat: add parent agent traversal to getConnectedAgents API#877
Husainbw786 wants to merge 1 commit intotestingfrom
feat/connected-agents-full-flow

Conversation

@Husainbw786
Copy link
Copy Markdown
Collaborator

What changed

The getConnectedAgents API now returns the complete connection flow including both upstream parents and downstream children.

Previous behavior

  • Only traversed downstream (child agents)
  • Missed parent agents that connected to the queried agent

New behavior

  • Finds all parent agents by searching for agents that have the given agent in their connected_agents field
  • Recursively traverses upstream to find all ancestor parents
  • Continues to traverse downstream to find all children (existing logic)
  • Returns a complete bidirectional map where every agent shows both its parentAgents and childAgents

Why

The API was incomplete — it only returned child agents. For a true "connected agents" view, you need to see the full graph: who calls this agent (parents) and who this agent calls (children).

Technical details

  • Added findParentAgents(agentId) — queries both configurationModel and bridgeVersionModel for documents that reference the agent in their connected_agents
  • Added processUpstream() — recursively walks up the parent chain
  • Renamed existing recursion to processDownstream() for clarity
  • Both directions merge into a single agentsMap with no duplicates

Notes

  • No breaking changes — the response shape remains the same (agentsMap keyed by agent ID)
  • Performance consideration: upstream search does a full scan of connected_agents docs per level, which should be fine for typical org sizes

Previously the API only returned child agents (downstream). Now it:
- Finds all parent agents by searching for agents that have the given agent in their connected_agents field
- Recursively traverses upstream to find all ancestor parents
- Continues to traverse downstream to find all child agents
- Returns complete bidirectional connection flow with no agents missed
Copy link
Copy Markdown
Contributor

@windsurf-bot windsurf-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 To request another review, post a new comment with "/windsurf-review".

async function processAgent(agentId, parentIds = [], docType = null) {
if (visited.has(agentId)) {
// Find all agents (bridges and versions) that have the given agentId in their connected_agents
async function findParentAgents(agentId) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The findParentAgents function is missing error handling. If the database queries fail, the error will propagate up and potentially crash the API call. Consider adding try/catch blocks:

Comment on lines +523 to +528
const parentBridges = await configurationModel
.find({
org_id,
$or: [{ "connected_agents": { $exists: true } }]
})
.lean();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query in findParentAgents() currently fetches all documents with a connected_agents field and then filters them in memory. For large organizations with many agents, this could be inefficient.

Consider optimizing with a more targeted query that directly filters for documents containing the target agentId:

Suggested change
const parentBridges = await configurationModel
.find({
org_id,
$or: [{ "connected_agents": { $exists: true } }]
})
.lean();
// Search in bridges (configurationModel)
const parentBridges = await configurationModel
.find({
org_id,
connected_agents: {
$exists: true,
$elemMatch: { $or: [{ version_id: agentId }, { bridge_id: agentId }] }
}
})
.lean();

Comment on lines +543 to +548
const parentVersions = await bridgeVersionModel
.find({
org_id,
$or: [{ "connected_agents": { $exists: true } }]
})
.lean();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the bridge query, this version query could be optimized to directly filter for documents containing the target agentId:

Suggested change
const parentVersions = await bridgeVersionModel
.find({
org_id,
$or: [{ "connected_agents": { $exists: true } }]
})
.lean();
// Search in versions (bridgeVersionModel)
const parentVersions = await bridgeVersionModel
.find({
org_id,
connected_agents: {
$exists: true,
$elemMatch: { $or: [{ version_id: agentId }, { bridge_id: agentId }] }
}
})
.lean();

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