floe-core 0.4.5

Core library for Floe, a YAML-driven technical ingestion tool.
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
use std::collections::BTreeMap;
use std::time::{SystemTime, UNIX_EPOCH};

use sha2::{Digest, Sha256};

use crate::config::{ConfigLocation, RootConfig, SourceOptions, StorageResolver};
use crate::manifest::model::{
    CommonManifest, ManifestArchiveTarget, ManifestColumnDef, ManifestDomain, ManifestEntity,
    ManifestEntitySchema, ManifestExecution, ManifestExecutionDefaults, ManifestResultContract,
    ManifestRunnerAuth, ManifestRunnerDefinition, ManifestRunnerResources, ManifestRunnerSecret,
    ManifestRunners, ManifestSinkTarget, ManifestSinks, ManifestSource,
};
use crate::profile::ProfileConfig;
use crate::FloeResult;

/// Controls how the `path` field is populated in source and sink entries.
#[derive(Debug, Default, PartialEq)]
pub enum PathMode {
    /// Keep the original relative path from the config file (default).
    #[default]
    Default,
    /// When a path has been resolved to a full URI, set `path = uri`.
    /// This makes the manifest self-contained for remote replay without
    /// needing the original config base directory.
    ResolvedUri,
}

/// Options that control manifest generation behaviour.
#[derive(Debug, Default)]
pub struct ManifestOptions {
    /// When true, `generated_at_ts_ms` is set to `0` so that the same inputs
    /// always produce byte-identical output. Use this for committed deployment
    /// artifacts that are diffed in CI.
    pub deterministic: bool,
    /// Optional stable logical name for this manifest (e.g. `"sales.prod"`).
    /// Stored as `manifest_name` in the output JSON.
    pub manifest_name: Option<String>,
    /// Display URI of the profile file (e.g. `local:///path/to/prod.yml`).
    /// Stored as `profile_uri` in the output JSON.
    pub profile_uri: Option<String>,
    /// Local filesystem path to the profile file used during generation.
    /// When supplied, a SHA-256 checksum of the file is computed and stored
    /// as `profile_checksum`.
    pub profile_path: Option<std::path::PathBuf>,
    /// When set, replaces the `{manifest_uri}` placeholder in
    /// `execution.base_args` with this URI. Use this to bake the deployed
    /// manifest location into a self-contained deployment contract.
    pub manifest_uri: Option<String>,
    /// Default domain applied to entities that have no explicit `domain`
    /// field. Drives `domain`, `group_name`, and `asset_key` generation.
    pub default_domain: Option<String>,
    /// Controls how the `path` field is set in source and sink entries.
    pub path_mode: PathMode,
}

#[derive(Debug)]
struct ResolvedOrRaw {
    storage: String,
    uri: String,
    resolved: bool,
}

pub fn build_common_manifest_json(
    config_location: &ConfigLocation,
    config: &RootConfig,
    selected_entities: &[String],
    profile: Option<&ProfileConfig>,
    options: &ManifestOptions,
) -> FloeResult<String> {
    let resolver = StorageResolver::new(config, config_location.base.clone())?;
    let mut manifest = build_common_manifest(
        config_location,
        config,
        selected_entities,
        &resolver,
        profile,
        options,
    );

    // Compute manifest_revision: SHA-256 of the canonical content, which
    // excludes volatile fields (generated_at_ts_ms, manifest_revision itself).
    let revision = compute_manifest_revision(&manifest)?;
    manifest.manifest_revision = Some(revision);

    Ok(serde_json::to_string_pretty(&manifest)?)
}

/// Compute a stable content hash over the manifest, excluding fields that
/// change on every generation (timestamp) or are themselves the hash output.
fn compute_manifest_revision(manifest: &CommonManifest) -> FloeResult<String> {
    let mut value: serde_json::Value = serde_json::to_value(manifest)?;
    if let Some(obj) = value.as_object_mut() {
        obj.remove("generated_at_ts_ms");
        obj.remove("manifest_revision");
    }
    let canonical = serde_json::to_string(&value)?;
    Ok(sha256_hex(canonical.as_bytes()))
}

fn sha256_hex(bytes: &[u8]) -> String {
    format!("sha256:{:x}", Sha256::digest(bytes))
}

fn build_common_manifest(
    config_location: &ConfigLocation,
    config: &RootConfig,
    selected_entities: &[String],
    resolver: &StorageResolver,
    profile: Option<&ProfileConfig>,
    options: &ManifestOptions,
) -> CommonManifest {
    let mut entities: Vec<_> = if selected_entities.is_empty() {
        config.entities.iter().collect()
    } else {
        config
            .entities
            .iter()
            .filter(|entity| selected_entities.iter().any(|name| name == &entity.name))
            .collect()
    };
    entities.sort_by(|left, right| left.name.cmp(&right.name));

    let report_path = config
        .report
        .as_ref()
        .map(|report| report.path.as_str())
        .unwrap_or("report");
    let report_storage = config
        .report
        .as_ref()
        .and_then(|report| report.storage.as_deref());
    let report_base = resolve_or_raw(
        resolver,
        "__manifest__",
        "report.path",
        report_storage,
        report_path,
    );

    let mut manifest_entities = Vec::with_capacity(entities.len());
    for entity in entities {
        let source = resolve_or_raw(
            resolver,
            &entity.name,
            "source.path",
            entity.source.storage.as_deref(),
            &entity.source.path,
        );
        let accepted = resolve_or_raw(
            resolver,
            &entity.name,
            "sink.accepted.path",
            entity.sink.accepted.storage.as_deref(),
            &entity.sink.accepted.path,
        );
        let rejected = entity.sink.rejected.as_ref().map(|target| {
            resolve_or_raw(
                resolver,
                &entity.name,
                "sink.rejected.path",
                target.storage.as_deref(),
                &target.path,
            )
        });
        let archive = entity.sink.archive.as_ref().map(|target| {
            resolve_or_raw(
                resolver,
                &entity.name,
                "sink.archive.path",
                target.storage.as_deref(),
                &target.path,
            )
        });

        let effective_domain = entity.domain.as_ref().or(options.default_domain.as_ref());
        let (asset_key, group_name, entity_domain) = if let Some(domain) = effective_domain {
            (
                vec![domain.clone(), entity.name.clone()],
                domain.clone(),
                Some(domain.clone()),
            )
        } else {
            (
                vec!["default".to_string(), entity.name.clone()],
                "default".to_string(),
                None,
            )
        };

        let mut tags = BTreeMap::new();
        if let Some(metadata) = &entity.metadata {
            if let Some(owner) = &metadata.owner {
                tags.insert("owner".to_string(), owner.clone());
            }
            if let Some(product) = &metadata.data_product {
                tags.insert("data_product".to_string(), product.clone());
            }
            if let Some(domain_tag) = &metadata.domain {
                tags.insert("domain".to_string(), domain_tag.clone());
            }
        }
        let tags = if tags.is_empty() { None } else { Some(tags) };

        let schema = ManifestEntitySchema {
            columns: entity
                .schema
                .columns
                .iter()
                .map(|c| ManifestColumnDef {
                    name: c.name.clone(),
                    column_type: c.column_type.clone(),
                    source: c.source.clone(),
                    nullable: c.nullable,
                    unique: c.unique,
                    width: c.width,
                    trim: c.trim,
                })
                .collect(),
            primary_key: entity.schema.primary_key.clone().unwrap_or_default(),
            unique_keys: entity.schema.unique_keys.clone().unwrap_or_default(),
            normalize_columns: entity
                .schema
                .normalize_columns
                .as_ref()
                .and_then(|v| serde_json::to_value(v).ok()),
            mismatch: entity
                .schema
                .mismatch
                .as_ref()
                .and_then(|v| serde_json::to_value(v).ok()),
            schema_evolution: entity
                .schema
                .schema_evolution
                .as_ref()
                .and_then(|v| serde_json::to_value(v).ok()),
        };

        let pii = entity
            .pii
            .as_ref()
            .and_then(|v| serde_json::to_value(v).ok());

        let source_path = if options.path_mode == PathMode::ResolvedUri && source.resolved {
            resolved_uri_to_path(&source.uri)
        } else {
            entity.source.path.clone()
        };
        let accepted_path = if options.path_mode == PathMode::ResolvedUri && accepted.resolved {
            resolved_uri_to_path(&accepted.uri)
        } else {
            entity.sink.accepted.path.clone()
        };

        manifest_entities.push(ManifestEntity {
            name: entity.name.clone(),
            domain: entity_domain,
            group_name,
            asset_key,
            source_format: entity.source.format.clone(),
            accepted_sink_uri: accepted.uri.clone(),
            rejected_sink_uri: rejected.as_ref().map(|value| value.uri.clone()),
            tags,
            source: ManifestSource {
                format: entity.source.format.clone(),
                storage: source.storage,
                uri: source.uri,
                path: source_path,
                resolved: source.resolved,
                cast_mode: entity.source.cast_mode.clone(),
                options: map_source_options(entity.source.options.as_ref()),
            },
            sinks: ManifestSinks {
                accepted: ManifestSinkTarget {
                    format: entity.sink.accepted.format.clone(),
                    storage: accepted.storage,
                    uri: accepted.uri,
                    path: accepted_path,
                    resolved: accepted.resolved,
                    options: entity
                        .sink
                        .accepted
                        .options
                        .as_ref()
                        .and_then(|v| serde_json::to_value(v).ok()),
                    partition_by: entity.sink.accepted.partition_by.clone(),
                    merge: entity
                        .sink
                        .accepted
                        .merge
                        .as_ref()
                        .and_then(|v| serde_json::to_value(v).ok()),
                    iceberg: entity
                        .sink
                        .accepted
                        .iceberg
                        .as_ref()
                        .and_then(|v| serde_json::to_value(v).ok()),
                    delta: entity
                        .sink
                        .accepted
                        .delta
                        .as_ref()
                        .and_then(|v| serde_json::to_value(v).ok()),
                },
                rejected: rejected.map(|value| {
                    let rej = entity.sink.rejected.as_ref();
                    let rej_raw_path = rej.map(|t| t.path.clone()).unwrap_or_default();
                    let rej_path = if options.path_mode == PathMode::ResolvedUri && value.resolved {
                        resolved_uri_to_path(&value.uri)
                    } else {
                        rej_raw_path
                    };
                    ManifestSinkTarget {
                        format: rej
                            .map(|t| t.format.clone())
                            .unwrap_or_else(|| "csv".to_string()),
                        storage: value.storage,
                        uri: value.uri,
                        path: rej_path,
                        resolved: value.resolved,
                        options: rej
                            .and_then(|t| t.options.as_ref())
                            .and_then(|v| serde_json::to_value(v).ok()),
                        partition_by: rej.and_then(|t| t.partition_by.clone()),
                        merge: rej
                            .and_then(|t| t.merge.as_ref())
                            .and_then(|v| serde_json::to_value(v).ok()),
                        iceberg: rej
                            .and_then(|t| t.iceberg.as_ref())
                            .and_then(|v| serde_json::to_value(v).ok()),
                        delta: rej
                            .and_then(|t| t.delta.as_ref())
                            .and_then(|v| serde_json::to_value(v).ok()),
                    }
                }),
                archive: archive.map(|value| {
                    let arc_raw_path = entity
                        .sink
                        .archive
                        .as_ref()
                        .map(|target| target.path.clone())
                        .unwrap_or_default();
                    let arc_path = if options.path_mode == PathMode::ResolvedUri && value.resolved {
                        resolved_uri_to_path(&value.uri)
                    } else {
                        arc_raw_path
                    };
                    ManifestArchiveTarget {
                        storage: value.storage,
                        uri: value.uri,
                        path: arc_path,
                        resolved: value.resolved,
                    }
                }),
            },
            runner: None,
            policy_severity: entity.policy.severity.as_str().to_string(),
            write_mode: entity.sink.write_mode.as_str().to_string(),
            incremental_mode: entity.incremental_mode.as_str().to_string(),
            schema,
            pii,
            state_path: entity.state.as_ref().and_then(|s| s.path.clone()),
        });
    }

    let config_uri = canonical_config_uri(&config_location.display);
    let config_checksum = std::fs::read(&config_location.path)
        .ok()
        .map(|b| sha256_hex(&b));

    let profile_checksum = options
        .profile_path
        .as_ref()
        .and_then(|p| std::fs::read(p).ok())
        .map(|b| sha256_hex(&b));

    let generated_at_ts_ms = if options.deterministic {
        0
    } else {
        now_ts_ms()
    };

    // Serialize profile sections as opaque JSON values so they can be re-applied at run time.
    let storages = profile
        .and_then(|p| p.storages.as_ref())
        .and_then(|v| serde_json::to_value(v).ok());
    let catalogs = profile
        .and_then(|p| p.catalogs.as_ref())
        .and_then(|v| serde_json::to_value(v).ok());
    let lineage = profile
        .and_then(|p| p.lineage.as_ref())
        .and_then(|v| serde_json::to_value(v).ok());

    CommonManifest {
        schema: "floe.manifest.v1",
        generated_at_ts_ms,
        floe_version: env!("CARGO_PKG_VERSION"),
        spec_version: config.version.clone(),
        manifest_name: options.manifest_name.clone(),
        manifest_id: build_manifest_id(&config_uri, config_checksum.as_deref()),
        manifest_revision: None,
        config_uri,
        config_checksum,
        profile_uri: options.profile_uri.clone(),
        profile_checksum,
        report_base_uri: report_base.uri,
        domains: config
            .domains
            .iter()
            .map(|domain| ManifestDomain {
                name: domain.name.clone(),
                incoming_dir: domain
                    .resolved_incoming_dir
                    .clone()
                    .unwrap_or_else(|| domain.incoming_dir.clone()),
            })
            .collect(),
        execution: default_execution_contract(options),
        runners: runners_contract(profile),
        entities: manifest_entities,
        storages,
        catalogs,
        lineage,
    }
}

