@@ -73,6 +73,11 @@ function expectLogExcludes(source: MockCallSource, text: string): void {
7373 expect ( logIncludes ( source , text ) , `Expected log not to include ${ text } ` ) . toBe ( false ) ;
7474}
7575
76+ async function flushNarrativeSettleTimers < T > ( operation : Promise < T > ) : Promise < T > {
77+ await vi . runAllTimersAsync ( ) ;
78+ return operation ;
79+ }
80+
7681async function expectPathMissing ( targetPath : string ) : Promise < void > {
7782 const accessResult = await fs
7883 . access ( targetPath )
@@ -683,6 +688,88 @@ describe("generateAndAppendDreamNarrative", () => {
683688 expect ( logger . info ) . toHaveBeenCalled ( ) ;
684689 } ) ;
685690
691+ it ( "waits for persisted assistant text before falling back" , async ( ) => {
692+ vi . useFakeTimers ( ) ;
693+ try {
694+ const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-narrative-" ) ;
695+ const subagent = createMockSubagent ( "" ) ;
696+ subagent . getSessionMessages
697+ . mockResolvedValueOnce ( {
698+ messages : [ { role : "user" , content : "prompt" } ] ,
699+ } )
700+ . mockResolvedValueOnce ( {
701+ messages : [
702+ { role : "user" , content : "prompt" } ,
703+ {
704+ role : "assistant" ,
705+ content : [ { type : "text" , text : "The delayed diary text finally settled." } ] ,
706+ } ,
707+ ] ,
708+ } ) ;
709+ const logger = createMockLogger ( ) ;
710+
711+ const operation = generateAndAppendDreamNarrative ( {
712+ subagent,
713+ workspaceDir,
714+ data : {
715+ phase : "light" ,
716+ snippets : [ "The narrative assistant persisted after the run completed." ] ,
717+ } ,
718+ nowMs : Date . parse ( "2026-04-05T03:00:00Z" ) ,
719+ timezone : "UTC" ,
720+ logger,
721+ } ) ;
722+ await flushNarrativeSettleTimers ( operation ) ;
723+
724+ expect ( subagent . getSessionMessages ) . toHaveBeenCalledTimes ( 2 ) ;
725+ expect ( subagent . getSessionMessages ) . toHaveBeenNthCalledWith ( 1 , {
726+ sessionKey : expect . stringContaining ( "dreaming-narrative-light-" ) ,
727+ limit : expect . any ( Number ) ,
728+ } ) ;
729+ expect ( subagent . getSessionMessages ) . toHaveBeenNthCalledWith ( 2 , {
730+ sessionKey : expect . stringContaining ( "dreaming-narrative-light-" ) ,
731+ limit : expect . any ( Number ) ,
732+ } ) ;
733+ const content = await fs . readFile ( path . join ( workspaceDir , "DREAMS.md" ) , "utf-8" ) ;
734+ expect ( content ) . toContain ( "The delayed diary text finally settled." ) ;
735+ expect ( content ) . not . toContain ( "A memory trace surfaced" ) ;
736+ expectLogExcludes ( logger . warn , "produced no text" ) ;
737+ } finally {
738+ vi . useRealTimers ( ) ;
739+ }
740+ } ) ;
741+
742+ it ( "falls back after settled assistant text never appears" , async ( ) => {
743+ vi . useFakeTimers ( ) ;
744+ try {
745+ const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-narrative-" ) ;
746+ const subagent = createMockSubagent ( "" ) ;
747+ const logger = createMockLogger ( ) ;
748+
749+ const operation = generateAndAppendDreamNarrative ( {
750+ subagent,
751+ workspaceDir,
752+ data : {
753+ phase : "light" ,
754+ snippets : [ "The narrative assistant never persisted text." ] ,
755+ } ,
756+ nowMs : Date . parse ( "2026-04-05T03:00:00Z" ) ,
757+ timezone : "UTC" ,
758+ logger,
759+ } ) ;
760+ await flushNarrativeSettleTimers ( operation ) ;
761+
762+ expect ( subagent . getSessionMessages ) . toHaveBeenCalledTimes ( 5 ) ;
763+ const content = await fs . readFile ( path . join ( workspaceDir , "DREAMS.md" ) , "utf-8" ) ;
764+ expect ( content ) . toContain (
765+ "A memory trace surfaced, but details were unavailable in this run." ,
766+ ) ;
767+ expectLogIncludes ( logger . warn , "produced no text" ) ;
768+ } finally {
769+ vi . useRealTimers ( ) ;
770+ }
771+ } ) ;
772+
686773 it ( "retries with the session default when the configured model cannot start" , async ( ) => {
687774 const workspaceDir = await createTempWorkspace ( "openclaw-dreaming-narrative-" ) ;
688775 const subagent = createMockSubagent ( "The default model carried the diary home." ) ;
0 commit comments