Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion docs/src/content/docs/cli.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: CLI
description: cigen validate, cigen generate and cigen hash, with every option.
description: cigen validate, cigen generate, cigen hash and cigen explain, with every option.
---

```text
Expand Down Expand Up @@ -101,6 +101,88 @@ cigen hash --pattern 'src/**/*.rs' --pattern Cargo.lock

Patterns that match no files hash to the word `empty`.

## `cigen explain`

Shows what a job's hash covers: each declared input with the files it matches, the inputs cigen derived from the job,
and the hash. Use it to see why a job ran or skipped, or to check that a new job's `inputs` match the files you meant.

```sh
cigen explain test
cigen explain test --format json # the same, as data
```

| Option | What it does |
| ----------------------- | --------------------------------------------------------------------------------- |
| `<job>` | The job's id. |
| `-c`, `--config <path>` | The config. Default: the one in `--base-dir`. |
| `--base-dir <dir>` | The project root, where input patterns and step paths are looked up. Default `.`. |
| `--format <format>` | `text` (the default) or `json`. |
| `--matrix <json>` | For a job that runs GitHub's own matrix: one entry, to show that entry's hash. |

For this config:

```yaml
# cigen.yml
provider: github
input_groups:
rust:
- 'src/**/*.rs'
- Cargo.toml
- Cargo.lock
workflows:
ci:
jobs:
test:
image: rust:1.85
inputs:
- '@rust'
- scripts/test.sh
steps:
- run: ./scripts/test.sh
```

```text
$ cigen explain test
Job `test` in workflow `ci`

Declared inputs:
@rust
Cargo.lock (1 file)
Cargo.lock
Cargo.toml (1 file)
Cargo.toml
src/**/*.rs (1 file)
src/main.rs
scripts/test.sh (1 file)
scripts/test.sh

Derived inputs:
Cargo.lock (lockfile)
rust-toolchain (toolchain pin)
scripts/test.sh (named in a step)
**/package.json (every package.json, no files)

Also hashed:
cigen 0.1.0
the job's definition (workflows.ci.jobs.test)
the settings of workflow `ci`

