/** * Audio PII redaction — span production. * * Pure, browser- + node-safe span math for the corpus PII scrub's audio lane: * text-PII verdicts (the matched surface strings from the tier-0 detectors / * `PII_SCRUB` model verdicts) are located inside a timed-ASR word stream * ({@link TranscriptWord}, ms from audio start — the same contract the * transcript player uses) and turned into padded, merged, non-overlapping * `[{startMs, endMs}]` windows an executor (pure-TS WAV zeroing or ffmpeg * `volume=0:enable=…`) can mute or bleep. * * Design constraints this module honors: * * - **Raw word spans in, no anchor seam dependency.** Input is the raw timed * ASR word list (any producer that satisfies the {@link TranscriptWord} * contract — fused `eliza_inference_asr_transcribe_timed`, an * OpenAI-compatible STT with `timestamp_granularities=word`, …), not a * transcript-fragment anchor record. * - **Fail-closed on unlocatable PII.** A PII span that cannot be located in * the word stream is returned in `unmatched` — the caller MUST treat a * non-empty `unmatched` as a hard failure (typed error, quarantined item), * never as "nothing to mute". Silence here would leak audible PII. * - **Over-redaction is the safe direction.** Matching is * separator-insensitive (normalized concatenation), so "555 0123" in the * verdict matches an ASR word "5550123." and vice versa; a rare over-broad * match mutes a little extra audio, never leaks any. * - **Labels are plain input.** Pseudonym/cluster labels (corpus map, * ) ride through untouched for observability; they never influence * the produced windows. * * The re-transcribe verifier is deliberately a SEPARATE module * (`audio-redaction-verify.ts`) with its own provider contract, so the span * producer and the verifier can run on different ASR backends (see the * acceptance note on ). */ import { ElizaError } from "./errors.js"; import type { TranscriptWord } from "./transcripts.js"; /** One redaction window, ms from audio start (same clock as word timings). */ export interface AudioRedactionSpan { startMs: number; endMs: number; /** Pseudonym/cluster labels of the PII that produced this window (audit). */ labels?: readonly string[]; } /** One text-PII verdict to locate in the word stream. */ export interface PiiTextSpan { /** The matched PII surface text (`Tier0Span.span` / `PiiScrubVerdict.span`). */ text: string; /** Pseudonym/cluster label from the corpus map — plain input. */ label?: string; } /** * Default padding applied to each side of a matched window. Empirically * calibrated on a live TTS fixture + faster-whisper word timings: at 150 ms a * boundary fragment of a planted phone number ("five…") survived the mute and * was re-transcribed as a stray digit; at 250–300 ms the re-transcription * carried no PII fragment at all (only non-PII filler hallucinated over the * gap). ASR word boundaries are estimates — pad generously. */ export declare const DEFAULT_REDACTION_PAD_MS = 250; /** Thrown for structurally invalid span input (fail-closed, never dropped). */ export declare class RedactionSpanError extends ElizaError { constructor(message: string); } /** * Normalize spoken text for separator-insensitive matching: lowercase and * strip everything that is not a Unicode letter or digit. `"555 0123."` and * `" 5550123"` both normalize to `"5550123"`. */ export declare function normalizeSpokenText(raw: string): string; /** Result of locating text-PII verdicts inside the word stream. */ export interface PiiSpanMatchResult { /** One raw (unpadded, unmerged) window per located occurrence. */ matches: AudioRedactionSpan[]; /** * PII spans that could NOT be located in the word stream. Non-empty means * the redaction job MUST fail typed/observable — audible PII would survive. */ unmatched: PiiTextSpan[]; } /** * Locate every occurrence of each text-PII span inside the timed word stream * and emit one `[startMs, endMs]` window per occurrence (first matched word's * start → last matched word's end). Matching is separator-insensitive in both * directions: a verdict spanning several ASR words ("John Smith") and an ASR * word fusing several verdict tokens ("5550123.") both match. ALL occurrences * are windowed — a name spoken three times is muted three times. */ export declare function matchPiiSpansToWords(words: readonly TranscriptWord[], piiSpans: readonly PiiTextSpan[]): PiiSpanMatchResult; /** Options for {@link mergeRedactionSpans}. */ export interface MergeRedactionSpanOptions { /** * Audio duration in ms — windows are clamped to `[0, durationMs]`. Pass `0` * to skip the upper clamp (mirrors `validateAsrWordTimings`). */ durationMs: number; /** Padding added to each side of every window before merging. */ padMs?: number; /** * Two windows closer than this merge into one (avoids leaving unmutable * slivers between adjacent PII words). `0` still merges touching windows. */ mergeGapMs?: number; } /** * Pad, clamp, sort, and merge raw windows into a non-overlapping ascending * list of integer-ms redaction spans. Overlapping, adjacent, and contained * windows collapse; labels of merged windows union. Malformed input * (non-finite, negative length) THROWS — a silently dropped window would leak * audible PII. Windows entirely outside `[0, durationMs]` throw because a * timing-domain mismatch must never become a byte-identical success. */ export declare function mergeRedactionSpans(spans: readonly AudioRedactionSpan[], options: MergeRedactionSpanOptions): AudioRedactionSpan[]; /** Result of the full text-PII → timestamp-window derivation. */ export interface AudioRedactionSpanPlan { /** Padded, merged, non-overlapping windows ready for the executor. */ spans: AudioRedactionSpan[]; /** Raw per-occurrence matches before pad/merge (audit trail). */ matches: AudioRedactionSpan[]; /** PII spans not locatable in the word stream — MUST fail the job if set. */ unmatched: PiiTextSpan[]; } /** * Full pipeline step: locate text-PII verdicts in the timed word stream, then * pad + merge into executor-ready windows. The caller owns the fail-closed * decision on `unmatched` (throw typed, quarantine the item) — this module * only guarantees it is never silently empty when PII could not be located. */ export declare function buildAudioRedactionSpans(words: readonly TranscriptWord[], piiSpans: readonly PiiTextSpan[], options: MergeRedactionSpanOptions): AudioRedactionSpanPlan; /** Fail-closed workflow gate before an executor receives a span plan. */ export declare function assertCompleteAudioRedactionPlan(plan: AudioRedactionSpanPlan): void;