earendil-works/pi · error · TypeError

Protocol JSON objects must be plain objects

Error message

Protocol JSON objects must be plain objects

What it means

An object crossing the protocol boundary has a prototype other than Object.prototype (or null), e.g. a class instance, Date, Map, or cross-realm object. toProtocolJsonValue only copies plain objects; such values must be converted to plain data first.

Solutions

  1. Send plain objects only; class instances are not supported.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/server/src/protocol.ts:152 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/1fc3e6c124e36797. Report an issue: GitHub.

Appendix: source

Thrown at packages/server/src/protocol.ts:152

function timestamp(value: number): number {
	if (!Number.isSafeInteger(value) || value < 0)
		throw new TypeError("Protocol timestamps must be non-negative integers");
	return value;
}

/** Validate and copy a value from an execution boundary into the protocol's JSON-compatible subset. */
export function toProtocolJsonValue(value: unknown, seen = new Set<object>()): JsonValue {
	if (value === null || typeof value === "string" || typeof value === "boolean") return value;
	if (typeof value === "number") {
		if (!Number.isFinite(value)) throw new TypeError("Protocol JSON numbers must be finite");
		return value;
	}
	if (typeof value !== "object") throw new TypeError(`Unsupported protocol JSON value: ${typeof value}`);
	if (seen.has(value)) throw new TypeError("Protocol JSON values must not contain circular references");
	const prototype = Object.getPrototypeOf(value);
	if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
		throw new TypeError("Protocol JSON objects must be plain objects");
	}
	seen.add(value);
	try {
		if (Array.isArray(value)) return Array.from(value, (entry) => toProtocolJsonValue(entry, seen));
		const result: Record<string, JsonValue> = {};
		for (const [key, entry] of Object.entries(value)) result[key] = toProtocolJsonValue(entry, seen);
		return result;
	} finally {
		seen.delete(value);
	}
}

/** Lossily sanitize diagnostic tool details that must not affect execution semantics. */
export function sanitizeProtocolDetails(value: unknown, seen = new Set<object>()): JsonValue | undefined {
	if (value === null || typeof value === "string" || typeof value === "boolean") return value;
	if (typeof value === "number") return Number.isFinite(value) ? value : String(value);
	if (typeof value === "bigint") return value.toString();
	if (value === undefined || typeof value === "function" || typeof value === "symbol") return undefined;

View on GitHub (pinned to 4af9d21d3b)