Skip to content

Commit 8b9957f

Browse files
committed
Generalize smart-routing engine (rename from intelligent routing)
Refactor the Codex smart-routing internals into a harness-agnostic core so a second agent can reuse them, and rename the feature "intelligent routing" -> "smart routing" throughout (package dir smart_routing/, symbols, CLI flags --enable/--disable-smart-routing, hidden codex-router-hook, user-facing strings) per naming guidance. - smart_routing/routing.py + hooks.py: shared routes:select call, decision shape, canary/audit bookkeeping, and hook add/remove machinery. - smart_routing/codex_routing.py + codex_hooks.py delegate to the shared core (behavior unchanged). - State key is the agent-neutral "smart_routing_enabled" so a later agent can share one opt-in. Co-authored-by: Isaac
1 parent e26479b commit 8b9957f

12 files changed

Lines changed: 641 additions & 500 deletions

File tree

src/ucode/agents/codex.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
build_tool_base_url,
2121
get_databricks_token,
2222
)
23-
from ucode.intelligent_routing.codex_hooks import (
24-
remove_intelligent_routing_hooks,
25-
sync_intelligent_routing_hooks,
26-
)
2723
from ucode.launcher import exec_or_spawn
24+
from ucode.smart_routing.codex_hooks import (
25+
remove_smart_routing_hooks,
26+
sync_smart_routing_hooks,
27+
)
2828
from ucode.state import mark_tool_managed, save_state
2929
from ucode.telemetry import agent_version, ucode_version
3030

@@ -39,7 +39,9 @@
3939
MINIMUM_CODEX_VERSION_TEXT = "0.134.0"
4040
MINIMUM_ROUTING_CODEX_VERSION = (0, 145, 0)
4141
MINIMUM_ROUTING_CODEX_VERSION_TEXT = "0.145.0"
42-
INTELLIGENT_ROUTING_STATE_KEY = "codex_intelligent_routing_enabled"
42+
# Shared across agents: one opt-in enables smart routing for every routing-capable
43+
# tool (codex, claude), so a workspace turns it on once.
44+
SMART_ROUTING_STATE_KEY = "smart_routing_enabled"
4345

4446

