ag-forge 0.15.15

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
//! Shared helpers used by forge review-request adapters.
//!
//! Each supported forge (GitHub, GitLab) needs the same normalization for
//! authentication failures, host-resolution failures, status-summary joining,
//! provider label casing, and spawn-time error mapping. Keeping these in one
//! module avoids divergence between adapters.

use std::sync::Arc;

use super::{
    ForgeCommand, ForgeCommandError, ForgeCommandOutput, ForgeCommandRunner, ForgeFuture,
    ForgeKind, ForgeRemote, ReviewRequestError, ReviewRequestMetadata, ReviewRequestSummary,
    UpdateReviewRequestInput, command_output_detail,
};

/// Provider-neutral partial edit produced after a best-effort recheck that the
/// remote fields still match the values used during semantic reconciliation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ReviewRequestMetadataEdit {
    pub(crate) body: Option<String>,
    pub(crate) title: Option<String>,
}

/// Shared command runner and operation flow used by forge adapters.
#[derive(Clone)]
pub(crate) struct ReviewRequestOperations {
    command_runner: Arc<dyn ForgeCommandRunner>,
}

impl ReviewRequestOperations {
    /// Builds shared review-request operations from one command runner.
    pub(crate) fn new(command_runner: Arc<dyn ForgeCommandRunner>) -> Self {
        Self { command_runner }
    }

    /// Verifies CLI authentication and normalizes common auth failures.
    pub(crate) async fn ensure_authenticated(
        &self,
        remote: &ForgeRemote,
        command: ForgeCommand,
    ) -> Result<(), ReviewRequestError> {
        let output = self
            .command_runner
            .run(command)
            .await
            .map_err(|error| Self::map_spawn_error(remote, error))?;
        if output.success() {
            return Ok(());
        }

        let detail = command_output_detail(&output);
        if Self::looks_like_host_resolution_failure(&detail) {
            return Err(ReviewRequestError::HostResolutionFailed {
                forge_kind: remote.forge_kind,
                host: remote.host.clone(),
            });
        }

        Err(ReviewRequestError::AuthenticationRequired {
            detail: Some(detail),
            forge_kind: remote.forge_kind,
            host: remote.host.clone(),
        })
    }

    /// Builds an authentication future for adapter trait implementations.
    pub(crate) fn ensure_authenticated_future(
        &self,
        remote: ForgeRemote,
        auth_status_command: fn(&ForgeRemote) -> ForgeCommand,
    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
        let operations = self.clone();

        Box::pin(async move {
            operations
                .ensure_authenticated(&remote, auth_status_command(&remote))
                .await
        })
    }

    /// Runs one authenticated forge CLI command and normalizes common
    /// failures.
    pub(crate) async fn run_review_command(
        &self,
        remote: &ForgeRemote,
        command: ForgeCommand,
        operation: &str,
    ) -> Result<ForgeCommandOutput, ReviewRequestError> {
        let output = self
            .command_runner
            .run(command)
            .await
            .map_err(|error| Self::map_spawn_error(remote, error))?;
        if output.success() {
            return Ok(output);
        }

        let detail = command_output_detail(&output);
        if Self::looks_like_host_resolution_failure(&detail) {
            return Err(ReviewRequestError::HostResolutionFailed {
                forge_kind: remote.forge_kind,
                host: remote.host.clone(),
            });
        }

        if Self::looks_like_authentication_failure(&detail, remote.forge_kind) {
            return Err(ReviewRequestError::AuthenticationRequired {
                detail: Some(detail),
                forge_kind: remote.forge_kind,
                host: remote.host.clone(),
            });
        }

        Err(operation_failed(
            remote.forge_kind,
            format!("{operation}: {detail}"),
        ))
    }

    /// Finds one review request by source branch, then builds an owned future
    /// that refreshes its full summary for adapter trait implementations.
    pub(crate) fn find_by_source_branch_future(
        &self,
        remote: ForgeRemote,
        source_branch: String,
        lookup_command: fn(&ForgeRemote, &str) -> ForgeCommand,
        operation: &'static str,
        parse_lookup_display_id: fn(&str) -> Result<Option<String>, String>,
        refresh_review_request: impl FnOnce(
            ForgeRemote,
            String,
        ) -> ForgeFuture<
            Result<ReviewRequestSummary, ReviewRequestError>,
        > + Send
        + 'static,
    ) -> ForgeFuture<Result<Option<ReviewRequestSummary>, ReviewRequestError>> {
        let operations = self.clone();

        Box::pin(async move {
            let lookup_command = lookup_command(&remote, &source_branch);
            let output = operations
                .run_review_command(&remote, lookup_command, operation)
                .await?;
            let display_id =
                map_parse_error(remote.forge_kind, parse_lookup_display_id(&output.stdout))?;

            let Some(display_id) = display_id else {
                return Ok(None);
            };

            refresh_review_request(remote, display_id).await.map(Some)
        })
    }

    /// Refreshes one review request by provider display id in an owned future
    /// for adapter trait implementations.
    pub(crate) fn refresh_review_request_future(
        &self,
        remote: ForgeRemote,
        display_id: String,
        parse_display_id: fn(&str) -> Result<String, ReviewRequestError>,
        view_command: fn(&ForgeRemote, &str) -> ForgeCommand,
        operation: &'static str,
        parse_view_response: fn(&str) -> Result<ReviewRequestSummary, String>,
    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
        let operations = self.clone();

        Box::pin(async move {
            let command_display_id = parse_display_id(&display_id)?;
            let output = operations
                .run_review_command(
                    &remote,
                    view_command(&remote, &command_display_id),
                    operation,
                )
                .await?;

            map_parse_error(remote.forge_kind, parse_view_response(&output.stdout))
        })
    }

    /// Fetches current review-request metadata in an owned future for adapter
    /// trait implementations.
    pub(crate) fn review_request_metadata_future(
        &self,
        remote: ForgeRemote,
        display_id: String,
        config: &SyncReviewRequestMetadataConfig,
    ) -> ForgeFuture<Result<ReviewRequestMetadata, ReviewRequestError>> {
        let parse_display_id = config.parse_display_id;
        let parse_metadata_response = config.parse_metadata_response;
        let view_metadata_command = config.view_metadata_command;
        let view_operation = config.view_operation;
        let operations = self.clone();

        Box::pin(async move {
            let command_display_id = parse_display_id(&display_id)?;
            let output = operations
                .run_review_command(
                    &remote,
                    view_metadata_command(&remote, &command_display_id),
                    view_operation,
                )
                .await?;

            map_parse_error(remote.forge_kind, parse_metadata_response(&output.stdout))
        })
    }

    /// Rechecks reconciled fields immediately before a best-effort metadata
    /// update, then refreshes the full summary in an owned future.
    ///
    /// Provider CLI updates have no atomic version precondition, so a manual
    /// edit made after this recheck can still be overwritten.
    pub(crate) fn sync_review_request_metadata_future(
        &self,
        remote: ForgeRemote,
        display_id: String,
        input: UpdateReviewRequestInput,
        config: &SyncReviewRequestMetadataConfig,
        refresh_review_request: impl FnOnce(
            ForgeRemote,
            String,
        ) -> ForgeFuture<
            Result<ReviewRequestSummary, ReviewRequestError>,
        > + Send
        + 'static,
    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
        let edit_metadata_command = config.edit_metadata_command;
        let edit_operation = config.edit_operation;
        let parse_display_id = config.parse_display_id;
        let parse_metadata_response = config.parse_metadata_response;
        let view_metadata_command = config.view_metadata_command;
        let view_operation = config.view_operation;
        let operations = self.clone();

        Box::pin(async move {
            let command_display_id = parse_display_id(&display_id)?;
            let output = operations
                .run_review_command(
                    &remote,
                    view_metadata_command(&remote, &command_display_id),
                    view_operation,
                )
                .await?;
            let metadata =
                map_parse_error(remote.forge_kind, parse_metadata_response(&output.stdout))?;
            let plan = ReviewRequestMetadataPlan::new(&metadata, &input);

            if plan.requires_edit() {
                operations
                    .run_review_command(
                        &remote,
                        edit_metadata_command(&remote, &command_display_id, &plan.edit),
                        edit_operation,
                    )
                    .await?;
            }

            refresh_review_request(remote, display_id).await
        })
    }

    /// Maps one spawn-time failure into a normalized review-request error for
    /// the forge owning `remote`.
    fn map_spawn_error(remote: &ForgeRemote, error: ForgeCommandError) -> ReviewRequestError {
        let forge_kind = remote.forge_kind;

        match error {
            ForgeCommandError::ExecutableNotFound { .. } => {
                ReviewRequestError::CliNotInstalled { forge_kind }
            }
            ForgeCommandError::SpawnFailed { message, .. } => {
                if Self::looks_like_host_resolution_failure(&message) {
                    return ReviewRequestError::HostResolutionFailed {
                        forge_kind,
                        host: remote.host.clone(),
                    };
                }

                ReviewRequestError::OperationFailed {
                    forge_kind,
                    message: format!("failed to execute `{}`: {message}", forge_kind.cli_name()),
                }
            }
            ForgeCommandError::TimedOut {
                executable,
                timeout,
            } => ReviewRequestError::OperationFailed {
                forge_kind,
                message: format!(
                    "`{executable}` timed out after {} seconds while contacting {}",
                    timeout.as_secs(),
                    remote.host
                ),
            },
        }
    }

