muse-code-sdk 1.4.20

Async Rust client for Muse Code over MSP
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
//! Recipes for the current Rust SDK API.

use std::future::{Future, pending};
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use muse_code_sdk::DuplexTransport;
use muse_code_sdk::approval::ApprovalDecisionInput;
use muse_code_sdk::client::{MuseClient, ResumeSessionOptions, StartSessionOptions};
use muse_code_sdk::errors::MuseError;
use muse_code_sdk::msp::{
    ApprovalDecision, ApprovalMode, ApprovalRequestParams, Item, ItemKind, ItemStatus,
    PlatformFamily, PlatformOs, SessionDurability, TurnInputPart, TurnInputPartType, TurnTerminal,
};
use muse_code_sdk::session::{ReconnectResolution, Reconnected, SendUserTurnOptions};
use muse_code_sdk::spawn::SpawnOptions;
use muse_code_sdk::turn::{Turn, TurnOutcome};
use serde_json::Value;

const MISMATCHED_FINGERPRINT: &str =
    "sha256:0000000000000000000000000000000000000000000000000000000000000000";

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    match std::env::args().nth(1).as_deref() {
        Some("approve-deny") => approve_deny().await?,
        Some("retry-safe") => retry_safe().await?,
        Some("cancel") => cancel_with_close().await?,
        Some("fingerprint-mismatch") => fingerprint_mismatch().await?,
        _ => stream_turn().await?,
    }
    Ok(())
}

async fn stream_turn() -> Result<(), Box<dyn std::error::Error>> {
    let connection = MuseClient::spawn(SpawnOptions::default()).await?;
    let client = connection.client;
    report_fingerprint(connection.fingerprint_warning.as_ref());

    let session = client.start_session(StartSessionOptions::default()).await?;
    let turn = session
        .send_user_turn(user_turn("Stream the answer as item revisions arrive."))
        .await?;

    let terminal = drain_turn(&turn).await?;
    print_terminal(&terminal);
    client.close().await;
    Ok(())
}

async fn approve_deny() -> Result<(), Box<dyn std::error::Error>> {
    let workspace_root = std::env::current_dir()?.to_string_lossy().into_owned();
    let connection = MuseClient::spawn(SpawnOptions::default()).await?;
    let client = connection.client;
    let result = run_approve_deny(&client, workspace_root).await;
    client.close().await;
    result
}

async fn run_approve_deny(
    client: &MuseClient,
    workspace_root: String,
) -> Result<(), Box<dyn std::error::Error>> {
    let session = client
        .start_session(approve_session_options(workspace_root.clone()))
        .await?;
    let approval_seen = Arc::new(AtomicBool::new(false));
    let approval_seen_by_handler = Arc::clone(&approval_seen);
    session.on_approval(move |request| {
        approval_seen_by_handler.store(true, Ordering::Release);
        async move {
            choose(
                &request,
                ApprovalDecision::ApprovedForSession,
                Some("approved by recipe".to_owned()),
            )
        }
    });
    let turn = session
        .send_user_turn(user_turn(
            "Use the shell to print the current directory, then summarize it.",
        ))
        .await?;
    let terminal = drain_turn(&turn).await?;
    print_terminal(&terminal);
    match check_approved_turn(&terminal, approval_seen.load(Ordering::Acquire)) {
        Ok(()) => run_default_deny_turn(client, workspace_root).await,
        Err(error) => Err(Box::new(error) as Box<dyn std::error::Error>),
    }
}

async fn run_default_deny_turn(
    client: &MuseClient,
    workspace_root: String,
) -> Result<(), Box<dyn std::error::Error>> {
    let session = client
        .start_session(default_deny_session_options(workspace_root))
        .await?;
    // No handler is registered; denyUnmatched makes the server reject unmatched requests.
    let turn = session
        .send_user_turn(user_turn(
            "Try to use the shell; this session leaves approval to server default-deny.",
        ))
        .await?;
    let (terminal, items) = drain_turn_items(&turn).await?;
    print_terminal(&terminal);
    check_default_deny_turn(&terminal, &items)?;
    Ok(())
}

fn approval_session_options(
    workspace_root: String,
    approval_mode: ApprovalMode,
) -> StartSessionOptions {
    StartSessionOptions {
        workspace_root: Some(workspace_root),
        approval_mode: Some(approval_mode),
        ..StartSessionOptions::default()
    }
}

fn approve_session_options(workspace_root: String) -> StartSessionOptions {
    approval_session_options(workspace_root, ApprovalMode::PromptUnmatched)
}

fn default_deny_session_options(workspace_root: String) -> StartSessionOptions {
    approval_session_options(workspace_root, ApprovalMode::DenyUnmatched)
}

fn check_approved_turn(terminal: &TurnOutcome, approval_seen: bool) -> io::Result<()> {
    // Check the turn first so a dead turn is not blamed on a missing workspace.
    require_completed_turn("approved", terminal)
        .and_then(|()| require_approval_request(approval_seen))
}

fn check_default_deny_turn(terminal: &TurnOutcome, items: &[Item]) -> io::Result<()> {
    // A denied tool call reaches the model as a typed denial, so the turn still
    // completes. Check the turn first so a dead turn is not blamed on the model.
    require_completed_turn("default-deny", terminal)
        .and_then(|()| require_rejected_tool_call(items))
}

