fix: open agent file links that carry line-range selectors (#1412)
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
// Package readselector parses Oh-My-Pi (omp) style read paths of the form
|
||||
// "path:<selector>" into the bare file path plus the parsed line range.
|
||||
//
|
||||
// OMP's read tool embeds a line/range/mode selector in the path argument
|
||||
// (e.g. "foo.go:43-94", "foo.go:50+150", "foo.go:5-16,960-973", "foo.go:raw",
|
||||
// "foo.go:2-4:raw"). Kandev opens files by stat'ing the path, so a selector
|
||||
// left on the path makes the open fail ("Failed to open file … no such file"
|
||||
// for workspace files, or "path traversal detected" for absolute external
|
||||
// files whose stat'd "<path>:<range>" simply doesn't exist).
|
||||
//
|
||||
// Both the ACP normalizer (so new file links are stored clean and the line
|
||||
// range is surfaced separately) and the file-open boundary (so already-persisted
|
||||
// links and any normalization gap still open) call Split — a single source of
|
||||
// truth shared by both.
|
||||
package readselector
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Split splits "path:<selector>" into the bare file path plus the parsed line
|
||||
// range. startLine is the first line referenced by the selector; lineCount is
|
||||
// the contiguous span when a closed range is given ("N-M" or "N+K"), and 0 when
|
||||
// the selector is open-ended ("N", "N-") or carries no line numbers ("raw").
|
||||
// When no valid selector is present the path is returned unchanged with zero
|
||||
// line info, so paths from non-omp agents (which never match the grammar) and
|
||||
// ordinary filenames are unaffected.
|
||||
func Split(raw string) (path string, startLine, lineCount int) {
|
||||
// Only inspect colons in the final path segment so a directory component
|
||||
// that legitimately contains a colon is never mistaken for a selector.
|
||||
// Consider both '/' and '\\' so Windows paths — and their drive-letter
|
||||
// colon, e.g. "C:\\dir\\file.go" — are not misread as a selector boundary.
|
||||
lastSep := strings.LastIndexByte(raw, '/')
|
||||
if b := strings.LastIndexByte(raw, '\\'); b > lastSep {
|
||||
lastSep = b
|
||||
}
|
||||
rel := strings.IndexByte(raw[lastSep+1:], ':')
|
||||
if rel < 0 {
|
||||
return raw, 0, 0
|
||||
}
|
||||
colon := lastSep + 1 + rel
|
||||
base, suffix := raw[:colon], raw[colon+1:]
|
||||
if base == "" || suffix == "" {
|
||||
return raw, 0, 0
|
||||
}
|
||||
start, count, ok := parseReadSelector(suffix)
|
||||
if !ok {
|
||||
return raw, 0, 0
|
||||
}
|
||||
return base, start, count
|
||||
}
|
||||
|
||||
// parseReadSelector validates the full selector tail (everything after the
|
||||
// first ':' in the final segment). Selector parts are joined by ':' so combos
|
||||
// such as "2-4:raw" and "raw:2-4" are accepted; each part must be a line-spec
|
||||
// list or a recognized mode keyword. The first line-spec encountered drives the
|
||||
// returned startLine/lineCount.
|
||||
func parseReadSelector(suffix string) (startLine, lineCount int, ok bool) {
|
||||
gotLine := false
|
||||
for _, part := range strings.Split(suffix, ":") {
|
||||
if part == "raw" || part == "conflicts" {
|
||||
continue
|
||||
}
|
||||
s, c, valid := parseLineSpecList(part)
|
||||
if !valid {
|
||||
return 0, 0, false
|
||||
}
|
||||
if !gotLine {
|
||||
startLine, lineCount, gotLine = s, c, true
|
||||
}
|
||||
}
|
||||
return startLine, lineCount, true
|
||||
}
|
||||
|
||||
// parseLineSpecList parses a comma-separated list of line specs ("5-16,960-973")
|
||||
// and reports the start/count of the first spec.
|
||||
func parseLineSpecList(part string) (startLine, lineCount int, ok bool) {
|
||||
segs := strings.Split(part, ",")
|
||||
for i, seg := range segs {
|
||||
s, c, valid := parseLineSpec(seg)
|
||||
if !valid {
|
||||
return 0, 0, false
|
||||
}
|
||||
if i == 0 {
|
||||
startLine, lineCount = s, c
|
||||
}
|
||||
}
|
||||
return startLine, lineCount, true
|
||||
}
|
||||
|
||||
// parseLineSpec parses a single line spec: "N", "N-", "N-M", or "N+K".
|
||||
func parseLineSpec(seg string) (startLine, lineCount int, ok bool) {
|
||||
if i := strings.IndexByte(seg, '+'); i >= 0 {
|
||||
start, errA := strconv.Atoi(seg[:i])
|
||||
count, errB := strconv.Atoi(seg[i+1:])
|
||||
if errA != nil || errB != nil || start <= 0 || count < 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
return start, count, true
|
||||
}
|
||||
if i := strings.IndexByte(seg, '-'); i >= 0 {
|
||||
start, errA := strconv.Atoi(seg[:i])
|
||||
if errA != nil || start <= 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
rest := seg[i+1:]
|
||||
if rest == "" { // "N-" — open-ended
|
||||
return start, 0, true
|
||||
}
|
||||
end, errB := strconv.Atoi(rest)
|
||||
if errB != nil || end < start {
|
||||
return 0, 0, false
|
||||
}
|
||||
return start, end - start + 1, true
|
||||
}
|
||||
start, err := strconv.Atoi(seg)
|
||||
if err != nil || start <= 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
return start, 0, true
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package readselector
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
wantPath string
|
||||
wantStart int
|
||||
wantCount int
|
||||
}{
|
||||
{"no selector", "config.json", "config.json", 0, 0},
|
||||
{"single line", "apps/web/lib/utils.ts:50", "apps/web/lib/utils.ts", 50, 0},
|
||||
{"open ended", "main.go:50-", "main.go", 50, 0},
|
||||
{"closed range", "apps/backend/internal/sentry/handlers.go:43-94", "apps/backend/internal/sentry/handlers.go", 43, 52},
|
||||
{"plus span", "main.go:50+150", "main.go", 50, 150},
|
||||
{"anchor", "main.go:20+1", "main.go", 20, 1},
|
||||
{"multi range", "main.go:5-16,960-973", "main.go", 5, 12},
|
||||
{"raw mode", "main.go:raw", "main.go", 0, 0},
|
||||
{"conflicts mode", "main.go:conflicts", "main.go", 0, 0},
|
||||
{"range then raw", "main.go:2-4:raw", "main.go", 2, 3},
|
||||
{"raw then range", "main.go:raw:2-4", "main.go", 2, 3},
|
||||
{"absolute path", "/home/u/.kandev/x/handlers.go:43-94", "/home/u/.kandev/x/handlers.go", 43, 52},
|
||||
{"absolute external", "/home/clem/.claude/CLAUDE.md:93-113", "/home/clem/.claude/CLAUDE.md", 93, 21},
|
||||
{"no extension single line", "Makefile:10", "Makefile", 10, 0},
|
||||
// Real OMP reads observed in the wild (multi-range, zero-length range,
|
||||
// range+mode combo, and a "~"-prefixed home path).
|
||||
{"omp multi range zero-len", "x/README.md:16-20,32-40,69-69,85-96", "x/README.md", 16, 5},
|
||||
{"omp range plus raw combo", "x/README.md:69+1:raw", "x/README.md", 69, 1},
|
||||
{"omp tilde multi range", "~/.kandev/x/README.md:27-28,35-36,66-66", "~/.kandev/x/README.md", 27, 2},
|
||||
// Windows path: drive-letter colon must not be read as the selector
|
||||
// boundary; the trailing "\\" segment + ":43-94" selector is stripped.
|
||||
{"windows absolute", `C:\Users\me\handlers.go:43-94`, `C:\Users\me\handlers.go`, 43, 52},
|
||||
// "N+0" is intentionally accepted (count 0, anchor at N) and stripped, so
|
||||
// the link still opens; rejecting it would leave the selector on the path.
|
||||
{"plus zero anchors", "main.go:50+0", "main.go", 50, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path, start, count := Split(tt.raw)
|
||||
if path != tt.wantPath {
|
||||
t.Errorf("path = %q, want %q", path, tt.wantPath)
|
||||
}
|
||||
if start != tt.wantStart {
|
||||
t.Errorf("startLine = %d, want %d", start, tt.wantStart)
|
||||
}
|
||||
if count != tt.wantCount {
|
||||
t.Errorf("lineCount = %d, want %d", count, tt.wantCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSplit_NoFalsePositives verifies that paths whose colon suffix is not a
|
||||
// valid omp selector are returned untouched, so legitimate filenames and
|
||||
// non-omp agent paths never lose data.
|
||||
func TestSplit_NoFalsePositives(t *testing.T) {
|
||||
unchanged := []string{
|
||||
"",
|
||||
"src/file.ts",
|
||||
"foo.go:bar", // non-numeric, non-keyword suffix
|
||||
"foo.go:1.2", // float-like, not a line spec
|
||||
`C:\Users\me\a.go`, // windows drive letter: suffix is not a line spec
|
||||
"dir:1/file.go", // colon lives in a directory component, not the file
|
||||
":43-94", // empty base
|
||||
"main.go:", // empty selector
|
||||
"main.go:-5", // negative / malformed start
|
||||
"main.go:0", // zero is not a valid 1-based line
|
||||
"main.go:10-5", // descending range
|
||||
"main.go:5,abc", // one invalid segment invalidates the list
|
||||
}
|
||||
for _, raw := range unchanged {
|
||||
t.Run(raw, func(t *testing.T) {
|
||||
path, start, count := Split(raw)
|
||||
if path != raw || start != 0 || count != 0 {
|
||||
t.Errorf("Split(%q) = (%q, %d, %d), want (%q, 0, 0)", raw, path, start, count, raw)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user