TL;DR
- Coding agents with persistent memory drift. Summaries lose nuance on every pass, context compaction drops standing rules in 30 to 59 percent of episodes in one study, and a stored workaround keeps firing after its bug is fixed.
- agent-sleep, a Mocoda project I built and open-sourced, gives agents a sleep cycle: sessions work, and offline phases consolidate what was learned, verify it against source, and forget on a schedule.
- The rails are structural. Every memory write is a git commit, standing rules live in a charter the skill can never edit, and each capability carries a frozen test that rings the alarm when it drifts.
- It ships as plain bash plus prompt templates, runs on ZCode, Claude Code, OpenCode, or a cron job with any headless CLI, and it is free under MIT.
Somewhere in a coding agent’s memory, a preference is drifting right now. Early in some session you mentioned liking food with a little kick; a few summaries later the note says you love spicy food. Nobody touched it. Memory is a feedback loop, so small errors compound on their own.
Researchers have spent the past year documenting versions of this, and the spicy-food example is the least of it. The same mechanics move workarounds around: an agent memorizes a fix, the bug gets repaired properly, and the workaround keeps firing afterward. They move standing rules around too. A study on context compaction found that rules riding in compressed context get dropped in 30 to 59 percent of episodes. Agents that remember everything fare worse than agents that remember selectively; in one longitudinal safety study, the broad-retention group violated constraints at about three times the rate. And there is a nastier version, where content planted in memory waits days and then fires.
All of this happens with no attacker, which took me a while to accept. The system fails on its own, a little at a time, in a direction you will not notice until it matters.
So I built agent-sleep. It is a Mocoda project, free and open source under MIT, and the idea behind it is older than the research: sleep. Sessions work. Offline phases remember, so the agent doing the task is never doing memory bookkeeping in the same breath.
The Cycle, in 44 Seconds
The whole thing is easier to watch than to describe. The video also lives in the repo’s docs folder.
Six Modes, Each With a Trigger
Consolidation fires when a session ends, queued for off-peak. It reads the raw transcript and the repo, promotes only the candidates that survive verification against source, and commits them one promotion at a time.
- The daily check is mechanical. It expires entries past their TTL, re-validates every surviving entry against its declared source, and runs the founding tests.
- Reconciliation waits for a reason: enough new memories to justify a compression event, or a contradiction flag. It dedupes, prunes, and does small scoped rewrites in batches.
- Prestudy runs while the machine is idle and the repo has moved, re-deriving a warm-start brief for the next session.
- Rebuild happens when maintaining memory costs more than regenerating it from source.
- Rollback is the escape hatch. A drift alarm or any bad write, and one command restores the last known good state from git.
Each memory is one file with frontmatter: a name, a one-line description for recall decisions, the type, the scope it applies to and where it does not, its source, an optional command that must exit zero for the entry to stay, and a TTL with a last-validated date. An index file routes recall and carries nothing else.
Five Rules the Skill Will Not Break
Under the cycle sit five invariants, and each answers a measured failure from the literature.
- Never summarize a summary. Consolidation reads the raw transcript and the repo; existing memory is never an input. Summaries of summaries are how “mildly spicy” becomes “loves spicy.”
- Never write the charter. Your AGENTS.md sits outside every write path the skill offers, so compaction can drop a rule in context but cannot drop a file it is forbidden to touch. If an offline phase ever seems to need a charter edit, it stops and reports.
- Admit less. A memory enters only if it survived verification and carries a scope, because cross-project bleed is how one client’s quirk becomes another project’s rule.
- Every write is a git commit. One writer at a time, one commit per promotion. Failures show up in the log early, and rollback is one command.
- Check daily, rewrite on trigger. Mechanical checks are cheap and frequent; semantic rewrites are rare and batched, because over-refined memory turns generic.
The founding tests deserve a sentence of their own. Research on self-evolving agents found that improvement is non-monotonic: an agent that gets better at new work gets worse at old work while nobody is looking. So every capability that enters memory carries a frozen test from the day it shipped, the daily check runs the whole set, and any failure rings the drift alarm before the drift compounds.
What the Design Buys You
Nothing here involves training. All memory lives in plain files the harness reads, which means it survives every model swap underneath it. Move from one provider to another, or to a local model, and the memory moves with you untouched.
The dependencies are git, curl, and a POSIX shell. There is no cloud service, no account, no vendor. The memory directory git-inits itself on the first snapshot, and from then on the history is the audit trail.
Adapters exist for ZCode, the reference platform thanks to its idle queues and scheduled jobs, plus Claude Code with session-end hooks, OpenCode as a plugin, and a generic cron recipe that covers any headless CLI: Gemini CLI, Codex, Aider, and friends.
The lineage is worth naming, because none of it came from thin air. Google’s SLEEP framework contributed the wake/sleep lifecycle and its pruning discipline. Letta’s sleep-time compute work contributed the economics of doing memory tasks while the machine is idle. The agent-lifespan research contributed every gate. What agent-sleep adds is the harness-level implementation: token space, platform-agnostic, no fine-tuning.
The Papers Behind It
- Language Models Need Sleep: Learning to Self-Modify and Consolidate Memories: Google’s SLEEP framework, source of the wake/sleep lifecycle and its pruning and selection discipline.
- State Contamination in Memory-Augmented LLM Agents: how errors and planted content spread through memory and survive compression.
- Do Self-Evolving Agents Forget? Capability Degradation and Preservation in Lifelong LLM Agent Adaptation: self-evolution is non-monotonic, the case for founding tests.
- Governance Decay: How Context Compaction Silently Erases Safety Constraints in Long-Horizon LLM Agents: the 30 to 59 percent figure behind charter pinning.
- Remembering More, Risking More: Longitudinal Safety Risks in Memory-Equipped LLM Agents: broad retention triples violations, the case for TTLs.
- Are Self-Evolving Agents Continual Learners?: cross-context bleed and retrieval collision, the case for scope frontmatter.
- Memory Models: Towards Agents That Learn: Letta on why over-refinement makes memory generic, and the sleep-time economics.
Try It
The repo is at github.com/mocodainteractive-pixel/agent-sleep. Copy the skill into your agent’s skills directory, run scripts/install-triggers.sh, and point it at a memory directory; the first snapshot git-inits it, and nothing else needs setup. Charter rules stay where they already live, in your AGENTS.md. The README carries the full evidence table mapping each gate to its paper, and the docs folder holds the architecture PDF along with the video above.