Skip to content

Commit 0647a31

Browse files
committed
fix(integrations): validate postgresql, mysql, mongodb, neo4j and upstash
1 parent 340861b commit 0647a31

52 files changed

Lines changed: 626 additions & 110 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/docs/content/docs/integrations/mongodb.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Execute find operation on MongoDB collection
5050
| `message` | string | Operation status message |
5151
| `documents` | array | Array of documents returned from the query |
5252
| `documentCount` | number | Number of documents returned |
53+
| `truncated` | boolean | Present and true only when documents were dropped to stay inside the response ceilings \(10,000 documents or 10 MiB\). Absent means the result is complete |
54+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
5355

5456
</ReferenceDetails>
5557

@@ -177,6 +179,8 @@ Execute MongoDB aggregation pipeline
177179
| `message` | string | Operation status message |
178180
| `documents` | array | Array of documents returned from aggregation |
179181
| `documentCount` | number | Number of documents returned |
182+
| `truncated` | boolean | Present and true only when documents were dropped to stay inside the response ceilings \(10,000 documents or 10 MiB\). Absent means the result is complete |
183+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
180184

181185
</ReferenceDetails>
182186

‎apps/docs/content/docs/integrations/mysql.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Execute SELECT query on MySQL database
5151
| `message` | string | Operation status message |
5252
| `rows` | array | Array of rows returned from the query |
5353
| `rowCount` | number | Number of rows returned |
54+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
55+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
5456

5557
</ReferenceDetails>
5658

@@ -143,6 +145,8 @@ Execute raw SQL query on MySQL database
143145
| `message` | string | Operation status message |
144146
| `rows` | array | Array of rows returned from the query |
145147
| `rowCount` | number | Number of rows affected |
148+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
149+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
146150

147151
</ReferenceDetails>
148152

‎apps/docs/content/docs/integrations/neo4j.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Execute MATCH queries to read nodes and relationships from Neo4j graph database.
4646
| `records` | array | Array of records returned from the query |
4747
| `recordCount` | number | Number of records returned |
4848
| `summary` | json | Query execution summary with timing and counters |
49+
| `truncated` | boolean | Present and true only when records were dropped to stay inside the response ceilings \(10,000 records or 10 MiB\). Absent means the result is complete |
50+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
4951

5052
</ReferenceDetails>
5153

@@ -189,6 +191,8 @@ Execute arbitrary Cypher queries on Neo4j graph database for complex operations
189191
| `records` | array | Array of records returned from the query |
190192
| `recordCount` | number | Number of records returned |
191193
| `summary` | json | Execution summary with timing and counters |
194+
| `truncated` | boolean | Present and true only when records were dropped to stay inside the response ceilings \(10,000 records or 10 MiB\). Absent means the result is complete |
195+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
192196

193197
</ReferenceDetails>
194198

‎apps/docs/content/docs/integrations/postgresql.mdx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Execute a SELECT query on PostgreSQL database
4848
| `message` | string | Operation status message |
4949
| `rows` | array | Array of rows returned from the query |
5050
| `rowCount` | number | Number of rows returned |
51+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
52+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
5153

5254
</ReferenceDetails>
5355

@@ -71,6 +73,8 @@ Insert data into PostgreSQL database
7173
| `message` | string | Operation status message |
7274
| `rows` | array | Inserted data \(if RETURNING clause used\) |
7375
| `rowCount` | number | Number of rows inserted |
76+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
77+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
7478

7579
</ReferenceDetails>
7680

@@ -95,6 +99,8 @@ Update data in PostgreSQL database
9599
| `message` | string | Operation status message |
96100
| `rows` | array | Updated data \(if RETURNING clause used\) |
97101
| `rowCount` | number | Number of rows updated |
102+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
103+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
98104

99105
</ReferenceDetails>
100106

@@ -118,6 +124,8 @@ Delete data from PostgreSQL database
118124
| `message` | string | Operation status message |
119125
| `rows` | array | Deleted data \(if RETURNING clause used\) |
120126
| `rowCount` | number | Number of rows deleted |
127+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
128+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
121129

