Files
kandev/apps/backend/internal/agentctl/server/process/workspace_files.go
T
José Almeida c79d138c45 feat(web): add diff expansion with expand-all in review panel (#340)
* feat(backend): add support for fetching file content at git ref

Add GetFileContentAtRef method to WorkspaceTracker for retrieving file content
at specific git refs (branches, commits, HEAD). This enables diff expansion by
fetching old file versions without checking out.

Changes:
- Extract maxFileSize constant to package level (10MB)
- Add GetFileContentAtRef to workspace_files.go using git show
- Add workspace.file.get_at_ref WebSocket action and handler
- Add agentctl client method for the new endpoint
- Register handler in workspace file handlers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(web): add diff expansion support for code review

Add lazy-loaded diff expansion to show more context in code reviews. When expansion
is enabled, users can expand hunks to see more surrounding code by clicking expand
buttons. Full file content is fetched on-demand via WebSocket.

Changes:
- Add useExpandableDiff hook for lazy-loading old/new file content
- Add requestFileContentAtRef to workspace-files.ts
- Integrate expansion into diff viewer components
- Add enableExpansion and baseRef props to DiffViewer
- Auto-load expansion content when enabled
- Pass expansion state through diff viewer hierarchy

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(web): gracefully handle expansion props for Monaco diff viewer

Monaco diff viewer has built-in expansion that doesn't require fetching
old file content. Strip pierre-diffs-only expansion props (enableExpansion,
baseRef) when Monaco is selected to avoid passing unsupported props.

This ensures the feature works correctly regardless of which diff viewer
the user selects in settings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(backend): add diff expansion e2e scenario and fix unstaged diff base ref

- Add scenarioDiffExpansionSetup mock-agent scenario that creates a
  200-line file, commits it, then modifies lines 50 and 150 to produce
  a two-hunk uncommitted diff for testing diff expansion in the UI
- Add workspace_file_handlers for workspace.file.get_at_ref WS action
- Fix enrichWithDiffData to always diff against HEAD for unstaged/staged
  content so locally committed files show correct diffs
- Add unit tests for GetFileContentAtRef (6 sub-tests)

* feat(web): add diff expansion UI and e2e test

- Wire useExpandableDiff hook into FileDiffViewer and ReviewDiffList
  with enableExpansion and baseRef props
- Gracefully handle expansion props for Monaco diff viewer fallback
- Add e2e Playwright test for diff expansion between hunks

* fix(web): fix diff expansion by correcting hunk separators and line splitting

Three bugs prevented @pierre/diffs expand buttons from rendering:
- hunkSeparators was "simple" (no expand UI); changed to "line-info" when expansion enabled
- expandUnchanged:true showed all lines expanded (no separators); removed the option
- .split("\n") stripped trailing newlines; now uses SPLIT_WITH_NEWLINES regex to match library

* feat(web): add editor provider settings UI

Add an "Editor Providers" section to the Editors settings page with
Select dropdowns for each configurable context (Code Editor, Diff Viewer,
Chat Code Blocks, Chat Diff Blocks). Reads from and writes to the
existing localStorage-backed editor resolver store. Changes apply
immediately without requiring a save action.

* test(web): add e2e tests for editor provider settings

Verify that the editor provider dropdowns render with correct defaults,
update on selection, and persist changes across page reloads via
localStorage.

* feat(web): add expand/collapse unchanged regions toggle to diff toolbar

Refactor diff header toolbar into composable ToolbarButtons component and
add a toggle button to expand or collapse unchanged regions in both
Pierre Diffs and Monaco diff viewers. Pass enableExpansion through to
Monaco to control hideUnchangedRegions.

* fix(web): handle deleted files in diff expansion

Treat "file not found" / "no such file" errors as empty content instead
of throwing, so diff expansion works correctly for deleted files.

* chore(web): add diff expansion debug e2e helpers

* test(web): split diff expansion tests by provider (Pierre Diffs + Monaco)

Restructure diff-expansion e2e tests into two describe blocks, one per
diff viewer provider. Each block sets the provider via localStorage
before navigation and verifies the correct viewer renders:

- Pierre Diffs: checks for diffs-container element and shadow DOM
  expand buttons between hunks
- Monaco: checks for .monaco-diff-viewer wrapper and the built-in
  hideUnchangedRegions widget with "Show Unchanged Region" link

Both suites verify that clicking expand reveals collapsed middle lines.

* fix(web): fix Pierre Diffs expand button selectors in e2e tests

Use shadow DOM data attributes ([data-expand-button], [data-expand-up])
instead of XPath parent traversal which doesn't work inside shadow DOM.
Wait for expand buttons to load asynchronously before clicking.

* fix(e2e): make diff-expansion-setup scenario idempotent

The mock-agent scenario now removes and re-commits the test file before
each run, so subsequent tests in the same repo don't fail with "git
commit failed" when the file already exists. Also increase test timeouts
to match other heavy e2e tests.

* refactor(web): remove editor provider settings UI, enable expand-all in review

Remove the editor provider switching UI and its tests — Monaco's diff
expansion doesn't work and exposing provider switching adds confusion.
Show Pierre Diffs header toolbar in the review panel (remove hideHeader)
so the expand-all button is accessible. Add e2e test for expand-all.

* style(web): fix prettier formatting and lint warnings

Run prettier on files with formatting drift. Extract ToolbarBtn helper
in diff-header-toolbar to stay within max-lines-per-function limit.
Consolidate useMonacoDiffViewerState to reduce line count.

* chore(web): remove debug scaffolding, revert Monaco changes, drop stray console.log

Revert Monaco diff-viewer-toolbar and monaco-diff-viewer to main
(expansion not functional with Monaco). Remove debug-expansion
screenshot scripts and spec. Strip console.log from commit-detail-panel.
Strip enableExpansion from Monaco path in diff-viewer-resolver.

* fix(web): use commit-relative base ref for diff expansion

Use `${commitSha}^` instead of hardcoded `HEAD~1` so expansion fetches
the correct old content when viewing non-HEAD commits.

* fix(web,backend): address CodeRabbit review feedback

- Add aria-label to icon-only toolbar buttons for screen reader access
- Guard auto-load expansion effect against error state to prevent retry
  loops when WebSocket fetch fails
- Invalidate expansion cache on file/ref change with request versioning
  to prevent stale content
- Preflight blob size with git cat-file -s before git show to avoid
  memory spikes on large files

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 00:05:39 +00:00

680 lines
20 KiB
Go

package process
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/kandev/kandev/internal/agentctl/types"
"go.uber.org/zap"
)
const maxFileSize = 10 * 1024 * 1024 // 10MB
// updateFiles updates the file listing
func (wt *WorkspaceTracker) updateFiles(ctx context.Context) {
files, err := wt.getFileList(ctx)
if err != nil {
wt.logger.Debug("failed to get file list", zap.Error(err))
return
}
wt.mu.Lock()
wt.currentFiles = files
wt.mu.Unlock()
}
// getFileList retrieves the list of files in the workspace
func (wt *WorkspaceTracker) getFileList(ctx context.Context) (types.FileListUpdate, error) {
update := types.FileListUpdate{
Timestamp: time.Now(),
Files: []types.FileEntry{},
}
// Use git ls-files to get tracked files AND untracked files (excluding ignored)
// --cached: include tracked files
// --others: include untracked files
// --exclude-standard: respect .gitignore
cmd := exec.CommandContext(ctx, "git", "ls-files", "--cached", "--others", "--exclude-standard")
cmd.Dir = wt.workDir
out, err := cmd.Output()
if err != nil {
return update, err
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
update.Files = append(update.Files, types.FileEntry{
Path: line,
IsDir: false,
})
}
return update, nil
}
// GetFileTree returns the file tree for a given path and depth
func (wt *WorkspaceTracker) GetFileTree(reqPath string, depth int) (*types.FileTreeNode, error) {
// Resolve the full path with path traversal protection
safePath := filepath.Join(wt.workDir, filepath.Clean(reqPath))
cleanWorkDir := filepath.Clean(wt.workDir)
if !strings.HasPrefix(safePath, cleanWorkDir+string(os.PathSeparator)) && safePath != cleanWorkDir {
return nil, fmt.Errorf("path traversal detected")
}
// Check if path exists
info, err := os.Stat(safePath)
if err != nil {
return nil, fmt.Errorf("path not found: %w", err)
}
// Build the tree
node, err := wt.buildFileTreeNode(safePath, reqPath, info, depth, 0)
if err != nil {
return nil, err
}
return node, nil
}
// buildFileTreeNode recursively builds a file tree node
func (wt *WorkspaceTracker) buildFileTreeNode(safePath, relPath string, info os.FileInfo, maxDepth, currentDepth int) (*types.FileTreeNode, error) {
node := &types.FileTreeNode{
Name: info.Name(),
Path: relPath,
IsDir: info.IsDir(),
Size: info.Size(),
}
// If it's a file or we've reached max depth, return
if !info.IsDir() || (maxDepth > 0 && currentDepth >= maxDepth) {
return node, nil
}
// Read directory contents
entries, err := os.ReadDir(safePath)
if err != nil {
return node, nil // Return node without children on error
}
// Build children
node.Children = make([]*types.FileTreeNode, 0, len(entries))
for _, entry := range entries {
// Skip specific directories that should be ignored
name := entry.Name()
if name == ".git" || name == "node_modules" || name == ".next" || name == "dist" || name == "build" {
continue
}
childFullPath := filepath.Join(safePath, name)
childRelPath := filepath.Join(relPath, name)
childInfo, err := entry.Info()
if err != nil {
continue
}
childNode, err := wt.buildFileTreeNode(childFullPath, childRelPath, childInfo, maxDepth, currentDepth+1)
if err != nil {
continue
}
node.Children = append(node.Children, childNode)
}
return node, nil
}
// resolvedWorkDir returns the workspace directory with symlinks resolved.
func (wt *WorkspaceTracker) resolvedWorkDir() string {
resolved, err := filepath.EvalSymlinks(filepath.Clean(wt.workDir))
if err != nil {
return filepath.Clean(wt.workDir)
}
return resolved
}
// resolveSafePath resolves reqPath to an absolute path within workDir,
// rejecting any path traversal attempts. The returned path is always
// constructed as filepath.Join(resolvedWorkDir, validatedRelPath) so that
// static analysis tools (CodeQL) can verify it stays within the workspace.
func (wt *WorkspaceTracker) resolveSafePath(reqPath string) (string, error) {
cleanWorkDir := filepath.Clean(wt.workDir)
cleanReqPath := filepath.Clean(reqPath)
// Resolve workspace directory symlinks first so that all constructed
// paths share the same canonical prefix (e.g. /private/var on macOS
// where /var is a symlink).
realWorkDir, err := filepath.EvalSymlinks(cleanWorkDir)
if err != nil {
realWorkDir = cleanWorkDir
}
var safePath string
if filepath.IsAbs(cleanReqPath) {
safePath = cleanReqPath
} else {
safePath = filepath.Join(realWorkDir, cleanReqPath)
}
// Resolve symlinks to prevent bypassing validation.
// When the path doesn't exist yet, walk up until we find an existing
// ancestor so symlinks (e.g. /tmp -> /private/tmp on macOS) are resolved
// consistently with realWorkDir below. Only fall back for not-found errors;
// propagate permission denied, symlink loops, etc.
realPath, err := filepath.EvalSymlinks(safePath)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("failed to resolve path: %w", err)
}
realPath, err = resolveNonExistentPath(safePath)
if err != nil {
return "", err
}
}
// Check that the real path is within the workspace
relPath, err := filepath.Rel(realWorkDir, realPath)
if err != nil {
return "", fmt.Errorf("invalid path: %w", err)
}
// Ensure the relative path doesn't escape the workspace
if strings.HasPrefix(relPath, ".."+string(os.PathSeparator)) || relPath == ".." {
return "", fmt.Errorf("path traversal detected: %s", reqPath)
}
// Reconstruct the absolute path from the trusted workspace root and the
// validated relative path. This ensures the returned path is provably
// inside the workspace, satisfying static-analysis taint checks.
return filepath.Join(realWorkDir, relPath), nil
}
// resolveNonExistentPath walks up from path until it finds an existing
// ancestor, resolves its symlinks, then reattaches the non-existent tail.
// This ensures paths under symlinked directories (e.g. /tmp on macOS)
// resolve consistently even when the target doesn't exist yet.
// Returns an error if an ancestor fails with something other than ErrNotExist
// (e.g. permission denied, symlink loop).
func resolveNonExistentPath(path string) (string, error) {
current := path
var tail []string
for {
resolved, err := filepath.EvalSymlinks(current)
if err == nil {
for i := len(tail) - 1; i >= 0; i-- {
resolved = filepath.Join(resolved, tail[i])
}
return resolved, nil
}
if !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("failed to resolve ancestor %q: %w", current, err)
}
parent := filepath.Dir(current)
if parent == current {
return "", fmt.Errorf("no existing ancestor for %q: %w", path, err)
}
tail = append(tail, filepath.Base(current))
current = parent
}
}
// GetFileContent returns the content of a file.
// If the file is not valid UTF-8, it is base64-encoded and isBinary is true.
func (wt *WorkspaceTracker) GetFileContent(reqPath string) (string, int64, bool, error) {
safePath, err := wt.resolveSafePath(reqPath)
if err != nil {
return "", 0, false, err
}
// Check if file exists and is a regular file
info, err := os.Stat(safePath)
if err != nil {
return "", 0, false, fmt.Errorf("file not found: %w", err)
}
if info.IsDir() {
return "", 0, false, fmt.Errorf("path is a directory, not a file")
}
// Check file size
if info.Size() > maxFileSize {
return "", info.Size(), false, fmt.Errorf("file too large (max 10MB)")
}
// Read file content
file, err := os.Open(safePath)
if err != nil {
return "", 0, false, fmt.Errorf("failed to open file: %w", err)
}
defer func() {
_ = file.Close()
}()
content, err := io.ReadAll(file)
if err != nil {
return "", 0, false, fmt.Errorf("failed to read file: %w", err)
}
// Detect binary: if content is not valid UTF-8, base64-encode it
if !utf8.Valid(content) {
encoded := base64.StdEncoding.EncodeToString(content)
return encoded, info.Size(), true, nil
}
return string(content), info.Size(), false, nil
}
// ApplyFileDiff applies a unified diff to a file with conflict detection
// Uses git apply for reliable, battle-tested patch application.
// For symlinked files, resolves to the real path and rewrites the diff header
// so that git apply operates on the actual file.
func (wt *WorkspaceTracker) ApplyFileDiff(reqPath string, unifiedDiff string, originalHash string) (string, error) {
safePath, err := wt.resolveSafePath(reqPath)
if err != nil {
return "", err
}
cleanWorkDir := filepath.Clean(wt.workDir)
// Read current file content
currentContent, _, _, err := wt.GetFileContent(reqPath)
if err != nil {
return "", fmt.Errorf("failed to read current file: %w", err)
}
// Calculate hash of current content for conflict detection
currentHash := calculateSHA256(currentContent)
if originalHash != "" && currentHash != originalHash {
return "", fmt.Errorf("conflict detected: file has been modified (expected hash %s, got %s)", originalHash, currentHash)
}
// If the file is a symlink, resolve to the real path and rewrite the diff header.
// git apply cannot patch through symlinks — it needs the real file path.
// Note: safePath is already resolved by resolveSafePath, so we check the
// unresolved path to detect whether the original request targets a symlink.
applyPath := reqPath
cleanReqPath := filepath.Clean(reqPath)
// Validate path doesn't attempt traversal before constructing filesystem path
if strings.Contains(cleanReqPath, "..") || filepath.IsAbs(cleanReqPath) {
return "", fmt.Errorf("invalid path: %s", reqPath)
}
unresolvedPath := filepath.Join(wt.workDir, cleanReqPath)
if info, lErr := os.Lstat(unresolvedPath); lErr == nil && info.Mode()&os.ModeSymlink != 0 {
// File is a symlink. safePath already points to the real target.
realWorkDir, _ := filepath.EvalSymlinks(cleanWorkDir)
if realWorkDir == "" {
realWorkDir = cleanWorkDir
}
realRel, relErr := filepath.Rel(realWorkDir, safePath)
if relErr == nil {
unifiedDiff = rewriteDiffPaths(unifiedDiff, reqPath, realRel)
applyPath = realRel
}
}
// Write diff to a temporary patch file
patchFile := filepath.Join(wt.workDir, ".kandev-patch.tmp")
err = os.WriteFile(patchFile, []byte(unifiedDiff), 0o644)
if err != nil {
return "", fmt.Errorf("failed to write patch file: %w", err)
}
defer func() {
_ = os.Remove(patchFile) // Best effort cleanup
}()
// Use git apply to apply the patch directly to the file
cmd := exec.Command("git", "apply", "-p0", "--unidiff-zero", "--whitespace=nowarn", patchFile)
cmd.Dir = wt.workDir
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("git apply failed: %w\nOutput: %s", err, string(output))
}
// Read the updated content (use the real file path for symlinks)
readPath := filepath.Join(cleanWorkDir, applyPath)
updatedContent, err := os.ReadFile(readPath)
if err != nil {
return "", fmt.Errorf("failed to read updated file: %w", err)
}
// Calculate new hash
newHash := calculateSHA256(string(updatedContent))
// Notify with the original relative path (not the resolved symlink target)
relPath := strings.TrimPrefix(safePath, cleanWorkDir+string(os.PathSeparator))
wt.notifyFileChange(relPath, types.FileOpWrite)
wt.logger.Debug("applied file diff using git apply",
zap.String("path", relPath),
zap.String("old_hash", currentHash),
zap.String("new_hash", newHash),
)
return newHash, nil
}
// rewriteDiffPaths replaces file paths in unified diff headers.
// Handles both "--- a/old" / "+++ b/new" (with strip prefix) and
// "--- old" / "+++ new" (p0 mode) formats.
func rewriteDiffPaths(diff, oldPath, newPath string) string {
lines := strings.Split(diff, "\n")
for i, line := range lines {
if strings.HasPrefix(line, "--- ") {
lines[i] = replaceDiffPath(line, "--- ", oldPath, newPath)
} else if strings.HasPrefix(line, "+++ ") {
lines[i] = replaceDiffPath(line, "+++ ", oldPath, newPath)
}
}
return strings.Join(lines, "\n")
}
// replaceDiffPath replaces oldPath with newPath in a diff header line.
func replaceDiffPath(line, prefix, oldPath, newPath string) string {
rest := line[len(prefix):]
// Handle "--- a/path" or "--- path" formats
cleaned := strings.TrimPrefix(rest, "a/")
cleaned = strings.TrimPrefix(cleaned, "b/")
if cleaned == oldPath || filepath.Clean(cleaned) == filepath.Clean(oldPath) {
return prefix + newPath
}
return line
}
// CreateFile creates a new file in the workspace
func (wt *WorkspaceTracker) CreateFile(reqPath string) error {
safePath, err := wt.resolveSafePath(reqPath)
if err != nil {
return err
}
// Create intermediate directories
dir := filepath.Dir(safePath)
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("failed to create directories: %w", err)
}
// Atomically create the file, failing if it already exists
f, err := os.OpenFile(safePath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
if err != nil {
if os.IsExist(err) {
return fmt.Errorf("file already exists: %s", reqPath)
}
return fmt.Errorf("failed to create file: %w", err)
}
_ = f.Close()
// Notify with the relative path
cleanWorkDir := filepath.Clean(wt.workDir)
relPath := strings.TrimPrefix(safePath, cleanWorkDir+string(os.PathSeparator))
wt.notifyFileChange(relPath, types.FileOpCreate)
return nil
}
// DeleteFile deletes a file or directory from the workspace.
func (wt *WorkspaceTracker) DeleteFile(reqPath string) error {
safePath, err := wt.resolveSafePath(reqPath)
if err != nil {
return err
}
cleanWorkDir := wt.resolvedWorkDir()
if safePath == cleanWorkDir {
return fmt.Errorf("cannot delete workspace root")
}
if err := wt.validateWorkspacePaths(safePath); err != nil {
return err
}
// Check if file exists
info, err := os.Stat(safePath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file does not exist: %s", reqPath)
}
return fmt.Errorf("failed to stat file: %w", err)
}
if info.IsDir() {
if err := os.RemoveAll(safePath); err != nil {
return fmt.Errorf("failed to delete directory: %w", err)
}
} else {
if err := os.Remove(safePath); err != nil {
return fmt.Errorf("failed to delete file: %w", err)
}
}
relPath := strings.TrimPrefix(safePath, cleanWorkDir+string(os.PathSeparator))
wt.notifyFileChange(relPath, types.FileOpRemove)
return nil
}
// RenameFile renames/moves a file or directory in the workspace.
func (wt *WorkspaceTracker) RenameFile(oldPath, newPath string) error {
if oldPath == "" || newPath == "" {
return fmt.Errorf("old_path and new_path are required")
}
oldSafePath, err := wt.resolveSafePath(oldPath)
if err != nil {
return err
}
newSafePath, err := wt.resolveSafePath(newPath)
if err != nil {
return err
}
if err := wt.validateWorkspacePaths(oldSafePath, newSafePath); err != nil {
return err
}
if oldSafePath == newSafePath {
return nil
}
if err := validateSourceExists(oldSafePath, oldPath); err != nil {
return err
}
if err := validateTargetAvailable(newSafePath, newPath); err != nil {
return err
}
parentDir := filepath.Dir(newSafePath)
if err := os.MkdirAll(parentDir, 0o755); err != nil {
return fmt.Errorf("failed to create target parent directories: %w", err)
}
if err := os.Rename(oldSafePath, newSafePath); err != nil {
return fmt.Errorf("failed to rename path: %w", err)
}
cleanWorkDir := wt.resolvedWorkDir()
oldRelPath := strings.TrimPrefix(oldSafePath, cleanWorkDir+string(os.PathSeparator))
newRelPath := strings.TrimPrefix(newSafePath, cleanWorkDir+string(os.PathSeparator))
wt.notifyFileChange(oldRelPath, types.FileOpRename)
if newRelPath != oldRelPath {
wt.notifyFileChange(newRelPath, types.FileOpRename)
}
return nil
}
// validateWorkspacePaths checks that all provided paths are strictly inside the workspace.
func (wt *WorkspaceTracker) validateWorkspacePaths(paths ...string) error {
cleanWorkDir := wt.resolvedWorkDir()
workDirPrefix := cleanWorkDir + string(os.PathSeparator)
for _, p := range paths {
if !strings.HasPrefix(p, workDirPrefix) {
return fmt.Errorf("path outside workspace")
}
}
return nil
}
func validateSourceExists(safePath, reqPath string) error {
_, err := os.Stat(safePath)
if err == nil {
return nil
}
if os.IsNotExist(err) {
return fmt.Errorf("path does not exist: %s", reqPath)
}
return fmt.Errorf("failed to stat path: %w", err)
}
func validateTargetAvailable(safePath, reqPath string) error {
_, err := os.Stat(safePath)
if err == nil {
return fmt.Errorf("target already exists: %s", reqPath)
}
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat target: %w", err)
}
// calculateSHA256 calculates the SHA256 hash of a string
func calculateSHA256(content string) string {
hash := sha256.Sum256([]byte(content))
return hex.EncodeToString(hash[:])
}
// scoredMatch holds a file path and its match score for sorting
type scoredMatch struct {
path string
score int
}
// GetFileContentAtRef returns the content of a file at a specific git ref (branch, commit, HEAD, etc).
// If the file is not valid UTF-8, it is base64-encoded and isBinary is true.
func (wt *WorkspaceTracker) GetFileContentAtRef(ctx context.Context, reqPath string, ref string) (string, int64, bool, error) {
// Validate path to prevent directory traversal
cleanPath := filepath.Clean(reqPath)
if strings.HasPrefix(cleanPath, "..") || filepath.IsAbs(cleanPath) {
return "", 0, false, fmt.Errorf("invalid path: path traversal detected")
}
// Use git show to get the file content at the specified ref
// Format: git show <ref>:<path>
gitRef := fmt.Sprintf("%s:%s", ref, cleanPath)
// Preflight: check blob size before materializing content to avoid memory spikes.
sizeCmd := exec.CommandContext(ctx, "git", "cat-file", "-s", gitRef)
sizeCmd.Dir = wt.workDir
sizeOut, err := sizeCmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
stderr := string(exitErr.Stderr)
if strings.Contains(stderr, "does not exist") ||
strings.Contains(stderr, "Not a valid object") ||
strings.Contains(stderr, "fatal: path") ||
strings.Contains(stderr, "fatal: not a valid") {
return "", 0, false, fmt.Errorf("file not found at ref %s: %s", ref, cleanPath)
}
}
return "", 0, false, fmt.Errorf("failed to stat file at ref: %w", err)
}
blobSize, err := strconv.ParseInt(strings.TrimSpace(string(sizeOut)), 10, 64)
if err != nil {
return "", 0, false, fmt.Errorf("failed to parse file size at ref: %w", err)
}
if blobSize > maxFileSize {
return "", blobSize, false, fmt.Errorf("file too large (max 10MB)")
}
cmd := exec.CommandContext(ctx, "git", "show", gitRef)
cmd.Dir = wt.workDir
content, err := cmd.Output()
if err != nil {
return "", 0, false, fmt.Errorf("failed to get file at ref: %w", err)
}
size := int64(len(content))
// Detect binary: if content is not valid UTF-8, base64-encode it
if !utf8.Valid(content) {
encoded := base64.StdEncoding.EncodeToString(content)
return encoded, size, true, nil
}
return string(content), size, false, nil
}
// SearchFiles searches for files matching the query string.
// It uses fuzzy matching with scoring based on how well the query matches.
func (wt *WorkspaceTracker) SearchFiles(query string, limit int) []string {
if query == "" {
return []string{}
}
if limit <= 0 {
limit = 20
}
query = strings.ToLower(query)
var matches []scoredMatch
wt.mu.RLock()
files := wt.currentFiles.Files
wt.mu.RUnlock()
for _, file := range files {
path := file.Path
lowerPath := strings.ToLower(path)
name := filepath.Base(lowerPath)
score := 0
switch {
case name == query:
score = 100 // Exact filename match
case strings.HasPrefix(name, query):
score = 75 // Filename starts with query
case strings.Contains(name, query):
score = 50 // Filename contains query
case strings.Contains(lowerPath, query):
score = 25 // Path contains query
}
if score > 0 {
matches = append(matches, scoredMatch{path: path, score: score})
}
}
// Sort by score descending
sort.Slice(matches, func(i, j int) bool {
if matches[i].score != matches[j].score {
return matches[i].score > matches[j].score
}
// Secondary sort by path length (shorter paths first)
return len(matches[i].path) < len(matches[j].path)
})
// Return top limit results
result := make([]string, 0, limit)
for i := 0; i < len(matches) && i < limit; i++ {
result = append(result, matches[i].path)
}
return result
}