gent-link 0.1.8

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
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
540
541
542
//! Bounded diagnosis with fixed messages and whitelisted evidence. Never echo
//! parser errors, configuration bodies, credentials, agent data or host output.
use crate::{auth, gateway, hooks, integration, node_home, owner, service};
use gent_daemon::config;
use gent_protocol::OwnerOp;
use serde::Serialize;
use serde_json::{Value, json};
use std::{
    fs,
    os::unix::fs::{FileTypeExt, MetadataExt},
    path::{Path, PathBuf},
    time::Duration,
};

#[derive(clap::Args, Default)]
#[group(id = "doctor_options")]
pub(crate) struct Options {
    /// Also check the saved account session at its pinned Gent service (five-second limit).
    #[arg(long)]
    online: bool,
    /// Inspect a nondefault Codex configuration directory.
    #[arg(long)]
    codex_config_dir: Option<PathBuf>,
    /// Inspect a nondefault Claude Code configuration directory.
    #[arg(long)]
    claude_config_dir: Option<PathBuf>,
    #[command(flatten)]
    hooks: hooks::Options,
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum Status {
    Pass,
    Warn,
    Fail,
    Skip,
}
#[derive(Serialize)]
struct Check {
    id: &'static str,
    status: Status,
    summary: &'static str,
    evidence: Value,
    next: Option<Advice>,
}
#[derive(Serialize)]
struct Advice {
    message: &'static str,
    command: Option<Vec<String>>,
}
#[derive(Serialize)]
pub(crate) struct Report {
    version: u8,
    home: Option<PathBuf>,
    cluster_id: Option<String>,
    online: bool,
    checks: Vec<Check>,
}
impl Report {
    fn add(
        &mut self,
        id: &'static str,
        status: Status,
        summary: &'static str,
        evidence: Value,
        advice: Option<(&'static str, &[&str])>,
    ) {
        let next = advice.map(|(message, args)| Advice {
            message,
            command: if args.is_empty()
                || self
                    .home
                    .as_ref()
                    .is_some_and(|p| p.to_str().is_none_or(|s| s.chars().any(char::is_control)))
            {
                None
            } else {
                Some(self.command(args))
            },
        });
        self.checks.push(Check {
            id,
            status,
            summary,
            evidence,
            next,
        });
    }
    fn command(&self, args: &[&str]) -> Vec<String> {
        let mut command = vec!["gent".into()];
        if let Some(home) = &self.home {
            command.extend(["--home".into(), home.to_string_lossy().into_owned()]);
        }
        if let Some(cluster) = &self.cluster_id {
            command.extend(["--cluster".into(), cluster.clone()]);
        }
        command.extend(args.iter().map(|s| (*s).into()));
        command
    }
    pub(crate) fn exit_code(&self) -> i32 {
        if self
            .checks
            .iter()
            .any(|c| matches!(c.status, Status::Warn | Status::Fail))
        {
            3
        } else {
            0
        }
    }
    pub(crate) fn print(&self) {
        crate::ui::heading("Doctor");
        println!(
            "  {}",
            if self.exit_code() == 0 {
                "available checks passed"
            } else {
                "attention needed"
            }
        );
        for check in &self.checks {
            let status = match check.status {
                Status::Pass => "OK",
                Status::Warn => "WARN",
                Status::Fail => "FAIL",
                Status::Skip => "SKIP",
            };
            println!(
                "  {}  {:18}  {}",
                crate::ui::state(&format!("{status:4}")),
                check.id,
                check.summary
            );
            if let Some(home) = check.evidence["integration_home"].as_str() {
                println!("     Installed for: {}", home.escape_debug());
            }
            if let Some(next) = &check.next {
                println!("     {}", next.message);
                if let Some(command) = &next.command {
                    println!(
                        "     {}",
                        command
                            .iter()
                            .map(|s| shell_arg(s))
                            .collect::<Vec<_>>()
                            .join(" ")
                    );
                }
            }
        }
    }
}
fn shell_arg(arg: &str) -> String {
    if arg.chars().any(char::is_control) {
        return format!("{:?}", arg);
    }
    if !arg.is_empty()
        && arg
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b"_./-:".contains(&b))
    {
        arg.into()
    } else {
        format!("'{}'", arg.replace('\'', "'\\''"))
    }
}

pub(crate) async fn run(root: &Path, selected: Option<&str>, options: Options) -> Report {
    let mut report = Report {
        version: 1,
        home: None,
        cluster_id: None,
        online: options.online,
        checks: vec![],
    };
    let resolved = node_home::resolve(root, selected);
    match resolved {
        Ok(home) => {
            report.home = Some(home.clone());
            match node_home::inspect(&home) {
                Ok(cfg) => {
                    report.cluster_id = Some(cfg.cluster.id.clone());
                    let valid = cfg.validate().is_ok();
                    report.add("configuration", if valid { Status::Pass } else { Status::Warn },
                        if valid { "Private cluster configuration is valid." } else { "Local identity is valid but the editable names or policy are invalid." },
                        json!({"node_id":cfg.node.id}), (!valid).then_some(("Validate the local edit before applying it; signed daemon authority is separate.", &["config", "validate"][..])));
                    inspect_node(&mut report, &home, &cfg, options.online).await;
                }
                Err(_) => {
                    let absent = !home.exists();
                    report.add("configuration", Status::Fail, if absent { "Gent is not set up in this directory." } else { "Configuration is missing, invalid or not private." }, json!({}),
                        if absent { Some(("Start guided setup.", &["daemon", "install"])) } else { Some(("Inspect this node's config.toml and file permissions. Preserve existing keys and state.", &[])) });
                }
            }
        }
        Err(_) => report.add("configuration", Status::Fail, "This node cannot be opened safely.", json!({}),
            Some(("Inspect config.toml and directory permissions. Legacy profiles require gent node reset and fresh enrollment.", &[]))),
    }
    let home = report.home.clone();
    if let Some(home) = home.as_deref() {
        for (id, host, directory) in [
            ("codex", integration::Host::Codex, options.codex_config_dir),
            (
                "claude_code",
                integration::Host::ClaudeCode,
                options.claude_config_dir,
            ),
        ] {
            let result = integration::execute(
                home,
                integration::Command::Status {
                    host: Some(host),
                    config_dir: directory.clone(),
                    live: false,
                    host_binary: None,
                },
            );
            inspect_integration(&mut report, id, result.ok());
            let check = report.checks.last_mut().expect("integration check");
            if let Some(command) = check.next.as_mut().and_then(|n| n.command.as_mut())
                && command.iter().any(|arg| arg == "integration")
            {
                command.push(
                    if host == integration::Host::Codex {
                        "codex"
                    } else {
                        "claude-code"
                    }
                    .into(),
                );
                advice_path(check, "--config-dir", directory.as_deref());
            }
        }
    }
    let shell = options.hooks.shell;
    let rc_file = options.hooks.rc_file.clone();
    let result = hooks::execute(hooks::Command::Status(options.hooks));
    match result {
        Ok(value) if value["state"] == "absent" => report.add("shell", Status::Skip, "Optional shell hooks are not installed.", json!({"shell":value["shell"]}), None),
        Ok(value) if value["state"] == "installed" && value["binary_available"] == true => report.add("shell", Status::Pass, "Managed shell hooks refer to an available binary; the current shell was not tested.", json!({"shell":value["shell"]}), None),
        _ => report.add("shell", Status::Warn, "Shell hooks are edited, unavailable or could not be inspected.", json!({}), Some(("Inspect shell setup; use --shell and --rc-file for a nondefault startup file.", &["hooks", "status"]))),
    }
    let check = report.checks.last_mut().expect("shell check");
    if let Some(command) = check.next.as_mut().and_then(|n| n.command.as_mut())
        && let Some(shell) = shell
    {
        command.extend([
            "--shell".into(),
            match shell {
                hooks::Shell::Zsh => "zsh",
                hooks::Shell::Bash => "bash",
                hooks::Shell::Fish => "fish",
            }
            .into(),
        ]);
    }
    advice_path(check, "--rc-file", rc_file.as_deref());
    report
}

fn advice_path(check: &mut Check, flag: &str, path: Option<&Path>) {
    if let Some(path) = path
        && let Some(next) = &mut check.next
    {
        if let Some(text) = path.to_str().filter(|s| !s.chars().any(char::is_control)) {
            if let Some(command) = &mut next.command {
                command.extend([flag.into(), text.into()]);
            }
        } else {
            next.command = None;
        }
    }
}

fn protected_file(path: &Path) -> bool {
    fs::symlink_metadata(path).is_ok_and(|m| {
        m.is_file()
            && m.nlink() == 1
            && m.uid() == rustix::process::geteuid().as_raw()
            && m.mode() & 0o077 == 0
    })
}
fn credential_file(path: &Path) -> bool {
    protected_file(path) && fs::symlink_metadata(path).is_ok_and(|m| m.len() <= 512)
}
async fn inspect_node(report: &mut Report, home: &Path, cfg: &config::Config, online: bool) {
    let owner_ok = credential_file(&home.join("owner.key"))
        && config::read_private(&home.join("owner.key")).is_ok();
    let identity = home.join("identity.key");
    let db = home.join("state.sqlite3");
    let new = cfg.applied_revision.is_none()
        && fs::symlink_metadata(&identity).is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound)
        && fs::symlink_metadata(&db).is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound);
    let state_ok = owner_ok && (new || (protected_file(&identity) && protected_file(&db)));
    report.add("local_state", if !state_ok {Status::Fail} else if new {Status::Skip} else {Status::Pass},
        if !state_ok {"Key or database files are missing, linked or insufficiently protected."} else if new {"The daemon has not initialized endpoint state yet."} else {"Key and database paths are protected; the live probe checks signed state."}, json!({}),
        (!state_ok).then_some(("Preserve this node. Inspect missing files and ownership before restarting; do not generate replacement keys or copy in an older database.", &[][..])));
    let socket = fs::symlink_metadata(home.join("daemon.sock"));
    let socket_ok = socket.as_ref().is_ok_and(|m| {
        m.file_type().is_socket()
            && m.uid() == rustix::process::geteuid().as_raw()
            && m.mode() & 0o077 == 0
    });
    let daemon = async {
        if !state_ok || !socket_ok {
            return None;
        }
        tokio::time::timeout(Duration::from_secs(2), owner(home, OwnerOp::Diagnose))
            .await
            .ok()?
            .ok()
    };
    let manager = async {
        tokio::time::timeout(Duration::from_secs(3), service::diagnose(home))
            .await
            .ok()
            .and_then(Result::ok)
    };
    let (daemon, manager, account) = tokio::join!(
        daemon,
        manager,
        auth::diagnose(home, cfg.service.as_ref(), online)
    );
    let daemon_pid = daemon.as_ref().and_then(|v| v["process_id"].as_u64());
    inspect_daemon(report, daemon, cfg);
    match manager {
        Some(v) if v["installed"] == true && v["available"] == true => report.add(
            "service",
            if v["running"] == true
                && v["binary_available"] == true
                && v["enabled"] == true
                && v["pid"].as_u64().is_some()
                && v["pid"].as_u64() == daemon_pid
            {
                Status::Pass
            } else {
                Status::Warn
            },
            "The managed service registration was inspected.",
            v.clone(),
            (v["running"] != true
                || v["binary_available"] != true
                || v["enabled"] != true
                || v["pid"].as_u64() != daemon_pid
                || daemon_pid.is_none())
            .then_some((
                "Inspect the service before starting or enabling it.",
                &["daemon", "status"][..],
            )),
        ),
        Some(v) if v["installed"] == false && v["loaded"] == false => report.add(
            "service",
            Status::Skip,
            "No managed service is installed for this node; foreground daemons are supported.",
            json!({"available":v["available"]}),
            None,
        ),
        _ => report.add(
            "service",
            Status::Warn,
            "The user service manager or Gent registration is unavailable or inconsistent.",
            json!({}),
            Some((
                "Inspect service status in the user's login session.",
                &["daemon", "status"],
            )),
        ),
    }
    let relay_key_ok = cfg.relay.as_ref().is_none_or(|r| {
        let path = home.join(&r.credential_file);
        credential_file(&path) && config::read_relay_credential(&path).is_ok()
    });
    if !relay_key_ok {
        report.add(
            "relay_credential",
            Status::Fail,
            "The configured relay credential is missing or invalid.",
            json!({}),
            Some((
                "Inspect the credential path and service setup. Preserve encryption keys.",
                &[],
            )),
        );
    }
    inspect_account(report, account.ok(), cfg.service.is_some(), online);
    match gateway::diagnose(home) {
        Ok(v) if v["configured"] == false => report.add("gateway", Status::Skip, "Optional remote MCP gateway is not configured.", json!({}), None),
        Ok(v) if v["clock_ahead"] == false && v["active_clients"].as_u64().is_some_and(|n| n > 0) => report.add("gateway", Status::Pass, "Gateway pins and host grants are valid; listener and provider behavior were not probed.", v, None),
        Ok(v) => report.add("gateway", Status::Warn, "Gateway access is blocked by its clock watermark or has no active host grants.", v, Some(("Check system time and host grant status.", &["gateway", "status"]))),
        Err(_) => report.add("gateway", Status::Fail, "Gateway state or its node/cluster pin is inconsistent.", json!({}), Some(("Preserve the registry and its config pin. Deleting it would lose established chat bindings.", &[]))),
    }
}

