artifactize-core 0.27.2

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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Saved Run and request representations and their wire conversions.
use crate::store::{self as store, ExecutionOptions, Origin, Producer, Provenance};
use crate::{
    config::ProfileKind,
    types::{
        Fingerprint, RequestId, RequestStatus, ReuseKey, RunId, RunStatus, SessionId, Timestamp,
    },
};
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
    time::Duration,
};

mod stored_request_key {
    use crate::types::RequestKey;
    use serde::{Deserialize, Deserializer, Serializer};

    pub(super) fn serialize<S: Serializer>(
        value: &Option<RequestKey>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match value {
            Some(key) => serializer.serialize_some(key.as_str()),
            None => serializer.serialize_none(),
        }
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<RequestKey>, D::Error> {
        Ok(Option::<String>::deserialize(deserializer)?.and_then(|raw| raw.parse().ok()))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "RunWire", into = "RunWire")]
pub struct Run {
    pub id: RunId,
    pub repo_path: PathBuf,
    /// Optional display metadata; schema 5 Runs written before this remain readable.
    pub repository: crate::repository::Identity,
    pub state_dir: PathBuf,
    pub state: store::RunState,
    pub modes: crate::runs::RunModes,
    pub control: Option<crate::runs::Control>,
    pub request_key: Option<crate::types::RequestKey>,
    pub request_args_hash: Option<String>,
    pub origin: Option<crate::runs::Origin>,
    /// What the requester said it is working on; for people only.
    pub comment: Option<crate::types::RunComment>,
    pub created_at: Timestamp,
    pub selection: crate::project::selection::Selection,
    pub definitions: store::definitions::Definitions,
    pub recursive: bool,
    pub force: bool,
    pub jobs: Option<usize>,
    /// Fingerprints computed at once; absent in Runs saved before 0.5.
    pub fingerprint_jobs: Option<usize>,
    pub max_executions: Option<u64>,
    pub executions_started: u64,
    pub wait_timeout_ms: Option<Duration>,
    pub wait_timed_out: bool,
    /// Kinds whose evals only reuse a result; one with nothing to reuse is not executed.
    pub reuse_only: BTreeSet<ProfileKind>,
    pub validation: store::Validation,
    /// Agent backends this Run stopped admitting reviews on, in the order they stopped.
    pub stopped_backends: Vec<StoppedBackend>,
    /// Verdicts the Run took from saved results for evals it has no request for, such as the
    /// dependencies of a partial Run, so readers judge gates on the Run's own evidence.
    pub evidence: BTreeMap<crate::types::EvalId, crate::types::RequestStatus>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RunWire {
    pub id: RunId,
    #[serde(with = "crate::platform::path_serde")]
    pub repo_path: PathBuf,
    /// Optional display metadata; schema 5 Runs written before this remain readable.
    #[serde(flatten)]
    pub repository: crate::repository::Identity,
    #[serde(with = "crate::platform::path_serde")]
    pub state_dir: PathBuf,
    #[serde(default, skip_serializing_if = "crate::runs::RunModes::is_default")]
    pub modes: crate::runs::RunModes,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub control: Option<crate::runs::Control>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "stored_request_key"
    )]
    pub request_key: Option<crate::types::RequestKey>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request_args_hash: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<crate::runs::Origin>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub comment: Option<crate::types::RunComment>,
    pub status: RunStatus,
    pub created_at: Timestamp,
    pub completed_at: Option<Timestamp>,
    pub selection: crate::project::selection::Selection,
    #[serde(default)]
    pub definitions: store::definitions::Definitions,
    #[serde(default)]
    pub recursive: bool,
    #[serde(default)]
    pub force: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jobs: Option<usize>,
    /// Fingerprints computed at once; absent in Runs saved before 0.5.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fingerprint_jobs: Option<usize>,
    #[serde(default)]
    pub max_executions: Option<u64>,
    #[serde(default)]
    pub executions_started: u64,
    #[serde(default, with = "store::wait_timeout")]
    pub wait_timeout_ms: Option<Duration>,
    #[serde(default)]
    pub wait_timed_out: bool,
    /// Kinds whose evals only reuse a result; one with nothing to reuse is not executed.
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub reuse_only: BTreeSet<ProfileKind>,
    pub validation: store::Validation,
    pub error: Option<String>,
    /// Agent backends this Run stopped admitting reviews on, in the order they stopped.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stopped_backends: Vec<StoppedBackend>,
    /// Verdicts the Run took from saved results for evals it has no request for, such as the
    /// dependencies of a partial Run, so readers judge gates on the Run's own evidence.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub evidence: BTreeMap<crate::types::EvalId, crate::types::RequestStatus>,
}

