@antiphony/shared 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,11 +11,20 @@ export { ReportedErrorEvent, buildReportedErrorEvent, reportError } from './obse
11
11
  /**
12
12
  * Audio hygiene / enrichment processing (B5).
13
13
  *
14
- * Antiphony can, when the calling app opts in, run two pieces of audio
15
- * processing on a post's audio: **transcription** (machine transcript, the
16
- * `dev.antiphony.audio.transcript` enrichment) and **denoise** (a cleaned
17
- * audio variant for playback). Both are OFF by default — the app asks for
18
- * them per post via `CreateAudioPostRequest.processing`.
14
+ * Antiphony can, when the calling app opts in, run audio processing on a
15
+ * post's audio. Four stages, classified on two axes (see
16
+ * `specs/enrichment-pipeline.md`):
17
+ *
18
+ * - **Byte-mutating** (`denoise`, `trim`) produce new audio. They compose in
19
+ * order into a SINGLE processed variant — trimmed-and-denoised audio is one
20
+ * artifact, not two — addressed by `processedBlobCid`.
21
+ * - **Derived** (`transcribe`, `waveform`) are pure analysis over the final
22
+ * variant and modify no audio. Because they are a function of the variant,
23
+ * recomputation is always the correct response to their input changing.
24
+ *
25
+ * Every stage is OFF by default — the app asks for them per post via
26
+ * `CreateAudioPostRequest.processing`, or after the fact via the `processing`
27
+ * opt-in on `PATCH /api/v1/posts/{postId}`.
19
28
  *
20
29
  * The work runs asynchronously (outside the create request), so a post
21
30
  * carries a mutable `processing` state that starts `pending` and settles to
@@ -23,6 +32,11 @@ export { ReportedErrorEvent, buildReportedErrorEvent, reportError } from './obse
23
32
  * it is NOT part of the canonical lexicon record and never enters the record
24
33
  * CID (like `kind` and `threadParticipants`), so processing can update it
25
34
  * without changing the post's content address.
35
+ *
36
+ * That immutability is why stage OUTPUT lives here rather than on the embed:
37
+ * `embed.audio.ref.$link`, `embed.durationMs`, and `embed.waveform` are inside
38
+ * the CID and can never be rewritten. The read-time view resolves per field
39
+ * between the record's canonical values and the variant values below.
26
40
  */
27
41
  /**
28
42
  * Per-stage status.
@@ -30,64 +44,271 @@ export { ReportedErrorEvent, buildReportedErrorEvent, reportError } from './obse
30
44
  * - `ready` — completed.
31
45
  * - `failed` — attempted and errored.
32
46
  * - `skipped` — requested but this deployment has no provider for it.
47
+ *
48
+ * A stage returning to `pending` after having been `ready` is NORMAL, not a
49
+ * regression: a byte-mutating stage completing invalidates derived artifacts,
50
+ * which are then recomputed. Clients treat any `pending` stage as "still
51
+ * working", which already covers this.
33
52
  */
34
53
  declare const ProcessingStageStatusSchema: z.ZodEnum<["pending", "ready", "failed", "skipped"]>;
35
54
  type ProcessingStageStatus = z.infer<typeof ProcessingStageStatusSchema>;
36
55
  /**
37
- * What the calling app opts into, on `CreateAudioPostRequest`. Both default
56
+ * The stage names, in the order a multi-stage request runs them:
57
+ * denoise → trim → (transcribe, waveform).
58
+ *
59
+ * All byte-mutating stages run first; the two derived stages then consume the
60
+ * final variant and are mutually independent. Denoise precedes trim
61
+ * deliberately — silence detection keys off a noise floor, so on noisy input
62
+ * the "silence" is not actually quiet and trim under-cuts.
63
+ */
64
+ declare const PROCESSING_STAGES: readonly ["denoise", "trim", "transcribe", "waveform"];
65
+ declare const ProcessingStageSchema: z.ZodEnum<["denoise", "trim", "transcribe", "waveform"]>;
66
+ type ProcessingStage = z.infer<typeof ProcessingStageSchema>;
67
+ /** Stages that produce new audio bytes, composing into one processed variant. */
68
+ declare const BYTE_MUTATING_STAGES: readonly ["denoise", "trim"];
69
+ /** Stages that are pure analysis over the final variant, modifying no audio. */
70
+ declare const DERIVED_STAGES: readonly ["transcribe", "waveform"];
71
+ /**
72
+ * What the calling app opts into, on `CreateAudioPostRequest`. All default
38
73
  * off; only `true` values request a stage.
39
74
  */
40
75
  declare const ProcessingRequestSchema: z.ZodObject<{
41
76
  transcribe: z.ZodOptional<z.ZodBoolean>;
42
77
  denoise: z.ZodOptional<z.ZodBoolean>;
78
+ trim: z.ZodOptional<z.ZodBoolean>;
79
+ waveform: z.ZodOptional<z.ZodBoolean>;
80
+ /**
81
+ * Whether a completed byte-mutating stage should invalidate and recompute
82
+ * the derived artifacts that describe the old audio. Defaults to **true**
83
+ * — a transcript of superseded audio is wrong, not merely stale.
84
+ *
85
+ * `false` opts out, for an app that would rather keep the existing
86
+ * transcript than pay to regenerate it. It does NOT name a stage, so a
87
+ * request carrying only `reprocess` requests no work.
88
+ */
89
+ reprocess: z.ZodOptional<z.ZodBoolean>;
43
90
  }, "strip", z.ZodTypeAny, {
44
- transcribe?: boolean | undefined;
45
91
  denoise?: boolean | undefined;
46
- }, {
92
+ trim?: boolean | undefined;
47
93
  transcribe?: boolean | undefined;
94
+ waveform?: boolean | undefined;
95
+ reprocess?: boolean | undefined;
96
+ }, {
48
97
  denoise?: boolean | undefined;
98
+ trim?: boolean | undefined;
99
+ transcribe?: boolean | undefined;
100
+ waveform?: boolean | undefined;
101
+ reprocess?: boolean | undefined;
49
102
  }>;
50
103
  type ProcessingRequest = z.infer<typeof ProcessingRequestSchema>;
51
104
  /**
52
- * Stored processing state on the post record (storage-layer; not in the CID).
53
- * A stage key is present iff that stage was requested. `denoisedBlobCid`
54
- * points at the cleaned audio variant once denoise completes the record's
55
- * own `embed.audio.ref.$link` stays the ORIGINAL CID (immutable content
56
- * address); only the read-time view swaps playback to the cleaned variant.
105
+ * Per-stage status across all stages the shape shared by the stored state,
106
+ * the hydrated view, and the resolved-initial-state handoff between the route
107
+ * and the service. A key is present iff that stage was requested.
108
+ */
109
+ declare const ProcessingStageMapSchema: z.ZodObject<{
110
+ transcribe: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
111
+ denoise: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
112
+ trim: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
113
+ waveform: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
114
+ }, "strip", z.ZodTypeAny, {
115
+ denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
116
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
117
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
118
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
119
+ }, {
120
+ denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
121
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
122
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
123
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
124
+ }>;
125
+ type ProcessingStageMap = z.infer<typeof ProcessingStageMapSchema>;
126
+ /**
127
+ * An opt-in request resolved against a deployment's capabilities: the initial
128
+ * per-stage state plus the settings the async worker needs to honour it.
129
+ *
130
+ * `reprocess` is carried here — and persisted — rather than passed to the
131
+ * worker as an argument, because the request that asks for the work and the
132
+ * pass that performs it are separated by a queue (step 8). Written on every
133
+ * request including the default, because the stored state is MERGED onto —
134
+ * absent means true only for posts written before this field existed.
135
+ */
136
+ declare const ResolvedProcessingSchema: z.ZodObject<{
137
+ transcribe: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
138
+ denoise: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
139
+ trim: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
140
+ waveform: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
141
+ } & {
142
+ reprocess: z.ZodOptional<z.ZodBoolean>;
143
+ }, "strip", z.ZodTypeAny, {
144
+ denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
145
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
146
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
147
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
148
+ reprocess?: boolean | undefined;
149
+ }, {
150
+ denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
151
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
152
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
153
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
154
+ reprocess?: boolean | undefined;
155
+ }>;
156
+ type ResolvedProcessing = z.infer<typeof ResolvedProcessingSchema>;
157
+ /**
158
+ * Stored processing state on the post record (storage-layer; not in the CID):
159
+ * the per-stage statuses plus the output of the stages themselves.
160
+ *
161
+ * The variant fields below all exist for the same reason — their canonical
162
+ * counterparts live inside the record CID and cannot be updated:
163
+ *
164
+ * - `processedBlobCid` ↔ `embed.audio.ref.$link`
165
+ * - `processedMimeType` ↔ `embed.audio.mimeType` (providers may transcode)
166
+ * - `processedDurationMs` ↔ `embed.durationMs` (trim changes duration)
167
+ * - `waveformPeaks` ↔ `embed.waveform` (the client's peaks describe the original)
57
168
  */
