firebrick-daemon 0.7.0

The fbkd daemon that manages Firebrick microVM sandboxes for coding agents.
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
//! Zed remote projects for sandboxes.
//!
//! Zed's remote development connects with the system `ssh`, so the sandbox hosts in the SSH
//! config already work. The daemon keeps one `ssh_connections` entry per sandbox in Zed's user
//! settings, with the sandbox's workspace as its project, so the user can pick the sandbox in
//! Remote Projects. The daemon owns the entries whose `host` ends in `.fbk`, and leaves the
//! rest of the file, comments and formatting included, as the user wrote it.

use crate::settings_file::{self, PARSE_OPTIONS};
use jsonc_parser::cst::{CstArray, CstInputValue, CstObject, CstRootNode};
use jsonc_parser::parse_to_value;
use std::io;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Errors of syncing Zed's settings file. The file is left unchanged.
#[derive(Error, Debug)]
pub enum SettingsError {
    /// The settings file couldn't be read, or is a symlink to a file that doesn't exist.
    #[error("failed to read {}", path.display())]
    Read {
        /// The settings file.
        path: PathBuf,
        /// The I/O error.
        source: io::Error,
    },
    /// The settings file isn't valid JSONC, or its root isn't an object.
    #[error("failed to parse {}: {message}", path.display())]
    Parse {
        /// The settings file.
        path: PathBuf,
        /// What's wrong with the file.
        message: String,
    },
    /// `ssh_connections` exists, but isn't an array.
    #[error("{CONNECTIONS_KEY} in {} isn't an array", path.display())]
    ConnectionsNotAnArray {
        /// The settings file.
        path: PathBuf,
    },
    /// An entry of `ssh_connections` isn't an object.
    #[error("{CONNECTIONS_KEY} in {} has an entry that isn't an object", path.display())]
    ConnectionNotAnObject {
        /// The settings file.
        path: PathBuf,
    },
    /// `ssh_connections` appears more than once, so editing one could leave the one Zed reads
    /// unchanged.
    #[error("{CONNECTIONS_KEY} appears more than once in {}", path.display())]
    DuplicateConnectionsKey {
        /// The settings file.
        path: PathBuf,
    },
    /// The settings file couldn't be written.
    #[error("failed to write {}", path.display())]
    Write {
        /// The settings file.
        path: PathBuf,
        /// The I/O error.
        source: io::Error,
    },
}

/// A sandbox as a Zed remote project.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteProject {
    /// SSH host name of the sandbox, e.g. `project.fbk`.
    pub host: String,
    /// Name Zed shows for the connection: the sandbox name.
    pub nickname: String,
    /// Guest path of the sandbox's workspace, e.g. `/workspaces/project`, if it has one.
    pub workspace_path: Option<String>,
}

const CONNECTIONS_KEY: &str = "ssh_connections";
const HOST_KEY: &str = "host";
const HOST_SUFFIX: &str = ".fbk";
const ZED_DIR: &str = "zed";
const SETTINGS_FILE: &str = "settings.json";

/// Returns the directory that holds Zed's `zed` config directory: `$FIREBRICK_EDITOR_CONFIG_ROOT`
/// when it's set, or the directory Zed uses on the platform otherwise.
pub fn config_root() -> Option<PathBuf> {
    settings_file::config_root_or(platform_config_root)
}

/// Returns `$XDG_CONFIG_HOME`, or `~/.config` when it isn't set.
#[cfg(not(target_os = "macos"))]
fn platform_config_root() -> Option<PathBuf> {
    settings_file::xdg_config_home()
}

/// Returns `~/.config`, which Zed uses on macOS regardless of `$XDG_CONFIG_HOME`.
#[cfg(target_os = "macos")]
fn platform_config_root() -> Option<PathBuf> {
    settings_file::home_config()
}

