gent-link 0.2.5

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
use super::*;
use gent_protocol::web_failover::Checkpoint;
use std::pin::Pin;
use tokio::sync::Mutex;
use tokio::time::{Instant, timeout_at};

pub(crate) struct Session {
    pub(super) client: Arc<Client>,
    home: PathBuf,
    pub(crate) epoch: u64,
    pub(crate) passive: bool,
    pub(super) lost: CancellationToken,
    grant: Mutex<Option<Grant>>,
    pub(crate) files: Arc<std::fs::File>,
    checkpoint: Mutex<Publication>,
    management: std::sync::RwLock<Option<String>>,
}

#[derive(Default)]
struct Publication {
    checkpoint: Option<Checkpoint>,
    synchronized: bool,
    committed: Option<(u64, String)>,
}

#[derive(Clone, Copy)]
struct Grant {
    until: Instant,
    expires_at: u64,
}

impl Grant {
    fn reusable(self, refreshing: Option<Instant>, now: Instant, wall: u64) -> bool {
        self.until > now + Duration::from_secs(5)
            && self.expires_at > wall.saturating_add(5)
            && refreshing.is_none_or(|previous| self.until > previous)
    }
}

impl Session {
    pub(super) fn new(
        client: Arc<Client>,
        home: PathBuf,
        lease: &Lease,
        passive: bool,
        files: Arc<std::fs::File>,
    ) -> Self {
        Self {
            client,
            home,
            epoch: lease.epoch,
            passive,
            lost: CancellationToken::new(),
            grant: Mutex::new(None),
            files,
            checkpoint: Mutex::new(Publication {
                checkpoint: lease.checkpoint.clone(),
                ..Publication::default()
            }),
            management: std::sync::RwLock::new(None),
        }
    }
    pub(crate) async fn connector(
        &self,
        action: gent_protocol::hosted_mcp::ConnectorAction,
    ) -> Result<String> {
        let cfg = node_home::inspect(&self.home)?;
        let base = gent_protocol::hosted_mcp::node_base(&cfg.cluster.id, &cfg.node.id)
            .context("invalid web node identity")?;
        let result = gateway::registry::capability::manage(
            &self.home,
            &format!("{}{base}", origin(&self.home)?),
            &action,
        )?;
        self.flush().await?;
        Ok(result)
    }

    pub(crate) fn credential(&self) -> Result<String> {
        self.client.credential()
    }
    pub(crate) fn management(&self) -> Option<String> {
        self.management.read().ok().and_then(|g| g.clone())
    }

    pub(crate) fn disconnected(&self) {
        self.lost.cancel();
    }

    // A quiet passive socket conveys reachability only. Obtain a signed
    // epoch grant before dispatch, then keep it live only while work is pending.
    pub(crate) async fn work<T>(
        &self,
        operation: Pin<Box<impl Future<Output = Result<T>>>>,
    ) -> Result<T> {
        if !self.passive {
            return operation.await;
        }
        let grants = async {
            let until = self.grant(None).await.inspect_err(|_| self.lost.cancel())?;
            // Management can mutate the registry before making a hosted-agent
            // RPC. Recheck the local route and epoch before entering it.
            timeout_at(until, self.current()).await??;
            self.while_granted(until, operation).await
        };
        tokio::select! {
            biased;
            _ = self.lost.cancelled() => anyhow::bail!("web carrier grant lost"),
            result = grants => result,
        }
    }

    pub(super) async fn while_granted<T>(
        &self,
        mut until: Instant,
        operation: Pin<Box<impl Future<Output = Result<T>>>>,
    ) -> Result<T> {
        let refresh = async {
            loop {
                // A legacy carrier keeps its existing five-second heartbeat.
                let pause = if self.passive { 10 } else { 5 };
                let next = (Instant::now() + Duration::from_secs(pause))
                    .min(until - Duration::from_secs(2));
                tokio::time::sleep_until(next).await;
                match timeout_at(until, self.grant(Some(until))).await {
                    Ok(Ok(next)) => until = next,
                    Ok(Err(_))
                        if !self.passive && Instant::now() + Duration::from_secs(2) < until =>
                    {
                        continue;
                    }
                    Ok(Err(error)) => return error,
                    Err(_) => return anyhow::anyhow!("web work grant expired"),
                }
            }
        };
        let result = tokio::select! {
            biased;
            error = refresh => { self.lost.cancel(); Err(error) },
            result = operation => result,
        };
        let value = result?;
        ensure!(
            Instant::now() < until && !self.lost.is_cancelled(),
            "web carrier grant expired"
        );
        Ok(value)
    }

