local-ci 0.20.0

Run the GitHub Actions jobs a branch would trigger, locally, in an isolated worktree, and print a short report
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
//! Execute an individual frozen task from immutable captured input.
use crate::{
    context::ExecContext,
    exec::{Opts, Runner, Workspace},
    frozen::FrozenPlan,
    setup::{PreparedReceipt, PreparedSetup, SetupRecipe, TemplateContext, TemplateKey},
    source::{ObjectStore, SourceManifest},
    task::{TaskInputs, TaskResult},
};
use anyhow::{bail, Context, Result};
mod evaluation;
pub(crate) use evaluation::GitWorkflowReader;
pub use evaluation::{
    git_evaluation_source, verify_git_evaluation_source, EvaluationBinding, EvaluationSource,
    JobScheduling, VerifiedEvaluationSource, WorkflowScheduling,
};
mod receipt;
pub use receipt::{AttemptBinding, PrivateReceiptDirectory};
use std::path::Path;
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};
static WORKSPACE_SEQUENCE: AtomicUsize = AtomicUsize::new(0);

fn new_namespace() -> Result<String> {
    let nonce = tempfile::Builder::new().prefix("pinned-").tempdir()?;
    Ok(format!(
        "{}-{}-{}",
        nonce
            .path()
            .file_name()
            .unwrap()
            .to_string_lossy()
            .to_ascii_lowercase(),
        std::process::id(),
        WORKSPACE_SEQUENCE.fetch_add(1, Ordering::Relaxed)
    ))
}

/// Host-owned independent matrix workspaces. Implementations restore secrets
/// separately, preserve validated setup contents, and remove only owned children.
/// They must never clone a live caller checkout or share mutable installs.
pub trait PinnedWorkspaceProvider: Sync {
    fn fork(&self, parent: &Path, child: &Path) -> Result<()>;
    fn remove(&self, child: &Path) -> Result<()>;
}

/// An owned source materialization. Its root cannot be retargeted through this API.
/// Hosts may restore secrets only after materialization, before executing setup.
pub struct PinnedWorkspace {
    workspace: Workspace,
    plan_digest: String,
    root: std::path::PathBuf,
    device: u64,
    inode: u64,
    pub(crate) namespace: String,
    pub(crate) sequence: Arc<AtomicUsize>,
    unverified: Vec<crate::source::RelPath>,
}

/// What a host placed in a workspace before binding it, and so asks
/// [`PinnedExecution::verify_workspace_with_overlay`] not to compare with the
/// captured source. Paths are relative to the workspace root.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WorkspaceOverlay {
    /// Files or directories: the output of a prepared setup, dependency
    /// directories copied in. Everything below a directory is left out.
    pub paths: Vec<crate::source::RelPath>,
    /// Restored secret files. Each must be one the manifest declares.
    pub secret_files: Vec<crate::source::RelPath>,
}

impl PinnedWorkspace {
    pub fn workspace(&self) -> &Workspace {
        &self.workspace
    }
    /// The paths that were left out when this workspace was verified, sorted:
    /// what [`WorkspaceOverlay`] declared. Empty for a workspace that was
    /// materialized, verified whole, or restored from a receipt.
    pub fn unverified_paths(&self) -> &[crate::source::RelPath] {
        &self.unverified
    }
    pub(crate) fn validate(&self, input: &PinnedExecution<'_>) -> Result<()> {
        use std::os::unix::fs::MetadataExt;
        let root = std::fs::canonicalize(&self.workspace.root)?;
        let metadata = std::fs::metadata(&root)?;
        if self.plan_digest != input.plan.digest
            || root != self.root
            || metadata.dev() != self.device
            || metadata.ino() != self.inode
        {
            bail!("captured workspace binding mismatch");
        }
        Ok(())
    }
}

