diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 819aa90c03b98..c903a721a6a83 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -87,6 +87,7 @@ import { contextBoundaryOrdinal, isScheduledFreshRun } from "./contextBoundary.t import { isPlaceholderThreadTitle } from "./placeholderThreadTitle.ts"; import { combinePullRequestWatchWakes, type PullRequestWatchWake } from "./pullRequestWatch.ts"; import { recordThreadOpen } from "@t3tools/shared/paletteSearch"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { backgroundWorkHoldsCompletion, derivePendingBackgroundWork, @@ -5158,7 +5159,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio return thread.titleRegeneration?.requestId === command.requestId ? { ...thread, - ...(command.title === undefined ? {} : { title: command.title }), + ...(command.title === undefined + ? {} + : { title: command.title, automaticTitle: command.title }), ...(command.outcome === undefined ? {} : { @@ -6795,6 +6798,46 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ]); }); + /* Runs inside dispatchMessage, which owns the span. */ + const automaticTitleRegenerationDue = Effect.fnUntraced(function* ( + command: Extract, + thread: OrchestrationV2AppThread, + userMessages: ReadonlyArray, + ) { + // Scheduled tasks send as their owner; like agent traffic, they should not spend title requests. + const fromPerson = ( + message: Pick< + OrchestrationV2ConversationMessage, + "createdBy" | "scheduledTaskId" | "text" | "attachments" + >, + ) => + message.createdBy === "user" && + message.scheduledTaskId === undefined && + !isNativeMaintenanceCommand(message); + // A title that differs from the last generated one was chosen by someone and stays. + if ( + !fromPerson(command) || + thread.titleRegeneration != null || + thread.automaticTitle !== thread.title + ) { + return false; + } + const settings = yield* serverSettings.getSettings.pipe(Effect.orDie); + const interval = resolveProjectSettings(settings, thread.projectId).settings + .threadTitleRegenerationInterval; + if (interval === null) return false; + // A compacted command projection omits older messages. Read only user messages, and keep + // any pending messages already added in this transaction without counting them twice. + const stored = yield* projectionStore + .getThreadRecords(command.threadId, ["messages"], { messageRoles: ["user"] }) + .pipe(mapDispatchError(command)); + const all = new Map( + [...stored.messages, ...userMessages].map((message) => [message.id, message]), + ); + if (all.has(command.messageId)) return false; + return ([...all.values()].filter(fromPerson).length + 1) % interval === 0; + }); + // Providers own native subagent conversations; messages cannot target them, and adapters ask questions on the parent. const refuseNativeSubagentMessage = Effect.fnUntraced(function* ( command: Pick & { readonly threadId: ThreadId }, @@ -7118,14 +7161,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio (yield* projectionStore.getMessageCount(command.threadId).pipe(mapDispatchError(command))) === 0 && !hasTitleSetBeforeFirstMessage(projection.thread, command.titleSeed); - if ( + const startsTitle = !isNativeMaintenanceCommand(command) && - (seedsUntitledThread || (command.createdBy === "user" && (yield* onlyMaintenanceHistory))) - ) { + (seedsUntitledThread || (command.createdBy === "user" && (yield* onlyMaintenanceHistory))); + const titleGeneration = startsTitle + ? ({ type: "initial", messageId: command.messageId } as const) + : (yield* automaticTitleRegenerationDue(command, projection.thread, userMessages)) + ? ({ type: "regenerate" } as const) + : undefined; + if (titleGeneration !== undefined) { const now = yield* DateTime.now; const thread: OrchestrationV2AppThread = { ...projection.thread, - title: command.titleSeed ?? projection.thread.title, + // The seed is T3's own first title, so a later regeneration may replace it. + ...(startsTitle && command.titleSeed !== undefined + ? { title: command.titleSeed, automaticTitle: command.titleSeed } + : {}), titleRegeneration: { requestId: command.commandId, startedAt: now }, updatedAt: now, }; @@ -7141,10 +7192,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio }); yield* Ref.update(effects, (existing) => [ ...existing, - pendingThreadTitleGenerationEffect(command.commandId, command.threadId, { - type: "initial", - messageId: command.messageId, - }), + pendingThreadTitleGenerationEffect(command.commandId, command.threadId, titleGeneration), ]); projection = applyToProjection(projection, event); } diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts index cd4e4df9ff1a3..92f872ca1e7d3 100644 --- a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts @@ -11,8 +11,10 @@ import { ProviderInstanceId, ProviderThreadId, RunId, + ScheduledTaskId, TextGenerationError, ThreadId, + TurnItemId, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; @@ -54,6 +56,7 @@ const adapter = { function makeHarness( options: { readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"]; + readonly regenerationInterval?: number; } = {}, ) { const layerDatabase = SqlitePersistence.layerMemory; @@ -61,7 +64,13 @@ function makeHarness( const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( { name: "thread-title-regeneration" }, layerRegistry, - { databaseLayer: layerDatabase, runEffectWorker: false }, + { + databaseLayer: layerDatabase, + runEffectWorker: false, + ...(options.regenerationInterval === undefined + ? {} + : { threadTitleRegenerationInterval: options.regenerationInterval }), + }, ); const layerServerSettings = ServerSettings.layerTest({}); const layerThreadManagement = ThreadManagement.layer.pipe( @@ -124,6 +133,7 @@ function createThread(input: { readonly command: string; readonly thread: string; readonly worktreePath?: string; + readonly title?: string; }) { return Effect.gen(function* () { const threads = yield* ThreadManagement.ThreadManagementService; @@ -133,7 +143,7 @@ function createThread(input: { commandId: CommandId.make(input.command), threadId, projectId, - title: "Seed title", + title: input.title ?? "Seed title", modelSelection, runtimeMode: "full-access", interactionMode: "default", @@ -151,6 +161,10 @@ function dispatchUserMessage(input: { readonly threadId: ThreadId; readonly text: string; readonly attachments?: ReadonlyArray; + readonly titleSeed?: string; + readonly messageId?: MessageId; + readonly createdBy?: "user" | "agent"; + readonly scheduledTaskId?: ScheduledTaskId; }) { return Effect.gen(function* () { const threads = yield* ThreadManagement.ThreadManagementService; @@ -158,12 +172,14 @@ function dispatchUserMessage(input: { type: "message.dispatch", commandId: CommandId.make(input.command), threadId: input.threadId, - messageId: MessageId.make(`${input.command}:message`), + messageId: input.messageId ?? MessageId.make(`${input.command}:message`), text: input.text, attachments: input.attachments ?? [], + ...(input.titleSeed === undefined ? {} : { titleSeed: input.titleSeed }), + ...(input.scheduledTaskId === undefined ? {} : { scheduledTaskId: input.scheduledTaskId }), modelSelection, dispatchMode: { type: "defer_start" }, - createdBy: "user", + createdBy: input.createdBy ?? "user", creationSource: "web", }); }); @@ -829,6 +845,8 @@ describe("ThreadTitleRegenerationService", () => { const projection = yield* threads.getThreadProjection(threadId); assert.equal(projection.thread.title, "Seed title"); assert.isNotOk(projection.thread.titleRegeneration); + // A failure must not hand a title someone chose to automatic regeneration. + assert.isUndefined(projection.thread.automaticTitle); assert.deepInclude(projection.thread.lastTitleRegeneration, { requestId, status: "failed", @@ -838,6 +856,183 @@ describe("ThreadTitleRegenerationService", () => { }), ); + it.effect.each([false, true])( + "regenerates on every Nth message until renamed (compacted: %s)", + (compacted) => + Effect.gen(function* () { + let generated = 0; + const harness = makeHarness({ + regenerationInterval: 2, + // Request 1 declines to title the seed; request 4 is the manual one and keeps what it finds. + generateTitle: () => + Effect.sync(() => { + generated += 1; + if (generated === 1) return { title: "New thread" }; + return { title: generated === 4 ? "Manual title" : `Generated ${generated}` }; + }), + }); + yield* Effect.gen(function* () { + const threads = yield* ThreadManagement.ThreadManagementService; + const outbox = yield* EffectOutbox.EffectOutboxV2; + const service = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService; + const threadId = yield* createThread({ + command: "command:title:auto:create", + thread: "thread:title:auto", + title: "New thread", + }); + const title = Effect.map(threads.getThreadProjection(threadId), (it) => it.thread.title); + // Sends a message, runs the title job it queued, and reports which kind that was. + const send = Effect.fn(function* ( + step: string, + message: Pick< + Parameters[0], + "titleSeed" | "createdBy" | "scheduledTaskId" | "messageId" + > = {}, + ) { + const command = `command:title:auto:${step}`; + yield* dispatchUserMessage({ command, threadId, text: `Message ${step}`, ...message }); + const requestId = CommandId.make(command); + const queued = (yield* outbox.listByCommandId(requestId)).flatMap((effect) => + effect.request.type === "thread-title.generate" ? [effect.request.kind] : [], + ); + for (const kind of queued) yield* service.execute({ threadId, requestId, kind }); + return queued.map((kind) => kind.type); + }); + + assert.deepEqual(yield* send("1", { titleSeed: "First ask" }), ["initial"]); + // The seed stays T3's title, so the next due message retries it. + assert.equal(yield* title, "First ask"); + assert.deepEqual(yield* send("2"), ["regenerate"]); + assert.equal(yield* title, "Generated 2"); + assert.deepEqual(yield* send("3"), []); + assert.deepEqual( + yield* send("3-redispatch", { + messageId: MessageId.make("command:title:auto:3:message"), + }), + [], + ); + if (compacted) { + const compactCommand = "command:title:auto:compact"; + yield* dispatchUserMessage({ command: compactCommand, threadId, text: "/compact" }); + const projection = yield* threads.getThreadProjection(threadId); + const compactRun = projection.runs.find( + (run) => run.userMessageId === MessageId.make(`${compactCommand}:message`), + )!; + const before = yield* DateTime.now; + const after = DateTime.add(before, { seconds: 1 }); + yield* (yield* EventSink.EventSinkV2).write({ + events: [ + ...projection.runs.map((run) => ({ + id: EventId.make(`${run.id}:completed`), + type: "run.updated" as const, + threadId, + runId: run.id, + occurredAt: after, + payload: { + ...run, + status: "completed" as const, + requestedAt: run.id === compactRun.id ? after : before, + startedAt: run.id === compactRun.id ? after : before, + completedAt: run.id === compactRun.id ? after : before, + }, + })), + { + id: EventId.make("event:title:auto:compacted"), + type: "turn-item.updated", + threadId, + runId: compactRun.id, + occurredAt: after, + payload: { + id: TurnItemId.make("item:title:auto:compacted"), + threadId, + runId: compactRun.id, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: projection.turnItems.length + 1, + status: "completed", + title: null, + startedAt: after, + completedAt: after, + updatedAt: after, + type: "compaction", + driver: adapter.driver, + }, + }, + ], + }); + } + // Agent and scheduled traffic neither trigger a regeneration nor count toward the next. + assert.deepEqual(yield* send("agent", { createdBy: "agent" }), []); + const scheduledTaskId = ScheduledTaskId.make("task:title:auto"); + assert.deepEqual(yield* send("scheduled", { scheduledTaskId }), []); + assert.deepEqual(yield* send("4"), ["regenerate"]); + assert.equal(yield* title, "Generated 3"); + + yield* threads.dispatch({ + type: "thread.metadata.update", + commandId: CommandId.make("command:title:auto:rename"), + threadId, + title: "Manual title", + }); + assert.deepEqual(yield* send("5"), []); + assert.deepEqual(yield* send("6"), []); + assert.equal(yield* title, "Manual title"); + + // Asking for a generated title hands the thread back, even when the title stands. + const requestId = yield* armRegeneration({ + command: "command:title:auto:regen", + threadId, + }); + yield* service.execute({ threadId, requestId, kind: { type: "regenerate" } }); + assert.equal(yield* title, "Manual title"); + assert.deepEqual(yield* send("7"), []); + assert.deepEqual(yield* send("8"), ["regenerate"]); + assert.equal(yield* title, "Generated 5"); + }).pipe(Effect.provide(harness.layer)); + }), + ); + + it.effect("leaves titles alone when no regeneration interval is set", () => + Effect.gen(function* () { + const harness = makeHarness(); + yield* Effect.gen(function* () { + const outbox = yield* EffectOutbox.EffectOutboxV2; + const service = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService; + const threadId = yield* createThread({ + command: "command:title:off:create", + thread: "thread:title:off", + title: "New thread", + }); + const first = "command:title:off:1"; + yield* dispatchUserMessage({ + command: first, + threadId, + text: "First ask", + titleSeed: "First ask", + }); + // Finish the first title so only the missing interval can hold regeneration back. + yield* service.execute({ + threadId, + requestId: CommandId.make(first), + kind: { type: "initial", messageId: MessageId.make(`${first}:message`) }, + }); + for (const step of [2, 3, 4, 5]) { + const command = `command:title:off:${step}`; + yield* dispatchUserMessage({ command, threadId, text: `Message ${step}` }); + assert.deepEqual( + (yield* outbox.listByCommandId(CommandId.make(command))).filter( + (effect) => effect.request.type === "thread-title.generate", + ), + [], + ); + } + }).pipe(Effect.provide(harness.layer)); + }), + ); + it.effect("records an unchanged outcome when the model keeps the current title", () => Effect.gen(function* () { const harness = makeHarness({ diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts index 725a15aa136e0..90eaf030eebb1 100644 --- a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts @@ -52,7 +52,7 @@ export class ThreadTitleRegenerationService extends Context.Service< type Outcome = | { readonly status: "renamed"; readonly title: string } - | { readonly status: "unchanged" } + | { readonly status: "unchanged"; readonly title?: string } | { readonly status: "failed"; readonly message: string }; const MAX_FAILURE_MESSAGE_LENGTH = 300; @@ -93,7 +93,9 @@ const make = Effect.gen(function* () { commandId: CommandId.make(`${input.requestId}:title-complete`), threadId: input.threadId, requestId: input.requestId, - ...(input.outcome.status === "renamed" ? { title: input.outcome.title } : {}), + ...(input.outcome.status !== "failed" && input.outcome.title !== undefined + ? { title: input.outcome.title } + : {}), ...(input.kind.type === "regenerate" ? { outcome: { @@ -232,7 +234,10 @@ const make = Effect.gen(function* () { const generatedTitle = result.title.trim(); return generatedTitle === "New thread" || (input.kind.type === "regenerate" && generatedTitle === projection.thread.title.trim()) - ? { status: "unchanged" as const } + ? { + status: "unchanged" as const, + ...(generatedTitle === "New thread" ? {} : { title: result.title }), + } : { status: "renamed" as const, title: result.title }; }).pipe( Effect.retry({ diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index 0ba9137e45dbc..70044aef6e1b0 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -283,6 +283,7 @@ export function layerWithRegistry( readonly runContinuationWorker?: boolean; readonly recoverOnStartup?: boolean; readonly continueThreadsAfterServerUpdate?: boolean; + readonly threadTitleRegenerationInterval?: number; /** Lets a test hold a projection read open to order concurrent dispatches. */ readonly wrapProjectionStore?: ( store: ProjectionStore.ProjectionStoreV2Shape, @@ -324,6 +325,9 @@ export function layerWithRegistry( ...(options.continueThreadsAfterServerUpdate === undefined ? {} : { continueThreadsAfterServerUpdate: options.continueThreadsAfterServerUpdate }), + ...(options.threadTitleRegenerationInterval === undefined + ? {} + : { threadTitleRegenerationInterval: options.threadTitleRegenerationInterval }), }, policy.summary, ), diff --git a/apps/web/src/components/settings/SettingInheritance.tsx b/apps/web/src/components/settings/SettingInheritance.tsx index 4cae56f264627..dccc95502418c 100644 --- a/apps/web/src/components/settings/SettingInheritance.tsx +++ b/apps/web/src/components/settings/SettingInheritance.tsx @@ -56,15 +56,17 @@ function formatValue( ? resolve(msg("Last selected")) : key === "sidebarAutoSettleAfterDays" ? resolve(msg("Never")) - : key === "defaultModelSelection" - ? resolve(msg("Automatic")) - : key === "sourceControlWriterModelSelection" - ? resolve(msg("Text generation model")) - : key === "reviewGuideModelSelection" - ? resolve(msg("Source control writer model")) - : key === "defaultThreadEnvMode" || key === "worktreeSubmodules" - ? resolve(msg("Inherit")) - : resolve(msg("Not set")); + : key === "threadTitleRegenerationInterval" + ? resolve(msg("Off", { $context: "setting value" })) + : key === "defaultModelSelection" + ? resolve(msg("Automatic")) + : key === "sourceControlWriterModelSelection" + ? resolve(msg("Text generation model")) + : key === "reviewGuideModelSelection" + ? resolve(msg("Source control writer model")) + : key === "defaultThreadEnvMode" || key === "worktreeSubmodules" + ? resolve(msg("Inherit")) + : resolve(msg("Not set")); } if (typeof value === "boolean") { return value @@ -74,7 +76,13 @@ function formatValue( if (typeof value === "number") { return key === "sidebarAutoSettleAfterDays" ? resolve(msg("{count, plural, one {{count} day} other {{count} days}}", { count: value })) - : String(value); + : key === "threadTitleRegenerationInterval" + ? resolve( + msg("{count, plural, one {Every message} other {Every {count} messages}}", { + count: value, + }), + ) + : String(value); } if (typeof value === "string") { if (key === "defaultThreadEnvMode" && (value === "local" || value === "worktree")) { diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index e0a12417848fa..b524e6e1c9615 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -20,7 +20,9 @@ import { DEFAULT_UNIFIED_SETTINGS, type DiffLayout, MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, + MAX_THREAD_TITLE_REGENERATION_INTERVAL, MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, + MIN_THREAD_TITLE_REGENERATION_INTERVAL, type ResponseStreamingMode, type QuitConfirmationMode, SidebarProjectSortOrder, @@ -632,6 +634,10 @@ export function useSettingsRestore(onRestored?: () => void) { ...(settings.threadTitleInstructions !== DEFAULT_UNIFIED_SETTINGS.threadTitleInstructions ? [gt("Thread title instructions")] : []), + ...(settings.threadTitleRegenerationInterval !== + DEFAULT_UNIFIED_SETTINGS.threadTitleRegenerationInterval + ? [gt("Automatic title regeneration")] + : []), ...getChangedBrowserSettingLabels(settings, messages), ...(settings.enableAgentBrowserAccess !== DEFAULT_UNIFIED_SETTINGS.enableAgentBrowserAccess ? [gt("Agent browser access")] @@ -744,6 +750,7 @@ export function useSettingsRestore(onRestored?: () => void) { confirmQuit: DEFAULT_UNIFIED_SETTINGS.confirmQuit, textGenerationModelSelection: DEFAULT_UNIFIED_SETTINGS.textGenerationModelSelection, threadTitleInstructions: DEFAULT_UNIFIED_SETTINGS.threadTitleInstructions, + threadTitleRegenerationInterval: DEFAULT_UNIFIED_SETTINGS.threadTitleRegenerationInterval, fontFamilySans: DEFAULT_UNIFIED_SETTINGS.fontFamilySans, fontFamilyComposer: DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer, fontFamilyCode: DEFAULT_UNIFIED_SETTINGS.fontFamilyCode, @@ -775,6 +782,7 @@ export function useSettingsRestore(onRestored?: () => void) { } const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_UNIFIED_SETTINGS.sidebarAutoSettleAfterDays ?? 7; +const TITLE_REGENERATION_DEFAULT_INTERVAL = 5; function AutoSettleDaysInput({ value, @@ -823,6 +831,44 @@ function AutoSettleDaysInput({ ); } +function BoundedIntegerInput({ + value, + min, + max, + onCommit, + "aria-label": ariaLabel, +}: { + value: number; + min: number; + max: number; + onCommit: (value: number) => void; + "aria-label": string; +}) { + const [draft, setDraft] = useState(String(value)); + const [source, setSource] = useState(value); + if (source !== value) { + setSource(value); + setDraft(String(value)); + } + return ( + { + setDraft(event.target.value); + const parsed = Number(event.target.value); + if (Number.isInteger(parsed) && parsed >= min && parsed <= max) onCommit(parsed); + }} + onBlur={() => setDraft(String(value))} + aria-label={ariaLabel} + /> + ); +} + export function GeneralSettingsPanel() { const gt = useGT(); const m = useMessages(); @@ -2238,9 +2284,70 @@ export function ThreadsSettingsPanel() { } /> + + updateSettings({ + threadTitleRegenerationInterval: + DEFAULT_UNIFIED_SETTINGS.threadTitleRegenerationInterval, + }) + } + /> + ) : null + } + control={ + + updateSettings({ + threadTitleRegenerationInterval: checked + ? TITLE_REGENERATION_DEFAULT_INTERVAL + : null, + }) + } + aria-label={gt("Regenerate thread titles automatically")} + /> + } + /> + {settings.threadTitleRegenerationInterval !== null ? ( + + updateSettings({ threadTitleRegenerationInterval: interval }) + } + aria-label={gt("Messages between title regenerations")} + /> + } + /> + ) : null}