fn require_rejected_tool_call(items: &[Item]) -> io::Result<()> {
    let rejected = items.iter().any(|item| {
        matches!(item.kind, ItemKind::ToolCall) && matches!(item.status, ItemStatus::Rejected)
    });
    if rejected {
        Ok(())
    } else {
        Err(io::Error::other(
            "approve-deny expected the default-deny turn to reject a tool call, but none settled rejected; with no tool call the model never tried the shell, and a completed one means the host ran it",
        ))
    }
}

fn turn_completed(outcome: &TurnOutcome) -> bool {
    matches!(
        outcome,
        TurnOutcome::Completed { params, .. } if params.terminal == TurnTerminal::Completed
    )
}

fn require_completed_turn(label: &str, outcome: &TurnOutcome) -> io::Result<()> {
    if turn_completed(outcome) {
        Ok(())
    } else {
        Err(io::Error::other(format!(
            "approve-deny expected the {label} turn to complete; see the terminal printed above"
        )))
    }
}

fn require_approval_request(approval_seen: bool) -> io::Result<()> {
    if approval_seen {
        Ok(())
    } else {
        Err(io::Error::other(
            "approve-deny expected an approval request, but the host sent none; verify that the session workspace exposes shell tools",
        ))
    }
}

async fn retry_safe() -> Result<(), Box<dyn std::error::Error>> {
    let connection = MuseClient::spawn(SpawnOptions::default()).await?;
    let client = connection.client;
    let session = client.start_session(StartSessionOptions::default()).await?;
    // Keep the handle: dropping it would discard the command locally, and a
    // discarded command is not carried across the reconnect.
    let first_attempt = session
        .send_user_turn(user_turn(
            "Start idempotent work; this spawned client will close before reconnecting.",
        ))
        .await?;
    println!(
        "submitted turn {}; reconnecting by session id",
        first_attempt.turn_id()
    );
    // Closing a spawned client also stops the host it launched; resume only
    // reattaches to whatever session state the next host can serve.
    client.close().await;
    let carried = session.pending();
    let session_id = session.session_id().to_owned();

    let reconnected = MuseClient::spawn(SpawnOptions::default()).await?;
    let resumed = reconnected
        .client
        .resume_session(ResumeSessionOptions {
            session_id,
            ..ResumeSessionOptions::default()
        })
        .await?;
    // The original command ids make this a retry: a written intake the new
    // host can still read replays its first decision instead of running the
    // work a second time. A host that cannot read it takes the resend as new
    // and may run the work again.
    let result = retry_result(resumed.resolve_reconnect(carried).await).await;
    drop(first_attempt);
    reconnected.client.close().await;
    result
}

/// The recipe's result: the reconnect's own error, or its first failed retry.
async fn retry_result(
    reconnect: Result<Vec<ReconnectResolution>, MuseError>,
) -> Result<(), Box<dyn std::error::Error>> {
    match reconnect {
        Ok(resolutions) => match settle_retries(resolutions).await.into_iter().next() {
            Some(failure) => Err(Box::new(failure)),
            None => Ok(()),
        },
        Err(error) => Err(Box::new(error)),
    }
}

/// Handles each entry on its own: a refusal does not stop the later entries,
/// and every live turn stays held until it settles. Returns every failed
/// retry, in order.
async fn settle_retries(resolutions: Vec<ReconnectResolution>) -> Vec<io::Error> {
    let mut failures = Vec::new();
    for resolution in resolutions {
        let failure = match resolution.result {
            Ok(Reconnected::Live(turn)) => {
                live_retry_failure(turn.turn_id(), drain_turn(&turn).await)
            }
            // The resend named the turn the resume restated as ended, usually
            // ended by this recipe's own close. A host that could read the
            // first admission replayed it; one that could not may have run the
            // work again.
            Ok(Reconnected::Ended { last_turn, .. }) => {
                print_last_turn_terminal(
                    &last_turn.turn_id,
                    &last_turn.terminal,
                    last_turn.reason.as_deref(),
                    last_turn.error.as_ref().map(|error| error.message.as_str()),
                );
                None
            }
            Err(error) => Some(io::Error::other(format!(
                "retry-safe: resending {} failed: {error}",
                resolution.command_id
            ))),
            // `Reconnected` is open: a later release may add a resolution.
            Ok(_) => {
                println!("resent {}", resolution.command_id);
                None
            }
        };
        if let Some(failure) = failure {
            println!("{failure}");
            failures.push(failure);
        }
    }
    failures
}

/// Why a retried live turn failed, if it did: it did not settle, or it settled
/// without completing.
fn live_retry_failure(
    turn_id: &str,
    drained: Result<TurnOutcome, Box<dyn std::error::Error>>,
) -> Option<io::Error> {
    match drained {
        Ok(terminal) => {
            print_terminal(&terminal);
            (!turn_completed(&terminal)).then(|| {
                io::Error::other(format!(
                    "retry-safe: retried turn {turn_id} did not complete; see the terminal printed above"
                ))
            })
        }
        Err(error) => Some(io::Error::other(format!(
            "retry-safe: turn {turn_id} did not settle: {error}"
        ))),
    }
}

async fn cancel_with_close() -> Result<(), Box<dyn std::error::Error>> {
    let connection = MuseClient::spawn(SpawnOptions::default()).await?;
    let client = connection.client;
    let session = client.start_session(StartSessionOptions::default()).await?;
    let turn = session
        .send_user_turn(user_turn(
            "Start a long-running task, then close the client.",
        ))
        .await?;

    println!("closing client while turn {} is pending", turn.turn_id());
    client.close().await;
    report_close_settlement(turn.completed().await)?;
    Ok(())
}

