Skip to content

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

TypeScript

fix(mcp-host): preserve attachment page continuation during pruning

Alfredo Lopez90bdc2e83522
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index a7fba88cd..10cb1d7b7 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.481",
+ "version": "0.1.482",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.481",
+ "version": "0.1.482",
"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 0724ea0e8..30b90dae3 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.481",
+ "version": "0.1.482",
"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/extensions/__tests__/prePrune.attachmentPageCollapse.test.ts b/mcp-host/src/core/extensions/__tests__/prePrune.attachmentPageCollapse.test.ts
index fa62b46c5..ab524cff0 100644
--- a/mcp-host/src/core/extensions/__tests__/prePrune.attachmentPageCollapse.test.ts
+++ b/mcp-host/src/core/extensions/__tests__/prePrune.attachmentPageCollapse.test.ts
@@ -122,6 +122,54 @@ describe('C17 collapseEarlierAttachmentPages — direct pass', () => {
expect(out[5].content).toBe(messages[5].content)
})
+ it('collapses paged results with nextOffset and keeps the validated continuation fields', () => {
+ const messages = conversation()
+ messages[2] = attachmentToolResult(
+ 'tc_old',
+ wrapped(
+ nativePage({
+ limit: 'page_budget',
+ nextOffset: 65536,
+ })
+ )
+ )
+ const out = prePruneModule.collapseEarlierAttachmentPages(messages)
+ const stub = JSON.parse(out[2].content.split('\n')[1]!)
+ expect(stub.text).toBe(stubMarker())
+ expect(stub.limit).toBe('page_budget')
+ expect(stub.nextOffset).toBe(65536)
+ expect(out[5]).toBe(messages[5])
+ expect(() => validateToolLinkages(out)).not.toThrow()
+ })
+
+ it('does not start another turn when a tool supplies a synthetic image user message', () => {
+ const messages = conversation()
+ messages.push({ role: 'user', content: 'tool-supplied image', imageOrigin: 'tool_result' })
+ const out = prePruneModule.collapseEarlierAttachmentPages(messages)
+ expect(JSON.parse(out[2].content).text).toBe(stubMarker())
+ expect(out[5]).toBe(messages[5])
+ expect(out[5].content).toBe(messages[5].content)
+ })
+
+ it('leaves corrupt continuation metadata untouched with a valid continuation witness', () => {
+ const valid = conversation()
+ valid[2] = attachmentToolResult('tc_old', nativePage({ nextOffset: 65536 }))
+ expect(JSON.parse(prePruneModule.collapseEarlierAttachmentPages(valid)[2].content).text).toBe(
+ stubMarker()
+ )
+ for (const fields of [
+ { nextOffset: 1 },
+ { nextOffset: -1 },
+ { nextOffset: '65536' },
+ { nextOffset: 65536, truncated: false },
+ { limit: 'invented_limit' },
+ ]) {
+ const messages = conversation()
+ messages[2] = attachmentToolResult('tc_old', nativePage(fields))
+ expect(prePruneModule.collapseEarlierAttachmentPages(messages)[2]).toBe(messages[2])
+ }
+ })
+
it('keeps untouched messages referentially identical and originals immutable', () => {
const messages = conversation()
const before = structuredClone(messages)
diff --git a/mcp-host/src/core/extensions/prePrune.ts b/mcp-host/src/core/extensions/prePrune.ts
index 93db8b091..f6af298af 100644
--- a/mcp-host/src/core/extensions/prePrune.ts
+++ b/mcp-host/src/core/extensions/prePrune.ts
@@ -220,6 +220,8 @@ interface AttachmentTextPage {
kind: 'text'
byteRange: { offset: number; length: number }
truncated: boolean
+ limit?: 'max_bytes' | 'page_budget' | 'turn_budget'
+ nextOffset?: number
text: string
}
@@ -257,7 +259,7 @@ export function collapseEarlierAttachmentPages(messages: ChatMessage[]): ChatMes
function findLastUserIndex(messages: ChatMessage[]): number {
for (let i = messages.length - 1; i >= 0; i--) {
- if (messages[i].role === 'user') return i
+ if (messages[i].role === 'user' && messages[i].imageOrigin !== 'tool_result') return i
}
return -1
}
@@ -286,7 +288,18 @@ function isAttachmentTextPage(value: unknown): value is AttachmentTextPage {
if (!value || typeof value !== 'object' || Array.isArray(value)) return false
const obj = value as Record<string, unknown>
const keys = Object.keys(obj)
- if (keys.length !== 6) return false
+ if (keys.length < 6 || keys.length > 8) return false
+ const allowed = new Set([
+ 'attachmentId',
+ 'byteRange',
+ 'kind',
+ 'referenceId',
+ 'text',
+ 'truncated',
+ 'limit',
+ 'nextOffset',
+ ])
+ if (keys.some(key => !allowed.has(key))) return false
for (const key of ['attachmentId', 'byteRange', 'kind', 'referenceId', 'text', 'truncated']) {
if (!(key in obj)) return false
}
@@ -311,6 +324,20 @@ function isAttachmentTextPage(value: unknown): value is AttachmentTextPage {
) {
return false
}
+ if (!Number.isSafeInteger(offset + length)) return false
+ if (
+ 'limit' in obj &&
+ !['max_bytes', 'page_budget', 'turn_budget'].includes(obj.limit as string)
+ ) {
+ return false
+ }
+ if (
+ 'nextOffset' in obj &&
+ (obj.truncated !== true ||
+ !Number.isSafeInteger(obj.nextOffset) ||
+ obj.nextOffset !== offset + length)
+ )
+ return false
return true
}
@@ -325,6 +352,8 @@ function buildAttachmentPageStub(parsed: {
kind: 'text',
byteRange: { offset: page.byteRange.offset, length: page.byteRange.length },
truncated: page.truncated,
+ ...(page.limit !== undefined ? { limit: page.limit } : {}),
+ ...(page.nextOffset !== undefined ? { nextOffset: page.nextOffset } : {}),
text: ATTACHMENT_PAGE_COLLAPSE_MARKER,
})
if (wrapperSanitized === null) return stub
diff --git a/tests/e2e/integration/chat-body-budget-parity.test.ts b/tests/e2e/integration/chat-body-budget-parity.test.ts
index 50d62f0da..69cd57c15 100644
--- a/tests/e2e/integration/chat-body-budget-parity.test.ts
+++ b/tests/e2e/integration/chat-body-budget-parity.test.ts
@@ -181,14 +181,14 @@ describe('chat body budget parity across rpc-proxy, mcp-host and the composer (#
)
})
- it('the Host admission default equals the per-file credit ceiling', () => {
- const [, literal] = soleMatch(
+ it('the Host admission default and configured maximum equal the per-file credit ceiling', () => {
+ const [, literal, maximum] = soleMatch(
sources.hostConfig,
- /getExecutionLimit\('CLERUM_ATTACHMENT_FILE_MAX_BYTES', ([0-9_]+)\)/,
- 'mcp-host config CLERUM_ATTACHMENT_FILE_MAX_BYTES default'
- )
- expect(Number(literal.replaceAll('_', ''))).toBe(
- constantValue(sources.mcpHost, 'MAX_FILE_DECODED_BYTES', 'mcp-host')
+ /getExecutionLimit\(\s*'CLERUM_ATTACHMENT_FILE_MAX_BYTES',\s*([0-9_]+),\s*false,\s*([0-9_]+)\s*\)/,
+ 'mcp-host config CLERUM_ATTACHMENT_FILE_MAX_BYTES bounds'
)
+ const ceiling = constantValue(sources.mcpHost, 'MAX_FILE_DECODED_BYTES', 'mcp-host')
+ expect(Number(literal.replaceAll('_', ''))).toBe(ceiling)
+ expect(Number(maximum.replaceAll('_', ''))).toBe(ceiling)
})
})
diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.json
index 5a53b1c45..882564dd9 100644
--- a/tests/e2e/package-lock.json
+++ b/tests/e2e/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.99",
+ "version": "0.1.100",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-e2e-tests",
- "version": "0.1.99",
+ "version": "0.1.100",
"devDependencies": {
"@clerum/llm-provider-attempt-contract": "file:../../packages/llm-provider-attempt-contract",
"@types/jsonwebtoken": "^9.0.10",
diff --git a/tests/e2e/package.json b/tests/e2e/package.json
index 8cdb46303..77b8a7ffd 100644
--- a/tests/e2e/package.json
+++ b/tests/e2e/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.99",
+ "version": "0.1.100",
"private": true,
"type": "module",
"scripts": {