Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/path-hermetic-lint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Added
- scripts/path_hermetic_lint.py flags a negative grep (`! grep ... worktrees ...`) in scripts/test-*.sh whose pattern is a path-segment word that also appears in the checkout path, unless the line or the one before it strips $ROOT/$PWD. Such a test passes in CI and fails in a lane worktree. Existing offenders are frozen in scripts/path-hermetic-baseline.tsv (one row today, fixed by an open PR); the regression test is registered through ci-tests.d.
2 changes: 2 additions & 0 deletions changelog.d/scan-summary-worktree-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Fixed
- The scan-summary tests no longer fail when the checkout lives under a `.claude/worktrees/` path (every lane worktree): the "default excludes" check matched `worktrees` in the absolute `--config` path the tool logs, not in a scanned file. The check now strips the checkout path, and the hermetic suite runs the test from a worktrees-style path so the regression stays caught. The failure was blamed on the host semgrep cache; it was not.
2 changes: 2 additions & 0 deletions scripts/ci-tests.d/path-hermetic-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Registers scripts/test_path_hermetic_lint.py with CI (see the "Per-PR tests" step in ci.yml).
python3 scripts/test_path_hermetic_lint.py
3 changes: 3 additions & 0 deletions scripts/path-hermetic-baseline.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# path<TAB>stripped line text. Frozen offenders; fixing one means deleting its row. Reviewed: pentest-load HOME row is an anchored literal subdir, ci-gates row is a full phrase, neither can match a checkout path.
scripts/test-app-pentest-load.sh grep -qE '^HOME=.*/ap\.[A-Za-z0-9]+$' "$T/fake/env" && ! grep -q "^HOME=$T/home" "$T/fake/env" && grep -qF "PATH=$T/fake:/usr/bin:/bin" "$T/fake/env"; ok $? "k6 HOME is the run's temp dir; PATH is passed on"
scripts/test-ci-gates.sh && ! grep -q 'DISCRIMINATE FAIL deep-code-review' "$WORK/last.log"; then
92 changes: 92 additions & 0 deletions scripts/path_hermetic_lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
"""path_hermetic_lint.py - a negative grep for a path-segment word is not hermetic: the checkout path is part of every absolute path in a log.

WHY: `! grep -E 'worktrees' "$LOG"` passes in CI (checkout /home/runner/work/...) but fails in a lane worktree
(/repo/.claude/worktrees/lane/...), because the scanner argv/log holds absolute paths that contain the word.
Flags, in scripts/test-*.sh, every `! grep` command segment (a line is split on ; && || | outside quotes) whose literal,
non-$var text holds a bare path-segment word (worktrees, Desktop, Users, home, tmp, var, ...) as a whole word, unless the
grep's input (its last arg) is written, on that line or the one before, by a sed/`${x//...}`/replace that uses $ROOT/$PWD.
Existing offenders are frozen in scripts/path-hermetic-baseline.tsv (`path<TAB>stripped line text`; a row whose line no longer
flags also fails, so the baseline only shrinks). Usage: path_hermetic_lint.py [root]. Exit 0 clean, 1 on a violation.
Side effects: none. Edge case: a pattern built from a variable is not seen (false negative, accepted).
"""
import glob
import os
import re
import sys
from pathlib import Path

WORDS = {"worktree", "worktrees", "desktop", "users", "home", "tmp", "private", "var", "checkout", "deep-code-review", "documents"}
STRIP = re.compile(r"(\bsed\b|\$\{[^}]*//|\breplace\b)")
ROOTS = re.compile(r"\$\{?(ROOT|PWD|REPO\w*|CHECKOUT\w*|SRC)\b|\$\(pwd\)|\$\(git rev-parse --show-toplevel\)")


def segments(line):
"""Split a shell line on ; && || | outside quotes."""
out, cur, q, i = [], "", "", 0
while i < len(line):
c = line[i]
if q:
q = "" if c == q else q
elif c in "\"'":
q = c
elif (c == "|" and cur.endswith(">")) is False and c in ";|" or (c == "&" and line[i:i + 2] == "&&"):
out.append(cur)
cur = ""
i += 2 if line[i:i + 2] in ("&&", "||") else 1
continue
cur += c
i += 1
return out + [cur]


