Initial commit: Go agentic harness extracted from Felix

This commit is contained in:
sausheong committed 2026-05-07 11:31:42 +08:00
commit 1f2c471ff9
89 files changed
+19388

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
package file
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/sausheong/harness/tool"
)
// EditFileTool performs a string-replace edit on a file.
type EditFileTool struct {
WorkDir string // if set, restricts edits to this directory
}
type editFileInput struct {
Path string `json:"path"`
OldString string `json:"old_string"`
NewString string `json:"new_string"`
}
func (t *EditFileTool) Name() string { return "edit_file" }
func (t *EditFileTool) Description() string {
return "Edit a file by replacing an exact string match. The old_string must match exactly one occurrence in the file. Use this for targeted edits rather than rewriting entire files."
}
func (t *EditFileTool) Parameters() json.RawMessage {
return json.RawMessage(`{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path to the file to edit"
},
"old_string": {
"type": "string",
"description": "The exact text to find and replace"
},
"new_string": {
"type": "string",
"description": "The replacement text"
}
},
"required": ["path", "old_string", "new_string"]
}`)
}
// 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) {
var in editFileInput
if err := json.Unmarshal(input, &in); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil
}
if in.Path == "" {
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
in.Path = tool.ResolveExistingPath(in.Path)
if t.WorkDir != "" {
if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {
return tool.ToolResult{Error: err.Error()}, nil
}
}
data, err := os.ReadFile(in.Path)
if err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil
}
content := string(data)
count := strings.Count(content, in.OldString)
if count == 0 {
return tool.ToolResult{Error: "old_string not found in file"}, nil
}
if count > 1 {
return tool.ToolResult{Error: fmt.Sprintf("old_string found %d times in file, must be unique", count)}, nil
}
newContent := strings.Replace(content, in.OldString, in.NewString, 1)
if err := os.WriteFile(in.Path, []byte(newContent), 0o644); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to write file: %v", err)}, nil
}
return tool.ToolResult{Output: "Successfully edited file"}, nil
}
+104
View File
@@ -0,0 +1,104 @@
package file
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReadFileTool(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
require.NoError(t, os.WriteFile(path, []byte("hello world"), 0o644))
tool := &ReadFileTool{}
input, _ := json.Marshal(readFileInput{Path: path})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.Equal(t, "hello world", result.Output)
assert.Empty(t, result.Error)
}
func TestReadFileToolMissing(t *testing.T) {
tool := &ReadFileTool{}
input, _ := json.Marshal(readFileInput{Path: "/nonexistent/file"})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.NotEmpty(t, result.Error)
}
func TestReadFileToolWorkDirClamps(t *testing.T) {
workspace := t.TempDir()
outside := filepath.Join(t.TempDir(), "secret.txt")
require.NoError(t, os.WriteFile(outside, []byte("nope"), 0o644))
tool := &ReadFileTool{WorkDir: workspace}
input, _ := json.Marshal(readFileInput{Path: outside})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.Contains(t, result.Error, "outside workspace",
"reads outside the workspace must be rejected when WorkDir is set")
}
func TestWriteFileTool(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "subdir", "output.txt")
tool := &WriteFileTool{}
input, _ := json.Marshal(writeFileInput{Path: path, Content: "test content"})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.Empty(t, result.Error)
assert.Contains(t, result.Output, "Successfully wrote")
data, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, "test content", string(data))
}
func TestEditFileTool(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "edit.txt")
require.NoError(t, os.WriteFile(path, []byte("hello world"), 0o644))
tool := &EditFileTool{}
input, _ := json.Marshal(editFileInput{
Path: path,
OldString: "world",
NewString: "Go",
})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.Empty(t, result.Error)
data, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, "hello Go", string(data))
}
func TestEditFileToolNotFound(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "edit.txt")
require.NoError(t, os.WriteFile(path, []byte("hello world"), 0o644))
tool := &EditFileTool{}
input, _ := json.Marshal(editFileInput{
Path: path,
OldString: "missing",
NewString: "replacement",
})
result, err := tool.Execute(context.Background(), input)
require.NoError(t, err)
assert.Contains(t, result.Error, "not found")
}
+111
View File
@@ -0,0 +1,111 @@
package file
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/sausheong/harness/llm"
"github.com/sausheong/harness/tool"
)
// ReadFileTool reads the contents of a file.
type ReadFileTool struct {
WorkDir string // if set, restricts reads to this directory
}
type readFileInput struct {
Path string `json:"path"`
}
func (t *ReadFileTool) Name() string { return "read_file" }
func (t *ReadFileTool) Description() string {
return "Read the contents of a file at the given path. Returns the file contents as text. For image files (jpg, png, gif, webp, bmp), returns the image for visual inspection."
}
func (t *ReadFileTool) Parameters() json.RawMessage {
return json.RawMessage(`{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path to the file to read"
}
},
"required": ["path"]
}`)
}
// imageExtMap maps file extensions to MIME types for image files.
var imageExtMap = map[string]string{
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".bmp": "image/bmp",
}
// detectImageMIMEFromBytes inspects magic bytes to determine the actual image
// format, falling back to hint (typically derived from file extension).
func detectImageMIMEFromBytes(data []byte, hint string) string {
if len(data) >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF {
return "image/jpeg"
}
if len(data) >= 4 && data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G' {
return "image/png"
}
if len(data) >= 4 && data[0] == 'G' && data[1] == 'I' && data[2] == 'F' && data[3] == '8' {
return "image/gif"
}
if len(data) >= 4 && data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F' {
return "image/webp"
}
return hint
}
// 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) {
var in readFileInput
if err := json.Unmarshal(input, &in); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil
}
if in.Path == "" {
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
in.Path = tool.ResolveExistingPath(in.Path)
if t.WorkDir != "" {
if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {
return tool.ToolResult{Error: err.Error()}, nil
}
}
data, err := os.ReadFile(in.Path)
if err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil
}
// Check if this is an image file
ext := strings.ToLower(filepath.Ext(in.Path))
if mimeType, ok := imageExtMap[ext]; ok {
mimeType = detectImageMIMEFromBytes(data, mimeType)
return tool.ToolResult{
Output: fmt.Sprintf("Image file: %s (%d bytes)", filepath.Base(in.Path), len(data)),
Images: []llm.ImageContent{
{MimeType: mimeType, Data: data},
},
}, nil
}
return tool.ToolResult{Output: string(data)}, nil
}
+77
View File
@@ -0,0 +1,77 @@
package file
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/sausheong/harness/tool"
)
// WriteFileTool creates or overwrites a file.
type WriteFileTool struct {
WorkDir string // if set, restricts writes to this directory
}
type writeFileInput struct {
Path string `json:"path"`
Content string `json:"content"`
}
func (t *WriteFileTool) Name() string { return "write_file" }
func (t *WriteFileTool) Description() string {
return "Write content to a file at the given path. Creates the file and any parent directories if they don't exist. Overwrites existing files."
}
func (t *WriteFileTool) Parameters() json.RawMessage {
return json.RawMessage(`{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path to the file to write"
},
"content": {
"type": "string",
"description": "The content to write to the file"
}
},
"required": ["path", "content"]
}`)
}
// IsConcurrencySafe returns false — write_file mutates the filesystem.
func (t *WriteFileTool) IsConcurrencySafe(_ json.RawMessage) bool { return false }
func (t *WriteFileTool) Execute(_ context.Context, input json.RawMessage) (tool.ToolResult, error) {
var in writeFileInput
if err := json.Unmarshal(input, &in); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil
}
if in.Path == "" {
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
if t.WorkDir != "" {
if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {
return tool.ToolResult{Error: err.Error()}, nil
}
}
// Create parent directories
dir := filepath.Dir(in.Path)
if err := os.MkdirAll(dir, 0o755); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to create directory: %v", err)}, nil
}
if err := os.WriteFile(in.Path, []byte(in.Content), 0o644); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to write file: %v", err)}, nil
}
return tool.ToolResult{Output: fmt.Sprintf("Successfully wrote %d bytes to %s", len(in.Content), in.Path)}, nil
}