gent-link 0.2.15

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
//! Bind a native message and its adjacent receipt before checking the delivery journal.
use super::*;

fn identity<'a>(p: &'a Event, thread: &str, turn: &Completed) -> Option<(&'a str, &'a str, i64)> {
    let item = p.item.as_ref()?;
    let at = p.completed_at_ms?;
    (p.r#type == "item_completed"
        && p.thread_id.as_deref() == Some(thread)
        && p.turn_id.as_ref() == Some(&turn.id)
        && item.r#type == "UserMessage"
        && (turn.started..=turn.ended).contains(&at.div_euclid(1000)))
    .then_some((
        item.id.as_deref().filter(|s| !s.is_empty())?,
        item.client_id.as_deref().filter(|s| !s.is_empty())?,
        at,
    ))
}

pub(super) fn confirm(
    rows: &ReverseLines<'_>,
    position: u64,
    initial: &Event,
    thread: &str,
    turn: &Completed,
) -> Result<Option<DirectInput>> {
    let Some((first_item, first_client, first_at)) = identity(initial, thread, turn) else {
        return Ok(None);
    };
    // Revisit just the later receipt and its adjacent response. A single-input
    // queue turn incurs no additional reads or decoding of user text.
    let Some((item, at, input)) = pair(rows, position, thread, turn, true)? else {
        return Ok(None);
    };
    Ok((item != first_item && input.client != first_client && at > first_at).then_some(input))
}

pub(super) fn finished_inputs(
    rows: &ReverseLines<'_>,
    positions: &[u64],
    thread: &str,
    turn: &Completed,
) -> Result<Option<Vec<DirectInput>>> {
    let mut inputs = Vec::with_capacity(positions.len());
    let mut items = HashSet::new();
    let mut clients = HashSet::new();
    let mut previous = None;
    for pos in positions.iter().rev() {
        let Some((item, at, input)) = pair(rows, *pos, thread, turn, false)? else {
            return Ok(None);
        };
        if !items.insert(item)
            || !clients.insert(input.client.clone())
            || previous.is_some_and(|t| t > at)
        {
            return Ok(None);
        }
        previous = Some(at);
        inputs.push(input);
    }
    Ok(Some(inputs))
}

fn pair(
    rows: &ReverseLines<'_>,
    position: u64,
    thread: &str,
    turn: &Completed,
    completed_relay: bool,
) -> Result<Option<(String, i64, DirectInput)>> {
    let mut cursor = ReverseLines {
        file: rows.file,
        floor: rows.floor,
        offset: position,
        end: 0,
        block: vec![0; rows.block.len()],
    };
    let mut line = Vec::new();
    if !cursor.previous(&mut line)? {
        return Ok(None);
    }
    let receipt: Record<Event> = serde_json::from_slice(&line)?;
    let Some((item, client, at)) = identity(&receipt.payload, thread, turn) else {
        return Ok(None);
    };
    if receipt.r#type != "event_msg" {
        return Ok(None);
    }
    let Some(hash) = input_hash(&line, true) else {
        return Ok(None);
    };
    if !cursor.previous(&mut line)? {
        return Ok(None);
    }
    let turn_id = Uuid::parse_str(&turn.id)?;
    let row: Record<Event> = serde_json::from_slice(&line)?;
    let p = row.payload;
    if crate::wake::codex::ordinary::user_source(&line, thread, turn_id, client).is_none()
        || !p
            .internal_chat_message_metadata_passthrough
            .as_ref()
            .is_some_and(|m| {
                m.turn_id.as_ref() == Some(&turn.id)
                    && m.content_item_kinds
                        .as_deref()
                        .is_some_and(|k| k == ["user.text"])
            })
        || input_hash(&line, false).as_ref() != Some(&hash)
    {
        return Ok(None);
    }
    Ok(Some((
        item.to_owned(),
        at,
        DirectInput {
            completed_relay,
            turn: turn_id,
            client: client.into(),
            hash,
        },
    )))
}