From bfff737de1d40feff71de63c484d2cecb60937e0 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Sun, 15 Nov 2026 11:11:04 +0000 Subject: [PATCH] fix(novelty): a too-deeply nested routing file is no partial result Settles the review point of #764 that `_is_partial_file` caught OSError and ValueError but not RecursionError: a routing file holding about 20000 `[` made `save_result` raise after a paid, capped run. `_is_partial_file` now also catches RecursionError, so such a file is treated like one this cannot read: not a partial result, and the capped run writes its partial result to the sidecar beside it. A test pins both the guard and `save_result` over a 20000-deep file. Changelog fragment (Fixed). Signed-off-by: Anton Volkov --- .../20261115_120000_partial-guard-recursion.md | 3 +++ dokimi/dokimi/commands/novelty_routing_report.py | 2 +- .../tests/test_novelty_routing_spend_cap_partial.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 dokimi/changelog.d/20261115_120000_partial-guard-recursion.md diff --git a/dokimi/changelog.d/20261115_120000_partial-guard-recursion.md b/dokimi/changelog.d/20261115_120000_partial-guard-recursion.md new file mode 100644 index 000000000..212756c36 --- /dev/null +++ b/dokimi/changelog.d/20261115_120000_partial-guard-recursion.md @@ -0,0 +1,3 @@ +### Fixed + +- **A routing result file nested too deep to parse is treated as no partial result, and a capped `novelty routing` run no longer raises `RecursionError` while saving** — the overwrite guard keeps such a file and writes the partial result to a sidecar beside it, as it does for any file it cannot read. diff --git a/dokimi/dokimi/commands/novelty_routing_report.py b/dokimi/dokimi/commands/novelty_routing_report.py index 63cceb000..833416387 100644 --- a/dokimi/dokimi/commands/novelty_routing_report.py +++ b/dokimi/dokimi/commands/novelty_routing_report.py @@ -529,7 +529,7 @@ def _is_partial_file(path: Path) -> bool: """Whether ``path`` holds a partial result; a file this cannot read is not one.""" try: earlier = json.loads(path.read_text(encoding="utf-8")) - except (OSError, ValueError): + except (OSError, ValueError, RecursionError): # RecursionError: nesting too deep for json return False return isinstance(earlier, dict) and ("partial" in earlier or earlier.get("status") == "partial") diff --git a/dokimi/tests/test_novelty_routing_spend_cap_partial.py b/dokimi/tests/test_novelty_routing_spend_cap_partial.py index f27f0409f..de980a97b 100644 --- a/dokimi/tests/test_novelty_routing_spend_cap_partial.py +++ b/dokimi/tests/test_novelty_routing_spend_cap_partial.py @@ -825,3 +825,15 @@ def test_a_file_marked_partial_by_status_alone_is_a_partial_result_to_write_over assert _is_partial_file(marked) assert save_result({}, marked, {"partial": PARTIAL}) == marked assert json.loads(marked.read_text(encoding="utf-8"))["partial"] == PARTIAL + + +def test_a_file_nested_too_deep_to_parse_is_not_a_partial_result_and_save_result_keeps_it(tmp_path): + from dokimi.commands.novelty_routing_report import _is_partial_file, save_result + + deep = tmp_path / "routing.json" + deep.write_text("[" * 20000, encoding="utf-8") + assert not _is_partial_file(deep) + saved = save_result({}, deep, {"partial": PARTIAL}) + assert saved != deep + assert deep.read_text(encoding="utf-8") == "[" * 20000 + assert json.loads(saved.read_text(encoding="utf-8"))["partial"] == PARTIAL