Skip to content

Commit 189cfd8

Browse files
fix(mobile): prevent linked subagent messages from crashing conversations (#75)
* test(mobile): reproduce linked thread title subscription loop * fix(mobile): keep cited thread families mounted while links remain
1 parent 006106d commit 189cfd8

3 files changed

Lines changed: 80 additions & 6 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { RegistryContext } from "@effect/atom-react";
2+
import { act } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { expect, it, vi } from "vite-plus/test";
5+
import { Atom, AtomRegistry } from "effect/reactivity";
6+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
7+
8+
vi.mock("./atom-registry", () => ({ appAtomRegistry: AtomRegistry.make() }));
9+
vi.mock("./projects", () => ({ environmentProjects: {} }));
10+
vi.mock("./server", () => ({
11+
environmentServerConfigsAtom: Atom.make(new Map()),
12+
serverEnvironment: {},
13+
}));
14+
vi.mock("./assets", () => ({ projectFaviconUrlAtom: vi.fn() }));
15+
vi.mock("../native/ProjectChipIconImages", () => ({
16+
projectGlyphImageAtom: vi.fn(),
17+
projectGlyphKey: vi.fn(),
18+
}));
19+
vi.mock("./threads", () => {
20+
const shell = Atom.make<{ title: string } | null>(null);
21+
return {
22+
environmentThreadDetails: {},
23+
environmentThreadShells: { threadShellAtom: () => shell },
24+
threadFamilies: {
25+
mount: (registry: AtomRegistry.AtomRegistry) => {
26+
registry.set(shell, { title: "Child result" });
27+
return () => registry.set(shell, null);
28+
},
29+
},
30+
};
31+
});
32+
33+
import { appAtomRegistry } from "./atom-registry";
34+
import { useLiveThreadLinkLabels } from "./entities";
35+
import { environmentThreadShells } from "./threads";
36+
37+
it("keeps a linked child title mounted until the message stops citing it", async () => {
38+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
39+
const container = document.createElement("div");
40+
const root = createRoot(container);
41+
let renders = 0;
42+
function Probe({ text }: { text: string }) {
43+
if (++renders > 20) throw new Error("Thread title subscription loop");
44+
return useLiveThreadLinkLabels(text, EnvironmentId.make("local"));
45+
}
46+
try {
47+
await act(async () =>
48+
root.render(
49+
<RegistryContext.Provider value={appAtomRegistry}>
50+
<Probe text="[Child](t3-thread://v2/local/child)" />
51+
</RegistryContext.Provider>,
52+
),
53+
);
54+
expect(container.textContent).toBe("[Child result](t3-thread://v2/local/child)");
55+
expect(renders).toBeLessThan(20);
56+
await act(async () =>
57+
root.render(
58+
<RegistryContext.Provider value={appAtomRegistry}>
59+
<Probe text="No link" />
60+
</RegistryContext.Provider>,
61+
),
62+
);
63+
expect(container.textContent).toBe("No link");
64+
expect(
65+
appAtomRegistry.get(
66+
environmentThreadShells.threadShellAtom({
67+
environmentId: EnvironmentId.make("local"),
68+
threadId: ThreadId.make("child"),
69+
}),
70+
),
71+
).toBeNull();
72+
} finally {
73+
await act(async () => root.unmount());
74+
}
75+
});

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,17 @@ export function useLiveThreadLinkLabels(markdown: string, environmentId: Environ
217217
);
218218
// Shells leave out idle subagent children and older settled threads, so a cited thread the
219219
// 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
220+
const refsKey = JSON.stringify(refs.map(threadKey));
221+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps cited families mounted until the links change; releasing on title arrival removes that title again
224222
useEffect(() => {
225-
const releases = (JSON.parse(missingKey) as ReadonlyArray<string>).map((key) => {
223+
const releases = (JSON.parse(refsKey) as ReadonlyArray<string>).map((key) => {
226224
const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key)));
227225
return threadFamilies.mount(appAtomRegistry, ref.environmentId, ref.threadId);
228226
});
229227
return () => {
230228
for (const release of releases) release();
231229
};
232-
}, [missingKey]);
230+
}, [refsKey]);
233231
return relabelThreadLinks(markdown, environmentId, (ref) => titles.get(threadKey(ref)));
234232
}
235233

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defineConfig, normalizePath } from "vite-plus";
44
import "vite-plus/test/config";
55

66
const reactTests = [
7+
"src/state/entities.test.tsx",
78
"src/features/external-history/ExternalHistoryRouteScreen.test.tsx",
89
"src/features/conversation-summaries/ConversationSummariesRouteScreen.test.tsx",
910
"src/components/ProjectKnowledgeEntry.test.tsx",

0 commit comments

Comments
 (0)