Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.integration.validate

Requirements index · View definition

evidence Valid

Requirement

habeas::validate() MUST check the current Cargo and mdBook project and make its Cargo test fail with all validation errors when any requirement evidence is invalid. Its failure message MUST instruct the agent to run habeas author --all, address the reported gaps, and rerun tests. Validation MUST remain deterministic and MUST NOT invoke authoring itself. cite requirements.authoring.batch cite requirements.authoring.deterministic_validation

Rationale

The tests in tests/validate.rs cover each obligation in requirements.integration.validate. The test function and helper names below are all in that file.

How the tests run: validate_child calls habeas::validate() only when the child env var is set. validate_in re-runs the test binary as a child process in a fixture directory. That makes the test’s own failure, exit status and message observable.

Obligations and the tests that check them:

  • Check the current Cargo and mdBook project.
    • validate_fails_the_test_with_every_error_and_authoring_instructions starts validation from the nested directory src/nested. It must find the project root, and it does.
    • validate_fails_outside_a_cargo_and_mdbook_project has a Cargo.toml but no book.toml. It asserts that the child fails with “cannot find Cargo and mdBook project root”.
  • Make the Cargo test fail when any evidence is invalid. The fixture has two invalid cases at once: rules.ready has no evidence file, and rules.broken has malformed JSON. The test asserts the child exits unsuccessfully and the message contains “Habeas validation failed”.
  • Report all validation errors. The same message must name both rules.ready and evidence/rules.broken.json. A validator that stopped at the first error would fail this assertion.
  • Failure message instructs the agent. The test asserts the message contains “Run habeas author --all” and “address any reported gaps and rerun the tests”. That covers all three instructions.
  • Validation MUST NOT invoke authoring. After the failure, the test asserts the evidence directory still holds only the original file and the Habeas state directory is empty. So no evidence or usage history was written. The environment is redirected to a temporary state directory with no credentials.
  • Deterministic. The test runs validation a second time from the project root. It asserts the sorted list of - error lines is identical to the first run.
  • Passing case. After the fixture is made valid (a correct reference digest), the child succeeds. No coverage data is created, so the success path is also checked.

Cited requirements: requirements.authoring.deterministic_validation says missing or stale evidence must stay a failure and must not trigger authoring. The missing-evidence and malformed-evidence cases, plus the check that nothing is written, cover that in the subject’s setting. The cited requirements.authoring.batch is context only.

Stale evidence is not exercised as a separate case in this file. The subject only requires failing “when any requirement evidence is invalid” under the existing rules, and two invalid classes plus the success case already check that. I report no gap.

Inputs

Tests

Input 4: outline tests/validate.rs fn[validate_child]
/// Runs `habeas::validate()` when started as the child process; otherwise
/// does nothing, so the ordinary test run ignores it.
// {test}`requirements.integration.validate`
#[test]
fn validate_child() {
    if std::env::var_os(CHILD).is_some() {
        habeas::validate();
    }
}
Input 5: outline tests/validate.rs fn[validate_fails_the_test_with_every_error_and_authoring_instructions]
  • File: tests/validate.rs
  • Selector: fn[validate_fails_the_test_with_every_error_and_authoring_instructions]
  • Kind: fn
  • tests/validate.rs:43–121
