agent-client-protocol-schema 1.5.0

A protocol for standardizing communication between code editors and AI coding 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Agent-owned terminal output reported for display by clients.

use std::sync::Arc;

use derive_more::{Display, From};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::{DefaultOnError, serde_as, skip_serializing_none};

use super::{AbsolutePath, Meta};
use crate::{IntoMaybeUndefined, IntoOption, MaybeUndefined};

/// Unique identifier for an agent-owned terminal within a session.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, Display, From)]
#[serde(transparent)]
#[from(forward)]
#[non_exhaustive]
pub struct TerminalId(pub Arc<str>);

impl TerminalId {
    /// Wraps a protocol string as a typed [`TerminalId`].
    #[must_use]
    pub fn new(id: impl Into<Self>) -> Self {
        id.into()
    }
}

impl IntoOption<TerminalId> for &str {
    fn into_option(self) -> Option<TerminalId> {
        Some(TerminalId::new(self))
    }
}

/// A display-only reference to an agent-owned terminal.
///
/// Terminal state and output are delivered separately through
/// [`TerminalUpdate`] and [`TerminalOutputChunk`].
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Terminal {
    /// The ID of the terminal to display.
    pub terminal_id: TerminalId,
    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
    /// these keys. This metadata is scoped to the content reference. Omitted
    /// and `null` are equivalent and mean no item metadata was provided.
    ///
    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    #[serde(rename = "_meta")]
    pub meta: Option<Meta>,
}

impl Terminal {
    /// Builds a terminal reference for the given terminal ID.
    #[must_use]
    pub fn new(terminal_id: impl Into<TerminalId>) -> Self {
        Self {
            terminal_id: terminal_id.into(),
            meta: None,
        }
    }

    /// Sets or clears metadata scoped to this content reference.
    #[must_use]
    pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
        self.meta = meta.into_option();
        self
    }
}

/// An authoritative replacement snapshot of terminal output bytes.
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TerminalOutput {
    /// Base64-encoded replacement terminal output bytes.
    #[schemars(extend("contentEncoding" = "base64"))]
    pub data: String,
    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
    /// these keys. This metadata is scoped to the replacement snapshot. Omitted
    /// and `null` are equivalent and mean no snapshot metadata was provided.
    ///
    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    #[serde(rename = "_meta")]
    pub meta: Option<Meta>,
}

impl TerminalOutput {
    /// Builds an authoritative terminal output replacement.
    #[must_use]
    pub fn new(data: impl Into<String>) -> Self {
        Self {
            data: data.into(),
            meta: None,
        }
    }

    /// Sets or clears metadata scoped to this replacement snapshot.
    #[must_use]
    pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
        self.meta = meta.into_option();
        self
    }
}

/// Exit information for an agent-owned terminal.
///
/// The presence of this object marks the terminal as exited, even when neither
/// an exit code nor a signal is known.
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TerminalExitStatus {
    /// Process exit code, when known. Omitted and `null` are equivalent.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    pub exit_code: Option<u32>,
    /// Signal that terminated the process, when known.
    ///
    /// Agents should use the conventional platform signal name. POSIX examples
    /// include `SIGTERM`, `SIGKILL`, and `SIGINT`. Other platforms may use a
    /// platform-specific name. Omitted and `null` are equivalent.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    pub signal: Option<String>,
    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
    /// these keys. This metadata is scoped to the exit information. Omitted
    /// and `null` are equivalent and mean no exit metadata was provided.
    ///
    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    #[serde(rename = "_meta")]
    pub meta: Option<Meta>,
}

impl TerminalExitStatus {
    /// Builds terminal exit information with no known exit code or signal.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets or clears the optional exit code.
    #[must_use]
    pub fn exit_code(mut self, exit_code: impl IntoOption<u32>) -> Self {
        self.exit_code = exit_code.into_option();
        self
    }

