ascent-research 0.3.1

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
use chrono::Utc;
use serde_json::{Value, json};
use std::fs;
use std::path::Path;

use crate::fetch::{
    self,
    smell::{ShortBodyMode, SmellConfig},
};
use crate::output::Envelope;
use crate::route::{self, Executor as RouteExecutor};
use crate::session::{
    active, config,
    event::{RejectReason, RouteDecision, SessionEvent, ToolCallStatus},
    layout, log, sources_block,
};

const CMD: &str = "research add";
const DEFAULT_TIMEOUT_MS: u64 = 30_000;

pub fn run(
    url: &str,
    slug_arg: Option<&str>,
    timeout_ms_arg: Option<u64>,
    readable_flag: bool,
    no_readable_flag: bool,
    min_bytes_arg: Option<u64>,
    on_short_body_arg: Option<&str>,
) -> Envelope {
    let slug = match slug_arg {
        Some(s) => s.to_string(),
        None => match active::get_active() {
            Some(s) => s,
            None => {
                return Envelope::fail(
                    CMD,
                    "NO_ACTIVE_SESSION",
                    "no active session — pass --slug or run `research new` first",
                );
            }
        },
    };
    if !config::exists(&slug) {
        return Envelope::fail(CMD, "SESSION_NOT_FOUND", format!("no session '{slug}'"))
            .with_context(json!({ "session": slug, "url": url }));
    }

    let cfg = match config::read(&slug) {
        Ok(c) => c,
        Err(e) => return Envelope::fail(CMD, "IO_ERROR", format!("read session.toml: {e}")),
    };

    let timeout_ms = timeout_ms_arg
        .or_else(|| {
            std::env::var("ACTIONBOOK_RESEARCH_ADD_TIMEOUT_MS")
                .ok()
                .and_then(|s| s.parse().ok())
        })
        .unwrap_or(DEFAULT_TIMEOUT_MS);

    let readable = if no_readable_flag {
        false
    } else if readable_flag {
        true
    } else {
        looks_like_article(url)
    };

    let existing = log::read_all(&slug).unwrap_or_default();
    if existing.iter().any(|e| match e {
        SessionEvent::SourceAccepted { url: u, .. } => u == url,
        _ => false,
    }) {
        let ev = SessionEvent::SourceRejected {
            timestamp: Utc::now(),
            url: url.into(),
            kind: "duplicate".into(),
            executor: "n/a".into(),
            reason: RejectReason::Duplicate,
            observed_url: None,
            observed_bytes: None,
            rejected_raw_path: None,
            note: None,
        };
        let _ = log::append(&slug, &ev);
        return Envelope::fail(
            CMD,
            "SMELL_REJECTED",
            format!("URL '{url}' already accepted in this session"),
        )
        .with_context(json!({ "session": slug, "url": url }))
        .with_details(reject_details(
            &RouteDecision {
                executor: "n/a".into(),
                kind: "duplicate".into(),
                command_template: "".into(),
            },
            RejectReason::Duplicate,
            0,
            None,
            None,
            &["duplicate URL in session".to_string()],
            false,
        ));
    }

    let compiled = match route::load_preset(Some(&cfg.preset), None) {
        Ok(p) => p,
        Err(e) => {
            return Envelope::fail(CMD, "PRESET_ERROR", e.message.clone()).with_details(json!({
                "sub_code": e.sub_code.as_str(),
            }));
        }
    };
    let classification = match route::classify(&compiled, url, false) {
        Ok(c) => c,
        Err(msg) => return Envelope::fail(CMD, "INVALID_ARGUMENT", msg),
    };
    let r = classification.route();
    let route_decision = RouteDecision {
        executor: r.executor.as_str().into(),
        kind: r.kind.clone(),
        command_template: r.command_template.clone(),
    };

    let raw_n = log::next_raw_index(&existing);
    let host = extract_host(url).unwrap_or_else(|| "unknown".into());

    let attempted = SessionEvent::SourceAttempted {
        timestamp: Utc::now(),
        url: url.into(),
        route_decision: route_decision.clone(),
        note: None,
    };
    if let Err(e) = log::append(&slug, &attempted) {
        return Envelope::fail(CMD, "IO_ERROR", format!("append attempted: {e}"));
    }

    let smell_cfg = match parse_smell_config(min_bytes_arg, on_short_body_arg) {
        Ok(c) => c,
        Err(e) => {
            return Envelope::fail(CMD, "INVALID_ARGUMENT", e)
                .with_context(json!({ "session": slug, "url": url }));
        }
    };

    let fetch_start = std::time::Instant::now();
    let call_id = format!("fetch-{raw_n}");
    let started = SessionEvent::ToolCallStarted {
        timestamp: Utc::now(),
        call_id: call_id.clone(),
        hand: hand_name(&route_decision.executor).into(),
        tool: tool_name(&route_decision.executor).into(),
        input_summary: format!("url={url} kind={} readable={readable}", r.kind),
        note: None,
    };
    if let Err(e) = log::append(&slug, &started) {
        return Envelope::fail(CMD, "IO_ERROR", format!("append tool_call_started: {e}"));
    }
    let (raw_bytes, outcome, executor_str) = fetch::execute(
        &route_decision,
        &slug,
        raw_n,
        url,
        readable,
        timeout_ms,
        smell_cfg,
    );
    let duration_ms = fetch_start.elapsed().as_millis() as u64;

    let raw_dir = layout::session_raw_dir(&slug);
    if let Err(e) = fs::create_dir_all(&raw_dir) {
        return Envelope::fail(CMD, "IO_ERROR", format!("create raw/: {e}"));
    }
    let base = format!(
        "{raw_n}-{kind}-{host}",
        kind = r.kind,
        host = sanitize(&host)
    );

    if outcome.accepted {
        let raw_path = raw_dir.join(format!("{base}.json"));
        if let Err(e) = fs::write(&raw_path, &raw_bytes) {
            return Envelope::fail(CMD, "IO_ERROR", format!("write raw: {e}"));
        }

        let trust = trust_score(r.executor, readable, outcome.bytes);
        let completed = SessionEvent::ToolCallCompleted {
            timestamp: Utc::now(),
            call_id,
            status: ToolCallStatus::Ok,
            duration_ms,
            output_summary: output_summary(
                outcome.bytes,
                outcome.observed_bytes,
                &outcome.warnings,
            ),
            artifact_refs: vec![rel_path(&raw_path)],
            error_code: None,
            note: None,
        };
        if let Err(e) = log::append(&slug, &completed) {
            return Envelope::fail(CMD, "IO_ERROR", format!("append tool_call_completed: {e}"));
        }
        let accepted_ev = SessionEvent::SourceAccepted {
            timestamp: Utc::now(),
            url: url.into(),
            kind: r.kind.clone(),
            executor: executor_str.clone(),
            raw_path: rel_path(&raw_path),
            bytes: outcome.bytes,
            trust_score: trust,
            note: None,
        };
        if let Err(e) = log::append(&slug, &accepted_ev) {
            return Envelope::fail(CMD, "IO_ERROR", format!("append accepted: {e}"));
        }

        let all = log::read_all(&slug).unwrap_or_default();
        if let Err(e) = sources_block::rebuild(&slug, &all) {
            if let sources_block::RewriteError::MarkerMissing(_) = e {
                return Envelope::fail(
                    CMD,
                    "SESSION_MD_MARKER_MISSING",
                    "session.md missing sources markers (regenerate with `research new` template)",
                )
                .with_context(json!({ "session": slug, "url": url }));
            }
            eprintln!("⚠ session.md rewrite failed");
        }

        return Envelope::ok(
            CMD,
            json!({
                "route_decision": {
                    "executor": route_decision.executor,
                    "kind": route_decision.kind,
                    "command_template": route_decision.command_template,
                },
                "fetch_success": true,
                "smell_pass": true,
                "bytes": outcome.bytes,
                "warnings": outcome.warnings,
                "raw_path": rel_path(&raw_path),
                "trust_score": trust,
                "duration_ms": duration_ms,
            }),
        )
        .with_context(json!({ "session": slug, "url": url }));
    }

    let rejected_path = raw_dir.join(format!("{base}.rejected.json"));
    let _ = fs::write(&rejected_path, &raw_bytes);

    let reason = outcome.reject_reason.unwrap_or(RejectReason::FetchFailed);
    let fetch_success = !matches!(reason, RejectReason::FetchFailed);
    let status = if fetch_success {
        ToolCallStatus::Ok
    } else {
        ToolCallStatus::Error
    };
    let completed = SessionEvent::ToolCallCompleted {
        timestamp: Utc::now(),
        call_id,
        status,
        duration_ms,
        output_summary: output_summary(outcome.bytes, outcome.observed_bytes, &outcome.warnings),
        artifact_refs: vec![rel_path(&rejected_path)],
        error_code: (!fetch_success).then(|| reason_str(reason).to_string()),
        note: None,
    };
    if let Err(e) = log::append(&slug, &completed) {
        return Envelope::fail(CMD, "IO_ERROR", format!("append tool_call_completed: {e}"));
    }

    let rejected_ev = SessionEvent::SourceRejected {
        timestamp: Utc::now(),
        url: url.into(),
        kind: r.kind.clone(),
        executor: executor_str.clone(),
        reason,
        observed_url: outcome.observed_url.clone(),
        observed_bytes: Some(outcome.observed_bytes),
        rejected_raw_path: Some(rel_path(&rejected_path)),
        note: None,
    };
    let _ = log::append(&slug, &rejected_ev);

    Envelope::fail(
        CMD,
        "SMELL_REJECTED",
        format!("source rejected: {}", reason_str(reason)),
    )
    .with_context(json!({ "session": slug, "url": url }))
    .with_details(reject_details(
        &route_decision,
        reason,
        outcome.bytes,
        outcome.observed_url.clone(),
        Some(rel_path(&rejected_path)),
        &outcome.warnings,
        fetch_success,
    ))
}

