gent-link 0.2.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
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
//! Opt-in attachment to an existing local Codex server. Never starts a server,
//! resumes a thread, overrides its settings, or answers host approval requests.
use crate::integration::{Bindings, wake::Registry};
use anyhow::{Context, Result, ensure};
use futures_util::{SinkExt, StreamExt};
use gent_protocol::{AgentOp, Request, wake::WakeTarget};
use serde_json::{Value, json};
use std::{
    os::unix::fs::{FileTypeExt, MetadataExt},
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::Duration,
};
use tokio::net::UnixStream;
use tokio_tungstenite::{
    WebSocketStream,
    tungstenite::{Message, protocol::WebSocketConfig},
};

const RPC_TIMEOUT: Duration = Duration::from_secs(10);

mod babysit;
mod daemon;
pub(crate) use daemon::maintain_babysitter;
mod direct;
mod ordinary;
pub(crate) mod recovery;

/// Owner diagnostic only. Initialization and a bounded list never load a thread
/// or submit input; returning a count avoids exporting unrelated conversation IDs.
pub(crate) async fn inspect(socket: &Path) -> Result<Value> {
    let mut host = Host::connect(socket).await?;
    let (threads, next) = host.loaded(None).await?;
    Ok(json!({"connected":true,"loaded_threads_on_page":threads.len(),"has_more":next.is_some()}))
}

