ironflow-store 2.27.0

Storage abstraction and implementations for ironflow run tracking
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
//! [`ArtifactStore`] implementation for [`InMemoryStore`].

use chrono::Utc;
use uuid::Uuid;

use crate::artifact_store::ArtifactStore;
use crate::entities::{Artifact, ArtifactLookup, NewArtifact};
use crate::error::StoreError;
use crate::store::StoreFuture;

use super::InMemoryStore;

impl ArtifactStore for InMemoryStore {
    fn create_artifact(&self, artifact: NewArtifact) -> StoreFuture<'_, Artifact> {
        Box::pin(async move {
            let mut state = self.state.write().await;

            if !state.steps.contains_key(&artifact.step_id) {
                return Err(StoreError::StepNotFound(artifact.step_id));
            }
            if state
                .artifacts
                .values()
                .any(|a| a.step_id == artifact.step_id && a.name == artifact.name)
            {
                return Err(StoreError::DuplicateArtifact {
                    step_id: artifact.step_id,
                    name: artifact.name,
                });
            }

            let now = Utc::now();
            let stored = Artifact {
                id: artifact.id,
                run_id: artifact.run_id,
                step_id: artifact.step_id,
                name: artifact.name,
                storage_key: artifact.storage_key,
                content_type: artifact.content_type,
                size_bytes: artifact.size_bytes,
                sha256: artifact.sha256,
                created_at: now,
                updated_at: now,
            };

            state.artifacts.insert(stored.id, stored.clone());
            Ok(stored)
        })
    }

    fn get_artifact(&self, step_id: Uuid, name: &str) -> StoreFuture<'_, Option<Artifact>> {
        let name = name.to_string();
        Box::pin(async move {
            let state = self.state.read().await;
            Ok(state
                .artifacts
                .values()
                .find(|a| a.step_id == step_id && a.name == name)
                .cloned())
        })
    }

    fn list_artifacts_for_run(&self, run_id: Uuid) -> StoreFuture<'_, Vec<Artifact>> {
        Box::pin(async move {
            let state = self.state.read().await;

            let mut artifacts: Vec<Artifact> = state
                .artifacts
                .values()
                .filter(|a| a.run_id == run_id)
                .cloned()
                .collect();

            artifacts.sort_by(|a, b| {
                let position = |artifact: &Artifact| {
                    state
                        .steps
                        .get(&artifact.step_id)
                        .map(|s| (s.attempt, s.position))
                        .unwrap_or((0, 0))
                };
                position(a)
                    .cmp(&position(b))
                    .then_with(|| a.name.cmp(&b.name))
            });

            Ok(artifacts)
        })
    }

    fn find_artifact_for_input(&self, lookup: ArtifactLookup) -> StoreFuture<'_, Option<Artifact>> {
        Box::pin(async move {
            let state = self.state.read().await;

            // Candidate steps: same run and attempt, matching name, positioned
            // strictly before the consumer. The closest one wins.
            let producer = state
                .steps
                .values()
                .filter(|s| {
                    s.run_id == lookup.run_id
                        && s.attempt == lookup.attempt
                        && s.name == lookup.step_name
                        && s.position < lookup.before_position
                })
                .max_by_key(|s| s.position);

            let Some(producer) = producer else {
                return Ok(None);
            };

            Ok(state
                .artifacts
                .values()
                .find(|a| a.step_id == producer.id && a.name == lookup.name)
                .cloned())
        })
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::entities::{NewStep, StepKind, step_trace_id};
    use crate::store::RunStore;

    use super::super::tests::new_run_req;
    use super::*;

    /// Create a run with one step at `position`, returning both ids.
    async fn run_with_step(store: &InMemoryStore, step_name: &str, position: u32) -> (Uuid, Uuid) {
        let run = store
            .create_run(new_run_req("artifacts"))
            .await
            .expect("create run")
            .into_run();
        let step = store
            .create_step(NewStep {
                run_id: run.id,
                trace_id: step_trace_id(run.id, step_name, position),
                name: step_name.to_string(),
                kind: StepKind::Shell,
                position,
                input: Some(json!({})),
                is_error_handler: false,
            })
            .await
            .expect("create step");

        (run.id, step.id)
    }

    fn new_artifact(run_id: Uuid, step_id: Uuid, name: &str) -> NewArtifact {
        let id = Uuid::now_v7();
        NewArtifact {
            id,
            run_id,
            step_id,
            name: name.to_string(),
            storage_key: format!("artifacts/{run_id}/{step_id}/{id}"),
            content_type: "text/plain".to_string(),
            size_bytes: 3,
            sha256: "0".repeat(64),
        }
    }

    #[tokio::test]
    async fn create_then_get_roundtrips() {
        let store = InMemoryStore::new();
        let (run_id, step_id) = run_with_step(&store, "build", 0).await;

        let created = store
            .create_artifact(new_artifact(run_id, step_id, "report.html"))
            .await
            .expect("create");

        let fetched = store
            .get_artifact(step_id, "report.html")
            .await
            .expect("get")
            .expect("present");

        assert_eq!(fetched.id, created.id);
        assert_eq!(fetched.name, "report.html");
        assert_eq!(fetched.size_bytes, 3);
    }

    #[tokio::test]
    async fn get_on_an_unknown_name_returns_none() {
        let store = InMemoryStore::new();
        let (_run_id, step_id) = run_with_step(&store, "build", 0).await;

        assert!(
            store
                .get_artifact(step_id, "nope")
                .await
                .expect("get")
                .is_none()
        );
    }

    #[tokio::test]
    async fn create_on_an_unknown_step_is_rejected() {
        let store = InMemoryStore::new();

        let err = store
            .create_artifact(new_artifact(Uuid::now_v7(), Uuid::now_v7(), "a.txt"))
            .await
            .expect_err("step does not exist");

        assert!(matches!(err, StoreError::StepNotFound(_)));
    }

    #[tokio::test]
    async fn the_same_name_twice_on_a_step_is_rejected() {
        let store = InMemoryStore::new();
        let (run_id, step_id) = run_with_step(&store, "build", 0).await;

        store
            .create_artifact(new_artifact(run_id, step_id, "a.txt"))
            .await
            .expect("first");

        let err = store
            .create_artifact(new_artifact(run_id, step_id, "a.txt"))
            .await
            .expect_err("duplicate");

        assert!(matches!(err, StoreError::DuplicateArtifact { .. }));
    }

    #[tokio::test]
    async fn the_same_name_on_two_steps_is_allowed() {
        let store = InMemoryStore::new();
        let (run_id, first) = run_with_step(&store, "build", 0).await;
        let second = store
            .create_step(NewStep {
                run_id,
                trace_id: step_trace_id(run_id, "test", 1),
                name: "test".to_string(),
                kind: StepKind::Shell,
                position: 1,
                input: None,
                is_error_handler: false,
            })
            .await
            .expect("create step")
            .id;

        store
            .create_artifact(new_artifact(run_id, first, "a.txt"))
            .await
            .expect("first");
        store
            .create_artifact(new_artifact(run_id, second, "a.txt"))
            .await
            .expect("second");

        assert_eq!(
            store
                .list_artifacts_for_run(run_id)
                .await
                .expect("list")
                .len(),
            2
        );
    }

    #[tokio::test]
    async fn list_is_scoped_to_the_run() {
        let store = InMemoryStore::new();
        let (run_a, step_a) = run_with_step(&store, "build", 0).await;
        let (run_b, step_b) = run_with_step(&store, "build", 0).await;

        store
            .create_artifact(new_artifact(run_a, step_a, "a.txt"))
            .await
            .expect("a");
        store
            .create_artifact(new_artifact(run_b, step_b, "b.txt"))
            .await
            .expect("b");

        let listed = store.list_artifacts_for_run(run_a).await.expect("list");
        assert_eq!(listed.len(), 1);
        assert_eq!(listed[0].name, "a.txt");
    }

    #[tokio::test]
    async fn list_on_a_run_without_artifacts_is_empty() {
        let store = InMemoryStore::new();
        let (run_id, _) = run_with_step(&store, "build", 0).await;

        assert!(
            store
                .list_artifacts_for_run(run_id)
                .await
                .expect("list")
                .is_empty()
        );
    }

    #[tokio::test]
    async fn input_resolves_from_an_earlier_step() {
        let store = InMemoryStore::new();
        let (run_id, step_id) = run_with_step(&store, "build", 0).await;
        store
            .create_artifact(new_artifact(run_id, step_id, "report.html"))
            .await
            .expect("create");

        let found = store
            .find_artifact_for_input(ArtifactLookup {
                run_id,
                attempt: 1,
                before_position: 1,
                step_name: "build".to_string(),
                name: "report.html".to_string(),
            })
            .await
            .expect("lookup");

        assert_eq!(found.expect("present").step_id, step_id);
    }

    #[tokio::test]
    async fn input_ignores_a_step_at_or_after_the_consumer() {
        let store = InMemoryStore::new();
        let (run_id, step_id) = run_with_step(&store, "build", 2).await;
        store
            .create_artifact(new_artifact(run_id, step_id, "report.html"))
            .await
            .expect("create");

        let found = store
            .find_artifact_for_input(ArtifactLookup {
                run_id,
                attempt: 1,
                before_position: 2,
                step_name: "build".to_string(),
                name: "report.html".to_string(),
            })
            .await
            .expect("lookup");

        assert!(found.is_none());
    }

    #[tokio::test]
    async fn input_picks_the_closest_producer_when_names_repeat() {
        let store = InMemoryStore::new();
        let (run_id, first) = run_with_step(&store, "build", 0).await;
        let second = store
            .create_step(NewStep {
                run_id,
                trace_id: step_trace_id(run_id, "build", 1),
                name: "build".to_string(),
                kind: StepKind::Shell,
                position: 1,
                input: None,
                is_error_handler: false,
            })
            .await
            .expect("create step")
            .id;

        store
            .create_artifact(new_artifact(run_id, first, "report.html"))
            .await
            .expect("first");
        store
            .create_artifact(new_artifact(run_id, second, "report.html"))
            .await
            .expect("second");

        let found = store
            .find_artifact_for_input(ArtifactLookup {
                run_id,
                attempt: 1,
                before_position: 2,
                step_name: "build".to_string(),
                name: "report.html".to_string(),
            })
            .await
            .expect("lookup")
            .expect("present");

        assert_eq!(found.step_id, second);
    }

    #[tokio::test]
    async fn input_does_not_cross_attempts() {
        let store = InMemoryStore::new();
        let (run_id, step_id) = run_with_step(&store, "build", 0).await;
        store
            .create_artifact(new_artifact(run_id, step_id, "report.html"))
            .await
            .expect("create");

        let found = store
            .find_artifact_for_input(ArtifactLookup {
                run_id,
                attempt: 2,
                before_position: 1,
                step_name: "build".to_string(),
                name: "report.html".to_string(),
            })
            .await
            .expect("lookup");

        assert!(found.is_none());
    }

    #[tokio::test]
    async fn input_does_not_cross_runs() {
        let store = InMemoryStore::new();
        let (run_a, step_a) = run_with_step(&store, "build", 0).await;
        let (run_b, _) = run_with_step(&store, "build", 0).await;
        store
            .create_artifact(new_artifact(run_a, step_a, "report.html"))
            .await
            .expect("create");

        let found = store
            .find_artifact_for_input(ArtifactLookup {
                run_id: run_b,
                attempt: 1,
                before_position: 1,
                step_name: "build".to_string(),
                name: "report.html".to_string(),
            })
            .await
            .expect("lookup");

        assert!(found.is_none());
    }
}