fn report_close_settlement(result: Result<TurnOutcome, MuseError>) -> Result<(), MuseError> {
    match result {
        Ok(outcome) => print_terminal(&outcome),
        Err(error) if is_expected_close_error(&error) => {
            println!("turn settled locally by close: {error}");
        }
        Err(error) => return Err(error),
    }
    Ok(())
}

fn is_expected_close_error(error: &MuseError) -> bool {
    matches!(error, MuseError::ClientClosed)
}

async fn fingerprint_mismatch() -> Result<(), Box<dyn std::error::Error>> {
    let transport = ScriptedAttachTransport::fingerprint_mismatch();
    let connection = MuseClient::attach(transport).await?;
    report_fingerprint(connection.fingerprint_warning.as_ref());
    connection.client.close().await;
    Ok(())
}

async fn drain_turn(turn: &Turn) -> Result<TurnOutcome, Box<dyn std::error::Error>> {
    Ok(drain_turn_items(turn).await?.0)
}

/// Drains the turn like `drain_turn` and also keeps every item revision it read.
async fn drain_turn_items(
    turn: &Turn,
) -> Result<(TurnOutcome, Vec<Item>), Box<dyn std::error::Error>> {
    let mut items = turn.items();
    let mut drained = Vec::new();
    while let Some(item) = items.recv().await? {
        print_item(&item);
        drained.push(item);
    }
    Ok((turn.completed().await?, drained))
}

fn choose(
    request: &ApprovalRequestParams,
    decision: ApprovalDecision,
    feedback: Option<String>,
) -> ApprovalDecisionInput {
    let requested_choice = request
        .available_choices
        .iter()
        .find(|choice| choice.decision == decision)
        .or_else(|| {
            (decision == ApprovalDecision::ApprovedForSession).then(|| {
                request
                    .available_choices
                    .iter()
                    .find(|choice| choice.decision == ApprovalDecision::Approved)
            })?
        });

    if let Some(choice) = requested_choice {
        return ApprovalDecisionInput {
            choice_id: choice.choice_id.clone(),
            feedback: feedback.filter(|_| choice.accepts_feedback),
        };
    }

    if let Some(choice) = request
        .available_choices
        .iter()
        .find(|choice| choice.decision == ApprovalDecision::Denied && choice.rule_preview.is_none())
        .or_else(|| {
            request
                .available_choices
                .iter()
                .find(|choice| choice.decision == ApprovalDecision::Abort)
        })
    {
        return ApprovalDecisionInput {
            choice_id: choice.choice_id.clone(),
            feedback: None,
        };
    };

    ApprovalDecisionInput {
        choice_id: "no_offered_choice".to_owned(),
        feedback: None,
    }
}

/// The model's answer text: a completed agent message. Reasoning,
/// reminder-child and other items carry text too, and `items()` also yields an
/// answer's in-progress revisions (empty text) and a retried attempt's failed
/// partial text; none of it is the reply, so the recipes do not print it.
fn answer_text(item: &Item) -> Option<&str> {
    match (&item.kind, &item.status) {
        (ItemKind::AgentMessage, ItemStatus::Completed) => item.text.as_deref(),
        _ => None,
    }
}

fn print_item(item: &Item) {
    if let Some(text) = answer_text(item) {
        println!("[{}] {text}", item.item_id);
    }
}

fn print_terminal(outcome: &TurnOutcome) {
    match outcome {
        TurnOutcome::Completed { params, .. } => match &params.terminal {
            TurnTerminal::Completed => println!("turn completed"),
            TurnTerminal::Cancelled => println!("turn cancelled"),
            TurnTerminal::Failed => {
                if let Some(error) = &params.error {
                    println!("turn failed: {}", error.message);
                } else if let Some(reason) = &params.reason {
                    println!("turn failed: {reason}");
                } else {
                    println!("turn failed");
                }
            }
            // An open enum: a newer host can send a terminal this SDK does not name.
            other => println!("turn ended: {}", other.as_str()),
        },
        TurnOutcome::Unqueued(params) => {
            println!("turn {} was unqueued before launch", params.turn_id);
        }
        // The SDK's outcome enums are open: a later release may add one.
        _ => println!("turn ended"),
    }
}

fn print_last_turn_terminal(
    turn_id: &str,
    terminal: &TurnTerminal,
    reason: Option<&str>,
    error_message: Option<&str>,
) {
    match terminal {
        TurnTerminal::Completed => println!("prior turn {turn_id} completed before reconnect"),
        TurnTerminal::Cancelled => println!("prior turn {turn_id} was cancelled before reconnect"),
        TurnTerminal::Failed => {
            if let Some(message) = error_message {
                println!("prior turn {turn_id} failed before reconnect: {message}");
            } else if let Some(reason) = reason {
                println!("prior turn {turn_id} failed before reconnect: {reason}");
            } else {
                println!("prior turn {turn_id} failed before reconnect");
            }
        }
        other => println!(
            "prior turn {turn_id} ended before reconnect: {}",
            other.as_str()
        ),
    }
}

fn report_fingerprint(warning: Option<&muse_code_sdk::fingerprint::FingerprintWarning>) {
    match warning {
        Some(warning) => println!(
            "continuing after schema warning: expected {}, host reported {}",
            warning.expected, warning.actual
        ),
        None => println!("schema fingerprint matches"),
    }
}

fn user_turn(prompt: impl Into<String>) -> SendUserTurnOptions {
    SendUserTurnOptions {
        input: vec![text_part(prompt)],
        ..SendUserTurnOptions::default()
    }
}

