ag-forge 0.19.2

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
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
//! GitLab review-request adapter routed through the `glab` CLI.

use std::sync::Arc;

use serde::Deserialize;
use url::{Url, form_urlencoded};

use super::{
    CreateReviewRequestInput, ForgeCommand, ForgeCommandRunner, ForgeFuture, ForgeKind,
    ForgeRemote, ReviewCheckFailure, ReviewComment, ReviewCommentAnchorSide, ReviewCommentSnapshot,
    ReviewCommentThread, ReviewRequestAdapter, ReviewRequestAttention, ReviewRequestError,
    ReviewRequestMetadata, ReviewRequestMetadataEdit, ReviewRequestOperations, ReviewRequestState,
    ReviewRequestSummary, SyncReviewRequestMetadataConfig, UpdateReviewRequestInput,
    is_gitlab_host, map_parse_error, normalize_provider_label, parse_remote_url,
    status_summary_parts, strip_port,
};

/// Lowest GitLab project access level trusted to request changes: Developer.
const TRUSTED_ACCESS_LEVEL: u32 = 30;
/// Outcome label reported for failed pipelines and jobs.
const FAILED_CHECK_OUTCOME: &str = "Failed";

/// GitLab merge-request adapter that normalizes `glab` command output.
#[derive(Clone)]
pub(crate) struct GitLabReviewRequestAdapter {
    operations: ReviewRequestOperations,
}

impl GitLabReviewRequestAdapter {
    /// Builds one GitLab adapter from a forge command runner.
    pub(crate) fn new(command_runner: Arc<dyn ForgeCommandRunner>) -> Self {
        Self {
            operations: ReviewRequestOperations::new(command_runner),
        }
    }

    /// Returns normalized GitLab remote metadata when `repo_url` is supported.
    pub(crate) fn detect_remote(repo_url: &str) -> Option<ForgeRemote> {
        let parsed_remote = parse_remote_url(repo_url)?;
        if !is_gitlab_host(strip_port(&parsed_remote.host)) {
            return None;
        }

        Some(parsed_remote.into_forge_remote(ForgeKind::GitLab))
    }