fn resolve_or_raw(
    resolver: &StorageResolver,
    entity_name: &str,
    field: &str,
    storage_name: Option<&str>,
    raw_path: &str,
) -> ResolvedOrRaw {
    match resolver.resolve_path(entity_name, field, storage_name, raw_path) {
        Ok(resolved) => ResolvedOrRaw {
            storage: resolved.storage,
            uri: resolved.uri,
            resolved: true,
        },
        Err(_) => ResolvedOrRaw {
            storage: storage_name.unwrap_or("local").to_string(),
            uri: raw_path.to_string(),
            resolved: false,
        },
    }
}

/// Convert a resolved URI to the path value used in manifest source/sink entries.
/// Remote URIs (s3://, gs://, abfs://) are kept as-is.
/// Local URIs (local:///abs/path) have the scheme stripped so the StorageResolver
/// receives a plain filesystem path rather than an unrecognised `local://` prefix.
fn resolved_uri_to_path(uri: &str) -> String {
    if let Some(path) = uri.strip_prefix("local://") {
        path.to_string()
    } else {
        uri.to_string()
    }
}

fn canonical_config_uri(display: &str) -> String {
    if display.contains("://") {
        display.to_string()
    } else {
        format!("local://{}", display)
    }
}

fn build_manifest_id(config_uri: &str, config_checksum: Option<&str>) -> String {
    const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
    const FNV_PRIME: u64 = 0x100000001b3;

    let mut hash = FNV_OFFSET_BASIS;
    hash = fnv1a_update(hash, config_uri.as_bytes(), FNV_PRIME);
    hash = fnv1a_update(hash, &[0], FNV_PRIME);
    hash = fnv1a_update(hash, config_checksum.unwrap_or("").as_bytes(), FNV_PRIME);

    format!("mfv1-{hash:016x}")
}

