⚡ perf: Exclude Agent Version History from Default getAgent Reads - #57
Closed
devin-ai-integration[bot] wants to merge 1 commit into
Closed
devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
getAgent now defaults its projection to { versions: 0 }; loadAgent/loadAddedAgent
and canAccessAgentFromBody use getAgentWithVersionCount so the version count stays
exact without transferring the unbounded versions array on every chat request.
Callers that need history (v1 update/revert handlers) request it explicitly.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
👋 Thanks for the contribution! LibreChat merges all changes into Nothing is needed from you; your commits, reviews and discussion are unchanged. If the diff now shows files you did not touch, rebase onto git remote add upstream https://github.com/berry-13/LibreChat.git
git fetch upstream dev
git rebase upstream/dev
git push --force-with-leaseMaintainers: apply the |
|
Lighthouse CI failed. The last 80 log lines contain the measured budgets and assertion failures. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every agent chat request read the agent document twice with no projection (
canAccessAgentFromBody→getAgent, thenbuildOptions→loadAgent→getAgent), so the unboundedversions[]snapshot array (one full agent copy per edit, never trimmed byupdateAgent) was transferred and BSON-decoded on every request, even though the chat path only readsversions.length. The same happened in the ~30 othergetAgent({ id })callers that only need metadata for permission checks.getAgentnow excludesversionsby default, and the chat hot path resolves the agent through the existinggetAgentWithVersionCountaggregate ($size→version), so the reportedversionis unchanged while the per-request payload drops from(1 + N versions) × agentSizeto~1 × agentSizeper read. Only the v1 update/revert handlers, which actually readagent.versions, request the full document explicitly.How it works
loadAgent/loadAddedAgentkeep thegetAgentdep name but its result is now typedAgent & { version?: number; versions?: unknown[] }, and the version count is taken fromversionwhen present (falling back toversions.lengthfor any injected dep still returning the full doc).build.js,client.jsandaddedConvo.jswiredb.getAgentWithVersionCountinto that dep.Callers that need history pass an explicit empty projection:
db.getAgent({ id }, {})inupdateAgentHandler(result may be returned asupdatedAgentwithversion = versions.length) andrevertAgentVersionHandler.Expected win: for an agent edited N times, each chat request read moves from O(N × agentSize) to O(agentSize) — e.g. 100 edits × 20 KB prompt ≈ 4 MB → ~40 KB per request across the two reads.
Change Type
Testing
packages/data-schemas:jest src/methods/agent.spec.ts(178 pass; assertions inspecting version history now pass{}togetAgent)packages/api:tsc --noEmit;jest src/agents/load.spec.ts src/agents/__tests__/load.spec.ts(43 pass; the integration spec now wiresgetAgentWithVersionCount, mirroring production)api:jestonv1.spec.js,__tests__/v1.spec.js,canAccessAgentFromBody.spec.js,addedConvo.spec.js,client.test.js,initialize.spec.js,resume.spec.js,steer.spec.js(679 pass)eslinton changed filesChecklist
Link to Devin session: https://app.devin.ai/sessions/28b7509f3db5434b88aa77685f0ae419
Open in Devin Desktop: https://app.devin.ai/desktop/session/28b7509f3db5434b88aa77685f0ae419?variant=devin
Requested by: @berry-13