rohitsar567 commited on
Commit
22a03fc
Β·
1 Parent(s): 79280e2

feat(audit): selftest in pytest gate (static-scoped, no recursion) + opt-in git hooks + README

Browse files
.githooks/pre-commit ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Opt-in: git config core.hooksPath .githooks
3
+ exec "$(git rev-parse --show-toplevel)/tools/audit.sh" --static
.githooks/pre-push ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Opt-in: git config core.hooksPath .githooks
3
+ set -e
4
+ root="$(git rev-parse --show-toplevel)"
5
+ "$root/tools/audit.sh" --build
6
+ "$root/.venv/bin/python" -m audit --deploy
audit/README.md ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # `audit/` β€” the repo's self-verifying error/risk gate
2
+
3
+ One runnable command that asserts the repo is sound. **24 checks across 5 tiers.**
4
+ Every check traces to a real incident this project hit (LFS quota silent
5
+ failures, deleted-module import breakage, ChromaDB disk bloat, etc.) β€” the
6
+ auditor exists so those classes of failure cannot ship silently again.
7
+
8
+ The auditor itself is verified: `--selftest` reconstructs the broken state of
9
+ each incident in a temp fixture and asserts the matching check still flags it.
10
+ A check that no longer detects its incident is reported as "not self-verifying".
11
+
12
+ ## CLI
13
+
14
+ ```
15
+ tools/audit.sh --static|--build|--functional|--deploy|--all|--selftest [--json]
16
+ ```
17
+
18
+ `tools/audit.sh` just execs `.venv/bin/python -m audit "$@"` from the repo root.
19
+ (Note: `tools/audit.sh` is the entrypoint β€” unrelated to the pre-existing
20
+ `tools/audit/` tool directory.)
21
+
22
+ `--json` emits the per-check results as a JSON array instead of the text
23
+ report (id / status / evidence / remediation).
24
+
25
+ ## Tiers
26
+
27
+ | Flag | Runs | Checks | When to run |
28
+ |------|------|--------|-------------|
29
+ | `--static` | static | T1.* + T2.* (11) | **Pre-commit.** Seconds. Pure repo/AST/import/lint β€” no build, no network, no servers. |
30
+ | `--build` | static + build | + T3.* (14) | Before push. Runs the full `pytest` gate, `next build` (production static export), and boots the backend for `/api/health`. Minutes. |
31
+ | `--functional` | static + build + functional | + T4.* (20) | Needs a **local backend on :8000 and frontend** up. API smoke (health/coverage/chat/upload/profile) + Playwright E2E journeys. |
32
+ | `--deploy` | deploy only | T5.* (4) | **Read-only** prod/deploy safety: LFS pre-push simulation, Dockerfile coherence, deployed-SHA vs local, standing tripwires. |
33
+ | `--all` | static + build + functional + deploy | all 24 | Everything. Run with backend (and ideally frontend) up. |
34
+
35
+ ## Exit codes
36
+
37
+ - Exit is **non-zero iff any check is `FAIL`**.
38
+ - `WARN` **never fails the gate** (it surfaces a soft / deferred condition,
39
+ e.g. T5.3 deferred-deploy SHA mismatch or T2.3 a stale-doc note).
40
+ - `SKIP` (e.g. a prerequisite service is down) also does not fail the gate.
41
+
42
+ ## `--selftest`
43
+
44
+ ```
45
+ .venv/bin/python -m audit --selftest # full: 24 checks
46
+ ```
47
+
48
+ For every check, a fixture stands up the broken state from the original
49
+ incident; the check must return its `selftest_expect` status on that fixture
50
+ (`FAIL` for most; `WARN` for T5.3 / T5.4 which are advisory by design). Output:
51
+ `24 checks Β· 0 not self-verifying`. A non-zero "not self-verifying" count means
52
+ the auditor has silently rotted and is no longer catching its own incident.
53
+
54
+ The pytest gate (`tests/test_audit_selftest.py`) runs the selftest **scoped to
55
+ the static tier only** β€” `core.selftest(only_tiers={"static"})`. The
56
+ build/functional/deploy tiers are deliberately excluded from the unit gate:
57
+ T3.1's fixture shells `pytest`, which would recurse into that very test; T3.2
58
+ runs a multi-minute `next build`; Tier 4 needs a live backend. The full
59
+ 24-check selftest is run via `tools/audit.sh --selftest` (manual / pre-push).
60
+
61
+ ## Opt-in git hooks
62
+
63
+ `.githooks/` ships a `pre-commit` (`--static`) and a `pre-push`
64
+ (`--build` then `--deploy`). They are **opt-in** β€” enable per clone with:
65
+
66
+ ```
67
+ git config core.hooksPath .githooks
68
+ ```
69
+
70
+ This repo does not set that automatically; nothing changes your git config
71
+ until you run the command above.
72
+
73
+ ## Tier 5 is read-only against prod
74
+
75
+ Every Tier 5 check is strictly read-only with respect to deployed
76
+ infrastructure: it inspects local artifacts (Dockerfile, LFS attributes,
77
+ tripwire state) and at most *reads* the deployed SHA for comparison. It never
78
+ pushes, deploys, mutates remote state, or touches the HF Space β€” so `--deploy`
79
+ and `--all` are safe to run at any time.
audit/core.py CHANGED
@@ -71,12 +71,13 @@ def run(selected: set[str], as_json: bool = False) -> int:
71
  f"{sum(1 for _,r in rows if r.status is Status.SKIP)} skip")
