Skip to content

refactor: priority-based tool result dispatch selection#127

Merged
wpak-ai merged 3 commits into
masterfrom
refactor/dispatch-simplify
Jul 10, 2026
Merged

refactor: priority-based tool result dispatch selection#127
wpak-ai merged 3 commits into
masterfrom
refactor/dispatch-simplify

Conversation

@clean6378-max-it

@clean6378-max-it clean6378-max-it commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Closes #118

Adding a new tool result shape in utils/tool_dispatch.py used to mean figuring out where to insert it in _TOOL_RESULT_DISPATCH so the right predicate won on overlap.
Tuple position was load-bearing, and the two intentional exceptions (plan over file_write, task_message over the narrower task predicates) were easy to miss unless you already knew the whole table.

This branch replaces the (predicate, builder) tuples with ToolResultDispatchEntry records that carry an explicit priority.
Every matching predicate is considered; the highest priority wins, with registration order as the tie-break.
The overlap pairs set priority 1 on the broader shape and leave the narrower ones at 0.

Classification output is unchanged for existing fixtures.
Ordering invariants, adversarial overlap tests, and real-session coverage were updated to assert priority instead of index. docs/architecture.md and CONTRIBUTING.md now describe the priority rule.

Summary by CodeRabbit

  • Bug Fixes
    • Improved toolUseResult classification for overlapping payloads by using priority-based selection with deterministic tie-breaking.
    • Hardened tool-result parsing and default handling for missing/invalid fields to avoid misclassification.
  • Documentation
    • Updated contribution and architecture guidance to reflect priority-based dispatch and safe extension rules.
    • Refreshed dispatch registry documentation for both backend behavior and frontend lookup.
  • Tests
    • Strengthened adversarial, ordering, and fixture coverage to verify “highest priority wins” and overlapping predicate invariants.

@clean6378-max-it clean6378-max-it self-assigned this Jul 9, 2026
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3f9e700a-f1f8-47ea-8ce4-b6f675fdffdf

📥 Commits

Reviewing files that changed from the base of the PR and between 26baa45 and 61153ab.

📒 Files selected for processing (5)
  • docs/architecture.md
  • tests/test_real_session_fixtures.py
  • tests/test_tool_dispatch_adversarial.py
  • tests/test_tool_dispatch_ordering.py
  • utils/tool_dispatch.py
✅ Files skipped from review due to trivial changes (1)
  • docs/architecture.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/test_real_session_fixtures.py
  • utils/tool_dispatch.py

📝 Walkthrough

Walkthrough

Tool result classification now uses explicit dispatch entries with priorities. All matching predicates are evaluated, the highest-priority entry wins, and registration order resolves ties. Tests, fixtures, and documentation describe and validate the new behavior.

Changes

Tool dispatch priority refactor

Layer / File(s) Summary
Dispatch entries and winner selection
utils/tool_dispatch.py
Introduces ToolResultDispatchEntry, explicit priorities, deterministic tie-breaking, winner-based parsing, and defensive result handling.
Priority invariants and fixture validation
tests/test_tool_dispatch_*.py, tests/test_real_session_fixtures.py, tests/test_jsonl_parser.py
Updates tests to validate priority relationships, inverted priorities, winner IDs, and overlapping classifications.
Dispatch extension documentation
CONTRIBUTING.md, docs/architecture.md
Documents priority-based registration, overlap invariants, tie-breaking, and backend lookup behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Sequence Diagram(s)

sequenceDiagram
  participant Parser as _parse_tool_result
  participant Registry as _TOOL_RESULT_DISPATCH
  participant Selector as _winning_dispatch_entry
  participant Builder as winning build function
  Parser->>Registry: evaluate matching predicates
  Registry-->>Selector: return matching entries
  Selector->>Selector: choose highest priority
  Selector-->>Parser: return winning entry
  Parser->>Builder: build parsed result
Loading

