Skip to content

fix: stop test_stdin_source_top_level_wins_344 and test_session_start_compact_recap_339 racing UTC midnight - #423

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/422
Aug 29, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/422

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #422.

Both tests/test_stdin_source_top_level_wins_344.py and its sibling tests/test_session_start_compact_recap_339.py named their own "today" staging file with Python's time.strftime("%Y-%m-%d") at pytest COLLECTION time (module import), while scripts/session-start-hook.sh computes its own $TODAY at RUN time -- minutes later on this suite's slower legs, since pytest collects every module before running any test.

A run that straddles a real UTC-midnight crossing between those two reads leaves the fixture's "today" file with a different date suffix than the one session-start-hook.sh:866 builds REMEMBER_TODAY_FILE from. That is a direct filename mismatch, not just a race: REMEMBER_TODAY_FILE then simply names a path that does not exist, so the file's body is never selected by MEMORY_FILES (session-start-hook.sh:1219), which alone explains "body missing" with no timing window required. The stale fixture file is also, correctly, counted by STAGING_COUNT (session-start-hook.sh:1395) as past-day staging and forks a background run-consolidation.sh (session-start-hook.sh:1399) -- both effects are visible in the CI artefact this was filed from (pytest (macos-latest, 3.9), PR #421, check-failure timestamped 57 seconds past UTC midnight, output carrying "1 day(s) of memory to compress").

Note for the record: the consolidation fork happens strictly after === MEMORY === is already printed to stdout (line 1243 vs. 1383+), and detaches via nohup ... & disown while the parent script runs to completion synchronously -- so there is no actual timing race for the test's own stdout capture. The mechanism is real and matches the observed failure, just slightly more direct than "races the test's own read": a plain filename mismatch is sufficient on its own.

Fix

Both files now shim date on PATH (matching the existing idiom in tests/test_ndc_day_boundary.py and tests/test_doctor_timezone_357.py) so the fixture's "today" filename and the hook subprocess's own runtime clock read one frozen, arbitrary value -- never the real wall clock, closing the exposure regardless of when the suite actually runs, rather than narrowing the window by re-deriving at test time. REMEMBER_NO_PRINTF_T=1 is set so the shim actually intercepts on bash >= 4.2, where _remember_date otherwise prefers the spawn-free printf '%(FMT)T' builtin and never touches PATH -- the exact seam tests/test_prompt_hook_spawns.py::test_no_test_fakes_the_clock_on_path_without_disabling_the_builtin guards, which both files satisfy and which was run directly to confirm.

Two new tests were added to the 344 file, both against the same shim mechanism:

  • test_a_stale_today_file_does_not_blank_the_rest_of_the_recap -- the must-fire pin. Drives the fixture's date and the hook's shimmed clock a day apart deliberately (no waiting for a real midnight) and asserts identity/core-memories/now/recent/archive bodies still all arrive, while documenting that the stale file is correctly excluded from the recap and correctly triggers consolidation -- that half is expected, legitimate behaviour once the two clocks genuinely disagree, not the defect.
  • test_agreeing_dates_trigger_no_background_consolidation -- the must-not-fire control: with the two dates agreeing, no === MEMORY CONSOLIDATION === section should appear.

Sibling module

Grepped tests/*.py for strftime|datetime.now()|date.today() at module scope. tests/test_session_start_compact_recap_339.py was the only other hit, with the byte-identical pattern and the same exposure -- fixed in this same commit with the same shim mechanism, since it is the same class, same file shape, no separate design decision needed.

Scope

This diff touches only the two test files named above plus changelog.d/422.fixed.md. Nothing under scripts/ was touched (that tree is PR #421's lane, still open).

Reproduction

A throwaway pytest file (not committed) built today-2099-03-16.md as the fixture's "today" file while shimming the hook's own date +%Y-%m-%d to answer 2099-03-17 -- reproducing a real midnight crossing deterministically. Asserting the original test's own claim (all bodies present) failed with exactly the CI failure's shape: today-2099-03-16.md body missing, alongside === MEMORY CONSOLIDATION === / 1 day(s) of memory to compress in the same output.

Docs

README.md (the repo's only docs_targets entry) was read against this change and needs none: it documents user-facing timezone/config behaviour and the test-running command, not this internal test-fixture convention.

Testing

python3 -m pytest tests/test_stdin_source_top_level_wins_344.py tests/test_session_start_compact_recap_339.py -q -- 22 passed. python3 -m pytest tests/test_prompt_hook_spawns.py::test_no_test_fakes_the_clock_on_path_without_disabling_the_builtin -q -- 1 passed. Full suite not run: change is confined to two independent test modules with no shared fixture/conftest/production-code edits, and no rebase occurred.

tests/test_stdin_source_top_level_wins_344.py and
tests/test_session_start_compact_recap_339.py named their own "today"
staging file with time.strftime() at pytest collection time while
scripts/session-start-hook.sh computes $TODAY at run time; a real UTC
midnight crossing between those two reads (observed once, PR #421,
pytest macos-latest/3.9, 57s past midnight) drops the today body from
the recap and forks a stray background consolidation. Both files now
shim `date` on PATH so both sides read one frozen value, and a new
pinning test in the 344 file drives the two dates apart deliberately
to confirm a genuine mismatch excludes only the stale file rather than
blanking the rest of the recap.

Fixes #422

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 9f3269e into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/422 branch August 29, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_stdin_source_top_level_wins_344 computes "today" at import time, so a UTC-midnight crossing fails the leg

1 participant