gent-link 0.2.0

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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Host metadata selects credentials outside model-controlled tool arguments.
//! The local host and OS user are trusted; this is not remote authentication.
mod claude_binding;
pub(crate) mod claude_wake;
pub(crate) mod codex_files;
pub(crate) mod discovery;
mod executable;
mod install;
pub(crate) mod naming;
pub(crate) mod pending;
pub(crate) use install::{refresh, retire};
pub(crate) mod recovery;
pub(crate) mod runtime;
pub(crate) mod vscode;
pub(crate) mod wake;
use anyhow::{Context, Result, ensure};
use clap::{Subcommand, ValueEnum};
use fs2::FileExt;
use gent_daemon::config;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    fs::{self, File, OpenOptions},
    io::Read,
    os::unix::fs::{MetadataExt, OpenOptionsExt},
    path::{Path, PathBuf},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

const TICKET_TTL: u64 = 600;
const MAX_TICKETS: usize = 4096;
const MAX_BINDINGS: usize = 128;

#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Host {
    Codex,
    ClaudeCode,
}
impl Host {
    pub(crate) fn name(self) -> &'static str {
        match self {
            Self::Codex => "codex",
            Self::ClaudeCode => "claude-code",
        }
    }
    fn client(self) -> &'static str {
        match self {
            Self::Codex => "codex-mcp-client",
            Self::ClaudeCode => "claude-code",
        }
    }
}

#[derive(Subcommand)]
pub enum Command {
    /// Refresh installed Gent fragments, preserving paths and host settings.
    Refresh,
    /// Inspect user-scope installation without changing settings.
    Status {
        host: Option<Host>,
        /// Host config directory (CODEX_HOME or CLAUDE_CONFIG_DIR).
        #[arg(long, requires = "host")]
        config_dir: Option<PathBuf>,
        /// Inspect native runtime state without starting or resuming a conversation.
        #[arg(long, requires = "host")]
        live: bool,
        /// Claude executable for live session inspection (defaults to claude).
        #[arg(long, requires = "live")]
        host_binary: Option<PathBuf>,
    },
    /// Start/reuse Codex, or reopen an existing Claude background session.
    Start(runtime::Args),
    /// Select node-local conversations for automatic startup and idle recovery.
    Recovery {
        #[command(subcommand)]
        command: recovery::Command,
    },
    /// Install MCP, conversation identity and automatic incoming-message delivery.
    Install(install::Args),
    /// Remove Gent-managed host entries, retaining conversations and Gent state.
    #[command(alias = "remove")]
    Uninstall(install::Args),
    /// Print MCP configuration and any required host hooks; does not edit settings.
    Config {
        host: Host,
        /// Enable queued delivery (the default; retained for compatibility).
        #[arg(long, conflicts_with = "wake_socket")]
        wake: bool,
        /// Codex config directory for its default wake socket (defaults to CODEX_HOME).
        #[arg(long)]
        config_dir: Option<PathBuf>,
        /// Opt into queued Codex wake through an existing local app-server socket.
        #[arg(long)]
        wake_socket: Option<PathBuf>,
    },
    /// Receive a host's PreToolUse event on stdin. For host configuration only.
    #[command(hide = true)]
    Hook { host: Host },
    /// Local native Claude plugin bridge. Input is bounded JSON on stdin.
    #[command(hide = true)]
    ClaudeWake {
        #[arg(long)]
        config_dir: PathBuf,
    },
    /// Retained Claude UserPromptSubmit command guard. For host settings only.
    #[command(hide = true)]
    ClaudeWakeGuard {
        #[arg(long)]
        config_dir: PathBuf,
    },
    /// Installed VS Code transport. The extension owns all input and approvals.
    #[command(hide = true)]
    CodexIde(vscode::Args),
    /// Transport wrapper launched behind AI Provider Switcher's model adapter.
    #[command(hide = true)]
    CodexWrapper(vscode::Args),
}

