gent-link 0.1.3

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
//! Compose existing recoverable commands into the daemon installer. Enrollment
//! and login keep their own journals, locks and trust checks.
mod finish;
use crate::{DaemonCommand, auth, cluster, hooks, invitations, managed_file, service};
use anyhow::{Context, Result, bail, ensure};
use clap::Args;
pub(crate) use finish::{after_join, discovery};
use gent_daemon::config;
use serde_json::{Value, json};
use std::{
    io::{self, BufRead, IsTerminal, Read, Write},
    path::{Path, PathBuf},
};

#[derive(Args, Default)]
pub struct Options {
    /// Create a service cluster, or resume creation under this original name.
    #[arg(long = "new", conflicts_with_all = ["local", "invite_file"])]
    new_cluster: Option<String>,
    /// Join using a privately transferred invitation file.
    #[arg(long, conflicts_with = "local")]
    invite_file: Option<PathBuf>,
    /// Name of the joining node (reuse its original name when retrying).
    #[arg(long, requires = "invite_file")]
    name: Option<String>,
    /// Explicitly replace an interrupted join's invitation, preserving its key.
    #[arg(long = "continue", requires = "invite_file")]
    replace: bool,
    /// Alternate account service; never inferred from an invitation file.
    #[arg(long, conflicts_with = "local")]
    service: Option<String>,
    /// Allow GitHub device login when setup needs authentication.
    #[arg(long, conflicts_with_all = ["local", "no_browser", "no_input"])]
    headless: bool,
    /// Allow browser login, printing the URL instead of opening the browser.
    #[arg(long, conflicts_with_all = ["local", "no_input"])]
    no_browser: bool,
    /// Never prompt or start login. Use an existing login and explicit setup flags.
    #[arg(long)]
    no_input: bool,
    /// Linux: keep user services running after logout and start them at boot.
    /// Enables systemd linger for this OS user; may require administrator setup.
    #[arg(long)]
    linger: bool,
    /// Install shell PATH and completion setup after the daemon starts.
    #[arg(long)]
    hooks: bool,
    /// Shell for --hooks (otherwise detected from SHELL).
    #[arg(long, requires = "hooks")]
    shell: Option<hooks::Shell>,
    /// Startup file for --hooks, preserving unrelated content.
    #[arg(long, requires = "hooks")]
    rc_file: Option<PathBuf>,
    /// Set native chat discovery explicitly; otherwise ask during interactive setup.
    #[arg(long, action = clap::ArgAction::Set)]
    discover_chats: Option<bool>,
}

impl Options {
    pub(crate) fn desktop(new_cluster: Option<String>) -> Self {
        Self {
            new_cluster,
            no_input: true,
            ..Self::default()
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
enum Plan {
    Existing,
    Local(String),
    Connect(Option<String>),
    CodeJoin {
        code: String,
    },
    Join {
        file: PathBuf,
        name: String,
        replace: bool,
    },
}

fn answer(reader: &mut impl BufRead, writer: &mut impl Write, prompt: &str) -> Result<String> {
    write!(writer, "{prompt}")?;
    writer.flush()?;
    let mut line = String::new();
    let count = reader.take(4097).read_line(&mut line)?;
    ensure!(
        count > 0,
        "setup cancelled: input closed; rerun gent daemon install"
    );
    ensure!(
        count <= 4096 && line.ends_with('\n'),
        "setup input is too long or incomplete"
    );
    let line = line.strip_suffix('\n').expect("newline checked");
    let line = line.strip_suffix('\r').unwrap_or(line);
    ensure!(
        !line.chars().any(char::is_control),
        "setup input contains control characters"
    );
    Ok(line.trim().into())
}

fn prompt(interactive: bool, text: &str, missing: &str) -> Result<String> {
    ensure!(interactive, "{missing}");
    answer(&mut io::stdin().lock(), &mut io::stderr().lock(), text)
}

pub(crate) fn interactive(human: bool) -> bool {
    human && io::stdin().is_terminal() && io::stderr().is_terminal()
}

fn confirm(
    reader: &mut impl BufRead,
    writer: &mut impl Write,
    text: &str,
    default: bool,
) -> Result<bool> {
    loop {
        let suffix = if default { "[Y/n]" } else { "[y/N]" };
        match answer(reader, writer, &format!("{text} {suffix}: "))?
            .to_ascii_lowercase()
            .as_str()
        {
            "" => return Ok(default),
            "y" | "yes" => return Ok(true),
            "n" | "no" => return Ok(false),
            _ => writeln!(writer, "Please answer y or n.")?,
        }
    }
}

fn ask(text: &str, default: bool) -> Result<bool> {
    confirm(
        &mut io::stdin().lock(),
        &mut io::stderr().lock(),
        text,
        default,
    )
}

fn present(path: &Path) -> Result<bool> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error.into()),
    }
}