Suggested reviewers: timon0305, wpak-ai

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 47.06% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: switching tool result dispatch to priority-based selection.
Linked Issues check ✅ Passed The PR implements priority-based dispatch, updates docs, and refreshes the specified tests while preserving existing classifications.
Out of Scope Changes check ✅ Passed The changes stay within dispatch refactoring, documentation, and related test updates, with no new tool types or registry changes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/dispatch-simplify

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
utils/tool_dispatch.py (1)

236-270: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Guard against accidental duplicate id values in the dispatch table.

_DISPATCH_ORDER is built via {entry.id: index for ...}; a duplicate id (easy to introduce when the whole point of this refactor is to let contributors append entries without reasoning about the full table) would silently collide, causing incorrect tie-break ordering with no test failure to catch it. A cheap assertion would fail fast on misconfiguration.

♻️ Proposed guard
 _DISPATCH_ORDER = {entry.id: index for index, entry in enumerate(_TOOL_RESULT_DISPATCH)}
+assert len(_DISPATCH_ORDER) == len(_TOOL_RESULT_DISPATCH), (
+    "Duplicate ToolResultDispatchEntry id in _TOOL_RESULT_DISPATCH"
+)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@utils/tool_dispatch.py` around lines 236 - 270, Add a fail-fast validation
near _DISPATCH_ORDER that detects duplicate entry.id values in
_TOOL_RESULT_DISPATCH before constructing the index mapping, raising a clear
assertion or configuration error identifying the duplicated IDs. Preserve the
existing registration-order mapping after validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/architecture.md`:
- Around line 74-80: Update the stale parenthetical in the backend dispatch
description near the referenced architecture section: replace “ordered
predicates” with wording that accurately describes priority-based predicate
matching, consistent with _parse_tool_result and _TOOL_RESULT_DISPATCH. Do not
alter the surrounding architectural guidance.

---

Nitpick comments:
In `@utils/tool_dispatch.py`:
- Around line 236-270: Add a fail-fast validation near _DISPATCH_ORDER that
detects duplicate entry.id values in _TOOL_RESULT_DISPATCH before constructing
the index mapping, raising a clear assertion or configuration error identifying
the duplicated IDs. Preserve the existing registration-order mapping after
validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 43342ceb-b8eb-47e6-bc4d-4ca339d050a6

📥 Commits

Reviewing files that changed from the base of the PR and between 964813f and 77eed93.

📒 Files selected for processing (7)
  • CONTRIBUTING.md
  • docs/architecture.md
  • tests/test_jsonl_parser.py
  • tests/test_real_session_fixtures.py
  • tests/test_tool_dispatch_adversarial.py
  • tests/test_tool_dispatch_ordering.py
  • utils/tool_dispatch.py

Comment thread docs/architecture.md

@timon0305 timon0305 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please check comments

Comment thread utils/tool_dispatch.py Outdated
Comment thread tests/test_tool_dispatch_ordering.py Outdated
@timon0305

Copy link
Copy Markdown
Collaborator

Looks good. All addressed. Dropping the priority boost on task_message and learning on registration order is the right call, and the ordering test now running _winning_dispatch_entry against real overlap blobs is a much stronger check than the old priority-int comparison. Adding the file_write beats task_message regression was a nice touch.
Once optional thing, not blocking: plan still carries priority=1. It is redundant since plan is registered before file_write, so order already wins that overlap.
Dropping it would make the whole table order-driven and consistent, but I'm happy either way.

@clean6378-max-it clean6378-max-it requested a review from wpak-ai July 10, 2026 09:25
@wpak-ai wpak-ai merged commit f294fb9 into master Jul 10, 2026
16 checks passed
@wpak-ai wpak-ai deleted the refactor/dispatch-simplify branch July 10, 2026 14:19
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.

claude-code-chat-browser: Simplify predicate-ordered tool dispatch selection

3 participants