resolve.py python
46 lines 1.6 KB
Raw
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1 docs: MuseHub-first before ISR #74 — staging solidify NEXT Human 9 hours ago
1 """Deterministic routing resolution (§PR.4)."""
2
3 from __future__ import annotations
4
5 from tools.model_routing.types import RouteDecision, RouteEntry, RouteSelector, RoutingPolicy
6
7
8 def selector_matches(when: RouteSelector, query: RouteSelector) -> bool:
9 """Return True when every present ``when`` key equals the query value.
10
11 Omitted ``when`` keys are wildcards. Omitted query keys do not satisfy a
12 present ``when`` key — the query must explicitly provide each dimension a
13 rule constrains.
14 """
15 for key in ("position", "phase_tier", "gate"):
16 when_val = getattr(when, key)
17 if when_val is None:
18 continue
19 query_val = getattr(query, key)
20 if query_val is None or query_val != when_val:
21 return False
22 return True
23
24
25 def resolve_route(policy: RoutingPolicy, query: RouteSelector) -> RouteDecision:
26 """Resolve ``query`` against ``policy`` using first-match-wins + mandatory defaults."""
27 for route in policy.routes:
28 if selector_matches(route.when, query):
29 return RouteDecision(
30 route_id=route.id,
31 model_tier=route.model_tier,
32 fallback=route.fallback,
33 )
34 return RouteDecision(
35 route_id="defaults",
36 model_tier=policy.defaults_model_tier,
37 fallback=policy.defaults_fallback,
38 )
39
40
41 def find_matching_route(policy: RoutingPolicy, query: RouteSelector) -> RouteEntry | None:
42 """Return the first matching route entry, if any."""
43 for route in policy.routes:
44 if selector_matches(route.when, query):
45 return route
46 return None
File History 1 commit
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1 docs: MuseHub-first before ISR #74 — staging solidify NEXT Human 9 hours ago