domainforge-core 0.17.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
use crate::application::canonical::{semantic_pack_set_hash, source_set_hash};
use crate::application::diagnostic::ApplicationDiagnostic;
use crate::application::envelope::{
    build_cep_envelope, invalid_declared_checkpoint_hash, resolve_semantic_envelope_with_packs,
    CepEnvelopeParams,
};
use crate::application::resolve::resolve_application_graph;
use crate::module::resolver::source_map_from_filesystem;
use crate::registry::NamespaceRegistry;
use crate::semantic_pack::canonical_json::{canonical_json, compute_sha256};
use anyhow::{Context, Result};
use chrono::SecondsFormat;
use clap::{Parser, ValueEnum};
use serde_json::{json, Value};
use std::fs;
use std::path::PathBuf;
use std::process::exit;
use uuid::Uuid;

#[derive(Parser)]
pub struct EnvelopeArgs {
    /// Entry .sea file whose resolved closure is emitted
    #[arg(required_unless_present = "capabilities")]
    pub entry: Option<PathBuf>,

    /// What to emit
    #[arg(long, value_enum, default_value_t = EmitMode::Representation)]
    pub emit: EmitMode,

    /// Semantic pack inputs as <pack_id>=<content_hash>, repeatable
    #[arg(long = "pack", value_parser = parse_pack)]
    pub packs: Vec<(String, String)>,

    /// Path to namespace registry file (defaults to discovery from entry)
    #[arg(long)]
    pub registry: Option<PathBuf>,

    /// Namespace to use when the entry has no resolved registry binding
    #[arg(long)]
    pub default_namespace: Option<String>,

    /// CEP scope object (JSON). Replaces the default derived scope.
    #[arg(long)]
    pub scope: Option<String>,

    /// Inline representations up to this many bytes; larger ones use content_ref
    #[arg(long, default_value_t = 65_536)]
    pub inline_threshold_bytes: u64,

    /// Write output to FILE instead of stdout
    #[arg(long, short = 'o')]
    pub out: Option<PathBuf>,

    /// Print adapter capabilities as JSON and exit
    #[arg(long)]
    pub capabilities: bool,
}

#[derive(ValueEnum, Clone, Debug, Copy, PartialEq, Eq)]
pub enum EmitMode {
    /// Canonical D only (byte-identical across runs)
    Representation,
    /// CEP-0008 semantic snapshot envelope only
    Cep,
    /// Both, as {"representation": D, "cep_envelope": E}
    Both,
    /// CEP `work_request` verification contract (§14.0)
    VerificationContract,
}

fn parse_pack(raw: &str) -> Result<(String, String), String> {
    let (pack_id, content_hash) = raw
        .split_once('=')
        .ok_or_else(|| format!("--pack expects <pack_id>=<content_hash>, got '{raw}'"))?;
    if pack_id.is_empty() || content_hash.is_empty() {
        return Err(format!(
            "--pack expects <pack_id>=<content_hash>, got '{raw}'"
        ));
    }
    Ok((pack_id.to_string(), content_hash.to_string()))
}

