diff --git a/FORK.md b/FORK.md index 04e651f1a8584..8ed372200d74f 100644 --- a/FORK.md +++ b/FORK.md @@ -512,7 +512,7 @@ migrations after upstream migrations. 98. Mobile reads artifacts ([#833](https://github.com/Jacksondr5/j5code/issues/833)), landing two reserved mobile seams. Route registration: `apps/mobile/src/Stack.tsx` imports J5-owned `j5ThreadArtifactRoutes` (`apps/mobile/src/j5/artifacts/routes.ts`) and spreads it after `ThreadFile`, adding the thread-scoped `ThreadArtifacts` list and `ThreadArtifact` viewer (`apps/mobile/src/j5/artifacts/ThreadArtifactsScreen.tsx`), read-only, with no delete. Link resolver: `onMarkdownLinkPress` in `apps/mobile/src/features/threads/ThreadFeed.tsx` makes one call to J5-owned `openArtifactLink` before its thread, media and file branches, so an `artifacts/…` link opens the viewer instead of a missing workspace file; its `fileContextMenu` and `onFileContextMenuAction` each make one call first (`artifactLinkContextMenu`, `onArtifactLinkMenuAction`), so an artifact link's long press offers Open artifact and Copy path instead of workspace-file actions. Links inside a Markdown artifact go through the same helpers in the J5 viewer. Recognition, preview kind and the HTTP read are shared with web in `packages/client-runtime/src/j5/artifacts.ts`. No upstream entry point reaches the list yet; it is one tap from any opened artifact. -99. Mobile's shared conversations show who wrote each message and who answered each prompt, the mobile half of case 80 ([#832](https://github.com/Jacksondr5/j5code/issues/832), register D33, Shared conversations AC9 to AC11, AC14 and AC16). The record and its change feed are `threadAuthors` and `threadAuthorsChanges` in `packages/client-runtime/src/j5/sharing.ts`, and the reading of it (`sharedMessageView`, the typed text, the answers without their mark, the search label) is `packages/client-runtime/src/j5/sharedConversation.ts`, both shared with web. `apps/mobile/src/features/threads/ThreadRouteScreen.tsx` imports `useSharedConversationFeed` from J5-owned `apps/mobile/src/j5/sharing/sharedConversation.tsx` and passes its answer as `ThreadDetailScreen`'s `selectedThreadFeed` in place of `composer.selectedThreadFeed`: an authored message shows as typed, after its note to the agent as a muted line that opens (drawn by case 97's `renderJ5TimelineNotice`), a message whose author is not read yet is held back or shown as this client typed it, and a question's answers lose the platform's `[Answer from …]` mark. Case 97's `useJ5Timeline` also reads the record, so `resolveJ5FeedMessage` puts another person's message on the left and `J5MessageAuthorLine` names them, or says who edited the reader's own. `apps/mobile/src/features/threads/thread-work-log.tsx` mounts `PromptAnsweredBy` after a work row's label (" · answered by Priya" on a settled question or approval). `apps/mobile/src/features/threads/ThreadQueueControl.tsx` reads its rows through `useSharedQueuedRuns`, so a queued message's row and the text an edit loads are the words typed. `apps/mobile/src/features/threads/thread-search-match.tsx` labels a hit with `threadSearchMatchLabel` instead of "You:" for every user-role hit. `apps/mobile/src/lib/filePreview.test.ts` mocks `sharedConversation.tsx` as it mocks case 97's `timelineState.ts`. Proofs: `apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts`, `apps/mobile/src/j5/a2a/timelinePresentation.test.ts`, `packages/client-runtime/src/j5/sharedConversation.test.ts`, `packages/client-runtime/src/j5/threadSearchMatchLabel.test.ts`, and the four files' rows in `apps/mobile/src/j5/upstreamSeams.test.ts`. On every rebase, keep the calls that test lists. +99. Mobile's shared conversations show who wrote each message and who answered each prompt, the mobile half of case 80 ([#832](https://github.com/Jacksondr5/j5code/issues/832), register D33, Shared conversations AC9 to AC11, AC14 and AC16). The record and its change feed are `threadAuthors` and `threadAuthorsChanges` in `packages/client-runtime/src/j5/sharing.ts`, beside who reaches the thread (`scopePeople` and `scopePeopleChanges`), which says the thread is shared before any author is recorded (`mayBeSharedConversation`, [#871](https://github.com/Jacksondr5/j5code/issues/871)); and the reading of it (`sharedMessageView`, the typed text, the answers without their mark, the search label) is `packages/client-runtime/src/j5/sharedConversation.ts`, both shared with web. `apps/mobile/src/features/threads/ThreadRouteScreen.tsx` imports `useSharedConversationFeed` from J5-owned `apps/mobile/src/j5/sharing/sharedConversation.tsx` and passes its answer as `ThreadDetailScreen`'s `selectedThreadFeed` in place of `composer.selectedThreadFeed`: an authored message shows as typed, after its note to the agent as a muted line that opens (drawn by case 97's `renderJ5TimelineNotice`), a message whose author is not read yet is held back or shown as this client typed it, and a question's answers lose the platform's `[Answer from …]` mark. Case 97's `useJ5Timeline` also reads the record, so `resolveJ5FeedMessage` puts another person's message on the left and `J5MessageAuthorLine` names them, or says who edited the reader's own. `apps/mobile/src/features/threads/thread-work-log.tsx` mounts `PromptAnsweredBy` after a work row's label (" · answered by Priya" on a settled question or approval). `apps/mobile/src/features/threads/ThreadQueueControl.tsx` reads its rows through `useSharedQueuedRuns`, so a queued message's row and the text an edit loads are the words typed. `apps/mobile/src/features/threads/thread-search-match.tsx` labels a hit with `threadSearchMatchLabel` instead of "You:" for every user-role hit. `apps/mobile/src/lib/filePreview.test.ts` mocks `sharedConversation.tsx` as it mocks case 97's `timelineState.ts`. Proofs: `apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts`, `apps/mobile/src/j5/a2a/timelinePresentation.test.ts`, `packages/client-runtime/src/j5/sharedConversation.test.ts`, `packages/client-runtime/src/j5/threadSearchMatchLabel.test.ts`, and the four files' rows in `apps/mobile/src/j5/upstreamSeams.test.ts`. On every rebase, keep the calls that test lists. ## Pin and upstream advance runbook diff --git a/apps/mobile/src/j5/sharing/sharedConversation.tsx b/apps/mobile/src/j5/sharing/sharedConversation.tsx index 9e5b6c71ab149..9a9a70f9b1fbd 100644 --- a/apps/mobile/src/j5/sharing/sharedConversation.tsx +++ b/apps/mobile/src/j5/sharing/sharedConversation.tsx @@ -25,6 +25,32 @@ const NO_RECORD = Atom.make(AsyncResult.initial(false)) as ReturnType< const NO_CHANGES = Atom.make(AsyncResult.initial(false)) as ReturnType< typeof j5Sharing.threadAuthorsChanges >; +const NO_PEOPLE = Atom.make(AsyncResult.initial(false)) as ReturnType; +const NO_PEOPLE_CHANGES = Atom.make(AsyncResult.initial(false)) as ReturnType< + typeof j5Sharing.scopePeopleChanges +>; + +/** + * Who reaches a thread, read again whenever the server says it changed, so a thread shared while + * it is open is known to be shared before anyone else's first message arrives. + */ +function useThreadPeople(environmentId: EnvironmentId | null, threadId: ThreadId | null) { + const target = + environmentId === null || threadId === null + ? null + : { environmentId, input: { scope: { kind: "thread" as const, threadId } } }; + const query = target === null ? NO_PEOPLE : j5Sharing.scopePeople(target); + const people = useAtomValue(query); + const changes = useAtomValue( + target === null ? NO_PEOPLE_CHANGES : j5Sharing.scopePeopleChanges(target), + ); + const revision = Option.getOrNull(AsyncResult.value(changes))?.revision; + // oxlint-disable-next-line t3code/react-effect-budget -- rereads who reaches the thread when the server's change counter moves. + useEffect(() => { + if (revision !== undefined && revision > 0) appAtomRegistry.refresh(query); + }, [query, revision]); + return Option.getOrNull(AsyncResult.value(people))?.people ?? null; +} function useThreadAuthorsResult(environmentId: EnvironmentId | null, threadId: ThreadId | null) { const result = useAtomValue( @@ -60,7 +86,8 @@ export function useSharedQueuedRuns< /** * Shared conversations AC16 for the native timeline (`presentSharedConversationFeed`), from the - * record of who sent each message, read again whenever the server says it changed. + * record of who sent each message and who reaches the thread, each read again whenever the server + * says it changed. */ export function useSharedConversationFeed( environmentId: EnvironmentId | null, @@ -71,6 +98,7 @@ export function useSharedConversationFeed( environmentId === null || threadId === null ? null : { environmentId, input: { threadId } }; const query = target === null ? NO_RECORD : j5Sharing.threadAuthors(target); const { record, failed, readAt } = useThreadAuthorsResult(environmentId, threadId); + const people = useThreadPeople(environmentId, threadId); const changes = useAtomValue( target === null ? NO_CHANGES : j5Sharing.threadAuthorsChanges(target), ); @@ -94,6 +122,7 @@ export function useSharedConversationFeed( failed, readAt, seen, + people, }); // oxlint-disable-next-line t3code/react-effect-budget -- asks once more about a message waiting on its author, in case the change feed is slow. useEffect(() => { diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts index 24f11dba9e682..7263fc52452d8 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts @@ -58,6 +58,7 @@ describe("presentSharedConversationFeed", () => { failed: false, readAt: 2, seen: seenAt(["m-1"], 1), + people: null, }); expect(texts(feed)).toEqual([ ["m-1:shared-note", "[Note for the agent]"], @@ -75,11 +76,38 @@ describe("presentSharedConversationFeed", () => { failed: false, readAt: null, seen: seenAt(["m-2"], 1), + people: null, }); expect(feed).toEqual([]); expect(pendingKey).toBe("m-2"); }); + it("holds a new writer's first message in a thread shared after its record was read", () => { + // The record was read before the thread was shared, so it still says not shared. + const input = { + feed: [message("m-4", `${prefix}First words from Priya.`)], + record: record({ shared: false, authors: [] }), + failed: false, + readAt: 1, + seen: seenAt(["m-4"], 2), + }; + const owner = { level: "owner" as const, you: true, firstOwner: true }; + const collaborator = { level: "collaborator" as const, you: false, firstOwner: false }; + const reaching = (people: ReadonlyArray) => + people.map((person, index) => ({ + ...person, + personId: `human:${index}`, + name: null, + via: [], + })); + expect( + presentSharedConversationFeed({ ...input, people: reaching([owner, collaborator]) }), + ).toEqual({ feed: [], pendingKey: "m-4" }); + expect(presentSharedConversationFeed({ ...input, people: reaching([owner]) }).feed).toEqual( + input.feed, + ); + }); + it("leaves a thread only one person writes to as stored", () => { const stored = message("m-3", "Just me."); const { feed } = presentSharedConversationFeed({ @@ -88,6 +116,7 @@ describe("presentSharedConversationFeed", () => { failed: false, readAt: 2, seen: seenAt(["m-3"], 1), + people: null, }); expect(feed).toEqual([stored]); }); @@ -125,6 +154,7 @@ describe("presentSharedConversationFeed", () => { failed: false, readAt: 2, seen: NO_SHARED_MESSAGES_SEEN, + people: null, }); const shown = feed[0]; expect( diff --git a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts index dcfba672e9ded..15cf845e5799f 100644 --- a/apps/mobile/src/j5/sharing/sharedConversationFeed.ts +++ b/apps/mobile/src/j5/sharing/sharedConversationFeed.ts @@ -2,12 +2,13 @@ import { answersWithoutPrefix, authorIndex, markSharedNote, + mayBeSharedConversation, readAnswersFor, type SeenSharedMessages, sharedMessageView, } from "@t3tools/client-runtime/j5/shared-conversation"; import { MessageId } from "@t3tools/contracts"; -import type { ThreadAuthors } from "@t3tools/contracts/j5"; +import type { ScopePerson, ThreadAuthors } from "@t3tools/contracts/j5"; import type { ThreadFeedEntry } from "../../lib/threadActivity"; @@ -22,10 +23,11 @@ export function presentSharedConversationFeed(input: { /** When the latest read came back; null before one has. */ readonly readAt: number | null; readonly seen: SeenSharedMessages; + /** Who reaches the thread; null until that read answers, or when it fails. */ + readonly people: ReadonlyArray | null; }): { readonly feed: ReadonlyArray; readonly pendingKey: string } { const { record, failed, readAt, seen } = input; - // Until the first read answers or fails, any thread may be shared. - const mayBeShared = record?.shared === true || (record === null && !failed); + const mayBeShared = mayBeSharedConversation({ record, failed, people: input.people }); const { authors, answers } = authorIndex(record); const pending: string[] = []; const shown = input.feed.flatMap((entry): ReadonlyArray => { diff --git a/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx b/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx new file mode 100644 index 0000000000000..9aa0cf51efb18 --- /dev/null +++ b/apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx @@ -0,0 +1,50 @@ +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { render } from "../../../test/render"; +import { useSharedConversationEntries } from "./sharedTimeline"; + +// The record and the people read are atom values, the same objects until the server answers again. +const authors = { + record: { shared: false, authors: [], answers: [] }, + readAt: 1, + failed: false, + refresh: () => {}, +}; +const people = { data: { people: [] }, error: null }; +vi.mock(import("./sharedConversation"), async (original) => ({ + ...(await original()), + useThreadAuthors: (() => authors) as never, +})); +vi.mock(import("./scopeAccess"), async (original) => ({ + ...(await original()), + useScopePeople: (() => people) as never, +})); + +const threadRef = { + environmentId: EnvironmentId.make("environment-1"), + threadId: ThreadId.make("thread-1"), +}; +const entries = [ + { + id: "entry-1", + kind: "message", + message: { id: "message-1", role: "user", text: "Hello", createdBy: "user" }, + }, +]; + +describe("useSharedConversationEntries", () => { + it("hands the timeline the same entries while nothing it reads has changed", async () => { + 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 }); + const settled = seen.length; + await screen.rerender(); + expect(seen.length).toBeGreaterThan(settled); + expect(seen.at(-1)).toBe(seen[settled - 1]); + }); +}); diff --git a/apps/web/src/j5/sharing/sharedTimeline.ts b/apps/web/src/j5/sharing/sharedTimeline.ts index e3c315ef8ac3b..37732a7193254 100644 --- a/apps/web/src/j5/sharing/sharedTimeline.ts +++ b/apps/web/src/j5/sharing/sharedTimeline.ts @@ -1,10 +1,10 @@ import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts"; import type { MessageAuthor, PromptAnswer } from "@t3tools/contracts/j5"; -import { levelReaches } from "@t3tools/contracts/j5"; import { useEffect, useState } from "react"; import { authorIndex, + mayBeSharedConversation, answersWithoutPrefix, NO_SHARED_MESSAGES_SEEN, observeSharedMessages, @@ -134,15 +134,6 @@ export function useSharedConversationEntries( threadRef === null ? null : { kind: "thread" as const, threadId: threadRef.threadId }; // The header's pill already reads who reaches the thread; this is the same read. const people = useScopePeople(threadRef?.environmentId ?? null, threadScope); - const reaching = people.data?.people ?? []; - const writers = reaching.filter((person) => levelReaches(person.level, "collaborator")).length; - // To anyone but the first owner, a message no one is recorded for may be the first owner's, - // which the next read says; their own view draws it as theirs. - const readsOthers = reaching.length > 1 && !reaching.some((p) => p.you && p.firstOwner); - // Until the first read answers or fails, any thread may be shared, so nothing is drawn plain - // that may carry lines for the agent. - const mayBeShared = - record?.shared === true || writers > 1 || readsOthers || (record === null && !failed); const [stored, setStored] = useState(NO_SHARED_MESSAGES_SEEN); const seen = observeSharedMessages( @@ -152,6 +143,14 @@ export function useSharedConversationEntries( ); 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({ + record, + failed, + people: people.data?.people ?? null, + }); + const shown = (() => { const { authors, answers } = authorIndex(record); const pending: string[] = []; diff --git a/apps/web/src/j5/sharing/sharingManage.ts b/apps/web/src/j5/sharing/sharingManage.ts index fc477fb2c3596..ccf6012e58e1c 100644 --- a/apps/web/src/j5/sharing/sharingManage.ts +++ b/apps/web/src/j5/sharing/sharingManage.ts @@ -1,7 +1,6 @@ import { createEnvironmentRpcCommand, createEnvironmentRpcQueryAtomFamily, - createEnvironmentRpcSubscriptionAtomFamily, squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import type { EnvironmentId } from "@t3tools/contracts"; @@ -38,15 +37,8 @@ export const sharingManage = { refreshTrigger: j5Environment.rereadOn("sharing"), }), /** Who reaches one thread or project, for anyone who reaches it (AC26). */ - scopePeople: createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, { - label: "environment-data:j5-people:scope", - tag: J5_PEOPLE_WS_METHODS.listScopePeople, - staleTimeMs: 0, - }), - scopePeopleChanges: createEnvironmentRpcSubscriptionAtomFamily(connectionAtomRuntime, { - label: "environment-data:j5-people:scope-changes", - tag: J5_PEOPLE_WS_METHODS.subscribeScopePeopleChanges, - }), + scopePeople: j5Sharing.scopePeople, + scopePeopleChanges: j5Sharing.scopePeopleChanges, /** For a device the server cannot place: everyone it could be (AC5). */ claimablePeople: createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, { label: "environment-data:j5-access:claimable", diff --git a/packages/client-runtime/src/j5/sharedConversation.test.ts b/packages/client-runtime/src/j5/sharedConversation.test.ts index 920ea4c918834..dd9f8cd3cd95b 100644 --- a/packages/client-runtime/src/j5/sharedConversation.test.ts +++ b/packages/client-runtime/src/j5/sharedConversation.test.ts @@ -7,6 +7,7 @@ import { recallableMessages, sharedMessageView, typedText, + mayBeSharedConversation, } from "./sharedConversation.ts"; const author = (overrides: Partial = {}): MessageAuthor => ({ @@ -149,3 +150,47 @@ describe("up-arrow recall in a shared conversation", () => { ).toEqual(["Shipped."]); }); }); + +describe("mayBeSharedConversation", () => { + const person = ( + level: "viewer" | "collaborator" | "owner", + you: boolean, + firstOwner = false, + ) => ({ + level, + you, + firstOwner, + }); + const unshared = { shared: false, authors: [], answers: [] }; + + it("holds every message until the first read answers or fails", () => { + expect(mayBeSharedConversation({ record: null, failed: false, people: null })).toBe(true); + expect(mayBeSharedConversation({ record: null, failed: true, people: null })).toBe(false); + }); + + it("knows a thread is shared from who reaches it, before any author is recorded", () => { + const firstOwner = person("owner", true, true); + expect( + mayBeSharedConversation({ + record: unshared, + failed: false, + people: [firstOwner, person("collaborator", false)], + }), + ).toBe(true); + expect( + mayBeSharedConversation({ + record: unshared, + failed: false, + people: [firstOwner, person("viewer", false)], + }), + ).toBe(false); + // To a Viewer, a message nobody is recorded for may be the first owner's. + expect( + mayBeSharedConversation({ + record: unshared, + failed: false, + people: [person("owner", false, true), person("viewer", true)], + }), + ).toBe(true); + }); +}); diff --git a/packages/client-runtime/src/j5/sharedConversation.ts b/packages/client-runtime/src/j5/sharedConversation.ts index b9f0bb053a4de..2b275207b9bdc 100644 --- a/packages/client-runtime/src/j5/sharedConversation.ts +++ b/packages/client-runtime/src/j5/sharedConversation.ts @@ -1,6 +1,8 @@ import { + levelReaches, type MessageAuthor, type PromptAnswer, + type ScopePerson, type ThreadAuthors, type ThreadSearchAuthor, typedText, @@ -127,6 +129,27 @@ export const readAnswersFor = (input: { readonly firstSeenAt: number; }) => input.readAt !== null && input.readAt >= input.firstSeenAt; +/** + * Whether a message nobody is recorded for yet may carry lines for the agent. Who reaches the + * thread says so as soon as it is shared: more than one writer, or anyone else at all to a reader + * who is not the first owner. Until the first read answers or fails, any thread may be shared. + */ +export function mayBeSharedConversation(input: { + readonly record: ThreadAuthors | null; + readonly failed: boolean; + readonly people: ReadonlyArray> | null; +}): boolean { + const reaching = input.people ?? []; + const writers = reaching.filter((person) => levelReaches(person.level, "collaborator")).length; + const readsOthers = reaching.length > 1 && !reaching.some((p) => p.you && p.firstOwner); + return ( + input.record?.shared === true || + writers > 1 || + readsOthers || + (input.record === null && !input.failed) + ); +} + /** * When this client first saw each server copy of a person's message, and what its person typed for * the messages they sent from here, which are first drawn from what they typed. diff --git a/packages/client-runtime/src/j5/sharing.ts b/packages/client-runtime/src/j5/sharing.ts index d016032d3b49c..2e0ff565b38cf 100644 --- a/packages/client-runtime/src/j5/sharing.ts +++ b/packages/client-runtime/src/j5/sharing.ts @@ -3,6 +3,7 @@ import { type ConnectionAccess, type HeldPersonLevel, J5_ACCESS_WS_METHODS, + J5_PEOPLE_WS_METHODS, J5_SHARED_CONVERSATION_WS_METHODS, type PersonLevel, higherLevel, @@ -321,6 +322,16 @@ export function createSharingEnvironmentAtoms( label: "environment-data:j5-shared-conversation:authors-changes", tag: J5_SHARED_CONVERSATION_WS_METHODS.subscribeThreadAuthorsChanges, }); + /** Who reaches one thread or project, for anyone who reaches it (People and access AC26). */ + const scopePeople = createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:j5-people:scope", + tag: J5_PEOPLE_WS_METHODS.listScopePeople, + staleTimeMs: 0, + }); + const scopePeopleChanges = createEnvironmentRpcSubscriptionAtomFamily(runtime, { + label: "environment-data:j5-people:scope-changes", + tag: J5_PEOPLE_WS_METHODS.subscribeScopePeopleChanges, + }); /** * Whether the connection is known to reach less than the whole server; false until the server @@ -338,6 +349,8 @@ export function createSharingEnvironmentAtoms( isNarrow, threadAuthors, threadAuthorsChanges, + scopePeople, + scopePeopleChanges, /** Reads the connection's access again, after this client changed it (claim, leave, rename). */ refreshConnectionAccess: (registry: AtomRegistry.AtomRegistry, environmentId: EnvironmentId) => registry.refresh(connectionAccessQuery({ environmentId, input: {} })),