Hash: 843bac1cb1ef04feb36b3f4f95fa1d00cf1fbacab3372cbead8d85ad403c4d19
```

A declared pattern that matches no files shows `(no files)`, which usually means a typo. Derived inputs are the files
cigen adds on its own (see [derived inputs](/docs/inputs/#derived-inputs)); commands and services the job uses are
listed by their config path, and patterns cigen adds (every `package.json`, and the files a job's modules contribute)
say which. A job that `needs` others lists them under `Needs` with their own hashes, which are part of its hash, so a
change upstream makes it run too. The hash is the one `cigen hash --job test` prints, and the one the generated workflow
looks up while it's up to date with the config. A job that runs GitHub's own matrix hashes each entry in, so each entry
has its own hash: explain says so, and `--matrix '{"rust": "nightly"}'` shows one entry's hash.

With `--format json`, it prints one object with `job`, `workflow`, `inputs` (each `entry` with its `patterns` and their
`files`), `derived` (`config` paths, and `files` and `patterns`, each with a `reason`), `needs` (each `job` with its
`hash`), `also_hashed`, `runs_matrix`, `matrix_entry` (the entry given, or `null`) and `hash`. An unknown job is an
error that suggests the closest id.

## Environment

| Variable | What it does |
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ what to write instead.
- `cigen validate`, which lists every problem in a config, as text or JSON.
- `cigen generate` for GitHub Actions and CircleCI.
- `cigen hash`, which computes a job's hash from its inputs and the inputs cigen derives.
- `cigen explain`, which shows what a job's hash covers: each input, the files it matches, and the derived inputs.
- Skipping on GitHub Actions, using markers in the GitHub Actions cache.
- Deterministic output: generating twice gives the same bytes.

Expand Down
6 changes: 3 additions & 3 deletions docs/src/content/docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ right. This page lists what's planned. Nothing on it exists yet; what exists is
already passed never start a runner, and a final job counts "skipped" as passed for required checks.
3. **Pluggable pass-record stores.** The GitHub Actions cache by default, self-hosted stores such as Redis and S3, and an
open protocol. If a store can't be reached, everything runs.
4. **Agent tools.** `cigen plan`, `cigen explain <job>` (why a job will run or skip) and `cigen expand <job>`. (The
[JSON schemas](/docs/configuration/#json-schemas) and [`cigen validate`](/docs/cli/#cigen-validate), with errors as
JSON, are already built.)
4. **Agent tools.** `cigen plan` (which jobs will run or skip, and why) and `cigen expand <job>`. (The
[JSON schemas](/docs/configuration/#json-schemas), [`cigen validate`](/docs/cli/#cigen-validate) with errors as JSON,
and [`cigen explain`](/docs/cli/#cigen-explain), which shows what a job's hash covers, are already built.)
5. **Prebuilt binaries** and Docker images with cigen installed. Generated workflows already install the release that
generated them; v0.2.0 will be the first release with the archives they download.
6. **Hosted pass records on VisualCI.** A free account, signed in with GitHub, with records visible across branches,
Expand Down
3 changes: 2 additions & 1 deletion docs/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
<li><code>cigen validate</code>, with errors as JSON for agents</li>
<li>GitHub Actions and CircleCI generation</li>
<li><code>cigen hash</code> with derived inputs</li>
<li><code>cigen explain</code>: what a job's hash covers</li>
<li>Skipping on GitHub Actions, with markers in its cache</li>
<li>Generated workflows install a checksum-checked cigen release</li>
<li>Byte-for-byte deterministic output</li>
Expand All @@ -205,7 +206,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
<li>The v0.2.0 release, the first those workflows can install</li>
<li>A planner job, so skipped jobs never start a runner</li>
<li>More modules: Node, Playwright, services and caches</li>
<li><code>cigen plan</code>, <code>explain</code> and <code>expand</code> for agents</li>
<li><code>cigen plan</code> and <code>expand</code> for agents</li>
<li>Free hosted pass records on VisualCI, shared across branches</li>
<li>Skipping on CircleCI</li>
</ul>
Expand Down
20 changes: 17 additions & 3 deletions internal_docs/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ What the code does today, checked against the code in December 2026. Where cigen
the config and exits non-zero with every problem it found; `--format json` prints
`{valid, config, errors: [{file, line, path, message, suggestion}]}` on stdout. Every command prints config errors
the same way (`src/commands/report.rs`).
`cigen explain <job> [--config <path>] [--base-dir <dir>] [--format text|json] [--matrix <json>]` shows what the job's
hash covers: its `inputs` with `@groups` expanded and the files each pattern matched, the derived inputs (config parts
by label, files with their reason: toolchain pin, lockfile or named in a step, and patterns with theirs: from a module
or every `package.json`, with the files they match), the jobs it `needs` with their hashes, what else is hashed, and
the hash. Job hashing lives in `src/hashing/`, and `hash --job` and `explain` both call `hashing::hash_job`, so the
two always agree. Without `--modules-digest`, `hash --job` uses the modules digest this cigen computes, so a local
hash equals the CI step's for a config that hasn't changed since it was generated. The jobs a job needs are hashed the
same way, with this cigen's modules digest, so a job that needs a job with modules got a new hash in CI with #14 (its
generated step passes no `--modules-digest` for the needed job): a one-time rerun of those jobs, never a skip. For a
job that runs GitHub's matrix, explain says each entry is hashed in, and with `--matrix` it shows that entry's hash
(`src/hashing/matrix.rs` mixes the entry into the job's hash, for `hash --matrix` too).
- **Config loading** (`src/config/`, `src/schema/`): one schema in two layouts. `cigen.yml` is the whole document;
`.cigen/` is the same document split into files (`config.yml` and `config/*.yml` for the top level,
`commands/<name>.yml`, `workflows/<name>.yml` or `workflows/<name>/config.yml` for a workflow's settings, and
Expand Down Expand Up @@ -44,7 +55,7 @@ What the code does today, checked against the code in December 2026. Where cigen
top level allows `$schema`. `tests/json_schema.rs`
fails when they're stale (refresh with `CIGEN_UPDATE_GOLDEN=1 cargo test --test json_schema`) and checks every
config in the repository against them.
- **Derived inputs** (`src/inputs.rs`, `src/commands/needs_hash.rs`): `cigen hash --job` adds, on top of a job's
- **Derived inputs** (`src/inputs.rs`, `src/hashing/needs.rs`): `cigen hash --job` adds, on top of a job's
`inputs`, the hashes of the jobs it `needs` (transitively, so a changed build reruns the tests that need it), the
commands it uses (through other commands too), the services it names, the top-level provider blocks, every project
file its steps name (`sh scripts/check.sh`, `require('./x.js')`, every file of a local `uses: ./action`), and the root
Expand Down Expand Up @@ -132,6 +143,9 @@ What the code does today, checked against the code in December 2026. Where cigen
- `tests/git_ownership.rs`: `cigen hash` in a checkout git treats as owned by another user
(`GIT_TEST_ASSUME_DIFFERENT_OWNER`), at the repository root and in a subdirectory, matches the hash computed as its
owner.
- `tests/explain.rs`: `cigen explain` lists declared and derived inputs (module inputs included) with their files and
the jobs a job needs, in JSON and text, and its hash equals `cigen hash --job`'s before and after an input, a module
input or a needed job changes, and for a matrix entry.
- `tests/derived_inputs.rs`: changing a script a step runs, a command the job uses, a toolchain pin or the
`package.json` script that `npm run test` runs (the root's or a sub-package's) makes the job run in mockci, and changing a used command, a named service, a top-level provider block, a local action, a lockfile or the
`packageManager` pin changes the job's hash; an unused command or service doesn't.
Expand All @@ -141,8 +155,8 @@ What the code does today, checked against the code in December 2026. Where cigen
`cigen generate`; examples of errors must fail with the error they show. Every file it reads must be an input of
cigen's own `test` job.
- `tests/module_scenarios.rs`: a job using the `rust` module installs nothing when it skips, and reruns in mockci when
a file the module contributes or its parameters change; the generated `--modules-digest` changes the hash; unknown
modules and parameters fail generation with a fix.
a file the module contributes or its parameters change; the generated `--modules-digest` changes the hash, and a
local `hash --job` without it gives the CI step's hash; unknown modules and parameters fail generation with a fix.
- `tests/module_catalogue.rs`: every registered module has a skill with frontmatter, a README documenting exactly its
parameters, a golden fixture (`integration_tests/module_<name>/`) for each provider it supports that mockci's parser
loads, and actions pinned to commit SHAs; the index is generated from the registry
Expand Down
123 changes: 123 additions & 0 deletions src/commands/explain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//! `cigen explain <job>`: what a job's hash covers, and the hash itself.

use super::hash::job_hash;
use super::report::{OutputFormat, print_json};
use anyhow::Result;
use cigen::hashing::{JobHash, PatternFiles};
use clap::Args;
use std::path::PathBuf;

#[derive(Debug, Args)]
pub struct ExplainArgs {
/// The job's id
pub job: String,

/// The config: a cigen.yml file or a .cigen/ directory (found in --base-dir by default)
#[arg(short, long)]
pub config: Option<PathBuf>,

/// The project root, where input patterns and step paths are looked up
#[arg(long = "base-dir", default_value = ".")]
pub base_dir: PathBuf,

/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
pub format: OutputFormat,

/// A matrix entry as JSON, for a job that runs GitHub's own matrix: the hash is that entry's,
/// as its CI hash step computes it
#[arg(long)]
pub matrix: Option<String>,
}

pub fn explain_command(args: ExplainArgs) -> Result<()> {
let explained = job_hash(
&args.base_dir,
args.config.as_deref(),
None,
&args.job,
None,
args.matrix.as_deref(),
)?;
match args.format {
OutputFormat::Json => print_json(&explained),
OutputFormat::Text => {
print!("{}", render(&explained));
Ok(())
}
}
}

fn render(job: &JobHash) -> String {
let mut out = format!("Job `{}` in workflow `{}`\n", job.job, job.workflow);

out.push_str("\nDeclared inputs:\n");
if job.inputs.is_empty() {
out.push_str(" (none: the job has no `inputs`, so it never skips)\n");
}
for input in &job.inputs {
let grouped = input.entry.starts_with('@');
if grouped {
out.push_str(&format!(" {}\n", input.entry));
}
let indent = if grouped { " " } else { " " };
for pattern in &input.patterns {
push_pattern(&mut out, indent, pattern, "");
}
}

out.push_str("\nDerived inputs:\n");
let derived = &job.derived;
if derived.config.is_empty() && derived.files.is_empty() && derived.patterns.is_empty() {
out.push_str(" (none)\n");
}
for label in &job.derived.config {
out.push_str(&format!(" {label}\n"));
}
for file in &job.derived.files {
out.push_str(&format!(" {} ({})\n", file.path, file.reason));
}
for derived in &job.derived.patterns {
let pattern = PatternFiles {
pattern: derived.pattern.clone(),
files: derived.files.clone(),
};
push_pattern(&mut out, " ", &pattern, &format!("{}, ", derived.reason));
}

if !job.needs.is_empty() {
out.push_str("\nNeeds (each job's own hash):\n");
for need in &job.needs {
out.push_str(&format!(" {} {}\n", need.job, need.hash));
}
}

out.push_str("\nAlso hashed:\n");
for item in &job.also_hashed {
out.push_str(&format!(" {item}\n"));
}
if let Some(entry) = &job.matrix_entry {
out.push_str(&format!(" the matrix entry {entry}\n"));
}
out.push_str(&format!("\nHash: {}\n", job.hash));
if job.runs_matrix && job.matrix_entry.is_none() {
out.push_str(
"\nThe job runs GitHub's matrix, and each entry is hashed in, so each entry's hash differs \
from this one.\nPass --matrix '<entry as JSON>' to see an entry's hash.\n",
);
}
out
}

/// A pattern with how many files it matches, then the files.
fn push_pattern(out: &mut String, indent: &str, pattern: &PatternFiles, note: &str) {
let count = match pattern.files.len() {
0 => "no files".to_string(),
1 => "1 file".to_string(),
n => format!("{n} files"),
};
out.push_str(&format!("{indent}{} ({note}{count})\n", pattern.pattern));
for file in &pattern.files {
out.push_str(&format!("{indent} {file}\n"));
}
}
Loading
Loading