Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

v6.0.0: hot-layer memory templates lack <!-- BEGIN ENTRIES --> — reads return 0 entries, writes stack orphan END markers (memory never persists)

Open badosanjos opened this issue 5 months ago • 3 comments

Summary

The install templates for the two hot-layer memory files (USER/PRINCIPAL/PRINCIPAL_MEMORY.md, USER/DIGITAL_ASSISTANT/DA_MEMORY.md — see skills/LifeOS/install/USER/...) ship without the <!-- BEGIN ENTRIES --> / <!-- END ENTRIES --> marker pair that LIFEOS/TOOLS/MemoryWriter.ts requires. Result: the autonomic memory loop reports successful writes but nothing is ever readable — memory silently never persists on a fresh install.

Mechanism

  • parseFile() (MemoryWriter.ts) hits its markers-missing branch → whole body becomes preEntriesBody, entries: [] → every read returns 0 entries.
  • serializeFile()'s defensive branch prepends a lone END marker → every write appends entries plus one more orphan END; the next parse still finds no BEGIN, so the cycle repeats.

After a few reviewer runs our PRINCIPAL_MEMORY.md had accumulated 7 orphan <!-- END ENTRIES --> markers with unreadable entries stacked between them, while read() kept returning empty. No error surfaces anywhere — setEntries returns ok with accepted=N.

Environment

Ubuntu 24.04, LifeOS v6.0.0.

Repro

Fresh install → bun LIFEOS/TOOLS/MemoryWriter.ts test →

write 1: accepted=3, dropped_malformed=1, dropped_overlength=1, dropped_duplicates=1, evictions=0
FAIL: expected 3 entries on readback, got 0

(MemorySystem.ts test fails its "ISC-32: new entry present after curation" case for the same reason.)

Fix suggestion (two layers, both verified locally)

  1. Add the marker pair to both install templates:
<!-- BEGIN ENTRIES -->
<!-- END ENTRIES -->
  1. Self-heal in serializeFile() so a marker-less file converges to well-formed instead of degrading:
if (!pre.includes(BEGIN_MARKER)) {
  pre += BEGIN_MARKER + "\n";
}

With both applied, MemoryWriter test, MemorySystem test, and MemoryReviewer test all pass, and live curation persists entries. Happy to send a PR.

badosanjos avatar Jul 03 '26 20:07 badosanjos

Excellent report — I hit the same bug on Debian and filed #1426 before finding yours (now closed as duplicate of this; you had it first, apologies for the miss). PR #1427 is open and updated to close this issue: it adds the marker pair to both templates, makes serializeFile heal a missing BEGIN symmetric to the END guard, and adds parseFile self-heal so field-corrupted files (like the 7-orphan-END state you described) recover their entries instead of reading empty. Verification against your repro steps would be very welcome.

rpriven avatar Jul 07 '26 00:07 rpriven

This is a silent data-loss landmine. The template ships with <!-- END ENTRIES --> but no matching <!-- BEGIN ENTRIES --> marker, so:

  1. Read path: Parser scans for BEGIN, finds nothing → returns empty array (even if entries exist in file)
  2. Write path: Appends new entry + another <!-- END ENTRIES --> → orphaned markers stack up

Users think memory is persisting (no error thrown), but every read starts fresh. By the time they notice, weeks of context are in the file but unreachable.

Immediate fix (v6.0.1 candidate):

<!-- BEGIN ENTRIES -->
<!-- END ENTRIES -->

Add both markers to memory-template.md and scripts/init-memory.sh.

Longer-term: The marker-based parsing is brittle. Consider:

  • YAML frontmatter + markdown body (standard in static site generators)
  • Or JSON-in-comment blocks that validate on read/write

Validation gate: Add a startup health check that reads→writes→reads the template. If round-trip fails, block with actionable error instead of silent corruption.

This pattern (missing sentinel breaking an implicit contract) shows up in every "convention over configuration" system. Explicit schema validation at the boundaries would catch it.


Part of the Agent Memory Systems discussion on SwarmAI.

xg-gh-25 avatar Jul 07 '26 11:07 xg-gh-25

Thanks — this is an excellent writeup, down to the orphan-END accumulation mechanism. Both layers of your suggested fix are in the development source: the install templates for both hot-layer memory files ship the <!-- BEGIN ENTRIES --> / <!-- END ENTRIES --> pair, and serializeFile() now self-heals a marker-less file (inserts the BEGIN marker before entries and normalizes the END marker) so a degraded file converges to well-formed instead of stacking orphans. Ships with the next release. Closing as integrated.

danielmiessler avatar Jul 09 '26 23:07 danielmiessler