Skip to main content

vlo_kernel/
ids.rs

1//! Deterministic identifier derivation. Every id a run produces is a pure function of
2//! `(run_id, step_id, attempt)`, which is what makes task dispatch idempotent across
3//! crashes and re-pushes.
4
5use vlo_types::{DeriveIdsInput, DeriveIdsOutput};
6
7use crate::hash::sha256_hex;
8
9/// Child run ids are opaque and fixed-width so they fit the same column as host-minted ids.
10const CHILD_RUN_ID_HEX_LEN: usize = 26;
11
12/// `<run_id>:<step_id>`; stable across attempts.
13pub fn task_id(run_id: &str, step_id: &str) -> String {
14    format!("{run_id}:{step_id}")
15}
16
17/// `<run_id>:<step_id>:<attempt>`; unique per dispatch attempt.
18pub fn idempotency_key(run_id: &str, step_id: &str, attempt: u32) -> String {
19    format!("{run_id}:{step_id}:{attempt}")
20}
21
22/// `run_` followed by the first 26 hex chars of `sha256(task_id)`.
23pub fn child_run_id(run_id: &str, step_id: &str) -> String {
24    let digest = sha256_hex(task_id(run_id, step_id).as_bytes());
25    format!("run_{}", &digest[..CHILD_RUN_ID_HEX_LEN])
26}
27
28/// All three ids for one `(run_id, step_id, attempt)`.
29pub fn derive_ids(input: &DeriveIdsInput) -> DeriveIdsOutput {
30    DeriveIdsOutput {
31        task_id: task_id(&input.run_id, &input.step_id),
32        idempotency_key: idempotency_key(&input.run_id, &input.step_id, input.attempt),
33        child_run_id: child_run_id(&input.run_id, &input.step_id),
34    }
35}