a3s 0.9.5

a3s — A3S coding agent CLI; `a3s code` launches the interactive TUI
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
use super::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResearchDiagnosticKind {
    Status,
    Explain,
    Replay,
}

pub(crate) async fn research_diagnostic(
    workspace: &Path,
    run_id: Option<&str>,
    kind: ResearchDiagnosticKind,
) -> Result<String> {
    let (runtime, projection) = if let Some(run_id) = run_id {
        let journal = DeepResearchStateJournal::open(workspace, run_id)
            .await?
            .with_context(|| format!("DeepResearch run `{run_id}` was not found"))?;
        let projection = journal.projection()?;
        (journal.runtime, projection)
    } else {
        load_latest_journal(workspace).await?
    };
    let head = graph_event_head(runtime.events()).unwrap_or("none");
    let coverage = projection
        .report_claim_coverage_basis_points
        .map(|basis_points| format!("{:.1}%", f32::from(basis_points) / 100.0))
        .unwrap_or_else(|| "not audited".to_string());
    let common = format!(
        "DeepResearch run {}\noutcome: {}\nevidence: {} accepted · {} sources · {} claims\nactive: {} steps · {} children\nclaim coverage: {}",
        projection.run_id,
        outcome_name(projection.outcome),
        projection.accepted_evidence_count,
        projection.source_count,
        projection.claim_count,
        projection.active_steps.len(),
        projection.active_children.len(),
        coverage,
    );
    Ok(match kind {
        ResearchDiagnosticKind::Status => common,
        ResearchDiagnosticKind::Explain => format!(
            "{}\nconvergence: {}\nreport audit: {}",
            common,
            projection
                .convergence_reason
                .as_deref()
                .unwrap_or("not evaluated"),
            projection
                .report_audit_reason
                .as_deref()
                .unwrap_or("not audited"),
        ),
        ResearchDiagnosticKind::Replay => format!(
            "{}\nstrict replay: ok\nevents: {}\ngraph: {} objects · {} relations\nhead: {}",
            common,
            runtime.events().len(),
            runtime.graph().objects().count(),
            runtime.graph().relations().count(),
            head,
        ),
    })
}