72
  return 1 if fails else 0
73
 
74
- def selftest() -> int:
75
  from audit.selftest_fixtures import FIXTURES
76
  if not CHECKS:
77
  _load_all_checks()
 
78
  bad = []
79
- for c in CHECKS:
80
  fx = FIXTURES.get(c.id)
81
  if fx is None:
82
  bad.append(f"{c.id}: NO selftest fixture")
@@ -89,5 +90,5 @@ def selftest() -> int:
89
  if r.status is not c.selftest_expect:
90
  bad.append(f"{c.id}: expected {c.selftest_expect.value} on broken fixture, got {r.status.value}")
91
  for b in bad: print(f" FAIL {b}")
92
- print(f"\n selftest: {len(CHECKS)} checks Β· {len(bad)} not self-verifying")
93
  return 1 if bad else 0
 
71
  f"{sum(1 for _,r in rows if r.status is Status.SKIP)} skip")
72
  return 1 if fails else 0
73
 
74
+ def selftest(only_tiers: set[str] | None = None) -> int:
75
  from audit.selftest_fixtures import FIXTURES
76
  if not CHECKS:
77
  _load_all_checks()
78
+ checks = CHECKS if only_tiers is None else [c for c in CHECKS if c.tier in only_tiers]
79
  bad = []
80
+ for c in checks:
81
  fx = FIXTURES.get(c.id)
82
  if fx is None:
83
  bad.append(f"{c.id}: NO selftest fixture")
 
90
  if r.status is not c.selftest_expect:
91
  bad.append(f"{c.id}: expected {c.selftest_expect.value} on broken fixture, got {r.status.value}")
92
  for b in bad: print(f" FAIL {b}")
93
+ print(f"\n selftest: {len(checks)} checks Β· {len(bad)} not self-verifying")
94
  return 1 if bad else 0
tests/test_audit_selftest.py CHANGED
@@ -18,3 +18,14 @@ def test_core_runner_fails_on_a_fail_check():
18
  "import sys; sys.exit(c.run({'static'}))"],
19
  cwd=REPO).returncode
20
  assert code == 1
 
 
 
 
 
 
 
 
 
 
 
 
18
  "import sys; sys.exit(c.run({'static'}))"],
19
  cwd=REPO).returncode
20
  assert code == 1
21
+
22
+
23
+ def test_static_checks_are_self_verifying():
24
+ """The auditor's own integrity is in the pytest gate β€” but ONLY the
25
+ 'static' tier (Tier 1+2). The build/functional/deploy tiers are excluded
26
+ here on purpose: T3.1's fixture shells `pytest`, which would recurse into
27
+ this very test; T3.2 runs a multi-minute `next build`; Tier 4 needs a
28
+ live backend. Those are exercised by `tools/audit.sh --selftest` (manual
29
+ / pre-push hook), not the unit gate."""
30
+ import audit.core as c
31
+ assert c.selftest(only_tiers={"static"}) == 0, "a static-tier check is not self-verifying (see stdout)"