feat: Replace context compaction with a hybrid Mastra(Code) Observational Memory
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 19.9k
- Forks
- 2.3k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 30
Description
Strategy (read this first)
Replace classic in-session compaction with a continuous observation + reflection loop (Mastra Observational Memory style), while keeping jcode’s long-term semantic memory graph unchanged.
- What gets replaced: one-shot “summarize older turns” compaction for the active context window
- What stays: the durable MemoryEntry graph (embeddings, cascade retrieval, supersedes/contradicts, ambient consolidation)
- How they connect: a promotion bridge extracts durable facts from the session observation log into the existing graph
This is a hybrid: OM-style context for the current session, existing graph for cross-session knowledge. Not a rip-out of long-term memory.
Summary
jcode already has a strong long-term semantic memory graph (see MEMORY_ARCHITECTURE.md and AMBIENT_MODE.md).
In-session context still uses classic one-shot compaction (summarize older turns, keep recent tail). That works, but long single sessions can still hit the usual “lossy summary cliff.”
Proposal: replace that compaction path with a continuous observation + reflection style inspired by Mastra Observational Memory, and keep a clear bridge that promotes durable facts into the existing graph.
These layers solve different problems and are complementary.
Problem
| Layer | Current | Gap |
|---|---|---|
| Long-term / cross-session | Semantic memory graph | Already strong |
| In-session context | Classic compaction | One-shot summaries can drop temporal detail, intermediate decisions, and priority signals |
Proposed hybrid
-
Session working context (replaces classic compaction)
Maintain an append-mostly observation log (dated, priority-tagged notes) instead of a single compaction summary.
Background Observer distills unobserved messages when a token threshold is hit; Reflector periodically condenses the log (merge, drop low-priority, keep high-value + temporal anchors).
Design reference: Mastra OM / research results / implementation. -
Long-term graph (unchanged role)
Existing MemoryEntry graph stays the durable store (Fact / Preference / Procedure / Correction, cascade retrieval, ambient garden). -
Promotion bridge
During observation / reflection / ambient cycles, durable items are extracted into normal graph nodes (provenanceObserved/Extracted). Transient session noise stays in the observation log and can decay under reflection. -
Prompt shape
- Stable observation prefix (cache-friendly)
- Recent unobserved messages
- Graph memories still injected via existing passive recall
Full raw history remains available for session search / RAG, same as today.
Why this fits jcode
- Fully async / non-blocking (reuse memory side-agent + ambient patterns)
- No need for a new vector/graph DB for the session layer (text observation log)
- Strengthens the existing graph with higher-quality, better-timed extractions
- Optional / feature-flaggable; classic compaction can remain as emergency fallback
- Aligns with current design docs: long-term graph vs in-session compaction are already separate concerns
Suggested phased approach
- Feature-flag observation log alongside current compaction
- Measure long-session coherence, token cost, cache behavior
- Add promotion rules into the graph
- Make observation-log the default compaction replacement; keep classic summary as hard fallback
References
- Ticket: https://github.com/1jehuang/jcode/issues/901
- jcode: https://github.com/1jehuang/jcode
- Memory architecture · Ambient mode
- Mastra OM: docs · research · blog · source · Mastra Code
Happy to refine the design or help prototype if this direction is interesting.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with docs/MEMORY_ARCHITECTURE.md and docs/AMBIENT_MODE.md, then compare the proposed session observation and reflection flow with the linked Mastra references. A complete implementation would preserve the existing durable memory graph, add the feature-flagged observation path and promotion bridge, and retain classic compaction as an emergency fallback.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- ai, cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100