fn plan(
    home: &Path,
    local: Option<String>,
    options: &mut Options,
    interactive: bool,
) -> Result<Plan> {
    // Snapshot rejects linked or malformed files even if Path::exists would
    // hide a broken symlink. Loading TOML retains the usual configuration checks.
    let configured = managed_file::Snapshot::read(&config::path(home))?
        .text
        .is_some();
    let cfg = configured.then(|| config::load(home)).transpose()?;
    let service = cluster::installation(home)?;
    let joining = invitations::installation(home)?;
    let staged = present(&config::pending_join(home)?)?;
    let pinned_service = service
        .as_ref()
        .map(|s| s.service.as_str())
        .or_else(|| {
            cfg.as_ref()
                .and_then(|c| c.service.as_ref())
                .map(|s| s.origin.as_str())
        })
        .or_else(|| {
            joining
                .as_ref()
                .map(|j| j.offer.offer.service.account_origin.as_str())
        });
    if let Some(pinned) = pinned_service {
        if let Some(requested) = &options.service {
            ensure!(
                auth::account_pin(requested)? == auth::account_pin(pinned)?,
                "this node is pinned to another service; rerun without --service"
            );
        }
        options.service = Some(pinned.into());
    }
    if let Some(name) = local {
        config::label(&name)?;
        ensure!(
            service.is_none() && cfg.as_ref().is_none_or(|c| c.service.is_none()),
            "this node uses service enrollment; rerun gent daemon install without --local"
        );
        ensure!(
            !staged && joining.is_none(),
            "a node join is pending; rerun the original join command"
        );
        ensure!(
            cfg.as_ref()
                .is_none_or(|c| c.cluster.local_only && c.cluster.name == name),
            "this node already belongs to a cluster; run gent node reset before enrolling in another cluster"
        );
        return Ok(Plan::Local(name));
    }
    if let Some(name) = &options.new_cluster {
        config::label(name)?;
        ensure!(
            !staged && joining.is_none(),
            "this node has invitation or join state; resume its original join"
        );
        if let Some(saved) = &service {
            ensure!(
                saved.owner && saved.name == *name,
                "cluster creation options changed; rerun with the original name or without --new"
            );
            return Ok(if saved.pending {
                Plan::Connect(None)
            } else {
                Plan::Existing
            });
        }
        ensure!(
            !configured,
            "already configured; existing identity was preserved; omit --new"
        );
    }
    if options.invite_file.is_none()
        && options.new_cluster.is_none()
        && !configured
        && let Some((file, name)) = crate::enrollment::pending(home)?
    {
        return Ok(Plan::Join {
            file,
            name,
            replace: false,
        });
    }
    if options.invite_file.is_none() && staged {
        ensure!(
            joining.is_some(),
            "an SSH join is pending; rerun the original gent cluster join command"
        );
        options.invite_file = Some(PathBuf::from(prompt(
            interactive,
            "Original private invitation file: ",
            "a join is pending; rerun gent daemon install --invite-file FILE --name NODE, or the original gent cluster join command",
        )?));
    }
    if options.invite_file.is_none()
        && !configured
        && service.is_none()
        && options.new_cluster.is_none()
    {
        if joining.is_some() {
            options.invite_file = Some(PathBuf::from(prompt(
                interactive,
                "Original private invitation file: ",
                "an invitation join is pending; pass --invite-file FILE and the original --name NODE",
            )?));
        } else {
            if home.exists() {
                ensure!(
                    std::fs::read_dir(home)?.all(|e| e.is_ok_and(|e| matches!(
                        e.file_name().to_str(),
                        Some("auth" | "service.lock" | "daemon.lock")
                    ))),
                    "state exists without config.toml; restore it before installing"
                );
            }
            let choice = prompt(
                interactive,
                "Set up Gent: [1] Create a cluster  [2] Join with a node invitation code  [3] Join with an invitation file  [q] Cancel: ",
                "no Gent configuration; use --new NAME or --invite-file FILE --name NODE after gent auth login, or --local NAME for development",
            )?;
            match choice.as_str() {
                "1" | "new" => options.new_cluster = Some(prompt(true, "Cluster name: ", "")?),
                "2" | "join" => {
                    return Ok(Plan::CodeJoin {
                        code: prompt(true, "Node invitation code: ", "")?,
                    });
                }
                "3" | "invitation" => {
                    options.invite_file = Some(PathBuf::from(prompt(
                        true,
                        "Private invitation file: ",
                        "",
                    )?))
                }
                "q" | "quit" => bail!("setup cancelled; no cluster was created"),
                _ => bail!("choose 1 to create a cluster or 2 to join; rerun gent daemon install"),
            }
        }
    }
    if let Some(file) = &options.invite_file {
        ensure!(
            (!configured && !staged) || joining.is_some(),
            "invitation join journal is missing; existing node state was preserved"
        );
        let invitation = invitations::decode_file(file)?;
        let name = options
            .name
            .clone()
            .or_else(|| joining.as_ref().map(|j| j.name.clone()))
            .map(Ok)
            .unwrap_or_else(|| prompt(interactive, "Node name: ", "joining needs --name NODE"))?;
        config::label(&name)?;
        if let (Some(cfg), Some(saved)) = (&cfg, &joining)
            && saved.complete
            && !staged
        {
            ensure!(
                saved.offer == *invitation.offer()
                    && saved.name == name
                    && saved.offer.offer.anchor.cluster_id == cfg.cluster.id
                    && saved
                        .node
                        .as_ref()
                        .is_some_and(|node| node.id == cfg.node.id),
                "installed node does not match this invitation; existing state was preserved"
            );
            return Ok(if service.as_ref().is_some_and(|s| s.pending) {
                Plan::Connect(None)
            } else {
                Plan::Existing
            });
        }
        return Ok(Plan::Join {
            file: file.clone(),
            name,
            replace: options.replace,
        });
    }
    if let Some(name) = &options.new_cluster {
        config::label(name)?;
        return Ok(Plan::Connect(Some(name.clone())));
    }
    if service.as_ref().is_some_and(|s| s.pending) {
        ensure!(
            service.as_ref().is_some_and(|s| s.owner) || configured,
            "joined configuration is missing; resume its original join or restore it"
        );
        return Ok(Plan::Connect(None));
    }
    ensure!(
        configured,
        "no configuration; use --new NAME or --invite-file FILE --name NODE"
    );
    Ok(Plan::Existing)
}

