ascent-research 0.3.0

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
439
440
441
//! Local runtime preflight for `ascent-research` skill/playbooks.

#[cfg(any(
    all(feature = "autoresearch", feature = "provider-claude"),
    all(feature = "autoresearch", feature = "provider-codex")
))]
use crate::autoresearch::provider::{AgentProvider, ProviderError};
use crate::output::Envelope;
use crate::route::rules::load_preset;
use crate::session::layout::research_root;
use serde::Serialize;
use serde_json::json;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

const CMD: &str = "research doctor";
const INSTALL_HINT: &str = "cargo install ascent-research --features \"provider-claude provider-codex\" && npm install -g postagent @actionbookdev/cli";

#[derive(Debug, Clone, Serialize)]
struct DoctorCheck {
    name: &'static str,
    ok: bool,
    required: bool,
    detail: String,
}

pub fn run(provider_smoke: bool, tool_smoke: bool, provider: &str) -> Envelope {
    let data_home = research_root();
    let postagent_bin = resolve_bin("POSTAGENT_BIN", "postagent");
    let actionbook_bin = resolve_bin("ACTIONBOOK_BIN", "actionbook");
    let mut checks = vec![
        check_data_home_writable(&data_home),
        check_builtin_preset("tech"),
        check_builtin_preset("sports"),
        check_bin(
            "postagent_bin",
            "POSTAGENT_BIN",
            "postagent",
            true,
            &postagent_bin,
        ),
        check_bin(
            "actionbook_bin",
            "ACTIONBOOK_BIN",
            "actionbook",
            true,
            &actionbook_bin,
        ),
        check_feature("autoresearch_enabled", cfg!(feature = "autoresearch"), true),
        check_feature(
            "provider_claude_enabled",
            cfg!(feature = "provider-claude"),
            false,
        ),
        check_feature(
            "provider_codex_enabled",
            cfg!(feature = "provider-codex"),
            false,
        ),
    ];
    if tool_smoke {
        checks.extend(tool_smoke_checks(&postagent_bin, &actionbook_bin));
    }
    if provider_smoke {
        let providers = match provider {
            "all" => vec!["claude", "codex"],
            "claude" | "codex" => vec![provider],
            other => {
                return Envelope::fail(
                    CMD,
                    "INVALID_PROVIDER",
                    format!("unknown doctor provider '{other}' — expected claude, codex, or all"),
                );
            }
        };
        for provider in providers {
            checks.push(check_provider_smoke(provider));
        }
    }

    let required_failed = checks.iter().any(|check| check.required && !check.ok);
    let payload = json!({
        "status": if required_failed { "missing_required" } else { "ok" },
        "data_home": data_home.display().to_string(),
        "install_hint": INSTALL_HINT,
        "checks": checks,
    });

    if required_failed {
        Envelope::fail(CMD, "DOCTOR_FAILED", "required doctor checks failed").with_details(payload)
    } else {
        Envelope::ok(CMD, payload)
    }
}

fn check_provider_smoke(provider: &str) -> DoctorCheck {
    match provider {
        "claude" => check_claude_smoke(),
        "codex" => check_codex_smoke(),
        _ => DoctorCheck {
            name: "provider_smoke_unknown",
            ok: false,
            required: true,
            detail: format!("unknown provider {provider}"),
        },
    }
}

#[cfg(all(feature = "autoresearch", feature = "provider-claude"))]
fn check_claude_smoke() -> DoctorCheck {
    smoke_provider(
        "provider_claude_smoke",
        crate::autoresearch::claude::ClaudeProvider::new(),
    )
}

#[cfg(not(all(feature = "autoresearch", feature = "provider-claude")))]
fn check_claude_smoke() -> DoctorCheck {
    DoctorCheck {
        name: "provider_claude_smoke",
        ok: false,
        required: true,
        detail: "provider-claude feature not compiled in".to_string(),
    }
}

