Skip to content

Commit 3b4d028

Browse files
committed
fix(aws): round-trip typed DynamoDB cursors and allow percentile-only CloudWatch statistics
Encode large-number and binary keys in lastEvaluatedKey as {N}/{B} and decode them in exclusiveStartKey, so pagination resumes on tables whose keys a JS number or string cannot carry. Item output is unchanged. Let get_metric_statistics take extendedStatistics alone: statistics is optional and the contract requires one of the two.
1 parent 005cb97 commit 3b4d028

8 files changed

Lines changed: 186 additions & 39 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Get statistics for a CloudWatch metric over a time range
239239
| `startTime` | number | Yes | Start time as Unix epoch seconds |
240240
| `endTime` | number | Yes | End time as Unix epoch seconds |
241241
| `period` | number | Yes | Granularity in seconds \(e.g., 60, 300, 3600\) |
242-
| `statistics` | array | Yes | Statistics to retrieve \(Average, Sum, Minimum, Maximum, SampleCount\) |
242+
| `statistics` | array | No | Statistics to retrieve \(Average, Sum, Minimum, Maximum, SampleCount\). Required unless extendedStatistics is set, which takes precedence |
243243
| `dimensions` | string | No | Dimensions as JSON \(e.g., \{"InstanceId": "i-1234"\}\) |
244244
| `extendedStatistics` | array | No | Percentile statistics to retrieve \(e.g., \["p99", "p95"\]\). AWS accepts either Statistics or ExtendedStatistics, so when set these are requested instead of statistics |
245245
| `unit` | string | No | Only return data reported with this unit \(e.g., Percent, Milliseconds\). Omit to return all units |

