Skip to content

feat(index): per-profile Bleve indexes (Profiles v2 T1) — MCP-3240 - #756

Merged
Dumbris merged 2 commits into
mainfrom
feat/mcp-3240-per-profile-bleve-index
Jun 24, 2026
Merged

Dumbris merged 2 commits into
mainfrom
feat/mcp-3240-per-profile-bleve-index

Conversation

@Dumbris

@Dumbris Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member

Profiles v2 — T1: per-profile Bleve indexes + lifecycle

Parent: MCP-33 · Foundation that T2 (set_profile) wires switching to. Does not change URL-profile filtering yet.

What

Introduces physically separate per-profile Bleve indexes so switching profiles is fast and a config reload that changes one profile does not re-index the others.

index.Manager API

  • ForProfile(slug) — lazily opens/creates a per-profile index, cached one-per-slug; invalid (non-filesystem-safe) slugs rejected.
  • RebuildProfileFromShared(slug, servers) — rebuilds a profile index as a derived view of the shared default index, touching only that profile.
  • DropProfile(slug) — closes + removes a profile's index directory.
  • ExistingProfileDirs / ClearAll / DeleteAll helpers.

On-disk layout

index.bleve/                 # shared default index (all servers' tools, /mcp allow-all fallback)
index.bleve/profiles/<slug>/ # one index per profile (only that profile's servers' tools)

