|
| 1 | +open Base |
| 2 | + |
| 3 | +let command_for_backend = function |
| 4 | + | "claude" -> Some "claude" |
| 5 | + | "codex" -> Some "codex" |
| 6 | + | "opencode" -> Some "opencode" |
| 7 | + | "pi" -> Some "pi" |
| 8 | + | "gemini" -> Some "gemini" |
| 9 | + | "patch-agent" -> Some "patch-agent" |
| 10 | + | _ -> None |
| 11 | + |
| 12 | +let path_dirs getenv_opt = |
| 13 | + match getenv_opt "PATH" with |
| 14 | + | None | Some "" -> [] |
| 15 | + | Some path -> |
| 16 | + String.split path ~on:':' |
| 17 | + |> List.map ~f:(fun dir -> if String.is_empty dir then "." else dir) |
| 18 | + |
| 19 | +let is_executable_file path = |
| 20 | + try |
| 21 | + match (Unix.stat path).Unix.st_kind with |
| 22 | + | Unix.S_REG -> |
| 23 | + Unix.access path [ Unix.X_OK ]; |
| 24 | + true |
| 25 | + | Unix.S_DIR | Unix.S_CHR | Unix.S_BLK | Unix.S_LNK | Unix.S_FIFO |
| 26 | + | Unix.S_SOCK -> |
| 27 | + false |
| 28 | + with Unix.Unix_error _ -> false |
| 29 | + |
| 30 | +let find_executable ?(getenv_opt = Stdlib.Sys.getenv_opt) |
| 31 | + ?(is_executable = is_executable_file) command = |
| 32 | + if String.contains command '/' then |
| 33 | + if is_executable command then Some command else None |
| 34 | + else |
| 35 | + path_dirs getenv_opt |
| 36 | + |> List.find_map ~f:(fun dir -> |
| 37 | + let path = Stdlib.Filename.concat dir command in |
| 38 | + if is_executable path then Some path else None) |
| 39 | + |
| 40 | +let check_backend ?getenv_opt ?is_executable backend = |
| 41 | + match command_for_backend backend with |
| 42 | + | None -> |
| 43 | + Error |
| 44 | + (Printf.sprintf "unknown backend %S; cannot run backend preflight" |
| 45 | + backend) |
| 46 | + | Some command -> ( |
| 47 | + match find_executable ?getenv_opt ?is_executable command with |
| 48 | + | Some _ -> Ok () |
| 49 | + | None -> |
| 50 | + Error |
| 51 | + (Printf.sprintf |
| 52 | + "backend %S requires executable %S on PATH, but it was not \ |
| 53 | + found or is not executable. Install that backend CLI, fix \ |
| 54 | + PATH, or choose another backend with --backend." |
| 55 | + backend command)) |
| 56 | + |
| 57 | +let selected_backends ~default_backend ~cli_model ~(repo_config : Repo_config.t) |
| 58 | + = |
| 59 | + let add acc backend = |
| 60 | + if List.mem acc backend ~equal:String.equal then acc else backend :: acc |
| 61 | + in |
| 62 | + let acc = add [] default_backend in |
| 63 | + let uses_routes = |
| 64 | + match cli_model with |
| 65 | + | Some model -> Backend_routing.is_auto_model (Some model) |
| 66 | + | None -> false |
| 67 | + in |
| 68 | + let acc = |
| 69 | + if uses_routes then |
| 70 | + List.fold repo_config.complexity_routes ~init:acc |
| 71 | + ~f:(fun acc (_, route) -> add acc route.Repo_config.backend) |
| 72 | + else acc |
| 73 | + in |
| 74 | + List.rev acc |
| 75 | + |
| 76 | +let validate ?getenv_opt ?is_executable ~default_backend ~cli_model ~repo_config |
| 77 | + () = |
| 78 | + let backends = selected_backends ~default_backend ~cli_model ~repo_config in |
| 79 | + let errors = |
| 80 | + List.filter_map backends ~f:(fun backend -> |
| 81 | + match check_backend ?getenv_opt ?is_executable backend with |
| 82 | + | Ok () -> None |
| 83 | + | Error msg -> Some msg) |
| 84 | + in |
| 85 | + match errors with [] -> Ok () | _ :: _ -> Error errors |
| 86 | + |
| 87 | +let%test "selected_backends ignores routes unless cli model is auto" = |
| 88 | + let repo_config = |
| 89 | + { |
| 90 | + Repo_config.complexity_routes = |
| 91 | + [ (1, { backend = "codex"; model = None }) ]; |
| 92 | + review_backends = []; |
| 93 | + } |
| 94 | + in |
| 95 | + List.equal String.equal |
| 96 | + (selected_backends ~default_backend:"claude" ~cli_model:None ~repo_config) |
| 97 | + [ "claude" ] |
| 98 | + |
| 99 | +let%test "selected_backends includes routes for auto model" = |
| 100 | + let repo_config = |
| 101 | + { |
| 102 | + Repo_config.complexity_routes = |
| 103 | + [ |
| 104 | + (1, { backend = "codex"; model = None }); |
| 105 | + (2, { backend = "claude"; model = Some "sonnet" }); |
| 106 | + ]; |
| 107 | + review_backends = []; |
| 108 | + } |
| 109 | + in |
| 110 | + List.equal String.equal |
| 111 | + (selected_backends ~default_backend:"claude" ~cli_model:(Some "auto") |
| 112 | + ~repo_config) |
| 113 | + [ "claude"; "codex" ] |
| 114 | + |
| 115 | +let%test "validate reports missing backend executable" = |
| 116 | + let repo_config = Repo_config.empty in |
| 117 | + match |
| 118 | + validate |
| 119 | + ~getenv_opt:(fun _ -> Some "/bin") |
| 120 | + ~is_executable:(fun _ -> false) |
| 121 | + ~default_backend:"codex" ~cli_model:None ~repo_config () |
| 122 | + with |
| 123 | + | Error [ msg ] -> |
| 124 | + String.is_substring msg ~substring:"codex" |
| 125 | + && String.is_substring msg ~substring:"not found or is not executable" |
| 126 | + | Ok () | Error _ -> false |
0 commit comments