Skip to content

Commit b3b29e0

Browse files
committed
Merge remote-tracking branch 'origin/main' into perf/command-read-window
# Conflicts: # apps/server/src/orchestration-v2/Orchestrator.ts # apps/server/src/orchestration-v2/ProjectionStore.ts
2 parents 874df5d + 65c0195 commit b3b29e0

34 files changed

Lines changed: 1392 additions & 218 deletions

‎apps/mobile/src/App.tsx‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { installThreadOutboxDelivery } from "./state/threadOutboxDelivery";
44
import * as Linking from "expo-linking";
55
import * as SplashScreen from "expo-splash-screen";
66
import { requireNativeModule } from "expo";
7-
import { useEffect } from "react";
7+
import { useEffect, useState } from "react";
88
import { Platform, StatusBar } from "react-native";
99
import { GestureHandlerRootView } from "react-native-gesture-handler";
1010
import { KeyboardProvider } from "react-native-keyboard-controller";
@@ -21,7 +21,7 @@ import {
2121
import { pipe } from "effect/Function";
2222
import { DEFAULT_HOSTED_APP_URL } from "@t3tools/shared/connectAuth";
2323

24-
import { RegistryContext } from "@effect/atom-react";
24+
import { RegistryContext, useAtomValue } from "@effect/atom-react";
2525
import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";
2626
import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
2727
import * as Dialog from "./components/ui/Dialog";
@@ -50,7 +50,7 @@ import { RootStack, rootPathnameFromState, workspaceLocationFromState } from "./
5050
import { appAtomRegistry } from "./state/atom-registry";
5151
import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
5252
import { environmentThreadShells, threadEnvironment } from "./state/threads";
53-
import { environmentShell } from "./state/shell";
53+
import { environmentShell, shellCachesReadAtom } from "./state/shell";
5454
import { OverlayPortalHost } from "./components/ui/OverlayPortal";
5555
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
5656
import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
@@ -204,13 +204,28 @@ disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.ho
204204
for (const dispose of appServiceDisposers) dispose();
205205
});
206206

207+
// Cached shells are local reads that end in a fraction of this time; the limit only keeps a
208+
// failed read from holding the splash screen.
209+
const SPLASH_SHELL_CACHE_WAIT_MS = 1_500;
210+
207211
function SplashScreenCoordinator() {
208212
const { isReady } = useAppearancePreferences();
213+
const shellCachesRead = useAtomValue(shellCachesReadAtom);
214+
const [waitEnded, setWaitEnded] = useState(false);
215+
216+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the limit on how long the native splash screen waits for cached shells
217+
useEffect(() => {
218+
const timer = setTimeout(() => setWaitEnded(true), SPLASH_SHELL_CACHE_WAIT_MS);
219+
return () => clearTimeout(timer);
220+
}, []);
209221

210-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hides the native splash screen once appearance preferences have loaded
222+
// The thread list paints every environment's cached rows at once when the splash hides
223+
// after they are read.
224+
const canHide = isReady && (shellCachesRead || waitEnded);
225+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hides the native splash screen once preferences and cached shells have loaded
211226
useEffect(() => {
212-
if (isReady) void SplashScreen.hide();
213-
}, [isReady]);
227+
if (canHide) void SplashScreen.hide();
228+
}, [canHide]);
214229

215230
return null;
216231
}

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "@t3tools/client-runtime/state/project-grouping";
1616

