fastmcp-client 0.11.0

MCP client implementation for FastMCP
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
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
//! Task-capable, credential-bound multi-round tool operations.
//!
//! A tool can request host input before it completes or creates a Task. This
//! owner joins the existing Tasks discovery/dispatch path to the shared MRTR
//! continuation validator. It never restarts tools/call after a lost response,
//! resolves an input implicitly, or turns a Task into a completed tool result.
//! Use the existing Task driver after explicitly receiving a Task result.
//!
//! The opening access credential is pinned across all rounds. Refresh cannot
//! move an opaque continuation into a different authorization context. Expiry
//! ends this operation instead of silently logging in again or replaying it.

use std::fmt;
use std::future::Future;
use std::time::Instant;

use asupersync::Cx;
use asupersync::types::Time;
use fastmcp_core::McpRequestCancellation;
use fastmcp_protocol::{
    CoreRequest, FinalCoreResult, FinalInputResponses, InputRequiredResult, RequestId,
    ServerNotification,
};
use serde_json::Value;

use super::{
    ManagedTaskCall, ManagedTaskEvent, ManagedTaskRequest, ManagedTaskRequestIds,
    ManagedTasksClient, ManagedTasksError, ManagedTasksLimits, OAuthCredentialSnapshot,
    OAuthSessionError, PreparedTask, PreparedTaskRound, TaskDecoder, deadline_after,
    encode_request, prepare,
};
use crate::http_auth::rpc::ManagedCoreLimits;
use crate::http_auth::rpc::interaction::{
    InputSelection, ManagedInteractionError, ManagedInteractionLimits, admit_challenge,
    admit_fresh_id, continuation_request_selected, validate_initial,
};

/// Owned tool submission with explicit uncertainty across send/read abandonment.
pub mod submission;

/// Whole-operation bounds, in addition to the Tasks client's per-frame and
/// per-request limits. The Tasks client's timeout is one deadline for the entire interaction,
/// including discovery, credential acquisition, reads and host input pauses.
/// Records count notifications and tool results across every round; discovery
/// has one independently frame-bounded response per round. At most
/// `maximum_continuations + 1` discovery/tool POST pairs can be attempted.
#[derive(Clone, Copy, Debug)]
pub struct ManagedTaskInteractionPolicy {
    input: ManagedInteractionLimits,
    maximum_records: usize,
}

impl Default for ManagedTaskInteractionPolicy {
    fn default() -> Self {
        Self {
            input: ManagedInteractionLimits::default(),
            maximum_records: 256,
        }
    }
}

impl ManagedTaskInteractionPolicy {
    /// Zero continuations permits only an immediately completed or Task result.
    /// Zero input responses still permits explicitly authorized state-only
    /// rounds. There is no automatic retry allowance hidden in these bounds.
    pub fn new(
        maximum_continuations: usize,
        maximum_input_responses: usize,
        maximum_records: usize,
    ) -> Result<Self, ManagedTaskInteractionError> {
        if !(1..=1024).contains(&maximum_records) {
            return Err(ManagedTaskInteractionError::InvalidPolicy);
        }
        let input = ManagedInteractionLimits::new(
            ManagedCoreLimits::default(),
            maximum_continuations,
            maximum_input_responses,
        )?;
        Ok(Self {
            input,
            maximum_records,
        })
    }
}

/// Diagnostics retain neither arguments, answers, continuation state, task IDs
/// nor credentials. The existing protocol and transport errors remain typed.
#[derive(Debug)]
pub enum ManagedTaskInteractionError {
    InvalidPolicy,
    RecordLimit,
    InputPending,
    NotAwaitingInput,
    Closed,
    Task(ManagedTasksError),
    Interaction(ManagedInteractionError),
}

impl fmt::Display for ManagedTaskInteractionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidPolicy => f.write_str("invalid managed Task interaction policy"),
            Self::RecordLimit => f.write_str("Task interaction cumulative record limit exceeded"),
            Self::InputPending => f.write_str("Task interaction requires explicit host input"),
            Self::NotAwaitingInput => f.write_str("Task interaction is not awaiting input"),
            Self::Closed => f.write_str("managed Task interaction is closed"),
            Self::Task(error) => fmt::Display::fmt(error, f),
            Self::Interaction(error) => fmt::Display::fmt(error, f),
        }
    }
}
impl std::error::Error for ManagedTaskInteractionError {}
impl From<ManagedTasksError> for ManagedTaskInteractionError {
    fn from(error: ManagedTasksError) -> Self {
        Self::Task(error)
    }
}
impl From<OAuthSessionError> for ManagedTaskInteractionError {
    fn from(error: OAuthSessionError) -> Self {
        Self::Task(error.into())
    }
}
impl From<ManagedInteractionError> for ManagedTaskInteractionError {
    fn from(error: ManagedInteractionError) -> Self {
        Self::Interaction(error)
    }
}

/// A tool result retains its complete/Task distinction. InputRequired is not
/// successful EOF and cannot cause another POST without an explicit resume.
pub enum ManagedTaskInteractionEvent {
    Notification(Box<ServerNotification>),
    InputRequired(Box<InputRequiredResult>),
    Result(Box<FinalCoreResult>),
}

/// The host, not the protocol peer, selects whether input should be submitted.
/// Replies cannot replace the original call or supply their own requestState.
pub enum ManagedTaskInputAction {
    Respond {
        ids: ManagedTaskRequestIds,
        input_responses: Option<FinalInputResponses>,
    },
    RespondPartial {
        ids: ManagedTaskRequestIds,
        input_responses: FinalInputResponses,
    },
    /// Return ownership with the exact challenge still pending, without a POST.
    Pause,
}

/// A paused operation remains explicitly resumable. A result may be an
/// ordinary tool completion (including isError) or an actual Task creation.
/// Neither is automatically retried, polled, or converted into success.
pub enum ManagedTaskDriveOutcome {
    Paused(Box<ManagedTaskInteraction>),
    Result(Box<FinalCoreResult>),
}