fn fnv1a_update(mut hash: u64, bytes: &[u8], prime: u64) -> u64 {
    for byte in bytes {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(prime);
    }
    hash
}

fn map_source_options(options: Option<&SourceOptions>) -> Option<serde_json::Value> {
    let options = options?;
    let mut map = serde_json::Map::new();
    map.insert("header".to_string(), serde_json::json!(options.header));
    map.insert(
        "separator".to_string(),
        serde_json::json!(options.separator),
    );
    map.insert("encoding".to_string(), serde_json::json!(options.encoding));
    map.insert(
        "null_values".to_string(),
        serde_json::json!(options.null_values),
    );
    map.insert(
        "recursive".to_string(),
        serde_json::json!(options.recursive),
    );
    map.insert("glob".to_string(), serde_json::json!(options.glob));
    map.insert(
        "json_mode".to_string(),
        serde_json::json!(options.json_mode),
    );
    map.insert("sheet".to_string(), serde_json::json!(options.sheet));
    map.insert(
        "header_row".to_string(),
        serde_json::json!(options.header_row),
    );
    map.insert("data_row".to_string(), serde_json::json!(options.data_row));
    map.insert("row_tag".to_string(), serde_json::json!(options.row_tag));
    map.insert(
        "namespace".to_string(),
        serde_json::json!(options.namespace),
    );
    map.insert(
        "value_tag".to_string(),
        serde_json::json!(options.value_tag),
    );
    Some(serde_json::Value::Object(map))
}

