domainforge-core 0.22.0

Rust core library implementing the SEA DSL primitives and validation engine.
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
//! Graph ML Dataset Projection: export the semantic graph for downstream
//! graph learning. Negative samples are resolver-grounded, never invented.

use super::report::{CoverageReport, GenerationReport, RejectedArtifact, ValidationReport};
use super::split::assign_split;
use super::AiLearningContext;
use serde::Serialize;
use serde_json::{json, Map, Value};
use std::collections::{BTreeMap, BTreeSet};
use xxhash_rust::xxh64::xxh64;

#[derive(Debug, Clone, Serialize)]
struct Node {
    node_id: String,
    name: String,
    node_type: String,
    namespace: String,
}

#[derive(Debug, Clone, Serialize)]
struct Edge {
    edge_id: String,
    edge_type: String,
    source: String,
    target: String,
    #[serde(skip_serializing_if = "Map::is_empty")]
    attributes: Map<String, Value>,
}

#[derive(Debug, Clone, Serialize)]
struct NegativeSample {
    sample_id: String,
    role_ref: String,
    operation: String,
    resource_ref: String,
    label: String,
    reason: String,
    resolver_reason_code: String,
    applicable_policies: Vec<String>,
}

fn csv_escape(value: &str) -> String {
    if value.contains(',') || value.contains('"') || value.contains('\n') {
        format!("\"{}\"", value.replace('"', "\"\""))
    } else {
        value.to_string()
    }
}

