Skip to content

Latest commit

 

History

History
144 lines (105 loc) · 6.5 KB

affected_set.md

File metadata and controls

144 lines (105 loc) · 6.5 KB

affected_set.py

Revision: 1 Last modified: 2026-12-04T19:00:00Z

Overview

affected_set.py decides which gates a change must run — and, crucially, refuses to guess when it cannot decide soundly.

This is the selector at the heart of the fast lane. Every saved minute comes from skipping gates, and every skipped gate is a chance to miss a regression. The tool's governing rule is therefore explicit (AS-010): any doubt ⇒ full applicable suite. When it cannot prove that the map is fresh and complete, it does not emit a narrow set — it emits the full set with a named reason.

Run it between "here is what changed" and "run these gates": its output is gate_runner.sh --affected.

Prerequisites

  • python3 (stdlib only).
  • git — a failed diff computation is itself a fallback trigger.
  • A gate map JSON (--map) produced by io_trace.sh build-map, containing per gate: observed_inputs, content_hash, and any undeclared_reads.
  • A layer value (--layer) naming the change's layer, e.g. docs.
  • No environment variables are read.

Usage examples

# what must run for a committed range?
python3 scripts/fastcycle/gates/affected_set.py \
  --config gate_config.json \
  --base "$(git rev-parse origin/main)" \
  --head "$(git rev-parse HEAD)" \
  --map gate_map.json --layer audio \
  --repo . --out affected.json

# what must run for an uncommitted edit?
python3 scripts/fastcycle/gates/affected_set.py \
  --config gate_config.json --base HEAD --head WORKTREE \
  --map gate_map.json --layer audio --repo . --out affected.json

Then:

sh scripts/fastcycle/gates/gate_runner.sh --config gate_config.json \
  --affected affected.json --out verdicts.json

Exit codes

Code Meaning
0 A determinable set was emitted.
2 Usage error.
4 A full-suite FALLBACK was emitted (determinable = false). The --out document is still written.

Exit 4 is the conservative-safe default (§11.4.201), never a silent narrower guess. It is a successful refusal, not a crash — the document is valid and contains the complete gate set.

A recorded discrepancy: exit 0 vs 4 for fallback

specs/004-fast-dev-cycles/contracts/affected-set-and-verdict-cache.md line 55 says the tool exits 0 for "determinable or fallback", reserving 4 specifically for an unreadable diff. The RED test's fixtures (as_bad_unmapped_path, as_bad_undeclared_read, as_bad_stale_map) instead pin expected_exit_code=4 for every determinate-false case.

This implementation follows the RED test, which is the mechanically-checked artifact. The discrepancy is recorded here rather than silently resolved (§11.4.6 — do not paper over a guess, and do not quietly "fix" a contract). A caller must treat 4 as "full set emitted", not as "failed".

Selection rules

A gate becomes a member when:

  • AS-010a (self-change) — the gate's own script is among the changed paths. This is unconditional.
  • Observed-input match — the gate's observed_inputs intersects the changed paths.

A mapped gate that is neither is skipped, with one of two reasons:

Skip reason When
LAYER_NOT_APPLICABLE AS-012: every changed path maps only to doc-layer gates, so change_class is docs-only and non-doc-layer gates are skipped.
NO_OBSERVED_INPUT_CHANGED The generic case — no observed input of this gate changed.

Fallback triggers

Any one of these sets determinable=false, makes members the full gate set with skipped=[], and records an exact fallback_reason naming the first triggering condition:

Trigger Clause Meaning
Stale map AS-003 Some gate's real current script sha256 — read at --base — does not match that gate's content_hash in the map.
Undeclared reads AS-001 / AS-010 Some map entry carries a non-empty undeclared_reads list (INPUTS_UNSOUND).
Unmapped path AS-010 A changed path is neither any gate's own script nor in any gate's observed_inputs (DEC-08 union rule).
Diff unreadable — The change itself could not be computed (git failure).

Why the map is validated at --base, not at head

A self-change at head is a positive inclusion signal, not a trust failure: the gate's own script changing is precisely why that gate should run. Validating the map's freshness at head would therefore turn every gate-self-change into a spurious full-suite fallback. Reading the script at --base asks the right question — was the map accurate for the tree the change started from?

Edge cases

  • An uncommitted edit. Use --head WORKTREE.
  • --base equal to --head. No changed paths; only AS-012-style layer logic can skip anything.
  • A gate with no content_hash in the map. Cannot be validated → fallback (4).
  • An empty undeclared_reads list. Sound; it is the non-empty list that forces fallback.
  • A changed path outside the repository. Cannot be mapped → fallback (4).

Internal behaviour

  1. Computes the changed path set for base..head (or base..WORKTREE).
  2. Validates the map: reads each gate's own script at --base and compares sha256 against the map's content_hash; checks undeclared_reads is empty everywhere.
  3. If any validation fails, or any changed path is unmappable, or the diff failed → emit the full set with determinable=false and a fallback_reason, then exit 4.
  4. Otherwise computes members (self-changes plus observed-input matches), derives change_class, records per-gate skip reasons, writes --out, exits 0.

Related scripts

Called by scripts/fastcycle/gates/lib/gate_runner_order.py and scripts/fastcycle/review/lib/precheck_pack_run.py.

Calls nothing at runtime beyond git; it consumes io_trace.sh's map as data.

Guarded by scripts/fastcycle/tests/test_affected_set_red.sh (T053), fixtures under scripts/fastcycle/tests/fixtures/affected_set/. Contract: specs/004-fast-dev-cycles/contracts/affected-set-and-verdict-cache.md (AS-010, AS-010a, AS-011, AS-012; AS-001, AS-003).

See also docs/scripts/io_trace.sh.md (produces the map), docs/scripts/gate_runner.sh.md (consumes the set) and docs/scripts/verdict_cache.py.md (the sibling caching layer).

Last verified

2026-12-04 — python3 -c "import ast;ast.parse(open('scripts/fastcycle/gates/affected_set.py').read())" → exit 0. python3 scripts/fastcycle/gates/affected_set.py with no arguments exits 2 with a usage message (required flags absent).