summaryrefslogtreecommitdiff
path: root/internal/lsp/completion_state.go
blob: 5c2716fefbeaeeb51e8d4bf3378372fbe136ee8b (plain)
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
package lsp

import (
	"context"
	"sync"
	"time"
)

// completionState manages the LRU completion cache, pending completions, and
// throttle timing. Its stateMu is independent of Server.mu — the two locks
// are never held simultaneously, so there is no ordering constraint.
type completionState struct {
	stateMu             sync.RWMutex
	compCache           map[string]string
	compCacheOrder      []string
	pendingCompletions  map[string][]CompletionItem
	lastLLMCall         time.Time
	completionsDisabled bool
}

func newCompletionState() completionState {
	return completionState{
		compCache:          make(map[string]string),
		pendingCompletions: make(map[string][]CompletionItem),
	}
}

func (s *completionState) storePendingCompletion(key string, items []CompletionItem) {
	if len(items) == 0 {
		return
	}
	cpy := make([]CompletionItem, len(items))
	copy(cpy, items)
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	if s.pendingCompletions == nil {
		s.pendingCompletions = make(map[string][]CompletionItem)
	}
	s.pendingCompletions[key] = cpy
}

func (s *completionState) setCompletionsDisabled(disabled bool) bool {
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	prev := s.completionsDisabled
	s.completionsDisabled = disabled
	return prev
}

func (s *completionState) completionDisabled() bool {
	s.stateMu.RLock()
	defer s.stateMu.RUnlock()
	return s.completionsDisabled
}

func (s *completionState) takePendingCompletion(key string) []CompletionItem {
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	if len(s.pendingCompletions) == 0 {
		return nil
	}
	items, ok := s.pendingCompletions[key]
	if !ok {
		return nil
	}
	delete(s.pendingCompletions, key)
	cpy := make([]CompletionItem, len(items))
	copy(cpy, items)
	return cpy
}

// cacheGet returns the cached value for key. Uses a single write lock to
// avoid a TOCTOU race between the lookup and the LRU touch — the key could
// be evicted between an RUnlock and a subsequent Lock promotion.
func (s *completionState) cacheGet(key string) (string, bool) {
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	v, ok := s.compCache[key]
	if !ok {
		return "", false
	}
	s.touchLocked(key)
	return v, true
}

func (s *completionState) cachePut(key, value string) {
	s.stateMu.Lock()
	defer s.stateMu.Unlock()
	if s.compCache == nil {
		s.compCache = make(map[string]string)
	}
	if _, exists := s.compCache[key]; !exists {
		s.compCacheOrder = append(s.compCacheOrder, key)
		s.compCache[key] = value
		if len(s.compCacheOrder) > 10 {
			old := s.compCacheOrder[0]
			s.compCacheOrder = s.compCacheOrder[1:]
			delete(s.compCache, old)
		}
		return
	}
	s.compCache[key] = value
	s.touchLocked(key)
}

// touchLocked moves key to the end of the LRU order list.
// Uses delete-and-append: remove the existing entry in-place, then append.
func (s *completionState) touchLocked(key string) {
	for i, k := range s.compCacheOrder {
		if k == key {
			s.compCacheOrder = append(s.compCacheOrder[:i], s.compCacheOrder[i+1:]...)
			break
		}
	}
	s.compCacheOrder = append(s.compCacheOrder, key)
}

func (s *completionState) waitForThrottle(ctx context.Context, interval time.Duration) bool {
	if interval <= 0 {
		return true
	}
	var wait time.Duration
	for {
		s.stateMu.Lock()
		next := s.lastLLMCall.Add(interval)
		now := time.Now()
		if now.Before(next) {
			wait = next.Sub(now)
			s.stateMu.Unlock()
			timer := time.NewTimer(wait)
			select {
			case <-ctx.Done():
				timer.Stop()
				return false
			case <-timer.C:
				continue
			}
		}
		s.lastLLMCall = now
		s.stateMu.Unlock()
		return true
	}
}

func (s *Server) storePendingCompletion(key string, items []CompletionItem) {
	s.completionState.storePendingCompletion(key, items)
}

func (s *Server) setCompletionsDisabled(disabled bool) bool {
	return s.completionState.setCompletionsDisabled(disabled)
}

func (s *Server) completionDisabled() bool {
	return s.completionState.completionDisabled()
}

func (s *Server) takePendingCompletion(key string) []CompletionItem {
	return s.completionState.takePendingCompletion(key)
}

func (s *Server) completionCacheGet(key string) (string, bool) {
	return s.completionState.cacheGet(key)
}

func (s *Server) completionCachePut(key, value string) {
	s.completionState.cachePut(key, value)
}

func (s *Server) waitForThrottle(ctx context.Context) bool {
	return s.completionState.waitForThrottle(ctx, s.completionThrottle())
}