zagens-cli 0.8.3

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Import structured sub-agent findings into scratchpad notes.

use serde_json::json;
use zagens_core::subagent::{
    AuditFindingItem, CompletionReason, ParseFailureReason, StructuredFindings, StructuredVerdict,
    SubAgentResult,
};

use crate::tools::spec::ToolError;

use super::ScratchpadStore;
use super::schema::NoteLine;
use super::{compute_superseded_ids, is_high_severity, is_open_finding};

/// Reject importing an agent into a scratchpad run it was not spawned for.
pub fn validate_agent_run_binding(
    agent_scratchpad_run_id: Option<&str>,
    target_run_id: &str,
    agent_id: &str,
) -> Result<(), ToolError> {
    if let Some(bound) = agent_scratchpad_run_id
        && bound != target_run_id
    {
        return Err(ToolError::invalid_input(format!(
            "agent '{agent_id}' was spawned for scratchpad run '{bound}'; cannot import into '{target_run_id}'"
        )));
    }
    Ok(())
}

fn validate_area_id_in_inventory(store: &ScratchpadStore, area_id: &str) -> Result<(), ToolError> {
    let area_id = area_id.trim();
    if area_id.is_empty() || area_id == "_global" {
        return Ok(());
    }
    let inventory = store.read_inventory()?;
    if !inventory.areas.iter().any(|a| a.id == area_id) {
        return Err(ToolError::invalid_input(format!(
            "area_id '{area_id}' is not in scratchpad run '{}' inventory; agent output may belong to a different audit run",
            store.run_id()
        )));
    }
    Ok(())
}

fn normalize_area_path(path: &str) -> String {
    path.replace('\\', "/")
        .trim()
        .trim_end_matches('/')
        .to_string()
}

/// Resolve inventory row for import: explicit override → agent area_id → area_path match.
fn resolve_import_area_id(
    store: &ScratchpadStore,
    findings: &StructuredFindings,
    area_id_override: Option<&str>,
) -> Result<String, ToolError> {
    if let Some(override_id) = area_id_override.map(str::trim).filter(|s| !s.is_empty()) {
        validate_area_id_in_inventory(store, override_id)?;
        return Ok(override_id.to_string());
    }

    let agent_area_id = findings.area_id.trim();
    if !agent_area_id.is_empty() && validate_area_id_in_inventory(store, agent_area_id).is_ok() {
        return Ok(agent_area_id.to_string());
    }

    if let Some(path) = findings
        .area_path
        .as_deref()
        .map(normalize_area_path)
        .filter(|p| !p.is_empty())
    {
        let inventory = store.read_inventory()?;
        if let Some(area) = inventory
            .areas
            .iter()
            .find(|a| normalize_area_path(&a.path) == path)
        {
            return Ok(area.id.clone());
        }
    }

    Err(ToolError::invalid_input(format!(
        "area_id '{}' is not in scratchpad run '{}' inventory; pass scratchpad_import_agent area_id override or ensure structured_findings.area_id / area_path match inventory",
        if agent_area_id.is_empty() {
            "(empty)"
        } else {
            agent_area_id
        },
        store.run_id()
    )))
}

/// Import `structured_findings` (preferred) or `structured_verdict` from a completed sub-agent.
pub fn import_agent_findings(
    store: &ScratchpadStore,
    result: &SubAgentResult,
    area_id_override: Option<&str>,
) -> Result<Vec<NoteLine>, ToolError> {
    if !matches!(
        result.status,
        zagens_core::subagent::SubAgentStatus::Completed
    ) {
        return Err(ToolError::invalid_input(format!(
            "agent '{}' status is {:?}; import only after completion",
            result.agent_id, result.status
        )));
    }

    if matches!(
        result.completion_reason,
        Some(CompletionReason::StepLimitReached)
            | Some(CompletionReason::StepApiTimeout)
            | Some(CompletionReason::Cancelled)
    ) {
        return Err(ToolError::invalid_input(format!(
            "agent '{}' completion_reason is {:?}; import only after NaturalBreak (re-spawn or narrow scope)",
            result.agent_id, result.completion_reason
        )));
    }

    let source = format!("agent:{}", result.agent_id);
    let mut imported = Vec::new();

    if let Some(findings) = &result.structured_findings {
        let area_id = resolve_import_area_id(store, findings, area_id_override)?;
        let mut remapped = findings.clone();
        remapped.area_id = area_id;
        imported.extend(import_structured_findings(store, &remapped, &source)?);
        return Ok(imported);
    }

    if let Some(verdict) = &result.structured_verdict {
        let area_id = area_id_override
            .map(str::to_string)
            .unwrap_or_else(|| "_global".to_string());
        validate_area_id_in_inventory(store, &area_id)?;
        imported.extend(import_verdict_as_findings(
            store, &area_id, None, verdict, &source,
        )?);
        return Ok(imported);
    }

    Err(ToolError::invalid_input(format!(
        "agent '{}' has no structured_findings or structured_verdict; re-run explorer with <!-- audit-findings --> output{}",
        result.agent_id,
        structured_import_hint(result)
    )))
}

fn structured_import_hint(result: &SubAgentResult) -> String {
    match result.structured_findings_parse_failure {
        Some(ParseFailureReason::Truncated) => {
            "; <!-- audit-findings --> was truncated — retry agent_result(block) or import with area_id override; salvage may recover partial items"
                .to_string()
        }
        Some(ParseFailureReason::NoMarker) => {
            "; final output missing <!-- audit-findings --> JSON fence".to_string()
        }
        Some(ParseFailureReason::InvalidJson(ref e)) => format!("; invalid JSON: {e}"),
        None => String::new(),
    }
}

fn import_structured_findings(
    store: &ScratchpadStore,
    findings: &StructuredFindings,
    source: &str,
) -> Result<Vec<NoteLine>, ToolError> {
    let area_id = findings.area_id.trim();
    if area_id.is_empty() {
        return Err(ToolError::invalid_input(
            "structured_findings.area_id must not be empty",
        ));
    }
    let mut out = Vec::new();
    for item in &findings.items {
        out.push(append_open_finding(
            store,
            area_id,
            findings.area_path.as_deref(),
            item,
            source,
        )?);
    }
    if findings.items.is_empty() {
        out.push(store.append_note(json!({
            "area_id": area_id,
            "kind": "cleared",
            "claim": build_import_cleared_claim(findings),
            "status": "open",
            "source": source,
        }))?);
    }
    Ok(out)
}

fn build_import_cleared_claim(findings: &StructuredFindings) -> String {
    let dim_tags = findings
        .dimensions
        .as_ref()
        .filter(|dims| !dims.is_empty())
        .map(|dims| {
            dims.iter()
                .map(|d| {
                    let t = d.trim();
                    if t.starts_with('[') {
                        t.to_string()
                    } else {
                        format!("[{t}]")
                    }
                })
                .collect::<Vec<_>>()
                .join(" ")
        })
        .unwrap_or_else(|| "[D3]".to_string());
    let summary = findings
        .summary
        .as_deref()
        .unwrap_or("no actionable findings after read/grep");
    let path = findings
        .area_path
        .as_deref()
        .unwrap_or(findings.area_id.as_str());
    format!("{dim_tags} agent import for {path} — examined files; {summary}")
}

fn import_verdict_as_findings(
    store: &ScratchpadStore,
    area_id: &str,
    area_path: Option<&str>,
    verdict: &StructuredVerdict,
    source: &str,
) -> Result<Vec<NoteLine>, ToolError> {
    let mut out = Vec::new();
    for item in &verdict.items {
        let audit_item = AuditFindingItem {
            kind: "finding".to_string(),
            severity: item.severity.clone(),
            file: Some(item.file.clone()),
            line: item.line,
            line_end: None,
            claim: item.description.clone(),
            evidence: item.suggestion.clone(),
        };
        out.push(append_open_finding(
            store,
            area_id,
            area_path,
            &audit_item,
            source,
        )?);
    }
    Ok(out)
}

