fix(detect): keep the racily-clean window when seen lands before mtime

_mtime_may_hide_a_rewrite() bailed out with "the mtime check already fired"
whenever seen - mtime came out negative. The caller only reaches it after the
stored and current mtime compared equal, so that shortcut skipped the hash on
a row that was still written in the same tick as the file.

seen is time.time() and mtime is the filesystem's, so the two clocks can
disagree by a few hundred nanoseconds in either direction (#5036: ~240 ns on
NTFS). When they do, a same-length rewrite inside the tick goes undetected and
detect_incremental reports the file as unchanged.

Only the magnitude of the delta says anything about whether the row is current,
so compare -delta against the same window. A row stamped a full window before
the mtime is still from an earlier run and keeps the stat-only fastpath.

Tests: test_a_row_stamped_just_before_the_mtime_still_hashes reproduces the
signed-delta miss, and test_a_row_stamped_well_before_the_mtime_keeps_the_
fastpath pins that the fastpath is not widened to every row.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Houyun SunandClaude committed 2026-11-08 15:46:34 +08:00
1 parent 6c8002eca4
commit 376f333f72
2 files changed
+63 -3

No files matched your search

+11 -3
View File
@@ -3057,12 +3057,20 @@ def _mtime_may_hide_a_rewrite(current_mtime: float, stored: dict) -> bool:
if not isinstance(seen, (int, float)):
return False
delta = float(seen) - float(current_mtime)
if delta < 0:
return False # file is newer than the row; the mtime check already fired
# Derive granularity from the timestamp: a whole-second mtime means the
# filesystem cannot separate writes inside that second.
coarse = float(current_mtime).is_integer()
return delta < (_MTIME_COARSE_S if coarse else _MTIME_SUBSECOND_S)
window = _MTIME_COARSE_S if coarse else _MTIME_SUBSECOND_S
if delta < 0:
# The row was stamped a hair BEFORE the mtime it describes (#5036). That
# is not "the file is newer": the caller only gets here when the stored
# and current mtime are equal, and ``seen`` comes from time.time() while
# mtime comes from the filesystem, so either clock can come out a few
# hundred nanoseconds the other way. Same-tick either way, so still
# worth the MD5. A row stamped a full window before the mtime is from an
# earlier run and stays on the stat-only fastpath.
return -delta < window
return delta < window
def classify_manifest_files(
+52
View File
@@ -66,6 +66,58 @@ def test_same_size_rewrite_in_one_tick_is_requeued(corpus):
assert queued[0].endswith("f1.md")
def test_a_row_stamped_just_before_the_mtime_still_hashes(corpus):
"""`seen` from time.time() can land a few hundred ns BEFORE the mtime (#5036).
Both clocks describe the same write, so the old `seen - mtime < 0 -> trust
stat` shortcut skipped the hash on a row that could still hide a rewrite.
The delta is only meaningful as a magnitude: anything inside the tick is
unprovable in either direction.
"""
import json
src, manifest = corpus
det.save_manifest(det.detect(src)["files"], manifest, root=src, kind="semantic")
rows = json.loads(Path(manifest).read_text(encoding="utf-8"))
for row in rows.values():
row["seen"] = row["mtime"] - 0.000001 # ~1 us before the stamp
Path(manifest).write_text(json.dumps(rows), encoding="utf-8")
target = src / "f1.md"
stat_before = target.stat()
target.write_text("# Doc 2\n\nA substantially longer body than before.\n", encoding="utf-8")
os.utime(target, ns=(stat_before.st_atime_ns, stat_before.st_mtime_ns))
assert target.stat().st_mtime_ns == stat_before.st_mtime_ns
queued = _queued(det.detect_incremental(src, manifest, kind="semantic"))
assert len(queued) == 1 and queued[0].endswith("f1.md")
def test_a_row_stamped_well_before_the_mtime_keeps_the_fastpath(corpus, monkeypatch):
"""Control for the fix: a window-old row is from an earlier run, not a tick."""
import json
src, manifest = corpus
det.save_manifest(det.detect(src)["files"], manifest, root=src, kind="semantic")
rows = json.loads(Path(manifest).read_text(encoding="utf-8"))
for row in rows.values():
row["seen"] = row["mtime"] - 3600 # an hour before; a later write moved mtime
Path(manifest).write_text(json.dumps(rows), encoding="utf-8")
calls = {"n": 0}
real = det._md5_file
def counting(p):
calls["n"] += 1
return real(p)
monkeypatch.setattr(det, "_md5_file", counting)
result = det.detect_incremental(src, manifest, kind="semantic")
assert _queued(result) == []
assert calls["n"] == 0, "a row from an earlier run must not be re-hashed"
def test_a_settled_corpus_costs_no_content_hashes(corpus, monkeypatch):
"""The fastpath must survive: an untouched corpus does zero MD5 work."""
src, manifest = corpus