Skip to content

Commit 07e4d62

Browse files
committed
fix: correct symlink sizing, FIFO hang, UTF-8 truncation, and skip accounting
Addresses mesutoezdil's review findings on PR #2267. Six were real bugs; all are regression-tested (each new test verified to fail against the pre-fix code). Go: - ListDirContent reported a symlink's own size -- the byte length of its stored target path -- because entry.Info() is Lstat-based. It now stats the target, so a symlinked file reports the file's size and a broken link is listed bare, matching Python's pathlib behavior. The needed os.Stat result was already being computed and discarded. - ReadFileContent had no regular-file guard, so read_file on a FIFO with no writer blocked forever with no timeout on the path. It now rejects non-regular files, as GrepContent already did. - Line truncation sliced bytes, not runes. Beyond emitting invalid UTF-8 from a split sequence, it cut CJK text at ~668 characters rather than the 2000 the tool descriptions promise. Both sites now share a truncateRunes helper that cuts on a rune boundary, matching Python's per-code-point slicing. - Wrap the bare errors in GrepContent and ReadFileContent with %w. Python: - grep_content dropped broken symlinks silently. A dangling link is a genuine read failure, so it now counts toward the "N entries could not be read" annotation, matching Go's walkEntryUnreadable. FIFOs and sockets stay silent, matching walkEntrySkip. - Entries were validated only against allowed_root, which in production is the whole session dir plus the skills dir -- wider than the directory being searched. A symlink could therefore pull in a sibling the caller never asked about, contradicting both the tool description and the README. Entries are now also bounded by the search root, as Go already does. Also corrects comments in five places that described list_files/grep_file as opt-in "alongside bash". Upstream removed bash's gating entirely in #2498, so bash is now unconditional in both runtimes and that comparison was false. Adds a test pinning the KAGENT_ENABLE_FILE_SEARCH_TOOLS literal in go/adk/pkg/tools to the `kagent env` registry entry in go/core/pkg/env so the two cannot drift. The import is test-only and does not add a dependency from the agent runtime onto the control-plane module. Signed-off-by: brandonkeung <[email protected]>
1 parent c94bf09 commit 07e4d62

9 files changed

Lines changed: 311 additions & 43 deletions

File tree

