Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): the code tool only reads files from its own conversation (#15878)

Hazem Adel25e92b0dbf56
diff --git a/brain/knowledge/flows-execution/chat.md b/brain/knowledge/flows-execution/chat.md
index 8c42354255f..0833f62e60d 100644
--- a/brain/knowledge/flows-execution/chat.md
+++ b/brain/knowledge/flows-execution/chat.md
@@ -41,7 +41,7 @@ A turn is kept alive / reclaimed by three separate mechanisms in `execute-agent-
- **Capability notes are built where `discoveryOnly` is not known, so a prompt can promise tools the worker has stripped.** `getAgentConfig` composes the system prompt in the api, while `discoveryOnly` rides on the job data; before Aug 2026 it never crossed that boundary. Meanwhile the worker strips image tools, email tools and the agent tools on such a run, so the notes claimed all three. Two of the three had been wrong since long before anyone noticed, because each note computed its own availability term. The flag now travels with the config request and the three notes read one shared `actingRun = !dryRun && !discoveryOnly`. Whenever a tool group is gated on a run mode in the worker, the note that advertises it has to be gated on the same term, in one place.
- **Tool output can't be trusted to waive a charge.** A user's own MCP server or `ap_run_code` controls its output's top-level keys, and they reach the persisted part unchanged. Chat billing only honours `billedAtCost` from `ap_web_search` and `ap_generate_image`; a new built-in tool that bills at cost must be added to `TOOLS_THAT_BILL_AT_COST` in `chatBilling`.
- **Credits are checked after every step, not just before the turn.** `runAgentTurn` asks the API (`agentHasCredits`) after each tool step, sending what the turn has used so far (`chatBilling.creditsForTurn`, the same rule end-of-turn billing uses), and stops once the remaining balance no longer covers it. Flat credits are only charged when the turn ends, so without that pending count the balance never moves mid-turn. The check fails open if the RPC errors.
-- **Chat files belong to a conversation, not just a project.** Conversations are private to their user, so a project check is not enough. `saveAgentFile` and `persistAgentAttachments` stamp `conversationId` into the file metadata, and `readAgentFile` refuses a file whose stamp doesn't match. Files saved before the stamp existed can't be read back (image editing can't use them). Any new tool that reads chat files by a model-supplied ID must go through `readAgentFile`.
+- **Chat files belong to a conversation, not just a project.** Conversations are private to their user, so a project check is not enough. `saveAgentFile` and `persistAgentAttachments` stamp `conversationId` into the file metadata, and `readAgentFile` refuses a file whose stamp doesn't match. Files saved before the stamp existed can't be read back (image editing can't use them). Any tool that reads chat files by a model-supplied ID must go through `readConversationFile` (`agent-file-utils.ts`), and any new save that hands the model a file ID must stamp `conversationId`.
- Local dev needs `AP_DB_TYPE=POSTGRES` + Redis; refuses PGLite. **Prefer `AP_EDITION=cloud` over `ee` for chat work.** Cloud boots locally against plain Postgres and Redis with no Autumn, Stripe or license-key config (verified Aug 2026: API healthy, migrations applied, zero billing or license errors), and on Cloud `chatVisibility` returns `planChatEnabled || cloudRolloutOpen || userHasChatted`, so chat is simply **on** while the rollout cap is unfilled. On `ee` it is gated behind `plan.chatEnabled` and you have to get a plan onto the platform first. Note SMTP is usually unset locally, which makes the auth card open on the password form rather than the email-code step. Debug a run with `npm run chat:logs -- <conversationId> [runId]` (needs `LOG_FILE=true`/`AP_LOG_FILE=true` set when the turn ran — otherwise `.evlog/logs` is empty).
- **Chat was renamed to agent in code and DB, but only the storage half.** As of release 0.87.1 (`1823000000000-AddRenamedChatTableCompatViews`) `chat_conversation` → `agent_conversation` and `user_chat_memory` → `user_memory`, the server module moved `ee/chat/` → `ee/agent/` (entry point `agentModule`), the worker dir moved `jobs/ee/chat/` → `jobs/ee/agent/`, shared types moved `core/shared/.../ee/chat/` → `.../ee/agent/`, and `server/utils/src/chat-ai-utils.ts` → `agent-ai-utils.ts`. **The rename is not uniform, and the split is the thing to learn**: files describing chat as a *user-facing surface* deliberately kept their `chat-` names inside `ee/agent/` — `chat-visibility.ts`, `chat-rollout-service.ts`, `chat-rollout-user-entity.ts`, `chat-analytics-sync.ts`, `chat-tool-billing.ts`, `chat-usage-tracker.ts`, `chat-plan-grant.ts`. So a new chat-surface concern keeps the `chat-` prefix; a new stored entity takes `agent_`. The migration also leaves `CREATE OR REPLACE VIEW` compat views at both old table names, so raw SQL against `chat_conversation` still reads fine and will NOT tell you the rename happened — grep the entity, not the database.
- **An AI SDK major bump can typecheck clean while a callback payload silently changed shape.** v7 keeps most v6 option *names* as working deprecated aliases (`system`, `onStepFinish`, `experimental_repairToolCall`, `stepCountIs`, `result.toUIMessageStream`), so the option compiles but the data underneath can differ: `experimental_onToolCallFinish` survived as an alias for `onToolExecutionEnd` while its event lost `durationMs`/`success`/`error` (now `toolExecutionMs` plus a `toolOutput.type === 'tool-result'` discriminator). A type-probe that only names the option passes; you have to exercise each callback's property access. `onStepEnd`'s `content` is also cast to a structural `ContentPartLike` with an `args ?? input` fallback (`agent-ai-utils.ts`), which means a shape change there fails at **runtime**, not compile time — always smoke a real turn after a provider/SDK major.
diff --git a/packages/server/api/src/app/ee/agent/agent-file-utils.ts b/packages/server/api/src/app/ee/agent/agent-file-utils.ts
index 90fd20f908a..49ce724ecdb 100644
--- a/packages/server/api/src/app/ee/agent/agent-file-utils.ts
+++ b/packages/server/api/src/app/ee/agent/agent-file-utils.ts
@@ -1,7 +1,8 @@
+import { ActivepiecesError, ErrorCode, isNil } from '@activepieces/core-utils'
import { CHAT_ALLOWED_MIME_TYPES, FileCompression, FileType } from '@activepieces/shared'
import { UserContent } from 'ai'
import { FastifyBaseLogger } from 'fastify'
-import { fileService } from '../../file/file.service'
+import { fileService, GetDataResponse } from '../../file/file.service'
const TEXT_MIME_TYPES = new Set<string>(
CHAT_ALLOWED_MIME_TYPES.filter((m) => m.startsWith('text/') || m === 'application/json'),
@@ -95,4 +96,16 @@ async function buildUserContentWithFiles({ text, files, attachmentNote }: {
]
}
-export { buildUserContentWithFiles, persistAgentAttachments, buildAttachmentNote }
+async function readConversationFile({ platformId, conversationId, accessibleProjectIds, fileId, log }: { platformId: string, conversationId: string, accessibleProjectIds: string[], fileId: string, log: FastifyBaseLogger }): Promise<GetDataResponse> {
+ const file = await fileService(log).getFileOrThrow({ fileId, type: FileType.FLOW_STEP_FILE })
+ const readableHere = file.platformId === platformId
+ && file.metadata?.['conversationId'] === conversationId
+ && !isNil(file.projectId)
+ && accessibleProjectIds.includes(file.projectId)
+ if (!readableHere) {
+ throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityType: 'file', entityId: fileId } })
+ }
+ return fileService(log).getDataOrThrow({ projectId: file.projectId ?? undefined, fileId, type: FileType.FLOW_STEP_FILE })
+}
+
+export { buildUserContentWithFiles, persistAgentAttachments, buildAttachmentNote, readConversationFile }
diff --git a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
index 2df77a17ca3..42aa6b44896 100644
--- a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
+++ b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
@@ -1,6 +1,7 @@
import { ActivepiecesError, ErrorCode, isNil, sanitizeObjectForPostgresql, spreadIfDefined } from '@activepieces/core-utils'
import { AgentConversationStatus, AgentCreditsLeftRequest, AgentRunSource, FileCompression, FileType, HeartbeatAgentConversationRequest, ReadAgentFileRequest, ReadFlowStepFileResponse, SaveAgentFileRequest, SaveAgentFileResponse, SaveAgentMessagesRequest, UpdateAgentProgressRequest, UpdateProjectContextRequest } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
+import { readConversationFile } from '.././agent-file-utils'
import { agentHelpers } from '.././agent-helpers'
import { chatAnalyticsTelemetry } from '.././chat-analytics-sync'
import { chatToolBilling } from '.././chat-tool-billing'
@@ -38,10 +39,12 @@ export const conversationRpc = (log: FastifyBaseLogger) => ({
},
async readAgentFile(input: ReadAgentFileRequest): Promise<ReadFlowStepFileResponse> {
- const file = await fileService(log).getDataOrThrow({ platformId: input.platformId, fileId: input.fileId, type: FileType.FLOW_STEP_FILE })
- if (file.metadata?.['conversationId'] !== input.conversationId) {
- throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityType: 'file', entityId: input.fileId } })
+ const conversation = await agentHelpers.conversationRepo().findOneBy({ id: input.conversationId, platformId: input.platformId })
+ if (isNil(conversation)) {
+ throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityType: 'conversation', entityId: input.conversationId } })
}
+ const projects = await agentHelpers.getUserProjects({ platformId: input.platformId, userId: conversation.userId, log })
+ const file = await readConversationFile({ platformId: input.platformId, conversationId: input.conversationId, accessibleProjectIds: projects.map((project) => project.id), fileId: input.fileId, log })
return {
data: file.data,
...spreadIfDefined('mimeType', file.metadata?.['mimetype']),
diff --git a/packages/server/api/src/app/ee/agent/tools/agent-tools.ts b/packages/server/api/src/app/ee/agent/tools/agent-tools.ts
index fb06e856be8..d3c694ac5e7 100644
--- a/packages/server/api/src/app/ee/agent/tools/agent-tools.ts
+++ b/packages/server/api/src/app/ee/agent/tools/agent-tools.ts
@@ -17,6 +17,7 @@ import { mcpUtils } from '../../../mcp/tools/mcp-utils'
import { pieceMetadataService } from '../../../pieces/metadata/piece-metadata-service'
import { tableService } from '../../../tables/table/table.service'
import { agentApprovalGate } from '../agent-approval-gate'
+import { readConversationFile } from '../agent-file-utils'
import { agentHelpers } from '../agent-helpers'
import { agentMemoryAi } from '../agent-memory-ai'
import { agentAudit, agentService } from '../agent-service'
@@ -667,9 +668,10 @@ async function executeCrossProjectTool({ toolName, toolInput, platformId, userId
// A large successful read (e.g. a 1.4MB Attio query) is persisted as a .json file and replaced in
// the model context with a compact shape preview + the fileId. The agent then processes the FULL
// data in ap_run_code (inputFileIds → inputs.data) — the blob never floods the context.
-function buildActionRunOffload({ projectId, platformId, pieceName, actionName, log }: {
+function buildActionRunOffload({ projectId, platformId, conversationId, pieceName, actionName, log }: {
projectId: string
platformId?: string
+ conversationId?: string
pieceName: string
actionName: string
log: FastifyBaseLogger
@@ -690,7 +692,7 @@ function buildActionRunOffload({ projectId, platformId, pieceName, actionName, l
type: FileType.FLOW_STEP_FILE,
fileName,
compression: FileCompression.NONE,
- metadata: { mimetype: 'application/json' },
+ metadata: { mimetype: 'application/json', ...spreadIfDefined('conversationId', conversationId) },
}))
if (error || isNil(saved)) {
log.warn({ error, pieceName, actionName }, '[agent] large-result offload failed; falling back to inline truncation')
@@ -746,7 +748,7 @@ async function runAgentAction({ toolInput, projects, availableProjectIds, conver
actionName,
input: parsedInput,
connectionExternalId,
- ...spreadIfDefined('offload', buildActionRunOffload({ projectId: resolvedProjectId, platformId, pieceName: normalizedPiece, actionName, log })),
+ ...spreadIfDefined('offload', buildActionRunOffload({ projectId: resolvedProjectId, platformId, conversationId, pieceName: normalizedPiece, actionName, log })),
log,
})
@@ -817,16 +819,12 @@ async function runAgentCode({ toolInput, projects, platformId, userId, conversat
: []
const inputFiles: { name: string, mimeType: string, base64: string }[] = []
for (const fileId of inputFileIds) {
- const { data: file, error: lookupError } = await tryCatch(() => fileService(log).getFileOrThrow({ fileId, type: FileType.FLOW_STEP_FILE }))
- // Confine to the current conversation's project, not just the platform — otherwise a
- // model-supplied fileId from another project on the same platform could be read.
- if (lookupError || isNil(file) || file.platformId !== platformId || file.projectId !== projectId) {
+ const { data: fileData } = await tryCatch(() => isNil(conversationId)
+ ? fileService(log).getDataOrThrow({ projectId, fileId, type: FileType.FLOW_STEP_FILE })
+ : readConversationFile({ platformId, conversationId, accessibleProjectIds: projects.map((project) => project.id), fileId, log }))
+ if (isNil(fileData)) {
return { text: `❌ Couldn't load attachment ${fileId}. If this is an image you generated, pass its URL into the code and fetch() it instead of using inputFileIds.`, producedFiles: [] }
}
- const { data: fileData, error: dataError } = await tryCatch(() => fileService(log).getDataOrThrow({ projectId: file.projectId ?? undefined, fileId, type: FileType.FLOW_STEP_FILE }))
- if (dataError || isNil(fileData)) {
- return { text: `❌ Couldn't read attachment ${fileId}.`, producedFiles: [] }
- }
const mimeType = isObject(fileData.metadata) && typeof fileData.metadata.mimetype === 'string' ? fileData.metadata.mimetype : 'application/octet-stream'
inputFiles.push({ name: fileData.fileName ?? fileId, mimeType, base64: fileData.data.toString('base64') })
}
@@ -851,7 +849,7 @@ async function runAgentCode({ toolInput, projects, platformId, userId, conversat
return { text: `❌ ${reason}`, producedFiles: [] }
}
- return persistProducedFiles({ output: result.output, projectId, platformId, log })
+ return persistProducedFiles({ output: result.output, projectId, platformId, conversationId, log })
}
function extractFilesFromOutput(output: unknown): { files: RawProducedFile[], rest: unknown } {
@@ -871,10 +869,11 @@ function decodeBase64(value: string): Buffer | undefined {
return buffer.length > 0 ? buffer : undefined
}
-async function persistProducedFiles({ output, projectId, platformId, log }: {
+async function persistProducedFiles({ output, projectId, platformId, conversationId, log }: {
output: unknown
projectId: string
platformId: string
+ conversationId?: string
log: FastifyBaseLogger
}): Promise<RunCodeToolResult> {
const { files, rest } = extractFilesFromOutput(output)
@@ -892,7 +891,7 @@ async function persistProducedFiles({ output, projectId, platformId, log }: {
type: FileType.FLOW_STEP_FILE,
fileName: file.name,
compression: FileCompression.NONE,
- metadata: { mimetype: file.mimeType },
+ metadata: { mimetype: file.mimeType, ...spreadIfDefined('conversationId', conversationId) },
})
const url = await filesService.constructReadUrl({ fileId: saved.id, fileType: saved.type, platformId })
producedFiles.push({ fileId: saved.id, url, mediaType: file.mimeType, fileName: file.name, byteSize: buffer.length })
diff --git a/packages/server/api/src/app/file/file.service.ts b/packages/server/api/src/app/file/file.service.ts
index 96b7893a82f..8c0a81e210f 100644
--- a/packages/server/api/src/app/file/file.service.ts
+++ b/packages/server/api/src/app/file/file.service.ts
@@ -326,7 +326,7 @@ export const fileService = (log: FastifyBaseLogger) => ({
},
})
-type GetDataResponse = {
+export type GetDataResponse = {
metadata?: Record<string, string>
data: Buffer
fileName?: string
diff --git a/packages/server/api/test/integration/cloud/agent/agent.test.ts b/packages/server/api/test/integration/cloud/agent/agent.test.ts
index 7383950ce7e..be8f32d260f 100644
--- a/packages/server/api/test/integration/cloud/agent/agent.test.ts
+++ b/packages/server/api/test/integration/cloud/agent/agent.test.ts
@@ -1,10 +1,11 @@
-import { DefaultProjectRole, PersistedAgentPartType, PersistedAgentRole } from '@activepieces/shared'
+import { DefaultProjectRole, FileCompression, FileType, PersistedAgentPartType, PersistedAgentRole } from '@activepieces/shared'
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { redisConnections } from '../../../../src/app/database/redis-connections'
import { agentHelpers } from '../../../../src/app/ee/agent/agent-helpers'
import { executeCrossProjectTool } from '../../../../src/app/ee/agent/tools/agent-tools'
+import { fileService } from '../../../../src/app/file/file.service'
import { createMemberContext, createTestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'
@@ -336,6 +337,35 @@ describe('Chat Conversations API', () => {
})
})
+ describe('Code tool file inputs', () => {
+ it('refuses a file saved in another conversation, even in the same project', async () => {
+ const ctx = await createTestContext(app, { plan: { chatEnabled: true } })
+ const mine = (await ctx.post(CONVERSATIONS_URL, { title: 'Mine' })).json().id
+ const other = (await ctx.post(CONVERSATIONS_URL, { title: 'Other' })).json().id
+ const file = await fileService(app.log).save({
+ projectId: ctx.project.id,
+ platformId: ctx.platform.id,
+ data: Buffer.from('{"secret":true}'),
+ size: 15,
+ type: FileType.FLOW_STEP_FILE,
+ fileName: 'secret.json',
+ compression: FileCompression.NONE,
+ metadata: { mimetype: 'application/json', conversationId: other },
+ })
+
+ const result = await executeCrossProjectTool({
+ toolName: 'ap_run_code',
+ toolInput: { code: 'export const code = async (inputs) => inputs.data', inputFileIds: [file.id] },
+ platformId: ctx.platform.id,
+ userId: ctx.user.id,
+ conversationId: mine,
+ log: app.log,
+ })
+
+ expect(JSON.stringify(result)).toContain(`Couldn't load attachment ${file.id}`)
+ })
+ })
+
describe('Get messages', () => {
it('returns empty messages for a new conversation', async () => {
const ctx = await createTestContext(app, { plan: { chatEnabled: true } })
diff --git a/packages/server/api/test/unit/app/ee/agent/conversation-rpc-read-file.test.ts b/packages/server/api/test/unit/app/ee/agent/conversation-rpc-read-file.test.ts
index 2bffa6a5899..5dfd0c476c1 100644
--- a/packages/server/api/test/unit/app/ee/agent/conversation-rpc-read-file.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/conversation-rpc-read-file.test.ts
@@ -1,11 +1,21 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
-const { mockGetData } = vi.hoisted(() => ({
+const { mockGetFile, mockGetData, mockFindConversation, mockGetUserProjects } = vi.hoisted(() => ({
+ mockGetFile: vi.fn(),
mockGetData: vi.fn(),
+ mockFindConversation: vi.fn(),
+ mockGetUserProjects: vi.fn(),
}))
vi.mock('../../../../../src/app/file/file.service', () => ({
- fileService: () => ({ getDataOrThrow: mockGetData }),
+ fileService: () => ({ getFileOrThrow: mockGetFile, getDataOrThrow: mockGetData }),
+}))
+
+vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({
+ agentHelpers: {
+ conversationRepo: () => ({ findOneBy: mockFindConversation }),
+ getUserProjects: mockGetUserProjects,
+ },
}))
const { conversationRpc } = await import('../../../../../src/app/ee/agent/rpc/conversation-rpc')
@@ -14,32 +24,41 @@ const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() } as
describe('readAgentFile', () => {
beforeEach(() => {
- mockGetData.mockReset()
+ mockGetFile.mockReset().mockResolvedValue(fileRecord({}))
+ mockGetData.mockReset().mockResolvedValue({ data: Buffer.from('png'), fileName: 'cat.png', metadata: { mimetype: 'image/png', conversationId: 'conv-1' } })
+ mockFindConversation.mockReset().mockResolvedValue({ id: 'conv-1', userId: 'user-1' })
+ mockGetUserProjects.mockReset().mockResolvedValue([{ id: 'project-1' }])
})
- it('returns a file saved in this conversation, looked up on its platform', async () => {
- mockGetData.mockResolvedValue({ data: Buffer.from('png'), fileName: 'cat.png', metadata: { mimetype: 'image/png', conversationId: 'conv-1' } })
-
+ it('returns a file saved in this conversation, in a project the user can access', async () => {
const file = await read()
- expect(mockGetData).toHaveBeenCalledWith(expect.objectContaining({ platformId: 'platform-1', fileId: 'file-1' }))
+ expect(mockGetUserProjects).toHaveBeenCalledWith(expect.objectContaining({ platformId: 'platform-1', userId: 'user-1' }))
expect(file).toEqual({ data: Buffer.from('png'), mimeType: 'image/png', fileName: 'cat.png' })
})
it('refuses a file from another conversation', async () => {
- mockGetData.mockResolvedValue({ data: Buffer.from('png'), metadata: { mimetype: 'image/png', conversationId: 'conv-2' } })
+ mockGetFile.mockResolvedValue(fileRecord({ conversationId: 'conv-2' }))
await expect(read()).rejects.toThrow()
+ expect(mockGetData).not.toHaveBeenCalled()
})
it('refuses a file with no conversation recorded', async () => {
- mockGetData.mockResolvedValue({ data: Buffer.from('png'), metadata: { mimetype: 'image/png' } })
+ mockGetFile.mockResolvedValue(fileRecord({ conversationId: null }))
await expect(read()).rejects.toThrow()
})
- it('refuses a file that is not on the platform', async () => {
- mockGetData.mockRejectedValue(new Error('File not found'))
+ it('refuses a file in a project the user can no longer access', async () => {
+ mockGetUserProjects.mockResolvedValue([{ id: 'project-2' }])
+
+ await expect(read()).rejects.toThrow()
+ expect(mockGetData).not.toHaveBeenCalled()
+ })
+
+ it('refuses a file on another platform', async () => {
+ mockGetFile.mockResolvedValue(fileRecord({ platformId: 'platform-2' }))
await expect(read()).rejects.toThrow()
})
@@ -48,3 +67,7 @@ describe('readAgentFile', () => {
function read(): ReturnType<ReturnType<typeof conversationRpc>['readAgentFile']> {
return conversationRpc(log).readAgentFile({ platformId: 'platform-1', conversationId: 'conv-1', fileId: 'file-1' })
}
+
+function fileRecord({ conversationId = 'conv-1', platformId = 'platform-1' }: { conversationId?: string | null, platformId?: string }): Record<string, unknown> {
+ return { id: 'file-1', platformId, projectId: 'project-1', metadata: { mimetype: 'image/png', conversationId } }
+}