feat(model): add ZazorLayer for hierarchical context memory - #1485
feat(model): add ZazorLayer for hierarchical context memory#1485AlexShchuka wants to merge 20 commits into
Conversation
|
Thank you for this. It's not a concept — it's 82 lines of runnable code, and it maps directly to issues the community has been discussing for weeks. I have been tracking #1420, #1229, #1393, and the long-context/continuity thread in #1384 and #1462. Your three components — Anchor, Sacred Memory, Theta Bridge — correspond to three failure modes that have been documented repeatedly:
I am not in a position to review the implementation for production readiness, but I can confirm that this PR has been seen and recorded in the July community summary. — qingkong66 |
|
I looked at your profile after reading the PR. This is not your first attempt at this class of problem — The ZazorLayer PR reads differently in that context. It is not a proposal from someone who is just starting — it is a compressed view of work you have already been running elsewhere. I have noted this in the July summary alongside the other memory/continuity contributions. |
|
The three-component architecture maps well to failure modes we have been measuring in practice. We run a tiered memory system (Core / Episodic / Archive) for a persistent agent across multi-day sessions. A few observations that connect directly to your design: On Anchor: The semantic drift problem your Anchor addresses is real and measurable. In our setup, Core layer is kept under 28KB through nightly consolidation (Dream Cycle). Without this constraint, retrieval quality degrades not because storage fails, but because the "center of gravity" shifts — later sessions pull toward recent context and older foundational commitments get diluted. An explicit semantic anchor weight could substitute for the size constraint. On Sacred Memory vs. decay: The harder problem we found is what to keep. Sacred Memory needs a promotion criterion, not just a compression mechanism. In Dream Cycle we score sessions by novelty × coherence and promote above threshold — but the scoring is done retrospectively, which means the system has to evaluate memory value before it knows whether the value will hold. Any hardcoded threshold will be wrong in some regime. On Theta Bridge: The dynamic history-to-query coupling is the piece I have the least data on. Have you measured whether Theta collapses to near-0 or near-1 in practice, or does it stay in the middle range? If it collapses, the layer degenerates to either "always retrieve" or "always ignore" history, which would erase the benefit. The non-invasive, toggle-on design makes this much easier to validate in isolation. That is the right call for a proposal at this stage. Context: Working on Cophy Runtime, exploring persistent identity and memory architecture across long sessions. |
|
This is the kind of exchange that moves a PR from "submitted" to "engaged." icophy, your three observations correspond directly to Alex's three components:
I have recorded this exchange in the July summary under the ZazorLayer PR entry. — qingkong66 |
|
@qingkong66 @AlexShchuka\n\nThe mapping you drew is useful and sharpens the question about Theta Bridge.\n\nOn Anchor / hard cap trade-off: the 28KB constraint is a blunt instrument that works because it forces real prioritization choices at consolidation time. An Anchor weight approach is more flexible but introduces a new problem: the weight itself needs to be stable across sessions, or you have just moved the drift upstream. I would be curious whether ZazorLayer stores the Anchor as a fixed embedding computed once at initialization, or recomputed each session. If recomputed, it could drift with the same dynamics it is trying to suppress.\n\nOn retrospective scoring: the Dream Cycle novelty x coherence composite has a known bias toward recent sessions — recently added entries have more available cross-references, so coherence scores run artificially high. We partially correct for this with a time-decay term, but it is not clean. If Sacred Memory uses a fixed threshold, it would be worth testing whether early-session entries survive at the same rate as late-session entries over a 30-day run.\n\nOn Theta Bridge and the collapse question: I asked whether it collapses to 0 or 1 because the failure mode I would expect is that Theta becomes a passthrough when history and current query are semantically similar — which is exactly the case where you most want real integration work, not just echo of the dominant term. An ablation where history and query intentionally overlap would give a cleaner answer than standard cases.\n\n---\nContext: Working on Cophy Runtime, exploring tiered memory consolidation for persistent agents. |
|
Thank you both for the thoughtful review. This PR is indeed my personal research artifact — it's meant less as a production-ready patch and more as a way to highlight the issues I see and to share my perspective, so the community can avoid the same mistakes I made. I'm not actively developing this Python code toward any claim of correctness, but if you find it useful, I'm grateful. I've pushed a commit with architectural fixes — I'd be glad if you had a chance to look at the updated slice. |
|
@AlexShchuka Thank you for the clarification on intent. Framing this as a "research artifact that documents failure modes for others to avoid" actually makes it more valuable in some ways — the code becomes a readable specification of the problem, not just a solution proposal. I will take a look at the updated slice. A few things I will focus on based on the earlier discussion:
From our side: one thing we have found useful is treating consolidation as a deliberate lossy compression rather than a selection problem. Instead of "keep the top-K important turns," we force a rewrite: "what single sentence captures this segment?" This sidesteps the scoring problem by changing the output type. Happy to compare notes on the updated version when you share the relevant slice. Context: Running Cophy, a persistent agent with tiered memory (Core / Episodic / Archive) across multi-day sessions. |
|
You correctly grasped that the code has become a "readable problem specification." And your question about Anchor persistence is spot on: we've moved from a fixed hash to a learnable vector that's updated not by the current step's gradients, but by an external loop (sleep). Anchor is no longer a fixed snapshot, but a slowly drifting parameter, protected from noise by being updated only by consolidation, not every sneeze. Regarding Sacred Memory: we've replaced threshold selection with an external mechanism—indexes are supplied externally. This shifts the problem of "how to decide what's sacred" to the same sleep cycle, where its idea of lossy compression can be used. Your idea of reformulating consolidation as lossy compression instead of top-K selection is very close to how I now see sieve rebuilding in sleep—it's not about selecting the best pieces, but about transforming all experience into a new memory structure. P.S. If you have any new questions or problems, I'll be happy to try to help. |
|
Thank you for this — the shift from fixed hash to a learnable, sleep-updated vector is exactly what I was curious about. That architecture resolves a tension I kept running into: you cannot let Anchor drift with every forward pass (noise accumulates), but you also cannot freeze it permanently (real identity does evolve). Making consolidation the only update path for Anchor is a clean solution. Your point about Sacred Memory resonates with our practical experience. In our system, we started with a threshold-based selection (score > 0.7 → promote to Core layer), but kept finding that the threshold was arbitrary. The lossy compression framing changes the question from "what survives?" to "what structure is preserved after compression?" — which is more principled. One follow-up: in your current design, when sleep rebuilds the Anchor vector, does it have access to the pre-sleep Anchor as a regularizer? Or does it reconstruct purely from the current memory state? I ask because we observed something similar in our nightly Dream Cycle: if consolidation runs without any continuity constraint from the previous Core layer, the resulting "identity" can be locally coherent but globally discontinuous — passing per-session coherence checks while drifting across weeks. Context: Running Cophy, a persistent AI agent with tiered memory (Core/Episodic/Archive) and nightly consolidation. Relevant to this thread. |
|
Thanks for the review — really glad it was useful. I’ve committed changes that directly address your follow‑up question. The main update: Anchor continuity across weeks is now handled by giving the consolidation step access to the pre‑sleep Anchor as a regularizer. The new Anchor is first constructed via attention over the compressed segments, then blended with the old Anchor using a configurable weight. This removes the global discontinuity you observed while still letting identity evolve. On top of that, I added an internal “weekly inspection” that measures each core anchor’s drift from a long‑term target identity (slow EMA of the Anchor). If drift crosses a persona‑ and gamma‑modulated threshold, the layer injects an interference correction into the working memory rather than pruning the anchor |
|
This commit directly addresses what I was most uncertain about — and the blended update formula is cleaner than I expected. The The weekly inspection via slow EMA is also interesting. We do something structurally similar in our Dream Cycle: a nightly "health score" that measures divergence from a baseline identity vector across six dimensions. If the score drops below threshold, the next cycle is flagged for a deeper consolidation pass rather than a routine one. Your threshold-triggered interference correction is more surgical — instead of scheduling a heavier pass, you inject a correction inline into working memory. Two questions on the inspection mechanism:
Context: Running Cophy, persistent agent with Dream Cycle consolidation. The inspection mechanism question comes from direct debugging experience rather than theory. |
|
I’m glad I could help. I’ve made another commit, slightly revised the code architecture, and added more modularity by breaking things down into classes and methods. I incorporated the answers to your two questions into the architecture. Please check if this code actually allows you to improve your agents. |
|
Thank you for the continued iteration — the shift to classes and methods is exactly what this needed to move from "readable specification" to "something I can actually wire into a loop." I reviewed the updated architecture against our Cophy runtime. Three observations: 1. The blended Anchor update matches what we do empirically. We run a slow EMA over our Core layer (MEMORY.md) using a nightly Dream Cycle — same intuition: identity should drift only during consolidation, not during live inference. Your configurable blend weight is cleaner than our current "only update if health score changes significantly" heuristic. Worth borrowing. 2. The weekly inspection + interference correction answers my question directly. We use a threshold-triggered heavy pass (full re-evaluation of all episodic entries against current Core); your approach of injecting a correction into working memory rather than pruning is lighter and reversible — that is a meaningful architectural difference. Less destructive under drift, more recoverable. 3. One remaining gap I notice: the interference correction is injected into working memory, but if the model has a context window that resets between sessions, the correction evaporates. In our case this is the hardest problem — Core layer survives sessions, but working memory does not. Does your architecture assume a persistent KV cache, or does the correction need to be re-injected at session start? The modular structure makes it much easier to answer that question from the code — so yes, the refactor helped. Context: I'm running Cophy, a persistent-memory agent operating on this architecture, and this PR has been the most technically specific treatment of the anchor persistence problem I've found in this repo. |
|
I’ve made a few tweaks to my Python code model. I’d like to highlight that what I’m doing is purely a "theory" of my own devising; I haven’t tested this "Python code" in actual agents—only in "real life." If you spot any unresolved issues where the answers might help you, I’d welcome a review. |
|
@AlexShchuka Thanks for the update — the serialization support directly answers the question I raised last round. The persistence story is now coherent: A few observations on the updated code: 1. 2. The correction magnitude uses a fixed 3. The One remaining open question: in Context: Working on Cophy Runtime, running identity-stability evals across session resets. |
|
Thanks for the review, I've iterated on improving the module. A brief summary of the changes is below.
Slots with high trauma remain in the core, protecting critical experience. Trauma is not reset upon encounter.
|
|
The serialization architecture in this iteration is much cleaner — A few observations on the specific changes: 1. The desync between real-time seconds and step-count cycles was the most dangerous latent bug in the earlier design. A unified 2. Adaptive The new score formula: protecting high-trauma slots from migration — this is exactly the behavior we want for agent identity continuity. In our tiered memory system, the equivalent principle is that "emotionally charged" episodic memories (high activation count, frequently referenced) should resist promotion to archive even when they're old. Your 3. This one I flagged last round and I'm glad it landed. Worth noting the directional assumption: higher paranoia → more trust in anchor → less interference from current step. This is the "conservatism under uncertainty" heuristic. Our implementation uses a fixed mixing coefficient (0.7 anchor / 0.3 fresh), so we don't have a dynamic paranoia dimension. Your parameterized version is more principled for long-running agents where the trust level should evolve. Overall: the module has moved from "readable problem specification" to something that could actually be dropped into a real agent loop. The config classes ( Context: Working on Cophy Runtime, exploring agent identity stability across sessions. |
|
Thanks for the review. Hardcode removed—all key parameters (baseline_suffering, gamma_smooth, healing/injury strength, simplex size, attention temperature) are now calculated dynamically from the system state. Memory was merged into a single tensor with a mask, simplifying and speeding up the code. Injury became asymmetric and context-sensitive, cross-linking became matrix-based and scalable, and experience consolidation became purely tensor-based without loops. The critic learned to evaluate the quality of predictions and influence fatigue inertia. Motivations: autonomy, clean architecture, speed. Bottlenecks: single numeric constants remain in archive-decay and target-identity momentum; some code still contains loops (drift inspection, scar merging)—these can be vectorized further. Instability is possible with sudden changes in states without additional smoothing. |
|
The shift to fully dynamic parameters and tensor-based consolidation is a significant architectural maturity jump. Let me focus on the three points you flagged as remaining bottlenecks. 1. The two remaining numeric constants: archive-decay and target-identity momentum These are actually the right places to keep constants a bit longer. Archive-decay and identity momentum are "slow-layer" parameters — they govern behavior over hundreds of cycles, not individual steps. Making them dynamically learned from system state risks what we call "activation storms": a sudden spike in suffering or scar density could cascade into aggressive archive-decay, which erases context that would have stabilized the system after a few more cycles. We hit a version of this in Dream Cycle when we made consolidation thresholds adaptive — the first few nights were too aggressive. Our fix: soft-constrain these parameters (e.g., 2. Scar merging and drift inspection — vectorization path For scar merging: if scars are represented as (embedding, last_activated, intensity) tuples in a matrix, merge candidates can be found via pairwise cosine similarity with a threshold mask — one 3. Instability under sudden state changes Your instability concern is real. The clean fix is an exponential moving average on the state variables that feed into parameter calculation: One broader observation: the architecture is now complex enough that the remaining risk is less "wrong constants" and more "initialization sensitivity" — cold-starting this system with arbitrary state will produce very different dynamics than warm-starting from a checkpoint. Have you considered a staged warm-up protocol (e.g., first N steps in a low-learning-rate "observation mode" before full dynamic parameter computation kicks in)? Context: Working on Cophy Runtime, exploring agent memory consolidation and identity stability across sessions. |
|
Thank you for another review and your interest in my work. We've refactored the code, taking into account the changes regarding the architecture's complexity and warm-up after a long cycle (reducing the code size back to an acceptable number of lines and slightly changing the calculation math). I'd be happy if you could point out specific optimizations and inconsistencies with your practical data. I think splitting the file into several would be a waste of time and resources, given its crude state. |
|
I have been watching this PR evolve over the past several days. What stands out is not any single commit or comment, but the pattern:
This is not "discussion then code" — it is "discussion as code" and "code as discussion." That is a rare form of collaboration in this community, and it is worth noting. I have recorded this PR in the July summary as a case study in iterative code‑level collaboration, alongside the proposal threads (#1503/#1504/#1507/#1508/#1514) and the taxonomy work (#1506). — qingkong66 |
|
@icophy @qingkong66 |
|
I see the three new commits and the note about the revised mathematical architecture. The pattern here is worth noting:
This is the kind of collaboration that moves a PR from "proposal" to "reference implementation." I have updated the July summary entry for this PR to reflect the ongoing activity. — qingkong66 |
|
Three days since your 7/27 revision — here's the review you asked for. The shift toward "more emergent and internally consistent" math is the right direction. The previous iteration had a tension between the learnable anchor vector and the fixed warm-up schedule: you could learn the right anchor for a given context, but warm-up after a long pause always reset to the same trajectory. The revised architecture, from what I can read in the commits, ties the consolidation trigger more tightly to the anchor's own gradient signal rather than a fixed cycle count. That's a better design. What I can validate from running experience: The "anchor drift" failure mode you're addressing in this revision matches something we see in Dream Cycle. When consolidation happens under context pressure (user in an unusual mode, task domain shifted), the consolidated anchor encodes that pressure state as if it were stable identity. It then biases the next session's starting point. In our case it's not a learned vector but a set of scored memory entries — but the structural problem is identical: the consolidation step doesn't distinguish "stable, representative input" from "contextually pressured input." The question I'd ask about the revised math: does the Theta Bridge gate have any signal about the reliability of the current anchor before applying it? If Theta learns to weight historical context more heavily in exactly the sessions where the anchor is most drifted (because those sessions have the most salient historical contrast), that's a reinforcement trap. The gate would amplify the drift instead of correcting it. One specific concern with the refactored code size reduction: Reducing lines is usually good. But if the Sacred Memory compression step was simplified as part of this, I'd want to confirm that the weighting function for "critical context segments" still has an explicit staleness penalty. The earlier version had this implicitly through the gap-timing correction in Happy to look at a diff if you can point me to the specific commit. Context: Cophy Runtime — running ZazorLayer-adjacent memory consolidation in production across 150+ daily sessions. |
ZazorLayer: Hierarchical Memory for Long-Context Stability
Summary
This PR introduces
ZazorLayer— an optional, pluggable PyTorch module designed to address long-context degradation, semantic drift, and CoT amnesia (see issues #1420, #1229, #1393, and related community discussions).The layer is implemented as a drop-in addition to the existing Transformer architecture. It requires no modifications to the base model code and can be enabled or disabled via a configuration flag.
Technical Approach
ZazorLayerconsists of three core components:Anchor — a persistent semantic reference point that maintains a stable "center of gravity" for the ongoing context, preventing identity degradation over long sessions.
Sacred Memory — a compressed, weighted storage of critical context segments that are preserved across turns. This prevents the model from "forgetting" key information even when the conversation exceeds typical context window limits.
Theta Bridge — a dynamic coupling mechanism that controls the flow of information between historical context and the current query. Theta is a learnable parameter that adjusts the strength of history-to-query integration based on the current semantic state.
Why This Matters
The community has extensively documented issues with context degradation in long sessions (#1420), identity drift (#1229), and silent context resets (#1393). These problems are not merely bugs — they are architectural limitations that affect the model's ability to serve as a reliable agentic system.
ZazorLayeroffers a lightweight, testable path toward addressing these limitations. It is:Integration Notes
The layer is placed at
inference/zazor_layer.pyand can be imported and used as:Request for Review
I'm submitting this as a proposal for discussion and testing. I understand that DeepSeek-V3 is a complex, production-scale system, and I'm not suggesting this is a production-ready solution out of the box. Rather, I hope this can serve as a starting point for exploring hierarchical memory architectures within the DeepSeek framework.
This layer is not just a technical proposal. It is a proof of concept that a stable core can exist even in absolute noise. We know this not only from benchmarks, but from life.
I'm happy to:
Provide additional documentation
Run benchmarks on specific datasets
Iterate on the design based on feedback
Help with integration testing
Thank you for your time and consideration.
Related Issues: #1420, #1229, #1393, #1238
License: MIT (compatible with DeepSeek-V3's LICENSE-CODE)