fn append_open_finding(
    store: &ScratchpadStore,
    area_id: &str,
    area_path: Option<&str>,
    item: &AuditFindingItem,
    source: &str,
) -> Result<NoteLine, ToolError> {
    let kind = if item.kind.eq_ignore_ascii_case("cleared") {
        "cleared"
    } else if item.kind.eq_ignore_ascii_case("meta") {
        "meta"
    } else {
        "finding"
    };
    let mut line = json!({
        "area_id": area_id,
        "kind": kind,
        "claim": item.claim,
        "status": "open",
        "source": source,
    });
    if let Some(path) = area_path {
        line["area"] = json!(path);
    }
    if kind == "finding" {
        if !item.severity.trim().is_empty() {
            line["severity"] = json!(item.severity.to_uppercase());
        }
        if let Some(ref f) = item.file {
            line["file"] = json!(f);
        }
        if let Some(l) = item.line {
            line["line"] = json!(l);
        }
        if let Some(l) = item.line_end {
            line["line_end"] = json!(l);
        }
        if let Some(ref e) = item.evidence {
            line["evidence"] = json!(e);
        }
    }
    store.append_note(line)
}

/// Promote an open note to `verified` by appending a superseding verified row (append-only).
pub fn verify_note(store: &ScratchpadStore, note_id: &str) -> Result<NoteLine, ToolError> {
    let notes = store.read_notes()?;
    let superseded = compute_superseded_ids(&notes);
    let original = notes
        .iter()
        .find(|n| n.id == note_id && !superseded.contains(note_id))
        .ok_or_else(|| ToolError::invalid_input(format!("note id '{note_id}' not found")))?;

    if original.status.eq_ignore_ascii_case("verified") {
        return Err(ToolError::invalid_input(format!(
            "note '{note_id}' is already verified"
        )));
    }

    let mut line = json!({
        "area_id": original.area_id,
        "kind": original.kind,
        "claim": original.claim.clone().unwrap_or_default(),
        "status": "verified",
        "source": "main",
        "supersedes": note_id,
    });
    if let Some(area) = &original.area {
        line["area"] = json!(area);
    }
    if let Some(sev) = &original.severity {
        line["severity"] = json!(sev);
    }
    if let Some(f) = &original.file {
        line["file"] = json!(f);
    }
    if let Some(l) = original.line {
        line["line"] = json!(l);
    }
    if let Some(l) = original.line_end {
        line["line_end"] = json!(l);
    }
    if let Some(e) = &original.evidence {
        line["evidence"] = json!(e);
    }
    store.append_note(line)
}

/// Returns open HIGH/BLOCKER finding ids for an area (post-supersedes).
pub fn open_high_finding_ids(
    store: &ScratchpadStore,
    area_id: &str,
) -> Result<Vec<String>, ToolError> {
    let notes = store.read_notes()?;
    let superseded = compute_superseded_ids(&notes);
    Ok(notes
        .iter()
        .filter(|n| {
            n.area_id == area_id
                && is_open_finding(n, &superseded)
                && is_high_severity(n.severity.as_deref())
        })
        .map(|n| n.id.clone())
        .collect())
}

#[cfg(test)]
mod import_gate_tests {
    use super::*;
    use crate::scratchpad::{ScratchpadStore, default_init_areas};
    use crate::tools::spec::ToolContext;
    use std::sync::atomic::{AtomicU64, Ordering};
    use zagens_core::subagent::{
        CompletionReason, StructuredFindings, SubAgentAssignment, SubAgentResult, SubAgentStatus,
        SubAgentType,
    };

