Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/sim/lib/internal/mongodb/execute-tool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getErrorMessage } from '@sim/utils/errors'
import type { z } from 'zod'
import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits'
import {
executeMongodbAggregation,
executeMongodbDelete,
Expand Down Expand Up @@ -40,6 +41,7 @@ async function executeOperation<TInput>(
return Response.json(result)
} catch (error) {
signal?.throwIfAborted()
if (isPayloadSizeLimitError(error)) throw error
if (error instanceof MongodbOperationInputError) {
return Response.json({ error: error.message }, { status: 400 })
}
Expand Down
27 changes: 21 additions & 6 deletions apps/sim/lib/internal/mongodb/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ import type {
MongodbQueryInput,
MongodbUpdateInput,
} from '@/lib/internal/mongodb/schema'
import { createResponseRowsBudget } from '@/lib/internal/tool-operations/response-limits'

export class MongodbOperationInputError extends Error {}

/**
* Reads documents one at a time, failing with the tool response size error as soon as they cannot
* fit the response. Leaving the loop early closes the server cursor, so an oversized result is
* never read into memory in full.
*/
async function readDocuments(cursor: AsyncIterable<Document>, toolId: string) {
const budget = createResponseRowsBudget(toolId)
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
const documents: Document[] = []
for await (const document of cursor) {
budget.admit(document)
documents.push(document)
}
return documents
}

async function withMongodbClient<TResult>(
input: MongodbConnectionConfig,
signal: AbortSignal | undefined,
Expand Down Expand Up @@ -71,7 +87,7 @@ export function executeMongodbQuery(input: MongodbQueryInput, signal?: AbortSign
}

cursor = cursor.limit(input.limit || 100)
const documents = await cursor.toArray()
const documents = await readDocuments(cursor, 'mongodb_query')
signal?.throwIfAborted()
return {
message: `Found ${documents.length} documents`,
Expand All @@ -92,11 +108,10 @@ export function executeMongodbAggregation(input: MongodbExecuteInput, signal?: A
const pipeline = JSON.parse(input.pipeline) as Document[]

return withMongodbClient(input, signal, async (client) => {
const documents = await client
.db(input.database)
.collection(collectionName)
.aggregate(pipeline, { signal })
.toArray()
const documents = await readDocuments(
client.db(input.database).collection(collectionName).aggregate(pipeline, { signal }),
'mongodb_execute'
)
signal?.throwIfAborted()
return {
message: `Aggregation completed, returned ${documents.length} documents`,
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/internal/neo4j/execute-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
neo4jQueryContract,
neo4jUpdateContract,
} from '@/lib/api/contracts/tools/databases/neo4j'
import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits'
import {
executeNeo4jCreate,
executeNeo4jDelete,
Expand All @@ -35,6 +36,7 @@ async function executeOperation<C extends AnyApiRouteContract>(
return Response.json(await execute(parsed.data, signal))
} catch (error) {
signal?.throwIfAborted()
if (isPayloadSizeLimitError(error)) throw error
if (error instanceof Neo4jOperationInputError) {
return Response.json({ error: `Query validation failed: ${error.message}` }, { status: 400 })
}
Expand Down
54 changes: 46 additions & 8 deletions apps/sim/lib/internal/neo4j/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('@/lib/internal/neo4j/client', () => ({
createNeo4jDriver: mocks.createDriver,
}))

import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits'
import { executeNeo4jQuery, Neo4jOperationInputError } from '@/lib/internal/neo4j/operations'

const INPUT = {
Expand All @@ -24,7 +25,8 @@ const INPUT = {
parameters: {},
}

function queryResult() {
/** A driver `Result` streaming `count` records, recording whether the rest was discarded. */
function streamingResult(count: number, value = 'Ada') {
const updates = {
nodesCreated: 0,
nodesDeleted: 0,
Expand All @@ -38,19 +40,32 @@ function queryResult() {
constraintsAdded: 0,
constraintsRemoved: 0,
}
const summary = {
resultAvailableAfter: { toNumber: () => 2 },
resultConsumedAfter: { toNumber: () => 3 },
counters: { updates: () => updates },
}
const state = { pulled: 0, discarded: false }
const record = { keys: ['name'], get: () => value }
return {
records: [{ keys: ['name'], get: vi.fn().mockReturnValue('Ada') }],
summary: {
resultAvailableAfter: { toNumber: () => 2 },
resultConsumedAfter: { toNumber: () => 3 },
counters: { updates: () => updates },
},
state,
[Symbol.asyncIterator]: () => ({
next: async () => {
if (state.pulled >= count) return { done: true, value: summary }
state.pulled++
return { done: false, value: record }
},
return: async () => {
state.discarded = true
return { done: true, value: summary }
},
}),
}
}

describe('Neo4j operations', () => {
beforeEach(() => {
mocks.run.mockResolvedValue(queryResult())
mocks.run.mockImplementation(() => streamingResult(1))
mocks.sessionClose.mockResolvedValue(undefined)
mocks.driverClose.mockResolvedValue(undefined)
mocks.createDriver.mockResolvedValue({
Expand All @@ -59,6 +74,29 @@ describe('Neo4j operations', () => {
})
})

it('returns every record of a result that fits the response limit', async () => {
const result = streamingResult(20_000)
mocks.run.mockReturnValue(result)

const response = await executeNeo4jQuery(INPUT)

expect(response).toMatchObject({ recordCount: 20_000, summary: { resultAvailableAfter: 2 } })
expect(result.state.discarded).toBe(false)
})

it('fails with the response size error and discards the rest once records cannot fit', async () => {
const result = streamingResult(1_000, 'x'.repeat(64 * 1024))
mocks.run.mockReturnValue(result)

const error = await executeNeo4jQuery(INPUT).catch((caught: unknown) => caught)

expect(isPayloadSizeLimitError(error)).toBe(true)
expect(error).toMatchObject({ label: 'neo4j_query response body' })
expect(result.state.discarded).toBe(true)
expect(result.state.pulled).toBeLessThan(1_000)
expect(mocks.sessionClose).toHaveBeenCalled()
})

it('rejects invalid Cypher before opening a driver', async () => {
await expect(executeNeo4jQuery({ ...INPUT, cypherQuery: ' ' })).rejects.toBeInstanceOf(
Neo4jOperationInputError
Expand Down
58 changes: 42 additions & 16 deletions apps/sim/lib/internal/neo4j/operations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger } from '@sim/logger'
import type { QueryResult, Session } from 'neo4j-driver'
import type { Record as Neo4jRecord, Result, ResultSummary, Session } from 'neo4j-driver'
import type {
Neo4jCreateRequest,
Neo4jDeleteRequest,
Expand All @@ -11,6 +11,7 @@ import type {
} from '@/lib/api/contracts/tools/databases/neo4j'
import { createNeo4jDriver } from '@/lib/internal/neo4j/client'
import { convertNeo4jValue } from '@/lib/internal/neo4j/values'
import { createResponseRowsBudget } from '@/lib/internal/tool-operations/response-limits'
import type { Neo4jNodeSchema, Neo4jRelationshipSchema } from '@/tools/neo4j/types'

const logger = createLogger('Neo4jOperations')
Expand Down Expand Up @@ -43,21 +44,44 @@ function bindSessionAbort(session: Session, signal?: AbortSignal): () => void {
return () => signal.removeEventListener('abort', abort)
}

function projectRecords(result: QueryResult): Array<Record<string, unknown>> {
return result.records.map((record) => {
const projected: Record<string, unknown> = {}
for (const key of record.keys) {
if (typeof key === 'string') projected[key] = convertNeo4jValue(record.get(key))
function projectRecord(record: Neo4jRecord): Record<string, unknown> {
const projected: Record<string, unknown> = {}
for (const key of record.keys) {
if (typeof key === 'string') projected[key] = convertNeo4jValue(record.get(key))
}
return projected
}

/**
* Streams the records, failing with the tool response size error as soon as they cannot fit the
* response. Returning the iterator early tells the server to discard the rest, so an oversized
* result is never buffered in full. `delete` returns no records, so it only waits for the summary.
*/
async function readResult(result: Result, kind: StatementKind) {
if (kind === 'delete') return { records: [], summary: await result.summary() }
const budget = createResponseRowsBudget(`neo4j_${kind}`)
const records: Array<Record<string, unknown>> = []
const iterator = result[Symbol.asyncIterator]()
try {
let next = await iterator.next()
while (!next.done) {
const projected = projectRecord(next.value)
budget.admit(projected)
records.push(projected)
next = await iterator.next()
}
return projected
})
return { records, summary: next.value }
} catch (error) {
await iterator.return?.().catch(() => undefined)
throw error
}
}

function projectSummary(result: QueryResult) {
const updates = result.summary.counters.updates()
function projectSummary(summary: ResultSummary) {
const updates = summary.counters.updates()
return {
resultAvailableAfter: result.summary.resultAvailableAfter.toNumber(),
resultConsumedAfter: result.summary.resultConsumedAfter.toNumber(),
resultAvailableAfter: summary.resultAvailableAfter.toNumber(),
resultConsumedAfter: summary.resultConsumedAfter.toNumber(),
counters: {
nodesCreated: updates.nodesCreated,
nodesDeleted: updates.nodesDeleted,
Expand All @@ -74,9 +98,11 @@ function projectSummary(result: QueryResult) {
}
}

function statementResponse(kind: StatementKind, result: QueryResult) {
const records = projectRecords(result)
const summary = projectSummary(result)
function statementResponse(
kind: StatementKind,
{ records, summary: resultSummary }: Awaited<ReturnType<typeof readResult>>
) {
const summary = projectSummary(resultSummary)
switch (kind) {
case 'query':
return {
Expand Down Expand Up @@ -128,7 +154,7 @@ async function executeStatement(input: StatementInput, kind: StatementKind, sign
const session = driver.session({ database: input.database })
const unbindAbort = bindSessionAbort(session, signal)
try {
const result = await session.run(input.cypherQuery, input.parameters ?? {})
const result = await readResult(session.run(input.cypherQuery, input.parameters ?? {}), kind)
signal?.throwIfAborted()
return statementResponse(kind, result)
} finally {
Expand Down
42 changes: 42 additions & 0 deletions apps/sim/lib/internal/tool-operations/response-limits.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest'
import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits'
import {
createResponseRowsBudget,
MAX_TOOL_RESPONSE_BODY_BYTES,
} from '@/lib/internal/tool-operations/response-limits'

/** Admits every row, returning the error the budget threw, if any. */
function admitAll(rows: unknown[]): unknown {
const budget = createResponseRowsBudget('test_tool')
try {
for (const row of rows) budget.admit(row)
return undefined
} catch (error) {
return error
}
}

describe('response rows budget', () => {
it('admits any number of rows while their array still fits the response limit', () => {
expect(admitAll(Array.from({ length: 50_000 }, (_, id) => ({ id })))).toBeUndefined()
})

it('fails exactly where the serialized array passes the response limit', () => {
const fitting = ['x'.repeat(MAX_TOOL_RESPONSE_BODY_BYTES - 4)]
expect(Buffer.byteLength(JSON.stringify(fitting))).toBe(MAX_TOOL_RESPONSE_BODY_BYTES)
expect(admitAll(fitting)).toBeUndefined()

const error = admitAll(['x'.repeat(MAX_TOOL_RESPONSE_BODY_BYTES - 3)])
expect(isPayloadSizeLimitError(error)).toBe(true)
expect(error).toMatchObject({
label: 'test_tool response body',
maxBytes: MAX_TOOL_RESPONSE_BODY_BYTES,
})
})

it('counts UTF-8 bytes, so multibyte text fails on its encoded size', () => {
const row = '漢'.repeat(MAX_TOOL_RESPONSE_BODY_BYTES / 3)
expect(row.length).toBeLessThan(MAX_TOOL_RESPONSE_BODY_BYTES)
expect(isPayloadSizeLimitError(admitAll([row]))).toBe(true)
})
})
26 changes: 26 additions & 0 deletions apps/sim/lib/internal/tool-operations/response-limits.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
import { PayloadSizeLimitError } from '@/lib/core/utils/stream-limits'

/** Maximum inline tool response size; binary file outputs use stored descriptors. */
export const MAX_TOOL_RESPONSE_BODY_BYTES = 10 * 1024 * 1024

/**
* Fails a streamed result as soon as its serialized rows alone exceed
* {@link MAX_TOOL_RESPONSE_BODY_BYTES}. Bytes are counted the way the response serializes them
* (UTF-8, plus the array's brackets and commas), and the body around the rows is larger still, so
* this fails exactly the results the executor's response limit would reject, but before the rest
* of the result is read into memory. The error is the same one that limit throws.
*/
export function createResponseRowsBudget(toolId: string) {
let bytes = 1
return {
admit(row: unknown): void {
const serialized = JSON.stringify(row)
bytes += (serialized === undefined ? 4 : Buffer.byteLength(serialized, 'utf8')) + 1
if (bytes > MAX_TOOL_RESPONSE_BODY_BYTES) {
throw new PayloadSizeLimitError({
label: `${toolId} response body`,
maxBytes: MAX_TOOL_RESPONSE_BODY_BYTES,
observedBytes: bytes,
})
}
},
}
}
3 changes: 2 additions & 1 deletion apps/sim/tools/upstash/redis_delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UPSTASH_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
import type { ToolConfig } from '@/tools/types'
import type { UpstashRedisDeleteParams, UpstashRedisDeleteResponse } from '@/tools/upstash/types'
import { upstashPathSegment } from '@/tools/upstash/utils'

export const upstashRedisDeleteTool: ToolConfig<
UpstashRedisDeleteParams,
Expand Down Expand Up @@ -34,7 +35,7 @@ export const upstashRedisDeleteTool: ToolConfig<
},

request: {
url: (params) => `${params.restUrl}/del/${encodeURIComponent(params.key)}`,
url: (params) => `${params.restUrl}/del/${upstashPathSegment(params.key, 'key')}`,
method: 'POST',
headers: (params) => ({
Authorization: `Bearer ${params.restToken}`,
Expand Down
3 changes: 2 additions & 1 deletion apps/sim/tools/upstash/redis_exists.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UPSTASH_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
import type { ToolConfig } from '@/tools/types'
import type { UpstashRedisExistsParams, UpstashRedisExistsResponse } from '@/tools/upstash/types'
import { upstashPathSegment } from '@/tools/upstash/utils'

export const upstashRedisExistsTool: ToolConfig<
UpstashRedisExistsParams,
Expand Down Expand Up @@ -35,7 +36,7 @@ export const upstashRedisExistsTool: ToolConfig<
},

request: {
url: (params) => `${params.restUrl}/exists/${encodeURIComponent(params.key)}`,
url: (params) => `${params.restUrl}/exists/${upstashPathSegment(params.key, 'key')}`,
method: 'GET',
headers: (params) => ({
Authorization: `Bearer ${params.restToken}`,
Expand Down
4 changes: 3 additions & 1 deletion apps/sim/tools/upstash/redis_expire.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UPSTASH_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
import type { ToolConfig } from '@/tools/types'
import type { UpstashRedisExpireParams, UpstashRedisExpireResponse } from '@/tools/upstash/types'
import { upstashInteger, upstashPathSegment } from '@/tools/upstash/utils'

export const upstashRedisExpireTool: ToolConfig<
UpstashRedisExpireParams,
Expand Down Expand Up @@ -40,7 +41,8 @@ export const upstashRedisExpireTool: ToolConfig<
},

request: {
url: (params) => `${params.restUrl}/expire/${encodeURIComponent(params.key)}/${params.seconds}`,
url: (params) =>
`${params.restUrl}/expire/${upstashPathSegment(params.key, 'key')}/${upstashInteger(params.seconds, 'seconds')}`,
method: 'GET',
headers: (params) => ({
Authorization: `Bearer ${params.restToken}`,
Expand Down
Loading
Loading