gent-link 0.2.22

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
//! A scoped tool declaration and its corresponding world-state delta may
//! precede the native turn context. The delta lists the newly declared tools.
use super::*;
use std::collections::BTreeMap;

#[derive(Deserialize)]
struct Record<T> {
    payload: T,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Delta {
    full: bool,
    state: State,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct State {
    top_level_tools: BTreeMap<String, String>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Declaration {
    #[serde(rename = "type")]
    kind: String,
    role: String,
    id: String,
    tools: Vec<Namespace>,
}

#[derive(Deserialize)]
struct Namespace {
    #[serde(rename = "type")]
    kind: String,
    name: String,
    tools: Vec<Definition>,
}
#[derive(Deserialize)]
struct Definition {
    #[serde(rename = "type")]
    kind: String,
    name: String,
}

pub(super) fn start(
    rows: &mut ReverseLines<'_>,
    line: &mut Vec<u8>,
    thread: &str,
    turn: &str,
) -> Result<Option<Row>> {
    let Some(row) = Row::next(rows, line)? else {
        return Ok(None);
    };
    if row.r#type != "world_state" {
        return Ok(Some(row));
    }
    declared_start(rows, line, thread, turn)
}

// Probe from the world-state row without consuming any ordinary proof records.
// An unfamiliar row can still contribute to the ordinary reader's result.
pub(in crate::wake::codex::ordinary::proof) fn foreground_start(
    rows: &ReverseLines<'_>,
    line: &mut Vec<u8>,
    thread: &str,
    turn: &str,
) -> bool {
    matches!(declared_start(&mut rows.clone(), line, thread, turn), Ok(Some(start))
        if start.r#type == "event_msg"
            && start.payload.r#type.as_deref() == Some("task_started")
            && start.payload.turn_id.as_deref() == Some(turn))
}

// Codex can compact before it records the first user input. The checkpoint
// retains the original admission. Its replacement history is model context,
// so only inspect the native attribution and the rows outside the checkpoint.
pub(in crate::wake::codex::ordinary::proof) fn compacted_start(
    rows: &ReverseLines<'_>,
    line: &mut Vec<u8>,
    failed: &Failed,
    deadline: Instant,
) -> Result<bool> {
    #[derive(Deserialize)]
    struct Checkpoint {
        resume_metadata: Resume,
    }
    #[derive(Deserialize)]
    struct Resume {
        last_started_turn_id: Uuid,
        turn_attribution: Attribution,
    }
    #[derive(Deserialize)]
    struct Snapshot {
        full: bool,
    }
    let mut rows = rows.clone();
    let turn = failed.turn.to_string();
    let thread = &failed.thread;
    while Instant::now() < deadline {
        let Some(row) = Row::next(&mut rows, line)? else {
            return Ok(false);
        };
        let p = row.payload;
        if !p.within(thread, &turn) {
            return Ok(false);
        }
        match (row.r#type.as_str(), p.r#type.as_deref()) {
            ("event_msg", Some("thread_settings_applied"))
                if p.thread_id.as_ref() == Some(thread) => {}
            ("event_msg", Some("token_count")) => {}
            ("turn_context", None)
                if p.turn_id.as_ref() == Some(&turn) && p.root_turn_id.as_ref() == Some(&turn) =>
            {
                break;
            }
            _ => return Ok(false),
        }
    }
    if !Row::next(&mut rows, line)?.is_some_and(|r| r.r#type == "world_state")
        || !serde_json::from_slice::<Record<Snapshot>>(line).is_ok_and(|r| r.payload.full)
        || !Row::next(&mut rows, line)?.is_some_and(|r| r.r#type == "compacted")
    {
        return Ok(false);
    }
    let Ok(row) = serde_json::from_slice::<Record<Checkpoint>>(line) else {
        return Ok(false);
    };
    let resume = row.payload.resume_metadata;
    let a = resume.turn_attribution;
    if resume.last_started_turn_id != failed.turn
        || a.turn_id != failed.turn
        || a.root_turn_id != failed.turn
        || a.parent_turn_id.is_some()
        || a.initiating_agent_path.is_some()
        || a.turn_trigger.as_deref() != Some("user")
    {
        return Ok(false);
    }
    while Instant::now() < deadline {
        let Some(row) = Row::next(&mut rows, line)? else {
            return Ok(false);
        };
        let p = &row.payload;
        match (row.r#type.as_str(), p.r#type.as_deref()) {
            ("event_msg", Some("task_started")) => return Ok(p.turn_id.as_ref() == Some(&turn)),
            ("token_usage_record", _)
            | ("event_msg", Some("token_count"))
            | ("response_item", Some("reasoning" | "message"))
                if row.work(thread, &turn) => {}
            _ => return Ok(false),
        }
    }
    Ok(false)
}

fn declared_start(
    rows: &mut ReverseLines<'_>,
    line: &mut Vec<u8>,
    thread: &str,
    turn: &str,
) -> Result<Option<Row>> {
    let Ok(delta) = serde_json::from_slice::<Record<Delta>>(line) else {
        return Ok(None);
    };
    let mut tools = delta.payload.state.top_level_tools;
    if delta.payload.full
        || tools.is_empty()
        || !tools
            .values()
            .all(|v| v.len() == 40 && v.bytes().all(|c| c.is_ascii_hexdigit()))
    {
        return Ok(None);
    }
    if !Row::next(rows, line)?.is_some_and(|r| r.r#type == "response_item") {
        return Ok(None);
    }
    let Ok(declaration) = serde_json::from_slice::<Record<Declaration>>(line) else {
        return Ok(None);
    };
    let d = declaration.payload;
    if d.kind != "additional_tools"
        || d.role != "developer"
        || d.id.is_empty()
        || d.tools.is_empty()
    {
        return Ok(None);
    }
    for ns in d.tools {
        if ns.kind != "namespace" || ns.name.is_empty() || ns.tools.is_empty() {
            return Ok(None);
        }
        for tool in ns.tools {
            if !matches!(tool.kind.as_str(), "custom" | "function")
                || tool.name.is_empty()
                || tools
                    .remove(&format!("{}.{}", ns.name, tool.name))
                    .is_none()
            {
                return Ok(None);
            }
        }
    }
    if !tools.is_empty() {
        return Ok(None);
    }
    let Some(update) = Row::next(rows, line)? else {
        return Ok(None);
    };
    let p = update.payload;
    if update.r#type != "response_item"
        || p.r#type.as_deref() != Some("message")
        || p.role.as_deref() != Some("developer")
        || p.id.as_deref().is_none_or(str::is_empty)
        || p.call_id.is_some()
        || !p.within(thread, turn)
        || !p
            .internal_chat_message_metadata_passthrough
            .is_some_and(|m| {
                m.turn_id.as_deref() == Some(turn)
                    && m.content_item_kinds
                        .as_deref()
                        .is_some_and(|k| k == ["tools.incremental_update"])
                    && m.executed_tool_calls
                        .as_deref()
                        .is_none_or(|calls| calls.is_empty())
            })
    {
        return Ok(None);
    }
    Row::next(rows, line)
}