    async fn grant(&self, refreshing: Option<Instant>) -> Result<Instant> {
        let mut cached = self.grant.lock().await;
        ensure!(!self.lost.is_cancelled(), "web carrier grant lost");
        let started = crate::auth::now()?;
        if let Some(grant) = *cached
            && grant.reusable(refreshing, Instant::now(), started)
        {
            return Ok(grant.until);
        }
        let lease: Lease = self
            .client
            .required(&Control::Renew { epoch: self.epoch })
            .await?;
        let grant = self.install(&lease, started).await?;
        *cached = Some(grant);
        Ok(grant.until)
    }

    pub(super) async fn acquired(&self, lease: &Lease, started: u64) -> Result<Instant> {
        let grant = self.install(lease, started).await?;
        *self.grant.lock().await = Some(grant);
        Ok(grant.until)
    }

    async fn install(&self, lease: &Lease, started: u64) -> Result<Grant> {
        ensure!(
            !lease.revoked
                && lease.epoch == self.epoch
                && lease.owner == node_home::inspect(&self.client.parent)?.node.id,
            "web carrier grant changed"
        );
        let expires_at = lease.expires_at.min(started.saturating_add(20));
        let remaining = expires_at.saturating_sub(crate::auth::now()?);
        ensure!(remaining > 3, "web carrier grant expired");
        // Reserve one second for the Unix clock's fractional part.
        let until = Instant::now() + Duration::from_secs(remaining - 1);
        timeout_at(
            until,
            owner(
                &self.client.parent,
                OwnerOp::WebGrant {
                    node_id: self.client.node.clone(),
                    epoch: self.epoch,
                    expires_at,
                },
            ),
        )
        .await??;
        ensure!(
            expires_at > crate::auth::now()? && until > Instant::now(),
            "local web grant expired"
        );
        Ok(Grant { until, expires_at })
    }

    async fn current(&self) -> Result<Value> {
        let state = owner(
            &self.client.parent,
            OwnerOp::WebCheckpoint {
                node_id: self.client.node.clone(),
            },
        )
        .await?;
        ensure!(
            state["hosting_here"] == true
                && state["epoch"].as_u64() == Some(self.epoch)
                && state["lease_until"]
                    .as_u64()
                    .is_some_and(|until| until > crate::auth::now().unwrap_or(u64::MAX)),
            "web carrier lease changed"
        );
        Ok(state)
    }