// {test}`requirements.integration.validate`
// {test}`requirements.test-coverage.check.guidance`
#[test]
fn validate_fails_the_test_with_every_error_and_authoring_instructions() {
    let temp = TempDir::new().unwrap();
    let root = temp.path().join("project");
    let state = temp.path().join("state");
    fs::create_dir_all(root.join("md")).unwrap();
    fs::create_dir_all(root.join("src/nested")).unwrap();
    fs::create_dir_all(root.join("evidence")).unwrap();
    fs::create_dir_all(&state).unwrap();
    fs::write(root.join("Cargo.toml"), "[package]\nname = \"fixture\"\n").unwrap();
    fs::write(root.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
    fs::write(
        root.join("md/rules.md"),
        ":::{req} ready\nReady.\n:::\n\n:::{req} broken\nBroken.\n:::\n",
    )
    .unwrap();
    fs::write(root.join("evidence/rules.broken.json"), "{broken").unwrap();

    // Validation starts from a nested directory and finds the project.
    let output = validate_in(&root.join("src/nested"), &state);
    assert!(!output.status.success(), "{output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let message = format!("{stdout}{stderr}");
    assert!(message.contains("Habeas validation failed"), "{message}");
    assert!(message.contains("rules.ready"), "{message}");
    assert!(message.contains("evidence/rules.broken.json"), "{message}");
    assert!(
        message.contains("Run `habeas author --all`")
            && message.contains("address any reported gaps and rerun the tests"),
        "{message}"
    );
    // Validation never authors: no evidence or usage history is written.
    assert_eq!(fs::read_dir(root.join("evidence")).unwrap().count(), 1);
    assert_eq!(fs::read_dir(&state).unwrap().count(), 0);

    // Repeating validation gives the same result.
    let again = validate_in(&root, &state);
    assert_eq!(
        format!(
            "{}{}",
            String::from_utf8_lossy(&again.stdout),
            String::from_utf8_lossy(&again.stderr)
        )
        .lines()
        .filter(|line| line.starts_with("- "))
        .collect::<Vec<_>>(),
        message
            .lines()
            .filter(|line| line.starts_with("- "))
            .collect::<Vec<_>>()
    );

    common::coverage_setup(&root);

    // Valid evidence passes.
    fs::write(root.join("md/rules.md"), ":::{req} ready\nReady.\n:::\n").unwrap();
    fs::remove_file(root.join("evidence/rules.broken.json")).unwrap();
    fs::write(
        root.join("evidence/rules.ready.json"),
        serde_json::json!({
            "requirement": "Ready.",
            "determination": true,
            "rationale": "Reviewed.",
            "references": habeas::reference_set_digest_for(&root, "rules.ready").unwrap(),
            "inputs": []
        })
        .to_string(),
    )
    .unwrap();
    let output = validate_in(&root, &state);
    assert!(output.status.success(), "{output:?}");
    assert!(
        !root.join("target/habeas/coverage").exists(),
        "static validation needs no execution data"
    );
}
Input 6: outline tests/validate.rs fn[validate_fails_outside_a_cargo_and_mdbook_project]
// {test}`requirements.integration.validate`
#[test]
fn validate_fails_outside_a_cargo_and_mdbook_project() {
    let temp = TempDir::new().unwrap();
    fs::write(temp.path().join("Cargo.toml"), "[package]\nname = \"x\"\n").unwrap();
    let output = validate_in(temp.path(), temp.path());
    assert!(!output.status.success(), "{output:?}");
    let message = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        message.contains("cannot find Cargo and mdBook project root"),
        "{message}"
    );
}
Input 7: outline tests/validate.rs (whole file)
//! `habeas::validate()` reads the process's current directory, so each case
//! runs it in a child copy of this test binary started in a fixture project.

mod common;

use std::fs;
use std::path::Path;
use std::process::{Command, Output};

use tempfile::TempDir;

const CHILD: &str = "HABEAS_VALIDATE_CHILD";

/// Runs `habeas::validate()` when started as the child process; otherwise
/// does nothing, so the ordinary test run ignores it.
// {test}`requirements.integration.validate`
#[test]
fn validate_child() {
    if std::env::var_os(CHILD).is_some() {
        habeas::validate();
    }
}

// {cite}`habeas:util`
/// Runs [`validate_child`] in `cwd`, with Habeas state directed to `state`.
fn validate_in(cwd: &Path, state: &Path) -> Output {
    Command::new(std::env::current_exe().unwrap())
        .args([
            "--exact",
            "validate_child",
            "--nocapture",
            "--test-threads=1",
        ])
        .current_dir(cwd)
        .env(CHILD, "1")
        .env("XDG_STATE_HOME", state)
        .env("XDG_CONFIG_HOME", state)
        .env("HOME", state)
        .output()
        .unwrap()
}

// {test}`requirements.integration.validate`
// {test}`requirements.test-coverage.check.guidance`
#[test]
fn validate_fails_the_test_with_every_error_and_authoring_instructions() {
    let temp = TempDir::new().unwrap();
    let root = temp.path().join("project");
    let state = temp.path().join("state");
    fs::create_dir_all(root.join("md")).unwrap();
    fs::create_dir_all(root.join("src/nested")).unwrap();
    fs::create_dir_all(root.join("evidence")).unwrap();
    fs::create_dir_all(&state).unwrap();
    fs::write(root.join("Cargo.toml"), "[package]\nname = \"fixture\"\n").unwrap();
    fs::write(root.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
    fs::write(
        root.join("md/rules.md"),
        ":::{req} ready\nReady.\n:::\n\n:::{req} broken\nBroken.\n:::\n",
    )
    .unwrap();
    fs::write(root.join("evidence/rules.broken.json"), "{broken").unwrap();

    // Validation starts from a nested directory and finds the project.
    let output = validate_in(&root.join("src/nested"), &state);
    assert!(!output.status.success(), "{output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let message = format!("{stdout}{stderr}");
    assert!(message.contains("Habeas validation failed"), "{message}");
    assert!(message.contains("rules.ready"), "{message}");
    assert!(message.contains("evidence/rules.broken.json"), "{message}");
    assert!(
        message.contains("Run `habeas author --all`")
            && message.contains("address any reported gaps and rerun the tests"),
        "{message}"
    );
    // Validation never authors: no evidence or usage history is written.
    assert_eq!(fs::read_dir(root.join("evidence")).unwrap().count(), 1);
    assert_eq!(fs::read_dir(&state).unwrap().count(), 0);

    // Repeating validation gives the same result.
    let again = validate_in(&root, &state);
    assert_eq!(
        format!(
            "{}{}",
            String::from_utf8_lossy(&again.stdout),
            String::from_utf8_lossy(&again.stderr)
        )
        .lines()
        .filter(|line| line.starts_with("- "))
        .collect::<Vec<_>>(),
        message
            .lines()
            .filter(|line| line.starts_with("- "))
            .collect::<Vec<_>>()
    );

    common::coverage_setup(&root);

    // Valid evidence passes.
    fs::write(root.join("md/rules.md"), ":::{req} ready\nReady.\n:::\n").unwrap();
    fs::remove_file(root.join("evidence/rules.broken.json")).unwrap();
    fs::write(
        root.join("evidence/rules.ready.json"),
        serde_json::json!({
            "requirement": "Ready.",
            "determination": true,
            "rationale": "Reviewed.",
            "references": habeas::reference_set_digest_for(&root, "rules.ready").unwrap(),
            "inputs": []
        })
        .to_string(),
    )
    .unwrap();
    let output = validate_in(&root, &state);
    assert!(output.status.success(), "{output:?}");
    assert!(
        !root.join("target/habeas/coverage").exists(),
        "static validation needs no execution data"
    );
}

// {test}`requirements.integration.validate`
#[test]
fn validate_fails_outside_a_cargo_and_mdbook_project() {
    let temp = TempDir::new().unwrap();
    fs::write(temp.path().join("Cargo.toml"), "[package]\nname = \"x\"\n").unwrap();
    let output = validate_in(temp.path(), temp.path());
    assert!(!output.status.success(), "{output:?}");
    let message = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        message.contains("cannot find Cargo and mdBook project root"),
        "{message}"
    );
}

Referenced definitions

Input 2: requirements selection requirements.authoring.batch
:::{req} batch
`habeas author --all [--config <name>] [--missing | --force] [-v|--verbose]` MUST author
the evidence of every current requirement and named example, together with the
reviews {cite}`requirements.references.citation_review` and
{cite}`requirements.references.site_review` include, under
{cite}`requirements.authoring.refresh`.
`--all` MUST be mutually exclusive with a subject name and `--stdout`. It MUST
also delete each orphaned citation review under
{cite}`requirements.references.citation_review` and each orphaned site review
under {cite}`requirements.references.site_review`, and report each deletion; the
deletion MUST NOT require model configuration or credentials.
Failure to discover declarations or to read, scan, or parse project sources MUST report an error,
not a successful empty batch.

Each subject MUST receive an independent assessment with its own prompt, inputs,
judgment, and usage record. The command MUST report aggregate completion and each
authoring error under {cite}`requirements.authoring.reporting`, and retain
successful writes when another assessment fails. It MUST return a nonzero
exit status if any assessment fails. False judgments MUST still be written as
completed assessments; they remain failures under ordinary evidence validation.
A denied mandatory input MUST abort only the affected assessment under
{cite}`requirements.authoring.read_denial.mandatory`. Interruption MUST instead stop the
whole batch: queued assessments MUST NOT start, running assessments MUST be
cancelled, and an assessment whose evidence has not been written when the
interruption is observed MUST NOT begin committing it. After preparing a
temporary evidence file, the harness MUST check interruption again before its
atomic replacement; a replacement already in progress MAY finish. Evidence already written MUST be
retained, and the command MUST report the number of assessments not started as
a batch failure.

The bundled Habeas skills MUST direct the supervising agent to run this command
to create or refresh evidence. They MUST leave judgments, rationales, and input
snapshots to the authoring harness; the supervising agent addresses findings by
changing requirements, source citations, implementation, or tests.
{cite}`requirements.authoring.concurrency`
{cite}`requirements.authoring.usage_storage`
{cite}`requirements.authoring.persistence`
:::
Input 3: requirements selection requirements.authoring.deterministic_validation
:::{req} deterministic_validation
Ordinary evidence validation, status inspection, and book rendering MUST continue
to check evidence using the existing rules without invoking models, contacting
providers, or requiring authoring configuration or credentials. Missing or stale
evidence MUST remain a validation failure rather than trigger authoring.
:::

Other sources

Input 1: requirements selection requirements.integration.validate
:::{req} validate
`habeas::validate()` MUST check the current Cargo and mdBook project and make its Cargo test fail with all validation errors when any requirement evidence is invalid.
Its failure message MUST instruct the agent to run `habeas author --all`, address
the reported gaps, and rerun tests. Validation MUST remain deterministic and MUST
NOT invoke authoring itself. {cite}`requirements.authoring.batch`
{cite}`requirements.authoring.deterministic_validation`
:::