fn inspect_daemon(report: &mut Report, value: Option<Value>, cfg: &config::Config) {
    let Some(v) = value else {
        report.add("daemon", Status::Fail, "No authenticated diagnostic response from this node's daemon.", json!({}), Some(("Inspect daemon status and logs before restarting. An older daemon may need an upgrade.", &["daemon", "status"])));
        return;
    };
    if v["version"] != 1 || v["cluster_id"] != cfg.cluster.id || v["node_id"] != cfg.node.id {
        report.add(
            "daemon",
            Status::Fail,
            "The daemon response does not match this node or diagnostic version.",
            json!({}),
            Some((
                "Preserve state and inspect the process serving this node's socket.",
                &[],
            )),
        );
        return;
    }
    report.add(
        "daemon",
        Status::Pass,
        "The daemon responded under local owner authentication.",
        json!({"process_id":v["process_id"]}),
        None,
    );
    let a = &v["authority"];
    let ready = a["fresh"] == true && a["member"] == true && a["clock_ahead"] == false;
    report.add("authority", if ready {Status::Pass} else {Status::Fail}, if ready {"Signed configuration is current and this node is enrolled."} else {"Signed authority is expired, this node is removed, or the clock moved backward."},
        json!({"revision":a["revision"],"expires_at":a["expires_at"],"fresh":a["fresh"],"member":a["member"],"clock_ahead":a["clock_ahead"]}),
        (!ready).then_some(("Check system time and the active owner's renewal/revocation state. Reconnecting cannot override removal.", &[][..])));
    report.add("config_projection", if v["config_saved"] == true {Status::Pass} else {Status::Warn},
        if v["config_saved"] == true {"No committed configuration is waiting for a file write."} else {"Signed configuration is committed but its TOML projection is pending."}, json!({}),
        (v["config_saved"] != true).then_some(("Check free space and node directory permissions; normal daemon maintenance retries the write.", &[][..])));
    if v["relay"]["configured"] != true {
        report.add(
            "relay",
            Status::Skip,
            "This node has no relay configured.",
            json!({}),
            None,
        );
        return;
    }
    let h = &v["relay"]["health"];
    let now = auth::now().unwrap_or(0);
    let recent = h["observed_at"]
        .as_u64()
        .is_some_and(|at| at <= now && now - at <= 90);
    let trouble = h["local_error"] == true || h["retrying_streams"].as_u64().unwrap_or(0) > 0;
    let contact = h["notifications_connected"] == true
        || h["last_success_at"]
            .as_u64()
            .is_some_and(|at| at <= now && now - at <= 90);
    report.add("relay", if trouble || !recent || !contact {Status::Warn} else {Status::Pass},
        if trouble {"Ordered synchronization has pending retries or a local error."} else if !recent {"Relay observations are missing or stale."} else if contact {"Recent relay contact was observed; this does not prove peer delivery or file transfer."} else {"Relay is configured; no successful network contact has been observed yet."},
        json!({"observed_at":h["observed_at"],"last_success_at":h["last_success_at"],"last_failure_at":h["last_failure_at"],"notifications_connected":h["notifications_connected"],"retrying_streams":h["retrying_streams"],"local_error":h["local_error"]}),
        (trouble || !recent || !contact).then_some(("Inspect daemon logs, network access and relay credentials; retry after recovery.", &["daemon", "logs"][..])));
}

