Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / infra / agents / proto / src / lib.rs
lib.rs196 lines · 6.5 KiBRaw
| 1 | //! Wire types for the agentd -> agent-runner HTTP API. |
| 2 | //! |
| 3 | //! agentd (trusted) calls the runner inside the builder sandbox over the |
| 4 | //! internal Docker network. Every route is `POST` with a JSON body except |
| 5 | //! `GET /v1/health` and `GET /v1/slots/{slot}/bundle`. Errors are any non-2xx |
| 6 | //! status with an [`ErrorBody`]. Paths in requests are relative to the slot's |
| 7 | //! checkout root; the runner rejects anything that resolves outside it. |
| 8 | |
| 9 | use serde::{Deserialize, Serialize}; |
| 10 | |
| 11 | /// Default runner port inside the builder container. |
| 12 | pub const RUNNER_PORT: u16 = 7070; |
| 13 | |
| 14 | /// Branch prefix agents work and push on. |
| 15 | pub const AGENT_BRANCH_PREFIX: &str = "agent/"; |
| 16 | |
| 17 | /// `POST /v1/slots/{slot}/prepare` — (re)create the slot checkout on a fresh |
| 18 | /// branch from the mirror's `main`, keeping ignored build outputs warm. |
| 19 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 20 | pub struct PrepareRequest { |
| 21 | /// Full branch name, must start with [`AGENT_BRANCH_PREFIX`]. |
| 22 | pub branch: String, |
| 23 | /// Resume a parked task instead of branching fresh from `origin/main`. |
| 24 | /// Absent = legacy fresh prepare (old agentd builds never send it). |
| 25 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 26 | pub resume: Option<ResumeSpec>, |
| 27 | } |
| 28 | |
| 29 | /// How to restore a parked task's checkout (possibly on a different slot). |
| 30 | /// |
| 31 | /// Runner sequence: tear down the slot (`reset --hard HEAD; clean -fd`), |
| 32 | /// then if `wip_ref` is set `fetch origin +<wip_ref>:<wip_ref>` from the |
| 33 | /// mirror and `checkout -B <branch> <wip_sha>`, else `checkout -B <branch> |
| 34 | /// <base_sha>`; then `reset --soft <base_sha>` so the work reappears as |
| 35 | /// uncommitted changes against the original base; write `base_sha` to |
| 36 | /// `.git/agent-base`; no `clean` afterwards. `PrepareResponse.base_sha` |
| 37 | /// echoes `base_sha`. |
| 38 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 39 | pub struct ResumeSpec { |
| 40 | /// Commit the task's diff is measured against (original base, or the |
| 41 | /// last pushed sha for follow-ups after an approved push). |
| 42 | pub base_sha: String, |
| 43 | /// Mirror ref holding the parked WIP commit, e.g. `refs/agent-wip/42`. |
| 44 | /// `None` = no uncommitted work was parked. |
| 45 | pub wip_ref: Option<String>, |
| 46 | /// Expected sha of `wip_ref`; the runner refuses to resume if the fetched |
| 47 | /// ref resolves to anything else. |
| 48 | pub wip_sha: Option<String>, |
| 49 | } |
| 50 | |
| 51 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 52 | pub struct PrepareResponse { |
| 53 | /// Commit the branch was created from. |
| 54 | pub base_sha: String, |
| 55 | } |
| 56 | |
| 57 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 58 | #[serde(rename_all = "snake_case")] |
| 59 | pub enum ExecKind { |
| 60 | /// Short command, runs niced, no global lock. |
| 61 | Shell, |
| 62 | /// Build/test command, serialized behind the runner's single build slot. |
| 63 | Build, |
| 64 | } |
| 65 | |
| 66 | /// `POST /v1/slots/{slot}/exec` — run `bash -lc <command>` in the slot root |
| 67 | /// as the unprivileged sandbox user, in its own process group. |
| 68 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 69 | pub struct ExecRequest { |
| 70 | pub command: String, |
| 71 | pub kind: ExecKind, |
| 72 | pub timeout_secs: u64, |
| 73 | } |
| 74 | |
| 75 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 76 | pub struct ExecResponse { |
| 77 | /// `None` when killed (timeout, cancel, preemption). |
| 78 | pub exit_code: Option<i32>, |
| 79 | /// Head + tail kept when output exceeds the runner's cap. |
| 80 | pub stdout: String, |
| 81 | pub stderr: String, |
| 82 | pub truncated: bool, |
| 83 | pub timed_out: bool, |
| 84 | /// Killed by `/cancel` or `/v1/builds/kill`. |
| 85 | pub killed: bool, |
| 86 | /// Time spent waiting for the build slot (Build only). |
| 87 | pub queued_ms: u64, |
| 88 | pub duration_ms: u64, |
| 89 | } |
| 90 | |
| 91 | /// `POST /v1/slots/{slot}/read` |
| 92 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 93 | pub struct ReadRequest { |
| 94 | pub path: String, |
| 95 | /// 1-based first line; `None` = start of file. |
| 96 | pub start_line: Option<usize>, |
| 97 | /// Max lines to return; `None` = runner default cap. |
| 98 | pub max_lines: Option<usize>, |
| 99 | } |
| 100 | |
| 101 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 102 | pub struct ReadResponse { |
| 103 | pub content: String, |
| 104 | pub total_lines: usize, |
| 105 | pub truncated: bool, |
| 106 | } |
| 107 | |
| 108 | /// `POST /v1/slots/{slot}/write` — create or overwrite (parents created). |
| 109 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 110 | pub struct WriteRequest { |
| 111 | pub path: String, |
| 112 | pub content: String, |
| 113 | } |
| 114 | |
| 115 | /// `POST /v1/slots/{slot}/edit` — replace exactly one occurrence of `old`. |
| 116 | /// Zero or multiple matches is a 409 with an explanatory [`ErrorBody`]. |
| 117 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 118 | pub struct EditRequest { |
| 119 | pub path: String, |
| 120 | pub old: String, |
| 121 | pub new: String, |
| 122 | } |
| 123 | |
| 124 | /// `POST /v1/slots/{slot}/list` — one directory level; dirs end with `/`. |
| 125 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 126 | pub struct ListRequest { |
| 127 | pub path: String, |
| 128 | } |
| 129 | |
| 130 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 131 | pub struct ListResponse { |
| 132 | pub entries: Vec<String>, |
| 133 | pub truncated: bool, |
| 134 | } |
| 135 | |
| 136 | /// `POST /v1/slots/{slot}/search` — ripgrep, respecting .gitignore. |
| 137 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 138 | pub struct SearchRequest { |
| 139 | pub pattern: String, |
| 140 | /// Subdirectory to search; `None` = slot root. |
| 141 | pub path: Option<String>, |
| 142 | /// e.g. `*.kt` |
| 143 | pub glob: Option<String>, |
| 144 | pub fixed_string: bool, |
| 145 | } |
| 146 | |
| 147 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 148 | pub struct SearchResponse { |
| 149 | /// `path:line:text` lines. |
| 150 | pub matches: String, |
| 151 | pub truncated: bool, |
| 152 | } |
| 153 | |
| 154 | /// `POST /v1/slots/{slot}/diff` (empty body) — working tree vs branch base, |
| 155 | /// including untracked files. |
| 156 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 157 | pub struct DiffResponse { |
| 158 | pub stat: String, |
| 159 | pub diff: String, |
| 160 | pub truncated: bool, |
| 161 | } |
| 162 | |
| 163 | /// `POST /v1/slots/{slot}/commit` — stage everything in the slot and commit. |
| 164 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 165 | pub struct CommitRequest { |
| 166 | pub message: String, |
| 167 | } |
| 168 | |
| 169 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 170 | pub struct CommitResponse { |
| 171 | pub sha: String, |
| 172 | } |
| 173 | |
| 174 | // `GET /v1/slots/{slot}/bundle` — `application/octet-stream` git bundle of |
| 175 | // `<base_sha>..<branch>` for the slot's current branch. agentd imports it |
| 176 | // into the mirror (bundles are data; nothing in the sandbox's repo config is |
| 177 | // ever executed by agentd). |
| 178 | |
| 179 | /// `POST /v1/slots/{slot}/cancel` (empty body) — kill whatever the slot runs. |
| 180 | /// `POST /v1/builds/kill` (empty body) — kill the in-flight Build, if any. |
| 181 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 182 | pub struct KillResponse { |
| 183 | pub killed: bool, |
| 184 | } |
| 185 | |
| 186 | /// `GET /v1/health` |
| 187 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 188 | pub struct HealthResponse { |
| 189 | pub slots: u32, |
| 190 | pub build_running: bool, |
| 191 | } |
| 192 | |
| 193 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 194 | pub struct ErrorBody { |
| 195 | pub error: String, |
| 196 | } |