xonecas.io
62159
Sy

symb / commit 5231fb6

feat: fix issues with UI lag and blocking call. Full ELM restored.

Sean2026-02-12 5231fb6902757626c30bbe04e37d41440e9d0764 parent 3cf884b

3 files changed

config.toml+1−1
@@ -1,7 +1,7 @@
11 # Symb Configuration File
22
33 # Default provider (optional - if not set, first provider in map is used)
4-default_provider = "ollama-qwen"
4+default_provider = "ollama-qwen-small"
55
66 # Ollama providers (local)
77 [providers.ollama-qwen]
internal/tui/textarea/textarea.go+25−20
@@ -17,6 +17,7 @@ import (
1717 "fmt"
1818 "strconv"
1919 "strings"
20+ "sync"
2021 "unicode"
2122
2223 "github.com/alecthomas/chroma/v2"
@@ -49,6 +50,12 @@ const (
4950 maxLines = 10000
5051 )
5152
53+// Global syntax highlighting cache (external to Model for ELM compliance)
54+var (
55+ highlightCache = make(map[string]string)
56+ highlightCacheMu sync.RWMutex
57+)
58+
5259 // Internal messages for clipboard operations.
5360 type (
5461 pasteMsg string
@@ -300,9 +307,6 @@ type Model struct {
300307 // Language for syntax highlighting (e.g., "go", "python", "javascript")
301308 // If empty, no syntax highlighting is applied.
302309 Language string
303-
304- // highlightCache caches highlighted lines to avoid re-highlighting on every frame
305- highlightCache map[string]string
306310 }
307311
308312 // New creates a new model with default settings.
@@ -326,7 +330,6 @@ func New() Model {
326330 ShowLineNumbers: true,
327331 Cursor: cur,
328332 KeyMap: DefaultKeyMap,
329- highlightCache: make(map[string]string),
330333
331334 value: make([][]rune, minHeight, maxLines),
332335 focus: false,
@@ -387,9 +390,6 @@ func (m *Model) InsertRune(r rune) {
387390
388391 // insertRunesFromUserInput inserts runes at the current cursor position.
389392 func (m *Model) insertRunesFromUserInput(runes []rune) {
390- // Clear highlight cache since text is being modified
391- m.highlightCache = make(map[string]string)
392-
393393 // Clean up any special characters in the input provided by the
394394 // clipboard. This avoids bugs due to e.g. tab characters and
395395 // whatnot.
@@ -629,8 +629,6 @@ func (m *Model) Reset() {
629629 m.row = 0
630630 m.viewport.GotoTop()
631631 m.SetCursor(0)
632- // Clear highlight cache on reset
633- m.highlightCache = make(map[string]string)
634632 }
635633
636634 // san initializes or retrieves the rune sanitizer.
@@ -646,7 +644,6 @@ func (m *Model) san() runeutil.Sanitizer {
646644 // deleteBeforeCursor deletes all text before the cursor. Returns whether or
647645 // not the cursor blink should be reset.
648646 func (m *Model) deleteBeforeCursor() {
649- m.highlightCache = make(map[string]string)
650647 m.value[m.row] = m.value[m.row][m.col:]
651648 m.SetCursor(0)
652649 }
@@ -655,7 +652,6 @@ func (m *Model) deleteBeforeCursor() {
655652 // the cursor blink should be reset. If input is masked delete everything after
656653 // the cursor so as not to reveal word breaks in the masked input.
657654 func (m *Model) deleteAfterCursor() {
658- m.highlightCache = make(map[string]string)
659655 m.value[m.row] = m.value[m.row][:m.col]
660656 m.SetCursor(len(m.value[m.row]))
661657 }
@@ -683,7 +679,6 @@ func (m *Model) deleteWordLeft() {
683679 if m.col == 0 || len(m.value[m.row]) == 0 {
684680 return
685681 }
686- m.highlightCache = make(map[string]string)
687682
688683 // Linter note: it's critical that we acquire the initial cursor position
689684 // here prior to altering it via SetCursor() below. As such, moving this
@@ -723,7 +718,6 @@ func (m *Model) deleteWordRight() {
723718 if m.col >= len(m.value[m.row]) || len(m.value[m.row]) == 0 {
724719 return
725720 }
726- m.highlightCache = make(map[string]string)
727721
728722 oldCol := m.col
729723
@@ -1037,7 +1031,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
10371031 if m.ReadOnly {
10381032 break
10391033 }
1040- m.highlightCache = make(map[string]string)
10411034 m.col = clamp(m.col, 0, len(m.value[m.row]))
10421035 if m.col <= 0 {
10431036 m.mergeLineAbove(m.row)
@@ -1053,7 +1046,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
10531046 if m.ReadOnly {
10541047 break
10551048 }
1056- m.highlightCache = make(map[string]string)
10571049 if len(m.value[m.row]) > 0 && m.col < len(m.value[m.row]) {
10581050 m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][m.col+1:]...)
10591051 }
@@ -1577,16 +1569,20 @@ func clamp(v, low, high int) int {
15771569
15781570 // highlightLine applies Chroma syntax highlighting to a line of text.
15791571 // Returns the original text if highlighting fails or language is empty.
1580-func (m *Model) highlightLine(text string) string {
1572+// Uses a global cache for performance (external to Model for ELM compliance).
1573+func (m Model) highlightLine(text string) string {
15811574 if m.Language == "" || text == "" {
15821575 return text
15831576 }
15841577
1585- // Check cache first
1578+ // Check cache first (with read lock)
15861579 cacheKey := m.Language + ":" + text
1587- if highlighted, ok := m.highlightCache[cacheKey]; ok {
1580+ highlightCacheMu.RLock()
1581+ if highlighted, ok := highlightCache[cacheKey]; ok {
1582+ highlightCacheMu.RUnlock()
15881583 return highlighted
15891584 }
1585+ highlightCacheMu.RUnlock()
15901586
15911587 // Get lexer for language
15921588 lexer := lexers.Get(m.Language)
@@ -1623,8 +1619,17 @@ func (m *Model) highlightLine(text string) string {
16231619 // Remove trailing newline if added by formatter
16241620 result := strings.TrimSuffix(buf.String(), "\n")
16251621
1626- // Cache the result
1627- m.highlightCache[cacheKey] = result
1622+ // Cache the result (with write lock)
1623+ highlightCacheMu.Lock()
1624+ // Limit cache size to prevent unbounded growth
1625+ if len(highlightCache) > 1000 {
1626+ // Clear map instead of replacing to avoid orphaning readers
1627+ for k := range highlightCache {
1628+ delete(highlightCache, k)
1629+ }
1630+ }
1631+ highlightCache[cacheKey] = result
1632+ highlightCacheMu.Unlock()
16281633
16291634 return result
16301635 }
internal/tui/tui.go+51−22
@@ -125,8 +125,8 @@ func (m Model) Init() tea.Cmd {
125125 }
126126
127127 // wrapText wraps text to the conversation pane width, applying style after wrapping.
128-// Returns lines that fit within the pane width.
129-func (m *Model) wrapText(text string, style lipgloss.Style) []string {
128+// Returns lines that fit within the pane width and the updated model with cache.
129+func (m Model) wrapText(text string, style lipgloss.Style) ([]string, Model) {
130130 if m.rightPaneWidth <= 0 {
131131 // Fallback if width not set yet
132132 lines := strings.Split(text, "\n")
@@ -134,7 +134,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
134134 for i, line := range lines {
135135 result[i] = style.Render(line)
136136 }
137- return result
137+ return result, m
138138 }
139139
140140 // Create cache key from text and width
@@ -142,7 +142,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
142142
143143 // Check cache
144144 if cached, ok := m.wrapCache[cacheKey]; ok {
145- return cached
145+ return cached, m
146146 }
147147
148148 // Word wrap first, then apply style to each line
@@ -162,7 +162,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
162162 }
163163 m.wrapCache[cacheKey] = result
164164
165- return result
165+ return result, m
166166 }
167167
168168 // llmUserMsg adds user message to conversation (ELM Msg)
@@ -205,6 +205,20 @@ type UpdateToolsMsg struct {
205205 Tools []mcp.Tool
206206 }
207207
208+// clipboardCopyMsg is sent to copy text to clipboard (ELM Msg)
209+type clipboardCopyMsg struct {
210+ text string
211+}
212+
213+// copyToClipboardCmd copies text to clipboard asynchronously (ELM Cmd)
214+func copyToClipboardCmd(text string) tea.Cmd {
215+ return func() tea.Msg {
216+ // Fire and forget - clipboard operations are best effort
217+ _ = clipboard.WriteAll(text)
218+ return nil
219+ }
220+}
221+
208222 // sendToLLM sends user message to LLM (ELM Cmd)
209223 func (m Model) sendToLLM(userInput string) tea.Cmd {
210224 return func() tea.Msg {
@@ -222,30 +236,42 @@ func (m Model) waitForLLMUpdate() tea.Cmd {
222236
223237 // processLLM runs LLM turn in background (ELM Cmd)
224238 func (m Model) processLLM() tea.Cmd {
239+ // Copy values to avoid capturing model state
240+ prov := m.provider
241+ proxy := m.mcpProxy
242+ // Copy tools slice
243+ tools := make([]mcp.Tool, len(m.mcpTools))
244+ copy(tools, m.mcpTools)
245+ // Copy history slice
246+ history := make([]provider.Message, len(m.history))
247+ copy(history, m.history)
248+ updateChan := m.updateChan
249+ ctx := m.ctx
250+
225251 return func() tea.Msg {
226252 go func() {
227253 startTime := time.Now()
228254
229255 opts := llm.ProcessTurnOptions{
230- Provider: m.provider,
231- Proxy: m.mcpProxy,
232- Tools: m.mcpTools,
233- History: m.history,
256+ Provider: prov,
257+ Proxy: proxy,
258+ Tools: tools,
259+ History: history,
234260 MaxToolRounds: 20,
235261 OnMessage: func(msg provider.Message) {
236262 // Send message to update history
237- m.updateChan <- llmHistoryMsg{msg: msg}
263+ updateChan <- llmHistoryMsg{msg: msg}
238264
239265 if msg.Role == "assistant" {
240266 // Send complete assistant message as single block
241- m.updateChan <- llmAssistantMsg{
267+ updateChan <- llmAssistantMsg{
242268 reasoning: msg.Reasoning,
243269 content: msg.Content,
244270 toolCalls: msg.ToolCalls,
245271 }
246272 } else if msg.Role == "tool" {
247273 // Send full tool result (wrapping will be handled by wrapText)
248- m.updateChan <- llmToolResultMsg{
274+ updateChan <- llmToolResultMsg{
249275 toolCallID: msg.ToolCallID,
250276 content: msg.Content,
251277 }
@@ -253,14 +279,14 @@ func (m Model) processLLM() tea.Cmd {
253279 },
254280 }
255281
256- err := llm.ProcessTurn(m.ctx, opts)
282+ err := llm.ProcessTurn(ctx, opts)
257283 if err != nil {
258- m.updateChan <- llmErrorMsg{err: err}
284+ updateChan <- llmErrorMsg{err: err}
259285 return
260286 }
261287
262288 // Send done message
263- m.updateChan <- llmDoneMsg{
289+ updateChan <- llmDoneMsg{
264290 duration: time.Since(startTime),
265291 timestamp: startTime.Format("15:04"),
266292 }
@@ -387,8 +413,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
387413 }
388414 }
389415
390- // Copy to clipboard (best effort, ignore errors)
391- _ = clipboard.WriteAll(selectedText.String())
416+ // Copy to clipboard via command
417+ cmds = append(cmds, copyToClipboardCmd(selectedText.String()))
392418 }
393419 // Clear selection
394420 m.conversationSelectStart = 0
@@ -450,7 +476,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
450476 })
451477 // Format and display user message first
452478 grayStyle := lipgloss.NewStyle().Foreground(lipgloss.NoColor{})
453- wrappedLines := m.wrapText(msg.content, grayStyle)
479+ wrappedLines, m := m.wrapText(msg.content, grayStyle)
454480 m.conversation = append(m.conversation, wrappedLines...)
455481 m.conversation = append(m.conversation, "")
456482 // Add timestamp separator after message
@@ -482,21 +508,24 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
482508
483509 // Display reasoning if present
484510 if msg.reasoning != "" {
485- wrappedLines := m.wrapText(msg.reasoning, grayStyle)
511+ var wrappedLines []string
512+ wrappedLines, m = m.wrapText(msg.reasoning, grayStyle)
486513 m.conversation = append(m.conversation, wrappedLines...)
487514 m.conversation = append(m.conversation, "")
488515 }
489516
490517 // Display content
491518 if msg.content != "" {
492- wrappedLines := m.wrapText(msg.content, plainStyle)
519+ var wrappedLines []string
520+ wrappedLines, m = m.wrapText(msg.content, plainStyle)
493521 m.conversation = append(m.conversation, wrappedLines...)
494522 m.conversation = append(m.conversation, "")
495523 }
496524
497525 // Display tool calls
498526 for _, tc := range msg.toolCalls {
499- wrappedLines := m.wrapText("→ "+tc.Name+"(...)", plainStyle)
527+ var wrappedLines []string
528+ wrappedLines, m = m.wrapText("→ "+tc.Name+"(...)", plainStyle)
500529 m.conversation = append(m.conversation, wrappedLines...)
501530 }
502531
@@ -504,7 +533,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
504533
505534 case llmToolResultMsg:
506535 plainStyle := lipgloss.NewStyle()
507- wrappedLines := m.wrapText("← "+msg.content, plainStyle)
536+ wrappedLines, m := m.wrapText("← "+msg.content, plainStyle)
508537 m.conversation = append(m.conversation, wrappedLines...)
509538 return m, m.waitForLLMUpdate() // Continue listening