    pub(crate) async fn flush(&self) -> Result<()> {
        let mut previous = self.checkpoint.lock().await;
        let state = self.current().await?;
        let revision = state["revision"].as_u64().context("web revision missing")?;
        let home = self.home.clone();
        let files = self.files.clone();
        let registry_hash = tokio::task::spawn_blocking(move || {
            let _guard = files;
            gateway::registry::portable::capture(&home)?.change_hash()
        })
        .await??;
        if previous.synchronized
            && previous.committed.as_ref() == Some(&(revision, registry_hash))
            && state["durable"]
                .as_u64()
                .is_some_and(|durable| durable >= revision)
        {
            return Ok(());
        }
        if !previous.synchronized {
            // Inspect only after acquisition or an uncertain publication. An
            // identical retry can recover a commit whose response was lost.
            let current: Lease = self.client.required(&Control::Inspect).await?;
            ensure!(
                current.epoch == self.epoch
                    && current.owner == node_home::inspect(&self.client.parent)?.node.id
                    && !current.revoked,
                "web carrier lease changed"
            );
            previous.checkpoint = current.checkpoint;
            previous.synchronized = true;
        }
        let parent = self.client.parent.clone();
        let home = self.home.clone();
        let node = self.client.node.clone();
        let epoch = self.epoch;
        let files = self.files.clone();
        let (bytes, revision, management, registry_hash) =
            tokio::task::spawn_blocking(move || -> Result<_> {
                let _guard = files;
                // Registry bindings precede daemon registration. Capture the store
                // first and the registry second so every stored chat has its handle.
                let snapshot = gent_daemon::hosted::checkpoint_for_epoch(&parent, &node, epoch)?;
                let revision = snapshot.revision;
                let registry = gateway::registry::portable::capture(&home)?;
                let management = registry.management.clone();
                let registry_hash = registry.change_hash()?;
                let bytes = serde_json::to_vec(&Bundle {
                    version: 1,
                    snapshot,
                    registry,
                })?;
                ensure!(
                    bytes.len() <= CHUNK_BYTES * MAX_CHUNKS,
                    "web checkpoint capacity reached"
                );
                Ok((bytes, revision, management, registry_hash))
            })
            .await??;
        let sha256 = format!("{:x}", Sha256::digest(&bytes));
        if !previous
            .checkpoint
            .as_ref()
            .is_some_and(|c| c.sha256 == sha256)
        {
            let checkpoint = Checkpoint {
                generation: previous.checkpoint.as_ref().map_or(1, |c| c.generation + 1),
                chunks: bytes.len().div_ceil(CHUNK_BYTES),
                bytes: bytes.len(),
                sha256,
            };
            // Mark uncertainty before sending: cancellation after cloud commit
            // must force an Inspect on the next attempt.
            previous.synchronized = false;
            let lease: Lease = if checkpoint.chunks <= MAX_BATCH {
                self.client
                    .required(&Control::Publish {
                        epoch: self.epoch,
                        checkpoint: checkpoint.clone(),
                        chunks: bytes
                            .chunks(CHUNK_BYTES)
                            .map(|p| STANDARD.encode(p))
                            .collect(),
                    })
                    .await?
            } else {
                for (batch, parts) in bytes.chunks(CHUNK_BYTES * MAX_BATCH).enumerate() {
                    let (status, _) = self
                        .client
                        .call(&Control::Stage {
                            epoch: self.epoch,
                            generation: checkpoint.generation,
                            start: batch * MAX_BATCH,
                            chunks: parts
                                .chunks(CHUNK_BYTES)
                                .map(|p| STANDARD.encode(p))
                                .collect(),
                        })
                        .await?;
                    ensure!(status == 200, "web checkpoint staging failed ({status})");
                }
                self.client
                    .required(&Control::Commit {
                        epoch: self.epoch,
                        checkpoint: checkpoint.clone(),
                    })
                    .await?
            };
            ensure!(
                lease.checkpoint.as_ref() == Some(&checkpoint),
                "web checkpoint commit changed"
            );
            previous.checkpoint = Some(checkpoint);
            previous.synchronized = true;
        }
        owner(
            &self.client.parent,
            OwnerOp::WebCommit {
                epoch: Some(self.epoch),
                node_id: self.client.node.clone(),
                revision,
            },
        )
        .await?;
        previous.committed = Some((revision, registry_hash));
        *self
            .management
            .write()
            .map_err(|_| anyhow::anyhow!("web checkpoint state unavailable"))? = management;
        Ok(())
    }

    pub(super) async fn maintain(&self, stop: CancellationToken) -> Result<()> {
        let mut interval = tokio::time::interval(Duration::from_millis(500));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            tokio::select! {_=stop.cancelled()=>return Ok(()),_=interval.tick()=>{}}
            let state = owner(
                &self.client.parent,
                OwnerOp::WebCheckpoint {
                    node_id: self.client.node.clone(),
                },
            )
            .await?;
            // A delayed configuration proposal can arrive after takeover.
            // Reacquire a new epoch and republish the route with a fresh retry
            // identity instead of keeping a live lease on an unusable carrier.
            ensure!(
                state["hosting_here"] == true && state["epoch"].as_u64() == Some(self.epoch),
                "web hosting route changed; reacquiring carrier lease"
            );
            if state["revision"] != state["durable"]
                || gateway::registry::portable::management(&self.home)? != self.management()
            {
                // A pending encrypted file upload is retryable. The local lease
                // continues independently while publication remains gated.
                let _ = self.work(Box::pin(self.flush())).await;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::http::HeaderMap;
    use gent_protocol::hosted_mcp::PASSIVE_HEADER;

    #[test]
    fn passive_mode_requires_one_exact_echo() {
        let mut headers = HeaderMap::new();
        assert!(!passive_echo(&headers).unwrap());
        headers.insert(PASSIVE_HEADER, "0".parse().unwrap());
        assert!(passive_echo(&headers).is_err());
        headers.insert(PASSIVE_HEADER, "1".parse().unwrap());
        assert!(passive_echo(&headers).unwrap());
        headers.append(PASSIVE_HEADER, "1".parse().unwrap());
        assert!(passive_echo(&headers).is_err());
    }

    #[test]
    fn concurrent_work_shares_only_a_fresh_unexpired_grant() {
        let now = Instant::now();
        let grant = Grant {
            until: now + Duration::from_secs(19),
            expires_at: 120,
        };
        assert!(grant.reusable(None, now, 100));
        // The first long-running call must renew. Its siblings can share the extension.
        assert!(!grant.reusable(Some(grant.until), now, 100));
        assert!(grant.reusable(Some(now + Duration::from_secs(9)), now, 100));
        assert!(!grant.reusable(None, now + Duration::from_secs(15), 100));
        assert!(!grant.reusable(None, now, 120));
    }
}