Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): keep GFS duplicates charged when their removal fails

Alfredo Lopez83d54556296d
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index b8ef6d390..21c3d4a36 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.535",
+ "version": "0.1.536",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.535",
+ "version": "0.1.536",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index fdcf325cd..2b2a69a70 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.535",
+ "version": "0.1.536",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.ts
index 3e39be701..abe0482df 100644
--- a/mcp-host/src/internalTools/gfsDownloadStore.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts
@@ -146,6 +146,12 @@ interface ActiveTransfer {
expiresAt: string
}
+/** A duplicate directory the last sweep could not remove; it stays charged. */
+interface HeldCopy {
+ callerRoot: string
+ sizeBytes: number
+}
+
export interface GfsDownloadSweepResult {
removedExpired: number
removedIncomplete: number
@@ -383,6 +389,8 @@ export class GfsDownloadStore {
private readonly requestedHostRoot: string
private readonly entries = new Map<string, Entry>()
private readonly active = new Map<string, ActiveTransfer>()
+ /** Duplicates the last sweep could not remove, by directory; see sweep(). */
+ private held = new Map<string, HeldCopy>()
private readonly activeByCaller = new Map<string, number>()
/** Keyed by `pinKey(callerIdentity, ownerId)`: callers never share or see a pin. */
private readonly pins = new Map<string, Set<string>>()
@@ -981,6 +989,19 @@ export class GfsDownloadStore {
else if (result === 'failed') totals.removeFailed += 1
return result !== 'failed'
}
+ // A duplicate this sweep could not remove is still on disk, so it stays
+ // charged to the caller root that holds it: a failed cleanup never turns
+ // into free capacity.
+ const held = new Map<string, HeldCopy>()
+ const removeDuplicate = async (id: string, directory: string): Promise<void> => {
+ if (await removeIncomplete(directory)) return
+ const state = await this.inspectDirectory(directory, id, now)
+ if (state.state === 'complete')
+ held.set(directory, {
+ callerRoot: state.entry.callerRoot,
+ sizeBytes: state.entry.sizeBytes,
+ })
+ }
for (const [id, directories] of candidates) {
const indexed = this.entries.get(id)
@@ -988,13 +1009,17 @@ export class GfsDownloadStore {
this.active.get(id)?.directory ??
(indexed?.provenance === 'published' ? indexed.directory : undefined)
if (owned === undefined && directories.length > 1) {
- this.forget(id)
- for (const duplicate of directories) await removeIncomplete(duplicate)
+ // The indexed directory is forgotten only once it is gone; a failed
+ // removal leaves it indexed and charged, like any other entry.
+ for (const duplicate of directories) {
+ if (duplicate !== indexed?.directory) await removeDuplicate(id, duplicate)
+ else if (await removeIncomplete(duplicate)) this.forget(id)
+ }
continue
}
if (owned !== undefined)
for (const duplicate of directories)
- if (duplicate !== owned) await removeIncomplete(duplicate)
+ if (duplicate !== owned) await removeDuplicate(id, duplicate)
if (this.active.has(id)) continue
const directory = owned ?? directories[0]!
const state =
@@ -1033,6 +1058,7 @@ export class GfsDownloadStore {
totals.retainedBytes += entry.sizeBytes
if (entry.provenance === 'adopted') totals.adopted += 1
}
+ this.held = held
return totals
}
@@ -1393,6 +1419,7 @@ export class GfsDownloadStore {
for (const entry of this.entries.values())
if (!excluded.has(entry.id)) add(entry.callerRoot, entry.sizeBytes)
for (const transfer of this.active.values()) add(transfer.callerRoot, transfer.sizeBytes)
+ for (const copy of this.held.values()) add(copy.callerRoot, copy.sizeBytes)
return usage
}
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
index d42fb093f..ae2304045 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
@@ -7,6 +7,7 @@
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { execFileSync } from 'node:child_process'
+import { randomUUID } from 'node:crypto'
import * as syncFs from 'node:fs'
import * as fs from 'node:fs/promises'
import { tmpdir } from 'node:os'
@@ -16,6 +17,7 @@ import {
callerDirectory,
completedCopy,
digestOf,
+ downloadDirectory,
exists,
expiryCount,
metaFor,
@@ -32,6 +34,7 @@ import {
} from '../workspace/protectedPaths'
import { deriveUserKey } from '../workspace/userKey'
import { GfsDownloadStore } from './gfsDownloadStore'
+import { GFS_FILE_LIMITS } from './gfsFilePolicy'
type FsOperation = 'open' | 'lstat' | 'rename' | 'rm' | 'readdir' | 'realpath'
/**
@@ -695,3 +698,119 @@ describe('GFS download store adversarial round: a FIFO never blocks publication'
expect(outcome).toBe('refused')
})
})
+
+describe('GFS download store adversarial round: a failed duplicate removal stays charged', () => {
+ const OWNER = 'erin-key'
+ const PLANTER = 'frank-key'
+
+ /**
+ * Fails the removal rename of every `input-<id>` directory for `ids`, under
+ * the caller keys given (the store works on the real path of the host root,
+ * so callers are matched by their key segment).
+ */
+ function failRemovals(keys: readonly string[], ids: readonly string[]): string[] {
+ const names = new Set(ids.map(id => `input-${id}`))
+ const calls: string[] = []
+ injectFault('rename', target => {
+ const inCaller = keys.some(key => target.includes(`${path.sep}${key}${path.sep}`))
+ if (!inCaller || !names.has(path.basename(target))) return undefined
+ calls.push(target)
+ return 'EIO'
+ })
+ return calls
+ }
+
+ it('duplicates of a full caller’s ids whose removal fails keep both callers charged', async () => {
+ const owner = callerDirectory(hostRoot, OWNER)
+ const planter = callerDirectory(hostRoot, PLANTER)
+ const bytes = Buffer.from('quota')
+ const ids = Array.from({ length: GFS_FILE_LIMITS.callerRetainedFiles }, () => randomUUID())
+ for (const id of ids) plantDownload(owner, { id, bytes, meta: metaFor(id, bytes, Date.now()) })
+ const store = await openStore()
+ expect((await store.debugInventory()).byCaller.get(OWNER)?.files).toBe(ids.length)
+ const renames = failRemovals([OWNER, PLANTER], ids)
+
+ // Control: without duplicates the full caller is refused; its one eviction
+ // attempt fails like every other removal here.
+ await expect(startTransfer(store, owner, OWNER, 1, 5)).rejects.toMatchObject({
+ code: 'caller_quota_exceeded',
+ })
+ expect(renames).toHaveLength(1)
+ for (const id of ids)
+ plantDownload(planter, { id, bytes, meta: metaFor(id, bytes, Date.now()) })
+
+ const sweep = await store.cleanupExpired()
+
+ // Witness: every copy of every id was a removal attempt, and each failed.
+ expect(sweep.removeFailed).toBe(ids.length * 2)
+ expect(renames).toHaveLength(1 + ids.length * 2)
+ for (const id of ids) {
+ expect(exists(downloadDirectory(owner, id))).toBe(true)
+ expect(exists(downloadDirectory(planter, id))).toBe(true)
+ }
+ // The copies left on disk are still charged to the caller root holding them.
+ await expect(startTransfer(store, owner, OWNER, 2, 5)).rejects.toMatchObject({
+ code: 'caller_quota_exceeded',
+ })
+ await expect(startTransfer(store, planter, PLANTER, 3, 5)).rejects.toMatchObject({
+ code: 'caller_quota_exceeded',
+ })
+
+ // Once the removals succeed, both callers have their capacity back.
+ faults.clear()
+ await store.cleanupExpired()
+ for (const id of ids) {
+ expect(exists(downloadDirectory(owner, id))).toBe(false)
+ expect(exists(downloadDirectory(planter, id))).toBe(false)
+ }
+ const admitted = await startTransfer(store, owner, OWNER, 4, 5)
+ await store.fail(admitted.transfer.id, OWNER)
+ const planted = await startTransfer(store, planter, PLANTER, 5, 5)
+ await store.fail(planted.transfer.id, PLANTER)
+ })
+
+ it('a duplicate of a published copy whose removal fails stays charged to its caller', async () => {
+ const limited = await withEnvironment(
+ { MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' },
+ async () => {
+ vi.resetModules()
+ const { GfsDownloadStore: Store } = await import('./gfsDownloadStore')
+ const opened = new Store(hostRoot) as unknown as GfsDownloadStore
+ stores.push(opened)
+ await opened.initialize()
+ return opened
+ }
+ )
+ const owner = callerDirectory(hostRoot, OWNER)
+ const planter = callerDirectory(hostRoot, PLANTER)
+ const { receipt, bytes } = await completedCopy(limited, owner, OWNER, 6, 16)
+ // Control: the planter has capacity for one copy.
+ const control = await startTransfer(limited, planter, PLANTER, 7, 16)
+ await limited.fail(control.transfer.id, PLANTER)
+ plantDownload(planter, {
+ id: receipt.id,
+ bytes,
+ meta: metaFor(receipt.id, bytes, Date.now()),
+ })
+ const renames = failRemovals([PLANTER], [receipt.id])
+
+ const sweep = await limited.cleanupExpired()
+
+ // Witness: the duplicate's removal was attempted and failed; the published
+ // copy is not replaced by it.
+ expect(sweep.removeFailed).toBe(1)
+ expect(renames).toHaveLength(1)
+ expect(exists(downloadDirectory(planter, receipt.id))).toBe(true)
+ await expect(limited.readManagedFile(receipt.path, OWNER)).resolves.toEqual(bytes)
+ await expect(startTransfer(limited, planter, PLANTER, 8, 16)).rejects.toMatchObject({
+ code: 'caller_quota_exceeded',
+ })
+
+ faults.clear()
+ await limited.cleanupExpired()
+ expect(exists(downloadDirectory(planter, receipt.id))).toBe(false)
+ const admitted = await startTransfer(limited, planter, PLANTER, 9, 16)
+ await limited.fail(admitted.transfer.id, PLANTER)
+ await expect(limited.readManagedFile(receipt.path, OWNER)).resolves.toEqual(bytes)
+ })
+})
diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.json
index cb013c877..c19048938 100644
--- a/scripts/ci/pinned-vitest-cases/mcp-host.json
+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json
@@ -778,5 +778,13 @@
{
"suite": "gfsContentDownload.test.ts",
"fullName": "governed GFS content download fails at once when source.partial is replaced by a FIFO before the download opens it"
+ },
+ {
+ "suite": "gfsDownloadStoreAdversarial.test.ts",
+ "fullName": "GFS download store adversarial round: a failed duplicate removal stays charged duplicates of a full caller’s ids whose removal fails keep both callers charged"
+ },
+ {
+ "suite": "gfsDownloadStoreAdversarial.test.ts",
+ "fullName": "GFS download store adversarial round: a failed duplicate removal stays charged a duplicate of a published copy whose removal fails stays charged to its caller"
}
]