Skip to content

Commit 0128290

Browse files
fix(web): a thread opened before its shell loads still clears its visit state
The thread structure leaves out lastVisitedAt. Before the thread reaches the shell index, ChatView builds its shell from that structure, so the shell had no visit mark and "Woke from snooze" stayed after the visit. That fallback now takes lastVisitedAt from lastVisitedAtAtom, a live binding of just the visit mark. With a shell present it is not subscribed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 008e756 commit 0128290

5 files changed

Lines changed: 62 additions & 13 deletions

File tree

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,14 +1629,18 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) {
16291629

16301630
/**
16311631
* A thread shell built from its loaded detail, for direct links that hydrate the detail first.
1632-
* `updatedAt` is passed live: the thread structure leaves it out.
1632+
* `updatedAt` and `lastVisitedAt` are passed live: the thread structure leaves them out.
16331633
*/
16341634
export function presentThreadShellFromProjection(
16351635
environmentId: EnvironmentId,
16361636
threadId: ThreadId,
16371637
projection: ThreadStructure,
1638-
updatedAt: ThreadProjection["updatedAt"],
1638+
live: {
1639+
readonly updatedAt: ThreadProjection["updatedAt"];
1640+
readonly lastVisitedAt: ThreadProjection["thread"]["lastVisitedAt"];
1641+
},
16391642
) {
1643+
const { updatedAt, lastVisitedAt } = live;
16401644
const latestRun = deriveLatestThreadRun(projection);
16411645
const runtime = deriveThreadRuntime({ ...projection, updatedAt });
16421646
const pendingRuntimeRequest =
@@ -1645,6 +1649,7 @@ export function presentThreadShellFromProjection(
16451649
return {
16461650
...presentThreadShell(environmentId, {
16471651
...projection.thread,
1652+
lastVisitedAt,
16481653
latestRunId: latestRun?.runId ?? null,
16491654
activeRunId: runtime?.activeRunId ?? null,
16501655
status: runtime?.status ?? "idle",

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ import {
236236
useProject,
237237
useQuietThreadProjection,
238238
useThreadUpdatedAt,
239+
useThreadLastVisitedAt,
239240
useQuietThreadVisibleTurnItems,
240241
useThreadStatus,
241242
useThreadHistory,
@@ -464,19 +465,17 @@ export default function ChatView(props: ChatViewProps) {
464465
});
465466
const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef);
466467
const serverProjection = serverThreadProjection?.projection ?? null;
467-
// Only the shell-less fallback needs it; with a shell, the shell carries it.
468-
const fallbackUpdatedAt = useThreadUpdatedAt(
469-
serverThreadShell === null ? routeThreadDetailRef : null,
470-
);
468+
// Only the shell-less fallback needs these; with a shell, the shell carries them.
469+
const fallbackDetailRef = serverThreadShell === null ? routeThreadDetailRef : null;
470+
const fallbackUpdatedAt = useThreadUpdatedAt(fallbackDetailRef);
471+
const fallbackLastVisitedAt = useThreadLastVisitedAt(fallbackDetailRef);
471472
const serverThread =
472473
serverThreadShell !== null || serverProjection === null || fallbackUpdatedAt === null
473474
? serverThreadShell
474-
: presentThreadShellFromProjection(
475-
environmentId,
476-
threadId,
477-
serverProjection,
478-
fallbackUpdatedAt,
479-
);
475+
: presentThreadShellFromProjection(environmentId, threadId, serverProjection, {
476+
updatedAt: fallbackUpdatedAt,
477+
lastVisitedAt: fallbackLastVisitedAt,
478+
});
480479
const promptRef = useRef("");
481480
const localComposerRef = useRef<ChatComposerHandle | null>(null);
482481
const composerRef = useComposerHandleContext() ?? localComposerRef;

‎apps/web/src/state/entities.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
6565
const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
6666
Object.freeze([]),
6767
).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty"));
68+
const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
69+
Atom.withLabel("web-thread-last-visited:empty"),
70+
);
6871
const EMPTY_UPDATED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
6972
Atom.withLabel("web-thread-updated-at:empty"),
7073
);
@@ -171,6 +174,13 @@ export function useQuietThreadProjection(
171174
);
172175
}
173176

177+
/** The detail's live visit mark, which the thread structure leaves out. */
178+
export function useThreadLastVisitedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
179+
return useAtomValue(
180+
ref === null ? EMPTY_LAST_VISITED_AT_ATOM : environmentThreadDetails.lastVisitedAtAtom(ref),
181+
);
182+
}
183+
174184
/** The detail's live `updatedAt`, which the thread structure leaves out. */
175185
export function useThreadUpdatedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
176186
return useAtomValue(

‎packages/client-runtime/src/state/threadDetail.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ describe("sameThreadStructure", () => {
9393
});
9494
});
9595

96+
describe("lastVisitedAtAtom", () => {
97+
it("follows a visit mark the quiet thread holds through", () => {
98+
const projection = withItem(assistant);
99+
const state = Atom.make(
100+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
101+
);
102+
const details = createEnvironmentThreadDetailAtoms(() => state);
103+
const ref = { environmentId: EnvironmentId.make("env"), threadId: v2ThreadId };
104+
const registry = AtomRegistry.make();
105+
const unmountQuiet = registry.mount(details.quietThreadAtom(ref));
106+
const unmountVisit = registry.mount(details.lastVisitedAtAtom(ref));
107+
const quietBefore = registry.get(details.quietThreadAtom(ref));
108+
109+
const visited = { ...projection, thread: { ...projection.thread, lastVisitedAt: later } };
110+
registry.set(
111+
state,
112+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(visited) }),
113+
);
114+
115+
expect(registry.get(details.quietThreadAtom(ref))).toBe(quietBefore);
116+
expect(registry.get(details.lastVisitedAtAtom(ref))).toBe(later);
117+
unmountQuiet();
118+
unmountVisit();
119+
registry.dispose();
120+
});
121+
});
122+
96123
describe("quietThreadAtom", () => {
97124
const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
98125

‎packages/client-runtime/src/state/threadDetail.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row
8080
}
8181

8282
// The open thread is marked visited several times while a reply streams. Visit
83-
// state is read from the shell, so a mark is not structure.
83+
// state is read from the shell, or from lastVisitedAtAtom while the thread is
84+
// not in the shell index yet, so a mark is not structure.
8485
function toThreadStructureThread(thread: AppThread): ThreadStructureThread {
8586
const { updatedAt: _updatedAt, lastVisitedAt: _lastVisitedAt, ...structure } = thread;
8687
return structure;
@@ -344,6 +345,12 @@ export function createEnvironmentThreadDetailAtoms<E>(
344345
}).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
345346
});
346347

348+
const lastVisitedAtAtomFamily = Atom.family((key: string) =>
349+
Atom.make(
350+
(get) =>
351+
Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.lastVisitedAt ?? null,
352+
).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-last-visited:${key}`)),
353+
);
347354
const updatedAtAtomFamily = Atom.family((key: string) =>
348355
Atom.make(
349356
(get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.updatedAt ?? null,
@@ -412,6 +419,7 @@ export function createEnvironmentThreadDetailAtoms<E>(
412419
return {
413420
worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
414421
updatedAtAtom: (ref: ScopedThreadRef) => updatedAtAtomFamily(threadKey(ref)),
422+
lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
415423
pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
416424
queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
417425
queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),

0 commit comments

Comments
 (0)