/// Capability binding a frozen plan to its verified captured source objects.
pub struct PinnedExecution<'a> {
    pub(crate) plan: &'a FrozenPlan,
    manifest: &'a SourceManifest,
    store: &'a ObjectStore,
}
impl<'a> PinnedExecution<'a> {
    pub fn new(
        plan: &'a FrozenPlan,
        manifest: &'a SourceManifest,
        store: &'a ObjectStore,
    ) -> Result<Self> {
        let input = Self {
            plan,
            manifest,
            store,
        };
        input.validate_binding(plan)?;
        crate::source::check_manifest_digest(manifest)?;
        crate::source::check_materialization_graph(store, manifest)?;
        Ok(input)
    }
    /// Materialize into a fresh host-owned root. No secret files are restored.
    /// Root ownership and lifecycle remain with the host; setup mutations are
    /// allowed, but replacing the root directory invalidates this capability.
    pub fn materialize_workspace(&self, workspace: Workspace) -> Result<PinnedWorkspace> {
        self.validate(self.plan)?;
        if !matches!(
            self.manifest.source_kind,
            crate::source::SourceKind::Directory
        ) {
            bail!("Git source requires source-aware Git materialization");
        }
        if workspace.root.exists() {
            bail!("captured workspace root must be fresh")
        }
        use std::os::unix::fs::DirBuilderExt;
        if let Some(parent) = workspace.root.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::DirBuilder::new()
            .mode(0o700)
            .create(&workspace.root)?;
        crate::source::materialize_from(self.store, self.manifest, &workspace.root)?;
        self.bind_verified_workspace(workspace)
    }

    /// Bind a workspace provided by the trusted host lifecycle, before secrets
    /// or setup. HEAD/index semantics come from the source kind, not a caller
    /// assertion: worktree captures preserve original HEAD and captured index.
    pub fn verify_workspace(
        &self,
        ctx: &ExecContext,
        workspace: Workspace,
    ) -> Result<PinnedWorkspace> {
        self.validate(self.plan)?;
        crate::source::verify_materialized_from(self.store, self.manifest, &workspace.root)?;
        self.verify_git(ctx, &workspace.root, false)?;
        self.bind_verified_workspace(workspace)
    }

    /// [`Self::verify_workspace`] for a workspace the host has already put
    /// secrets, setup output or dependencies into. Only what `overlay`
    /// declares, and what lies below it, is left out; every other byte must
    /// match the captured source, and the Git HEAD and index are checked as
    /// before. The declared paths are refused when they are not normalized
    /// relative paths, are or lead through a symbolic link, touch a submodule,
    /// or name a secret file the manifest does not declare. The bound
    /// workspace records them ([`PinnedWorkspace::unverified_paths`]).
    ///
    /// The content of the declared paths is the host's word: nothing here
    /// checks it, and steps run with it.
    pub fn verify_workspace_with_overlay(
        &self,
        ctx: &ExecContext,
        workspace: Workspace,
        overlay: &WorkspaceOverlay,
    ) -> Result<PinnedWorkspace> {
        self.validate(self.plan)?;
        let unverified = crate::source::check_unverified_paths(
            self.manifest,
            &workspace.root,
            &overlay.paths,
            &overlay.secret_files,
        )?;
        crate::source::verify_materialized_except(
            self.store,
            self.manifest,
            &workspace.root,
            &unverified,
        )?;
        self.verify_git(ctx, &workspace.root, false)?;
        let mut bound = self.bind_verified_workspace(workspace)?;
        bound.unverified = unverified;
        Ok(bound)
    }

    /// SDK-owned legacy Git materialization: HEAD is the execution commit and
    /// the index is its tree, matching ordinary local-ci worktree execution.
    /// `workspace.repo_root` must already contain the imported captured commit.
    pub fn materialize_git_workspace(
        &self,
        ctx: &ExecContext,
        workspace: Workspace,
    ) -> Result<PinnedWorkspace> {
        self.validate(self.plan)?;
        if matches!(
            self.manifest.source_kind,
            crate::source::SourceKind::Directory
        ) {
            bail!("directory source has no Git materialization");
        }
        if std::fs::symlink_metadata(&workspace.root).is_ok() {
            bail!("captured workspace root must be fresh");
        }
        crate::source::add_worktree(
            ctx,
            self.store,
            &workspace.repo_root,
            &workspace.root,
            self.manifest,
        )?;
        crate::source::verify_materialized_from(self.store, self.manifest, &workspace.root)?;
        self.verify_git(ctx, &workspace.root, true)?;
        self.bind_verified_workspace(workspace)
    }

