-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoctor.go
More file actions
230 lines (206 loc) · 7.33 KB
/
Copy pathdoctor.go
File metadata and controls
230 lines (206 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package contexting
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type DoctorStatus string
const (
DoctorPass DoctorStatus = "pass"
DoctorWarn DoctorStatus = "warn"
DoctorFail DoctorStatus = "fail"
)
type DoctorCheck struct {
Name string `json:"name"`
Status DoctorStatus `json:"status"`
Message string `json:"message"`
Suggestion string `json:"suggestion,omitempty"`
}
type DoctorReport struct {
Healthy bool `json:"healthy"`
Checks []DoctorCheck `json:"checks"`
}
type DoctorOptions struct {
ConfigPath string
RootPath string
IndexPath string
CachePath string
WriteCheck bool
}
func RunDoctor(opts DoctorOptions) DoctorReport {
report := DoctorReport{Healthy: true, Checks: make([]DoctorCheck, 0, 8)}
cfg := &ContextingConfig{}
configExists := false
if opts.ConfigPath != "" {
if _, err := os.Stat(opts.ConfigPath); err == nil {
configExists = true
report.add(DoctorCheck{Name: "config.exists", Status: DoctorPass, Message: "Config file found: " + opts.ConfigPath})
loaded, err := LoadContextingConfig(opts.ConfigPath)
if err != nil {
report.add(DoctorCheck{Name: "config.parse", Status: DoctorFail, Message: err.Error(), Suggestion: "Fix TOML syntax or run `ctxt config init --force` to reset."})
} else {
cfg = loaded
report.add(DoctorCheck{Name: "config.parse", Status: DoctorPass, Message: "Config parsed successfully."})
}
} else if os.IsNotExist(err) {
report.add(DoctorCheck{Name: "config.exists", Status: DoctorWarn, Message: "Config file not found: " + opts.ConfigPath, Suggestion: "Run `ctxt config init` to create a starter config."})
} else {
report.add(DoctorCheck{Name: "config.exists", Status: DoctorFail, Message: err.Error(), Suggestion: "Check file permissions and path."})
}
}
rootPath := opts.RootPath
if rootPath == "" {
if cfg.Init.RootPath != "" {
rootPath = cfg.Init.RootPath
} else if cfg.Watch.RootPath != "" {
rootPath = cfg.Watch.RootPath
} else {
rootPath = "."
}
}
absRoot, err := filepath.Abs(rootPath)
if err != nil {
report.add(DoctorCheck{Name: "root.resolve", Status: DoctorFail, Message: err.Error(), Suggestion: "Use a valid root path."})
return report
}
info, err := os.Stat(absRoot)
if err != nil {
report.add(DoctorCheck{Name: "root.exists", Status: DoctorFail, Message: err.Error(), Suggestion: "Create the directory or pass --root to an existing project."})
return report
}
if !info.IsDir() {
report.add(DoctorCheck{Name: "root.exists", Status: DoctorFail, Message: "Root path is not a directory: " + absRoot, Suggestion: "Pass a directory path with --root."})
return report
}
report.add(DoctorCheck{Name: "root.exists", Status: DoctorPass, Message: "Project root: " + absRoot})
common := defaultCommonFlags()
if configExists {
applyCommonConfigNoCLI(&common, cfg.Common)
}
common.normalize()
indexPath := opts.IndexPath
if indexPath == "" {
if cfg.Search.IndexPath != "" {
indexPath = cfg.Search.IndexPath
} else {
indexPath = common.OutputPath
}
}
cachePath := opts.CachePath
if cachePath == "" {
cachePath = common.SynonymCache
}
indexPath = resolveConfigPath(opts.ConfigPath, indexPath)
if resolved, absErr := filepath.Abs(indexPath); absErr == nil {
indexPath = resolved
}
cachePath = resolveProjectPath(absRoot, cachePath)
checkIndexFile(&report, indexPath)
checkCacheFile(&report, cachePath)
checkAPIKey(&report, common, cfg.LLM)
if opts.WriteCheck {
checkWriteAccess(&report, absRoot)
}
return report
}
func checkIndexFile(report *DoctorReport, indexPath string) {
if _, err := os.Stat(indexPath); err != nil {
if os.IsNotExist(err) {
report.add(DoctorCheck{Name: "index.exists", Status: DoctorWarn, Message: "Index file not found: " + indexPath, Suggestion: "Run `ctxt init` to generate the index."})
return
}
report.add(DoctorCheck{Name: "index.exists", Status: DoctorFail, Message: err.Error(), Suggestion: "Check file permissions and path."})
return
}
index, err := LoadContextIndex(indexPath)
if err != nil {
report.add(DoctorCheck{Name: "index.parse", Status: DoctorFail, Message: err.Error(), Suggestion: "Regenerate with `ctxt init` if file is corrupted."})
return
}
stats := ComputeStats(index.Tree)
report.add(DoctorCheck{Name: "index.parse", Status: DoctorPass, Message: fmt.Sprintf("Index OK: %d nodes (%d files, %d dirs)", stats.TotalNodes, stats.TotalFiles, stats.TotalDirs)})
}
func checkCacheFile(report *DoctorReport, cachePath string) {
if _, err := os.Stat(cachePath); err != nil {
if os.IsNotExist(err) {
report.add(DoctorCheck{Name: "cache.exists", Status: DoctorWarn, Message: "Synonym cache not found: " + cachePath, Suggestion: "Run `ctxt init` or `watch` to create cache."})
return
}
report.add(DoctorCheck{Name: "cache.exists", Status: DoctorFail, Message: err.Error(), Suggestion: "Check cache path permissions."})
return
}
cache, err := LoadSynonymCache(cachePath)
if err != nil {
report.add(DoctorCheck{Name: "cache.parse", Status: DoctorFail, Message: err.Error(), Suggestion: "Delete cache file and let ctxt recreate it."})
return
}
report.add(DoctorCheck{Name: "cache.parse", Status: DoctorPass, Message: fmt.Sprintf("Cache OK: %d entries", len(cache))})
}
func checkAPIKey(report *DoctorReport, flags CommonFlags, cfg LLMConfig) {
_, _, key, _, _, _ := resolveLLMConfig(flags, cfg)
if key == "" {
report.add(DoctorCheck{Name: "llm.api_key", Status: DoctorWarn, Message: "LLM API key not configured; local indexing remains available", Suggestion: "Configure llm.api_key_env for optional LLM synonym generation."})
return
}
report.add(DoctorCheck{Name: "llm.api_key", Status: DoctorPass, Message: "Configured LLM API key is available."})
}
func checkWriteAccess(report *DoctorReport, root string) {
ctxDir := filepath.Join(root, ".ctxt")
if err := os.MkdirAll(ctxDir, 0o755); err != nil {
report.add(DoctorCheck{Name: "root.write", Status: DoctorFail, Message: err.Error(), Suggestion: "Ensure write permission on project root."})
return
}
tmp, err := os.CreateTemp(ctxDir, ".doctor-*.tmp")
if err != nil {
report.add(DoctorCheck{Name: "root.write", Status: DoctorFail, Message: err.Error(), Suggestion: "Ensure write permission on project root."})
return
}
_ = tmp.Close()
_ = os.Remove(tmp.Name())
report.add(DoctorCheck{Name: "root.write", Status: DoctorPass, Message: "Project root is writable."})
}
func (r *DoctorReport) add(check DoctorCheck) {
r.Checks = append(r.Checks, check)
if check.Status == DoctorFail {
r.Healthy = false
}
}
func (r DoctorReport) toJSON() (string, error) {
bytes, err := json.MarshalIndent(r, "", " ")
if err != nil {
return "", err
}
return string(bytes), nil
}
func defaultCommonFlags() CommonFlags {
flags := CommonFlags{}
flags.normalize()
return flags
}
func applyCommonConfigNoCLI(flags *CommonFlags, cfg CommonConfig) {
if cfg.OutputPath != "" {
flags.OutputPath = cfg.OutputPath
}
if cfg.SynonymCache != "" {
flags.SynonymCache = cfg.SynonymCache
}
if cfg.Model != "" {
flags.Model = cfg.Model
}
if cfg.APIKey != "" {
flags.APIKey = cfg.APIKey
}
if cfg.BatchSize > 0 {
flags.BatchSize = cfg.BatchSize
}
if cfg.SynonymsPerName > 0 {
flags.SynonymsPerName = cfg.SynonymsPerName
}
if len(cfg.ExtraIgnores) > 0 {
flags.ExtraIgnores = append([]string(nil), cfg.ExtraIgnores...)
}
if cfg.Verbose != nil {
flags.Verbose = *cfg.Verbose
}
}