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.cli.distributed_guidance.lists

Requirements index · View definition

evidence Valid

Requirement

The habeas skill’s reference on distributed requirements MUST give the guidance of cite requirements.cli.distributed_guidance.when, cite requirements.cli.distributed_guidance.queries, cite requirements.cli.distributed_guidance.site_text, and cite requirements.cli.distributed_guidance.reviews as a list of things to do and a list of things not to do.

Rationale

The test tests/authoring_cli.rs#fn[bundled_skills_give_distributed_guidance_as_do_and_dont_lists] adequately checks requirements.cli.distributed_guidance.lists.

Obligation. The habeas skill’s distributed-requirements reference (assets/skills/habeas/references/distributed-requirements.md) must give the guidance of when, queries, site_text and reviews as a list of things to do and a list of things not to do.

Structure.

  • The test finds the ## Do and ## Don't sections in the raw reference file. unwrap makes it fail if either is missing.
  • For each section it collects the top-level - bullets and asserts there are at least two.
  • Every ## Do bullet must start with - and must not start with - Don't .
  • Every ## Don't bullet must start with - Don't .
  • Together these assertions make the first list a Do list and the second a Don’t list. A bullet in the wrong list would fail.

Content placement. assert_text runs over the collapsed section(...) body of each list. It requires phrases for each of the four cited requirements to sit in the right list:

  • when: the Do bullet “Make a requirement distributed only when…” and the Do bullet “Separate obligations each site upholds…”. These cover the distributed-only-when guidance and marking {impl} on code that carries out obligations the sites don’t uphold.
  • queries: the Do bullets “Write a violation query” and “Give every query match and no_match snippets”. It also checks the sentence about checking that the site queries select every place the obligation applies and no others.
  • site_text: the Do bullet “Describe sites exactly as the site queries select them”, plus the Don’t bullets “Don’t describe sites more broadly” and “Don’t state a per-site obligation”.
  • reviews: the Do bullet “Let habeas author --all author the site reviews.” and the Don’t bullets “Don’t reference the distributed requirement from a site” and “Don’t re-run a false site review for a different result”.

I read the reference file and confirmed that its ## Do and ## Don't sections contain these bullets, so the expected results are correct. The helpers section, assert_text and distributed_reference are reached from the marked test and need no reference of their own.

Residual points.

  • This requirement is about the list form. Whether each piece of guidance says the right thing is covered by the refining requirements’ own tests, such as bundled_skills_make_a_requirement_distributed_only_for_per_site_obligations, bundled_skills_explain_site_references_and_site_reviews and the others that use section(distributed_reference(), "## Do") and "## Don't". Those obligations need no test here.
  • Some detail inside a bullet is not asserted here. One example is the {impl} clause of the “Make a requirement distributed only when” bullet. The test still verifies that the guidance sits in the Do list, and the content tests cover the text.

I found no gap that is not already covered, and there are no acknowledgments to consider.

Inputs

Implementation

Input 7: outline assets/skills/habeas/references/distributed-requirements.md section[Distributed%20requirements]
# Distributed requirements

Some obligations apply on their own at each of many sites, such as a convention
every validation function follows. Marking every site `{impl}` spreads the
requirement across the codebase and asks one review to judge them all. A
distributed requirement instead declares queries that find its sites, and
Habeas reviews each site separately.

## Queries

A requirement becomes distributed by containing query directives, each with one
fenced `yaml` block and a name unique within the requirement:

- `:::{sites} <name>` selects the places the obligation applies.
- `:::{violations} <name>` selects constructs likely to break it, such as `?`,
  `return`, `continue`, or `break` in a function that must keep going after a
  failed check. Each needs `within: <site query>`, naming a site query of the same
  requirement; only matches inside that query's sites count. Violation queries
  point site reviews at likely problems; each site's review still judges the
  whole site, and a violation query that misses a construct does not by itself
  make the requirement false.

Query directives belong inside a requirement. Each name is a single identifier
(ASCII letters, digits, underscores, or hyphens), unique across both query kinds in
that requirement. Validation reports missing or extra YAML blocks, invalid YAML,
unknown query keys, unsupported languages, and rules that cannot compile. A
violation query naming a nonexistent site query in `within` is also invalid;
`within` belongs only to violation queries.
Validation also rejects empty `match` or `no_match` lists, snippets that fail to
parse or have the wrong match result, and violation queries without a site query.

Each body has these keys: `language` (`rust`, `python`, `typescript`, `tsx`,
`css`, `markdown`, `toml`, or `yaml`); optional `paths` and `exclude`, lists of
project-relative files or directories; an ast-grep `rule`, with optional `utils`
and `constraints`; `within`, required for violation queries; and `match` and
`no_match` snippet lists. Give the requirement a longer outer fence:

````text
::::{req} errors
A validation function that collects validation errors MUST hold them in a
parameter or local variable named `errors`, and a function with such a variable
is a site. After one of its checks fails, a site MUST still run every later check
that does not depend on the failed check's result.

