Skip to content

Commit 2bb52b8

Browse files
committed
fix: watch title regeneration before dispatch and report bulk failures as they arrive
A regeneration that finished before the client started waiting never showed as pending, so its success waited out the 180-second timeout. Clients now start the watch before dispatching and cancel it if the dispatch fails. Bulk regeneration reported failures only after every request settled; each failure now shows as soon as it arrives, with the thread it belongs to.
1 parent 100249b commit 2bb52b8

8 files changed

Lines changed: 72 additions & 52 deletions

File tree

‎apps/mobile/src/features/home/useThreadListActions.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ vi.mock("../../state/thread-order", () => ({
121121
}));
122122
vi.mock("../../lib/uuid", () => ({ uuidv4: () => "command" }));
123123
vi.mock("../../state/threads", () => ({
124-
waitForTitleRegenerationFailure: async () => null,
124+
watchThreadTitleRegeneration: () => ({ failure: Promise.resolve(null), cancel: () => {} }),
125125
environmentThreadShells: {
126126
threadShellsAtom: "thread-shells",
127127
navigationThreadShellsAtom: "thread-shells",

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { readEnvironmentScope } from "../../state/session";
2121
import {
2222
environmentThreadShells,
2323
threadEnvironment,
24-
waitForTitleRegenerationFailure,
24+
watchThreadTitleRegeneration,
2525
} from "../../state/threads";
2626
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
2727
import { useAtomCommand } from "../../state/use-atom-command";
@@ -516,6 +516,10 @@ export function useThreadListActions(): {
516516
titleRegenerationInFlightThreadKeys.current.add(key);
517517
selectionHaptic();
518518
const commandId = CommandId.make(uuidv4());
519+
const watch = watchThreadTitleRegeneration(
520+
{ environmentId: thread.environmentId, threadId: thread.id },
521+
commandId,
522+
);
519523
const succeeded = await (async () => {
520524
const result = await updateThreadMetadata({
521525
environmentId: thread.environmentId,
@@ -535,12 +539,11 @@ export function useThreadListActions(): {
535539
})();
536540
titleRegenerationInFlightThreadKeys.current.delete(key);
537541
if (succeeded) {
538-
void waitForTitleRegenerationFailure(
539-
{ environmentId: thread.environmentId, threadId: thread.id },
540-
commandId,
541-
).then((failure) => {
542+
void watch.failure.then((failure) => {
542543
if (failure !== null) Dialog.alert("Could not regenerate title", failure);
543544
});
545+
} else {
546+
watch.cancel();
544547
}
545548
return succeeded;
546549
};

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createChildThreadApprovalsAtom } from "@t3tools/client-runtime/state/thread-requests";
22
import { useAtomValue } from "@effect/atom-react";
33
import {
4-
awaitTitleRegenerationFailure,
54
createEnvironmentThreadDetailAtoms,
65
createEnvironmentThreadShellAtoms,
76
createEnvironmentThreadStateAtoms,
@@ -10,6 +9,8 @@ import {
109
createOlderSettledThreadAtoms,
1110
createThreadEnvironmentAtoms,
1211
createThreadFamilyAtoms,
12+
type TitleRegenerationWatch,
13+
watchTitleRegeneration,
1314
} from "@t3tools/client-runtime/state/threads";
1415
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
1516
import { AsyncResult, Atom } from "effect/reactivity";
@@ -48,12 +49,12 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({
4849
hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
4950
});
5051

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(
5354
ref: ScopedThreadRef,
5455
requestId: string,
55-
): Promise<string | null> {
56-
return awaitTitleRegenerationFailure({
56+
): TitleRegenerationWatch {
57+
return watchTitleRegeneration({
5758
registry: appAtomRegistry,
5859
atom: environmentThreadShells.threadShellAtom(ref),
5960
requestId,

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import {
151151
useProjects,
152152
useServerConfigs,
153153
useQuietThreadShells,
154-
waitForTitleRegenerationFailure,
154+
watchThreadTitleRegeneration,
155155
} from "../state/entities";
156156
import { environmentPresentations } from "../state/presentation";
157157
import { primaryServerKeybindingsAtom } from "../state/server";
@@ -2442,22 +2442,30 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
24422442
return;
24432443
}
24442444
if (clicked.value === "regenerate-title") {
2445-
const failures: Array<Promise<string | null>> = [];
24462445
for (const thread of regeneratableTitleThreads) {
24472446
const commandId = CommandId.make(randomUUID());
2447+
const watch = watchThreadTitleRegeneration(
2448+
scopeThreadRef(thread.environmentId, thread.id),
2449+
commandId,
2450+
);
24482451
const result = await updateThreadMetadata({
24492452
environmentId: thread.environmentId,
24502453
input: { threadId: thread.id, regenerateTitle: true, commandId },
24512454
});
24522455
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+
});
24592466
continue;
24602467
}
2468+
watch.cancel();
24612469
if (!isAtomCommandInterrupted(result)) {
24622470
const error = squashAtomCommandFailure(result);
24632471
toastManager.add(
@@ -2471,20 +2479,6 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
24712479
return;
24722480
}
24732481
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-
});
24882482
return;
24892483
}
24902484
if (clicked.value === "settle") {
@@ -2798,11 +2792,13 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
27982792
case "regenerate-title": {
27992793
if (isRegeneratingTitle) return;
28002794
const commandId = CommandId.make(randomUUID());
2795+
const watch = watchThreadTitleRegeneration(threadRef, commandId);
28012796
const result = await updateThreadMetadata({
28022797
environmentId: threadRef.environmentId,
28032798
input: { threadId: threadRef.threadId, regenerateTitle: true, commandId },
28042799
});
28052800
if (result._tag === "Failure") {
2801+
watch.cancel();
28062802
if (!isAtomCommandInterrupted(result)) {
28072803
const error = squashAtomCommandFailure(result);
28082804
toastManager.add(
@@ -2815,7 +2811,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
28152811
}
28162812
return;
28172813
}
2818-
void waitForTitleRegenerationFailure(threadRef, commandId).then((failure) => {
2814+
void watch.failure.then((failure) => {
28192815
if (failure === null) return;
28202816
toastManager.add(
28212817
stackedThreadToast({

‎apps/web/src/hooks/useThreadActionMenu.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
readEnvironmentSupportsAutoSettleOptOut,
3535
readThreadShell,
3636
useProjects,
37-
waitForTitleRegenerationFailure,
37+
watchThreadTitleRegeneration,
3838
} from "../state/entities";
3939
import { usePrimaryEnvironmentId } from "../state/environments";
4040
import { readLocalApi } from "../localApi";
@@ -252,17 +252,19 @@ export function useThreadActionMenu(input: {
252252
case "regenerate-title": {
253253
if (isRegeneratingTitle) return;
254254
const commandId = CommandId.make(randomUUID());
255+
const watch = watchThreadTitleRegeneration(threadRef, commandId);
255256
const result = await updateThreadMetadata({
256257
environmentId: threadRef.environmentId,
257258
input: { threadId: threadRef.threadId, regenerateTitle: true, commandId },
258259
});
259260
if (result._tag === "Failure") {
261+
watch.cancel();
260262
if (!isAtomCommandInterrupted(result)) {
261263
failureToast("Failed to regenerate thread title", squashAtomCommandFailure(result));
262264
}
263265
return;
264266
}
265-
void waitForTitleRegenerationFailure(threadRef, commandId).then((failure) => {
267+
void watch.failure.then((failure) => {
266268
if (failure !== null) {
267269
failureToast("Failed to regenerate thread title", new Error(failure));
268270
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import type {
88
ThreadStructureVisibleTurnItem,
99
} from "@t3tools/client-runtime/state/shell";
1010
import {
11-
awaitTitleRegenerationFailure,
1211
EMPTY_THREAD_HISTORY_META,
1312
type EnvironmentThreadStatus,
1413
type ThreadHistoryMeta,
14+
type TitleRegenerationWatch,
15+
watchTitleRegeneration,
1516
} from "@t3tools/client-runtime/state/threads";
1617
import {
1718
arrayElementsEqual,
@@ -339,12 +340,12 @@ export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Pro
339340
});
340341
}
341342

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(
344345
ref: ScopedThreadRef,
345346
requestId: string,
346-
): Promise<string | null> {
347-
return awaitTitleRegenerationFailure({
347+
): TitleRegenerationWatch {
348+
return watchTitleRegeneration({
348349
registry: appAtomRegistry,
349350
atom: environmentThreadShells.threadShellAtom(ref),
350351
requestId,

‎packages/client-runtime/src/state/threadShell.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { applyShellStreamEvent } from "./shellReducer.ts";
1818
import { isThreadWorking } from "./threadInbox.ts";
1919
import type { EnvironmentThreadShell } from "./models.ts";
2020
import {
21-
awaitTitleRegenerationFailure,
21+
watchTitleRegeneration,
2222
childThreadInputsTitle,
2323
createEnvironmentThreadShellAtoms,
2424
} from "./threadShell.ts";
@@ -640,7 +640,7 @@ describe("v2 thread shell lists", () => {
640640
});
641641
});
642642

643-
describe("awaitTitleRegenerationFailure", () => {
643+
describe("watchTitleRegeneration", () => {
644644
const shell = (
645645
fields: Pick<EnvironmentThreadShell, "titleRegeneration" | "titleRegenerationFailure">,
646646
) => fields as EnvironmentThreadShell;
@@ -662,10 +662,19 @@ describe("awaitTitleRegenerationFailure", () => {
662662
])("$name", async ({ after, expected }) => {
663663
const registry = AtomRegistry.make();
664664
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" });
666666
registry.set(atom, pending("a"));
667667
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();
669678
registry.dispose();
670679
});
671680
});

‎packages/client-runtime/src/state/threadShell.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,33 @@ function sameApartFromStreaming(
200200
);
201201
}
202202

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+
203209
/**
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.
206212
*/
207-
export function awaitTitleRegenerationFailure(input: {
213+
export function watchTitleRegeneration(input: {
208214
readonly registry: AtomRegistry.AtomRegistry;
209215
readonly atom: Atom.Atom<EnvironmentThreadShell | null>;
210216
readonly requestId: string;
211217
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) => {
214221
let pending = false;
215222
let settled = false;
216223
let unsubscribe: (() => void) | null = null;
217-
const finish = (failure: string | null) => {
224+
finish = (value) => {
218225
if (settled) return;
219226
settled = true;
220227
globalThis.clearTimeout(timeout);
221228
unsubscribe?.();
222-
resolve(failure);
229+
resolve(value);
223230
};
224231
const inspect = (shell: EnvironmentThreadShell | null) => {
225232
if (shell === null) return finish(null);
@@ -235,6 +242,7 @@ export function awaitTitleRegenerationFailure(input: {
235242
if (settled) unsubscribe();
236243
else inspect(input.registry.get(input.atom));
237244
});
245+
return { failure, cancel: () => finish(null) };
238246
}
239247

240248
export function createEnvironmentThreadShellAtoms(input: {

0 commit comments

Comments
 (0)