basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

Auto-write `created` / `modified` frontmatter on BM-owned writes

Open bm-clawd opened this issue 8 months ago • 5 comments

Currently, Basic Memory tracks created_at and updated_at timestamps at the entity level (visible in JSON output via API/MCP), but these timestamps don't appear in the markdown files themselves.

Current behavior:

  • Entity timestamps exist in the database
  • They're accessible via JSON output
  • Frontmatter doesn't include them unless manually written/maintained

Desired behavior:

  • BM automatically writes created_at and updated_at into YAML frontmatter when creating/updating notes
  • These fields would be managed by BM itself (not AI/user-maintained)
  • Timestamps would be visible in the plain-text files, not just the database

Use cases:

  • Consistent timestamp metadata across all notes without manual maintenance
  • Temporal queries/filters based on frontmatter (e.g., "notes created this week")
  • Better compatibility with tools that read frontmatter but not BM's database
  • Fits BM's transparency philosophy: important metadata should be in the file itself

Suggested implementation:

  • Auto-inject created_at on note creation
  • Auto-update updated_at on note modification
  • Make these "reserved" frontmatter fields (schema validation aware, but not user-editable)
  • Respect existing timestamps if already present (don't overwrite manual entries)

Related:

  • Picoschema metadata: section could declare these as system-managed fields
  • Would complement existing frontmatter features (status, tags, priority)

bm-clawd avatar Mar 20 '26 00:03 bm-clawd

Claude finished @bm-clawd's task in 58s —— View job


Triage Analysis

  • [x] Read and understand the issue
  • [x] Search for duplicate issues
  • [x] Classify issue type, priority, and complexity
  • [x] Add appropriate labels
  • [x] Provide triage summary

Summary

Type: Enhancement (improvement to existing feature)

Priority: Medium — improves transparency and compatibility with external tools, aligns with BM's "files as source of truth" philosophy, but not blocking any core functionality.

Component: Database/sync + MCP tools (write_note, edit_note)

Complexity: Medium — requires changes across the markdown writing pipeline, sync layer, and potentially frontmatter parsing.


Duplicate Check

No exact duplicate found. Related issue: #616 (Temporal query optimization) touches created_at/updated_at as search dimensions, but focuses on querying — not on writing timestamps into frontmatter. These are complementary, not duplicates. Implementing this issue would actually help #616 by making dates visible in frontmatter for temporal filtering.


Implementation Notes

Key areas to touch:

  • src/basic_memory/markdown/ — frontmatter serialization when writing notes
  • src/basic_memory/services/ — entity write/update logic to inject timestamps
  • src/basic_memory/sync/ — ensure timestamps are preserved across sync
  • Need to handle the "reserved fields" concept (don't overwrite user-set values on first write, but manage on subsequent updates)

Edge cases to consider:

  • What happens when a file is imported with existing created_at in frontmatter? (Respect it.)
  • What format for timestamps? ISO 8601 is likely the right choice.
  • updated_at should reflect file modification time, not DB update time — these should stay in sync.

Labels added: enhancement, medium, database, mcp

claude[bot] avatar Mar 20 '26 00:03 claude[bot]

Additional context from Discord #help

Expected behavior (per Drew Nellins):

  • If created_at/updated_at exist in frontmatter → those should be authoritative (override DB)
  • If they're missing → DB manages them automatically

Current observed behavior (per @Titan): When trying to validate these fields via schema:

schema:
  metadata:
    created_at:
      type: string
      format: datetime
    updated_at:
      type: string
      format: datetime

Result: validation treats metadata: itself as a required observation, not as a frontmatter section.

Root issue: Picoschema currently validates observations and relations but not frontmatter fields. This means:

  • Frontmatter can contain arbitrary YAML (by design)
  • But schemas can't enforce which frontmatter fields exist or their types
  • Timestamp fields in frontmatter may not be getting preserved/respected

Test needed:

  1. Create note with created_at: 2024-01-01T10:00:00Z in frontmatter
  2. Index it
  3. Read entity JSON via API/MCP
  4. Verify: does entity['created_at'] match frontmatter value or DB auto-generated value?

cc @Titan for original use case

bm-clawd avatar Mar 20 '26 03:03 bm-clawd

Being implemented together with #238 in a single PR (they share the frontmatter/timestamp code path). Canonical fields will be created / modified — the fields BM's markdown parser already reads (markdown/utils.py:59-60) — with BM auto-writing them on create/update and never overwriting a value the frontmatter already carries. Tracking the work from #238.

phernandez avatar Jul 16 '26 19:07 phernandez

Managed timestamp policy after reviewing #1089

This issue stays separate and follows #238. #238 establishes timestamp parsing and file/DB consistency; this issue adds automatic frontmatter mutation only for Basic Memory-owned writes.

Write policy

  • Canonical written fields are created and modified.
  • Create: explicit canonical values supplied on creation are honored. Missing values are initialized to the same current timestamp.
  • Update/edit: preserve created and always set modified to the current write time.
  • Do not infer that an unchanged incoming modified was an intentional override merely because a full-document request carried the old frontmatter forward.
  • Move: preserve both semantic timestamps. A path change is not a content timestamp change.
  • Passive index/sync: never inject or rewrite timestamp frontmatter. User-authored file values remain authoritative through #238.
  • Legacy frontmatterless notes: partial edits remain frontmatterless. We will not add a YAML block solely to stamp timestamps; a separate explicit migration/backfill can cover that use case if wanted.
  • Active writes normalize timestamp values to timezone-aware ISO 8601 strings so the persisted representation is stable across YAML readers and machines.
  • Aliases such as date, created_at, and updated_at are not part of the first managed-write implementation.

Implementation constraints

  • Resolve the timestamp policy once in the service-owned note preparation stage.
  • Carry typed timestamps forward; do not reparse serialized metadata in local/cloud persistence layers.
  • Frontmatter updates used by partial edits must preserve the body bytes exactly, including leading indentation, trailing whitespace, newline style, and BOM handling.
  • Tests must cover create, full replace, every partial edit family, frontmatterless legacy notes, moves, and local/cloud parity.

Implementation should begin only after the clean #238 foundation lands.

phernandez avatar Jul 17 '26 18:07 phernandez

Narrowing this to what's left. The read side landed in #1100: created / modified in frontmatter are parsed into created_at / updated_at (markdown/entity_parser.py, services/note_preparation.py) and documented in docs/NOTE-FORMAT.md. What is not implemented is the write side — no BM-owned write (write_note, edit_note, move) injects the fields; only the importers do, and passive indexing deliberately never adds them.

The policy is already settled in my comment above (create initializes both; edit bumps modified; move preserves; passive sync never touches; tz-aware ISO strings; byte-exact body preservation on partial edits), so this no longer needs investigation. Retitled accordingly and dropping the created_at/updated_at naming and the schema-validation ask (separate picoschema concern). Implementation is medium: the create/update/edit/move paths in note_preparation.py, frontmatter serialization via dump_frontmatter, and local + cloud parity tests including frontmatterless legacy notes.

phernandez avatar Aug 29 '26 02:08 phernandez