pliego-cli 0.0.1

Unified PliegoRS build, dev, and inspect workflow.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Celiums Solutions LLC

use pliego_artifact::{
    BUILD_GRAPH_NAME, BuildGraph, BuildReport, SourceDependencies, decode_build_graph,
    validate_build_graph_against_report, verify_build_report,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io::Write;
use std::path::Path;

const REBUILD_RECORD_VERSION: &str = "pliego-rebuild/1";
const REBUILD_RECORD_PATH: &str = "target/.pliego/last-rebuild.json";
const MAX_REBUILD_RECORD_BYTES: usize = 1024 * 1024;
const MAX_REBUILD_ITEMS: usize = 100_000;

#[derive(Clone, Debug)]
pub(crate) struct VerifiedGraph {
    pub graph: BuildGraph,
    pub report: BuildReport,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum HmrKind {
    None,
    Css,
    Content,
    Adapter,
    Reload,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct HmrUpdate {
    pub kind: HmrKind,
    pub paths: Vec<String>,
    pub routes: Vec<String>,
}

impl HmrUpdate {
    pub(crate) fn reload() -> Self {
        Self {
            kind: HmrKind::Reload,
            paths: Vec::new(),
            routes: Vec::new(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct RebuildRecord {
    pub record_version: String,
    pub generation: u64,
    pub changed_sources: Vec<String>,
    pub affected_routes: Vec<String>,
    pub affected_artifacts: Vec<String>,
    pub changed_artifacts: Vec<String>,
    pub hmr: HmrUpdate,
    pub receipt_before: Option<String>,
    pub receipt_after: String,
}

pub(crate) fn load_verified_graph(output_root: &Path) -> Result<VerifiedGraph, String> {
    let verified = verify_build_report(output_root).map_err(|error| error.to_string())?;
    let graph_path = output_root.join(BUILD_GRAPH_NAME);
    let bytes = fs::read(&graph_path)
        .map_err(|error| format!("cannot read {}: {error}", graph_path.display()))?;
    let graph = decode_build_graph(&bytes).map_err(|error| error.to_string())?;
    validate_build_graph_against_report(&graph, &verified.report)
        .map_err(|error| error.to_string())?;
    Ok(VerifiedGraph {
        graph,
        report: verified.report,
    })
}

pub(crate) fn explain_rebuild(
    generation: u64,
    changed_sources: BTreeSet<String>,
    before: Option<&VerifiedGraph>,
    after: &VerifiedGraph,
) -> RebuildRecord {
    let known_sources = before
        .into_iter()
        .flat_map(|build| build.graph.sources.iter())
        .chain(after.graph.sources.iter())
        .map(|source| source.path.as_str())
        .collect::<BTreeSet<_>>();
    let global_change = changed_sources
        .iter()
        .any(|path| !known_sources.contains(path.as_str()));

    let mut affected_routes = BTreeSet::new();
    let mut affected_artifacts = BTreeSet::new();
    for graph in before.into_iter().chain(std::iter::once(after)) {
        for route in &graph.graph.routes {
            if global_change || dependencies_touch(&route.sources, &changed_sources) {
                affected_routes.insert(route.route.clone());
                affected_artifacts.extend(route.artifacts.iter().cloned());
            }
        }
        for artifact in &graph.graph.artifacts {
            if global_change || dependencies_touch(&artifact.sources, &changed_sources) {
                affected_artifacts.insert(artifact.path.clone());
            }
        }
    }

    let before_artifacts = before
        .map(|build| artifact_hashes(&build.graph))
        .unwrap_or_default();
    let after_artifacts = artifact_hashes(&after.graph);
    let changed_artifacts = before_artifacts
        .keys()
        .chain(after_artifacts.keys())
        .filter(|path| before_artifacts.get(*path) != after_artifacts.get(*path))
        .cloned()
        .collect::<BTreeSet<_>>();
    let hmr = classify_hmr(&changed_artifacts, &affected_routes, &after.graph);

    RebuildRecord {
        record_version: REBUILD_RECORD_VERSION.to_owned(),
        generation,
        changed_sources: changed_sources.into_iter().collect(),
        affected_routes: affected_routes.into_iter().collect(),
        affected_artifacts: affected_artifacts.into_iter().collect(),
        changed_artifacts: changed_artifacts.into_iter().collect(),
        hmr,
        receipt_before: before.map(|build| build.report.receipt_sha256.clone()),
        receipt_after: after.report.receipt_sha256.clone(),
    }
}

fn dependencies_touch(
    dependencies: &SourceDependencies,
    changed_sources: &BTreeSet<String>,
) -> bool {
    match dependencies {
        SourceDependencies::AllSources => !changed_sources.is_empty(),
        SourceDependencies::Explicit { paths } => paths
            .iter()
            .any(|path| changed_sources.contains(path.as_str())),
    }
}

fn artifact_hashes(graph: &BuildGraph) -> BTreeMap<String, String> {
    graph
        .artifacts
        .iter()
        .map(|artifact| (artifact.path.clone(), artifact.sha256.clone()))
        .collect()
}

fn classify_hmr(
    changed: &BTreeSet<String>,
    affected_routes: &BTreeSet<String>,
    graph: &BuildGraph,
) -> HmrUpdate {
    if changed.is_empty() {
        return HmrUpdate {
            kind: HmrKind::None,
            paths: Vec::new(),
            routes: affected_routes.iter().cloned().collect(),
        };
    }
    let nodes = changed
        .iter()
        .filter_map(|path| {
            graph
                .artifacts
                .iter()
                .find(|artifact| artifact.path == *path)
        })
        .collect::<Vec<_>>();
    let kind = if nodes.len() != changed.len() {
        HmrKind::Reload
    } else if nodes.iter().all(|artifact| artifact.path.ends_with(".css")) {
        HmrKind::Css
    } else if nodes.iter().all(|artifact| artifact.kind == "route") {
        HmrKind::Content
    } else if nodes.iter().all(|artifact| {
        [".js", ".mjs", ".wasm"]
            .iter()
            .any(|extension| artifact.path.ends_with(extension))
    }) {
        HmrKind::Adapter
    } else {
        HmrKind::Reload
    };
    HmrUpdate {
        kind,
        paths: changed.iter().map(|path| format!("/{path}")).collect(),
        routes: affected_routes.iter().cloned().collect(),
    }
}

pub(crate) fn write_rebuild_record(root: &Path, record: &RebuildRecord) -> Result<(), String> {
    validate_rebuild_record(record)?;
    let mut bytes = serde_json::to_vec_pretty(record).map_err(|error| error.to_string())?;
    bytes.push(b'\n');
    if bytes.len() > MAX_REBUILD_RECORD_BYTES {
        return Err(format!(
            "rebuild record exceeds {MAX_REBUILD_RECORD_BYTES} bytes"
        ));
    }
    let path = root.join(REBUILD_RECORD_PATH);
    let parent = path
        .parent()
        .ok_or_else(|| "rebuild record has no parent directory".to_owned())?;
    fs::create_dir_all(parent).map_err(|error| format!("{}: {error}", parent.display()))?;
    let temporary = parent.join(format!("last-rebuild-{}.tmp", std::process::id()));
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&temporary)
        .map_err(|error| format!("{}: {error}", temporary.display()))?;
    let result = (|| {
        file.write_all(&bytes)
            .and_then(|()| file.sync_all())
            .map_err(|error| format!("{}: {error}", temporary.display()))?;
        drop(file);
        if let Ok(metadata) = fs::symlink_metadata(&path) {
            if metadata.file_type().is_symlink() || !metadata.is_file() {
                return Err(format!(
                    "refusing to replace non-regular rebuild record {}",
                    path.display()
                ));
            }
            fs::remove_file(&path).map_err(|error| format!("{}: {error}", path.display()))?;
        }
        fs::rename(&temporary, &path)
            .map_err(|error| format!("{} -> {}: {error}", temporary.display(), path.display()))
    })();
    if result.is_err() {
        let _ = fs::remove_file(&temporary);
    }
    result
}

pub(crate) fn read_rebuild_record(root: &Path) -> Result<RebuildRecord, String> {
    let path = root.join(REBUILD_RECORD_PATH);
    let metadata =
        fs::symlink_metadata(&path).map_err(|error| format!("{}: {error}", path.display()))?;
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(format!("{} is not a regular file", path.display()));
    }
    if metadata.len() > MAX_REBUILD_RECORD_BYTES as u64 {
        return Err(format!(
            "rebuild record exceeds {MAX_REBUILD_RECORD_BYTES} bytes"
        ));
    }
    let bytes = fs::read(&path).map_err(|error| format!("{}: {error}", path.display()))?;
    let record: RebuildRecord =
        serde_json::from_slice(&bytes).map_err(|error| error.to_string())?;
    validate_rebuild_record(&record)?;
    Ok(record)
}

fn validate_rebuild_record(record: &RebuildRecord) -> Result<(), String> {
    if record.record_version != REBUILD_RECORD_VERSION || record.receipt_after.len() != 64 {
        return Err("unsupported or invalid rebuild record".to_owned());
    }
    for items in [
        &record.changed_sources,
        &record.affected_routes,
        &record.affected_artifacts,
        &record.changed_artifacts,
        &record.hmr.paths,
        &record.hmr.routes,
    ] {
        if items.len() > MAX_REBUILD_ITEMS
            || items.windows(2).any(|pair| pair[0] >= pair[1])
            || items.iter().any(|item| item.len() > 4096)
        {
            return Err("rebuild record contains an invalid path set".to_owned());
        }
    }
    Ok(())
}

pub(crate) fn explain_artifact(graph: &BuildGraph, query: &str) -> Result<String, String> {
    let query = query.trim();
    let artifact = if query.starts_with('/') {
        graph
            .routes
            .iter()
            .find(|route| route.route == query)
            .and_then(|route| route.artifacts.first())
            .and_then(|path| {
                graph
                    .artifacts
                    .iter()
                    .find(|artifact| artifact.path == *path)
            })
            .or_else(|| {
                let path = query.trim_start_matches('/');
                graph
                    .artifacts
                    .iter()
                    .find(|artifact| artifact.path == path)
            })
    } else {
        graph
            .artifacts
            .iter()
            .find(|artifact| artifact.path == query)
    }
    .ok_or_else(|| {
        format!("artifact or route {query:?} is not present in the current build graph")
    })?;

    let causal = match &artifact.sources {
        SourceDependencies::AllSources => {
            format!(
                "all {} captured project sources (conservative edge)",
                graph.sources.len()
            )
        }
        SourceDependencies::Explicit { paths } => paths.join(", "),
    };
    let route = artifact
        .route
        .as_deref()
        .map(|route| format!(" via route {route}"))
        .unwrap_or_default();
    Ok(format!(
        "{}{} <- {}\nproducer: {}\nsha256: {}",
        artifact.path, route, causal, artifact.producer, artifact.sha256
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use pliego_artifact::{
        ArtifactReceipt, BuildContext, GraphArtifact, GraphRoute, GraphSource, OutputSet,
        Ownership, ReplacementPolicy,
    };

    fn graph(css: &[u8], html: &[u8]) -> VerifiedGraph {
        let source = GraphSource {
            path: "src/main.rs".to_owned(),
            sha256: pliego_artifact::sha256_bytes(b"source"),
        };
        let dependencies = SourceDependencies::Explicit {
            paths: vec![source.path.clone()],
        };
        let graph = BuildGraph {
            graph_version: pliego_artifact::BUILD_GRAPH_VERSION.to_owned(),
            project_id: "dev-test".to_owned(),
            source_set_sha256: "1".repeat(64),
            sources: vec![source],
            routes: vec![GraphRoute {
                route: "/".to_owned(),
                sources: dependencies.clone(),
                artifacts: vec!["index.html".to_owned()],
            }],
            artifacts: vec![
                GraphArtifact {
                    path: "assets/site.css".to_owned(),
                    kind: "asset".to_owned(),
                    producer: "assets/site.css".to_owned(),
                    route: None,
                    sources: dependencies.clone(),
                    sha256: pliego_artifact::sha256_bytes(css),
                },
                GraphArtifact {
                    path: "index.html".to_owned(),
                    kind: "route".to_owned(),
                    producer: "/".to_owned(),
                    route: Some("/".to_owned()),
                    sources: dependencies,
                    sha256: pliego_artifact::sha256_bytes(html),
                },
            ],
        };
        let context = BuildContext {
            ownership: Ownership {
                project_id: "dev-test".to_owned(),
                site_package: "dev-test".to_owned(),
            },
            framework: pliego_artifact::FrameworkEvidence {
                version: "0.0.1".to_owned(),
                source_revision: "test".to_owned(),
            },
            toolchain: Vec::new(),
            configuration: Vec::new(),
            sources: vec![pliego_artifact::EvidenceFile {
                path: "src/main.rs".to_owned(),
                bytes: 6,
                sha256: pliego_artifact::sha256_bytes(b"source"),
            }],
            materials: Vec::new(),
            source_set_sha256: "1".repeat(64),
            excluded_paths: Vec::new(),
        };
        VerifiedGraph {
            graph,
            report: BuildReport {
                report_version: "test".to_owned(),
                receipt_sha256: pliego_artifact::sha256_bytes(html),
                receipt: ArtifactReceipt {
                    receipt_version: "test".to_owned(),
                    namespace_version: "test".to_owned(),
                    context,
                    replacement_policy: ReplacementPolicy {
                        required_previous_project_id: "dev-test".to_owned(),
                    },
                    previous_ownership: None,
                    outputs: OutputSet {
                        files: Vec::new(),
                        file_count: 0,
                        total_bytes: 0,
                        sha256: pliego_artifact::sha256_bytes(css),
                    },
                },
            },
        }
    }

    #[test]
    fn rebuild_explanation_selects_precise_hmr_kind() {
        let before = graph(b"a", b"home");
        let after = graph(b"b", b"home");
        let record = explain_rebuild(
            7,
            BTreeSet::from(["src/main.rs".to_owned()]),
            Some(&before),
            &after,
        );
        assert_eq!(record.hmr.kind, HmrKind::Css);
        assert_eq!(record.changed_artifacts, ["assets/site.css"]);
        assert_eq!(record.affected_routes, ["/"]);
    }

    #[test]
    fn unknown_build_input_forces_conservative_invalidation() {
        let before = graph(b"a", b"home");
        let after = graph(b"a", b"changed");
        let record = explain_rebuild(
            8,
            BTreeSet::from(["Cargo.toml".to_owned()]),
            Some(&before),
            &after,
        );
        assert_eq!(record.hmr.kind, HmrKind::Content);
        assert_eq!(record.affected_routes, ["/"]);
        assert_eq!(record.affected_artifacts.len(), 2);
    }

    #[test]
    fn content_and_adapter_outputs_select_their_hot_update_protocols() {
        let before = graph(b"same", b"home");
        let content = graph(b"same", b"changed");
        let content_record = explain_rebuild(
            9,
            BTreeSet::from(["src/main.rs".to_owned()]),
            Some(&before),
            &content,
        );
        assert_eq!(content_record.hmr.kind, HmrKind::Content);

        let adapter_graph = |bytes: &[u8]| {
            let mut build = graph(b"same", b"home");
            build.graph.routes.clear();
            build.graph.artifacts = vec![GraphArtifact {
                path: "assets/plugin.js".to_owned(),
                kind: "asset".to_owned(),
                producer: "adapter".to_owned(),
                route: None,
                sources: SourceDependencies::Explicit {
                    paths: vec!["src/main.rs".to_owned()],
                },
                sha256: pliego_artifact::sha256_bytes(bytes),
            }];
            build
        };
        let before_adapter = adapter_graph(b"before");
        let after_adapter = adapter_graph(b"after");
        let adapter_record = explain_rebuild(
            10,
            BTreeSet::from(["src/main.rs".to_owned()]),
            Some(&before_adapter),
            &after_adapter,
        );
        assert_eq!(adapter_record.hmr.kind, HmrKind::Adapter);
    }
}