58
169
  declare const ProcessingStateSchema: z.ZodObject<{
59
170
  transcribe: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
60
171
  denoise: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
61
- /** Content CID of the denoised audio variant, once `denoise === 'ready'`. */
62
- denoisedBlobCid: z.ZodOptional<z.ZodString>;
172
+ trim: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
173
+ waveform: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
174
+ } & {
175
+ reprocess: z.ZodOptional<z.ZodBoolean>;
176
+ } & {
177
+ /**
178
+ * Content CID of the processed audio variant — the composed output of every
179
+ * byte-mutating stage that has completed. The record's own
180
+ * `embed.audio.ref.$link` stays the ORIGINAL CID (immutable content
181
+ * address); only the read-time view swaps playback to this variant.
182
+ */
183
+ processedBlobCid: z.ZodOptional<z.ZodString>;
184
+ /**
185
+ * MIME type of the processed variant. Present because providers may
186
+ * TRANSCODE — the ElevenLabs Voice Isolator returns MP3 regardless of what
187
+ * it is given — so the variant's type cannot be assumed to match
188
+ * `embed.audio.mimeType`. Anything reading the variant's bytes must use
189
+ * this, not the embed's.
190
+ */
191
+ processedMimeType: z.ZodOptional<z.ZodString>;
192
+ /**
193
+ * Duration of the processed variant, when a byte-mutating stage changed it
194
+ * (i.e. trim). Absent when the variant's duration matches the original.
195
+ */
196
+ processedDurationMs: z.ZodOptional<z.ZodNumber>;
197
+ /**
198
+ * Peaks for the processed variant, once the `waveform` stage completes.
199
+ * Same normalization and bounds as `embed.waveform` (0–100, max 1000), so
200
+ * a view can never carry a larger payload than the record allows.
201
+ */
202
+ waveformPeaks: z.ZodOptional<z.ZodArray<z.ZodNumber, "many">>;
203
+ /**
204
+ * When the current runner's exclusive claim on this post expires.
205
+ *
206
+ * Queue delivery is at-least-once, so the same job can arrive twice and
207
+ * run CONCURRENTLY. `process()` is idempotent under sequential retry — it
208
+ * acts on `pending` and re-does nothing already settled — but two passes
209
+ * interleaved is a different failure: both read the same `pending` state,
210
+ * both bill the provider for the same stage, and both write
211
+ * `processedBlobCid`, so the surviving variant is whichever finished last
212
+ * and the other's blob is orphaned.
213
+ *
214
+ * A runner claims this field transactionally before doing any work and
215
+ * clears it when finished; a second runner finding it unexpired declines
216
+ * and returns. It is an EXPIRY, not a boolean lock, because the holder can
217
+ * die mid-run (instance recycled, process killed) with no chance to
218
+ * release — a plain flag would strand the post permanently, where a lapsed
219
+ * lease lets the next delivery pick it up.
220
+ *
221
+ * Internal, like the variant fields above: `toProcessingView` projects
222
+ * stages only, so this never reaches a client.
223
+ */
224
+ leaseUntil: z.ZodOptional<z.ZodEffects<z.ZodUnion<[z.ZodType<unknown, z.ZodTypeDef, unknown>, z.ZodString, z.ZodNumber, z.ZodDate]>, Date, unknown>>;
63
225
  updatedAt: z.ZodEffects<z.ZodUnion<[z.ZodType<unknown, z.ZodTypeDef, unknown>, z.ZodString, z.ZodNumber, z.ZodDate]>, Date, unknown>;
64
226
  }, "strip", z.ZodTypeAny, {
65
227
  updatedAt: Date;
66
- transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
67
228
  denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
68
- denoisedBlobCid?: string | undefined;
69
- }, {
229
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
70
230
  transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
231
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
232
+ reprocess?: boolean | undefined;
233
+ processedBlobCid?: string | undefined;
234
+ processedMimeType?: string | undefined;
235
+ processedDurationMs?: number | undefined;
236
+ waveformPeaks?: number[] | undefined;
237
+ leaseUntil?: Date | undefined;
238
+ }, {
71
239
  denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
72
- denoisedBlobCid?: string | undefined;
240
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
241
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
242
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
243
+ reprocess?: boolean | undefined;
244
+ processedBlobCid?: string | undefined;
245
+ processedMimeType?: string | undefined;
246
+ processedDurationMs?: number | undefined;
247
+ waveformPeaks?: number[] | undefined;
248
+ leaseUntil?: unknown;
73
249
  updatedAt?: unknown;
74
250
  }>;
