fix(app): start MCP servers only for open directories (#28937)

This commit is contained in:
Luke Parker 2026-05-29 07:51:11 +10:00 committed by GitHub
commit e16bfd745d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 180 additions and 23 deletions

View file

@ -0,0 +1,49 @@
import { describe, expect, test } from "bun:test"
import { createRoot } from "solid-js"
import { createRefCountMap } from "./refcount"
import { pathKey } from "./path-key"
describe("createRefCountMap", () => {
test("removes an item after its last owner is disposed", () => {
const removed: string[] = []
const map = createRefCountMap(
(key) => key,
(key) => removed.push(key),
)
const first = createRoot((dispose) => {
map("/project")
return dispose
})
const second = createRoot((dispose) => {
map("/project")
return dispose
})
first()
expect(removed).toEqual([])
second()
expect(removed).toEqual(["/project"])
})
test("keeps equivalent path consumers until the last owner is disposed", () => {
const removed: string[] = []
const map = createRefCountMap(
(key) => key,
(key) => removed.push(key),
pathKey,
)
const first = createRoot((dispose) => {
map("C:\\repo")
return dispose
})
const second = createRoot((dispose) => {
map("C:/repo/")
return dispose
})
first()
expect(removed).toEqual([])
second()
expect(removed).toEqual(["C:/repo"])
})
})

View file

@ -1,26 +1,32 @@
import { onCleanup } from "solid-js"
export function createRefCountMap<T>(create: (key: string) => T) {
export function createRefCountMap<T>(
create: (key: string) => T,
remove?: (key: string) => void,
identity: (key: string) => string = (key) => key,
) {
const items = new Map<string, T>()
const refCounts = new Map<string, number>()
return (key: string) => {
const id = identity(key)
onCleanup(() => {
refCounts.set(key, (refCounts.get(key) ?? 0) - 1)
if (refCounts.get(key) === 0) {
items.delete(key)
refCounts.delete(key)
refCounts.set(id, (refCounts.get(id) ?? 0) - 1)
if (refCounts.get(id) === 0) {
remove?.(id)
items.delete(id)
refCounts.delete(id)
}
})
const cached = items.get(key)
const cached = items.get(id)
if (cached) {
refCounts.set(key, (refCounts.get(key) ?? 0) + 1)
refCounts.set(id, (refCounts.get(id) ?? 0) + 1)
return cached
}
const item = create(key)
items.set(key, item)
refCounts.set(key, 1)
items.set(id, item)
refCounts.set(id, 1)
return item
}
}