mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
🚋 fix: Scope Document Name Traversal to Bedrock Agents (#17627)
* 🚋 fix: Scope Document Name Traversal to Bedrock Agents
* test: Preserve Replay Content Across Token Cache Refreshes
* fix: Include All Bedrock Replay Recipients in Document Naming
* fix: Include Lazy Provider Metadata in Document Naming
* perf: Short-Circuit Reachable Agent Predicates
---------
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
94d3d12bd5
commit
f03a4686ff
7 changed files with 372 additions and 29 deletions
|
|
@ -2907,7 +2907,7 @@ class AgentClient extends BaseClient {
|
|||
}
|
||||
}
|
||||
/** After the steer stamp, which adds replayed steers' documents of its own. */
|
||||
dedupeDocumentNames(payload);
|
||||
dedupeDocumentNames(payload, agentsById.values());
|
||||
if (hasFileContext) {
|
||||
for (let i = 0; i < orderedMessages.length; i++) {
|
||||
memoryPayload.push(
|
||||
|
|
|
|||
|
|
@ -6485,7 +6485,87 @@ describe('AgentClient - titleConvo', () => {
|
|||
expect(client.publishRunContextMeta).toHaveBeenCalledTimes(expectSeed ? 1 : 0);
|
||||
});
|
||||
|
||||
it('gives every resent document a unique name before formatting', async () => {
|
||||
it.each([Providers.OPENAI, Providers.ANTHROPIC, Providers.GOOGLE])(
|
||||
'continues a large existing %s chat with a new short message',
|
||||
async (provider) => {
|
||||
client.options.agent.provider = provider;
|
||||
client.agentConfigs = new Map([
|
||||
['secondary-agent', { ...mockAgent, id: 'secondary-agent', provider: Providers.GOOGLE }],
|
||||
]);
|
||||
const resolve = jest.fn();
|
||||
client.options.agent.lazySubagentConfigs = [
|
||||
{ id: 'lazy-agent', provider: Providers.ANTHROPIC, configId: 'lazy-config', resolve },
|
||||
];
|
||||
const history = Array.from({ length: 80 }, (_, index) => ({
|
||||
messageId: `history-${index}`,
|
||||
parentMessageId: index === 0 ? null : `history-${index - 1}`,
|
||||
sender: index % 2 === 0 ? 'User' : 'Assistant',
|
||||
isCreatedByUser: index % 2 === 0,
|
||||
content: Array.from({ length: 51 }, () => ({ type: ContentTypes.TEXT, text: 'ok' })),
|
||||
}));
|
||||
const latest = {
|
||||
messageId: 'next-message',
|
||||
parentMessageId: 'history-79',
|
||||
sender: 'User',
|
||||
isCreatedByUser: true,
|
||||
text: 'x',
|
||||
};
|
||||
const messages = [...history, latest];
|
||||
const original = structuredClone(messages);
|
||||
client.setModelBoundStoredMessages(messages);
|
||||
|
||||
const result = await client.buildMessages(messages, latest.messageId, {}, {});
|
||||
|
||||
expect(result.prompt).toHaveLength(messages.length);
|
||||
expect(result.prompt.at(-1).content).toBe('x');
|
||||
expect(messages).toMatchObject(original);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
'primary',
|
||||
'secondary',
|
||||
'nested subagent',
|
||||
'nested graph member',
|
||||
'lazy subagent',
|
||||
'nested lazy subagent',
|
||||
'lazy graph member',
|
||||
])('gives every resent document a unique name for a Bedrock %s', async (recipient) => {
|
||||
const bedrockAgent = {
|
||||
...mockAgent,
|
||||
id: 'bedrock-recipient',
|
||||
provider: Providers.BEDROCK,
|
||||
};
|
||||
const resolve = jest.fn().mockResolvedValue(bedrockAgent);
|
||||
const lazyAgent = { ...bedrockAgent, configId: 'bedrock-config', resolve };
|
||||
if (recipient === 'primary') {
|
||||
client.options.agent.provider = Providers.BEDROCK;
|
||||
} else if (recipient === 'secondary') {
|
||||
client.agentConfigs = new Map([[bedrockAgent.id, bedrockAgent]]);
|
||||
} else if (recipient === 'nested subagent') {
|
||||
client.options.agent.subagentAgentConfigs = [
|
||||
{ ...mockAgent, id: 'middle-agent', subagentAgentConfigs: [bedrockAgent] },
|
||||
];
|
||||
} else if (recipient === 'nested graph member') {
|
||||
client.options.agent.subagentGraphConfigs = [{ memberConfigs: [bedrockAgent] }];
|
||||
} else if (recipient === 'lazy subagent') {
|
||||
client.options.agent.lazySubagentConfigs = [lazyAgent];
|
||||
} else if (recipient === 'nested lazy subagent') {
|
||||
client.options.agent.subagentAgentConfigs = [
|
||||
{ ...mockAgent, id: 'middle-agent', lazySubagentConfigs: [lazyAgent] },
|
||||
];
|
||||
} else {
|
||||
client.options.agent.lazySubagentConfigs = [
|
||||
{
|
||||
id: 'lazy-team',
|
||||
provider: Providers.OPENAI,
|
||||
configId: 'team-config',
|
||||
resolve,
|
||||
subagentGraphMemberMetadata: [bedrockAgent],
|
||||
},
|
||||
];
|
||||
}
|
||||
const bedrockDocument = (name) => ({
|
||||
type: 'document',
|
||||
document: { name, format: 'pdf', source: { bytes: Buffer.from(name) } },
|
||||
|
|
@ -6526,6 +6606,9 @@ describe('AgentClient - titleConvo', () => {
|
|||
: [],
|
||||
);
|
||||
expect(names).toEqual(['Stripe fees', 'Stripe fees (2)']);
|
||||
expect(firstQuestion.documents[0].document.name).toBe('Stripe fees');
|
||||
expect(secondQuestion.documents[0].document.name).toBe('Stripe fees');
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('preserves persisted source identity through both agent formatting passes', async () => {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { collectReachableAgents } from './traversal';
|
||||
import { someReachableAgent, collectReachableAgents } from './traversal';
|
||||
|
||||
interface TestAgent {
|
||||
id: string;
|
||||
|
|
@ -43,3 +43,101 @@ describe('collectReachableAgents', () => {
|
|||
expect(collectReachableAgents([root])).toEqual([root, eager, lazy, graphMetadata, graphMember]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('someReachableAgent', () => {
|
||||
it('stops before reading matched descendants or consuming later roots', () => {
|
||||
const descendants = jest.fn(() => {
|
||||
throw new Error('Matched descendants must not be read');
|
||||
});
|
||||
const matched: TestAgent = {
|
||||
id: 'match',
|
||||
get lazySubagentConfigs() {
|
||||
return descendants();
|
||||
},
|
||||
};
|
||||
const laterRoots = jest.fn(() => {
|
||||
throw new Error('Later roots must not be consumed');
|
||||
});
|
||||
function* roots() {
|
||||
yield matched;
|
||||
yield laterRoots();
|
||||
}
|
||||
const predicate = jest.fn((agent: TestAgent) => agent.id === 'match');
|
||||
|
||||
expect(someReachableAgent(roots(), predicate)).toBe(true);
|
||||
expect(predicate).toHaveBeenCalledTimes(1);
|
||||
expect(descendants).not.toHaveBeenCalled();
|
||||
expect(laterRoots).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
'subagentAgentConfigs',
|
||||
'lazySubagentConfigs',
|
||||
'subagentGraphMemberMetadata',
|
||||
'subagentGraphConfigs',
|
||||
] as const)('finds a descendant through %s without resolving it', (route) => {
|
||||
const resolve = jest.fn();
|
||||
const matched = { id: 'match', resolve };
|
||||
const root: TestAgent = {
|
||||
id: 'root',
|
||||
...(route === 'subagentGraphConfigs'
|
||||
? { subagentGraphConfigs: [{ memberConfigs: [matched] }] }
|
||||
: { [route]: [matched] }),
|
||||
};
|
||||
|
||||
expect(someReachableAgent([root], (agent) => agent.id === 'match')).toBe(true);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('visits every distinct object once in breadth-first order when nothing matches', () => {
|
||||
const first: TestAgent = { id: 'same' };
|
||||
const second: TestAgent = { id: 'same' };
|
||||
const root: TestAgent = {
|
||||
id: 'root',
|
||||
subagentAgentConfigs: [first, second],
|
||||
lazySubagentConfigs: [first],
|
||||
};
|
||||
first.subagentGraphMemberMetadata = [root, second];
|
||||
second.subagentAgentConfigs = [root];
|
||||
const predicate = jest.fn(() => false);
|
||||
|
||||
expect(someReachableAgent([null, root, root, undefined], predicate)).toBe(false);
|
||||
expect(predicate.mock.calls).toEqual([[root], [first], [second]]);
|
||||
expect(collectReachableAgents([null, root, root, undefined])).toEqual([root, first, second]);
|
||||
});
|
||||
|
||||
it('stops before inspecting later descendants after a nested match', () => {
|
||||
const match: TestAgent = { id: 'match' };
|
||||
const later: TestAgent = { id: 'later' };
|
||||
const root: TestAgent = { id: 'root', lazySubagentConfigs: [match, later] };
|
||||
const predicate = jest.fn((agent: TestAgent) => agent === match);
|
||||
|
||||
expect(someReachableAgent([root], predicate)).toBe(true);
|
||||
expect(predicate.mock.calls).toEqual([[root], [match]]);
|
||||
});
|
||||
|
||||
it('does not invoke the predicate for empty roots', () => {
|
||||
const predicate = jest.fn(() => true);
|
||||
|
||||
expect(someReachableAgent<TestAgent>([], predicate)).toBe(false);
|
||||
expect(predicate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('propagates predicate failures without expanding the matched node', () => {
|
||||
const failure = new Error('Predicate failed');
|
||||
const descendants = jest.fn();
|
||||
const root: TestAgent = {
|
||||
id: 'root',
|
||||
get subagentAgentConfigs() {
|
||||
return descendants();
|
||||
},
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
someReachableAgent([root], () => {
|
||||
throw failure;
|
||||
}),
|
||||
).toThrow(failure);
|
||||
expect(descendants).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,34 +7,63 @@ export interface ReachableAgent<TAgent> {
|
|||
}[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects each agent object reachable from the supplied roots exactly once.
|
||||
* Object identity preserves distinct initialized snapshots while preventing
|
||||
* shared subagent nodes and cyclic graphs from repeating work.
|
||||
*/
|
||||
/** Visits each object once in breadth-first order, stopping before a match's descendants. */
|
||||
export function someReachableAgent<T extends ReachableAgent<T>>(
|
||||
roots: Iterable<T | null | undefined>,
|
||||
predicate: (agent: T) => boolean,
|
||||
): boolean {
|
||||
let visited: Set<T> | undefined;
|
||||
let pending: Array<T | null | undefined> | undefined;
|
||||
const matches = (agent: T | null | undefined): boolean => {
|
||||
if (agent == null || visited?.has(agent)) {
|
||||
return false;
|
||||
}
|
||||
if (predicate(agent)) {
|
||||
return true;
|
||||
}
|
||||
(visited ??= new Set<T>()).add(agent);
|
||||
const eager = agent.subagentAgentConfigs;
|
||||
const lazy = agent.lazySubagentConfigs;
|
||||
const metadata = agent.subagentGraphMemberMetadata;
|
||||
if (eager?.length) {
|
||||
(pending ??= []).push(...eager);
|
||||
}
|
||||
if (lazy?.length) {
|
||||
(pending ??= []).push(...lazy);
|
||||
}
|
||||
if (metadata?.length) {
|
||||
(pending ??= []).push(...metadata);
|
||||
}
|
||||
for (const graph of agent.subagentGraphConfigs ?? []) {
|
||||
const members = graph.memberConfigs;
|
||||
if (members?.length) {
|
||||
(pending ??= []).push(...members);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
for (const root of roots) {
|
||||
if (matches(root)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (let index = 0; pending && index < pending.length; index++) {
|
||||
if (matches(pending[index])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Collects distinct snapshots in breadth-first order, including shared and cyclic graphs. */
|
||||
export function collectReachableAgents<T extends ReachableAgent<T>>(
|
||||
roots: readonly (T | null | undefined)[],
|
||||
): T[] {
|
||||
const agents: T[] = [];
|
||||
const visited = new Set<T>();
|
||||
const pending = [...roots];
|
||||
|
||||
for (let index = 0; index < pending.length; index++) {
|
||||
const agent = pending[index];
|
||||
if (agent == null || visited.has(agent)) {
|
||||
continue;
|
||||
}
|
||||
visited.add(agent);
|
||||
someReachableAgent(roots, (agent) => {
|
||||
agents.push(agent);
|
||||
pending.push(
|
||||
...(agent.subagentAgentConfigs ?? []),
|
||||
...(agent.lazySubagentConfigs ?? []),
|
||||
...(agent.subagentGraphMemberMetadata ?? []),
|
||||
);
|
||||
for (const graph of agent.subagentGraphConfigs ?? []) {
|
||||
pending.push(...(graph.memberConfigs ?? []));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
return agents;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1222,6 +1222,113 @@ describe('dedupeDocumentNames', () => {
|
|||
);
|
||||
const contentOf = (message: Message) => (Array.isArray(message.content) ? message.content : []);
|
||||
|
||||
it.each(Object.values(Providers).filter((provider) => provider !== Providers.BEDROCK))(
|
||||
'does not inspect replay content for %s',
|
||||
(provider) => {
|
||||
const content = jest.fn(() => {
|
||||
throw new Error('Non-Bedrock content must not be traversed');
|
||||
});
|
||||
const messages: Message[] = [
|
||||
{
|
||||
get content() {
|
||||
return content();
|
||||
},
|
||||
},
|
||||
];
|
||||
messages.length = CONTENT_TRAVERSAL_MAX_NODES + 1;
|
||||
|
||||
dedupeDocumentNames(messages, [{ provider }]);
|
||||
|
||||
expect(content).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it('stops recipient traversal as soon as Bedrock naming is required', () => {
|
||||
const descendants = jest.fn(() => {
|
||||
throw new Error('Bedrock descendants must not be read');
|
||||
});
|
||||
const recipients = [
|
||||
{
|
||||
provider: Providers.BEDROCK,
|
||||
get lazySubagentConfigs() {
|
||||
return descendants();
|
||||
},
|
||||
},
|
||||
];
|
||||
const messages: Message[] = [{ content: [bedrock('notes'), bedrock('notes')] }];
|
||||
|
||||
dedupeDocumentNames(messages, recipients);
|
||||
|
||||
expect(names(contentOf(messages[0]))).toEqual(['notes', 'notes (2)']);
|
||||
expect(descendants).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('renames documents for a Bedrock recipient after a non-Bedrock primary', () => {
|
||||
const messages: Message[] = [{ content: [bedrock('notes'), bedrock('notes')] }];
|
||||
const recipients = new Map([
|
||||
['primary', { provider: Providers.OPENAI }],
|
||||
['secondary', { provider: Providers.BEDROCK }],
|
||||
]);
|
||||
|
||||
dedupeDocumentNames(messages, recipients.values());
|
||||
|
||||
expect(names(contentOf(messages[0]))).toEqual(['notes', 'notes (2)']);
|
||||
});
|
||||
|
||||
it.each(['lazySubagentConfigs', 'subagentGraphMemberMetadata'] as const)(
|
||||
'includes Bedrock providers in %s without resolving them',
|
||||
(route) => {
|
||||
const messages: Message[] = [{ content: [bedrock('notes'), bedrock('notes')] }];
|
||||
const resolve = jest.fn();
|
||||
const child = { provider: Providers.BEDROCK, resolve };
|
||||
const recipients = [{ provider: Providers.OPENAI, [route]: [child] }];
|
||||
|
||||
dedupeDocumentNames(messages, recipients);
|
||||
|
||||
expect(names(contentOf(messages[0]))).toEqual(['notes', 'notes (2)']);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it('handles cyclic metadata without losing distinct snapshots of the same agent', () => {
|
||||
const child = { id: 'shared-id', provider: Providers.BEDROCK };
|
||||
const root = {
|
||||
id: 'shared-id',
|
||||
provider: Providers.OPENAI,
|
||||
lazySubagentConfigs: [] as Array<{ provider: Providers }>,
|
||||
};
|
||||
root.lazySubagentConfigs.push(root, child, child);
|
||||
const messages: Message[] = [{ content: [bedrock('notes'), bedrock('notes')] }];
|
||||
|
||||
dedupeDocumentNames(messages, [root]);
|
||||
|
||||
expect(names(contentOf(messages[0]))).toEqual(['notes', 'notes (2)']);
|
||||
});
|
||||
|
||||
it('leaves a large replay untouched without any Bedrock recipients', () => {
|
||||
const content: PayloadContentPart[] = new Array(CONTENT_TRAVERSAL_MAX_NODES);
|
||||
const messages: Message[] = [{ content }];
|
||||
const recipients = new Map([
|
||||
['primary', { provider: Providers.OPENAI }],
|
||||
['secondary', { provider: Providers.ANTHROPIC }],
|
||||
]);
|
||||
|
||||
const resolve = jest.fn();
|
||||
const lazy = {
|
||||
provider: Providers.GOOGLE,
|
||||
resolve,
|
||||
subagentGraphMemberMetadata: [{ provider: Providers.ANTHROPIC }],
|
||||
lazySubagentConfigs: [] as Array<{ provider: Providers }>,
|
||||
};
|
||||
lazy.lazySubagentConfigs.push(lazy);
|
||||
const root = { provider: Providers.OPENAI, lazySubagentConfigs: [lazy] };
|
||||
|
||||
dedupeDocumentNames(messages, [...recipients.values(), root]);
|
||||
|
||||
expect(messages[0].content).toBe(content);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('renames repeated Bedrock document names across the payload in order', () => {
|
||||
const first = bedrock('Stripe fees');
|
||||
const messages: Message[] = [
|
||||
|
|
@ -1347,6 +1454,12 @@ describe('dedupeDocumentNames', () => {
|
|||
const messages: Message[] = [{ content }];
|
||||
|
||||
expect(() => dedupeDocumentNames(messages)).toThrow(ContentTraversalLimitError);
|
||||
expect(() =>
|
||||
dedupeDocumentNames(messages, [
|
||||
{ provider: Providers.OPENAI },
|
||||
{ provider: Providers.BEDROCK },
|
||||
]),
|
||||
).toThrow(ContentTraversalLimitError);
|
||||
expect(messages[0].content).toBe(content);
|
||||
expect(content[1].document.name).toBe('notes');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import type {
|
|||
DocumentResult,
|
||||
ServerRequest,
|
||||
} from '~/types';
|
||||
import type { ReachableAgent } from '~/agents/traversal';
|
||||
import {
|
||||
CONTENT_TRAVERSAL_MAX_DEPTH,
|
||||
CONTENT_TRAVERSAL_MAX_NODES,
|
||||
|
|
@ -26,6 +27,7 @@ import {
|
|||
isAttachmentObjectNotFoundError,
|
||||
} from './utils';
|
||||
import { validatePdf, validateBedrockDocument } from '~/files/validation';
|
||||
import { someReachableAgent } from '~/agents/traversal';
|
||||
import { runGuardedEncode } from './memoryGuard';
|
||||
|
||||
/** The longest document name LibreChat sends to Bedrock. */
|
||||
|
|
@ -95,6 +97,18 @@ function renameDocumentParts(
|
|||
return renamed ?? parts;
|
||||
}
|
||||
|
||||
interface DocumentRecipient extends ReachableAgent<DocumentRecipient> {
|
||||
readonly provider?: string;
|
||||
}
|
||||
|
||||
function isBedrockRecipient({ provider }: DocumentRecipient): boolean {
|
||||
return provider === Providers.BEDROCK;
|
||||
}
|
||||
|
||||
function hasBedrockRecipient(recipients: Iterable<DocumentRecipient>): boolean {
|
||||
return someReachableAgent(recipients, isBedrockRecipient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bedrock rejects a request in which two documents share a name, and every turn resends the
|
||||
* documents of earlier turns (and of steers replayed with their attachments), so two uploads with
|
||||
|
|
@ -102,11 +116,17 @@ function renameDocumentParts(
|
|||
* formatted payload in conversation order (`report`, `report (2)`), leaving the first occurrence,
|
||||
* every other kind of part, and the parts themselves untouched: a message whose documents change
|
||||
* gets a new content array. Names compare case-insensitively, as the restriction is not
|
||||
* documented to be case-sensitive. Replay always enforces traversal limits, even without filters.
|
||||
* documented to be case-sensitive. Recipient metadata includes lazy children and graph
|
||||
* members without resolving them. Only runs with a Bedrock recipient need naming;
|
||||
* Bedrock replay enforces traversal limits even without filters.
|
||||
*/
|
||||
export function dedupeDocumentNames(
|
||||
messages: Array<{ content?: string | PayloadContentPart[] | null }>,
|
||||
recipients?: Iterable<DocumentRecipient>,
|
||||
): void {
|
||||
if (recipients != null && !hasBedrockRecipient(recipients)) {
|
||||
return;
|
||||
}
|
||||
const budget = { remaining: CONTENT_TRAVERSAL_MAX_NODES - messages.length };
|
||||
if (budget.remaining < 0) {
|
||||
throw new ContentTraversalLimitError();
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import type { TMessage } from 'librechat-data-provider';
|
|||
import type { ReactNode } from 'react';
|
||||
import { submissionByIndex, useIsSubmitting } from '../submission';
|
||||
import useQueuedTurnReveal from '../steering/useQueuedTurnReveal';
|
||||
import useQueueDrain from '../steering/useQueueDrain';
|
||||
import { useResumeOnLoad } from '../stream/resume/load';
|
||||
import useQueueDrain from '../steering/useQueueDrain';
|
||||
import useAdaptiveSSE from '../stream/adaptive';
|
||||
import { ChatContext } from './context';
|
||||
import useChatHelpers from './helpers';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue