Skip to content

Commit 3e0e222

Browse files
HanchenHanchen
authored andcommitted
fix(memory-core): drop dreaming theme echoes from promotion candidates
Light Sleep and Deep Sleep phases write - Theme: `X` kept surfacing across N memories. into daily notes. The next dreaming cycle re-ingests just the first line as a fresh candidate before its indented status:staged and recalls fields land. The 5-field contamination check then misses it, so the theme echo gets promoted into MEMORY.md and the cycle keeps recursing. Real workspaces accumulate dozens of these per agent. Match the unique "kept surfacing across N memories" signature when a narrative lead is present, even without status/recalls. Add a regression test covering both the line-prefixed and bare-Reflections variants observed in production daily notes. Thanks @keane-751892
1 parent 82ca6ec commit 3e0e222

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎extensions/memory-core/src/short-term-promotion.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,19 @@ describe("short-term promotion", () => {
11011101
).toBe(false);
11021102
});
11031103

1104+
it("treats Light Sleep theme echoes as contaminated even without status/recalls", () => {
1105+
expect(
1106+
__testing.isContaminatedDreamingSnippet(
1107+
"Candidate: Reflections: Theme: `memory/2026-04-13.md` kept surfacing across 271 memories.; confidence: 1.00; evidence: memory/2026-04-13.md:73-76, memory/2026-04-13.md:53-56; note: reflection",
1108+
),
1109+
).toBe(true);
1110+
expect(
1111+
__testing.isContaminatedDreamingSnippet(
1112+
"Reflections: Theme: `kept` kept surfacing across 64 memories.",
1113+
),
1114+
).toBe(true);
1115+
});
1116+
11041117
it("treats transcript-style dreaming prompt echoes as contaminated", () => {
11051118
expect(
11061119
__testing.isContaminatedDreamingSnippet(

‎extensions/memory-core/src/short-term-promotion.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ const PHASE_SIGNAL_HALF_LIFE_DAYS = 14;
4040
const DREAMING_TRANSCRIPT_PROMPT_LINE_RE =
4141
/\[[^\]]*dreaming-narrative[^\]]*]\s*(?:User|Assistant):\s*Write a dream diary entry from these memory fragments:?/i;
4242
const DREAMING_DIFF_PREFIX_RE = /@@\s*-\d+(?:,\d+)?\s+[-*+]\s+/iy;
43+
// Light Sleep and Deep Sleep phases write `- Theme: \`X\` kept surfacing across
44+
// N memories.` lines into daily notes. Without filtering, the next dreaming
45+
// cycle re-ingests those rows as fresh candidates and promotes them again,
46+
// growing a self-referential feedback loop in MEMORY.md. Match the unique
47+
// "kept surfacing across N memories" signature so we can drop them even when
48+
// the snippet does not yet carry a status:staged or recalls field.
49+
const DREAMING_THEME_SIGNATURE_RE = /\bkept surfacing across\s+\d+\s+memor(?:y|ies)\b/i;
4350
const inProcessShortTermLocks = new Map<string, Promise<void>>();
4451
const ensuredShortTermDirs = new Map<string, Promise<void>>();
4552

@@ -285,6 +292,9 @@ function isContaminatedDreamingSnippet(raw: string): boolean {
285292
}
286293

287294
const hasNarrativeLead = hasDreamingNarrativeLead(snippet);
295+
if (hasNarrativeLead && DREAMING_THEME_SIGNATURE_RE.test(snippet)) {
296+
return true;
297+
}
288298
const hasConfidence = /\bconfidence:\s*\d/i.test(snippet);
289299
const hasEvidence = /\bevidence:\s*(?:memory\/\.dreams\/session-corpus\/|memory\/)/i.test(
290300
snippet,

0 commit comments

Comments
 (0)