Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): key the GFS download store by the channel-namespaced caller key
Alfredo Lopez3092ba50292c
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex f9164e0e4..3d058f119 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.527",+ "version": "0.1.529","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.527",+ "version": "0.1.529","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex bc2473f04..a1d6c7858 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.527",+ "version": "0.1.529","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts b/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.tsindex 00976c4e0..d3e529f1e 100644--- a/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts+++ b/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts@@ -1,8 +1,11 @@import { describe, expect, it } from 'vitest'import type { ApprovalConfig } from '../../core/extensions/approvalTypes'import type { GfsDownloadStore } from '../../internalTools/gfsDownloadStore'+import { deriveUserKeyFromSource } from '../../workspace/userKey'import {+ GFS_SYSTEM_CALLER_IDENTITY,gfsManagedWorkspaceExecution,+ gfsStoreCallerIdentity,gfsWorkspaceExecutionEnabled,} from '../gfsExecutionCapability'@@ -120,3 +123,20 @@ describe('store-associated executable workspace', () => {expect(managed?.callerWorkspacePath).toBeUndefined()})})++describe('gfsStoreCallerIdentity', () => {+ it('keys the store by the channel-namespaced caller-root key, never the raw sender', () => {+ const slack = { sender: 'same-sender', channelType: 'slack' as const }+ const rpc = { sender: 'same-sender', channelType: 'rpc' as const }+ expect(gfsStoreCallerIdentity(slack)).not.toBe(gfsStoreCallerIdentity(rpc))+ expect(gfsStoreCallerIdentity(slack)).not.toBe('same-sender')+ // Equal to the key that names the caller root `users/<key>`.+ expect(gfsStoreCallerIdentity(slack)).toBe(deriveUserKeyFromSource(slack))+ expect(gfsStoreCallerIdentity(rpc)).toBe(deriveUserKeyFromSource(rpc))+ })++ it('gives a task without a source message the system key of its caller root', () => {+ expect(gfsStoreCallerIdentity(undefined)).toBe(GFS_SYSTEM_CALLER_IDENTITY)+ expect(deriveUserKeyFromSource(undefined)).toBe(GFS_SYSTEM_CALLER_IDENTITY)+ })+})diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.tsindex 9937678b9..485a89118 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts@@ -3,7 +3,7 @@ import { createHash, randomUUID } from 'node:crypto'import * as fs from 'node:fs/promises'import { createRequire } from 'node:module'import { tmpdir } from 'node:os'-import { join } from 'node:path'+import { basename, join } from 'node:path'import { config as appConfig } from '../../config'import { ConversationManager } from '../../core/conversation/conversation'import {@@ -19,6 +19,7 @@ import type { SingleTurnProvider } from '../../llm/types'import type { Task } from '../../queue/types'import { resolveVisualDeliveryLimits } from '../../visualInput/deliveryLimits'import { ScopedWorkspaceProvider } from '../../workspace/scopedWorkspace'+import { deriveUserKeyFromSource } from '../../workspace/userKey'import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../taskExecutor'const { clientFactory } = vi.hoisted(() => ({ clientFactory: vi.fn() }))@@ -201,7 +202,10 @@ it('keeps the exact workspace receipt through same-batch image suspension, SQLitresponseCallback: vi.fn(async () => undefined),}const callerRoot = new ScopedWorkspaceProvider(root).forSource(task.sourceMessage).userRootPath- taskOwner = { id: task.id, caller: task.sourceMessage!.sender }+ // The store keys the caller like its root: the channel-namespaced key, never the raw sender.+ const storeCaller = deriveUserKeyFromSource(task.sourceMessage)+ expect(basename(callerRoot)).toBe(storeCaller)+ taskOwner = { id: task.id, caller: storeCaller }const lifecycle = new TaskLifecycle()lifecycle.register(task)const onFail = vi.fn()@@ -305,6 +309,6 @@ it('keeps the exact workspace receipt through same-batch image suspension, SQLitexpect(proof).toBeDefined()expect(JSON.parse(proof!)).toEqual({ bytes: bytes.byteLength, sha256: digest })expect(contentRequests).toBe(1)- expect(releaseOwner).toHaveBeenCalledExactlyOnceWith(task.id, task.sourceMessage!.sender)+ expect(releaseOwner).toHaveBeenCalledExactlyOnceWith(task.id, storeCaller)expect(JSON.stringify(requests)).not.toContain(bytes.toString('base64'))})diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.tsindex 95698a5e7..74548393f 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts@@ -14,8 +14,12 @@ import { GfsDownloadStore } from '../../internalTools/gfsDownloadStore'import { TaskLifecycle } from '../../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../../llm/types'import type { Task } from '../../queue/types'+import { deriveUserKey } from '../../workspace/userKey'import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../taskExecutor'+/** The store keys the caller like its root: the channel-namespaced key, never the raw sender. */+const UNIT_CALLER_STORE_KEY = deriveUserKey('unit-caller', 'rpc')+const { clientFactory } = vi.hoisted(() => ({ clientFactory: vi.fn() }))// Only GFSC's external HTTP/token-file boundary is doubled. The real client,// store, registry, approval gate, TaskExecutor and shell all execute.@@ -93,7 +97,9 @@ async function scenario() {const root = await fs.mkdtemp(join(tmpdir(), 'gfs-preparation-task-'))roots.push(root)- const callerRoot = join(root, 'users', 'unit-caller')+ // The same `users/<key>` the Agent binds for an rpc sender: the store refuses+ // an identity that is not its caller root's key.+ const callerRoot = join(root, 'users', deriveUserKey(options.sender ?? 'unit-caller', 'rpc'))await fs.mkdir(callerRoot, { recursive: true, mode: 0o700 })const store = new GfsDownloadStore(root)await store.initialize()@@ -392,7 +398,7 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',expect(test.createTransfer).toHaveBeenCalledWith(expect.objectContaining({ retentionOwnerId: test.task.id }))- expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, UNIT_CALLER_STORE_KEY)})it('keeps graceful-shutdown completion pending until the real owner release settles', async () => {@@ -530,7 +536,7 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',expect(test.executor.executorState).toBe('waiting_approval')await test.executor.deny()- expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, UNIT_CALLER_STORE_KEY)expect(test.onApprovalNeeded).toHaveBeenCalledExactlyOnceWith(test.executor.pendingApproval?.request_id ?? expect.any(String),test.task.id,@@ -548,7 +554,10 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',test.executor.abort()await test.executor.waitForCompletion()- expect(test.releaseReceiptOwner).toHaveBeenCalledExactlyOnceWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledExactlyOnceWith(+ test.task.id,+ UNIT_CALLER_STORE_KEY+ )expect(test.contentRequests).toBe(1)expect(test.lifecycle.getStatus(test.task.id)).toBe('cancelled')})@@ -574,7 +583,7 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',expect(test.contentRequests).toBe(1)expect(test.requests).toEqual([])expect((await test.store.debugInventory()).files).toBe(0)- expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, UNIT_CALLER_STORE_KEY)})it('joins a cancelled large clerum__gfs_read producer before terminal release', async () => {@@ -591,7 +600,7 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',expect(test.contentRequests).toBe(1)expect(test.requests).toHaveLength(1)expect((await test.store.debugInventory()).files).toBe(0)- expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, UNIT_CALLER_STORE_KEY)})it.each([@@ -673,7 +682,7 @@ describe('TaskExecutor prepares admitted GFS files before its first model call',expect(test.requests).toEqual([])expect((await test.store.debugInventory()).files).toBe(0)- expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, 'unit-caller')+ expect(test.releaseReceiptOwner).toHaveBeenCalledWith(test.task.id, UNIT_CALLER_STORE_KEY)})it.each([diff --git a/mcp-host/src/agent/gfsExecutionCapability.ts b/mcp-host/src/agent/gfsExecutionCapability.tsindex c7aee1f07..acb44a5ac 100644--- a/mcp-host/src/agent/gfsExecutionCapability.ts+++ b/mcp-host/src/agent/gfsExecutionCapability.ts@@ -1,6 +1,8 @@import type { ApprovalConfig } from '../core/extensions/approvalTypes'import type { GfsDownloadStore } from '../internalTools/gfsDownloadStore'import type { TaskSource } from '../queue/types'+import type { IncomingMessage } from '../server/types'+import { deriveUserKeyFromSource } from '../workspace/userKey'export interface GfsManagedWorkspaceExecution {/** Trusted task/caller identity for store admission; system tasks use the existing `_system` namespace. */@@ -15,6 +17,20 @@ export interface GfsManagedWorkspaceExecution {export const GFS_SYSTEM_CALLER_IDENTITY = '_system'+/**+ * The download store's caller identity for a task: the channel-namespaced key+ * that also names the caller root `users/<key>`. The raw sender is not unique+ * across channels, so keying the store by it would let two humans with the+ * same sender on different channels share reuse, managed reads and pins while+ * their copies live in different roots. Tasks with no source message get the+ * `_system` key, the same as their caller root.+ */+export function gfsStoreCallerIdentity(+ sourceMessage?: Pick<IncomingMessage, 'sender' | 'channelType'> | null+): string {+ return deriveUserKeyFromSource(sourceMessage)+}+export interface GfsExecutionCapabilityInput {approvalEnabled: booleansource: TaskSourcediff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.tsindex 81e1c945e..f32525896 100644--- a/mcp-host/src/agent/taskExecutor.ts+++ b/mcp-host/src/agent/taskExecutor.ts@@ -132,8 +132,8 @@ import { attachmentContextWindow } from './attachmentContextWindow'import type { CronScheduler } from './cronScheduler'import { referencedFilesForTurnContext } from './fileReferenceResolver'import {- GFS_SYSTEM_CALLER_IDENTITY,gfsManagedWorkspaceExecution,+ gfsStoreCallerIdentity,gfsWorkspaceExecutionEnabled,} from './gfsExecutionCapability'import { hasLargeAvailableGfsReferences, prepareGfsFiles } from './gfsFilePreparation'@@ -1079,9 +1079,9 @@ export class TaskExecutor {return Promise.resolve()if (this.gfsRetentionOwnerSettlement) return this.gfsRetentionOwnerSettlementthis.gfsRetentionOwnerSettlement = (async () => {- const callerIdentity = this.task.sourceMessage?.sender ?? GFS_SYSTEM_CALLER_IDENTITY+ const callerIdentity = gfsStoreCallerIdentity(this.task.sourceMessage)const store = this.deps.gfsDownloadStore- if (!callerIdentity || !store?.isAvailable()) return+ if (!store?.isAvailable()) returntry {await store.releaseReceiptOwner(this.taskId, callerIdentity)} catch (error) {@@ -2228,7 +2228,8 @@ export class TaskExecutor {// #1003 — native-tool presentation is host-wide and provider-independent:// it never depends on the MCP presentation resolved above.const nativeAuto = appConfig.nativeToolPresentation === 'auto'- const gfsCallerIdentity = this.task.sourceMessage?.sender ?? GFS_SYSTEM_CALLER_IDENTITY+ // The store is keyed like the caller root, never by the raw sender.+ const gfsCallerIdentity = gfsStoreCallerIdentity(this.task.sourceMessage)const gfsWorkspace = gfsManagedWorkspaceExecution({approvalEnabled: appConfig.enableApproval,source: this.task.source,diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.tsindex 70ba1da93..ea0a60627 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts@@ -17,6 +17,7 @@ const GFS_DOWNLOAD_STORE_LOG_COMPONENT = 'GfsDownloadStore'/** Caller-root directory that holds every download of that caller. */const DOWNLOADS_DIRECTORY = '.gfs-downloads'+const USERS_DIRECTORY = 'users'const META_FILE = 'meta.json'const SOURCE_FILE = 'source'const PARTIAL_FILE = 'source.partial'@@ -420,6 +421,11 @@ export class GfsDownloadStore {throw new RangeError('GFS download expiry must be within the retention window')if (input.retentionOwnerId !== undefined) this.assertReceiptOwner(input.retentionOwnerId)const callerRoot = await this.validateCallerRoot(input.callerWorkspacePath)+ // The identity is the caller root's own key, so every later identity check+ // (reuse, managed reads, pins, active counts) is bound to this root. A raw+ // sender is not unique across channels and is refused here.+ if (path.basename(callerRoot) !== input.callerIdentity)+ throw new GfsDownloadStoreError('caller_mismatch')const id = randomUUID()const downloadsRoot = path.join(callerRoot, DOWNLOADS_DIRECTORY)const directory = path.join(downloadsRoot, `input-${id}`)@@ -1024,7 +1030,7 @@ export class GfsDownloadStore {totals?: SweepTotals): Promise<Array<{ id: string; directory: string }>> {const found: Array<{ id: string; directory: string }> = []- const usersRoot = path.join(this.hostRoot, 'users')+ const usersRoot = path.join(this.hostRoot, USERS_DIRECTORY)let users: import('node:fs').Dirent[]try {const info = await fs.lstat(usersRoot)@@ -1481,7 +1487,12 @@ export class GfsDownloadStore {const real = await fs.realpath(lexical).catch(() => {throw new GfsDownloadStoreError('workspace_unavailable')})- if (real !== expected || !isWithinDirectory(real, await fs.realpath(this.hostRoot)))+ // Exactly `<host>/users/<key>`: never the Host root, `users/` itself, or a+ // directory nested inside a caller root.+ if (+ real !== expected ||+ path.dirname(real) !== path.join(await fs.realpath(this.hostRoot), USERS_DIRECTORY)+ )throw new GfsDownloadStoreError('workspace_unavailable')const info = await fs.lstat(real)if (!info.isDirectory() || info.isSymbolicLink())diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.tsnew file mode 100644index 000000000..be0635e7f--- /dev/null+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts@@ -0,0 +1,166 @@+/**+ * Adversarial round of the GFS download store: caller identity across+ * channels, transient I/O that is not corruption, stranded trash, eviction and+ * undo failures, and the retirement/expiry/recency edge cases. Faults are+ * injected through a pass-through `node:fs/promises` seam; every case runs on+ * a real temporary directory.+ */+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, sourceFor } from '../__tests__/fixtures/gfsStoreTestKit'+import { deriveUserKey } from '../workspace/userKey'+import { GfsDownloadStore } from './gfsDownloadStore'++type FsOperation = 'open' | 'lstat' | 'rename' | 'rm' | 'readdir'+/** A fault returns the errno to throw for this call, or undefined to pass through. */+type Fault = (target: string) => string | undefined++const { faults } = vi.hoisted(() => ({+ faults: new Map<string, (target: string) => string | undefined>(),+}))++function errnoError(code: string): NodeJS.ErrnoException {+ const error = new Error(`${code}: injected`) as NodeJS.ErrnoException+ error.code = code+ return error+}++vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const wrap =+ <F extends (...args: never[]) => unknown>(operation: string, real: F) =>+ (...args: Parameters<F>) => {+ const code = faults.get(operation)?.(String(args[0]))+ if (code !== undefined) return Promise.reject(errnoError(code))+ return real(...args)+ }+ return {+ ...actual,+ open: wrap('open', actual.open),+ lstat: wrap('lstat', actual.lstat),+ rename: wrap('rename', actual.rename),+ rm: wrap('rm', actual.rm),+ readdir: wrap('readdir', actual.readdir),+ }+})++function injectFault(operation: FsOperation, fault: Fault): void {+ faults.set(operation, fault)+}++let hostRoot: string+const stores: GfsDownloadStore[] = []++async function openStore(): Promise<GfsDownloadStore> {+ const opened = new GfsDownloadStore(hostRoot)+ stores.push(opened)+ await opened.initialize()+ return opened+}++beforeEach(() => {+ faults.clear()+ hostRoot = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-adversarial-'))+})++afterEach(async () => {+ faults.clear()+ vi.restoreAllMocks()+ vi.useRealTimers()+ for (const opened of stores.splice(0)) await opened.close(0)+ await fs.rm(hostRoot, { recursive: true, force: true })+})++describe('GFS download store adversarial round: caller identity (ADV-1)', () => {+ const SENDER = 'same-sender'+ const KEY_A = deriveUserKey(SENDER, 'channel-a')+ const KEY_B = deriveUserKey(SENDER, 'channel-b')++ it('two channels with the same sender never see each other’s copy', async () => {+ expect(KEY_A).not.toBe(KEY_B)+ const store = await openStore()+ const rootA = callerDirectory(hostRoot, KEY_A)+ const rootB = callerDirectory(hostRoot, KEY_B)+ const published = await completedCopy(store, rootA, KEY_A, 1, 64)++ // Witness: the owner's reuse and read hit the published copy.+ await expect(store.reusableReceipt(KEY_A, sourceFor(1), 64)).resolves.toMatchObject({+ id: published.receipt.id,+ })+ await expect(store.readManagedFile(published.receipt.path, KEY_A)).resolves.toEqual(+ published.bytes+ )++ // The other channel's caller: no reuse, and the foreign id reads exactly+ // like an id that never existed.+ await expect(store.reusableReceipt(KEY_B, sourceFor(1), 64)).resolves.toBeUndefined()+ const missing = `.gfs-downloads/input-${'0'.repeat(8)}-0000-4000-8000-${'0'.repeat(12)}/source`+ const foreign = await store.readManagedFile(published.receipt.path, KEY_B).catch(e => e)+ const nonexistent = await store.readManagedFile(missing, KEY_B).catch(e => e)+ expect(foreign).toMatchObject({ code: 'download_missing' })+ expect(nonexistent).toMatchObject({ code: 'download_missing' })+ expect(String(foreign)).toBe(String(nonexistent))++ // B downloads its own copy into its own root.+ const own = await completedCopy(store, rootB, KEY_B, 1, 64)+ expect(own.receipt.id).not.toBe(published.receipt.id)+ expect(syncFs.existsSync(path.join(rootB, own.receipt.path))).toBe(true)+ })++ it('refuses an identity that is not the caller root’s key', async () => {+ const store = await openStore()+ const rootA = callerDirectory(hostRoot, KEY_A)+ const admission = {+ callerWorkspacePath: rootA,+ source: sourceFor(2),+ sizeBytes: 8,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ }+ await expect(+ store.createTransfer({ ...admission, callerIdentity: SENDER })+ ).rejects.toMatchObject({ code: 'caller_mismatch' })+ await expect(+ store.createTransfer({ ...admission, callerIdentity: KEY_B })+ ).rejects.toMatchObject({ code: 'caller_mismatch' })+ // Witness: the root's own key is admitted.+ await expect(+ store.createTransfer({ ...admission, callerIdentity: KEY_A })+ ).resolves.toMatchObject({ sizeBytes: 8 })+ })+})++describe('GFS download store adversarial round: caller root shape (F9)', () => {+ it.each([+ ['the Host root', (host: string) => host],+ ['the users directory', (host: string) => path.join(host, 'users')],+ ['a directory inside a caller root', (host: string) => path.join(host, 'users', 'k', 'nested')],+ ['a directory outside users', (host: string) => path.join(host, 'other', 'k')],+ ])('refuses %s as a caller root', async (_label, rootOf) => {+ const store = await openStore()+ const root = rootOf(hostRoot)+ syncFs.mkdirSync(root, { recursive: true, mode: 0o700 })+ await expect(+ store.createTransfer({+ callerIdentity: path.basename(root),+ callerWorkspacePath: root,+ source: sourceFor(3),+ sizeBytes: 8,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ ).rejects.toMatchObject({ code: 'workspace_unavailable' })+ // Witness: `users/<key>` itself is admitted.+ const valid = callerDirectory(hostRoot, 'k')+ await expect(+ store.createTransfer({+ callerIdentity: 'k',+ callerWorkspacePath: valid,+ source: sourceFor(3),+ sizeBytes: 8,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ ).resolves.toMatchObject({ sizeBytes: 8 })+ })+})