"function createCursorNativeUsage(promptId) {\n if (typeof promptId !== \"string\" || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(promptId)) throw new Error(\"Invalid Cursor usage prompt identity\");\n const limits = { maxObservations: 64, maxInvocations: 64, maxBytes: 16384 };\n // Reserve bounded ACPX request/message identities and wrapper keys within 16 KiB.\n const envelopeMaxBytes = limits.maxBytes - 640;\n const fields = [\"inputTokens\", \"outputTokens\", \"cacheReadTokens\", \"cacheWriteTokens\", \"reasoningTokens\"];\n const reasons = new Set([\"native_counter_semantics_unverified\"]);\n const observations = [];\n const children = new WeakMap();\n const trackedInvocations = new Set(); // At most maxInvocations, including children.\n let closed = false, truncated = false, invocations = 0;\n let finished;\n const envelope = () => ({\n schema: \"paperclip.cursor.native-usage.v1\", source: \"native_turn_ended\", promptId,\n completeness: \"partial\", reasons: [...reasons], observations, limits, truncated,\n });\n const truncate = () => { truncated = true; reasons.add(\"observation_limit_reached\"); };\n function begin(role, nativeRun) {\n if (closed) return null;\n if (invocations >= limits.maxInvocations) { truncate(); return null; }\n const invocationId = `invocation-${++invocations}`;\n let ended = false, sequence = 0;\n const collector = {\n observe(update) {\n if (closed || ended || truncated || update?.message?.case !== \"turnEnded\") return;\n if (observations.length >= limits.maxObservations) { truncate(); return; }\n const value = update.message.value;\n const counters = {};\n for (const field of fields) {\n const raw = value?.[field];\n if (raw === undefined) continue;\n const number = typeof raw === \"bigint\" && raw >= 0n && raw <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(raw) : raw;\n if (typeof number === \"number\" && Number.isSafeInteger(number) && number >= 0) counters[field] = number;\n else reasons.add(\"invalid_native_counter\");\n }\n if (Object.keys(counters).length < fields.length) reasons.add(\"native_counters_missing\");\n if (sequence > 0) reasons.add(\"multiple_terminal_observations\");\n observations.push({ invocationId, role, nativeRun, sequence: ++sequence, counters });\n },\n end() { if (!ended && sequence === 0 && !closed) reasons.add(\"native_terminal_not_observed\"); ended = true; },\n };\n trackedInvocations.add(collector);\n return collector;\n }\n return {\n beginParent() { return begin(\"parent\", invocations + 1); },\n observeChild(child, update) {\n if (closed || !child || child.terminal) return;\n if (!Number.isSafeInteger(child.runs) || child.runs < 1) { reasons.add(\"child_run_attribution_unverified\"); return; }\n // The native callback identifies only the child, not its run generation.\n // After ID reuse, delayed old-run updates cannot be distinguished safely.\n if (child.runs !== 1) { reasons.add(\"child_run_attribution_unverified\"); return; }\n let run = children.get(child);\n if (!run || run.nativeRun !== child.runs) {\n run?.collector?.end();\n run = { nativeRun: child.runs, collector: begin(\"child\", child.runs) };\n children.set(child, run);\n }\n run.collector?.observe(update);\n },\n finish() {\n if (finished !== undefined) return JSON.parse(finished);\n for (const invocation of trackedInvocations) invocation.end();\n trackedInvocations.clear();\n closed = true;\n if (observations.length === 0) reasons.add(\"native_terminal_not_observed\");\n // Bound the FINAL envelope after every reason has been added. Its fixed\n // ASCII keys/IDs/reasons make JSON length equal to its UTF-8 byte length.\n finished = JSON.stringify(envelope());\n while (finished.length > envelopeMaxBytes && observations.length > 0) {\n observations.pop(); truncate(); finished = JSON.stringify(envelope());\n }\n // Detached snapshot: late provider callbacks cannot mutate a returned receipt.\n return JSON.parse(finished);\n },\n close() { closed = true; trackedInvocations.clear(); },\n };\n}"