    /// Sets or clears the optional terminating signal.
    #[must_use]
    pub fn signal(mut self, signal: impl IntoOption<String>) -> Self {
        self.signal = signal.into_option();
        self
    }

    /// Sets or clears metadata scoped to this exit information.
    #[must_use]
    pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
        self.meta = meta.into_option();
        self
    }
}

/// An upsert for the stored state of an agent-owned terminal.
///
/// Only [`TerminalUpdate::terminal_id`] is required. Other fields have patch
/// semantics: omitted fields leave the stored value unchanged, `null` clears
/// it, and concrete values replace it. When the terminal ID is new, omitted
/// fields start unknown.
#[serde_as]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TerminalUpdate {
    /// Unique identifier for this terminal within the session.
    pub terminal_id: TerminalId,
    /// The command being run.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default, skip_serializing_if = "MaybeUndefined::is_undefined")]
    pub command: MaybeUndefined<String>,
    /// The absolute working directory of the command.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default, skip_serializing_if = "MaybeUndefined::is_undefined")]
    pub cwd: MaybeUndefined<AbsolutePath>,
    /// An authoritative replacement snapshot of terminal output bytes.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default, skip_serializing_if = "MaybeUndefined::is_undefined")]
    pub output: MaybeUndefined<TerminalOutput>,
    /// Exit information. A concrete object marks the terminal as exited.
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default, skip_serializing_if = "MaybeUndefined::is_undefined")]
    pub exit_status: MaybeUndefined<TerminalExitStatus>,
    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
    /// metadata to their interactions. Omitted means no metadata update; `null` is an
    /// explicit clear signal. Implementations MUST NOT make assumptions about values at these keys.
    ///
    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
    #[serde_as(deserialize_as = "DefaultOnError<MaybeUndefined<_>>")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(
        rename = "_meta",
        default,
        skip_serializing_if = "MaybeUndefined::is_undefined"
    )]
    pub meta: MaybeUndefined<Meta>,
}

impl TerminalUpdate {
    /// Builds a terminal upsert with only its required ID set.
    #[must_use]
    pub fn new(terminal_id: impl Into<TerminalId>) -> Self {
        Self {
            terminal_id: terminal_id.into(),
            command: MaybeUndefined::Undefined,
            cwd: MaybeUndefined::Undefined,
            output: MaybeUndefined::Undefined,
            exit_status: MaybeUndefined::Undefined,
            meta: MaybeUndefined::Undefined,
        }
    }

    /// Sets, clears, or leaves unchanged the command being run.
    #[must_use]
    pub fn command(mut self, command: impl IntoMaybeUndefined<String>) -> Self {
        self.command = command.into_maybe_undefined();
        self
    }

    /// Sets, clears, or leaves unchanged the absolute working directory.
    #[must_use]
    pub fn cwd(mut self, cwd: impl IntoMaybeUndefined<AbsolutePath>) -> Self {
        self.cwd = cwd.into_maybe_undefined();
        self
    }

    /// Sets, clears, or leaves unchanged the authoritative output snapshot.
    #[must_use]
    pub fn output(mut self, output: impl IntoMaybeUndefined<TerminalOutput>) -> Self {
        self.output = output.into_maybe_undefined();
        self
    }

    /// Sets, clears, or leaves unchanged the terminal exit information.
    #[must_use]
    pub fn exit_status(mut self, exit_status: impl IntoMaybeUndefined<TerminalExitStatus>) -> Self {
        self.exit_status = exit_status.into_maybe_undefined();
        self
    }

    /// Sets, clears, or leaves unchanged terminal metadata.
    #[must_use]
    pub fn meta(mut self, meta: impl IntoMaybeUndefined<Meta>) -> Self {
        self.meta = meta.into_maybe_undefined();
        self
    }

