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

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

perf(shell): cold connects skip older settled threads, 93% of the shell JSON (#24723)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 25, 2026 at 03:30 UTC e57c093cdd9032355c6251dd7192280b915b7a23
63 files changed +3455 -268
apps/mobile/src/features/home/HomeScreen.tsx
+21 -4
@@ -291,8 +291,6 @@ export function HomeScreen(props: HomeScreenProps) {
291 )
292 ? [props.selectedEnvironmentId]
293 : [];
294 - const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
295 - const threadSearchMatchByKey = useThreadListSearchMatches(props.threads, threadSearch);
294 const handleSwipeableWillOpen = (methods: SwipeableMethods) => {
295 if (openSwipeableRef.current !== methods) {
296 openSwipeableRef.current?.close();
@@ -380,6 +378,14 @@ export function HomeScreen(props: HomeScreenProps) {
378
379 const v2ProjectScopeKey = props.selectedProjectKey;
380 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);
389 const v2ProjectTitleByProjectKey = new Map(
390 projectScopes.flatMap((scope) =>
391 scope.projectRefs.map(
@@ -447,6 +453,7 @@ export function HomeScreen(props: HomeScreenProps) {
453 layout: threadListV2Layout,
454 items: threadListV2Items,
455 contentOnlyThreadKeys,
456 + olderSettledShelf,
457 } = useThreadListV2({
458 threads: props.threads,
459 pendingTasks: props.pendingTasks,
@@ -658,7 +665,9 @@ export function HomeScreen(props: HomeScreenProps) {
665 };
666
667 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;
671 const selectedEnvironmentLabel =
672 props.selectedEnvironmentId === null
673 ? null
@@ -806,7 +815,15 @@ export function HomeScreen(props: HomeScreenProps) {
815 settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0 ? (
816 <ThreadListV2ShowMoreRow
817 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 + }}
827 />
828 ) : null
829 }
apps/mobile/src/features/keyboard/CommandPalette.tsx
+15 -1
@@ -241,7 +241,21 @@ export function CommandPalette(props: {
241 .map((environment) => environment.environmentId),
242 [environments],
243 );
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 + );
259 const contentMatchByKey = useMemo(
260 () =>
261 new Map(
apps/mobile/src/features/prompt-links/usePromptLinkResolver.ts
+69 -17
@@ -2,7 +2,7 @@ import * as Dialog from "../../components/ui/Dialog";
2 import { StackActions, useNavigation } from "@react-navigation/native";
3 import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
4 import {
5 - findPromptLinkProject,
5 + findPromptLinkProjects,
6 findPromptLinkPullRequestThread,
7 resolvePromptLinkModelSelection,
8 } from "@t3tools/client-runtime/prompt-links";
@@ -11,7 +11,7 @@ import {
11 squashAtomCommandFailure,
12 } from "@t3tools/client-runtime/state/runtime";
13 import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
14 -import type { EnvironmentId } from "@t3tools/contracts";
14 +import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
15 import {
16 parseNewThreadLink,
17 type LinkedPrompt,
@@ -63,6 +63,9 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
63 const loadPullRequest = useAtomQueryRunner(composerPullRequests.detail, {
64 reportFailure: false,
65 });
66 + const loadLinkedThreads = useAtomQueryRunner(composerPullRequests.linkedThreadsUncached, {
67 + reportFailure: false,
68 + });
69 const search = promptLinkSearchFromParams(params);
70 // Wait for cold-launch shells or existing repositories may be offered for cloning.
71 const waitingForShells =
@@ -89,6 +92,61 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
92 return resolved?.exact !== true;
93 };
94
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 +
150 const openProjectDraft = async (project: EnvironmentProject, request: LinkedDraftRequest) => {
151 try {
152 await waitForComposerDraftsLoaded();
@@ -234,19 +292,7 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
292 if (link.kind === "pull-request") {
293 const thread = findPromptLinkPullRequestThread(threads, link.pullRequest);
294 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);
296 return;
297 }
298 }
@@ -263,14 +309,20 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
309 }
310 const activityOf = (project: EnvironmentProject) =>
311 lastActivity.get(`${project.environmentId}:${project.id}`) ?? "";
266 - const project = findPromptLinkProject(
312 + const projects = findPromptLinkProjects(
313 flow.projectScopes
314 .flatMap((scope) => scope.projects)
315 .sort((left, right) => activityOf(right).localeCompare(activityOf(left))),
316 repository,
317 );
318 + const project = projects[0];
319 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 + })();
326 return;
327 }
328 Dialog.alert(`You don't have ${promptLinkRepositoryName(repository)}`, undefined, [
apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx
+2
@@ -42,6 +42,7 @@ import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-en
42 import { leaveSettingsPage } from "./settings-navigation";
43 import { workspaceHasColumns } from "../workspace/workspace-selection-store";
44 import { DEFAULT_SETTINGS_PAGE } from "../workspace/workspace-tab-navigation";
45 +import { readUnloadedThreadCount } from "../../state/olderSettledShelf";
46
47 export function SettingsProjectOverviewRouteScreen() {
48 const insets = useSafeAreaInsets();
@@ -175,6 +176,7 @@ function ProjectOverviewContent(props: {
176 hasOtherMembers: props.hasOtherMembers,
177 displayName,
178 threads,
179 + unloadedThreadCount: readUnloadedThreadCount(members, threads),
180 });
181 Dialog.confirm({
182 title: plan.targetKind === "checkout" ? "Remove checkout" : "Remove project",
apps/mobile/src/features/threads/ThreadArrangementSheet.tsx
+53 -5
@@ -16,6 +16,8 @@ import { SymbolView } from "../../components/ui/AppSymbol";
16 import { scopedThreadKey } from "../../lib/scopedEntities";
17 import { environmentServerConfigsAtom } from "../../state/server";
18 import { environmentThreadShells } from "../../state/threads";
19 +import { sortSettledThreads } from "@t3tools/client-runtime/state/thread-sort";
20 +import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
21 import {
22 pendingThreadOrderAtom,
23 threadDropBusyAtom,
@@ -28,7 +30,12 @@ import {
30 threadDragAction,
31 type ThreadMoveDestination,
32 } 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";
39 import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences";
40
41 const ROW_HEIGHT = 56;
@@ -40,6 +47,8 @@ type Row = {
47 key: string;
48 section: Section;
49 thread?: EnvironmentThreadShell;
50 + /** The Settled section's "Show more" row. */
51 + more?: true;
52 offset: number;
53 height: number;
54 };
@@ -158,6 +167,15 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
167 const { workingShelfEnabled } = useThreadListV2ShelfPreferences();
168 const [now, setNow] = useState(() => new Date().toISOString());
169 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 + });
179 useEffect(() => {
180 const wakeAt = Math.min(
181 ...threads.flatMap((thread) => {
@@ -201,9 +219,13 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
219 ? sortInboxThreadsByReturn(active, threadListInboxReturns.returnedAt)
220 : active,
221 snoozed: parked.filter((thread) => effectiveSnoozed(thread, { now })),
204 - settled: parked.filter((thread) => !effectiveSnoozed(thread, { now })),
222 + settled: sortSettledThreads(parked.filter((thread) => !effectiveSnoozed(thread, { now }))),
223 };
224 }, [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);
229 const planners = useMemo(() => {
230 const planner = (section: "pinned" | "active") =>
231 createThreadMovePlanner({
@@ -232,13 +254,17 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
254 result.push({ key: section, section, offset, height: HEADER_HEIGHT });
255 offset += HEADER_HEIGHT;
256 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]) {
258 result.push({ key: keyOf(thread), section, thread, offset, height: ROW_HEIGHT });
259 offset += ROW_HEIGHT;
260 }
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 + }
265 }
266 return result;
241 - }, [sections, expanded]);
267 + }, [sections, expanded, visibleSettled, settledCount]);
268 const list = useRef<FlatList<Row>>(null);
269 const geometry = useRef({ height: 0, offset: 0 });
270 const drag = useRef<Drag | null>(null);
@@ -507,6 +533,25 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
533 }}
534 />
535 </>
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>
555 ) : (
556 <Pressable
557 disabled={item.section === "pinned" || item.section === "active"}
@@ -519,7 +564,10 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
564 >
565 <Text className="text-sm font-t3-semibold text-foreground-muted">
566 {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 + )
571 </Text>
572 </Pressable>
573 )}
apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
+28 -7
@@ -83,7 +83,12 @@ import { NativeScrollView } from "../../native/NativeScrollViewMarker";
83
84 type SidebarListItem =
85 | ThreadListV2ListItem
86 - | { readonly type: "v2-show-more"; readonly key: string; readonly hiddenCount: number };
86 + | {
87 + readonly type: "v2-show-more";
88 + readonly key: string;
89 + readonly hiddenCount: number;
90 + readonly state?: "loading" | "failed";
91 + };
92
93 const SIDEBAR_STICKY_HEADER_HEIGHT = 106;
94
@@ -187,10 +192,6 @@ function ThreadNavigationSidebarPane(
192 )
193 ? [options.selectedEnvironmentId]
194 : [];
190 - const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
191 - // This map reaches LegendList's extraData, which re-renders every row whenever it changes
192 - // identity; the hook keeps it stable.
193 - const threadSearchMatchByKey = useThreadListSearchMatches(threads, threadSearch);
195 const projectScopeKey = options.selectedProjectKey;
196 const resolvedProjectKey = resolveScratchProjectScopeKey(projectScopeKey, projects, {
197 sidebarProjectGroupingMode: options.projectGroupingMode,
@@ -231,6 +232,16 @@ function ThreadNavigationSidebarPane(
232 );
233 const selectedProjectScope =
234 projectScopes.find((scope) => scope.key === resolvedProjectKey) ?? null;
235 + const threadSearch = useThreadSearch(
236 + searchEnvironmentIds,
237 + props.searchQuery,
238 + selectedProjectScope === null
239 + ? {}
240 + : { projectIds: selectedProjectScope.projectRefs.map((ref) => ref.projectId) },
241 + );
242 + // This map reaches LegendList's extraData, which re-renders every row whenever it changes
243 + // identity; the hook keeps it stable.
244 + const threadSearchMatchByKey = useThreadListSearchMatches(threads, threadSearch);
245 const selectedProjectKey = selectedProjectScope?.key ?? null;
246 const projectByKey = useMemo(() => {
247 const map = new Map<string, EnvironmentProject>();
@@ -288,6 +299,7 @@ function ThreadNavigationSidebarPane(
299 layout: threadListV2Layout,
300 items: threadListV2Items,
301 contentOnlyThreadKeys,
302 + olderSettledShelf,
303 } = useThreadListV2({
304 threads,
305 pendingTasks,
@@ -331,6 +343,11 @@ function ThreadNavigationSidebarPane(
343 type: "v2-show-more",
344 key: "v2-show-more",
345 hiddenCount: threadListV2Layout.hiddenSettledCount,
346 + ...(olderSettledShelf.loading
347 + ? { state: "loading" as const }
348 + : olderSettledShelf.failed
349 + ? { state: "failed" as const }
350 + : {}),
351 },
352 ]
353 : threadListV2Items;
@@ -477,7 +494,7 @@ function ThreadNavigationSidebarPane(
494 return threadListV2ListItemsAreEqual(previous, item);
495 }
496 if (previous.type === "v2-show-more" && item.type === "v2-show-more") {
480 - return previous.hiddenCount === item.hiddenCount;
497 + return previous.hiddenCount === item.hiddenCount && previous.state === item.state;
498 }
499 return false;
500 };
@@ -648,7 +665,11 @@ function ThreadNavigationSidebarPane(
665 <ThreadListV2ShowMoreRow
666 pane="sidebar"
667 hiddenCount={item.hiddenCount}
651 - onPress={showMoreSettled}
668 + {...(item.state === undefined ? {} : { state: item.state })}
669 + onPress={() => {
670 + olderSettledShelf.retry();
671 + showMoreSettled();
672 + }}
673 />
674 );
675 }
apps/mobile/src/features/threads/new-task-flow-provider.tsx
+8 -5
@@ -124,6 +124,7 @@ import {
124 } from "./new-task-context-presentation";
125 import { resolveEnvironmentProjectMatch } from "./new-task-project-selection";
126 import { resolveProjectThreadCreationBranch } from "./projectThreadCreationValidation";
127 +import { useOlderSettledCount } from "../../state/olderSettledShelf";
128
129 type WorkspaceMode = "local" | "worktree";
130
@@ -511,13 +512,15 @@ export function NewTaskFlowProvider(
512 t3ProjectFile,
513 );
514 const canChooseWorkspace = !isScratchDraft;
515 + const selectedProjectOlderSettledCount = useOlderSettledCount(selectedProject);
516 const selectedProjectHasThreads =
517 selectedProject !== null &&
516 - threads.some(
517 - (thread) =>
518 - thread.environmentId === selectedProject.environmentId &&
519 - thread.projectId === selectedProject.id,
520 - );
518 + (selectedProjectOlderSettledCount > 0 ||
519 + threads.some(
520 + (thread) =>
521 + thread.environmentId === selectedProject.environmentId &&
522 + thread.projectId === selectedProject.id,
523 + ));
524 const defaultWorkspaceMode: WorkspaceMode = canChooseWorkspace
525 ? resolveNewThreadEnvMode({
526 projectSettings,
apps/mobile/src/features/threads/thread-list-v2-items.tsx
+15 -2
@@ -287,12 +287,25 @@ export const ThreadListV2SettledShelfHeader = memo(function ThreadListV2SettledS
287 export const ThreadListV2ShowMoreRow = memo(function ThreadListV2ShowMoreRow(props: {
288 readonly pane?: "screen" | "sidebar";
289 readonly hiddenCount: number;
290 + /** Older settled threads are loading, or their last read failed. */
291 + readonly state?: "loading" | "failed";
292 readonly onPress: () => void;
293 }) {
294 + const label =
295 + props.state === "loading"
296 + ? "Loading"
297 + : props.state === "failed"
298 + ? "Could not load older threads. Retry"
299 + : `Show more (${props.hiddenCount} settled hidden)`;
300 return (
301 <Pressable
302 accessibilityRole="button"
295 - accessibilityLabel={`Show ${Math.min(props.hiddenCount, THREAD_LIST_V2_SETTLED_PAGE_COUNT)} more settled threads`}
303 + accessibilityLabel={
304 + props.state === undefined
305 + ? `Show ${Math.min(props.hiddenCount, THREAD_LIST_V2_SETTLED_PAGE_COUNT)} more settled threads`
306 + : label
307 + }
308 + disabled={props.state === "loading"}
309 onPress={props.onPress}
310 className="mx-4 mt-2 items-center rounded-lg border border-dashed border-border py-2.5"
311 style={({ pressed }) => ({ opacity: pressed ? 0.6 : 1 })}
@@ -304,7 +317,7 @@ export const ThreadListV2ShowMoreRow = memo(function ThreadListV2ShowMoreRow(pro
317 : "text-xs font-t3-medium text-foreground-muted"
318 }
319 >
307 - Show more ({props.hiddenCount} settled hidden)
320 + {label}
321 </Text>
322 </Pressable>
323 );
apps/mobile/src/features/threads/threadListV2.test.ts
+41
@@ -549,6 +549,47 @@ describe("buildThreadListV2Items settled paging", () => {
549 "settled-2",
550 ]);
551 });
552 +
553 + it("lists only loaded settled rows and counts the threads the shell left out", () => {
554 + const settled = (index: number) =>
555 + makeThread({
556 + id: ThreadId.make(`settled-${index}`),
557 + title: `Settled ${index}`,
558 + settledOverride: "settled",
559 + settledAt: `2026-06-0${index}T00:00:00.000Z`,
560 + });
561 + const threads = [settled(1), settled(2), settled(3)];
562 + const layout = buildThreadListV2Items({
563 + threads,
564 + environmentId: null,
565 + searchQuery: "",
566 + settledLimit: 10,
567 + now: NOW,
568 + // settled-1 came back mid-session, below the loaded range; the route keeps it visible.
569 + isListedSettled: (thread) => thread.id !== "settled-1",
570 + unloadedSettledCount: 40,
571 + selectedThreadKey: `${threads[0]!.environmentId}:settled-1`,
572 + });
573 + expect(layout.items.map((item) => item.thread.id)).toEqual([
574 + "settled-3",
575 + "settled-2",
576 + "settled-1",
577 + ]);
578 + expect(layout.settledCount).toBe(43);
579 + expect(layout.hiddenSettledCount).toBe(40);
580 +
581 + // A scope whose settled threads are all older still shows its Settled header.
582 + const unloadedOnly = buildThreadListV2Items({
583 + threads: [],
584 + environmentId: null,
585 + searchQuery: "",
586 + settledLimit: 10,
587 + now: NOW,
588 + unloadedSettledCount: 5,
589 + });
590 + expect(unloadedOnly.settledCount).toBe(5);
591 + expect(unloadedOnly.settledShelfHeaderIndex).toBe(0);
592 + });
593 });
594
595 describe("buildThreadListV2Items settled sort reuse", () => {
apps/mobile/src/features/threads/threadListV2.ts
+22 -8
@@ -277,6 +277,8 @@ export interface ThreadListV2Item {
277 export interface ThreadListV2Layout {
278 readonly items: ThreadListV2Item[];
279 readonly hiddenSettledCount: number;
280 + /** The settled rows the shelf may list, before its visible limit. */
281 + readonly listedSettledThreads: ReadonlyArray<EnvironmentThreadShell>;
282 readonly workingCount: number;
283 readonly workingShelfHeaderIndex: number | null;
284 readonly snoozedCount: number;
@@ -583,6 +585,10 @@ export function buildThreadListV2Items(input: {
585 readonly settledShelfExpanded?: boolean;
586 readonly selectedThreadKey?: string | null;
587 readonly queuedThreadKeys?: ReadonlySet<string>;
588 + /** Whether the shelf may list a settled row yet; older settled rows load in pages. */
589 + readonly isListedSettled?: (thread: EnvironmentThreadShell) => boolean;
590 + /** Settled threads in scope that are not local (older settled threads not loaded). */
591 + readonly unloadedSettledCount?: number;
592 }): ThreadListV2Layout {
593 const now = input.now;
594 const pending =
@@ -695,6 +701,7 @@ export function buildThreadListV2Items(input: {
701 isLast: index === results.length - 1,
702 })),
703 hiddenSettledCount: 0,
704 + listedSettledThreads: [],
705 workingCount: 0,
706 workingShelfHeaderIndex: null,
707 snoozedCount: 0,
@@ -728,15 +735,21 @@ export function buildThreadListV2Items(input: {
735 (thread) => `${thread.environmentId}:${thread.id}` === selectedThreadKey,
736 );
737 const orderedSettled = sortSettledThreadsReusingLast(settled);
738 + const listedSettled =
739 + input.isListedSettled === undefined
740 + ? orderedSettled
741 + : orderedSettled.filter(input.isListedSettled);
742 + const settledTotal = orderedSettled.length + (input.unloadedSettledCount ?? 0);
743 const settledLimit = input.settledLimit ?? Number.POSITIVE_INFINITY;
744 const limitedSettled =
733 - orderedSettled.length > settledLimit ? orderedSettled.slice(0, settledLimit) : orderedSettled;
745 + listedSettled.length > settledLimit ? listedSettled.slice(0, settledLimit) : listedSettled;
746 const selectedSettled =
735 - selectedThreadKey === null
747 + selectedThreadKey === null ||
748 + limitedSettled.some((thread) => `${thread.environmentId}:${thread.id}` === selectedThreadKey)
749 ? undefined
737 - : orderedSettled
738 - .slice(limitedSettled.length)
739 - .find((thread) => `${thread.environmentId}:${thread.id}` === selectedThreadKey);
750 + : orderedSettled.find(
751 + (thread) => `${thread.environmentId}:${thread.id}` === selectedThreadKey,
752 + );
753 const pagedSettled =
754 selectedSettled === undefined ? limitedSettled : [...limitedSettled, selectedSettled];
755 const visibleSettled =
@@ -793,7 +806,7 @@ export function buildThreadListV2Items(input: {
806 isLast: false,
807 });
808 }
796 - const settledShelfHeaderIndex = orderedSettled.length > 0 ? items.length : null;
809 + const settledShelfHeaderIndex = settledTotal > 0 ? items.length : null;
810 for (const thread of visibleSettled) {
811 items.push({
812 thread,
@@ -810,12 +823,13 @@ export function buildThreadListV2Items(input: {
823 }
824 return {
825 items,
813 - hiddenSettledCount: orderedSettled.length - pagedSettled.length,
826 + hiddenSettledCount: Math.max(0, settledTotal - pagedSettled.length),
827 + listedSettledThreads: listedSettled,
828 workingCount: orderedWorking.length,
829 workingShelfHeaderIndex,
830 snoozedCount: orderedSnoozed.length,
831 snoozedShelfHeaderIndex,
818 - settledCount: orderedSettled.length,
832 + settledCount: settledTotal,
833 settledShelfHeaderIndex,
834 nextSnoozeWakeAt,
835 contentOnlyThreadKeys,
apps/mobile/src/features/threads/use-composer-command-menu.ts
+11 -1
@@ -50,7 +50,11 @@ import { useEffect, useRef, useState } from "react";
50 import type { ComposerEditorSelection } from "../../components/ComposerEditor";
51 import { serverEnvironment } from "../../state/server";
52 import { useAtomCommand } from "../../state/use-atom-command";
53 -import { useComposerPathSearch, useComposerPullRequestSearch } from "../../state/queries";
53 +import {
54 + useComposerPathSearch,
55 + useComposerPullRequestSearch,
56 + useThreadSearch,
57 +} from "../../state/queries";
58 import type { ComposerCommandItem } from "./ComposerCommandPopover";
59 import { matchesSlashSkillQuery } from "./composerSlashSkillSearch";
60
@@ -344,6 +348,12 @@ export function useComposerCommandMenu({
348 cwd: trigger?.kind === "path" ? projectCwd : null,
349 query: trigger?.kind === "path" ? trigger.query : null,
350 });
351 + // Mounts older settled threads matching the mention, which shells may leave out.
352 + useThreadSearch(
353 + trigger?.kind === "path" && environmentId ? [environmentId] : [],
354 + trigger?.kind === "path" ? trigger.query : "",
355 + { titlesOnly: true },
356 + );
357 const pullRequestSearch = useComposerPullRequestSearch({
358 environmentId,
359 projectId: pullRequestProjectId,
apps/mobile/src/features/threads/useThreadListV2.ts
+41 -1
@@ -1,7 +1,9 @@
1 +import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads";
2 import type { EnvironmentId } from "@t3tools/contracts";
3 import { useMemo, useSyncExternalStore } from "react";
4
5 import { scopedProjectKey } from "../../lib/scopedEntities";
6 +import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
7 import type { PendingNewTask } from "../../state/use-pending-new-tasks";
8 import {
9 buildThreadListV2Items,
@@ -88,6 +90,14 @@ export function useThreadListV2(input: {
90 ...sectionAvailability("pinned"),
91 ...(workingShelfEnabled ? [] : sectionAvailability("active")),
92 ]);
93 + // Older settled threads load in pages; the shelf lists them only as far as they are loaded.
94 + const scopes = olderSettledScopes(input);
95 + const olderSettledShelf = useOlderSettledShelf({
96 + scopes,
97 + expanded: input.settledShelfExpanded,
98 + // A search lists only matches, so the shelf would page forever; loaded pages stay.
99 + visibleCount: input.searchQuery.trim().length === 0 ? input.settledLimit : 0,
100 + });
101 const layout = buildThreadListV2Items({
102 pendingOrder: input.pendingOrder,
103 threads: threads.filter((thread) => thread.archivedAt === null),
@@ -107,7 +117,20 @@ export function useThreadListV2(input: {
117 snoozedShelfExpanded: input.snoozedShelfExpanded,
118 settledShelfExpanded: input.settledShelfExpanded,
119 selectedThreadKey: input.selectedThreadKey,
120 + isListedSettled: olderSettledShelf.isListed,
121 + unloadedSettledCount: olderSettledShelf.unloadedCount(
122 + threads.filter(
123 + (thread) =>
124 + thread.archivedAt === null &&
125 + scopes.some(
126 + (scope) =>
127 + scope.environmentId === thread.environmentId &&
128 + (scope.projectIds === null || scope.projectIds.includes(thread.projectId)),
129 + ),
130 + ),
131 + ),
132 });
133 + useOlderSettledShelfGrowth(olderSettledShelf, layout.listedSettledThreads);
134 const contentOnlyThreadKeys = useKeySet([...layout.contentOnlyThreadKeys].sort().join("\n"));
135 const scopedProjectKeys =
136 input.projectRefs == null
@@ -141,5 +164,22 @@ export function useThreadListV2(input: {
164 moveAvailability,
165 shelfPreferencesLoading: !input.shelfPreferencesLoaded,
166 });
144 - return { layout, items, contentOnlyThreadKeys };
167 + return { layout, items, contentOnlyThreadKeys, olderSettledShelf };
168 +}
169 +
170 +/** The list's scope per environment: its projects there, or every project. */
171 +function olderSettledScopes(input: {
172 + readonly environmentId: EnvironmentId | null;
173 + readonly projectRefs: LayoutInput["projectRefs"];
174 + readonly settlementEnvironmentIds: ReadonlySet<EnvironmentId>;
175 +}): ReadonlyArray<OlderSettledScope> {
176 + const environmentIds =
177 + input.environmentId === null ? [...input.settlementEnvironmentIds] : [input.environmentId];
178 + return environmentIds.flatMap((environmentId): ReadonlyArray<OlderSettledScope> => {
179 + if (input.projectRefs == null) return [{ environmentId, projectIds: null }];
180 + const projectIds = input.projectRefs.flatMap((ref) =>
181 + ref.environmentId === environmentId ? [ref.projectId] : [],
182 + );
183 + return projectIds.length === 0 ? [] : [{ environmentId, projectIds }];
184 + });
185 }
apps/mobile/src/state/olderSettledShelf.ts new
+135
@@ -0,0 +1,135 @@
1 +import { useAtomValue } from "@effect/atom-react";
2 +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
3 +import {
4 + isListedSettledThread,
5 + settledShelfFrontier,
6 + unloadedOlderSettledCount,
7 + wantedOlderSettledPages,
8 + type OlderSettled,
9 + type OlderSettledScope,
10 +} from "@t3tools/client-runtime/state/threads";
11 +import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
12 +import { Atom } from "effect/reactivity";
13 +import { useEffect, useState } from "react";
14 +
15 +import { appAtomRegistry } from "./atom-registry";
16 +import { olderSettledThreads, threadFamilies } from "./threads";
17 +
18 +const NO_PAGES: ReadonlyMap<EnvironmentId, number> = new Map();
19 +const NO_OLDER_SETTLED: Atom.Atom<OlderSettled | null> = Atom.make(null);
20 +
21 +/**
22 + * The Settled shelf for one scope when shells leave older settled threads out: which rows it may
23 + * list, how many threads it holds in all, and the pages read below each environment's cut, whose
24 + * rows merge into the thread list. `useOlderSettledShelfGrowth` reads the next pages.
25 + */
26 +export function useOlderSettledShelf(input: {
27 + readonly scopes: ReadonlyArray<OlderSettledScope>;
28 + /** A collapsed shelf mounts no pages, so a reconnect reads none; expanding restores them. */
29 + readonly expanded: boolean;
30 + /** Rows the shelf shows; pages are read until it lists that many. */
31 + readonly visibleCount: number;
32 +}) {
33 + const scopeKey = JSON.stringify(input.scopes);
34 + const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });
35 + const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;
36 + const shelfScopes = input.scopes.map((scope) => ({
37 + scope,
38 + pages: pages.get(scope.environmentId) ?? 0,
39 + }));
40 + const shelfKey = JSON.stringify(shelfScopes);
41 + const shelf = useAtomValue(olderSettledThreads.shelfAtom(shelfScopes));
42 + useEffect(() => {
43 + const releases = (JSON.parse(shelfKey) as typeof shelfScopes).flatMap(({ scope, pages }) =>
44 + pages === 0
45 + ? []
46 + : [
47 + threadFamilies.mountRows(
48 + appAtomRegistry,
49 + scope.environmentId,
50 + olderSettledThreads.rowsKey(scope, pages),
51 + olderSettledThreads.pageRowsAtom(scope, pages),
52 + ),
53 + ],
54 + );
55 + return () => {
56 + for (const release of releases) release();
57 + };
58 + }, [shelfKey]);
59 +
60 + const olderOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.older ?? null;
61 + const pagesOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.pages;
62 + const frontier = settledShelfFrontier([...shelf.values()]);
63 + const loaded = shelfScopes.filter(({ pages }) => pages > 0);
64 + return {
65 + environments: shelf,
66 + visibleCount: input.expanded ? input.visibleCount : 0,
67 + pages,
68 + grow: (next: ReadonlyMap<EnvironmentId, number>) => setRequested({ scopeKey, pages: next }),
69 + isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
70 + /** Older settled threads in scope that are not local; add it to the local Settled rows. */
71 + unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
72 + unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),
73 + loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
74 + failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
75 + retry: () => {
76 + for (const { scope, pages } of loaded) {
77 + olderSettledThreads.retryPages(appAtomRegistry, scope, pages);
78 + }
79 + },
80 + };
81 +}
82 +
83 +/** Reads the next pages while the shelf lists fewer rows than it shows. */
84 +export function useOlderSettledShelfGrowth(
85 + shelf: ReturnType<typeof useOlderSettledShelf>,
86 + listedSettledThreads: ReadonlyArray<EnvironmentThreadShell>,
87 +) {
88 + const wanted = wantedOlderSettledPages({
89 + requested: shelf.pages,
90 + environments: shelf.environments,
91 + listedCount: listedSettledThreads.length,
92 + visibleCount: shelf.visibleCount,
93 + });
94 + const { grow, pages } = shelf;
95 + useEffect(() => {
96 + if (wanted !== pages) grow(wanted);
97 + }, [wanted, pages, grow]);
98 +}
99 +
100 +/**
101 + * Threads of these projects that this client does not hold (older settled threads), for counts
102 + * shown outside the shelf, such as a removal confirm.
103 + */
104 +export function readUnloadedThreadCount(
105 + members: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ProjectId }>,
106 + threads: ReadonlyArray<EnvironmentThreadShell>,
107 +): number {
108 + return unloadedOlderSettledCount({
109 + olderSettled: (environmentId) =>
110 + appAtomRegistry.get(olderSettledThreads.olderSettledAtom(environmentId)),
111 + scopes: [...new Set(members.map((member) => member.environmentId))].map((environmentId) => ({
112 + environmentId,
113 + projectIds: members.flatMap((member) =>
114 + member.environmentId === environmentId ? [member.id] : [],
115 + ),
116 + })),
117 + threads: threads.filter((thread) =>
118 + members.some(
119 + (member) => member.environmentId === thread.environmentId && member.id === thread.projectId,
120 + ),
121 + ),
122 + });
123 +}
124 +
125 +/** How many older settled threads one project has, loaded or not, kept current for render. */
126 +export function useOlderSettledCount(
127 + project: { readonly environmentId: EnvironmentId; readonly id: ProjectId } | null,
128 +): number {
129 + const older = useAtomValue(
130 + project === null
131 + ? NO_OLDER_SETTLED
132 + : olderSettledThreads.olderSettledAtom(project.environmentId),
133 + );
134 + return project === null ? 0 : (older?.counts.get(project.id) ?? 0);
135 +}
apps/mobile/src/state/queries.ts
+58 -13
@@ -20,6 +20,8 @@ import {
20 visibleThreadSearch,
21 type EnvironmentThreadSearchMatch,
22 type SettledThreadSearch,
23 + type ThreadSearchResultsState,
24 + type ThreadSearchScope,
25 } from "@t3tools/client-runtime/state/thread-search";
26 import { THREAD_SEARCH_QUERY_MAX_LENGTH } from "@t3tools/contracts";
27 import { messageSearchTokens } from "@t3tools/shared/paletteSearch";
@@ -32,6 +34,7 @@ import { useEffect, useState } from "react";
34
35 import { appAtomRegistry } from "./atom-registry";
36 import { orchestrationEnvironment } from "./orchestration";
37 +import { olderSettledThreads, threadFamilies } from "./threads";
38 import { projectEnvironment } from "./projects";
39 import { useEnvironmentQuery } from "./query";
40 import { vcsEnvironment } from "./vcs";
@@ -49,17 +52,31 @@ const VCS_REF_LIST_LIMIT = 100;
52 const EMPTY_REFS: ReadonlyArray<VcsRef> = [];
53 const INITIAL_BRANCH_CURSORS = [undefined] as const;
54 const EMPTY_THREAD_SEARCH_MATCHES: ReadonlyArray<EnvironmentThreadSearchMatch> = Object.freeze([]);
52 -const EMPTY_THREAD_SEARCH_ATOM = Atom.make({
55 +const EMPTY_THREAD_SEARCH_ATOM = Atom.make<ThreadSearchResultsState>({
56 matches: EMPTY_THREAD_SEARCH_MATCHES,
57 isLoading: false,
58 + rows: [],
59 }).pipe(Atom.withLabel("mobile:thread-search:empty"));
60
57 -const threadSearchResultsAtom = createThreadSearchResultsAtomFamily({
58 - getSearchAtom: (environmentId, query) =>
59 - orchestrationEnvironment.threadSearch({
60 - environmentId,
61 - input: { query },
62 - }),
61 +const olderSettledBeforeAtom = Atom.family((environmentId: EnvironmentId) =>
62 + Atom.make((get) => get(olderSettledThreads.olderSettledAtom(environmentId))?.before ?? null).pipe(
63 + Atom.withLabel(`mobile:older-settled-before:${environmentId}`),
64 + ),
65 +);
66 +// Whether any of these environments leaves older settled threads out, so short queries matter.
67 +const anyOlderSettledAtom = Atom.family((environmentIdsKey: string) =>
68 + Atom.make((get) =>
69 + (JSON.parse(environmentIdsKey) as ReadonlyArray<EnvironmentId>).some(
70 + (environmentId) => get(olderSettledBeforeAtom(environmentId)) !== null,
71 + ),
72 + ).pipe(Atom.withLabel(`mobile:any-older-settled:${environmentIdsKey}`)),
73 +);
74 +const threadSearch = createThreadSearchResultsAtomFamily({
75 + getSearchAtom: (environmentId, input) =>
76 + input.olderSettledBefore === undefined
77 + ? orchestrationEnvironment.threadSearch({ environmentId, input })
78 + : olderSettledThreads.searchAtom(environmentId, input),
79 + olderSettledBefore: olderSettledBeforeAtom,
80 labelPrefix: "mobile:thread-search",
81 });
82
@@ -163,9 +180,15 @@ export function useComposerPullRequestSearch(input: {
180 };
181 }
182
183 +/**
184 + * Server matches for a thread search: message hits, and on environments that leave older settled
185 + * threads out of the shell, the rows of the best older matches, mounted into the thread list while
186 + * they show so the caller's own ranking finds them.
187 + */
188 export function useThreadSearch(
189 environmentIds: ReadonlyArray<EnvironmentId>,
190 query: string,
191 + scope: ThreadSearchScope = {},
192 ): {
193 readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
194 /** The query `matches` are for, which lags the typed one while a search runs. */
@@ -175,20 +198,25 @@ export function useThreadSearch(
198 // A pasted wall of text searches by its start rather than failing the request.
199 const normalizedQuery = query.trim().slice(0, THREAD_SEARCH_QUERY_MAX_LENGTH).trim();
200 const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
178 - // The server needs a word of three or more letters; shorter queries would always come back empty.
201 + const anyOlderSettled = useAtomValue(anyOlderSettledAtom(JSON.stringify(environmentIds)));
202 + // Messages need a word of three or more letters; older settled titles match from two.
203 const canSearch =
180 - environmentIds.length > 0 && messageSearchTokens(normalizedQuery).long.length > 0;
204 + environmentIds.length > 0 &&
205 + ((scope.titlesOnly !== true && messageSearchTokens(normalizedQuery).long.length > 0) ||
206 + (anyOlderSettled && normalizedQuery.length >= 2));
207 const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
182 - const searchKey =
183 - settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery);
208 + const searchKeyOf = (searchQuery: string) =>
209 + makeThreadSearchKey(environmentIds, searchQuery, scope);
210 + const searchKey = settledQuery === null ? null : searchKeyOf(settledQuery);
211 const result = useAtomValue(
185 - searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
212 + searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearch.results(searchKey),
213 );
214 const isPending = canSearch && (normalizedQuery !== debouncedQuery || result.isLoading);
215 // What fast environments returned shows without waiting for a slow one;
216 // callers keep shown rows in place with `nextShownOrder`.
217 const ready =
191 - settledQuery !== null && (!result.isLoading || result.matches.length > 0)
218 + settledQuery !== null &&
219 + (!result.isLoading || result.matches.length > 0 || result.rows.length > 0)
220 ? { query: settledQuery, matches: result.matches }
221 : null;
222 // Kept from earlier renders: React's pattern for information from previous renders.
@@ -197,6 +225,23 @@ export function useThreadSearch(
225 const visible = canSearch
226 ? visibleThreadSearch(normalizedQuery, ready, previous)
227 : { query: "", matches: EMPTY_THREAD_SEARCH_MATCHES };
228 + // The shown results' rows stay mounted until newer results replace them.
229 + const shownKey = visible.query === "" ? null : searchKeyOf(visible.query);
230 + useEffect(() => {
231 + if (shownKey === null) return;
232 + const environments = (JSON.parse(shownKey) as [ReadonlyArray<EnvironmentId>])[0];
233 + const releases = environments.map((environmentId) =>
234 + threadFamilies.mountRows(
235 + appAtomRegistry,
236 + environmentId,
237 + `search:${shownKey}`,
238 + threadSearch.rows(shownKey, environmentId),
239 + ),
240 + );
241 + return () => {
242 + for (const release of releases) release();
243 + };
244 + }, [shownKey]);
245 return {
246 matches: visible.matches,
247 query: visible.query,
apps/mobile/src/state/threads.ts
+5
@@ -5,6 +5,7 @@ import {
5 createEnvironmentThreadStateAtoms,
6 EMPTY_ENVIRONMENT_THREAD_STATE,
7 type EnvironmentThreadState,
8 + createOlderSettledThreadAtoms,
9 createThreadEnvironmentAtoms,
10 createThreadFamilyAtoms,
11 } from "@t3tools/client-runtime/state/threads";
@@ -22,6 +23,10 @@ export const threadFamilies = createThreadFamilyAtoms(
23 connectionAtomRuntime,
24 environmentShell.stateAtom,
25 );
26 +export const olderSettledThreads = createOlderSettledThreadAtoms(
27 + connectionAtomRuntime,
28 + environmentShell.stateAtom,
29 +);
30 export const threadEnvironment = createThreadEnvironmentAtoms(
31 connectionAtomRuntime,
32 threadFamilies.snapshotAtom,
apps/server/src/auth/RpcAuthorization.ts
+1
@@ -50,6 +50,7 @@ export const RPC_REQUIRED_SCOPES = {
50 [ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot]: AuthOrchestrationReadScope,
51 [ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: AuthOrchestrationReadScope,
52 [ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds]: AuthOrchestrationReadScope,
53 + [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: AuthOrchestrationReadScope,
54 [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: AuthOrchestrationReadScope,
55 [ORCHESTRATION_V2_WS_METHODS.getTurnItem]: AuthOrchestrationReadScope,
56 [ORCHESTRATION_V2_WS_METHODS.launchThread]: AuthOrchestrationOperateScope,
apps/server/src/observability/RpcInstrumentation.ts
+1
@@ -23,6 +23,7 @@ const RPC_AGGREGATES = {
23 [ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot]: "orchestration",
24 [ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: "orchestration",
25 [ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds]: "orchestration",
26 + [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: "orchestration",
27 [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: "orchestrationV2",
28 [ORCHESTRATION_V2_WS_METHODS.launchThread]: "orchestration",
29 [ORCHESTRATION_V2_WS_METHODS.subscribeArchivedShell]: "orchestration",
apps/server/src/orchestration-v2/Orchestrator.ts
+14
@@ -133,6 +133,8 @@ import {
133 type ProjectionRecordFilter,
134 type ProjectionRecords,
135 type ProjectionCheckpointContext,
136 + type OlderSettledThreadsListing,
137 + type OlderSettledThreadsQuery,
138 type ShellSnapshotOptions,
139 } from "./ProjectionStore.ts";
140 import { ProviderAdapterRegistryV2 } from "./ProviderAdapterRegistry.ts";
@@ -388,6 +390,9 @@ export interface OrchestratorV2Shape {
390 ReadonlyArray<ThreadId>,
391 OrchestratorV2Error
392 >;
393 + readonly listOlderSettledThreads: (
394 + query: OlderSettledThreadsQuery,
395 + ) => Effect.Effect<OlderSettledThreadsListing, OrchestratorV2Error>;
396 readonly readShellSnapshot: (
397 options?: ShellSnapshotOptions,
398 ) => Effect.Effect<
@@ -12474,6 +12479,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12479 projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError)),
12480 listActiveSubagentThreadIds: () =>
12481 projectionStore.listActiveSubagentThreadIds().pipe(Effect.mapError(shellProjectionError)),
12482 + listOlderSettledThreads: (query) =>
12483 + projectionStore.listOlderSettledThreads(query).pipe(Effect.mapError(shellProjectionError)),
12484 readShellSnapshot: (options) =>
12485 projectionStore
12486 .readShellSnapshot(options)
@@ -12625,6 +12632,13 @@ const layerUnavailable: Layer.Layer<OrchestratorV2> = Layer.succeed(
12632 cause: "Orchestration V2 live runtime is not configured.",
12633 }),
12634 ),
12635 + listOlderSettledThreads: () =>
12636 + Effect.fail(
12637 + new OrchestratorProjectionError({
12638 + threadId: ThreadId.make("thread:shell"),
12639 + cause: "Orchestration V2 live runtime is not configured.",
12640 + }),
12641 + ),
12642 readShellSnapshot: () =>
12643 Effect.fail(
12644 new OrchestratorProjectionError({
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+101 -4
@@ -1166,11 +1166,12 @@ it.effect.each([
1166
1167 const family = (threadId: ThreadId) =>
1168 store
1169 - .getShellSnapshot({ location: "active", subagentFamilyOf: threadId })
1169 + .getShellSnapshot({ location: "active", familyOf: threadId })
1170 .pipe(Effect.map((snapshot) => snapshot.threads.map((thread) => thread.id)));
1171 - // Any member, or the root itself, names the same active subagent tree.
1172 - assert.sameMembers([...(yield* family(id("root")))], [id("child"), id("grandchild")]);
1173 - assert.sameMembers([...(yield* family(id("grandchild")))], [id("child"), id("grandchild")]);
1171 + // Any member, or the root itself, names the same active tree, root included.
1172 + const tree = [id("root"), id("child"), id("grandchild")];
1173 + assert.sameMembers([...(yield* family(id("root")))], tree);
1174 + assert.sameMembers([...(yield* family(id("grandchild")))], tree);
1175 assert.deepEqual([...(yield* family(id("missing")))], []);
1176
1177 // Includes another root's idle child on the same path, which a shell may leave out.
@@ -1191,6 +1192,102 @@ it.effect.each([
1192 }).pipe(Effect.provide(layer)),
1193 );
1194
1195 +it.effect.each([
1196 + ["memory", ProjectionStore.layerMemory],
1197 + ["SQL", layerTest],
1198 +] as const)("%s list older settled threads below a cursor", ([, layer]) =>
1199 + Effect.gen(function* () {
1200 + const store = yield* ProjectionStore.ProjectionStoreV2;
1201 + const now = yield* DateTime.now;
1202 + const id = (name: string) => ThreadId.make(`thread:older:${name}`);
1203 + const at = (day: number) => DateTime.makeUnsafe(Date.UTC(2026, 9, day));
1204 + const createThread = (input: {
1205 + readonly name: string;
1206 + readonly settledDay: number | null;
1207 + readonly project?: string;
1208 + readonly subagent?: boolean;
1209 + readonly archived?: boolean;
1210 + readonly snoozedUntilDay?: number;
1211 + }) =>
1212 + store.apply({
1213 + id: EventId.make(`event:older:${input.name}`),
1214 + type: "thread.created",
1215 + threadId: id(input.name),
1216 + occurredAt: now,
1217 + payload: {
1218 + createdBy: "user",
1219 + creationSource: "web",
1220 + id: id(input.name),
1221 + projectId: ProjectId.make(input.project ?? "project:a"),
1222 + title: input.name,
1223 + providerInstanceId,
1224 + modelSelection,
1225 + runtimeMode: "full-access",
1226 + interactionMode: "default",
1227 + branch: null,
1228 + worktreePath: null,
1229 + activeProviderThreadId: null,
1230 + lineage: {
1231 + parentThreadId: input.subagent === true ? id("b") : null,
1232 + relationshipToParent: input.subagent === true ? "subagent" : null,
1233 + rootThreadId: input.subagent === true ? id("b") : id(input.name),
1234 + },
1235 + forkedFrom: null,
1236 + createdAt: now,
1237 + updatedAt: now,
1238 + archivedAt: input.archived === true ? now : null,
1239 + settledOverride: input.settledDay === null ? null : "settled",
1240 + settledAt: input.settledDay === null ? null : at(input.settledDay),
1241 + snoozedUntil: input.snoozedUntilDay === undefined ? null : at(input.snoozedUntilDay),
1242 + lastVisitedAt: null,
1243 + deletedAt: null,
1244 + },
1245 + });
1246 + yield* createThread({ name: "a", settledDay: 5 });
1247 + yield* createThread({ name: "b", settledDay: 4 });
1248 + yield* createThread({ name: "c", settledDay: 4 });
1249 + yield* createThread({ name: "d", settledDay: 3, project: "project:b" });
1250 + yield* createThread({ name: "snoozed", settledDay: 2, snoozedUntilDay: 30 });
1251 + yield* createThread({ name: "recent", settledDay: 20 });
1252 + yield* createThread({ name: "unsettled", settledDay: null });
1253 + yield* createThread({ name: "child", settledDay: 1, subagent: true });
1254 + yield* createThread({ name: "archived", settledDay: 1, archived: true });
1255 +
1256 + const list = (
1257 + before: { readonly settledAt: string; readonly threadId?: ThreadId },
1258 + limit: number,
1259 + projectIds?: ReadonlyArray<ProjectId>,
1260 + ) => store.listOlderSettledThreads({ before, projectIds, limit });
1261 + const before = { settledAt: DateTime.formatIso(at(10)) };
1262 + const first = yield* list(before, 2);
1263 + assert.deepEqual(first, { threadIds: [id("a"), id("b")], counts: [] });
1264 + assert.sameDeepMembers(
1265 + [...(yield* list(before, 0)).counts],
1266 + [
1267 + { projectId: ProjectId.make("project:a"), count: 4 },
1268 + { projectId: ProjectId.make("project:b"), count: 1 },
1269 + ],
1270 + );
1271 + // A cursor without an id is strict, as the snapshot's cut is: the day-4 tie is not counted.
1272 + assert.sameDeepMembers(
1273 + [...(yield* list({ settledAt: DateTime.formatIso(at(4)) }, 0)).counts],
1274 + [
1275 + { projectId: ProjectId.make("project:a"), count: 1 },
1276 + { projectId: ProjectId.make("project:b"), count: 1 },
1277 + ],
1278 + );
1279 + // The cursor is the last row: the tie on day 4 continues by id.
1280 + const next = yield* list({ settledAt: DateTime.formatIso(at(4)), threadId: id("b") }, 10);
1281 + assert.deepEqual(next.threadIds, [id("c"), id("d"), id("snoozed")]);
1282 + assert.deepEqual(yield* list(before, 25, []), { threadIds: [], counts: [] });
1283 + const scoped = yield* list(before, 0, [ProjectId.make("project:b")]);
1284 + assert.deepEqual(scoped, {
1285 + threadIds: [],
1286 + counts: [{ projectId: ProjectId.make("project:b"), count: 1 }],
1287 + });
1288 + }).pipe(Effect.provide(layer)),
1289 +);
1290 +
1291 it.effect("lists only active subagent IDs without loading shell data", () =>
1292 Effect.gen(function* () {
1293 const store = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+133 -12
@@ -26,6 +26,8 @@ import type {
26 PlanId,
27 OrchestrationV2ConversationMessage,
28 OrchestrationV2DomainEvent,
29 + OrchestrationV2OlderSettledCount,
30 + OrchestrationV2OlderSettledCursor,
31 OrchestrationV2ProjectedTurnItem,
32 OrchestrationV2ProviderThread,
33 OrchestrationV2ProviderTurn,
@@ -37,7 +39,6 @@ import type {
39 OrchestrationV2ThreadProjection,
40 OrchestrationV2TurnItem,
41 GuestShareId,
40 - ProjectId,
42 ProviderSessionId,
43 ProviderThreadId,
44 ProviderTurnId,
@@ -71,6 +72,7 @@ import {
72 ThreadId,
73 TurnItemId,
74 NodeId,
75 + ProjectId,
76 ProviderInstanceId,
77 } from "@t3tools/contracts";
78 import {
@@ -410,11 +412,24 @@ export interface ShellSnapshotOptions {
412 readonly projectId?: ProjectId;
413 readonly unsettledOnly?: boolean;
414 /**
413 - * Only the subagent threads sharing this thread's `lineage.rootThreadId`. The thread may be
414 - * deleted: forks keep a deleted source's root, and their subagent trees still need it.
415 + * Only the threads sharing this thread's `lineage.rootThreadId`: the root, its forks, and their
416 + * subagent trees. The thread may be deleted: forks keep a deleted source's root.
417 */
416 - readonly subagentFamilyOf?: ThreadId;
418 + readonly familyOf?: ThreadId;
419 readonly worktreePath?: string;
420 + readonly threadIds?: ReadonlyArray<ThreadId>;
421 +}
422 +
423 +export interface OlderSettledThreadsQuery {
424 + readonly before: OrchestrationV2OlderSettledCursor;
425 + readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
426 + readonly limit: number;
427 +}
428 +
429 +export interface OlderSettledThreadsListing {
430 + readonly threadIds: ReadonlyArray<ThreadId>;
431 + /** Only for `limit: 0`. */
432 + readonly counts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
433 }
434
435 export interface ProjectionStoreV2Shape {
@@ -461,6 +476,10 @@ export interface ProjectionStoreV2Shape {
476 ReadonlyArray<ThreadId>,
477 ProjectionStoreV2Error
478 >;
479 + /** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
480 + readonly listOlderSettledThreads: (
481 + query: OlderSettledThreadsQuery,
482 + ) => Effect.Effect<OlderSettledThreadsListing, ProjectionStoreV2Error>;
483 /**
484 * Callers that combine the shell with other reads run this inside their own
485 * transaction and the returned decode after it commits, so the shared
@@ -5718,8 +5737,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5737 location,
5738 projectId,
5739 unsettledOnly = false,
5721 - subagentFamilyOf,
5740 + familyOf,
5741 worktreePath,
5742 + threadIds,
5743 }: ShellSnapshotOptions = {},
5744 ) =>
5745 sql<ShellThreadRow>`
@@ -5922,18 +5942,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5942 ? sql` AND json_extract(t.payload_json, '$.settledAt') IS NULL AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'`
5943 : sql``
5944 }${
5925 - subagentFamilyOf === undefined
5945 + familyOf === undefined
5946 ? sql``
5927 - : sql` AND json_extract(t.payload_json, '$.lineage.relationshipToParent') = 'subagent' AND json_extract(t.payload_json, '$.lineage.rootThreadId') = (
5947 + : sql` AND json_extract(t.payload_json, '$.lineage.rootThreadId') = (
5948 SELECT json_extract(member.payload_json, '$.lineage.rootThreadId')
5949 FROM orchestration_v2_projection_threads member
5930 - WHERE member.thread_id = ${subagentFamilyOf}
5950 + WHERE member.thread_id = ${familyOf}
5951 )`
5952 }${
5953 worktreePath === undefined
5954 ? sql``
5955 : sql` AND json_extract(t.payload_json, '$.worktreePath') = ${worktreePath}`
5936 - }
5956 + }${threadIds === undefined ? sql`` : sql` AND t.thread_id IN ${sql.in(threadIds)}`}
5957 ORDER BY t.updated_at ASC, t.thread_id ASC
5958 `;
5959
@@ -6577,6 +6597,55 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6597 return rows[0] === undefined ? null : ThreadId.make(rows[0].thread_id);
6598 }, Effect.mapError(shellSnapshotReadError));
6599
6600 + const listOlderSettledThreads: ProjectionStoreV2Shape["listOlderSettledThreads"] = Effect.fn(
6601 + "ProjectionStore.listOlderSettledThreads",
6602 + )(function* ({ before, projectIds, limit }) {
6603 + // ISO strings in one format, so string order is time order.
6604 + const older = sql`
6605 + SELECT t.thread_id, t.project_id,
6606 + json_extract(t.payload_json, '$.settledAt') AS settled_at
6607 + FROM orchestration_v2_projection_threads t
6608 + WHERE t.deleted_at IS NULL
6609 + AND json_extract(t.payload_json, '$.archivedAt') IS NULL
6610 + AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent'
6611 + AND json_extract(t.payload_json, '$.settledOverride') = 'settled'
6612 + AND json_extract(t.payload_json, '$.settledAt') IS NOT NULL${
6613 + projectIds === undefined ? sql`` : sql` AND t.project_id IN ${sql.in(projectIds)}`
6614 + }
6615 + `;
6616 + const belowCursor =
6617 + before.threadId === undefined
6618 + ? sql`older.settled_at < ${before.settledAt}`
6619 + : sql`(older.settled_at < ${before.settledAt}
6620 + OR (older.settled_at = ${before.settledAt} AND older.thread_id > ${before.threadId}))`;
6621 + const pageRows =
6622 + limit === 0
6623 + ? []
6624 + : yield* sql<{ readonly thread_id: string }>`
6625 + WITH older AS (${older})
6626 + SELECT older.thread_id FROM older
6627 + WHERE ${belowCursor}
6628 + ORDER BY older.settled_at DESC, older.thread_id ASC
6629 + LIMIT ${limit}
6630 + `;
6631 + const countRows =
6632 + limit !== 0
6633 + ? []
6634 + : yield* sql<{ readonly project_id: string; readonly count: number }>`
6635 + WITH older AS (${older})
6636 + SELECT older.project_id, COUNT(*) AS count FROM older
6637 + WHERE ${belowCursor}
6638 + GROUP BY older.project_id
6639 + `;
6640 + return {
6641 + threadIds: pageRows.map((row) => ThreadId.make(row.thread_id)),
6642 + counts: countRows.map((row) => ({
6643 + projectId: ProjectId.make(row.project_id),
6644 + count: row.count,
6645 + })),
6646 + };
6647 + }, Effect.mapError(shellSnapshotReadError));
6648 +
6649 const getThreadShell: ProjectionStoreV2Shape["getThreadShell"] = (threadId) =>
6650 sql
6651 .withTransaction(
@@ -6671,6 +6740,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6740 getShellSnapshot: readOnReader(getShellSnapshot),
6741 listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
6742 readShellSnapshot: readOnReader(readShellSnapshot),
6743 + listOlderSettledThreads: readOnReader(listOlderSettledThreads),
6744 getThreadShell: readOnReader(getThreadShell),
6745 findGuestShareThreadId: readOnReader(findGuestShareThreadId),
6746 getThread: readOnReader(getThread),
@@ -6761,16 +6831,21 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
6831 ) {
6832 return false;
6833 }
6764 - if (options?.subagentFamilyOf !== undefined) {
6765 - const member = existing.get(options.subagentFamilyOf)?.thread;
6834 + if (options?.familyOf !== undefined) {
6835 + const member = existing.get(options.familyOf)?.thread;
6836 if (
6837 member === undefined ||
6768 - projection.thread.lineage.relationshipToParent !== "subagent" ||
6838 projection.thread.lineage.rootThreadId !== member.lineage.rootThreadId
6839 ) {
6840 return false;
6841 }
6842 }
6843 + if (
6844 + options?.threadIds !== undefined &&
6845 + !options.threadIds.includes(projection.thread.id)
6846 + ) {
6847 + return false;
6848 + }
6849 if (
6850 options?.worktreePath !== undefined &&
6851 projection.thread.worktreePath !== options.worktreePath
@@ -6808,6 +6883,52 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
6883 .map(({ thread }) => thread.id);
6884 },
6885 ),
6886 + listOlderSettledThreads: Effect.fn("ProjectionStore.listOlderSettledThreads")(function* ({
6887 + before,
6888 + projectIds,
6889 + limit,
6890 + }) {
6891 + const older = [...(yield* Ref.get(replayState)).projections.values()].flatMap(
6892 + ({ thread }) => {
6893 + if (
6894 + thread.deletedAt !== null ||
6895 + thread.archivedAt !== null ||
6896 + thread.lineage.relationshipToParent === "subagent" ||
6897 + thread.settledOverride !== "settled" ||
6898 + thread.settledAt === null ||
6899 + (projectIds !== undefined && !projectIds.includes(thread.projectId))
6900 + ) {
6901 + return [];
6902 + }
6903 + const settledAt = DateTime.formatIso(thread.settledAt);
6904 + const below =
6905 + settledAt < before.settledAt ||
6906 + (before.threadId !== undefined &&
6907 + settledAt === before.settledAt &&
6908 + thread.id > before.threadId);
6909 + return below ? [{ thread, settledAt }] : [];
6910 + },
6911 + );
6912 + const counts = new Map<ProjectId, number>();
6913 + for (const { thread } of limit === 0 ? older : []) {
6914 + counts.set(thread.projectId, (counts.get(thread.projectId) ?? 0) + 1);
6915 + }
6916 + return {
6917 + threadIds: older
6918 + .toSorted((left, right) =>
6919 + left.settledAt !== right.settledAt
6920 + ? left.settledAt > right.settledAt
6921 + ? -1
6922 + : 1
6923 + : left.thread.id < right.thread.id
6924 + ? -1
6925 + : 1,
6926 + )
6927 + .slice(0, limit)
6928 + .map(({ thread }) => thread.id),
6929 + counts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
6930 + };
6931 + }),
6932 readShellSnapshot: (options) =>
6933 service.getShellSnapshot(options).pipe(Effect.map(Effect.succeed)),
6934 getThreadShell: Effect.fn("ProjectionStore.getThreadShell")(function* (threadId) {
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -212,6 +212,7 @@ it.effect(
212 getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
213 getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
214 listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
215 + listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
216 readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
217 getThreadShell: () => Effect.die("unused getThreadShell"),
218 findGuestShareThreadId: () => Effect.die("unused findGuestShareThreadId"),
apps/server/src/orchestration-v2/ShellStream.test.ts
+88
@@ -22,6 +22,7 @@ import {
22 composeShellStreamWithEnrichment,
23 dedupeShellEnrichment,
24 omitIdleSubagentShells,
25 + omitOlderSettledShells,
26 shellStreamItemFromEnrichmentRefresh,
27 shellStreamItemsFromInitialSnapshot,
28 shouldPublishShellApplicationEvent,
@@ -204,6 +205,93 @@ describe("omitIdleSubagentShells", () => {
205 });
206 });
207
208 +describe("omitOlderSettledShells", () => {
209 + const DAY = 24 * 60 * 60 * 1000;
210 + const now = DateTime.makeUnsafe(100 * DAY);
211 + const row = (
212 + id: string,
213 + settledDaysAgo: number | null,
214 + overrides: Partial<OrchestrationV2ThreadShell> = {},
215 + ) =>
216 + shellFixture({
217 + id: ThreadId.make(id),
218 + projectId: ProjectId.make(id.startsWith("b-") ? "project-b" : "project-a"),
219 + lineage: {
220 + parentThreadId: null,
221 + relationshipToParent: null,
222 + rootThreadId: ThreadId.make(id),
223 + },
224 + status: "completed",
225 + activityRunStatus: null,
226 + pendingRuntimeRequest: null,
227 + pendingBackgroundTasks: [],
228 + settledOverride: settledDaysAgo === null ? null : "settled",
229 + settledAt:
230 + settledDaysAgo === null ? null : DateTime.makeUnsafe(100 * DAY - settledDaysAgo * DAY),
231 + ...overrides,
232 + });
233 + const newest = Array.from({ length: 25 }, (_, index) => row(`new-${index}`, index / 10));
234 + const omit = (threads: ReadonlyArray<OrchestrationV2ThreadShell>) =>
235 + omitOlderSettledShells({ threads, snapshotSequence: 7, now });
236 +
237 + it("keeps the 25 newest settled threads and counts the rest per project", () => {
238 + const result = omit([...newest, row("old", 30), row("b-old", 40), row("unsettled", null)]);
239 + expect(result.threads.map((thread) => thread.id)).toEqual([
240 + ...newest.map((thread) => thread.id),
241 + "unsettled",
242 + ]);
243 + expect(result.olderSettled).toEqual({
244 + before: DateTime.formatIso(DateTime.makeUnsafe(100 * DAY - 2.4 * DAY)),
245 + asOfSequence: 7,
246 + counts: [
247 + { projectId: "project-a", count: 1 },
248 + { projectId: "project-b", count: 1 },
249 + ],
250 + });
251 + });
252 +
253 + it("keeps threads tied with the 25th newest", () => {
254 + const result = omit([...newest, row("tie", 2.4), row("older", 2.5)]);
255 + expect(result.threads.map((thread) => thread.id).slice(25)).toEqual(["tie"]);
256 + });
257 +
258 + it("omits nothing with fewer than 25 settled threads, however old", () => {
259 + const old = Array.from({ length: 5 }, (_, index) => row(`old-${index}`, 300 + index));
260 + const result = omit(old);
261 + expect(result.threads).toEqual(old);
262 + expect(result.olderSettled.counts).toEqual([]);
263 + });
264 +
265 + it("keeps older rows that are snoozed, live, or above a kept subagent, and still counts them", () => {
266 + const threads = [
267 + ...newest,
268 + row("snoozed", 30, { snoozedUntil: DateTime.makeUnsafe(101 * DAY) }),
269 + row("woke", 30, { snoozedUntil: DateTime.makeUnsafe(99 * DAY) }),
270 + row("running", 30, { activityRunStatus: "running" }),
271 + row("parent", 30),
272 + shellFixture({
273 + id: ThreadId.make("child"),
274 + projectId: ProjectId.make("project-a"),
275 + lineage: {
276 + parentThreadId: ThreadId.make("parent"),
277 + relationshipToParent: "subagent",
278 + rootThreadId: ThreadId.make("parent"),
279 + },
280 + settledOverride: null,
281 + settledAt: null,
282 + }),
283 + ];
284 + const result = omit(threads);
285 + expect(result.threads.map((thread) => thread.id).slice(25)).toEqual([
286 + "snoozed",
287 + "running",
288 + "parent",
289 + "child",
290 + ]);
291 + expect(result.olderSettled.counts).toEqual([{ projectId: "project-a", count: 4 }]);
292 + });
293 +});
294 +
295 describe("shellStreamItemsFromInitialSnapshot", () => {
296 it("keeps thread rows out of the metadata-only enrichment frame", () => {
297 const snapshot = {
apps/server/src/orchestration-v2/ShellStream.ts
+132
@@ -2,14 +2,19 @@ import type {
2 ApplicationStoredEvent,
3 OrchestrationProjectShell,
4 OrchestrationV2ArchivedShellStreamItem,
5 + OrchestrationV2OlderSettledCount,
6 + OrchestrationV2OlderSettledThreads,
7 OrchestrationV2ShellSnapshot,
8 OrchestrationV2ThreadShell,
9 OrchestrationV2ThreadShellSnapshot,
10 OrchestrationV2ShellStreamItem,
11 OrchestrationV2StoredEvent,
12 + ProjectId,
13 + ThreadId,
14 } from "@t3tools/contracts";
15 import {
16 isLiveSubagentShell,
17 + isOlderSettledThreadShell,
18 OrchestrationProjectShell as ProjectShellSchema,
19 OrchestrationV2ThreadShell as ThreadShellSchema,
20 } from "@t3tools/contracts";
@@ -17,6 +22,7 @@ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
22 import type * as Arr from "effect/Array";
23 import type * as Cause from "effect/Cause";
24 import * as Clock from "effect/Clock";
25 +import * as DateTime from "effect/DateTime";
26 import * as Deferred from "effect/Deferred";
27 import * as Effect from "effect/Effect";
28 import * as Exit from "effect/Exit";
@@ -108,6 +114,93 @@ export function activeShellRowsForSubscriber(
114 };
115 }
116
117 +const NEWEST_SETTLED_KEPT = 25;
118 +
119 +/**
120 + * Opted-in snapshots keep the 25 newest settled top-level threads (and ties) and count the rest per
121 + * project in `olderSettled`; clients page them with `getOlderSettledThreads`. Older rows stay while
122 + * pinned, snoozed, live, or above a kept subagent, so walks over shell rows still find them.
123 + */
124 +export function omitOlderSettledShells(input: {
125 + readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
126 + readonly snapshotSequence: number;
127 + readonly now: DateTime.Utc;
128 +}): {
129 + readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
130 + readonly olderSettled: OrchestrationV2OlderSettledThreads;
131 +} {
132 + const nowMs = DateTime.toEpochMillis(input.now);
133 + const settledTimes = input.threads
134 + .flatMap((thread) =>
135 + thread.lineage.relationshipToParent !== "subagent" &&
136 + thread.settledOverride === "settled" &&
137 + thread.settledAt !== null
138 + ? [DateTime.toEpochMillis(thread.settledAt)]
139 + : [],
140 + )
141 + .sort((left, right) => right - left);
142 + // With fewer than 25, the oldest one is the cut, so nothing is older.
143 + const beforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1) ?? nowMs;
144 + const ancestorIds = new Set<string>();
145 + for (const thread of input.threads) {
146 + if (thread.lineage.relationshipToParent !== "subagent") continue;
147 + if (thread.lineage.parentThreadId !== null) ancestorIds.add(thread.lineage.parentThreadId);
148 + ancestorIds.add(thread.lineage.rootThreadId);
149 + }
150 + const counts = new Map<ProjectId, number>();
151 + const threads = input.threads.filter((thread) => {
152 + if (!isOlderSettledThreadShell(thread, beforeMs)) return true;
153 + counts.set(thread.projectId, (counts.get(thread.projectId) ?? 0) + 1);
154 + return (
155 + thread.pinnedAt != null ||
156 + (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs) ||
157 + isLiveSubagentShell(thread) ||
158 + ancestorIds.has(thread.id)
159 + );
160 + });
161 + return {
162 + threads,
163 + olderSettled: {
164 + before: DateTime.formatIso(DateTime.makeUnsafe(beforeMs)),
165 + asOfSequence: input.snapshotSequence,
166 + counts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
167 + },
168 + };
169 +}
170 +
171 +/**
172 + * The snapshot a shell subscriber gets for its opt-ins, and the subagent ids whose transcript
173 + * events its stream drops (from every row, so an omitted child that wakes keeps that filter).
174 + */
175 +export function shellSnapshotForSubscriber(
176 + base: OrchestrationV2ShellSnapshot,
177 + options: {
178 + readonly omitIdleSubagents: boolean;
179 + readonly omitOlderSettled: boolean;
180 + readonly now: DateTime.Utc;
181 + },
182 +): {
183 + readonly snapshot: OrchestrationV2ShellSnapshot;
184 + readonly subagentThreadIds: Set<string>;
185 +} {
186 + const rows = activeShellRowsForSubscriber(base.threads, options.omitIdleSubagents);
187 + if (!options.omitOlderSettled) {
188 + return {
189 + snapshot: { ...base, threads: rows.threads },
190 + subagentThreadIds: rows.subagentThreadIds,
191 + };
192 + }
193 + const older = omitOlderSettledShells({
194 + threads: rows.threads,
195 + snapshotSequence: base.snapshotSequence,
196 + now: options.now,
197 + });
198 + return {
199 + snapshot: { ...base, threads: older.threads, olderSettled: older.olderSettled },
200 + subagentThreadIds: rows.subagentThreadIds,
201 + };
202 +}
203 +
204 /**
205 * One transaction covers the reads so they agree; the threads are decoded after it commits,
206 * because the server shares one SQLite connection and decoding a large shell takes longer than
@@ -140,6 +233,45 @@ export const loadShellSnapshotParts = Effect.fn("ShellStream.loadShellSnapshotPa
233 };
234 });
235
236 +/** A page of older settled threads, read and decoded like `loadShellSnapshotParts`. */
237 +export const loadOlderSettledThreadsPage = Effect.fn("ShellStream.loadOlderSettledThreadsPage")(
238 + function* <E1, E2, E3, E4>(input: {
239 + readonly sql: SqlClient.SqlClient;
240 + readonly listThreads: Effect.Effect<
241 + {
242 + readonly threadIds: ReadonlyArray<ThreadId>;
243 + readonly counts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
244 + },
245 + E1
246 + >;
247 + readonly readThreads: (
248 + threadIds: ReadonlyArray<ThreadId>,
249 + ) => Effect.Effect<Effect.Effect<OrchestrationV2ThreadShellSnapshot, E3>, E2>;
250 + readonly latestSequence: Effect.Effect<number, E4>;
251 + }) {
252 + const read = yield* input.sql
253 + .withTransaction(
254 + Effect.gen(function* () {
255 + const listed = yield* input.listThreads;
256 + return {
257 + listed,
258 + decodeThreads:
259 + listed.threadIds.length === 0 ? null : yield* input.readThreads(listed.threadIds),
260 + snapshotSequence: yield* input.latestSequence,
261 + };
262 + }),
263 + )
264 + .pipe(NodeSqliteClient.readOnly);
265 + const threads = read.decodeThreads === null ? [] : (yield* read.decodeThreads).threads;
266 + const order = new Map(read.listed.threadIds.map((threadId, index) => [threadId, index]));
267 + return {
268 + snapshotSequence: read.snapshotSequence,
269 + threads: threads.toSorted((left, right) => order.get(left.id)! - order.get(right.id)!),
270 + counts: read.listed.counts,
271 + };
272 + },
273 +);
274 +
275 export type ShellApplicationEvent =
276 | Pick<
277 Extract<ApplicationStoredEvent, { readonly aggregateKind: "project" }>,
apps/server/src/orchestration-v2/ThreadManagementService.ts
+3 -1
@@ -384,8 +384,9 @@ export interface ThreadManagementServiceShape {
384 readonly location?: "active" | "archive";
385 }) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
386 readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
387 + readonly listOlderSettledThreads: Orchestrator.OrchestratorV2["Service"]["listOlderSettledThreads"];
388 readonly readShellSnapshot: (
388 - options?: Pick<ShellSnapshotOptions, "location" | "subagentFamilyOf" | "worktreePath">,
389 + options?: Pick<ShellSnapshotOptions, "location" | "familyOf" | "worktreePath" | "threadIds">,
390 ) => Effect.Effect<
391 Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>,
392 Orchestrator.OrchestratorV2Error
@@ -1243,6 +1244,7 @@ const make = Effect.gen(function* () {
1244 getProjectThread,
1245 getShellSnapshot: orchestrator.getShellSnapshot,
1246 listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
1247 + listOlderSettledThreads: orchestrator.listOlderSettledThreads,
1248 readShellSnapshot: orchestrator.readShellSnapshot,
1249 getThreadShell: orchestrator.getThreadShell,
1250 listProjectThreads,
apps/server/src/orchestration-v2/ThreadSearch.test.ts
+164 -2
@@ -6,6 +6,7 @@ import {
6 ProviderInstanceId,
7 ThreadId,
8 type OrchestrationV2DomainEvent,
9 + type ThreadLinkedPullRequest,
10 } from "@t3tools/contracts";
11 import * as NodeServices from "@effect/platform-node/NodeServices";
12 import * as DateTime from "effect/DateTime";
@@ -65,6 +66,9 @@ const thread = (
66 readonly deletedAt?: DateTime.Utc;
67 readonly subagentOf?: ThreadId;
68 readonly title?: string;
69 + readonly settledAt?: DateTime.Utc;
70 + readonly linkedPullRequest?: ThreadLinkedPullRequest;
71 + readonly branchPullRequest?: ThreadLinkedPullRequest;
72 } = {},
73 ): OrchestrationV2DomainEvent => ({
74 id: EventId.make(`created:${threadId}`),
@@ -84,6 +88,8 @@ const thread = (
88 interactionMode: "default",
89 branch: null,
90 worktreePath: null,
91 + linkedPullRequest: overrides.linkedPullRequest ?? null,
92 + branchPullRequest: overrides.branchPullRequest ?? null,
93 activeProviderThreadId: null,
94 lineage: overrides.subagentOf
95 ? {
@@ -96,8 +102,8 @@ const thread = (
102 createdAt: at(0),
103 updatedAt: at(0),
104 archivedAt: overrides.archivedAt ?? null,
99 - settledOverride: null,
100 - settledAt: null,
105 + settledOverride: overrides.settledAt === undefined ? null : "settled",
106 + settledAt: overrides.settledAt ?? null,
107 lastVisitedAt: null,
108 deletedAt: overrides.deletedAt ?? null,
109 },
@@ -662,6 +668,162 @@ it.layer(layerTest)("ThreadSearch across scripts and without the index", (it) =>
668 );
669 });
670
671 +it.layer(layerTest)("ThreadSearch.searchOlderSettled", (it) => {
672 + it.effect("returns older settled title matches in scope, then older message hits", () =>
673 + Effect.gen(function* () {
674 + const projections = yield* ProjectionStore.ProjectionStoreV2;
675 + const search = yield* ThreadSearch.ThreadSearch;
676 + const project = ProjectId.make("project:kestrel");
677 + const other = ProjectId.make("project:kestrel-other");
678 + yield* createProject(project);
679 + yield* createProject(other);
680 + const id = (name: string) => ThreadId.make(`thread:kestrel-${name}`);
681 + const old = at(10);
682 + yield* Effect.forEach(
683 + [
684 + thread(id("old"), project, { title: "Kestrel nest", settledAt: old }),
685 + thread(id("recent"), project, { title: "Kestrel nest", settledAt: at(50) }),
686 + thread(id("open"), project, { title: "Kestrel nest" }),
687 + thread(id("child"), project, { title: "Kestrel nest", subagentOf: id("old") }),
688 + thread(id("elsewhere"), other, { title: "Kestrel nest", settledAt: old }),
689 + thread(id("note"), project, { title: "Unrelated", settledAt: old }),
690 + thread(id("new-note"), project, { title: "Unrelated", settledAt: at(50) }),
691 + ],
692 + projections.apply,
693 + { discard: true },
694 + );
695 + const before = DateTime.formatIso(at(30));
696 + const messageThreadIds = [id("new-note"), id("note"), id("old")];
697 + assert.sameMembers(
698 + [...(yield* search.searchOlderSettled({ query: "kestrel", before, messageThreadIds }))],
699 + [id("old"), id("elsewhere"), id("note")],
700 + );
701 + // The scope applies to titles and message hits; message hits follow the titles in order.
702 + assert.deepEqual(
703 + yield* search.searchOlderSettled({
704 + query: "kestrel",
705 + before,
706 + projectIds: [project],
707 + messageThreadIds: [id("elsewhere"), ...messageThreadIds],
708 + }),
709 + [id("old"), id("note")],
710 + );
711 + // A palette also matches the environment's name.
712 + assert.include(
713 + yield* search.searchOlderSettled({
714 + query: "laptop unrelated",
715 + before,
716 + environmentLabel: "Laptop",
717 + messageThreadIds: [],
718 + }),
719 + id("note"),
720 + );
721 + }),
722 + );
723 +
724 + it.effect("matches the branch and linked pull requests, like the palette", () =>
725 + Effect.gen(function* () {
726 + const projections = yield* ProjectionStore.ProjectionStoreV2;
727 + const search = yield* ThreadSearch.ThreadSearch;
728 + const project = ProjectId.make("project:heron");
729 + yield* createProject(project);
730 + const pullRequest = (number: number): ThreadLinkedPullRequest => ({
731 + projectId: project,
732 + repository: "acme/heron",
733 + number,
734 + url: `https://github.com/acme/heron/pull/${number}`,
735 + });
736 + const branch = ThreadId.make("thread:heron-branch");
737 + const linked = ThreadId.make("thread:heron-linked");
738 + yield* Effect.forEach(
739 + [
740 + thread(branch, project, {
741 + title: "Heron",
742 + settledAt: at(10),
743 + branchPullRequest: pullRequest(4101),
744 + }),
745 + thread(linked, project, {
746 + title: "Heron",
747 + settledAt: at(10),
748 + linkedPullRequest: pullRequest(4202),
749 + }),
750 + ],
751 + projections.apply,
752 + { discard: true },
753 + );
754 + const find = (query: string) =>
755 + search.searchOlderSettled({
756 + query,
757 + before: DateTime.formatIso(at(30)),
758 + messageThreadIds: [],
759 + });
760 + assert.deepEqual(yield* find("#4101"), [branch]);
761 + assert.deepEqual(yield* find("acme/heron#4202"), [linked]);
762 + }),
763 + );
764 +});
765 +
766 +it.layer(layerTest)("ThreadSearch.searchThreadsWithOlderSettledRows", (it) => {
767 + it.effect("adds older settled rows to message hits, or alone for titles only", () =>
768 + Effect.gen(function* () {
769 + const projections = yield* ProjectionStore.ProjectionStoreV2;
770 + const search = yield* ThreadSearch.ThreadSearch;
771 + const sql = yield* SqlClient.SqlClient;
772 + const project = ProjectId.make("project:plover");
773 + yield* createProject(project);
774 + const old = ThreadId.make("thread:plover-old");
775 + const recent = ThreadId.make("thread:plover-recent");
776 + yield* Effect.forEach(
777 + [
778 + thread(old, project, { title: "Plover nest", settledAt: at(10) }),
779 + thread(recent, project, { title: "Plover nest", settledAt: at(50) }),
780 + message(recent, "plover-note", "user", "a plover note", { minute: 2 }),
781 + ],
782 + projections.apply,
783 + { discard: true },
784 + );
785 + const run = (request: { readonly titlesOnly?: boolean }) =>
786 + ThreadSearch.searchThreadsWithOlderSettledRows({
787 + request: { query: "plover", olderSettledBefore: DateTime.formatIso(at(30)), ...request },
788 + search: search.search,
789 + searchOlderSettled: search.searchOlderSettled,
790 + sql,
791 + readThreads: (threadIds) =>
792 + projections.readShellSnapshot({ location: "active", threadIds }),
793 + latestSequence: Effect.succeed(7),
794 + });
795 + const withMessages = yield* run({});
796 + assert.deepEqual(
797 + withMessages.matches.map((match) => match.threadId),
798 + [recent],
799 + );
800 + assert.equal(withMessages.snapshotSequence, 7);
801 + assert.deepEqual(
802 + withMessages.threads?.map((row) => row.id),
803 + [old],
804 + );
805 + const titles = yield* run({ titlesOnly: true });
806 + assert.deepEqual(titles.matches, []);
807 + assert.deepEqual(
808 + titles.threads?.map((row) => row.id),
809 + [old],
810 + );
811 + // Without the cut it is the plain message search.
812 + assert.isUndefined(
813 + (yield* ThreadSearch.searchThreadsWithOlderSettledRows({
814 + request: { query: "plover" },
815 + search: search.search,
816 + searchOlderSettled: search.searchOlderSettled,
817 + sql,
818 + readThreads: (threadIds) =>
819 + projections.readShellSnapshot({ location: "active", threadIds }),
820 + latestSequence: Effect.succeed(7),
821 + })).threads,
822 + );
823 + }),
824 + );
825 +});
826 +
827 it.layer(layerTest)("ThreadSearch.searchThreads", (it) => {
828 it.effect("keeps room for a message-only thread behind many titled ones", () =>
829 Effect.gen(function* () {
apps/server/src/orchestration-v2/ThreadSearch.ts
+184 -1
@@ -4,8 +4,13 @@ import {
4 OrchestrationThreadSearchSource,
5 OrchestrationV2AppThreadJson,
6 OrchestrationV2ThreadOpens,
7 + ThreadLinkedPullRequest,
8 + ThreadPullRequestLink,
9 type OrchestrationSearchThreadsInput,
10 type OrchestrationSearchThreadsResult,
11 + type OrchestrationV2SearchThreadsInput,
12 + type OrchestrationV2SearchThreadsResult,
13 + type OrchestrationV2ThreadShellSnapshot,
14 ProjectId,
15 ThreadId,
16 } from "@t3tools/contracts";
@@ -29,6 +34,7 @@ import {
34 } from "@t3tools/shared/paletteSearch";
35 import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests";
36 import { indexedMessageCondition } from "./messageSearchIndex.ts";
37 +import { loadOlderSettledThreadsPage } from "./ShellStream.ts";
38
39 // When the user last wrote in the thread aliased `threads`, as the clients
40 // rank recency; agent notices in the user role do not count.
@@ -252,9 +258,40 @@ export class ThreadSearch extends Context.Service<
258 readonly searchThreads: (
259 input: OrchestrationSearchThreadsInput & { readonly projectId?: ProjectId | undefined },
260 ) => Effect.Effect<{ readonly matches: ReadonlyArray<ThreadSearchHit> }, ThreadSearchError>;
261 + /**
262 + * The settled threads before `before` that clients omitting older settled threads should
263 + * receive for a query: the best title, branch, pull request and project matches, ranked as
264 + * the palette ranks them, then the older ones among `messageThreadIds`, in their order.
265 + */
266 + readonly searchOlderSettled: (input: {
267 + readonly query: string;
268 + readonly before: string;
269 + readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
270 + /** The caller's name for this environment, which its palette also matches. */
271 + readonly environmentLabel?: string | undefined;
272 + readonly messageThreadIds: ReadonlyArray<ThreadId>;
273 + }) => Effect.Effect<ReadonlyArray<ThreadId>, ThreadSearchError>;
274 }
275 >()("t3/orchestration-v2/ThreadSearch") {}
276
277 +// Rows a client gets per search: enough for the shown results without sending every match.
278 +const OLDER_SETTLED_TITLE_ROWS = 15;
279 +const OLDER_SETTLED_MESSAGE_ROWS = 5;
280 +
281 +// Only the fields the palette scorer reads, so an interactive search does not decode payloads.
282 +const OlderSettledTitleRow = Schema.Struct({
283 + id: ThreadId,
284 + title: Schema.String,
285 + branch: Schema.NullOr(Schema.String),
286 + pullRequests: Schema.NullOr(Schema.fromJsonString(Schema.Array(ThreadPullRequestLink))),
287 + linkedPullRequest: Schema.NullOr(Schema.fromJsonString(ThreadLinkedPullRequest)),
288 + branchPullRequest: Schema.NullOr(Schema.fromJsonString(ThreadLinkedPullRequest)),
289 + opens: Schema.NullOr(Schema.String),
290 + createdAt: IsoDateTime,
291 + projectTitle: Schema.String,
292 + latestUserMessageAt: Schema.NullOr(IsoDateTime),
293 +});
294 +
295 /** @public */
296 export const make = Effect.gen(function* () {
297 const sql = yield* SqlClient.SqlClient;
@@ -298,6 +335,61 @@ export const make = Effect.gen(function* () {
335 `,
336 });
337
338 + const olderSettledTitleCandidates = SqlSchema.findAll({
339 + Request: Schema.Struct({
340 + before: Schema.String,
341 + projectIds: Schema.NullOr(TokenList),
342 + }),
343 + Result: OlderSettledTitleRow,
344 + execute: ({ before, projectIds }) => sql`
345 + SELECT
346 + threads.thread_id AS id,
347 + json_extract(threads.payload_json, '$.title') AS title,
348 + json_extract(threads.payload_json, '$.branch') AS branch,
349 + json_extract(threads.payload_json, '$.pullRequests') AS "pullRequests",
350 + json_extract(threads.payload_json, '$.linkedPullRequest') AS "linkedPullRequest",
351 + json_extract(threads.payload_json, '$.branchPullRequest') AS "branchPullRequest",
352 + json_extract(threads.payload_json, '$.opens') AS opens,
353 + json_extract(threads.payload_json, '$.createdAt') AS "createdAt",
354 + projects.title AS "projectTitle",
355 + ${latestUserMessageAt(sql)} AS "latestUserMessageAt"
356 + FROM orchestration_v2_projection_threads AS threads
357 + INNER JOIN projection_projects AS projects
358 + ON projects.project_id = threads.project_id
359 + WHERE threads.deleted_at IS NULL
360 + AND json_extract(threads.payload_json, '$.archivedAt') IS NULL
361 + AND projects.deleted_at IS NULL
362 + AND json_extract(threads.payload_json, '$.lineage.relationshipToParent')
363 + IS NOT 'subagent'
364 + AND json_extract(threads.payload_json, '$.settledOverride') = 'settled'
365 + AND json_extract(threads.payload_json, '$.settledAt') < ${before}
366 + AND (${projectIds} IS NULL
367 + OR threads.project_id IN (SELECT value FROM json_each(${projectIds})))
368 + `,
369 + });
370 +
371 + const olderSettledIds = SqlSchema.findAll({
372 + Request: Schema.Struct({
373 + before: Schema.String,
374 + threadIds: TokenList,
375 + projectIds: Schema.NullOr(TokenList),
376 + }),
377 + Result: Schema.Struct({ id: ThreadId }),
378 + execute: ({ before, threadIds, projectIds }) => sql`
379 + SELECT threads.thread_id AS id
380 + FROM orchestration_v2_projection_threads AS threads
381 + WHERE threads.thread_id IN (SELECT value FROM json_each(${threadIds}))
382 + AND threads.deleted_at IS NULL
383 + AND json_extract(threads.payload_json, '$.archivedAt') IS NULL
384 + AND json_extract(threads.payload_json, '$.lineage.relationshipToParent')
385 + IS NOT 'subagent'
386 + AND json_extract(threads.payload_json, '$.settledOverride') = 'settled'
387 + AND json_extract(threads.payload_json, '$.settledAt') < ${before}
388 + AND (${projectIds} IS NULL
389 + OR threads.project_id IN (SELECT value FROM json_each(${projectIds})))
390 + `,
391 + });
392 +
393 const read = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
394 sql.withTransaction(effect).pipe(
395 NodeSqliteClient.readOnly,
@@ -478,7 +570,98 @@ export const make = Effect.gen(function* () {
570 };
571 });
572
481 - return ThreadSearch.of({ search, searchThreads });
573 + const searchOlderSettled: ThreadSearch["Service"]["searchOlderSettled"] = Effect.fn(
574 + "ThreadSearch.searchOlderSettled",
575 + )(function* ({ query, before, projectIds, environmentLabel, messageThreadIds }) {
576 + const normalizedQuery = foldSearchQuery(query);
577 + const now = DateTime.toEpochMillis(yield* DateTime.now);
578 + const candidates = yield* read(
579 + olderSettledTitleCandidates({ before, projectIds: projectIds ?? null }),
580 + );
581 + const subjectOf = (row: typeof OlderSettledTitleRow.Type): ThreadSearchSubject => ({
582 + id: row.id,
583 + title: row.title,
584 + branch: row.branch,
585 + projectTitle: row.projectTitle,
586 + environmentLabel: environmentLabel ?? null,
587 + pullRequests: threadSearchPullRequests({
588 + pullRequests: row.pullRequests ?? undefined,
589 + linkedPullRequest: row.linkedPullRequest,
590 + branchPullRequest: row.branchPullRequest,
591 + }),
592 + // Like the clients: what the user last wrote, else when the thread began.
593 + activityAt: Date.parse(row.latestUserMessageAt ?? row.createdAt),
594 + opens: Option.getOrNull(decodeThreadOpens(row.opens)),
595 + });
596 + // Picks on, as in the command palette: a thread picked for this query must make the cut.
597 + const titleIds = rankSearchItems(candidates, (row, options) =>
598 + scoreThreadSearch(normalizedQuery, subjectOf(row), now, { ...options, picks: true }),
599 + )
600 + .slice(0, OLDER_SETTLED_TITLE_ROWS)
601 + .map(({ item }) => item.id);
602 + const shown = new Set(titleIds);
603 + const messageCandidates = [...new Set(messageThreadIds)].filter((id) => !shown.has(id));
604 + const older =
605 + messageCandidates.length === 0
606 + ? new Set<ThreadId>()
607 + : new Set(
608 + (yield* read(
609 + olderSettledIds({
610 + before,
611 + threadIds: messageCandidates,
612 + projectIds: projectIds ?? null,
613 + }),
614 + )).map((row) => row.id),
615 + );
616 + return [
617 + ...titleIds,
618 + ...messageCandidates.filter((id) => older.has(id)).slice(0, OLDER_SETTLED_MESSAGE_ROWS),
619 + ];
620 + });
621 +
622 + return ThreadSearch.of({ search, searchThreads, searchOlderSettled });
623 });
624
625 export const layer = Layer.effect(ThreadSearch, make);
626 +
627 +/**
628 + * The client search: message hits, and with `olderSettledBefore`, the rows of the best older
629 + * settled matches, which shells that omit them do not hold.
630 + */
631 +export const searchThreadsWithOlderSettledRows = Effect.fn(
632 + "ThreadSearch.searchThreadsWithOlderSettledRows",
633 +)(function* <E1, E2, E3, E4, E5>(input: {
634 + readonly request: OrchestrationV2SearchThreadsInput;
635 + readonly search: (
636 + input: OrchestrationSearchThreadsInput,
637 + ) => Effect.Effect<OrchestrationSearchThreadsResult, E1>;
638 + readonly searchOlderSettled: (
639 + input: Parameters<ThreadSearch["Service"]["searchOlderSettled"]>[0],
640 + ) => Effect.Effect<ReadonlyArray<ThreadId>, E2>;
641 + readonly sql: SqlClient.SqlClient;
642 + readonly readThreads: (
643 + threadIds: ReadonlyArray<ThreadId>,
644 + ) => Effect.Effect<Effect.Effect<OrchestrationV2ThreadShellSnapshot, E4>, E3>;
645 + readonly latestSequence: Effect.Effect<number, E5>;
646 +}) {
647 + const { olderSettledBefore, projectIds, environmentLabel, titlesOnly, ...request } =
648 + input.request;
649 + const messages: OrchestrationV2SearchThreadsResult =
650 + titlesOnly === true ? { matches: [] } : yield* input.search(request);
651 + if (olderSettledBefore === undefined) return messages;
652 + // Ranked before the rows' transaction, so scoring never holds the shared reader.
653 + const threadIds = yield* input.searchOlderSettled({
654 + query: request.query,
655 + before: olderSettledBefore,
656 + projectIds,
657 + environmentLabel,
658 + messageThreadIds: messages.matches.map((match) => match.threadId),
659 + });
660 + const older = yield* loadOlderSettledThreadsPage({
661 + sql: input.sql,
662 + listThreads: Effect.succeed({ threadIds, counts: [] }),
663 + readThreads: input.readThreads,
664 + latestSequence: input.latestSequence,
665 + });
666 + return { ...messages, snapshotSequence: older.snapshotSequence, threads: older.threads };
667 +});
apps/server/src/orchestration-v2/http.test.ts
+55 -3
@@ -96,9 +96,9 @@ const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
96 })),
97 );
98
99 -it.effect("exports full history with size, scope, and missing-thread checks", () =>
99 +// Serves the orchestration HTTP routes over real stores; `allowed` gates the read scope.
100 +const serveOrchestrationHttp = (allowed: () => boolean) =>
101 Effect.gen(function* () {
101 - let allowed = true;
102 const runtime = ProviderReplayHarness.layerWithRegistry(
103 { name: "thread-transcript" },
104 ProviderAdapterRegistry.layerFromAdapters([]),
@@ -143,7 +143,7 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
143 sessionId: AuthSessionId.make("test-session"),
144 subject: "test",
145 method: "bearer-access-token",
146 - scopes: new Set<AuthEnvironmentScope>(allowed ? ["orchestration:read"] : []),
146 + scopes: new Set<AuthEnvironmentScope>(allowed() ? ["orchestration:read"] : []),
147 }),
148 ),
149 ),
@@ -157,6 +157,13 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
157 );
158 const { handler, dispose } = HttpRouter.toWebHandler(routes, { disableLogger: true });
159 yield* Effect.addFinalizer(() => Effect.promise(dispose));
160 + return { services, eventSink, handler };
161 + });
162 +
163 +it.effect("exports full history with size, scope, and missing-thread checks", () =>
164 + Effect.gen(function* () {
165 + let allowed = true;
166 + const { services, eventSink, handler } = yield* serveOrchestrationHttp(() => allowed);
167 const read = (threadId: ThreadId) =>
168 Effect.promise(() =>
169 handler(
@@ -242,3 +249,48 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
249 expect((yield* read(thread.id)).status).toBe(403);
250 }),
251 );
252 +
253 +it.effect("leaves older settled threads out of the HTTP shell only when asked", () =>
254 + Effect.gen(function* () {
255 + const { eventSink, handler } = yield* serveOrchestrationHttp(() => true);
256 + yield* eventSink.write({
257 + events: Array.from({ length: 26 }, (_, index) => {
258 + const id = ThreadId.make(`settled-${index}`);
259 + return {
260 + id: EventId.make(`create-${id}`),
261 + type: "thread.created" as const,
262 + threadId: id,
263 + occurredAt: now,
264 + payload: {
265 + ...thread,
266 + id,
267 + lineage: { ...thread.lineage, rootThreadId: id },
268 + settledOverride: "settled" as const,
269 + settledAt: DateTime.makeUnsafe(Date.UTC(2026, 8, 1 + index)),
270 + },
271 + };
272 + }),
273 + });
274 + const shell = (query: string) =>
275 + Effect.promise(async () => {
276 + const response = await handler(
277 + new Request(`http://localhost/api/orchestration/shell${query}`, {
278 + headers: { [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT },
279 + }),
280 + );
281 + return (await response.json()) as {
282 + readonly threads: ReadonlyArray<{ readonly id: string }>;
283 + readonly olderSettled?: { readonly counts: ReadonlyArray<unknown> };
284 + };
285 + });
286 +
287 + const full = yield* shell("");
288 + expect(full.threads).toHaveLength(27);
289 + expect(full.olderSettled).toBeUndefined();
290 + const cut = yield* shell("?omitOlderSettled=1");
291 + expect(cut.threads.map((row) => row.id).toSorted()).toEqual(
292 + [thread.id, ...Array.from({ length: 25 }, (_, index) => `settled-${index + 1}`)].toSorted(),
293 + );
294 + expect(cut.olderSettled?.counts).toEqual([{ projectId: thread.projectId, count: 1 }]);
295 + }),
296 +);
apps/server/src/orchestration-v2/http.ts
+27 -20
@@ -3,6 +3,7 @@ import {
3 EnvironmentHttpApi,
4 type OrchestrationProjectShell,
5 } from "@t3tools/contracts";
6 +import * as DateTime from "effect/DateTime";
7 import * as Effect from "effect/Effect";
8 import * as Predicate from "effect/Predicate";
9 import * as Schema from "effect/Schema";
@@ -33,7 +34,7 @@ import * as ProjectStore from "./ProjectStore.ts";
34 import {
35 buildActiveShellSnapshot,
36 loadShellSnapshotParts,
36 - omitIdleSubagentShells,
37 + shellSnapshotForSubscriber,
38 } from "./ShellStream.ts";
39 import { boundedSnapshotResponseFields } from "./ThreadStream.ts";
40 import { projectThreadProjectionForWire } from "./WireProjection.ts";
@@ -77,24 +78,27 @@ export const layer = HttpApiBuilder.group(
78 ),
79 );
80
80 - const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(function* (
81 - omitIdleSubagents: boolean,
82 - ) {
83 - const base = buildActiveShellSnapshot(
84 - yield* loadShellSnapshotParts({
85 - sql,
86 - readThreads: threadManagement.readShellSnapshot({ location: "active" }),
87 - listProjects: projectStore.listShells(),
88 - latestSequence: applicationEvents.latestApplicationSequence,
89 - }),
90 - );
91 - const projects = yield* enrichProjectShells(base.projects);
92 - return {
93 - ...base,
94 - threads: omitIdleSubagents ? omitIdleSubagentShells(base.threads) : base.threads,
95 - projects,
96 - };
97 - });
81 + const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(
82 + function* (options: {
83 + readonly omitIdleSubagents: boolean;
84 + readonly omitOlderSettled: boolean;
85 + }) {
86 + const base = buildActiveShellSnapshot(
87 + yield* loadShellSnapshotParts({
88 + sql,
89 + readThreads: threadManagement.readShellSnapshot({ location: "active" }),
90 + listProjects: projectStore.listShells(),
91 + latestSequence: applicationEvents.latestApplicationSequence,
92 + }),
93 + );
94 + const { snapshot } = shellSnapshotForSubscriber(base, {
95 + ...options,
96 + now: yield* DateTime.now,
97 + });
98 + const projects = yield* enrichProjectShells(base.projects);
99 + return { ...snapshot, projects };
100 + },
101 + );
102
103 const loadThreadSnapshot = Effect.fn("http.orchestration.loadThreadSnapshot")(function* (
104 threadId: Parameters<typeof threadManagement.getThreadSnapshot>[0],
@@ -162,7 +166,10 @@ export const layer = HttpApiBuilder.group(
166 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
167 yield* annotateEnvironmentRequest(args.endpoint.name);
168 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
165 - return yield* loadShellSnapshot(args.query.omitIdleSubagents === "1").pipe(
169 + return yield* loadShellSnapshot({
170 + omitIdleSubagents: args.query.omitIdleSubagents === "1",
171 + omitOlderSettled: args.query.omitOlderSettled === "1",
172 + }).pipe(
173 traceLocalHandlerWork,
174 Effect.catch((cause) =>
175 failEnvironmentInternal("orchestration_snapshot_failed", cause),
apps/server/src/relay/AgentAwarenessRelay.test.ts
+1
@@ -188,6 +188,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
188 return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
189 }),
190 listActiveSubagentThreadIds: unused,
191 + listOlderSettledThreads: unused,
192 readShellSnapshot: unused,
193 ensureLegacyTranscript: unused,
194 dispatch: unused,
apps/server/src/ws.test.ts
+39 -11
@@ -14,6 +14,7 @@ import {
14 type ServerConfigStreamEvent,
15 } from "@t3tools/contracts";
16 import * as Deferred from "effect/Deferred";
17 +import * as DateTime from "effect/DateTime";
18 import * as Effect from "effect/Effect";
19 import * as Layer from "effect/Layer";
20 import * as PubSub from "effect/PubSub";
@@ -292,33 +293,45 @@ it.live.each(["thread", "shell"] as const)(
293 }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
294 );
295
295 -it.effect("shell snapshots leave idle subagent children out only for clients that ask", () =>
296 +it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
297 Effect.gen(function* () {
297 - const shell = (id: string, parent: string | null, activityRunStatus: "running" | null) =>
298 + const shell = (
299 + id: string,
300 + parent: string | null,
301 + activityRunStatus: "running" | null,
302 + settledDaysAgo: number | null = null,
303 + ) =>
304 ({
305 id: ThreadId.make(id),
306 + projectId: "project",
307 archivedAt: null,
308 status: activityRunStatus ?? "completed",
309 activityRunStatus,
310 pendingRuntimeRequest: null,
311 pendingBackgroundTasks: [],
312 + settledOverride: settledDaysAgo === null ? null : "settled",
313 + settledAt:
314 + settledDaysAgo === null ? null : DateTime.makeUnsafe(-settledDaysAgo * 86_400_000),
315 lineage: {
316 parentThreadId: parent === null ? null : ThreadId.make(parent),
317 relationshipToParent: parent === null ? null : "subagent",
308 - rootThreadId: ThreadId.make("parent"),
318 + rootThreadId: ThreadId.make(parent ?? id),
319 },
320 }) as unknown as OrchestrationV2ThreadShell;
321 const threads = [
322 shell("parent", null, null),
323 shell("idle", "parent", null),
324 shell("live", "parent", "running"),
325 + ...Array.from({ length: 25 }, (_, index) => shell(`recent-${index}`, null, null, 1)),
326 + shell("old", null, null, 30),
327 ];
316 - const firstSnapshotThreadIds = (input: { readonly omitIdleSubagents?: boolean }) =>
328 + const firstSnapshot = (input: {
329 + readonly omitIdleSubagents?: boolean;
330 + readonly omitOlderSettled?: boolean;
331 + }) =>
332 subscribeOrchestrationV2Shell(input).pipe(
333 Effect.flatMap((stream) => stream.pipe(Stream.take(1), Stream.runCollect)),
319 - Effect.map(([item]) =>
320 - item?.kind === "snapshot" ? item.snapshot.threads.map((thread) => thread.id) : [],
321 - ),
334 + Effect.map(([item]) => (item?.kind === "snapshot" ? item.snapshot : null)),
335 Effect.provideService(ThreadManagement.ThreadManagementService, {
336 readShellSnapshot: () =>
337 Effect.succeed(
@@ -349,10 +362,25 @@ it.effect("shell snapshots leave idle subagent children out only for clients tha
362 ),
363 );
364
352 - assert.deepEqual(yield* firstSnapshotThreadIds({}), ["parent", "idle", "live"]);
353 - assert.deepEqual(yield* firstSnapshotThreadIds({ omitIdleSubagents: true }), [
354 - "parent",
355 - "live",
365 + const recent = threads.slice(3, 28).map((thread) => thread.id);
366 + const full = yield* firstSnapshot({});
367 + assert.deepEqual(
368 + full?.threads.map((thread) => thread.id),
369 + threads.map((thread) => thread.id),
370 + );
371 + assert.isUndefined(full?.olderSettled);
372 + const withoutChildren = yield* firstSnapshot({ omitIdleSubagents: true });
373 + assert.deepEqual(
374 + withoutChildren?.threads.map((thread) => thread.id),
375 + ["parent", "live", ...recent, "old"],
376 + );
377 + const withoutOld = yield* firstSnapshot({ omitIdleSubagents: true, omitOlderSettled: true });
378 + assert.deepEqual(
379 + withoutOld?.threads.map((thread) => thread.id),
380 + ["parent", "live", ...recent],
381 + );
382 + assert.deepEqual(withoutOld?.olderSettled?.counts, [
383 + { projectId: ProjectId.make("project"), count: 1 },
384 ]);
385 }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
386 );
apps/server/src/ws.ts
+37 -10
@@ -131,11 +131,12 @@ import {
131 coalesceStoredThreadEvents,
132 composeShellStreamWithEnrichment,
133 dedupeShellEnrichment,
134 - activeShellRowsForSubscriber,
134 + loadOlderSettledThreadsPage,
135 loadShellSnapshotParts,
136 shellStreamItemFromEnrichmentRefresh,
137 shellStreamItemFromThreadShell,
138 shellStreamItemsFromInitialSnapshot,
139 + shellSnapshotForSubscriber,
140 shellStreamItemsFromResumeSnapshot,
141 shouldPublishShellApplicationEvent,
142 skipUnchangedThreadShells,
@@ -989,6 +990,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
990 readonly afterSequence?: number;
991 readonly requestCompletionMarker?: boolean;
992 readonly omitIdleSubagents?: boolean;
993 + readonly omitOlderSettled?: boolean;
994 }) {
995 const sql = yield* SqlClient.SqlClient;
996 const threadManagement = yield* ThreadManagementService.ThreadManagementService;
@@ -1025,13 +1027,16 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1027 latestSequence: applicationEvents.latestApplicationSequence,
1028 }),
1029 );
1028 - const rows = activeShellRowsForSubscriber(base.threads, input.omitIdleSubagents === true);
1029 - subagentThreadIds = rows.subagentThreadIds;
1030 + const forSubscriber = shellSnapshotForSubscriber(base, {
1031 + omitIdleSubagents: input.omitIdleSubagents === true,
1032 + omitOlderSettled: input.omitOlderSettled === true,
1033 + now: yield* DateTime.now,
1034 + });
1035 + subagentThreadIds = forSubscriber.subagentThreadIds;
1036 const enriched = yield* enrichProjectShells(base.projects);
1037 return {
1038 snapshot: {
1033 - ...base,
1034 - threads: rows.threads,
1039 + ...forSubscriber.snapshot,
1040 projects: enriched.projects,
1041 } as OrchestrationV2ShellSnapshot,
1042 resolvedRepositoryIdentityRoots: enriched.resolvedRepositoryIdentityRoots,
@@ -1778,6 +1783,7 @@ export const layerWsRpc = (
1783 settings,
1784 shellResumeCompletionMarker: true,
1785 shellThreadFamilies: true,
1786 + shellOlderSettled: true,
1787 threadResumeCompletionMarker: true,
1788 threadSnapshotPagination: true,
1789 threadFind: true,
@@ -1827,7 +1833,7 @@ export const layerWsRpc = (
1833 // Rows the shell can omit, read with the same snapshot sequence the shell uses.
1834 const readOmittableShellRows = Effect.fn("ws.orchestrationV2.readOmittableShellRows")(
1835 function* (
1830 - options: { readonly subagentFamilyOf: ThreadId } | { readonly worktreePath: string },
1836 + options: { readonly familyOf: ThreadId } | { readonly worktreePath: string },
1837 message: string,
1838 ) {
1839 const { threads, snapshotSequence } = yield* loadShellSnapshotParts({
@@ -1981,7 +1987,15 @@ export const layerWsRpc = (
1987 .searchThreadStream(input)
1988 .pipe(Stream.mapError((cause) => new OrchestrationV2SearchThreadError({ cause }))),
1989 [ORCHESTRATION_V2_WS_METHODS.searchThreads]: (input) =>
1984 - threadSearch.search(input).pipe(
1990 + ThreadSearch.searchThreadsWithOlderSettledRows({
1991 + request: input,
1992 + search: threadSearch.search,
1993 + searchOlderSettled: threadSearch.searchOlderSettled,
1994 + sql,
1995 + readThreads: (threadIds) =>
1996 + threadManagement.readShellSnapshot({ location: "active", threadIds }),
1997 + latestSequence: applicationEvents.latestApplicationSequence,
1998 + }).pipe(
1999 Effect.mapError(
2000 (cause) =>
2001 new OrchestrationSearchThreadsError({
@@ -1993,9 +2007,22 @@ export const layerWsRpc = (
2007 [ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot]: (_input) =>
2008 getOrchestrationV2ArchivedShellSnapshot,
2009 [ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: (input) =>
1996 - readOmittableShellRows(
1997 - { subagentFamilyOf: input.threadId },
1998 - "Failed to load the thread family",
2010 + readOmittableShellRows({ familyOf: input.threadId }, "Failed to load the thread family"),
2011 + [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: (input) =>
2012 + loadOlderSettledThreadsPage({
2013 + sql,
2014 + listThreads: threadManagement.listOlderSettledThreads(input),
2015 + readThreads: (threadIds) =>
2016 + threadManagement.readShellSnapshot({ location: "active", threadIds }),
2017 + latestSequence: applicationEvents.latestApplicationSequence,
2018 + }).pipe(
2019 + Effect.mapError(
2020 + (cause) =>
2021 + new OrchestrationV2GetShellSnapshotError({
2022 + message: "Failed to load older settled threads",
2023 + cause,
2024 + }),
2025 + ),
2026 ),
2027 [ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds]: (input) =>
2028 readOmittableShellRows(
apps/web/src/components/CommandPaletteThreadItems.tsx
+16 -1
@@ -68,10 +68,25 @@ function useProviderEntryByEnvironmentAndInstanceId() {
68
69 function useConnectedThreadSearch(query: string) {
70 const { environments } = useEnvironments();
71 + const { projectEnvironmentLocationById } = useEnvironmentLookups();
72 const environmentIds: ReadonlyArray<EnvironmentId> = environments
73 .filter((environment) => environment.connection.phase === "connected")
74 .map((environment) => environment.environmentId);
74 - return useThreadSearch(environmentIds, query);
75 + // The palette matches environment names when there are several; older rows must match the same.
76 + const environmentLabels =
77 + environments.length > 1
78 + ? Object.fromEntries(
79 + environmentIds.flatMap((environmentId) => {
80 + const label = projectEnvironmentLocationById.get(environmentId)?.label;
81 + return label === undefined || label === null ? [] : [[environmentId, label]];
82 + }),
83 + )
84 + : undefined;
85 + return useThreadSearch(
86 + environmentIds,
87 + query,
88 + environmentLabels === undefined ? {} : { environmentLabels },
89 + );
90 }
91
92 /**
apps/web/src/components/LegacySidebar.tsx
+16 -4
@@ -128,6 +128,7 @@ import { useDesktopUpdateState } from "../state/desktopUpdate";
128 import { useThreadActions } from "../hooks/useThreadActions";
129 import { projectEnvironment } from "../state/projects";
130 import { threadEnvironment, useEnvironmentThread } from "../state/threads";
131 +import { readUnloadedThreadCount } from "../state/olderSettledShelf";
132 import { useEnvironment, useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
133 import { buildThreadRouteParams, resolveActiveThreadRouteRef } from "../threadRoutes";
134 import { stackedThreadToast, toastManager } from "./ui/toast";
@@ -1487,7 +1488,12 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
1488 }
1489
1490 const memberProjectRef = scopeProjectRef(member.environmentId, member.id);
1490 - const memberThreadCount = memberThreadCountByPhysicalKey.get(member.physicalProjectKey) ?? 0;
1491 + const memberThreadCount =
1492 + (memberThreadCountByPhysicalKey.get(member.physicalProjectKey) ?? 0) +
1493 + readUnloadedThreadCount(
1494 + [{ environmentId: member.environmentId, id: member.id }],
1495 + sidebarThreads,
1496 + );
1497 if (memberThreadCount > 0) {
1498 const warningToastId = toastManager.add(
1499 stackedThreadToast({
@@ -1510,11 +1516,17 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
1516 thread.environmentId === memberProjectRef.environmentId &&
1517 thread.projectId === memberProjectRef.projectId,
1518 );
1519 + const threadCount =
1520 + latestProjectThreads.length +
1521 + readUnloadedThreadCount(
1522 + [{ environmentId: member.environmentId, id: member.id }],
1523 + latestProjectThreads,
1524 + );
1525 const confirmed = await api.dialogs.confirm(
1514 - latestProjectThreads.length > 0
1526 + threadCount > 0
1527 ? [
1516 - `Remove project "${member.title}" and delete its ${latestProjectThreads.length} thread${
1517 - latestProjectThreads.length === 1 ? "" : "s"
1528 + `Remove project "${member.title}" and delete its ${threadCount} thread${
1529 + threadCount === 1 ? "" : "s"
1530 }?`,
1531 `Path: ${member.workspaceRoot}`,
1532 ...(member.environmentLabel
apps/web/src/components/Sidebar.tsx
+70 -17
@@ -53,9 +53,11 @@ import {
53 } from "@t3tools/client-runtime/environment";
54 import {
55 AuthOrchestrationOperateScope,
56 + ProjectId,
57 type ScopedThreadRef,
58 type ThreadId,
59 } from "@t3tools/contracts";
60 +import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads";
61
62 import { FolderIcon, ListFilterIcon, PlusIcon, SettingsIcon } from "lucide-react";
63 import {
@@ -133,6 +135,7 @@ import { environmentPresentations } from "../state/presentation";
135 import { primaryServerKeybindingsAtom, serverEnvironment } from "../state/server";
136 import { environmentProjects } from "../state/projects";
137 import { threadEnvironment } from "../state/threads";
138 +import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../state/olderSettledShelf";
139 import { useThreadSearch } from "../state/queries";
140 import { useOrchestrationCommand } from "../state/use-orchestration-command";
141 import { readEnvironmentScope } from "../state/session";
@@ -431,16 +434,28 @@ function useSidebarThreads(input: {
434
435 const { pinnedThreads, activeThreads, workingThreads, snoozedThreads, settledThreads } =
436 partition;
437 + // Older settled threads load in pages; the shelf lists them only as far as they are loaded.
438 + const olderSettledShelf = useOlderSettledShelf({
439 + scopes: olderSettledScopes([...serverConfigs.keys()], scopedProjectKeys),
440 + expanded: settledShelfExpanded,
441 + visibleCount: settledVisibleCount,
442 + });
443 + const listedSettledThreads = settledThreads.filter(olderSettledShelf.isListed);
444 + useOlderSettledShelfGrowth(olderSettledShelf, listedSettledThreads);
445 + const settledCount =
446 + settledThreads.length + olderSettledShelf.unloadedCount(partition.visibleThreads);
447 const visibleSettledThreads = (() => {
435 - if (settledThreads.length <= settledVisibleCount) return settledThreads;
436 - const visible = settledThreads.slice(0, settledVisibleCount);
437 - if (routeRowThreadKey !== null) {
438 - const routeThread = settledThreads
439 - .slice(settledVisibleCount)
440 - .find((thread) => threadShellKey(thread) === routeRowThreadKey);
448 + const visible = listedSettledThreads.slice(0, settledVisibleCount);
449 + if (
450 + routeRowThreadKey !== null &&
451 + !visible.some((thread) => threadShellKey(thread) === routeRowThreadKey)
452 + ) {
453 + const routeThread = settledThreads.find(
454 + (thread) => threadShellKey(thread) === routeRowThreadKey,
455 + );
456 if (routeThread !== undefined) visible.push(routeThread);
457 }
443 - return visible;
458 + return visible.length === settledThreads.length ? settledThreads : visible;
459 })();
460 const renderedSettledThreads = settledShelfExpanded
461 ? visibleSettledThreads
@@ -481,7 +496,9 @@ function useSidebarThreads(input: {
496 ...settledThreads,
497 ],
498 settledVisibleCount,
484 - hiddenSettledCount: settledThreads.length - visibleSettledThreads.length,
499 + settledCount,
500 + olderSettledShelf,
501 + hiddenSettledCount: Math.max(0, settledCount - visibleSettledThreads.length),
502 showMoreSettled: () =>
503 setSettledTail((current) => ({
504 scopeKey: current.scopeKey,
@@ -602,10 +619,28 @@ function partitionSidebarThreads(input: SidebarThreadPartitionInput) {
619 firstValidTimestampMs(right.snoozedUntil ?? null),
620 ),
621 settledThreads: sortSettledThreads(settled),
622 + visibleThreads: visible,
623 snoozeNow: preciseNow,
624 };
625 }
626
627 +/** The sidebar's scope per environment: the scoped projects there, or every project. */
628 +function olderSettledScopes(
629 + environmentIds: ReadonlyArray<EnvironmentId>,
630 + scopedProjectKeys: ReadonlySet<string> | null,
631 +): ReadonlyArray<OlderSettledScope> {
632 + if (scopedProjectKeys === null) {
633 + return environmentIds.map((environmentId) => ({ environmentId, projectIds: null }));
634 + }
635 + return environmentIds.flatMap((environmentId) => {
636 + const prefix = `${environmentId}:`;
637 + const projectIds = [...scopedProjectKeys].flatMap((key) =>
638 + key.startsWith(prefix) ? [ProjectId.make(key.slice(prefix.length))] : [],
639 + );
640 + return projectIds.length === 0 ? [] : [{ environmentId, projectIds }];
641 + });
642 +}
643 +
644 function isOptimisticDropReleased(input: {
645 readonly optimisticDrop: SidebarOptimisticDrop;
646 readonly threads: readonly EnvironmentThreadShell[];
@@ -874,7 +909,6 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
909 const [threadSearchQuery, setThreadSearchQuery] = useState("");
910 const [searchHighlightKey, setSearchHighlightKey] = useState<string | null>(null);
911 const searchEnvironmentIds = useConnectedEnvironmentIds();
877 - const threadSearch = useThreadSearch(searchEnvironmentIds, threadSearchQuery);
912 const handleNewThreadRef = useRef(newThreadContext.handleNewThread);
913 const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
914 const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
@@ -952,6 +986,17 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
986 (projectRef) => `${projectRef.environmentId}:${projectRef.projectId}`,
987 ),
988 );
989 + const threadSearch = useThreadSearch(
990 + searchEnvironmentIds,
991 + threadSearchQuery,
992 + scopedProjectGroup === null
993 + ? {}
994 + : {
995 + projectIds: scopedProjectGroup.memberProjectRefs.map(
996 + (projectRef) => projectRef.projectId,
997 + ),
998 + },
999 + );
1000 useEffect(() => {
1001 const nextKey = resolveSidebarProjectScopeKey({
1002 groups: projectGroups,
@@ -1037,6 +1082,8 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1082 snoozeNow,
1083 searchableThreads,
1084 settledVisibleCount,
1085 + settledCount,
1086 + olderSettledShelf,
1087 hiddenSettledCount,
1088 showMoreSettled,
1089 settledShelfExpanded,
@@ -1672,7 +1719,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1719 activeThreads.length +
1720 workingThreads.length +
1721 snoozedThreads.length +
1675 - settledThreads.length ===
1722 + settledCount ===
1723 0
1724 ) {
1725 return [];
@@ -3128,9 +3175,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
3175 workingThreads.length + snoozedThreads.length === 0 && "mt-auto",
3176 )}
3177 label={
3131 - settledShelfExpanded
3132 - ? "Settled"
3133 - : `Settled (${settledThreads.length})`
3178 + settledShelfExpanded ? "Settled" : `Settled (${settledCount})`
3179 }
3180 dragging={from !== null}
3181 isDropTarget={dragTargetSection === "settled"}
@@ -3167,11 +3212,19 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
3212 <li className="list-none">
3213 <button
3214 type="button"
3170 - onClick={showMoreSettled}
3171 - className="flex h-9 w-full cursor-pointer items-center gap-2.5 rounded-md px-2.5 text-left text-sm text-sidebar-muted-foreground/55 hover:bg-sidebar-row-hover hover:text-sidebar-foreground"
3215 + disabled={olderSettledShelf.loading}
3216 + onClick={() => {
3217 + olderSettledShelf.retry();
3218 + showMoreSettled();
3219 + }}
3220 + className="flex h-9 w-full cursor-pointer items-center gap-2.5 rounded-md px-2.5 text-left text-sm text-sidebar-muted-foreground/55 hover:bg-sidebar-row-hover hover:text-sidebar-foreground disabled:cursor-default"
3221 >
3222 <PlusIcon aria-hidden className="size-4 shrink-0" />
3174 - Show {Math.min(hiddenSettledCount, SETTLED_TAIL_PAGE_COUNT)} more
3223 + {olderSettledShelf.loading
3224 + ? "Loading"
3225 + : olderSettledShelf.failed
3226 + ? "Could not load older threads. Retry"
3227 + : `Show ${Math.min(hiddenSettledCount, SETTLED_TAIL_PAGE_COUNT)} more`}
3228 </button>
3229 </li>
3230 ) : null}
@@ -3186,7 +3239,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
3239 activeThreads.length +
3240 workingThreads.length +
3241 snoozedThreads.length +
3189 - settledThreads.length ===
3242 + settledCount ===
3243 0 ? (
3244 <div className="flex flex-col items-center gap-2 px-2 py-6 text-center text-xs text-muted-foreground/60">
3245 {projects.length === 0 ? (
apps/web/src/components/chat/useComposerMenuItems.ts
+3 -1
@@ -29,7 +29,7 @@ import {
29 type EnvironmentQueryTarget,
30 } from "~/state/pullRequests";
31 import { useEnvironmentQuery } from "~/state/query";
32 -import { useComposerPathSearch, useDebouncedValue } from "~/state/queries";
32 +import { useComposerPathSearch, useDebouncedValue, useThreadSearch } from "~/state/queries";
33 import {
34 matchesPullRequestQuery,
35 rankPullRequestMatches,
@@ -92,6 +92,8 @@ export function useComposerMenuItems({
92 pullRequestTextQuery === debouncedPullRequestTextQuery ? pullRequestTextQuery : null;
93 const isPathTrigger = composerTriggerKind === "path";
94 const environmentThreadShells = useThreadShells(isPathTrigger);
95 + // Mounts older settled threads matching the mention, which shells may leave out.
96 + useThreadSearch(isPathTrigger ? [environmentId] : [], pathTriggerQuery, { titlesOnly: true });
97 const workspaceEntries = useComposerPathSearch({
98 environmentId,
99 cwd: isPathTrigger ? gitCwd : null,
apps/web/src/components/promptLinks/NewThreadLinkView.tsx
+68 -9
@@ -1,8 +1,9 @@
1 import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
2 import { getCloneDestinationPath } from "@t3tools/client-runtime/operations/projects";
3 import {
4 - findPromptLinkProject,
4 + findPromptLinkProjects,
5 findPromptLinkPullRequestThread,
6 + pickPromptLinkPullRequestThread,
7 resolvePromptLinkModelSelection,
8 } from "@t3tools/client-runtime/prompt-links";
9 import {
@@ -10,6 +11,7 @@ import {
11 squashAtomCommandFailure,
12 } from "@t3tools/client-runtime/state/runtime";
13 import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
14 +import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
15 import {
16 type NewThreadLink,
17 type PromptLinkRepository,
@@ -17,6 +19,7 @@ import {
19 } from "@t3tools/shared/promptLinks";
20 import { useAtomValue } from "@effect/atom-react";
21 import { useLocation, useNavigate } from "@tanstack/react-router";
22 +import { Atom } from "effect/reactivity";
23 import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react";
24
25 import { useComposerDraftStore } from "~/composerDraftStore";
@@ -28,8 +31,9 @@ import { useProjects, useThreadShells, waitForProject } from "~/state/entities";
31 import { appAtomRegistry } from "~/rpc/atomRegistry";
32 import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
33 import { environmentPresentations } from "~/state/presentation";
31 -import { allReachableShellsLiveAtom } from "~/state/shell";
34 +import { allReachableShellsLiveAtom, environmentShell } from "~/state/shell";
35 import { sourceControlEnvironment } from "~/state/sourceControl";
36 +import { pullRequestEnvironment } from "~/state/pullRequests";
37 import { useAtomCommand } from "~/state/use-atom-command";
38
39 import { usePullRequestHandoffs } from "../pullRequest/usePullRequestActions";
@@ -52,13 +56,46 @@ type LinkState =
56 };
57
58 type LinkLaunch =
55 - | { readonly kind: "thread"; readonly thread: ReturnType<typeof useThreadShells>[number] }
59 + | {
60 + readonly kind: "thread";
61 + readonly thread: Pick<ReturnType<typeof useThreadShells>[number], "environmentId" | "id">;
62 + }
63 | { readonly kind: "draft"; readonly project: EnvironmentProject }
64 | { readonly kind: "scratch" }
65 | { readonly kind: "settled" };
66
67 const openedLinkKeys = new Set<string>();
68
69 +type PullRequestThreadLookup = {
70 + readonly targets: ReadonlyArray<readonly [environmentId: EnvironmentId, projectId: ProjectId]>;
71 + readonly repository: string;
72 + readonly number: number;
73 +};
74 +
75 +// A pull request's thread can be an older settled thread no shell holds, so ask every
76 +// environment with the repository.
77 +const pullRequestThreadLookupAtom = Atom.family((key: string) => {
78 + const { targets, repository, number } = JSON.parse(key) as PullRequestThreadLookup;
79 + return Atom.make((get) =>
80 + pickPromptLinkPullRequestThread(
81 + targets
82 + // A shell that is not live cannot answer, and its lookup would wait forever.
83 + .filter(([environmentId]) => get(environmentShell.statusAtom(environmentId)) === "live")
84 + .map(([environmentId, projectId]) => ({
85 + environmentId,
86 + result: get(
87 + pullRequestEnvironment.linkedThreadsUncached({
88 + environmentId,
89 + input: { projectId, repository, number },
90 + }),
91 + ),
92 + })),
93 + ),
94 + ).pipe(Atom.withLabel(`web-prompt-link-pull-request-thread:${key}`));
95 +});
96 +
97 +const NO_PULL_REQUEST_THREAD_LOOKUP = Atom.make(null);
98 +
99 export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
100 const locationKey = useLocation({ select: (location) => location.state.__TSR_key ?? "" });
101 const navigate = useNavigate();
@@ -218,15 +255,37 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
255 await openDraft(project);
256 };
257
221 - if (ready && state.status === "opening" && !started) {
258 + const localPullRequestThread =
259 + ready && link.kind === "pull-request"
260 + ? findPromptLinkPullRequestThread(threads, link.pullRequest)
261 + : undefined;
262 + // Mounted only while deciding, so "Open it again" asks the server again.
263 + const lookupProjects =
264 + ready &&
265 + state.status === "opening" &&
266 + !started &&
267 + link.kind === "pull-request" &&
268 + localPullRequestThread === undefined
269 + ? findPromptLinkProjects(recentProjects(), link.pullRequest)
270 + : [];
271 + const serverPullRequestThread = useAtomValue(
272 + link.kind === "pull-request" && lookupProjects.length > 0
273 + ? pullRequestThreadLookupAtom(
274 + JSON.stringify({
275 + targets: lookupProjects.map((project) => [project.environmentId, project.id]),
276 + repository: `${link.pullRequest.owner}/${link.pullRequest.name}`,
277 + number: link.pullRequest.number,
278 + } satisfies PullRequestThreadLookup),
279 + )
280 + : NO_PULL_REQUEST_THREAD_LOOKUP,
281 + );
282 +
283 + if (ready && serverPullRequestThread !== undefined && state.status === "opening" && !started) {
284 setStarted(true);
223 - const pullRequestThread =
224 - link.kind === "pull-request"
225 - ? findPromptLinkPullRequestThread(threads, link.pullRequest)
226 - : undefined;
285 + const pullRequestThread = localPullRequestThread ?? serverPullRequestThread ?? undefined;
286 const repository = link.kind === "new" ? link.repository : link.pullRequest;
287 const project =
229 - repository === null ? undefined : findPromptLinkProject(recentProjects(), repository);
288 + repository === null ? undefined : findPromptLinkProjects(recentProjects(), repository)[0];
289 if (pullRequestThread !== undefined) {
290 setState({ status: "opened" });
291 setLaunch({ kind: "thread", thread: pullRequestThread });
apps/web/src/components/pullRequest/PullRequestThreadLinks.tsx
+3
@@ -12,6 +12,7 @@ import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pu
12 import { useProjects, useServerConfigs, useThreadShell, useThreadShells } from "~/state/entities";
13 import { pullRequestEnvironment } from "~/state/pullRequests";
14 import { useEnvironmentQuery } from "~/state/query";
15 +import { useThreadSearch } from "~/state/queries";
16 import { appAtomRegistry } from "~/rpc/atomRegistry";
17 import { openCommandPalette } from "~/commandPaletteBus";
18 import { Button } from "../ui/button";
@@ -191,6 +192,8 @@ function ThreadPicker({
192 const linking = usePullRequestLinking(environmentId);
193 const projects = useProjects();
194 const [query, setQuery] = useState("");
195 + // Mounts older settled threads matching the query, which shells may leave out.
196 + useThreadSearch([environmentId], query, { titlesOnly: true });
197 const projectNames = new Map(
198 projects
199 .filter((project) => project.environmentId === environmentId)
apps/web/src/components/settings/ProjectSettingsPanel.tsx
+2
@@ -48,6 +48,7 @@ import { ProjectActionsSettings } from "./ProjectActionsSettings";
48 import { ProjectDefaultsSettings } from "./ProjectDefaultsSettings";
49 import { projectGroupTitleNeedsUpdate } from "./ProjectSettingsPanel.logic";
50 import { useSettingsProjectGroups } from "./useSettingsProjectGroups";
51 +import { readUnloadedThreadCount } from "../../state/olderSettledShelf";
52 import { settingsScopeEnvironmentLabels } from "./settingsScopeAxis";
53
54 const ProjectIconPickerDialog = lazy(() =>
@@ -321,6 +322,7 @@ function ProjectDetail({
322 hasOtherMembers,
323 displayName: group.displayName,
324 threads,
325 + unloadedThreadCount: readUnloadedThreadCount(members, threads),
326 });
327 const confirmed = await settlePromise(() =>
328 api.dialogs.confirm(confirmationMessage, { variant: "destructive" }),
apps/web/src/hooks/useHandleNewThread.test.ts
+1
@@ -200,6 +200,7 @@ vi.mock("../state/entities", () => {
200 useThread: () => null,
201 };
202 });
203 +vi.mock("../state/olderSettledShelf", () => ({ readUnloadedThreadCount: () => 0 }));
204 vi.mock("../state/presentation", () => ({
205 environmentSummaries: { connectedEnvironmentIdsAtom: "connected-environment-ids" },
206 }));
apps/web/src/hooks/useHandleNewThread.ts
+11 -5
@@ -42,6 +42,7 @@ import { resolveThreadRouteTarget } from "../threadRoutes";
42 import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
43 import { useClientSettings } from "./useSettings";
44 import { useThreadRouteTarget } from "./useThreadRouteTarget";
45 +import { readUnloadedThreadCount } from "../state/olderSettledShelf";
46
47 interface NewThreadWorkspaceOptions {
48 branch?: string | null;
@@ -159,11 +160,16 @@ export function useNewThreadHandler() {
160 ),
161 workspaceRoot: project?.workspaceRoot ?? null,
162 newProjectsRoot: environmentServerConfigs.get(projectRef.environmentId)?.newProjectsRoot,
162 - projectHasThreads: readThreadShells().some(
163 - (thread) =>
164 - thread.environmentId === projectRef.environmentId &&
165 - thread.projectId === projectRef.projectId,
166 - ),
163 + projectHasThreads:
164 + readThreadShells().some(
165 + (thread) =>
166 + thread.environmentId === projectRef.environmentId &&
167 + thread.projectId === projectRef.projectId,
168 + ) ||
169 + readUnloadedThreadCount(
170 + [{ environmentId: projectRef.environmentId, id: projectRef.projectId }],
171 + [],
172 + ) > 0,
173 });
174 };
175 const logicalProjectKey = project
apps/web/src/state/olderSettledShelf.ts new
+120
@@ -0,0 +1,120 @@
1 +import { useAtomValue } from "@effect/atom-react";
2 +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
3 +import {
4 + isListedSettledThread,
5 + settledShelfFrontier,
6 + unloadedOlderSettledCount,
7 + wantedOlderSettledPages,
8 + type OlderSettledScope,
9 +} from "@t3tools/client-runtime/state/threads";
10 +import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
11 +import { useEffect, useState } from "react";
12 +
13 +import { appAtomRegistry } from "../rpc/atomRegistry";
14 +import { olderSettledThreads, threadFamilies } from "./threads";
15 +
16 +const NO_PAGES: ReadonlyMap<EnvironmentId, number> = new Map();
17 +
18 +/**
19 + * The Settled shelf for one scope when shells leave older settled threads out: which rows it may
20 + * list, how many threads it holds in all, and the pages read below each environment's cut, whose
21 + * rows merge into the thread list. `useOlderSettledShelfGrowth` reads the next pages.
22 + */
23 +export function useOlderSettledShelf(input: {
24 + readonly scopes: ReadonlyArray<OlderSettledScope>;
25 + /** A collapsed shelf mounts no pages, so a reconnect reads none; expanding restores them. */
26 + readonly expanded: boolean;
27 + /** Rows the shelf shows; pages are read until it lists that many. */
28 + readonly visibleCount: number;
29 +}) {
30 + const scopeKey = JSON.stringify(input.scopes);
31 + const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });
32 + const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;
33 + const shelfScopes = input.scopes.map((scope) => ({
34 + scope,
35 + pages: pages.get(scope.environmentId) ?? 0,
36 + }));
37 + const shelfKey = JSON.stringify(shelfScopes);
38 + const shelf = useAtomValue(olderSettledThreads.shelfAtom(shelfScopes));
39 + useEffect(() => {
40 + const releases = (JSON.parse(shelfKey) as typeof shelfScopes).flatMap(({ scope, pages }) =>
41 + pages === 0
42 + ? []
43 + : [
44 + threadFamilies.mountRows(
45 + appAtomRegistry,
46 + scope.environmentId,
47 + olderSettledThreads.rowsKey(scope, pages),
48 + olderSettledThreads.pageRowsAtom(scope, pages),
49 + ),
50 + ],
51 + );
52 + return () => {
53 + for (const release of releases) release();
54 + };
55 + }, [shelfKey]);
56 +
57 + const olderOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.older ?? null;
58 + const pagesOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.pages;
59 + const frontier = settledShelfFrontier([...shelf.values()]);
60 + const loaded = shelfScopes.filter(({ pages }) => pages > 0);
61 + return {
62 + environments: shelf,
63 + visibleCount: input.expanded ? input.visibleCount : 0,
64 + pages,
65 + grow: (next: ReadonlyMap<EnvironmentId, number>) => setRequested({ scopeKey, pages: next }),
66 + isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
67 + /** Older settled threads in scope that are not local; add it to the local Settled rows. */
68 + unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
69 + unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),
70 + loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
71 + failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
72 + retry: () => {
73 + for (const { scope, pages } of loaded) {
74 + olderSettledThreads.retryPages(appAtomRegistry, scope, pages);
75 + }
76 + },
77 + };
78 +}
79 +
80 +/** Reads the next pages while the shelf lists fewer rows than it shows. */
81 +export function useOlderSettledShelfGrowth(
82 + shelf: ReturnType<typeof useOlderSettledShelf>,
83 + listedSettledThreads: ReadonlyArray<EnvironmentThreadShell>,
84 +) {
85 + const wanted = wantedOlderSettledPages({
86 + requested: shelf.pages,
87 + environments: shelf.environments,
88 + listedCount: listedSettledThreads.length,
89 + visibleCount: shelf.visibleCount,
90 + });
91 + const { grow, pages } = shelf;
92 + useEffect(() => {
93 + if (wanted !== pages) grow(wanted);
94 + }, [wanted, pages, grow]);
95 +}
96 +
97 +/**
98 + * Threads of these projects that this client does not hold (older settled threads), for counts
99 + * shown outside the shelf, such as a removal confirm.
100 + */
101 +export function readUnloadedThreadCount(
102 + members: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ProjectId }>,
103 + threads: ReadonlyArray<EnvironmentThreadShell>,
104 +): number {
105 + return unloadedOlderSettledCount({
106 + olderSettled: (environmentId) =>
107 + appAtomRegistry.get(olderSettledThreads.olderSettledAtom(environmentId)),
108 + scopes: [...new Set(members.map((member) => member.environmentId))].map((environmentId) => ({
109 + environmentId,
110 + projectIds: members.flatMap((member) =>
111 + member.environmentId === environmentId ? [member.id] : [],
112 + ),
113 + })),
114 + threads: threads.filter((thread) =>
115 + members.some(
116 + (member) => member.environmentId === thread.environmentId && member.id === thread.projectId,
117 + ),
118 + ),
119 + });
120 +}
apps/web/src/state/queries.ts
+58 -13
@@ -9,6 +9,8 @@ import {
9 visibleThreadSearch,
10 type EnvironmentThreadSearchMatch,
11 type SettledThreadSearch,
12 + type ThreadSearchResultsState,
13 + type ThreadSearchScope,
14 } from "@t3tools/client-runtime/state/thread-search";
15 import { THREAD_SEARCH_QUERY_MAX_LENGTH } from "@t3tools/contracts";
16 import { messageSearchTokens } from "@t3tools/shared/paletteSearch";
@@ -32,6 +34,7 @@ import { useEffect, useState } from "react";
34 import { appAtomRegistry } from "../rpc/atomRegistry";
35 import { useFilesystemReadAccess } from "./filesystem";
36 import { orchestrationEnvironment } from "./orchestration";
37 +import { olderSettledThreads, threadFamilies } from "./threads";
38 import { isPaginatedBranchesNextPagePending } from "./paginatedBranches";
39 import { projectContentSearch, projectEnvironment } from "./projects";
40 import { useEnvironmentQuery } from "./query";
@@ -48,17 +51,31 @@ const EMPTY_PROJECT_ENTRIES: ReadonlyArray<ProjectEntry> = [];
51 const EMPTY_CONTENT_MATCHES: ReadonlyArray<ProjectContentMatch> = [];
52 const INITIAL_BRANCH_CURSORS = [undefined] as const;
53 const EMPTY_THREAD_SEARCH_MATCHES: ReadonlyArray<EnvironmentThreadSearchMatch> = Object.freeze([]);
51 -const EMPTY_THREAD_SEARCH_ATOM = Atom.make({
54 +const EMPTY_THREAD_SEARCH_ATOM = Atom.make<ThreadSearchResultsState>({
55 matches: EMPTY_THREAD_SEARCH_MATCHES,
56 isLoading: false,
57 + rows: [],
58 }).pipe(Atom.withLabel("web:thread-search:empty"));
59
56 -const threadSearchResultsAtom = createThreadSearchResultsAtomFamily({
57 - getSearchAtom: (environmentId, query) =>
58 - orchestrationEnvironment.threadSearch({
59 - environmentId,
60 - input: { query },
61 - }),
60 +const olderSettledBeforeAtom = Atom.family((environmentId: EnvironmentId) =>
61 + Atom.make((get) => get(olderSettledThreads.olderSettledAtom(environmentId))?.before ?? null).pipe(
62 + Atom.withLabel(`web:older-settled-before:${environmentId}`),
63 + ),
64 +);
65 +// Whether any of these environments leaves older settled threads out, so short queries matter.
66 +const anyOlderSettledAtom = Atom.family((environmentIdsKey: string) =>
67 + Atom.make((get) =>
68 + (JSON.parse(environmentIdsKey) as ReadonlyArray<EnvironmentId>).some(
69 + (environmentId) => get(olderSettledBeforeAtom(environmentId)) !== null,
70 + ),
71 + ).pipe(Atom.withLabel(`web:any-older-settled:${environmentIdsKey}`)),
72 +);
73 +const threadSearch = createThreadSearchResultsAtomFamily({
74 + getSearchAtom: (environmentId, input) =>
75 + input.olderSettledBefore === undefined
76 + ? orchestrationEnvironment.threadSearch({ environmentId, input })
77 + : olderSettledThreads.searchAtom(environmentId, input),
78 + olderSettledBefore: olderSettledBeforeAtom,
79 labelPrefix: "web:thread-search",
80 });
81
@@ -77,9 +94,15 @@ export function useDebouncedValue<A>(value: A, delayMs: number): A {
94 return debounced;
95 }
96
97 +/**
98 + * Server matches for a thread search: message hits, and on environments that leave older settled
99 + * threads out of the shell, the rows of the best older matches, mounted into the thread list while
100 + * they show so the caller's own ranking finds them.
101 + */
102 export function useThreadSearch(
103 environmentIds: ReadonlyArray<EnvironmentId>,
104 query: string,
105 + scope: ThreadSearchScope = {},
106 ): {
107 readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
108 readonly query: string;
@@ -88,20 +111,25 @@ export function useThreadSearch(
111 // A pasted wall of text searches by its start rather than failing the request.
112 const normalizedQuery = query.trim().slice(0, THREAD_SEARCH_QUERY_MAX_LENGTH).trim();
113 const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
91 - // The server needs a word of three or more letters; shorter queries would always come back empty.
114 + const anyOlderSettled = useAtomValue(anyOlderSettledAtom(JSON.stringify(environmentIds)));
115 + // Messages need a word of three or more letters; older settled titles match from two.
116 const canSearch =
93 - environmentIds.length > 0 && messageSearchTokens(normalizedQuery).long.length > 0;
117 + environmentIds.length > 0 &&
118 + ((scope.titlesOnly !== true && messageSearchTokens(normalizedQuery).long.length > 0) ||
119 + (anyOlderSettled && normalizedQuery.length >= 2));
120 const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
95 - const searchKey =
96 - settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery);
121 + const searchKeyOf = (searchQuery: string) =>
122 + makeThreadSearchKey(environmentIds, searchQuery, scope);
123 + const searchKey = settledQuery === null ? null : searchKeyOf(settledQuery);
124 const result = useAtomValue(
98 - searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
125 + searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearch.results(searchKey),
126 );
127 const isPending = canSearch && (normalizedQuery !== debouncedQuery || result.isLoading);
128 // What fast environments returned shows without waiting for a slow one;
129 // callers keep shown rows in place with `nextShownOrder`.
130 const ready =
104 - settledQuery !== null && (!result.isLoading || result.matches.length > 0)
131 + settledQuery !== null &&
132 + (!result.isLoading || result.matches.length > 0 || result.rows.length > 0)
133 ? { query: settledQuery, matches: result.matches }
134 : null;
135 // Kept from earlier renders: React's pattern for information from previous renders.
@@ -110,6 +138,23 @@ export function useThreadSearch(
138 const visible = canSearch
139 ? visibleThreadSearch(normalizedQuery, ready, previous)
140 : { query: "", matches: EMPTY_THREAD_SEARCH_MATCHES };
141 + // The shown results' rows stay mounted until newer results replace them.
142 + const shownKey = visible.query === "" ? null : searchKeyOf(visible.query);
143 + useEffect(() => {
144 + if (shownKey === null) return;
145 + const environments = (JSON.parse(shownKey) as [ReadonlyArray<EnvironmentId>])[0];
146 + const releases = environments.map((environmentId) =>
147 + threadFamilies.mountRows(
148 + appAtomRegistry,
149 + environmentId,
150 + `search:${shownKey}`,
151 + threadSearch.rows(shownKey, environmentId),
152 + ),
153 + );
154 + return () => {
155 + for (const release of releases) release();
156 + };
157 + }, [shownKey]);
158 return {
159 matches: visible.matches,
160 query: visible.query,
apps/web/src/state/threads.ts
+9 -1
@@ -7,6 +7,7 @@ import {
7 createEnvironmentThreadStateAtoms,
8 EMPTY_ENVIRONMENT_THREAD_STATE,
9 type EnvironmentThreadState,
10 + createOlderSettledThreadAtoms,
11 createThreadEnvironmentAtoms,
12 createThreadFamilyAtoms,
13 } from "@t3tools/client-runtime/state/threads";
@@ -20,7 +21,14 @@ import { connectionAtomRuntime } from "../connection/runtime";
21 import { appAtomRegistry } from "../rpc/atomRegistry";
22 import { environmentShell } from "./shell";
23
23 -const threadFamilies = createThreadFamilyAtoms(connectionAtomRuntime, environmentShell.stateAtom);
24 +export const threadFamilies = createThreadFamilyAtoms(
25 + connectionAtomRuntime,
26 + environmentShell.stateAtom,
27 +);
28 +export const olderSettledThreads = createOlderSettledThreadAtoms(
29 + connectionAtomRuntime,
30 + environmentShell.stateAtom,
31 +);
32 export const threadEnvironment = createThreadEnvironmentAtoms(
33 connectionAtomRuntime,
34 threadFamilies.snapshotAtom,
docs/internals/connection-runtime.md
+28 -8
@@ -59,14 +59,34 @@ connection nor overwrite newer live data during a reconnect. Loading and
59 resuming snapshots belongs to the shared state services, so every view agrees
60 on which data is current.
61
62 -Servers that advertise `shellThreadFamilies` leave idle subagent children out of
63 -shell snapshots when the client asks. A child stays while it has a live or
64 -queued run, a pending request or background work, or a subagent child of its
65 -own, so badge and working walks never meet a gap. Other child rows come from the
66 -[thread family](../../packages/client-runtime/src/state/threadFamilies.ts) that
67 -a thread view or a queued message mounts. A missing shell row therefore does not
68 -prove a thread is gone: code that deletes data or a worktree on absence must
69 -check the family or the server first.
62 +Shell snapshots can leave rows out when the client asks. Servers that advertise
63 +`shellThreadFamilies` leave out idle subagent children: a child stays while it
64 +has a live or queued run, a pending request or background work, or a subagent
65 +child of its own, so badge and working walks never meet a gap. Servers that also
66 +advertise `shellOlderSettled` keep only the 25 newest settled top-level threads
67 +(and any tied with the 25th, pinned, snoozed, live, or above a kept child); the
68 +snapshot's `olderSettled` names that cut and counts the rest per project. Left-out
69 +rows load on demand and merge behind shell rows
70 +([thread families](../../packages/client-runtime/src/state/threadFamilies.ts)):
71 +the family a thread view or a queued message mounts holds every active thread
72 +sharing the thread's root, so it also brings back an old settled thread opened by
73 +link; the Settled shelf reads pages below the cut; searches return the rows of
74 +older matches. A missing shell row therefore does not prove a thread is gone:
75 +code that deletes data or a worktree on absence must check the family or the
76 +server first.
77 +
78 +The Settled count is local Settled rows, minus local rows below the cut, plus the
79 +server's counts
80 +([older settled threads](../../packages/client-runtime/src/state/olderSettledThreads.ts)).
81 +The counts are re-read when a removal or an old row's arrival or change could
82 +move them, so archiving any thread costs one small read. A shelf lists an older
83 +row only above the highest page frontier of the environments that can still
84 +load, so the merged list stays a correct prefix while a page loads. An
85 +environment with no snapshot this session, or a failed page, holds no one back.
86 +On-demand reads wait for the session's first authoritative snapshot
87 +(`generation > 0`), and each is keyed by generation. A resume that skips that
88 +snapshot must still bump `generation` once verified, and may reuse a cached
89 +snapshot only when its omissions match what the session asks for.
90
91 [Thread detail](../../packages/client-runtime/src/state/threads.ts) separates
92 subscription lifetime from cache lifetime. Mounted consumers share one live
packages/client-runtime/src/promptLinks.test.ts
+55 -7
@@ -1,11 +1,20 @@
1 import {
2 + EnvironmentId,
3 + ProjectId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 + ThreadId,
7 + type PullRequestLinkedThreadsResult,
8 type RepositoryIdentity,
9 } from "@t3tools/contracts";
10 +import { AsyncResult } from "effect/reactivity";
11 import { describe, expect, it } from "vite-plus/test";
12
8 -import { findPromptLinkProject, resolvePromptLinkModelSelection } from "./promptLinks.ts";
13 +import {
14 + findPromptLinkProjects,
15 + pickPromptLinkPullRequestThread,
16 + resolvePromptLinkModelSelection,
17 +} from "./promptLinks.ts";
18
19 const identity = (canonicalKey: string, origin?: string): RepositoryIdentity => ({
20 canonicalKey,
@@ -13,8 +22,8 @@ const identity = (canonicalKey: string, origin?: string): RepositoryIdentity =>
22 ...(origin ? { origin: { canonicalKey: origin } } : {}),
23 });
24
16 -describe("findPromptLinkProject", () => {
17 - it("takes the first exact match and never a fork of the repository", () => {
25 +describe("findPromptLinkProjects", () => {
26 + it("keeps exact matches in order and never a fork of the repository", () => {
27 const projects = [
28 {
29 id: "fork",
@@ -24,11 +33,50 @@ describe("findPromptLinkProject", () => {
33 { id: "recent", repositoryIdentity: identity("github.com/PingDotGG/T3Code") },
34 { id: "older", repositoryIdentity: identity("github.com/pingdotgg/t3code") },
35 ];
27 - expect(findPromptLinkProject(projects, { owner: "pingdotgg", name: "t3code" })?.id).toBe(
28 - "recent",
36 + const ids = (owner: string, name: string) =>
37 + findPromptLinkProjects(projects, { owner, name }).map((project) => project.id);
38 + expect(ids("pingdotgg", "t3code")).toEqual(["recent", "older"]);
39 + expect(ids("me", "t3code")).toEqual(["fork"]);
40 + expect(ids("gitlab", "x")).toEqual([]);
41 + });
42 +});
43 +
44 +describe("pickPromptLinkPullRequestThread", () => {
45 + it("takes the first open thread in order once every earlier lookup has answered", () => {
46 + const laptop = EnvironmentId.make("laptop");
47 + const server = EnvironmentId.make("server");
48 + const linked = (
49 + id: string,
50 + archivedAt: string | null = null,
51 + ): PullRequestLinkedThreadsResult => ({
52 + threads: [{ id: ThreadId.make(id), projectId: ProjectId.make("p"), title: id, archivedAt }],
53 + });
54 + const none: PullRequestLinkedThreadsResult = { threads: [] };
55 + const pick = (
56 + ...results: ReadonlyArray<AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>>
57 + ) =>
58 + pickPromptLinkPullRequestThread(
59 + results.map((result, index) => ({ environmentId: index === 0 ? laptop : server, result })),
60 + );
61 + // A value shown while it refetches is not an answer yet.
62 + expect(pick(AsyncResult.success(none, { waiting: true }))).toBeUndefined();
63 + expect(pick(AsyncResult.success(none), AsyncResult.initial(true))).toBeUndefined();
64 + expect(pick(AsyncResult.initial(true), AsyncResult.success(linked("b")))).toBeUndefined();
65 + const laptopThread = { environmentId: laptop, id: ThreadId.make("a") };
66 + expect(pick(AsyncResult.success(linked("a")), AsyncResult.initial(true))).toEqual(laptopThread);
67 + expect(pick(AsyncResult.success(linked("a")), AsyncResult.success(linked("b")))).toEqual(
68 + laptopThread,
69 );
30 - expect(findPromptLinkProject(projects, { owner: "me", name: "t3code" })?.id).toBe("fork");
31 - expect(findPromptLinkProject(projects, { owner: "gitlab", name: "x" })).toBeUndefined();
70 + expect(pick(AsyncResult.success(none), AsyncResult.success(linked("old")))).toEqual({
71 + environmentId: server,
72 + id: ThreadId.make("old"),
73 + });
74 + expect(
75 + pick(
76 + AsyncResult.success(linked("archived", "2026-01-01T00:00:00.000Z")),
77 + AsyncResult.fail(1),
78 + ),
79 + ).toBeNull();
80 });
81 });
82
packages/client-runtime/src/promptLinks.ts
+30 -4
@@ -1,12 +1,16 @@
1 import {
2 repositoryGroupingKeyOf,
3 + type EnvironmentId,
4 type ModelSelection,
5 + type PullRequestLinkedThreadsResult,
6 type RepositoryIdentity,
7 type ServerProvider,
8 type ThreadLinkedPullRequest,
9 + type ThreadId,
10 type ThreadPullRequestLink,
11 } from "@t3tools/contracts";
12 import { resolveSelectableModel, type SelectableModelOption } from "@t3tools/shared/model";
13 +import type { AsyncResult } from "effect/reactivity";
14 import type { PromptLinkPullRequest, PromptLinkRepository } from "@t3tools/shared/promptLinks";
15 import {
16 threadPullRequestKeyOf,
@@ -18,13 +22,14 @@ const repositoryKey = (repository: PromptLinkRepository) =>
22 `github.com/${repository.owner}/${repository.name}`.toLowerCase();
23
24 /**
21 - * Callers pass projects most recently active first, so the user lands where they last worked.
25 + * The repository's projects in the order given. Callers pass projects most recently active
26 + * first and open the first, so the user lands where they last worked.
27 */
23 -export function findPromptLinkProject<
28 +export function findPromptLinkProjects<
29 P extends { readonly repositoryIdentity?: RepositoryIdentity | null | undefined },
25 ->(projects: ReadonlyArray<P>, repository: PromptLinkRepository): P | undefined {
30 +>(projects: ReadonlyArray<P>, repository: PromptLinkRepository): P[] {
31 const key = repositoryKey(repository);
27 - return projects.find(
32 + return projects.filter(
33 (project) =>
34 project.repositoryIdentity != null &&
35 repositoryGroupingKeyOf(project.repositoryIdentity).toLowerCase() === key,
@@ -54,6 +59,27 @@ export function findPromptLinkPullRequestThread<
59 return latest;
60 }
61
62 +/**
63 + * The first open thread from per-environment pull request lookups, in the order given, once
64 + * every lookup before it has answered. `null` when none has one; `undefined` until then.
65 + */
66 +export function pickPromptLinkPullRequestThread(
67 + lookups: ReadonlyArray<{
68 + readonly environmentId: EnvironmentId;
69 + readonly result: AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>;
70 + }>,
71 +): { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null | undefined {
72 + for (const { environmentId, result } of lookups) {
73 + if (result._tag === "Initial" || result.waiting) return undefined;
74 + const thread =
75 + result._tag === "Success"
76 + ? result.value.threads.find((candidate) => candidate.archivedAt === null)
77 + : undefined;
78 + if (thread !== undefined) return { environmentId, id: thread.id };
79 + }
80 + return null;
81 +}
82 +
83 const PROVIDER_ALIASES: Record<string, string> = { claude: "claudeagent" };
84
85 /**
packages/client-runtime/src/state/olderSettledThreads.test.ts new
+566
@@ -0,0 +1,566 @@
1 +import {
2 + EnvironmentId,
3 + ORCHESTRATION_V2_WS_METHODS,
4 + OrchestrationV2GetShellSnapshotError,
5 + ProjectId,
6 + ThreadId,
7 + type OrchestrationV2OlderSettledThreadsPage,
8 + type OrchestrationV2SearchThreadsResult,
9 + type OrchestrationV2ShellSnapshot,
10 + type OrchestrationV2ThreadShell,
11 +} from "@t3tools/contracts";
12 +import { describe, expect, it } from "@effect/vitest";
13 +import * as Clock from "effect/Clock";
14 +import * as DateTime from "effect/DateTime";
15 +import * as Deferred from "effect/Deferred";
16 +import * as Effect from "effect/Effect";
17 +import * as Layer from "effect/Layer";
18 +import * as Option from "effect/Option";
19 +import * as Queue from "effect/Queue";
20 +import * as Stream from "effect/Stream";
21 +import * as SubscriptionRef from "effect/SubscriptionRef";
22 +import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
23 +import * as TestClock from "effect/testing/TestClock";
24 +
25 +import {
26 + AVAILABLE_CONNECTION_STATE,
27 + PrimaryConnectionTarget,
28 + type PreparedConnection,
29 + type SupervisorConnectionState,
30 +} from "../connection/model.ts";
31 +import * as EnvironmentRegistry from "../connection/registry.ts";
32 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
33 +import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
34 +import type * as RpcSession from "../rpc/session.ts";
35 +import { EMPTY_SHELL_STATE, type EnvironmentShellState } from "./shell.ts";
36 +import {
37 + createOlderSettledThreadAtoms,
38 + isListedSettledThread,
39 + olderSettledCountKey,
40 + settledShelfFrontier,
41 + unloadedOlderSettledCount,
42 + wantedOlderSettledPages,
43 + type OlderSettled,
44 + type OlderSettledPages,
45 +} from "./olderSettledThreads.ts";
46 +
47 +const DAY = 24 * 60 * 60 * 1000;
48 +const environmentId = EnvironmentId.make("env-a");
49 +const project = ProjectId.make("project-a");
50 +const before = DateTime.formatIso(DateTime.makeUnsafe(50 * DAY));
51 +const older: OlderSettled = {
52 + before,
53 + beforeMs: 50 * DAY,
54 + counts: new Map([[project, 3]]),
55 +};
56 +
57 +const row = (
58 + id: string,
59 + input: {
60 + readonly settledDay?: number;
61 + readonly createdDay?: number;
62 + readonly subagent?: boolean;
63 + },
64 +) =>
65 + ({
66 + id: ThreadId.make(id),
67 + projectId: project,
68 + lineage: {
69 + parentThreadId: input.subagent === true ? ThreadId.make("parent") : null,
70 + relationshipToParent: input.subagent === true ? "subagent" : null,
71 + rootThreadId: ThreadId.make(id),
72 + },
73 + settledOverride: input.settledDay === undefined ? null : "settled",
74 + settledAt: input.settledDay === undefined ? null : DateTime.makeUnsafe(input.settledDay * DAY),
75 + createdAt: DateTime.makeUnsafe((input.createdDay ?? 1) * DAY),
76 + }) as unknown as OrchestrationV2ThreadShell;
77 +
78 +describe("olderSettledCountKey", () => {
79 + const key = (
80 + threads: ReadonlyArray<OrchestrationV2ThreadShell>,
81 + removedAt: ReadonlyMap<ThreadId, number> = new Map(),
82 + ) => olderSettledCountKey(threads, older.beforeMs, removedAt);
83 + const base = [row("kept-old", { settledDay: 10 }), row("open", { createdDay: 60 })];
84 +
85 + it("changes on every event that can change the older count", () => {
86 + const start = key(base);
87 + // A removal, even of an id removed before (the map keeps its size).
88 + expect(key(base, new Map([[ThreadId.make("x"), 4]]))).not.toBe(start);
89 + expect(key(base, new Map([[ThreadId.make("x"), 9]]))).not.toBe(
90 + key(base, new Map([[ThreadId.make("x"), 4]])),
91 + );
92 + // An omitted thread that arrives unsettled.
93 + expect(key([...base, row("woken", { createdDay: 5 })])).not.toBe(start);
94 + // A local row that turns older (a backdated auto-settle).
95 + expect(key([base[0]!, row("open", { createdDay: 60, settledDay: 20 })])).not.toBe(start);
96 + });
97 +
98 + it("stays put for new threads and subagents", () => {
99 + const start = key(base);
100 + expect(key([...base, row("new", { createdDay: 70 })])).toBe(start);
101 + expect(key([...base, row("new-settled", { createdDay: 70, settledDay: 71 })])).toBe(start);
102 + expect(key([...base, row("child", { createdDay: 5, subagent: true })])).toBe(start);
103 + });
104 +});
105 +
106 +describe("settled shelf listing", () => {
107 + const pages = (frontier: OlderSettledPages["frontier"]): OlderSettledPages => ({
108 + reads: [],
109 + frontier,
110 + status: "ready",
111 + });
112 + const at = (day: number) => DateTime.formatIso(DateTime.makeUnsafe(day * DAY));
113 +
114 + it("lists rows at or above the highest frontier still loading", () => {
115 + // No page yet: the cut is the frontier, so only rows newer than it list.
116 + const one = settledShelfFrontier([{ older, pages: pages({ settledAt: before }) }]);
117 + expect(isListedSettledThread(row("recent", { settledDay: 60 }), one)).toBe(true);
118 + expect(isListedSettledThread(row("old", { settledDay: 30 }), one)).toBe(false);
119 + // A row that became older inside the loaded range lists at its place.
120 + const loaded = settledShelfFrontier([{ older, pages: pages({ settledAt: at(20) }) }]);
121 + expect(isListedSettledThread(row("old", { settledDay: 30 }), loaded)).toBe(true);
122 + expect(isListedSettledThread(row("older", { settledDay: 10 }), loaded)).toBe(false);
123 + // All loaded, or a shell that keeps every settled thread: everything lists.
124 + expect(settledShelfFrontier([{ older, pages: pages(null) }])).toBeNull();
125 + expect(settledShelfFrontier([{ older: null, pages: pages(null) }])).toBeNull();
126 + });
127 +
128 + it("holds back another environment's rows below an environment still loading", () => {
129 + const frontier = settledShelfFrontier([
130 + { older, pages: pages({ settledAt: at(40) }) },
131 + { older, pages: pages({ settledAt: at(20) }) },
132 + ]);
133 + expect(frontier).toEqual({ settledAt: at(40) });
134 + expect(isListedSettledThread(row("b-loaded", { settledDay: 30 }), frontier)).toBe(false);
135 + // A failed environment holds no one back.
136 + expect(
137 + settledShelfFrontier([
138 + { older, pages: { ...pages({ settledAt: at(40) }), status: "failed" } },
139 + { older, pages: pages({ settledAt: at(20) }) },
140 + ]),
141 + ).toEqual({ settledAt: at(20) });
142 + });
143 +
144 + it("on a settle-time tie, stops at the cursor that admits fewest rows", () => {
145 + const strict = pages({ settledAt: at(30) });
146 + const afterM = pages({ settledAt: at(30), threadId: ThreadId.make("m") });
147 + const afterK = pages({ settledAt: at(30), threadId: ThreadId.make("k") });
148 + const frontier = (...all: ReadonlyArray<OlderSettledPages>) =>
149 + settledShelfFrontier(all.map((each) => ({ older, pages: each })));
150 + expect(frontier(strict, afterM)).toEqual(afterM.frontier);
151 + expect(frontier(afterM, strict)).toEqual(afterM.frontier);
152 + expect(frontier(afterM, afterK)).toEqual(afterK.frontier);
153 + });
154 +
155 + it("follows the server's id order on a settle-time tie", () => {
156 + const frontier = { settledAt: at(30), threadId: ThreadId.make("m") };
157 + expect(isListedSettledThread(row("a", { settledDay: 30 }), frontier)).toBe(true);
158 + expect(isListedSettledThread(row("z", { settledDay: 30 }), frontier)).toBe(false);
159 + });
160 +});
161 +
162 +it("counts older threads the client does not hold, once", () => {
163 + const local = [
164 + // Kept by the snapshot (snoozed) and loaded by a page: the server counts both.
165 + { source: row("snoozed-old", { settledDay: 10 }), environmentId },
166 + { source: row("paged-old", { settledDay: 20 }), environmentId },
167 + { source: row("recent", { settledDay: 60 }), environmentId },
168 + ];
169 + const olderSettled = () => older;
170 + expect(
171 + unloadedOlderSettledCount({
172 + olderSettled,
173 + scopes: [{ environmentId, projectIds: null }],
174 + threads: local,
175 + }),
176 + ).toBe(1);
177 + expect(
178 + unloadedOlderSettledCount({
179 + olderSettled,
180 + scopes: [{ environmentId, projectIds: [ProjectId.make("other")] }],
181 + threads: local,
182 + }),
183 + ).toBe(0);
184 + // Two projects in one environment: each subtracts only its own loaded rows.
185 + const other = ProjectId.make("project-b");
186 + const twoProjects = (): OlderSettled => ({
187 + ...older,
188 + counts: new Map([
189 + [project, 10],
190 + [other, 10],
191 + ]),
192 + });
193 + expect(
194 + unloadedOlderSettledCount({
195 + olderSettled: twoProjects,
196 + scopes: [{ environmentId, projectIds: [project, other] }],
197 + threads: [
198 + { source: row("a-old", { settledDay: 10 }), environmentId },
199 + { source: { ...row("b-old", { settledDay: 10 }), projectId: other }, environmentId },
200 + ],
201 + }),
202 + ).toBe(18);
203 +});
204 +
205 +describe("wantedOlderSettledPages", () => {
206 + const otherEnvironment = EnvironmentId.make("env-b");
207 + const full = (settledAt: string): OlderSettledPages => ({
208 + reads: [],
209 + frontier: { settledAt, threadId: ThreadId.make("t") },
210 + status: "ready",
211 + });
212 + const wanted = (input: {
213 + readonly requested?: ReadonlyMap<EnvironmentId, number>;
214 + readonly environments: ReadonlyMap<EnvironmentId, { readonly pages: OlderSettledPages }>;
215 + readonly listed: number;
216 + readonly visible: number;
217 + }) =>
218 + wantedOlderSettledPages({
219 + requested: input.requested ?? new Map(),
220 + environments: new Map(
221 + [...input.environments].map(([id, { pages }]) => [id, { older, pages }] as const),
222 + ),
223 + listedCount: input.listed,
224 + visibleCount: input.visible,
225 + });
226 +
227 + it("reads the next page of the environment holding the shelf back", () => {
228 + const environments = new Map([
229 + [environmentId, { pages: full("2026-01-10T00:00:00.000Z") }],
230 + [otherEnvironment, { pages: full("2026-01-20T00:00:00.000Z") }],
231 + ]);
232 + expect(wanted({ environments, listed: 9, visible: 10 })).toEqual(
233 + new Map([[otherEnvironment, 1]]),
234 + );
235 + // A deleted row drops the listed count again.
236 + expect(
237 + wanted({ requested: new Map([[environmentId, 1]]), environments, listed: 34, visible: 35 }),
238 + ).toEqual(
239 + new Map([
240 + [environmentId, 1],
241 + [otherEnvironment, 1],
242 + ]),
243 + );
244 + });
245 +
246 + it("waits for pending pages and stops at a short page or a closed shelf", () => {
247 + const one = (pages: OlderSettledPages) => new Map([[environmentId, { pages }]]);
248 + const pending = { ...full(before), status: "pending" as const };
249 + const requested = new Map([[environmentId, 1]]);
250 + expect(wanted({ requested, environments: one(pending), listed: 0, visible: 10 })).toBe(
251 + requested,
252 + );
253 + const done = { ...full(before), frontier: null };
254 + expect(wanted({ requested, environments: one(done), listed: 3, visible: 10 })).toBe(requested);
255 + expect(wanted({ requested, environments: one(full(before)), listed: 10, visible: 10 })).toBe(
256 + requested,
257 + );
258 + expect(wanted({ requested, environments: one(full(before)), listed: 0, visible: 0 })).toBe(
259 + requested,
260 + );
261 + });
262 +});
263 +
264 +const TARGET = new PrimaryConnectionTarget({
265 + environmentId,
266 + label: "Older settled environment",
267 + httpBaseUrl: "https://older.example.test",
268 + wsBaseUrl: "wss://older.example.test",
269 +});
270 +
271 +// The atoms over a fake connected environment whose `getOlderSettledThreads` and `searchThreads`
272 +// take their replies, in order, from `replies`.
273 +const makeAtoms = Effect.fn(function* () {
274 + const replies =
275 + yield* Queue.unbounded<
276 + Effect.Effect<
277 + OrchestrationV2OlderSettledThreadsPage | OrchestrationV2SearchThreadsResult,
278 + OrchestrationV2GetShellSnapshotError
279 + >
280 + >();
281 + const requests: unknown[] = [];
282 + // Each request, with a signal for when its read is stopped.
283 + const requestLog = yield* Queue.unbounded<{
284 + readonly input: unknown;
285 + readonly stopped: Deferred.Deferred<void>;
286 + }>();
287 + const reply = (input: unknown) =>
288 + Effect.gen(function* () {
289 + requests.push(input);
290 + const stopped = yield* Deferred.make<void>();
291 + yield* Queue.offer(requestLog, { input, stopped });
292 + return yield* Effect.flatten(Queue.take(replies)).pipe(
293 + Effect.onInterrupt(() => Deferred.succeed(stopped, undefined)),
294 + );
295 + });
296 + const client = {
297 + [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: reply,
298 + [ORCHESTRATION_V2_WS_METHODS.searchThreads]: reply,
299 + } as unknown as WsRpcProtocolClient;
300 + const session = {
301 + client,
302 + initialConfig: Effect.succeed({} as never),
303 + ready: Effect.void,
304 + probe: Effect.void,
305 + closed: Effect.never,
306 + } as unknown as RpcSession.RpcSession;
307 + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
308 + target: TARGET,
309 + state: yield* SubscriptionRef.make<SupervisorConnectionState>({
310 + ...AVAILABLE_CONNECTION_STATE,
311 + desired: true,
312 + network: "online",
313 + phase: "connected",
314 + attempt: 1,
315 + generation: 1,
316 + }),
317 + session: yield* SubscriptionRef.make(Option.some(session)),
318 + prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
319 + connect: Effect.void,
320 + disconnect: Effect.void,
321 + retryNow: Effect.void,
322 + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
323 + const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (_environmentId, effect) =>
324 + Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
325 + const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = (
326 + _environmentId,
327 + stream,
328 + ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
329 + const runtime = Atom.runtime(
330 + Layer.mergeAll(
331 + Layer.succeed(
332 + EnvironmentRegistry.EnvironmentRegistry,
333 + EnvironmentRegistry.EnvironmentRegistry.of({
334 + run,
335 + followStream,
336 + } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]),
337 + ),
338 + // The test's clock, so the count re-read's wait moves with `TestClock.adjust`.
339 + Layer.succeed(Clock.Clock, yield* Clock.Clock),
340 + ),
341 + );
342 + const shellState = Atom.make<AsyncResult.AsyncResult<EnvironmentShellState>>(
343 + AsyncResult.success(EMPTY_SHELL_STATE),
344 + );
345 + const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
346 + Effect.sync(() => registry.dispose()),
347 + );
348 + return {
349 + replies,
350 + requests,
351 + requestLog,
352 + atoms: createOlderSettledThreadAtoms(runtime, () => shellState),
353 + registry,
354 + setShell: (state: Partial<EnvironmentShellState>) =>
355 + registry.set(shellState, AsyncResult.success({ ...EMPTY_SHELL_STATE, ...state })),
356 + // The first value that passes, from a subscription.
357 + until: <A>(atom: Atom.Atom<A>, done: (value: A) => boolean) =>
358 + AtomRegistry.toStream(registry, atom).pipe(
359 + Stream.filter(done),
360 + Stream.runHead,
361 + Effect.map(Option.getOrThrow),
362 + ),
363 + };
364 +});
365 +
366 +const shellSnapshot = (input: {
367 + readonly sequence: number;
368 + readonly asOfSequence: number;
369 + readonly count: number;
370 +}) =>
371 + Option.some({
372 + schemaVersion: 1,
373 + snapshotSequence: input.sequence,
374 + projects: [],
375 + threads: [row("recent", { settledDay: 60 })],
376 + archivedThreads: [],
377 + olderSettled: {
378 + before,
379 + asOfSequence: input.asOfSequence,
380 + counts: [{ projectId: project, count: input.count }],
381 + },
382 + } as unknown as OrchestrationV2ShellSnapshot);
383 +
384 +describe("createOlderSettledThreadAtoms", () => {
385 + it.effect("keeps counts current: the snapshot's own, then one read after changes settle", () =>
386 + Effect.gen(function* () {
387 + const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms();
388 + const olderAtom = atoms.olderSettledAtom(environmentId);
389 + const count = (value: OlderSettled | null) => value?.counts.get(project);
390 + registry.mount(olderAtom);
391 +
392 + // A cached shell: its counts stand, read nothing.
393 + setShell({ snapshot: shellSnapshot({ sequence: 5, asOfSequence: 3, count: 5 }) });
394 + expect(count(registry.get(olderAtom))).toBe(5);
395 + // This session's snapshot: its counts are current until something changes.
396 + setShell({
397 + snapshot: shellSnapshot({ sequence: 3, asOfSequence: 3, count: 5 }),
398 + generation: 1,
399 + });
400 + expect(count(registry.get(olderAtom))).toBe(5);
401 +
402 + // A removal can change the count: one read once changes stop for a moment.
403 + yield* Queue.offer(
404 + replies,
405 + Effect.succeed({
406 + snapshotSequence: 4,
407 + threads: [],
408 + counts: [{ projectId: project, count: 4 }],
409 + }),
410 + );
411 + setShell({
412 + snapshot: shellSnapshot({ sequence: 4, asOfSequence: 3, count: 5 }),
413 + generation: 1,
414 + removedAt: new Map([[ThreadId.make("gone"), 4]]),
415 + });
416 + expect(count(registry.get(olderAtom))).toBe(5);
417 + yield* TestClock.adjust("500 millis");
418 + expect(count(yield* until(olderAtom, (value) => count(value) === 4))).toBe(4);
419 + expect(requests).toEqual([{ before: { settledAt: before }, limit: 0 }]);
420 +
421 + // A new snapshot brings its own counts and drops the read.
422 + setShell({
423 + snapshot: shellSnapshot({ sequence: 9, asOfSequence: 9, count: 7 }),
424 + generation: 2,
425 + });
426 + expect(count(registry.get(olderAtom))).toBe(7);
427 + expect(requests).toHaveLength(1);
428 + }),
429 + );
430 +
431 + it.effect("reads pages in order until a short one, and retries a failed page", () =>
432 + Effect.gen(function* () {
433 + const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms();
434 + const scope = { environmentId, projectIds: null };
435 + const snapshot = shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 });
436 +
437 + // A cached shell holds no other environment back: nothing can load yet.
438 + setShell({ snapshot });
439 + expect(registry.get(atoms.pagesAtom(scope, 1))).toEqual({
440 + reads: [],
441 + frontier: null,
442 + status: "ready",
443 + });
444 +
445 + setShell({ snapshot, generation: 1 });
446 + expect(registry.get(atoms.pagesAtom(scope, 0)).frontier).toEqual({ settledAt: before });
447 + const full = Array.from({ length: 25 }, (_, index) =>
448 + row(`full-${String(index).padStart(2, "0")}`, { settledDay: 40 - index }),
449 + );
450 + yield* Queue.offer(
451 + replies,
452 + Effect.succeed({ snapshotSequence: 3, threads: full, counts: [] }),
453 + );
454 + yield* Queue.offer(
455 + replies,
456 + Effect.succeed({
457 + snapshotSequence: 3,
458 + threads: [row("short", { settledDay: 10 })],
459 + counts: [],
460 + }),
461 + );
462 + const twoPages = atoms.pagesAtom(scope, 2);
463 + registry.mount(twoPages);
464 + const done = yield* until(twoPages, (pages) => pages.reads.length === 2);
465 + expect(done.frontier).toBeNull();
466 + expect(done.status).toBe("ready");
467 + // The second page continues from the first page's last row.
468 + expect(requests).toEqual([
469 + { before: { settledAt: before }, limit: 25 },
470 + {
471 + before: { settledAt: DateTime.formatIso(full[24]!.settledAt!), threadId: full[24]!.id },
472 + limit: 25,
473 + },
474 + ]);
475 +
476 + // A new snapshot reads again; a failed page shows until a retry reads it.
477 + const onePage = atoms.pagesAtom(scope, 1);
478 + registry.mount(onePage);
479 + setShell({ snapshot, generation: 2 });
480 + expect(registry.get(onePage).status).toBe("pending");
481 + yield* Queue.offer(
482 + replies,
483 + Effect.fail(new OrchestrationV2GetShellSnapshotError({ message: "read failed" })),
484 + );
485 + expect((yield* until(onePage, (pages) => pages.status === "failed")).frontier).toEqual({
486 + settledAt: before,
487 + });
488 + atoms.retryPages(registry, scope, 1);
489 + expect(registry.get(onePage).status).toBe("pending");
490 + yield* Queue.offer(
491 + replies,
492 + Effect.succeed({ snapshotSequence: 3, threads: full.slice(0, 3), counts: [] }),
493 + );
494 + const retried = yield* until(onePage, (pages) => pages.status === "ready");
495 + expect(retried.reads).toHaveLength(1);
496 + expect(retried.frontier).toBeNull();
497 + }),
498 + );
499 +
500 + it.effect("searches read once per generation, and nothing before the first snapshot", () =>
501 + Effect.gen(function* () {
502 + const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms();
503 + const snapshot = shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 });
504 + const input = { query: "old", olderSettledBefore: before, titlesOnly: true };
505 + const search = atoms.searchAtom(environmentId, input);
506 +
507 + // That snapshot would drop the rows: loading while it is on its way, idle while offline.
508 + setShell({ snapshot, status: "synchronizing" });
509 + registry.mount(search);
510 + expect(registry.get(search).waiting).toBe(true);
511 + setShell({ snapshot, status: "cached" });
512 + expect(registry.get(search).waiting).toBe(false);
513 +
514 + const found = { matches: [], snapshotSequence: 3, threads: [row("old", { settledDay: 9 })] };
515 + setShell({ snapshot, status: "live", generation: 1 });
516 + yield* Queue.offer(replies, Effect.succeed(found));
517 + yield* until(search, AsyncResult.isSuccess);
518 + // A new snapshot reads the rows again.
519 + setShell({ snapshot, status: "live", generation: 2 });
520 + yield* Queue.offer(replies, Effect.succeed({ ...found, snapshotSequence: 8 }));
521 + yield* until(
522 + search,
523 + (result) => Option.getOrNull(AsyncResult.value(result))?.snapshotSequence === 8,
524 + );
525 + expect(requests).toEqual([input, input]);
526 + }),
527 + );
528 +
529 + it.effect("growing reads only the next page, and collapsing stops the shelf's reads", () =>
530 + Effect.gen(function* () {
531 + const { replies, requestLog, atoms, registry, setShell, until } = yield* makeAtoms();
532 + setShell({
533 + snapshot: shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 }),
534 + generation: 1,
535 + });
536 + const scope = { environmentId, projectIds: null };
537 + const full = Array.from({ length: 25 }, (_, index) =>
538 + row(`full-${String(index).padStart(2, "0")}`, { settledDay: 40 - index }),
539 + );
540 + const releaseOne = registry.mount(atoms.pagesAtom(scope, 1));
541 + expect((yield* Queue.take(requestLog)).input).toEqual({
542 + before: { settledAt: before },
543 + limit: 25,
544 + });
545 + yield* Queue.offer(
546 + replies,
547 + Effect.succeed({ snapshotSequence: 3, threads: full, counts: [] }),
548 + );
549 + yield* until(atoms.pagesAtom(scope, 1), (pages) => pages.reads.length === 1);
550 +
551 + // React builds the next shelf before the last one lets go, so page 1 is not read again.
552 + registry.get(atoms.pagesAtom(scope, 2));
553 + releaseOne();
554 + const releaseTwo = registry.mount(atoms.pagesAtom(scope, 2));
555 + const second = yield* Queue.take(requestLog);
556 + expect(second.input).toEqual({
557 + before: { settledAt: DateTime.formatIso(full[24]!.settledAt!), threadId: full[24]!.id },
558 + limit: 25,
559 + });
560 +
561 + // Collapsing stops the pending read: nothing is left to run again on a reconnect.
562 + releaseTwo();
563 + yield* Deferred.await(second.stopped);
564 + }),
565 + );
566 +});
packages/client-runtime/src/state/olderSettledThreads.ts new
+470
@@ -0,0 +1,470 @@
1 +import {
2 + isOlderSettledThreadShell,
3 + ORCHESTRATION_V2_WS_METHODS,
4 + type EnvironmentId,
5 + type OrchestrationV2OlderSettledCursor,
6 + type OrchestrationV2OlderSettledThreadsPage,
7 + type OrchestrationV2SearchThreadsInput,
8 + type OrchestrationV2ThreadFamily,
9 + type OrchestrationV2ThreadShell,
10 + type ProjectId,
11 + type ThreadId,
12 +} from "@t3tools/contracts";
13 +import * as DateTime from "effect/DateTime";
14 +import * as Effect from "effect/Effect";
15 +import * as Option from "effect/Option";
16 +import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";
17 +
18 +import type * as EnvironmentRegistry from "../connection/registry.ts";
19 +import { request } from "../rpc/client.ts";
20 +import { createEnvironmentQueryAtomFamily, selectEnvironmentQueryData } from "./runtime.ts";
21 +import type { EnvironmentShellState } from "./shell.ts";
22 +import { threadFamilyRead } from "./threadFamilies.ts";
23 +
24 +// Rows per page of older settled threads: one "Show more" step of the Settled shelf.
25 +const OLDER_SETTLED_PAGE_SIZE = 25;
26 +// A burst of changes (a PR sync touching many old threads) costs one count read.
27 +const COUNT_REREAD_DELAY = "500 millis";
28 +
29 +/** Where an environment's shell cut settled threads, and how many are older per project. */
30 +export interface OlderSettled {
31 + readonly before: string;
32 + readonly beforeMs: number;
33 + readonly counts: ReadonlyMap<ProjectId, number>;
34 +}
35 +
36 +/** The scope's projects in one environment, or null for all of them. */
37 +export interface OlderSettledScope {
38 + readonly environmentId: EnvironmentId;
39 + readonly projectIds: ReadonlyArray<ProjectId> | null;
40 +}
41 +
42 +/**
43 + * The pages read below an environment's cut. Everything at or above `frontier` is local: pages
44 + * hold the rows that were there when read, and shell deltas keep that range current. A null
45 + * frontier means no more can load: every older thread is loaded, or this session has no snapshot.
46 + */
47 +export interface OlderSettledPages {
48 + readonly reads: ReadonlyArray<OrchestrationV2ThreadFamily>;
49 + readonly frontier: OrchestrationV2OlderSettledCursor | null;
50 + readonly status: "ready" | "pending" | "failed";
51 +}
52 +
53 +const NO_PAGES: OlderSettledPages = { reads: [], frontier: null, status: "ready" };
54 +
55 +type OlderSettledRow = Pick<
56 + OrchestrationV2ThreadShell,
57 + "id" | "projectId" | "lineage" | "settledOverride" | "settledAt"
58 +>;
59 +
60 +export interface OlderSettledShelfEnvironment {
61 + readonly older: OlderSettled | null;
62 + readonly pages: OlderSettledPages;
63 +}
64 +
65 +/**
66 + * Changes whenever the older count can: any removal (by largest sequence, so a repeat counts), an
67 + * older row that arrives or changes, or an old row arriving outside the set (unsettled). Threads
68 + * created after the cut never enter it, as threads settle after creation. Archived-thread events
69 + * arrive as removals too, each costing one count read.
70 + */
71 +export function olderSettledCountKey(
72 + threads: ReadonlyArray<OrchestrationV2ThreadShell>,
73 + beforeMs: number,
74 + removedAt: ReadonlyMap<ThreadId, number>,
75 +): string {
76 + let lastRemoval = 0;
77 + for (const sequence of removedAt.values()) lastRemoval = Math.max(lastRemoval, sequence);
78 + const rows: string[] = [];
79 + for (const thread of threads) {
80 + if (thread.lineage.relationshipToParent === "subagent") continue;
81 + const older = isOlderSettledThreadShell(thread, beforeMs);
82 + if (older || DateTime.toEpochMillis(thread.createdAt) < beforeMs) {
83 + rows.push(`${thread.id}:${older ? 1 : 0}`);
84 + }
85 + }
86 + return `${lastRemoval}|${rows.sort().join(",")}`;
87 +}
88 +
89 +/** Whether a row sits at or above a page cursor in the server's order (settle time desc, id asc). */
90 +function atOrAboveCursor(
91 + thread: Pick<OrchestrationV2ThreadShell, "id" | "settledAt">,
92 + cursor: OrchestrationV2OlderSettledCursor,
93 +): boolean {
94 + if (thread.settledAt === null) return true;
95 + const settledAt = DateTime.formatIso(thread.settledAt);
96 + return !(
97 + settledAt < cursor.settledAt ||
98 + (cursor.threadId !== undefined && settledAt === cursor.settledAt && thread.id > cursor.threadId)
99 + );
100 +}
101 +
102 +/**
103 + * Where a shelf over these environments stops listing: the highest frontier of any environment
104 + * that can still load older threads (its cut until its first page). Rows below it could sort under
105 + * rows not loaded yet, so the merged list is a correct prefix even while a page loads. A failed
106 + * environment holds no one back; its Retry rejoins it.
107 + */
108 +export function settledShelfFrontier(
109 + environments: ReadonlyArray<OlderSettledShelfEnvironment>,
110 +): OrchestrationV2OlderSettledCursor | null {
111 + let highest: OrchestrationV2OlderSettledCursor | null = null;
112 + for (const { older, pages } of environments) {
113 + if (older === null || pages.frontier === null || pages.status === "failed") continue;
114 + const frontier = pages.frontier;
115 + // On a settle-time tie, a cursor with an id stops above one without, and a smaller id higher.
116 + if (
117 + highest === null ||
118 + frontier.settledAt > highest.settledAt ||
119 + (frontier.settledAt === highest.settledAt &&
120 + frontier.threadId !== undefined &&
121 + (highest.threadId === undefined || frontier.threadId < highest.threadId))
122 + ) {
123 + highest = frontier;
124 + }
125 + }
126 + return highest;
127 +}
128 +
129 +/**
130 + * Whether the Settled shelf may list a row: at or above the shelf's frontier. So a row that turns
131 + * older mid-session (a backdated auto-settle, a snooze that ends, an unarchive) waits for the
132 + * range that holds it.
133 + */
134 +export function isListedSettledThread(
135 + thread: Pick<OrchestrationV2ThreadShell, "id" | "settledAt">,
136 + frontier: OrchestrationV2OlderSettledCursor | null,
137 +): boolean {
138 + return frontier === null || atOrAboveCursor(thread, frontier);
139 +}
140 +
141 +/**
142 + * Older settled threads in scope that this client does not hold: the server's counts minus the
143 + * older rows it has. Add it to the local rows for the Settled label or a project's thread count.
144 + */
145 +export function unloadedOlderSettledCount(input: {
146 + readonly olderSettled: (environmentId: EnvironmentId) => OlderSettled | null;
147 + readonly scopes: ReadonlyArray<OlderSettledScope>;
148 + /** Every row the client holds in scope, in any section. */
149 + readonly threads: ReadonlyArray<{
150 + readonly environmentId: EnvironmentId;
151 + readonly source: OlderSettledRow;
152 + }>;
153 +}): number {
154 + let unloaded = 0;
155 + for (const scope of input.scopes) {
156 + const older = input.olderSettled(scope.environmentId);
157 + if (older === null) continue;
158 + if (scope.projectIds === null) {
159 + for (const count of older.counts.values()) unloaded += count;
160 + } else {
161 + for (const projectId of scope.projectIds) unloaded += older.counts.get(projectId) ?? 0;
162 + }
163 + for (const thread of input.threads) {
164 + if (
165 + thread.environmentId === scope.environmentId &&
166 + (scope.projectIds === null || scope.projectIds.includes(thread.source.projectId)) &&
167 + isOlderSettledThreadShell(thread.source, older.beforeMs)
168 + ) {
169 + unloaded -= 1;
170 + }
171 + }
172 + }
173 + return Math.max(0, unloaded);
174 +}
175 +
176 +/**
177 + * The pages each environment should read: while the shelf lists fewer than `visibleCount` rows,
178 + * one more for each environment whose frontier is the shelf's, once its pages have loaded. A
179 + * short page ends an environment's chain; counts can lag a backdated settle, so they do not gate.
180 + */
181 +export function wantedOlderSettledPages(input: {
182 + readonly requested: ReadonlyMap<EnvironmentId, number>;
183 + readonly environments: ReadonlyMap<EnvironmentId, OlderSettledShelfEnvironment>;
184 + readonly listedCount: number;
185 + readonly visibleCount: number;
186 +}): ReadonlyMap<EnvironmentId, number> {
187 + if (input.listedCount >= input.visibleCount) return input.requested;
188 + const frontier = settledShelfFrontier([...input.environments.values()]);
189 + if (frontier === null) return input.requested;
190 + let next: Map<EnvironmentId, number> | null = null;
191 + for (const [environmentId, { older, pages }] of input.environments) {
192 + if (older === null || pages.status !== "ready" || pages.frontier === null) continue;
193 + if (
194 + pages.frontier.settledAt !== frontier.settledAt ||
195 + pages.frontier.threadId !== frontier.threadId
196 + ) {
197 + continue;
198 + }
199 + next ??= new Map(input.requested);
200 + next.set(environmentId, (input.requested.get(environmentId) ?? 0) + 1);
201 + }
202 + return next ?? input.requested;
203 +}
204 +
205 +/**
206 + * Shells that omit older settled threads (`omitOlderSettled`) name the cut and count what they left
207 + * out. These atoms keep those counts current and read older threads in pages for the Settled shelf.
208 + */
209 +export function createOlderSettledThreadAtoms<R, ER, E>(
210 + runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, ER>,
211 + shellStateAtom: (
212 + environmentId: EnvironmentId,
213 + ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentShellState, E>>,
214 +) {
215 + const shellStateValue = (state: AsyncResult.AsyncResult<EnvironmentShellState, E>) =>
216 + Option.getOrNull(AsyncResult.value(state));
217 +
218 + const countReadAtom = createEnvironmentQueryAtomFamily(runtime, {
219 + label: "environment-data:orchestration:older-settled-counts",
220 + staleTimeMs: Number.POSITIVE_INFINITY,
221 + // A newer key unmounts this read; with no idle time its wait is interrupted before the RPC.
222 + idleTtlMs: 0,
223 + execute: Effect.fn("OlderSettledThreads.readCounts")(function* (input: {
224 + readonly generation: number;
225 + readonly before: string;
226 + readonly key: string;
227 + }) {
228 + yield* Effect.sleep(COUNT_REREAD_DELAY);
229 + return yield* request(ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads, {
230 + before: { settledAt: input.before },
231 + limit: 0,
232 + });
233 + }),
234 + });
235 +
236 + const pageReadAtom = createEnvironmentQueryAtomFamily(runtime, {
237 + label: "environment-data:orchestration:older-settled-page",
238 + staleTimeMs: Number.POSITIVE_INFINITY,
239 + // A collapsed shelf keeps no read alive, as an idle one runs again when a reconnect replaces
240 + // the session.
241 + idleTtlMs: 0,
242 + execute: Effect.fn("OlderSettledThreads.readPage")(function* (input: {
243 + readonly generation: number;
244 + readonly projectIds: ReadonlyArray<ProjectId> | null;
245 + readonly before: OrchestrationV2OlderSettledCursor;
246 + }) {
247 + return yield* request(ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads, {
248 + before: input.before,
249 + ...(input.projectIds === null ? {} : { projectIds: input.projectIds }),
250 + limit: OLDER_SETTLED_PAGE_SIZE,
251 + });
252 + }),
253 + });
254 +
255 + const countsOf = (source: {
256 + readonly counts: OrchestrationV2OlderSettledThreadsPage["counts"];
257 + }) => new Map(source.counts.map(({ projectId, count }) => [projectId, count] as const));
258 +
259 + const olderSettledAtom = Atom.family((environmentId: EnvironmentId) => {
260 + let generation = -1;
261 + // The key of the first state seen this generation, when that state is the snapshot itself.
262 + let snapshotKey: string | null = null;
263 + let keyInputs: { readonly threads: unknown; readonly removedAt: unknown } | null = null;
264 + let key = "";
265 + let fromSnapshot: OlderSettled | null = null;
266 + let lastRead: OlderSettled | null = null;
267 + let lastReadPage: OrchestrationV2OlderSettledThreadsPage | null = null;
268 + return Atom.make((get): OlderSettled | null => {
269 + const state = shellStateValue(get(shellStateAtom(environmentId)));
270 + const snapshot = state === null ? null : Option.getOrNull(state.snapshot);
271 + const cut = snapshot?.olderSettled;
272 + if (state === null || snapshot === null || cut === undefined) return null;
273 + if (fromSnapshot?.before !== cut.before || state.generation !== generation) {
274 + fromSnapshot = {
275 + before: cut.before,
276 + beforeMs: DateTime.toEpochMillis(DateTime.makeUnsafe(cut.before)),
277 + counts: countsOf(cut),
278 + };
279 + }
280 + // Most shell updates move only the cursor; the key needs new rows or removals.
281 + if (keyInputs?.threads !== snapshot.threads || keyInputs.removedAt !== state.removedAt) {
282 + keyInputs = { threads: snapshot.threads, removedAt: state.removedAt };
283 + key = olderSettledCountKey(snapshot.threads, fromSnapshot.beforeMs, state.removedAt);
284 + }
285 + if (state.generation !== generation) {
286 + generation = state.generation;
287 + snapshotKey = snapshot.snapshotSequence === cut.asOfSequence ? key : null;
288 + lastRead = null;
289 + }
290 + // Generation 0 is a cached shell: no reads until this session's first snapshot.
291 + if (key === snapshotKey || state.generation === 0) return fromSnapshot;
292 + const read = selectEnvironmentQueryData(
293 + get(
294 + countReadAtom({
295 + environmentId,
296 + input: { generation: state.generation, before: cut.before, key },
297 + }),
298 + ),
299 + );
300 + if (read !== null && read !== lastReadPage) {
301 + lastReadPage = read;
302 + lastRead = { ...fromSnapshot, counts: countsOf(read) };
303 + }
304 + return lastRead ?? fromSnapshot;
305 + }).pipe(Atom.withLabel(`environment-older-settled:${environmentId}`));
306 + });
307 +
308 + const pagesKey = (scope: OlderSettledScope, pages: number) =>
309 + JSON.stringify([
310 + scope.environmentId,
311 + scope.projectIds === null ? null : [...scope.projectIds].sort(),
312 + pages,
313 + ]);
314 +
315 + const pagesAtom = Atom.family((key: string) => {
316 + const [environmentId, projectIds, pages] = JSON.parse(key) as [
317 + EnvironmentId,
318 + ReadonlyArray<ProjectId> | null,
319 + number,
320 + ];
321 + let previous = NO_PAGES;
322 + const keep = (next: OlderSettledPages): OlderSettledPages => {
323 + const same =
324 + next.status === previous.status &&
325 + next.frontier?.settledAt === previous.frontier?.settledAt &&
326 + next.frontier?.threadId === previous.frontier?.threadId &&
327 + (next.frontier === null) === (previous.frontier === null) &&
328 + next.reads.length === previous.reads.length &&
329 + next.reads.every((read, index) => read === previous.reads[index]);
330 + if (!same) previous = next;
331 + return previous;
332 + };
333 + return Atom.make((get) => {
334 + const state = shellStateValue(get(shellStateAtom(environmentId)));
335 + const cut = state === null ? undefined : Option.getOrNull(state.snapshot)?.olderSettled;
336 + // A cached shell (an environment not connected yet, or offline) must not hold the shelf
337 + // back for other environments, so it lists what it holds until its first snapshot.
338 + if (state === null || cut === undefined || state.generation === 0) return keep(NO_PAGES);
339 + let frontier: OrchestrationV2OlderSettledCursor = { settledAt: cut.before };
340 + if (pages === 0) return keep({ reads: [], frontier, status: "ready" });
341 + const reads: OrchestrationV2ThreadFamily[] = [];
342 + for (let index = 0; index < pages; index += 1) {
343 + const result = get(
344 + pageReadAtom({
345 + environmentId,
346 + input: { generation: state.generation, projectIds, before: frontier },
347 + }),
348 + );
349 + const page = selectEnvironmentQueryData(result);
350 + if (page === null) {
351 + return keep({
352 + reads,
353 + frontier,
354 + status: threadFamilyRead(result) === "failed" ? "failed" : "pending",
355 + });
356 + }
357 + reads.push(page);
358 + const last = page.threads.at(-1);
359 + if (page.threads.length < OLDER_SETTLED_PAGE_SIZE || last?.settledAt == null) {
360 + return keep({ reads, frontier: null, status: "ready" });
361 + }
362 + frontier = { settledAt: DateTime.formatIso(last.settledAt), threadId: last.id };
363 + }
364 + return keep({ reads, frontier, status: "ready" });
365 + }).pipe(Atom.withLabel(`environment-older-settled-pages:${key}`));
366 + });
367 +
368 + const pageRowsAtom = Atom.family((key: string) =>
369 + Atom.map(pagesAtom(key), (pages) => pages.reads).pipe(
370 + Atom.withLabel(`environment-older-settled-page-rows:${key}`),
371 + ),
372 + );
373 +
374 + const searchGenerationAtom = Atom.family((environmentId: EnvironmentId) =>
375 + Atom.make((get): number | "waiting" | "none" => {
376 + const state = shellStateValue(get(shellStateAtom(environmentId)));
377 + if (state !== null && state.generation > 0) return state.generation;
378 + return state?.status === "synchronizing" ? "waiting" : "none";
379 + }),
380 + );
381 + const searchReadAtom = createEnvironmentQueryAtomFamily(runtime, {
382 + label: "environment-data:orchestration:older-settled-search",
383 + idleTtlMs: 60_000,
384 + execute: Effect.fn("OlderSettledThreads.search")(function* (input: {
385 + readonly generation: number;
386 + readonly search: OrchestrationV2SearchThreadsInput;
387 + }) {
388 + return yield* request(ORCHESTRATION_V2_WS_METHODS.searchThreads, input.search);
389 + }),
390 + });
391 + // Keyed by shell generation like pages: rows read before a new snapshot do not merge, so the
392 + // search reads them again.
393 + const searchAtom = Atom.family((key: string) => {
394 + const [environmentId, search] = JSON.parse(key) as [
395 + EnvironmentId,
396 + OrchestrationV2SearchThreadsInput,
397 + ];
398 + return Atom.make((get) => {
399 + const generation = get(searchGenerationAtom(environmentId));
400 + // Nothing is read before this session's first snapshot, which would drop the rows. It shows
401 + // as loading only while that snapshot is on its way, so an offline environment never spins.
402 + return typeof generation === "number"
403 + ? get(searchReadAtom({ environmentId, input: { generation, search } }))
404 + : AsyncResult.initial(generation === "waiting");
405 + }).pipe(Atom.withLabel(`environment-older-settled-search:${key}`));
406 + });
407 +
408 + // One read for a shelf that spans environments: each one's cut, counts and pages.
409 + const shelfAtom = Atom.family((key: string) => {
410 + const entries = JSON.parse(key) as ReadonlyArray<readonly [string, string]>;
411 + let previous: ReadonlyMap<EnvironmentId, OlderSettledShelfEnvironment> = new Map();
412 + return Atom.make((get): ReadonlyMap<EnvironmentId, OlderSettledShelfEnvironment> => {
413 + const shelf = new Map<EnvironmentId, OlderSettledShelfEnvironment>();
414 + let changed = previous.size !== entries.length;
415 + for (const [environmentId, pagesKey] of entries) {
416 + const older = get(olderSettledAtom(environmentId as EnvironmentId));
417 + const pages = get(pagesAtom(pagesKey));
418 + const prior = previous.get(environmentId as EnvironmentId);
419 + changed ||= prior?.older !== older || prior.pages !== pages;
420 + shelf.set(environmentId as EnvironmentId, { older, pages });
421 + }
422 + if (changed) previous = shelf;
423 + return previous;
424 + }).pipe(Atom.withLabel(`environment-older-settled-shelf:${key}`));
425 + });
426 +
427 + /** Reads a failed page again (a no-op when none failed); the pages after it follow. */
428 + const retryPages = (
429 + registry: AtomRegistry.AtomRegistry,
430 + scope: OlderSettledScope,
431 + pages: number,
432 + ) => {
433 + const state = shellStateValue(registry.get(shellStateAtom(scope.environmentId)));
434 + const result = registry.get(pagesAtom(pagesKey(scope, pages)));
435 + if (state === null || result.status !== "failed" || result.frontier === null) return;
436 + registry.refresh(
437 + pageReadAtom({
438 + environmentId: scope.environmentId,
439 + input: {
440 + generation: state.generation,
441 + projectIds: scope.projectIds === null ? null : [...scope.projectIds].sort(),
442 + before: result.frontier,
443 + },
444 + }),
445 + );
446 + };
447 +
448 + return {
449 + /** The environment's cut and current counts, or null when its shell keeps every settled thread. */
450 + olderSettledAtom,
451 + /** Each scope's cut, counts and first `pages` pages. */
452 + shelfAtom: (
453 + scopes: ReadonlyArray<{ readonly scope: OlderSettledScope; readonly pages: number }>,
454 + ) =>
455 + shelfAtom(
456 + JSON.stringify(
457 + scopes.map(({ scope, pages }) => [scope.environmentId, pagesKey(scope, pages)] as const),
458 + ),
459 + ),
460 + retryPages,
461 + /** The first `pages` pages below the cut for one scope, read in order. */
462 + pagesAtom: (scope: OlderSettledScope, pages: number) => pagesAtom(pagesKey(scope, pages)),
463 + /** Their rows, for `createThreadFamilyAtoms(...).mountRows`, under `rowsKey`. */
464 + pageRowsAtom: (scope: OlderSettledScope, pages: number) => pageRowsAtom(pagesKey(scope, pages)),
465 + rowsKey: (scope: OlderSettledScope, pages: number) => `older-settled:${pagesKey(scope, pages)}`,
466 + /** `orchestration.searchThreads` for a search that asks for older settled rows. */
467 + searchAtom: (environmentId: EnvironmentId, search: OrchestrationV2SearchThreadsInput) =>
468 + searchAtom(JSON.stringify([environmentId, search])),
469 + };
470 +}
packages/client-runtime/src/state/projectRemoval.ts
+6 -3
@@ -12,19 +12,22 @@ export function planProjectRemoval<
12 readonly hasOtherMembers: boolean;
13 readonly displayName: string;
14 readonly threads: ReadonlyArray<Thread>;
15 + /** The members' threads this client does not hold: older settled threads its shell left out. */
16 + readonly unloadedThreadCount?: number;
17 }) {
18 const { members, groupMemberCount, hasOtherMembers, displayName, threads } = options;
19 const memberKeys = new Set(members.map((member) => `${member.environmentId}:${member.id}`));
20 const projectThreads = threads.filter((thread) =>
21 memberKeys.has(`${thread.environmentId}:${thread.projectId}`),
22 );
23 + const threadCount = projectThreads.length + (options.unloadedThreadCount ?? 0);
24 const isWholeGroup = members.length === groupMemberCount;
25 const targetKind = hasOtherMembers || !isWholeGroup ? "checkout" : "project";
26 const singleMember = members.length === 1 ? members[0]! : null;
27 const targetLabel = singleMember?.title ?? displayName;
28 const confirmationMessage = [
26 - projectThreads.length > 0
27 - ? `Remove ${targetKind} "${targetLabel}" and delete its ${projectThreads.length} thread${projectThreads.length === 1 ? "" : "s"}?`
29 + threadCount > 0
30 + ? `Remove ${targetKind} "${targetLabel}" and delete its ${threadCount} thread${threadCount === 1 ? "" : "s"}?`
31 : `Remove ${targetKind} "${targetLabel}"?`,
32 ...(singleMember
33 ? [
@@ -34,7 +37,7 @@ export function planProjectRemoval<
37 : []),
38 ]
39 : [`This removes ${members.length} grouped project entries.`]),
37 - ...(projectThreads.length > 0
40 + ...(threadCount > 0
41 ? ["This permanently clears conversation history for those threads and any archived threads."]
42 : ["This permanently clears any archived conversation history."]),
43 isWholeGroup && !hasOtherMembers
packages/client-runtime/src/state/pullRequests.ts
+8
@@ -302,6 +302,14 @@ export function createPullRequestEnvironmentAtoms<R, E>(
302 refreshIntervalMs: 10_000,
303 refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
304 }),
305 + // For callers that open a thread from the answer: a persisted one reads as final before
306 + // the connection is known, and a kept one can predate the thread.
307 + linkedThreadsUncached: createEnvironmentRpcQueryAtomFamily(runtime, {
308 + label: "environment-data:pull-requests:linked-threads-uncached",
309 + tag: WS_METHODS.pullRequestsLinkedThreads,
310 + staleTimeMs: 0,
311 + idleTtlMs: 0,
312 + }),
313 list: createEnvironmentRpcQueryAtomFamily(runtime, {
314 label: "environment-data:pull-requests:list",
315 persistence: persistQuery(PullRequestListResult),
packages/client-runtime/src/state/shell-sync.test.ts
+6 -2
@@ -628,7 +628,7 @@ describe("environment shell synchronization", () => {
628 }),
629 );
630
631 - it.effect("opts in to idle-child omission and counts authoritative snapshots and removals", () =>
631 + it.effect("opts in to shell omissions and counts authoritative snapshots and removals", () =>
632 Effect.gen(function* () {
633 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
634 const subscribeInput = yield* Deferred.make<unknown>();
@@ -644,6 +644,7 @@ describe("environment shell synchronization", () => {
644 initialConfig: Effect.succeed({
645 shellResumeCompletionMarker: true,
646 shellThreadFamilies: true,
647 + shellOlderSettled: true,
648 } as never),
649 };
650 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
@@ -687,8 +688,11 @@ describe("environment shell synchronization", () => {
688 expect(yield* Deferred.await(subscribeInput)).toMatchObject({
689 afterSequence: 1,
690 omitIdleSubagents: true,
691 + omitOlderSettled: true,
692 });
691 - expect(yield* Ref.get(loadOptions)).toEqual([{ omitIdleSubagents: true }]);
693 + expect(yield* Ref.get(loadOptions)).toEqual([
694 + { omitIdleSubagents: true, omitOlderSettled: true },
695 + ]);
696 const loaded = Option.getOrThrow(yield* stateWhere((state) => state.generation > 0));
697 expect(loaded.removedAt.size).toBe(0);
698
packages/client-runtime/src/state/shell.ts
+22 -12
@@ -361,16 +361,20 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
361 ORCHESTRATION_V2_WS_METHODS.subscribeShell,
362 Effect.fn("EnvironmentShellState.makeSubscribeInput")(function* (session) {
363 yield* Ref.set(activeSubscriptionSession, session);
364 - const { supportsCompletionMarker, omitIdleSubagents } = yield* session.initialConfig.pipe(
365 - Effect.map((config) => ({
366 - supportsCompletionMarker: config.shellResumeCompletionMarker === true,
367 - omitIdleSubagents: config.shellThreadFamilies === true,
368 - })),
369 - Effect.orElseSucceed(() => ({
370 - supportsCompletionMarker: false,
371 - omitIdleSubagents: false,
372 - })),
373 - );
364 + const { supportsCompletionMarker, omitIdleSubagents, omitOlderSettled } =
365 + yield* session.initialConfig.pipe(
366 + Effect.map((config) => ({
367 + supportsCompletionMarker: config.shellResumeCompletionMarker === true,
368 + omitIdleSubagents: config.shellThreadFamilies === true,
369 + omitOlderSettled:
370 + config.shellOlderSettled === true && config.shellThreadFamilies === true,
371 + })),
372 + Effect.orElseSucceed(() => ({
373 + supportsCompletionMarker: false,
374 + omitIdleSubagents: false,
375 + omitOlderSettled: false,
376 + })),
377 + );
378 yield* Ref.set(awaitingCompletion, supportsCompletionMarker);
379 yield* setSynchronizing;
380
@@ -396,7 +400,10 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
400 ),
401 );
402 // HTTP and the socket must agree, or omitted children would return through the socket.
399 - const httpSnapshot = yield* snapshotLoader.load(prepared, { omitIdleSubagents });
403 + const httpSnapshot = yield* snapshotLoader.load(prepared, {
404 + omitIdleSubagents,
405 + omitOlderSettled,
406 + });
407 if (Option.isSome(httpSnapshot)) {
408 yield* cancelActiveFill;
409 const previous = yield* SubscriptionRef.get(state);
@@ -418,7 +425,10 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
425 }
426 }
427
421 - const omission = omitIdleSubagents ? { omitIdleSubagents: true as const } : {};
428 + const omission = {
429 + ...(omitIdleSubagents ? { omitIdleSubagents: true as const } : {}),
430 + ...(omitOlderSettled ? { omitOlderSettled: true as const } : {}),
431 + };
432 if (!canResume || Option.isNone(current.snapshot)) {
433 return {
434 ...omission,
packages/client-runtime/src/state/shellSnapshotHttp.ts
+7 -2
@@ -34,9 +34,13 @@ export const fetchEnvironmentShellSnapshot = Effect.fn(
34 >;
35 readonly timeoutMs?: number;
36 readonly omitIdleSubagents?: boolean;
37 + readonly omitOlderSettled?: boolean;
38 }) {
39 const endpoint = {
39 - query: input.omitIdleSubagents === true ? { omitIdleSubagents: "1" } : {},
40 + query: {
41 + ...(input.omitIdleSubagents === true ? { omitIdleSubagents: "1" } : {}),
42 + ...(input.omitOlderSettled === true ? { omitOlderSettled: "1" } : {}),
43 + },
44 };
45 return yield* executeAuthenticatedEnvironmentHttpRequest({
46 ...input,
@@ -85,7 +89,7 @@ export class ShellSnapshotLoader extends Context.Service<
89 {
90 readonly load: (
91 prepared: PreparedConnection,
88 - options?: { readonly omitIdleSubagents?: boolean },
92 + options?: { readonly omitIdleSubagents?: boolean; readonly omitOlderSettled?: boolean },
93 ) => Effect.Effect<Option.Option<DeferredShellSnapshot>>;
94 }
95 >()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
@@ -107,6 +111,7 @@ export const layer: Layer.Layer<ShellSnapshotLoader, never, HttpClient.HttpClien
111 signer,
112 remoteAuthorization,
113 ...(options?.omitIdleSubagents === true ? { omitIdleSubagents: true } : {}),
114 + ...(options?.omitOlderSettled === true ? { omitOlderSettled: true } : {}),
115 }).pipe(
116 Effect.map(Option.some<DeferredShellSnapshot>),
117 Effect.provideService(HttpClient.HttpClient, httpClient),
packages/client-runtime/src/state/threadFamilies.test.ts
+43
@@ -75,6 +75,49 @@ const TARGET = new PrimaryConnectionTarget({
75 });
76
77 describe("createThreadFamilyAtoms", () => {
78 + it("merges mounted rows behind shell rows until released", () => {
79 + const runtime = Atom.runtime(
80 + Layer.succeed(
81 + EnvironmentRegistry.EnvironmentRegistry,
82 + {} as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"],
83 + ),
84 + );
85 + const shellState = Atom.make<AsyncResult.AsyncResult<EnvironmentShellState>>(
86 + AsyncResult.success<EnvironmentShellState>({
87 + ...EMPTY_SHELL_STATE,
88 + status: "live",
89 + generation: 1,
90 + snapshot: Option.some({
91 + schemaVersion: 1,
92 + snapshotSequence: 9,
93 + projects: [],
94 + threads: [row("kept")],
95 + archivedThreads: [],
96 + olderSettled: { before: "2026-10-01T00:00:00.000Z", asOfSequence: 4, counts: [] },
97 + } as OrchestrationV2ShellSnapshot),
98 + removedAt: new Map([[ThreadId.make("removed"), 9]]),
99 + }),
100 + );
101 + const families = createThreadFamilyAtoms(runtime, () => shellState);
102 + const registry = AtomRegistry.make();
103 + const merged = () => registry.get(families.snapshotAtom(TARGET.environmentId))?.threads;
104 + registry.mount(families.snapshotAtom(TARGET.environmentId));
105 + // A settled shelf page read at sequence 5: the shell's copy wins, a later removal drops a row.
106 + const page = Atom.make<ReadonlyArray<OrchestrationV2ThreadFamily>>([
107 + { snapshotSequence: 5, threads: [row("kept", "page copy"), row("old"), row("removed")] },
108 + // Read before the latest authoritative snapshot (sequence 4), whose removals it cleared.
109 + { snapshotSequence: 3, threads: [row("stale-search-row")] },
110 + ]);
111 + const release = families.mountRows(registry, TARGET.environmentId, "older-settled:page", page);
112 + expect(merged()?.map(({ id, title }) => [id, title])).toEqual([
113 + ["kept", "kept"],
114 + ["old", "old"],
115 + ]);
116 + release();
117 + expect(merged()?.map(({ id }) => id)).toEqual(["kept"]);
118 + registry.dispose();
119 + });
120 +
121 it.effect("reads a family per shell generation and retries a failed read", () =>
122 Effect.scoped(
123 Effect.gen(function* () {
packages/client-runtime/src/state/threadFamilies.ts
+56 -23
@@ -19,7 +19,13 @@ import { createEnvironmentQueryAtomFamily, selectEnvironmentQueryData } from "./
19 import type { EnvironmentShellState } from "./shell.ts";
20
21 const EMPTY_FAMILY: OrchestrationV2ThreadFamily = { snapshotSequence: 0, threads: [] };
22 -const EMPTY_MOUNTS: ReadonlyMap<ThreadId, number> = new Map();
22 +const EMPTY_READS: ReadonlyArray<OrchestrationV2ThreadFamily> = [];
23 +
24 +interface MountedRows {
25 + readonly rows: Atom.Atom<ReadonlyArray<OrchestrationV2ThreadFamily>>;
26 + readonly count: number;
27 +}
28 +const EMPTY_MOUNTS: ReadonlyMap<string, MountedRows> = new Map();
29 // Generation 0 is the cached shell from before this session's first authoritative snapshot. A read
30 // for it would be thrown away once that snapshot lands, so it waits instead.
31 const AWAITING_AUTHORITATIVE_SHELL = AsyncResult.initial<never, never>(true);
@@ -60,9 +66,10 @@ export function threadFamilyRowsMissingFromShell(input: {
66 }
67
68 /**
63 - * Shells with `omitIdleSubagents` leave idle children to families: the subagent tree sharing a
64 - * `lineage.rootThreadId`, read once per shell generation and never persisted. Family rows merge
65 - * behind shell rows; shell deltas still carry every change, so a changed child's shell row wins.
69 + * Shells with `omitIdleSubagents` or `omitOlderSettled` leave rows to on-demand reads: a family is
70 + * every active thread sharing a `lineage.rootThreadId`, read once per shell generation; shelf pages
71 + * and search rows mount through `mountRows`. All merge behind shell rows, which win (deltas carry
72 + * every change), and are never persisted.
73 */
74 export function createThreadFamilyAtoms<R, ER, E>(
75 runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, ER>,
@@ -137,33 +144,58 @@ export function createThreadFamilyAtoms<R, ER, E>(
144 ),
145 );
146
140 - /** Keeps a thread's family in the merged rows until the returned release runs. */
141 - const mount = (
147 + const familyRowsAtom = Atom.family((key: string) =>
148 + Atom.make((get): ReadonlyArray<OrchestrationV2ThreadFamily> => {
149 + const family = selectEnvironmentQueryData(get(familyAtom(key)));
150 + return family === null ? EMPTY_READS : [family];
151 + }).pipe(Atom.withLabel(`environment-thread-family-rows:${key}`)),
152 + );
153 +
154 + /**
155 + * Merges `rows` behind the shell rows until the returned release runs. Reads read under `key`
156 + * share one entry; each read's `snapshotSequence` decides which removals drop its rows.
157 + */
158 + const mountRows = (
159 registry: AtomRegistry.AtomRegistry,
160 environmentId: EnvironmentId,
144 - threadId: ThreadId,
161 + key: string,
162 + rows: Atom.Atom<ReadonlyArray<OrchestrationV2ThreadFamily>>,
163 ): (() => void) => {
164 const atom = mountsAtom(environmentId);
165 const mounts = registry.get(atom);
148 - registry.set(atom, new Map(mounts).set(threadId, (mounts.get(threadId) ?? 0) + 1));
149 - // Mounting again after a failure retries instead of keeping the failure until reconnect.
150 - if (
151 - threadFamilyRead(registry.get(familyAtom(familyKey(environmentId, threadId)))) === "failed"
152 - ) {
153 - refresh(registry, environmentId, threadId);
154 - }
166 + const entry = mounts.get(key);
167 + registry.set(
168 + atom,
169 + new Map(mounts).set(key, { rows: entry?.rows ?? rows, count: (entry?.count ?? 0) + 1 }),
170 + );
171 let released = false;
172 return () => {
173 if (released) return;
174 released = true;
175 const next = new Map(registry.get(atom));
160 - const count = (next.get(threadId) ?? 0) - 1;
161 - if (count > 0) next.set(threadId, count);
162 - else next.delete(threadId);
176 + const entry = next.get(key);
177 + if (entry !== undefined && entry.count > 1)
178 + next.set(key, { ...entry, count: entry.count - 1 });
179 + else next.delete(key);
180 registry.set(atom, next);
181 };
182 };
183
184 + /** Keeps a thread's family in the merged rows until the returned release runs. */
185 + const mount = (
186 + registry: AtomRegistry.AtomRegistry,
187 + environmentId: EnvironmentId,
188 + threadId: ThreadId,
189 + ): (() => void) => {
190 + const key = familyKey(environmentId, threadId);
191 + const release = mountRows(registry, environmentId, `family:${key}`, familyRowsAtom(key));
192 + // Mounting again after a failure retries instead of keeping the failure until reconnect.
193 + if (threadFamilyRead(registry.get(familyAtom(key))) === "failed") {
194 + refresh(registry, environmentId, threadId);
195 + }
196 + return release;
197 + };
198 +
199 const snapshotAtom = Atom.family((environmentId: EnvironmentId) => {
200 let previous: {
201 readonly snapshot: OrchestrationV2ShellSnapshot;
@@ -176,12 +208,12 @@ export function createThreadFamilyAtoms<R, ER, E>(
208 const snapshot = state === null ? null : Option.getOrNull(state.snapshot);
209 const mounts = get(mountsAtom(environmentId));
210 if (snapshot === null || state === null || mounts.size === 0) return snapshot;
179 - const families = [...mounts.keys()].flatMap((threadId) => {
180 - const family = selectEnvironmentQueryData(
181 - get(familyAtom(familyKey(environmentId, threadId))),
182 - );
183 - return family === null ? [] : [family];
184 - });
211 + // A read from before the latest authoritative snapshot can hold threads removed before it,
212 + // and that snapshot cleared those removals; such reads (a cached search) wait for new ones.
213 + const cut = snapshot.olderSettled?.asOfSequence ?? 0;
214 + const families = [...mounts.values()].flatMap(({ rows }) =>
215 + get(rows).filter((read) => read.snapshotSequence >= cut),
216 + );
217 // Most shell updates move only the cursor. With the same rows, removals and families, keep
218 // the merged rows so thread lists skip their rebuild.
219 const unchanged =
@@ -219,6 +251,7 @@ export function createThreadFamilyAtoms<R, ER, E>(
251 familyAtom: (environmentId: EnvironmentId, threadId: ThreadId) =>
252 familyAtom(familyKey(environmentId, threadId)),
253 mount,
254 + mountRows,
255 refresh,
256 };
257 }
packages/client-runtime/src/state/threadSearch.test.ts
+50 -4
@@ -3,6 +3,8 @@ import {
3 ProjectId,
4 ThreadId,
5 type OrchestrationSearchThreadsResult,
6 + type OrchestrationV2SearchThreadsInput,
7 + type OrchestrationV2ThreadShell,
8 } from "@t3tools/contracts";
9 import * as Cause from "effect/Cause";
10 import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
@@ -55,15 +57,15 @@ it("ignores invalid search keys", () => {
57 return Atom.make(AsyncResult.success({ matches: [] }));
58 },
59 labelPrefix: "test:thread-search",
58 - });
60 + }).results;
61 const registry = AtomRegistry.make();
62
63 try {
64 const state = registry.get(searchAtom(makeThreadSearchKey([envA], "a".repeat(201))));
65 const malformedState = registry.get(searchAtom("not json{{"));
66
65 - expect(state).toEqual({ matches: [], isLoading: false });
66 - expect(malformedState).toEqual({ matches: [], isLoading: false });
67 + expect(state).toEqual({ matches: [], isLoading: false, rows: [] });
68 + expect(malformedState).toEqual({ matches: [], isLoading: false, rows: [] });
69 expect(searchCount).toBe(0);
70 } finally {
71 registry.dispose();
@@ -92,19 +94,63 @@ it("merges successful environments and silently ignores failures", () => {
94 ),
95 ),
96 labelPrefix: "test:thread-search",
95 - });
97 + }).results;
98 const registry = AtomRegistry.make();
99
100 const state = registry.get(searchAtom(makeThreadSearchKey([envB, envA], "needle")));
101 expect(state).toEqual({
102 matches: [{ ...result.matches[0], environmentId: envA }],
103 isLoading: false,
104 + rows: [],
105 });
106 expect(threadSearchMatchKey(state.matches[0]!)).toBe('["env-a","thread-a"]');
107
108 registry.dispose();
109 });
110
111 +it("asks environments that omit older settled threads for their rows, with the caller's scope", () => {
112 + const inputs: Array<readonly [EnvironmentId, OrchestrationV2SearchThreadsInput]> = [];
113 + const row = { id: ThreadId.make("thread-old") } as OrchestrationV2ThreadShell;
114 + const search = createThreadSearchResultsAtomFamily<Error>({
115 + getSearchAtom: (environmentId, input) => {
116 + inputs.push([environmentId, input]);
117 + return Atom.make(AsyncResult.success({ matches: [], snapshotSequence: 9, threads: [row] }));
118 + },
119 + olderSettledBefore: (environmentId) =>
120 + Atom.make(environmentId === envA ? "2026-10-01T00:00:00.000Z" : null),
121 + labelPrefix: "test:thread-search",
122 + });
123 + const registry = AtomRegistry.make();
124 + const scope = {
125 + projectIds: [ProjectId.make("project-a")],
126 + environmentLabels: { [envA]: "Laptop" },
127 + };
128 +
129 + // Too short to match messages: only the environment with older rows is asked, for titles.
130 + const key = makeThreadSearchKey([envA, envB], "ui", scope);
131 + expect(registry.get(search.results(key)).rows).toEqual([
132 + { environmentId: envA, read: { snapshotSequence: 9, threads: [row] } },
133 + ]);
134 + expect(registry.get(search.rows(key, envA))).toEqual([{ snapshotSequence: 9, threads: [row] }]);
135 + expect(inputs).toEqual([
136 + [
137 + envA,
138 + {
139 + query: "ui",
140 + olderSettledBefore: "2026-10-01T00:00:00.000Z",
141 + projectIds: scope.projectIds,
142 + environmentLabel: "Laptop",
143 + titlesOnly: true,
144 + },
145 + ],
146 + ]);
147 + inputs.length = 0;
148 + registry.get(search.results(makeThreadSearchKey([envB], "needle")));
149 + expect(inputs).toEqual([[envB, { query: "needle" }]]);
150 +
151 + registry.dispose();
152 +});
153 +
154 it("keeps the previous results visible only while a refined or widened query is pending", () => {
155 const previous = {
156 query: "websock",
packages/client-runtime/src/state/threadSearch.ts
+94 -13
@@ -1,14 +1,18 @@
1 import {
2 EnvironmentId,
3 OrchestrationSearchThreadsInput,
4 - type OrchestrationSearchThreadsResult,
4 + ProjectId,
5 type OrchestrationThreadSearchMatch,
6 + type OrchestrationV2SearchThreadsInput,
7 + type OrchestrationV2SearchThreadsResult,
8 + type OrchestrationV2ThreadFamily,
9 } from "@t3tools/contracts";
10 import * as Option from "effect/Option";
11 import * as Schema from "effect/Schema";
12 import { AsyncResult, Atom } from "effect/reactivity";
13 import {
14 foldSearchQuery,
15 + messageSearchTokens,
16 THREAD_OPEN_DEDUPE_MS,
17 type ThreadSearchSubject,
18 } from "@t3tools/shared/paletteSearch";
@@ -46,21 +50,46 @@ export interface EnvironmentThreadSearchMatch extends OrchestrationThreadSearchM
50 export interface ThreadSearchResultsState {
51 readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
52 readonly isLoading: boolean;
53 + /** Rows of matching older settled threads, which shells that omit them do not hold. */
54 + readonly rows: ReadonlyArray<{
55 + readonly environmentId: EnvironmentId;
56 + readonly read: OrchestrationV2ThreadFamily;
57 + }>;
58 }
59
60 +/**
61 + * How a search reaches environments whose shells omit older settled threads (others ignore it):
62 + * the caller's project scope and its names for environments, which its own ranking matches, and
63 + * whether to skip message search.
64 + */
65 +export const ThreadSearchScope = Schema.Struct({
66 + projectIds: Schema.optionalKey(Schema.Array(ProjectId)),
67 + environmentLabels: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
68 + titlesOnly: Schema.optionalKey(Schema.Boolean),
69 +});
70 +export type ThreadSearchScope = typeof ThreadSearchScope.Type;
71 +
72 const ThreadSearchKey = Schema.fromJsonString(
52 - Schema.Tuple([Schema.Array(EnvironmentId), OrchestrationSearchThreadsInput.fields.query]),
73 + Schema.Union([
74 + Schema.Tuple([Schema.Array(EnvironmentId), OrchestrationSearchThreadsInput.fields.query]),
75 + Schema.Tuple([
76 + Schema.Array(EnvironmentId),
77 + OrchestrationSearchThreadsInput.fields.query,
78 + ThreadSearchScope,
79 + ]),
80 + ]),
81 );
82 const decodeThreadSearchKey = Schema.decodeUnknownOption(ThreadSearchKey);
83 +const EMPTY_SEARCH_ROWS: ThreadSearchResultsState["rows"] = [];
84 +const EMPTY_READS: ReadonlyArray<OrchestrationV2ThreadFamily> = [];
85
86 export function makeThreadSearchKey(
87 environmentIds: ReadonlyArray<EnvironmentId>,
88 query: string,
89 + scope: ThreadSearchScope = {},
90 ): string {
60 - return JSON.stringify([
61 - [...environmentIds].sort((left, right) => left.localeCompare(right)),
62 - query,
63 - ]);
91 + const sorted = [...environmentIds].sort((left, right) => left.localeCompare(right));
92 + return JSON.stringify(Object.keys(scope).length === 0 ? [sorted, query] : [sorted, query, scope]);
93 }
94
95 function parseThreadSearchKey(key: string) {
@@ -73,36 +102,88 @@ export function threadSearchMatchKey(
102 return JSON.stringify([match.environmentId, match.threadId]);
103 }
104
105 +/**
106 + * Message matches from every environment, plus, from environments whose shells omit older settled
107 + * threads (`olderSettledBefore` is set), the rows of the best older matches. Callers mount those
108 + * rows (`rows`) so their own ranking sees them, as it sees shell rows.
109 + */
110 export function createThreadSearchResultsAtomFamily<E>(options: {
111 readonly getSearchAtom: (
112 environmentId: EnvironmentId,
79 - query: string,
80 - ) => Atom.Atom<AsyncResult.AsyncResult<OrchestrationSearchThreadsResult, E>>;
113 + input: OrchestrationV2SearchThreadsInput,
114 + ) => Atom.Atom<AsyncResult.AsyncResult<OrchestrationV2SearchThreadsResult, E>>;
115 + readonly olderSettledBefore?: (environmentId: EnvironmentId) => Atom.Atom<string | null>;
116 readonly labelPrefix: string;
117 }) {
83 - return Atom.family((key: string) =>
118 + const results = Atom.family((key: string) =>
119 Atom.make((get): ThreadSearchResultsState => {
120 const parsedKey = parseThreadSearchKey(key);
121 if (Option.isNone(parsedKey)) {
87 - return { matches: [], isLoading: false };
122 + return { matches: [], isLoading: false, rows: EMPTY_SEARCH_ROWS };
123 }
124
90 - const [environmentIds, query] = parsedKey.value;
125 + const [environmentIds, query, scope = {}] = parsedKey.value;
126 + // The server needs a word of three or more letters to match messages.
127 + const searchMessages =
128 + scope.titlesOnly !== true && messageSearchTokens(query).long.length > 0;
129 const matches: EnvironmentThreadSearchMatch[] = [];
130 + const rows: Array<ThreadSearchResultsState["rows"][number]> = [];
131 let isLoading = false;
132
133 for (const environmentId of environmentIds) {
95 - const result = get(options.getSearchAtom(environmentId, query));
134 + const before = options.olderSettledBefore
135 + ? get(options.olderSettledBefore(environmentId))
136 + : null;
137 + if (!searchMessages && before === null) continue;
138 + const label = scope.environmentLabels?.[environmentId];
139 + const result = get(
140 + options.getSearchAtom(environmentId, {
141 + query,
142 + ...(before === null
143 + ? {}
144 + : {
145 + olderSettledBefore: before,
146 + ...(scope.projectIds === undefined ? {} : { projectIds: scope.projectIds }),
147 + ...(label === undefined ? {} : { environmentLabel: label }),
148 + ...(searchMessages ? {} : { titlesOnly: true }),
149 + }),
150 + }),
151 + );
152 isLoading ||= result.waiting;
153 const value = Option.getOrNull(AsyncResult.value(result));
154 if (value !== null) {
155 matches.push(...value.matches.map((match) => ({ ...match, environmentId })));
156 + if (value.threads !== undefined && value.threads.length > 0) {
157 + rows.push({
158 + environmentId,
159 + read: { snapshotSequence: value.snapshotSequence ?? 0, threads: value.threads },
160 + });
161 + }
162 }
163 }
164
103 - return { matches, isLoading };
165 + return { matches, isLoading, rows: rows.length === 0 ? EMPTY_SEARCH_ROWS : rows };
166 }).pipe(Atom.withLabel(`${options.labelPrefix}:${key}`)),
167 );
168 + const rowsAtom = Atom.family((rowsKey: string) => {
169 + const [key, environmentId] = JSON.parse(rowsKey) as [string, EnvironmentId];
170 + let previous = EMPTY_READS;
171 + return Atom.make((get) => {
172 + const next = get(results(key)).rows.flatMap((row) =>
173 + row.environmentId === environmentId ? [row.read] : [],
174 + );
175 + if (next.length === previous.length && next.every((read, index) => read === previous[index]))
176 + return previous;
177 + previous = next.length === 0 ? EMPTY_READS : next;
178 + return previous;
179 + }).pipe(Atom.withLabel(`${options.labelPrefix}-rows:${rowsKey}`));
180 + });
181 + return {
182 + results,
183 + /** One environment's older settled rows for a search key, for `mountRows`. */
184 + rows: (key: string, environmentId: EnvironmentId) =>
185 + rowsAtom(JSON.stringify([key, environmentId])),
186 + };
187 }
188
189 export type ThreadSearchShell = Pick<
packages/client-runtime/src/state/threadSort.ts
+3 -1
@@ -58,7 +58,9 @@ export function sortSettledThreads<T extends SettledThreadTimestampInput & { rea
58 })
59 .sort(
60 (left, right) =>
61 - right.timestampMs - left.timestampMs || left.thread.id.localeCompare(right.thread.id),
61 + right.timestampMs - left.timestampMs ||
62 + // Plain order, as the server pages ties (ids mix upper and lower case).
63 + (left.thread.id < right.thread.id ? -1 : left.thread.id > right.thread.id ? 1 : 0),
64 )
65 .map(({ thread }) => thread);
66 }
packages/client-runtime/src/state/threads.ts
+1
@@ -1053,5 +1053,6 @@ export * from "./threadCommands.ts";
1053 export * from "./threadFeedback.ts";
1054 export * from "./threadDetail.ts";
1055 export * from "./threadFamilies.ts";
1056 +export * from "./olderSettledThreads.ts";
1057 export * from "./threadShell.ts";
1058 export * from "./threadState.ts";
packages/contracts/src/environmentHttp.ts
+1
@@ -669,6 +669,7 @@ const EnvironmentOrchestrationThreadSnapshotParams = Schema.Struct({
669
670 const EnvironmentOrchestrationShellSnapshotQuery = Schema.Struct({
671 omitIdleSubagents: Schema.optionalKey(Schema.String),
672 + omitOlderSettled: Schema.optionalKey(Schema.String),
673 });
674
675 const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
packages/contracts/src/orchestrationV2.ts
+107 -3
@@ -1,5 +1,6 @@
1 import { OrchestrationMessageContext } from "./composerContext.ts";
2 import { withRecordIdTable } from "./orchestrationV2IdTable.ts";
3 +import * as DateTime from "effect/DateTime";
4 import * as Effect from "effect/Effect";
5 import * as Schema from "effect/Schema";
6 import * as SchemaAST from "effect/SchemaAST";
@@ -41,6 +42,10 @@ import {
42 } from "./baseSchemas.ts";
43 import { ChatAttachment } from "./chatAttachment.ts";
44 import { ThreadGuestShare } from "./guestShare.ts";
45 +import {
46 + OrchestrationSearchThreadsInput,
47 + OrchestrationSearchThreadsResult,
48 +} from "./threadSearch.ts";
49 import {
50 OrchestrationGetFullThreadDiffInput,
51 OrchestrationGetFullThreadDiffResult,
@@ -1992,6 +1997,40 @@ export function isLiveSubagentShell(
1997 );
1998 }
1999
2000 +/**
2001 + * A settled top-level thread settled before `beforeMs`. Shells that omit older settled threads
2002 + * count every such thread, so the server and clients share this one rule.
2003 + */
2004 +export function isOlderSettledThreadShell(
2005 + thread: Pick<OrchestrationV2ThreadShell, "lineage" | "settledOverride" | "settledAt">,
2006 + beforeMs: number,
2007 +): boolean {
2008 + return (
2009 + thread.lineage.relationshipToParent !== "subagent" &&
2010 + thread.settledOverride === "settled" &&
2011 + thread.settledAt !== null &&
2012 + DateTime.toEpochMillis(thread.settledAt) < beforeMs
2013 + );
2014 +}
2015 +
2016 +export const OrchestrationV2OlderSettledCount = Schema.Struct({
2017 + projectId: ProjectId,
2018 + count: NonNegativeInt,
2019 +});
2020 +export type OrchestrationV2OlderSettledCount = typeof OrchestrationV2OlderSettledCount.Type;
2021 +
2022 +/**
2023 + * Snapshots that leave out older settled threads (`omitOlderSettled`) name the cut and count, per
2024 + * project, every thread `isOlderSettledThreadShell` matches, sent or not. `asOfSequence` is the
2025 + * snapshot's own sequence: the counts are exact until a later change.
2026 + */
2027 +export const OrchestrationV2OlderSettledThreads = Schema.Struct({
2028 + before: IsoDateTime,
2029 + asOfSequence: NonNegativeInt,
2030 + counts: Schema.Array(OrchestrationV2OlderSettledCount),
2031 +});
2032 +export type OrchestrationV2OlderSettledThreads = typeof OrchestrationV2OlderSettledThreads.Type;
2033 +
2034 export const OrchestrationV2ThreadShellSnapshot = Schema.Struct({
2035 schemaVersion: PositiveInt,
2036 snapshotSequence: NonNegativeInt,
@@ -2003,6 +2042,7 @@ export type OrchestrationV2ThreadShellSnapshot = typeof OrchestrationV2ThreadShe
2042 export const OrchestrationV2ShellSnapshot = Schema.Struct({
2043 ...OrchestrationV2ThreadShellSnapshot.fields,
2044 projects: Schema.Array(OrchestrationProjectShell),
2045 + olderSettled: Schema.optionalKey(OrchestrationV2OlderSettledThreads),
2046 });
2047 export type OrchestrationV2ShellSnapshot = typeof OrchestrationV2ShellSnapshot.Type;
2048
@@ -3252,6 +3292,7 @@ export const ORCHESTRATION_V2_WS_METHODS = {
3292 getArchivedShellSnapshot: "orchestration.getArchivedShellSnapshot",
3293 getThreadFamily: "orchestration.getThreadFamily",
3294 getWorktreeThreadIds: "orchestration.getWorktreeThreadIds",
3295 + getOlderSettledThreads: "orchestration.getOlderSettledThreads",
3296 getThreadProjection: "orchestration.getThreadProjection",
3297 getWorkflowScript: "orchestration.getWorkflowScript",
3298 stopWorkflow: "orchestration.stopWorkflow",
@@ -3366,6 +3407,11 @@ export const OrchestrationV2SubscribeShellInput = Schema.Struct({
3407 * servers that advertise `shellThreadFamilies`.
3408 */
3409 omitIdleSubagents: Schema.optionalKey(Schema.Boolean),
3410 + /**
3411 + * Snapshots leave out older settled threads and count them in `olderSettled`; read them with
3412 + * `getOlderSettledThreads`. Send only to servers that advertise `shellOlderSettled`.
3413 + */
3414 + omitOlderSettled: Schema.optionalKey(Schema.Boolean),
3415 });
3416 export type OrchestrationV2SubscribeShellInput = typeof OrchestrationV2SubscribeShellInput.Type;
3417
@@ -3375,9 +3421,9 @@ export const OrchestrationV2GetThreadFamilyInput = Schema.Struct({
3421 export type OrchestrationV2GetThreadFamilyInput = typeof OrchestrationV2GetThreadFamilyInput.Type;
3422
3423 /**
3378 - * The active subagent threads that share the requested thread's `lineage.rootThreadId`: its whole
3379 - * subagent tree, in any run state. An unknown thread has an empty family; a deleted one still
3380 - * names its root.
3424 + * The active threads that share the requested thread's `lineage.rootThreadId`: the root, its
3425 + * forks, and their whole subagent trees, in any run state. An unknown thread has an empty family;
3426 + * a deleted one still names its root.
3427 */
3428 export const OrchestrationV2ThreadFamily = Schema.Struct({
3429 snapshotSequence: NonNegativeInt,
@@ -3391,6 +3437,60 @@ export const OrchestrationV2GetWorktreeThreadIdsInput = Schema.Struct({
3437 export type OrchestrationV2GetWorktreeThreadIdsInput =
3438 typeof OrchestrationV2GetWorktreeThreadIdsInput.Type;
3439
3440 +/**
3441 + * The client's thread search. With `olderSettledBefore`, the result also carries the rows of the
3442 + * best matching threads settled before it, which shells that omit older settled threads leave out:
3443 + * title, branch, pull request and project matches (in `projectIds` when given), then message hits.
3444 + * Send the extra keys only to servers that advertise `shellOlderSettled`.
3445 + */
3446 +export const OrchestrationV2SearchThreadsInput = Schema.Struct({
3447 + ...OrchestrationSearchThreadsInput.fields,
3448 + olderSettledBefore: Schema.optionalKey(IsoDateTime),
3449 + projectIds: Schema.optionalKey(Schema.Array(ProjectId)),
3450 + /** The client's name for this environment, which its own ranking also matches. */
3451 + environmentLabel: Schema.optionalKey(Schema.String),
3452 + /** Skips message search, for pickers that match titles only. */
3453 + titlesOnly: Schema.optionalKey(Schema.Boolean),
3454 +});
3455 +export type OrchestrationV2SearchThreadsInput = typeof OrchestrationV2SearchThreadsInput.Type;
3456 +
3457 +export const OrchestrationV2SearchThreadsResult = Schema.Struct({
3458 + ...OrchestrationSearchThreadsResult.fields,
3459 + snapshotSequence: Schema.optionalKey(NonNegativeInt),
3460 + threads: Schema.optionalKey(Schema.Array(OrchestrationV2ThreadShell)),
3461 +});
3462 +export type OrchestrationV2SearchThreadsResult = typeof OrchestrationV2SearchThreadsResult.Type;
3463 +
3464 +/** Where a page of older settled threads starts: below this settle time, then after this id. */
3465 +export const OrchestrationV2OlderSettledCursor = Schema.Struct({
3466 + settledAt: IsoDateTime,
3467 + threadId: Schema.optionalKey(ThreadId),
3468 +});
3469 +export type OrchestrationV2OlderSettledCursor = typeof OrchestrationV2OlderSettledCursor.Type;
3470 +
3471 +export const OrchestrationV2GetOlderSettledThreadsInput = Schema.Struct({
3472 + before: OrchestrationV2OlderSettledCursor,
3473 + /** Every project when absent. */
3474 + projectIds: Schema.optionalKey(Schema.Array(ProjectId)),
3475 + /** 0 reads only the counts. */
3476 + limit: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 50 })),
3477 +});
3478 +export type OrchestrationV2GetOlderSettledThreadsInput =
3479 + typeof OrchestrationV2GetOlderSettledThreadsInput.Type;
3480 +
3481 +/**
3482 + * Older settled threads below the cursor, newest settle first (`settledAt` desc, then id asc). A
3483 + * `limit: 0` read fills `counts` instead: the older settled threads below the cursor per project,
3484 + * as `OrchestrationV2OlderSettledThreads.counts` count them. A cursor without an id is strict.
3485 + */
3486 +export const OrchestrationV2OlderSettledThreadsPage = Schema.Struct({
3487 + snapshotSequence: NonNegativeInt,
3488 + threads: Schema.Array(OrchestrationV2ThreadShell),
3489 + counts: Schema.Array(OrchestrationV2OlderSettledCount),
3490 +});
3491 +export type OrchestrationV2OlderSettledThreadsPage =
3492 + typeof OrchestrationV2OlderSettledThreadsPage.Type;
3493 +
3494 /** Active threads on a worktree path, including subagent children a shell may leave out. */
3495 export const OrchestrationV2WorktreeThreadIds = Schema.Struct({
3496 threadIds: Schema.Array(ThreadId),
@@ -3783,6 +3883,10 @@ export const OrchestrationV2RpcSchemas = {
3883 input: OrchestrationV2GetWorktreeThreadIdsInput,
3884 output: OrchestrationV2WorktreeThreadIds,
3885 },
3886 + getOlderSettledThreads: {
3887 + input: OrchestrationV2GetOlderSettledThreadsInput,
3888 + output: OrchestrationV2OlderSettledThreadsPage,
3889 + },
3890 getThreadProjection: {
3891 input: OrchestrationV2GetThreadProjectionInput,
3892 output: OrchestrationV2ThreadProjection,
packages/contracts/src/rpc.ts
+15 -7
@@ -2,6 +2,8 @@ import {
2 OrchestrationV2SearchThreadError,
3 OrchestrationV2SearchThreadInput,
4 OrchestrationV2SearchThreadResult,
5 + OrchestrationV2SearchThreadsInput,
6 + OrchestrationV2SearchThreadsResult,
7 } from "./orchestrationV2.ts";
8 import { OrchestrationDispatchCommandError } from "./orchestrationDispatch.ts";
9 import {
@@ -150,11 +152,7 @@ import {
152 ReviewDiffPreviewResult,
153 } from "./review.ts";
154 import { KeybindingsConfigError } from "./keybindings.ts";
153 -import {
154 - OrchestrationSearchThreadsError,
155 - OrchestrationSearchThreadsInput,
156 - OrchestrationSearchThreadsResult,
157 -} from "./threadSearch.ts";
155 +import { OrchestrationSearchThreadsError } from "./threadSearch.ts";
156 import {
157 ProviderUploadFeedbackError,
158 ProviderUploadFeedbackInput,
@@ -1661,8 +1659,8 @@ const WsOrchestrationV2SearchThreadStreamRpc = Rpc.make(
1659 );
1660
1661 const WsOrchestrationV2SearchThreadsRpc = Rpc.make(ORCHESTRATION_V2_WS_METHODS.searchThreads, {
1664 - payload: OrchestrationSearchThreadsInput,
1665 - success: OrchestrationSearchThreadsResult,
1662 + payload: OrchestrationV2SearchThreadsInput,
1663 + success: OrchestrationV2SearchThreadsResult,
1664 error: Schema.Union([OrchestrationSearchThreadsError, EnvironmentAuthorizationError]),
1665 });
1666
@@ -1690,6 +1688,15 @@ const WsOrchestrationV2GetWorktreeThreadIdsRpc = Rpc.make(
1688 },
1689 );
1690
1691 +const WsOrchestrationV2GetOlderSettledThreadsRpc = Rpc.make(
1692 + ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads,
1693 + {
1694 + payload: OrchestrationV2RpcSchemas.getOlderSettledThreads.input,
1695 + success: OrchestrationV2RpcSchemas.getOlderSettledThreads.output,
1696 + error: Schema.Union([OrchestrationV2GetShellSnapshotError, EnvironmentAuthorizationError]),
1697 + },
1698 +);
1699 +
1700 const WsOrchestrationV2GetThreadProjectionRpc = Rpc.make(
1701 ORCHESTRATION_V2_WS_METHODS.getThreadProjection,
1702 {
@@ -2092,6 +2099,7 @@ export const WsRpcGroup = RpcGroup.make(
2099 WsOrchestrationV2GetArchivedShellSnapshotRpc,
2100 WsOrchestrationV2GetThreadFamilyRpc,
2101 WsOrchestrationV2GetWorktreeThreadIdsRpc,
2102 + WsOrchestrationV2GetOlderSettledThreadsRpc,
2103 WsOrchestrationV2GetThreadProjectionRpc,
2104 WsOrchestrationV2LaunchThreadRpc,
2105 WsOrchestrationV2SubscribeArchivedShellRpc,
packages/contracts/src/server.ts
+5
@@ -599,6 +599,11 @@ export const ServerConfig = Schema.Struct({
599 * `getThreadFamily` and `getWorktreeThreadIds` serve the rows it leaves out.
600 */
601 shellThreadFamilies: Schema.optionalKey(Schema.Boolean),
602 + /**
603 + * The shell can leave older settled threads out of snapshots (`omitOlderSettled`), and
604 + * `getOlderSettledThreads` serves them. Only servers that also advertise `shellThreadFamilies`.
605 + */
606 + shellOlderSettled: Schema.optionalKey(Schema.Boolean),
607 shellRevealInFileManager: Schema.optionalKey(Schema.Boolean),
608 shellRevealInFileManagerKind: Schema.optionalKey(FileManagerRevealKind),
609 threadResumeCompletionMarker: Schema.optionalKey(Schema.Boolean),