@@ -5,13 +5,9 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8- "io/fs"
9- "os"
10- "path/filepath"
118 "strings"
129 "time"
1310
14- "github.com/xonecas/symb/internal/delta"
1511 "github.com/xonecas/symb/internal/mcp"
1612 "github.com/xonecas/symb/internal/shell"
1713 )
@@ -28,7 +24,7 @@ func NewShellTool() mcp.Tool {
2824 return mcp.Tool{
2925 Name: "Shell",
3026 Description: `Execute a shell command in an in-process POSIX interpreter.
31-Commands run inside the project working directory. Shell state (cwd, env vars) persists across calls within the same session.
27+Commands run inside the project working directory. The shell is anchored to the project root — you cannot cd outside it. Env vars persist across calls.
3228 Dangerous commands (network, sudo, package managers, system modification) are blocked.
3329 Use this for: running builds, tests, linters, git operations, file manipulation, and inspecting project state.`,
3430 InputSchema: json.RawMessage(`{
@@ -45,16 +41,15 @@ Use this for: running builds, tests, linters, git operations, file manipulation,
4541
4642 // ShellHandler handles Shell tool calls.
4743 type ShellHandler struct {
48- sh *shell.Shell
49- deltaTracker *delta.Tracker
44+ sh *shell.Shell
5045 // OnOutput is called with incremental output chunks for real-time streaming.
5146 // May be nil.
5247 OnOutput func(chunk string)
5348 }
5449
5550 // NewShellHandler creates a handler for the Shell tool.
56-func NewShellHandler(sh *shell.Shell, dt *delta.Tracker) *ShellHandler {
57- return &ShellHandler{sh: sh, deltaTracker: dt}
51+func NewShellHandler(sh *shell.Shell) *ShellHandler {
52+ return &ShellHandler{sh: sh}
5853 }
5954
6055 // Handle implements the mcp.ToolHandler interface.
@@ -78,15 +73,6 @@ func (h *ShellHandler) Handle(ctx context.Context, arguments json.RawMessage) (*
7873 ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
7974 defer cancel()
8075
81- // Snapshot the shell's working directory for undo — only when the delta
82- // tracker is active (has a turn), to avoid expensive walks on read-only commands.
83- shellCwd := h.sh.Dir()
84- trackDeltas := h.deltaTracker != nil && h.deltaTracker.TurnID() > 0
85- var preSnap map[string]fileSnapshot
86- if trackDeltas {
87- preSnap = snapshotDir(shellCwd)
88- }
89-
9076 // Execute command with streaming output.
9177 var stdout, stderr bytes.Buffer
9278
@@ -98,14 +84,6 @@ func (h *ShellHandler) Handle(ctx context.Context, arguments json.RawMessage) (*
9884 execErr = h.sh.ExecStream(ctx, args.Command, &stdout, &stderr)
9985 }
10086
101- // Post-execution: diff the *original* cwd to record deltas for undo.
102- // Always use shellCwd (the pre-exec directory) for both snapshots so a
103- // `cd` inside the command doesn't cause a cross-directory mismatch.
104- if trackDeltas {
105- postSnap := snapshotDir(shellCwd)
106- recordDeltas(h.deltaTracker, shellCwd, preSnap, postSnap)
107- }
108-
10987 // Format result.
11088 exitCode := shell.ExitCode(execErr)
11189 output := formatShellOutput(stdout.String(), stderr.String(), exitCode, ctx.Err())
@@ -176,85 +154,3 @@ func truncateMiddle(s string, maxChars int) string {
176154 half := maxChars / 2
177155 return string(runes[:half]) + "\n\n... [truncated] ...\n\n" + string(runes[len(runes)-half:])
178156 }
179-
180-// ---------------------------------------------------------------------------
181-// CWD snapshot for undo
182-// ---------------------------------------------------------------------------
183-
184-// fileSnapshot holds mtime+size+content for change detection and undo.
185-type fileSnapshot struct {
186- modTime time.Time
187- size int64
188- content []byte // pre-read for undo; nil for large files
189-}
190-
191-// maxSnapshotFileSize is the max file size we pre-read for undo (1 MB).
192-const maxSnapshotFileSize = 1 << 20
193-
194-// skipDirs are directories skipped during snapshot walks.
195-var skipDirs = map[string]bool{
196- ".git": true, "node_modules": true, "__pycache__": true,
197- ".venv": true, "vendor": true, ".cache": true, ".next": true,
198- "dist": true, "build": true, "target": true,
199-}
200-
201-// snapshotDir walks root and returns a map of relative path -> fileSnapshot.
202-// Files under maxSnapshotFileSize have their content pre-read for undo.
203-func snapshotDir(root string) map[string]fileSnapshot {
204- snap := make(map[string]fileSnapshot)
205- _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
206- if err != nil {
207- return nil
208- }
209- if d.IsDir() {
210- if skipDirs[d.Name()] {
211- return filepath.SkipDir
212- }
213- return nil
214- }
215- rel, err := filepath.Rel(root, path)
216- if err != nil {
217- return nil
218- }
219- info, err := d.Info()
220- if err != nil {
221- return nil
222- }
223- fs := fileSnapshot{modTime: info.ModTime(), size: info.Size()}
224- if info.Size() <= maxSnapshotFileSize {
225- fs.content, _ = os.ReadFile(path)
226- }
227- snap[rel] = fs
228- return nil
229- })
230- return snap
231-}
232-
233-// recordDeltas compares pre/post snapshots and records deltas for undo.
234-func recordDeltas(dt *delta.Tracker, root string, pre, post map[string]fileSnapshot) {
235- // New or modified files.
236- for rel, postInfo := range post {
237- absPath := filepath.Join(root, rel)
238- preInfo, existed := pre[rel]
239- if !existed {
240- dt.RecordCreate(absPath)
241- continue
242- }
243- if preInfo.modTime != postInfo.modTime || preInfo.size != postInfo.size {
244- // File was modified — use pre-read content for undo.
245- dt.RecordModify(absPath, preInfo.content)
246- }
247- }
248- // Deleted files — existed in pre but not in post.
249- for rel, preInfo := range pre {
250- if _, exists := post[rel]; !exists {
251- absPath := filepath.Join(root, rel)
252- if preInfo.content != nil {
253- // We have the original content; record as modify so undo restores it.
254- dt.RecordModify(absPath, preInfo.content)
255- }
256- // If content is nil (file was > maxSnapshotFileSize), we can't
257- // restore it. Skip rather than writing a 0-byte file on undo.
258- }
259- }
260-}