systemless 0.88.0

High-Level Emulation for classic Macintosh applications
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use super::capture::{CaptureStore, PendingCapture, RetainedCapture};
use super::error::{DebugError, DebugResult};
use super::ids::{
    AddressSpaceId, ArtifactId, BreakpointId, CaptureId, ContextId, OperationId, ProviderId,
    SessionId, TypedAllocator,
};
use super::model::{
    CaptureBoundaryKind, DebugExecutionState, DebugNotification, ExecutionLocation,
    NotificationPayload, OperationOutcome, OperationState, OperationStatus, StopInfo, StopReason,
};
use super::request::{BreakpointInfo, BreakpointSpec, CaptureMode, CaptureRequest};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_SESSION: AtomicU64 = AtomicU64::new(1);

pub const NOTIFICATION_RETENTION: usize = 1024;
pub const OPERATION_RETENTION: usize = 1024;

#[derive(Clone, Debug)]
struct PendingStep {
    operation: OperationId,
    context: ContextId,
    units_remaining: u32,
}

#[derive(Debug)]
pub struct DebuggerCoordinator {
    session: SessionId,
    generation: u64,
    revision: u64,
    execution_state: DebugExecutionState,
    next_stop_id: u64,
    next_breakpoint_id: TypedAllocator<BreakpointId>,
    next_operation_id: TypedAllocator<OperationId>,
    breakpoints: BTreeMap<BreakpointId, BreakpointInfo>,
    // Stops happen before execution, so resume ignores that breakpoint until
    // one unit in its context retires.
    suppressed_breakpoints: BTreeSet<BreakpointId>,
    pending_step: Option<PendingStep>,
    operations: BTreeMap<OperationId, OperationState>,
    operation_order: VecDeque<OperationId>,
    expired_operation_through: u64,
    providers: Vec<super::provider::ProviderRegistration>,
    adapter_registrations: Vec<super::adapters::AdapterRegistration>,
    capture_store: CaptureStore,
    pending_captures: Vec<PendingCapture>,
    notifications: VecDeque<DebugNotification>,
    next_notification_sequence: u64,
    terminal_announced: bool,
    pending_ppc_fault: Option<super::model::PpcGuestFault>,
    observed_context: Option<ContextId>,
}

impl DebuggerCoordinator {
    pub fn fresh() -> Self {
        let session = SessionId(NEXT_SESSION.fetch_add(1, Ordering::Relaxed));
        let mut coordinator = Self::new(session);
        coordinator.generation = session.0;
        coordinator
    }

    pub fn new(session: SessionId) -> Self {
        Self {
            session,
            generation: 1,
            revision: 0,
            execution_state: DebugExecutionState::Running,
            next_stop_id: 1,
            next_breakpoint_id: TypedAllocator::new(),
            next_operation_id: TypedAllocator::new(),
            breakpoints: BTreeMap::new(),
            suppressed_breakpoints: BTreeSet::new(),
            pending_step: None,
            operations: BTreeMap::new(),
            operation_order: VecDeque::new(),
            expired_operation_through: 0,
            providers: super::provider::builtin_providers().to_vec(),
            adapter_registrations: super::adapters::builtin_adapter_registrations(),
            capture_store: CaptureStore::new(),
            pending_captures: Vec::new(),
            notifications: VecDeque::new(),
            next_notification_sequence: 1,
            terminal_announced: false,
            pending_ppc_fault: None,
            observed_context: None,
        }
    }

    pub fn session(&self) -> SessionId {
        self.session
    }

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

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

    pub fn execution_state(&self) -> &DebugExecutionState {
        &self.execution_state
    }

    pub(crate) fn advance_generation(&mut self) {
        self.generation = self.generation.saturating_add(1);
        self.revision = self.revision.saturating_add(1);
        self.execution_state = DebugExecutionState::Running;
        self.breakpoints.clear();
        self.suppressed_breakpoints.clear();
        self.cancel_pending_step(OperationOutcome::Unavailable {
            reason: "runner generation changed".into(),
        });
        self.fail_pending_captures("runner generation changed");
        self.operations.clear();
        self.operation_order.clear();
        self.expired_operation_through = 0;
        self.terminal_announced = false;
        self.pending_ppc_fault = None;
        self.observed_context = None;
        self.push_notification(NotificationPayload::Invalidated {
            session: self.session,
        });
    }

    pub(crate) fn touch(&mut self) -> u64 {
        self.revision = self.revision.saturating_add(1);
        self.revision
    }