def hit_words(seg):
"""Return the WORDS found as whole words in the literal (non-$var) text of a `! grep` segment, else []."""
seg = seg.strip()
if not re.match(r"!\s+grep\b", seg):
return []
lit = re.sub(r"\$\{[^}]*\}|\$\([^)]*\)|\$\w+", " ", seg).lower()
lit = re.sub(r"(?<=\s)-[a-z]*?e(?=\w)", r"\g<0> ", lit) # -ePATTERN / -qePATTERN glue
return [w for w in WORDS if re.search(r"(?<![\w-])" + re.escape(w) + r"(?![\w-])", lit)]


def exempt(seg, lines):
"""True if a non-grep segment on this or the previous line has STRIP and ROOTS and redirects (> f, >f, >| f) exactly into the grep's input file (last arg, quotes stripped)."""
f = seg.split()[-1].strip("\"'")
for l in lines:
for s in segments(l):
if s.strip().startswith("! grep") or not (STRIP.search(s) and ROOTS.search(s)):
continue
if any(t.strip("\"'") == f for t in re.findall(r"(?<![>&\d])>\|?\s*(\"[^\"]*\"|'[^']*'|[^\s;&|]+)", s)):
return True
return False


def lint(root="."):
"""Return (new, stale): new = `path:line` of unexempt hits whose `path<TAB>text` key is not in the baseline, stale = baseline keys that no longer hit."""
with open(os.path.join(root, "scripts/path-hermetic-baseline.tsv"), encoding="utf-8") as f: # missing = fail closed
base = {l.rstrip("\n") for l in f if l.strip() and not l.startswith("#")}
hits = {}
for p in sorted(glob.glob(os.path.join(root, "scripts/test-*.sh"))):
rel = os.path.relpath(p, root)
ls = Path(p).read_text(encoding="utf-8").splitlines()
for n, l in enumerate(ls):
if l.lstrip().startswith("#"):
continue
for seg in segments(l):
if hit_words(seg) and not exempt(seg, [l] + ls[n - 1:n]):
hits[f"{rel}\t{l.strip()}"] = f"{rel}:{n + 1}"
return sorted(v for k, v in hits.items() if k not in base), sorted(base - set(hits))


def main(argv):
new, stale = lint(argv[1] if len(argv) > 1 else ".")
for k in new:
print(f"path_hermetic_lint: {k} has a `! grep` for a path-segment word that is also in the checkout path; strip $ROOT/$PWD first (sed \"s|$ROOT||g\")", file=sys.stderr)
for k in stale:
print(f"path_hermetic_lint: baseline row no longer flags (delete it from scripts/path-hermetic-baseline.tsv): {k.replace(chr(9), ' :: ')}", file=sys.stderr)
return 1 if new or stale else 0


if __name__ == "__main__":
sys.exit(main(sys.argv))
9 changes: 9 additions & 0 deletions scripts/test-review-checks-scan-summary-hermetic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ else
grep '^FAIL' "$T/out" || tail -5 "$T/out"
echo "FAILED: the scan-summary test depends on the host's semgrep cache"; exit 1
fi
# Same for the checkout path: lanes run from .claude/worktrees/<name>, so "worktrees" sits in every absolute path the tool logs.
mkdir -p "$T/.claude/worktrees/w"; ln -s "$ROOT/scripts" "$T/.claude/worktrees/w/scripts"
mkdir "$T/.claude/worktrees/w/.claude"; ln -s "$ROOT/.claude/skills" "$T/.claude/worktrees/w/.claude/skills"
if bash "$T/.claude/worktrees/w/scripts/test-review-checks-scan-summary.sh" >"$T/out2" 2>&1; then
echo "ok: the scan-summary test passes from a checkout path containing 'worktrees'"
else
grep '^FAIL' "$T/out2" || tail -5 "$T/out2"
echo "FAILED: the scan-summary test depends on the checkout path"; exit 1
fi
4 changes: 3 additions & 1 deletion scripts/test-review-checks-scan-summary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ mkdir -p node_modules/p .claude/worktrees/w vendor
printf 'x\n' >node_modules/p/a.txt; printf 'x\n' >.claude/worktrees/w/b.txt; printf 'x\n' >vendor/c.txt; printf 'x\n' >real.txt
git add -f . && git -c user.email=t@example.com -c user.name=t commit -q -m vendored
: >"$GL_LOG"; : >"$SG_LOG"; run
grep -q 'real.txt' "$GL_LOG" && ! grep -Eq 'node_modules|worktrees|vendor' "$GL_LOG" "$SG_LOG" && ok "default excludes" || bad "default excludes"
# Strip the checkout path first: a lane worktree (.claude/worktrees/...) puts "worktrees" in the absolute --config path.
cat "$GL_LOG" "$SG_LOG" | sed "s|$ROOT||g" >"$T/args.log"
grep -q 'real.txt' "$GL_LOG" && ! grep -Eq 'node_modules|worktrees|vendor' "$T/args.log" && ok "default excludes" || bad "default excludes"
grep -q -- '--skip-dirs node_modules,.claude/worktrees,vendor' "$TV_LOG" && ok "trivy skip-dirs" || bad "trivy skip-dirs"

