pb-mapper-server 0.6.1

Relay server implementation for pb-mapper
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
//! Server-side administrator request execution.
//!
//! ```text
//! authenticated AdminRequest -> revalidated AuthContext -> auth actor / manager
//!                                                        -> AdminResponse
//! ```
//!
//! Credential lifecycle operations go to `AuthRuntime`; service and connection
//! requests go to the routing manager. Every audited operation keeps its primary
//! response when only audit emission fails.
//!
//! Reads and mutations differ in where the authorization point sits: see
//! [`query_inventory`] and [`apply_mutation`].

use std::time::Duration;

use tokio::net::TcpStream;
use tokio_util::sync::CancellationToken;

use super::error::Error;
use super::{ManagerTask, ManagerTaskSender, Result, compose_service_key, validate_service_name};
use pb_mapper_auth::{AuthContext, AuthFailure, AuthRuntime, KeyId};
use pb_mapper_core::checksum::{Credential, parse_credential};
use pb_mapper_core::conn_id::RemoteConnId;
use pb_mapper_protocol::MessageWriter;
use pb_mapper_protocol::command::{AdminRequest, AdminResponse, MessageSerializer, PbConnResponse};
use pb_mapper_protocol::secure::ServerHeaderSession;

pub async fn handle_admin_request(
    request: AdminRequest,
    authorization: AuthContext,
    auth: AuthRuntime,
    manager: ManagerTaskSender,
    conn_id: RemoteConnId,
    mut conn: TcpStream,
    session: ServerHeaderSession,
) -> Result<()> {
    let result = execute(request, &authorization, auth, manager).await;
    let response = match result {
        Ok(response) => PbConnResponse::Admin(response),
        Err(failure) => {
            tracing::warn!(
                event = "admin_operation_failed",
                auth_stage = "permission_or_state",
                conn_id = %conn_id,
                reason = %failure.code,
                retryable = failure.retryable,
                error = %failure.message,
                "administrator operation failed"
            );
            PbConnResponse::error(failure.code, failure.message, failure.retryable)
        }
    };
    let message = response.encode().map_err(|error| Error::AdminOperation {
        detail: format!("failed to encode response: {error}"),
    })?;
    let mut writer = session
        .response_writer(&mut conn)
        .map_err(|error| Error::AdminOperation {
            detail: format!("failed to create response writer: {error}"),
        })?;
    writer
        .write_msg(&message)
        .await
        .map_err(|error| Error::AdminOperation {
            detail: format!("failed to write response: {error}"),
        })
}

