-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.go
More file actions
400 lines (334 loc) · 11.1 KB
/
Copy pathserver.go
File metadata and controls
400 lines (334 loc) · 11.1 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package mcp
import (
"context"
"fmt"
"log/slog"
"net"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/radimsem/remindb/internal/loghelper"
"github.com/radimsem/remindb/internal/redaction"
"github.com/radimsem/remindb/pkg/config"
"github.com/radimsem/remindb/pkg/logbuf"
"github.com/radimsem/remindb/pkg/mcp/ledger"
"github.com/radimsem/remindb/pkg/mcp/notify"
"github.com/radimsem/remindb/pkg/mcp/rescanstat"
"github.com/radimsem/remindb/pkg/mcp/resources"
"github.com/radimsem/remindb/pkg/mcp/session"
"github.com/radimsem/remindb/pkg/mcp/sessionlog"
"github.com/radimsem/remindb/pkg/mcp/tools"
"github.com/radimsem/remindb/pkg/query"
"github.com/radimsem/remindb/pkg/relations"
"github.com/radimsem/remindb/pkg/store"
"github.com/radimsem/remindb/pkg/temperature"
"github.com/radimsem/remindb/pkg/version"
)
const (
TransportStdio = "stdio"
TransportHttp = "http"
DefaultListenAddr = "127.0.0.1:7474"
DefaultSessionFlushInterval = 30 * time.Second
)
type Server struct {
mcp *mcp.Server
logger *slog.Logger
notifyThreshold float64
transport string
listen string
listener net.Listener
notifier *notify.Publisher
sessions *session.Registry
sessionFlush time.Duration
}
type Option func(*options)
type options struct {
sourceDir string
logger *slog.Logger
transport string
listen string
listener net.Listener
workspaceConfig config.Config
redactor *redaction.Redactor
logBuffer *logbuf.Buffer
rescanStatus *rescanstat.Status
}
func WithSourceDir(dir string) Option {
return func(o *options) { o.sourceDir = dir }
}
func WithLogger(l *slog.Logger) Option {
return func(o *options) { o.logger = l }
}
func WithTransport(t string) Option {
return func(o *options) { o.transport = t }
}
func WithListen(addr string) Option {
return func(o *options) { o.listen = addr }
}
func WithListener(l net.Listener) Option {
return func(o *options) { o.listener = l }
}
func WithWorkspaceConfig(c config.Config) Option {
return func(o *options) { o.workspaceConfig = c }
}
func WithRedactor(r *redaction.Redactor) Option {
return func(o *options) { o.redactor = r }
}
func WithLogBuffer(b *logbuf.Buffer) Option {
return func(o *options) { o.logBuffer = b }
}
func WithRescanStatus(s *rescanstat.Status) Option {
return func(o *options) { o.rescanStatus = s }
}
func NewServer(st *store.Store, tracker *temperature.Tracker, cfg temperature.Config, opts ...Option) (*Server, error) {
var o options
for _, opt := range opts {
opt(&o)
}
logger := loghelper.OrDiscard(o.logger)
transport := o.transport
if transport == "" {
transport = TransportStdio
}
listen := o.listen
if listen == "" {
listen = DefaultListenAddr
}
red := o.redactor
if red == nil {
def, err := redaction.New(redaction.DefaultConfig())
if err != nil {
return nil, fmt.Errorf("failed to build: redactor: %w", err)
}
red = def
}
pub, err := notify.NewPublisher(o.workspaceConfig.Server.Resources, logger)
if err != nil {
return nil, fmt.Errorf("failed to build: resource notifier: %w", err)
}
mcpSrv := mcp.NewServer(&mcp.Implementation{
Name: "remindb",
Version: version.Get(),
}, &mcp.ServerOptions{
SubscribeHandler: pub.HandleSubscribe,
UnsubscribeHandler: pub.HandleUnsubscribe,
})
pub.Attach(mcpSrv)
var sessLedger *ledger.Ledger
if o.sourceDir != "" {
sessLedger, err = ledger.New(o.sourceDir, logger)
if err != nil {
return nil, fmt.Errorf("failed to build: session ledger: %w", err)
}
}
flush := DefaultSessionFlushInterval
if fi := o.workspaceConfig.Server.Sessions.FlushInterval; fi != nil {
flush = time.Duration(*fi)
}
sessions := session.NewRegistry(mcpSrv,
session.WithTransport(transport),
session.WithListen(listen),
session.WithLedger(sessLedger),
session.WithLogger(logger),
)
mcpSrv.AddReceivingMiddleware(sessions.Middleware)
s := &Server{
mcp: mcpSrv,
logger: logger,
notifyThreshold: cfg.NotifyThreshold,
transport: transport,
listen: listen,
listener: o.listener,
notifier: pub,
sessions: sessions,
sessionFlush: flush,
}
deps := &tools.Deps{
Store: st,
Engine: query.NewEngine(st),
Resolver: relations.New(st),
Tracker: tracker,
Redactor: red,
Logger: logger,
SourceDir: o.sourceDir,
WorkspaceConfig: o.workspaceConfig,
HotThreshold: cfg.HotThreshold,
ColdThreshold: cfg.ColdThreshold,
SummarizeRebound: cfg.SummarizeRebound,
Notifier: pub,
}
sessionLogDir := ""
if o.sourceDir != "" {
sessionLogDir = sessionlog.Dir(o.sourceDir)
}
registerTools(s.mcp, deps)
resources.Register(s.mcp, &resources.Deps{Store: st, HotThreshold: cfg.HotThreshold, ColdThreshold: cfg.ColdThreshold, LogBuffer: o.logBuffer, Sessions: sessions, Ledger: sessLedger, RescanStatus: o.rescanStatus, SessionLogDir: sessionLogDir})
return s, nil
}
// RunSessionLedger flushes the session ledger on its interval until ctx ends.
func (s *Server) RunSessionLedger(ctx context.Context) {
s.sessions.Run(ctx, s.sessionFlush)
}
// FlushSessions forces a ledger flush — used at shutdown and in tests.
func (s *Server) FlushSessions() {
s.sessions.Flush(time.Now().Unix())
}
// NotifyTemperatureTick signals that a temperature tick mutated heat values.
func (s *Server) NotifyTemperatureTick() {
s.notifier.Touch(resources.TemperatureURI)
}
// NotifyRescan signals that a source rescan reshaped the compiled set.
func (s *Server) NotifyRescan() {
s.notifier.Touch(resources.FilesURI)
s.notifier.Touch(resources.TreeURI)
s.notifier.Touch(resources.SnapshotsURI)
s.notifier.Touch(resources.RescanURI)
}
// NotifyLogRecord signals a new server log record (heavily coalesced).
func (s *Server) NotifyLogRecord() {
s.notifier.Touch(resources.LogsURI)
}
func (s *Server) Run(ctx context.Context) error {
defer s.notifier.Close()
switch s.transport {
case TransportStdio:
return s.mcp.Run(ctx, &mcp.StdioTransport{})
case TransportHttp:
return s.runHttp(ctx)
default:
return fmt.Errorf("unsupported transport %q", s.transport)
}
}
func (s *Server) Connect(ctx context.Context, t mcp.Transport) (*mcp.ServerSession, error) {
return s.mcp.Connect(ctx, t, nil)
}
// Send a cold-node warning and return the IDs that reached at least one session.
func (s *Server) NotifyColdNodes(ctx context.Context, cold []*store.Node) []string {
toNotify := make([]*store.Node, 0, len(cold))
for _, n := range cold {
if n.Temperature < s.notifyThreshold {
toNotify = append(toNotify, n)
}
}
if len(toNotify) == 0 {
return nil
}
if sent := s.sendColdLogging(ctx, toNotify); sent == 0 {
return nil
}
ids := make([]string, len(toNotify))
for i, n := range toNotify {
ids[i] = n.ID
}
return ids
}
type coldNodeEntry struct {
ID string `json:"id"`
Label string `json:"label"`
SourceFile string `json:"file"`
Temperature float64 `json:"temperature"`
}
type coldNodePayload struct {
Message string `json:"message"`
SuggestedAction string `json:"suggested_action"`
Nodes []coldNodeEntry `json:"nodes"`
}
func (s *Server) sendColdLogging(ctx context.Context, nodes []*store.Node) int {
entries := make([]coldNodeEntry, len(nodes))
for i, n := range nodes {
entries[i] = coldNodeEntry{
ID: n.ID,
Label: n.Label,
SourceFile: n.SourceFile,
Temperature: n.Temperature,
}
}
params := &mcp.LoggingMessageParams{
Level: "warning",
Logger: "remindb.temperature",
Data: coldNodePayload{
Message: "Cold nodes detected; consider summarizing via MemorySummarize",
SuggestedAction: "MemorySummarize",
Nodes: entries,
},
}
sent := 0
for ss := range s.mcp.Sessions() {
if err := ss.Log(ctx, params); err != nil {
s.logger.Warn("failed to send: cold-node notification", "err", err)
continue
}
sent++
}
s.logger.Debug("cold-node notification dispatched", "nodes", len(nodes), "sessions", sent)
return sent
}
func registerTools(srv *mcp.Server, d *tools.Deps) {
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryFetch",
Description: "Retrieve context around an anchor node within a token budget",
}, d.HandleFetch)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryFetchBatch",
Description: "Retrieve content for a list of node IDs in one call (shared token budget; missing IDs and over-budget IDs surfaced inline)",
}, d.HandleFetchBatch)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemorySearch",
Description: "Full-text search for nodes within a token budget",
}, d.HandleSearch)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryWrite",
Description: "Write or update content at an anchor node, creating a snapshot",
}, d.HandleWrite)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryCompile",
Description: "Compile source files or a directory into the memory database",
}, d.HandleCompile)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryDelta",
Description: "Return changes since a given snapshot",
}, d.HandleDelta)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryDiff",
Description: "Return git-diff-style changes between two snapshots (from exclusive, to inclusive)",
}, d.HandleDiff)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemorySummarize",
Description: "Replace a node's content with a provided summary",
}, d.HandleSummarize)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryHistory",
Description: "Browse version history for a specific node",
}, d.HandleHistory)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryTree",
Description: "Return the node tree structure with labels",
}, d.HandleTree)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryRelated",
Description: "Traverse the relations graph from an anchor node",
}, d.HandleRelated)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryRelate",
Description: "Create a manual edge from one node to another (does not snapshot)",
}, d.HandleRelate)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryForget",
Description: "Remove a node by ID. Modes: 'strict' (default) refuses parents, 'cascade' also removes descendants, 'reparent' promotes children to the target's parent",
}, d.HandleForget)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryRollback",
Description: "Restore the node graph to a target snapshot. drop_after=false keeps intervening snapshots reachable as branched history; drop_after=true hard-deletes them. Temperature, pinned state, and relations are not restored",
}, d.HandleRollback)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryPin",
Description: "Protect a node from temperature decay and cold-set selection (does not snapshot)",
}, d.HandlePin)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryUnpin",
Description: "Release a previously pinned node back into the temperature lifecycle (does not snapshot)",
}, d.HandleUnpin)
mcp.AddTool(srv, &mcp.Tool{
Name: "MemoryStats",
Description: "Report database health and shape: node counts (with per-type breakdown), token totals, snapshot summary, temperature spread, relation counts (with per-origin breakdown), pinned/cold/hot tallies",
}, d.HandleStats)
}