claudy 0.9.2

Modern multi-provider launcher for Claude CLI
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
//! Session bridge: exchange messages with existing codex/agy sessions from a
//! running Claude session (MCP tools `list_sessions` / `read_session` /
//! `send_message`).
//!
//! Mechanics (verified 2026-08-28):
//! - codex idle session: `codex queue --thread <id> --message <msg>` parks the
//!   message; `codex exec resume --skip-git-repo-check <id> <nudge>` consumes
//!   it (the queued text appears in the rollout turn).
//! - agy any session: `agy --conversation <id> -p <msg>` continues the same
//!   conversation thread with prior context intact.
//! - reading: the handoff readers already parse both stores live (rollout
//!   JSONL / conversation SQLite WAL).

use std::path::Path;
use std::time::Duration;

use crate::adapters::handoff::{agy, agy_home, codex};
use crate::adapters::mcp::runner;
use crate::domain::handoff::{DigestEvent, ForeignSessionSummary, HandoffSource};

/// Sessions listed per `list_sessions` call.
const LIST_LIMIT: usize = 50;
/// Default / max event count for `read_session`.
pub const TAIL_DEFAULT: usize = 20;
const TAIL_MAX: usize = 200;
/// Fallback send timeouts (seconds) when the agent isn't in the discovery set.
pub const CODEX_SEND_TIMEOUT_SECS: u64 = 3600;
pub const AGY_SEND_TIMEOUT_SECS: u64 = 300;
/// Max message length, matching ask_agent's prompt cap.
const MESSAGE_MAX_CHARS: usize = 100_000;
/// Id prefix length shown in listings. codex ids are UUIDv7, whose first 8
/// hex chars are a second-resolution timestamp — sessions started in the same
/// second collide at 8. 13 reaches into the random block, so the displayed
/// prefix stays unique and can be pasted straight back into read_session.
const ID_DISPLAY_LEN: usize = 13;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BridgeSource {
    Codex,
    Agy,
}

impl BridgeSource {
    pub fn parse(s: &str) -> anyhow::Result<Self> {
        match s {
            "codex" => Ok(BridgeSource::Codex),
            "agy" => Ok(BridgeSource::Agy),
            other => anyhow::bail!("Unknown source '{other}' (expected 'codex' or 'agy')"),
        }
    }

    fn handoff_source(self) -> HandoffSource {
        match self {
            BridgeSource::Codex => HandoffSource::Codex,
            BridgeSource::Agy => HandoffSource::Agy,
        }
    }
}

/// List recent foreign sessions, newest first.
pub fn list_sessions(source: BridgeSource) -> Vec<ForeignSessionSummary> {
    match source {
        BridgeSource::Codex => codex::discover_codex(LIST_LIMIT),
        BridgeSource::Agy => agy::discover_agy(LIST_LIMIT),
    }
}

/// Render a session summary as one text line for the MCP response.
pub fn render_session(s: &ForeignSessionSummary, now_secs: u64) -> String {
    let age_secs = now_secs.saturating_sub(s.last_modified);
    let age = if age_secs < 3600 {
        format!("{}m", age_secs / 60)
    } else if age_secs < 86400 {
        format!("{}h", age_secs / 3600)
    } else {
        format!("{}d", age_secs / 86400)
    };
    let title = s
        .title
        .as_deref()
        .unwrap_or("(untitled)")
        .replace('\n', " ");
    let title: String = title.chars().take(80).collect();
    format!(
        "{:<width$}  {:<9}  {:<4}  {}  {}",
        &s.id[..s.id.len().min(ID_DISPLAY_LEN)],
        s.source.as_str(),
        age,
        s.cwd.as_deref().unwrap_or("-"),
        title,
        width = ID_DISPLAY_LEN
    )
}

