Skip to content

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

TypeScript

fix(mcp-host): cap the bytes one caller can protect from eviction

Alfredo Lopez3d9f9d542c23
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 1df010922..5027e4889 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.541",
+ "version": "0.1.542",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.541",
+ "version": "0.1.542",
"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 490042602..b1de58c92 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.541",
+ "version": "0.1.542",
"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/gfsDownloadMetrics.ts b/mcp-host/src/internalTools/gfsDownloadMetrics.ts
index 215861968..b5e35f174 100644
--- a/mcp-host/src/internalTools/gfsDownloadMetrics.ts
+++ b/mcp-host/src/internalTools/gfsDownloadMetrics.ts
@@ -39,7 +39,11 @@ export type GfsDownloadShellLimitOutcome = 'output_limit_exceeded'
export type GfsDownloadQuotaScope = 'host' | 'caller'
-export type GfsDownloadQuotaReason = 'storage_bytes' | 'active_downloads' | 'free_space'
+export type GfsDownloadQuotaReason =
+ | 'storage_bytes'
+ | 'active_downloads'
+ | 'free_space'
+ | 'protected_bytes'
export type GfsDownloadExpiryOutcome =
| 'expired_removed'
diff --git a/mcp-host/src/internalTools/gfsDownloadStore.test.ts b/mcp-host/src/internalTools/gfsDownloadStore.test.ts
index bf0b3c509..588df37b1 100644
--- a/mcp-host/src/internalTools/gfsDownloadStore.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStore.test.ts
@@ -439,17 +439,26 @@ describe('GFS download store: quotas and eviction', () => {
})
it('F10: when eviction cannot make room nothing is deleted', async () => {
+ // Budget 16; a caller protects at most 8. CALLER pins 4 + 4, caller-b pins
+ // 8, and caller-c, protecting nothing yet, asks for 8: only the host budget
+ // can refuse it.
volumeOf(16)
const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)
- await completedCopy(limited, callerRoot, CALLER, 30, 8, { owner: 'task-1' })
+ const otherRoot = await callerDirectory(hostRoot, 'caller-b')
+ await completedCopy(limited, callerRoot, CALLER, 30, 4, { owner: 'task-1' })
+ tick()
+ await completedCopy(limited, callerRoot, CALLER, 31, 4, { owner: 'task-1' })
tick()
- await completedCopy(limited, callerRoot, CALLER, 31, 8, { owner: 'task-1' })
+ await completedCopy(limited, otherRoot, 'caller-b', 33, 8, { owner: 'task-2' })
const downloads = path.join(callerRoot, '.gfs-downloads')
+ const otherDownloads = path.join(otherRoot, '.gfs-downloads')
const before = (await nativeFs.readdir(downloads)).sort()
+ const otherBefore = (await nativeFs.readdir(otherDownloads)).sort()
const denied = await quotaCount('host', 'storage_bytes')
lstatBoundary.mockClear()
- await expect(startTransfer(limited, callerRoot, CALLER, 32, 8)).rejects.toMatchObject({
+ const requester = await callerDirectory(hostRoot, 'caller-c')
+ await expect(startTransfer(limited, requester, 'caller-c', 32, 8)).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
@@ -457,6 +466,8 @@ describe('GFS download store: quotas and eviction', () => {
expect(lstatBoundary.mock.calls.length).toBeGreaterThan(0)
expect((await nativeFs.readdir(downloads)).sort()).toEqual(before)
expect(before).toHaveLength(2)
+ expect((await nativeFs.readdir(otherDownloads)).sort()).toEqual(otherBefore)
+ expect(otherBefore).toHaveLength(1)
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
})
@@ -543,7 +554,7 @@ describe('GFS download store: quotas and eviction', () => {
})
it('F13: eviction candidates are not hashed', async () => {
- volumeOf(15)
+ volumeOf(18)
const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)
const first = await completedCopy(limited, callerRoot, CALLER, 60, 3)
tick()
@@ -553,8 +564,9 @@ describe('GFS download store: quotas and eviction', () => {
tick()
hashBoundary.mockClear()
- // 15 retained + 6 requested against a budget of 15: the plan takes the two 3-byte copies.
- await startTransfer(limited, callerRoot, CALLER, 64, 6)
+ // 15 retained + 9 requested against a budget of 18: the plan takes the two
+ // 3-byte copies. 9 is the most one caller may protect: floor(18 / 2).
+ await startTransfer(limited, callerRoot, CALLER, 64, 9)
// Witness: two candidates were evicted.
expect(await exists(downloadDirectory(callerRoot, first.receipt.id))).toBe(false)
@@ -984,20 +996,26 @@ describe('GFS download store: adversarial review kill tests', () => {
it('ADV-S08: an infeasible eviction plan deletes nothing even when some copies are evictable', async () => {
volumeOf(15)
const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)
+ // Budget 15; a caller protects at most 7. The 10 pinned bytes are held by
+ // caller-b and caller-c (5 each) and CALLER asks for 7: 5 + 10 + 7 = 22,
+ // and evicting the loose 5 still leaves 17 > 15.
const loose = await completedCopy(limited, callerRoot, CALLER, 70, 5)
tick()
- await completedCopy(limited, callerRoot, CALLER, 71, 8, { owner: 'task-1' })
+ const otherRoot = await callerDirectory(hostRoot, 'caller-b')
+ const thirdRoot = await callerDirectory(hostRoot, 'caller-c')
+ await completedCopy(limited, otherRoot, 'caller-b', 71, 5, { owner: 'task-1' })
+ await completedCopy(limited, thirdRoot, 'caller-c', 73, 5, { owner: 'task-2' })
const denied = await quotaCount('host', 'storage_bytes')
- await expect(startTransfer(limited, callerRoot, CALLER, 72, 10)).rejects.toMatchObject({
+ await expect(startTransfer(limited, callerRoot, CALLER, 72, 7)).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
// Witness: the admission reached the quota decision.
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
// The unpinned copy could not make room on its own, so it must survive.
expect(await exists(downloadDirectory(callerRoot, loose.receipt.id))).toBe(true)
- // Witness: once the pin is released the same admission evicts and succeeds.
- await limited.releaseReceiptOwner('task-1', CALLER)
- await expect(startTransfer(limited, callerRoot, CALLER, 72, 10)).resolves.toBeDefined()
+ // Witness: once one pin is released the same admission evicts and succeeds.
+ await limited.releaseReceiptOwner('task-1', 'caller-b')
+ await expect(startTransfer(limited, callerRoot, CALLER, 72, 7)).resolves.toBeDefined()
})
it('ADV-S11: publish refuses a digest that does not match the partial bytes', async () => {
diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.ts
index 2463de13f..9fad21f9e 100644
--- a/mcp-host/src/internalTools/gfsDownloadStore.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts
@@ -543,6 +543,8 @@ export class GfsDownloadStore {
const volume = await this.assertPhysicalCapacity(input.sizeBytes)
await this.sweep(Date.now())
const budget = retainedBudget(volume)
+ // The reservation itself is protected until it settles, pinned or not.
+ this.assertProtectedRoom(input.callerIdentity, budget, { sizeBytes: input.sizeBytes })
await this.reclaimForAdmission(input.sizeBytes, budget)
if (this.overBudget(input.sizeBytes, budget)) {
// The code alone: whose copies fill the budget is never part of it.
@@ -766,6 +768,14 @@ export class GfsDownloadStore {
candidate.source.version === source.version
)
if (!entry) return undefined
+ // Pinning a copy this caller does not protect yet adds it to the
+ // caller's protected bytes; checked before hashing, so a refusal costs
+ // no read.
+ if (bounds?.retentionOwnerId !== undefined)
+ this.assertProtectedRoom(callerIdentity, retainedBudget(await this.measureVolume()), {
+ id: entry.id,
+ sizeBytes: entry.sizeBytes,
+ })
let verified = false
let transient: string | undefined
try {
@@ -1431,12 +1441,17 @@ export class GfsDownloadStore {
* A statfs whose block size or block count is not positive cannot size
* anything; it refuses the admission as `free_space`, like a full volume.
*/
- private async assertPhysicalCapacity(sizeBytes: number): Promise<VolumeSpace> {
+ private async measureVolume(): Promise<VolumeSpace> {
const space = await fs.statfs(this.hostRoot, { bigint: true })
if (space.bsize <= 0n || space.blocks <= 0n || space.bavail < 0n) {
recordGfsDownloadQuota('host', 'free_space')
throw new GfsDownloadStoreError('host_quota_exceeded')
}
+ return { bsize: space.bsize, blocks: space.blocks, bavail: space.bavail }
+ }
+
+ private async assertPhysicalCapacity(sizeBytes: number): Promise<VolumeSpace> {
+ const space = await this.measureVolume()
const reserve = (bytes: number): bigint => {
const amount = BigInt(bytes)
return ((amount + space.bsize - 1n) / space.bsize) * space.bsize
@@ -1449,7 +1464,44 @@ export class GfsDownloadStore {
recordGfsDownloadQuota('host', 'free_space')
throw new GfsDownloadStoreError('host_quota_exceeded')
}
- return { bsize: space.bsize, blocks: space.blocks, bavail: space.bavail }
+ return space
+ }
+
+ /**
+ * Ids one caller holds out of eviction: its active reservations and the
+ * copies its retention owners pin, each id once.
+ */
+ private protectedIds(callerIdentity: string): Set<string> {
+ const ids = new Set<string>()
+ for (const transfer of this.active.values())
+ if (transfer.callerIdentity === callerIdentity) ids.add(transfer.id)
+ for (const [key, pinned] of this.pins)
+ if ((JSON.parse(key) as [string, string])[0] === callerIdentity)
+ for (const id of pinned) ids.add(id)
+ return ids
+ }
+
+ /**
+ * Eviction never reclaims a pinned copy or a reservation, so without a bound
+ * one caller's open tasks could hold the whole budget and refuse every other
+ * caller. A caller's protected bytes may therefore reach at most half the
+ * budget, which always leaves the other half reclaimable for anyone else.
+ * Only this caller's own usage decides; the refusal is the fixed code.
+ */
+ private assertProtectedRoom(
+ callerIdentity: string,
+ budget: bigint,
+ addition: { id?: string; sizeBytes: number }
+ ): void {
+ const ids = this.protectedIds(callerIdentity)
+ if (addition.id !== undefined && ids.has(addition.id)) return
+ let bytes = BigInt(addition.sizeBytes)
+ for (const id of ids)
+ bytes += BigInt(this.active.get(id)?.sizeBytes ?? this.entries.get(id)?.sizeBytes ?? 0)
+ if (bytes > budget / 2n) {
+ recordGfsDownloadQuota('caller', 'protected_bytes')
+ throw new GfsDownloadStoreError('host_quota_exceeded')
+ }
}
/** Retained bytes: indexed copies, active reservations and held duplicates. */
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
index 7daf5665b..1f477fca1 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts
@@ -391,7 +391,10 @@ describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/
})
it('an eviction whose removal fails keeps the copy published and charged', async () => {
- volume.totalBytes = 15n
+ // Budget 20, so a caller protects at most 10. KEY's unpinned 10, another
+ // caller's pinned 5 and KEY's requested 10 make 25: the unpinned copy is
+ // the only candidate, and evicting it would have made room.
+ volume.totalBytes = 20n
const limited = await withEnvironment(
{
MCP_HOST_GFS_MAX_FILE_BYTES: '10',
@@ -408,6 +411,10 @@ describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/
)
const root = callerDirectory(hostRoot, KEY)
const { receipt, bytes } = await completedCopy(limited, root, KEY, 3, 10)
+ const otherKey = `${KEY}-other`
+ await completedCopy(limited, callerDirectory(hostRoot, otherKey), otherKey, 7, 5, {
+ owner: 'task-other',
+ })
const failedRemovals = await expiryCount('remove_failed')
const renames = watch('rename', `input-${receipt.id}`, 'EIO')
@@ -596,7 +603,8 @@ describe('GFS download store adversarial round: expiry, close, recency and remov
await expect(limited.readManagedFile(older.receipt.path, KEY)).resolves.toEqual(older.bytes)
tick()
- await startTransfer(limited, root, KEY, 6, 10)
+ // 5 + 5 + 7 against 15: one copy goes. 7 is the most KEY may protect: floor(15 / 2).
+ await startTransfer(limited, root, KEY, 6, 7)
expect(exists(path.join(root, `.gfs-downloads/input-${newer.receipt.id}`))).toBe(false)
expect(exists(path.join(root, `.gfs-downloads/input-${older.receipt.id}`))).toBe(true)
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
index faf6930b9..44d32264c 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
@@ -306,16 +306,19 @@ describe('GFS download store boundaries: meta.json validation', () => {
describe('GFS download store boundaries: quota and capacity', () => {
it('Q1: retained bytes exactly at the volume budget are admitted and one byte more is refused', async () => {
- volumeOf(15)
+ // Budget 20; a caller protects at most 10, so A and B each pin half and
+ // the refused byte comes from C, which protects nothing yet.
+ volumeOf(20)
const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)
await completedCopy(limited, rootA, A, 310, 10, { owner: 'task-q1' })
const denied = await quotaCount('host', 'storage_bytes')
- const atBudget = await completedCopy(limited, rootA, A, 311, 5, { owner: 'task-q1' })
+ const atBudget = await completedCopy(limited, rootB, B, 311, 10, { owner: 'task-q1' })
expect(await quotaCount('host', 'storage_bytes')).toBe(denied)
- await expect(limited.readManagedFile(atBudget.receipt.path, A)).resolves.toEqual(atBudget.bytes)
+ await expect(limited.readManagedFile(atBudget.receipt.path, B)).resolves.toEqual(atBudget.bytes)
- await expect(startTransfer(limited, rootA, A, 312, 1)).rejects.toMatchObject({
+ const rootC = callerDirectory(hostRoot, 'caller-c')
+ await expect(startTransfer(limited, rootC, 'caller-c', 312, 1)).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
@@ -323,13 +326,21 @@ describe('GFS download store boundaries: quota and capacity', () => {
it('Q2: there is no retained-file limit: one caller keeps 65 copies and only bytes refuse', async () => {
// 65 one-byte copies: more than the removed 8-per-caller and 64-per-Host limits.
+ // A caller protects at most half the budget, so the budget is 132: A pins
+ // 65 bytes and its 66th byte stays within its 66, while B pins 60 (six
+ // 10-byte copies, the file limit) and C pins the remaining 7.
const files = 65
- volumeOf(files)
+ volumeOf(132)
const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)
const denied = await quotaCount('host', 'storage_bytes')
for (let index = 0; index < files; index += 1)
await completedCopy(limited, rootA, A, 400 + index, 1, { owner: 'task-q2' })
+ for (let index = 0; index < 6; index += 1)
+ await completedCopy(limited, rootB, B, 500 + index, 10, { owner: 'task-q2' })
+ const rootC = callerDirectory(hostRoot, 'caller-c')
+ await completedCopy(limited, rootC, 'caller-c', 506, 7, { owner: 'task-q2' })
expect((await limited.debugInventory()).byCaller.get(A)).toEqual({ files, bytes: files })
+ expect((await limited.debugInventory()).bytes).toBe(132)
expect(await quotaCount('host', 'storage_bytes')).toBe(denied)
// Every copy is pinned, so the byte budget is the only thing that can refuse.
@@ -427,8 +438,9 @@ describe('GFS download store boundaries: quota and capacity', () => {
const foreign = plantComplete(rootB, B, 329, Buffer.alloc(5, 0x38))
tick()
- // 5 + 5 + 5 + 10 against 15: both adopted copies go, whoever's directory holds them.
- await startTransfer(limited, rootA, A, 330, 10)
+ // 5 + 5 + 5 + 7 against 15: both adopted copies go, whoever's directory
+ // holds them. 7 is the most A may protect: floor(15 / 2).
+ await startTransfer(limited, rootA, A, 330, 7)
expect(exists(adopted.directory)).toBe(false)
expect(exists(foreign.directory)).toBe(false)
@@ -448,27 +460,32 @@ describe('GFS download store boundaries: quota and capacity', () => {
})
tick()
- await startTransfer(limited, rootA, A, 333, 10)
+ // 5 + 5 + 7 against 15: one copy goes. 7 is the most A may protect: floor(15 / 2).
+ await startTransfer(limited, rootA, A, 333, 7)
expect(exists(downloadDirectory(rootA, newer.receipt.id))).toBe(false)
expect(exists(downloadDirectory(rootA, older.receipt.id))).toBe(true)
})
it('Q12: when every copy is pinned admission fails with the quota code and succeeds after release', async () => {
+ // Budget 15; a caller protects at most 7. B and C each pin 7, and A's 7
+ // would bring the Host to 21: only the host budget can refuse it.
volumeOf(15)
const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)
- const pinned = await completedCopy(limited, rootA, A, 334, 10, { owner: 'task-q12' })
- const directory = downloadDirectory(rootA, pinned.receipt.id)
+ const pinned = await completedCopy(limited, rootB, B, 334, 7, { owner: 'task-q12' })
+ const rootC = callerDirectory(hostRoot, 'caller-c')
+ await completedCopy(limited, rootC, 'caller-c', 336, 7, { owner: 'task-q12' })
+ const directory = downloadDirectory(rootB, pinned.receipt.id)
const denied = await quotaCount('host', 'storage_bytes')
- await expect(startTransfer(limited, rootA, A, 335, 10)).rejects.toMatchObject({
+ await expect(startTransfer(limited, rootA, A, 335, 7)).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
expect(exists(directory)).toBe(true)
- await limited.releaseReceiptOwner('task-q12', A)
- await expect(startTransfer(limited, rootA, A, 335, 10)).resolves.toBeDefined()
+ await limited.releaseReceiptOwner('task-q12', B)
+ await expect(startTransfer(limited, rootA, A, 335, 7)).resolves.toBeDefined()
expect(exists(directory)).toBe(false)
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
})
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts
index f19dc18fc..15b530d34 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts
@@ -98,15 +98,20 @@ afterEach(async () => {
describe('GFS download store budget: share of the workspace volume', () => {
it('BUD-1: the default 85% budget admits exactly up to floor(total * 85 / 100) bytes', async () => {
- // 101 bytes of volume: 85.85 floors to 85.
+ // 101 bytes of volume: 85.85 floors to 85. A caller may protect at most
+ // floor(85 / 2) = 42 bytes, so the pinned 85 are spread over three callers:
+ // A 40 + B 40 + C 5. The refused 1-byte request leaves A at 41, under its half.
volumeOf(101n)
const store = await openStore()
- await completedCopy(store, rootA, A, 1, 80, { owner: 'task-a' })
+ const C = 'caller-c'
+ const rootC = callerDirectory(hostRoot, C)
+ await completedCopy(store, rootA, A, 1, 40, { owner: 'task-a' })
+ await completedCopy(store, rootB, B, 4, 40, { owner: 'task-b' })
const denied = await quotaCount('host', 'storage_bytes')
- const atBudget = await completedCopy(store, rootA, A, 2, 5, { owner: 'task-a' })
+ const atBudget = await completedCopy(store, rootC, C, 2, 5, { owner: 'task-c' })
expect(await quotaCount('host', 'storage_bytes')).toBe(denied)
- await expect(store.readManagedFile(atBudget.receipt.path, A)).resolves.toEqual(atBudget.bytes)
+ await expect(store.readManagedFile(atBudget.receipt.path, C)).resolves.toEqual(atBudget.bytes)
await expect(startTransfer(store, rootA, A, 3, 1)).rejects.toMatchObject({
code: 'host_quota_exceeded',
@@ -126,9 +131,16 @@ describe('GFS download store budget: share of the workspace volume', () => {
const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: percent })
const denied = await quotaCount('host', 'storage_bytes')
- await completedCopy(store, rootA, A, 10, budget, { owner: 'task-a' })
+ // A caller may protect at most half the budget, so the pinned budget is
+ // two halves held by A and B; the refused byte comes from a third caller
+ // that protects nothing yet, so only the host budget can refuse it.
+ await completedCopy(store, rootA, A, 10, budget / 2, { owner: 'task-a' })
+ await completedCopy(store, rootB, B, 12, budget - budget / 2, { owner: 'task-b' })
expect(await quotaCount('host', 'storage_bytes')).toBe(denied)
- await expect(startTransfer(store, rootB, B, 11, 1)).rejects.toMatchObject({
+ const C = 'caller-c'
+ await expect(
+ startTransfer(store, callerDirectory(hostRoot, C), C, 11, 1)
+ ).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
@@ -136,18 +148,25 @@ describe('GFS download store budget: share of the workspace volume', () => {
)
it('BUD-3: a volume resized between two admissions is used by the second, without restart', async () => {
+ // Budget 20, so each caller protects at most 10: 15 bytes pinned by A (10)
+ // and C (5), then B asks for 10. 15 + 10 > 20 is refused by the host budget;
+ // after the resize 15 + 10 fits in 25, and B's 10 stays under floor(25 / 2).
volumeOf(20n)
const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '100' })
await completedCopy(store, rootA, A, 20, 10, { owner: 'task-a' })
+ const C = 'caller-c'
+ await completedCopy(store, callerDirectory(hostRoot, C), C, 22, 5, { owner: 'task-c' })
+ const denied = await quotaCount('host', 'storage_bytes')
- await expect(startTransfer(store, rootB, B, 21, 15)).rejects.toMatchObject({
+ await expect(startTransfer(store, rootB, B, 21, 10)).rejects.toMatchObject({
code: 'host_quota_exceeded',
})
+ expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
volumeOf(25n)
statfsBoundary.mockClear()
- const admitted = await startTransfer(store, rootB, B, 21, 15)
- expect(admitted.transfer.sizeBytes).toBe(15)
+ const admitted = await startTransfer(store, rootB, B, 21, 10)
+ expect(admitted.transfer.sizeBytes).toBe(10)
// Witness: the new size came from a statfs taken by this admission.
expect(statfsBoundary).toHaveBeenCalled()
})
@@ -181,7 +200,7 @@ describe('GFS download store budget: share of the workspace volume', () => {
})
})
-describe('GFS download store budget: no per-caller limit', () => {
+describe('GFS download store budget: no per-caller storage or file-count limit', () => {
it('BUD-6: one caller fills the whole budget with 18 files of 16 MiB and another caller evicts its least recently used copy', async () => {
const size = 16 * MIB
const files = 18
@@ -225,7 +244,9 @@ describe('GFS download store budget: no per-caller limit', () => {
const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '100' })
const pinned = await completedCopy(store, rootA, A, 40, 10, { owner: 'task-a' })
tick()
- const unpinned = await completedCopy(store, rootA, A, 41, 10)
+ // A protects at most floor(30 / 2) = 15: the pinned 10 plus this 5-byte
+ // reservation while it is written.
+ const unpinned = await completedCopy(store, rootA, A, 41, 5)
tick()
const bytes = Buffer.alloc(10, 0x42)
const plantedId = '00000000-0000-4000-8000-000000000042'
@@ -236,13 +257,14 @@ describe('GFS download store budget: no per-caller limit', () => {
})
tick()
- // 30 retained + 10 requested against 30: one copy goes, and it is the adopted one.
+ // 25 retained + 10 requested against 30: one copy goes, and it is the adopted one.
await startTransfer(store, rootB, B, 42, 10)
expect(exists(planted.directory)).toBe(false)
expect(exists(downloadDirectory(rootA, unpinned.receipt.id))).toBe(true)
expect(exists(downloadDirectory(rootA, pinned.receipt.id))).toBe(true)
- // Next: the unpinned published copy goes; the pinned one, older, never does.
+ // Next, 15 retained + B's 10 reserved + 10 requested against 30: the
+ // unpinned published copy goes; the pinned one, older, never does.
const second = await startTransfer(
store,
callerDirectory(hostRoot, 'caller-c'),
@@ -261,16 +283,34 @@ describe('GFS download store budget: no per-caller limit', () => {
try {
const own = callerDirectory(host, A)
const other = callerDirectory(host, `caller-${'b'.repeat(12)}`)
+ const third = callerDirectory(host, `caller-${'c'.repeat(12)}`)
const store = await openStore(host)
- const filler = fillOwnRoot ? own : other
- const fillerKey = path.basename(filler)
- await completedCopy(store, filler, fillerKey, 50, 80, { owner: 'task-fill' })
+ // 80 pinned bytes either way; no caller protects more than floor(85 / 2) = 42.
+ const fillers: Array<[string, number]> = fillOwnRoot
+ ? [
+ [own, 30],
+ [other, 40],
+ [third, 10],
+ ]
+ : [
+ [other, 40],
+ [third, 40],
+ ]
+ for (const [index, [filler, sizeBytes]] of fillers.entries())
+ await completedCopy(store, filler, path.basename(filler), 50 + index * 2, sizeBytes, {
+ owner: 'task-fill',
+ })
+ const denied = await quotaCount('host', 'storage_bytes')
const error = await startTransfer(store, own, A, 51, 10).then(
() => undefined,
(rejection: unknown) => rejection
)
- // Witness: the refusal was the budget; releasing the pin admits the same request.
- await store.releaseReceiptOwner('task-fill', fillerKey)
+ // The refusal came from the host budget: A stays under its own half
+ // (at most 30 + 10 = 40 of 42).
+ expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)
+ // Witness: the refusal was the budget; releasing the pins admits the same request.
+ for (const [filler] of fillers)
+ await store.releaseReceiptOwner('task-fill', path.basename(filler))
await expect(startTransfer(store, own, A, 51, 10)).resolves.toBeDefined()
return error
} finally {
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts
new file mode 100644
index 000000000..38d2ba840
--- /dev/null
+++ b/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts
@@ -0,0 +1,207 @@
+/**
+ * R3-F1 (#1028, Addendum 9): eviction never reclaims a pinned copy or an
+ * active reservation, so one caller's protected bytes — pins of its retention
+ * owners plus its reservations, each id once — may reach at most half the
+ * budget. Unpinned copies still use the whole budget. Ported from the final
+ * review's SOL-R3-PINS repro. A 100-byte volume at the default 85% gives a
+ * budget of 85 and a per-caller protected cap of floor(85 / 2) = 42.
+ */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import * as syncFs from 'node:fs'
+import * as fs from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import * as path from 'node:path'
+import {
+ callerDirectory,
+ completedCopy,
+ downloadDirectory,
+ exists,
+ quotaCount,
+ sourceFor,
+ startTransfer,
+} from '../__tests__/fixtures/gfsStoreTestKit'
+import { GfsDownloadStore, GfsDownloadStoreError } from './gfsDownloadStore'
+
+const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))
+vi.mock('node:fs/promises', async original => ({
+ ...(await original<typeof fs>()),
+ statfs: statfsBoundary,
+}))
+
+const A = 'caller-a'
+const B = 'caller-b'
+const C = 'caller-c'
+
+let nativeFs: typeof fs
+let hostRoot: string
+let rootA: string
+let rootB: string
+let rootC: string
+const stores: GfsDownloadStore[] = []
+
+async function openStore(): Promise<GfsDownloadStore> {
+ const opened = new GfsDownloadStore(hostRoot)
+ stores.push(opened)
+ await opened.initialize()
+ return opened
+}
+
+/** `count` published 5-byte copies, pinned by `owner` when given. */
+async function copies(
+ store: GfsDownloadStore,
+ root: string,
+ caller: string,
+ first: number,
+ count: number,
+ owner?: string
+) {
+ const receipts = []
+ for (let index = first; index < first + count; index += 1)
+ receipts.push(
+ (await completedCopy(store, root, caller, index, 5, owner ? { owner } : {})).receipt
+ )
+ return receipts
+}
+
+const protectedRefusals = () => quotaCount('caller', 'protected_bytes')
+const hostRefusals = () => quotaCount('host', 'storage_bytes')
+
+beforeEach(async () => {
+ nativeFs = await vi.importActual<typeof fs>('node:fs/promises')
+ vi.useFakeTimers({ toFake: ['Date'] })
+ vi.setSystemTime(new Date('2026-10-08T12:00:00.000Z'))
+ statfsBoundary.mockReset()
+ statfsBoundary.mockImplementation(async (target: string) => {
+ const real = await nativeFs.statfs(target, { bigint: true })
+ return { ...real, bsize: 1n, blocks: 100n, bavail: real.bavail * real.bsize }
+ })
+ hostRoot = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-protected-'))
+ rootA = callerDirectory(hostRoot, A)
+ rootB = callerDirectory(hostRoot, B)
+ rootC = callerDirectory(hostRoot, C)
+})
+
+afterEach(async () => {
+ vi.restoreAllMocks()
+ vi.useRealTimers()
+ for (const opened of stores.splice(0)) await opened.close(0)
+ await nativeFs.rm(hostRoot, { recursive: true, force: true })
+})
+
+describe('GFS download store: a caller protects at most half the budget (R3-F1)', () => {
+ it('SOL-R3-PINS: one caller pins up to the cap, exactly, and other callers keep admitting', async () => {
+ const store = await openStore()
+ const pinned = await copies(store, rootA, A, 0, 8, 'task-a')
+ // 40 pinned + 2 = 42, the cap exactly.
+ await completedCopy(store, rootA, A, 8, 2, { owner: 'task-a' })
+ const protectedBefore = await protectedRefusals()
+ const hostBefore = await hostRefusals()
+
+ const refusal = await startTransfer(store, rootA, A, 9, 1).catch(error => error)
+ expect(refusal).toBeInstanceOf(GfsDownloadStoreError)
+ expect(refusal).toMatchObject({ code: 'host_quota_exceeded' })
+ expect(await protectedRefusals()).toBe(protectedBefore + 1)
+ expect(await hostRefusals()).toBe(hostBefore)
+
+ // The repro's denial: B and C are admitted, before and after expiry.
+ const b = await startTransfer(store, rootB, B, 10, 1)
+ await store.fail(b.transfer.id, B)
+ vi.setSystemTime(Date.now() + 8 * 24 * 60 * 60 * 1000)
+ expect((await store.cleanupExpired()).removedExpired).toBe(0)
+ const c = await startTransfer(store, rootC, C, 11, 1)
+ await store.fail(c.transfer.id, C)
+ for (const receipt of pinned) expect(exists(downloadDirectory(rootA, receipt.id))).toBe(true)
+
+ // Releasing the owner gives A its room back.
+ await store.releaseReceiptOwner('task-a', A)
+ const again = await startTransfer(store, rootA, A, 12, 1)
+ await store.fail(again.transfer.id, A)
+ })
+
+ it('a refused caller gets the same error object as a full Host budget', async () => {
+ const store = await openStore()
+ await copies(store, rootA, A, 0, 8, 'task-a')
+ await completedCopy(store, rootA, A, 8, 2, { owner: 'task-a' })
+ const capRefusal = await startTransfer(store, rootA, A, 9, 1).catch(error => error)
+
+ // B pins 40 beside A's 42: 82 of 85, so C's 4 bytes exceed the Host
+ // budget while C protects nothing.
+ await copies(store, rootB, B, 20, 8, 'task-b')
+ const hostBefore = await hostRefusals()
+ const protectedBefore = await protectedRefusals()
+ const budgetRefusal = await startTransfer(store, rootC, C, 40, 4).catch(error => error)
+ expect(await hostRefusals()).toBe(hostBefore + 1)
+ expect(await protectedRefusals()).toBe(protectedBefore)
+ expect(budgetRefusal).toBeInstanceOf(GfsDownloadStoreError)
+ expect(capRefusal).toBeInstanceOf(GfsDownloadStoreError)
+ expect({ code: capRefusal.code, message: capRefusal.message }).toEqual({
+ code: budgetRefusal.code,
+ message: budgetRefusal.message,
+ })
+ })
+
+ it('an active reservation counts, pinned or not', async () => {
+ const store = await openStore()
+ await copies(store, rootA, A, 0, 8, 'task-a')
+ const before = await protectedRefusals()
+
+ // 40 pinned + a 3-byte reservation without owner = 43.
+ await expect(startTransfer(store, rootA, A, 9, 3)).rejects.toMatchObject({
+ code: 'host_quota_exceeded',
+ })
+ expect(await protectedRefusals()).toBe(before + 1)
+ // 40 + 2 = 42 is admitted.
+ const admitted = await startTransfer(store, rootA, A, 10, 2)
+ await store.fail(admitted.transfer.id, A)
+ })
+
+ it('unpinned copies are not protected: one caller still fills the whole budget', async () => {
+ const store = await openStore()
+ await copies(store, rootA, A, 0, 16)
+ const before = await protectedRefusals()
+
+ // 80 unpinned + 5 = 85, the budget exactly, far over the 42 cap.
+ const last = await completedCopy(store, rootA, A, 16, 5)
+ expect(await protectedRefusals()).toBe(before)
+ await expect(store.readManagedFile(last.receipt.path, A)).resolves.toEqual(last.bytes)
+ })
+
+ it('pinning a reused copy counts it once, and only when it is not already protected', async () => {
+ const store = await openStore()
+ // The unpinned copy first: its 20-byte reservation beside 30 pinned
+ // bytes would itself exceed the cap.
+ await completedCopy(store, rootA, A, 1, 20)
+ const held = await completedCopy(store, rootA, A, 0, 30, { owner: 'task-a' })
+ const before = await protectedRefusals()
+
+ // A copy already pinned by A adds nothing: 30 stays 30.
+ await expect(
+ store.reusableReceipt(A, sourceFor(0), 30, { retentionOwnerId: 'task-b' })
+ ).resolves.toMatchObject({ id: held.receipt.id })
+ expect(await protectedRefusals()).toBe(before)
+
+ // The unpinned 20-byte copy would make 50: refused, and still reusable
+ // without an owner.
+ await expect(
+ store.reusableReceipt(A, sourceFor(1), 20, { retentionOwnerId: 'task-b' })
+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })
+ expect(await protectedRefusals()).toBe(before + 1)
+ await expect(store.reusableReceipt(A, sourceFor(1), 20)).resolves.toMatchObject({
+ sizeBytes: 20,
+ })
+ })
+
+ it('a file larger than the cap is refused before any eviction', async () => {
+ const store = await openStore()
+ const other = await completedCopy(store, rootB, B, 0, 5)
+ const before = await protectedRefusals()
+
+ await expect(startTransfer(store, rootA, A, 1, 43)).rejects.toMatchObject({
+ code: 'host_quota_exceeded',
+ })
+ expect(await protectedRefusals()).toBe(before + 1)
+ expect(exists(downloadDirectory(rootB, other.receipt.id))).toBe(true)
+ const admitted = await startTransfer(store, rootA, A, 2, 42)
+ await store.fail(admitted.transfer.id, A)
+ })
+})
diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.json
index 6aff524b3..982115953 100644
--- a/scripts/ci/pinned-vitest-cases/mcp-host.json
+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json
@@ -809,15 +809,15 @@
},
{
"suite": "gfsDownloadStoreBudget.test.ts",
- "fullName": "GFS download store budget: no per-caller limit BUD-6: one caller fills the whole budget with 18 files of 16 MiB and another caller evicts its least recently used copy"
+ "fullName": "GFS download store budget: no per-caller storage or file-count limit BUD-6: one caller fills the whole budget with 18 files of 16 MiB and another caller evicts its least recently used copy"
},
{
"suite": "gfsDownloadStoreBudget.test.ts",
- "fullName": "GFS download store budget: no per-caller limit BUD-7: another caller's admission never evicts a pinned copy and takes an adopted one first"
+ "fullName": "GFS download store budget: no per-caller storage or file-count limit BUD-7: another caller's admission never evicts a pinned copy and takes an adopted one first"
},
{
"suite": "gfsDownloadStoreBudget.test.ts",
- "fullName": "GFS download store budget: no per-caller limit BUD-8: the quota denial is identical whoever fills the budget"
+ "fullName": "GFS download store budget: no per-caller storage or file-count limit BUD-8: the quota denial is identical whoever fills the budget"
},
{
"suite": "gfsDownloadStoreBudget.test.ts",
@@ -902,5 +902,29 @@
{
"suite": "gfsDownloadStoreHeldCharges.test.ts",
"fullName": "GFS download store: held duplicate charges survive transient failures (R2-F2) a carried directory that is now indexed is charged once, not twice"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) SOL-R3-PINS: one caller pins up to the cap, exactly, and other callers keep admitting"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) a refused caller gets the same error object as a full Host budget"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) an active reservation counts, pinned or not"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) unpinned copies are not protected: one caller still fills the whole budget"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) pinning a reused copy counts it once, and only when it is not already protected"
+ },
+ {
+ "suite": "gfsDownloadStoreProtected.test.ts",
+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) a file larger than the cap is refused before any eviction"
}
]