    /// Builds the `glab auth status` command for one GitLab host.
    fn auth_status_command(remote: &ForgeRemote) -> ForgeCommand {
        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "auth".to_string(),
                "status".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
            ],
        )
    }

    /// Builds one base `glab` command with deterministic color settings and the
    /// optional session worktree for repository-aware host detection.
    fn gitlab_command(
        remote: &ForgeRemote,
        executable: &'static str,
        arguments: Vec<String>,
    ) -> ForgeCommand {
        ForgeCommand::new(executable, arguments)
            .with_environment("CLICOLOR", "0")
            .with_environment("NO_COLOR", "1")
            .with_environment("GITLAB_HOST", remote.host.clone())
            .with_optional_working_directory(remote.command_working_directory.clone())
    }

    /// Builds the `glab mr list` command for open merge requests matching
    /// `source_branch`.
    fn lookup_command(remote: &ForgeRemote, source_branch: &str) -> ForgeCommand {
        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "mr".to_string(),
                "list".to_string(),
                "--repo".to_string(),
                remote.web_url.clone(),
                "--source-branch".to_string(),
                source_branch.to_string(),
                "--order".to_string(),
                "created_at".to_string(),
                "--sort".to_string(),
                "desc".to_string(),
                "--per-page".to_string(),
                "1".to_string(),
                "--output".to_string(),
                "json".to_string(),
            ],
        )
    }

    /// Parses one optional display id from a GitLab merge-request lookup
    /// response.
    fn parse_lookup_display_id(stdout: &str) -> Result<Option<String>, String> {
        let merge_requests: Vec<GitLabLookupResponse> = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab merge-request lookup response: {error}"))?;

        Ok(merge_requests
            .first()
            .map(|merge_request| format!("!{}", merge_request.iid)))
    }

    /// Builds the `glab mr create` command for `input`.
    ///
    /// GitLab merge requests default to draft so session-published review
    /// requests do not appear ready for merge before the user chooses to
    /// mark them ready.
    fn create_command(remote: &ForgeRemote, input: &CreateReviewRequestInput) -> ForgeCommand {
        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "mr".to_string(),
                "create".to_string(),
                "--repo".to_string(),
                remote.web_url.clone(),
                "--draft".to_string(),
                "--source-branch".to_string(),
                input.source_branch.clone(),
                "--target-branch".to_string(),
                input.target_branch.clone(),
                "--title".to_string(),
                input.title.clone(),
                "--description".to_string(),
                input.body.clone().unwrap_or_default(),
                "--yes".to_string(),
            ],
        )
    }

    /// Parses one merge-request display id from `glab mr create` stdout.
    fn parse_create_display_id(stdout: &str) -> Result<String, String> {
        let created_url = stdout
            .lines()
            .map(str::trim)
            .find(|line| !line.is_empty())
            .ok_or_else(|| "missing GitLab merge-request URL in create response".to_string())?;
        let created_url = Url::parse(created_url).map_err(|error| {
            format!("invalid GitLab merge-request create response URL: {error}")
        })?;
        let path_segments = created_url
            .path_segments()
            .ok_or_else(|| "invalid GitLab merge-request create response URL path".to_string())?
            .collect::<Vec<_>>();
        let merge_request_index = path_segments
            .iter()
            .rposition(|segment| *segment == "merge_requests")
            .ok_or_else(|| {
                "missing merge request path segment in create response URL".to_string()
            })?;
        let merge_request_iid = path_segments
            .get(merge_request_index + 1)
            .ok_or_else(|| "missing merge request iid in create response URL".to_string())?;
        let display_id = format!("!{merge_request_iid}");
        Self::parse_display_id_value(&display_id)?;

        Ok(display_id)
    }

    /// Validates one GitLab merge-request display id and returns its numeric
    /// value.
    fn parse_display_id_value(display_id: &str) -> Result<String, String> {
        let trimmed = display_id.trim().trim_start_matches('!');
        if trimmed.is_empty() || !trimmed.chars().all(|character| character.is_ascii_digit()) {
            return Err(format!(
                "invalid GitLab merge-request display id: `{display_id}`"
            ));
        }

        Ok(trimmed.to_string())
    }

    /// Parses one GitLab merge-request display id into the numeric argument for
    /// `glab`.
    fn parse_display_id(display_id: &str) -> Result<String, ReviewRequestError> {
        Self::parse_display_id_value(display_id).map_err(|message| {
            ReviewRequestError::OperationFailed {
                forge_kind: ForgeKind::GitLab,
                message,
            }
        })
    }

    /// Builds the `glab mr view` command for one merge-request IID.
    fn view_command(remote: &ForgeRemote, merge_request_iid: &str) -> ForgeCommand {
        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "mr".to_string(),
                "view".to_string(),
                merge_request_iid.to_string(),
                "--repo".to_string(),
                remote.web_url.clone(),
                "--output".to_string(),
                "json".to_string(),
            ],
        )
    }

    /// Parses one merge-request summary from a `glab mr view --output json`
    /// response.
    fn parse_view_response(stdout: &str) -> Result<ReviewRequestSummary, String> {
        let merge_request: GitLabViewResponse = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab merge-request view response: {error}"))?;
        let state = merge_request.review_request_state();
        let status_summary = merge_request.status_summary();

        Ok(ReviewRequestSummary {
            display_id: format!("!{}", merge_request.iid),
            forge_kind: ForgeKind::GitLab,
            source_branch: merge_request.source_branch,
            state,
            status_summary,
            target_branch: merge_request.target_branch,
            title: merge_request.title,
            web_url: merge_request.web_url,
        })
    }

    /// Parses head-commit, conflict, and head-pipeline facts from a `glab mr
    /// view --output json` response.
    fn parse_attention_response(stdout: &str) -> Result<GitLabAttentionResponse, String> {
        let merge_request: GitLabAttentionResponse = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab merge-request attention response: {error}"))?;
        if merge_request.sha.is_empty() {
            return Err(
                "GitLab merge-request attention response is missing a head commit".to_string(),
            );
        }

        Ok(merge_request)
    }

    /// Builds the `glab api` request for every failed job of one pipeline.
    ///
    /// `--paginate` merges all pages into one JSON array, so a pipeline with
    /// more failed jobs than one page still reports each of them.
    fn failed_pipeline_jobs_command(remote: &ForgeRemote, pipeline_id: u64) -> ForgeCommand {
        let encoded_project_path = Self::encoded_project_path(remote);

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "--paginate".to_string(),
                format!(
                    "/projects/{encoded_project_path}/pipelines/{pipeline_id}/jobs?scope[]=failed&\
                     per_page=100"
                ),
            ],
        )
    }

    /// Returns the URL-encoded `<namespace>/<project>` path used by API
    /// endpoints.
    fn encoded_project_path(remote: &ForgeRemote) -> String {
        form_urlencoded::byte_serialize(remote.project_path().as_bytes()).collect()
    }

    /// Parses the failed jobs that block pipeline `pipeline_id`. Jobs allowed
    /// to fail are omitted. Each job is rerun by retrying its pipeline.
    fn parse_failed_pipeline_jobs_response(
        stdout: &str,
        pipeline_id: u64,
    ) -> Result<Vec<ReviewCheckFailure>, String> {
        let jobs: Vec<GitLabPipelineJob> = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab pipeline jobs response: {error}"))?;

        Ok(jobs
            .into_iter()
            .filter(|job| !job.allow_failure)
            .map(|job| ReviewCheckFailure {
                name: job.name,
                outcome: FAILED_CHECK_OUTCOME.to_string(),
                rerun_id: Some(pipeline_id.to_string()),
                web_url: job.web_url,
            })
            .collect())
    }

    /// Builds the `glab api` lookup for the project membership of `user_id`,
    /// including inherited and invited members.
    ///
    /// Filtering by user id matches exactly, so the response never depends on
    /// how many other members share a similar name.
    fn project_member_command(remote: &ForgeRemote, user_id: u64) -> ForgeCommand {
        let encoded_project_path = Self::encoded_project_path(remote);

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                format!("/projects/{encoded_project_path}/members/all?user_ids[]={user_id}"),
            ],
        )
    }

    /// Returns whether the member lookup lists `user_id` with at least
    /// Developer access. Entries for any other user are ignored.
    fn parse_project_member_response(stdout: &str, user_id: u64) -> Result<bool, String> {
        let members: Vec<GitLabProjectMember> = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab project members response: {error}"))?;

        Ok(members
            .iter()
            .any(|member| member.id == user_id && member.access_level >= TRUSTED_ACCESS_LEVEL))
    }

    /// Parses the unresolved inline discussions still awaiting a response,
    /// keeping their notes for author-trust evaluation.
    fn parse_attention_candidates(
        stdout: &str,
        current_user_id: u64,
    ) -> Result<Vec<GitLabAttentionCandidate>, String> {
        let discussions: Vec<GitLabDiscussion> = serde_json::from_str(stdout).map_err(|error| {
            format!("invalid GitLab merge-request discussions response: {error}")
        })?;

        Ok(discussions
            .into_iter()
            .filter_map(|discussion| {
                let notes = discussion
                    .notes
                    .into_iter()
                    .filter(|note| !note.system)
                    .collect::<Vec<_>>();
                let thread = Self::review_comment_thread_from_discussion(
                    &discussion.id,
                    &notes,
                    current_user_id,
                )?;

                thread
                    .is_actionable()
                    .then_some(GitLabAttentionCandidate { notes, thread })
            })
            .collect())
    }

    /// Returns the distinct user ids, in first-seen order, of candidate note
    /// authors other than the authenticated user.
    fn candidate_author_ids(
        candidates: &[GitLabAttentionCandidate],
        current_user_id: u64,
    ) -> Vec<u64> {
        let mut author_ids = Vec::new();
        let note_author_ids = candidates
            .iter()
            .flat_map(|candidate| &candidate.notes)
            .filter_map(|note| note.author.id);
        for author_id in note_author_ids {
            if author_id != current_user_id && !author_ids.contains(&author_id) {
                author_ids.push(author_id);
            }
        }

        author_ids
    }

    /// Loads the authenticated user used to identify Agentty replies.
    async fn fetch_current_user(
        operations: &ReviewRequestOperations,
        remote: &ForgeRemote,
    ) -> Result<GitLabCurrentUser, ReviewRequestError> {
        let current_user_output = operations
            .run_review_command(
                remote,
                Self::current_user_command(remote),
                "fetch authenticated GitLab user",
            )
            .await?;

        map_parse_error(
            ForgeKind::GitLab,
            Self::parse_current_user_response(&current_user_output.stdout),
        )
    }

    /// Loads the failures that block one head pipeline.
    ///
    /// A failed pipeline reports its failed jobs, or itself when every failed
    /// job is allowed to fail or no job is listed.
    async fn fetch_failed_checks(
        operations: &ReviewRequestOperations,
        remote: &ForgeRemote,
        pipeline: &GitLabPipeline,
    ) -> Result<Vec<ReviewCheckFailure>, ReviewRequestError> {
        if pipeline.status != "failed" {
            return Ok(Vec::new());
        }

        let jobs_output = operations
            .run_review_command(
                remote,
                Self::failed_pipeline_jobs_command(remote, pipeline.id),
                "fetch failed pipeline jobs",
            )
            .await?;
        let mut failed_checks = map_parse_error(
            ForgeKind::GitLab,
            Self::parse_failed_pipeline_jobs_response(&jobs_output.stdout, pipeline.id),
        )?;
        if failed_checks.is_empty() {
            failed_checks.push(ReviewCheckFailure {
                name: format!("pipeline #{}", pipeline.id),
                outcome: FAILED_CHECK_OUTCOME.to_string(),
                rerun_id: Some(pipeline.id.to_string()),
                web_url: pipeline.web_url.clone(),
            });
        }

        Ok(failed_checks)
    }

    /// Loads the inline discussions awaiting a response from trusted authors,
    /// looking up each distinct note author's project membership once.
    /// Authors whose username is listed in `trusted_bots` are trusted too.
    async fn fetch_trusted_review_threads(
        operations: &ReviewRequestOperations,
        remote: &ForgeRemote,
        merge_request_iid: &str,
        trusted_bots: &[String],
    ) -> Result<Vec<ReviewCommentThread>, ReviewRequestError> {
        let current_user = Self::fetch_current_user(operations, remote).await?;
        let discussions_output = operations
            .run_review_command(
                remote,
                Self::discussions_command(remote, merge_request_iid),
                "fetch merge-request discussions",
            )
            .await?;
        let candidates = map_parse_error(
            ForgeKind::GitLab,
            Self::parse_attention_candidates(&discussions_output.stdout, current_user.id),
        )?;
        let mut trusted_author_ids = Vec::new();
        for author_id in Self::candidate_author_ids(&candidates, current_user.id) {
            let member_output = operations
                .run_review_command(
                    remote,
                    Self::project_member_command(remote, author_id),
                    "look up merge-request comment author",
                )
                .await?;
            if map_parse_error(
                ForgeKind::GitLab,
                Self::parse_project_member_response(&member_output.stdout, author_id),
            )? {
                trusted_author_ids.push(author_id);
            }
        }

        Ok(candidates
            .into_iter()
            .filter_map(|candidate| {
                candidate.into_trusted_thread(current_user.id, &trusted_author_ids, trusted_bots)
            })
            .collect())
    }

    /// Builds GitLab-specific metadata view and edit configuration.
    fn metadata_sync_config() -> SyncReviewRequestMetadataConfig {
        SyncReviewRequestMetadataConfig {
            edit_metadata_command: Self::update_metadata_command,
            edit_operation: "update merge-request metadata",
            parse_display_id: Self::parse_display_id,
            parse_metadata_response: Self::parse_metadata_response,
            view_metadata_command: Self::view_command,
            view_operation: "view merge-request metadata",
        }
    }

    /// Builds the `glab mr update` command for updating one merge-request
    /// title/description.
    fn update_metadata_command(
        remote: &ForgeRemote,
        merge_request_iid: &str,
        edit: &ReviewRequestMetadataEdit,
    ) -> ForgeCommand {
        let mut arguments = vec![
            "mr".to_string(),
            "update".to_string(),
            merge_request_iid.to_string(),
            "--repo".to_string(),
            remote.web_url.clone(),
        ];
        if let Some(title) = edit.title.as_ref() {
            arguments.extend(["--title".to_string(), title.clone()]);
        }
        if let Some(body) = edit.body.as_ref() {
            arguments.extend(["--description".to_string(), body.clone()]);
        }
        arguments.push("--yes".to_string());

        Self::gitlab_command(remote, "glab", arguments)
    }

    /// Parses current merge-request title/description metadata from `glab mr
    /// view` JSON.
    fn parse_metadata_response(stdout: &str) -> Result<ReviewRequestMetadata, String> {
        let metadata: GitLabMetadataResponse = serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab merge-request metadata response: {error}"))?;

        Ok(ReviewRequestMetadata {
            body: metadata.description,
            title: metadata.title,
        })
    }

    /// Builds the GitLab API request for the authenticated user identity.
    fn current_user_command(remote: &ForgeRemote) -> ForgeCommand {
        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "/user".to_string(),
            ],
        )
    }

    /// Parses the authenticated GitLab user required to identify Agentty
    /// replies.
    fn parse_current_user_response(stdout: &str) -> Result<GitLabCurrentUser, String> {
        serde_json::from_str(stdout)
            .map_err(|error| format!("invalid GitLab current-user response: {error}"))
    }

    /// Builds the `glab api` command for merge-request discussions.
    fn discussions_command(remote: &ForgeRemote, merge_request_iid: &str) -> ForgeCommand {
        let encoded_project_path = Self::encoded_project_path(remote);
        let endpoint = format!(
            "/projects/{encoded_project_path}/merge_requests/{merge_request_iid}/discussions?\
             per_page=100"
        );

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "--paginate".to_string(),
                endpoint,
            ],
        )
    }

    /// Parses merge-request discussions into inline threads and MR-level
    /// comments.
    fn parse_review_comment_snapshot_response(
        stdout: &str,
        current_user_id: u64,
    ) -> Result<ReviewCommentSnapshot, String> {
        let discussions: Vec<GitLabDiscussion> = serde_json::from_str(stdout).map_err(|error| {
            format!("invalid GitLab merge-request discussions response: {error}")
        })?;
        let mut pr_level_comments = Vec::new();
        let mut threads = Vec::new();

        for discussion in discussions {
            let mut notes = discussion
                .notes
                .into_iter()
                .filter(|note| !note.system)
                .collect::<Vec<_>>();
            if notes.is_empty() {
                continue;
            }

            if let Some(thread) =
                Self::review_comment_thread_from_discussion(&discussion.id, &notes, current_user_id)
            {
                threads.push(thread);
            } else {
                pr_level_comments.extend(
                    notes
                        .drain(..)
                        .map(|note| Self::review_comment_from_note(note, current_user_id)),
                );
            }
        }

        Ok(ReviewCommentSnapshot {
            pr_level_comments,
            threads,
        })
    }

    /// Converts one GitLab discussion into an inline thread when it carries a
    /// diff note position.
    fn review_comment_thread_from_discussion(
        discussion_id: &str,
        notes: &[GitLabDiscussionNote],
        current_user_id: u64,
    ) -> Option<ReviewCommentThread> {
        let anchor_note = notes.iter().find(|note| {
            note.note_type.as_deref() == Some("DiffNote") && note.position.as_ref().is_some()
        })?;
        let position = anchor_note.position.as_ref()?;
        let (anchor_side, path, line) = Self::gitlab_anchor_from_position(position);

        Some(ReviewCommentThread {
            anchor_side,
            comments: notes
                .iter()
                .cloned()
                .map(|note| Self::review_comment_from_note(note, current_user_id))
                .collect(),
            id: discussion_id.to_string(),
            is_outdated: None,
            is_resolved: anchor_note.resolved,
            line,
            path,
            start_line: None,
        })
    }

    /// Converts one GitLab diff-note position into Agentty's normalized anchor.
    fn gitlab_anchor_from_position(
        position: &GitLabDiscussionPosition,
    ) -> (ReviewCommentAnchorSide, String, Option<u32>) {
        if let Some(new_line) = position.new_line {
            return (
                ReviewCommentAnchorSide::New,
                position
                    .new_path
                    .clone()
                    .or_else(|| position.old_path.clone())
                    .unwrap_or_default(),
                Some(new_line),
            );
        }

        if let Some(old_line) = position.old_line {
            return (
                ReviewCommentAnchorSide::Old,
                position
                    .old_path
                    .clone()
                    .or_else(|| position.new_path.clone())
                    .unwrap_or_default(),
                Some(old_line),
            );
        }

        (
            ReviewCommentAnchorSide::File,
            position
                .new_path
                .clone()
                .or_else(|| position.old_path.clone())
                .unwrap_or_default(),
            None,
        )
    }

    /// Converts one GitLab discussion note into the forge-neutral comment
    /// shape.
    fn review_comment_from_note(note: GitLabDiscussionNote, current_user_id: u64) -> ReviewComment {
        let authored_by_current_user = note.author.id == Some(current_user_id);

        ReviewComment {
            author: note
                .author
                .username
                .or(note.author.name)
                .unwrap_or_default(),
            authored_by_current_user,
            body: note.body,
        }
    }

    /// Builds one `glab api` request that replies to a merge-request
    /// discussion.
    fn reply_to_thread_command(
        remote: &ForgeRemote,
        merge_request_iid: &str,
        thread_id: &str,
        body: &str,
    ) -> ForgeCommand {
        let endpoint =
            Self::discussion_endpoint(remote, merge_request_iid, thread_id, Some("notes"));

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "--method".to_string(),
                "POST".to_string(),
                "--raw-field".to_string(),
                format!("body={body}"),
                endpoint,
            ],
        )
    }

    /// Returns the encoded GitLab discussion endpoint for one optional child
    /// resource.
    fn discussion_endpoint(
        remote: &ForgeRemote,
        merge_request_iid: &str,
        thread_id: &str,
        child_resource: Option<&str>,
    ) -> String {
        let encoded_project_path = Self::encoded_project_path(remote);
        let encoded_thread_id: String =
            form_urlencoded::byte_serialize(thread_id.as_bytes()).collect();
        let mut endpoint = format!(
            "/projects/{encoded_project_path}/merge_requests/{merge_request_iid}/discussions/\
             {encoded_thread_id}"
        );
        if let Some(child_resource) = child_resource {
            endpoint.push('/');
            endpoint.push_str(child_resource);
        }

        endpoint
    }

    /// Builds one `glab api` request that resolves a merge-request discussion.
    fn resolve_thread_command(
        remote: &ForgeRemote,
        merge_request_iid: &str,
        thread_id: &str,
    ) -> ForgeCommand {
        let endpoint = Self::discussion_endpoint(remote, merge_request_iid, thread_id, None);

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "--method".to_string(),
                "PUT".to_string(),
                "--field".to_string(),
                "resolved=true".to_string(),
                endpoint,
            ],
        )
    }

    /// Builds one `glab api` request that retries the failed and canceled
    /// jobs of one pipeline.
    fn retry_pipeline_command(remote: &ForgeRemote, pipeline_id: &str) -> ForgeCommand {
        let encoded_project_path = Self::encoded_project_path(remote);

        Self::gitlab_command(
            remote,
            "glab",
            vec![
                "api".to_string(),
                "--hostname".to_string(),
                remote.host.clone(),
                "--method".to_string(),
                "POST".to_string(),
                format!("/projects/{encoded_project_path}/pipelines/{pipeline_id}/retry"),
            ],
        )
    }
}

