Skip to content

refactor(memory): consolidation gets its own home (code-shape 3/5) - #20

Merged
emanuilo merged 2 commits into
mainfrom
refactor/3-consolidation
Nov 22, 2026
Merged

emanuilo merged 2 commits into
mainfrom
refactor/3-consolidation

Conversation

@emanuilo

Copy link
Copy Markdown
Owner

Summary

Stack 3/5 of the code-shape restructuring (base: #19). MEMORY.md consolidation gets its own home; agent.py keeps only turn handling.

 memclaw/
-  agent.py            699 lines: turns + consolidation prompt/budget/history/meta/schedule + system prompt
+  agent.py            348 lines: context block, handle(), sessions
+  instructions.py     system-prompt template, AGENTS.md/MEMORY.md loading, fingerprint
+  memory/
+    __init__.py       exports Consolidator (only public name)
+    consolidation.py  Consolidator: prompt, budget, .history/, meta.json, schedule, the only MEMORY.md writer
+    refs.py           was memory_refs.py
-  memory_refs.py
 memclaw consolidate
-  agent._maybe_consolidate(force=True, since=...)      private access
+  agent.consolidator.consolidate(force=True, since=...)

Consolidator gets the LLM as any object with run_one_shot, so memory/ never imports backends. New contract: other code imports memclaw.memory, not its modules; memory is a layer between tools and the core modules.

Evidence

  • Pinned behaviour, old vs new: a 20-scenario script (exact prompt and message, MEMORY.md written, .history/ copies and pruning, meta.json, failing backend leaves MEMORY.md and meta alone, empty reply, edits during a run, budget, corrupt meta, threshold/since, background schedule, 1-hour retry, one-at-a-time, shutdown cancel, system prompt, fingerprint, session hashes, log messages).
    Before (1d6d9c2) and after each commit: byte-identical output.
  • memclaw consolidate (with/without --since, bad date) through click with a fake backend: same output, exit codes and files; only the loguru source location changes.
  • code-shape: agent.py 699 → 348 lines; MemclawAgent 482 → 298 lines (under 300); private-access findings 1 → 0; baseline 39 → 35.
  • pytest: 514 passed, 20 skipped (was 501); new tests/memory/test_consolidation.py tests Consolidator without an agent. ruff, code-shape check clean.

Merge Danger

Door: two-way

Blast Radius: memory consolidation

  • Intended to be byte-for-byte the same behaviour; the pin script and moved tests cover it.
  • Module paths change (memclaw.memory_refs → memclaw.memory.refs; consolidation methods leave MemclawAgent). docs/memory-architecture.md updated.
  • Still open (not in this PR): a consolidation reply that isn't a valid MEMORY.md can still overwrite it if a backend returns bad text instead of raising; the guard belongs in Consolidator._replace_memory.
  • Stacked on refactor(bot): one message pipeline for all chat platforms (code-shape 2/5) #19; merge bottom-up.

🤖 Generated with Claude Code

…solidation.py

About 245 lines of MemclawAgent were MEMORY.md consolidation. They are now
one class, Consolidator, the only code that writes MEMORY.md. It owns the
prompt, the daily-text budget, the .history/ copies, the meta.json marker
and the background schedule. Its interface:

- consolidate(force=False, since=None): run it now. `memclaw consolidate`
  calls it through agent.consolidator instead of the private
  agent._maybe_consolidate (`since` was `consolidated_through_override`).
- schedule(): run it in the background after a reply (one at a time, an
  hour's pause after a failure).
- aclose() / cancel(): stop a background run at shutdown.

It reaches the LLM through the backend the agent hands it (anything with
run_one_shot), so it doesn't import memclaw.backends.

It lives in a new memclaw/memory/ package because the top-level folder is
full; the coming memory/ package move (store, index, search) joins it there.

Guards: a `memory` layer between tools and the core modules, and a new
contract that other code imports memclaw.memory, not the modules inside.
The concept "MEMORY.md + consolidation" now has memory/consolidation.py as
its home. Baseline tightened: agent.py is no longer over 500 lines,
MemclawAgent is 303 lines (was 482) with 17 attributes (was 18), and the
cli -> agent private access is gone.

Behaviour is unchanged: same prompt and message text, same files written,
same retry, logs and failure handling (a failed one-shot call still leaves
MEMORY.md and meta.json alone). The consolidation tests moved to
tests/memory/test_consolidation.py and test Consolidator on its own.
- instructions.py holds what a session is started with: the system-prompt
  template, loading AGENTS.md and MEMORY.md for it (with the 20,000-char
  cap), the content hash and instructions_fingerprint. agent.py keeps turn
  handling: the context block, handle() and the session bookkeeping
  (348 lines, was 699 before the consolidation move; MemclawAgent is under
  the 300-line class limit).
- memory_refs.py becomes memclaw/memory/refs.py, next to consolidation,
  its only user. That keeps the top-level folder at 13 modules while
  instructions.py is added.

Guards: instructions joins the core layer; memory.refs joins the modules
only memclaw.memory may import. Baseline tightened (MemclawAgent class
length, _handle's locals 19 -> 18).

Same template text, fingerprint, truncation and session hashes, so saved
sessions resume as before. Tests: tests/test_instructions.py (was part of
test_agent.py), tests/memory/test_refs.py (was test_memory_refs.py).
@emanuilo
emanuilo force-pushed the refactor/3-consolidation branch from 729be36 to 4756094 Compare November 22, 2026 19:27
@emanuilo
emanuilo changed the base branch from refactor/2-chat-pipeline to main November 22, 2026 19:28
@emanuilo emanuilo closed this Nov 22, 2026
@emanuilo emanuilo reopened this Nov 22, 2026
@emanuilo
emanuilo merged commit 2acd6bf into main Nov 22, 2026
5 checks passed
@emanuilo
emanuilo deleted the refactor/3-consolidation 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