pub fn execute(home: &Path, command: Command) -> Result<Value> {
    match command {
        Command::Refresh => refresh::refresh(home),
        Command::ClaudeWake { .. }
        | Command::CodexIde(_)
        | Command::CodexWrapper(_)
        | Command::ClaudeWakeGuard { .. }
        | Command::Start(_)
        | Command::Recovery { .. }
        | Command::Status { live: true, .. } => {
            anyhow::bail!("native bridge requires async dispatch")
        }
        Command::Status {
            host, config_dir, ..
        } => install::status(home, host, config_dir),
        Command::Install(args) => install::change(home, args, true),
        Command::Uninstall(args) => install::change(home, args, false),
        Command::Config {
            host,
            wake: _,
            config_dir,
            wake_socket,
        } => {
            ensure!(
                host == Host::Codex,
                "Claude automatic delivery includes a managed plugin; use gent integration install claude-code"
            );
            let socket = match wake_socket {
                Some(socket) => Some(socket),
                None => Some(install::default_wake_socket(host, config_dir)?),
            };
            configuration(home, host, socket.as_deref())
        }
        Command::Hook { host } => {
            ensure!(
                host == Host::ClaudeCode,
                "this host does not need a binding hook"
            );
            let mut bytes = Vec::new();
            std::io::stdin()
                .take(gent_protocol::MAX_FRAME_BYTES as u64 + 1)
                .read_to_end(&mut bytes)?;
            ensure!(
                bytes.len() <= gent_protocol::MAX_FRAME_BYTES,
                "hook input too large"
            );
            let input: Value = serde_json::from_slice(&bytes).context("invalid hook JSON")?;
            record_hook(home, &input, now()?)?;
            // No permission decision or model context is supplied by this hook.
            Ok(json!({}))
        }
    }
}

/// Reuse the ordinary installer and its ownership journal during guided setup.
pub(crate) fn setup_install(home: &Path, host: Host, vscode: bool) -> Result<Value> {
    install::change(
        home,
        install::Args {
            host,
            config_dir: None,
            wake: false,
            wake_socket: None,
            vscode,
            no_vscode: false,
            vscode_settings: None,
        },
        true,
    )
}

pub(crate) async fn setup_start_codex(home: &Path) -> Result<Value> {
    runtime::start(
        home,
        runtime::Args {
            host: Host::Codex,
            session_id: None,
            config_dir: None,
            host_binary: Some(executable::resolve(Host::Codex)?),
        },
    )
    .await
}

fn configuration(home: &Path, host: Host, wake_socket: Option<&Path>) -> Result<Value> {
    let home = fs::canonicalize(home).context("initialize this Gent node first")?;
    config::load(&home)?;
    let binary = fs::canonicalize(std::env::current_exe()?)?;
    configure_wake(
        render_configuration(&home, &binary, host)?,
        host,
        wake_socket,
    )
}

fn configure_wake(mut fragments: Value, host: Host, socket: Option<&Path>) -> Result<Value> {
    if let Some(socket) = socket {
        ensure!(
            host == Host::Codex,
            "wake sockets are supported only for Codex"
        );
        let path = socket.to_str().context("wake socket path must be UTF-8")?;
        ensure!(
            socket.is_absolute() && !path.chars().any(char::is_control),
            "wake socket path must be absolute and contain no control characters"
        );
        fragments["mcp_servers"]["gent"]["args"]
            .as_array_mut()
            .expect("Codex arguments")
            .extend([json!("--wake-socket"), json!(path)]);
    }
    Ok(fragments)
}

fn render_configuration(home: &Path, binary: &Path, host: Host) -> Result<Value> {
    let binary = binary
        .to_str()
        .context("Gent executable path must be UTF-8")?;
    let home = home.to_str().context("Gent state path must be UTF-8")?;
    let args = ["--home", home, "mcp", "serve", "--host", host.name()];
    match host {
        Host::Codex => {
            Ok(json!({"host":host,"mcp_servers":{"gent":{"command":binary,"args":args}}}))
        }
        Host::ClaudeCode => {
            let command = [binary, "--home", home, "integration", "hook", "claude-code"]
                .map(shell_arg)
                .join(" ");
            Ok(
                json!({"host":host,"mcpServers":{"gent":{"type":"stdio","command":binary,"args":args}},"settings":{"hooks":{"PreToolUse":[{"matcher":"^mcp__gent__gent_","hooks":[{"type":"command","command":command,"timeout":10}]}]}}}),
            )
        }
    }
}
fn shell_arg(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}
fn now() -> Result<u64> {
    Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())
}
fn digest(value: &Value) -> String {
    format!(
        "{:x}",
        Sha256::digest(serde_json::to_vec(value).expect("JSON serializes"))
    )
}
fn identifier(value: Option<&Value>) -> Result<&str> {
    let value = value
        .and_then(Value::as_str)
        .context("host conversation metadata is missing")?;
    ensure!(
        !value.is_empty()
            && value.len() <= 256
            && value
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_')),
        "invalid host conversation metadata"
    );
    Ok(value)
}
fn session_key(session: &str, agent: Option<&str>) -> String {
    digest(&json!(["gent-host-binding-v1", session, agent]))
}
fn scope(home: &Path, host: Host) -> Result<PathBuf> {
    let config = config::load(home)?;
    let bindings = home.join("host-bindings");
    config::private_dir(home)?;
    config::private_dir(&bindings)?;
    let folder = scope_path(home, &config, host);
    config::private_dir(&folder)?;
    Ok(folder)
}