impl ReviewRequestAdapter for GitLabReviewRequestAdapter {
    fn ensure_authenticated(
        &self,
        remote: &ForgeRemote,
    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
        self.operations
            .ensure_authenticated_future(remote.clone(), Self::auth_status_command)
    }

    /// Finds one existing merge request for `source_branch`.
    fn find_authenticated_by_source_branch(
        &self,
        remote: ForgeRemote,
        source_branch: String,
    ) -> ForgeFuture<Result<Option<ReviewRequestSummary>, ReviewRequestError>> {
        let adapter = self.clone();

        self.operations.find_by_source_branch_future(
            remote,
            source_branch,
            Self::lookup_command,
            "find merge request",
            Self::parse_lookup_display_id,
            move |remote, display_id| {
                adapter.refresh_authenticated_review_request(remote, display_id)
            },
        )
    }

    /// Creates one new draft merge request from `input`.
    fn create_authenticated_review_request(
        &self,
        remote: ForgeRemote,
        input: CreateReviewRequestInput,
    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
        let adapter = self.clone();
        let operations = self.operations.clone();

        Box::pin(async move {
            let create_command = Self::create_command(&remote, &input);
            let output = operations
                .run_review_command(&remote, create_command, "create merge request")
                .await?;
            let display_id = map_parse_error(
                remote.forge_kind,
                Self::parse_create_display_id(&output.stdout),
            )?;

            adapter
                .refresh_authenticated_review_request(remote, display_id)
                .await
        })
    }

