Skip to content

fix(storage): close remaining timestamp/FK/upsert integrity gaps #820

Closed

Description

@Sinity

Current status

This issue started as an audit dump of timestamp and storage integrity bugs. Some findings are already fixed or moved by later work:

  • _iso_to_epoch UTC handling was fixed in 4d60bdee / PR fix: datetime UTC, MCP filter wiring, export fields, paste role guard #821-era work.
  • parse_timestamp(None) no longer needs to crash at materialization sort-key calculation; materialization_runtime._timestamp_sort_key() now returns None when parsing fails.
  • conversations.raw_id now uses ON DELETE SET NULL in the archive schema.
  • attachment_refs.message_id now uses ON DELETE SET NULL, and perf(daemon): keep live convergence status bounded (#845) #854 added an FK-support index for replacement performance.
  • Old publications.py / runs.py query modules referenced by the original audit are no longer present on origin/master after the daemon-first cleanup.

Keep this issue open only for the remaining concrete integrity review and regression coverage.

Remaining scope

Self-referential message FK policy

messages.parent_message_id still references messages(message_id) without an explicit ON DELETE policy. Decide whether the correct behavior is:

  • ON DELETE SET NULL, preserving children after deleting a parent message;
  • cascade through conversation/message replacement semantics;
  • no FK, with branch/orphan handling enforced at the archive domain layer;
  • or the current behavior, if replacement/delete paths prove it is safe.

Silent attachment-ref insert handling

Shared attachment-ref insertion still uses INSERT OR IGNORE. Verify whether this can silently swallow FK violations or other integrity problems that should be surfaced during archive writes. If it is intentionally idempotent, narrow the ignored conflict to the expected uniqueness case or add diagnostics for unexpected skips.

Regression coverage

The issue should close with tests that prove the remaining decisions, not with another audit list.

Likely files:

  • polylogue/storage/sqlite/schema_ddl_archive.py
  • polylogue/storage/sqlite/schema_bootstrap.py
  • polylogue/core/common.py
  • polylogue/storage/repository/archive/writes/
  • tests/unit/storage/test_schema_upgrades.py
  • tests/unit/storage/test_backend.py
  • tests/unit/storage/test_store_ops.py
  • tests/unit/pipeline/test_ingest_batch.py

Non-goals

  • Do not revive old publication/run tables as part of this issue.
  • Do not solve broad write-boundary hash honesty; refactor(storage): make conversation writes bundle/hash-honest #841 owns that.
  • Do not make schema changes without an exact fresh-schema and upgrade-path decision.
  • Do not preserve the original stale audit wording if code evidence shows a finding is already fixed.

Acceptance criteria

  • The issue body/PR closure comment classifies each original finding as fixed, obsolete, still-open, or moved.
  • The messages.parent_message_id delete policy is explicitly decided and tested.
  • Attachment-ref insertion either surfaces unexpected FK/integrity failures or documents and tests why INSERT OR IGNORE is safe there.
  • Any schema change includes fresh-schema DDL, exact upgrade handling, and schema upgrade tests.
  • Focused storage/pipeline tests cover the chosen behavior.

Verification

  • Focused storage schema and write-path tests for the changed integrity behavior.
  • devtools verify-migrations if schema version/upgrade manifests are touched.
  • devtools verify --quick, and full devtools verify before PR if schema/write paths change.

Refs #841 #854 #852.

Activity

  1. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Update: _iso_to_epoch UTC fix landed in 4d60bde. Remaining items: parse_timestamp None crash, naive-aware datetime comparison, 3 FK gaps.

  2. changed the title [-]fix(core): datetime handling — _iso_to_epoch local-time shift, naive-aware comparison crash, None.timestamp crash, 3 FK gaps[/-] [+]fix(storage): close remaining timestamp/FK/upsert integrity gaps[/+] on May 6, 2026
  3. Sinity commented on May 7, 2026

    @Sinity
    OwnerAuthor

    Wave 1 classification (#852)

    Each original finding classified against current origin/master (1bc0d37):

    Finding Status Evidence
    _iso_to_epoch UTC handling Fixed 4d60bdee
    parse_timestamp(None) crash Fixed Returns None at sort-key calc path
    conversations.raw_id FK policy Fixed ON DELETE SET NULL in DDL (line 60)
    attachment_refs.message_id FK policy Fixed ON DELETE SET NULL in DDL (line 202), FK-support index via #854
    Old publications.py / runs.py Removed No longer on origin/master
    messages.parent_message_id self-referential FK Still open — implement below
    INSERT OR IGNORE on attachment_refs Still open — implement below

    Scope for this PR

    1. parent_message_id FK: Keep NO ACTION (the implicit default). This is the correct policy — message deletion is conversation-scoped, and the FK prevents orphaned references within normal operation. Add regression test proving conversation-scoped deletion works with self-referential chains.

    2. INSERT OR IGNORE on attachment_refs: Narrow to INSERT ... ON CONFLICT(ref_id) DO NOTHING. This preserves idempotency on ref_id primary key conflicts while surfacing FK violations that should never occur in correct operation (attachments always upserted before refs). If an FK violation does occur, we want the error, not silent discard.

    3. Tests: Message self-referential FK integrity, attachment-ref FK surface, idempotency preserved.

    Non-goals

    Files

    • polylogue/core/common.py — narrow INSERT OR IGNORE
    • tests/infra/storage_records.py — update duplicate SQL
    • tests/unit/storage/test_backend.py — FK integrity tests
    • Issue body update (closure comment) — classify all findings
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:fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions