Skip to main content

aft_base/
github_address.rs

1//! GitHub resource spellings must not be interpreted as filesystem paths.
2
3use crate::protocol::Response;
4
5pub fn is_github_resource_path(path: &str) -> bool {
6    path.starts_with("issue://") || path.starts_with("pr://")
7}
8
9/// Where and how to turn GitHub comment writes on; shared by every refusal so
10/// the agent always learns the exact setting.
11pub const GITHUB_WRITE_ENABLE_HINT: &str = "set \"github\": { \"write\": true } in the user config (~/.config/cortexkit/aft.jsonc) and restart the host";
12
13/// Error code for a GitHub address handed to a tool that only works on files.
14pub const GITHUB_RESOURCE_NOT_FILE: &str = "github_resource_not_file";
15
16/// Refusal text for a GitHub address (`issue://…`, `pr://…`) given to a tool
17/// that can only touch local files. Such a spelling must never be resolved
18/// into a project path: joined onto the root it becomes `<root>/issue:/N` and
19/// a file or directory would be created there instead of a comment.
20pub fn github_resource_not_file_message(operation: &str, path: &str) -> String {
21    format!(
22        "{operation}: '{path}' is a GitHub issue or pull-request address, not a local file, so nothing was changed. \
23         Only `write` (post a new comment) and `edit` with one edits[] find/replace entry on issue://N/comments/K \
24         (change a comment) accept GitHub addresses, and only while github.write is enabled: {GITHUB_WRITE_ENABLE_HINT}."
25    )
26}
27
28/// Returns an error response when `path` is a GitHub address. Mutating
29/// handlers call this before resolving a path so the address can never become
30/// a file on disk.
31pub fn reject_github_resource_path(
32    req_id: &str,
33    operation: &str,
34    path: &str,
35) -> Result<(), Response> {
36    if is_github_resource_path(path) {
37        return Err(Response::error(
38            req_id,
39            GITHUB_RESOURCE_NOT_FILE,
40            github_resource_not_file_message(operation, path),
41        ));
42    }
43    Ok(())
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn github_prefixes_keep_the_same_filesystem_refusal() {
52        for address in ["issue://7", "pr://owner/repo/8/comments/2", "issue://"] {
53            let refusal = reject_github_resource_path("request", "file operation", address)
54                .expect_err("GitHub prefixes are not local files");
55            assert!(!refusal.success);
56            assert_eq!(refusal.data["code"], "github_resource_not_file");
57        }
58        for path in ["issue:/7", "pr/8", "ISSUE://7", "src/pr.rs"] {
59            assert!(reject_github_resource_path("request", "file operation", path).is_ok());
60        }
61    }
62}