From 3c5fcff3855762e6d24cf19fd45d8d69253ee594 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 26 Feb 2026 13:52:36 -0500 Subject: [PATCH] perf(tools/view): pause briefly for LSP diagnostics when viewing a file --- internal/agent/tools/diagnostics.go | 21 +++++++++++++++++++++ internal/agent/tools/view.go | 2 ++ 2 files changed, 23 insertions(+) diff --git a/internal/agent/tools/diagnostics.go b/internal/agent/tools/diagnostics.go index 3adadd17f70584be221458dda8210f3e644ac4fd..06c2c1e813a61099be6aad26a988317df38b639b 100644 --- a/internal/agent/tools/diagnostics.go +++ b/internal/agent/tools/diagnostics.go @@ -59,6 +59,27 @@ func openInLSPs( } } +// waitForLSPDiagnostics waits briefly for diagnostics publication after a file +// has been opened. Intended for read-only situations where viewing up-to-date +// files matters but latency should remain low (i.e. when using the view tool). +func waitForLSPDiagnostics( + ctx context.Context, + manager *lsp.Manager, + filepath string, + timeout time.Duration, +) { + if filepath == "" || manager == nil || timeout <= 0 { + return + } + + for client := range manager.Clients().Seq() { + if !client.HandlesFile(filepath) { + continue + } + client.WaitForDiagnostics(ctx, timeout) + } +} + // notifyLSPs notifies LSP servers that a file has changed and waits for // updated diagnostics. Use this after edit/multiedit operations. func notifyLSPs( diff --git a/internal/agent/tools/view.go b/internal/agent/tools/view.go index f304e9f7c45c195b6aef7d768b6671c1cdf74e82..7b73e2e1fb0f95b3d729bc3f5da72a99d362f520 100644 --- a/internal/agent/tools/view.go +++ b/internal/agent/tools/view.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "strings" + "time" "unicode/utf8" "charm.land/fantasy" @@ -194,6 +195,7 @@ func NewViewTool( } openInLSPs(ctx, lspManager, filePath) + waitForLSPDiagnostics(ctx, lspManager, filePath, 300*time.Millisecond) output := "\n" output += addLineNumbers(content, params.Offset+1)