impl TryFrom<RunWire> for Run {
    type Error = String;
    fn try_from(wire: RunWire) -> Result<Self, Self::Error> {
        let state = store::RunState::from_parts(wire.status, wire.completed_at, wire.error)?;
        Ok(Self {
            state,
            modes: wire.modes,
            control: wire.control,
            request_key: wire.request_key,
            request_args_hash: wire.request_args_hash,
            origin: wire.origin,
            comment: wire.comment,
            id: wire.id,
            repo_path: wire.repo_path,
            repository: wire.repository,
            state_dir: wire.state_dir,
            created_at: wire.created_at,
            selection: wire.selection,
            definitions: wire.definitions,
            recursive: wire.recursive,
            force: wire.force,
            jobs: wire.jobs,
            fingerprint_jobs: wire.fingerprint_jobs,
            max_executions: wire.max_executions,
            executions_started: wire.executions_started,
            wait_timeout_ms: wire.wait_timeout_ms,
            wait_timed_out: wire.wait_timed_out,
            reuse_only: wire.reuse_only,
            validation: wire.validation,
            stopped_backends: wire.stopped_backends,
            evidence: wire.evidence,
        })
    }
}
impl From<Run> for RunWire {
    fn from(record: Run) -> Self {
        Self {
            modes: record.modes.clone(),
            control: record.control.clone(),
            request_key: record.request_key.clone(),
            request_args_hash: record.request_args_hash.clone(),
            origin: record.origin.clone(),
            comment: record.comment.clone(),
            status: record.status(),
            completed_at: record.completed_at(),
            error: record.error().map(str::to_owned),
            id: record.id,
            repo_path: record.repo_path,
            repository: record.repository,
            state_dir: record.state_dir,
            created_at: record.created_at,
            selection: record.selection,
            definitions: record.definitions,
            recursive: record.recursive,
            force: record.force,
            jobs: record.jobs,
            fingerprint_jobs: record.fingerprint_jobs,
            max_executions: record.max_executions,
            executions_started: record.executions_started,
            wait_timeout_ms: record.wait_timeout_ms,
            wait_timed_out: record.wait_timed_out,
            reuse_only: record.reuse_only,
            validation: record.validation,
            stopped_backends: record.stopped_backends,
            evidence: record.evidence,
        }
    }
}
impl Run {
    pub fn status(&self) -> RunStatus {
        self.state.status()
    }
    /// Show the Run WAITING_HUMAN while one of its requests, saved with `statuses`, is an
    /// unsubmitted sign-off, and RUNNING otherwise while it is open.
    pub(in crate::store) fn show_signoffs(
        &mut self,
        mut statuses: impl Iterator<Item = crate::types::RequestStatus>,
    ) {
        let open = statuses.any(crate::types::RequestStatus::is_human_wait);
        self.state = self.state.clone().with_signoffs(open);
    }
    pub fn completed_at(&self) -> Option<Timestamp> {
        self.state.completed_at()
    }
    pub fn error(&self) -> Option<&str> {
        self.state.error()
    }
}

/// An Agent backend a Run stopped after an AUTHENTICATION or QUOTA failure, which every
/// later review on it would repeat.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StoppedBackend {
    pub backend: crate::config::Backend,
    pub error_code: crate::types::BackendStopCode,
    pub eval_id: crate::types::EvalId,
    pub request_id: RequestId,
    pub error: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "RequestWire", into = "RequestWire")]