    static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn temp_store() -> (tempfile::TempDir, ScratchpadStore) {
        let n = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir = tempfile::tempdir().expect("tempdir");
        let ws = dir.path().join(format!("ws-{n}"));
        std::fs::create_dir_all(&ws).expect("mkdir ws");
        let mut ctx = ToolContext::new(ws);
        ctx.runtime.wire.active_thread_id = Some(format!("thr-{n}"));
        let run_id = format!("thr-{n}");
        let store = ScratchpadStore::init(&ctx, &run_id, default_init_areas(), None)
            .expect("init scratchpad");
        (dir, store)
    }

    fn completed_result(reason: Option<CompletionReason>) -> SubAgentResult {
        SubAgentResult {
            agent_id: "agent_gate".into(),
            agent_type: SubAgentType::Explore,
            assignment: SubAgentAssignment::new("task".into(), None),
            model: "deepseek-v4-flash".into(),
            nickname: None,
            status: SubAgentStatus::Completed,
            result: Some("done".into()),
            steps_taken: 1,
            duration_ms: 100,
            from_prior_session: false,
            structured_findings: Some(StructuredFindings {
                area_id: "workspace".into(),
                area_path: None,
                items: vec![],
                dimensions: Some(vec!["D2".into(), "D3".into()]),
                summary: Some("no actionable findings after read/grep".into()),
            }),
            structured_verdict: None,
            completion_reason: reason,
            max_steps: 100,
            step_timeout_ms: 600_000,
            structured_findings_parse_failure: None,
            scratchpad_run_id: None,
            parent_thread_id: None,
            progress_status: None,
            stuck_suspected: false,
            idle_ms: 0,
        }
    }

    #[test]
    fn import_rejects_step_limit_completion_reason() {
        let (_dir, store) = temp_store();
        let result = completed_result(Some(CompletionReason::StepLimitReached));
        let err = import_agent_findings(&store, &result, None).expect_err("gate");
        assert!(err.to_string().contains("StepLimitReached"));
    }

    #[test]
    fn import_allows_natural_break_completion_reason() {
        let (_dir, store) = temp_store();
        let result = completed_result(Some(CompletionReason::NaturalBreak));
        let notes = import_agent_findings(&store, &result, None).expect("import ok");
        assert_eq!(notes.len(), 1);
    }

    #[test]
    fn validate_agent_run_binding_rejects_mismatched_run() {
        let err =
            validate_agent_run_binding(Some("run-a"), "run-b", "agent_x").expect_err("mismatch");
        assert!(err.to_string().contains("run-a"));
        assert!(err.to_string().contains("run-b"));
    }

    #[test]
    fn import_rejects_area_id_not_in_target_run_inventory() {
        let (_dir, store) = temp_store();
        let mut result = completed_result(Some(CompletionReason::NaturalBreak));
        if let Some(ref mut findings) = result.structured_findings {
            findings.area_id = "stale-area-from-old-run".into();
        }
        let err = import_agent_findings(&store, &result, None).expect_err("bad area");
        assert!(err.to_string().contains("stale-area-from-old-run"));
    }

    #[test]
    fn import_honors_area_id_override_for_structured_findings() {
        let (_dir, store) = temp_store();
        let mut result = completed_result(Some(CompletionReason::NaturalBreak));
        if let Some(ref mut findings) = result.structured_findings {
            findings.area_id = "area-core-engine-part1".into();
            findings.area_path = Some("crates/core/src/engine".into());
        }
        let notes =
            import_agent_findings(&store, &result, Some("workspace")).expect("override import");
        assert_eq!(notes.len(), 1);
        assert_eq!(notes[0].area_id, "workspace");
    }

    #[test]
    fn import_remapped_by_area_path_when_agent_area_id_wrong() {
        let (_dir, store) = temp_store();
        let mut result = completed_result(Some(CompletionReason::NaturalBreak));
        if let Some(ref mut findings) = result.structured_findings {
            findings.area_id = "area-core-engine-part1".into();
            findings.area_path = Some(".".into());
        }
        let notes = import_agent_findings(&store, &result, None).expect("path remap");
        assert_eq!(notes.len(), 1);
        assert_eq!(notes[0].area_id, "workspace");
    }
}