Skip to content

Commit 9b8bbd6

Browse files
fix(web): a thread opened before its shell loads still clears its visit state
The quiet thread holds through visit marks. Before the thread reaches the shell index, ChatView builds its shell from that quiet projection, so a visit kept the stale lastVisitedAt 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 827b21d commit 9b8bbd6

5 files changed

Lines changed: 75 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,11 +1626,15 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) {
16261626
];
16271627
}
16281628

1629-
/** A thread shell built from its loaded detail, for direct links that hydrate the detail first. */
1629+
/**
1630+
* A thread shell built from its loaded detail, for direct links that hydrate the detail first.
1631+
* `lastVisitedAt` is passed live: the quiet projection holds through visit marks.
1632+
*/
16301633
export function presentThreadShellFromProjection(
16311634
environmentId: EnvironmentId,
16321635
threadId: ThreadId,
16331636
projection: ThreadProjection,
1637+
lastVisitedAt: ThreadProjection["thread"]["lastVisitedAt"],
16341638
) {
16351639
const latestRun = deriveLatestThreadRun(projection);
16361640
const runtime = deriveThreadRuntime(projection);
@@ -1640,6 +1644,7 @@ export function presentThreadShellFromProjection(
16401644
return {
16411645
...presentThreadShell(environmentId, {
16421646
...projection.thread,
1647+
lastVisitedAt,
16431648
latestRunId: latestRun?.runId ?? null,
16441649
activeRunId: runtime?.activeRunId ?? null,
16451650
status: runtime?.status ?? "idle",

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ import {
234234
useEnvironmentSupportsServerBrowser,
235235
useProject,
236236
useQuietThreadProjection,
237+
useThreadLastVisitedAt,
237238
useQuietThreadVisibleTurnItems,
238239
useThreadStatus,
239240
useThreadHistory,
@@ -462,10 +463,19 @@ export default function ChatView(props: ChatViewProps) {
462463
});
463464
const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef);
464465
const serverProjection = serverThreadProjection?.projection ?? null;
466+
// Only the shell-less fallback needs it; with a shell, visit state comes from the shell.
467+
const fallbackLastVisitedAt = useThreadLastVisitedAt(
468+
serverThreadShell === null ? routeThreadDetailRef : null,
469+
);
465470
const serverThread =
466471
serverThreadShell !== null || serverProjection === null
467472
? serverThreadShell
468-
: presentThreadShellFromProjection(environmentId, threadId, serverProjection);
473+
: presentThreadShellFromProjection(
474+
environmentId,
475+
threadId,
476+
serverProjection,
477+
fallbackLastVisitedAt,
478+
);
469479
const promptRef = useRef("");
470480
const localComposerRef = useRef<ChatComposerHandle | null>(null);
471481
const composerRef = useComposerHandleContext() ?? localComposerRef;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type * as DateTime from "effect/DateTime";
12
import { useAtomValue } from "@effect/atom-react";
23
import type {
34
EnvironmentProject,
@@ -62,6 +63,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
6263
const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
6364
Object.freeze([]),
6465
).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty"));
66+
const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
67+
Atom.withLabel("web-thread-last-visited:empty"),
68+
);
6569
const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe(
6670
Atom.withLabel("web-thread-history:empty"),
6771
);
@@ -163,6 +167,13 @@ export function useQuietThreadProjection(ref: ScopedThreadRef | null): Environme
163167
);
164168
}
165169

170+
/** The detail's live visit mark, which the quiet projection holds through. */
171+
export function useThreadLastVisitedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
172+
return useAtomValue(
173+
ref === null ? EMPTY_LAST_VISITED_AT_ATOM : environmentThreadDetails.lastVisitedAtAtom(ref),
174+
);
175+
}
176+
166177
export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
167178
return useAtomValue(
168179
ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { MessageId, RunId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts";
1+
import {
2+
EnvironmentId,
3+
MessageId,
4+
RunId,
5+
TurnItemId,
6+
type OrchestrationV2TurnItem,
7+
} from "@t3tools/contracts";
28
import { describe, expect, it } from "@effect/vitest";
39
import * as DateTime from "effect/DateTime";
10+
import * as Option from "effect/Option";
11+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
412

513
import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
6-
import { sameThreadStructure } from "./threadDetail.ts";
14+
import { createEnvironmentThreadDetailAtoms, sameThreadStructure } from "./threadDetail.ts";
15+
import { EMPTY_ENVIRONMENT_THREAD_STATE } from "./threadState.ts";
716

817
const later = DateTime.makeUnsafe("2026-06-20T00:00:01.000Z");
918

@@ -82,3 +91,30 @@ describe("sameThreadStructure", () => {
8291
expect(sameThreadStructure(before, { ...before, runs: [] })).toBe(false);
8392
});
8493
});
94+
95+
describe("lastVisitedAtAtom", () => {
96+
it("follows a visit mark the quiet thread holds through", () => {
97+
const projection = withItem(assistant);
98+
const state = Atom.make(
99+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
100+
);
101+
const details = createEnvironmentThreadDetailAtoms(() => state);
102+
const ref = { environmentId: EnvironmentId.make("env"), threadId: v2ThreadId };
103+
const registry = AtomRegistry.make();
104+
const unmountQuiet = registry.mount(details.quietThreadAtom(ref));
105+
const unmountVisit = registry.mount(details.lastVisitedAtAtom(ref));
106+
const quietBefore = registry.get(details.quietThreadAtom(ref));
107+
108+
const visited = { ...projection, thread: { ...projection.thread, lastVisitedAt: later } };
109+
registry.set(
110+
state,
111+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(visited) }),
112+
);
113+
114+
expect(registry.get(details.quietThreadAtom(ref))).toBe(quietBefore);
115+
expect(registry.get(details.lastVisitedAtAtom(ref))).toBe(later);
116+
unmountQuiet();
117+
unmountVisit();
118+
registry.dispose();
119+
});
120+
});

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export function sameThreadStructure(
7272
if (previous[key] !== next[key]) return false;
7373
}
7474
// The open thread is marked visited several times while a reply streams.
75-
// Visit state is read from the shell, so a mark is not structure.
75+
// Visit state is read from the shell, or from lastVisitedAtAtom while the
76+
// thread is not in the shell index yet, so a mark is not structure.
7677
if (
7778
previous.thread !== next.thread &&
7879
!sameAppThread(
@@ -268,6 +269,12 @@ export function createEnvironmentThreadDetailAtoms<E>(
268269
}).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
269270
});
270271

272+
const lastVisitedAtAtomFamily = Atom.family((key: string) =>
273+
Atom.make(
274+
(get) =>
275+
Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.lastVisitedAt ?? null,
276+
).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-last-visited:${key}`)),
277+
);
271278
const worktreePathAtomFamily = Atom.family((key: string) =>
272279
Atom.make(
273280
(get) =>
@@ -330,6 +337,7 @@ export function createEnvironmentThreadDetailAtoms<E>(
330337

331338
return {
332339
worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
340+
lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
333341
pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
334342
queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
335343
queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),

0 commit comments

Comments
 (0)