From ce3f638680287ecb32a1ae733fef8756ee48a69a Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:30:22 +0100 Subject: [PATCH 1/7] filesystem: avoid duplicate string(content) allocation in handleReadFile Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 34e250a85..86aa1dca5 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -543,10 +543,12 @@ func (t *FilesystemTool) handleReadFile(_ context.Context, args ReadFileArgs) (* }, nil } + text := string(content) + return &tools.ToolCallResult{ - Output: string(content), + Output: text, Meta: ReadFileMeta{ - LineCount: strings.Count(string(content), "\n") + 1, + LineCount: strings.Count(text, "\n") + 1, }, }, nil } From 8b3e7d96a01f9c3a07338701dcad80bfdf6b733e Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:31:00 +0100 Subject: [PATCH 2/7] filesystem: avoid triple string(content) allocation in handleReadMultipleFiles Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 86aa1dca5..3d6fe006d 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -641,12 +641,13 @@ func (t *FilesystemTool) handleReadMultipleFiles(ctx context.Context, args ReadM continue } + text := string(content) contents = append(contents, PathContent{ Path: path, - Content: string(content), + Content: text, }) - entry.Content = string(content) - entry.LineCount = strings.Count(string(content), "\n") + 1 + entry.Content = text + entry.LineCount = strings.Count(text, "\n") + 1 meta.Files = append(meta.Files, entry) } From 7528035547b07ccb83f9cfdc54dadb73fe52a975 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:31:47 +0100 Subject: [PATCH 3/7] filesystem: use errors.Is(err, fs.ErrNotExist) instead of os.IsNotExist os.IsNotExist does not unwrap errors. errors.Is handles wrapped errors correctly. Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 3d6fe006d..1400422cc 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "io/fs" "log/slog" @@ -512,7 +513,7 @@ func (t *FilesystemTool) handleReadFile(_ context.Context, args ReadFileArgs) (* info, err := os.Stat(resolvedPath) if err != nil { var errMsg string - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { errMsg = "not found" } else { errMsg = err.Error() @@ -558,7 +559,7 @@ func (t *FilesystemTool) readImageFile(resolvedPath, originalPath string) (*tool data, err := os.ReadFile(resolvedPath) if err != nil { var errMsg string - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { errMsg = "not found" } else { errMsg = err.Error() @@ -629,7 +630,7 @@ func (t *FilesystemTool) handleReadMultipleFiles(ctx context.Context, args ReadM content, err := os.ReadFile(resolvedPath) if err != nil { errMsg := err.Error() - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { errMsg = "not found" } contents = append(contents, PathContent{ From 370923b49d11ee050da8e25e4acc272feb55fbf9 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:32:23 +0100 Subject: [PATCH 4/7] filesystem: simplify shouldIgnorePath return Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 1400422cc..35a4c10a7 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -376,11 +376,7 @@ func (t *FilesystemTool) shouldIgnorePath(path string) bool { // Lazily initialize the gitignore matcher on first use t.initGitignoreMatcher() - if t.repoMatcher != nil && t.repoMatcher.ShouldIgnore(path) { - return true - } - - return false + return t.repoMatcher != nil && t.repoMatcher.ShouldIgnore(path) } // Handler implementations From 77cbdbabbd4b47f1b2e8efcce813886d3e3c91ef Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:33:11 +0100 Subject: [PATCH 5/7] filesystem: remove dead not-found check in readImageFile The caller (handleReadFile) already confirmed the file exists via os.Stat, so the os.ErrNotExist branch inside readImageFile can never trigger. Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 35a4c10a7..6ff9e8315 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -551,20 +551,15 @@ func (t *FilesystemTool) handleReadFile(_ context.Context, args ReadFileArgs) (* } // readImageFile reads an image file and returns it as base64-encoded image content. +// The caller must ensure the file exists (e.g. via os.Stat) before calling this method. func (t *FilesystemTool) readImageFile(resolvedPath, originalPath string) (*tools.ToolCallResult, error) { data, err := os.ReadFile(resolvedPath) if err != nil { - var errMsg string - if errors.Is(err, fs.ErrNotExist) { - errMsg = "not found" - } else { - errMsg = err.Error() - } return &tools.ToolCallResult{ - Output: errMsg, + Output: err.Error(), IsError: true, Meta: ReadFileMeta{ - Error: errMsg, + Error: err.Error(), }, }, nil } From 091ab58b7ba8fa327195de7d45449b873aadbc32 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:33:55 +0100 Subject: [PATCH 6/7] filesystem: promote no-op isPathAllowed to package-level var Avoids allocating a new closure on every handleDirectoryTree call. Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index 6ff9e8315..a978746c8 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -52,6 +52,9 @@ var ( _ tools.Instructable = (*FilesystemTool)(nil) ) +// allowAllPaths is a no-op path filter that permits every path. +var allowAllPaths = func(_ string) error { return nil } + type FileSystemOpt func(*FilesystemTool) func WithPostEditCommands(postEditCommands []PostEditConfig) FileSystemOpt { @@ -384,11 +387,7 @@ func (t *FilesystemTool) shouldIgnorePath(path string) bool { func (t *FilesystemTool) handleDirectoryTree(ctx context.Context, args DirectoryTreeArgs) (*tools.ToolCallResult, error) { resolvedPath := t.resolvePath(args.Path) - isPathAllowed := func(_ string) error { - return nil - } - - tree, err := fsx.DirectoryTree(ctx, resolvedPath, isPathAllowed, t.shouldIgnorePath, maxFiles) + tree, err := fsx.DirectoryTree(ctx, resolvedPath, allowAllPaths, t.shouldIgnorePath, maxFiles) if err != nil { return tools.ResultError(fmt.Sprintf("Error building directory tree: %s", err)), nil } From ea4271fc7c2aba183f3ad2b092979888611b72b2 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Mar 2026 15:41:35 +0100 Subject: [PATCH 7/7] filesystem: fix review issues in previous commits - Make allowAllPaths a function instead of a var to prevent reassignment - Avoid duplicate err.Error() call in readImageFile Assisted-By: docker-agent --- pkg/tools/builtin/filesystem.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/tools/builtin/filesystem.go b/pkg/tools/builtin/filesystem.go index a978746c8..1423985e2 100644 --- a/pkg/tools/builtin/filesystem.go +++ b/pkg/tools/builtin/filesystem.go @@ -53,7 +53,7 @@ var ( ) // allowAllPaths is a no-op path filter that permits every path. -var allowAllPaths = func(_ string) error { return nil } +func allowAllPaths(_ string) error { return nil } type FileSystemOpt func(*FilesystemTool) @@ -554,11 +554,12 @@ func (t *FilesystemTool) handleReadFile(_ context.Context, args ReadFileArgs) (* func (t *FilesystemTool) readImageFile(resolvedPath, originalPath string) (*tools.ToolCallResult, error) { data, err := os.ReadFile(resolvedPath) if err != nil { + errMsg := err.Error() return &tools.ToolCallResult{ - Output: err.Error(), + Output: errMsg, IsError: true, Meta: ReadFileMeta{ - Error: err.Error(), + Error: errMsg, }, }, nil }