    /// Refreshes one existing merge request by display id.
    fn refresh_authenticated_review_request(
        &self,
        remote: ForgeRemote,
        display_id: String,
    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
        self.operations.refresh_review_request_future(
            remote,
            display_id,
            Self::parse_display_id,
            Self::view_command,
            "refresh merge request",
            Self::parse_view_response,
        )
    }

    /// Loads current merge-request title/description metadata.
    fn authenticated_review_request_metadata(
        &self,
        remote: ForgeRemote,
        display_id: String,
    ) -> ForgeFuture<Result<ReviewRequestMetadata, ReviewRequestError>> {
        self.operations.review_request_metadata_future(
            remote,
            display_id,
            &Self::metadata_sync_config(),
        )
    }

    /// Checks current merge-request metadata and updates fields that still
    /// match the reconciled input.
    fn sync_authenticated_review_request_metadata(
        &self,
        remote: ForgeRemote,
        display_id: String,
        input: UpdateReviewRequestInput,
    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
        let adapter = self.clone();

        self.operations.sync_review_request_metadata_future(
            remote,
            display_id,
            input,
            &Self::metadata_sync_config(),
            move |remote, display_id| {
                adapter.refresh_authenticated_review_request(remote, display_id)
            },
        )
    }

    /// Fetches merge-request discussions through GitLab's REST API and
    /// normalizes diff notes plus review-request-wide notes for session
    /// review-comment views.
    fn fetch_authenticated_review_comment_snapshot(
        &self,
        remote: ForgeRemote,
        display_id: String,
    ) -> ForgeFuture<Result<ReviewCommentSnapshot, ReviewRequestError>> {
        let operations = self.operations.clone();

        Box::pin(async move {
            let merge_request_iid = Self::parse_display_id(&display_id)?;
            let current_user = Self::fetch_current_user(&operations, &remote).await?;
            let discussions_output = operations
                .run_review_command(
                    &remote,
                    Self::discussions_command(&remote, &merge_request_iid),
                    "fetch merge-request discussions",
                )
                .await?;

            map_parse_error(
                ForgeKind::GitLab,
                Self::parse_review_comment_snapshot_response(
                    &discussions_output.stdout,
                    current_user.id,
                ),
            )
        })
    }

    /// Fetches head-commit, conflict, and head-pipeline facts from the merge
    /// request, the failed jobs of a failed pipeline, and the discussions
    /// awaiting a response from trusted project members and trusted bots.
    /// Pipeline jobs have no reporting bot, so every failed job is reported.
    fn fetch_authenticated_review_request_attention(
        &self,
        remote: ForgeRemote,
        display_id: String,
        trusted_bots: Vec<String>,
    ) -> ForgeFuture<Result<ReviewRequestAttention, ReviewRequestError>> {
        let operations = self.operations.clone();

        Box::pin(async move {
            let merge_request_iid = Self::parse_display_id(&display_id)?;
            let view_output = operations
                .run_review_command(
                    &remote,
                    Self::view_command(&remote, &merge_request_iid),
                    "fetch merge-request attention",
                )
                .await?;
            let merge_request = map_parse_error(
                ForgeKind::GitLab,
                Self::parse_attention_response(&view_output.stdout),
            )?;
            let mut failed_checks = Vec::new();
            let mut has_pending_checks = false;
            if let Some(pipeline) = merge_request.head_pipeline.as_ref() {
                failed_checks = Self::fetch_failed_checks(&operations, &remote, pipeline).await?;
                has_pending_checks = pipeline.is_pending();
            }
            let review_threads = Self::fetch_trusted_review_threads(
                &operations,
                &remote,
                &merge_request_iid,
                &trusted_bots,
            )
            .await?;

            Ok(ReviewRequestAttention {
                failed_checks,
                has_merge_conflict: merge_request.has_conflicts,
                has_pending_checks,
                head_commit: merge_request.sha,
                review_threads,
            })
        })
    }

    /// Retries the failed jobs of each pipeline.
    fn rerun_authenticated_failed_checks(
        &self,
        remote: ForgeRemote,
        rerun_ids: Vec<String>,
    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
        self.operations.rerun_failed_checks_future(
            remote,
            rerun_ids,
            Self::retry_pipeline_command,
            "retry failed pipeline jobs",
        )
    }

    fn reply_to_authenticated_thread(
        &self,
        remote: ForgeRemote,
        display_id: String,
        thread_id: String,
        body: String,
    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
        let operations = self.operations.clone();

        Box::pin(async move {
            let merge_request_iid = Self::parse_display_id(&display_id)?;
            operations
                .run_review_command(
                    &remote,
                    Self::reply_to_thread_command(&remote, &merge_request_iid, &thread_id, &body),
                    "reply to merge-request discussion",
                )
                .await?;

            Ok(())
        })
    }

    fn resolve_authenticated_thread(
        &self,
        remote: ForgeRemote,
        display_id: String,
        thread_id: String,
    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
        let operations = self.operations.clone();

        Box::pin(async move {
            let merge_request_iid = Self::parse_display_id(&display_id)?;
            operations
                .run_review_command(
                    &remote,
                    Self::resolve_thread_command(&remote, &merge_request_iid, &thread_id),
                    "resolve merge-request discussion",
                )
                .await?;

            Ok(())
        })
    }
}

/// Minimal GitLab list payload used to find an existing merge request.
#[derive(Deserialize)]
struct GitLabLookupResponse {
    iid: u64,
}

/// GitLab merge-request JSON payload returned by `glab mr view --output json`.
#[derive(Deserialize)]
struct GitLabViewResponse {
    #[serde(rename = "detailed_merge_status")]
    detailed_merge_status: Option<String>,
    #[serde(default)]
    draft: bool,
    iid: u64,
    #[serde(rename = "merge_status")]
    merge_status: Option<String>,
    #[serde(rename = "merged_at")]
    merged_at: Option<String>,
    #[serde(rename = "source_branch")]
    source_branch: String,
    state: String,
    #[serde(rename = "target_branch")]
    target_branch: String,
    title: String,
    #[serde(rename = "web_url")]
    web_url: String,
}

impl GitLabViewResponse {
    /// Maps GitLab state fields into the normalized review-request state.
    fn review_request_state(&self) -> ReviewRequestState {
        if self.merged_at.is_some() || self.state.eq_ignore_ascii_case("merged") {
            return ReviewRequestState::Merged;
        }

        if matches!(self.state.as_str(), "closed" | "locked") {
            return ReviewRequestState::Closed;
        }

        ReviewRequestState::Open
    }

    /// Formats the provider-specific status summary for the UI.
    fn status_summary(&self) -> Option<String> {
        let mut parts = Vec::new();
        if self.draft {
            parts.push("Draft".to_string());
        }

        if let Some(merge_summary) = Self::merge_status_summary(
            self.merge_status.as_deref(),
            self.detailed_merge_status.as_deref(),
        ) {
            parts.push(merge_summary);
        }

        status_summary_parts(&parts)
    }

    /// Formats one GitLab merge-status label for the UI.
    fn merge_status_summary(
        merge_status: Option<&str>,
        detailed_merge_status: Option<&str>,
    ) -> Option<String> {
        let status = detailed_merge_status.or(merge_status)?;

        match status {
            "can_be_merged" | "mergeable" => Some("Mergeable".to_string()),
            "cannot_be_merged" => Some("Conflicts".to_string()),
            "cannot_be_merged_recheck" | "checking" | "unchecked" => Some("Checking".to_string()),
            "ci_still_running" | "commits_status" => Some("Checks pending".to_string()),
            "ci_must_pass" => Some("Checks required".to_string()),
            "discussions_not_resolved" => Some("Discussions unresolved".to_string()),
            "draft_status" | "not_open" => None,
            other => Some(normalize_provider_label(other)),
        }
    }
}