pub fn run(args: EnvelopeArgs) -> Result<()> {
    if args.capabilities {
        let caps = json!({
            "producer": "domainforge-core",
            "version": env!("CARGO_PKG_VERSION"),
            "supported_emit_modes": ["representation", "cep", "both", "verification-contract"],
            "contracts": ["CEP-0008", "domainforge-semantic-envelope/v1"]
        });
        println!("{}", serde_json::to_string_pretty(&caps)?);
        return Ok(());
    }

    let entry = args
        .entry
        .as_ref()
        .expect("entry path required unless --capabilities is specified");

    let source = match fs::read_to_string(entry) {
        Ok(source) => source,
        Err(error) => {
            eprintln!("error: failed to read {}: {error}", entry.display());
            exit(2);
        }
    };
    let registry = match &args.registry {
        Some(path) => match NamespaceRegistry::from_file(path) {
            Ok(registry) => Some(registry),
            Err(error) => {
                eprintln!("error: failed to load registry {}: {error}", path.display());
                exit(2);
            }
        },
        None => NamespaceRegistry::discover(entry).ok().flatten(),
    };
    let default_namespace = args.default_namespace.clone().or_else(|| {
        registry
            .as_ref()
            .and_then(|reg| reg.namespace_for(entry).map(str::to_string))
    });
    let registry_content_hash = registry_content_hash(registry.as_ref());
    let user_scope = match args.scope.as_deref() {
        None => None,
        Some(text) => match serde_json::from_str::<Value>(text) {
            Ok(value @ Value::Object(_)) => Some(value),
            Ok(_) => {
                eprintln!("error: --scope must be a JSON object");
                exit(2);
            }
            Err(error) => {
                eprintln!("error: --scope is not valid JSON: {error}");
                exit(2);
            }
        },
    };
    let created_at = chrono::Utc::now().to_rfc3339_opts(SecondsFormat::Micros, true);
    let envelope_id = Uuid::now_v7().to_string();
    let inline_threshold_bytes = args.inline_threshold_bytes;

    let (entry_logical_path, sources) = match source_map_from_filesystem(
        entry,
        &source,
        registry.as_ref(),
        default_namespace.as_deref(),
    ) {
        Ok(resolved) => resolved,
        Err(diagnostics) => {
            let messages: Vec<String> = diagnostics.iter().map(render_diagnostic).collect();
            let logical = entry.display().to_string();
            emit_failed(
                &args,
                FailureContext {
                    entry_logical_path: &logical,
                    sources: &[(logical.clone(), source)],
                    messages: &messages,
                    registry_content_hash: registry_content_hash.as_deref(),
                    user_scope: user_scope.as_ref(),
                    envelope_id: &envelope_id,
                    created_at: &created_at,
                    inline_threshold_bytes,
                },
            );
        }
    };

    let sources_json =
        serde_json::to_string(&sources.0).context("failed to serialize resolved source map")?;

    match resolve_semantic_envelope_with_packs(&entry_logical_path, &sources_json, &args.packs) {
        Ok(doc) => {
            let validation = resolve_application_graph(&entry_logical_path, &sources_json)
                .map(|graph| graph.validate());
            let model_valid = validation
                .as_ref()
                .map(|result| result.error_count == 0)
                .unwrap_or(false);
            let diagnostics: Vec<String> = match &validation {
                Ok(result) => result
                    .violations
                    .iter()
                    .map(|violation| format!("{}: {}", violation.policy_name, violation.message))
                    .collect(),
                Err(application_diagnostics) => application_diagnostics
                    .iter()
                    .map(render_diagnostic)
                    .collect(),
            };
            let resolved_namespaces: Vec<(String, String)> = doc
                .envelope
                .namespace_bindings
                .iter()
                .map(|binding| (binding.logical_id.clone(), binding.namespace.clone()))
                .collect();
            let scope = match &user_scope {
                Some(value) => value.clone(),
                None => json!({
                    "entry_logical_path": entry_logical_path,
                    "semantic_closure_hash": doc.semantic_closure_hash,
                }),
            };
            let envelope = build_cep_envelope(&CepEnvelopeParams {
                doc: Some(&doc),
                model_valid,
                source_set_hash: &doc.inputs.source_set_hash,
                invalid_declared_checkpoint_hash: None,
                diagnostics: &diagnostics,
                scope,
                entry_logical_path: &entry_logical_path,
                inline_threshold_bytes,
                envelope_id: &envelope_id,
                created_at: &created_at,
                registry_content_hash: registry_content_hash.as_deref(),
                resolved_namespaces: &resolved_namespaces,
            });
            let representation = serde_json::to_string_pretty(&doc)
                .context("failed to serialize canonical envelope document")?;
            if args.emit == EmitMode::VerificationContract {
                if !model_valid {
                    eprintln!(
                        "error: verification contract requires a valid declared model; {} error(s)",
                        diagnostics.len()
                    );
                    exit(1);
                }
                let contract =
                    crate::application::verification_contract::build_verification_contract(
                        &doc,
                        &entry_logical_path,
                        &sources_json,
                        &doc.inputs.source_set_hash,
                        &envelope_id,
                        &created_at,
                        registry_content_hash.as_deref(),
                        &resolved_namespaces,
                    )
                    .map_err(|e| anyhow::anyhow!("failed to build verification contract: {e}"))?;
                emit(&args, &EmitPayload::Envelope(contract))?;
                return Ok(());
            }
            let payload = match args.emit {
                EmitMode::Representation => EmitPayload::Representation(representation),
                EmitMode::Cep | EmitMode::VerificationContract => EmitPayload::Envelope(envelope),
                EmitMode::Both => EmitPayload::Both(representation, envelope),
            };
            emit(&args, &payload)?;
            if !model_valid {
                eprintln!(
                    "error: model validation failed: {} error(s); envelope emitted",
                    diagnostics.len()
                );
                exit(1);
            }
            Ok(())
        }
        Err(diagnostics) => {
            let messages: Vec<String> = diagnostics.iter().map(render_diagnostic).collect();
            let sources: Vec<(String, String)> = sources
                .0
                .iter()
                .map(|(id, text)| (id.clone(), text.clone()))
                .collect();
            emit_failed(
                &args,
                FailureContext {
                    entry_logical_path: &entry_logical_path,
                    sources: &sources,
                    messages: &messages,
                    registry_content_hash: registry_content_hash.as_deref(),
                    user_scope: user_scope.as_ref(),
                    envelope_id: &envelope_id,
                    created_at: &created_at,
                    inline_threshold_bytes,
                },
            );
        }
    }
}