/// Read the last `tail` events of a foreign session, rendered as text.
pub fn read_session(source: BridgeSource, id: &str, tail: usize) -> anyhow::Result<String> {
    let summary = find_session(source, id)?;
    let events = extract_events(source, &summary)?;
    let tail = tail.clamp(1, TAIL_MAX);
    let start = events.len().saturating_sub(tail);
    let slice = &events[start..];
    Ok(render_events(
        source,
        &summary,
        slice,
        events.len() - slice.len(),
    ))
}

/// The nudge that makes the resume turn consume queued bridge messages.
const CODEX_RESUME_NUDGE: &str =
    "A bridged message was just queued for you. Process it now and reply with its answer.";

/// `codex queue` argv: park the message on the session's queue.
fn codex_queue_args(id: &str, message: &str) -> Vec<String> {
    vec![
        "queue".to_string(),
        "--thread".to_string(),
        id.to_string(),
        "--message".to_string(),
        message.to_string(),
    ]
}

/// `codex exec resume` argv: resume headlessly; the turn consumes the queue.
fn codex_resume_args(id: &str) -> Vec<String> {
    vec![
        "exec".to_string(),
        "resume".to_string(),
        "--skip-git-repo-check".to_string(),
        id.to_string(),
        CODEX_RESUME_NUDGE.to_string(),
    ]
}

/// `agy` argv: continue the conversation headlessly with one prompt.
fn agy_send_args(id: &str, message: &str) -> Vec<String> {
    vec![
        "--conversation".to_string(),
        id.to_string(),
        "-p".to_string(),
        message.to_string(),
    ]
}

/// Deliver a message into an existing foreign session, returning the reply.
///
/// codex: if the resume step fails (e.g. quota exhaustion), the queued
/// message stays parked and is consumed by the next successful resume.
pub async fn send_message(
    source: BridgeSource,
    id: &str,
    message: &str,
    timeout_secs: u64,
) -> anyhow::Result<String> {
    if message.is_empty() {
        anyhow::bail!("Message must not be empty");
    }
    if message.len() > MESSAGE_MAX_CHARS {
        anyhow::bail!("Message exceeds maximum length of {MESSAGE_MAX_CHARS} characters");
    }
    let summary = find_session(source, id)?;
    let cwd = summary.cwd.as_deref().map(Path::new);

    match source {
        BridgeSource::Codex => {
            // Park the message, then resume headlessly — the resume turn
            // consumes the queued message.
            let queue_args = codex_queue_args(&summary.id, message);
            runner::run_command(
                "codex queue",
                "codex",
                &queue_args,
                cwd,
                Duration::from_secs(timeout_secs),
            )
            .await?;

            let resume_args = codex_resume_args(&summary.id);
            runner::run_command(
                "codex exec resume",
                "codex",
                &resume_args,
                cwd,
                Duration::from_secs(timeout_secs),
            )
            .await
        }
        BridgeSource::Agy => {
            let args = agy_send_args(&summary.id, message);
            runner::run_command("agy", "agy", &args, cwd, Duration::from_secs(timeout_secs)).await
        }
    }
}

/// Find a session by exact id or unique prefix (the listing shows
/// `ID_DISPLAY_LEN` chars).
fn find_session(source: BridgeSource, id: &str) -> anyhow::Result<ForeignSessionSummary> {
    find_in(&list_sessions(source), id, source.handoff_source().as_str())
}

fn find_in(
    sessions: &[ForeignSessionSummary],
    id: &str,
    source_name: &str,
) -> anyhow::Result<ForeignSessionSummary> {
    if id.is_empty() {
        anyhow::bail!("Id must not be empty");
    }
    let matches: Vec<&ForeignSessionSummary> = sessions
        .iter()
        .filter(|s| s.id == id || s.id.starts_with(id))
        .collect();
    match matches.as_slice() {
        [] => anyhow::bail!(
            "No {source_name} session matching id '{id}' (use list_sessions to see ids)"
        ),
        [one] => Ok((*one).clone()),
        _ => anyhow::bail!(
            "Id '{id}' matches {} {source_name} sessions — use more characters",
            matches.len()
        ),
    }
}

