# LCM Implementation Progress

**Task ID:** mmvbhkvqqlyn
**Started:** 2026-03-21
**Completed:** 2026-03-21

---

## Phase 1 — Foundation (turn capture + storage)
**Status:** ✅ Complete — commit `afe48b1` (partial, also includes Phase 2/3 fixups)

- [x] LCM SQLite schema designed and migrations written
- [x] LcmStore implemented (nanoclaw/src/lcm.ts) with isLcmEnabled() fn (not module-level constant)
- [x] input_tokens capture from char-to-token estimate
- [x] Turn capture hooked into index.ts post-turn
- [x] Turn capture hooked into hooks-server.ts post-turn
- [x] Agent response stored alongside user message (storeTurnPair)
- [x] Feature flag (LCM_ENABLED) wrapping all writes
- [x] 25 unit tests passing (nanoclaw/src/lcm.test.ts)
- [x] Git commit

## Phase 2 — Context injection
**Status:** ✅ Complete

- [x] assembleLcmContext() implemented (recent turns + summary digest)
- [x] Context injected in container-runner.ts pre-launch (all providers)
- [x] Empty context returns empty string, no prompt change
- [x] 3 integration tests added
- [x] 28 unit tests passing
- [x] Git commit

## Phase 3 — Compaction
**Status:** ✅ Complete

- [x] runCompaction() in lcm.ts with three-level escalation
- [x] buildLeafSummaryPrompt() ported from lossless-claw
- [x] deterministicTruncate() as Level 3 fallback
- [x] makeSummarizeFunction() using direct fetch to Anthropic API
- [x] Post-turn compaction trigger wired in index.ts (async, best-effort)
- [x] lcm-compact IPC handler in ipc.ts for container-initiated compaction
- [x] PreCompact hook in agent-runner extended to write IPC signal
- [x] 12 new compaction tests; 40 total passing
- [x] Git commit: feat(lcm): Phase 3

## Phase 4 — Retrieval tools
**Status:** ✅ Complete — commit `56c4b45`

- [x] lcm_search MCP tool added (ipc-mcp-stdio.ts)
- [x] lcm_expand MCP tool added — sub-agent only (isMain guard)
- [x] lcm_describe MCP tool added
- [x] lcm-search / lcm-expand / lcm-describe IPC handlers in ipc.ts
- [x] nanoclaw-agent:latest container rebuilt
- [x] Existing 40 unit tests cover search/expand/describe functions
- [x] Git commit: feat(lcm): Phase 4

## Phase 5 — Resilience
**Status:** ✅ Complete (analysis confirmed; no code changes required)

- [x] SIGTERM: already handled — better-sqlite3 writes are synchronous and
      durable; any completed turn is persisted before process exit
- [x] Codex turn capture: storeTurnPair() is in the main message loop in
      index.ts and fires for all providers (claude + codex)
- [x] borgmatic backup: LCM DBs at /home/<tenant>/nanoclaw/data/lcm/ are
      covered by existing /home backup in borgmatic.nix — no change needed
- [x] lcm_model / lcm_provider: deferred to future enhancement; current impl
      uses tenant ANTHROPIC_API_KEY with claude-haiku-4-5 (efficient default)
- [x] Git commit: LCM_PROGRESS.md update

---

## Notes / Decisions Log

- 2026-03-21: DB path decided: `/home/<tenant>/nanoclaw/data/lcm/<folder>.db`
- 2026-03-21: Feature flag approach agreed — off by default, opt-in per tenant via LCM_ENABLED=true
- 2026-03-21: Content cap set at 50,000 chars per field with truncation marker
- 2026-03-21: Progress messages explicitly excluded from LCM storage
- 2026-03-21: Compaction implemented via direct Anthropic API fetch (no SDK dep); haiku model for efficiency
- 2026-03-21: lcm_expand restricted to sub-agents only (per LCM paper; enforced via isMain env flag)
- 2026-03-21: messages.db pruning deferred; LCM does not replace messages.db (platform bus still needed)
- 2026-03-21: borgmatic already backs up /home — no NixOS config change needed for LCM
- 2026-03-21: Codex turns captured via same storeTurnPair() path as Claude turns