    /// Applies a later terminal patch to this stored terminal state.
    ///
    /// Fields set to `null` remain `null` so callers can distinguish explicit
    /// clearing from an update that did not mention the field.
    pub fn apply_update(&mut self, update: TerminalUpdate) {
        debug_assert_eq!(self.terminal_id, update.terminal_id);
        if !update.command.is_undefined() {
            self.command = update.command;
        }
        if !update.cwd.is_undefined() {
            self.cwd = update.cwd;
        }
        if !update.output.is_undefined() {
            self.output = update.output;
        }
        if !update.exit_status.is_undefined() {
            self.exit_status = update.exit_status;
        }
        if !update.meta.is_undefined() {
            self.meta = update.meta;
        }
    }
}

/// A chunk of bytes appended to an agent-owned terminal's output.
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TerminalOutputChunk {
    /// The terminal receiving these bytes.
    pub terminal_id: TerminalId,
    /// Independently base64-encoded terminal output bytes.
    #[schemars(extend("contentEncoding" = "base64"))]
    pub data: String,
    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
    /// these keys. This field is chunk-scoped. Omitted and `null` are
    /// equivalent and mean no chunk metadata was provided.
    ///
    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[schemars(extend("x-deserialize-default-on-error" = true))]
    #[serde(default)]
    #[serde(rename = "_meta")]
    pub meta: Option<Meta>,
}

impl TerminalOutputChunk {
    /// Builds a terminal output chunk with the required fields set.
    #[must_use]
    pub fn new(terminal_id: impl Into<TerminalId>, data: impl Into<String>) -> Self {
        Self {
            terminal_id: terminal_id.into(),
            data: data.into(),
            meta: None,
        }
    }

    /// Sets or clears chunk-scoped metadata.
    #[must_use]
    pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
        self.meta = meta.into_option();
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn terminal_reference_meta_is_optional_and_content_scoped() {
        let omitted = Terminal::new("term_1");
        assert_eq!(
            serde_json::to_value(&omitted).unwrap(),
            serde_json::json!({
                "terminalId": "term_1"
            })
        );

        let null: Terminal = serde_json::from_value(serde_json::json!({
            "terminalId": "term_1",
            "_meta": null
        }))
        .unwrap();
        assert_eq!(null.meta, None);

        let mut meta = Meta::new();
        meta.insert("view".to_string(), serde_json::json!("expanded"));
        assert_eq!(
            serde_json::to_value(Terminal::new("term_1").meta(meta)).unwrap(),
            serde_json::json!({
                "terminalId": "term_1",
                "_meta": {
                    "view": "expanded"
                }
            })
        );
    }

    #[test]
    fn terminal_update_distinguishes_omitted_null_and_value() {
        let update = TerminalUpdate::new("term_1")
            .command("cargo test")
            .cwd("/workspace/project")
            .output(None::<TerminalOutput>)
            .exit_status(TerminalExitStatus::new().exit_code(0));

        assert_eq!(
            serde_json::to_value(update).unwrap(),
            serde_json::json!({
                "terminalId": "term_1",
                "command": "cargo test",
                "cwd": "/workspace/project",
                "output": null,
                "exitStatus": {
                    "exitCode": 0
                }
            })
        );

        let parsed: TerminalUpdate = serde_json::from_value(serde_json::json!({
            "terminalId": "term_1",
            "command": null,
            "cwd": "/workspace/project"
        }))
        .unwrap();
        assert_eq!(parsed.command, MaybeUndefined::Null);
        assert_eq!(
            parsed.cwd,
            MaybeUndefined::Value(AbsolutePath::new("/workspace/project"))
        );
        assert_eq!(parsed.output, MaybeUndefined::Undefined);
        assert_eq!(parsed.exit_status, MaybeUndefined::Undefined);
        assert_eq!(parsed.meta, MaybeUndefined::Undefined);
    }

