artifactize-core 0.13.1

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
//! Human review: the facts and lifecycle jobs every interface shares. The terminal screens
//! live in `artifactize-tui`.

mod detail;
mod form;
mod runs;
pub mod search;

/// The Tool log every review interface shows; see [`log::Sink`].
pub use crate::tools::human::log;
pub use detail::{Stage, meta, outcome_text, shared, stage, technical};
pub use form::{Field, Form, Input, Key, Row, check as check_schema};
pub use runs::Runs;

use crate::{
    config::{HumanToolKind, ToolName},
    human,
    store::{self, HumanClaim, Request},
    tools::human::{CommandLine, Content, ToolResult},
    types::RequestId,
};
use serde_json::Value;
use std::path::PathBuf;
use tokio_util::sync::CancellationToken;

/// The refusal for reviewing a request nobody claimed.
pub const CLAIM_FIRST: &str = "Claim this request before reviewing it.";
/// The refusal for releasing a claim that is not this reviewer's.
pub const NOT_CLAIMED: &str = "The request is not claimed.";

/// What `reviewer` may do with the request now: its id and whether nobody holds the claim
/// yet, or why it is read-only (loading, settled, claimed by someone else). Every interface
/// asks this, so they refuse alike.
pub fn actionable(
    view: Option<&crate::store::RequestView>,
    reviewer: &crate::types::ReviewerId,
) -> Result<(RequestId, bool), String> {
    let view = view.ok_or("The request is still loading.")?;
    if !view.request.status().is_human_wait() {
        return Err(format!(
            "The request is {}; there is nothing to review.",
            view.request.status()
        ));
    }
    match &view.claim {
        Some(claim) if &claim.reviewer != reviewer => Err(format!(
            "Claimed by {}; read-only for {}.",
            claim.reviewer, reviewer
        )),
        claim => Ok((view.request.id.clone(), claim.is_none())),
    }
}

/// The refusal for choosing a verdict, running a tool or submitting, as the reviewer sees it:
/// only the claimant reviews, and a claim is always the explicit one.
pub fn review_refusal(
    view: Option<&crate::store::RequestView>,
    reviewer: &crate::types::ReviewerId,
) -> Option<String> {
    match actionable(view, reviewer) {
        Ok((_, false)) => None,
        Ok((_, true)) | Err(_) => Some(CLAIM_FIRST.into()),
    }
}

/// The refusal for releasing: only a claim of this reviewer is released.
pub fn release_refusal(
    view: Option<&crate::store::RequestView>,
    reviewer: &crate::types::ReviewerId,
) -> Option<String> {
    match actionable(view, reviewer) {
        Ok((_, false)) => None,
        Ok((_, true)) => Some(NOT_CLAIMED.into()),
        Err(error) => Some(error),
    }
}

/// A Human submission has one verdict, parsed with its owner fields at the UI edge.
/// The private envelope prevents a separate display choice from contradicting the result;
/// the request's owner schema is still checked by the shared Human submission operation.
#[derive(Debug, Clone, PartialEq)]
pub struct Submission(crate::agent::verdict::ValidatedResult);
impl TryFrom<Value> for Submission {
    type Error = String;
    fn try_from(value: Value) -> Result<Self, Self::Error> {
        serde_json::from_value(value)
            .map(Self)
            .map_err(|error| error.to_string())
    }
}
impl Submission {
    pub fn verdict(&self) -> crate::runtime::Verdict {
        self.0.verdict
    }
    pub fn to_json(&self) -> Value {
        self.0.to_json()
    }
}

/// Lifecycle work with owned inputs, so the screen keeps drawing while it runs.
#[derive(Debug, Clone, PartialEq)]
pub enum Job {
    /// Explicit claim for an embedded single-request review.
    Claim { id: RequestId },
    /// Claim first when `claim`, then run the tool.
    Run {
        id: RequestId,
        tool: ToolName,
        claim: bool,
    },
    /// Claim first when `claim`, then submit and publish like `request submit`.
    Submit {
        id: RequestId,
        result: Submission,
        claim: bool,
    },
    /// Release the reviewer's remaining claims; quit afterwards when `quit`.
    Release { ids: Vec<RequestId>, quit: bool },
}

#[derive(Debug)]
pub enum Outcome {
    Claimed {
        result: Result<HumanClaim, String>,
    },
    /// Publishing failed after settlement; never offer to submit the verdict twice.
    SavedLocally {
        request: Box<Request>,
        error: String,
    },
    Ran {
        tool: ToolName,
        claimed: Option<HumanClaim>,
        result: Result<ToolResult, String>,
    },
    Submitted {
        id: RequestId,
        claimed: Option<HumanClaim>,
        result: Result<Box<Request>, String>,
    },
    Released {
        ids: Vec<RequestId>,
        quit: bool,
        /// Request IDs whose release failed, with the error.
        failed: Vec<(RequestId, String)>,
    },
}