75
251
  type ProcessingState = z.infer<typeof ProcessingStateSchema>;
76
252
  /**
77
253
  * The processing status surfaced on the hydrated view — the per-stage status
78
- * only (no internal storage fields). Absent when no processing was requested.
79
- * Clients treat any `pending` stage as "still working".
254
+ * only (no internal storage fields: variant CID, duration, peaks, timestamps).
255
+ * Absent when no processing was requested.
80
256
  */
81
257
  declare const ProcessingViewSchema: z.ZodObject<{
82
258
  transcribe: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
83
259
  denoise: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
260
+ trim: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
261
+ waveform: z.ZodOptional<z.ZodEnum<["pending", "ready", "failed", "skipped"]>>;
84
262
  }, "strip", z.ZodTypeAny, {
85
- transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
86
263
  denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
87
- }, {
264
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
88
265
  transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
266
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
267
+ }, {
89
268
  denoise?: "pending" | "ready" | "failed" | "skipped" | undefined;
269
+ trim?: "pending" | "ready" | "failed" | "skipped" | undefined;
270
+ transcribe?: "pending" | "ready" | "failed" | "skipped" | undefined;
271
+ waveform?: "pending" | "ready" | "failed" | "skipped" | undefined;
90
272
  }>;
91
273
  type ProcessingView = z.infer<typeof ProcessingViewSchema>;
274
+ /**
275
+ * Project the stored state onto the view — drops every internal field.
276
+ *
277
+ * Derived from `PROCESSING_STAGES` rather than listing the stages by hand, so
278
+ * a stage added to the set cannot be silently omitted from the view (which
279
+ * would leave clients unable to tell "not requested" from "in progress").
280
+ */
281
+ declare function toProcessingView(state: ProcessingState): ProcessingView;
282
+ /** The record's own audio fields — canonical, inside the CID, never rewritten. */
283
+ interface CanonicalAudioFields {
284
+ blobCid: string;
285
+ durationMs?: number;
286
+ waveform?: number[];
287
+ }
288
+ /**
289
+ * Resolve the audio fields a reader should see: canonical from the record,
290
+ * variant from `ProcessingState` wherever processing has superseded it.
291
+ *
292
+ * The three fields have to move together. Peaks are rendered ACROSS a duration
293
+ * and a duration describes a specific set of bytes, so serving a processed URL
294
+ * beside the original duration puts a scrubber out of alignment with the audio
295
+ * under it — the failure this function exists to prevent.
296
+ *
297
+ * Resolution is not a uniform `??` per field, because the state's fields do not
298
+ * all mean the same thing when absent:
299
+ *
300
+ * - `processedDurationMs` absent is DEFINED as "the variant's duration equals
301
+ * the original" (denoise transcodes without retiming), so falling back to
302
+ * the record there is correct, not a guess.
303
+ * - `waveformPeaks` carries no such guarantee. Recompute marks the stage
304
+ * `pending` without clearing the field, so between a variant change and the
305
+ * recomputed peaks landing it holds peaks for the SUPERSEDED variant. Hence
306
+ * the status gate rather than a presence check.
307
+ *
308
+ * Peaks do not key off `processedBlobCid`: `waveform` always runs over the
309
+ * final variant, so when it is `ready` its peaks describe whatever playback
310
+ * resolves to here, variant or original alike.
311
+ */
312
+ declare function resolveAudioVariant(canonical: CanonicalAudioFields, state: ProcessingState | undefined): CanonicalAudioFields;
92
313
 