    /// Returns whether `detail` looks like a DNS or host-resolution failure.
    fn looks_like_host_resolution_failure(detail: &str) -> bool {
        let normalized_detail = detail.to_ascii_lowercase();

        normalized_detail.contains("no such host")
            || normalized_detail.contains("name or service not known")
            || normalized_detail.contains("temporary failure in name resolution")
            || normalized_detail.contains("could not resolve host")
            || normalized_detail.contains("lookup ")
    }

    /// Returns whether `detail` looks like a forge CLI authentication failure.
    ///
    /// Parameterized on `forge_kind` so the CLI-specific `{cli} auth login`
    /// marker stays accurate across forges while the remaining substrings are
    /// shared.
    fn looks_like_authentication_failure(detail: &str, forge_kind: ForgeKind) -> bool {
        let normalized_detail = detail.to_ascii_lowercase();
        let auth_login_marker = format!("{} auth login", forge_kind.cli_name());

        normalized_detail.contains(&auth_login_marker)
            || normalized_detail.contains("not logged in")
            || normalized_detail.contains("authentication failed")
            || normalized_detail.contains("authentication required")
            || normalized_detail.contains("http 401")
    }
}

/// Configuration for provider-specific metadata synchronization.
pub(crate) struct SyncReviewRequestMetadataConfig {
    /// Builds the edit command when metadata differs from desired input.
    pub(crate) edit_metadata_command:
        fn(&ForgeRemote, &str, &ReviewRequestMetadataEdit) -> ForgeCommand,
    /// User-facing operation prefix for edit failures.
    pub(crate) edit_operation: &'static str,
    /// Parses one provider display id into a CLI argument.
    pub(crate) parse_display_id: fn(&str) -> Result<String, ReviewRequestError>,
    /// Parses provider metadata JSON.
    pub(crate) parse_metadata_response: fn(&str) -> Result<ReviewRequestMetadata, String>,
    /// Builds the metadata view command.
    pub(crate) view_metadata_command: fn(&ForgeRemote, &str) -> ForgeCommand,
    /// User-facing operation prefix for metadata view failures.
    pub(crate) view_operation: &'static str,
}

/// Wraps a provider operation failure with its forge kind.
pub(crate) fn operation_failed(
    forge_kind: ForgeKind,
    message: impl Into<String>,
) -> ReviewRequestError {
    ReviewRequestError::OperationFailed {
        forge_kind,
        message: message.into(),
    }
}

/// Maps provider parser failures into a normalized operation error.
pub(crate) fn map_parse_error<T>(
    forge_kind: ForgeKind,
    result: Result<T, String>,
) -> Result<T, ReviewRequestError> {
    result.map_err(|message| operation_failed(forge_kind, message))
}

/// Joins one ordered list of status-summary parts into a comma-separated
/// label, returning `None` when `parts` is empty.
pub(crate) fn status_summary_parts(parts: &[String]) -> Option<String> {
    if parts.is_empty() {
        return None;
    }

    Some(parts.join(", "))
}

/// Formats one provider enum-like label into sentence case words.
pub(crate) fn normalize_provider_label(label: &str) -> String {
    let lowercase = label.replace('_', " ").to_ascii_lowercase();
    let mut characters = lowercase.chars();
    let Some(first_character) = characters.next() else {
        return String::new();
    };

    let mut normalized = first_character.to_uppercase().collect::<String>();
    normalized.push_str(characters.as_str());

    normalized
}

/// Best-effort conditional edit calculated from the last observed metadata.
struct ReviewRequestMetadataPlan {
    edit: ReviewRequestMetadataEdit,
}

impl ReviewRequestMetadataPlan {
    /// Builds a field-level edit when the last observed remote value matches
    /// the value used by the semantic reconciliation step.
    fn new(metadata: &ReviewRequestMetadata, input: &UpdateReviewRequestInput) -> Self {
        let body = input.body.as_ref().and_then(|field| {
            (metadata.body == field.current && metadata.body != field.desired)
                .then(|| field.desired.clone())
        });
        let title = input.title.as_ref().and_then(|field| {
            (metadata.title == field.current && metadata.title != field.desired)
                .then(|| field.desired.clone())
        });

        Self {
            edit: ReviewRequestMetadataEdit { body, title },
        }
    }

    /// Returns whether at least one reconciled field needs a remote edit.
    fn requires_edit(&self) -> bool {
        self.edit.body.is_some() || self.edit.title.is_some()
    }
}

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