‎apps/sim/lib/api/contracts/tools/aws/cloudwatch-get-metric-statistics.ts‎

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,33 @@ import type {
77
import { defineRouteContract } from '@/lib/api/contracts/types'
88
import { validateAwsRegion } from '@/lib/core/security/input-validation'
99

10-
const GetMetricStatisticsSchema = z.object({
11-
region: z
12-
.string()
13-
.min(1, 'AWS region is required')
14-
.refine((v) => validateAwsRegion(v).isValid, {
15-
message: 'Invalid AWS region format (e.g., us-east-1, eu-west-2)',
16-
}),
17-
accessKeyId: z.string().min(1, 'AWS access key ID is required'),
18-
secretAccessKey: z.string().min(1, 'AWS secret access key is required'),
19-
namespace: z.string().min(1, 'Namespace is required'),
20-
metricName: z.string().min(1, 'Metric name is required'),
21-
startTime: z.coerce.number().int(),
22-
endTime: z.coerce.number().int(),
23-
period: z.coerce.number().int().min(1),
24-
statistics: z
25-
.array(z.enum(['Average', 'Sum', 'Minimum', 'Maximum', 'SampleCount']))
26-
.min(1, 'At least one statistic is required')
27-
.max(5, 'At most 5 statistics are allowed per GetMetricStatistics call'),
28-
dimensions: z.string().optional(),
29-
extendedStatistics: z.array(z.string().min(1)).max(10).optional(),
30-
unit: z.string().optional(),
31-
})
10+
const GetMetricStatisticsSchema = z
11+
.object({
12+
region: z
13+
.string()
14+
.min(1, 'AWS region is required')
15+
.refine((v) => validateAwsRegion(v).isValid, {
16+
message: 'Invalid AWS region format (e.g., us-east-1, eu-west-2)',
17+
}),
18+
accessKeyId: z.string().min(1, 'AWS access key ID is required'),
19+
secretAccessKey: z.string().min(1, 'AWS secret access key is required'),
20+
namespace: z.string().min(1, 'Namespace is required'),
21+
metricName: z.string().min(1, 'Metric name is required'),
22+
startTime: z.coerce.number().int(),
23+
endTime: z.coerce.number().int(),
24+
period: z.coerce.number().int().min(1),
25+
statistics: z
26+
.array(z.enum(['Average', 'Sum', 'Minimum', 'Maximum', 'SampleCount']))
27+
.max(5, 'At most 5 statistics are allowed per GetMetricStatistics call')
28+
.optional(),
29+
dimensions: z.string().optional(),
30+
extendedStatistics: z.array(z.string().min(1)).max(10).optional(),
31+
unit: z.string().optional(),
32+
})
33+
.refine((body) => Boolean(body.statistics?.length || body.extendedStatistics?.length), {
34+
message: 'Provide statistics or extendedStatistics',
35+
path: ['statistics'],
36+
})
3237

3338
const GetMetricStatisticsResponseSchema = z.object({
3439
success: z.literal(true),

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ function clientReturning(output: Record<string, unknown>) {
1414
return client
1515
}
1616

17+
/** A client that records the marshalled request input DynamoDB would receive. */
18+
function clientCapturing(onInput: (input: Record<string, unknown>) => void) {
19+
const client = createDynamoDBClient({
20+
region: 'us-east-1',
21+
accessKeyId: 'AKIAEXAMPLE',
22+
secretAccessKey: 'secret',
23+
})
24+
client.middlewareStack.add(
25+
() => async (args) => {
26+
onInput(args.input as Record<string, unknown>)
27+
return { output: { $metadata: {}, Items: [], Count: 0 }, response: {} }
28+
},
29+
{ step: 'build' }
30+
)
31+
return client
32+
}
33+
1734
describe('DynamoDB item unmarshalling', () => {
1835
it('returns JSON-serializable items for sets, binary and integers beyond 2^53', async () => {
1936
const client = clientReturning({
@@ -51,4 +68,45 @@ describe('DynamoDB item unmarshalling', () => {
5168
})
5269
client.destroy()
5370
})
71+
72+
it('round-trips large-number and binary keys through the pagination cursor', async () => {
73+
const pageClient = clientReturning({
74+
Count: 0,
75+
Items: [],
76+
LastEvaluatedKey: {
77+
pk: { S: 'USER#1' },
78+
sk: { N: '1234567890123456789' },
79+
bin: { B: new Uint8Array([104, 105]) },
80+
age: { N: '42' },
81+
},
82+
})
83+
const firstPage = await queryItems(pageClient, 'Users', 'pk = :pk', {
84+
expressionAttributeValues: { ':pk': 'USER#1' },
85+
})
86+
const cursor = await Response.json(firstPage.lastEvaluatedKey).json()
87+
expect(cursor).toEqual({
88+
pk: 'USER#1',
89+
sk: { N: '1234567890123456789' },
90+
bin: { B: 'aGk=' },
91+
age: 42,
92+
})
93+
94+
let sentStartKey: unknown
95+
const client = clientCapturing((input) => {
96+
sentStartKey = input.ExclusiveStartKey
97+
})
98+
await queryItems(client, 'Users', 'pk = :pk', {
99+
expressionAttributeValues: { ':pk': 'USER#1' },
100+
exclusiveStartKey: cursor,
101+
})
102+
103+
expect(sentStartKey).toEqual({
104+
pk: { S: 'USER#1' },
105+
sk: { N: '1234567890123456789' },
106+
bin: { B: new Uint8Array([104, 105]) },
107+
age: { N: '42' },
108+
})
109+
pageClient.destroy()
110+
client.destroy()
111+
})
54112
})

‎apps/sim/lib/internal/dynamodb/client.ts‎

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,39 @@ import {
33
DeleteCommand,
44
DynamoDBDocumentClient,
55
GetCommand,
6+
NumberValue,
67
PutCommand,
78
QueryCommand,
89
ScanCommand,
910
UpdateCommand,
1011
} from '@aws-sdk/lib-dynamodb'
12+
import { isRecordLike } from '@sim/utils/object'
1113
import { awsClientConfig } from '@/lib/internal/aws/credentials'
1214
import type { DynamoDBConnectionConfig, DynamoDBTableSchema } from '@/tools/dynamodb/types'
1315

1416
type DynamoDBReturnValues = 'NONE' | 'ALL_OLD' | 'UPDATED_OLD' | 'ALL_NEW' | 'UPDATED_NEW'
1517

1618
/**
1719
* Keeps integers beyond `Number.MAX_SAFE_INTEGER` (and other values a JS number cannot hold)
18-
* as their exact DynamoDB string, since the SDK default turns them into a `BigInt` that the
19-
* JSON response cannot serialize.
20+
* as a `NumberValue` holding the exact DynamoDB string, since the SDK default turns them into a
21+
* `BigInt` that the JSON response cannot serialize.
2022
*/
21-
function unwrapNumber(value: string): number | string {
23+
function unwrapNumber(value: string): number | NumberValue {
2224
const parsed = Number(value)
23-
if (!Number.isFinite(parsed)) return value
24-
if (Number.isInteger(parsed) && !Number.isSafeInteger(parsed)) return value
25+
if (!Number.isFinite(parsed) || (Number.isInteger(parsed) && !Number.isSafeInteger(parsed))) {
26+
return NumberValue.from(value)
27+
}
2528
return parsed
2629
}
2730

2831
/**
2932
* Converts unmarshalled DynamoDB values into JSON-serializable shapes: string, number and
30-
* binary sets become arrays, and binary values become base64 strings. Without this a set
31-
* serializes as `{}` and a binary value as an object of byte indexes.
33+
* binary sets become arrays, binary values become base64 strings, and numbers a JS number
34+
* cannot hold become their exact string. Without this a set serializes as `{}` and a binary
35+
* value as an object of byte indexes.
3236
*/
3337
function toJsonSafe(value: unknown): unknown {
38+
if (value instanceof NumberValue) return value.toString()
3439
if (value instanceof Set) return Array.from(value, toJsonSafe)
3540
if (value instanceof Uint8Array) return Buffer.from(value).toString('base64')
3641
if (Array.isArray(value)) return value.map(toJsonSafe)
@@ -46,6 +51,42 @@ function toJsonSafeRecord(
4651
return value ? (toJsonSafe(value) as Record<string, unknown>) : undefined
4752
}
4853

54+
/**
55+
* Encodes a `LastEvaluatedKey` so it can be passed back as `exclusiveStartKey`. Key
56+
* attributes are always scalar (S, N or B), so strings and safe numbers stay as-is while a
57+
* number a JS number cannot hold becomes `{ N }` and a binary key becomes `{ B }` (base64):
58+
* plain strings would otherwise be sent back as S and fail the key type check.
59+
*/
60+
function toCursor(key: Record<string, unknown> | undefined): Record<string, unknown> | undefined {
61+
if (!key) return undefined
62+
return Object.fromEntries(
63+
Object.entries(key).map(([name, value]) => {
64+
if (value instanceof NumberValue) return [name, { N: toJsonSafe(value) }]
65+
if (value instanceof Uint8Array) return [name, { B: toJsonSafe(value) }]
66+
return [name, value]
67+
})
68+
)
69+
}
70+
71+
const DYNAMODB_NUMBER = /^-?\d+(\.\d+)?([eE][+-]?\d+)?$/
72+
73+
/** Restores the `{ N }` and `{ B }` cursor values written by {@link toCursor}. */
74+
function fromCursor(key: Record<string, unknown>): Record<string, unknown> {
75+
return Object.fromEntries(
76+
Object.entries(key).map(([name, value]) => {
77+
if (isRecordLike(value) && Object.keys(value).length === 1) {
78+
if (typeof value.N === 'string' && DYNAMODB_NUMBER.test(value.N)) {
79+
return [name, NumberValue.from(value.N)]
80+
}
81+
if (typeof value.B === 'string') {
82+
return [name, new Uint8Array(Buffer.from(value.B, 'base64'))]
83+
}
84+
}
85+
return [name, value]
86+
})
87+
)
88+
}
89+
4990
export function createDynamoDBClient(config: DynamoDBConnectionConfig): DynamoDBDocumentClient {
5091
const client = new DynamoDBClient({
5192
...awsClientConfig(config.region, config.accessKeyId, config.secretAccessKey),
@@ -152,7 +193,7 @@ export async function queryItems(
152193
}),
153194
...(options?.indexName && { IndexName: options.indexName }),
154195
...(options?.limit && { Limit: options.limit }),
155-
...(options?.exclusiveStartKey && { ExclusiveStartKey: options.exclusiveStartKey }),
196+
...(options?.exclusiveStartKey && { ExclusiveStartKey: fromCursor(options.exclusiveStartKey) }),
156197
...(options?.scanIndexForward !== undefined && { ScanIndexForward: options.scanIndexForward }),
157198
...(options?.projectionExpression && { ProjectionExpression: options.projectionExpression }),
158199
...(options?.consistentRead !== undefined && { ConsistentRead: options.consistentRead }),
@@ -163,7 +204,7 @@ export async function queryItems(
163204
items: (response.Items ?? []).map((item) => toJsonSafeRecord(item) ?? {}),
164205
count: response.Count || 0,
165206
scannedCount: response.ScannedCount ?? null,
166-
lastEvaluatedKey: toJsonSafeRecord(response.LastEvaluatedKey),
207+
lastEvaluatedKey: toCursor(response.LastEvaluatedKey),
167208
}
168209
}
169210

@@ -200,7 +241,7 @@ export async function scanItems(
200241
ExpressionAttributeValues: options.expressionAttributeValues,
201242
}),
202243
...(options?.limit && { Limit: options.limit }),
203-
...(options?.exclusiveStartKey && { ExclusiveStartKey: options.exclusiveStartKey }),
244+
...(options?.exclusiveStartKey && { ExclusiveStartKey: fromCursor(options.exclusiveStartKey) }),
204245
...(options?.indexName && { IndexName: options.indexName }),
205246
...(options?.consistentRead !== undefined && { ConsistentRead: options.consistentRead }),
206247
...(options?.segment !== undefined && { Segment: options.segment }),
@@ -212,7 +253,7 @@ export async function scanItems(
212253
items: (response.Items ?? []).map((item) => toJsonSafeRecord(item) ?? {}),
213254
count: response.Count || 0,
214255
scannedCount: response.ScannedCount ?? null,
215-
lastEvaluatedKey: toJsonSafeRecord(response.LastEvaluatedKey),
256+
lastEvaluatedKey: toCursor(response.LastEvaluatedKey),
216257
}
217258
}
218259

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { awsCloudwatchGetMetricStatisticsContract } from '@/lib/api/contracts/tools/aws/cloudwatch-get-metric-statistics'
3+
import { getMetricStatisticsTool } from '@/tools/cloudwatch/get_metric_statistics'
4+
import { validateRequiredParametersAfterMerge } from '@/tools/utils'
5+
6+
const PARAMS = {
7+
awsRegion: 'us-east-1',
8+
awsAccessKeyId: 'AKIAEXAMPLE',
9+
awsSecretAccessKey: 'secret',
10+
namespace: 'AWS/EC2',
11+
metricName: 'CPUUtilization',
12+
startTime: 1700000000,
13+
endTime: 1700003600,
14+
period: 300,
15+
}
16+
17+
function parseRequest(params: Record<string, unknown>) {
18+
const body = getMetricStatisticsTool.operation.input(params as never)
19+
return awsCloudwatchGetMetricStatisticsContract.body?.safeParse(body)
20+
}
21+
22+
describe('cloudwatch_get_metric_statistics', () => {
23+
it('accepts percentile statistics without a standard statistic', () => {
24+
const params = { ...PARAMS, extendedStatistics: ['p99', 'p95'] }
25+
expect(() =>
26+
validateRequiredParametersAfterMerge(
27+
'cloudwatch_get_metric_statistics',
28+
getMetricStatisticsTool,
29+
params
30+
)
31+
).not.toThrow()
32+
expect(parseRequest(params)?.success).toBe(true)
33+
})
34+
35+
it('rejects a request with neither standard nor percentile statistics', () => {
36+
const result = parseRequest(PARAMS)
37+
expect(result?.success).toBe(false)
38+
expect(result?.error?.issues.map((issue) => issue.message)).toEqual([
39+
'Provide statistics or extendedStatistics',
40+
])
41+
})
42+
})

‎apps/sim/tools/cloudwatch/get_metric_statistics.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ export const getMetricStatisticsTool: InternalToolConfig<
6666
},
6767
statistics: {
6868
type: 'array',
69-
required: true,
69+
required: false,
7070
visibility: 'user-or-llm',
71-
description: 'Statistics to retrieve (Average, Sum, Minimum, Maximum, SampleCount)',
71+
description:
72+
'Statistics to retrieve (Average, Sum, Minimum, Maximum, SampleCount). Required unless extendedStatistics is set, which takes precedence',
7273
},
7374
dimensions: {
7475
type: 'string',
@@ -102,7 +103,7 @@ export const getMetricStatisticsTool: InternalToolConfig<
102103
startTime: params.startTime,
103104
endTime: params.endTime,
104105
period: params.period,
105-
statistics: params.statistics,
106+
...(params.statistics && params.statistics.length > 0 && { statistics: params.statistics }),
106107
...(params.dimensions && { dimensions: params.dimensions }),
107108
...(params.extendedStatistics &&
108109
params.extendedStatistics.length > 0 && {

‎apps/sim/tools/cloudwatch/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export interface CloudWatchGetMetricStatisticsParams extends CloudWatchConnectio
106106
startTime: number
107107
endTime: number
108108
period: number
109-
statistics: string[]
109+
statistics?: string[]
110110
dimensions?: string
111111
extendedStatistics?: string[]
112112
unit?: string

‎apps/sim/tools/generated/tool-metadata.ts‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)