    fn verify_git(&self, ctx: &ExecContext, root: &Path, execution_snapshot: bool) -> Result<()> {
        use crate::source::SourceKind;
        let m = self.manifest;
        if matches!(m.source_kind, SourceKind::Directory) {
            if std::fs::symlink_metadata(root.join(".git")).is_ok() {
                bail!("directory input cannot claim a Git checkout");
            }
            return Ok(());
        }
        let oid = |value: Option<&str>| -> Result<String> {
            let value = value.context("captured Git source lacks required HEAD/index identity")?;
            if value.len() != 40 || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
                bail!("invalid captured Git object identity");
            }
            Ok(value.to_string())
        };
        let head = if execution_snapshot || matches!(m.source_kind, SourceKind::Commit) {
            oid(m.execution_commit.as_deref())?
        } else {
            oid(m.original_head.as_deref())?
        };
        let tree = if execution_snapshot || matches!(m.source_kind, SourceKind::Commit) {
            format!("{head}^{{tree}}")
        } else {
            oid(m.index_tree.as_deref())?
        };
        if std::fs::canonicalize(crate::git::toplevel(ctx, root)?)? != std::fs::canonicalize(root)?
            || crate::git::git(ctx, root, &["rev-parse", "--verify", "HEAD"])? != head
        {
            bail!("captured workspace Git HEAD/root mismatch");
        }
        crate::git::git(
            ctx,
            root,
            &["diff-index", "--cached", "--quiet", &tree, "--"],
        )?;
        Ok(())
    }

    fn bind_verified_workspace(&self, mut workspace: Workspace) -> Result<PinnedWorkspace> {
        use std::os::unix::fs::MetadataExt;
        let root = std::fs::canonicalize(&workspace.root)?;
        let metadata = std::fs::metadata(&root)?;
        if !metadata.is_dir() {
            bail!("captured workspace root is not a directory");
        }
        if [
            &workspace.root,
            &workspace.repo_root,
            &workspace.run_dir,
            &workspace.tools,
            &workspace.lock_file,
        ]
        .iter()
        .any(|path| !path.is_absolute())
        {
            bail!("captured workspace paths must be absolute");
        }
        let namespace = new_namespace()?;
        workspace.tools = workspace.tools.join(&namespace);
        Ok(PinnedWorkspace {
            namespace,
            sequence: Arc::new(AtomicUsize::new(0)),
            workspace,
            plan_digest: self.plan.digest.clone(),
            root,
            device: metadata.dev(),
            inode: metadata.ino(),
            unverified: vec![],
        })
    }

    pub(crate) fn validate(&self, plan: &FrozenPlan) -> Result<()> {
        plan.verify()?;
        if plan.digest != self.plan.digest {
            bail!("captured execution plan mismatch");
        }
        Ok(())
    }

    fn validate_binding(&self, plan: &FrozenPlan) -> Result<()> {
        self.validate(plan)?;
        let Some(p) = &plan.source.pinned else {
            bail!("captured execution requires a pinned plan")
        };
        let m = self.manifest;
        if plan.digest != self.plan.digest
            || plan.source.source_id != m.source_id
            || p.manifest_digest != m.manifest_digest
            || p.kind != m.source_kind
            || p.repository_id != m.repository_id
            || p.cwd_relative != m.cwd_relative
            || plan.source.execution_commit != m.execution_commit
            || plan.source.base != m.base
            || plan.branch != m.branch
            || m.secret_files
                .iter()
                .any(|s| m.object_manifest.entries.iter().any(|e| e.path == s.path))
        {
            bail!("captured execution identity mismatch");
        }
        // Blob/tree verification belongs to actual materialization, once per root.
        Ok(())
    }
}

/// Refuse nested workspaces unless a host supplies an explicit lifecycle.
/// This also prevents accidental secret-bearing recursive copies.
pub struct RefusePinnedForks;
impl PinnedWorkspaceProvider for RefusePinnedForks {
    fn fork(&self, _: &Path, _: &Path) -> Result<()> {
        bail!("captured matrix fork requires a host workspace and secret provider")
    }
    fn remove(&self, _: &Path) -> Result<()> {
        Ok(())
    }
}