fn text_part(text: impl Into<String>) -> TurnInputPart {
    TurnInputPart {
        part_type: TurnInputPartType::Text,
        text: Some(text.into()),
        media_type: None,
        base64_data: None,
        width: None,
        height: None,
        selector: None,
        arguments: None,
        upload_id: None,
        client_ref: None,
        source: None,
    }
}

struct ScriptedAttachTransport {
    fingerprint: String,
    response: Option<String>,
    initialize_written: bool,
}

impl ScriptedAttachTransport {
    fn fingerprint_mismatch() -> Self {
        Self {
            fingerprint: MISMATCHED_FINGERPRINT.to_owned(),
            response: None,
            initialize_written: false,
        }
    }
}

impl DuplexTransport for ScriptedAttachTransport {
    fn read_chunk(
        &mut self,
    ) -> Pin<Box<dyn Future<Output = io::Result<Option<String>>> + Send + '_>> {
        if self.initialize_written
            && let Some(response) = self.response.take()
        {
            Box::pin(async move { Ok(Some(response)) })
        } else {
            Box::pin(pending::<io::Result<Option<String>>>())
        }
    }

    fn write(&mut self, frame: &str) -> Pin<Box<dyn Future<Output = io::Result<()>> + Send + '_>> {
        if let Some(id) = serde_json::from_str::<Value>(frame)
            .ok()
            .filter(|message| message.get("method").and_then(Value::as_str) == Some("initialize"))
            .and_then(|message| message.get("id").cloned())
        {
            self.response = Some(initialize_response(&self.fingerprint, id));
            self.initialize_written = true;
        }
        Box::pin(async { Ok(()) })
    }
}

fn initialize_response(fingerprint: &str, id: Value) -> String {
    // A fake host answers with wire JSON; received MSP structs are
    // `#[non_exhaustive]`, so a client never builds them by hand.
    let result = serde_json::json!({
        "serverInfo": {"name": "muse", "version": env!("CARGO_PKG_VERSION")},
        "userAgent": "muse/sdk-example",
        "museHome": "/tmp/muse-home",
        "platformFamily": PlatformFamily::Unix,
        "platformOs": PlatformOs::Linux,
        "schema": {"version": 1, "fingerprint": fingerprint},
        "grantedCapabilities": [],
        "experimentalApi": false,
        "sessionDurability": SessionDurability::Durable,
    });
    let response = serde_json::json!({
        "jsonrpc": "2.0",
        "id": id,
        "result": result,
    });
    format!("{response}\n")
}

#[cfg(test)]
mod tests {
    use std::task::{Context, Poll, Waker};

    use serde_json::json;

    use super::*;

    fn ready<F: Future>(future: F) -> Option<F::Output> {
        let waker = Waker::noop();
        let mut context = Context::from_waker(waker);
        let mut future = std::pin::pin!(future);
        match future.as_mut().poll(&mut context) {
            Poll::Ready(output) => Some(output),
            Poll::Pending => None,
        }
    }

    fn item(kind: &str, status: &str, text_member: &str, text: &str) -> Result<Item, String> {
        let mut value = json!({
            "itemId": "item-1",
            "kind": kind,
            "turnId": "turn-1",
            "revision": 1,
            "status": status,
        });
        value[text_member] = serde_json::Value::from(text);
        serde_json::from_value(value)
            .map_err(|error| format!("{kind} item did not decode: {error}"))
    }

    #[test]
    fn recipes_print_only_the_answer() -> Result<(), String> {
        let answer = item("agentMessage", "completed", "text", "Two files changed.")?;
        assert_eq!(answer_text(&answer), Some("Two files changed."));
        let reasoning = item(
            "reasoning",
            "completed",
            "text",
            "First I should list the files.",
        )?;
        assert_eq!(answer_text(&reasoning), None, "reasoning is not the answer");
        let reminder = item(
            "reminderChild",
            "completed",
            "fallbackText",
            "Reminder child session",
        )?;
        assert_eq!(
            answer_text(&reminder),
            None,
            "a reminder child is not the answer"
        );
        let opening = item("agentMessage", "inProgress", "text", "")?;
        assert_eq!(
            answer_text(&opening),
            None,
            "an answer still in progress is not printed"
        );
        let burned = item("agentMessage", "failed", "text", "The repository hol")?;
        assert_eq!(
            answer_text(&burned),
            None,
            "a failed attempt's partial text is not the answer"
        );
        Ok(())
    }

    fn approval_request(
        available_choices: serde_json::Value,
    ) -> Result<ApprovalRequestParams, String> {
        serde_json::from_value(json!({
            "sessionId": "session-1",
            "approvalId": "approval-1",
            "turnId": "turn-1",
            "taskId": "task-1",
            "itemId": "item-1",
            "toolCallId": "call-1",
            "toolName": "shell",
            "rawArgs": "{\"command\":\"pwd\"}",
            "viewCursor": "cursor-1",
            "sourceRange": {
                "stream": { "kind": "session", "id": "session-1" },
                "first": { "id": "event-1", "sequence": 1 },
                "last": { "id": "event-1", "sequence": 1 }
            },
            "subject": {
                "kind": "shell",
                "command": "pwd"
            },
            "currentRequirementId": {
                "approvalId": "approval-1",
                "sourceIndex": 0
            },
            "availableChoices": available_choices,
            "protectedWrite": false,
            "judgeEscalated": false
        }))
        .map_err(|error| format!("approval fixture did not decode: {error}"))
    }

    #[test]
    fn approve_recipe_uses_normal_approving_choice_without_unsupported_feedback()
    -> Result<(), String> {
        let request = approval_request(json!([
            {
                "choiceId": "allow_once",
                "label": "Allow once",
                "decision": "approved",
                "scope": "once"
            },
            {
                "choiceId": "deny",
                "label": "Deny",
                "decision": "denied",
                "scope": "once"
            }
        ]))?;

        let decision = choose(
            &request,
            ApprovalDecision::ApprovedForSession,
            Some("approved by recipe".to_owned()),
        );
        assert_eq!(decision.choice_id, "allow_once");
        assert_eq!(decision.feedback, None);
        Ok(())
    }

    #[test]
    fn approve_recipe_keeps_feedback_when_the_choice_accepts_it() -> Result<(), String> {
        let request = approval_request(json!([
            {
                "choiceId": "allow_session",
                "label": "Allow for session",
                "decision": "approvedForSession",
                "scope": "session",
                "acceptsFeedback": true
            },
            {
                "choiceId": "deny",
                "label": "Deny",
                "decision": "denied",
                "scope": "once"
            }
        ]))?;

        let decision = choose(
            &request,
            ApprovalDecision::ApprovedForSession,
            Some("approved by recipe".to_owned()),
        );
        assert_eq!(decision.choice_id, "allow_session");
        assert_eq!(decision.feedback.as_deref(), Some("approved by recipe"));
        Ok(())
    }

    #[test]
    fn approve_recipe_uses_real_abort_choice_when_no_approval_is_offered() -> Result<(), String> {
        let request = approval_request(json!([
            {
                "choiceId": "abort",
                "label": "Reject",
                "decision": "abort",
                "scope": "once",
                "acceptsFeedback": true
            }
        ]))?;

        let decision = choose(
            &request,
            ApprovalDecision::ApprovedForSession,
            Some("approved by recipe".to_owned()),
        );
        assert_eq!(decision.choice_id, "abort");
        assert_eq!(decision.feedback, None);
        Ok(())
    }

    #[test]
    fn approve_recipe_uses_server_denial_when_no_approval_is_offered() -> Result<(), String> {
        let request = approval_request(json!([
            {
                "choiceId": "abort",
                "label": "Reject",
                "decision": "abort",
                "scope": "once"
            },
            {
                "choiceId": "deny",
                "label": "Deny",
                "decision": "denied",
                "scope": "once",
                "acceptsFeedback": true
            }
        ]))?;

        let decision = choose(
            &request,
            ApprovalDecision::ApprovedForSession,
            Some("approved by recipe".to_owned()),
        );
        assert_eq!(decision.choice_id, "deny");
        assert_eq!(decision.feedback, None);
        Ok(())
    }

    #[test]
    fn approve_recipe_aborts_instead_of_persisting_a_deny_rule() -> Result<(), String> {
        let request = approval_request(json!([
            {
                "choiceId": "deny_computer_use_app",
                "label": "Always deny this app",
                "decision": "denied",
                "scope": "once",
                "rulePreview": "com.apple.Safari"
            },
            {
                "choiceId": "abort",
                "label": "Reject",
                "decision": "abort",
                "scope": "once"
            }
        ]))?;

        let decision = choose(
            &request,
            ApprovalDecision::ApprovedForSession,
            Some("approved by recipe".to_owned()),
        );
        assert_eq!(decision.choice_id, "abort");
        assert_eq!(decision.feedback, None);
        Ok(())
    }

    /// Hands every frame the client writes to the test and answers
    /// `initialize` like the scripted attach host, with the SDK's own
    /// fingerprint.
    struct RecordingTransport {
        attach: ScriptedAttachTransport,
        written: tokio::sync::mpsc::UnboundedSender<String>,
    }

    impl DuplexTransport for RecordingTransport {
        fn read_chunk(
            &mut self,
        ) -> Pin<Box<dyn Future<Output = io::Result<Option<String>>> + Send + '_>> {
            self.attach.read_chunk()
        }

