Skip to content

Commit e57c093

Browse files
t3dotggclaude
andauthored
perf(shell): cold connects skip older settled threads, 93% of the shell JSON (#24723)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2d620f5 commit e57c093

63 files changed

Lines changed: 3455 additions & 268 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ export function HomeScreen(props: HomeScreenProps) {
291291
)
292292
? [props.selectedEnvironmentId]
293293
: [];
294-
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
295-
const threadSearchMatchByKey = useThreadListSearchMatches(props.threads, threadSearch);
296294
const handleSwipeableWillOpen = (methods: SwipeableMethods) => {
297295
if (openSwipeableRef.current !== methods) {
298296
openSwipeableRef.current?.close();
@@ -380,6 +378,14 @@ export function HomeScreen(props: HomeScreenProps) {
380378

381379
const v2ProjectScopeKey = props.selectedProjectKey;
382380
const v2ScopedProjectGroup = findHomeProjectScope(projectScopes, v2ProjectScopeKey);
381+
const threadSearch = useThreadSearch(
382+
searchEnvironmentIds,
383+
props.searchQuery,
384+
v2ScopedProjectGroup === null
385+
? {}
386+
: { projectIds: v2ScopedProjectGroup.projectRefs.map((ref) => ref.projectId) },
387+
);
388+
const threadSearchMatchByKey = useThreadListSearchMatches(props.threads, threadSearch);
383389
const v2ProjectTitleByProjectKey = new Map(
384390
projectScopes.flatMap((scope) =>
385391
scope.projectRefs.map(
@@ -447,6 +453,7 @@ export function HomeScreen(props: HomeScreenProps) {
447453
layout: threadListV2Layout,
448454
items: threadListV2Items,
449455
contentOnlyThreadKeys,
456+
olderSettledShelf,
450457
} = useThreadListV2({
451458
threads: props.threads,
452459
pendingTasks: props.pendingTasks,
@@ -658,7 +665,9 @@ export function HomeScreen(props: HomeScreenProps) {
658665
};
659666

660667
const hasAnyThreads =
661-
props.threads.some((thread) => thread.archivedAt === null) || props.pendingTasks.length > 0;
668+
props.threads.some((thread) => thread.archivedAt === null) ||
669+
props.pendingTasks.length > 0 ||
670+
threadListV2Layout.settledCount > 0;
662671
const selectedEnvironmentLabel =
663672
props.selectedEnvironmentId === null
664673
? null
@@ -806,7 +815,15 @@ export function HomeScreen(props: HomeScreenProps) {
806815
settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0 ? (
807816
<ThreadListV2ShowMoreRow
808817
hiddenCount={threadListV2Layout.hiddenSettledCount}
809-
onPress={showMoreSettled}
818+
{...(olderSettledShelf.loading
819+
? { state: "loading" as const }
820+
: olderSettledShelf.failed
821+
? { state: "failed" as const }
822+
: {})}
823+
onPress={() => {
824+
olderSettledShelf.retry();
825+
showMoreSettled();
826+
}}
810827
/>
811828
) : null
812829
}

‎apps/mobile/src/features/keyboard/CommandPalette.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,21 @@ export function CommandPalette(props: {
241241
.map((environment) => environment.environmentId),
242242
[environments],
243243
);
244-
const search = useThreadSearch(searchEnvironmentIds, query.startsWith(">") ? "" : query);
244+
// The palette matches environment names when there are several; older rows must match the same.
245+
const environmentLabels =
246+
environments.length > 1
247+
? Object.fromEntries(
248+
searchEnvironmentIds.flatMap((environmentId) => {
249+
const label = savedConnectionsById[environmentId]?.environmentLabel;
250+
return label === undefined ? [] : [[environmentId, label]];
251+
}),
252+
)
253+
: undefined;
254+
const search = useThreadSearch(
255+
searchEnvironmentIds,
256+
query.startsWith(">") ? "" : query,
257+
environmentLabels === undefined ? {} : { environmentLabels },
258+
);
245259
const contentMatchByKey = useMemo(
246260
() =>
247261
new Map(

‎apps/mobile/src/features/prompt-links/usePromptLinkResolver.ts‎

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Dialog from "../../components/ui/Dialog";
22
import { StackActions, useNavigation } from "@react-navigation/native";
33
import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
44
import {
5-
findPromptLinkProject,
5+
findPromptLinkProjects,
66
findPromptLinkPullRequestThread,
77
resolvePromptLinkModelSelection,
88
} from "@t3tools/client-runtime/prompt-links";
@@ -11,7 +11,7 @@ import {
1111
squashAtomCommandFailure,
1212
} from "@t3tools/client-runtime/state/runtime";
1313
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
14-
import type { EnvironmentId } from "@t3tools/contracts";
14+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
1515
import {
1616
parseNewThreadLink,
1717
type LinkedPrompt,
@@ -63,6 +63,9 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
6363
const loadPullRequest = useAtomQueryRunner(composerPullRequests.detail, {
6464
reportFailure: false,
6565
});
66+
const loadLinkedThreads = useAtomQueryRunner(composerPullRequests.linkedThreadsUncached, {
67+
reportFailure: false,
68+
});
6669
const search = promptLinkSearchFromParams(params);
6770
// Wait for cold-launch shells or existing repositories may be offered for cloning.
6871
const waitingForShells =
@@ -89,6 +92,61 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
8992
return resolved?.exact !== true;
9093
};
9194

95+
const openThread = (
96+
thread: { readonly environmentId: EnvironmentId; readonly id: ThreadId },
97+
prompt: LinkedDraftRequest["prompt"],
98+
) => {
99+
(navigation.getParent() ?? navigation).dispatch(
100+
StackActions.replace("Thread", {
101+
environmentId: String(thread.environmentId),
102+
threadId: String(thread.id),
103+
}),
104+
);
105+
if (prompt) {
106+
void applyLinkedPromptToThreadDraft(
107+
scopedThreadKey(thread.environmentId, thread.id),
108+
prompt,
109+
promptLinkNotice({ hiddenCharacters: prompt.hiddenCharacters }),
110+
);
111+
}
112+
};
113+
114+
// A pull request's thread can be an older settled thread no shell holds, so ask every
115+
// connected environment with the repository. A lookup on another one would wait forever.
116+
const findServerPullRequestThread = async (
117+
projects: ReadonlyArray<EnvironmentProject>,
118+
pullRequest: PromptLinkPullRequest,
119+
) => {
120+
const connected = new Set(
121+
connectedEnvironments
122+
.filter((environment) => environment.connectionState === "connected")
123+
.map((environment) => environment.environmentId),
124+
);
125+
const lookups = projects
126+
.filter((project) => connected.has(project.environmentId))
127+
.map(async (project) => {
128+
const linked = await loadLinkedThreads({
129+
environmentId: project.environmentId,
130+
input: {
131+
projectId: project.id,
132+
repository: promptLinkRepositoryName(pullRequest),
133+
number: pullRequest.number,
134+
},
135+
});
136+
const thread =
137+
linked._tag === "Success"
138+
? linked.value.threads.find((candidate) => candidate.archivedAt === null)
139+
: undefined;
140+
return thread && { environmentId: project.environmentId, id: thread.id };
141+
});
142+
// The most recent project wins without waiting for the lookups after it.
143+
for (const lookup of lookups) {
144+
const thread = await lookup;
145+
if (thread) return thread;
146+
}
147+
return undefined;
148+
};
149+
92150
const openProjectDraft = async (project: EnvironmentProject, request: LinkedDraftRequest) => {
93151
try {
94152
await waitForComposerDraftsLoaded();
@@ -234,19 +292,7 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
234292
if (link.kind === "pull-request") {
235293
const thread = findPromptLinkPullRequestThread(threads, link.pullRequest);
236294
if (thread) {
237-
(navigation.getParent() ?? navigation).dispatch(
238-
StackActions.replace("Thread", {
239-
environmentId: String(thread.environmentId),
240-
threadId: String(thread.id),
241-
}),
242-
);
243-
if (link.prompt) {
244-
void applyLinkedPromptToThreadDraft(
245-
scopedThreadKey(thread.environmentId, thread.id),
246-
link.prompt,
247-
promptLinkNotice({ hiddenCharacters: link.prompt.hiddenCharacters }),
248-
);
249-
}
295+
openThread(thread, link.prompt);
250296
return;
251297
}
252298
}
@@ -263,14 +309,20 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
263309
}
264310
const activityOf = (project: EnvironmentProject) =>
265311
lastActivity.get(`${project.environmentId}:${project.id}`) ?? "";
266-
const project = findPromptLinkProject(
312+
const projects = findPromptLinkProjects(
267313
flow.projectScopes
268314
.flatMap((scope) => scope.projects)
269315
.sort((left, right) => activityOf(right).localeCompare(activityOf(left))),
270316
repository,
271317
);
318+
const project = projects[0];
272319
if (project) {
273-
void openProjectDraft(project, request);
320+
const { pullRequest } = request;
321+
void (async () => {
322+
const thread = pullRequest && (await findServerPullRequestThread(projects, pullRequest));
323+
if (thread) openThread(thread, request.prompt);
324+
else await openProjectDraft(project, request);
325+
})();
274326
return;
275327
}
276328
Dialog.alert(`You don't have ${promptLinkRepositoryName(repository)}`, undefined, [

‎apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-en
4242
import { leaveSettingsPage } from "./settings-navigation";
4343
import { workspaceHasColumns } from "../workspace/workspace-selection-store";
4444
import { DEFAULT_SETTINGS_PAGE } from "../workspace/workspace-tab-navigation";
45+
import { readUnloadedThreadCount } from "../../state/olderSettledShelf";
4546

4647
export function SettingsProjectOverviewRouteScreen() {
4748
const insets = useSafeAreaInsets();
@@ -175,6 +176,7 @@ function ProjectOverviewContent(props: {
175176
hasOtherMembers: props.hasOtherMembers,
176177
displayName,
177178
threads,
179+
unloadedThreadCount: readUnloadedThreadCount(members, threads),
178180
});
179181
Dialog.confirm({
180182
title: plan.targetKind === "checkout" ? "Remove checkout" : "Remove project",

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { SymbolView } from "../../components/ui/AppSymbol";
1616
import { scopedThreadKey } from "../../lib/scopedEntities";
1717
import { environmentServerConfigsAtom } from "../../state/server";
1818
import { environmentThreadShells } from "../../state/threads";
19+
import { sortSettledThreads } from "@t3tools/client-runtime/state/thread-sort";
20+
import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
1921
import {
2022
pendingThreadOrderAtom,
2123
threadDropBusyAtom,
@@ -28,7 +30,12 @@ import {
2830
threadDragAction,
2931
type ThreadMoveDestination,
3032
} from "./threadOrder";
31-
import { getThreadListV2OrderedSection, threadListInboxReturns } from "./threadListV2";
33+
import {
34+
getThreadListV2OrderedSection,
35+
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
36+
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
37+
threadListInboxReturns,
38+
} from "./threadListV2";
3239
import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences";
3340

3441
const ROW_HEIGHT = 56;
@@ -40,6 +47,8 @@ type Row = {
4047
key: string;
4148
section: Section;
4249
thread?: EnvironmentThreadShell;
50+
/** The Settled section's "Show more" row. */
51+
more?: true;
4352
offset: number;
4453
height: number;
4554
};
@@ -158,6 +167,15 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
158167
const { workingShelfEnabled } = useThreadListV2ShelfPreferences();
159168
const [now, setNow] = useState(() => new Date().toISOString());
160169
const [expanded, setExpanded] = useState({ snoozed: false, settled: false });
170+
const [settledVisibleCount, setSettledVisibleCount] = useState(
171+
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
172+
);
173+
// Older settled threads load in pages, as in the thread list.
174+
const olderSettledShelf = useOlderSettledShelf({
175+
scopes: [...configs.keys()].map((environmentId) => ({ environmentId, projectIds: null })),
176+
expanded: expanded.settled,
177+
visibleCount: settledVisibleCount,
178+
});
161179
useEffect(() => {
162180
const wakeAt = Math.min(
163181
...threads.flatMap((thread) => {
@@ -201,9 +219,13 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
201219
? sortInboxThreadsByReturn(active, threadListInboxReturns.returnedAt)
202220
: active,
203221
snoozed: parked.filter((thread) => effectiveSnoozed(thread, { now })),
204-
settled: parked.filter((thread) => !effectiveSnoozed(thread, { now })),
222+
settled: sortSettledThreads(parked.filter((thread) => !effectiveSnoozed(thread, { now }))),
205223
};
206224
}, [threads, configs, now, queuedThreadKeys, pendingOrder, workingShelfEnabled]);
225+
const listedSettled = sections.settled.filter(olderSettledShelf.isListed);
226+
useOlderSettledShelfGrowth(olderSettledShelf, listedSettled);
227+
const visibleSettled = listedSettled.slice(0, settledVisibleCount);
228+
const settledCount = sections.settled.length + olderSettledShelf.unloadedCount(threads);
207229
const planners = useMemo(() => {
208230
const planner = (section: "pinned" | "active") =>
209231
createThreadMovePlanner({
@@ -232,13 +254,17 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
232254
result.push({ key: section, section, offset, height: HEADER_HEIGHT });
233255
offset += HEADER_HEIGHT;
234256
if ((section === "snoozed" || section === "settled") && !expanded[section]) continue;
235-
for (const thread of sections[section]) {
257+
for (const thread of section === "settled" ? visibleSettled : sections[section]) {
236258
result.push({ key: keyOf(thread), section, thread, offset, height: ROW_HEIGHT });
237259
offset += ROW_HEIGHT;
238260
}
261+
if (section === "settled" && settledCount > visibleSettled.length) {
262+
result.push({ key: "settled-more", section, more: true, offset, height: ROW_HEIGHT });
263+
offset += ROW_HEIGHT;
264+
}
239265
}
240266
return result;
241-
}, [sections, expanded]);
267+
}, [sections, expanded, visibleSettled, settledCount]);
242268
const list = useRef<FlatList<Row>>(null);
243269
const geometry = useRef({ height: 0, offset: 0 });
244270
const drag = useRef<Drag | null>(null);
@@ -507,6 +533,25 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
507533
}}
508534
/>
509535
</>
536+
) : item.more ? (
537+
<Pressable
538+
disabled={olderSettledShelf.loading}
539+
className="flex-1 justify-center self-stretch"
540+
onPress={() => {
541+
olderSettledShelf.retry();
542+
setSettledVisibleCount(
543+
(count) => count + THREAD_LIST_V2_SETTLED_PAGE_COUNT,
544+
);
545+
}}
546+
>
547+
<Text className="text-sm text-foreground-muted">
548+
{olderSettledShelf.loading
549+
? "Loading"
550+
: olderSettledShelf.failed
551+
? "Could not load older threads. Retry"
552+
: `Show more (${settledCount - visibleSettled.length} settled hidden)`}
553+
</Text>
554+
</Pressable>
510555
) : (
511556
<Pressable
512557
disabled={item.section === "pinned" || item.section === "active"}
@@ -519,7 +564,10 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
519564
>
520565
<Text className="text-sm font-t3-semibold text-foreground-muted">
521566
{item.section[0]!.toUpperCase() + item.section.slice(1)} (
522-
{sections[item.section].length})
567+
{item.section === "settled"
568+
? settledCount
569+
: sections[item.section].length}
570+
)
523571
</Text>
524572
</Pressable>
525573
)}

0 commit comments

Comments
 (0)