pub fn execute_pinned_setup(
    ctx: &ExecContext,
    input: &PinnedExecution<'_>,
    recipe: &SetupRecipe,
    key: &TemplateKey,
    ws: &PinnedWorkspace,
    opts: Opts,
    workspaces: &dyn PinnedWorkspaceProvider,
) -> Result<PreparedSetup> {
    input.validate(input.plan)?;
    ws.validate(input)?;
    crate::wire::check_version("setup recipe", recipe.schema_version)?;
    if crate::setup::derive_setup_recipe(input.plan, &recipe.task)? != *recipe {
        bail!("setup recipe does not belong to the frozen plan");
    }
    let runner = Runner::new_pinned(ctx, input, ws, opts, workspaces)?;
    let result = (|| {
        runner.prepare_tools()?;
        runner.run_recipe(recipe, key.digest())
    })();
    ctx.cancel.remove_containers(ctx);
    result
}

#[allow(clippy::too_many_arguments)]
pub fn execute_pinned_task(
    ctx: &ExecContext,
    input: &PinnedExecution<'_>,
    task_id: &str,
    inputs: &TaskInputs,
    prepared: Option<(&PreparedReceipt, &TemplateContext)>,
    ws: &PinnedWorkspace,
    opts: Opts,
    workspaces: &dyn PinnedWorkspaceProvider,
) -> Result<TaskResult> {
    input.validate(input.plan)?;
    ws.validate(input)?;
    let runner = Runner::new_pinned(ctx, input, ws, opts, workspaces)?;
    crate::task::execute_with_runner(
        ctx,
        input.plan,
        task_id,
        inputs,
        prepared,
        ws.workspace(),
        runner,
    )
}

