Skip to content

Commit 9d0b415

Browse files
fix(mobile): prompt links reach archived threads and never land over another screen
- A thread link also counts its thread as shown when the open screen has loaded the thread's detail, so archived threads (which have no shell) get their draft. Detail is read only while the screen already holds it. - A newer thread link clears a waiting one before it is validated, so an invalid link no longer lets an earlier one apply. - A new-task link needs New task to be its navigator's focused route; pushing another screen above it drops the link. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent db8152b commit 9d0b415

5 files changed

Lines changed: 115 additions & 21 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export function takeNewTaskPromptLink(
108108
export function resolvePendingNewTaskPromptLink(state: NavigationState | undefined): void {
109109
if (pendingLink === null) return;
110110
const step = stepPendingPromptLink(pendingLink, {
111-
screenOnStack: findStackRoute(state, NEW_TASK_ROUTE) !== undefined,
111+
// A screen pushed above New task (such as Add project) drops the link, so it never lands
112+
// over the wrong screen.
113+
screenOnStack: findStackRoute(state, NEW_TASK_ROUTE)?.focused === true,
112114
ready: appAtomRegistry.get(shellsSettledAtom),
113115
});
114116
if (step.pending === null) dropPendingLink();

‎apps/mobile/src/features/prompt-links/threadPromptLink.test.ts‎

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { NavigationState, PartialState } from "@react-navigation/native";
2-
import { Atom } from "effect/reactivity";
2+
import * as Option from "effect/Option";
3+
import { AsyncResult, Atom } from "effect/reactivity";
34
import { describe, expect, it, vi } from "vite-plus/test";
45

56
const fixture = vi.hoisted(() => ({
67
applied: [] as { draftKey: string; text: string }[],
78
root: undefined as NavigationState | undefined,
9+
detailOpens: 0,
810
}));
911

1012
vi.mock("../../components/ui/Dialog", () => ({ alert: () => {} }));
@@ -21,27 +23,48 @@ vi.mock("../../state/atom-registry", async () => {
2123
return { appAtomRegistry: AtomRegistry.make() };
2224
});
2325
vi.mock("../../state/threads", async () => {
24-
const { Atom } = await import("effect/reactivity");
26+
const { AsyncResult, Atom } = await import("effect/reactivity");
27+
const Option = await import("effect/Option");
2528
const shell = Atom.make<object | null>(null);
26-
return { environmentThreadShells: { threadShellAtom: () => shell }, fixtureShell: shell };
29+
const detailSource = Atom.make(AsyncResult.success({ data: Option.none() }));
30+
// Computing the real detail atom opens the thread's server stream.
31+
const detail = Atom.make((get) => {
32+
fixture.detailOpens += 1;
33+
return get(detailSource);
34+
});
35+
return {
36+
environmentThreadShells: { threadShellAtom: () => shell },
37+
environmentThreads: { stateAtom: () => detail },
38+
fixtureShell: shell,
39+
fixtureDetail: detail,
40+
fixtureDetailSource: detailSource,
41+
};
2742
});
2843

2944
const { appAtomRegistry } = await import("../../state/atom-registry");
30-
const { fixtureShell } = (await import("../../state/threads")) as unknown as {
31-
fixtureShell: Atom.Writable<object | null>;
32-
};
45+
const { fixtureDetail, fixtureDetailSource, fixtureShell } =
46+
(await import("../../state/threads")) as unknown as {
47+
fixtureShell: Atom.Writable<object | null>;
48+
fixtureDetail: Atom.Atom<unknown>;
49+
fixtureDetailSource: Atom.Writable<AsyncResult.AsyncResult<{ data: Option.Option<object> }>>;
50+
};
3351
const { resolvePendingThreadPromptLink, takeThreadPromptLink } = await import("./threadPromptLink");
3452

3553
function setup() {
3654
fixture.applied.length = 0;
3755
fixture.root = undefined;
56+
appAtomRegistry.reset();
3857
appAtomRegistry.set(fixtureShell, null);
58+
appAtomRegistry.set(fixtureDetailSource, AsyncResult.success({ data: Option.none() }));
59+
fixture.detailOpens = 0;
3960
}
4061

62+
const loadedDetail = AsyncResult.success({ data: Option.some({}) });
63+
4164
const threadParams = { environmentId: "env-1", threadId: "thread-1" };
4265

43-
function linkedState(): PartialState<NavigationState> {
44-
return { routes: [{ name: "Thread", params: { ...threadParams, prompt: "Fix the bug" } }] };
66+
function linkedState(prompt = "Fix the bug"): PartialState<NavigationState> {
67+
return { routes: [{ name: "Thread", params: { ...threadParams, prompt } }] };
4568
}
4669

4770
function stack(routes: NavigationState["routes"]): NavigationState {
@@ -84,4 +107,38 @@ describe("takeThreadPromptLink", () => {
84107
appAtomRegistry.set(fixtureShell, { id: "thread-1", title: "renamed" });
85108
expect(fixture.applied).toHaveLength(1);
86109
});
110+
111+
it("fills the draft of an archived thread its screen shows from detail", () => {
112+
setup();
113+
takeThreadPromptLink(linkedState());
114+
fixture.root = threadRoute;
115+
// Archived threads have no shell; the open screen subscribes to their detail instead.
116+
using _screen = { [Symbol.dispose]: appAtomRegistry.mount(fixtureDetail) };
117+
resolvePendingThreadPromptLink(threadRoute);
118+
expect(fixture.applied).toEqual([]);
119+
120+
appAtomRegistry.set(fixtureDetailSource, loadedDetail);
121+
expect(fixture.applied).toEqual([{ draftKey: expect.any(String), text: "Fix the bug" }]);
122+
});
123+
124+
it("opens no detail for a thread no screen has loaded", () => {
125+
setup();
126+
takeThreadPromptLink(linkedState());
127+
fixture.root = threadRoute;
128+
resolvePendingThreadPromptLink(threadRoute);
129+
appAtomRegistry.set(fixtureDetailSource, loadedDetail);
130+
expect(fixture.detailOpens).toBe(0);
131+
expect(fixture.applied).toEqual([]);
132+
resolvePendingThreadPromptLink(stack([{ key: "home", name: "Home" }]));
133+
});
134+
135+
it("drops an earlier link to the thread when a newer one is invalid", () => {
136+
setup();
137+
takeThreadPromptLink(linkedState());
138+
takeThreadPromptLink(linkedState("\u200b"));
139+
fixture.root = threadRoute;
140+
resolvePendingThreadPromptLink(threadRoute);
141+
appAtomRegistry.set(fixtureShell, { id: "thread-1" });
142+
expect(fixture.applied).toEqual([]);
143+
});
87144
});

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import * as Dialog from "../../components/ui/Dialog";
22
import type { NavigationState, PartialState } from "@react-navigation/native";
33
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
44
import { parseThreadLinkPrompt, type LinkedPrompt } from "@t3tools/shared/promptLinks";
5+
import * as Option from "effect/Option";
6+
import { AsyncResult } from "effect/reactivity";
57

68
import { findStackRoute } from "../../lib/appLinking";
79
import { rootNavigationRef } from "../../lib/rootNavigation";
810
import { scopedThreadKey } from "../../lib/scopedEntities";
911
import { appAtomRegistry } from "../../state/atom-registry";
10-
import { environmentThreadShells } from "../../state/threads";
12+
import { environmentThreadShells, environmentThreads } from "../../state/threads";
1113
import { applyLinkedPromptToThreadDraft } from "./prompt-link-actions";
1214
import {
1315
promptLinkNotice,
@@ -25,7 +27,8 @@ function firstString(value: unknown): string | null {
2527
}
2628

2729
let pendingLink: PendingThreadLink | null = null;
28-
let stopWaitingForThread: (() => void) | null = null;
30+
let stopWaitingForShell: (() => void) | null = null;
31+
let stopWaitingForDetail: (() => void) | null = null;
2932

3033
/**
3134
* `threads/<environment>/<thread>?prompt=…` fills that thread's composer draft. The param is
@@ -42,6 +45,8 @@ export function takeThreadPromptLink(
4245
const params = route?.params as Readonly<Record<string, unknown>> | undefined;
4346
const search = promptLinkSearchFromParams(params, THREAD_LINK_KEYS);
4447
if (!route || !params || search === null) return state;
48+
// A newer link replaces a waiting one even when it turns out to be invalid.
49+
dropPendingLink();
4550
const { prompt: _prompt, ...rest } = params;
4651
const routes = state.routes.slice();
4752
routes[index] = { ...route, params: rest };
@@ -55,7 +60,6 @@ export function takeThreadPromptLink(
5560
Dialog.alert("Could not open link", parsed.error);
5661
return nextState;
5762
}
58-
dropPendingLink();
5963
if (parsed.value !== null) {
6064
pendingLink = {
6165
ref: { environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId) },
@@ -69,9 +73,13 @@ export function takeThreadPromptLink(
6973
export function resolvePendingThreadPromptLink(state: NavigationState | undefined): void {
7074
if (pendingLink === null) return;
7175
const { ref } = pendingLink;
72-
// Every thread the screen can show gets a shell: for a subagent thread the environment's list
73-
// leaves out, the screen mounts its family, which adds it.
76+
// A subagent thread the environment's list leaves out gets a shell once the screen mounts its
77+
// family. An archived thread never does; the screen shows it from its detail.
7478
const shellAtom = environmentThreadShells.threadShellAtom(ref);
79+
const detailAtom = environmentThreads.stateAtom(ref.environmentId, ref.threadId);
80+
// Read detail only while the screen holds it: opening it here for an unknown thread would keep
81+
// retrying with no screen to close it.
82+
const screenHoldsDetail = appAtomRegistry.getNodes().has(detailAtom);
7583
const step = stepPendingPromptLink(pendingLink, {
7684
screenOnStack:
7785
findStackRoute(
@@ -81,7 +89,9 @@ export function resolvePendingThreadPromptLink(state: NavigationState | undefine
8189
firstString(params?.environmentId) === ref.environmentId &&
8290
firstString(params?.threadId) === ref.threadId,
8391
) !== undefined,
84-
ready: appAtomRegistry.get(shellAtom) !== null,
92+
ready:
93+
appAtomRegistry.get(shellAtom) !== null ||
94+
(screenHoldsDetail && detailLoaded(appAtomRegistry.get(detailAtom))),
8595
});
8696
if (step.pending === null) dropPendingLink();
8797
if (step.resolve !== null) {
@@ -92,14 +102,27 @@ export function resolvePendingThreadPromptLink(state: NavigationState | undefine
92102
promptLinkNotice({ hiddenCharacters: prompt.hiddenCharacters }),
93103
);
94104
} else if (step.pending !== null) {
95-
stopWaitingForThread ??= appAtomRegistry.subscribe(shellAtom, (shell) => {
105+
stopWaitingForShell ??= appAtomRegistry.subscribe(shellAtom, (shell) => {
96106
if (shell !== null) resolvePendingThreadPromptLink(rootNavigationRef.getRootState());
97107
});
108+
if (screenHoldsDetail) {
109+
stopWaitingForDetail ??= appAtomRegistry.subscribe(detailAtom, (detail) => {
110+
if (detailLoaded(detail)) resolvePendingThreadPromptLink(rootNavigationRef.getRootState());
111+
});
112+
}
98113
}
99114
}
100115

116+
function detailLoaded(
117+
result: AsyncResult.AsyncResult<{ readonly data: Option.Option<unknown> }, unknown>,
118+
): boolean {
119+
return Option.isSome(Option.flatMap(AsyncResult.value(result), (state) => state.data));
120+
}
121+
101122
function dropPendingLink(): void {
102123
pendingLink = null;
103-
stopWaitingForThread?.();
104-
stopWaitingForThread = null;
124+
stopWaitingForShell?.();
125+
stopWaitingForShell = null;
126+
stopWaitingForDetail?.();
127+
stopWaitingForDetail = null;
105128
}

‎apps/mobile/src/lib/appLinking.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,19 @@ describe("findStackRoute", () => {
157157
expect(findStackRoute(root, "NewTaskProject")).toEqual({
158158
navigatorKey: "settings-stack",
159159
routeKey: "new-task",
160+
focused: true,
160161
});
161162
});
162163

164+
it("reports whether the screen is its navigator's focused route", () => {
165+
const covered = stack("root", [
166+
{ key: "new-task", name: "NewTaskProject" },
167+
{ key: "add-project", name: "AddProject" },
168+
]);
169+
expect(findStackRoute(covered, "NewTaskProject")?.focused).toBe(false);
170+
expect(findStackRoute(covered, "AddProject")?.focused).toBe(true);
171+
});
172+
163173
it("prefers the top-most screen and reports a missing one", () => {
164174
const root = stack("root", [
165175
{ key: "first", name: "NewTaskProject" },

‎apps/mobile/src/lib/appLinking.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ export function findLinkedRoute(
6363
/**
6464
* The last route named `name` (and accepted by `accept`, when given) at any depth of a live
6565
* navigation state, with the key of the navigator holding it, so an action can be dispatched as
66-
* that screen would.
66+
* that screen would. `focused` says whether it is that navigator's current route.
6767
*/
6868
export function findStackRoute(
6969
state: NavigationState | undefined,
7070
name: string,
7171
accept?: (params: Readonly<Record<string, unknown>> | undefined) => boolean,
72-
): { readonly navigatorKey: string; readonly routeKey: string } | undefined {
72+
):
73+
| { readonly navigatorKey: string; readonly routeKey: string; readonly focused: boolean }
74+
| undefined {
7375
if (!state) return undefined;
7476
for (let index = state.routes.length - 1; index >= 0; index -= 1) {
7577
const route = state.routes[index]!;
@@ -78,7 +80,7 @@ export function findStackRoute(
7880
(accept === undefined ||
7981
accept(route.params as Readonly<Record<string, unknown>> | undefined))
8082
) {
81-
return { navigatorKey: state.key, routeKey: route.key };
83+
return { navigatorKey: state.key, routeKey: route.key, focused: index === state.index };
8284
}
8385
const nested = route.state;
8486
// Mounted navigators always have keys; a partial state from a link has not been mounted yet.

0 commit comments

Comments
 (0)