pub(crate) async fn research_diff(
    workspace: &Path,
    left_run_id: &str,
    right_run_id: &str,
) -> Result<String> {
    let left = DeepResearchStateJournal::open(workspace, left_run_id)
        .await?
        .with_context(|| format!("DeepResearch run `{left_run_id}` was not found"))?;
    let right = DeepResearchStateJournal::open(workspace, right_run_id)
        .await?
        .with_context(|| format!("DeepResearch run `{right_run_id}` was not found"))?;
    let diff = left.runtime.diff(&right.runtime);
    let object_summary = summarize_object_diff(&diff);
    let relation_summary = summarize_relation_diff(&diff);
    Ok(format!(
        "DeepResearch structural diff\nleft: {} · {} events\nright: {} · {} events\nobjects: {}\nrelations: {}\nempty: {}",
        left_run_id,
        left.runtime.events().len(),
        right_run_id,
        right.runtime.events().len(),
        object_summary,
        relation_summary,
        diff.is_empty(),
    ))
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ResearchForkSummary {
    pub(crate) branch_store_id: String,
    pub(crate) fork_sequence: u64,
    pub(crate) objects_added: usize,
    pub(crate) relations_added: usize,
}

pub(crate) async fn fork_with_validated_evidence(
    workspace: &Path,
    run_id: &str,
    fork_sequence: u64,
    branch_name: &str,
    evidence: &[super::super::deep_research_evidence_ledger::AcceptedEvidence],
) -> Result<ResearchForkSummary> {
    validate_run_id(run_id)?;
    validate_run_id(branch_name).context("invalid DeepResearch branch name")?;
    if evidence.is_empty() {
        anyhow::bail!("a research strategy fork requires validated evidence");
    }
    let base = DeepResearchStateJournal::open(workspace, run_id)
        .await?
        .with_context(|| format!("DeepResearch run `{run_id}` was not found"))?;
    let fork = base
        .runtime
        .fork_at(fork_sequence)
        .context("fork DeepResearch graph at a strict event boundary")?;
    let branch_store_id = format!("branch-{run_id}-{branch_name}");
    let store = FileGraphEventStore::new(store_root(workspace));
    if store.load(&branch_store_id).await?.is_some() {
        anyhow::bail!("DeepResearch branch `{branch_name}` already exists");
    }
    match store
        .save_if_head(&branch_store_id, None, fork.events())
        .await?
    {
        GraphSaveOutcome::Saved => {}
        GraphSaveOutcome::Conflict { .. } => {
            anyhow::bail!("DeepResearch branch `{branch_name}` was created concurrently")
        }
    }
    let mut branch = DeepResearchStateJournal {
        store,
        checkpoint_root: checkpoint_root(workspace),
        store_id: branch_store_id.clone(),
        run_id: run_id.to_string(),
        runtime: fork,
    };
    branch
        .append_evidence(
            ResearchDomainEvent {
                source: format!("evidence_branch_{branch_name}"),
                source_sequence: 1,
                source_event_id: format!("{run_id}:branch:{branch_name}:evidence"),
                name: "research.evidence.accepted".to_string(),
                payload: serde_json::json!({
                    "branch": branch_name,
                    "accepted_evidence": evidence.len(),
                    "sources": evidence.iter().map(|item| item.sources.len()).sum::<usize>(),
                    "claims": evidence.iter().map(|item| item.claims.len()).sum::<usize>(),
                }),
            },
            evidence,
        )
        .await?;
    let diff = base.runtime.diff(&branch.runtime);
    Ok(ResearchForkSummary {
        branch_store_id,
        fork_sequence,
        objects_added: diff.objects_added.len(),
        relations_added: diff.relations_added.len(),
    })
}

pub(crate) async fn fork_current_for_contradiction_review(
    workspace: &Path,
    run_id: &str,
    evidence: &[super::super::deep_research_evidence_ledger::AcceptedEvidence],
) -> Result<ResearchForkSummary> {
    if !evidence.iter().any(|item| !item.contradictions.is_empty()) {
        anyhow::bail!("contradiction review fork requires contradictory evidence");
    }
    let journal = DeepResearchStateJournal::open(workspace, run_id)
        .await?
        .with_context(|| format!("DeepResearch run `{run_id}` was not found"))?;
    let sequence = u64::try_from(journal.runtime.events().len())
        .context("DeepResearch event sequence exceeds u64")?;
    drop(journal);
    fork_with_validated_evidence(
        workspace,
        run_id,
        sequence,
        "contradiction-review",
        evidence,
    )
    .await
}

fn summarize_object_diff(diff: &a3s_code_core::state_graph::GraphDiff) -> String {
    let mut counts = std::collections::BTreeMap::<String, (usize, usize, usize)>::new();
    for object in &diff.objects_added {
        counts.entry(object.object_type.clone()).or_default().0 += 1;
    }
    for object in &diff.objects_removed {
        counts.entry(object.object_type.clone()).or_default().1 += 1;
    }
    for (left, _) in &diff.objects_changed {
        counts.entry(left.object_type.clone()).or_default().2 += 1;
    }
    if counts.is_empty() {
        return "no changes".to_string();
    }
    counts
        .into_iter()
        .map(|(kind, (added, removed, changed))| format!("{kind} +{added}/-{removed}/~{changed}"))
        .collect::<Vec<_>>()
        .join(" · ")
}

fn summarize_relation_diff(diff: &a3s_code_core::state_graph::GraphDiff) -> String {
    let mut counts = std::collections::BTreeMap::<String, (usize, usize, usize)>::new();
    for relation in &diff.relations_added {
        counts.entry(relation.relation_type.clone()).or_default().0 += 1;
    }
    for relation in &diff.relations_removed {
        counts.entry(relation.relation_type.clone()).or_default().1 += 1;
    }
    for (left, _) in &diff.relations_changed {
        counts.entry(left.relation_type.clone()).or_default().2 += 1;
    }
    if counts.is_empty() {
        return "no changes".to_string();
    }
    counts
        .into_iter()
        .map(|(kind, (added, removed, changed))| format!("{kind} +{added}/-{removed}/~{changed}"))
        .collect::<Vec<_>>()
        .join(" · ")
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ResearchRecoverySummary {
    pub(crate) run_id: String,
    pub(crate) cancel_children: Vec<String>,
    pub(crate) orphaned_children: Vec<String>,
}

pub(crate) async fn reconcile_interrupted_latest_run(
    workspace: &Path,
    running_tracker_children: &HashSet<String>,
) -> Result<Option<ResearchRecoverySummary>> {
    let (runtime, projection) = match load_latest_journal(workspace).await {
        Ok(value) => value,
        Err(error)
            if error.to_string().contains("no DeepResearch event journal")
                || error
                    .to_string()
                    .contains("no DeepResearch event journal was found") =>
        {
            return Ok(None)
        }
        Err(error) => return Err(error),
    };
    if projection.outcome.is_terminal() {
        return Ok(None);
    }
    let host_pid = runtime
        .graph()
        .object(&spec_object_id(&projection.run_id))
        .and_then(|object| serde_json::from_value::<ResearchSpec>(object.data.clone()).ok())
        .map(|spec| spec.host_pid)
        .unwrap_or_default();
    if host_pid > 0 && process_is_alive(host_pid) {
        return Ok(None);
    }
    let mut cancel_children = projection
        .active_children
        .iter()
        .filter(|task_id| running_tracker_children.contains(*task_id))
        .cloned()
        .collect::<Vec<_>>();
    let mut orphaned_children = projection
        .active_children
        .iter()
        .filter(|task_id| !running_tracker_children.contains(*task_id))
        .cloned()
        .collect::<Vec<_>>();
    cancel_children.sort();
    orphaned_children.sort();
    let run_id = projection.run_id;
    append_event_with_retry(
        workspace,
        &run_id,
        ResearchDomainEvent {
            source: "recovery".to_string(),
            source_sequence: 1,
            source_event_id: format!("{run_id}:recovery:reconciled"),
            name: "research.recovery.reconciled".to_string(),
            payload: serde_json::json!({
                "cancelled_children": cancel_children,
                "orphaned_children": orphaned_children,
                "reason": "host restarted without a valid parent operation lease",
            }),
        },
    )
    .await?;
    append_event_with_retry(
        workspace,
        &run_id,
        ResearchDomainEvent {
            source: "recovery".to_string(),
            source_sequence: 2,
            source_event_id: format!("{run_id}:recovery:failed"),
            name: "research.run.failed".to_string(),
            payload: serde_json::json!({
                "outcome": "failed",
                "reason": "host restarted without a valid parent operation lease",
            }),
        },
    )
    .await?;
    Ok(Some(ResearchRecoverySummary {
        run_id,
        cancel_children,
        orphaned_children,
    }))
}

fn process_is_alive(pid: u32) -> bool {
    if pid == std::process::id() {
        return true;
    }
    #[cfg(unix)]
    {
        std::process::Command::new("kill")
            .arg("-0")
            .arg(pid.to_string())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .is_ok_and(|status| status.success())
    }
    #[cfg(not(unix))]
    {
        false
    }
}

async fn load_latest_journal(workspace: &Path) -> Result<(GraphRuntime, ResearchRunProjection)> {
    if let Some(checkpoint) = load_latest_checkpoint(workspace).await {
        if let Some(journal) = DeepResearchStateJournal::open(workspace, &checkpoint.run_id).await?
        {
            let head = graph_event_head(journal.runtime.events()).map(str::to_string);
            if head == checkpoint.event_head
                && journal.runtime.events().len() == checkpoint.event_count
                && journal.projection()? == checkpoint.projection
            {
                return Ok((journal.runtime, checkpoint.projection));
            }
        }
    }
    let root = store_root(workspace);
    let mut entries = tokio::fs::read_dir(&root).await.with_context(|| {
        format!(
            "no DeepResearch event journal found under `{}`",
            root.display()
        )
    })?;
    let mut latest: Option<(std::time::SystemTime, PathBuf)> = None;
    while let Some(entry) = entries.next_entry().await? {
        let path = entry.path();
        if path.extension().and_then(|extension| extension.to_str()) != Some("json") {
            continue;
        }
        let metadata = entry.metadata().await?;
        if !metadata.is_file() || metadata.len() > 256 * 1024 * 1024 {
            continue;
        }
        let modified = metadata.modified().unwrap_or(std::time::UNIX_EPOCH);
        if latest
            .as_ref()
            .is_none_or(|(current, _)| modified > *current)
        {
            latest = Some((modified, path));
        }
    }
    let (_, path) = latest.context("no DeepResearch event journal was found")?;
    let bytes = tokio::fs::read(&path).await?;
    let events: Vec<GraphEventRecord> =
        serde_json::from_slice(&bytes).with_context(|| format!("decode `{}`", path.display()))?;
    let runtime =
        GraphRuntime::restore(events).context("strictly replay latest DeepResearch run")?;
    let object = runtime
        .graph()
        .objects()
        .find(|object| object.object_type == RUN_OBJECT_TYPE)
        .context("latest DeepResearch graph has no run projection")?;
    let projection = serde_json::from_value(object.data.clone())?;
    Ok((runtime, projection))
}

pub(super) async fn load_latest_checkpoint(workspace: &Path) -> Option<ResearchCheckpoint> {
    let root = checkpoint_root(workspace);
    let mut entries = tokio::fs::read_dir(root).await.ok()?;
    let mut candidates = Vec::new();
    while let Some(entry) = entries.next_entry().await.ok()? {
        let path = entry.path();
        if path.extension().and_then(|extension| extension.to_str()) != Some("json") {
            continue;
        }
        let metadata = entry.metadata().await.ok()?;
        if !metadata.is_file() || metadata.len() > 1024 * 1024 {
            continue;
        }
        candidates.push((metadata.modified().unwrap_or(std::time::UNIX_EPOCH), path));
    }
    candidates.sort_by(|(left, _), (right, _)| right.cmp(left));
    for (_, path) in candidates.into_iter().take(128) {
        let Ok(bytes) = tokio::fs::read(path).await else {
            continue;
        };
        let Ok(checkpoint) = serde_json::from_slice::<ResearchCheckpoint>(&bytes) else {
            continue;
        };
        if checkpoint.schema_version == 1 && validate_run_id(&checkpoint.run_id).is_ok() {
            return Some(checkpoint);
        }
    }
    None
}