omni-dev 0.46.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
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
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
//! Gated Docs/Sheets content writes and lease acquisition over MCP (ADR-0091).
//!
//! Handlers delegate to the CLI's engines. Gates, backup leases, freshness,
//! locking and audit records belong to those engines, never this transport.

use anyhow::{Context, Result};
use rmcp::{
    handler::server::wrapper::Parameters,
    model::{CallToolResult, ContentBlock as Content},
    schemars, tool, tool_router, ErrorData as McpError,
};
use serde::{Deserialize, Serialize};

use super::{error::tool_error, server::OmniDevServer};
use crate::cli::drive::{helpers, lease::LeaseFlags, sheets::values::ValuesFormat};
use crate::drive::{
    client::DriveClient,
    docs::{client::DocsClient, write as docs_write},
    lease::{
        acquire::{self, AcquireOptions, AcquireResult},
        authenticate,
    },
    sheets::{api::ValueInputOption, client::SheetsClient, write as sheets_write},
};
use crate::mcp::drive_tools::account_param_doc;

/// Parameters for DocsReplace operations.
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DriveDocsReplaceParams {
    /// Document id from /d/<ID>/ in a Docs URL, e.g. 1a2B3c4D. Required.
    pub document_id: String,
    /// Literal text to find, e.g. draft; not a regular expression. Required.
    pub search: String,
    /// Replacement text, e.g. final. Empty deletes matches. Required.
    pub replace: String,
    /// Case-insensitive search. Default false (case-sensitive).
    #[serde(default)]
    pub ignore_case: Option<bool>,
    /// Preview without mutating or requiring a lease. Default false; preview first.
    #[serde(default)]
    pub dry_run: Option<bool>,
    /// Token from drive_lease_acquire or omni-dev drive lease acquire. Not needed for
    /// dry_run or operator require_lease:false rules; any supplied token is still
    /// validated.
    #[serde(default)]
    pub lease: Option<String>,
    #[doc = account_param_doc!()]
    #[serde(default)]
    pub account: Option<String>,
}

/// Parameters for DocsAppend operations.
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DriveDocsAppendParams {
    /// Document id from /d/<ID>/ in a Docs URL, e.g. 1a2B3c4D. Required.
    pub document_id: String,
    /// Text to append, e.g. a new paragraph. Exactly one of text or text_path is required.
    #[serde(default)]
    pub text: Option<String>,
    /// Local UTF-8 text file, mutually exclusive with text. Stdin (-) is unsupported.
    #[serde(default)]
    pub text_path: Option<String>,
    /// Preview without mutating or requiring a lease. Default false; preview first.
    #[serde(default)]
    pub dry_run: Option<bool>,
    /// Token from drive_lease_acquire or omni-dev drive lease acquire. Not needed for
    /// dry_run or operator require_lease:false rules; any supplied token is still
    /// validated.
    #[serde(default)]
    pub lease: Option<String>,
    #[doc = account_param_doc!()]
    #[serde(default)]
    pub account: Option<String>,
}

/// Parameters for SheetsWrite operations.
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DriveSheetsWriteParams {
    /// Spreadsheet id from /d/<ID>/ in a Sheets URL, e.g. 1a2B3c4D. Required.
    pub spreadsheet_id: String,
    /// A1 range, e.g. A1:B2 or Sheet1!A1:B2. Omit with sheet to target the tab.
    #[serde(default)]
    pub range: Option<String>,
    /// Tab title, e.g. Sheet1. Supplies a bare range prefix; conflicts with a range already
    /// naming a tab.
    #[serde(default)]
    pub sheet: Option<String>,
    /// Inline array of rows, e.g. `[["name", "score"], ["Ada", "42"]]`. Exactly one of values
    /// or values_path is required.
    #[serde(default)]
    pub values: Option<Vec<Vec<String>>>,
    /// Local UTF-8 CSV/TSV/JSON file; mutually exclusive with values. Stdin (-) is unsupported.
    #[serde(default)]
    pub values_path: Option<String>,
    /// File format: auto (default; infer extension), csv, tsv, json. Only applies to values_path.
    #[serde(default)]
    pub values_format: Option<String>,
    /// Interpretation: user-entered (default; parses formulas/dates/numbers) or raw (literal text).
    #[serde(default)]
    pub input: Option<String>,
    /// Preview without mutating or requiring a lease. Default false; preview first.
    #[serde(default)]
    pub dry_run: Option<bool>,
    /// Token from drive_lease_acquire or omni-dev drive lease acquire. Not needed for
    /// dry_run or operator require_lease:false rules; any supplied token is still
    /// validated.
    #[serde(default)]
    pub lease: Option<String>,
    #[doc = account_param_doc!()]
    #[serde(default)]
    pub account: Option<String>,
}

/// Parameters for SheetsClear operations.
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DriveSheetsClearParams {
    /// Spreadsheet id from /d/<ID>/ in a Sheets URL, e.g. 1a2B3c4D. Required.
    pub spreadsheet_id: String,
    /// A1 range, e.g. A1:B2 or Sheet1!A1:B2. Omit with sheet to target the tab.
    #[serde(default)]
    pub range: Option<String>,
    /// Tab title, e.g. Sheet1. Supplies a bare range prefix; conflicts with a range already
    /// naming a tab.
    #[serde(default)]
    pub sheet: Option<String>,
    /// Preview without mutating or requiring a lease. Default false; preview first.
    #[serde(default)]
    pub dry_run: Option<bool>,
    /// Token from drive_lease_acquire or omni-dev drive lease acquire. Not needed for
    /// dry_run or operator require_lease:false rules; any supplied token is still
    /// validated.
    #[serde(default)]
    pub lease: Option<String>,
    #[doc = account_param_doc!()]
    #[serde(default)]
    pub account: Option<String>,
}

/// Parameters for LeaseAcquire operations.
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DriveLeaseAcquireParams {
    /// Drive file id to back up and lease, e.g. 1a2B3c4D. Required.
    pub file_id: String,
    /// Lease lifetime in minutes (1–1440). Defaults to operator env/settings, then 30;
    /// writes never extend it.
    #[serde(default)]
    pub expiry_minutes: Option<i64>,
    #[doc = account_param_doc!()]
    #[serde(default)]
    pub account: Option<String>,
}