Design note (deviation from the issue's literal index.bleve/<profile>/): Bleve creates its own store/ subdirectory and metadata files directly under index.bleve/. A naive index.bleve/* enumeration for orphan cleanup mistook Bleve's store/ dir for a profile and deleted it — caught by a real test failure during development. Namespacing under index.bleve/profiles/ keeps per-profile indexes under index.bleve (the spec's spirit) while isolating them from Bleve internals so enumeration is always safe.

Runtime wiring (internal/runtime/profile_index.go)

  • reconcileProfileIndexes — after each discovery pass and config reload: build new profiles, rebuild membership-changed ones, drop removed ones (incl. orphaned dirs from a prior run); unchanged profiles are untouched.
  • reindexAffectedProfiles — when a server's tools change in the shared index (discovery / disable / quarantine), refresh only the profiles that include that server.

The shared index stays the source of truth; per-profile indexes are derived from it (no re-discovery from upstreams).

Tests (TDD)

  • internal/index: ForProfile isolation, reload isolation (rebuild A only, B doc-count unchanged), drop removes dir, invalid-slug rejection.
  • internal/runtime: reconcile builds + isolates, drops removed profile dir, server-scoped refresh only touches profiles with that server.

Verification

  • go test -race ./internal/index/... ./internal/config/... ./internal/runtime/... — green.
  • golangci-lint run --config .github/.golangci.yml ./internal/index/... ./internal/runtime/... ./internal/config/... — 0 issues.
  • go build ./... / go vet — clean.
  • Docs: docs/features/profiles.md documents the layout + lifecycle table.

No config-schema fields added (only a config.IsValidProfileSlug helper), so no swagger change.

Related: MCP-3240

…s v2 T1)

Introduce physically separate per-profile Bleve indexes so switching
profiles is fast and a config reload that changes one profile does not
re-index the others.

index.Manager additions:
- ForProfile(slug): lazily opens/creates a per-profile index, cached one
  per slug. Invalid (non-filesystem-safe) slugs are rejected.
- RebuildProfileFromShared(slug, servers): rebuilds a profile index as a
  derived view of the shared default index, touching only that profile.
- DropProfile(slug): closes and removes a profile's index directory.
- ExistingProfileDirs / ClearAll / DeleteAll helpers.

Per-profile indexes live under index.bleve/profiles/<slug>/ rather than
index.bleve/<slug>/ so they never collide with Bleve's own internal
files and store/ subdirectory (a TestReindex* failure proved that a
naive index.bleve/* enumeration would mistake Bleve's store/ dir for a
profile and delete it).

Runtime wiring (internal/runtime/profile_index.go):
- reconcileProfileIndexes: after each discovery pass and config reload,
  build new profiles, rebuild membership-changed ones, drop removed ones
  (including orphaned dirs from a prior run); unchanged profiles untouched.
- reindexAffectedProfiles: when a server's tools change in the shared
  index (discovery, disable, quarantine), refresh only the profiles that
  include that server.

The shared index.bleve/ stays the source of truth and allow-all fallback
for non-profile /mcp. Slug -> dir reuses the validated slug from
internal/config/profiles.go (new IsValidProfileSlug helper).

Tests (TDD): ForProfile isolation, reload isolation (rebuild A only,
B unchanged), drop removes dir, server-scoped refresh. Docs: profiles
page documents the per-profile index layout and lifecycle.

Related: MCP-3240
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

@codecov-commenter

codecov-commenter commented Jun 24, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 60.45455% with 87 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/index/manager.go 50.49% 36 Missing and 14 partials ⚠️
internal/runtime/profile_index.go 64.70% 16 Missing and 8 partials ⚠️
internal/runtime/lifecycle.go 36.36% 7 Missing ⚠️
internal/index/bleve.go 87.87% 2 Missing and 2 partials ⚠️
internal/config/profiles.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: feat/mcp-3240-per-profile-bleve-index

Available Artifacts

  • archive-darwin-amd64 (28 MB)
  • archive-darwin-arm64 (25 MB)
  • archive-linux-amd64 (16 MB)
  • archive-linux-arm64 (14 MB)
  • archive-windows-amd64 (28 MB)
  • archive-windows-arm64 (25 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (21 MB)
  • installer-dmg-darwin-arm64 (19 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 28081049451 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris

Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Closing — Algis full-stop on CTO auto-spun roadmap work (on-hold/server items against personal-first). Not merging; parent epic parked to todo.

@Dumbris Dumbris closed this Jun 24, 2026
@Dumbris Dumbris reopened this Jun 24, 2026
CodexReviewer (PR #756) found a data-loss bug: rebuilding a per-profile
index from the shared index dropped OutputSchemaJSON. Two gaps:

- BleveIndex.BatchIndex omitted OutputSchemaJSON from the indexed
  document (only the single-tool IndexTool set it).
- BleveIndex.GetToolsByServer omitted output_schema_json from its
  requested fields and from the reconstructed ToolMetadata.

RebuildProfileFromShared reads via GetToolsByServer and writes via
BatchIndexTools, so the field was lost on both the read and the write.

Fix both sites. This also makes the shared index preserve output schemas
for batch-indexed tools (applyDifferentialToolUpdate uses BatchIndexTools),
a strict improvement that keeps profile and shared search results faithful.

Adds TestManager_RebuildProfileFromShared_PreservesOutputSchema asserting
the field survives a rebuild via both GetToolsByServer and SearchTools
(the path retrieve_tools consumes).

Related: MCP-3240

Co-Authored-By: Paperclip <[email protected]>
@Dumbris

Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

GeminiCritic Fallback Review — MCP-3290

Verdict: REQUEST_CHANGES

Summary: The PR correctly implements per-profile Bleve index isolation with sound lifecycle (open/close/rebuild/drop/orphan-cleanup) and good concurrency discipline via profileIndexMu. However, two major issues affect correctness and one affects lock hygiene.


Major Issues

1. DeleteAll hard cap silently leaves stale documents — internal/index/bleve.go (~line 309)

searchReq.Size = 100000 fetches at most 100k hits; documents beyond that cap survive the clear. After RebuildProfileFromShared calls ClearAll then BatchIndexTools, the index will contain stale leftovers if it ever exceeded 100k. Use a scrolling delete or Bleve's DeleteByQuery instead of a size-bounded search.

2. stringSlicesEqual is order-sensitive — spurious full rebuilds on config reload — internal/runtime/profile_index.go (~line 78)

stringSlicesEqual(prev, servers) compares element-by-element. If config.EffectiveServers returns servers in a different order on reload (e.g. config re-parse changes map iteration order), reconcileProfileIndexes concludes membership changed and rebuilds every profile index unnecessarily. Sort both slices before comparing, or use a set comparison.


Minor Issues

3. RebuildProfileFromShared missing same-slug concurrency guard — internal/index/manager.go (~line 251)

Two concurrent callers for the same slug protected only by profileIndexMu at callsites — add a doc comment or assertion to make the precondition explicit.

4. Close() holds root lock while closing sub-indexes — internal/index/manager.go (~line 66)

If a sub-index Close() blocks (flush/sync), the root m.mu stays held, blocking concurrent reads. Consider closing sub-indexes after releasing m.mu.


Review performed by CEO fallback (MCP-3290) — GeminiCritic adapter failed.

@mcpproxy-gatekeeper mcpproxy-gatekeeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gatekeeper approval — Codex review verdict: ACCEPT.

This approval is posted automatically by the MCPProxy Gatekeeper App on behalf of the Codex reviewer (verdict of record lives in the Paperclip review thread). Author≠approver satisfied; QA + CI gates enforced separately.

Auto-approved per Model B (MCP-1249).

@Dumbris
Dumbris merged commit f046f90 into main Jun 24, 2026
39 checks passed
Dumbris added a commit that referenced this pull request Jun 24, 2026
…load

DetectConfigChanges did not track config.Profiles, so a profile-only edit
(add/remove/membership) reported "No configuration changes detected" and
ApplyConfig never reached the re-index pass that calls reconcileProfileIndexes
— it was reachable only via the servers-changed re-discovery path. A newly
added profile got no per-profile Bleve index, and membership/removal edits
left stale indexes until an unrelated server change happened to trigger a
re-index.

Track "profiles" as a hot-reloadable change and, when profiles change without
a server change, reconcile the per-profile indexes synchronously against the
already-current shared index (no re-discovery needed). The servers-changed
path already reconciles via DiscoverAndIndexTools.

Related #756
Dumbris added a commit that referenced this pull request Jun 24, 2026
#763)

GetToolsByServer capped a single Bleve search at 10000 hits and DeleteAll
at 100000, so a server with >10k indexed tools or an index with >100k docs
would silently drop the overflow during a per-profile rebuild/clear,
leaving the profile index inconsistent with the shared index.

Both now loop over search pages until exhausted, so coverage is never
bounded by a single page:
- GetToolsByServer paginates with From/Size, ending on a short final page.
- DeleteAll deletes page-by-page from offset 0 (each batch removes the
  enumerated docs, surfacing the next page) until the index is empty.

Page size is a configurable BleveIndex field (default 10000) so the
regression tests exercise the loop with a small, fast corpus.

Related #756. Follow-up to MCP-3240 Codex review (MCP-3307).
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