fn extract_events(
    source: BridgeSource,
    summary: &ForeignSessionSummary,
) -> anyhow::Result<Vec<DigestEvent>> {
    match source {
        BridgeSource::Codex => {
            let path = summary.path.as_ref().ok_or_else(|| {
                anyhow::anyhow!("No rollout file recorded for session {}", summary.id)
            })?;
            codex::extract_codex_events(path)
        }
        BridgeSource::Agy => {
            let dir = agy_home().ok_or_else(|| anyhow::anyhow!("Cannot locate the agy store"))?;
            agy::extract_agy_events(&dir, &summary.id)
        }
    }
}

fn render_events(
    source: BridgeSource,
    summary: &ForeignSessionSummary,
    events: &[DigestEvent],
    elided: usize,
) -> String {
    let mut out = format!(
        "Session {} ({}) — {} events, {} earlier elided\n\n",
        &summary.id[..summary.id.len().min(ID_DISPLAY_LEN)],
        source.handoff_source().as_str(),
        events.len(),
        elided
    );
    if events.is_empty() {
        out.push_str("(no events could be extracted)\n");
        return out;
    }
    for event in events {
        match event {
            DigestEvent::User(t) => out.push_str(&format!("USER: {}\n", single_line(t, 400))),
            DigestEvent::Assistant(t) => {
                out.push_str(&format!("ASSISTANT: {}\n", single_line(t, 400)))
            }
            DigestEvent::ToolCall { name, args } => {
                out.push_str(&format!("TOOL {name}: {}\n", single_line(args, 160)))
            }
            DigestEvent::ToolOutput(t) => out.push_str(&format!("{}\n", single_line(t, 160))),
        }
    }
    out
}