/// Head-commit, conflict, and head-pipeline facts from the merge-request JSON
/// returned by `glab mr view --output json`.
#[derive(Deserialize)]
struct GitLabAttentionResponse {
    #[serde(default)]
    has_conflicts: bool,
    head_pipeline: Option<GitLabPipeline>,
    #[serde(default)]
    sha: String,
}

/// Pipeline running on the head commit of a merge-request source branch.
#[derive(Deserialize)]
struct GitLabPipeline {
    id: u64,
    status: String,
    web_url: Option<String>,
}

impl GitLabPipeline {
    /// Returns whether the pipeline has not reached a final status yet.
    fn is_pending(&self) -> bool {
        matches!(
            self.status.as_str(),
            "created"
                | "waiting_for_resource"
                | "preparing"
                | "waiting_for_callback"
                | "pending"
                | "running"
                | "scheduled"
        )
    }
}

/// One job returned by the pipeline jobs API.
#[derive(Deserialize)]
struct GitLabPipelineJob {
    #[serde(default)]
    allow_failure: bool,
    name: String,
    web_url: Option<String>,
}

/// One project member returned by the members API.
#[derive(Deserialize)]
struct GitLabProjectMember {
    access_level: u32,
    id: u64,
}

/// One unresolved inline discussion awaiting a response, paired with the
/// notes its normalized thread was built from.
struct GitLabAttentionCandidate {
    notes: Vec<GitLabDiscussionNote>,
    thread: ReviewCommentThread,
}

impl GitLabAttentionCandidate {
    /// Returns the thread with only the comments of the authenticated user,
    /// trusted members, and trusted bots when the latest of them still awaits
    /// a response.
    ///
    /// Untrusted notes are dropped before the thread is evaluated, so they
    /// can neither request work nor hide a trusted request.
    fn into_trusted_thread(
        self,
        current_user_id: u64,
        trusted_author_ids: &[u64],
        trusted_bots: &[String],
    ) -> Option<ReviewCommentThread> {
        let trusted_notes = self
            .notes
            .iter()
            .map(|note| {
                note.author.id.is_some_and(|author_id| {
                    author_id == current_user_id || trusted_author_ids.contains(&author_id)
                }) || note
                    .author
                    .username
                    .as_ref()
                    .is_some_and(|username| trusted_bots.contains(username))
            })
            .collect::<Vec<_>>();
        let mut thread = self.thread;
        thread.comments = std::mem::take(&mut thread.comments)
            .into_iter()
            .zip(trusted_notes)
            .filter_map(|(comment, is_trusted)| is_trusted.then_some(comment))
            .collect();

        (!thread.comments.is_empty() && thread.is_actionable()).then_some(thread)
    }
}

/// GitLab merge-request title/description payload returned by
/// `glab mr view --output json`.
#[derive(Deserialize)]
struct GitLabMetadataResponse {
    #[serde(default)]
    description: String,
    title: String,
}

/// Authenticated GitLab user returned by `GET /user`.
#[derive(Deserialize)]
struct GitLabCurrentUser {
    id: u64,
}

/// GitLab merge-request discussion returned by the discussions API.
#[derive(Clone, Deserialize)]
struct GitLabDiscussion {
    id: String,
    notes: Vec<GitLabDiscussionNote>,
}

/// One GitLab discussion note, optionally carrying a diff position.
#[derive(Clone, Deserialize)]
struct GitLabDiscussionNote {
    author: GitLabDiscussionAuthor,
    body: String,
    #[serde(rename = "type")]
    note_type: Option<String>,
    position: Option<GitLabDiscussionPosition>,
    #[serde(default)]
    resolved: bool,
    #[serde(default)]
    system: bool,
}

/// Minimal GitLab note author data shown in session review-comment views.
#[derive(Clone, Deserialize)]
struct GitLabDiscussionAuthor {
    id: Option<u64>,
    name: Option<String>,
    username: Option<String>,
}

/// Minimal GitLab diff position used to anchor inline comments.
#[derive(Clone, Deserialize)]
struct GitLabDiscussionPosition {
    #[serde(rename = "new_line")]
    new_line: Option<u32>,
    #[serde(rename = "new_path")]
    new_path: Option<String>,
    #[serde(rename = "old_line")]
    old_line: Option<u32>,
    #[serde(rename = "old_path")]
    old_path: Option<String>,
}

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