enum EmitPayload {
    Representation(String),
    Envelope(Value),
    Both(String, Value),
}

fn emit(args: &EnvelopeArgs, payload: &EmitPayload) -> Result<()> {
    let text = match (args.emit, payload) {
        (EmitMode::Representation, EmitPayload::Representation(document)) => document.clone(),
        (
            EmitMode::Cep | EmitMode::Both | EmitMode::VerificationContract,
            EmitPayload::Envelope(envelope),
        ) => serde_json::to_string_pretty(envelope)?,
        (EmitMode::Cep, EmitPayload::Both(_, envelope)) => serde_json::to_string_pretty(envelope)?,
        (EmitMode::Both, EmitPayload::Both(representation, envelope)) => {
            let representation: Value = serde_json::from_str(representation)?;
            serde_json::to_string_pretty(&json!({
                "representation": representation,
                "cep_envelope": envelope,
            }))?
        }
        (EmitMode::Representation, EmitPayload::Envelope(_) | EmitPayload::Both(_, _)) => {
            eprintln!(
                "error: cannot emit representation: canonical D is unavailable (model validation failed)"
            );
            exit(1);
        }
        (
            EmitMode::Cep | EmitMode::Both | EmitMode::VerificationContract,
            EmitPayload::Representation(_),
        ) => {
            unreachable!()
        }
        (EmitMode::VerificationContract, EmitPayload::Both(_, _)) => unreachable!(),
    };
    match &args.out {
        Some(path) => {
            fs::write(path, text).with_context(|| format!("failed to write {}", path.display()))?
        }
        None => {
            print!("{text}");
            if !text.ends_with('\n') {
                println!();
            }
        }
    }
    Ok(())
}

/// Identity and diagnostic context for a failed-model emission (§14.4).
struct FailureContext<'a> {
    entry_logical_path: &'a str,
    sources: &'a [(String, String)],
    messages: &'a [String],
    registry_content_hash: Option<&'a str>,
    user_scope: Option<&'a Value>,
    envelope_id: &'a str,
    created_at: &'a str,
    inline_threshold_bytes: u64,
}

fn emit_failed(args: &EnvelopeArgs, context: FailureContext<'_>) -> ! {
    let source_set_hash = source_set_hash(
        context
            .sources
            .iter()
            .map(|(id, text)| (id.as_str(), text.as_str())),
    )
    .unwrap_or_else(|_| format!("sha256:{}", "0".repeat(64)));
    let pack_set_hash = semantic_pack_set_hash(&args.packs)
        .unwrap_or_else(|_| format!("sha256:{}", "0".repeat(64)));
    let checkpoint = invalid_declared_checkpoint_hash(
        &source_set_hash,
        &pack_set_hash,
        context.entry_logical_path,
        context.registry_content_hash,
        &[],
    );
    let scope = match context.user_scope {
        Some(value) => value.clone(),
        None => json!({
            "entry_logical_path": context.entry_logical_path,
            "invalid_declared_checkpoint_hash": checkpoint,
        }),
    };
    let envelope = build_cep_envelope(&CepEnvelopeParams {
        doc: None,
        model_valid: false,
        source_set_hash: &source_set_hash,
        invalid_declared_checkpoint_hash: Some(&checkpoint),
        diagnostics: context.messages,
        scope,
        entry_logical_path: context.entry_logical_path,
        inline_threshold_bytes: context.inline_threshold_bytes,
        envelope_id: context.envelope_id,
        created_at: context.created_at,
        registry_content_hash: context.registry_content_hash,
        resolved_namespaces: &[],
    });
    if let Err(error) = emit(args, &EmitPayload::Envelope(envelope)) {
        eprintln!("error: failed to emit envelope: {error}");
        exit(2);
    }
    eprintln!(
        "error: model validation failed: {} diagnostic(s); envelope emitted with missing-D omission",
        context.messages.len()
    );
    exit(1);
}

fn render_diagnostic(diagnostic: &ApplicationDiagnostic) -> String {
    format!("{}: {}", diagnostic.code.slug(), diagnostic.message)
}

fn registry_content_hash(registry: Option<&NamespaceRegistry>) -> Option<String> {
    let registry = registry?;
    let mut files: Vec<(String, String)> = match registry.resolve_files() {
        Ok(files) => files
            .iter()
            .map(|binding| {
                (
                    binding.path.to_string_lossy().to_string(),
                    binding.namespace.clone(),
                )
            })
            .collect(),
        Err(_) => return None,
    };
    files.sort();
    let value = json!({
        "schema_version": "domainforge-registry/v1",
        "bindings": files,
    });
    Some(compute_sha256(canonical_json(&value).as_bytes()))
}