Repository navigation
saves: one explicit JSON nesting limit, not the decoder's RecursionError (test_saves fails on Python 3.14/3.15) - #5240
sovljanskii wants to merge 7 commits into
Conversation
|
At 8270d27, extracting a second ASAV into an existing export folder leaves two different revisions of the payload. The command exits 0. The new payload.bin contains the second input. The generated payload.json still contains revision 1 from the first input. Run this from the repository root with from pathlib import Path
import runpy, sys, tempfile, subprocess
t=runpy.run_path("tests/test_saves.py")
with tempfile.TemporaryDirectory() as tmp:
src=Path(tmp)/"input.asav"
out=Path(tmp)/"export"
for body in (b'{"revision":1}', b'{"a":'+b'['*512+b'0'+b']'*512+b'}'):
src.write_bytes(t["make_asav"](kind=2,payload=body))
r=subprocess.run([sys.executable,"-I","tools/saves.py","extract",str(src),str(out)],capture_output=True,text=True)
print(r.returncode,(out/"payload.bin").read_bytes()==body,(out/"payload.json").read_text()[:24],r.stdout.splitlines()[0])Expected: after the binary-only fallback, the folder contains the new payload.bin and no stale generated payload.json. The test for a 513-level payload currently starts with an empty folder. On merge base 3faac55, the second extract accepts that depth and updates both files. I also measured the new depth check with a wide, shallow array. In a fresh process, run: import json, sys, tracemalloc
sys.path.insert(0, "tools")
from saves import json_depth
for count in (10000, 100000, 250000):
value = json.loads("[" + ",".join(["[]"] * count) + "]")
tracemalloc.start()
depth = json_depth(value)
print(count, depth, tracemalloc.get_traced_memory()[1])
tracemalloc.stop()Depth stays at 2. Peak additional Python allocation is 725,048 bytes for 10,000 children, 7,073,376 bytes for 100,000, and 17,927,904 bytes for 250,000. The measurement starts after decoding. stack.extend queues one tuple per child. Keeping one child iterator per active level would make the traversal storage depend on depth. The head suite ran 66 tests with three skips. AI-assisted. |
Kam471
left a comment
There was a problem hiding this comment.
Ran this at 8270d27a6 on Windows 11: pytest tests/test_saves.py -q -> 63 passed, 3 skipped, 90 subtests passed; git diff --check clean.
The saves JSON reader now enforces one explicit nesting limit and reports it as the tool's error, instead of letting the decoder's RecursionError escape (which fails test_saves on Python 3.14/3.15); the added cases cover the limit boundary, the over-limit document and the ordinary save. No blocking issue.
|
@shdann both are right, thanks. Fixed in
|
Hiiva
left a comment
There was a problem hiding this comment.
Independently reviewed exact head 41abf003dd7417eabea11840d3da53825c9a4e4e on Windows 11 x64 with CPython 3.14.6. No actionable finding in the changed save-reader/extraction paths.
python -I tests/test_saves.py: 68 tests run, 65 passed and 3 skipped.- Source review confirms that depth traversal keeps one iterator per active container level and that binary-only ASAV extraction removes a preceding generated
payload.json. - The actual CLI tests cover the 512-level boundary, rejection one level above it, unchanged prior JSAV exports on rejection, ASAV binary fallback, and repeated extraction after a valid JSON payload.
I reran the two new follow-up methods unchanged against a separate copy of the preceding PR implementation, 8270d27a6e2ee0ad2b3d617affe3277cff162801. They produce four failures: the wide-array allocation bound and stale JSON after each of the three binary-only replacement cases. The same methods pass against this head, with no errors in either run.
For a decoded 100,000-child array at depth 2, a separate tracemalloc measurement around json_depth records 7,074,072 bytes of additional peak allocation on the preceding head and 64 bytes on this head. Decoding is outside that measurement.
The exact-head worktree stays clean, git diff --check passes, and the remote head was rechecked before posting. CPU-only synthetic saves; no game files.
AI-assisted: yes.
|
Tested 41abf00 on Linux, Python 3.13 (CPU only, no GPU, no game files): pass with notes Ran
Read
AI-assisted (Claude Code). |
King1565's review on ItsNotRexed#5240: a block nested 511 to 600 levels packed with exit 0, and then info refused the file pack had written ("nests 513 levels deep"). The body puts each block two levels down, so pack now refuses a block deeper than MAX_JSON_DEPTH - 2 and names it. A block that isn't JSON is still copied verbatim, as before. The comment above MAX_JSON_DEPTH said the indenting encoder stops at 994 levels on 3.12, 3.14 and 3.15 alike. It goes tens of thousands of levels from 3.14 on, so the comment now gives the measured figures per release. docs/saves.md states the limit and the block rule.
|
@King1565 thanks, both notes were right.
Tests: |
|
Re-tested c1466fb on Linux, Python 3.13 (CPU only, no game files): pass
AI-assisted (Claude Code). |
kacper-serewis
left a comment
There was a problem hiding this comment.
AI-assisted independent review. Windows recheck of earlier head 41abf00, CPython 3.13.15 and 3.14.2. JSAV info/verify/diff/extract and index packing accepted depth 512 and rejected 513, preserving prior outputs on failure. ASAV retained the binary at 513 and cleared stale pretty JSON. Both 68-test runs had one skip and two pre-existing symlink-spelling subtest failures. The PR has since moved to c1466fb.
kacper-serewis
left a comment
There was a problem hiding this comment.
AI-assisted Windows recheck of c1466fb on CPython 3.13.15 and 3.14.2. The earlier synthetic save round-tripped byte for byte, including three code and two script blocks. Both accepted body depth 512 and rejected 513. An edited block at depth 511 was rejected before output replacement. All 17 focused tests passed on each interpreter. I separately verified that pack also checks index.json itself: depth 513 exited 2, named the 512 limit, and preserved the existing output. The full suite retained the two Windows symlink-spelling subtest failures and one skip.
|
Tested
|
|
Reviewed c1466fb (CPU, Linux, Python 3.13): 1 issues
|
tedin7's review on ItsNotRexed#5240: info on a save folder and import (save_slot) read chives.json with a bare json.loads, so 100,000 nested arrays gave a RecursionError traceback on 3.13 and were accepted from 3.14 on, while docs/saves.md says every command reads at most 512 levels. read_json() decodes a file, turns a decode error or RecursionError into the existing "<path>: invalid JSON (...)" ValueError, and refuses anything deeper than MAX_JSON_DEPTH. info, save_slot and pack's index.json use it, so all three report one ERROR line (exit 2), and import writes nothing.
|
@tedin7 thanks, right. The doc sentence was true for the save formats but not for Change: Tests: |
|
Reviewed e1e99ec (code read + CPU tests on Linux, Python 3.14.8): pass.
Pre-existing, not from this PR: a AI-assisted (Claude Code). |
|
Thanks @cynbun. The non-object
AI-assisted. |
tests/test_saves.py failed 7 subtests on main (3faac55) under Python 3.14 and 3.15, and passed on 3.12. The nesting guards from ItsNotRexed#3565 relied on json raising RecursionError, and how deep the decoder goes depends on the version. Measured as the deepest array nesting that decodes: 9,996 levels on 3.12.16, 116,219 on 3.14.10 and 130,746 on 3.15.2. So from 3.14 on, the 100,000-level fixtures were accepted: - info, verify and diff exited 0 on the deep JSAV body; - extract died with a RecursionError traceback in json.dump(indent=1); - pack read the deep index.json and reported "index must be an object" instead of naming the invalid JSON. The indenting encoder that extract writes with stops at 994 levels on every one of these versions. So on 3.12 too, a body between 994 and 9,996 levels passed info and gave a traceback from extract. extract's ASAV payload path only caught ValueError, so a deep plain-text payload ended in a traceback on any version. saves.py now measures the decoded value without recursion (json_depth) and refuses anything deeper than MAX_JSON_DEPTH = 512, well below the encoder's limit: - a JSAV body: the same "JSAV body is not valid UTF-8 JSON" error, exit 2, for info, verify, diff and extract; - pack's index.json: the same "invalid JSON" error; - an ASAV payload: the existing "did not parse as JSON" note. payload.bin is kept, payload.json is not written, and 3.12's own RecursionError is caught there too. json_depth takes about 130 ms on a 6.4 MB body. Tests: a body exactly at the limit is read and extracted, and one level over is refused by all four read commands with a prior export left alone; ASAV payloads at the limit, one over it, and at 20,000 levels; json_depth on scalars, mixed values and a 100,000-level value. tests/test_saves.py: 66 tests OK on Python 3.12.16, 3.14.10 and 3.15.2 (macOS, Apple M4). Without the saves.py change, the new tests fail and the 7 old subtests fail again on 3.15.
…keeps one iterator per level Two points from shdann's review of 8270d27: - Extracting an ASAV into a folder that already held a payload.json kept that file next to the new payload.bin whenever the new payload wasn't written as JSON: too deep (new in this branch), not valid JSON, or not text at all (both already so on main). extract now removes payload.json in that case, so the folder never holds two revisions of the payload. - json_depth queued one tuple per child, so a wide, shallow array cost memory in proportion to its width. It now keeps one child iterator per open level. Peak allocation on 100,000 empty arrays at depth 2: 7,073,384 bytes before, 64 after (250,000: 17.9 MB -> 64 bytes). Time: 165 -> 18 ms. A 100,000-level value still measures without recursion. tests/test_saves.py: 67 OK on Python 3.12.16, 3.14.10 and 3.15.2 (macOS, Apple M4). Without the saves.py change, the 4 new checks fail.
King1565's review on ItsNotRexed#5240: a block nested 511 to 600 levels packed with exit 0, and then info refused the file pack had written ("nests 513 levels deep"). The body puts each block two levels down, so pack now refuses a block deeper than MAX_JSON_DEPTH - 2 and names it. A block that isn't JSON is still copied verbatim, as before. The comment above MAX_JSON_DEPTH said the indenting encoder stops at 994 levels on 3.12, 3.14 and 3.15 alike. It goes tens of thousands of levels from 3.14 on, so the comment now gives the measured figures per release. docs/saves.md states the limit and the block rule.
tedin7's review on ItsNotRexed#5240: info on a save folder and import (save_slot) read chives.json with a bare json.loads, so 100,000 nested arrays gave a RecursionError traceback on 3.13 and were accepted from 3.14 on, while docs/saves.md says every command reads at most 512 levels. read_json() decodes a file, turns a decode error or RecursionError into the existing "<path>: invalid JSON (...)" ValueError, and refuses anything deeper than MAX_JSON_DEPTH. info, save_slot and pack's index.json use it, so all three report one ERROR line (exit 2), and import writes nothing.
cynbun's review of e1e99ec found that a chives.json holding valid JSON other than an object ([], "story", 5, null) still made info and import stop with an AttributeError traceback, and that a save_directory array made import stop with a TypeError, because the label is looked up in the --map table. Both happen on main as well. read_chives() reads the file through read_json() and then requires an object whose save_directory is a string, null or absent, naming the file and what it found otherwise. info and save_slot (and so import) use it. A missing, null or empty save_directory still falls back to the folder name. ChivesShapeTest covers the five non-object bodies and four non-string labels through save_slot, info and import (exit 2, one ERROR line, no traceback, nothing written), plus the fallback. Against the previous tools/saves.py those nine cases fail or error. test_saves gives 78 OK (1 skipped) on Python 3.15.2, 3.14.10 and 3.12.16 on macOS (Apple M4).
main's ASSET-262 (c2aff9c) made extract write profile.json for a kind-3 ASAV payload. That is a second file describing payload.bin, so it can go stale exactly like payload.json did before this branch: extract a profile record into a folder, then extract another ASAV into the same folder, and the old profile.json stays next to the new payload.bin. extract now removes whichever of payload.json and profile.json this run did not write. The test extracts a profile, then a kind-2 JSON payload or a kind-3 payload with a bad version, into the same folder. Either way profile.json is gone. A profile extracted over a JSON payload removes payload.json. Without the removal, both stale-profile cases fail. test_saves gives 83 OK (1 skipped) on Python 3.15.2, 3.14.10 and 3.12.16 on macOS (Apple M4), after rebasing onto main 561b65a.
32f46b3 to
54dcf19
Compare
|
Rebased onto
AI-assisted. |
|
Re-checked 54dcf19 (the rebase of 32f46b3 onto
AI-assisted (Claude Code). No game files. |
…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.
|
Thanks @cynbun. Pushed I added #4968's If #4968 lands first, the rebase keeps AI-assisted. |
|
Tested 6c93a77 merged with main 561b65a on Linux (WSL2) Python 3.12.16: pass tests/test_saves.py: 83 run, 82 passed, 0 failed, 1 skipped. Also tests/test_save_keys.py, tests/test_saves_import_args.py and all 11 tests/test_docs_*.py exit 0. Negative control: restoring tools/saves.py from upstream/main (keeping the PR's tests) gives 83 run, 8 failures + 25 errors, 1 skipped: the new JsonDepthTest, ChivesJsonDepthTest, ChivesShapeTest and the PackIndexValidationTest depth cases error out (no MAX_JSON_DEPTH / read_chives / json_depth on main). With the PR's tools/saves.py they pass. File restored afterwards. The doc paragraph in docs/saves.md matches the code (MAX_JSON_DEPTH = 512; pack refuses nesting above 512 - JSAV_BLOCK_NESTING = 510; chives.json must be an object with a string/null/absent save_directory). Python 3.12.16 only; I did not run it on 3.14/3.15, where the decoder's own limit is far higher, so the cross-version table in the PR body is not independently checked. |
Claimed in the coordination issue: claim. A new BUILD item.
What failed
tests/test_saves.pyfails 7 subtests onmain3faac557under Python 3.14 and 3.15. It passes on 3.12. The nesting guards from #3565 relied onjsonraisingRecursionError, and how deep the decoder goes depends on the version.json.dump(indent=1))So from 3.14 on, the 100,000-level fixtures are accepted:
info,verifyanddiffexit 0 on the deep JSAV body;extractdies with aRecursionErrortraceback injson.dump(indent=1);packreads the deepindex.jsonand reports "index must be an object" instead of the invalid JSON.On 3.12 too: a body between 994 and 9,996 levels passes
infoand gives a traceback fromextract.extract's ASAV payload path catches onlyValueError, so a deep plain-text payload ends in a traceback on any version.Change
tools/saves.pymeasures the decoded value without recursion (json_depth) and refuses anything deeper thanMAX_JSON_DEPTH = 512, well below the encoder's limit on every version:JSAV body is not valid UTF-8 JSONerror, exit 2, forinfo,verify,diffandextract. Prior exports are untouched.pack'sindex.json: the existinginvalid JSONerror, naming the file.payload.binis kept andpayload.jsonis not written. 3.12's ownRecursionErroris caught there too.json_depthtakes about 130 ms on a 6.4 MB body.Tested
macOS, Apple M4, synthetic containers only.
tests/test_saves.py: 66 tests OK on Python 3.12.16, 3.14.10 and 3.15.2 (on 3.14 and 3.15,mainfails 7).ERRORline and no change to a prior export;payload.json), one over it and at 20,000 levels (kept as binary only, no traceback);json_depthon scalars, mixed values and a 100,000-level value.saves.pychange, the 13 new checks fail and the 7 old subtests fail again on 3.15.python -m compileall -q tools testsandgit diff --check: clean. The test runs underpython -Iin CI's standard-library job (it has__main__, no pytest).AI-assisted.