pub(crate) struct Runner(tokio::task::JoinHandle<()>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Sweep {
    WakeOnly,
    GoalsOnly,
    WakeAndGoals,
}

#[derive(Default)]
struct Attached {
    wake: Option<std::fs::File>,
    goals: Option<std::fs::File>,
}

impl Attached {
    fn work(&mut self, registry: &Registry, home: &Path) -> Result<Option<Sweep>> {
        if self.wake.is_none() {
            self.wake = registry.coordinator()?;
        }
        let goals = self.inspect_goals(registry, home);
        match (self.wake.is_some(), goals) {
            (true, Ok(true)) => Ok(Some(Sweep::WakeAndGoals)),
            // A Babysitter opt-out or election failure must preserve inbox delivery.
            (true, _) => Ok(Some(Sweep::WakeOnly)),
            (false, goals) => Ok(goals?.then_some(Sweep::GoalsOnly)),
        }
    }

    fn inspect_goals(&mut self, registry: &Registry, home: &Path) -> Result<bool> {
        if !babysitter_running(home)? {
            self.goals = None;
        } else if self.goals.is_none() {
            self.goals = registry.attached_babysitter()?;
        }
        // Keep this probe out of the host sweep. The daemon can take ownership
        // even while a standalone fallback is waiting on a slow host.
        Ok(self.goals.is_some() && registry.daemon_babysitter()?.is_some())
    }
}

fn babysitter_running(home: &Path) -> Result<bool> {
    Ok(!home.join("babysitter-disabled").try_exists()?
        || gent_daemon::config::ordinary_babysitter_enabled(home)?)
}

impl Drop for Runner {
    fn drop(&mut self) {
        self.0.abort();
    }
}
impl Runner {
    pub(crate) fn start(
        home: PathBuf,
        socket: PathBuf,
        bindings: Arc<Mutex<Bindings>>,
    ) -> Result<Self> {
        ensure!(
            socket.is_absolute(),
            "Codex wake socket path must be absolute"
        );
        let registry = bindings
            .lock()
            .expect("host bindings lock poisoned")
            .enable_wake(&home, &socket)?;
        let mut attached = Attached::default();
        Ok(Self::spawn(home, registry, move |registry, home| {
            attached.work(registry, home)
        }))
    }

    fn spawn(
        home: PathBuf,
        registry: Registry,
        mut work: impl FnMut(&Registry, &Path) -> Result<Option<Sweep>> + Send + 'static,
    ) -> Self {
        Self(tokio::spawn(async move {
            let mut unavailable = false;
            let mut cursor = None;
            loop {
                let result = async {
                    if let Some(mode) = work(&registry, &home)? {
                        sweep(&home, &registry, &mut cursor, mode).await?;
                    }
                    Ok::<_, anyhow::Error>(())
                }
                .await;
                if result.is_err() && !unavailable {
                    eprintln!(
                        "Gent: Codex endpoint unavailable or submission uncertain. Inbox polling remains available."
                    );
                }
                unavailable = result.is_err();
                tokio::time::sleep(Duration::from_secs(5)).await;
            }
        }))
    }
}

async fn agent(home: &Path, credential: &str, action: AgentOp) -> Result<Value> {
    tokio::time::timeout(
        RPC_TIMEOUT,
        gent_daemon::request(
            home,
            Request::Agent {
                credential: credential.into(),
                action,
            },
        ),
    )
    .await
    .context("Gent RPC timed out")?
}

async fn sweep(
    home: &Path,
    registry: &Registry,
    cursor: &mut Option<String>,
    mode: Sweep,
) -> Result<()> {
    gent_daemon::recovery::ensure_active(home)?;
    let socket = &registry.endpoint;
    let mut host = Host::connect(socket).await?;
    // Cursors belong to the host session. Start over if the host rejects one after a restart.
    let (threads, next) = host.loaded(cursor.take().as_deref()).await?;
    *cursor = next;
    drop(host);
    let mut failed = false;
    for thread in threads {
        // A stale or unavailable conversation must not starve other bindings.
        match registry.credential(&thread) {
            Ok(Some(credential)) => {
                if mode != Sweep::GoalsOnly {
                    failed |= dispatch(home, registry, &thread, &credential, None)
                        .await
                        .is_err();
                }
                if mode != Sweep::WakeOnly {
                    if gent_daemon::config::ordinary_babysitter_enabled(home).unwrap_or(false) {
                        failed |= ordinary::inspect(home, registry, &thread, &credential)
                            .await
                            .is_err()
                            && mode == Sweep::GoalsOnly;
                    }
                    let inspection = babysit::inspect(home, socket, &thread, &credential).await;
                    // Older servers may support inbox delivery without a goal API.
                    failed |= mode == Sweep::GoalsOnly && inspection.is_err();
                }
            }
            Ok(None) => {}
            Err(_) => failed = true,
        }
    }
    ensure!(
        !failed,
        "one or more Codex operations unavailable or uncertain"
    );
    Ok(())
}

/// The daemon reconciles existing managed attachments without taking the MCP
/// wake lease. A current coordinator racing this pass uses the same journal.
pub(crate) async fn catch_up(
    home: &Path,
    socket: &Path,
    thread: &str,
    recipient: &str,
) -> Result<()> {
    let registry = Registry::codex(home, socket)?;
    if registry.coordinator()?.is_none()
        && let Some(credential) = registry.credential(thread)?
    {
        dispatch(home, &registry, thread, &credential, Some(recipient)).await?;
    }
    Ok(())
}

async fn dispatch(
    home: &Path,
    registry: &Registry,
    thread: &str,
    credential: &str,
    recipient: Option<&str>,
) -> Result<()> {
    let socket = &registry.endpoint;
    // The attached wake owner still collects receipts and relay replies. Catch-up
    // handles only new pending messages on a host state that older owners declined.
    let recovery = if recipient.is_some() {
        Ok(())
    } else {
        direct::collect(home, socket, thread, credential).await
    };
    // Registration may still be in flight. Revoked or otherwise unavailable
    // agents leave the pending path without any external host operation.
    let Ok(pending) = agent(home, credential, AgentOp::WakePending).await else {
        return Ok(());
    };
    if recipient.is_some_and(|id| pending["agent"] != id) {
        return Ok(());
    }
    let pending = pending["pending"]
        .as_array()
        .context("invalid wake pending result")?;
    if pending.is_empty() {
        return recovery;
    }
    let mut host = Host::connect(socket).await?;
    let Some(ready) = host.input_ready(thread).await? else {
        return Ok(());
    };
    if recipient.is_some()
        && (ready == InputReady::Normal
            || !crate::integration::refresh::codex_sockets(home)?.contains(socket))
    {
        return Ok(());
    }
    // One claim per agent per sweep. The daemon enforces durable budgets.
    let message = pending[0]["message_id"]
        .as_str()
        .context("invalid pending message")?;
    let attempt = uuid::Uuid::new_v4().to_string();
    let claim = agent(
        home,
        credential,
        AgentOp::WakeDirectClaim {
            message_id: message.into(),
            attempt_id: attempt.clone(),
            target: WakeTarget::Codex {
                thread_id: thread.into(),
            },
        },
    )
    .await;
    let Ok(claim) = claim else { return Ok(()) };
    if claim["may_submit"] != true {
        return Ok(());
    }
    // Any cancellation, error or lost response beyond the committed claim
    // leaves evidence. No external submission is retried here.
    if ready != InputReady::Normal
        && (host.input_ready(thread).await? != Some(ready)
            || registry.credential(thread)?.as_deref() != Some(credential)
            || (recipient.is_some()
                && !crate::integration::refresh::codex_sockets(home)?.contains(socket)))
    {
        return Ok(());
    }
    let checked = agent(
        home,
        credential,
        AgentOp::WakeDirectRead {
            message_id: message.into(),
            attempt_id: attempt.clone(),
            target: WakeTarget::Codex {
                thread_id: thread.into(),
            },
        },
    )
    .await?;
    if checked["eligible"] != true {
        return Ok(());
    }
    let delivery: gent_protocol::wake::WakeDelivery =
        serde_json::from_value(checked["delivery"].clone())?;
    let submission = host.submit(thread, &attempt, &delivery.prompt()).await?;
    agent(
        home,
        credential,
        AgentOp::WakeSubmitted {
            message_id: message.into(),
            attempt_id: attempt,
            submission_id: submission,
        },
    )
    .await?;
    recovery
}

/// Authenticate a local endpoint before any protocol data is sent.
pub(crate) async fn connect_socket(
    path: &Path,
    limit: usize,
) -> Result<WebSocketStream<UnixStream>> {
    // Codex uses a rendezvous symlink to a private socket. Resolve it, then
    // validate the endpoint and the connected OS peer, not a claimed UUID.
    let path = std::fs::canonicalize(path).context("Codex socket unavailable")?;
    let meta = std::fs::symlink_metadata(&path)?;
    let uid = rustix::process::geteuid().as_raw();
    ensure!(
        meta.file_type().is_socket() && meta.uid() == uid && meta.mode() & 0o077 == 0,
        "Codex socket must be private and owned by the current user"
    );
    let parent = std::fs::metadata(path.parent().context("missing socket parent")?)?;
    ensure!(
        parent.is_dir() && parent.uid() == uid && parent.mode() & 0o077 == 0,
        "Codex socket directory must be private and owned by the current user"
    );
    let stream = tokio::time::timeout(RPC_TIMEOUT, UnixStream::connect(path)).await??;
    ensure!(
        stream.peer_cred()?.uid() == uid,
        "Codex socket peer belongs to another user"
    );
    let config = WebSocketConfig::default()
        .max_message_size(Some(limit))
        .max_frame_size(Some(limit));
    let (socket, _) = tokio::time::timeout(
        RPC_TIMEOUT,
        tokio_tungstenite::client_async_with_config("ws://localhost/", stream, Some(config)),
    )
    .await??;
    Ok(socket)
}

#[derive(PartialEq)]
enum InputReady {
    Normal,
    ProviderFailure {
        turn: uuid::Uuid,
        completed: i64,
        reason: gent_protocol::notifications::Failure,
    },
}

pub(crate) struct Host {
    socket: WebSocketStream<UnixStream>,
    sequence: u64,
}
impl Host {
    async fn loaded(&mut self, cursor: Option<&str>) -> Result<(Vec<String>, Option<String>)> {
        let result = self
            .rpc("thread/loaded/list", json!({"limit":128,"cursor":cursor}))
            .await?;
        let ids = result["data"]
            .as_array()
            .context("invalid loaded thread list")?;
        ensure!(ids.len() <= 128, "too many loaded threads in one page");
        let mut threads = Vec::new();
        for id in ids {
            let id = id.as_str().context("invalid loaded thread ID")?;
            let uuid = uuid::Uuid::parse_str(id)?;
            ensure!(
                !uuid.is_nil() && uuid.to_string() == id && !threads.iter().any(|s| s == id),
                "invalid or duplicate loaded thread ID"
            );
            threads.push(id.to_owned());
        }
        let next = match result.get("nextCursor") {
            None | Some(Value::Null) => None,
            Some(Value::String(next))
                if !next.is_empty() && next.len() <= 512 && Some(next.as_str()) != cursor =>
            {
                Some(next.clone())
            }
            _ => anyhow::bail!("invalid loaded thread cursor"),
        };
        Ok((threads, next))
    }

    async fn connect(path: &Path) -> Result<Self> {
        Self::connect_with_limit(path, 1024 * 1024).await
    }

    pub(crate) async fn connect_with_limit(path: &Path, limit: usize) -> Result<Self> {
        let socket = connect_socket(path, limit).await?;
        let mut host = Self {
            socket,
            sequence: 0,
        };
        host.rpc(
            "initialize",
            json!({"clientInfo":{"name":"gent-wake","version":env!("CARGO_PKG_VERSION")},
            "capabilities":{"experimentalApi":true}}),
        )
        .await?;
        tokio::time::timeout(
            RPC_TIMEOUT,
            host.socket.send(Message::Text(
                json!({"method":"initialized"}).to_string().into(),
            )),
        )
        .await??;
        Ok(host)
    }

    pub(crate) async fn rpc(&mut self, method: &str, params: Value) -> Result<Value> {
        self.rpc_with_timeout(method, params, RPC_TIMEOUT).await
    }

    pub(crate) async fn rpc_with_timeout(
        &mut self,
        method: &str,
        params: Value,
        timeout: Duration,
    ) -> Result<Value> {
        self.sequence += 1;
        let id = self.sequence;
        tokio::time::timeout(timeout, async {
            self.socket
                .send(Message::Text(
                    json!({"id":id,"method":method,"params":params})
                        .to_string()
                        .into(),
                ))
                .await?;
            for _ in 0..1024 {
                let frame = self.socket.next().await.context("Codex socket closed")??;
                let text = match frame {
                    Message::Text(text) => text,
                    Message::Ping(_) | Message::Pong(_) => continue,
                    _ => anyhow::bail!("unexpected Codex socket frame"),
                };
                let value: Value = serde_json::from_str(&text)?;
                ensure!(
                    value.get("method").is_none() || value.get("id").is_none(),
                    "Gent cannot handle Codex server requests"
                );
                if value.get("id") == Some(&json!(id)) {
                    ensure!(value.get("error").is_none(), "Codex refused operation");
                    return value
                        .get("result")
                        .cloned()
                        .context("missing Codex response");
                }
            }
            anyhow::bail!("too many Codex notifications")
        })
        .await
        .with_context(|| {
            format!(
                "Codex {method} timed out after {} seconds",
                timeout.as_secs()
            )
        })?
    }

    async fn input_ready(&mut self, thread: &str) -> Result<Option<InputReady>> {
        let value = self
            .rpc(
                "thread/read",
                json!({"threadId":thread,"includeTurns":false}),
            )
            .await?;
        let metadata = &value["thread"];
        ensure!(metadata["id"] == thread, "Codex returned another thread");
        if metadata["canAcceptDirectInput"] != true {
            return Ok(None);
        }
        let status = &metadata["status"];
        match status["type"].as_str() {
            Some("idle" | "active") => return Ok(Some(InputReady::Normal)),
            Some("systemError")
                if status
                    .get("activeFlags")
                    .is_none_or(|flags| flags.as_array().is_some_and(Vec::is_empty)) => {}
            _ => return Ok(None),
        }
        let page = self
            .rpc(
                "thread/turns/list",
                json!({"threadId":thread,
            "sortDirection":"desc","limit":1,"itemsView":"notLoaded"}),
            )
            .await?;
        let Some([head]) = page["data"].as_array().map(Vec::as_slice) else {
            return Ok(None);
        };
        let Some(reason) = babysit::terminal_failure(head) else {
            return Ok(None);
        };
        let Some(id) = head["id"]
            .as_str()
            .filter(|id| crate::integration::wake::valid_thread(id))
        else {
            return Ok(None);
        };
        let Some((started, completed)) =
            head["startedAt"].as_i64().zip(head["completedAt"].as_i64())
        else {
            return Ok(None);
        };
        if !(0..=completed).contains(&started) || completed > gent_daemon::store::now() {
            return Ok(None);
        }
        let queue = self
            .rpc("thread/queue/list", json!({"threadId":thread}))
            .await?;
        Ok(
            (queue["data"].as_array().is_some_and(Vec::is_empty) && queue["nextCursor"].is_null())
                .then_some(InputReady::ProviderFailure {
                    turn: uuid::Uuid::parse_str(id)?,
                    completed,
                    reason,
                }),
        )
    }

    async fn submit(&mut self, thread: &str, attempt: &str, prompt: &str) -> Result<String> {
        // Ordinary native input starts an idle turn or appends to an active
        // one. Do not park it in the next-turn queue, interrupt work, or change
        // the host's model, permission, or execution settings.
        let result = self
            .rpc(
                "turn/start",
                json!({"threadId":thread,"clientUserMessageId":attempt,
            "input":[{"type":"text","text":prompt}]}),
            )
            .await?;
        let id = result["turn"]["id"]
            .as_str()
            .context("missing host turn ID")?;
        let parsed = uuid::Uuid::parse_str(id)?;
        ensure!(
            !parsed.is_nil() && parsed.to_string() == id,
            "invalid host submission ID"
        );
        Ok(id.into())
    }
}

#[cfg(test)]
mod tests;