Skip to content

refactor: replace if/elif dispatch chain with tool handler registry (PR 7)#2

Open
anonymort wants to merge 13 commits intoShinMegamiBoson:mainfrom
anonymort:main
Open

refactor: replace if/elif dispatch chain with tool handler registry (PR 7)#2
anonymort wants to merge 13 commits intoShinMegamiBoson:mainfrom
anonymort:main

Conversation

@anonymort
Copy link

@anonymort anonymort commented Feb 20, 2026

Summary

  • Replaces the 260-line `if/elif` tool dispatch chain in `_apply_tool_call` (engine.py:639–898) with a `dict[str, Callable]` registry, cutting that method from 260 lines to 15
  • Extracts `subtask` and `execute` into dedicated `_apply_subtask` / `_apply_execute` methods (they need call-time parameters `depth`, `context`, `on_event`, etc. that can't be pre-bound)
  • Adds a design doc at `docs/plans/2026-02-20-tool-registry-design.md`

How to add a tool now

  1. Add `handle(self, args: dict[str, Any]) -> tuple[bool, str]` to `RLMEngine`
  2. Register it in `_build_tool_registry()`
  3. Add schema to `tool_defs.py`

No changes to `_apply_tool_call` required.

Changes

File Change
`agent/engine.py` `_apply_tool_call` 260 → 15 lines; +17 `handle*` methods; +`_build_tool_registry`; +`_apply_subtask`; +`_apply_execute`
`docs/plans/2026-02-20-tool-registry-design.md` New — design rationale, architecture, alternatives, 3-step recipe for new tools

Test results

```
410 passed, 22 skipped, 4 failed (pre-existing, unchanged)
```

Zero regressions introduced.

Test plan

  • All 410 previously-passing tests still pass
  • No new failures
  • `_apply_tool_call` dispatches correctly for all 18 tool names
  • Unknown tool names still return `"Unknown action type: {name}"`

Add Apache 2.0 license file, contribution guide with code of conduct,
and [project.optional-dependencies] dev group (pytest, mypy, ruff).
Update README to reference the new files.
Ruff violations (12 fixed):
- N806: Rename _PARALLEL_TOOLS to parallel_tools (engine.py)
- B007: Rename unused loop var attempt to _attempt (model.py)
- RUF012: Add ClassVar annotation to mutable class attribute (tui.py)
- F841: Remove unused fed variable (cast_to_video.py)
- C408: Convert dict() calls to dict literals (5 files)
- RUF002: Replace × with x in docstring (test_integration.py)
- RUF005: Use iterable unpacking instead of concatenation (test_replay_log.py)
- RUF059: Prefix unused unpacked variable with underscore (test_settings.py)
- RUF015: Replace slice[0] with next() (test_tool_defs.py)

Mypy errors (19 fixed):
- Add type parameters to untyped dicts (settings.py, builder.py, tools.py)
- Add missing return type annotations (tools.py, tui.py)
- Fix incompatible type assignments (tools.py, demo.py, builder.py, engine.py)
- Fix Generator import from collections.abc (tools.py)
- Add TextIO type annotation (tools.py)
- Add cast for json.loads return value (runtime.py)
- Add type annotations for Rich integration (tui.py)
- Remove unused type: ignore comments (tools.py, engine.py, tui.py)
- Keep necessary type: ignore for Rich library compatibility (engine.py, tui.py)

All checks now pass:
- ruff: 0 violations
- mypy: 0 errors
- pytest: 412 passing tests
Replaces the 260-line if/elif chain in _apply_tool_call with a dict
registry pattern. Each stateless tool gets a dedicated _handle_* method;
subtask/execute are extracted into _apply_subtask/_apply_execute. Zero
behavior change — all argument extraction and validation logic is
copied verbatim from the original branches.
@anonymort anonymort changed the title docs: add LICENSE, CONTRIBUTING.md, and dev dependencies refactor: replace if/elif dispatch chain with tool handler registry (PR 7) Feb 20, 2026
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