@@ -7,8 +7,11 @@ const PROMOTION_MARKER_LINE = "<!-- openclaw-memory-promotion:memory/short-term.
77function promotionSection ( date : string , sizeChars : number ) : string {
88 const heading = `## Promoted From Short-Term Memory (${ date } )\n` ;
99 const marker = `${ PROMOTION_MARKER_LINE } \n` ;
10- const padding = "x" . repeat ( Math . max ( 0 , sizeChars - heading . length - marker . length ) ) ;
11- return `${ heading } ${ marker } ${ padding } ` ;
10+ const entryPrefix = "- " ;
11+ const padding = "x" . repeat (
12+ Math . max ( 0 , sizeChars - heading . length - marker . length - entryPrefix . length ) ,
13+ ) ;
14+ return `${ heading } ${ marker } ${ entryPrefix } ${ padding } ` ;
1215}
1316
1417function markerFreeSection ( date : string , body : string ) : string {
@@ -214,20 +217,20 @@ describe("compactMemoryForBudget — bounded MEMORY.md compaction (regression fo
214217 } ,
215218 ) ;
216219
217- it ( "does not treat a four-space-indented hash line as an ATX heading " , ( ) => {
220+ it ( "preserves a generated-looking block with ambiguous indented content " , ( ) => {
218221 const existing =
219222 `${ promotionSection ( "2026-04-10" , 400 ) } \n` +
220- " ### Indented code \n keep this inside the generated block \n\n" +
223+ " ### Indented user note \n keep this durable content \n\n" +
221224 promotionSection ( "2026-04-20" , 400 ) ;
222225 const newSection = `\n${ promotionSection ( "2026-04-29" , 400 ) } ` ;
223226 const result = compactMemoryForBudget ( {
224227 existingMemory : existing ,
225228 newSection,
226229 budgetChars : 900 ,
227230 } ) ;
228- expect ( result . droppedDates ) . toContain ( "2026-04-10" ) ;
229- expect ( result . compacted ) . not . toContain ( "### Indented code " ) ;
230- expect ( result . compacted ) . not . toContain ( "keep this inside the generated block " ) ;
231+ expect ( result . droppedDates ) . not . toContain ( "2026-04-10" ) ;
232+ expect ( result . compacted ) . toContain ( "### Indented user note " ) ;
233+ expect ( result . compacted ) . toContain ( "keep this durable content " ) ;
231234 } ) ;
232235
233236 it ( "preserves a user `#` section written after the only promotion section" , ( ) => {
@@ -342,7 +345,7 @@ describe("compactMemoryForBudget — bounded MEMORY.md compaction (regression fo
342345 } ,
343346 ) ;
344347
345- it ( "preserves a marker-free section whose heading matches the generated promotion pattern" , ( ) => {
348+ it ( "preserves a marker-free section whose heading matches the promotion pattern" , ( ) => {
346349 const existing = `# Long-Term Memory\n\n${ markerFreeSection (
347350 "2026-04-10" ,
348351 "USER-AUTHORED: recovery key is paper-copy-17" ,
@@ -355,42 +358,75 @@ describe("compactMemoryForBudget — bounded MEMORY.md compaction (regression fo
355358 } ) ;
356359 expect ( result . droppedDates ) . toEqual ( [ ] ) ;
357360 expect ( result . compacted ) . toBe ( existing ) ;
358- expect ( result . compacted ) . toContain ( "USER-AUTHORED: recovery key is paper-copy-17" ) ;
359361 } ) ;
360362
361- it ( "drops only the marker-backed section when a marker-free lookalike sits beside it " , ( ) => {
363+ it ( "drops only a clean generated section beside a marker-free lookalike" , ( ) => {
362364 const userSection = markerFreeSection (
363365 "2026-04-10" ,
364366 "USER-AUTHORED: recovery key is paper-copy-17" ,
365367 ) ;
366368 const existing = `# Long-Term Memory\n\n${ userSection } \n${ promotionSection ( "2026-04-20" , 600 ) } ` ;
367- const newSection = `\n${ promotionSection ( "2026-04-29" , 600 ) } ` ;
368369 const result = compactMemoryForBudget ( {
369370 existingMemory : existing ,
370- newSection,
371+ newSection : `\n ${ promotionSection ( "2026-04-29" , 600 ) } ` ,
371372 budgetChars : 700 ,
372373 } ) ;
373374 expect ( result . droppedDates ) . toEqual ( [ "2026-04-20" ] ) ;
374- expect ( result . compacted ) . not . toContain ( "(2026-04-20)" ) ;
375- expect ( result . compacted ) . toContain ( "## Promoted From Short-Term Memory (2026-04-10)" ) ;
376375 expect ( result . compacted ) . toContain ( "USER-AUTHORED: recovery key is paper-copy-17" ) ;
376+ expect ( result . compacted ) . not . toContain ( "(2026-04-20)" ) ;
377377 } ) ;
378378
379- it ( "still drops a marker-backed multi-project section that a user heading follows " , ( ) => {
380- const existing =
381- ` ${ projectGroupedSection ( " 2026-04-10" ) } \n` +
382- "### Global\n\nMy own global rule, not a promoted entry.\n\n" +
383- projectGroupedSection ( "2026-04-20" ) ;
384- const newSection = `\n ${ projectGroupedSection ( "2026-04-29" ) } ` ;
379+ it ( "preserves a marker-only promotion-shaped block " , ( ) => {
380+ const existing = [
381+ "## Promoted From Short-Term Memory ( 2026-04-10)" ,
382+ PROMOTION_MARKER_LINE ,
383+ "" ,
384+ ] . join ( "\n" ) ;
385385 const result = compactMemoryForBudget ( {
386386 existingMemory : existing ,
387- newSection,
387+ newSection : `\n${ promotionSection ( "2026-04-29" , 600 ) } ` ,
388+ budgetChars : 400 ,
389+ } ) ;
390+ expect ( result . droppedDates ) . toEqual ( [ ] ) ;
391+ expect ( result . compacted ) . toBe ( existing ) ;
392+ } ) ;
393+
394+ it ( "preserves an entire mixed block when user text follows a generated entry" , ( ) => {
395+ const existing = [
396+ promotionSection ( "2026-04-10" , 400 ) ,
397+ "" ,
398+ "USER-AUTHORED: keep this unheaded durable note." ,
399+ "" ,
400+ promotionSection ( "2026-04-20" , 400 ) ,
401+ ] . join ( "\n" ) ;
402+ const result = compactMemoryForBudget ( {
403+ existingMemory : existing ,
404+ newSection : `\n${ promotionSection ( "2026-04-29" , 400 ) } ` ,
388405 budgetChars : 900 ,
389406 } ) ;
390- expect ( result . droppedDates ) . toContain ( "2026-04-10" ) ;
391- expect ( result . compacted ) . not . toContain ( "### Project: alpha" ) ;
392- expect ( result . compacted ) . toContain ( "### Global" ) ;
393- expect ( result . compacted ) . toContain ( "My own global rule, not a promoted entry." ) ;
407+ expect ( result . droppedDates ) . toEqual ( [ "2026-04-20" ] ) ;
408+ expect ( result . compacted ) . toContain ( "(2026-04-10)" ) ;
409+ expect ( result . compacted ) . toContain ( "USER-AUTHORED: keep this unheaded durable note." ) ;
410+ expect ( result . compacted ) . not . toContain ( "(2026-04-20)" ) ;
411+ } ) ;
412+
413+ it ( "preserves a block with user text between generated entries" , ( ) => {
414+ const existing = [
415+ "## Promoted From Short-Term Memory (2026-04-10)" ,
416+ PROMOTION_MARKER_LINE ,
417+ "- first generated entry" ,
418+ "USER-AUTHORED: this line is not owned by either marker." ,
419+ "<!-- openclaw-memory-promotion:memory/short-term.md#second -->" ,
420+ "- second generated entry" ,
421+ "" ,
422+ ] . join ( "\n" ) ;
423+ const result = compactMemoryForBudget ( {
424+ existingMemory : existing ,
425+ newSection : `\n${ promotionSection ( "2026-04-29" , 600 ) } ` ,
426+ budgetChars : 400 ,
427+ } ) ;
428+ expect ( result . droppedDates ) . toEqual ( [ ] ) ;
429+ expect ( result . compacted ) . toBe ( existing ) ;
394430 } ) ;
395431
396432 it ( "exposes a sane default budget below the bootstrap injection cap" , ( ) => {
0 commit comments