Which Memory Edits Can an Agent Apply Without You? Inside agentdream's --safe Policy

2026-12-05来源 dev.toEnglish

Letting an agent rewrite its own memory with nobody watching sounds like a bad idea. Reviewing every tiny edit by hand is the reason nobody cleans their memory file at all.

agentdream is an open source tool that runs "dreams" over an agent's memory: it reads your notes (for example Claude Code's CLAUDE.md) plus recent session transcripts, and proposes a cleaned up store. By default nothing is applied until you accept it. Version 0.1.1 added a middle path called --safe: apply the boring edits automatically, keep the risky ones for a human. This post looks at where that line is drawn, the code that draws it, and the bug the project found in its own "safe" bucket.

The analogy: a bank's auto-approve limit

A bank does not send every card payment to a human reviewer. A coffee goes through on its own. A large transfer to a new account gets held. The limit is not about how confident the system feels. It is about what kind of transaction it is and how bad a mistake would be.

agentdream's --safe works the same way. It sorts each proposed change by what the change is based on, then auto-approves only one class.

Four kinds of change, two kinds of evidence

A dream produces operations. Each has a type, a confidence and a stable id. The Claude Code doc in the repo (docs/CLAUDE_CODE.md) lists three modes for the hook that runs after your sessions end:

Mode Flag Applied without you Left staged for review
Review everything (none) nothing every operation
Safe auto-update --safe duplicate merges and stale-value replacements at confidence >= 0.75 corrections, stance reversals, insights
Apply everything --apply every operation nothing

The reasoning, quoted from the same doc:

A merge or a replacement only reorganises what your notes already say: the same sentence twice, or two dated values where the newer one wins. A correction or a reversal rewrites a note on the strength of what a transcript appears to show, and an insight adds a note nobody wrote, so those wait for a person.

So the split is about the source of truth. A merge and a replacement can be checked against the notes alone. A correction depends on reading a transcript correctly, which is exactly where a tool (or a model) can be wrong.

Turning it on

Install the hook in a project:

pip install agentdream cd ~/code/myrepo agentdream install-claude-hook --every 3 --safe 

After every third session a background dream runs. The doc says --safe is short for:

--apply --apply-types merge,replace --apply-min-confidence 0.75 

You can see that expansion in src/agentdream/cli.py:

apply_types = _types(args.apply_types) or (["merge", "replace"] if args.safe else None) min_conf = args.apply_min_confidence if args.apply_min_confidence is not None else (0.75 if args.safe else None) applying = bool(args.apply or args.safe) 

If you prefer config over flags, the same policy lives in .agentdream.toml:

[hook] every = 3 apply = true apply_types = ["merge", "replace"] apply_min_confidence = 0.75 

And it works by hand on any dream, no hook needed:

agentdream accept <job-id> --types merge,replace --min-confidence 0.75 

The whole policy is 16 lines

The function that decides is policy_split in src/agentdream/review.py. It returns the ids that meet the policy and the ids that do not:

def policy_split(ops: list[Operation], types: Selection = None, min_confidence: float | None = None ) -> tuple[list[str], list[str]]: """(ids that meet the policy, ids that don't). ``types=None`` allows every type and ``min_confidence=None`` every confidence, so no policy means everything passes.""" allowed = parse_types(types) if types is not None else None if types is not None and not allowed: raise ReviewError("--types was given but names no operation type (omit it to allow every type)") if min_confidence is not None and not 0.0 <= float(min_confidence) <= 1.0: raise ReviewError(f"min confidence must be between 0 and 1, got {min_confidence}") ok: list[str] = [] rest: list[str] = [] for o in ops: good = (allowed is None or o.type in allowed) and (min_confidence is None or o.confidence >= float(min_confidence)) (ok if good else rest).append(o.id) return ok, rest 

Two details are worth copying into your own tools. An empty type list is an error, not "allow everything". And a confidence outside 0 to 1 is rejected instead of silently clamped. A policy that fails loudly is better than one that quietly approves all.

What happens around the write

