Skip to content

fix(judge): publish judged.json through a staged rename - #248

Open
argszero wants to merge 1 commit into
google-research:mainfrom
argszero:fix/atomic-judge-write
Open

argszero wants to merge 1 commit into
google-research:mainfrom
argszero:fix/atomic-judge-write

Conversation

@argszero

@argszero argszero commented Dec 1, 2026

Copy link
Copy Markdown

Summary

harness2.core.judge.write() published judged.json by calling Path.write_text() on the destination itself, which truncates the previous file before any of the new JSON exists. A write that stops partway — a full disk, an interrupted run — therefore destroyed the last valid grades, and every later reader of that file found nothing.

The JSON is now staged in a temporary file beside the destination and renamed over it with os.replace, so a failed publish leaves the previous judged.json exactly as it was. The destination path and the on-disk format (indent=2, default=str, frac/graded merged into each row) are unchanged.

Where the shape comes from

This is not a new pattern in the tree — it is the one the project already uses in two other places, so the change copies that wiring rather than inventing a third:

  • harness2/core/runmark.py:118-121 stages f"{MARK}.{os.getpid()}.tmp" and finishes with os.replace.
  • harness2/core/schedule.py:354-356 does the same with .json.tmp beside the summary.

I followed those two rather than adding a helper module, because a shared atomic-write helper would be a change to a layer four call sites already own, and the issue is about this one call site.

Related Issue

Closes #245

What a failed write leaves behind

The destination is untouched. The staging file (judged.json.<pid>.tmp) remains after a failed write, which is what runmark.py and schedule.py already do; nothing globs the run namespace for *.json, so it cannot be mistaken for a result file. The new test asserts the destination and the directory contents, so if that ever changes the test says so.

Tests

  • Existing tests pass
  • New tests added

Measured on this branch's base, 6868817 (the current main), with python3 -m unittest discover -s tests -p 'test_*.py' from the repository root:

tree result
6868817, unmodified Ran 70 tests — FAILED (errors=4, skipped=6)
this branch Ran 73 tests — FAILED (errors=4, skipped=6)

The four failing names are identical on both trees, and they are environmental rather than project defects: RolloutIsolationTests.test_fence_blocks_other_rollouts_but_keeps_own_dirs_and_tmp, test_installed_commands_stay_on_path, test_later_rollout_cannot_see_earlier_installs_or_temp_files, test_retry_starts_from_a_clean_sandbox all fail with FileNotFoundError: [Errno 2] No such file or directory: 'timeout' — this machine is macOS, which has no timeout(1). So the delta is +3 tests, +3 passing, the same 4 names failing.

tests/test_judge_write_atomic.py is the regression test, and it was run against the code as it was before this change:

  • On unmodified 6868817: test_failed_publish_keeps_the_previous_result_file FAILS — the destination is left holding half a JSON document, which is the reported behaviour, not a missing symbol or a broken fixture. The other two cases pass on both trees.
  • On this branch: all three pass.

The failing case writes a valid judged.json, makes the publish stop partway (OSError(28, ...) after partial bytes), and then asserts on the file that survives — it still parses, and still holds the previous row — rather than on what write() returned.

Not in this change

harness2/cli/judge.py:152 publishes base_judged.json with the same direct write_text(), so it has the same weakness. It is a different function and a different file from the one this issue names, so it is reported here rather than changed here — say the word and it is a one-line follow-up.

write() opened the destination before the new JSON existed, so a write that
stopped partway truncated the previous judged.json and left every later reader
with nothing. Stage the JSON beside the destination and rename it over the
original, the way runmark.py and schedule.py already publish their files.
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.

Preserve prior judge results when publishing a replacement fails

1 participant