test_intel_null_timestamps.py
python
sha256:4c44d2ec119e8474f8001095984adbf80b0364d805b133ce62fdae7f4deb896c
fix(intel): a missing commit timestamp is an explicit error…
Sonnet 5.5
minor
⚠ breaking
4 days ago
| 1 | """A commit without a timestamp never gets a made-up one — musehub#8, Tier A review D3. |
| 2 | |
| 3 | Run ONLY this file, against a private Postgres (never the live hub database):: |
| 4 | |
| 5 | TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test \ |
| 6 | python -P -m pytest tests/test_intel_null_timestamps.py |
| 7 | |
| 8 | ``musehub_commits.timestamp`` is NOT NULL, so this cannot happen on a healthy database — but the code typed it ``datetime | None`` and |
| 9 | filled the gap two different, both wrong, ways: ``_upsert_history_entries`` substituted the WALL CLOCK (a different value on every run, |
| 10 | frozen by ``ON CONFLICT DO NOTHING``: the index stopped being a pure function of the revision), and an analysed head without one became |
| 11 | ``datetime.min``, whose arithmetic (``as_of - 30 days``) raised ``OverflowError`` far from the cause. |
| 12 | |
| 13 | Now: ``MissingCommitTimestamp`` (a ``ValueError`` naming the commit), raised before anything is written, so the job fails like any |
| 14 | other failure — retried, then ``failed`` — and neither history rows nor the watermark change. Historical commit data is never touched. |
| 15 | Ordering is the one place a missing timestamp is tolerated: it is only the tie-break between commits that ancestry does not order, and it |
| 16 | sorts first, deterministically. |
| 17 | """ |
| 18 | from __future__ import annotations |
| 19 | |
| 20 | import random |
| 21 | from datetime import datetime, timedelta, timezone |
| 22 | from types import SimpleNamespace |
| 23 | |
| 24 | import pytest |
| 25 | import pytest_asyncio |
| 26 | from sqlalchemy import select |
| 27 | |
| 28 | from musehub.db.musehub_intel_models import MusehubIntelResult, MusehubSymbolHistoryEntry |
| 29 | from musehub.services import musehub_intel_providers as P |
| 30 | from musehub.services import musehub_intel_revision as R |
| 31 | from musehub.services import musehub_symbol_indexer as indexer |
| 32 | from tests.factories import create_repo |
| 33 | from tests.test_intel_code_correctness import ins, rep, snapshot_of, standard_dag |
| 34 | |
| 35 | |
| 36 | @pytest_asyncio.fixture |
| 37 | async def session_factory(db_session, session_factory): # noqa: F811 |
| 38 | return session_factory |
| 39 | |
| 40 | |
| 41 | def test_a_missing_timestamp_is_an_explicit_error_naming_the_commit() -> None: |
| 42 | with pytest.raises(R.MissingCommitTimestamp, match="sha256:abc"): |
| 43 | R.require_timestamp("sha256:abc", None) |
| 44 | assert issubclass(R.MissingCommitTimestamp, ValueError) |
| 45 | |
| 46 | |
| 47 | def test_a_naive_timestamp_is_utc_and_a_real_one_is_returned_unchanged() -> None: |
| 48 | naive = datetime(2026, 1, 1, 12, 0) |
| 49 | assert R.require_timestamp("c", naive) == naive.replace(tzinfo=timezone.utc) |
| 50 | aware = datetime(2026, 1, 1, 12, 0, tzinfo=timezone(timedelta(hours=2))) |
| 51 | assert R.require_timestamp("c", aware) == aware |
| 52 | |
| 53 | |
| 54 | def test_ordering_tolerates_a_missing_timestamp_deterministically() -> None: |
| 55 | """Ties between commits that ancestry does not order sort a missing timestamp first, whatever the input order or the clock.""" |
| 56 | t = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| 57 | parents: R.ParentMap = {"a": ((), None), "b": ((), t), "c": ((), None), "d": (("a", "b"), t)} |
| 58 | expected = R.causal_order(parents, set(parents)) |
| 59 | assert expected.index("a") < expected.index("d") and expected.index("b") < expected.index("d") |
| 60 | for seed in range(5): |
| 61 | ids = list(parents) |
| 62 | random.Random(seed).shuffle(ids) |
| 63 | assert R.causal_order(parents, set(ids)) == expected |
| 64 | assert R.sort_ts(None) == R.sort_ts(None) |
| 65 | |
| 66 | |
| 67 | @pytest.mark.asyncio |
| 68 | async def test_history_rows_never_take_the_wall_clock_for_a_missing_commit_timestamp(session_factory) -> None: |
| 69 | async with session_factory() as s: |
| 70 | repo = await create_repo(s, owner="gabriel", slug="null-ts-history") |
| 71 | rid = repo.repo_id |
| 72 | await s.commit() |
| 73 | commit = SimpleNamespace(commit_id="sha256:" + "1" * 64, timestamp=None, author="gabriel", message="m", branch="main", |
| 74 | structured_delta={"ops": [ins("a.py", "a", "a0")]}) |
| 75 | with pytest.raises(R.MissingCommitTimestamp, match="sha256:" + "1" * 8): |
| 76 | await indexer._upsert_history_entries(s, rid, [commit]) |
| 77 | await s.rollback() |
| 78 | assert (await s.execute(select(MusehubSymbolHistoryEntry).where(MusehubSymbolHistoryEntry.repo_id == rid))).first() is None |
| 79 | |
| 80 | |
| 81 | @pytest.mark.asyncio |
| 82 | async def test_an_analysed_head_without_a_timestamp_fails_explicitly_and_the_watermark_does_not_move(session_factory, monkeypatch) -> None: |
| 83 | dag = standard_dag() |
| 84 | async with session_factory() as s: |
| 85 | repo = await create_repo(s, owner="gabriel", slug="null-ts-head") |
| 86 | rid = repo.repo_id |
| 87 | await dag.install(s, rid, ["c0", "c1", "c2", "m", "c3"]) |
| 88 | await s.commit() |
| 89 | results = await indexer.build_symbol_index(s, rid, dag.commits_id("m")) |
| 90 | await P.persist_intel_results(s, rid, dag.commits_id("m"), results) |
| 91 | await s.commit() |
| 92 | before = await snapshot_of(s, rid) |
| 93 | |
| 94 | real = indexer.load_parent_map |
| 95 | head = dag.commits_id("c3") |
| 96 | |
| 97 | async def without_head_timestamp(session, repo_id): |
| 98 | parents = await real(session, repo_id) |
| 99 | parents[head] = (parents[head][0], None) |
| 100 | return parents |
| 101 | monkeypatch.setattr(indexer, "load_parent_map", without_head_timestamp) |
| 102 | with pytest.raises(R.MissingCommitTimestamp, match="sha256:"): # not OverflowError, not a quiet substitute |
| 103 | await indexer.build_symbol_index(s, rid, head) |
| 104 | await s.rollback() |
| 105 | assert await snapshot_of(s, rid) == before # history, derived rows and the watermark are untouched |
| 106 | watermark = (await s.execute(select(MusehubIntelResult.ref).where( |
| 107 | MusehubIntelResult.repo_id == rid, MusehubIntelResult.intel_type == "code.intel_summary"))).scalar_one() |
| 108 | assert watermark == dag.commits_id("m") |
| 109 | |
| 110 | |
| 111 | @pytest.mark.asyncio |
| 112 | async def test_a_provider_asked_for_the_time_of_a_commit_without_one_fails_explicitly() -> None: |
| 113 | class Row: |
| 114 | def __init__(self, value): self._v = value |
| 115 | def first(self): return self._v |
| 116 | |
| 117 | class FakeSession: |
| 118 | def __init__(self, row): self._row = row |
| 119 | async def execute(self, stmt): return Row(self._row) |
| 120 | |
| 121 | with pytest.raises(R.MissingCommitTimestamp, match="sha256:x"): |
| 122 | await P._revision_time(FakeSession((None,)), "repo", "sha256:x") |
| 123 | with pytest.raises(LookupError, match="not referenced"): |
| 124 | await P._revision_time(FakeSession(None), "repo", "sha256:x") |
| 125 | t = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| 126 | assert await P._revision_time(FakeSession((t,)), "repo", "sha256:x") == t |
File History
1 commit
sha256:4c44d2ec119e8474f8001095984adbf80b0364d805b133ce62fdae7f4deb896c
fix(intel): a missing commit timestamp is an explicit error…
Sonnet 5.5
minor
⚠
4 days ago