# Buddy audio and nano usage integration

## Implemented in the private website
- `/usage`: live refresh of the latest 100 privately saved local audio diagnostics, every five seconds while visible.
- AudioWorklet microphone energy meter: explicit permission, 30-second cap, RMS threshold 0.015 and 200 ms hold. Stop, page-hide and Buddy-off release microphone tracks and audio resources. These are tuning parameters, not proof of semantic speech detection.
- 10 Hz meter updates. No claim of measured 60 fps, zero latency or shared-memory WebSocket transport.
- Audio never leaves the worklet. Only counters/energy reach the page. Saving diagnostics explicitly sends sample counts, gate counts, sample rate and block count; no waveform or transcript.
- Authenticated diagnostic API with tenant isolation, bounded input and unique owner/event key. Same event replay is idempotent; changed payload conflicts. Browser measurements are not trusted billing evidence.
- Exact 15-digit fractional cost calculator using integer arithmetic and explicit user-entered token counts/rates. Costs are not earnings. No hardcoded model response, rate, tokens or cash balances.

## Native source included, not running as a hosted service
`native/metering/journal.py` provides a stdlib SQLite WAL usage-cost outbox. It durably commits each event before returning, validates duplicate payloads, prepares immutable bounded batches transactionally, supports async append through a worker thread, and retains unacknowledged batches across restart. Exact cost numerators are integer strings with denominator 10^21 currency units; distinct currencies are never summed together.

Two native tests verify retries/restarts, acknowledgement, conflicting duplicates, exact small amounts and concurrent writers. This is not lock-free or a double-entry financial ledger. SQLite serializes writes. No measured throughput or latency claim is made. For remote delivery, replay pending batches with their original identifiers and acknowledge only after a destination commits the same batch idempotently. The journal does not implement that external sink, verify payment settlement or create a wallet.

## Not enabled
- Raw PCM WebSocket upload, ASR, native inference or model learning.
- Redis, Supabase, DigitalOcean containers, systemd service, DOCR CI or Triton/CUDA kernels.
- Provider token ingestion, automatic earnings, funded rewards, deposits, transfers, withdrawals or currency conversion.
- Mobile-device microphone testing or native journal integration into the hosted Cloudflare runtime.

These need an authenticated runtime endpoint, selected actual model/tokenizer, deployment target and credentials, agreed rate versions, a funded payer and a tested settlement destination. The website uses its existing D1/R2 stack. No unrelated infrastructure was provisioned.

## Corrections to the submitted examples
`ScriptProcessorNode` is deprecated; use AudioWorklet. RMS is normalized sound energy, not decibels and not semantic speech recognition. Browser-to-server WebSockets are not POSIX zero-copy shared memory. A receiver that merely counts bytes does not transcribe or reason.

`numeric(30,15)` means 30 significant digits total and 15 fractional digits, not 30 decimal places. A ledger insert followed by read-modify-write wallet updates is not atomic. Retrying after partial failure can duplicate credits; clearing only in-memory state risks loss on crash. Neither sound buffers nor API output generate money without a funded contract.

DeepSeek exposes actual token usage fields; character length is not an exact tokenizer. Free chat does not prove zero serving cost or a sustainable price for our workload. Claims about private profitability and trading subsidy have not been verified and are not marketing copy here.

## Sources checked 2026-08-28
- https://developer.mozilla.org/en-US/docs/Web/API/AudioWorklet
- https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode
- https://www.postgresql.org/docs/current/datatype-numeric.html
- https://api-docs.deepseek.com/api/create-chat-completion/
- https://api-docs.deepseek.com/quick_start/pricing/
