Skip to content

[routing] Negative routing — tool exclusion by ID and tag #112

@dgenio

Description

@dgenio

Problem

In multi-turn conversations, agents sometimes re-call the same tool or route to inappropriate tools for the current phase. There is no way to express "don't route to X" or "exclude tools matching tag Y". This leads to redundant tool calls and wasted tokens.

For example, after successfully calling db_query, the agent might route to it again on the next turn instead of moving to send_email. Exclusion patterns prevent these loops.

Proposal

Add exclusion parameters to Router.route():

result = router.route(
    query="send the results",
    exclude_ids={"db_query", "db_schema"},          # Exclude specific tools
    exclude_tags={"database"},                       # Exclude all tools with tag
)

Implementation

  1. Router.route() signature — add exclude_ids: set[str] | None and exclude_tags: set[str] | None
  2. Pre-filter — before beam search, mark excluded leaf nodes as unreachable
  3. Pruning — if all children of an interior node are excluded, prune the parent too
  4. RouteResult — include excluded_count: int showing how many tools were filtered

Constraints

  • Exclusions are applied before scoring (not post-filter)
  • If all tools are excluded, raise RouteError with descriptive message
  • Empty exclude sets = no filtering (backward-compatible)

Acceptance Criteria

  • Router.route() accepts exclude_ids and exclude_tags parameters
  • Excluded tools never appear in route results
  • Interior nodes with all-excluded children are pruned
  • RouteError raised if all tools are excluded
  • Empty exclusions produce identical results to current behavior
  • RouteResult reports excluded count
  • Tests cover ID exclusion, tag exclusion, full-prune edge case

File Paths

  • src/contextweaver/routing/router.py (edit)
  • tests/test_router.py (edit)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/routingRouting engine: catalog, graph, router, cardscomplexity/simpleStraightforward change, minimal riskenhancementNew feature or requestmilestone/v0.3v0.3 — Production readinesspriority/mediumMedium priority — production readiness

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions