agent-file-tools 0.55.1

Agent File Tools — tree-sitter powered code analysis for AI agents
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
use std::fmt;
use std::path::PathBuf;
use std::process::Command;
use std::sync::LazyLock;

use serde_json::Value;

use super::model::GithubDocument;
use super::normalize::normalize_structured_document;
use super::resource::{GithubResource, GithubResourceKind};

const ISSUE_JSON_FIELDS: &str = "number,title,state,author,createdAt,updatedAt,labels,assignees,milestone,body,reactionGroups,comments,url";
const PR_JSON_FIELDS: &str = "number,title,state,author,createdAt,updatedAt,labels,assignees,milestone,body,reactionGroups,comments,files,reviews,url";
const PR_REVIEW_COMMENTS_QUERY: &str = "query AftReadPullRequestReviewComments($owner: String!, $name: String!, $number: Int!) { repository(owner: $owner, name: $name) { nameWithOwner pullRequest(number: $number) { number reviews(first: 100) { nodes { author { login } body state submittedAt comments(first: 100) { totalCount nodes { author { login } body createdAt updatedAt isMinimized } } } } } } }";

/// Request context passed to a structured GitHub fetcher.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GithubFetchRequest {
    pub resource: GithubResource,
    pub working_directory: PathBuf,
}

/// Fetches structured GitHub data. Implementations never return CLI display
/// text; the engine only accepts a normalized document from this interface.
pub trait GithubFetcher: Send + Sync {
    fn fetch(&self, request: &GithubFetchRequest) -> Result<GithubDocument, GithubReadError>;
}

/// The typed failures returned by the GitHub read engine.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GithubReadError {
    GithubReadDisabled,
    InvalidResource(String),
    InvalidCommentSelector(String),
    GithubCliMissing,
    FetchFailed(String),
    InvalidStructuredResponse(String),
}

impl GithubReadError {
    pub fn code(&self) -> &'static str {
        match self {
            Self::GithubReadDisabled => "gh_read_disabled",
            Self::InvalidResource(_) => "invalid_resource",
            Self::InvalidCommentSelector(_) => "invalid_comment_selector",
            Self::GithubCliMissing => "github_cli_missing",
            Self::FetchFailed(_) | Self::InvalidStructuredResponse(_) => "github_fetch_failed",
        }
    }

    pub fn invalid_resource(message: impl Into<String>) -> Self {
        Self::InvalidResource(message.into())
    }
}

impl fmt::Display for GithubReadError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::GithubReadDisabled => formatter
                .write_str("GitHub reads are disabled; set gh_read.enabled: true in aft.jsonc"),
            Self::InvalidResource(message)
            | Self::InvalidCommentSelector(message)
            | Self::FetchFailed(message)
            | Self::InvalidStructuredResponse(message) => formatter.write_str(message),
            Self::GithubCliMissing => formatter.write_str(
                "GitHub reads require the `gh` CLI. Install GitHub CLI and authenticate it with `gh auth login`.",
            ),
        }
    }
}

impl std::error::Error for GithubReadError {}

/// A subprocess result for the `gh` command seam.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GhCommandOutput {
    pub success: bool,
    pub stdout: Vec<u8>,
    pub stderr: Vec<u8>,
}

/// Error running `gh` before it produced a process result.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GhCommandError {
    NotFound,
    Other(String),
}

/// Injectable `gh` runner. Fixture runners can assert the exact command and
/// request working directory without requiring a real GitHub installation.
pub trait GhCommandRunner: Send + Sync {
    fn run(
        &self,
        working_directory: &std::path::Path,
        args: &[String],
    ) -> Result<GhCommandOutput, GhCommandError>;
}

/// Production runner that executes the bare `gh` command in the caller's
/// working directory so the CLI owns short-form repository resolution.
#[derive(Default)]
pub struct SystemGhCommandRunner;

impl GhCommandRunner for SystemGhCommandRunner {
    fn run(
        &self,
        working_directory: &std::path::Path,
        args: &[String],
    ) -> Result<GhCommandOutput, GhCommandError> {
        let output = Command::new("gh")
            .args(args)
            .current_dir(working_directory)
            .output()
            .map_err(|error| match error.kind() {
                std::io::ErrorKind::NotFound => GhCommandError::NotFound,
                _ => GhCommandError::Other(error.to_string()),
            })?;
        Ok(GhCommandOutput {
            success: output.status.success(),
            stdout: output.stdout,
            stderr: output.stderr,
        })
    }
}

/// Structured `gh issue view` / `gh pr view` fetcher.
///
/// The only parser after the subprocess boundary is JSON normalization. In
/// particular, stderr is used only to construct an actionable redacted error,
/// never to infer fields or repository selection.
pub struct GhCliFetcher<R = SystemGhCommandRunner> {
    runner: R,
}

impl GhCliFetcher<SystemGhCommandRunner> {
    pub fn system() -> Self {
        Self {
            runner: SystemGhCommandRunner,
        }
    }
}

impl<R> GhCliFetcher<R> {
    pub fn new(runner: R) -> Self {
        Self { runner }
    }
}

impl<R: GhCommandRunner> GithubFetcher for GhCliFetcher<R> {
    fn fetch(&self, request: &GithubFetchRequest) -> Result<GithubDocument, GithubReadError> {
        let json =
            self.structured_json(&request.working_directory, &gh_view_args(&request.resource))?;
        let mut document = normalize_document(&request.resource, &json)?;
        if request.resource.kind == GithubResourceKind::PullRequest {
            let review_json = self.structured_json(
                &request.working_directory,
                &gh_pr_review_comments_args(&request.resource, &document.repository)?,
            )?;
            let review_document = normalize_document(&request.resource, &review_json)?;
            document.review_comment_sections = review_document.review_comment_sections;
        }
        Ok(document)
    }
}

impl<R: GhCommandRunner> GhCliFetcher<R> {
    fn structured_json(
        &self,
        working_directory: &std::path::Path,
        args: &[String],
    ) -> Result<Value, GithubReadError> {
        let result = self
            .runner
            .run(working_directory, args)
            .map_err(|error| match error {
                GhCommandError::NotFound => GithubReadError::GithubCliMissing,
                GhCommandError::Other(message) => GithubReadError::FetchFailed(format!(
                    "could not start GitHub CLI: {}",
                    redact_gh_error(&message)
                )),
            })?;
        if !result.success {
            return Err(GithubReadError::FetchFailed(redact_gh_error(
                &best_gh_error(&result.stderr, &result.stdout),
            )));
        }
        let json: Value = serde_json::from_slice(&result.stdout).map_err(|error| {
            GithubReadError::InvalidStructuredResponse(format!(
                "GitHub CLI returned invalid structured JSON: {error}"
            ))
        })?;
        if json.get("success").and_then(Value::as_bool) == Some(false) {
            let underlying = json
                .get("error")
                .or_else(|| json.get("message"))
                .and_then(Value::as_str)
                .unwrap_or("GitHub declined the resource request");
            return Err(GithubReadError::FetchFailed(redact_gh_error(underlying)));
        }
        if let Some(errors) = json.get("errors") {
            return Err(GithubReadError::FetchFailed(redact_gh_error(
                &errors.to_string(),
            )));
        }
        Ok(json)
    }
}

fn normalize_document(
    resource: &GithubResource,
    json: &Value,
) -> Result<GithubDocument, GithubReadError> {
    normalize_structured_document(resource, json).map_err(|error| {
        GithubReadError::InvalidStructuredResponse(format!(
            "GitHub returned an incomplete structured response: {}",
            redact_gh_error(&error.to_string())
        ))
    })
}

/// Build the exact structured-view invocation. Short forms intentionally omit
/// `-R`; explicit forms include it and are otherwise identical.
pub fn gh_view_args(resource: &GithubResource) -> Vec<String> {
    let fields = match resource.kind {
        GithubResourceKind::Issue => ISSUE_JSON_FIELDS,
        GithubResourceKind::PullRequest => PR_JSON_FIELDS,
    };
    let mut args = vec![
        resource.kind.command().to_string(),
        "view".to_string(),
        resource.number.to_string(),
    ];
    if let Some(repository) = &resource.repository {
        args.push("-R".to_string());
        args.push(repository.clone());
    }
    args.push("--json".to_string());
    args.push(fields.to_string());
    args
}

/// Build the structured GraphQL fetch for inline PR review comments. The first
/// `pr view --json` call resolves the repository; this second JSON call fills
/// comment sections that `gh pr view` does not expose as a display field.
pub fn gh_pr_review_comments_args(
    resource: &GithubResource,
    resolved_repository: &str,
) -> Result<Vec<String>, GithubReadError> {
    if resource.kind != GithubResourceKind::PullRequest {
        return Err(GithubReadError::InvalidStructuredResponse(
            "review-comment query requested for a non-pull-request resource".to_string(),
        ));
    }
    let (owner, repository) = resolved_repository.split_once('/').ok_or_else(|| {
        GithubReadError::InvalidStructuredResponse(
            "GitHub structured response returned an invalid resolved repository".to_string(),
        )
    })?;
    if owner.is_empty() || repository.is_empty() || repository.contains('/') {
        return Err(GithubReadError::InvalidStructuredResponse(
            "GitHub structured response returned an invalid resolved repository".to_string(),
        ));
    }
    let number = i32::try_from(resource.number).map_err(|_| {
        GithubReadError::InvalidStructuredResponse(
            "GitHub resource number exceeds the GraphQL integer range".to_string(),
        )
    })?;
    let mut args = vec![
        "api".to_string(),
        "graphql".to_string(),
        "-f".to_string(),
        format!("query={PR_REVIEW_COMMENTS_QUERY}"),
        "-F".to_string(),
        format!("owner={owner}"),
        "-F".to_string(),
        format!("name={repository}"),
        "-F".to_string(),
        format!("number={number}"),
    ];
    if let Some(repository) = &resource.repository {
        args.push("-R".to_string());
        args.push(repository.clone());
    }
    Ok(args)
}

fn best_gh_error(stderr: &[u8], stdout: &[u8]) -> String {
    let stderr = String::from_utf8_lossy(stderr).trim().to_string();
    if !stderr.is_empty() {
        return stderr;
    }
    let stdout = String::from_utf8_lossy(stdout).trim().to_string();
    if !stdout.is_empty() {
        return stdout;
    }
    "GitHub CLI failed without an error message".to_string()
}

static GITHUB_TOKEN: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"(?:gh[pousr]_[A-Za-z0-9_]+|github_pat_[A-Za-z0-9_]+)")
        .expect("GitHub token redaction expression is valid")
});
static AUTHORIZATION_VALUE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"(?i)((?:authorization|token)\s*[:=]\s*)[^\s,;]+")
        .expect("authorization redaction expression is valid")
});

/// Redact ambient credentials while keeping GitHub's actionable authorization,
/// private-resource, and not-found diagnostics visible to the caller.
pub fn redact_gh_error(message: &str) -> String {
    let with_tokens = GITHUB_TOKEN.replace_all(message, "[redacted]");
    AUTHORIZATION_VALUE
        .replace_all(&with_tokens, "${1}[redacted]")
        .into_owned()
}

#[cfg(test)]
mod tests {
    use std::sync::Mutex;

    use serde_json::json;

    use super::*;
    use crate::github_read::resource::{GithubResource, GithubResourceKind};

    #[derive(Default)]
    struct FixtureRunner {
        calls: Mutex<Vec<(PathBuf, Vec<String>)>>,
        output: Mutex<Option<Result<GhCommandOutput, GhCommandError>>>,
    }

    impl GhCommandRunner for FixtureRunner {
        fn run(
            &self,
            working_directory: &std::path::Path,
            args: &[String],
        ) -> Result<GhCommandOutput, GhCommandError> {
            self.calls
                .lock()
                .unwrap()
                .push((working_directory.to_path_buf(), args.to_vec()));
            self.output.lock().unwrap().take().unwrap()
        }
    }

    #[test]
    fn explicit_and_short_forms_differ_only_by_repo_flag() {
        let short = GithubResource {
            kind: GithubResourceKind::Issue,
            number: 1,
            repository: None,
            comment_selector: None,
        };
        let explicit = GithubResource {
            repository: Some("owner/repo".to_string()),
            ..short.clone()
        };
        let short_args = gh_view_args(&short);
        let explicit_args = gh_view_args(&explicit);
        assert!(!short_args.iter().any(|argument| argument == "-R"));
        assert!(explicit_args
            .windows(2)
            .any(|pair| pair == ["-R", "owner/repo"]));

        let short_pr = GithubResource {
            kind: GithubResourceKind::PullRequest,
            ..short.clone()
        };
        let explicit_pr = GithubResource {
            repository: Some("owner/repo".to_string()),
            ..short_pr.clone()
        };
        let short_review_args = gh_pr_review_comments_args(&short_pr, "owner/repo").unwrap();
        assert!(!short_review_args.iter().any(|argument| argument == "-R"));
        let explicit_review_args = gh_pr_review_comments_args(&explicit_pr, "owner/repo").unwrap();
        assert!(explicit_review_args
            .windows(2)
            .any(|pair| pair == ["-R", "owner/repo"]));
        assert!(short_review_args
            .iter()
            .any(|argument| argument.starts_with("query=query AftReadPullRequestReviewComments")));
    }

    #[test]
    fn fetcher_uses_structured_json_and_redacts_failures() {
        let runner = FixtureRunner::default();
        *runner.output.lock().unwrap() = Some(Ok(GhCommandOutput {
            success: true,
            stdout: serde_json::to_vec(&json!({
                "number": 1,
                "title": "fixture",
                "url": "https://github.com/owner/repo/issues/1"
            }))
            .unwrap(),
            stderr: Vec::new(),
        }));
        let fetcher = GhCliFetcher::new(runner);
        let request = GithubFetchRequest {
            resource: GithubResource {
                kind: GithubResourceKind::Issue,
                number: 1,
                repository: None,
                comment_selector: None,
            },
            working_directory: PathBuf::from("/fixture"),
        };
        let document = fetcher.fetch(&request).unwrap();
        assert_eq!(document.repository, "owner/repo");

        let redacted = redact_gh_error("HTTP 401 token=ghp_secret github_pat_secret");
        assert!(!redacted.contains("secret"));
        assert!(redacted.contains("HTTP 401"));
    }
}