Skip to content

feat: add session memory tracking, proactive hints, and context recovery#150

Open
maplenk wants to merge 12 commits intoDeusData:mainfrom
maplenk:feat/session-memory
Open

feat: add session memory tracking, proactive hints, and context recovery#150
maplenk wants to merge 12 commits intoDeusData:mainfrom
maplenk:feat/session-memory

Conversation

@maplenk
Copy link

@maplenk maplenk commented Mar 26, 2026

Summary

Adds session-level memory that tracks what the agent touched during a session, enriches tool responses with proactive hints, and provides a summary tool for context recovery after compaction.

Stacked PR: This builds on #149 (Compound Queries) → #148#147. Please merge those first — this PR's unique changes are the 4 commits after PR 3's HEAD.

Motivation

GrapeRoot (graperoot.dev) benchmarked 13 points higher than a reactive graph tool by pre-loading relevant context based on session history. Their finding: "proactive context beats reactive querying." This PR brings session awareness to codebase-memory-mcp.

New tools

  • get_session_context — Returns files read, symbols queried, areas explored, and related_untouched: graph neighbors of touched nodes that haven't been examined yet, ranked by PageRank. Answers "what should I look at next?"

  • get_session_summary — Compact markdown summary of the entire session: files touched, symbols investigated, areas explored, key findings, suggested next steps. Designed for context recovery after Claude Code compacts its context window.

Enhanced tools (proactive hints)

5 existing tools now include a session_hint field when relevant:

  • Repeat query detection ("you already queried this symbol 5 queries ago")
  • Untouched neighbor suggestions ("OrderService callers include InvoiceService, not yet examined")
  • Edit conflict warnings ("this file was already edited this session")
  • High-PageRank untouched file nudges after 10+ queries

Implementation

  • In-memory session_state_t — hash sets for files/symbols/areas, initialized on first tool call, reset on server restart (matches MCP session lifecycle). Not persisted to SQLite.
  • Tracking hooks in existing tool handlers — lightweight set insertions, zero SQLite overhead.
  • related_untouched computed via 1-hop graph query on touched symbols, filtered by already-read files, ranked by PageRank.
  • Hints capped at ~200 tokens, skipped if response already near max_tokens.

Tests

18 new tests across phases 7A/7B/7C. Total: 2657 passing, ASan/UBSan clean.


Part 4 of a 4-PR series. Depends on #149#148#147.


Built with OpenAI Codex and Claude Code.

Naman Khator and others added 12 commits March 26, 2026 12:19
Account for optional signatures in the search_graph and trace_call_path size estimators, and improve compact trace chains to report omitted-node counts.

This also documents the normal-path output enrichment introduced with Task 4: search_graph results now include file_path, start_line, end_line, and signature, and trace_call_path hop items now include file_path, start_line, and signature.
Prefer non-test symbol matches before pagerank for ambiguous short names. Batch-load visited node metadata and pagerank scores to remove per-node query loops during impact analysis. Hide test counts from the public summary when include_tests=false, and add max_tokens truncation support to get_impact_analysis with MCP coverage.
Add the impact-analysis store API declaration, expose get_impact_analysis in CLI help text, and cover the tool with the existing integration fixture.
Phase 5: Three new compound MCP tools (explore, understand, prepare_change)
that bundle multiple graph queries into single-call responses:
- explore: area search with matches, dependencies, hotspots, entry points
- understand: symbol deep-dive with 3-tier resolution (exact QN, exact name
  with auto-pick, QN suffix with suggestions), callers, callees, source,
  connected symbols, is_key_symbol flag
- prepare_change: wraps impact analysis with review_scope (must_review,
  should_review, tests) and include_tests=false support
- All three support max_tokens budget with progressive truncation
- Wire qn_pattern in store search (completing pre-existing API contract)

Bug fixes across all committed phases:
- Fix REQUIRE_STORE leaking heap args in 5 handlers (get_key_symbols,
  get_impact_analysis, explore, understand, prepare_change)
- Fix markdown_builder_reserve infinite loop on OOM (NULL check after
  safe_realloc)
- Fix SQLite bind parameter limit in impact_fetch_nodes_with_scores
  (chunk into batches of 900)
- Fix cbm_mcp_text_result(NULL) crash on OOM (guard with empty string)
- Fix POSIX regex: remove invalid PCRE (?i) prefix from contains pattern
- Fix search degree filter: set min_degree/max_degree to -1 (disabled)
  in compound search helpers
- Fix summary_count_nodes returning -1 on SQL error (return 0 instead)
- Fix explore total_results overcounting unfiltered hotspots
- Fix qsort(NULL, 0) undefined behavior in explore
- Fix handle_understand early return leaking search outputs (use goto)
- Refactor handle_prepare_change to use goto cleanup pattern

Output enrichment (non-breaking):
- search_graph results now include start_line, end_line, signature
- trace_call_path hops now include file_path, start_line, signature

Tests: 2639 passed (+44 new), 0 failures

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Track files read/edited, symbols queried, areas explored, and impact
analyses across the MCP session. New get_session_context tool returns
accumulated state plus related_untouched: graph neighbors of touched
symbols ranked by PageRank that haven't been examined yet.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Enrich search_graph, explore, understand, prepare_change, and
get_impact_analysis responses with session-aware hints when actionable:
- Area re-exploration overlap with new/already-seen result counts
- Symbol re-query detection with unexamined neighbor suggestions
- Blast radius overlap with already-edited files warning
- High-PageRank untouched symbol suggestion after 10+ queries

Hints are budget-aware (skipped if response near max_tokens) and
non-breaking (optional session_hint string field in JSON response).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use HINT_SNPRINTF macro consistently across all hint builders and
initialize buf[0] to match sibling functions (post-review P2 fixes).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
New MCP tool returns a compact markdown summary of the session:
files touched (read/edited), symbols investigated with PageRank
enrichment, impact analyses run, areas explored, and suggested
next steps (unexamined graph neighbors ranked by PageRank).

Designed for context recovery after Claude Code compaction — call
get_session_summary to instantly restore the full session picture.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
maplenk added a commit to maplenk/codebase-memory-mcp that referenced this pull request Mar 26, 2026
All install paths, download URLs, self-update checks, CI workflows,
and documentation now reference maplenk/codebase-memory-mcp so the
fork can operate independently with its own releases while upstream
PRs (DeusData#147-DeusData#150) are pending. Upstream attribution in README fork
section and LICENSE preserved.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@DeusData DeusData added the enhancement New feature or request label Mar 26, 2026
@DeusData
Copy link
Owner

Thanks @maplenk — session memory and context recovery would improve the multi-session experience significantly. Will review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants