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(memory): cancel a banned user's process-topic run instead of returning before its first step (#22775)

Arvin Xu6701940f7aaf
diff --git a/apps/server/src/router-hono/workflows/memory-user-memory/workflows/__tests__/processTopic.hourlyTask.test.ts b/apps/server/src/router-hono/workflows/memory-user-memory/workflows/__tests__/processTopic.hourlyTask.test.ts
index 51d702f249e..3c0d7cf63c2 100644
--- a/apps/server/src/router-hono/workflows/memory-user-memory/workflows/__tests__/processTopic.hourlyTask.test.ts
+++ b/apps/server/src/router-hono/workflows/memory-user-memory/workflows/__tests__/processTopic.hourlyTask.test.ts
@@ -1,4 +1,5 @@
import { LayersEnum, MemorySourceType } from '@lobechat/types';
+import { WorkflowAbort } from '@upstash/workflow';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createStepRunner } from '@/server/workflows/testing/stepContext';
@@ -114,24 +115,31 @@ describe('processTopicHandler hourly task behavior', () => {
expect(mocks.extractTopic).toHaveBeenCalledTimes(2);
});
- it('does not extract for a banned user, including runs queued before the ban', async () => {
+ it('cancels a banned user untracked run before any step instead of returning', async () => {
mocks.isBanned.mockResolvedValue(true);
- const context = createContext({
- baseUrl: 'https://app.example.com',
- sources: [MemorySourceType.ChatTopic],
- topicIds: ['t1'],
- userIds: ['u1'],
- });
+ // Upstash's request authentication and its real context both surface cancel() as a
+ // WorkflowAbort; a handler that returns instead fails authentication with a 400.
+ const cancel = vi.fn().mockRejectedValue(new WorkflowAbort('cancel', undefined, true));
+ const context = {
+ ...createContext({
+ baseUrl: 'https://app.example.com',
+ sources: [MemorySourceType.ChatTopic],
+ topicIds: ['t1'],
+ userIds: ['u1'],
+ }),
+ cancel,
+ };
- await expect(processTopicHandler(context as never)).resolves.toMatchObject({ skipped: true });
+ await expect(processTopicHandler(context as never)).rejects.toBeInstanceOf(WorkflowAbort);
+ expect(cancel).toHaveBeenCalledTimes(1);
// Ends before any persisted step, so nothing is queued on the shared flow-control key. The
// startup helpers are mocked, so assert they were never reached rather than `context.run`.
expect(ensureWorkflowStarted).not.toHaveBeenCalled();
expect(checkGuard).not.toHaveBeenCalled();
expect(context.run).not.toHaveBeenCalled();
- expect(mocks.spanEnd).toHaveBeenCalledTimes(1);
expect(mocks.extractTopic).not.toHaveBeenCalled();
+ expect(mocks.spanEnd).toHaveBeenCalledTimes(1);
});
it('still reports progress for a banned user tracked task, without extracting', async () => {
diff --git a/apps/server/src/router-hono/workflows/memory-user-memory/workflows/processTopic.ts b/apps/server/src/router-hono/workflows/memory-user-memory/workflows/processTopic.ts
index 6a5566dfb36..0244d4cf78d 100644
--- a/apps/server/src/router-hono/workflows/memory-user-memory/workflows/processTopic.ts
+++ b/apps/server/src/router-hono/workflows/memory-user-memory/workflows/processTopic.ts
@@ -57,7 +57,11 @@ export const processTopicHandler = async (context: WorkflowContext<MemoryExtract
span.setStatus({ code: SpanStatusCode.OK });
// Outside the try/finally below, so this path ends its own span.
span.end();
- return { message: 'User is banned, skipping memory extraction.', skipped: true };
+ // NOTICE: Cancel, never return. Upstash authenticates a delivery by running the handler
+ // until it reaches a step or `cancel()`; a plain return before any step fails that check
+ // ("Failed to authenticate Workflow request"), answers 400 and the delivery is retried.
+ // `cancel()` passes the check and ends the run on this delivery without queueing a step.
+ await context.cancel();
}
await ensureWorkflowStarted(context, WORKFLOW_PATH);