struct ToolProgress {
    original: CoreRequest,
    pending: Option<Box<InputRequiredResult>>,
    used_ids: Vec<RequestId>,
    continuations: usize,
    input_responses: usize,
    records: usize,
    finished: bool,
    policy: ManagedTaskInteractionPolicy,
}

impl ToolProgress {
    fn new(
        original: CoreRequest,
        ids: &ManagedTaskRequestIds,
        policy: ManagedTaskInteractionPolicy,
    ) -> Result<Self, ManagedTaskInteractionError> {
        validate_initial(&original)?;
        Ok(Self {
            original,
            pending: None,
            used_ids: vec![ids.discovery.clone(), ids.operation.clone()],
            continuations: 0,
            input_responses: 0,
            records: 0,
            finished: false,
            policy,
        })
    }

    fn remaining_records(&self) -> usize {
        self.policy.maximum_records.saturating_sub(self.records)
    }

    // The caller has consumed response custody before invoking this method.
    // A validation error can never leave an input challenge resumable.
    fn admit_result(
        &mut self,
        result: Box<FinalCoreResult>,
    ) -> Result<ManagedTaskInteractionEvent, ManagedTaskInteractionError> {
        if self.remaining_records() == 0 {
            return Err(ManagedTaskInteractionError::RecordLimit);
        }
        self.records += 1;
        match result.as_ref() {
            FinalCoreResult::ToolsCallInputRequired { result: input, .. } => {
                let input: &InputRequiredResult = input;
                if self.remaining_records() == 0 {
                    return Err(ManagedTaskInteractionError::RecordLimit);
                }
                admit_challenge(
                    &self.original,
                    input,
                    self.policy.input,
                    self.continuations,
                    self.input_responses,
                )?;
                let input = Box::new(input.clone());
                self.pending = Some(input.clone());
                Ok(ManagedTaskInteractionEvent::InputRequired(input))
            }
            FinalCoreResult::ToolsCall { .. } | FinalCoreResult::ToolsCallTask { .. } => {
                self.finished = true;
                Ok(ManagedTaskInteractionEvent::Result(result))
            }
            _ => Err(ManagedTasksError::InvalidResponse.into()),
        }
    }

    // Pure local admission. A wrong key/type, reused ID, or oversized encoded
    // answer cannot consume the challenge or acquire a new credential.
    fn prepare_resume(
        &self,
        target: &str,
        ids: &ManagedTaskRequestIds,
        responses: Option<FinalInputResponses>,
        selection: InputSelection,
        limits: ManagedTasksLimits,
    ) -> Result<(PreparedTask, usize), ManagedTaskInteractionError> {
        let input = self
            .pending
            .as_ref()
            .ok_or(ManagedTaskInteractionError::NotAwaitingInput)?;
        if self.remaining_records() == 0 {
            return Err(ManagedTaskInteractionError::RecordLimit);
        }
        admit_challenge(
            &self.original,
            input,
            self.policy.input,
            self.continuations,
            self.input_responses,
        )?;
        admit_fresh_id(&self.used_ids, &ids.discovery)?;
        admit_fresh_id(&self.used_ids, &ids.operation)?;
        // ManagedTaskRequestIds also excludes aliases within the pair.
        let _ = ManagedTaskRequestIds::new(ids.discovery.clone(), ids.operation.clone())?;
        let count = responses.as_ref().map_or(0, FinalInputResponses::len);
        let next = continuation_request_selected(&self.original, input, responses, selection)?;
        let parameters = next
            .encode_params()
            .map_err(|_| ManagedTasksError::InvalidRequest)?
            .ok_or(ManagedTasksError::InvalidRequest)?;
        let name = parameters
            .get("name")
            .and_then(Value::as_str)
            .ok_or(ManagedTasksError::InvalidRequest)?
            .to_owned();
        let progress = parameters["_meta"]
            .get("progressToken")
            .map(|value| {
                serde_json::from_value(value.clone()).map_err(|_| ManagedTasksError::InvalidRequest)
            })
            .transpose()?;
        let wire = encode_request(
            target,
            "tools/call",
            &ids.operation,
            parameters,
            Some(name),
            limits.request_bytes,
        )?;
        Ok((
            PreparedTask {
                wire,
                decoder: TaskDecoder::Tool(Box::new(next)),
                progress,
            },
            count,
        ))
    }

    fn commit_resume(&mut self, ids: &ManagedTaskRequestIds, count: usize) {
        self.pending = None;
        self.used_ids
            .extend([ids.discovery.clone(), ids.operation.clone()]);
        self.continuations += 1;
        self.input_responses += count;
    }
}

/// Non-Clone ownership of the original tool operation, its current response or
/// pending challenge, and its opening credential. No previous answers are kept.
/// Abandoning a polled read/resume closes the operation. Local validation errors
/// during resume preserve the challenge, so the host can correct its answers.
/// There is no implicit server cancellation or exactly-once/restart guarantee.
pub struct ManagedTaskInteraction {
    client: ManagedTasksClient,
    credential: Option<OAuthCredentialSnapshot>,
    call: Option<Box<ManagedTaskCall>>,
    progress: ToolProgress,
    cancellation: McpRequestCancellation,
    deadline: Time,
}

impl fmt::Debug for ManagedTaskInteraction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ManagedTaskInteraction")
            .field("continuations", &self.progress.continuations)
            .field("awaiting_input", &self.progress.pending.is_some())
            .field("finished", &self.progress.finished)
            .finish_non_exhaustive()
    }
}

impl ManagedTasksClient {
    /// Starts a task-capable tool call that can be explicitly resumed through
    /// core input-required rounds. The original arguments and metadata remain
    /// immutable. Each round re-discovers Tasks with the exact opening token.
    pub async fn start_tool_interaction(
        &self,
        cx: &Cx,
        ids: ManagedTaskRequestIds,
        name: String,
        arguments: Option<Value>,
        policy: ManagedTaskInteractionPolicy,
    ) -> Result<ManagedTaskInteraction, ManagedTaskInteractionError> {
        self.start_tool_interaction_with_cancellation(
            cx,
            &McpRequestCancellation::new(),
            ids,
            name,
            arguments,
            policy,
        )
        .await
    }

