Skip to content

refactor: memory/ and setup/ packages (code-shape 4/5) - #21

Merged
emanuilo merged 6 commits into
mainfrom
refactor/4-packages
Nov 22, 2026
Merged

emanuilo merged 6 commits into
mainfrom
refactor/4-packages

Conversation

@emanuilo

Copy link
Copy Markdown
Owner

Summary

Stack 4/5 of the code-shape restructuring (base: #20). The flat top level becomes memory/ and setup/ packages, each sealed behind its __init__.

 memclaw/
-  store.py index.py search.py prompts.py setup.py   (13 top-level modules)
+  terminal_input.py   was prompts.py (masked_input, choose); below backends, so backends' own setup questions import down
+  memory/             exports MemoryStore, MemoryIndex, HybridSearch, SearchResult, Consolidator
+    notes.py          was store.py
+    chunking.py       chunk splitting, out of the index
+    index.py          the only SQL in memory/
+    search.py         no SQL, no sqlite3; reads index.db through MemoryIndex
+    refs.py consolidation.py
+  setup/              exports run_setup, needs_setup, print_logo, print_probe_report
+    wizard.py         was setup.py
+    doctor.py         OpenAI model-access check + report

New contracts (9 → 11): other code imports only memclaw.memory / memclaw.setup exports; internal layers in memory/ (consolidation, search → notes, index, refs → chunking). Kit rule: sqlite3 only in memory/index.py and reminders.py.

Ordering note: the kit treats a baseline entry as new once its file moves, so logic fixes come first (chunking and search-merge split, masked-input key loop, typed backend classes) and the move commits are pure git mv + imports.

Evidence

  • Search, old (9a145e6) vs new: a fixture vault queried 300 ways (hybrid, keyword-heavy, no decay, two MMR settings, file filters, embedding cache, image search) → identical results and scores.
  • Chunking: 300 random notes × 5 chunk sizes → the same 5,713 chunks, byte for byte.
  • Database: sqlite3 .schema dump of index.db identical.
  • Wizard / CLI: first-run and four memclaw configure scenarios, doctor, status, --help, search, index, save → same output and same .env.
  • code-shape: top-level modules 13 → 9; SQL in search.py 4 → 0; sqlite3 users 3 → 2; mypy errors 9 → 2; baseline 35 → 24.
  • pytest: 526 passed, 20 skipped (was 514); tests mirror memory/ and setup/. ruff, code-shape check clean.

Merge Danger

Door: two-way

Blast Radius: library imports

  • Behaviour, files and DB schema unchanged.
  • Import paths change with no shims: from memclaw.memory import MemoryStore, MemoryIndex, HybridSearch; memclaw.prompts → memclaw.terminal_input; memclaw.setup is now a package. docs/library.md and docs/upgrading.md updated.
  • Stacked on refactor(memory): consolidation gets its own home (code-shape 3/5) #20; merge bottom-up.

🤖 Generated with Claude Code

search.py ran its own SQL on index.db (4 queries, its own sqlite3
import). MemoryIndex now offers what search needs:

- embeddings_version(): (count, highest id) of the embedded chunks, the
  key search caches its embedding matrix on.
- chunk_embeddings(): (chunk id, embedding) of every embedded chunk.
- keyword_matches(fts_query, limit): the FTS5/BM25 matches; a query
  SQLite rejects is logged and finds nothing, as before.
- get_chunk(id): one stored chunk.

search.py keeps the ranking (query words, score weights, decay, MMR) and
has no SQL or sqlite3 import left. HybridSearch._merge is split into
_normalise, _combine and _load.

MemoryIndex is 345 lines (was 419): the table definitions and the
telegram_images migration are module functions (_create_schema), with the
SQL text unchanged, since SQLite stores it in sqlite_master.

Baseline tightened: search.py's sqlite3 use and _merge's complexity and
locals are gone, MemoryIndex 419 -> 345 lines.

Same results: a fixture vault searched old vs new (300 searches over
hybrid, keyword-heavy, no-decay and MMR settings, file filters, the
embedding cache, images) gives identical results and scores, and the
schema in sqlite_master is identical.
- store.py becomes memory/notes.py (MemoryStore), search.py becomes
  memory/search.py (HybridSearch, SearchResult). memclaw.memory exports
  them next to Consolidator; agent, cli, tools and the tests import them
  from there. Only import paths change.
- Tests mirror the move: tests/memory/test_notes.py, test_search.py.

Guards: the memory-via-package contract now also covers memory.notes and
memory.search, and the core layer loses store and search (they're part of
the memory layer). The top-level folder is down to 11 modules (limit 12),
so its baseline entry is gone.

Library users: `from memclaw.memory import MemoryStore, HybridSearch`
(docs/library.md, docs/upgrading.md).
MemoryIndex.chunk_text was the most tangled function left in the index
(cognitive complexity 27, 17 locals). It is now chunk_text(text,
file_path, target_words=, overlap_words=) in chunking.py, with the
running state in a small _Chunker (add a line, flush a chunk, keep the
overlap after a size split). chunking.py has no I/O and imports nothing
from memclaw; Chunk moves there too.

MemoryIndex is under the 300-line class limit now, so its baseline entry
is gone, with chunk_text's three.

Guards: chunking sits in the layer below index, next to openai_client.
It joins memclaw/memory/ with the index in the next commit.

Same chunks: 300 random notes (headings, `---` lines, blank lines, long
lines) indexed with five chunk-size/overlap settings give the same 5,713
chunks, line ranges and text as before. Tests: tests/test_chunking.py
(was part of test_index.py), plus two cases for the size split.
…package

- index.py becomes memory/index.py and chunking.py memory/chunking.py.
  memclaw.memory exports MemoryIndex; agent, cli, tools and the tests
  import it from there. Only import paths change.
- Tests mirror the move: tests/memory/test_index.py, test_chunking.py.

The memory subsystem is now one package: notes, chunking, index, search,
consolidation, refs. memclaw/memory/__init__.py lists what it offers;
docs/memory-architecture.md has a "Where the Code Lives" table.

Guards:
- memory-via-package covers every module inside (chunking, index, notes,
  search, consolidation, refs): other code uses memclaw.memory.
- New memory-layers contract: inside the package, consolidation and
  search sit above notes, index and refs, and chunking below them
  (exhaustive, so a new module there must be placed).
- sqlite3 (kit banned_api) is allowed only in memory/index.py and
  reminders.py.
- The top-level layers lose index and chunking.

Library users: `from memclaw.memory import MemoryIndex` (docs/library.md,
docs/upgrading.md).
…s its own function

- The backend registry is typed as type[AgentBackend] (it was plain
  `type`), so mypy checks what the wizard and cli call on a backend class
  (name, display_name, wizard_setup, is_configured, ...). That fixes all 5
  type errors in setup.py and the 2 in cli.py.
- masked_input's terminal key loop (echo, backspace, Ctrl-C) moves to
  _read_masked, under the complexity limit; masked_input keeps the
  terminal set-up and restore. New tests drive the key loop.

Baseline tightened: those type errors and masked_input's complexity are
gone, so setup.py and prompts.py can move into memclaw/setup/ next
without carrying old entries.

Same wizard: `memclaw configure` (Claude/Cursor, Telegram/Slack/
WhatsApp/terminal, a missing required key), first-run setup, doctor and
status print the same text and write the same .env as before.
…p/; prompts.py is terminal_input.py

- setup.py becomes setup/wizard.py (the wizard and the logo). The OpenAI
  model-access check and report move to setup/doctor.py (check_openai,
  was _check_openai, and print_probe_report), shared by the wizard and
  `memclaw doctor`. memclaw.setup exports run_setup, needs_setup,
  print_logo and print_probe_report, what cli uses.
- prompts.py becomes terminal_input.py: it is the terminal prompts
  (numbered choice, masked input), not LLM prompts. It stays at the top
  level, in the core layer below the backends, because the backends ask
  their own setup questions (wizard_setup) and setup imports the backend
  registry: inside memclaw.setup it would make backends import upward.
- Tests mirror the move: tests/setup/test_wizard.py (with the platform
  and backend pickers, from test_prompts.py), tests/setup/test_doctor.py
  (new), tests/test_terminal_input.py.

Guards: a setup-via-package contract (other code uses memclaw.setup, not
its modules); terminal_input replaces prompts in the core layer; the
"setup wizard" concept's home is setup/wizard.py.

Same wizard: first-run setup, `memclaw configure` (several backends and
platforms, a missing required key), doctor and status print the same
text and write the same .env as before.
@emanuilo
emanuilo force-pushed the refactor/4-packages branch from e3c894d to d4ec491 Compare November 22, 2026 19:32
@emanuilo
emanuilo changed the base branch from refactor/3-consolidation to main November 22, 2026 19:32
@emanuilo emanuilo closed this Nov 22, 2026
@emanuilo emanuilo reopened this Nov 22, 2026
@emanuilo
emanuilo merged commit 7ebc3cc into main Nov 22, 2026
10 checks passed
@emanuilo
emanuilo deleted the refactor/4-packages branch November 22, 2026 20:12
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