/// Writes one `ssh_connections` entry per project into `<config_root>/zed/settings.json`,
/// rewrites the `*.fbk` entries that differ and removes the stale ones. Does nothing when Zed
/// isn't installed, that is when the `zed` directory doesn't exist.
pub fn sync_settings(config_root: &Path, projects: &[RemoteProject]) -> Result<(), SettingsError> {
    let zed_dir = config_root.join(ZED_DIR);

    if !zed_dir.is_dir() {
        return Ok(());
    }

    let _guard = settings_file::lock();
    let path = zed_dir.join(SETTINGS_FILE);
    let text = settings_file::read(&path).map_err(|source| SettingsError::Read {
        path: path.clone(),
        source,
    })?;

    match update_connections(&path, &text, projects)? {
        Some(updated) => {
            settings_file::write(&path, &updated).map_err(|source| SettingsError::Write {
                path: path.clone(),
                source,
            })
        }
        None => Ok(()),
    }
}

/// Returns the settings text with the projects synced into `ssh_connections`, or `None` when
/// nothing changes.
fn update_connections(
    path: &Path,
    text: &str,
    projects: &[RemoteProject],
) -> Result<Option<String>, SettingsError> {
    let root = CstRootNode::parse(text, &PARSE_OPTIONS).map_err(|err| SettingsError::Parse {
        path: path.to_path_buf(),
        message: err.to_string(),
    })?;

    let Some(connections) = connections_array(&root, path, projects)? else {
        return Ok(None);
    };
    let entries = connection_objects(&connections, path)?;

    let mut missing: Vec<&RemoteProject> = projects.iter().collect();
    missing.sort_by(|a, b| a.host.cmp(&b.host));

    for entry in entries {
        sync_entry(entry, &mut missing);
    }

    for project in missing {
        connections.append(connection_value(project));
    }

    let updated = root.to_string();
    Ok((updated != text).then_some(updated))
}

/// Returns the `ssh_connections` array, creating it when there are projects to add, or `None`
/// when it doesn't exist and there's nothing to add.
fn connections_array(
    root: &CstRootNode,
    path: &Path,
    projects: &[RemoteProject],
) -> Result<Option<CstArray>, SettingsError> {
    let settings = match root.value() {
        None if projects.is_empty() => return Ok(None),
        None => root.object_value_or_set(),
        Some(value) => value.as_object().ok_or_else(|| SettingsError::Parse {
            path: path.to_path_buf(),
            message: "the settings aren't an object".to_string(),
        })?,
    };

    if settings_file::key_count(&settings, CONNECTIONS_KEY) > 1 {
        return Err(SettingsError::DuplicateConnectionsKey {
            path: path.to_path_buf(),
        });
    }

    match settings.get(CONNECTIONS_KEY) {
        None if projects.is_empty() => Ok(None),
        None => Ok(Some(settings.array_value_or_set(CONNECTIONS_KEY))),
        Some(prop) => {
            prop.array_value()
                .map(Some)
                .ok_or_else(|| SettingsError::ConnectionsNotAnArray {
                    path: path.to_path_buf(),
                })
        }
    }
}

/// Returns the entries of `ssh_connections`, or an error when one of them isn't an object.
fn connection_objects(
    connections: &CstArray,
    path: &Path,
) -> Result<Vec<CstObject>, SettingsError> {
    connections
        .elements()
        .iter()
        .map(|element| {
            element
                .as_object()
                .ok_or_else(|| SettingsError::ConnectionNotAnObject {
                    path: path.to_path_buf(),
                })
        })
        .collect()
}

/// Syncs one entry: keeps other hosts, rewrites a firebrick host that is still `missing` when it
/// differs, and removes the stale and duplicate firebrick hosts.
fn sync_entry(entry: CstObject, missing: &mut Vec<&RemoteProject>) {
    let Some(host) = entry_host(&entry).filter(|host| host.ends_with(HOST_SUFFIX)) else {
        return;
    };

    let Some(index) = missing.iter().position(|project| project.host == host) else {
        entry.remove();
        return;
    };

    let project = missing.remove(index);

    if !same_json(&entry.to_string(), &connection_text(project)) {
        entry.replace_with(connection_value(project));
    }
}

/// Returns the entry's `host`, when it's a string.
fn entry_host(entry: &CstObject) -> Option<String> {
    entry
        .get(HOST_KEY)?
        .value()?
        .as_string_lit()?
        .decoded_value()
        .ok()
}

