on main help sign in
perf: reduce memory usage (#1812)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

commitd1382bb55d03936047df0fc81b3ab9da3cafe4b5 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateFri 9 Jan 2026, 14:21 (9 months ago) parent617f9e2
16 files changed+193−80
Taskfile.yaml+2−1 go.mod+7−1 go.sum+26−6 internal/agent/common_test.go+4 internal/agent/tools/fetch_helpers.go+3−2 internal/agent/tools/mcp/init.go+1−2 internal/app/lsp_events.go+1−2 internal/csync/maps.go+8−4 internal/csync/versionedmap.go+5 internal/db/connect.go+3−24 internal/db/connect_modernc.go+30 internal/db/connect_ncruces.go+37 internal/lsp/client.go+49−1 internal/oauth/copilot/client.go+2−1 internal/tui/components/chat/header/header.go+1−8 internal/tui/components/lsp/lsp.go+14−28
Taskfile.yaml+2−1
@@ -44,7 +44,8 @@ tasks:
4444 run:
4545 desc: Run build
4646 cmds:
47− - go run . {{.CLI_ARGS}}
47+ - go build -o crush .
48+ - ./crush {{.CLI_ARGS}}
4849
4950 test:
5051 desc: Run tests
go.mod+7−1
@@ -25,7 +25,7 @@ require (
2525 github.com/charmbracelet/x/ansi v0.11.3
2626 github.com/charmbracelet/x/editor v0.2.0
2727 github.com/charmbracelet/x/etag v0.2.0
28− github.com/charmbracelet/x/exp/charmtone v0.0.0-20250708181618-a60a724ba6c3
28+ github.com/charmbracelet/x/exp/charmtone v0.0.0-20260109001716-2fbdffcb221f
2929 github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f
3030 github.com/charmbracelet/x/exp/ordered v0.1.0
3131 github.com/charmbracelet/x/exp/slice v0.0.0-20251201173703-9f73bfd934ff
@@ -63,6 +63,7 @@ require (
6363 golang.org/x/text v0.32.0
6464 gopkg.in/natefinch/lumberjack.v2 v2.2.1
6565 gopkg.in/yaml.v3 v3.0.1
66+ modernc.org/sqlite v1.43.0
6667 mvdan.cc/sh/moreinterp v0.0.0-20250902163504-3cf4fd5717a5
6768 mvdan.cc/sh/v3 v3.12.1-0.20250902163504-3cf4fd5717a5
6869)
@@ -141,9 +142,11 @@ require (
141142 github.com/muesli/mango-cobra v1.2.0 // indirect
142143 github.com/muesli/mango-pflag v0.1.0 // indirect
143144 github.com/muesli/roff v0.1.0 // indirect
145+ github.com/ncruces/go-strftime v0.1.9 // indirect
144146 github.com/ncruces/julianday v1.0.0 // indirect
145147 github.com/pierrec/lz4/v4 v4.1.22 // indirect
146148 github.com/pmezard/go-difflib v1.0.0 // indirect
149+ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
147150 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
148151 github.com/sethvargo/go-retry v0.3.0 // indirect
149152 github.com/sourcegraph/jsonrpc2 v0.2.1 // indirect
@@ -180,4 +183,7 @@ require (
180183 google.golang.org/protobuf v1.36.10 // indirect
181184 gopkg.in/dnaeon/go-vcr.v4 v4.0.6-0.20251110073552-01de4eb40290 // indirect
182185 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
186+ modernc.org/libc v1.66.10 // indirect
187+ modernc.org/mathutil v1.7.1 // indirect
188+ modernc.org/memory v1.11.0 // indirect
183189)
go.sum+26−6
@@ -108,8 +108,8 @@ github.com/charmbracelet/x/editor v0.2.0 h1:7XLUKtaRaB8jN7bWU2p2UChiySyaAuIfYiIR
108108github.com/charmbracelet/x/editor v0.2.0/go.mod h1:p3oQ28TSL3YPd+GKJ1fHWcp+7bVGpedHpXmo0D6t1dY=
109109github.com/charmbracelet/x/etag v0.2.0 h1:Euj1VkheoHfTYA9y+TCwkeXF/hN8Fb9l4LqZl79pt04=
110110github.com/charmbracelet/x/etag v0.2.0/go.mod h1:C1B7/bsgvzzxpfu0Rabbd+rTHJa5TmC/qgTseCf6DF0=
111−github.com/charmbracelet/x/exp/charmtone v0.0.0-20250708181618-a60a724ba6c3 h1:1xwHZg6eMZ9Wv5TE1UGub6ARubyOd1Lo5kPUI/6VL50=
112−github.com/charmbracelet/x/exp/charmtone v0.0.0-20250708181618-a60a724ba6c3/go.mod h1:T9jr8CzFpjhFVHjNjKwbAD7KwBNyFnj2pntAO7F2zw0=
111+github.com/charmbracelet/x/exp/charmtone v0.0.0-20260109001716-2fbdffcb221f h1:OKFNbG2sSmgpQW9EC3gYNG+QrcQ4+wWYjzfmJvWkkDo=
112+github.com/charmbracelet/x/exp/charmtone v0.0.0-20260109001716-2fbdffcb221f/go.mod h1:nsExn0DGyX0lh9LwLHTn2Gg+hafdzfSXnC+QmEJTZFY=
113113github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA=
114114github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I=
115115github.com/charmbracelet/x/exp/ordered v0.1.0 h1:55/qLwjIh0gL0Vni+QAWk7T/qRVP6sBf+2agPBgnOFE=
@@ -184,6 +184,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
184184github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
185185github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
186186github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
187+github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
188+github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
187189github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0=
188190github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM=
189191github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -501,14 +503,32 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
501503gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
502504gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
503505gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
504−modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
505−modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
506+modernc.org/cc/v4 v4.26.5 h1:xM3bX7Mve6G8K8b+T11ReenJOT+BmVqQj0FY5T4+5Y4=
507+modernc.org/cc/v4 v4.26.5/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
508+modernc.org/ccgo/v4 v4.28.1 h1:wPKYn5EC/mYTqBO373jKjvX2n+3+aK7+sICCv4Fjy1A=
509+modernc.org/ccgo/v4 v4.28.1/go.mod h1:uD+4RnfrVgE6ec9NGguUNdhqzNIeeomeXf6CL0GTE5Q=
510+modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
511+modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
512+modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
513+modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
514+modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
515+modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
516+modernc.org/libc v1.66.10 h1:yZkb3YeLx4oynyR+iUsXsybsX4Ubx7MQlSYEw4yj59A=
517+modernc.org/libc v1.66.10/go.mod h1:8vGSEwvoUoltr4dlywvHqjtAqHBaw0j1jI7iFBTAr2I=
506518modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
507519modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
508520modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
509521modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
510−modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
511−modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
522+modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
523+modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
524+modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
525+modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
526+modernc.org/sqlite v1.43.0 h1:8YqiFx3G1VhHTXO2Q00bl1Wz9KhS9Q5okwfp9Y97VnA=
527+modernc.org/sqlite v1.43.0/go.mod h1:+VkC6v3pLOAE0A0uVucQEcbVW0I5nHCeDaBf+DpsQT8=
528+modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
529+modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
530+modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
531+modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
512532mvdan.cc/sh/moreinterp v0.0.0-20250902163504-3cf4fd5717a5 h1:mO2lyKtGwu4mGQ+Qqjx0+fd5UU5BXhX/rslFmxd5aco=
513533mvdan.cc/sh/moreinterp v0.0.0-20250902163504-3cf4fd5717a5/go.mod h1:Of9PCedbLDYT8b3EyiYG64rNnx5nOp27OLCVdDrjJyo=
514534mvdan.cc/sh/v3 v3.12.1-0.20250902163504-3cf4fd5717a5 h1:e7Z/Lgw/zMijvQBVrfh/vUDZ+9FpuSLrJDVGBuoJtuo=
internal/agent/common_test.go+4
@@ -182,6 +182,10 @@ func coderAgent(r *vcr.Recorder, env fakeEnv, large, small fantasy.LanguageModel
182182 // would be included in prompt and break VCR cassette matching.
183183 cfg.Options.SkillsPaths = []string{}
184184
185+ // Clear LSP config to ensure test reproducibility - user's LSP config
186+ // would be included in prompt and break VCR cassette matching.
187+ cfg.LSP = nil
188+
185189 systemPrompt, err := prompt.Build(context.TODO(), large.Provider(), large.Model(), *cfg)
186190 if err != nil {
187191 return nil, err
internal/agent/tools/fetch_helpers.go+3−2
@@ -19,6 +19,8 @@ import (
1919// BrowserUserAgent is a realistic browser User-Agent for better compatibility.
2020const BrowserUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
2121
22+var multipleNewlinesRe = regexp.MustCompile(`\n{3,}`)
23+
2224// FetchURLAndConvert fetches a URL and converts HTML content to markdown.
2325func FetchURLAndConvert(ctx context.Context, client *http.Client, url string) (string, error) {
2426 req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
@@ -128,8 +130,7 @@ func removeNoisyElements(htmlContent string) string {
128130// cleanupMarkdown removes excessive whitespace and blank lines from markdown.
129131func cleanupMarkdown(content string) string {
130132 // Collapse multiple blank lines into at most two.
131− multipleNewlines := regexp.MustCompile(`\n{3,}`)
132− content = multipleNewlines.ReplaceAllString(content, "\n\n")
133+ content = multipleNewlinesRe.ReplaceAllString(content, "\n\n")
133134
134135 // Remove trailing whitespace from each line.
135136 lines := strings.Split(content, "\n")
internal/agent/tools/mcp/init.go+1−2
@@ -9,7 +9,6 @@ import (
99 "fmt"
1010 "io"
1111 "log/slog"
12− "maps"
1312 "net/http"
1413 "os"
1514 "os/exec"
@@ -98,7 +97,7 @@ func SubscribeEvents(ctx context.Context) <-chan pubsub.Event[Event] {
9897
9998// GetStates returns the current state of all MCP clients
10099func GetStates() map[string]ClientInfo {
101− return maps.Collect(states.Seq2())
100+ return states.Copy()
102101}
103102
104103// GetState returns the state of a specific MCP client
internal/app/lsp_events.go+1−2
@@ -2,7 +2,6 @@ package app
22
33import (
44 "context"
5− "maps"
65 "time"
76
87 "github.com/charmbracelet/crush/internal/csync"
@@ -49,7 +48,7 @@ func SubscribeLSPEvents(ctx context.Context) <-chan pubsub.Event[LSPEvent] {
4948
5049// GetLSPStates returns the current state of all LSP clients
5150func GetLSPStates() map[string]LSPClientInfo {
52− return maps.Collect(lspStates.Seq2())
51+ return lspStates.Copy()
5352}
5453
5554// GetLSPState returns the state of a specific LSP client
internal/csync/maps.go+8−4
@@ -96,12 +96,16 @@ func (m *Map[K, V]) Take(key K) (V, bool) {
9696 return v, ok
9797}
9898
99+// Copy returns a copy of the inner map.
100+func (m *Map[K, V]) Copy() map[K]V {
101+ m.mu.RLock()
102+ defer m.mu.RUnlock()
103+ return maps.Clone(m.inner)
104+}
105+
99106// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
100107func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
101− dst := make(map[K]V)
102− m.mu.RLock()
103− maps.Copy(dst, m.inner)
104− m.mu.RUnlock()
108+ dst := m.Copy()
105109 return func(yield func(K, V) bool) {
106110 for k, v := range dst {
107111 if !yield(k, v) {
internal/csync/versionedmap.go+5
@@ -40,6 +40,11 @@ func (m *VersionedMap[K, V]) Seq2() iter.Seq2[K, V] {
4040 return m.m.Seq2()
4141}
4242
43+// Copy returns a copy of the inner map.
44+func (m *VersionedMap[K, V]) Copy() map[K]V {
45+ return m.m.Copy()
46+}
47+
4348// Len returns the number of items in the map.
4449func (m *VersionedMap[K, V]) Len() int {
4550 return m.m.Len()
internal/db/connect.go+3−24
@@ -7,42 +7,21 @@ import (
77 "log/slog"
88 "path/filepath"
99
10− "github.com/ncruces/go-sqlite3"
11− "github.com/ncruces/go-sqlite3/driver"
12− _ "github.com/ncruces/go-sqlite3/embed"
13−
1410 "github.com/pressly/goose/v3"
1511)
1612
13+// Connect opens a SQLite database connection and runs migrations.
1714func Connect(ctx context.Context, dataDir string) (*sql.DB, error) {
1815 if dataDir == "" {
1916 return nil, fmt.Errorf("data.dir is not set")
2017 }
2118 dbPath := filepath.Join(dataDir, "crush.db")
2219
23− // Set pragmas for better performance
24− pragmas := []string{
25− "PRAGMA foreign_keys = ON;",
26− "PRAGMA journal_mode = WAL;",
27− "PRAGMA page_size = 4096;",
28− "PRAGMA cache_size = -8000;",
29− "PRAGMA synchronous = NORMAL;",
30− "PRAGMA secure_delete = ON;",
31− }
32−
33− db, err := driver.Open(dbPath, func(c *sqlite3.Conn) error {
34− for _, pragma := range pragmas {
35− if err := c.Exec(pragma); err != nil {
36− return fmt.Errorf("failed to set pragma `%s`: %w", pragma, err)
37− }
38− }
39− return nil
40− })
20+ db, err := openDB(dbPath)
4121 if err != nil {
42− return nil, fmt.Errorf("failed to open database: %w", err)
22+ return nil, err
4323 }
4424
45− // Verify connection
4625 if err = db.PingContext(ctx); err != nil {
4726 db.Close()
4827 return nil, fmt.Errorf("failed to connect to database: %w", err)
internal/db/connect_modernc.gonew file+30
@@ -0,0 +1,30 @@
1+//go:build (darwin && (amd64 || arm64)) || (freebsd && (amd64 || arm64)) || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64 || s390x)) || (windows && (386 || amd64 || arm64))
2+
3+package db
4+
5+import (
6+ "database/sql"
7+ "fmt"
8+ "net/url"
9+
10+ _ "modernc.org/sqlite"
11+)
12+
13+func openDB(dbPath string) (*sql.DB, error) {
14+ // Set pragmas for better performance via _pragma query params.
15+ // Format: _pragma=name(value)
16+ params := url.Values{}
17+ params.Add("_pragma", "foreign_keys(on)")
18+ params.Add("_pragma", "journal_mode(WAL)")
19+ params.Add("_pragma", "page_size(4096)")
20+ params.Add("_pragma", "cache_size(-8000)")
21+ params.Add("_pragma", "synchronous(NORMAL)")
22+ params.Add("_pragma", "secure_delete(on)")
23+
24+ dsn := fmt.Sprintf("file:%s?%s", dbPath, params.Encode())
25+ db, err := sql.Open("sqlite", dsn)
26+ if err != nil {
27+ return nil, fmt.Errorf("failed to open database: %w", err)
28+ }
29+ return db, nil
30+}
internal/db/connect_ncruces.gonew file+37
@@ -0,0 +1,37 @@
1+//go:build !((darwin && (amd64 || arm64)) || (freebsd && (amd64 || arm64)) || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64 || s390x)) || (windows && (386 || amd64 || arm64)))
2+
3+package db
4+
5+import (
6+ "database/sql"
7+ "fmt"
8+
9+ "github.com/ncruces/go-sqlite3"
10+ "github.com/ncruces/go-sqlite3/driver"
11+ _ "github.com/ncruces/go-sqlite3/embed"
12+)
13+
14+func openDB(dbPath string) (*sql.DB, error) {
15+ // Set pragmas for better performance.
16+ pragmas := []string{
17+ "PRAGMA foreign_keys = ON;",
18+ "PRAGMA journal_mode = WAL;",
19+ "PRAGMA page_size = 4096;",
20+ "PRAGMA cache_size = -8000;",
21+ "PRAGMA synchronous = NORMAL;",
22+ "PRAGMA secure_delete = ON;",
23+ }
24+
25+ db, err := driver.Open(dbPath, func(c *sqlite3.Conn) error {
26+ for _, pragma := range pragmas {
27+ if err := c.Exec(pragma); err != nil {
28+ return fmt.Errorf("failed to set pragma %q: %w", pragma, err)
29+ }
30+ }
31+ return nil
32+ })
33+ if err != nil {
34+ return nil, fmt.Errorf("failed to open database: %w", err)
35+ }
36+ return db, nil
37+}
internal/lsp/client.go+49−1
@@ -9,6 +9,7 @@ import (
99 "os"
1010 "path/filepath"
1111 "strings"
12+ "sync"
1213 "sync/atomic"
1314 "time"
1415
@@ -21,6 +22,14 @@ import (
2122 "github.com/charmbracelet/x/powernap/pkg/transport"
2223)
2324
25+// DiagnosticCounts holds the count of diagnostics by severity.
26+type DiagnosticCounts struct {
27+ Error int
28+ Warning int
29+ Information int
30+ Hint int
31+}
32+
2433type Client struct {
2534 client *powernap.Client
2635 name string
@@ -37,6 +46,11 @@ type Client struct {
3746 // Diagnostic cache
3847 diagnostics *csync.VersionedMap[protocol.DocumentURI, []protocol.Diagnostic]
3948
49+ // Cached diagnostic counts to avoid map copy on every UI render.
50+ diagCountsCache DiagnosticCounts
51+ diagCountsVersion uint64
52+ diagCountsMu sync.Mutex
53+
4054 // Files are currently opened by the LSP
4155 openFiles *csync.Map[string, *OpenFileInfo]
4256
@@ -350,7 +364,41 @@ func (c *Client) GetFileDiagnostics(uri protocol.DocumentURI) []protocol.Diagnos
350364
351365// GetDiagnostics returns all diagnostics for all files.
352366func (c *Client) GetDiagnostics() map[protocol.DocumentURI][]protocol.Diagnostic {
353− return maps.Collect(c.diagnostics.Seq2())
367+ return c.diagnostics.Copy()
368+}
369+
370+// GetDiagnosticCounts returns cached diagnostic counts by severity.
371+// Uses the VersionedMap version to avoid recomputing on every call.
372+func (c *Client) GetDiagnosticCounts() DiagnosticCounts {
373+ currentVersion := c.diagnostics.Version()
374+
375+ c.diagCountsMu.Lock()
376+ defer c.diagCountsMu.Unlock()
377+
378+ if currentVersion == c.diagCountsVersion {
379+ return c.diagCountsCache
380+ }
381+
382+ // Recompute counts.
383+ counts := DiagnosticCounts{}
384+ for _, diags := range c.diagnostics.Seq2() {
385+ for _, diag := range diags {
386+ switch diag.Severity {
387+ case protocol.SeverityError:
388+ counts.Error++
389+ case protocol.SeverityWarning:
390+ counts.Warning++
391+ case protocol.SeverityInformation:
392+ counts.Information++
393+ case protocol.SeverityHint:
394+ counts.Hint++
395+ }
396+ }
397+ }
398+
399+ c.diagCountsCache = counts
400+ c.diagCountsVersion = currentVersion
401+ return counts
354402}
355403
356404// OpenFileOnDemand opens a file only if it's not already open.
internal/oauth/copilot/client.go+2−1
@@ -12,6 +12,8 @@ import (
1212 "github.com/charmbracelet/crush/internal/log"
1313)
1414
15+var assistantRolePattern = regexp.MustCompile(`"role"\s*:\s*"assistant"`)
16+
1517// NewClient creates a new HTTP client with a custom transport that adds the
1618// X-Initiator header based on message history in the request body.
1719func NewClient(isSubAgent, debug bool) *http.Client {
@@ -58,7 +60,6 @@ func (t *initiatorTransport) RoundTrip(req *http.Request) (*http.Response, error
5860 // Check for assistant messages using regex to handle whitespace
5961 // variations in the JSON while avoiding full unmarshalling overhead.
6062 initiator := userInitiator
61− assistantRolePattern := regexp.MustCompile(`"role"\s*:\s*"assistant"`)
6263 if assistantRolePattern.Match(bodyBytes) || t.isSubAgent {
6364 slog.Debug("Setting X-Initiator header to agent (found assistant messages in history)")
6465 initiator = agentInitiator
internal/tui/components/chat/header/header.go+1−8
@@ -15,7 +15,6 @@ import (
1515 "github.com/charmbracelet/crush/internal/tui/styles"
1616 "github.com/charmbracelet/crush/internal/tui/util"
1717 "github.com/charmbracelet/x/ansi"
18− "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
1918)
2019
2120type Header interface {
@@ -106,13 +105,7 @@ func (h *header) details(availWidth int) string {
106105
107106 errorCount := 0
108107 for l := range h.lspClients.Seq() {
109− for _, diagnostics := range l.GetDiagnostics() {
110− for _, diagnostic := range diagnostics {
111− if diagnostic.Severity == protocol.SeverityError {
112− errorCount++
113− }
114− }
115− }
108+ errorCount += l.GetDiagnosticCounts().Error
116109 }
117110
118111 if errorCount > 0 {
internal/tui/components/lsp/lsp.go+14−28
@@ -11,7 +11,6 @@ import (
1111 "github.com/charmbracelet/crush/internal/lsp"
1212 "github.com/charmbracelet/crush/internal/tui/components/core"
1313 "github.com/charmbracelet/crush/internal/tui/styles"
14− "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
1514)
1615
1716// RenderOptions contains options for rendering LSP lists.
@@ -61,36 +60,23 @@ func RenderLSPList(lspClients *csync.Map[string, *lsp.Client], opts RenderOption
6160 // Calculate diagnostic counts if we have LSP clients
6261 var extraContent string
6362 if lspClients != nil {
64− lspErrs := map[protocol.DiagnosticSeverity]int{
65− protocol.SeverityError: 0,
66− protocol.SeverityWarning: 0,
67− protocol.SeverityHint: 0,
68− protocol.SeverityInformation: 0,
69− }
7063 if client, ok := lspClients.Get(l.Name); ok {
71− for _, diagnostics := range client.GetDiagnostics() {
72− for _, diagnostic := range diagnostics {
73− if severity, ok := lspErrs[diagnostic.Severity]; ok {
74− lspErrs[diagnostic.Severity] = severity + 1
75− }
76− }
64+ counts := client.GetDiagnosticCounts()
65+ errs := []string{}
66+ if counts.Error > 0 {
67+ errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, counts.Error)))
7768 }
69+ if counts.Warning > 0 {
70+ errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, counts.Warning)))
71+ }
72+ if counts.Hint > 0 {
73+ errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, counts.Hint)))
74+ }
75+ if counts.Information > 0 {
76+ errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, counts.Information)))
77+ }
78+ extraContent = strings.Join(errs, " ")
7879 }
79−
80− errs := []string{}
81− if lspErrs[protocol.SeverityError] > 0 {
82− errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
83− }
84− if lspErrs[protocol.SeverityWarning] > 0 {
85− errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
86− }
87− if lspErrs[protocol.SeverityHint] > 0 {
88− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
89− }
90− if lspErrs[protocol.SeverityInformation] > 0 {
91− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
92− }
93− extraContent = strings.Join(errs, " ")
9480 }
9581
9682 lspList = append(lspList,