Skip to content

fix(retrieval): keep manual metadata filter reusable inside Iteration - #14849

Merged
KevinHuSh merged 1 commit into
infiniflow:mainfrom
plind-junior:fix/iteration-metadata-filter-variable
May 19, 2026
Merged

KevinHuSh merged 1 commit into
infiniflow:mainfrom
plind-junior:fix/iteration-metadata-filter-variable

Conversation

@plind-junior

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Closes #12582.

When a Retrieval component sits inside an Iteration with a manual metadata filter that references the iteration variable (e.g. {IterationItem:abc@item}), every iteration reuses the value resolved on the first pass.

Root cause: _resolve_manual_filter in agent/tools/retrieval.py mutated flt["value"] in place. The filters list passed in is the live self._param.meta_data_filter["manual"] (see apply_meta_data_filter in common/metadata_utils.py:257-261), so after the first iteration the param dict permanently held the resolved string instead of the original variable reference.

iter #1: flt["value"] = "{IterationItem:abc@item}"  →  resolved to "AI"
         after mutation: flt["value"] = "AI"        ← written back into _param

iter #2: flt["value"] = "AI"                         ← no {…} matches
         retrieval keeps filtering by "AI" forever

This PR returns a shallow copy with the resolved value instead, leaving the original filter (and its variable reference) intact for the next iteration.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Test plan

  • Build an agent: Agent (structured output → list of areas) → Iteration → Retrieval (manual filter: Area = {IterationItem/Item}) → Message. Run with a multi-area query and confirm each iteration's Retrieval result matches its own item, not the first item.
  • Regression: Retrieval with a manual metadata filter outside an Iteration still resolves the variable correctly on each request.
  • Regression: Retrieval with no metadata filter and with auto / semi_auto filters behave unchanged.

_resolve_manual_filter mutated flt["value"] in place. The filters list
passed in is the live self._param.meta_data_filter["manual"], so once
the first iteration resolved a variable reference like
{IterationItem:abc@item}, the param dict permanently held the resolved
value and every subsequent iteration reused it — Retrieval kept
matching the first item's value for the entire loop.

Return a shallow copy with the resolved value instead, leaving the
original filter dict (and the variable reference) untouched.

Closes infiniflow#12582.
@plind-junior
plind-junior marked this pull request as ready for review May 12, 2026 16:53
@coderabbitai

coderabbitai Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c014c8c-3187-4008-8d55-a5382b5db805

📥 Commits

Reviewing files that changed from the base of the PR and between 5e46457 and 086e915.

📒 Files selected for processing (1)
  • agent/tools/retrieval.py

📝 Walkthrough

Walkthrough

The _resolve_manual_filter method in the Retrieval component's knowledge base retrieval logic was mutating the input filter dictionary in place. This caused resolved filter values to persist and be reused across iteration cycles, always applying the first iteration item's resolved metadata filter. The fix creates and returns a new dictionary copy with the substituted value instead.

Changes

Filter Resolution Fix

Layer / File(s) Summary
Prevent manual filter dictionary mutation
agent/tools/retrieval.py
Added comments clarifying that _resolve_manual_filter must return a new dict. Refactored to copy the input filter dict, set the substituted "value" on the copy, and return the copy instead of mutating the original reference, preventing stale resolved values from persisting across iteration calls.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A filter that once would cling,
Now passes through without a sting,
Each loop gets fresh, no stale refrain,
Iteration's bugs no more constrain!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: preventing the manual metadata filter from being mutated, keeping it reusable within iterations.
Description check ✅ Passed The description includes detailed problem statement, root cause analysis with code references, and a comprehensive test plan matching the template structure.
Linked Issues check ✅ Passed The code changes directly address issue #12582 by preventing mutation of the manual metadata filter, allowing iteration variables to be resolved separately for each iteration.
Out of Scope Changes check ✅ Passed All changes are focused on the identified root cause in _resolve_manual_filter; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels May 12, 2026
@yingfeng
yingfeng requested a review from qinling0210 May 18, 2026 13:00
@yingfeng yingfeng added the ci Continue Integration label May 18, 2026
@yingfeng
yingfeng marked this pull request as draft May 18, 2026 13:00
@yingfeng
yingfeng marked this pull request as ready for review May 18, 2026 13:00
@codecov

codecov Bot commented May 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.16%. Comparing base (2f2d156) to head (086e915).
⚠️ Report is 117 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14849      +/-   ##
==========================================
- Coverage   95.59%   94.16%   -1.43%     
==========================================
  Files          10       10              
  Lines         703      703              
  Branches      112      112              
==========================================
- Hits          672      662      -10     
- Misses         21       25       +4     
- Partials       10       16       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KevinHuSh
KevinHuSh merged commit 7edabdf into infiniflow:main May 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The Retrieval component inside an Iteration component didn't follow the IterationItem variable during retrieval.

3 participants