async fn execute(
    request: AdminRequest,
    authorization: &AuthContext,
    auth: AuthRuntime,
    manager: ManagerTaskSender,
) -> std::result::Result<AdminResponse, AuthFailure> {
    match request {
        AdminRequest::KeyIssue { ttl_seconds, label } => auth
            .issue(authorization, Duration::from_secs(ttl_seconds), label)
            .await
            .map(AdminResponse::KeyIssued),
        AdminRequest::KeyList { page, page_size } => {
            audit_action(
                &auth,
                authorization,
                "temporary_key_list",
                None,
                Some(format!("page={page},page_size={page_size}")),
            )
            .await;
            auth.list(authorization, page, page_size)
                .await
                .map(AdminResponse::KeyList)
        }
        AdminRequest::KeyShow { key_id } => auth
            .show(authorization, KeyId::from_u64(key_id), false)
            .await
            .map(AdminResponse::KeyShown),
        AdminRequest::KeyReveal { key_id } => auth
            .show(authorization, KeyId::from_u64(key_id), true)
            .await
            .map(AdminResponse::KeyShown),
        AdminRequest::KeyRenew {
            key_id,
            ttl_seconds,
        } => auth
            .renew(
                authorization,
                KeyId::from_u64(key_id),
                Duration::from_secs(ttl_seconds),
            )
            .await
            .map(AdminResponse::KeyRenewed),
        AdminRequest::KeyRevoke { key_id } => auth
            .revoke(authorization, KeyId::from_u64(key_id))
            .await
            .map(AdminResponse::KeyRevoked),
        AdminRequest::KeyGc => auth
            .gc(authorization)
            .await
            .map(|removed| AdminResponse::KeyGc { removed }),
        AdminRequest::AuthStatus => {
            audit_action(&auth, authorization, "auth_status", None, None).await;
            auth.status(authorization)
                .await
                .map(AdminResponse::AuthStatus)
        }
        AdminRequest::AuthStateReset { confirm } => {
            if !confirm {
                return Err(AuthFailure::new(
                    "confirmation_required",
                    "auth-state reset requires explicit confirmation",
                    false,
                ));
            }
            auth.reset(authorization).await?;
            Ok(AdminResponse::Ok {
                action: "auth_state_reset".to_string(),
            })
        }
        AdminRequest::RootKeyRotate { new_admin_key } => {
            let Credential::Admin(new_key) = parse_credential(new_admin_key.trim())
                .map_err(|error| AuthFailure::new("administrator_key_invalid", error, false))?
            else {
                return Err(AuthFailure::new(
                    "administrator_key_invalid",
                    "root rotation requires a 32-byte administrator key",
                    false,
                ));
            };
            auth.rotate_root(authorization, new_key).await?;
            Ok(AdminResponse::Ok {
                action: "administrator_key_rotated".to_string(),
            })
        }
        AdminRequest::LegacyProtocolSet { policy } => {
            auth.set_legacy_protocol(authorization, policy).await?;
            Ok(AdminResponse::Ok {
                action: "legacy_protocol_updated".to_string(),
            })
        }
        AdminRequest::ServiceList {
            key_id,
            page,
            page_size,
        } => {
            audit_action(
                &auth,
                authorization,
                "service_list",
                key_id.map(KeyId::from_u64),
                Some(format!("page={page},page_size={page_size}")),
            )
            .await;
            let (response_sender, receiver) = tokio::sync::oneshot::channel();
            let response = query_inventory(
                authorization,
                &manager,
                ManagerTask::AdminServiceList {
                    key_id,
                    page,
                    page_size,
                    response_sender,
                },
                receiver,
                "service",
            )
            .await?;
            Ok(AdminResponse::Services(response))
        }
        AdminRequest::ConnectionList {
            key_id,
            page,
            page_size,
        } => {
            audit_action(
                &auth,
                authorization,
                "connection_list",
                key_id.map(KeyId::from_u64),
                Some(format!("page={page},page_size={page_size}")),
            )
            .await;
            let (response_sender, receiver) = tokio::sync::oneshot::channel();
            let response = query_inventory(
                authorization,
                &manager,
                ManagerTask::AdminConnectionList {
                    key_id,
                    page,
                    page_size,
                    response_sender,
                },
                receiver,
                "connection",
            )
            .await?;
            Ok(AdminResponse::Connections(response))
        }
        AdminRequest::ConnectionRetire {
            key_id,
            service_name,
            conn_id,
        } => {
            // The name arrives from the wire, so it gets the same check a
            // registration's does. Without it a name carrying the NUL separator
            // would compose another namespace's routing key, retiring a service
            // the audit record does not name.
            validate_service_name(&service_name)?;
            let namespace = key_id.unwrap_or_default();
            let key = compose_service_key(namespace, &service_name);
            let (response_sender, receiver) = tokio::sync::oneshot::channel();
            let retired = apply_mutation(
                authorization,
                &manager,
                ManagerTask::AdminConnectionRetire {
                    key,
                    conn_id: conn_id.map(RemoteConnId::from),
                    response_sender,
                },
                receiver,
                "connection retirement",
            )
            .await?;
            // Audited after the fact, not before: the interesting record is what
            // was actually dropped. A `retired=0` entry says the operator named
            // something the relay no longer had. Reached unconditionally, since
            // `apply_mutation` does not re-check the lease — a change that
            // happened gets recorded even if the credential rotated meanwhile.
            audit_action(
                &auth,
                authorization,
                "connection_retire",
                key_id.map(KeyId::from_u64),
                Some(format!(
                    "service={service_name},conn_id={},retired={retired}",
                    conn_id.map_or_else(|| "all".to_string(), |id| id.to_string())
                )),
            )
            .await;
            Ok(AdminResponse::ConnectionsRetired { retired })
        }
    }
}

/// Dispatch an inventory read only while the administrator lease remains current.
///
/// Root rotation cancels the old lease. Racing both channel operations against that
/// cancellation prevents a request authenticated under the old root from waiting for or
/// returning relay inventory after the rotation has taken effect. The final revalidation
/// establishes the successful read's authorization point after the manager produced its page.
async fn query_inventory<T>(
    authorization: &AuthContext,
    manager: &ManagerTaskSender,
    task: ManagerTask,
    receiver: tokio::sync::oneshot::Receiver<T>,
    inventory: &'static str,
) -> std::result::Result<T, AuthFailure> {
    let cancellation = send_manager_task(authorization, manager, task).await?;
    let response = tokio::select! {
        biased;
        _ = cancellation.cancelled() => return Err(cancelled_authorization(authorization)),
        result = receiver => result.map_err(|_| manager_dropped(inventory))?,
    };
    authorization.ensure_active()?;
    Ok(response)
}

