Skip to content

Commit fa8b1b0

Browse files
committed
fix(web): resolve remote thread links after failed family reads
Let legacy links use a unique match in another environment when a local family read fails. Keep pending reads blocked and preserve literal ID precedence within the source environment.
1 parent db63c4d commit fa8b1b0

2 files changed

Lines changed: 66 additions & 10 deletions

File tree

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

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ import { threadFamilies } from "../../state/threads";
1515
import { ThreadContextChip } from "../ThreadContextChip";
1616
import { MarkdownThreadLink } from "./MarkdownThreadLink";
1717

18+
type FamilyResult = ReturnType<ReturnType<typeof threadFamilies.familyAtom>["read"]>;
19+
1820
const mocks = await vi.hoisted(async () => {
1921
const { Atom, AsyncResult } = await import("effect/reactivity");
2022
return {
2123
shells: Atom.make<ReadonlyMap<string, OrchestrationV2ThreadShell>>(new Map()).pipe(
2224
Atom.keepAlive,
2325
),
2426
reads: Atom.family((_key: string) =>
25-
Atom.make<AsyncResult.AsyncResult<OrchestrationV2ThreadFamily, never>>(
26-
AsyncResult.initial(),
27-
).pipe(Atom.keepAlive),
27+
Atom.make<FamilyResult>(AsyncResult.initial()).pipe(Atom.keepAlive),
2828
),
2929
};
3030
});
@@ -33,7 +33,15 @@ const key = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.thr
3333

3434
vi.mock("../../state/entities", () => ({
3535
useProject: () => null,
36-
useThreadRefs: () => [],
36+
useThreadRefs: (enabled = true) => {
37+
const rows = useAtomValue(mocks.shells);
38+
return enabled
39+
? [...rows.keys()].map((key) => {
40+
const [environmentId, threadId] = JSON.parse(key) as [EnvironmentId, ThreadId];
41+
return { environmentId, threadId };
42+
})
43+
: [];
44+
},
3745
useThreadShell: (ref: ScopedThreadRef | null) => {
3846
const rows = useAtomValue(mocks.shells);
3947
if (ref === null) return null;
@@ -106,6 +114,13 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
106114
});
107115
return {
108116
requested,
117+
fail: (target: ScopedThreadRef, waiting = false) =>
118+
act(() =>
119+
appAtomRegistry.set(
120+
mocks.reads(key(target)),
121+
AsyncResult.fail(new Error("offline"), { waiting }),
122+
),
123+
),
109124
resolve: (target: ScopedThreadRef, threads: ReadonlyArray<OrchestrationV2ThreadShell>) =>
110125
act(() =>
111126
appAtomRegistry.set(
@@ -120,13 +135,17 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
120135
};
121136
}
122137

123-
function view(target: ScopedThreadRef, kind: "context" | "markdown" = "markdown") {
138+
function view(target: ScopedThreadRef, kind: "context" | "markdown" = "markdown", legacy = false) {
124139
return (
125140
<AppAtomRegistryProvider>
126141
{kind === "context" ? (
127142
<ThreadContextChip record={{ ...target, title: "Saved title" }} />
128143
) : (
129-
<MarkdownThreadLink environmentId={ENV} link={target} label={null} />
144+
<MarkdownThreadLink
145+
environmentId={ENV}
146+
link={{ ...target, environmentId: legacy ? null : target.environmentId }}
147+
label={null}
148+
/>
130149
)}
131150
</AppAtomRegistryProvider>
132151
);
@@ -153,23 +172,48 @@ describe("thread link family lookups", () => {
153172
},
154173
);
155174

156-
it("prefers a literal percent escape to the decoded thread", async () => {
175+
it.each([false, true])("prefers a literal percent escape, legacy link %s", async (legacy) => {
157176
const literal = ref("provider%3A1");
158177
const decoded = ref("provider:1");
159178
using fixture = setup(new Map([[key(decoded), row(decoded.threadId, "Decoded thread")]]));
160-
await using screen = await render(view(literal));
179+
await using screen = await render(view(literal, "markdown", legacy));
161180
await expect
162181
.element(screen.getByRole("link", { name: "Thread, Decoded thread" }))
163182
.not.toBeInTheDocument();
164183
expect(fixture.requested.has(key(literal))).toBe(true);
165184
expect(fixture.requested.has(key(decoded))).toBe(false);
185+
await fixture.fail(literal);
186+
await expect
187+
.element(screen.getByRole("link", { name: "Thread, Decoded thread" }))
188+
.not.toBeInTheDocument();
166189
await fixture.resolve(literal, [row(literal.threadId, "Literal thread")]);
167190
await expect
168191
.element(screen.getByRole("link", { name: "Thread, Literal thread" }))
169192
.toBeInTheDocument();
170193
expect(fixture.requested.has(key(decoded))).toBe(false);
171194
});
172195

196+
it.each(["written", "decoded"] as const)(
197+
"resolves a legacy link remotely when the %s family read fails",
198+
async (lookup) => {
199+
const literal = ref("provider%3A1");
200+
const decoded = ref("provider:1");
201+
const failed = lookup === "written" ? literal : decoded;
202+
const remote = ref(failed.threadId, EnvironmentId.make("remote-environment"));
203+
using fixture = setup(new Map([[key(remote), row(remote.threadId, "Remote thread")]]));
204+
await using screen = await render(view(literal, "markdown", true));
205+
if (lookup === "decoded") await fixture.resolve(literal, []);
206+
await fixture.fail(failed, true);
207+
await expect
208+
.element(screen.getByRole("link", { name: "Thread, Remote thread" }))
209+
.not.toBeInTheDocument();
210+
await fixture.fail(failed);
211+
await expect
212+
.element(screen.getByRole("link", { name: "Thread, Remote thread" }))
213+
.toBeInTheDocument();
214+
},
215+
);
216+
173217
it("tries the decoded ID only after a successful empty literal lookup", async () => {
174218
using fixture = setup();
175219
const literal = ref("provider%3A1");

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import { threadFamilyRead } from "@t3tools/client-runtime/state/threads";
23
import type { EnvironmentId } from "@t3tools/contracts";
34
import { AsyncResult } from "effect/reactivity";
45
import {
@@ -44,10 +45,21 @@ export function MarkdownThreadLink(props: {
4445
!decodedFamily.waiting &&
4546
!decodedFamily.value.threads.some((thread) => thread.id === decodedId));
4647
const local = written ?? decoded;
48+
const lookupFailed =
49+
threadFamilyRead(writtenFamily) === "failed" || threadFamilyRead(decodedFamily) === "failed";
4750
const guess =
48-
local === null && writtenMissing && decodedMissing && props.link.environmentId === null;
51+
local === null &&
52+
(lookupFailed || (writtenMissing && decodedMissing)) &&
53+
props.link.environmentId === null;
4954
const refs = useThreadRefs(guess);
50-
const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null);
55+
const remote = useThreadShell(
56+
guess
57+
? guessThreadLinkRef(
58+
props.link,
59+
refs.filter((ref) => ref.environmentId !== environmentId),
60+
)
61+
: null,
62+
);
5163
const thread = local ?? remote;
5264
if (thread === null) {
5365
const unknown = unknownThreadLinkLabel(props.link, props.environmentId);

0 commit comments

Comments
 (0)