#[allow(missing_docs)] // tool_router generates the public router.
#[tool_router(router = drive_write_tool_router, vis = "pub")]
impl OmniDevServer {
    /// Replace literal text in a Google Doc; use drive_docs_append to add text at the end.
    #[tool(
        description = "Replace literal text in a Google Doc; use drive_docs_append to add text at the end. \
                       Mirrors `omni-dev drive docs replace`. Case-sensitive by default. Example: \
                       document_id, search:\"draft\", replace:\"final\", dry_run:true. occurrences is a \
                       body-only estimate excluding headers/footers/footnotes; zero still sends a real \
                       request, and occurrences_changed is authoritative. stale-revision means reread and \
                       retry through the complete engine, which may then refuse a stale lease. dry_run \
                       defaults to false. Dry-run first: previews need no lease. Real writes require an \
                       operator allow rule and a lease unless require_lease:false. Acquire with \
                       drive_lease_acquire. A lease permits repeated writes to one file until expiry. \
                       refused-lease-stale needs a fresh backup: release with omni-dev drive lease release \
                       or wait for expiry before acquiring (acquire reuses stale live tokens too). \
                       Cancellation/timeout is not rollback; inspect state before retrying, especially \
                       append. Output is complete tagged YAML; refusals/failures set is_error."
    )]
    pub async fn drive_docs_replace(
        &self,
        Parameters(params): Parameters<DriveDocsReplaceParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        let payload = docs_write::WritePayload::Replace {
            search: params.search.clone(),
            replace: params.replace.clone(),
            match_case: !params.ignore_case.unwrap_or(false),
        };
        run_docs_write(
            &client,
            &DocsClient::from_drive_client(&client).map_err(tool_error)?,
            &params.document_id,
            payload,
            params.dry_run.unwrap_or(false),
            params.lease.clone(),
            params.account.as_deref(),
        )
        .await
        .map_err(tool_error)
    }

    /// Append text at the end of a Google Doc; use drive_docs_replace to change existing text.
    #[tool(
        description = "Append text at the end of a Google Doc; use drive_docs_replace to change existing \
                       text. Mirrors `omni-dev drive docs append`. Example: document_id, text:\"new \
                       paragraph\", dry_run:true. Use text_path for existing files. stale-revision means \
                       reread and retry through the complete engine, which may then refuse a stale lease. \
                       dry_run defaults to false. Dry-run first: previews need no lease. Real writes require \
                       an operator allow rule and a lease unless require_lease:false. Acquire with \
                       drive_lease_acquire. A lease permits repeated writes to one file until expiry. \
                       refused-lease-stale needs a fresh backup: release with omni-dev drive lease release \
                       or wait for expiry before acquiring (acquire reuses stale live tokens too). \
                       Cancellation/timeout is not rollback; inspect state before retrying, especially \
                       append. Output is complete tagged YAML; refusals/failures set is_error."
    )]
    pub async fn drive_docs_append(
        &self,
        Parameters(params): Parameters<DriveDocsAppendParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        let input = params.clone();
        let text = tokio::task::spawn_blocking(move || append_text(&input))
            .await
            .context("Text input task failed")
            .map_err(tool_error)?
            .map_err(tool_error)?;
        run_docs_write(
            &client,
            &DocsClient::from_drive_client(&client).map_err(tool_error)?,
            &params.document_id,
            docs_write::WritePayload::Append { text },
            params.dry_run.unwrap_or(false),
            params.lease.clone(),
            params.account.as_deref(),
        )
        .await
        .map_err(tool_error)
    }

    /// Overwrite cell values in a Google Sheet, dropping rich-text runs; use
    /// drive_sheets_append to add table rows or drive_sheets_clear to empty values.
    #[tool(
        description = "Overwrite cell values in a Google Sheet, dropping rich-text runs; use \
                       drive_sheets_append to add table rows or drive_sheets_clear to empty values. Mirrors \
                       `omni-dev drive sheets write`. Example: spreadsheet_id, range:\"A1:B2\", \
                       values:[[\"a\",\"b\"]], dry_run:true. dry_run defaults to false. Dry-run first: previews \
                       need no lease. Real writes require an operator allow rule and a lease unless \
                       require_lease:false. Acquire with drive_lease_acquire. A lease permits repeated \
                       writes to one file until expiry. refused-lease-stale needs a fresh backup: release \
                       with omni-dev drive lease release or wait for expiry before acquiring (acquire reuses \
                       stale live tokens too). Cancellation/timeout is not rollback; inspect state before \
                       retrying, especially append. Output is complete tagged YAML; refusals/failures set \
                       is_error."
    )]
    pub async fn drive_sheets_write(
        &self,
        Parameters(params): Parameters<DriveSheetsWriteParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        run_sheets_write(
            &client,
            &SheetsClient::from_drive_client(&client).map_err(tool_error)?,
            &params,
            sheets_write::WriteVerb::Write,
        )
        .await
        .map_err(tool_error)
    }

    /// Append rows after the table in a Google Sheet range; use drive_sheets_write to
    /// overwrite existing cells.
    #[tool(
        description = "Append rows after the table in a Google Sheet range; use drive_sheets_write to \
                       overwrite existing cells. Mirrors `omni-dev drive sheets append`. Example: \
                       spreadsheet_id, range:\"A1:B2\", values:[[\"a\",\"b\"]], dry_run:true. dry_run defaults to \
                       false. Dry-run first: previews need no lease. Real writes require an operator allow \
                       rule and a lease unless require_lease:false. Acquire with drive_lease_acquire. A \
                       lease permits repeated writes to one file until expiry. refused-lease-stale needs a \
                       fresh backup: release with omni-dev drive lease release or wait for expiry before \
                       acquiring (acquire reuses stale live tokens too). Cancellation/timeout is not \
                       rollback; inspect state before retrying, especially append. Output is complete tagged \
                       YAML; refusals/failures set is_error."
    )]
    pub async fn drive_sheets_append(
        &self,
        Parameters(params): Parameters<DriveSheetsWriteParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        run_sheets_write(
            &client,
            &SheetsClient::from_drive_client(&client).map_err(tool_error)?,
            &params,
            sheets_write::WriteVerb::Append,
        )
        .await
        .map_err(tool_error)
    }

    /// Clear cell values while retaining formatting; use drive_sheets_write to replace them.
    #[tool(
        description = "Clear cell values while retaining formatting; use drive_sheets_write to replace them. \
                       Mirrors `omni-dev drive sheets clear`. Example: spreadsheet_id, range:\"A1:B2\", \
                       dry_run:true. dry_run defaults to false. Dry-run first: previews need no lease. Real \
                       writes require an operator allow rule and a lease unless require_lease:false. Acquire \
                       with drive_lease_acquire. A lease permits repeated writes to one file until expiry. \
                       refused-lease-stale needs a fresh backup: release with omni-dev drive lease release \
                       or wait for expiry before acquiring (acquire reuses stale live tokens too). \
                       Cancellation/timeout is not rollback; inspect state before retrying, especially \
                       append. Output is complete tagged YAML; refusals/failures set is_error."
    )]
    pub async fn drive_sheets_clear(
        &self,
        Parameters(params): Parameters<DriveSheetsClearParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        run_sheets_clear(
            &client,
            &SheetsClient::from_drive_client(&client).map_err(tool_error)?,
            &params,
        )
        .await
        .map_err(tool_error)
    }

    /// Back up a Drive file and acquire a write lease.
    #[tool(
        description = "Back up a Drive file and acquire a write lease. Mirrors `omni-dev drive lease \
                       acquire`. Example: file_id:\"1a2B3c4D\", expiry_minutes:30. Prompts for device-owner \
                       authentication (Touch ID or account password; operator biometrics_only requires Touch \
                       ID). Native Docs/Sheets need the selected account's lease_backup_folder_id. \
                       Operator-only headless waiver applies; callers cannot set it. One consent covers \
                       repeated writes to that file until expiry. already-leased successfully returns the \
                       existing token, even if stale: release it via CLI or wait for expiry before renewing. \
                       The OS prompt can wait up to 120 seconds. Timeout/cancellation is not rollback: wait \
                       for the original call to finish, then explicitly repeat acquisition to recover an \
                       undelivered live token without another prompt/backup. Do not automatically race \
                       acquisitions. Output is tagged YAML; denied/unavailable/refused/failed set is_error."
    )]
    pub async fn drive_lease_acquire(
        &self,
        Parameters(params): Parameters<DriveLeaseAcquireParams>,
    ) -> Result<CallToolResult, McpError> {
        let client = helpers::create_client_for(params.account.as_deref()).map_err(tool_error)?;
        let opts = acquire_options(&params).map_err(tool_error)?;
        let authenticator = authenticate::platform_authenticator();
        run_lease_acquire(&client, &opts, authenticator.as_ref())
            .await
            .map_err(tool_error)
    }
}

