ascent-research 0.4.2

ascent-research — an incremental research workflow CLI for AI agents. Every session resumes; knowledge accretes across runs. Mixes HTTP, browser, and local file ingest into a durable per-session wiki + figure-rich HTML report.
Documentation
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
//! `provider-codex` — CodexProvider speaking JSON-RPC 2.0 over stdio to
//! `codex app-server`.
//!
//! Protocol reference:
//! https://github.com/openai/codex/tree/main/codex-rs/app-server#protocol
//!
//! Lifecycle per `ask()` call:
//! 1. spawn `codex app-server --listen stdio://` with piped stdin/stdout
//! 2. send `initialize` request + `initialized` notification
//! 3. `thread/start` → capture `threadId`
//! 4. `turn/start` with the user prompt (system is prefixed in text)
//! 5. read the event stream, concatenate every assistant-message text,
//!    return when `turn/completed` arrives
//! 6. kill the subprocess
//!
//! Keep each `ask()` in its own thread — loop iterations are independent
//! and don't need cross-turn context (the research session itself is the
//! durable context, not codex's thread).

use super::provider::{AgentProvider, ProviderError};
use async_trait::async_trait;
use serde_json::{Value, json};
use std::process::Stdio;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{ChildStdin, ChildStdout, Command};
use tokio::time::{Duration, timeout};

/// How long we're willing to wait for the whole exchange. Codex turns can
/// be slow (10-30 s typical) but not minutes.
const TURN_TIMEOUT: Duration = Duration::from_secs(120);

/// Codex-backed provider. Spawns a fresh `codex app-server` per call —
/// simpler and safer than reusing connections across iterations.
pub struct CodexProvider {
    binary: String,
}

impl CodexProvider {
    pub fn new() -> Self {
        Self {
            binary: std::env::var("CODEX_BIN").unwrap_or_else(|_| "codex".to_string()),
        }
    }

    /// Override the codex binary path — used by tests that point at a
    /// stub app-server.
    pub fn with_binary(binary: impl Into<String>) -> Self {
        Self {
            binary: binary.into(),
        }
    }
}

impl Default for CodexProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl AgentProvider for CodexProvider {
    async fn ask(&self, system: &str, user: &str) -> Result<String, ProviderError> {
        timeout(TURN_TIMEOUT, self.exchange(system, user))
            .await
            .map_err(|_| {
                ProviderError::CallFailed(format!(
                    "codex exchange exceeded {}s",
                    TURN_TIMEOUT.as_secs()
                ))
            })?
    }

    fn name(&self) -> &'static str {
        "codex"
    }
}

impl CodexProvider {
    async fn exchange(&self, system: &str, user: &str) -> Result<String, ProviderError> {
        let mut child = Command::new(&self.binary)
            .args(["app-server", "--listen", "stdio://"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .map_err(|e| match e.kind() {
                std::io::ErrorKind::NotFound => ProviderError::NotAvailable(format!(
                    "codex binary '{}' not on PATH (install codex or set CODEX_BIN)",
                    self.binary
                )),
                _ => ProviderError::CallFailed(format!("spawn codex: {e}")),
            })?;

        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| ProviderError::CallFailed("codex stdin unavailable".into()))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| ProviderError::CallFailed("codex stdout unavailable".into()))?;
        let mut reader = BufReader::new(stdout).lines();
        let mut writer = stdin;

        // ── Handshake ─────────────────────────────────────────────────
        send(&mut writer, &build_initialize(1)).await?;
        // Wait for the matching `id: 1` response.
        wait_for_response(&mut reader, 1).await?;
        send(&mut writer, &build_initialized_notification()).await?;

        // ── Start thread ──────────────────────────────────────────────
        send(&mut writer, &build_thread_start(2, system)).await?;
        let thread_resp = wait_for_response(&mut reader, 2).await?;
        let thread_id = extract_thread_id(&thread_resp)
            .ok_or_else(|| {
                ProviderError::CallFailed(format!(
                    "thread/start response missing thread id: {thread_resp}"
                ))
            })?
            .to_string();

        // ── Start turn ────────────────────────────────────────────────
        // Prefix the system preamble onto the user text — Codex's
        // `turn/start` doesn't accept a separate system prompt on every
        // call; the thread-level personality is the closest analog.
        let combined = format!("[system]\n{system}\n\n[user]\n{user}");
        send(&mut writer, &build_turn_start(3, &thread_id, &combined)).await?;
        wait_for_response(&mut reader, 3).await?; // initial turn object

        // ── Stream events until turn/completed ────────────────────────
        let mut assistant_text = String::new();
        let mut turn_status: Option<String> = None;
        loop {
            let Some(line) = read_next_line(&mut reader).await? else {
                break;
            };
            maybe_debug_log_line(&line);
            let msg: Value = match serde_json::from_str(&line) {
                Ok(v) => v,
                Err(_) => continue,
            };
            match msg.get("method").and_then(Value::as_str) {
                Some("item/completed") => {
                    if let Some(text) = extract_assistant_text(&msg) {
                        assistant_text.push_str(&text);
                    }
                }
                Some("turn/completed") => {
                    turn_status = msg["params"]["turn"]["status"].as_str().map(str::to_string);
                    break;
                }
                _ => {}
            }
        }

        let _ = writer.shutdown().await;
        let _ = child.start_kill();
        let _ = child.wait().await;

        match turn_status.as_deref() {
            Some("completed") => {
                if assistant_text.trim().is_empty() {
                    Err(ProviderError::EmptyResponse)
                } else {
                    Ok(assistant_text)
                }
            }
            Some(other) => Err(ProviderError::CallFailed(format!(
                "codex turn status '{other}' — see codex stderr for details"
            ))),
            None => Err(ProviderError::CallFailed(
                "codex stream closed without turn/completed".into(),
            )),
        }
    }
}

