@@ -11,6 +11,28 @@ import {
1111import { createMemoryCoreTestHarness } from "./test-helpers.js" ;
1212
1313const { createTempWorkspace } = createMemoryCoreTestHarness ( ) ;
14+ const DREAMING_TEST_BASE_TIME = new Date ( "2026-04-05T10:00:00.000Z" ) ;
15+ const DREAMING_TEST_DAY = "2026-04-05" ;
16+ const LIGHT_DREAMING_TEST_CONFIG : OpenClawConfig = {
17+ plugins : {
18+ entries : {
19+ "memory-core" : {
20+ config : {
21+ dreaming : {
22+ enabled : true ,
23+ phases : {
24+ light : {
25+ enabled : true ,
26+ limit : 20 ,
27+ lookbackDays : 2 ,
28+ } ,
29+ } ,
30+ } ,
31+ } ,
32+ } ,
33+ } ,
34+ } ,
35+ } ;
1436
1537function createHarness ( config : OpenClawConfig , workspaceDir ?: string ) {
1638 let beforeAgentReply :
@@ -55,7 +77,124 @@ function createHarness(config: OpenClawConfig, workspaceDir?: string) {
5577 return { beforeAgentReply, logger } ;
5678}
5779
80+ function setDreamingTestTime ( offsetMinutes = 0 ) {
81+ vi . setSystemTime ( new Date ( DREAMING_TEST_BASE_TIME . getTime ( ) + offsetMinutes * 60_000 ) ) ;
82+ }
83+
84+ async function withDreamingTestClock ( run : ( ) => Promise < void > ) {
85+ vi . useFakeTimers ( ) ;
86+ try {
87+ await run ( ) ;
88+ } finally {
89+ vi . useRealTimers ( ) ;
90+ }
91+ }
92+
93+ async function writeDailyNote ( workspaceDir : string , lines : string [ ] ) : Promise < void > {
94+ await fs . mkdir ( path . join ( workspaceDir , "memory" ) , { recursive : true } ) ;
95+ await fs . writeFile (
96+ path . join ( workspaceDir , "memory" , `${ DREAMING_TEST_DAY } .md` ) ,
97+ lines . join ( "\n" ) ,
98+ "utf-8" ,
99+ ) ;
100+ }
101+
102+ function createLightDreamingHarness ( workspaceDir : string ) {
103+ return createHarness ( LIGHT_DREAMING_TEST_CONFIG , workspaceDir ) ;
104+ }
105+
106+ async function triggerLightDreaming (
107+ beforeAgentReply : NonNullable < ReturnType < typeof createHarness > [ "beforeAgentReply" ] > ,
108+ workspaceDir : string ,
109+ offsetMinutes : number ,
110+ ) : Promise < void > {
111+ setDreamingTestTime ( offsetMinutes ) ;
112+ await beforeAgentReply (
113+ { cleanedBody : "__openclaw_memory_core_light_sleep__" } ,
114+ { trigger : "heartbeat" , workspaceDir } ,
115+ ) ;
116+ }
117+
118+ async function readCandidateSnippets ( workspaceDir : string , nowIso : string ) : Promise < string [ ] > {
119+ const candidates = await rankShortTermPromotionCandidates ( {
120+ workspaceDir,
121+ minScore : 0 ,
122+ minRecallCount : 0 ,
123+ minUniqueQueries : 0 ,
124+ nowMs : Date . parse ( nowIso ) ,
125+ } ) ;
126+ return candidates . map ( ( candidate ) => candidate . snippet ) ;
127+ }
128+
58129describe ( "memory-core dreaming phases" , ( ) => {
130+ it ( "does not re-ingest managed light dreaming blocks from daily notes" , async ( ) => {
131+ const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-phases-" ) ;
132+ await withDreamingTestClock ( async ( ) => {
133+ await writeDailyNote ( workspaceDir , [
134+ `# ${ DREAMING_TEST_DAY } ` ,
135+ "" ,
136+ "- Move backups to S3 Glacier." ,
137+ "- Keep retention at 365 days." ,
138+ ] ) ;
139+
140+ const { beforeAgentReply } = createLightDreamingHarness ( workspaceDir ) ;
141+ const candidateCounts : number [ ] = [ ] ;
142+ const candidateSnippets : string [ ] [ ] = [ ] ;
143+ for ( let run = 0 ; run < 3 ; run += 1 ) {
144+ await triggerLightDreaming ( beforeAgentReply , workspaceDir , run + 1 ) ;
145+ candidateSnippets . push (
146+ await readCandidateSnippets ( workspaceDir , `2026-04-05T10:0${ run + 1 } :00.000Z` ) ,
147+ ) ;
148+ candidateCounts . push ( candidateSnippets . at ( - 1 ) ?. length ?? 0 ) ;
149+ }
150+
151+ expect ( candidateCounts ) . toEqual ( [ 1 , 1 , 1 ] ) ;
152+ expect ( candidateSnippets ) . toEqual ( [
153+ [ "Move backups to S3 Glacier.; Keep retention at 365 days." ] ,
154+ [ "Move backups to S3 Glacier.; Keep retention at 365 days." ] ,
155+ [ "Move backups to S3 Glacier.; Keep retention at 365 days." ] ,
156+ ] ) ;
157+
158+ const dailyContent = await fs . readFile (
159+ path . join ( workspaceDir , "memory" , `${ DREAMING_TEST_DAY } .md` ) ,
160+ "utf-8" ,
161+ ) ;
162+ expect ( dailyContent ) . toContain ( "## Light Sleep" ) ;
163+ expect ( dailyContent . match ( / ^ - C a n d i d a t e : / gm) ) . toHaveLength ( 1 ) ;
164+ expect ( dailyContent ) . not . toContain ( "Light Sleep: Candidate:" ) ;
165+ } ) ;
166+ } ) ;
167+
168+ it ( "stops stripping a malformed managed block at the next section boundary" , async ( ) => {
169+ const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-phases-" ) ;
170+ await withDreamingTestClock ( async ( ) => {
171+ await writeDailyNote ( workspaceDir , [
172+ `# ${ DREAMING_TEST_DAY } ` ,
173+ "" ,
174+ "- Move backups to S3 Glacier." ,
175+ "" ,
176+ "## Light Sleep" ,
177+ "<!-- openclaw:dreaming:light:start -->" ,
178+ "- Candidate: Old staged summary." ,
179+ "" ,
180+ "## Ops" ,
181+ "- Rotate access keys." ,
182+ "" ,
183+ "## Light Sleep" ,
184+ "<!-- openclaw:dreaming:light:start -->" ,
185+ "- Candidate: Fresh staged summary." ,
186+ "<!-- openclaw:dreaming:light:end -->" ,
187+ ] ) ;
188+
189+ const { beforeAgentReply } = createLightDreamingHarness ( workspaceDir ) ;
190+ await triggerLightDreaming ( beforeAgentReply , workspaceDir , 1 ) ;
191+
192+ expect ( await readCandidateSnippets ( workspaceDir , "2026-04-05T10:01:00.000Z" ) ) . toContain (
193+ "Ops: Rotate access keys." ,
194+ ) ;
195+ } ) ;
196+ } ) ;
197+
59198 it ( "checkpoints daily ingestion and skips unchanged daily files" , async ( ) => {
60199 const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-phases-" ) ;
61200 await fs . mkdir ( path . join ( workspaceDir , "memory" ) , { recursive : true } ) ;
0 commit comments