4547
SPEC: ToolSpec = {
@@ -325,10 +327,9 @@ def write_tool_config(state: dict, model: str | None = None, provider: str | Non
325327
databricks_profile = state.get("profile")
326328

327329
if _use_legacy_layout():
328-
if intelligent_routing_enabled(state) and provider is None:
330+
if smart_routing_enabled(state) and provider is None:
329331
raise RuntimeError(
330-
"Codex intelligent routing requires Codex "
331-
f"{MINIMUM_ROUTING_CODEX_VERSION_TEXT} or newer."
332+
f"Codex smart routing requires Codex {MINIMUM_ROUTING_CODEX_VERSION_TEXT} or newer."
332333
)
333334
# Codex < 0.134.0 only reads ~/.codex/config.toml. Write the shared
334335
# config with [profiles.ucode] + shared [model_providers.ucode-databricks]
@@ -370,10 +371,10 @@ def write_tool_config(state: dict, model: str | None = None, provider: str | Non
370371
# deep_merge can't drop keys, so clear a `model` pinned by an earlier
371372
# non-provider run that the provider overlay omits.
372373
doc.pop("model", None)
373-
sync_intelligent_routing_hooks(
374+
sync_smart_routing_hooks(
374375
doc,
375376
state,
376-
enabled=intelligent_routing_enabled(state) and provider is None,
377+
enabled=smart_routing_enabled(state) and provider is None,
377378
)
378379
write_toml_file(CODEX_CONFIG_PATH, doc)
379380
state = mark_tool_managed(state, "codex", MANAGED_KEYS)
@@ -416,38 +417,38 @@ def launch(state: dict, tool_args: list[str]) -> None:
416417
exec_or_spawn([binary, "--profile", CODEX_PROFILE_NAME, *tool_args])
417418

418419

419-
def intelligent_routing_enabled(state: dict) -> bool:
420+
def smart_routing_enabled(state: dict) -> bool:
420421
"""Return whether the current workspace opted into Codex routing."""
421-
return state.get(INTELLIGENT_ROUTING_STATE_KEY) is True
422+
return state.get(SMART_ROUTING_STATE_KEY) is True
422423

423424

424-
def enable_intelligent_routing(state: dict) -> dict:
425-
"""Persist the current workspace's Codex intelligent-routing opt-in."""
425+
def enable_smart_routing(state: dict) -> dict:
426+
"""Persist the current workspace's Codex smart-routing opt-in."""
426427
parsed = _parse_version(agent_version(SPEC["binary"]))
427428
if parsed is not None and parsed < MINIMUM_ROUTING_CODEX_VERSION:
428429
raise RuntimeError(
429-
"Codex intelligent routing requires Codex "
430+
"Codex smart routing requires Codex "
430431
f"{MINIMUM_ROUTING_CODEX_VERSION_TEXT} or newer; found "
431432
f"{agent_version(SPEC['binary'])}."
432433
)
433-
state[INTELLIGENT_ROUTING_STATE_KEY] = True
434+
state[SMART_ROUTING_STATE_KEY] = True
434435
return state
435436

436437

437-
def disable_intelligent_routing(state: dict) -> bool:
438+
def disable_smart_routing(state: dict) -> bool:
438439
"""Disable routing and remove only ucode's Codex routing hooks."""
439-
state.pop(INTELLIGENT_ROUTING_STATE_KEY, None)
440+
state.pop(SMART_ROUTING_STATE_KEY, None)
440441
if state.get("workspace"):
441442
save_state(state)
442443
changed = False
443444
for path in (CODEX_CONFIG_PATH, LEGACY_CODEX_CONFIG_PATH):
444445
if not path.exists():
445446
continue
446447
doc = read_toml_safe(path)
447-
if remove_intelligent_routing_hooks(doc):
448+
if remove_smart_routing_hooks(doc):
448449
write_toml_file(path, doc)
449450
changed = True
450-
from ucode.intelligent_routing.codex_routing import clear_routing_artifacts
451+
from ucode.smart_routing.codex_routing import clear_routing_artifacts
451452

452453
clear_routing_artifacts()
453454
return changed

src/ucode/cli.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
launch as launch_agent,
3131
)
3232
from ucode.agents.codex import (
33-
disable_intelligent_routing,
34-
enable_intelligent_routing,
35-
intelligent_routing_enabled,
33+
disable_smart_routing,
34+
enable_smart_routing,
3635
revert_legacy_shared_config,
36+
smart_routing_enabled,
3737
)
3838
from ucode.agents.pi import PI_SETTINGS_BACKUP_PATH, PI_SETTINGS_PATH
3939
from ucode.config_io import restore_file, set_dry_run
@@ -58,7 +58,6 @@
5858
resolve_pat_token,
5959
run_databricks_login,
6060
)
61-
from ucode.intelligent_routing.codex_routing import route_launch_model
6261
from ucode.mcp import (
6362
MCP_CLIENTS,
6463
SKILLS_MCP_KIND,
@@ -68,6 +67,7 @@
6867
revert_mcp_configs,
6968
)
7069
from ucode.skills_download import configure_skills_download_command
70+
from ucode.smart_routing.codex_routing import route_launch_model
7171
from ucode.state import (
7272
STATE_PATH,
7373
clear_state,
@@ -967,11 +967,11 @@ def codex_router_hook_cmd(
967967
use_pat: Annotated[bool, typer.Option("--use-pat")] = False,
968968
model: Annotated[list[str] | None, typer.Option("--model")] = None,
969969
) -> None:
970-
"""Run a Codex intelligent-routing lifecycle hook."""
970+
"""Run a Codex smart-routing lifecycle hook."""
971971
import json
972972
import sys
973973

974-
from ucode.intelligent_routing.codex_routing import (
974+
from ucode.smart_routing.codex_routing import (
975975
record_session_start,
976976
record_subagent_start,
977977
route_pre_tool_use,
@@ -993,7 +993,7 @@ def codex_router_hook_cmd(
993993
sys.stdout.write(
994994
json.dumps(
995995
{
996-
"systemMessage": "Intelligent Routing verified. "
996+
"systemMessage": "Smart Routing verified. "
997997
f"Subagent is using {record.get('model')}."
998998
}
999999
)
@@ -1002,7 +1002,7 @@ def codex_router_hook_cmd(
10021002
sys.stdout.write(
10031003
json.dumps(
10041004
{
1005-
"systemMessage": "Intelligent Routing mismatch: router requested "
1005+
"systemMessage": "Smart Routing mismatch: router requested "
10061006
f"{record.get('requested_model')}, but Codex started "
10071007
f"{record.get('model')}."
10081008
}
@@ -1073,7 +1073,7 @@ def _launch_tool(
10731073
provider: str | None = None,
10741074
skip_preflight: bool = False,
10751075
workspace: str | None = None,
1076-
enable_codex_intelligent_routing: bool = False,
1076+
enable_codex_smart_routing: bool = False,
10771077
) -> None:
10781078
try:
10791079
tool = normalize_tool(tool_name)
@@ -1097,9 +1097,9 @@ def _launch_tool(
10971097
# An explicit --provider overrides the persisted choice; otherwise fall
10981098
# back to whatever `ucode configure` saved for this tool.
10991099
provider = provider or get_provider_service(state, tool)
1100-
if tool == "codex" and enable_codex_intelligent_routing and provider:
1100+
if tool == "codex" and enable_codex_smart_routing and provider:
11011101
raise RuntimeError(
1102-
"Codex intelligent routing cannot be enabled with --provider. "
1102+
"Codex smart routing cannot be enabled with --provider. "
11031103
"Launch without a Model Provider Service and try again."
11041104
)
11051105
# Validate the provider service before launching — it must exist, be a
@@ -1125,8 +1125,8 @@ def _launch_tool(
11251125
skip_model_discovery=bool(provider),
11261126
skip_preflight=skip_preflight,
11271127
)
1128-
if tool == "codex" and enable_codex_intelligent_routing:
1129-
state = enable_intelligent_routing(state)
1128+
if tool == "codex" and enable_codex_smart_routing:
1129+
state = enable_smart_routing(state)
11301130
if provider:
11311131
# Routing through a Model Provider Service pins no Databricks model;
11321132
# the agent uses its own canonical model names (header selects the
@@ -1135,16 +1135,15 @@ def _launch_tool(
11351135
resolved_model = None
11361136
else:
11371137
state, resolved_model = resolve_launch_model(tool, state, None)
1138-
if tool == "codex" and intelligent_routing_enabled(state):
1139-
with spinner("Selecting a Codex model with intelligent routing..."):
1138+
if tool == "codex" and smart_routing_enabled(state):
1139+
with spinner("Selecting a Codex model with smart routing..."):
11401140
decision, routing_error = route_launch_model(state, ctx.args)
11411141
if decision is not None:
11421142
resolved_model = decision.model
1143-
print_note(f"Using Intelligent Routing. Routing to {resolved_model}.")
1143+
print_note(f"Using Smart Routing. Routing to {resolved_model}.")
11441144
elif routing_error:
11451145
print_warning(
1146-
f"Intelligent routing was unavailable ({routing_error}); "
1147-
f"using {resolved_model}."
1146+
f"Smart routing was unavailable ({routing_error}); using {resolved_model}."
11481147
)
11491148
state = configure_tool(
11501149
tool,
@@ -1159,9 +1158,9 @@ def _launch_tool(
11591158
print_kv("Provider", provider)
11601159
elif resolved_model:
11611160
print_kv("Model", resolved_model)
1162-
if tool == "codex" and intelligent_routing_enabled(state) and not provider:
1163-
print_kv("Intelligent routing", "enabled")
1164-
if enable_codex_intelligent_routing:
1161+
if tool == "codex" and smart_routing_enabled(state) and not provider:
1162+
print_kv("Smart routing", "enabled")
1163+
if enable_codex_smart_routing:
11651164
print_note(
11661165
"Codex requires one-time hook review. Open `/hooks` and trust the "
11671166
"ucode routing hooks if prompted."
@@ -1220,36 +1219,36 @@ def codex_cmd(
12201219
] = None,
12211220
skip_preflight: SkipPreflightOption = False,
12221221
workspace: WorkspaceOption = None,
1223-
enable_intelligent_routing_flag: Annotated[
1222+
enable_smart_routing_flag: Annotated[
12241223
bool,
12251224
typer.Option(
1226-
"--enable-intelligent-routing",
1225+
"--enable-smart-routing",
12271226
help="Enable AI Gateway model routing for Codex sessions and subagents.",
12281227
),
12291228
] = False,
1230-
disable_intelligent_routing_flag: Annotated[
1229+
disable_smart_routing_flag: Annotated[
12311230
bool,
12321231
typer.Option(
1233-
"--disable-intelligent-routing",
1234-
help="Disable intelligent routing and remove ucode's Codex routing hooks.",
1232+
"--disable-smart-routing",
1233+
help="Disable smart routing and remove ucode's Codex routing hooks.",
12351234
),
12361235
] = False,
12371236
) -> None:
12381237
"""Launch Codex via Databricks."""
1239-
if enable_intelligent_routing_flag and disable_intelligent_routing_flag:
1240-
print_err("Use only one of --enable-intelligent-routing or --disable-intelligent-routing.")
1238+
if enable_smart_routing_flag and disable_smart_routing_flag:
1239+
print_err("Use only one of --enable-smart-routing or --disable-smart-routing.")
12411240
raise typer.Exit(1)
1242-
if disable_intelligent_routing_flag:
1243-
disable_intelligent_routing(load_state())
1244-
print_success("Codex intelligent routing disabled; ucode routing hooks removed")
1241+
if disable_smart_routing_flag:
1242+
disable_smart_routing(load_state())
1243+
print_success("Codex smart routing disabled; ucode routing hooks removed")
12451244
return
12461245
_launch_tool(
12471246
"codex",
12481247
ctx,
12491248
provider=provider,
12501249
skip_preflight=skip_preflight,
12511250
workspace=workspace,
1252-
enable_codex_intelligent_routing=enable_intelligent_routing_flag,
1251+
enable_codex_smart_routing=enable_smart_routing_flag,
12531252
)
12541253

12551254

src/ucode/intelligent_routing/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)