mirror of
https://github.com/lukaszraczylo/claude-mnemonic.git
synced 2026-06-05 23:03:55 +00:00
a81482d06a
MCP server (5 fixes):
- Move semaphore acquisition inside goroutine so main loop stays
responsive when all slots are taken
- Add 10s write timeout to sendResponse to prevent pipe deadlock
when Claude Code pauses reading stdout
- Send fallback JSON-RPC error when json.Marshal fails instead of
silently swallowing the error and leaving caller waiting forever
- Silence unknown notification methods (req.ID == nil) instead of
sending unsolicited error responses that may desync the host
- Return MCP isError content for tool failures instead of top-level
JSON-RPC error, matching the MCP specification
Vector/embedding (3 fixes):
- Move EmbedBatchWithContext call before writeMu.Lock in AddDocuments
so ONNX inference runs outside the write lock
- Replace singleflight.Do with DoChan + ctx select in both
getOrComputeEmbedding and UnifiedSearch so callers can bail out
independently when their context expires
- Add activeQueries atomic counter; skip cache warming when user
queries are in-flight; reduce warming timeout from 5s to 2s
Hooks (4 fixes):
- Cap EnsureWorkerRunning to 15s hard deadline with context; reduce
StartupTimeout from 30s to 10s; reduce port-in-use retries
- Fix nil dereference panic in user-prompt hook when initResult is
nil (non-JSON worker response); use comma-ok assertions
- Use package-level hookClient/healthClient with DisableKeepAlives
to prevent FD leaks in short-lived hook processes
- Set SysProcAttr{Setpgid: true} to detach worker from hook process
group, preventing kill-cascade from Claude Code
Worker/DB (3 fixes):
- Replace os.Exit(0) in MCP config watcher with context cancellation
for clean protocol shutdown
- Add 60s context.WithTimeout around ProcessObservation calls in
processAllSessions to prevent hung CLI subprocesses from blocking
the queue processor forever
- Set explicit PRAGMA wal_autocheckpoint=1000 and add PASSIVE WAL
checkpoint to Optimize() to prevent checkpoint stalls
Adds 20+ regression tests across all fix areas.
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
// Package main provides the MCP server entry point for claude-mnemonic.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/claude-mnemonic/internal/config"
|
|
"github.com/lukaszraczylo/claude-mnemonic/internal/mcp"
|
|
"github.com/lukaszraczylo/claude-mnemonic/internal/watcher"
|
|
"github.com/lukaszraczylo/oss-telemetry"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// Version is set at build time via ldflags.
|
|
var Version = "dev"
|
|
|
|
func main() {
|
|
// Parse flags
|
|
project := flag.String("project", "", "Project name (required)")
|
|
debug := flag.Bool("debug", false, "Enable debug logging")
|
|
flag.Parse()
|
|
|
|
// Setup logging - MCP uses stdout for communication, so log to stderr
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
if *debug {
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
}
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true})
|
|
|
|
if *project == "" {
|
|
log.Fatal().Msg("--project is required")
|
|
}
|
|
|
|
// Get worker port from config
|
|
port := config.GetWorkerPort()
|
|
workerURL := fmt.Sprintf("http://localhost:%d", port)
|
|
|
|
// Create HTTP client for worker
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// Handle signals
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
go func() {
|
|
<-sigCh
|
|
log.Info().Msg("Shutting down MCP server")
|
|
cancel()
|
|
}()
|
|
|
|
// Start file watchers for config changes
|
|
startWatchers(cancel)
|
|
|
|
telemetry.Send("claude-mnemonic", Version)
|
|
|
|
// Create and run MCP server
|
|
server := mcp.NewServer(client, workerURL, *project, Version)
|
|
log.Info().Str("project", *project).Str("version", Version).Str("worker", workerURL).Msg("Starting MCP server")
|
|
|
|
if err := server.Run(ctx); err != nil {
|
|
if err == context.Canceled {
|
|
log.Info().Msg("MCP server shut down (config change or signal)")
|
|
return
|
|
}
|
|
log.Fatal().Err(err).Msg("MCP server error")
|
|
}
|
|
}
|
|
|
|
// startWatchers initializes file watchers for config.
|
|
func startWatchers(cancel context.CancelFunc) {
|
|
// Watch config file for changes (triggers graceful shutdown via context cancellation)
|
|
configPath := config.SettingsPath()
|
|
configWatcher, err := watcher.New(configPath, func() {
|
|
log.Warn().Str("path", configPath).Msg("Config file changed, shutting down gracefully...")
|
|
cancel() // Triggers ctx.Done() in server.Run(), which drains in-flight requests
|
|
})
|
|
if err != nil {
|
|
log.Warn().Err(err).Msg("Failed to create config watcher")
|
|
} else {
|
|
if err := configWatcher.Start(); err != nil {
|
|
log.Warn().Err(err).Msg("Failed to start config watcher")
|
|
} else {
|
|
log.Info().Str("path", configPath).Msg("Config file watcher started")
|
|
}
|
|
}
|
|
}
|