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
|
//! Stable endpoint compatibility contract for client-owned shells.
//!
//! The endpoint generation is intentionally independent from the private
//! binary protocol used by same-install CLI, direct-terminal, and handoff
//! paths. Generation 1 is the compatibility floor for Local, SSH, and Cloud
//! shell endpoints and must remain available indefinitely unless retired for a
//! security reason. New JSON fields must be optional or have serde defaults;
//! new enum values need an `Unknown` fallback. Unknown named controls are
//! optional and ignored unless negotiated as part of the core.
use serde::{Deserialize, Serialize};
use super::{ClientShellSnapshot, ClientSurfaceSize, ServerMessage};
pub const ENDPOINT_PROTOCOL_GENERATION: u32 = 1;
pub const ENDPOINT_HELLO_KIND: &str = "endpoint.hello.v1";
pub const ENDPOINT_WELCOME_KIND: &str = "endpoint.welcome.v1";
pub const SNAPSHOT_CODEC_V1: &str = "shell.snapshot.v1";
pub const ENDPOINT_SNAPSHOT_KIND: &str = SNAPSHOT_CODEC_V1;
pub const SURFACE_CODEC_V1: &str = "shell.surface.v1";
pub const INPUT_CODEC_V1: &str = "shell.input.semantic.v1";
pub const BLOB_CODEC_V1: &str = "shell.blob.v1";
pub const SURFACE_INTEREST_CAPABILITY: &str = "surface_interest";
pub const PRESENTATION_EFFECTS_FENCE_CAPABILITY: &str = "presentation_effects_fence";
pub const PRESENTATION_EFFECTS_SYNC_KIND: &str = "endpoint.presentation.sync.v1";
pub const PRESENTATION_EFFECTS_READY_KIND: &str = "endpoint.presentation.ready.v1";
pub const HEALTH_CHECK_CAPABILITY: &str = "health_check";
pub const HEALTH_PING_KIND: &str = "endpoint.health.ping.v1";
pub const HEALTH_PONG_KIND: &str = "endpoint.health.pong.v1";
pub const AGENT_VIEW_PROJECTION_CAPABILITY: &str = "agent_view_projection";
pub const AGENT_VIEW_PROJECTION_KIND: &str = "endpoint.agent-view.v1";
pub const AGENT_COMPLETIONS_CAPABILITY: &str = "agent_completions";
pub const AGENT_COMPLETIONS_KIND: &str = "endpoint.agent-completions.v1";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EndpointAgentCompletions {
pub boot_id: String,
pub revision: u64,
pub completions: std::collections::BTreeMap<String, u64>,
}
fn default_true() -> bool {
true
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EndpointClientHello {
pub generation: u32,
pub cell_width_px: u32,
pub cell_height_px: u32,
pub surface_size: ClientSurfaceSize,
pub pixel_mouse: bool,
pub direct_graphics: bool,
pub endpoint_keybindings: bool,
pub mouse_capture: bool,
#[serde(default = "default_true")]
pub surface_active: bool,
/// Accept the optional cell-retaining surface encoding on this connection.
#[serde(default)]
pub surface_reuse: bool,
/// Accept the optional surface-delta encoding on this connection.
#[serde(default)]
pub surface_delta: bool,
/// Accept the optional scroll-aware patch encoding on this connection.
#[serde(default)]
pub surface_scroll: bool,
#[serde(default)]
pub snapshot_codecs: Vec<String>,
#[serde(default)]
pub surface_codecs: Vec<String>,
#[serde(default)]
pub input_codecs: Vec<String>,
#[serde(default)]
pub blob_codecs: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EndpointHandshakeError {
pub code: String,
pub message: String,
}
/// Optional companion to a V1 snapshot. The view payload is intentionally opaque to this
/// stable envelope so clients can ignore view language additions they do not understand.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EndpointAgentViewProjection {
pub boot_id: String,
pub revision: u64,
#[serde(default)]
pub view: Option<serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EndpointServerWelcome {
pub generation: u32,
pub server_version: String,
pub snapshot_codec: String,
pub surface_codec: String,
pub input_codec: String,
pub blob_codec: String,
#[serde(default)]
pub methods: Vec<String>,
#[serde(default)]
pub capabilities: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<EndpointHandshakeError>,
}
pub fn snapshot_message(snapshot: &ClientShellSnapshot) -> serde_json::Result<ServerMessage> {
Ok(ServerMessage::EndpointControl {
kind: ENDPOINT_SNAPSHOT_KIND.into(),
data: serde_json::to_string(snapshot)?,
})
}
pub fn agent_completions_message(
projection: &EndpointAgentCompletions,
) -> serde_json::Result<ServerMessage> {
Ok(ServerMessage::EndpointControl {
kind: AGENT_COMPLETIONS_KIND.into(),
data: serde_json::to_string(projection)?,
})
}
pub fn agent_view_projection_message(
boot_id: &str,
revision: u64,
view: Option<&crate::api::schema::AgentViewSetParams>,
) -> serde_json::Result<ServerMessage> {
let projection = EndpointAgentViewProjection {
boot_id: boot_id.to_owned(),
revision,
view: view.map(serde_json::to_value).transpose()?,
};
Ok(ServerMessage::EndpointControl {
kind: AGENT_VIEW_PROJECTION_KIND.into(),
data: serde_json::to_string(&projection)?,
})
}
impl EndpointClientHello {
pub fn supports_required_codecs(&self) -> bool {
self.snapshot_codecs
.iter()
.any(|codec| codec == SNAPSHOT_CODEC_V1)
&& self
.surface_codecs
.iter()
.any(|codec| codec == SURFACE_CODEC_V1)
&& self
.input_codecs
.iter()
.any(|codec| codec == INPUT_CODEC_V1)
&& self.blob_codecs.iter().any(|codec| codec == BLOB_CODEC_V1)
}
}
impl EndpointServerWelcome {
pub fn compatible(methods: Vec<String>) -> Self {
Self {
generation: ENDPOINT_PROTOCOL_GENERATION,
server_version: crate::build_info::version(),
snapshot_codec: SNAPSHOT_CODEC_V1.into(),
surface_codec: SURFACE_CODEC_V1.into(),
input_codec: INPUT_CODEC_V1.into(),
blob_codec: BLOB_CODEC_V1.into(),
methods,
capabilities: vec![
super::surface_reuse::CAPABILITY.into(),
super::surface_delta::CAPABILITY.into(),
super::surface_scroll::CAPABILITY.into(),
SURFACE_INTEREST_CAPABILITY.into(),
PRESENTATION_EFFECTS_FENCE_CAPABILITY.into(),
HEALTH_CHECK_CAPABILITY.into(),
AGENT_VIEW_PROJECTION_CAPABILITY.into(),
AGENT_COMPLETIONS_CAPABILITY.into(),
],
error: None,
}
}
pub fn incompatible(code: &str, message: impl Into<String>) -> Self {
Self {
generation: ENDPOINT_PROTOCOL_GENERATION,
server_version: crate::build_info::version(),
snapshot_codec: SNAPSHOT_CODEC_V1.into(),
surface_codec: SURFACE_CODEC_V1.into(),
input_codec: INPUT_CODEC_V1.into(),
blob_codec: BLOB_CODEC_V1.into(),
methods: Vec::new(),
capabilities: Vec::new(),
error: Some(EndpointHandshakeError {
code: code.into(),
message: message.into(),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn hello() -> EndpointClientHello {
EndpointClientHello {
generation: ENDPOINT_PROTOCOL_GENERATION,
cell_width_px: 8,
cell_height_px: 16,
surface_size: ClientSurfaceSize { cols: 80, rows: 24 },
pixel_mouse: true,
direct_graphics: false,
endpoint_keybindings: false,
mouse_capture: true,
surface_active: true,
surface_reuse: false,
surface_delta: false,
surface_scroll: false,
snapshot_codecs: vec![SNAPSHOT_CODEC_V1.into()],
surface_codecs: vec![SURFACE_CODEC_V1.into()],
input_codecs: vec![INPUT_CODEC_V1.into()],
blob_codecs: vec![BLOB_CODEC_V1.into()],
}
}
fn snapshot() -> ClientShellSnapshot {
ClientShellSnapshot {
boot_id: "boot".into(),
revision: 1,
config_diagnostic: None,
product_announcement: None,
update_available: None,
update_install_command: "herdr update".into(),
server_keybindings_toml: None,
latest_release_notes_available: false,
integration_updates_available: false,
worktree_directory: String::new(),
release_notes: None,
focused_workspace_id: None,
focused_tab_id: None,
focused_pane_id: None,
tab_bar_right: Vec::new(),
tab_bar_right_separator: String::new(),
agent_view_label: None,
agent_order: Vec::new(),
workspaces: Vec::new(),
tabs: Vec::new(),
panes: Vec::new(),
agents: Vec::new(),
commands: Vec::new(),
}
}
#[test]
fn hello_ignores_future_named_fields() {
let mut value = serde_json::to_value(hello()).unwrap();
value["future_feature"] = serde_json::json!({"enabled": true});
let decoded: EndpointClientHello = serde_json::from_value(value).unwrap();
assert_eq!(decoded, hello());
}
#[test]
fn frozen_generation_one_handshake_decodes() {
let hello: EndpointClientHello = serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/endpoint-hello-v1.json"
)))
.unwrap();
assert!(hello.supports_required_codecs());
let welcome: EndpointServerWelcome = serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/endpoint-welcome-v1.json"
)))
.unwrap();
assert_eq!(welcome.generation, ENDPOINT_PROTOCOL_GENERATION);
assert_eq!(welcome.snapshot_codec, SNAPSHOT_CODEC_V1);
assert_eq!(welcome.surface_codec, SURFACE_CODEC_V1);
assert_eq!(welcome.input_codec, INPUT_CODEC_V1);
assert_eq!(welcome.blob_codec, BLOB_CODEC_V1);
assert!(welcome.capabilities.is_empty());
}
#[test]
fn frozen_generation_one_snapshot_decodes() {
let snapshot: ClientShellSnapshot = serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/endpoint-snapshot-v1.json"
)))
.unwrap();
assert_eq!(snapshot.boot_id, "boot-v1");
assert_eq!(
snapshot.workspaces[0].agent_status,
crate::api::schema::AgentStatus::Unknown
);
}
#[test]
fn snapshot_message_uses_named_json_control() {
let snapshot = snapshot();
let ServerMessage::EndpointControl { kind, data } = snapshot_message(&snapshot).unwrap()
else {
panic!("snapshot should use endpoint control");
};
assert_eq!(kind, ENDPOINT_SNAPSHOT_KIND);
let decoded: ClientShellSnapshot = serde_json::from_str(&data).unwrap();
assert_eq!(decoded, snapshot);
}
#[test]
fn agent_view_projection_is_an_optional_revision_bound_control() {
let view = crate::api::schema::AgentViewSetParams {
source: "example.views".into(),
label: Some("focus".into()),
filter: None,
sort: Vec::new(),
};
let ServerMessage::EndpointControl { kind, data } =
agent_view_projection_message("boot", 7, Some(&view)).unwrap()
else {
panic!("projection should use endpoint control");
};
assert_eq!(kind, AGENT_VIEW_PROJECTION_KIND);
let projection: EndpointAgentViewProjection = serde_json::from_str(&data).unwrap();
assert_eq!(projection.boot_id, "boot");
assert_eq!(projection.revision, 7);
assert_eq!(
projection
.view
.map(serde_json::from_value)
.transpose()
.unwrap(),
Some(view)
);
}
#[test]
fn snapshot_json_tolerates_future_fields_and_command_actions() {
let mut snapshot = match snapshot_message(&snapshot()).unwrap() {
ServerMessage::EndpointControl { data, .. } => {
serde_json::from_str::<serde_json::Value>(&data).unwrap()
}
_ => unreachable!(),
};
snapshot["future_projection"] = serde_json::json!({"enabled": true});
snapshot["commands"] = serde_json::json!([{
"command_id": "future",
"binding_label": "x",
"binding_labels": ["x"],
"action": "FutureAction",
"description": null
}]);
let decoded: ClientShellSnapshot = serde_json::from_value(snapshot).unwrap();
assert_eq!(
decoded.commands[0].action,
crate::protocol::ClientShellCommandAction::Unknown
);
}
#[test]
fn legacy_hello_defaults_to_an_active_surface() {
let mut value = serde_json::to_value(hello()).unwrap();
value.as_object_mut().unwrap().remove("surface_active");
value.as_object_mut().unwrap().remove("surface_reuse");
value.as_object_mut().unwrap().remove("surface_delta");
value.as_object_mut().unwrap().remove("surface_scroll");
let decoded: EndpointClientHello = serde_json::from_value(value).unwrap();
assert!(decoded.surface_active);
assert!(!decoded.surface_reuse);
assert!(!decoded.surface_delta);
assert!(!decoded.surface_scroll);
}
#[test]
fn compatible_server_advertises_endpoint_lifecycle_capabilities() {
let welcome = EndpointServerWelcome::compatible(Vec::new());
assert_eq!(
welcome.capabilities,
vec![
super::super::surface_reuse::CAPABILITY.to_string(),
super::super::surface_delta::CAPABILITY.to_string(),
super::super::surface_scroll::CAPABILITY.to_string(),
SURFACE_INTEREST_CAPABILITY.to_string(),
PRESENTATION_EFFECTS_FENCE_CAPABILITY.to_string(),
HEALTH_CHECK_CAPABILITY.to_string(),
AGENT_VIEW_PROJECTION_CAPABILITY.to_string(),
AGENT_COMPLETIONS_CAPABILITY.to_string(),
]
);
}
#[test]
fn required_codecs_are_explicit() {
let mut value = hello();
assert!(value.supports_required_codecs());
value.snapshot_codecs.clear();
assert!(!value.supports_required_codecs());
let mut value = hello();
value.surface_codecs.clear();
assert!(!value.supports_required_codecs());
let mut value = hello();
value.input_codecs.clear();
assert!(!value.supports_required_codecs());
let mut value = hello();
value.blob_codecs.clear();
assert!(!value.supports_required_codecs());
}
#[test]
fn welcome_ignores_future_named_fields() {
let welcome = EndpointServerWelcome::compatible(vec!["pane.close".into()]);
let mut value = serde_json::to_value(&welcome).unwrap();
value["future_service"] = serde_json::json!("v2");
let decoded: EndpointServerWelcome = serde_json::from_value(value).unwrap();
assert_eq!(decoded, welcome);
}
}
|