dpcs 0.8.0

Reference implementation of the Data Pipeline Contract Standard (DPCS)
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
//! Shared plan views used by orchestrator adapters.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Component, Path};

use crate::binding::artifact::BindingFile;
use crate::binding::diagnostics::{self, report_error};
use crate::binding::framework::BindContext;
use crate::diagnostics::ValidationReport;
use crate::model::{
    ContractReference, ExecutionRequirements, FailureSemantics, PipelineStep, QualityGate,
    SchedulingIntent, SchedulingMode,
};
use crate::plan::{PipelinePlan, PlanDependencyEdge};

/// Stable, adapter-facing projection of a Pipeline Plan.
#[derive(Debug, Clone)]
pub struct PlanView<'a> {
    /// Source plan.
    pub plan: &'a PipelinePlan,
    /// Binding context (profile + capability report).
    pub ctx: &'a BindContext<'a>,
}

impl<'a> PlanView<'a> {
    /// Create a plan view.
    pub fn new(plan: &'a PipelinePlan, ctx: &'a BindContext<'a>) -> Self {
        Self { plan, ctx }
    }

    /// Contract identifier.
    pub fn contract_id(&self) -> &str {
        &self.plan.contract_id
    }

    /// Contract version.
    pub fn contract_version(&self) -> &str {
        &self.plan.contract_version
    }

    /// Profile identity used for binding.
    pub fn profile_identity(&self) -> &str {
        self.ctx.profile_identity
    }

    /// Ordered step identifiers (plan order).
    pub fn step_order(&self) -> &[String] {
        &self.plan.step_order
    }

    /// Steps keyed by declaration with lookup by id via [`Self::step`].
    pub fn steps(&self) -> &[PipelineStep] {
        &self.plan.steps
    }

    /// Find a step by id.
    pub fn step(&self, id: &str) -> Option<&PipelineStep> {
        self.plan.steps.iter().find(|step| step.id == id)
    }

    /// Dependency edges.
    pub fn dependency_edges(&self) -> &[PlanDependencyEdge] {
        &self.plan.dependency_edges
    }

    /// Contract references.
    pub fn contract_references(&self) -> &[ContractReference] {
        &self.plan.contract_references
    }

    /// Scheduling intents.
    pub fn scheduling(&self) -> &[SchedulingIntent] {
        &self.plan.scheduling
    }

    /// Quality gates.
    pub fn quality_gates(&self) -> &[QualityGate] {
        &self.plan.quality_gates
    }

    /// Failure semantics.
    pub fn failure_semantics(&self) -> &[FailureSemantics] {
        &self.plan.failure_semantics
    }

    /// Execution requirements.
    pub fn execution(&self) -> Option<&ExecutionRequirements> {
        self.plan.execution.as_ref()
    }

    /// First cron expression from scheduled intents, if any.
    pub fn primary_cron(&self) -> Option<&str> {
        self.plan.scheduling.iter().find_map(|intent| {
            if matches!(intent.mode, SchedulingMode::Scheduled) {
                intent.cron.as_deref()
            } else {
                None
            }
        })
    }

    /// First timezone from scheduling intents, if any.
    pub fn primary_timezone(&self) -> Option<&str> {
        self.plan
            .scheduling
            .iter()
            .find_map(|intent| intent.timezone.as_deref())
    }

    /// Predecessor step ids for `step_id` derived from dependency edges (sorted).
    pub fn predecessors(&self, step_id: &str) -> Vec<&str> {
        let mut preds: Vec<&str> = self
            .dependency_edges()
            .iter()
            .filter(|edge| edge.to == step_id)
            .map(|edge| edge.from.as_str())
            .collect();
        preds.sort_unstable();
        preds.dedup();
        preds
    }

    /// Unique Python identifiers for every plan step id (and optional extras).
    pub fn unique_python_idents(&self, extras: &[&str]) -> BTreeMap<String, String> {
        let mut ids: Vec<String> = self.step_order().to_vec();
        for extra in extras {
            if !ids.iter().any(|id| id == *extra) {
                ids.push((*extra).to_owned());
            }
        }
        unique_sanitized(&ids, Self::python_ident)
    }

    /// Unique Kubernetes name fragments for every plan step id (and optional extras).
    pub fn unique_k8s_names(&self, extras: &[&str]) -> BTreeMap<String, String> {
        let mut ids: Vec<String> = self.step_order().to_vec();
        for extra in extras {
            if !ids.iter().any(|id| id == *extra) {
                ids.push((*extra).to_owned());
            }
        }
        unique_sanitized(&ids, Self::k8s_name)
    }

