fix(agent): use atomic writes for context length YAML cache - #35140
fix(agent): use atomic writes for context length YAML cache#35140annguyenNous wants to merge 1 commit into
Conversation
save_context_length() and _invalidate_cached_context_length() wrote directly to the YAML cache file with open(path, 'w'). A crash mid-write truncates the file to zero/partial bytes, corrupting the cache and losing all cached context lengths. Replace with atomic_yaml_write() from utils.py which uses temp file + fsync + os.replace — the previous version remains intact if the process crashes during the write.
|
Subset of #29019 which also converts model_metadata to atomic writes (among 6 other files). Focused single-file fix. |
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved
Overview
Clean 3-line fix replacing direct file writes with atomic_yaml_write for the context length YAML cache. 3 additions, 7 deletions.
What's Right
- Uses existing atomic_yaml_write utility that handles temp file, fsync, os.replace, and permissions
- Prevents cache corruption from mid-write crashes
- Removes duplicated mkdir+open+write pattern (DRY improvement)
- All 102 model_metadata tests pass
- Minimal, focused change with clear safety benefit
Safety
- atomic_yaml_write creates temp file in same directory (same filesystem, rename is atomic)
- fsync ensures data reaches disk before rename
- os.replace is atomic on POSIX, falls back to shutil.move on Windows
- Previous cache version preserved if crash occurs during write
Reviewed by Hermes Agent (cron)
|
Thanks for the focused crash-safety fix. The premise is confirmed on current main: both Problems
Suggested changes
Automated hermes-sweeper review. |
|
Resolved via PR #85509 (merge commit 6def7ce). You were the FIRST to submit this fix — 8 days before the duplicate #40919 — and you're credited as first submitter in the salvage PR body. The cherry-pick used #40919's implementation because it carried a regression test for the interrupted-write case; the code change itself is the same |
Problem
save_context_length()and_invalidate_cached_context_length()inagent/model_metadata.pywrite directly to the YAML context length cache file usingopen(path, "w") + yaml.dump(). If the process crashes mid-write, the file is truncated to zero or partial bytes, corrupting the cache and losing all cached context lengths.The codebase already has
atomic_yaml_write()inutils.pywhich uses the correct temp-file + fsync +os.replace()pattern.Fix
Replace direct
open(path, "w") + yaml.dump()calls withatomic_yaml_write(path, data). The existing helper handles directory creation, temp file, fsync, atomic rename, and file permission preservation.Before vs After
Tests
All 102 model_metadata tests pass, including:
TestContextLengthCache::test_save_and_loadTestContextLengthCache::test_corrupted_yaml_returns_emptyTestContextLengthCache::test_idempotent_save