    /// Retains one request-local cancellation domain and one absolute timeout
    /// across all rounds. A task returned at the end is not automatically polled
    /// or cancelled; its ongoing lifecycle belongs to the existing Task APIs.
    pub async fn start_tool_interaction_with_cancellation(
        &self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        ids: ManagedTaskRequestIds,
        name: String,
        arguments: Option<Value>,
        policy: ManagedTaskInteractionPolicy,
    ) -> Result<ManagedTaskInteraction, ManagedTaskInteractionError> {
        self.session.check(cx, cancellation)?;
        let deadline = deadline_after(cx, self.limits.timeout)?;
        let prepared = prepare(
            self.session.resource().as_str(),
            &self.metadata,
            &ids.operation,
            ManagedTaskRequest::CallTool { name, arguments },
            self.limits,
        )?;
        let TaskDecoder::Tool(original) = &prepared.decoder else {
            return Err(ManagedTasksError::InvalidRequest.into());
        };
        let progress = ToolProgress::new((**original).clone(), &ids, policy)?;
        let round = self.prepare_round(ids, prepared)?;
        let credential = self
            .session
            .await_active(cx, cancellation, deadline, None, async {
                self.session
                    .credential_with_cancellation(cx, cancellation)
                    .await
            })
            .await?;
        let call = self
            .execute_round(
                cx,
                cancellation,
                round,
                &credential,
                deadline,
                policy.maximum_records,
            )
            .await?;
        check_live(cx, self, cancellation, deadline, &credential)?;
        Ok(ManagedTaskInteraction {
            client: self.clone(),
            credential: Some(credential),
            call: Some(Box::new(call)),
            progress,
            cancellation: cancellation.clone(),
            deadline,
        })
    }
}

impl ManagedTaskInteraction {
    pub fn pending_input(&self) -> Option<&InputRequiredResult> {
        self.progress.pending.as_deref()
    }
    pub fn continuation_count(&self) -> usize {
        self.progress.continuations
    }
    pub fn is_finished(&self) -> bool {
        self.progress.finished
    }

    /// Drops the current response, challenge and credential without sending a
    /// cancellation notification or cancelling another operation's token.
    pub fn close(&mut self) {
        self.call = None;
        self.credential = None;
        self.progress.pending = None;
    }

    /// Drives input-required rounds with explicitly supplied host callbacks.
    /// No built-in roots, model, tool executor, browser, or consent policy is
    /// installed. The resolver receives each current challenge once; returning
    /// Pause hands this owner back without consuming state or request IDs.
    /// Notifications remain incremental rather than being buffered to the end.
    ///
    /// The opening credential's expiry, original operation deadline, session
    /// closure and both cancellation domains bound pending host work as well
    /// as network work. A callback must cooperate with cancellation by dropping
    /// its own child work; synchronous non-yielding work cannot be preempted.
    /// A callback error, invalid reply or abandoned driver is terminal locally,
    /// not permission to repeat an external host action or tool POST.
    pub async fn drive<R, F, N>(
        mut self,
        cx: &Cx,
        mut resolve: R,
        mut notify: N,
    ) -> Result<ManagedTaskDriveOutcome, ManagedTaskInteractionError>
    where
        R: FnMut(Box<InputRequiredResult>) -> F,
        F: Future<Output = Result<ManagedTaskInputAction, ManagedTaskInteractionError>>,
        N: FnMut(Box<ServerNotification>) -> Result<(), ManagedTaskInteractionError>,
    {
        loop {
            self.check(cx)?;
            let event = match self.pending_input() {
                Some(input) => ManagedTaskInteractionEvent::InputRequired(Box::new(input.clone())),
                None => self
                    .next_event(cx)
                    .await?
                    .ok_or(ManagedTaskInteractionError::Closed)?,
            };
            match event {
                ManagedTaskInteractionEvent::Notification(notification) => {
                    self.check(cx)?;
                    notify(notification)?;
                    self.check(cx)?;
                }
                ManagedTaskInteractionEvent::InputRequired(input) => {
                    self.check(cx)?;
                    let expiry = self
                        .credential
                        .as_ref()
                        .ok_or(ManagedTaskInteractionError::Closed)?
                        .expires_at();
                    // Invoke the host inside the guard, never before its first
                    // cancellation/deadline check. Session closure also wakes
                    // an idle resolver, without touching the caller's Cx.
                    let action = self
                        .client
                        .session
                        .await_active(cx, &self.cancellation, self.deadline, Some(expiry), async {
                            Ok(resolve(input).await)
                        })
                        .await??;
                    self.check(cx)?;
                    match action {
                        ManagedTaskInputAction::Pause => {
                            return Ok(ManagedTaskDriveOutcome::Paused(Box::new(self)));
                        }
                        ManagedTaskInputAction::Respond {
                            ids,
                            input_responses,
                        } => {
                            self.resume(cx, ids, input_responses).await?;
                        }
                        ManagedTaskInputAction::RespondPartial {
                            ids,
                            input_responses,
                        } => {
                            self.resume_partial(cx, ids, input_responses).await?;
                        }
                    }
                }
                ManagedTaskInteractionEvent::Result(result) => {
                    return Ok(ManagedTaskDriveOutcome::Result(result));
                }
            }
        }
    }

    pub async fn next_event(
        &mut self,
        cx: &Cx,
    ) -> Result<Option<ManagedTaskInteractionEvent>, ManagedTaskInteractionError> {
        if self.progress.finished {
            return Ok(None);
        }
        self.check(cx)?;
        if self.progress.pending.is_some() {
            return Err(ManagedTaskInteractionError::InputPending);
        }
        if self.progress.remaining_records() == 0 {
            self.close();
            return Err(ManagedTaskInteractionError::RecordLimit);
        }
        let mut call = self
            .call
            .take()
            .ok_or(ManagedTaskInteractionError::Closed)?;
        // The read future, not the reusable owner, now owns the credential too.
        let credential = self
            .credential
            .take()
            .ok_or(ManagedTaskInteractionError::Closed)?;
        let event = call
            .next_event(cx)
            .await?
            .ok_or(ManagedTasksError::MissingTerminal)?;
        check_live(
            cx,
            &self.client,
            &self.cancellation,
            self.deadline,
            &credential,
        )?;
        let event = match event {
            ManagedTaskEvent::Notification(notification) => {
                self.progress.records += 1;
                self.call = Some(call);
                ManagedTaskInteractionEvent::Notification(notification)
            }
            ManagedTaskEvent::ToolResult(result) => self.progress.admit_result(result)?,
            _ => return Err(ManagedTasksError::InvalidResponse.into()),
        };
        if !self.progress.finished {
            self.credential = Some(credential);
        }
        Ok(Some(event))
    }

