Skip to content

Commit 4a12482

Browse files
perf(web,mobile): thread links resolve by key, never by scanning every thread
Mobile resolved every thread link, v2 included, with a pass over all known threads, and titled links from a map rebuilt over each named environment's threads on any change. A v2 link now reads only its own thread: one keyed title atom per linked thread. Every thread is read only to place a v1 link the message's environment lacks, on web and mobile alike. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 244fc0b commit 4a12482

5 files changed

Lines changed: 101 additions & 100 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import {
5656
} from "@t3tools/contracts";
5757
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
5858
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
59-
import { parseThreadLinkHref, resolveThreadLinkRef } from "@t3tools/shared/threadLinks";
59+
import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
6060
import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks";
6161
import {
6262
parseComposerContextHref,
@@ -239,6 +239,7 @@ import {
239239
WORKSPACE_BASENAME_LOOKUP_LIMIT,
240240
} from "@t3tools/client-runtime/workspace-basename-lookup";
241241
import {
242+
resolveThreadLink,
242243
useLiveThreadLinkLabels,
243244
useQuietThreadShell,
244245
useThreadChipIcons,
@@ -2257,8 +2258,11 @@ export const ThreadFeed = memo(function ThreadFeed({
22572258
const resolveLinkedThread = (href: string) => {
22582259
const link = parseThreadLinkHref(href);
22592260
if (!link) return null;
2260-
const refs = appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
2261-
const resolved = resolveThreadLinkRef(link, props.environmentId, refs);
2261+
const resolved = resolveThreadLink(
2262+
(atom) => appAtomRegistry.get(atom),
2263+
link,
2264+
props.environmentId,
2265+
);
22622266
if (resolved !== null || link.environmentId !== null) return resolved;
22632267
// A v1 link names no environment, so it falls back to this message's.
22642268
return scopeThreadRef(props.environmentId, link.threadId);

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

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {
1717
} from "@t3tools/contracts";
1818
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
1919
import {
20+
guessThreadLinkRef,
2021
hasThreadLinks,
2122
parseThreadLinkHref,
2223
relabelThreadLinks,
23-
resolveThreadLinkRef,
24-
threadLinkEnvironmentIds,
24+
threadLinkCandidates,
25+
threadLinkRefs,
26+
type ThreadLink,
2527
} from "@t3tools/shared/threadLinks";
2628
import * as Option from "effect/Option";
2729
import * as Schema from "effect/Schema";
@@ -47,41 +49,35 @@ const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(
4749
const EMPTY_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
4850
Atom.withLabel("mobile-thread:empty"),
4951
);
50-
const EMPTY_THREAD_TITLES: ReadonlyMap<ThreadId, string> = new Map();
51-
const EMPTY_THREAD_LINK_TITLES_ATOM = Atom.make(
52-
new Map<EnvironmentId, ReadonlyMap<ThreadId, string>>(),
53-
).pipe(Atom.withLabel("mobile-thread-link-titles:empty"));
52+
const EMPTY_THREAD_LINK_TITLES_ATOM = Atom.make<ReadonlyMap<string, string>>(new Map()).pipe(
53+
Atom.withLabel("mobile-thread-link-titles:empty"),
54+
);
5455

55-
const threadTitlesAtom = Atom.family((environmentId: EnvironmentId) => {
56-
let previous = EMPTY_THREAD_TITLES;
57-
return Atom.make((get) => {
58-
const index = get(environmentThreadShells.environmentThreadIndexAtom(environmentId));
59-
const unchanged =
60-
index.size === previous.size &&
61-
Array.from(index).every(([threadId, shell]) => previous.get(threadId) === shell.title);
62-
if (!unchanged) {
63-
previous = new Map(Array.from(index, ([threadId, shell]) => [threadId, shell.title]));
64-
}
65-
return previous;
66-
}).pipe(Atom.withLabel(`mobile-thread-titles:${environmentId}`));
56+
const decodeThreadKey = Schema.decodeUnknownSync(Schema.Tuple([EnvironmentId, ThreadId]));
57+
const threadKey = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.threadId]);
58+
59+
// Null when this client doesn't know the thread. Keyed, so a link never walks an environment's threads.
60+
const threadTitleAtom = Atom.family((key: string) => {
61+
const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key)));
62+
return Atom.make((get) => get(environmentThreadShells.threadShellAtom(ref))?.title ?? null).pipe(
63+
Atom.withLabel(`mobile-thread-title:${key}`),
64+
);
6765
});
6866

69-
const decodeEnvironmentIds = Schema.decodeUnknownSync(Schema.Array(EnvironmentId));
67+
const decodeThreadKeys = Schema.decodeUnknownSync(Schema.Array(Schema.String));
7068

7169
const threadLinkTitlesAtom = Atom.family((key: string) => {
72-
const environmentIds = decodeEnvironmentIds(JSON.parse(key));
73-
return Atom.make(
74-
(get) =>
75-
new Map(
76-
environmentIds.map((environmentId) => [
77-
environmentId,
78-
get(threadTitlesAtom(environmentId)),
79-
]),
80-
),
81-
).pipe(Atom.withLabel(`mobile-thread-link-titles:${key}`));
70+
const refKeys = decodeThreadKeys(JSON.parse(key));
71+
return Atom.make((get) => {
72+
const titles = new Map<string, string>();
73+
for (const refKey of refKeys) {
74+
const title = get(threadTitleAtom(refKey));
75+
if (title !== null) titles.set(refKey, title);
76+
}
77+
return titles;
78+
}).pipe(Atom.withLabel(`mobile-thread-link-titles:${key}`));
8279
});
8380

84-
const decodeThreadKey = Schema.decodeUnknownSync(Schema.Tuple([EnvironmentId, ThreadId]));
8581
const decodeThreadChipTargets = Schema.decodeUnknownSync(
8682
Schema.Array(
8783
Schema.Struct({
@@ -135,18 +131,11 @@ const threadChipIconsAtom = Atom.family((key: string) => {
135131
return Atom.make((get) => {
136132
const icons = new Map<string, string>();
137133
for (const target of targets) {
138-
const source = scopeThreadRef(target.environmentId, target.threadId);
139134
const link = target.link ? parseThreadLinkHref(target.href) : null;
140-
const ref = link
141-
? (resolveThreadLinkRef(
142-
link,
143-
target.environmentId,
144-
get(environmentThreadShells.threadRefsAtom),
145-
) ?? source)
146-
: source;
147-
const icon = get(
148-
threadProjectChipIconAtom(JSON.stringify([ref.environmentId, ref.threadId])),
149-
);
135+
const ref =
136+
(link ? resolveThreadLink(get, link, target.environmentId) : null) ??
137+
scopeThreadRef(target.environmentId, target.threadId);
138+
const icon = get(threadProjectChipIconAtom(threadKey(ref)));
150139
if (icon) icons.set(target.href, icon);
151140
}
152141
return icons;
@@ -213,12 +202,25 @@ export function useChildThreadInputs(ref: ScopedThreadRef) {
213202
export function useLiveThreadLinkLabels(markdown: string, environmentId: EnvironmentId): string {
214203
const titles = useAtomValue(
215204
hasThreadLinks(markdown)
216-
? threadLinkTitlesAtom(JSON.stringify(threadLinkEnvironmentIds(markdown, environmentId)))
205+
? threadLinkTitlesAtom(JSON.stringify(threadLinkRefs(markdown, environmentId).map(threadKey)))
217206
: EMPTY_THREAD_LINK_TITLES_ATOM,
218207
);
219-
return relabelThreadLinks(markdown, environmentId, (ref) =>
220-
titles.get(ref.environmentId)?.get(ref.threadId),
208+
return relabelThreadLinks(markdown, environmentId, (ref) => titles.get(threadKey(ref)));
209+
}
210+
211+
/** Keyed reads; every thread is read only to place a v1 link its message's environment lacks. */
212+
export function resolveThreadLink(
213+
read: <A>(atom: Atom.Atom<A>) => A,
214+
link: ThreadLink,
215+
messageEnvironmentId: EnvironmentId,
216+
): ScopedThreadRef | null {
217+
const known = threadLinkCandidates(link, messageEnvironmentId).find(
218+
(ref) => read(threadTitleAtom(threadKey(ref))) !== null,
221219
);
220+
if (known) return known;
221+
return link.environmentId === null
222+
? guessThreadLinkRef(link, read(environmentThreadShells.threadRefsAtom))
223+
: null;
222224
}
223225

224226
export function useThreadChipIcons(

‎apps/web/src/components/chat/MarkdownThreadLink.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import type { EnvironmentId } from "@t3tools/contracts";
33
import {
44
formatThreadLink,
5+
guessThreadLinkRef,
56
percentDecodedThreadLinkId,
6-
resolveThreadLinkRef,
77
type ThreadLink,
88
unknownThreadLinkLabel,
99
} from "@t3tools/shared/threadLinks";
@@ -27,9 +27,7 @@ export function MarkdownThreadLink(props: {
2727
const local = written ?? decoded;
2828
const guess = local === null && props.link.environmentId === null;
2929
const refs = useThreadRefs(guess);
30-
const remote = useThreadShell(
31-
guess ? resolveThreadLinkRef(props.link, props.environmentId, refs) : null,
32-
);
30+
const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null);
3331
const thread = local ?? remote;
3432
if (thread === null) {
3533
const unknown = unknownThreadLinkLabel(props.link, props.environmentId);

‎packages/shared/src/threadLinks.test.ts‎

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contract
33

44
import {
55
formatThreadLink,
6+
guessThreadLinkRef,
67
parseThreadLinkHref,
78
relabelThreadLinks,
8-
resolveThreadLinkRef,
9-
threadLinkEnvironmentIds,
9+
threadLinkCandidates,
10+
threadLinkRefs,
1011
} from "./threadLinks.ts";
1112

1213
const ref = (environmentId: string, threadId = "chat-1") => ({
@@ -21,47 +22,37 @@ const titlesIn =
2122
titles[environmentId]?.[threadId];
2223

2324
describe("thread link destinations", () => {
24-
it("prefers the local literal id, then the local decoded id, over remote matches", () => {
25-
const source = ref("local", "provider%3A1");
26-
const decoded = ref("local", "provider:1");
27-
const remote = ref("remote", "provider%3A1");
28-
const link = v1("provider%3A1");
29-
expect(resolveThreadLinkRef(link, local, [remote, decoded, source])).toEqual(source);
30-
expect(resolveThreadLinkRef(link, local, [remote, decoded])).toEqual(decoded);
25+
it("names a v1 link's thread in the message's environment, as written and then decoded", () => {
26+
expect(threadLinkCandidates(v1("provider%3A1"), local)).toEqual([
27+
ref("local", "provider%3A1"),
28+
ref("local", "provider:1"),
29+
]);
3130
});
3231

3332
it("opens the unique matching chat in another environment for a v1 link", () => {
3433
const destination = ref("remote");
35-
expect(
36-
resolveThreadLinkRef(v1("chat-1"), local, [ref("local", "other-chat"), destination]),
37-
).toEqual(destination);
34+
expect(guessThreadLinkRef(v1("chat-1"), [ref("local", "other-chat"), destination])).toEqual(
35+
destination,
36+
);
3837
});
3938

4039
it("opens a v2 link only in its own environment", () => {
41-
const there = ref("there");
42-
const link = { ...there, threadId: ThreadId.make("chat-1") };
43-
expect(resolveThreadLinkRef(link, local, [ref("local"), ref("elsewhere"), there])).toEqual(
44-
there,
40+
const there = ref("there", "a%3A1");
41+
expect(threadLinkCandidates(there, local)).toEqual([there, ref("there", "a:1")]);
42+
expect(guessThreadLinkRef(there, [ref("elsewhere", "a%3A1"), ref("elsewhere", "a:1")])).toBe(
43+
null,
4544
);
46-
expect(resolveThreadLinkRef(link, local, [ref("local"), ref("elsewhere")])).toBeNull();
47-
expect(
48-
resolveThreadLinkRef({ ...there, threadId: ThreadId.make("a%3A1") }, local, [
49-
ref("there", "a:1"),
50-
]),
51-
).toEqual(ref("there", "a:1"));
5245
});
5346

5447
it("leaves ambiguous or missing destinations unresolved", () => {
5548
const link = v1("provider%3A1");
5649
const literal = ref("remote", "provider%3A1");
5750
const decoded = ref("remote", "provider:1");
5851
expect(
59-
resolveThreadLinkRef(link, local, [literal, ref("other-remote", literal.threadId), decoded]),
60-
).toBeNull();
61-
expect(
62-
resolveThreadLinkRef(link, local, [decoded, ref("other-remote", decoded.threadId)]),
52+
guessThreadLinkRef(link, [literal, ref("other-remote", literal.threadId), decoded]),
6353
).toBeNull();
64-
expect(resolveThreadLinkRef(link, local, [ref("remote", "other-chat")])).toBeNull();
54+
expect(guessThreadLinkRef(link, [decoded, ref("other-remote", decoded.threadId)])).toBeNull();
55+
expect(guessThreadLinkRef(link, [ref("remote", "other-chat")])).toBeNull();
6556
});
6657
});
6758

@@ -143,13 +134,13 @@ describe("thread links", () => {
143134
);
144135
});
145136

146-
it("lists the environments a message's links need titles from", () => {
137+
it("lists the threads a message's links can name, outside code", () => {
147138
expect(
148-
threadLinkEnvironmentIds(
149-
"<t3-thread://v2/there/t1> [a](t3-thread://v1/t2) `<t3-thread://v2/code/t3>`",
139+
threadLinkRefs(
140+
"<t3-thread://v2/there/t1> [a](t3-thread://v1/t%3A2) `<t3-thread://v2/code/t3>`",
150141
local,
151142
),
152-
).toEqual(["local", "there"]);
143+
).toEqual([ref("there", "t1"), ref("local", "t%3A2"), ref("local", "t:2")]);
153144
});
154145

155146
it("writes a titleless autolink that opens that exact thread", () => {

‎packages/shared/src/threadLinks.ts‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,25 @@ export function percentDecodedThreadLinkId(threadId: ThreadId): ThreadId | null
4242
}
4343
}
4444

45-
/** A v2 link opens only its own environment's thread; a v1 link may open a unique match elsewhere. */
46-
export function resolveThreadLinkRef(
45+
/** The threads a link can name, in its own environment or else the message's: as written, then decoded. */
46+
export function threadLinkCandidates(
4747
link: ThreadLink,
4848
messageEnvironmentId: EnvironmentId,
49-
refs: ReadonlyArray<ScopedThreadRef>,
50-
): ScopedThreadRef | null {
49+
): ReadonlyArray<ScopedThreadRef> {
5150
const environmentId = link.environmentId ?? messageEnvironmentId;
5251
const decodedId = percentDecodedThreadLinkId(link.threadId);
53-
const ids = decodedId === null ? [link.threadId] : [link.threadId, decodedId];
54-
for (const threadId of ids) {
55-
const local = refs.find(
56-
(ref) => ref.environmentId === environmentId && ref.threadId === threadId,
57-
);
58-
if (local) return local;
59-
}
52+
const written = { environmentId, threadId: link.threadId };
53+
return decodedId === null ? [written] : [written, { environmentId, threadId: decodedId }];
54+
}
55+
56+
/** A v1 link its message's environment lacks opens a unique match elsewhere; a v2 link never does. */
57+
export function guessThreadLinkRef(
58+
link: ThreadLink,
59+
refs: ReadonlyArray<ScopedThreadRef>,
60+
): ScopedThreadRef | null {
6061
if (link.environmentId !== null) return null;
61-
for (const threadId of ids) {
62+
const decodedId = percentDecodedThreadLinkId(link.threadId);
63+
for (const threadId of decodedId === null ? [link.threadId] : [link.threadId, decodedId]) {
6264
let match: ScopedThreadRef | null = null;
6365
for (const ref of refs) {
6466
if (ref.threadId !== threadId) continue;
@@ -89,17 +91,21 @@ export function hasThreadLinks(markdown: string): boolean {
8991
return markdown.includes(`${THREAD_LINK_PROTOCOL}://v`);
9092
}
9193

92-
export function threadLinkEnvironmentIds(
94+
/** Every thread the markdown's links can name, so their titles are keyed reads. */
95+
export function threadLinkRefs(
9396
markdown: string,
9497
messageEnvironmentId: EnvironmentId,
95-
): ReadonlyArray<EnvironmentId> {
96-
const environmentIds = new Set([messageEnvironmentId]);
98+
): ReadonlyArray<ScopedThreadRef> {
99+
const refs = new Map<string, ScopedThreadRef>();
97100
for (const match of markdown.matchAll(THREAD_LINK_OUTSIDE_CODE)) {
98101
const href = match.groups?.href ?? match.groups?.autolink;
99-
const environmentId = href === undefined ? null : parseThreadLinkHref(href)?.environmentId;
100-
if (environmentId) environmentIds.add(environmentId);
102+
const link = href === undefined ? null : parseThreadLinkHref(href);
103+
if (link === null) continue;
104+
for (const ref of threadLinkCandidates(link, messageEnvironmentId)) {
105+
refs.set(`${ref.environmentId}/${ref.threadId}`, ref);
106+
}
101107
}
102-
return [...environmentIds].sort();
108+
return [...refs.values()];
103109
}
104110

105111
export function unknownThreadLinkLabel(

0 commit comments

Comments
 (0)