{"record":{"id":"10bf35e2a9ae80dd","repo":"Hmbown/CodeWhale","slug":"max-chars-must-be-0","errorCode":null,"errorMessage":"max_chars must be > 0","messagePattern":"max_chars must be > 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"crates/tui/src/repl/runtime.rs","lineNumber":895,"sourceCode":"            break\n        start, end = m.span()\n        snippet_start = max(0, start - 120)\n        snippet_end = min(len(_context), end + 120)\n        hits.append({\n            \"index\": i,\n            \"start\": start,\n            \"end\": end,\n            \"match\": m.group(0),\n            \"snippet\": _context[snippet_start:snippet_end],\n        })\n    return hits\n\ndef chunk(max_chars=20000, overlap=0):\n    \"\"\"Return full-coverage input chunks with index/start/end/text fields.\"\"\"\n    max_chars = int(max_chars)\n    overlap = max(0, int(overlap))\n    if max_chars <= 0:\n        raise ValueError(\"max_chars must be > 0\")\n    if overlap >= max_chars:\n        raise ValueError(\"overlap must be smaller than max_chars\")\n    chunks = []\n    start = 0\n    idx = 0\n    total = len(_context)\n    while start < total:\n        end = min(total, start + max_chars)\n        chunks.append({\"index\": idx, \"start\": start, \"end\": end, \"text\": _context[start:end]})\n        idx += 1\n        if end >= total:\n            break\n        start = end - overlap\n    return chunks\n\ndef chunk_context(max_chars=20000, overlap=0):\n    \"\"\"Compatibility alias for chunk().\"\"\"\n    return chunk(max_chars=max_chars, overlap=overlap)","sourceCodeStart":877,"sourceCodeEnd":913,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/crates/tui/src/repl/runtime.rs#L877-L913","documentation":"chunk(max_chars=20000, overlap=0) in the embedded Python REPL coerces max_chars with int() and raises ValueError when it is 0 or negative. A positive window is what guarantees the chunk loop advances and keeps chunk counts bounded for large inputs. Note that int('abc') raises a different 'invalid literal' ValueError, so this exact message means the value was numeric but not positive.","triggerScenarios":"Calling chunk(0), chunk(-1), chunk('0'), or passing a computed size that collapses to zero, e.g. max_chars = total // 10**9 or a ratio that rounds down to 0.","commonSituations":"Generated code deriving the window from a fraction of context length that underflows; passing 0 expecting 'use the default' semantics from other APIs; unit confusion between bytes, tokens, and chars.","solutions":["Pass a positive window such as the 20000 default","Clamp computed values: max_chars = max(1, int(computed))","Skip chunking entirely for empty inputs; chunk returns [] anyway"],"exampleFix":"# before\nsize = len(text) // 1_000_000\nchunks = chunk(max_chars=size)\n# after\nchunks = chunk(max_chars=20000)","handlingStrategy":"validation","validationCode":"window = int(computed_size)\nif window <= 0:\n    window = 20000  # fall back to the documented default\nchunks = chunk(max_chars=window)","typeGuard":null,"tryCatchPattern":"try:\n    chunks = chunk(max_chars=size)\nexcept ValueError:\n    chunks = chunk()  # defaults: max_chars=20000, overlap=0","preventionTips":["Clamp computed window sizes with max(1, ...) at the source","Never use 0 to mean 'default'; this API has no such convention","Sanity-check derived sizes against the context length before chunking"],"tags":["python","repl","validation","chunking"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}