Commit 41f637d
feat: KG-driven model routing with provider probing (#761)
* feat: add KG model routing with action directive
Create 10 ADF routing rule markdown files with route/action/priority/
synonyms directives for KG-based agent dispatch. Add action:: directive
to RouteDirective for CLI command templates. Support multiple route/action
pairs per file with backward-compatible route field.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: add KG router module to orchestrator
KgRouter loads routing rules from markdown taxonomy directory,
builds thesaurus from synonyms, and uses terraphim_automata::find_matches
for Aho-Corasick pattern matching against agent task descriptions.
Returns KgRouteDecision with provider, model, action template, confidence,
and ordered fallback routes. Supports health-aware fallback via
first_healthy_route() and template rendering via render_action().
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: wire KG routing and provider health into agent dispatch
Add provider_probe.rs with ProviderHealthMap using CircuitBreaker from
terraphim_spawner::health. Probes CLI tools via action:: templates from
KG rules, measures latency, saves pi-benchmark compatible JSON results.
Wire KG router into spawn_agent(): KG routing tried first (Aho-Corasick
synonym match), with health-aware fallback skipping unhealthy providers.
Falls back to existing keyword RoutingEngine when no KG match found.
Add [routing] config section to OrchestratorConfig with taxonomy_path,
probe_ttl_secs, probe_results_dir, and probe_on_startup fields.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: add hot-reload for KG routing rules via mtime detection
KgRouter now tracks the latest mtime of .md files in the taxonomy
directory. reload_if_changed() compares current mtime against cached
value and rebuilds the Aho-Corasick automaton if files have been
modified. Called on the orchestrator's reconciliation tick for
zero-restart routing updates.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: use std::io::Error::other and add V-model report
Fix D-1: replace deprecated std::io::Error::new(ErrorKind::Other, e)
with std::io::Error::other(e) in provider_probe.rs.
Add verification and validation report from V-model right-side review.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: wire all V-model defects D-2 through D-5
D-2: probe_all() called on startup when probe_on_startup=true, and
re-probed in reconcile_tick when cached results expire (TTL-based).
Saves JSON results to configured probe_results_dir.
D-3: ExitClassifier ModelError/RateLimit feeds record_failure() into
provider circuit breaker. Success/EmptySuccess feeds record_success().
D-4: reload_if_changed() called every reconcile_tick, checks mtime
of markdown files and rebuilds Aho-Corasick automaton if changed.
D-5: Use sh -c for action template execution instead of
split_whitespace, matching CommandStep::Shell pattern in tinyclaw.
Handles quoted arguments correctly.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: use bash -lc for probe execution to pick up user PATH
The probe's sh -c doesn't have ~/.local/bin, ~/.bun/bin, ~/.cargo/bin
on PATH where opencode and claude live. Use bash -lc (login shell)
to source the user profile, matching the systemd ExecStart pattern.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: prepend tool dirs to PATH instead of login shell
Replace bash -lc (which fails if .profile has errors) with bash -c
plus explicit PATH prepend of ~/.local/bin, ~/.bun/bin, ~/bin,
~/.cargo/bin, ~/go/bin. Avoids broken .profile sourcing while
ensuring CLI tools are discoverable.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: correct action templates for opencode and claude CLIs
opencode requires 'run -m provider/model "prompt"' syntax.
All action templates now use {{ model }} placeholder from route
directive instead of hardcoding model names.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: use correct CLI paths and subscription model names
Use absolute paths for opencode (/home/alex/.bun/bin/opencode) and
claude (/home/alex/.local/bin/claude). Add --format json to opencode.
Replace pay-per-use opencode/ models with subscription providers:
gpt-5-nano -> opencode-go/minimax-m2.5, minimax-m2.5-free ->
minimax-coding-plan/MiniMax-M2.5.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* test: add integration test loading real ADF taxonomy
Validates 10 rules loaded, every route has action:: template,
security_audit matches cargo audit/CVE, reasoning has priority 80,
and multi-route fallback chains are present.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* test: e2e routing for all 12 ADF agents
Add e2e test verifying every ADF agent routes to expected provider+model
via KG synonym matching. Fix multi-line synonyms: parser requires
synonyms:: prefix on each line. All 12 agents route correctly.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: add zai and openai as fallback providers
Expand all 10 routing rules from 2 to 4 routes each:
- Coding tasks: +zai-coding-plan/glm-5-turbo +openai/gpt-5.3-codex
- Reasoning tasks: +zai-coding-plan/glm-5 +openai/gpt-5.4
- Documentation/cost: +zai-coding-plan/glm-5-turbo +openai/gpt-5.4-mini
All subscription providers only (no opencode/ pay-per-use prefix).
E2e test updated: 12/12 agents route correctly with 4 fallbacks.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: probe results override circuit breaker for health decisions
Probe timeout/error marks provider unhealthy immediately, not after
5 failures. Probe success is authoritative over circuit breaker state.
Mixed results: if ANY model succeeds for a provider, provider is healthy.
This fixes the bug where kimi timed out in probe (30s) but was still
selected as primary because circuit breaker threshold wasn't reached.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: phase-aware 3-tier model routing
Replace 10 category-based routing files with 3 tier files:
- planning_tier.md (pri=80): opus for strategic planning, architecture
- review_tier.md (pri=60): haiku for verification, validation, compliance
- implementation_tier.md (pri=50): sonnet for coding, testing, security
KG routing now takes priority over static model config in spawn_agent.
Phase keywords in task text determine tier, not agent name.
E2e test: 13/13 agents route to correct tier:
- 2 agents -> PLANNING (opus): meta-coordinator, product-development
- 5 agents -> REVIEW (haiku): spec-validator, quality-coord, compliance,
drift-detector, merge-coordinator
- 6 agents -> IMPLEMENTATION (sonnet): security-sentinel, test-guardian,
implementation-swarm, documentation-gen, browser-qa, log-analyst
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: override CLI tool from KG action template
When KG tier routing selects a model that uses a different CLI than
the agent's static cli_tool (e.g., claude instead of opencode),
extract the CLI path from the action:: template and use it for the
Provider construction. This enables seamless routing across CLI tools.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: increase probe timeout to 60s for opencode agent lifecycle
opencode run completes in ~11s but the full agent lifecycle (init,
step_start, tool_use, step_finish, next_step, session_end) can take
longer under load. 30s was too tight causing false-positive timeouts
for kimi provider. Increase to 60s to match actual completion time.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: tighten planning tier synonyms to prevent false escalation
Remove ambiguous words (specification, research, design the, blueprint,
triage, risk assessment) that appear in issue bodies and cause review
agents to escalate to opus. Keep only unambiguous planning phrases
like 'create a plan', 'architecture design', 'strategic planning'.
Fixes quality-coordinator being routed to opus when reviewing an issue
whose body contained planning language.
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* feat: git worktree isolation for implementation-tier agents
Each non-review agent gets its own git worktree in /tmp/adf-worktrees/
before spawning. Review-tier agents (haiku) skip isolation since they
are read-only. Worktrees are cleaned up after agent exit.
Flow: create_agent_worktree() -> spawn with worktree as working_dir ->
try_commit_agent_work(worktree) -> remove_agent_worktree()
Prevents concurrent agents from corrupting each other's working tree.
Fail-open: if worktree creation fails, agent uses shared working_dir.
Fixes #246 Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
* fix: resolve clippy needless_borrow on save_results path
Refs #400
Co-Authored-By: Terraphim AI <noreply@terraphim.ai>
---------
Co-authored-by: Terraphim CI <alex@terraphim.ai>
Co-authored-by: Terraphim AI <noreply@terraphim.ai>1 parent af451ec commit 41f637d
12 files changed
Lines changed: 1965 additions & 54 deletions
File tree
- .docs
- crates
- terraphim_automata/src
- terraphim_orchestrator
- src
- tests
- terraphim_types/src
- docs/taxonomy/routing_scenarios/adf
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| |||
412 | 412 | | |
413 | 413 | | |
414 | 414 | | |
415 | | - | |
416 | | - | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
417 | 461 | | |
418 | 462 | | |
419 | | - | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
420 | 469 | | |
421 | 470 | | |
422 | | - | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
423 | 474 | | |
424 | 475 | | |
425 | 476 | | |
| |||
440 | 491 | | |
441 | 492 | | |
442 | 493 | | |
443 | | - | |
444 | | - | |
| 494 | + | |
| 495 | + | |
445 | 496 | | |
446 | 497 | | |
447 | | - | |
448 | | - | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
449 | 533 | | |
450 | 534 | | |
451 | | - | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
452 | 547 | | |
453 | 548 | | |
454 | | - | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
455 | 555 | | |
456 | 556 | | |
457 | 557 | | |
| |||
487 | 587 | | |
488 | 588 | | |
489 | 589 | | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
496 | 595 | | |
497 | 596 | | |
498 | 597 | | |
| |||
0 commit comments