93
- export { type ProcessingRequest, ProcessingRequestSchema, type ProcessingStageStatus, ProcessingStageStatusSchema, type ProcessingState, ProcessingStateSchema, type ProcessingView, ProcessingViewSchema };
314
+ export { BYTE_MUTATING_STAGES, type CanonicalAudioFields, DERIVED_STAGES, PROCESSING_STAGES, type ProcessingRequest, ProcessingRequestSchema, type ProcessingStage, type ProcessingStageMap, ProcessingStageMapSchema, ProcessingStageSchema, type ProcessingStageStatus, ProcessingStageStatusSchema, type ProcessingState, ProcessingStateSchema, type ProcessingView, ProcessingViewSchema, type ResolvedProcessing, ResolvedProcessingSchema, resolveAudioVariant, toProcessingView };
@@ -46,21 +46,85 @@ var FirestoreTimestampSchema = zod.z.union([
46
46
  return date;
47
47
  });
48
48
  var ProcessingStageStatusSchema = zod.z.enum(["pending", "ready", "failed", "skipped"]);
49
+ var PROCESSING_STAGES = ["denoise", "trim", "transcribe", "waveform"];
50
+ zod.z.enum(PROCESSING_STAGES);
49
51
  zod.z.object({
50
52
  transcribe: zod.z.boolean().optional(),
51
- denoise: zod.z.boolean().optional()
53
+ denoise: zod.z.boolean().optional(),
54
+ trim: zod.z.boolean().optional(),
55
+ waveform: zod.z.boolean().optional(),
56
+ /**
57
+ * Whether a completed byte-mutating stage should invalidate and recompute
58
+ * the derived artifacts that describe the old audio. Defaults to **true**
59
+ * — a transcript of superseded audio is wrong, not merely stale.
60
+ *
61
+ * `false` opts out, for an app that would rather keep the existing
62
+ * transcript than pay to regenerate it. It does NOT name a stage, so a
63
+ * request carrying only `reprocess` requests no work.
64
+ */
65
+ reprocess: zod.z.boolean().optional()
52
66
  });
