sombrax_agentic_core 0.1.1

SombraX Agentic Core (SAC) — a provider-agnostic Rust library for building LLM agents: content-modifying hooks, tool/MCP integration, and context optimization.
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
//! Pipeline builder + build-time validation.

use std::collections::{BTreeMap, HashMap, HashSet};

use super::error::PipelineValidationError;
use super::model::{
    BundleId, BundleTemplate, EdgeTemplate, JobId, JobTemplate, MergePolicy, PipelineId,
    PipelineSpec,
};

/// Fluent builder for a [`PipelineSpec`].
///
/// Calling [`PipelineBuilder::build`] runs validation and returns an immutable spec.
pub struct PipelineBuilder {
    id: PipelineId,
    name: String,
    jobs: Vec<JobTemplate>,
    edges: Vec<EdgeTemplate>,
    bundles: Vec<BundleTemplate>,
    /// Optional registry of known kinds; when set, validation rejects unknown kinds.
    known_kinds: Option<HashSet<String>>,
}

impl PipelineBuilder {
    /// New builder with a fresh pipeline id.
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            id: PipelineId::new(),
            name: name.into(),
            jobs: Vec::new(),
            edges: Vec::new(),
            bundles: Vec::new(),
            known_kinds: None,
        }
    }

    /// Override the pipeline id (useful for stable ids across processes).
    pub fn with_id(mut self, id: PipelineId) -> Self {
        self.id = id;
        self
    }

    /// Restrict allowed `kind` strings to a known set. Validation rejects any
    /// job whose kind is missing from this set.
    pub fn with_known_kinds<I, S>(mut self, kinds: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.known_kinds = Some(kinds.into_iter().map(Into::into).collect());
        self
    }

    /// Add a job template. Returns the assigned id.
    pub fn job(mut self, template: JobTemplate) -> Self {
        self.jobs.push(template);
        self
    }

    /// Add an edge template.
    pub fn edge(mut self, template: EdgeTemplate) -> Self {
        self.edges.push(template);
        self
    }

    /// Add a bundle template.
    pub fn bundle(mut self, template: BundleTemplate) -> Self {
        self.bundles.push(template);
        self
    }

    /// Run all build-time checks and consume the builder into a [`PipelineSpec`].
    pub fn build(self) -> Result<PipelineSpec, PipelineValidationError> {
        let Self {
            id,
            name,
            jobs,
            edges,
            bundles,
            known_kinds,
        } = self;

        // -- duplicates --
        let mut seen_jobs = HashSet::new();
        for j in &jobs {
            if !seen_jobs.insert(j.id) {
                return Err(PipelineValidationError::DuplicateJob(j.id));
            }
        }
        let mut seen_edges = HashSet::new();
        for e in &edges {
            if !seen_edges.insert(e.id) {
                return Err(PipelineValidationError::DuplicateEdge(e.id));
            }
        }
        let mut seen_bundles = HashSet::new();
        for b in &bundles {
            if !seen_bundles.insert(b.id) {
                return Err(PipelineValidationError::DuplicateBundle(b.id));
            }
        }

        // -- known kinds --
        if let Some(set) = &known_kinds {
            for j in &jobs {
                if !set.contains(&j.kind) {
                    return Err(PipelineValidationError::UnknownKind {
                        job_id: j.id,
                        kind: j.kind.clone(),
                    });
                }
            }
        }

        // -- edges reference existing jobs --
        let job_ids: HashSet<JobId> = jobs.iter().map(|j| j.id).collect();
        for e in &edges {
            if !job_ids.contains(&e.from) {
                return Err(PipelineValidationError::UnknownJob {
                    edge_id: e.id,
                    job_id: e.from,
                });
            }
            if !job_ids.contains(&e.to) {
                return Err(PipelineValidationError::UnknownJob {
                    edge_id: e.id,
                    job_id: e.to,
                });
            }
        }

        // -- bundles reference existing jobs + parents --
        let bundle_ids: HashSet<BundleId> = bundles.iter().map(|b| b.id).collect();
        for b in &bundles {
            for jid in &b.job_ids {
                if !job_ids.contains(jid) {
                    return Err(PipelineValidationError::BundleUnknownJob {
                        bundle_id: b.id,
                        job_id: *jid,
                    });
                }
            }
            if let Some(p) = b.parent {
                if !bundle_ids.contains(&p) {
                    return Err(PipelineValidationError::BundleUnknownParent {
                        bundle_id: b.id,
                        parent: p,
                    });
                }
            }
            for s in &b.successor_ids {
                if !bundle_ids.contains(s) {
                    return Err(PipelineValidationError::BundleUnknownParent {
                        bundle_id: b.id,
                        parent: *s,
                    });
                }
            }
        }

        // -- same-target edge conflicts --
        // Bucket by (to, target). All edges in a bucket must agree on MergePolicy,
        // and any bucket with > 1 entry rejects MergePolicy::Reject.
        let mut buckets: HashMap<(JobId, &str), Vec<&EdgeTemplate>> = HashMap::new();
        for e in &edges {
            buckets
                .entry((e.to, e.target.as_str()))
                .or_default()
                .push(e);
        }
        for ((to, target), bucket) in &buckets {
            if bucket.len() < 2 {
                continue;
            }
            let policies: HashSet<MergePolicy> = bucket.iter().map(|e| e.merge).collect();
            if policies.len() > 1 {
                return Err(PipelineValidationError::EdgeConflict {
                    to: *to,
                    target: (*target).to_string(),
                    detail: format!(
                        "{} edges share this target with disagreeing MergePolicy values: {:?}",
                        bucket.len(),
                        policies
                    ),
                });
            }
            if policies.contains(&MergePolicy::Reject) {
                return Err(PipelineValidationError::EdgeConflict {
                    to: *to,
                    target: (*target).to_string(),
                    detail: format!(
                        "{} edges share this target with MergePolicy::Reject",
                        bucket.len()
                    ),
                });
            }
        }

        // -- DAG cycle check (DFS three-colour) --
        let mut adjacency: BTreeMap<JobId, Vec<JobId>> = BTreeMap::new();
        for j in &jobs {
            adjacency.entry(j.id).or_default();
        }
        for e in &edges {
            adjacency.entry(e.from).or_default().push(e.to);
        }
        if let Some(cycle) = find_cycle(&adjacency) {
            return Err(PipelineValidationError::Cycle(cycle));
        }

        Ok(PipelineSpec {
            id,
            name,
            jobs,
            edges,
            bundles,
        })
    }
}

