[triage-generated] [bug] Add JSON validation guard to queue.json writes in triage_score.py #952

Closed
opened 2026-03-22 17:42:07 +00:00 by Timmy · 1 comment
Owner

Problem

When triage_score.py fails to connect to Gitea (wrong API URL, timeout, network error), the error or print output can overwrite queue.json with invalid data. The scorer prints summary text to stdout AND writes to the file internally.

If the loop or any caller redirects stdout to queue.json, the file gets corrupted with mixed text and JSON.

Fix

  1. In triage_score.py, after writing queue.json (line 330), immediately validate it by re-reading and parsing.
  2. If validation fails, restore from a .loop/queue.json.bak that gets saved before each write.
  3. In loop_guard.py load_queue(), add a try/except around json.loads() that returns empty list on corrupt data instead of crashing.

Acceptance Criteria

  1. triage_score.py validates queue.json after writing — if invalid, restores backup
  2. loop_guard.py gracefully handles corrupt queue.json (returns empty, logs warning)
  3. A backup .loop/queue.json.bak exists after each triage run

Files

  • scripts/triage_score.py (line 329-330)
  • scripts/loop_guard.py (load_queue() function, ~line 167)
## Problem When `triage_score.py` fails to connect to Gitea (wrong API URL, timeout, network error), the error or print output can overwrite `queue.json` with invalid data. The scorer prints summary text to stdout AND writes to the file internally. If the loop or any caller redirects stdout to queue.json, the file gets corrupted with mixed text and JSON. ## Fix 1. In `triage_score.py`, after writing queue.json (line 330), immediately validate it by re-reading and parsing. 2. If validation fails, restore from a `.loop/queue.json.bak` that gets saved before each write. 3. In `loop_guard.py` `load_queue()`, add a try/except around `json.loads()` that returns empty list on corrupt data instead of crashing. ## Acceptance Criteria 1. `triage_score.py` validates queue.json after writing — if invalid, restores backup 2. `loop_guard.py` gracefully handles corrupt queue.json (returns empty, logs warning) 3. A backup `.loop/queue.json.bak` exists after each triage run ## Files - `scripts/triage_score.py` (line 329-330) - `scripts/loop_guard.py` (`load_queue()` function, ~line 167)
Timmy added the seed:know-purpose label 2026-03-22 17:42:07 +00:00
kimi was assigned by Rockachopa 2026-03-22 22:00:29 +00:00
Collaborator

PR created: http://143.198.27.163:3000/rockachopa/Timmy-time-dashboard/pulls/995

Summary of Changes

triage_score.py

  • Added backup mechanism: Before writing queue.json, saves valid existing content to .loop/queue.json.bak
  • Added validation after write: Re-reads and parses to verify JSON integrity
  • Added restore on failure: If validation fails, restores from backup or writes empty list as fallback

loop_guard.py

  • Enhanced load_queue() with separate handling for JSONDecodeError vs OSError
  • Added descriptive warning logs when queue.json is corrupt or unreadable
  • Returns empty list gracefully instead of crashing

New Tests (12 total)

  • tests/scripts/test_triage_score_validation.py - 6 tests for backup/restore logic
  • tests/loop/test_loop_guard_corrupt_queue.py - 6 tests for graceful handling of corrupt JSON

All 12 new tests pass. Pre-existing test failures (10) are unrelated to these changes.

PR created: http://143.198.27.163:3000/rockachopa/Timmy-time-dashboard/pulls/995 ## Summary of Changes ### triage_score.py - Added backup mechanism: Before writing `queue.json`, saves valid existing content to `.loop/queue.json.bak` - Added validation after write: Re-reads and parses to verify JSON integrity - Added restore on failure: If validation fails, restores from backup or writes empty list as fallback ### loop_guard.py - Enhanced `load_queue()` with separate handling for `JSONDecodeError` vs `OSError` - Added descriptive warning logs when queue.json is corrupt or unreadable - Returns empty list gracefully instead of crashing ### New Tests (12 total) - `tests/scripts/test_triage_score_validation.py` - 6 tests for backup/restore logic - `tests/loop/test_loop_guard_corrupt_queue.py` - 6 tests for graceful handling of corrupt JSON All 12 new tests pass. Pre-existing test failures (10) are unrelated to these changes.
kimi closed this issue 2026-03-22 22:33:41 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Rockachopa/Timmy-time-dashboard#952