---
title: Daemon
description: The per-KB service for MCP requests, sync, file watching, incremental indexing, events, and embeddings.
---

# Daemon

The GitKB daemon provides a shared service for one knowledge base. It handles MCP requests, keeps the knowledge base in sync with its default remote, watches document workspaces and source files, schedules indexing, and manages embeddings and event subscriptions.

CLI commands that write to the knowledge base go through the daemon and start one if none is running; a running daemon is always used. To write without starting one, pass `--no-daemon` (or set `GITKB_NO_DAEMON=1` or `daemon.auto_start = false`). Read-only code queries such as `git-kb code callers` read the local index directly.

## Architecture

On macOS and Linux, clients normally connect through `.kb/.cache/gitkb.sock`. Deeply nested KB paths use a hashed socket path under `/tmp` when the normal path exceeds the platform limit. Windows uses a named pipe derived from the KB root.

```mermaid
flowchart TD
    Files[Source file changes] --> Watcher[Source watcher]
    Watcher -->|Debounced file events| Events[Event bus]
    Events --> Pipeline[Incremental index pipeline]
    Pipeline --> Index[Persisted code index]
    CLI[CLI code queries] --> Index
    Agent[Agent MCP client] --> MCP[git-kb mcp]
    MCP --> Socket[Per-KB socket]
    Socket --> Daemon[Daemon handlers]
    Daemon --> Index
```

The watcher reports changes; the index pipeline extracts symbols and resolves relationships. Queries read the resulting index. Separate KBs have separate daemon endpoints.

## Start and inspect

MCP and commands that write to the knowledge base start the daemon automatically.

```bash
# Run in the foreground
git-kb daemon start

# Or start a background process
git-kb daemon start --background

# Inspect status and registered workspaces
git-kb daemon status --json
git-kb workspace list --json

# Stop or restart
git-kb daemon stop
git-kb daemon restart
```

Use `git-kb workspace status --json` and `git-kb code doctor --json` to check workspace checkouts and repository indexing state.

Commands that need the daemon start a lease-scoped one automatically; it can stop once no command is using it. Start a daemon yourself (`git-kb daemon start --background`) when you want it to keep running, for example to keep the knowledge base in sync.

## Sync

A running daemon keeps the knowledge base converged with its **default remote**: on an interval (`daemon.sync_interval_secs`) it runs the automatic sync steps — pull, replay your unpublished commits, publish — so a healthy knowledge base stays in sync without commands.

- It stops at anything that needs a decision, such as a conflict. `git-kb sync status --json` lists those rows and the command each one needs.
- It never writes into a document you are editing. That document waits until you commit or pull it yourself.
- Other remotes sync only when you run `git-kb sync converge <remote>`.

## File watching

The source watcher respects `.gitignore` and `.gitkbignore` and batches rapid saves with a default 500 ms debounce. It publishes file-change events, and the daemon schedules incremental indexing for registered source roots and linked worktrees.

```bash
git-kb code index src/ --json
git-kb daemon status --json
```

Indexing is asynchronous. A saved file can still be waiting in a batch or indexing queue when a query runs. Check `git-kb code doctor --json` and daemon logs if results are missing or stale.

The workspace watcher monitors `.kb/workspaces/` for document edits. Those files remain pending edits until an explicit `git-kb commit`; watching does not commit your work.

## Embeddings

Initialize local semantic search and index committed documents on demand:

```bash
git-kb semantic init
git-kb semantic index --json
git-kb semantic status --json
```

See [Configuration](/docs/reference/configuration/) for `[embeddings]` settings (`backend`, `model`, `batch_size`, `batch_delay_ms`). Model loading initializes lazily on demand; document and code services remain available while embeddings initialize, and memory use depends on the selected model, backend, and vector indexes.

## Events

Subscribe to the daemon's event stream from the CLI:

```bash
git-kb events --filter 'code:*' --count 10 --idle-timeout 30
```

Events let integrations observe KB and code-index changes without repeatedly querying for updates. The CLI exposes filters and NDJSON output; see [CLI Reference](/docs/cli-reference/) for the current options.

## Logs and diagnostics

```bash
# Inspect recent background-process logs
git-kb daemon logs --tail 100

# Follow new log entries
git-kb daemon logs --follow

# Run a foreground daemon with verbose logging
git-kb --verbose daemon start

# Inspect configuration and code-index health
git-kb doctor --json
git-kb code doctor --json
```

Foreground logs go to the terminal. A background daemon writes `.kb/.cache/daemon.log`.

## Next steps

- [MCP Setup](/docs/getting-started/mcp-setup/) — Connect an agent to the service
- [Code Intelligence](/docs/core-concepts/code-intelligence/) — Query indexed code
- [Configuration](/docs/reference/configuration/) — Configure the KB
- [Troubleshooting](/docs/reference/troubleshooting/) — Diagnose service problems
