wesley-core 0.2.0

Wesley Rust Core - Deterministic compiler kernel
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
//! Domain-free Wesley project manifest parsing and validation.
//!
//! The manifest names GraphQL source files, generic output/evidence locations,
//! and extension target metadata. It intentionally does not assign database,
//! runtime, renderer, or product semantics to those targets.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::{Map as JsonMap, Value as JsonValue};
use yaml_rust2::{Yaml, YamlLoader};

/// Supported Wesley project manifest API version.
pub const PROJECT_MANIFEST_API_VERSION: &str = "wesley.project-manifest/v1";

/// Domain-free project manifest for Wesley compiler and evidence workflows.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ProjectManifest {
    /// Manifest API version.
    #[serde(default = "default_api_version")]
    pub api_version: String,
    /// GraphQL schema paths, optionally with per-schema rebuild globs.
    #[serde(default)]
    pub schema_paths: Vec<SchemaPathConfig>,
    /// Directory used for generated Wesley evidence bundles.
    #[serde(default = "default_bundle_dir")]
    pub bundle_dir: String,
    /// Global file globs that force every schema set to rebuild.
    #[serde(default)]
    pub rebuild_on_globs: Vec<String>,
    /// Preferred PR comment update behavior for automation.
    #[serde(default)]
    pub comment_mode: CommentMode,
    /// Optional dashboard artifact settings.
    #[serde(default)]
    pub dashboard: DashboardConfig,
    /// Selected extension targets. Target-specific semantics belong to modules.
    #[serde(default)]
    pub targets: Vec<ManifestTargetConfig>,
}

impl ProjectManifest {
    /// Returns schema paths with stable ids and explicit per-schema globs.
    pub fn resolved_schema_paths(&self) -> Vec<ResolvedSchemaPath> {
        self.schema_paths
            .iter()
            .enumerate()
            .map(|(index, entry)| entry.resolve(index))
            .collect()
    }
}

/// Schema path entry, either compact string form or object form.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum SchemaPathConfig {
    /// Compact schema path form.
    Path(String),
    /// Detailed schema path form.
    Detailed(DetailedSchemaPathConfig),
}

/// Detailed schema path form with a stable id and rebuild globs.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DetailedSchemaPathConfig {
    /// Stable schema set id.
    #[serde(default)]
    pub id: Option<String>,
    /// GraphQL SDL path.
    pub path: String,
    /// File globs that rebuild this schema set.
    #[serde(default)]
    pub rebuild_on_globs: Vec<String>,
}

impl SchemaPathConfig {
    fn resolve(&self, index: usize) -> ResolvedSchemaPath {
        match self {
            Self::Path(path) => ResolvedSchemaPath {
                id: slug_from_path(path, index),
                path: path.clone(),
                rebuild_on_globs: Vec::new(),
            },
            Self::Detailed(DetailedSchemaPathConfig {
                id,
                path,
                rebuild_on_globs,
            }) => ResolvedSchemaPath {
                id: id.clone().unwrap_or_else(|| slug_from_path(path, index)),
                path: path.clone(),
                rebuild_on_globs: rebuild_on_globs.clone(),
            },
        }
    }
}

/// Normalized schema path used for validation and changed-file selection.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResolvedSchemaPath {
    /// Stable schema set id.
    pub id: String,
    /// GraphQL SDL path.
    pub path: String,
    /// File globs that rebuild this schema set.
    pub rebuild_on_globs: Vec<String>,
}

/// Schema selected by changed-file analysis.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectedSchemaPath {
    /// Stable schema set id.
    pub id: String,
    /// GraphQL SDL path.
    pub path: String,
    /// Bundle directory for this schema set.
    pub bundle_dir: String,
    /// Why this schema set was selected.
    pub reason: String,
}

/// PR comment update behavior for Wesley automation.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CommentMode {
    /// Update the existing bot comment when possible.
    #[default]
    Update,
    /// Append a new comment for each run.
    Append,
    /// Do not write PR comments.
    Silent,
}

impl CommentMode {
    /// Stable manifest string for this mode.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Update => "update",
            Self::Append => "append",
            Self::Silent => "silent",
        }
    }
}

/// Optional dashboard artifact settings.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DashboardConfig {
    /// Whether to emit dashboard artifacts.
    #[serde(default)]
    pub enabled: bool,
    /// Dashboard artifact path.
    #[serde(default)]
    pub artifact_path: Option<String>,
}

