Skip to content

Conversation

@nielsenko
Copy link
Collaborator

@nielsenko nielsenko commented Dec 2, 2025

Description

  • Refactor PathTrie.lookup() from iterative to recursive implementation
  • Add backtracking support: when a literal match leads to a dead end, falls back to parameter/tail routes
  • Preserve original iterative implementation as _lookupIterative() for reference
  • Add _composeMap() helper for combining map functions
  • Add 11 new tests covering backtracking behavior with parameters and tail routes

Related Issues

Pre-Launch Checklist

  • This update focuses on a single feature or bug fix.
  • I have read and followed the Dart Style Guide and formatted the code using dart format.
  • I have referenced at least one issue this PR fixes or is related to.
  • I have updated/added relevant documentation (doc comments with ///), ensuring consistency with existing project documentation.
  • I have added new tests to verify the changes.
  • All existing and new tests pass successfully.
  • I have documented any breaking changes below.

Breaking Changes

  • Includes breaking changes.
  • No breaking changes.

Routes that previously returned null when a literal match led to a dead end will now backtrack and potentially match a parameter or tail route instead.

Additional Notes

One test skipped: "literal segments are prioritized over parameters, even if it prevents a match" - this behavior is intentionally changed by backtracking.

Summary by CodeRabbit

  • Refactor

    • Enhanced route matching and resolution logic for improved efficiency in path routing.
  • Tests

    • Added comprehensive test coverage for route resolution backtracking and precedence scenarios to ensure reliable routing behavior in complex path hierarchies.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 2, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

Introduces a recursive lookup implementation for PathTrie with a new _lookupRecursive method and _composeMap helper function to handle backtracking during path traversal. The recursive approach prioritizes literal child matches before dynamic segments (parameters, wildcards, tails), accumulates composed mapping functions, and returns a TrieMatch when a value is found. Tests add extensive backtracking scenarios and modify an existing test with a skip annotation.

Changes

Cohort / File(s) Change Summary
Core Recursive Lookup Implementation
lib/src/router/path_trie.dart
Introduces _lookupRecursive for recursive path traversal with backtracking logic (literal children prioritized over dynamic segments), _composeMap helper to compose mapping functions during nested lookups, and refactors lookup to delegate to the new recursive path. Keeps unused _lookupIterative as reference.
Backtracking Tests
test/router/path_trie_test.dart
Adds comprehensive "Backtracking" test group covering multi-level literal-to-parameter fallback scenarios, tail/glob pattern interactions, edge cases, and parameter-route precedence verification. Skips existing test in "Route Precedence" group with message about backtracking allowing parameter route fallback.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • path_trie.dart: Logic-dense recursive traversal implementation with backtracking state management and function composition; requires careful review of traversal order (literal prioritization) and mapping composition chain.
  • path_trie_test.dart: Large test additions spanning multiple overlapping scenarios; verify backtracking coverage aligns with backtracking algorithm implementation; validate skip annotation reasoning.

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main feature being introduced: backtracking support during routing, with the breaking change indicator (!) appropriately included.
Description check ✅ Passed The PR description covers all required template sections: detailed changes, related issue reference, completed pre-launch checklist, breaking changes documentation, and additional notes about the skipped test.
Linked Issues check ✅ Passed The code changes directly implement the backtracking mechanism required by issue #296, allowing fallback to parameter and tail routes when literal matches lead to dead ends.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing backtracking in the router as specified in issue #296; the recursive implementation, helper functions, and extensive test coverage are all in scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

❌ Patch coverage is 91.07143% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.93%. Comparing base (ba3f3a9) to head (7f9a294).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/src/router/relic_app.dart 50.00% 4 Missing ⚠️
lib/src/router/path_trie.dart 97.67% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #297      +/-   ##
==========================================
- Coverage   92.00%   91.93%   -0.07%     
==========================================
  Files          97       97              
  Lines        3664     3682      +18     
  Branches     1881     1888       +7     
==========================================
+ Hits         3371     3385      +14     
- Misses        293      297       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nielsenko
Copy link
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 2, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nielsenko nielsenko self-assigned this Dec 2, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
lib/src/router/path_trie.dart (1)

464-465: Consider removing in a future cleanup.

The preserved iterative implementation serves as a useful reference during this transition. Once the recursive implementation is validated in production, consider removing this dead code to reduce maintenance burden.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c75273d and 2d9fc82.

📒 Files selected for processing (2)
  • lib/src/router/path_trie.dart (1 hunks)
  • test/router/path_trie_test.dart (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.dart: All Dart code must pass static analysis using dart analyze --fatal-infos with no issues
All Dart files must be formatted with dart format (CI enforces dart format --set-exit-if-changed .)

Files:

  • test/router/path_trie_test.dart
  • lib/src/router/path_trie.dart
test/**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

test/**/*.dart: Tests should follow the Given-When-Then pattern in descriptions (flexible structuring allowed)
Use Arrange-Act-Assert pattern within test bodies
Provide clear, descriptive test titles; prefer single responsibility per test unless related assertions improve clarity
Place tests in the test/ directory mirroring the lib/ structure

Files:

  • test/router/path_trie_test.dart
lib/**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

lib/**/*.dart: Use Uint8List for request/response bodies for performance; avoid List for body payloads
Use type-safe HTTP header parsing and validation when accessing headers
Use router with trie-based matching and symbol-based path parameters (e.g., #name, #age) for routing
Ensure WebSocket handling includes proper lifecycle management (e.g., ping/pong for connection health)

Files:

  • lib/src/router/path_trie.dart
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: serverpod/relic PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-09T16:21:09.310Z
Learning: Applies to lib/**/*.dart : Use router with trie-based matching and symbol-based path parameters (e.g., `#name`, `#age`) for routing
Learnt from: nielsenko
Repo: serverpod/relic PR: 186
File: lib/src/router/router.dart:60-64
Timestamp: 2025-10-03T08:29:41.568Z
Learning: In the Router class in lib/src/router/router.dart, the use() method stores transformation functions on trie nodes. These functions are composed and applied during lookup (in PathTrie.lookup() at line 405), but the original stored route values are never modified. The transformation is temporary and only affects the returned value from lookup operations.
Learnt from: nielsenko
Repo: serverpod/relic PR: 48
File: lib/src/handler/handler.dart:59-67
Timestamp: 2025-04-25T07:39:38.915Z
Learning: Nielsenko prefers using switch statements with pattern matching over if statements when working with sealed classes in Dart, as they provide exhaustiveness checking at compile time and can be more concise.
📚 Learning: 2025-10-09T16:21:09.310Z
Learnt from: CR
Repo: serverpod/relic PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-09T16:21:09.310Z
Learning: Applies to lib/**/*.dart : Use router with trie-based matching and symbol-based path parameters (e.g., `#name`, `#age`) for routing

Applied to files:

  • test/router/path_trie_test.dart
  • lib/src/router/path_trie.dart
📚 Learning: 2025-05-05T14:40:00.323Z
Learnt from: nielsenko
Repo: serverpod/relic PR: 52
File: lib/src/router/router.dart:37-53
Timestamp: 2025-05-05T14:40:00.323Z
Learning: In the Router and PathTrie implementation in Dart, both static and dynamic routes consistently throw ArgumentError when attempting to add duplicate routes, despite comments suggesting dynamic routes would be overwritten with a warning.

Applied to files:

  • test/router/path_trie_test.dart
  • lib/src/router/path_trie.dart
📚 Learning: 2025-10-09T16:21:09.310Z
Learnt from: CR
Repo: serverpod/relic PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-09T16:21:09.310Z
Learning: Applies to test/**/*.dart : Provide clear, descriptive test titles; prefer single responsibility per test unless related assertions improve clarity

Applied to files:

  • test/router/path_trie_test.dart
📚 Learning: 2025-05-05T16:06:15.941Z
Learnt from: nielsenko
Repo: serverpod/relic PR: 52
File: lib/src/router/normalized_path.dart:72-86
Timestamp: 2025-05-05T16:06:15.941Z
Learning: When writing tests for Dart classes with private constructors and factory methods, extending the class for test purposes may not be possible. The NormalizedPath class in particular uses a private constructor and factory constructor pattern.

Applied to files:

  • test/router/path_trie_test.dart
📚 Learning: 2025-05-09T10:11:33.427Z
Learnt from: nielsenko
Repo: serverpod/relic PR: 62
File: lib/src/router/router.dart:0-0
Timestamp: 2025-05-09T10:11:33.427Z
Learning: In the Router implementation, re-adding a route with the same path isn't currently allowed and will throw an ArgumentError from _allRoutes.add(), but handling cache invalidation or updates proactively is important for future-proofing in case route updates are later supported.

Applied to files:

  • test/router/path_trie_test.dart
📚 Learning: 2025-10-03T08:29:41.568Z
Learnt from: nielsenko
Repo: serverpod/relic PR: 186
File: lib/src/router/router.dart:60-64
Timestamp: 2025-10-03T08:29:41.568Z
Learning: In the Router class in lib/src/router/router.dart, the use() method stores transformation functions on trie nodes. These functions are composed and applied during lookup (in PathTrie.lookup() at line 405), but the original stored route values are never modified. The transformation is temporary and only affects the returned value from lookup operations.

Applied to files:

  • test/router/path_trie_test.dart
  • lib/src/router/path_trie.dart
🔇 Additional comments (9)
lib/src/router/path_trie.dart (5)

365-367: LGTM!

Clean delegation to the recursive implementation with appropriate initial parameters: root node, starting index 0, root's map function, and empty parameters map.


378-399: Verify that tail node's map is intentionally not applied in the fallback case.

When the path exactly matches a prefix ending at a tail (e.g., /archive matching /archive/**), the fallback retrieves the value from dynamicSegment.node (line 387) but applies currentMap (line 392), which doesn't include dynamicSegment.node.map.

The iterative implementation has the same behavior, so this is consistent. Confirm this is intentional—if a map is set via use() on the tail route itself, it won't be applied when matching the prefix without the tail segment.


401-416: LGTM! Clean backtracking implementation.

The literal-first approach with fallback to dynamic segments is well-structured. If the literal path leads to a dead end (returns null), execution naturally falls through to attempt dynamic segment matching, implementing backtracking without explicit stack management.


418-449: LGTM!

Dynamic segment handling is correct:

  • Parameters correctly extract values into the params map using Symbol(dynamicSegment.name)
  • Tail segments return immediately when a value exists, capturing the remaining path
  • Non-tail segments (parameters/wildcards) continue the recursive search

The distinction between tail (greedy match) and parameter/wildcard (single segment match with continued traversal) is properly implemented.


454-462: LGTM!

The composition order outer(inner(v)) correctly applies child node maps first, then parent maps—matching the composition logic in use() at line 190 and the iterative implementation at lines 476-481. Based on learnings, this ensures transformation functions are properly composed during lookup.

test/router/path_trie_test.dart (4)

116-134: LGTM!

The skip annotation with a clear message appropriately documents that this test verifies the old (non-backtracking) behavior. The new expected behavior is covered by the test at lines 138-149 in the Backtracking group.


137-149: LGTM! Excellent test coverage for the core backtracking behavior.

This test directly demonstrates the breaking change: paths that previously returned null (when a literal match led to a dead end) now backtrack and match alternative parameter routes. The test clearly documents the new expected behavior.


204-213: LGTM!

Good edge case coverage. The test correctly expects null because:

  1. "guest" matches the :id parameter, but :id node has no "settings" child
  2. Backtracking cannot switch to the literal "admin" path since we're already at "users" level
  3. This confirms backtracking respects the trie structure boundaries

228-316: LGTM! Comprehensive tail route backtracking coverage.

These tests thoroughly cover tail route (/**) interactions with backtracking:

  • Tail as fallback when literal paths fail (lines 228-245)
  • Tail combined with parameter routes (lines 247-265)
  • Nested tail precedence (lines 267-286)
  • Tail with intermediate literal nodes (lines 288-315)

The assertions for matched.path and remaining.path correctly verify the path splitting behavior documented in TrieMatch.

@nielsenko nielsenko marked this pull request as ready for review December 2, 2025 10:33
@nielsenko nielsenko requested a review from SandPod December 2, 2025 10:33
@nielsenko
Copy link
Collaborator Author

This does dethrone us ever so slightly:

❯ ./benchmark/benchmark.exe run -v
Setting up benchmark data with 1000 routes...
Setup complete.
Starting benchmarks
Add;Static;x1000;Routingkit;RunTime;512.0287342085708;us
Add;Static;x1000;Spanner;RunTime;443.9005;us
Add;Static;x1000;Router;RunTime;302.7725717685557;us
Lookup;Static;x1000;Routingkit;RunTime;195.55810561120296;us
Lookup;Static;x1000;Spanner;RunTime;131.90784647629027;us
Lookup;Static;x1000;Router;RunTime;136.60510102683008;us
Add;Dynamic;x1000;Routingkit;RunTime;731.6195;us
Add;Dynamic;x1000;Spanner;RunTime;3724.7884615384614;us
Add;Dynamic;x1000;Router;RunTime;556.02675;us
Lookup;Dynamic;x1000;Routingkit;RunTime;1786.8708333333334;us
Lookup;Dynamic;x1000;Spanner;RunTime;345.2196118488253;us
Lookup;Dynamic;x1000;Router;RunTime;355.00105894811156;us
Done

😄

@nielsenko nielsenko requested a review from a team December 2, 2025 10:58
Copy link
Contributor

@SandPod SandPod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice quick work, couple of comments.

Copy link
Contributor

@SandPod SandPod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Given that a config option is exposed that could disable back-track and that the last tets are fixed.

@nielsenko
Copy link
Collaborator Author

Rebased on main after merge of #295

Copy link
Contributor

@SandPod SandPod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@nielsenko nielsenko merged commit 1ab5c28 into serverpod:main Dec 3, 2025
25 of 27 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.

Implement backtracking in router to prevent overshadowing of wildcard routes

2 participants