Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(agent-runtime): keep step_start uiMessages snapshots out of the Redis event stream (#22528)

Mocha513ea8ec2162
diff --git a/apps/server/src/modules/AgentRuntime/StreamEventManager.ts b/apps/server/src/modules/AgentRuntime/StreamEventManager.ts
index ee4afe9d549..2792f88062b 100644
--- a/apps/server/src/modules/AgentRuntime/StreamEventManager.ts
+++ b/apps/server/src/modules/AgentRuntime/StreamEventManager.ts
@@ -87,8 +87,8 @@ const stripStateForStream = <T extends Record<string, any>>(
/**
* Chokepoint helper applied inside every stream-event publish site.
- * Step completion events omit finalState unless the persisted run host opts in.
- * For other events carrying a `finalState`, strip `expertise`, `messages`,
+ * Step completion events omit finalState unless the persisted run host opts in,
+ * and step start events omit their `uiMessages` snapshot. For other events carrying a `finalState`, strip `expertise`, `messages`,
* and the tool-set group off it (see `stripStateForStream` for the rationale).
*
* Centralizing the strip here means new callers — including direct
@@ -109,6 +109,15 @@ export const stripFinalStateInEventData = (data: unknown, eventType?: unknown):
const { finalState: _finalState, ...rest } = record;
return rest;
}
+ // A step_start snapshot is the whole message list (megabytes on a long topic),
+ // one per step. Clients take it from the gateway push, which publishes the
+ // event before this strip; nothing reads it back from the stream (the SSE
+ // route serves the CLI, which ignores it), so a stored copy per step only
+ // grew the stream and every replay of it.
+ if (eventType === 'step_start' && 'uiMessages' in record) {
+ const { uiMessages: _uiMessages, ...rest } = record;
+ return rest;
+ }
const finalState = record.finalState;
if (!finalState || typeof finalState !== 'object') return data;
return { ...record, finalState: stripStateForStream(finalState as Record<string, any>) };
diff --git a/apps/server/src/modules/AgentRuntime/__tests__/StreamEventManager.test.ts b/apps/server/src/modules/AgentRuntime/__tests__/StreamEventManager.test.ts
index 1af6903bd6b..6e26240b9c6 100644
--- a/apps/server/src/modules/AgentRuntime/__tests__/StreamEventManager.test.ts
+++ b/apps/server/src/modules/AgentRuntime/__tests__/StreamEventManager.test.ts
@@ -582,6 +582,42 @@ describe('StreamEventManager', () => {
});
});
+ describe('publishStreamEvent step_start', () => {
+ const storedData = () => {
+ const dataArg = mockRedis.xadd.mock.calls[0]?.find(
+ (a: any) => typeof a === 'string' && a.startsWith('{'),
+ );
+ return JSON.parse(dataArg);
+ };
+
+ // The snapshot is a whole message list per step (megabytes on a long topic).
+ // Its consumers get it from the gateway push; nothing reads it back from the
+ // stream, so keeping a copy per step only grew the stream and every replay.
+ it('keeps the uiMessages snapshot out of the stored stream entry', async () => {
+ mockRedis.xadd.mockResolvedValue('event-id-step');
+
+ await streamManager.publishStreamEvent('op-step', {
+ data: { llmCalls: 3, uiMessages: [{ content: 'x'.repeat(64), id: 'msg_1' }] },
+ stepIndex: 4,
+ type: 'step_start',
+ });
+
+ expect(storedData()).toEqual({ llmCalls: 3 });
+ });
+
+ it('stores a protocol-2 step_start unchanged', async () => {
+ mockRedis.xadd.mockResolvedValue('event-id-step');
+
+ await streamManager.publishStreamEvent('op-step', {
+ data: { llmCalls: 1, messageRevision: 4 },
+ stepIndex: 4,
+ type: 'step_start',
+ });
+
+ expect(storedData()).toEqual({ llmCalls: 1, messageRevision: 4 });
+ });
+ });
+
describe('stripFinalStateInEventData', () => {
it('returns data unchanged when finalState is absent', () => {
const data = { phase: 'execution_complete', reason: 'done' };