pub fn emit(
    ctx: &AiLearningContext,
    sink: &mut super::ArtifactSink,
) -> Result<Vec<String>, String> {
    let cfg = &ctx.recipe.projections.graph_ml_dataset;
    let tasks: BTreeSet<&str> = cfg.tasks.iter().map(|s| s.as_str()).collect();
    if tasks.contains("policy_violation_detection") {
        ctx.require_authority("graph_ml policy_violation_detection")?;
    }

    let graph = ctx.graph;
    let mut nodes: Vec<Node> = Vec::new();
    let mut edges: Vec<Edge> = Vec::new();
    let mut errors: Vec<String> = Vec::new();

    // Nodes: what the Graph actually exposes, categorical features only.
    for e in graph.all_entities() {
        nodes.push(Node {
            node_id: e.id().to_string(),
            name: e.name().to_string(),
            node_type: "entity".to_string(),
            namespace: e.namespace().to_string(),
        });
    }
    for r in graph.all_roles() {
        nodes.push(Node {
            node_id: r.id().to_string(),
            name: r.name().to_string(),
            node_type: "role".to_string(),
            namespace: r.namespace().to_string(),
        });
    }
    for r in graph.all_resources() {
        nodes.push(Node {
            node_id: r.id().to_string(),
            name: r.name().to_string(),
            node_type: "resource".to_string(),
            namespace: r.namespace().to_string(),
        });
    }
    // Authority-pack policies participate as nodes so applicability is learnable.
    for info in ctx.policy_info.values() {
        nodes.push(Node {
            node_id: info.policy_id.clone(),
            name: info.policy_id.clone(),
            node_type: "policy".to_string(),
            namespace: info.pack_id.clone(),
        });
    }
    nodes.sort_by(|a, b| a.node_id.cmp(&b.node_id));

    // Edges from the actual relation/assignment vocabulary.
    let mut push_edge =
        |edge_type: &str, source: String, target: String, attributes: Map<String, Value>| {
            let edge_id = super::split::record_id(&[edge_type, &source, &target]);
            edges.push(Edge {
                edge_id,
                edge_type: edge_type.to_string(),
                source,
                target,
                attributes,
            });
        };

    for entity in graph.all_entities() {
        if let Some(role_ids) = graph.roles_for_entity(entity.id()) {
            for role_id in role_ids {
                push_edge(
                    "role_assignment",
                    entity.id().to_string(),
                    role_id.to_string(),
                    Map::new(),
                );
            }
        }
    }
    for flow in graph.all_flows() {
        let mut attributes = Map::new();
        attributes.insert(
            "resource".into(),
            Value::String(flow.resource_id().to_string()),
        );
        push_edge(
            "flow",
            flow.from_id().to_string(),
            flow.to_id().to_string(),
            attributes,
        );
    }
    for relation in graph.all_relations() {
        let mut attributes = Map::new();
        attributes.insert(
            "predicate".into(),
            Value::String(relation.predicate().to_string()),
        );
        attributes.insert(
            "relation_name".into(),
            Value::String(relation.name().to_string()),
        );
        push_edge(
            "relation",
            relation.subject_role().to_string(),
            relation.object_role().to_string(),
            attributes,
        );
    }
    for info in ctx.policy_info.values() {
        if let Some(role_name) = &info.actor_role {
            if let Some(role_id) = graph.find_role_by_name(role_name) {
                push_edge(
                    "policy_applies_to_role",
                    info.policy_id.clone(),
                    role_id.to_string(),
                    Map::new(),
                );
            }
        }
        if let Some(resource_name) = &info.resource_type {
            if let Some(resource_id) = graph.find_resource_by_name(resource_name) {
                push_edge(
                    "policy_applies_to_resource",
                    info.policy_id.clone(),
                    resource_id.to_string(),
                    Map::new(),
                );
            }
        }
    }
    edges.sort_by(|a, b| a.edge_id.cmp(&b.edge_id));

    // Validation: every edge endpoint must be a known node.
    let node_ids: BTreeSet<&str> = nodes.iter().map(|n| n.node_id.as_str()).collect();
    let mut rejected: Vec<RejectedArtifact> = Vec::new();
    edges.retain(|edge| {
        let ok = node_ids.contains(edge.source.as_str()) && node_ids.contains(edge.target.as_str());
        if !ok {
            rejected.push(RejectedArtifact {
                artifact_id: edge.edge_id.clone(),
                reason: "edge_references_missing_node".to_string(),
            });
        }
        ok
    });

    // Resolver-grounded samples for policy_violation_detection.
    let mut positives: Vec<NegativeSample> = Vec::new();
    let mut negatives: Vec<NegativeSample> = Vec::new();
    if tasks.contains("policy_violation_detection") {
        for case in &ctx.authority_cases {
            let sample_id = super::split::record_id(&[
                "authz_sample",
                &case.role_ref,
                &case.operation,
                &case.resource_ref,
            ]);
            let sample = |label: &str, reason: &str| NegativeSample {
                sample_id: sample_id.clone(),
                role_ref: case.role_ref.clone(),
                operation: case.operation.clone(),
                resource_ref: case.resource_ref.clone(),
                label: label.to_string(),
                reason: reason.to_string(),
                resolver_reason_code: case.reason_code.clone(),
                applicable_policies: case.applicable_policies.clone(),
            };
            match case.label {
                // Allow is a positive sample and must never be emitted as negative.
                "allow" => positives.push(sample("positive", "allowed_by_policy")),
                "deny" | "reject" => negatives.push(sample("negative", "forbidden_by_policy")),
                "escalate" => negatives.push(sample("negative", "requires_escalation")),
                "unknown" => {
                    // Open-world safety: Unknown is not evidence of prohibition.
                    if cfg.treat_unknown_as_negative {
                        negatives.push(sample("negative", "unknown_authority"));
                    }
                }
                other => errors.push(format!("unhandled decision label '{other}'")),
            }
        }
        // Hard error path: a negative sample whose resolver decision is Allow
        // is a generator bug.
        let allow_tuples: BTreeSet<String> = ctx
            .authority_cases
            .iter()
            .filter(|c| c.label == "allow")
            .map(|c| format!("{}|{}|{}", c.role_ref, c.operation, c.resource_ref))
            .collect();
        for neg in &negatives {
            let key = format!("{}|{}|{}", neg.role_ref, neg.operation, neg.resource_ref);
            if allow_tuples.contains(&key) {
                return Err(format!(
                    "generator bug: negative sample {} has resolver decision Allow",
                    neg.sample_id
                ));
            }
        }
        // Deterministic cap: keep the lowest stable-hash negatives.
        let cap = (positives.len() as u64).saturating_mul(cfg.max_negative_ratio as u64) as usize;
        if negatives.len() > cap {
            negatives.sort_by_key(|n| {
                xxh64(
                    format!("{}\u{1}{}\u{1}{}", n.sample_id, ctx.model_hash, ctx.seed).as_bytes(),
                    ctx.seed,
                )
            });
            negatives.truncate(cap);
            negatives.sort_by(|a, b| a.sample_id.cmp(&b.sample_id));
        }
    }

    // Split masks over nodes (node_classification) — disjoint by construction.
    let mut masks: BTreeMap<&str, Vec<String>> = BTreeMap::new();
    masks.insert("train", vec![]);
    masks.insert("validation", vec![]);
    masks.insert("test", vec![]);
    for node in &nodes {
        let split = assign_split(&node.node_id, &ctx.model_hash, ctx.seed, &ctx.recipe.splits);
        masks.get_mut(split).unwrap().push(node.node_id.clone());
    }
    let mut warnings: Vec<String> = Vec::new();
    if masks["train"].is_empty() && !nodes.is_empty() {
        let moved = nodes[0].node_id.clone();
        for list in masks.values_mut() {
            list.retain(|id| id != &moved);
        }
        masks.get_mut("train").unwrap().push(moved);
        warnings.push(
            "train mask was empty; reassigned one node to train (tiny-dataset rule)".to_string(),
        );
    }

    // Labels per task.
    let mut labels = Map::new();
    if tasks.contains("node_classification") {
        let mut node_labels = Map::new();
        for node in &nodes {
            node_labels.insert(node.node_id.clone(), Value::String(node.node_type.clone()));
        }
        labels.insert("node_classification".into(), Value::Object(node_labels));
    }
    if tasks.contains("link_prediction") {
        labels.insert(
            "link_prediction".into(),
            json!({
                "positive_edges": edges.iter().map(|e| e.edge_id.clone()).collect::<Vec<_>>(),
                "negative_samples_ref": "negative_samples.json",
            }),
        );
    }
    if tasks.contains("policy_violation_detection") {
        labels.insert(
            "policy_violation_detection".into(),
            json!({
                "positive_count": positives.len(),
                "negative_count": negatives.len(),
                "samples_ref": "negative_samples.json",
            }),
        );
    }

    let mut artifacts = Vec::new();

    let graph_json = json!({
        "nodes": nodes,
        "edges": edges,
        "features": {
            "node": ["node_type", "namespace"],
            "edge": ["edge_type"],
            "note": "categorical only; no invented numeric features in v0",
        },
        "provenance": ctx.provenance(vec![], "graph_export", "unreviewed"),
    });
    sink.write("graph.json", &super::to_json_pretty(&graph_json)?)?;
    artifacts.push("graph.json".to_string());

    let mut nodes_csv = String::from("node_id,name,node_type,namespace\n");
    for n in &nodes {
        nodes_csv.push_str(&format!(
            "{},{},{},{}\n",
            csv_escape(&n.node_id),
            csv_escape(&n.name),
            csv_escape(&n.node_type),
            csv_escape(&n.namespace)
        ));
    }
    sink.write("nodes.csv", &nodes_csv)?;
    artifacts.push("nodes.csv".to_string());

    let mut edges_csv = String::from("edge_id,edge_type,source,target\n");
    for e in &edges {
        edges_csv.push_str(&format!(
            "{},{},{},{}\n",
            csv_escape(&e.edge_id),
            csv_escape(&e.edge_type),
            csv_escape(&e.source),
            csv_escape(&e.target)
        ));
    }
    sink.write("edges.csv", &edges_csv)?;
    artifacts.push("edges.csv".to_string());

    sink.write(
        "labels.json",
        &super::to_json_pretty(&Value::Object(labels))?,
    )?;
    artifacts.push("labels.json".to_string());

    sink.write("masks.json", &super::to_json_pretty(&json!(masks))?)?;
    artifacts.push("masks.json".to_string());

    let neg_json = json!({
        "positives": positives,
        "negatives": negatives,
        "policy": {
            "unknown_excluded_by_default": !cfg.treat_unknown_as_negative,
            "max_negative_ratio": cfg.max_negative_ratio,
        },
        "provenance": ctx.provenance(vec![], "authority_resolver", "unreviewed"),
    });
    sink.write("negative_samples.json", &super::to_json_pretty(&neg_json)?)?;
    artifacts.push("negative_samples.json".to_string());

    // Reports.
    let mut node_type_counts: BTreeMap<String, u64> = BTreeMap::new();
    for n in &nodes {
        *node_type_counts.entry(n.node_type.clone()).or_default() += 1;
    }
    let mut edge_type_counts: BTreeMap<String, u64> = BTreeMap::new();
    for e in &edges {
        *edge_type_counts.entry(e.edge_type.clone()).or_default() += 1;
    }
    let mut reason_counts: BTreeMap<String, u64> = BTreeMap::new();
    for n in &negatives {
        *reason_counts.entry(n.reason.clone()).or_default() += 1;
    }
    let connected: BTreeSet<&str> = edges
        .iter()
        .flat_map(|e| [e.source.as_str(), e.target.as_str()])
        .collect();
    let orphans: Vec<String> = nodes
        .iter()
        .filter(|n| !connected.contains(n.node_id.as_str()))
        .map(|n| n.node_id.clone())
        .collect();

    let manifest = json!({
        "dataset_kind": "graph_ml_dataset",
        "tasks": cfg.tasks,
        "node_count": nodes.len(),
        "edge_count": edges.len(),
        "provenance": ctx.provenance(vec![], "graph_export", "unreviewed"),
    });
    sink.write("dataset_manifest.json", &super::to_json_pretty(&manifest)?)?;
    artifacts.push("dataset_manifest.json".to_string());

    let mut details = Map::new();
    details.insert("node_count".into(), Value::from(nodes.len() as u64));
    details.insert("edge_count".into(), Value::from(edges.len() as u64));
    details.insert("rejected_edges".into(), Value::from(rejected.len() as u64));
    let status = if errors.is_empty() {
        "passed"
    } else {
        "failed"
    };
    let validation = ValidationReport {
        projection_kind: "graph_ml_dataset".to_string(),
        status: status.to_string(),
        requirements_passed: (nodes.len() + edges.len()) as u64,
        requirements_failed: rejected.len() as u64,
        warnings: warnings.clone(),
        errors: errors.clone(),
        rejected_artifacts: rejected,
        repair_recommendations: vec![],
        details,
    };
    sink.write(
        "validation_report.json",
        &super::to_json_pretty(&validation)?,
    )?;
    artifacts.push("validation_report.json".to_string());

    let mut cov_details = Map::new();
    cov_details.insert(
        "node_type_distribution".into(),
        super::report::counts_to_value(&node_type_counts),
    );
    cov_details.insert(
        "edge_type_distribution".into(),
        super::report::counts_to_value(&edge_type_counts),
    );
    cov_details.insert(
        "negative_sample_reasons".into(),
        super::report::counts_to_value(&reason_counts),
    );
    cov_details.insert(
        "negative_sample_count".into(),
        Value::from(negatives.len() as u64),
    );
    cov_details.insert(
        "positive_sample_count".into(),
        Value::from(positives.len() as u64),
    );
    cov_details.insert("orphan_nodes".into(), json!(orphans.clone()));
    let coverage = CoverageReport {
        projection_kind: "graph_ml_dataset".to_string(),
        details: cov_details,
        known_gaps: orphans,
    };
    sink.write("coverage_report.json", &super::to_json_pretty(&coverage)?)?;
    artifacts.push("coverage_report.json".to_string());

    let generation = GenerationReport {
        projection_kind: "graph_ml_dataset".to_string(),
        recipe_ref: ctx.recipe_ref.clone(),
        recipe_hash: ctx.recipe_hash.clone(),
        source_model_ref: ctx.model_ref.clone(),
        source_model_hash: ctx.model_hash.clone(),
        generator_version: super::GENERATOR_VERSION.to_string(),
        seed: ctx.seed,
        started_at: ctx.created_at.clone(),
        completed_at: ctx.created_at.clone(),
        artifacts_generated: artifacts.clone(),
        families_generated: cfg.tasks.clone(),
        generation_methods_used: vec!["graph_export".to_string(), "authority_resolver".to_string()],
        warnings,
        errors,
    };
    sink.write(
        "generation_report.json",
        &super::to_json_pretty(&generation)?,
    )?;
    artifacts.push("generation_report.json".to_string());

    Ok(artifacts)
}