Skip to content

Commit d00c29e

Browse files
committed
Merge remote-tracking branch 'origin/staging' into fix/workflow-code-staging-qa
2 parents d2cf60a + b3e928f commit d00c29e

7 files changed

Lines changed: 107 additions & 31 deletions

File tree

‎apps/sim/lib/core/redis/byte-budget.server.test.ts‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,16 @@ describe('getRedisBudgetKeys', () => {
1717

1818
/**
1919
* These keys are shared with counters written before this module existed, so the
20-
* layout is a wire contract: a change here strands every counter in flight.
20+
* execution layout is a wire contract: a change here strands every counter in flight.
2121
*/
22-
it('separates owner kinds without disturbing the execution key layout', () => {
22+
it('gives a stream its own owner and user counters', () => {
2323
expect(
2424
getRedisBudgetKeys({ kind: 'copilot_stream', id: 'stream-1', userId: 'user-1' })
2525
).toEqual([
2626
'execution:redis-budget:copilot_stream:stream-1',
27-
'execution:redis-budget:user:user-1',
27+
'execution:redis-budget:copilot_stream_user:user-1',
2828
])
2929
})
30-
31-
it('shares one user ceiling across owner kinds', () => {
32-
const [, executionUserKey] = getRedisBudgetKeys({
33-
kind: 'execution',
34-
id: 'exec-1',
35-
userId: 'user-1',
36-
})
37-
const [, streamUserKey] = getRedisBudgetKeys({
38-
kind: 'copilot_stream',
39-
id: 'stream-1',
40-
userId: 'user-1',
41-
})
42-
expect(streamUserKey).toBe(executionUserKey)
43-
})
4430
})
4531

4632
describe('getRedisBudgetLimits', () => {

‎apps/sim/lib/core/redis/byte-budget.server.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,27 @@ import type { Logger } from '@sim/logger'
3636
*/
3737
const REDIS_BUDGET_PREFIX = 'execution:redis-budget:'
3838

39-
/** What a budget is charged to. One counter per owner, plus one per user across owners. */
39+
/** What a budget is charged to. One counter per owner, plus one per user per kind. */
4040
export type RedisBudgetOwnerKind = 'execution' | 'copilot_stream'
4141

42+
/**
43+
* The user counter's key segment for each kind.
44+
*
45+
* Each kind has its own user ceiling, so each needs its own counter: a shared one lets
46+
* workflow runs, within their larger ceiling, lock the user out of Chat for the window.
47+
* `user` is the execution counter's original segment, kept so in-flight counters still apply.
48+
*/
49+
const USER_KEY_SEGMENTS: Record<RedisBudgetOwnerKind, string> = {
50+
execution: 'user',
51+
copilot_stream: 'copilot_stream_user',
52+
}
53+
4254
export interface RedisBudgetScope {
4355
kind: RedisBudgetOwnerKind
4456
/** The owner's id — an execution id, a stream id, a table id. */
4557
id: string
4658
/**
47-
* Charges the write to a second, user-wide counter as well. Omitted where the
59+
* Charges the write to the user's counter for this kind as well. Omitted where the
4860
* writer has no user in scope; the owner counter still applies.
4961
*/
5062
userId?: string
@@ -64,12 +76,12 @@ export interface RedisBudgetLimits {
6476
* itself, so sliding its TTL on every write keeps the counter and the bytes it
6577
* represents in step.
6678
*
67-
* A user counter aggregates across every owner that user writes to. Sliding it on
68-
* each write would keep it alive indefinitely for anyone who stays active while the
69-
* data underneath it keeps expiring — so the counter would accrue bytes Redis has
70-
* already dropped and eventually pin the user at their ceiling until they went a full
71-
* window without writing. User counters therefore get a fixed window: set on
72-
* creation, never extended.
79+
* A user counter aggregates across every owner of its kind that user writes to.
80+
* Sliding it on each write would keep it alive indefinitely for anyone who stays
81+
* active while the data underneath it keeps expiring — so the counter would accrue
82+
* bytes Redis has already dropped and eventually pin the user at their ceiling until
83+
* they went a full window without writing. User counters therefore get a fixed
84+
* window: set on creation, never extended.
7385
*
7486
* Because a trim refunds both counters, the user counter bounds bytes HELD across a
7587
* user's owners, not bytes written per hour: a single long copilot stream holds at most
@@ -113,7 +125,7 @@ export function getRedisBudgetLimits(kind: RedisBudgetOwnerKind): RedisBudgetLim
113125
export function getRedisBudgetKeys(scope: RedisBudgetScope): string[] {
114126
const keys = [`${REDIS_BUDGET_PREFIX}${scope.kind}:${scope.id}`]
115127
if (scope.userId) {
116-
keys.push(`${REDIS_BUDGET_PREFIX}user:${scope.userId}`)
128+
keys.push(`${REDIS_BUDGET_PREFIX}${USER_KEY_SEGMENTS[scope.kind]}:${scope.userId}`)
117129
}
118130
return keys
119131
}

‎apps/sim/lib/execution/resource-errors.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { formatLargeValueSize } from '@/lib/execution/payloads/large-value-ref'
2+
13
export const EXECUTION_RESOURCE_LIMIT_CODE = 'execution_resource_limit_exceeded' as const
24

35
export type ExecutionResourceLimitResource =
@@ -14,6 +16,26 @@ export interface ExecutionResourceLimitDetails {
1416
statusCode?: number
1517
}
1618

19+
const GENERIC_LIMIT_MESSAGE = 'Execution memory limit exceeded. Reduce payload size and try again.'
20+
21+
/**
22+
* A payload refusal names the value's size and the limit, so whoever fixes the workflow
23+
* (a person or Chat) can see it is a structural problem rather than retrying it. A Redis
24+
* budget's byte counts describe a write chunk, which means nothing to a user. A size
25+
* that reads the same as the limit (only known to exceed it, or just over) is left unstated.
26+
*/
27+
function getResourceLimitMessage(details: ExecutionResourceLimitDetails): string {
28+
if (details.resource !== 'execution_payload_bytes') return GENERIC_LIMIT_MESSAGE
29+
const limit = formatSize(details.limitBytes)
30+
const attempted = formatSize(details.attemptedBytes)
31+
const size = attempted === limit ? 'is larger than' : `is ${attempted}, more than`
32+
return `Execution memory limit exceeded: the data ${size} the ${limit} limit. Pass a smaller field or file, or have the blocks that produce it output less.`
33+
}
34+
35+
function formatSize(bytes: number): string {
36+
return bytes < 1024 * 1024 ? `${Math.ceil(bytes / 1024)} KB` : formatLargeValueSize(bytes)
37+
}
38+
1739
export class ExecutionResourceLimitError extends Error {
1840
readonly code = EXECUTION_RESOURCE_LIMIT_CODE
1941
readonly statusCode: number
@@ -23,7 +45,7 @@ export class ExecutionResourceLimitError extends Error {
2345
readonly currentBytes?: number
2446

2547
constructor(details: ExecutionResourceLimitDetails) {
26-
super('Execution memory limit exceeded. Reduce payload size and try again.')
48+
super(getResourceLimitMessage(details))
2749
this.name = 'ExecutionResourceLimitError'
2850
this.resource = details.resource
2951
this.attemptedBytes = details.attemptedBytes

‎apps/sim/lib/function-execution/execute-request.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,39 @@ describe('Function execution request', () => {
27752775
expect(data.error).toContain('File secret provenance is unavailable')
27762776
expect(JSON.stringify(data)).not.toContain('private file content')
27772777
})
2778+
2779+
it('tells user code how large a referenced value is when it is too large to load', async () => {
2780+
const ref = {
2781+
__simLargeValueRef: true,
2782+
version: 1,
2783+
id: 'lv_loopresult01',
2784+
kind: 'array',
2785+
size: 28 * 1024 * 1024,
2786+
key: 'execution/workspace-1/workflow-1/execution-1/large-value-lv_loopresult01.json',
2787+
executionId: 'execution-1',
2788+
}
2789+
mockExecuteInIsolatedVM.mockImplementationOnce(async (_input, options) => ({
2790+
result: await options.brokers['sim.values.read']({ ref }).catch(
2791+
(error: Error) => error.message
2792+
),
2793+
stdout: '',
2794+
}))
2795+
2796+
const response = await POST(
2797+
createMockRequest('POST', {
2798+
code: 'return 1',
2799+
language: 'javascript',
2800+
workspaceId: 'workspace-1',
2801+
workflowId: 'workflow-1',
2802+
executionId: 'execution-1',
2803+
})
2804+
)
2805+
2806+
const data = await response.json()
2807+
expect(data.output.result).toBe(
2808+
'Execution memory limit exceeded: the data is 28.0 MB, more than the 16.0 MB limit. Pass a smaller field or file, or have the blocks that produce it output less.'
2809+
)
2810+
})
27782811
})
27792812

27802813
describe('Template Variable Resolution', () => {

‎apps/sim/lib/mothership/request/session/buffer.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ describe('mothership-stream-outbox', () => {
310310
mockRedis.eval.mockClear()
311311
await appendEvents([envelope], { streamId: 'stream-1', userId: 'user-1' })
312312
expect(mockRedis.eval.mock.calls[0][1]).toBe(4)
313-
expect(mockRedis.eval.mock.calls[0][5]).toBe('execution:redis-budget:user:user-1')
313+
expect(mockRedis.eval.mock.calls[0][5]).toBe(
314+
'execution:redis-budget:copilot_stream_user:user-1'
315+
)
314316
})
315317

316318
it('clears persisted stream state during teardown cleanup', async () => {
@@ -443,13 +445,15 @@ describe('mothership-stream-outbox', () => {
443445
expect(mockRedis.del.mock.calls[0]).toContain('execution:redis-budget:copilot_stream:stream-1')
444446
})
445447

446-
it('never touches the shared user counter when clearing a buffer', async () => {
448+
it('never touches the user counter when clearing a buffer', async () => {
447449
// An owner id is not proof of who wrote the bytes, so crediting the user counter here would
448450
// let anyone who can name a stream decrement a ceiling they never charged.
449451
await clearBuffer('stream-1')
450452

451453
const keys = mockRedis.del.mock.calls[0] as string[]
452-
expect(keys.some((key) => key.includes('redis-budget:user:'))).toBe(false)
454+
expect(keys.filter((key) => key.startsWith('execution:redis-budget:'))).toEqual([
455+
'execution:redis-budget:copilot_stream:stream-1',
456+
])
453457
})
454458
})
455459

‎apps/sim/lib/mothership/request/session/buffer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function clearBuffer(streamId: string, operation = 'clear_outbox'):
142142
its reservation already erased. One variadic DEL is a single atomic command, so no script
143143
is needed to get that.
144144
145-
The shared user counter is deliberately untouched: an owner id is not proof of who wrote
145+
The user's stream counter is deliberately untouched: an owner id is not proof of who wrote
146146
the bytes, so crediting it here would let anyone able to name a stream decrement a ceiling
147147
they never charged — and a counter driven down grants writes rather than denying them. Its
148148
fixed window settles it instead, over-counting in the safe direction meanwhile.

‎apps/sim/lib/mothership/request/session/replay-budget.integration.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,25 @@ describe.runIf(Boolean(redisUrl))('a turn whose stream exhausts its replay budge
867867
])
868868
}, 180_000)
869869

870+
it("is not refused for bytes the user's workflow runs hold", async () => {
871+
const [, executionUserKey] = getRedisBudgetKeys({
872+
kind: 'execution',
873+
id: generateId(),
874+
userId,
875+
})
876+
await redis().set(executionUserKey, getRedisBudgetLimits('execution').maxUserBytes, 'EX', 300)
877+
try {
878+
const { runId, frames } = await runTurn([text('The digest is ready.')])
879+
880+
expect(frames.map((frame) => frame.type)).not.toContain('error')
881+
expect(frames.at(-1)).toMatchObject({ type: 'complete', payload: { status: 'complete' } })
882+
const [stored] = await db.select().from(copilotRuns).where(eq(copilotRuns.id, runId))
883+
expect(stored.status).toBe('complete')
884+
} finally {
885+
await redis().del(executionUserKey)
886+
}
887+
})
888+
870889
/**
871890
* An unfinished run with no live controller whose ring a byte trim has advanced past
872891
* its head: seqs 1–2 are gone and 3–4 remain.

0 commit comments

Comments
 (0)