    /// Submit exactly the current challenge's answers, with fresh discovery and
    /// operation IDs. State-only and absent/empty input-map distinctions are
    /// retained. The caller cannot replace requestState, metadata or arguments.
    pub async fn resume(
        &mut self,
        cx: &Cx,
        ids: ManagedTaskRequestIds,
        responses: Option<FinalInputResponses>,
    ) -> Result<(), ManagedTaskInteractionError> {
        self.resume_selected(cx, ids, responses, InputSelection::Complete)
            .await
    }

    /// Submit a nonempty subset of answers. A proper subset requires nonempty
    /// server continuation state; omitted inputs receive no synthetic answer.
    pub async fn resume_partial(
        &mut self,
        cx: &Cx,
        ids: ManagedTaskRequestIds,
        responses: FinalInputResponses,
    ) -> Result<(), ManagedTaskInteractionError> {
        self.resume_selected(cx, ids, Some(responses), InputSelection::Partial)
            .await
    }

    fn take_resume(
        &mut self,
        cx: &Cx,
        ids: ManagedTaskRequestIds,
        responses: Option<FinalInputResponses>,
        selection: InputSelection,
    ) -> Result<(PreparedTaskRound, OAuthCredentialSnapshot), ManagedTaskInteractionError> {
        self.check(cx)?;
        let (prepared, count) = self.progress.prepare_resume(
            self.client.session.resource().as_str(),
            &ids,
            responses,
            selection,
            self.client.limits,
        )?;
        let round = self.client.prepare_round(ids.clone(), prepared)?;
        self.check(cx)?;
        let credential = self
            .credential
            .take()
            .ok_or(ManagedTaskInteractionError::Closed)?;
        self.progress.commit_resume(&ids, count);
        // No await precedes this ownership transfer. Failure after this point
        // is an uncertain attempt, never permission to reuse the old challenge.
        Ok((round, credential))
    }

    async fn resume_selected(
        &mut self,
        cx: &Cx,
        ids: ManagedTaskRequestIds,
        responses: Option<FinalInputResponses>,
        selection: InputSelection,
    ) -> Result<(), ManagedTaskInteractionError> {
        let (round, credential) = self.take_resume(cx, ids, responses, selection)?;
        let call = self
            .client
            .execute_round(
                cx,
                &self.cancellation,
                round,
                &credential,
                self.deadline,
                self.progress.remaining_records(),
            )
            .await?;
        check_live(
            cx,
            &self.client,
            &self.cancellation,
            self.deadline,
            &credential,
        )?;
        self.call = Some(Box::new(call));
        self.credential = Some(credential);
        Ok(())
    }

    fn check(&mut self, cx: &Cx) -> Result<(), ManagedTaskInteractionError> {
        let result = match self.credential.as_ref() {
            Some(credential) => check_live(
                cx,
                &self.client,
                &self.cancellation,
                self.deadline,
                credential,
            ),
            None => Err(ManagedTaskInteractionError::Closed),
        };
        if result.is_err() {
            self.close();
        }
        result
    }
}

