Skip to content

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

TypeScript

fix(gfs): charge rejected reads and source-bound images

Alfredo Lopez22e593bd1dfe
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 77595a5ff..412f0840f 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.371",
+ "version": "0.1.372",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.371",
+ "version": "0.1.372",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 1e5649454..c6d454df5 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.371",
+ "version": "0.1.372",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts
index 711acfbb2..6a3aa6ee4 100644
--- a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts
+++ b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts
@@ -4,6 +4,7 @@ import type {
ToolCompleteEvent,
ToolStartEvent,
} from '../../../progress/types.js'
+import { VisualInputBudget } from '../../../visualInput/policy'
import { makeFakeConversation } from '../../conversation/__testing__/makeFakeConversation'
import { LlmError, LlmErrorCode } from '../../errors'
import type { ReasoningPort, Tool, ToolRegistry } from '../../interfaces'
@@ -85,6 +86,55 @@ function createMockRegistry(tools: Tool[]): ToolRegistry {
// ─── Loop Control Tests ────────────────────────────────────
describe('runToolUseLoop — loop control', () => {
+ it('counts source-bound composer images without counting retained GFS images twice', async () => {
+ const budget = new VisualInputBudget(1000, 1000)
+ const config = buildLoopConfig({
+ reasoning: createMockReasoning([
+ { type: 'tool_calls', calls: [{ id: 'tc_1', name: 'noop', arguments: {} }] },
+ { type: 'text', content: 'done' },
+ ]),
+ toolRegistry: createMockRegistry([createMockTool('noop')]),
+ safety: new BasicSafety(),
+ events: new SimpleEventEmitter(),
+ conversation: makeFakeConversation(),
+ })
+ config.visualInput = {
+ budget,
+ resolveCapability: async () => ({ status: 'unknown' }),
+ }
+
+ await runToolUseLoop(config, [
+ {
+ role: 'user',
+ content: 'inspect',
+ contentParts: [
+ { type: 'text', text: 'inspect' },
+ {
+ type: 'image',
+ mimeType: 'image/png',
+ data: 'YQ==',
+ source: { kind: 'attachment', attachmentId: 'user-image', messageId: 'msg-1' },
+ },
+ {
+ type: 'image',
+ mimeType: 'image/png',
+ data: 'Yg==',
+ source: {
+ kind: 'gfs',
+ drive: 'main',
+ resourceId: 'a'.repeat(32),
+ gfsUri: `gfs://main/${'a'.repeat(32)}`,
+ version: 1,
+ name: 'gfs.png',
+ },
+ },
+ ],
+ },
+ ])
+
+ expect(budget.residentBytes).toBe(8)
+ })
+
it('should return response when reasoning produces text', async () => {
const reasoning = createMockReasoning([{ type: 'text', content: 'Hello world' }])
const config = buildLoopConfig({
diff --git a/mcp-host/src/core/orchestration/toolUseLoop.ts b/mcp-host/src/core/orchestration/toolUseLoop.ts
index eb9e5f9b9..00c1a242a 100644
--- a/mcp-host/src/core/orchestration/toolUseLoop.ts
+++ b/mcp-host/src/core/orchestration/toolUseLoop.ts
@@ -335,7 +335,7 @@ export async function runToolUseLoop(
for (const message of messages) {
for (const part of message.contentParts ?? []) {
- if (part.type === 'image' && !part.source)
+ if (part.type === 'image' && part.source?.kind !== 'gfs')
config.visualInput?.budget.observeExternalImage(part.data)
}
}
diff --git a/mcp-host/src/internalTools/gfsClient.test.ts b/mcp-host/src/internalTools/gfsClient.test.ts
index 45d0cb6a5..73016b41f 100644
--- a/mcp-host/src/internalTools/gfsClient.test.ts
+++ b/mcp-host/src/internalTools/gfsClient.test.ts
@@ -443,7 +443,7 @@ describe('GFS binary content snapshots', () => {
result.reservation.release()
})
- it('cancels a chunked overrun before retaining extra bytes', async () => {
+ it('cancels a chunked overrun without retaining extra bytes or refunding read work', async () => {
const cancel = vi.fn()
const { client, budget } = contentHarness(Buffer.from('abc'), {
response: () =>
@@ -463,7 +463,31 @@ describe('GFS binary content snapshots', () => {
})
expect(cancel).toHaveBeenCalled()
expect(budget.residentBytes).toBe(0)
- expect(budget.readBytes).toBe(3)
+ expect(budget.readBytes).toBe(8)
+ })
+
+ it('charges rejected first chunks across retries and stops at the turn read limit', async () => {
+ const { client, fetchFn } = contentHarness(Buffer.from('abc'), {
+ response: () =>
+ new Response(new Uint8Array(Buffer.from('over')), {
+ headers: { 'x-gfs-uri': FILE_URI, 'x-gfs-version': '3' },
+ }),
+ })
+ const budget = new VisualInputBudget(200_000, 8)
+
+ await expect(client.read(READ_ARGS, { budget })).rejects.toMatchObject({
+ code: 'limit_exceeded',
+ })
+ expect(budget.readBytes).toBe(4)
+ await expect(client.read(READ_ARGS, { budget })).rejects.toMatchObject({
+ code: 'limit_exceeded',
+ })
+ expect(budget.readBytes).toBe(8)
+ await expect(client.read(READ_ARGS, { budget })).rejects.toMatchObject({
+ code: 'limit_exceeded',
+ })
+ expect(fetchFn).toHaveBeenCalledTimes(4)
+ expect(budget.residentBytes).toBe(0)
})
it('rejects short content and unsolicited partial responses', async () => {
diff --git a/mcp-host/src/internalTools/gfsContentRead.ts b/mcp-host/src/internalTools/gfsContentRead.ts
index fdf4be657..6b3981a3a 100644
--- a/mcp-host/src/internalTools/gfsContentRead.ts
+++ b/mcp-host/src/internalTools/gfsContentRead.ts
@@ -34,8 +34,9 @@ async function collect(
const next = await reader.read()
if (signal.aborted) throw new VisualInputError('cancelled')
if (next.done) break
- if (next.value.byteLength > maximum - size) throw new VisualInputError('limit_exceeded')
+ // Transport work counts even when this chunk exceeds the file snapshot.
onChunk?.(next.value.byteLength)
+ if (next.value.byteLength > maximum - size) throw new VisualInputError('limit_exceeded')
size += next.value.byteLength
chunks.push(next.value)
}