Skip to content

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

TypeScript

test(gfs): prove stat cancellation and durable approval resume

Alfredo Lopezd881a7355a5a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index f05a8e63b..57e1f20e3 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"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 0b6cff67a..cbe5371b8 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/orchestration/__tests__/approvedToolsLifecycle.integration.test.ts b/mcp-host/src/core/orchestration/__tests__/approvedToolsLifecycle.integration.test.ts
index 4dfbc00d9..db4519389 100644
--- a/mcp-host/src/core/orchestration/__tests__/approvedToolsLifecycle.integration.test.ts
+++ b/mcp-host/src/core/orchestration/__tests__/approvedToolsLifecycle.integration.test.ts
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { TaskExecutor, type TaskExecutorDeps } from '../../../agent/taskExecutor'
import { config as appConfig } from '../../../config'
+import type { PendingApprovalRow } from '../../../db/worker/protocol'
import { TaskLifecycle } from '../../../lifecycle/taskLifecycle'
import type { SingleTurnProvider } from '../../../llm/types'
import { McpManager } from '../../../mcp/manager'
@@ -9,6 +10,9 @@ import * as boundedValidation from '../../adapters/boundedSchemaValidation'
import { CompositeToolRegistry, McpToolRegistryAdapter } from '../../adapters/toolRegistryAdapter'
import { makeFakeConversation } from '../../conversation/__testing__/makeFakeConversation'
import { ConversationManager } from '../../conversation/conversation'
+import { makeSqliteStore } from '../../conversation/persistence/__tests__/testHelpers'
+import { reconstructPendingApproval } from '../../conversation/persistence/reconstruct'
+import { SqliteConversationStore } from '../../conversation/persistence/sqliteConversationStore'
import { ApprovalController } from '../../extensions/approvalController'
import { InLoopContextManager, PressureContextManager } from '../../extensions/contextManager'
import { UnifiedApprovalGateController } from '../../extensions/mcpApprovalGateController'
@@ -564,9 +568,19 @@ describe('approved catalog across presentation and lifecycle', () => {
)
})
- it.each(['approve', 'direct', 'default', 'deny', 'cancel', 'revoke', 'schema-change'] as const)(
+ it.each([
+ 'approve',
+ 'durable',
+ 'direct',
+ 'default',
+ 'deny',
+ 'cancel',
+ 'revoke',
+ 'schema-change',
+ ] as const)(
'TaskExecutor %s uses the actual loop and never duplicates the MCP call',
async decision => {
+ const durable = decision === 'durable' ? makeSqliteStore() : undefined
const direct = decision === 'direct' || decision === 'default'
const saved = {
enableApproval: appConfig.enableApproval,
@@ -662,7 +676,7 @@ describe('approved catalog across presentation and lifecycle', () => {
const lifecycle = new TaskLifecycle()
lifecycle.register(task)
const deps: TaskExecutorDeps = {
- conversationManager: new ConversationManager(),
+ conversationManager: new ConversationManager(durable?.store),
llmProvider: provider,
mcpManager: manager,
workspaceService: undefined,
@@ -683,7 +697,7 @@ describe('approved catalog across presentation and lifecycle', () => {
onFail: vi.fn(),
dynamicEnvProvider: () => ({}),
}
- const executor = new TaskExecutor(task, deps)
+ let executor = new TaskExecutor(task, deps)
await executor.run()
expect(deps.onFail).not.toHaveBeenCalled()
expect(executor.executorState).toBe('waiting_approval')
@@ -691,7 +705,24 @@ describe('approved catalog across presentation and lifecycle', () => {
const approvalId = executor.pendingApproval!.request_id
expect(approvalId).toBeTruthy()
expect(remote.calls).not.toHaveBeenCalled()
- if (decision === 'approve' || direct) {
+ if (durable) {
+ const row = durable.worker.db
+ .prepare('SELECT * FROM pending_approvals WHERE request_id = ?')
+ .get(approvalId) as PendingApprovalRow
+ const restored = reconstructPendingApproval(row)
+ expect(restored.tool_call_id).toBe(call.id)
+ expect(restored.parameters).toEqual(executor.pendingApproval!.parameters)
+ expect(restored.task_budget).toEqual(executor.pendingApproval!.task_budget)
+ const sessionKey = executor.sessionKey!
+ // A fresh cache and executor force the production cold-load path through SQLite.
+ deps.conversationManager = new ConversationManager(
+ new SqliteConversationStore(durable.persistQueue, { cacheSize: 8 })
+ )
+ executor = new TaskExecutor(task, deps)
+ await executor.rehydrateWaitingApproval(sessionKey, restored)
+ expect(executor.pendingApproval?.request_id).toBe(approvalId)
+ }
+ if (decision === 'approve' || decision === 'durable' || direct) {
await executor.resumeAfterApproval(false)
expect(executor.executorState).toBe('completed')
expect(remote.calls).toHaveBeenCalledTimes(1)
@@ -700,6 +731,10 @@ describe('approved catalog across presentation and lifecycle', () => {
providerCalls[1].flatMap(message => message.tool_calls ?? []).map(tool => tool.id)
).toContain(call.id)
expect(deps.onApprovalNeeded).toHaveBeenCalledTimes(1)
+ if (durable) {
+ await expect(executor.resumeAfterApproval(false)).rejects.toThrow('Cannot resume')
+ expect(remote.calls).toHaveBeenCalledTimes(1)
+ }
} else if (decision === 'schema-change') {
const selected = remote.catalogs
.get('alpha')!
@@ -737,6 +772,7 @@ describe('approved catalog across presentation and lifecycle', () => {
}
expect(deps.onFail).not.toHaveBeenCalled()
} finally {
+ await durable?.shutdown()
Object.assign(appConfig, saved)
}
}
diff --git a/mcp-host/src/internalTools/gfsClient.test.ts b/mcp-host/src/internalTools/gfsClient.test.ts
index 2f733dac5..37dc787cc 100644
--- a/mcp-host/src/internalTools/gfsClient.test.ts
+++ b/mcp-host/src/internalTools/gfsClient.test.ts
@@ -514,6 +514,44 @@ describe('GFS binary content snapshots', () => {
expect(budget.residentBytes).toBe(0)
})
+ it('cancels stat when the caller aborts and cleans its timer and listener', async () => {
+ vi.useFakeTimers()
+ try {
+ const budget = new VisualInputBudget()
+ const controller = new AbortController()
+ const remove = vi.spyOn(controller.signal, 'removeEventListener')
+ let entered!: () => void
+ const reading = new Promise<void>(resolve => {
+ entered = resolve
+ })
+ const fetchFn = vi.fn((_url: string, init?: RequestInit) => {
+ entered()
+ return new Promise<Response>((_resolve, reject) => {
+ init!.signal!.addEventListener('abort', () => reject(new Error('private detail')), {
+ once: true,
+ })
+ })
+ })
+ const client = createGfscClient({
+ get: () => undefined,
+ readFile: async () => 'unit-gfs-read-identity',
+ fetch: fetchFn,
+ })
+ const failure = expect(
+ client.read(READ_ARGS, { budget, signal: controller.signal })
+ ).rejects.toMatchObject({ code: 'cancelled' })
+ await reading
+ controller.abort()
+ await failure
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(budget.residentBytes).toBe(0)
+ expect(remove).toHaveBeenCalledWith('abort', expect.any(Function))
+ expect(vi.getTimerCount()).toBe(0)
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('bounds the entire metadata request by a deadline', async () => {
vi.useFakeTimers()
try {