Skip to content

Commit bbd678e

Browse files
saphidclaude
andcommitted
fix(mobile): threads on an unreachable environment no longer hide in the Working shelf
With the Working shelf on, a thread whose environment stopped answering kept its last cached running state, so it stayed folded into the collapsed shelf indefinitely and a broken thread was invisible. Past a first connection attempt (the rule thread-route hydration uses), the list treats the environment as disconnected and lists its threads in the inbox instead of the shelf until it reconnects. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fc9e5f commit bbd678e

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎apps/mobile/src/features/threads/threadListV2.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
buildThreadListV2Items,
3535
buildThreadListV2ListItems,
3636
getThreadListV2OrderedSection,
37+
isThreadListV2EnvironmentDisconnected,
3738
resolveThreadListV2SnoozeMenuSelection,
3839
resolveThreadListV2SnoozePresets,
3940
resolveThreadListV2Status,
@@ -154,6 +155,20 @@ describe("resolveThreadListV2Status", () => {
154155
);
155156
});
156157

158+
describe("isThreadListV2EnvironmentDisconnected", () => {
159+
it.each([
160+
["connected", false],
161+
["connecting", false],
162+
["reconnecting", true],
163+
["offline", true],
164+
["error", true],
165+
["unsupported", true],
166+
["available", true],
167+
] as const)("treats %s as disconnected: %s", (connectionState, disconnected) => {
168+
expect(isThreadListV2EnvironmentDisconnected(connectionState)).toBe(disconnected);
169+
});
170+
});
171+
157172
describe("queued messages keep a settled thread active", () => {
158173
const threads = [
159174
makeThread({ id: ThreadId.make("active"), title: "Active" }),
@@ -1648,6 +1663,23 @@ describe("Working section beta", () => {
16481663
}
16491664
});
16501665

1666+
it("lists a working thread on a disconnected environment outside the shelf", () => {
1667+
const unreachableId = EnvironmentId.make("environment-unreachable");
1668+
const unreachable = makeThreadShellFixture({
1669+
environmentId: unreachableId,
1670+
id: ThreadId.make("unreachable"),
1671+
title: "unreachable",
1672+
runtime: running,
1673+
});
1674+
const layout = build({
1675+
threads: [...threads, unreachable],
1676+
disconnectedEnvironmentIds: new Set([unreachableId]),
1677+
});
1678+
1679+
expect(ids(layout)).toContain("unreachable");
1680+
expect(layout.workingCount).toBe(1);
1681+
});
1682+
16511683
it("orders the inbox by the latest return this device observed", () => {
16521684
const layout = build({
16531685
inboxReturnAt: (thread) =>

‎apps/mobile/src/features/threads/threadListV2.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
sortPinnedThreadsByOrderKey,
2828
sortSettledThreads,
2929
} from "@t3tools/client-runtime/state/thread-sort";
30+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
3031
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
3132

3233
import type { ThreadListProvider } from "../../state/thread-list-environments";
@@ -196,6 +197,16 @@ export function resolveThreadListV2WokeAt(
196197
: null;
197198
}
198199

200+
/**
201+
* Past a first connection attempt the environment is not answering, so its threads' last run
202+
* states are stale. Matches `threadRouteIsHydrating`.
203+
*/
204+
export function isThreadListV2EnvironmentDisconnected(
205+
connectionState: EnvironmentConnectionPhase,
206+
): boolean {
207+
return connectionState !== "connected" && connectionState !== "connecting";
208+
}
209+
199210
export function resolveThreadListV2Status(
200211
thread: Pick<
201212
EnvironmentThreadShell,
@@ -601,6 +612,7 @@ export function buildThreadListV2Items(input: {
601612
readonly matchedThreadKeys?: ReadonlySet<string>;
602613
readonly settlementEnvironmentIds?: ReadonlySet<EnvironmentId>;
603614
readonly snoozeEnvironmentIds?: ReadonlySet<EnvironmentId>;
615+
readonly disconnectedEnvironmentIds?: ReadonlySet<string>;
604616
readonly settledLimit?: number;
605617
readonly now: string;
606618
readonly workingShelfEnabled?: boolean;
@@ -642,6 +654,8 @@ export function buildThreadListV2Items(input: {
642654
const snoozed: EnvironmentThreadShell[] = [];
643655
let nextSnoozeWakeAt: string | null = null;
644656
const queuedThreadKeys = input.queuedThreadKeys?.size ? input.queuedThreadKeys : undefined;
657+
const isDisconnected = (thread: EnvironmentThreadShell) =>
658+
input.disconnectedEnvironmentIds?.has(thread.environmentId) === true;
645659
const listedThreads = input.threads.filter(
646660
(thread) =>
647661
thread.archivedAt === null &&
@@ -692,7 +706,12 @@ export function buildThreadListV2Items(input: {
692706
settled.push(thread);
693707
} else if (thread.pinnedAt != null) {
694708
pinned.push(thread);
695-
} else if (workingShelfEnabled && (input.isWorking ?? isThreadWorking)(thread)) {
709+
} else if (
710+
workingShelfEnabled &&
711+
// A shelf the user collapsed must not hide a thread whose progress is unknown.
712+
!isDisconnected(thread) &&
713+
(input.isWorking ?? isThreadWorking)(thread)
714+
) {
696715
working.push(thread);
697716
} else {
698717
active.push(thread);

‎apps/mobile/src/features/threads/useThreadListV2.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { useMemo, useSyncExternalStore } from "react";
55
import { scopedProjectKey } from "../../lib/scopedEntities";
66
import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
77
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
8+
import { useWorkspaceEnvironments } from "../../state/workspace";
89
import {
910
buildThreadListV2Items,
1011
buildThreadListV2ListItems,
1112
getThreadListV2OrderedSection,
13+
isThreadListV2EnvironmentDisconnected,
1214
threadListInboxReturns,
1315
} from "./threadListV2";
1416
import { computeThreadMoveAvailability } from "./threadOrder";
@@ -66,6 +68,13 @@ export function useThreadListV2(input: {
6668
// oxlint-disable-next-line react/memo-dependencies -- the version signals an expired grace
6769
[threads, workingShelfEnabled, inboxVersion],
6870
);
71+
const disconnectedEnvironmentIds = useKeySet(
72+
useWorkspaceEnvironments()
73+
.filter((environment) => isThreadListV2EnvironmentDisconnected(environment.connectionState))
74+
.map((environment) => environment.environmentId)
75+
.sort()
76+
.join("\n"),
77+
);
6978
// Up/down menu availability for every card, computed once per section per
7079
// rebuild (see computeThreadMoveAvailability): per-thread planner calls made
7180
// list construction quadratic, and this list rebuilds on every minute tick.
@@ -107,6 +116,7 @@ export function useThreadListV2(input: {
107116
matchedThreadKeys: new Set(input.searchMatches.keys()),
108117
settlementEnvironmentIds: input.settlementEnvironmentIds,
109118
snoozeEnvironmentIds: input.snoozeEnvironmentIds,
119+
disconnectedEnvironmentIds,
110120
queuedThreadKeys: input.queuedThreadKeys,
111121
settledLimit: input.settledLimit,
112122
now: input.now,

0 commit comments

Comments
 (0)