/// Run a manager task that changes relay state, and report what it did.
///
/// Differs from [`query_inventory`] in where the authorization point sits. A read
/// re-checks the lease afterwards, because a snapshot taken under a credential
/// that has since rotated should not be handed back. A mutation cannot: by the
/// time the manager answers, the change is already applied, so racing the
/// cancellation or re-checking the lease would report failure for a mutation that
/// happened — and skip auditing it. The authorization is therefore established
/// before the task is sent, and the outcome reported unconditionally after.
async fn apply_mutation<T>(
    authorization: &AuthContext,
    manager: &ManagerTaskSender,
    task: ManagerTask,
    receiver: tokio::sync::oneshot::Receiver<T>,
    mutation: &'static str,
) -> std::result::Result<T, AuthFailure> {
    let _cancellation = send_manager_task(authorization, manager, task).await?;
    receiver.await.map_err(|_| manager_dropped(mutation))
}

/// Authorize the caller, then hand the task to the manager.
///
/// Returns the administrator lease's cancellation token, since a caller that
/// still has waiting left to do needs it. The bounded Tokio mailbox guarantees
/// that a Pending send has not delivered anything. Once send completes, mutation
/// callers await the manager's outcome without racing credential cancellation.
async fn send_manager_task(
    authorization: &AuthContext,
    manager: &ManagerTaskSender,
    task: ManagerTask,
) -> std::result::Result<CancellationToken, AuthFailure> {
    let cancellation = authorization.admin_cancellation_token()?;
    tokio::select! {
        biased;
        _ = cancellation.cancelled() => return Err(cancelled_authorization(authorization)),
        result = manager.send(task) => result.map_err(|_| {
            AuthFailure::new(
                "server_state_unavailable",
                "relay connection manager is unavailable",
                true,
            )
        })?,
    }
    Ok(cancellation)
}

fn manager_dropped(what: &'static str) -> AuthFailure {
    AuthFailure::new(
        "server_state_unavailable",
        format!("relay connection manager dropped the {what} query"),
        true,
    )
}

fn cancelled_authorization(authorization: &AuthContext) -> AuthFailure {
    match authorization.ensure_active() {
        Err(error) => error,
        Ok(_) => AuthFailure::new(
            "administrator_key_rotated",
            "administrator credential lease was cancelled during the inventory query",
            false,
        ),
    }
}