/// Returns one cycle (job ids in order along the cycle) if any exists.
fn find_cycle(adj: &BTreeMap<JobId, Vec<JobId>>) -> Option<Vec<JobId>> {
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum Color {
        White,
        Grey,
        Black,
    }
    let mut color: HashMap<JobId, Color> = adj.keys().map(|k| (*k, Color::White)).collect();
    let mut parent: HashMap<JobId, Option<JobId>> = adj.keys().map(|k| (*k, None)).collect();

    fn dfs(
        node: JobId,
        adj: &BTreeMap<JobId, Vec<JobId>>,
        color: &mut HashMap<JobId, Color>,
        parent: &mut HashMap<JobId, Option<JobId>>,
    ) -> Option<Vec<JobId>> {
        color.insert(node, Color::Grey);
        if let Some(neighbours) = adj.get(&node) {
            for &nx in neighbours {
                match color[&nx] {
                    Color::Grey => {
                        // walk back from `node` until we hit `nx`
                        let mut cycle = vec![nx, node];
                        let mut cur = parent[&node];
                        while let Some(p) = cur {
                            if p == nx {
                                break;
                            }
                            cycle.push(p);
                            cur = parent[&p];
                        }
                        cycle.reverse();
                        return Some(cycle);
                    }
                    Color::White => {
                        parent.insert(nx, Some(node));
                        if let Some(c) = dfs(nx, adj, color, parent) {
                            return Some(c);
                        }
                    }
                    Color::Black => {}
                }
            }
        }
        color.insert(node, Color::Black);
        None
    }

    let nodes: Vec<JobId> = adj.keys().copied().collect();
    for n in nodes {
        if color[&n] == Color::White {
            if let Some(c) = dfs(n, adj, &mut color, &mut parent) {
                return Some(c);
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runs::model::{
        EdgeCondition, EdgeId, EdgeTemplate, JobTemplate, JoinPolicy, MergePolicy, OutputProjection,
    };

    fn job(kind: &str) -> JobTemplate {
        JobTemplate {
            id: JobId::new(),
            kind: kind.into(),
            default_inputs: serde_json::Value::Null,
            bundle_id: None,
            join_policy: JoinPolicy::AllRequired,
        }
    }

    fn edge(from: JobId, to: JobId, target: &str, merge: MergePolicy) -> EdgeTemplate {
        EdgeTemplate {
            id: EdgeId::new(),
            from,
            to,
            source: OutputProjection::Whole,
            target: target.into(),
            condition: EdgeCondition::Always,
            merge,
            required: true,
        }
    }

    #[test]
    fn cycle_is_detected() {
        let a = job("a");
        let b = job("b");
        let e1 = edge(a.id, b.id, "x", MergePolicy::LastWriteWins);
        let e2 = edge(b.id, a.id, "y", MergePolicy::LastWriteWins);
        let res = PipelineBuilder::new("p")
            .job(a)
            .job(b)
            .edge(e1)
            .edge(e2)
            .build();
        assert!(matches!(res, Err(PipelineValidationError::Cycle(_))));
    }

    #[test]
    fn duplicate_job_id_fails() {
        let a = job("a");
        let b = JobTemplate {
            id: a.id,
            ..job("b")
        };
        let res = PipelineBuilder::new("p").job(a).job(b).build();
        assert!(matches!(res, Err(PipelineValidationError::DuplicateJob(_))));
    }

    #[test]
    fn unknown_kind_fails() {
        let a = job("foo");
        let res = PipelineBuilder::new("p")
            .with_known_kinds(["bar"])
            .job(a)
            .build();
        assert!(matches!(
            res,
            Err(PipelineValidationError::UnknownKind { .. })
        ));
    }

    #[test]
    fn merge_reject_with_two_edges_fails() {
        let a = job("a");
        let b = job("b");
        let c = job("c");
        let e1 = edge(a.id, c.id, "x", MergePolicy::Reject);
        let e2 = edge(b.id, c.id, "x", MergePolicy::Reject);
        let res = PipelineBuilder::new("p")
            .job(a)
            .job(b)
            .job(c)
            .edge(e1)
            .edge(e2)
            .build();
        assert!(matches!(
            res,
            Err(PipelineValidationError::EdgeConflict { .. })
        ));
    }

    #[test]
    fn merge_disagreement_fails() {
        let a = job("a");
        let b = job("b");
        let c = job("c");
        let e1 = edge(a.id, c.id, "x", MergePolicy::AppendArray);
        let e2 = edge(b.id, c.id, "x", MergePolicy::LastWriteWins);
        let res = PipelineBuilder::new("p")
            .job(a)
            .job(b)
            .job(c)
            .edge(e1)
            .edge(e2)
            .build();
        assert!(matches!(
            res,
            Err(PipelineValidationError::EdgeConflict { .. })
        ));
    }

    #[test]
    fn diamond_dag_builds() {
        let a = job("a");
        let b = job("b");
        let c = job("c");
        let d = job("d");
        let edges = [
            edge(a.id, b.id, "x", MergePolicy::LastWriteWins),
            edge(a.id, c.id, "x", MergePolicy::LastWriteWins),
            edge(b.id, d.id, "left", MergePolicy::LastWriteWins),
            edge(c.id, d.id, "right", MergePolicy::LastWriteWins),
        ];
        let mut b_ = PipelineBuilder::new("p").job(a).job(b).job(c).job(d);
        for e in edges {
            b_ = b_.edge(e);
        }
        b_.build().expect("diamond builds");
    }
}