/// Domain-free extension target selection.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ManifestTargetConfig {
    /// Stable target name.
    pub name: String,
    /// Optional external module identity that owns target semantics.
    #[serde(default)]
    pub module: Option<String>,
    /// Whether this target is selected when no explicit target is requested.
    #[serde(default, rename = "default")]
    pub is_default: bool,
    /// Optional output directory for this target.
    #[serde(default)]
    pub output_dir: Option<String>,
    /// Generic mutually-exclusive target group.
    #[serde(default)]
    pub exclusive_group: Option<String>,
    /// Target names that cannot be selected with this target.
    #[serde(default)]
    pub conflicts_with: Vec<String>,
}

/// Parse or validation error for a Wesley project manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProjectManifestError {
    /// The manifest could not be parsed as JSON or YAML.
    Parse(String),
    /// The manifest API version is unsupported.
    UnsupportedApiVersion(String),
    /// The manifest shape parsed but failed validation.
    Validation(Vec<String>),
}

impl std::fmt::Display for ProjectManifestError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Parse(message) => write!(formatter, "manifest parse error: {message}"),
            Self::UnsupportedApiVersion(version) => write!(
                formatter,
                "unsupported manifest apiVersion '{version}'; expected {PROJECT_MANIFEST_API_VERSION}"
            ),
            Self::Validation(diagnostics) => {
                write!(formatter, "manifest validation failed")?;
                for diagnostic in diagnostics {
                    write!(formatter, "; {diagnostic}")?;
                }
                Ok(())
            }
        }
    }
}

impl std::error::Error for ProjectManifestError {}

/// Parse and validate a Wesley project manifest from JSON or YAML.
pub fn load_project_manifest(source: &str) -> Result<ProjectManifest, ProjectManifestError> {
    let value = parse_manifest_value(source)?;
    let manifest = serde_json::from_value::<ProjectManifest>(value)
        .map_err(|source| ProjectManifestError::Parse(source.to_string()))?;
    validate_project_manifest(&manifest)?;
    Ok(manifest)
}

/// Validate a parsed Wesley project manifest.
pub fn validate_project_manifest(manifest: &ProjectManifest) -> Result<(), ProjectManifestError> {
    if manifest.api_version != PROJECT_MANIFEST_API_VERSION {
        return Err(ProjectManifestError::UnsupportedApiVersion(
            manifest.api_version.clone(),
        ));
    }

    let mut diagnostics = Vec::new();
    let schemas = manifest.resolved_schema_paths();
    let mut schema_ids = BTreeSet::new();
    let mut schema_paths = BTreeSet::new();
    for schema in &schemas {
        if schema.id.trim().is_empty() {
            diagnostics.push("schemaPaths contains a blank id".to_owned());
        }
        if schema_id_is_dot_only(&schema.id) {
            diagnostics.push(format!(
                "schemaPath id '{}' must not be '.', '..', or only dots",
                schema.id
            ));
        } else if !schema_id_is_path_safe(&schema.id) {
            diagnostics.push(format!(
                "schemaPath id '{}' must contain only ASCII letters, digits, '.', '_', or '-'",
                schema.id
            ));
        }
        if schema.path.trim().is_empty() {
            diagnostics.push(format!("schemaPath `{}` has a blank path", schema.id));
        }
        if !schema_ids.insert(schema.id.clone()) {
            diagnostics.push(format!("duplicate schemaPath id '{}'", schema.id));
        }
        if !schema_paths.insert(normalize_path(&schema.path)) {
            diagnostics.push(format!("duplicate schemaPath path '{}'", schema.path));
        }
    }

    if manifest.bundle_dir.trim().is_empty() {
        diagnostics.push("bundleDir must not be blank".to_owned());
    }

    let mut target_names = BTreeSet::new();
    let mut default_targets = Vec::new();
    let mut exclusive_groups: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for target in &manifest.targets {
        if target.name.trim().is_empty() {
            diagnostics.push("targets contains a blank name".to_owned());
            continue;
        }
        if !target_names.insert(target.name.clone()) {
            diagnostics.push(format!("duplicate target '{}'", target.name));
        }
        if target.is_default {
            default_targets.push(target.name.clone());
        }
        if let Some(group) = target.exclusive_group.as_deref() {
            if group.trim().is_empty() {
                diagnostics.push(format!(
                    "target '{}' has a blank exclusiveGroup",
                    target.name
                ));
            } else {
                exclusive_groups
                    .entry(group.to_owned())
                    .or_default()
                    .push(target.name.clone());
            }
        }
    }

    if default_targets.len() > 1 {
        diagnostics.push(format!(
            "multiple default targets selected: {}",
            default_targets.join(", ")
        ));
    }

    for (group, targets) in exclusive_groups {
        if targets.len() > 1 {
            diagnostics.push(format!(
                "exclusive group '{group}' selects multiple targets: {}",
                targets.join(", ")
            ));
        }
    }

    for target in &manifest.targets {
        for conflict in &target.conflicts_with {
            if target_names.contains(conflict) {
                diagnostics.push(format!(
                    "target '{}' conflicts with selected target '{}'",
                    target.name, conflict
                ));
            }
        }
    }

    if diagnostics.is_empty() {
        Ok(())
    } else {
        Err(ProjectManifestError::Validation(diagnostics))
    }
}

