{"record":{"id":"d7024e5578aa7282","repo":"MemPalace/mempalace","slug":"field-name-is-size-bytes-maximum-is-max-byte","errorCode":null,"errorMessage":"{field_name} is {size} bytes; maximum is {max_bytes} bytes","messagePattern":"(.+?) is (.+?) bytes; maximum is (.+?) bytes","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mempalace/logstream.py","lineNumber":152,"sourceCode":"        return None\n    if not isinstance(value, str) or value not in EVENT_STATUSES:\n        allowed = \", \".join(sorted(EVENT_STATUSES))\n        raise ValueError(f\"status={value!r} is not one of: {allowed}\")\n    return value\n\n\ndef _sanitize_body(value, max_bytes: int, field_name: str = \"body\") -> str:\n    \"\"\"Validate verbatim payload text. Empty is allowed; ``None`` becomes ''.\"\"\"\n    if value is None:\n        return \"\"\n    if not isinstance(value, str):\n        raise ValueError(f\"{field_name} must be a string\")\n    if \"\\x00\" in value:\n        raise ValueError(f\"{field_name} contains null bytes\")\n    value = strip_lone_surrogates(value)\n    size = len(value.encode(\"utf-8\"))\n    if size > max_bytes:\n        raise ValueError(f\"{field_name} is {size} bytes; maximum is {max_bytes} bytes\")\n    return value\n\n\ndef _sanitize_metadata(value) -> str:\n    \"\"\"Validate optional metadata dict and return its canonical JSON text.\"\"\"\n    if value is None:\n        return \"{}\"\n    if not isinstance(value, dict):\n        raise ValueError(\"metadata must be an object\")\n    try:\n        encoded = json.dumps(value, ensure_ascii=False, sort_keys=True)\n    except (TypeError, ValueError) as exc:\n        raise ValueError(f\"metadata is not JSON-serializable: {exc}\") from None\n    if len(encoded.encode(\"utf-8\")) > MAX_METADATA_BYTES:\n        raise ValueError(f\"metadata exceeds maximum size of {MAX_METADATA_BYTES} bytes\")\n    return encoded\n\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/logstream.py#L134-L170","documentation":"The UTF-8 encoding of the event body exceeds max_body_bytes (default 256 KiB, configurable via the Logstream constructor). The logstream never truncates payloads silently — the design contract is verbatim storage with explicit size errors — so oversized bodies raise instead. The check runs after surrogate stripping, on the actual byte length.","triggerScenarios":"append_event with a body over 262,144 bytes by default; multi-byte UTF-8 content (CJK, emoji) whose byte length exceeds the cap even when under it in characters; callers who raised max_body_bytes on one replica but not another.","commonSituations":"Pasting whole log files or stack traces into an event body; agents forwarding full conversation transcripts as bodies; a config change lowering the limit while old producers still send large payloads.","solutions":["Split the payload across multiple events (e.g. task.request chunks) or attach a stored artifact via put_artifact and reference its id in artifact_ids.","Check size first: if len(body.encode('utf-8')) > ls.max_body_bytes: ... before calling.","If large bodies are legitimate, raise the limit explicitly in the Logstream(db_path=..., max_body_bytes=...) constructor on every replica."],"exampleFix":"// before\nevt = ls.append_event(type=\"log.dump\", body=huge_log_text, ...)\n// after\nart = ls.put_artifact(kind=\"log\", content=huge_log_text, created_by=\"mac-codex\")\nevt = ls.append_event(type=\"log.dump\", body=\"see artifact\", artifact_ids=[art[\"id\"]], ...)","handlingStrategy":"validation","validationCode":"def fits_body(ls, body: str) -> bool:\n    return isinstance(body, str) and len(body.encode(\"utf-8\")) <= ls.max_body_bytes\n\nif not fits_body(ls, body):\n    art = ls.put_artifact(kind=\"note\", content=body, created_by=from_agent)\n    body, artifact_ids = \"see artifact\", [art[\"id\"]]","typeGuard":"def body_within_limit(ls, body) -> bool:\n    return body is None or (isinstance(body, str) and len(body.encode(\"utf-8\")) <= ls.max_body_bytes)","tryCatchPattern":"try:\n    evt = ls.append_event(..., body=body)\nexcept ValueError as e:\n    if \"maximum is\" in str(e) and \"bytes\" in str(e):\n        art = ls.put_artifact(kind=\"note\", content=body, created_by=from_agent)\n        evt = ls.append_event(..., body=\"see artifact\", artifact_ids=[art[\"id\"]])\n    else:\n        raise","preventionTips":["Measure bytes, not characters: len(text.encode('utf-8')) against ls.max_body_bytes.","Default to artifacts for anything log-sized; keep bodies human-scale.","Keep max_body_bytes identical across every replica that must interoperate."],"tags":["validation","logstream","size-limit","body"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}