Skip to content

feat: route orchestration decisions through ai - #9

Merged
xshsama merged 1 commit into
mainfrom
xshsama/dev
Jun 4, 2026
Merged

xshsama merged 1 commit into
mainfrom
xshsama/dev

Conversation

@xshsama

@xshsama xshsama commented Jun 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

Release Notes

  • New Features
    • CLI detect command now displays routing metadata, including routing decision source and optional reasoning.
    • System now includes intelligent AI-based routing to automatically determine whether to use single-agent or multi-agent execution modes, enabled by default with configurable timeout.

Copilot AI review requested due to automatic review settings June 4, 2026 07:33
@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b996e5c7-3261-41c4-814e-da344ef3661d

📥 Commits

Reviewing files that changed from the base of the PR and between 1e5e047 and 0de1866.

📒 Files selected for processing (5)
  • ai_collab/cli.py
  • ai_collab/core/config.py
  • ai_collab/core/detector.py
  • config/config.template.json
  • tests/test_detector.py

📝 Walkthrough

Walkthrough

This PR adds an AI routing subsystem to the detector that consults an external provider to decide multi-vs-single agent execution, optionally overriding planner decisions, and tracks routing provenance through result metadata and CLI output across all detection paths.

Changes

AI Routing and Detector Enhancement

Layer / File(s) Summary
Configuration and Result Schema
ai_collab/core/config.py, config/config.template.json, ai_collab/core/detector.py
ai_routing configuration block added to defaults and template with enabled: true, provider: "current", and timeout: 20. CollaborationResult extended with routing_decision_source and routing_decision_reason fields to track decision provenance.
AI Routing Subsystem Implementation
ai_collab/core/detector.py
Imports for subprocess, JSON, and provider CLI resolution. Full AI routing subsystem: enabled/disabled checking, CLI invocation with timeout, prompt construction from task/categories/agents/plan, JSON extraction and parsing from noisy output, field normalization, and plan transformation helpers (_collapse_to_single_agent_plan(), _ensure_multi_agent_plan(), _choose_ai_reviewer()).
Detection Pipeline Integration
ai_collab/core/detector.py
detect() method refactored to optionally invoke _decide_route_with_ai(), compute routing decision source and reason based on AI availability and validity, handle AI-aware low-signal fallback, and normalize intent/trigger from either AI outputs or planner-derived inference. Early returns updated to include routing provenance.
Plan Transformation and Agent Selection
ai_collab/core/detector.py
AI-decision-driven plan shaping applies single-agent collapse or multi-agent ensure based on AI execution_mode. Primary/reviewer selection in multi-agent branch prefers AI fields when present, otherwise falls back to planner-derived selection. Multi-agent ensuring reapplied in fullstack promotion path.
Routing Metadata Propagation
ai_collab/core/detector.py
routing_decision_source and routing_decision_reason included in CollaborationResult across all return paths: planner multi-agent branch, fallback multi-agent branch, no-collaboration cases, and multi-collaboration construction.
CLI Output and Test Coverage
ai_collab/cli.py, tests/test_detector.py
detect command prints routing decision source and optional reason in both multi-AI and single-AI branches. New test (test_ai_routing_can_force_multi_agent_review) verifies AI routing can override planner to force multi-agent execution with expected agents and quality-review step.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 A judge arrives with routing might,
To steer the AI dance just right—
Ask the provider, parse its plea,
Transform the plan in harmony!
Single or many, the choice is wise,
hop hop toward better skies! 🌟

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch xshsama/dev

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@xshsama
xshsama merged commit d270bdb into main Jun 4, 2026
1 of 6 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces an AI-assisted routing layer for collaboration detection, allowing an LLM to override the planner’s initial single-/multi-agent decision and emitting metadata about how and why a routing decision was made.

Changes:

  • Add ai_routing configuration (template + defaults) to enable AI-driven routing decisions with a configurable provider/timeout.
  • Extend CollaborationDetector to call a provider CLI for a compact JSON routing decision and apply it to the orchestration plan (including forcing single- vs multi-agent).
  • Surface routing decision provenance (routing_decision_source / routing_decision_reason) in both the CLI output and tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
ai_collab/core/detector.py Adds AI routing decision execution/parsing and threads decision metadata through CollaborationResult.
ai_collab/core/config.py Adds ai_routing to the default auto-collaboration configuration.
config/config.template.json Exposes auto_collaboration.ai_routing in the user-facing config template.
ai_collab/cli.py Prints routing decision source/reason in ai-collab detect output.
tests/test_detector.py Adds a unit test asserting AI routing can force a multi-agent review plan.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +664 to +681
start = raw.find("{")
while start >= 0:
depth = 0
for index in range(start, len(raw)):
char = raw[index]
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
candidate = raw[start : index + 1]
try:
value = json.loads(candidate)
except json.JSONDecodeError:
break
return value if isinstance(value, dict) else None
start = raw.find("{", start + 1)
return None
Comment on lines +124 to +128
"ai_routing": {
"enabled": true,
"provider": "current",
"timeout": 20
},
Comment thread ai_collab/core/config.py
Comment on lines 145 to +153
def default_auto_collaboration_config() -> Dict[str, Any]:
return {
"enabled": True,
"auto_orchestration_enabled": True,
"ai_routing": {
"enabled": True,
"provider": "current",
"timeout": 20,
},
Comment thread tests/test_detector.py
Comment on lines +171 to +203
def test_ai_routing_can_force_multi_agent_review(config, monkeypatch):
"""AI routing should be able to override a single-agent planner candidate."""
_mock_profile(monkeypatch, categories=["systems-tooling"])
config.auto_collaboration["ai_routing"] = {
"enabled": True,
"provider": "current",
"timeout": 3,
}

def fake_run(*_args, **_kwargs):
return SimpleNamespace(
returncode=0,
stdout=(
'{"execution_mode":"multi-agent","intent":"documentation",'
'"trigger":"docs-writing","primary_agent":"codex",'
'"reviewer_agents":["claude"],"reason":"User asked for independent review."}'
),
stderr="",
)

monkeypatch.setattr("ai_collab.core.detector.subprocess.run", fake_run)

detector = CollaborationDetector(config)
result = detector.detect("扫描目录并写 md,最后让 Claude 独立审查", "codex")

assert result.need_collaboration is True
assert result.execution_mode == "multi-agent"
assert result.routing_decision_source == "ai"
assert result.primary == "codex"
assert result.reviewers == ["claude"]
assert {"codex", "claude"}.issubset(set(result.selected_agents))
assert any(step["role"] == "quality-review" for step in result.orchestration_plan)

Comment on lines +558 to +574
timeout = self._ai_route_timeout(ai_cfg)
try:
parts = shlex.split(provider.cli)
except ValueError:
parts = provider.cli.strip().split()
if not parts:
return None

command = resolve_subprocess_command(parts) + [prompt]
try:
completed = subprocess.run(
command,
capture_output=True,
text=True,
timeout=timeout,
check=False,
)
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