From 352e65d3612fd19cf165791f649056141cb23687 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 8 Jul 2026 00:00:51 +0000 Subject: [PATCH] fix(mobile): harden New Thread widget sync and pin matching Retry failed snapshot writes on a timer, sync the full project set so low-activity pins resolve, and disambiguate duplicate titles so pins cannot deep-link the wrong environment. --- .../threads/new-thread-widget.test.ts | 60 ++++++++++++++--- .../src/features/threads/new-thread-widget.ts | 61 ++++++++++++++---- .../threads/use-new-thread-widget-sync.ts | 64 ++++++++++++++----- apps/mobile/src/widgets/NewThread.test.ts | 56 ++++++++++++++++ apps/mobile/src/widgets/NewThread.tsx | 30 +++++---- 5 files changed, 224 insertions(+), 47 deletions(-) diff --git a/apps/mobile/src/features/threads/new-thread-widget.test.ts b/apps/mobile/src/features/threads/new-thread-widget.test.ts index d19979b169..50781f7c9c 100644 --- a/apps/mobile/src/features/threads/new-thread-widget.test.ts +++ b/apps/mobile/src/features/threads/new-thread-widget.test.ts @@ -4,10 +4,7 @@ import type { EnvironmentThreadShell, } from "@t3tools/client-runtime/state/shell"; -import { - makeNewThreadWidgetProps, - NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT, -} from "./new-thread-widget"; +import { makeNewThreadWidgetProps } from "./new-thread-widget"; function makeProject(overrides: { readonly id: string; @@ -81,9 +78,10 @@ describe("makeNewThreadWidgetProps", () => { expect(props.projects?.map((project) => project.title)).toEqual(["two", "one"]); }); - it("caps the payload at the widget's sync limit", () => { + it("syncs every project so low-activity pins can still resolve", () => { + const count = 25; const props = makeNewThreadWidgetProps( - Array.from({ length: NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT + 5 }, (_, n) => + Array.from({ length: count }, (_, n) => makeProject({ id: `p${n}`, updatedAt: `2026-06-${String(n + 1).padStart(2, "0")}T00:00:00.000Z`, @@ -91,8 +89,54 @@ describe("makeNewThreadWidgetProps", () => { ), [], ); - expect(props.projects).toHaveLength(NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT); - expect(props.projects?.[0]?.title).toBe(`p${NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT + 4}`); + expect(props.projects).toHaveLength(count); + expect(props.projects?.[0]?.title).toBe(`p${count - 1}`); + expect(props.projects?.[count - 1]?.title).toBe("p0"); + }); + + it("disambiguates duplicate titles with environment labels", () => { + const props = makeNewThreadWidgetProps( + [ + makeProject({ + id: "proj-a", + environmentId: "env-1", + title: "t3code", + updatedAt: "2026-07-07T00:00:00.000Z", + }), + makeProject({ + id: "proj-b", + environmentId: "env-2", + title: "t3code", + updatedAt: "2026-07-01T00:00:00.000Z", + }), + ], + [], + new Map([ + ["env-1", "Laptop"], + ["env-2", "Desktop"], + ]), + ); + expect(props.projects?.map((project) => project.title)).toEqual([ + "t3code (Laptop)", + "t3code (Desktop)", + ]); + expect(props.projects?.[0]?.deepLink).toContain("environmentId=env-1"); + expect(props.projects?.[0]?.deepLink).toContain("title=t3code"); + expect(props.projects?.[1]?.deepLink).toContain("environmentId=env-2"); + }); + + it("falls back to environment id when labels are missing for duplicates", () => { + const props = makeNewThreadWidgetProps( + [ + makeProject({ id: "a", environmentId: "env-1", title: "shared" }), + makeProject({ id: "b", environmentId: "env-2", title: "shared" }), + ], + [], + ); + expect(props.projects?.map((project) => project.title)).toEqual([ + "shared (env-1)", + "shared (env-2)", + ]); }); it("URL-encodes deep link parameters", () => { diff --git a/apps/mobile/src/features/threads/new-thread-widget.ts b/apps/mobile/src/features/threads/new-thread-widget.ts index 07ea6a08a8..7a08f71ce0 100644 --- a/apps/mobile/src/features/threads/new-thread-widget.ts +++ b/apps/mobile/src/features/threads/new-thread-widget.ts @@ -5,24 +5,26 @@ import type { import type { NewThreadWidgetProps } from "../../widgets/NewThread"; -// Candidate pool synced to the widget. Deliberately larger than any family -// displays (medium shows 3 rows, large 7): the "pinned projects" widget -// configuration is per widget instance and only visible inside the widget -// process, so the layout matches pins against this pool โ€” a pin should still -// resolve when its project is not among the most recent few. -export const NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT = 20; - /** - * Builds the home-screen widget payload: the most-recently-active projects, - * each with a deep link into the new-task draft composer for that project. + * Builds the home-screen widget payload: all projects ordered by recent + * activity, each with a deep link into the new-task draft composer. + * + * The full set is synced (not just the visible rows) so pinned-project matching + * inside the widget can resolve a deliberately pinned low-activity project. + * Medium shows 3 rows and large 7; the widget layout slices after pin matching. * * "Recently active" means the newest thread update in the project, falling * back to the project's own updatedAt for projects with no threads โ€” so the * widget surfaces where the user actually works, not what was created last. + * + * When two projects share a title, the display title is disambiguated with the + * environment label (falling back to environment id, then project id) so pin + * matching and the deep link target the intended workspace. */ export function makeNewThreadWidgetProps( projects: ReadonlyArray, threads: ReadonlyArray, + environmentLabels: ReadonlyMap = new Map(), ): NewThreadWidgetProps { const latestThreadActivity = new Map(); for (const thread of threads) { @@ -44,12 +46,14 @@ export function makeNewThreadWidgetProps( }; const ordered = [...projects].sort((a, b) => lastActivity(b).localeCompare(lastActivity(a))); + const displayTitles = disambiguateProjectTitles(ordered, environmentLabels); return { - projects: ordered.slice(0, NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT).map((project) => ({ - title: project.title, + projects: ordered.map((project, index) => ({ + title: displayTitles[index] ?? project.title, // Matches the NewTaskSheet > NewTaskDraft linking config in Stack.tsx; - // the widget layout prefixes the scheme after a safety check. + // the widget layout prefixes the scheme after a safety check. Keep the + // original project title in the query โ€” not the disambiguated display. deepLink: `/new/draft?environmentId=${encodeURIComponent(String(project.environmentId))}` + `&projectId=${encodeURIComponent(String(project.id))}` + @@ -58,6 +62,39 @@ export function makeNewThreadWidgetProps( }; } +function disambiguateProjectTitles( + projects: ReadonlyArray, + environmentLabels: ReadonlyMap, +): string[] { + const titleCounts = countBy((project) => project.title.toLowerCase(), projects); + const withEnvironment = projects.map((project) => { + if ((titleCounts.get(project.title.toLowerCase()) ?? 0) <= 1) { + return project.title; + } + const envLabel = + environmentLabels.get(String(project.environmentId)) ?? String(project.environmentId); + return `${project.title} (${envLabel})`; + }); + + const envTitleCounts = countBy((title) => title.toLowerCase(), withEnvironment); + return withEnvironment.map((title, index) => { + if ((envTitleCounts.get(title.toLowerCase()) ?? 0) <= 1) { + return title; + } + const project = projects[index]; + return project ? `${title} ยท ${project.id}` : title; + }); +} + +function countBy(keyOf: (value: T) => string, values: ReadonlyArray): Map { + const counts = new Map(); + for (const value of values) { + const key = keyOf(value); + counts.set(key, (counts.get(key) ?? 0) + 1); + } + return counts; +} + function projectActivityKey(environmentId: string, projectId: string): string { return `${environmentId}\u0000${projectId}`; } diff --git a/apps/mobile/src/features/threads/use-new-thread-widget-sync.ts b/apps/mobile/src/features/threads/use-new-thread-widget-sync.ts index 9c3d1167a1..a075c92442 100644 --- a/apps/mobile/src/features/threads/use-new-thread-widget-sync.ts +++ b/apps/mobile/src/features/threads/use-new-thread-widget-sync.ts @@ -1,7 +1,8 @@ -import { useEffect } from "react"; +import { useEffect, useMemo } from "react"; import { Platform } from "react-native"; import { useProjects, useThreadShells } from "../../state/entities"; +import { useEnvironments } from "../../state/environments"; import NewThreadWidget from "../../widgets/NewThread"; import { makeNewThreadWidgetProps } from "./new-thread-widget"; @@ -10,6 +11,9 @@ import { makeNewThreadWidgetProps } from "./new-thread-widget"; // in a WidgetCenter reload, and iOS throttles widgets that reload too often. let lastSyncedSignature: string | null = null; +/** Delay before retrying a failed snapshot write while entity data is unchanged. */ +const SNAPSHOT_RETRY_MS = 5_000; + /** * Keeps the NewThread home-screen widget's project shortcuts in sync with the * workspace. Writing the snapshot also persists the widget layout to the @@ -19,25 +23,53 @@ let lastSyncedSignature: string | null = null; export function useNewThreadWidgetSync(): void { const projects = useProjects(); const threads = useThreadShells(); + const { environments } = useEnvironments(); + const environmentLabels = useMemo(() => { + const labels = new Map(); + for (const environment of environments) { + labels.set(String(environment.environmentId), environment.label); + } + return labels; + }, [environments]); useEffect(() => { if (Platform.OS !== "ios") { return; } - const props = makeNewThreadWidgetProps(projects, threads); - const signature = JSON.stringify(props); - if (signature === lastSyncedSignature) { - return; - } - lastSyncedSignature = signature; - try { - NewThreadWidget.updateSnapshot(props); - } catch (error) { - // Clear the signature so the next data change retries the write. - lastSyncedSignature = null; - if (__DEV__) { - console.warn("[new-thread-widget] snapshot update failed", error); + + let cancelled = false; + let retryTimer: ReturnType | null = null; + + const sync = () => { + if (cancelled) { + return; } - } - }, [projects, threads]); + const props = makeNewThreadWidgetProps(projects, threads, environmentLabels); + const signature = JSON.stringify(props); + if (signature === lastSyncedSignature) { + return; + } + try { + NewThreadWidget.updateSnapshot(props); + lastSyncedSignature = signature; + } catch (error) { + // Do not treat the write as succeeded. Schedule a retry so a transient + // app-group failure still recovers when projects/threads stay the same. + lastSyncedSignature = null; + if (__DEV__) { + console.warn("[new-thread-widget] snapshot update failed", error); + } + retryTimer = setTimeout(sync, SNAPSHOT_RETRY_MS); + } + }; + + sync(); + + return () => { + cancelled = true; + if (retryTimer !== null) { + clearTimeout(retryTimer); + } + }; + }, [projects, threads, environmentLabels]); } diff --git a/apps/mobile/src/widgets/NewThread.test.ts b/apps/mobile/src/widgets/NewThread.test.ts index d02f615a6c..18d8b9fc01 100644 --- a/apps/mobile/src/widgets/NewThread.test.ts +++ b/apps/mobile/src/widgets/NewThread.test.ts @@ -174,6 +174,62 @@ describe("NewThread widget layout", () => { expect(layout).not.toContain("Project 3"); }); + it("skips ambiguous pin titles instead of linking the first match", () => { + const layout = JSON.stringify( + NewThread( + { + projects: [ + makeProject({ + title: "t3code (Laptop)", + deepLink: "/new/draft?environmentId=env-1&projectId=a&title=t3code", + }), + makeProject({ + title: "t3code (Desktop)", + deepLink: "/new/draft?environmentId=env-2&projectId=b&title=t3code", + }), + makeProject({ title: "recent", deepLink: "/new/draft?projectId=recent" }), + ], + }, + makeEnvironment({ + widgetFamily: "systemMedium", + configuration: { pinnedProjects: "t3code", showRecent: false }, + }), + ), + ); + // Ambiguous bare title must not deep-link either duplicate. + expect(layout).not.toContain("environmentId=env-1"); + expect(layout).not.toContain("environmentId=env-2"); + expect(layout).toContain('"widgetURL":"t3code://new"'); + }); + + it("pins a disambiguated title to the intended environment", () => { + const layout = JSON.stringify( + NewThread( + { + projects: [ + makeProject({ + title: "t3code (Laptop)", + deepLink: "/new/draft?environmentId=env-1&projectId=a&title=t3code", + }), + makeProject({ + title: "t3code (Desktop)", + deepLink: "/new/draft?environmentId=env-2&projectId=b&title=t3code", + }), + ], + }, + makeEnvironment({ + widgetFamily: "systemMedium", + configuration: { pinnedProjects: "t3code (Desktop)", showRecent: false }, + }), + ), + ); + expect(layout).toContain("t3code (Desktop)"); + expect(layout).toContain( + '"destination":"t3code://new/draft?environmentId=env-2&projectId=b&title=t3code"', + ); + expect(layout).not.toContain("environmentId=env-1"); + }); + it("hides recents when the fill toggle is off", () => { const layout = JSON.stringify( NewThread( diff --git a/apps/mobile/src/widgets/NewThread.tsx b/apps/mobile/src/widgets/NewThread.tsx index 2872298f53..1d954d610a 100644 --- a/apps/mobile/src/widgets/NewThread.tsx +++ b/apps/mobile/src/widgets/NewThread.tsx @@ -22,10 +22,10 @@ export interface NewThreadWidgetProps { /** * Candidate projects, most recently active first. The medium/large widgets * render a slice of these as one-tap shortcuts into the draft composer. - * More are synced than any family displays: pinned-project matching happens - * here in the widget (the configuration is per widget instance, so the app - * cannot precompute it), and a pin should resolve even when its project has - * fallen out of the top displayed rows. + * The full workspace set is synced (not just the visible rows): pinned- + * project matching happens here in the widget (the configuration is per + * widget instance, so the app cannot precompute it), and a pin should + * resolve even when its project has fallen out of the top displayed rows. * Optional because WidgetKit renders placeholder/gallery entries with no * props before the app has ever written a timeline. */ @@ -83,7 +83,9 @@ export function NewThread( // Pins are typed titles (see app.config.ts for why there is no picker). // Match case-insensitively, exact title first and then prefix, preserving - // the user's pin order. Unmatched pins (typo, deleted project) are skipped. + // the user's pin order. A query that matches more than one remaining project + // is skipped (no ambiguous deep link) rather than picking the first by + // activity order. Unmatched pins (typo, deleted project) are skipped. const pinnedQueries = ( typeof configuration?.pinnedProjects === "string" ? configuration.pinnedProjects : "" ) @@ -92,13 +94,19 @@ export function NewThread( .filter((title) => title.length > 0); const pinned: NewThreadWidgetProject[] = []; for (const query of pinnedQueries) { - const match = - available.find( - (project) => !pinned.includes(project) && project.title.toLowerCase() === query, - ) ?? - available.find( - (project) => !pinned.includes(project) && project.title.toLowerCase().startsWith(query), + const unused = available.filter((project) => !pinned.includes(project)); + const exactMatches = unused.filter((project) => project.title.toLowerCase() === query); + let match: NewThreadWidgetProject | undefined; + if (exactMatches.length === 1) { + match = exactMatches[0]; + } else if (exactMatches.length === 0) { + const prefixMatches = unused.filter((project) => + project.title.toLowerCase().startsWith(query), ); + if (prefixMatches.length === 1) { + match = prefixMatches[0]; + } + } if (match) { pinned.push(match); }