122130
</ReferenceDetails>
123131

@@ -140,6 +148,8 @@ Execute raw SQL query on PostgreSQL database
140148
| `message` | string | Operation status message |
141149
| `rows` | array | Array of rows returned from the query |
142150
| `rowCount` | number | Number of rows affected |
151+
| `truncated` | boolean | Present and true only when rows were dropped to stay inside the response ceilings \(10,000 rows or 10 MiB\). Absent means the result is complete |
152+
| `truncationReason` | string | Which ceiling was hit and how to read the rest |
143153

144154
</ReferenceDetails>
145155

‎apps/sim/blocks/blocks/mongodb.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,14 @@ Return ONLY the MongoDB query filter as valid JSON - no explanations, no markdow
959959
type: 'number',
960960
description: 'Number of documents affected by the operation',
961961
},
962+
truncated: {
963+
type: 'boolean',
964+
description: 'True when the result hit a row or byte ceiling and documents were dropped',
965+
},
966+
truncationReason: {
967+
type: 'string',
968+
description: 'Explanation of the ceiling that truncated the result',
969+
},
962970
insertedId: {
963971
type: 'string',
964972
description: 'ID of the inserted document (single insert)',

‎apps/sim/blocks/blocks/mysql.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ Return ONLY the SQL query - no explanations, no markdown, no extra text.`,
412412
type: 'number',
413413
description: 'Number of rows affected by the operation',
414414
},
415+
truncated: {
416+
type: 'boolean',
417+
description: 'True when the result hit a row or byte ceiling and rows were dropped',
418+
},
419+
truncationReason: {
420+
type: 'string',
421+
description: 'Explanation of the ceiling that truncated the result',
422+
},
415423
},
416424
}
417425

‎apps/sim/blocks/blocks/neo4j.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,14 @@ Return ONLY valid JSON.`,
706706
type: 'number',
707707
description: 'Number of records returned or affected',
708708
},
709+
truncated: {
710+
type: 'boolean',
711+
description: 'True when the result hit a row or byte ceiling and records were dropped',
712+
},
713+
truncationReason: {
714+
type: 'string',
715+
description: 'Explanation of the ceiling that truncated the result',
716+
},
709717
summary: {
710718
type: 'json',
711719
description: 'Execution summary with timing and database change counters',

‎apps/sim/blocks/blocks/postgresql.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ Return ONLY the SQL query - no explanations, no markdown, no extra text.`,
427427
type: 'number',
428428
description: 'Number of rows affected by the operation',
429429
},
430+
truncated: {
431+
type: 'boolean',
432+
description: 'True when the result hit a row or byte ceiling and rows were dropped',
433+
},
434+
truncationReason: {
435+
type: 'string',
436+
description: 'Explanation of the ceiling that truncated the result',
437+
},
430438
tables: {
431439
type: 'array',
432440
description: 'Array of table schemas with columns, keys, and indexes (introspect operation)',

‎apps/sim/lib/internal/mongodb/operations.ts‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,45 @@ import type {
2323
MongodbQueryInput,
2424
MongodbUpdateInput,
2525
} from '@/lib/internal/mongodb/schema'
26+
import {
27+
createResultRowsBudget,
28+
resultTruncationReason,
29+
} from '@/lib/internal/tool-operations/response-limits'
2630

2731
export class MongodbOperationInputError extends Error {}
2832

33+
/**
34+
* Reads documents until the response budget is spent. Leaving the loop early closes the server
35+
* cursor, so an unbounded find or aggregation never materializes past the ceiling.
36+
*/
37+
async function readCappedDocuments(
38+
cursor: AsyncIterable<Document>,
39+
describe: (count: number) => string
40+
) {
41+
const budget = createResultRowsBudget()
42+
const documents: Document[] = []
43+
let truncated = false
44+
for await (const document of cursor) {
45+
if (!budget.admit(document)) {
46+
truncated = true
47+
break
48+
}
49+
documents.push(document)
50+
}
51+
const message = describe(documents.length)
52+
if (!truncated) return { message, documents }
53+
const truncationReason = resultTruncationReason(
54+
documents.length,
55+
'Narrow the filter or page with $skip and $limit to read the rest.'
56+
)
57+
return {
58+
message: `${message} ${truncationReason}`,
59+
documents,
60+
truncated: true,
61+
truncationReason,
62+
}
63+
}
64+
2965
async function withMongodbClient<TResult>(
3066
input: MongodbConnectionConfig,
3167
signal: AbortSignal | undefined,
@@ -71,13 +107,9 @@ export function executeMongodbQuery(input: MongodbQueryInput, signal?: AbortSign
71107
}
72108

73109
cursor = cursor.limit(input.limit || 100)
74-
const documents = await cursor.toArray()
110+
const result = await readCappedDocuments(cursor, (count) => `Found ${count} documents`)
75111
signal?.throwIfAborted()
76-
return {
77-
message: `Found ${documents.length} documents`,
78-
documents,
79-
documentCount: documents.length,
80-
}
112+
return { ...result, documentCount: result.documents.length }
81113
})
82114
}
83115

@@ -92,17 +124,12 @@ export function executeMongodbAggregation(input: MongodbExecuteInput, signal?: A
92124
const pipeline = JSON.parse(input.pipeline) as Document[]
93125

94126
return withMongodbClient(input, signal, async (client) => {
95-
const documents = await client
96-
.db(input.database)
97-
.collection(collectionName)
98-
.aggregate(pipeline, { signal })
99-
.toArray()
127+
const result = await readCappedDocuments(
128+
client.db(input.database).collection(collectionName).aggregate(pipeline, { signal }),
129+
(count) => `Aggregation completed, returned ${count} documents`
130+
)
100131
signal?.throwIfAborted()
101-
return {
102-
message: `Aggregation completed, returned ${documents.length} documents`,
103-
documents,
104-
documentCount: documents.length,
105-
}
132+
return { ...result, documentCount: result.documents.length }
106133
})
107134
}
108135

‎apps/sim/lib/internal/mysql/client.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ describe('MySQL client', () => {
8585
expect(socket.setNoDelay).toHaveBeenCalledWith(true)
8686
})
8787

88+
it('connects unencrypted for ssl=preferred when the server does not offer TLS', async () => {
89+
const connection = { end: vi.fn(), destroy: vi.fn() }
90+
mockCreateConnection
91+
.mockRejectedValueOnce(
92+
Object.assign(new Error('Server does not support secure connection'), {
93+
code: 'HANDSHAKE_NO_SSL_SUPPORT',
94+
})
95+
)
96+
.mockResolvedValueOnce(connection)
97+
98+
await expect(createMysqlConnection({ ...CONNECTION_CONFIG, ssl: 'preferred' })).resolves.toBe(
99+
connection
100+
)
101+
expect(mockCreateConnection).toHaveBeenCalledTimes(2)
102+
expect(mockCreateConnection.mock.calls[1][0].ssl).toBeUndefined()
103+
})
104+
105+
it.each(['required', 'disabled'] as const)(
106+
'does not retry ssl=%s after a handshake failure',
107+
async (ssl) => {
108+
mockCreateConnection.mockRejectedValueOnce(
109+
Object.assign(new Error('Server does not support secure connection'), {
110+
code: 'HANDSHAKE_NO_SSL_SUPPORT',
111+
})
112+
)
113+
114+
await expect(createMysqlConnection({ ...CONNECTION_CONFIG, ssl })).rejects.toThrow(
115+
'Server does not support secure connection'
116+
)
117+
expect(mockCreateConnection).toHaveBeenCalledTimes(1)
118+
}
119+
)
120+
88121
it.each([
89122
new Map([['key', 'value']]),
90123
new Set(['value']),

0 commit comments

Comments
 (0)