Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): bound the GFS cache on the PVC and scope native tool approvals to the task
Alfredo Lopez650c14dbe08d
diff --git a/deploy/overlays/minikube/configmaps/mcp-host-config.yaml b/deploy/overlays/minikube/configmaps/mcp-host-config.yamlindex 3e24bc29c..3ba801023 100644--- a/deploy/overlays/minikube/configmaps/mcp-host-config.yaml+++ b/deploy/overlays/minikube/configmaps/mcp-host-config.yaml@@ -23,7 +23,7 @@ data:CLERUM_SHELL_TIMEOUT: '1500000'CLERUM_TOOL_TIMEOUT: '1500000'MCP_HOST_GFS_MAX_FILE_BYTES: '16777216'- MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '85'+ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '70'MCP_HOST_GFS_CALLER_DOWNLOAD_CONCURRENCY: '1'MCP_HOST_GFS_DOWNLOAD_TTL_HOURS: '168'CLERUM_MCP_TOOL_TIMEOUT_MS: '1500000'diff --git a/desktop-app/test/e2e-playwright/gfs-agent-large-file-cli.test.ts b/desktop-app/test/e2e-playwright/gfs-agent-large-file-cli.test.tsindex 8ce43209d..ff13e2f80 100644--- a/desktop-app/test/e2e-playwright/gfs-agent-large-file-cli.test.ts+++ b/desktop-app/test/e2e-playwright/gfs-agent-large-file-cli.test.ts@@ -5,7 +5,9 @@* E2E contract (e2e-test-guardian):* - Real user journey: login → Files → visible upload of an exact 3,836,961-byte* CSV → exact managed agent chat → governed download → user-visible- * attended shell approvals → completed tool stepper and bounded response.+ * attended shell approval (one card per task; later shell_exec calls in the+ * same task run without a new card) → completed tool stepper and bounded+ * response.* - Business signals: the download step reports a workspace file; execution* completes after attended review; the final response reports the independent* data-record count and complete header. The synthetic file also proves its@@ -154,17 +156,21 @@ async function sendTaskAndWaitForReviewedShell(await expect(commandPreview).toContainText(receiptPath!)await expect(completedToolStepRow(page, 'shell_exec')).toHaveCount(0)// The agent creates its own program from the normal business request. A- // reviewer inspects each complete visible command and approves individually- // in the UI; keyword matching cannot authorize arbitrary generated code.+ // reviewer inspects the complete command shown on the task's single shell+ // approval card and approves it in the UI; that approval covers later+ // shell_exec calls for the rest of the task, so later commands are not+ // reviewed card by card. Keyword matching cannot authorize arbitrary+ // generated code.await test.info().attach('attended-shell-review-required', {contentType: 'text/plain',- body: 'Review each complete command and referenced script, verify read-only access to this receipt and bounded summary output, then approve each request individually in the owned Desktop window.',+ body: "Review the complete command and referenced script shown on the task's shell approval card, verify read-only access to this receipt and bounded summary output, then approve it in the owned Desktop window. That approval covers later shell commands for the rest of this task only; the next user message asks again.",})// Local control becomes actionable only after this test verifies the initial// receipt and unexecuted shell. Every decision still clicks the visible UI.reviewer?.activate()- // Wait on the stable turn while the attending reviewer handles every- // request through the one-use local transport and visible approval button.+ // Wait on the stable turn while the attending reviewer decides the task's+ // shell approval card through the one-use local transport and visible+ // approval button; later shell_exec calls in this task need no new card.// Merely recognizing a program's keywords cannot grant execution consent.await expect(stepper).toHaveClass(/\bstatus-completed\b/, {timeout: RESPONSE_TIMEOUT_MS,diff --git a/desktop-app/test/e2e-playwright/playwright.config.ts b/desktop-app/test/e2e-playwright/playwright.config.tsindex 2bf362ab2..565cba260 100644--- a/desktop-app/test/e2e-playwright/playwright.config.ts+++ b/desktop-app/test/e2e-playwright/playwright.config.ts@@ -169,6 +169,7 @@ export default defineConfig({'**/codex-image-input.spec.ts','**/subscription-image-input.spec.ts','**/native-tool-discovery.spec.ts',+ '**/session-tool-approval-scope.test.ts',],},{diff --git a/desktop-app/test/e2e-playwright/playwright.session-tool-approval.config.ts b/desktop-app/test/e2e-playwright/playwright.session-tool-approval.config.tsnew file mode 100644index 000000000..8cbc3ed4c--- /dev/null+++ b/desktop-app/test/e2e-playwright/playwright.session-tool-approval.config.ts@@ -0,0 +1,21 @@+import { defineConfig } from '@playwright/test'+import base from './playwright.config.js'++// Validate admission before the inherited global setup can contact services.+// A missing opt-in is a failed explicit lane, never a successful all-skipped run.+if (process.env.E2E_SESSION_TOOL_APPROVAL !== '1') {+ throw new Error('Session tool approval lane requires E2E_SESSION_TOOL_APPROVAL=1')+}+// The spec reads Host logs with kubectl; never fall back to a default context.+if (!process.env.E2E_K8S_CONTEXT?.trim()) {+ throw new Error('Session tool approval lane requires explicit E2E_K8S_CONTEXT')+}++export default defineConfig({+ ...base,+ // Four real LLM turns with attended approvals run in one serial test.+ timeout: 1_500_000,+ projects: [+ { name: 'session-tool-approval', testMatch: '**/session-tool-approval-scope.test.ts' },+ ],+})diff --git a/desktop-app/test/e2e-playwright/session-tool-approval-scope.test.ts b/desktop-app/test/e2e-playwright/session-tool-approval-scope.test.tsnew file mode 100644index 000000000..95c762006--- /dev/null+++ b/desktop-app/test/e2e-playwright/session-tool-approval-scope.test.ts@@ -0,0 +1,456 @@+/**+ * E2E_GUARDIAN_IPC_FLOW: Desktop sends through main-process IPC. The end user+ * signs in through the visible login fixture, opens the chatllm Agent Chat and+ * decides every tool approval card in the owned Desktop window. Each journey+ * step is checked three ways: approval cards that appeared in the UI (a DOM+ * ledger), the owned Host's own `Tool requires approval` log events for the+ * same window, and the completed tool steps in the response stepper.+ *+ * Contract under test (mcp-host per-task approval scope for session-scoped+ * native tools shell_exec, http_request and cron_manage):+ * T1 a plain approval of shell_exec covers later shell_exec calls in the same+ * task: one card, two completed shell steps.+ * T2 the next user message is a new task: shell_exec asks again.+ * T3 a shell_exec approval does not cover http_request: it gets its own card.+ * T4 a shell_exec approval does not cover cron_manage (action "list"): it gets+ * its own card. cron_manage create/enable on stateless Hosts always asks,+ * which is a separate fix and out of scope here.+ * T5 (opt-in) an MCP tool approval does not cover a later shell_exec.+ * "Always" approval is not reachable from the Desktop UI (it offers Approve and+ * Deny only), so it is covered by mcp-host unit tests, not by this lane.+ *+ * Dedicated, explicitly opted-in lane against a real chatllm Host and its+ * configured LLM; nothing is mocked. Run from desktop-app/ with the branch+ * profile's port-forwards held (Control API, Profiles, Desktop gateway):+ *+ * npm run build+ * E2E_SESSION_TOOL_APPROVAL=1 E2E_K8S_CONTEXT=<branch-profile> \+ * npx playwright test --config test/e2e-playwright/playwright.session-tool-approval.config.ts+ *+ * Add E2E_SESSION_TOOL_APPROVAL_MCP=1 to also run T5; it needs the MongoDB MCP+ * server attached to the chatllm Host (same prerequisite as chat.test.ts test 5).+ */+import type { Locator, Page, TestInfo } from '@playwright/test'+import { randomUUID } from 'node:crypto'+import { expect, test } from './fixtures.js'+import {+ CHATLLM_HOST_REF,+ enterChatllmChat,+ expandResponseToolDetails,+ humanClick,+ kubectl,+ sendChatPrompt,+ startFreshThread,+} from './workflowAgentChatTools.js'++const RUN_ID = randomUUID().replace(/-/g, '').slice(0, 8)+const TURN_TIMEOUT = 300_000+const CARD_TIMEOUT = 180_000+const RUN_MCP_STEP = process.env.E2E_SESSION_TOOL_APPROVAL_MCP === '1'++type ApprovalCard = { label: string; preview: string }+type Decision = 'approve' | 'deny'+type ExpectedCard = { label: RegExp; preview?: string; decision: Decision }+type HostLogEvent = Record<string, unknown> & { msg?: string }+type LedgerWindow = Window & { __staCards?: ApprovalCard[]; __staObserver?: MutationObserver }++function markerFor(testInfo: TestInfo, step: string): string {+ const testId = testInfo.testId.replace(/[^A-Za-z0-9]/g, '').slice(0, 16)+ return `STA_${RUN_ID}_${testId}_${step}`+}++function shellCommand(marker: string): string {+ return `printf ${marker}`+}++function escapeRegExp(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')+}++function hostEventsSince(since: string): HostLogEvent[] {+ const output = kubectl([+ '-n',+ 'mcp-host',+ 'logs',+ `deploy/${CHATLLM_HOST_REF}`,+ `--since-time=${since}`,+ ])+ return output+ .split('\n')+ .filter(line => line.startsWith('{'))+ .map(line => JSON.parse(line) as HostLogEvent)+}++function approvalToolNamesSince(since: string): string[] {+ return hostEventsSince(since)+ .filter(event => event.msg === 'Tool requires approval')+ .map(event => String(event.toolName))+}++function selectedToolNamesSince(since: string): string[] {+ return hostEventsSince(since)+ .filter(event => event.msg === 'Tool calls selected')+ .flatMap(event => (event.toolNames as string[] | undefined) ?? [])+}++/**+ * Records every approval card that is ever rendered, keyed by DOM identity, so a+ * card that appears and is decided between two polls is still counted.+ */+async function installCardLedger(page: Page): Promise<void> {+ await page.evaluate(() => {+ const w = window as LedgerWindow+ if (w.__staObserver) return+ const cards: ApprovalCard[] = []+ const seen = new WeakSet<Element>()+ const record = (): void => {+ for (const button of Array.from(+ document.querySelectorAll('[data-testid="approval-approve-btn"]')+ )) {+ if (seen.has(button)) continue+ seen.add(button)+ const stepper = button.closest('[data-testid="progress-stepper"]')+ cards.push({+ label: stepper?.querySelector('.stepper-suspended-label')?.textContent?.trim() ?? '',+ preview:+ stepper+ ?.querySelector('[data-testid="approval-input-preview"] pre')+ ?.textContent?.trim() ?? '',+ })+ }+ }+ w.__staCards = cards+ w.__staObserver = new MutationObserver(record)+ w.__staObserver.observe(document.body, { childList: true, subtree: true })+ record()+ })+}++async function readCardLedger(page: Page): Promise<ApprovalCard[]> {+ const cards = await page.evaluate(() => {+ const w = window as LedgerWindow+ return w.__staObserver && w.__staCards ? [...w.__staCards] : null+ })+ if (!cards) throw new Error('The approval card ledger is not installed in the Desktop window')+ return cards+}++function matchesCard(card: ApprovalCard, expected: ExpectedCard): boolean {+ return (+ expected.label.test(card.label) &&+ (expected.preview === undefined || card.preview.includes(expected.preview))+ )+}++/** Locates the suspended stepper that renders this specific card. */+function suspendedCard(page: Page, expected: ExpectedCard): Locator {+ let stepper = page+ .locator('[data-testid="progress-stepper"].status-suspended')+ .filter({ has: page.locator('.stepper-suspended-label', { hasText: expected.label }) })+ if (expected.preview !== undefined) {+ stepper = stepper.filter({+ has: page.getByTestId('approval-input-preview').filter({ hasText: expected.preview }),+ })+ }+ return stepper+}++type TurnResult = {+ response: Locator+ cards: ApprovalCard[]+ approvalLog: string[]+ selectedTools: string[]+}++/**+ * Sends one user message, decides exactly the expected cards in order, and+ * fails as soon as any further card appears before the turn settles.+ */+async function runTurn(+ page: Page,+ testInfo: TestInfo,+ name: string,+ prompt: string,+ expectedCards: ExpectedCard[],+ settled: RegExp[]+): Promise<TurnResult> {+ const since = new Date().toISOString()+ const ledgerBefore = (await readCardLedger(page)).length+ const responseIndex = await sendChatPrompt(page, prompt)+ const response = page.getByTestId('agent-response').nth(responseIndex)++ for (const [index, expected] of expectedCards.entries()) {+ await expect+ .poll(async () => (await readCardLedger(page)).length - ledgerBefore, {+ message: `${name}: approval card ${index + 1} (${expected.label}) must appear`,+ timeout: CARD_TIMEOUT,+ intervals: [500, 1_000],+ })+ .toBeGreaterThanOrEqual(index + 1)+ const card = (await readCardLedger(page))[ledgerBefore + index]+ expect(+ matchesCard(card, expected),+ `${name}: card ${index + 1} was ${JSON.stringify(card)}, expected ${expected.label}${+ expected.preview ? ` with ${expected.preview}` : ''+ }`+ ).toBe(true)++ const stepper = suspendedCard(page, expected)+ await expect(stepper).toHaveCount(1, { timeout: CARD_TIMEOUT })+ const button = stepper.getByTestId(+ expected.decision === 'approve' ? 'approval-approve-btn' : 'approval-deny-btn'+ )+ await expect(button).toBeVisible()+ await humanClick(button, { beforeMs: [700, 1_200], afterMs: [700, 1_200] })+ await expect(stepper).toHaveCount(0, { timeout: 60_000 })+ }++ // Either the turn settles with its business signals or an unexpected card+ // appears; the second outcome fails immediately with the card it showed.+ const outcome = async (): Promise<'extra-card' | 'settled' | 'waiting'> => {+ if ((await readCardLedger(page)).length - ledgerBefore > expectedCards.length) {+ return 'extra-card'+ }+ if (!(await response.isVisible())) return 'waiting'+ const text = (await response.textContent()) ?? ''+ return settled.every(pattern => pattern.test(text)) ? 'settled' : 'waiting'+ }+ await expect+ .poll(outcome, {+ message: `${name}: the turn must settle or show an unexpected card`,+ timeout: TURN_TIMEOUT,+ intervals: [1_000],+ })+ .not.toBe('waiting')+ const cards = (await readCardLedger(page)).slice(ledgerBefore)+ expect(cards, `${name}: approval cards rendered in this turn`).toHaveLength(expectedCards.length)+ expect(await outcome()).toBe('settled')++ const expectedLogLength = expectedCards.length+ await expect+ .poll(() => approvalToolNamesSince(since).length, {+ message: `${name}: Host approval events for this turn`,+ timeout: 30_000,+ })+ .toBeGreaterThanOrEqual(expectedLogLength)+ const approvalLog = approvalToolNamesSince(since)+ const selectedTools = selectedToolNamesSince(since)+ await testInfo.attach(`${name}-evidence`, {+ body: JSON.stringify({ since, cards, approvalLog, selectedTools }, null, 2),+ contentType: 'application/json',+ })+ return { response, cards, approvalLog, selectedTools }+}++/** Completed Shell rows of the response, with their command and visible output. */+async function completedShellSteps(+ response: Locator+): Promise<Array<{ input: string; output: string }>> {+ await expandResponseToolDetails(response)+ const stepper = response.getByTestId('progress-stepper').last()+ await expect(stepper).toHaveClass(/status-completed/)+ const rows = stepper+ .locator('.stepper-step:has(.stepper-step-icon.state-completed)')+ .filter({ hasText: /Shell.*shell_exec/i })+ const count = await rows.count()+ const steps: Array<{ input: string; output: string }> = []+ for (let index = 0; index < count; index += 1) {+ const row = rows.nth(index)+ const input = row.locator('xpath=following-sibling::*[1][@data-testid="step-input-preview"]')+ await expect(input).toBeVisible()+ await humanClick(row, { afterMs: [300, 600] })+ const output = row.locator(+ 'xpath=following-sibling::*[position()<=2][@data-testid="step-output-panel"]'+ )+ await expect(output).toBeVisible()+ steps.push({+ input: (await input.textContent())?.trim() ?? '',+ output: (await output.textContent())?.trim() ?? '',+ })+ }+ return steps+}++function expectShellStep(+ steps: Array<{ input: string; output: string }>,+ marker: string,+ context: string+): void {+ const step = steps.find(candidate => candidate.input === shellCommand(marker))+ expect(step, `${context}: completed Shell step for ${marker}`).toBeDefined()+ expect(step?.output, `${context}: Shell output for ${marker}`).toContain(marker)+}++test.beforeAll(() => {+ expect(process.env.E2E_SESSION_TOOL_APPROVAL, 'Use the dedicated, opted-in lane').toBe('1')+ // Host log evidence is read from deploy/<host>; with several replicas it could+ // come from a pod that did not serve the turn.+ const replicas = kubectl([+ '-n',+ 'mcp-host',+ 'get',+ 'deploy',+ CHATLLM_HOST_REF,+ '-o',+ 'jsonpath={.status.replicas}',+ ]).trim()+ expect(replicas, `deploy/${CHATLLM_HOST_REF} must run exactly one replica`).toBe('1')+})++test.describe.serial('per-task tool approval scope', () => {+ test('a plain approval covers one task and one session-scoped tool only', async ({+ appPage,+ }, testInfo) => {+ const markerA = markerFor(testInfo, 'T1A')+ const markerB = markerFor(testInfo, 'T1B')+ const markerC = markerFor(testInfo, 'T2C')+ const markerD = markerFor(testInfo, 'T3D')+ const markerE = markerFor(testInfo, 'T4E')+ const shellCard = (marker: string, decision: Decision): ExpectedCard => ({+ label: /^Shell requires approval$/,+ preview: shellCommand(marker),+ decision,+ })++ await test.step('end user opens a fresh chatllm thread', async () => {+ await enterChatllmChat(appPage)+ await startFreshThread(appPage)+ await installCardLedger(appPage)+ expect(await readCardLedger(appPage)).toEqual([])+ })++ await test.step('T1: one task with two shell_exec calls shows one Shell card', async () => {+ const prompt = [+ `Use the native shell_exec tool to run exactly this command: ${shellCommand(markerA)}`,+ `After you see its output, call shell_exec again, as a separate call, to run exactly: ${shellCommand(markerB)}`,+ 'Do not combine the commands and do not use any other tool.',+ 'Then reply with both outputs verbatim.',+ ].join(' ')+ const turn = await runTurn(+ appPage,+ testInfo,+ 'T1',+ prompt,+ [shellCard(markerA, 'approve')],+ [new RegExp(escapeRegExp(markerA)), new RegExp(escapeRegExp(markerB))]+ )+ // Liveness witness for "no second card": the second call was selected and+ // completed in this same task.+ expect(+ turn.selectedTools.filter(tool => tool === 'shell_exec').length+ ).toBeGreaterThanOrEqual(2)+ const steps = await completedShellSteps(turn.response)+ expect(steps.length).toBeGreaterThanOrEqual(2)+ expectShellStep(steps, markerA, 'T1')+ expectShellStep(steps, markerB, 'T1')+ expect(turn.approvalLog).toEqual(['shell_exec'])+ })++ await test.step('T2: the next user message asks for shell_exec again', async () => {+ const prompt = [+ `Use the native shell_exec tool once to run exactly: ${shellCommand(markerC)}`,+ 'Do not use any other tool, then reply with its output verbatim.',+ ].join(' ')+ const turn = await runTurn(+ appPage,+ testInfo,+ 'T2',+ prompt,+ [shellCard(markerC, 'approve')],+ [new RegExp(escapeRegExp(markerC))]+ )+ // Same thread: the T1 task is still on screen.+ await expect(appPage.getByTestId('message-list')).toContainText(shellCommand(markerA))+ const steps = await completedShellSteps(turn.response)+ expectShellStep(steps, markerC, 'T2')+ expect(turn.approvalLog).toEqual(['shell_exec'])+ })++ await test.step('T3: an approved shell_exec does not cover http_request', async () => {+ const prompt = [+ `First use the native shell_exec tool to run exactly: ${shellCommand(markerD)}`,+ 'After you see its output, use the native http_request tool exactly once to make a GET request to https://example.com/.',+ 'Do not use any other tool and wait for me to decide each approval request in this app.',+ 'Then reply with the shell output verbatim and the outcome of the HTTP request.',+ ].join(' ')+ const turn = await runTurn(+ appPage,+ testInfo,+ 'T3',+ prompt,+ [shellCard(markerD, 'approve'), { label: /^HTTP requires approval$/, decision: 'deny' }],+ [/http_request.*denied by the user/i]+ )+ const steps = await completedShellSteps(turn.response)+ expectShellStep(steps, markerD, 'T3')+ expect(turn.approvalLog).toEqual(['shell_exec', 'http_request'])+ })++ await test.step('T4: an approved shell_exec does not cover cron_manage list', async () => {+ const prompt = [+ `First use the native shell_exec tool to run exactly: ${shellCommand(markerE)}`,+ 'After you see its output, use the native cron_manage tool exactly once with action "list".',+ 'Do not create, enable or change any job, do not use any other tool, and wait for me to decide each approval request in this app.',+ 'Then reply with the shell output verbatim and the outcome of the cron_manage call.',+ ].join(' ')+ const turn = await runTurn(+ appPage,+ testInfo,+ 'T4',+ prompt,+ [+ shellCard(markerE, 'approve'),+ { label: /^cron_manage requires approval$/, decision: 'deny' },+ ],+ [/cron_manage.*denied by the user/i]+ )+ const steps = await completedShellSteps(turn.response)+ expectShellStep(steps, markerE, 'T4')+ expect(turn.approvalLog).toEqual(['shell_exec', 'cron_manage'])+ })+ })++ test('an MCP tool approval does not cover a later shell_exec', async ({ appPage }, testInfo) => {+ test.skip(+ !RUN_MCP_STEP,+ 'Set E2E_SESSION_TOOL_APPROVAL_MCP=1 when the MongoDB MCP server is attached to chatllm.'+ )+ const markerF = markerFor(testInfo, 'T5F')++ await test.step('end user opens a fresh chatllm thread', async () => {+ await enterChatllmChat(appPage)+ await startFreshThread(appPage)+ await installCardLedger(appPage)+ expect(await readCardLedger(appPage)).toEqual([])+ })++ await test.step('T5: the Shell card still appears after an MCP approval', async () => {+ const prompt = [+ 'First list all MongoDB databases available using the mongodb tools.',+ `After you see the result, use the native shell_exec tool once to run exactly: ${shellCommand(markerF)}`,+ 'Do not use any other tool and wait for me to decide each approval request in this app.',+ 'Then reply with the database names and the shell output verbatim.',+ ].join(' ')+ const turn = await runTurn(+ appPage,+ testInfo,+ 'T5',+ prompt,+ [+ { label: /^(?!Shell requires approval$).+ requires approval$/, decision: 'approve' },+ {+ label: /^Shell requires approval$/,+ preview: shellCommand(markerF),+ decision: 'approve',+ },+ ],+ [new RegExp(escapeRegExp(markerF))]+ )+ const steps = await completedShellSteps(turn.response)+ expectShellStep(steps, markerF, 'T5')+ expect(turn.approvalLog).toHaveLength(2)+ expect(turn.approvalLog[0]).toContain('__')+ expect(turn.approvalLog[1]).toBe('shell_exec')+ })+ })+})diff --git a/docs/architecture/overview.md b/docs/architecture/overview.mdindex 367762ed3..3b6218181 100644--- a/docs/architecture/overview.md+++ b/docs/architecture/overview.md@@ -1657,6 +1657,11 @@ Approving any MCP tool automatically approves **all tools from the same MCP serv| LLM calls 3 tools from `airtable-server` | 1 (first tool prompts, rest auto-approved) || LLM calls tools from `airtable-server` + `mongodb-server` | 2 (one per server) || Native tools (no `__` in name) | Per-tool (unchanged) |+| `shell_exec` called 5 times, then `http_request` | 2 (one per tool, for the rest of the task) |++`shell_exec`, `http_request` and `cron_manage` (`SESSION_SCOPED_APPROVAL_TOOLS` in `approvalController.ts`) are approved per tool. A plain approval covers that tool for the rest of the current task, including every later iteration and the resume after another tool's approval card; the next user message starts a new task and asks again. An "always" approval covers the tool in later tasks while the conversation stays in memory. Approving one stores only its own name, never the turn-wide `'*'`, and a `'*'` granted by another approval does not cover them. A Host restart or cold resume asks again. A guardrail `ask` approval authorizes only that exact call.++On a stateless Host that allows cron management, `cron_manage` `create` and `enable` ask on every call. The card has `exact_invocation` scope: approving it runs only that call and stores nothing, and no stored approval (`'*'`, a per-task or "always" `cron_manage` approval, a server prefix) covers such a call. Other `cron_manage` actions follow the per-task rule above.**Channel commands:**diff --git a/docs/operations/gfs-large-file-host-downloads.md b/docs/operations/gfs-large-file-host-downloads.mdindex 228756d57..d5d7004f7 100644--- a/docs/operations/gfs-large-file-host-downloads.md+++ b/docs/operations/gfs-large-file-host-downloads.md@@ -50,7 +50,7 @@ without discarding the path, checksum, version or retained original.| Environment variable | Default | Ceiling | Meaning || ------------------------------------------ | ---------: | ----------: | ------------------------------------------------------------------ || `MCP_HOST_GFS_MAX_FILE_BYTES` | `16777216` | `209715200` | Largest GFS source admitted by MCP Host. |-| `MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT` | `85` | `100` | Retained download budget, as a percentage of the workspace volume. |+| `MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT` | `70` | `100` | Retained download budget, as a percentage of the workspace volume. || `MCP_HOST_GFS_CALLER_DOWNLOAD_CONCURRENCY` | `1` | `2` | Simultaneous active transfers per caller. || `MCP_HOST_GFS_DOWNLOAD_TTL_HOURS` | `168` | `8760` | Retention period for a completed copy. |@@ -58,6 +58,13 @@ All values are parsed as positive decimal byte/percent/count/hour integers. Empt`MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES`, `MCP_HOST_GFS_CALLER_DOWNLOAD_STORAGE_BYTES` and `MCP_HOST_GFS_CALLER_DOWNLOAD_MAX_FILES` were removed. The per-user byte and file limits were removed by user decision: a fixed byte budget did not follow the size of the workspace volume. A Host that still has any of them set starts normally, ignores it and logs one `warn` `GFS download store ignores a removed retained-storage variable; the budget is MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT of the workspace volume` per variable, naming the variable and never its value.+The retained cache limit therefore moved from a fixed 1 GiB per Host (dev,+`gfsFilePolicy.ts`) to 70% of the workspace PVC, with a free-space floor of+`max(15% of the volume, 16 MiB)` kept free. Copies that running tasks protect+(pinned or being downloaded) are capped at half the budget per caller and three+quarters of it for all callers together: 3.5 GiB and 5.25 GiB on a 10 Gi+volume at 70% (see [Quotas and eviction](#quotas-and-eviction)).+The supported end-to-end size is the minimum of the applicable Desktop/GFS upload limit, GFSC transport limits, MCP Host admission, workspace PVC capacity, download quotas, and provider-specific visual limits. Raising only MCP Host admission does not raise any other layer.## Delivery behavior@@ -102,7 +109,8 @@ signal and remaining execution duration.Repeated preparation first reauthorizes the remote metadata, then looks for acopy that this Host process published for the same caller, drive, resource,-version and size and that has not expired. It re-hashes that copy and checks+version and size and that has at least one hour left before it expires; a copy+with less is a cache miss and is neither reused nor pinned. It re-hashes that copy and checksits private mode and the inode it published. A valid copy keeps its downloadID, path and expiry without another content transfer or quota reservation. Anymismatch removes the copy and the file is downloaded again under a new ID;@@ -120,11 +128,18 @@ a new ID, after fresh GFS authorization.`shell_exec` remains the trust boundary:- The working directory and `HOME` are the caller workspace. A supplied allowlisted or dynamic `HOME` cannot relocate execution outside it.-- `shell_exec` follows the same approval rules as every other native tool,- such as `http_request` (as before #979): approving one call authorizes the- rest of that turn, and "always" approval authorizes later turns. A guardrail- rule with `action: 'ask'` on `shell_exec` restores per-call approval.- Persisted approvals without a scope are treated as individual invocations.+- `shell_exec`, `http_request` and `cron_manage` are approved per tool. A plain+ approval of one of them covers that tool for the rest of the current task:+ every iteration, including resumes after other approvals in the same task.+ The next user message starts a new task and asks again. An "always" approval+ keeps the tool approved for later tasks of the conversation while the+ conversation stays in memory. Either approval stores only that tool's name+ and never adds `'*'`, and a `'*'` granted by another approval (an MCP tool,+ `workflow_trigger`) does not cover these three tools, so a `shell_exec`+ approval does not approve `http_request` or `cron_manage`. A Host restart or+ a cold resume asks again. A guardrail rule with `action: 'ask'`+ (`exact_invocation`) or a persisted legacy approval without a scope+ authorizes only that one frozen call.- 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.- `shell_exec` never calls the GFS download store (#1019). It takes nothingfrom the store, does not depend on `store.isAvailable()`, and keeps@@ -142,9 +157,18 @@ a new ID, after fresh GFS authorization.### Accepted risks-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`.+1. A retained copy can be removed while a command is using it:+ (a) it expires during the command; a reused copy has at least one hour left+ (a copy with less is downloaded again) and a fresh download has the full+ TTL; (b) it is an unpinned copy from an earlier task whose pin was released,+ which an admission or the free-space restore can evict; (c) after a Host+ restart no pin exists, because pins are held in memory, and the copies found+ on disk are adopted and evicted first; (d) a process started with `setsid`+ outlives its call and its task and keeps using the copy. In every case a+ file descriptor that is already open keeps reading, a later open gets+ `ENOENT`, and the model downloads the file again. The store does not detect+ open descriptors (it does not scan `/proc`): the scan would be racy and+ Linux-only, and a descriptor held open would block removal indefinitely.2. A command can alter or delete a retained copy. Reuse and full managed readsre-hash the copy against the digest this process holds in memory and checkthat the file is still the inode it published. On a mismatch the copy is@@ -156,10 +180,12 @@ a new ID, after fresh GFS authorization.sent. Altered bytes therefore never reach a model, but an altered copy canstay on disk until its next reuse, full managed read, expiry or eviction. Adeleted copy is a cache miss.-3. Executors share the Host UID and run without a sandbox or chroot. Live- approval, `BasicSafety` validation and credential-slot stripping remain the- shell's controls. The store has no lease or lock that a shell holds or- waits on.+3. Executors share the Host UID and run without a sandbox or chroot. The+ shell's controls are approval (one approval of `shell_exec` covers the rest+ of the task, and "always" covers later tasks of the conversation; see risk+ 8), `BasicSafety` validation and credential-slot stripping. A guardrail rule+ with `action: 'ask'` on `shell_exec` restores per-call approval. The store+ has no lease or lock that a shell holds or waits on.4. No admission or cleanup runs before a shell, so a command can read anexpired copy until the next sweep removes it.5. Caller isolation is a property of the managed store APIs, not of the shell.@@ -190,17 +216,14 @@ a new ID, after fresh GFS authorization.if the parent moved). The windows that remain can be won only by a processrunning with the Host UID, which can already alter or delete those filesdirectly (risk 3); winning one gives no capability beyond that.-8. A turn-wide approval of `shell_exec`, like any turn-wide approval,- auto-approves every approval-gated tool for the rest of the turn, not only- shell commands: `http_request`, `cron_manage`, `file_write` and MCP tools run- without a new approval card, including calls the model issues after it has- read untrusted GFS content in that turn. This is the behaviour of an- `http_request` approval, and of a `shell_exec` approval before #979. An- "always" approval also keeps `shell_exec` approved in later turns. The shell- runs with the Host UID. A configured `clerum__gfs_download` approval is still- asked for each call. Operators who need per-call approval add a guardrail- rule with `action: 'ask'` for `shell_exec` and for any other tool that must- be asked each time.+8. One approval of `shell_exec` covers later shell commands for the rest of the+ task (with "always", for the rest of the conversation while it stays in+ memory), including commands the model issues after it has read untrusted+ GFS content. The shell runs with the Host UID. The approval covers only+ `shell_exec`: `http_request` and `cron_manage` are asked for separately. A+ configured `clerum__gfs_download` approval is still asked for each call.+ Operators who need per-call approval add a guardrail rule with+ `action: 'ask'` for `shell_exec`, which restores per-call approval.Partial reads are not possible, because publication is an atomic `rename` of`source.partial`.@@ -327,7 +350,7 @@ sweep could not remove, in every caller's directory. The budget isvolume bytes are `blocks × bsize` from the same `statfs` of the Host root thatthe free-space check reads at each admission, so a resized volume counts fromthe next admission without a restart. By default the cache can therefore use-up to 85% of the volume. An admission is refused with `host_quota_exceeded`+up to 70% of the volume. An admission is refused with `host_quota_exceeded`(reason `storage_bytes`) when usage plus its size would exceed the budget; ausage exactly at the budget is admitted. There is no per-caller file count andno per-caller limit on unpinned copies: those per-user limits were removed by@@ -337,26 +360,35 @@ budget has room.Eviction cannot reclaim what a caller protects: the unexpired copies its taskspin and its active reservations. A caller's protected bytes (each copy once,whichever of its tasks pins it) may therefore reach at most `floor(budget / 2)`.-An admission that would take a caller over that half, or a reuse that would pin-a copy the caller does not protect yet, is refused with `host_quota_exceeded`-(scope `caller`, reason `protected_bytes`) before any eviction. The decision and-the error use only that caller's own usage; the error is the same as a full-Host budget. A single download larger than half the budget is always refused-(about 4.25 GiB on a 10 Gi volume at 85%, far above the per-file limit).-Re-pinning a copy the caller already protects adds nothing and is decided-before the volume is measured.--The cap bounds each caller, not their sum. Two callers at their caps together-protect `2 × floor(budget / 2)` bytes, that is the budget minus `budget mod 2`:-the whole budget when it is even, all but one byte when it is odd. Every-admission that does not fit in the rest is then refused (with an even budget,-every admission of a positive size) until a task releases its pins or the-pinned copies expire. This is an accepted risk: a pin never outlives the copy's-`expiresAt` (see [Retention pins](#retention-pins-and-cold-resume)), so each-pinned copy holds its share for at most the retention period,-`MCP_HOST_GFS_DOWNLOAD_TTL_HOURS` (168 hours by default). Callers whose tasks-keep pinning freshly downloaded copies, each with a new expiry, can keep the-budget saturated for longer than one retention period.+The protected bytes of all callers together (each copy once) may reach+`floor(budget × 3 / 4)`. An admission that would take a caller over its half,+or a reuse that would pin a copy the caller does not protect yet, is refused+with `host_quota_exceeded` (scope `caller`, reason `protected_bytes`) before+any eviction; one that would take the Host total over three quarters is+refused the same way (scope `host`, reason `protected_bytes`). The error is the+same as a full Host budget and carries nothing about other callers. A single+download larger than half the budget is always refused (about 3.5 GiB on a+10 Gi volume at 70%, far above the per-file limit). Re-pinning a copy the+caller already protects adds nothing and is decided before the volume is+measured.++On a 10 Gi volume at the default 70% the budget is 7 GiB: one caller protects+at most 3.5 GiB, and all callers together at most 5.25 GiB. A caller at its cap+leaves 1.75 GiB for the other callers' protected copies, so a single caller+cannot block the others' admissions; reaching the Host-wide cap takes at least+two callers. When it is reached, every caller's admission that needs protected+room is refused until a task releases its pins or the pinned copies expire.++Because the protected bytes and a new request never exceed three quarters of+the budget, evicting copies that nobody protects can always bring usage under+the budget, and at least a quarter of the budget (1.75 GiB in the example+above) always remains for unprotected copies. A pin never outlives the copy's+`expiresAt` (see+[Retention pins](#retention-pins-and-cold-resume)), so each pinned copy holds+its share for at most the retention period, `MCP_HOST_GFS_DOWNLOAD_TTL_HOURS`+(168 hours by default). Tasks that keep pinning freshly downloaded copies, each+with a new expiry, can keep the protected bytes at their cap for longer than+one retention period.Before refusing, the store plans an eviction of complete copies that areunpinned or pinned but already expired and are not being transferred, in any@@ -370,13 +402,25 @@ computed before anything is deleted: when no plan fits, nothing is deleted andthe admission is refused. Eviction candidates are not hashed.Shell writes do not trigger eviction. Files a command writes elsewhere in a-workspace share the volume with the cache but are not charged to the budget, so-a full disk can make a shell write fail with `ENOSPC` until the next download-admission evicts unpinned copies to free space or the next sweep removes-expired ones. Nothing evicts copies between admissions.+workspace share the volume with the cache but are not charged to the budget. On+stateless Hosts `state.db` shares it too: the Host's `state/` directory is a+subPath of the same PVC. The store therefore keeps a free-space floor of+`max(15% of the volume, 16 MiB)`. When the store initializes, after the startup+sweep, and on every 5-minute cycle, a volume below the floor is restored by+evicting unpinned copies (pinned ones already expired included) in the+eviction order above until free space reaches the floor plus 5% of the volume,+or no candidate is left; a partial restore is kept. Each copy removed this way+is counted as `expired_removed`, and the restore logs one `warn` with counts+only. On a 10 Gi PVC at the default 70% the cache uses at most its 7 GiB budget+while at least 1.5 GiB stay free, and a restore that runs frees space up to+2 GiB. The floor cannot cover three cases: a writer that fills the volume+between two restores (a shell write then fails with `ENOSPC` until the next+cycle or admission), pinned copies and active reservations, which are never+evicted and may reach three quarters of the budget, and `state.db` growth beyond what+evicting every unpinned copy frees.Free space must cover the block-rounded size of the new download, every-active reservation and a 16 MiB margin. The admission sweep runs first, so+active reservation and the free-space floor. The admission sweep runs first, soexpired copies (pinned ones included) and incomplete directories are removedbefore the volume is measured and never cause a `disk_full`. One eviction planthen covers both deficits: the bytes over the budget and the free-space@@ -386,7 +430,7 @@ are not cache copies (other workspace files, other services on the volume)therefore make the store evict unpinned copies instead of refusing. The checksrun in this order, all before anything is deleted: if the evictable copiescannot cover the free-space deficit, the admission is refused with `disk_full`-(reason `free_space`); then the per-caller protected cap; then, if no plan fits+(reason `free_space`); then the per-caller and Host-wide protected caps; then, if no plan fitsthe budget, `host_quota_exceeded`. After the eviction the budget and the freespace are checked again, and a volume still short is refused with`disk_full`. A volume that cannot hold the download therefore costs no cached@@ -411,8 +455,8 @@ lost by this, because a restart already makes every copy adopted.### Expiry and sweeps-A sweep runs when the store initializes, before every admission, and hourly-after that. It walks `users/*/.gfs-downloads/input-*` without following+A sweep runs when the store initializes, before every admission, and every 5+minutes after that. It walks `users/*/.gfs-downloads/input-*` without followingsymlinks. It removes incomplete directories and expired copies, pinned onesincluded. It never touches a directory that is an active transfer of this process,and it never replaces a published copy: another directory carrying the same ID@@ -526,8 +570,8 @@ using the same owner ID holds a different pin. Pins are held in memory, so aftera restart a resumed task finds its copy unpinned and not reusable, and downloadsit again. A shell command never pins a copy. A pin is not bounded by the task'sactive-time budget (a task waiting for approval keeps its pins), which is why a-caller's pinned and reserved bytes are capped at half the budget (see Quotas and-eviction).+caller's pinned and reserved bytes are capped at half the budget, and those of+all callers at three quarters (see Quotas and eviction).### Integrity@@ -543,19 +587,19 @@ byte is sent to a model.### Errors visible to the model-| Code | Meaning |-| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |-| `host_quota_exceeded` | The Host's retained bytes would exceed the volume budget and eviction cannot make room, or the caller's protected bytes would exceed its cap of half the budget. |-| `disk_full` | Free space on the Host volume cannot hold the download, every active reservation and the 16 MiB margin. |-| `volume_unmeasurable` | The Host volume's `statfs` was invalid on two consecutive readings, so no budget or free space could be computed. |-| `limit_exceeded` | The declared size is not a non-negative integer up to `MCP_HOST_GFS_MAX_FILE_BYTES`. |-| `download_busy` | Too many active transfers, the store is closed or closing, or no such transfer exists. |-| `download_missing` | No copy this process published for this caller has that path, or the copy failed verification. |-| `download_expired` | The copy or the transfer is past its expiry. |-| `publication_cancelled` | The task was cancelled or ran out of time while the copy was being verified or published. |-| `storage_write_failed` | A filesystem operation failed during admission, publication or removal. |-| `workspace_unavailable` | The store is not initialized, or the Host root or caller directory is not a real directory inside the Host root. |-| `caller_mismatch` | The request carries no caller identity, one that is not its caller directory's key, or a malformed retention owner ID. |+| Code | Meaning |+| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `host_quota_exceeded` | The Host's retained bytes would exceed the volume budget and eviction cannot make room, or the caller's protected bytes would exceed its cap of half the budget, or all callers' protected bytes would exceed three quarters of it. |+| `disk_full` | Free space on the Host volume cannot hold the download, every active reservation and the free-space floor. |+| `volume_unmeasurable` | The Host volume's `statfs` was invalid on two consecutive readings, so no budget or free space could be computed. |+| `limit_exceeded` | The declared size is not a non-negative integer up to `MCP_HOST_GFS_MAX_FILE_BYTES`. |+| `download_busy` | Too many active transfers, the store is closed or closing, or no such transfer exists. |+| `download_missing` | No copy this process published for this caller has that path, or the copy failed verification. |+| `download_expired` | The copy or the transfer is past its expiry. |+| `publication_cancelled` | The task was cancelled or ran out of time while the copy was being verified or published. |+| `storage_write_failed` | A filesystem operation failed during admission, publication or removal. |+| `workspace_unavailable` | The store is not initialized, or the Host root or caller directory is not a real directory inside the Host root. |+| `caller_mismatch` | The request carries no caller identity, one that is not its caller directory's key, or a malformed retention owner ID. |Another caller's download is answered with the same code and message as an IDthat does not exist (`download_missing` for reads and publication,@@ -613,7 +657,8 @@ refusals described in [Quotas and eviction](#quotas-and-eviction).`initialize()` fails only when the Host root is not a usable directory(`workspace_unavailable`). The runtime then logs `GFS download storeunavailable; managed GFS operations are disabled and initialization is retried-by the hourly cycle`, keeps safe RPC capabilities running, and retries hourly.+by the next cycle`, keeps safe RPC capabilities running, and retries every 5+minutes.Managed shell execution keeps the verified caller root and continues; it cannotfall back to the Host's shared workspace. A failed sweep never makes the storeunavailable.@@ -717,7 +762,7 @@ Rolling back to the dev image (`74e0d81d9`) is safe. That image creates a newthe `meta.json` directories this image left. The dev image charges only itsledger records, so those directories are outside its accounting: they are notcharged against its quota, only its free-space check sees them, and their size-is bounded by the budget in force when this image wrote them (85% of the volume+is bounded by the budget in force when this image wrote them (70% of the volumeby default) plus any partial files. They stay until a later image sweeps them. Aretired tree or a `.gfs-downloads.trash-*` tree left by a failed removal is notprotected from workspace tools under that image. Rolling forward again retiresdiff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex c6d00618c..6b11cad41 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.553",+ "version": "0.1.554","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.553",+ "version": "0.1.554","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 3d2893b68..bdccc946c 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.553",+ "version": "0.1.554","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__/fixtures/gfsStoreTestKit.ts b/mcp-host/src/__tests__/fixtures/gfsStoreTestKit.tsindex aa07269db..3e3d10df2 100644--- a/mcp-host/src/__tests__/fixtures/gfsStoreTestKit.ts+++ b/mcp-host/src/__tests__/fixtures/gfsStoreTestKit.ts@@ -109,6 +109,24 @@ export function plantDownload(return { id, directory, sourcePath }}+type BigintStatfs = (target: string, options: { bigint: true }) => Promise<fs.BigIntStatsFs>++/**+ * statfs as the test volume reports it, with the volume sized to its free+ * space (`blocks = bfree = bavail`). The store keeps a free-space floor of+ * 15% of the volume, so on a development or CI disk that is more than 85%+ * full the real size would refuse every admission as disk_full; sized to its+ * free space, the volume's other data no longer counts, and what the tests+ * write is still taken from real free space. A test whose subject is the+ * size of the volume replaces this with its own reading.+ */+export function freeSpaceSizedStatfs(real: BigintStatfs): BigintStatfs {+ return async (target, options) => {+ const reading = await real(target, options)+ return { ...reading, blocks: reading.bavail, bfree: reading.bavail }+ }+}+export async function metricValue(name: string, labels: Record<string, string>): Promise<number> {const metric = register.getSingleMetric(name)if (metric === undefined) throw new Error(`metric ${name} is not registered`)@@ -146,7 +164,11 @@ export interface TransferringStore {): Promise<{ id: string; path: string; sha256: string; expiresAt: string }>}-/** Admits a transfer and writes its partial file; `fill` sets every byte. */+/**+ * Admits a transfer and writes its partial file; `fill` sets every byte. The+ * default expiry is two hours, more than the one hour a copy must have left to+ * be reused.+ */export async function startTransfer(store: TransferringStore,root: string,@@ -161,7 +183,7 @@ export async function startTransfer(callerWorkspacePath: root,source: sourceFor(index, options.version ?? 1),sizeBytes,- expiresAt: options.expiresAt ?? new Date(Date.now() + 60 * 60_000).toISOString(),+ expiresAt: options.expiresAt ?? new Date(Date.now() + 2 * 60 * 60_000).toISOString(),...(options.owner === undefined ? {} : { retentionOwnerId: options.owner }),})fs.writeFileSync(path.join(root, transfer.partialPath), bytes)diff --git a/mcp-host/src/__tests__/gfsRuntime.test.ts b/mcp-host/src/__tests__/gfsRuntime.test.tsindex bc33ea084..473322a8e 100644--- a/mcp-host/src/__tests__/gfsRuntime.test.ts+++ b/mcp-host/src/__tests__/gfsRuntime.test.ts@@ -8,6 +8,14 @@ import { type GfsRuntime, bootstrapGfsRuntime } from '../gfsRuntime'import { GfsDownloadStore } from '../internalTools/gfsDownloadStore'import { logger } from '../logger'+// statfs is the only boundary replaced: the volume sized to its free space,+// so the disk's occupancy never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const { freeSpaceSizedStatfs } = await import('./fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+const roots: string[] = []const runtimes: GfsRuntime[] = []@@ -80,7 +88,7 @@ describe('GFS runtime bootstrap', () => {expect(runtime.store.isAvailable()).toBe(false)expect(error).toHaveBeenCalledWith({ component: 'gfs-runtime', code: 'workspace_unavailable' },- expect.stringContaining('retried by the hourly cycle')+ expect.stringContaining('retried by the next cycle'))// The cause goes away: the symlink becomes a real directory.await fs.rm(link)@@ -203,6 +211,45 @@ describe('GFS runtime bootstrap', () => {).rejects.toMatchObject({ code: 'download_busy' })})+ it('the default cycle runs cleanup every five minutes', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ const cleanup = vi.spyOn(GfsDownloadStore.prototype, 'cleanupExpired')+ const runtime = await start(await root())+ expect(runtime.store.isAvailable()).toBe(true)++ await vi.advanceTimersByTimeAsync(5 * 60_000 - 1)+ expect(cleanup).not.toHaveBeenCalled()+ // Witness: the cycle reaches cleanup at five minutes, and again five minutes later.+ await vi.advanceTimersByTimeAsync(1)+ await vi.waitFor(() => expect(cleanup).toHaveBeenCalledTimes(1))+ // The next cycle is scheduled only after this one settles.+ await cleanup.mock.results[0]!.value+ await vi.advanceTimersByTimeAsync(5 * 60_000)+ await vi.waitFor(() => expect(cleanup).toHaveBeenCalledTimes(2))+ })++ it('stop resolves when the cycle in flight never settles: it waits stopTimeoutMs, warns and closes the store', async () => {+ const runtime = await bootstrapGfsRuntime(await root(), { cycleMs: 25, stopTimeoutMs: 50 })+ runtimes.push(runtime)+ const sweepStarted = deferred()+ vi.spyOn(GfsDownloadStore.prototype, 'cleanupExpired').mockImplementationOnce(() => {+ sweepStarted.resolve()+ return new Promise(() => undefined)+ })+ await sweepStarted.promise+ const close = vi.spyOn(runtime.store, 'close')+ const warn = vi.spyOn(logger, 'warn')++ await runtime.stop()++ expect(warn).toHaveBeenCalledWith(+ { component: 'gfs-runtime', timeoutMs: 50 },+ 'GFS runtime stop timed out waiting for the cycle in flight; the store is closed anyway'+ )+ expect(close).toHaveBeenCalledWith(50)+ expect(runtime.store.isAvailable()).toBe(false)+ })+it('stop during a slow cycle initialize leaves the store unavailable', async () => {const { link } = await symlinkedHostRoot()const realInitialize = GfsDownloadStore.prototype.initializediff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.tsindex e3f0ef5f0..adc6d98dd 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsBatchReceipt.integration.test.ts@@ -22,6 +22,14 @@ import { ScopedWorkspaceProvider } from '../../workspace/scopedWorkspace'import { deriveUserKeyFromSource } from '../../workspace/userKey'import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../taskExecutor'+// statfs reports the volume sized to its free space, so the disk's occupancy+// never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const { freeSpaceSizedStatfs } = await import('../../__tests__/fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+const { clientFactory } = vi.hoisted(() => ({ clientFactory: vi.fn() }))// Double only GFSC's external transport and the model. The native registry,// store, decoder, shell, SQLite dispatcher and approval reconstruction stay real.diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.tsindex 2866992f6..011b647f1 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsLiveApproval.integration.test.ts@@ -11,6 +11,7 @@ import {makeSqliteStore,} from '../../core/conversation/persistence/__tests__/testHelpers'import { SqliteConversationStore } from '../../core/conversation/persistence/sqliteConversationStore'+import { STATELESS_CRON_APPROVAL_PROMPT } from '../../core/extensions/mcpApprovalGateController'import { SimpleEventEmitter } from '../../core/orchestration/eventEmitter'import { HttpRequestTool } from '../../core/tools/httpRequest'import { type ChatMessage, FinishReason, type ToolCall } from '../../core/types'@@ -19,11 +20,20 @@ import { TaskLifecycle } from '../../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../../llm/types'import { MessageQueue } from '../../queue'import type { Task } from '../../queue/types'+import { STATELESS_ALLOW_CRON_MANAGE_ENV } from '../../statelessCronPolicy'import { resolveCallerRootBinding } from '../../workspace/callerRootBinding'import { ScopedWorkspaceProvider } from '../../workspace/scopedWorkspace'import { CronScheduler } from '../cronScheduler'import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../taskExecutor'+// statfs reports the volume sized to its free space, so the disk's occupancy+// never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof import('node:fs/promises')>()+ const { freeSpaceSizedStatfs } = await import('../../__tests__/fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+// Observe process admission while retaining the real shell, registry, approval// controllers, conversation manager and caller-bound GFS store.vi.mock('child_process', async importOriginal => {@@ -36,7 +46,10 @@ const savedConfig = {codexToolPresentation: appConfig.codexToolPresentation,dynamicToolsEnabled: appConfig.dynamicToolsEnabled,promptCacheEnabled: appConfig.promptCacheEnabled,+ statelessLifecycle: appConfig.statelessLifecycle,+ nativeTool: appConfig.nativeTool,}+const savedAllowCronManage = process.env[STATELESS_ALLOW_CRON_MANAGE_ENV]const stores: GfsDownloadStore[] = []const roots: string[] = []const sqliteHandles: StoreHandle[] = []@@ -56,6 +69,8 @@ afterEach(async () => {for (const store of stores.splice(0)) await store.close()for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true })Object.assign(appConfig, savedConfig)+ if (savedAllowCronManage === undefined) delete process.env[STATELESS_ALLOW_CRON_MANAGE_ENV]+ else process.env[STATELESS_ALLOW_CRON_MANAGE_ENV] = savedAllowCronManagevi.unstubAllGlobals()})@@ -68,20 +83,27 @@ const failingCommand = `${firstCommand}; exit 127`// Use the verified test runtime; no assumption about Python or an ambient Node.const secondCommand = `'${process.execPath.replace(/'/g, "'\\''")}' -e 'process.stdout.write("second-approved-result")'`-function riskyFollowUp(name: 'http_request' | 'cron_manage'): ToolCall {- if (name === 'http_request')- return { id: `${name}-follow-up`, name, arguments: { url: 'https://example.test/' } }- return { id: `${name}-follow-up`, name, arguments: { action: 'list' } }+function riskyFollowUp(name: 'http_request' | 'cron_manage', id = `${name}-follow-up`): ToolCall {+ if (name === 'http_request') return { id, name, arguments: { url: 'https://example.test/' } }+ return { id, name, arguments: { action: 'list' } }+}++type SessionTool = 'shell_exec' | 'http_request' | 'cron_manage'++function sessionToolCall(name: SessionTool, id: string): ToolCall {+ return name === 'shell_exec' ? shellCall(id, firstCommand) : riskyFollowUp(name, id)}type StoreState = 'available' | 'closed' | 'uninitialized'+// A null entry ends the turn with a plain answer; later entries serve the next turn.async function scenario(- calls: ToolCall[],+ calls: (ToolCall | null)[],guardrailAsk = false,conversationManager = new ConversationManager(),storeState: StoreState = 'available',- persistentShellApproval = true+ persistentShellApproval = true,+ options: { maxToolCalls?: number; requireDownloadApproval?: boolean } = {}) {const root = await mkdtemp(join(tmpdir(), 'gfs-live-approval-'))roots.push(root)@@ -161,7 +183,11 @@ async function scenario(gfsDownloadStore: store,gfsCallerWorkspacePath: callerWorkspace,modelName: 'fixture-model',- approvalConfig: { defaultPolicy: 'channel_users', channels: {} },+ approvalConfig: {+ defaultPolicy: 'channel_users',+ channels: {},+ ...(options.requireDownloadApproval ? { tools: { clerum__gfs_download: true } } : {}),+ },guardrailsConfig: guardrailAsk? {rules: [@@ -175,7 +201,7 @@ async function scenario(: undefined,config: {maxTaskDuration: 30_000,- maxToolCallsPerTask: 10,+ maxToolCallsPerTask: options.maxToolCalls ?? 10,autoStart: true,taskDelay: 0,approvalTimeout: 30_000,@@ -204,8 +230,33 @@ async function scenario(}}-// Shell approval follows the same turn and "always" rules as every other native-// tool (as before #979). Only clerum__gfs_download keeps per-call live approval.+type Scenario = Awaited<ReturnType<typeof scenario>>++/** Runs the next user turn of the same conversation through a new task and executor. */+async function nextTurn(s: Scenario, conversationManager = s.deps.conversationManager) {+ const task: Task = {+ ...s.task,+ id: randomUUID(),+ status: 'pending',+ createdAt: new Date(),+ sourceMessage: {+ ...s.task.sourceMessage!,+ content: 'Process it again',+ messageId: `gfs-live-approval-message-${randomUUID()}`,+ },+ conversationHistory: [{ role: 'user', content: 'Process it again', timestamp: new Date() }],+ responseCallback: vi.fn(async () => undefined),+ }+ s.deps.taskLifecycle!.register(task)+ const executor = new TaskExecutor(task, { ...s.deps, conversationManager })+ expect(resolveTaskSessionKey(task)).toBe(s.sessionKey)+ await executor.run()+ return executor+}++// Shell approval is session-scoped (SESSION_SCOPED_APPROVAL_TOOLS): one approval+// covers later shell_exec calls of the conversation while it stays in memory.+// Only clerum__gfs_download keeps per-call live approval.it('runs shell without a new approval when shell_exec is persistently approved', async () => {const call = shellCall('first-shell-call', firstCommand)const s = await scenario([call])@@ -279,51 +330,170 @@ it('one shell approval covers the rest of the turn: two shell calls, one approvaexpect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)})-it.each(['http_request', 'shell_exec'] as const)(- 'approval parity: %s asks once per turn and the approval covers the next call',+// SESSION_SCOPED_APPROVAL_TOOLS: one approval of shell_exec, http_request or+// cron_manage covers that tool alone. A plain approval lasts for the current+// task; "always" lasts for later tasks while the conversation stays in memory.+async function sessionToolScenario(toolName: SessionTool, persistent: boolean) {+ // http_request resolves and pins its own socket; the tool boundary is the witness.+ const httpExecute = vi+ .spyOn(HttpRequestTool.prototype, 'execute')+ .mockResolvedValue({ content: 'follow-up body', duration_ms: 1, is_error: false })+ const first = sessionToolCall(toolName, 'session-first')+ const s = await scenario(+ [+ first,+ sessionToolCall(toolName, 'session-same-task'),+ null,+ sessionToolCall(toolName, 'session-next-task'),+ null,+ ],+ false,+ new ConversationManager(),+ 'available',+ false+ )+ const listJobs = vi.spyOn(s.deps.cronScheduler!, 'getAllJobs')+ const executions = () =>+ toolName === 'shell_exec'+ ? vi.mocked(spawn).mock.calls.length+ : toolName === 'http_request'+ ? httpExecute.mock.calls.length+ : listJobs.mock.calls.length+ await s.executor.run()++ // Witness: the first call suspended with an approval card and did not run.+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: toolName,+ tool_call_id: first.id,+ authorization_scope: 'turn_tools',+ })+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)+ expect(executions()).toBe(0)+ await s.executor.resumeAfterApproval(persistent)++ // Witness: the second call of the same task ran without a card.+ expect(s.onFail).not.toHaveBeenCalled()+ expect(s.executor.executorState).toBe('completed')+ expect(executions()).toBe(2)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)+ return { s, executions }+}++it.each(['shell_exec', 'http_request', 'cron_manage'] as const)(+ 'a plain %s approval covers its later calls in the same task and a new task asks again',async toolName => {- // http_request resolves and pins its own socket; the tool boundary is the witness.- const httpExecute = vi- .spyOn(HttpRequestTool.prototype, 'execute')- .mockResolvedValue({ content: 'follow-up body', duration_ms: 1, is_error: false })- const first: ToolCall =- toolName === 'shell_exec'- ? shellCall('parity-first', firstCommand)- : { ...riskyFollowUp('http_request'), id: 'parity-first' }- const s = await scenario(- [first, riskyFollowUp('http_request')],- false,- new ConversationManager(),- 'available',- false- )- await s.executor.run()+ const { s, executions } = await sessionToolScenario(toolName, false)+ expect(s.conversation.task_approved_tools).toEqual(new Set([toolName]))+ expect(s.conversation.auto_approved_tools).toEqual(new Set())- expect(s.executor.pendingApproval).toMatchObject({+ const next = await nextTurn(s)++ expect(s.onFail).not.toHaveBeenCalled()+ expect(next.executorState).toBe('waiting_approval')+ expect(next.pendingApproval).toMatchObject({tool_name: toolName,+ tool_call_id: 'session-next-task',authorization_scope: 'turn_tools',})- await s.executor.resumeAfterApproval(false)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)+ expect(executions()).toBe(2)+ }+)++it.each(['shell_exec', 'http_request', 'cron_manage'] as const)(+ 'an "always" %s approval covers its calls in later tasks',+ async toolName => {+ const { s, executions } = await sessionToolScenario(toolName, true)+ expect(s.conversation.auto_approved_tools).toEqual(new Set([toolName]))++ const next = await nextTurn(s)expect(s.onFail).not.toHaveBeenCalled()- // Witness: the follow-up HTTP call ran without a new approval.- expect(httpExecute).toHaveBeenCalledTimes(toolName === 'http_request' ? 2 : 1)- expect(spawn).toHaveBeenCalledTimes(toolName === 'shell_exec' ? 1 : 0)- expect(s.executor.executorState).toBe('completed')+ expect(next.executorState).toBe('completed')+ // Witness: the next task's call executed, still with the single card.+ expect(executions()).toBe(3)expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)+ expect(s.providerCalls).toHaveLength(5)})-// Accepted risk: a turn-wide shell_exec approval, like any turn-wide approval,-// adds '*' and so auto-approves every approval-gated tool for the rest of the turn.-it('one turn-wide shell approval auto-approves cron_manage and http_request for the rest of the turn', async () => {+it('a plain shell_exec approval covers 31 later shell calls of the same task', async () => {+ const later = Array.from({ length: 31 }, (_, index) =>+ shellCall(`many-shell-${index}`, firstCommand)+ )+ const s = await scenario(+ [shellCall('many-shell-first', firstCommand), ...later],+ false,+ new ConversationManager(),+ 'available',+ false,+ { maxToolCalls: 40 }+ )+ await s.executor.run()+ expect(s.executor.pendingApproval).toMatchObject({ tool_name: 'shell_exec' })+ expect(spawn).not.toHaveBeenCalled()+ await s.executor.resumeAfterApproval(false)++ expect(s.onFail).not.toHaveBeenCalled()+ expect(s.executor.executorState).toBe('completed')+ // Witness: all 32 shell calls ran after the single card.+ expect(spawn).toHaveBeenCalledTimes(32)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)+})++it('a plain shell_exec approval still covers shell after a clerum__gfs_download card in the same task', async () => {+ const download: ToolCall = {+ id: 'live-download',+ name: 'clerum__gfs_download',+ arguments: { drive: 'main', resourceId: 'governed-resource', expectedVersion: 7 },+ }+ const s = await scenario(+ [+ shellCall('before-download', firstCommand),+ download,+ shellCall('after-download', secondCommand),+ ],+ false,+ new ConversationManager(),+ 'available',+ false,+ { requireDownloadApproval: true }+ )+ await s.executor.run()+ expect(s.executor.pendingApproval).toMatchObject({ tool_name: 'shell_exec' })+ await s.executor.resumeAfterApproval(false)++ // Witness: the live download card interrupted the task after the first shell call.+ expect(spawn).toHaveBeenCalledTimes(1)+ expect(s.executor.executorState).toBe('waiting_approval')+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'clerum__gfs_download',+ tool_call_id: download.id,+ authorization_scope: 'exact_invocation',+ })+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)+ await s.executor.resumeAfterApproval(false)++ expect(s.onFail).not.toHaveBeenCalled()+ expect(s.executor.executorState).toBe('completed')+ expect(vi.mocked(spawn).mock.calls.map(call => call[1])).toEqual([+ ['-c', firstCommand],+ ['-c', secondCommand],+ ])+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)+})++it('after a shell_exec approval, cron_manage and http_request still suspend for their own approval', async () => {// http_request resolves and pins its own socket; the tool boundary is the witness.const httpExecute = vi.spyOn(HttpRequestTool.prototype, 'execute').mockResolvedValue({ content: 'follow-up body', duration_ms: 1, is_error: false })- const shell = shellCall('wildcard-shell', firstCommand)+ const shell = shellCall('session-shell', firstCommand)+ const sameTurnShell = shellCall('session-shell-again', secondCommand)+ const cron = riskyFollowUp('cron_manage')+ const http = riskyFollowUp('http_request')const s = await scenario(- [shell, riskyFollowUp('cron_manage'), riskyFollowUp('http_request')],+ [shell, sameTurnShell, cron, http],false,new ConversationManager(),'available',@@ -331,25 +501,195 @@ it('one turn-wide shell approval auto-approves cron_manage and http_request for)const listJobs = vi.spyOn(s.deps.cronScheduler!, 'getAllJobs')await s.executor.run()-expect(s.executor.pendingApproval).toMatchObject({tool_name: 'shell_exec',tool_call_id: shell.id,- authorization_scope: 'turn_tools',})- expect(spawn).not.toHaveBeenCalled()await s.executor.resumeAfterApproval(false)- expect(s.onFail).not.toHaveBeenCalled()- // Witness: each of the three tools ran once, after a single approval card.- expect(vi.mocked(spawn).mock.calls.map(call => call.slice(0, 2))).toEqual([- ['/bin/sh', ['-c', firstCommand]],+ // Witness: both shell calls of this turn ran under the one shell approval.+ expect(vi.mocked(spawn).mock.calls.map(call => call[1])).toEqual([+ ['-c', firstCommand],+ ['-c', secondCommand],])+ expect(s.executor.executorState).toBe('waiting_approval')+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'cron_manage',+ tool_call_id: cron.id,+ authorization_scope: 'turn_tools',+ })+ expect(listJobs).not.toHaveBeenCalled()+ expect(s.conversation.task_approved_tools).toEqual(new Set(['shell_exec']))+ expect(s.conversation.auto_approved_tools).toEqual(new Set())+ await s.executor.resumeAfterApproval(false)+expect(listJobs).toHaveBeenCalledOnce()+ expect(s.executor.executorState).toBe('waiting_approval')+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'http_request',+ tool_call_id: http.id,+ authorization_scope: 'turn_tools',+ })+ expect(httpExecute).not.toHaveBeenCalled()+ await s.executor.resumeAfterApproval(false)++ expect(s.onFail).not.toHaveBeenCalled()expect(httpExecute).toHaveBeenCalledOnce()- expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)expect(s.executor.executorState).toBe('completed')- expect(s.conversation.auto_approved_tools.has('*')).toBe(true)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(3)+ expect(s.conversation.task_approved_tools).toEqual(+ new Set(['shell_exec', 'cron_manage', 'http_request'])+ )+ expect(s.conversation.auto_approved_tools).toEqual(new Set())+})++it('on a stateless host every cron_manage create asks, and an approved create card runs only that call', async () => {+ // Stateless host with cron management explicitly allowed: create/enable are forced approvals.+ Object.assign(appConfig, {+ statelessLifecycle: true,+ nativeTool: { ...appConfig.nativeTool, statelessLifecycle: true },+ })+ process.env[STATELESS_ALLOW_CRON_MANAGE_ENV] = 'true'+ const createCall = (id: string, name: string): ToolCall => ({+ id,+ name: 'cron_manage',+ arguments: { action: 'create', name, schedule: '0 0 1 1 *', task: 'yearly report' },+ })+ const s = await scenario(+ [+ riskyFollowUp('cron_manage', 'stateless-list'),+ riskyFollowUp('cron_manage', 'stateless-list-again'),+ createCall('stateless-create', 'first-schedule'),+ createCall('stateless-create-again', 'second-schedule'),+ ],+ false,+ new ConversationManager(),+ 'available',+ false+ )+ const scheduler = s.deps.cronScheduler!+ const listJobs = vi.spyOn(scheduler, 'getAllJobs')+ await s.executor.run()+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'cron_manage',+ tool_call_id: 'stateless-list',+ authorization_scope: 'turn_tools',+ })+ await s.executor.resumeAfterApproval(false)++ // Witness: both list calls ran under the plain list approval, then the create suspended.+ expect(listJobs).toHaveBeenCalledTimes(2)+ expect(s.conversation.task_approved_tools).toEqual(new Set(['cron_manage']))+ expect(s.executor.executorState).toBe('waiting_approval')+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'cron_manage',+ tool_call_id: 'stateless-create',+ authorization_scope: 'exact_invocation',+ description: STATELESS_CRON_APPROVAL_PROMPT,+ })+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)+ expect(scheduler.getAllJobs()).toEqual([])+ await s.executor.resumeAfterApproval(true)++ // Witness: the approved create ran; the next create asked again.+ expect(scheduler.getAllJobs().map(job => job.name)).toEqual(['first-schedule'])+ expect(s.executor.executorState).toBe('waiting_approval')+ expect(s.executor.pendingApproval).toMatchObject({+ tool_name: 'cron_manage',+ tool_call_id: 'stateless-create-again',+ authorization_scope: 'exact_invocation',+ })+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(3)+ // The exact create approval, even with "always", stored nothing new.+ expect(s.conversation.task_approved_tools).toEqual(new Set(['cron_manage']))+ expect(s.conversation.auto_approved_tools).toEqual(new Set())+ expect(s.onFail).not.toHaveBeenCalled()+})++it('asks again for a plain shell_exec approval when a cold resume rebuilds the conversation mid-task', async () => {+ const handle = makeSqliteStore()+ sqliteHandles.push(handle)+ const s = await scenario(+ [+ shellCall('cold-task-first', firstCommand),+ riskyFollowUp('cron_manage', 'cold-task-cron'),+ shellCall('cold-task-after-resume', secondCommand),+ ],+ false,+ new ConversationManager(handle.store),+ 'available',+ false+ )+ const listJobs = vi.spyOn(s.deps.cronScheduler!, 'getAllJobs')+ await s.executor.run()+ await s.executor.resumeAfterApproval(false)++ // Witness: the approved shell_exec ran, then the task suspended on cron_manage.+ expect(spawn).toHaveBeenCalledTimes(1)+ expect(s.executor.pendingApproval).toMatchObject({ tool_name: 'cron_manage' })+ expect(s.conversation.task_approved_tools).toEqual(new Set(['shell_exec']))++ const coldManager = new ConversationManager(+ new SqliteConversationStore(handle.persistQueue, { cacheSize: 8 })+ )+ const coldConversation = await coldManager.getOrCreate(s.sessionKey)+ expect(coldConversation).not.toBe(s.conversation)+ expect(coldConversation.task_approved_tools).toBeUndefined()+ const cold = new TaskExecutor(s.task, { ...s.deps, conversationManager: coldManager })+ await cold.rehydrateWaitingApproval(s.sessionKey, coldConversation.pending_approval!)+ await cold.resumeAfterApproval(false)++ expect(s.onFail).not.toHaveBeenCalled()+ // Witness: the rehydrated task resumed and ran the approved cron_manage call.+ expect(listJobs).toHaveBeenCalledOnce()+ expect(cold.executorState).toBe('waiting_approval')+ expect(cold.pendingApproval).toMatchObject({+ tool_name: 'shell_exec',+ tool_call_id: 'cold-task-after-resume',+ })+ expect(spawn).toHaveBeenCalledTimes(1)+})++it('asks again for an "always" shell_exec approval after a cold resume rebuilds the conversation', async () => {+ const handle = makeSqliteStore()+ sqliteHandles.push(handle)+ const s = await scenario(+ [+ shellCall('cold-first', firstCommand),+ shellCall('cold-same-turn', secondCommand),+ null,+ shellCall('cold-next-turn', firstCommand),+ ],+ false,+ new ConversationManager(handle.store),+ 'available',+ false+ )+ await s.executor.run()+ await s.executor.resumeAfterApproval(true)++ // Witness: before the cold resume, the approved shell_exec proceeded without a card.+ expect(s.executor.executorState).toBe('completed')+ expect(spawn).toHaveBeenCalledTimes(2)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(1)+ expect(s.conversation.auto_approved_tools).toEqual(new Set(['shell_exec']))++ const coldManager = new ConversationManager(+ new SqliteConversationStore(handle.persistQueue, { cacheSize: 8 })+ )+ const coldConversation = await coldManager.getOrCreate(s.sessionKey)+ expect(coldConversation).not.toBe(s.conversation)+ expect(coldConversation.auto_approved_tools.size).toBe(0)+ const cold = await nextTurn(s, coldManager)++ expect(s.onFail).not.toHaveBeenCalled()+ expect(cold.executorState).toBe('waiting_approval')+ expect(cold.pendingApproval).toMatchObject({+ tool_name: 'shell_exec',+ tool_call_id: 'cold-next-turn',+ })+ expect(spawn).toHaveBeenCalledTimes(2)+ expect(s.onApprovalNeeded).toHaveBeenCalledTimes(2)})it.each(['http_request', 'cron_manage'] as const)(@@ -438,7 +778,7 @@ const resumeCases = [{ firstExit: 0, repeatedCommand: true, alwaysApprove: false, rehydrate: true },{ firstExit: 127, repeatedCommand: false, alwaysApprove: false, rehydrate: true },// A guardrail `action: 'ask'` rule is what keeps shell approval per call now- // that shell_exec follows the turn and "always" rules of every native tool.+ // that one shell_exec approval covers the tool for the rest of the task.].map(c => ({ ...c, guardrailAsk: true }))it.each(resumeCases)(diff --git a/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.tsindex 73869a5e1..be08641c2 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.gfsPreparation.test.ts@@ -24,6 +24,14 @@ import { TaskExecutor, type TaskExecutorDeps, resolveTaskSessionKey } from '../t/** The store keys the caller like its root: the channel-namespaced key, never the raw sender. */const UNIT_CALLER_STORE_KEY = deriveUserKey('unit-caller', 'rpc')+// statfs reports the volume sized to its free space, so the disk's occupancy+// never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const { freeSpaceSizedStatfs } = await import('../../__tests__/fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+const { clientFactory } = vi.hoisted(() => ({ clientFactory: vi.fn() }))// Only GFSC's external HTTP/token-file boundary is doubled. The real client,// store, registry, approval gate, TaskExecutor and shell all execute.diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.tsindex 0e5e52fdf..2282efd8e 100644--- a/mcp-host/src/agent/taskExecutor.ts+++ b/mcp-host/src/agent/taskExecutor.ts@@ -2329,7 +2329,7 @@ export class TaskExecutor {const cronManageGateApplies =appConfig.enableApproval && this.task.source === 'cron' && appConfig.statelessLifecycleconst approvalApplies = interactiveApprovalApplies || cronManageGateApplies- const baseController = approvalApplies+ const approvalGate = approvalApplies? new UnifiedApprovalGateController(compositeRegistry,this.deps.approvalConfig,@@ -2343,19 +2343,22 @@ export class TaskExecutor {cronManageGateOnly: cronManageGateApplies,})- : new DefaultLoopController()- const innerController = approvalApplies+ : null+ const innerController = approvalGate? new ApprovalController(this.conversation!,- baseController,+ approvalGate,gfsWorkspace &&appConfig.enableApproval &&this.task.source === 'channel' &&this.deps.approvalConfig?.tools?.shell_exec !== false? new Set(['clerum__gfs_download'])- : undefined+ : undefined,+ // The same gate's forced approvals (stateless cron create/enable) run+ // before any stored approval, so none of them can cover such a call.+ approvalGate)- : baseController+ : new DefaultLoopController()const mcpManager = this.deps.mcpManager// Exact native membership preserves every native/plugin capability.diff --git a/mcp-host/src/core/conversation/__tests__/authorizationScope.test.ts b/mcp-host/src/core/conversation/__tests__/authorizationScope.test.tsindex fda83dc6d..1e1dfd76d 100644--- a/mcp-host/src/core/conversation/__tests__/authorizationScope.test.ts+++ b/mcp-host/src/core/conversation/__tests__/authorizationScope.test.ts@@ -2,11 +2,14 @@ import { describe, expect, it } from 'vitest'import { ConversationState, type PendingApproval } from '../../types'import { ConversationManager } from '../conversation'-function approval(scope?: PendingApproval['authorization_scope']): PendingApproval {+function approval(+ scope?: PendingApproval['authorization_scope'],+ toolName = 'shell_exec'+): PendingApproval {return {request_id: 'approval-scope-test',authorization_scope: scope,- tool_name: 'shell_exec',+ tool_name: toolName,parameters: { command: 'printf exact-call' },description: 'Unit approval scope',tool_call_id: 'tool-scope-test',@@ -14,11 +17,15 @@ function approval(scope?: PendingApproval['authorization_scope']): PendingApprov}}-async function approvedScope(scope?: PendingApproval['authorization_scope'], always = false) {+async function approvedScope(+ scope?: PendingApproval['authorization_scope'],+ always = false,+ toolName = 'shell_exec'+) {const manager = new ConversationManager()const conversation = await manager.getOrCreate('scope-user:rpc:scope-agent:chat')conversation.state = ConversationState.Processing- await manager.suspendForApproval(conversation, approval(scope))+ await manager.suspendForApproval(conversation, approval(scope, toolName))await manager.approve(conversation, always)return conversation.auto_approved_tools}@@ -29,7 +36,13 @@ describe('approval authorization scope', () => {})it('preserves explicitly consented turn-wide behavior for ordinary new approvals', async () => {- await expect(approvedScope('turn_tools', true)).resolves.toEqual(new Set(['*', 'shell_exec']))+ await expect(approvedScope('turn_tools', true, 'workflow_trigger')).resolves.toEqual(+ new Set(['*', 'workflow_trigger'])+ )+ })++ it('scopes turn-wide consent for shell_exec to the tool itself, without a wildcard', async () => {+ await expect(approvedScope('turn_tools', true)).resolves.toEqual(new Set(['shell_exec']))})it('treats a legacy unknown scope as exact rather than granting a wildcard', async () => {diff --git a/mcp-host/src/core/conversation/__tests__/conversation.test.ts b/mcp-host/src/core/conversation/__tests__/conversation.test.tsindex c03cd8b74..e04a4708e 100644--- a/mcp-host/src/core/conversation/__tests__/conversation.test.ts+++ b/mcp-host/src/core/conversation/__tests__/conversation.test.ts@@ -285,15 +285,16 @@ describe('ConversationManager — approval transitions', () => {it('should NOT add server prefix for non-MCP tools (no __ separator)', async () => {const conv = await manager.getOrCreate('user-3')- await manager.startTurn(conv, 'Run shell', 'test-task')+ await manager.startTurn(conv, 'Trigger a workflow', 'test-task')+ // workflow_trigger is a non-MCP tool outside SESSION_SCOPED_APPROVAL_TOOLS.await manager.suspendForApproval(conv,ordinaryApproval({request_id: 'req-3',- tool_name: 'shell_exec',- parameters: { command: 'ls' },- description: 'Shell command',+ tool_name: 'workflow_trigger',+ parameters: { recipe: 'report' },+ description: 'Workflow trigger',tool_call_id: 'tc_3',context_snapshot: [],})@@ -302,7 +303,8 @@ describe('ConversationManager — approval transitions', () => {// Non-MCP tool: approve adds wildcard "*" for "approve once, run all" within this turnawait manager.approve(conv, false)expect(conv.auto_approved_tools.has('*')).toBe(true)- expect(conv.auto_approved_tools.has('shell_exec')).toBe(false) // individual tool NOT added without alwaysApprove+ expect(conv.auto_approved_tools.has('workflow_trigger')).toBe(false) // individual tool NOT added without alwaysApprove+ expect(conv.auto_approved_tools).toEqual(new Set(['*'])) // no server prefix either})it('should transition AwaitingApproval → Idle on deny, clearing pending approval', async () => {@@ -397,9 +399,9 @@ describe('ConversationManager — approve-once wildcard lifecycle', () => {conv,ordinaryApproval({request_id: 'req-wc-1',- tool_name: 'shell_exec',- parameters: { command: 'echo hi' },- description: 'Shell',+ tool_name: 'workflow_trigger',+ parameters: { recipe: 'report' },+ description: 'Workflow trigger',tool_call_id: 'tc_wc_1',context_snapshot: [],})@@ -418,9 +420,9 @@ describe('ConversationManager — approve-once wildcard lifecycle', () => {conv,ordinaryApproval({request_id: 'req-wc-2',- tool_name: 'shell_exec',+ tool_name: 'workflow_trigger',parameters: {},- description: 'Shell',+ description: 'Workflow trigger',tool_call_id: 'tc_wc_2',context_snapshot: [],})@@ -468,6 +470,76 @@ describe('ConversationManager — approve-once wildcard lifecycle', () => {})})+describe('ConversationManager — session-scoped tool approvals', () => {+ let manager: ConversationManager++ beforeEach(() => {+ manager = new ConversationManager()+ })++ function sessionToolApproval(toolName: string, requestId: string): PendingApproval {+ return ordinaryApproval({+ request_id: requestId,+ tool_name: toolName,+ parameters: {},+ description: toolName,+ tool_call_id: `tc_${requestId}`,+ context_snapshot: [],+ })+ }++ it.each(['shell_exec', 'http_request', 'cron_manage'] as const)(+ 'a plain approve() of %s stores the tool for the current task only, without a wildcard',+ async toolName => {+ const conv = await manager.getOrCreate(`user-session-plain-${toolName}`)+ await manager.startTurn(conv, 'Run a session tool', 'test-task')+ await manager.suspendForApproval(conv, sessionToolApproval(toolName, `req-${toolName}`))++ await manager.approve(conv, false)++ expect(conv.state).toBe(ConversationState.Processing)+ expect(conv.task_approved_tools).toEqual(new Set([toolName]))+ expect(conv.auto_approved_tools).toEqual(new Set())+ }+ )++ it.each(['shell_exec', 'http_request', 'cron_manage'] as const)(+ 'an "always" approve() of %s stores the tool for later tasks, without a wildcard',+ async toolName => {+ const conv = await manager.getOrCreate(`user-session-always-${toolName}`)+ await manager.startTurn(conv, 'Run a session tool', 'test-task')+ await manager.suspendForApproval(conv, sessionToolApproval(toolName, `req-${toolName}`))++ await manager.approve(conv, true)++ expect(conv.state).toBe(ConversationState.Processing)+ expect(conv.auto_approved_tools).toEqual(new Set([toolName]))+ expect(conv.task_approved_tools).toBeUndefined()+ }+ )++ it('startTurn clears plain session-scoped approvals and keeps "always" ones', async () => {+ const conv = await manager.getOrCreate('user-session-turns')+ await manager.startTurn(conv, 'First message', 'test-task')+ await manager.suspendForApproval(conv, sessionToolApproval('mongodb-server__find', 'req-m'))+ await manager.approve(conv, false)+ await manager.suspendForApproval(conv, sessionToolApproval('shell_exec', 'req-s'))+ await manager.approve(conv, false)+ await manager.suspendForApproval(conv, sessionToolApproval('http_request', 'req-h'))+ await manager.approve(conv, true)++ // The earlier MCP '*' is neither revoked nor duplicated by the session approvals.+ expect(conv.auto_approved_tools).toEqual(new Set(['*', 'mongodb-server', 'http_request']))+ expect(conv.task_approved_tools).toEqual(new Set(['shell_exec']))++ await manager.completeTurn(conv, 'Done')+ await manager.startTurn(conv, 'Second message', 'test-task')++ expect(conv.auto_approved_tools).toEqual(new Set(['mongodb-server', 'http_request']))+ expect(conv.task_approved_tools).toEqual(new Set())+ })+})+describe('cancelTurn (BUG-9)', () => {it('prevents cancelled user_input from leaking into next LLM call', async () => {const cm = new ConversationManager()diff --git a/mcp-host/src/core/conversation/conversation.ts b/mcp-host/src/core/conversation/conversation.tsindex 5c1bf3028..5c9ec5755 100644--- a/mcp-host/src/core/conversation/conversation.ts+++ b/mcp-host/src/core/conversation/conversation.ts@@ -2,6 +2,7 @@ import type { ModelSelectionWriteOutcome } from '../../db/worker/protocol'import { parseSessionKey } from '../../session/types'import { projectGfsApproval } from '../../visualInput/suspension'import { ConversationError, ConversationErrorCode } from '../errors'+import { SESSION_SCOPED_APPROVAL_TOOLS } from '../extensions/approvalController'import { isMcpToolName } from '../extensions/mcpApprovalGateController'import {ChatMessage,@@ -269,6 +270,8 @@ export class ConversationManager {// Clear per-turn wildcard approval — each new message requires fresh approvalconversation.auto_approved_tools.delete('*')+ // A new user message starts a new task: plain session-scoped approvals end here.+ conversation.task_approved_tools?.clear()// Materialize the auto-title (spec 15) in RAM before persisting so the// dual-store projection and the durable COALESCE write agree. `??=` keeps@@ -546,6 +549,10 @@ export class ConversationManager {* plus the MCP server prefix for future turns. Exact-call and legacy unknown* scopes authorize only the frozen invocation being resumed.*+ * SESSION_SCOPED_APPROVAL_TOOLS are the exception: turn-wide consent for one+ * of them stores only that tool name, never '*' — in `task_approved_tools`+ * for the current task, or in `auto_approved_tools` with alwaysApprove.+ ** When alwaysApprove=true, also stores the individual tool name (backwards compat).** **IronClaw write-through**: awaits durable approval-state mutation.@@ -563,20 +570,28 @@ export class ConversationManager {const toolName = conversation.pending_approval.tool_nameconst turnWide = conversation.pending_approval.authorization_scope === 'turn_tools'- // Only proven turn-wide consent expands. Exact-call and legacy unknown- // scopes authorize just the frozen invocation being resumed.- if (turnWide) conversation.auto_approved_tools.add('*')- else conversation.auto_approved_tools.delete('*')-- // MCP tools: also auto-approve the entire server for future turns- if (turnWide && isMcpToolName(toolName)) {- const serverPrefix = toolName.split('__')[0]- conversation.auto_approved_tools.add(serverPrefix)- }-- // alwaysApprove also stores the individual tool name (for future turns)- if (turnWide && alwaysApprove) {- conversation.auto_approved_tools.add(toolName)+ if (turnWide && SESSION_SCOPED_APPROVAL_TOOLS.has(toolName)) {+ // Session-scoped consent covers this tool alone and neither grants nor+ // revokes '*'. A plain approval lasts for the current task; "always"+ // lasts for later tasks while the conversation is in memory.+ if (alwaysApprove) conversation.auto_approved_tools.add(toolName)+ else (conversation.task_approved_tools ??= new Set()).add(toolName)+ } else {+ // Only proven turn-wide consent expands. Exact-call and legacy unknown+ // scopes authorize just the frozen invocation being resumed.+ if (turnWide) conversation.auto_approved_tools.add('*')+ else conversation.auto_approved_tools.delete('*')++ // MCP tools: also auto-approve the entire server for future turns+ if (turnWide && isMcpToolName(toolName)) {+ const serverPrefix = toolName.split('__')[0]+ conversation.auto_approved_tools.add(serverPrefix)+ }++ // alwaysApprove also stores the individual tool name (for future turns)+ if (turnWide && alwaysApprove) {+ conversation.auto_approved_tools.add(toolName)+ }}}diff --git a/mcp-host/src/core/extensions/__tests__/approvalController.test.ts b/mcp-host/src/core/extensions/__tests__/approvalController.test.tsindex fb4bc0ce3..6c4df2e62 100644--- a/mcp-host/src/core/extensions/__tests__/approvalController.test.ts+++ b/mcp-host/src/core/extensions/__tests__/approvalController.test.ts@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'import { DefaultLoopController, buildLoopConfig } from '../../orchestration/loopConfig'import { ConversationState } from '../../types'import type { Conversation, PendingApproval } from '../../types'-import { ApprovalController } from '../approvalController'+import { ApprovalController, SESSION_SCOPED_APPROVAL_TOOLS } from '../approvalController'function makeConversation(overrides?: Partial<Conversation>): Conversation {return {@@ -277,7 +277,7 @@ describe('ApprovalController', () => {expect(result2).toBe('proceed') // DefaultLoopController returns "proceed"})- it("should bypass ALL tools when wildcard '*' is in auto_approved_tools", () => {+ it("should bypass every tool outside the session-scoped set when wildcard '*' is in auto_approved_tools", () => {const conv = makeConversation({auto_approved_tools: new Set(['*']),})@@ -305,16 +305,63 @@ describe('ApprovalController', () => {const controller = new ApprovalController(conv, customDelegate)- // Wildcard should bypass delegate for any tool- expect(controller.beforeTool('shell_exec', { command: 'rm -rf /' })).toBe('proceed')+ // Wildcard bypasses the delegate for MCP tools and other gated nativesexpect(controller.beforeTool('mongodb-server__drop_database', {})).toBe('proceed')- expect(controller.beforeTool('http_request', { url: 'https://evil.com' })).toBe('proceed')+ expect(controller.beforeTool('workflow_trigger', { recipe: 'report' })).toBe('proceed')expect(controller.beforeTool('airtable-server__delete_all', {})).toBe('proceed')- // Delegate should NEVER have been called — wildcard bypasses everything+ // Delegate should NEVER have been called for those toolsexpect(customDelegate.beforeTool).not.toHaveBeenCalled()})+ it("does not let wildcard '*' cover shell_exec, http_request or cron_manage", () => {+ const conv = makeConversation({+ auto_approved_tools: new Set(['*']),+ })+ const suspendFor = (toolName: string) => ({+ type: 'suspend' as const,+ approval: {+ request_id: `req-${toolName}`,+ tool_name: toolName,+ parameters: {},+ description: toolName,+ tool_call_id: `tc-${toolName}`,+ context_snapshot: [],+ },+ })+ const customDelegate = {+ ...new DefaultLoopController(),+ beforeTool: vi.fn((toolName: string) => suspendFor(toolName)),+ shouldAccept: delegate.shouldAccept.bind(delegate),+ onTextRejected: delegate.onTextRejected.bind(delegate),+ onExhaustion: delegate.onExhaustion.bind(delegate),+ refreshTools: delegate.refreshTools.bind(delegate),+ }+ const controller = new ApprovalController(conv, customDelegate)++ // Witness: the wildcard is live and covers an MCP tool of the same turn.+ expect(controller.beforeTool('mongodb-server__find', {})).toBe('proceed')+ expect(customDelegate.beforeTool).not.toHaveBeenCalled()++ for (const toolName of SESSION_SCOPED_APPROVAL_TOOLS) {+ expect(controller.beforeTool(toolName, {})).toEqual(suspendFor(toolName))+ }+ expect(customDelegate.beforeTool.mock.calls.map(call => call[0])).toEqual([+ 'shell_exec',+ 'http_request',+ 'cron_manage',+ ])++ // Each session-scoped tool proceeds once its own name is approved, either+ // for later tasks ("always") or for the current task.+ conv.auto_approved_tools.add('shell_exec')+ conv.task_approved_tools = new Set(['cron_manage'])+ expect(controller.beforeTool('shell_exec', {})).toBe('proceed')+ expect(controller.beforeTool('cron_manage', {})).toBe('proceed')+ expect(controller.beforeTool('http_request', {})).toEqual(suspendFor('http_request'))+ expect(customDelegate.beforeTool).toHaveBeenCalledTimes(4)+ })+it('should NOT bypass tools when wildcard is absent and individual tool is not approved', () => {const conv = makeConversation({auto_approved_tools: new Set(), // empty — no wildcard, no approvalsdiff --git a/mcp-host/src/core/extensions/__tests__/sessionScopedApproval.test.ts b/mcp-host/src/core/extensions/__tests__/sessionScopedApproval.test.tsnew file mode 100644index 000000000..8400e3f36--- /dev/null+++ b/mcp-host/src/core/extensions/__tests__/sessionScopedApproval.test.ts@@ -0,0 +1,118 @@+import { describe, expect, it } from 'vitest'+import { ConversationManager } from '../../conversation/conversation'+import type { ToolRegistry } from '../../interfaces'+import { ApprovalController } from '../approvalController'+import { UnifiedApprovalGateController } from '../mcpApprovalGateController'++// The real approval decision and the real approve() expansion, joined through one+// conversation. The empty registry plus the per-tool overrides make every native+// tool here approval-gated; MCP tools are gated by their name.+const registry: ToolRegistry = { get: () => null, register: () => {}, listDefinitions: () => [] }+const gate = new UnifiedApprovalGateController(registry, {+ defaultPolicy: 'channel_users',+ channels: {},+ tools: { shell_exec: true, http_request: true, cron_manage: true, workflow_trigger: true },+})++async function harness(sessionKey: string) {+ const manager = new ConversationManager()+ const conversation = await manager.getOrCreate(sessionKey)+ const controller = new ApprovalController(conversation, gate)+ await manager.startTurn(conversation, 'First message', 'test-task')+ let callCount = 0++ /** One model-generated call: 'proceed', or 'suspend' after recording the card. */+ async function call(toolName: string): Promise<'proceed' | 'suspend'> {+ const decision = controller.beforeTool(toolName, {})+ if (decision === 'skip') throw new Error(`Unexpected skip for ${toolName}`)+ if (decision === 'proceed') return 'proceed'+ callCount += 1+ await manager.suspendForApproval(conversation, {+ ...decision.approval,+ tool_call_id: `tc-${callCount}`,+ })+ return 'suspend'+ }++ /** Approve the open card and consume it, as TaskExecutor.resumeAfterApproval does. */+ async function approve(always = false) {+ expect(conversation.pending_approval?.authorization_scope).toBe('turn_tools')+ await manager.approve(conversation, always)+ conversation.pending_approval = undefined+ }++ async function nextTurn() {+ await manager.completeTurn(conversation, 'Done')+ await manager.startTurn(conversation, 'Next message', 'test-task')+ }++ return { conversation, call, approve, nextTurn }+}++describe('session-scoped approvals next to turn-wide MCP approvals', () => {+ it("an MCP turn-wide approval adds '*' that does not cover shell_exec", async () => {+ const h = await harness('scope-user:rpc:scope-agent:mcp-wildcard')+ expect(await h.call('mongodb-server__find')).toBe('suspend')+ await h.approve()+ expect(h.conversation.auto_approved_tools).toEqual(new Set(['*', 'mongodb-server']))++ // Witness: '*' covers another MCP server's tool in the same turn.+ expect(await h.call('airtable-server__list_tables')).toBe('proceed')+ expect(await h.call('shell_exec')).toBe('suspend')+ expect(h.conversation.pending_approval?.tool_name).toBe('shell_exec')+ })++ it("keeps MCP behaviour: '*' covers other MCP tools in the turn and the server prefix persists", async () => {+ const h = await harness('scope-user:rpc:scope-agent:mcp-prefix')+ expect(await h.call('mongodb-server__find')).toBe('suspend')+ await h.approve()+ expect(await h.call('airtable-server__list_tables')).toBe('proceed')+ expect(await h.call('mongodb-server__insert_many')).toBe('proceed')++ await h.nextTurn()++ expect(h.conversation.auto_approved_tools).toEqual(new Set(['mongodb-server']))+ // Witness: the persisted prefix still covers its server in the next turn.+ expect(await h.call('mongodb-server__aggregate')).toBe('proceed')+ expect(await h.call('airtable-server__list_tables')).toBe('suspend')+ })++ it("shell_exec approval after an MCP '*' covers shell_exec without revoking '*'", async () => {+ const h = await harness('scope-user:rpc:scope-agent:mcp-then-shell')+ expect(await h.call('mongodb-server__find')).toBe('suspend')+ await h.approve()+ expect(await h.call('shell_exec')).toBe('suspend')+ await h.approve()++ expect(await h.call('shell_exec')).toBe('proceed')+ expect(await h.call('airtable-server__list_tables')).toBe('proceed')+ expect(await h.call('http_request')).toBe('suspend')+ })++ it("keeps workflow_trigger turn-wide: '*' covers other gated tools this turn, and a new turn asks again", async () => {+ const h = await harness('scope-user:rpc:scope-agent:workflow')+ expect(await h.call('workflow_trigger')).toBe('suspend')+ await h.approve()+ expect(h.conversation.auto_approved_tools).toEqual(new Set(['*']))++ // Witness: '*' from workflow_trigger covers the next workflow_trigger and an MCP tool.+ expect(await h.call('workflow_trigger')).toBe('proceed')+ expect(await h.call('mongodb-server__find')).toBe('proceed')++ await h.nextTurn()+ expect(await h.call('workflow_trigger')).toBe('suspend')+ })++ it('keeps workflow_trigger "always" approval across turns through its tool name', async () => {+ const h = await harness('scope-user:rpc:scope-agent:workflow-always')+ expect(await h.call('workflow_trigger')).toBe('suspend')+ await h.approve(true)+ expect(h.conversation.auto_approved_tools).toEqual(new Set(['*', 'workflow_trigger']))++ await h.nextTurn()++ expect(h.conversation.auto_approved_tools).toEqual(new Set(['workflow_trigger']))+ expect(await h.call('workflow_trigger')).toBe('proceed')+ expect(await h.call('mongodb-server__find')).toBe('suspend')+ })+})diff --git a/mcp-host/src/core/extensions/__tests__/statelessCronForcedApproval.test.ts b/mcp-host/src/core/extensions/__tests__/statelessCronForcedApproval.test.tsnew file mode 100644index 000000000..600a078e8--- /dev/null+++ b/mcp-host/src/core/extensions/__tests__/statelessCronForcedApproval.test.ts@@ -0,0 +1,175 @@+import { describe, expect, it } from 'vitest'+import { CronScheduler } from '../../../agent/cronScheduler'+import { MessageQueue } from '../../../queue/messageQueue'+import { ConversationManager } from '../../conversation/conversation'+import type { ToolRegistry } from '../../interfaces'+import { CronManageTool } from '../../tools/cronManage'+import { ApprovalController } from '../approvalController'+import {+ STATELESS_CRON_APPROVAL_PROMPT,+ UnifiedApprovalGateController,+} from '../mcpApprovalGateController'++// The real gate, ApprovalController and approve() joined through one+// conversation. On a stateless host with cron management allowed, cron_manage+// create/enable is a forced approval; every other cron_manage action follows+// the per-task session rule.+function gateFor(options: { stateless: boolean; cronManageGateOnly?: boolean }) {+ const cronTool = new CronManageTool(+ new CronScheduler(new MessageQueue(), { allowEnabledJobs: true }),+ undefined,+ options.stateless,+ true+ )+ const registry: ToolRegistry = {+ get: name => (name === 'cron_manage' ? cronTool : null),+ register: () => {},+ listDefinitions: () => [],+ }+ return new UnifiedApprovalGateController(+ registry,+ { defaultPolicy: 'channel_users', channels: {}, tools: { shell_exec: true } },+ undefined,+ { statelessLifecycle: options.stateless, cronManageGateOnly: options.cronManageGateOnly }+ )+}++async function harness(+ sessionKey: string,+ options: { stateless: boolean; cronManageGateOnly?: boolean } = { stateless: true }+) {+ const manager = new ConversationManager()+ const conversation = await manager.getOrCreate(sessionKey)+ const gate = gateFor(options)+ const controller = new ApprovalController(conversation, gate, undefined, gate)+ await manager.startTurn(conversation, 'First message', 'test-task')+ let callCount = 0++ /** One model-generated call: 'proceed', or 'suspend' after recording the card. */+ async function call(+ toolName: string,+ params: Record<string, unknown> = {}+ ): Promise<'proceed' | 'suspend'> {+ const decision = controller.beforeTool(toolName, params)+ if (decision === 'skip') throw new Error(`Unexpected skip for ${toolName}`)+ if (decision === 'proceed') return 'proceed'+ callCount += 1+ await manager.suspendForApproval(conversation, {+ ...decision.approval,+ tool_call_id: `tc-${callCount}`,+ })+ return 'suspend'+ }++ /** Approve the open card and consume it, as TaskExecutor.resumeAfterApproval does. */+ async function approve(always = false) {+ await manager.approve(conversation, always)+ conversation.pending_approval = undefined+ }++ async function nextTurn() {+ await manager.completeTurn(conversation, 'Done')+ await manager.startTurn(conversation, 'Next message', 'test-task')+ }++ return { conversation, call, approve, nextTurn }+}++const list = { action: 'list' }+const create = { action: 'create', name: 'nightly', schedule: '0 0 * * *', task: 'report' }+const enable = { action: 'enable', jobId: 'job-1' }++describe('stateless cron_manage create/enable forced approval', () => {+ it('after a plain cron_manage list approval in the task, create still suspends with exact scope', async () => {+ const h = await harness('cron-user:rpc:cron-agent:plain-list')+ expect(await h.call('cron_manage', list)).toBe('suspend')+ expect(h.conversation.pending_approval?.authorization_scope).toBe('turn_tools')+ await h.approve()+ expect(h.conversation.task_approved_tools).toEqual(new Set(['cron_manage']))++ // Witness: the per-task approval covers a later list call.+ expect(await h.call('cron_manage', list)).toBe('proceed')+ expect(await h.call('cron_manage', create)).toBe('suspend')+ expect(h.conversation.pending_approval).toMatchObject({+ tool_name: 'cron_manage',+ authorization_scope: 'exact_invocation',+ description: STATELESS_CRON_APPROVAL_PROMPT,+ })+ })++ it('after an "always" cron_manage approval, enable still suspends in a later task', async () => {+ const h = await harness('cron-user:rpc:cron-agent:always')+ expect(await h.call('cron_manage', list)).toBe('suspend')+ await h.approve(true)+ await h.nextTurn()+ expect(h.conversation.auto_approved_tools).toEqual(new Set(['cron_manage']))++ // Witness: the "always" approval covers list in the new task.+ expect(await h.call('cron_manage', list)).toBe('proceed')+ expect(await h.call('cron_manage', enable)).toBe('suspend')+ expect(h.conversation.pending_approval).toMatchObject({+ tool_name: 'cron_manage',+ authorization_scope: 'exact_invocation',+ })+ })++ it("after an MCP approval adds '*', create still suspends with exact scope", async () => {+ const h = await harness('cron-user:rpc:cron-agent:wildcard')+ expect(await h.call('mongodb-server__find')).toBe('suspend')+ await h.approve()+ expect(h.conversation.auto_approved_tools).toEqual(new Set(['*', 'mongodb-server']))++ // Witness: '*' covers another MCP server's tool in the same turn.+ expect(await h.call('airtable-server__list_tables')).toBe('proceed')+ expect(await h.call('cron_manage', create)).toBe('suspend')+ expect(h.conversation.pending_approval?.authorization_scope).toBe('exact_invocation')+ })++ it('approving a forced create card stores no approval, so the next create asks again', async () => {+ const h = await harness('cron-user:rpc:cron-agent:exact-card')+ expect(await h.call('cron_manage', create)).toBe('suspend')+ const firstCard = h.conversation.pending_approval+ expect(firstCard?.authorization_scope).toBe('exact_invocation')+ await h.approve(true)++ expect(h.conversation.task_approved_tools).toBeUndefined()+ expect(h.conversation.auto_approved_tools).toEqual(new Set())+ expect(await h.call('cron_manage', create)).toBe('suspend')+ // Witness: a new card replaced the approved one.+ expect(h.conversation.pending_approval?.request_id).not.toBe(firstCard?.request_id)+ expect(h.conversation.pending_approval?.authorization_scope).toBe('exact_invocation')+ // The exact approval did not cover list either.+ await h.approve()+ expect(await h.call('cron_manage', list)).toBe('suspend')+ })++ it('on a non-stateless host a plain cron_manage approval covers create for the rest of the task', async () => {+ const h = await harness('cron-user:rpc:cron-agent:stateful', { stateless: false })+ expect(await h.call('cron_manage', create)).toBe('suspend')+ expect(h.conversation.pending_approval).toMatchObject({+ authorization_scope: 'turn_tools',+ description: 'Tool "cron_manage" requires approval before execution',+ })+ await h.approve()++ expect(await h.call('cron_manage', create)).toBe('proceed')+ expect(await h.call('cron_manage', enable)).toBe('proceed')+ await h.nextTurn()+ expect(await h.call('cron_manage', create)).toBe('suspend')+ })++ it('keeps the cron-sourced gate-only path: create suspends and every other call proceeds', async () => {+ const h = await harness('cron-user:rpc:cron-agent:gate-only', {+ stateless: true,+ cronManageGateOnly: true,+ })+ expect(await h.call('cron_manage', list)).toBe('proceed')+ expect(await h.call('shell_exec', { command: 'true' })).toBe('proceed')+ expect(await h.call('cron_manage', create)).toBe('suspend')+ expect(h.conversation.pending_approval).toMatchObject({+ tool_name: 'cron_manage',+ authorization_scope: 'exact_invocation',+ description: STATELESS_CRON_APPROVAL_PROMPT,+ })+ })+})diff --git a/mcp-host/src/core/extensions/approvalController.ts b/mcp-host/src/core/extensions/approvalController.tsindex ca07197e1..9a2e84d9d 100644--- a/mcp-host/src/core/extensions/approvalController.ts+++ b/mcp-host/src/core/extensions/approvalController.ts@@ -20,6 +20,33 @@ import { ChatMessage, PendingApproval, ToolDefinition } from '../types'import type { Conversation } from '../types'import { getMcpServerPrefix } from './mcpApprovalGateController'+/**+ * Native tools whose approval is scoped to the tool itself. A plain approval+ * stores the name in `task_approved_tools`, so the tool runs without a new card+ * for the rest of the current task, including after resumes; the next user+ * message starts a new task and asks again. An "always" approval stores it in+ * `auto_approved_tools`, which covers later tasks while the conversation stays+ * in memory. Neither survives a cold resume. These approvals never add the+ * turn-wide `'*'`, and a `'*'` granted by any other approval does not cover+ * these tools: each one needs its own approval.+ */+export const SESSION_SCOPED_APPROVAL_TOOLS: ReadonlySet<string> = new Set([+ 'shell_exec',+ 'http_request',+ 'cron_manage',+])++/**+ * Source of approvals that no stored approval may cover (the cron×stateless+ * create/enable gate). Implemented by UnifiedApprovalGateController.+ */+export interface ForcedApprovalGate {+ forcedApproval(+ toolName: string,+ params: Record<string, unknown>+ ): { type: 'suspend'; approval: PendingApproval } | null+}+/*** Decorator that checks auto_approved_tools before delegating to base controller.*/@@ -27,15 +54,18 @@ export class ApprovalController implements LoopController {private readonly conversation: Conversationprivate readonly delegate: LoopControllerprivate readonly liveApprovalTools: ReadonlySet<string>+ private readonly forcedApprovalGate: ForcedApprovalGate | undefinedconstructor(conversation: Conversation,delegate: LoopController,- liveApprovalTools?: ReadonlySet<string>+ liveApprovalTools?: ReadonlySet<string>,+ forcedApprovalGate?: ForcedApprovalGate) {this.conversation = conversationthis.delegate = delegatethis.liveApprovalTools = liveApprovalTools ?? new Set<string>()+ this.forcedApprovalGate = forcedApprovalGate}shouldAccept(content: string, iteration: number): boolean {@@ -47,8 +77,12 @@ export class ApprovalController implements LoopController {}/**+ * A forced approval (cron×stateless create/enable) is checked first and+ * always suspends, before any stored approval is consulted.+ ** Live-approval tools always consult the delegate. Other tools retain the- * auto_approved_tools and matching pending_approval one-shot behavior.+ * auto_approved_tools and matching pending_approval one-shot behavior;+ * `'*'` does not apply to SESSION_SCOPED_APPROVAL_TOOLS.** Non-live one-shot approval: if the conversation has a pending_approval whose* tool_name matches, this means the user approved the tool for this@@ -60,6 +94,12 @@ export class ApprovalController implements LoopController {toolName: string,params: Record<string, unknown>): 'proceed' | 'skip' | { type: 'suspend'; approval: PendingApproval } {+ // Forced approvals ask on every call: no '*', per-task, "always" or server+ // prefix approval covers them, and their exact scope keeps an approved card+ // from adding any of those.+ const forced = this.forcedApprovalGate?.forcedApproval(toolName, params)+ if (forced) return forced+// The approved frozen call executes directly in resumeAfterApproval.// Every model-generated live call needs a new delegate decision; retained// snapshot data and persistent approvals cannot authorize another invocation.@@ -76,13 +116,21 @@ export class ApprovalController implements LoopController {: decision}- // "Approve once, run all" — user approved any tool in this turn, auto-approve rest- if (this.conversation.auto_approved_tools.has('*')) {+ // "Approve once, run all" — user approved any tool in this turn, auto-approve+ // the rest, except session-scoped tools, which need their own approval.+ if (+ this.conversation.auto_approved_tools.has('*') &&+ !SESSION_SCOPED_APPROVAL_TOOLS.has(toolName)+ ) {return 'proceed'}- // Check individual tool name- if (this.conversation.auto_approved_tools.has(toolName)) {+ // Check individual tool name: "always" grants, then this task's plain+ // session-scoped approvals.+ if (+ this.conversation.auto_approved_tools.has(toolName) ||+ this.conversation.task_approved_tools?.has(toolName) === true+ ) {return 'proceed'}diff --git a/mcp-host/src/core/extensions/mcpApprovalGateController.ts b/mcp-host/src/core/extensions/mcpApprovalGateController.tsindex 86608ddba..f65974d99 100644--- a/mcp-host/src/core/extensions/mcpApprovalGateController.ts+++ b/mcp-host/src/core/extensions/mcpApprovalGateController.ts@@ -160,7 +160,7 @@ export class UnifiedApprovalGateController implements LoopController {// proceeds without a gate, so LIST/GET/DELETE/DISABLE/TRIGGER and all// non-cron_manage work run unblocked.if (this.options?.cronManageGateOnly === true) {- return this.cronManageForcedApproval(toolName, params) ?? 'proceed'+ return this.forcedApproval(toolName, params) ?? 'proceed'}const registryWithNative = this.toolRegistry as NativeAwareRegistry@@ -185,7 +185,7 @@ export class UnifiedApprovalGateController implements LoopController {// `approval.tools["cron_manage"]: false` cannot waive it. Default-forbid// hosts expose cron_manage for cleanup/observability, but the tool reports// requiresApproval=false and rejects CREATE/ENABLE server-side.- const cronForced = this.cronManageForcedApproval(toolName, params)+ const cronForced = this.forcedApproval(toolName, params)if (cronForced) {return cronForced}@@ -226,8 +226,13 @@ export class UnifiedApprovalGateController implements LoopController {* cron-sourced `cronManageGateOnly` path so both apply the same criterion.* NOT waivable by the per-tool CRD override — it is checked before that* override on both paths.+ *+ * Public so ApprovalController can consult it before any stored approval:+ * every forced call asks, and no `'*'`, per-task, "always" or server-prefix+ * approval covers it. The suspension is `exact_invocation`, so approving it+ * authorizes only that frozen call.*/- private cronManageForcedApproval(+ forcedApproval(toolName: string,params: Record<string, unknown>): { type: 'suspend'; approval: PendingApproval } | null {@@ -241,7 +246,16 @@ export class UnifiedApprovalGateController implements LoopController {const action = typeof params.action === 'string' ? params.action : ''if (STATELESS_CRON_GATED_ACTIONS.has(action)) {- return this.createSuspension(toolName, params, STATELESS_CRON_APPROVAL_PROMPT, tool)+ const suspension = this.createSuspension(+ toolName,+ params,+ STATELESS_CRON_APPROVAL_PROMPT,+ tool+ )+ return {+ ...suspension,+ approval: { ...suspension.approval, authorization_scope: 'exact_invocation' },+ }}}return nulldiff --git a/mcp-host/src/core/types.ts b/mcp-host/src/core/types.tsindex 2055b11a0..2ce6002d6 100644--- a/mcp-host/src/core/types.ts+++ b/mcp-host/src/core/types.ts@@ -445,6 +445,14 @@ export interface Conversation {turns: Turn[]pending_approval?: PendingApprovalauto_approved_tools: Set<string>+ /**+ * Session-scoped tools (SESSION_SCOPED_APPROVAL_TOOLS) approved without+ * "always" during the current task. Every later call of the task proceeds,+ * including after resumes; `startTurn` clears it when the next user message+ * starts a new task. Ephemeral (like `auto_approved_tools`): not persisted,+ * so a cold resume asks again. Absent until the first such approval.+ */+ task_approved_tools?: Set<string>/*** Guardrail doom-loop counter (spec §6.4) — tracks consecutive identical* `(resolved tool, effective-input)` tool calls across turns within a task.diff --git a/mcp-host/src/gfsRuntime.ts b/mcp-host/src/gfsRuntime.tsindex 3d554b26d..043a746c1 100644--- a/mcp-host/src/gfsRuntime.ts+++ b/mcp-host/src/gfsRuntime.ts@@ -11,9 +11,23 @@ export interface GfsRuntime {export interface GfsRuntimeOptions {/** Interval of the lifecycle cycle: initialize when unavailable, otherwise sweep. */cycleMs?: number+ /**+ * How long stop() waits for the cycle in flight, and then for the store's+ * drain in close(); each wait has this deadline.+ */+ stopTimeoutMs?: number}-const DEFAULT_CYCLE_MS = 60 * 60 * 1000+/**+ * Every five minutes: each cycle sweeps expired copies and restores the+ * free-space floor, so a volume another writer filled is given back within+ * this window. A cycle costs one walk of users/<key>/.gfs-downloads and one+ * statfs; it removes files only when something expired or the volume is+ * below the floor.+ */+const DEFAULT_CYCLE_MS = 5 * 60 * 1000+/** The same deadline as GfsDownloadStore.close()'s default drain. */+const DEFAULT_STOP_TIMEOUT_MS = 5_000function failureCode(error: unknown): string {if (error instanceof GfsDownloadStoreError) return error.code@@ -37,6 +51,9 @@ export async function bootstrapGfsRuntime(const cycleMs = options.cycleMs ?? DEFAULT_CYCLE_MSif (!Number.isSafeInteger(cycleMs) || cycleMs < 25)throw new Error('GFS runtime cycleMs must be an integer of at least 25ms')+ const stopTimeoutMs = options.stopTimeoutMs ?? DEFAULT_STOP_TIMEOUT_MS+ if (!Number.isSafeInteger(stopTimeoutMs) || stopTimeoutMs < 0)+ throw new Error('GFS runtime stopTimeoutMs must be a non-negative integer')let stopped = falselet cycleTimer: ReturnType<typeof setTimeout> | undefinedlet cycleInFlight: Promise<void> | undefined@@ -49,7 +66,7 @@ export async function bootstrapGfsRuntime(if (stopped) returnlogger.error({ component: 'gfs-runtime', code: failureCode(error) },- 'GFS download store unavailable; managed GFS operations are disabled and initialization is retried by the hourly cycle'+ 'GFS download store unavailable; managed GFS operations are disabled and initialization is retried by the next cycle')}}@@ -110,8 +127,26 @@ export async function bootstrapGfsRuntime(if (cycleTimer) clearTimeout(cycleTimer)cycleTimer = undefinedstopping = (async () => {- await cycleInFlight- await store.close()+ // A cycle that never settles (a hung statfs or rm) must not hold+ // shutdown: the wait is bounded and the store is closed regardless.+ // close() then drains the mutation queue under its own deadline.+ const inFlight = cycleInFlight+ if (inFlight !== undefined) {+ let timer: ReturnType<typeof setTimeout> | undefined+ const timedOut = await Promise.race([+ inFlight.then(() => false),+ new Promise<boolean>(resolve => {+ timer = setTimeout(() => resolve(true), stopTimeoutMs)+ }),+ ])+ clearTimeout(timer)+ if (timedOut)+ logger.warn(+ { component: 'gfs-runtime', timeoutMs: stopTimeoutMs },+ 'GFS runtime stop timed out waiting for the cycle in flight; the store is closed anyway'+ )+ }+ await store.close(stopTimeoutMs)})()return stopping},diff --git a/mcp-host/src/internalTools/gfsContentDownload.test.ts b/mcp-host/src/internalTools/gfsContentDownload.test.tsindex 7c3d3fc24..a046d8df3 100644--- a/mcp-host/src/internalTools/gfsContentDownload.test.ts+++ b/mcp-host/src/internalTools/gfsContentDownload.test.ts@@ -19,8 +19,12 @@ const { rmFailure } = vi.hoisted(() => ({// directory fail once, as a read-only parent or a busy mount would.vi.mock('node:fs/promises', async original => {const actual = await original<typeof import('node:fs/promises')>()+ // statfs reports the volume sized to its free space, so the disk's occupancy+ // never meets the store's free-space floor.+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')return {...actual,+ statfs: freeSpaceSizedStatfs(actual.statfs),rm: async (...values: Parameters<typeof actual.rm>) => {if (rmFailure.target !== undefined &&diff --git a/mcp-host/src/internalTools/gfsDownloadStore.test.ts b/mcp-host/src/internalTools/gfsDownloadStore.test.tsindex 817e70022..53bd53147 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.test.ts@@ -4,6 +4,7 @@ import * as fs from 'node:fs/promises'import { tmpdir } from 'node:os'import * as path from 'node:path'import { register } from 'prom-client'+import { freeSpaceSizedStatfs } from '../__tests__/fixtures/gfsStoreTestKit'import { logger } from '../logger'import type { GfsImageSource } from '../visualInput/policy'import { GfsDownloadStore, GfsDownloadStoreError } from './gfsDownloadStore'@@ -53,7 +54,8 @@ function sourceFor(index: number, version = 1): GfsImageSource {}}-function future(ms = 60 * 60_000): string {+/** Two hours by default: a reused copy must have more than one hour left. */+function future(ms = 2 * 60 * 60_000): string {return new Date(Date.now() + ms).toISOString()}@@ -115,6 +117,50 @@ async function completedCopy(return { receipt, bytes }}+/**+ * Leaves each copy charged under a trash name: it expires and its removal+ * fails. Eviction cannot reclaim a held charge, and protected bytes are capped+ * at half the budget per caller and three quarters Host-wide, so held charges+ * are how these tests fill the budget with bytes no eviction can free. The rm fault covers trash names in+ * this caller's store directory only, so evictions elsewhere still remove,+ * and it stays installed until the test restores `rmBoundary`. Copies made+ * afterwards are not expired.+ */+async function heldTrash(+ target: GfsDownloadStore,+ root: string,+ caller: string,+ copies: Array<{ index: number; sizeBytes: number; owner?: string }>+) {+ const receipts = []+ for (const copy of copies)+ receipts.push(+ await completedCopy(+ target,+ root,+ caller,+ copy.index,+ copy.sizeBytes,+ copy.owner === undefined ? {} : { owner: copy.owner }+ )+ )+ vi.setSystemTime(Date.now() + 3 * 60 * 60_000)+ // The store removes through the resolved path, so match the caller directory by name.+ const callerName = path.basename(root)+ rmBoundary.mockImplementation(async (removed: string, options: unknown) => {+ const parent = path.dirname(removed)+ if (+ path.basename(parent) === '.gfs-downloads' &&+ path.basename(path.dirname(parent)) === callerName &&+ path.basename(removed).startsWith('.trash-')+ )+ throw Object.assign(new Error('injected EIO'), { code: 'EIO' })+ return nativeFs.rm(removed, options as Parameters<typeof nativeFs.rm>[1])+ })+ expect((await target.cleanupExpired()).removeFailed).toBe(copies.length)+ return receipts+}+async function metricValue(name: string, labels: Record<string, string>): Promise<number> {const metric = register.getSingleMetric(name)if (metric === undefined) throw new Error(`metric ${name} is not registered`)@@ -203,7 +249,7 @@ beforeEach(async () => {vi.setSystemTime(new Date('2026-10-08T10:00:00.000Z'))for (const boundary of [statfsBoundary, renameBoundary, rmBoundary, lstatBoundary, hashBoundary])boundary.mockReset()- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))renameBoundary.mockImplementation(nativeFs.rename)rmBoundary.mockImplementation(nativeFs.rm)lstatBoundary.mockImplementation(nativeFs.lstat)@@ -439,26 +485,26 @@ describe('GFS download store: quotas and eviction', () => {})it('F10: when eviction cannot make room nothing is deleted', async () => {- // Budget 16; a caller protects at most 8. CALLER pins 4 + 4, caller-b pins- // 8, and caller-c, protecting nothing yet, asks for 8: only the host budget- // can refuse it.+ // Budget 16; protected bytes are capped at 8. caller-b holds 9 bytes under+ // trash names it cannot remove, CALLER keeps a pinned 2 and a loose 2, and+ // caller-c asks for 6: 9 + 2 + 6 = 17 > 16 even after evicting the loose+ // copy, so only the host budget can refuse it.volumeOf(16)const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)- const otherRoot = await callerDirectory(hostRoot, 'caller-b')- await completedCopy(limited, callerRoot, CALLER, 30, 4, { owner: 'task-1' })- tick()- await completedCopy(limited, callerRoot, CALLER, 31, 4, { owner: 'task-1' })+ await heldTrash(limited, await callerDirectory(hostRoot, 'caller-b'), 'caller-b', [+ { index: 33, sizeBytes: 5 },+ { index: 34, sizeBytes: 4 },+ ])+ await completedCopy(limited, callerRoot, CALLER, 30, 2, { owner: 'task-1' })tick()- await completedCopy(limited, otherRoot, 'caller-b', 33, 8, { owner: 'task-2' })+ await completedCopy(limited, callerRoot, CALLER, 31, 2)const downloads = path.join(callerRoot, '.gfs-downloads')- const otherDownloads = path.join(otherRoot, '.gfs-downloads')const before = (await nativeFs.readdir(downloads)).sort()- const otherBefore = (await nativeFs.readdir(otherDownloads)).sort()const denied = await quotaCount('host', 'storage_bytes')lstatBoundary.mockClear()const requester = await callerDirectory(hostRoot, 'caller-c')- await expect(startTransfer(limited, requester, 'caller-c', 32, 8)).rejects.toMatchObject({+ await expect(startTransfer(limited, requester, 'caller-c', 32, 6)).rejects.toMatchObject({code: 'host_quota_exceeded',})@@ -466,9 +512,10 @@ describe('GFS download store: quotas and eviction', () => {expect(lstatBoundary.mock.calls.length).toBeGreaterThan(0)expect((await nativeFs.readdir(downloads)).sort()).toEqual(before)expect(before).toHaveLength(2)- expect((await nativeFs.readdir(otherDownloads)).sort()).toEqual(otherBefore)- expect(otherBefore).toHaveLength(1)expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)+ // Witness: once the held bytes can be removed the same admission succeeds.+ rmBoundary.mockImplementation(nativeFs.rm)+ await expect(startTransfer(limited, requester, 'caller-c', 32, 6)).resolves.toBeDefined()})it("F10b: a quota denial carries only its code, never another caller's id, path, name or usage", async () => {@@ -476,13 +523,19 @@ describe('GFS download store: quotas and eviction', () => {const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)const otherRoot = await callerDirectory(hostRoot, 'caller-b')const thirdRoot = await callerDirectory(hostRoot, 'caller-c')- const other = await completedCopy(limited, otherRoot, 'caller-b', 24, 10, { owner: 'task-b' })- const third = await completedCopy(limited, thirdRoot, 'caller-c', 25, 10, { owner: 'task-c' })+ // caller-c holds 20 bytes under trash names it cannot remove; the pinned+ // copies take 10 of the 15 protected bytes the Host allows.+ const [, third] = await heldTrash(limited, thirdRoot, 'caller-c', [+ { index: 29, sizeBytes: 10 },+ { index: 25, sizeBytes: 10, owner: 'task-c' },+ ])+ const other = await completedCopy(limited, otherRoot, 'caller-b', 24, 5, { owner: 'task-b' })await completedCopy(limited, callerRoot, CALLER, 28, 5, { owner: 'task-a' })- // Within maxFileBytes (10); the Host would reach 35 of 30 and every copy- // is pinned, so only the Host quota decision can refuse it.- const error = await startTransfer(limited, callerRoot, CALLER, 26, 10).then(+ // Within maxFileBytes (10) and the protected cap; the Host would reach 35+ // of 30 and nothing is evictable, so only the Host quota decision can+ // refuse it.+ const error = await startTransfer(limited, callerRoot, CALLER, 26, 5).then(() => undefined,(rejection: unknown) => rejection)@@ -508,27 +561,35 @@ describe('GFS download store: quotas and eviction', () => {])expect(serialized).not.toContain(secret)expect(await exists(downloadDirectory(otherRoot, other.receipt.id))).toBe(true)- // Neither size nor free space was the cause: 5 bytes fit the Host's 30 exactly.- await expect(startTransfer(limited, callerRoot, CALLER, 27, 5)).resolves.toBeDefined()+ // Neither size nor free space was the cause: once the held bytes can be+ // removed, the same 5 bytes are admitted.+ rmBoundary.mockImplementation(nativeFs.rm)+ await expect(startTransfer(limited, callerRoot, CALLER, 26, 5)).resolves.toBeDefined()})it('F11: pinned and active downloads are never evicted', async () => {- volumeOf(30)+ // Budget 18; the pinned, active and requested bytes stay within the 9+ // protected bytes the Host allows.+ volumeOf(18)const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)const otherRoot = await callerDirectory(hostRoot, 'caller-b')const thirdRoot = await callerDirectory(hostRoot, 'caller-c')- const pinned = await completedCopy(limited, callerRoot, CALLER, 40, 10, { owner: 'task-40' })+ const pinned = await completedCopy(limited, callerRoot, CALLER, 40, 3, { owner: 'task-40' })tick()- const active = await startTransfer(limited, otherRoot, 'caller-b', 41, 10)+ const unpinned = await completedCopy(limited, thirdRoot, 'caller-c', 42, 5)tick()- const unpinned = await completedCopy(limited, thirdRoot, 'caller-c', 42, 10)+ const newest = await completedCopy(limited, thirdRoot, 'caller-c', 44, 5)+ tick()+ const active = await startTransfer(limited, otherRoot, 'caller-b', 41, 3)tick()- // The Host would reach 40 of 30; one eviction makes room.- await startTransfer(limited, await callerDirectory(hostRoot, 'caller-d'), 'caller-d', 43, 10)+ // The Host would reach 19 of 18; one eviction makes room.+ await startTransfer(limited, await callerDirectory(hostRoot, 'caller-d'), 'caller-d', 43, 3)- // Witness: the only eligible copy, although the newest, was evicted.+ // Witness: the oldest eligible copy, although newer than the pinned one,+ // was evicted, and only that one.expect(await exists(downloadDirectory(thirdRoot, unpinned.receipt.id))).toBe(false)+ expect(await exists(downloadDirectory(thirdRoot, newest.receipt.id))).toBe(true)expect(await exists(downloadDirectory(callerRoot, pinned.receipt.id))).toBe(true)expect(await exists(downloadDirectory(otherRoot, active.transfer.id))).toBe(true)})@@ -537,9 +598,17 @@ describe('GFS download store: quotas and eviction', () => {const otherRoot = await callerDirectory(hostRoot, 'caller-b')await startTransfer(store, otherRoot, 'caller-b', 50, 4096 * 10)const observed = await nativeFs.statfs(hostRoot, { bigint: true })+ // A 40 MiB volume: 15% of it is under 16 MiB, so the free-space floor is+ // its 16 MiB minimum.+ const volumeBlocks = 10_240nconst reserveBlocks = (16n * 1024n * 1024n) / 4096n- // Room for the request and the reserve, but not for the active reservation.- statfsBoundary.mockResolvedValue({ ...observed, bsize: 4096n, bavail: reserveBlocks + 2n })+ // Room for the request and the floor, but not for the active reservation.+ statfsBoundary.mockResolvedValue({+ ...observed,+ bsize: 4096n,+ blocks: volumeBlocks,+ bavail: reserveBlocks + 2n,+ })const denied = await quotaCount('host', 'free_space')await expect(startTransfer(store, callerRoot, CALLER, 51, 4096)).rejects.toMatchObject({@@ -549,7 +618,12 @@ describe('GFS download store: quotas and eviction', () => {expect(await quotaCount('host', 'free_space')).toBe(denied + 1)// Control: with room for the active reservation too, the same request is admitted.- statfsBoundary.mockResolvedValue({ ...observed, bsize: 4096n, bavail: reserveBlocks + 11n })+ statfsBoundary.mockResolvedValue({+ ...observed,+ bsize: 4096n,+ blocks: volumeBlocks,+ bavail: reserveBlocks + 11n,+ })await expect(startTransfer(store, callerRoot, CALLER, 51, 4096)).resolves.toBeDefined()})@@ -996,26 +1070,30 @@ describe('GFS download store: adversarial review kill tests', () => {it('ADV-S08: an infeasible eviction plan deletes nothing even when some copies are evictable', async () => {volumeOf(15)const { limited } = await limitedStore(SMALL_FILES_FULL_VOLUME)- // Budget 15; a caller protects at most 7. The 10 pinned bytes are held by- // caller-b and caller-c (5 each) and CALLER asks for 7: 5 + 10 + 7 = 22,- // and evicting the loose 5 still leaves 17 > 15.- const loose = await completedCopy(limited, callerRoot, CALLER, 70, 5)+ // Budget 15; protected bytes are capped at 7. caller-c holds 9 bytes under+ // trash names it cannot remove, caller-b pins 2 and CALLER asks for 5:+ // 9 + 4 + 2 + 5 = 20, and evicting the loose 4 still leaves 16 > 15.+ const thirdRoot = await callerDirectory(hostRoot, 'caller-c')+ await heldTrash(limited, thirdRoot, 'caller-c', [+ { index: 73, sizeBytes: 5 },+ { index: 74, sizeBytes: 4 },+ ])+ const loose = await completedCopy(limited, callerRoot, CALLER, 70, 4)tick()const otherRoot = await callerDirectory(hostRoot, 'caller-b')- const thirdRoot = await callerDirectory(hostRoot, 'caller-c')- await completedCopy(limited, otherRoot, 'caller-b', 71, 5, { owner: 'task-1' })- await completedCopy(limited, thirdRoot, 'caller-c', 73, 5, { owner: 'task-2' })+ await completedCopy(limited, otherRoot, 'caller-b', 71, 2, { owner: 'task-1' })const denied = await quotaCount('host', 'storage_bytes')- await expect(startTransfer(limited, callerRoot, CALLER, 72, 7)).rejects.toMatchObject({+ await expect(startTransfer(limited, callerRoot, CALLER, 72, 5)).rejects.toMatchObject({code: 'host_quota_exceeded',})// Witness: the admission reached the quota decision.expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)// The unpinned copy could not make room on its own, so it must survive.expect(await exists(downloadDirectory(callerRoot, loose.receipt.id))).toBe(true)- // Witness: once one pin is released the same admission evicts and succeeds.+ // Witness: once the pin is released the same admission evicts and succeeds.await limited.releaseReceiptOwner('task-1', 'caller-b')- await expect(startTransfer(limited, callerRoot, CALLER, 72, 7)).resolves.toBeDefined()+ await expect(startTransfer(limited, callerRoot, CALLER, 72, 5)).resolves.toBeDefined()+ expect(await exists(downloadDirectory(callerRoot, loose.receipt.id))).toBe(false)})it('ADV-S11: publish refuses a digest that does not match the partial bytes', async () => {diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.tsindex 4a1d0d2b9..86db91240 100644--- a/mcp-host/src/internalTools/gfsDownloadStore.ts+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts@@ -29,7 +29,22 @@ const META_FILE = 'meta.json'const SOURCE_FILE = 'source'const PARTIAL_FILE = 'source.partial'const META_MAX_BYTES = 64 * 1024-const FREE_SPACE_RESERVE_BYTES = 16n * 1024n * 1024n+/**+ * Share of the volume admission keeps free and the startup and periodic+ * sweeps restore. The+ * volume is shared: on a stateless Host `state/` (state.db) is a subPath of+ * the same PVC, and shell commands and file writes land on it too.+ */+const FREE_SPACE_FLOOR_PERCENT = 15n+/** The floor on a volume too small for its percentage to reach this. */+const FREE_SPACE_FLOOR_MIN_BYTES = 16n * 1024n * 1024n+/**+ * Share of the volume, above the floor, that a restore frees once the volume+ * fell below the floor. Admission still requires only the floor.+ */+const FREE_SPACE_RESTORE_HEADROOM_PERCENT = 5n+/** A copy reused with less time left could expire while a shell command reads it. */+const REUSE_MIN_REMAINING_MS = 60 * 60 * 1000/** Host-root directory of the ledger store written before #1028; never read. */const LEGACY_STORE_DIRECTORY = '.gfs-download-store'@@ -53,7 +68,7 @@ const SHA256_RE = /^[0-9a-f]{64}$/export type GfsDownloadStoreErrorCode =| 'caller_mismatch'- /** The Host volume's free space cannot hold the reservation plus its margin. */+ /** The Host volume's free space cannot hold the reservation plus the free-space floor. */| 'disk_full'| 'download_busy'| 'download_expired'@@ -176,6 +191,26 @@ function retainedBudget(volume: VolumeSpace): bigint {return (volume.blocks * volume.bsize * BigInt(GFS_FILE_LIMITS.storagePercent)) / 100n}+/**+ * Free bytes the volume must keep after every admission:+ * `max(floor(volumeTotalBytes * 15 / 100), 16 MiB)`, in bigint.+ */+export function freeSpaceFloor(volume: { bsize: bigint; blocks: bigint }): bigint {+ const share = (volume.blocks * volume.bsize * FREE_SPACE_FLOOR_PERCENT) / 100n+ return share > FREE_SPACE_FLOOR_MIN_BYTES ? share : FREE_SPACE_FLOOR_MIN_BYTES+}++/**+ * Free bytes a restore aims for once the volume is below the floor:+ * `freeSpaceFloor + floor(volumeTotalBytes * 5 / 100)`, in bigint.+ */+export function freeSpaceRestoreTarget(volume: { bsize: bigint; blocks: bigint }): bigint {+ return (+ freeSpaceFloor(volume) ++ (volume.blocks * volume.bsize * FREE_SPACE_RESTORE_HEADROOM_PERCENT) / 100n+ )+}+/** `bytes` rounded up to whole blocks of the volume. */function blockBytes(volume: VolumeSpace, bytes: number): bigint {return ((BigInt(bytes) + volume.bsize - 1n) / volume.bsize) * volume.bsize@@ -461,20 +496,51 @@ async function treeUsage(root: string): Promise<{ files: number; bytes: number }return usage}+/**+ * Gives the owner rwx on one directory, keeping its other mode bits. The name+ * is opened with O_DIRECTORY | O_NOFOLLOW and the mode is changed through+ * that descriptor, after its fstat confirms a directory: a symlink in the+ * name's place is refused by the open and never followed, and the mode that+ * is set is computed from, and applied to, the inode that was opened,+ * whatever the name leads to afterwards. A symlink or a non-directory+ * changes nothing and returns false. The open needs the owner's read bit, so+ * a directory whose mode denies its owner read access (`chmod 0300`, `0000`)+ * cannot be repaired here: that EACCES propagates, like any other failure.+ */+async function restoreDirectoryOwnerAccess(directory: string): Promise<boolean> {+ let handle: fs.FileHandle+ try {+ handle = await fs.open(+ directory,+ constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW | constants.O_NONBLOCK+ )+ } catch (error) {+ const code = errorCode(error)+ if (code === 'ELOOP' || code === 'ENOTDIR') return false+ throw error+ }+ try {+ const info = await handle.stat()+ if (!info.isDirectory()) return false+ await handle.chmod((info.mode & 0o7777) | 0o700)+ return true+ } finally {+ await handle.close()+ }+}+/*** Gives the owner rwx on every real directory under `root`, root included,- * keeping the other mode bits. Every path is lstat-ed first and only a- * directory is changed, so no symlink is followed to its target; a name- * swapped for a symlink between that lstat and the chmod leads to an inode- * the Host UID owns or cannot change, and gains owner bits only.+ * through restoreDirectoryOwnerAccess. Children are listed and reached by+ * path, because Node has no openat: the final component of each path is+ * never followed, but a directory swapped for a symlink after it was changed+ * is resolved as a parent component when its children are listed and opened.*/async function restoreOwnerAccess(root: string): Promise<void> {const pending = [root]while (pending.length > 0) {const directory = pending.pop()!- const info = await fs.lstat(directory)- if (!info.isDirectory()) continue- await fs.chmod(directory, (info.mode & 0o7777) | 0o700)+ if (!(await restoreDirectoryOwnerAccess(directory))) continuefor (const entry of await fs.readdir(directory, { withFileTypes: true }))if (entry.isDirectory()) pending.push(path.join(directory, entry.name))}@@ -596,7 +662,14 @@ export class GfsDownloadStore {if (error instanceof GfsDownloadStoreError) throw errorthrow new GfsDownloadStoreError('workspace_unavailable')}- const totals = await this.serialize(() => this.sweep(Date.now()))+ // The floor is restored at startup too: a Host restarting on a full volume+ // frees unpinned copies before the rest of the process needs to write.+ const totals = await this.serialize(async () => {+ const now = Date.now()+ const swept = await this.sweep(now)+ await this.restoreFreeSpaceFloor(now)+ return swept+ })if (this.closed || this.closing) throw new GfsDownloadStoreError('download_busy')this.initialized = truelogger.info(@@ -905,12 +978,14 @@ export class GfsDownloadStore {if (bounds?.retentionOwnerId !== undefined) this.assertReceiptOwner(bounds.retentionOwnerId)// Only copies this process published: an adopted meta.json carries an// identity and a digest that anyone with the Host UID could have written.- // A miss is final; the caller downloads the file again.+ // A miss is final; the caller downloads the file again. A copy with less+ // than REUSE_MIN_REMAINING_MS left is a miss too, so a fresh download+ // with the full retention replaces it instead of being hashed for nothing.const entry = [...this.entries.values()].find(candidate =>candidate.provenance === 'published' &&candidate.callerIdentity === callerIdentity &&- Date.parse(candidate.expiresAt) > Date.now() &&+ Date.parse(candidate.expiresAt) - REUSE_MIN_REMAINING_MS > Date.now() &&candidate.sizeBytes === sizeBytes &&candidate.source.drive === source.drive &&candidate.source.resourceId === source.resourceId &&@@ -918,7 +993,8 @@ export class GfsDownloadStore {)if (!entry) return undefined// Pinning a copy this caller does not protect yet adds it to the- // caller's protected bytes; checked before hashing, so a refusal costs+ // caller's protected bytes (capped at half the budget) and to the Host+ // total (capped at three quarters); checked before hashing, so a refusal costs// no read. A copy the caller already protects adds nothing, so that is// decided first and re-pinning it never depends on statfs.if (@@ -964,10 +1040,11 @@ export class GfsDownloadStore {return undefined}this.assertPublicationOpen(bounds)- // Expiry may have been crossed while the copy was hashed: an expired- // copy is a cache miss, never pinned or returned. The caller downloads- // the file again and the sweep removes this copy.- if (Date.parse(entry.expiresAt) <= Date.now()) return undefined+ // The reuse margin may have been crossed while the copy was hashed: a+ // copy that expires within REUSE_MIN_REMAINING_MS is a cache miss, never+ // pinned or returned. The caller downloads the file again and the sweep+ // removes this copy once it expires.+ if (Date.parse(entry.expiresAt) - REUSE_MIN_REMAINING_MS <= Date.now()) return undefinedentry.lastUsedMs = Date.now()if (bounds?.retentionOwnerId !== undefined)this.pin(bounds.retentionOwnerId, callerIdentity, entry.id)@@ -1079,11 +1156,20 @@ export class GfsDownloadStore {})}- /** Removes expired and incomplete downloads; startup runs the same sweep. */+ /**+ * Removes expired and incomplete downloads, then restores the free-space+ * floor in the same critical section. Startup runs the same sweep and+ * restore; an admission runs only the sweep and plans its own eviction+ * against the floor.+ */async cleanupExpired(now = Date.now()): Promise<GfsDownloadSweepResult> {this.assertReadable()try {- const totals = await this.serialize(() => this.sweep(now))+ const totals = await this.serialize(async () => {+ const swept = await this.sweep(now)+ await this.restoreFreeSpaceFloor(now)+ return swept+ })return {removedExpired: totals.removedExpired,removedIncomplete: totals.removedIncomplete,@@ -1732,22 +1818,29 @@ export class GfsDownloadStore {* about space. A statfs that rejects is not a reading and propagates as is.*/private async measureVolume(): Promise<VolumeSpace> {+ const volume = await this.readVolume()+ if (volume !== undefined) return volume+ recordGfsDownloadQuota('host', 'free_space')+ throw new GfsDownloadStoreError('volume_unmeasurable')+ }++ /** Up to two statfs readings; undefined when both are invalid (see measureVolume). */+ private async readVolume(): Promise<VolumeSpace | undefined> {for (let reading = 0; reading < 2; reading += 1) {const space = await fs.statfs(this.hostRoot, { bigint: true })if (space.bsize > 0n && space.blocks > 0n && space.bavail >= 0n)return { bsize: space.bsize, blocks: space.blocks, bavail: space.bavail }}- recordGfsDownloadQuota('host', 'free_space')- throw new GfsDownloadStoreError('volume_unmeasurable')+ return undefined}/*** Bytes the volume is short of for this admission: the request and every- * active reservation, each rounded up to whole blocks, plus the 16 MiB- * margin, minus the available bytes; 0n when it fits.+ * active reservation, each rounded up to whole blocks, plus the free-space+ * floor, minus the available bytes; 0n when it fits.*/private physicalDeficit(space: VolumeSpace, sizeBytes: number): bigint {- let required = blockBytes(space, sizeBytes) + FREE_SPACE_RESERVE_BYTES+ let required = blockBytes(space, sizeBytes) + freeSpaceFloor(space)// Use the full outstanding reservation of every active transfer rather// than crediting sparse, shared or concurrently written allocation.for (const transfer of this.active.values()) required += blockBytes(space, transfer.sizeBytes)@@ -1763,17 +1856,22 @@ export class GfsDownloadStore {}/**- * Ids one caller holds out of eviction: its active reservations and the- * unexpired copies its retention owners pin, each id once. An expired- * pinned copy protects nothing: the sweep and eviction both remove it.+ * Ids held out of eviction: active reservations and the unexpired copies+ * retention owners pin, each id once; those of one caller, or of every+ * caller when `callerIdentity` is omitted. An expired pinned copy protects+ * nothing: the sweep and eviction both remove it.*/- private protectedIds(callerIdentity: string): Set<string> {+ private protectedIds(callerIdentity?: string): Set<string> {const ids = new Set<string>()const now = Date.now()for (const transfer of this.active.values())- if (transfer.callerIdentity === callerIdentity) ids.add(transfer.id)+ if (callerIdentity === undefined || transfer.callerIdentity === callerIdentity)+ ids.add(transfer.id)for (const [key, pinned] of this.pins)- if ((JSON.parse(key) as [string, string])[0] === callerIdentity)+ if (+ callerIdentity === undefined ||+ (JSON.parse(key) as [string, string])[0] === callerIdentity+ )for (const id of pinned) {const entry = this.entries.get(id)if (entry !== undefined && Date.parse(entry.expiresAt) <= now) continue@@ -1782,19 +1880,28 @@ export class GfsDownloadStore {return ids}+ /** Bytes of the protected `ids`: a reservation's size, or an indexed copy's. */+ private protectedBytes(ids: ReadonlySet<string>): bigint {+ let bytes = 0n+ for (const id of ids)+ bytes += BigInt(this.active.get(id)?.sizeBytes ?? this.entries.get(id)?.sizeBytes ?? 0)+ return bytes+ }+/*** Eviction never reclaims an unexpired pinned copy or a reservation, so- * without a bound one caller's open tasks could hold the whole budget and- * refuse every other caller. One caller's protected bytes may therefore- * reach at most `floor(budget / 2)`. That bounds each caller, not their- * sum: two callers at the cap protect `budget - (budget mod 2)` bytes, the- * whole budget when it is even and all but one byte when it is odd, and- * more callers can protect all of it. Then admissions larger than what is- * left are refused until protection is released or the pinned copies+ * without a bound open tasks could hold the whole budget, refuse every+ * other admission and leave the free-space restore nothing to evict. Two+ * bounds apply: the bytes one caller protects may reach `floor(budget / 2)`,+ * and the bytes every caller protects together may reach+ * `floor(budget * 3 / 4)`, so at least a quarter of the budget always stays+ * evictable. One caller at its bound leaves a quarter of the budget for the+ * other callers' protected copies; filling the Host bound takes at least two+ * callers. A refusal waits until protection is released or the pinned copies* expire. Expiry is absolute, so one pinned copy cannot outlive its* retention window, but re-pinning fresh copies (new downloads or reuse of- * unexpired ones) can keep that state beyond one TTL. Only this caller's- * own usage decides; the refusal is the fixed code.+ * unexpired ones) can keep that state beyond one TTL. Both refusals are the+ * fixed code; the metric scope says which bound refused.*/private assertProtectedRoom(callerIdentity: string,@@ -1803,13 +1910,15 @@ export class GfsDownloadStore {): void {const ids = this.protectedIds(callerIdentity)if (addition.id !== undefined && ids.has(addition.id)) return- let bytes = BigInt(addition.sizeBytes)- for (const id of ids)- bytes += BigInt(this.active.get(id)?.sizeBytes ?? this.entries.get(id)?.sizeBytes ?? 0)- if (bytes > budget / 2n) {+ const added = BigInt(addition.sizeBytes)+ if (this.protectedBytes(ids) + added > budget / 2n) {recordGfsDownloadQuota('caller', 'protected_bytes')throw new GfsDownloadStoreError('host_quota_exceeded')}+ if (this.protectedBytes(this.protectedIds()) + added > (budget * 3n) / 4n) {+ recordGfsDownloadQuota('host', 'protected_bytes')+ throw new GfsDownloadStoreError('host_quota_exceeded')+ }}/** Retained bytes: indexed copies, active reservations and held duplicates. */@@ -1838,7 +1947,7 @@ export class GfsDownloadStore {* process published, then published copies least recently used first.* The same plan covers the free-space deficit: with the budget at a share* of the volume, the cache plus everything else on it can leave less free- * space than a request and its margin while the cache is under budget, and+ * space than a request and the floor while the cache is under budget, and* the copies it holds are what can be freed. A copy is credited with its* size in whole blocks; the volume is measured again after the removals,* so a copy that freed less (a hard link, say) refuses then, not here.@@ -1856,11 +1965,7 @@ export class GfsDownloadStore {budgetCovered: !this.overBudget(sizeBytes, budget, ids),physicalCovered: freed >= deficit,})- const now = Date.now()- const candidates = [...this.entries.values()]- .filter(entry => !this.isPinned(entry.id) || Date.parse(entry.expiresAt) <= now)- .sort(evictionOrder)- for (const candidate of candidates) {+ for (const candidate of this.evictionCandidates(Date.now())) {const state = covered()if (state.budgetCovered && state.physicalCovered) breakids.add(candidate.id)@@ -1869,6 +1974,17 @@ export class GfsDownloadStore {return { ids, ...covered() }}+ /**+ * Indexed copies eviction may take, in evictionOrder: every copy that is+ * not pinned, or whose pin protects nothing because it expired by `now`.+ * Reservations are never indexed, so no active transfer is a candidate.+ */+ private evictionCandidates(now: number): Entry[] {+ return [...this.entries.values()]+ .filter(entry => !this.isPinned(entry.id) || Date.parse(entry.expiresAt) <= now)+ .sort(evictionOrder)+ }+/** Removes a planned eviction; each removed copy counts as expired_removed. */private async evict(ids: ReadonlySet<string>): Promise<void> {for (const id of ids) {@@ -1877,6 +1993,72 @@ export class GfsDownloadStore {}}+ /**+ * The periodic and startup half of the free-space floor. Admission keeps+ * the floor for its own request, but nothing else on the volume asks+ * first: state.db, shell output and file writes can take the free space+ * below the floor after the cache filled. When the volume is below the+ * floor, unpinned copies are evicted in evictionOrder (adopted first, then+ * published least recently used; an expired pinned copy counts as+ * unpinned, as in planEviction) until the copies actually removed, each+ * credited with its size in whole blocks, bring the free space to the+ * floor plus FREE_SPACE_RESTORE_HEADROOM_PERCENT of the volume, or no+ * candidate is left. The headroom keeps the next small write from putting+ * the volume below the floor again right away. Unlike+ * an admission, a partial restore is kept: any space freed helps every+ * other writer on the volume. Each removal goes through removeEntry, so+ * charges follow its rules: a copy whose removal failed stays indexed and+ * charged, and one left under a trash name frees nothing and is not+ * credited. An unmeasurable volume (statfs rejects, or two invalid+ * readings) is logged and counted as sweep_failed, and the restore is+ * skipped: the sweep that already ran keeps its result, and the next cycle+ * measures again.+ */+ private async restoreFreeSpaceFloor(now: number): Promise<void> {+ let volume: VolumeSpace | undefined+ try {+ volume = await this.readVolume()+ } catch (error) {+ this.reportFloorUnmeasured(errorCode(error))+ return+ }+ if (volume === undefined) {+ this.reportFloorUnmeasured('volume_unmeasurable')+ return+ }+ const free = volume.bavail * volume.bsize+ if (free >= freeSpaceFloor(volume)) return+ const deficit = freeSpaceRestoreTarget(volume) - free+ let freed = 0n+ let evicted = 0+ let removeFailed = 0+ for (const candidate of this.evictionCandidates(now)) {+ if (freed >= deficit) break+ const result = await this.removeEntry(candidate, 'expired_removed')+ if (result === 'removed') {+ evicted += 1+ freed += blockBytes(volume, candidate.sizeBytes)+ } else if (result === 'failed' || result === 'trashed') removeFailed += 1+ }+ logger.warn(+ {+ component: GFS_DOWNLOAD_STORE_LOG_COMPONENT,+ evicted,+ removeFailed,+ restored: freed >= deficit,+ },+ 'GFS download store found the volume below its free-space floor and evicted unpinned copies'+ )+ }++ private reportFloorUnmeasured(code: string): void {+ recordGfsDownloadExpiry('sweep_failed')+ logger.warn(+ { component: GFS_DOWNLOAD_STORE_LOG_COMPONENT, code },+ 'GFS download store could not measure the volume to restore its free-space floor; the next sweep retries it'+ )+ }+/*** Removes an indexed entry's directory, then forgets it. A failed removal* keeps the entry indexed: still charged to its caller, and a published@@ -1887,9 +2069,10 @@ export class GfsDownloadStore {private async removeEntry(entry: Entry,outcome: 'incomplete_removed' | 'expired_removed'- ): Promise<void> {+ ): Promise<RemovalResult> {const result = await this.removeDirectory(entry.directory, outcome, entry.sizeBytes)if (result !== 'failed') this.forget(entry.id)+ return result}/**@@ -1963,6 +2146,15 @@ export class GfsDownloadStore {* so whatever the swapped path led to keeps its name and content. A* failure after the rename throws TrashLeftError naming the trash path,* because the bytes are then on disk under that name.+ *+ * A shell running with the Host UID can take the write bit off the parent+ * (`chmod 0500 .gfs-downloads`); every rename out of it then fails with+ * EACCES or EPERM and the entry would stay charged forever. Those two+ * codes, on a parent that passed the check, restore the owner's rwx on that+ * parent only (restoreDirectoryOwnerAccess: opened without following a+ * symlink, other mode bits kept) and retry the rename once. Any other code,+ * a parent that is no longer a real directory, or a second failure leaves+ * the directory in place (KeptInPlaceError).*/private async removeVerified(directory: string): Promise<'removed' | 'absent'> {await this.assertRemovable(directory)@@ -1973,9 +2165,20 @@ export class GfsDownloadStore {try {await fs.rename(directory, trash)} catch (error) {- if (errorCode(error) !== 'ENOENT') throw new KeptInPlaceError(error)- await assertAbsent(directory)- return 'absent'+ const code = errorCode(error)+ if (code === 'ENOENT') {+ await assertAbsent(directory)+ return 'absent'+ }+ if (code !== 'EACCES' && code !== 'EPERM') throw new KeptInPlaceError(error)+ try {+ if (!(await restoreDirectoryOwnerAccess(path.dirname(directory))))+ throw new KeptInPlaceError(error)+ await fs.rename(directory, trash)+ } catch (retryError) {+ if (retryError instanceof KeptInPlaceError) throw retryError+ throw new KeptInPlaceError(retryError)+ }}try {await this.assertRemovable(trash)diff --git a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.tsindex 8186a4153..17c3c83bc 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreAdversarial.test.ts@@ -74,10 +74,14 @@ vi.mock('node:fs/promises', async original => {readdir: wrap('readdir', actual.readdir),realpath: wrap('realpath', actual.realpath),// Real free space; a test that sets `volume.totalBytes` sizes the volume- // (block size 1), and with it the retained budget.+ // (block size 1), and with it the retained budget. Otherwise the volume+ // is sized to its free space (freeSpaceSizedStatfs).statfs: async (target: string, options: { bigint: true }) => {+ if (volume.totalBytes === undefined) {+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')+ return freeSpaceSizedStatfs(actual.statfs)(target, options)+ }const real = await actual.statfs(target, options)- if (volume.totalBytes === undefined) return realreturn { ...real, bsize: 1n, blocks: volume.totalBytes, bavail: real.bavail * real.bsize }},}@@ -416,9 +420,10 @@ describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/})it('an eviction whose removal fails keeps the copy published and charged', async () => {- // Budget 20, so a caller protects at most 10. KEY's unpinned 10, another- // caller's pinned 5 and KEY's requested 10 make 25: the unpinned copy is- // the only candidate, and evicting it would have made room.+ // Budget 20, so protected bytes are capped at 10. KEY's unpinned 6 and 6,+ // another caller's pinned 2 and KEY's requested 8 make 22: the older+ // unpinned copy is the first candidate, and evicting it would have made+ // room.volume.totalBytes = 20nconst limited = await withEnvironment({@@ -435,15 +440,17 @@ describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/})const root = callerDirectory(hostRoot, KEY)- const { receipt, bytes } = await completedCopy(limited, root, KEY, 3, 10)+ const { receipt, bytes } = await completedCopy(limited, root, KEY, 3, 6)+ vi.setSystemTime(Date.now() + 1_000)+ await completedCopy(limited, root, KEY, 8, 6)const otherKey = `${KEY}-other`- await completedCopy(limited, callerDirectory(hostRoot, otherKey), otherKey, 7, 5, {+ await completedCopy(limited, callerDirectory(hostRoot, otherKey), otherKey, 7, 2, {owner: 'task-other',})const failedRemovals = await expiryCount('remove_failed')const renames = watch('rename', `input-${receipt.id}`, 'EIO')- await expect(startTransfer(limited, root, KEY, 4, 10)).rejects.toMatchObject({+ await expect(startTransfer(limited, root, KEY, 4, 8)).rejects.toMatchObject({code: 'host_quota_exceeded',})faults.clear()@@ -452,7 +459,7 @@ describe('GFS download store adversarial round: trash, eviction and undo (ADV-2/expect(renames).toHaveLength(1)expect(await expiryCount('remove_failed')).toBe(failedRemovals + 1)await expect(limited.readManagedFile(receipt.path, KEY)).resolves.toEqual(bytes)- await expect(limited.reusableReceipt(KEY, sourceFor(3), 10)).resolves.toMatchObject({+ await expect(limited.reusableReceipt(KEY, sourceFor(3), 6)).resolves.toMatchObject({id: receipt.id,})// A sweep does not turn it into an adopted copy either.@@ -823,7 +830,10 @@ describe('GFS download store adversarial round: a failed duplicate removal stays})it('a duplicate of a published copy whose removal fails stays charged', async () => {- // Room for the published copy and one more of its size, not for a duplicate too.+ // The pinned copy and an admission of its size take 16 protected bytes,+ // half of a 32-byte budget. A refusal cannot witness the held duplicate+ // here: with protected bytes capped below the budget, evicting unpinned+ // copies always makes room, so the charge is read directly.volume.totalBytes = 32nconst limited = await withEnvironment({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '100' },@@ -839,11 +849,11 @@ describe('GFS download store adversarial round: a failed duplicate removal staysconst owner = callerDirectory(hostRoot, OWNER)const planter = callerDirectory(hostRoot, PLANTER)// Pinned, so no admission can evict it to make room.- const { receipt, bytes } = await completedCopy(limited, owner, OWNER, 6, 16, {+ const { receipt, bytes } = await completedCopy(limited, owner, OWNER, 6, 8, {owner: 'task-erin',})// Control: the planter has capacity for one copy.- const control = await startTransfer(limited, planter, PLANTER, 7, 16)+ const control = await startTransfer(limited, planter, PLANTER, 7, 8)await limited.fail(control.transfer.id, PLANTER)plantDownload(planter, {id: receipt.id,@@ -860,14 +870,13 @@ describe('GFS download store adversarial round: a failed duplicate removal staysexpect(renames).toHaveLength(1)expect(exists(downloadDirectory(planter, receipt.id))).toBe(true)await expect(limited.readManagedFile(receipt.path, OWNER)).resolves.toEqual(bytes)- await expect(startTransfer(limited, planter, PLANTER, 8, 16)).rejects.toMatchObject({- code: 'host_quota_exceeded',- })+ expect([...heldCharges(limited).values()]).toEqual([8])faults.clear()await limited.cleanupExpired()expect(exists(downloadDirectory(planter, receipt.id))).toBe(false)- const admitted = await startTransfer(limited, planter, PLANTER, 9, 16)+ expect(heldCharges(limited).size).toBe(0)+ const admitted = await startTransfer(limited, planter, PLANTER, 9, 8)await limited.fail(admitted.transfer.id, PLANTER)await expect(limited.readManagedFile(receipt.path, OWNER)).resolves.toEqual(bytes)})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.tsindex 86bf26ee8..1dbd4e2c3 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts@@ -18,6 +18,7 @@ import {downloadDirectory,exists,expiryCount,+ freeSpaceSizedStatfs,metaFor,plantDownload,quotaCount,@@ -29,22 +30,27 @@ import { logger } from '../logger'import { GfsDownloadStore, GfsDownloadStoreError } from './gfsDownloadStore'import { GFS_FILE_LIMITS } from './gfsFilePolicy'-const { statfsBoundary, renameBoundary, openBoundary, realpathBoundary } = vi.hoisted(() => ({- statfsBoundary: vi.fn(),- renameBoundary: vi.fn(),- openBoundary: vi.fn(),- realpathBoundary: vi.fn(),-}))+const { statfsBoundary, renameBoundary, openBoundary, realpathBoundary, rmBoundary } = vi.hoisted(+ () => ({+ statfsBoundary: vi.fn(),+ renameBoundary: vi.fn(),+ openBoundary: vi.fn(),+ realpathBoundary: vi.fn(),+ rmBoundary: vi.fn(),+ })+)// Pass-through boundaries, overridden only to simulate statfs results and// failures or to act between the two renames of a publication; open is only// observed, to prove which files a lookup touched; realpath is where a path-// swap is injected right after the store's check returns.+// swap is injected right after the store's check returns; rm fails to leave+// bytes held under a trash name.vi.mock('node:fs/promises', async original => ({...(await original<typeof fs>()),statfs: statfsBoundary,rename: renameBoundary,open: openBoundary,realpath: realpathBoundary,+ rm: rmBoundary,}))const A = 'caller-a'@@ -109,6 +115,37 @@ function volumeOf(totalBytes: number): void {})}+/**+ * Leaves `sizes` charged under trash names in `caller`'s store directory: the+ * copies expire and their removal fails. Eviction cannot reclaim a held+ * charge, and protected bytes are capped at half the budget per caller and three+ * quarters Host-wide, so held charges are how a test fills the budget with bytes+ * no eviction can free.+ * The rm fault stays installed for the rest of the test.+ */+async function heldTrash(+ target: GfsDownloadStore,+ caller: string,+ firstIndex: number,+ sizes: number[]+): Promise<void> {+ const root = callerDirectory(hostRoot, caller)+ for (const [offset, size] of sizes.entries())+ await completedCopy(target, root, caller, firstIndex + offset, size)+ vi.setSystemTime(Date.now() + 3 * HOUR)+ rmBoundary.mockImplementation(async (removed: string, options: unknown) => {+ const parent = path.dirname(removed)+ if (+ path.basename(parent) === '.gfs-downloads' &&+ path.basename(path.dirname(parent)) === caller &&+ path.basename(removed).startsWith('.trash-')+ )+ throw Object.assign(new Error('injected EIO'), { code: 'EIO' })+ return nativeFs.rm(removed, options as Parameters<typeof nativeFs.rm>[1])+ })+ expect((await target.cleanupExpired()).removeFailed).toBe(sizes.length)+}+/** meta.json padded with an ignored field to exactly `bytes` bytes. */function paddedMeta(id: string, bytes: Buffer, size: number): string {const base = JSON.stringify({ ...metaFor(id, bytes, Date.now()), padding: '' })@@ -124,9 +161,16 @@ beforeEach(async () => {nativeFs = await vi.importActual<typeof fs>('node:fs/promises')vi.useFakeTimers({ toFake: ['Date'] })vi.setSystemTime(new Date('2026-10-08T10:00:00.000Z'))- for (const boundary of [statfsBoundary, renameBoundary, openBoundary, realpathBoundary])+ for (const boundary of [+ statfsBoundary,+ renameBoundary,+ openBoundary,+ realpathBoundary,+ rmBoundary,+ ])boundary.mockReset()- statfsBoundary.mockImplementation(nativeFs.statfs)+ rmBoundary.mockImplementation(nativeFs.rm)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))renameBoundary.mockImplementation(nativeFs.rename)openBoundary.mockImplementation(nativeFs.open)realpathBoundary.mockImplementation(nativeFs.realpath)@@ -306,14 +350,16 @@ describe('GFS download store boundaries: meta.json validation', () => {describe('GFS download store boundaries: quota and capacity', () => {it('Q1: retained bytes exactly at the volume budget are admitted and one byte more is refused', async () => {- // Budget 20; a caller protects at most 10, so A and B each pin half and- // the refused byte comes from C, which protects nothing yet.+ // Budget 20; protected bytes are capped at 10. caller-e holds 11 bytes+ // under trash names, A and B pin 4 and 5, and the refused byte from C+ // stays within the protected cap, so only the budget can refuse it.volumeOf(20)const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)- await completedCopy(limited, rootA, A, 310, 10, { owner: 'task-q1' })+ await heldTrash(limited, 'caller-e', 313, [6, 5])+ await completedCopy(limited, rootA, A, 310, 4, { owner: 'task-q1' })const denied = await quotaCount('host', 'storage_bytes')- const atBudget = await completedCopy(limited, rootB, B, 311, 10, { owner: 'task-q1' })+ const atBudget = await completedCopy(limited, rootB, B, 311, 5, { owner: 'task-q1' })expect(await quotaCount('host', 'storage_bytes')).toBe(denied)await expect(limited.readManagedFile(atBudget.receipt.path, B)).resolves.toEqual(atBudget.bytes)@@ -326,24 +372,20 @@ describe('GFS download store boundaries: quota and capacity', () => {it('Q2: there is no retained-file limit: one caller keeps 65 copies and only bytes refuse', async () => {// 65 one-byte copies: more than the removed 8-per-caller and 64-per-Host limits.- // A caller protects at most half the budget, so the budget is 132: A pins- // 65 bytes and its 66th byte stays within its 66, while B pins 60 (six- // 10-byte copies, the file limit) and C pins the remaining 7.+ // A caller's protected bytes are capped at half the budget, so the budget is 132: A+ // pins 65 bytes and its 66th byte stays within the 66, while B holds 67+ // bytes (six 10-byte copies, the file limit, and 7) under trash names.const files = 65volumeOf(132)const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)+ await heldTrash(limited, B, 500, [10, 10, 10, 10, 10, 10, 7])const denied = await quotaCount('host', 'storage_bytes')for (let index = 0; index < files; index += 1)await completedCopy(limited, rootA, A, 400 + index, 1, { owner: 'task-q2' })- for (let index = 0; index < 6; index += 1)- await completedCopy(limited, rootB, B, 500 + index, 10, { owner: 'task-q2' })- const rootC = callerDirectory(hostRoot, 'caller-c')- await completedCopy(limited, rootC, 'caller-c', 506, 7, { owner: 'task-q2' })expect((await limited.debugInventory()).byCaller.get(A)).toEqual({ files, bytes: files })- expect((await limited.debugInventory()).bytes).toBe(132)expect(await quotaCount('host', 'storage_bytes')).toBe(denied)- // Every copy is pinned, so the byte budget is the only thing that can refuse.+ // Every copy is pinned or held, so the byte budget is the only thing that can refuse.await expect(startTransfer(limited, rootA, A, 400 + files, 1)).rejects.toMatchObject({code: 'host_quota_exceeded',})@@ -353,16 +395,18 @@ describe('GFS download store boundaries: quota and capacity', () => {}, 60_000)it('Q3: Host bytes exactly at the budget are admitted and one byte more is refused', async () => {+ // Budget 30; protected bytes are capped at 15. caller-e holds 16 bytes+ // under trash names and B, C and A pin 14 between them.volumeOf(30)const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)+ await heldTrash(limited, 'caller-e', 320, [8, 8])const rootC = callerDirectory(hostRoot, 'caller-c')- await completedCopy(limited, rootB, B, 316, 10, { owner: 'task-b' })- await completedCopy(limited, rootC, 'caller-c', 317, 10, { owner: 'task-c' })+ await completedCopy(limited, rootB, B, 316, 5, { owner: 'task-b' })+ await completedCopy(limited, rootC, 'caller-c', 317, 4, { owner: 'task-c' })const denied = await quotaCount('host', 'storage_bytes')- await completedCopy(limited, rootA, A, 318, 10, { owner: 'task-a' })+ await completedCopy(limited, rootA, A, 318, 5, { owner: 'task-a' })expect(await quotaCount('host', 'storage_bytes')).toBe(denied)- expect((await limited.debugInventory()).bytes).toBe(30)const rootD = callerDirectory(hostRoot, 'caller-d')await expect(startTransfer(limited, rootD, 'caller-d', 319, 1)).rejects.toMatchObject({@@ -374,8 +418,11 @@ describe('GFS download store boundaries: quota and capacity', () => {it('Q5: free space exactly at the threshold is admitted and one byte less is refused', async () => {const observed = await nativeFs.statfs(hostRoot, { bigint: true })const size = 100- const threshold = BigInt(size) + 16n * 1024n * 1024n- statfsBoundary.mockResolvedValue({ ...observed, bsize: 1n, bavail: threshold - 1n })+ // A 1 GiB volume (block size 1): the free-space floor is+ // floor(2^30 * 15 / 100) = 161061273 bytes, above its 16 MiB minimum.+ const blocks = 2n ** 30n+ const threshold = BigInt(size) + 161_061_273n+ statfsBoundary.mockResolvedValue({ ...observed, bsize: 1n, blocks, bavail: threshold - 1n })const denied = await quotaCount('host', 'free_space')await expect(startTransfer(store, rootA, A, 320, size)).rejects.toMatchObject({@@ -383,16 +430,18 @@ describe('GFS download store boundaries: quota and capacity', () => {})expect(await quotaCount('host', 'free_space')).toBe(denied + 1)- statfsBoundary.mockResolvedValue({ ...observed, bsize: 1n, bavail: threshold })+ statfsBoundary.mockResolvedValue({ ...observed, bsize: 1n, blocks, bavail: threshold })await expect(startTransfer(store, rootA, A, 320, size)).resolves.toBeDefined()expect(await quotaCount('host', 'free_space')).toBe(denied + 1)})it('Q6: a statfs failure refuses that admission loudly and the next one succeeds', async () => {statfsBoundary.mockRejectedValueOnce(Object.assign(new Error('injected EIO'), { code: 'EIO' }))+ // Startup already measured the volume once to restore the free-space floor.+ const measured = statfsBoundary.mock.calls.lengthawait expect(startTransfer(store, rootA, A, 321, 4)).rejects.toMatchObject({ code: 'EIO' })- expect(statfsBoundary).toHaveBeenCalledTimes(1)+ expect(statfsBoundary).toHaveBeenCalledTimes(measured + 1)// The caller's single concurrency slot was not kept by the failed admission.const next = await completedCopy(store, rootA, A, 321, 4)@@ -467,47 +516,65 @@ describe('GFS download store boundaries: quota and capacity', () => {expect(exists(downloadDirectory(rootA, older.receipt.id))).toBe(true)})- it('Q12: when every copy is pinned admission fails with the quota code and succeeds after release', async () => {- // Budget 15; a caller protects at most 7. B and C each pin 7, and A's 7- // would bring the Host to 21: only the host budget can refuse it.+ it('Q12: when pins fill the Host protected cap admission fails with the quota code and succeeds after release', async () => {+ // Budget 15; protected bytes are capped at 7 per caller and 11 Host-wide.+ // Two loose copies and the pins of B (6) and C (5) fill the budget, and+ // A's 1 byte would bring the protected bytes to 12: the Host-wide cap+ // refuses it before any eviction. The loose copies come first: an open+ // reservation counts as protected too.volumeOf(15)const limited = await limitedStore(SMALL_FILES_FULL_VOLUME)- const pinned = await completedCopy(limited, rootB, B, 334, 7, { owner: 'task-q12' })+ const rootD = callerDirectory(hostRoot, 'caller-d')+ const loose = await completedCopy(limited, rootD, 'caller-d', 337, 2)+ tick()+ const looseNewer = await completedCopy(limited, rootD, 'caller-d', 338, 2)+ tick()+ const pinned = await completedCopy(limited, rootB, B, 334, 6, { owner: 'task-q12' })+ tick()const rootC = callerDirectory(hostRoot, 'caller-c')- await completedCopy(limited, rootC, 'caller-c', 336, 7, { owner: 'task-q12' })+ await completedCopy(limited, rootC, 'caller-c', 336, 5, { owner: 'task-q12' })const directory = downloadDirectory(rootB, pinned.receipt.id)- const denied = await quotaCount('host', 'storage_bytes')+ const denied = await quotaCount('host', 'protected_bytes')- await expect(startTransfer(limited, rootA, A, 335, 7)).rejects.toMatchObject({+ await expect(startTransfer(limited, rootA, A, 335, 1)).rejects.toMatchObject({code: 'host_quota_exceeded',})- expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)- expect(exists(directory)).toBe(true)+ expect(await quotaCount('host', 'protected_bytes')).toBe(denied + 1)+ // Nothing was evicted for the refused admission.+ expect(exists(downloadDirectory(rootD, loose.receipt.id))).toBe(true)+ expect(exists(downloadDirectory(rootD, looseNewer.receipt.id))).toBe(true)await limited.releaseReceiptOwner('task-q12', B)- await expect(startTransfer(limited, rootA, A, 335, 7)).resolves.toBeDefined()- expect(exists(directory)).toBe(false)- expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)+ await expect(startTransfer(limited, rootA, A, 335, 1)).resolves.toBeDefined()+ // Witness: the admitted byte needed one eviction, and it took the oldest+ // loose copy.+ expect(exists(downloadDirectory(rootD, loose.receipt.id))).toBe(false)+ expect(exists(downloadDirectory(rootD, looseNewer.receipt.id))).toBe(true)+ expect(exists(directory)).toBe(true)+ expect(await quotaCount('host', 'protected_bytes')).toBe(denied + 1)})})describe('GFS download store boundaries: expiry', () => {async function expiringCopy(index: number) {- const expiresAtMs = Date.now() + HOUR+ const expiresAtMs = Date.now() + 2 * HOURconst copy = await completedCopy(store, rootA, A, index, 8, { expiresAt: at(expiresAtMs) })return { ...copy, expiresAtMs, directory: downloadDirectory(rootA, copy.receipt.id) }}- it('E1: reuse matches until one millisecond before expiry', async () => {+ it('E1: reuse matches until one millisecond before the copy has one hour left', async () => {const copy = await expiringCopy(340)- vi.setSystemTime(copy.expiresAtMs - 1)+ vi.setSystemTime(copy.expiresAtMs - HOUR - 1)await expect(store.reusableReceipt(A, sourceFor(340), 8)).resolves.toMatchObject({id: copy.receipt.id,})- vi.setSystemTime(copy.expiresAtMs)- await expect(store.reusableReceipt(A, sourceFor(340), 8)).resolves.toBeUndefined()- vi.setSystemTime(copy.expiresAtMs + 1)- await expect(store.reusableReceipt(A, sourceFor(340), 8)).resolves.toBeUndefined()+ for (const instant of [copy.expiresAtMs - HOUR, copy.expiresAtMs - 1, copy.expiresAtMs]) {+ vi.setSystemTime(instant)+ await expect(store.reusableReceipt(A, sourceFor(340), 8)).resolves.toBeUndefined()+ }+ // Witness: a miss inside the last hour leaves the copy readable until it expires.+ vi.setSystemTime(copy.expiresAtMs - 1)+ await expect(store.readManagedFile(copy.receipt.path, A)).resolves.toEqual(copy.bytes)})it('E2: a managed read succeeds until one millisecond before expiry', async () => {@@ -831,6 +898,36 @@ describe('GFS download store boundaries: caller isolation', () => {})+ it.each(Object.keys(reads) as Array<keyof typeof reads>)(+ 'U1b: %s refuses an adopted copy recorded for the same caller from the index, before touching its directory',+ async api => {+ // Planted with this caller's identity, so only the provenance check in+ // the index lookup can refuse it before the disk is reached.+ const planted = plantComplete(rootA, A, 72, Buffer.alloc(8, 7))+ await store.cleanupExpired()+ const published = await completedCopy(store, rootA, A, 73, 8)+ openBoundary.mockClear()+ realpathBoundary.mockClear()++ await expect(reads[api](store, receiptPathOf(planted.id), A)).rejects.toMatchObject({+ code: 'download_missing',+ })++ expect(opensUnder(planted.id)).toBe(0)+ expect(realpathBoundary).not.toHaveBeenCalled()+ // Witness: the planted copy is indexed as adopted for this caller, and+ // the published copy read through the same API opens its source.+ const index = (store as unknown as { entries: Map<string, Record<string, unknown>> }).entries+ expect(index.get(planted.id)).toMatchObject({ provenance: 'adopted', callerIdentity: A })+ const owned = await reads[api](store, published.receipt.path, A)+ expect(owned).toEqual(+ api === 'readManagedFile' ? published.bytes : published.bytes.subarray(0, 4)+ )+ expect(opensUnder(published.receipt.id)).toBeGreaterThan(0)+ expect(realpathBoundary).toHaveBeenCalled()+ }+ )+it('U2: an expired copy of another caller is still answered as nonexistent, not as expired', async () => {const { receipt } = await completedCopy(store, rootA, A, 71, 8)vi.setSystemTime(Date.parse(receipt.expiresAt))diff --git a/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.tsindex 841a60fb1..dec0bf09b 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreBudget.test.ts@@ -2,8 +2,9 @@* The retained-storage budget of the GFS download store (#1028, Addendum 8):* `floor(volumeTotalBytes * percent / 100)`, with the volume measured by* statfs at every admission. There is no per-caller or file-count limit;- * eviction order and the free-space check are unchanged. statfs is the only- * boundary replaced: it reports the real free space and a chosen volume size.+ * eviction order and the free-space check are unchanged. statfs reports the+ * real free space and a chosen volume size; rm passes through unless a test+ * leaves bytes held under a trash name.*/import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { hash } from 'node:crypto'@@ -16,6 +17,7 @@ import {completedCopy,downloadDirectory,exists,+ freeSpaceSizedStatfs,metaFor,plantDownload,quotaCount,@@ -27,10 +29,14 @@ import { logger } from '../logger'import { GfsDownloadStore, GfsDownloadStoreError } from './gfsDownloadStore'import { REMOVED_GFS_STORAGE_VARIABLES } from './gfsFilePolicy'-const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))+const { statfsBoundary, rmBoundary } = vi.hoisted(() => ({+ statfsBoundary: vi.fn(),+ rmBoundary: vi.fn(),+}))vi.mock('node:fs/promises', async original => ({...(await original<typeof fs>()),statfs: statfsBoundary,+ rm: rmBoundary,}))const A = 'caller-a'@@ -77,12 +83,45 @@ function tick(): void {vi.setSystemTime(Date.now() + 1_000)}+/**+ * Leaves `sizes` charged under trash names in `caller`'s store directory: the+ * copies expire and their removal fails. Eviction cannot reclaim a held+ * charge, and protected bytes are capped at half the budget per caller and three+ * quarters Host-wide, so held charges are how a test fills the budget with bytes+ * no eviction can free.+ * The rm fault stays installed for the rest of the test.+ */+async function heldTrash(+ target: GfsDownloadStore,+ caller: string,+ firstIndex: number,+ sizes: number[]+): Promise<void> {+ const root = callerDirectory(hostRoot, caller)+ for (const [offset, size] of sizes.entries())+ await completedCopy(target, root, caller, firstIndex + offset, size)+ vi.setSystemTime(Date.now() + 3 * 60 * 60_000)+ rmBoundary.mockImplementation(async (removed: string, options: unknown) => {+ const parent = path.dirname(removed)+ if (+ path.basename(parent) === '.gfs-downloads' &&+ path.basename(path.dirname(parent)) === caller &&+ path.basename(removed).startsWith('.trash-')+ )+ throw Object.assign(new Error('injected EIO'), { code: 'EIO' })+ return nativeFs.rm(removed, options as Parameters<typeof nativeFs.rm>[1])+ })+ expect((await target.cleanupExpired()).removeFailed).toBe(sizes.length)+}+beforeEach(async () => {nativeFs = await vi.importActual<typeof fs>('node:fs/promises')vi.useFakeTimers({ toFake: ['Date'] })vi.setSystemTime(new Date('2026-10-08T10:00:00.000Z'))statfsBoundary.mockReset()- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))+ rmBoundary.mockReset()+ rmBoundary.mockImplementation(nativeFs.rm)hostRoot = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-budget-'))rootA = callerDirectory(hostRoot, A)rootB = callerDirectory(hostRoot, B)@@ -97,19 +136,21 @@ afterEach(async () => {})describe('GFS download store budget: share of the workspace volume', () => {- it('BUD-1: the default 85% budget admits exactly up to floor(total * 85 / 100) bytes', async () => {- // 101 bytes of volume: 85.85 floors to 85. A caller may protect at most- // floor(85 / 2) = 42 bytes, so the pinned 85 are spread over three callers:- // A 40 + B 40 + C 5. The refused 1-byte request leaves A at 41, under its half.+ it('BUD-1: the default 70% budget admits exactly up to floor(total * 70 / 100) bytes', async () => {+ // 101 bytes of volume: 70.7 floors to 70, and protected bytes are capped+ // at floor(70 / 2) = 35 per caller and floor(70 * 3 / 4) = 52 Host-wide.+ // caller-e holds 36 bytes under trash names, A pins 20 and C 14; the+ // refused 1-byte request keeps A at 21 and the Host at 35, so only the+ // budget can refuse it.volumeOf(101n)const store = await openStore()const C = 'caller-c'const rootC = callerDirectory(hostRoot, C)- await completedCopy(store, rootA, A, 1, 40, { owner: 'task-a' })- await completedCopy(store, rootB, B, 4, 40, { owner: 'task-b' })+ await heldTrash(store, 'caller-e', 5, [18, 18])+ await completedCopy(store, rootA, A, 1, 20, { owner: 'task-a' })const denied = await quotaCount('host', 'storage_bytes')- const atBudget = await completedCopy(store, rootC, C, 2, 5, { owner: 'task-c' })+ const atBudget = await completedCopy(store, rootC, C, 2, 14, { owner: 'task-c' })expect(await quotaCount('host', 'storage_bytes')).toBe(denied)await expect(store.readManagedFile(atBudget.receipt.path, C)).resolves.toEqual(atBudget.bytes)@@ -129,13 +170,16 @@ describe('GFS download store budget: share of the workspace volume', () => {async ({ percent, blocks, budget }) => {volumeOf(blocks)const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: percent })- const denied = await quotaCount('host', 'storage_bytes')- // A caller may protect at most half the budget, so the pinned budget is- // two halves held by A and B; the refused byte comes from a third caller- // that protects nothing yet, so only the host budget can refuse it.- await completedCopy(store, rootA, A, 10, budget / 2, { owner: 'task-a' })- await completedCopy(store, rootB, B, 12, budget - budget / 2, { owner: 'task-b' })+ // Protected bytes are capped at 5 per caller and 7 Host-wide: caller-e+ // holds 6 bytes under trash names, A and B pin 2 each, and the refused+ // byte from a third caller keeps the protected bytes at 5, so only the+ // host budget can refuse it.+ expect(budget).toBe(10)+ await heldTrash(store, 'caller-e', 13, [3, 3])+ const denied = await quotaCount('host', 'storage_bytes')+ await completedCopy(store, rootA, A, 10, 2, { owner: 'task-a' })+ await completedCopy(store, rootB, B, 12, 2, { owner: 'task-b' })expect(await quotaCount('host', 'storage_bytes')).toBe(denied)const C = 'caller-c'await expect(@@ -148,32 +192,39 @@ describe('GFS download store budget: share of the workspace volume', () => {)it('BUD-3: a volume resized between two admissions is used by the second, without restart', async () => {- // Budget 20, so each caller protects at most 10: 15 bytes pinned by A (10)- // and C (5), then B asks for 10. 15 + 10 > 20 is refused by the host budget;- // after the resize 15 + 10 fits in 25, and B's 10 stays under floor(25 / 2).+ // Budget 20, so protected bytes are capped at 10: caller-e holds 11 bytes+ // under trash names and A pins 3, then B asks for 7. 14 + 7 > 20 is+ // refused by the host budget; after the resize 14 + 7 fits in 25.volumeOf(20n)const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '100' })- await completedCopy(store, rootA, A, 20, 10, { owner: 'task-a' })- const C = 'caller-c'- await completedCopy(store, callerDirectory(hostRoot, C), C, 22, 5, { owner: 'task-c' })+ await heldTrash(store, 'caller-e', 23, [6, 5])+ await completedCopy(store, rootA, A, 20, 3, { owner: 'task-a' })const denied = await quotaCount('host', 'storage_bytes')- await expect(startTransfer(store, rootB, B, 21, 10)).rejects.toMatchObject({+ await expect(startTransfer(store, rootB, B, 21, 7)).rejects.toMatchObject({code: 'host_quota_exceeded',})expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)volumeOf(25n)statfsBoundary.mockClear()- const admitted = await startTransfer(store, rootB, B, 21, 10)- expect(admitted.transfer.sizeBytes).toBe(10)+ const admitted = await startTransfer(store, rootB, B, 21, 7)+ expect(admitted.transfer.sizeBytes).toBe(7)// Witness: the new size came from a statfs taken by this admission.expect(statfsBoundary).toHaveBeenCalled()})it('BUD-4: a large volume is sized in bigint without rounding', async () => {- // 2^53 + 2 blocks of 4096 bytes is far beyond Number precision.- volumeOf(2n ** 53n + 2n, 4096n)+ // 2^53 + 2 blocks of 4096 bytes is far beyond Number precision. The+ // whole volume is free: its free-space floor (15% of it) is far more+ // than the real free space volumeOf would report.+ const blocks = 2n ** 53n + 2n+ statfsBoundary.mockImplementation(async (target: string) => ({+ ...(await nativeFs.statfs(target, { bigint: true })),+ bsize: 4096n,+ blocks,+ bavail: blocks,+ }))const store = await openStore()const admitted = await startTransfer(store, rootA, A, 30, 1024)expect(admitted.transfer.sizeBytes).toBe(1024)@@ -201,7 +252,7 @@ describe('GFS download store budget: share of the workspace volume', () => {expect(await quotaCount('host', 'free_space')).toBe(denied + 1)// Witness: the real statfs admits the same request.- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))await expect(startTransfer(store, rootA, A, 31, 4)).resolves.toBeDefined()})@@ -209,14 +260,16 @@ describe('GFS download store budget: share of the workspace volume', () => {describe('GFS download store budget: a full disk and a full cache are distinct refusals', () => {it('DSK-1: a full disk refuses as disk_full while a full budget still refuses as host_quota_exceeded', async () => {- // Budget floor(20 * 85 / 100) = 17; A and B pin 8 each, so 2 more bytes+ // Budget floor(20 * 70 / 100) = 14, protected bytes capped at 7. caller-e+ // holds 9 bytes under trash names and A and B pin 2 each, so 2 more bytes// exceed the budget while 1 more fits.volumeOf(20n)const store = await openStore()const C = 'caller-c'const rootC = callerDirectory(hostRoot, C)- await completedCopy(store, rootA, A, 40, 8, { owner: 'task-a' })- await completedCopy(store, rootB, B, 41, 8, { owner: 'task-b' })+ await heldTrash(store, 'caller-e', 44, [5, 4])+ await completedCopy(store, rootA, A, 40, 2, { owner: 'task-a' })+ await completedCopy(store, rootB, B, 41, 2, { owner: 'task-b' })const budgetDenied = await quotaCount('host', 'storage_bytes')const diskDenied = await quotaCount('host', 'free_space')@@ -226,7 +279,8 @@ describe('GFS download store budget: a full disk and a full cache are distinct rexpect(await quotaCount('host', 'storage_bytes')).toBe(budgetDenied + 1)expect(await quotaCount('host', 'free_space')).toBe(diskDenied)- // The same volume with one byte less than the 16 MiB margin plus the request.+ // The same volume with one byte less than the free-space floor (16 MiB,+ // its minimum on a volume this small) plus the request.statfsBoundary.mockImplementation(async (target: string) => ({...(await nativeFs.statfs(target, { bigint: true })),bsize: 1n,@@ -261,7 +315,7 @@ describe('GFS download store budget: a full disk and a full cache are distinct rexpect(await quotaCount('host', 'storage_bytes')).toBe(budgetDenied)// Witness: the real statfs admits the same request.- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))await expect(startTransfer(store, rootA, A, 44, 4)).resolves.toBeDefined()})@@ -275,7 +329,7 @@ describe('GFS download store budget: a full disk and a full cache are distinct rasync ({ fields }) => {const store = await openStore()const observed = await nativeFs.statfs(hostRoot, { bigint: true })- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))statfsBoundary.mockResolvedValueOnce({ ...observed, ...fields })statfsBoundary.mockClear()const denied = await quotaCount('host', 'free_space')@@ -367,13 +421,13 @@ describe('GFS download store budget: no per-caller storage or file-count limit',it("BUD-7: another caller's admission never evicts a pinned copy and takes an adopted one first", async () => {volumeOf(30n)const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '100' })- const pinned = await completedCopy(store, rootA, A, 40, 10, { owner: 'task-a' })+ const pinned = await completedCopy(store, rootA, A, 40, 4, { owner: 'task-a' })tick()- // A protects at most floor(30 / 2) = 15: the pinned 10 plus this 5-byte- // reservation while it is written.- const unpinned = await completedCopy(store, rootA, A, 41, 5)+ // A's protected bytes are capped at floor(30 / 2) = 15: the pinned 4 plus+ // this 10-byte reservation while it is written.+ const unpinned = await completedCopy(store, rootA, A, 41, 10)tick()- const bytes = Buffer.alloc(10, 0x42)+ const bytes = Buffer.alloc(11, 0x42)const plantedId = '00000000-0000-4000-8000-000000000042'const planted = plantDownload(rootA, {id: plantedId,@@ -382,27 +436,28 @@ describe('GFS download store budget: no per-caller storage or file-count limit',})tick()- // 25 retained + 10 requested against 30: one copy goes, and it is the adopted one.- await startTransfer(store, rootB, B, 42, 10)+ // 25 retained + 6 requested against 30: one copy goes, and it is the adopted one.+ await completedCopy(store, rootB, B, 42, 6)+ tick()expect(exists(planted.directory)).toBe(false)expect(exists(downloadDirectory(rootA, unpinned.receipt.id))).toBe(true)expect(exists(downloadDirectory(rootA, pinned.receipt.id))).toBe(true)- // Next, 15 retained + B's 10 reserved + 10 requested against 30: the- // unpinned published copy goes; the pinned one, older, never does.+ // Next, 20 retained + 11 requested against 30: the least recently used+ // unpinned copy goes; the pinned one, older, never does.const second = await startTransfer(store,callerDirectory(hostRoot, 'caller-c'),'caller-c',43,- 10+ 11)- expect(second.transfer.sizeBytes).toBe(10)+ expect(second.transfer.sizeBytes).toBe(11)expect(exists(downloadDirectory(rootA, unpinned.receipt.id))).toBe(false)expect(exists(downloadDirectory(rootA, pinned.receipt.id))).toBe(true)})- it('BUD-8: the quota denial is identical whoever fills the budget', async () => {+ it('BUD-8: the quota denial is identical whoever holds the pinned bytes', async () => {const denialFor = async (fillOwnRoot: boolean) => {const host = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-budget-denial-'))try {@@ -410,30 +465,31 @@ describe('GFS download store budget: no per-caller storage or file-count limit',const other = callerDirectory(host, `caller-${'b'.repeat(12)}`)const third = callerDirectory(host, `caller-${'c'.repeat(12)}`)const store = await openStore(host)- // 80 pinned bytes either way; no caller protects more than floor(85 / 2) = 42.+ // 45 pinned bytes either way, under the Host-wide cap of+ // floor(70 * 3 / 4) = 52 protected bytes, and each caller under its+ // own cap of floor(70 / 2) = 35.const fillers: Array<[string, number]> = fillOwnRoot? [- [own, 30],- [other, 40],- [third, 10],+ [own, 10],+ [other, 20],+ [third, 15],]: [- [other, 40],- [third, 40],+ [other, 25],+ [third, 20],]for (const [index, [filler, sizeBytes]] of fillers.entries())await completedCopy(store, filler, path.basename(filler), 50 + index * 2, sizeBytes, {owner: 'task-fill',})- const denied = await quotaCount('host', 'storage_bytes')+ const denied = await quotaCount('host', 'protected_bytes')const error = await startTransfer(store, own, A, 51, 10).then(() => undefined,(rejection: unknown) => rejection)- // The refusal came from the host budget: A stays under its own half- // (at most 30 + 10 = 40 of 42).- expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)- // Witness: the refusal was the budget; releasing the pins admits the same request.+ // The refusal came from the Host-wide protected cap: 45 + 10 > 52.+ expect(await quotaCount('host', 'protected_bytes')).toBe(denied + 1)+ // Witness: releasing the pins admits the same request.for (const [filler] of fillers)await store.releaseReceiptOwner('task-fill', path.basename(filler))await expect(startTransfer(store, own, A, 51, 10)).resolves.toBeDefined()@@ -442,7 +498,7 @@ describe('GFS download store budget: no per-caller storage or file-count limit',await nativeFs.rm(host, { recursive: true, force: true })}}- // 100 bytes of volume, default 85%: 80 retained + 10 requested is over.+ // 100 bytes of volume, default 70%: a budget of 70 bytes.volumeOf(100n)const ownFill = await denialFor(true)const foreignFill = await denialFor(false)@@ -526,16 +582,16 @@ describe('GFS download store budget: removed variables', () => {it('BUD-9c: removed variables set to 1 byte and 1 file do not limit retained storage', async () => {// Set for the whole test and the store re-imported with them, so a limit- // read at module load or at admission would apply. Two 40-byte copies for- // one caller exceed both old limits; the 85-byte budget of a 101-byte- // volume admits them.+ // read at module load or at admission would apply. Two 30-byte copies for+ // one caller exceed both old limits; the 70-byte budget of a 101-byte+ // volume admits them, each within the 35 protected bytes it allows.const removed = Object.fromEntries(REMOVED_GFS_STORAGE_VARIABLES.map(name => [name, '1']))for (const [name, value] of Object.entries(removed)) vi.stubEnv(name, value)volumeOf(101n)const store = await limitedStore(removed)- const first = await completedCopy(store, rootA, A, 1, 40)+ const first = await completedCopy(store, rootA, A, 1, 30)tick()- const second = await completedCopy(store, rootA, A, 2, 40)+ const second = await completedCopy(store, rootA, A, 2, 30)await expect(store.readManagedFile(first.receipt.path, A)).resolves.toEqual(first.bytes)await expect(store.readManagedFile(second.receipt.path, A)).resolves.toEqual(second.bytes)diff --git a/mcp-host/src/internalTools/gfsDownloadStoreDiskPressure.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreDiskPressure.test.tsindex 8eedec41d..a49ff4475 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreDiskPressure.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreDiskPressure.test.ts@@ -2,7 +2,8 @@* Physical free space versus cached copies (PR #1028 review R1-H1, R1-L1).** With the budget at 85% of the volume, the cache plus foreign data can leave- * less free space than a request and its 16 MiB margin while the cache is+ * less free space than a request and the free-space floor (16 MiB, its+ * minimum, on the 40 MiB volume used here) while the cache is* still under budget. Unpinned, unreserved copies are evicted to cover that* deficit, planned as a whole before any delete; pinned copies never are.* statfs is the only boundary replaced: it reports a simulated volume whose@@ -127,7 +128,7 @@ describe('GFS download store: disk pressure evicts unpinned copies', () => {it('DP-1: a full disk under budget evicts the least recently used unpinned copies of another caller instead of refusing as disk_full', async () => {// 40 MiB volume, 10 MiB foreign, budget floor(40 MiB * 85%) = 34 MiB.// Ten 1 MiB copies leave 20 MiB (minus meta.json bytes) free; a 5 MiB- // download needs 5 MiB + the 16 MiB margin = 21 MiB, while the cache+ // download needs 5 MiB + the 16 MiB floor = 21 MiB, while the cache// (10 MiB + 5 MiB) is far under budget.simulatedVolume(40 * MIB, 10 * MIB)const store = await openStore()@@ -187,7 +188,7 @@ describe('GFS download store: disk pressure evicts unpinned copies', () => {const ids = await fillWithCopiesOfB(store)const evicted = await expiryCount('expired_removed')- // 15 MiB + 16 MiB margin = 31 MiB; at most 20 + 10 = 30 MiB can be free.+ // 15 MiB + 16 MiB floor = 31 MiB; at most 20 + 10 = 30 MiB can be free.await expect(startTransfer(store, rootA, A, 603, 15 * MIB)).rejects.toMatchObject({code: 'disk_full',})@@ -201,17 +202,17 @@ describe('GFS download store: disk pressure evicts unpinned copies', () => {describe('GFS download store: eviction metric', () => {it('EVM-1: a budget eviction increments expired_removed and removes the evicted directory', async () => {- // 100-byte volume, budget 85: 40 + 40 retained + 10 requested is over.+ // 100-byte volume, budget 70: 30 + 30 retained + 15 requested is over.volumeOf(100)const store = await openStore()- const oldest = await completedCopy(store, rootB, B, 700, 40)+ const oldest = await completedCopy(store, rootB, B, 700, 30)tick()- const newer = await completedCopy(store, rootA, A, 701, 40)+ const newer = await completedCopy(store, rootA, A, 701, 30)tick()const evicted = await expiryCount('expired_removed')const removed = await expiryCount('incomplete_removed')- await startTransfer(store, rootC, C, 702, 10)+ await startTransfer(store, rootC, C, 702, 15)// Witness: the eviction happened, and only the least recently used copy went.expect(exists(downloadDirectory(rootB, oldest.receipt.id))).toBe(false)diff --git a/mcp-host/src/internalTools/gfsDownloadStoreFreeSpaceFloor.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreFreeSpaceFloor.test.tsnew file mode 100644index 000000000..fa510fc4e--- /dev/null+++ b/mcp-host/src/internalTools/gfsDownloadStoreFreeSpaceFloor.test.ts@@ -0,0 +1,402 @@+/**+ * The free-space floor (PR #1028 review round 2, A1-A3).+ *+ * The store keeps max(15% of the volume, 16 MiB) free. Admission refuses as+ * disk_full when evicting unpinned copies cannot cover its request plus the+ * floor, and startup and the periodic cleanupExpired evict unpinned copies+ * when something else on the volume took the free space below the floor,+ * until the floor plus 5% of the volume is free. statfs is the only+ * boundary replaced: it reports a simulated 200 MiB volume (floor 30 MiB)+ * whose free space is its size minus `foreign` bytes the store does not own+ * minus the bytes of every regular file under the Host root, so a removal+ * frees space exactly as on a disk.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { randomUUID } from 'node:crypto'+import * as syncFs from 'node:fs'+import * as fs from 'node:fs/promises'+import { tmpdir } from 'node:os'+import * as path from 'node:path'+import {+ callerDirectory,+ completedCopy,+ downloadDirectory,+ exists,+ expiryCount,+ metaFor,+ plantDownload,+ quotaCount,+ sourceFor,+ startTransfer,+} from '../__tests__/fixtures/gfsStoreTestKit'+import { logger } from '../logger'+import { GfsDownloadStore, freeSpaceFloor, freeSpaceRestoreTarget } from './gfsDownloadStore'++const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))+vi.mock('node:fs/promises', async original => ({+ ...(await original<typeof fs>()),+ statfs: statfsBoundary,+}))++const A = 'caller-a'+const B = 'caller-b'+const C = 'caller-c'+const MIB = 1024 * 1024+const HOUR = 60 * 60_000+const TOTAL = 200 * MIB+/** 15% of the 200 MiB volume: 31 457 280 bytes. */+const FLOOR = 30 * MIB+const FLOOR_WARNING =+ 'GFS download store found the volume below its free-space floor and evicted unpinned copies'++let nativeFs: typeof fs+let hostRoot: string+let rootA: string+let rootB: string+let rootC: string+let foreign = 0+const stores: GfsDownloadStore[] = []++function regularBytes(directory: string): number {+ let sum = 0+ for (const child of syncFs.readdirSync(directory, { withFileTypes: true })) {+ const target = path.join(directory, child.name)+ if (child.isDirectory()) sum += regularBytes(target)+ else if (child.isFile()) sum += syncFs.lstatSync(target).size+ }+ return sum+}++function simulatedVolume(): void {+ statfsBoundary.mockImplementation(async (target: string) => {+ const real = await nativeFs.statfs(target, { bigint: true })+ const free = BigInt(TOTAL - foreign - regularBytes(hostRoot))+ return { ...real, bsize: 1n, blocks: BigInt(TOTAL), bavail: free < 0n ? 0n : free }+ })+}++/** Sets the foreign data so the volume has exactly `freeBytes` free now. */+function leaveFree(freeBytes: number): void {+ foreign = TOTAL - freeBytes - regularBytes(hostRoot)+}++async function openStore(): Promise<GfsDownloadStore> {+ const opened = new GfsDownloadStore(hostRoot)+ stores.push(opened)+ await opened.initialize()+ return opened+}++/** Distinct createdAt and last-use values so the eviction order is deterministic. */+function tick(): void {+ vi.setSystemTime(Date.now() + 1_000)+}++function provenanceOf(store: GfsDownloadStore, id: string): string | undefined {+ return (store as unknown as { entries: Map<string, { provenance: string }> }).entries.get(id)+ ?.provenance+}++function floorWarnings(warn: { mock: { calls: unknown[][] } }): unknown[] {+ return warn.mock.calls.filter(call => call[1] === FLOOR_WARNING).map(call => call[0])+}++/** Splits `named` directories into the ones removed and the ones still on disk. */+function survey(named: Record<string, string>): { removed: string[]; kept: string[] } {+ const removed: string[] = []+ const kept: string[] = []+ for (const [name, directory] of Object.entries(named))+ (exists(directory) ? kept : removed).push(name)+ return { removed: removed.sort(), kept: kept.sort() }+}++beforeEach(async () => {+ nativeFs = await vi.importActual<typeof fs>('node:fs/promises')+ vi.useFakeTimers({ toFake: ['Date'] })+ vi.setSystemTime(new Date('2026-10-09T10:00:00.000Z'))+ statfsBoundary.mockReset()+ foreign = 0+ simulatedVolume()+ hostRoot = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-free-space-floor-'))+ rootA = callerDirectory(hostRoot, A)+ rootB = callerDirectory(hostRoot, B)+ rootC = callerDirectory(hostRoot, C)+})++afterEach(async () => {+ vi.restoreAllMocks()+ vi.useRealTimers()+ for (const opened of stores.splice(0)) await opened.close(0)+ await nativeFs.rm(hostRoot, { recursive: true, force: true })+})++describe('GFS download store: free-space floor value', () => {+ it('FSF-1: the floor is 15% of the volume, and never less than 16 MiB', () => {+ // 10 GiB in 4 KiB blocks.+ expect(freeSpaceFloor({ bsize: 4096n, blocks: 2_621_440n })).toBe(1_610_612_736n)+ expect(freeSpaceFloor({ bsize: 1n, blocks: BigInt(TOTAL) })).toBe(BigInt(FLOOR))+ // 15% of 100 MiB is 15 MiB, under the minimum.+ expect(freeSpaceFloor({ bsize: 4096n, blocks: 25_600n })).toBe(16n * 1024n * 1024n)+ expect(freeSpaceFloor({ bsize: 1n, blocks: 1000n })).toBe(16n * 1024n * 1024n)+ expect(freeSpaceFloor({ bsize: 4096n, blocks: 0n })).toBe(16n * 1024n * 1024n)+ })++ it('FSF-1b: a restore aims for the floor plus 5% of the volume', () => {+ expect(freeSpaceRestoreTarget({ bsize: 1n, blocks: BigInt(TOTAL) })).toBe(+ BigInt(FLOOR + 10 * MIB)+ )+ // 16 MiB minimum floor plus 5% of 1000 bytes.+ expect(freeSpaceRestoreTarget({ bsize: 1n, blocks: 1000n })).toBe(16n * 1024n * 1024n + 50n)+ })+})++describe('GFS download store: admission keeps the free-space floor', () => {+ it('FSF-2: an admission is refused as disk_full while every copy is pinned, and admitted after evicting released copies', async () => {+ const store = await openStore()+ const ids: string[] = []+ for (let index = 0; index < 10; index += 1) {+ const { receipt } = await completedCopy(store, rootB, B, 100 + index, MIB, {+ owner: 'task-b',+ })+ ids.push(receipt.id)+ tick()+ }+ // 30 MiB free: a 5 MiB request needs 5 MiB plus the 30 MiB floor.+ leaveFree(FLOOR)+ const diskDenied = await quotaCount('host', 'free_space')+ const evicted = await expiryCount('expired_removed')++ await expect(startTransfer(store, rootA, A, 200, 5 * MIB)).rejects.toMatchObject({+ code: 'disk_full',+ })+ expect(await quotaCount('host', 'free_space')).toBe(diskDenied + 1)+ expect(ids.every(id => exists(downloadDirectory(rootB, id)))).toBe(true)+ expect(await expiryCount('expired_removed')).toBe(evicted)++ await store.releaseReceiptOwner('task-b', B)+ const admitted = await startTransfer(store, rootA, A, 201, 5 * MIB)++ // Witness: the admitted transfer exists and the five least recently used+ // copies were evicted to cover its 5 MiB; the other five stay.+ expect(exists(downloadDirectory(rootA, admitted.transfer.id))).toBe(true)+ expect(ids.map(id => exists(downloadDirectory(rootB, id)))).toEqual([+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ true,+ ])+ expect(await expiryCount('expired_removed')).toBe(evicted + 5)+ await store.fail(admitted.transfer.id, A)+ })+})++describe('GFS download store: the periodic sweep restores the free-space floor', () => {+ it('FSF-3: below the floor, cleanupExpired evicts the adopted copy and then the least recently used ones up to the floor plus 5%, and keeps pinned copies and active transfers', async () => {+ const plantedBytes = Buffer.alloc(4 * MIB, 3)+ const plantedId = randomUUID()+ const planted = plantDownload(rootC, {+ id: plantedId,+ bytes: plantedBytes,+ meta: metaFor(plantedId, plantedBytes, Date.now(), {+ callerIdentity: C,+ source: sourceFor(300),+ expiresAt: new Date(Date.now() + 2 * HOUR).toISOString(),+ }),+ })+ const store = await openStore()+ expect(provenanceOf(store, planted.id)).toBe('adopted')+ tick()+ // The pinned copy is the oldest published one, so only its pin keeps it.+ const pinned = await completedCopy(store, rootA, A, 301, 4 * MIB, { owner: 'task-a' })+ tick()+ const oldest = await completedCopy(store, rootB, B, 302, 4 * MIB)+ tick()+ const middle = await completedCopy(store, rootB, B, 303, 4 * MIB)+ tick()+ const newest = await completedCopy(store, rootB, B, 304, 4 * MIB)+ tick()+ const active = await startTransfer(store, rootA, A, 305, 4 * MIB)+ // 1 MiB below the 30 MiB floor; the restore aims for 30 + 10 MiB free, so+ // it needs 11 MiB: the adopted copy, then the oldest and the middle one.+ leaveFree(FLOOR - MIB)+ const warn = vi.spyOn(logger, 'warn')+ const evicted = await expiryCount('expired_removed')++ const result = await store.cleanupExpired()++ expect(result).toEqual({ removedExpired: 0, removedIncomplete: 0, removeFailed: 0 })+ expect(+ survey({+ adopted: planted.directory,+ pinned: downloadDirectory(rootA, pinned.receipt.id),+ oldest: downloadDirectory(rootB, oldest.receipt.id),+ middle: downloadDirectory(rootB, middle.receipt.id),+ newest: downloadDirectory(rootB, newest.receipt.id),+ active: downloadDirectory(rootA, active.transfer.id),+ })+ ).toEqual({ removed: ['adopted', 'middle', 'oldest'], kept: ['active', 'newest', 'pinned'] })+ expect(floorWarnings(warn)).toEqual([+ expect.objectContaining({ evicted: 3, removeFailed: 0, restored: true }),+ ])+ expect(await expiryCount('expired_removed')).toBe(evicted + 3)+ // The kept copies are still served.+ expect((await store.readManagedFile(pinned.receipt.path, A)).equals(pinned.bytes)).toBe(true)+ await store.fail(active.transfer.id, A)+ })++ it('FSF-4: above the floor, cleanupExpired evicts nothing while it still removes an expired copy', async () => {+ const store = await openStore()+ const expiring = await completedCopy(store, rootB, B, 400, MIB, {+ expiresAt: new Date(Date.now() + HOUR).toISOString(),+ })+ tick()+ const live = await completedCopy(store, rootB, B, 401, MIB)+ leaveFree(FLOOR + 10 * MIB)+ vi.setSystemTime(Date.now() + 90 * 60_000)+ const warn = vi.spyOn(logger, 'warn')+ const evicted = await expiryCount('expired_removed')++ const result = await store.cleanupExpired()++ // Witness: the same call removed the expired copy.+ expect(result).toEqual({ removedExpired: 1, removedIncomplete: 0, removeFailed: 0 })+ expect(exists(downloadDirectory(rootB, expiring.receipt.id))).toBe(false)+ expect(await expiryCount('expired_removed')).toBe(evicted + 1)+ expect(exists(downloadDirectory(rootB, live.receipt.id))).toBe(true)+ expect(floorWarnings(warn)).toEqual([])+ })++ it('FSF-5: a deficit larger than every unpinned copy is restored partially, and the evictions are kept', async () => {+ const store = await openStore()+ const pinned = await completedCopy(store, rootA, A, 500, MIB, { owner: 'task-a' })+ tick()+ const first = await completedCopy(store, rootB, B, 501, MIB)+ tick()+ const second = await completedCopy(store, rootB, B, 502, MIB)+ leaveFree(FLOOR - 5 * MIB)+ const warn = vi.spyOn(logger, 'warn')+ const evicted = await expiryCount('expired_removed')++ await expect(store.cleanupExpired()).resolves.toEqual({+ removedExpired: 0,+ removedIncomplete: 0,+ removeFailed: 0,+ })++ expect(+ survey({+ pinned: downloadDirectory(rootA, pinned.receipt.id),+ first: downloadDirectory(rootB, first.receipt.id),+ second: downloadDirectory(rootB, second.receipt.id),+ })+ ).toEqual({ removed: ['first', 'second'], kept: ['pinned'] })+ expect(floorWarnings(warn)).toEqual([+ expect.objectContaining({ evicted: 2, removeFailed: 0, restored: false }),+ ])+ expect(await expiryCount('expired_removed')).toBe(evicted + 2)+ })++ it.each([+ ['statfs rejects', 'EIO'],+ ['two invalid readings', 'volume_unmeasurable'],+ ])(+ 'FSF-6: when %s, cleanupExpired keeps its sweep result, evicts nothing and logs the failure',+ async (_case, code) => {+ const store = await openStore()+ const expiring = await completedCopy(store, rootB, B, 600, MIB, {+ expiresAt: new Date(Date.now() + HOUR).toISOString(),+ })+ tick()+ const unpinned = await completedCopy(store, rootB, B, 601, MIB)+ leaveFree(FLOOR - 5 * MIB)+ vi.setSystemTime(Date.now() + 90 * 60_000)+ statfsBoundary.mockReset()+ if (code === 'EIO')+ statfsBoundary.mockRejectedValue(Object.assign(new Error('simulated'), { code: 'EIO' }))+ else+ statfsBoundary.mockImplementation(async (target: string) => ({+ ...(await nativeFs.statfs(target, { bigint: true })),+ blocks: 0n,+ }))+ const warn = vi.spyOn(logger, 'warn')+ const failed = await expiryCount('sweep_failed')++ const result = await store.cleanupExpired()++ expect(result).toEqual({ removedExpired: 1, removedIncomplete: 0, removeFailed: 0 })+ expect(exists(downloadDirectory(rootB, expiring.receipt.id))).toBe(false)+ // Witness: the restore ran and could not measure the volume.+ expect(statfsBoundary).toHaveBeenCalled()+ expect(await expiryCount('sweep_failed')).toBe(failed + 1)+ expect(warn).toHaveBeenCalledWith(+ expect.objectContaining({ code }),+ 'GFS download store could not measure the volume to restore its free-space floor; the next sweep retries it'+ )+ expect(exists(downloadDirectory(rootB, unpinned.receipt.id))).toBe(true)+ expect(floorWarnings(warn)).toEqual([])+ }+ )++ it('FSF-7: a restore continues past the floor and stops once the floor plus 5% is free', async () => {+ const store = await openStore()+ const ids: string[] = []+ for (let index = 0; index < 5; index += 1) {+ const { receipt } = await completedCopy(store, rootB, B, 700 + index, 3 * MIB)+ ids.push(receipt.id)+ tick()+ }+ // One byte below the floor. Restoring only the floor would take the+ // oldest copy; the floor plus 10 MiB needs 10 MiB + 1 byte: four copies.+ leaveFree(FLOOR - 1)+ const warn = vi.spyOn(logger, 'warn')++ await store.cleanupExpired()++ expect(ids.map(id => exists(downloadDirectory(rootB, id)))).toEqual([+ false,+ false,+ false,+ false,+ true,+ ])+ expect(floorWarnings(warn)).toEqual([+ expect.objectContaining({ evicted: 4, removeFailed: 0, restored: true }),+ ])+ })+})++describe('GFS download store: startup restores the free-space floor', () => {+ it('FSF-8: a store initialized on a volume below the floor evicts adopted copies before it is available', async () => {+ const first = await openStore()+ const ids: string[] = []+ for (let index = 0; index < 4; index += 1) {+ const { receipt } = await completedCopy(first, rootB, B, 800 + index, 4 * MIB)+ ids.push(receipt.id)+ tick()+ }+ stores.splice(stores.indexOf(first), 1)+ await first.close(0)+ // 1 MiB below the floor at restart: the restore needs 11 MiB, three copies.+ leaveFree(FLOOR - MIB)+ const warn = vi.spyOn(logger, 'warn')+ const evicted = await expiryCount('expired_removed')+ statfsBoundary.mockClear()++ const restarted = await openStore()++ expect(restarted.isAvailable()).toBe(true)+ // Witness: initialize measured the volume and logged the restore.+ expect(statfsBoundary).toHaveBeenCalled()+ expect(floorWarnings(warn)).toEqual([+ expect.objectContaining({ evicted: 3, removeFailed: 0, restored: true }),+ ])+ expect(ids.filter(id => exists(downloadDirectory(rootB, id)))).toHaveLength(1)+ expect(await expiryCount('expired_removed')).toBe(evicted + 3)+ })+})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreHeldCharges.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreHeldCharges.test.tsindex e539d80a9..c55059fce 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreHeldCharges.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreHeldCharges.test.ts@@ -28,9 +28,9 @@ const { faults } = vi.hoisted(() => ({faults: new Map<string, (target: string) => string | undefined>(),}))-// A 95-byte volume at the default 85% gives a budget of floor(80.75) = 80:+// A 115-byte volume at the default 70% gives a budget of floor(80.5) = 80:// eight 5-byte copies per caller fill it exactly.-const VOLUME_BYTES = 95n+const VOLUME_BYTES = 115nvi.mock('node:fs/promises', async original => {const actual = await original<typeof fs>()diff --git a/mcp-host/src/internalTools/gfsDownloadStoreNonBlocking.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreNonBlocking.test.tsindex 154958857..081309479 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreNonBlocking.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreNonBlocking.test.ts@@ -4,7 +4,7 @@* bounded race reports `blocked`, the other end is opened once to release the* worker thread, and the assertion on the outcome fails the test.*/-import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { execFileSync } from 'node:child_process'import { randomUUID } from 'node:crypto'import * as syncFs from 'node:fs'@@ -21,6 +21,14 @@ import {import { bootstrapGfsRuntime } from '../gfsRuntime'import { GfsDownloadStore } from './gfsDownloadStore'+// statfs is the only boundary replaced: the volume sized to its free space,+// so the disk's occupancy never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+const KEY = 'fifo-key'const BOUND_MS = 2_000diff --git a/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.tsindex bf7e79f03..703b5daae 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreProtected.test.ts@@ -2,9 +2,12 @@* R3-F1 (#1028, Addendum 9): eviction never reclaims a pinned copy or an* active reservation, so one caller's protected bytes — pins of its retention* owners plus its reservations, each id once — may reach at most half the- * budget. Unpinned copies still use the whole budget. Ported from the final- * review's SOL-R3-PINS repro. A 100-byte volume at the default 85% gives a- * budget of 85 and a per-caller protected cap of floor(85 / 2) = 42.+ * budget. The protected bytes of all callers together are capped at three+ * quarters, so one caller at its cap leaves room for the others. Unpinned+ * copies still use the whole budget. Ported from the final review's+ * SOL-R3-PINS repro. A 122-byte volume at the default 70% gives a budget of+ * floor(85.4) = 85, a per-caller cap of floor(85 / 2) = 42 and a Host-wide cap+ * of floor(85 * 3 / 4) = 63.** Addendum 10: expiry is absolute. A pin protects a copy from eviction, never* past its `expiresAt`: the sweep removes an expired pinned copy, it stops@@ -78,7 +81,7 @@ async function copies(}const protectedRefusals = () => quotaCount('caller', 'protected_bytes')-const hostRefusals = () => quotaCount('host', 'storage_bytes')+const hostProtectedRefusals = () => quotaCount('host', 'protected_bytes')beforeEach(async () => {nativeFs = await vi.importActual<typeof fs>('node:fs/promises')@@ -87,7 +90,7 @@ beforeEach(async () => {statfsBoundary.mockReset()statfsBoundary.mockImplementation(async (target: string) => {const real = await nativeFs.statfs(target, { bigint: true })- return { ...real, bsize: 1n, blocks: 100n, bavail: real.bavail * real.bsize }+ return { ...real, bsize: 1n, blocks: 122n, bavail: real.bavail * real.bsize }})lstatBoundary.mockReset()lstatBoundary.mockImplementation((...args: unknown[]) =>@@ -107,26 +110,29 @@ afterEach(async () => {})describe('GFS download store: a caller protects at most half the budget (R3-F1)', () => {- it('SOL-R3-PINS: one caller pins up to the cap, exactly, and other callers keep admitting', async () => {+ it('SOL-R3-PINS: one caller pins up to its cap, exactly, and another caller is still admitted beside it', async () => {const store = await openStore()const pinned = await copies(store, rootA, A, 0, 8, 'task-a')// 40 pinned + 2 = 42, the cap exactly.await completedCopy(store, rootA, A, 8, 2, { owner: 'task-a' })const protectedBefore = await protectedRefusals()- const hostBefore = await hostRefusals()+ const hostBefore = await hostProtectedRefusals()const refusal = await startTransfer(store, rootA, A, 9, 1).catch(error => error)expect(refusal).toBeInstanceOf(GfsDownloadStoreError)expect(refusal).toMatchObject({ code: 'host_quota_exceeded' })expect(await protectedRefusals()).toBe(protectedBefore + 1)- expect(await hostRefusals()).toBe(hostBefore)-- // The repro's denial: B and C are admitted while A holds its cap. Expiry- // of pinned copies is covered by the absolute-retention tests below.- const b = await startTransfer(store, rootB, B, 10, 1)+ expect(await hostProtectedRefusals()).toBe(hostBefore)++ // A at its cap does not block B: B's 21-byte reservation brings the Host+ // to 63, the Host-wide cap exactly, and is admitted. A's pinned copies+ // stay. Expiry of pinned copies is covered by the absolute-retention tests+ // below.+ const b = await startTransfer(store, rootB, B, 10, 21)+ expect(b.transfer.id).toEqual(expect.any(String))+ expect(await hostProtectedRefusals()).toBe(hostBefore)+ expect(await protectedRefusals()).toBe(protectedBefore + 1)await store.fail(b.transfer.id, B)- const c = await startTransfer(store, rootC, C, 11, 1)- await store.fail(c.transfer.id, C)for (const receipt of pinned) expect(exists(downloadDirectory(rootA, receipt.id))).toBe(true)// Releasing the owner gives A its room back.@@ -135,28 +141,76 @@ describe('GFS download store: a caller protects at most half the budget (R3-F1)'await store.fail(again.transfer.id, A)})- it('a refused caller gets the same error object as a full Host budget', async () => {+ it('TWO-CALLERS-HOST-CAP: two callers that together protect three quarters of the budget refuse a third caller with the Host-wide scope', async () => {const store = await openStore()+ // A pins 42, its cap, and B pins 21, under its cap: 63, the Host-wide cap.await copies(store, rootA, A, 0, 8, 'task-a')await completedCopy(store, rootA, A, 8, 2, { owner: 'task-a' })- const capRefusal = await startTransfer(store, rootA, A, 9, 1).catch(error => error)+ await copies(store, rootB, B, 20, 4, 'task-b')+ await completedCopy(store, rootB, B, 24, 1, { owner: 'task-b' })+ const callerBefore = await protectedRefusals()+ const hostBefore = await hostProtectedRefusals()- // B pins 40 beside A's 42: 82 of 85, so C's 4 bytes exceed the Host- // budget while C protects nothing.- await copies(store, rootB, B, 20, 8, 'task-b')- const hostBefore = await hostRefusals()+ // C protects nothing, so its own cap is not the bound that refuses it.+ await expect(startTransfer(store, rootC, C, 40, 1)).rejects.toMatchObject({+ code: 'host_quota_exceeded',+ })+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ expect(await protectedRefusals()).toBe(callerBefore)++ // Witness: the same request is admitted once B's pins are released.+ await store.releaseReceiptOwner('task-b', B)+ const admitted = await startTransfer(store, rootC, C, 40, 1)+ await store.fail(admitted.transfer.id, C)+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ })++ it('a refused caller gets the same error object as a Host-wide refusal', async () => {+ const store = await openStore()+ await copies(store, rootA, A, 0, 4, 'task-a')+ await completedCopy(store, rootA, A, 8, 2, { owner: 'task-a' })+ // A protects 22: its 21-byte request exceeds its own cap of 42.const protectedBefore = await protectedRefusals()- const budgetRefusal = await startTransfer(store, rootC, C, 40, 4).catch(error => error)- expect(await hostRefusals()).toBe(hostBefore + 1)- expect(await protectedRefusals()).toBe(protectedBefore)- expect(budgetRefusal).toBeInstanceOf(GfsDownloadStoreError)+ const capRefusal = await startTransfer(store, rootA, A, 9, 21).catch(error => error)+ expect(await protectedRefusals()).toBe(protectedBefore + 1)++ // B pins 40 beside A's 22: 62, so C's 4 bytes exceed the Host-wide cap+ // of 63 while C protects nothing.+ await copies(store, rootB, B, 20, 8, 'task-b')+ const hostBefore = await hostProtectedRefusals()+ const hostRefusal = await startTransfer(store, rootC, C, 40, 4).catch(error => error)+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ expect(await protectedRefusals()).toBe(protectedBefore + 1)+ expect(hostRefusal).toBeInstanceOf(GfsDownloadStoreError)expect(capRefusal).toBeInstanceOf(GfsDownloadStoreError)expect({ code: capRefusal.code, message: capRefusal.message }).toEqual({- code: budgetRefusal.code,- message: budgetRefusal.message,+ code: hostRefusal.code,+ message: hostRefusal.message,})})+ it('HOST-CAP: two callers each under their own cap are refused once their sum would exceed three quarters of the budget, and admitted after a release', async () => {+ const store = await openStore()+ // A pins 40 and B 20: each under its own cap of 42, 60 together.+ await copies(store, rootA, A, 0, 8, 'task-a')+ await copies(store, rootB, B, 20, 4, 'task-b')+ const callerBefore = await protectedRefusals()+ const hostBefore = await hostProtectedRefusals()++ // B's 5 more would leave B at 25 of its 42, but the Host at 65 of 63.+ await expect(startTransfer(store, rootB, B, 30, 5)).rejects.toMatchObject({+ code: 'host_quota_exceeded',+ })+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ expect(await protectedRefusals()).toBe(callerBefore)++ // Witness: the same request is admitted once A's pins are released.+ await store.releaseReceiptOwner('task-a', A)+ const admitted = await startTransfer(store, rootB, B, 30, 5)+ await store.fail(admitted.transfer.id, B)+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ })+it('the new reservation itself counts, pinned or not', async () => {const store = await openStore()await copies(store, rootA, A, 0, 8, 'task-a')@@ -351,13 +405,22 @@ describe('GFS download store: retention pins never outlive expiresAt (Addendum 1it('eviction reclaims an expired pinned copy the admission sweep could not inspect, and keeps unexpired pins', async () => {const store = await openStore()- // A pins 42 bytes for one hour; B pins 42 bytes for a day. 84 of 85.- const expiring = await completedCopy(store, rootA, A, 0, 42, { owner: 'task-a' })- const lasting = await completedCopy(store, rootB, B, 1, 42, {+ // 40 unpinned bytes that live a day come first. Then A pins 22 bytes for+ // two hours and B pins 20 bytes for a day: 82 of 85.+ const lastingExpiry = new Date(Date.now() + 24 * HOUR_MS).toISOString()+ const loose = [+ await completedCopy(store, rootC, C, 3, 20, { expiresAt: lastingExpiry }),+ await completedCopy(store, rootC, C, 4, 20, { expiresAt: lastingExpiry }),+ ]+ const expiring = await completedCopy(store, rootA, A, 0, 22, { owner: 'task-a' })+ const lasting = await completedCopy(store, rootB, B, 1, 20, {owner: 'task-b',- expiresAt: new Date(Date.now() + 24 * HOUR_MS).toISOString(),+ expiresAt: lastingExpiry,})- vi.setSystemTime(Date.now() + 2 * HOUR_MS)+ vi.setSystemTime(Date.now() + 3 * HOUR_MS)+ // Reading the loose copies makes A's expired copy the least recently used.+ for (const copy of loose)+ await expect(store.readManagedFile(copy.receipt.path, C)).resolves.toEqual(copy.bytes)// The admission sweep removes expired copies before eviction plans, so// the eviction path is isolated by making the sweep's one inspection of@@ -373,12 +436,13 @@ describe('GFS download store: retention pins never outlive expiresAt (Addendum 1return passThrough(...args)})- // C's 5 bytes need 4 bytes evicted: only A's expired copy can give them.+ // C's 5 bytes need 2 bytes evicted: A's expired copy, first in order, gives them.const admitted = await startTransfer(store, rootC, C, 2, 5)expect(injected).toBe(1)expect(exists(downloadDirectory(rootA, expiring.receipt.id))).toBe(false)expect(exists(downloadDirectory(rootB, lasting.receipt.id))).toBe(true)await expect(store.readManagedFile(lasting.receipt.path, B)).resolves.toEqual(lasting.bytes)+ for (const copy of loose) expect(exists(downloadDirectory(rootC, copy.receipt.id))).toBe(true)await store.fail(admitted.transfer.id, C)})})@@ -410,31 +474,37 @@ describe('GFS download store: re-pinning an already protected copy (R4-F1)', ()describe('GFS download store: pins are per caller and per process (F24/U4 ported)', () => {it('releasing an owner id unpins only the releasing caller; the same id under another caller keeps counting', async () => {const store = await openStore()- // A pins 42 (its cap) and B pins 40, both under the same owner id.- await copies(store, rootA, A, 0, 8, 'task-shared')+ // A pins 22 and B pins 39, both under the same owner id: 61 of the+ // Host-wide cap of 63.+ await copies(store, rootA, A, 0, 4, 'task-shared')await completedCopy(store, rootA, A, 8, 2, { owner: 'task-shared' })- await copies(store, rootB, B, 20, 8, 'task-shared')+ await copies(store, rootB, B, 20, 7, 'task-shared')+ await completedCopy(store, rootB, B, 27, 4, { owner: 'task-shared' })const before = await protectedRefusals()+ const hostBefore = await hostProtectedRefusals()- // Before any release B is refused too: 40 + 3 > 42.+ // Before any release B is refused by the Host-wide cap: 61 + 3 > 63, while+ // B itself stays at its cap, 39 + 3 = 42.await expect(startTransfer(store, rootB, B, 30, 3)).rejects.toMatchObject({code: 'host_quota_exceeded',})- expect(await protectedRefusals()).toBe(before + 1)+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)await expect(store.releaseReceiptOwner('task-shared', B)).resolves.toBeUndefined()await expect(store.releaseReceiptOwner('unknown-owner', A)).resolves.toBeUndefined()- // B's release freed B: its 3 bytes are admitted (82 + 3 = 85 fits the- // budget, so only the cap could refuse them).+ // B's release freed B: its 3 bytes are admitted (22 + 3 protected).const admittedB = await startTransfer(store, rootB, B, 31, 3)await store.fail(admittedB.transfer.id, B)- expect(await protectedRefusals()).toBe(before + 1)- // A's pin under the same id still holds A at its cap.- await expect(startTransfer(store, rootA, A, 32, 1)).rejects.toMatchObject({+ expect(await hostProtectedRefusals()).toBe(hostBefore + 1)+ // A's pin under the same id still counts toward A's cap: 22 + 21 > 42 is+ // refused, 22 + 20 is admitted.+ await expect(startTransfer(store, rootA, A, 32, 21)).rejects.toMatchObject({code: 'host_quota_exceeded',})- expect(await protectedRefusals()).toBe(before + 2)+ expect(await protectedRefusals()).toBe(before + 1)+ const admittedA = await startTransfer(store, rootA, A, 33, 20)+ await store.fail(admittedA.transfer.id, A)})it('a second store on the same root holds no pins', async () => {diff --git a/mcp-host/src/internalTools/gfsDownloadStoreProvenance.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreProvenance.test.tsindex f622656e3..ad19b469d 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreProvenance.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreProvenance.test.ts@@ -39,14 +39,18 @@ vi.mock('node:crypto', async original => ({createHash: hashBoundary,}))// statfs reports the real free space; a test that sets `volume.totalBytes`-// sizes the volume (block size 1), and with it the retained budget.+// sizes the volume (block size 1), and with it the retained budget. Otherwise+// the volume is sized to its free space (freeSpaceSizedStatfs).vi.mock('node:fs/promises', async original => {const actual = await original<typeof fs>()return {...actual,statfs: async (target: string, options: { bigint: true }) => {+ if (volume.totalBytes === undefined) {+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')+ return freeSpaceSizedStatfs(actual.statfs)(target, options)+ }const real = await actual.statfs(target, options)- if (volume.totalBytes === undefined) return realreturn { ...real, bsize: 1n, blocks: volume.totalBytes, bavail: real.bavail * real.bsize }},}@@ -196,7 +200,9 @@ describe('GFS download store provenance: planted copies', () => {it('T-S4: a planted entry is inventoried under the directory that contains it and evicted first', async () => {const bytes = Buffer.alloc(10, 0x43)const planted = plantComplete(rootB, C, 9, bytes)- volume.totalBytes = 20n+ // Budget 18; the active reservations stay within the 9 protected bytes+ // the Host allows.+ volume.totalBytes = 18nconst limited = await withEnvironment({MCP_HOST_GFS_MAX_FILE_BYTES: '10',@@ -216,13 +222,13 @@ describe('GFS download store provenance: planted copies', () => {expect(inventory.byCaller.has(C)).toBe(false)const denied = await quotaCount('host', 'storage_bytes')- // 10 planted + 10 requested fit the budget of 20, so nothing is evicted.+ // 10 planted + 4 requested fit the budget of 18, so nothing is evicted.const rootC = callerDirectory(hostRoot, C)- await startTransfer(limited, rootC, C, 10, 10)+ await startTransfer(limited, rootC, C, 10, 4)expect(exists(planted.directory)).toBe(true)- // 10 + 10 + 10 does not: the plant goes, the active reservation stays.- await startTransfer(limited, rootB, B, 11, 10)+ // 10 + 4 + 5 does not: the plant goes, the active reservation stays.+ await startTransfer(limited, rootB, B, 11, 5)expect(exists(planted.directory)).toBe(false)expect(await quotaCount('host', 'storage_bytes')).toBe(denied)})diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.tsindex 76e085d5e..e7d101592 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreRecovery.test.ts@@ -18,6 +18,7 @@ import {digestOf,downloadDirectory,exists,+ freeSpaceSizedStatfs,metaFor,plantDownload,quotaCount,@@ -27,10 +28,14 @@ import { logger } from '../logger'import { GfsDownloadStore } from './gfsDownloadStore'import { REMOVED_GFS_STORAGE_VARIABLES } from './gfsFilePolicy'-const { statfsBoundary } = vi.hoisted(() => ({ statfsBoundary: vi.fn() }))+const { statfsBoundary, renameBoundary } = vi.hoisted(() => ({+ statfsBoundary: vi.fn(),+ renameBoundary: vi.fn(),+}))vi.mock('node:fs/promises', async original => ({...(await original<typeof fs>()),statfs: statfsBoundary,+ rename: renameBoundary,}))const A = 'caller-a'@@ -85,7 +90,11 @@ function storeDirectories(callerRoot: string): string[] {beforeEach(async () => {nativeFs = await vi.importActual<typeof fs>('node:fs/promises')statfsBoundary.mockReset()- statfsBoundary.mockImplementation(nativeFs.statfs)+ statfsBoundary.mockImplementation(freeSpaceSizedStatfs(nativeFs.statfs))+ renameBoundary.mockReset()+ renameBoundary.mockImplementation((...args: unknown[]) =>+ Reflect.apply(nativeFs.rename, nativeFs, args)+ )hostRoot = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-recovery-'))outside = syncFs.mkdtempSync(path.join(tmpdir(), 'gfs-store-recovery-outside-'))rootA = callerDirectory(hostRoot, A)@@ -118,11 +127,11 @@ describe('GFS download store: a store directory made unwritable is still removedvi.useFakeTimers({ toFake: ['Date'] })vi.setSystemTime(new Date('2026-10-08T10:00:00.000Z'))- // 100-byte volume, budget 85: A and B hold 40 bytes each.+ // 100-byte volume, budget 70: A and B hold 30 bytes each.volumeOf(100)const store = await openStore()- const copyA = await completedCopy(store, rootA, A, 800, 40)- const copyB = await completedCopy(store, rootB, B, 801, 40)+ const copyA = await completedCopy(store, rootA, A, 800, 30)+ const copyB = await completedCopy(store, rootB, B, 801, 30)const directoryA = downloadDirectory(rootA, copyA.receipt.id)const directoryB = downloadDirectory(rootB, copyB.receipt.id)// A symlink inside the tree to a directory outside the Host root whose@@ -140,12 +149,106 @@ describe('GFS download store: a store directory made unwritable is still removedexpect(sweep).toMatchObject({ removedExpired: 2, removeFailed: 0 })expect(storeDirectories(rootA)).toEqual([])expect(storeDirectories(rootB)).toEqual([])- const admitted = await startTransfer(store, rootC, C, 802, 40)- expect(admitted.transfer.sizeBytes).toBe(40)+ const admitted = await startTransfer(store, rootC, C, 802, 30)+ expect(admitted.transfer.sizeBytes).toBe(30)expect(await quotaCount('host', 'storage_bytes')).toBe(budgetDenied)// The symlink was removed, never followed: its target keeps its mode.expect(syncFs.statSync(target).mode & 0o777).toBe(0o500)})++ /** A valid planted copy of `sizeBytes` zero bytes; the source is sparse. */+ function plantSparseCopy(callerRoot: string, sizeBytes: number) {+ const id = randomUUID()+ const bytes = Buffer.alloc(sizeBytes)+ const planted = plantDownload(callerRoot, {+ id,+ bytes: Buffer.alloc(0),+ meta: metaFor(id, bytes, Date.now()),+ })+ syncFs.truncateSync(path.join(planted.directory, 'source'), sizeBytes)+ return planted+ }++ /** Whether `candidate` names `directory` in its .gfs-downloads parent. */+ function isPlantedPath(candidate: string, directory: string): boolean {+ return (+ path.basename(candidate) === path.basename(directory) &&+ path.basename(path.dirname(candidate)) === '.gfs-downloads'+ )+ }++ // Root bypasses the mode bits, so the refused rename cannot be produced.+ it.skipIf(process.getuid?.() === 0)(+ 'RCV-1c: an adopted copy whose .gfs-downloads parent is chmod 0500 is evicted after the owner rwx is restored on the parent, and its charge is released',+ async () => {+ // 100-byte volume, budget 70: the adopted 50 bytes plus B's 30 exceed it.+ volumeOf(100)+ const planted = plantSparseCopy(rootA, 50)+ const parent = path.dirname(planted.directory)+ const store = await openStore()+ syncFs.chmodSync(parent, 0o500)+ // Precondition: the parent refuses a rename out of it for this user.+ const probe = path.join(parent, `probe-${randomUUID()}`)+ expect(() => syncFs.renameSync(planted.directory, probe)).toThrow(+ expect.objectContaining({ code: 'EACCES' })+ )+ const budgetDenied = await quotaCount('host', 'storage_bytes')++ const admitted = await startTransfer(store, rootB, B, 803, 30)++ expect(admitted.transfer.sizeBytes).toBe(30)+ expect(exists(planted.directory)).toBe(false)+ expect(storeDirectories(rootA)).toEqual([])+ expect(await quotaCount('host', 'storage_bytes')).toBe(budgetDenied)+ // Witness: the first rename was refused and the retry succeeded on the+ // parent whose owner bits were restored, other bits kept.+ // Paths are matched by name: the store works on resolved paths.+ const renamesOutOfParent = renameBoundary.mock.calls.filter(call =>+ isPlantedPath(String(call[0]), planted.directory)+ )+ expect(renamesOutOfParent).toHaveLength(2)+ expect(syncFs.statSync(parent).mode & 0o777).toBe(0o700)+ }+ )++ it.skipIf(process.getuid?.() === 0)(+ 'RCV-1d: a symlink swapped in for the .gfs-downloads parent after a refused rename is not followed, and the removal is refused',+ async () => {+ vi.useFakeTimers({ toFake: ['Date'] })+ vi.setSystemTime(new Date('2026-10-08T10:00:00.000Z'))+ volumeOf(100)+ const planted = plantSparseCopy(rootA, 20)+ const parent = path.dirname(planted.directory)+ const store = await openStore()+ // The directory the symlink will lead to; its mode must not change.+ const target = path.join(outside, 'target')+ syncFs.mkdirSync(target, { mode: 0o500 })+ const movedParent = path.join(outside, 'moved-downloads')+ let swapped = 0+ renameBoundary.mockImplementation(async (...args: unknown[]) => {+ const from = String(args[0])+ if (swapped === 0 && isPlantedPath(from, planted.directory)) {+ swapped += 1+ // The refused rename, then the parent swapped for a symlink before+ // the store restores the parent's owner access.+ await nativeFs.rename(path.dirname(from), movedParent)+ await nativeFs.symlink(target, path.dirname(from))+ throw Object.assign(new Error('EACCES: injected'), { code: 'EACCES' })+ }+ return Reflect.apply(nativeFs.rename, nativeFs, args)+ })+ vi.setSystemTime(Date.now() + 2 * HOUR)++ const sweep = await store.cleanupExpired()++ expect(swapped).toBe(1)+ expect(sweep).toMatchObject({ removedExpired: 0, removeFailed: 1 })+ // The symlink was refused, never followed: its target keeps its mode.+ expect(syncFs.lstatSync(parent).isSymbolicLink()).toBe(true)+ expect(syncFs.statSync(target).mode & 0o777).toBe(0o500)+ expect(exists(path.join(movedParent, path.basename(planted.directory)))).toBe(true)+ }+ )})describe('GFS download store: a non-regular meta.json', () => {diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRemovalCharge.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRemovalCharge.test.tsindex c8c0261f1..8dd486b9d 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreRemovalCharge.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreRemovalCharge.test.ts@@ -17,6 +17,7 @@ import {completedCopy,digestOf,downloadDirectory,+ expiryCount,plantDownload,quotaCount,sourceFor,@@ -119,9 +120,11 @@ function failTrashRemoval(): string[] {return hits}+// Unpinned: two 4 MiB pins would exceed the Host-wide protected cap (7.5 MiB+// of the 10 MiB budget), and an expired copy is swept whether it is pinned or not.async function expiredTrash(store: GfsDownloadStore) {- await completedCopy(store, roots.a, 'a', 1, 4 * MIB, { owner: 'owner-a' })- await completedCopy(store, roots.b, 'b', 2, 4 * MIB, { owner: 'owner-b' })+ await completedCopy(store, roots.a, 'a', 1, 4 * MIB)+ await completedCopy(store, roots.b, 'b', 2, 4 * MIB)vi.setSystemTime(Date.now() + 2 * HOUR)const hits = failTrashRemoval()expect((await store.cleanupExpired()).removeFailed).toBe(2)@@ -175,8 +178,9 @@ afterEach(async () => {describe('GFS download store removal charges', () => {it('RC-UNDO-FAIL: post-rename verification and undo failures retain the renamed bytes', async () => {const store = await storeWithBudget()- await completedCopy(store, roots.a, 'a', 1, 4 * MIB, { owner: 'a-owner' })- await completedCopy(store, roots.b, 'b', 2, 4 * MIB, { owner: 'b-owner' })+ // Unpinned, as in expiredTrash: the copies expire before the sweep.+ await completedCopy(store, roots.a, 'a', 1, 4 * MIB)+ await completedCopy(store, roots.b, 'b', 2, 4 * MIB)vi.setSystemTime(Date.now() + 2 * HOUR)const checkFault = new Set<string>()const undone: string[] = []@@ -211,10 +215,13 @@ describe('GFS download store removal charges', () => {'RC-FAILED-TRANSFER-%s: failed transfer trash keeps its former reservation charged',async stage => {const store = await storeWithBudget()- const attempts = []+ const hits = failTrashRemoval()+ // One transfer at a time: two 4 MiB reservations at once would exceed the+ // Host-wide protected cap (7.5 MiB of the 10 MiB budget). A held trash+ // charge is not protected, so the second transfer is admitted after the+ // first one failed.for (const [index, caller] of ['a', 'b'].entries()) {const attempt = await startTransfer(store, roots[caller], caller, index, 4 * MIB)- attempts.push({ ...attempt, caller })if (stage === 'post-source-rename') {const directory = downloadDirectory(roots[caller], attempt.transfer.id)boundaries.open.mockImplementation(async (...args: unknown[]) => {@@ -229,18 +236,18 @@ describe('GFS download store removal charges', () => {expect(syncFs.existsSync(path.join(directory, 'meta.json'))).toBe(true)restoreBoundary('open')}- }- expect(view(store).active).toBe(8 * MIB)- const hits = failTrashRemoval()- for (const attempt of attempts)- await expect(store.fail(attempt.transfer.id, attempt.caller)).rejects.toMatchObject({+ expect(view(store).active).toBe(4 * MIB)+ await expect(store.fail(attempt.transfer.id, caller)).rejects.toMatchObject({code: 'storage_write_failed',})- expect(hits).toHaveLength(2)+ }+ // Three removals: a's trash, a's trash again under a new trash name+ // (retried by b's admission sweep), then b's trash.+ expect(hits).toHaveLength(3)expect(storedBytes(hostRoot)).toBe(8 * MIB)- // Each reservation moved, unchanged, to the trash name its removal left.+ // Each reservation moved, unchanged, to the last trash name its removal left.expect(view(store)).toMatchObject({ active: 0, held: 8 * MIB, charged: 8 * MIB })- expect([...view(store).state.held.keys()].sort()).toEqual([...hits].sort())+ expect([...view(store).state.held.keys()].sort()).toEqual(hits.slice(1).sort())await expectRefused(store)await recoverAndWitness(store)}@@ -336,12 +343,15 @@ describe('GFS download store removal charges', () => {it('RC-SERIAL-SWEEPS: concurrent admission and cleanup queue behind the pre-check sweep and preserve live pins and active transfers', async () => {const store = await storeWithBudget()const expiry = new Date(Date.now() + 24 * HOUR).toISOString()- const live = await completedCopy(store, roots.a, 'a', 0, 2 * MIB, {+ // Protected bytes stay within the Host-wide cap (7.5 MiB of the 10 MiB+ // budget): the live pin and the active transfer take 1 MiB each, and the+ // expiring copy and the admission below 3 MiB each, one after the other.+ const live = await completedCopy(store, roots.a, 'a', 0, MIB, {owner: 'live',expiresAt: expiry,})- const active = await startTransfer(store, roots.a, 'a', 1, 2 * MIB, { expiresAt: expiry })- const expiring = await completedCopy(store, roots.b, 'b', 2, 4 * MIB, { owner: 'expire' })+ const active = await startTransfer(store, roots.a, 'a', 1, MIB, { expiresAt: expiry })+ const expiring = await completedCopy(store, roots.b, 'b', 2, 3 * MIB)vi.setSystemTime(Date.now() + 2 * HOUR)let enter!: () => voidlet release!: () => void@@ -362,7 +372,7 @@ describe('GFS download store removal charges', () => {return Reflect.apply(nativeFs.rm, nativeFs, args)})boundaries.statfs.mockClear()- const first = startTransfer(store, roots.c, 'c', 3, 4 * MIB)+ const first = startTransfer(store, roots.c, 'c', 3, 3 * MIB)await enteredlet secondSettled = falselet cleanupSettled = false@@ -399,24 +409,25 @@ describe('GFS download store removal charges', () => {code: 'download_missing',message: 'GFS download store failed (download_missing)',})- expect(view(store).charged).toBe(8 * MIB)+ expect(view(store).charged).toBe(5 * MIB)await store.fail(active.transfer.id, 'a')- const next = await startTransfer(store, roots.d, 'd', 4, 4 * MIB)- expect(view(store).charged).toBe(10 * MIB)+ const next = await startTransfer(store, roots.d, 'd', 4, MIB)+ expect(view(store).charged).toBe(5 * MIB)expect((await store.readManagedFile(live.receipt.path, 'a')).equals(live.bytes)).toBe(true)await store.fail(admitted.transfer.id, 'c')await store.fail(next.transfer.id, 'd')})it.each([false, true])(- 'RC-REUSE-UNPINNED-%s: post-hash expiry is a miss with and without a new retention owner',+ 'RC-REUSE-UNPINNED-%s: crossing the one-hour reuse margin after the hash is a miss with and without a new retention owner',async owner => {const store = await storeWithBudget()const copy = await completedCopy(store, roots.a, 'a', 0, 1024)await expect(store.reusableReceipt('a', sourceFor(0), 1024)).resolves.toMatchObject({id: copy.receipt.id,})- vi.setSystemTime(Date.parse(copy.receipt.expiresAt) - 1)+ const reuseMargin = 60 * 60_000+ vi.setSystemTime(Date.parse(copy.receipt.expiresAt) - reuseMargin - 1)let enter!: () => voidlet release!: () => voidconst entered = new Promise<void>(resolve => {@@ -447,11 +458,12 @@ describe('GFS download store removal charges', () => {owner ? { retentionOwnerId: 'new-owner' } : undefined)await entered- vi.setSystemTime(Date.parse(copy.receipt.expiresAt))+ vi.setSystemTime(Date.parse(copy.receipt.expiresAt) - reuseMargin)release()await expect(reuse).resolves.toBeUndefined()expect(view(store).state.pins.size).toBe(0)- await store.cleanupExpired()+ vi.setSystemTime(Date.parse(copy.receipt.expiresAt))+ expect((await store.cleanupExpired()).removedExpired).toBe(1)const fresh = await completedCopy(store, roots.a, 'a', 0, 1024, { owner: 'new-owner' })expect((await store.readManagedFile(fresh.receipt.path, 'a')).equals(fresh.bytes)).toBe(true)}@@ -473,8 +485,6 @@ describe('GFS download store removal charges', () => {it('RC-FAIL-BEFORE-RENAME: a failed transfer still in its original incomplete directory retains its charge', async () => {const store = await storeWithBudget()- const a = await startTransfer(store, roots.a, 'a', 1, 4 * MIB)- const b = await startTransfer(store, roots.b, 'b', 2, 4 * MIB)const hits: string[] = []boundaries.rename.mockImplementation((...args: unknown[]) => {if (path.basename(String(args[0])).startsWith('input-')) {@@ -483,17 +493,24 @@ describe('GFS download store removal charges', () => {}return Reflect.apply(nativeFs.rename, nativeFs, args)})+ // One transfer at a time: two 4 MiB reservations at once would exceed the+ // Host-wide protected cap (7.5 MiB of the 10 MiB budget).+ const a = await startTransfer(store, roots.a, 'a', 1, 4 * MIB)await expect(store.fail(a.transfer.id, 'a')).rejects.toMatchObject({code: 'storage_write_failed',})+ expect(hits).toHaveLength(1)+ // b's admission sweep retries a's directory once more.+ const b = await startTransfer(store, roots.b, 'b', 2, 4 * MIB)+ expect(hits).toHaveLength(2)await expect(store.fail(b.transfer.id, 'b')).rejects.toMatchObject({code: 'storage_write_failed',})- expect(hits).toHaveLength(2)+ expect(hits).toHaveLength(3)expect(storedBytes(hostRoot)).toBe(8 * MIB)expect(view(store).held).toBe(8 * MIB)await expectRefused(store)- expect(hits).toHaveLength(4)+ expect(hits).toHaveLength(5)await recoverAndWitness(store)})@@ -610,12 +627,7 @@ describe('GFS download store removal charges', () => {it('RC-FAIL-PARENT-REFUSED: a failed transfer whose parent check refuses before the rename keeps its reservation charged', async () => {const store = await storeWithBudget()- const a = await startTransfer(store, roots.a, 'a', 1, 4 * MIB)- const b = await startTransfer(store, roots.b, 'b', 2, 4 * MIB)- const faulted = new Set([- path.join(roots.a, '.gfs-downloads'),- path.join(roots.b, '.gfs-downloads'),- ])+ const faulted = new Set<string>()const hits: string[] = []boundaries.realpath.mockImplementation(async (...args: unknown[]) => {if (faulted.has(String(args[0]))) {@@ -624,13 +636,16 @@ describe('GFS download store removal charges', () => {}return Reflect.apply(nativeFs.realpath, nativeFs, args)})- for (const [attempt, caller] of [- [a, 'a'],- [b, 'b'],- ] as const)+ // One transfer at a time: two 4 MiB reservations at once would exceed the+ // Host-wide protected cap (7.5 MiB of the 10 MiB budget). Each parent is+ // faulted after its own admission, which resolves it.+ for (const [index, caller] of ['a', 'b'].entries()) {+ const attempt = await startTransfer(store, roots[caller], caller, index + 1, 4 * MIB)+ faulted.add(path.join(roots[caller], '.gfs-downloads'))await expect(store.fail(attempt.transfer.id, caller)).rejects.toMatchObject({code: 'storage_write_failed',})+ }expect(hits.length).toBeGreaterThanOrEqual(2)expect(storedBytes(hostRoot)).toBe(8 * MIB)expect(view(store)).toMatchObject({ entries: 0, active: 0, held: 8 * MIB, charged: 8 * MIB })@@ -662,7 +677,9 @@ describe('GFS download store removal charges', () => {return Reflect.apply(nativeFs.readdir, nativeFs, args)})for (let i = 0; i < 3; i++) await store.cleanupExpired()- expect(renames).toHaveLength(3)+ // Two per sweep: an EACCES rename restores the owner's access on the+ // caller root and is retried once, and the retry fails the same way.+ expect(renames).toHaveLength(6)expect(walked).toEqual([])expect(view(store)).toMatchObject({ held: 0, charged: 4 * MIB })expect((await store.readManagedFile(copy.receipt.path, 'b')).equals(copy.bytes)).toBe(true)@@ -767,6 +784,46 @@ describe('GFS download store removal charges', () => {await store.fail(fits.transfer.id, 'c')})+ it('RC-EXPIRED-KEPT: an expired entry whose removal fails in the sweep stays indexed and charged', async () => {+ const store = await storeWithBudget()+ const expiresAt = new Date(Date.now() + HOUR).toISOString()+ const copyA = await completedCopy(store, roots.a, 'a', 1, 4 * MIB, { expiresAt })+ const copyB = await completedCopy(store, roots.b, 'b', 2, 4 * MIB, { expiresAt })+ const stuck = [copyA, copyB].map(({ receipt }, index) =>+ downloadDirectory(roots[index === 0 ? 'a' : 'b'], receipt.id)+ )+ vi.setSystemTime(Date.now() + 2 * HOUR)+ const attempts: string[] = []+ boundaries.rename.mockImplementation((...args: unknown[]) => {+ if (stuck.includes(String(args[0]))) {+ attempts.push(String(args[0]))+ return Promise.reject(eio())+ }+ return Reflect.apply(nativeFs.rename, nativeFs, args)+ })+ const removeFailed = await expiryCount('remove_failed')+ const removedExpired = await expiryCount('expired_removed')++ expect(await store.cleanupExpired()).toMatchObject({ removedExpired: 0, removeFailed: 2 })++ // Witness: the sweep tried to remove both expired copies, and counted it.+ expect([...attempts].sort()).toEqual([...stuck].sort())+ expect(await expiryCount('remove_failed')).toBe(removeFailed + 2)+ expect(await expiryCount('expired_removed')).toBe(removedExpired)+ // Both are still indexed under their own names, and their 8 MiB charged.+ expect([...view(store).state.entries.keys()].sort()).toEqual(+ [copyA.receipt.id, copyB.receipt.id].sort()+ )+ expect(view(store)).toMatchObject({ entries: 8 * MIB, held: 0, charged: 8 * MIB })+ expect(storedBytes(hostRoot)).toBe(8 * MIB)+ // The charge still counts against the 10 MiB budget: 8 + 4 MiB is refused.+ const denied = await quotaCount('host', 'storage_bytes')+ await expectRefused(store)+ expect(await quotaCount('host', 'storage_bytes')).toBe(denied + 1)+ restoreBoundary('rename')+ await recoverAndWitness(store)+ })+it('RC-UNDO-RESTORED-UNINDEXED: startup leftovers restored by an undone refusal are measured once their parent passes again', async () => {for (const caller of ['a', 'b'])plantDownload(roots[caller], { bytes: Buffer.alloc(4 * MIB), meta: 'omit' })diff --git a/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.tsindex 92bbd6eef..c9abd3959 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreRestart.test.ts@@ -33,8 +33,12 @@ const { renameFailure, rmFailure } = vi.hoisted(() => ({// would (EBUSY: EACCES and EPERM from rm restore owner access and retry once).vi.mock('node:fs/promises', async original => {const actual = await original<typeof import('node:fs/promises')>()+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')return {...actual,+ // The volume sized to its free space, so the disk's occupancy never+ // meets the store's free-space floor.+ statfs: freeSpaceSizedStatfs(actual.statfs),rename: async (...values: Parameters<typeof actual.rename>) => {if (renameFailure.target !== undefined &&@@ -206,7 +210,7 @@ describe('GFS download store restart', () => {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.+ // The periodic sweep retries without a restart.await expect(store.cleanupExpired()).resolves.toMatchObject({ removeFailed: 0 })expect(retirementWarnings(warn)).toHaveLength(1)diff --git a/mcp-host/src/internalTools/gfsDownloadStoreTrashAndExpiry.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreTrashAndExpiry.test.tsindex 1440ca8a8..4f05138d1 100644--- a/mcp-host/src/internalTools/gfsDownloadStoreTrashAndExpiry.test.ts+++ b/mcp-host/src/internalTools/gfsDownloadStoreTrashAndExpiry.test.ts@@ -47,7 +47,7 @@ const B = 'caller-b'const C = 'caller-c'const HOUR_MS = 60 * 60_000const MIB = 1024 * 1024-/** The store's fixed free-space margin. */+/** The free-space floor on the 1000-byte volumes below: its 16 MiB minimum. */const MARGIN = 16n * BigInt(MIB)let nativeFs: typeof fs@@ -228,13 +228,17 @@ describe('GFS download store: expired copies never make the disk full (M1)', ()})describe('GFS download store: a copy left under a trash name stays charged (R5-F1)', () => {- it('SOL-R5-TRASH-CHARGE: expired pinned copies whose trash cannot be removed keep their charge until cleanup succeeds', async () => {- // 1% of a 1000 MiB volume: budget 10 MiB, caller cap 5 MiB; the free- // space always holds the 16 MiB margin, so only the budget can refuse.+ it('SOL-R5-TRASH-CHARGE: an expired pinned copy and an unpinned one whose trash cannot be removed keep their charge until cleanup succeeds', async () => {+ // 1% of a 1000 MiB volume: budget 10 MiB, protected caps 5 MiB per caller+ // and 7.5 MiB Host-wide; the free+ // space always holds the 150 MiB free-space floor, so only the budget+ // can refuse. One copy is pinned, and the unpinned one comes first: a+ // second 4 MiB pin, or its reservation beside the pin, would exceed the+ // Host-wide protected cap.coherentVolume(1000n * BigInt(MIB), 1000n * BigInt(MIB))const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '1' })+ const copyB = await completedCopy(store, rootB, B, 1, 4 * MIB)const copyA = await completedCopy(store, rootA, A, 0, 4 * MIB, { owner: 'task-a' })- const copyB = await completedCopy(store, rootB, B, 1, 4 * MIB, { owner: 'task-b' })vi.setSystemTime(Date.now() + 2 * HOUR_MS)const trashRemovals: string[] = []rmBoundary.mockImplementation((...args: unknown[]) => {@@ -291,8 +295,11 @@ describe('GFS download store: a copy left under a trash name stays charged (R5-Fit('OPUS-R6-RESTART: after a Host restart a trash directory whose removal keeps failing is sized and charged again', async () => {coherentVolume(1000n * BigInt(MIB), 1000n * BigInt(MIB))const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '1' })+ // One copy is pinned, and the unpinned one comes first: a second 4 MiB+ // pin, or its reservation beside the pin, would exceed the Host-wide+ // protected cap of 7.5 MiB.+ await completedCopy(store, rootB, B, 1, 4 * MIB)await completedCopy(store, rootA, A, 0, 4 * MIB, { owner: 'task-a' })- await completedCopy(store, rootB, B, 1, 4 * MIB, { owner: 'task-b' })vi.setSystemTime(Date.now() + 2 * HOUR_MS)const trashRemovals: string[] = []rmBoundary.mockImplementation((...args: unknown[]) => {@@ -334,8 +341,6 @@ describe('GFS download store: a copy left under a trash name stays charged (R5-Fit('DS-R6-FAIL-PARTIAL: a failed transfer whose trash cannot be removed stays charged with the bytes it wrote', async () => {coherentVolume(1000n * BigInt(MIB), 1000n * BigInt(MIB))const store = await limitedStore({ MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT: '1' })- const first = await startTransfer(store, rootA, A, 0, 4 * MIB)- const second = await startTransfer(store, rootB, B, 1, 4 * MIB)const trashRemovals: string[] = []rmBoundary.mockImplementation((...args: unknown[]) => {if (path.basename(String(args[0])).startsWith('.trash-')) {@@ -346,14 +351,19 @@ describe('GFS download store: a copy left under a trash name stays charged (R5-F})// Neither transfer was published, so the store holds no indexed size for- // either; both partials are left on disk under trash names.+ // either; both partials are left on disk under trash names. One transfer+ // at a time: two 4 MiB reservations at once would exceed the Host-wide+ // protected cap of 7.5 MiB.+ const first = await startTransfer(store, rootA, A, 0, 4 * MIB)await expect(store.fail(first.transfer.id, A)).rejects.toMatchObject({code: 'storage_write_failed',})+ // The second admission's sweep retries the first trash once.+ const second = await startTransfer(store, rootB, B, 1, 4 * MIB)await expect(store.fail(second.transfer.id, B)).rejects.toMatchObject({code: 'storage_write_failed',})- expect(trashRemovals).toHaveLength(2)+ expect(trashRemovals).toHaveLength(3)expect(trashIn(rootA)).toHaveLength(1)expect(trashIn(rootB)).toHaveLength(1)expect(storedSourceBytes(hostRoot)).toBe(8 * MIB)@@ -376,30 +386,63 @@ describe('GFS download store: a copy left under a trash name stays charged (R5-F})describe('GFS download store: reuse that crosses expiry while hashing (R5-F2)', () => {- it('SOL-R5-REUSE-CROSS-EXPIRY: a copy that expires while it is hashed is a cache miss and gets no pin', async () => {+ it('REUSE-MARGIN: a copy with 59 minutes left is a cache miss and gets no pin, while a copy with 61 minutes left is reused and pinned', async () => {+ coherentVolume(1000n, MARGIN + 1000n)+ const store = await openStore()+ const minute = 60_000+ const short = await completedCopy(store, rootA, A, 20, 30, {+ expiresAt: new Date(Date.now() + 59 * minute).toISOString(),+ })+ const long = await completedCopy(store, rootA, A, 21, 30, {+ expiresAt: new Date(Date.now() + 61 * minute).toISOString(),+ })+ const pinnedIds = () =>+ [...(store as unknown as { pins: Map<string, Set<string>> }).pins.values()].flatMap(ids => [+ ...ids,+ ])++ await expect(+ store.reusableReceipt(A, sourceFor(20), 30, { retentionOwnerId: 'task-b' })+ ).resolves.toBeUndefined()+ expect(pinnedIds()).toEqual([])+ // Witness: the same call for the copy with 61 minutes left returns its receipt and pins it.+ await expect(+ store.reusableReceipt(A, sourceFor(21), 30, { retentionOwnerId: 'task-b' })+ ).resolves.toMatchObject({ id: long.receipt.id, expiresAt: long.receipt.expiresAt })+ expect(pinnedIds()).toEqual([long.receipt.id])+ // The miss does not remove the short copy: it stays readable until it expires.+ await expect(store.readManagedFile(short.receipt.path, A)).resolves.toEqual(short.bytes)+ })++ it('SOL-R5-REUSE-CROSS-EXPIRY: a copy that enters its last hour while it is hashed is a cache miss and gets no pin', async () => {coherentVolume(1000n, MARGIN + 1000n)const store = await openStore()const held = await completedCopy(store, rootA, A, 0, 30, { owner: 'task-a' })- // Witness: before expiry the copy is reused and pinned for task-b.+ // A copy with less than this left is not reused.+ const reuseMargin = 60 * 60_000+ // Witness: before the margin the copy is reused and pinned for task-b.await expect(store.reusableReceipt(A, sourceFor(0), 30, { retentionOwnerId: 'task-b' })).resolves.toMatchObject({ id: held.receipt.id })expect(pinOwners(store)).toEqual([`${A}/task-a`, `${A}/task-b`])- vi.setSystemTime(Date.parse(held.receipt.expiresAt) - 1)+ vi.setSystemTime(Date.parse(held.receipt.expiresAt) - reuseMargin - 1)const gate = pauseNextSourceRead()const reusing = store.reusableReceipt(A, sourceFor(0), 30, {retentionOwnerId: 'task-c',deadlineMs: Date.now() + 60_000,signal: new AbortController().signal,})- // Witness: the hash read was reached before expiry and paused there.+ // Witness: the hash read was reached before the margin and paused there.await gate.reached- vi.setSystemTime(Date.parse(held.receipt.expiresAt))+ vi.setSystemTime(Date.parse(held.receipt.expiresAt) - reuseMargin)gate.resume()await expect(reusing).resolves.toBeUndefined()expect(pinOwners(store)).toEqual([`${A}/task-a`, `${A}/task-b`])+ // The miss does not shorten the copy's life: it is readable until it expires.+ await expect(store.readManagedFile(held.receipt.path, A)).resolves.toEqual(held.bytes)+ vi.setSystemTime(Date.parse(held.receipt.expiresAt))await expect(store.readManagedFile(held.receipt.path, A)).rejects.toMatchObject({code: 'download_expired',})diff --git a/mcp-host/src/internalTools/gfsFilePolicy.test.ts b/mcp-host/src/internalTools/gfsFilePolicy.test.tsindex e2edfcde7..a4ed74173 100644--- a/mcp-host/src/internalTools/gfsFilePolicy.test.ts+++ b/mcp-host/src/internalTools/gfsFilePolicy.test.ts@@ -26,7 +26,7 @@ describe('GFS file policy', () => {inlineTextBytes: 8192,metadataBytes: 64 * 1024,errorBytes: 8 * 1024,- storagePercent: 85,+ storagePercent: 70,callerActiveDownloads: 1,retentionMs: 7 * 24 * 60 * 60 * 1000,})@@ -97,7 +97,7 @@ describe('GFS file policy', () => {const policy = await import('./gfsFilePolicy')- expect(policy.GFS_FILE_LIMITS.storagePercent).toBe(85)+ expect(policy.GFS_FILE_LIMITS.storagePercent).toBe(70)expect(Object.keys(policy.GFS_FILE_LIMITS)).not.toContain('storageBytes')expect(policy.REMOVED_GFS_STORAGE_VARIABLES).toEqual(['MCP_HOST_GFS_DOWNLOAD_STORAGE_BYTES',diff --git a/mcp-host/src/internalTools/gfsFilePolicy.ts b/mcp-host/src/internalTools/gfsFilePolicy.tsindex 977e48f15..f32920ddf 100644--- a/mcp-host/src/internalTools/gfsFilePolicy.ts+++ b/mcp-host/src/internalTools/gfsFilePolicy.ts@@ -41,12 +41,14 @@ export const GFS_FILE_LIMITS = Object.freeze({/*** Share of the volume holding the Host root that retained downloads may* occupy: `floor(volumeTotalBytes * storagePercent / 100)`, measured with- * statfs at every admission.+ * statfs at every admission. The default 70 plus the store's 15% free-space+ * floor leaves 15% of the volume that the cache never fills, so on a volume+ * with a 5% root reservation the budget, not the floor, is the cache's limit.*/storagePercent: configuredGfsInteger('MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT',process.env.MCP_HOST_GFS_DOWNLOAD_STORAGE_PERCENT,- 85,+ 70,100),callerActiveDownloads: configuredGfsInteger(diff --git a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts b/mcp-host/src/internalTools/gfsImageInput.integration.test.tsindex e1483a1f1..d1b2febb7 100644--- a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts+++ b/mcp-host/src/internalTools/gfsImageInput.integration.test.ts@@ -26,6 +26,14 @@ const { sdkCreate, clientFactory } = vi.hoisted(() => ({clientFactory: vi.fn(),}))+// statfs reports the volume sized to its free space, so the disk's occupancy+// never meets the store's free-space floor.+vi.mock('node:fs/promises', async original => {+ const actual = await original<typeof fs>()+ const { freeSpaceSizedStatfs } = await import('../__tests__/fixtures/gfsStoreTestKit')+ return { ...actual, statfs: freeSpaceSizedStatfs(actual.statfs) }+})+// Only external transport boundaries are replaced. Production client, tool,// native adapter, message construction and provider serialization all execute.vi.mock('openai', () => ({diff --git a/mcp-host/src/internalTools/gfsSpaceGuidance.ts b/mcp-host/src/internalTools/gfsSpaceGuidance.tsindex e9a186287..c827c8b6e 100644--- a/mcp-host/src/internalTools/gfsSpaceGuidance.ts+++ b/mcp-host/src/internalTools/gfsSpaceGuidance.ts@@ -16,8 +16,9 @@ export const GFS_DISK_FULL_GUIDANCE =// Deleting downloaded copies by hand cannot admit the download. The budget// refusal comes when evicting every copy no running task protects would still// not make room, or when a planned eviction fails to remove a copy, which-// stays charged until a sweep removes it. The per-caller cap, checked before-// any eviction, counts only copies the caller's running tasks protect. Only+// stays charged until a sweep removes it. The per-caller and Host-wide+// protected caps, checked before any eviction, count only copies that running+// tasks protect. Only// tasks finishing (or being cancelled), copies expiring and sweeps free that// space.export const GFS_CACHE_FULL_GUIDANCE =diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.jsonindex 8018257da..6499b729f 100644--- a/scripts/ci/pinned-vitest-cases/mcp-host.json+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json@@ -159,14 +159,6 @@"suite": "taskExecutor.gfsLiveApproval.integration.test.ts","fullName": "one shell approval covers the rest of the turn: two shell calls, one approval"},- {- "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",- "fullName": "approval parity: http_request asks once per turn and the approval covers the next call"- },- {- "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",- "fullName": "approval parity: shell_exec asks once per turn and the approval covers the next call"- },{"suite": "taskExecutor.gfsPreparation.test.ts","fullName": "TaskExecutor prepares admitted GFS files before its first model call asks for each configured clerum__gfs_download despite turn and persistent approval"@@ -297,11 +289,11 @@},{"suite": "gfsDownloadStoreBoundaries.test.ts",- "fullName": "GFS download store boundaries: quota and capacity Q12: when every copy is pinned admission fails with the quota code and succeeds after release"+ "fullName": "GFS download store boundaries: quota and capacity Q12: when pins fill the Host protected cap admission fails with the quota code and succeeds after release"},{"suite": "gfsDownloadStoreBoundaries.test.ts",- "fullName": "GFS download store boundaries: expiry E1: reuse matches until one millisecond before expiry"+ "fullName": "GFS download store boundaries: expiry E1: reuse matches until one millisecond before the copy has one hour left"},{"suite": "gfsDownloadStoreBoundaries.test.ts",@@ -777,7 +769,7 @@},{"suite": "gfsDownloadStoreBudget.test.ts",- "fullName": "GFS download store budget: share of the workspace volume BUD-1: the default 85% budget admits exactly up to floor(total * 85 / 100) bytes"+ "fullName": "GFS download store budget: share of the workspace volume BUD-1: the default 70% budget admits exactly up to floor(total * 70 / 100) bytes"},{"suite": "gfsDownloadStoreBudget.test.ts",@@ -805,7 +797,7 @@},{"suite": "gfsDownloadStoreBudget.test.ts",- "fullName": "GFS download store budget: no per-caller storage or file-count limit BUD-8: the quota denial is identical whoever fills the budget"+ "fullName": "GFS download store budget: no per-caller storage or file-count limit BUD-8: the quota denial is identical whoever holds the pinned bytes"},{"suite": "gfsDownloadStoreBudget.test.ts",@@ -897,11 +889,11 @@},{"suite": "gfsDownloadStoreProtected.test.ts",- "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) SOL-R3-PINS: one caller pins up to the cap, exactly, and other callers keep admitting"+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) SOL-R3-PINS: one caller pins up to its cap, exactly, and another caller is still admitted beside it"},{"suite": "gfsDownloadStoreProtected.test.ts",- "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) a refused caller gets the same error object as a full Host budget"+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) a refused caller gets the same error object as a Host-wide refusal"},{"suite": "gfsDownloadStoreProtected.test.ts",@@ -1097,11 +1089,11 @@},{"suite": "gfsDownloadStoreTrashAndExpiry.test.ts",- "fullName": "GFS download store: a copy left under a trash name stays charged (R5-F1) SOL-R5-TRASH-CHARGE: expired pinned copies whose trash cannot be removed keep their charge until cleanup succeeds"+ "fullName": "GFS download store: a copy left under a trash name stays charged (R5-F1) SOL-R5-TRASH-CHARGE: an expired pinned copy and an unpinned one whose trash cannot be removed keep their charge until cleanup succeeds"},{"suite": "gfsDownloadStoreTrashAndExpiry.test.ts",- "fullName": "GFS download store: reuse that crosses expiry while hashing (R5-F2) SOL-R5-REUSE-CROSS-EXPIRY: a copy that expires while it is hashed is a cache miss and gets no pin"+ "fullName": "GFS download store: reuse that crosses expiry while hashing (R5-F2) SOL-R5-REUSE-CROSS-EXPIRY: a copy that enters its last hour while it is hashed is a cache miss and gets no pin"},{"suite": "gfsManagedImage.test.ts",@@ -1173,11 +1165,11 @@},{"suite": "gfsDownloadStoreRemovalCharge.test.ts",- "fullName": "GFS download store removal charges RC-REUSE-UNPINNED-false: post-hash expiry is a miss with and without a new retention owner"+ "fullName": "GFS download store removal charges RC-REUSE-UNPINNED-false: crossing the one-hour reuse margin after the hash is a miss with and without a new retention owner"},{"suite": "gfsDownloadStoreRemovalCharge.test.ts",- "fullName": "GFS download store removal charges RC-REUSE-UNPINNED-true: post-hash expiry is a miss with and without a new retention owner"+ "fullName": "GFS download store removal charges RC-REUSE-UNPINNED-true: crossing the one-hour reuse margin after the hash is a miss with and without a new retention owner"},{"suite": "gfsDownloadStoreRemovalCharge.test.ts",@@ -1295,10 +1287,6 @@"suite": "config.executionLimits.test.ts","fullName": "tool timeouts bounded to the executable timer range (#1021) U11: the accepted maximum runs a shell call and one more is refused by the timer guard"},- {- "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",- "fullName": "one turn-wide shell approval auto-approves cron_manage and http_request for the rest of the turn"- },{"suite": "taskExecutor.gfsPreparation.test.ts","fullName": "TaskExecutor prepares admitted GFS files before its first model call maps the real tool output for a disk_full store refusal to its preparation code"@@ -1370,5 +1358,217 @@{"suite": "gfsDownloadStoreRestart.test.ts","fullName": "GFS download store restart R1: restarting on a volume written by the dev store never blocks admission: restarted by the dev Host, every record quarantined (#1019)"+ },+ {+ "suite": "gfsDownloadStoreBoundaries.test.ts",+ "fullName": "GFS download store boundaries: caller isolation U1b: readManagedFile refuses an adopted copy recorded for the same caller from the index, before touching its directory"+ },+ {+ "suite": "gfsDownloadStoreBoundaries.test.ts",+ "fullName": "GFS download store boundaries: caller isolation U1b: readManagedFilePrefix refuses an adopted copy recorded for the same caller from the index, before touching its directory"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: free-space floor value FSF-1: the floor is 15% of the volume, and never less than 16 MiB"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: free-space floor value FSF-1b: a restore aims for the floor plus 5% of the volume"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: admission keeps the free-space floor FSF-2: an admission is refused as disk_full while every copy is pinned, and admitted after evicting released copies"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-3: below the floor, cleanupExpired evicts the adopted copy and then the least recently used ones up to the floor plus 5%, and keeps pinned copies and active transfers"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-4: above the floor, cleanupExpired evicts nothing while it still removes an expired copy"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-5: a deficit larger than every unpinned copy is restored partially, and the evictions are kept"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-6: when statfs rejects, cleanupExpired keeps its sweep result, evicts nothing and logs the failure"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-6: when two invalid readings, cleanupExpired keeps its sweep result, evicts nothing and logs the failure"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: the periodic sweep restores the free-space floor FSF-7: a restore continues past the floor and stops once the floor plus 5% is free"+ },+ {+ "suite": "gfsDownloadStoreFreeSpaceFloor.test.ts",+ "fullName": "GFS download store: startup restores the free-space floor FSF-8: a store initialized on a volume below the floor evicts adopted copies before it is available"+ },+ {+ "suite": "gfsDownloadStoreProtected.test.ts",+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) HOST-CAP: two callers each under their own cap are refused once their sum would exceed three quarters of the budget, and admitted after a release"+ },+ {+ "suite": "gfsDownloadStoreRecovery.test.ts",+ "fullName": "GFS download store: a store directory made unwritable is still removed RCV-1c: an adopted copy whose .gfs-downloads parent is chmod 0500 is evicted after the owner rwx is restored on the parent, and its charge is released"+ },+ {+ "suite": "gfsDownloadStoreRecovery.test.ts",+ "fullName": "GFS download store: a store directory made unwritable is still removed RCV-1d: a symlink swapped in for the .gfs-downloads parent after a refused rename is not followed, and the removal is refused"+ },+ {+ "suite": "gfsDownloadStoreRemovalCharge.test.ts",+ "fullName": "GFS download store removal charges RC-EXPIRED-KEPT: an expired entry whose removal fails in the sweep stays indexed and charged"+ },+ {+ "suite": "gfsDownloadStoreTrashAndExpiry.test.ts",+ "fullName": "GFS download store: reuse that crosses expiry while hashing (R5-F2) REUSE-MARGIN: a copy with 59 minutes left is a cache miss and gets no pin, while a copy with 61 minutes left is reused and pinned"+ },+ {+ "suite": "gfsRuntime.test.ts",+ "fullName": "GFS runtime bootstrap the default cycle runs cleanup every five minutes"+ },+ {+ "suite": "gfsRuntime.test.ts",+ "fullName": "GFS runtime bootstrap stop resolves when the cycle in flight never settles: it waits stopTimeoutMs, warns and closes the store"+ },+ {+ "suite": "gfsDownloadStoreProtected.test.ts",+ "fullName": "GFS download store: a caller protects at most half the budget (R3-F1) TWO-CALLERS-HOST-CAP: two callers that together protect three quarters of the budget refuse a third caller with the Host-wide scope"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "a plain shell_exec approval covers its later calls in the same task and a new task asks again"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "a plain http_request approval covers its later calls in the same task and a new task asks again"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "a plain cron_manage approval covers its later calls in the same task and a new task asks again"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "an \"always\" shell_exec approval covers its calls in later tasks"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "an \"always\" http_request approval covers its calls in later tasks"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "an \"always\" cron_manage approval covers its calls in later tasks"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "a plain shell_exec approval covers 31 later shell calls of the same task"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "a plain shell_exec approval still covers shell after a clerum__gfs_download card in the same task"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "after a shell_exec approval, cron_manage and http_request still suspend for their own approval"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "on a stateless host every cron_manage create asks, and an approved create card runs only that call"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "asks again for a plain shell_exec approval when a cold resume rebuilds the conversation mid-task"+ },+ {+ "suite": "taskExecutor.gfsLiveApproval.integration.test.ts",+ "fullName": "asks again for an \"always\" shell_exec approval after a cold resume rebuilds the conversation"+ },+ {+ "suite": "authorizationScope.test.ts",+ "fullName": "approval authorization scope scopes turn-wide consent for shell_exec to the tool itself, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals a plain approve() of shell_exec stores the tool for the current task only, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals an \"always\" approve() of shell_exec stores the tool for later tasks, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals a plain approve() of http_request stores the tool for the current task only, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals an \"always\" approve() of http_request stores the tool for later tasks, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals a plain approve() of cron_manage stores the tool for the current task only, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals an \"always\" approve() of cron_manage stores the tool for later tasks, without a wildcard"+ },+ {+ "suite": "conversation.test.ts",+ "fullName": "ConversationManager — session-scoped tool approvals startTurn clears plain session-scoped approvals and keeps \"always\" ones"+ },+ {+ "suite": "approvalController.test.ts",+ "fullName": "ApprovalController should bypass every tool outside the session-scoped set when wildcard '*' is in auto_approved_tools"+ },+ {+ "suite": "approvalController.test.ts",+ "fullName": "ApprovalController does not let wildcard '*' cover shell_exec, http_request or cron_manage"+ },+ {+ "suite": "sessionScopedApproval.test.ts",+ "fullName": "session-scoped approvals next to turn-wide MCP approvals an MCP turn-wide approval adds '*' that does not cover shell_exec"+ },+ {+ "suite": "sessionScopedApproval.test.ts",+ "fullName": "session-scoped approvals next to turn-wide MCP approvals keeps MCP behaviour: '*' covers other MCP tools in the turn and the server prefix persists"+ },+ {+ "suite": "sessionScopedApproval.test.ts",+ "fullName": "session-scoped approvals next to turn-wide MCP approvals shell_exec approval after an MCP '*' covers shell_exec without revoking '*'"+ },+ {+ "suite": "sessionScopedApproval.test.ts",+ "fullName": "session-scoped approvals next to turn-wide MCP approvals keeps workflow_trigger turn-wide: '*' covers other gated tools this turn, and a new turn asks again"+ },+ {+ "suite": "sessionScopedApproval.test.ts",+ "fullName": "session-scoped approvals next to turn-wide MCP approvals keeps workflow_trigger \"always\" approval across turns through its tool name"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval after a plain cron_manage list approval in the task, create still suspends with exact scope"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval after an \"always\" cron_manage approval, enable still suspends in a later task"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval after an MCP approval adds '*', create still suspends with exact scope"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval approving a forced create card stores no approval, so the next create asks again"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval on a non-stateless host a plain cron_manage approval covers create for the rest of the task"+ },+ {+ "suite": "statelessCronForcedApproval.test.ts",+ "fullName": "stateless cron_manage create/enable forced approval keeps the cron-sourced gate-only path: create suspends and every other call proceeds"}]