    /// Sanitize an identifier for use as a Python name.
    pub fn python_ident(raw: &str) -> String {
        let mut out = String::with_capacity(raw.len());
        for ch in raw.chars() {
            if ch.is_ascii_alphanumeric() || ch == '_' {
                out.push(ch);
            } else {
                out.push('_');
            }
        }
        if out.is_empty() {
            return "_unnamed".to_owned();
        }
        if out.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            out.insert(0, '_');
        }
        out
    }

    /// Build a PascalCase Python class name (Temporal workflow class).
    pub fn python_class_name(raw: &str) -> String {
        let mut parts = Vec::new();
        let mut current = String::new();
        for ch in raw.chars() {
            if ch.is_ascii_alphanumeric() {
                current.push(ch);
            } else if !current.is_empty() {
                parts.push(std::mem::take(&mut current));
            }
        }
        if !current.is_empty() {
            parts.push(current);
        }
        let mut name = String::new();
        for part in parts {
            let mut chars = part.chars();
            if let Some(first) = chars.next() {
                name.push(first.to_ascii_uppercase());
                for ch in chars {
                    name.push(ch.to_ascii_lowercase());
                }
            }
        }
        if name.is_empty() {
            name = "Pipeline".to_owned();
        }
        if name.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            name.insert(0, '_');
        }
        if !name.ends_with("Workflow") {
            name.push_str("Workflow");
        }
        name
    }

    /// Sanitize an identifier for use as a Kubernetes label / resource name fragment.
    pub fn k8s_name(raw: &str) -> String {
        let mut out: String = raw
            .chars()
            .map(|ch| {
                if ch.is_ascii_alphanumeric() {
                    ch.to_ascii_lowercase()
                } else {
                    '-'
                }
            })
            .collect();
        while out.contains("--") {
            out = out.replace("--", "-");
        }
        out = out.trim_matches('-').to_owned();
        if out.is_empty() {
            "pipeline".to_owned()
        } else if out.len() > 63 {
            out.truncate(63);
            out.trim_end_matches('-').to_owned()
        } else {
            out
        }
    }

    /// Escape a string for embedding in a Python double-quoted literal.
    pub fn py_string(raw: &str) -> String {
        let mut out = String::with_capacity(raw.len());
        for ch in raw.chars() {
            match ch {
                '\\' => out.push_str("\\\\"),
                '"' => out.push_str("\\\""),
                '\n' => out.push_str("\\n"),
                '\r' => out.push_str("\\r"),
                '\t' => out.push_str("\\t"),
                c if c.is_control() => {
                    out.push_str(&format!("\\u{{{:04x}}}", u32::from(c)));
                }
                c => out.push(c),
            }
        }
        out
    }

    /// Escape a string for a YAML double-quoted scalar.
    pub fn yaml_string(raw: &str) -> String {
        let mut out = String::with_capacity(raw.len());
        for ch in raw.chars() {
            match ch {
                '\\' => out.push_str("\\\\"),
                '"' => out.push_str("\\\""),
                '\n' => out.push_str("\\n"),
                '\r' => out.push_str("\\r"),
                '\t' => out.push_str("\\t"),
                c if c.is_control() => {
                    out.push_str(&format!("\\u{:04x}", u32::from(c)));
                }
                c => out.push(c),
            }
        }
        out
    }

    /// Build a shared header comment block documenting preserved semantics.
    pub fn header_comment(&self, platform: &str) -> String {
        let mut lines = vec![
            format!("# DPCS orchestrator binding scaffold ({platform})"),
            format!("# contractId: {}", self.contract_id()),
            format!("# contractVersion: {}", self.contract_version()),
            format!("# profileIdentity: {}", self.profile_identity()),
            format!("# dpcsVersion: {}", self.plan.dpcs_version),
            format!("# stepOrder: {}", self.step_order().join(", ")),
        ];
        if !self.scheduling().is_empty() {
            let modes: Vec<_> = self
                .scheduling()
                .iter()
                .map(|intent| intent.mode.as_str())
                .collect();
            lines.push(format!("# schedulingModes: {}", modes.join(", ")));
        }
        if let Some(cron) = self.primary_cron() {
            lines.push(format!("# scheduleCron: {cron}"));
        }
        if let Some(tz) = self.primary_timezone() {
            lines.push(format!("# scheduleTimezone: {tz}"));
        }
        if !self.quality_gates().is_empty() {
            let ids: Vec<_> = self.quality_gates().iter().map(|g| g.id.as_str()).collect();
            lines.push(format!("# qualityGates: {}", ids.join(", ")));
        }
        if !self.failure_semantics().is_empty() {
            let ids: Vec<_> = self
                .failure_semantics()
                .iter()
                .map(|f| f.id.as_str())
                .collect();
            lines.push(format!("# failureSemantics: {}", ids.join(", ")));
        }
        if let Some(exec) = self.execution() {
            if !exec.required_capabilities.is_empty() {
                lines.push(format!(
                    "# requiredCapabilities: {}",
                    exec.required_capabilities.join(", ")
                ));
            }
        }
        if let Some(lineage) = &self.plan.lineage {
            if let Some(prov) = &lineage.provenance {
                if let Some(originating) = &prov.originating {
                    lines.push(format!("# lineageOriginating: {originating}"));
                }
            }
        }
        for step in self.steps() {
            let contract = step
                .contract_ref
                .as_deref()
                .or(step.transform_ref.as_deref())
                .unwrap_or("-");
            lines.push(format!(
                "# step {} type={} contractRef={}",
                step.id, step.step_type, contract
            ));
        }
        for edge in self.dependency_edges() {
            lines.push(format!("# dependency {} -> {}", edge.from, edge.to));
        }
        for reference in self.contract_references() {
            lines.push(format!(
                "# contractReference {} type={} location={}",
                reference.id, reference.reference_type, reference.location
            ));
        }
        lines.push(
            "# Scaffold encodes identity/topology where the target allows; QG/FS/execution intents are documented here."
                .to_owned(),
        );
        lines.push(
            "# Generated by dpcs; do not invent runtime behavior beyond the Pipeline Plan."
                .to_owned(),
        );
        lines.join("\n")
    }
}

