Skip to main content

aft/
protocol.rs

1use serde::{Deserialize, Serialize};
2
3use crate::bash_background::BgTaskStatus;
4
5/// Full payload returned by the `status` command and cached by status push frames.
6pub type StatusPayload = serde_json::Value;
7
8/// v0.18 streaming semantics for hoisted bash.
9///
10/// Foreground `bash` execution may emit zero or more `progress` frames before
11/// its final `Response`. Each progress frame is NDJSON on stdout with the same
12/// `request_id` as the original request and a `kind` of `stdout` or `stderr`.
13/// The final response remains the existing `{ id, success, ... }` envelope so
14/// older callers can ignore streaming frames. Bash permission prompts surface
15/// through the `permission_required` error code, which carries the permission
16/// ask payload the plugin uses to prompt and retry.
17pub const ERROR_PERMISSION_REQUIRED: &str = "permission_required";
18
19#[derive(Debug, Clone, Serialize)]
20#[serde(rename_all = "snake_case")]
21pub enum ProgressKind {
22    Stdout,
23    Stderr,
24}
25
26#[derive(Debug, Clone, Serialize)]
27pub struct ProgressFrame {
28    #[serde(rename = "type")]
29    pub frame_type: &'static str,
30    pub request_id: String,
31    pub kind: ProgressKind,
32    pub chunk: String,
33}
34
35#[derive(Debug, Clone, Serialize)]
36pub struct PermissionAskFrame {
37    #[serde(rename = "type")]
38    pub frame_type: &'static str,
39    pub request_id: String,
40    pub asks: serde_json::Value,
41}
42
43#[derive(Debug, Clone, Serialize)]
44pub struct BashCompletedFrame {
45    #[serde(rename = "type")]
46    pub frame_type: &'static str,
47    pub task_id: String,
48    pub session_id: String,
49    pub status: BgTaskStatus,
50    pub exit_code: Option<i32>,
51    pub command: String,
52    /// Tail of stdout+stderr (≤300 bytes), already decoded as lossy UTF-8.
53    /// Empty string when no output was captured. Used by plugins to inline
54    /// short results in the system-reminder so agents don't need a follow-up
55    /// `bash_status` round-trip for typical short commands.
56    #[serde(default)]
57    pub output_preview: String,
58    /// True when the task produced more output than `output_preview` shows
59    /// (rotated buffer, file > 300 bytes, etc). Plugins use this to render a
60    /// `…` prefix and signal that `bash_status` would return more.
61    #[serde(default)]
62    pub output_truncated: bool,
63    /// Token count of raw stdout+stderr before compression. Omitted when the
64    /// payload exceeded the 128 KiB per-stream tokenization cap.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub original_tokens: Option<u32>,
67    /// Token count of the compressed completion payload. Omitted when raw
68    /// tokenization was skipped due to the cap.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub compressed_tokens: Option<u32>,
71    /// True when output exceeded the tokenization cap and was not measured.
72    #[serde(default)]
73    pub tokens_skipped: bool,
74}
75
76#[derive(Debug, Clone, Serialize)]
77pub struct BashLongRunningFrame {
78    #[serde(rename = "type")]
79    pub frame_type: &'static str,
80    pub task_id: String,
81    pub session_id: String,
82    pub command: String,
83    pub elapsed_ms: u64,
84}
85
86#[derive(Debug, Clone, Serialize)]
87pub struct BashPatternMatchFrame {
88    #[serde(rename = "type")]
89    pub frame_type: &'static str,
90    pub task_id: String,
91    pub session_id: String,
92    pub watch_id: String,
93    pub match_text: String,
94    pub match_offset: u64,
95    pub context: String,
96    pub once: bool,
97    pub reason: &'static str,
98}
99
100/// Pushed after configure has completed, when the deferred file walk and
101/// language detection produce warnings (missing formatter/checker/LSP binaries,
102/// and search-index file-count warnings). The walk runs in a background
103/// thread so configure itself returns in <100 ms even on huge directories
104/// (e.g. user's $HOME). When the walk finishes, AFT pushes one frame with
105/// the merged warnings — the plugin delivers them through the same path as
106/// the synchronous warnings that configure used to return.
107#[derive(Debug, Clone, Serialize)]
108pub struct ConfigureWarningsFrame {
109    #[serde(rename = "type")]
110    pub frame_type: &'static str,
111    /// Session id from the configure request that spawned the deferred walk.
112    /// Project-shared bridges can serve multiple sessions, so plugins need this
113    /// to route async warning notifications back to the initiating session.
114    #[serde(default)]
115    pub session_id: Option<String>,
116    /// Project root the warnings refer to. Plugins use this to scope the
117    /// session-id deduplication of repeated identical warnings.
118    pub project_root: String,
119    /// Source-file count discovered by the bounded walk.
120    pub source_file_count: usize,
121    /// Merged formatter/checker/LSP missing-binary warnings.
122    pub warnings: Vec<serde_json::Value>,
123}
124
125#[derive(Debug, Clone, Serialize)]
126pub struct StatusChangedFrame {
127    #[serde(rename = "type")]
128    pub frame_type: &'static str,
129    #[serde(default)]
130    pub session_id: Option<String>,
131    pub snapshot: StatusPayload,
132}
133
134#[derive(Debug, Clone, Serialize)]
135#[serde(untagged)]
136pub enum PushFrame {
137    Progress(ProgressFrame),
138    BashCompleted(BashCompletedFrame),
139    BashLongRunning(BashLongRunningFrame),
140    BashPatternMatch(BashPatternMatchFrame),
141    ConfigureWarnings(ConfigureWarningsFrame),
142    StatusChanged(StatusChangedFrame),
143}
144
145impl PermissionAskFrame {
146    pub fn new(request_id: impl Into<String>, asks: serde_json::Value) -> Self {
147        Self {
148            frame_type: "permission_ask",
149            request_id: request_id.into(),
150            asks,
151        }
152    }
153}
154
155impl ProgressFrame {
156    pub fn new(
157        request_id: impl Into<String>,
158        kind: ProgressKind,
159        chunk: impl Into<String>,
160    ) -> Self {
161        Self {
162            frame_type: "progress",
163            request_id: request_id.into(),
164            kind,
165            chunk: chunk.into(),
166        }
167    }
168}
169
170impl ConfigureWarningsFrame {
171    pub fn new(
172        project_root: impl Into<String>,
173        source_file_count: usize,
174        warnings: Vec<serde_json::Value>,
175    ) -> Self {
176        Self::new_with_session_id(None, project_root, source_file_count, warnings)
177    }
178
179    pub fn new_with_session_id(
180        session_id: Option<String>,
181        project_root: impl Into<String>,
182        source_file_count: usize,
183        warnings: Vec<serde_json::Value>,
184    ) -> Self {
185        Self {
186            frame_type: "configure_warnings",
187            session_id,
188            project_root: project_root.into(),
189            source_file_count,
190            warnings,
191        }
192    }
193}
194
195impl StatusChangedFrame {
196    pub fn new(session_id: Option<String>, snapshot: StatusPayload) -> Self {
197        Self {
198            frame_type: "status_changed",
199            session_id,
200            snapshot: status_push_payload(snapshot),
201        }
202    }
203}
204
205fn status_push_payload(mut snapshot: StatusPayload) -> StatusPayload {
206    if let Some(object) = snapshot.as_object_mut() {
207        object.remove("session");
208        if let Some(compression) = object
209            .get_mut("compression")
210            .and_then(serde_json::Value::as_object_mut)
211        {
212            compression.remove("session");
213        }
214    }
215    snapshot
216}
217
218#[cfg(test)]
219mod tests {
220    use super::*;
221    use serde::Deserialize;
222    use serde_json::json;
223
224    #[derive(Debug, Deserialize)]
225    struct ConfigureWarningsFrameRoundTrip {
226        #[serde(rename = "type")]
227        frame_type: String,
228        session_id: Option<String>,
229        project_root: String,
230        source_file_count: usize,
231        warnings: Vec<serde_json::Value>,
232    }
233
234    #[test]
235    fn configure_warnings_frame_serializes_null_session_id_by_default() {
236        let frame = ConfigureWarningsFrame::new(
237            "/repo",
238            42,
239            vec![json!({
240                "kind": "formatter_not_installed",
241                "tool": "biome",
242                "hint": "Install biome."
243            })],
244        );
245
246        let json = serde_json::to_string(&frame).expect("serialize ConfigureWarningsFrame");
247        let decoded: ConfigureWarningsFrameRoundTrip =
248            serde_json::from_str(&json).expect("deserialize ConfigureWarningsFrame JSON");
249
250        assert_eq!(decoded.session_id, None);
251    }
252
253    #[test]
254    fn configure_warnings_frame_serializes_session_id() {
255        let frame = ConfigureWarningsFrame::new_with_session_id(
256            Some("session-1".to_string()),
257            "/repo",
258            42,
259            vec![json!({
260                "kind": "formatter_not_installed",
261                "tool": "biome",
262                "hint": "Install biome."
263            })],
264        );
265
266        let json = serde_json::to_string(&frame).expect("serialize ConfigureWarningsFrame");
267        let decoded: ConfigureWarningsFrameRoundTrip =
268            serde_json::from_str(&json).expect("deserialize ConfigureWarningsFrame JSON");
269
270        assert_eq!(decoded.frame_type, "configure_warnings");
271        assert_eq!(decoded.session_id.as_deref(), Some("session-1"));
272        assert_eq!(decoded.project_root, "/repo");
273        assert_eq!(decoded.source_file_count, 42);
274        assert_eq!(decoded.warnings[0]["tool"], "biome");
275    }
276
277    #[test]
278    fn status_changed_frame_serializes_correctly() {
279        let frame = StatusChangedFrame::new(
280            None,
281            json!({
282                "version": "0.24.0",
283                "project_root": "/repo",
284                "cache_role": "main",
285                "canonical_root": "/repo",
286                "search_index": { "status": "ready" },
287                "semantic_index": { "status": "disabled" },
288            }),
289        );
290
291        let json = serde_json::to_value(PushFrame::StatusChanged(frame)).unwrap();
292        assert_eq!(json["type"], "status_changed");
293        assert!(json["session_id"].is_null());
294        assert_eq!(json["snapshot"]["cache_role"], "main");
295        assert_eq!(json["snapshot"]["project_root"], "/repo");
296    }
297
298    #[test]
299    fn status_changed_frame_strips_session_scoped_push_fields() {
300        let frame = StatusChangedFrame::new(
301            None,
302            json!({
303                "version": "0.24.0",
304                "checkpoints_total": 7,
305                "session": { "id": "default", "tracked_files": 2, "checkpoints": 1 },
306                "compression": {
307                    "project": { "events": 3 },
308                    "session": { "events": 99 }
309                }
310            }),
311        );
312
313        assert!(frame.snapshot.get("session").is_none());
314        assert_eq!(frame.snapshot["checkpoints_total"], 7);
315        assert_eq!(frame.snapshot["compression"]["project"]["events"], 3);
316        assert!(frame.snapshot["compression"].get("session").is_none());
317    }
318}
319
320impl BashCompletedFrame {
321    pub fn new(
322        task_id: impl Into<String>,
323        session_id: impl Into<String>,
324        status: BgTaskStatus,
325        exit_code: Option<i32>,
326        command: impl Into<String>,
327        output_preview: impl Into<String>,
328        output_truncated: bool,
329        original_tokens: Option<u32>,
330        compressed_tokens: Option<u32>,
331        tokens_skipped: bool,
332    ) -> Self {
333        Self {
334            frame_type: "bash_completed",
335            task_id: task_id.into(),
336            session_id: session_id.into(),
337            status,
338            exit_code,
339            command: command.into(),
340            output_preview: output_preview.into(),
341            output_truncated,
342            original_tokens,
343            compressed_tokens,
344            tokens_skipped,
345        }
346    }
347}
348
349impl BashLongRunningFrame {
350    pub fn new(
351        task_id: impl Into<String>,
352        session_id: impl Into<String>,
353        command: impl Into<String>,
354        elapsed_ms: u64,
355    ) -> Self {
356        Self {
357            frame_type: "bash_long_running",
358            task_id: task_id.into(),
359            session_id: session_id.into(),
360            command: command.into(),
361            elapsed_ms,
362        }
363    }
364}
365
366impl BashPatternMatchFrame {
367    pub fn new(
368        task_id: impl Into<String>,
369        session_id: impl Into<String>,
370        watch_id: impl Into<String>,
371        match_text: impl Into<String>,
372        match_offset: u64,
373        context: impl Into<String>,
374        once: bool,
375    ) -> Self {
376        Self {
377            frame_type: "bash_pattern_match",
378            task_id: task_id.into(),
379            session_id: session_id.into(),
380            watch_id: watch_id.into(),
381            match_text: match_text.into(),
382            match_offset,
383            context: context.into(),
384            once,
385            reason: "pattern_match",
386        }
387    }
388
389    pub fn task_exit(
390        task_id: impl Into<String>,
391        session_id: impl Into<String>,
392        match_text: impl Into<String>,
393        context: impl Into<String>,
394    ) -> Self {
395        Self {
396            frame_type: "bash_pattern_match",
397            task_id: task_id.into(),
398            session_id: session_id.into(),
399            watch_id: "exit".to_string(),
400            match_text: match_text.into(),
401            match_offset: 0,
402            context: context.into(),
403            once: true,
404            reason: "task_exit",
405        }
406    }
407}
408
409/// Fallback session identifier used when a request arrives without one.
410///
411/// Introduced alongside project-shared bridges (issue #14): one `aft` process
412/// can now serve many OpenCode sessions in the same project. Undo/checkpoint
413/// state is partitioned by session inside Rust, but callers that haven't been
414/// updated to pass `session_id` (older plugins, direct CLI usage, tests) still
415/// need to work — they share this default namespace.
416///
417/// Also used as the migration target for legacy pre-session backups on disk.
418pub const DEFAULT_SESSION_ID: &str = "__default__";
419
420/// Inbound request envelope.
421///
422/// Two-stage parse: deserialize this first to get `id` + `command`, then
423/// dispatch on `command` and pull specific params from the flattened `params`.
424#[derive(Debug, Deserialize)]
425pub struct RawRequest {
426    pub id: String,
427    #[serde(alias = "method")]
428    pub command: String,
429    /// Optional LSP hints from the plugin (R031 forward compatibility).
430    #[serde(default)]
431    pub lsp_hints: Option<serde_json::Value>,
432    /// Optional session namespace for undo/checkpoint isolation.
433    ///
434    /// When the plugin passes `session_id`, Rust partitions backup/checkpoint
435    /// state by it so concurrent OpenCode sessions sharing one bridge can't
436    /// see or restore each other's snapshots. When absent, falls back to
437    /// [`DEFAULT_SESSION_ID`].
438    #[serde(default)]
439    pub session_id: Option<String>,
440    /// All remaining fields are captured here for per-command deserialization.
441    #[serde(flatten)]
442    pub params: serde_json::Value,
443}
444
445impl RawRequest {
446    /// Session namespace for this request, falling back to [`DEFAULT_SESSION_ID`]
447    /// when the plugin didn't supply one.
448    pub fn session(&self) -> &str {
449        self.session_id.as_deref().unwrap_or(DEFAULT_SESSION_ID)
450    }
451}
452
453/// Outbound response envelope.
454///
455/// `data` is flattened into the top-level JSON object, so a response like
456/// `Response { id: "1", success: true, data: json!({"command": "pong"}) }`
457/// serializes to `{"id":"1","success":true,"command":"pong"}`.
458///
459/// # Honest reporting convention (tri-state)
460///
461/// Tools that search, check, or otherwise produce results MUST follow this
462/// convention so agents can distinguish "did the work, found nothing" from
463/// "couldn't do the work" from "partially did the work":
464///
465/// 1. **`success: false`** — the requested work could not be performed.
466///    Includes a `code` (e.g., `"path_not_found"`, `"no_lsp_server"`,
467///    `"project_too_large"`) and a human-readable `message`. The agent
468///    should treat this as an error and read the message.
469///
470/// 2. **`success: true` + completion signaling** — the work was performed.
471///    Tools must report whether the result is *complete* OR which subset
472///    was actually performed. Conventional fields:
473///    - `complete: true` — full result, agent can trust absence of items
474///    - `complete: false` + `pending_files: [...]` / `unchecked_files: [...]`
475///      / `scope_warnings: [...]` — partial result, with named gaps
476///    - `removed: true|false` (for mutations) — did the file actually change
477///    - `skipped_files: [{file, reason}]` — files we couldn't process inside
478///      the requested scope
479///    - `no_files_matched_scope: bool` — the scope (path/glob) found zero
480///      candidates (distinct from "candidates found, no matches")
481///
482/// 3. **Side-effect skip codes** — when the main work succeeded but a
483///    non-essential side step was skipped (e.g., post-write formatting),
484///    use a `<step>_skipped_reason` field. Approved values:
485///    - `format_skipped_reason`: `"unsupported_language"` |
486///      `"no_formatter_configured"` | `"formatter_not_installed"` |
487///      `"formatter_excluded_path"` | `"timeout"` | `"error"`
488///    - `validate_skipped_reason`: `"unsupported_language"` |
489///      `"no_checker_configured"` | `"checker_not_installed"` |
490///      `"timeout"` | `"error"`
491///
492/// **Anti-patterns to avoid:**
493/// - Returning `success: true` with empty results when the scope didn't
494///   resolve to any files — agent reads as "all clear" but really nothing
495///   was checked. Use `no_files_matched_scope: true` or
496///   `success: false, code: "path_not_found"`.
497/// - Reusing `format_skipped_reason: "not_found"` for two different causes
498///   ("no formatter configured" vs "configured formatter binary missing").
499///   The agent can't act on the ambiguous code.
500///
501/// See ARCHITECTURE.md "Honest reporting convention" for the full rationale.
502#[derive(Debug, Serialize)]
503pub struct Response {
504    pub id: String,
505    pub success: bool,
506    #[serde(flatten)]
507    pub data: serde_json::Value,
508}
509
510/// Parameters for the `echo` command.
511#[derive(Debug, Deserialize)]
512pub struct EchoParams {
513    pub message: String,
514}
515
516impl Response {
517    /// Build a success response with arbitrary data merged at the top level.
518    pub fn success(id: impl Into<String>, data: serde_json::Value) -> Self {
519        Response {
520            id: id.into(),
521            success: true,
522            data,
523        }
524    }
525
526    /// Build an error response with `code` and `message` fields.
527    pub fn error(id: impl Into<String>, code: &str, message: impl Into<String>) -> Self {
528        Response {
529            id: id.into(),
530            success: false,
531            data: serde_json::json!({
532                "code": code,
533                "message": message.into(),
534            }),
535        }
536    }
537
538    /// Build an error response with `code`, `message`, and additional structured data.
539    ///
540    /// The `extra` fields are merged into the top-level response alongside `code` and `message`.
541    pub fn error_with_data(
542        id: impl Into<String>,
543        code: &str,
544        message: impl Into<String>,
545        extra: serde_json::Value,
546    ) -> Self {
547        let mut data = serde_json::json!({
548            "code": code,
549            "message": message.into(),
550        });
551        if let (Some(base), Some(ext)) = (data.as_object_mut(), extra.as_object()) {
552            for (k, v) in ext {
553                base.insert(k.clone(), v.clone());
554            }
555        }
556        Response {
557            id: id.into(),
558            success: false,
559            data,
560        }
561    }
562}