fn inspect_account(report: &mut Report, value: Option<Value>, required: bool, online: bool) {
    let Some(v) = value else {
        report.add(
            "account",
            Status::Fail,
            "Saved account state or its node pin is invalid.",
            json!({}),
            Some((
                "Inspect the account source and service/account pins; preserve cluster keys.",
                &[],
            )),
        );
        return;
    };
    let status = v["status"].as_str().unwrap_or("");
    let evidence = json!({"status":v["status"],"source":v["source"],"expires_at":v["expires_at"],"shared":v["shared"]});
    match status {
        "active" => report.add(
            "account",
            Status::Pass,
            "The Gent service accepted the saved account session.",
            evidence,
            None,
        ),
        "cached" => report.add(
            "account",
            Status::Skip,
            "A locally unexpired session is saved; remote validity was not checked.",
            evidence,
            Some((
                "Use --online to check this session without signing in or renewing.",
                &["doctor", "--online"],
            )),
        ),
        "signed_out" if !required => report.add(
            "account",
            Status::Skip,
            "No account session is required for this development node.",
            evidence,
            None,
        ),
        "signed_out" | "expired" | "login_pending" => report.add(
            "account",
            Status::Warn,
            "Account login is missing, expired or unfinished.",
            evidence,
            Some((
                "Complete account login; this does not replace encryption identity.",
                &["auth", "login"],
            )),
        ),
        "revocation_pending" => report.add(
            "account",
            Status::Warn,
            "Local logout is pending service revocation.",
            evidence,
            Some((
                "Retry the pending logout when the service is reachable.",
                &["auth", "logout"],
            )),
        ),
        _ => report.add(
            "account",
            Status::Warn,
            if online {
                "The saved session could not be verified at the service."
            } else {
                "The saved session state could not be classified."
            },
            evidence,
            Some((
                "Check connectivity and service availability, then repeat the diagnostic.",
                &["doctor", "--online"],
            )),
        ),
    }
}

fn inspect_integration(report: &mut Report, id: &'static str, value: Option<Value>) {
    match value {
        Some(v) if v["state"] == "absent" => report.add(id, Status::Skip, "Optional user-scope MCP integration is not installed.", json!({}), None),
        Some(v) if v["state"] == "configured" && v["current_home"] == true && v["binary_available"] == true => report.add(id, Status::Pass, "Managed MCP configuration matches this node; effective GUI/project settings were not tested.", json!({}), None),
        Some(v) if v["state"] == "configured" && v["current_home"] == false => report.add(id, Status::Warn, "This integration points to an obsolete Gent directory.", json!({"integration_home":v["gent_home"]}), Some(("Run gent node reset to retire legacy installations, re-enroll, then reinstall the host integration.", &["node", "reset"]))),
        _ => report.add(id, Status::Warn, "MCP configuration is edited, disabled, incomplete or points to an unavailable binary.", json!({}), Some(("Inspect installation status; preserve unrelated host settings and pending operations.", &["integration", "status"]))),
    }
}

#[cfg(test)]
mod tests;