fn default_execution_contract(options: &ManifestOptions) -> ManifestExecution {
    let mut exit_codes = BTreeMap::new();
    exit_codes.insert("0", "success_or_rejected");
    exit_codes.insert("1", "technical_failure");
    exit_codes.insert("2", "aborted");

    const PLACEHOLDER: &str = "{manifest_uri}";
    let base_args = [
        "run",
        "--manifest",
        PLACEHOLDER,
        "--log-format",
        "json",
        "--quiet",
    ]
    .iter()
    .map(|&a| {
        if a == PLACEHOLDER {
            options
                .manifest_uri
                .as_deref()
                .unwrap_or(PLACEHOLDER)
                .to_string()
        } else {
            a.to_string()
        }
    })
    .collect();

    ManifestExecution {
        entrypoint: "floe",
        base_args,
        per_entity_args: vec!["--entities".to_string(), "{entity_name}".to_string()],
        log_format: "json",
        result_contract: ManifestResultContract {
            run_finished_event: true,
            summary_uri_field: "summary_uri",
            exit_codes,
        },
        defaults: ManifestExecutionDefaults {
            env: BTreeMap::new(),
            workdir: None,
        },
    }
}

fn runners_contract(profile: Option<&ProfileConfig>) -> ManifestRunners {
    let profile_runner_type = profile
        .and_then(|p| p.execution.as_ref())
        .map(|e| e.runner.runner_type.as_str());

    match profile_runner_type {
        Some("kubernetes_job") => {
            let profile_runner = profile
                .and_then(|p| p.execution.as_ref())
                .map(|e| &e.runner);
            let mut definitions = BTreeMap::new();
            definitions.insert(
                "default",
                ManifestRunnerDefinition {
                    runner_type: "kubernetes_job",
                    command: profile_runner.and_then(|r| r.command.clone()),
                    args: profile_runner.and_then(|r| r.args.clone()),
                    timeout_seconds: profile_runner.and_then(|r| r.timeout_seconds),
                    ttl_seconds_after_finished: profile_runner
                        .and_then(|r| r.ttl_seconds_after_finished),
                    poll_interval_seconds: profile_runner.and_then(|r| r.poll_interval_seconds),
                    secrets: profile_runner.and_then(|r| {
                        r.secrets.as_ref().map(|secrets| {
                            secrets
                                .iter()
                                .map(|s| ManifestRunnerSecret {
                                    name: s.name.clone(),
                                    secret_name: s.secret_name.clone(),
                                    key: s.key.clone(),
                                })
                                .collect()
                        })
                    }),
                    image: profile_runner.and_then(|r| r.image.clone()),
                    namespace: profile_runner.and_then(|r| r.namespace.clone()),
                    service_account: profile_runner.and_then(|r| r.service_account.clone()),
                    resources: profile_runner.and_then(|r| {
                        r.resources.as_ref().map(|res| ManifestRunnerResources {
                            cpu: res.cpu.clone(),
                            memory_mb: res.memory_mb,
                        })
                    }),
                    env: profile_runner.and_then(|r| {
                        r.env
                            .as_ref()
                            .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
                    }),
                    workspace_url: None,
                    existing_cluster_id: None,
                    config_uri: None,
                    python_file_uri: None,
                    job_name: None,
                    auth: None,
                    env_parameters: None,
                },
            );
            ManifestRunners {
                default: "default",
                definitions,
            }
        }
        Some("databricks_job") => {
            let profile_runner = profile
                .and_then(|p| p.execution.as_ref())
                .map(|e| &e.runner);
            let mut definitions = BTreeMap::new();
            definitions.insert(
                "default",
                ManifestRunnerDefinition {
                    runner_type: "databricks_job",
                    command: profile_runner.and_then(|r| r.command.clone()),
                    args: profile_runner.and_then(|r| r.args.clone()),
                    timeout_seconds: profile_runner.and_then(|r| r.timeout_seconds),
                    ttl_seconds_after_finished: None,
                    poll_interval_seconds: profile_runner.and_then(|r| r.poll_interval_seconds),
                    secrets: None,
                    image: None,
                    namespace: None,
                    service_account: None,
                    resources: None,
                    env: None,
                    workspace_url: profile_runner.and_then(|r| r.workspace_url.clone()),
                    existing_cluster_id: profile_runner.and_then(|r| r.existing_cluster_id.clone()),
                    config_uri: profile_runner.and_then(|r| r.config_uri.clone()),
                    python_file_uri: profile_runner.and_then(|r| r.python_file_uri.clone()),
                    job_name: profile_runner
                        .and_then(|r| r.job_name.clone())
                        .or_else(|| Some("floe-{domain}-{env}".to_string())),
                    auth: profile_runner.and_then(|r| {
                        r.auth.as_ref().map(|auth| ManifestRunnerAuth {
                            service_principal_oauth_ref: auth.service_principal_oauth_ref.clone(),
                        })
                    }),
                    env_parameters: profile_runner.and_then(|r| {
                        r.env_parameters
                            .as_ref()
                            .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
                    }),
                },
            );
            ManifestRunners {
                default: "default",
                definitions,
            }
        }
        // "local" or absent → local_process (backward-compatible default)
        _ => {
            let mut definitions = BTreeMap::new();
            definitions.insert(
                "local",
                ManifestRunnerDefinition {
                    runner_type: "local_process",
                    command: None,
                    args: None,
                    timeout_seconds: None,
                    ttl_seconds_after_finished: None,
                    poll_interval_seconds: None,
                    secrets: None,
                    image: None,
                    namespace: None,
                    service_account: None,
                    resources: None,
                    env: None,
                    workspace_url: None,
                    existing_cluster_id: None,
                    config_uri: None,
                    python_file_uri: None,
                    job_name: None,
                    auth: None,
                    env_parameters: None,
                },
            );
            ManifestRunners {
                default: "local",
                definitions,
            }
        }
    }
}

fn now_ts_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_millis() as u64)
        .unwrap_or(0)
}