fn scope_path(home: &Path, cfg: &config::Config, host: Host) -> PathBuf {
    home.join("host-bindings")
        .join(digest(&json!([cfg.cluster.id, cfg.node.id, host.name()])))
}

pub(crate) fn recovery_binding(
    home: &Path,
    host: Host,
    conversation: &str,
    subagent: Option<&str>,
) -> Result<PathBuf> {
    identifier(Some(&json!(conversation)))?;
    if let Some(id) = subagent {
        ensure!(
            host == Host::ClaudeCode,
            "Codex child chats use their own conversation IDs"
        );
        identifier(Some(&json!(id)))?;
    }
    Ok(scope(home, host)?.join(format!("{}.key", session_key(conversation, subagent))))
}
fn private_file(path: &Path) -> Result<()> {
    let metadata = fs::symlink_metadata(path)?;
    ensure!(
        metadata.is_file() && metadata.nlink() == 1 && metadata.mode() & 0o077 == 0,
        "host binding state must be a private regular file without links"
    );
    Ok(())
}
fn read_bounded(path: &Path) -> Result<Vec<u8>> {
    private_file(path)?;
    let mut bytes = Vec::new();
    File::open(path)?.take(4097).read_to_end(&mut bytes)?;
    ensure!(bytes.len() <= 4096, "host binding record is too large");
    Ok(bytes)
}
fn lock(path: &Path) -> Result<File> {
    match fs::symlink_metadata(path) {
        Ok(_) => private_file(path)?,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
        Err(e) => return Err(e.into()),
    }
    let file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .mode(0o600)
        .open(path)?;
    let end = std::time::Instant::now() + Duration::from_secs(3);
    loop {
        match file.try_lock_exclusive() {
            Ok(()) => return Ok(file),
            Err(e)
                if e.kind() == std::io::ErrorKind::WouldBlock
                    && std::time::Instant::now() < end =>
            {
                std::thread::sleep(Duration::from_millis(10));
            }
            Err(e) => return Err(e).context("host binding state is busy; retry the call"),
        }
    }
}

struct Binding {
    credential: String,
    _opened: crate::mcp::binding::Opened,
}
impl Binding {
    fn open(path: &Path, cfg: &config::Config) -> Result<Self> {
        let opened = crate::mcp::binding::open(path, cfg, true)?;
        Ok(Self {
            credential: opened.credential.clone(),
            _opened: opened,
        })
    }
}

pub struct Bindings {
    host: Host,
    cfg: config::Config,
    folder: PathBuf,
    active: BTreeMap<String, Binding>,
    wake_endpoint: Option<PathBuf>,
}
impl Bindings {
    pub fn new(home: &Path, host: Host) -> Result<Self> {
        Ok(Self {
            host,
            cfg: config::load(home)?,
            folder: scope(home, host)?,
            active: BTreeMap::new(),
            wake_endpoint: None,
        })
    }
    pub fn credential(
        &mut self,
        client: &str,
        meta: &Value,
        tool: &str,
        arguments: &Value,
    ) -> Result<String> {
        ensure!(
            client == self.host.client(),
            "MCP client does not match the configured host binding mode"
        );
        let key = match self.host {
            // sessionId can identify a tree of forked threads. Only threadId
            // identifies this conversation; never fall back to the tree root.
            Host::Codex => session_key(identifier(meta.get("threadId"))?, None),
            Host::ClaudeCode => self.ticket(meta, tool, arguments)?.binding,
        };
        if !self.active.contains_key(&key) {
            ensure!(
                self.active.len() < MAX_BINDINGS,
                "MCP connection binding limit reached; reconnect the host"
            );
            // Separate lock protects initial credential creation, including two
            // hosts reconnecting before either has opened the binding file.
            let _lock = lock(&self.folder.join("bindings.lock"))?;
            let binding = Binding::open(&self.folder.join(format!("{key}.key")), &self.cfg)?;
            self.active.insert(key.clone(), binding);
        }
        if self.host == Host::Codex
            && let Some(thread) = meta.get("threadId").and_then(Value::as_str)
            && wake::valid_thread(thread)
        {
            wake::remember(
                &self.folder,
                &self.cfg,
                thread,
                &self.active[&key].credential,
                self.wake_endpoint.as_deref(),
            )?;
        }
        Ok(self.active[&key].credential.clone())
    }

