Skip to content

db: harden location + mount identity invariants before phase 4 #7

Closed

Description

@utof

Context

Codex rescue review (autonomous second-opinion pass) flagged multiple identity-integrity issues in `crates/db` that would compound once phase 4 (file_metadata, thumbnails, background queue) starts writing secondary state keyed on `file_locations` / `volume_mounts`.

Fix these before phase 4.

Scope

1. `record_mount` accumulates stale rows

`crates/db/src/volume_repo.rs:162-205,214-279` — inserts on new `(volume_id, machine_id, mount_path)` but never tombstones superseded paths. `VolumeRecord.mounts_on_this_machine` can drift to include paths no longer in use.

Fix: on remount, soft-delete prior rows for the same `(volume_id, machine_id)` whose `mount_path` differs, OR upsert on `(volume_id, machine_id)` keeping only the current path.

2. `update_location_path` rename-to-existing not guarded

`crates/db/src/file_repo.rs:181-220,298-338` — rewrites `old → new` without checking if an active row exists at `new`. Can create two active `file_locations` rows for the same `(volume, relative_path)`.

Fix: before update, check for an existing active row at destination; if present, merge (soft-delete the old one) or mark the existing one `missing`. Wrap in a transaction.

3. SELECT-then-INSERT/UPDATE races across connections

`upsert_location`, `find_or_create`, `record_mount` all use read-modify-write across separate `Mutex` instances. SQLite serializes statements, not sequences — concurrent CLI + desktop ops can produce duplicate active rows.

Fix: wrap each sequence in `BEGIN IMMEDIATE` + `COMMIT`. Acquires the writer lock atomically.

4. `to_string_lossy()` for mount paths is lossy

`crates/db/src/volume_repo.rs:172-205,258-266` — non-UTF8 mount points (rare but possible on Linux) can't round-trip through `TEXT` storage.

Fix: either document the ASCII/UTF8 assumption explicitly and error on non-UTF8 input (preferred for v1), OR store as `BLOB` (ugly). Erroring is fine — nobody has non-UTF8 mount paths in practice.

Acceptance

  • Remount of same volume at different path replaces the mount entry (not appends)
  • Rename-to-existing does not produce two active rows; test covers it
  • Concurrent `find_or_create` from two connections never produces two active volumes with same identifiers (stress test with `thread::spawn`)
  • Non-UTF8 mount path returns `CoreError::Validation` (or equivalent), not silent corruption

Flagged by

Codex rescue / whole-codebase review (phases 0-3 audit, 2026-04-16).

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

    type/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions