Repository navigation
feat(settings): regenerate thread titles every N messages #34682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3fb9ba6
1ca831a
1959ce0
4e137b4
a98cc00
d30f64c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<OrchestrationV2Command, { readonly type: "message.dispatch" }>, | ||
| thread: OrchestrationV2AppThread, | ||
| userMessages: ReadonlyArray<OrchestrationV2ConversationMessage>, | ||
| ) { | ||
| // 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; | ||
|
Comment on lines
+6831
to
+6838
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid loading the full message history on every eligible send. Even with interval 100, this reads all user message records and rebuilds the ID map on sends where no refresh is due. On long compacted threads, this adds work that grows with history while holding the thread's dispatch lock. Use a narrow count and message-ID lookup, or maintain an eligibility count in the projection. Preserve the distinction between stored rows and pending updates. As per coding guidelines, “Make sure all changes are considerate of performance impact.”
|
||
| }); | ||
|
|
||
| // Providers own native subagent conversations; messages cannot target them, and adapters ask questions on the parent. | ||
| const refuseNativeSubagentMessage = Effect.fnUntraced(function* ( | ||
| command: Pick<OrchestrationV2Command, "commandId"> & { 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); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,14 +56,21 @@ const adapter = { | |
| function makeHarness( | ||
| options: { | ||
| readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"]; | ||
| readonly regenerationInterval?: number; | ||
| } = {}, | ||
| ) { | ||
| const layerDatabase = SqlitePersistence.layerMemory; | ||
| const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([adapter]); | ||
| 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,19 +161,25 @@ function dispatchUserMessage(input: { | |
| readonly threadId: ThreadId; | ||
| readonly text: string; | ||
| readonly attachments?: ReadonlyArray<ChatAttachment>; | ||
| readonly titleSeed?: string; | ||
| readonly messageId?: MessageId; | ||
| readonly createdBy?: "user" | "agent"; | ||
| readonly scheduledTaskId?: ScheduledTaskId; | ||
| }) { | ||
| return Effect.gen(function* () { | ||
| const threads = yield* ThreadManagement.ThreadManagementService; | ||
| yield* threads.dispatch({ | ||
| 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<typeof dispatchUserMessage>[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]) { | ||
|
Comment on lines
+1017
to
+1022
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prove the thread is eligible before checking the off setting. If initial completion leaves
|
||
| 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({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not reject a chat message when its title count cannot be read.
If the recent command projection loads but the additional title-history read fails, dispatch records the send as rejected before it commits the message. Retrying the same command ID returns the saved rejection. Before this change, the send did not depend on this extra read. Log the title-history failure and skip this refresh. Keep normal dispatch validation and commit errors on the existing failure path.