agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: OverlayFilesystem.grep keeps stale lower-layer matches after upper edits

Open wangxuefen88 opened this issue 4 months ago • 3 comments

OverlayFilesystem documents that upper entries override lower entries on path collision, but grep merges results by path and line number only.

When an upper-layer file exists at the same path but no longer matches the pattern, the lower-layer match remains in the result because upperResult is empty for that path.

Expected: If a path exists in the upper layer, lower-layer matches for that path should not be returned.

Suggested fix: Before merging lower matches, exclude matches whose path exists in the upper layer. Add a regression test where an upper edit removes a lower-layer match.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88

🤖 Bot Assessment — Bug Confirmed ✅

Root cause verified in code. OverlayFilesystem.grep() (lines 196-217) merges results by path:line key:

Map<String, GrepMatch> merged = new LinkedHashMap<>();
// 1. Add all lower matches
for (GrepMatch m : lowerResult.matches()) {
    merged.put(m.path() + ":" + m.line(), m);
}
// 2. Overlay upper matches (same key overrides)
for (GrepMatch m : upperResult.matches()) {
    merged.put(m.path() + ":" + m.line(), m);
}

The gap: When an upper-layer file exists at path P but no longer matches the pattern, upperResult has no entries for P. The lower-layer matches at P survive the merge — violating the documented overlay semantics ("upper entries override lower entries on path collision").

Suggested fix: Before merging lower matches, collect the set of file paths present in upperResult and exclude lower matches whose path exists in that set. This ensures upper-layer file existence takes precedence regardless of match content.

oss-maintainer avatar Jul 28 '26 05:07 oss-maintainer

Thanks for confirming the bug.

One small clarification: collecting paths from upperResult alone would not cover the reported case, because the upper-layer file intentionally has no grep match and therefore does not appear in upperResult.

The merge logic needs to check whether each lower-match path exists in the upper filesystem independently, for example via upper.exists(...), and suppress the lower match when it does.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88

I'd like to take this one. Plan: when merging OverlayFilesystem.grep results, exclude lower-layer matches whose path exists in the upper layer (even if the upper file has no matching lines), plus a regression test.

Blue-Berrys avatar Jul 28 '26 06:07 Blue-Berrys