world_index: restore write mode before unlink on replace failure and publish mode (WORLD-INDEX-STAGE-CLEANUP) #889

Merged
trigger337 merged 1 commit from upstream-pr/889 into main 2026-11-29 18:59:39 +00:00
trigger337 commented 2026-11-24 14:58:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by trigger337 on 2026-11-24T14:58:10Z.


Overview

In tools/world_index.py, apply published_mode(destination) in _staged_entity_report before atomic replacement, and ensure write mode (0o600) is restored before unlinking in the finally block if replacement fails.

On Windows, unlinking a read-only staged file or cleaning up on os.replace failure causes a PermissionError [WinError 5], leaving stranded temporary files behind and masking underlying exceptions.

Changes

  1. tools/world_index.py: Import published_mode, apply it to staged before replacement, and restore write mode before os.unlink in finally:.
  2. tests/test_world_index.py: Add test_staged_entity_report_restores_write_mode_on_replace_failure asserting write mode restoration on replace failure.

agent: yes
AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5474614274 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/889). Originally posted by [trigger337](https://github.com/trigger337) on 2026-11-24T14:58:10Z. --- ### Overview In `tools/world_index.py`, apply `published_mode(destination)` in `_staged_entity_report` before atomic replacement, and ensure write mode (`0o600`) is restored before unlinking in the finally block if replacement fails. On Windows, unlinking a read-only staged file or cleaning up on `os.replace` failure causes a `PermissionError` [WinError 5], leaving stranded temporary files behind and masking underlying exceptions. ### Changes 1. `tools/world_index.py`: Import `published_mode`, apply it to `staged` before replacement, and restore write mode before `os.unlink` in `finally:`. 2. `tests/test_world_index.py`: Add `test_staged_entity_report_restores_write_mode_on_replace_failure` asserting write mode restoration on replace failure. agent: yes AI-assisted: yes
MoonEyes commented 2026-11-25 08:37:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-25T08:37:51Z.


Two failures on Windows with this PR. Windows 11, Python 3.14, python -I tests/test_world_index.py:

  • main 3f81d46ac: Ran 46 tests, OK.
  • this PR (head of pull/889): Ran 47 tests, FAILED (failures=2).
  1. test_creates_a_verbatim_placement_report_directory (existing test, passes on main): AssertionError: 2 != 0. The tool prints
    ERROR: [Errno 2] No such file or directory: '\\?\C:\...\Temp\tmp...\reports\.mode-<hex>.probe'.
    The new os.chmod(staged, published_mode(destination)) reaches file_mode.new_file_mode() for a new report, and its probe file cannot be created at that \\?\-prefixed path in reports\. On main the staged file only got mode & 0o777 when a mode was known, so this path was never taken.
  2. test_staged_entity_report_restores_write_mode_on_replace_failure (new test): AssertionError: 438 != 384. On Windows os.chmod only sets or clears the read-only flag, so chmod(..., 0o600) reads back as 0o666. The assertion needs a POSIX-only guard, or it should check the write bit (stat.S_IWRITE) as the removed os.name == "nt" branch did.

The other changes (os.rmdir / chmod failures tolerated in finally) look fine to me. I did not measure on real data: world_index needs a raw RPF9 package or carved .bin files, which my work folder does not keep.

AI-assisted: yes (Claude Code ran the tests; I checked them).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7157275985 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/889#issuecomment-7157275985). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-25T08:37:51Z. --- Two failures on Windows with this PR. Windows 11, Python 3.14, `python -I tests/test_world_index.py`: - main 3f81d46ac: `Ran 46 tests`, `OK`. - this PR (head of `pull/889`): `Ran 47 tests`, `FAILED (failures=2)`. 1. **`test_creates_a_verbatim_placement_report_directory` (existing test, passes on main)**: `AssertionError: 2 != 0`. The tool prints `ERROR: [Errno 2] No such file or directory: '\\?\C:\...\Temp\tmp...\reports\.mode-<hex>.probe'`. The new `os.chmod(staged, published_mode(destination))` reaches `file_mode.new_file_mode()` for a new report, and its probe file cannot be created at that `\\?\`-prefixed path in `reports\`. On main the staged file only got `mode & 0o777` when a mode was known, so this path was never taken. 2. **`test_staged_entity_report_restores_write_mode_on_replace_failure` (new test)**: `AssertionError: 438 != 384`. On Windows `os.chmod` only sets or clears the read-only flag, so `chmod(..., 0o600)` reads back as `0o666`. The assertion needs a POSIX-only guard, or it should check the write bit (`stat.S_IWRITE`) as the removed `os.name == "nt"` branch did. The other changes (`os.rmdir` / `chmod` failures tolerated in `finally`) look fine to me. I did not measure on real data: `world_index` needs a raw RPF9 package or carved `.bin` files, which my work folder does not keep. AI-assisted: yes (Claude Code ran the tests; I checked them).
zmasi commented 2026-11-29 00:44:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-29T00:44:09Z.


Independent verification on Windows 11 (10.0.26200, x64) with 4822bcf9de merged onto origin/main (e66c65b950, 0 merge conflicts), tested under both Python 3.12.10 and Python 3.14.5 to re-check both Windows 11 + Python 3.14 failures reported in #7157275985:

  • Red (origin/main + #889 tests/test_world_index.py):
    • python -I tests/test_world_index.py: Ran 47 tests, FAILED (failures=1) (test_staged_entity_report_restores_write_mode_on_replace_failure: origin/main passed stat.S_IWRITE = 0o200 rather than 0o600) on both Python 3.12.10 and Python 3.14.5.
  • Green (4822bcf9de merged onto origin/main):
    • python -I tests/test_world_index.py: Ran 48 tests, OK on both Python 3.12.10 (0.55 s) and Python 3.14.5 (0.52 s) — both test_creates_a_verbatim_placement_report_directory and test_staged_entity_report_restores_write_mode_on_replace_failure now pass cleanly on Windows 11.
    • python -I tests/test_file_mode.py: Ran 5 tests, OK (skipped=2) on both Python versions.
    • Unmocked NTFS read-only destination check: running world_index._staged_entity_report(out) against an existing 0o444 entities.jsonl raises PermissionError ([WinError 5]) on os.replace, restores write permission in finally:, unlinks staged, and removes .world-index-* with 0 stranded temporary directories on both Python 3.12.10 and Python 3.14.5.
  • Minor non-blocking notes:
    1. tools/world_index.py:42 adds sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) directly above the existing _TOOLS_DIR sys.path setup at lines 46-48 (from file_mode import published_mode could sit below line 48).
    2. For context on why published_mode(destination) raised FileNotFoundError in test_creates_a_verbatim_placement_report_directory (now caught by try ... except OSError: pass): file_mode.published_mode() calls os.path.abspath(path), which invokes Win32 GetFullPathNameW even on \\?\ verbatim paths and strips trailing spaces/dots from directory names (\\?\...\reports \entities.jsonl -> \\?\...\reports\entities.jsonl). Since open(staged, "w") already creates new files with open()'s default mode (unlike NamedTemporaryFile's 0o600), ignoring OSError there leaves new verbatim-path files with the expected default mode.
  • Hygiene: git diff --check, python -I -m compileall -q, python -I scripts/check_no_game_data.py, python -I tests/test_ci_registration.py (18/18), and python -I tests/test_text_write_line_endings.py (3/3) all exited 0.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248315636 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7248315636). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-29T00:44:09Z. --- Independent verification on Windows 11 (`10.0.26200`, x64) with `4822bcf9de` merged onto `origin/main` (`e66c65b950`, `0` merge conflicts), tested under both `Python 3.12.10` and `Python 3.14.5` to re-check both Windows 11 + Python 3.14 failures reported in `#7157275985`: - **Red (`origin/main` + `#889` `tests/test_world_index.py`)**: - `python -I tests/test_world_index.py`: `Ran 47 tests`, `FAILED (failures=1)` (`test_staged_entity_report_restores_write_mode_on_replace_failure`: `origin/main` passed `stat.S_IWRITE` = `0o200` rather than `0o600`) on both `Python 3.12.10` and `Python 3.14.5`. - **Green (`4822bcf9de` merged onto `origin/main`)**: - `python -I tests/test_world_index.py`: `Ran 48 tests`, `OK` on both `Python 3.12.10` (`0.55 s`) and `Python 3.14.5` (`0.52 s`) — both `test_creates_a_verbatim_placement_report_directory` and `test_staged_entity_report_restores_write_mode_on_replace_failure` now pass cleanly on Windows 11. - `python -I tests/test_file_mode.py`: `Ran 5 tests`, `OK (skipped=2)` on both Python versions. - Unmocked NTFS read-only destination check: running `world_index._staged_entity_report(out)` against an existing `0o444` `entities.jsonl` raises `PermissionError` (`[WinError 5]`) on `os.replace`, restores write permission in `finally:`, unlinks `staged`, and removes `.world-index-*` with `0` stranded temporary directories on both `Python 3.12.10` and `Python 3.14.5`. - **Minor non-blocking notes**: 1. `tools/world_index.py:42` adds `sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))` directly above the existing `_TOOLS_DIR` `sys.path` setup at lines `46-48` (`from file_mode import published_mode` could sit below line `48`). 2. For context on why `published_mode(destination)` raised `FileNotFoundError` in `test_creates_a_verbatim_placement_report_directory` (now caught by `try ... except OSError: pass`): `file_mode.published_mode()` calls `os.path.abspath(path)`, which invokes Win32 `GetFullPathNameW` even on `\\?\` verbatim paths and strips trailing spaces/dots from directory names (`\\?\...\reports \entities.jsonl` -> `\\?\...\reports\entities.jsonl`). Since `open(staged, "w")` already creates new files with `open()`'s default mode (unlike `NamedTemporaryFile`'s `0o600`), ignoring `OSError` there leaves new verbatim-path files with the expected default mode. - **Hygiene**: `git diff --check`, `python -I -m compileall -q`, `python -I scripts/check_no_game_data.py`, `python -I tests/test_ci_registration.py` (`18/18`), and `python -I tests/test_text_write_line_endings.py` (`3/3`) all exited `0`. AI-assisted.
OreoFR commented 2026-11-29 01:31:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T01:31:34Z.


Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 53b77df14 : red 1 failure (WorldIndexTest.test_staged_entity_report_restores_write_mode_on_replace_failure) / green 48 OK.

  • PR head 4822bcf9d, python -I tests/test_world_index.py from the repository root.
  • Red (main + only this PR's test files): 47 run, 1 failure, 5 skips. AssertionError: False is not true : [], no chmod restore of the write mode is recorded when the replace fails.
  • Green (main + git merge --no-ff of this PR, no conflict): 48 run, OK, 5 skips.
  • Skips: 5 on both sides, Windows-only: "requires Windows verbatim paths" (3 tests), "requires Windows path aliases" (2 tests).
  • Verdict: READY.

Red = main + only the test files from this PR (git checkout pr-889 -- tests/test_world_index.py); green = main + the merged PR. When green runs more tests than red, it is because the merge keeps tests main added since the PR branched.

AI-assisted: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248839254 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7248839254). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T01:31:34Z. --- Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 53b77df14 : red 1 failure (`WorldIndexTest.test_staged_entity_report_restores_write_mode_on_replace_failure`) / green 48 OK. - PR head 4822bcf9d, `python -I tests/test_world_index.py` from the repository root. - Red (main + only this PR's test files): 47 run, 1 failure, 5 skips. `AssertionError: False is not true : []`, no chmod restore of the write mode is recorded when the replace fails. - Green (main + `git merge --no-ff` of this PR, no conflict): 48 run, OK, 5 skips. - Skips: 5 on both sides, Windows-only: "requires Windows verbatim paths" (3 tests), "requires Windows path aliases" (2 tests). - Verdict: READY. Red = main + only the test files from this PR (`git checkout pr-889 -- tests/test_world_index.py`); green = main + the merged PR. When green runs more tests than red, it is because the merge keeps tests main added since the PR branched. AI-assisted: Claude Code (Claude Opus 5.5).
OreoFR commented 2026-11-29 08:34:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T08:34:10Z.


Thanks for this PR. It has a red/green verification by OreoFR on macOS (READY): https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7248839254

The only thing missing for a merge is your claim in the coordination issue #2570 (see CONTRIBUTING.md). Once it is there, a maintainer can pick it up.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7253189358 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7253189358). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T08:34:10Z. --- Thanks for this PR. It has a red/green verification by OreoFR on macOS (READY): https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7248839254 The only thing missing for a merge is your claim in the coordination issue #2570 (see CONTRIBUTING.md). Once it is there, a maintainer can pick it up. AI-assisted: Claude Code
MoonEyes commented 2026-11-29 18:59:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T18:59:35Z.


Merging. Head 4822bcf9d has red/green verdicts from reviewers other than the author (above), merges cleanly with main e5042dd50, does not force a rebase of #2566, and carries a claim ID. Remaining main CI reds (translator-tests mesh, semantic-tests f16) are pre-existing. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7260919695 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/889#issuecomment-7260919695). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T18:59:35Z. --- Merging. Head `4822bcf9d` has red/green verdicts from reviewers other than the author (above), merges cleanly with `main` `e5042dd50`, does not force a rebase of #2566, and carries a claim ID. Remaining main CI reds (`translator-tests` mesh, `semantic-tests` f16) are pre-existing. Merged by MoonEyes. AI-assisted (Claude Code).
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!889
No description provided.