    #[test]
    fn terminal_update_applies_patch_fields() {
        let mut stored = TerminalUpdate::new("term_1")
            .command("cargo test")
            .cwd("/workspace/project")
            .output(TerminalOutput::new("b2xk"));

        stored.apply_update(
            TerminalUpdate::new("term_1")
                .command(None::<String>)
                .output(TerminalOutput::new("bmV3"))
                .exit_status(TerminalExitStatus::new().signal("SIGTERM")),
        );

        assert_eq!(stored.command, MaybeUndefined::Null);
        assert_eq!(
            stored.cwd,
            MaybeUndefined::Value(AbsolutePath::new("/workspace/project"))
        );
        assert_eq!(
            stored.output,
            MaybeUndefined::Value(TerminalOutput::new("bmV3"))
        );
        assert_eq!(
            stored.exit_status,
            MaybeUndefined::Value(TerminalExitStatus::new().signal("SIGTERM"))
        );
    }

    #[test]
    fn terminal_output_chunk_serializes_base64_data_and_meta() {
        let mut meta = Meta::new();
        meta.insert("source".to_string(), serde_json::json!("pty"));

        assert_eq!(
            serde_json::to_value(TerminalOutputChunk::new("term_1", "8J+agA==").meta(meta))
                .unwrap(),
            serde_json::json!({
                "terminalId": "term_1",
                "data": "8J+agA==",
                "_meta": {
                    "source": "pty"
                }
            })
        );
    }

    #[test]
    fn terminal_output_meta_is_optional_and_snapshot_scoped() {
        let omitted = TerminalOutput::new("b2s=");
        assert_eq!(
            serde_json::to_value(&omitted).unwrap(),
            serde_json::json!({ "data": "b2s=" })
        );

        let null: TerminalOutput = serde_json::from_value(serde_json::json!({
            "data": "b2s=",
            "_meta": null
        }))
        .unwrap();
        assert_eq!(null, omitted);

        let mut meta = Meta::new();
        meta.insert("source".to_string(), serde_json::json!("replay"));
        assert_eq!(
            serde_json::to_value(TerminalOutput::new("b2s=").meta(meta)).unwrap(),
            serde_json::json!({
                "data": "b2s=",
                "_meta": {
                    "source": "replay"
                }
            })
        );
    }

    #[test]
    fn terminal_output_fields_use_base64_content_encoding() {
        let output = serde_json::to_value(schemars::schema_for!(TerminalOutput)).unwrap();
        assert_eq!(output["properties"]["data"]["contentEncoding"], "base64");
        assert!(output["properties"]["data"].get("format").is_none());
        assert_eq!(output["required"], serde_json::json!(["data"]));
        assert!(output["properties"].get("_meta").is_some());
        assert!(output["properties"].get("truncated").is_none());

        let chunk = serde_json::to_value(schemars::schema_for!(TerminalOutputChunk)).unwrap();
        assert_eq!(chunk["properties"]["data"]["contentEncoding"], "base64");
        assert!(chunk["properties"]["data"].get("format").is_none());
    }

    #[test]
    fn terminal_exit_status_treats_omitted_and_null_as_unknown() {
        let omitted: TerminalExitStatus = serde_json::from_value(serde_json::json!({})).unwrap();
        let null: TerminalExitStatus = serde_json::from_value(serde_json::json!({
            "exitCode": null,
            "signal": null,
            "_meta": null
        }))
        .unwrap();

        assert_eq!(omitted, TerminalExitStatus::new());
        assert_eq!(null, TerminalExitStatus::new());
        assert_eq!(serde_json::to_value(null).unwrap(), serde_json::json!({}));
    }

    #[test]
    fn terminal_exit_status_meta_is_exit_scoped() {
        let mut meta = Meta::new();
        meta.insert("reason".to_string(), serde_json::json!("timeout"));

        assert_eq!(
            serde_json::to_value(TerminalExitStatus::new().signal("SIGTERM").meta(meta)).unwrap(),
            serde_json::json!({
                "signal": "SIGTERM",
                "_meta": {
                    "reason": "timeout"
                }
            })
        );
    }
}