fn reject_details(
    decision: &RouteDecision,
    reason: RejectReason,
    bytes: u64,
    observed_url: Option<String>,
    rejected_raw_path: Option<String>,
    warnings: &[String],
    fetch_success: bool,
) -> Value {
    json!({
        "route_decision": {
            "executor": decision.executor,
            "kind": decision.kind,
            "command_template": decision.command_template,
        },
        "fetch_success": fetch_success,
        "smell_pass": false,
        "bytes": bytes,
        "warnings": warnings,
        "reject_reason": reason_str(reason),
        "observed_url": observed_url,
        "rejected_raw_path": rejected_raw_path,
    })
}

fn reason_str(r: RejectReason) -> &'static str {
    match r {
        RejectReason::FetchFailed => "fetch_failed",
        RejectReason::WrongUrl => "wrong_url",
        RejectReason::EmptyContent => "empty_content",
        RejectReason::ApiError => "api_error",
        RejectReason::Duplicate => "duplicate",
    }
}

fn hand_name(executor: &str) -> &'static str {
    match executor {
        "postagent" => "postagent",
        "browser" => "actionbook",
        "local" => "local",
        _ => "research-cli",
    }
}

fn tool_name(executor: &str) -> &'static str {
    match executor {
        "postagent" => "postagent send",
        "browser" => "actionbook browser",
        "local" => "local read_file",
        _ => "research-cli",
    }
}