/// Select schema paths affected by a set of changed files.
pub fn select_changed_schema_paths(
    manifest: &ProjectManifest,
    changed_files: impl IntoIterator<Item = impl AsRef<str>>,
) -> Vec<SelectedSchemaPath> {
    let changed = changed_files
        .into_iter()
        .map(|path| normalize_path(path.as_ref()))
        .filter(|path| !path.is_empty())
        .collect::<Vec<_>>();
    let schemas = manifest.resolved_schema_paths();

    if changed.is_empty() {
        let schema_count = schemas.len();
        return schemas
            .into_iter()
            .map(|schema| SelectedSchemaPath {
                bundle_dir: schema_bundle_dir(&manifest.bundle_dir, &schema.id, schema_count),
                id: schema.id,
                path: schema.path,
                reason: "no changed files provided".to_owned(),
            })
            .collect();
    }

    for glob in &manifest.rebuild_on_globs {
        if changed.iter().any(|path| glob_matches(glob, path)) {
            let schema_count = schemas.len();
            return schemas
                .into_iter()
                .map(|schema| SelectedSchemaPath {
                    bundle_dir: schema_bundle_dir(&manifest.bundle_dir, &schema.id, schema_count),
                    id: schema.id,
                    path: schema.path,
                    reason: format!("matched global rebuild glob `{glob}`"),
                })
                .collect();
        }
    }

    let schema_count = schemas.len();
    schemas
        .into_iter()
        .filter_map(|schema| {
            let schema_path = normalize_path(&schema.path);
            let bundle_dir = schema_bundle_dir(&manifest.bundle_dir, &schema.id, schema_count);
            if changed.iter().any(|path| path == &schema_path) {
                return Some(SelectedSchemaPath {
                    bundle_dir,
                    id: schema.id,
                    path: schema.path,
                    reason: format!("matched schema path `{schema_path}`"),
                });
            }

            for glob in &schema.rebuild_on_globs {
                if changed.iter().any(|path| glob_matches(glob, path)) {
                    return Some(SelectedSchemaPath {
                        bundle_dir,
                        id: schema.id,
                        path: schema.path,
                        reason: format!("matched schema rebuild glob `{glob}`"),
                    });
                }
            }

            None
        })
        .collect()
}

fn schema_bundle_dir(base: &str, schema_id: &str, schema_count: usize) -> String {
    let base = normalize_path(base);
    if schema_count <= 1 {
        return base;
    }
    join_normalized_path(&base, schema_id)
}

fn join_normalized_path(base: &str, child: &str) -> String {
    match base {
        "." => format!("./{child}"),
        "/" => format!("/{child}"),
        _ => format!("{base}/{child}"),
    }
}

fn schema_id_is_path_safe(id: &str) -> bool {
    id.chars()
        .all(|character| character.is_ascii_alphanumeric() || matches!(character, '.' | '_' | '-'))
}

fn schema_id_is_dot_only(id: &str) -> bool {
    !id.is_empty() && id.chars().all(|character| character == '.')
}

fn default_api_version() -> String {
    PROJECT_MANIFEST_API_VERSION.to_owned()
}

fn default_bundle_dir() -> String {
    ".wesley-cache".to_owned()
}

fn parse_manifest_value(source: &str) -> Result<JsonValue, ProjectManifestError> {
    match serde_json::from_str::<JsonValue>(source) {
        Ok(value) => Ok(value),
        Err(json_error) => {
            let documents = YamlLoader::load_from_str(source).map_err(|yaml_error| {
                ProjectManifestError::Parse(format!(
                    "not valid JSON ({json_error}); not valid YAML ({yaml_error})"
                ))
            })?;
            if documents.is_empty() {
                return Err(ProjectManifestError::Parse(
                    "YAML source contained no document".to_owned(),
                ));
            }
            if documents.len() != 1 {
                return Err(ProjectManifestError::Parse(
                    "YAML manifests must contain exactly one document".to_owned(),
                ));
            }
            yaml_to_json(&documents[0])
        }
    }
}

