Skip to content

Commit adb6780

Browse files
t3dotggclaude
andcommitted
fix(server): measure replay payload budget in bytes
SQLite length() on TEXT counts code points, so non-ASCII payloads could pass the 8 MiB budget at up to four times their byte size. Use octet_length() and cover a multibyte row in the stats test. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent d7bb9cb commit adb6780

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,16 +948,22 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
948948
'replay-event-3', 'thread', 'thread-replay', 3, 'thread.activity-appended',
949949
'2026-03-01T00:00:02.000Z', NULL, NULL, NULL, 'provider',
950950
json_object('output', printf('%.*c', 3000, 'x')), '{}'
951+
),
952+
(
953+
'replay-event-4', 'thread', 'thread-replay', 4, 'thread.activity-appended',
954+
'2026-03-01T00:00:03.000Z', NULL, NULL, NULL, 'provider',
955+
json_object('output', '😀'), '{}'
951956
)
952957
`;
953958

959+
// Bytes, not code points: the 4-byte emoji row is {"output":"😀"}, 17 bytes.
954960
const stats = yield* snapshotQuery.getEventReplayStats({
955961
fromSequenceExclusive: 1,
956-
toSequenceInclusive: 3,
962+
toSequenceInclusive: 4,
957963
});
958964
assert.deepStrictEqual(stats, {
959-
eventCount: 2,
960-
payloadBytes: 5026,
965+
eventCount: 3,
966+
payloadBytes: 5043,
961967
});
962968
}),
963969
);

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
828828
sql`
829829
SELECT
830830
COUNT(*) AS "eventCount",
831-
COALESCE(SUM(length(payload_json)), 0) AS "payloadBytes"
831+
COALESCE(SUM(octet_length(payload_json)), 0) AS "payloadBytes"
832832
FROM orchestration_events
833833
WHERE sequence > ${fromSequenceExclusive}
834834
AND sequence <= ${toSequenceInclusive}

‎apps/server/src/ws.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ const SHELL_RESUME_MAX_GAP = 1_000;
335335
// hundreds of thousands of events behind have OOM-killed servers on large
336336
// databases. Past this gap the client is reset with a fresh thread snapshot.
337337
const THREAD_RESUME_MAX_GAP = 1_000;
338+
// Row count alone does not bound replay memory: a few events with large tool
339+
// payloads can decode to gigabytes. Before replaying, sum the serialized
340+
// payload bytes of the range in SQL and reset with a snapshot past this budget.
338341
const ORCHESTRATION_REPLAY_PAYLOAD_BUDGET_BYTES = 8 * 1024 * 1024;
339342

340343
function toAuthAccessStreamEvent(

0 commit comments

Comments
 (0)