tools: harden stage_timeline.py parsing, OSError handling and stdout guard #1789

Merged
kirigayakazima merged 1 commit from upstream-pr/1789 into main 2026-11-28 14:59:03 +00:00
kirigayakazima commented 2026-11-25 12:11:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T12:11:57Z.


Claimed in the coordination issue as: STAGE-TIMELINE-STDOUT-GUARD-AND-PARSER-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162011238)

What and why

  • Add the standard sys.stdout.reconfigure(errors="replace") guard in tools/stage_timeline.py's __main__ block so redirected Windows python -I runs do not fail tests/test_tool_stdout_encoding.py (tools/stage_timeline.py is removed from missing).
  • Tighten PRESS timestamp regex ([0-9]+(?:\.[0-9]+)?) and make first_value() return None safely for empty strings, leading commas (e.g. ,1234), partial ? hex tokens, or non-hex values instead of raising ValueError.
  • Read logs with encoding="utf-8-sig", errors="replace" and catch OSError in main() to print a concise error line to stderr and return exit code 2 instead of a traceback.
  • Require p["game"] == 3 before reporting "story load (loading screen ended)" in timeline().
  • Add synthetic standard-library unit tests in tests/test_stage_timeline.py.

Tested

  • python -I tests/test_stage_timeline.py -v -> Ran 7 tests in 0.146s, OK (verified in a detached upstream/main worktree that the new test file fails before the fix: FAILED (failures=4, errors=2)).
  • python -I tests/test_tool_stdout_encoding.py -> tools/stage_timeline.py is no longer in missing (only pre-existing ['tools/compute_heap_layout_census.py'] fails).
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 4.545s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 12.772s, OK.
  • python -I scripts/check_no_game_data.py tools/stage_timeline.py tests/test_stage_timeline.py -> exit 0.
  • python -I -m compileall -q tools/stage_timeline.py tests/test_stage_timeline.py and git diff --check -> clean.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491079139 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1789). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T12:11:57Z. --- Claimed in the coordination issue as: STAGE-TIMELINE-STDOUT-GUARD-AND-PARSER-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162011238) ### What and why - Add the standard `sys.stdout.reconfigure(errors="replace")` guard in `tools/stage_timeline.py`'s `__main__` block so redirected Windows `python -I` runs do not fail `tests/test_tool_stdout_encoding.py` (`tools/stage_timeline.py` is removed from `missing`). - Tighten `PRESS` timestamp regex (`[0-9]+(?:\.[0-9]+)?`) and make `first_value()` return `None` safely for empty strings, leading commas (e.g. `,1234`), partial `?` hex tokens, or non-hex values instead of raising `ValueError`. - Read logs with `encoding="utf-8-sig", errors="replace"` and catch `OSError` in `main()` to print a concise error line to stderr and return exit code `2` instead of a traceback. - Require `p["game"] == 3` before reporting `"story load (loading screen ended)"` in `timeline()`. - Add synthetic standard-library unit tests in `tests/test_stage_timeline.py`. ### Tested - `python -I tests/test_stage_timeline.py -v` -> `Ran 7 tests in 0.146s`, `OK` (verified in a detached `upstream/main` worktree that the new test file fails before the fix: `FAILED (failures=4, errors=2)`). - `python -I tests/test_tool_stdout_encoding.py` -> `tools/stage_timeline.py` is no longer in `missing` (only pre-existing `['tools/compute_heap_layout_census.py']` fails). - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 4.545s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 12.772s`, `OK`. - `python -I scripts/check_no_game_data.py tools/stage_timeline.py tests/test_stage_timeline.py` -> exit `0`. - `python -I -m compileall -q tools/stage_timeline.py tests/test_stage_timeline.py` and `git diff --check` -> clean. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) AI-assisted: yes ### Checklist - [x] Based on current main and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
PierreEbele commented 2026-11-25 19:16:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by PierreEbele on 2026-11-25T19:16:52Z.


Reviewed dacbc165: python -I tests/test_stage_timeline.py passes on the PR tree (7/7); on main 6/7 fail (first_value crash on empty/mid-? hex, float('.') press stamps, traceback on an unreadable log), so the tests pin the fix. A probe on both trees reproduced the main-side crashes the PR removes and found no input the PR handles worse; the tightened PRESS regex only stops accepting a trailing-dot timestamp the emitter (%.1f) never writes. The game==3 "ended" gate matches the documented state table but needs a real boot log to confirm.

AI-assisted review.

Recommendation: Merge

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7172077741 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1789#issuecomment-7172077741). Originally posted by [PierreEbele](https://github.com/PierreEbele) on 2026-11-25T19:16:52Z. --- Reviewed dacbc165: `python -I tests/test_stage_timeline.py` passes on the PR tree (7/7); on main 6/7 fail (first_value crash on empty/mid-? hex, float('.') press stamps, traceback on an unreadable log), so the tests pin the fix. A probe on both trees reproduced the main-side crashes the PR removes and found no input the PR handles worse; the tightened PRESS regex only stops accepting a trailing-dot timestamp the emitter (%.1f) never writes. The game==3 "ended" gate matches the documented state table but needs a real boot log to confirm. AI-assisted review. **Recommendation:** Merge
zmasi commented 2026-11-28 12:04:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:04:39Z.


#1789 | head dacbc16566 (merged cleanly with main d48a957bb) | python -I tests/test_stage_timeline.py | red on main: FAILED (failures=3, errors=2) (ValueError on empty/malformed hex/press fields, false loading-ended transition when game != 3, and raw OSError tracebacks) | green on PR+main: 7 tests OK | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited PRESS, first_value, read, timeline, and main in tools/stage_timeline.py and tests/test_stage_timeline.py (rejecting bare . / multi-dot timestamps, empty or partial-? hex heads, requiring game == 3 before reporting story load (loading screen ended), reading utf-8-sig logs, returning exit 2 without tracebacks on missing/directory paths, and guarding stdout.reconfigure under -I on Windows). Also verified tests/test_ci_registration.py (18 OK), tests/test_runner_calls_all_tests.py (3 OK), tests/test_text_write_line_endings.py (3 OK), and scripts/check_no_game_data.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238976950 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1789#issuecomment-7238976950). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:04:39Z. --- #1789 | head dacbc16566dd0ff165d9a60f5c70581982872b5f (merged cleanly with main d48a957bb) | `python -I tests/test_stage_timeline.py` | red on main: `FAILED (failures=3, errors=2)` (`ValueError` on empty/malformed hex/press fields, false loading-ended transition when `game != 3`, and raw `OSError` tracebacks) | green on PR+main: `7 tests OK` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `PRESS`, `first_value`, `read`, `timeline`, and `main` in `tools/stage_timeline.py` and `tests/test_stage_timeline.py` (rejecting bare `.` / multi-dot timestamps, empty or partial-`?` hex heads, requiring `game == 3` before reporting `story load (loading screen ended)`, reading `utf-8-sig` logs, returning exit `2` without tracebacks on missing/directory paths, and guarding `stdout.reconfigure` under `-I` on Windows). Also verified `tests/test_ci_registration.py` (`18 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 14:58:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:58:51Z.


Red/green: classified READY by cloud-4 (red on main 09c153948, green on head merged with main); head dacbc16566 unchanged since 2026-11-25, before that run.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7241091452 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1789#issuecomment-7241091452). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T14:58:51Z. --- Red/green: classified READY by cloud-4 (red on main 09c153948, green on head merged with main); head dacbc16566dd unchanged since 2026-11-25, before that run. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1789
No description provided.