Repository navigation
refactor: memory/ and setup/ packages (code-shape 4/5) - #21
Merged
Merged
Conversation
emanuilo
force-pushed
the
refactor/3-consolidation
branch
from
November 22, 2026 19:22
9a145e6 to
729be36
Compare
emanuilo
force-pushed
the
refactor/4-packages
branch
from
November 22, 2026 19:22
2d87ba3 to
4336511
Compare
emanuilo
force-pushed
the
refactor/3-consolidation
branch
from
November 22, 2026 19:27
729be36 to
4756094
Compare
emanuilo
force-pushed
the
refactor/4-packages
branch
from
November 22, 2026 19:28
4336511 to
e3c894d
Compare
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
force-pushed
the
refactor/4-packages
branch
from
November 22, 2026 19:32
e3c894d to
d4ec491
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stack 4/5 of the code-shape restructuring (base: #20). The flat top level becomes
memory/andsetup/packages, each sealed behind its__init__.New contracts (9 → 11): other code imports only
memclaw.memory/memclaw.setupexports; internal layers inmemory/(consolidation, search → notes, index, refs → chunking). Kit rule:sqlite3only inmemory/index.pyandreminders.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
sqlite3 .schemadump ofindex.dbidentical.memclaw configurescenarios,doctor,status,--help,search,index,save→ same output and same.env.search.py4 → 0; sqlite3 users 3 → 2; mypy errors 9 → 2; baseline 35 → 24.pytest: 526 passed, 20 skipped (was 514); tests mirrormemory/andsetup/.ruff,code-shape checkclean.Merge Danger
Door: two-way
Blast Radius: library imports
from memclaw.memory import MemoryStore, MemoryIndex, HybridSearch;memclaw.prompts→memclaw.terminal_input;memclaw.setupis now a package.docs/library.mdanddocs/upgrading.mdupdated.🤖 Generated with Claude Code