Skip to content

fix(batch): one truncated run-meta.json no longer discards the batch summary - #312

Closed
vaibhavdabas16 wants to merge 1 commit into
TIGER-AI-Lab:mainfrom
vaibhavdabas16:fix/batch-stats-atomic-run-meta
Closed

vaibhavdabas16 wants to merge 1 commit into
TIGER-AI-Lab:mainfrom
vaibhavdabas16:fix/batch-stats-atomic-run-meta

Conversation

@vaibhavdabas16

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #303: a single truncated run-meta.json currently destroys the machine-readable results for an entire batch, and the truncation itself is made impossible in the first place.

The crash. print_run_stats() parsed every run-meta.json with a bare json.loads() (batch.py:450). async_main() calls it at batch.py:753, one line before write_summary_json() at :754 — so one malformed file raised JSONDecodeError out of the function and took batch-summary.json down with it. Every other job in the batch lost its recorded results, even though those runs succeeded.

The truncation. write_run_meta() (metadata.py:288) wrote with a plain write_text(). A run killed mid-write — Ctrl-C, OOM, host shutdown — leaves a half-written file on disk, which is precisely what the reader above then chokes on.

Both halves are addressed:

  • New clawbench.utils.jsonio with write_json_atomic() (same-directory temp file, flush + fsync, then os.replace) and read_json_or_none(). os.replace replaces atomically on POSIX and Windows, so an interrupted write leaves either the previous file or no file — never a truncated one.
  • write_run_meta() and write_summary_json() now write atomically.
  • print_run_stats() reads via read_json_or_none(). It also rejects valid JSON that isn't an object — a bare list previously reached .get() and raised AttributeError — prints a warning naming the offending file, and falls back to the directory name for that one run.

Reporting now degrades to a single warning line for the affected run instead of losing the whole batch.

Corpus

  • v2
  • v1
  • both
  • not applicable

Host-side runner/reporting change; no task data involved.

Test plan

  • New tests/test_batch_stats_resilience.py (11 tests): atomic round-trip and parent-dir creation, no leftover temp files, previous file survives a failed serialization, read_json_or_none() against truncated / empty / garbage / undecodable input, and two print_run_stats() regression tests covering a truncated run-meta.json and a non-object one.
  • Confirmed the regression tests genuinely catch the bug: reverted batch.py and metadata.py to main and re-ran, which fails with JSONDecodeError and AttributeError respectively; both pass with the fix applied.
  • Full suite: 203 passed, 3 skipped. The one unrelated failure, test_host_tasks.py::test_checked_task_json_files_parse_and_validate[v1-lite], reproduces identically on a clean main checkout on this machine — the v1-lite task files are git symlinks (mode 120000) that Windows checks out as plain text.
  • ruff check and ruff format --check clean on all changed files. Removing the last json. uses from batch.py left import json unused, so it is dropped (F401).

Related issues

Fixes #303.

One note for maintainers, deliberately left out of scope: eval/rescore.py reads run-meta.json with the same unguarded json.loads() at lines 58, 102 and 279. Happy to follow up in a separate PR if that is wanted.

…summary

print_run_stats() parsed every run-meta.json with a bare json.loads(). A
single truncated or malformed file raised JSONDecodeError out of the
function, and because async_main() calls it at batch.py:753 *before*
write_summary_json() at :754, the exception took batch-summary.json down
with it -- one bad run destroyed the machine-readable results for every
other job in the batch.

The file gets truncated in the first place because write_run_meta() wrote
run-meta.json with a plain write_text(): a run killed mid-write (Ctrl-C,
OOM, host shutdown) leaves a half-written file behind.

Both halves are now fixed:

- New clawbench.utils.jsonio with write_json_atomic() (same-directory
  temp file, flush + fsync, os.replace) and read_json_or_none().
  os.replace is atomic on POSIX and Windows, so a crash mid-write leaves
  either the previous file or no file, never a truncated one.
- write_run_meta() and write_summary_json() write atomically.
- print_run_stats() reads via read_json_or_none(), also rejects valid
  JSON that is not an object (a bare list previously hit .get() and
  raised AttributeError), prints a warning naming the file, and falls
  back to the directory name for that one run.

Reporting now degrades to one warning line for the affected run instead
of losing the whole batch.

Fixes TIGER-AI-Lab#303.
@vaibhavdabas16
vaibhavdabas16 force-pushed the fix/batch-stats-atomic-run-meta branch from 758efda to 3b63507 Compare August 20, 2026 05:18
@Perry2004

Copy link
Copy Markdown
Collaborator

Thanks for the contribution. I'll take a look.

@Perry2004

Copy link
Copy Markdown
Collaborator

I believe it doesn't worth adding such amount of custom atomic disk io snippet just to handle such rare case.
Rather I'd suggest to change the order to put print_run_stats as the last step so even it fails there's no data loss.

Closed in favor of #325.

@Perry2004 Perry2004 closed this Aug 30, 2026
@github-project-automation github-project-automation Bot moved this from Under Review to Done in ClawBench Aug 30, 2026
@Perry2004 Perry2004 added the wontfix This will not be worked on label Aug 30, 2026
vaibhavdabas16 added a commit to vaibhavdabas16/ClawBench that referenced this pull request Sep 13, 2026
--resume decided a job was done if batch-logs/<case>-<model>.log existed. That
log is opened when a job *starts* (batch.py:320), so resume skipped every job
that was ever attempted -- including the ones killed mid-run. That inverts the
flag: after a network blip or an OOM takes out 20 of 130 tasks, --resume skips
exactly those 20 and calls the batch complete.

The second effect was worse, because it destroyed data rather than omitting it.
write_summary_json rewrites batch-summary.json from the in-memory job list, and
every carried-over job was marked "skipped" with duration 0. A resumed batch
therefore reported none of the results it already had, in the artifact that the
per-run stats and the HuggingFace upload consume.

run-meta.json is the authoritative record: run.py writes one for any run that
got far enough to have an outcome, failures included -- the invariant TIGER-AI-Lab#303,
(test_case, model) straight from the metadata, so a model name containing "/"
matches despite the run directory sanitising it to "--", and the newest run
wins when repeated resumes have left several directories for one pair. A
truncated run-meta.json is treated as an unknown outcome and the job runs
again rather than having a result invented for it (TIGER-AI-Lab#312/TIGER-AI-Lab#325 showed that file
can be truncated).

A carried-over job now keeps the recorded result -- passed / failed / error,
with its real duration -- so the rewritten summary preserves the original
tallies. Jobs are marked `resumed` so the scheduler still skips running them
and a reader of batch-summary.json can tell a carried-over row from one this
invocation produced.

--retry-failed additionally re-runs jobs whose recorded outcome was an
infra-class failure. It uses the scorer's own NON_MODEL_FAILURE_CATEGORIES --
infra_failure, api_or_credit, task_data, build_instruction -- which are exactly
the categories excluded from adjusted scoring, so those runs carry no signal
about the model and a re-run is the only way to get one. Genuine model failures
are kept: the model was asked and it failed, and re-running buys the same
answer at full cost. Say the word if you would rather the flag retry those too.

tests/test_batch_resume.py covers the path, which had none: resume passed
`resume=None` everywhere. Eight of its seventeen tests fail against the
log-existence heuristic, checked by swapping that logic back in and holding the
tests constant.

Fixes TIGER-AI-Lab#297. Related: TIGER-AI-Lab#160 asks for exactly this path to be verified.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

One truncated run-meta.json aborts end-of-batch stats before batch-summary.json is written; metadata writes are not atomic

2 participants