pub async fn install(
    home: &Path,
    local: Option<String>,
    mut options: Options,
    human: bool,
) -> Result<Value> {
    let home = managed_file::normalize(home)?;
    service::validate_linger(options.linger)?;
    let interactive = interactive(human && !options.no_input);
    if let Some(origin) = &options.service {
        auth::origin(origin)?;
    }
    service::preflight(&home).await?;
    let plan = plan(&home, local, &mut options, interactive)?;
    if options.hooks && options.shell.is_none() {
        options.shell = Some(hooks::Shell::detected(
            std::env::var_os("SHELL").as_deref(),
        )?);
    }
    let needs_login = matches!(
        plan,
        Plan::Connect(_) | Plan::Join { .. } | Plan::CodeJoin { .. }
    );
    if needs_login {
        if interactive || options.headless || options.no_browser {
            if human {
                eprintln!("Checking GitHub sign-in for Gent setup...");
            }
            auth::execute(
                &home,
                auth::Command::Login {
                    headless: options.headless,
                    no_browser: options.no_browser,
                    service: options.service.clone(),
                },
            )
            .await?;
        } else {
            let login = auth::Service::open(&home).await.context(
                "setup needs an active login; run gent auth login, then rerun this command",
            )?;
            if let Some(origin) = &options.service {
                ensure!(
                    login.origin()? == auth::account_pin(origin)?,
                    "sign into the selected Gent service first"
                );
            }
        }
    }
    let mut enrollment = None;
    let local = match plan {
        Plan::Existing => None,
        Plan::CodeJoin { code } => {
            enrollment = Some(
                crate::enrollment::join(&home, {
                    let mut args = crate::pairing::JoinArgs::code(Some(code));
                    args.no_daemon = true;
                    args.no_input = true;
                    args
                })
                .await?,
            );
            None
        }
        Plan::Local(name) => Some(name),
        Plan::Connect(name) => {
            enrollment = Some(cluster::connect(&home, name.as_deref()).await.context(
                "cluster setup is incomplete; rerun this command to preserve its identity",
            )?);
            None
        }
        Plan::Join {
            file,
            name,
            replace,
        } => {
            enrollment = Some(invitations::join(&home, &file, None, &name, replace).await
                .context("invitation setup is incomplete; rerun with the same file and node name; a replacement invitation requires --continue")?);
            None
        }
    };
    let mut result = {
        let _progress = crate::ui::Progress::start("Installing and starting Gent daemon");
        service::execute(
            &home,
            DaemonCommand::Install {
                local,
                setup: Options::default(),
            },
        )
        .await?
    };
    if config::load(&home)?.service.is_some() {
        let status = crate::owner(&home, gent_protocol::OwnerOp::Status).await?;
        let nodes = crate::owner(&home, gent_protocol::OwnerOp::Nodes).await?;
        if status["authority"]["mode"] == "legacy" && nodes.as_array().is_some_and(|n| n.len() == 1)
        {
            crate::peer_configuration::upgrade(&home).await.context(
                "daemon is installed; finish configuration migration with gent cluster upgrade",
            )?;
        }
    }
    if options.linger {
        service::enable_linger()
            .await
            .context("daemon is installed, but startup without login could not be enabled")?;
        result = service::execute(&home, DaemonCommand::Status).await?;
    }
    if let Some(enrollment) = enrollment {
        result["enrollment"] = enrollment;
    }
    finish::run(&home, &mut result, interactive, options)
        .await
        .context("daemon is installed; rerun gent setup to finish setup")?;
    Ok(result)
}

#[cfg(test)]
mod tests;