#[cfg(all(feature = "autoresearch", feature = "provider-codex"))]
fn check_codex_smoke() -> DoctorCheck {
    smoke_provider(
        "provider_codex_smoke",
        crate::autoresearch::codex::CodexProvider::new(),
    )
}

#[cfg(not(all(feature = "autoresearch", feature = "provider-codex")))]
fn check_codex_smoke() -> DoctorCheck {
    DoctorCheck {
        name: "provider_codex_smoke",
        ok: false,
        required: true,
        detail: "provider-codex feature not compiled in".to_string(),
    }
}

#[cfg(any(
    all(feature = "autoresearch", feature = "provider-claude"),
    all(feature = "autoresearch", feature = "provider-codex")
))]
fn smoke_provider<P>(name: &'static str, provider: P) -> DoctorCheck
where
    P: AgentProvider,
{
    let prompt = "Reply with exactly: ok";
    let runtime = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
    {
        Ok(runtime) => runtime,
        Err(e) => {
            return DoctorCheck {
                name,
                ok: false,
                required: true,
                detail: format!("runtime init failed: {e}"),
            };
        }
    };
    let result = runtime.block_on(provider.ask(
        "You are a health-check endpoint. Return only the requested literal text.",
        prompt,
    ));
    match result {
        Ok(text) if text.trim().eq_ignore_ascii_case("ok") => DoctorCheck {
            name,
            ok: true,
            required: true,
            detail: "provider returned ok".to_string(),
        },
        Ok(text) => DoctorCheck {
            name,
            ok: false,
            required: true,
            detail: format!(
                "provider returned unexpected text: {}",
                truncate_detail(text.trim())
            ),
        },
        Err(ProviderError::NotAvailable(e)) => DoctorCheck {
            name,
            ok: false,
            required: true,
            detail: format!("provider unavailable: {e}"),
        },
        Err(ProviderError::CallFailed(e)) => DoctorCheck {
            name,
            ok: false,
            required: true,
            detail: format!("provider call failed: {e}"),
        },
        Err(ProviderError::EmptyResponse) => DoctorCheck {
            name,
            ok: false,
            required: true,
            detail: "provider returned empty response".to_string(),
        },
    }
}

fn truncate_detail(text: &str) -> String {
    const MAX: usize = 160;
    if text.len() <= MAX {
        text.to_string()
    } else {
        format!("{}...", &text[..MAX])
    }
}

fn check_data_home_writable(root: &Path) -> DoctorCheck {
    let result = (|| -> Result<(), String> {
        fs::create_dir_all(root).map_err(|e| format!("cannot create data home: {e}"))?;
        let probe = root.join(".doctor-write-test");
        fs::write(&probe, b"ok").map_err(|e| format!("cannot write probe: {e}"))?;
        fs::remove_file(&probe).map_err(|e| format!("cannot remove probe: {e}"))?;
        Ok(())
    })();

    match result {
        Ok(()) => DoctorCheck {
            name: "data_home_writable",
            ok: true,
            required: true,
            detail: format!("writable at {}", root.display()),
        },
        Err(detail) => DoctorCheck {
            name: "data_home_writable",
            ok: false,
            required: true,
            detail,
        },
    }
}

fn check_builtin_preset(name: &'static str) -> DoctorCheck {
    let check_name = match name {
        "tech" => "builtin_preset_tech",
        "sports" => "builtin_preset_sports",
        _ => "builtin_preset_unknown",
    };
    match load_preset(Some(name), None) {
        Ok(preset) => DoctorCheck {
            name: check_name,
            ok: true,
            required: true,
            detail: format!("loaded preset '{}'", preset.name),
        },
        Err(e) => DoctorCheck {
            name: check_name,
            ok: false,
            required: true,
            detail: e.to_string(),
        },
    }
}

fn check_bin(
    name: &'static str,
    env_var: &'static str,
    bin_name: &'static str,
    required: bool,
    resolution: &BinResolution,
) -> DoctorCheck {
    match resolution {
        BinResolution::Found(path) => DoctorCheck {
            name,
            ok: true,
            required,
            detail: format!("found at {}", path.display()),
        },
        BinResolution::MissingEnv(path) => DoctorCheck {
            name,
            ok: false,
            required,
            detail: format!("{env_var} target not found: {}", path.display()),
        },
        BinResolution::MissingPath => DoctorCheck {
            name,
            ok: false,
            required,
            detail: format!("{bin_name} not found on PATH; set {env_var} to override"),
        },
    }
}

fn tool_smoke_checks(postagent: &BinResolution, actionbook: &BinResolution) -> Vec<DoctorCheck> {
    let mut checks = Vec::new();
    match postagent {
        BinResolution::Found(path) => {
            checks.push(check_command(
                "postagent_version",
                path,
                &["--version"],
                true,
                "postagent --version",
            ));
            checks.push(check_command(
                "postagent_send_help",
                path,
                &["send", "--help"],
                true,
                "postagent send --help",
            ));
            checks.push(check_command(
                "postagent_public_dry_run",
                path,
                &["send", "https://example.com", "--dry-run"],
                false,
                "postagent send https://example.com --dry-run",
            ));
        }
        _ => checks.push(DoctorCheck {
            name: "postagent_version",
            ok: false,
            required: true,
            detail: "postagent binary missing; cannot run tool smoke".to_string(),
        }),
    }

    match actionbook {
        BinResolution::Found(path) => {
            checks.push(check_command(
                "actionbook_version",
                path,
                &["--version"],
                true,
                "actionbook --version",
            ));
            checks.push(check_command(
                "actionbook_browser_list_sessions",
                path,
                &["browser", "list-sessions", "--json"],
                true,
                "actionbook browser list-sessions --json",
            ));
        }
        _ => checks.push(DoctorCheck {
            name: "actionbook_version",
            ok: false,
            required: true,
            detail: "actionbook binary missing; cannot run tool smoke".to_string(),
        }),
    }
    checks
}

fn check_command(
    name: &'static str,
    bin: &Path,
    args: &[&str],
    required: bool,
    label: &str,
) -> DoctorCheck {
    match Command::new(bin).args(args).output() {
        Ok(output) if output.status.success() => DoctorCheck {
            name,
            ok: true,
            required,
            detail: format!("{label} ok: {}", summarize_output(&output.stdout)),
        },
        Ok(output) => {
            let stderr = summarize_output(&output.stderr);
            let stdout = summarize_output(&output.stdout);
            let detail = if stderr.is_empty() {
                format!("{label} exited {}: {stdout}", output.status)
            } else {
                format!("{label} exited {}: {stderr}", output.status)
            };
            DoctorCheck {
                name,
                ok: false,
                required,
                detail,
            }
        }
        Err(e) => DoctorCheck {
            name,
            ok: false,
            required,
            detail: format!("{label} failed to spawn: {e}"),
        },
    }
}

fn summarize_output(bytes: &[u8]) -> String {
    let text = String::from_utf8_lossy(bytes);
    truncate_detail(text.trim())
}

fn check_feature(name: &'static str, enabled: bool, required: bool) -> DoctorCheck {
    DoctorCheck {
        name,
        ok: enabled,
        required,
        detail: if enabled {
            "compiled in".to_string()
        } else {
            "not compiled in".to_string()
        },
    }
}

enum BinResolution {
    Found(PathBuf),
    MissingEnv(PathBuf),
    MissingPath,
}

fn resolve_bin(env_var: &str, bin_name: &str) -> BinResolution {
    if let Some(path) = env::var_os(env_var).filter(|value| !value.is_empty()) {
        let path = PathBuf::from(path);
        return if path.is_file() {
            BinResolution::Found(path)
        } else {
            BinResolution::MissingEnv(path)
        };
    }

    let Some(paths) = env::var_os("PATH") else {
        return BinResolution::MissingPath;
    };

    for dir in env::split_paths(&paths) {
        let candidate = dir.join(bin_name);
        if candidate.is_file() {
            return BinResolution::Found(candidate);
        }
    }

    BinResolution::MissingPath
}