Skip to content

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

TypeScript

fix(mcp-host): sweep stranded GFS trash and forget entries only after removal

Alfredo Lopez5926457da63c
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 850fed973..0031398af 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.530",
+ "version": "0.1.531",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.530",
+ "version": "0.1.531",
"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 d0ad8c6a6..3c50c27d4 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.530",
+ "version": "0.1.531",
"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 4e37bf417..29fc5dce2 100644
--- a/mcp-host/src/internalTools/gfsDownloadStore.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts
@@ -4,7 +4,10 @@ import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import { logger } from '../logger'
import type { GfsImageSource } from '../visualInput/policy'
-import { RETIRED_GFS_DOWNLOAD_STORE_PREFIX } from '../workspace/protectedPaths'
+import {
+ GFS_DOWNLOADS_TRASH_PREFIX,
+ RETIRED_GFS_DOWNLOAD_STORE_PREFIX,
+} from '../workspace/protectedPaths'
import { recordGfsDownloadExpiry, recordGfsDownloadQuota } from './gfsDownloadMetrics'
import {
GFS_FILE_LIMITS,
@@ -36,6 +39,10 @@ export const GFS_INPUT_DIRECTORY_RE = new RegExp(`^input-(${UUID_PATTERN})$`)
/** A directory renamed out of the way before removal; swept if a removal stops. */
const TRASH_PREFIX = '.trash-'
const TRASH_DIRECTORY_RE = new RegExp(`^\\.trash-${UUID_PATTERN}$`)
+/** A replaced `.gfs-downloads`, renamed inside its caller root before removal. */
+const DOWNLOADS_TRASH_DIRECTORY_RE = new RegExp(
+ `^${GFS_DOWNLOADS_TRASH_PREFIX.replaceAll('.', '\\.')}${UUID_PATTERN}$`
+)
/** Host-root names the retirement of the pre-#1028 store gives the old tree. */
const RETIRED_DIRECTORY_RE = new RegExp(
`^${RETIRED_GFS_DOWNLOAD_STORE_PREFIX.replaceAll('.', '\\.')}${UUID_PATTERN}$`
@@ -165,6 +172,8 @@ export interface GfsDownloadInventory {
* `unknown`: the disk did not answer (EMFILE, EIO, ...). Nothing is decided
* about the directory; it is kept as it is and inspected again later.
*/
+type RemovalResult = 'removed' | 'absent' | 'failed'
+
type DirectoryState =
| { state: 'absent' }
| { state: 'incomplete' }
@@ -774,7 +783,7 @@ export class GfsDownloadStore {
if (this.active.get(id) !== transfer) throw new GfsDownloadStoreError('download_busy')
this.releaseActive(id)
this.unpinId(id)
- if (!(await this.removeDirectory(transfer.directory, 'incomplete_removed')))
+ if ((await this.removeDirectory(transfer.directory, 'incomplete_removed')) === 'failed')
throw new GfsDownloadStoreError('storage_write_failed')
})
}
@@ -953,9 +962,11 @@ export class GfsDownloadStore {
for (const { id, directory } of await this.listInputDirectories(totals))
addCandidate(id, directory)
for (const entry of this.entries.values()) addCandidate(entry.id, entry.directory)
- const removeIncomplete = async (directory: string) => {
- if (await this.removeDirectory(directory, 'incomplete_removed')) totals.removedIncomplete += 1
- else totals.removeFailed += 1
+ const removeIncomplete = async (directory: string): Promise<boolean> => {
+ const result = await this.removeDirectory(directory, 'incomplete_removed')
+ if (result === 'removed') totals.removedIncomplete += 1
+ else if (result === 'failed') totals.removeFailed += 1
+ return result !== 'failed'
}
for (const [id, directories] of candidates) {
@@ -990,16 +1001,18 @@ export class GfsDownloadStore {
)
continue
}
+ // An entry is forgotten only once its directory is gone: a failed
+ // removal leaves it indexed, charged and with its provenance.
if (state.state === 'incomplete') {
- this.forget(id)
- await removeIncomplete(directory)
+ if (await removeIncomplete(directory)) this.forget(id)
continue
}
const entry = state.entry
if (Date.parse(entry.expiresAt) <= now && !this.isPinned(id)) {
- this.forget(id)
- if (await this.removeDirectory(directory, 'expired_removed')) totals.removedExpired += 1
- else totals.removeFailed += 1
+ const result = await this.removeDirectory(directory, 'expired_removed')
+ if (result === 'removed') totals.removedExpired += 1
+ if (result === 'failed') totals.removeFailed += 1
+ else this.forget(id)
continue
}
this.entries.set(id, entry)
@@ -1077,7 +1090,7 @@ export class GfsDownloadStore {
for (const name of names) {
if (!RETIRED_DIRECTORY_RE.test(name)) continue
try {
- await this.removeVerified(path.join(this.hostRoot, name), RETIRED_GFS_DOWNLOAD_STORE_PREFIX)
+ await this.removeVerified(path.join(this.hostRoot, name))
} catch (error) {
failed('remove', error)
}
@@ -1104,18 +1117,22 @@ export class GfsDownloadStore {
this.reportSweepFailure(error)
return found
}
+ const removeStray = async (directory: string, sweep: SweepTotals) => {
+ const result = await this.removeDirectory(directory, 'incomplete_removed')
+ if (result === 'removed') sweep.removedIncomplete += 1
+ else if (result === 'failed') sweep.removeFailed += 1
+ }
for (const user of users) {
if (!user.isDirectory()) continue
- const downloadsRoot = path.join(usersRoot, user.name, DOWNLOADS_DIRECTORY)
+ const callerRoot = path.join(usersRoot, user.name)
+ if (totals)
+ await this.sweepDownloadsTrash(callerRoot, directory => removeStray(directory, totals))
+ const downloadsRoot = path.join(callerRoot, DOWNLOADS_DIRECTORY)
let children: import('node:fs').Dirent[]
try {
const info = await fs.lstat(downloadsRoot)
if (!info.isDirectory()) {
- if (totals) {
- if (await this.removeDirectory(downloadsRoot, 'incomplete_removed'))
- totals.removedIncomplete += 1
- else totals.removeFailed += 1
- }
+ if (totals) await removeStray(downloadsRoot, totals)
continue
}
children = await fs.readdir(downloadsRoot, { withFileTypes: true })
@@ -1127,19 +1144,34 @@ export class GfsDownloadStore {
for (const child of children) {
const match = GFS_INPUT_DIRECTORY_RE.exec(child.name)
if (match) found.push({ id: match[1]!, directory: path.join(downloadsRoot, child.name) })
- else if (totals && TRASH_DIRECTORY_RE.test(child.name)) {
+ else if (totals && TRASH_DIRECTORY_RE.test(child.name))
// Left by a removal that stopped between its rename and its rm.
- if (
- await this.removeDirectory(path.join(downloadsRoot, child.name), 'incomplete_removed')
- )
- totals.removedIncomplete += 1
- else totals.removeFailed += 1
- }
+ await removeStray(path.join(downloadsRoot, child.name), totals)
}
}
return found
}
+ /**
+ * A replaced `.gfs-downloads` is renamed to `.gfs-downloads.trash-<uuid>`
+ * in its caller root; one a stopped removal left there still holds copies
+ * of user files, so the sweep removes it like any other trash.
+ */
+ private async sweepDownloadsTrash(
+ callerRoot: string,
+ remove: (directory: string) => Promise<void>
+ ): Promise<void> {
+ let names: string[]
+ try {
+ names = await fs.readdir(callerRoot)
+ } catch (error) {
+ if (errorCode(error) !== 'ENOENT') this.reportSweepFailure(error)
+ return
+ }
+ for (const name of names)
+ if (DOWNLOADS_TRASH_DIRECTORY_RE.test(name)) await remove(path.join(callerRoot, name))
+ }
+
/** Reads one directory from disk; a complete result is always `adopted`. */
private async inspectDirectory(
directory: string,
@@ -1294,8 +1326,9 @@ export class GfsDownloadStore {
} catch (error) {
if (!isDefinitiveMismatch(error)) throw error
}
- for (const id of this.idsUnder(downloadsRoot)) this.forget(id)
+ const replaced = this.idsUnder(downloadsRoot)
await this.removeVerified(downloadsRoot)
+ for (const id of replaced) this.forget(id)
await fs.mkdir(downloadsRoot, { mode: 0o700 })
await verifyPrivateStoreDirectory(downloadsRoot)
}
@@ -1394,14 +1427,18 @@ export class GfsDownloadStore {
}
}
- /** Removes an indexed entry's directory and forgets it. */
+ /**
+ * Removes an indexed entry's directory, then forgets it. A failed removal
+ * keeps the entry indexed: still charged to its caller, and a published
+ * copy stays published instead of coming back as adopted.
+ */
private async removeEntry(
entry: Entry,
outcome: 'incomplete_removed' | 'expired_removed',
count = true
): Promise<void> {
- this.forget(entry.id)
- await this.removeDirectory(entry.directory, count ? outcome : undefined)
+ if ((await this.removeDirectory(entry.directory, count ? outcome : undefined)) !== 'failed')
+ this.forget(entry.id)
}
/**
@@ -1415,19 +1452,21 @@ export class GfsDownloadStore {
private async removeDirectory(
directory: string,
outcome: 'incomplete_removed' | 'expired_removed' | undefined
- ): Promise<boolean> {
+ ): Promise<RemovalResult> {
+ let result: RemovalResult
try {
- await this.removeVerified(directory)
+ result = await this.removeVerified(directory)
} catch (error) {
recordGfsDownloadExpiry('remove_failed')
logger.warn(
{ component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, code: errorCode(error) },
'GFS download store could not remove a download directory; the next sweep retries it'
)
- return false
+ return 'failed'
}
- if (outcome) recordGfsDownloadExpiry(outcome)
- return true
+ // A directory that never existed is not a removal.
+ if (result === 'removed' && outcome) recordGfsDownloadExpiry(outcome)
+ return result
}
/**
@@ -1438,24 +1477,49 @@ export class GfsDownloadStore {
* parent moved in between, the rename is undone and the removal refused,
* so whatever the swapped path led to keeps its name and content.
*/
- private async removeVerified(directory: string, trashPrefix = TRASH_PREFIX): Promise<void> {
+ private async removeVerified(directory: string): Promise<'removed' | 'absent'> {
await this.assertRemovable(directory)
- const trash = path.join(path.dirname(directory), `${trashPrefix}${randomUUID()}`)
+ const trash = path.join(
+ path.dirname(directory),
+ `${this.trashPrefixFor(directory)}${randomUUID()}`
+ )
try {
await fs.rename(directory, trash)
} catch (error) {
if (errorCode(error) !== 'ENOENT') throw error
await assertAbsent(directory)
- return
+ return 'absent'
}
try {
await this.assertRemovable(trash)
} catch (error) {
- await fs.rename(trash, directory)
+ try {
+ await fs.rename(trash, directory)
+ } catch (undoError) {
+ // The refusal is what the caller must see; the trash name it is left
+ // under is one the sweep collects.
+ logger.warn(
+ { component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, code: errorCode(undoError) },
+ 'GFS download store could not restore a directory after refusing to remove it'
+ )
+ }
throw error
}
await fs.rm(trash, { recursive: true, force: true })
await assertAbsent(trash)
+ return 'removed'
+ }
+
+ /**
+ * The trash name is one the sweep lists in that parent: `.trash-<uuid>`
+ * inside `.gfs-downloads`, `.gfs-downloads.trash-<uuid>` in a caller root,
+ * `.gfs-download-store.retired-<uuid>` in the Host root.
+ */
+ private trashPrefixFor(directory: string): string {
+ const parent = path.dirname(directory)
+ if (parent === this.hostRoot) return RETIRED_GFS_DOWNLOAD_STORE_PREFIX
+ if (path.basename(parent) === DOWNLOADS_DIRECTORY) return TRASH_PREFIX
+ return GFS_DOWNLOADS_TRASH_PREFIX
}
/**
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
index 5eb91e4a1..49c83646c 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
@@ -19,12 +19,18 @@ import {
plantDownload,
sourceFor,
startTransfer,
+ withEnvironment,
} from '../__tests__/fixtures/gfsStoreTestKit'
import { logger } from '../logger'
+import {
+ GFS_DOWNLOADS_TRASH_PREFIX,
+ isGfsDownloadPath,
+ isProtectedWorkspacePath,
+} from '../workspace/protectedPaths'
import { deriveUserKey } from '../workspace/userKey'
import { GfsDownloadStore } from './gfsDownloadStore'
-type FsOperation = 'open' | 'lstat' | 'rename' | 'rm' | 'readdir'
+type FsOperation = 'open' | 'lstat' | 'rename' | 'rm' | 'readdir' | 'realpath'
/** A fault returns the errno to throw for this call, or undefined to pass through. */
type Fault = (target: string) => string | undefined
@@ -54,6 +60,7 @@ vi.mock('node:fs/promises', async original => {
rename: wrap('rename', actual.rename),
rm: wrap('rm', actual.rm),
readdir: wrap('readdir', actual.readdir),
+ realpath: wrap('realpath', actual.realpath),
}
})
@@ -331,3 +338,127 @@ describe('GFS download store adversarial round: transient I/O is not corruption
}
)
})
+
+const REMOVE_FAILED =
+ 'GFS download store could not remove a download directory; the next sweep retries it'
+const UNDO_FAILED = 'GFS download store could not restore a directory after refusing to remove it'
+
+describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/F2/F3/F8)', () => {
+ const KEY = 'bob-key'
+
+ it('a replaced .gfs-downloads stranded by a failed rm is protected and swept', async () => {
+ const store = await openStore()
+ const root = callerDirectory(hostRoot, KEY)
+ const { receipt } = await completedCopy(store, root, KEY, 1, 48)
+ syncFs.chmodSync(path.join(root, '.gfs-downloads'), 0o755)
+ const removals = watch('rm', '', 'EIO')
+
+ await expect(startTransfer(store, root, KEY, 2, 16)).rejects.toMatchObject({
+ code: 'storage_write_failed',
+ })
+ faults.clear()
+
+ expect(removals).toHaveLength(1)
+ const stranded = syncFs.readdirSync(root).filter(name => name.startsWith('.gfs-downloads.'))
+ expect(stranded).toHaveLength(1)
+ expect(stranded[0]).toMatch(new RegExp(`^${GFS_DOWNLOADS_TRASH_PREFIX.replaceAll('.', '\\.')}`))
+ expect(isProtectedWorkspacePath(`${stranded[0]}/input-${receipt.id}/source`)).toBe(true)
+ expect(isGfsDownloadPath(stranded[0]!)).toBe(true)
+
+ const removed = await expiryCount('incomplete_removed')
+ await store.cleanupExpired()
+ expect(exists(path.join(root, stranded[0]!))).toBe(false)
+ expect(await expiryCount('incomplete_removed')).toBeGreaterThan(removed)
+ })
+
+ it('an eviction whose removal fails keeps the copy published and charged', async () => {
+ const limited = await withEnvironment(
+ {
+ MCP_HOST_GFS_MAX_FILE_BYTES: '10',
+ MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: '30',
+ MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: '15',
+ },
+ 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 root = callerDirectory(hostRoot, KEY)
+ const { receipt, bytes } = await completedCopy(limited, root, KEY, 3, 10)
+ const failedRemovals = await expiryCount('remove_failed')
+ const renames = watch('rename', `input-${receipt.id}`, 'EIO')
+
+ await expect(startTransfer(limited, root, KEY, 4, 10)).rejects.toMatchObject({
+ code: 'caller_quota_exceeded',
+ })
+ faults.clear()
+
+ // Witness: the eviction was attempted and failed.
+ expect(renames).toHaveLength(1)
+ expect(await expiryCount('remove_failed')).toBe(failedRemovals + 1)
+ await expect(limited.readManagedFile(receipt.path, KEY)).resolves.toEqual(bytes)
+ await expect(limited.reusableReceipt(KEY, sourceFor(3), 10)).resolves.toMatchObject({
+ id: receipt.id,
+ })
+ // A sweep does not turn it into an adopted copy either.
+ await limited.cleanupExpired()
+ await expect(limited.readManagedFile(receipt.path, KEY)).resolves.toEqual(bytes)
+ })
+
+ it('a refused removal whose undo also fails reports the refusal, not the undo', async () => {
+ const store = await openStore()
+ const root = callerDirectory(hostRoot, KEY)
+ const { transfer } = await startTransfer(store, root, KEY, 5, 16)
+ const warn = vi.spyOn(logger, 'warn')
+ let renamedToTrash = false
+ injectFault('rename', target => {
+ if (target.endsWith(`input-${transfer.id}`)) {
+ renamedToTrash = true
+ return undefined
+ }
+ return path.basename(target).startsWith('.trash-') ? 'EXDEV' : undefined
+ })
+ injectFault('realpath', target =>
+ renamedToTrash && target.endsWith('.gfs-downloads') ? 'EIO' : undefined
+ )
+
+ await expect(store.fail(transfer.id, KEY)).rejects.toMatchObject({
+ code: 'storage_write_failed',
+ })
+ faults.clear()
+
+ expect(renamedToTrash).toBe(true)
+ expect(warnings(warn)).toContainEqual({ code: 'EXDEV', message: UNDO_FAILED })
+ expect(warnings(warn)).toContainEqual({ code: 'EIO', message: REMOVE_FAILED })
+ expect(warnings(warn)).not.toContainEqual({ code: 'EXDEV', message: REMOVE_FAILED })
+ })
+
+ it('a removal of a directory that never existed is not counted as removed', async () => {
+ const store = await openStore()
+ const root = callerDirectory(hostRoot, KEY)
+ await completedCopy(store, root, KEY, 6, 16)
+ const removed = await expiryCount('incomplete_removed')
+ const opens = watch('open', `${KEY}/.gfs-downloads`, 'EMFILE')
+ const renames: string[] = []
+ injectFault('rename', target => {
+ if (path.basename(target).startsWith('input-')) renames.push(target)
+ return undefined
+ })
+
+ await expect(startTransfer(store, root, KEY, 7, 16)).rejects.toMatchObject({
+ code: 'storage_write_failed',
+ })
+ faults.clear()
+
+ // Witness: the admission failed after reserving its directory name and
+ // tried to remove it.
+ expect(opens.length).toBeGreaterThan(0)
+ expect(renames).toHaveLength(1)
+ expect(exists(renames[0]!)).toBe(false)
+ expect(await expiryCount('incomplete_removed')).toBe(removed)
+ })
+})
diff --git a/mcp-host/src/workspace/protectedPaths.ts b/mcp-host/src/workspace/protectedPaths.ts
index 21d8b1f9e..fd310d2e7 100644
--- a/mcp-host/src/workspace/protectedPaths.ts
+++ b/mcp-host/src/workspace/protectedPaths.ts
@@ -13,8 +13,18 @@ export const PROTECTED_WORKSPACE_DIRS: ReadonlySet<string> = new Set([
*/
export const RETIRED_GFS_DOWNLOAD_STORE_PREFIX = '.gfs-download-store.retired-'
+/**
+ * Name prefix of a caller's `.gfs-downloads` the store renamed inside the
+ * caller root before replacing it. It holds the same copies, so it is
+ * protected the same way until the sweep removes it.
+ */
+export const GFS_DOWNLOADS_TRASH_PREFIX = '.gfs-downloads.trash-'
+
function isRetiredGfsStoreSegment(segment: string): boolean {
- return segment.startsWith(RETIRED_GFS_DOWNLOAD_STORE_PREFIX)
+ return (
+ segment.startsWith(RETIRED_GFS_DOWNLOAD_STORE_PREFIX) ||
+ segment.startsWith(GFS_DOWNLOADS_TRASH_PREFIX)
+ )
}
function isProtectedSegment(segment: string): boolean {