Skip to content

Commit 4b78690

Browse files
authored
fix(mothership): cut oversized run errors after projection and collapse long block runs (#10150)
1 parent f51dad7 commit 4b78690

4 files changed

Lines changed: 265 additions & 63 deletions

File tree

‎apps/sim/lib/mothership/request/tools/client.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ export async function waitForWorkflowToolCompletion({
485485
const executionId = trustedExecution.executionId
486486
const status = getTerminalConfirmationStatus(trustedExecution.status)
487487
const genericMessage = getWorkflowToolCompletionMessage(status)
488-
const error =
488+
const runError =
489489
status !== MothershipStreamV1ToolOutcome.success
490490
? (trustedExecution.error ?? genericMessage)
491491
: undefined
492-
const rawData = boundRunResultForModel(
492+
const { data: rawData, error } = boundRunResultForModel(
493493
{
494494
success: status === MothershipStreamV1ToolOutcome.success,
495495
workflowId,
@@ -504,7 +504,7 @@ export async function waitForWorkflowToolCompletion({
504504
? { reason: 'user_cancelled', cancelledByUser: true }
505505
: {}),
506506
},
507-
error,
507+
runError,
508508
executionId,
509509
toolRegistry
510510
)

‎apps/sim/lib/mothership/tools/handlers/workflow/mutations.ts‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,25 @@ function buildExecutionOutput(
144144
: result.error || failedBlockError(logs) || 'Workflow execution failed'
145145
// A caller that names the outputs it wants gets those and nothing else: a seven-block
146146
// run otherwise costs ~14K chars of logs to learn one headline.
147-
return {
148-
success: result.success,
149-
output: boundRunResultForModel(
150-
{
151-
executionId,
152-
success: result.success,
153-
...extra,
154-
output: lifted ? lifted.output : output,
155-
...(lifted ? { outputFrom: lifted.outputFrom } : {}),
156-
...presentWorkflowLogsForModel(logs, executionId, registry, select, {
157-
previewLongInputs: true,
158-
}),
159-
},
160-
error,
147+
const bounded = boundRunResultForModel(
148+
{
161149
executionId,
162-
registry
163-
),
150+
success: result.success,
151+
...extra,
152+
output: lifted ? lifted.output : output,
153+
...(lifted ? { outputFrom: lifted.outputFrom } : {}),
154+
...presentWorkflowLogsForModel(logs, executionId, registry, select, {
155+
previewLongInputs: true,
156+
}),
157+
},
164158
error,
159+
executionId,
160+
registry
161+
)
162+
return {
163+
success: result.success,
164+
output: bounded.data,
165+
error: bounded.error,
165166
effect: executionEffect(phase, executionId),
166167
}
167168
}

‎apps/sim/lib/mothership/tools/handlers/workflow/run-workflow-result-budget.test.ts‎

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,71 @@ describe('run_workflow model-facing result budget', () => {
176176
expect(output.output).toEqual(finalOutput)
177177
const presented = output.logs as Array<Record<string, unknown>>
178178
expect(presented.map((log) => log.blockName)).toEqual(logs.map((log) => log.blockName))
179-
expect(presented[3]).toMatchObject({ success: false, error: 'SMTP refused' })
179+
// The failing block keeps its name and status; its error is the run error, said once.
180+
expect(presented[3]).toMatchObject({ success: false, error: '…[same as the run error]' })
181+
})
182+
183+
/**
184+
* An HTTP block that fails with a whole HTML page as its error carried it twice, in the run error
185+
* and the block entry. The entry now defers to the run error, which keeps its head, cut after
186+
* projection so a secret straddling the cut leaves no fragment.
187+
*/
188+
it('cuts a page-sized block error once, after secret projection', async () => {
189+
const page = `<html>${'a'.repeat(1_480)}${SECRET}${'b'.repeat(30_000)}</html>`
190+
mocks.executeWorkflowUseCase.mockResolvedValue({
191+
success: false,
192+
output: {},
193+
logs: [
194+
{ blockId: 'start', blockName: 'Start', success: true, output: { ok: true } },
195+
{ blockId: 'http', blockName: 'HTTP', success: false, error: page, output: {} },
196+
],
197+
metadata: { executionId: EXECUTION_ID },
198+
})
199+
200+
const settled = await executeRunWorkflow({ workflowId: 'wf-1' }, context)
201+
const projection = inspectToolResultForCopilot(settled, registry, 'run_workflow')
202+
203+
expect(projection.safe).toBe(true)
204+
const serialized = JSON.stringify(projection.result)
205+
expect(serialized.length).toBeLessThanOrEqual(24_000)
206+
for (let length = 4; length <= SECRET.length; length += 1) {
207+
expect(serialized).not.toContain(SECRET.slice(0, length))
208+
}
209+
expect(projection.result.error).toMatch(/^HTTP: <html>a+/)
210+
expect(projection.result.error).toContain(`logs get ${EXECUTION_ID} --trace`)
211+
const logs = (projection.result.output as { logs: Array<Record<string, unknown>> }).logs
212+
expect(logs[1]).toMatchObject({ blockName: 'HTTP', success: false })
213+
expect(String(logs[1]?.error).length).toBeLessThan(100)
214+
})
215+
216+
/** A loop's iterations each log a block entry; past the window the middle ones give way. */
217+
it('keeps the failed iteration and the ends of a long loop run', async () => {
218+
const logs = Array.from({ length: 400 }, (_, index) => ({
219+
blockId: `iteration-${index}`,
220+
blockName: `Fetch page (iteration ${index})`,
221+
success: index !== 250,
222+
...(index === 250 ? { error: 'Rate limited' } : {}),
223+
output: { ok: index !== 250 },
224+
}))
225+
mocks.executeWorkflowUseCase.mockResolvedValue({
226+
success: false,
227+
output: {},
228+
logs,
229+
metadata: { executionId: EXECUTION_ID },
230+
})
231+
const inactive = secretRegistry({ active: false })
232+
233+
const settled = await executeRunWorkflow({ workflowId: 'wf-1' }, callContext(inactive))
234+
const output = settled.output as { logs: unknown[] }
235+
236+
expect(JSON.stringify({ output, error: settled.error }).length).toBeLessThanOrEqual(16_000)
237+
expect(settled.error).toBe('Fetch page (iteration 250): Rate limited')
238+
expect(output.logs).toContainEqual(expect.objectContaining({ blockId: 'iteration-250' }))
239+
expect(output.logs[0]).toEqual(logs[0])
240+
expect(output.logs.at(-1)).toEqual(logs[399])
241+
expect(output.logs).toContainEqual(
242+
expect.stringMatching(/^…\[\d+ block entries omitted; inspect with logs get /)
243+
)
180244
})
181245

182246
/** A short secret under a long name grows when projected, so the window is measured after it. */
@@ -211,13 +275,17 @@ describe('run_workflow model-facing result budget', () => {
211275
expect(output.logs[0]?.output).toEqual(expect.stringContaining(`logs get ${EXECUTION_ID}`))
212276
})
213277

214-
/** Many small block entries leave less room than a full preview, so the preview shrinks to fit. */
278+
/**
279+
* Failed block entries are never dropped, so many of them (blocks that routed into error paths)
280+
* leave less room than a full preview, and the preview shrinks to fit.
281+
*/
215282
it('shrinks the final output preview to the room the block entries leave', async () => {
216-
const logs = Array.from({ length: 150 }, (_, index) => ({
283+
const logs = Array.from({ length: 120 }, (_, index) => ({
217284
blockId: `step-${index}`,
218285
blockName: `Step ${index} of the nightly import`,
219-
success: true,
220-
output: { ok: true },
286+
success: false,
287+
error: `retry ${index}`,
288+
output: { ok: false },
221289
}))
222290
mocks.executeWorkflowUseCase.mockResolvedValue({
223291
success: true,
@@ -602,7 +670,7 @@ describe('run_workflow model-facing result budget', () => {
602670
const projection = inspectToolResultForCopilot(settled, registry, 'run_workflow')
603671

604672
expect(projection.safe).toBe(true)
605-
expect(projection.result.error).toBe(error)
673+
expect(projection.result.error).toMatch(/^Report failed: e+ …\[error cut at 1500 of /)
606674
expect(
607675
(projection.result.output as { logs: Array<{ output: unknown }> }).logs[0]?.output
608676
).toEqual(expect.stringContaining(`logs get ${EXECUTION_ID} --trace`))

0 commit comments

Comments
 (0)