Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): decouple shell_exec from the GFS download store
Alfredo Lopez06ef00a7faa0
diff --git a/docs/operations/gfs-large-file-host-downloads.md b/docs/operations/gfs-large-file-host-downloads.mdindex e8891a67e..dec77ab98 100644--- a/docs/operations/gfs-large-file-host-downloads.md+++ b/docs/operations/gfs-large-file-host-downloads.md@@ -110,8 +110,7 @@ releases its pin only after its executions settle. A positively absent copy mayrelease its charge; corrupt or ambiguous copies remain charged until recovery.A replacement needs fresh GFS authorization.-After a clean v2 writer restart with no unknown inherited execution, pins alone-do not disable delivery. Fresh remote authorization and source version, size,+After a clean v2 writer restart, pins alone do not disable delivery. Fresh remote authorization and source version, size,checksum and caller validation can rebind the exact restored task owner to thenew writer session. Other tasks keep their pins. A task ID alone is insufficientto recover access.@@ -126,20 +125,36 @@ to recover access.proven scope. Persisted approvals without a scope are treated as individualinvocations.- Combined retained command output is bounded to 1 MiB. Live progress is bounded to 64 KiB. Exceeding the output bound terminates the process group and returns a truthful `output_limit_exceeded` result.-- A processing lease is acquired after approval and before the child process is created. Integrity checks precede its processing budget, and file expiry, lease expiry and shutdown are rechecked after durable admission. If admission becomes unavailable, it is rolled back and no process starts.-- The lease is released only after process-group termination and output settlement. A live execution protects its copy even if the durable deadline has elapsed; a timer alone does not prove physical termination. If release fails, the result is an error and cleanup protection remains.-- Every execution lease is durable, including a command admitted before any- download exists. Recovery retains inherited executions and their protected- copies until physical settlement is proved. Neither a deadline, a PID probe- nor acquisition of the writer lock proves that a detached child terminated.- Unknown inherited executors disable managed downloads and execution for the- whole Host because Stage 1 executors share one UID. A failed admission rollback- remains charged when persistence is uncertain; it does not invent a running- execution.+- `shell_exec` never calls the GFS download store (#1019). It does not acquire+ a processing lease, does not depend on `store.isAvailable()`, and keeps+ running while the store is recovery-required. When a Host-owned store exists,+ the shell stays bound to the verified caller root and keeps forced live+ approval; before every command it re-verifies that the caller root is still+ canonical. That check is per-user directory scoping, not store state.+- The store's lifecycle (expiry, pressure eviction, quota, `close()`) counts+ only its own transfers and task receipt owners. A running shell does not+ protect a copy and does not delay `close()`.++Accepted risks of this decoupling:++1. Expiry or eviction can remove a retained copy while a command is using it.+ A file descriptor that is already open keeps reading; a later open gets+ `ENOENT`.+2. A command can alter a retained copy. SHA-256 re-verification in+ `reusableReceipt` and at startup reconciliation quarantines an altered copy+ instead of reusing it.+3. The processing lease was never a security boundary: executors share the Host+ UID and run without a sandbox. Live approval, `BasicSafety` validation and+ credential-slot stripping remain the shell's controls.+4. Admission cleanup no longer runs before a shell, so a command can read an+ expired copy until the hourly sweep removes it.++Partial reads are not possible, because publication is an atomic `rename` of+`source.partial`.Unix directory modes and random directory names do not provide cross-caller OS isolation when a Host shares one UID. Approved arbitrary shell access remains a documented Stage 1 residual; stronger executor isolation is separate Stage 2 work.-Shell cleanup signals and waits for the detached process group before releasing a processing lease. Operating systems may reuse a process-group identifier after the original leader has been reaped; Stage 1 narrows that window by signaling immediately on leader close, but stronger executor identity is required to eliminate it.+Shell cleanup signals and waits for the detached process group before the tool result is returned. Operating systems may reuse a process-group identifier after the original leader has been reaped; Stage 1 narrows that window by signaling immediately on leader close, but stronger executor identity is required to eliminate it.Generic workspace tools reject direct and symlink-resolved access to`.gfs-downloads` and the complete `.gfs-download-store` accounting namespace@@ -152,7 +167,7 @@ Caller binding canonicalizes the configured Host base, then verifies realcaller child. Redirected caller namespaces produce unavailable bindings;ordinary text tasks continue while managed tools remain denied. A legitimateplatform alias on the configured Host base is retained. Managed shell also-revalidates its actual canonical root before lease acquisition and spawning.+revalidates its actual canonical root before spawning.These checks prevent a known redirected root from granting generic tool accessto accounting. They do not provide FD-anchored executor isolation against allshared-UID filesystem races; that remains the Stage 2 boundary.@@ -160,14 +175,14 @@ shared-UID filesystem races; that remains the Stage 2 boundary.## Retention, quotas, and recovery- Completed copies have a configured maximum retention time, seven days by- default. Active executions and task receipt pins protect a copy past its TTL- until the consumer physically settles.+ default. Task receipt pins protect a copy past its TTL until the task+ releases them. A shell execution does not protect a copy (accepted risk 1).- Host and caller quotas account for partial and completed files. Unknown or corrupt accounting fails closed rather than reporting zero usage.-- A new store publishes an atomic schema-1 ledger before accepting transfers. Every existing ledger is parsed, including empty content; invalid record or lease maps are rejected.+- A new store publishes an atomic schema-1 ledger before accepting transfers. Every existing ledger is parsed, including empty content; invalid record or owner maps, and malformed legacy processing-lease maps, are rejected with `corrupt_store_ledger`.- A pre-existing store directory with a missing ledger is unknown accounting, including an interrupted first initialization before ledger publication. Startup rejects it and preserves retained bytes for operator recovery instead of silently resetting quota. This can require recovery after a bootstrap interruption.- Startup does not reconstruct an accounting directory deleted in its entirety while caller copies remain. Approved shell commands share the Host UID and can destroy this state; whole-store deletion remains outside the recovery guarantee and requires operator inventory of retained copies.- Admission may evict the oldest verified completed copies that have no active- transfer, execution lease or task receipt owner. The complete eviction plan+ transfer or task receipt owner. The complete eviction planmust satisfy both Host and caller quotas before any copy is removed. Animpossible or invalid request cannot evict another caller's files. Active,pinned, inherited, corrupt and ambiguous entries are never pressure victims.@@ -184,7 +199,21 @@ shared-UID filesystem races; that remains the Stage 2 boundary.Quota charges are released only after positive filesystem absence. A cleanupfailure remains charged and is observable for recovery.- Startup reconciles the ledger and partial files before the capability is advertised.-- Shutdown stops new admission, drains active work where possible, and leaves unproven lease/recovery state protected.+- Legacy processing leases written by builds before #1019 are discarded at+ initialize, after the ledger is parsed and before reconciliation. No lease in+ an older ledger can protect an executor of the current boot, and a lease left+ by a crashed Host would otherwise fence the store forever. The removal is+ persisted first; only after that persist succeeds does the Host log a warning+ with the count and increment+ `clerum_gfs_legacy_processing_leases_discarded_total`. If the persist fails,+ initialize rejects, the ledger keeps the lease, and nothing is logged or+ counted; the next initialize retries. Records that protected copies are then+ reconciled like any other: an intact completed copy is reusable, an altered+ or unfinished one is quarantined.+- Records an earlier boot left quarantined are counted at initialize, logged+ and added to `clerum_gfs_inherited_quarantined_records_total`. They stay+ charged to quota until operator recovery.+- Shutdown stops new admission, drains active transfers where possible, and leaves unproven recovery state protected.- Pending and queued admission rechecks shutdown after asynchronous validation and persistence. Shutdown rechecks active ownership before releasing the writer lease.Writer exclusion uses the existing SQLite dependency with a kernel-held@@ -204,8 +233,10 @@ owners, groups, symlinks and hard links remain errors.A store initialization failure leaves safe RPC capabilities running and recordsa recovery-required download state. Caller workspace binding remains in force.-Managed shell execution fails before spawning a process; it cannot fall back to-the Host's shared workspace or omit the processing lease.+Managed shell execution keeps the verified caller root and continues; it cannot+fall back to the Host's shared workspace. When initialization succeeds but the+store is not available, the runtime logs an error that operator recovery is+required and managed GFS delivery is disabled.Verified contention with an active v2 writer and a valid unchanged ledger is atemporary state. The runtime retries that case up to 60 times at two-second@@ -216,11 +247,10 @@ starts after successful acquisition, including acquisition after retry.Shutdown cancels scheduling, joins outstanding work and closes held writerownership even when delivery is unavailable.-Lease admission verifies the checksum of every retained completed caller copy-under store serialization. Its cost grows with retained caller bytes; the-default caller budget is 256 MiB. This correctness check has not been benchmarked-on every supported PVC. Stage 2 must measure concurrency and evaluate verified-descriptor/state revalidation outside the mutex before changing that contract.+Reuse verifies the checksum of a retained completed copy under store+serialization, and startup reconciliation verifies every retained completed+copy. Their cost grows with retained caller bytes; the default caller budget is+256 MiB. This correctness check has not been benchmarked on every supported PVC.A pending task pin can survive a Host restart and protect bytes beyond TTL.If the task never resumes, use the operator inventory and exact terminal-owner@@ -228,10 +258,11 @@ recovery protocol. A time limit or cache pressure cannot prove that owner isunused. Monitor bounded counts and retained bytes; do not label metrics withowner or caller IDs.-Execution safety binding is independent of delivery eligibility. Cron,-internal and approval-disabled tasks associated with this store cannot obtain-an unleased shared-root shell during recovery. Trusted system tasks use the-existing system workspace contract; a missing verified root denies spawning.+Execution safety binding is independent of delivery eligibility and of store+availability. Cron, internal and approval-disabled tasks associated with this+store cannot obtain a shared-root shell, during recovery or otherwise. Trusted+system tasks use the existing system workspace contract; a missing verified+root denies spawning.Healthy unattended execution keeps its existing policy, while large workspacedelivery still requires its attended caller and approval capability.@@ -246,6 +277,8 @@ The global `/metrics` endpoint exposes fixed-cardinality instruments:- `clerum_gfs_download_quota_total{scope,reason}`- `clerum_gfs_download_expiry_total{outcome}`- `clerum_gfs_shell_output_limits_total{outcome}`+- `clerum_gfs_legacy_processing_leases_discarded_total` (legacy leases removed at initialize)+- `clerum_gfs_inherited_quarantined_records_total` (records an earlier boot quarantined)Labels contain bounded enums only. Caller, resource, download, command, path, correlation IDs, filenames, credentials, and raw output are prohibited as metric labels.@@ -286,18 +319,22 @@ contents, filenames, caller identities, commands or credentials.whole operation and recheck it before filesystem effects.3. Inspect and review the three hashes and exact selection IDs. Unknown orunjournaled caches fail inventory even with an empty ledger. Select only- executions proved settled, task owners proved terminal, and explicitly- approved unused partial copies. The default preserves copies and charges.+ task owners proved terminal and explicitly approved unused partial copies.+ The default preserves copies and charges. Since #1019 the Host discards+ legacy processing leases itself at initialize, so the operator no longer+ settles them to restore availability; `settledProcessingLeaseIds` remains+ accepted only for a ledger written by an older build that is inspected+ before any current Host opens it.4. Call `recoverGfsStoreUnderPhysicalFence` with those expected hashes,- `settledProcessingLeaseIds`, optional `terminalReceiptOwnerIds` and+ any `settledProcessingLeaseIds` from step 3, optional `terminalReceiptOwnerIds` and`removeSettledTransferIds`, and the real `withPhysicalFence` provider.A constant successful callback or an environment flag is not a provider.5. Verify the returned before/after receipt and preserved published copies.Only an exact verified partial directory can be removed; its charge isreleased after positive absence. A published `source` remains protected evenwhen a crash left its ledger entry without a completed state or checksum.- Verified copies with their original checksum can become reusable after all- unknown executions settle; corrupt copies remain charged and quarantined.+ Verified copies with their original checksum can become reusable; corrupt+ copies remain charged and quarantined.6. Reopen the Host on the same retained PVC. Prove availability, authenticatedreuse, source/version equality and quotas before restoring ordinary traffic.On interruption, preserve the last receipt and accounting objects, obtain adiff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 0d8680d47..ae1cfe4e2 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.500",+ "version": "0.1.501","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.500",+ "version": "0.1.501","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 07d66f086..5f434642c 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.500",+ "version": "0.1.501","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 9c5665e6d..2b7b7f65d 100644--- a/mcp-host/src/__tests__/gfsRuntime.test.ts+++ b/mcp-host/src/__tests__/gfsRuntime.test.ts@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os'import { join } from 'node:path'import { type GfsRuntime, bootstrapGfsRuntime } from '../gfsRuntime'import { GfsDownloadStore, GfsDownloadStoreError } from '../internalTools/gfsDownloadStore'+import { logger } from '../logger'const roots: string[] = []const stores: GfsDownloadStore[] = []@@ -158,6 +159,26 @@ describe('GFS runtime bootstrap', () => {expect(vi.getTimerCount()).toBe(0)})+ it('U9: logs that recovery is required when initialize succeeds but the store is unavailable', async () => {+ const initialize = vi.spyOn(GfsDownloadStore.prototype, 'initialize')+ vi.spyOn(GfsDownloadStore.prototype, 'isAvailable').mockReturnValue(false)+ const error = vi.spyOn(logger, 'error')+ const runtime = await bootstrapGfsRuntime(await root())+ runtimes.push(runtime)++ expect(initialize).toHaveBeenCalledOnce()+ await expect(initialize.mock.results[0]!.value).resolves.toBeUndefined()+ const recoveryLogs = error.mock.calls.filter(([, message]) =>+ message.includes('operator recovery is required')+ )+ expect(recoveryLogs).toEqual([+ [+ { component: 'gfs-runtime', available: false },+ 'GFS download store initialized but is not available; operator recovery is required and managed GFS delivery is disabled',+ ],+ ])+ })+it('closes initialized ownership even when quarantine makes the store unavailable', async () => {const runtime = await bootstrapGfsRuntime(await root())runtimes.push(runtime)diff --git a/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts b/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.tsindex 41ef82edc..0c0b54c8d 100644--- a/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts+++ b/mcp-host/src/agent/__tests__/gfsExecutionCapability.test.ts@@ -1,7 +1,6 @@import { describe, expect, it } from 'vitest'import type { ApprovalConfig } from '../../core/extensions/approvalTypes'import type { GfsDownloadStore } from '../../internalTools/gfsDownloadStore'-import type { GfsProcessingLeaseProvider } from '../../internalTools/gfsProcessingLease'import {gfsManagedWorkspaceExecution,gfsWorkspaceExecutionEnabled,@@ -11,13 +10,6 @@ const approvalConfig: ApprovalConfig = {defaultPolicy: 'channel_users',channels: {},}-const leases: GfsProcessingLeaseProvider = {- acquireProcessingLease: async () => ({- leaseId: 'lease-1',- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }),- releaseProcessingLease: async () => undefined,-}function input(overrides?: Partial<Parameters<typeof gfsWorkspaceExecutionEnabled>[0]>) {return {@@ -26,7 +18,6 @@ function input(overrides?: Partial<Parameters<typeof gfsWorkspaceExecutionEnablecallerIdentity: 'caller-1',store: { isAvailable: () => true } as GfsDownloadStore,callerWorkspacePath: '/workspace/users/caller-1',- processingLeaseProvider: leases,approvalConfig,retentionOwnerId: 'task-owner',...overrides,@@ -68,7 +59,6 @@ describe('gfsWorkspaceExecutionEnabled', () => {['missing caller identity', { callerIdentity: undefined }],['missing Host store', { store: undefined }],['missing caller workspace', { callerWorkspacePath: undefined }],- ['missing processing lease provider', { processingLeaseProvider: undefined }],['missing task retention owner', { retentionOwnerId: undefined }],])('rejects %s', (_name, overrides) => {expect(gfsWorkspaceExecutionEnabled(input(overrides))).toBe(false)@@ -84,22 +74,39 @@ describe('store-associated executable workspace', () => {'shell consent disabled',{ approvalConfig: { ...approvalConfig, tools: { shell_exec: false } } },],- ['missing caller root', { callerWorkspacePath: undefined }],['missing delivery owner', { retentionOwnerId: undefined }],- ])('keeps processing lease protection for %s', (_name, overrides) => {- expect(gfsManagedWorkspaceExecution(input(overrides))?.processingLeaseProvider).toBe(leases)+ ])('keeps the store-bound caller root for %s', (_name, overrides) => {+ expect(gfsManagedWorkspaceExecution(input(overrides))).toMatchObject({+ callerIdentity: 'caller-1',+ callerWorkspacePath: '/workspace/users/caller-1',+ })})- it('derives a store-bound provider when none was supplied instead of dropping protection', () => {- const store = {- isAvailable: () => false,- processingLeaseProvider: () => leases,- } as unknown as GfsDownloadStore- const managed = gfsManagedWorkspaceExecution(- input({ store, processingLeaseProvider: undefined })- )- expect(managed).toMatchObject({ processingLeaseProvider: leases, deliveryAvailable: false })+ it('keeps the caller root and only disables delivery while the store is unavailable', () => {+ const touched: string[] = []+ // Every store member other than isAvailable() records its access: managed+ // execution must not depend on any other store operation (#1019).+ const store = new Proxy({} as GfsDownloadStore, {+ get(_target, property) {+ touched.push(String(property))+ if (property === 'isAvailable') return () => false+ return undefined+ },+ })+ const managed = gfsManagedWorkspaceExecution(input({ store }))+ expect(managed).toMatchObject({+ callerIdentity: 'caller-1',+ callerWorkspacePath: '/workspace/users/caller-1',+ deliveryAvailable: false,+ })expect(managed?.store).toBeUndefined()+ // Witness: the availability probe ran; nothing else on the store was read.+ expect(touched).toEqual(['isAvailable'])+ expect(+ gfsWorkspaceExecutionEnabled(+ input({ store: managed?.store, callerWorkspacePath: managed?.callerWorkspacePath })+ )+ ).toBe(false)})it('uses the system admission identity while keeping absent caller roots unavailable', () => {diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.tsindex fd2e71e5e..93c0132d2 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts@@ -11,6 +11,8 @@ import { GfsDownloadStore } from '../../internalTools/gfsDownloadStore'import { TaskLifecycle } from '../../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../../llm/types'import type { Task, TaskSource } from '../../queue/types'+import { resolveCallerRootBinding } from '../../workspace/callerRootBinding'+import { ScopedWorkspaceProvider } from '../../workspace/scopedWorkspace'import { TaskExecutor, type TaskExecutorDeps } from '../taskExecutor'vi.mock('child_process', async importOriginal => {@@ -18,6 +20,37 @@ vi.mock('child_process', async importOriginal => {return { ...actual, spawn: vi.fn(actual.spawn) }})+const { clientFactory } = vi.hoisted(() => ({ clientFactory: vi.fn() }))+// Only GFSC's external boundary is doubled; the store, registry, approval gate,+// TaskExecutor and shell are real.+vi.mock('../../internalTools/gfsClient', async importOriginal => ({+ ...(await importOriginal<typeof import('../../internalTools/gfsClient')>()),+ getGfsToolScopes: () => new Set(['gfs.read']),+ createGfscClient: clientFactory,+}))++const largeResourceId = 'c'.repeat(32)+const largeFile = {+ source: {+ kind: 'gfs' as const,+ drive: 'main',+ resourceId: largeResourceId,+ gfsUri: `gfs://main/${largeResourceId}`,+ name: 'large.csv',+ version: 7,+ },+ size: 3_836_961,+}+let gfsClient: {+ accessible: ReturnType<typeof vi.fn>+ list: ReturnType<typeof vi.fn>+ read: ReturnType<typeof vi.fn>+ readMetadata: ReturnType<typeof vi.fn>+ download: ReturnType<typeof vi.fn>+ stat: ReturnType<typeof vi.fn>+ resolve: ReturnType<typeof vi.fn>+}+const savedConfig = {enableApproval: appConfig.enableApproval,codexToolPresentation: appConfig.codexToolPresentation,@@ -35,6 +68,20 @@ beforeEach(() => {promptCacheEnabled: false,})vi.clearAllMocks()+ const unexpected = (name: string) =>+ vi.fn(async () => {+ throw new Error(`Unexpected GFSC ${name} call`)+ })+ gfsClient = {+ accessible: unexpected('accessible'),+ list: unexpected('list'),+ read: unexpected('read'),+ readMetadata: vi.fn(async () => structuredClone(largeFile)),+ download: unexpected('download'),+ stat: unexpected('stat'),+ resolve: unexpected('resolve'),+ }+ clientFactory.mockReturnValue(gfsClient)})afterEach(async () => {@@ -53,6 +100,36 @@ async function unavailableStore() {return { root, store }}+/**+ * A store whose ledger carries a processing lease written by a pre-#1019 Host+ * that crashed: expired, empty, and owned by a foreign writer session.+ */+async function legacyLeaseStore() {+ const root = await fs.mkdtemp(join(tmpdir(), 'gfs-shell-safety-legacy-'))+ roots.push(root)+ const first = new GfsDownloadStore(root)+ await first.initialize()+ await first.close()+ const ledgerPath = join(root, '.gfs-download-store', 'ledger-v1.json')+ const ledger = JSON.parse(await fs.readFile(ledgerPath, 'utf8'))+ const leaseId = '77777777-7777-4777-8777-777777777777'+ ledger.processingLeases = {+ [leaseId]: {+ leaseId,+ callerIdentity: '_system',+ recordIds: [],+ acquiredAt: new Date(Date.now() - 120_000).toISOString(),+ expiresAt: new Date(Date.now() - 60_000).toISOString(),+ writerSessionId: '88888888-8888-4888-8888-888888888888',+ },+ }+ await fs.writeFile(ledgerPath, JSON.stringify(ledger), { mode: 0o600 })+ const store = new GfsDownloadStore(root)+ stores.push(store)+ await store.initialize()+ return { root, store }+}+async function healthyStore() {const root = await fs.mkdtemp(join(tmpdir(), 'gfs-shell-safety-healthy-'))roots.push(root)@@ -67,20 +144,46 @@ async function shellScenario({approvalEnabled = true,shellApprovalDisabled = false,healthy = false,+ legacyLease = false,+ channelCaller = false,missingCallerRoot = false,+ calls = [{ id: 'shell-safety', name: 'shell_exec', arguments: { command: 'pwd' } }],}: {source: TaskSourceapprovalEnabled?: booleanshellApprovalDisabled?: booleanhealthy?: boolean+ legacyLease?: boolean+ channelCaller?: booleanmissingCallerRoot?: boolean+ calls?: ToolCall[]}) {appConfig.enableApproval = approvalEnabled- const { root, store } = healthy ? await healthyStore() : await unavailableStore()- const callerWorkspace = join(root, 'users', '_system')+ const { root, store } = legacyLease+ ? await legacyLeaseStore()+ : healthy+ ? await healthyStore()+ : await unavailableStore()+ const sourceMessage = channelCaller+ ? {+ sender: 'gfs-safety-caller',+ content: 'run pwd',+ channelType: 'rpc' as const,+ channelId: 'gfs-safety-channel',+ messageId: 'gfs-safety-message',+ timestamp: new Date().toISOString(),+ hostRef: 'gfs-safety-host',+ }+ : undefined+ let callerWorkspace = join(root, 'users', '_system')+ if (sourceMessage) {+ const binding = resolveCallerRootBinding(new ScopedWorkspaceProvider(root), sourceMessage)+ if (!binding.root) throw new Error(`Test caller binding failed: ${binding.failureCode}`)+ callerWorkspace = binding.root+ }await fs.mkdir(callerWorkspace, { recursive: true, mode: 0o700 })- const call: ToolCall = { id: 'shell-safety', name: 'shell_exec', arguments: { command: 'pwd' } }const providerCalls: ChatMessage[][] = []+ const advertisedTools: string[][] = []const provider: SingleTurnProvider = {getProviderType: () => 'codex-subscription',classifyError: () => ({@@ -91,13 +194,15 @@ async function shellScenario({completeSingleTurn: async () => {throw new Error('Unexpected non-tool completion')},- completeSingleTurnWithTools: async messages => {+ completeSingleTurnWithTools: async (messages, tools) => {providerCalls.push(structuredClone(messages))+ advertisedTools.push(tools.map(tool => tool.name))+ const call = calls[providerCalls.length - 1]return {- content: providerCalls.length === 1 ? null : 'Shell finished.',- tool_calls: providerCalls.length === 1 ? [call] : null,+ content: call ? null : 'Shell finished.',+ tool_calls: call ? [call] : null,usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 },- finish_reason: providerCalls.length === 1 ? FinishReason.ToolUse : FinishReason.Stop,+ finish_reason: call ? FinishReason.ToolUse : FinishReason.Stop,}},}@@ -107,12 +212,14 @@ async function shellScenario({status: 'pending',priority: 'normal',createdAt: new Date(),+ ...(sourceMessage ? { sourceMessage } : {}),conversationHistory: [{ role: 'user', content: 'run pwd', timestamp: new Date() }],responseCallback: vi.fn(async () => undefined),}const lifecycle = new TaskLifecycle()lifecycle.register(task)const onFail = vi.fn()+ const onApprovalNeeded = vi.fn()const deps: TaskExecutorDeps = {conversationManager: new ConversationManager(),llmProvider: provider,@@ -120,7 +227,6 @@ async function shellScenario({workspaceService: undefined,gfsDownloadStore: store,gfsCallerWorkspacePath: missingCallerRoot ? undefined : callerWorkspace,- gfsProcessingLeaseProvider: store.processingLeaseProvider('_system'),modelName: 'fixture-model',approvalConfig: {defaultPolicy: 'channel_users',@@ -137,12 +243,20 @@ async function shellScenario({coreEvents: new SimpleEventEmitter(),cronScheduler: null,taskLifecycle: lifecycle,- onApprovalNeeded: vi.fn(),+ onApprovalNeeded,onComplete: vi.fn(),onFail,dynamicEnvProvider: () => ({}),}- return { executor: new TaskExecutor(task, deps), providerCalls, onFail, callerWorkspace }+ return {+ executor: new TaskExecutor(task, deps),+ providerCalls,+ advertisedTools,+ onFail,+ onApprovalNeeded,+ callerWorkspace,+ store,+ }}it.each([@@ -150,13 +264,73 @@ it.each([['internal source', { source: 'internal' as TaskSource, approvalEnabled: false }],['approval disabled', { source: 'channel' as TaskSource, approvalEnabled: false }],['shell approval disabled', { source: 'channel' as TaskSource, shellApprovalDisabled: true }],-] as const)('denies managed shell during GFS recovery: %s', async (_name, options) => {- const scenario = await shellScenario(options)+] as const)(+ 'runs managed shell on the caller root while the GFS store needs recovery: %s',+ async (_name, options) => {+ const scenario = await shellScenario(options)+ expect(scenario.store.isAvailable()).toBe(false)+ await scenario.executor.run()++ expect(scenario.onFail).not.toHaveBeenCalled()+ expect(JSON.stringify(scenario.providerCalls)).toContain(scenario.callerWorkspace)+ expect(spawn).toHaveBeenCalledOnce()+ }+)++it('X1: an inherited legacy lease no longer blocks an approved shell or GFS delivery', async () => {+ const scenario = await shellScenario({+ source: 'channel',+ legacyLease: true,+ channelCaller: true,+ })+ expect(scenario.store.isAvailable()).toBe(true)+ await scenario.executor.run()+ expect(scenario.executor.executorState).toBe('waiting_approval')+ expect(scenario.onApprovalNeeded).toHaveBeenCalledTimes(1)+ expect(scenario.advertisedTools[0]).toContain('clerum__gfs_download')+ expect(scenario.advertisedTools[0]).toContain('shell_exec')++ await scenario.executor.resumeAfterApproval(false)++ expect(scenario.onFail).not.toHaveBeenCalled()+ expect(spawn).toHaveBeenCalledOnce()+ const toolResult = JSON.stringify(scenario.providerCalls.at(-1))+ expect(toolResult).toContain(scenario.callerWorkspace)+ expect(toolResult).not.toContain('Processing lease failed')+ expect(toolResult).not.toContain('download_busy')+})++it('X2: a store at corrupt_store_ledger keeps shell and GFS reads but withdraws delivery', async () => {+ const scenario = await shellScenario({+ source: 'cron',+ calls: [+ {+ id: 'gfs-read-large',+ name: 'clerum__gfs_read',+ arguments: { drive: 'main', resourceId: largeResourceId },+ },+ { id: 'shell-safety', name: 'shell_exec', arguments: { command: 'pwd' } },+ ],+ })+ expect(scenario.store.isAvailable()).toBe(false)await scenario.executor.run()expect(scenario.onFail).not.toHaveBeenCalled()- expect(JSON.stringify(scenario.providerCalls)).toContain('Processing lease failed')- expect(vi.mocked(spawn)).not.toHaveBeenCalled()+ const firstTools = scenario.advertisedTools[0]!+ expect(firstTools).toEqual(+ expect.arrayContaining([+ 'shell_exec',+ 'clerum__gfs_read',+ 'clerum__gfs_list',+ 'clerum__gfs_stat',+ ])+ )+ expect(firstTools).not.toContain('clerum__gfs_download')+ expect(gfsClient.readMetadata).toHaveBeenCalledOnce()+ expect(gfsClient.download).not.toHaveBeenCalled()+ expect(JSON.stringify(scenario.providerCalls[1])).toContain('workspace_delivery_unavailable')+ expect(spawn).toHaveBeenCalledOnce()+ expect(JSON.stringify(scenario.providerCalls.at(-1))).toContain(scenario.callerWorkspace)})it.each([diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.tsindex 9e2efd0b6..6e8e97ced 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts@@ -24,7 +24,7 @@ import { CronScheduler } from '../cronScheduler'import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../taskExecutor'// Observe process admission while retaining the real shell, registry, approval-// controllers, conversation manager and caller-bound processing lease provider.+// controllers, conversation manager and caller-bound GFS store.vi.mock('child_process', async importOriginal => {const actual = await importOriginal<typeof import('child_process')>()return { ...actual, spawn: vi.fn(actual.spawn) }@@ -111,9 +111,6 @@ async function scenario(throw new Error(`Test caller binding failed: ${callerBinding.failureCode}`)const callerWorkspace = callerBinding.rootconst releaseReceiptOwner = vi.spyOn(store, 'releaseReceiptOwner')- const processingLeases = store.processingLeaseProvider('gfs-caller')- const acquireLease = vi.spyOn(processingLeases, 'acquireProcessingLease')- const releaseLease = vi.spyOn(processingLeases, 'releaseProcessingLease')const providerCalls: ChatMessage[][] = []const provider: SingleTurnProvider = {getProviderType: () => 'codex-subscription',@@ -157,7 +154,6 @@ async function scenario(workspaceService: undefined,gfsDownloadStore: store,gfsCallerWorkspacePath: callerWorkspace,- gfsProcessingLeaseProvider: processingLeases,modelName: 'fixture-model',approvalConfig: { defaultPolicy: 'channel_users', channels: {} },guardrailsConfig: guardrailAsk@@ -194,8 +190,6 @@ async function scenario(conversation,executor: new TaskExecutor(task, deps),providerCalls,- acquireLease,- releaseLease,releaseReceiptOwner,onApprovalNeeded,onComplete,@@ -219,7 +213,6 @@ it('requires live shell approval despite persistent shell auto-approval', asyncexpect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)expect(s.providerCalls).toHaveLength(1)expect(spawn).not.toHaveBeenCalled()- expect(s.acquireLease).not.toHaveBeenCalled()})it.each(['http_request', 'cron_manage'] as const)(@@ -343,7 +336,6 @@ it.each(resumeCases)(expect(firstApproval.tool_call_id).toBe(firstCall.id)expect(firstApproval.context_snapshot.length).toBeGreaterThan(0)expect(spawn).not.toHaveBeenCalled()- expect(s.acquireLease).not.toHaveBeenCalled()let executor = s.executorif (rehydrate) {@@ -356,8 +348,6 @@ it.each(resumeCases)(expect(s.onFail).not.toHaveBeenCalled()expect(spawn).toHaveBeenCalledTimes(1)expect(vi.mocked(spawn).mock.calls[0].slice(0, 2)).toEqual(['/bin/sh', ['-c', approvedCommand]])- expect(s.acquireLease).toHaveBeenCalledTimes(1)- expect(s.releaseLease).toHaveBeenCalledTimes(1)expect(s.providerCalls).toHaveLength(2)const firstResult = s.providerCalls[1].find(message => message.role === 'tool' && message.tool_call_id === firstCall.id@@ -387,8 +377,6 @@ it.each(resumeCases)(['-c', approvedCommand],['-c', secondCall.arguments.command],])- expect(s.acquireLease).toHaveBeenCalledTimes(2)- expect(s.releaseLease).toHaveBeenCalledTimes(2)expect(s.providerCalls).toHaveLength(3)const secondResult = s.providerCalls[2].find(message => message.role === 'tool' && message.tool_call_id === secondCall.id@@ -424,6 +412,5 @@ it.each([false, true])(expect(s.executor.pendingApproval!.request_id).not.toBe(firstApproval.request_id)expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)expect(spawn).not.toHaveBeenCalled()- expect(s.acquireLease).not.toHaveBeenCalled()})diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsOutputBounds.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsOutputBounds.test.tsindex afe385329..54fe27371 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsOutputBounds.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsOutputBounds.test.ts@@ -148,7 +148,6 @@ function makeScenario(options: {workspaceService: undefined,gfsDownloadStore: store,gfsCallerWorkspacePath: callerWorkspace,- gfsProcessingLeaseProvider: store.processingLeaseProvider('gfs-caller'),modelName: 'fixture-model',approvalConfig: {defaultPolicy: 'channel_users',diff --git a/mcp-host/src/agent/gfsExecutionCapability.ts b/mcp-host/src/agent/gfsExecutionCapability.tsindex 240a7d70a..316797c7c 100644--- a/mcp-host/src/agent/gfsExecutionCapability.ts+++ b/mcp-host/src/agent/gfsExecutionCapability.ts@@ -1,6 +1,5 @@import type { ApprovalConfig } from '../core/extensions/approvalTypes'import type { GfsDownloadStore } from '../internalTools/gfsDownloadStore'-import type { GfsProcessingLeaseProvider } from '../internalTools/gfsProcessingLease'import type { TaskSource } from '../queue/types'export interface GfsManagedWorkspaceExecution {@@ -10,7 +9,6 @@ export interface GfsManagedWorkspaceExecution {store?: GfsDownloadStore/** Undefined only when no trusted caller root can be derived; shell must then fail closed. */callerWorkspacePath?: string- processingLeaseProvider: GfsProcessingLeaseProviderretentionOwnerId?: stringdeliveryAvailable: boolean}@@ -23,7 +21,6 @@ export interface GfsExecutionCapabilityInput {callerIdentity: string | undefinedstore: GfsDownloadStore | undefinedcallerWorkspacePath: string | undefined- processingLeaseProvider: GfsProcessingLeaseProvider | undefinedapprovalConfig: ApprovalConfig | undefinedretentionOwnerId?: string}@@ -40,7 +37,6 @@ export function gfsWorkspaceExecutionEnabled(callerIdentity: stringstore: GfsDownloadStorecallerWorkspacePath: string- processingLeaseProvider: GfsProcessingLeaseProvider} {return Boolean(input.approvalEnabled &&@@ -48,7 +44,6 @@ export function gfsWorkspaceExecutionEnabled(input.callerIdentity &&input.store?.isAvailable() &&input.callerWorkspacePath &&- input.processingLeaseProvider &&input.retentionOwnerId &&input.approvalConfig?.tools?.shell_exec !== false)@@ -57,7 +52,8 @@ export function gfsWorkspaceExecutionEnabled(/*** Associate every executable registry with the Host GFS store independently of* download eligibility. Shell never falls back to the shared Host root while a- * store exists; recovery-required or missing caller roots fail closed.+ * store exists and fails closed without a caller root. Shell never calls the+ * store (#1019), so a recovery-required store only disables delivery.*/export function gfsManagedWorkspaceExecution(input: GfsExecutionCapabilityInput@@ -69,8 +65,6 @@ export function gfsManagedWorkspaceExecution(callerIdentity,store: storeAvailable ? input.store : undefined,callerWorkspacePath: input.callerWorkspacePath,- processingLeaseProvider:- input.processingLeaseProvider ?? input.store.processingLeaseProvider(callerIdentity),retentionOwnerId: input.retentionOwnerId,deliveryAvailable: storeAvailable,}diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.tsindex 27c9081fa..8585a7a17 100644--- a/mcp-host/src/agent/taskExecutor.ts+++ b/mcp-host/src/agent/taskExecutor.ts@@ -97,7 +97,6 @@ import { ApprovalExpiredError } from '../core/types'import { prependTextToParts, textContentFromParts } from '../core/types'import type { UsageContext } from '../core/types'import type { GfsDownloadStore } from '../internalTools/gfsDownloadStore'-import type { GfsProcessingLeaseProvider } from '../internalTools/gfsProcessingLease'import type { TaskLifecycle } from '../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../llm'import type { ImageInputResolver } from '../llm/imageInput'@@ -186,8 +185,6 @@ export interface TaskExecutorDeps {gfsDownloadStore?: GfsDownloadStore/** Trusted caller workspace root, separate from memory-tool availability. */gfsCallerWorkspacePath?: string- /** Required fail-closed lease boundary for approved local processing. */- gfsProcessingLeaseProvider?: GfsProcessingLeaseProviderconfig: AgentConfigmodelName: string/**@@ -2131,18 +2128,12 @@ export class TaskExecutor {this.deps.failover?.policy.fallbacks)const gfsCallerIdentity = this.task.sourceMessage?.sender ?? GFS_SYSTEM_CALLER_IDENTITY- const gfsProcessingLeaseProvider =- this.deps.gfsProcessingLeaseProvider ??- (this.deps.gfsDownloadStore && gfsCallerIdentity- ? this.deps.gfsDownloadStore.processingLeaseProvider(gfsCallerIdentity)- : undefined)const gfsWorkspace = gfsManagedWorkspaceExecution({approvalEnabled: appConfig.enableApproval,source: this.task.source,callerIdentity: gfsCallerIdentity,store: this.deps.gfsDownloadStore,callerWorkspacePath: this.deps.gfsCallerWorkspacePath,- processingLeaseProvider: gfsProcessingLeaseProvider,approvalConfig: this.deps.approvalConfig,retentionOwnerId: this.taskId,})@@ -2153,7 +2144,6 @@ export class TaskExecutor {callerIdentity: this.task.sourceMessage?.sender,store: gfsWorkspace.store,callerWorkspacePath: gfsWorkspace.callerWorkspacePath,- processingLeaseProvider: gfsWorkspace.processingLeaseProvider,approvalConfig: this.deps.approvalConfig,retentionOwnerId: this.taskId,})diff --git a/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.ts b/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.tsnew file mode 100644index 000000000..1c69bdf7a--- /dev/null+++ b/mcp-host/src/core/tools/__tests__/nativeToolRegistry.shellStoreDecoupling.test.ts@@ -0,0 +1,144 @@+import { afterEach, describe, expect, it, vi } from 'vitest'+import { mkdir, mkdtemp, rm } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { GfsDownloadStore } from '../../../internalTools/gfsDownloadStore'+import type { NativeToolConfig } from '../../interfaces'+import { executeWithTimeout } from '../../orchestration/toolExecutionTimeout'+import { NativeToolRegistry } from '../nativeToolRegistry'++// #1019: the managed shell is bound to the verified caller root, never to the+// GFS download store. These tests run shell_exec through the real registry with+// a real initialized store, and through the same timeout boundary the tool loop+// uses (toolUseLoopSingleTool -> executeWithTimeout).++const hosts: string[] = []+const stores: GfsDownloadStore[] = []++afterEach(async () => {+ vi.restoreAllMocks()+ for (const store of stores.splice(0)) await store.close()+ for (const host of hosts.splice(0)) await rm(host, { recursive: true, force: true })+})++function nativeConfig(shellTimeout: number): NativeToolConfig {+ return {+ workspacePath: '/nonexistent-shared-root',+ shellTimeout,+ toolTimeout: shellTimeout,+ toolProgressInterval: 30_000,+ httpAllowlist: [],+ envAllowlist: ['PATH'],+ memoryMaxSize: 1_048_576,+ }+}++async function managedHost(): Promise<{ store: GfsDownloadStore; callerRoot: string }> {+ const host = await mkdtemp(join(tmpdir(), 'gfs-shell-decoupling-'))+ hosts.push(host)+ const callerRoot = join(host, 'users', 'caller-a')+ await mkdir(callerRoot, { recursive: true, mode: 0o700 })+ const store = new GfsDownloadStore(host)+ stores.push(store)+ await store.initialize()+ return { store, callerRoot }+}++function managedRegistry(+ shellTimeout: number,+ store: GfsDownloadStore,+ callerRoot: string+): NativeToolRegistry {+ return new NativeToolRegistry(+ nativeConfig(shellTimeout),+ 'shell-store-decoupling',+ undefined, // cronScheduler+ undefined, // sourceMessage+ undefined, // workspace+ undefined, // dynamicEnvProvider+ undefined, // workflowCallerContextOverride+ undefined, // attachmentOptions+ undefined, // spilloverStorage+ undefined, // sessionSearchService+ undefined, // mcpManager+ undefined, // dynamicToolsEnabled+ undefined, // activeLlmProvider+ {+ store,+ deliveryAvailable: true,+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ retentionOwnerId: '99999999-9999-4999-8999-999999999999',+ }+ )+}++function storeMethodNames(): string[] {+ return Object.getOwnPropertyNames(GfsDownloadStore.prototype).filter(+ name =>+ name !== 'constructor' &&+ typeof Object.getOwnPropertyDescriptor(GfsDownloadStore.prototype, name)?.value === 'function'+ )+}++describe('managed shell_exec without the GFS download store (#1019)', () => {+ it.each([+ ['just under one hour', 3_594_000, 3_594_000],+ ['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')+ expect(shell).not.toBeNull()+ const output = await executeWithTimeout(+ shell!,+ { command: 'printf u5-ok; pwd' },+ { onOutput: () => undefined },+ effectiveTimeout+ )+ expect(output.is_error).toBe(false)+ expect(output.content).toContain('u5-ok')+ expect(output.content).toContain(callerRoot)+ })++ it('X3: a shell run makes zero calls on the GFS download store', async () => {+ const { store, callerRoot } = await managedHost()+ const methods = storeMethodNames()+ // Witness that the spy net is not empty: the store's public surface is covered.+ expect(methods).toEqual(+ expect.arrayContaining([+ 'isAvailable',+ 'createTransfer',+ 'publish',+ 'reusableReceipt',+ 'fail',+ 'inspect',+ 'releaseReceiptOwner',+ 'cleanupExpired',+ 'close',+ ])+ )+ const spies = methods.map(name =>+ vi.spyOn(store as unknown as Record<string, (...args: unknown[]) => unknown>, name)+ )+ const shell = managedRegistry(60_000, store, callerRoot).get('shell_exec')+ expect(shell).not.toBeNull()+ const output = await executeWithTimeout(+ shell!,+ { command: 'printf x3-ok' },+ { onOutput: () => undefined },+ 60_000+ )+ expect(output.is_error).toBe(false)+ expect(output.content).toContain('x3-ok')+ const called = spies+ .map((spy, index) => [methods[index], spy.mock.calls.length] as const)+ .filter(([, calls]) => calls > 0)+ expect(called).toEqual([])+ // The spies are live: a direct call is recorded.+ store.isAvailable()+ expect(spies[methods.indexOf('isAvailable')]!.mock.calls).toHaveLength(1)+ })+})diff --git a/mcp-host/src/core/tools/__tests__/shell.test.ts b/mcp-host/src/core/tools/__tests__/shell.test.tsindex 4e089e12d..3ad34a1ad 100644--- a/mcp-host/src/core/tools/__tests__/shell.test.ts+++ b/mcp-host/src/core/tools/__tests__/shell.test.ts@@ -1,10 +1,19 @@-import { afterEach, beforeEach, describe, expect, it } from 'vitest'-import { mkdtemp, readFile, rm, writeFile } from 'fs/promises'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { spawn } from 'child_process'+import { existsSync } from 'fs'+import { mkdir, mkdtemp, readFile, rename, rm, symlink, writeFile } from 'fs/promises'import { tmpdir } from 'os'import { join } from 'path'import { ALL_PROVIDERS, LlmProvider, PROVIDERS } from '../../../llm/registryCore'+import { executeWithTimeout } from '../../orchestration/toolExecutionTimeout'import { ShellTool } from '../shell'+// Real processes run; the spy only observes whether a process was started.+vi.mock('child_process', async importOriginal => {+ const actual = await importOriginal<typeof import('child_process')>()+ return { ...actual, spawn: vi.fn(actual.spawn) }+})+let workspacePath: stringbeforeEach(async () => {@@ -449,4 +458,204 @@ describe('ShellTool', () => {expect(result.content).toContain('kept')})})++ // PR #1005 pins the sha256 of the native tools[] baseline, which includes+ // shell_exec. Decoupling the shell from the GFS download store (#1019) must+ // not change the advertised name, description or schema. The two+ // machine-specific paths are normalized so the pin is portable.+ it('keeps the advertised shell_exec definition byte-identical to the pre-#1019 contract', () => {+ const tool = new ShellTool(workspacePath, 5000, ['PATH'])+ const description = tool+ .description()+ .replace(JSON.stringify(process.execPath), '<NODE_EXEC_PATH>')+ .replace(JSON.stringify(require.resolve('exceljs')), '<EXCELJS_PATH>')+ expect(tool.name()).toBe('shell_exec')+ expect(description).toBe(+ 'Execute a shell command in the workspace directory. ' ++ 'Supports full shell syntax (pipes, redirects, &&, etc.). ' ++ 'Commands run with a timeout and restricted environment. ' ++ 'Node.js executable: <NODE_EXEC_PATH>. ' ++ "Resolve installed Host libraries with require('node:module').createRequire(<EXCELJS_PATH>); load fast-csv through that resolver and use parseStream for streaming CSV parsing (exceljs.csv is not available). " ++ 'Verify any other executable or library before using it. ' ++ 'This tool requires approval before execution.'+ )+ expect(JSON.stringify(tool.parametersSchema())).toBe(+ JSON.stringify({+ type: 'object',+ properties: {+ command: {+ type: 'string',+ description:+ "The shell command to execute (e.g., 'ls -la', 'echo hello | wc -c', 'git status')",+ },+ },+ required: ['command'],+ })+ )+ })+})++/** A Host-managed per-user root: `<host>/users/<caller>`. */+async function managedCallerRoot(): Promise<{ host: string; callerRoot: string }> {+ const host = await mkdtemp(join(tmpdir(), 'clerum-shell-managed-'))+ const callerRoot = join(host, 'users', 'caller')+ await mkdir(callerRoot, { recursive: true })+ return { host, callerRoot }+}++async function waitForFile(file: string, timeoutMs = 5_000): Promise<string> {+ const deadline = Date.now() + timeoutMs+ while (Date.now() < deadline) {+ if (existsSync(file)) {+ const content = (await readFile(file, 'utf8')).trim()+ if (content.length > 0) return content+ }+ await new Promise(resolve => setTimeout(resolve, 10))+ }+ throw new Error(`Timed out waiting for ${file}`)+}++function processIsGone(pid: number): boolean {+ try {+ process.kill(pid, 0)+ return false+ } catch (error) {+ return (error as NodeJS.ErrnoException).code === 'ESRCH'+ }+}++describe('ShellTool without the GFS download store (#1019)', () => {+ const hosts: string[] = []+ beforeEach(() => {+ vi.mocked(spawn).mockClear()+ })+ afterEach(async () => {+ for (const host of hosts.splice(0)) await rm(host, { recursive: true, force: true })+ })++ it('U1: runs in the verified caller root with no store dependency', async () => {+ const { host, callerRoot } = await managedCallerRoot()+ hosts.push(host)+ const tool = new ShellTool(callerRoot, 5_000, ['PATH'], () => ({}), undefined, true)++ const result = await tool.execute({ command: 'printf ok' })++ expect(result).toMatchObject({ is_error: false, content: 'stdout:\nok' })+ expect(vi.mocked(spawn).mock.calls[0][2]).toMatchObject({ cwd: callerRoot })+ })++ it('U2: fails closed when the caller root is no longer canonical', async () => {+ const { host, callerRoot } = await managedCallerRoot()+ hosts.push(host)+ const tool = new ShellTool(callerRoot, 5_000, ['PATH'], () => ({}), undefined, true)+ // Witness: the same tool runs while the root is still canonical.+ expect(await tool.execute({ command: 'printf canonical' })).toMatchObject({+ is_error: false,+ content: 'stdout:\ncanonical',+ })+ expect(spawn).toHaveBeenCalledTimes(1)++ const moved = join(host, 'moved-caller')+ await rename(callerRoot, moved)+ await symlink(moved, callerRoot)+ const result = await tool.execute({ command: 'printf escaped' })++ expect(result.is_error).toBe(true)+ expect(result.content).toBe(+ 'Managed shell unavailable: the verified caller workspace root is no longer canonical.'+ )+ expect(spawn).toHaveBeenCalledTimes(1)+ })++ describe('U6: process-group termination is unchanged', () => {+ const background = (pidFile: string) =>+ `sleep 30 & echo $! > ${JSON.stringify(pidFile)}; printf started;`++ it('kills the whole group on timeout', async () => {+ const pidFile = join(workspacePath, 'timeout.pid')+ const tool = new ShellTool(workspacePath, 500, ['PATH'])+ const result = await tool.execute({ command: `${background(pidFile)} wait` })++ expect(result.is_error).toBe(true)+ expect(result.content).toContain('[Command killed after 500ms timeout')+ const grandchild = Number(await waitForFile(pidFile))+ expect(grandchild).toBeGreaterThan(0)+ expect(processIsGone(grandchild)).toBe(true)+ })++ it('kills the whole group on cancellation', async () => {+ const pidFile = join(workspacePath, 'cancel.pid')+ const controller = new AbortController()+ const tool = new ShellTool(workspacePath, 30_000, ['PATH'])+ const execution = tool.execute(+ { command: `${background(pidFile)} wait` },+ { signal: controller.signal, onOutput: () => {} }+ )+ const grandchild = Number(await waitForFile(pidFile))+ controller.abort(new Error('user cancelled'))+ const result = await execution++ expect(result.is_error).toBe(true)+ expect(result.content).toContain('[Command cancelled — partial output above]')+ expect(result.content).toContain('started')+ expect(processIsGone(grandchild)).toBe(true)+ })++ it('kills the whole group when output exceeds the bound', async () => {+ const pidFile = join(workspacePath, 'maxbuffer.pid')+ const tool = new ShellTool(workspacePath, 30_000, ['PATH'])+ const result = await tool.execute({ command: `${background(pidFile)} yes` })++ expect(result.is_error).toBe(true)+ expect(result.content).toContain('output_limit_exceeded')+ const grandchild = Number(await waitForFile(pidFile))+ expect(processIsGone(grandchild)).toBe(true)+ })++ it('does not spawn when cancellation precedes process start', async () => {+ const tool = new ShellTool(workspacePath, 5_000, ['PATH'])+ const reason = new Error('cancelled before start')+ const controller = new AbortController()+ controller.abort(reason)++ await expect(+ tool.execute({ command: 'printf late' }, { signal: controller.signal, onOutput: () => {} })+ ).rejects.toBe(reason)+ expect(spawn).not.toHaveBeenCalled()++ // Witness: the same tool with a live signal does start the process.+ const live = await tool.execute(+ { command: 'printf live' },+ { signal: new AbortController().signal, onOutput: () => {} }+ )+ expect(live).toMatchObject({ is_error: false, content: 'stdout:\nlive' })+ expect(spawn).toHaveBeenCalledTimes(1)+ })++ it('joins the outer cancellation boundary until the group has exited', async () => {+ const pidFile = join(workspacePath, 'join.pid')+ const tool = new ShellTool(workspacePath, 30_000, ['PATH'])+ expect(tool.joinsAbortSettlement()).toBe(true)+ const parent = new AbortController()+ let settled = false+ const execution = executeWithTimeout(+ tool,+ { command: `${background(pidFile)} wait` },+ { onOutput: () => {} },+ 30_000,+ parent.signal+ ).finally(() => {+ settled = true+ })+ const grandchild = Number(await waitForFile(pidFile))+ parent.abort(new Error('outer cancellation'))+ await new Promise<void>(resolve => setImmediate(resolve))+ expect(settled).toBe(false)++ const result = await execution+ expect(result.is_error).toBe(true)+ expect(result.content).toContain('[Command cancelled — partial output above]')+ expect(processIsGone(grandchild)).toBe(true)+ })+ })})diff --git a/mcp-host/src/core/tools/__tests__/shellOutputBounds.test.ts b/mcp-host/src/core/tools/__tests__/shellOutputBounds.test.tsindex 18c307ac0..6fe894f6f 100644--- a/mcp-host/src/core/tools/__tests__/shellOutputBounds.test.ts+++ b/mcp-host/src/core/tools/__tests__/shellOutputBounds.test.ts@@ -2,7 +2,6 @@ import { afterEach, expect, it } from 'vitest'import * as fs from 'node:fs'import * as os from 'node:os'import * as path from 'node:path'-import type { GfsProcessingLeaseProvider } from '../../../internalTools/gfsProcessingLease'import type { ExecutionContext } from '../../interfaces'import { ShellTool } from '../shell'@@ -63,107 +62,30 @@ it('resets HOME to the caller workspace after environment merging', async () =>}})-it('does not spawn when processing lease acquisition fails', async () => {- const workspace = managedWorkspace('shell-lease-denied-')- const marker = path.join(workspace, 'started')- const leases: GfsProcessingLeaseProvider = {- acquireProcessingLease: async () => {- throw new Error('download expired')- },- releaseProcessingLease: async () => undefined,- }- const tool = new ShellTool(workspace, 5_000, ['PATH'], () => ({}), undefined, leases)-- try {- const result = await tool.execute({ command: `touch ${JSON.stringify(marker)}` })- expect(result.is_error).toBe(true)- expect(result.content).toContain('download expired')- expect(fs.existsSync(marker)).toBe(false)- } finally {- expect(fs.existsSync(marker)).toBe(false)- }-})--it('releases the processing lease after command completion', async () => {- const workspace = managedWorkspace('shell-lease-release-')- const events: string[] = []- const leases: GfsProcessingLeaseProvider = {- acquireProcessingLease: async () => {- events.push('acquire')- return { leaseId: 'lease-1', expiresAt: new Date(Date.now() + 60_000).toISOString() }- },- releaseProcessingLease: async () => {- events.push('release')- },- }- const tool = new ShellTool(workspace, 5_000, ['PATH'], () => ({}), undefined, leases)-- try {- const result = await tool.execute({ command: 'printf done' })- expect(result.is_error).toBe(false)- expect(events).toEqual(['acquire', 'release'])- } finally {- expect(events).toEqual(['acquire', 'release'])- }-})--it('does not report success when processing lease release fails', async () => {- const workspace = managedWorkspace('shell-lease-failed-release-')- const leases: GfsProcessingLeaseProvider = {- acquireProcessingLease: async () => ({- leaseId: 'lease-1',- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }),- releaseProcessingLease: async () => {- throw new Error('lease ledger locked')- },- }- const tool = new ShellTool(workspace, 5_000, ['PATH'], () => ({}), undefined, leases)-- try {- const result = await tool.execute({ command: 'printf done' })- expect(result.is_error).toBe(true)- expect(result.content).toContain('processing_lease_release_failed')- } finally {- // Cleanup is centralized by managedHosts; keep the operation under try for- // symmetrical failure reporting without duplicating the result assertion.- }-})--it('releases the processing lease only after process-group termination', async () => {- const workspace = managedWorkspace('shell-group-lease-')- const marker = path.join(workspace, 'group-settled')+it('resolves only after the detached process group is terminated', async () => {+ const workspace = managedWorkspace('shell-group-settled-')+ const marker = path.join(workspace, 'grandchild-pid')const script = path.join(workspace, 'spawn-group-child.js')- const releaseSawSettledMarker: boolean[] = []fs.writeFileSync(script,`const cp = require('child_process')\n` +`cp.spawn(process.execPath, ['-e', ${JSON.stringify(- `require('fs').writeFileSync(${JSON.stringify(marker)}, 'yes'); setInterval(() => {}, 1000)`+ `require('fs').writeFileSync(${JSON.stringify(marker)}, String(process.pid)); setInterval(() => {}, 1000)`)}], { stdio: 'ignore' })\n` +`const timer = setInterval(() => {\n` +` if (require('fs').existsSync(${JSON.stringify(marker)})) { clearInterval(timer); process.exit(0) }\n` +`}, 5)\n`,'utf8')- const leases: GfsProcessingLeaseProvider = {- acquireProcessingLease: async () => ({- leaseId: 'lease-group',- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }),- releaseProcessingLease: async () => {- releaseSawSettledMarker.push(fs.existsSync(marker))- },- }- const tool = new ShellTool(workspace, 5_000, ['PATH'], () => ({}), undefined, leases)-- try {- const result = await tool.execute({ command: `node ${JSON.stringify(script)}` })- expect(result.is_error).toBe(false)- expect(result.content).not.toContain('process_group_termination_failed')- expect(fs.readFileSync(marker, 'utf8')).toBe('yes')- expect(releaseSawSettledMarker).toEqual([true])- } finally {- expect(fs.existsSync(marker)).toBe(true)- }+ const tool = new ShellTool(workspace, 5_000, ['PATH'], () => ({}), undefined, true)++ const result = await tool.execute({ command: `node ${JSON.stringify(script)}` })+ expect(result.is_error).toBe(false)+ expect(result.content).not.toContain('process_group_termination_failed')+ // Witness: the grandchild ran and recorded its pid before the leader exited.+ const grandchildPid = Number(fs.readFileSync(marker, 'utf8'))+ expect(Number.isSafeInteger(grandchildPid) && grandchildPid > 0).toBe(true)+ // The leader exiting does not end the group; the tool resolves only after+ // the long-lived grandchild has been killed.+ expect(() => process.kill(grandchildPid, 0)).toThrow(expect.objectContaining({ code: 'ESRCH' }))})diff --git a/mcp-host/src/core/tools/__tests__/shellProcessingLeaseCancellation.test.ts b/mcp-host/src/core/tools/__tests__/shellProcessingLeaseCancellation.test.tsdeleted file mode 100644index c6e7aaca4..000000000--- a/mcp-host/src/core/tools/__tests__/shellProcessingLeaseCancellation.test.ts+++ /dev/null@@ -1,114 +0,0 @@-import { afterEach, beforeEach, expect, it, vi } from 'vitest'-import * as fs from 'node:fs'-import * as os from 'node:os'-import * as path from 'node:path'-import type {- GfsProcessingLease,- GfsProcessingLeaseProvider,-} from '../../../internalTools/gfsProcessingLease'-import { executeWithTimeout } from '../../orchestration/toolExecutionTimeout'-import { ShellTool } from '../shell'--const mocks = vi.hoisted(() => ({ spawn: vi.fn() }))-// The delayed lease is the race under test; no process may start in this suite.-vi.mock('child_process', () => ({ spawn: mocks.spawn }))--beforeEach(() => vi.clearAllMocks())--const managedHosts: string[] = []--function managedWorkspace(): string {- const host = fs.mkdtempSync(path.join(os.tmpdir(), 'shell-managed-cancellation-'))- managedHosts.push(host)- const workspace = path.join(host, 'users', 'caller')- fs.mkdirSync(workspace, { recursive: true })- return workspace-}--afterEach(() => {- for (const host of managedHosts.splice(0)) fs.rmSync(host, { recursive: true, force: true })-})--function delayedLease() {- let finishAcquisition!: (lease: GfsProcessingLease) => void- const pending = new Promise<GfsProcessingLease>(resolve => {- finishAcquisition = resolve- })- const provider: GfsProcessingLeaseProvider = {- acquireProcessingLease: vi.fn(() => pending),- releaseProcessingLease: vi.fn(async () => undefined),- }- const lease = {- leaseId: 'cancelled-admission',- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }- return { provider, lease, finishAcquisition }-}--it('does not spawn and releases a lease acquired after cancellation', async () => {- const { provider, lease, finishAcquisition } = delayedLease()- const controller = new AbortController()- const reason = new Error('cancelled during processing lease acquisition')- const tool = new ShellTool(managedWorkspace(), 5_000, [], () => ({}), undefined, provider)- const execution = tool.execute(- { command: 'printf done' },- { signal: controller.signal, onOutput: vi.fn() }- )- expect(provider.acquireProcessingLease).toHaveBeenCalledOnce()-- controller.abort(reason)- finishAcquisition(lease)-- await expect(execution).rejects.toBe(reason)- expect(mocks.spawn).not.toHaveBeenCalled()- expect(provider.releaseProcessingLease).toHaveBeenCalledExactlyOnceWith(lease)-})--it('keeps cancellation before spawn when releasing the admitted lease fails', async () => {- const { provider, lease, finishAcquisition } = delayedLease()- vi.mocked(provider.releaseProcessingLease).mockRejectedValueOnce(new Error('ledger unavailable'))- const controller = new AbortController()- const tool = new ShellTool(managedWorkspace(), 5_000, [], () => ({}), undefined, provider)- const execution = tool.execute(- { command: 'printf done' },- { signal: controller.signal, onOutput: vi.fn() }- )-- controller.abort()- finishAcquisition(lease)-- const result = await execution- expect(result.is_error).toBe(true)- expect(result.content).toContain('processing_lease_release_failed')- expect(mocks.spawn).not.toHaveBeenCalled()- expect(provider.releaseProcessingLease).toHaveBeenCalledExactlyOnceWith(lease)-})--it('joins the actual late shell lease through the outer cancellation boundary', async () => {- const { provider, lease, finishAcquisition } = delayedLease()- const controller = new AbortController()- const reason = new Error('cancelled while acquiring the shell lease')- const tool = new ShellTool(managedWorkspace(), 5_000, [], () => ({}), undefined, provider)- let completed = false- const execution = executeWithTimeout(- tool,- { command: 'printf done' },- {- onOutput: () => {},- },- 5_000,- controller.signal- ).catch(error => {- completed = true- return error- })- controller.abort(reason)- await new Promise<void>(resolve => setImmediate(resolve))- expect(completed).toBe(false)- expect(provider.releaseProcessingLease).not.toHaveBeenCalled()- finishAcquisition(lease)-- expect(await execution).toBe(reason)- expect(mocks.spawn).not.toHaveBeenCalled()- expect(provider.releaseProcessingLease).toHaveBeenCalledExactlyOnceWith(lease)-})diff --git a/mcp-host/src/core/tools/nativeToolRegistry.ts b/mcp-host/src/core/tools/nativeToolRegistry.tsindex 78cbf8c4e..7cbc5e5ba 100644--- a/mcp-host/src/core/tools/nativeToolRegistry.ts+++ b/mcp-host/src/core/tools/nativeToolRegistry.ts@@ -13,7 +13,6 @@ import {} from '../../internalTools/gfs'import { createGfscClient, getGfsToolScopes } from '../../internalTools/gfsClient'import type { GfsDownloadStore } from '../../internalTools/gfsDownloadStore'-import type { GfsProcessingLeaseProvider } from '../../internalTools/gfsProcessingLease'import type { LlmProvider } from '../../llm/registryCore'import type { McpManager } from '../../mcp/manager'import type { IncomingMessage } from '../../server'@@ -197,7 +196,6 @@ export class NativeToolRegistry implements ToolRegistry {deliveryAvailable: booleancallerIdentity: stringcallerWorkspacePath?: string- processingLeaseProvider: GfsProcessingLeaseProviderretentionOwnerId?: string}) {@@ -221,6 +219,9 @@ export class NativeToolRegistry implements ToolRegistry {// it (route memory/daily-class file_write through WorkspaceService) is future// hardening, independent of F5.if (fileToolsRoot !== undefined) this.register(new FileWriteTool(fileToolsRoot))+ // A Host-owned GFS store binds the shell to the verified caller root. The+ // shell never calls the store itself (#1019): it only re-verifies that the+ // caller root is still canonical before each command.this.register(new ShellTool(gfsDownload ? gfsDownload.callerWorkspacePath : config.workspacePath,@@ -228,7 +229,7 @@ export class NativeToolRegistry implements ToolRegistry {config.envAllowlist,dynamicEnvProvider,activeLlmProvider,- gfsDownload?.processingLeaseProvider+ gfsDownload !== undefined))this.register(new HttpRequestTool(config.httpAllowlist))diff --git a/mcp-host/src/core/tools/shell.ts b/mcp-host/src/core/tools/shell.tsindex dc861f55f..e0774d220 100644--- a/mcp-host/src/core/tools/shell.ts+++ b/mcp-host/src/core/tools/shell.ts@@ -1,10 +1,6 @@import { spawn } from 'child_process'import { setTimeout as sleep } from 'node:timers/promises'import { recordGfsShellOutputLimit } from '../../internalTools/gfsDownloadMetrics'-import type {- GfsProcessingLease,- GfsProcessingLeaseProvider,-} from '../../internalTools/gfsProcessingLease'import { ALL_PROVIDERS, LlmProvider, PROVIDERS } from '../../llm/registryCore'import { verifyManagedCallerRootPath } from '../../workspace/callerRootBinding'import { ToolError, ToolErrorCode } from '../errors'@@ -90,8 +86,12 @@ export class ShellTool implements Tool {// child env; every other provider slot is deleted. Undefined (tool-name// listing registry, legacy tests) strips ALL slots — the secure default.private readonly activeLlmProvider?: LlmProvider,- /** Caller-bound cleanup protection for retained GFS downloads. */- private readonly processingLeases?: GfsProcessingLeaseProvider+ /**+ * Re-verify that the caller workspace root is still canonical before every+ * command. Set for Host-managed per-user roots; this is per-user directory+ * scoping and never touches the GFS download store.+ */+ private readonly verifyCallerRoot: boolean = false) {}name() {@@ -150,7 +150,7 @@ export class ShellTool implements Tool {const timeout = Math.min(this.timeout, context?.timeoutMs ?? this.timeout)const startTime = Date.now()const command = params.command as string- if (this.processingLeases && this.workspacePath) {+ if (this.verifyCallerRoot && this.workspacePath) {if (verifyManagedCallerRootPath(this.workspacePath) === undefined) {return this.managedRootUnavailable(startTime)}@@ -212,36 +212,6 @@ export class ShellTool implements Tool {}}- let processingLease: GfsProcessingLease | undefined- if (this.processingLeases) {- try {- processingLease = await this.processingLeases.acquireProcessingLease({- durationMs: timeout + ShellTool.SIGKILL_GRACE_MS + 1_000,- })- } catch (err) {- return {- content: `Processing lease failed before command start: ${(err as Error).message}`,- duration_ms: Date.now() - startTime,- is_error: true,- }- }- }-- if (context?.signal?.aborted) {- if (processingLease && this.processingLeases) {- try {- await this.processingLeases.releaseProcessingLease(processingLease)- } catch {- return {- content: 'Command cancelled before process start [processing_lease_release_failed]',- duration_ms: Date.now() - startTime,- is_error: true,- }- }- }- context.signal.throwIfAborted()- }-return new Promise<ToolOutput>(resolve => {// detached: true makes the child a process group leader so we can kill// the whole group (shell + grandchildren like `sleep`) with -pid signal.@@ -266,25 +236,6 @@ export class ShellTool implements Tool {resolve(out)}- const complete = async (- out: ToolOutput,- options: { releaseLease?: boolean } = {}- ): Promise<void> => {- if (processingLease && this.processingLeases && options.releaseLease !== false) {- try {- await this.processingLeases.releaseProcessingLease(processingLease)- } catch {- const status = '\n\n[processing_lease_release_failed]'- out.is_error = true- out.content = `${truncateUtf8Bytes(- out.content,- ShellTool.MAX_RESULT_BYTES - Buffer.byteLength(status, 'utf8')- )}${status}`- }- }- resolveOnce(out)- }-const forceKill = () => {// Kill the entire process group so grandchildren (e.g., `sleep` inside a// semi-colon chain) are also terminated and the stdout/stderr pipes close.@@ -381,7 +332,7 @@ export class ShellTool implements Tool {}const handleClose = async (exitCode: number | null): Promise<void> => {// Leader/stdio close does not prove descendants exited. Always complete- // group termination before releasing a processing lease or resolving.+ // group termination before resolving.const processGroupId = child.pidconst groupTerminated =typeof processGroupId === 'number' && processGroupId > 0@@ -439,10 +390,8 @@ export class ShellTool implements Tool {duration_ms: Date.now() - startTime,is_error: true,}- await complete(out, { releaseLease: false })- return}- await complete(out)+ resolveOnce(out)}child.on('close', exitCode => {@@ -451,7 +400,7 @@ export class ShellTool implements Tool {child.on('error', err => {cleanup()- void complete({+ resolveOnce({content: `Command failed to start: ${err.message}`,duration_ms: Date.now() - startTime,is_error: true,diff --git a/mcp-host/src/gfsRuntime.ts b/mcp-host/src/gfsRuntime.tsindex 770d5961e..e5b116d59 100644--- a/mcp-host/src/gfsRuntime.ts+++ b/mcp-host/src/gfsRuntime.ts@@ -118,6 +118,11 @@ export async function bootstrapGfsRuntime(try {await store.initialize()if (store.isAvailable()) startCycle('cleanup')+ else+ logger.error(+ { component: 'gfs-runtime', available: false },+ 'GFS download store initialized but is not available; operator recovery is required and managed GFS delivery is disabled'+ )} catch (error) {if (transientWriterContention(error)) {logger.warn(diff --git a/mcp-host/src/internalTools/gfsDownloadMetrics.test.ts b/mcp-host/src/internalTools/gfsDownloadMetrics.test.tsindex 5411038c9..8d7991a41 100644--- a/mcp-host/src/internalTools/gfsDownloadMetrics.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadMetrics.test.ts@@ -7,9 +7,18 @@ import {recordGfsDownloadExpiry,recordGfsDownloadQuota,recordGfsDownloadTransfer,+ recordGfsInheritedQuarantinedRecords,+ recordGfsLegacyProcessingLeasesDiscarded,recordGfsShellOutputLimit,} from './gfsDownloadMetrics'+async function counterValue(name: string): Promise<number> {+ const metric = register.getSingleMetric(name)+ if (metric === undefined) throw new Error(`metric ${name} is not registered`)+ const { values } = await metric.get()+ return values.reduce((sum, sample) => sum + sample.value, 0)+}+describe('GFS download metrics', () => {it('registers fixed-cardinality instruments on the global metrics endpoint', async () => {const scraped = await register.metrics()@@ -43,4 +52,22 @@ describe('GFS download metrics', () => {)expect(scraped).toContain('clerum_gfs_download_expiry_total{outcome="cleanup_failed"}')})++ it('U10: registers the legacy-lease discard and inherited-quarantine counters', async () => {+ const scraped = await register.metrics()+ expect(scraped).toContain('# TYPE clerum_gfs_legacy_processing_leases_discarded_total counter')+ expect(scraped).toContain('# TYPE clerum_gfs_inherited_quarantined_records_total counter')+ const discardedBefore = await counterValue(+ 'clerum_gfs_legacy_processing_leases_discarded_total'+ )+ const quarantinedBefore = await counterValue('clerum_gfs_inherited_quarantined_records_total')+ recordGfsLegacyProcessingLeasesDiscarded(2)+ recordGfsInheritedQuarantinedRecords(3)+ expect(await counterValue('clerum_gfs_legacy_processing_leases_discarded_total')).toBe(+ discardedBefore + 2+ )+ expect(await counterValue('clerum_gfs_inherited_quarantined_records_total')).toBe(+ quarantinedBefore + 3+ )+ })})diff --git a/mcp-host/src/internalTools/gfsDownloadMetrics.ts b/mcp-host/src/internalTools/gfsDownloadMetrics.tsindex ea13ac94d..5d3472d54 100644--- a/mcp-host/src/internalTools/gfsDownloadMetrics.ts+++ b/mcp-host/src/internalTools/gfsDownloadMetrics.ts@@ -89,6 +89,26 @@ const gfsDownloadExpiryTotal = getOrCreateCounter({labelNames: ['outcome'] as const as Array<'outcome'>,})+const gfsLegacyProcessingLeasesDiscardedTotal = getOrCreateCounter({+ name: 'clerum_gfs_legacy_processing_leases_discarded_total',+ help: 'Legacy shell processing leases discarded from the GFS download store ledger at initialize.',+ labelNames: [] as const as Array<never>,+})++const gfsInheritedQuarantinedRecordsTotal = getOrCreateCounter({+ name: 'clerum_gfs_inherited_quarantined_records_total',+ help: 'GFS download store records found quarantined by an earlier boot at initialize; they stay charged to quota until operator recovery.',+ labelNames: [] as const as Array<never>,+})++export function recordGfsLegacyProcessingLeasesDiscarded(count: number): void {+ gfsLegacyProcessingLeasesDiscardedTotal.inc(count)+}++export function recordGfsInheritedQuarantinedRecords(count: number): void {+ gfsInheritedQuarantinedRecordsTotal.inc(count)+}+export function recordGfsDownloadAdmission(outcome: GfsDownloadAdmissionOutcome): void {gfsDownloadAdmissionsTotal.labels(outcome).inc()}diff --git a/mcp-host/src/internalTools/gfsDownloadStore.test.ts b/mcp-host/src/internalTools/gfsDownloadStore.test.tsindex 8fac3ab4d..2df5b137f 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.test.ts@@ -3,6 +3,7 @@ import { createHash } from 'node:crypto'import * as fs from 'node:fs/promises'import { tmpdir } from 'node:os'import * as path from 'node:path'+import { ShellTool } from '../core/tools/shell'import type { GfsImageSource } from '../visualInput/policy'import { GfsDownloadStore } from './gfsDownloadStore'import { GFS_FILE_LIMITS } from './gfsFilePolicy'@@ -670,3 +671,167 @@ describe('GFS download store', () => {})})++describe('S6: expiry and admission without processing-lease exemptions (#1019)', () => {+ async function publishFixture(+ expiresAt = new Date(Date.now() + 60_000).toISOString(),+ retentionOwnerId?: string+ ) {+ const transfer = await store.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ source,+ sizeBytes: 7,+ expiresAt,+ retentionOwnerId,+ })+ await fs.writeFile(path.join(callerRoot, transfer.partialPath), 'fixture')+ return store.publish(+ transfer.id,+ 'caller-a',+ createHash('sha256').update('fixture').digest('hex')+ )+ }++ it('removes an expired record even after a shell read it', async () => {+ const receipt = await publishFixture()+ const shell = new ShellTool(callerRoot, 5_000, ['PATH'], () => ({}), undefined, true)+ const read = await shell.execute({ command: `cat ${JSON.stringify(receipt.path)}` })+ expect(read.is_error).toBe(false)+ expect(read.content).toContain('fixture')+ await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)+ expect(store.debugRecord(receipt.id)).toBeUndefined()+ await expect(fs.lstat(path.join(callerRoot, path.dirname(receipt.path)))).rejects.toMatchObject(+ { code: 'ENOENT' }+ )+ expect(store.debugUsage()).toMatchObject({ bytes: 0, files: 0 })+ })++ it('keeps an expired record only while a retention owner holds it', async () => {+ const receipt = await publishFixture(undefined, 'waiting-approval-task')+ await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)+ expect(store.debugRecord(receipt.id)).toBeDefined()+ expect(await fs.readFile(path.join(callerRoot, receipt.path), 'utf8')).toBe('fixture')+ await store.releaseReceiptOwner('waiting-approval-task', 'caller-a')+ await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)+ expect(store.debugRecord(receipt.id)).toBeUndefined()+ })++ it('fences a transfer whose caller validation completes after shutdown', async () => {+ const receipt = await publishFixture()+ let resume!: () => void+ let entered!: () => void+ const barrier = new Promise<void>(resolve => {+ resume = resolve+ })+ const waiting = new Promise<void>(resolve => {+ entered = resolve+ })+ const validatingStore = store as unknown as {+ validateCallerRoot: (root: string) => Promise<string>+ }+ const validate = validatingStore.validateCallerRoot.bind(store)+ vi.spyOn(validatingStore, 'validateCallerRoot').mockImplementationOnce(async root => {+ const validated = await validate(root)+ entered()+ await barrier+ return validated+ })+ const outcome = Promise.allSettled([+ store.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ source,+ sizeBytes: 7,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ }),+ ])+ await waiting+ await store.close()+ resume()++ const [result] = await outcome+ expect(result!.status).toBe('rejected')+ if (result!.status === 'rejected')+ expect(result!.reason).toMatchObject({ code: 'workspace_unavailable' })+ expect(await fs.readdir(path.join(callerRoot, '.gfs-downloads'))).toEqual([+ path.basename(path.dirname(receipt.path)),+ ])+ const restarted = new GfsDownloadStore(hostRoot)+ await restarted.initialize()+ expect(restarted.debugUsage().bytes).toBe(7)+ await restarted.close()+ })++ it('keeps another caller admissible while one caller has an expired record', async () => {+ const expired = await publishFixture()+ const callerBRoot = path.join(hostRoot, 'users', 'caller-b')+ await fs.mkdir(callerBRoot, { recursive: true, mode: 0o700 })+ const callerBTransfer = await store.createTransfer({+ callerIdentity: 'caller-b',+ callerWorkspacePath: callerBRoot,+ source,+ sizeBytes: 7,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ await fs.writeFile(path.join(callerBRoot, callerBTransfer.partialPath), 'fixture')+ const callerBReceipt = await store.publish(+ callerBTransfer.id,+ 'caller-b',+ createHash('sha256').update('fixture').digest('hex')+ )+ vi.spyOn(Date, 'now').mockReturnValue(Date.parse(expired.expiresAt) + 1)++ await store.cleanupExpired()+ expect(store.debugRecord(expired.id)).toBeUndefined()+ expect(store.isAvailable()).toBe(true)+ expect(await store.reusableReceipt('caller-b', source, 7)).toMatchObject({+ id: callerBReceipt.id,+ })+ })++ it('rechecks failed-transfer ownership after a queued publication', async () => {+ const transfer = await store.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ source,+ sizeBytes: 7,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ await fs.writeFile(path.join(callerRoot, transfer.partialPath), 'fixture')+ const persistedStore = store as unknown as { persist: () => Promise<void> }+ const persist = persistedStore.persist.bind(store)+ let resume!: () => void+ let entered!: () => void+ const barrier = new Promise<void>(resolve => {+ resume = resolve+ })+ const waiting = new Promise<void>(resolve => {+ entered = resolve+ })+ vi.spyOn(persistedStore, 'persist').mockImplementationOnce(async () => {+ await persist()+ entered()+ await barrier+ })+ const published = store.publish(+ transfer.id,+ 'caller-a',+ createHash('sha256').update('fixture').digest('hex')+ )+ await waiting+ const failed = expect(store.fail(transfer.id, 'caller-a')).rejects.toMatchObject({+ code: 'download_busy',+ })+ resume()+ const receipt = await published+ await failed+ expect(store.debugRecord(receipt.id)).toMatchObject({ state: 'completed' })+ expect(await fs.readFile(path.join(callerRoot, receipt.path), 'utf8')).toBe('fixture')+ const ledger = JSON.parse(+ await fs.readFile(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')+ )+ expect(ledger.records[receipt.id].state).toBe('completed')+ expect(ledger).not.toHaveProperty('processingLeases')+ })+})diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.tsindex cbc22f5b5..483a88057 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts@@ -2,18 +2,19 @@ import { createHash, randomUUID } from 'node:crypto'import { constants } from 'node:fs'import * as fs from 'node:fs/promises'import * as path from 'node:path'+import { logger } from '../logger'import type { GfsImageSource } from '../visualInput/policy'-import { recordGfsDownloadExpiry, recordGfsDownloadQuota } from './gfsDownloadMetrics'+import {+ recordGfsDownloadExpiry,+ recordGfsDownloadQuota,+ recordGfsInheritedQuarantinedRecords,+ recordGfsLegacyProcessingLeasesDiscarded,+} from './gfsDownloadMetrics'import {GFS_FILE_LIMITS,GFS_HOST_ACTIVE_DOWNLOADS,GFS_HOST_RETAINED_FILES,} from './gfsFilePolicy'-import type {- GfsProcessingLease,- GfsProcessingLeaseAcquisition,- GfsProcessingLeaseProvider,-} from './gfsProcessingLease'import { openPrivateStoreObject, verifyPrivateStoreDirectory } from './gfsStorePrivateFiles'import { GfsStoreWriterLease, GfsStoreWriterOwnershipError } from './gfsStoreWriterLease'@@ -77,11 +78,15 @@ export interface GfsDownloadReceipt {export interface StoreLedger {schemaVersion: 1records: Record<string, GfsDownloadRecord>- processingLeases?: Record<string, GfsProcessingLeaseRecord>+ /**+ * Legacy shell processing leases written before #1019. Still validated when+ * a ledger carries them, discarded at initialize, and never written again.+ */+ processingLeases?: Record<string, LegacyProcessingLeaseRecord>retentionOwners?: Record<string, GfsReceiptOwnerRecord>}-interface GfsProcessingLeaseRecord {+interface LegacyProcessingLeaseRecord {leaseId: stringcallerIdentity: stringrecordIds: string[]@@ -121,7 +126,6 @@ export function parseLedger(raw: string): StoreLedger {const result: StoreLedger = {schemaVersion: 1,records: {},- processingLeases: {},retentionOwners: {},}for (const [id, value] of Object.entries(records)) {@@ -176,8 +180,9 @@ export function parseLedger(raw: string): StoreLedger {Array.isArray(rawProcessingLeases))throw new GfsDownloadStoreError('corrupt_store_ledger')+ result.processingLeases = {}for (const [id, value] of Object.entries(rawProcessingLeases)) {- const lease = value as GfsProcessingLeaseRecord+ const lease = value as LegacyProcessingLeaseRecordif (!UUID_RE.test(id) ||typeof value !== 'object' ||@@ -197,7 +202,7 @@ export function parseLedger(raw: string): StoreLedger {Date.parse(lease.expiresAt) <= Date.parse(lease.acquiredAt))throw new GfsDownloadStoreError('corrupt_store_ledger')- result.processingLeases![id] = lease+ result.processingLeases[id] = lease}}const owners = (parsed as { retentionOwners?: unknown }).retentionOwners@@ -301,12 +306,10 @@ export class GfsDownloadStore {private ledger: StoreLedger = {schemaVersion: 1,records: {},- processingLeases: {},retentionOwners: {},}private readonly activeByCaller = new Map<string, number>()private readonly activeIds = new Set<string>()- private readonly liveProcessingLeases = new Set<string>()private active = 0private mutationTail = Promise.resolve()private initialized = false@@ -321,8 +324,7 @@ export class GfsDownloadStore {}isAvailable(): boolean {- if (!this.initialized || this.unsafe || !this.writerLease || this.hasInheritedExecutors())- return false+ if (!this.initialized || this.unsafe || !this.writerLease) return falsetry {this.writerLease.assertHeld()return true@@ -384,6 +386,8 @@ export class GfsDownloadStore {// A successful read must always parse, including empty content, so a// truncated ledger fails closed.this.ledger = parseLedger(raw)+ await this.discardLegacyProcessingLeases()+ this.reportInheritedQuarantine()} else if (createdStoreRoot) {// Genuinely new store directory: make the durable schema-1 ledger exist// before reconciliation or any transfer exposure, so a ledger missing@@ -482,18 +486,15 @@ export class GfsDownloadStore {input.retentionOwnerId === undefined? undefined: this.ledger.retentionOwners?.[input.retentionOwnerId]- const previousLeases = this.ledger.processingLeasestry {if (input.retentionOwnerId !== undefined)this.retainReceiptRecord(record, input.retentionOwnerId)this.ledger.records[id] = record- this.protectFutureRecord(record)await this.persist()} catch (error) {// No filesystem transfer or worker has been exposed. In-memory// admission rolls back; an uncertain durable write stays fail-closed.delete this.ledger.records[id]- this.ledger.processingLeases = previousLeasesif (input.retentionOwnerId !== undefined) {if (previousOwner) this.ledger.retentionOwners![input.retentionOwnerId] = previousOwnerelse delete this.ledger.retentionOwners![input.retentionOwnerId]@@ -529,16 +530,10 @@ export class GfsDownloadStore {} catch (error) {try {await this.serialize(async () => {- // Other already-running commands can observe this new path too.- // Producer settlement releases its active slot, never their protection.record.state = 'cleanup_failed'- // The producer's new pin has never exposed a transfer or receipt.- // With no executor protection, proven absence can roll it back too.- if (this.hasProcessingLease(id)) {- await this.persist()- return- }- // Delete/prove absence before releasing the durable reservation.+ // The producer's new pin has never exposed a transfer or receipt, so+ // proven absence rolls it back. Delete/prove absence before+ // releasing the durable reservation.if (!(await this.removeRecordDirectory(record))) {await this.persist()throw new GfsDownloadStoreError('storage_write_failed')@@ -725,7 +720,7 @@ export class GfsDownloadStore {)throw new GfsDownloadStoreError('download_busy')record.state = 'cleanup_failed'- if (this.hasProcessingLease(id) || this.hasReceiptOwner(id)) {+ if (this.hasReceiptOwner(id)) {await this.persist()return}@@ -737,7 +732,7 @@ export class GfsDownloadStore {})} finally {// The caller invokes failure only after its stream/descriptor is settled.- // Uncertain bytes stay charged; an older executor has its separate lease.+ // Uncertain bytes stay charged.this.releaseActive(record)}}@@ -867,127 +862,6 @@ export class GfsDownloadStore {}}- processingLeaseProvider(callerIdentity: string): GfsProcessingLeaseProvider {- return {- acquireProcessingLease: (options?: GfsProcessingLeaseAcquisition) =>- this.acquireProcessingLease(callerIdentity, options),- releaseProcessingLease: (lease: GfsProcessingLease) =>- this.releaseProcessingLease(callerIdentity, lease),- }- }-- private async acquireProcessingLease(- callerIdentity: string,- options?: GfsProcessingLeaseAcquisition- ): Promise<GfsProcessingLease> {- this.assertInitialized()- if (this.closing) throw new GfsDownloadStoreError('download_busy')- if (typeof callerIdentity !== 'string' || callerIdentity.length === 0)- throw new GfsDownloadStoreError('caller_mismatch')- const requestedMs = options?.durationMs ?? 60_000- if (!Number.isSafeInteger(requestedMs) || requestedMs <= 0 || requestedMs > 3_600_000)- throw new GfsDownloadStoreError('download_busy')-- return this.serialize(async () => {- this.assertInitialized()- if (this.closing) throw new GfsDownloadStoreError('download_busy')- const now = Date.now()- await this.cleanupUnconsumedForAdmission(callerIdentity, now)- const retained = Object.values(this.ledger.records).filter(- record => record.callerIdentity === callerIdentity- )- // Admission covers this caller's retained files without parsing the- // command. Expired bytes can remain while an existing execution holds- // them, but they must not authorize another execution before cleanup.- if (retained.some(record => Date.parse(record.expiresAt) <= now))- throw new GfsDownloadStoreError('download_expired')- if (retained.some(record => record.state === 'quarantined'))- throw new GfsDownloadStoreError('download_busy')- const candidates = retained.filter(- record => record.state === 'completed' && record.sha256 !== undefined- )- // An execution admitted before the first download can still write this- // caller's future paths. Keep empty leases durable and owner-bound too.- for (const record of candidates) await this.inspect(record.path, callerIdentity)- if (this.closing) throw new GfsDownloadStoreError('download_busy')- const admittedAt = Date.now()- if (retained.some(record => Date.parse(record.expiresAt) <= admittedAt))- throw new GfsDownloadStoreError('download_expired')- const leaseId = randomUUID()- const expiresAt = new Date(admittedAt + requestedMs).toISOString()- const lease: GfsProcessingLeaseRecord = {- leaseId,- callerIdentity,- recordIds: retained.map(record => record.id),- acquiredAt: new Date(admittedAt).toISOString(),- expiresAt,- writerSessionId: this.writerSessionId,- }- this.ledger.processingLeases ??= {}- this.ledger.processingLeases[leaseId] = lease- try {- await this.persist()- } catch (error) {- delete this.ledger.processingLeases[leaseId]- throw error- }- // Hashing and durable publication can outlast either deadline. Only- // return a new lease while both remain valid; an already admitted- // execution keeps its existing protection after the file's expiry.- const publishedAt = Date.now()- const denial = retained.some(record => Date.parse(record.expiresAt) <= publishedAt)- ? 'download_expired'- : this.closing || Date.parse(expiresAt) <= publishedAt- ? 'download_busy'- : undefined- if (denial) {- delete this.ledger.processingLeases[leaseId]- try {- await this.persist()- } catch (error) {- // A failed rollback retains the durable reservation until its- // deadline, without registering an execution that never started.- this.ledger.processingLeases[leaseId] = lease- throw error- }- throw new GfsDownloadStoreError(denial)- }- this.liveProcessingLeases.add(leaseId)- return { leaseId, expiresAt }- })- }-- private async releaseProcessingLease(- callerIdentity: string,- lease: GfsProcessingLease- ): Promise<void> {- this.assertInitialized()- await this.serialize(async () => {- const record = this.ledger.processingLeases?.[lease.leaseId]- if (!record) {- this.liveProcessingLeases.delete(lease.leaseId)- return- }- if (record.callerIdentity !== callerIdentity || record.expiresAt !== lease.expiresAt)- throw new GfsDownloadStoreError('caller_mismatch')- if (- record.writerSessionId !== this.writerSessionId ||- !this.liveProcessingLeases.has(lease.leaseId)- )- throw new GfsDownloadStoreError('download_busy')- delete this.ledger.processingLeases![lease.leaseId]- try {- await this.persist()- } catch (error) {- this.ledger.processingLeases![lease.leaseId] = record- this.liveProcessingLeases.add(lease.leaseId)- throw error- }- this.liveProcessingLeases.delete(lease.leaseId)- await this.cleanupSettledFailedTransfers(callerIdentity)- })- }-/** Caller/task lifecycle must prove consumer settlement before invoking this. */async releaseReceiptOwner(ownerId: string, callerIdentity: string): Promise<void> {this.assertInitialized()@@ -1072,30 +946,17 @@ export class GfsDownloadStore {if (!this.initialized) returnthis.closing = trueconst deadline = Date.now() + drainTimeoutMs- while (- (this.activeIds.size > 0 ||- this.liveProcessingLeases.size > 0 ||- this.hasLiveReceiptOwners()) &&- Date.now() < deadline- ) {+ while ((this.activeIds.size > 0 || this.hasLiveReceiptOwners()) && Date.now() < deadline) {await new Promise(resolve => setTimeout(resolve, 25))}- if (- this.activeIds.size > 0 ||- this.liveProcessingLeases.size > 0 ||- this.hasLiveReceiptOwners()- ) {+ if (this.activeIds.size > 0 || this.hasLiveReceiptOwners()) {this.closing = falsethrow new GfsDownloadStoreError('download_busy')}await this.mutationTail// Work already queued before shutdown can finish admission while close// waits. Never release writer ownership over a newly active operation.- if (- this.activeIds.size > 0 ||- this.liveProcessingLeases.size > 0 ||- this.hasLiveReceiptOwners()- ) {+ if (this.activeIds.size > 0 || this.hasLiveReceiptOwners()) {this.closing = falsethrow new GfsDownloadStoreError('download_busy')}@@ -1111,15 +972,13 @@ export class GfsDownloadStore {}async cleanupExpired(now = Date.now()): Promise<void> {- this.assertInitialized(true)- if (this.hasInheritedExecutors()) return+ this.assertInitialized()try {await this.serialize(async () => {for (const record of Object.values(this.ledger.records)) {if (Date.parse(record.expiresAt) > now ||this.activeIds.has(record.id) ||- this.hasProcessingLease(record.id) ||this.hasReceiptOwner(record.id) ||record.state === 'quarantined')@@ -1140,66 +999,12 @@ export class GfsDownloadStore {}}- private async cleanupUnconsumedForAdmission(callerIdentity: string, now: number): Promise<void> {- for (const record of Object.values(this.ledger.records)) {- if (- record.callerIdentity !== callerIdentity ||- Date.parse(record.expiresAt) > now ||- this.activeIds.has(record.id) ||- this.hasProcessingLease(record.id) ||- this.hasReceiptOwner(record.id) ||- record.state === 'quarantined' ||- !['completed', 'missing'].includes(record.state)- )- continue- let safe = false- if (record.state === 'completed') {- try {- await this.verifyRecordContent(record)- safe = true- } catch {- /* Verify absence below. */- }- }- if (!safe) {- const directory = path.join(this.hostRoot, record.directory)- try {- // An empty, owned directory or its positive absence has no remaining- // copy to consume; unknown bytes, partials and inaccessible state stay charged.- await this.verifyManagedDirectory(record)- safe = (await fs.readdir(directory)).length === 0- } catch (error) {- safe = (error as NodeJS.ErrnoException).code === 'ENOENT'- }- }- if (!safe || !(await this.removeRecordDirectory(record))) continue- await this.releaseRecordCharge(record)- recordGfsDownloadExpiry('expired_removed')- }- }-- private protectFutureRecord(record: GfsDownloadRecord): void {- const next = { ...this.ledger.processingLeases }- for (const [leaseId, lease] of Object.entries(next)) {- if (- lease.callerIdentity !== record.callerIdentity ||- lease.writerSessionId !== this.writerSessionId ||- !this.liveProcessingLeases.has(leaseId)- )- continue- next[leaseId] = { ...lease, recordIds: [...new Set([...lease.recordIds, record.id])] }- }- // Committed atomically with the reservation, before its path is exposed.- this.ledger.processingLeases = next- }-private async cleanupSettledFailedTransfers(callerIdentity: string): Promise<void> {for (const record of Object.values(this.ledger.records)) {if (record.callerIdentity !== callerIdentity ||record.state !== 'cleanup_failed' ||this.activeIds.has(record.id) ||- this.hasProcessingLease(record.id) ||this.hasReceiptOwner(record.id))continue@@ -1258,7 +1063,6 @@ export class GfsDownloadStore {record =>record.state === 'completed' &&!this.activeIds.has(record.id) &&- !this.hasProcessingLease(record.id) &&!this.hasReceiptOwner(record.id)).sort(@@ -1371,16 +1175,58 @@ export class GfsDownloadStore {}}+ /**+ * #1019: shell_exec no longer holds processing leases, so no lease in a+ * ledger written by an earlier build protects an executor of this boot. A+ * lease left by a crashed Host would otherwise fence the store forever. The+ * field is removed durably before reconciliation; the warning and the+ * counter are emitted only after that removal is persisted, so a failed+ * persist leaves the ledger untouched and initialize rejects.+ */+ private async discardLegacyProcessingLeases(): Promise<void> {+ const legacy = this.ledger.processingLeases+ if (legacy === undefined) return+ const discarded = Object.keys(legacy).length+ delete this.ledger.processingLeases+ // An empty legacy map is dropped by the final initialize persist.+ if (discarded === 0) return+ try {+ await this.persist()+ } catch (error) {+ this.ledger.processingLeases = legacy+ throw error+ }+ logger.warn(+ { discarded },+ `GFS download store discarded ${discarded} legacy processing lease(s) at initialize`+ )+ recordGfsLegacyProcessingLeasesDiscarded(discarded)+ }++ /**+ * Records an earlier boot quarantined stay charged to quota (`usageFor`).+ * They are reported so an operator can recover them; this boot never+ * reuses or deletes them.+ */+ private reportInheritedQuarantine(): void {+ const quarantined = Object.values(this.ledger.records).filter(+ record => record.state === 'quarantined'+ ).length+ if (quarantined === 0) return+ logger.warn(+ { quarantined },+ `GFS download store found ${quarantined} record(s) quarantined by an earlier boot; they stay charged to quota until operator recovery`+ )+ recordGfsInheritedQuarantinedRecords(quarantined)+ }+private async reconcile(): Promise<void> {for (const record of Object.values(this.ledger.records)) {// Kernel ownership recovery proves only the previous Host writer exited.- // A detached executor can survive it, and may still read/write its paths.- // Keep all inherited executions and unfinished/uncertain transfers charged.- if (- this.hasInheritedExecutors() ||- record.state !== 'completed' ||- this.hasProcessingLease(record.id)- ) {+ // Unfinished/uncertain transfers stay charged. A completed copy is reused+ // only after its content re-verifies: a shell command can have altered it+ // (#1019 accepted risk), and an altered copy is quarantined.+ if (record.state !== 'completed') {record.state = 'quarantined'continue}@@ -1499,19 +1345,6 @@ export class GfsDownloadStore {return record}- private hasInheritedExecutors(): boolean {- return Object.values(this.ledger.processingLeases ?? {}).some(- lease => lease.writerSessionId !== this.writerSessionId- )- }-- private hasProcessingLease(recordId: string): boolean {- // Deadline is admission/output budgeting, never physical-termination proof.- return Object.values(this.ledger.processingLeases ?? {}).some(lease =>- lease.recordIds.includes(recordId)- )- }-private releaseActive(record: GfsDownloadRecord): void {if (!this.activeIds.has(record.id)) returnthis.active = Math.max(0, this.active - 1)@@ -1545,14 +1378,9 @@ export class GfsDownloadStore {throw new GfsDownloadStoreError('publication_cancelled')}- private assertInitialized(allowRecovery = false): void {+ private assertInitialized(): void {if (!this.initialized) throw new GfsDownloadStoreError('workspace_unavailable')if (this.unsafe) throw new GfsDownloadStoreError('storage_write_failed')- // Stage 1 executors share the Host UID. Their surviving process can reach- // future paths beyond its caller cwd, so recovery fences managed work for- // the entire Host until physical settlement has been established.- if (!allowRecovery && this.hasInheritedExecutors())- throw new GfsDownloadStoreError('download_busy')try {this.writerLease?.assertHeld()} catch {diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarialQuota.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarialQuota.test.tsindex f58b28606..64bad0adf 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreAdversarialQuota.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarialQuota.test.ts@@ -3,16 +3,26 @@ import { createHash } from 'node:crypto'import * as fs from 'node:fs/promises'import { tmpdir } from 'node:os'import * as path from 'node:path'+import { ShellTool } from '../core/tools/shell'import { GfsDownloadStore } from './gfsDownloadStore'import { GFS_FILE_LIMITS } from './gfsFilePolicy'+const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))+// Physical capacity is a deterministic filesystem boundary for the capacity+// block below. Every other call, and every other test, uses the real statfs.+vi.mock('node:fs/promises', async original => ({+ ...(await original<typeof fs>()),+ statfs: statfsBoundary,+}))+let nativeFs: typeof fs+/*** Adversarial quota/reuse/rollback regression probes.** These are mutation-sensitive on purpose: each block fails when its guard is* removed from gfsDownloadStore.ts. Pressure eviction is expected, so every- * denial assertion keeps its targets protected with an explicit receipt owner- * or a live processing lease; positive blocks assert the earliest-created+ * denial assertion keeps its targets protected with an explicit receipt owner;+ * a shell run never protects a copy (#1019). Positive blocks assert the earliest-created* eligible copy is the one reclaimed.*/@@ -150,6 +160,9 @@ async function seedCrossCallerPressure(}beforeEach(async () => {+ nativeFs = await vi.importActual<typeof fs>('node:fs/promises')+ statfsBoundary.mockReset()+ statfsBoundary.mockImplementation(nativeFs.statfs)hostRoot = await fs.mkdtemp(path.join(tmpdir(), 'gfs-adversarial-quota-'))callerRoot = await callerDirectory(hostRoot, CALLER)store = new GfsDownloadStore(hostRoot)@@ -381,29 +394,31 @@ describe('GFS download store adversarial quota', () => {await store.releaseReceiptOwner('filler-owner', CALLER)})- it('protects copies held by a live processing lease from pressure eviction', async () => {+ it('evicts a copy a shell has just read: a shell run holds no protection (#1019)', async () => {const size = GFS_FILE_LIMITS.inlineTextBytes + 1const receipts = []for (let index = 1; index <= GFS_FILE_LIMITS.callerRetainedFiles; index += 1)receipts.push(await completedCopy(store, callerRoot, CALLER, index, 1, size))+ const ids = receipts.map(receipt => receipt.id)+ await forceAgeOrder(store, ids)- const provider = store.processingLeaseProvider(CALLER)- const lease = await provider.acquireProcessingLease({ durationMs: 60_000 })- const pressure = {+ const shell = new ShellTool(callerRoot, 5_000, ['PATH'], () => ({}), undefined, true)+ const read = await shell.execute({+ command: `wc -c < ${JSON.stringify(receipts[0]!.path)}`,+ })+ expect(read.is_error).toBe(false)+ expect(read.content).toMatch(new RegExp(`\\b${size}\\b`))++ const next = await store.createTransfer({callerIdentity: CALLER,callerWorkspacePath: callerRoot,source: sourceFor(92, 1),sizeBytes: size,expiresAt: future(),- }- await expect(store.createTransfer(pressure)).rejects.toMatchObject({- code: 'caller_quota_exceeded',})- for (const receipt of receipts)- expect(store.debugRecord(receipt.id)).toMatchObject({ state: 'completed' })-- await provider.releaseProcessingLease(lease)- const next = await store.createTransfer(pressure)+ expect(store.debugRecord(ids[0]!)).toBeUndefined()+ for (const id of ids.slice(1))+ expect(store.debugRecord(id)).toMatchObject({ state: 'completed' })expect(store.debugUsage()).toMatchObject({ files: GFS_FILE_LIMITS.callerRetainedFiles })await store.fail(next.id, CALLER)})@@ -434,7 +449,7 @@ describe('GFS download store adversarial quota', () => {await store.fail(next.id, CALLER)})- it('fences the host on an inherited executor and adopts a verified inherited pin only after revalidation', async () => {+ it('discards inherited legacy leases and adopts a verified inherited pin only after revalidation', async () => {const receipt = await completedCopy(store, callerRoot, CALLER, 1, 1, 7)await store.close()@@ -443,8 +458,8 @@ describe('GFS download store adversarial quota', () => {const writeLedger = async (value: unknown) => fs.writeFile(ledgerPath, JSON.stringify(value))const foreignSession = '00000000-0000-4000-8000-000000000000'- // Phase 1: an inherited PROCESSING LEASE is an unknown executor. The Host- // fences, every managed copy quarantines, and no admission or release runs.+ // Phase 1: a legacy PROCESSING LEASE written before #1019 that protected a+ // copy. It is discarded at initialize; the copy re-verifies and stays usable.let ledger = await readLedger()ledger.processingLeases = {'11111111-1111-4111-8111-111111111111': {@@ -460,29 +475,25 @@ describe('GFS download store adversarial quota', () => {let reopened = new GfsDownloadStore(hostRoot)await reopened.initialize()- expect(reopened.debugRecord(receipt.id)).toMatchObject({ state: 'quarantined' })- expect(reopened.isAvailable()).toBe(false)+ expect(reopened.isAvailable()).toBe(true)+ expect(reopened.debugRecord(receipt.id)).toMatchObject({ state: 'completed' })expect(reopened.debugUsage()).toMatchObject({ bytes: 7, files: 1 })- await expect(reopened.reusableReceipt(CALLER, sourceFor(1, 1), 7)).rejects.toMatchObject({- code: 'download_busy',+ expect(await readLedger()).not.toHaveProperty('processingLeases')+ await expect(reopened.reusableReceipt(CALLER, sourceFor(1, 1), 7)).resolves.toMatchObject({+ id: receipt.id,})- await expect(- reopened.createTransfer({- callerIdentity: CALLER,- callerWorkspacePath: callerRoot,- source: sourceFor(2, 1),- sizeBytes: 7,- expiresAt: future(),- })- ).rejects.toMatchObject({ code: 'download_busy' })- await expect(reopened.releaseReceiptOwner('carry-owner', CALLER)).rejects.toMatchObject({- code: 'download_busy',+ const admitted = await reopened.createTransfer({+ callerIdentity: CALLER,+ callerWorkspacePath: callerRoot,+ source: sourceFor(2, 1),+ sizeBytes: 7,+ expiresAt: future(),})- await reopened.cleanupExpired(Date.now() + GFS_FILE_LIMITS.retentionMs * 10)- expect(reopened.debugRecord(receipt.id)).toMatchObject({ state: 'quarantined' })+ await reopened.fail(admitted.id, CALLER)await reopened.close()- // Phase 2: even an empty inherited lease fences the Host.+ // Phase 2: an empty inherited lease, the shape that fenced the Host forever+ // before #1019, is discarded the same way.ledger = await readLedger()ledger.processingLeases = {'22222222-2222-4222-8222-222222222222': {@@ -497,8 +508,9 @@ describe('GFS download store adversarial quota', () => {await writeLedger(ledger)reopened = new GfsDownloadStore(hostRoot)await reopened.initialize()- expect(reopened.isAvailable()).toBe(false)- expect(reopened.debugRecord(receipt.id)).toMatchObject({ state: 'quarantined' })+ expect(reopened.isAvailable()).toBe(true)+ expect(reopened.debugRecord(receipt.id)).toMatchObject({ state: 'completed' })+ expect(await readLedger()).not.toHaveProperty('processingLeases')await reopened.close()// Phase 3: receipt-only inherited pins are not executors. Integrity-checked@@ -642,3 +654,235 @@ describe('GFS download store adversarial quota', () => {})})})++const MARGIN = 16 * 1024 * 1024+const capacitySource = sourceFor(200, 7)++/** Physical-capacity admission, ported from the deleted lease-capacity suite (#1019). */+describe('GFS download store physical capacity and reclaim', () => {+ const capacityStores: GfsDownloadStore[] = []+ const capacityRoots: string[] = []++ afterEach(async () => {+ vi.restoreAllMocks()+ statfsBoundary.mockImplementation(nativeFs.statfs)+ for (const target of capacityStores.splice(0))+ await target.close(0).catch(async () => {+ await (+ target as unknown as { writerLease?: { release(): Promise<void> } }+ ).writerLease?.release()+ })+ for (const root of capacityRoots.splice(0))+ await nativeFs.rm(root, { recursive: true, force: true })+ })++ async function setup(extra: Record<string, string> = {}) {+ const { Store } = await loadLimitedStore({+ MCP_HOST_GFS_MAX_FILE_BYTES: '20',+ MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: '100',+ MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: '50',+ ...extra,+ })+ const root = await nativeFs.mkdtemp(path.join(tmpdir(), 'gfs-capacity-'))+ capacityRoots.push(root)+ const target = new Store(root)+ capacityStores.push(target)+ await target.initialize()+ return { root, store: target }+ }++ async function copy(+ target: GfsDownloadStore,+ root: string,+ identity: string,+ bytes = 7,+ owner?: string+ ) {+ const transfer = await target.createTransfer({+ callerIdentity: identity,+ callerWorkspacePath: root,+ source: capacitySource,+ sizeBytes: bytes,+ expiresAt: future(),+ retentionOwnerId: owner,+ })+ const body = Buffer.alloc(bytes, 65)+ await nativeFs.writeFile(path.join(root, transfer.partialPath), body)+ return target.publish(transfer.id, identity, createHash('sha256').update(body).digest('hex'))+ }++ async function capacity(root: string, bytes: number) {+ const observed = await nativeFs.statfs(root, { bigint: true })+ statfsBoundary.mockResolvedValue({+ ...observed,+ bsize: 4096n,+ bavail: BigInt(Math.floor(bytes / 4096)),+ })+ }++ it('rejects impossible physical margin before evicting another caller at a logical quota boundary', async () => {+ const { root, store: target } = await setup()+ const otherRoot = await callerDirectory(root, 'caller-b')+ const receipts = []+ for (let index = 0; index < 5; index++)+ receipts.push(await copy(target, otherRoot, 'caller-b', 10))+ const requestRoot = await callerDirectory(root, 'caller-a')+ // Add durable, protected charges to make aggregate 100 while requester has room.+ for (const identity of ['caller-c', 'caller-d', 'caller-e', 'caller-f', 'caller-g'])+ await copy(target, await callerDirectory(root, identity), identity, 10, `${identity}-owner`)+ await capacity(root, 0)+ await expect(+ target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: requestRoot,+ source: capacitySource,+ sizeBytes: 20,+ expiresAt: future(),+ })+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })+ expect(target.debugUsage()).toMatchObject({ bytes: 100, files: 10 })+ for (const receipt of receipts) expect(target.debugRecord(receipt.id)).toBeDefined()+ for (const identity of ['caller-c', 'caller-d', 'caller-e', 'caller-f', 'caller-g'])+ await target.releaseReceiptOwner(`${identity}-owner`, identity)+ })++ it('keeps all default-cap small copies when current physical capacity is zero', async () => {+ const { root, store: target } = await setup({+ MCP_HOST_GFS_MAX_FILE_BYTES: '1024',+ MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: String(1024 * 1024),+ MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: String(128 * 1024),+ })+ const receipts = []+ for (let index = 0; index < 8; index++) {+ const identity = `caller-${index}`+ const directory = await callerDirectory(root, identity)+ for (let file = 0; file < 8; file++) receipts.push(await copy(target, directory, identity, 1))+ }+ await capacity(root, 0)+ await expect(+ target.createTransfer({+ callerIdentity: 'caller-new',+ callerWorkspacePath: await callerDirectory(root, 'caller-new'),+ source: capacitySource,+ sizeBytes: 1,+ expiresAt: future(),+ })+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })+ expect(target.debugUsage().files).toBe(64)+ for (const receipt of receipts) expect(target.debugRecord(receipt.id)).toBeDefined()+ })++ it('respects pending active reservations before any cache-pressure effect', async () => {+ const { root, store: target } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })+ const cachedRoot = await callerDirectory(root, 'caller-a')+ const receipt = await copy(target, cachedRoot, 'caller-a', 7)+ const active = await target.createTransfer({+ callerIdentity: 'caller-b',+ callerWorkspacePath: await callerDirectory(root, 'caller-b'),+ source: capacitySource,+ sizeBytes: 7,+ expiresAt: future(),+ })+ await capacity(root, MARGIN + 4096)+ await expect(+ target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: cachedRoot,+ source: capacitySource,+ sizeBytes: 7,+ expiresAt: future(),+ })+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })+ expect(target.debugRecord(receipt.id)).toBeDefined()+ await target.fail(active.id, 'caller-b')+ })++ it('never credits sparse apparent bytes as available physical capacity', async () => {+ const bytes = 16 * 1024 * 1024+ const { root, store: target } = await setup({+ MCP_HOST_GFS_MAX_FILE_BYTES: String(bytes),+ MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: String(bytes * 4),+ MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: String(bytes * 2),+ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1',+ })+ const sparseRoot = await callerDirectory(root, 'caller-a')+ const transfer = await target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: sparseRoot,+ source: capacitySource,+ sizeBytes: bytes,+ expiresAt: future(),+ })+ const handle = await nativeFs.open(path.join(sparseRoot, transfer.partialPath), 'r+')+ await handle.truncate(bytes)+ await handle.close()+ const sparse = await nativeFs.stat(path.join(sparseRoot, transfer.partialPath))+ expect(sparse.blocks * 512).toBeLessThan(sparse.size)+ const receipt = await target.publish(+ transfer.id,+ 'caller-a',+ createHash('sha256').update(Buffer.alloc(bytes)).digest('hex')+ )+ await capacity(root, 0)+ await expect(+ target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: sparseRoot,+ source: capacitySource,+ sizeBytes: 7,+ expiresAt: future(),+ })+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })+ expect(target.debugRecord(receipt.id)).toBeDefined()+ expect((await nativeFs.stat(path.join(sparseRoot, receipt.path))).size).toBe(bytes)+ })++ it('refuses admission if real capacity changes after a positively settled victim', async () => {+ const { root, store: target } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })+ const victimRoot = await callerDirectory(root, 'caller-a')+ const receipt = await copy(target, victimRoot, 'caller-a', 7)+ const directory = path.join(victimRoot, path.dirname(receipt.path))+ const observed = await nativeFs.statfs(root, { bigint: true })+ statfsBoundary.mockImplementation(async () => {+ try {+ await nativeFs.lstat(directory)+ return observed+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error+ return { ...observed, bavail: 0n }+ }+ })+ await expect(+ target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: victimRoot,+ source: capacitySource,+ sizeBytes: 7,+ expiresAt: future(),+ })+ ).rejects.toMatchObject({ code: 'host_quota_exceeded' })+ expect(target.debugUsage().files).toBe(0)+ await expect(nativeFs.lstat(directory)).rejects.toMatchObject({ code: 'ENOENT' })+ })++ it('reclaims logical quota and admits when real physical capacity is already sufficient', async () => {+ const { root, store: target } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })+ const reclaimRoot = await callerDirectory(root, 'caller-a')+ const receipt = await copy(target, reclaimRoot, 'caller-a', 7)+ const before = await nativeFs.statfs(root, { bigint: true })+ expect(before.bavail * before.bsize).toBeGreaterThan(BigInt(MARGIN + 4096))+ const next = await target.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: reclaimRoot,+ source: capacitySource,+ sizeBytes: 7,+ expiresAt: future(),+ })+ expect(target.debugRecord(receipt.id)).toBeUndefined()+ await expect(+ nativeFs.lstat(path.join(reclaimRoot, path.dirname(receipt.path)))+ ).rejects.toMatchObject({ code: 'ENOENT' })+ expect(target.debugRecord(next.id)).toBeDefined()+ await target.fail(next.id, 'caller-a')+ })+})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.tsdeleted file mode 100644index 6f54720c5..000000000--- a/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts+++ /dev/null@@ -1,452 +0,0 @@-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { type ChildProcess, spawn } from 'node:child_process'-import { createHash } from 'node:crypto'-import * as fs from 'node:fs/promises'-import { tmpdir } from 'node:os'-import * as path from 'node:path'-import type { GfsDownloadStore } from './gfsDownloadStore'--const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))-// Capacity is a deterministic filesystem boundary. Directory/file effects and-// their physical allocation remain real in each isolated temporary store.-vi.mock('node:fs/promises', async original => ({- ...(await original<typeof fs>()),- statfs: statfsBoundary,-}))-const MARGIN = 16 * 1024 * 1024-const source = {- kind: 'gfs' as const,- drive: 'main',- resourceId: 'a'.repeat(32),- gfsUri: `gfs://main/${'a'.repeat(32)}`,- name: 'input.csv',- version: 7,-}-let nativeFs: typeof fs-let roots: string[]-let stores: GfsDownloadStore[]-let children: ChildProcess[]-beforeEach(async () => {- nativeFs = await vi.importActual<typeof fs>('node:fs/promises')- statfsBoundary.mockImplementation(nativeFs.statfs)- roots = []- stores = []- children = []-})-afterEach(async () => {- vi.restoreAllMocks()- statfsBoundary.mockImplementation(nativeFs.statfs)- for (const child of children)- if (child.exitCode === null && child.signalCode === null)- await new Promise<void>(resolve => {- child.once('exit', () => resolve())- child.kill('SIGKILL')- })- for (const store of stores)- await store.close(0).catch(async () => {- await (- store as unknown as { writerLease?: { release(): Promise<void> } }- ).writerLease?.release()- })- for (const root of roots) await nativeFs.rm(root, { recursive: true, force: true })-})-async function setup(extra: Record<string, string> = {}) {- const keys = {- MCP_HOST_GFS_MAX_FILE_BYTES: '20',- MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: '100',- MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: '50',- ...extra,- }- const previous = new Map(Object.keys(keys).map(key => [key, process.env[key]]))- Object.assign(process.env, keys)- vi.resetModules()- const { GfsDownloadStore: Store } = await import('./gfsDownloadStore')- for (const [key, value] of previous) {- if (value === undefined) delete process.env[key]- else process.env[key] = value- }- const root = await nativeFs.mkdtemp(path.join(tmpdir(), 'gfs-future-capacity-'))- roots.push(root)- const store = new Store(root)- stores.push(store)- await store.initialize()- return { root, store }-}-async function caller(root: string, identity: string) {- const directory = path.join(root, 'users', identity)- await nativeFs.mkdir(directory, { recursive: true, mode: 0o700 })- return directory-}-async function copy(- store: GfsDownloadStore,- callerRoot: string,- identity: string,- bytes = 7,- owner?: string-) {- const transfer = await store.createTransfer({- callerIdentity: identity,- callerWorkspacePath: callerRoot,- source,- sizeBytes: bytes,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- retentionOwnerId: owner,- })- const body = Buffer.alloc(bytes, 65)- await nativeFs.writeFile(path.join(callerRoot, transfer.partialPath), body)- return store.publish(transfer.id, identity, createHash('sha256').update(body).digest('hex'))-}-async function capacity(root: string, bytes: number) {- const observed = await nativeFs.statfs(root, { bigint: true })- statfsBoundary.mockResolvedValue({- ...observed,- bsize: 4096n,- bavail: BigInt(Math.floor(bytes / 4096)),- })-}-async function ledger(root: string) {- return JSON.parse(- await nativeFs.readFile(path.join(root, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )-}-async function executor(callerRoot: string) {- const child = spawn(- process.execPath,- [- '-e',- `const fs=require('node:fs');process.send({ready:true});process.on('message',m=>{try{process.send({body:fs.readFileSync(m.path,'utf8')})}catch(e){process.send({error:e.code})}})`,- ],- { cwd: callerRoot, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }- )- children.push(child)- await new Promise(resolve => child.once('message', resolve))- return child-}-async function readFromExecutor(child: ChildProcess, filename: string) {- return new Promise<any>(resolve => {- child.once('message', resolve)- child.send({ path: filename })- })-}-async function settleExecutor(child: ChildProcess) {- await new Promise<void>(resolve => {- child.once('exit', () => resolve())- child.kill('SIGTERM')- })-}--describe('future execution protection and whole admission capacity', () => {- it('protects a future published copy through an already-running empty lease and owner release', async () => {- const { root, store } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })- const callerRoot = await caller(root, 'caller-a')- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 500 })- const child = await executor(callerRoot)- const receipt = await copy(store, callerRoot, 'caller-a', 7, 'future-owner')- await store.releaseReceiptOwner('future-owner', 'caller-a')- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'caller_quota_exceeded' })- expect((await ledger(root)).processingLeases[lease.leaseId].recordIds).toContain(receipt.id)- expect(await readFromExecutor(child, path.join(callerRoot, receipt.path))).toEqual({- body: 'AAAAAAA',- })- await store.cleanupExpired(- Math.max(Date.parse(lease.expiresAt), Date.parse(receipt.expiresAt)) + 1- )- expect(store.debugUsage().files).toBe(1)- await expect(store.close(0)).rejects.toMatchObject({ code: 'download_busy' })- await settleExecutor(child)- await provider.releaseProcessingLease(lease)- })-- it('settles a protected failed producer without freeing its bytes or occupying the active slot', async () => {- const { root, store } = await setup()- const callerRoot = await caller(root, 'caller-a')- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease()- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- retentionOwnerId: 'failed-owner',- })- await nativeFs.writeFile(path.join(callerRoot, transfer.partialPath), 'partial')- await store.fail(transfer.id, 'caller-a')- expect(store.debugRecord(transfer.id)).toMatchObject({ state: 'cleanup_failed', sizeBytes: 7 })- expect(store.debugUsage().files).toBe(1)- await store.releaseReceiptOwner('failed-owner', 'caller-a')- expect(await nativeFs.readFile(path.join(callerRoot, transfer.partialPath), 'utf8')).toBe(- 'partial'- )- const next = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- await store.fail(next.id, 'caller-a')- await provider.releaseProcessingLease(lease)- expect(store.debugUsage().files).toBe(0)- await store.close(0)- })-- it('covers an already-transferring path when a new execution lease is admitted', async () => {- const { root, store } = await setup()- const callerRoot = await caller(root, 'caller-a')- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- await nativeFs.writeFile(path.join(callerRoot, transfer.partialPath), 'partial')- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease()- expect((await ledger(root)).processingLeases[lease.leaseId].recordIds).toContain(transfer.id)- await store.fail(transfer.id, 'caller-a')- expect(await nativeFs.readFile(path.join(callerRoot, transfer.partialPath), 'utf8')).toBe(- 'partial'- )- await provider.releaseProcessingLease(lease)- expect(store.debugUsage().files).toBe(0)- })-- it('rolls back future lease references when reservation persistence fails before exposure', async () => {- const { root, store } = await setup()- const callerRoot = await caller(root, 'caller-a')- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease()- const before = await ledger(root)- // Inject a failure before the real journal producer starts; no directory,- // record ID or consumer path has been physically exposed.- vi.spyOn(store as unknown as { persist(): Promise<void> }, 'persist').mockRejectedValueOnce(- new Error('reservation unavailable')- )- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- retentionOwnerId: 'unpublished-owner',- })- ).rejects.toThrow('reservation unavailable')- expect(store.debugUsage().files).toBe(0)- expect(await ledger(root)).toEqual(before)- await expect(nativeFs.lstat(path.join(callerRoot, '.gfs-downloads'))).rejects.toMatchObject({- code: 'ENOENT',- })- const privateStore = store as unknown as {- ledger: { processingLeases: Record<string, { recordIds: string[] }> }- }- expect(privateStore.ledger.processingLeases[lease.leaseId].recordIds).toEqual([])- await provider.releaseProcessingLease(lease)- })-- it('settles cancelled publication while a preexisting executor retains its partial protection', async () => {- const { root, store } = await setup()- const callerRoot = await caller(root, 'caller-a')- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease()- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- await nativeFs.writeFile(path.join(callerRoot, transfer.partialPath), 'partial')- const controller = new AbortController()- controller.abort()- await expect(- store.publish(transfer.id, 'caller-a', createHash('sha256').update('partial').digest('hex'), {- signal: controller.signal,- })- ).rejects.toMatchObject({ code: 'publication_cancelled' })- await store.fail(transfer.id, 'caller-a')- expect(store.debugRecord(transfer.id)).toMatchObject({ state: 'cleanup_failed', sizeBytes: 7 })- expect((await ledger(root)).processingLeases[lease.leaseId].recordIds).toContain(transfer.id)- await expect(store.close(0)).rejects.toMatchObject({ code: 'download_busy' })- await provider.releaseProcessingLease(lease)- expect(store.debugUsage().files).toBe(0)- await store.close(0)- })-- it('rejects impossible physical margin before evicting another caller at a logical quota boundary', async () => {- const { root, store } = await setup()- const otherRoot = await caller(root, 'caller-b')- const receipts = []- for (let index = 0; index < 5; index++)- receipts.push(await copy(store, otherRoot, 'caller-b', 10))- const requestRoot = await caller(root, 'caller-a')- // Add durable, protected charges to make aggregate 100 while requester has room.- for (const identity of ['caller-c', 'caller-d', 'caller-e', 'caller-f', 'caller-g'])- await copy(store, await caller(root, identity), identity, 10, `${identity}-owner`)- await capacity(root, 0)- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: requestRoot,- source,- sizeBytes: 20,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'host_quota_exceeded' })- expect(store.debugUsage()).toMatchObject({ bytes: 100, files: 10 })- for (const receipt of receipts) expect(store.debugRecord(receipt.id)).toBeDefined()- for (const identity of ['caller-c', 'caller-d', 'caller-e', 'caller-f', 'caller-g'])- await store.releaseReceiptOwner(`${identity}-owner`, identity)- })-- it('keeps all default-cap small copies when current physical capacity is zero', async () => {- const { root, store } = await setup({- MCP_HOST_GFS_MAX_FILE_BYTES: '1024',- MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: String(1024 * 1024),- MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: String(128 * 1024),- })- const receipts = []- for (let index = 0; index < 8; index++) {- const identity = `caller-${index}`- const directory = await caller(root, identity)- for (let file = 0; file < 8; file++) receipts.push(await copy(store, directory, identity, 1))- }- await capacity(root, 0)- await expect(- store.createTransfer({- callerIdentity: 'caller-new',- callerWorkspacePath: await caller(root, 'caller-new'),- source,- sizeBytes: 1,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'host_quota_exceeded' })- expect(store.debugUsage().files).toBe(64)- for (const receipt of receipts) expect(store.debugRecord(receipt.id)).toBeDefined()- })-- it('respects pending active reservations before any cache-pressure effect', async () => {- const { root, store } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })- const cachedRoot = await caller(root, 'caller-a')- const receipt = await copy(store, cachedRoot, 'caller-a', 7)- const active = await store.createTransfer({- callerIdentity: 'caller-b',- callerWorkspacePath: await caller(root, 'caller-b'),- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- await capacity(root, MARGIN + 4096)- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: cachedRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'host_quota_exceeded' })- expect(store.debugRecord(receipt.id)).toBeDefined()- await store.fail(active.id, 'caller-b')- })-- it('never credits sparse apparent bytes as available physical capacity', async () => {- const bytes = 16 * 1024 * 1024- const { root, store } = await setup({- MCP_HOST_GFS_MAX_FILE_BYTES: String(bytes),- MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES: String(bytes * 4),- MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES: String(bytes * 2),- MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1',- })- const callerRoot = await caller(root, 'caller-a')- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: bytes,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- const handle = await nativeFs.open(path.join(callerRoot, transfer.partialPath), 'r+')- await handle.truncate(bytes)- await handle.close()- const sparse = await nativeFs.stat(path.join(callerRoot, transfer.partialPath))- expect(sparse.blocks * 512).toBeLessThan(sparse.size)- const receipt = await store.publish(- transfer.id,- 'caller-a',- createHash('sha256').update(Buffer.alloc(bytes)).digest('hex')- )- await capacity(root, 0)- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'host_quota_exceeded' })- expect(store.debugRecord(receipt.id)).toBeDefined()- expect((await nativeFs.stat(path.join(callerRoot, receipt.path))).size).toBe(bytes)- })-- it('refuses admission if real capacity changes after a positively settled victim', async () => {- const { root, store } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })- const callerRoot = await caller(root, 'caller-a')- const receipt = await copy(store, callerRoot, 'caller-a', 7)- const directory = path.join(callerRoot, path.dirname(receipt.path))- const observed = await nativeFs.statfs(root, { bigint: true })- statfsBoundary.mockImplementation(async () => {- try {- await nativeFs.lstat(directory)- return observed- } catch (error) {- if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error- return { ...observed, bavail: 0n }- }- })- await expect(- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- ).rejects.toMatchObject({ code: 'host_quota_exceeded' })- expect(store.debugUsage().files).toBe(0)- await expect(nativeFs.lstat(directory)).rejects.toMatchObject({ code: 'ENOENT' })- })-- it('reclaims logical quota and admits when real physical capacity is already sufficient', async () => {- const { root, store } = await setup({ MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES: '1' })- const callerRoot = await caller(root, 'caller-a')- const receipt = await copy(store, callerRoot, 'caller-a', 7)- const before = await nativeFs.statfs(root, { bigint: true })- expect(before.bavail * before.bsize).toBeGreaterThan(BigInt(MARGIN + 4096))- const next = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- expect(store.debugRecord(receipt.id)).toBeUndefined()- await expect(- nativeFs.lstat(path.join(callerRoot, path.dirname(receipt.path)))- ).rejects.toMatchObject({ code: 'ENOENT' })- expect(store.debugRecord(next.id)).toBeDefined()- await store.fail(next.id, 'caller-a')- })-})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreProcessingLease.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreProcessingLease.test.tsdeleted file mode 100644index 81a1f27e7..000000000--- a/mcp-host/src/internalTools/gfsDownloadStoreProcessingLease.test.ts+++ /dev/null@@ -1,498 +0,0 @@-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { createHash } from 'node:crypto'-import * as fs from 'node:fs'-import * as os from 'node:os'-import * as path from 'node:path'-import type { GfsImageSource } from '../visualInput/policy'-import { GfsDownloadStore } from './gfsDownloadStore'--const source: GfsImageSource = {- kind: 'gfs',- drive: 'main',- resourceId: '0123456789abcdef0123456789abcdef',- gfsUri: 'gfs://main/0123456789abcdef0123456789abcdef',- name: 'input.csv',- version: 7,-}--describe('GfsDownloadStore processing leases', () => {- let hostRoot: string- let callerRoot: string- let store: GfsDownloadStore-- beforeEach(() => {- hostRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'gfs-processing-lease-'))- callerRoot = path.join(hostRoot, 'users', 'caller-a')- fs.mkdirSync(callerRoot, { recursive: true, mode: 0o700 })- store = new GfsDownloadStore(hostRoot)- })-- afterEach(async () => {- vi.restoreAllMocks()- await store.close(0).catch(async () => {- // No fixture executor survives teardown; release the test-owned kernel lock.- await (- store as unknown as { writerLease?: { release(): Promise<void> } }- ).writerLease?.release()- })- fs.rmSync(hostRoot, { recursive: true, force: true })- })-- async function publishFixture(- expiresAt = new Date(Date.now() + 60_000).toISOString(),- retentionOwnerId?: string- ) {- await store.initialize()- const bytes = Buffer.from('fixture')- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: bytes.byteLength,- expiresAt,- retentionOwnerId,- })- fs.writeFileSync(path.join(callerRoot, transfer.partialPath), bytes)- return store.publish(transfer.id, 'caller-a', createHash('sha256').update(bytes).digest('hex'))- }-- it('acquires and releases a caller-bound durable lease', async () => {- const receipt = await publishFixture()- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 60_000 })-- expect(lease.leaseId).toMatch(/^[0-9a-f-]{36}$/)- expect(lease.expiresAt).toBeDefined()- await provider.releaseProcessingLease(lease)- expect(store.debugRecord(receipt.id)).toBeDefined()- })-- it('rejects release by another caller', async () => {- await publishFixture()- const lease = await store.processingLeaseProvider('caller-a').acquireProcessingLease()- await expect(- store.processingLeaseProvider('caller-b').releaseProcessingLease(lease)- ).rejects.toMatchObject({ code: 'caller_mismatch' })- })-- it.each(['completed', 'missing-directory', 'empty-directory'] as const)(- 'settles an expired unconsumed copy before ordinary shell admission (%s)',- async kind => {- const receipt = await publishFixture()- if (kind === 'missing-directory')- fs.rmSync(path.join(callerRoot, path.dirname(receipt.path)), {- recursive: true,- force: true,- })- if (kind === 'empty-directory') fs.unlinkSync(path.join(callerRoot, receipt.path))- vi.spyOn(Date, 'now').mockReturnValue(Date.parse(receipt.expiresAt))- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease()- expect(store.debugRecord(receipt.id)).toBeUndefined()- expect(store.debugUsage().bytes).toBe(0)- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(false)- await provider.releaseProcessingLease(lease)- await store.close()- }- )-- it('rejects a new lease after expiry while preserving an admitted execution', async () => {- const receipt = await publishFixture()- const provider = store.processingLeaseProvider('caller-a')- const admitted = await provider.acquireProcessingLease()- vi.spyOn(Date, 'now').mockReturnValue(Date.parse(receipt.expiresAt) + 1)-- await expect(provider.acquireProcessingLease()).rejects.toMatchObject({- code: 'download_expired',- })- await store.cleanupExpired()- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(true)- expect(store.debugRecord(receipt.id)).toBeDefined()-- await provider.releaseProcessingLease(admitted)- await store.cleanupExpired()- expect(store.debugRecord(receipt.id)).toBeUndefined()- await store.close()- })-- it('rejects expiry during integrity inspection before publishing a lease', async () => {- const receipt = await publishFixture()- const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now())- const inspect = store.inspect.bind(store)- vi.spyOn(store, 'inspect').mockImplementationOnce(async (...args) => {- const verified = await inspect(...args)- clock.mockReturnValue(Date.parse(receipt.expiresAt))- return verified- })-- await expect(- store.processingLeaseProvider('caller-a').acquireProcessingLease()- ).rejects.toMatchObject({ code: 'download_expired' })- const ledger = JSON.parse(- fs.readFileSync(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- expect(Object.keys(ledger.processingLeases)).toHaveLength(0)- await store.close()- })-- it('starts the processing budget after integrity inspection completes', async () => {- await publishFixture()- const startedAt = Date.now()- const clock = vi.spyOn(Date, 'now').mockReturnValue(startedAt)- const inspect = store.inspect.bind(store)- vi.spyOn(store, 'inspect').mockImplementationOnce(async (...args) => {- const verified = await inspect(...args)- clock.mockReturnValue(startedAt + 1_000)- return verified- })-- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 500 })- expect(Date.parse(lease.expiresAt)).toBe(startedAt + 1_500)- await provider.releaseProcessingLease(lease)- await store.close()- })-- it('rolls back a durable lease when the file expires during persistence', async () => {- const receipt = await publishFixture()- const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now())- const persistedStore = store as unknown as { persist: () => Promise<void> }- const persist = persistedStore.persist.bind(store)- vi.spyOn(persistedStore, 'persist').mockImplementationOnce(async () => {- await persist()- clock.mockReturnValue(Date.parse(receipt.expiresAt))- })-- await expect(- store.processingLeaseProvider('caller-a').acquireProcessingLease()- ).rejects.toMatchObject({ code: 'download_expired' })- const ledger = JSON.parse(- fs.readFileSync(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- expect(Object.keys(ledger.processingLeases)).toHaveLength(0)- expect(store.debugUsage().bytes).toBe(7)- await store.cleanupExpired()- expect(store.debugRecord(receipt.id)).toBeUndefined()- await store.close()- })-- it('rejects a processing budget exhausted while persisting admission', async () => {- await publishFixture()- const startedAt = Date.now()- const clock = vi.spyOn(Date, 'now').mockReturnValue(startedAt)- const persistedStore = store as unknown as { persist: () => Promise<void> }- const persist = persistedStore.persist.bind(store)- vi.spyOn(persistedStore, 'persist').mockImplementationOnce(async () => {- await persist()- clock.mockReturnValue(startedAt + 500)- })-- await expect(- store.processingLeaseProvider('caller-a').acquireProcessingLease({ durationMs: 500 })- ).rejects.toMatchObject({ code: 'download_busy' })- const ledger = JSON.parse(- fs.readFileSync(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- expect(Object.keys(ledger.processingLeases)).toHaveLength(0)- const provider = store.processingLeaseProvider('caller-a')- const retry = await provider.acquireProcessingLease({ durationMs: 500 })- await provider.releaseProcessingLease(retry)- await store.close()- })-- it('retains a failed admission rollback after its deadline when durable release is unknown', async () => {- const receipt = await publishFixture()- const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now())- const persistedStore = store as unknown as { persist: () => Promise<void> }- const persist = persistedStore.persist.bind(store)- vi.spyOn(persistedStore, 'persist')- .mockImplementationOnce(async () => {- await persist()- clock.mockReturnValue(Date.parse(receipt.expiresAt))- })- .mockRejectedValueOnce(new Error('rollback unavailable'))-- await expect(- store.processingLeaseProvider('caller-a').acquireProcessingLease()- ).rejects.toThrow('rollback unavailable')- const ledger = JSON.parse(- fs.readFileSync(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- const reservations = Object.values(ledger.processingLeases) as Array<{ expiresAt: string }>- expect(reservations).toHaveLength(1)- expect(store.debugUsage().bytes).toBe(7)-- clock.mockReturnValue(Date.parse(reservations[0]!.expiresAt) + 1)- await store.cleanupExpired()- expect(store.debugRecord(receipt.id)).toBeDefined()- expect(store.debugUsage().bytes).toBe(7)- await store.close()- })-- it.each(['inspection', 'persistence'] as const)(- 'fences pending and queued admissions when closing during %s',- async phase => {- const receipt = await publishFixture()- let resume!: () => void- let entered!: () => void- const barrier = new Promise<void>(resolve => {- resume = resolve- })- const waiting = new Promise<void>(resolve => {- entered = resolve- })- if (phase === 'inspection') {- const inspect = store.inspect.bind(store)- vi.spyOn(store, 'inspect').mockImplementationOnce(async (...args) => {- const verified = await inspect(...args)- entered()- await barrier- return verified- })- } else {- const persistedStore = store as unknown as { persist: () => Promise<void> }- const persist = persistedStore.persist.bind(store)- vi.spyOn(persistedStore, 'persist').mockImplementationOnce(async () => {- await persist()- entered()- await barrier- })- }-- const provider = store.processingLeaseProvider('caller-a')- const first = provider.acquireProcessingLease()- await waiting- const outcomes = Promise.allSettled([first, provider.acquireProcessingLease()])- const closed = store.close()- resume()-- const results = await outcomes- await closed- for (const result of results) {- expect(result.status).toBe('rejected')- if (result.status === 'rejected')- expect(result.reason).toMatchObject({ code: 'download_busy' })- }- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(true)- const restarted = new GfsDownloadStore(hostRoot)- await restarted.initialize()- expect(restarted.debugUsage().bytes).toBe(7)- await restarted.close()- }- )-- it('fences a transfer whose caller validation completes after shutdown', async () => {- const receipt = await publishFixture()- let resume!: () => void- let entered!: () => void- const barrier = new Promise<void>(resolve => {- resume = resolve- })- const waiting = new Promise<void>(resolve => {- entered = resolve- })- const validatingStore = store as unknown as {- validateCallerRoot: (root: string) => Promise<string>- }- const validate = validatingStore.validateCallerRoot.bind(store)- vi.spyOn(validatingStore, 'validateCallerRoot').mockImplementationOnce(async root => {- const validated = await validate(root)- entered()- await barrier- return validated- })- const outcome = Promise.allSettled([- store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }),- ])- await waiting- await store.close()- resume()-- const [result] = await outcome- expect(result!.status).toBe('rejected')- if (result!.status === 'rejected')- expect(result!.reason).toMatchObject({ code: 'workspace_unavailable' })- expect(fs.readdirSync(path.join(callerRoot, '.gfs-downloads'))).toEqual([- path.basename(path.dirname(receipt.path)),- ])- const restarted = new GfsDownloadStore(hostRoot)- await restarted.initialize()- expect(restarted.debugUsage().bytes).toBe(7)- await restarted.close()- })-- it('preserves an expired receipt in use during ordinary shell admission', async () => {- const receipt = await publishFixture(undefined, 'waiting-approval-task')- vi.spyOn(Date, 'now').mockReturnValue(Date.parse(receipt.expiresAt) + 1)- await expect(- store.processingLeaseProvider('caller-a').acquireProcessingLease()- ).rejects.toMatchObject({ code: 'download_expired' })- expect(store.debugRecord(receipt.id)).toBeDefined()- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(true)- await store.releaseReceiptOwner('waiting-approval-task', 'caller-a')- await store.cleanupExpired()- await store.close()- })-- it('keeps another caller admissible while one caller has an expired record', async () => {- const expired = await publishFixture()- const callerBRoot = path.join(hostRoot, 'users', 'caller-b')- fs.mkdirSync(callerBRoot, { recursive: true, mode: 0o700 })- const callerBTransfer = await store.createTransfer({- callerIdentity: 'caller-b',- callerWorkspacePath: callerBRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- fs.writeFileSync(path.join(callerBRoot, callerBTransfer.partialPath), Buffer.from('fixture'))- await store.publish(- callerBTransfer.id,- 'caller-b',- createHash('sha256').update('fixture').digest('hex')- )- vi.spyOn(Date, 'now').mockReturnValue(Date.parse(expired.expiresAt) + 1)-- const providerA = store.processingLeaseProvider('caller-a')- const leaseA = await providerA.acquireProcessingLease()- await providerA.releaseProcessingLease(leaseA)- expect(store.debugRecord(expired.id)).toBeUndefined()-- const providerB = store.processingLeaseProvider('caller-b')- const leaseB = await providerB.acquireProcessingLease()- await providerB.releaseProcessingLease(leaseB)- await store.close()- })-- it('rechecks failed-transfer ownership after a queued publication and lease admission', async () => {- await store.initialize()- const transfer = await store.createTransfer({- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- })- fs.writeFileSync(path.join(callerRoot, transfer.partialPath), 'fixture')- const persistedStore = store as unknown as { persist: () => Promise<void> }- const persist = persistedStore.persist.bind(store)- let resume!: () => void- let entered!: () => void- const barrier = new Promise<void>(resolve => {- resume = resolve- })- const waiting = new Promise<void>(resolve => {- entered = resolve- })- vi.spyOn(persistedStore, 'persist').mockImplementationOnce(async () => {- await persist()- entered()- await barrier- })- const published = store.publish(- transfer.id,- 'caller-a',- createHash('sha256').update('fixture').digest('hex')- )- await waiting- const provider = store.processingLeaseProvider('caller-a')- const admission = provider.acquireProcessingLease()- const failed = expect(store.fail(transfer.id, 'caller-a')).rejects.toMatchObject({- code: 'download_busy',- })- resume()- const receipt = await published- const lease = await admission- await failed- expect(store.debugRecord(receipt.id)).toMatchObject({ state: 'completed' })- expect(fs.readFileSync(path.join(callerRoot, receipt.path), 'utf8')).toBe('fixture')- const ledger = JSON.parse(- fs.readFileSync(path.join(hostRoot, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- expect(ledger.processingLeases[lease.leaseId].recordIds).toContain(receipt.id)- await provider.releaseProcessingLease(lease)- await store.close()- })-- it('protects an expired download from cleanup while leased', async () => {- const receipt = await publishFixture()- const lease = await store- .processingLeaseProvider('caller-a')- .acquireProcessingLease({ durationMs: 60_000 })-- await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)- expect(store.debugRecord(receipt.id)).toBeDefined()-- await store.processingLeaseProvider('caller-a').releaseProcessingLease(lease)- await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)- expect(store.debugRecord(receipt.id)).toBeUndefined()- })-- it('protects a live execution past both deadlines until its explicit release', async () => {- const receipt = await publishFixture()- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 500 })- const cleanupAt = Math.max(Date.parse(receipt.expiresAt), Date.parse(lease.expiresAt)) + 1-- await store.cleanupExpired(cleanupAt)- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(true)- expect(store.debugRecord(receipt.id)).toBeDefined()- await expect(store.close(10)).rejects.toMatchObject({ code: 'download_busy' })-- await provider.releaseProcessingLease(lease)- await store.cleanupExpired(cleanupAt)- expect(store.debugRecord(receipt.id)).toBeUndefined()- await store.close()- })-- it('recovers a lease conservatively after a store restart', async () => {- const receipt = await publishFixture(new Date(Date.now() + 60_000).toISOString())- const lease = await store- .processingLeaseProvider('caller-a')- .acquireProcessingLease({ durationMs: 1_000 })- await expect(store.close(10)).rejects.toMatchObject({ code: 'download_busy' })- ;(- store as unknown as {- liveProcessingLeases: Set<string>- }- ).liveProcessingLeases.delete(lease.leaseId)- await store.close()-- const restarted = new GfsDownloadStore(hostRoot)- await restarted.initialize()- await restarted.cleanupExpired(Date.parse(receipt.expiresAt) + 1)- expect(restarted.debugRecord(receipt.id)).toBeDefined()-- await restarted.cleanupExpired(Date.parse(lease.expiresAt) + 1)- expect(restarted.debugRecord(receipt.id)).toMatchObject({ state: 'quarantined' })- expect(fs.existsSync(path.join(callerRoot, receipt.path))).toBe(true)- await expect(- restarted.processingLeaseProvider('caller-a').releaseProcessingLease(lease)- ).rejects.toMatchObject({ code: 'download_busy' })- await restarted.close()- })-- it('retains protection when lease release persistence fails', async () => {- const receipt = await publishFixture()- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 60_000 })- vi.spyOn(store as unknown as { persist: () => Promise<void> }, 'persist').mockRejectedValueOnce(- new Error('disk unavailable')- )-- await expect(provider.releaseProcessingLease(lease)).rejects.toThrow()- await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)- expect(store.debugRecord(receipt.id)).toBeDefined()-- vi.restoreAllMocks()- await provider.releaseProcessingLease(lease)- await store.cleanupExpired(Date.parse(receipt.expiresAt) + 1)- expect(store.debugRecord(receipt.id)).toBeUndefined()- })-})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.tsindex 843350d69..f75f3b1b1 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts@@ -1,4 +1,4 @@-import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { type ChildProcess, spawn } from 'node:child_process'import { createHash } from 'node:crypto'import { constants } from 'node:fs'@@ -6,6 +6,9 @@ import * as fs from 'node:fs/promises'import type { FileHandle } from 'node:fs/promises'import { tmpdir } from 'node:os'import * as path from 'node:path'+import { register } from 'prom-client'+import { ShellTool } from '../core/tools/shell'+import { logger } from '../logger'import type { GfsImageSource } from '../visualInput/policy'import { GfsDownloadStore } from './gfsDownloadStore'@@ -35,29 +38,23 @@ const { createHash } = require('node:crypto');const mode = process.argv[3];const store = new GfsDownloadStore(root);await store.initialize();- if (mode === 'lease' || mode === 'emptyLease' || mode === 'pin') {+ if (mode === 'pin' || mode === 'transfer') {const callerRoot = path.join(root, 'users', 'caller-a');fs.mkdirSync(callerRoot, { recursive: true, mode: 0o700 });- let receipt;- if (mode === 'lease' || mode === 'pin') {const bytes = Buffer.from('fixture');const transfer = await store.createTransfer({ callerIdentity: 'caller-a', callerWorkspacePath: callerRoot,- source: ${JSON.stringify(source)}, sizeBytes: bytes.length, expiresAt: new Date(Date.now() + (mode === 'pin' ? 60000 : 1000)).toISOString(),+ source: ${JSON.stringify(source)}, sizeBytes: bytes.length, expiresAt: new Date(Date.now() + 60000).toISOString(),retentionOwnerId: mode === 'pin' ? '11111111-1111-4111-8111-111111111111' : undefined });- fs.writeFileSync(path.join(callerRoot, transfer.partialPath), bytes);- receipt = await store.publish(transfer.id, 'caller-a', createHash('sha256').update(bytes).digest('hex'));- }- if (mode === 'pin') {- await store.reusableReceipt('caller-a', ${JSON.stringify(source)}, 7, { retentionOwnerId: '22222222-2222-4222-8222-222222222222' });- process.send({ receipt });+ if (mode === 'transfer') {+ // The Host dies here, mid-transfer: the record stays 'transferring'.+ process.send({ transferId: transfer.id });setInterval(() => {}, 1000);return;}- const lease = await store.processingLeaseProvider('caller-a').acquireProcessingLease({ durationMs: 500 });- const executor = require('node:child_process').spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'],- { detached: true, stdio: 'ignore' });- executor.unref();- process.send({ receipt, lease, executorPid: executor.pid });+ fs.writeFileSync(path.join(callerRoot, transfer.partialPath), bytes);+ const receipt = await store.publish(transfer.id, 'caller-a', createHash('sha256').update(bytes).digest('hex'));+ await store.reusableReceipt('caller-a', ${JSON.stringify(source)}, 7, { retentionOwnerId: '22222222-2222-4222-8222-222222222222' });+ process.send({ receipt });} else process.send({ ready: true });setInterval(() => {}, 1000);})().catch(error => { process.send({ error: error.code || String(error), transientWriterContention: error.transientWriterContention === true }); process.exit(1); });@@ -117,13 +114,17 @@ function freshStore(): GfsDownloadStore {stores.push(store)return store}-async function fixture(store: GfsDownloadStore, retentionOwnerId?: string) {+async function fixture(+ store: GfsDownloadStore,+ retentionOwnerId?: string,+ fixtureSource: GfsImageSource = source+) {const callerRoot = path.join(root, 'users', 'caller-a')await fs.mkdir(callerRoot, { recursive: true, mode: 0o700 })const transfer = await store.createTransfer({callerIdentity: 'caller-a',callerWorkspacePath: callerRoot,- source,+ source: fixtureSource,sizeBytes: 7,expiresAt: new Date(Date.now() + 60_000).toISOString(),retentionOwnerId,@@ -136,6 +137,238 @@ async function fixture(store: GfsDownloadStore, retentionOwnerId?: string) {)}+const DISCARDED_COUNTER = 'clerum_gfs_legacy_processing_leases_discarded_total'+const QUARANTINED_COUNTER = 'clerum_gfs_inherited_quarantined_records_total'+const FOREIGN_WRITER_SESSION = '33333333-3333-4333-8333-333333333333'+const LEGACY_LEASE_ID = '44444444-4444-4444-8444-444444444444'+const otherSource: GfsImageSource = {+ ...source,+ resourceId: 'b'.repeat(32),+ gfsUri: `gfs://main/${'b'.repeat(32)}`,+ name: 'other.csv',+}++function ledgerFile(): string {+ return path.join(root, '.gfs-download-store', 'ledger-v1.json')+}++async function counterValue(name: string): Promise<number> {+ const metric = register.getSingleMetric(name)+ if (metric === undefined) throw new Error(`metric ${name} is not registered`)+ const { values } = await metric.get()+ return values.reduce((sum, sample) => sum + sample.value, 0)+}++/** A processing lease exactly as a pre-#1019 Host wrote it, by default expired and empty. */+function legacyLease(overrides: Record<string, unknown> = {}): Record<string, unknown> {+ return {+ leaseId: LEGACY_LEASE_ID,+ callerIdentity: 'caller-a',+ recordIds: [],+ acquiredAt: new Date(Date.now() - 120_000).toISOString(),+ expiresAt: new Date(Date.now() - 60_000).toISOString(),+ writerSessionId: FOREIGN_WRITER_SESSION,+ ...overrides,+ }+}++/** Writes legacy leases into the ledger of a closed store, as an older build left them. */+async function seedLegacyLeases(leases: Record<string, unknown>[]): Promise<string> {+ const ledger = JSON.parse(await fs.readFile(ledgerFile(), 'utf8'))+ ledger.processingLeases = Object.fromEntries(leases.map(lease => [lease.leaseId, lease]))+ const raw = JSON.stringify(ledger)+ await fs.writeFile(ledgerFile(), raw, { mode: 0o600 })+ return raw+}++async function closedStore(): Promise<void> {+ const store = freshStore()+ await store.initialize()+ await store.close()+}++function discardWarnings(calls: unknown[][]): unknown[][] {+ return calls.filter(+ call => typeof call[1] === 'string' && call[1].includes('legacy processing lease')+ )+}++describe('legacy processing leases after #1019', () => {+ afterEach(() => {+ vi.restoreAllMocks()+ })++ it('S1: discards an expired foreign-session lease left by a crashed Host and admits downloads', async () => {+ await closedStore()+ await seedLegacyLeases([legacyLease()])+ const warn = vi.spyOn(logger, 'warn')+ const before = await counterValue(DISCARDED_COUNTER)+ const reopened = freshStore()+ await reopened.initialize()+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(1)+ expect(discardWarnings(warn.mock.calls)).toEqual([+ [{ discarded: 1 }, 'GFS download store discarded 1 legacy processing lease(s) at initialize'],+ ])+ const onDisk = JSON.parse(await fs.readFile(ledgerFile(), 'utf8'))+ expect(onDisk.records).toEqual({})+ expect(onDisk).not.toHaveProperty('processingLeases')+ 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()+ const intact = await fixture(store)+ const altered = await fixture(store, undefined, otherSource)+ await store.close()+ await seedLegacyLeases([+ legacyLease({+ recordIds: [intact.id, altered.id],+ expiresAt: new Date(Date.now() + 600_000).toISOString(),+ }),+ ])+ await fs.writeFile(path.join(root, 'users', 'caller-a', altered.path), 'changed')+ const before = await counterValue(DISCARDED_COUNTER)+ const reopened = freshStore()+ await reopened.initialize()+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(1)+ expect(reopened.isAvailable()).toBe(true)+ expect(await reopened.reusableReceipt('caller-a', source, 7)).toMatchObject({+ id: intact.id,+ sha256: intact.sha256,+ })+ expect(reopened.debugRecord(intact.id)?.state).toBe('completed')+ expect(reopened.debugRecord(altered.id)?.state).toBe('quarantined')+ expect(await reopened.reusableReceipt('caller-a', otherSource, 7)).toBeUndefined()+ })++ it.each([+ ['a lease id that is not a UUID', { leaseId: 'not-a-uuid' }],+ [+ 'an expiry that does not follow acquisition',+ { acquiredAt: '2026-01-01T00:00:00.000Z', expiresAt: '2026-01-01T00:00:00.000Z' },+ ],+ ])(+ 'S3: fails closed on a malformed legacy lease (%s) without touching the ledger',+ async (_label, overrides) => {+ await closedStore()+ const raw = await seedLegacyLeases([legacyLease(overrides)])+ const before = await counterValue(DISCARDED_COUNTER)+ await expect(freshStore().initialize()).rejects.toMatchObject({+ code: 'corrupt_store_ledger',+ })+ expect(await fs.readFile(ledgerFile(), 'utf8')).toBe(raw)+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(0)+ }+ )++ it('S4: a clean restart discards nothing', async () => {+ const store = freshStore()+ await store.initialize()+ await fixture(store)+ await store.close()+ const warn = vi.spyOn(logger, 'warn')+ const before = await counterValue(DISCARDED_COUNTER)+ const reopened = freshStore()+ await reopened.initialize()+ expect(reopened.isAvailable()).toBe(true)+ expect(reopened.debugUsage()).toMatchObject({ bytes: 7, files: 1 })+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(0)+ expect(discardWarnings(warn.mock.calls)).toEqual([])+ })++ it('S5: close() is blocked only by its own transfers, never by a running shell', async () => {+ const store = freshStore()+ await store.initialize()+ const callerRoot = path.join(root, 'users', 'caller-a')+ await fs.mkdir(callerRoot, { recursive: true, mode: 0o700 })+ const transfer = await store.createTransfer({+ callerIdentity: 'caller-a',+ callerWorkspacePath: callerRoot,+ source,+ sizeBytes: 7,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ })+ await expect(store.close(0)).rejects.toMatchObject({ code: 'download_busy' })+ expect(store.isAvailable()).toBe(true)+ await store.fail(transfer.id, 'caller-a')++ const shell = new ShellTool(callerRoot, 30_000, ['PATH'], () => ({}), undefined, true)+ const controller = new AbortController()+ const pidFile = path.join(callerRoot, 'shell.pid')+ const running = shell.execute(+ { command: `echo $$ > ${JSON.stringify(pidFile)}; exec sleep 30` },+ { signal: controller.signal, onOutput: () => undefined }+ )+ let pid = ''+ for (const deadline = Date.now() + 5_000; pid === '' && Date.now() < deadline; ) {+ pid = (await fs.readFile(pidFile, 'utf8').catch(() => '')).trim()+ if (pid === '') await new Promise(resolve => setTimeout(resolve, 10))+ }+ expect(pid).toMatch(/^[0-9]+$/)+ await store.close(0)+ expect(store.isAvailable()).toBe(false)+ // Witness: the shell was still running when close() resolved.+ expect(() => process.kill(Number(pid), 0)).not.toThrow()+ controller.abort()+ const result = await running+ expect(result.is_error).toBe(true)+ })++ it('S8: an interrupted discard leaves the ledger byte-identical and is retried by the next initialize', async () => {+ await closedStore()+ const raw = await seedLegacyLeases([legacyLease()])+ const warn = vi.spyOn(logger, 'warn')+ const before = await counterValue(DISCARDED_COUNTER)+ const interrupted = freshStore()+ const persist = vi+ .spyOn(interrupted as unknown as { persist: () => Promise<void> }, 'persist')+ .mockRejectedValueOnce(new Error('injected persist failure'))+ await expect(interrupted.initialize()).rejects.toThrow('injected persist failure')+ expect(persist).toHaveBeenCalledTimes(1)+ expect(interrupted.isAvailable()).toBe(false)+ expect(await fs.readFile(ledgerFile(), 'utf8')).toBe(raw)+ expect(JSON.parse(raw).processingLeases[LEGACY_LEASE_ID]).toBeDefined()+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(0)+ expect(discardWarnings(warn.mock.calls)).toEqual([])++ const retried = freshStore()+ await retried.initialize()+ expect(retried.isAvailable()).toBe(true)+ expect((await counterValue(DISCARDED_COUNTER)) - before).toBe(1)+ expect(discardWarnings(warn.mock.calls)).toHaveLength(1)+ expect(JSON.parse(await fs.readFile(ledgerFile(), 'utf8'))).not.toHaveProperty(+ 'processingLeases'+ )+ })++ it('S9: counts and logs records an earlier boot quarantined', async () => {+ const { child, message } = await childStore('transfer')+ expect(message.transferId).toMatch(/^[0-9a-f-]{36}$/)+ await kill(child)+ const warn = vi.spyOn(logger, 'warn')+ const before = await counterValue(QUARANTINED_COUNTER)+ const recovering = freshStore()+ await recovering.initialize()+ // This boot quarantines the interrupted transfer; it inherited none.+ expect(recovering.debugRecord(message.transferId)?.state).toBe('quarantined')+ expect((await counterValue(QUARANTINED_COUNTER)) - before).toBe(0)+ await recovering.close()++ const next = freshStore()+ await next.initialize()+ expect((await counterValue(QUARANTINED_COUNTER)) - before).toBe(1)+ expect(+ warn.mock.calls.filter(call => JSON.stringify(call[0]) === JSON.stringify({ quarantined: 1 }))+ ).toHaveLength(1)+ expect(next.debugRecord(message.transferId)?.state).toBe('quarantined')+ expect(next.debugUsage().bytes).toBe(7)+ })+})+describe('GFS writer recovery and filesystem invariants', () => {it.each(['SIGKILL', 'SIGTERM'] as const)('excludes another process and recovers after %s on the same inode',@@ -218,31 +451,6 @@ describe('GFS writer recovery and filesystem invariants', () => {})- it('preserves inherited copies while a detached executor survives Host death past both deadlines', async () => {- const { child, message } = await childStore('lease')- expect(message.error).toBeUndefined()- executors.push(message.executorPid)- await kill(child)- expect(() => process.kill(message.executorPid, 0)).not.toThrow()- const reopened = freshStore()- await reopened.initialize()- await reopened.cleanupExpired(- Math.max(Date.parse(message.receipt.expiresAt), Date.parse(message.lease.expiresAt)) + 1- )- expect(() => process.kill(message.executorPid, 0)).not.toThrow()- expect(reopened.debugRecord(message.receipt.id)).toMatchObject({- state: 'quarantined',- sizeBytes: 7,- })- expect(reopened.debugUsage().bytes).toBe(7)- expect(- await fs.readFile(path.join(root, 'users', 'caller-a', message.receipt.path), 'utf8')- ).toBe('fixture')- await expect(- reopened.processingLeaseProvider('caller-a').releaseProcessingLease(message.lease)- ).rejects.toMatchObject({ code: 'download_busy' })- })-it('reauthorizes a clean v2 cold-resume copy for the exact task owner and preserves every other pin', async () => {const { child, message } = await childStore('pin')await kill(child)@@ -307,63 +515,6 @@ describe('GFS writer recovery and filesystem invariants', () => {})- it('durably protects an execution admitted before the first retained download', async () => {- const store = freshStore()- await store.initialize()- const provider = store.processingLeaseProvider('caller-a')- const lease = await provider.acquireProcessingLease({ durationMs: 500 })- const ledger = JSON.parse(- await fs.readFile(path.join(root, '.gfs-download-store', 'ledger-v1.json'), 'utf8')- )- expect(ledger.processingLeases[lease.leaseId].recordIds).toEqual([])- await expect(store.close(0)).rejects.toMatchObject({ code: 'download_busy' })- await provider.releaseProcessingLease(lease)- await store.close()- })-- it('fences Host-wide managed work after an inherited empty execution', async () => {- const { child, message } = await childStore('emptyLease')- expect(message.error).toBeUndefined()- executors.push(message.executorPid)- await kill(child)- const reopened = freshStore()- await reopened.initialize()- const callerRoot = path.join(root, 'users', 'caller-a')- const input = {- callerIdentity: 'caller-a',- callerWorkspacePath: callerRoot,- source,- sizeBytes: 7,- expiresAt: new Date(Date.now() + 60_000).toISOString(),- }- await reopened.cleanupExpired(Date.parse(message.lease.expiresAt) + 1)- expect(() => process.kill(message.executorPid, 0)).not.toThrow()- await expect(reopened.createTransfer(input)).rejects.toMatchObject({ code: 'download_busy' })- await expect(reopened.reusableReceipt('caller-a', source, 7)).rejects.toMatchObject({- code: 'download_busy',- })- await expect(- reopened.processingLeaseProvider('caller-a').acquireProcessingLease()- ).rejects.toMatchObject({ code: 'download_busy' })- await expect(fs.stat(path.join(callerRoot, '.gfs-downloads'))).rejects.toMatchObject({- code: 'ENOENT',- })- const callerBRoot = path.join(root, 'users', 'caller-b')- await fs.mkdir(callerBRoot, { recursive: true, mode: 0o700 })- await expect(- reopened.createTransfer({- ...input,- callerIdentity: 'caller-b',- callerWorkspacePath: callerBRoot,- })- ).rejects.toMatchObject({ code: 'download_busy' })- await expect(- reopened.processingLeaseProvider('caller-b').acquireProcessingLease()- ).rejects.toMatchObject({ code: 'download_busy' })- expect(reopened.isAvailable()).toBe(false)- expect(reopened.debugUsage().bytes).toBe(0)- })-it('keeps kernel ownership after busy close and admits another process only after consumers settle', async () => {const store = freshStore()await store.initialize()diff --git a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts b/mcp-host/src/internalTools/gfsImageInput.integration.test.tsindex 80dd271b9..e1483a1f1 100644--- a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts+++ b/mcp-host/src/internalTools/gfsImageInput.integration.test.ts@@ -20,7 +20,6 @@ import type { SingleTurnProvider } from '../llm/types'import { VisualInputBudget } from '../visualInput/policy'import { projectGfsApproval } from '../visualInput/suspension'import { GfsDownloadStore } from './gfsDownloadStore'-import type { GfsProcessingLeaseProvider } from './gfsProcessingLease'const { sdkCreate, clientFactory } = vi.hoisted(() => ({sdkCreate: vi.fn(),@@ -190,7 +189,6 @@ async function setup(deliveryAvailable: booleancallerIdentity: stringcallerWorkspacePath: string- processingLeaseProvider: GfsProcessingLeaseProviderretentionOwnerId: string}| undefined@@ -207,7 +205,6 @@ async function setup(deliveryAvailable: true,callerWorkspacePath,callerIdentity: 'unit-caller',- processingLeaseProvider: store.processingLeaseProvider('unit-caller'),retentionOwnerId: 'gfs-image-integration-task',}}diff --git a/mcp-host/src/internalTools/gfsProcessingLease.ts b/mcp-host/src/internalTools/gfsProcessingLease.tsdeleted file mode 100644index fcb6624b1..000000000--- a/mcp-host/src/internalTools/gfsProcessingLease.ts+++ /dev/null@@ -1,21 +0,0 @@-/**- * Caller-bound cleanup protection for approved local processing.- *- * The provider is intentionally narrower than the download store: ShellTool can- * protect retained downloads without gaining store administration capability.- */-export interface GfsProcessingLease {- leaseId: string- expiresAt: string-}--export interface GfsProcessingLeaseAcquisition {- /** Bounded lease duration in milliseconds. Implementations enforce a ceiling. */- durationMs?: number-}--export interface GfsProcessingLeaseProvider {- acquireProcessingLease(options?: GfsProcessingLeaseAcquisition): Promise<GfsProcessingLease>- releaseProcessingLease(lease: GfsProcessingLease): Promise<void>-}-// Processing leases protect retained downloads while a shell command runs.diff --git a/mcp-host/src/internalTools/gfsStoreOperatorRecovery.test.ts b/mcp-host/src/internalTools/gfsStoreOperatorRecovery.test.tsindex 8159e9855..06bd31da2 100644--- a/mcp-host/src/internalTools/gfsStoreOperatorRecovery.test.ts+++ b/mcp-host/src/internalTools/gfsStoreOperatorRecovery.test.ts@@ -149,20 +149,30 @@ describe('local GFS operator recovery', () => {})})- it('restores only a verified published copy after explicit executor settlement', async () => {+ it('S7: settles a legacy ledger that still carries a pre-#1019 processing lease', async () => {const store = fresh()await store.initialize()const receipt = await publish(store)- const lease = await store.processingLeaseProvider('caller-a').acquireProcessingLease()await simulateWriterDeath(store)- const disabled = fresh()- await disabled.initialize()- expect(disabled.isAvailable()).toBe(false)- expect(disabled.debugRecord(receipt.id)?.state).toBe('quarantined')- await disabled.close()- const input = await recoveryInput({ settledProcessingLeaseIds: [lease.leaseId] })+ // A Host older than #1019 died holding a lease over this copy. Seed the+ // lease exactly as that build persisted it.+ const ledgerPath = path.join(root, '.gfs-download-store', 'ledger-v1.json')+ const ledger = JSON.parse(await fs.readFile(ledgerPath, 'utf8'))+ const leaseId = '55555555-5555-4555-8555-555555555555'+ ledger.processingLeases = {+ [leaseId]: {+ leaseId,+ callerIdentity: 'caller-a',+ recordIds: [receipt.id],+ acquiredAt: new Date(Date.now() - 60_000).toISOString(),+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ writerSessionId: '66666666-6666-4666-8666-666666666666',+ },+ }+ await fs.writeFile(ledgerPath, JSON.stringify(ledger), { mode: 0o600 })+ const input = await recoveryInput({ settledProcessingLeaseIds: [leaseId] })const result = await recoverGfsStoreUnderPhysicalFence(input)- expect(result.before.selections.processingLeaseIds).toEqual([lease.leaseId])+ expect(result.before.selections.processingLeaseIds).toEqual([leaseId])expect(result.after.counts).toMatchObject({ files: 1, bytes: 7, processingLeases: 0 })const recovered = fresh()await recovered.initialize()diff --git a/mcp-host/src/workspace/__tests__/gfsCallerRootBinding.test.ts b/mcp-host/src/workspace/__tests__/gfsCallerRootBinding.test.tsindex e22a8612f..9e4a4c2c2 100644--- a/mcp-host/src/workspace/__tests__/gfsCallerRootBinding.test.ts+++ b/mcp-host/src/workspace/__tests__/gfsCallerRootBinding.test.ts@@ -73,7 +73,6 @@ describe('NativeRegistry GFS caller file-tool binding', () => {deliveryAvailable,callerIdentity: 'alice',callerWorkspacePath: callerRoot,- processingLeaseProvider: store.processingLeaseProvider('alice'),retentionOwnerId: '11111111-1111-4111-8111-111111111111',})