fn yaml_outcome(outcome: &impl Serialize, is_error: bool) -> Result<CallToolResult> {
    let yaml = serde_yaml::to_string(outcome).context("Failed to serialize Drive outcome")?;
    Ok(if is_error {
        CallToolResult::error(vec![Content::text(yaml)])
    } else {
        CallToolResult::success(vec![Content::text(yaml)])
    })
}

fn docs_is_error(result: &docs_write::WriteResult) -> bool {
    use docs_write::WriteResult::{
        Appended, Blocked, Deleted, Failed, Formatted, Inserted, ListFormatted, MutatedNamedRange,
        RefusedAnchor, RefusedLeaseExpired, RefusedLeaseStale, RefusedLeaseWrongFile,
        RefusedNamedRange, RefusedNoLease, RefusedNoRevisionId, RefusedNoVisibleParents,
        RefusedNotADocument, RefusedShortcut, Replaced, StaleRevision, WouldAppend, WouldDelete,
        WouldFormat, WouldFormatList, WouldInsert, WouldMutateNamedRange, WouldReplace,
    };
    match result {
        WouldMutateNamedRange { .. }
        | MutatedNamedRange { .. }
        | WouldReplace { .. }
        | WouldAppend { .. }
        | docs_write::WriteResult::WouldEditTable { .. }
        | docs_write::WriteResult::EditedTable { .. }
        | WouldInsert { .. }
        | WouldDelete { .. }
        | WouldFormat { .. }
        | Replaced { .. }
        | Appended { .. }
        | Inserted { .. }
        | Deleted { .. }
        | WouldFormatList { .. }
        | ListFormatted { .. }
        | Formatted { .. } => false,
        RefusedNamedRange { .. }
        | RefusedNotADocument { .. }
        | docs_write::WriteResult::RefusedTable { .. }
        | RefusedAnchor { .. }
        | RefusedShortcut
        | RefusedNoVisibleParents
        | RefusedNoRevisionId
        | Blocked { .. }
        | RefusedNoLease
        | RefusedLeaseExpired
        | RefusedLeaseWrongFile
        | RefusedLeaseStale
        | StaleRevision { .. }
        | Failed { .. } => true,
    }
}

fn sheets_is_error(result: &sheets_write::WriteResult) -> bool {
    use sheets_write::WriteResult::{
        Appended, Blocked, Cleared, Failed, RefusedEmptyValues, RefusedLeaseExpired,
        RefusedLeaseStale, RefusedLeaseWrongFile, RefusedNoLease, RefusedNoVisibleParents,
        RefusedNotASpreadsheet, RefusedSheetNotFound, RefusedShortcut, WouldClear, WouldWrite,
        Written,
    };
    match result {
        WouldWrite { .. } | WouldClear | Written { .. } | Appended { .. } | Cleared { .. } => false,
        RefusedNotASpreadsheet { .. }
        | RefusedShortcut
        | RefusedNoVisibleParents
        | RefusedSheetNotFound { .. }
        | RefusedEmptyValues
        | Blocked { .. }
        | RefusedNoLease
        | RefusedLeaseExpired
        | RefusedLeaseWrongFile
        | RefusedLeaseStale
        | Failed { .. } => true,
    }
}

async fn run_docs_write(
    drive: &DriveClient,
    docs: &DocsClient,
    document_id: &str,
    payload: docs_write::WritePayload,
    dry_run: bool,
    lease: Option<String>,
    account: Option<&str>,
) -> Result<CallToolResult> {
    let opts = docs_write::WriteOptions {
        document_id: document_id.to_owned(),
        payload,
        dry_run,
        lease_token: lease,
        ledger_path: helpers::resolve_ledger_path(dry_run)?,
    };
    let rules = helpers::account_rules(account)?;
    let outcome = docs_write::write(drive, docs, &opts, &rules).await;
    yaml_outcome(&outcome, docs_is_error(&outcome.result))
}

