Skip to content
Open
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 @@ -166,7 +166,7 @@ public List<StoreItem> search(List<String> namespace, int limit, int offset) {
Collections.sort(dataKeys);

int start = Math.min(offset, dataKeys.size());
int end = Math.min(start + limit, dataKeys.size());
int end = start + Math.min(limit, dataKeys.size() - start);
List<String> page = dataKeys.subList(start, end);

List<StoreItem> items = new ArrayList<>(page.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ void search_returnsEmpty_whenNoMatches() {
assertTrue(items.isEmpty());
}

@Test
void searchHandlesLargeLimitWithoutOverflow() {
ObjectListing listing = mock(ObjectListing.class);
COSObjectSummary first = new COSObjectSummary();
first.setKey("test/store/ns1/a.json");
COSObjectSummary second = new COSObjectSummary();
second.setKey("test/store/ns1/b.json");
when(listing.getObjectSummaries()).thenReturn(List.of(first, second));
when(mockCos.listObjects(any(ListObjectsRequest.class))).thenReturn(listing);
when(mockCos.getObjectMetadata(any(), any(String.class))).thenReturn(new ObjectMetadata());
notFound("test/store/ns1/b.version");
when(mockCos.getObject(any(GetObjectRequest.class))).thenAnswer(inv -> cosObjectWith("{}"));

assertEquals(
List.of("b"),
store.search(List.of("ns1"), Integer.MAX_VALUE, 1).stream()
.map(StoreItem::key)
.toList());
assertTrue(store.search(List.of("ns1"), Integer.MAX_VALUE, Integer.MAX_VALUE).isEmpty());
assertEquals("b", store.search(List.of("ns1"), 1, 1).get(0).key());
assertTrue(store.search(List.of("ns1"), 0, 1).isEmpty());
}

@Test
void search_returnsSinglePage() {
String prefix = "test/store/ns1/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public List<StoreItem> search(List<String> namespace, int limit, int offset) {
Collections.sort(dataKeys);

int start = Math.min(offset, dataKeys.size());
int end = Math.min(start + limit, dataKeys.size());
int end = start + Math.min(limit, dataKeys.size() - start);
List<String> page = dataKeys.subList(start, end);

List<StoreItem> items = new ArrayList<>(page.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import static org.mockito.Mockito.when;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ListObjectsV2Request;
import com.aliyun.oss.model.ListObjectsV2Result;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import io.agentscope.harness.agent.filesystem.remote.store.StoreItem;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -60,6 +63,36 @@ void builderRejectsNullClient() {
NullPointerException.class, () -> OssBaseStore.builder().bucketName("b").build());
}

@Test
void searchHandlesLargeLimitWithoutOverflow() {
ListObjectsV2Result listing = new ListObjectsV2Result();
for (String key : List.of("a", "b")) {
OSSObjectSummary summary = new OSSObjectSummary();
summary.setKey("test/store/ns1/" + key + ".json");
listing.getObjectSummaries().add(summary);
}
when(mockOss.listObjectsV2(any(ListObjectsV2Request.class))).thenReturn(listing);
when(mockOss.doesObjectExist("test-bucket", "test/store/ns1/b.json")).thenReturn(true);
when(mockOss.getObject("test-bucket", "test/store/ns1/b.json"))
.thenAnswer(
inv -> {
OSSObject object = new OSSObject();
object.setObjectContent(
new ByteArrayInputStream(
"{}".getBytes(StandardCharsets.UTF_8)));
return object;
});

assertEquals(
List.of("b"),
store.search(List.of("ns1"), Integer.MAX_VALUE, 1).stream()
.map(StoreItem::key)
.toList());
assertTrue(store.search(List.of("ns1"), Integer.MAX_VALUE, Integer.MAX_VALUE).isEmpty());
assertEquals("b", store.search(List.of("ns1"), 1, 1).get(0).key());
assertTrue(store.search(List.of("ns1"), 0, 1).isEmpty());
}

@Test
void builderRejectsBlankBucket() {
assertThrows(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public List<StoreItem> search(List<String> namespace, int limit, int offset) {
Collections.sort(matches, (a, b) -> a.key().compareTo(b.key()));

int start = Math.min(offset, matches.size());
int end = Math.min(start + limit, matches.size());
int end = start + Math.min(limit, matches.size() - start);
return matches.subList(start, end);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,20 @@ void search_includesChildNamespaceItems() {
private static BaseStore newStore() {
return new InMemoryStore();
}

@Test
void searchHandlesLargeLimitWithoutOverflow() {
BaseStore store = newStore();
store.put(List.of("ws"), "a", Map.of());
store.put(List.of("ws"), "b", Map.of());
assertEquals(
List.of("b"),
store.search(List.of("ws"), Integer.MAX_VALUE, 1).stream()
.map(StoreItem::key)
.toList());
assertTrue(store.search(List.of("ws"), Integer.MAX_VALUE, Integer.MAX_VALUE).isEmpty());
assertEquals("b", store.search(List.of("ws"), 1, 1).get(0).key());
assertEquals(2, store.search(List.of("ws"), Integer.MAX_VALUE, 0).size());
assertTrue(store.search(List.of("ws"), 0, 1).isEmpty());
}
}
2 changes: 2 additions & 0 deletions docs/dev/en/docs/harness/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ This mode **does not provide shell** — on purpose: for shell, use mode 2 (sand
| `JdbcStore` | JDBC-based, for MySQL / PostgreSQL / H2 / SQLite | `agentscope-extensions-jdbc` |
| `InMemoryStore` | In-memory, for testing | `agentscope-harness` |

For direct `BaseStore.search` calls on the in-memory, OSS and COS backends, a nonnegative offset selects the page start and a positive limit caps its length. A limit larger than the remaining result count returns the available items without overflowing the page calculation; an offset at or beyond the end returns an empty page. This does not reduce object-storage listing work: OSS and COS still enumerate matching object keys before selecting a page.

---

### Mode 2: sandbox (`SandboxFilesystemSpec` family)
Expand Down
2 changes: 2 additions & 0 deletions docs/dev/zh/docs/harness/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ HarnessAgent agent = HarnessAgent.builder()
| `JdbcStore` | 基于 JDBC,适合 MySQL / PostgreSQL / H2 / SQLite | `agentscope-extensions-jdbc` |
| `InMemoryStore` | 内存实现,适合测试 | `agentscope-harness` |

直接调用内存、OSS 和 COS 后端的 `BaseStore.search` 时,非负 offset 指定分页起点,正数 limit 限制返回数量。limit 大于剩余条目数时,只返回可用条目,不会因页尾计算溢出而失败;offset 位于结果末尾或之后时,返回空页。这不会减少对象存储的列举工作:OSS 和 COS 仍会先列举匹配的对象 key,再选取当前页。

---

### 模式 2:沙箱(`SandboxFilesystemSpec` 系列)
Expand Down
Loading