pub struct Request {
    pub state: store::RequestState,
    pub id: RequestId,
    pub run_id: RunId,
    pub eval_id: crate::types::EvalId,
    pub target: crate::types::ArtifactName,
    pub title: String,
    pub profile: crate::config::StoredProfile,
    pub requested_profile: crate::config::StoredProfile,
    pub eval_def_hash: crate::types::DefinitionHash,
    /// The requested execution options, or a reused result's.
    pub options: ExecutionOptions,
    pub execution_id: Option<crate::types::ExecutionId>,
    pub provenance: Option<Provenance>,
    /// Usage spent by this request; a reused request spent none.
    pub usage: Option<Vec<crate::llm::Attempt>>,
    /// The reused execution's original usage, never counted as spent.
    pub reused_usage: Option<Vec<crate::llm::Attempt>>,
    /// The reused result came from the live execution this request waited for, not a
    /// completed record. Saved only; output reports it as `source.kind` ([`crate::query::source`]).
    pub joined: bool,
    /// Who produced a reused result.
    pub producer: Option<Producer>,
    /// The Human claimant who submitted a reused result.
    pub reviewer: Option<crate::types::ReviewerId>,
    /// The remote store, publisher and publication time of a result reused from a mirror.
    pub origin: Option<Origin>,
    /// The Agent review's session id ([`crate::agent::session_id`]), which every request of
    /// the review sends as its prompt-cache identity. Absent when no Agent review ran, as on
    /// reuse, and on requests saved before it existed.
    pub session_id: Option<SessionId>,
    /// Where the saved conversation behind this request's result lives: its own review's,
    /// or for a reused result the producing review's. Absent when none was saved.
    pub session: Option<crate::agent::session::SessionRef>,
    /// Free-form Human response accompanying the schema-owned answer on this receipt.
    pub review_notes: Option<String>,
    pub human_definition: Option<store::HumanDefinition>,
    pub payload: crate::config::StoredPayload,
    pub references: BTreeMap<crate::config::ReferenceName, crate::types::ArtifactName>,
    pub deps: Vec<crate::types::ArtifactName>,
    pub force: bool,
    /// The target Artifact's fingerprint.
    pub fingerprint: Option<Fingerprint>,
    /// The reuse key; absent without a fingerprint on every Artifact the eval depends on.
    pub key: Option<ReuseKey>,
    /// Each Artifact the key covers, with its fingerprint.
    pub fingerprints: std::collections::BTreeMap<crate::types::ArtifactName, Fingerprint>,
    pub created_at: Timestamp,
    pub started_at: Option<Timestamp>,
    pub cwd: PathBuf,
    pub run_dir: Option<PathBuf>,
    pub argv: Option<Vec<String>>,
    pub child: Option<store::ChildIdentity>,
    pub blocked_reason: Option<String>,
    /// Unfulfilled Evals behind this request’s dependency gates.
    pub blocked_by: Vec<store::Blocker>,
}

/// What a queued request waits for besides a job slot of its Run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum QueueReason {
    /// A free machine-wide slot of this backend (`limits.json`).
    Slot { backend: crate::config::Backend },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RequestWire {
    pub id: RequestId,
    pub run_id: RunId,
    pub eval_id: crate::types::EvalId,
    pub target: crate::types::ArtifactName,
    pub title: String,
    pub profile: crate::config::StoredProfile,
    pub requested_profile: crate::config::StoredProfile,
    pub eval_def_hash: crate::types::DefinitionHash,
    /// The requested execution options, or a reused result's.
    #[serde(default)]
    pub options: ExecutionOptions,
    pub execution_id: Option<crate::types::ExecutionId>,
    pub provenance: Option<Provenance>,
    /// Usage spent by this request; a reused request spent none.
    pub usage: Option<Vec<crate::llm::Attempt>>,
    /// The reused execution's original usage, never counted as spent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reused_usage: Option<Vec<crate::llm::Attempt>>,
    /// The reused result came from the live execution this request waited for, not a
    /// completed record. Saved only; output reports it as `source.kind` ([`crate::query::source`]).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub joined: bool,
    /// Who produced a reused result.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub producer: Option<Producer>,
    /// The Human claimant who submitted a reused result.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reviewer: Option<crate::types::ReviewerId>,
    /// The remote store, publisher and publication time of a result reused from a mirror.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<Origin>,
    /// The Agent review's session id ([`crate::agent::session_id`]), which every request of
    /// the review sends as its prompt-cache identity. Absent when no Agent review ran, as on
    /// reuse, and on requests saved before it existed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<SessionId>,
    /// Where the saved conversation behind this request's result lives: its own review's,
    /// or for a reused result the producing review's. Absent when none was saved.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session: Option<crate::agent::session::SessionRef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub review_notes: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub human_definition: Option<store::HumanDefinition>,
    pub payload: crate::config::StoredPayload,
    pub references: BTreeMap<crate::config::ReferenceName, crate::types::ArtifactName>,
    pub deps: Vec<crate::types::ArtifactName>,
    #[serde(default)]
    pub force: bool,
    /// The target Artifact's fingerprint.
    #[serde(default)]
    pub fingerprint: Option<Fingerprint>,
    /// The reuse key; absent without a fingerprint on every Artifact the eval depends on.
    #[serde(default)]
    pub key: Option<ReuseKey>,
    /// Each Artifact the key covers, with its fingerprint.
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
    pub fingerprints: std::collections::BTreeMap<crate::types::ArtifactName, Fingerprint>,
    pub status: RequestStatus,
    pub created_at: Timestamp,
    pub started_at: Option<Timestamp>,
    pub completed_at: Option<Timestamp>,
    #[serde(with = "crate::platform::path_serde")]
    pub cwd: PathBuf,
    #[serde(default, with = "crate::platform::path_serde::option")]
    pub run_dir: Option<PathBuf>,
    pub argv: Option<Vec<String>>,
    pub child: Option<store::ChildIdentity>,
    pub result: Option<store::ExecutionResult>,
    pub error: Option<String>,
    pub error_code: Option<crate::types::FailureCode>,
    pub blocked_reason: Option<String>,
    /// Unfulfilled Evals behind this request’s dependency gates.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub blocked_by: Vec<store::Blocker>,
    /// Absent from requests saved before it existed, which read as waiting for a job.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub queue: Option<QueueReason>,
}

