fix(merge-chunks): coerce non-numeric chunk token counts (follow-up to #1953)

An untrusted chunk with a non-numeric input_tokens/output_tokens would abort
the whole merge with a TypeError after other chunks had already merged. Coerce
to 0 so a bad token field can't defeat the per-chunk validation guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
safishamsi
2026-07-16 23:38:09 +01:00
co-authored by Claude Opus 4.8
parent ee74d80b45
commit 3305ef1059
+6 -2
View File
@@ -3064,8 +3064,12 @@ def dispatch_command(cmd: str) -> None:
merged["nodes"].append(n)
merged["edges"].extend(chunk.get("edges", []))
merged["hyperedges"].extend(chunk.get("hyperedges", []))
merged["input_tokens"] += chunk.get("input_tokens", 0)
merged["output_tokens"] += chunk.get("output_tokens", 0)
# Coerce token counts: a chunk is untrusted, so a non-numeric
# input_tokens/output_tokens must not abort the whole merge with a
# TypeError after other chunks already merged.
for _tok in ("input_tokens", "output_tokens"):
_v = chunk.get(_tok, 0)
merged[_tok] += _v if isinstance(_v, (int, float)) else 0
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(merged, ensure_ascii=False), encoding="utf-8")
print(