Hmbown/CodeWhale · error · ValueError

max_chars must be > 0

Error message

max_chars must be > 0

What it means

chunk() splits the REPL input into full-coverage slices of at most max_chars characters. A non-positive max_chars would produce an infinite/empty chunking loop, so the function rejects max_chars <= 0 with this ValueError before doing any work.

Solutions

  1. Pass a positive integer, e.g. chunk(max_chars=20000)
  2. Validate the source config/flag before calling and clamp with max(1, value)
  3. If deriving dynamically, ensure the computed budget is positive before the call

Example fix

// before
chunk(max_chars=0)
// after
chunk(max_chars=20000)
Defensive patterns

Strategy: validation

Validate before calling

const mc = Math.trunc(Number(maxChars));
if (!Number.isFinite(mc) || mc <= 0) throw new Error("max_chars must be > 0 before calling chunk()");

Type guard

const isPositiveInt = (v) => Number.isInteger(v) && v > 0;

Try / catch

try { const chunks = chunk(maxChars, overlap); } catch (e) { if (String(e).includes("max_chars must be")) { return chunk(20000, overlap); } throw e; }

Prevention

When it happens

Trigger: Calling chunk(max_chars=0), a negative max_chars, or a value that int() coerces to <= 0 (e.g. max_chars="0", max_chars=None raising earlier, max_chars=-500).

Common situations: Computing max_chars from a config or CLI flag that defaulted to 0; an off-by-sign bug when deriving the budget from a remaining-token count; passing an empty string that int() coerces unexpectedly in generated calls.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/10bf35e2a9ae80dd. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/repl/runtime.rs:895

            break
        start, end = m.span()
        snippet_start = max(0, start - 120)
        snippet_end = min(len(_context), end + 120)
        hits.append({
            "index": i,
            "start": start,
            "end": end,
            "match": m.group(0),
            "snippet": _context[snippet_start:snippet_end],
        })
    return hits

def chunk(max_chars=20000, overlap=0):
    """Return full-coverage input chunks with index/start/end/text fields."""
    max_chars = int(max_chars)
    overlap = max(0, int(overlap))
    if max_chars <= 0:
        raise ValueError("max_chars must be > 0")
    if overlap >= max_chars:
        raise ValueError("overlap must be smaller than max_chars")
    chunks = []
    start = 0
    idx = 0
    total = len(_context)
    while start < total:
        end = min(total, start + max_chars)
        chunks.append({"index": idx, "start": start, "end": end, "text": _context[start:end]})
        idx += 1
        if end >= total:
            break
        start = end - overlap
    return chunks

def chunk_context(max_chars=20000, overlap=0):
    """Compatibility alias for chunk()."""
    return chunk(max_chars=max_chars, overlap=overlap)

View on GitHub (pinned to 73e0f67d83)