Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

When using a specific AI coding agent, the agent itself reads its own configuration files. This adds a target agent feature to reduce context size and token usage by excluding the target agent's own paths while including rules from other agents and generic rules. Uses a typed Agent enum for better type safety and maintainability.

Changes

New -a flag

  • Accepts a single agent name: cursor, opencode, copilot, claude, gemini, augment, windsurf, codex
  • Specifies which agent you're using (excludes that agent's own paths, includes other agents and generic rules)
  • Case-sensitive validation with helpful error messages showing all supported agents
  • Automatically adds the agent name as a selector for rule filtering

Implementation

  • pkg/codingcontext/agent.go: Typed Agent enum with constants (AgentCursor, AgentOpenCode, etc.) for type safety
  • ParseAgent() function validates input (case-sensitive, no trimming) and provides helpful error messages
  • agentPathPatterns map defines path patterns for each agent (.cursor/, .opencode/agent, AGENTS.md for codex, etc.)
  • TargetAgent type is a simple string type (empty string means no agent specified)
  • pkg/codingcontext/context.go: Uses WithAgent() option to configure target agent; filters paths in findExecuteRuleFiles() and ruleFileWalker() to exclude only the target agent's own paths
  • Target agent is automatically added as a selector, allowing generic rules to self-filter with agent: cursor in frontmatter
  • Exclusions apply to both local and remote rule sources
  • Generic .agents/rules paths are never excluded
  • AllAgents() and error messages dynamically generated from agentPathPatterns map for easier maintenance

How it works:
When you specify -a cursor:

  1. Cursor's own paths are excluded (.cursor/, .cursorrules) - Cursor reads these itself
  2. Other agents' paths are included (.opencode/, .github/copilot-instructions.md, etc.)
  3. Generic rules from .agents/rules/ are included
  4. The agent name is added as a selector (agent: cursor)
  5. Generic rules can filter themselves with agent: cursor in frontmatter to be cursor-specific

Tests

  • 45+ unit tests covering Agent type, parsing, pattern matching, and edge cases
  • Integration test verifying end-to-end filtering across target agents with selector-based filtering

Example

# Using Cursor - excludes .cursor/ and .cursorrules, includes other agents
coding-context -a cursor fix-bug

# Generic rule can filter for cursor:
# ---
# agent: cursor
# ---
# Cursor-specific generic rule content

# Invalid agent name provides helpful error
coding-context -a invalid test
# Error: unknown agent: invalid (supported: cursor, opencode, copilot, claude, gemini, augment, windsurf, codex)

Token reduction: 18-61% depending on agent-specific rule density.

Benefits

  • Avoid duplication: The target agent reads its own config, so exclude it from the context
  • Cross-agent rules: Include rules from other agents that might be relevant
  • Type safety: Compile-time checking prevents typos in agent names
  • Better UX: Clear error messages show all supported agents
  • Maintainable: Adding new agents only requires updating the agentPathPatterns map
  • Self-documenting: AllAgents() provides programmatic access to supported agents
  • Automatic selector: Agent name is added as a selector for fine-grained rule filtering in generic rules
  • Simple design: TargetAgent is a string type where empty string means no agent specified
Original prompt

When using a particular agent (e.g. cursor or opencode) I don't need to include prompts for other agents. Add an option to exculde by CLI name.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 21, 2025 01:05
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copilot AI changed the title [WIP] Add option to exclude agents by CLI name Add -x flag to exclude agent-specific rules by CLI name Nov 21, 2025
Copilot AI requested a review from alexec November 21, 2025 01:10
@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot lets have a type for the agent

  • Cursor
  • GitHub
  • OpenCode
  • Codex
  • etc

Don't call in "WithExclude", instead "WithAgent".

…hExcludes to WithAgent

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot lets have a type for the agent

  • Cursor
  • GitHub...

Done! Refactored to use a typed Agent enum (Cursor, OpenCode, Copilot, Claude, Gemini, Augment, Windsurf, Codex) and renamed WithExcludes to WithAgent. The Agent type provides better type safety, validation, and error messages. See commit c43cf01.