impl Outcome {
    /// What the review tells the reviewer about the outcome, and whether it is an error.
    pub fn notice(&self) -> (String, bool) {
        match self {
            Self::SavedLocally { error, .. } => (error.clone(), true),
            Self::Claimed { result: Ok(_) } => (
                "Claimed. Fill GREEN or RED fields, then Submit.".into(),
                false,
            ),
            Self::Claimed { result: Err(error) } => (error.clone(), true),
            Self::Ran { tool, result, .. } => {
                let launched = Content::Launch { launched: true };
                match result {
                    Ok(result) if result.is_error() => (
                        format!("{tool} failed; a tool error is not a verdict."),
                        true,
                    ),
                    Ok(result) if result.content().contains(&launched) => {
                        (format!("{tool} launched."), false)
                    }
                    Ok(_) => (format!("{tool} finished."), false),
                    Err(_) => (format!("{tool} did not run."), true),
                }
            }
            Self::Submitted {
                result: Ok(request),
                ..
            } => (
                format!(
                    "Submitted {} for {} ({}).",
                    request.status(),
                    request.eval_id,
                    request.id
                ),
                false,
            ),
            Self::Submitted {
                result: Err(error), ..
            } => (error.clone(), true),
            Self::Released { failed, .. } if failed.is_empty() => ("Claim released.".into(), false),
            Self::Released { failed, .. } => {
                let lines = failed.iter().map(|(id, error)| format!("{id}: {error}"));
                (lines.collect::<Vec<_>>().join("\n"), true)
            }
        }
    }
}

/// A declared Human tool as the saved definition records it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tool {
    pub name: ToolName,
    /// The Artifact that declares the tool.
    pub artifact: crate::types::ArtifactName,
    pub kind: HumanToolKind,
    pub description: String,
    pub declared: Runs,
}

impl Tool {
    /// Its name in the Artifact that declares it: `open` of `notes` is `open_notes`.
    pub fn operation(&self) -> &str {
        self.name
            .as_str()
            .strip_suffix(&format!("_{}", self.artifact))
            .unwrap_or(self.name.as_str())
    }

    /// What running the tool does, as the Tools pane shows it: a command's resolved line once
    /// `resolved` is known, else its declaration; a builtin's action on its logical target.
    pub fn action(&self, resolved: Option<&Result<CommandLine, String>>) -> String {
        match (&self.declared, resolved) {
            (Runs::Command(_), Some(Ok(command))) => {
                let program = crate::platform::path_text(std::path::Path::new(&command.program));
                let words = std::iter::once(program).chain(command.args.iter().cloned());
                let words: Vec<_> = words.collect();
                format!("$ {}", shell(words.iter().map(String::as_str)))
            }
            (declared, _) => declared.line(),
        }
    }
}

impl Job {
    /// Carry out the job through the `human` lifecycle API, as `request` does; every UI runs
    /// its Human actions through here. A tool run logs to `log` from its start line to its end,
    /// failures included; a launched program's output keeps arriving after the job returns.
    pub async fn run(
        self,
        state: PathBuf,
        reviewer: crate::types::ReviewerId,
        cancel: CancellationToken,
        log: log::Sink,
    ) -> Outcome {
        let mut claimed = None;
        match self {
            Job::Claim { id } => {
                let result = async {
                    let (receipts, _) = human::open(&state, &id).await?;
                    let result = human::claim(&receipts, &id, &reviewer).await;
                    let closed = receipts.close().await;
                    result.and_then(|claim| closed.map(|()| claim))
                }
                .await;
                Outcome::Claimed { result }
            }
            Job::Run {
                id,
                tool,
                claim: first,
            } => {
                // The entry names the tool as the saved request declares it.
                let declared = store::read_request(&state, &id)
                    .await
                    .ok()
                    .and_then(|view| {
                        tools(&view.request)
                            .into_iter()
                            .find(|declared| declared.name == tool)
                    });
                let run = match &declared {
                    Some(declared) => {
                        log.start(&id, declared.operation(), declared.artifact.as_str())
                    }
                    None => log.start(&id, tool.as_str(), ""),
                };
                let result = async {
                    let (receipts, _) = match human::open(&state, &id).await {
                        Ok(opened) => opened,
                        Err(error) => {
                            run.end(log::End::Failed(error.clone()));
                            return Err(error);
                        }
                    };
                    let result = async {
                        if first {
                            match human::claim(&receipts, &id, &reviewer).await {
                                Ok(claim) => claimed = Some(claim),
                                Err(error) => {
                                    run.end(log::End::Failed(error.clone()));
                                    return Err(error);
                                }
                            }
                        }
                        human::run_human_tool(&receipts, &id, &reviewer, &tool, cancel, run).await
                    }
                    .await;
                    let closed = receipts.close().await;
                    result.and_then(|result| closed.map(|()| result))
                }
                .await;
                Outcome::Ran {
                    tool,
                    claimed,
                    result,
                }
            }
            Job::Submit {
                id,
                result,
                claim: first,
            } => {
                let result = async {
                    if first {
                        let (receipts, _) = human::open(&state, &id).await?;
                        let result = human::claim(&receipts, &id, &reviewer).await;
                        let closed = receipts.close().await;
                        claimed = Some(result.and_then(|claim| closed.map(|()| claim))?);
                    }
                    human::submit_and_publish(&state, &id, &reviewer, &result.to_json(), cancel)
                        .await
                        .map(Box::new)
                }
                .await;
                if let Err(error) = &result
                    && let Ok(view) = store::read_request(&state, &id).await
                    && matches!(
                        view.request.status(),
                        crate::types::RequestStatus::Green | crate::types::RequestStatus::Red
                    )
                    && view.execution.as_ref().is_some_and(|execution| {
                        execution.reviewer.as_deref() == Some(reviewer.as_str())
                    })
                {
                    return Outcome::SavedLocally {
                        request: Box::new(view.request),
                        error: error.clone(),
                    };
                }
                Outcome::Submitted {
                    id,
                    claimed,
                    result,
                }
            }
            Job::Release { ids, quit } => {
                let mut failed = Vec::new();
                for id in &ids {
                    let release = async {
                        // Settled requests and claims released elsewhere leave nothing to release.
                        let view = store::read_request(&state, id).await?;
                        if view.claim.is_none_or(|claim| claim.reviewer != reviewer) {
                            return Ok(());
                        }
                        let (receipts, _) = human::open(&state, id).await?;
                        let result = human::unclaim(&receipts, id, &reviewer).await.map(drop);
                        let closed = receipts.close().await;
                        result.and(closed)
                    };
                    if let Err(error) = release.await {
                        failed.push((id.clone(), error));
                    }
                }
                Outcome::Released { ids, quit, failed }
            }
        }
    }
}