fn append_text(params: &DriveDocsAppendParams) -> Result<String> {
    super::content_input::require_bounded_content_input(
        params.text.as_deref(),
        params.text_path.as_deref(),
        "text",
        crate::drive::files_api::MAX_UPLOAD_BYTES,
    )
}

fn parse_values_format(value: Option<&str>) -> Result<ValuesFormat> {
    match value.unwrap_or("auto").to_ascii_lowercase().as_str() {
        "auto" => Ok(ValuesFormat::Auto),
        "csv" => Ok(ValuesFormat::Csv),
        "tsv" => Ok(ValuesFormat::Tsv),
        "json" => Ok(ValuesFormat::Json),
        _ => anyhow::bail!("values_format must be auto, csv, tsv or json"),
    }
}

fn parse_input(value: Option<&str>) -> Result<ValueInputOption> {
    match value
        .unwrap_or("user-entered")
        .to_ascii_lowercase()
        .as_str()
    {
        "user-entered" => Ok(ValueInputOption::UserEntered),
        "raw" => Ok(ValueInputOption::Raw),
        _ => anyhow::bail!("input must be user-entered or raw"),
    }
}

fn sheets_values(params: &DriveSheetsWriteParams) -> Result<Vec<Vec<String>>> {
    let format = parse_values_format(params.values_format.as_deref())?;
    match (&params.values, &params.values_path) {
        (Some(_), Some(_)) => anyhow::bail!("Provide either values or values_path, not both"),
        (Some(values), None) => {
            anyhow::ensure!(
                params.values_format.is_none(),
                "values_format applies only to values_path"
            );
            Ok(values.clone())
        }
        (None, Some(path)) => {
            anyhow::ensure!(
                path != "-",
                "values_path must be a file; stdin is unsupported"
            );
            let content = super::content_input::require_bounded_content_input(
                None,
                Some(path),
                "values",
                crate::drive::files_api::MAX_UPLOAD_BYTES,
            )?;
            crate::cli::drive::sheets::values::parse(&content, format.resolve(path))
        }
        (None, None) => anyhow::bail!("Provide either values or values_path"),
    }
}

async fn run_sheets_write(
    drive: &DriveClient,
    sheets: &SheetsClient,
    params: &DriveSheetsWriteParams,
    verb: sheets_write::WriteVerb,
) -> Result<CallToolResult> {
    let dry_run = params.dry_run.unwrap_or(false);
    let input = params.clone();
    let values = tokio::task::spawn_blocking(move || sheets_values(&input))
        .await
        .context("Values input task failed")??;
    let opts = sheets_write::WriteOptions {
        spreadsheet_id: params.spreadsheet_id.clone(),
        verb,
        range: params.range.clone(),
        sheet: params.sheet.clone(),
        values,
        input: parse_input(params.input.as_deref())?,
        dry_run,
        lease_token: params.lease.clone(),
        ledger_path: helpers::resolve_ledger_path(dry_run)?,
    };
    let rules = helpers::account_rules(params.account.as_deref())?;
    let outcome = sheets_write::write(drive, sheets, &opts, &rules).await;
    yaml_outcome(&outcome, sheets_is_error(&outcome.result))
}

async fn run_sheets_clear(
    drive: &DriveClient,
    sheets: &SheetsClient,
    params: &DriveSheetsClearParams,
) -> Result<CallToolResult> {
    let dry_run = params.dry_run.unwrap_or(false);
    let opts = sheets_write::WriteOptions {
        spreadsheet_id: params.spreadsheet_id.clone(),
        verb: sheets_write::WriteVerb::Clear,
        range: params.range.clone(),
        sheet: params.sheet.clone(),
        values: Vec::new(),
        input: ValueInputOption::default(),
        dry_run,
        lease_token: params.lease.clone(),
        ledger_path: helpers::resolve_ledger_path(dry_run)?,
    };
    let rules = helpers::account_rules(params.account.as_deref())?;
    let outcome = sheets_write::write(drive, sheets, &opts, &rules).await;
    yaml_outcome(&outcome, sheets_is_error(&outcome.result))
}

fn acquire_options(params: &DriveLeaseAcquireParams) -> Result<AcquireOptions> {
    let resolved = LeaseFlags {
        backup_dir: None,
        expiry_minutes: params.expiry_minutes,
        biometrics_only: false,
        allow_headless: false,
    }
    .resolve()?;
    Ok(AcquireOptions {
        file_id: params.file_id.clone(),
        backup_dir: resolved.backup_dir,
        expiry: resolved.expiry,
        auth_policy: resolved.auth_policy,
        allow_headless: resolved.allow_headless,
        native_backup_folder_id: helpers::account_lease_backup_folder_id(
            params.account.as_deref(),
        )?,
        ledger_path: crate::drive::lease::ledger::ledger_path()?,
        supersedes: None,
    })
}

