Skip to content

fix: include user/project agents in task(subagent_type) resolution#2974

Open
trafgals wants to merge 4 commits intocode-yeongyu:devfrom
trafgals:fix/user-agents-callable-v2
Open

fix: include user/project agents in task(subagent_type) resolution#2974
trafgals wants to merge 4 commits intocode-yeongyu:devfrom
trafgals:fix/user-agents-callable-v2

Conversation

@trafgals
Copy link
Copy Markdown

@trafgals trafgals commented Mar 31, 2026

Summary

Re-implements the functionality from closed PR #2690 to make user-defined and project-defined agents callable via task(subagent_type=...).

Problem

The original PR #2690 attempted to allow user agents (from ~/.config/opencode/agents/) and project agents (from .claude/agents/) to be callable via task(subagent_type="my-agent"). However, the branch became incompatible with dev due to ongoing refactoring while the PR was open.

Solution

This PR re-implements the feature against current upstream/dev:

  • Import loadUserAgents and loadProjectAgents from claude-code-agent-loader
  • Merge user/project agents with server agents in resolveSubagentExecution()
  • Server agents take precedence on name collisions
  • Primary-mode agents from user/project are filtered out

Changes

  • src/tools/delegate-task/subagent-resolver.ts: Added agent merging logic
  • src/tools/delegate-task/subagent-resolver.test.ts: Added 4 new test cases

Test Results

All 17 tests pass (13 existing + 4 new):

  • User agent resolution from loadUserAgents()
  • Project agent resolution from loadProjectAgents()
  • Server agent precedence over user/project agents with same name
  • Primary-mode agents correctly filtered out

Summary by cubic

Enable task(subagent_type=...) to resolve user and project agents. We merge user (~/.config/opencode/agents/) and project (.claude/agents/) agents with server agents; server wins, project overrides user; ignore primary agents.

  • Bug Fixes

    • Resolve user/project agents via loadUserAgents/loadProjectAgents in resolveSubagentExecution(); enforce server > project > user; filter primary; resolve models.
    • Add 5 tests covering user agent, project agent, server precedence, project-overrides-user, and primary filtering.
  • Refactors

    • Switch tests to use spyOn for agent loader mocks instead of vi.mock (bun:test best practice).

Written for commit 5441c84. Summary will update on new commits.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 31, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files

Confidence score: 3/5

  • There is a concrete behavior risk in src/tools/delegate-task/subagent-resolver.ts: same-named project agents are not overriding user agents, so task(subagent_type=...) can resolve the wrong target in shadowing repositories.
  • Given the 6/10 severity with high confidence (9/10), this is more than a cosmetic issue and could cause user-visible misrouting of delegated tasks, so merge risk is moderate rather than minimal.
  • Pay close attention to src/tools/delegate-task/subagent-resolver.ts - merge-order precedence needs to ensure project agents win over same-named user agents.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/tools/delegate-task/subagent-resolver.ts">

<violation number="1" location="src/tools/delegate-task/subagent-resolver.ts:95">
P2: Project agents should override same-named user agents here; the current merge order makes `task(subagent_type=...)` resolve the wrong agent in repositories that shadow a global agent.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/tools/delegate-task/subagent-resolver.test.ts">

<violation number="1" location="src/tools/delegate-task/subagent-resolver.test.ts:652">
P2: This test doesn’t actually verify project-over-user precedence. Both candidates are named `my-custom-agent`, and the only assertion checks the shared name, so the test will pass even if the resolver picks the user agent. Add an assertion on a distinguishing field (like the resolved model) to make the precedence check meaningful.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Auto-approved: Logic correctly implements agent precedence (Server > Project > User) and case-insensitive matching, matching existing behavior while adding tested support for local agents.

@trafgals
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

trafgals added 3 commits April 3, 2026 11:51
Re-implement PR code-yeongyu#2690 against current upstream/dev since original
branch became incompatible with codebase refactoring.

Changes:
- Import loadUserAgents and loadProjectAgents from claude-code-agent-loader
- Merge user/project agents with server agents in resolveSubagentExecution()
- Server agents take precedence on name collisions
- Primary-mode agents from user/project are filtered out

Tests: 4 new test cases covering user agent resolution, project
agent resolution, server precedence, and primary agent filtering.
Address PR review comment: project agents should take precedence
over user agents when both define the same agent name.
Previous fix only changed comments, not logic. Now project agents are
added to the map before user agents, so project takes precedence.
@trafgals trafgals force-pushed the fix/user-agents-callable-v2 branch from 851469b to 6440882 Compare April 3, 2026 01:51
@trafgals
Copy link
Copy Markdown
Author

trafgals commented Apr 3, 2026

I have read the CLA Document and I hereby sign the CLA

Copy link
Copy Markdown
Owner

@code-yeongyu code-yeongyu left a comment

Choose a reason for hiding this comment

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

QA Results

Tests: 18 pass, 0 fail (bun test src/tools/delegate-task/subagent-resolver.test.ts)

All 18 tests pass including the 4 new test cases:

  • User agent resolution from loadUserAgents()
  • Project agent resolution from loadProjectAgents()
  • Server agent precedence over user/project agents with same name
  • Primary-mode agents correctly filtered out

Code review notes:

  • The bun.lock diff bumps all platform binaries from 3.11.0 → 3.14.0 which is noisy but harmless (rebased on newer dev)
  • Agent merging logic with proper precedence (server > project > user) is clean
  • Case-insensitive matching via .toLowerCase() is a nice touch

One minor concern: this PR uses vi.mock (vitest syntax) in bun:test. It works in bun but isn't idiomatic — the rest of the test suite uses spyOn. Not a blocker.

Use spyOn which is more idiomatic for bun:test, as suggested in PR review.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

0 issues found across 1 file (changes from recent commits).

Requires human review: Logic for agent merging is sound, but the bun.lock update includes binary dependency bumps (3.11 to 3.14) which cannot be guaranteed 100% regression-free without further context.

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