Skip to content

Latest commit

 

History

History
66 lines (56 loc) · 3.75 KB

CONTEXT_MEMORY.md

File metadata and controls

66 lines (56 loc) · 3.75 KB

pit — active context management (delayed-batch memory)

Built 2026-07-04 around a MemGPT-variant design. Two complementary mechanisms in memory.py.

Why: the context is 90% stale tool output

Measured on a real 15-msg session: tool results = 90.3% of context (~22.6k of 25k tokens); a single grep hit the 30k-char cap. The always-on stack (system prompt + 12 tool schemas) is only ~3,057 tok — lean. The problem is tool dumps, re-sent in full every turn.

Mechanism 1 — mechanical stale-tool-result pruning (the acute fix)

prune_stale_tool_results(messages) — NO model. Collapses tool results older than the 3 newest, over 1,200 chars, into a one-line stub. Preserves each tool message's tool_call_id so the assistant tool_call it answers stays paired (API stays valid). Runs every step inside run_turn (loop.py, PRUNE_TOOL_RESULTS). Real result: 89k -> 30k chars, instant. The model can re-run a call if it needs the pruned output again.

Mechanism 2 — delayed batch memory

The pitch vs MemGPT: MemGPT pays a constant per-turn tax editing memory during the conversation. This pays it lazily, once, after the fact — a secretary taking raw notes during the meeting, organizing them afterward.

Trigger: the live buffer just accumulates; when it crosses 24,000 CHARACTERS (char count, not tokens — a deliberate choice), the REPL runs one batch pass.

Batch pass (batch_process, one local-model call):

  1. Mechanical-prune the buffer first (less noise for the model too).
  2. One completion (thinking disabled) with prompts/batch.md: given existing memory + the new conversation, return JSON with:
    • ops — Mem0-style ADD / UPDATE / DELETE / NOOP per item, so revisions and obsoletions don't pile up as duplicates. Each op targets a typed bucket: facts (stable truths), tasks (todo/doing/done), context (situational, may expire), decisions (choice + reason).
    • subjects — topic-keyed threads, datetime-stamped entries appended.
    • summary — a dense chunk summary.
  3. Apply ops to the store; append thread entries; merge the summary progressively + hierarchically (batch -> session -> rolling every 4 batches), so summaries don't degrade from repeated re-summarizing.
  4. Rebuild the live buffer = system + rendered memory + last few raw turns.

Retrieval is by subject thread, NOT vector search — relevant_threads() scores threads by keyword overlap with the current focus and injects the top few. ("Pull the squat-form thread," not a semantic embedding lookup.)

Storage (memory_store/<project>-<hash>/): store.json (buckets + threads + summary) and a human-readable MEMORY.md mirror. Keyed per project (cwd), so memory persists across sessions and the REPL loads it on start.

Verified (real 90k session, local Q4_K_P)

Batch pass: 89,423 -> 9,448 chars (89% smaller), extracting 8 correctly-typed items (financials->facts, 3 deadlined action items->tasks, run-rate goal+reason-> decisions, editing bottleneck->context) + 1 subject thread, all datetime-stamped.

Not yet built (next iterations)

  • Graph links between subjects (knee-injury -> can't-squat -> prefers-swimming). The planned final step; store has room (threads keys) but no edges yet.
  • /recall <subject> tool so the model can pull a specific thread on demand mid-turn (today threads are injected only at compaction).
  • UPDATE/DELETE exercise — only fires once buckets carry items across multiple batches; single-batch tests show ADD only. Needs a multi-batch session to prove dedup end to end.
  • Tune BATCH_CHARS / KEEP_RECENT_TURNS against real long REPL sessions.
  • Batch currently uses the local model; could offload to consult_oracle for a sharper extraction when the buffer is dense.