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(topic): clear a stale unread dot when opening a project topic (#21645)

LiJiand5ec01ac3438
diff --git a/src/store/chat/slices/operation/__tests__/markTopicRead.test.ts b/src/store/chat/slices/operation/__tests__/markTopicRead.test.ts
index 268fd0b1e91..c98f5ea8f3c 100644
--- a/src/store/chat/slices/operation/__tests__/markTopicRead.test.ts
+++ b/src/store/chat/slices/operation/__tests__/markTopicRead.test.ts
@@ -70,16 +70,78 @@ describe('markTopicRead', () => {
* @example A persisted project row still says unread, but a run started since
* on another device: the server keeps `running`, so nothing writes `active`.
*/
- it('never writes active itself and mirrors nothing when the server says the topic moved on', async () => {
+ it('never writes active itself and mirrors the server status when the topic moved on', async () => {
vi.mocked(topicService.markTopicRead).mockResolvedValue(null);
+ vi.spyOn(topicService, 'getTopicDetail').mockResolvedValue({
+ id: TOPIC_ID,
+ status: 'running',
+ } as any);
seedBucket('unread');
await useChatStore.getState().markTopicRead({ agentId: AGENT_ID, topicId: TOPIC_ID });
expect(topicService.updateTopic).not.toHaveBeenCalled();
+ expect(bucketStatus()).toBe('running');
+ });
+
+ /**
+ * @example The run's terminal `active` write landed after its `unread` one,
+ * so the server row is already read while this client still holds `unread`.
+ * A project page never refetches the agent's topic list, so without this the
+ * dot stayed on the conversation the user was reading.
+ */
+ it('drops a stale local unread when the server already has the topic read', async () => {
+ vi.mocked(topicService.markTopicRead).mockResolvedValue(null);
+ vi.spyOn(topicService, 'getTopicDetail').mockResolvedValue({
+ id: TOPIC_ID,
+ status: 'active',
+ } as any);
+ seedBucket('unread');
+
+ await useChatStore.getState().markTopicRead({ agentId: AGENT_ID, topicId: TOPIC_ID });
+
+ expect(topicService.getTopicDetail).toHaveBeenCalledWith(TOPIC_ID);
+ expect(bucketStatus()).toBe('active');
+ });
+
+ it('keeps the local unread when the server still reports it unread', async () => {
+ vi.mocked(topicService.markTopicRead).mockResolvedValue(null);
+ vi.spyOn(topicService, 'getTopicDetail').mockResolvedValue({
+ id: TOPIC_ID,
+ status: 'unread',
+ } as any);
+ seedBucket('unread');
+
+ await useChatStore.getState().markTopicRead({ agentId: AGENT_ID, topicId: TOPIC_ID });
+
expect(bucketStatus()).toBe('unread');
});
+ /**
+ * @example The sidebar dot reads every loaded bucket, but the clear used to
+ * mirror only into the active agent/group bucket.
+ */
+ it('clears the topic in every loaded bucket, not only the active one', async () => {
+ const otherBucket = topicMapKey({ agentId: 'agent-2' });
+ useChatStore.setState({
+ topicDataMap: {
+ [otherBucket]: {
+ hasMore: false,
+ items: [{ id: TOPIC_ID, status: 'unread', title: 't' } as any],
+ } as any,
+ },
+ });
+
+ await useChatStore
+ .getState()
+ .markTopicRead({ agentId: AGENT_ID, status: 'unread', topicId: TOPIC_ID });
+
+ expect(
+ useChatStore.getState().topicDataMap[otherBucket]?.items?.find((t) => t.id === TOPIC_ID)
+ ?.status,
+ ).toBe('active');
+ });
+
/**
* @example The user sends a message while the clear is in flight: the clear's
* `active` answer is already stale and must not replace the run's status.
diff --git a/src/store/chat/slices/operation/actions.ts b/src/store/chat/slices/operation/actions.ts
index ad7bac1cd41..8827a42692c 100644
--- a/src/store/chat/slices/operation/actions.ts
+++ b/src/store/chat/slices/operation/actions.ts
@@ -837,6 +837,11 @@ export class OperationActionsImpl {
* still being unread, and the local mirror never outranks a newer transition β€”
* a run started since keeps its `running` status. A transient failure is
* retried while the topic stays open, since the callers fire once per open.
+ *
+ * When the server reports the row is no longer unread, the local copy is the
+ * stale one (e.g. a run's terminal `active` write landed after its `unread`
+ * one). The row's actual status is read back and mirrored, because surfaces
+ * like the project page never refetch the agent's topic list to correct it.
*/
markTopicRead = async ({
agentId,
@@ -862,15 +867,24 @@ export class OperationActionsImpl {
if (knownStatus !== 'unread') return;
const cleared = await this.#clearUnread(topicId);
- if (!cleared) return;
+ if (cleared === undefined) return;
+
+ if (cleared === null) {
+ const current = await topicService.getTopicDetail(topicId).catch(() => null);
+ if (current?.status && current.status !== 'unread')
+ this.#get().internal_settleTopicRead?.({ status: current.status, topicId });
+ return;
+ }
- this.#get().internal_settleTopicRead?.({ agentId, groupId: groupId ?? undefined, topicId });
+ this.#get().internal_settleTopicRead?.({ topicId });
void getHomeStoreState().refreshAgentList?.();
};
/**
* The conditional server clear, retried with backoff while the topic stays
- * open. A 4xx will not change on retry, so it ends the attempt.
+ * open. A 4xx will not change on retry, so it ends the attempt. Resolves to
+ * the cleared row, `null` when the server row was no longer unread, or
+ * `undefined` when the write failed.
*/
#clearUnread = async (topicId: string) => {
for (let attempt = 0; ; attempt += 1) {
@@ -879,12 +893,12 @@ export class OperationActionsImpl {
} catch (error) {
if (!isRetryableWriteError(error)) {
console.error('[markTopicRead] persist failed:', error);
- return null;
+ return undefined;
}
await new Promise((resolve) =>
setTimeout(resolve, Math.min(MARK_READ_RETRY_BASE_MS * 2 ** attempt, 30_000)),
);
- if (this.#get().activeTopicId !== topicId) return null;
+ if (this.#get().activeTopicId !== topicId) return undefined;
}
}
};
diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.ts
index 484c723bac8..7fdbeb823da 100644
--- a/src/store/chat/slices/topic/action.ts
+++ b/src/store/chat/slices/topic/action.ts
@@ -1071,27 +1071,19 @@ export class ChatTopicActionImpl {
* Unlike {@link internal_pinTopicStatus} it never pins: the clear's response
* may already be stale when it lands (a run started meanwhile, here or on
* another device), so a newer pending write wins and the next fetch stays
- * authoritative. Only a loaded row that is still unread follows.
+ * authoritative. Only loaded copies that are still unread follow β€” every one
+ * of them, since the sidebar's unread dot reads all loaded buckets.
+ *
+ * `status` is the server's current status when the clear found the row
+ * already moved on; it defaults to the `active` the clear itself wrote.
*/
- internal_settleTopicRead = (params: { agentId?: string; groupId?: string; topicId: string }) => {
- const { agentId, groupId, topicId } = params;
+ internal_settleTopicRead = (params: { status?: ChatTopicStatus; topicId: string }) => {
+ const { status = 'active', topicId } = params;
if (!this.#pendingTopicStatusWrites.settle(topicId, 'unread')) return;
- const state = this.#get();
- const key = topicMapKey({
- agentId: agentId ?? state.activeAgentId,
- groupId: groupId ?? state.activeGroupId,
- });
- const topic = state.topicDataMap[key]?.items?.find((t) => t.id === topicId);
- if (topic?.status !== 'unread') return;
-
- state.internal_dispatchTopic({
- type: 'updateTopic',
- id: topicId,
- value: { status: 'active' },
- agentId,
- groupId,
- });
+ this.#topicEntity.update(topicId, (topic) =>
+ topic.status === 'unread' ? { ...topic, status } : topic,
+ );
};
#getTopicUpdatedAt = (topic: RunningTopicForWatchdog): number | undefined => {