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}