fn check_live(
    cx: &Cx,
    client: &ManagedTasksClient,
    cancellation: &McpRequestCancellation,
    deadline: Time,
    credential: &OAuthCredentialSnapshot,
) -> Result<(), ManagedTaskInteractionError> {
    client.session.check(cx, cancellation)?;
    let deadline = cx
        .budget()
        .deadline
        .map_or(deadline, |caller| caller.min(deadline));
    if cx.now() >= deadline {
        return Err(OAuthSessionError::TimedOut.into());
    }
    if Instant::now() >= credential.expires_at() || credential.credential().is_revoked() {
        return Err(OAuthSessionError::LoginRequired.into());
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::super::TASKS_EXTENSION;
    use super::*;
    use fastmcp_protocol::{ClientCapabilities, FinalRequestMeta};
    use serde_json::json;

    fn ids(first: i64, second: i64) -> ManagedTaskRequestIds {
        ManagedTaskRequestIds::new(RequestId::Number(first), RequestId::Number(second)).unwrap()
    }

    fn progress(capabilities: Value, policy: ManagedTaskInteractionPolicy) -> ToolProgress {
        let mut metadata =
            serde_json::to_value(FinalRequestMeta::new(ClientCapabilities::default())).unwrap();
        metadata[fastmcp_protocol::FINAL_CLIENT_CAPABILITIES_META_KEY] = capabilities;
        metadata["com.example/original"] = json!("retained");
        let prepared = prepare(
            "https://mcp.example/mcp",
            &metadata,
            &RequestId::Number(2),
            ManagedTaskRequest::CallTool {
                name: "work".to_owned(),
                arguments: Some(json!({"source":"original"})),
            },
            ManagedTasksLimits::default(),
        )
        .unwrap();
        let TaskDecoder::Tool(original) = prepared.decoder else {
            panic!("tool decoder")
        };
        ToolProgress::new(*original, &ids(1, 2), policy).unwrap()
    }

    fn receive(
        state: &mut ToolProgress,
        result: &str,
    ) -> Result<ManagedTaskInteractionEvent, ManagedTaskInteractionError> {
        let bytes = format!(r#"{{"jsonrpc":"2.0","id":2,"result":{result}}}"#);
        let decoder = TaskDecoder::Tool(Box::new(state.original.clone()));
        let ManagedTaskEvent::ToolResult(result) =
            super::super::decode_result(&decoder, bytes.as_bytes(), &RequestId::Number(2), 65536)
                .unwrap()
        else {
            panic!("tool result")
        };
        state.admit_result(result)
    }

    fn answers(value: Value) -> FinalInputResponses {
        serde_json::from_value(value).unwrap()
    }

    #[test]
    fn task_capable_continuation_preserves_original_and_can_finish_as_a_task() {
        let mut state = progress(json!({"roots":{}}), ManagedTaskInteractionPolicy::default());
        let before = state.original.encode_params().unwrap().unwrap();
        assert!(matches!(receive(&mut state, r#"{"resultType":"input_required","inputRequests":{"roots":{"method":"roots/list"}},"requestState":"  opaque+/%\u0000  "}"#).unwrap(),
            ManagedTaskInteractionEvent::InputRequired(_)));
        let (next, count) = state
            .prepare_resume(
                "https://mcp.example/mcp",
                &ids(3, 4),
                Some(answers(json!({"roots":{"roots":[]}}))),
                InputSelection::Complete,
                ManagedTasksLimits::default(),
            )
            .unwrap();
        let wire: Value = serde_json::from_slice(next.wire.body()).unwrap();
        assert_eq!(wire["method"], "tools/call");
        assert_eq!(wire["id"], 4);
        assert_eq!(wire["params"]["requestState"], "  opaque+/%\0  ");
        assert_eq!(
            wire["params"]["inputResponses"],
            json!({"roots":{"roots":[]}})
        );
        let mut after = wire["params"].clone();
        after.as_object_mut().unwrap().remove("inputResponses");
        after.as_object_mut().unwrap().remove("requestState");
        assert_eq!(after, before);
        assert_eq!(state.original.encode_params().unwrap().unwrap(), before);
        assert_eq!(
            after["_meta"][fastmcp_protocol::FINAL_CLIENT_CAPABILITIES_META_KEY]["extensions"],
            json!({TASKS_EXTENSION:{}})
        );
        state.commit_resume(&ids(3, 4), count);
        assert!(state.pending.is_none());
        let event = receive(&mut state, r#"{"resultType":"task","taskId":"task-one","status":"working","createdAt":"2026-09-16T00:00:00Z","lastUpdatedAt":"2026-09-16T00:00:00Z","ttlMs":60000}"#).unwrap();
        assert!(
            matches!(event, ManagedTaskInteractionEvent::Result(result) if matches!(*result, FinalCoreResult::ToolsCallTask { .. }))
        );
        assert!(state.finished);
        assert_eq!(state.continuations, 1);
        assert_eq!(state.records, 2);
    }

    #[test]
    fn invalid_answers_and_numeric_aliases_preserve_the_pending_challenge() {
        let mut state = progress(json!({"roots":{}}), ManagedTaskInteractionPolicy::default());
        receive(&mut state, r#"{"resultType":"input_required","inputRequests":{"roots":{"method":"roots/list"}},"requestState":"sealed"}"#).unwrap();
        for supplied in [
            json!({}),
            json!({"unknown":{"roots":[]}}),
            json!({"roots":{"action":"decline"}}),
        ] {
            assert!(
                state
                    .prepare_resume(
                        "https://mcp.example/mcp",
                        &ids(3, 4),
                        Some(answers(supplied)),
                        InputSelection::Complete,
                        ManagedTasksLimits::default()
                    )
                    .is_err()
            );
            assert_eq!(
                state.pending.as_ref().unwrap().request_state(),
                Some("sealed")
            );
            assert_eq!(
                (
                    state.continuations,
                    state.input_responses,
                    state.used_ids.len()
                ),
                (0, 0, 2)
            );
        }
        for pair in [
            ids(1, 4),
            ids(3, 2),
            ManagedTaskRequestIds::new(serde_json::from_str("2e0").unwrap(), RequestId::Number(4))
                .unwrap(),
        ] {
            assert!(matches!(
                state.prepare_resume(
                    "https://mcp.example/mcp",
                    &pair,
                    Some(answers(json!({"roots":{"roots":[]}}))),
                    InputSelection::Complete,
                    ManagedTasksLimits::default()
                ),
                Err(ManagedTaskInteractionError::Interaction(
                    ManagedInteractionError::RepeatedRequestId
                ))
            ));
        }
        assert!(state.pending.is_some());
    }

    #[test]
    fn partial_answers_require_state_and_never_accumulate_old_answers() {
        let mut state = progress(json!({"roots":{}}), ManagedTaskInteractionPolicy::default());
        receive(&mut state, r#"{"resultType":"input_required","inputRequests":{"a":{"method":"roots/list"},"b":{"method":"roots/list"}},"requestState":"state-one"}"#).unwrap();
        let (_, count) = state
            .prepare_resume(
                "https://mcp.example/mcp",
                &ids(3, 4),
                Some(answers(json!({"a":{"roots":[]}}))),
                InputSelection::Partial,
                ManagedTasksLimits::default(),
            )
            .unwrap();
        state.commit_resume(&ids(3, 4), count);
        receive(
            &mut state,
            r#"{"resultType":"input_required","inputRequests":{"b":{"method":"roots/list"}}}"#,
        )
        .unwrap();
        let (next, _) = state
            .prepare_resume(
                "https://mcp.example/mcp",
                &ids(5, 6),
                Some(answers(json!({"b":{"roots":[]}}))),
                InputSelection::Complete,
                ManagedTasksLimits::default(),
            )
            .unwrap();
        let wire: Value = serde_json::from_slice(next.wire.body()).unwrap();
        assert_eq!(wire["params"]["inputResponses"], json!({"b":{"roots":[]}}));
        assert!(wire["params"].get("requestState").is_none());

        let mut stateless = progress(json!({"roots":{}}), ManagedTaskInteractionPolicy::default());
        receive(&mut stateless, r#"{"resultType":"input_required","inputRequests":{"a":{"method":"roots/list"},"b":{"method":"roots/list"}}}"#).unwrap();
        assert!(matches!(
            stateless.prepare_resume(
                "https://mcp.example/mcp",
                &ids(3, 4),
                Some(answers(json!({"a":{"roots":[]}}))),
                InputSelection::Partial,
                ManagedTasksLimits::default()
            ),
            Err(ManagedTaskInteractionError::Interaction(
                ManagedInteractionError::PartialStateRequired
            ))
        ));
        assert!(stateless.pending.is_some());
    }

    #[test]
    fn challenge_limits_precede_host_work_but_immediate_completion_needs_no_resolver() {
        let mut no_capability = progress(json!({}), ManagedTaskInteractionPolicy::default());
        assert!(matches!(
            receive(
                &mut no_capability,
                r#"{"resultType":"input_required","inputRequests":{"roots":{"method":"roots/list"}}}"#
            ),
            Err(ManagedTaskInteractionError::Interaction(
                ManagedInteractionError::CapabilityNotAdvertised
            ))
        ));
        assert!(no_capability.pending.is_none());
        let policy = ManagedTaskInteractionPolicy::new(0, 0, 1).unwrap();
        let mut complete = progress(json!({}), policy);
        assert!(matches!(
            receive(
                &mut complete,
                r#"{"resultType":"complete","content":[],"isError":true}"#
            )
            .unwrap(),
            ManagedTaskInteractionEvent::Result(_)
        ));
        assert!(complete.finished);
        let mut cannot_continue = progress(json!({}), policy);
        assert!(
            receive(
                &mut cannot_continue,
                r#"{"resultType":"input_required","requestState":"state"}"#
            )
            .is_err()
        );
        assert!(cannot_continue.pending.is_none());
    }

    #[test]
    fn records_and_input_work_do_not_reset_on_each_continuation() {
        let policy = ManagedTaskInteractionPolicy::new(8, 1, 3).unwrap();
        let mut state = progress(json!({"roots":{}}), policy);
        receive(
            &mut state,
            r#"{"resultType":"input_required","inputRequests":{"a":{"method":"roots/list"}}}"#,
        )
        .unwrap();
        let (_, count) = state
            .prepare_resume(
                "https://mcp.example/mcp",
                &ids(3, 4),
                Some(answers(json!({"a":{"roots":[]}}))),
                InputSelection::Complete,
                ManagedTasksLimits::default(),
            )
            .unwrap();
        state.commit_resume(&ids(3, 4), count);
        assert!(matches!(
            receive(
                &mut state,
                r#"{"resultType":"input_required","inputRequests":{"b":{"method":"roots/list"}}}"#
            ),
            Err(ManagedTaskInteractionError::Interaction(
                ManagedInteractionError::InputLimit
            ))
        ));
        assert_eq!(state.input_responses, 1);
        assert_eq!(state.records, 2);
        assert!(state.pending.is_none());
    }

    #[test]
    fn absent_and_present_empty_answers_remain_distinct_with_tasks_negotiated() {
        for (source, supplied, present) in [
            (
                r#"{"resultType":"input_required","requestState":""}"#,
                None,
                false,
            ),
            (
                r#"{"resultType":"input_required","inputRequests":{}}"#,
                Some(answers(json!({}))),
                true,
            ),
        ] {
            let mut state = progress(json!({}), ManagedTaskInteractionPolicy::default());
            receive(&mut state, source).unwrap();
            let (next, count) = state
                .prepare_resume(
                    "https://mcp.example/mcp",
                    &ids(3, 4),
                    supplied,
                    InputSelection::Complete,
                    ManagedTasksLimits::default(),
                )
                .unwrap();
            let wire: Value = serde_json::from_slice(next.wire.body()).unwrap();
            assert_eq!(wire["params"].get("inputResponses").is_some(), present);
            assert_eq!(count, 0);
        }
    }

    fn pending_interaction() -> (
        ManagedTaskInteraction,
        crate::http_auth::BoundBearerCredential,
    ) {
        use crate::http_auth::managed::{OAuthSessionPolicy, SessionInner};
        use crate::http_auth::oauth::{OAuthClient, OAuthClientConfiguration};
        use crate::http_auth::{BoundBearerCredential, CanonicalHttpUrl};
        use asupersync::sync::Mutex;
        use std::sync::{
            Arc,
            atomic::{AtomicBool, AtomicUsize},
        };

        let url = |value| CanonicalHttpUrl::parse(value).unwrap();
        let resource = url("https://mcp.example/mcp");
        let config = OAuthClientConfiguration::from_trusted_endpoints(
            "https://issuer.example",
            url("https://issuer.example/authorize"),
            url("https://issuer.example/token"),
            resource.clone(),
            "native-client",
            vec![],
        )
        .unwrap();
        let session = super::super::ManagedOAuthSession {
            inner: Arc::new(SessionInner {
                client: OAuthClient::new(config),
                resource: resource.clone(),
                policy: OAuthSessionPolicy::default(),
                state: Arc::new(Mutex::new(None)),
                closed: McpRequestCancellation::new(),
                logout_handoff: AtomicBool::new(false),
                pending: AtomicUsize::new(0),
            }),
        };
        let expiry = Instant::now() + std::time::Duration::from_secs(60);
        let bearer =
            BoundBearerCredential::bind_with_expiry(resource, "opening-secret", expiry).unwrap();
        let credential =
            OAuthCredentialSnapshot::new(&bearer, &[], 1, expiry, &session.inner.closed).unwrap();
        let mut progress = progress(json!({"roots":{}}), ManagedTaskInteractionPolicy::default());
        receive(&mut progress, r#"{"resultType":"input_required","inputRequests":{"roots":{"method":"roots/list"}},"requestState":"sealed-original"}"#).unwrap();
        let metadata = progress.original.encode_params().unwrap().unwrap()["_meta"].clone();
        (
            ManagedTaskInteraction {
                client: ManagedTasksClient {
                    session,
                    metadata,
                    limits: ManagedTasksLimits::default(),
                },
                credential: Some(credential),
                call: None,
                progress,
                cancellation: McpRequestCancellation::new(),
                deadline: Time::from_nanos(u64::MAX),
            },
            bearer,
        )
    }

    #[test]
    fn resume_transfers_custody_once_and_keeps_continuations_out_of_discovery() {
        let cx = Cx::for_testing();
        let (mut operation, bearer) = pending_interaction();
        let rejected = operation.take_resume(
            &cx,
            ids(3, 4),
            Some(answers(json!({}))),
            InputSelection::Complete,
        );
        assert!(rejected.is_err());
        assert!(operation.pending_input().is_some());
        assert!(operation.credential.is_some());
        let (round, credential) = operation
            .take_resume(
                &cx,
                ids(3, 4),
                Some(answers(json!({"roots":{"roots":[]}}))),
                InputSelection::Complete,
            )
            .unwrap();
        assert!(operation.pending_input().is_none());
        assert!(operation.credential.is_none());
        assert_eq!(operation.continuation_count(), 1);
        let discovery: Value = serde_json::from_slice(round.discover_wire.body()).unwrap();
        let continuation: Value = serde_json::from_slice(round.prepared.wire.body()).unwrap();
        assert_eq!(discovery["method"], "server/discover");
        assert!(discovery["params"].get("requestState").is_none());
        assert!(discovery["params"].get("inputResponses").is_none());
        assert_eq!(continuation["params"]["requestState"], "sealed-original");
        for wire in [&round.discover_wire, &round.prepared.wire] {
            let authenticated = credential.authorize_request(wire).unwrap();
            assert!(
                authenticated
                    .headers()
                    .iter()
                    .any(|(name, value)| name.eq_ignore_ascii_case("authorization")
                        && value == "Bearer opening-secret")
            );
        }
        // Abandon the exact attempt before receiving a response. There is no
        // retained credential or pending challenge with which to send it again.
        drop((round, credential));
        assert!(matches!(
            operation.take_resume(
                &cx,
                ids(5, 6),
                Some(answers(json!({"roots":{"roots":[]}}))),
                InputSelection::Complete
            ),
            Err(ManagedTaskInteractionError::Closed)
        ));
        assert!(!bearer.is_revoked());
        assert!(cx.checkpoint().is_ok());
    }

    #[test]
    fn oversized_continuation_is_correctable_without_consuming_ids_or_state() {
        let cx = Cx::for_testing();
        let (mut operation, _) = pending_interaction();
        operation.client.limits =
            ManagedTasksLimits::new(1024, 4096, 16, std::time::Duration::from_secs(60)).unwrap();
        let uri = format!("file:///{}", "x".repeat(2048));
        assert!(matches!(
            operation.take_resume(
                &cx,
                ids(3, 4),
                Some(answers(json!({"roots":{"roots":[{"uri":uri}]}}))),
                InputSelection::Complete
            ),
            Err(ManagedTaskInteractionError::Task(
                ManagedTasksError::RequestTooLarge
            ))
        ));
        assert!(operation.pending_input().is_some());
        assert_eq!(operation.continuation_count(), 0);
        assert_eq!(operation.progress.used_ids.len(), 2);
        assert!(
            operation
                .take_resume(
                    &cx,
                    ids(3, 4),
                    Some(answers(json!({"roots":{"roots":[]}}))),
                    InputSelection::Complete
                )
                .is_ok()
        );
    }

    #[test]
    fn revoked_opening_credential_closes_pending_input_instead_of_renewing() {
        let cx = Cx::for_testing();
        let (mut operation, bearer) = pending_interaction();
        let (sibling, independent) = pending_interaction();
        bearer.revoke();
        assert!(matches!(
            operation.take_resume(
                &cx,
                ids(3, 4),
                Some(answers(json!({"roots":{"roots":[]}}))),
                InputSelection::Complete
            ),
            Err(ManagedTaskInteractionError::Task(
                ManagedTasksError::Session(OAuthSessionError::LoginRequired)
            ))
        ));
        assert!(operation.pending_input().is_none());
        assert!(operation.credential.is_none());
        assert_eq!(operation.continuation_count(), 0);
        assert!(!operation.client.session.inner.closed.is_cancel_requested());
        assert!(!independent.is_revoked());
        assert!(sibling.pending_input().is_some());
    }

    #[test]
    fn pending_input_is_not_eof_and_cancellation_closes_only_its_owner() {
        use std::future::Future;
        use std::task::{Context, Poll, Waker};
        let cx = Cx::for_testing();
        let (mut operation, bearer) = pending_interaction();
        let mut task = Context::from_waker(Waker::noop());
        {
            let mut read = Box::pin(operation.next_event(&cx));
            assert!(matches!(
                read.as_mut().poll(&mut task),
                Poll::Ready(Err(ManagedTaskInteractionError::InputPending))
            ));
        }
        assert!(operation.pending_input().is_some());
        operation.cancellation.cancel();
        {
            let mut read = Box::pin(operation.next_event(&cx));
            assert!(matches!(
                read.as_mut().poll(&mut task),
                Poll::Ready(Err(ManagedTaskInteractionError::Task(
                    ManagedTasksError::Session(OAuthSessionError::Cancelled)
                )))
            ));
        }
        assert!(operation.pending_input().is_none());
        assert!(!bearer.is_revoked());
        assert!(!operation.client.session.inner.closed.is_cancel_requested());
        assert!(cx.checkpoint().is_ok());
    }

    #[test]
    fn host_driver_can_pause_without_spending_continuation_state_or_ids() {
        use asupersync::runtime::{RuntimeBuilder, reactor::create_reactor};
        use std::cell::Cell;
        RuntimeBuilder::current_thread()
            .with_reactor(create_reactor().unwrap())
            .build()
            .unwrap()
            .block_on(async {
                let cx = Cx::current().unwrap();
                let (operation, bearer) = pending_interaction();
                let calls = Cell::new(0);
                let outcome = operation
                    .drive(
                        &cx,
                        |input| {
                            calls.set(calls.get() + 1);
                            assert_eq!(input.request_state(), Some("sealed-original"));
                            std::future::ready(Ok(ManagedTaskInputAction::Pause))
                        },
                        |_| panic!("no notification was received"),
                    )
                    .await
                    .unwrap();
                let ManagedTaskDriveOutcome::Paused(mut operation) = outcome else {
                    panic!("host pause")
                };
                assert_eq!(calls.get(), 1);
                assert_eq!(operation.continuation_count(), 0);
                assert_eq!(operation.progress.used_ids.len(), 2);
                assert_eq!(
                    operation.pending_input().unwrap().request_state(),
                    Some("sealed-original")
                );
                assert!(
                    operation
                        .take_resume(
                            &cx,
                            ids(3, 4),
                            Some(answers(json!({"roots":{"roots":[]}}))),
                            InputSelection::Complete
                        )
                        .is_ok()
                );
                assert!(!bearer.is_revoked());
                assert!(cx.checkpoint().is_ok());
            });
    }

    #[test]
    fn host_driver_does_not_retry_resolver_failure_or_invalid_answers() {
        use asupersync::runtime::{RuntimeBuilder, reactor::create_reactor};
        use std::cell::Cell;
        RuntimeBuilder::current_thread()
            .with_reactor(create_reactor().unwrap())
            .build()
            .unwrap()
            .block_on(async {
                let cx = Cx::current().unwrap();
                for invalid in [false, true] {
                    let (operation, bearer) = pending_interaction();
                    let calls = Cell::new(0);
                    let result = operation
                        .drive(
                            &cx,
                            |_| {
                                calls.set(calls.get() + 1);
                                std::future::ready(if invalid {
                                    Ok(ManagedTaskInputAction::Respond {
                                        ids: ids(3, 4),
                                        input_responses: Some(answers(json!({}))),
                                    })
                                } else {
                                    Err(ManagedInteractionError::AbortedByHost.into())
                                })
                            },
                            |_| Ok(()),
                        )
                        .await;
                    assert!(matches!(
                        result,
                        Err(ManagedTaskInteractionError::Interaction(_))
                    ));
                    assert_eq!(calls.get(), 1);
                    assert!(!bearer.is_revoked());
                    assert!(cx.checkpoint().is_ok());
                }
            });
    }

    struct PendingResolver(std::sync::Arc<std::sync::atomic::AtomicUsize>);
    impl Future for PendingResolver {
        type Output = Result<ManagedTaskInputAction, ManagedTaskInteractionError>;
        fn poll(
            self: std::pin::Pin<&mut Self>,
            _: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Self::Output> {
            std::task::Poll::Pending
        }
    }
    impl Drop for PendingResolver {
        fn drop(&mut self) {
            self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
    }

    #[test]
    fn closing_session_wakes_and_drops_an_idle_host_resolver() {
        use asupersync::runtime::{RuntimeBuilder, reactor::create_reactor};
        use std::sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        };
        use std::task::{Context, Poll, Wake, Waker};
        struct Count(AtomicUsize);
        impl Wake for Count {
            fn wake(self: Arc<Self>) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
            fn wake_by_ref(self: &Arc<Self>) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }
        RuntimeBuilder::current_thread()
            .with_reactor(create_reactor().unwrap())
            .build()
            .unwrap()
            .block_on(async {
                let cx = Cx::current().unwrap();
                let (operation, _) = pending_interaction();
                let session_close = operation.client.session.inner.closed.clone();
                let cancellation = operation.cancellation.clone();
                let dropped = Arc::new(AtomicUsize::new(0));
                let counter = Arc::new(Count(AtomicUsize::new(0)));
                let waker = Waker::from(Arc::clone(&counter));
                let mut task = Context::from_waker(&waker);
                let mut driver = Box::pin(operation.drive(
                    &cx,
                    |_| PendingResolver(Arc::clone(&dropped)),
                    |_| Ok(()),
                ));
                assert!(driver.as_mut().poll(&mut task).is_pending());
                assert_eq!(dropped.load(Ordering::SeqCst), 0);
                let before = counter.0.load(Ordering::SeqCst);
                session_close.cancel();
                assert!(
                    counter.0.load(Ordering::SeqCst) > before,
                    "must wake without a resolver event"
                );
                assert!(matches!(
                    driver.as_mut().poll(&mut task),
                    Poll::Ready(Err(ManagedTaskInteractionError::Task(
                        ManagedTasksError::Session(OAuthSessionError::Closed)
                    )))
                ));
                assert_eq!(dropped.load(Ordering::SeqCst), 1);
                assert!(!cancellation.is_cancel_requested());
                assert!(cx.checkpoint().is_ok());
            });
    }

    #[test]
    fn abandoning_driver_drops_host_work_without_revoking_sibling_credentials() {
        use asupersync::runtime::{RuntimeBuilder, reactor::create_reactor};
        use std::sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        };
        use std::task::{Context, Waker};
        RuntimeBuilder::current_thread()
            .with_reactor(create_reactor().unwrap())
            .build()
            .unwrap()
            .block_on(async {
                let cx = Cx::current().unwrap();
                let (operation, bearer) = pending_interaction();
                let session = operation.client.session.clone();
                let dropped = Arc::new(AtomicUsize::new(0));
                let mut driver = Box::pin(operation.drive(
                    &cx,
                    |_| PendingResolver(Arc::clone(&dropped)),
                    |_| Ok(()),
                ));
                let mut task = Context::from_waker(Waker::noop());
                assert!(driver.as_mut().poll(&mut task).is_pending());
                drop(driver);
                assert_eq!(dropped.load(Ordering::SeqCst), 1);
                assert!(!session.inner.closed.is_cancel_requested());
                assert!(!bearer.is_revoked());
                assert!(cx.checkpoint().is_ok());
            });
    }

    #[test]
    fn pre_cancelled_driver_never_invokes_host_code() {
        use std::cell::Cell;
        use std::task::{Context, Poll, Waker};
        let cx = Cx::for_testing();
        let (operation, bearer) = pending_interaction();
        operation.cancellation.cancel();
        let calls = Cell::new(0);
        let mut driver = Box::pin(operation.drive(
            &cx,
            |_| {
                calls.set(calls.get() + 1);
                std::future::ready(Ok(ManagedTaskInputAction::Pause))
            },
            |_| Ok(()),
        ));
        let mut task = Context::from_waker(Waker::noop());
        assert!(matches!(
            driver.as_mut().poll(&mut task),
            Poll::Ready(Err(ManagedTaskInteractionError::Task(
                ManagedTasksError::Session(OAuthSessionError::Cancelled)
            )))
        ));
        assert_eq!(calls.get(), 0);
        assert!(!bearer.is_revoked());
    }
}