Skip to content

Commit abe303b

Browse files
t3dotggclaude
andcommitted
fix(mobile): held rows show on disconnect, and a move waits for current rows
Review findings on the steady thread list: - Rows that wait now show when their environment disconnects. Before, they stayed on the older rows until it reconnected. - Events that follow a connection's server snapshot wait until the environment is live, so each connection changes the list at most twice. - An environment that already shows its server snapshot does not hold others. - A move in a list is refused while its rows wait to show. - The splash gate reads every environment's cache state, so no read waits for the one before it. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 8468274 commit abe303b

7 files changed

Lines changed: 168 additions & 88 deletions

File tree

‎apps/mobile/src/features/home/useThreadListActions.test.ts‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const state = vi.hoisted(() => ({
1616
connectedEnvironmentIds: [] as EnvironmentId[],
1717
pendingOrder: null as object | null,
1818
dropBusy: false,
19+
holdingRows: false,
1920
failedAction: null as string | null,
2021
archivedRefreshes: [] as string[],
2122
scopes: new Map<string, Set<string>>(),
@@ -78,26 +79,28 @@ vi.mock("../../state/atom-registry", () => ({
7879
? state.connectedEnvironmentIds
7980
: atom === "thread-drop-busy"
8081
? state.dropBusy
81-
: atom === "thread-shells"
82-
? state.shells
83-
: atom === "queued-thread-keys"
84-
? new Set<string>()
85-
: new Map(
86-
[...state.scopes.keys()].map((environmentId) => [
87-
environmentId,
88-
{
89-
environment: {
90-
capabilities: {
91-
threadSettlement: true,
92-
threadSnooze: true,
93-
threadPinning: true,
94-
threadPinReorder: true,
95-
threadTitleRegeneration: true,
82+
: atom === "steady-shells-holding"
83+
? state.holdingRows
84+
: atom === "thread-shells"
85+
? state.shells
86+
: atom === "queued-thread-keys"
87+
? new Set<string>()
88+
: new Map(
89+
[...state.scopes.keys()].map((environmentId) => [
90+
environmentId,
91+
{
92+
environment: {
93+
capabilities: {
94+
threadSettlement: true,
95+
threadSnooze: true,
96+
threadPinning: true,
97+
threadPinReorder: true,
98+
threadTitleRegeneration: true,
99+
},
96100
},
97101
},
98-
},
99-
]),
100-
),
102+
]),
103+
),
101104
},
102105
}));
103106
vi.mock("../../state/use-atom-command", () => ({
@@ -123,6 +126,7 @@ vi.mock("../../state/thread-order", () => ({
123126
vi.mock("../../state/threads", () => ({
124127
environmentThreadShells: { threadShellsAtom: "thread-shells" },
125128
steadyNavigationThreadShellsAtom: "thread-shells",
129+
steadyShellsHoldingAtom: "steady-shells-holding",
126130
threadEnvironment: Object.fromEntries(
127131
[
128132
"archive",
@@ -191,6 +195,7 @@ beforeEach(() => {
191195
state.archivedRefreshes = [];
192196
state.pendingOrder = null;
193197
state.dropBusy = false;
198+
state.holdingRows = false;
194199
state.scopes = new Map([
195200
[primaryEnvironmentId, new Set([AuthOrchestrationOperateScope])],
196201
[otherEnvironmentId, new Set<string>()],
@@ -272,6 +277,12 @@ describe("pinned thread operation permissions", () => {
272277
}),
273278
];
274279

280+
// Rows that wait to show can hold order keys the server has replaced.
281+
state.holdingRows = true;
282+
expect(await useThreadListActions().moveThread(moved, "up")).toBe(false);
283+
expect(state.requests).toEqual([]);
284+
state.holdingRows = false;
285+
275286
expect(await useThreadListActions().moveThread(moved, "up")).toBe(true);
276287
expect(state.requests).toEqual([
277288
expect.objectContaining({ action: "reorderPin", environmentId: primaryEnvironmentId }),

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { readEnvironmentScope } from "../../state/session";
2020
import {
2121
environmentThreadShells,
2222
steadyNavigationThreadShellsAtom,
23+
steadyShellsHoldingAtom,
2324
threadEnvironment,
2425
} from "../../state/threads";
2526
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
@@ -576,6 +577,9 @@ export function useThreadListActions(): {
576577
const moveThread = async (thread: EnvironmentThreadShell, direction: ThreadMoveDestination) => {
577578
if (!checkThreadOperationPermission(thread, "Could not move thread")) return false;
578579
if (getPendingThreadOrder() !== null || appAtomRegistry.get(threadDropBusyAtom)) return false;
580+
// The rows on screen can be older than the server's while they wait to show. A move
581+
// planned from them could act on a pin or an order key that the server has replaced.
582+
if (appAtomRegistry.get(steadyShellsHoldingAtom)) return false;
579583
const shells = appAtomRegistry.get(steadyNavigationThreadShellsAtom);
580584
const current = shells.find(
581585
(row) => row.id === thread.id && row.environmentId === thread.environmentId,

‎apps/mobile/src/state/shell.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ const shellCacheReadAtom = Atom.family((environmentId: EnvironmentId) =>
3939
export const shellCachesReadAtom = Atom.make((get) => {
4040
const catalog = get(environmentCatalog.catalogValueAtom);
4141
if (!catalog.isReady) return false;
42+
// Reads every environment, so each one loads its cache now and not after the one before it.
43+
let allRead = true;
4244
for (const environmentId of enabledEnvironmentIds(catalog)) {
43-
if (!get(shellCacheReadAtom(environmentId))) return false;
45+
if (!get(shellCacheReadAtom(environmentId))) allRead = false;
4446
}
45-
return true;
47+
return allRead;
4648
}).pipe(Atom.withLabel("mobile-shell-caches-read"));
4749

4850
export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {

‎apps/mobile/src/state/threads.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const steadyShells = createSteadyShellAtoms({
5757
* The threads that lists arrange. Rows wait while their environment synchronizes, so a list
5858
* changes once when environments connect, not once per environment.
5959
*/
60+
export const steadyShellsHoldingAtom = steadyShells.holdingAtom;
6061
export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({
6162
catalogValueAtom: environmentCatalog.catalogValueAtom,
6263
snapshotAtom: steadyShells.snapshotAtom,

‎docs/internals/connection-runtime.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ on which data is current.
7878
Each environment synchronizes at its own time, so a thread list that follows every
7979
snapshot moves once per environment on each cold start and resume. Mobile lists read
8080
[steady shells](../../packages/client-runtime/src/state/steadyShells.ts): an
81-
environment's rows stay as shown until its connection's server snapshot arrives (or,
82-
for a replay of events, until it is synchronized), and environments that get there
83-
within two seconds of each other show together. Only an environment on
84-
its first connection attempt is waited for; a retry can take minutes. Point reads (a
81+
environment's rows stay as shown until its connection's server snapshot arrives,
82+
it is synchronized, or it disconnects, and environments that get there within two
83+
seconds of each other show together. Only an environment that is connected or on
84+
its first attempt, and still owes its snapshot, is waited for; a retry can take
85+
minutes. A move in a list is refused while its rows wait, because its plan would
86+
use order keys the server may have replaced. Point reads (a
8587
thread's own shell, route checks) keep the live snapshot, where a row the server just
8688
sent must not look missing. The decision reads the snapshot's own sequence: a second
8789
atom derived from the same shell state can be one update behind the rows. The mobile

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

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ const snapshot = (snapshotSequence: number): OrchestrationV2ShellSnapshot => ({
3030
const shell = (
3131
environmentId: EnvironmentId,
3232
value: OrchestrationV2ShellSnapshot | null,
33-
phase: "connecting" | "retrying" | "live",
33+
phase: "connecting" | "disconnected" | "live",
3434
generation = 0,
3535
): SteadyShellInput => ({
3636
environmentId,
3737
snapshot: value,
3838
serverSequence: value?.snapshotSequence ?? null,
3939
status: phase === "live" ? "live" : phase === "connecting" ? "synchronizing" : "cached",
4040
generation,
41-
connecting: phase !== "retrying",
41+
connecting: phase !== "disconnected",
4242
});
4343
const shown = (state: SteadyShellsState, environmentId: EnvironmentId) =>
4444
state.shells.get(environmentId)?.shown;
@@ -63,17 +63,73 @@ describe("stepSteadyShells", () => {
6363
expect(state.releaseAt).toBeNull();
6464
});
6565

66-
it("shows a connection's server snapshot when it arrives, before the socket confirms it", () => {
66+
it("shows a connection's server snapshot when it arrives, and the events after it when live", () => {
6767
const [cached, fromServer, withEvents] = [snapshot(7), snapshot(9), snapshot(10)];
6868
let state = stepSteadyShells(EMPTY_STEADY_SHELLS_STATE, [shell(A, cached, "connecting")], 0);
6969
state = stepSteadyShells(state, [shell(A, fromServer, "connecting", 1)], 10);
7070
expect(shown(state, A)).toBe(fromServer);
7171
state = stepSteadyShells(state, [shell(A, withEvents, "connecting", 1)], 20);
72-
expect(shown(state, A)).toBe(withEvents);
72+
expect(shown(state, A)).toBe(fromServer);
7373
state = stepSteadyShells(state, [shell(A, withEvents, "live", 1)], 30);
7474
expect(shown(state, A)).toBe(withEvents);
7575
});
7676

77+
it("does not wait for an environment that already shows its server snapshot", () => {
78+
const [cachedA, cachedB, serverA, serverB] = [
79+
snapshot(1),
80+
snapshot(2),
81+
snapshot(3),
82+
snapshot(4),
83+
];
84+
let state = stepSteadyShells(
85+
EMPTY_STEADY_SHELLS_STATE,
86+
[shell(A, cachedA, "connecting"), shell(B, cachedB, "connecting")],
87+
0,
88+
);
89+
state = stepSteadyShells(
90+
state,
91+
[shell(A, cachedA, "connecting"), shell(B, serverB, "connecting", 1)],
92+
10,
93+
);
94+
expect(shown(state, B)).toBe(cachedB);
95+
state = stepSteadyShells(
96+
state,
97+
[shell(A, serverA, "connecting", 2), shell(B, serverB, "connecting", 1)],
98+
20,
99+
);
100+
expect(shown(state, A)).toBe(serverA);
101+
expect(shown(state, B)).toBe(serverB);
102+
// B is not live yet, and A's next rows do not wait for it.
103+
const liveA = snapshot(5);
104+
state = stepSteadyShells(
105+
state,
106+
[shell(A, liveA, "live", 2), shell(B, serverB, "connecting", 1)],
107+
30,
108+
);
109+
expect(shown(state, A)).toBe(liveA);
110+
});
111+
112+
it("shows rows that wait when their environment disconnects", () => {
113+
const [cachedA, cachedB, serverA] = [snapshot(7), snapshot(2), snapshot(9)];
114+
let state = stepSteadyShells(
115+
EMPTY_STEADY_SHELLS_STATE,
116+
[shell(A, cachedA, "connecting"), shell(B, cachedB, "connecting")],
117+
0,
118+
);
119+
state = stepSteadyShells(
120+
state,
121+
[shell(A, serverA, "connecting", 1), shell(B, cachedB, "connecting")],
122+
10,
123+
);
124+
expect(shown(state, A)).toBe(cachedA);
125+
const disconnected = [shell(A, serverA, "disconnected", 1), shell(B, cachedB, "connecting")];
126+
state = stepSteadyShells(state, disconnected, 20);
127+
expect(shown(state, A)).toBe(cachedA);
128+
expect(state.releaseAt).toBe(10 + WINDOW);
129+
state = stepSteadyShells(state, disconnected, 10 + WINDOW);
130+
expect(shown(state, A)).toBe(serverA);
131+
});
132+
77133
it("holds replayed events after a reconnect until the environment is live again", () => {
78134
const [first, replayed] = [snapshot(1), snapshot(4)];
79135
let state = stepSteadyShells(EMPTY_STEADY_SHELLS_STATE, [shell(A, first, "live", 1)], 0);
@@ -136,10 +192,14 @@ describe("stepSteadyShells", () => {
136192
const [cachedA, cachedB, liveA] = [snapshot(1), snapshot(2), snapshot(3)];
137193
let state = stepSteadyShells(
138194
EMPTY_STEADY_SHELLS_STATE,
139-
[shell(A, cachedA, "connecting"), shell(B, cachedB, "retrying")],
195+
[shell(A, cachedA, "connecting"), shell(B, cachedB, "disconnected")],
140196
0,
141197
);
142-
state = stepSteadyShells(state, [shell(A, liveA, "live"), shell(B, cachedB, "retrying")], 500);
198+
state = stepSteadyShells(
199+
state,
200+
[shell(A, liveA, "live"), shell(B, cachedB, "disconnected")],
201+
500,
202+
);
143203
expect(shown(state, A)).toBe(liveA);
144204
});
145205

0 commit comments

Comments
 (0)