:::{sites} error_accumulators
```yaml
language: rust
paths: [src]
rule:
  kind: function_item
  has:
    stopBy: { kind: function_item }
    any:
      - kind: parameter
        has: { field: pattern, regex: "^errors$" }
      - kind: let_declaration
        has: { field: pattern, regex: "^errors$" }
match:
  - "fn check(value: &str, errors: &mut Vec<String>) {}"
  - |
    fn run() {
        let mut errors = Vec::new();
        check("", &mut errors);
    }
no_match:
  - "fn check(value: &str, problems: &mut Vec<String>) {}"
  - |
    fn run() {
        let mut problems = Vec::new();
        check("", &mut problems);
    }
```
:::

:::{violations} early_exits
```yaml
language: rust
paths: [src]
within: error_accumulators
rule:
  any:
    - kind: try_expression
    - kind: return_expression
match:
  - |
    fn check(v: &Value, errors: &mut Vec<String>) -> Option<u8> {
        let n = v.as_u64()?;
        if v.is_null() { errors.push("null".into()); }
        Some(n as u8)
    }
  - |
    fn check(v: &Value, errors: &mut Vec<String>) {
        if !v.is_object() { errors.push("object".into()); return; }
        if v.is_null() { errors.push("null".into()); }
    }
no_match:
  - |
    fn parse(v: &Value) -> Option<u8> {
        let n = v.as_u64()?;
        Some(n as u8)
    }
  - |
    fn check(v: &Value, errors: &mut Vec<String>) {
        if v.is_null() { errors.push("null".into()); }
    }
```
:::
::::
````

Each site-query match makes a site: the innermost outline node containing the
match, such as the enclosing function. A violation match is reported to the site
that contains it.

## Do

- Make a requirement distributed only when its obligation applies on its own at
  each site; when particular code carries out the requirement as a whole, use
  `{impl}` there.
- Describe sites exactly as the site queries select them. When a query relies on
  a convention, such as how a variable is named, state the convention as an
  obligation, so code that breaks it is a real gap you fix in the code.
- Separate obligations each site upholds from obligations particular code
  carries out, and mark that code `{impl}`.
- State the requirement's scope in its text, and use `paths` and `exclude` for
  scope a rule cannot see, such as authoring code outside validation.
- Give every query `match` and `no_match` snippets that test its conditions the
  way you would test code: `match` snippets for what it selects, and `no_match`
  snippets for nearby code it must not select, such as the right type with the
  wrong name. A condition needs no snippet of its own. This applies to violation
  queries too.
- Write a violation query, with `within` naming the site query, for every kind
  of construct that could break the obligation, such as each early exit in a
  function that must keep going.
- Before the habeas-requirements review (its step 3), check that the site
  queries select every place the obligation
  applies and no others: think about places they could miss, such as a list
  held in a local variable or under another name, and places they select that
  the obligation does not cover. Widen or narrow the query, or state the
  convention as an obligation so a miss becomes a code gap. The requirement's
  review searches for both.
- Present the queries and snippets with the requirement text when reviewing it
  with the user.
- Check with `habeas info <full-name>` that the sites are the ones you expect.
- Let `habeas author --all` author the site reviews.

## Don't

- Don't describe sites more broadly than the queries select, as in "however it is
  named".
- Don't state a per-site obligation that a reviewer cannot check from the site
  and the helpers it calls, such as "not reported by another check", which needs
  reading other sites; narrow it to the site, such as "does not report one
  failure twice".
- Don't reference the distributed requirement from a site just because it is a
  site; sites need none. A site review reads its site, which covers it for Rust
  item coverage, but a site that is a root of another requirement gets
  `{impl}` of that one, and a site that carries out one of this requirement's
  other obligations, such as the function that reports the collected errors,
  gets `{impl}` of this one like any other root.
- Don't re-run a false site review for a different result; fix the site, or
  tighten the queries or the text, as the habeas-evidence skill's
  `references/reviews.md` describes.

## Gotchas

- Patterns parse as standalone code, so a type or parameter fragment needs
  ast-grep's `context` and `selector` form, such as
  `{ context: "fn f(x: &mut Vec<String>) {}", selector: reference_type }`. A plain
  pattern that parses differently silently matches nothing; the snippets catch
  this.
- Each snippet parses as a whole file, so a Rust snippet must be complete items,
  not a bare expression.
- `habeas coverage` runs every snippet and reports a wrong result, and a
  site query that matches nothing in the project.

## Review

`habeas author --all` authors a site review for each site, judging the per-site
obligations and each violation match in it. The requirement's own review checks
the Do list above.

Tests

