Skip to content

fix: atomic writes and error recovery in ensure_*_issues_file and add_known_issue (#464) - #465

Merged
azalio merged 1 commit into
mainfrom
claude/compassionate-cerf-i75n45
Sep 16, 2026
Merged

azalio merged 1 commit into
mainfrom
claude/compassionate-cerf-i75n45

Conversation

@azalio

@azalio azalio commented Sep 16, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes four non-atomic writes and a bare json.loads() in map_step_runner.py.jinja that could leave the MAP Framework in a permanently broken state after a crash or OOM kill mid-write.

Changes

  • ensure_active_issues_file: replaced path.write_text() with _write_json_file() (atomic tempfile + os.replace)
  • ensure_known_issues_file: same fix
  • add_known_issue: replaced bare json.loads() with safe _read_json_file(); on corrupt or missing file now falls back to KNOWN_ISSUES_DEFAULT instead of raising; type annotation widened from dict[str, object] to dict[str, Any] to satisfy Pyright for downstream .append() and len() calls
  • ensure_human_artifacts: replaced path.write_text() with _write_text_file() (atomic)

All four helpers (_write_json_file, _write_text_file, _read_json_file, atomic_write_text) were already present in the codebase; this PR wires the three ensure_* and add_known_issue functions up to them consistently with the rest of the file.

Tests

Three regression tests added to tests/test_map_step_runner.py:

Test Covers
TestEnsureActiveIssuesFile.test_corrupt_file_is_not_written_partially corrupt file is not overwritten (no partial write)
TestEnsureKnownIssuesFile.test_new_file_write_is_valid_json new file write produces valid JSON
TestAddKnownIssue.test_add_known_issue_recovers_from_corrupt_file corrupt known-issues.json triggers safe fallback

Verification

ruff check src/ tests/           → All checks passed!
python -m pyright src/           → 0 errors, 0 warnings, 0 informations
make check-render                → Generated trees match templates_src ✅
pytest tests/ -x -q --timeout=60 → 5805 passed, 4 skipped

Closes #464


🤖 Generated with Claude Code

https://claude.ai/code/session_014U8hzu49WSe2TXQqPmnD82


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of issue-tracking files to avoid partial overwrites when existing data is invalid.
    • Known-issue updates now recover gracefully from corrupted files instead of failing.
    • New issue files are created with valid, consistently formatted JSON.
  • Tests

    • Added coverage for corrupted-file handling, valid file creation, and recovery when adding known issues.

…_known_issue (#464)

Four non-atomic writes and a bare json.loads() in map_step_runner could leave the
MAP Framework in a broken state after a crash mid-write:

- ensure_active_issues_file: replace write_text() with _write_json_file() (atomic)
- ensure_known_issues_file: same fix
- add_known_issue: replace bare json.loads() with _read_json_file(); on corrupt/missing
  file fall back to KNOWN_ISSUES_DEFAULT instead of raising; type annotation changed
  from dict[str, object] to dict[str, Any] to satisfy Pyright downstream operations
- ensure_human_artifacts: replace write_text() with _write_text_file() (atomic)

Three regression tests added:
- test_corrupt_file_is_not_written_partially (active-issues.json)
- test_new_file_write_is_valid_json (known-issues.json)
- test_add_known_issue_recovers_from_corrupt_file

Full suite: 5805 passed, 4 skipped.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014U8hzu49WSe2TXQqPmnD82
@azalio
azalio merged commit b8c16dd into main Sep 16, 2026
1 of 2 checks passed
@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 2763f9c5-08ac-40a2-947e-19a4c98777f9

📥 Commits

Reviewing files that changed from the base of the PR and between d2bf326 and 2bad492.

📒 Files selected for processing (4)
  • .map/scripts/map_step_runner.py
  • src/mapify_cli/templates/map/scripts/map_step_runner.py
  • src/mapify_cli/templates_src/map/scripts/map_step_runner.py.jinja
  • tests/test_map_step_runner.py

📝 Walkthrough

Walkthrough

The change routes map-step file writes and JSON reads through shared helpers. Known-issue appends now recover from corrupt files. Tests cover atomic file preservation, valid JSON creation, and corrupt-file recovery.

Changes

Issue file I/O

Layer / File(s) Summary
Shared file helpers and recovery
.map/scripts/map_step_runner.py, src/mapify_cli/templates/map/scripts/map_step_runner.py, src/mapify_cli/templates_src/map/scripts/map_step_runner.py.jinja, tests/test_map_step_runner.py
Text and JSON writes use _write_text_file and _write_json_file. Known-issue reads use _read_json_file with a default fallback. Tests cover corrupt files and newly created JSON files.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: vladislav00

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/compassionate-cerf-i75n45

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the issue file bright
Safe writes guard the data through the night
Corrupt JSON finds a fresh new start
Shared helpers keep each path in part
Tests hop lightly, proving each chart
Carrots celebrate the repaired heart

Comment @coderabbitai help to get the list of available commands.

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.

Bug: non-atomic write_text() in ensure_active_issues_file / ensure_known_issues_file — missed by #456 batch

2 participants