Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): never block a Host on leftover GFS store state at startup
Alfredo Lopeze08a17332298
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 9aa0c3a94..ae3e7054f 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.523",+ "version": "0.1.524","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.523",+ "version": "0.1.524","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex b2b6704b7..31949ce8f 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.523",+ "version": "0.1.524","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.tsindex 1a625f5f7..a5b9be2fa 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsExecutionSafety.test.ts@@ -148,9 +148,21 @@ async function preRedesignStore({ live = false }: { live?: boolean } = {}) {const [seededLease] = Object.values(ledger.processingLeases)if (seededLease === undefined)throw new Error('preRedesignStore fixture: no processing lease was written')+ // Witness for every X1 case: the old store is on disk before the start and+ // the start retires it, so availability cannot come from a missing fixture.+ await expect(fs.lstat(devStore.storeRoot)).resolves.toBeDefined()+ const warn = vi.spyOn(logger, 'warn')const store = new GfsDownloadStore(root)stores.push(store)await store.initialize()+ expect(warn).toHaveBeenCalledWith(+ expect.objectContaining({ component: 'GfsDownloadStore' }),+ 'Retired the pre-#1028 GFS download store'+ )+ warn.mockRestore()+ await expect(fs.lstat(join(root, '.gfs-download-store'))).rejects.toMatchObject({+ code: 'ENOENT',+ })return { root, store, seededLease }}diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.tsindex dfed29a7d..0b7d09efb 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts@@ -4,6 +4,7 @@ import * as fs from 'node:fs/promises'import * as path from 'node:path'import { logger } from '../logger'import type { GfsImageSource } from '../visualInput/policy'+import { RETIRED_GFS_DOWNLOAD_STORE_PREFIX } from '../workspace/protectedPaths'import { recordGfsDownloadExpiry, recordGfsDownloadQuota } from './gfsDownloadMetrics'import {GFS_FILE_LIMITS,@@ -21,6 +22,8 @@ const SOURCE_FILE = 'source'const PARTIAL_FILE = 'source.partial'const META_MAX_BYTES = 64 * 1024const FREE_SPACE_RESERVE_BYTES = 16n * 1024n * 1024n+/** Host-root directory of the ledger store written before #1028; never read. */+const LEGACY_STORE_DIRECTORY = '.gfs-download-store'const UUID_PATTERN = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'/** The only directory names under `.gfs-downloads` that belong to the store. */@@ -28,6 +31,10 @@ export const GFS_INPUT_DIRECTORY_RE = new RegExp(`^input-(${UUID_PATTERN})$`)/** A directory renamed out of the way before removal; swept if a removal stops. */const TRASH_PREFIX = '.trash-'const TRASH_DIRECTORY_RE = new RegExp(`^\\.trash-${UUID_PATTERN}$`)+/** Host-root names the retirement of the pre-#1028 store gives the old tree. */+const RETIRED_DIRECTORY_RE = new RegExp(+ `^${RETIRED_GFS_DOWNLOAD_STORE_PREFIX.replaceAll('.', '\\.')}${UUID_PATTERN}$`+)/** Caller-relative receipt path; the same contract gfsFilePreparation enforces. */const RECEIPT_PATH_RE = new RegExp(`^\\.gfs-downloads/input-(${UUID_PATTERN})/source$`)const SHA256_RE = /^[0-9a-f]{64}$/@@ -138,6 +145,8 @@ interface SweepTotals extends GfsDownloadSweepResult {retainedBytes: number/** Retained entries this process did not publish. */adopted: number+ /** This sweep renamed the pre-#1028 store out of the way. */+ retiredLegacyStore: boolean}export interface GfsDownloadInventory {@@ -264,6 +273,28 @@ async function sha256FileHandle(handle: fs.FileHandle, assertActive?: () => voidreturn digest.digest('hex')}+/**+ * Regular files and their bytes under a directory, for the retirement log+ * only. Nothing is opened and no symlink is followed: entries are classified+ * by their own lstat type.+ */+async function treeUsage(root: string): Promise<{ files: number; bytes: number }> {+ const usage = { files: 0, bytes: 0 }+ const pending = [root]+ while (pending.length > 0) {+ const directory = pending.pop()!+ for (const entry of await fs.readdir(directory, { withFileTypes: true })) {+ const child = path.join(directory, entry.name)+ if (entry.isDirectory()) pending.push(child)+ else if (entry.isFile()) {+ usage.files += 1+ usage.bytes += (await fs.lstat(child)).size+ }+ }+ }+ return usage+}+function receiptPath(id: string): string {return path.join(DOWNLOADS_DIRECTORY, `input-${id}`, SOURCE_FILE)}@@ -355,7 +386,7 @@ export class GfsDownloadStore {retainedCompleted: totals.retainedCompleted,retainedBytes: totals.retainedBytes,adopted: totals.adopted,- retiredLegacyStore: false,+ retiredLegacyStore: totals.retiredLegacyStore,},'GFS download store initialized')@@ -830,7 +861,8 @@ export class GfsDownloadStore {* never touched, and a published entry is never replaced: another directory* carrying the id of a reservation or of a published copy is removed, and an* id found in several directories with neither is removed everywhere (a- * random uuid does not repeat by chance).+ * random uuid does not repeat by chance). Every sweep first retires the+ * pre-#1028 store if one is present.*/private async sweep(now: number): Promise<SweepTotals> {const totals: SweepTotals = {@@ -840,7 +872,9 @@ export class GfsDownloadStore {retainedCompleted: 0,retainedBytes: 0,adopted: 0,+ retiredLegacyStore: false,}+ await this.retireLegacyStore(totals)const candidates = new Map<string, string[]>()const addCandidate = (id: string, directory: string) => {const directories = candidates.get(id) ?? []@@ -898,6 +932,80 @@ export class GfsDownloadStore {return totals}+ /**+ * Retires the ledger store of the pre-#1028 image, at startup and in every+ * sweep: `.gfs-download-store` is renamed to+ * `.gfs-download-store.retired-<uuid>` inside the Host root, then every+ * retired tree is removed the way any store directory is (renamed to a new+ * private name, the parent checked again, removed). Nothing inside is read;+ * the warning carries only file and byte counts. A failed step is logged+ * with its code and step and counted, and never fails the sweep: leftover+ * state can cost disk space, never availability. A retired tree a failed+ * removal left is protected from workspace tools and retried by the next+ * sweep.+ */+ private async retireLegacyStore(totals: SweepTotals): Promise<void> {+ const failed = (step: 'inspect' | 'rename' | 'list' | 'remove', error: unknown) => {+ totals.removeFailed += 1+ recordGfsDownloadExpiry('remove_failed')+ logger.warn(+ { component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, code: errorCode(error), step },+ 'GFS download store could not retire the pre-#1028 store; the next sweep retries it'+ )+ }+ const legacy = path.join(this.hostRoot, LEGACY_STORE_DIRECTORY)+ let info: import('node:fs').Stats | undefined+ try {+ info = await fs.lstat(legacy)+ } catch (error) {+ if (errorCode(error) !== 'ENOENT') failed('inspect', error)+ }+ if (info) {+ let usage: { files: number; bytes: number } | undefined+ try {+ usage = info.isDirectory()+ ? await treeUsage(legacy)+ : { files: info.isFile() ? 1 : 0, bytes: info.isFile() ? info.size : 0 }+ } catch (error) {+ // The counts only feed the log line; the retirement goes ahead without them.+ logger.warn(+ { component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, code: errorCode(error), step: 'measure' },+ 'GFS download store could not measure the pre-#1028 store before retiring it'+ )+ }+ try {+ await this.assertRemovable(legacy)+ await fs.rename(+ legacy,+ path.join(this.hostRoot, `${RETIRED_GFS_DOWNLOAD_STORE_PREFIX}${randomUUID()}`)+ )+ totals.retiredLegacyStore = true+ recordGfsDownloadExpiry('retired_legacy_store')+ logger.warn(+ { component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, ...usage },+ 'Retired the pre-#1028 GFS download store'+ )+ } catch (error) {+ failed('rename', error)+ }+ }+ let names: string[]+ try {+ names = await fs.readdir(this.hostRoot)+ } catch (error) {+ failed('list', error)+ return+ }+ for (const name of names) {+ if (!RETIRED_DIRECTORY_RE.test(name)) continue+ try {+ await this.removeVerified(path.join(this.hostRoot, name), RETIRED_GFS_DOWNLOAD_STORE_PREFIX)+ } catch (error) {+ failed('remove', error)+ }+ }+ }+/*** Lists store-owned directories. A `.gfs-downloads` that is a symlink or not* a directory is removed without following it; names that do not match@@ -1244,9 +1352,9 @@ export class GfsDownloadStore {* parent moved in between, the rename is undone and the removal refused,* so whatever the swapped path led to keeps its name and content.*/- private async removeVerified(directory: string): Promise<void> {+ private async removeVerified(directory: string, trashPrefix = TRASH_PREFIX): Promise<void> {await this.assertRemovable(directory)- const trash = path.join(path.dirname(directory), `${TRASH_PREFIX}${randomUUID()}`)+ const trash = path.join(path.dirname(directory), `${trashPrefix}${randomUUID()}`)try {await fs.rename(directory, trash)} catch (error) {@@ -1264,10 +1372,13 @@ export class GfsDownloadStore {await assertAbsent(trash)}- /** The parent of a directory about to be removed is real and inside the Host root. */+ /**+ * The parent of a directory about to be removed is real and is the Host+ * root (only for the retired pre-#1028 store) or inside it.+ */private async assertRemovable(directory: string): Promise<void> {const parent = path.dirname(directory)- if (!isWithinDirectory(parent, this.hostRoot))+ if (parent !== this.hostRoot && !isWithinDirectory(parent, this.hostRoot))throw new GfsDownloadStoreError('workspace_unavailable')let real: stringtry {diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.tsnew file mode 100644index 000000000..d226af9a5--- /dev/null+++ b/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.ts@@ -0,0 +1,363 @@+/**+ * Restart behaviour of the filesystem GFS download store: a volume written by+ * the ledger store of the pre-#1028 image (fixture shapes from dev 74e0d81d9)+ * never blocks admission, and a volume written by this store is swept but its+ * copies are never reused or served after a restart.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import * as fs from 'node:fs/promises'+import { tmpdir } from 'node:os'+import * as path from 'node:path'+import {+ type DevFenceShape,+ type DevStoreShape,+ buildDevStore,+} from '../__tests__/fixtures/devGfsStoreFixture'+import {+ callerDirectory,+ completedCopy,+ downloadDirectory,+ exists,+ expiryCount,+ sourceFor,+} from '../__tests__/fixtures/gfsStoreTestKit'+import { logger } from '../logger'+import { isProtectedWorkspacePath } from '../workspace/protectedPaths'+import { GfsDownloadStore } from './gfsDownloadStore'++const { renameFailure, rmFailure } = vi.hoisted(() => ({+ /** `target` is a Host-root entry name; the store renames it by its real path. */+ renameFailure: { target: undefined as string | undefined, injected: 0 },+ /** `prefix` is a Host-root entry name prefix the next `rm` refuses once. */+ rmFailure: { prefix: undefined as string | undefined, injected: 0 },+}))+// Pass-through `rename` and `rm`: R3 makes the retirement rename of the legacy+// store fail once and R5 the removal of the retired tree, as a busy mount or a+// read-only Host root would.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof import('node:fs/promises')>()+ return {+ ...actual,+ rename: async (...values: Parameters<typeof actual.rename>) => {+ if (+ renameFailure.target !== undefined &&+ String(values[0]).endsWith(`${path.sep}${renameFailure.target}`)+ ) {+ renameFailure.target = undefined+ renameFailure.injected += 1+ throw Object.assign(new Error('simulated rename failure'), { code: 'EACCES' })+ }+ return actual.rename(...values)+ },+ rm: async (...values: Parameters<typeof actual.rm>) => {+ if (+ rmFailure.prefix !== undefined &&+ path.basename(String(values[0])).startsWith(rmFailure.prefix)+ ) {+ rmFailure.prefix = undefined+ rmFailure.injected += 1+ throw Object.assign(new Error('simulated rm failure'), { code: 'EACCES' })+ }+ return actual.rm(...values)+ },+ }+})++const CALLER = 'caller-a'+const RETIRED_PREFIX = '.gfs-download-store.retired-'+const RETIREMENT_MESSAGE = 'Retired the pre-#1028 GFS download store'+const INPUT_RE = /^input-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/++let hostRoot: string+const stores: GfsDownloadStore[] = []++async function open(): Promise<GfsDownloadStore> {+ const store = new GfsDownloadStore(hostRoot)+ stores.push(store)+ await store.initialize()+ return store+}++type LogCall = [Record<string, unknown>, string]++function startupLines(info: { mock: { calls: unknown[][] } }): Record<string, unknown>[] {+ return (info.mock.calls as LogCall[])+ .filter(([, message]) => message === 'GFS download store initialized')+ .map(([fields]) => fields)+}++function lastStartupLine(info: { mock: { calls: unknown[][] } }): Record<string, unknown> {+ const lines = startupLines(info)+ if (lines.length === 0) throw new Error('the store logged no startup line')+ return lines[lines.length - 1]!+}++function retirementWarnings(warn: { mock: { calls: unknown[][] } }): Record<string, unknown>[] {+ return (warn.mock.calls as LogCall[])+ .filter(([, message]) => message === RETIREMENT_MESSAGE)+ .map(([fields]) => fields)+}++async function hostRootEntries(): Promise<string[]> {+ return (await fs.readdir(hostRoot)).sort()+}++beforeEach(async () => {+ hostRoot = await fs.realpath(await fs.mkdtemp(path.join(tmpdir(), 'gfs-store-restart-')))+})++afterEach(async () => {+ renameFailure.target = undefined+ renameFailure.injected = 0+ rmFailure.prefix = undefined+ rmFailure.injected = 0+ for (const store of stores.splice(0)) await store.close()+ vi.restoreAllMocks()+ await fs.rm(hostRoot, { recursive: true, force: true })+})++describe('GFS download store restart', () => {+ it.each([+ ['v2 fence with a matching device', { ledger: 'records', fence: 'v2-matching' }],+ ['v2 fence with another device', { ledger: 'records', fence: 'v2-mismatched-device' }],+ ['v2 fence without a device', { ledger: 'records', fence: 'v2-no-device' }],+ ['v3 fence', { ledger: 'records', fence: 'v3' }],+ ['ledger without a fence', { ledger: 'records', fence: 'none' }],+ [+ 'live processing lease of a crashed Host',+ { ledger: 'processingLeasesEmpty', fence: 'v2-matching', lease: 'live' },+ ],+ ] as Array<[string, Pick<DevStoreShape, 'ledger' | 'fence' | 'lease'>]>)(+ 'R1: restarting on a volume written by the dev store never blocks admission: %s',+ async (_label, shape) => {+ const dev = await buildDevStore(hostRoot, {+ ...shape,+ sqlite: (shape.fence as DevFenceShape) !== 'none',+ userDirs: ['completed', 'transferring-partial'],+ })+ // Witness: the old store directory is on disk before the start.+ expect(exists(dev.storeRoot)).toBe(true)+ const warn = vi.spyOn(logger, 'warn')+ const info = vi.spyOn(logger, 'info')+ const retired = await expiryCount('retired_legacy_store')++ const store = await open()++ expect(store.isAvailable()).toBe(true)+ const retirements = retirementWarnings(warn)+ expect(retirements).toHaveLength(1)+ expect(retirements[0]).toMatchObject({ component: 'GfsDownloadStore' })+ expect(retirements[0]!.files as number).toBeGreaterThan(0)+ expect(retirements[0]!.bytes as number).toBeGreaterThan(0)+ expect(lastStartupLine(info)).toMatchObject({+ retiredLegacyStore: true,+ removedIncomplete: 2,+ })+ expect(await expiryCount('retired_legacy_store')).toBe(retired + 1)+ expect(exists(dev.storeRoot)).toBe(false)+ expect((await hostRootEntries()).filter(name => name.startsWith(RETIRED_PREFIX))).toEqual([])+ const { receipt, bytes } = await completedCopy(+ store,+ dev.callerRoot,+ dev.callerIdentity,+ 1,+ 64+ )+ await expect(store.readManagedFile(receipt.path, dev.callerIdentity)).resolves.toEqual(bytes)+ }+ )++ it('R2: dev user directories of every state are removed at startup and do not count against quota', async () => {+ const dev = await buildDevStore(hostRoot, {+ ledger: 'records',+ fence: 'v2-matching',+ sqlite: true,+ userDirs: ['completed', 'transferring-partial', 'quarantined', 'missing', 'orphan-partial'],+ })+ // Witness: every dev directory and its bytes are on disk before the start.+ expect(dev.userDirectories).toHaveLength(5)+ expect(dev.userBytes).toBeGreaterThan(0)+ for (const directory of dev.userDirectories) expect(exists(directory)).toBe(true)+ const info = vi.spyOn(logger, 'info')++ const store = await open()++ expect(lastStartupLine(info)).toMatchObject({+ removedIncomplete: 5,+ retainedCompleted: 0,+ retainedBytes: 0,+ adopted: 0,+ })+ for (const directory of dev.userDirectories) expect(exists(directory)).toBe(false)+ expect(await store.debugInventory()).toMatchObject({ bytes: 0, files: 0 })+ const { bytes } = await completedCopy(store, dev.callerRoot, dev.callerIdentity, 2, 64)+ expect(await store.debugInventory()).toMatchObject({ bytes: bytes.byteLength, files: 1 })+ })++ it('R3: a failed retirement is logged, the store is available and the next sweep retires it', async () => {+ const dev = await buildDevStore(hostRoot, {+ ledger: 'records',+ fence: 'v2-matching',+ sqlite: true,+ userDirs: [],+ })+ const warn = vi.spyOn(logger, 'warn')+ const removeFailed = await expiryCount('remove_failed')+ renameFailure.target = '.gfs-download-store'++ const store = await open()++ // Witness: the retirement rename was attempted and failed exactly once.+ expect(renameFailure.injected).toBe(1)+ expect(warn).toHaveBeenCalledWith(+ { component: 'GfsDownloadStore', code: 'EACCES', step: 'rename' },+ expect.stringContaining('could not retire')+ )+ expect(await expiryCount('remove_failed')).toBe(removeFailed + 1)+ expect(store.isAvailable()).toBe(true)+ expect(retirementWarnings(warn)).toEqual([])+ expect(exists(dev.storeRoot)).toBe(true)+ const root = callerDirectory(hostRoot, CALLER)+ const { receipt, bytes } = await completedCopy(store, root, CALLER, 3, 64)+ await expect(store.readManagedFile(receipt.path, CALLER)).resolves.toEqual(bytes)++ // The hourly sweep retries without a restart.+ await expect(store.cleanupExpired()).resolves.toMatchObject({ removeFailed: 0 })++ expect(retirementWarnings(warn)).toHaveLength(1)+ expect(exists(dev.storeRoot)).toBe(false)+ expect(await hostRootEntries()).toEqual(['users'])+ })++ it('R4: after startup the volume has no store directory and only valid input directories', async () => {+ const dev = await buildDevStore(hostRoot, {+ ledger: 'records',+ fence: 'v2-matching',+ sqlite: true,+ userDirs: ['completed', 'transferring-partial', 'quarantined', 'missing', 'orphan-partial'],+ })+ const store = await open()+ const published = await completedCopy(store, dev.callerRoot, dev.callerIdentity, 7, 64)+ await completedCopy(store, callerDirectory(hostRoot, CALLER), CALLER, 8, 64)++ expect(await hostRootEntries()).toEqual(['users'])+ let directories = 0+ for (const user of await fs.readdir(path.join(hostRoot, 'users'))) {+ const downloads = path.join(hostRoot, 'users', user, '.gfs-downloads')+ if (!exists(downloads)) continue+ for (const name of await fs.readdir(downloads)) {+ directories += 1+ expect(name).toMatch(INPUT_RE)+ const directory = path.join(downloads, name)+ expect((await fs.readdir(directory)).sort()).toEqual(['meta.json', 'source'])+ const meta = JSON.parse(await fs.readFile(path.join(directory, 'meta.json'), 'utf8'))+ expect(meta).toMatchObject({ schemaVersion: 1, id: name.slice('input-'.length) })+ const source = await fs.lstat(path.join(directory, 'source'))+ expect(source.isFile()).toBe(true)+ expect(source.size).toBe(meta.sizeBytes)+ }+ }+ // Witness: the predicate walked both published downloads.+ expect(directories).toBe(2)+ expect(exists(downloadDirectory(dev.callerRoot, published.receipt.id))).toBe(true)+ })++ it('R5: a retired directory left by a failed removal is removed by the next sweep and is protected meanwhile', async () => {+ const dev = await buildDevStore(hostRoot, {+ ledger: 'records',+ fence: 'v2-matching',+ sqlite: true,+ userDirs: [],+ })+ const warn = vi.spyOn(logger, 'warn')+ const removeFailed = await expiryCount('remove_failed')+ rmFailure.prefix = RETIRED_PREFIX++ const store = await open()++ // Witness: the old store was renamed and the removal of the renamed tree failed once.+ expect(rmFailure.injected).toBe(1)+ expect(retirementWarnings(warn)).toHaveLength(1)+ expect(warn).toHaveBeenCalledWith(+ { component: 'GfsDownloadStore', code: 'EACCES', step: 'remove' },+ expect.stringContaining('could not retire')+ )+ expect(await expiryCount('remove_failed')).toBe(removeFailed + 1)+ expect(store.isAvailable()).toBe(true)+ expect(exists(dev.storeRoot)).toBe(false)+ const leftovers = (await hostRootEntries()).filter(name => name.startsWith(RETIRED_PREFIX))+ expect(leftovers).toHaveLength(1)+ expect(isProtectedWorkspacePath(`${leftovers[0]}/ledger-v1.json`)).toBe(true)++ await expect(store.cleanupExpired()).resolves.toMatchObject({ removeFailed: 0 })++ expect(await hostRootEntries()).toEqual(['users'])+ })++ it('R6: a retired directory left by an earlier start is removed at startup and is protected meanwhile', async () => {+ const leftover = `${RETIRED_PREFIX}00000000-0000-4000-8000-000000000000`+ await fs.mkdir(path.join(hostRoot, leftover), { mode: 0o700 })+ await fs.writeFile(path.join(hostRoot, leftover, 'ledger-v1.json'), '{}', { mode: 0o600 })+ expect(isProtectedWorkspacePath(`${leftover}/ledger-v1.json`)).toBe(true)+ expect(isProtectedWorkspacePath(leftover)).toBe(true)+ // Witness: the check is a prefix match, not a blanket refusal.+ expect(isProtectedWorkspacePath('.gfs-download-storex/ledger-v1.json')).toBe(false)+ const info = vi.spyOn(logger, 'info')+ const removeFailed = await expiryCount('remove_failed')++ const store = await open()++ expect(store.isAvailable()).toBe(true)+ // Nothing named .gfs-download-store was found, so nothing was retired this start.+ expect(lastStartupLine(info)).toMatchObject({ retiredLegacyStore: false })+ expect(await expiryCount('remove_failed')).toBe(removeFailed)+ expect(exists(path.join(hostRoot, leftover))).toBe(false)+ expect(await hostRootEntries()).toEqual([])+ })++ it('R7: restarting with copies left by this design keeps them for accounting and does not reuse them', async () => {+ const root = callerDirectory(hostRoot, CALLER)+ const first = await open()+ const { receipt, bytes } = await completedCopy(first, root, CALLER, 9, 64)+ // Witness: before the restart the copy is reused and served.+ await expect(first.reusableReceipt(CALLER, sourceFor(9), bytes.byteLength)).resolves.toEqual(+ receipt+ )+ await first.close()+ const info = vi.spyOn(logger, 'info')++ const second = await open()++ expect(lastStartupLine(info)).toMatchObject({+ retiredLegacyStore: false,+ removedIncomplete: 0,+ retainedCompleted: 1,+ retainedBytes: bytes.byteLength,+ adopted: 1,+ })+ expect(await second.debugInventory()).toMatchObject({ bytes: bytes.byteLength, files: 1 })+ await expect(+ second.reusableReceipt(CALLER, sourceFor(9), bytes.byteLength)+ ).resolves.toBeUndefined()+ await expect(second.readManagedFile(receipt.path, CALLER)).rejects.toMatchObject({+ code: 'download_missing',+ })+ expect(exists(downloadDirectory(root, receipt.id))).toBe(true)+ })++ it('R8: after a restart the same file downloads again under a new id that is reused and served', async () => {+ const root = callerDirectory(hostRoot, CALLER)+ const first = await open()+ const before = await completedCopy(first, root, CALLER, 10, 64)+ await first.close()++ const second = await open()+ const after = await completedCopy(second, root, CALLER, 10, 64)++ expect(after.receipt.id).not.toBe(before.receipt.id)+ await expect(second.readManagedFile(after.receipt.path, CALLER)).resolves.toEqual(after.bytes)+ await expect(second.reusableReceipt(CALLER, sourceFor(10), 64)).resolves.toEqual(after.receipt)+ // The adopted copy still counts until it expires or is evicted.+ expect(await second.debugInventory()).toMatchObject({ bytes: 128, files: 2 })+ })+})diff --git a/mcp-host/src/workspace/__tests__/protectedWorkspacePaths.test.ts b/mcp-host/src/workspace/__tests__/protectedWorkspacePaths.test.tsindex a45e2bcf9..37a83f76f 100644--- a/mcp-host/src/workspace/__tests__/protectedWorkspacePaths.test.ts+++ b/mcp-host/src/workspace/__tests__/protectedWorkspacePaths.test.ts@@ -73,6 +73,28 @@ describe('protected workspace paths', () => {await expect(workspace.search('accounting-sentinel')).resolves.toEqual([])})+ it('blocks a retired pre-#1028 store tree and leaves a lookalike name readable', async () => {+ const retired = '.gfs-download-store.retired-00000000-0000-4000-8000-000000000000'+ fs.mkdirSync(path.join(root, retired))+ fs.writeFileSync(path.join(root, retired, 'ledger-v1.json'), 'retired-sentinel', 'utf-8')+ fs.mkdirSync(path.join(root, '.gfs-download-storex'))+ fs.writeFileSync(path.join(root, '.gfs-download-storex', 'notes.md'), 'marmalade', 'utf-8')+ const workspace = new WorkspaceService(root)++ // Witness: the prefix check does not refuse every name that starts alike.+ await expect(workspace.read('.gfs-download-storex/notes.md')).resolves.toBe('marmalade')+ await expect(workspace.search('marmalade')).resolves.toEqual([+ expect.objectContaining({ path: '.gfs-download-storex/notes.md' }),+ ])+ await expect(workspace.read(`${retired}/ledger-v1.json`)).rejects.toBeInstanceOf(+ GfsDownloadPathError+ )+ await expect(workspace.list()).resolves.not.toContainEqual(+ expect.objectContaining({ name: retired })+ )+ await expect(workspace.search('retired-sentinel')).resolves.toEqual([])+ })+it('rejects a workspace root that itself resolves into GFS accounting', async () => {const accountingRoot = path.join(root, '.gfs-download-store')fs.writeFileSync(path.join(accountingRoot, 'MEMORY.md'), 'accounting-sentinel', 'utf-8')diff --git a/mcp-host/src/workspace/protectedPaths.ts b/mcp-host/src/workspace/protectedPaths.tsindex 4b1c99f28..21d8b1f9e 100644--- a/mcp-host/src/workspace/protectedPaths.ts+++ b/mcp-host/src/workspace/protectedPaths.ts@@ -6,6 +6,21 @@ export const PROTECTED_WORKSPACE_DIRS: ReadonlySet<string> = new Set(['.gfs-downloads',])+/**+ * Name prefix of a pre-#1028 store directory the GFS download store renamed+ * before removing it. A tree left by a failed removal still holds copies of+ * user files, so it is protected like the directory it replaced.+ */+export const RETIRED_GFS_DOWNLOAD_STORE_PREFIX = '.gfs-download-store.retired-'++function isRetiredGfsStoreSegment(segment: string): boolean {+ return segment.startsWith(RETIRED_GFS_DOWNLOAD_STORE_PREFIX)+}++function isProtectedSegment(segment: string): boolean {+ return PROTECTED_WORKSPACE_DIRS.has(segment) || isRetiredGfsStoreSegment(segment)+}+export const PROTECTED_STATE_DB_FILES: ReadonlySet<string> = new Set(['state.db','state.db-wal',@@ -30,7 +45,7 @@ export function isProtectedWorkspacePath(relativePath: string): boolean {const segments = normalizeWorkspacePath(relativePath).split('/').filter(segment => segment.length > 0 && segment !== '.')- if (segments.some(segment => PROTECTED_WORKSPACE_DIRS.has(segment))) return true+ if (segments.some(isProtectedSegment)) return trueconst base = segments[segments.length - 1]return base !== undefined && PROTECTED_STATE_DB_FILES.has(base)}@@ -51,7 +66,12 @@ export function isGfsDownloadPath(relativePath: string): boolean {const segments = normalizeWorkspacePath(relativePath).split('/').filter(segment => segment.length > 0 && segment !== '.')- return segments.some(segment => segment === '.gfs-downloads' || segment === '.gfs-download-store')+ return segments.some(+ segment =>+ segment === '.gfs-downloads' ||+ segment === '.gfs-download-store' ||+ isRetiredGfsStoreSegment(segment)+ )}export function stateDbProtectedMessage(filename: string): string {@@ -99,7 +119,7 @@ export function protectedWorkspacePathMessage(relativePath: string): string {function isProtectedAbsoluteRealPath(realPath: string): boolean {if (!path.isAbsolute(realPath)) return falseconst segments = realPath.split(path.sep).filter(segment => segment.length > 0)- if (segments.some(segment => PROTECTED_WORKSPACE_DIRS.has(segment))) return true+ if (segments.some(isProtectedSegment)) return trueconst base = segments[segments.length - 1]return base !== undefined && PROTECTED_STATE_DB_FILES.has(base)}@@ -107,7 +127,12 @@ function isProtectedAbsoluteRealPath(realPath: string): boolean {export function isProtectedGfsRealPath(realPath: string): boolean {if (!path.isAbsolute(realPath)) return falseconst segments = realPath.split(path.sep).filter(segment => segment.length > 0)- return segments.some(segment => segment === '.gfs-download-store' || segment === '.gfs-downloads')+ return segments.some(+ segment =>+ segment === '.gfs-download-store' ||+ segment === '.gfs-downloads' ||+ isRetiredGfsStoreSegment(segment)+ )}/**@@ -120,7 +145,7 @@ export function isProtectedRealPath(realPath: string, realWorkspaceRoot: string)const relative = path.relative(realWorkspaceRoot, realPath)if (relative.startsWith('..') || path.isAbsolute(relative) || relative === '') return falseconst segments = relative.split(path.sep).filter(segment => segment.length > 0)- if (segments.some(segment => PROTECTED_WORKSPACE_DIRS.has(segment))) return true+ if (segments.some(isProtectedSegment)) return trueconst base = segments[segments.length - 1]return base !== undefined && PROTECTED_STATE_DB_FILES.has(base)}diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.jsonindex 5bae17df7..0c1a59946 100644--- a/scripts/ci/pinned-vitest-cases/mcp-host.json+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json@@ -514,5 +514,61 @@{"suite": "gfsDownloadStoreBoundaries.test.ts","fullName": "GFS download store boundaries: path swapped between check and use K3: a read whose caller directory is swapped after the check serves nothing from outside"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: v2 fence with a matching device"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: v2 fence with another device"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: v2 fence without a device"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: v3 fence"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: ledger without a fence"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: live processing lease of a crashed Host"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R2: dev user directories of every state are removed at startup and do not count against quota"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R3: a failed retirement is logged, the store is available and the next sweep retires it"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R4: after startup the volume has no store directory and only valid input directories"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R5: a retired directory left by a failed removal is removed by the next sweep and is protected meanwhile"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R6: a retired directory left by an earlier start is removed at startup and is protected meanwhile"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R7: restarting with copies left by this design keeps them for accounting and does not reuse them"+ },+ {+ "suite": "gfsDownloadStoreRestart.test.ts",+ "fullName": "GFS download store restart R8: after a restart the same file downloads again under a new id that is reused and served"+ },+ {+ "suite": "protectedWorkspacePaths.test.ts",+ "fullName": "protected workspace paths blocks a retired pre-#1028 store tree and leaves a lookalike name readable"}]