fn yaml_to_json(value: &Yaml) -> Result<JsonValue, ProjectManifestError> {
    match value {
        Yaml::Real(value) => value
            .parse::<f64>()
            .map(JsonValue::from)
            .map_err(|source| ProjectManifestError::Parse(source.to_string())),
        Yaml::Integer(value) => Ok(JsonValue::from(*value)),
        Yaml::String(value) => Ok(JsonValue::from(value.clone())),
        Yaml::Boolean(value) => Ok(JsonValue::from(*value)),
        Yaml::Array(values) => values
            .iter()
            .map(yaml_to_json)
            .collect::<Result<Vec<_>, _>>()
            .map(JsonValue::from),
        Yaml::Hash(values) => {
            let mut object = JsonMap::new();
            for (key, value) in values {
                let key = match key {
                    Yaml::String(key) => key.clone(),
                    _ => {
                        return Err(ProjectManifestError::Parse(
                            "YAML manifest keys must be strings".to_owned(),
                        ));
                    }
                };
                object.insert(key, yaml_to_json(value)?);
            }
            Ok(JsonValue::Object(object))
        }
        Yaml::Null | Yaml::BadValue => Ok(JsonValue::Null),
        Yaml::Alias(_) => Err(ProjectManifestError::Parse(
            "YAML aliases are not supported in Wesley manifests".to_owned(),
        )),
    }
}

fn slug_from_path(path: &str, index: usize) -> String {
    let mut slug = normalize_path(path)
        .chars()
        .map(|character| {
            if character.is_ascii_alphanumeric() {
                character.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect::<String>();
    while slug.contains("--") {
        slug = slug.replace("--", "-");
    }
    let slug = slug.trim_matches('-').to_owned();
    if slug.is_empty() {
        format!("schema-{index}")
    } else {
        slug
    }
}

fn normalize_path(path: &str) -> String {
    let path = path.trim().replace('\\', "/");
    let is_absolute = path.starts_with('/');
    let is_current = path == "." || path == "./";
    let normalized = path
        .split('/')
        .filter(|segment| !segment.is_empty() && *segment != ".")
        .collect::<Vec<_>>()
        .join("/");

    if normalized.is_empty() {
        if is_absolute {
            "/".to_owned()
        } else if is_current {
            ".".to_owned()
        } else {
            String::new()
        }
    } else if is_absolute {
        format!("/{normalized}")
    } else {
        normalized
    }
}

fn glob_matches(pattern: &str, path: &str) -> bool {
    let pattern = normalize_path(pattern);
    let path = normalize_path(path);
    if pattern.is_empty() {
        return false;
    }
    if !pattern.contains('*') && !pattern.contains('?') {
        return pattern == path;
    }

    let pattern_segments = pattern.split('/').collect::<Vec<_>>();
    let path_segments = path.split('/').collect::<Vec<_>>();
    match_glob_segments(&pattern_segments, &path_segments)
}

fn match_glob_segments(pattern: &[&str], path: &[&str]) -> bool {
    match pattern.split_first() {
        None => path.is_empty(),
        Some((&"**", rest)) => {
            match_glob_segments(rest, path)
                || (!path.is_empty() && match_glob_segments(pattern, &path[1..]))
        }
        Some((segment_pattern, rest)) => {
            if let Some((path_segment, path_rest)) = path.split_first() {
                segment_matches(segment_pattern, path_segment)
                    && match_glob_segments(rest, path_rest)
            } else {
                false
            }
        }
    }
}

fn segment_matches(pattern: &str, value: &str) -> bool {
    let pattern = pattern.as_bytes();
    let value = value.as_bytes();
    let mut pattern_index = 0;
    let mut value_index = 0;
    let mut star_index = None;
    let mut star_value_index = 0;

    while value_index < value.len() {
        if pattern_index < pattern.len()
            && (pattern[pattern_index] == b'?' || pattern[pattern_index] == value[value_index])
        {
            pattern_index += 1;
            value_index += 1;
        } else if pattern_index < pattern.len() && pattern[pattern_index] == b'*' {
            star_index = Some(pattern_index);
            star_value_index = value_index;
            pattern_index += 1;
        } else if let Some(star) = star_index {
            pattern_index = star + 1;
            star_value_index += 1;
            value_index = star_value_index;
        } else {
            return false;
        }
    }

    while pattern_index < pattern.len() && pattern[pattern_index] == b'*' {
        pattern_index += 1;
    }

    pattern_index == pattern.len()
}