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(bot): keep Discord thread topic across the 4h idle reset (#19978)

Arvin Xu0189acbe3604
diff --git a/apps/server/src/services/bot/AgentBridgeService.ts b/apps/server/src/services/bot/AgentBridgeService.ts
index 2ecd2c1f0d2..de5961b0886 100644
--- a/apps/server/src/services/bot/AgentBridgeService.ts
+++ b/apps/server/src/services/bot/AgentBridgeService.ts
@@ -699,8 +699,12 @@ export class AgentBridgeService {
await thread.setState({ ...threadState, topicId: undefined });
return this.handleMention(thread, message, opts);
}
+ // A platform thread that is itself a bounded conversation (e.g. a
+ // Discord guild thread) keeps its topic regardless of idle time β€” a
+ // reply hours later is still the same conversation.
+ const expiresWhenIdle = opts.client?.shouldExpireIdleTopic?.(thread.id) ?? true;
const elapsed = Date.now() - new Date(existingTopic.updatedAt).getTime();
- if (elapsed > TOPIC_STALE_THRESHOLD) {
+ if (expiresWhenIdle && elapsed > TOPIC_STALE_THRESHOLD) {
log(
'handleSubscribedMessage: topic=%s is stale (%.1fh since last activity), creating new topic',
topicId,
diff --git a/apps/server/src/services/bot/__tests__/AgentBridgeService.test.ts b/apps/server/src/services/bot/__tests__/AgentBridgeService.test.ts
index 260af2c0e41..f174226cc33 100644
--- a/apps/server/src/services/bot/__tests__/AgentBridgeService.test.ts
+++ b/apps/server/src/services/bot/__tests__/AgentBridgeService.test.ts
@@ -499,6 +499,51 @@ describe('AgentBridgeService', () => {
expect(mockExecAgent.mock.calls[0][0].appContext?.topicId).toBe('topic-1');
});
+ it('resets the cached topicId when the topic has been idle past the threshold', async () => {
+ mockTopicFindById.mockResolvedValue({
+ agentId: 'agent-1',
+ id: 'topic-1',
+ updatedAt: new Date(Date.now() - 5 * 60 * 60 * 1000),
+ });
+ const service = new AgentBridgeService(FAKE_DB, USER_ID);
+ const thread = createThread({ topicId: 'topic-1' });
+
+ await service.handleSubscribedMessage(thread, createMessage(), {
+ agentId: 'agent-1',
+ botContext: { platformThreadId: THREAD_ID } as any,
+ client: createClient(),
+ });
+
+ expect(thread.setState).toHaveBeenCalledWith(expect.objectContaining({ topicId: undefined }));
+ expect(mockExecAgent.mock.calls[0][0].appContext?.topicId).toBeUndefined();
+ });
+
+ it('continues an idle topic when the platform thread never expires (Discord guild thread)', async () => {
+ // A reply in a Discord thread 8h after the last turn used to fork a
+ // new topic with no working directory, losing the whole conversation.
+ mockTopicFindById.mockResolvedValue({
+ agentId: 'agent-1',
+ id: 'topic-1',
+ updatedAt: new Date(Date.now() - 8 * 60 * 60 * 1000),
+ });
+ const service = new AgentBridgeService(FAKE_DB, USER_ID);
+ const thread = createThread({ topicId: 'topic-1' });
+ const client = { ...createClient(), shouldExpireIdleTopic: vi.fn().mockReturnValue(false) };
+
+ await service.handleSubscribedMessage(thread, createMessage(), {
+ agentId: 'agent-1',
+ botContext: { platformThreadId: THREAD_ID } as any,
+ client,
+ });
+
+ expect(client.shouldExpireIdleTopic).toHaveBeenCalledWith(thread.id);
+ expect(thread.setState).not.toHaveBeenCalledWith(
+ expect.objectContaining({ topicId: undefined }),
+ );
+ expect(mockExecAgent).toHaveBeenCalledTimes(1);
+ expect(mockExecAgent.mock.calls[0][0].appContext?.topicId).toBe('topic-1');
+ });
+
it('retries as a fresh mention when queue-mode execAgent reports Topic not found (delete race)', async () => {
// Pre-flight sees the topic, but it vanishes before the topic-start
// reservation β€” execAgent throws a plain "Topic not found" error.
diff --git a/apps/server/src/services/bot/platforms/discord/client.test.ts b/apps/server/src/services/bot/platforms/discord/client.test.ts
index 1e802e4d683..197cd777f4e 100644
--- a/apps/server/src/services/bot/platforms/discord/client.test.ts
+++ b/apps/server/src/services/bot/platforms/discord/client.test.ts
@@ -34,6 +34,20 @@ describe('DiscordGatewayClient', () => {
});
});
+ describe('shouldExpireIdleTopic', () => {
+ it('keeps the topic of a Discord guild thread regardless of idle time', () => {
+ const client = createClient();
+
+ expect(client.shouldExpireIdleTopic?.('discord:guild-1:channel-1:thread-1')).toBe(false);
+ });
+
+ it('expires idle topics in DMs', () => {
+ const client = createClient();
+
+ expect(client.shouldExpireIdleTopic?.('discord:@me:dm-channel-1')).toBe(true);
+ });
+ });
+
describe('isSoloBotConversation', () => {
const installFakeApi = (client: any) => {
const listThreadMembers = vi.fn().mockResolvedValue([]);
diff --git a/apps/server/src/services/bot/platforms/discord/client.ts b/apps/server/src/services/bot/platforms/discord/client.ts
index c4e97e93c39..b748421c505 100644
--- a/apps/server/src/services/bot/platforms/discord/client.ts
+++ b/apps/server/src/services/bot/platforms/discord/client.ts
@@ -468,6 +468,12 @@ class DiscordGatewayClient implements PlatformClient {
return isSubscribableThread(threadId);
}
+ shouldExpireIdleTopic(threadId: string): boolean {
+ // Only DMs are an unbounded stream; a guild thread is one conversation.
+ const [, guildId, , discordThreadId] = threadId.split(':');
+ return guildId === '@me' || !discordThreadId;
+ }
+
/**
* Spawn a Discord thread off the triggering message when the bot wakes
* in a top-level guild channel via a watch-keyword match. The chat-sdk
diff --git a/apps/server/src/services/bot/platforms/types.ts b/apps/server/src/services/bot/platforms/types.ts
index 0cdb144134b..37f1b7cc1e4 100644
--- a/apps/server/src/services/bot/platforms/types.ts
+++ b/apps/server/src/services/bot/platforms/types.ts
@@ -439,6 +439,14 @@ export interface PlatformClient {
*/
sanitizeUserInput?: (text: string, message?: unknown) => string;
+ /**
+ * Whether a subscribed thread's topic expires after the idle threshold
+ * (4h), so the next message starts a fresh topic. Default: true.
+ * Discord: returns false for guild threads β€” a thread is already a bounded
+ * conversation, so a late reply must continue the same topic.
+ */
+ shouldExpireIdleTopic?: (threadId: string) => boolean;
+
/**
* Whether the bot should subscribe to a thread. Default: true.
* Discord: returns false for top-level channels (not threads).