53
- var ProcessingStateSchema = zod.z.object({
67
+ var ProcessingStageMapSchema = zod.z.object({
54
68
  transcribe: ProcessingStageStatusSchema.optional(),
55
69
  denoise: ProcessingStageStatusSchema.optional(),
56
- /** Content CID of the denoised audio variant, once `denoise === 'ready'`. */
57
- denoisedBlobCid: zod.z.string().optional(),
58
- updatedAt: FirestoreTimestampSchema
70
+ trim: ProcessingStageStatusSchema.optional(),
71
+ waveform: ProcessingStageStatusSchema.optional()
59
72
  });
60
- var ProcessingViewSchema = zod.z.object({
61
- transcribe: ProcessingStageStatusSchema.optional(),
62
- denoise: ProcessingStageStatusSchema.optional()
73
+ var ResolvedProcessingSchema = ProcessingStageMapSchema.extend({
74
+ reprocess: zod.z.boolean().optional()
75
+ });
76
+ var ProcessingStateSchema = ResolvedProcessingSchema.extend({
77
+ /**
78
+ * Content CID of the processed audio variant — the composed output of every
79
+ * byte-mutating stage that has completed. The record's own
80
+ * `embed.audio.ref.$link` stays the ORIGINAL CID (immutable content
81
+ * address); only the read-time view swaps playback to this variant.
82
+ */
83
+ processedBlobCid: zod.z.string().optional(),
84
+ /**
85
+ * MIME type of the processed variant. Present because providers may
86
+ * TRANSCODE — the ElevenLabs Voice Isolator returns MP3 regardless of what
87
+ * it is given — so the variant's type cannot be assumed to match
88
+ * `embed.audio.mimeType`. Anything reading the variant's bytes must use
89
+ * this, not the embed's.
90
+ */
91
+ processedMimeType: zod.z.string().optional(),
92
+ /**
93
+ * Duration of the processed variant, when a byte-mutating stage changed it
94
+ * (i.e. trim). Absent when the variant's duration matches the original.
95
+ */
96
+ processedDurationMs: zod.z.number().int().min(0).optional(),
97
+ /**
98
+ * Peaks for the processed variant, once the `waveform` stage completes.
99
+ * Same normalization and bounds as `embed.waveform` (0–100, max 1000), so
100
+ * a view can never carry a larger payload than the record allows.
101
+ */
102
+ waveformPeaks: zod.z.array(zod.z.number().int().min(0).max(100)).max(1e3).optional(),
103
+ /**
104
+ * When the current runner's exclusive claim on this post expires.
105
+ *
106
+ * Queue delivery is at-least-once, so the same job can arrive twice and
107
+ * run CONCURRENTLY. `process()` is idempotent under sequential retry — it
108
+ * acts on `pending` and re-does nothing already settled — but two passes
109
+ * interleaved is a different failure: both read the same `pending` state,
110
+ * both bill the provider for the same stage, and both write
111
+ * `processedBlobCid`, so the surviving variant is whichever finished last
112
+ * and the other's blob is orphaned.
113
+ *
114
+ * A runner claims this field transactionally before doing any work and
115
+ * clears it when finished; a second runner finding it unexpired declines
116
+ * and returns. It is an EXPIRY, not a boolean lock, because the holder can
117
+ * die mid-run (instance recycled, process killed) with no chance to
118
+ * release — a plain flag would strand the post permanently, where a lapsed
119
+ * lease lets the next delivery pick it up.
120
+ *
121
+ * Internal, like the variant fields above: `toProcessingView` projects
122
+ * stages only, so this never reaches a client.
123
+ */
124
+ leaseUntil: FirestoreTimestampSchema.optional(),
125
+ updatedAt: FirestoreTimestampSchema
63
126
  });
127
+ var ProcessingViewSchema = ProcessingStageMapSchema;
64
128
 
65
129
  // types/audio.ts
66
130
  var StrongRefSchema = zod.z.object({
@@ -96,20 +160,34 @@ var TimedTranscriptSchema = zod.z.object({
96
160
  });
97
161
  var AudioEmbedViewSchema = zod.z.object({
98
162
  $type: zod.z.literal("dev.antiphony.embed.audio#view"),
163
+ /**
164
+ * `url`, `durationMs` and `waveform` are RESOLVED, not copied: once
165
+ * processing has produced an audio variant they describe that variant
166
+ * rather than the bytes the client uploaded. They always agree with one
167
+ * another — a duration and a set of peaks are only meaningful against the
168
+ * audio `url` actually points at.
169
+ *
170
+ * A client that stored `durationMs` at upload time should therefore expect
171
+ * it to change (trim removes leading/trailing silence), and should render
172
+ * these three as a set rather than caching them independently. The record's
173
+ * originals are immutable and unaffected; this is a read-time resolution.
174
+ */
99
175
  url: zod.z.string().url(),
100
176
  durationMs: zod.z.number().int().min(0).optional(),
101
- // `alt`/`waveform` are copied from the stored embed; keep the same bounds
102
- // so a view can never carry a larger payload than the record allows.
177
+ // `alt` is copied from the stored embed; keep the same bounds so a view can
178
+ // never carry a larger payload than the record allows.
103
179
  alt: zod.z.string().max(1e4).optional(),
104
180
  waveform: zod.z.array(zod.z.number().int().min(0).max(100)).max(1e3).optional(),
105
181
  /** Lifted from the transcript enrichment record; absent until transcription completes. */
106
182
  transcript: TimedTranscriptSchema.optional(),
107
183
  /**
108
- * Per-stage audio-processing status (transcribe / denoise), when the app
109
- * opted into processing on create. Absent otherwise. A `pending` stage
110
- * means the client should poll (or re-render) for the result. When
111
- * `denoise === 'ready'`, `url` above already resolves to the cleaned
112
- * audio variant. See `types/processing.ts`.
184
+ * Per-stage audio-processing status (denoise / trim / transcribe /
185
+ * waveform), when the app opted into processing. Absent otherwise. A
186
+ * `pending` stage means the client should poll (or re-render) for the
187
+ * result including a stage that returns to `pending` after having been
188
+ * `ready`, which is how a recompute surfaces. Once a byte-mutating stage
189
+ * completes, `url`/`durationMs`/`waveform` above already resolve to the
190
+ * processed audio variant. See `types/processing.ts`.
113
191
  */
114
192
  processing: ProcessingViewSchema.optional()
115
193
  });