    fn ticket(&self, meta: &Value, tool: &str, arguments: &Value) -> Result<Ticket> {
        let id = identifier(meta.get("claudecode/toolUseId"))?;
        let path = self
            .folder
            .join("calls")
            .join(format!("{}.json", digest(&json!(id))));
        let ticket: Ticket = serde_json::from_slice(&read_bounded(&path)
            .context("Gent's Claude Code PreToolUse hook is required for every call; configure or re-enable it")?)?;
        ticket.verify(now()?, tool, arguments)?;
        Ok(ticket)
    }

    /// Called only after credential() authenticated this exact host call.
    pub(crate) fn registration_name(
        &self,
        meta: &Value,
        credential: &str,
        arguments: &Value,
    ) -> Result<naming::Source> {
        let (key, source) = match self.host {
            Host::Codex => {
                let thread = identifier(meta.get("threadId"))?;
                let source = match &self.wake_endpoint {
                    Some(endpoint) if wake::valid_thread(thread) => naming::Source::Codex {
                        endpoint: endpoint.clone(),
                        thread: thread.into(),
                    },
                    _ => naming::Source::Title(None),
                };
                (session_key(thread, None), source)
            }
            Host::ClaudeCode => {
                let ticket = self.ticket(meta, "gent_register", arguments)?;
                (
                    ticket.binding,
                    naming::Source::Title(ticket.registration_name),
                )
            }
        };
        ensure!(
            self.active
                .get(&key)
                .is_some_and(|b| b.credential == credential),
            "host naming binding changed"
        );
        Ok(source)
    }

    pub(crate) fn enable_wake(&mut self, home: &Path, endpoint: &Path) -> Result<wake::Registry> {
        ensure!(
            self.host == Host::Codex,
            "queued wake requires Codex bindings"
        );
        let registry = wake::Registry::new(home, self.folder.clone(), self.cfg.clone(), endpoint)?;
        self.wake_endpoint = Some(registry.endpoint.clone());
        Ok(registry)
    }

    pub(crate) fn has_host_files(&self) -> bool {
        self.host == Host::Codex && self.wake_endpoint.is_some()
    }