/// Declared Human tools in the eval scope, named like the registry.
pub fn tools(request: &Request) -> Vec<Tool> {
    use crate::store::definitions::HumanTool;
    let Some(artifacts) = request
        .human_definition
        .as_ref()
        .and_then(|definition| definition.artifacts.value())
    else {
        return Vec::new();
    };
    let mut tools: Vec<_> = artifacts
        .iter()
        .flat_map(|(id, artifact)| {
            artifact
                .views
                .value()
                .and_then(|views| views.human_tools.value())
                .into_iter()
                .flatten()
                // A saved name that is not a valid tool name has no tool to run; the
                // registry, built from the same declarations, refuses it as well.
                .filter_map(move |(operation, tool)| {
                    let (kind, description) = match tool {
                        HumanTool::Command(tool) => (tool.kind, &tool.description),
                        HumanTool::Builtin(tool) => (tool.kind, &tool.description),
                    };
                    Some(Tool {
                        name: format!("{operation}_{id}").parse().ok()?,
                        artifact: id.clone(),
                        kind,
                        description: description.replace("{artifactName}", id.as_str()),
                        declared: Runs::parse(artifacts, id, tool),
                    })
                })
        })
        .collect();
    tools.sort_by(|a, b| a.name.cmp(&b.name));
    tools
}

/// The owner schema of `verdict` recorded with the request: `None` when it declares none.
/// Without a recorded Human definition the schemas are unknown, not empty: an empty form
/// would submit a verdict the schema then rejects.
pub fn schema(
    request: &Request,
    verdict: crate::runtime::Verdict,
) -> Result<Option<Value>, String> {
    let definition = request.human_definition.as_ref().ok_or(
        "This request records no Human definition; open the original request its actions go to.",
    )?;
    let declaration = definition.eval.declaration.value();
    let schema = declaration.and_then(|declaration| match verdict {
        crate::runtime::Verdict::Green => declaration.pass_schema.value(),
        crate::runtime::Verdict::Red => declaration.fail_schema.value(),
    });
    Ok(schema.map(|schema| Value::Object(schema.clone())))
}

/// Words joined for an on-screen preview, double-quoted when a word has whitespace or a
/// quote of its own. This is a reading aid, not a shell command: it claims no single
/// shell's escaping rules, which differ between Unix shells and Windows's.
pub fn shell<'a>(words: impl IntoIterator<Item = &'a str>) -> String {
    let quote = |word: &str| {
        let plain = !word.is_empty()
            && word
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || "-_./=:,+@%{}".contains(c));
        if plain {
            word.to_owned()
        } else {
            format!("\"{}\"", word.replace('"', "\"\""))
        }
    };
    words.into_iter().map(quote).collect::<Vec<_>>().join(" ")
}

#[cfg(test)]
mod submission_tests {
    use super::Submission;
    use crate::runtime::Verdict;
    use serde_json::json;

    #[test]
    fn submission_parses_one_verdict_and_keeps_its_owner_fields() {
        for (value, verdict) in [
            (json!({"verdict":"GREEN","approved":true}), Verdict::Green),
            (json!({"verdict":"RED","reason":"needs work"}), Verdict::Red),
        ] {
            let submitted = Submission::try_from(value.clone()).unwrap();
            assert_eq!(submitted.verdict(), verdict);
            assert_eq!(submitted.to_json(), value);
        }
        assert!(Submission::try_from(json!({"approved":true})).is_err());
        assert!(Submission::try_from(json!({"verdict":"ERROR"})).is_err());
    }
}