gent-link 0.1.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
//! Older native histories split the client ID and the turn receipt across adjacent rows.
use super::{Item, ReverseLines};
use anyhow::Result;
use serde::Deserialize;
use uuid::Uuid;

// Project only identity and provenance. Serde skips all message and tool content.
#[derive(Deserialize)]
struct Row {
    r#type: String,
    payload: Payload,
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    metadata: Option<RowMetadata>,
}

#[derive(Deserialize)]
struct RowMetadata {
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    retained_source: Option<RetainedSource>,
}

#[derive(Deserialize)]
struct RetainedSource {
    id: Option<SourceId>,
}

#[derive(Deserialize)]
struct SourceId {
    message_id: Option<String>,
    role: Option<String>,
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
}

#[derive(Deserialize)]
struct Payload {
    r#type: String,
    id: Option<String>,
    client_id: Option<String>,
    role: Option<String>,
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    internal_chat_message_metadata_passthrough: Option<MessageMetadata>,
}

#[derive(Deserialize)]
struct MessageMetadata {
    #[serde(alias = "turnId")]
    turn_id: Uuid,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    content_item_kinds: Vec<String>,
    content_item_metadata: Vec<ContentMetadata>,
}

#[derive(Deserialize)]
struct ContentMetadata {
    provenance: Provenance,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
}

#[derive(Deserialize)]
struct Provenance {
    r#type: String,
    #[serde(alias = "threadId")]
    thread_id: Option<String>,
    #[serde(alias = "turnId")]
    turn_id: Option<Uuid>,
}

struct Turn<'a> {
    thread: &'a str,
    id: Uuid,
}

impl Turn<'_> {
    fn agrees(&self, thread_id: Option<&str>, turn_id: Option<Uuid>) -> bool {
        thread_id.is_none_or(|id| id == self.thread) && turn_id.is_none_or(|id| id == self.id)
    }
}

impl Row {
    fn user_message_id(&self, turn: &Turn<'_>, client: &str) -> Option<&str> {
        let p = &self.payload;
        let id = p.id.as_deref()?;
        (self.r#type == "response_item"
            && p.r#type == "message"
            && p.role.as_deref() == Some("user")
            && !id.trim().is_empty()
            && self.agrees(turn, id)
            && p.client_id.as_deref().is_none_or(|id| id == client)
            && p.internal_chat_message_metadata_passthrough
                .as_ref()?
                .user_input(turn))
        .then_some(id)
    }

    fn agrees(&self, turn: &Turn<'_>, item: &str) -> bool {
        turn.agrees(self.thread_id.as_deref(), self.turn_id)
            && turn.agrees(self.payload.thread_id.as_deref(), self.payload.turn_id)
            && self.metadata.as_ref().is_none_or(|m| {
                turn.agrees(m.thread_id.as_deref(), m.turn_id)
                    && m.retained_source
                        .as_ref()
                        .and_then(|s| s.id.as_ref())
                        .is_none_or(|id| {
                            turn.agrees(id.thread_id.as_deref(), id.turn_id)
                                && id.role.as_deref().is_none_or(|role| role == "user")
                                && id.message_id.as_deref().is_none_or(|other| other == item)
                        })
            })
    }
}

impl MessageMetadata {
    fn user_input(&self, turn: &Turn<'_>) -> bool {
        turn.agrees(self.thread_id.as_deref(), Some(self.turn_id))
            && !self.content_item_kinds.is_empty()
            && self.content_item_kinds.len() == self.content_item_metadata.len()
            && self
                .content_item_kinds
                .iter()
                .zip(&self.content_item_metadata)
                .all(|(kind, m)| {
                    matches!(
                        kind.as_str(),
                        "user.text" | "user.image" | "user.audio" | "user.file"
                    ) && m.provenance.r#type == "user"
                        && turn.agrees(m.thread_id.as_deref(), m.turn_id)
                        && turn.agrees(m.provenance.thread_id.as_deref(), m.provenance.turn_id)
                })
    }
}

// A scoped item receipt and its response message use different native item
// IDs. Check the message against its own retained source and the receipt's turn.
pub(super) fn user_source(line: &[u8], thread: &str, turn: Uuid, client: &str) -> Option<String> {
    let message = serde_json::from_slice::<Row>(line).ok()?;
    message.user_message_id(&Turn { thread, id: turn }, client)?;
    message.payload.id
}