‎go/adk/pkg/tools/shell.go‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ import (
1616

1717
type CommandExecutor struct{}
1818

19+
// maxLineRunes is the longest line read_file and grep_file will emit before
20+
// truncating. Counted in runes, not bytes -- see truncateRunes.
21+
const maxLineRunes = 2000
22+
23+
// truncateRunes shortens s to at most maxRunes code points, appending "..."
24+
// when it truncates.
25+
//
26+
// Slicing a Go string directly (s[:n]) cuts on a byte boundary, which can
27+
// split a multi-byte UTF-8 sequence and leave a partial code point that
28+
// renders as U+FFFD. It also makes the limit mean bytes, so the same line
29+
// would truncate at a different point than in the Python runtime (whose str
30+
// slicing is per-code-point) and than the tool descriptions promise, which
31+
// say "characters".
32+
func truncateRunes(s string, maxRunes int) string {
33+
count := 0
34+
// Ranging a string yields one iteration per rune, with i the byte index
35+
// where that rune starts -- so at the maxRunes'th rune, s[:i] holds
36+
// exactly maxRunes complete runes.
37+
for i := range s {
38+
if count == maxRunes {
39+
return s[:i] + "..."
40+
}
41+
count++
42+
}
43+
return s
44+
}
45+
1946
// GetSessionPath creates the working directory used by skill execution tools.
2047
func GetSessionPath(sessionID, skillsDirectory string) (string, error) {
2148
if sessionID == "" {
@@ -55,9 +82,20 @@ func GetSessionPath(sessionID, skillsDirectory string) (string, error) {
5582

5683
// ReadFileContent reads a file with line numbers.
5784
func ReadFileContent(path string, offset, limit int) (string, error) {
85+
// Stat before opening: os.Open on a FIFO with no writer connected blocks
86+
// indefinitely, and nothing on this path imposes a timeout. GrepContent
87+
// rejects non-regular files for the same reason.
88+
info, err := os.Stat(path)
89+
if err != nil {
90+
return "", fmt.Errorf("failed to stat %q: %w", path, err)
91+
}
92+
if !info.Mode().IsRegular() {
93+
return "", fmt.Errorf("%q is not a regular file", path)
94+
}
95+
5896
file, err := os.Open(path)
5997
if err != nil {
60-
return "", err
98+
return "", fmt.Errorf("failed to open %q: %w", path, err)
6199
}
62100
defer file.Close()
63101

@@ -69,10 +107,7 @@ func ReadFileContent(path string, offset, limit int) (string, error) {
69107

70108
for scanner.Scan() {
71109
if lineNum >= start {
72-
line := scanner.Text()
73-
if len(line) > 2000 {
74-
line = line[:2000] + "..."
75-
}
110+
line := truncateRunes(scanner.Text(), maxLineRunes)
76111
fmt.Fprintf(&result, "%6d|%s\n", lineNum, line)
77112
count++
78113
if limit > 0 && count >= limit {
@@ -162,16 +197,25 @@ func ListDirContent(path string) (string, error) {
162197
continue
163198
}
164199

165-
// entry.IsDir() reflects the entry's own type (Lstat-like) and is
166-
// false for a symlink even when its target is a directory -- e.g.
167-
// the "skills" symlink present in every session dir. Stat (which
168-
// follows symlinks) to classify those correctly, matching Python's
169-
// pathlib Path.is_dir(), which follows symlinks by default.
200+
// entry.IsDir() and entry.Info() both describe the entry itself
201+
// (Lstat-like), so for a symlink they report the link rather than
202+
// its target -- the target's type is lost, and Info().Size() is the
203+
// length of the stored target path, not the file's size. os.Stat
204+
// follows the link, so use it for both, matching Python's pathlib
205+
// Path.is_dir()/Path.stat(), which follow symlinks by default.
170206
if entry.Type()&fs.ModeSymlink != 0 {
171-
if target, statErr := os.Stat(filepath.Join(path, entry.Name())); statErr == nil && target.IsDir() {
207+
target, statErr := os.Stat(filepath.Join(path, entry.Name()))
208+
switch {
209+
case statErr != nil:
210+
// Broken link: there is no target to size. Print the bare
211+
// name, as Python does when its stat() raises here.
212+
fmt.Fprintf(&result, "%s\n", entry.Name())
213+
case target.IsDir():
172214
fmt.Fprintf(&result, "%s/\n", entry.Name())
173-
continue
215+
default:
216+
fmt.Fprintf(&result, "%s\t%d\n", entry.Name(), target.Size())
174217
}
218+
continue
175219
}
176220

177221
info, err := entry.Info()
@@ -270,7 +314,7 @@ func GrepContent(path, pattern string, recursive, ignoreCase bool) (string, erro
270314

271315
info, err := os.Stat(path)
272316
if err != nil {
273-
return "", err
317+
return "", fmt.Errorf("failed to stat %q: %w", path, err)
274318
}
275319

276320
var result strings.Builder
@@ -287,10 +331,7 @@ func GrepContent(path, pattern string, recursive, ignoreCase bool) (string, erro
287331
lineNum := 1
288332
for scanner.Scan() {
289333
if line := scanner.Text(); re.MatchString(line) {
290-
if len(line) > 2000 {
291-
line = line[:2000] + "..."
292-
}
293-
fmt.Fprintf(&result, "%s:%d:%s\n", filePath, lineNum, line)
334+
fmt.Fprintf(&result, "%s:%d:%s\n", filePath, lineNum, truncateRunes(line, maxLineRunes))
294335
}
295336
lineNum++
296337
}
@@ -307,7 +348,7 @@ func GrepContent(path, pattern string, recursive, ignoreCase bool) (string, erro
307348
var root string
308349
root, err = filepath.EvalSymlinks(path)
309350
if err != nil {
310-
return "", err
351+
return "", fmt.Errorf("failed to resolve %q: %w", path, err)
311352
}
312353
// Walk the resolved root, not path: filepath.WalkDir uses Lstat on
313354
// its root argument, so if path itself were an unresolved directory
@@ -350,7 +391,7 @@ func GrepContent(path, pattern string, recursive, ignoreCase bool) (string, erro
350391
err = grepFile(path)
351392
}
352393
if err != nil {
353-
return "", err
394+
return "", fmt.Errorf("failed to search %q: %w", path, err)
354395
}
355396

356397
if result.Len() == 0 {

‎go/adk/pkg/tools/shell_test.go‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package tools
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"strings"
89
"syscall"
910
"testing"
1011
"time"
12+
"unicode/utf8"
1113
)
1214

1315
func createTempDir(t *testing.T) string {
@@ -174,6 +176,78 @@ func TestReadFileContent(t *testing.T) {
174176
}
175177
}
176178

179+
func TestReadFileContent_FIFOReturnsErrorInsteadOfHanging(t *testing.T) {
180+
fifoDir := createTempDir(t)
181+
defer os.RemoveAll(fifoDir)
182+
183+
fifoPath := filepath.Join(fifoDir, "pipe")
184+
if err := syscall.Mkfifo(fifoPath, 0644); err != nil {
185+
t.Skipf("FIFOs not supported: %v", err)
186+
}
187+
188+
// Opening a FIFO with no writer connected blocks forever, and nothing on
189+
// this path has a timeout -- so run it off-goroutine and fail on the
190+
// timeout rather than hanging the whole test binary.
191+
done := make(chan struct{})
192+
var err error
193+
go func() {
194+
_, err = ReadFileContent(fifoPath, 0, 0)
195+
close(done)
196+
}()
197+
198+
select {
199+
case <-done:
200+
case <-time.After(5 * time.Second):
201+
t.Fatal("ReadFileContent hung opening a FIFO instead of erroring")
202+
}
203+
204+
if err == nil {
205+
t.Fatal("expected an error for a non-regular file target, got nil")
206+
}
207+
if !strings.Contains(err.Error(), "not a regular file") {
208+
t.Errorf("expected a not-a-regular-file error, got %v", err)
209+
}
210+
}
211+
212+
func TestTruncateRunes(t *testing.T) {
213+
t.Run("truncates on a rune boundary and stays valid UTF-8", func(t *testing.T) {
214+
// 3000 three-byte runes: byte-slicing at 2000 would land mid-rune.
215+
long := strings.Repeat("世", 3000)
216+
217+
got := truncateRunes(long, maxLineRunes)
218+
219+
if !utf8.ValidString(got) {
220+
t.Error("expected truncated output to be valid UTF-8")
221+
}
222+
if strings.ContainsRune(got, utf8.RuneError) {
223+
t.Error("expected no U+FFFD replacement char from a split rune")
224+
}
225+
body := strings.TrimSuffix(got, "...")
226+
if n := utf8.RuneCountInString(body); n != maxLineRunes {
227+
t.Errorf("expected exactly %d runes before the ellipsis, got %d", maxLineRunes, n)
228+
}
229+
if !strings.HasSuffix(got, "...") {
230+
t.Errorf("expected truncated output to end with '...', got %q", got)
231+
}
232+
})
233+
234+
t.Run("leaves a short string untouched", func(t *testing.T) {
235+
if got := truncateRunes("héllo", maxLineRunes); got != "héllo" {
236+
t.Errorf("expected input returned unchanged, got %q", got)
237+
}
238+
})
239+
240+
t.Run("counts characters not bytes, matching the Python runtime", func(t *testing.T) {
241+
// 2000 multi-byte runes is 6000 bytes -- well over a byte-based
242+
// limit, but exactly at the rune limit, so it must NOT truncate.
243+
exact := strings.Repeat("世", maxLineRunes)
244+
if got := truncateRunes(exact, maxLineRunes); got != exact {
245+
t.Errorf("expected a string of exactly %d runes to be left alone, got %d runes",
246+
maxLineRunes, utf8.RuneCountInString(got))
247+
}
248+
})
249+
}
250+
177251
func TestWriteFileContent(t *testing.T) {
178252
tmpDir := createTempDir(t)
179253
defer os.RemoveAll(tmpDir)
@@ -362,6 +436,58 @@ func TestListDirContent(t *testing.T) {
362436
t.Errorf("expected symlinked directory not to be listed as a file, got %q", result)
363437
}
364438
})
439+
440+
t.Run("symlink to a file reports the target's size, not the link's", func(t *testing.T) {
441+
linkDir := createTempDir(t)
442+
defer os.RemoveAll(linkDir)
443+
444+
// The target's contents must be longer than the symlink's own
445+
// "size" (the byte length of the stored target path) so the two are
446+
// unambiguous: Lstat would report len(target path), Stat reports 20.
447+
target := filepath.Join(linkDir, "target.txt")
448+
contents := strings.Repeat("x", 20)
449+
if err := os.WriteFile(target, []byte(contents), 0644); err != nil {
450+
t.Fatalf("Failed to write target file: %v", err)
451+
}
452+
linkPath := filepath.Join(linkDir, "file-link")
453+
if err := os.Symlink(target, linkPath); err != nil {
454+
t.Skipf("symlinks not supported: %v", err)
455+
}
456+
457+
result, err := ListDirContent(linkDir)
458+
if err != nil {
459+
t.Fatalf("ListDirContent() error = %v", err)
460+
}
461+
if !strings.Contains(result, fmt.Sprintf("file-link\t%d", len(contents))) {
462+
t.Errorf("expected symlinked file to report the target's size (%d), got %q", len(contents), result)
463+
}
464+
if strings.Contains(result, fmt.Sprintf("file-link\t%d", len(target))) {
465+
t.Errorf("expected not to report the symlink's own size (%d), got %q", len(target), result)
466+
}
467+
})
468+
469+
t.Run("broken symlink is listed without a size", func(t *testing.T) {
470+
linkDir := createTempDir(t)
471+
defer os.RemoveAll(linkDir)
472+
473+
linkPath := filepath.Join(linkDir, "broken-link")
474+
if err := os.Symlink(filepath.Join(linkDir, "does-not-exist"), linkPath); err != nil {
475+
t.Skipf("symlinks not supported: %v", err)
476+
}
477+
478+
result, err := ListDirContent(linkDir)
479+
if err != nil {
480+
t.Fatalf("ListDirContent() error = %v", err)
481+
}
482+
// Bare name, no size and no trailing slash -- matching Python's
483+
// list_dir_content, whose stat() raises on a dangling link.
484+
if !strings.Contains(result, "broken-link") {
485+
t.Errorf("expected broken symlink to be listed, got %q", result)
486+
}
487+
if strings.Contains(result, "broken-link\t") || strings.Contains(result, "broken-link/") {
488+
t.Errorf("expected broken symlink to be listed with no size and no trailing slash, got %q", result)
489+
}
490+
})
365491
}
366492

367493
func TestGrepContent(t *testing.T) {

‎go/adk/pkg/tools/skills.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313
)
1414

1515
// enableFileSearchToolsEnv gates the list_files and grep_file tools, which
16-
// are opt-in (disabled by default): they let an agent walk the filesystem
17-
// under its session/skills roots without a shell, which some deployments
18-
// want to keep off by default alongside bash rather than enable implicitly.
16+
// are opt-in (disabled by default): they let an agent enumerate and search
17+
// the filesystem under its session/skills roots without invoking a shell, so
18+
// deployments that want to grant that visibility deliberately can, rather
19+
// than having it enabled implicitly.
1920
//
2021
// Also registered (separately, for `kagent env` CLI discoverability only,
21-
// not read here) as KagentEnableFileSearchTools in go/core/pkg/env/kagent.go
22-
// -- keep both string literals in sync if this name ever changes.
22+
// not read here) as KagentEnableFileSearchTools in go/core/pkg/env/kagent.go.
23+
// TestEnableFileSearchToolsEnvMatchesRegistry pins the two literals together
24+
// so they cannot drift.
2325
const enableFileSearchToolsEnv = "KAGENT_ENABLE_FILE_SEARCH_TOOLS"
2426

2527
// fileSearchToolsEnabled accepts the same case-insensitive true-values as
@@ -230,8 +232,8 @@ func NewSkillExecutionTools(skillsDirectory string) ([]tool.Tool, error) {
230232
tools := []tool.Tool{readFileTool, writeFileTool, editFileTool}
231233

232234
// list_files/grep_file are opt-in: they give an agent broad filesystem
233-
// visibility, so some deployments want them off unless explicitly
234-
// enabled, same as bash below.
235+
// visibility, so deployments enable them deliberately. Note this gate is
236+
// theirs alone -- bash below is always registered.
235237
fileSearchEnabled := fileSearchToolsEnabled()
236238
if fileSearchEnabled {
237239
listFilesTool, err := functiontool.New(functiontool.Config{

‎go/adk/pkg/tools/skills_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,27 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/kagent-dev/kagent/go/core/pkg/env"
1011
adkagent "google.golang.org/adk/v2/agent"
1112
"google.golang.org/adk/v2/tool"
1213
"google.golang.org/adk/v2/tool/toolconfirmation"
1314
)
1415

16+
// TestEnableFileSearchToolsEnvMatchesRegistry pins this package's env var
17+
// literal to the `kagent env` registry entry in go/core/pkg/env. The name is
18+
// declared twice on purpose -- the runtime reads the raw variable here, while
19+
// the registry exists solely so `kagent env` can document it -- so without
20+
// this assertion nothing would catch the two drifting apart.
21+
//
22+
// This is a test-only import: it does not put a dependency on the
23+
// control-plane module into the shipped agent runtime.
24+
func TestEnableFileSearchToolsEnvMatchesRegistry(t *testing.T) {
25+
if got, want := enableFileSearchToolsEnv, env.KagentEnableFileSearchTools.Name(); got != want {
26+
t.Fatalf("env var name drift: %s has %q, go/core/pkg/env registry has %q",
27+
"go/adk/pkg/tools/skills.go", got, want)
28+
}
29+
}
30+
1531
// fakeToolContext is a minimal agent.Context for directly invoking tools in
1632
// tests, bypassing the full ADK flow engine. Embeds StrictContextMock (an
1733
// ADK test double) and overrides only the methods functiontool.Run() calls.

‎go/core/pkg/env/kagent.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ var (
9090

9191
// Registered here for `kagent env` CLI discoverability only -- the
9292
// actual gate is read independently (raw os.Getenv, not via this var)
93-
// in go/adk/pkg/tools/skills.go's enableFileSearchToolsEnv. Keep both
94-
// string literals in sync if this name ever changes.
93+
// in go/adk/pkg/tools/skills.go's enableFileSearchToolsEnv. The two
94+
// literals are pinned together by that package's
95+
// TestEnableFileSearchToolsEnvMatchesRegistry.
9596
KagentEnableFileSearchTools = RegisterBoolVar(
9697
"KAGENT_ENABLE_FILE_SEARCH_TOOLS",
9798
false,
9899
"When true, enables the list_files and grep_file skills tools, which let an agent "+
99-
"walk the filesystem under its session/skills roots without a shell. Disabled by "+
100-
"default alongside bash; set on the Agent's env to opt in.",
100+
"enumerate and search the filesystem under its session/skills roots without a "+
101+
"shell. Disabled by default; set on the Agent's env to opt in.",
101102
ComponentAgentRuntime,
102103
)
103104

‎python/packages/kagent-adk/src/kagent/adk/tools/skills_plugin.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def add_skills_tool_to_agent(
5353
logger.debug(f"Added edit file tool to agent: {agent.name}")
5454

5555
# list_files/grep_file are opt-in: they give an agent broad filesystem
56-
# visibility, so some deployments want them off unless explicitly
57-
# enabled, same as bash.
56+
# visibility, so deployments enable them deliberately. Note this gate is
57+
# theirs alone -- bash above is always registered.
5858
if file_search_tools_enabled():
5959
if "list_files" not in existing_tool_names:
6060
agent.tools.append(ListFilesTool(skills_directory))

0 commit comments

Comments
 (0)