Repository navigation
fix: Restrict the agent to using memory compatible with the embedding model. - #16699
Conversation
📝 WalkthroughWalkthroughThe ChangesMemory selection embedding constraint
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant MemoriesFormField
participant useDisableDifferenceEmbeddingMemory
participant useFetchAllMemoryList
participant MultiSelect
MemoriesFormField->>useDisableDifferenceEmbeddingMemory: call with name
useDisableDifferenceEmbeddingMemory->>useFetchAllMemoryList: fetch memories
useFetchAllMemoryList-->>useDisableDifferenceEmbeddingMemory: memory list
useDisableDifferenceEmbeddingMemory->>useDisableDifferenceEmbeddingMemory: read selected memoryIds via useWatch
useDisableDifferenceEmbeddingMemory->>useDisableDifferenceEmbeddingMemory: compute options with disabled state
useDisableDifferenceEmbeddingMemory-->>MemoriesFormField: options
MemoriesFormField->>MultiSelect: render with options
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
web/src/components/memories-form-field.tsx (1)
29-47: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSide effect (ref mutation) inside
useMemo.Populating
memoryCacheRef.current(lines 30-32) inside auseMemofactory mutates a ref as part of the render phase, which violates React's pure-render expectations, even though the mutation is currently idempotent. Consider moving the cache population into auseEffectkeyed onmemoryListOrigin.♻️ Suggested refactor
+ useEffect(() => { + memoryListOrigin?.forEach((memory) => { + memoryCacheRef.current.set(memory.id, memory); + }); + }, [memoryListOrigin]); + const memoryList = useMemo(() => { - memoryListOrigin?.forEach((memory) => { - memoryCacheRef.current.set(memory.id, memory); - }); - const selectedIds = Array.isArray(memoryIds) ? memoryIds : [];🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/components/memories-form-field.tsx` around lines 29 - 47, The memory cache population inside `memoryList` is mutating `memoryCacheRef.current` during `useMemo`, which should stay pure. Move the `memoryCacheRef.current.set(...)` logic out of the `useMemo` factory and into a `useEffect` that runs when `memoryListOrigin` changes, then keep `useMemo` only for deriving the merged memory list from `memoryIds`, `memoryListOrigin`, and the cache.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/components/memories-form-field.tsx`:
- Around line 54-73: The `options` memo in `memories-form-field.tsx` is
disabling already-selected memories when their `embd_id` differs from
`selectedEmbedId`, which makes legacy incompatible selections impossible to
remove in `MultiSelect`. Update the `disabled` logic so only unselected items
with mismatched `embd_id` are disabled, while existing selected values remain
removable; use the `options` mapping and the `selectedEmbedId`/`memoryList`
checks to locate the fix.
---
Nitpick comments:
In `@web/src/components/memories-form-field.tsx`:
- Around line 29-47: The memory cache population inside `memoryList` is mutating
`memoryCacheRef.current` during `useMemo`, which should stay pure. Move the
`memoryCacheRef.current.set(...)` logic out of the `useMemo` factory and into a
`useEffect` that runs when `memoryListOrigin` changes, then keep `useMemo` only
for deriving the merged memory list from `memoryIds`, `memoryListOrigin`, and
the cache.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f483dd19-7138-4e15-804e-7bc1894aac94
📒 Files selected for processing (2)
web/src/components/memories-form-field.tsxweb/src/interfaces/database/memory.ts
| const options = useMemo(() => { | ||
| return memoryList | ||
| .filter(Boolean) | ||
| .map((item: IMemory) => { | ||
| return { | ||
| label: item.name, | ||
| icon: () => ( | ||
| <RAGFlowAvatar | ||
| className="size-4" | ||
| avatar={item.avatar ?? ''} | ||
| name={item.name} | ||
| /> | ||
| ), | ||
| suffix: <MemoryLabel text={item.embd_id} />, | ||
| value: item.id, | ||
| disabled: | ||
| item.embd_id !== selectedEmbedId && selectedEmbedId !== '', | ||
| }; | ||
| }); | ||
| }, [memoryList, selectedEmbedId]); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Already-selected mismatched memories become permanently stuck (cannot be removed).
disabled is computed over memoryList, which merges memoryListOrigin with selectedMemories (line 34-46). This means any already-selected memory whose embd_id differs from memoryIds?.[0] (the anchor) is also flagged disabled: true. Per the MultiSelect implementation (web/src/components/ui/multi-select.tsx), disabled option values are excluded from toggleOption, backspace removal, and clearExtraOptions/handleClear preservation logic — so a user who has legacy or pre-existing selections with mixed embd_ids (e.g., data saved before this restriction existed) will be unable to deselect the incompatible ones via the UI. This directly undermines the PR's goal of restricting memory to embedding-compatible options, since users get trapped with unremovable incompatible selections instead of being guided to fix them.
Consider excluding already-selected ids from the disabled computation (only disable unselected options with mismatched embd_id), so users can still remove existing mismatched selections.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/components/memories-form-field.tsx` around lines 54 - 73, The
`options` memo in `memories-form-field.tsx` is disabling already-selected
memories when their `embd_id` differs from `selectedEmbedId`, which makes legacy
incompatible selections impossible to remove in `MultiSelect`. Update the
`disabled` logic so only unselected items with mismatched `embd_id` are
disabled, while existing selected values remain removable; use the `options`
mapping and the `selectedEmbedId`/`memoryList` checks to locate the fix.
Summary
fix: Restrict the agent to using memory compatible with the embedding model.