pocketstation 1.0.1

Source-aware desktop audio Session SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;

use crate::frame::{SessionId, SourceId, StreamId};
use crate::graph::{
    ConfigError, ExecutionPartition, NodeConfig, NodeDefinition, NodeDescriptor, NodeTypeId,
    PortDirection, PortSpec, SafetyContract, SignalContinuityTracker, SignalEnvelope,
};
use crate::runtime::{
    TypedEdgeBranchSpec, TypedEdgeBuildError, TypedEdgeFanout, TypedEdgePublishError,
    TypedEdgeReceiver,
};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SourceTypeId(String);

impl SourceTypeId {
    pub fn new(value: impl Into<String>) -> Result<Self, SourceManifestError> {
        let value = value.into();
        if value.trim().is_empty() {
            return Err(SourceManifestError::EmptySourceTypeId);
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SourceConfiguration {
    values: BTreeMap<String, String>,
}

impl SourceConfiguration {
    pub fn insert(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.values.insert(key.into(), value.into());
    }

    pub fn get(&self, key: &str) -> Option<&str> {
        self.values.get(key).map(String::as_str)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
        self.values
            .iter()
            .map(|(key, value)| (key.as_str(), value.as_str()))
    }
}

#[derive(Debug, Clone)]
pub struct SourceManifest {
    pub(crate) source_type_id: SourceTypeId,
    pub(crate) revision: u32,
    pub(crate) generation: u32,
    pub(crate) outputs: Vec<PortSpec>,
    pub(crate) execution: ExecutionPartition,
    pub(crate) safety: SafetyContract,
}

impl SourceManifest {
    pub fn new(
        source_type_id: SourceTypeId,
        revision: u32,
        generation: u32,
        outputs: Vec<PortSpec>,
        execution: ExecutionPartition,
        safety: SafetyContract,
    ) -> Result<Self, SourceManifestError> {
        let manifest = Self {
            source_type_id,
            revision,
            generation,
            outputs,
            execution,
            safety,
        };
        manifest.validate()?;
        Ok(manifest)
    }

    pub const fn source_type_id(&self) -> &SourceTypeId {
        &self.source_type_id
    }

    pub const fn revision(&self) -> u32 {
        self.revision
    }

    pub const fn generation(&self) -> u32 {
        self.generation
    }

    pub fn outputs(&self) -> &[PortSpec] {
        &self.outputs
    }

    pub const fn execution(&self) -> ExecutionPartition {
        self.execution
    }

    pub const fn safety(&self) -> SafetyContract {
        self.safety
    }

    pub fn validate(&self) -> Result<(), SourceManifestError> {
        if self.revision == 0 || self.generation == 0 {
            return Err(SourceManifestError::ZeroVersion);
        }
        if !self.safety.is_valid_for(self.execution) {
            return Err(SourceManifestError::InvalidSafetyContract);
        }
        if self.execution != ExecutionPartition::BlockingWorker {
            return Err(SourceManifestError::UnsupportedExecutionPartition);
        }
        if self.outputs.is_empty() {
            return Err(SourceManifestError::NoOutputs);
        }
        let mut names = BTreeSet::new();
        for output in &self.outputs {
            if output.direction != PortDirection::Output {
                return Err(SourceManifestError::NonOutputPort);
            }
            if output.name.trim().is_empty() {
                return Err(SourceManifestError::EmptyOutputName);
            }
            if !names.insert(output.name.as_str()) {
                return Err(SourceManifestError::DuplicateOutputName);
            }
            output
                .signal
                .validate()
                .map_err(|_| SourceManifestError::InvalidSignal)?;
            if !output.media.supports_signal(&output.signal) {
                return Err(SourceManifestError::SignalMediaMismatch);
            }
        }
        Ok(())
    }

    pub fn output_port(&self, name: &str) -> Option<&PortSpec> {
        self.outputs.iter().find(|output| output.name == name)
    }
}

#[derive(Debug, Clone)]
pub struct SourcePrepareContext {
    pub manifest: SourceManifest,
    pub session: Option<SourceSessionContext>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceOutputIdentity {
    pub output_port: String,
    pub stream_id: StreamId,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceSessionContext {
    pub session_id: SessionId,
    pub source_id: SourceId,
    pub outputs: Vec<SourceOutputIdentity>,
}

impl SourceSessionContext {
    pub fn output(&self, output_port: &str) -> Option<&SourceOutputIdentity> {
        self.outputs
            .iter()
            .find(|output| output.output_port == output_port)
    }
}

#[derive(Clone)]
pub struct SourceCancellation {
    cancelled: Arc<AtomicBool>,
}

impl SourceCancellation {
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Acquire)
    }
}

#[derive(Debug)]
pub struct SourceEmission {
    pub output_port: String,
    pub envelope: SignalEnvelope,
    pub terminal: bool,
}

pub trait SourceDriver: Send {
    fn prepare(&mut self, context: &SourcePrepareContext) -> Result<(), SourceDriverError>;
    fn next(
        &mut self,
        cancellation: &SourceCancellation,
    ) -> Result<Option<SourceEmission>, SourceDriverError>;
    fn close(&mut self) -> Result<(), SourceDriverError>;
}

pub trait SourceFactory: Send + Sync {
    fn manifest(&self) -> &SourceManifest;
    fn validate_config(&self, configuration: &SourceConfiguration) -> Result<(), ConfigError>;
    fn create(
        &self,
        configuration: &SourceConfiguration,
    ) -> Result<Box<dyn SourceDriver>, SourceDriverError>;
}

#[derive(Default)]
pub struct SourceRegistry {
    factories: BTreeMap<SourceTypeId, Arc<dyn SourceFactory>>,
}

impl SourceRegistry {
    pub fn manifest(&self, source_type_id: &SourceTypeId) -> Option<&SourceManifest> {
        self.factories
            .get(source_type_id)
            .map(|factory| factory.manifest())
    }

    pub fn register(
        &mut self,
        factory: Arc<dyn SourceFactory>,
    ) -> Result<(), SourceRegistrationError> {
        factory
            .manifest()
            .validate()
            .map_err(SourceRegistrationError::InvalidManifest)?;
        let source_type_id = factory.manifest().source_type_id.clone();
        if self.factories.contains_key(&source_type_id) {
            return Err(SourceRegistrationError::DuplicateSourceType(source_type_id));
        }
        self.factories.insert(source_type_id, factory);
        Ok(())
    }

    pub fn validate_config(
        &self,
        source_type_id: &SourceTypeId,
        configuration: &SourceConfiguration,
    ) -> Result<(), SourceRuntimeError> {
        let factory = self
            .factories
            .get(source_type_id)
            .ok_or_else(|| SourceRuntimeError::UnregisteredSource(source_type_id.clone()))?;
        factory
            .validate_config(configuration)
            .map_err(SourceRuntimeError::InvalidConfiguration)
    }

    #[cfg(any(test, feature = "internal-testing"))]
    pub fn spawn(
        &self,
        source_type_id: &SourceTypeId,
        configuration: &SourceConfiguration,
        branch_specs: &[SourceOutputBranchSpec],
    ) -> Result<(SourceRuntime, Vec<SourceOutputReceiver>), SourceRuntimeError> {
        let (prepared, receivers) = self.prepare(source_type_id, configuration, branch_specs)?;
        Ok((prepared.start()?, receivers))
    }

    #[cfg(any(test, feature = "internal-testing"))]
    pub fn prepare(
        &self,
        source_type_id: &SourceTypeId,
        configuration: &SourceConfiguration,
        branch_specs: &[SourceOutputBranchSpec],
    ) -> Result<(PreparedSourceRuntime, Vec<SourceOutputReceiver>), SourceRuntimeError> {
        let factory = self
            .factories
            .get(source_type_id)
            .cloned()
            .ok_or_else(|| SourceRuntimeError::UnregisteredSource(source_type_id.clone()))?;
        PreparedSourceRuntime::prepare(factory, configuration, branch_specs, None)
    }

    pub fn prepare_session(
        &self,
        source_type_id: &SourceTypeId,
        configuration: &SourceConfiguration,
        branch_specs: &[SourceOutputBranchSpec],
        session: SourceSessionContext,
    ) -> Result<(PreparedSourceRuntime, Vec<SourceOutputReceiver>), SourceRuntimeError> {
        let factory = self
            .factories
            .get(source_type_id)
            .cloned()
            .ok_or_else(|| SourceRuntimeError::UnregisteredSource(source_type_id.clone()))?;
        PreparedSourceRuntime::prepare(factory, configuration, branch_specs, Some(session))
    }
}

#[derive(Debug, Clone)]
pub struct SourceOutputBranchSpec {
    pub output_port: String,
    pub branch: TypedEdgeBranchSpec,
}

pub struct SourceOutputReceiver {
    pub output_port: String,
    pub receiver: TypedEdgeReceiver,
}

#[derive(Default)]
struct SourceRuntimeObservationState {
    emitted_total: AtomicU64,
    dropped_total: AtomicU64,
    failure_total: AtomicU64,
    cancellation_total: AtomicU64,
    discontinuity_total: AtomicU64,
    recovery_total: AtomicU64,
    policy_change_total: AtomicU64,
    ready: AtomicBool,
    joined: AtomicBool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceRuntimeObservations {
    pub emitted_total: u64,
    pub dropped_total: u64,
    pub failure_total: u64,
    pub cancellation_total: u64,
    pub discontinuity_total: u64,
    pub recovery_total: u64,
    pub policy_change_total: u64,
    pub ready: bool,
    pub joined: bool,
}

#[derive(Clone)]
pub struct SourceRuntimeObservationHandle {
    state: Arc<SourceRuntimeObservationState>,
}

impl SourceRuntimeObservationHandle {
    pub fn snapshot(&self) -> SourceRuntimeObservations {
        SourceRuntimeObservations {
            emitted_total: self.state.emitted_total.load(Ordering::Relaxed),
            dropped_total: self.state.dropped_total.load(Ordering::Relaxed),
            failure_total: self.state.failure_total.load(Ordering::Relaxed),
            cancellation_total: self.state.cancellation_total.load(Ordering::Relaxed),
            discontinuity_total: self.state.discontinuity_total.load(Ordering::Relaxed),
            recovery_total: self.state.recovery_total.load(Ordering::Relaxed),
            policy_change_total: self.state.policy_change_total.load(Ordering::Relaxed),
            ready: self.state.ready.load(Ordering::Acquire),
            joined: self.state.joined.load(Ordering::Acquire),
        }
    }
}

pub struct SourceRuntime {
    cancellation: SourceCancellation,
    observations: SourceRuntimeObservationHandle,
    join: Option<JoinHandle<Result<(), SourceRuntimeError>>>,
}

/// Fully validated source resources which have not started producing signals.
///
/// Keeping this state distinct is what lets Session prepare every bounded
/// branch and endpoint transactionally before the first source callback runs.
pub struct PreparedSourceRuntime {
    driver: Option<Box<dyn SourceDriver>>,
    manifest: SourceManifest,
    fanouts: Option<BTreeMap<String, TypedEdgeFanout>>,
    session: Option<SourceSessionContext>,
}

impl PreparedSourceRuntime {
    fn prepare(
        factory: Arc<dyn SourceFactory>,
        configuration: &SourceConfiguration,
        branch_specs: &[SourceOutputBranchSpec],
        session: Option<SourceSessionContext>,
    ) -> Result<(Self, Vec<SourceOutputReceiver>), SourceRuntimeError> {
        factory
            .manifest()
            .validate()
            .map_err(SourceRuntimeError::InvalidManifest)?;
        factory
            .validate_config(configuration)
            .map_err(SourceRuntimeError::InvalidConfiguration)?;
        let mut driver = factory
            .create(configuration)
            .map_err(SourceRuntimeError::Driver)?;
        let manifest = factory.manifest().clone();
        let mut fanouts = BTreeMap::new();
        let mut receivers = Vec::new();
        for output in &manifest.outputs {
            let specifications: Vec<_> = branch_specs
                .iter()
                .filter(|branch| branch.output_port == output.name)
                .map(|branch| branch.branch)
                .collect();
            if specifications.is_empty() {
                continue;
            }
            let (fanout, output_receivers) =
                TypedEdgeFanout::new(&specifications).map_err(SourceRuntimeError::EdgeBuild)?;
            fanouts.insert(output.name.clone(), fanout);
            receivers.extend(
                output_receivers
                    .into_iter()
                    .map(|receiver| SourceOutputReceiver {
                        output_port: output.name.clone(),
                        receiver,
                    }),
            );
        }
        if fanouts.is_empty() {
            return Err(SourceRuntimeError::NoRoutedOutputs);
        }
        driver
            .prepare(&SourcePrepareContext {
                manifest: manifest.clone(),
                session: session.clone(),
            })
            .map_err(SourceRuntimeError::Driver)?;
        Ok((
            Self {
                driver: Some(driver),
                manifest,
                fanouts: Some(fanouts),
                session,
            },
            receivers,
        ))
    }

    pub fn start(mut self) -> Result<SourceRuntime, SourceRuntimeError> {
        let mut driver = self
            .driver
            .take()
            .ok_or(SourceRuntimeError::PreparedStateConsumed)?;
        let manifest = self.manifest.clone();
        let mut fanouts = self
            .fanouts
            .take()
            .ok_or(SourceRuntimeError::PreparedStateConsumed)?;
        let session = self.session.clone();
        let cancellation = SourceCancellation {
            cancelled: Arc::new(AtomicBool::new(false)),
        };
        let task_cancellation = cancellation.clone();
        let state = Arc::new(SourceRuntimeObservationState::default());
        let task_state = Arc::clone(&state);
        let join = std::thread::Builder::new()
            .name("pks-typed-source".to_owned())
            .spawn(move || {
                task_state.ready.store(true, Ordering::Release);
                let result = run_source_driver(
                    driver.as_mut(),
                    &manifest,
                    &mut fanouts,
                    &task_cancellation,
                    &task_state,
                    session.as_ref(),
                );
                if result.is_err() {
                    task_state.failure_total.fetch_add(1, Ordering::Relaxed);
                }
                if task_cancellation.is_cancelled() {
                    task_state
                        .cancellation_total
                        .fetch_add(1, Ordering::Relaxed);
                }
                let close_result = driver.close().map_err(SourceRuntimeError::Driver);
                task_state.joined.store(true, Ordering::Release);
                result.and(close_result)
            })
            .map_err(SourceRuntimeError::Spawn)?;
        Ok(SourceRuntime {
            cancellation,
            observations: SourceRuntimeObservationHandle { state },
            join: Some(join),
        })
    }
}

impl Drop for PreparedSourceRuntime {
    fn drop(&mut self) {
        if let Some(driver) = self.driver.as_mut() {
            let _ = driver.close();
        }
    }
}

impl SourceRuntime {
    #[cfg(any(test, feature = "internal-testing"))]
    pub fn spawn(
        factory: Arc<dyn SourceFactory>,
        configuration: &SourceConfiguration,
        branch_specs: &[SourceOutputBranchSpec],
    ) -> Result<(Self, Vec<SourceOutputReceiver>), SourceRuntimeError> {
        let (prepared, receivers) =
            PreparedSourceRuntime::prepare(factory, configuration, branch_specs, None)?;
        Ok((prepared.start()?, receivers))
    }

    pub fn cancel(&self) {
        self.cancellation.cancelled.store(true, Ordering::Release);
    }

    pub fn observations(&self) -> SourceRuntimeObservationHandle {
        self.observations.clone()
    }

    pub fn join(&mut self) -> Result<(), SourceRuntimeError> {
        self.join
            .take()
            .ok_or(SourceRuntimeError::AlreadyJoined)?
            .join()
            .map_err(|_| SourceRuntimeError::WorkerPanicked)??;
        Ok(())
    }
}

impl Drop for SourceRuntime {
    fn drop(&mut self) {
        self.cancel();
        if let Some(join) = self.join.take() {
            let _ = join.join();
        }
    }
}

fn run_source_driver(
    driver: &mut dyn SourceDriver,
    manifest: &SourceManifest,
    fanouts: &mut BTreeMap<String, TypedEdgeFanout>,
    cancellation: &SourceCancellation,
    observations: &SourceRuntimeObservationState,
    session: Option<&SourceSessionContext>,
) -> Result<(), SourceRuntimeError> {
    let mut continuity = BTreeMap::<String, SignalContinuityTracker>::new();
    while !cancellation.is_cancelled() {
        let Some(emission) = driver
            .next(cancellation)
            .map_err(SourceRuntimeError::Driver)?
        else {
            break;
        };
        let output = manifest
            .output_port(&emission.output_port)
            .ok_or_else(|| SourceRuntimeError::UnknownOutput(emission.output_port.clone()))?;
        if emission.envelope.spec.class != output.signal.class
            || emission.envelope.spec.schema != output.signal.schema
            || !output.media.supports_signal(&emission.envelope.spec)
        {
            return Err(SourceRuntimeError::OutputContractMismatch);
        }
        if let Some(session) = session {
            let identity = session
                .output(&emission.output_port)
                .ok_or_else(|| SourceRuntimeError::UnknownOutput(emission.output_port.clone()))?;
            let lineage = emission
                .envelope
                .lineage
                .ok_or(SourceRuntimeError::MissingSessionLineage)?;
            if lineage.session_id != session.session_id
                || lineage.source_id != session.source_id
                || lineage.stream_id != identity.stream_id
            {
                return Err(SourceRuntimeError::OutputIdentityMismatch);
            }
        }
        let continuity_observation = continuity
            .entry(emission.output_port.clone())
            .or_default()
            .observe(&emission.envelope)
            .map_err(SourceRuntimeError::Continuity)?;
        if continuity_observation.discontinuity_observed {
            observations
                .discontinuity_total
                .fetch_add(1, Ordering::Relaxed);
        }
        if continuity_observation.source_recovered {
            observations.recovery_total.fetch_add(1, Ordering::Relaxed);
        }
        if continuity_observation.policy_changed {
            observations
                .policy_change_total
                .fetch_add(1, Ordering::Relaxed);
        }
        let fanout = fanouts
            .get_mut(&emission.output_port)
            .ok_or_else(|| SourceRuntimeError::UnroutedOutput(emission.output_port.clone()))?;
        let report = fanout
            .publish(emission.envelope, emission.terminal)
            .map_err(SourceRuntimeError::Publish)?;
        observations
            .emitted_total
            .fetch_add(report.delivered_total, Ordering::Relaxed);
        observations
            .dropped_total
            .fetch_add(report.dropped_total, Ordering::Relaxed);
    }
    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum SourceManifestError {
    #[error("source type identifier cannot be empty")]
    EmptySourceTypeId,
    #[error("source revision and generation must be non-zero")]
    ZeroVersion,
    #[error("source manifest requires at least one output")]
    NoOutputs,
    #[error("source manifest contains a non-output port")]
    NonOutputPort,
    #[error("source output name cannot be empty")]
    EmptyOutputName,
    #[error("source output names must be unique")]
    DuplicateOutputName,
    #[error("source output SignalSpec is invalid")]
    InvalidSignal,
    #[error("source output SignalSpec and MediaCaps are incompatible")]
    SignalMediaMismatch,
    #[error("source safety contract is incompatible with its execution partition")]
    InvalidSafetyContract,
    #[error("in-process source drivers currently require the BlockingWorker partition")]
    UnsupportedExecutionPartition,
}

#[derive(Debug, thiserror::Error)]
pub enum SourceRegistrationError {
    #[error("invalid source manifest: {0}")]
    InvalidManifest(SourceManifestError),
    #[error("source type {0} is already registered")]
    DuplicateSourceType(SourceTypeId),
    #[error("source type {0} conflicts with an existing graph node type")]
    NodeTypeConflict(SourceTypeId),
}

pub(crate) fn source_node_definition(factory: Arc<dyn SourceFactory>) -> Arc<dyn NodeDefinition> {
    Arc::new(SourceNodeDefinition { factory })
}

struct SourceNodeDefinition {
    factory: Arc<dyn SourceFactory>,
}

impl NodeDefinition for SourceNodeDefinition {
    fn descriptor(&self) -> NodeDescriptor {
        let manifest = self.factory.manifest();
        NodeDescriptor {
            type_id: NodeTypeId::from(manifest.source_type_id.as_str()),
            display_name: "External source",
            inputs: Vec::new(),
            outputs: manifest.outputs.clone(),
            execution: manifest.execution,
            safety: manifest.safety,
            stateful: true,
        }
    }

    fn validate_config(&self, config: &NodeConfig) -> Result<(), ConfigError> {
        let mut source_configuration = SourceConfiguration::default();
        for (key, value) in config.iter() {
            source_configuration.insert(key, value);
        }
        self.factory.validate_config(&source_configuration)
    }
}

impl std::fmt::Display for SourceTypeId {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SourceDriverError {
    #[error("source driver failed: {0}")]
    Failed(String),
}

#[derive(Debug, thiserror::Error)]
pub enum SourceRuntimeError {
    #[error("invalid source manifest: {0}")]
    InvalidManifest(SourceManifestError),
    #[error("invalid source configuration: {0}")]
    InvalidConfiguration(ConfigError),
    #[error("source driver failure: {0}")]
    Driver(SourceDriverError),
    #[error("typed edge build failed: {0}")]
    EdgeBuild(TypedEdgeBuildError),
    #[error("source runtime requires at least one routed output")]
    NoRoutedOutputs,
    #[error("source emitted unknown output {0}")]
    UnknownOutput(String),
    #[error("source emitted unrouted output {0}")]
    UnroutedOutput(String),
    #[error("source output does not match its manifest contract")]
    OutputContractMismatch,
    #[error("Session-owned source output is missing signal lineage")]
    MissingSessionLineage,
    #[error("source output identity does not match the Session prepare context")]
    OutputIdentityMismatch,
    #[error("source continuity validation failed: {0}")]
    Continuity(crate::graph::SignalContinuityError),
    #[error("typed source publish failed: {0}")]
    Publish(TypedEdgePublishError),
    #[error("source worker could not spawn: {0}")]
    Spawn(std::io::Error),
    #[error("source worker panicked")]
    WorkerPanicked,
    #[error("source worker has already been joined")]
    AlreadyJoined,
    #[error("prepared source state has already been consumed")]
    PreparedStateConsumed,
    #[error("source type {0} is not registered")]
    UnregisteredSource(SourceTypeId),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::{BinaryFormat, MediaCaps, Multiplicity, SignalSpec};

    fn output(name: &str) -> PortSpec {
        PortSpec {
            name: name.to_owned(),
            direction: PortDirection::Output,
            signal: SignalSpec::custom("dev.pocketstation.test.v1").with_schema("urn:test:v1"),
            media: MediaCaps::Binary(BinaryFormat::Raw),
            multiplicity: Multiplicity::Many,
            required: true,
        }
    }

    fn manifest(outputs: Vec<PortSpec>) -> SourceManifest {
        SourceManifest {
            source_type_id: SourceTypeId::new("dev.pocketstation.source.test.v1").unwrap(),
            revision: 1,
            generation: 1,
            outputs,
            execution: ExecutionPartition::BlockingWorker,
            safety: SafetyContract::AllocationAllowed,
        }
    }

    #[test]
    fn given_schema_backed_output_when_manifest_validated_then_contract_is_open() {
        assert_eq!(manifest(vec![output("out")]).validate(), Ok(()));
    }

    #[test]
    fn given_duplicate_output_names_when_manifest_validated_then_rejected() {
        assert_eq!(
            manifest(vec![output("out"), output("out")]).validate(),
            Err(SourceManifestError::DuplicateOutputName)
        );
    }
}