Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(gitlink): preserve unknown shell command and operand positions (#915)

Rudy Mizrahi Celekli4ef9cfb25e59
diff --git a/cli/beacon/internal/gitlink/shell.go b/cli/beacon/internal/gitlink/shell.go
index b884b2b7..3f9c9802 100644
--- a/cli/beacon/internal/gitlink/shell.go
+++ b/cli/beacon/internal/gitlink/shell.go
@@ -114,9 +114,17 @@ func stripRedirections(words []shellWord) (rest, targets []string) {
op, target = redirection(w.text)
}
if op == "" {
- if !w.expands {
- rest = append(rest, w.text)
+ text := w.text
+ if w.expands {
+ // Keep unknown words in place: deleting one can promote an argument
+ // to the command, or a copy source to the destination.
+ text = ""
+ if eq := strings.IndexByte(w.text, '='); w.assignment && len(unwrappedCommand(rest)) == 0 {
+ // An assignment's value is irrelevant when unwrapping the command.
+ text = w.text[:eq+1]
+ }
}
+ rest = append(rest, text)
continue
}
if target == "" && i+1 < len(words) {
@@ -202,24 +210,26 @@ func splitShellSegments(command string) []string {
// shellWord is one word of a simple command. quoted records that some of it was quoted, which
// stops it being read as an operator; expands records that its value depends on an expansion ($,
// `, a glob), which makes it unknown here -- such a word is never reported as a path.
+// assignment means an unquoted shell identifier and equals sign preceded its value.
type shellWord struct {
- text string
- quoted bool
- expands bool
+ text string
+ quoted bool
+ expands bool
+ assignment bool
}
// shellWords splits a simple command into words, honoring quotes and backslashes.
func shellWords(segment string) []shellWord {
var words []shellWord
var cur strings.Builder
- inWord, expands, quoted := false, false, false
+ inWord, expands, quoted, assignment := false, false, false, false
quote := byte(0)
flush := func() {
if inWord {
- words = append(words, shellWord{text: cur.String(), quoted: quoted, expands: expands})
+ words = append(words, shellWord{text: cur.String(), quoted: quoted, expands: expands, assignment: assignment})
}
cur.Reset()
- inWord, expands, quoted = false, false, false
+ inWord, expands, quoted, assignment = false, false, false, false
}
for i := 0; i < len(segment); i++ {
c := segment[i]
@@ -254,6 +264,9 @@ func shellWords(segment string) []shellWord {
case c == '(' || c == ')' || c == '{' || c == '}':
flush()
default:
+ if c == '=' && !quoted && shellAssignmentName(cur.String()) {
+ assignment = true
+ }
if c == '$' || c == '`' || c == '*' || c == '?' || c == '[' || c == '~' {
expands = true
}
@@ -265,6 +278,19 @@ func shellWords(segment string) []shellWord {
return words
}
+// shellAssignmentName recognizes the unquoted name before an assignment's equals sign.
+func shellAssignmentName(name string) bool {
+ if name == "" {
+ return false
+ }
+ for i, c := range name {
+ if c != '_' && !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(i > 0 && c >= '0' && c <= '9') {
+ return false
+ }
+ }
+ return true
+}
+
// PatchPaths returns the files an apply_patch envelope touches: Add, Update, Delete, and the
// destination of a Move.
func PatchPaths(patch string) []string {
diff --git a/cli/beacon/internal/gitlink/shell_test.go b/cli/beacon/internal/gitlink/shell_test.go
index 15ce3655..a5aac50d 100644
--- a/cli/beacon/internal/gitlink/shell_test.go
+++ b/cli/beacon/internal/gitlink/shell_test.go
@@ -32,7 +32,20 @@ func TestShellWrittenPaths(t *testing.T) {
{`rm "file with spaces.txt"`, []string{"file with spaces.txt"}},
{`rm file\ two.txt`, []string{"file two.txt"}},
{"echo '> not a redirect' ; ls", nil},
- {"rm $TARGET", nil}, // unknown value
+ {"rm $TARGET", nil}, // unknown value
+ {`"$TOOL" rm victim.txt`, nil},
+ {`command "$TOOL" rm victim.txt`, nil},
+ {`cp first.txt second.txt "$DEST"`, nil},
+ {`cp "$SOURCE" output.txt`, []string{"output.txt"}},
+ {`rm known.txt "$UNKNOWN"`, []string{"known.txt"}},
+ {`"$TOOL" rm victim.txt > known.log`, []string{"known.log"}},
+ {`ROOT="$DIR" cd sub && rm relative.txt`, nil},
+ {`ROOT="$DIR" rm known.txt`, []string{"known.txt"}},
+ {`"ROOT=$DIR" rm victim.txt`, nil},
+ {`"$TOOL=x" rm victim.txt`, nil},
+ {`"ROOT"=$DIR rm victim.txt`, nil},
+ {`ROOT\=$DIR rm victim.txt`, nil},
+ {`ROOT=$DIR rm known.txt`, []string{"known.txt"}},
{"rm *.log", nil}, // glob
{"rm \"$(pwd)/x\"", nil}, // expansion inside quotes
{"(cd sub && rm x)", nil}, // x is sub/x, which this parser does not follow