v0.40.0 076e2bb

lsp_diagnostics.go RawHistoryFolder

1package tools
2
3import (
4	"context"
5	"errors"
6	"strings"
7	"time"
8
9	"github.com/bnema/zut/packages/agent/lsp"
10	"github.com/bnema/zut/packages/core"
11	"github.com/bnema/zut/packages/provider"
12)
13
14const writeDiagnosticsTimeout = 2 * time.Second
15
16// CloseLSPManagers closes every manager referenced by the registry. Write,
17// edit, and lsp share one manager in normal runs, so de-duplicate pointers
18// before closing them. This is used by mode owners when an agent or registry
19// is replaced; LSP processes should not outlive the session that created them.
20func CloseLSPManagers(reg core.Registry) error {
21	managers := make(map[*lsp.Manager]struct{})
22	for _, tool := range reg {
23		switch value := tool.(type) {
24		case *LSPTool:
25			if value != nil && value.Manager != nil {
26				managers[value.Manager] = struct{}{}
27			}
28		case *WriteTool:
29			if value != nil && value.LSP != nil {
30				managers[value.LSP] = struct{}{}
31			}
32		case *EditTool:
33			if value != nil && value.LSP != nil {
34				managers[value.LSP] = struct{}{}
35			}
36		}
37	}
38	var firstErr error
39	for manager := range managers {
40		if err := manager.Close(); err != nil && firstErr == nil {
41			firstErr = err
42		}
43	}
44	return firstErr
45}
46
47// attachWriteDiagnostics performs a bounded post-write diagnostics pass. A
48// failed or slow language server must never turn a successful filesystem
49// mutation into a failed tool call, so callers only attach diagnostics that
50// were actually returned before the short budget expired.
51func attachWriteDiagnostics(ctx context.Context, cwd, path string, manager *lsp.Manager, result *core.ToolResult) {
52	attachMutationDiagnostics(ctx, cwd, []string{path}, manager, result)
53}
54
55const maxMutationDiagnosticFiles = 3
56
57func attachMutationDiagnostics(ctx context.Context, cwd string, paths []string, manager *lsp.Manager, result *core.ToolResult) {
58	if manager == nil || result == nil || len(paths) == 0 {
59		return
60	}
61	if ctx == nil {
62		ctx = context.Background()
63	}
64	diagnosticsCtx, cancel := context.WithTimeout(ctx, writeDiagnosticsTimeout)
65	defer cancel()
66	if len(paths) > maxMutationDiagnosticFiles {
67		paths = paths[:maxMutationDiagnosticFiles]
68		if details, ok := result.Details.(map[string]any); ok {
69			details["diagnostics_files_truncated"] = true
70		}
71	}
72	var diagnostics []lsp.Diagnostic
73	for _, path := range paths {
74		found, err := manager.Diagnostics(diagnosticsCtx, cwd, path)
75		if err != nil {
76			setDiagnosticsError(result, err)
77			continue
78		}
79		found = manager.ReduceDiagnostics(cwd, path, found)
80		diagnostics = append(diagnostics, found...)
81	}
82	if len(diagnostics) == 0 {
83		return
84	}
85	summary := lsp.SummarizeDiagnostics(diagnostics, cwd, 8)
86	if strings.TrimSpace(summary) == "" || summary == "No diagnostics." {
87		return
88	}
89	for index, block := range result.Content {
90		text, ok := block.(provider.TextBlock)
91		if !ok {
92			continue
93		}
94		text.Text = strings.TrimRight(text.Text, "\n") + "\n\n" + summary + "\n"
95		result.Content[index] = text
96		break
97	}
98	if details, ok := result.Details.(map[string]any); ok {
99		details["diagnostics"] = diagnostics
100		details["diagnostics_summary"] = summary
101	}
102}
103
104func setDiagnosticsError(result *core.ToolResult, err error) {
105	if details, ok := result.Details.(map[string]any); ok {
106		status := "unavailable"
107		switch {
108		case errors.Is(err, context.DeadlineExceeded):
109			status = "timeout"
110		case errors.Is(err, context.Canceled):
111			status = "canceled"
112		}
113		details["diagnostics_status"] = status
114		details["diagnostics_error"] = err.Error()
115	}
116}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close