pub(super) fn input(
    rows: &mut ReverseLines<'_>,
    line: &mut Vec<u8>,
    thread: &str,
    turn: Uuid,
) -> Result<Option<Item>> {
    let Ok(event) = serde_json::from_slice::<Row>(line) else {
        return Ok(None);
    };
    if event.r#type != "event_msg"
        || event.payload.r#type != "user_message"
        || event
            .payload
            .internal_chat_message_metadata_passthrough
            .is_some()
        || !event
            .payload
            .client_id
            .as_ref()
            .is_some_and(|id| id.len() <= 256 && !id.trim().is_empty())
        || !rows.previous(line)?
    {
        return Ok(None);
    }
    let Ok(message) = serde_json::from_slice::<Row>(line) else {
        return Ok(None);
    };
    let turn = Turn { thread, id: turn };
    let Some(id) = event
        .payload
        .client_id
        .as_deref()
        .and_then(|client| message.user_message_id(&turn, client))
    else {
        return Ok(None);
    };
    if !event.agrees(&turn, id)
        || event.payload.id.as_ref().is_some_and(|other| other != id)
        || event
            .payload
            .role
            .as_deref()
            .is_some_and(|role| role != "user")
    {
        return Ok(None);
    }
    Ok(Some(Item {
        r#type: "UserMessage".into(),
        id: message.payload.id,
        client_id: event.payload.client_id,
    }))
}

