fix(file): honor ctx cancellation in read_file/edit_file (N12)
This commit is contained in:
1 parent
e133f97e1e
commit
1752cbf2fb
3 files changed
+36
-2
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in new issue
Block a user