Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #36.
This commit is contained in:
@@ -783,23 +783,39 @@ func trimQuotes(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// trivialProbe flags a technique command that is just a one-shot read of a
|
||||
// file or system state (ls/cat/head/…, or a non-recursive grep) with no
|
||||
// pipeline, redirection, or diagnostic tooling. These carry no transferable
|
||||
// method, so they are junk (hidden by default) while real diagnostics — an ssh
|
||||
// probe, a dmesg/journalctl check, a recursive search, a test run — are kept.
|
||||
// trivialProbe flags a technique command that is just a read of a file or
|
||||
// system state (ls/cat/head/git log/…, or a non-recursive grep) with no
|
||||
// diagnostic tooling. These carry no transferable method, so they are junk
|
||||
// (hidden by default) while real work — an ssh probe, a dmesg/journalctl check,
|
||||
// a recursive search, a test run — is kept.
|
||||
//
|
||||
// Every segment counts, including segments on their own line: a multi-line
|
||||
// sequence of inspections is still an inspection. It used to bail out on any
|
||||
// newline, which kept whole transcripts of `cd … && cat …` chores as candidates.
|
||||
// Deliberately conservative: when in doubt, keep it.
|
||||
func trivialProbe(cmd string) bool {
|
||||
cmd = strings.TrimSpace(cmd)
|
||||
if cmd == "" || strings.Contains(cmd, "\n") {
|
||||
if cmd == "" {
|
||||
return false
|
||||
}
|
||||
if strings.Contains(cmd, "$(") || strings.Contains(cmd, "`") {
|
||||
return false // command substitution runs something
|
||||
}
|
||||
if selectsHistory(cmd) {
|
||||
return false // context flags turn a read into an investigation
|
||||
}
|
||||
return trivialTokens(shellTokens(scrubRedirects(cmd)))
|
||||
}
|
||||
|
||||
// historyFlagRe matches flags that make a read an *investigation*: grep context
|
||||
// lines (-A/-B/-C), git log -p/--follow/--graph/--since. A `git log -p | grep
|
||||
// -B 50` archaeology chain is transferable method, not a chore, so it is kept.
|
||||
var historyFlagRe = regexp.MustCompile(`(^|\s)(-[ABC][0-9]*|-[pF]|--since|--until|--graph|--author|--follow|--all)(\s|$)`)
|
||||
|
||||
func selectsHistory(cmd string) bool {
|
||||
return historyFlagRe.MatchString(cmd)
|
||||
}
|
||||
|
||||
func scrubRedirects(s string) string {
|
||||
return strings.NewReplacer(
|
||||
"2>&1", "", "2>/dev/null", "", "&>/dev/null", "", ">/dev/null", "",
|
||||
@@ -872,9 +888,26 @@ func trivialSSH(args []string) bool {
|
||||
}
|
||||
|
||||
// trivialReader reports whether a single command is a read-only local inspection.
|
||||
// grep/rg count only when non-recursive; docker/podman read subcommands count.
|
||||
// grep/rg count only when non-recursive; docker/podman read subcommands count,
|
||||
// and git counts only for subcommands that cannot mutate a repository.
|
||||
func trivialReader(base string, args []string) bool {
|
||||
switch base {
|
||||
case "sed":
|
||||
for _, tok := range args[1:] {
|
||||
if strings.HasPrefix(tok, "-") && strings.Contains(tok, "i") {
|
||||
return false // in-place edit
|
||||
}
|
||||
}
|
||||
return true
|
||||
case "git":
|
||||
if len(args) >= 2 {
|
||||
switch args[1] {
|
||||
case "status", "log", "diff", "show", "rev-parse", "describe",
|
||||
"blame", "shortlog", "ls-files", "grep", "cat-file", "reflog":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
case "ls", "cat", "head", "tail", "pwd", "which", "whoami", "echo", "printf",
|
||||
"wc", "file", "stat", "du", "df", "readlink", "realpath", "env", "printenv",
|
||||
"id", "true", "cd", "basename", "dirname", "sort", "uniq", "cut", "tr",
|
||||
@@ -952,9 +985,10 @@ func shellTokens(s string) []string {
|
||||
} else {
|
||||
toks = append(toks, "&")
|
||||
}
|
||||
case ';':
|
||||
case ';', '\n':
|
||||
flush()
|
||||
toks = append(toks, ";")
|
||||
case '\r':
|
||||
case '>':
|
||||
flush()
|
||||
if i+1 < len(s) && s[i+1] == '>' {
|
||||
|
||||
@@ -166,3 +166,39 @@ func TestSnippetExtractorDoesNotDoubleCountV1AndV2(t *testing.T) {
|
||||
t.Fatalf("projects = %v, want demo and old", seen)
|
||||
}
|
||||
}
|
||||
|
||||
// trivialProbe decides what a "technique" candidate is: an inspection chore
|
||||
// (hidden) versus transferable method (kept). Multi-line commands used to be
|
||||
// exempted wholesale, which kept every transcript of `cd … && cat …` chores.
|
||||
func TestTrivialProbe(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
cmd string
|
||||
want bool
|
||||
}{
|
||||
{"single read", "ls -la /tmp", true},
|
||||
{"chained reads", "cd /repo && sed -n '60,200p' tools/deploy.py", true},
|
||||
{"multi-line reads", "ls -la\nsed -n '1,20p' main.go\nwc -l main.go", true},
|
||||
{"git chores", "git status --short && git log --oneline -3", true},
|
||||
{"show file", "git show 1d244cf:engine/scroll.py | head -n 100", true},
|
||||
{"pwd", "pwd && git rev-parse --show-toplevel", true},
|
||||
|
||||
{"test run", "go test ./... 2>&1 | tail -6", false},
|
||||
{"archaeology", `git log --all -p --since="3 months ago" -- engine/app.py | grep -B 50 "class Camera"`, false},
|
||||
{"context grep", `grep -A 5 "def main" engine/app.py`, false},
|
||||
{"ssh diagnostic", "ssh host 'dmesg | grep dwc3'", false},
|
||||
{"ssh read", "ssh host 'ls /usr/src'", true},
|
||||
{"heredoc script", "python3 - <<'PY'\nprint(1)\nPY", false},
|
||||
{"in-place edit", "sed -i 's/a/b/' main.go", false},
|
||||
{"write", "git push origin main", false},
|
||||
{"redirect", "cat > out.txt <<EOF\nx\nEOF", false},
|
||||
{"substitution", "echo $(date)", false},
|
||||
{"compose up", "docker compose up -d", false},
|
||||
{"empty", "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := trivialProbe(c.cmd); got != c.want {
|
||||
t.Errorf("%s: trivialProbe(%q)=%v, want %v", c.name, c.cmd, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user