Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(mcp-host): tighten #1019-#1021 coverage after the test audit
Alfredo Lopezf2ad16da600f
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 7623f5d93..66063ae3d 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.514",+ "version": "0.1.515","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.514",+ "version": "0.1.515","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 33618b8df..e78313f38 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.514",+ "version": "0.1.515","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/__tests__/gfsRuntime.test.ts b/mcp-host/src/__tests__/gfsRuntime.test.tsindex ef94475f3..092698c28 100644--- a/mcp-host/src/__tests__/gfsRuntime.test.ts+++ b/mcp-host/src/__tests__/gfsRuntime.test.ts@@ -1,5 +1,7 @@import { afterEach, describe, expect, it, vi } from 'vitest'+import { createHash } from 'node:crypto'import * as fs from 'node:fs/promises'+import type { FileHandle } from 'node:fs/promises'import { tmpdir } from 'node:os'import { join } from 'node:path'import { type GfsRuntime, bootstrapGfsRuntime } from '../gfsRuntime'@@ -191,27 +193,92 @@ describe('GFS runtime bootstrap', () => {it('U9: logs an error and stops the periodic lifecycle when the store becomes unavailable after initialize', async () => {vi.useFakeTimers()+ const value = await root()const initialize = vi.spyOn(GfsDownloadStore.prototype, 'initialize')const error = vi.spyOn(logger, 'error')- let sweeps = 0- // The first sweep runs against the real, available store; a failed persist- // during it is what makes a real store unavailable (`unsafe`).- const cleanup = vi- .spyOn(GfsDownloadStore.prototype, 'cleanupExpired')- .mockImplementation(async () => {- sweeps += 1- })- const runtime = await bootstrapGfsRuntime(await root())+ const warn = vi.spyOn(logger, 'warn')+ // Real sweeps throughout: the store becomes unavailable only because a real+ // persist fails during a periodic sweep, which marks the store `unsafe`.+ const cleanup = vi.spyOn(GfsDownloadStore.prototype, 'cleanupExpired')+ const runtime = await bootstrapGfsRuntime(value)runtimes.push(runtime)await expect(initialize.mock.results[0]!.value).resolves.toBeUndefined()// initialize() sweeps once itself; the lifecycle's first sweep is the second.- for (let turn = 0; turn < 20 && sweeps < 2; turn += 1) await Promise.resolve()+ for (let turn = 0; turn < 20 && cleanup.mock.calls.length < 2; turn += 1)+ await Promise.resolve()expect(cleanup).toHaveBeenCalledTimes(2)+ await cleanup.mock.results[1]!.value+ // The settled sweep schedules the next one through promise callbacks only;+ // flush microtasks without advancing the fake clock.+ for (let turn = 0; turn < 20 && vi.getTimerCount() === 0; turn += 1) await Promise.resolve()expect(runtime.store.isAvailable()).toBe(true)expect(vi.getTimerCount()).toBe(1)- vi.spyOn(runtime.store, 'isAvailable').mockReturnValue(false)+ // A published record that expires before the next periodic sweep, so that+ // sweep must remove it and persist the released charge.+ const callerRoot = join(value, 'users', 'caller-a')+ await fs.mkdir(callerRoot, { recursive: true, mode: 0o700 })+ const transfer = await runtime.store.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ source: {+ kind: 'gfs',+ drive: 'main',+ resourceId: 'a'.repeat(32),+ gfsUri: `gfs://main/${'a'.repeat(32)}`,+ name: 'input.csv',+ version: 7,+ },+ sizeBytes: 7,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ await fs.writeFile(join(callerRoot, transfer.partialPath), 'fixture')+ const receipt = await runtime.store.publish(+ transfer.id,+ 'caller-a',+ createHash('sha256').update('fixture').digest('hex')+ )+ expect(runtime.store.debugRecord(receipt.id)?.state).toBe('completed')++ // The next ledger write fails at the filesystem layer, before its rename.+ const probe = await fs.open(join(value, '.gfs-download-store', 'ledger-v1.json'), 'r')+ const prototype = Object.getPrototypeOf(probe) as FileHandle+ await probe.close()+ const originalWriteFile = prototype.writeFile+ const failedLedgerWrites: Record<string, Record<string, unknown>>[] = []+ vi.spyOn(prototype, 'writeFile').mockImplementation(async function (+ this: FileHandle,+ ...args: Parameters<FileHandle['writeFile']>+ ) {+ const [data] = args+ if (+ failedLedgerWrites.length === 0 &&+ typeof data === 'string' &&+ data.includes('"records"')+ ) {+ failedLedgerWrites.push(JSON.parse(data))+ throw Object.assign(new Error('injected ledger write failure'), { code: 'EIO' })+ }+ return originalWriteFile.apply(this, args)+ })+await vi.advanceTimersByTimeAsync(60 * 60 * 1000)+ // Witness: the periodic sweep ran and its persist is the write that failed,+ // releasing the expired record's charge. The sweep does real I/O, so wait+ // for it to settle rather than for the fake clock.+ expect(cleanup).toHaveBeenCalledTimes(3)+ await expect(cleanup.mock.results[2]!.value).rejects.toThrow('injected ledger write failure')+ const stopLogged = () =>+ error.mock.calls.some(+ ([, message]) => typeof message === 'string' && message.includes('no longer available')+ )+ for (let turn = 0; turn < 20 && !stopLogged(); turn += 1) await Promise.resolve()+ expect(failedLedgerWrites).toHaveLength(1)+ expect(failedLedgerWrites[0]!.records).not.toHaveProperty(receipt.id)+ expect(+ warn.mock.calls.filter(([, message]) => message === 'GFS download cleanup failed')+ ).toHaveLength(1)+ expect(runtime.store.isAvailable()).toBe(false)const stopLogs = error.mock.calls.filter(([, message]) => typeof message === 'string' && message.includes('no longer available'))@@ -221,9 +288,10 @@ describe('GFS runtime bootstrap', () => {'GFS download store is no longer available; periodic cleanup stopped and managed GFS operations are disabled',],])- // Witness: the lifecycle ran its first sweep; it sweeps and schedules nothing more.- expect(cleanup).toHaveBeenCalledTimes(2)+ // The lifecycle schedules nothing more: another period passes without a sweep.expect(vi.getTimerCount()).toBe(0)+ await vi.advanceTimersByTimeAsync(60 * 60 * 1000)+ expect(cleanup).toHaveBeenCalledTimes(3)})it('closes initialized ownership even when a failed persist has made the store unavailable', async () => {diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.tsindex 62161a55b..56ce0c910 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts@@ -11,6 +11,7 @@ import { type ChatMessage, FinishReason, type ToolCall } from '../../core/types'import { GfsDownloadStore, GfsDownloadStoreError } from '../../internalTools/gfsDownloadStore'import { TaskLifecycle } from '../../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../../llm/types'+import { logger } from '../../logger'import type { Task, TaskSource } from '../../queue/types'import { resolveCallerRootBinding } from '../../workspace/callerRootBinding'import { ScopedWorkspaceProvider } from '../../workspace/scopedWorkspace'@@ -169,6 +170,7 @@ async function shellScenario({missingCallerRoot = false,calls = [{ id: 'shell-safety', name: 'shell_exec', arguments: { command: 'pwd' } }],turnObserver,+ dynamicEnv = {},}: {source: TaskSourceapprovalEnabled?: boolean@@ -179,6 +181,8 @@ async function shellScenario({missingCallerRoot?: booleancalls?: ToolCall[]turnObserver?: TurnObserver+ /** Operator-managed env the shell receives through `dynamicEnvProvider`. */+ dynamicEnv?: Record<string, string>}) {appConfig.enableApproval = approvalEnabledconst fixture = legacyLease@@ -273,7 +277,7 @@ async function shellScenario({onApprovalNeeded,onComplete: vi.fn(),onFail,- dynamicEnvProvider: () => ({}),+ dynamicEnvProvider: () => ({ ...dynamicEnv }),}return {executor: new TaskExecutor(task, deps),@@ -365,6 +369,92 @@ it('X2: a store at corrupt_store_ledger keeps shell and GFS reads but withdrawsexpect(JSON.stringify(scenario.providerCalls.at(-1))).toContain(scenario.callerWorkspace)})+it('X2b: a store at corrupt_store_ledger still answers admitted inline reads, accessible and resolve', async () => {+ const smallResourceId = 'd'.repeat(32)+ const smallSource = {+ kind: 'gfs' as const,+ drive: 'main',+ resourceId: smallResourceId,+ gfsUri: `gfs://main/${smallResourceId}`,+ name: 'small.txt',+ version: 3,+ }+ const smallText = 'inline-admitted-sentinel region=north total=42'+ const accessibleUri = 'gfs://main/accessible-sentinel'+ const resolvedPath = '/reports/resolve-sentinel'+ gfsClient.readMetadata.mockImplementation(async () => ({+ source: structuredClone(smallSource),+ size: Buffer.byteLength(smallText),+ }))+ const releaseReservation = vi.fn()+ gfsClient.read.mockImplementation(async () => ({+ source: structuredClone(smallSource),+ bytes: Buffer.from(smallText, 'utf8'),+ reservation: { release: releaseReservation },+ }))+ gfsClient.accessible.mockImplementation(async () => ({+ items: [{ gfsUri: accessibleUri, permissions: ['read'] }],+ }))+ gfsClient.resolve.mockImplementation(async () => ({+ gfsUri: accessibleUri,+ pathCache: resolvedPath,+ }))+ const scenario = await shellScenario({+ source: 'cron',+ calls: [+ {+ id: 'gfs-read-small',+ name: 'clerum__gfs_read',+ arguments: { drive: 'main', resourceId: smallResourceId },+ },+ { id: 'gfs-accessible', name: 'clerum__gfs_accessible', arguments: { drive: 'main' } },+ { id: 'gfs-resolve', name: 'clerum__gfs_resolve', arguments: { uri: accessibleUri } },+ ],+ })+ // Same fixture as X2: the store refused to initialize with corrupt_store_ledger.+ expect(scenario.storeInitError).toBeInstanceOf(GfsDownloadStoreError)+ expect(scenario.storeInitError).toMatchObject({ code: 'corrupt_store_ledger' })+ expect(scenario.store.isAvailable()).toBe(false)+ await scenario.executor.run()++ expect(scenario.onFail).not.toHaveBeenCalled()+ const firstTools = scenario.advertisedTools[0]!+ expect(firstTools).toEqual(+ expect.arrayContaining(['clerum__gfs_read', 'clerum__gfs_accessible', 'clerum__gfs_resolve'])+ )+ expect(firstTools).not.toContain('clerum__gfs_download')+ // Four model turns: three tool calls and the closing answer.+ expect(scenario.providerCalls).toHaveLength(4)++ const toolResult = (turn: number, toolCallId: string) => {+ const message = scenario.providerCalls[turn]!.find(+ m => m.role === 'tool' && m.tool_call_id === toolCallId+ )+ if (!message) throw new Error(`No tool result for ${toolCallId} in turn ${turn}`)+ return typeof message.content === 'string' ? message.content : JSON.stringify(message.content)+ }++ // (a) The inline read went through metadata admission, then the in-memory+ // read, and its exact text reached the model; delivery was never attempted.+ expect(gfsClient.readMetadata).toHaveBeenCalledOnce()+ expect(gfsClient.readMetadata.mock.calls[0]![0]).toMatchObject({ resourceId: smallResourceId })+ expect(gfsClient.read).toHaveBeenCalledOnce()+ expect(gfsClient.read.mock.calls[0]![0]).toMatchObject({ resourceId: smallResourceId })+ expect(releaseReservation).toHaveBeenCalledOnce()+ expect(gfsClient.download).not.toHaveBeenCalled()+ const readResult = toolResult(1, 'gfs-read-small')+ expect(readResult).toContain(smallText)+ expect(readResult).not.toContain('workspace_delivery_unavailable')++ // (b) accessible and resolve return their normal client result.+ expect(gfsClient.accessible).toHaveBeenCalledOnce()+ expect(gfsClient.accessible.mock.calls[0]![0]).toMatchObject({ drive: 'main' })+ expect(toolResult(2, 'gfs-accessible')).toContain(accessibleUri)+ expect(gfsClient.resolve).toHaveBeenCalledOnce()+ expect(gfsClient.resolve.mock.calls[0]![0]).toMatchObject({ uri: accessibleUri })+ expect(toolResult(3, 'gfs-resolve')).toContain(resolvedPath)+})+it.each([['cron source', { source: 'cron' as TaskSource }],['internal source', { source: 'internal' as TaskSource, approvalEnabled: false }],@@ -606,3 +696,73 @@ it('X3-TE: a shell dispatch inside TaskExecutor makes zero GFS download store caexpect(dispatch.windows[0]!.storeCalls).toEqual([])expect(executeWindows[0]!.storeCalls).toEqual([])})++it('X5-env: a NUL secret in the dynamic shell environment fails to start without exposing its value (#1020)', async () => {+ const secretTail = 's3cr3t-marker-1028'+ const secretFragments = ['s3cr3t', 'marker-1028', secretTail]+ const envCall: ToolCall = {+ id: 'shell-env-nul',+ name: 'shell_exec',+ arguments: { command: 'printf "%s" "$CLERUM_TEST_SECRET"' },+ }+ const logCalls: unknown[][] = []+ for (const level of ['error', 'warn', 'info', 'debug'] as const) {+ const spy = vi.spyOn(logger, level)+ installedSpies.push(spy)+ logCalls.push(spy.mock.calls as unknown as unknown[])+ }+ const loggedText = () =>+ JSON.stringify(logCalls.flat(), (_key, value) =>+ value instanceof Error ? { message: value.message, stack: value.stack } : value+ )+ const toolResult = (providerCalls: ChatMessage[][]) => {+ const message = providerCalls[1]?.find(m => m.role === 'tool' && m.tool_call_id === envCall.id)+ if (!message) throw new Error(`No tool result for ${envCall.id} in turn 1`)+ return typeof message.content === 'string' ? message.content : JSON.stringify(message.content)+ }++ // Witness: the same dynamic variable without the NUL reaches the shell, so the+ // provider is wired and the command would print the secret if it could start.+ const clean = await shellScenario({+ source: 'cron',+ healthy: true,+ calls: [envCall],+ dynamicEnv: { CLERUM_TEST_SECRET: `a${secretTail}` },+ })+ await clean.executor.run()+ expect(clean.onFail).not.toHaveBeenCalled()+ expect(spawn).toHaveBeenCalledOnce()+ expect(toolResult(clean.providerCalls)).toContain(`a${secretTail}`)++ vi.mocked(spawn).mockClear()+ for (const calls of logCalls) calls.splice(0)+ const scenario = await shellScenario({+ source: 'cron',+ healthy: true,+ calls: [envCall],+ dynamicEnv: { CLERUM_TEST_SECRET: `a\0${secretTail}` },+ })+ await scenario.executor.run()++ expect(scenario.onFail).not.toHaveBeenCalled()+ // The model sees only the key, never the value, inside the tool-output envelope.+ const result = toolResult(scenario.providerCalls)+ expect(result).toBe(+ [+ '<tool_output name="shell_exec" sanitized="false">',+ 'Command failed to start: environment variable CLERUM_TEST_SECRET contains a NUL character',+ '</tool_output>',+ ].join('\n')+ )+ expect(result).not.toContain('Tool execution failed:')+ const modelVisible = JSON.stringify(scenario.providerCalls)+ // Witness for the log assertions: the shell logged its own start failure.+ expect(loggedText()).toContain('ENV_VALUE_CONTAINS_NUL')+ for (const fragment of secretFragments) {+ expect(result).not.toContain(fragment)+ expect(modelVisible).not.toContain(fragment)+ expect(loggedText()).not.toContain(fragment)+ }+ expect(loggedText()).not.toContain('Tool execution failed')+ expect(spawn).not.toHaveBeenCalled()+})diff --git a/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.ts b/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.tsindex eda0f04b3..cb0164317 100644--- a/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.ts+++ b/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.ts@@ -94,7 +94,6 @@ describe('managed shell_exec without the GFS download store (#1019), registry-le['exactly one hour', 3_600_000, 3_600_000],['one millisecond over one hour', 3_600_001, 3_600_001],['the default shell timeout', 1_500_000, 1_500_000],- ['a context timeout shorter than the shell timeout', 3_600_001, 60_000],])('U5: runs with an effective timeout of %s', async (_label, shellTimeout, effectiveTimeout) => {const { store, callerRoot } = await managedHost()const shell = managedRegistry(shellTimeout, store, callerRoot).get('shell_exec')@@ -110,6 +109,25 @@ describe('managed shell_exec without the GFS download store (#1019), registry-leexpect(output.content).toContain(callerRoot)})+ it('U5: a context timeout shorter than the shell timeout kills a command that outlives it', async () => {+ const { store, callerRoot } = await managedHost()+ const shell = managedRegistry(3_600_001, store, callerRoot).get('shell_exec')+ expect(shell).not.toBeNull()+ const startedAt = performance.now()+ const output = await executeWithTimeout(+ shell!,+ { command: 'printf u5-started; sleep 5' },+ { onOutput: () => undefined },+ 300+ )+ const elapsedMs = performance.now() - startedAt+ // Witness: the command really started before the context timeout ended it.+ expect(output.content).toContain('u5-started')+ expect(output.is_error).toBe(true)+ expect(output.content).toContain('[Command killed after 300ms timeout')+ expect(elapsedMs).toBeLessThan(4_000)+ })+it('X3: a shell run makes zero calls on the GFS download store', async () => {const { store, callerRoot } = await managedHost()const methods = storeMethodNames()diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.tsindex 29d9d9ca7..2ec44d12f 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts@@ -222,6 +222,35 @@ describe('legacy processing leases after #1019', () => {expect(reopened.debugUsage()).toMatchObject({ bytes: 7, files: 1 })})+ it('S1b: discards a non-expired foreign-session lease left by a crashed Host and admits downloads', async () => {+ await closedStore()+ const raw = await seedLegacyLeases([+ legacyLease({ expiresAt: new Date(Date.now() + 600_000).toISOString() }),+ ])+ // Witness: the seeded lease is still live by its own expiry, so the discard+ // below cannot be explained by expiry.+ const seeded = JSON.parse(raw).processingLeases[LEGACY_LEASE_ID]+ expect(Date.parse(seeded.expiresAt)).toBeGreaterThan(Date.now())+ const warn = vi.spyOn(logger, 'warn')+ const before = await counterValue(DISCARDED_COUNTER)+ const reopened = freshStore()+ await reopened.initialize()+ const onDisk = JSON.parse(await fs.readFile(ledgerFile(), 'utf8'))+ expect(onDisk.records).toEqual({})+ expect(onDisk).not.toHaveProperty('processingLeases')+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(1)+ expect(discardWarnings(warn.mock.calls)).toEqual([+ [+ { component: 'GfsDownloadStore', discarded: 1 },+ 'GFS download store discarded 1 legacy processing lease(s) at initialize',+ ],+ ])+ expect(reopened.isAvailable()).toBe(true)+ const receipt = await fixture(reopened)+ expect(reopened.debugRecord(receipt.id)?.state).toBe('completed')+ expect(reopened.debugUsage()).toMatchObject({ bytes: 7, files: 1 })+ })+it('S2: re-verifies records a discarded lease protected, reusing the intact copy and quarantining the altered one', async () => {const store = freshStore()await store.initialize()@@ -334,25 +363,40 @@ describe('legacy processing leases after #1019', () => {it('S8: a discard whose persist fails before the rename leaves the ledger byte-identical and is retried by the next initialize', async () => {await closedStore()const raw = await seedLegacyLeases([legacyLease()])+ const probe = await fs.open(ledgerFile(), 'r')+ const prototype = Object.getPrototypeOf(probe) as FileHandle+ await probe.close()+ const originalWriteFile = prototype.writeFile+ // The real persist runs; only the temporary-ledger write of the discard+ // fails, before the rename can make anything visible.+ let failedLedgerWrite: Record<string, unknown> | undefined+ vi.spyOn(prototype, 'writeFile').mockImplementation(async function (+ this: FileHandle,+ ...args: Parameters<FileHandle['writeFile']>+ ) {+ const [data] = args+ if (failedLedgerWrite === undefined && typeof data === 'string') {+ const written = JSON.parse(data) as Record<string, unknown>+ if ('records' in written && !('processingLeases' in written)) {+ failedLedgerWrite = written+ throw Object.assign(new Error('injected ledger write failure'), { code: 'EIO' })+ }+ }+ return originalWriteFile.apply(this, args)+ })const warn = vi.spyOn(logger, 'warn')const before = await counterValue(DISCARDED_COUNTER)const interrupted = freshStore()const privateStore = interrupted as unknown as {ledger: { processingLeases?: Record<string, unknown> }- persist: () => Promise<void>reconcile: () => Promise<void>}- let leasesAtFailedPersist: unknown = 'persist not reached'- const persist = vi.spyOn(privateStore, 'persist').mockImplementationOnce(async () => {- leasesAtFailedPersist = privateStore.ledger.processingLeases- throw new Error('injected persist failure')- })const reconcile = vi.spyOn(privateStore, 'reconcile')- await expect(interrupted.initialize()).rejects.toThrow('injected persist failure')- expect(persist).toHaveBeenCalledTimes(1)- // The failing persist is the discard's own: the lease map was already- // removed from memory when it ran, and initialize stopped before reconcile.- expect(leasesAtFailedPersist).toBeUndefined()+ await expect(interrupted.initialize()).rejects.toThrow('injected ledger write failure')+ // Witness: the failing write is the discard's own lease-free ledger, and+ // initialize stopped before reconcile.+ expect(failedLedgerWrite).toBeDefined()+ expect(failedLedgerWrite).not.toHaveProperty('processingLeases')expect(reconcile).not.toHaveBeenCalled()expect(privateStore.ledger.processingLeases?.[LEGACY_LEASE_ID]).toBeDefined()expect(interrupted.isAvailable()).toBe(false)