Skip to content

perf: defer vault indexing to background — MCP server times out on large repos #36

Closed

Description

@jflowers

Problem

Dewey's serve command performs vault indexing (incremental index + embedding generation) synchronously before starting the MCP server. On repos with 255+ local pages and 689 external pages, this takes ~32 seconds. OpenCode's MCP server startup timeout is ~30 seconds, causing Dewey to be killed before it can accept requests.

Evidence

From .uf/dewey/dewey.log:

13:35:49 INFO serve starting
13:35:49 INFO persistent store opened    elapsed=7ms
13:35:49 INFO ollama state               elapsed=5ms
13:35:49 INFO embedding model available
...  (incremental indexing + embedding generation takes ~30s)
13:36:32 INFO vault indexed              elapsed=~30s
13:36:32 INFO external pages loaded      pages=689 blocks=4091 elapsed=83ms
13:36:32 INFO file watcher started       elapsed=28ms
13:36:32 INFO server ready               startup=31.97s  <-- too late

The server ready message appears at 32 seconds. OpenCode has already timed out at ~30 seconds.

Root Cause

In main.go:executeServe(), the call chain is:

  1. initObsidianBackend() — calls indexVault() synchronously (line 560)
  2. indexVault() — runs incremental index + generates embeddings for changed blocks via Ollama API
  3. External pages loaded (line 574)
  4. File watcher started (line 586)
  5. Only then: runServer() starts accepting MCP requests (line 265)

The bottleneck is step 2 — each embedding generation is a round-trip HTTP call to Ollama. With dozens of changed blocks, this accumulates to ~30 seconds.

Proposed Fix

Start the MCP server immediately after store/backend initialization, then perform indexing in a background goroutine:

  1. Open store + check Ollama + verify model — fast (~20ms total)
  2. Create MCP server + start stdio transport — start accepting requests immediately
  3. Launch indexVault() + loadExternalPages() + Watch() in a background goroutine
  4. While indexing: search/traverse tools return results from the persistent store (stale but available). Semantic search may return partial results.
  5. When indexing completes: log it, backlinks rebuilt, watcher active

This means the server is available in <1 second instead of 32 seconds. The tradeoff is that the first few queries after startup may return stale results from the previous session's index — but that's better than timing out completely.

Additional Finding

One block in specs/008-swarm-orchestration/spec (1,410 chars) exceeds granite-embedding:30m's context length:

WARN failed to generate embedding chunkLen=1410 err="the input length exceeds the context length"

This should be handled gracefully (skip the block, log the warning — which it does) but the chunker could also be updated to split blocks that exceed the model's context window.

Environment

  • dewey version: dev (built from source)
  • MCP SDK: go-sdk v1.2.0
  • Index: 255 local pages, 689 external pages, 4,091 blocks
  • Ollama model: granite-embedding:30m
  • Platform: macOS arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions