Skip to content

Commit 9bd2a4c

Browse files
authored
fix(server): keep regenerated titles on topic (pingdotgg#5365)
1 parent 4fb03af commit 9bd2a4c

4 files changed

Lines changed: 290 additions & 60 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,123 @@ describe("ProviderCommandReactor", () => {
785785
expect(thread?.titleRegeneration).toBeNull();
786786
});
787787

788+
it("pins the first user message when regeneration context is truncated", async () => {
789+
const harness = await createHarness();
790+
const now = "2026-01-01T00:00:00.000Z";
791+
const firstUserMessage = `Review subagent monitoring risks. ${"Opening context. ".repeat(200)}`;
792+
const recentUserMessage = `LATEST FINDING: ${"implementation detail ".repeat(320)}`;
793+
harness.generateThreadTitle.mockReturnValue(
794+
Effect.succeed({ title: "Review subagent monitoring risks" }),
795+
);
796+
797+
await harness.runEffect(
798+
harness.engine.dispatch({
799+
type: "thread.meta.update",
800+
commandId: CommandId.make("cmd-thread-title-existing-long"),
801+
threadId: ThreadId.make("thread-1"),
802+
title: "Generic PR review",
803+
}),
804+
);
805+
await harness.runEffect(
806+
harness.engine.dispatch({
807+
type: "thread.turn.start",
808+
commandId: CommandId.make("cmd-turn-start-before-long-title-regeneration"),
809+
threadId: ThreadId.make("thread-1"),
810+
message: {
811+
messageId: asMessageId("user-message-before-long-title-regeneration"),
812+
role: "user",
813+
text: firstUserMessage,
814+
attachments: [
815+
{
816+
type: "image",
817+
id: "opening-context-image",
818+
name: "image.png",
819+
mimeType: "image/png",
820+
sizeBytes: 5,
821+
},
822+
],
823+
},
824+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
825+
runtimeMode: "approval-required",
826+
createdAt: now,
827+
}),
828+
);
829+
await harness.runEffect(
830+
harness.engine.dispatch({
831+
type: "thread.turn.start",
832+
commandId: CommandId.make("cmd-middle-turn-before-long-title-regeneration"),
833+
threadId: ThreadId.make("thread-1"),
834+
message: {
835+
messageId: asMessageId("middle-message-before-long-title-regeneration"),
836+
role: "user",
837+
text: "Temporary handoff details.",
838+
attachments: [
839+
{
840+
type: "image",
841+
id: "middle-context-image",
842+
name: "image.png",
843+
mimeType: "image/png",
844+
sizeBytes: 5,
845+
},
846+
],
847+
},
848+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
849+
runtimeMode: "approval-required",
850+
createdAt: "2026-01-01T00:00:01.000Z",
851+
}),
852+
);
853+
await harness.runEffect(
854+
harness.engine.dispatch({
855+
type: "thread.turn.start",
856+
commandId: CommandId.make("cmd-recent-turn-before-long-title-regeneration"),
857+
threadId: ThreadId.make("thread-1"),
858+
message: {
859+
messageId: asMessageId("recent-message-before-long-title-regeneration"),
860+
role: "user",
861+
text: recentUserMessage,
862+
attachments: [
863+
{
864+
type: "image",
865+
id: "recent-context-image",
866+
name: "image.png",
867+
mimeType: "image/png",
868+
sizeBytes: 5,
869+
},
870+
],
871+
},
872+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
873+
runtimeMode: "approval-required",
874+
createdAt: "2026-01-01T00:00:02.000Z",
875+
}),
876+
);
877+
await harness.runEffect(
878+
harness.engine.dispatch({
879+
type: "thread.meta.update",
880+
commandId: CommandId.make("cmd-thread-title-regenerate-long"),
881+
threadId: ThreadId.make("thread-1"),
882+
regenerateTitle: true,
883+
}),
884+
);
885+
886+
await harness.drain();
887+
888+
expect(harness.generateThreadTitle).toHaveBeenCalledTimes(1);
889+
const input = harness.generateThreadTitle.mock.calls[0]?.[0];
890+
if (!input) {
891+
throw new Error("Expected a title generation input");
892+
}
893+
const message = input.message;
894+
expect(message.startsWith("USER:\nReview subagent monitoring risks.")).toBe(true);
895+
expect(message).toContain("[First user message truncated]");
896+
expect(message).toContain("[Earlier content truncated]");
897+
expect(message).toContain("image.png");
898+
expect(message).toHaveLength(8_000);
899+
expect(input.attachments?.map((attachment) => attachment.id)).toEqual([
900+
"opening-context-image",
901+
"recent-context-image",
902+
]);
903+
});
904+
788905
it("clears title regeneration state left pending across reactor startup", async () => {
789906
const harness = await createHarness({
790907
titleRegenerationBeforeStart: "one",
@@ -972,10 +1089,14 @@ describe("ProviderCommandReactor", () => {
9721089
expect(thread?.titleRegeneration).toBeNull();
9731090
});
9741091

975-
it("keeps the full retained context and excludes attachments outside it", async () => {
1092+
it("pins the first user context and attachment before the retained tail", async () => {
9761093
const harness = await createHarness();
9771094
const now = "2026-01-01T00:00:00.000Z";
978-
const retainedContext = "x".repeat(8_000);
1095+
const firstUserContext = "USER:\nOld visual issue\n[Attachments: old-issue.png]";
1096+
const truncationMarker = "[Earlier content truncated]\n\n";
1097+
const retainedContext = "x".repeat(
1098+
8_000 - firstUserContext.length - "\n\n".length - truncationMarker.length,
1099+
);
9791100

9801101
await harness.runEffect(
9811102
harness.engine.dispatch({
@@ -1040,9 +1161,14 @@ describe("ProviderCommandReactor", () => {
10401161
await harness.drain();
10411162

10421163
expect(harness.generateThreadTitle.mock.calls[0]?.[0].message).toBe(
1043-
`[Earlier content truncated]\n\n${retainedContext}`,
1164+
`${firstUserContext}\n\n${truncationMarker}${retainedContext}`,
10441165
);
1045-
expect(harness.generateThreadTitle.mock.calls[0]?.[0].attachments).toBeUndefined();
1166+
expect(harness.generateThreadTitle.mock.calls[0]?.[0].attachments).toEqual([
1167+
expect.objectContaining({
1168+
id: "old-title-context-image",
1169+
name: "old-issue.png",
1170+
}),
1171+
]);
10461172
});
10471173

10481174
it("does not overwrite a manual rename while title regeneration is running", async () => {

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,41 +94,61 @@ const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
9494
const DEFAULT_THREAD_TITLE = "New thread";
9595
const MAX_REGENERATION_ATTACHMENTS = 4;
9696
const MAX_THREAD_TITLE_CONTEXT_CHARS = 8_000;
97+
const MAX_FIRST_USER_TITLE_CONTEXT_CHARS = 2_000;
9798
const THREAD_TITLE_CONTEXT_TRUNCATION_MARKER = "[Earlier content truncated]\n\n";
99+
const FIRST_USER_CONTEXT_TRUNCATION_MARKER = "\n[First user message truncated]";
98100

99-
function formatThreadTitleContext(
100-
messages: ReadonlyArray<{
101-
readonly role: "user" | "assistant" | "system";
102-
readonly text: string;
103-
readonly attachments?: ReadonlyArray<ChatAttachment> | undefined;
104-
}>,
101+
type ThreadTitleMessage = {
102+
readonly role: "user" | "assistant" | "system";
103+
readonly text: string;
104+
readonly attachments?: ReadonlyArray<ChatAttachment> | undefined;
105+
};
106+
107+
function formatThreadTitleSection(message: ThreadTitleMessage): string | undefined {
108+
if (message.role === "system") {
109+
return undefined;
110+
}
111+
const text = message.text.trim();
112+
const attachmentSummary = (message.attachments ?? [])
113+
.map((attachment) => attachment.name)
114+
.join(", ");
115+
const contents = [
116+
...(text.length > 0 ? [text] : []),
117+
...(attachmentSummary.length > 0 ? [`[Attachments: ${attachmentSummary}]`] : []),
118+
].join("\n");
119+
return contents.length > 0 ? `${message.role.toUpperCase()}:\n${contents}` : undefined;
120+
}
121+
122+
function limitFirstUserSection(section: string): string {
123+
if (section.length <= MAX_FIRST_USER_TITLE_CONTEXT_CHARS) {
124+
return section;
125+
}
126+
return `${section.slice(
127+
0,
128+
MAX_FIRST_USER_TITLE_CONTEXT_CHARS - FIRST_USER_CONTEXT_TRUNCATION_MARKER.length,
129+
)}${FIRST_USER_CONTEXT_TRUNCATION_MARKER}`;
130+
}
131+
132+
function collectRecentThreadTitleContext(
133+
messages: ReadonlyArray<ThreadTitleMessage>,
134+
maxChars: number,
105135
): {
106-
readonly message: string;
136+
readonly context: string;
107137
readonly attachments: ReadonlyArray<ChatAttachment>;
138+
readonly truncated: boolean;
108139
} {
109140
let context = "";
110141
let truncated = false;
111142
const retainedAttachments: Array<ChatAttachment> = [];
112143

113144
for (const message of messages.toReversed()) {
114-
if (message.role === "system") {
115-
continue;
116-
}
117-
const text = message.text.trim();
118-
const attachmentSummary = (message.attachments ?? [])
119-
.map((attachment) => attachment.name)
120-
.join(", ");
121-
const contents = [
122-
...(text.length > 0 ? [text] : []),
123-
...(attachmentSummary.length > 0 ? [`[Attachments: ${attachmentSummary}]`] : []),
124-
].join("\n");
125-
if (contents.length === 0) {
145+
const section = formatThreadTitleSection(message);
146+
if (section === undefined) {
126147
continue;
127148
}
128149

129-
const section = `${message.role.toUpperCase()}:\n${contents}`;
130150
const separator = context.length > 0 ? "\n\n" : "";
131-
const available = MAX_THREAD_TITLE_CONTEXT_CHARS - context.length - separator.length;
151+
const available = maxChars - context.length - separator.length;
132152
if (section.length > available) {
133153
if (available > 0) {
134154
context = `${section.slice(-available)}${separator}${context}`;
@@ -141,9 +161,54 @@ function formatThreadTitleContext(
141161
retainedAttachments.unshift(...(message.attachments ?? []));
142162
}
143163

164+
return { context, attachments: retainedAttachments, truncated };
165+
}
166+
167+
function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>): {
168+
readonly message: string;
169+
readonly attachments: ReadonlyArray<ChatAttachment>;
170+
} {
171+
const recent = collectRecentThreadTitleContext(messages, MAX_THREAD_TITLE_CONTEXT_CHARS);
172+
if (!recent.truncated) {
173+
return {
174+
message: recent.context,
175+
attachments: recent.attachments.slice(-MAX_REGENERATION_ATTACHMENTS),
176+
};
177+
}
178+
179+
const firstUserMessage = messages.find(
180+
(message) => message.role === "user" && formatThreadTitleSection(message),
181+
);
182+
const firstUserSection = firstUserMessage
183+
? formatThreadTitleSection(firstUserMessage)
184+
: undefined;
185+
if (!firstUserMessage || !firstUserSection) {
186+
return {
187+
message: `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${recent.context}`,
188+
attachments: recent.attachments.slice(-MAX_REGENERATION_ATTACHMENTS),
189+
};
190+
}
191+
192+
const pinnedSection = limitFirstUserSection(firstUserSection);
193+
const recentContextBudget =
194+
MAX_THREAD_TITLE_CONTEXT_CHARS -
195+
pinnedSection.length -
196+
"\n\n".length -
197+
THREAD_TITLE_CONTEXT_TRUNCATION_MARKER.length;
198+
const retainedRecent = collectRecentThreadTitleContext(messages, recentContextBudget);
199+
const pinnedAttachment = firstUserMessage.attachments?.[0];
200+
const recentAttachments = retainedRecent.attachments.filter(
201+
(attachment) => attachment.id !== pinnedAttachment?.id,
202+
);
203+
144204
return {
145-
message: truncated ? `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${context}` : context,
146-
attachments: retainedAttachments.slice(-MAX_REGENERATION_ATTACHMENTS),
205+
message: `${pinnedSection}\n\n${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${retainedRecent.context}`,
206+
attachments: [
207+
...(pinnedAttachment ? [pinnedAttachment] : []),
208+
...recentAttachments.slice(
209+
-(MAX_REGENERATION_ATTACHMENTS - (pinnedAttachment === undefined ? 0 : 1)),
210+
),
211+
],
147212
};
148213
}
149214

‎apps/server/src/textGeneration/TextGenerationPrompts.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,17 @@ describe("buildThreadTitlePrompt", () => {
195195
});
196196

197197
expect(result.prompt).toContain(
198-
"Generate a new title that will help the user recognize this T3 Code thread weeks later.",
198+
"Regenerate the title for an existing T3 Code thread so the user can recognize it weeks later.",
199199
);
200200
expect(result.prompt).toContain('The previous title was "Investigate reconnect regressions".');
201201
expect(result.prompt).toContain(
202-
"Capture the current durable subject and outcome across the whole thread, not merely its initial request or latest step.",
202+
"Read the USER messages first. Identify the latest explicit durable goal.",
203+
);
204+
expect(result.prompt).toContain(
205+
"Do not promote one assistant finding into the thread subject unless the user adopts it as a new goal.",
206+
);
207+
expect(result.prompt).toContain(
208+
'A subagent-monitoring review that finds a Codex roster bug remains "Review Subagent Monitoring Risks,"',
203209
);
204210
expect(result.prompt).toContain("Thread contents:");
205211
expect(result.prompt).toContain("The remaining issue is stale session state");

0 commit comments

Comments
 (0)