// ── JSON-RPC plumbing ───────────────────────────────────────────────────

fn build_initialize(id: u64) -> Value {
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "method": "initialize",
        "params": {
            "clientInfo": {
                "name": "research-rs",
                "version": env!("CARGO_PKG_VERSION"),
            },
            "capabilities": {},
        },
    })
}

fn build_initialized_notification() -> Value {
    json!({ "jsonrpc": "2.0", "method": "initialized", "params": {} })
}

fn build_thread_start(id: u64, _system: &str) -> Value {
    // We deliberately omit cwd / approvalPolicy / sandbox — we never ask
    // codex to execute tools, only to return text. Using the user's own
    // config defaults keeps us out of the permission prompt path.
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "method": "thread/start",
        "params": {
            "approvalPolicy": "never",
            "sessionStartSource": "startup",
        },
    })
}

fn build_turn_start(id: u64, thread_id: &str, text: &str) -> Value {
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "method": "turn/start",
        "params": {
            "threadId": thread_id,
            "input": [{ "type": "text", "text": text }],
        },
    })
}

fn extract_thread_id(resp: &Value) -> Option<&str> {
    resp["result"]["threadId"]
        .as_str()
        .or_else(|| resp["result"]["thread"]["id"].as_str())
}

fn extract_assistant_text(msg: &Value) -> Option<String> {
    // `item/completed` notifications may carry an assistant message body at
    // `params.item.text` (raw text) or `params.item.content` (rich). We
    // accept both and stringify `content` as a fallback.
    let item = msg.get("params")?.get("item")?;
    let item_type = item.get("type").and_then(Value::as_str);
    if !matches!(item_type, Some("assistantMessage" | "agentMessage")) {
        return None;
    }
    if let Some(text) = item.get("text").and_then(Value::as_str) {
        return Some(text.to_string());
    }
    item.get("content").map(|v| v.to_string())
}

fn maybe_debug_log_line(line: &str) {
    let Ok(path) = std::env::var("ASR_CODEX_DEBUG_LOG") else {
        return;
    };
    if path.trim().is_empty() {
        return;
    }
    use std::io::Write;
    if let Ok(mut file) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
    {
        let _ = writeln!(file, "{line}");
    }
}

async fn send(writer: &mut ChildStdin, msg: &Value) -> Result<(), ProviderError> {
    let mut line = serde_json::to_string(msg).unwrap_or_default();
    line.push('\n');
    writer
        .write_all(line.as_bytes())
        .await
        .map_err(|e| ProviderError::CallFailed(format!("write codex stdin: {e}")))?;
    writer
        .flush()
        .await
        .map_err(|e| ProviderError::CallFailed(format!("flush codex stdin: {e}")))
}

async fn read_next_line(
    reader: &mut tokio::io::Lines<BufReader<ChildStdout>>,
) -> Result<Option<String>, ProviderError> {
    reader
        .next_line()
        .await
        .map_err(|e| ProviderError::CallFailed(format!("read codex stdout: {e}")))
}

async fn wait_for_response(
    reader: &mut tokio::io::Lines<BufReader<ChildStdout>>,
    expected_id: u64,
) -> Result<Value, ProviderError> {
    loop {
        let Some(line) = read_next_line(reader).await? else {
            return Err(ProviderError::CallFailed(
                "codex stream closed before response".into(),
            ));
        };
        let msg: Value = match serde_json::from_str(&line) {
            Ok(v) => v,
            Err(_) => continue,
        };
        if msg.get("id").and_then(Value::as_u64) == Some(expected_id) {
            if let Some(err) = msg.get("error") {
                return Err(ProviderError::CallFailed(format!(
                    "codex JSON-RPC error: {err}"
                )));
            }
            return Ok(msg);
        }
        // Skip notifications while waiting for this id's response.
    }
}

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

    #[test]
    fn initialize_carries_client_info() {
        let msg = build_initialize(1);
        assert_eq!(msg["jsonrpc"], "2.0");
        assert_eq!(msg["id"], 1);
        assert_eq!(msg["method"], "initialize");
        assert_eq!(msg["params"]["clientInfo"]["name"], "research-rs");
        assert!(
            msg["params"]["clientInfo"]["version"]
                .as_str()
                .map(|s| !s.is_empty())
                .unwrap_or(false)
        );
    }

    #[test]
    fn initialized_is_a_notification_without_id() {
        let msg = build_initialized_notification();
        assert_eq!(msg["method"], "initialized");
        assert!(msg.get("id").is_none(), "notifications must not carry id");
    }

    #[test]
    fn thread_start_uses_never_approval() {
        let msg = build_thread_start(2, "system");
        assert_eq!(msg["method"], "thread/start");
        assert_eq!(msg["params"]["approvalPolicy"], "never");
    }

    #[test]
    fn turn_start_wraps_text_as_input_item() {
        let msg = build_turn_start(3, "thr_abc", "hello codex");
        assert_eq!(msg["method"], "turn/start");
        assert_eq!(msg["params"]["threadId"], "thr_abc");
        let input = &msg["params"]["input"][0];
        assert_eq!(input["type"], "text");
        assert_eq!(input["text"], "hello codex");
    }

    #[test]
    fn extract_thread_id_accepts_legacy_and_current_shapes() {
        let legacy = json!({ "result": { "threadId": "thr_legacy" } });
        let current = json!({ "result": { "thread": { "id": "thr_current" } } });
        assert_eq!(extract_thread_id(&legacy), Some("thr_legacy"));
        assert_eq!(extract_thread_id(&current), Some("thr_current"));
    }

    #[test]
    fn extract_assistant_text_picks_text_field_over_content() {
        let msg = json!({
            "method": "item/completed",
            "params": {
                "item": {
                    "type": "assistantMessage",
                    "text": "direct text body",
                    "content": [{"type":"text","text":"nested"}],
                }
            }
        });
        assert_eq!(
            extract_assistant_text(&msg).as_deref(),
            Some("direct text body")
        );
    }

    #[test]
    fn extract_assistant_text_accepts_current_agent_message_type() {
        let msg = json!({
            "method": "item/completed",
            "params": {
                "item": {
                    "type": "agentMessage",
                    "text": "new app-server final answer"
                }
            }
        });
        assert_eq!(
            extract_assistant_text(&msg).as_deref(),
            Some("new app-server final answer")
        );
    }

    #[test]
    fn extract_assistant_text_falls_back_to_content() {
        let msg = json!({
            "method": "item/completed",
            "params": {
                "item": {
                    "type": "assistantMessage",
                    "content": [{"type":"text","text":"nested body"}]
                }
            }
        });
        let got = extract_assistant_text(&msg).unwrap();
        assert!(got.contains("nested body"));
    }

    #[test]
    fn extract_assistant_text_ignores_non_assistant_items() {
        let msg = json!({
            "method": "item/completed",
            "params": { "item": { "type": "userMessage", "text": "ignored" } }
        });
        assert!(extract_assistant_text(&msg).is_none());
    }

    #[tokio::test]
    async fn codex_provider_name_is_codex() {
        let p = CodexProvider::new();
        assert_eq!(p.name(), "codex");
    }

    #[tokio::test]
    async fn codex_missing_binary_returns_not_available() {
        let p = CodexProvider::with_binary("/nonexistent/path/to/codex-does-not-exist");
        match p.ask("sys", "usr").await {
            Err(ProviderError::NotAvailable(msg)) => {
                assert!(msg.contains("codex") || msg.contains("PATH"));
            }
            other => panic!("expected NotAvailable, got {other:?}"),
        }
    }
}