Canonical bencode output
Bencora is a Windows coding client for reviewing canonical bencode output. Supply relevant project context to a compatible model and inspect its handling of byte keys, container depth and output size.
def bencode(value):
active, visited, used, parts = set(), 0, 0, []
def emit(*chunks):
nonlocal used
size = sum(len(chunk) for chunk in chunks)
if size > 65536 - used:
raise ValueError("bencode.output")
used += size
parts.extend(chunks)
def encode(item, depth):
nonlocal visited
visited += 1
if depth > 16 or visited > 2048:
raise ValueError("bencode.bounds")
if type(item) is int:
if not -10**18 <= item <= 10**18:
raise ValueError("bencode.integer")
emit(b"i", str(item).encode("ascii"), b"e")
elif type(item) is bytes:
if len(item) > 65536:
raise ValueError("bencode.bytes")
emit(str(len(item)).encode("ascii"), b":", item)
elif type(item) in (list, dict):
if id(item) in active or len(item) > 64:
raise ValueError("bencode.container")
active.add(id(item))
try:
emit(b"l" if type(item) is list else b"d")
if type(item) is list:
for child in item:
encode(child, depth+1)
else:
if any(type(k) is not bytes for k in item):
raise ValueError("bencode.key")
for key in sorted(item):
encode(key, depth+1)
encode(item[key], depth+1)
emit(b"e")
finally:
active.remove(id(item))
else:
raise ValueError("bencode.type")
encode(value, 0)
return b"".join(parts)
- Project
- Test empty containers and nesting, rejecting cycles, Booleans, integers outside ±10^18, more than 64 container entries, depth above 16 or over 2048 visited values.
- Review before applying
- Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
- Connection
- Base URL, credentials and a reachable server.
def bencode(value):
active, visited, used, parts = set(), 0, 0, []
def emit(*chunks):
nonlocal used
size = sum(len(chunk) for chunk in chunks)
if size > 65536 - used:
raise ValueError("bencode.output")
used += size
parts.extend(chunks)
def encode(item, depth):
nonlocal visited
visited += 1
if depth > 16 or visited > 2048:
raise ValueError("bencode.bounds")
if type(item) is int:
if not -10**18 <= item <= 10**18:
raise ValueError("bencode.integer")
emit(b"i", str(item).encode("ascii"), b"e")
elif type(item) is bytes:
if len(item) > 65536:
raise ValueError("bencode.bytes")
emit(str(len(item)).encode("ascii"), b":", item)
elif type(item) in (list, dict):
if id(item) in active or len(item) > 64:
raise ValueError("bencode.container")
active.add(id(item))
try:
emit(b"l" if type(item) is list else b"d")
if type(item) is list:
for child in item:
encode(child, depth+1)
else:
if any(type(k) is not bytes for k in item):
raise ValueError("bencode.key")
for key in sorted(item):
encode(key, depth+1)
encode(item[key], depth+1)
emit(b"e")
finally:
active.remove(id(item))
else:
raise ValueError("bencode.type")
encode(value, 0)
return b"".join(parts)
Keep serialization changes compatible and bounded
Binary output can change meaning without looking different in an ordinary text diff. Bencora is a Windows AI coding workspace for reviewing serialization code with that risk in view. Open your project and ask the connected model to inspect the accepted values, output contract and consuming code before it suggests a patch.
A concrete task might review canonical bencode output for a metadata writer. Ask the assistant to examine byte-key ordering, nested containers and rejection of unsupported types. Provide known output fixtures and describe the limits your application needs. Keep the proposal focused on that writer and its callers rather than assuming a serializer change is harmless because it produces valid-looking bytes.
Check resource boundaries alongside ordinary round trips. Run your own tests for maximum depth, oversized output and cyclic containers, and compare exact bytes for representative dictionaries. Inspect cleanup paths after rejection so a failed attempt does not corrupt later work. Compatibility needs evidence from the project’s consumers as well as examples that encode successfully.
Bencora supports connections to OpenAI-compatible or Anthropic endpoints. An uncensored AI model can be selected where offered by your configured service, with tools, restrictions and context determined by that setup. Use the conversation to explain the change, while your fixtures establish whether the output contract remains intact.
Reviewing a binary writer safely
Why compare exact bytes rather than decoded values alone?
Equivalent decoded values can conceal differences in ordering or representation that matter to a canonical format. Keep known byte fixtures for those requirements and inspect them alongside any round-trip checks your project already uses.
What context matters for dictionary ordering?
Show the actual key types and the consumers that rely on the encoded output. Ask the assistant to distinguish byte ordering from language-specific text ordering. A convenient string conversion can alter a contract that was defined over raw bytes.
Should output size limits be checked only after encoding?
Review the resource policy your application needs. A final size check may occur after substantial work or allocation. Ask where the proposal enforces bounds and test rejection paths, including nested values that exceed the permitted output.
Why include a cyclic-container case?
It checks how the writer handles an input that cannot be expanded as an ordinary finite tree. Verify explicit rejection and cleanup. A model’s successful encoding examples do not establish that malformed container structures terminate correctly.
Bencora · Updated 2026-10-25