From 65021e1fe21b4875da58cf1a24e239dc71f7fc06 Mon Sep 17 00:00:00 2001 From: sausheong Date: Tue, 12 May 2026 17:10:32 +0800 Subject: [PATCH] fix(file tools): resolve relative paths against WorkDir before workspace validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the agent supplies a relative path (e.g. "app.py"), filepath.Abs resolves it against the process CWD, not the workspace — causing ValidatePathInWorkDir to reject paths that are legitimately inside the workspace. Join relative paths with WorkDir before resolution so the agent can use bare filenames without hitting the outside-workspace error. --- tools/file/editfile.go | 5 +++++ tools/file/readfile.go | 3 +++ tools/file/writefile.go | 3 +++ 3 files changed, 11 insertions(+) diff --git a/tools/file/editfile.go b/tools/file/editfile.go index d136b6a..c1f43d7 100644 --- a/tools/file/editfile.go +++ b/tools/file/editfile.go @@ -5,7 +5,9 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "strings" + "github.com/sausheong/harness/tool" ) @@ -61,6 +63,9 @@ func (t *EditFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } in.Path = tool.ExpandHome(in.Path) + if t.WorkDir != "" && !filepath.IsAbs(in.Path) { + in.Path = filepath.Join(t.WorkDir, in.Path) + } in.Path = tool.ResolveExistingPath(in.Path) if t.WorkDir != "" { diff --git a/tools/file/readfile.go b/tools/file/readfile.go index 8b8d33f..8a6c793 100644 --- a/tools/file/readfile.go +++ b/tools/file/readfile.go @@ -82,6 +82,9 @@ func (t *ReadFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } in.Path = tool.ExpandHome(in.Path) + if t.WorkDir != "" && !filepath.IsAbs(in.Path) { + in.Path = filepath.Join(t.WorkDir, in.Path) + } in.Path = tool.ResolveExistingPath(in.Path) if t.WorkDir != "" { diff --git a/tools/file/writefile.go b/tools/file/writefile.go index 058855a..1a63b81 100644 --- a/tools/file/writefile.go +++ b/tools/file/writefile.go @@ -56,6 +56,9 @@ func (t *WriteFileTool) Execute(_ context.Context, input json.RawMessage) (tool. } in.Path = tool.ExpandHome(in.Path) + if t.WorkDir != "" && !filepath.IsAbs(in.Path) { + in.Path = filepath.Join(t.WorkDir, in.Path) + } if t.WorkDir != "" { if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {