Skip to content

Commit 82cfe5d

Browse files
committed
fix(slack): serialize appends to a message shared by several invocations
1 parent f1596f6 commit 82cfe5d

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎apps/sim/lib/webhooks/slack-execution-stream.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,40 @@ describe('SlackExecutionStreamController', () => {
827827
)
828828
})
829829

830+
it('never overlaps appends from the timeline and the answer on the shared message', async () => {
831+
let inFlight = 0
832+
let maxInFlight = 0
833+
mockAppendSlackAgentStream.mockImplementation(async () => {
834+
inFlight++
835+
maxInFlight = Math.max(maxInFlight, inFlight)
836+
await new Promise((resolve) => setTimeout(resolve, 5))
837+
inFlight--
838+
})
839+
const { controller } = await createController({
840+
...TIMELINE_CONFIG,
841+
outputConfigs: [{ blockId: 'reply', path: 'content' }],
842+
})
843+
await Promise.all([
844+
streamTimeline(controller),
845+
controller.callbacks.onStream?.({
846+
blockId: 'reply',
847+
executionOrder: 3,
848+
stream: createByteStream(),
849+
execution: { success: true, output: {} },
850+
clientStreamTransformed: false,
851+
subscribe: ({ onEvent }) => {
852+
void onEvent({ type: 'text_delta', text: 'Reviewed answer. ', turn: 'pending' })
853+
void onEvent({ type: 'turn_end', turn: 'final' })
854+
return vi.fn()
855+
},
856+
}),
857+
])
858+
859+
expect(mockStartSlackAgentStream).toHaveBeenCalledTimes(1)
860+
expect(mockAppendSlackAgentStream.mock.calls.length).toBeGreaterThan(1)
861+
expect(maxInFlight).toBe(1)
862+
})
863+
830864
it('still rejects a stream from a block in neither list', async () => {
831865
const { controller } = await createController(TIMELINE_CONFIG)
832866
await expect(

‎apps/sim/lib/webhooks/slack-execution-stream.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ interface SlackMessageStream {
4343
textLength: number
4444
textComplete: boolean
4545
stopped: boolean
46+
/** Settles after this message's latest append, so writers sharing it append in order. */
47+
writes: Promise<void>
4648
}
4749

4850
/** Prefer a nearby paragraph/sentence, then a whole word; only oversized words need hard cuts. */
@@ -173,6 +175,7 @@ async function openSlackMessage(
173175
textLength: markdownLength(chunks),
174176
textComplete: false,
175177
stopped: false,
178+
writes: Promise.resolve(),
176179
}
177180
}
178181

@@ -311,7 +314,11 @@ class SlackInvocationStream {
311314
const target = message ?? (await this.ensureStarted())
312315
const textLength = markdownLength(chunks)
313316
try {
314-
await appendSlackAgentStream(this.token, target.channel, target.ts, chunks, this.signal)
317+
const write = target.writes.then(() =>
318+
appendSlackAgentStream(this.token, target.channel, target.ts, chunks, this.signal)
319+
)
320+
target.writes = write.catch(() => {})
321+
await write
315322
target.textLength += textLength
316323
} catch (error) {
317324
logger.warn('Slack stream append failed', {

0 commit comments

Comments
 (0)