Skip to content

Reconcile file lineage when an older materialization publishes after its successor #1522

Description

@phernandez

Problem

An older successful materialization can publish its file lineage after a newer materialization has already written and published. The older stale_db_version plan can replace file_version=4, synced with file_version=3, pending, although storage still contains revision 4.

This was reproduced with real PostgreSQL sessions and the Cloud runtime while validating https://github.com/basicmachines-co/basic-memory-cloud/pull/2012. It is a separate existing Core publication issue, not evidence that conditional storage writes failed. No exact historical production occurrence of this ordering has been established.

Reproduction

Core pin: bccf09c

Use Cloud's apps/cloud/tests/pgq/test_materialization_overlap.py at PR2012 as the fixture:

  1. Start accepted revision 3 with no published file version and no stored object.
  2. Let worker A prepare, then accept revision 4 and let worker B prepare.
  3. Let A's conditional PUT land, but pause its get_file_metadata return before Core publication.
  4. Let B observe accepted revision 3, write revision 4, and finish DB publication.
  5. Release A's metadata response and let its older publication finish.

Observed: storage remains revision 4, but NoteContent.file_version is 3 and file_write_status is pending, with db_version=4. The regression's assert row.file_version == row.db_version == 4 fails with 3 == 4.

In the fixture, use the unpublished-first-write ordering but wait for second_finished in A's metadata callback and do not hold B's PUT until first_finished. The checked-in ordering isolates the separate first-publication NULL-version guard; this alternate ordering isolates publication regression.

Investigation and constraints

Inspect RepositoryNoteMaterializationPublisher.publish_written_file_state and plan_written_note_materialization_publish. The CAS protects the current DB version read by the publisher, but that DB version can already be 4 when the older publisher plans a file-version-3 update. The successful CAS then applies a stale lineage plan.

Preserve the documented derived-state convergence policy and canonical lock ordering. Do not add SELECT-time locks or hold a transaction across storage. Account for local filesystem writers as well as Cloud conditional writes before assuming successful writes always arrive in accepted-version order.

Acceptance

  • Add a real-session regression for both storage and publication completion orders.
  • After the newer file has been written and published, an older completion must not leave synchronized current content falsely pending without durable convergence work.
  • Preserve legitimate stale writes, moved-path cleanup, external conflicts, and current accepted content.
  • Demonstrate the regression fails on the prior implementation.
  • Follow through with the Cloud Core-pin update and hosted runtime regression once Core is fixed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions