Artur Mukhamadiev 38a8ca67f7 agent: pi runtime track handoff (contract revision 1)
Pinned pi SDK 0.85.1 bridge, Python supervisor, artifact exporter,
scripted backend peer, image checks and boundary checks under agent/**.
Review findings F1-F3 are recorded in docs/implementation/PI_AGENT_REVIEW.md.
2026-09-14 21:57:54 +03:00

31 lines
1.4 KiB
TypeScript

/** Bounded serialization fragments: large escaped strings never form one copy. */
export function* jsonPieces(value: any, depth = 0): Generator<string> {
if (depth > 64) throw new Error('JSON nesting limit');
if (typeof value === 'string') {
yield '"';
for (let offset = 0; offset < value.length;) {
let end = Math.min(value.length, offset + 16384);
if (end < value.length && /[\uD800-\uDBFF]/.test(value[end - 1])) end--;
yield JSON.stringify(value.slice(offset, end)).slice(1, -1); offset = end;
}
yield '"';
} else if (Array.isArray(value)) {
yield '[';
for (let i = 0; i < value.length; i++) { if (i) yield ','; yield* jsonPieces(value[i] ?? null, depth + 1); }
yield ']';
} else if (value && typeof value === 'object') {
yield '{'; let first = true;
for (const key of Object.keys(value)) {
if (value[key] === undefined || typeof value[key] === 'function' || typeof value[key] === 'symbol') continue;
if (!first) yield ','; first = false;
yield JSON.stringify(key); yield ':'; yield* jsonPieces(value[key], depth + 1);
}
yield '}';
} else { yield JSON.stringify(value) ?? 'null'; }
}
export function jsonBytes(value: any, limit = Number.MAX_SAFE_INTEGER): number {
let bytes = 0;
for (const part of jsonPieces(value)) { bytes += Buffer.byteLength(part); if (bytes > limit) return bytes; }
return bytes;
}