From b588e7684e486f77d46f418c4f09d605a78e8bb0 Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:27:23 +0000 Subject: [PATCH] Move squabbler corpus under tests/ and drop Python checker Carries the local leftover of the PR #169 move: corpus/ removed, tests/squabbler-corpus/ added with the shell and jq checker. Same content as PR #170. Committed locally only; not pushed. --- corpus/tests/run-tests.py | 70 -------------- corpus/validate.py | 95 ------------------- .../squabbler-corpus}/README.adoc | 8 +- .../squabbler-corpus}/cases/SQ-CASE-0001.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0002.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0003.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0004.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0005.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0006.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0007.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0008.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0009.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0010.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0011.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0012.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0013.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0014.json | 0 .../squabbler-corpus}/cases/SQ-CASE-0015.json | 0 tests/squabbler-corpus/check-case.jq | 91 ++++++++++++++++++ tests/squabbler-corpus/check-case.sh | 40 ++++++++ tests/squabbler-corpus/run-tests.sh | 75 +++++++++++++++ .../squabbler-corpus}/schema/case.schema.json | 2 +- 22 files changed, 211 insertions(+), 170 deletions(-) delete mode 100644 corpus/tests/run-tests.py delete mode 100644 corpus/validate.py rename {corpus => tests/squabbler-corpus}/README.adoc (82%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0001.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0002.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0003.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0004.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0005.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0006.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0007.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0008.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0009.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0010.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0011.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0012.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0013.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0014.json (100%) rename {corpus => tests/squabbler-corpus}/cases/SQ-CASE-0015.json (100%) create mode 100644 tests/squabbler-corpus/check-case.jq create mode 100755 tests/squabbler-corpus/check-case.sh create mode 100755 tests/squabbler-corpus/run-tests.sh rename {corpus => tests/squabbler-corpus}/schema/case.schema.json (98%) diff --git a/corpus/tests/run-tests.py b/corpus/tests/run-tests.py deleted file mode 100644 index 16d0a9a..0000000 --- a/corpus/tests/run-tests.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: MPL-2.0 -# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) -"""Fire and silence tests for validate.py. Offline. Exit 0 only if every test behaves as expected.""" -import copy, glob, json, os, sys -HERE = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.dirname(HERE)) -from validate import check - -BASE = json.load(open(os.path.join(HERE, "..", "cases", "SQ-CASE-0012.json"), encoding="utf-8")) -def mut(fn): - c = copy.deepcopy(BASE); fn(c); return c -def setp(path, value): - def fn(c): - o = c - for k in path[:-1]: o = o[k] - o[path[-1]] = value - return fn - -FIRE = { # rule -> (mutation, must fire exactly this rule) - "V001": lambda c: c.pop("job"), - "V002": setp(["classification","group"], "nonsense"), - "V003": None, # handled below (file name) - "V004": lambda c: c["evidence"].update(log_excerpt="\n".join(["x"]*31), error_strings=["x"]), - "V005": setp(["fix","description"], "use ghp_" + "A"*24 + " to push"), - "V006": setp(["fix","description"], "ask someone@example.org"), - "V007": setp(["fix","description"], "runner build01.internal is slow"), - "V009": lambda c: (c["evidence"].update(error_strings=[]), c["classification"].update(confidence="proven")), - "V010": setp(["fix","requires_owner_go"], False), - "V011": setp(["observed_at"], "20 Nov 2026"), - "V012": lambda c: c["evidence"].update(log_excerpt="unrelated line", error_strings=["not in the excerpt"]), -} -SILENT = { # near-misses that must stay silent - "plain case": lambda c: None, - "hash-like text, no token prefix": setp(["fix","description"], "see commit c519c452 and sha256 6cbeb457"), - "ghp inside a word": setp(["fix","description"], "ghpath is a variable name"), - "private-looking but public host": setp(["fix","description"], "archive.ubuntu.com timed out"), - "version number, not an address": setp(["fix","description"], "upload-artifact v7.0.2 and 10.2 seconds"), - "none owner without owner go": lambda c: c["fix"].update(owner="none", requires_owner_go=False), - "null observed_at": setp(["observed_at"], None), - "hypothesis without strings": lambda c: (c["evidence"].update(error_strings=[]), c["classification"].update(confidence="hypothesis")), - "30-line excerpt": lambda c: c["evidence"].update(log_excerpt="\n".join(["x"]*30), error_strings=["x"]), - "action@version is not an email": setp(["fix","description"], "actions/upload-artifact@v7.0.2 is missing"), - "pr null (main)": setp(["pr"], None), -} -fails = []; n = 0 -for code, fn in FIRE.items(): - n += 1 - if code == "V003": - got = [x[0] for x in check(copy.deepcopy(BASE), "SQ-CASE-9999.json")] - else: - got = [x[0] for x in check(mut(fn), None)] - if got != [code]: - fails.append(f"FIRE {code}: expected exactly [{code}], got {got}") -for name, fn in SILENT.items(): - n += 1 - got = [x[0] for x in check(mut(fn), None)] - if got: - fails.append(f"SILENT '{name}': expected no findings, got {got}") -real = sorted(glob.glob(os.path.join(HERE, "..", "cases", "*.json"))) -for p in real: - n += 1 - got = [x[0] for x in check(json.load(open(p, encoding="utf-8")), p)] - if got: - fails.append(f"SILENT {os.path.basename(p)}: expected no findings, got {got}") -n += 1 -if len(real) != 15: fails.append(f"expected 15 cases, found {len(real)}") -for f in fails: print("FAIL", f) -print(f"{n - len(fails)}/{n} tests behaved as expected ({len(FIRE)} fire, {len(SILENT)} near-miss silence, {len(real)} real cases silent)") -sys.exit(1 if fails else 0) diff --git a/corpus/validate.py b/corpus/validate.py deleted file mode 100644 index 47fbeff..0000000 --- a/corpus/validate.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: MPL-2.0 -# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) -"""Offline validator for squabbler corpus cases. Standard library only. -Usage: validate.py FILE.json [FILE.json ...] (exit 0 = silent, 1 = findings)""" -import json, os, re, sys - -TOP = {"id","schema_version","repo","pr","run_id","job","step","attempt","observed_at", - "evidence","classification","main_comparison","fix","retry_policy","provenance"} -GROUPS = {"rate-limit","infra-flake","cancelled-timeout","lock-drift","stale-generated-file", - "real-failure","missing-tool","quality-gate"} -CONF = {"proven","likely","hypothesis"} -MAIN = {"passes","fails","absent","unknown"} -OWNERS = {"ci-gate-owner","pr-author","repo-owner","estate-owner","none"} -RISK = {"low","medium","high"} -RETRY = {"none","rerun","rerun-after-window","fix-then-rerun"} -READBY = {"log","api"} -SECRET = re.compile(r"(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|AKIA[0-9A-Z]{16}|" - r"-----BEGIN [A-Z ]*PRIVATE KEY-----|(?i:bearer)\s+[A-Za-z0-9._~+/=-]{20,}|" - r"(?i:(?:token|secret|password|apikey)\s*[:=]\s*[^\s\"']{8,}))") -EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b") -INTERNAL = re.compile(r"(\b[\w-]+\.(?:internal|corp|local|lan|intranet)\b|\b(?:10|192\.168)\.\d+\.\d+(?:\.\d+)?\b|\b172\.(?:1[6-9]|2\d|3[01])\.\d+\.\d+\b)", re.I) -TS = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$") - -def texts(o): - if isinstance(o, str): yield o - elif isinstance(o, dict): - for v in o.values(): yield from texts(v) - elif isinstance(o, list): - for v in o: yield from texts(v) - -def isint(v): return isinstance(v, int) and not isinstance(v, bool) - -def check(c, filename=None): - out = [] - def f(code, msg): out.append((code, msg)) - if not isinstance(c, dict): return [("V001", "case is not an object")] - if set(c) != TOP: - f("V001", f"fields differ: missing {sorted(TOP-set(c))} extra {sorted(set(c)-TOP)}") - return out - ev, cl, mc, fx, rp, pv = (c["evidence"], c["classification"], c["main_comparison"], - c["fix"], c["retry_policy"], c["provenance"]) - ok = True - def need(cond, msg): - nonlocal ok - if not cond: ok = False; f("V002", msg) - need(c["schema_version"] == "1", "schema_version must be \"1\"") - need(isinstance(c["id"], str) and re.fullmatch(r"SQ-CASE-\d{4}", c["id"] or ""), "id must match SQ-CASE-NNNN") - need(isinstance(c["repo"], str) and re.fullmatch(r"[\w.-]+/[\w.-]+", c["repo"] or ""), "repo must be owner/name") - need(c["pr"] is None or isint(c["pr"]), "pr must be integer or null (null = main)") - need(c["run_id"] is None or isint(c["run_id"]), "run_id must be integer or null") - need(c["attempt"] is None or isint(c["attempt"]), "attempt must be integer or null") - need(isinstance(c["job"], str) and c["job"], "job must be a non-empty string") - need(c["step"] is None or isinstance(c["step"], str), "step must be string or null") - need(isinstance(ev, dict) and set(ev) == {"log_excerpt","exit_code","error_strings"}, "evidence fields") - need(isinstance(cl, dict) and cl.get("group") in GROUPS and cl.get("confidence") in CONF, "classification group/confidence") - need(isinstance(mc, dict) and mc.get("state") in MAIN and isinstance(mc.get("note"), str), "main_comparison") - need(isinstance(fx, dict) and fx.get("owner") in OWNERS and fx.get("risk") in RISK and - isinstance(fx.get("files"), list) and isinstance(fx.get("requires_owner_go"), bool) and - isinstance(fx.get("description"), str), "fix fields") - need(isinstance(rp, dict) and rp.get("action") in RETRY, "retry_policy.action") - need(isinstance(pv, dict) and pv.get("read_by") in READBY, "provenance.read_by must be log or api") - if not ok: return out - if c["observed_at"] is not None and not (isinstance(c["observed_at"], str) and TS.match(c["observed_at"])): - f("V011", "observed_at must be UTC YYYY-MM-DDThh:mm:ssZ or null") - if filename and os.path.splitext(os.path.basename(filename))[0] != c["id"]: - f("V003", "file name must equal the case id") - ex = ev["log_excerpt"] - if ex is not None and (not isinstance(ex, str) or len(ex.splitlines()) > 30): - f("V004", "log_excerpt must be a string of at most 30 lines, or null") - strings = ev["error_strings"] - if not isinstance(strings, list) or not all(isinstance(s, str) and s for s in strings): - f("V002", "error_strings must be a list of non-empty strings"); return out - blob = list(texts(c)) - if any(SECRET.search(t) for t in blob): f("V005", "possible secret or token") - if any(EMAIL.search(t) for t in blob): f("V006", "email address") - if any(INTERNAL.search(t) for t in blob): f("V007", "internal hostname or private address") - if cl["confidence"] == "proven" and not strings: f("V009", "proven needs at least one verbatim error string") - if ex is not None and any(s not in ex for s in strings): f("V012", "error string not present in log_excerpt") - if fx["owner"] != "none" and not fx["requires_owner_go"]: f("V010", "a fix owned by someone needs requires_owner_go true") - return out - -def main(argv): - bad = 0 - for p in argv: - try: - with open(p, encoding="utf-8") as fh: c = json.load(fh) - except (OSError, ValueError) as e: - print(f"{p}: V000 unreadable: {e}"); bad += 1; continue - for code, msg in check(c, p): - print(f"{p}: {code} {msg}"); bad += 1 - return 1 if bad else 0 - -if __name__ == "__main__": - sys.exit(main(sys.argv[1:])) diff --git a/corpus/README.adoc b/tests/squabbler-corpus/README.adoc similarity index 82% rename from corpus/README.adoc rename to tests/squabbler-corpus/README.adoc index 4ff9e94..53f6d41 100644 --- a/corpus/README.adoc +++ b/tests/squabbler-corpus/README.adoc @@ -10,8 +10,8 @@ what fix is proposed, who owns it, and how to retry. * `schema/case.schema.json`: the case shape. * `cases/SQ-CASE-NNNN.json`: the cases (this patch adds 15). -* `validate.py`: offline checker (standard library only). Silent means the case is valid and scrubbed. -* `tests/run-tests.py`: fire and silence tests for the validator, offline. +* `check-case.sh` and `check-case.jq`: offline checker (POSIX shell and jq). Silent means the case is valid and scrubbed. +* `run-tests.sh`: fire and silence tests for the checker, offline. == Rules the validator enforces @@ -40,6 +40,6 @@ what fix is proposed, who owns it, and how to retry. == Run ---- -python3 -I tests/run-tests.py -python3 -I validate.py cases/*.json +tests/squabbler-corpus/run-tests.sh +tests/squabbler-corpus/check-case.sh tests/squabbler-corpus/cases/*.json ---- diff --git a/corpus/cases/SQ-CASE-0001.json b/tests/squabbler-corpus/cases/SQ-CASE-0001.json similarity index 100% rename from corpus/cases/SQ-CASE-0001.json rename to tests/squabbler-corpus/cases/SQ-CASE-0001.json diff --git a/corpus/cases/SQ-CASE-0002.json b/tests/squabbler-corpus/cases/SQ-CASE-0002.json similarity index 100% rename from corpus/cases/SQ-CASE-0002.json rename to tests/squabbler-corpus/cases/SQ-CASE-0002.json diff --git a/corpus/cases/SQ-CASE-0003.json b/tests/squabbler-corpus/cases/SQ-CASE-0003.json similarity index 100% rename from corpus/cases/SQ-CASE-0003.json rename to tests/squabbler-corpus/cases/SQ-CASE-0003.json diff --git a/corpus/cases/SQ-CASE-0004.json b/tests/squabbler-corpus/cases/SQ-CASE-0004.json similarity index 100% rename from corpus/cases/SQ-CASE-0004.json rename to tests/squabbler-corpus/cases/SQ-CASE-0004.json diff --git a/corpus/cases/SQ-CASE-0005.json b/tests/squabbler-corpus/cases/SQ-CASE-0005.json similarity index 100% rename from corpus/cases/SQ-CASE-0005.json rename to tests/squabbler-corpus/cases/SQ-CASE-0005.json diff --git a/corpus/cases/SQ-CASE-0006.json b/tests/squabbler-corpus/cases/SQ-CASE-0006.json similarity index 100% rename from corpus/cases/SQ-CASE-0006.json rename to tests/squabbler-corpus/cases/SQ-CASE-0006.json diff --git a/corpus/cases/SQ-CASE-0007.json b/tests/squabbler-corpus/cases/SQ-CASE-0007.json similarity index 100% rename from corpus/cases/SQ-CASE-0007.json rename to tests/squabbler-corpus/cases/SQ-CASE-0007.json diff --git a/corpus/cases/SQ-CASE-0008.json b/tests/squabbler-corpus/cases/SQ-CASE-0008.json similarity index 100% rename from corpus/cases/SQ-CASE-0008.json rename to tests/squabbler-corpus/cases/SQ-CASE-0008.json diff --git a/corpus/cases/SQ-CASE-0009.json b/tests/squabbler-corpus/cases/SQ-CASE-0009.json similarity index 100% rename from corpus/cases/SQ-CASE-0009.json rename to tests/squabbler-corpus/cases/SQ-CASE-0009.json diff --git a/corpus/cases/SQ-CASE-0010.json b/tests/squabbler-corpus/cases/SQ-CASE-0010.json similarity index 100% rename from corpus/cases/SQ-CASE-0010.json rename to tests/squabbler-corpus/cases/SQ-CASE-0010.json diff --git a/corpus/cases/SQ-CASE-0011.json b/tests/squabbler-corpus/cases/SQ-CASE-0011.json similarity index 100% rename from corpus/cases/SQ-CASE-0011.json rename to tests/squabbler-corpus/cases/SQ-CASE-0011.json diff --git a/corpus/cases/SQ-CASE-0012.json b/tests/squabbler-corpus/cases/SQ-CASE-0012.json similarity index 100% rename from corpus/cases/SQ-CASE-0012.json rename to tests/squabbler-corpus/cases/SQ-CASE-0012.json diff --git a/corpus/cases/SQ-CASE-0013.json b/tests/squabbler-corpus/cases/SQ-CASE-0013.json similarity index 100% rename from corpus/cases/SQ-CASE-0013.json rename to tests/squabbler-corpus/cases/SQ-CASE-0013.json diff --git a/corpus/cases/SQ-CASE-0014.json b/tests/squabbler-corpus/cases/SQ-CASE-0014.json similarity index 100% rename from corpus/cases/SQ-CASE-0014.json rename to tests/squabbler-corpus/cases/SQ-CASE-0014.json diff --git a/corpus/cases/SQ-CASE-0015.json b/tests/squabbler-corpus/cases/SQ-CASE-0015.json similarity index 100% rename from corpus/cases/SQ-CASE-0015.json rename to tests/squabbler-corpus/cases/SQ-CASE-0015.json diff --git a/tests/squabbler-corpus/check-case.jq b/tests/squabbler-corpus/check-case.jq new file mode 100644 index 0000000..1c5442c --- /dev/null +++ b/tests/squabbler-corpus/check-case.jq @@ -0,0 +1,91 @@ +# SPDX-License-Identifier: MPL-2.0 +# Rules for one squabbler corpus case. Input: the case object. +# Arg $fn: the file's basename (used for rule V003). +# Output: one line per finding, "CODE message". Silent means valid and scrubbed. + +def TOP: ["id","schema_version","repo","pr","run_id","job","step","attempt","observed_at", + "evidence","classification","main_comparison","fix","retry_policy","provenance"]; +def GROUPS: ["rate-limit","infra-flake","cancelled-timeout","lock-drift","stale-generated-file", + "real-failure","missing-tool","quality-gate"]; +def CONF: ["proven","likely","hypothesis"]; +def MAIN: ["passes","fails","absent","unknown"]; +def OWNERS: ["ci-gate-owner","pr-author","repo-owner","estate-owner","none"]; +def RISK: ["low","medium","high"]; +def RETRY: ["none","rerun","rerun-after-window","fix-then-rerun"]; +def READBY: ["log","api"]; + +def inlist($v; $list): ($list | any(. == $v)); +def isint: (type == "number") and (. == floor); +def isstr: type == "string"; +def isobj: type == "object"; +def nonempty_str: isstr and (length > 0); + +def SECRET: "(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|AKIA[0-9A-Z]{16}|-----BEGIN [A-Z ]*PRIVATE KEY-----|(?i:bearer)\\s+[A-Za-z0-9._~+/=-]{20,}|(?i:(?:token|secret|password|apikey)\\s*[:=]\\s*[^\\s\"']{8,}))"; +def EMAIL: "[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}\\b"; +def INTERNAL: "(\\b[\\w-]+\\.(?:internal|corp|local|lan|intranet)\\b|\\b(?:10|192\\.168)\\.\\d+\\.\\d+(?:\\.\\d+)?\\b|\\b172\\.(?:1[6-9]|2\\d|3[01])\\.\\d+\\.\\d+\\b)"; +def TS: "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"; + +def lines_of($s): ($s | split("\n") | if (.[-1] == "") then .[:-1] else . end | length); + +# Structural checks (V002). Each entry is [condition, message]. +def structural: + . as $c + | [ + [($c.schema_version == "1"), "schema_version must be \"1\""], + [(($c.id | isstr) and ($c.id | test("^SQ-CASE-[0-9]{4}$"))), "id must match SQ-CASE-NNNN"], + [(($c.repo | isstr) and ($c.repo | test("^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$"))), "repo must be owner/name"], + [(($c.pr == null) or ($c.pr | isint)), "pr must be integer or null (null = main)"], + [(($c.run_id == null) or ($c.run_id | isint)), "run_id must be integer or null"], + [(($c.attempt == null) or ($c.attempt | isint)), "attempt must be integer or null"], + [($c.job | nonempty_str), "job must be a non-empty string"], + [(($c.step == null) or ($c.step | isstr)), "step must be string or null"], + [(($c.evidence | isobj) and (($c.evidence | keys | sort) == (["error_strings","exit_code","log_excerpt"] | sort))), "evidence fields"], + [(($c.classification | isobj) and inlist($c.classification.group; GROUPS) and inlist($c.classification.confidence; CONF)), "classification group/confidence"], + [(($c.main_comparison | isobj) and inlist($c.main_comparison.state; MAIN) and ($c.main_comparison.note | isstr)), "main_comparison"], + [(($c.fix | isobj) and inlist($c.fix.owner; OWNERS) and inlist($c.fix.risk; RISK) + and (($c.fix.files | type) == "array") and (($c.fix.requires_owner_go | type) == "boolean") + and ($c.fix.description | isstr)), "fix fields"], + [(($c.retry_policy | isobj) and inlist($c.retry_policy.action; RETRY)), "retry_policy.action"], + [(($c.provenance | isobj) and inlist($c.provenance.read_by; READBY)), "provenance.read_by must be log or api"] + ]; + +def findings($fn): + . as $c + | if ($c | isobj | not) then [["V001", "case is not an object"]] + elif ((($c | keys | sort)) != (TOP | sort)) then + [["V001", "fields differ: missing \((TOP - ($c | keys)) | sort) extra \((($c | keys) - TOP) | sort)"]] + else + ($c | structural | [ .[] | select(.[0] | not) | ["V002", .[1]] ]) as $s + | if ($s | length) > 0 then $s + else + [ + (if ($c.observed_at != null) and ((($c.observed_at | isstr) and ($c.observed_at | test(TS))) | not) + then ["V011", "observed_at must be UTC YYYY-MM-DDThh:mm:ssZ or null"] else empty end), + (if ($fn != null) and ((($c.id) + ".json") != $fn) + then ["V003", "file name must equal the case id"] else empty end), + (if ($c.evidence.log_excerpt != null) + and ((($c.evidence.log_excerpt | isstr) | not) + or (lines_of($c.evidence.log_excerpt) > 30)) + then ["V004", "log_excerpt must be a string of at most 30 lines, or null"] else empty end), + (if (($c.evidence.error_strings | type) != "array") + or ([ $c.evidence.error_strings[] | (isstr and length > 0) ] | all | not) + then ["V002", "error_strings must be a list of non-empty strings"] + else + ($c.evidence.error_strings) as $strings + | ([ .. | strings ]) as $blob + | (if ($blob | any(test(SECRET))) then ["V005", "possible secret or token"] else empty end), + (if ($blob | any(test(EMAIL))) then ["V006", "email address"] else empty end), + (if ($blob | any(test(INTERNAL; "i"))) then ["V007", "internal hostname or private address"] else empty end), + (if ($c.classification.confidence == "proven") and ($strings | length == 0) + then ["V009", "proven needs at least one verbatim error string"] else empty end), + (if ($c.evidence.log_excerpt != null) + and ([ $strings[] | . as $s | ($c.evidence.log_excerpt | contains($s)) ] | all | not) + then ["V012", "error string not present in log_excerpt"] else empty end), + (if ($c.fix.owner != "none") and ($c.fix.requires_owner_go != true) + then ["V010", "a fix owned by someone needs requires_owner_go true"] else empty end) + end) + ] + end + end; + +findings($fn) | .[] | "\(.[0]) \(.[1])" diff --git a/tests/squabbler-corpus/check-case.sh b/tests/squabbler-corpus/check-case.sh new file mode 100755 index 0000000..6b95a7a --- /dev/null +++ b/tests/squabbler-corpus/check-case.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# SPDX-License-Identifier: MPL-2.0 +# Offline validator for squabbler corpus cases. Needs jq; no other dependencies. +# Usage: check-case.sh FILE.json [FILE.json ...] +# Exit 0 = every file silent. Exit 1 = at least one finding or unreadable file. +set -u + +here=$(cd "$(dirname "$0")" && pwd) +rules="$here/check-case.jq" + +if ! command -v jq >/dev/null 2>&1; then + echo "check-case.sh: jq is required" >&2 + exit 2 +fi +if [ "$#" -eq 0 ]; then + echo "usage: check-case.sh FILE.json [FILE.json ...]" >&2 + exit 2 +fi + +rc=0 +for path in "$@"; do + if ! jq -e . "$path" >/dev/null 2>&1; then + printf '%s: V000 unreadable or not valid JSON\n' "$path" + rc=1 + continue + fi + name=$(basename "$path") + if ! out=$(jq -r --arg fn "$name" -f "$rules" "$path"); then + printf '%s: V000 rule evaluation failed\n' "$path" + rc=1 + continue + fi + if [ -n "$out" ]; then + printf '%s\n' "$out" | while IFS= read -r line; do + printf '%s: %s\n' "$path" "$line" + done + rc=1 + fi +done +exit "$rc" diff --git a/tests/squabbler-corpus/run-tests.sh b/tests/squabbler-corpus/run-tests.sh new file mode 100755 index 0000000..b9aab29 --- /dev/null +++ b/tests/squabbler-corpus/run-tests.sh @@ -0,0 +1,75 @@ +#!/bin/sh +# SPDX-License-Identifier: MPL-2.0 +# Fire and silence tests for check-case.sh. Offline. Needs jq. +# Exit 0 only if every test behaves as expected. +set -u + +here=$(cd "$(dirname "$0")" && pwd) +checker="$here/check-case.sh" +base="$here/cases/SQ-CASE-0012.json" +work=$(mktemp -d "${TMPDIR:-/tmp}/squabbler-corpus.XXXXXX") +trap 'rm -rf "$work"' EXIT INT TERM + +n=0 +fails=0 +fail() { fails=$((fails + 1)); printf 'FAIL %s\n' "$1"; } + +# codes_for FILTER [BASENAME]: write the mutated base case, run the checker, print its rule codes. +codes_for() { + name=${2:-SQ-CASE-0012.json} + dir=$(mktemp -d "$work/c.XXXXXX") + jq "$1" "$base" > "$dir/$name" || { echo "BADFILTER"; return; } + "$checker" "$dir/$name" | sed -E 's/^.*: (V[0-9]{3}) .*$/\1/' | sort -u | tr '\n' ' ' | sed 's/ $//' +} + +# Fire tests: each mutation must produce exactly its own rule code. +fire() { + n=$((n + 1)) + got=$(codes_for "$2" "${3:-SQ-CASE-0012.json}") + if [ "$got" != "$1" ]; then fail "FIRE $1: expected exactly [$1], got [$got]"; fi +} + +# Silence tests: each near miss must produce no findings. +silent() { + n=$((n + 1)) + got=$(codes_for "$2") + if [ -n "$got" ]; then fail "SILENT '$1': expected no findings, got [$got]"; fi +} + +fire V001 'del(.job)' +fire V002 '.classification.group = "nonsense"' +# V003: the file name must equal the case id. Checked with a file named SQ-CASE-9999.json. +fire V003 '.' 'SQ-CASE-9999.json' +fire V004 '.evidence.log_excerpt = ([range(31) | "x"] | join("\n")) | .evidence.error_strings = ["x"]' +fire V005 '.fix.description = "use ghp_AAAAAAAAAAAAAAAAAAAAAAAA to push"' +fire V006 '.fix.description = "ask someone@example.org"' +fire V007 '.fix.description = "runner build01.internal is slow"' +fire V009 '.evidence.error_strings = [] | .classification.confidence = "proven"' +fire V010 '.fix.requires_owner_go = false' +fire V011 '.observed_at = "20 Nov 2026"' +fire V012 '.evidence.log_excerpt = "unrelated line" | .evidence.error_strings = ["not in the excerpt"]' + +silent "plain case" '.' +silent "hash-like text, no token prefix" '.fix.description = "see commit c519c452 and sha256 6cbeb457"' +silent "ghp inside a word" '.fix.description = "ghpath is a variable name"' +silent "private-looking but public host" '.fix.description = "archive.ubuntu.com timed out"' +silent "version number, not an address" '.fix.description = "upload-artifact v7.0.2 and 10.2 seconds"' +silent "none owner without owner go" '.fix.owner = "none" | .fix.requires_owner_go = false' +silent "null observed_at" '.observed_at = null' +silent "hypothesis without strings" '.evidence.error_strings = [] | .classification.confidence = "hypothesis"' +silent "30-line excerpt" '.evidence.log_excerpt = ([range(30) | "x"] | join("\n")) | .evidence.error_strings = ["x"]' +silent "action@version is not an email" '.fix.description = "actions/upload-artifact@v7.0.2 is missing"' +silent "pr null (main)" '.pr = null' + +# Real cases must stay silent, and there must be exactly 15 of them. +real=0 +for p in "$here"/cases/*.json; do + n=$((n + 1)) + real=$((real + 1)) + if ! out=$("$checker" "$p"); then fail "SILENT $(basename "$p"): $out"; fi +done +n=$((n + 1)) +if [ "$real" -ne 15 ]; then fail "expected 15 cases, found $real"; fi + +printf '%d/%d tests behaved as expected (%s)\n' "$((n - fails))" "$n" "11 fire, 11 near-miss silence, $real real cases silent" +[ "$fails" -eq 0 ] diff --git a/corpus/schema/case.schema.json b/tests/squabbler-corpus/schema/case.schema.json similarity index 98% rename from corpus/schema/case.schema.json rename to tests/squabbler-corpus/schema/case.schema.json index e017459..d96aad8 100644 --- a/corpus/schema/case.schema.json +++ b/tests/squabbler-corpus/schema/case.schema.json @@ -1,7 +1,7 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Squabbler corpus case", - "description": "One observed CI failure and its classification. validate.py enforces this plus the scrub rules.", + "description": "One observed CI failure and its classification. check-case.jq enforces this plus the scrub rules.", "type": "object", "additionalProperties": false, "required": ["id","schema_version","repo","pr","run_id","job","step","attempt","observed_at","evidence","classification","main_comparison","fix","retry_policy","provenance"],