/// Record an administrator action, and never fail the action if recording fails.
///
/// The audit itself needs a live administrator lease, so a root rotation between
/// the action and this call loses the record. For a mutation that record is the
/// only trace the change leaves, which is why the failure is logged rather than
/// swallowed: the log line is what an operator reconstructs the gap from.
async fn audit_action(
    auth: &AuthRuntime,
    authorization: &AuthContext,
    action: &str,
    key_id: Option<KeyId>,
    detail: Option<String>,
) {
    if let Err(error) = auth
        .audit_admin(authorization, action, key_id, detail)
        .await
    {
        tracing::warn!(
            event = "admin_audit_failed",
            auth_stage = "audit",
            action,
            reason = %error.code,
            error = %error.message,
            "administrator operation could not be audited"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pb_mapper_auth::ADMIN_KEY_ID;
    use pb_mapper_auth::{AuthConfig, LegacyProtocolPolicy};

    fn temp_state_dir(name: &str) -> std::path::PathBuf {
        std::env::temp_dir().join(format!(
            "pb-mapper-admin-{name}-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|duration| duration.as_nanos())
                .unwrap_or_default()
        ))
    }

    async fn inventory_query_rejects_rotation(connection_query: bool) {
        let _process_credential_guard = pb_mapper_core::test_support::PROCESS_CREDENTIAL_TEST_LOCK
            .lock()
            .await;
        let state_dir = temp_state_dir(if connection_query {
            "connections"
        } else {
            "services"
        });
        let old_key = *b"0123456789abcdefghijklmnopqrstuv";
        let new_key = *b"abcdefghijklmnopqrstuvwxyz012345";
        let runtime = AuthRuntime::start(
            old_key,
            AuthConfig {
                state_dir: state_dir.clone(),
                max_temporary_keys: 4,
                max_temporary_key_ttl: Duration::from_secs(3600),
                legacy_protocol: LegacyProtocolPolicy::Allow,
            },
        )
        .await
        .expect("authentication runtime should start");
        let admin = runtime
            .authenticate_presented(ADMIN_KEY_ID, &old_key)
            .expect("old administrator key should authenticate");
        let request = if connection_query {
            AdminRequest::ConnectionList {
                key_id: None,
                page: 0,
                page_size: 100,
            }
        } else {
            AdminRequest::ServiceList {
                key_id: None,
                page: 0,
                page_size: 100,
            }
        };
        let (manager, mut receiver) = crate::manager::task_channel(1024);
        let request_admin = admin.clone();
        let request_runtime = runtime.clone();
        let pending = tokio::spawn(async move {
            execute(request, &request_admin, request_runtime, manager).await
        });

        let manager_task = receiver
            .recv()
            .await
            .expect("inventory request should reach the manager");
        runtime
            .rotate_root(&admin, new_key)
            .await
            .expect("root rotation should succeed");
        match manager_task {
            ManagerTask::AdminServiceList {
                response_sender, ..
            } => {
                let _ = response_sender.send(pb_mapper_protocol::command::AdminServicePage {
                    schema_version: 1,
                    items: Vec::new(),
                    next_page: None,
                });
            }
            ManagerTask::AdminConnectionList {
                response_sender, ..
            } => {
                let _ = response_sender.send(pb_mapper_protocol::command::AdminConnectionPage {
                    schema_version: 1,
                    items: Vec::new(),
                    next_page: None,
                });
            }
            _ => panic!("expected an administrator inventory manager task"),
        }

        let failure = pending
            .await
            .expect("inventory task should not panic")
            .expect_err("rotated administrator must not receive inventory");
        assert_eq!(failure.code, "administrator_key_rotated");

        drop(runtime);
        tokio::time::sleep(Duration::from_millis(20)).await;
        let _ = std::fs::remove_dir_all(state_dir);
    }

    #[tokio::test]
    async fn service_inventory_rejects_root_rotation_after_dispatch() {
        inventory_query_rejects_rotation(false).await;
    }

    #[tokio::test]
    async fn connection_inventory_rejects_root_rotation_after_dispatch() {
        inventory_query_rejects_rotation(true).await;
    }

    /// A retirement that has already happened is reported, not disowned.
    ///
    /// The mirror image of the two cases above: a read taken under a credential
    /// that has since rotated must be refused, but a mutation cannot be, because
    /// refusing it would tell the operator nothing was retired while leaving the
    /// connections gone and the action unaudited.
    #[tokio::test]
    async fn connection_retire_reports_a_mutation_that_outlived_its_credential() {
        let _process_credential_guard = pb_mapper_core::test_support::PROCESS_CREDENTIAL_TEST_LOCK
            .lock()
            .await;
        let state_dir = temp_state_dir("retire-rotation");
        let old_key = *b"0123456789abcdefghijklmnopqrstuv";
        let new_key = *b"abcdefghijklmnopqrstuvwxyz012345";
        let runtime = AuthRuntime::start(
            old_key,
            AuthConfig {
                state_dir: state_dir.clone(),
                max_temporary_keys: 4,
                max_temporary_key_ttl: Duration::from_secs(3600),
                legacy_protocol: LegacyProtocolPolicy::Allow,
            },
        )
        .await
        .expect("authentication runtime should start");
        let admin = runtime
            .authenticate_presented(ADMIN_KEY_ID, &old_key)
            .expect("old administrator key should authenticate");
        let (manager, mut receiver) = crate::manager::task_channel(1024);
        let request_admin = admin.clone();
        let request_runtime = runtime.clone();
        let pending = tokio::spawn(async move {
            execute(
                AdminRequest::ConnectionRetire {
                    key_id: None,
                    service_name: "echo".to_string(),
                    conn_id: None,
                },
                &request_admin,
                request_runtime,
                manager,
            )
            .await
        });

        let manager_task = receiver
            .recv()
            .await
            .expect("retire request should reach the manager");
        runtime
            .rotate_root(&admin, new_key)
            .await
            .expect("root rotation should succeed");
        let ManagerTask::AdminConnectionRetire {
            response_sender, ..
        } = manager_task
        else {
            panic!("expected an administrator retire manager task");
        };
        let _ = response_sender.send(3);

        let response = pending
            .await
            .expect("retire task should not panic")
            .expect("a retirement that happened must be reported");
        assert!(matches!(
            response,
            AdminResponse::ConnectionsRetired { retired: 3 }
        ));

        drop(runtime);
        tokio::time::sleep(Duration::from_millis(20)).await;
        let _ = std::fs::remove_dir_all(state_dir);
    }

    /// A retire name carrying the scoped-key separator never reaches the manager.
    ///
    /// `@{namespace:016x}\0{name}` is the routing key of a namespaced service, so
    /// a name spelling one out would retire a service in a namespace the request
    /// never named — and the audit record would name the unscoped one.
    #[tokio::test]
    async fn connection_retire_rejects_a_name_that_spells_out_a_scoped_key() {
        let _process_credential_guard = pb_mapper_core::test_support::PROCESS_CREDENTIAL_TEST_LOCK
            .lock()
            .await;
        let state_dir = temp_state_dir("retire-nul");
        let key = *b"0123456789abcdefghijklmnopqrstuv";
        let runtime = AuthRuntime::start(
            key,
            AuthConfig {
                state_dir: state_dir.clone(),
                max_temporary_keys: 4,
                max_temporary_key_ttl: Duration::from_secs(3600),
                legacy_protocol: LegacyProtocolPolicy::Allow,
            },
        )
        .await
        .expect("authentication runtime should start");
        let admin = runtime
            .authenticate_presented(ADMIN_KEY_ID, &key)
            .expect("administrator key should authenticate");
        let (manager, receiver) = crate::manager::task_channel(1024);

        let failure = execute(
            AdminRequest::ConnectionRetire {
                key_id: None,
                service_name: "@0000000000000001\u{0}echo".to_string(),
                conn_id: None,
            },
            &admin,
            runtime.clone(),
            manager.clone(),
        )
        .await
        .expect_err("a NUL-carrying service name must be refused");
        assert_eq!(failure.code, "service_name_invalid");
        assert!(
            receiver.is_empty(),
            "a refused retirement must not reach the manager"
        );

        drop(runtime);
        tokio::time::sleep(Duration::from_millis(20)).await;
        let _ = std::fs::remove_dir_all(state_dir);
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod cancellation_audit {
    use super::*;
    use std::{
        future::{Future, poll_fn},
        task::Poll,
    };
    #[tokio::test]
    async fn applied_mutation_must_not_be_reported_as_cancelled() {
        let config = pb_mapper_auth::AuthConfig {
            state_dir: std::env::temp_dir()
                .join(format!("pb-audit-admin-{}", rand::random::<u64>())),
            ..pb_mapper_auth::AuthConfig::default()
        };
        let auth = AuthRuntime::from_isolated_state(config).await.unwrap();
        let admin = auth
            .authenticate_presented(pb_mapper_auth::ADMIN_KEY_ID, &auth.admin_key().unwrap())
            .unwrap();
        let cancellation = admin.admin_cancellation_token().unwrap();
        let (sender, mut receiver) = crate::manager::task_channel(1);
        sender.send(ManagerTask::SweepServerLeases).await.unwrap();
        let (response, reply) = tokio::sync::oneshot::channel();
        let task = ManagerTask::AdminConnectionRetire {
            key: "audit".into(),
            conn_id: None,
            response_sender: response,
        };
        let mut mutation = Box::pin(apply_mutation(&admin, &sender, task, reply, "audit"));
        poll_fn(|cx| {
            assert!(mutation.as_mut().poll(cx).is_pending());
            Poll::Ready(())
        })
        .await;
        assert!(matches!(
            receiver.recv().await.unwrap(),
            ManagerTask::SweepServerLeases
        ));
        assert!(
            receiver.is_empty(),
            "Pending admission cannot already have applied the mutation"
        );
        poll_fn(|cx| {
            assert!(mutation.as_mut().poll(cx).is_pending());
            Poll::Ready(())
        })
        .await;
        match receiver.recv().await.unwrap() {
            ManagerTask::AdminConnectionRetire {
                response_sender, ..
            } => response_sender.send(1).unwrap(),
            _ => panic!("wrong manager task"),
        }
        cancellation.cancel();
        let result = mutation.await;
        auth.shutdown_actor().await;
        assert!(
            matches!(result, Ok(1)),
            "already-applied mutation reported {result:?}"
        );
    }
}