Skip to content

refactor(jsonl-parser): replace _parse_tool_result if/elif with dispa…#31

Open
clean6378-max-it wants to merge 3 commits intomasterfrom
feat/29-parse-tool-result-dispatch
Open

refactor(jsonl-parser): replace _parse_tool_result if/elif with dispa…#31
clean6378-max-it wants to merge 3 commits intomasterfrom
feat/29-parse-tool-result-dispatch

Conversation

@clean6378-max-it
Copy link
Copy Markdown
Collaborator

@clean6378-max-it clean6378-max-it commented May 6, 2026

Summary

This pull request refactors utils/jsonl_parser.py::_parse_tool_result by replacing the long if / elif chain with an ordered dispatch table, _TOOL_RESULT_DISPATCH, of (predicate, builder) pairs. The goal is to satisfy the structural half of CCC1 (single-site registration for new tool-result shapes) without changing parsing semantics.

Changes

  • Introduce _TOOL_RESULT_DISPATCH and fifteen _tool_result_pred_* / _tool_result_build_* helper pairs mirroring the previous branch order (bash through plan).
  • Implement _parse_tool_result as: reject non-dicts, iterate dispatch until the first matching predicate, otherwise set result_type to unknown as before.
  • Extend the docstring to document how to append a new (predicate, builder) pair and why order matters.

Testing

  • pytest (full suite): 181 passed locally.

References

Summary by CodeRabbit

  • Refactor

    • Improved internal parsing of tool results to be more maintainable and robust; behavior is unchanged for public interfaces.
  • Tests

    • Added a test to ensure web search result counts are handled correctly when results are missing or null.

…tch table

Implement _TOOL_RESULT_DISPATCH as an ordered sequence of (predicate,
builder) pairs with explicit _tool_result_pred_* / _tool_result_build_*
helpers so new tool-result shapes register in one place. Preserve legacy
branch order and unknown fallback; no intentional behavior change.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

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: 0d3a45f7-f97d-41a6-a52f-82489a5ddadd

📥 Commits

Reviewing files that changed from the base of the PR and between 8ceb7cc and 06d0388.

📒 Files selected for processing (1)
  • utils/jsonl_parser.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • utils/jsonl_parser.py

📝 Walkthrough

Walkthrough

Refactors tool-result parsing in utils/jsonl_parser.py by replacing a long conditional chain with a dispatch-based system: adds predicate and builder helpers for 14 tool result shapes, registers them in a centralized _TOOL_RESULT_DISPATCH, and rewrites _parse_tool_result to use the registry while preserving external parser APIs.

Changes

Tool-Result Dispatch Refactor

Layer / File(s) Summary
Predicate & Builder Helpers
utils/jsonl_parser.py (lines 329–534)
Added many private _tool_result_pred_* and _tool_result_build_* helpers covering bash, file_edit, file_write, glob, grep, file_read, web_search, web_fetch, task_message, task_retrieval, task_completed, task_async, todo_write, user_input, and plan shapes.
Dispatch Table
utils/jsonl_parser.py (lines 536–554)
Introduced _TOOL_RESULT_DISPATCH registry of (predicate, builder) pairs in matching order.
Refactored Parser Logic
utils/jsonl_parser.py (lines 556–574)
Replaced monolithic _parse_tool_result with dispatch-driven implementation that classifies tool results via the registry, always includes slug, returns None for non-dict inputs, and falls back to an "unknown" result_type when no predicate matches.
Tests
tests/test_jsonl_parser.py (lines ~174–180)
Added a test ensuring web_search result_count is 0 when results is None or not a list.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • timon0305
  • wpak-ai

Poem

A rabbit nibble on the code so neat,
Dispatches sorted, no more long repeat,
Predicates hop, builders make it right,
Slugs included, parsing light as flight,
Hooray — the parser hums beneath moonlight! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.25% 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 accurately reflects the main refactoring: replacing the if/elif chain in _parse_tool_result with a dispatch table approach.
Linked Issues check ✅ Passed The PR implements the dispatch table refactor, preserves branch ordering and matching semantics, maintains non-dict/null behavior, adds documentation, and passes existing tests.
Out of Scope Changes check ✅ Passed All changes are scoped to _parse_tool_result refactoring and related test coverage; no out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feat/29-parse-tool-result-dispatch

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

Copy link
Copy Markdown

@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: 1

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

556-556: ⚡ Quick win

Use explicit optional typing for slug.

slug: str = None is implicitly Optional; use slug: str | None = None instead for PEP 484 compliance and type-checker consistency.

🤖 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/jsonl_parser.py` at line 556, Update the function signature of
_parse_tool_result to use an explicit optional type for the slug parameter:
change the parameter annotation from slug: str = None to slug: str | None = None
so the signature reads def _parse_tool_result(tool_result, slug: str | None =
None) -> dict | None; this ensures PEP 484/typing consistency and satisfies type
checkers for optional values.
🤖 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 `@utils/jsonl_parser.py`:
- Around line 425-431: _guard the "results" value before calling len() in
_tool_result_build_web_search: fetch results with tr.get("results"), then
compute result_count only if that value is a sized container (e.g., has __len__
or is an instance of list/tuple/set/dict); otherwise set result_count to 0;
assign that count to result["result_count"] and leave the rest of the function
unchanged so parsing won't crash on None or other non-sized types.

---

Nitpick comments:
In `@utils/jsonl_parser.py`:
- Line 556: Update the function signature of _parse_tool_result to use an
explicit optional type for the slug parameter: change the parameter annotation
from slug: str = None to slug: str | None = None so the signature reads def
_parse_tool_result(tool_result, slug: str | None = None) -> dict | None; this
ensures PEP 484/typing consistency and satisfies type checkers for optional
values.
🪄 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: 57806755-a588-4992-aa6b-57948eb6dfa9

📥 Commits

Reviewing files that changed from the base of the PR and between 09de881 and ce95de9.

📒 Files selected for processing (1)
  • utils/jsonl_parser.py

Comment thread utils/jsonl_parser.py
@clean6378-max-it clean6378-max-it requested a review from timon0305 May 6, 2026 22:04
Comment thread utils/jsonl_parser.py
Comment thread utils/jsonl_parser.py
Comment thread utils/jsonl_parser.py
Comment thread utils/jsonl_parser.py Outdated
Comment thread tests/test_jsonl_parser.py
@clean6378-max-it clean6378-max-it requested a review from timon0305 May 7, 2026 19:13
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.

Issue — claude-code-chat-browser — Refactor _parse_tool_result dispatch table (5 pt)

2 participants