The policy is only half of "safe". The doc lists what an automatic update does and does not do:

  • A backup of the store is taken before every write, in .agentdream/jobs/<job-id>/backups/, and agentdream restore <job-id> --jobs-dir .agentdream/jobs puts it back.
  • The job is marked partially_accepted, and hook.log says how many operations were applied and how many were left staged.
  • If the memory store changed while the dream ran, nothing is applied.
  • If no operation meets the policy, nothing is written and the dream stays pending.
  • An operation that depends on a staged one is staged with it, so the store is never left half-updated.

The hook worker in src/agentdream/claude_hook.py logs the outcome either way:

meets, below = policy_split(job.operations, types, min_conf) if not meets: log(f"worker: none of the {len(below)} operation(s) meet the auto-apply policy{policy}; nothing " f"applied. Review: {review}") else: res = accept(job, types=types, min_confidence=min_conf) 

The bug in the safe bucket

Here is the part I like most. The project wrote a benchmark (agentdream benchmark) that builds a memory store where every defect is known: duplicates, stale values, wrong notes, reversed preferences, plus controls that must not change. Running it against the published 0.1.0 exposed a false merge.

From docs/BENCHMARK.md: the duplicate rule accepted any two notes with enough shared words. Ten notes shaped like The on-call rotation for <team> changes every Monday differ in two words out of nine, so nine of them were folded into one, at confidence 0.85. Only 2 of 10 unrelated notes survived.

Look at that confidence again. 0.85 clears the 0.75 bar, and merge is an auto-applied type. As the doc puts it, "a merge is exactly what the safe policy applies without review". The threshold alone would not have saved you.

The fix in 0.1.1 is a guard in src/agentdream/engine/deterministic.py. Two notes that line up word for word except for a swap of unrelated words are never duplicates:

ta, tb = token_set(a.content), token_set(b.content) if _substituted(a.content, b.content, ta, tb): return False # same sentence about a different thing ("... for the billing queue" / "... for the mail queue") j = jaccard(ta, tb) if j >= 0.75: return True 

The regression tests in tests/test_auto_apply.py pin both sides. These must not merge:

["Alice owns the billing dashboard and reviews its alerts weekly.", "Bob owns the billing dashboard and reviews its alerts weekly."], 

And these still must:

["Run the test suite with `pytest -q` before committing.", "Run the tests with `pytest -q` before every commit."], 

What safe mode buys you, in numbers

The benchmark scores the store three ways: before, after every operation, and after only the safe ones. On the default workload (80 facts, 130 notes, 50 sessions, deterministic backend), docs/BENCHMARK.md reports:

Metric Before All operations Safe auto-apply
Facts answered (one value, the true one) 50/80 80/80 60/80
Facts clean (answered, in a single note) 20/80 70/80 50/80
Unrelated notes kept verbatim 10/10 10/10 10/10

Safe mode fixes duplicates and stale values and leaves the 20 transcript-based rewrites staged. The repo is honest about the limit: when a model was asked questions from the notes (agentdream benchmark --ask claude-cli), it scored 60/80 both before the dream and after safe auto-apply. A capable model already picks the newer of two dated values. What safe mode bought there was a smaller store, 13% fewer tokens on every session. The answers only changed when the corrections were accepted by a person.

The docs also say these numbers are an upper bound, because the benchmark notes are template-generated plain English.

Takeaways

  • Decide auto-apply by what a change is based on, then add a confidence threshold. Confidence alone is not a safety rule.
  • agentdream's --safe applies only merge and replace at confidence >= 0.75. Corrections, reversals and insights wait for you.
  • Every automatic write takes a backup first, and agentdream restore <job-id> undoes it.
  • Put controls in your benchmark. The false merge was only visible because some notes were supposed to stay untouched.
  • Safe mode keeps memory tidy and smaller. Fixing wrong notes still needs a review, so run /dream or agentdream show <job-id> now and then.

Repo: github.com/mvplab-ai/agentdream

原文:https://www.a8q9.cn/en/article/which-memory-edits-can-an-agent-apply-without-you-inside-agentdream-s-safe-polic-6d568f.html

相关文章