fix: Deduplicate scoped request context

This commit is contained in:
Danny Avila 2026-05-18 09:30:05 -04:00
parent 13bbd7f930
commit 04701fe5c1
2 changed files with 37 additions and 1 deletions

View file

@ -272,10 +272,17 @@ class AgentClient extends BaseClient {
}))
: []),
];
const sharedRunAttachmentIds = new Set();
if (this.options.attachments) {
const attachments = await this.options.attachments;
const latestMessage = orderedMessages[orderedMessages.length - 1];
for (const file of attachments) {
if (file?.file_id) {
sharedRunAttachmentIds.add(file.file_id);
}
}
if (this.message_file_map) {
this.message_file_map[latestMessage.messageId] = attachments;
} else {
@ -417,7 +424,9 @@ class AgentClient extends BaseClient {
const agentScopedContextEntries = await Promise.all(
allAgents.map(async ({ agentId }) => {
const attachments = getAgentContextAttachments(agentId);
const attachments = getAgentContextAttachments(agentId).filter(
(file) => !file?.file_id || !sharedRunAttachmentIds.has(file.file_id),
);
if (!attachments || attachments.length === 0) {
return [agentId, ''];
}

View file

@ -1551,6 +1551,33 @@ describe('AgentClient - titleConvo', () => {
expect(handoffAgent.additional_instructions).not.toContain('Primary private context');
});
it('does not duplicate a file that is both request context and scoped context', async () => {
const sharedFile = makeTextFile('shared-file', 'shared.txt', 'Shared duplicate context');
client.options.attachments = [sharedFile];
client.options.agentContextAttachmentsByAgentId = new Map([['primary-agent', [sharedFile]]]);
client.agentConfigs = new Map();
await client.buildMessages(
[
{
messageId: 'msg-1',
parentMessageId: null,
sender: 'User',
text: 'Use the available context.',
isCreatedByUser: true,
},
],
'msg-1',
{},
);
const occurrences = (
mockAgent.additional_instructions.match(/Shared duplicate context/g) ?? []
).length;
expect(occurrences).toBe(1);
});
it('keeps direct chats with context-doc agents working without request attachments', async () => {
const primaryContext = makeTextFile(
'primary-context',