perf(lsp): don't watch for changes when simply reading files

Christian Rocha created

Change summary

internal/agent/tools/diagnostics.go | 26 +++++++++++++++++++++++---
internal/agent/tools/view.go        |  2 +-
2 files changed, 24 insertions(+), 4 deletions(-)

Detailed changes

internal/agent/tools/diagnostics.go 🔗

@@ -37,16 +37,36 @@ func NewDiagnosticsTool(lspManager *lsp.Manager) fantasy.AgentTool {
 		})
 }
 
-func notifyLSPs(
+// openInLSPs ensures LSP servers are running and aware of the file, but does
+// not notify changes or wait for fresh diagnostics. Use this for read-only
+// operations like view where the file content hasn't changed.
+func openInLSPs(
 	ctx context.Context,
 	manager *lsp.Manager,
 	filepath string,
 ) {
-	if filepath == "" {
+	if filepath == "" || manager == nil {
 		return
 	}
 
-	if manager == nil {
+	manager.Start(ctx, filepath)
+
+	for client := range manager.Clients().Seq() {
+		if !client.HandlesFile(filepath) {
+			continue
+		}
+		_ = client.OpenFileOnDemand(ctx, filepath)
+	}
+}
+
+// notifyLSPs notifies LSP servers that a file has changed and waits for
+// updated diagnostics. Use this after edit/multiedit operations.
+func notifyLSPs(
+	ctx context.Context,
+	manager *lsp.Manager,
+	filepath string,
+) {
+	if filepath == "" || manager == nil {
 		return
 	}
 

internal/agent/tools/view.go 🔗

@@ -193,7 +193,7 @@ func NewViewTool(
 				return fantasy.NewTextErrorResponse("File content is not valid UTF-8"), nil
 			}
 
-			notifyLSPs(ctx, lspManager, filePath)
+			openInLSPs(ctx, lspManager, filePath)
 			output := "<file>\n"
 			output += addLineNumbers(content, params.Offset+1)