Skip to content

fix(serve-http): memoize project detection on the stateless request path - #409

Merged
AVIDS2 merged 1 commit into
AVIDS2:mainfrom
jfsema:fix/http-modern-detection-cache
Nov 27, 2026
Merged

AVIDS2 merged 1 commit into
AVIDS2:mainfrom
jfsema:fix/http-modern-detection-cache

Conversation

@jfsema

@jfsema jfsema commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Branch: fix/http-modern-detection-cache → main

Problem

Stateless (2026-07-28) requests have no session, so serve-http resolves the
project root on every request, up to three times: in handlePost,
resolveModernDataDir and the binding lookup. When the server was started
outside a Git repository, which is the usual case for a background control
plane, the fast .git walk fails. Detection then falls through to a
synchronous execSync('git rev-parse --show-toplevel'), and that call blocks
the event loop.

On the Windows test host, cmd /c ver took 0.7 s and git --version took
1.3–2.5 s. Measurements on that host showed:

  • a server/discover request with MCP-Protocol-Version: 2026-07-28 took
    2.07 s on an empty test server started outside Git, and 1–5 s on the loaded
    production control plane; on the same build started inside a repository it
    took 0.01 s;
  • The Claude Code logs showed the 5 s stateless probe timing out and the client
    falling back to a legacy session. Those connections took 15–20 s overall.

Change

  • New project-detection-cache.ts memoizes detection per path, including
    "not a project", for 5 minutes. At 256 paths, a new entry evicts the oldest
    cached entry.
  • Only the three stateless request-path call sites use it. Session start,
    roots changes and the CLI keep live detection.

Tests

  • tests/cli/project-detection-cache.test.ts covers positive and negative
    hits within the TTL, re-detection after expiry, and eviction at capacity.
  • Measured on the built server started outside Git: the first request takes
    1.39 s, the next ones 0.008–0.018 s (2.07 s each before the change).
  • In a repeat run on two isolated servers started outside Git, all four
    discovery requests returned HTTP 200 on each build. The fork took 3,438 ms
    for the first request and 9–10 ms for the next three. The official 1.10.4
    package took 7,837–10,969 ms per request.
  • Measured with a real Claude Code 2.1.330 against http://127.0.0.1:<port>/mcp:
    the stateless protocol is negotiated (protocolEra: modern) and the
    connection completes in 3.5 s, with no legacy fallback.
  • Four CLI suites failed in both the broader run and the unmodified main
    baseline. See fix(serve-http): release legacy sessions whose client stream closed #408 for the comparison.

Stateless (2026-07-28) requests resolve the project root on every call, up
to three times. For a server started outside a Git repository, each call
falls through to a synchronous `git rev-parse` subprocess that blocks the
event loop. On hosts with slow process creation (endpoint security
scanners on Windows add 1-3 s per spawn) the stateless probe of Claude
Code exceeded its 5 s budget and the client fell back to legacy sessions.

Detection results, including "not a project", are now cached per path
for 5 minutes (256 paths max).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AVIDS2

AVIDS2 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Integrated in #411 with original contributor commits retained. The bounded positive/negative cache stays limited to stateless request detection; explicit session binding retains live detection. Production-host smoke outside Git measured 350ms, 5ms, and 4ms for successive discovery requests locally. Cache TTL/eviction tests pass, and CI now exercises built-host discovery on all three desktop platforms. Included in 1.10.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants