Skip to content

Latest commit

 

History

History
102 lines (75 loc) · 5.02 KB

default_scan_root.md

File metadata and controls

102 lines (75 loc) · 5.02 KB

default_scan_root.sh

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

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".

Prerequisites

  • bash, awk, grep, git (optional for the superproject probe; the resolver degrades to the canon dir without it).
  • cm_is_canon_root needs scripts/gates to exist under the candidate and the anchor heading to be present outside a code fence.

Usage examples

. 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

Exit codes / return values

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.

Edge cases

  • GATE_DIR empty — returns 1 immediately (no guess).
  • <gate_dir>/../.. does not exist or is not readable — returns 1.
  • git absent — 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_DIR is explicitly cleared (env -u …) around the rev-parse, so a leftover variable from another repository cannot make the probe answer about the wrong tree.
  • cm_is_canon_root on a directory holding an empty or stray Constitution.md — returns 1. Three things are required: the file, a scripts/gates directory, and the §11.4.1 anchor heading outside a code fence.
  • A fenced copy of the heading does not satisfy the test: the awk pre-filter toggles on ``` / ~~~ fences and drops fenced lines before grep.
  • 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 -m1 with early exit — the filter uses process substitution, not a pipe, so grep's early exit cannot turn a present heading into a pipefail miss.

Internal behaviour

  1. cm_default_scan_root resolves <gate_dir>/../.. physically (pwd -P) to get the canon dir.
  2. It probes git -C <canon> rev-parse --show-superproject-working-tree with the four repository-locating variables unset.
  3. A non-empty, existing superproject is printed; otherwise the canon dir is printed.
  4. cm_is_canon_root requires, in order: Constitution.md present, scripts/gates a directory, the ^##+ +§11.4\.1 +(—|-) heading present outside a fence (via the awk fence filter feeding LC_ALL=C grep -q -m1 through process substitution), and an empty superproject probe.

Related scripts

  • 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 canonical CL_GIT_SCRUB_VARS list.
  • scripts/gates/* — the scanner gates that consume cm_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.

Last verified

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.