impl TryFrom<RequestWire> for Request {
    type Error = String;
    fn try_from(wire: RequestWire) -> Result<Self, Self::Error> {
        let mut state = store::RequestState::from_parts(
            wire.status,
            wire.result,
            wire.error,
            wire.error_code,
            wire.completed_at,
        )?;
        // Old receipts could carry a queue reason on a different state; only queued ones use it.
        if let store::RequestState::Queued { reason, .. } = &mut state {
            *reason = wire.queue;
        }
        Ok(Self {
            state,
            id: wire.id,
            run_id: wire.run_id,
            eval_id: wire.eval_id,
            target: wire.target,
            title: wire.title,
            profile: wire.profile,
            requested_profile: wire.requested_profile,
            eval_def_hash: wire.eval_def_hash,
            options: wire.options,
            execution_id: wire.execution_id,
            provenance: wire.provenance,
            usage: wire.usage,
            reused_usage: wire.reused_usage,
            joined: wire.joined,
            producer: wire.producer,
            reviewer: wire.reviewer,
            origin: wire.origin,
            session_id: wire.session_id,
            session: wire.session,
            review_notes: wire.review_notes,
            human_definition: wire.human_definition,
            payload: wire.payload,
            references: wire.references,
            deps: wire.deps,
            force: wire.force,
            fingerprint: wire.fingerprint,
            key: wire.key,
            fingerprints: wire.fingerprints,
            created_at: wire.created_at,
            started_at: wire.started_at,
            cwd: wire.cwd,
            run_dir: wire.run_dir,
            argv: wire.argv,
            child: wire.child,
            blocked_reason: wire.blocked_reason,
            blocked_by: wire.blocked_by,
        })
    }
}
impl From<Request> for RequestWire {
    fn from(record: Request) -> Self {
        Self {
            queue: record.state.queue_reason().cloned(),
            status: record.status(),
            result: record.result().cloned(),
            error: record.error().map(str::to_owned),
            error_code: record.error_code(),
            completed_at: record.completed_at(),
            id: record.id,
            run_id: record.run_id,
            eval_id: record.eval_id,
            target: record.target,
            title: record.title,
            profile: record.profile,
            requested_profile: record.requested_profile,
            eval_def_hash: record.eval_def_hash,
            options: record.options,
            execution_id: record.execution_id,
            provenance: record.provenance,
            usage: record.usage,
            reused_usage: record.reused_usage,
            joined: record.joined,
            producer: record.producer,
            reviewer: record.reviewer,
            origin: record.origin,
            session_id: record.session_id,
            session: record.session,
            review_notes: record.review_notes,
            human_definition: record.human_definition,
            payload: record.payload,
            references: record.references,
            deps: record.deps,
            force: record.force,
            fingerprint: record.fingerprint,
            key: record.key,
            fingerprints: record.fingerprints,
            created_at: record.created_at,
            started_at: record.started_at,
            cwd: record.cwd,
            run_dir: record.run_dir,
            argv: record.argv,
            child: record.child,
            blocked_reason: record.blocked_reason,
            blocked_by: record.blocked_by,
        }
    }
}
impl Request {
    pub fn status(&self) -> crate::types::RequestStatus {
        self.state.status()
    }
    pub fn result(&self) -> Option<&store::ExecutionResult> {
        self.state.result()
    }
    pub fn error(&self) -> Option<&str> {
        self.state.error()
    }
    pub fn error_code(&self) -> Option<crate::types::FailureCode> {
        self.state.error_code()
    }
    pub fn completed_at(&self) -> Option<crate::types::Timestamp> {
        self.state.completed_at()
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RunView {
    #[serde(flatten)]
    pub run: Run,
    pub requests: Vec<std::sync::Arc<Request>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unreadable: Vec<store::Unreadable>,
}

#[cfg(test)]
mod tests;