Revision: 1 Last modified: 2026-12-04T18:15:21Z
This is a library. It is sourced, never executed. Mode rw-rw-r--.
It is the one resolver for the DEFAULT scan root of scanner gates, and it exists to fix a specific defect:
scanner gates used to default their scan root to the literal .., i.e. relative to the caller's cwd. Run from
the canon root, .. scans the entire parent directory — every sibling project, many minutes, and findings from
foreign repositories.
The rule is that the default must depend on where the gate lives, never on where the caller happens to stand.
- The gate belongs to the repository at
<gate_dir>/../..(the "canon dir"). - If that repository is checked out as a submodule of a superproject (a consumer), the default root is the superproject working tree — the consumer, which is what "cwd=constitution, root=.." used to mean for consumers.
- Otherwise (a standalone canon checkout) the default root is the canon dir.
An explicit --root or environment override always wins and is never touched here.
It also provides cm_is_canon_root, a content-based test for "is this directory really the canon?" — which is
distinct from "is there a file called Constitution.md here".
bash,awk,grep,git(optional for the superproject probe; the resolver degrades to the canon dir without it).cm_is_canon_rootneedsscripts/gatesto exist under the candidate and the anchor heading to be present outside a code fence.
. scripts/lib/default_scan_root.sh
# a scanner gate asks for its default root, passing its OWN directory
root="$(cm_default_scan_root "$SCRIPT_DIR")" || exit 2
exec some-scanner --root "$root" ...
# is a directory the canon itself?
if cm_is_canon_root /path/to/checkout; then echo "canon"; fi| Function | Contract |
|---|---|
cm_default_scan_root GATE_DIR |
prints one absolute path; returns 1 and prints nothing when the canon dir cannot be resolved |
cm_is_canon_root DIR |
0 iff DIR is a canon repository itself; 1 otherwise. Read-only. |
GATE_DIRempty — returns 1 immediately (no guess).<gate_dir>/../..does not exist or is not readable — returns 1.gitabsent — the superproject probe is skipped and the canon dir is returned. The answer is still correct for a standalone checkout; for a submodule checkout without git it cannot know, and it does not pretend to.- An ambient
GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE/GIT_COMMON_DIRis explicitly cleared (env -u …) around therev-parse, so a leftover variable from another repository cannot make the probe answer about the wrong tree. cm_is_canon_rooton a directory holding an empty or strayConstitution.md— returns 1. Three things are required: the file, ascripts/gatesdirectory, and the§11.4.1anchor heading outside a code fence.- A fenced copy of the heading does not satisfy the test: the
awkpre-filter toggles on```/~~~fences and drops fenced lines beforegrep. - A canon that is checked out as a submodule is not a canon root: a vendored canon belongs to its consumer, so the superproject probe must come back empty.
grep -q -m1with early exit — the filter uses process substitution, not a pipe, sogrep's early exit cannot turn a present heading into apipefailmiss.
cm_default_scan_rootresolves<gate_dir>/../..physically (pwd -P) to get the canon dir.- It probes
git -C <canon> rev-parse --show-superproject-working-treewith the four repository-locating variables unset. - A non-empty, existing superproject is printed; otherwise the canon dir is printed.
cm_is_canon_rootrequires, in order:Constitution.mdpresent,scripts/gatesa directory, the^##+ +§11.4\.1 +(—|-)heading present outside a fence (via theawkfence filter feedingLC_ALL=C grep -q -m1through process substitution), and an empty superproject probe.
scripts/lib/constitution_layout.sh— the analogous "do not depend on the caller's cwd" resolver for the constitution/project paths, and the owner of the canonicalCL_GIT_SCRUB_VARSlist.scripts/gates/*— the scanner gates that consumecm_default_scan_root.scripts/gates/lib/gate_env_census.py— the static census of verdict-affecting ambient reads, which is what keeps a gate from reading a stray environment variable instead of using a resolver like this one.
2026-12-04 — bash -n scripts/lib/default_scan_root.sh → exit 0 (a library: sourced, not executed).
Behaviour a reader can repeat in this checkout:
bash -c '. scripts/lib/default_scan_root.sh; cm_default_scan_root "$PWD/scripts/gates"; \
cm_is_canon_root "$PWD" && echo canon-ok'Expected: the resolved default scan root (this checkout, or its superproject when vendored), then canon-ok.