From 1752cbf2fb549af93608091282bf1830ac753847 Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 12:51:30 +0800 Subject: [PATCH] fix(file): honor ctx cancellation in read_file/edit_file (N12) --- tools/file/editfile.go | 6 +++++- tools/file/readfile.go | 6 +++++- tools/file/readfile_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/file/editfile.go b/tools/file/editfile.go index 54e43e1..1f5c39a 100644 --- a/tools/file/editfile.go +++ b/tools/file/editfile.go @@ -52,7 +52,7 @@ func (t *EditFileTool) Parameters() json.RawMessage { // IsConcurrencySafe returns false — edit_file mutates the filesystem. func (t *EditFileTool) IsConcurrencySafe(_ json.RawMessage) bool { return false } -func (t *EditFileTool) Execute(_ context.Context, input json.RawMessage) (tool.ToolResult, error) { +func (t *EditFileTool) Execute(ctx context.Context, input json.RawMessage) (tool.ToolResult, error) { var in editFileInput if err := json.Unmarshal(input, &in); err != nil { return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil @@ -74,6 +74,10 @@ func (t *EditFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } } + if err := ctx.Err(); err != nil { + return tool.ToolResult{Error: err.Error()}, nil + } + if msg := checkFileSize(in.Path, int64(maxTextFileSize), "text"); msg != "" { return tool.ToolResult{Error: msg}, nil } diff --git a/tools/file/readfile.go b/tools/file/readfile.go index 703c1b1..3e9096a 100644 --- a/tools/file/readfile.go +++ b/tools/file/readfile.go @@ -72,7 +72,7 @@ func detectImageMIMEFromBytes(data []byte, hint string) string { // IsConcurrencySafe returns true — read_file is a pure read. func (t *ReadFileTool) IsConcurrencySafe(_ json.RawMessage) bool { return true } -func (t *ReadFileTool) Execute(_ context.Context, input json.RawMessage) (tool.ToolResult, error) { +func (t *ReadFileTool) Execute(ctx context.Context, input json.RawMessage) (tool.ToolResult, error) { var in readFileInput if err := json.Unmarshal(input, &in); err != nil { return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil @@ -94,6 +94,10 @@ func (t *ReadFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } } + if err := ctx.Err(); err != nil { + return tool.ToolResult{Error: err.Error()}, nil + } + ext := strings.ToLower(filepath.Ext(in.Path)) limit := int64(maxTextFileSize) kind := "text" diff --git a/tools/file/readfile_test.go b/tools/file/readfile_test.go index 744123a..78bdc8c 100644 --- a/tools/file/readfile_test.go +++ b/tools/file/readfile_test.go @@ -58,3 +58,29 @@ func TestEditFile_RejectsOversized(t *testing.T) { require.NoError(t, err) require.Contains(t, res.Error, "too large") } + +func TestReadFile_HonorsCancelledContext(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "f.txt") + require.NoError(t, os.WriteFile(p, []byte("hello"), 0o600)) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + tool := &ReadFileTool{} + in, _ := json.Marshal(map[string]string{"path": p}) + res, err := tool.Execute(ctx, in) + require.NoError(t, err) + require.NotEmpty(t, res.Error, "cancelled ctx must short-circuit") +} + +func TestEditFile_HonorsCancelledContext(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "f.txt") + require.NoError(t, os.WriteFile(p, []byte("hello"), 0o600)) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + tool := &EditFileTool{} + in, _ := json.Marshal(map[string]string{"path": p, "old_string": "h", "new_string": "H"}) + res, err := tool.Execute(ctx, in) + require.NoError(t, err) + require.NotEmpty(t, res.Error, "cancelled ctx must short-circuit") +}