GeezerVison is showing github.com — fix: watch title regeneration before dispatch and report bulk failure… · pingdotgg/t3code@2bb52b8
File treeExpand file treeCollapse file tree Open diff view settings packages/client-runtime/src/state
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/home/useThreadListActions.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -121,7 +121,7 @@ vi.mock("../../state/thread-order", () => ({ | 121 | 121 | })); | 122 | 122 | vi.mock("../../lib/uuid", () => ({ uuidv4: () => "command" })); | 123 | 123 | vi.mock("../../state/threads", () => ({ | 124 | | - waitForTitleRegenerationFailure: async () => null, | | 124 | + watchThreadTitleRegeneration: () => ({ failure: Promise.resolve(null), cancel: () => {} }), | 125 | 125 | environmentThreadShells: { | 126 | 126 | threadShellsAtom: "thread-shells", | 127 | 127 | navigationThreadShellsAtom: "thread-shells", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/home/useThreadListActions.ts +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,7 @@ import { readEnvironmentScope } from "../../state/session"; | 21 | 21 | import { | 22 | 22 | environmentThreadShells, | 23 | 23 | threadEnvironment, | 24 | | - waitForTitleRegenerationFailure, | | 24 | + watchThreadTitleRegeneration, | 25 | 25 | } from "../../state/threads"; | 26 | 26 | import { queuedThreadKeysAtom } from "../../state/use-thread-outbox"; | 27 | 27 | import { useAtomCommand } from "../../state/use-atom-command"; | @@ -516,6 +516,10 @@ export function useThreadListActions(): { | 516 | 516 | titleRegenerationInFlightThreadKeys.current.add(key); | 517 | 517 | selectionHaptic(); | 518 | 518 | const commandId = CommandId.make(uuidv4()); | | 519 | + const watch = watchThreadTitleRegeneration( | | 520 | + { environmentId: thread.environmentId, threadId: thread.id }, | | 521 | + commandId, | | 522 | + ); | 519 | 523 | const succeeded = await (async () => { | 520 | 524 | const result = await updateThreadMetadata({ | 521 | 525 | environmentId: thread.environmentId, | @@ -535,12 +539,11 @@ export function useThreadListActions(): { | 535 | 539 | })(); | 536 | 540 | titleRegenerationInFlightThreadKeys.current.delete(key); | 537 | 541 | if (succeeded) { | 538 | | - void waitForTitleRegenerationFailure( | 539 | | - { environmentId: thread.environmentId, threadId: thread.id }, | 540 | | - commandId, | 541 | | - ).then((failure) => { | | 542 | + void watch.failure.then((failure) => { | 542 | 543 | if (failure !== null) Dialog.alert("Could not regenerate title", failure); | 543 | 544 | }); | | 545 | + } else { | | 546 | + watch.cancel(); | 544 | 547 | } | 545 | 548 | return succeeded; | 546 | 549 | }; | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/state/threads.ts +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createChildThreadApprovalsAtom } from "@t3tools/client-runtime/state/thread-requests"; | 2 | 2 | import { useAtomValue } from "@effect/atom-react"; | 3 | 3 | import { | 4 | | - awaitTitleRegenerationFailure, | 5 | 4 | createEnvironmentThreadDetailAtoms, | 6 | 5 | createEnvironmentThreadShellAtoms, | 7 | 6 | createEnvironmentThreadStateAtoms, | @@ -10,6 +9,8 @@ import { | 10 | 9 | createOlderSettledThreadAtoms, | 11 | 10 | createThreadEnvironmentAtoms, | 12 | 11 | createThreadFamilyAtoms, | | 12 | + type TitleRegenerationWatch, | | 13 | + watchTitleRegeneration, | 13 | 14 | } from "@t3tools/client-runtime/state/threads"; | 14 | 15 | import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts"; | 15 | 16 | import { AsyncResult, Atom } from "effect/reactivity"; | @@ -48,12 +49,12 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({ | 48 | 49 | hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom, | 49 | 50 | }); | 50 | 51 |
| 51 | | -/** Resolves with why the requested regeneration kept the title, or null. */ | 52 | | -export function waitForTitleRegenerationFailure( | | 52 | +/** Start before dispatching the request; see `watchTitleRegeneration`. */ | | 53 | +export function watchThreadTitleRegeneration( | 53 | 54 | ref: ScopedThreadRef, | 54 | 55 | requestId: string, | 55 | | -): Promise<string | null> { | 56 | | - return awaitTitleRegenerationFailure({ | | 56 | +): TitleRegenerationWatch { | | 57 | + return watchTitleRegeneration({ | 57 | 58 | registry: appAtomRegistry, | 58 | 59 | atom: environmentThreadShells.threadShellAtom(ref), | 59 | 60 | requestId, | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/Sidebar.tsx +19-23Lines changed: 19 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -151,7 +151,7 @@ import { | 151 | 151 | useProjects, | 152 | 152 | useServerConfigs, | 153 | 153 | useQuietThreadShells, | 154 | | - waitForTitleRegenerationFailure, | | 154 | + watchThreadTitleRegeneration, | 155 | 155 | } from "../state/entities"; | 156 | 156 | import { environmentPresentations } from "../state/presentation"; | 157 | 157 | import { primaryServerKeybindingsAtom } from "../state/server"; | @@ -2442,22 +2442,30 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve | 2442 | 2442 | return; | 2443 | 2443 | } | 2444 | 2444 | if (clicked.value === "regenerate-title") { | 2445 | | - const failures: Array<Promise<string | null>> = []; | 2446 | 2445 | for (const thread of regeneratableTitleThreads) { | 2447 | 2446 | const commandId = CommandId.make(randomUUID()); | | 2447 | + const watch = watchThreadTitleRegeneration( | | 2448 | + scopeThreadRef(thread.environmentId, thread.id), | | 2449 | + commandId, | | 2450 | + ); | 2448 | 2451 | const result = await updateThreadMetadata({ | 2449 | 2452 | environmentId: thread.environmentId, | 2450 | 2453 | input: { threadId: thread.id, regenerateTitle: true, commandId }, | 2451 | 2454 | }); | 2452 | 2455 | if (result._tag === "Success") { | 2453 | | - failures.push( | 2454 | | - waitForTitleRegenerationFailure( | 2455 | | - scopeThreadRef(thread.environmentId, thread.id), | 2456 | | - commandId, | 2457 | | - ), | 2458 | | - ); | | 2456 | + void watch.failure.then((failure) => { | | 2457 | + if (failure === null) return; | | 2458 | + toastManager.add( | | 2459 | + stackedThreadToast({ | | 2460 | + type: "error", | | 2461 | + title: "Failed to regenerate thread title", | | 2462 | + description: `${thread.title}: ${failure}`, | | 2463 | + }), | | 2464 | + ); | | 2465 | + }); | 2459 | 2466 | continue; | 2460 | 2467 | } | | 2468 | + watch.cancel(); | 2461 | 2469 | if (!isAtomCommandInterrupted(result)) { | 2462 | 2470 | const error = squashAtomCommandFailure(result); | 2463 | 2471 | toastManager.add( | @@ -2471,20 +2479,6 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve | 2471 | 2479 | return; | 2472 | 2480 | } | 2473 | 2481 | clearSelection(); | 2474 | | - void Promise.all(failures).then((outcomes) => { | 2475 | | - const messages = outcomes.filter((message) => message !== null); | 2476 | | - if (messages.length === 0) return; | 2477 | | - toastManager.add( | 2478 | | - stackedThreadToast({ | 2479 | | - type: "error", | 2480 | | - title: | 2481 | | - messages.length === 1 | 2482 | | - ? "Failed to regenerate a thread title" | 2483 | | - : `Failed to regenerate ${messages.length} thread titles`, | 2484 | | - description: messages[0], | 2485 | | - }), | 2486 | | - ); | 2487 | | - }); | 2488 | 2482 | return; | 2489 | 2483 | } | 2490 | 2484 | if (clicked.value === "settle") { | @@ -2798,11 +2792,13 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve | 2798 | 2792 | case "regenerate-title": { | 2799 | 2793 | if (isRegeneratingTitle) return; | 2800 | 2794 | const commandId = CommandId.make(randomUUID()); | | 2795 | + const watch = watchThreadTitleRegeneration(threadRef, commandId); | 2801 | 2796 | const result = await updateThreadMetadata({ | 2802 | 2797 | environmentId: threadRef.environmentId, | 2803 | 2798 | input: { threadId: threadRef.threadId, regenerateTitle: true, commandId }, | 2804 | 2799 | }); | 2805 | 2800 | if (result._tag === "Failure") { | | 2801 | + watch.cancel(); | 2806 | 2802 | if (!isAtomCommandInterrupted(result)) { | 2807 | 2803 | const error = squashAtomCommandFailure(result); | 2808 | 2804 | toastManager.add( | @@ -2815,7 +2811,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve | 2815 | 2811 | } | 2816 | 2812 | return; | 2817 | 2813 | } | 2818 | | - void waitForTitleRegenerationFailure(threadRef, commandId).then((failure) => { | | 2814 | + void watch.failure.then((failure) => { | 2819 | 2815 | if (failure === null) return; | 2820 | 2816 | toastManager.add( | 2821 | 2817 | stackedThreadToast({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/hooks/useThreadActionMenu.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,7 @@ import { | 34 | 34 | readEnvironmentSupportsAutoSettleOptOut, | 35 | 35 | readThreadShell, | 36 | 36 | useProjects, | 37 | | - waitForTitleRegenerationFailure, | | 37 | + watchThreadTitleRegeneration, | 38 | 38 | } from "../state/entities"; | 39 | 39 | import { usePrimaryEnvironmentId } from "../state/environments"; | 40 | 40 | import { readLocalApi } from "../localApi"; | @@ -252,17 +252,19 @@ export function useThreadActionMenu(input: { | 252 | 252 | case "regenerate-title": { | 253 | 253 | if (isRegeneratingTitle) return; | 254 | 254 | const commandId = CommandId.make(randomUUID()); | | 255 | + const watch = watchThreadTitleRegeneration(threadRef, commandId); | 255 | 256 | const result = await updateThreadMetadata({ | 256 | 257 | environmentId: threadRef.environmentId, | 257 | 258 | input: { threadId: threadRef.threadId, regenerateTitle: true, commandId }, | 258 | 259 | }); | 259 | 260 | if (result._tag === "Failure") { | | 261 | + watch.cancel(); | 260 | 262 | if (!isAtomCommandInterrupted(result)) { | 261 | 263 | failureToast("Failed to regenerate thread title", squashAtomCommandFailure(result)); | 262 | 264 | } | 263 | 265 | return; | 264 | 266 | } | 265 | | - void waitForTitleRegenerationFailure(threadRef, commandId).then((failure) => { | | 267 | + void watch.failure.then((failure) => { | 266 | 268 | if (failure !== null) { | 267 | 269 | failureToast("Failed to regenerate thread title", new Error(failure)); | 268 | 270 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/state/entities.ts +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,10 +8,11 @@ import type { | 8 | 8 | ThreadStructureVisibleTurnItem, | 9 | 9 | } from "@t3tools/client-runtime/state/shell"; | 10 | 10 | import { | 11 | | - awaitTitleRegenerationFailure, | 12 | 11 | EMPTY_THREAD_HISTORY_META, | 13 | 12 | type EnvironmentThreadStatus, | 14 | 13 | type ThreadHistoryMeta, | | 14 | + type TitleRegenerationWatch, | | 15 | + watchTitleRegeneration, | 15 | 16 | } from "@t3tools/client-runtime/state/threads"; | 16 | 17 | import { | 17 | 18 | arrayElementsEqual, | @@ -339,12 +340,12 @@ export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Pro | 339 | 340 | }); | 340 | 341 | } | 341 | 342 |
| 342 | | -/** Resolves with why the requested regeneration kept the title, or null. */ | 343 | | -export function waitForTitleRegenerationFailure( | | 343 | +/** Start before dispatching the request; see `watchTitleRegeneration`. */ | | 344 | +export function watchThreadTitleRegeneration( | 344 | 345 | ref: ScopedThreadRef, | 345 | 346 | requestId: string, | 346 | | -): Promise<string | null> { | 347 | | - return awaitTitleRegenerationFailure({ | | 347 | +): TitleRegenerationWatch { | | 348 | + return watchTitleRegeneration({ | 348 | 349 | registry: appAtomRegistry, | 349 | 350 | atom: environmentThreadShells.threadShellAtom(ref), | 350 | 351 | requestId, | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/threadShell.test.ts +13-4Lines changed: 13 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,7 @@ import { applyShellStreamEvent } from "./shellReducer.ts"; | 18 | 18 | import { isThreadWorking } from "./threadInbox.ts"; | 19 | 19 | import type { EnvironmentThreadShell } from "./models.ts"; | 20 | 20 | import { | 21 | | - awaitTitleRegenerationFailure, | | 21 | + watchTitleRegeneration, | 22 | 22 | childThreadInputsTitle, | 23 | 23 | createEnvironmentThreadShellAtoms, | 24 | 24 | } from "./threadShell.ts"; | @@ -640,7 +640,7 @@ describe("v2 thread shell lists", () => { | 640 | 640 | }); | 641 | 641 | }); | 642 | 642 |
| 643 | | -describe("awaitTitleRegenerationFailure", () => { | | 643 | +describe("watchTitleRegeneration", () => { | 644 | 644 | const shell = ( | 645 | 645 | fields: Pick<EnvironmentThreadShell, "titleRegeneration" | "titleRegenerationFailure">, | 646 | 646 | ) => fields as EnvironmentThreadShell; | @@ -662,10 +662,19 @@ describe("awaitTitleRegenerationFailure", () => { | 662 | 662 | ])("$name", async ({ after, expected }) => { | 663 | 663 | const registry = AtomRegistry.make(); | 664 | 664 | const atom = Atom.make<EnvironmentThreadShell | null>(shell({})).pipe(Atom.keepAlive); | 665 | | - const outcome = awaitTitleRegenerationFailure({ registry, atom, requestId: "a" }); | | 665 | + const watch = watchTitleRegeneration({ registry, atom, requestId: "a" }); | 666 | 666 | registry.set(atom, pending("a")); | 667 | 667 | registry.set(atom, after); | 668 | | - await expect(outcome).resolves.toBe(expected); | | 668 | + await expect(watch.failure).resolves.toBe(expected); | | 669 | + registry.dispose(); | | 670 | + }); | | 671 | + | | 672 | + it("settles when cancelled because the request was never accepted", async () => { | | 673 | + const registry = AtomRegistry.make(); | | 674 | + const atom = Atom.make<EnvironmentThreadShell | null>(shell({})).pipe(Atom.keepAlive); | | 675 | + const watch = watchTitleRegeneration({ registry, atom, requestId: "a" }); | | 676 | + watch.cancel(); | | 677 | + await expect(watch.failure).resolves.toBeNull(); | 669 | 678 | registry.dispose(); | 670 | 679 | }); | 671 | 680 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/threadShell.ts +15-7Lines changed: 15 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -200,26 +200,33 @@ function sameApartFromStreaming( | 200 | 200 | ); | 201 | 201 | } | 202 | 202 |
| | 203 | +export interface TitleRegenerationWatch { | | 204 | + /** The server's reason when the request kept the title; null when it landed or stopped mattering. */ | | 205 | + readonly failure: Promise<string | null>; | | 206 | + readonly cancel: () => void; | | 207 | +} | | 208 | + | 203 | 209 | /** | 204 | | - * Resolves with the server's reason once a title regeneration request ends without a new | 205 | | - * title, or null when it lands, is superseded, the thread goes away, or it times out. | | 210 | + * Watches one title regeneration request. Start it before dispatching: a request that | | 211 | + * finishes before the watch starts never shows as pending, so its success goes unseen. | 206 | 212 | */ | 207 | | -export function awaitTitleRegenerationFailure(input: { | | 213 | +export function watchTitleRegeneration(input: { | 208 | 214 | readonly registry: AtomRegistry.AtomRegistry; | 209 | 215 | readonly atom: Atom.Atom<EnvironmentThreadShell | null>; | 210 | 216 | readonly requestId: string; | 211 | 217 | readonly timeoutMs?: number; | 212 | | -}): Promise<string | null> { | 213 | | - return new Promise((resolve) => { | | 218 | +}): TitleRegenerationWatch { | | 219 | + let finish: (failure: string | null) => void = () => {}; | | 220 | + const failure = new Promise<string | null>((resolve) => { | 214 | 221 | let pending = false; | 215 | 222 | let settled = false; | 216 | 223 | let unsubscribe: (() => void) | null = null; | 217 | | - const finish = (failure: string | null) => { | | 224 | + finish = (value) => { | 218 | 225 | if (settled) return; | 219 | 226 | settled = true; | 220 | 227 | globalThis.clearTimeout(timeout); | 221 | 228 | unsubscribe?.(); | 222 | | - resolve(failure); | | 229 | + resolve(value); | 223 | 230 | }; | 224 | 231 | const inspect = (shell: EnvironmentThreadShell | null) => { | 225 | 232 | if (shell === null) return finish(null); | @@ -235,6 +242,7 @@ export function awaitTitleRegenerationFailure(input: { | 235 | 242 | if (settled) unsubscribe(); | 236 | 243 | else inspect(input.registry.get(input.atom)); | 237 | 244 | }); | | 245 | + return { failure, cancel: () => finish(null) }; | 238 | 246 | } | 239 | 247 |
| 240 | 248 | export function createEnvironmentThreadShellAtoms(input: { | |
|