Input 6: outline tests/authoring_cli.rs fn[bundled_skills_give_distributed_guidance_as_do_and_dont_lists]
// {test}`requirements.cli.distributed_guidance.lists`
#[test]
fn bundled_skills_give_distributed_guidance_as_do_and_dont_lists() {
    let reference = distributed_reference();
    for (heading, prefix) in [("## Do", "- "), ("## Don't", "- Don't ")] {
        let (_, rest) = reference.split_once(&format!("\n{heading}\n")).unwrap();
        let list = rest.split("\n## ").next().unwrap();
        let items = list
            .lines()
            .filter(|line| line.starts_with("- "))
            .collect::<Vec<_>>();
        assert!(items.len() >= 2, "{heading}");
        for item in &items {
            assert!(item.starts_with(prefix), "{heading}: {item}");
            if heading == "## Do" {
                assert!(!item.starts_with("- Don't "), "{heading}: {item}");
            }
        }
    }
    assert_text(
        "Do list",
        &section(reference, "## Do"),
        &[
            "- Make a requirement distributed only when",
            "- Separate obligations each site upholds",
            "- Write a violation query",
            "- Give every query `match` and `no_match` snippets",
            "check that the site queries select every place the obligation applies and no others",
            "- Describe sites exactly as the site queries select them",
            "- Let `habeas author --all` author the site reviews.",
        ],
    );
    assert_text(
        "Don't list",
        &section(reference, "## Don't"),
        &[
            "- Don't describe sites more broadly",
            "- Don't state a per-site obligation",
            "- Don't reference the distributed requirement from a site",
            "- Don't re-run a false site review for a different result",
        ],
    );
}

Referenced definitions

Input 2: requirements selection requirements.cli.distributed_guidance.when
:::{req} distributed_guidance.when
The bundled skills MUST direct agents to make a requirement distributed only when its obligation applies on its own at each of many sites, such as a convention every validation function follows; to use `{impl}` instead when particular code carries out the requirement as a whole; and to mark with `{impl}` the code that carries out any obligation of a distributed requirement that its sites do not uphold.
:::
Input 3: requirements selection requirements.cli.distributed_guidance.queries
:::{req} distributed_guidance.queries
The bundled skills MUST direct agents to write site queries that select every place the obligation applies and no others, violation queries for every kind of construct that could break it, and, for every query, `match` and `no_match` snippets that test its conditions.
:::
Input 4: requirements selection requirements.cli.distributed_guidance.site_text
:::{req} distributed_guidance.site_text
The bundled skills MUST direct agents to describe sites exactly as the site queries select them, to state as an obligation any convention the queries rely on, such as how a list is named, and to state only per-site obligations that a reviewer can check from the site and the helpers it calls.
:::
Input 5: requirements selection requirements.cli.distributed_guidance.reviews
:::{req} distributed_guidance.reviews
The bundled skills MUST tell agents that sites need no reference to the requirement, that `habeas author --all` authors site reviews under {cite}`requirements.references.site_review`, and that a false site review is addressed by fixing the site, or by tightening the queries or the requirement text when the site should not be covered or the text admits more than one reading, under {cite}`requirements.cli.flip_guidance`, not by re-running the review.
:::

Other sources

Input 1: requirements selection requirements.cli.distributed_guidance.lists
:::{req} distributed_guidance.lists
The `habeas` skill's reference on distributed requirements MUST give the guidance of {cite}`requirements.cli.distributed_guidance.when`, {cite}`requirements.cli.distributed_guidance.queries`, {cite}`requirements.cli.distributed_guidance.site_text`, and {cite}`requirements.cli.distributed_guidance.reviews` as a list of things to do and a list of things not to do.
:::
Input 8: outline tests/authoring_cli.rs fn[section]
/// The collapsed body of a Markdown section, from the line after `heading` to
/// the next heading of the same or a higher level outside a code fence.
// {cite}`habeas:util`
fn section(raw: &str, heading: &str) -> String {
    let level = heading.chars().take_while(|c| *c == '#').count();
    let mut lines = raw.lines().skip_while(|line| *line != heading);
    assert!(lines.next().is_some(), "no section {heading:?}");
    let mut fence: Option<usize> = None;
    let mut body = Vec::new();
    for line in lines {
        let ticks = line.trim_start().chars().take_while(|c| *c == '`').count();
        match fence {
            Some(open) if ticks >= open && line.trim().chars().all(|c| c == '`') => fence = None,
            Some(_) => {}
            None if ticks >= 3 => fence = Some(ticks),
            None => {
                let hashes = line.chars().take_while(|c| *c == '#').count();
                if hashes > 0 && hashes <= level && line[hashes..].starts_with(' ') {
                    break;
                }
            }
        }
        body.push(line);
    }
    collapse(&body.join("\n"))
}
Input 9: outline tests/authoring_cli.rs fn[assert_text]
// {cite}`habeas:util`
fn assert_text(label: &str, text: &str, phrases: &[&str]) {
    for phrase in phrases {
        assert!(text.contains(phrase), "{label} lacks {phrase:?}");
    }
}
Input 10: outline tests/authoring_cli.rs fn[distributed_reference]
/// The habeas skill's reference on distributed requirements, raw.
// {cite}`habeas:util`
fn distributed_reference() -> &'static str {
    skill_file("habeas", "references/distributed-requirements.md")
}