Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public GrepResult grep(
String grepOpts = "-rHnF";
String globPattern = "";
if (glob != null && !glob.isBlank()) {
globPattern = "--include=" + FilesystemUtils.shellQuote(glob);
globPattern = "--include=" + FilesystemUtils.shellQuote(stripRecursivePrefix(glob));
}
String patternEscaped = FilesystemUtils.shellQuote(pattern);

Expand Down Expand Up @@ -319,7 +319,7 @@ public GrepResult grep(
@Override
public GlobResult glob(RuntimeContext runtimeContext, String pattern, String path) {
String escapedPath = FilesystemUtils.shellQuote(path != null ? path : "/");
String escapedPattern = FilesystemUtils.shellQuote(pattern);
String escapedPattern = FilesystemUtils.shellQuote(stripRecursivePrefix(pattern));

String cmd =
"find " + escapedPath + " -type f -name " + escapedPattern + " 2>/dev/null | sort";
Expand Down Expand Up @@ -379,6 +379,21 @@ public boolean exists(RuntimeContext runtimeContext, String path) {
return result.output() != null && result.output().strip().startsWith("yes");
}

/**
* Strips the recursive glob prefix {@code **/} from a pattern so it can be passed to
* tools like {@code find -name} or {@code grep --include=} that match only the filename
* portion. For example, {@code **/*.java} becomes {@code *.java}.
*
* @param pattern the glob pattern, may be {@code null}
* @return the pattern with any leading {@code **/} removed, or the original value if absent
*/
private static String stripRecursivePrefix(String pattern) {
if (pattern != null && pattern.startsWith("**/")) {
return pattern.substring(3);
}
return pattern;
}

private static String jsonEscape(String s) {
if (s == null) {
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.sandbox;

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.model.ExecuteResponse;
import io.agentscope.harness.agent.filesystem.model.FileDownloadResponse;
import io.agentscope.harness.agent.filesystem.model.FileUploadResponse;
import io.agentscope.harness.agent.filesystem.model.GlobResult;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

class BaseSandboxFilesystemTest {

private static final RuntimeContext RT = RuntimeContext.empty();

@Test
Comment thread
chickenlj marked this conversation as resolved.
Comment thread
chickenlj marked this conversation as resolved.
void glob_recursivePattern_stripsDoubleStarPrefixBeforeFindName() {
FakeSandboxFilesystem filesystem = new FakeSandboxFilesystem();

GlobResult result = filesystem.glob(RT, "**/*.md", "/workspace");

assertTrue(result.isSuccess());
assertEquals(
"find '/workspace' -type f -name '*.md' 2>/dev/null | sort",
filesystem.lastCommand);
assertEquals(
List.of("/workspace/README.md", "/workspace/docs/guide.md"),
result.matches().stream().map(match -> match.path()).collect(Collectors.toList()));
}

@Test
void glob_plainPattern_keepsFindNamePatternUnchanged() {
FakeSandboxFilesystem filesystem = new FakeSandboxFilesystem();

GlobResult result = filesystem.glob(RT, "*.md", "/workspace");

assertTrue(result.isSuccess());
assertEquals(
"find '/workspace' -type f -name '*.md' 2>/dev/null | sort",
filesystem.lastCommand);
assertEquals(
List.of("/workspace/README.md", "/workspace/docs/guide.md"),
result.matches().stream().map(match -> match.path()).collect(Collectors.toList()));
}

private static final class FakeSandboxFilesystem extends BaseSandboxFilesystem {

private String lastCommand;

@Override
public String id() {
return "fake";
}

@Override
public ExecuteResponse execute(
RuntimeContext runtimeContext, String command, Integer timeoutSeconds) {
lastCommand = command;
if ("find '/workspace' -type f -name '*.md' 2>/dev/null | sort".equals(command)) {
return new ExecuteResponse(
"/workspace/README.md\n/workspace/docs/guide.md\n", 0, false);
}
return new ExecuteResponse("", 0, false);
}

@Override
public List<FileUploadResponse> uploadFiles(
RuntimeContext runtimeContext, List<Map.Entry<String, byte[]>> files) {
return List.of();
}

@Override
public List<FileDownloadResponse> downloadFiles(
RuntimeContext runtimeContext, List<String> paths) {
return List.of();
}
}
}
Loading