        fn write(
            &mut self,
            frame: &str,
        ) -> Pin<Box<dyn Future<Output = io::Result<()>> + Send + '_>> {
            let _ = self.written.send(frame.to_owned());
            self.attach.write(frame)
        }
    }

    async fn session_start_params(
        frames: &mut tokio::sync::mpsc::UnboundedReceiver<String>,
    ) -> Result<muse_code_sdk::msp::SessionStartParams, String> {
        while let Some(frame) = frames.recv().await {
            let message: Value = serde_json::from_str(&frame)
                .map_err(|error| format!("client wrote a frame that is not json: {error}"))?;
            if message["method"] == "session/start" {
                return serde_json::from_value(message["params"].clone())
                    .map_err(|error| format!("session/start params did not decode: {error}"));
            }
        }
        Err("the client closed its transport before writing session/start".to_owned())
    }

    fn current_thread_runtime() -> Result<tokio::runtime::Runtime, String> {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|error| format!("failed to build the test runtime: {error}"))
    }

    /// Attaches a client over a `RecordingTransport` and returns it with the
    /// receiver of every frame it writes.
    async fn attach_recording()
    -> Result<(MuseClient, tokio::sync::mpsc::UnboundedReceiver<String>), String> {
        let (written, frames) = tokio::sync::mpsc::unbounded_channel();
        let transport = RecordingTransport {
            attach: ScriptedAttachTransport {
                fingerprint: muse_code_sdk::fingerprint::EXPECTED.to_owned(),
                response: None,
                initialize_written: false,
            },
            written,
        };
        let client = MuseClient::attach(transport)
            .await
            .map_err(|error| format!("attach failed: {error}"))?
            .client;
        Ok((client, frames))
    }

    #[test]
    fn default_deny_recipe_starts_its_session_with_deny_unmatched() -> Result<(), String> {
        current_thread_runtime()?.block_on(async {
            let (client, mut frames) = attach_recording().await?;
            // The scripted host never answers `session/start`, so the recipe
            // waits there; the frame it wrote is the evidence.
            let start = tokio::select! {
                result = run_default_deny_turn(&client, "/workspace/project".to_owned()) => {
                    return Err(format!(
                        "the recipe finished with no session/start answer: {result:?}"
                    ));
                }
                start = session_start_params(&mut frames) => start,
            };
            client.close().await;
            let start = start?;
            if start.approval_mode != Some(ApprovalMode::DenyUnmatched) {
                return Err(format!(
                    "the default-deny recipe started its session with {:?}, not denyUnmatched",
                    start.approval_mode
                ));
            }
            if start.workspace_root.as_deref() != Some("/workspace/project") {
                return Err(format!(
                    "the default-deny recipe started in {:?}",
                    start.workspace_root
                ));
            }
            Ok(())
        })
    }

    #[test]
    fn approve_recipe_starts_its_session_with_prompt_unmatched() -> Result<(), String> {
        current_thread_runtime()?.block_on(async {
            let (client, mut frames) = attach_recording().await?;
            // As above: the recipe waits on the unanswered `session/start`.
            let start = tokio::select! {
                result = run_approve_deny(&client, "/workspace/project".to_owned()) => {
                    return Err(format!(
                        "the recipe finished with no session/start answer: {result:?}"
                    ));
                }
                start = session_start_params(&mut frames) => start,
            };
            client.close().await;
            let start = start?;
            if start.approval_mode != Some(ApprovalMode::PromptUnmatched) {
                return Err(format!(
                    "the approve recipe started its session with {:?}, not promptUnmatched",
                    start.approval_mode
                ));
            }
            if start.workspace_root.as_deref() != Some("/workspace/project") {
                return Err(format!(
                    "the approve recipe started in {:?}",
                    start.workspace_root
                ));
            }
            Ok(())
        })
    }

    #[test]
    fn approve_recipe_fails_when_no_approval_request_arrives() {
        assert!(require_approval_request(false).is_err());
    }

    fn finished_turn(terminal: &str) -> Result<TurnOutcome, String> {
        let params = serde_json::from_value(json!({
            "sessionId": "session-1",
            "viewCursor": "cursor-1",
            "sourceRange": {
                "stream": {"kind": "run", "id": "turn-1"},
                "first": {"id": "event-1", "sequence": 1},
                "last": {"id": "event-1", "sequence": 1}
            },
            "turnId": "turn-1",
            "terminal": terminal
        }))
        .map_err(|error| format!("turn/completed fixture did not decode: {error}"))?;
        Ok(TurnOutcome::Completed {
            params,
            observed_start: true,
        })
    }

    #[test]
    fn approve_recipe_reports_a_dead_turn_before_the_workspace_hint() -> Result<(), String> {
        let error = match check_approved_turn(&finished_turn("failed")?, false) {
            Ok(()) => return Err("a failed turn with no approval passed the recipe".to_owned()),
            Err(error) => error.to_string(),
        };
        if !error.contains("expected the approved turn to complete") {
            return Err(format!(
                "a dead turn was not reported as an incomplete turn: {error}"
            ));
        }
        Ok(())
    }

    #[test]
    fn approve_deny_recipe_fails_when_a_turn_does_not_complete() -> Result<(), String> {
        require_completed_turn("approved", &finished_turn("completed")?)
            .map_err(|error| format!("a completed turn was refused: {error}"))?;
        for terminal in ["failed", "cancelled"] {
            if require_completed_turn("approved", &finished_turn(terminal)?).is_ok() {
                return Err(format!("a {terminal} approved turn passed the recipe"));
            }
        }
        let unqueued = TurnOutcome::Unqueued(
            serde_json::from_value(json!({
                "sessionId": "session-1",
                "viewCursor": "cursor-1",
                "sourceRange": {
                    "stream": {"kind": "session", "id": "session-1"},
                    "first": {"id": "event-1", "sequence": 1},
                    "last": {"id": "event-1", "sequence": 1}
                },
                "turnId": "turn-1",
                "commandId": "turn-1"
            }))
            .map_err(|error| format!("turn/unqueued fixture did not decode: {error}"))?,
        );
        if require_completed_turn("approved", &unqueued).is_ok() {
            return Err("an unqueued approved turn passed the recipe".to_owned());
        }
        Ok(())
    }

    fn ended_retry(terminal: &str) -> Result<ReconnectResolution, String> {
        let ack = serde_json::from_value(json!({
            "commandId": "command-1",
            "status": "accepted",
            "turnId": "turn-1",
            "startedNewTurn": true,
            "disposition": "started"
        }))
        .map_err(|error| format!("turn/start result fixture did not decode: {error}"))?;
        let last_turn = serde_json::from_value(json!({"turnId": "turn-1", "terminal": terminal}))
            .map_err(|error| format!("lastTurn fixture did not decode: {error}"))?;
        Ok(ReconnectResolution {
            command_id: "command-1".to_owned(),
            result: Ok(Reconnected::Ended { ack, last_turn }),
        })
    }

    fn refused_retry(command_id: &str) -> ReconnectResolution {
        ReconnectResolution {
            command_id: command_id.to_owned(),
            result: Err(MuseError::TransportEnded),
        }
    }

    fn settled(resolutions: Vec<ReconnectResolution>) -> Result<Vec<String>, String> {
        ready(settle_retries(resolutions))
            .map(|failures| failures.iter().map(ToString::to_string).collect())
            .ok_or_else(|| "settling entries with no live turn stayed pending".to_owned())
    }

    /// Every entry is handled, so a refusal does not stop the ones after it;
    /// an `Ended` entry names a turn the resume restated as ended, whatever its
    /// terminal, so it is not a failed retry.
    #[test]
    fn retry_safe_recipe_reports_each_refused_resend_and_passes_a_replayed_end()
    -> Result<(), String> {
        let failures = settled(vec![
            refused_retry("command-a"),
            ended_retry("cancelled")?,
            ended_retry("failed")?,
            ended_retry("completed")?,
            refused_retry("command-c"),
        ])?;
        let expected = ["resending command-a failed", "resending command-c failed"];
        if failures.len() != expected.len()
            || !failures
                .iter()
                .zip(expected)
                .all(|(failure, expected)| failure.contains(expected))
        {
            return Err(format!("unexpected failed retries: {failures:?}"));
        }
        let failures = settled(vec![ended_retry("cancelled")?])?;
        if !failures.is_empty() {
            return Err(format!("a replayed end was a failed retry: {failures:?}"));
        }
        Ok(())
    }

    #[test]
    fn retry_safe_recipe_fails_a_live_retry_that_does_not_complete() -> Result<(), String> {
        if let Some(failure) = live_retry_failure("turn-1", Ok(finished_turn("completed")?)) {
            return Err(format!("a completed live retry failed: {failure}"));
        }
        for terminal in ["failed", "cancelled"] {
            match live_retry_failure("turn-1", Ok(finished_turn(terminal)?)) {
                Some(failure)
                    if failure
                        .to_string()
                        .contains("retried turn turn-1 did not complete") => {}
                other => return Err(format!("a {terminal} live retry gave {other:?}")),
            }
        }
        let unsettled: Box<dyn std::error::Error> = Box::new(MuseError::TransportEnded);
        match live_retry_failure("turn-1", Err(unsettled)) {
            Some(failure) if failure.to_string().contains("turn turn-1 did not settle") => Ok(()),
            other => Err(format!("an unsettled live retry gave {other:?}")),
        }
    }

    #[test]
    fn retry_safe_recipe_returns_its_first_failed_retry() -> Result<(), String> {
        let failed = ready(retry_result(Ok(vec![
            refused_retry("command-a"),
            ended_retry("cancelled")?,
            refused_retry("command-c"),
        ])))
        .ok_or("the result of entries with no live turn stayed pending")?;
        match failed {
            Err(error) if error.to_string().contains("resending command-a failed") => {}
            other => return Err(format!("a failed retry did not fail the recipe: {other:?}")),
        }
        let replayed = ready(retry_result(Ok(vec![ended_retry("cancelled")?])))
            .ok_or("the result of a replayed end stayed pending")?;
        if let Err(error) = replayed {
            return Err(format!("a replayed end failed the recipe: {error}"));
        }
        match ready(retry_result(Err(MuseError::TransportEnded))) {
            Some(Err(error)) if error.to_string().contains("transport ended") => Ok(()),
            other => Err(format!(
                "a failed reconnect did not fail the recipe: {other:?}"
            )),
        }
    }

    /// Answers `initialize`, `session/start` and `turn/start` like a host, and
    /// sends each frame the test hands it once those replies are read.
    struct ScriptedTurnTransport {
        attach: ScriptedAttachTransport,
        replies: std::collections::VecDeque<String>,
        later: tokio::sync::mpsc::UnboundedReceiver<String>,
    }

    impl DuplexTransport for ScriptedTurnTransport {
        fn read_chunk(
            &mut self,
        ) -> Pin<Box<dyn Future<Output = io::Result<Option<String>>> + Send + '_>> {
            if self.attach.initialize_written && self.attach.response.is_some() {
                return self.attach.read_chunk();
            }
            if let Some(reply) = self.replies.pop_front() {
                return Box::pin(async move { Ok(Some(reply)) });
            }
            Box::pin(async move {
                match self.later.recv().await {
                    Some(frame) => Ok(Some(frame)),
                    None => pending::<io::Result<Option<String>>>().await,
                }
            })
        }

        fn write(
            &mut self,
            frame: &str,
        ) -> Pin<Box<dyn Future<Output = io::Result<()>> + Send + '_>> {
            let message: Value = serde_json::from_str(frame).unwrap_or(Value::Null);
            let id = message.get("id").cloned();
            let result = match message.get("method").and_then(Value::as_str) {
                Some("session/start") => Some(json!({
                    "session": {
                        "sessionId": "session-1",
                        "path": "/tmp/session.jsonl",
                        "status": "idle",
                        "activeTurnId": null,
                        "createdAt": "2026-09-29T00:00:00Z",
                        "updatedAt": "2026-09-29T00:00:00Z",
                        "workspaceRoot": null,
                        "providerId": null,
                        "modelId": null,
                        "turnCount": 0,
                        "forkedFrom": null
                    },
                    "viewCursor": "cursor-1"
                })),
                Some("turn/start") => Some(json!({
                    "commandId": message["params"]["commandId"],
                    "status": "accepted",
                    "turnId": "turn-1",
                    "startedNewTurn": true,
                    "disposition": "started"
                })),
                _ => None,
            };
            if let (Some(id), Some(result)) = (id, result) {
                self.replies.push_back(format!(
                    "{}\n",
                    json!({"jsonrpc": "2.0", "id": id, "result": result})
                ));
            }
            self.attach.write(frame)
        }
    }

    #[test]
    fn retry_safe_recipe_fails_a_live_retry_whose_turn_fails() -> Result<(), String> {
        current_thread_runtime()?.block_on(async {
            let (later, frames) = tokio::sync::mpsc::unbounded_channel();
            let transport = ScriptedTurnTransport {
                attach: ScriptedAttachTransport {
                    fingerprint: muse_code_sdk::fingerprint::EXPECTED.to_owned(),
                    response: None,
                    initialize_written: false,
                },
                replies: std::collections::VecDeque::new(),
                later: frames,
            };
            let client = MuseClient::attach(transport)
                .await
                .map_err(|error| format!("attach failed: {error}"))?
                .client;
            let session = client
                .start_session(StartSessionOptions::default())
                .await
                .map_err(|error| format!("session/start failed: {error}"))?;
            let turn = session
                .send_user_turn(user_turn("retried work"))
                .await
                .map_err(|error| format!("turn/start failed: {error}"))?;
            let completed = json!({
                "jsonrpc": "2.0",
                "method": "turn/completed",
                "params": {
                    "sessionId": "session-1",
                    "viewCursor": "cursor-2",
                    "sourceRange": {
                        "stream": {"kind": "run", "id": "turn-1"},
                        "first": {"id": "event-1", "sequence": 1},
                        "last": {"id": "event-1", "sequence": 1}
                    },
                    "turnId": "turn-1",
                    "terminal": "failed"
                }
            });
            later
                .send(format!("{completed}\n"))
                .map_err(|_| "the scripted host stopped reading".to_owned())?;
            let failures = settle_retries(vec![ReconnectResolution {
                command_id: "command-1".to_owned(),
                result: Ok(Reconnected::Live(turn)),
            }])
            .await;
            client.close().await;
            match failures.as_slice() {
                [failure]
                    if failure
                        .to_string()
                        .contains("retried turn turn-1 did not complete") =>
                {
                    Ok(())
                }
                other => Err(format!("a failed live retry was not reported: {other:?}")),
            }
        })
    }

    fn tool_call(status: &str) -> Result<Item, String> {
        item("toolCall", status, "fallbackText", "shell: pwd")
    }

    #[test]
    fn default_deny_recipe_requires_a_rejected_tool_call() -> Result<(), String> {
        let completed = finished_turn("completed")?;
        check_default_deny_turn(
            &completed,
            &[tool_call("inProgress")?, tool_call("rejected")?],
        )
        .map_err(|error| format!("a turn with a rejected tool call was refused: {error}"))?;
        for (case, items) in [
            (
                "no tool call",
                vec![item("agentMessage", "completed", "text", "I could not")?],
            ),
            ("a tool call the host ran", vec![tool_call("completed")?]),
            (
                "a tool call that failed on its own",
                vec![tool_call("failed")?],
            ),
            (
                "only a rejected subagent spawn",
                vec![item(
                    "subagent",
                    "rejected",
                    "fallbackText",
                    "spawn: helper",
                )?],
            ),
        ] {
            if check_default_deny_turn(&completed, &items).is_ok() {
                return Err(format!("a default-deny turn with {case} passed the recipe"));
            }
        }
        // A dead turn with no items is reported as incomplete, not as a model
        // that never tried the shell.
        let error = match check_default_deny_turn(&finished_turn("failed")?, &[]) {
            Ok(()) => return Err("a failed default-deny turn passed the recipe".to_owned()),
            Err(error) => error.to_string(),
        };
        if !error.contains("expected the default-deny turn to complete") {
            return Err(format!(
                "a dead default-deny turn was not reported as incomplete: {error}"
            ));
        }
        Ok(())
    }

    #[test]
    fn cancel_recipe_accepts_typed_client_closed_result() {
        assert!(is_expected_close_error(&MuseError::ClientClosed));
        assert!(!is_expected_close_error(&MuseError::TransportEnded));
    }

    #[test]
    fn cancel_recipe_propagates_non_close_errors() {
        assert!(report_close_settlement(Err(MuseError::ClientClosed)).is_ok());
        assert!(matches!(
            report_close_settlement(Err(MuseError::TransportEnded)),
            Err(MuseError::TransportEnded)
        ));
    }

    #[test]
    fn scripted_transport_uses_initialize_request_id() -> Result<(), String> {
        let mut transport = ScriptedAttachTransport::fingerprint_mismatch();
        let frame = json!({
            "jsonrpc": "2.0",
            "id": "initialize-42",
            "method": "initialize",
            "params": {}
        })
        .to_string();

        match ready(transport.write(&frame)) {
            Some(Ok(())) => {}
            Some(Err(error)) => return Err(format!("write failed: {error}")),
            None => return Err("write stayed pending".to_owned()),
        }
        let response = match ready(transport.read_chunk()) {
            Some(Ok(Some(response))) => response,
            Some(Ok(None)) => return Err("transport closed instead of responding".to_owned()),
            Some(Err(error)) => return Err(format!("read failed: {error}")),
            None => return Err("read stayed pending".to_owned()),
        };
        let response: serde_json::Value = serde_json::from_str(response.trim())
            .map_err(|error| format!("initialize response was not json: {error}"))?;
        assert_eq!(response["id"], json!("initialize-42"));
        assert_eq!(
            response["result"]["schema"]["fingerprint"],
            json!(MISMATCHED_FINGERPRINT)
        );
        Ok(())
    }
}