Skip to content

refactor: clear the remaining code-shape baseline (18 → 0) - #23

Merged
emanuilo merged 5 commits into
mainfrom
refactor/shape-debt
Nov 24, 2026
Merged

emanuilo merged 5 commits into
mainfrom
refactor/shape-debt

Conversation

@emanuilo

Copy link
Copy Markdown
Owner

Summary

Clears the last 18 code-shape baseline entries. .code-shape/baseline.json is now "entries": {}, with no suppressions, raised limits or type: ignore.

-memclaw/tools.py                          575 lines, ToolExecutor(7 args)
+memclaw/tools/      catalog executor context memory images files reminders
+                    Tool(definition, handler, label) per kind; ToolExecutor(ToolContext)
-memclaw/backends/claude.py                762 lines, _query cognitive 39
-memclaw/backends/claude_models.py
+memclaw/backends/claude/   backend cli replies settings cost configure models
+                    one handler per SDK message type; one wizard step per question
-memclaw/backends/cursor.py                590 lines, class 406 lines / 13 attrs
-memclaw/backends/cursor_hooks.py
-memclaw/backends/cursor_sdk_adapter.py    collect_run_result cognitive 24
+memclaw/backends/cursor/   backend bridge agents configure hooks sdk_adapter
+                    CursorBridge (shared process), ChatAgent (one chat), RunCollector

New contract: other code imports memclaw.tools, not its modules. The backend-internals contract lists the two new packages.

Evidence

  • Before: code-shape check baseline 18 entries (file too long 3, class too long 2, cyclomatic 2, cognitive 3, args/positional 2, branches 1, locals 2, attributes 1, mypy 2).
    After: 0 entries; max file 467 lines, max fan-out 8, mypy 0 errors.
  • TOOL_DEFINITIONS + step labels serialized: byte-identical to 89f6ca8.
  • 11 wizard scenarios (7 Claude, 4 Cursor) with a fake HOME: console output, return values, logs and files written byte-identical to 89f6ca8.
  • New pinning tests passed on an untouched 89f6ca8 copy and on the new code: every tool's replies/errors (27), Cursor run collection log lines/turn count, Claude turn and one-shot reply reading.
  • pytest: 603 passed, 20 skipped (was 567). ruff, lint-imports (12 contracts), code-shape check clean.

Merge Danger

Door: two-way

Blast Radius: backends + tools

  • Behaviour pinned as identical; Claude still reads model/effort at the same moments.
  • Library changes (in docs/upgrading.md): ToolExecutor(ToolContext(...)), executor.context.chat_id; claude_models, cursor_hooks, cursor_sdk_adapter moved inside their packages. ClaudeAgentBackend / CursorAgentBackend import paths unchanged.
  • tests/e2e/test_retrieval_e2e.py constructor update is unverified here (needs real API keys).
  • Tracked (non-gating) metric "module without its own test file" rises 7 → 25: new modules are tested through test_claude.py, test_cursor.py, test_tools.py.

🤖 Generated with Claude Code

- mcp_bridge: track whether the listening socket was handed over with a
  flag instead of setting the local to None, so it keeps its socket type.
- claude: claude_models types the effort levels as the SDK's Literal and
  sdk_effort() narrows a stored level to it before ClaudeAgentOptions.
tools.py (575 lines) held every tool's schema, code and progress label in
three parallel lists. Now each kind of tool has its own module (memory,
images, files, reminders) listing its tools as Tool records (definition,
handler, step label); catalog.py puts them in the same order, so
TOOL_DEFINITIONS is unchanged (byte-identical JSON), and executor.py runs
a call.

ToolExecutor takes a single ToolContext (config, store, index, search,
found_images, platform, scheduler, chat_id) instead of seven arguments.
The reminder tools get the scheduler and chat from one helper, which also
removes their type: ignore comments.

Adds tests/test_tools.py pinning every tool's replies (passes on the old
and the new code), a tools-via-package import contract, and an upgrading
note for the new constructor.
… collector

The run-stream loop kept the latest text, the running tool calls and the
reported steps in locals; _RunStream now holds them, with one method per
step (handle a message, report a step once, warn about unfinished calls),
and _count_turns works out the turn count and its warning.

New tests pin the logged lines, the text chosen and the turn count; they
pass on the old and the new code.
cursor.py (590 lines, a 406-line class with 13 attributes) becomes a
package, with the Cursor-only modules moved in beside it:

- backend.py: CursorAgentBackend (lifecycle, model selection, turns)
- bridge.py: CursorBridge, the shared bridge process (launch, relaunch
  when it died, drop), and bridge_failed / drain_stderr
- agents.py: ChatAgent, one chat's agent (open/resume, send, close), the
  options and messages it gets, and prompt_once for one-shot calls
- configure.py: the memclaw configure questions and the hook install
- hooks.py, sdk_adapter.py: moved from cursor_hooks.py and
  cursor_sdk_adapter.py unchanged

Behaviour, prompts, log lines and errors are unchanged. The wizard output
with a fake HOME is byte-identical to 89f6ca8 for every scenario tried; a
new test pins the hook status logged at start (passes on old and new).
claude.py (762 lines, a 488-line class) becomes a package:

- backend.py: ClaudeAgentBackend (settings, /model and /effort, and the
  turn and one-shot entry points), now 162 lines
- cli.py: ClaudeCli, running the CLI: the locked-down options, one
  connection per chat (connect/resume, reconnect after a model change,
  reset, shutdown), one-shot calls and the in-process MCP server
- replies.py: the stream-json image prompt, and the message loops with
  one handler per message type (_TurnReply for a turn, OneShotReply for
  a one-shot call) in place of the nested branches in _query and
  run_one_shot
- settings.py: credential, env, model and effort resolution
- cost.py: SessionCosts and the price table
- configure.py: the wizard, one function per question (auth mode,
  credential, model, effort)
- models.py: moved from claude_models.py unchanged

Prompts, options, log lines and errors are unchanged. New tests pin the
turn and one-shot reply reading (pass on 89f6ca8 and now), and the wizard
output with a fake HOME is byte-identical to 89f6ca8 in every scenario.
The code-shape baseline is now empty.
@emanuilo
emanuilo merged commit 79d85ce into main Nov 24, 2026
5 checks passed
@emanuilo
emanuilo deleted the refactor/shape-debt branch November 24, 2026 08:45
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