…ally from agentPathPatterns

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copilot AI changed the title Add -x flag to exclude agent-specific rules by CLI name Add -x flag to exclude agent-specific rules using typed Agent enum Nov 21, 2025
@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot Use "-a" rather than "-x"


// ParseAgent parses a string into an Agent type
func ParseAgent(s string) (Agent, error) {
normalized := Agent(strings.ToLower(strings.TrimSpace(s)))
Copy link
Contributor

@alexec alexec Nov 21, 2025

Choose a reason for hiding this comment

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

@copilot Don't be case-insensitive, don't trim spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed case-insensitive normalization and trimming from ParseAgent. See commit 7566314.

@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot it is probably worthwhile noting that you exclude the rules, because you are going to use that agent.

The agent name should automatically be added as a "selector".

Lets not call in "exclude", "target agent".

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot Use "-a" rather than "-x"

Changed flag from -x to -a. See commit c18419c.

Help output showing -a flag

Copilot AI changed the title Add -x flag to exclude agent-specific rules using typed Agent enum Add -a flag to exclude agent-specific rules using typed Agent enum Nov 21, 2025
Copilot AI requested a review from alexec November 21, 2025 01:43
…dex patterns

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot it is probably worthwhile noting that you exclude the rules, because you are going to use that agent.

The agent name should automatically be added as a "selector".
...

Refactored from "exclude" to "target agent" concept. When you specify -a cursor, it includes cursor-specific rules and automatically excludes all other agents' rules. The agent name is also automatically added as a selector so rules can filter themselves with agent: cursor in frontmatter. See commit 89610b0.

@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot sorry - the target is the agent you plan to use the rules with. The result should exclude rules for that agent based on path, but include generic rules based on frontmatter selector.

Copilot AI changed the title Add -a flag to exclude agent-specific rules using typed Agent enum Add -a flag to specify target agent (excludes other agents' rules) Nov 21, 2025
…les with selectors

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot sorry - the target is the agent you plan to use the rules with. The result should exclude rules for that agent based on path, but include generic rules based on frontmatter selector.

Fixed! Now when you specify a target agent (e.g., -a cursor), ALL agent-specific paths are excluded (including the target agent's own paths like .cursor/rules). The target agent uses generic rules from .agents/rules/, which can filter themselves with agent: cursor in frontmatter. See commit 7c8ade6.

Copilot AI changed the title Add -a flag to specify target agent (excludes other agents' rules) Add -a flag to specify target agent (excludes all agent-specific paths) Nov 21, 2025
@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot Sorry, I meant all rules that that agent itself would use, e.g. "cursor" should not include ".cursorrules" but should include opencode and .agents.

…ric rules

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot Sorry, I meant all rules that that agent itself would use, e.g. "cursor" should not include ".cursorrules" but should include opencode and .agents.

Fixed! Now -a cursor excludes only cursor's own paths (.cursor/, .cursorrules) but includes rules from other agents (opencode, copilot, etc.) and generic rules. The target agent reads its own config, so we don't duplicate it. See commit b5bd2be.

Copilot AI changed the title Add -a flag to specify target agent (excludes all agent-specific paths) Add -a flag to specify target agent (excludes target agent's own paths) Nov 21, 2025
@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot TargetAgent to string, empty string means no agent specified

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
@alexec
Copy link
Contributor

alexec commented Nov 21, 2025

@copilot Try again

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot Try again

Done! TargetAgent is now a simple string type where an empty string means no agent is specified. See commit b6c2ca3.

The type now works as:

  • TargetAgent is type TargetAgent string
  • Empty string "" = no agent specified
  • IsSet() method checks if agent is set
  • Simpler API without pointer indirection

@alexec alexec marked this pull request as ready for review November 21, 2025 03:38
@alexec alexec merged commit a73798f into main Nov 21, 2025
2 checks passed
@alexec alexec deleted the copilot/add-option-to-exclude-agent branch November 21, 2025 03:40
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