From 22e9cccc4f7232947971b53123b7d15da4487004 Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Sun, 1 Nov 2026 21:48:40 -0500 Subject: [PATCH 1/2] fix(mobile): sharing a thread never takes a drawn message off the screen Once a thread could be shared, any message with no author and a first sighting after the record's last read was held back, including messages already on screen. A thread whose messages hydrated after its record read lost them all the moment it was shared, until the record read again: rows left and re-entered the list mid-layout. A message drawn before the thread could be shared now stays; only one first seen after it waits for its author. Refs #892 Co-Authored-By: Claude Opus 5.5 --- .../src/j5/sharing/sharedConversation.tsx | 2 + .../j5/sharing/sharedConversationFeed.test.ts | 23 +++++++++++ .../src/j5/sharing/sharedConversationFeed.ts | 6 +-- apps/web/src/j5/sharing/sharedTimeline.ts | 18 ++++----- .../src/j5/sharedConversation.test.ts | 39 +++++++++++++++++++ .../src/j5/sharedConversation.ts | 37 ++++++++++++++++-- 6 files changed, 108 insertions(+), 17 deletions(-) diff --git a/apps/mobile/src/j5/sharing/sharedConversation.tsx b/apps/mobile/src/j5/sharing/sharedConversation.tsx index 6ccc23af12bd..055ae2871177 100644 --- a/apps/mobile/src/j5/sharing/sharedConversation.tsx +++ b/apps/mobile/src/j5/sharing/sharedConversation.tsx @@ -2,6 +2,7 @@ import { useAtomValue } from "@effect/atom-react"; import { authorIndex, isSettledPrompt, + mayBeSharedConversation, NO_SHARED_MESSAGES_SEEN, observeSharedMessages, sharedQueuedRuns, @@ -112,6 +113,7 @@ export function useSharedConversationFeed( const seen = observeSharedMessages( stored, feed.flatMap((entry) => (entry.type === "message" ? [entry.message] : [])), + mayBeSharedConversation({ record, failed, people }), Date.now, ); if (seen !== stored) setStored(seen); diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts index 7263fc52452d..417482114498 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts @@ -108,6 +108,29 @@ describe("presentSharedConversationFeed", () => { ); }); + it("keeps a message already on screen when the thread becomes shared", () => { + // The owner's message hydrated after the record's last read, and was drawn while unshared. + const stored = message("m-5", "My words from before sharing."); + const owner = { level: "owner" as const, you: true, firstOwner: true }; + const collaborator = { level: "collaborator" as const, you: false, firstOwner: false }; + const reaching = [owner, collaborator].map((person, index) => ({ + ...person, + personId: `human:${index}`, + name: null, + via: [], + })); + const { feed, pendingKey } = presentSharedConversationFeed({ + feed: [stored], + record: record({ shared: false, authors: [] }), + failed: false, + readAt: 1, + seen: { ...seenAt(["m-5"], 2), sharedSince: 3 }, + people: reaching, + }); + expect(feed).toEqual([stored]); + expect(pendingKey).toBe(""); + }); + it("leaves a thread only one person writes to as stored", () => { const stored = message("m-3", "Just me."); const { feed } = presentSharedConversationFeed({ diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts index 15cf845e5799..f611d3caa0b3 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts @@ -3,8 +3,8 @@ import { authorIndex, markSharedNote, mayBeSharedConversation, - readAnswersFor, type SeenSharedMessages, + sharedMessageAnsweredFor, sharedMessageView, } from "@t3tools/client-runtime/j5/shared-conversation"; import { MessageId } from "@t3tools/contracts"; @@ -61,9 +61,7 @@ export function presentSharedConversationFeed(input: { fromServer, author: authors.get(message.id), answeredFor: - !fromServer || - failed || - readAnswersFor({ readAt, firstSeenAt: seen.firstSeen.get(message.id) ?? Infinity }), + !fromServer || failed || sharedMessageAnsweredFor({ messageId: message.id, readAt, seen }), mayBeShared, typedHere: seen.typedHere.get(message.id) ?? null, }); diff --git a/apps/web/src/j5/sharing/sharedTimeline.ts b/apps/web/src/j5/sharing/sharedTimeline.ts index 37732a719325..acd67e2436a8 100644 --- a/apps/web/src/j5/sharing/sharedTimeline.ts +++ b/apps/web/src/j5/sharing/sharedTimeline.ts @@ -11,9 +11,9 @@ import { sharedQueuedRuns, isSharedNote, markSharedNote, - readAnswersFor, recallableMessages, type SharedMessageView, + sharedMessageAnsweredFor, sharedMessageView, typedText, } from "@t3tools/client-runtime/j5/shared-conversation"; @@ -136,13 +136,6 @@ export function useSharedConversationEntries( const people = useScopePeople(threadRef?.environmentId ?? null, threadScope); const [stored, setStored] = useState(NO_SHARED_MESSAGES_SEEN); - const seen = observeSharedMessages( - stored, - entries.flatMap((entry) => (entry.message === undefined ? [] : [entry.message])), - Date.now, - ); - if (seen !== stored) setStored(seen); - // After the state hook: computed before it, the call stops the compiler memoizing the entries, // and ConversationTimeline re-renders forever on entries that change each render. const mayBeShared = mayBeSharedConversation({ @@ -150,6 +143,13 @@ export function useSharedConversationEntries( failed, people: people.data?.people ?? null, }); + const seen = observeSharedMessages( + stored, + entries.flatMap((entry) => (entry.message === undefined ? [] : [entry.message])), + mayBeShared, + Date.now, + ); + if (seen !== stored) setStored(seen); const shown = (() => { const { authors, answers } = authorIndex(record); @@ -182,7 +182,7 @@ export function useSharedConversationEntries( answeredFor: !fromServer || failed || - readAnswersFor({ readAt, firstSeenAt: seen.firstSeen.get(message.id) ?? Infinity }), + sharedMessageAnsweredFor({ messageId: message.id, readAt, seen }), mayBeShared, typedHere: seen.typedHere.get(message.id) ?? null, }); diff --git a/packages/client-runtime/src/j5/sharedConversation.test.ts b/packages/client-runtime/src/j5/sharedConversation.test.ts index dd9f8cd3cd95..72f163b5f043 100644 --- a/packages/client-runtime/src/j5/sharedConversation.test.ts +++ b/packages/client-runtime/src/j5/sharedConversation.test.ts @@ -8,6 +8,9 @@ import { sharedMessageView, typedText, mayBeSharedConversation, + NO_SHARED_MESSAGES_SEEN, + observeSharedMessages, + sharedMessageAnsweredFor, } from "./sharedConversation.ts"; const author = (overrides: Partial = {}): MessageAuthor => ({ @@ -194,3 +197,39 @@ describe("mayBeSharedConversation", () => { ).toBe(true); }); }); + +describe("holding a message for its author", () => { + const message = (id: string) => ({ id, role: "user", text: "Hello", createdBy: "user" }); + const clock = (...times: ReadonlyArray) => { + let index = 0; + return () => times[index++]!; + }; + + it("never holds a message already on screen when the thread comes to be shared", () => { + const unshared = observeSharedMessages( + NO_SHARED_MESSAGES_SEEN, + [message("old")], + false, + clock(2), + ); + const shared = observeSharedMessages(unshared, [message("old")], true, clock(5)); + expect(shared.sharedSince).toBe(5); + expect(sharedMessageAnsweredFor({ messageId: "old", readAt: 1, seen: shared })).toBe(true); + }); + + it("holds a message first seen once the thread may be shared, until a read answers for it", () => { + const seen = observeSharedMessages(NO_SHARED_MESSAGES_SEEN, [message("new")], true, clock(5)); + expect(sharedMessageAnsweredFor({ messageId: "new", readAt: 1, seen })).toBe(false); + expect(sharedMessageAnsweredFor({ messageId: "new", readAt: 6, seen })).toBe(true); + }); + + it("stores nothing new while nothing has changed", () => { + const seen = observeSharedMessages(NO_SHARED_MESSAGES_SEEN, [message("a")], true, clock(5)); + expect(observeSharedMessages(seen, [message("a")], true, clock(9))).toBe(seen); + }); + + it("forgets the thread was shared when a read says it is not", () => { + const seen = observeSharedMessages(NO_SHARED_MESSAGES_SEEN, [], true, clock(5)); + expect(observeSharedMessages(seen, [], false, clock(9)).sharedSince).toBeNull(); + }); +}); diff --git a/packages/client-runtime/src/j5/sharedConversation.ts b/packages/client-runtime/src/j5/sharedConversation.ts index 2b275207b9bd..f588d88a7eb3 100644 --- a/packages/client-runtime/src/j5/sharedConversation.ts +++ b/packages/client-runtime/src/j5/sharedConversation.ts @@ -157,14 +157,17 @@ export function mayBeSharedConversation(input: { export interface SeenSharedMessages { readonly firstSeen: ReadonlyMap; readonly typedHere: ReadonlyMap; + /** Since when this client has held the thread may be shared; null while it holds it isn't. */ + readonly sharedSince: number | null; } export const NO_SHARED_MESSAGES_SEEN: SeenSharedMessages = { firstSeen: new Map(), typedHere: new Map(), + sharedSince: null, }; -/** `seen` itself when the entries hold nothing it lacks, so render stores a new record only then. */ +/** `seen` itself when nothing it records has changed, so render stores a new record only then. */ export function observeSharedMessages( seen: SeenSharedMessages, messages: ReadonlyArray<{ @@ -173,10 +176,14 @@ export function observeSharedMessages( readonly text: string; readonly createdBy?: unknown; }>, + mayBeShared: boolean, now: () => number, ): SeenSharedMessages { + let at: number | null = null; + const stamp = () => (at ??= now()); let firstSeen: Map | null = null; let typedHere: Map | null = null; + const sharedSince = mayBeShared ? (seen.sharedSince ?? stamp()) : null; for (const message of messages) { if (message.role !== "user") continue; if (message.createdBy === undefined) { @@ -185,11 +192,33 @@ export function observeSharedMessages( typedHere.set(message.id, message.text); } else if (!(firstSeen ?? seen.firstSeen).has(message.id)) { firstSeen ??= new Map(seen.firstSeen); - firstSeen.set(message.id, now()); + firstSeen.set(message.id, stamp()); } } - if (firstSeen === null && typedHere === null) return seen; - return { firstSeen: firstSeen ?? seen.firstSeen, typedHere: typedHere ?? seen.typedHere }; + if (firstSeen === null && typedHere === null && sharedSince === seen.sharedSince) return seen; + return { + firstSeen: firstSeen ?? seen.firstSeen, + typedHere: typedHere ?? seen.typedHere, + sharedSince, + }; +} + +/** + * Whether nothing more is coming about who wrote a server message, so it need not be held: the + * read came back after this client first saw it, or it was on screen before the thread could be + * shared. A row already drawn never leaves the list to wait for a read. + */ +export function sharedMessageAnsweredFor(input: { + readonly messageId: string; + readonly readAt: number | null; + readonly seen: SeenSharedMessages; +}): boolean { + const firstSeenAt = input.seen.firstSeen.get(input.messageId) ?? Infinity; + const { sharedSince } = input.seen; + return ( + readAnswersFor({ readAt: input.readAt, firstSeenAt }) || + (sharedSince !== null && firstSeenAt < sharedSince) + ); } /** From 66ab8f5ee1bb617aa4e47489f8494ff833217c3e Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Sun, 1 Nov 2026 21:53:51 -0500 Subject: [PATCH 2/2] fix(mobile): a message kept on screen still asks the record who wrote it The hold's fix kept a drawn message but stopped its re-read, so a message first seen just before the thread was known to be shared could stay drawn as the reader's own. It now stays on screen and asks once more, on both clients. Refs #892 Co-Authored-By: Claude Opus 5.5 --- .../j5/sharing/sharedConversationFeed.test.ts | 14 +++++-- .../src/j5/sharing/sharedConversationFeed.ts | 9 ++++- .../sharing/sharedTimeline.browser.test.tsx | 40 +++++++++++++++++-- apps/web/src/j5/sharing/sharedTimeline.ts | 9 ++++- .../src/j5/sharedConversation.test.ts | 17 ++++++++ .../src/j5/sharedConversation.ts | 21 ++++++++++ 6 files changed, 101 insertions(+), 9 deletions(-) diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts index 417482114498..4627f5e2df3e 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts @@ -119,16 +119,22 @@ describe("presentSharedConversationFeed", () => { name: null, via: [], })); - const { feed, pendingKey } = presentSharedConversationFeed({ + const shared = { feed: [stored], record: record({ shared: false, authors: [] }), failed: false, - readAt: 1, seen: { ...seenAt(["m-5"], 2), sharedSince: 3 }, people: reaching, + }; + // It stays on screen, and the record is asked once more, since it may yet name the author. + expect(presentSharedConversationFeed({ ...shared, readAt: 1 })).toEqual({ + feed: [stored], + pendingKey: "m-5", + }); + expect(presentSharedConversationFeed({ ...shared, readAt: 4 })).toEqual({ + feed: [stored], + pendingKey: "", }); - expect(feed).toEqual([stored]); - expect(pendingKey).toBe(""); }); it("leaves a thread only one person writes to as stored", () => { diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts index f611d3caa0b3..ef4881214c72 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts @@ -5,6 +5,7 @@ import { mayBeSharedConversation, type SeenSharedMessages, sharedMessageAnsweredFor, + sharedMessageAwaitsRead, sharedMessageView, } from "@t3tools/client-runtime/j5/shared-conversation"; import { MessageId } from "@t3tools/contracts"; @@ -65,7 +66,13 @@ export function presentSharedConversationFeed(input: { mayBeShared, typedHere: seen.typedHere.get(message.id) ?? null, }); - if (view.kind === "plain") return [entry]; + if (view.kind === "plain") { + const awaitsRead = + fromServer && + sharedMessageAwaitsRead({ messageId: message.id, readAt, seen, mayBeShared, failed }); + if (awaitsRead) pending.push(message.id); + return [entry]; + } if (view.kind === "pending") { pending.push(message.id); return view.typed === null ? [] : [{ ...entry, message: { ...message, text: view.typed } }]; diff --git a/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx b/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx index 9aa0cf51efb1..807b90b484dd 100644 --- a/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx +++ b/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx @@ -9,16 +9,33 @@ const authors = { record: { shared: false, authors: [], answers: [] }, readAt: 1, failed: false, - refresh: () => {}, + refresh: vi.fn(), }; -const people = { data: { people: [] }, error: null }; +const person = (level: "owner" | "collaborator", you: boolean) => ({ + personId: null, + name: null, + level, + via: [], + firstOwner: you, + you, +}); +const onlyMe = { data: { people: [person("owner", true)] }, error: null }; +const twoWriters = { + data: { people: [person("owner", true), person("collaborator", false)] }, + error: null, +}; +const reach = { people: onlyMe }; +function setup() { + authors.refresh.mockClear(); + reach.people = onlyMe; +} vi.mock(import("./sharedConversation"), async (original) => ({ ...(await original()), useThreadAuthors: (() => authors) as never, })); vi.mock(import("./scopeAccess"), async (original) => ({ ...(await original()), - useScopePeople: (() => people) as never, + useScopePeople: (() => reach.people) as never, })); const threadRef = { @@ -35,6 +52,7 @@ const entries = [ describe("useSharedConversationEntries", () => { it("hands the timeline the same entries while nothing it reads has changed", async () => { + setup(); const seen: Array> = []; function Probe(props: { readonly tick: number }) { seen.push(useSharedConversationEntries(threadRef, entries)); @@ -47,4 +65,20 @@ describe("useSharedConversationEntries", () => { expect(seen.length).toBeGreaterThan(settled); expect(seen.at(-1)).toBe(seen[settled - 1]); }); + + it("keeps a message on screen when the thread becomes shared, and asks about it once", async () => { + setup(); + const seen: Array> = []; + function Probe(props: { readonly tick: number }) { + seen.push(useSharedConversationEntries(threadRef, entries)); + return {props.tick}; + } + // The hook reads no route. + await using screen = await render(, { router: false }); + expect(authors.refresh).not.toHaveBeenCalled(); + reach.people = twoWriters; + await screen.rerender(); + expect(seen.at(-1)?.map((entry) => entry.id)).toEqual(["entry-1"]); + expect(authors.refresh).toHaveBeenCalledTimes(1); + }); }); diff --git a/apps/web/src/j5/sharing/sharedTimeline.ts b/apps/web/src/j5/sharing/sharedTimeline.ts index acd67e2436a8..e4a2912170bf 100644 --- a/apps/web/src/j5/sharing/sharedTimeline.ts +++ b/apps/web/src/j5/sharing/sharedTimeline.ts @@ -14,6 +14,7 @@ import { recallableMessages, type SharedMessageView, sharedMessageAnsweredFor, + sharedMessageAwaitsRead, sharedMessageView, typedText, } from "@t3tools/client-runtime/j5/shared-conversation"; @@ -187,7 +188,13 @@ export function useSharedConversationEntries( typedHere: seen.typedHere.get(message.id) ?? null, }); views.set(message, view); - if (view.kind === "plain") return [entry]; + if (view.kind === "plain") { + const awaitsRead = + fromServer && + sharedMessageAwaitsRead({ messageId: message.id, readAt, seen, mayBeShared, failed }); + if (awaitsRead) pending.push(message.id); + return [entry]; + } if (view.kind === "pending") { pending.push(message.id); return view.typed === null ? [] : [{ ...entry, message: { ...message, text: view.typed } }]; diff --git a/packages/client-runtime/src/j5/sharedConversation.test.ts b/packages/client-runtime/src/j5/sharedConversation.test.ts index 72f163b5f043..d380d97c8da9 100644 --- a/packages/client-runtime/src/j5/sharedConversation.test.ts +++ b/packages/client-runtime/src/j5/sharedConversation.test.ts @@ -11,6 +11,7 @@ import { NO_SHARED_MESSAGES_SEEN, observeSharedMessages, sharedMessageAnsweredFor, + sharedMessageAwaitsRead, } from "./sharedConversation.ts"; const author = (overrides: Partial = {}): MessageAuthor => ({ @@ -223,6 +224,22 @@ describe("holding a message for its author", () => { expect(sharedMessageAnsweredFor({ messageId: "new", readAt: 6, seen })).toBe(true); }); + it("still asks the record about a message left on screen, until a read answers for it", () => { + const seen = { ...NO_SHARED_MESSAGES_SEEN, firstSeen: new Map([["old", 2]]), sharedSince: 5 }; + const asks = (overrides: { readAt: number; mayBeShared?: boolean; failed?: boolean }) => + sharedMessageAwaitsRead({ + messageId: "old", + seen, + mayBeShared: true, + failed: false, + ...overrides, + }); + expect(asks({ readAt: 1 })).toBe(true); + expect(asks({ readAt: 3 })).toBe(false); + expect(asks({ readAt: 1, mayBeShared: false })).toBe(false); + expect(asks({ readAt: 1, failed: true })).toBe(false); + }); + it("stores nothing new while nothing has changed", () => { const seen = observeSharedMessages(NO_SHARED_MESSAGES_SEEN, [message("a")], true, clock(5)); expect(observeSharedMessages(seen, [message("a")], true, clock(9))).toBe(seen); diff --git a/packages/client-runtime/src/j5/sharedConversation.ts b/packages/client-runtime/src/j5/sharedConversation.ts index f588d88a7eb3..9495e045ff06 100644 --- a/packages/client-runtime/src/j5/sharedConversation.ts +++ b/packages/client-runtime/src/j5/sharedConversation.ts @@ -221,6 +221,27 @@ export function sharedMessageAnsweredFor(input: { ); } +/** + * Whether a message left on screen still wants a read: the record may yet name who wrote it, so it + * is asked once more, though the row stays put meanwhile. + */ +export function sharedMessageAwaitsRead(input: { + readonly messageId: string; + readonly readAt: number | null; + readonly seen: SeenSharedMessages; + readonly mayBeShared: boolean; + readonly failed: boolean; +}): boolean { + return ( + input.mayBeShared && + !input.failed && + !readAnswersFor({ + readAt: input.readAt, + firstSeenAt: input.seen.firstSeen.get(input.messageId) ?? Infinity, + }) + ); +} + /** * A thread's queued messages with the words their authors typed, for the queue's rows and the * composer an edit loads (Shared conversations AC14): the server records a message's author when