Routes: fail boot on unreachable or ambiguous constrained params #127

Closed
opened 2026-10-10 23:22:44 +00:00 by chrnx · 0 comments
Owner

Today, constrained route params can silently shadow or tie with their siblings. The winner depends only on rank and registration order:

  • /algo/:id + /algo/:a(.*): the constraint accepts every segment, so :id can never be reached. Nothing warns about it.
  • /u/:id(\d+) + /u/:id([0-9]+): the same route written two ways. The shape check compares regex source, so it doesn't see the duplicate, and the first one registered wins.
  • /u/:id(\d+) + /u/:n(\d{1,3}): they overlap on 42. The first registered wins, silently.

Why this is decidable here. assertSafeConstraint allows only a small subset: a sequence of atoms (\d \D \s \S \w \W, escaped . - _ /, literal chars, [...] classes), each with an optional + * ? {n,m} quantifier (m ≤ 2048). There are no groups, no alternation and no backreferences. Every constraint is therefore a linear sequence of (character set, min, max), a regular language with a tiny automaton. Intersection and inclusion between two of them can be decided exactly with a product-automaton walk. No heuristics are needed.

Proposed rules, for the same method at the same position under the same prefix:

  1. Unreachable → boot error. A plain :param (any non-empty segment) next to a constraint that accepts every non-empty segment (e.g. (.*), (.+), ([^/]+), (\S+) when segments can't contain spaces... decide by inclusion, not by a list): the plain route is dead. The same rule applies to a constraint fully included in a higher-precedence sibling's language.
  2. Ambiguous → boot error. Two constraints whose languages intersect: the winner is decided only by registration order. The error names both declarations and a witness segment that both accept (e.g. both match '42').
  3. Not an error: a plain :param next to a constraint that does not cover every segment, e.g. /u/:id(\d+) + /u/:slug (digits go to :id, everything else to :slug). That fallback is deliberate, documented precedence, and the corpus freezes it as intended behaviour.

Proposal from the owner (2026-10-10): fail on ambiguity between /algo/:id and /algo/:a(.*). That is rule 1. Rule 3 keeps the legitimate fallback pattern working.

Scope and order

  • It's a breaking change: apps that boot today may fail. It needs a ### Breaking CHANGELOG entry and docs (routing guide), and should land before RC.1 (API freeze).
  • Do it after CARD-07 (radix parity). The radix tree makes the check cheap, because it only compares siblings in the same node. The parity corpus freezes today's equal-rank tie behaviour, so this change also updates the boot captures with explicit approval.
  • Apply the same rule to WS routes, whose selection today is first-registered.
  • Bound the work: counted quantifiers up to 2048 make the product walk at most about 2048×2048 states per pair. That is fine at boot, but add a test with the worst case.

Tests: one unreachable case, one ambiguous case with its witness in the message, the legitimate fallback still booting, equivalent sources (\d+ vs [0-9]+) detected as ambiguous, disjoint constraints (\d+ vs [a-z]+) booting.

Today, constrained route params can silently shadow or tie with their siblings. The winner depends only on rank and registration order: - `/algo/:id` + `/algo/:a(.*)`: the constraint accepts every segment, so `:id` can never be reached. Nothing warns about it. - `/u/:id(\d+)` + `/u/:id([0-9]+)`: the same route written two ways. The shape check compares regex *source*, so it doesn't see the duplicate, and the first one registered wins. - `/u/:id(\d+)` + `/u/:n(\d{1,3})`: they overlap on `42`. The first registered wins, silently. **Why this is decidable here.** `assertSafeConstraint` allows only a small subset: a sequence of atoms (`\d \D \s \S \w \W`, escaped `. - _ /`, literal chars, `[...]` classes), each with an optional `+ * ? {n,m}` quantifier (m ≤ 2048). There are no groups, no alternation and no backreferences. Every constraint is therefore a linear sequence of (character set, min, max), a regular language with a tiny automaton. Intersection and inclusion between two of them can be decided exactly with a product-automaton walk. No heuristics are needed. **Proposed rules**, for the same method at the same position under the same prefix: 1. **Unreachable → boot error.** A plain `:param` (any non-empty segment) next to a constraint that accepts every non-empty segment (e.g. `(.*)`, `(.+)`, `([^/]+)`, `(\S+)` when segments can't contain spaces... decide by inclusion, not by a list): the plain route is dead. The same rule applies to a constraint fully included in a higher-precedence sibling's language. 2. **Ambiguous → boot error.** Two constraints whose languages intersect: the winner is decided only by registration order. The error names both declarations and a **witness segment** that both accept (e.g. `both match '42'`). 3. **Not an error:** a plain `:param` next to a constraint that does *not* cover every segment, e.g. `/u/:id(\d+)` + `/u/:slug` (digits go to `:id`, everything else to `:slug`). That fallback is deliberate, documented precedence, and the corpus freezes it as intended behaviour. **Proposal from the owner (2026-10-10):** fail on ambiguity between `/algo/:id` and `/algo/:a(.*)`. That is rule 1. Rule 3 keeps the legitimate fallback pattern working. **Scope and order** - It's a breaking change: apps that boot today may fail. It needs a `### Breaking` CHANGELOG entry and docs (routing guide), and should land **before RC.1** (API freeze). - Do it **after CARD-07** (radix parity). The radix tree makes the check cheap, because it only compares siblings in the same node. The parity corpus freezes today's equal-rank tie behaviour, so this change also updates the boot captures with explicit approval. - Apply the same rule to WS routes, whose selection today is first-registered. - Bound the work: counted quantifiers up to 2048 make the product walk at most about 2048×2048 states per pair. That is fine at boot, but add a test with the worst case. Tests: one unreachable case, one ambiguous case with its witness in the message, the legitimate fallback still booting, equivalent sources (`\d+` vs `[0-9]+`) detected as ambiguous, disjoint constraints (`\d+` vs `[a-z]+`) booting.
chrnx closed this issue 2026-10-14 02:22:31 +00:00
Sign in to join this conversation.