JuliusBrussee/caveman · error
runtime policy response stream returned invalid bytes
Error message
runtime policy response stream returned invalid bytes
What it means
While streaming the policy response, each chunk must be a Uint8Array. If reader.read() returns a value that is not a Uint8Array, the library throws this error indicating the byte stream contract was violated.
Solutions
- Ensure chunks enqueued into the stream are Uint8Array instances (encode strings with TextEncoder)
- In mocks, use real Uint8Array values instead of strings/objects
- If chunk is a different typed array, wrap it: new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength)
- Check for cross-realm issues and compare via ArrayBuffer.isView/constructor instead of relying on the library fix
Example fix
// before
stream.enqueue("policy-json-text");
// after
stream.enqueue(new TextEncoder().encode("policy-json-text")); Defensive patterns
Strategy: validation
Validate before calling
const chunks = [new TextEncoder().encode("policy")];
if (!chunks.every(c => c instanceof Uint8Array)) throw new Error("mock stream must enqueue Uint8Array chunks"); Type guard
function isByteChunk(v: unknown): v is Uint8Array {
return v instanceof Uint8Array;
} Try / catch
try {
const text = await readRuntimePolicyResponseText(response);
} catch (e) {
if (/invalid bytes/.test(e.message)) console.error("stream produced non-byte chunks — fix encoder/mock");
else throw e;
} Prevention
- Always TextEncoder-encode strings before enqueueing to streams
- Use Uint8Array (not Buffer subclasses from other realms) in mocks
- Beware cross-realm Uint8Array in iframe/vm test setups
When it happens
Trigger: A reader whose read() yields non-Uint8Array chunks — typically a mocked or custom ReadableStream in tests, or a runtime returning a different typed-array/window type (e.g. cross-realm Uint8Array).
Common situations: Test doubles queuing strings or plain objects as chunks; cross-realm issues (chunk from another iframe/vm context failing instanceof); custom TransformStreams emitting decoded strings.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- artifactId is required
- assemble requires model and sessionId
- cave artifact response missing artifact_id
- cave checkpoint response missing source_ref or messages
- cave_harness_adapter_version_invalid
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/7ac0003f431e0218.
Report an issue: GitHub.
Appendix: source
Thrown at packages/sdk/typescript/src/index.ts:890
/** Runtime-policy envelopes are KB-sized; stop a hostile body after 1 MiB. */
const RUNTIME_POLICY_MAX_RESPONSE_BYTES = 1024 * 1024;
async function readRuntimePolicyResponseText(response: Response): Promise<string> {
const contentLength = response.headers?.get?.("content-length");
if (typeof contentLength === "string" && /^[0-9]+$/.test(contentLength) && Number(contentLength) > RUNTIME_POLICY_MAX_RESPONSE_BYTES) {
await Promise.resolve(response.body?.cancel?.()).catch(() => undefined);
throw new Error("oversized_response");
}
const reader = response.body?.getReader?.();
if (!reader) throw new Error("bounded_response_unavailable");
const chunks: Uint8Array[] = [];
let total = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (!(value instanceof Uint8Array)) throw new Error("runtime policy response stream returned invalid bytes");
total += value.byteLength;
if (total > RUNTIME_POLICY_MAX_RESPONSE_BYTES) {
await Promise.resolve(reader.cancel()).catch(() => undefined);
throw new Error("oversized_response");
}
chunks.push(value);
}
} finally {
try {
reader.releaseLock();
} catch {
// A broken fetch implementation must not mask the bounded-read result.
}
}
const joined = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
joined.set(chunk, offset);View on GitHub (pinned to 3ee70a1026)