Skip to content

Routes are named sets of rules, assigned to keys - #10

Merged
fylorn merged 1 commit into
mainfrom
dev
Sep 14, 2026
Merged

fylorn merged 1 commit into
mainfrom
dev

Conversation

@fylorn

@fylorn fylorn commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

What

A route is now a named group of rules that a key can be given, instead of a flat table where each rule optionally carried a client: condition.

routes:
  - name: 默认
    default: true          # applies to every key, at most one
    rules:
      - name: cached goes to official
        when: { cache: true }
        to: official
  - name: long context
    rules:
      - name: over 200k downgrades
        when: { input_tokens: ">200k" }
        set: { model: claude-haiku-4-5 }

clients:
  - name: codex
    key: tw-…
    routes: [long context]   # stacked on top of the default

Route is renamed Rule; RouteSet is the new thing called a route. The rename is the point, not collateral — "assign this route to that key" means a group of rules, and calling one rule a route would have kept that ambiguity permanently.

Three properties, each with a test

The default route is always evaluated first, wherever it sits in the file. Order is semantics here — to takes the first match — so a default written at the end would become the one rule nothing can reach.

A route serves several keys without being copied. That is what naming buys. Two keys referencing one route see the same rule instance; copies drift, and only one of them ever gets fixed.

A key with no assignment still works, on the default route alone. That is the first-run state and a freshly created key, not an error.

when.client is removed — assignment does that job now, and keeping both would be two ways to express one thing.

What did not change

The engine still evaluates a flat list. It just builds that list per request from the default route's rules plus the ones assigned to the requesting key, so set and guard still accumulate from every matching rule while to and deny take the first.

That accumulation is why the flat list survives: "everyone gets redaction, codex also swaps the model" stays two rules rather than two copies of the redaction rule.

Three new load-time errors

Each corresponds to a failure that was otherwise completely silent:

why it has to fail loudly
two routes with one name "which one is assigned" has no answer
more than one default route —
a key assigned to a route that does not exist that key quietly falls back to the default while its owner believes the rules are live

Dry run

Now traces only the rules the simulated key actually evaluates. It used to list every rule in the config, reporting rules belonging to other keys as skipped — they are not skipped, they were never on this request's path.

Breaking

routes: changes shape and when.client is gone. Nothing is released, and a config carrying the old form fails at the schema stage naming the field it did not expect.

Verification

cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, 1078 tests (6 new on the route model), and scripts/smoke.sh — 46 checks, 0 failures.

🤖 Generated with Claude Code

A route is now a named group of rules that a key can be given, instead
of a flat table where each rule optionally carried a `client:` condition.

    routes:
      - name: 默认
        default: true          # applies to every key, at most one
        rules:
          - name: cached goes to official
            when: { cache: true }
            to: official
      - name: long context
        rules:
          - name: over 200k downgrades
            when: { input_tokens: ">200k" }
            set: { model: claude-haiku-4-5 }

    clients:
      - name: codex
        key: tw-…
        routes: [long context]  # stacked on top of the default

`Route` is renamed `Rule` and `RouteSet` is the new thing called a route.
The rename is the point, not collateral: "assign this route to that key"
means a group of rules, and calling one rule a route would have kept that
ambiguity forever.

**The default route is always evaluated first**, wherever it appears in
the file. Order is semantics here — `to` takes the first match — so a
default written at the end would become the one rule nothing can reach.

**A route serves several keys without being copied.** That is what naming
buys, and there is a test pinning it: two keys referencing one route see
the same rule instance. Copies drift, and only one of them gets fixed.

`when.client` is removed. Assignment does that job now, and keeping both
would be two ways to express one thing.

## What did not change

The engine still evaluates a flat list; it just builds that list per
request from the default route's rules plus the ones assigned to the
requesting key. So `set` and `guard` still accumulate from every matching
rule while `to` and `deny` take the first — the accumulation semantics
that make "everyone gets redaction, codex also swaps the model" two
rules instead of two copies.

## Three new load-time errors

Each corresponds to a failure that was otherwise silent:

  · two routes with one name — "which one is assigned" has no answer
  · more than one default route
  · a key assigned to a route that does not exist — that key would
    quietly fall back to the default while its owner believed the rules
    were live

## Dry run

Now traces only the rules the simulated key actually evaluates. It used
to list every rule in the config, which reported rules belonging to other
keys as "skipped" — they are not skipped, they were never on this
request's path at all.

## Breaking

`routes:` changes shape and `when.client` is gone. Nothing is released,
and a config carrying the old form fails at the schema stage with the
field it did not expect.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 384126f into main Sep 14, 2026
3 checks passed
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.

1 participant