Skip to content

saves: a non-object chives.json is one ERROR line, not a traceback - #4968

Open
SebSilent wants to merge 1 commit into
ItsNotRexed:mainfrom
SebSilent:fix/saves-info-chives-type
Open

SebSilent wants to merge 1 commit into
ItsNotRexed:mainfrom
SebSilent:fix/saves-info-chives-type

Conversation

@SebSilent

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new BUILD item, saves info non-object chives.json

What and why

tools/saves.py info <folder> reads the folder's chives.json and prints the save directory:

meta = json.loads(read(cj))
print(f'{p}: save folder, dir {meta.get("save_directory")}')

There is no guard on the parsed value's type. A well-formed JSON whose top level is not an object ([], null, 5, "x") reaches meta.get(...) and raises AttributeError ('list' object has no attribute 'get', etc.). main() only catches (ValueError, struct.error, OSError):

except (ValueError, struct.error, OSError) as e:
    # A malformed save must report one clear line, not a traceback.
    print('ERROR: %s' % e, file=sys.stderr)
    return 2

so the AttributeError escapes as a full traceback, exit 1. Invalid JSON syntax already exits 2 (it is a ValueError); only the well-formed-non-object case crashes.

pack_from_dir in the same file already guards its parsed index the intended way:

if not isinstance(idx, dict):
    invalid('index', 'an object')   # raise ValueError(f'{index_path}: {field} must be {expected}')

This makes info() consistent: raise ValueError(f'{cj}: chives.json must be an object') when the parsed meta is not a dict, which main() turns into one ERROR: line, exit 2.

Tested

Linux (CachyOS), Python 3.14.10. CPU only, no game files; every chives.json fixture is synthetic JSON written by the test.

  • python -I tests/test_saves.py: 63 tests OK (1 skip), including the new InfoChivesTest:
    • test_a_non_object_chives_is_one_error_line: 5 subtests ([], null, 5, "x", [1, 2]), each asserting exit 2, no Traceback, and ERROR: ... chives.json must be an object on stderr.
    • test_an_object_chives_still_reports_the_folder: a valid {"save_directory": "SLOT0"} still prints save folder, dir SLOT0, exit 0.
  • Red before: with the tool change stashed, all 5 subtests fail (exit 1, AttributeError traceback); green after with the fix.
  • python -m compileall -q tools/saves.py tests/test_saves.py, git diff --check: clean.

Note: open #1438 edits extract/pack and adds helpers after info(), not the chives.json read; #4872's meta.get is in import_saves (chives.dat). Whichever lands second rebases.

AI-assisted: yes (Tacit).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a, no translator rule)

info() reads a save folder's chives.json and calls meta.get('save_directory')
with no type guard. A well-formed JSON that is not an object (list/null/
number/string) raised AttributeError, exit 1, while main() only catches
(ValueError, struct.error, OSError). pack_from_dir in the same file already
guards its index with ValueError(f'{path}: {field} must be ...'), which main
turns into one ERROR line exit 2. Raise the same ValueError when chives.json
is not an object. Five synthetic subtests plus a positive control, red before
green after.
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 2e4e238 on Windows 11, CPython 3.14.7, CPU only (no GPU, no game files): pass.

py -3.14 -I tests/test_saves.py runs 63 tests. 60 pass and three skip. With the same test file on merge base 3faac557, all five new non-object cases fail.

I also ran py -3.14 -I tools/saves.py info <folder> with synthetic UTF-16 metadata. For both false and 0.5, the PR exits with code 2 and prints one error line. The merge base exits with code 1 and prints an 11-line traceback. For {} and {"save_directory":"SLOT0"}, both revisions exit with code 0 and print the same folder information.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 2e4e238 merged with main 3faac55 on Linux (WSL2, Ubuntu), CPython 3.12.16, -I, CPU only: pass.

tests/test_saves.py: 63 run, 62 passed, 0 failed, 1 skipped.

Negative control: with tools/saves.py taken from main, the same file runs 63 with 5 failures; restored, 0 failures. The diff is the two-line isinstance(meta, dict) guard, which raises ValueError that info() already reports as one ERROR line.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 2e4e238 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_saves.py -> Ran 63 tests, FAILED (failures=2, skipped=1) - 2 are the known test_destination_symlinks_update_the_target_and_remain_links Windows symlink-spelling quirk, identical on upstream/main
  • Negative control (tools/saves.py swapped to upstream/main): FAILED (failures=7) - differentiates on the PR's cases
  • Verdict: pass aside from the pre-existing platform failures.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Gap (checked on Linux, Python 3.14.8, synthetic ASAV from the tests' make_asav, tools/saves.py on main 3faac557 and on this head): info() is guarded here, but save_slot() (used by import, ~tools/saves.py:450-451) reads chives.json the same way and still crashes:

  • chives.json = []: import <folder> --dry-run ends in AttributeError: 'list' object has no attribute 'get' from save_slot, on main and with this PR.
  • {"save_directory": ["a"]}: TypeError: cannot use 'list' as a dict key from sd in maps (and f'{sd:34}' would fail too), so save_directory also needs a string-or-absent check.

One option: a small shared reader used by both callers that checks isinstance(meta, dict) and the save_directory type, with the same ERROR: line as here. Note #5240 (open) rewrites both of these read lines into a read_json() helper, so the two PRs will conflict on adjacent lines in info(). AI-assisted (Claude Code).

sovljanskii added a commit to sovljanskii/Leonida that referenced this pull request Nov 23, 2026
…checks

The two PRs report the same mistake, so they should use the same words for it. This one only adds
the kind that was found ("not an array"). With this change ItsNotRexed#4968's InfoChivesTest passes on this
branch as it stands. Whichever of the two lands second rebases only the code in info(), not a test.

AI-assisted.
@sovljanskii

Copy link
Copy Markdown
Contributor

@SebSilent a heads-up: #5240 covers the same non-object chives.json case. It also covers import and a save_directory that isn't a string or null. The two conflict in info() in tools/saves.py.

At 6c93a77e, #5240 uses this PR's words (chives.json must be an object, plus the kind found), and your InfoChivesTest passes on that head unchanged. Whichever of the two lands second only needs its code in info() rebased. No test has to change.

AI-assisted.

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.

6 participants