fn single_line(s: &str, cap: usize) -> String {
    let joined: String = s
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .collect::<Vec<_>>()
        .join(" ");
    let mut out: String = joined.chars().take(cap).collect();
    if joined.chars().count() > cap {
        out.push('');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn source_parse() {
        assert_eq!(BridgeSource::parse("codex").unwrap(), BridgeSource::Codex);
        assert_eq!(BridgeSource::parse("agy").unwrap(), BridgeSource::Agy);
        assert!(BridgeSource::parse("slack").is_err());
    }

    #[test]
    fn find_in_requires_unique_prefix() {
        let mk = |id: &str| ForeignSessionSummary {
            source: HandoffSource::Codex,
            id: id.to_string(),
            title: None,
            cwd: None,
            last_modified: 0,
            path: None,
        };
        let sessions = vec![mk("aaaa1111-xx"), mk("aaaa2222-xx")];
        assert_eq!(
            find_in(&sessions, "aaaa1", "codex").unwrap().id,
            "aaaa1111-xx"
        );
        assert_eq!(
            find_in(&sessions, "aaaa2222-xx", "codex").unwrap().id,
            "aaaa2222-xx"
        ); // exact wins ambiguity
        let ambiguous = find_in(&sessions, "aaaa", "codex").unwrap_err().to_string();
        assert!(ambiguous.contains("matches 2"));
        let missing = find_in(&sessions, "zzzz", "codex").unwrap_err().to_string();
        assert!(missing.contains("No codex session"));
        // Empty id would prefix-match every session — rejected outright.
        assert!(find_in(&sessions, "", "codex").is_err());
    }

    #[test]
    fn codex_queue_args_shape() {
        let args = codex_queue_args("01a04867-abcd", "hello --flag $HOME");
        assert_eq!(
            args,
            vec![
                "queue",
                "--thread",
                "01a04867-abcd",
                "--message",
                "hello --flag $HOME",
            ]
        );
    }

    #[test]
    fn codex_resume_args_shape() {
        let args = codex_resume_args("01a04867-abcd");
        assert_eq!(
            args[..4],
            ["exec", "resume", "--skip-git-repo-check", "01a04867-abcd"][..]
        );
        assert_eq!(args[4], CODEX_RESUME_NUDGE);
    }

    #[test]
    fn agy_send_args_shape() {
        let args = agy_send_args("c4eada91-1234", "what was the codeword?");
        assert_eq!(
            args,
            vec![
                "--conversation",
                "c4eada91-1234",
                "-p",
                "what was the codeword?"
            ]
        );
    }

    #[test]
    fn render_session_line() {
        let s = ForeignSessionSummary {
            source: HandoffSource::Agy,
            id: "f66b432b-35c1".to_string(),
            title: Some("multi\nline title".to_string()),
            cwd: Some("/tmp/proj".to_string()),
            last_modified: 1_800_000_000,
            path: None,
        };
        let line = render_session(&s, 1_800_000_300);
        assert!(line.contains("f66b432b"));
        assert!(line.contains("agy"));
        assert!(line.contains("5m"));
        assert!(line.contains("/tmp/proj"));
        assert!(line.contains("multi line title"));
    }

    #[test]
    fn rendered_prefix_separates_same_second_uuidv7_ids() {
        // Real codex ids from two sessions started in the same second: the
        // UUIDv7 timestamp block makes them identical for 8 chars, so the
        // rendered prefix must be long enough to tell them apart and must
        // still resolve through find_in.
        let mk = |id: &str| ForeignSessionSummary {
            source: HandoffSource::Codex,
            id: id.to_string(),
            title: None,
            cwd: None,
            last_modified: 1_800_000_000,
            path: None,
        };
        let a = mk("01a05675-6cdd-7f92-94e4-522c5a639afe");
        let b = mk("01a05675-6cde-7ac2-a62c-07ba6ad49de5");
        assert_eq!(a.id[..8], b.id[..8], "ids collide at the old 8-char width");

        let prefix_of = |s: &ForeignSessionSummary| {
            render_session(s, 1_800_000_000)
                .split_whitespace()
                .next()
                .unwrap()
                .to_string()
        };
        let (pa, pb) = (prefix_of(&a), prefix_of(&b));
        assert_ne!(pa, pb);

        // The prefix as displayed is directly usable as a lookup key.
        let sessions = vec![a.clone(), b.clone()];
        assert_eq!(find_in(&sessions, &pa, "codex").unwrap().id, a.id);
        assert_eq!(find_in(&sessions, &pb, "codex").unwrap().id, b.id);
    }

    #[test]
    fn render_events_caps_lines() {
        let s = ForeignSessionSummary {
            source: HandoffSource::Codex,
            id: "019fb39e-32f4".to_string(),
            title: None,
            cwd: None,
            last_modified: 0,
            path: None,
        };
        let events = vec![
            DigestEvent::User("fix\nthe\nparser".to_string()),
            DigestEvent::Assistant("ok".repeat(1000)),
        ];
        let text = render_events(BridgeSource::Codex, &s, &events, 3);
        assert!(text.contains("USER: fix the parser"));
        assert!(text.contains("3 earlier elided"));
        assert!(text.contains(''));
    }

    #[test]
    fn real_store_read_tail() {
        // Smoke: reads whichever sessions exist on this machine.
        for source in [BridgeSource::Codex, BridgeSource::Agy] {
            let sessions = list_sessions(source);
            let Some(first) = sessions.first() else {
                continue;
            };
            let text = read_session(source, &first.id, 5).unwrap();
            assert!(text.contains("Session"));
            // Header reports the returned event count — tail must bound it.
            let header = text.lines().next().unwrap_or_default();
            let shown: usize = header
                .split("")
                .nth(1)
                .and_then(|s| s.split(" events").next())
                .and_then(|s| s.parse().ok())
                .unwrap_or(0);
            assert!(shown <= 5, "tail=5 but {shown} events returned");
        }
    }
}