async fn run_lease_acquire(
    drive: &DriveClient,
    opts: &AcquireOptions,
    authenticator: &dyn authenticate::Authenticator,
) -> Result<CallToolResult> {
    let outcome = acquire::acquire(drive, opts, authenticator).await;
    let is_error = match &outcome {
        AcquireResult::Acquired { .. } | AcquireResult::AlreadyLeased { .. } => false,
        AcquireResult::RefusedNativeDocument
        | AcquireResult::RefusedConcurrentChange { .. }
        | AcquireResult::Denied { .. }
        | AcquireResult::Unavailable { .. }
        | AcquireResult::Failed { .. } => true,
    };
    yaml_outcome(&outcome, is_error)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::drive::lease::authenticate::AuthOutcome;
    use crate::drive::test_support::{
        client_with_bootstrapped_token, seed_lease, EnvGuard, FakeAuthenticator,
    };
    use crate::test_support::env::MapEnv;
    use crate::utils::settings::Settings;
    use serde_json::json;
    use wiremock::{
        matchers::{body_partial_json, method, path, query_param},
        Mock, MockServer, ResponseTemplate,
    };

    // EnvGuard serializes this HOME/settings-dependent test with other domains.
    // Restore logging overrides even if an assertion panics.
    struct LogRoute(Vec<(&'static str, Option<std::ffi::OsString>)>);
    impl LogRoute {
        fn new(path: &std::path::Path) -> Self {
            let snapshot = ["OMNI_DEV_LOG_FILE", "OMNI_DEV_LOG_DISABLE"]
                .into_iter()
                .map(|key| (key, std::env::var_os(key)))
                .collect();
            std::env::set_var("OMNI_DEV_LOG_FILE", path);
            std::env::remove_var("OMNI_DEV_LOG_DISABLE");
            Self(snapshot)
        }
    }
    impl Drop for LogRoute {
        fn drop(&mut self) {
            for (key, value) in &self.0 {
                if let Some(value) = value {
                    std::env::set_var(key, value);
                } else {
                    std::env::remove_var(key);
                }
            }
        }
    }

    fn decoded(result: &CallToolResult) -> serde_yaml::Value {
        let text = result.content[0].as_text().unwrap();
        serde_yaml::from_str(&text.text).unwrap()
    }

    fn params<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> T {
        serde_json::from_value(value).unwrap()
    }

    fn configure(dir: &std::path::Path, allow: bool) {
        let rules = if allow {
            json!({"rules":[{"file_id":"target", "allow":["docs-write","sheets-write"], "require_lease":true}]})
        } else {
            json!({"rules":[{"file_id":"target", "deny":["docs-write","sheets-write"]}]})
        };
        Settings::upsert_drive_account(
            &dir.join(".omni-dev/settings.json"),
            "work",
            &[
                ("write_permissions", rules),
                ("lease_backup_folder_id", json!("backup-folder")),
            ],
        )
        .unwrap();
    }

    async fn clients(server: &MockServer) -> (DriveClient, DocsClient, SheetsClient) {
        let drive = client_with_bootstrapped_token(server).await;
        let env = MapEnv::new()
            .with(crate::drive::docs::client::DOCS_API_URL, &server.uri())
            .with(crate::drive::sheets::client::SHEETS_API_URL, &server.uri());
        let docs = DocsClient::from_drive_client_with(&env, &drive).unwrap();
        let sheets = SheetsClient::from_drive_client_with(&env, &drive).unwrap();
        (drive, docs, sheets)
    }

    async fn file(server: &MockServer, mime: &str) {
        Mock::given(method("GET")).and(path("/drive/v3/files/target"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id":"target", "name":"Target", "mimeType":mime, "version":"1", "parents":[]})))
            .mount(server).await;
    }

    async fn document(server: &MockServer) {
        Mock::given(method("GET")).and(path("/v1/documents/target"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"documentId":"target", "revisionId":"rev-1", "body":{"content":[{"startIndex":1,"endIndex":7,"paragraph":{"elements":[{"startIndex":1,"endIndex":7,"textRun":{"content":"draft\n"}}]}}]}})))
            .mount(server).await;
    }

    #[tokio::test]
    async fn all_five_verbs_preview_refuse_missing_lease_and_apply_a_valid_lease() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), true);
        // Three stages for every verb. Exact mutation counts catch a preview/refusal
        // that accidentally writes, as well as a real write that does nothing.
        for verb in ["replace", "docs-append", "write", "sheets-append", "clear"] {
            let server = MockServer::start().await;
            let (drive, docs, sheets) = clients(&server).await;
            let is_docs = verb == "replace" || verb == "docs-append";
            file(
                &server,
                if is_docs {
                    crate::drive::types::GOOGLE_DOC_MIME_TYPE
                } else {
                    crate::drive::types::GOOGLE_SHEET_MIME_TYPE
                },
            )
            .await;
            if is_docs {
                document(&server).await;
                let request = if verb == "replace" {
                    json!({"requests":[{"replaceAllText":{"containsText":{"text":"draft", "matchCase":true},"replaceText":"final"}}],"writeControl":{"requiredRevisionId":"rev-1"}})
                } else {
                    json!({"writeControl":{"requiredRevisionId":"rev-1"}})
                };
                Mock::given(method("POST"))
                    .and(path("/v1/documents/target:batchUpdate"))
                    .and(body_partial_json(request))
                    .respond_with(ResponseTemplate::new(200).set_body_json(
                        json!({"replies":[{"replaceAllText":{"occurrencesChanged":1}}]}),
                    ))
                    .expect(1)
                    .mount(&server)
                    .await;
            } else {
                let endpoint = match verb {
                    "write" => "/v4/spreadsheets/target/values/A1:B2",
                    "sheets-append" => "/v4/spreadsheets/target/values/A1:B2:append",
                    _ => "/v4/spreadsheets/target/values/A1:B2:clear",
                };
                let mock = Mock::given(method(if verb == "write" { "PUT" } else { "POST" }))
                    .and(path(endpoint));
                let mock = if verb == "clear" {
                    mock
                } else {
                    mock.and(query_param("valueInputOption", "USER_ENTERED"))
                        .and(body_partial_json(json!({"values":[["a","b"]]})))
                };
                mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({"updatedRange":"A1:B2", "updatedCells":2, "clearedRange":"A1:B2", "updates":{"updatedRange":"A1:B2","updatedCells":2}}))).expect(1).mount(&server).await;
            }
            for stage in 0..3 {
                let token = (stage == 2).then(|| {
                    seed_lease(
                        &crate::drive::lease::ledger::ledger_path().unwrap(),
                        "target",
                        "1",
                    )
                });
                let result = if is_docs {
                    let payload = if verb == "replace" {
                        docs_write::WritePayload::Replace {
                            search: "draft".into(),
                            replace: "final".into(),
                            match_case: true,
                        }
                    } else {
                        docs_write::WritePayload::Append {
                            text: "new\n".into(),
                        }
                    };
                    run_docs_write(
                        &drive,
                        &docs,
                        "target",
                        payload,
                        stage == 0,
                        token,
                        Some("work"),
                    )
                    .await
                    .unwrap()
                } else if verb == "clear" {
                    run_sheets_clear(&drive, &sheets, &params(json!({"spreadsheet_id":"target", "range":"A1:B2", "dry_run":stage == 0,"lease":token,"account":"work"}))).await.unwrap()
                } else {
                    run_sheets_write(&drive, &sheets, &params(json!({"spreadsheet_id":"target","range":"A1:B2","values":[["a","b"]],"dry_run":stage == 0,"lease":token,"account":"work"})), if verb == "write" { sheets_write::WriteVerb::Write } else { sheets_write::WriteVerb::Append }).await.unwrap()
                };
                let yaml = decoded(&result);
                let expected = match (stage, verb) {
                    (0, "replace") => "would-replace",
                    (0, "docs-append") => "would-append",
                    (0, "clear") => "would-clear",
                    (0, _) => "would-write",
                    (1, _) => "refused-no-lease",
                    (2, "replace") => "replaced",
                    (2, "docs-append" | "sheets-append") => "appended",
                    (2, "write") => "written",
                    _ => "cleared",
                };
                assert_eq!(
                    yaml["result"]["status"].as_str(),
                    Some(expected),
                    "{verb}/{stage}: {yaml:?}"
                );
                assert_eq!(result.is_error, Some(stage == 1));
                if stage < 2 {
                    let requests = server.received_requests().await.unwrap();
                    assert!(!requests.iter().any(|r| r.method.as_str() == "PUT"
                        || r.url.path().ends_with(":batchUpdate")
                        || r.url.path().ends_with(":append")
                        || r.url.path().ends_with(":clear")));
                }
            }
        }
    }

    #[tokio::test]
    async fn blocked_writes_fetch_no_content_and_signal_error() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), false);
        let log = dir.path().join("requests.jsonl");
        let _log_route = LogRoute::new(&log);
        let server = MockServer::start().await;
        let (drive, docs, _) = clients(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        let result = crate::request_log::CTX
            .scope(
                crate::request_log::RequestLogContext::mcp("drive_docs_append"),
                run_docs_write(
                    &drive,
                    &docs,
                    "target",
                    docs_write::WritePayload::Append {
                        text: "hello".into(),
                    },
                    false,
                    None,
                    Some("work"),
                ),
            )
            .await
            .unwrap();
        assert_eq!(decoded(&result)["result"]["status"], "blocked");
        assert_eq!(result.is_error, Some(true));
        let records: Vec<serde_json::Value> = std::fs::read_to_string(&log)
            .unwrap()
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        let mutation = records
            .iter()
            .find(|r| r["kind"] == "drivemutation")
            .unwrap();
        assert_eq!(mutation["source"], "mcp");
        assert_eq!(mutation["mcp_tool"], "drive_docs_append");
        assert_eq!(mutation["context"]["status"], "blocked");
        assert!(server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .all(|r| r.url.path() == "/token" || r.url.path() == "/drive/v3/files/target"));
    }

    #[tokio::test]
    async fn stale_lease_and_stale_revision_remain_distinct_errors() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), true);
        let server = MockServer::start().await;
        let (drive, docs, _) = clients(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        document(&server).await;
        let ledger = crate::drive::lease::ledger::ledger_path().unwrap();
        let token = seed_lease(&ledger, "target", "0");
        let result = run_docs_write(
            &drive,
            &docs,
            "target",
            docs_write::WritePayload::Append {
                text: "hello".into(),
            },
            false,
            Some(token),
            Some("work"),
        )
        .await
        .unwrap();
        assert_eq!(decoded(&result)["result"]["status"], "refused-lease-stale");
        assert_eq!(result.is_error, Some(true));
        Mock::given(method("POST")).and(path("/v1/documents/target:batchUpdate"))
            .respond_with(ResponseTemplate::new(400).set_body_json(json!({"error":{"code":400,"message":"The required revision ID 'rev-1' does not match the latest revision.","status":"INVALID_ARGUMENT"}}))).expect(1).mount(&server).await;
        let token = seed_lease(&ledger, "target", "1");
        let result = run_docs_write(
            &drive,
            &docs,
            "target",
            docs_write::WritePayload::Append {
                text: "hello".into(),
            },
            false,
            Some(token),
            Some("work"),
        )
        .await
        .unwrap();
        assert_eq!(decoded(&result)["result"]["status"], "stale-revision");
        assert_eq!(decoded(&result)["required_revision_id"], "rev-1");
        assert_eq!(result.is_error, Some(true));
    }

    #[test]
    fn inputs_validate_conflicts_formats_defaults_and_files() {
        assert_eq!(parse_input(None).unwrap(), ValueInputOption::UserEntered);
        assert_eq!(parse_input(Some("RAW")).unwrap(), ValueInputOption::Raw);
        assert!(parse_input(Some("invalid")).is_err());
        assert!(parse_values_format(Some("invalid")).is_err());
        let dir = tempfile::tempdir().unwrap();
        for (extension, content) in [
            ("csv", "a,b\n"),
            ("tsv", "a\tb\n"),
            ("json", r#"[["a","b"]]"#),
        ] {
            let p = dir.path().join(format!("cells.{extension}"));
            std::fs::write(&p, content).unwrap();
            for format in ["auto", extension] {
                let p: DriveSheetsWriteParams = params(
                    json!({"spreadsheet_id":"target","values_path":p,"values_format":format}),
                );
                assert_eq!(sheets_values(&p).unwrap(), vec![vec!["a", "b"]]);
            }
        }
        for value in [
            json!({}),
            json!({"values":[["a"]],"values_path":"missing"}),
            json!({"values_path":"-"}),
            json!({"values":[["a"]],"values_format":"csv"}),
        ] {
            let mut value = value;
            value["spreadsheet_id"] = json!("target");
            assert!(sheets_values(&params(value)).is_err());
        }
        for value in [
            json!({}),
            json!({"text":"a","text_path":"missing"}),
            json!({"text_path":"-"}),
        ] {
            let mut value = value;
            value["document_id"] = json!("target");
            assert!(append_text(&params(value)).is_err());
        }
        let text_path = dir.path().join("text.txt");
        std::fs::write(&text_path, "hello").unwrap();
        assert_eq!(
            append_text(&params(
                json!({"document_id":"target","text_path":text_path})
            ))
            .unwrap(),
            "hello"
        );
    }

    #[test]
    fn explicit_account_controls_rules_and_backup_folder() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), true);
        Settings::upsert_drive_account(
            &dir.path().join(".omni-dev/settings.json"),
            "other",
            &[("lease_backup_folder_id", json!("other-backups"))],
        )
        .unwrap();
        std::env::set_var(crate::drive::account::DRIVE_ACCOUNT_ENV, "other");
        assert!(helpers::account_rules(None).unwrap().is_empty());
        assert_eq!(helpers::account_rules(Some("work")).unwrap().len(), 1);
        assert_eq!(
            helpers::account_lease_backup_folder_id(Some("work"))
                .unwrap()
                .as_deref(),
            Some("backup-folder")
        );
        assert_eq!(
            helpers::account_lease_backup_folder_id(None)
                .unwrap()
                .as_deref(),
            Some("other-backups")
        );
        assert!(helpers::account_rules(Some("unknown")).is_err());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn acquisition_authorizes_native_backup_and_recovers_existing_token() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        let server = MockServer::start().await;
        let drive = client_with_bootstrapped_token(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        Mock::given(method("POST")).and(path("/drive/v3/files/target/copy"))
            .and(body_partial_json(json!({"parents":["backup-folder"]})))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id":"backup-copy","name":"Backup","mimeType":crate::drive::types::GOOGLE_DOC_MIME_TYPE}))).expect(1).mount(&server).await;
        let opts = AcquireOptions {
            file_id: "target".into(),
            backup_dir: dir.path().join("backups"),
            native_backup_folder_id: Some("backup-folder".into()),
            expiry: chrono::Duration::minutes(30),
            auth_policy: authenticate::AuthPolicy::DeviceOwner,
            ledger_path: dir.path().join("ledger.jsonl"),
            allow_headless: false,
            supersedes: None,
        };
        let result = run_lease_acquire(&drive, &opts, &FakeAuthenticator(AuthOutcome::Authorized))
            .await
            .unwrap();
        let yaml = decoded(&result);
        assert_eq!(yaml["status"], "acquired");
        assert_eq!(result.is_error, Some(false));
        let recovered = run_lease_acquire(
            &drive,
            &opts,
            &FakeAuthenticator(AuthOutcome::Denied("should not prompt".into())),
        )
        .await
        .unwrap();
        assert_eq!(decoded(&recovered)["status"], "already-leased");
        assert_eq!(decoded(&recovered)["token"], yaml["token"]);
        assert_eq!(recovered.is_error, Some(false));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn acquisition_denied_and_unavailable_take_no_backup_and_mint_no_token() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        let server = MockServer::start().await;
        let drive = client_with_bootstrapped_token(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        for (auth, status, waiver) in [
            (AuthOutcome::Denied("no".into()), "denied", false),
            (
                AuthOutcome::NoAuthenticator("headless".into()),
                "unavailable",
                false,
            ),
            (
                AuthOutcome::PolicyUnsatisfiable("no biometrics".into()),
                "unavailable",
                true,
            ),
        ] {
            let ledger = dir.path().join(format!("{status}-{waiver}.jsonl"));
            let opts = AcquireOptions {
                file_id: "target".into(),
                backup_dir: dir.path().join("backups"),
                native_backup_folder_id: Some("backup-folder".into()),
                expiry: chrono::Duration::minutes(30),
                auth_policy: authenticate::AuthPolicy::DeviceOwner,
                ledger_path: ledger.clone(),
                allow_headless: waiver,
                supersedes: None,
            };
            let result = run_lease_acquire(&drive, &opts, &FakeAuthenticator(auth))
                .await
                .unwrap();
            assert_eq!(decoded(&result)["status"], status);
            assert_eq!(result.is_error, Some(true));
            assert!(
                !ledger.exists() || std::fs::read_to_string(&ledger).unwrap().trim().is_empty()
            );
        }
        assert!(server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .all(|r| r.url.path() == "/token" || r.url.path() == "/drive/v3/files/target"));
    }

    #[tokio::test]
    async fn all_six_handlers_propagate_credentials_errors() {
        let guard = EnvGuard::take();
        let _dir = guard.clear_credentials();
        let server = OmniDevServer::new();
        assert!(server
            .drive_docs_replace(Parameters(params(
                json!({"document_id":"target","search":"a","replace":"b"})
            )))
            .await
            .is_err());
        assert!(server
            .drive_docs_append(Parameters(params(
                json!({"document_id":"target","text":"a"})
            )))
            .await
            .is_err());
        assert!(server
            .drive_sheets_write(Parameters(params(
                json!({"spreadsheet_id":"target","values":[["a"]]})
            )))
            .await
            .is_err());
        assert!(server
            .drive_sheets_append(Parameters(params(
                json!({"spreadsheet_id":"target","values":[["a"]]})
            )))
            .await
            .is_err());
        assert!(server
            .drive_sheets_clear(Parameters(params(
                json!({"spreadsheet_id":"target","range":"A1"})
            )))
            .await
            .is_err());
        assert!(server
            .drive_lease_acquire(Parameters(params(json!({"file_id":"target"}))))
            .await
            .is_err());
    }
    #[test]
    fn mcp_sources_do_not_call_mutating_api_wrappers() {
        let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
        for (file, names) in [
            (
                "src/drive/files_api.rs",
                &[
                    "rename",
                    "move_to",
                    "trash",
                    "untrash",
                    "create",
                    "copy",
                    "upload",
                    "edit_content",
                ][..],
            ),
            ("src/drive/docs/api.rs", &["batch_update"][..]),
            (
                "src/drive/sheets/api.rs",
                &[
                    "values_update",
                    "values_append",
                    "values_clear",
                    "batch_update",
                    "copy_to",
                ][..],
            ),
        ] {
            let source = std::fs::read_to_string(manifest.join(file)).unwrap();
            for name in names {
                assert!(
                    source.contains(&format!("pub(in crate::drive) async fn {name}(")),
                    "{file}: mutation wrapper {name} must stay fenced"
                );
            }
        }

        let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/mcp");
        for entry in std::fs::read_dir(root).unwrap() {
            let path = entry.unwrap().path();
            if path.extension().is_none_or(|ext| ext != "rs") {
                continue;
            }
            let source = std::fs::read_to_string(&path).unwrap();
            let production = source.split("#[cfg(test)]").next().unwrap();
            for method in [
                "batch_update",
                "values_update",
                "values_append",
                "values_clear",
                "edit_content",
                "rename",
                "move_to",
                "trash",
                "untrash",
                "copy_to",
                "copy",
                "upload",
                "create",
            ] {
                let direct = format!(".{method}(");
                let associated = format!("FilesApi::{method}(");
                // create is generic across services; check only the Drive-associated form.
                assert!(
                    (method == "create" || !production.contains(&direct))
                        && !production.contains(&associated),
                    "{} directly calls mutation wrapper {method}",
                    path.display()
                );
            }
        }
    }

    #[tokio::test]
    async fn a_write_ahead_audit_failure_is_an_error_without_mutation() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), true);
        let server = MockServer::start().await;
        let (drive, docs, _) = clients(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        document(&server).await;
        let token = seed_lease(
            &crate::drive::lease::ledger::ledger_path().unwrap(),
            "target",
            "1",
        );
        let _audit = crate::test_support::AuditLogGuard::redirect(dir.path());
        std::fs::create_dir(dir.path().join("audit.jsonl")).unwrap();
        let result = crate::request_log::CTX
            .scope(
                crate::request_log::RequestLogContext::mcp("drive_docs_append"),
                run_docs_write(
                    &drive,
                    &docs,
                    "target",
                    docs_write::WritePayload::Append {
                        text: "hello".into(),
                    },
                    false,
                    Some(token),
                    Some("work"),
                ),
            )
            .await
            .unwrap();
        assert_eq!(decoded(&result)["result"]["status"], "failed");
        assert_eq!(result.is_error, Some(true));
        assert!(!server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .any(|r| r.url.path().ends_with(":batchUpdate")));
    }

    /// Formatting outcomes — previews and applied writes alike — are successes,
    /// not refusals, so the MCP result must not carry `is_error`.
    #[test]
    fn docs_formatting_outcomes_are_not_errors() {
        use crate::drive::docs::anchor::{EditPreview, ListPreview};
        use crate::drive::docs::style::{StylePatch, TextStyle};
        use crate::drive::docs::write_types::BulletPreset;

        let edit = EditPreview {
            start_index: 1,
            end_index: 4,
            tab_id: None,
            segment_id: None,
            segment_kind: None,
            paragraphs: 1,
            chars: 3,
            bytes: 3,
        };
        let list = ListPreview {
            start_index: 1,
            end_index: 5,
            tab_id: None,
            paragraphs: 1,
            leading_tabs_removed: 0,
        };
        let style = StylePatch::Text(TextStyle {
            bold: Some(true),
            ..TextStyle::default()
        });
        let preset = Some(BulletPreset::BulletDiscCircleSquare);

        for (result, is_error) in [
            (
                docs_write::WriteResult::WouldFormat {
                    edit: edit.clone(),
                    style: style.clone(),
                    fields: "bold".into(),
                },
                false,
            ),
            (
                docs_write::WriteResult::Formatted {
                    edit,
                    style,
                    fields: "bold".into(),
                },
                false,
            ),
            (
                docs_write::WriteResult::WouldFormatList {
                    edit: list.clone(),
                    preset,
                },
                false,
            ),
            (
                docs_write::WriteResult::ListFormatted { edit: list, preset },
                false,
            ),
            (docs_write::WriteResult::RefusedNoLease, true),
        ] {
            assert_eq!(docs_is_error(&result), is_error, "{result:?}");
        }
    }

    #[test]
    fn expiry_is_bounded_and_acquisition_has_no_policy_override() {
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        configure(dir.path(), true);
        for expiry in [0, -1, 1441, i64::MAX] {
            assert!(acquire_options(&params(
                json!({"file_id":"target","expiry_minutes":expiry,"account":"work"})
            ))
            .is_err());
        }
        for expiry in [1, 30, 1440] {
            let opts = acquire_options(&params(
                json!({"file_id":"target","expiry_minutes":expiry,"account":"work"}),
            ))
            .unwrap();
            assert_eq!(opts.expiry, chrono::Duration::minutes(expiry));
            assert_eq!(
                opts.native_backup_folder_id.as_deref(),
                Some("backup-folder")
            );
            assert!(opts.supersedes.is_none());
        }
        assert!(serde_json::from_value::<DriveLeaseAcquireParams>(
            json!({"file_id":"target","allow_headless":true})
        )
        .is_err());
    }
    #[test]
    fn file_inputs_refuse_oversize_before_reading() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("large.txt");
        std::fs::File::create(&path)
            .unwrap()
            .set_len(crate::drive::files_api::MAX_UPLOAD_BYTES + 1)
            .unwrap();
        assert!(
            append_text(&params(json!({"document_id":"target","text_path":path})))
                .unwrap_err()
                .to_string()
                .contains("cap")
        );
        assert!(sheets_values(&params(
            json!({"spreadsheet_id":"target","values_path":path})
        ))
        .unwrap_err()
        .to_string()
        .contains("cap"));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
    async fn authentication_wait_does_not_stall_other_mcp_work() {
        struct WaitingAuthenticator {
            receive: std::sync::Mutex<std::sync::mpsc::Receiver<()>>,
            started: std::sync::Arc<tokio::sync::Notify>,
        }
        impl authenticate::Authenticator for WaitingAuthenticator {
            fn authenticate(
                &self,
                _reason: &str,
                _policy: authenticate::AuthPolicy,
            ) -> AuthOutcome {
                self.started.notify_one();
                self.receive
                    .lock()
                    .unwrap()
                    .recv_timeout(std::time::Duration::from_secs(2))
                    .expect("runtime must keep other tasks alive during authentication");
                AuthOutcome::Denied("test consent declined".into())
            }
        }
        let guard = EnvGuard::take();
        let dir = guard.clear_credentials();
        let server = MockServer::start().await;
        let drive = client_with_bootstrapped_token(&server).await;
        file(&server, crate::drive::types::GOOGLE_DOC_MIME_TYPE).await;
        let opts = AcquireOptions {
            file_id: "target".into(),
            backup_dir: dir.path().join("backups"),
            native_backup_folder_id: Some("backup-folder".into()),
            expiry: chrono::Duration::minutes(30),
            auth_policy: authenticate::AuthPolicy::DeviceOwner,
            ledger_path: dir.path().join("ledger.jsonl"),
            allow_headless: false,
            supersedes: None,
        };
        let (send, receive) = std::sync::mpsc::channel();
        let started = std::sync::Arc::new(tokio::sync::Notify::new());
        let heartbeat_started = started.clone();
        let acquire = tokio::spawn(async move {
            run_lease_acquire(
                &drive,
                &opts,
                &WaitingAuthenticator {
                    receive: std::sync::Mutex::new(receive),
                    started,
                },
            )
            .await
        });
        let heartbeat = tokio::spawn(async move {
            heartbeat_started.notified().await;
            tokio::time::sleep(std::time::Duration::from_millis(30)).await;
            send.send(()).unwrap();
        });
        let result = acquire.await.unwrap().unwrap();
        heartbeat.await.unwrap();
        assert_eq!(decoded(&result)["status"], "denied");
    }
}