    /// Only called after credential() checked this request's host metadata.
    /// Neither the endpoint nor the conversation can come from tool arguments.
    pub(crate) fn host_files(&self, meta: &Value, credential: &str) -> Result<codex_files::Source> {
        ensure!(self.has_host_files(), "native attachments are unavailable");
        let thread = identifier(meta.get("threadId"))?;
        ensure!(wake::valid_thread(thread), "invalid Codex thread");
        ensure!(
            self.active
                .get(&session_key(thread, None))
                .is_some_and(|b| b.credential == credential),
            "native attachment binding changed"
        );
        Ok(codex_files::Source::new(
            self.wake_endpoint.clone().expect("checked endpoint"),
            thread.into(),
            credential.into(),
        ))
    }
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Ticket {
    version: u8,
    created_at: u64,
    binding: String,
    call: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    registration_name: Option<String>,
}
impl Ticket {
    fn verify(&self, time: u64, tool: &str, arguments: &Value) -> Result<()> {
        ensure!(
            self.version == 1 && self.created_at <= time && time - self.created_at <= TICKET_TTL,
            "host call handoff expired; retry through the host hook"
        );
        ensure!(
            self.binding.len() == 64 && self.binding.bytes().all(|b| b.is_ascii_hexdigit()),
            "invalid host binding record"
        );
        ensure!(
            self.call == digest(&json!([tool, arguments])),
            "host call does not match its hook handoff"
        );
        if let Some(name) = &self.registration_name {
            config::label(name)?;
            ensure!(
                tool == "gent_register" && arguments.get("name").is_none(),
                "unexpected registration title"
            );
        }
        Ok(())
    }
}

fn record_hook(home: &Path, input: &Value, time: u64) -> Result<()> {
    ensure!(
        input["hook_event_name"] == "PreToolUse",
        "expected a PreToolUse event"
    );
    let session = identifier(input.get("session_id"))?;
    let agent = input
        .get("agent_id")
        .map(|value| identifier(Some(value)))
        .transpose()?;
    let id = identifier(input.get("tool_use_id"))?;
    let tool = input["tool_name"]
        .as_str()
        .and_then(|t| t.strip_prefix("mcp__gent__"))
        .filter(|t| t.starts_with("gent_"))
        .context("expected a Gent tool call")?;
    let arguments = input
        .get("tool_input")
        .filter(|v| v.is_object())
        .context("expected tool arguments")?;
    let ticket = Ticket {
        version: 1,
        created_at: time,
        binding: session_key(session, agent),
        call: digest(&json!([tool, arguments])),
        registration_name: (tool == "gent_register" && arguments.get("name").is_none())
            .then(|| naming::claude_title(input))
            .flatten(),
    };
    let folder = scope(home, Host::ClaudeCode)?.join("calls");
    config::private_dir(&folder)?;
    let _lock = lock(&folder.join("calls.lock"))?;
    let path = folder.join(format!("{}.json", digest(&json!(id))));
    match fs::symlink_metadata(&path) {
        Ok(_) => {
            let existing: Ticket = serde_json::from_slice(&read_bounded(&path)?)?;
            ensure!(
                existing.binding == ticket.binding && existing.call == ticket.call,
                "conflicting host call handoff"
            );
            existing.verify(time, tool, arguments)?;
            return Ok(());
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(error.into()),
    }
    let mut count = 0;
    for entry in fs::read_dir(&folder)?.take(MAX_TICKETS + 2) {
        let entry = entry?;
        if entry.file_name() == "calls.lock" {
            continue;
        }
        let stale = fs::symlink_metadata(entry.path())?
            .modified()?
            .elapsed()
            .is_ok_and(|elapsed| elapsed.as_secs() > TICKET_TTL);
        if stale {
            private_file(&entry.path())?;
            fs::remove_file(entry.path())?;
        } else {
            count += 1;
        }
    }
    ensure!(
        count < MAX_TICKETS,
        "too many pending host call handoffs; retry after older calls expire"
    );
    // Only hashes, a timestamp and an optional registration title persist.
    // Never write tool inputs, session IDs, credentials or transcripts here.
    config::write_private(&path, &serde_json::to_vec(&ticket)?)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    fn home() -> tempfile::TempDir {
        use std::os::unix::fs::PermissionsExt;
        let folder = tempfile::tempdir().unwrap();
        fs::set_permissions(folder.path(), fs::Permissions::from_mode(0o700)).unwrap();
        config::initialize(folder.path(), "bindings").unwrap();
        folder
    }
    #[test]
    fn codex_threads_are_isolated_and_resume_without_model_selected_identity() {
        let home = home();
        let mut bindings = Bindings::new(home.path(), Host::Codex).unwrap();
        let call = |b: &mut Bindings, thread| {
            b.credential(
                "codex-mcp-client",
                &json!({"threadId":thread,"sessionId":"shared-root"}),
                "gent_register",
                &json!({}),
            )
        };
        let first = call(&mut bindings, "thread-a").unwrap();
        let second = call(&mut bindings, "thread-b").unwrap();
        assert_ne!(first, second);
        assert_eq!(first, call(&mut bindings, "thread-a").unwrap());
        assert!(
            bindings
                .credential(
                    "codex-mcp-client",
                    &json!({"sessionId":"shared-root"}),
                    "gent_register",
                    &json!({"threadId":"thread-a"})
                )
                .is_err()
        );
        assert!(
            bindings
                .credential(
                    "other",
                    &json!({"threadId":"thread-a"}),
                    "gent_identity",
                    &json!({})
                )
                .is_err()
        );
        assert!(call(&mut bindings, "../escape").is_err());
        let mut duplicate = Bindings::new(home.path(), Host::Codex).unwrap();
        assert!(call(&mut duplicate, "thread-a").is_err());
        drop(bindings);
        assert_eq!(first, call(&mut duplicate, "thread-a").unwrap());
        assert_ne!(
            scope(home.path(), Host::Codex).unwrap(),
            scope(home.path(), Host::ClaudeCode).unwrap()
        );
    }
    #[test]
    fn claude_handoff_is_per_call_with_session_and_subagent_isolation() {
        let home = home();
        let time = now().unwrap();
        let mut bindings = Bindings::new(home.path(), Host::ClaudeCode).unwrap();
        let mut input = json!({"hook_event_name":"PreToolUse","session_id":"old-chat","tool_use_id":"use-a","tool_name":"mcp__gent__gent_register","tool_input":{"name":"private content","provider":"claude-code"}});
        let call = |b: &mut Bindings, id| {
            b.credential(
                "claude-code",
                &json!({"claudecode/toolUseId":id}),
                "gent_register",
                &input["tool_input"],
            )
        };
        assert!(call(&mut bindings, "use-a").is_err());
        record_hook(home.path(), &input, time).unwrap();
        let a = call(&mut bindings, "use-a").unwrap();
        record_hook(home.path(), &input, time).unwrap();
        assert_eq!(a, call(&mut bindings, "use-a").unwrap());
        assert!(
            bindings
                .credential(
                    "claude-code",
                    &json!({"claudecode/toolUseId":"use-a"}),
                    "gent_inbox",
                    &json!({})
                )
                .is_err()
        );
        input["session_id"] = "new-chat".into();
        assert!(record_hook(home.path(), &input, time).is_err());
        input["tool_use_id"] = "use-b".into();
        record_hook(home.path(), &input, time).unwrap();
        let b = bindings
            .credential(
                "claude-code",
                &json!({"claudecode/toolUseId":"use-b"}),
                "gent_register",
                &input["tool_input"],
            )
            .unwrap();
        assert_ne!(a, b);
        input["agent_id"] = "child".into();
        input["tool_use_id"] = "use-c".into();
        record_hook(home.path(), &input, time).unwrap();
        assert_ne!(
            b,
            bindings
                .credential(
                    "claude-code",
                    &json!({"claudecode/toolUseId":"use-c"}),
                    "gent_register",
                    &input["tool_input"]
                )
                .unwrap()
        );
        let path = bindings
            .folder
            .join("calls")
            .join(format!("{}.json", digest(&json!("use-a"))));
        let bytes = read_bounded(&path).unwrap();
        let text = String::from_utf8(bytes.clone()).unwrap();
        assert!(
            !text.contains("private content") && !text.contains("old-chat") && !text.contains(&a)
        );
        let ticket: Ticket = serde_json::from_slice(&bytes).unwrap();
        assert!(
            ticket
                .verify(time + TICKET_TTL + 1, "gent_register", &input["tool_input"])
                .is_err()
        );
        assert!(
            ticket
                .verify(time - 1, "gent_register", &input["tool_input"])
                .is_err()
        );
        input["tool_use_id"] = "linked".into();
        let path = bindings
            .folder
            .join("calls")
            .join(format!("{}.json", digest(&json!("linked"))));
        std::os::unix::fs::symlink(home.path().join("absent"), &path).unwrap();
        assert!(record_hook(home.path(), &input, time).is_err());
        assert!(fs::symlink_metadata(path).unwrap().file_type().is_symlink());
    }
    #[test]
    fn host_state_rejects_shared_symlink_and_hard_link_files() {
        use std::os::unix::fs::PermissionsExt;
        let home = home();
        let path = home.path().join("credential");
        let cfg = config::load(home.path()).unwrap();
        let binding = Binding::open(&path, &cfg).unwrap();
        drop(binding);
        fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
        assert!(Binding::open(&path, &cfg).is_err());
        fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
        let link = home.path().join("link");
        std::os::unix::fs::symlink(&path, &link).unwrap();
        assert!(Binding::open(&link, &cfg).is_err());
        fs::remove_file(&link).unwrap();
        fs::hard_link(&path, &link).unwrap();
        assert!(Binding::open(&path, &cfg).is_err());
        assert_eq!(shell_arg("a '$HOME"), "'a '\"'\"'$HOME'");
    }
}