fix(recents): keep an opened task until its read settles

Replace the in-tab restore with a stored awaitingRead flag. A fresh
visit is kept, but not shown, until its read request settles, so a
summary from any tab cannot prune it first. Settling clears the flag
before the summary refresh, which then shows or prunes the entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
devinfoleyandClaude Opus 5.5 committed 2026-12-04 02:47:44 +00:00
1 parent d383e8c940
commit 5758701a8d
3 files changed
+60 -35

No files matched your search

+27 -9
View File
@@ -12,7 +12,7 @@ import {
readRecentTasks,
recordRecentTask,
recordStartedRoutineRunTask,
restoreRecentTaskVisit,
settleRecentTaskVisit,
RECENT_TASKS_UPDATED_EVENT,
} from "@/lib/recent-tasks";
import { RECENT_TASK_SUMMARY_CONCURRENCY, useRecentTasks } from "./useRecentTasks";
@@ -386,22 +386,40 @@ describe("recent task synchronization", () => {
.toEqual([["older", "Older", 200], ["existing", "Existing", 60]]);
});
it("restores an opened automatic task when its read is saved after a summary pruned it", async () => {
it("keeps an opened automatic task until its read is saved, then shows it", async () => {
const opened = { ...task(10, "Routine result"), id: "opened", isParticipantForMe: false };
recordRecentTask(opened, "user-1", 100);
recordRecentTask(opened, "user-1", 100, { awaitingRead: true });
// The summary read races ahead of the read state that makes it eligible.
summaries.mockResolvedValue([{ ...opened, isRecentEligibleForMe: false }]);
const cache = client();
await act(async () => root.render(<QueryClientProvider client={cache}><RecentTasks name="race" /></QueryClientProvider>));
await flush();
expect(readRecentTasks(storageKey, "company-1")).toEqual([]);
expect(container.textContent).toBe("");
expect(readRecentTasks(storageKey, "company-1").map(entry => entry.id)).toEqual(["opened"]);
summaries.mockResolvedValue([{ ...opened, isRecentEligibleForMe: true }]);
await act(async () => { restoreRecentTaskVisit("company-1", "opened", "user-1"); });
await act(async () => {
settleRecentTaskVisit("company-1", "opened", "user-1");
await cache.invalidateQueries({ queryKey: queryKeys.issues.summaries("company-1") });
});
await flush();
expect(container.textContent).toBe("Routine result:todo");
expect(readRecentTasks(storageKey, "company-1")[0]?.recordedAt).toBe(100);
// A restore applies once; an unrelated later read does not resurrect it.
await act(async () => { restoreRecentTaskVisit("company-1", "opened", "user-1"); });
expect(readRecentTasks(storageKey, "company-1")).toHaveLength(1);
expect(readRecentTasks(storageKey, "company-1")[0]).toMatchObject({ recordedAt: 100 });
expect(readRecentTasks(storageKey, "company-1")[0]).not.toHaveProperty("awaitingRead");
});
it("prunes an opened task after its read settles without making it eligible", async () => {
const opened = { ...task(10, "Failed read"), id: "opened", isParticipantForMe: false };
recordRecentTask(opened, "user-1", 100, { awaitingRead: true });
summaries.mockResolvedValue([{ ...opened, isRecentEligibleForMe: false }]);
const cache = client();
await act(async () => root.render(<QueryClientProvider client={cache}><RecentTasks name="failed" /></QueryClientProvider>));
await flush();
await act(async () => {
settleRecentTaskVisit("company-1", "opened", "user-1");
await cache.invalidateQueries({ queryKey: queryKeys.issues.summaries("company-1") });
});
await flush();
expect(readRecentTasks(storageKey, "company-1")).toEqual([]);
});
it("fetches a 25-task history in one request", async () => {
+21 -21
View File
@@ -19,6 +19,12 @@ export interface RecentTaskEntry {
// Server version of the title/status snapshot, independent of comment activity.
// Legacy entries have no version until a detail query refreshes them.
snapshotUpdatedAt?: number;
/**
* A fresh visit whose read is not saved yet. The read state is what makes an
* opened automatic task eligible, so the entry is kept (but not shown) until
* the read finishes. Stored so every tab honors it.
*/
awaitingRead?: boolean;
}
interface RecentTasksUpdatedDetail {
@@ -142,6 +148,7 @@ export function recordRecentTask(
issue: Pick<Issue, "id" | "companyId" | "title" | "identifier" | "status" | "displayStatus" | "externalConversationState" | "blockerAttention" | "updatedAt" | "conversationAgentId" | "archivedAt">,
userId: string | null | undefined,
recordedAt?: number,
options?: { awaitingRead?: boolean },
) {
if (issue.conversationAgentId || issue.archivedAt) return;
const storageKey = getRecentTasksStorageKey(issue.companyId, userId);
@@ -170,6 +177,7 @@ export function recordRecentTask(
...snapshot,
// A comment can promote activity even when its task details are stale.
recordedAt: Math.max(activityAt, snapshot.recordedAt),
...(options?.awaitingRead ? { awaitingRead: true } : {}),
};
if (
existing
@@ -181,6 +189,7 @@ export function recordRecentTask(
&& sameBlockerAttention(existing.blockerAttention, entry.blockerAttention)
&& existing.recordedAt === entry.recordedAt
&& existing.snapshotUpdatedAt === entry.snapshotUpdatedAt
&& existing.awaitingRead === entry.awaitingRead
) return;
writeRecentTasks(
@@ -213,12 +222,6 @@ export function recordStartedRoutineRunTask(
writeRecentTasks(storageKey, [entry, ...current.filter((candidate) => candidate.id !== issueId)]);
}
// Entries pruned in this tab, kept so a read that confirms a visit later can
// restore them. Opening a task records the visit before its read is saved, so
// a summary read can race ahead of the read state that makes it eligible.
const prunedThisSession = new Map<string, RecentTaskEntry>();
const prunedKey = (storageKey: string, issueId: string) => `${storageKey}\n${issueId}`;
export function pruneRecentTasks(
storageKey: string,
companyId: string,
@@ -226,27 +229,24 @@ export function pruneRecentTasks(
) {
if (removeIds.size === 0) return;
const current = readRecentTasks(storageKey, companyId);
const next = current.filter((entry) => !removeIds.has(entry.id));
if (next.length === current.length) return;
for (const entry of current) {
if (removeIds.has(entry.id)) prunedThisSession.set(prunedKey(storageKey, entry.id), entry);
}
writeRecentTasks(storageKey, next);
// A visit waiting for its read cannot be judged yet; the read decides it.
const next = current.filter((entry) => entry.awaitingRead || !removeIds.has(entry.id));
if (next.length !== current.length) writeRecentTasks(storageKey, next);
}
/**
* Restores a visit that was pruned before its read state was saved. The next
* navigation summary still decides whether the entry is shown or pruned again.
* Ends the wait that a fresh visit started. Call it when the visit's read
* settles; the next navigation summary then shows or prunes the entry.
*/
export function restoreRecentTaskVisit(companyId: string, issueId: string, userId: string | null | undefined) {
export function settleRecentTaskVisit(companyId: string, issueId: string, userId: string | null | undefined) {
const storageKey = getRecentTasksStorageKey(companyId, userId);
const key = prunedKey(storageKey, issueId);
const pruned = prunedThisSession.get(key);
if (!pruned) return;
prunedThisSession.delete(key);
const current = readRecentTasks(storageKey, companyId);
if (current.some((entry) => entry.id === issueId)) return;
writeRecentTasks(storageKey, [pruned, ...current]);
if (!current.some((entry) => entry.id === issueId && entry.awaitingRead)) return;
writeRecentTasks(storageKey, current.map((entry) => {
if (entry.id !== issueId) return entry;
const { awaitingRead: _awaitingRead, ...settled } = entry;
return settled;
}));
}
export function updateRecentTaskSnapshots(
+12 -5
View File
@@ -220,7 +220,7 @@ import {
upsertInterruptedRun,
} from "../lib/optimistic-issue-runs";
import { useProjectOrder } from "../hooks/useProjectOrder";
import { recordRecentTask, restoreRecentTaskVisit } from "../lib/recent-tasks";
import { recordRecentTask, settleRecentTaskVisit } from "../lib/recent-tasks";
import { modelStillUnavailableToast } from "../lib/model-retry-refusal";
import {
relativeTime,
@@ -3902,7 +3902,11 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS
if (!(streamlinedUiEnabled || coordinatorWorkspaceEnabled) || !loadedIssue || !sessionResolved
|| conversation || loadedIssue.conversationAgentId || loadedIssue.hiddenAt) return;
const visit = `${loadedIssue.companyId}:${currentUserId}:${loadedIssue.id}`;
recordRecentTask(loadedIssue, currentUserId, recentTaskVisit.current === visit ? undefined : Date.now());
const freshVisit = recentTaskVisit.current !== visit;
// A fresh visit waits for its read unless that read is already confirmed.
const readConfirmed = readReceiptRef.current?.issueId === loadedIssue.id && readReceiptRef.current.confirmed;
recordRecentTask(loadedIssue, currentUserId, freshVisit ? Date.now() : undefined,
freshVisit && !readConfirmed ? { awaitingRead: true } : undefined);
recentTaskVisit.current = visit;
}, [loadedIssue, currentUserId, sessionResolved, streamlinedUiEnabled, coordinatorWorkspaceEnabled, conversation]);
const { data: boardAccess } = useQuery({
@@ -4686,9 +4690,8 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS
},
onSuccess: (_data, receipt) => {
receipt.confirmed = true;
// The saved read makes an opened automatic task eligible for Recents. A
// summary read that ran before it may have pruned the visit already.
restoreRecentTaskVisit(receipt.companyId, receipt.issueId, currentUserId);
// Settle before the refresh below so the new summary judges this visit.
settleRecentTaskVisit(receipt.companyId, receipt.issueId, currentUserId);
// A late receipt belongs to its original company, even after navigation.
queryClient.invalidateQueries({ queryKey: queryKeys.issues.summaries(receipt.companyId) });
if (selectedCompanyId) {
@@ -4708,6 +4711,10 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS
},
onFinished: (receipt) => {
pendingReadReceiptsRef.current.delete(receipt.issueId);
// The saved read makes an opened automatic task eligible for Recents.
// Until now Recents kept the visit without judging it; after a failed
// read, the next summary prunes it unless the person qualifies otherwise.
settleRecentTaskVisit(receipt.companyId, receipt.issueId, currentUserId);
// An older read can settle after navigation has changed the mutation observer.
if (readReceiptRef.current === receipt) setReadReceiptRevision(revision => revision + 1);
},