Skip to content

fix: serialize concurrent same-target supersession writes across every write leg #166

Closed

Description

@jflowers

Origin

Deferred from PR #156 review feedback (thread on tools/learning_temporal.go:211), independently flagged by the review council on the fix commit (LOW, concurrency), and confirmed by the /uf.address-feedback Tier 2 dispatch (MEDIUM concurrency finding, retained as advisory).

Problem

Two concurrent StoreLearning calls superseding the same target can both pass validation. The one-successor invariant (design D3 of learning-temporal-validity: "chains, not forks") is enforced by a check-then-act sequence:

  1. resolveSupersededTarget reads superseded_by during validation (prepareTemporal).
  2. Writes land in two legs after the successor's page insert:
    • linkSuperseded calls store.UpdatePage(target) from the validation-time snapshot — the properties map was read before the successor file write and store insert, so a competing update in between is blindly overwritten.
    • markSupersededFile read-modify-writes the target's markdown frontmatter, blindly replacing superseded_by.

The second writer overwrites the first successor, silently forking the chain — no supersedes_warning is emitted.

Scope of the unserialized window: the store lock serializes processes; the race is between concurrent in-process tool calls under streamable HTTP sharing one store handle. The window spans the successor file write, store insert, and embedding generation (seconds-wide).

Impact is metadata-only (forked supersession chain), but it violates the documented invariant.

Fix scope — every write leg

  • Store leg: compare-and-swap update guarded on the properties JSON (superseded_by lives in the JSON blob per design D1 — needs json_extract in the UPDATE predicate or a schema change), or a per-target in-process mutex.
  • File leg: markSupersededFile must re-read and re-check superseded_by before replacing it (or the mutex must cover both legs).

Both legs must be covered: re-ingestion restores only missing pages, so after a graph.db loss the stale file leg wins and reintroduces the fork.

Constraints

  • Spec-first: this is a real increment (SQL/locking strategy + both write legs + tests) and needs its own change spec.
  • Follow the PR-145 toctou pattern (CAS preferred over lock widening).

Acceptance

  • Two concurrent supersessions of the same target cannot fork the chain (second writer fails validation or receives supersedes_warning)
  • Both write legs guarded (store + markdown file)
  • Behavior survives graph.db deletion + re-ingestion from markdown
  • Race regression test (concurrent goroutines superseding the same target)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions