Skip to content

Commit 224145f

Browse files
committed
fix(server): retain regen attachments by message
1 parent c375d78 commit 224145f

2 files changed

Lines changed: 47 additions & 30 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ describe("ProviderCommandReactor", () => {
815815
{
816816
type: "image",
817817
id: "opening-context-image",
818-
name: "opening.png",
818+
name: "image.png",
819819
mimeType: "image/png",
820820
sizeBytes: 5,
821821
},
@@ -839,7 +839,7 @@ describe("ProviderCommandReactor", () => {
839839
{
840840
type: "image",
841841
id: "middle-context-image",
842-
name: "middle.png",
842+
name: "image.png",
843843
mimeType: "image/png",
844844
sizeBytes: 5,
845845
},
@@ -863,7 +863,7 @@ describe("ProviderCommandReactor", () => {
863863
{
864864
type: "image",
865865
id: "recent-context-image",
866-
name: "recent.png",
866+
name: "image.png",
867867
mimeType: "image/png",
868868
sizeBytes: 5,
869869
},
@@ -894,8 +894,7 @@ describe("ProviderCommandReactor", () => {
894894
expect(message.startsWith("USER:\nReview subagent monitoring risks.")).toBe(true);
895895
expect(message).toContain("[First user message truncated]");
896896
expect(message).toContain("[Earlier content truncated]");
897-
expect(message).toContain("recent.png");
898-
expect(message).not.toContain("middle.png");
897+
expect(message).toContain("image.png");
899898
expect(message).toHaveLength(8_000);
900899
expect(input.attachments?.map((attachment) => attachment.id)).toEqual([
901900
"opening-context-image",

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ function limitFirstUserSection(section: string): string {
129129
)}${FIRST_USER_CONTEXT_TRUNCATION_MARKER}`;
130130
}
131131

132-
function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>): {
133-
readonly message: string;
132+
function collectRecentThreadTitleContext(
133+
messages: ReadonlyArray<ThreadTitleMessage>,
134+
maxChars: number,
135+
): {
136+
readonly context: string;
134137
readonly attachments: ReadonlyArray<ChatAttachment>;
138+
readonly truncated: boolean;
135139
} {
136140
let context = "";
137141
let truncated = false;
@@ -144,7 +148,7 @@ function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>):
144148
}
145149

146150
const separator = context.length > 0 ? "\n\n" : "";
147-
const available = MAX_THREAD_TITLE_CONTEXT_CHARS - context.length - separator.length;
151+
const available = maxChars - context.length - separator.length;
148152
if (section.length > available) {
149153
if (available > 0) {
150154
context = `${section.slice(-available)}${separator}${context}`;
@@ -157,34 +161,48 @@ function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>):
157161
retainedAttachments.unshift(...(message.attachments ?? []));
158162
}
159163

160-
let recentContext = context;
161-
const firstUserMessage = truncated
162-
? messages.find((message) => message.role === "user" && formatThreadTitleSection(message))
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)
163184
: undefined;
164-
if (firstUserMessage) {
165-
const firstUserSection = formatThreadTitleSection(firstUserMessage);
166-
if (firstUserSection) {
167-
const pinnedSection = limitFirstUserSection(firstUserSection);
168-
const recentContextBudget =
169-
MAX_THREAD_TITLE_CONTEXT_CHARS -
170-
pinnedSection.length -
171-
"\n\n".length -
172-
THREAD_TITLE_CONTEXT_TRUNCATION_MARKER.length;
173-
recentContext = context.slice(-recentContextBudget);
174-
context = `${pinnedSection}\n\n${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${recentContext}`;
175-
}
176-
} else if (truncated) {
177-
context = `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${context}`;
185+
if (!firstUserMessage || !firstUserSection) {
186+
return {
187+
message: `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${recent.context}`,
188+
attachments: recent.attachments.slice(-MAX_REGENERATION_ATTACHMENTS),
189+
};
178190
}
179191

180-
const pinnedAttachment = firstUserMessage?.attachments?.[0];
181-
const recentAttachments = retainedAttachments.filter(
182-
(attachment) =>
183-
attachment.id !== pinnedAttachment?.id && recentContext.includes(attachment.name),
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,
184202
);
185203

186204
return {
187-
message: context,
205+
message: `${pinnedSection}\n\n${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${retainedRecent.context}`,
188206
attachments: [
189207
...(pinnedAttachment ? [pinnedAttachment] : []),
190208
...recentAttachments.slice(

0 commit comments

Comments
 (0)