@devries-cameron20 / t3code-1 / commits / b5ced43833
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): local visit migration preserves mark unread (#25746)

Cement committed Nov 1, 2026 at 10:33 UTC b5ced43833576c3208d070f8a3a5d68e87f6481a
2 files changed +105 -4
apps/web/src/hooks/useThreadVisitedMigration.test.ts new
+103
@@ -0,0 +1,103 @@
1 +import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2 +import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
3 +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4 +import { EnvironmentId } from "@t3tools/contracts";
5 +import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
6 +import { describe, expect, it, vi } from "vite-plus/test";
7 +
8 +import { environmentThreadShells } from "../state/threads";
9 +import { makeThreadFixture } from "../test-fixtures";
10 +import { startThreadVisitedMigration } from "./useThreadVisitedMigration";
11 +
12 +const state = vi.hoisted(() => ({
13 + localVisits: {} as Record<string, string>,
14 +}));
15 +
16 +vi.mock("@t3tools/client-runtime/state/runtime", () => ({ runAtomCommand: vi.fn() }));
17 +vi.mock("../uiStateStore", () => ({
18 + useUiStateStore: { getState: () => ({ threadLastVisitedAtById: state.localVisits }) },
19 +}));
20 +vi.mock("../state/threads", async () => {
21 + const { Atom } = await import("effect/reactivity");
22 + return {
23 + environmentThreadShells: {
24 + threadShellsAtom: Atom.make<ReadonlyArray<EnvironmentThreadShell>>([]),
25 + },
26 + threadEnvironment: { visit: {} },
27 + };
28 +});
29 +
30 +const localVisit = "2026-10-27T10:00:00.000Z";
31 +
32 +function setup(threads: ReadonlyArray<EnvironmentThreadShell>, local = localVisit) {
33 + state.localVisits = Object.fromEntries(
34 + threads.map((thread) => [
35 + scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
36 + local,
37 + ]),
38 + );
39 + vi.mocked(runAtomCommand).mockReset().mockResolvedValue(AsyncResult.success(undefined));
40 + const tasks: Array<() => void> = [];
41 + const registry = AtomRegistry.make({
42 + scheduleTask: (task) => {
43 + tasks.push(task);
44 + return () => {};
45 + },
46 + });
47 + const shells = environmentThreadShells.threadShellsAtom;
48 + if (!Atom.isWritable(shells)) throw new Error("Expected writable test shell state");
49 + registry.set(shells, threads);
50 + const stop = startThreadVisitedMigration(registry);
51 + return {
52 + setThreads(next: ReadonlyArray<EnvironmentThreadShell>) {
53 + registry.set(shells, next);
54 + for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task();
55 + },
56 + [Symbol.dispose]() {
57 + stop();
58 + registry.dispose();
59 + },
60 + };
61 +}
62 +
63 +describe("startThreadVisitedMigration", () => {
64 + it("preserves mark unread when a client with a newer local visit reloads", () => {
65 + using _migration = setup([makeThreadFixture({ lastVisitedAt: "2026-10-27T09:59:59.999Z" })]);
66 +
67 + expect(runAtomCommand).not.toHaveBeenCalled();
68 + });
69 +
70 + it("seeds unset visits once per environment and thread", () => {
71 + const thread = makeThreadFixture({ lastVisitedAt: null });
72 + const remote = { ...thread, environmentId: EnvironmentId.make("remote") };
73 + using migration = setup([thread, remote]);
74 +
75 + expect(vi.mocked(runAtomCommand).mock.calls.map((call) => call[2])).toEqual([
76 + {
77 + environmentId: thread.environmentId,
78 + input: { threadId: thread.id, visitedAt: localVisit },
79 + },
80 + {
81 + environmentId: remote.environmentId,
82 + input: { threadId: remote.id, visitedAt: localVisit },
83 + },
84 + ]);
85 + migration.setThreads([{ ...thread }, { ...remote }]);
86 + expect(runAtomCommand).toHaveBeenCalledTimes(2);
87 + });
88 +
89 + it("waits for older servers to support visits before migrating", () => {
90 + const thread = makeThreadFixture();
91 + expect(thread.lastVisitedAt).toBeUndefined();
92 + using migration = setup([thread]);
93 + expect(runAtomCommand).not.toHaveBeenCalled();
94 +
95 + migration.setThreads([{ ...thread, lastVisitedAt: null }]);
96 + expect(runAtomCommand).toHaveBeenCalledTimes(1);
97 + });
98 +
99 + it.each(["", "not-a-date"])("ignores invalid local visits: %s", (local) => {
100 + using _migration = setup([makeThreadFixture({ lastVisitedAt: null })], local);
101 + expect(runAtomCommand).not.toHaveBeenCalled();
102 + });
103 +});
apps/web/src/hooks/useThreadVisitedMigration.ts
+2 -4
@@ -15,8 +15,8 @@ export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry)
15 const migratedThreadKeys = new Set<string>();
16 const migrate = (threads: ReadonlyArray<EnvironmentThreadShell>) => {
17 for (const thread of threads) {
18 - // Missing means unsupported; retain the local value until the server upgrades.
19 - if (thread.lastVisitedAt === undefined) continue;
18 + // An established server watermark may be deliberately older after Mark unread.
19 + if (thread.lastVisitedAt !== null) continue;
20 const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
21 if (migratedThreadKeys.has(threadKey)) continue;
22 migratedThreadKeys.add(threadKey);
@@ -24,8 +24,6 @@ export function startThreadVisitedMigration(registry: AtomRegistry.AtomRegistry)
24 if (!local) continue;
25 const localMs = Date.parse(local);
26 if (!Number.isFinite(localMs)) continue;
27 - const serverMs = thread.lastVisitedAt === null ? null : Date.parse(thread.lastVisitedAt);
28 - if (serverMs !== null && Number.isFinite(serverMs) && serverMs >= localMs) continue;
27 void runAtomCommand(
28 registry,
29 threadEnvironment.visit,