Skip to content

Commit 5440c7f

Browse files
Fix thread citations rendering as "Thread no longer available"
Angle-bracketed link destinations ([label](<t3-thread://...>)) were not parsed as labeled links: the inner <...> matched as an autolink, the label was discarded, and an unresolved title substituted "Thread no longer available" as the destination text. Thread titles were also resolved only from the shell snapshot, which omits idle subagent children and all but the 25 newest settled threads, so live threads cited from chat read as deleted. Mobile and web now mount the cited thread's family read (the on-demand rows search and the Settled shelf already use) until its title lands. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 8b44f42 commit 5440c7f

6 files changed

Lines changed: 65 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,16 @@ describe("Enriched link presentation", () => {
103103
]);
104104
expect(threadChipTargets(here, "No links here.")).toEqual([]);
105105
});
106+
107+
it("finds thread links with angle-bracketed destinations", () => {
108+
const here = "env-here" as EnvironmentId;
109+
expect(threadChipTargets(here, "See [A](<t3-thread://v2/env-here/thread_a>).")).toEqual([
110+
{
111+
href: "t3-thread://v2/env-here/thread_a",
112+
environmentId: here,
113+
threadId: "thread_a",
114+
link: true,
115+
},
116+
]);
117+
});
106118
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function threadChipTargets(
5858
): ReadonlyArray<ThreadChipTarget> {
5959
const targets = new Map<string, ThreadChipTarget>();
6060
if (hasThreadLinks(markdown)) {
61-
for (const [, href = ""] of markdown.matchAll(/\]\((t3-thread:\/\/v[12]\/[^\s)]+)\)/g)) {
61+
for (const [, href = ""] of markdown.matchAll(/\]\(<?(t3-thread:\/\/v[12]\/[^\s>)]+)>?\)/g)) {
6262
const link = parseThreadLinkHref(href);
6363
if (link) {
6464
targets.set(href, {

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useAtomValue } from "@effect/atom-react";
2+
import { useEffect } from "react";
23
import { deriveReportedModelSelection } from "@t3tools/client-runtime/state/thread-execution";
34

45
import { appAtomRegistry } from "./atom-registry";
@@ -34,7 +35,7 @@ import { projectGlyphImageAtom, projectGlyphKey } from "../native/ProjectChipIco
3435
import { projectFaviconUrlAtom } from "./assets";
3536
import { environmentProjects } from "./projects";
3637
import { environmentServerConfigsAtom, serverEnvironment } from "./server";
37-
import { environmentThreadDetails, environmentThreadShells } from "./threads";
38+
import { environmentThreadDetails, environmentThreadShells, threadFamilies } from "./threads";
3839

3940
const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
4041
Atom.withLabel("mobile-project:empty"),
@@ -208,11 +209,27 @@ export function useSideChats(ref: ScopedThreadRef) {
208209
}
209210

210211
export function useLiveThreadLinkLabels(markdown: string, environmentId: EnvironmentId): string {
212+
const refs = hasThreadLinks(markdown) ? threadLinkRefs(markdown, environmentId) : [];
211213
const titles = useAtomValue(
212-
hasThreadLinks(markdown)
213-
? threadLinkTitlesAtom(JSON.stringify(threadLinkRefs(markdown, environmentId).map(threadKey)))
214-
: EMPTY_THREAD_LINK_TITLES_ATOM,
214+
refs.length === 0
215+
? EMPTY_THREAD_LINK_TITLES_ATOM
216+
: threadLinkTitlesAtom(JSON.stringify(refs.map(threadKey))),
215217
);
218+
// Shells leave out idle subagent children and older settled threads, so a cited thread the
219+
// shell lacks is read with its family until it lands; only a settled empty read proves it gone.
220+
const missingKey = JSON.stringify(
221+
refs.filter((ref) => !titles.has(threadKey(ref))).map(threadKey),
222+
);
223+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts family reads for cited threads the shell omits while this message shows them
224+
useEffect(() => {
225+
const releases = (JSON.parse(missingKey) as ReadonlyArray<string>).map((key) => {
226+
const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key)));
227+
return threadFamilies.mount(appAtomRegistry, ref.environmentId, ref.threadId);
228+
});
229+
return () => {
230+
for (const release of releases) release();
231+
};
232+
}, [missingKey]);
216233
return relabelThreadLinks(markdown, environmentId, (ref) => titles.get(threadKey(ref)));
217234
}
218235

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from "react";
12
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
23
import type { EnvironmentId } from "@t3tools/contracts";
34
import {
@@ -9,7 +10,9 @@ import {
910
} from "@t3tools/shared/threadLinks";
1011

1112
import { configuredHostedAppUrl } from "../../hostedPairing";
13+
import { appAtomRegistry } from "../../rpc/atomRegistry";
1214
import { useThreadRefs, useThreadShell } from "../../state/entities";
15+
import { threadFamilies } from "../../state/threads";
1316
import { UnresolvedChip } from "../contextChipParts";
1417
import { ThreadContextChip } from "../ThreadContextChip";
1518

@@ -26,6 +29,17 @@ export function MarkdownThreadLink(props: {
2629
written === null && decodedId !== null ? scopeThreadRef(environmentId, decodedId) : null,
2730
);
2831
const local = written ?? decoded;
32+
// Shells leave out idle subagent children and older settled threads, so a cited thread the
33+
// shell lacks is read with its family until it lands; only a settled empty read proves it gone.
34+
const missingThreadId = local === null ? (decodedId ?? props.link.threadId) : null;
35+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the family read for a cited thread the shell omits while this link shows it
36+
useEffect(
37+
() =>
38+
missingThreadId === null
39+
? undefined
40+
: threadFamilies.mount(appAtomRegistry, environmentId, missingThreadId),
41+
[environmentId, missingThreadId],
42+
);
2943
const guess = local === null && props.link.environmentId === null;
3044
const refs = useThreadRefs(guess);
3145
const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ describe("thread links", () => {
104104
).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
105105
});
106106

107+
it("reads an angle-bracketed destination as the labeled link, not an autolink", () => {
108+
const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } });
109+
expect(
110+
relabelThreadLinks(
111+
"[old](<t3-thread://v2/there/t1>) and [older](<t3-thread://v2/local/t1>)",
112+
local,
113+
title,
114+
),
115+
).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
116+
expect(
117+
relabelThreadLinks("[Kept label](<t3-thread://v2/local/t1>)", local, () => undefined),
118+
).toBe("Kept label");
119+
expect(threadLinkRefs("[a](<t3-thread://v2/there/t2>)", local)).toEqual([ref("there", "t2")]);
120+
});
121+
107122
it("turns a v2 link it cannot resolve into plain text, never the raw link", () => {
108123
expect(
109124
relabelThreadLinks(

‎packages/shared/src/threadLinks.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const HOSTED_PREFIX = `${DEFAULT_HOSTED_APP_URL}/threads/`;
1111
const HOSTED_SEGMENT = "[A-Za-z0-9_%-]+";
1212
const HOSTED_LINK = `${HOSTED_PREFIX.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}${HOSTED_SEGMENT}/${HOSTED_SEGMENT}`;
1313
// Code comes first in the alternation so a link written inside a code span or fence is skipped.
14+
// A labeled link's destination may be angle-bracketed, as CommonMark allows: [label](<href>).
1415
const THREAD_LINK_OUTSIDE_CODE = new RegExp(
15-
String.raw`(?<fence>(\`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(\`+)[^\n]*?\4)|\[(?<label>[^\]\n]*)\]\((?<href>t3-thread:\/\/v[12]\/[^\s)]+|${HOSTED_LINK})\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+|${HOSTED_LINK})>|(?<bare>${HOSTED_LINK})(?![\w%/?#-])`,
16+
String.raw`(?<fence>(\`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(\`+)[^\n]*?\4)|\[(?<label>[^\]\n]*)\]\(<?(?<href>t3-thread:\/\/v[12]\/[^\s>)]+|${HOSTED_LINK})>?\)|<(?<autolink>t3-thread:\/\/v[12]\/[^\s<>]+|${HOSTED_LINK})>|(?<bare>${HOSTED_LINK})(?![\w%/?#-])`,
1617
"g",
1718
);
1819

0 commit comments

Comments
 (0)