From ce8352f107cd6e4457c58a3622da387fddd4cd7d Mon Sep 17 00:00:00 2001 From: Blue-Berrys <75206464+Blue-Berrys@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:53:17 +0800 Subject: [PATCH] fix(harness): exclude shadowed lower grep matches in OverlayFilesystem When an upper-layer file exists at the same path, lower-layer grep hits for that path must not be returned even if the upper file no longer matches. Closes #2448. --- .../agent/filesystem/OverlayFilesystem.java | 15 +++- .../filesystem/OverlayFilesystemGrepTest.java | 81 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/OverlayFilesystemGrepTest.java diff --git a/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/OverlayFilesystem.java b/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/OverlayFilesystem.java index 5a7d52896a..d28e2d48f5 100644 --- a/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/OverlayFilesystem.java +++ b/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/OverlayFilesystem.java @@ -29,9 +29,11 @@ import io.agentscope.harness.agent.filesystem.model.WriteResult; import io.agentscope.harness.agent.filesystem.sandbox.AbstractSandboxFilesystem; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * A layered filesystem that overlays a user-specific "upper" layer on top of a shared "lower" @@ -202,10 +204,21 @@ public GrepResult grep( return lowerResult; } + // Upper-layer paths fully shadow lower-layer content for the same path, even when the + // upper file no longer matches the pattern (upperResult has no entries for that path). + Set upperShadowedPaths = new HashSet<>(); Map merged = new LinkedHashMap<>(); if (lowerResult.isSuccess() && lowerResult.matches() != null) { for (GrepMatch m : lowerResult.matches()) { - merged.put(m.path() + ":" + m.line(), m); + String matchPath = m.path(); + if (!upperShadowedPaths.contains(matchPath) + && upper.exists(runtimeContext, matchPath)) { + upperShadowedPaths.add(matchPath); + } + if (upperShadowedPaths.contains(matchPath)) { + continue; + } + merged.put(matchPath + ":" + m.line(), m); } } if (upperResult.isSuccess() && upperResult.matches() != null) { diff --git a/agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/OverlayFilesystemGrepTest.java b/agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/OverlayFilesystemGrepTest.java new file mode 100644 index 0000000000..4f2bc73158 --- /dev/null +++ b/agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/OverlayFilesystemGrepTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2024-2026 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.agentscope.harness.agent.filesystem; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.agentscope.core.agent.RuntimeContext; +import io.agentscope.harness.agent.filesystem.local.LocalFilesystem; +import io.agentscope.harness.agent.filesystem.model.GrepMatch; +import io.agentscope.harness.agent.filesystem.model.GrepResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Regression for #2448: upper-layer files must fully shadow lower-layer grep matches for the + * same path, even when the upper file no longer contains the pattern. + */ +class OverlayFilesystemGrepTest { + + private static final RuntimeContext RT = RuntimeContext.empty(); + + @Test + void grep_excludesLowerMatches_whenUpperFileExistsButNoLongerMatches( + @TempDir Path lowerRoot, @TempDir Path upperRoot) throws Exception { + Files.writeString(lowerRoot.resolve("notes.md"), "alpha SECRET beta\n"); + Files.writeString(upperRoot.resolve("notes.md"), "alpha beta\n"); + Files.writeString(lowerRoot.resolve("other.md"), "still SECRET here\n"); + + OverlayFilesystem overlay = + new OverlayFilesystem(sandboxed(upperRoot), sandboxed(lowerRoot)); + + GrepResult result = overlay.grep(RT, "SECRET", "/", null); + + assertTrue(result.isSuccess()); + List paths = + result.matches().stream().map(GrepMatch::path).collect(Collectors.toList()); + assertEquals(List.of("/other.md"), paths); + } + + @Test + void grep_upperMatchOverridesLowerMatchOnSamePathAndLine( + @TempDir Path lowerRoot, @TempDir Path upperRoot) throws Exception { + Files.writeString(lowerRoot.resolve("notes.md"), "old SECRET text"); + Files.writeString(upperRoot.resolve("notes.md"), "new SECRET text"); + + OverlayFilesystem overlay = + new OverlayFilesystem(sandboxed(upperRoot), sandboxed(lowerRoot)); + + GrepResult result = overlay.grep(RT, "SECRET", "/", null); + + assertTrue(result.isSuccess()); + assertEquals(1, result.matches().size()); + GrepMatch match = result.matches().get(0); + assertEquals("/notes.md", match.path()); + assertEquals(1, match.line()); + assertTrue(match.text().contains("new SECRET text")); + assertTrue(!match.text().contains("old SECRET text")); + } + + private static LocalFilesystem sandboxed(Path root) { + return new LocalFilesystem(root, true, 10); + } +}