/// Execute only a recomputed member of the original captured dynamic task.
/// Host must bind dispatch/inputs/facts to its committed readiness/attempt.
#[allow(clippy::too_many_arguments)]
pub fn execute_pinned_dispatch(
    ctx: &ExecContext,
    input: &PinnedExecution<'_>,
    dispatch: &crate::driver::DerivedDispatch,
    inputs: &TaskInputs,
    facts: &crate::driver::PortableFacts,
    prepared: Option<(&PreparedReceipt, &TemplateContext)>,
    ws: &PinnedWorkspace,
    opts: Opts,
    workspaces: &dyn PinnedWorkspaceProvider,
) -> Result<TaskResult> {
    input.validate(input.plan)?;
    ws.validate(input)?;
    let source = input.evaluation_source()?;
    let selection =
        crate::driver::dispatch::validate(input.plan, &source, facts, dispatch, inputs)?;
    let task = selection.task.clone();
    let runner = Runner::new_pinned(ctx, input, ws, opts, workspaces)?
        .select_derived(selection, facts.clone());
    crate::task::execute_resolved_with_runner(ctx, &task, inputs, prepared, ws.workspace(), runner)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn retained_proof_rejects_public_runner_plan_retargeting_at_every_entry() {
        let area = tempfile::tempdir().unwrap();
        let source = area.path().join("source");
        std::fs::create_dir_all(source.join(".github/workflows")).unwrap();
        std::fs::write(
            source.join(".github/workflows/ci.yml"),
            "on: push\njobs:\n  test:\n    runs-on: x\n    steps:\n      - run: npm ci\n",
        )
        .unwrap();
        let store = ObjectStore::open(area.path().join("store")).unwrap();
        let ctx = ExecContext::new(Default::default());
        let manifest = crate::source::capture_directory(
            &ctx,
            &source,
            &crate::source::CaptureOptions {
                store: Some(store.root().into()),
                ..Default::default()
            },
        )
        .unwrap()
        .manifest;
        let plan = crate::build_frozen_plan_from_source(
            &store,
            &manifest,
            &crate::PinnedPlanOptions {
                filters: crate::frozen::Filters {
                    all: false,
                    include_disabled: true,
                    workflows_dirs: vec![],
                    workflows: vec![],
                    jobs: vec![],
                },
                workflow_dirs_override: None,
                disabled: vec![],
                base_branch: None,
                changed_files: None,
            },
        )
        .unwrap();
        let input = PinnedExecution::new(&plan, &manifest, &store).unwrap();
        let workspace = input
            .materialize_workspace(Workspace {
                root: area.path().join("root"),
                repo_root: area.path().join("no-repo"),
                run_dir: area.path().join("run"),
                tools: area.path().join("tools"),
                lock_file: area.path().join("lock"),
            })
            .unwrap();
        let mut legacy = plan.clone();
        legacy.schema_version = 6;
        legacy.source.pinned = None;
        legacy.source.execution_commit = Some("a".repeat(40));
        legacy.source.base = Some("main".into());
        let opts = Opts {
            parallel: 1,
            traffic_controller: None,
            executor: None,
            portool: None,
            containers: 1,
            job_timeout_min: 1.,
            keep: false,
            trash: area.path().join("trash"),
            all_os_legs: false,
        };
        let provider = RefusePinnedForks;
        let mut runner = Runner::new_pinned(&ctx, &input, &workspace, opts, &provider).unwrap();
        runner.fp = &legacy;
        let recipe = crate::setup::derive_setup_recipe(&plan, &plan.tasks[0].id).unwrap();
        assert!(runner.prepare_tools().is_err());
        assert!(runner.run_recipe(&recipe, "key").is_err());
        assert!(runner.run_units().is_err());
        assert!(runner.release_ports(&workspace.workspace().root).is_err());
        assert!(runner
            .run_task(
                &workspace.workspace().root,
                &plan.tasks[0],
                &Default::default(),
                None,
                true,
                false,
                false
            )
            .is_err());
        assert!(!workspace.workspace().tools.exists());
    }
    #[test]
    fn replaced_workspace_at_final_cleanup_retains_actual_result_and_port_reference() {
        struct ReplaceRoot;
        impl crate::StepExecutor for ReplaceRoot {
            fn run(&self, req: &crate::StepRequest) -> Result<crate::StepExit> {
                std::fs::rename(&req.cwd, req.cwd.with_extension("original"))?;
                std::fs::create_dir(&req.cwd)?;
                Ok(crate::StepExit {
                    exit: crate::exec::Exit::Code(0),
                    run_secs: 0.,
                    queue_wait_secs: None,
                })
            }
        }
        let area = tempfile::tempdir().unwrap();
        let source = area.path().join("source");
        std::fs::create_dir_all(source.join(".github/workflows")).unwrap();
        std::fs::write(
            source.join(".github/workflows/ci.yml"),
            "on: push\njobs:\n  test:\n    runs-on: x\n    steps:\n      - run: echo completed\n",
        )
        .unwrap();
        let store = ObjectStore::open(area.path().join("store")).unwrap();
        let ctx = ExecContext::new(Default::default());
        let manifest = crate::source::capture_directory(
            &ctx,
            &source,
            &crate::source::CaptureOptions {
                store: Some(store.root().into()),
                ..Default::default()
            },
        )
        .unwrap()
        .manifest;
        let plan = crate::build_frozen_plan_from_source(
            &store,
            &manifest,
            &crate::PinnedPlanOptions {
                filters: crate::frozen::Filters {
                    all: false,
                    include_disabled: true,
                    workflows_dirs: vec![],
                    workflows: vec![],
                    jobs: vec![],
                },
                workflow_dirs_override: None,
                disabled: vec![],
                base_branch: None,
                changed_files: None,
            },
        )
        .unwrap();
        let input = PinnedExecution::new(&plan, &manifest, &store).unwrap();
        let workspace = input
            .materialize_workspace(Workspace {
                root: area.path().join("root"),
                repo_root: area.path().join("no-repo"),
                run_dir: area.path().join("run"),
                tools: area.path().join("tools"),
                lock_file: area.path().join("lock"),
            })
            .unwrap();
        let opts = Opts {
            parallel: 1,
            traffic_controller: None,
            executor: Some(Arc::new(ReplaceRoot)),
            portool: Some(area.path().join("never-invoked-portool")),
            containers: 1,
            job_timeout_min: 1.,
            keep: false,
            trash: area.path().join("trash"),
            all_os_legs: false,
        };
        let provider = RefusePinnedForks;
        let runner = Runner::new_pinned(&ctx, &input, &workspace, opts, &provider).unwrap();
        let outcome = crate::task::execute_resolved_outcome(
            &ctx,
            &plan.tasks[0],
            &TaskInputs::default(),
            None,
            workspace.workspace(),
            runner,
        );
        assert_eq!(
            outcome.execution.as_ref().unwrap().outcomes[0].status,
            crate::JobStatus::Success
        );
        assert!(outcome.final_error.is_some());
        assert_eq!(
            outcome.cleanup.pending.port_paths,
            vec![workspace.workspace().root.clone()]
        );
        assert!(
            crate::execution_outcome::retained_result(&outcome.legacy_result().unwrap_err())
                .is_some()
        );
    }
}