Skip to content

fix(collector): handle malformed previous overview and allow self-healing (#1) - #3

Open
goanscryca wants to merge 1 commit into
ariadev:mainfrom
goanscryca:fix/corrupt-previous-state-self-heal
Open

goanscryca wants to merge 1 commit into
ariadev:mainfrom
goanscryca:fix/corrupt-previous-state-self-heal

Conversation

@goanscryca

Copy link
Copy Markdown

Resolves #1

Summary

When bin/gitwork reads its previous overview file (-output, e.g. overview.json) to carry forward state, an invalid JSON file (such as one corrupted by an interrupted write or trailing bytes) caused jq -c . to fail or stream multiple tokens. As a result, passing it to jq --argjson previous "$previous" failed with invalid JSON text passed to --argjson, permanently crashing every subsequent collector run.

Changes

  1. Robust JSON validation: Read and validate the previous overview using jq -c -s 'if length == 1 and (.[0] | type == "object") then .[0] else empty end'. If the document is not a single valid JSON object, it is treated as missing (null).
  2. Self-healing: If the existing file was malformed non-empty JSON, gitwork emits a warning to stderr and proceeds with an empty previous state, replacing the broken file with a fresh, valid overview.
  3. Regression tests: Added test case in tests/collector-test.sh asserting that a corrupted previous file (overview.json with trailing syntax errors) emits a warning, recovers cleanly, writes valid JSON, and preserves the schema version.

Verification

All test suites pass:

  • tests/calendar-test.sh: 38 passed, 0 failed
  • tests/carry-forward-test.sh: 25 passed, 0 failed
  • tests/collector-test.sh: 37 passed, 0 failed
  • tests/hosts-test.sh: 32 passed, 0 failed
  • tests/items-test.sh: 52 passed, 0 failed

…elf-healing

When reading previous state from -output, an invalid or truncated JSON
document (e.g. from an interrupted write or trailing bytes) caused jq
to fail or emit multiple values, breaking --argjson previous and
aborting subsequent collector runs indefinitely.

Now gitwork validates that the previous file is a single JSON object.
If it is invalid or empty, gitwork logs a warning to stderr, falls back
to null (empty state) and replaces the corrupt file with a valid overview
on the current run.

Resolves ariadev#1
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.

A single failed run permanently corrupts the collector state, and it can never self-heal

1 participant