    fn stop(
        &mut self,
        reason: StopReason,
        context: Option<ContextId>,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> StopInfo {
        self.revision = self.revision.saturating_add(1);
        let stop = StopInfo {
            id: self.next_stop_id,
            revision: self.revision,
            context,
            location,
            reason,
            guest_tick,
            execution_units,
        };
        self.next_stop_id = self.next_stop_id.saturating_add(1);
        self.execution_state = DebugExecutionState::Paused { stop: stop.clone() };
        self.push_notification(NotificationPayload::ExecutionStateChanged {
            state: self.execution_state.clone(),
        });
        stop
    }

    pub fn pause(
        &mut self,
        context: Option<ContextId>,
        location: Option<ExecutionLocation>,
    ) -> StopInfo {
        self.pause_at(context, location, None, None)
    }

    pub fn pause_at(
        &mut self,
        context: Option<ContextId>,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> StopInfo {
        self.cancel_pending_step(OperationOutcome::Cancelled);
        if let DebugExecutionState::Paused { stop } = &self.execution_state {
            return stop.clone();
        }
        self.stop(
            StopReason::UserPause,
            context,
            location,
            guest_tick,
            execution_units,
        )
    }

    pub fn resume(&mut self) -> bool {
        if !self.execution_state.is_paused() {
            return false;
        }
        if let DebugExecutionState::Paused { stop } = &self.execution_state {
            if let StopReason::PcBreakpoint { id } = stop.reason {
                if self.breakpoints.contains_key(&id) {
                    self.suppressed_breakpoints.insert(id);
                }
            }
        }
        self.cancel_pending_step(OperationOutcome::Cancelled);
        self.execution_state = DebugExecutionState::Running;
        self.push_notification(NotificationPayload::ExecutionStateChanged {
            state: self.execution_state.clone(),
        });
        true
    }

    pub fn is_paused(&self) -> bool {
        self.execution_state.is_paused()
    }

    pub fn is_stepping(&self) -> bool {
        self.pending_step.is_some()
    }

    pub(crate) fn begin_step(&mut self, context: ContextId) -> OperationId {
        self.cancel_pending_step(OperationOutcome::Cancelled);
        if let DebugExecutionState::Paused { stop } = &self.execution_state {
            if let StopReason::PcBreakpoint { id } = stop.reason {
                if self.breakpoints.contains_key(&id) {
                    self.suppressed_breakpoints.insert(id);
                }
            }
        }
        let operation = self.next_operation_id.alloc();
        self.operations.insert(operation, OperationState::Pending);
        self.operation_order.push_back(operation);
        self.pending_step = Some(PendingStep {
            operation,
            context,
            units_remaining: 1,
        });
        self.execution_state = DebugExecutionState::Stepping {
            operation_id: operation,
        };
        self.touch();
        self.push_notification(NotificationPayload::ExecutionStateChanged {
            state: self.execution_state.clone(),
        });
        operation
    }

    pub fn step_target_context(&self) -> Option<ContextId> {
        self.pending_step.as_ref().map(|step| step.context)
    }

    pub fn step_units_remaining(&self) -> Option<u32> {
        self.pending_step.as_ref().map(|step| step.units_remaining)
    }

    pub(crate) fn breakpoint_rearming(&self) -> bool {
        !self.suppressed_breakpoints.is_empty()
    }

    pub(crate) fn note_executed_units(&mut self, context: ContextId, units: u32) {
        if units == 0 {
            return;
        }
        self.suppressed_breakpoints.retain(|id| {
            self.breakpoints
                .get(id)
                .is_some_and(|breakpoint| breakpoint.spec.context != Some(context))
        });
        if let Some(step) = self.pending_step.as_mut() {
            if step.context == context {
                step.units_remaining = step.units_remaining.saturating_sub(units);
            }
        }
    }

    pub(crate) fn finish_step(
        &mut self,
        context: ContextId,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> Option<StopInfo> {
        let operation = self.pending_step.take()?.operation;
        let stop = self.stop(
            StopReason::StepCompleted,
            Some(context),
            location,
            guest_tick,
            execution_units,
        );
        self.complete_operation(
            operation,
            OperationOutcome::Completed {
                stop: Some(stop.clone()),
            },
        );
        Some(stop)
    }

    pub fn abort_step(&mut self, reason: impl Into<String>) {
        if let Some(step) = self.pending_step.take() {
            self.suppressed_breakpoints.clear();
            self.complete_operation(
                step.operation,
                OperationOutcome::Unavailable {
                    reason: reason.into(),
                },
            );
            self.execution_state = DebugExecutionState::Running;
            self.push_notification(NotificationPayload::ExecutionStateChanged {
                state: self.execution_state.clone(),
            });
        }
    }

    fn cancel_pending_step(&mut self, outcome: OperationOutcome) -> bool {
        let Some(step) = self.pending_step.take() else {
            return false;
        };
        self.suppressed_breakpoints.clear();
        self.complete_operation(step.operation, outcome);
        true
    }

    pub fn insert_breakpoint(&mut self, spec: BreakpointSpec) -> BreakpointInfo {
        let id = self.next_breakpoint_id.alloc();
        let info = BreakpointInfo {
            id,
            spec,
            hit_count: 0,
        };
        self.breakpoints.insert(id, info.clone());
        self.touch();
        info
    }

    pub fn remove_breakpoint(&mut self, id: BreakpointId) -> bool {
        let removed = self.breakpoints.remove(&id).is_some();
        self.suppressed_breakpoints.remove(&id);
        if removed {
            self.touch();
        }
        removed
    }

    pub fn set_breakpoint_enabled(
        &mut self,
        id: BreakpointId,
        enabled: bool,
    ) -> Option<BreakpointInfo> {
        let info = self.breakpoints.get_mut(&id)?;
        info.spec.enabled = enabled;
        let cloned = info.clone();
        if !enabled {
            self.suppressed_breakpoints.remove(&id);
        }
        self.touch();
        Some(cloned)
    }

    pub fn breakpoints(&self) -> Vec<BreakpointInfo> {
        self.breakpoints.values().cloned().collect()
    }

    pub(crate) fn breakpoint_watch_addresses(
        &self,
        context: ContextId,
        space: super::ids::AddressSpaceId,
    ) -> Vec<u64> {
        let mut addresses = Vec::new();
        for (id, info) in &self.breakpoints {
            if !info.spec.enabled
                || info.spec.context != Some(context)
                || info.spec.address.space != space
                || self.suppressed_breakpoints.contains(id)
            {
                continue;
            }
            addresses.push(info.spec.address.offset);
        }
        addresses
    }

    pub(crate) fn breakpoint_at(
        &self,
        context: ContextId,
        space: super::ids::AddressSpaceId,
        address: u64,
    ) -> Option<BreakpointId> {
        self.breakpoints.iter().find_map(|(id, info)| {
            (info.spec.enabled
                && !self.suppressed_breakpoints.contains(id)
                && info.spec.context == Some(context)
                && info.spec.address.space == space
                && info.spec.address.offset == address)
                .then_some(*id)
        })
    }

    fn note_breakpoint_hit(&mut self, id: BreakpointId) -> bool {
        let Some(info) = self.breakpoints.get_mut(&id) else {
            return false;
        };
        info.hit_count = info.hit_count.saturating_add(1);
        info.spec.one_shot
    }

    pub(crate) fn breakpoint_stop(
        &mut self,
        id: BreakpointId,
        context: ContextId,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> StopInfo {
        let operation = self.pending_step.take().map(|step| step.operation);
        let remove = self.note_breakpoint_hit(id);
        if remove {
            self.breakpoints.remove(&id);
            self.suppressed_breakpoints.remove(&id);
        }
        let stop = self.stop(
            StopReason::PcBreakpoint { id },
            Some(context),
            location,
            guest_tick,
            execution_units,
        );
        if let Some(operation) = operation {
            self.complete_operation(
                operation,
                OperationOutcome::Completed {
                    stop: Some(stop.clone()),
                },
            );
        }
        stop
    }

    pub(crate) fn note_active_context(&mut self, context: Option<ContextId>) {
        if self.observed_context != context {
            self.observed_context = context;
            self.push_notification(NotificationPayload::ContextChanged { active: context });
        }
    }

    pub(crate) fn note_terminal_ppc_fault(&mut self, fault: super::model::PpcGuestFault) {
        if !self.terminal_announced {
            self.pending_ppc_fault = Some(fault);
        }
    }

    pub(crate) fn stop_at_terminal_halt(
        &mut self,
        context: Option<ContextId>,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> Option<StopInfo> {
        if self.terminal_announced {
            return None;
        }
        if let Some(step) = self.pending_step.take() {
            self.suppressed_breakpoints.clear();
            self.complete_operation(
                step.operation,
                OperationOutcome::Unavailable {
                    reason: "runner reached a terminal halt".into(),
                },
            );
        }
        let reason = self
            .pending_ppc_fault
            .take()
            .map_or(StopReason::TerminalHalt, |fault| StopReason::GuestFault { fault });
        let stop = self.stop(
            reason,
            context,
            location,
            guest_tick,
            execution_units,
        );
        self.terminal_announced = true;
        self.push_notification(NotificationPayload::TerminalState { halted: true });
        Some(stop)
    }

    fn create_operation(&mut self) -> OperationId {
        let id = self.next_operation_id.alloc();
        self.operations.insert(id, OperationState::Pending);
        self.operation_order.push_back(id);
        id
    }

    pub(crate) fn complete_operation(&mut self, id: OperationId, result: OperationOutcome) {
        if let Some(state) = self.operations.get_mut(&id) {
            let cloned = result.clone();
            *state = OperationState::Completed { result };
            self.push_notification(NotificationPayload::OperationCompleted {
                operation_id: id,
                result: Box::new(cloned),
            });
            self.prune_operations();
        }
    }

    fn prune_operations(&mut self) {
        while self
            .operations
            .values()
            .filter(|state| matches!(state, OperationState::Completed { .. }))
            .count()
            > OPERATION_RETENTION
        {
            let Some(index) = self.operation_order.iter().position(|id| {
                matches!(
                    self.operations.get(id),
                    Some(OperationState::Completed { .. })
                )
            }) else {
                break;
            };
            let id = self
                .operation_order
                .remove(index)
                .expect("located operation exists");
            self.operations.remove(&id);
            self.expired_operation_through = self.expired_operation_through.max(id.0);
        }
    }

    pub fn operation_status(&self, id: OperationId) -> Option<OperationStatus> {
        self.operations
            .get(&id)
            .cloned()
            .map(|state| OperationStatus { id, state })
            .or_else(|| {
                (id.0 <= self.expired_operation_through).then_some(OperationStatus {
                    id,
                    state: OperationState::Expired,
                })
            })
    }

    pub fn providers(&self) -> &[super::provider::ProviderRegistration] {
        &self.providers
    }

    pub fn provider(&self, id: ProviderId) -> Option<super::provider::ProviderRegistration> {
        self.providers
            .iter()
            .find(|provider| provider.descriptor().id == id)
            .cloned()
    }

    pub fn register_provider<P>(&mut self, provider: P) -> DebugResult<()>
    where
        P: super::provider::InspectionProvider + 'static,
    {
        self.register_provider_arc(std::sync::Arc::new(provider))
    }

    pub fn register_provider_arc(
        &mut self,
        provider: super::provider::ProviderRegistration,
    ) -> DebugResult<()> {
        super::provider::validate_provider_contract(provider.as_ref())?;
        let id = provider.descriptor().id;
        if self.provider(id).is_some() {
            return Err(DebugError::InvalidValue {
                detail: format!("provider {id} is already registered"),
            });
        }
        self.providers.push(provider);
        Ok(())
    }

    pub fn adapter_registrations(&self) -> &[super::adapters::AdapterRegistration] {
        &self.adapter_registrations
    }

    pub fn register_adapter(
        &mut self,
        registration: super::adapters::AdapterRegistration,
    ) -> DebugResult<()> {
        if registration.context == ContextId::UNSPECIFIED
            || registration
                .address_spaces
                .iter()
                .any(|space| *space == AddressSpaceId::UNSPECIFIED)
            || registration
                .address_spaces
                .iter()
                .enumerate()
                .any(|(index, space)| registration.address_spaces[..index].contains(space))
        {
            return Err(DebugError::InvalidValue {
                detail: "adapter context and address-space IDs must be non-zero and unique within a registration".into(),
            });
        }
        if self.adapter_registrations.iter().any(|existing| {
            existing.context == registration.context
                || existing
                    .address_spaces
                    .iter()
                    .any(|space| registration.address_spaces.contains(space))
        }) {
            return Err(DebugError::InvalidValue {
                detail: "adapter context and address-space IDs must be unique".into(),
            });
        }
        self.adapter_registrations.push(registration);
        Ok(())
    }

    pub(crate) fn alloc_artifact(&mut self) -> u64 {
        self.capture_store.alloc_artifact()
    }

    fn alloc_capture(&mut self) -> CaptureId {
        self.capture_store.alloc_capture()
    }

    pub fn begin_capture(
        &mut self,
        request: CaptureRequest,
        boundary: CaptureBoundaryKind,
    ) -> PendingCapture {
        let pending = self.make_capture(request, boundary);
        self.pending_captures.push(pending.clone());
        pending
    }

    pub fn begin_immediate_capture(&mut self, request: CaptureRequest) -> PendingCapture {
        self.make_capture(request, CaptureBoundaryKind::CommandSafePoint)
    }

    fn make_capture(
        &mut self,
        request: CaptureRequest,
        boundary: CaptureBoundaryKind,
    ) -> PendingCapture {
        let operation = self.create_operation();
        let capture = self.alloc_capture();
        let pause = request.mode == CaptureMode::PauseAtBoundary;
        PendingCapture {
            operation,
            capture,
            request,
            boundary,
            pause,
        }
    }

    pub(crate) fn take_pending_captures(
        &mut self,
        boundary: CaptureBoundaryKind,
    ) -> Vec<PendingCapture> {
        let mut taken = Vec::new();
        let mut remaining = Vec::with_capacity(self.pending_captures.len());
        for pending in std::mem::take(&mut self.pending_captures) {
            if pending.boundary == boundary {
                taken.push(pending);
            } else {
                remaining.push(pending);
            }
        }
        self.pending_captures = remaining;
        taken
    }

    pub fn cancel_capture(&mut self, operation: OperationId) -> bool {
        let Some(index) = self
            .pending_captures
            .iter()
            .position(|pending| pending.operation == operation)
        else {
            return false;
        };
        self.pending_captures.remove(index);
        self.complete_operation(operation, OperationOutcome::Cancelled);
        true
    }

    pub(crate) fn pending_capture_count(&self) -> usize {
        self.pending_captures.len()
    }

    pub(crate) fn has_pending_capture_boundary(&self, boundary: CaptureBoundaryKind) -> bool {
        self.pending_captures
            .iter()
            .any(|capture| capture.boundary == boundary)
    }

    pub(crate) fn fail_pending_captures(&mut self, reason: impl Into<String>) {
        let reason = reason.into();
        for pending in std::mem::take(&mut self.pending_captures) {
            self.complete_operation(
                pending.operation,
                OperationOutcome::Unavailable {
                    reason: reason.clone(),
                },
            );
        }
    }

    pub(crate) fn stop_at_capture_boundary(
        &mut self,
        capture: CaptureId,
        context: Option<ContextId>,
        location: Option<ExecutionLocation>,
        guest_tick: Option<u32>,
        execution_units: Option<u64>,
    ) -> StopInfo {
        self.stop(
            StopReason::CaptureBoundary { id: capture },
            context,
            location,
            guest_tick,
            execution_units,
        )
    }

    pub fn insert_capture(&mut self, capture: RetainedCapture) -> Option<CaptureId> {
        let id = capture.manifest.id;
        let evicted = self.capture_store.insert(capture);
        self.push_notification(NotificationPayload::CaptureAvailable { id });
        evicted
    }

    pub fn extract_capture_store(&mut self) -> CaptureStore {
        self.fail_pending_captures("runner replacing capture store");
        self.capture_store.drain()
    }

    pub fn adopt_capture_store(&mut self, store: CaptureStore) {
        self.capture_store.adopt(store);
    }

    pub fn capture(&self, id: CaptureId) -> Option<&RetainedCapture> {
        self.capture_store.get(id)
    }

    pub fn capture_snapshot(
        &self,
        capture: CaptureId,
        provider: ProviderId,
    ) -> Option<super::provider::ProviderSnapshot> {
        self.capture_store.snapshot(capture, provider)
    }

    pub fn artifact(&self, id: ArtifactId) -> Option<(super::model::ArtifactDescriptor, Vec<u8>)> {
        self.capture_store.artifact(id)
    }

    pub fn release_capture(&mut self, id: CaptureId) -> bool {
        self.capture_store.release(id)
    }

    pub fn captures(&self) -> Vec<super::model::CaptureManifest> {
        self.capture_store.manifests()
    }

    pub fn set_capture_retention(&mut self, retention: usize) {
        self.capture_store.set_retention(retention);
    }

    pub fn push_notification(&mut self, payload: NotificationPayload) -> u64 {
        let sequence = self.next_notification_sequence;
        self.next_notification_sequence = self.next_notification_sequence.saturating_add(1);
        self.notifications
            .push_back(DebugNotification { sequence, payload });
        while self.notifications.len() > NOTIFICATION_RETENTION {
            self.notifications.pop_front();
        }
        sequence
    }

    pub fn notifications_since(&self, after: u64) -> Vec<DebugNotification> {
        self.notifications
            .iter()
            .filter(|notification| notification.sequence > after)
            .cloned()
            .collect()
    }

    pub fn notification_history_available_after(&self, after: u64) -> bool {
        self.notifications
            .front()
            .is_none_or(|first| first.sequence <= after.saturating_add(1))
    }

    pub fn last_notification_sequence(&self) -> Option<u64> {
        self.notifications
            .back()
            .map(|notification| notification.sequence)
    }
}

impl Default for DebuggerCoordinator {
    fn default() -> Self {
        Self::new(SessionId(1))
    }
}


#[cfg(test)]
#[path = "coordinator_tests.rs"]
mod tests;