/// Map original ids to unique sanitized identifiers, disambiguating collisions.
pub fn unique_sanitized(
    ids: &[String],
    sanitize: impl Fn(&str) -> String,
) -> BTreeMap<String, String> {
    let mut used = BTreeSet::new();
    let mut map = BTreeMap::new();
    for id in ids {
        let base = sanitize(id);
        let mut candidate = base.clone();
        let mut n = 2u32;
        while !used.insert(candidate.clone()) {
            candidate = format!("{base}__{n}");
            n += 1;
        }
        map.insert(id.clone(), candidate);
    }
    map
}

/// Reject relative paths that can escape `out_dir`.
pub fn validate_relative_path(relative_path: &str) -> Result<(), ValidationReport> {
    if relative_path.is_empty() {
        return Err(report_error(diagnostics::write_failed(
            "binding file relative_path must not be empty",
        )));
    }
    if relative_path.contains('\0') {
        return Err(report_error(diagnostics::write_failed(
            "binding file relative_path must not contain NUL",
        )));
    }
    let path = Path::new(relative_path);
    if path.is_absolute() {
        return Err(report_error(diagnostics::write_failed(format!(
            "binding file relative_path must be relative, got `{relative_path}`"
        ))));
    }
    for component in path.components() {
        match component {
            Component::Normal(_) | Component::CurDir => {}
            Component::ParentDir => {
                return Err(report_error(diagnostics::write_failed(format!(
                    "binding file relative_path must not contain `..`: `{relative_path}`"
                ))));
            }
            Component::RootDir | Component::Prefix(_) => {
                return Err(report_error(diagnostics::write_failed(format!(
                    "binding file relative_path must be relative, got `{relative_path}`"
                ))));
            }
        }
    }
    Ok(())
}

/// Helper to build a Python source file artifact.
pub fn python_file(relative_path: &str, content: String) -> BindingFile {
    BindingFile::new(relative_path, "text/x-python", content)
}

/// Helper to build a YAML artifact.
pub fn yaml_file(relative_path: &str, content: String) -> BindingFile {
    BindingFile::new(relative_path, "application/yaml", content)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unique_sanitized_disambiguates_collisions() {
        let ids = vec!["a.b".to_owned(), "a_b".to_owned()];
        let map = unique_sanitized(&ids, PlanView::python_ident);
        assert_ne!(map["a.b"], map["a_b"]);
        assert!(map.values().all(|v| v.starts_with("a_b")));
    }

    #[test]
    fn validate_relative_path_rejects_escape() {
        assert!(validate_relative_path("../etc/passwd").is_err());
        assert!(validate_relative_path("/etc/passwd").is_err());
        assert!(validate_relative_path("dags/ok.py").is_ok());
    }

    #[test]
    fn python_class_name_is_pascal_case() {
        assert_eq!(
            PlanView::python_class_name("valid.execution.model"),
            "ValidExecutionModelWorkflow"
        );
    }
}