Skip to content

Commit 1c9ea56

Browse files
flambohmaria-rcksYash-Singh1
authored
fix(clients): report title regeneration outcomes (#31418)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Yash Singh <[email protected]>
1 parent 1001bc7 commit 1c9ea56

19 files changed

Lines changed: 549 additions & 78 deletions

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ProviderInstanceId,
88
RunId,
99
ThreadId,
10+
type ThreadTitleRegenerationOutcome,
1011
} from "@t3tools/contracts";
1112
import * as Cause from "effect/Cause";
1213
import { AsyncResult } from "effect/reactivity";
@@ -18,6 +19,7 @@ const state = vi.hoisted(() => ({
1819
dropBusy: false,
1920
holdingRows: false,
2021
failedAction: null as string | null,
22+
titleOutcome: null as ThreadTitleRegenerationOutcome | null,
2123
archivedRefreshes: [] as string[],
2224
scopes: new Map<string, Set<string>>(),
2325
shells: [] as EnvironmentThreadShell[],
@@ -141,6 +143,7 @@ vi.mock("../../state/threads", () => ({
141143
"reorderPin",
142144
"reorderActive",
143145
"updateMetadata",
146+
"regenerateTitle",
144147
].map((action) => [
145148
action,
146149
async (request: {
@@ -155,7 +158,7 @@ vi.mock("../../state/threads", () => ({
155158
if (action === state.failedAction)
156159
return AsyncResult.failure(Cause.fail(new Error("Server rejected action")));
157160
state.afterRequest?.();
158-
return AsyncResult.success(undefined);
161+
return AsyncResult.success(action === "regenerateTitle" ? state.titleOutcome : undefined);
159162
},
160163
]),
161164
),
@@ -186,12 +189,13 @@ const mutationCases = [
186189
["unsnoozeThread", "unsnooze"],
187190
["pinThread", "pin"],
188191
["unpinThread", "unpin"],
189-
["regenerateThreadTitle", "updateMetadata"],
192+
["regenerateThreadTitle", "regenerateTitle"],
190193
] as const;
191194

192195
beforeEach(() => {
193196
state.connectedEnvironmentIds = [primaryEnvironmentId, otherEnvironmentId];
194197
state.failedAction = null;
198+
state.titleOutcome = null;
195199
state.archivedRefreshes = [];
196200
state.pendingOrder = null;
197201
state.dropBusy = false;
@@ -214,6 +218,16 @@ afterEach(() => {
214218
vi.unstubAllEnvs();
215219
});
216220

221+
it("reports a failed title generation as unsuccessful and allows another request", async () => {
222+
const actions = useThreadListActions();
223+
const thread = makeThread();
224+
state.titleOutcome = { status: "failed", message: "Model unavailable" };
225+
expect(await actions.regenerateThreadTitle(thread)).toBe(false);
226+
227+
state.titleOutcome = { status: "renamed" };
228+
expect(await actions.regenerateThreadTitle(thread)).toBe(true);
229+
});
230+
217231
describe("thread list operation permissions", () => {
218232
it.each(mutationCases)(
219233
"%s rejects a retained callback after its environment loses permission",

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ export function useThreadListActions(): {
240240
const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
241241
reportFailure: false,
242242
});
243+
const regenerateTitle = useAtomCommand(threadEnvironment.regenerateTitle, {
244+
reportFailure: false,
245+
});
243246
const snoozeInFlightThreadKeys = useRef(new Set<string>());
244247
const titleRegenerationInFlightThreadKeys = useRef(new Set<string>());
245248

@@ -529,9 +532,9 @@ export function useThreadListActions(): {
529532
titleRegenerationInFlightThreadKeys.current.add(key);
530533
selectionHaptic();
531534
const succeeded = await (async () => {
532-
const result = await updateThreadMetadata({
535+
const result = await regenerateTitle({
533536
environmentId: thread.environmentId,
534-
input: { threadId: thread.id, regenerateTitle: true },
537+
input: { threadId: thread.id },
535538
});
536539
if (result._tag === "Failure") {
537540
const error = Cause.squash(result.cause);
@@ -543,6 +546,13 @@ export function useThreadListActions(): {
543546
);
544547
return false;
545548
}
549+
if (result.value?.status === "failed") {
550+
Dialog.alert(
551+
"Could not regenerate title",
552+
result.value.message ?? "The thread title could not be regenerated.",
553+
);
554+
return false;
555+
}
546556
return true;
547557
})();
548558
titleRegenerationInFlightThreadKeys.current.delete(key);

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,13 @@ function threadDetail(
898898
requestId: projection.thread.titleRegeneration.requestId,
899899
startedAt: DateTime.formatIso(projection.thread.titleRegeneration.startedAt),
900900
},
901+
lastTitleRegeneration:
902+
projection.thread.lastTitleRegeneration == null
903+
? null
904+
: {
905+
...projection.thread.lastTitleRegeneration,
906+
completedAt: DateTime.formatIso(projection.thread.lastTitleRegeneration.completedAt),
907+
},
901908
branch: projection.thread.branch,
902909
worktreePath: projection.thread.worktreePath,
903910
parentThreadId: projection.thread.lineage.parentThreadId,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,6 +4768,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47684768
? {
47694769
...thread,
47704770
...(command.title === undefined ? {} : { title: command.title }),
4771+
...(command.outcome === undefined
4772+
? {}
4773+
: {
4774+
lastTitleRegeneration: {
4775+
...command.outcome,
4776+
requestId: command.requestId,
4777+
completedAt: now,
4778+
},
4779+
}),
47714780
titleRegeneration: null,
47724781
updatedAt: now,
47734782
}

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,7 @@ export function threadShellFromProjection(
17951795
lastVisitedAt: projection.thread.lastVisitedAt,
17961796
opens: projection.thread.opens ?? null,
17971797
titleRegeneration: projection.thread.titleRegeneration ?? null,
1798+
lastTitleRegeneration: projection.thread.lastTitleRegeneration ?? null,
17981799
limitRecovery: projection.thread.limitRecovery ?? null,
17991800
deletedAt: projection.thread.deletedAt,
18001801
};
@@ -2091,6 +2092,7 @@ function shellFromState(input: {
20912092
lastVisitedAt: input.state.thread.lastVisitedAt,
20922093
opens: input.state.thread.opens ?? null,
20932094
titleRegeneration: input.state.thread.titleRegeneration ?? null,
2095+
lastTitleRegeneration: input.state.thread.lastTitleRegeneration ?? null,
20942096
limitRecovery: input.state.thread.limitRecovery ?? null,
20952097
deletedAt: input.state.thread.deletedAt,
20962098
};

‎apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ProviderInstanceId,
1212
ProviderThreadId,
1313
RunId,
14+
TextGenerationError,
1415
ThreadId,
1516
} from "@t3tools/contracts";
1617
import * as DateTime from "effect/DateTime";
@@ -432,6 +433,10 @@ describe("ThreadTitleRegenerationService", () => {
432433
const projection = yield* threads.getThreadProjection(threadId);
433434
assert.equal(projection.thread.title, "Fresh title");
434435
assert.isNotOk(projection.thread.titleRegeneration);
436+
assert.deepInclude(projection.thread.lastTitleRegeneration, {
437+
requestId,
438+
status: "renamed",
439+
});
435440
}
436441
}).pipe(Effect.provide(harness.layer));
437442
}).pipe(Effect.provide(NodeServices.layer)),
@@ -518,7 +523,14 @@ describe("ThreadTitleRegenerationService", () => {
518523
it.effect("clears the marker and keeps the title when generation fails", () =>
519524
Effect.gen(function* () {
520525
const harness = makeHarness({
521-
generateTitle: () => Effect.die(new Error("model unavailable")),
526+
generateTitle: () =>
527+
Effect.fail(
528+
new TextGenerationError({
529+
operation: "generateThreadTitle",
530+
detail:
531+
"Codex CLI command failed: OpenAI Codex\nuser\nConversation text\nmodel unavailable\n",
532+
}),
533+
),
522534
});
523535
yield* Effect.gen(function* () {
524536
const threads = yield* ThreadManagement.ThreadManagementService;
@@ -540,6 +552,44 @@ describe("ThreadTitleRegenerationService", () => {
540552
const projection = yield* threads.getThreadProjection(threadId);
541553
assert.equal(projection.thread.title, "Seed title");
542554
assert.isNotOk(projection.thread.titleRegeneration);
555+
assert.deepInclude(projection.thread.lastTitleRegeneration, {
556+
requestId,
557+
status: "failed",
558+
message: "model unavailable",
559+
});
560+
}).pipe(Effect.provide(harness.layer));
561+
}),
562+
);
563+
564+
it.effect("records an unchanged outcome when the model keeps the current title", () =>
565+
Effect.gen(function* () {
566+
const harness = makeHarness({
567+
generateTitle: () => Effect.succeed({ title: "Seed title" }),
568+
});
569+
yield* Effect.gen(function* () {
570+
const threads = yield* ThreadManagement.ThreadManagementService;
571+
const titleRegeneration = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService;
572+
const threadId = yield* createThread({
573+
command: "command:title:unchanged:create",
574+
thread: "thread:title:unchanged",
575+
});
576+
yield* dispatchUserMessage({
577+
command: "command:title:unchanged:message",
578+
threadId,
579+
text: "Some conversation",
580+
});
581+
const requestId = yield* armRegeneration({
582+
command: "command:title:unchanged:1",
583+
threadId,
584+
});
585+
586+
yield* titleRegeneration.execute({ threadId, requestId, kind: { type: "regenerate" } });
587+
588+
const projection = yield* threads.getThreadProjection(threadId);
589+
assert.deepInclude(projection.thread.lastTitleRegeneration, {
590+
requestId,
591+
status: "unchanged",
592+
});
543593
}).pipe(Effect.provide(harness.layer));
544594
}),
545595
);

‎apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts‎

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type OrchestrationV2ConversationMessage,
99
type RunId,
1010
type ServerSettingsError,
11+
TextGenerationError,
1112
type ThreadId,
1213
} from "@t3tools/contracts";
1314
import * as Cause from "effect/Cause";
@@ -17,6 +18,7 @@ import * as FileSystem from "effect/FileSystem";
1718
import * as Layer from "effect/Layer";
1819
import * as Option from "effect/Option";
1920
import * as Schedule from "effect/Schedule";
21+
import * as Schema from "effect/Schema";
2022

2123
import * as ServerSettings from "../serverSettings.ts";
2224
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
@@ -46,6 +48,29 @@ export class ThreadTitleRegenerationService extends Context.Service<
4648
}
4749
>()("t3/orchestration-v2/ThreadTitleRegenerationService") {}
4850

51+
type Outcome =
52+
| { readonly status: "renamed"; readonly title: string }
53+
| { readonly status: "unchanged" }
54+
| { readonly status: "failed"; readonly message: string };
55+
56+
const MAX_FAILURE_MESSAGE_LENGTH = 300;
57+
const isTextGenerationError = Schema.is(TextGenerationError);
58+
59+
function failureMessage(cause: Cause.Cause<unknown>): string {
60+
const error = Cause.squash(cause);
61+
const lines = isTextGenerationError(error)
62+
? error.detail
63+
.split("\n")
64+
.map((line) => line.trim())
65+
.filter((line) => line.length > 0)
66+
: [];
67+
const detail = lines.at(-1);
68+
if (detail === undefined) return "Title generation failed.";
69+
return detail.length > MAX_FAILURE_MESSAGE_LENGTH
70+
? `${detail.slice(0, MAX_FAILURE_MESSAGE_LENGTH - 1)}…`
71+
: detail;
72+
}
73+
4974
const make = Effect.gen(function* () {
5075
const threads = yield* ThreadManagementService.ThreadManagementService;
5176
const projects = yield* ProjectStore.ProjectStoreV2;
@@ -56,15 +81,24 @@ const make = Effect.gen(function* () {
5681
const complete = (input: {
5782
readonly threadId: ThreadId;
5883
readonly requestId: CommandId;
59-
readonly title?: string;
84+
readonly kind: { readonly type: "initial" | "regenerate" };
85+
readonly outcome: Outcome;
6086
}) =>
6187
threads
6288
.dispatch({
6389
type: "thread.title.regeneration.complete",
6490
commandId: CommandId.make(`${input.requestId}:title-complete`),
6591
threadId: input.threadId,
6692
requestId: input.requestId,
67-
...(input.title === undefined ? {} : { title: input.title }),
93+
...(input.outcome.status === "renamed" ? { title: input.outcome.title } : {}),
94+
...(input.kind.type === "regenerate"
95+
? {
96+
outcome: {
97+
status: input.outcome.status,
98+
...(input.outcome.status === "failed" ? { message: input.outcome.message } : {}),
99+
},
100+
}
101+
: {}),
68102
})
69103
.pipe(Effect.asVoid);
70104

@@ -110,9 +144,7 @@ const make = Effect.gen(function* () {
110144
const execute: ThreadTitleRegenerationService["Service"]["execute"] = Effect.fn(
111145
"ThreadTitleRegenerationService.execute",
112146
)(function* (input) {
113-
const outcome:
114-
| { readonly type: "stale" }
115-
| { readonly type: "complete"; readonly title?: string } = yield* Effect.gen(function* () {
147+
const outcome: Outcome | { readonly status: "stale" } = yield* Effect.gen(function* () {
116148
const projection = yield* threads.getThreadRecords(
117149
input.threadId,
118150
["messages"],
@@ -121,12 +153,12 @@ const make = Effect.gen(function* () {
121153
: { messageRoles: ["user", "assistant"] },
122154
);
123155
if (projection.thread.titleRegeneration?.requestId !== input.requestId) {
124-
return { type: "stale" as const };
156+
return { status: "stale" as const };
125157
}
126158

127159
const project = yield* projects.get(projection.thread.projectId);
128160
if (Option.isNone(project)) {
129-
return { type: "complete" as const };
161+
return { status: "failed" as const, message: "This thread's project no longer exists." };
130162
}
131163

132164
let context: {
@@ -151,7 +183,10 @@ const make = Effect.gen(function* () {
151183
);
152184
}
153185
if (context.message.length === 0 && context.attachments.length === 0) {
154-
return { type: "complete" as const };
186+
return {
187+
status: "failed" as const,
188+
message: "This thread has no messages to title yet.",
189+
};
155190
}
156191

157192
// A deleted source should not cost the fork its title.
@@ -181,8 +216,8 @@ const make = Effect.gen(function* () {
181216
const generatedTitle = result.title.trim();
182217
return generatedTitle === "New thread" ||
183218
(input.kind.type === "regenerate" && generatedTitle === projection.thread.title.trim())
184-
? { type: "complete" as const }
185-
: { type: "complete" as const, title: result.title };
219+
? { status: "unchanged" as const }
220+
: { status: "renamed" as const, title: result.title };
186221
}).pipe(
187222
Effect.retry({
188223
times: input.kind.type === "initial" ? 2 : 0,
@@ -195,17 +230,14 @@ const make = Effect.gen(function* () {
195230
threadId: input.threadId,
196231
requestId: input.requestId,
197232
cause,
198-
}).pipe(Effect.as({ type: "complete" as const })),
233+
}).pipe(Effect.as({ status: "failed" as const, message: failureMessage(cause) })),
199234
),
200235
);
201236

202-
if (outcome.type === "stale") {
237+
if (outcome.status === "stale") {
203238
return;
204239
}
205-
yield* complete({
206-
...input,
207-
...(outcome.title === undefined ? {} : { title: outcome.title }),
208-
});
240+
yield* complete({ ...input, outcome });
209241
});
210242

211243
return ThreadTitleRegenerationService.of({ execute });

0 commit comments

Comments
 (0)