/// Returns the `ssh_connections` entry of a project.
fn connection_value(project: &RemoteProject) -> CstInputValue {
    let projects = match &project.workspace_path {
        Some(workspace_path) => vec![CstInputValue::Object(vec![(
            "paths".to_string(),
            vec![workspace_path.as_str()].into(),
        )])],
        None => Vec::new(),
    };

    CstInputValue::Object(vec![
        (HOST_KEY.to_string(), project.host.as_str().into()),
        ("nickname".to_string(), project.nickname.as_str().into()),
        ("projects".to_string(), CstInputValue::Array(projects)),
    ])
}

/// Returns the `ssh_connections` entry of a project as JSON text.
fn connection_text(project: &RemoteProject) -> String {
    let root = CstRootNode::parse("", &PARSE_OPTIONS);

    root.map(|root| {
        root.set_value(connection_value(project));
        root.to_string()
    })
    .unwrap_or_default()
}

/// Returns whether two JSONC texts hold the same value, ignoring formatting and comments.
fn same_json(a: &str, b: &str) -> bool {
    match (
        parse_to_value(a, &PARSE_OPTIONS),
        parse_to_value(b, &PARSE_OPTIONS),
    ) {
        (Ok(a), Ok(b)) => a == b,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn project(leaf: &str) -> RemoteProject {
        RemoteProject {
            host: format!("{leaf}.fbk"),
            nickname: leaf.to_string(),
            workspace_path: Some(format!("/workspaces/{leaf}")),
        }
    }

    /// Returns a config root with a `zed` directory, and the path of its settings.
    fn zed_root() -> (TempDir, PathBuf) {
        let root = TempDir::new().unwrap();
        fs::create_dir_all(root.path().join("zed")).unwrap();
        let settings = root.path().join("zed/settings.json");

        (root, settings)
    }

    #[test]
    fn adds_projects_and_keeps_comments_other_hosts_and_keys() {
        let (root, settings) = zed_root();
        fs::write(
            &settings,
            "{\n  // my own comment stays\n  \"ui_font_size\": 16,\n  \"ssh_connections\": [\n    \
             { \"host\": \"myserver\", \"projects\": [{ \"paths\": [\"~/code\"] }] },\n  ],\n}\n",
        )
        .unwrap();

        sync_settings(root.path(), &[project("project")]).unwrap();

        let content = fs::read_to_string(&settings).unwrap();
        assert!(
            content.starts_with(
                "{\n  // my own comment stays\n  \"ui_font_size\": 16,\n  \
                 \"ssh_connections\": [\n    \
                 { \"host\": \"myserver\", \"projects\": [{ \"paths\": [\"~/code\"] }] },\n"
            ),
            "{content}"
        );
        assert!(content.ends_with("  ],\n}\n"), "{content}");
        assert!(same_json(
            &content,
            "{\"ui_font_size\": 16, \"ssh_connections\": [\
             {\"host\": \"myserver\", \"projects\": [{\"paths\": [\"~/code\"]}]},\
             {\"host\": \"project.fbk\", \"nickname\": \"project\", \
             \"projects\": [{\"paths\": [\"/workspaces/project\"]}]}]}"
        ));
    }

    #[test]
    fn rewrites_changed_projects_and_removes_stale_ones() {
        let (root, settings) = zed_root();
        fs::write(
            &settings,
            "{\n  \"ssh_connections\": [\n    { \"host\": \"old.fbk\", \"projects\": [] },\n    \
             { \"host\": \"myserver\", \"projects\": [] },\n    \
             { \"host\": \"web.fbk\", \"nickname\": \"stale\", \"projects\": [] }\n  ]\n}\n",
        )
        .unwrap();

        sync_settings(root.path(), &[project("web")]).unwrap();

        let content = fs::read_to_string(&settings).unwrap();
        assert!(same_json(
            &content,
            "{\"ssh_connections\": [{\"host\": \"myserver\", \"projects\": []},\
             {\"host\": \"web.fbk\", \"nickname\": \"web\", \
             \"projects\": [{\"paths\": [\"/workspaces/web\"]}]}]}"
        ));
    }

    #[test]
    fn removes_duplicate_firebrick_entries() {
        let (root, settings) = zed_root();
        let entry = "{\"host\": \"web.fbk\", \"nickname\": \"web\", \
                     \"projects\": [{\"paths\": [\"/workspaces/web\"]}]}";
        fs::write(
            &settings,
            format!("{{\"ssh_connections\": [{entry}, {entry}]}}"),
        )
        .unwrap();

        sync_settings(root.path(), &[project("web")]).unwrap();

        let content = fs::read_to_string(&settings).unwrap();
        assert!(same_json(
            &content,
            &format!("{{\"ssh_connections\": [{entry}]}}")
        ));
    }

    #[test]
    fn writes_empty_projects_without_workspace_path() {
        let (root, settings) = zed_root();
        let project = RemoteProject {
            workspace_path: None,
            ..project("named")
        };

        sync_settings(root.path(), &[project]).unwrap();

        let content = fs::read_to_string(&settings).unwrap();
        assert!(same_json(
            &content,
            "{\"ssh_connections\": [{\"host\": \"named.fbk\", \"nickname\": \"named\", \
             \"projects\": []}]}"
        ));
    }

    #[test]
    fn leaves_up_to_date_file_untouched() {
        let (root, settings) = zed_root();
        let content = "{\"ssh_connections\": [{\n  // mine\n  \"nickname\": \"project\",\n  \
                       \"host\": \"project.fbk\", \
                       \"projects\": [{\"paths\": [\"/workspaces/project\"]}]}]}";
        fs::write(&settings, content).unwrap();

        sync_settings(root.path(), &[project("project")]).unwrap();

        assert_eq!(fs::read_to_string(&settings).unwrap(), content);
    }

    #[test]
    fn creates_missing_settings_file() {
        let (root, settings) = zed_root();

        sync_settings(root.path(), &[project("project")]).unwrap();

        let content = fs::read_to_string(&settings).unwrap();
        assert!(same_json(
            &content,
            "{\"ssh_connections\": [{\"host\": \"project.fbk\", \"nickname\": \"project\", \
             \"projects\": [{\"paths\": [\"/workspaces/project\"]}]}]}"
        ));
    }

    #[test]
    fn leaves_missing_settings_file_alone_without_projects() {
        let (root, settings) = zed_root();

        sync_settings(root.path(), &[]).unwrap();

        assert!(!settings.exists());
    }

    #[test]
    fn skips_zed_when_it_is_not_installed() {
        let root = TempDir::new().unwrap();

        sync_settings(root.path(), &[project("project")]).unwrap();

        assert!(!root.path().join("zed").exists());
    }

    #[test]
    fn leaves_invalid_file_unchanged() {
        let (root, settings) = zed_root();
        let invalid = "{ \"ui_font_size\": 16, oops";
        fs::write(&settings, invalid).unwrap();

        let result = sync_settings(root.path(), &[project("project")]);

        assert!(
            matches!(result, Err(SettingsError::Parse { ref path, .. }) if *path == settings),
            "{result:?}"
        );
        assert_eq!(fs::read_to_string(&settings).unwrap(), invalid);
    }

    #[test]
    fn leaves_file_with_malformed_connections_unchanged() {
        let (root, settings) = zed_root();
        for content in [
            "{ \"ssh_connections\": {} }\n",
            "{ \"ssh_connections\": [{ \"host\": \"old.fbk\" }, \"myserver\"] }\n",
            "{ \"ssh_connections\": [], \"ssh_connections\": [] }\n",
        ] {
            fs::write(&settings, content).unwrap();

            let result = sync_settings(root.path(), &[project("project")]);

            assert!(
                matches!(
                    result,
                    Err(SettingsError::ConnectionsNotAnArray { .. }
                        | SettingsError::ConnectionNotAnObject { .. }
                        | SettingsError::DuplicateConnectionsKey { .. })
                ),
                "{content}: {result:?}"
            );
            assert_eq!(fs::read_to_string(&settings).unwrap(), content);
        }
    }
}