pub(super) fn unpaired_user(line: &[u8]) -> Result<bool> {
    #[derive(Deserialize)]
    struct Envelope<P> {
        r#type: String,
        payload: P,
    }
    #[derive(Deserialize)]
    struct Tag {
        r#type: Option<String>,
        role: Option<String>,
    }
    #[derive(Deserialize)]
    struct Message {
        internal_chat_message_metadata_passthrough: Kinds,
    }
    #[derive(Deserialize)]
    struct Kinds {
        content_item_kinds: Vec<String>,
    }

    let row: Envelope<Tag> = serde_json::from_slice(line)?;
    if row.r#type != "response_item"
        || row.payload.r#type.as_deref() != Some("message")
        || row.payload.role.as_deref() != Some("user")
    {
        return Ok(false);
    }
    // Environment context also carries role=user. Only the known context-only shape
    // can be skipped without a client ID; missing or mixed metadata needs a receipt.
    let environment = serde_json::from_slice::<Envelope<Message>>(line).is_ok_and(|r| {
        let kinds = r
            .payload
            .internal_chat_message_metadata_passthrough
            .content_item_kinds;
        !kinds.is_empty()
            && kinds
                .iter()
                .all(|kind| kind == "environments.environment_context")
    });
    Ok(!environment)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{Value, json};
    use std::io::Write;

    const THREAD: &str = "00000000-0000-0000-0000-000000000003";
    const TURN: Uuid = Uuid::from_u128(1);
    const FOREIGN: Uuid = Uuid::from_u128(2);

    fn message() -> Value {
        json!({"type":"response_item","payload":{
            "type":"message", "role":"user", "id":"item-a",
            "content":[{"type":"input_text","text":"ignored"}],
            "internal_chat_message_metadata_passthrough":{
                "turn_id":TURN, "content_item_kinds":["user.text"],
                "content_item_metadata":[{"provenance":{"type":"user"}}]
            }},
            "metadata":{"retained_source":{"id":{"turn_id":TURN,"role":"user","message_id":"item-a"}}}
        })
    }

    fn event() -> Value {
        json!({"type":"event_msg","payload":{"type":"user_message","client_id":"client-a",
            "message":"ignored","images":[],"audio":[]}})
    }

    fn parse(rows: &[Value]) -> Result<Option<Item>> {
        let mut file = tempfile::tempfile()?;
        for row in rows {
            serde_json::to_writer(&mut file, row)?;
            writeln!(file)?;
        }
        let mut reader = ReverseLines::new(&file, file.metadata()?.len(), true)?;
        let mut line = Vec::new();
        assert!(reader.previous(&mut line)?);
        input(&mut reader, &mut line, THREAD, TURN)
    }

    #[test]
    fn joins_only_adjacent_native_ids_without_interpreting_content() -> Result<()> {
        let mut m = message();
        let meta = &mut m["payload"]["internal_chat_message_metadata_passthrough"];
        meta["content_item_kinds"] = json!(["user.audio", "user.image", "user.text", "user.file"]);
        meta["content_item_metadata"] = json!(vec![json!({"provenance":{"type":"user"}}); 4]);
        // The proof must not deserialize even the shape of content or event data.
        m["payload"]["content"] = json!({"opaque":true});
        let mut e = event();
        e["payload"]["turn_id"] = json!(TURN);
        e["payload"]["message"] = json!({"opaque":true});
        e["payload"]["audio"] = json!({"opaque":true});
        m["thread_id"] = json!(THREAD);
        m["payload"]["threadId"] = json!(THREAD);
        m["payload"]["internal_chat_message_metadata_passthrough"]["thread_id"] = json!(THREAD);
        m["metadata"]["thread_id"] = json!(THREAD);
        m["metadata"]["retained_source"]["id"]["thread_id"] = json!(THREAD);
        e["payload"]["thread_id"] = json!(THREAD);
        let item = parse(&[m, e])?.unwrap();
        assert_eq!(item.r#type, "UserMessage");
        assert_eq!(item.id.as_deref(), Some("item-a"));
        assert_eq!(item.client_id.as_deref(), Some("client-a"));

        let mut minimal = message();
        minimal.as_object_mut().unwrap().remove("metadata");
        let mut boundary = event();
        boundary["payload"]["client_id"] = json!("x".repeat(256));
        assert!(parse(&[minimal, boundary])?.is_some());
        Ok(())
    }

    #[test]
    fn rejects_missing_or_intervening_rows_and_wrong_discriminators() -> Result<()> {
        assert!(parse(&[event()])?.is_none());
        assert!(parse(&[event(), message()])?.is_none());
        let tool = json!({"type":"response_item","payload":{"type":"function_call","arguments":"ignored"}});
        assert!(parse(&[message(), tool, event()])?.is_none());
        assert!(parse(&[message(), event(), event()])?.is_none());
        for (row, path, value) in [
            (0, "/type", "event_msg"),
            (0, "/payload/type", "reasoning"),
            (0, "/payload/role", "assistant"),
            (0, "/payload/id", ""),
            (1, "/type", "response_item"),
            (1, "/payload/type", "item_completed"),
        ] {
            let mut pair = [message(), event()];
            *pair[row].pointer_mut(path).unwrap() = json!(value);
            assert!(parse(&pair)?.is_none(), "accepted row {row} {path}");
        }
        Ok(())
    }

    #[test]
    fn rejects_missing_client_id_and_conflicting_scope() -> Result<()> {
        for bad in [
            Value::Null,
            json!(""),
            json!(" \t"),
            json!("é".repeat(129)),
            json!(42),
        ] {
            let mut e = event();
            e["payload"]["client_id"] = bad;
            assert!(parse(&[message(), e])?.is_none());
        }
        for (row, container, key, value) in [
            (1, "/payload", "turn_id", json!(FOREIGN)),
            (1, "", "threadId", json!(FOREIGN)),
            (1, "/payload", "thread_id", json!(FOREIGN)),
            (0, "/payload", "client_id", json!("another-client")),
            (0, "/payload", "turnId", json!(FOREIGN)),
            (
                0,
                "/payload/internal_chat_message_metadata_passthrough",
                "turn_id",
                Value::Null,
            ),
            (
                0,
                "/payload/internal_chat_message_metadata_passthrough",
                "turn_id",
                json!(FOREIGN),
            ),
            (
                0,
                "/payload/internal_chat_message_metadata_passthrough/content_item_metadata/0/provenance",
                "thread_id",
                json!(FOREIGN),
            ),
            (0, "/metadata/retained_source/id", "turn_id", json!(FOREIGN)),
            (
                0,
                "/metadata/retained_source/id",
                "message_id",
                json!("another-item"),
            ),
            (
                0,
                "/metadata/retained_source/id",
                "role",
                json!("assistant"),
            ),
        ] {
            let mut pair = [message(), event()];
            pair[row].pointer_mut(container).unwrap()[key] = value;
            assert!(
                parse(&pair)?.is_none(),
                "accepted row {row} {container}/{key}"
            );
        }
        Ok(())
    }

    #[test]
    fn requires_provenance_for_every_known_input() -> Result<()> {
        let user = json!({"provenance":{"type":"user"}});
        for (kinds, provenance) in [
            (json!([]), json!([])),
            (json!(["user.text"]), Value::Null),
            (json!(["user.text"]), json!([{}])),
            (json!(["user.text"]), json!([user, user])),
            (json!(["user.text", "user.audio"]), json!([user])),
            (
                json!(["user.text", "user.audio"]),
                json!([user,{"provenance":{"type":"harness"}}]),
            ),
            (json!(["user.text", "user.goal"]), json!([user, user])),
            (json!(["environments.environment_context"]), json!([user])),
        ] {
            let mut m = message();
            let meta = &mut m["payload"]["internal_chat_message_metadata_passthrough"];
            meta["content_item_kinds"] = kinds;
            meta["content_item_metadata"] = provenance;
            assert!(parse(&[m, event()])?.is_none());
        }
        Ok(())
    }

    #[test]
    fn accounts_for_every_user_response_except_pure_environment_context() -> Result<()> {
        let mut m = message();
        let key = "internal_chat_message_metadata_passthrough";
        for (kinds, expected) in [
            (json!(["user.text"]), true),
            (
                json!(["environments.environment_context", "user.image"]),
                true,
            ),
            (json!(["unknown"]), true),
            (Value::Null, true),
            (json!([]), true),
            (json!([{"unknown":true}]), true),
            (
                json!([
                    "environments.environment_context",
                    "environments.environment_context"
                ]),
                false,
            ),
        ] {
            m["payload"][key]["content_item_kinds"] = kinds;
            assert_eq!(unpaired_user(&serde_json::to_vec(&m)?)?, expected);
        }
        m["payload"].as_object_mut().unwrap().remove(key);
        assert!(unpaired_user(&serde_json::to_vec(&m)?)?);
        for (kind, role) in [
            ("message", "assistant"),
            ("function_call", "user"),
            ("function_call_output", "tool"),
        ] {
            m["payload"]["type"] = json!(kind);
            m["payload"]["role"] = json!(role);
            assert!(!unpaired_user(&serde_json::to_vec(&m)?)?);
        }
        assert!(!unpaired_user(&serde_json::to_vec(&event())?)?);
        Ok(())
    }
}