1717
import { useProjects } from "../../state/entities";
18-
import { environmentThreadShells } from "../../state/threads";
18+
import { steadyNavigationThreadShellsAtom } from "../../state/threads";
1919
import { useLaunchingThreads, usePendingNewTasks } from "../../state/use-pending-new-tasks";
2020
import { useWorkspaceState } from "../../state/workspace";
2121
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
@@ -47,7 +47,7 @@ export function HomeRouteScreen() {
4747
// Stop rebuilding the streaming thread list while Thread covers Home.
4848
const visible = useHomeRouteVisible();
4949
const threads = useLaunchingThreads(
50-
useAtomValueWhileVisible(environmentThreadShells.navigationThreadShellsAtom, visible),
50+
useAtomValueWhileVisible(steadyNavigationThreadShellsAtom, visible),
5151
);
5252
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
5353
const { savedConnectionsById } = useSavedRemoteConnections();

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

Lines changed: 55 additions & 22 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", () => ({
@@ -121,10 +124,9 @@ vi.mock("../../state/thread-order", () => ({
121124
},
122125
}));
123126
vi.mock("../../state/threads", () => ({
124-
environmentThreadShells: {
125-
threadShellsAtom: "thread-shells",
126-
navigationThreadShellsAtom: "thread-shells",
127-
},
127+
environmentThreadShells: { threadShellsAtom: "thread-shells" },
128+
steadyNavigationThreadShellsAtom: "thread-shells",
129+
steadyShellsHoldingAtom: "steady-shells-holding",
128130
threadEnvironment: Object.fromEntries(
129131
[
130132
"archive",
@@ -193,6 +195,7 @@ beforeEach(() => {
193195
state.archivedRefreshes = [];
194196
state.pendingOrder = null;
195197
state.dropBusy = false;
198+
state.holdingRows = false;
196199
state.scopes = new Map([
197200
[primaryEnvironmentId, new Set([AuthOrchestrationOperateScope])],
198201
[otherEnvironmentId, new Set<string>()],
@@ -274,6 +277,12 @@ describe("pinned thread operation permissions", () => {
274277
}),
275278
];
276279

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+
277286
expect(await useThreadListActions().moveThread(moved, "up")).toBe(true);
278287
expect(state.requests).toEqual([
279288
expect.objectContaining({ action: "reorderPin", environmentId: primaryEnvironmentId }),
@@ -494,6 +503,25 @@ describe("thread action undo", () => {
494503
},
495504
);
496505

506+
it.each(["settleThread", "unpinThread"] as const)(
507+
"%s plans its undo from the live row when the list holds an older one",
508+
async (handler) => {
509+
const held = makeThread({ pinnedAt: "2026-09-02T00:00:00.000Z", pinOrderKey: "m" });
510+
const live = makeThread({
511+
pinnedAt: "2026-09-02T00:00:00.000Z",
512+
pinOrderKey: "t",
513+
snoozedUntil: "2099-01-01T09:00:00.000Z",
514+
});
515+
state.shells = [live];
516+
await useThreadListActions()[handler](held);
517+
await readThreadUndoNotice()!.undo();
518+
expect(state.requests.slice(-2)).toMatchObject([
519+
{ action: "pin", input: { threadId: live.id, orderKey: "t" } },
520+
{ action: "snooze", input: { threadId: live.id, snoozedUntil: live.snoozedUntil } },
521+
]);
522+
},
523+
);
524+
497525
// regression: #21401 restore archive and thread action undo
498526
it("does not restore snooze if undoing an unpin fails", async () => {
499527
const thread = makeThread({
@@ -570,5 +598,10 @@ describe("thread archive guard", () => {
570598
expect(state.requests).toEqual([]);
571599
expect(state.archivedRefreshes).toEqual([]);
572600
expect(state.alerts.at(-1)?.title).toBe("Could not archive thread");
601+
602+
// A list can hold a row from before the run started.
603+
state.shells = [thread];
604+
expect(await useThreadListActions().archiveThread(makeThread())).toBe(false);
605+
expect(state.requests).toEqual([]);
573606
});
574607
});

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import { appAtomRegistry } from "../../state/atom-registry";
1717
import { environmentServerConfigsAtom } from "../../state/server";
1818
import { connectedEnvironmentIdsAtom } from "../../state/presentation";
1919
import { readEnvironmentScope } from "../../state/session";
20-
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
20+
import {
21+
environmentThreadShells,
22+
steadyNavigationThreadShellsAtom,
23+
steadyShellsHoldingAtom,
24+
threadEnvironment,
25+
} from "../../state/threads";
2126
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
2227
import { useAtomCommand } from "../../state/use-atom-command";
2328
import {
@@ -83,6 +88,19 @@ function checkThreadOperationPermission(thread: EnvironmentThreadShell, title: s
8388
return false;
8489
}
8590

91+
/**
92+
* The live row of a thread that a list passes to an action. A list holds its rows while an
93+
* environment synchronizes (`steadyNavigationThreadShellsAtom`), so its row can be older than
94+
* the client's data: an undo plan or a guard read from it would use replaced state.
95+
*/
96+
function liveThread(thread: EnvironmentThreadShell): EnvironmentThreadShell {
97+
return (
98+
appAtomRegistry
99+
.get(environmentThreadShells.threadShellsAtom)
100+
.find((row) => row.id === thread.id && row.environmentId === thread.environmentId) ?? thread
101+
);
102+
}
103+
86104
function useThreadActionExecutor(
87105
onCompleted?: (action: ThreadListAction, thread: EnvironmentThreadShell) => void,
88106
) {
@@ -572,7 +590,10 @@ export function useThreadListActions(): {
572590
const moveThread = async (thread: EnvironmentThreadShell, direction: ThreadMoveDestination) => {
573591
if (!checkThreadOperationPermission(thread, "Could not move thread")) return false;
574592
if (getPendingThreadOrder() !== null || appAtomRegistry.get(threadDropBusyAtom)) return false;
575-
const shells = appAtomRegistry.get(environmentThreadShells.navigationThreadShellsAtom);
593+
// The rows on screen can be older than the server's while they wait to show. A move
594+
// planned from them could act on a pin or an order key that the server has replaced.
595+
if (appAtomRegistry.get(steadyShellsHoldingAtom)) return false;
596+
const shells = appAtomRegistry.get(steadyNavigationThreadShellsAtom);
576597
const current = shells.find(
577598
(row) => row.id === thread.id && row.environmentId === thread.environmentId,
578599
);
@@ -708,19 +729,20 @@ export function useThreadListActions(): {
708729
const confirmDeleteThread = (thread: EnvironmentThreadShell) =>
709730
confirmThreadDeletion(executeAction, thread);
710731

732+
// `moveThread` looks up its own row, and refuses while a list holds older rows.
711733
return {
712-
archiveThread,
713-
confirmDeleteThread,
714-
settleThread,
715-
snoozeThread,
716-
unsnoozeThread,
717-
unsettleThread,
718-
pinThread,
719-
unpinThread,
720-
setThreadAutoSettle,
734+
archiveThread: (thread) => archiveThread(liveThread(thread)),
735+
confirmDeleteThread: (thread) => confirmDeleteThread(liveThread(thread)),
736+
settleThread: (thread) => settleThread(liveThread(thread)),
737+
snoozeThread: (thread, snoozedUntil) => snoozeThread(liveThread(thread), snoozedUntil),
738+
unsnoozeThread: (thread) => unsnoozeThread(liveThread(thread)),
739+
unsettleThread: (thread) => unsettleThread(liveThread(thread)),
740+
pinThread: (thread) => pinThread(liveThread(thread)),
741+
unpinThread: (thread) => unpinThread(liveThread(thread)),
742+
setThreadAutoSettle: (thread, enabled) => setThreadAutoSettle(liveThread(thread), enabled),
721743
moveThread,
722-
renameThread,
723-
regenerateThreadTitle,
744+
renameThread: (thread) => renameThread(liveThread(thread)),
745+
regenerateThreadTitle: (thread) => regenerateThreadTitle(liveThread(thread)),
724746
};
725747
}
726748

‎apps/mobile/src/features/threads/ThreadArrangementSheet.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { scopedThreadKey } from "../../lib/scopedEntities";
1818
import { deriveThreadArrangementSizing } from "../../lib/layout";
1919
import { environmentServerConfigsAtom } from "../../state/server";
2020
import { threadListEnvironmentsAtom } from "../../state/presentation";
21-
import { environmentThreadShells } from "../../state/threads";
21+
import { steadyNavigationThreadShellsAtom, steadyShellsHoldingAtom } from "../../state/threads";
2222
import { sortSettledThreads } from "@t3tools/client-runtime/state/thread-sort";
2323
import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
2424
import {
@@ -170,12 +170,13 @@ function ThreadArrangementSheet(props: { onClose: () => void }) {
170170
baseFontSize: appearance.baseFontSize,
171171
fontScale,
172172
});
173-
const threads = useAtomValue(environmentThreadShells.navigationThreadShellsAtom);
173+
const threads = useAtomValue(steadyNavigationThreadShellsAtom);
174174
const configs = useAtomValue(environmentServerConfigsAtom);
175175
const listEnvironments = useAtomValue(threadListEnvironmentsAtom);
176176
const queuedThreadKeys = useAtomValue(queuedThreadKeysAtom);
177177
const pendingOrder = useAtomValue(pendingThreadOrderAtom);
178178
const dropBusy = useAtomValue(threadDropBusyAtom);
179+
const holdingRows = useAtomValue(steadyShellsHoldingAtom);
179180
const { moveThread } = useThreadListActions();
180181
const { workingShelfEnabled } = useThreadListV2ShelfPreferences();
181182
const now = useThreadListNow(threads);
@@ -489,6 +490,7 @@ function ThreadArrangementSheet(props: { onClose: () => void }) {
489490
disabled={
490491
dropBusy ||
491492
pendingOrder !== null ||
493+
holdingRows ||
492494
(!listEnvironments.pinReorderEnvironmentIds.has(
493495
thread.environmentId,
494496
) &&

‎apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
2727
import { AppText as Text } from "../../components/ui/AppText";
2828
import { Button } from "../../components/ui/Button";
2929
import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities";
30-
import { useProjects, useNavigationThreadShells } from "../../state/entities";
30+
import { useProjects } from "../../state/entities";
31+
import { steadyNavigationThreadShellsAtom } from "../../state/threads";
3132
import { useThreadSearch } from "../../state/queries";
3233
import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences";
3334
import { useThreadListNow } from "./use-thread-list-now";
@@ -121,7 +122,7 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
121122
const insets = useSafeAreaInsets();
122123
const { fabClearance, fabSize } = useAndroidControlSizing();
123124
const projects = useProjects();
124-
const threads = useNavigationThreadShells();
125+
const threads = useAtomValue(steadyNavigationThreadShellsAtom);
125126
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
126127
const { savedConnectionsById } = useSavedRemoteConnections();
127128
const openSwipeableRef = useRef<SwipeableMethods | null>(null);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { enabledEnvironmentIds } from "@t3tools/client-runtime/state/connections";
12
import {
23
createEnvironmentShellAtoms,
34
createEnvironmentShellSummaryAtom,
@@ -27,6 +28,25 @@ const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.erro
2728
// The empty placeholder stands until the environment has read its cached shell.
2829
const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE;
2930

31+
const shellCacheReadAtom = Atom.family((environmentId: EnvironmentId) =>
32+
Atom.make((get) => shellCacheRead(get(environmentShell.stateValueAtom(environmentId)))),
33+
);
34+
35+
/**
36+
* True once every enabled environment has read its cached shell. A list that paints before
37+
* then gets the cached rows of one environment at a time.
38+
*/
39+
export const shellCachesReadAtom = Atom.make((get) => {
40+
const catalog = get(environmentCatalog.catalogValueAtom);
41+
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;
44+
for (const environmentId of enabledEnvironmentIds(catalog)) {
45+
if (!get(shellCacheReadAtom(environmentId))) allRead = false;
46+
}
47+
return allRead;
48+
}).pipe(Atom.withLabel("mobile-shell-caches-read"));
49+
3050
export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
3151
const atom =
3252
environmentId === null

‎apps/mobile/src/state/thread-order.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
pendingThreadOrderAtom,
1313
} from "./thread-order";
1414
import { environmentServerConfigsAtom } from "./server";
15-
import { environmentThreadShells } from "./threads";
15+
import { steadyNavigationThreadShellsAtom } from "./threads";
1616

1717
vi.mock("./atom-registry", async () => {
1818
const { AtomRegistry } = await import("effect/reactivity");
@@ -21,7 +21,7 @@ vi.mock("./atom-registry", async () => {
2121
vi.mock("./threads", async () => {
2222
const { Atom } = await import("effect/reactivity");
2323
return {
24-
environmentThreadShells: { navigationThreadShellsAtom: Atom.make([]).pipe(Atom.keepAlive) },
24+
steadyNavigationThreadShellsAtom: Atom.make([]).pipe(Atom.keepAlive),
2525
};
2626
});
2727
vi.mock("./server", async () => {
@@ -36,7 +36,7 @@ vi.mock("./use-thread-outbox", async () => {
3636
type ServerConfigs = Atom.Type<typeof environmentServerConfigsAtom>;
3737
type ServerConfig = ServerConfigs extends ReadonlyMap<EnvironmentId, infer Config> ? Config : never;
3838
const configsAtom = environmentServerConfigsAtom as Atom.Writable<ServerConfigs, ServerConfigs>;
39-
const shellsAtom = environmentThreadShells.navigationThreadShellsAtom as Atom.Writable<
39+
const shellsAtom = steadyNavigationThreadShellsAtom as Atom.Writable<
4040
readonly EnvironmentThreadShell[],
4141
readonly EnvironmentThreadShell[]
4242
>;

0 commit comments

Comments
 (0)