# Heavy lease: with one slot (tokens=off) taken by a live holder, REVIEW_LEASE_FULL=skip skips heavy scanners (default: degraded, see test-review-checks-lease-degraded.sh).
Expand Down
69 changes: 69 additions & 0 deletions scripts/test_path_hermetic_lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""Tests for path_hermetic_lint.py: a bare path-word negative grep fails, a $ROOT-stripped or non-path one passes, a baselined one is exempt; the repo is clean."""
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "scripts"))
import path_hermetic_lint as p # noqa: E402

BAD = '! grep -Eq "node_modules|worktrees|vendor" "$LOG"\n'
GOOD = 'sed "s|$ROOT||g" "$LOG" >"$LOG.rel"\n! grep -Eq "worktrees" "$LOG.rel"\n'


def tree(body, baseline=""):
d = Path(tempfile.mkdtemp())
(d / "scripts").mkdir()
(d / "scripts/path-hermetic-baseline.tsv").write_text(baseline)
(d / "scripts/test-x.sh").write_text(body)
return str(d)


class Lint(unittest.TestCase):
def test_bad_fixture_fails(self):
self.assertEqual(p.lint(tree(BAD)), (["scripts/test-x.sh:1"], []))

def test_reviewer_cases_fail(self):
for body in (
'! grep -q fatal "$L"; ! grep -q worktrees "$L"\n', # 2nd grep on a line
'! grep -q "unterminated worktrees "$L\n', # unbalanced quote
'! grep -qE -e worktrees "$L"\n', # -e not last
'! grep -m 1 -A 2 worktrees "$L"\n', # -m N / -A N
'! grep --regexp=worktrees "$L"\n',
'! grep -eworktrees "$L"\n',
'! grep -qeworktrees "$L"\n', # glued combined flags
'sed "s|$ROOT||g" "$LOG" >"$LOG2"\n! grep -q worktrees "$LOG"\n', # sed writes $LOG2, not $LOG
'sed "s|$ROOT||g" "$LOG" | tee x\n! grep -q worktrees "$LOG"\n', # read-only sed
'sed "s|$ROOT||g" "$A" >"$A.rel"\n! grep -q worktrees "$L"\n', # sed unrelated to the input
):
self.assertEqual(p.lint(tree(body))[0], ["scripts/test-x.sh:%d" % (body.count("\n"))], body)

def test_good_fixture_passes(self):
self.assertEqual(p.lint(tree(GOOD)), ([], []))
self.assertEqual(p.lint(tree('! grep -q "$ROOT" "$L"; ! grep -q fatal "$L"\n# ! grep -q worktrees "$L"\n')), ([], []))

def test_baseline_exempts_then_goes_stale(self):
k = "scripts/test-x.sh\t" + BAD.strip() + "\n"
self.assertEqual(p.lint(tree(BAD, k)), ([], []))
self.assertEqual(p.lint(tree(GOOD, k)), ([], [k.strip()]))
# a new offender in a baselined file is not covered by the row
self.assertEqual(p.lint(tree(BAD + '! grep -q home "$L"\n', k)), (["scripts/test-x.sh:2"], []))

def test_missing_baseline_fails_closed(self):
r = tree(GOOD)
os.remove(os.path.join(r, "scripts/path-hermetic-baseline.tsv"))
with self.assertRaises(OSError):
p.lint(r)

def test_repo_is_clean(self):
r = subprocess.run([sys.executable, "-I", str(ROOT / "scripts/path_hermetic_lint.py"), str(ROOT)], capture_output=True, text=True,
env={"PATH": os.environ["PATH"]})
self.assertEqual(r.returncode, 0, r.stderr)


if __name__ == "__main__":
unittest.main()
Loading