fn output_summary(bytes: u64, observed_bytes: u64, warnings: &[String]) -> String {
    format!(
        "bytes={bytes} observed_bytes={observed_bytes} warnings={}",
        warnings.len()
    )
}

fn looks_like_article(url: &str) -> bool {
    let l = url.to_lowercase();
    ["/blog/", "/post/", "/rfd/", "/paper/", "/article/"]
        .iter()
        .any(|s| l.contains(s))
        || url.split('/').filter(|s| !s.is_empty()).count() >= 4
}

fn extract_host(url: &str) -> Option<String> {
    let rest = url
        .strip_prefix("https://")
        .or_else(|| url.strip_prefix("http://"))?;
    let authority = rest.split('/').next()?;
    let host = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
    Some(host.split(':').next()?.to_ascii_lowercase())
}

fn sanitize(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '.' || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect()
}

fn rel_path(p: &Path) -> String {
    let comps: Vec<_> = p.components().collect();
    let n = comps.len();
    if n >= 2 {
        format!(
            "{}/{}",
            comps[n - 2].as_os_str().to_string_lossy(),
            comps[n - 1].as_os_str().to_string_lossy()
        )
    } else {
        p.to_string_lossy().into_owned()
    }
}

/// Parse `--min-bytes` + `--on-short-body` into a SmellConfig. Shared by
/// `add` and `batch` — keep the validation logic in one place.
pub(crate) fn parse_smell_config(
    min_bytes: Option<u64>,
    on_short_body: Option<&str>,
) -> Result<SmellConfig, String> {
    let short_body_mode = match on_short_body {
        None | Some("reject") => ShortBodyMode::Reject,
        Some("warn") => ShortBodyMode::Warn,
        Some(other) => {
            return Err(format!(
                "invalid --on-short-body value '{other}': expected 'warn' or 'reject'"
            ));
        }
    };
    Ok(SmellConfig {
        min_bytes_override: min_bytes,
        short_body_mode,
    })
}

fn trust_score(exec: RouteExecutor, readable: bool, bytes: u64) -> f64 {
    match exec {
        RouteExecutor::Postagent => 2.0,
        RouteExecutor::Browser if readable && bytes >= 2000 => 1.5,
        RouteExecutor::Browser => 1.0,
        // Local reads: content is already on disk, user supplied the
        // path → same trust tier as a primary-source API.
        RouteExecutor::Local => 2.0,
    }
}